Skip to content

llama: add BertForTokenClassification support - #19725

Open
giladgd wants to merge 5 commits into
ggml-org:masterfrom
giladgd:modernBertForTokenClassification
Open

llama: add BertForTokenClassification support#19725
giladgd wants to merge 5 commits into
ggml-org:masterfrom
giladgd:modernBertForTokenClassification

Conversation

@giladgd

@giladgd giladgd commented Feb 19, 2026

Copy link
Copy Markdown
Contributor

This PR adds token classification support for BertForTokenClassification and ModernBertForTokenClassification, which allows me to use this model and also this one.
I added a new pooling type for token-level classification since it makes llama_get_embeddings_ith return an array with a length of llama_model_n_cls_out(model) (instead of llama_model_n_embd_out(model)).


AI disclosure: I used Codex for assistance but wrote most of the code myself.

@giladgd

giladgd commented Feb 26, 2026

Copy link
Copy Markdown
Contributor Author

@CISC I saw you reviewed and added support for classification models in the past, so it'd be great if you could review this PR

richiejp added a commit to richiejp/LocalAI that referenced this pull request Jun 12, 2026
…k queue

Score and TokenClassify previously bypassed llama.cpp's slot scheduler and
drove llama_decode directly on the shared llama_context. That required
static mutexes, a fatal conflict_guard tripwire that aborted the process if
they ever overlapped the slot loop, and a config-validation rule forbidding
score/token_classify from sharing a llama-cpp model config with
chat/completion/embeddings.

Both now ride the server task queue:

  - patch 0001 gains PR ggml-org/llama.cpp#19725's tools/server hunks
    (re-based to the pin): send_embedding treats TOKEN_CLS pooling as
    token-level, so embedding tasks return one raw n_cls logit row per
    token. TokenClassify submits its overlapping windows as embedding
    tasks and keeps log-softmax/Viterbi/span stitching in the handler.
  - new patch 0006 adds SERVER_TASK_TYPE_SCORE: the batch builder flags
    candidate positions for logits, a per-chunk harvest accumulates
    log_softmax(logits)[next_token] after every decode (a scored region
    can span multiple n_batch chunks, which also lifts the input cap to
    n_ctx), and send_score emits the summed result at DONE_PROMPT.
    Prompt-cache reuse is clamped to score_start-1 so the position
    predicting the first scored token is always re-decoded; beyond that,
    cross-candidate prompt-KV reuse comes free from the slot prompt
    cache, retiring the old re-decode-per-candidate perf TODO.
    server_n_outputs_max gains bounded headroom (+64) for score outputs
    and the batch fill enforces that budget per decode - without this the
    upstream n_outputs_max ceiling (sized for last-token-only logits)
    asserts, which also means the old direct-decode Score violated it at
    the current pin.
  - the conflict_guard machinery, both RPC mutexes, and the
    ModelConfig.Validate usecase-conflict blocks are deleted; a single
    llama-cpp config may now combine score/token_classify with
    chat/completion/embeddings, and the GGUF importer guard keeps its
    behavior (reserved models stay out of chat pickers) with the
    rationale updated.

Verified locally against the CPU grpc-server build: TokenClassify spans
match the known-good output with 4 concurrent calls returning identical
results; score rankings are sane with exact per-token logprob counts,
bitwise-identical single-chunk results across n_batch 512 vs 64, chunk-
invariant (fp-noise) multi-chunk results including a 212-token candidate
through the output-budget path; Score and Predict run concurrently with
no abort; pooled embeddings still L2-normalize (dims 768, norm 1.0).
All six patches apply at pin 7c158fbb with --fuzz=0, idempotently.

Assisted-by: claude-code:claude-fable-5 [Claude Code]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples python python script changes server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant