Skip to content

Static-shape ONNX export for span models - #408

Open
daescha wants to merge 2 commits into
urchade:mainfrom
daescha:static-shape-export
Open

daescha wants to merge 2 commits into
urchade:mainfrom
daescha:static-shape-export

Conversation

@daescha

@daescha daescha commented Nov 24, 2026

Copy link
Copy Markdown

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 in extract_prompt_features and the extract_*_word_embeddings functions export as NonZero. Its output shape depends on tensor values.
  • The word axis is text_lengths.max(), which is a value of an input.
  • The label axis is the number of [ENT] tokens in input_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.py
    • Word and prompt extraction scatter into a trailing dummy slot that is sliced off, in place of boolean indexing. This covers all four subtoken pooling modes and both embed_ent_token settings. Eager outputs are unchanged.
    • extract_prompts_mask(input_ids, attention_mask, class_token_index) returns the (batch, num_classes) prompt mask.
    • extract_prompt_features takes an optional prompts_mask; its width sets the number of prompts.
    • extract_prompt_features_and_word_embeddings takes an optional max_text_length.
  • gliner/modeling/base.py
    • UniEncoderSpanModel.forward sizes the word axis from span_idx.size(1) // max_width. This is the length the forward already fits words to, so results are unchanged.
    • The forward also passes an optional prompts_mask through get_representations.
  • gliner/model.py
    • The UniEncoderSpanGLiNER export has a new input prompts_mask, with shape (batch_size, num_classes) and dtype int64.
    • _runtime_inputs builds prompts_mask from input_ids for any exported graph that declares it. Export and the ONNX Runtime / OpenVINO inference path both use it.
  • tests/test_static_shape_export.py
    • Compares the new extraction against the boolean-indexing reference on random padded batches.
    • Exports the extraction functions and a small UniEncoderSpanGLiNER.
    • Asserts the graph has neither NonZero nor shape arguments computed from input values.
    • Checks ONNX Runtime output against eager PyTorch.

ONNX interface and compatibility

  • New UniEncoderSpanGLiNER exports take one extra input, prompts_mask. It is 1 for 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.
  • GLiNER's own runtime adds prompts_mask only for graphs that declare it. Existing exports, such as onnx-community/gliner_multi-v2.1, keep working unchanged.
  • External consumers of new exports must feed prompts_mask. Consumers of older exports need no change.
  • The PyTorch API and eager results are unchanged.
  • Other architectures (token, bi-encoder, relex) keep their current export interface. The extraction change also removes NonZero from 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.

Comparison Result
New export vs export of the unchanged code, ONNX Runtime logits max abs diff 0 (8 single texts and one 8-text batch)
predict_entities with the new export vs the PyTorch model same entities, max score diff 3e-5
NonZero nodes 3 → 0
Shape arguments computed from input values none in the new export
Test suite 731 passed, 16 skipped

The new export also compiles and runs under GoMLX (XLA and pure-Go backends). Entities are identical to ONNX Runtime there.

Follow-ups

  • Apply the same prompts_mask input to the token, bi-encoder and relex exports.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant