Skip to content

Add GLiNER2.5-Decide, a zero-shot decision model, for macOS - #592

Open
carinapeng wants to merge 4 commits into
apple:mainfrom
carinapeng:carina/gliner2-decide
Open

carinapeng wants to merge 4 commits into
apple:mainfrom
carinapeng:carina/gliner2-decide

Conversation

@carinapeng

Copy link
Copy Markdown
Contributor

Type of Change

  • Bug fix
  • Small feature (agreed in an issue)
  • New model (export recipe only)
  • New model (export recipe and new runtime support)
  • Documentation update

Motivation and Context

Addresses the model request in #449.

Adds GLiNER2.5-Decide (fastino, Apache-2.0), a zero-shot decision model for text: a text plus caller-named tasks and labels go in, and one probability per label comes out of a single forward pass, with several tasks per call. It is the first decision model in the repo.

  • Model definition (models/macos/gliner2.py): a DeBERTa-v2 encoder with disentangled attention and GLiNER2's classification head. The relative-position terms are realigned with a reshape instead of a gather, and the position projections are folded at load. Attention is written out because DeBERTa's additive position bias cannot go through the boolean-mask SDPA composite.
  • Export (coreai.classification.export gliner2.5-decide): writes a bundle of kind classification. It contains model.aimodel with one batch-1 function per token length (128, 256, 512) over shared weights and 64 label slots, the tokenizer, and metadata.json, which describes how a host builds the token row.
  • Run (coreai.classification.run): builds the row, runs the bundle on the Core AI runtime and returns label probabilities.

New model

Numerical equivalence

Measured against the upstream gliner2 package (2.2.0, PyTorch float32, CPU) on 25 requests (36 tasks, 30–444 tokens). Token ids match upstream on all 25 requests.

Runtime max |Δlogit| max |Δp| argmax selections
PyTorch definition, float32 4.3e-6 8.1e-7 36/36 —
Core AI float32, GPU 1.1e-5 2.6e-6 36/36 36/36
Core AI float32, CPU 6.7e-6 1.2e-6 36/36 36/36
Core AI float16, GPU 6.2e-3 2.1e-3 36/36 36/36

Performance

Batch 1, full-length rows, tokenization excluded, median of 50 runs after 10 warm-up runs, same Mac.

Backend 128 tokens 256 tokens 512 tokens % of reference throughput
Core AI float16, GPU (this PR) 6.1 ms 9.1 ms 17.0 ms 160% / 141% / 163%
gliner2-mlx 0.1.2, float32 (reference) 9.7 ms 12.8 ms 27.6 ms 100%
gliner2 PyTorch MPS, float16 19.7 ms 30.9 ms 77.8 ms —

The float32 bundle reaches 89% / 64% / 66% of the reference.

Example output

$ uv run coreai.classification.run exports/gliner2.5-decide_float16 \
    --text "I was charged twice for my order, please refund one of the charges." \
    --tasks '{"intent": ["refund", "shipping", "account"]}'
{
  "intent": {
    "selected": [
      "refund"
    ],
    "probabilities": {
      "refund": 0.9974806304129946,
      "shipping": 0.0007869329128077359,
      "account": 0.0017324366741975287
    }
  }
}

(Core AI float16 bundle, macOS GPU.)

Limits

  • Up to 512 tokens per call. Trailing words beyond that are dropped and counted in the output.
  • Up to 64 labels per call.
  • No long-text chunking.
  • Neural Engine placement is not supported for this macOS bundle; it runs on the GPU.
  • swift-transformers 1.4.0 has no DebertaV2Tokenizer entry and maps GLiNER's marker tokens to unknown. The bundle contract therefore has hosts write the markers by id, as coreai.classification.run does.

Testing

  • Tested locally

  • Added/updated tests

  • Added/updated docs

  • test_gliner2.py (19 tests): encoder vs Hugging Face DebertaV2Model and head vs a reference implementation on tiny random configs.

  • test_classification_gliner2.py (31 tests): contract, row layout, export dry runs, Core AI CPU and GPU runs of a tiny bundle, and recorded upstream outputs (run when the model is in the local Hugging Face cache).

  • Registry and embedding-contract tests pass.

  • ruff check and ruff format --check are clean, and uv.lock is unchanged.

GLiNER2's classification path scores caller-named labels against a text
in one forward pass. The macOS definition is a DeBERTa-v2 encoder with
disentangled attention (relative-position terms realigned with a reshape
instead of a gather, position projections folded at load) and the GLiNER2
classification head; the span and count heads are not loaded. Attention
is written out because DeBERTa's additive position bias cannot pass
through the boolean-mask SDPA composite.

Tests compare the encoder with Hugging Face DebertaV2Model and the head
with a reference implementation on tiny random configurations.
`coreai.classification.export gliner2.5-decide` writes a bundle of kind
"classification": model.aimodel with one batch-1 function per token
length (128, 256, 512) over shared weights and 64 label slots, the
tokenizer, and metadata.json describing how a host builds the token row
(marker tokens are written by id). float16 compute is the default;
float32 is available. `coreai.classification.run` builds the row, runs
the bundle on the Core AI runtime and returns a probability per label.
Adds the registry entry (macOS) and asset metadata.

Tests cover the contract, the row layout, export dry runs, Core AI CPU
and GPU runs of a tiny bundle, and recorded outputs of the upstream
gliner2 package (run when the model is in the local Hugging Face cache).
Export and run steps, bundle layout, limits, parity against the upstream gliner2 package and latency on the macOS GPU. Lists the model under Decision Models in models/README.md.
@carinapeng
carinapeng requested a review from a team as a code owner December 6, 2026 05:37
- Task specs must be a list of strings or a string-to-string mapping;
  other values raise ValueError instead of splitting strings or crashing.
- validate_block requires each marker role to carry an integer id and the
  text block to carry pad_token_id, append_period, lowercase_words and
  word_pattern. The runner rejects bundles whose text rules differ from
  the ones the encoder implements.
- The compute precision is resolved before an existing bundle is removed
  or a snapshot is fetched.
- classify returns the number of dropped trailing words; the CLI writes it
  to the JSON output and warns on stderr when text was truncated.
- The exporter reuses resolve_snapshot (now taking allow_patterns) and a
  shared find_repo_root in export/bundle.py, also used by the embedding
  exporter.
- Removes the unused token_pooling field and corrects the attention
  comment. The model card documents truncation and request limits;
  models/README.md lists the exporter under --include-debug-info.

Tests: malformed task specs and metadata, precision checked before
overwrite, over-long text on CPU and GPU and in the CLI, resolve_snapshot
patterns, find_repo_root, the recorded-output test pinned to the model
revision and skipped only when the snapshot is not cached, and float16
logits compared with float32.
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