from __future__ import annotations import hashlib import json import os from pathlib import Path from typing import Any AUDIO_COMMAND_STREAMING_PROMPT = ( "Listen for the spoken task in the audio, then carry out that task as body motion." ) HFD_AUDIO_SOURCE_FORMAT = "hfd_audio_source_v2" HFD_ONLINE_ENCODER_CONTRACT = ( "qwen2_5_omni_thinker_final_hidden_b1_bf16_flash_attention_2_v1" ) HFD_ONLINE_ENCODER_BATCH_SIZE = 1 HFD_ONLINE_ENCODER_DTYPE = "bfloat16" HFD_ONLINE_ENCODER_ATTENTION = "flash_attention_2" HFD_ENCODER_IDENTITY_KEYS = ( "format", "encoder_contract", "encoder_batch_size", "encoder_dtype", "attention_implementation", "streaming_prompt_sha1", "model_fingerprint", ) def _sha256_file(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as handle: for chunk in iter(lambda: handle.read(1024 * 1024), b""): digest.update(chunk) return digest.hexdigest() def qwen_model_fingerprint(model_path: str | Path) -> str: """Fingerprint the exact local Qwen artifacts used by cache and inference.""" root = Path(model_path).expanduser().resolve() if not (root / "config.json").is_file(): raise FileNotFoundError(f"Invalid Qwen model directory: {root}") small_files = ( "config.json", "model.safetensors.index.json", "preprocessor_config.json", "tokenizer_config.json", "tokenizer.json", "chat_template.json", ) payload: dict[str, Any] = {"root": str(root), "files": {}} for name in small_files: path = root / name if path.is_file(): payload["files"][name] = { "size": path.stat().st_size, "sha256": _sha256_file(path), } index_path = root / "model.safetensors.index.json" if index_path.is_file(): index = json.loads(index_path.read_text(encoding="utf-8")) shard_names = sorted(set(index.get("weight_map", {}).values())) payload["weight_shards"] = { name: { "size": (root / name).stat().st_size, "mtime_ns": (root / name).stat().st_mtime_ns, } for name in shard_names } repo_metadata = root / ".hfd" / "repo_metadata.json" if repo_metadata.is_file(): raw = json.loads(repo_metadata.read_text(encoding="utf-8")) payload["repository_revision"] = raw.get("sha") serialized = json.dumps( payload, ensure_ascii=False, sort_keys=True, separators=(",", ":"), ).encode("utf-8") return hashlib.sha256(serialized).hexdigest() def encoder_contract_payload(model_path: str | Path) -> dict[str, Any]: return { "format": "hfd_online_encoder_contract_v1", "encoder_contract": HFD_ONLINE_ENCODER_CONTRACT, "encoder_batch_size": HFD_ONLINE_ENCODER_BATCH_SIZE, "encoder_dtype": HFD_ONLINE_ENCODER_DTYPE, "attention_implementation": HFD_ONLINE_ENCODER_ATTENTION, "streaming_prompt": AUDIO_COMMAND_STREAMING_PROMPT, "streaming_prompt_sha1": hashlib.sha1( AUDIO_COMMAND_STREAMING_PROMPT.encode("utf-8") ).hexdigest(), "model_path": str(Path(model_path).expanduser().resolve()), "model_fingerprint": qwen_model_fingerprint(model_path), } def write_json_atomic(path: Path, payload: dict[str, Any]) -> None: path.parent.mkdir(parents=True, exist_ok=True) tmp = path.with_name(f".{path.name}.{os.getpid()}.tmp") tmp.write_text( json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8", ) os.replace(tmp, path) def encoder_contract_identity(payload: dict[str, Any]) -> dict[str, Any]: missing = [key for key in HFD_ENCODER_IDENTITY_KEYS if key not in payload] if missing: raise RuntimeError(f"Incomplete HFD encoder contract; missing={missing}") return {key: payload[key] for key in HFD_ENCODER_IDENTITY_KEYS} def validate_encoder_cache( cache_root: str | Path, model_path: str | Path, *, require_online_verification: bool = True, ) -> dict[str, Any]: """Reject stale hidden caches before training or online inference.""" root = Path(cache_root).expanduser().resolve() contract_path = root / "_reports" / "encoder_contract.json" if not contract_path.is_file(): raise RuntimeError( f"Hidden cache has no encoder contract: {contract_path}. " "Regenerate it with backup/prepare_omni_training_data.sh." ) actual = json.loads(contract_path.read_text(encoding="utf-8")) expected = encoder_contract_payload(model_path) for key in HFD_ENCODER_IDENTITY_KEYS: if actual.get(key) != expected[key]: raise RuntimeError( f"Hidden encoder contract mismatch at {contract_path}: " f"{key}={actual.get(key)!r}, expected={expected[key]!r}." ) if require_online_verification: verify_path = root / "_reports" / "online_hidden_verify.json" if not verify_path.is_file(): raise RuntimeError( f"Hidden cache has no online equivalence report: {verify_path}" ) verification = json.loads(verify_path.read_text(encoding="utf-8")) verified_contract = all( verification.get(key) == expected[key] for key in HFD_ENCODER_IDENTITY_KEYS ) if ( not verified_contract or int(verification.get("verified_windows", 0)) <= 0 or int(verification.get("mismatched_windows", -1)) != 0 or float(verification.get("max_abs_error", -1.0)) != 0.0 ): raise RuntimeError( f"Hidden cache failed exact online equivalence: {verify_path}" ) return actual