Skip to content

armgit / sie

Public Imported from GitHub
0

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

16.0 KB
Raw Blame
"""The bounded word window GLiNER-family adapters read documents through."""

from __future__ import annotations

import re
from collections.abc import Iterator, Sequence
from typing import Any

import pytest
from sie_server.adapters._word_window import (
    ATTENTION_BUDGET,
    DEBERTA_MAX_DOCUMENT_SUBWORDS,
    MAX_DOCUMENT_SUBWORDS,
    MAX_WORD_CHARS,
    SUBWORDS_PER_WORD,
    DocumentWindow,
    SubwordCounter,
    WindowedSplitter,
    document_windows,
    merge_window_spans,
    pieces,
    plan_forwards,
    read_window,
    split_word_counter,
    subword_budget,
    window_rows,
)

_WORD = re.compile(r"\w+(?:[-_]\w+)*|\S")


def words(text: str) -> Iterator[tuple[str, int, int]]:
    for match in _WORD.finditer(text):
        yield match.group(), match.start(), match.end()


def per_char(batch: Sequence[str]) -> list[int]:
    """One subword per character, as DeBERTa tokenizers split a run of accented letters."""
    return [len(word) for word in batch]


def window(text: str, *, max_words: int = 100, max_subwords: int = 1000) -> Any:
    return read_window(text, words(text), max_words=max_words, max_subwords=max_subwords, count_subwords=per_char)


def test_short_words_pass_through() -> None:
    result = window("Priya Raman works at Novartis.")

    assert [word for word, _, _ in result.words] == ["Priya", "Raman", "works", "at", "Novartis", "."]
    assert result.subwords == 26
    assert result.cut is None


def test_a_long_word_is_read_in_pieces_with_their_own_offsets() -> None:
    text = "id " + "x" * (2 * MAX_WORD_CHARS + 10) + " end"

    result = window(text, max_subwords=10_000)

    spans = [(start, end) for _, start, end in result.words]
    assert spans == [(0, 2), (3, 259), (259, 515), (515, 525), (526, 529)]
    assert all(word == text[start:end] for word, start, end in result.words)
    assert result.cut is None


def test_reading_stops_before_the_word_that_passes_the_budget() -> None:
    text = "aaaa bbbb cccc dddd"

    result = window(text, max_subwords=10)

    assert [word for word, _, _ in result.words] == ["aaaa", "bbbb"]
    assert result.subwords == 8
    assert result.cut == 14  # the end of "cccc": text[:14] reads the same window


def test_reading_stops_inside_a_long_word_at_the_budget() -> None:
    text = "go " + "y" * 1000 + " tail"

    result = window(text, max_subwords=600)

    assert [(start, end) for _, start, end in result.words] == [(0, 2), (3, 259), (259, 515)]
    assert result.subwords == 514
    assert result.cut == 1003  # the end of the long word
    assert window(text[: result.cut], max_subwords=600) == result


def test_the_first_word_is_always_read() -> None:
    result = window("x" * 1000, max_subwords=5)

    assert [(start, end) for _, start, end in result.words] == [(0, MAX_WORD_CHARS)]
    assert result.subwords == MAX_WORD_CHARS


def test_reading_stops_after_max_words() -> None:
    text = "a b c d e"

    assert [word for word, _, _ in window(text, max_words=3).words] == ["a", "b", "c"]
    assert window(text, max_words=3).cut == 5
    assert window("a b c", max_words=3).cut is None
    assert window("a b c  ", max_words=3).cut is None


def test_pieces_of_a_word_whose_lowercase_changes_length_follow_the_text() -> None:
    # gliner2 2.x lowercases each word; "\u0130" lowercases to two characters.
    text = "\u0130" * 300
    lowered = [(text.lower(), 0, 300)]

    result = list(pieces(lowered, text))

    assert [(start, end) for _, start, end, _ in result] == [(0, 256), (256, 300)]
    assert [word for word, _, _, _ in result] == [text[0:256].lower(), text[256:300].lower()]
    assert all(word_end == 300 for _, _, _, word_end in result)


def test_the_windowed_splitter_passes_arguments_through() -> None:
    calls: list[tuple[str, tuple[Any, ...], dict[str, Any]]] = []

    def splitter(text: str, *args: Any, **kwargs: Any) -> Iterator[tuple[str, int, int]]:
        calls.append((text, args, kwargs))
        return words(text)

    windowed = WindowedSplitter(splitter, max_words=2, max_subwords=100, count_subwords=per_char)

    assert list(windowed("one two three", lower=True)) == [("one", 0, 3), ("two", 4, 7)]
    assert calls == [("one two three", (), {"lower": True})]


def test_the_counter_counts_each_word_once_and_forgets_the_oldest() -> None:
    counted: list[list[str]] = []

    def count(batch: Sequence[str]) -> list[int]:
        counted.append(list(batch))
        return [len(word) for word in batch]

    counter = SubwordCounter(count, size=2)

    assert counter(["ab", "c", "ab"]) == [2, 1, 2]
    assert counter(["c", "ddd"]) == [1, 3]
    assert counter(["ab"]) == [2]
    assert counted == [["ab", "c"], ["ddd"], ["ab"]]


class SplitWordTokenizer:
    """Two subwords per word, reported through ``word_ids`` as fast tokenizers do."""

    def __call__(self, words: list[str], *, is_split_into_words: bool, add_special_tokens: bool) -> Any:
        assert is_split_into_words
        assert not add_special_tokens
        ids = [index for index, word in enumerate(words) for _ in range(2 if word else 0)]
        return type("Encoding", (), {"word_ids": lambda self: ids})()


def test_the_counter_keeps_no_word_longer_than_a_piece() -> None:
    counter = SubwordCounter(per_char)
    long_word = "q" * (MAX_WORD_CHARS + 1)

    assert counter([long_word, "ab", long_word]) == [MAX_WORD_CHARS + 1, 2, MAX_WORD_CHARS + 1]
    assert counter._cache == {"ab": 2}


def test_split_words_are_counted_as_the_tokenizer_encodes_them() -> None:
    assert split_word_counter(SplitWordTokenizer())(["a", "", "bc"]) == [2, 0, 2]


def test_a_slow_tokenizer_is_counted_word_by_word() -> None:
    class Slow:
        def __call__(self, *args: Any, **kwargs: Any) -> Any:
            raise ValueError("word_ids() is not available when using non-fast tokenizers")

        def tokenize(self, word: str) -> list[str]:
            return list(word)

    assert split_word_counter(Slow())(["ab", "cde"]) == [2, 3]


def test_the_budget_allows_subwords_per_word_up_to_the_encoder_cap() -> None:
    assert subword_budget(512) == SUBWORDS_PER_WORD * 512
    assert subword_budget(512, per_word=4) == 2048
    assert subword_budget(2048) == MAX_DOCUMENT_SUBWORDS
    assert subword_budget(2048, {"model_type": "deberta-v2"}) == DEBERTA_MAX_DOCUMENT_SUBWORDS
    assert subword_budget(384, {"model_type": "deberta-v2"}) == SUBWORDS_PER_WORD * 384
    bert = {"model_type": "bert", "position_embedding_type": "absolute", "max_position_embeddings": 512}
    assert subword_budget(386, bert) == 508
    relative = {"model_type": "deberta-v2", "max_position_embeddings": 512}
    assert subword_budget(384, relative) == SUBWORDS_PER_WORD * 384
    # transformers 5 writes no position_embedding_type for BERT-style encoders.
    assert subword_budget(386, {"model_type": "bert", "max_position_embeddings": 512}) == 508
    assert subword_budget(386, {"model_type": "xlm-roberta", "max_position_embeddings": 514}) == 510
    bert_relative = {"model_type": "bert", "position_embedding_type": "relative_key", "max_position_embeddings": 512}
    assert subword_budget(386, bert_relative) == SUBWORDS_PER_WORD * 386
    rotary = {"model_type": "modernbert", "max_position_embeddings": 8192}
    assert subword_budget(2048, rotary) == MAX_DOCUMENT_SUBWORDS


def test_a_batch_that_fits_runs_as_it_is() -> None:
    assert plan_forwards([600] * 12, rows_per_pass=12) is None
    assert plan_forwards([3000] * 12, rows_per_pass=1) is None
    assert plan_forwards([], rows_per_pass=8) is None


@pytest.mark.parametrize("rows_per_pass", [8, 12])
def test_long_rows_run_in_passes_within_the_budget(rows_per_pass: int) -> None:
    rows = [3100, 40, 2000, 40, 1500, 3100, 40, 40, 900, 40, 40, 4096]

    groups = plan_forwards(rows, rows_per_pass=rows_per_pass)

    assert groups is not None
    assert sorted(index for group in groups for index in group) == list(range(len(rows)))
    for group in groups:
        longest = max(rows[index] for index in group)
        assert len(group) <= rows_per_pass
        assert len(group) == 1 or len(group) * longest**2 <= ATTENTION_BUDGET
    for group in groups:
        lengths = [rows[index] for index in group]
        assert max(lengths) <= 2 * min(lengths)  # short rows are not padded to long ones


def test_a_long_row_does_not_pad_the_short_rows_it_would_fit_with() -> None:
    rows = [2000] + [50] * 11

    groups = plan_forwards(rows, rows_per_pass=8)

    assert groups == [[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11], [0]]


def splitter(max_words: int, max_subwords: int = 1000) -> WindowedSplitter:
    return WindowedSplitter(words, max_words=max_words, max_subwords=max_subwords, count_subwords=per_char)


def test_a_document_read_whole_is_one_window_of_all_of_it() -> None:
    text = "Priya Raman works at Novartis.  "

    assert document_windows(text, splitter(10)) == [DocumentWindow(0, len(text), len(text) - 2, None)]


def test_windows_share_words_and_read_every_word() -> None:
    text = " ".join(f"w{index}" for index in range(100))

    windows = document_windows(text, splitter(20), overlap_words=5)

    assert windows is not None
    read = [text[window.start : window.read_end].split() for window in windows]
    assert read[0] == [f"w{index}" for index in range(20)]
    assert read[1][:5] == read[0][-5:]
    assert [window.overlap for window in windows] == [None] + [5] * (len(windows) - 1)
    assert {word for words_read in read for word in words_read} == set(text.split())
    assert windows[-1].end == len(text)


def test_windows_advance_through_a_word_read_in_pieces() -> None:
    text = "x" * (10 * MAX_WORD_CHARS) + " tail"

    windows = document_windows(text, splitter(10, max_subwords=MAX_WORD_CHARS))

    # One piece a window: nothing is shared, and every piece is read.
    assert windows is not None
    assert [window.start for window in windows] == [index * MAX_WORD_CHARS for index in range(10)] + [len(text) - 4]
    assert [window.overlap for window in windows] == [None] + [0] * 10


def test_windows_through_a_word_longer_than_a_slice_read_each_piece_once() -> None:
    text = "x" * 50_000 + " tail"
    reader = splitter(10, max_subwords=MAX_WORD_CHARS)

    windows = document_windows(text, reader, max_windows=1000)

    assert windows is not None
    pieces_read = -(-50_000 // MAX_WORD_CHARS)
    # The last window reads the last piece and "tail".
    assert [window.start for window in windows] == [index * MAX_WORD_CHARS for index in range(pieces_read)]
    assert windows[-1].read_end == len(text)
    for window in windows[:-1]:
        # Each window's text reads the piece the window reads, from a bounded prefix of the document.
        assert window.end - window.start <= 20_000
        (word,) = reader(text[window.start : window.end])
        assert (word[1] + window.start, word[2] + window.start) == (window.start, window.read_end)


def test_windows_of_a_document_longer_than_the_window_limit_are_none() -> None:
    text = " ".join(f"w{index}" for index in range(100))

    assert document_windows(text, splitter(20), overlap_words=10, max_windows=8) is None
    assert document_windows(text, splitter(20), overlap_words=10, max_windows=9) is not None


@pytest.mark.parametrize("budget", [1, 256, 512, 768])
def test_windows_keep_long_word_pieces_when_a_window_starts_on_a_hyphen(budget: int) -> None:
    text = "x" * MAX_WORD_CHARS + ("-" + "x" * (MAX_WORD_CHARS - 1)) * 7 + " tail"
    reader = splitter(10, max_subwords=budget)
    expected = [(word, start, end) for word, start, end, _ in pieces(words(text), text)]

    windows = document_windows(text, reader)

    assert windows is not None
    rows, _, overlaps = window_rows([text], [windows])
    seen = []
    for row, window, overlap in zip(rows, windows, overlaps, strict=True):
        read = list(reader(row))
        seen.extend((word, window.start + start, window.start + end) for word, start, end in read[overlap or 0 :])
        # The first word may exceed the budget, but subsequent words may not.
        assert len(read) == 1 or sum(len(word) for word, _, _ in read) <= budget
    assert seen == expected


def test_identical_window_texts_keep_independent_document_word_boundaries() -> None:
    suffix = "-" + "x" * (MAX_WORD_CHARS - 1)
    texts = ["x" * MAX_WORD_CHARS + suffix, suffix]
    reader = splitter(2, max_subwords=MAX_WORD_CHARS)
    plans = [document_windows(text, reader) for text in texts]

    rows, owners, _ = window_rows(texts, plans)

    assert owners == [0, 0, 1]
    assert rows[1] == rows[2] == suffix
    assert [word for word, _, _ in reader(rows[1])] == [suffix]
    assert [word for word, _, _ in reader(rows[2])] == ["-", suffix[1:]]
    # Reading the other document does not alter an already-planned window.
    assert [word for word, _, _ in reader(rows[1])] == [suffix]


def test_windows_of_a_long_document_count_only_a_bounded_prefix() -> None:
    text = " ".join(f"w{index}" for index in range(200_000))
    batches: list[list[str]] = []

    def count(batch: Sequence[str]) -> list[int]:
        batches.append(list(batch))
        return per_char(batch)

    reader = WindowedSplitter(words, max_words=64, max_subwords=1000, count_subwords=count)

    windows = document_windows(text, reader, max_windows=10)

    assert windows is None
    assert all(len(batch) <= 64 for batch in batches)
    # First window: 64 words, then 32 new words in each of 9 windows. At most
    # one counting block may be prefetched; none of the huge remainder is tokenized.
    assert sum(map(len, batches)) <= 64 + 9 * 32 + 64


def span(start: int, end: int, score: float, label: str = "person") -> dict[str, Any]:
    return {"text": "", "label": label, "score": score, "start": start, "end": end}


TEXT = "0123456789" * 3
# Windows reading characters 0-20 and 10-30 (the merge uses offsets only).
TWO = [DocumentWindow(0, 20, 20, None), DocumentWindow(10, 30, 30, 2)]


def merged(spans: list[list[dict[str, Any]]], *, flat_ner: bool = True, multi_label: bool = False) -> list[Any]:
    result = merge_window_spans(TWO, spans, TEXT, flat_ner=flat_ner, multi_label=multi_label)
    return [(one["start"], one["end"], one["label"], one["score"], one["text"]) for one in result]


def test_one_window_spans_are_returned_as_they_are() -> None:
    spans = [span(3, 1, 0.5, "odd")]

    assert merge_window_spans([TWO[0]], [spans], TEXT, flat_ner=True, multi_label=False) == spans


def test_merged_spans_move_to_document_offsets_and_keep_their_best_score() -> None:
    result = merged([[span(12, 15, 0.6), span(2, 4, 0.7)], [span(2, 5, 0.9), span(12, 15, 0.8)]])

    assert result == [(2, 4, "person", 0.7, "23"), (12, 15, "person", 0.9, "234"), (22, 25, "person", 0.8, "234")]


def test_a_span_cut_at_an_edge_gives_way_to_the_neighbour_that_reads_it_whole() -> None:
    # 18-20 ends at the first window's edge; the second window found 18-22.
    result = merged([[span(18, 20, 0.99)], [span(8, 12, 0.7)]])

    assert result == [(18, 22, "person", 0.7, "8901")]


def test_a_span_at_an_edge_stays_when_the_neighbour_found_nothing_there() -> None:
    assert merged([[span(18, 20, 0.99)], []]) == [(18, 20, "person", 0.99, "89")]
    # A neighbour's span of another label does not replace it.
    assert merged([[span(18, 20, 0.99)], [span(8, 12, 0.7, "place")]]) == [(18, 20, "person", 0.99, "89")]


def test_a_span_at_the_start_of_a_window_gives_way_to_the_window_before() -> None:
    result = merged([[span(8, 12, 0.5)], [span(0, 2, 0.99)]])

    assert result == [(8, 12, "person", 0.5, "8901")]


def test_overlapping_spans_from_two_windows_resolve_as_gliner_resolves_them() -> None:
    first = [span(12, 16, 0.6, "person")]
    second = [span(4, 8, 0.8, "place"), span(5, 6, 0.9, "thing")]

    # flat_ner: highest score first, nothing overlapping what is kept.
    assert merged([first, second]) == [(15, 16, "thing", 0.9, "5")]
    # Nested: a span inside a kept one stays, a partly overlapping one does not.
    assert merged([first, second], flat_ner=False) == [(14, 18, "place", 0.8, "4567"), (15, 16, "thing", 0.9, "5")]


def test_multi_label_keeps_one_span_with_several_labels() -> None:
    spans = [[span(12, 15, 0.6, "person")], [span(2, 5, 0.9, "place")]]

    assert merged(spans) == [(12, 15, "place", 0.9, "234")]
    assert merged(spans, multi_label=True) == [(12, 15, "place", 0.9, "234"), (12, 15, "person", 0.6, "234")]