GLiNER bi-large v2.0 (ONNX)
ONNX export of knowledgator/gliner-bi-large-v2.0
— the bi-encoder span architecture, which encodes text and entity labels with two separate
transformers and matches them with a dot product. Because the label side never sees the text, its
embeddings can be computed once for a label set and reused across every document.
As far as we can tell this is the first published ONNX export of any GLiNER bi-encoder.
Upstream's convert_to_onnx does not work on this family — see
#225 and
#237, both open, both dying with
AttributeError: 'NoneType' object has no attribute 'max' at text_lengths.max(). That is a bug
in the export harness rather than a limitation of the model: it calls GLiNER.forward with a
positional tuple, and text_lengths only ever exists to size a padding bound for batches. Exporting
the two branches directly never reaches the line.
Exported for SecuPi's unstructured AI service (SPAI), which runs it through ONNX Runtime from Java. Published so that service can fetch it without a PyTorch dependency; anyone else is welcome to use it under the same licence as the base model.
Files
| file | what it is |
|---|---|
onnx/span_encoder.onnx |
ettin-encoder-400m (ModernBERT) + 1024→768 projection + word pooling + the markerV0 span head. Emits the whole span grid. |
onnx/label_encoder.onnx |
bge-base-en-v1.5 + label projection + masked mean pooling + the prompt head. |
gliner_bi_config.json |
shapes, graph paths, and the decode contract (span_end_inclusive, the word-splitter regex, lowercase_text: false). |
tokenizer.json, tokenizer_config.json |
the text tokenizer (ModernBERT byte-level BPE, cased). |
labels_tokenizer/ |
the label tokenizer (BAAI/bge-small-family WordPiece, uncased). Bundled because the base model repo does not contain it — the library downloads it separately at load time. |
config.json |
a two-key stub so the directory looks like a model repo. Nothing reads it. |
There is no .onnx.data: both graphs stay under the 2 GB protobuf limit.
How to run it
The text side carries no label prompt — that is the point of a bi-encoder. <<ENT>> and
<<SEP>> appear in the tokenizer's added vocab but the embedding matrix was never resized for
them (from_pretrained declines to resize for bi-encoders), so they are unusable vestiges. Do not
emit them.
Split the raw text into words with
\w+(?:[-_]\w+)*|\S. Do not lowercase — the text encoder is cased. In Java compile this withPattern.UNICODE_CHARACTER_CLASS; Java's\wis ASCII-only by default, which splitsMüllerinto three words and shifts every later offset.input_ids = [CLS] + concat(subtokens of " " + word) + [SEP], and recordword_first_index[w], the absolute index of wordw's first sub-token (so[0] == 1). Pooling is first-sub-token; the graph does the gather.Run
span_encoder.onnx:input shape input_ids[1, L]int64attention_mask[1, L]int64word_first_index[1, W]int64output shape span_rep[1, W, 12, 768]float32span_rep[0][w][k]is the span covering wordswthroughw+kinclusive. The grid is built inside the graph, so there is no flat span list whose ordering you could get wrong. Entries wherew + k >= Ware invalid and must be skipped.Tokenize the labels with
labels_tokenizer/, padded to the batch maximum, and runlabel_encoder.onnx:input shape labels_input_ids[C, S]int64labels_attention_mask[C, S]int64output shape prompts_embedding[C, 768]float32This output depends only on the label text, so cache it per label set.
scores[w][k][c] = sigmoid(dot(span_rep[0][w][k], prompts_embedding[c])).Take the argmax label per span; drop anything below the threshold.
Sort survivors by score descending and greedily keep a span only if it overlaps nothing already kept — flat NER, across labels, on inclusive ranges.
Char offsets are
wordCharStart[w] .. wordCharEnd[w+k].
A runnable version of exactly the above, which diffs its output against native PyTorch, ships with
the export tooling as verify_gbi2_export.py.
Export notes
- The RNN is dead code on this path.
gliner_config.jsonsayshas_rnn: trueand the checkpoint carries 1.58M trained bidirectional LSTM weights, butBaseBiEncoderModelnever calls them — only the uni-encoder and streaming paths do. A forward-hook count confirms zero invocations, and the exporter re-checks it and refuses to run if that ever changes, because a graph that silently omitted a live layer would still return plausible entities. SpanMarkerV0bakes the word count. Itsforwardends with.view(B, span_idx.size(1) // max_width, max_width, D), where the middle term is a Python int read off the traced shape — so a graph traced on 37 words rejects every other count. Reshaping that axis with-1is exactly equivalent and keeps it dynamic.- ModernBERT did not need a length patch. Its local-attention mask and position ids are built
from
arange(seq_len), which can trace to a constant, so the export is verified from 4 to 242 tokens rather than assumed. It generalises; no patch was added. - Traced in the middle of the verification range on purpose: tracing long and verifying short can pass on a baked table by slicing.
Verified against native PyTorch
Before writing anything, the exporter reproduces the model's own logits from just these two graphs'
contents — bit-exactly, 0.000e+00, at 3/18/48 words and 1/3/8 labels.
| graph | worst max|diff| | swept over |
|---|---|---|
span_encoder |
5.39e-05 | 1, 2, 11, 12, 13, 37, 120 words (4–242 tokens) |
label_encoder |
6.14e-06 | 1, 2, 5, 19, 47 labels |
And end to end, ONNX + the decode above returns identical entity sets to
model.predict_entities() on 6/6 test documents.
Provenance
Exported from knowledgator/gliner-bi-large-v2.0 at revision
71eec1d5fe57684cd6669ad8a1e1e3e7401a9a55, with gliner==0.2.28, transformers==4.57.0,
torch==2.13.0, opset 18.
Text encoder: jhu-clsp/ettin-encoder-400m.
Label encoder: BAAI/bge-base-en-v1.5.
Licence
Apache 2.0, inherited from the base model. This repository redistributes the same weights in a different serialisation; all credit for the model belongs to Knowledgator.