79 lines
2.9 KiB
Python
79 lines
2.9 KiB
Python
|
|
import hashlib
|
||
|
|
import json
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
from scripts.verify_reference_profile import verify_checkpoint
|
||
|
|
from llm_services.reference_profiles import get_profile
|
||
|
|
|
||
|
|
|
||
|
|
def prepared(tmp_path):
|
||
|
|
profile = get_profile("qwen-image-edit-2511-v1")
|
||
|
|
model = tmp_path / "model"
|
||
|
|
model.mkdir()
|
||
|
|
siblings = []
|
||
|
|
paths = {
|
||
|
|
"model_index.json": b'{"_class_name":"QwenImageEditPlusPipeline"}',
|
||
|
|
"tokenizer/vocab.json": b"{}",
|
||
|
|
"tokenizer/tokenizer_config.json": b"{}",
|
||
|
|
"processor/preprocessor_config.json": b"{}",
|
||
|
|
"scheduler/scheduler_config.json": b"{}",
|
||
|
|
"text_encoder/config.json": b"{}",
|
||
|
|
"transformer/config.json": b"{}",
|
||
|
|
"vae/config.json": b"{}",
|
||
|
|
"text_encoder/model.safetensors": b"encoder",
|
||
|
|
"transformer/diffusion_pytorch_model.safetensors": b"transformer",
|
||
|
|
"vae/diffusion_pytorch_model.safetensors": b"vae",
|
||
|
|
}
|
||
|
|
for name, content in paths.items():
|
||
|
|
path = model / name
|
||
|
|
path.parent.mkdir(exist_ok=True)
|
||
|
|
path.write_bytes(content)
|
||
|
|
siblings.append(
|
||
|
|
{
|
||
|
|
"rfilename": name,
|
||
|
|
"size": len(content),
|
||
|
|
"blobId": hashlib.sha1(
|
||
|
|
b"blob " + str(len(content)).encode() + b"\0" + content
|
||
|
|
).hexdigest(),
|
||
|
|
}
|
||
|
|
)
|
||
|
|
metadata = tmp_path / "metadata.json"
|
||
|
|
metadata.write_text(json.dumps({"sha": profile.revision, "siblings": siblings}))
|
||
|
|
(model / ".deployment-manifest.json").write_text(
|
||
|
|
json.dumps(
|
||
|
|
{
|
||
|
|
"repo": profile.repo,
|
||
|
|
"revision": profile.revision,
|
||
|
|
"files": [{"path": name} for name in paths],
|
||
|
|
}
|
||
|
|
)
|
||
|
|
)
|
||
|
|
return profile, model, metadata
|
||
|
|
|
||
|
|
|
||
|
|
def test_offline_checkpoint_validation_checks_real_canonical_hashes(tmp_path):
|
||
|
|
profile, model, metadata = prepared(tmp_path)
|
||
|
|
report = verify_checkpoint(profile, model, metadata)
|
||
|
|
assert report["verified_files"] == 11
|
||
|
|
assert report["available"] is False # 权重完整不代表资源、抢占、质量已认证。
|
||
|
|
(model / "tokenizer/vocab.json").write_bytes(b"[]")
|
||
|
|
with pytest.raises(ValueError, match="checksum"):
|
||
|
|
verify_checkpoint(profile, model, metadata)
|
||
|
|
|
||
|
|
|
||
|
|
def test_offline_checkpoint_validation_requires_exact_revision_and_component_set(tmp_path):
|
||
|
|
profile, model, metadata = prepared(tmp_path)
|
||
|
|
value = json.loads(metadata.read_text())
|
||
|
|
value["sha"] = "wrong-revision"
|
||
|
|
metadata.write_text(json.dumps(value))
|
||
|
|
with pytest.raises(ValueError, match="revision"):
|
||
|
|
verify_checkpoint(profile, model, metadata)
|
||
|
|
value["sha"] = profile.revision
|
||
|
|
value["siblings"] = [
|
||
|
|
item for item in value["siblings"] if not item["rfilename"].startswith("vae/")
|
||
|
|
]
|
||
|
|
metadata.write_text(json.dumps(value))
|
||
|
|
with pytest.raises(ValueError, match="component"):
|
||
|
|
verify_checkpoint(profile, model, metadata)
|