"""Run the A7b explicit Reference downgrade GPU Matrix.""" from __future__ import annotations import argparse import copy from dataclasses import dataclass import hashlib 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 _output_latents, _output_text from v38_a3c_selective_refine_gpu_matrix import ( GateFailure, PROMPT, _execute, _find_one, _marker_json, ) from v38_a7a_packed_row_gpu_matrix import ( _cross_run_plan_identity, _parse_packed_rows, ) ACTION_DISABLED = "Disabled" ACTION_REDUCE = "Reduce References One Step" IMAGE_MAX = "Max Identity" IMAGE_MATCH = "Match Output" VIDEO_BALANCED = "Balanced - 0.6 MP" VIDEO_EFFICIENT = "Efficient - 0.4 MP" POLICY_STATUS = "Memory Action Policy [Experimental A7b v1]" SEED_PATTERN = re.compile(r"chunk 1/1: seed=(\d+)") IMAGE_GEOMETRY_PATTERN = re.compile(r"Ref1=(\d+)x(\d+)") @dataclass(frozen=True) class A7bCase: name: str conditioning: str reference_count: int = 0 video_file: str = "" @property def is_image(self) -> bool: return self.conditioning == "image" @property def is_video(self) -> bool: return self.conditioning == "video" def _sha256_json(value: Any) -> str: payload = json.dumps( value, sort_keys=True, separators=(",", ":"), ensure_ascii=True, ).encode("utf-8") return hashlib.sha256(payload).hexdigest() def _configure_prompt( template: Mapping[str, Any], *, case: A7bCase, action: str, output_prefix: str, run_name: str, reference_image: str, ) -> dict[str, Any]: prompt = copy.deepcopy(dict(template)) sampler_id, sampler = _find_one(prompt, "H3ContinuumSamplerV38") sampler["class_type"] = "H3ContinuumSamplerV38MemoryPolicyExperimental" inputs = sampler["inputs"] inputs["diagnostics"] = "Detailed Report" inputs["run_storage"] = "Save + Auto Resume" inputs["run_name"] = run_name inputs["project_id"] = "" inputs["reference_size"] = IMAGE_MAX inputs["video_reference_size"] = VIDEO_BALANCED for name in ( "reference_image_1", "reference_image_2", "reference_image_3", "reference_video_1", ): inputs.pop(name, None) prompt["949"] = { "class_type": "H3ContinuumMemoryActionPolicyExperimental", "inputs": {"reference_action": action}, "_meta": {"title": "A7b Explicit Memory Action Policy"}, } inputs["memory_action_policy"] = ["949", 0] prompt_id = str(inputs["sequence_prompt"][0]) prompt[prompt_id]["inputs"]["value"] = PROMPT if case.is_image: labels = [] for index in range(1, case.reference_count + 1): node_id = str(949 + index) input_name = f"reference_image_{index}" prompt[node_id] = { "class_type": "LoadImage", "inputs": {"image": reference_image}, "_meta": {"title": f"A7b Reference Image {index}"}, } inputs[input_name] = [node_id, 0] labels.append(f"") prompt[prompt_id]["inputs"]["value"] = ( PROMPT + " Preserve the identity and natural appearance from " + ", ".join(labels) + "." ) elif case.is_video: prompt["950"] = { "class_type": "H3ContinuumLoadVideo", "inputs": { "enable_video": True, "file": case.video_file, "force_rate": 24.0, }, "_meta": {"title": "A7b Video Guide"}, } inputs["reference_video_1"] = ["950", 0] prompt[prompt_id]["inputs"]["value"] = ( PROMPT + " Follow the appearance and motion guidance from