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): refuse GLiClass items whose labels leave no room for the document (#427)

svonavab5778bd2fcb3
diff --git a/packages/sie_sdk/README.md b/packages/sie_sdk/README.md
index 5c6be6b91..9f22be40b 100644
--- a/packages/sie_sdk/README.md
+++ b/packages/sie_sdk/README.md
@@ -149,7 +149,10 @@ instruction or examples, each row's count is capped at the model window minus
that row's label prompt, unless the document count alone is already higher.
An item whose document pushes the labels out of the window, in any of its rows,
comes back with an `INPUT_TOO_LONG` error in its `error` field and is not
-billed. The other items still succeed.
+billed. The other items still succeed. Models that read the labels before
+the document count only the part of the document they read, and a row whose
+labels leave no room for the document fails its item the same way, instead of
+being scored without the document.
On a CUDA server, the operator can load the DeBERTa-based GLiClass models with
CUDA graphs, which cut the CPU time spent launching kernels. With `bucketed`
diff --git a/packages/sie_server/README.md b/packages/sie_server/README.md
index 02eeceb89..1c4771784 100644
--- a/packages/sie_server/README.md
+++ b/packages/sie_server/README.md
@@ -71,6 +71,16 @@ is already higher. An item refused because its document pushes the labels out
of the window returns a per-item `INPUT_TOO_LONG` error and counts nothing. A
request that sends no instruction or examples is counted exactly as before.
+The models that read the labels before the document (`prompt_first` in the
+checkpoint's config: every shipped GLiClass model except `gliclass-small-v1.0`,
+`gliclass-base-v1.0` and `gliclass-large-v1.0`) cut the document to the room
+the label prompt and instruction leave, and count only the part of the document
+they read, as `truncate_text` would. When the labels leave no room for any of
+the document, each item returns a per-item `INPUT_TOO_LONG` error and counts
+nothing, rather than being scored without its document. With `overflow_policy`
+`truncate_text` or `error`, such a request is refused with `INPUT_TOO_LONG`, as
+before.
+
The instruction and each example text may be at most 2,048 characters, and
together with the example labels at most 8,192 characters. Up to 32 examples
are accepted, and they must leave room for the document in the model window.
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 ba2e57506..bad634272 100644
--- a/packages/sie_server/src/sie_server/adapters/gliclass/__init__.py
+++ b/packages/sie_server/src/sie_server/adapters/gliclass/__init__.py
@@ -153,6 +153,14 @@ _ERR_ITEM_LABELS_TRUNCATED = (
"The document pushes the labels out of the gliclass model's max sequence length. "
"Shorten the document, or send options.overflow_policy='truncate_text'."
)
+# Models that read the labels first (``prompt_first``) cut the document to the
+# room the label prompt and instruction leave. An item is refused unless its
+# document keeps at least this many tokens, rather than scored without it.
+_MIN_DOCUMENT_TOKENS = 1
+_ERR_ITEM_NO_ROOM_FOR_DOCUMENT = (
+ "The labels leave no room for the document in the gliclass model's max sequence length, "
+ "so the model would not read it. Send fewer or shorter labels."
+)
_ERR_ITEM_DOCUMENT_TOO_SPARSE = (
"The part of the document the gliclass model reads spans more than {limit} characters, too many to "
"encode once per label group. Shorten the document, send fewer groups, or send "
@@ -169,7 +177,10 @@ class _RequestLayout:
last marker sits at ``last_marker``. ``context_tokens`` counts the
billable instruction and example texts. ``overhead_tokens`` is everything
a row encodes besides its document and special tokens: the label prompt,
- the instruction and the formatted examples.
+ the instruction and the formatted examples. ``document_room`` is the most
+ document tokens the model reads: the window for a model that reads the
+ document first, and for one that reads the labels first the window minus
+ the label prompt and instruction that precede the document.
"""
prompt_first: bool
@@ -178,12 +189,7 @@ class _RequestLayout:
last_marker: int
context_tokens: int
overhead_tokens: int
-
- def labels_fit(self, document_tokens: int) -> bool:
- """Whether every label marker survives truncation next to this document."""
- if self.prompt_first:
- return self.last_marker < self.window
- return document_tokens + self.last_marker < self.window
+ document_room: int
@dataclass
@@ -835,9 +841,10 @@ class GLiClassAdapter(BaseAdapter):
texts, normalized_labels, overflow_policy, prompt=prompt, examples=examples, tokens=tokens
)
layout = self._request_layout(normalized_labels, prompt, examples, tokens)
- fits = self._items_fit(texts, layout, normalized_labels, prompt, examples, tokens=tokens)
+ failures = self._item_failures(texts, layout, normalized_labels, prompt, examples, tokens=tokens)
+ fits = [failure is None for failure in failures]
input_token_counts = self._input_token_counts(texts, prompt, examples, layout, fits, tokens=tokens)
- errors = self._item_errors(fits)
+ errors = self._item_errors(failures)
kept = [index for index, ok in enumerate(fits) if ok]
kept_texts = [texts[index] for index in kept]
@@ -1194,10 +1201,12 @@ class GLiClassAdapter(BaseAdapter):
Each row is what a request with ``labels`` set to the group's labels
encodes: its overflow policy, window check and metering apply per
- row. An item whose labels do not fit in one of its rows fails with
- ``INPUT_TOO_LONG``, is billed nothing and is not checked against the
- remaining groups; the other items still run. ``texts`` holds each
- document's readable prefix, or None for one whose readable part
+ row. An item whose labels do not fit in one of its rows, or whose
+ row leaves no room for its document, fails with ``INPUT_TOO_LONG``,
+ is billed nothing and is not checked against the remaining groups;
+ the other items still run. A group whose labels cannot fit the
+ window refuses the request whatever the documents. ``texts`` holds
+ each document's readable prefix, or None for one whose readable part
exceeds the per-group character bound.
The first group's context is tokenized and checked alone, so a
@@ -1212,9 +1221,9 @@ class GLiClassAdapter(BaseAdapter):
item_rows: list[list[str]] = [[] for _ in range(item_count)]
item_lengths: list[list[int]] | None = [[] for _ in range(item_count)]
for group, (_, group_labels) in enumerate(label_groups):
+ # Once every item has failed, the remaining groups still run their
+ # request checks (overflow policy, label window) on no rows.
alive = [index for index, failure in enumerate(failures) if failure is None]
- if not alive:
- break
if group == 1:
self._contexts(
tokens,
@@ -1235,7 +1244,7 @@ class GLiClassAdapter(BaseAdapter):
indices=alive,
)
layout = self._request_layout(group_labels, prompt, row_examples, tokens)
- row_fits = self._items_fit(row_texts, layout, group_labels, prompt, row_examples, tokens=tokens)
+ row_failures = self._item_failures(row_texts, layout, group_labels, prompt, row_examples, tokens=tokens)
row_counts = self._input_token_counts(
row_texts, prompt, row_examples, layout, [True] * len(alive), tokens=tokens
)
@@ -1245,8 +1254,8 @@ class GLiClassAdapter(BaseAdapter):
if row_lengths is None:
item_lengths = None
for position, index in enumerate(alive):
- if not row_fits[position]:
- failures[index] = _ERR_ITEM_LABELS_TRUNCATED
+ if row_failures[position] is not None:
+ failures[index] = row_failures[position]
continue
item_rows[index].append(row_texts[position])
if counts is not None and row_counts is not None:
@@ -1825,16 +1834,18 @@ class GLiClassAdapter(BaseAdapter):
"GLiClass instruction, examples and labels leave no room for the document in the "
f"model's {window}-token window; shorten the instruction or send fewer examples"
)
+ prompt_first = bool(getattr(config, "prompt_first", False))
return _RequestLayout(
- prompt_first=bool(getattr(config, "prompt_first", False)),
+ prompt_first=prompt_first,
window=window,
label_tokens=len(label_ids),
last_marker=markers[-1],
context_tokens=prompt_tokens + example_text_tokens,
overhead_tokens=len(label_ids) + prompt_tokens + examples_tokens,
+ document_room=window - len(label_ids) - prompt_tokens if prompt_first else window,
)
- def _items_fit(
+ def _item_failures(
self,
texts: list[str],
layout: _RequestLayout | None,
@@ -1843,29 +1854,42 @@ class GLiClassAdapter(BaseAdapter):
examples: list[dict[str, Any]] | None,
*,
tokens: _RequestTokens | None = None,
- ) -> list[bool]:
- """Whether each item's label markers survive truncation.
-
- Documents are tokenized once, on their own. Tokenizing a document next
- to the label prompt can differ from that by a token or two (a trailing
- space before a marker, for example), so items within a few tokens of
- the window edge are checked exactly on their fused encoding. Counting
- up to that margin past the window means a document that fills the
- window by itself is refused without the exact check.
+ ) -> list[str | None]:
+ """Why each item cannot be scored as sent (an ``INPUT_TOO_LONG`` message), or None.
+
+ A model that reads the labels first keeps every label and cuts the
+ document to the room left after the label prompt and instruction.
+ When that room is under ``_MIN_DOCUMENT_TOKENS``, the model would read
+ none of the document, so every item is refused. Otherwise a longer
+ document is cut, as ``truncate_text`` would cut it.
+
+ A model that reads the document first keeps the document and loses
+ the labels past the window, so an item is refused when its document
+ pushes a label marker out. Documents are tokenized once, on their own.
+ Tokenizing a document next to the label prompt can differ from that by
+ a token or two (a trailing space before a marker, for example), so
+ items within a few tokens of the window edge are checked exactly on
+ their fused encoding. Counting up to that margin past the window means
+ a document that fills the window by itself is refused without the
+ exact check.
"""
- if layout is None or layout.prompt_first or self._tokenizer is None:
- return [True] * len(texts)
+ if layout is None or self._tokenizer is None:
+ return [None] * len(texts)
+ if layout.prompt_first:
+ failure = None if layout.document_room >= _MIN_DOCUMENT_TOKENS else _ERR_ITEM_NO_ROOM_FOR_DOCUMENT
+ return [failure] * len(texts)
tokens = tokens or _RequestTokens(self._tokenizer)
- fits: list[bool] = []
+ failures: list[str | None] = []
for text, ids in zip(texts, tokens.cut(texts, layout.window + _FIT_MARGIN_TOKENS), strict=True):
estimate = len(ids) + layout.last_marker
if estimate < layout.window - _FIT_MARGIN_TOKENS:
- fits.append(True)
+ fits = True
elif estimate >= layout.window + _FIT_MARGIN_TOKENS:
- fits.append(False)
+ fits = False
else:
- fits.append(self._labels_survive(text, list(ids[: layout.window]), labels, prompt, examples))
- return fits
+ fits = self._labels_survive(text, list(ids[: layout.window]), labels, prompt, examples)
+ failures.append(None if fits else _ERR_ITEM_LABELS_TRUNCATED)
+ return failures
def _labels_survive(
self,
@@ -1887,17 +1911,17 @@ class GLiClassAdapter(BaseAdapter):
return ids.count(marker) >= text_ids.count(marker) + len(labels)
@staticmethod
- def _item_errors(fits: list[bool]) -> list[ExtractItemError | None] | None:
- """Per-item INPUT_TOO_LONG for items whose labels would be cut off.
+ def _item_errors(failures: list[str | None]) -> list[ExtractItemError | None] | None:
+ """Per-item INPUT_TOO_LONG for items that cannot be scored as sent (see ``_item_failures``).
Reporting these per item keeps one oversized document from failing
every request batched with it.
"""
- if all(fits):
+ if all(failure is None for failure in failures):
return None
return [
- None if ok else ExtractItemError(code=ErrorCode.INPUT_TOO_LONG.value, message=_ERR_ITEM_LABELS_TRUNCATED)
- for ok in fits
+ None if failure is None else ExtractItemError(code=ErrorCode.INPUT_TOO_LONG.value, message=failure)
+ for failure in failures
]
def _input_token_counts(
@@ -1918,12 +1942,19 @@ class GLiClassAdapter(BaseAdapter):
pipeline's marker tokens are not billed. With an instruction or
examples, a row's total is capped at the model window minus the
label prompt, the most free-form text it can encode, unless the
- document count alone is already higher. Items refused because their
- labels would be cut off are billed nothing.
+ document count alone is already higher. A document counts at most
+ the tokens of it the model reads (``document_room``), so a model that
+ reads the labels first counts what its window keeps after them, as
+ ``truncate_text`` would. Items refused because their labels would be
+ cut off, or because the labels leave no room for the document, are
+ billed nothing.
"""
counts = self._doc_input_token_counts(texts, tokens)
if counts is None:
return None
+ if layout is not None:
+ readable = max(layout.document_room, 0) + self._special_count
+ counts = [min(count, readable) for count in counts]
if prompt or examples:
if layout is not None:
context_tokens = layout.context_tokens
diff --git a/packages/sie_server/tests/adapters/test_gliclass_request_options.py b/packages/sie_server/tests/adapters/test_gliclass_request_options.py
index 1f2dd1b0b..81f153db2 100644
--- a/packages/sie_server/tests/adapters/test_gliclass_request_options.py
+++ b/packages/sie_server/tests/adapters/test_gliclass_request_options.py
@@ -820,6 +820,25 @@ class TestSeparateGroupEncoding:
}
assert output.input_token_counts == [(7 + 2) + (5 + 2)]
+ def test_a_group_whose_labels_leave_no_room_fails_every_item(self) -> None:
+ # Labels-first model keeping 7 content tokens: the urgency row's label
+ # prompt (5 tokens) leaves room for the document, the topic row's
+ # (7 tokens: 3 markers, 3 labels and SEP) leaves none.
+ adapter, pipe = _group_adapter(max_length=9)
+
+ output = adapter.extract(
+ [Item(text="Charged twice, fix today"), Item(text="Dark mode someday?")], options=_group_options()
+ )
+
+ assert pipe.prepare_calls == []
+ assert output.errors is not None
+ for error in output.errors:
+ assert error is not None
+ assert error.code == "INPUT_TOO_LONG"
+ assert "no room for the document" in error.message
+ assert output.data == [{}, {}]
+ assert output.input_token_counts == [0, 0]
+
def test_an_item_whose_labels_miss_any_row_fails_alone(self) -> None:
# Text-first model keeping 10 content tokens: the six-word document leaves
# room for both urgency markers but pushes the third topic marker out.
@@ -1168,6 +1187,73 @@ class TestFlatLabelOverflow:
with pytest.raises(InputTooLongError):
adapter.extract([Item(text="The app crashes")], labels=list(_LABELS))
+ # The label prompt is 9 tokens (3 markers, 5 label words and SEP) with its
+ # last marker at index 5. At max_length 11 the window keeps 9 tokens: every
+ # marker fits, and nothing is left for the document.
+ @pytest.mark.parametrize(("max_length", "refused"), [(11, True), (12, False)])
+ def test_labels_first_items_are_refused_when_the_labels_leave_no_room_for_the_document(
+ self, max_length: int, refused: bool
+ ) -> None:
+ adapter, pipe = self._adapter(prompt_first=True, max_length=max_length)
+ items = [Item(text="The app crashes"), Item(text="Charged twice")]
+
+ output = adapter.extract(items, labels=list(_LABELS))
+
+ if not refused:
+ assert output.errors is None
+ assert [call["texts"] for call in pipe.prepare_calls] == [["The app crashes", "Charged twice"]]
+ # The model reads one token of each document: usage counts that token and CLS/SEP.
+ assert output.input_token_counts == [3, 3]
+ return
+ assert pipe.prepare_calls == []
+ assert output.errors is not None
+ for error in output.errors:
+ assert error is not None
+ assert error.code == "INPUT_TOO_LONG"
+ assert "no room for the document" in error.message
+ assert output.classifications == [[], []]
+ assert output.input_token_counts == [0, 0]
+
+ @pytest.mark.parametrize(("words", "refused"), [(3, False), (4, True)])
+ def test_documents_first_items_at_the_same_window_keep_their_document(self, words: int, refused: bool) -> None:
+ # The document comes first, so it is read in full; an item is refused
+ # only when its document pushes the last marker (index 5) out: 3 + 5 < 9.
+ adapter, pipe = self._adapter(prompt_first=False, max_length=11)
+ text = " ".join(["word"] * words)
+
+ output = adapter.extract([Item(text=text)], labels=list(_LABELS))
+
+ if not refused:
+ assert output.errors is None
+ assert [call["texts"] for call in pipe.prepare_calls] == [[text]]
+ return
+ assert pipe.prepare_calls == []
+ assert output.errors is not None
+ assert output.errors[0] is not None
+ assert output.errors[0].code == "INPUT_TOO_LONG"
+ assert "pushes the labels out" in output.errors[0].message
+ assert output.input_token_counts == [0]
+
+ def test_truncate_text_still_refuses_labels_that_leave_no_room_for_the_document(self) -> None:
+ adapter, _ = self._adapter(prompt_first=True, max_length=11)
+
+ with pytest.raises(InputTooLongError, match="exceeds max_sequence_length"):
+ adapter.extract(
+ [Item(text="The app crashes")], labels=list(_LABELS), options={"overflow_policy": "truncate_text"}
+ )
+
+ def test_truncate_text_cuts_the_document_to_the_room_the_labels_leave(self) -> None:
+ adapter, pipe = self._adapter(prompt_first=True, max_length=12)
+
+ output = adapter.extract(
+ [Item(text="The app crashes")], labels=list(_LABELS), options={"overflow_policy": "truncate_text"}
+ )
+
+ assert output.errors is None
+ assert len(pipe.prepare_calls) == 1
+ assert len(pipe.prepare_calls[0]["texts"][0].split()) == 1
+ assert output.input_token_counts == [1 + 2]
+
def test_a_marker_inside_the_document_is_not_an_overflow(self) -> None:
adapter, _ = self._adapter(prompt_first=False)
@@ -1209,8 +1295,17 @@ class TestFlatLabelOverflow:
output = adapter.extract([Item(text=text)], labels=list(_LABELS), instruction="one two three")
# label prompt = 3 markers + 5 label words + SEP = 9 tokens; cap = 40 - 9 = 31.
- # The document count alone is 32 (30 words + CLS/SEP), so it stands.
- assert output.input_token_counts == [32]
+ # The model reads 38 - 9 - 3 = 26 of the 30 document words after the labels
+ # and instruction, so it counts 26 + 3 + CLS/SEP = 31, what a truncate_text
+ # request counts.
+ assert output.input_token_counts == [31]
+ truncated = adapter.extract(
+ [Item(text=text)],
+ labels=list(_LABELS),
+ instruction="one two three",
+ options={"overflow_policy": "truncate_text"},
+ )
+ assert truncated.input_token_counts == [31]
short = adapter.extract([Item(text="doc doc")], labels=list(_LABELS), instruction="one two three")
assert short.input_token_counts == [4 + 3]