Repository navigation
Conversation
Scatter into a dropped trailing slot instead of boolean indexing, so the ONNX graph has no NonZero nodes. Outputs match the boolean-indexing version; the new test checks eager and ONNX Runtime results.
Word count comes from span_idx, label count from a new (batch, num_classes) prompts_mask input. The runtime builds prompts_mask only for graphs that declare it, so older exports keep working. Exported gliner_multi-v2.1 logits match the previous export bit for bit.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Static-shape ONNX export for span models
Motivation
Ahead-of-time compilers such as XLA (JAX, GoMLX), TensorRT and similar runtimes need every tensor shape to follow from input shapes. The current span-model export breaks this in three places:
torch.where/ boolean indexing inextract_prompt_featuresand theextract_*_word_embeddingsfunctions export asNonZero. Its output shape depends on tensor values.text_lengths.max(), which is a value of an input.[ENT]tokens ininput_ids, which is also a value.ONNX Runtime handles all three. Static-shape compilers reject the graph. For example, GoMLX fails with "cannot materialize constant/static value ... depends on non-constant: inputs=[text_lengths]".
Changes
gliner/modeling/utils.pyembed_ent_tokensettings. Eager outputs are unchanged.extract_prompts_mask(input_ids, attention_mask, class_token_index)returns the(batch, num_classes)prompt mask.extract_prompt_featurestakes an optionalprompts_mask; its width sets the number of prompts.extract_prompt_features_and_word_embeddingstakes an optionalmax_text_length.gliner/modeling/base.pyUniEncoderSpanModel.forwardsizes the word axis fromspan_idx.size(1) // max_width. This is the length the forward already fits words to, so results are unchanged.prompts_maskthroughget_representations.gliner/model.pyUniEncoderSpanGLiNERexport has a new inputprompts_mask, with shape(batch_size, num_classes)and dtype int64._runtime_inputsbuildsprompts_maskfrominput_idsfor any exported graph that declares it. Export and the ONNX Runtime / OpenVINO inference path both use it.tests/test_static_shape_export.pyUniEncoderSpanGLiNER.NonZeronor shape arguments computed from input values.ONNX interface and compatibility
UniEncoderSpanGLiNERexports take one extra input,prompts_mask. It is1for each label slot in a row, so a batch with labels[person, org, city]passes ones of shape(batch, 3). Only its shape matters to the graph.prompts_maskonly for graphs that declare it. Existing exports, such as onnx-community/gliner_multi-v2.1, keep working unchanged.prompts_mask. Consumers of older exports need no change.NonZerofrom their graphs.Parity
The parity data uses
urchade/gliner_multi-v2.1, exported with torch 2.15 and transformers 5.22, and 8 texts in English, German and Spanish with 3 or 4 labels.predict_entitieswith the new export vs the PyTorch modelNonZeronodesThe new export also compiles and runs under GoMLX (XLA and pure-Go backends). Entities are identical to ONNX Runtime there.
Follow-ups
prompts_maskinput to the token, bi-encoder and relex exports.