From 993e953f612c412956d1cf6952f02db5a308b10f Mon Sep 17 00:00:00 2001 From: AJ Roetker Date: Sat, 27 Dec 2025 01:34:29 -0800 Subject: [PATCH] fix(onnx): use explicit max_width in SpanMLP.view() for ONNX export The SpanMLP class used an inferred dimension (-1) in the view() call, which causes ONNX export to fail with a reshape size mismatch error. When exporting to ONNX with dynamic shapes, PyTorch's ONNX exporter cannot reliably infer the -1 dimension, resulting in malformed reshape dimensions like [1, 128, 12, 512] for an input of shape [1, 128, 512]. This fix: 1. Stores max_width as an instance variable 2. Uses self.max_width explicitly in view() instead of -1 This makes SpanMLP consistent with other span representation classes (SpanMarker, SpanMarkerV0, SpanMarkerV1) which already use explicit max_width in their view() calls. --- gliner/modeling/span_rep.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gliner/modeling/span_rep.py b/gliner/modeling/span_rep.py index 677447e..573b8e5 100644 --- a/gliner/modeling/span_rep.py +++ b/gliner/modeling/span_rep.py @@ -75,6 +75,7 @@ def __init__(self, hidden_size, max_width): """ super().__init__() + self.max_width = max_width self.mlp = nn.Linear(hidden_size, hidden_size * max_width) def forward(self, h, *args): @@ -95,7 +96,7 @@ def forward(self, h, *args): span_rep = self.mlp(h) - span_rep = span_rep.view(B, L, -1, D) + span_rep = span_rep.view(B, L, self.max_width, D) return span_rep.relu()