Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ tests/data/gguf_gemma4_tokenizer_oracle.json text eol=lf
tests/data/gguf_gemma4_qualification_inputs.tar.xz binary
tests/data/gguf_tokenizer_artifact_evidence.json text eol=lf
tests/data/gguf_tokenizer_artifact_inputs.tar.xz binary
testdata/evidence/**/*.json text eol=lf
111 changes: 104 additions & 7 deletions src/mobius/integrations/gguf/_draft_runtime_evidence_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,125 @@

import hashlib
import json
import os
import re
import shutil
import subprocess
from pathlib import Path

_REPO_ROOT = Path(__file__).parents[4]
_EVIDENCE_DIR = _REPO_ROOT / "testdata" / "evidence"


def _evidence() -> dict:
path = (
Path(__file__).parents[4]
/ "testdata"
/ "evidence"
/ "gguf_draft_runtime_evidence.json"
)
path = _EVIDENCE_DIR / "gguf_draft_runtime_evidence.json"
return json.loads(path.read_text(encoding="utf-8"))


def _independent_trace(record: dict) -> dict:
metadata = record["independent_direct_ort_trace"]
path = Path(__file__).parents[4] / "testdata" / "evidence" / metadata["filename"]
path = _EVIDENCE_DIR / metadata["filename"]
payload = path.read_bytes()
assert hashlib.sha256(payload).hexdigest() == metadata["sha256"]
return json.loads(payload)


def _effective_git_attributes(
repository: Path, paths: list[str], *, global_attributes: Path
) -> dict[str, dict[str, str]]:
environment = os.environ.copy()
environment["GIT_ATTR_NOSYSTEM"] = "1"
result = subprocess.run(
[
"git",
"-c",
f"core.attributesFile={global_attributes}",
"check-attr",
"-z",
"text",
"eol",
"--",
*paths,
],
cwd=repository,
check=True,
capture_output=True,
env=environment,
)
fields = result.stdout.decode("utf-8").split("\0")
assert fields.pop() == ""
assert len(fields) % 3 == 0
attributes: dict[str, dict[str, str]] = {}
for path, attribute, value in zip(fields[0::3], fields[1::3], fields[2::3], strict=True):
attributes.setdefault(path, {})[attribute] = value
return attributes


def test_evidence_json_files_are_lf_normalized_for_raw_hashes(tmp_path: Path) -> None:
isolated_repo = tmp_path / "repository"
isolated_repo.mkdir()
subprocess.run(["git", "init", "--quiet"], cwd=isolated_repo, check=True)
shutil.copyfile(_REPO_ROOT / ".gitattributes", isolated_repo / ".gitattributes")

global_attributes = tmp_path / "global-attributes"
global_attributes.touch()
evidence_json = sorted(
path.relative_to(_REPO_ROOT).as_posix() for path in _EVIDENCE_DIR.rglob("*.json")
)
assert any(path.startswith("testdata/evidence/causal-lm/") for path in evidence_json)
future_binary = "testdata/evidence/future-evidence.bin"
for relative_path in [*evidence_json, future_binary]:
path = isolated_repo / relative_path
path.parent.mkdir(parents=True, exist_ok=True)
path.touch()
attributes = _effective_git_attributes(
isolated_repo,
[*evidence_json, future_binary],
global_attributes=global_attributes,
)

for relative_path in evidence_json:
assert attributes[relative_path] == {"text": "set", "eol": "lf"}
assert attributes[future_binary] == {
"text": "unspecified",
"eol": "unspecified",
}

expected_trace_hashes = {
"gguf_dflash_independent_ort_trace.json": {
"crlf": "e0e70e909f33ed44aa87da39bab721ebbee0c3e730e2f89dbc509c6683701e42",
"semantic": "ea1f1414d5bc28c2b7776fa66f2a3b4bc72eeda2b38ecaa8d1ca3f8f8bfe8fcf",
},
"gguf_eagle3_independent_ort_trace.json": {
"crlf": "78aa94f24d5f1b9ae020de1ffa5a70c6a30f30ff45eb5310a540cdd2b3d8fab2",
"semantic": "4dbd07a42b89f2067deb1eb249ce8f06ccb973cd700724592f3892deaf78a5db",
},
}
traces = {
record["independent_direct_ort_trace"]["filename"]: record[
"independent_direct_ort_trace"
]
for record in _evidence()["routes"]
}
assert traces.keys() == expected_trace_hashes.keys()
for filename, metadata in traces.items():
payload = (_EVIDENCE_DIR / filename).read_bytes()
assert b"\n" in payload
assert b"\r\n" not in payload
assert hashlib.sha256(payload).hexdigest() == metadata["sha256"]
semantic_payload = json.dumps(
json.loads(payload), sort_keys=True, separators=(",", ":")
).encode()
assert (
hashlib.sha256(semantic_payload).hexdigest()
== expected_trace_hashes[filename]["semantic"]
)
crlf_payload = payload.replace(b"\n", b"\r\n")
crlf_sha256 = hashlib.sha256(crlf_payload).hexdigest()
assert crlf_sha256 == expected_trace_hashes[filename]["crlf"]
assert crlf_sha256 != metadata["sha256"]


def test_draft_runtime_evidence_is_bounded_immutable_and_complete() -> None:
evidence = _evidence()
policy = evidence["policy"]
Expand Down
1 change: 1 addition & 0 deletions testdata/evidence/gguf_dflash_independent_ort_trace.json
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

{"counters":{"rounds":5,"proposed":75,"accepted":18,"multi_token_rounds":3,"rejections":5},"final_draft_cache":{"length":28,"sha256":"f605ea72eeaad1ef074a36551390d3aa14b93159e03b15483e4358b719cc6456"},"final_target_cache":{"length":47,"sha256":"0107c259dfd716ad347b6d6f5ee6a5a590111bf627172aa96a91ac082edb4467"},"generated_tokens_sha256":"78c55f7b34e9a14f4c02850a631ed9f85e0a155f25901b5d99db36f1755b7a35","reorder":{"supported":false,"reason":"reference coordinator is batch-size 1"},"rounds":[{"proposal_ids":[9665,397,1067,366,3215,1339,1339,1004,366,3215,3215,3215,3215,3215,262],"proposal_tokens":[9665,397,1067,366,3215,1339,1339,1004,366,3215,3215,3215,3215,3215,262],"proposal_logits_sha256":"7d4394456101a75eac8208a09bd4d9394021c43b622c5bbac5775ff91198de09","posterior_tokens":[9665,397,970,366,3215,397,970,970,3974,271,4159,4159,29,3215,3215,737],"accepted_prefix":2,"correction_token":970,"target":{"before_length":16,"before_sha256":"5e7df359df34adde23fa09f2eb47d892025c83721563aebd080b4ad9a6b6aa90","tentative_length":32,"tentative_sha256":"ef2539592e67a1bca8d82e695c2e507dcded79b443bb1ae1c09e0914597a7997","committed_length":19,"committed_sha256":"569fa3bd3ee7f69974bc68dd7b8e80b848872933ed391813f4909a8652809c68","replay_length":19,"replay_sha256":"844f3a07602a104a1e2af6700fdd5c833378861326aa94dab3f48daf920f7404","replay_max_abs":0.38140740990638733,"replay_logits_sha256":"8c37b3902528b3c48c9ff007b97c6671d2e546eb84d48cf70d3054e8ab96947f","replay_tokens":[9665,397,970],"replay_tokens_match":true},"draft":{"before_length":0,"before_sha256":"2256da259b2ba5263517c15404741b28f4ad539e981103e75a09120017c4a8aa","tentative_length":32,"tentative_sha256":"89e21cd47e81c23f6b0b0e6773c54ed531520482d6914ad16a5ee3f8e8fe5fec","committed_length":16,"committed_sha256":"1ccaa4079c3ff0d1784e21bb1ed701a90addb4cb4123b1a7838108656c1ee144","replay_length":16,"replay_sha256":"1ccaa4079c3ff0d1784e21bb1ed701a90addb4cb4123b1a7838108656c1ee144","replay_max_abs":0.0}},{"proposal_ids":[4473,19688,1339,1004,4473,1548,1548,2890,11,526,3347,10303,12995,2890,2890],"proposal_tokens":[4473,19688,1339,1004,4473,1548,1548,2890,11,526,3347,10303,12995,2890,2890],"proposal_logits_sha256":"100711fefb9b6633de2581544dda1044d5930c2eefb5878e5ae85a92b7000818","posterior_tokens":[4473,1460,401,1004,3974,1460,2890,9,11,526,3347,11,11,526,11,11],"accepted_prefix":1,"correction_token":1460,"target":{"before_length":19,"before_sha256":"569fa3bd3ee7f69974bc68dd7b8e80b848872933ed391813f4909a8652809c68","tentative_length":35,"tentative_sha256":"49ce90b4b89c117215575f24c4e3e74e46f8927155d085b0fabdbdd438a56137","committed_length":21,"committed_sha256":"3a90d2e4e5bede1223c479d8c37bbcef20a712ca59abb3d9de44def548998e21","replay_length":21,"replay_sha256":"28e403a97d6ea2c5f6c7adb38ce65e6cd1df29edcb0dcab0e9b86345dbb8a045","replay_max_abs":0.42594051361083984,"replay_logits_sha256":"882877c702243681fb9b1ba65af4a2fd42d9415fd43ff968c847a39abfd1e3fb","replay_tokens":[4473,1460],"replay_tokens_match":true},"draft":{"before_length":16,"before_sha256":"1ccaa4079c3ff0d1784e21bb1ed701a90addb4cb4123b1a7838108656c1ee144","tentative_length":35,"tentative_sha256":"6a231365b75a07eef8a18f5d57b2bfce80b518d5312e1ef3d185a0c9da9eafc2","committed_length":19,"committed_sha256":"4547e247919a66d0f26b6a8b0c2ca4373f4fcfd3b9b496dccc8b1f521be3a353","replay_length":19,"replay_sha256":"4547e247919a66d0f26b6a8b0c2ca4373f4fcfd3b9b496dccc8b1f521be3a353","replay_max_abs":0.0}},{"proposal_ids":[1460,401,1004,14291,1548,2890,11,262,2890,11,11,11,11,11,11],"proposal_tokens":[1460,401,1004,14291,1548,2890,11,262,2890,11,11,11,11,11,11],"proposal_logits_sha256":"6ed1f7fa13163fd3dc6685910765ae8f93fbc92a5c2c1f1539dd68cd35551cf7","posterior_tokens":[401,401,1004,3974,1548,353,12995,526,526,11,526,526,526,526,526,1154],"accepted_prefix":0,"correction_token":401,"target":{"before_length":21,"before_sha256":"3a90d2e4e5bede1223c479d8c37bbcef20a712ca59abb3d9de44def548998e21","tentative_length":37,"tentative_sha256":"9a4f7926144625ddf6bd08b34aff64dc6dd96ef5be7ccdaa051615af4b2c6de9","committed_length":22,"committed_sha256":"8d0972cf012af1f15fe7abaa0924ce364426b068042242cb922b1c916fbbcd09","replay_length":22,"replay_sha256":"d79f0b1431b2983185338a2ccea149fe255c860872d7b8de5620408925fe4ba6","replay_max_abs":0.42594051361083984,"replay_logits_sha256":"bb1b1bedb240fa5ba5f0f5cc03d54af17754412dca571302b7a52e7c43d60265","replay_tokens":[401],"replay_tokens_match":true},"draft":{"before_length":19,"before_sha256":"4547e247919a66d0f26b6a8b0c2ca4373f4fcfd3b9b496dccc8b1f521be3a353","tentative_length":37,"tentative_sha256":"acccd907957d5f5099191761c075a7c1411153f34c1c1143126ad347c7401726","committed_length":21,"committed_sha256":"491310117ccb8b31bb0ec19813b5cbc3553d3a3754a23866e01de1002f1932a6","replay_length":21,"replay_sha256":"491310117ccb8b31bb0ec19813b5cbc3553d3a3754a23866e01de1002f1932a6","replay_max_abs":0.0}},{"proposal_ids":[1004,3974,10231,1548,2890,3347,526,11,11,11,2890,11,11,11,11],"proposal_tokens":[1004,3974,10231,1548,2890,3347,526,11,11,11,2890,11,11,11,11],"proposal_logits_sha256":"77f7266c190ca90cb49f5b505a5ecd5a055781c8cc8b25bca096a1b7e13a0149","posterior_tokens":[1004,3974,10231,1548,2890,12995,11,1550,526,526,526,11,526,526,526,526],"accepted_prefix":5,"correction_token":12995,"target":{"before_length":22,"before_sha256":"8d0972cf012af1f15fe7abaa0924ce364426b068042242cb922b1c916fbbcd09","tentative_length":38,"tentative_sha256":"55c77188f51c200ff87de3cda8996b4c34153ee2ffb0eb99e4977dbb1dcc6651","committed_length":28,"committed_sha256":"9308aee1b626c47d6205c13db448e40b2c6114630c99587b1fad5abede557f8c","replay_length":28,"replay_sha256":"dfed2bc06bb7c0c1df97d5ca6788230b749b1db18e3754a9300879db8698088a","replay_max_abs":0.42594051361083984,"replay_logits_sha256":"8390ad34c283728352a3bad080e0e41ec5b73a21d2918a5f8fa43937a6d6ae03","replay_tokens":[1004,3974,10231,1548,2890,12995],"replay_tokens_match":true},"draft":{"before_length":21,"before_sha256":"491310117ccb8b31bb0ec19813b5cbc3553d3a3754a23866e01de1002f1932a6","tentative_length":38,"tentative_sha256":"c7c82c1f8ddc5d25ef7d2551d69342d1ecb274204406f240e6975f2914ee1f5c","committed_length":22,"committed_sha256":"bee2317b6bb1ee062cadf209a54e46bc359ca868ce2335a046fd22ff363af48b","replay_length":22,"replay_sha256":"bee2317b6bb1ee062cadf209a54e46bc359ca868ce2335a046fd22ff363af48b","replay_max_abs":0.0}},{"proposal_ids":[526,3347,11,526,1550,8,341,262,421,320,366,1550,8,341,3347],"proposal_tokens":[526,3347,11,526,1550,8,341,262,421,320,366,1550,8,341,3347],"proposal_logits_sha256":"34779cc62958835e854c331118a0669f9cd63f4f0caf775e03082995406e4a36","posterior_tokens":[526,3347,11,526,1550,8,341,262,421,320,10303,220,8,341,286,284],"accepted_prefix":10,"correction_token":10303,"target":{"before_length":28,"before_sha256":"9308aee1b626c47d6205c13db448e40b2c6114630c99587b1fad5abede557f8c","tentative_length":44,"tentative_sha256":"6dd6d7d8a2363b689ac193c5a1d929fdb0a4188c3515652ef752da99b553b40a","committed_length":39,"committed_sha256":"15ea231d4104dfec54a7b4801c9975214d502cb41fe5e020339ce342244f3b5b","replay_length":39,"replay_sha256":"fe857aa5c9169817aaef93b4ad84b796f7330ea191d519ef1a89aff5d59ef295","replay_max_abs":1.9676249027252197,"replay_logits_sha256":"a5dde6b14cbcf60f319b9d60bdf514bdd18f6dee27380db0b1088c7da0d74b6b","replay_tokens":[526,3347,11,526,1550,8,341,262,421,320,10303],"replay_tokens_match":true},"draft":{"before_length":22,"before_sha256":"bee2317b6bb1ee062cadf209a54e46bc359ca868ce2335a046fd22ff363af48b","tentative_length":44,"tentative_sha256":"ea4068ba98caef1960a4fee2010087e3cd181f40f20183f51d58cdebf6618a29","committed_length":28,"committed_sha256":"f605ea72eeaad1ef074a36551390d3aa14b93159e03b15483e4358b719cc6456","replay_length":28,"replay_sha256":"f605ea72eeaad1ef074a36551390d3aa14b93159e03b15483e4358b719cc6456","replay_max_abs":0.0}}],"tokens_equal":true}
4 changes: 2 additions & 2 deletions testdata/evidence/gguf_draft_runtime_evidence.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
},
"independent_direct_ort_trace": {
"filename": "gguf_dflash_independent_ort_trace.json",
"sha256": "d81f0d1ec7294289679a04d637c56a0c46cba0153e6e0aa6a2e079d3fb2a9519",
"sha256": "1062e5ed665aeafbcf0d0fb250f9b48c520091ebc4b2ad6b19dbd341518c481f",
"mutation_discriminators": {
"draft_mapping": "semantic.draft_mapping",
"proposal_order": "semantic.proposal_order",
Expand Down Expand Up @@ -251,7 +251,7 @@
},
"independent_direct_ort_trace": {
"filename": "gguf_eagle3_independent_ort_trace.json",
"sha256": "2f45ddddd60bbc480914b0a95c58291c209b39524517baa3f872d32df52e56c6",
"sha256": "9d6c6fdb630bc0a040eacc2a2357a2991609ea0d904ff16574a9b55f7d010d55",
"mutation_discriminators": {
"draft_mapping": "semantic.draft_mapping",
"proposal_order": "semantic.proposal_order",
Expand Down
Loading
Loading