Skip to content

Commit 6304528

Browse files
committed
Tighten GPT4O evidence validation
Require GPT4O oracle evidence to match the dispatch commit and reject template processors unless they exactly implement the GGUF-enabled BOS insertion. Apply repository formatting from the all-files lint pass. Co-authored-by: Copilot App <[email protected]> Copilot-Session: d5d1d772-b7f9-4243-b8de-a13634d0da7b Signed-off-by: Justin Chu <[email protected]>
1 parent c3b5db9 commit 6304528

4 files changed

Lines changed: 72 additions & 18 deletions

File tree

‎src/mobius/integrations/gguf/_tokenizer.py‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,12 @@ def _template_processor_proves_bos_insertion(
10591059
)
10601060

10611061

1062-
def _canonicalize_gpt4o_pipeline(tokenizer_json: dict[str, Any], *, pre: str) -> None:
1062+
def _canonicalize_gpt4o_pipeline(
1063+
tokenizer_json: dict[str, Any],
1064+
metadata: Mapping[str, Any],
1065+
*,
1066+
pre: str,
1067+
) -> None:
10631068
pre_tokenizer = tokenizer_json.get("pre_tokenizer")
10641069
source_pre_tokenizer = {
10651070
**_GPT4O_PRE_TOKENIZER,
@@ -1073,13 +1078,8 @@ def _canonicalize_gpt4o_pipeline(tokenizer_json: dict[str, Any], *, pre: str) ->
10731078
}
10741079
post_processor = tokenizer_json.get("post_processor")
10751080
valid_post_processor = post_processor == _GPT4O_POST_BYTE_LEVEL or (
1076-
isinstance(post_processor, Mapping)
1077-
and post_processor.get("type") == "Sequence"
1078-
and isinstance(post_processor.get("processors"), list)
1079-
and len(post_processor["processors"]) == 2
1080-
and post_processor["processors"][0] == _GPT4O_POST_BYTE_LEVEL
1081-
and isinstance(post_processor["processors"][1], Mapping)
1082-
and post_processor["processors"][1].get("type") == "TemplateProcessing"
1081+
metadata.get("tokenizer.ggml.add_bos_token") is True
1082+
and _template_processor_proves_bos_insertion(tokenizer_json, metadata)
10831083
)
10841084
if (
10851085
tokenizer_json.get("normalizer") is not None
@@ -1122,7 +1122,7 @@ def _validate_pinned_tokenizer(
11221122
model["merges"] = metadata.get("tokenizer.ggml.merges")
11231123
config = _canonicalize_gpt4o_tokenizer_config(metadata, config)
11241124
if policy is not None and policy.pre_type == "GPT4O":
1125-
_canonicalize_gpt4o_pipeline(tokenizer_json, pre=pre)
1125+
_canonicalize_gpt4o_pipeline(tokenizer_json, metadata, pre=pre)
11261126
raw_tokenizer = json.dumps(
11271127
tokenizer_json, ensure_ascii=False, separators=(",", ":")
11281128
).encode()

‎src/mobius/integrations/gguf/_tokenizer_evidence.py‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,17 @@ def __post_init__(self) -> None:
191191
)
192192
if self.reconstruct_gpt4o_from_gguf and witness.pre_type != "GPT4O":
193193
raise ValueError("GGUF-native reconstruction is supported only for GPT4O evidence")
194+
if witness.pre_type == "GPT4O" and self.llamacpp_oracle is None:
195+
raise ValueError("GPT4O evidence requires an exact pinned llama.cpp oracle")
194196
if self.llamacpp_oracle is not None:
195197
commit, case_count, digest = self.llamacpp_oracle
198+
dispatch = tokenizer_alias_evidence().get(self.pre_identifier)
196199
if (
197200
re.fullmatch(r"[0-9a-f]{40}", commit) is None
198201
or case_count <= 0
199202
or re.fullmatch(r"[0-9a-f]{64}", digest) is None
203+
or dispatch is None
204+
or commit != dispatch.source_commit
200205
):
201206
raise ValueError("llama.cpp oracle evidence identity is invalid")
202207

@@ -551,11 +556,44 @@ def source(self) -> GGUFTokenizerSource:
551556
(" spaced text\n", (32, 25_156, 32, 5_272, 10)),
552557
(
553558
"你好,世界!", # noqa: RUF001
554-
(228, 189, 160, 229, 165, 189, 239, 188, 140, 57_632, 150, 231, 149, 140, 239, 188, 129),
559+
(
560+
228,
561+
189,
562+
160,
563+
229,
564+
165,
565+
189,
566+
239,
567+
188,
568+
140,
569+
57_632,
570+
150,
571+
231,
572+
149,
573+
140,
574+
239,
575+
188,
576+
129,
577+
),
555578
),
556579
(
557580
"Café — κόσμος 🚀",
558-
(67, 1_063, 1_238, 461, 12_887, 12_562, 6_076, 7_938, 14_917, 32, 240, 159, 154, 128),
581+
(
582+
67,
583+
1_063,
584+
1_238,
585+
461,
586+
12_887,
587+
12_562,
588+
6_076,
589+
7_938,
590+
14_917,
591+
32,
592+
240,
593+
159,
594+
154,
595+
128,
596+
),
559597
),
560598
("<|user|>hello<|end|>", (65_537, 257, 12_227, 65_536)),
561599
(

‎src/mobius/integrations/gguf/_tokenizer_evidence_test.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ def test_evidence_schema_accepts_scores_instead_of_bpe_merges() -> None:
9494
)
9595

9696

97+
def test_gpt4o_evidence_requires_oracle_at_dispatch_commit() -> None:
98+
evidence = tokenizer_evidence("talkie-13b-q4-native-tokenizer")
99+
assert evidence is not None
100+
with pytest.raises(ValueError, match=r"requires an exact pinned llama\.cpp oracle"):
101+
dataclasses.replace(evidence, llamacpp_oracle=None)
102+
with pytest.raises(ValueError, match="oracle evidence identity is invalid"):
103+
dataclasses.replace(
104+
evidence,
105+
llamacpp_oracle=("0" * 40, 444, evidence.llamacpp_oracle[2]),
106+
)
107+
108+
97109
def test_registry_derived_census_has_a_concrete_disposition_for_every_alias() -> None:
98110
census = tokenizer_route_census()
99111
assert len(census) == 87

‎src/mobius/integrations/gguf/_tokenizer_test.py‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,7 @@ def _gpt4o_payloads(metadata: dict) -> dict[str, bytes]:
643643
{"SpecialToken": {"id": "<bos>", "type_id": 1}},
644644
{"Sequence": {"id": "B", "type_id": 1}},
645645
],
646-
"special_tokens": {
647-
"<bos>": {"id": "<bos>", "ids": [2], "tokens": ["<bos>"]}
648-
},
646+
"special_tokens": {"<bos>": {"id": "<bos>", "ids": [2], "tokens": ["<bos>"]}},
649647
},
650648
],
651649
}
@@ -708,19 +706,25 @@ def test_gpt4o_rejects_pipeline_or_template_bos_mismatch(mismatch: str) -> None:
708706
tokenizer = json.loads(payloads["tokenizer.json"])
709707
if mismatch == "regex":
710708
tokenizer["pre_tokenizer"]["pretokenizers"][0]["pattern"]["Regex"] += "x"
711-
message = "pipeline differs"
712709
elif mismatch == "decoder":
713710
tokenizer["decoder"]["trim_offsets"] = False
714-
message = "pipeline differs"
715711
else:
716712
tokenizer["post_processor"]["processors"][1]["single"].reverse()
717-
message = "cannot prove GGUF add_bos_token"
718713
payloads["tokenizer.json"] = json.dumps(tokenizer).encode()
719714

720-
with pytest.raises(ValueError, match=message):
715+
with pytest.raises(ValueError, match="pipeline differs"):
721716
_tokenizer._validate_pinned_tokenizer(metadata, payloads)
722717

723718

719+
def test_gpt4o_rejects_template_bos_when_gguf_disables_it() -> None:
720+
metadata = _metadata(pre="kanana2")
721+
metadata.pop("tokenizer.ggml.scores")
722+
metadata["tokenizer.ggml.add_bos_token"] = False
723+
724+
with pytest.raises(ValueError, match="pipeline differs"):
725+
_tokenizer._validate_pinned_tokenizer(metadata, _gpt4o_payloads(metadata))
726+
727+
724728
def test_pipeline_mismatch_leaves_no_partial_output(tmp_path: Path, monkeypatch) -> None:
725729
metadata = _metadata(pre="smollm")
726730
payloads = _pinned_payloads(metadata)

0 commit comments

Comments
 (0)