Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

fix(server): score a lone GLiClass label on its own in single-label mode (#616)

Filip Makraduli45c3fee8a4d6
diff --git a/packages/sie_sdk/README.md b/packages/sie_sdk/README.md
index 7074a0001..17f2a124d 100644
--- a/packages/sie_sdk/README.md
+++ b/packages/sie_sdk/README.md
@@ -64,7 +64,9 @@ GLiClass models (`knowledgator/gliclass-*` and the `knowledgator/opir-*`
guardrail models) score text against labels passed with each request. Every
label comes back in `classifications`, sorted by score. By default the scores
form one distribution that sums to 1; `options={"classification_type":
-"multi-label"}` scores each label independently instead.
+"multi-label"}` scores each label independently instead. A request with a
+single label is always scored independently: its score is the probability that
+the label applies, not a distribution of one.
```python
ticket = Item(text="I was charged twice and support has not replied in three days.")
diff --git a/packages/sie_server/src/sie_server/adapters/gliclass/__init__.py b/packages/sie_server/src/sie_server/adapters/gliclass/__init__.py
index ff551f76e..0c13e4d80 100644
--- a/packages/sie_server/src/sie_server/adapters/gliclass/__init__.py
+++ b/packages/sie_server/src/sie_server/adapters/gliclass/__init__.py
@@ -12,7 +12,9 @@ Performance note (Dec 2025):
Scoring runs the gliclass model directly with the library's own prompt assembly
and tokenization, in the pipeline's sub-batches of eight rows, and applies the
-pipeline's softmax or sigmoid. The scores equal the pipeline's. The adapter skips
+pipeline's softmax or sigmoid. The scores equal the pipeline's, except that a
+single-label request with one label gets that label's sigmoid (the multi-label
+score) where the pipeline's softmax returns 1.0 for any text. The adapter skips
the pipeline's progress bar and its per-label reads from the device, and it
tokenizes each document once for its length checks and metering.
@@ -987,7 +989,9 @@ class GLiClassAdapter(BaseAdapter):
forward passes of at most ``batch_size``, by default the pipeline's
sub-batches. A single-label row gets a softmax over the label slots a
call with only its labels would have; a multi-label row a sigmoid per
- label. Each forward's scores come back in one device-to-host copy.
+ label. A single-label row with one label gets that label's sigmoid,
+ since a softmax over one label would be 1.0 for any text. Each
+ forward's scores come back in one device-to-host copy.
``graphs`` chooses how forwards run on CUDA (see ``cuda_graphs``).
"""
pipe = self._require_pipe()
@@ -1013,6 +1017,10 @@ class GLiClassAdapter(BaseAdapter):
raise InputTooLongError(_ERR_INPUT_TOO_LONG)
if probs is not None:
rows.append(probs[row, :count])
+ elif count == 1:
+ # A softmax over one label is 1.0 whatever the text, so a
+ # lone label is scored on its own, as multi-label scores it.
+ rows.append(torch.sigmoid(logits[row, :1]))
elif width == logits.shape[-1]:
rows.append(torch.softmax(logits[row], dim=-1)[:count])
else:
diff --git a/packages/sie_server/tests/adapters/test_gliclass_parity.py b/packages/sie_server/tests/adapters/test_gliclass_parity.py
index 0cbe6a5ea..0c86946d1 100644
--- a/packages/sie_server/tests/adapters/test_gliclass_parity.py
+++ b/packages/sie_server/tests/adapters/test_gliclass_parity.py
@@ -167,6 +167,22 @@ def test_labels_requests_score_bit_for_bit_like_the_pipeline(
assert _scores(output) == rig.pipeline_scores(_TEXTS, _LABELS, classification_type, **context)
+def test_one_label_is_scored_on_its_own_in_single_label_mode(rig: _Rig) -> None:
+ # The pipeline's single-label softmax over one label is 1.0 whatever the text.
+ # The adapter scores a lone label as multi-label does: its own sigmoid.
+ label: list[str] = [_LABELS[0]]
+ items = [Item(text=text) for text in _TEXTS]
+
+ default = rig.adapter.extract(items, labels=label)
+ single = rig.adapter.extract(items, labels=label, options={"classification_type": "single-label"})
+ multi = rig.adapter.extract(items, labels=label, options={"classification_type": "multi-label"})
+
+ assert rig.pipeline_scores(_TEXTS, label, "single-label") == [{label[0]: 1.0}] * len(_TEXTS)
+ assert _scores(multi) == rig.pipeline_scores(_TEXTS, label, "multi-label")
+ assert _scores(default) == _scores(single) == _scores(multi)
+ assert all(0.0 < row[label[0]] < 1.0 for row in _scores(single))
+
+
@pytest.mark.parametrize("classification_type", ["single-label", "multi-label"])
def test_separate_groups_score_like_one_labels_request_per_group(rig: _Rig, classification_type: str) -> None:
items = [Item(text=text) for text in _TEXTS[:4]]