"""Run the A7a read-only Packed-row Observability GPU Matrix.""" from __future__ import annotations import argparse import copy from dataclasses import dataclass import json from pathlib import Path import re import shutil import sys from typing import Any, Mapping TOOLS_ROOT = Path(__file__).resolve().parent if str(TOOLS_ROOT) not in sys.path: sys.path.insert(0, str(TOOLS_ROOT)) from v38_a2_audio_only_gpu_canary import ( _history_entry, _output_latents, _output_text, ) from v38_a3c_selective_refine_gpu_matrix import ( GateFailure, PROMPT, _configure_base, _execute, _find_one, _marker_json, _remove_class, ) @dataclass(frozen=True) class FirstPassCase: name: str chunks: int terminal_merge: bool conditioning: str baseline_kind: str baseline_history: Path | None = None @property def expected_groups(self) -> int: return 2 if self.terminal_merge else self.chunks @property def expected_seconds(self) -> float: return float(self.chunks * 5) PLAN_PATTERN = re.compile( r"execution_order=(\[[^\]]*\]),\s*" r"max_concurrent_physical_groups=(\d+),\s*" r"observed_groups=(\d+)/(\d+),\s*" r"peak_candidate=(\d+|unavailable),\s*" r"peak_total_rows=(\d+|unavailable)" ) GROUP_PATTERN = re.compile( r"physical_group=(\d+),\s*logical_chunks=(\[[^\]]*\]),\s*" r"terminal_atomic=(true|false),\s*total=(\d+),\s*text=(\d+),\s*" r"condition_video=(\d+),\s*condition_audio=(\d+),\s*" r"target_video=(\d+),\s*target_audio=(\d+),\s*unknown=(\d+)" ) def _history_only_entry(path: Path) -> Mapping[str, Any]: history = json.loads(path.read_text(encoding="utf-8")) if not isinstance(history, Mapping) or len(history) != 1: raise GateFailure(f"baseline history must contain one prompt: {path}") return _history_entry(history, str(next(iter(history)))) def _baseline_from_a6(path: Path) -> dict[str, Any]: entry = _history_only_entry(path) probe_status = _output_text(entry, "908") probe = _marker_json(probe_status, "A3C_PROBE_JSON=") return { "origin": str(path), "video_sha256": list(probe.get("input_video_sha256") or []), "audio_sha256": list(probe.get("input_audio_sha256") or []), "assembly_sha256": str((probe.get("input_plan") or {}).get("sha256") or ""), "assembly_normalized": copy.deepcopy( (probe.get("input_plan") or {}).get("normalized") ), "decoded": None, } def _cross_run_plan_identity(value: Any) -> Any: """Ignore only the pre-existing per-execution monotonic start marker.""" normalized = copy.deepcopy(value) if isinstance(normalized, dict): normalized.pop("_runtime_started_at", None) return normalized def _parse_packed_rows(status: str) -> dict[str, Any]: plan_line = next( (line for line in status.splitlines() if line.startswith("Packed-row plan [A7a v1]:")), "", ) if not plan_line: raise GateFailure("A7a packed-row plan is missing from Detailed status") match = PLAN_PATTERN.search(plan_line) if match is None: raise GateFailure("A7a packed-row plan could not be parsed") groups = [] for line in status.splitlines(): if not line.startswith("Packed-row group [A7a v1]:"): continue current = GROUP_PATTERN.search(line) if current is None: raise GateFailure(f"A7a packed-row group could not be parsed: {line}") group = { "physical_group": int(current.group(1)), "logical_chunks": json.loads(current.group(2)), "terminal_atomic": current.group(3) == "true", "total": int(current.group(4)), "text": int(current.group(5)), "condition_video": int(current.group(6)), "condition_audio": int(current.group(7)), "target_video": int(current.group(8)), "target_audio": int(current.group(9)), "unknown": int(current.group(10)), "point_in_time_memory_observation": ( "pre_system_available=" in line and "post_system_available=" in line and "pre_CUDA_allocated=" in line and "post_CUDA_allocated=" in line and "pre_Core_free=" in line and "post_Core_free=" in line ), } group["component_sum"] = sum( group[key] for key in ( "text", "condition_video", "condition_audio", "target_video", "target_audio", "unknown", ) ) groups.append(group) peak_candidate = None if match.group(5) == "unavailable" else int(match.group(5)) peak_rows = None if match.group(6) == "unavailable" else int(match.group(6)) return { "execution_order": json.loads(match.group(1)), "max_concurrent_physical_groups": int(match.group(2)), "observed_groups": int(match.group(3)), "recorded_groups": int(match.group(4)), "peak_row_candidate": peak_candidate, "peak_total_rows": peak_rows, "groups": groups, "report_contract": { "actual_core_rows_only": "actual Core rows only" in plan_line, "no_vram_time_prediction": "VRAM/time prediction=not provided" in plan_line, "execution_policy_unchanged": "execution policy=unchanged" in plan_line, }, } def _configure_first_pass( template: Mapping[str, Any], *, case: FirstPassCase, diagnostics: str, output_prefix: str, reference_image: str, reference_video: str, ) -> dict[str, Any]: prompt = _configure_base( template, chunks=case.chunks, terminal_merge=case.terminal_merge, output_prefix=output_prefix, ) _remove_class(prompt, "H3ContinuumSecondPassV35") _remove_class(prompt, "H3ContinuumSelectiveSecondPassExperimental") sampler_id, sampler_node = _find_one(prompt, "H3ContinuumSamplerV38") inputs = sampler_node["inputs"] inputs["diagnostics"] = diagnostics inputs["run_storage"] = "Off" prompt_id = str(inputs["sequence_prompt"][0]) prompt[prompt_id]["inputs"]["value"] = PROMPT for node_id in ("900", "901", "902", "903", "904", "905", "906", "907", "908", "909", "910", "911"): prompt.pop(node_id, None) if case.conditioning == "reference_images_3": prompt[prompt_id]["inputs"]["value"] = ( PROMPT + " Preserve the identities and natural appearance from , " ", and ." ) for offset, input_name in enumerate( ("reference_image_1", "reference_image_2", "reference_image_3"), start=950, ): prompt[str(offset)] = { "class_type": "LoadImage", "inputs": {"image": reference_image}, "_meta": {"title": f"A7a Reference Image {offset - 949}"}, } inputs[input_name] = [str(offset), 0] elif case.conditioning == "reference_video": prompt[prompt_id]["inputs"]["value"] = ( PROMPT + " Follow the motion and appearance guidance from