Squashed 'vendor/dumas_h3_continuum/' content from commit d07ed31

git-subtree-dir: vendor/dumas_h3_continuum
git-subtree-split: d07ed31b442d283d6a1740f875a843b9feda4c91
This commit is contained in:
2026-10-20 13:43:38 +00:00
commit bf8f76505f
481 changed files with 131517 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
"""Runtime support helpers shipped with H3 Continuum."""
+230
View File
@@ -0,0 +1,230 @@
"""Temporary GPU-canary node for the internal A2 Audio Only path."""
from __future__ import annotations
import hashlib
import importlib
import json
import sys
from types import SimpleNamespace
from typing import Any
import torch
def _single(name: str, value: Any) -> Any:
if not isinstance(value, list) or len(value) != 1:
raise ValueError(f"{name} must contain exactly one value")
return value[0]
def _active_modules() -> SimpleNamespace:
import nodes as comfy_nodes
sampler_class = comfy_nodes.NODE_CLASS_MAPPINGS.get("H3ContinuumSamplerV38")
if sampler_class is None:
raise RuntimeError("H3ContinuumSamplerV38 is not loaded")
module_name = sampler_class.__module__
base_name = (
module_name.split(".v3.", 1)[0]
if ".v3." in module_name
else module_name.rsplit(".", 2)[0]
)
return SimpleNamespace(
refine_schedule=importlib.import_module(f"{base_name}.v3.refine_schedule"),
refine_target=importlib.import_module(f"{base_name}.v3.refine_target"),
targeted_second_pass=importlib.import_module(
f"{base_name}.v3.targeted_second_pass"
),
)
def _samples(latent: Any, *, name: str) -> torch.Tensor:
if not isinstance(latent, dict) or not torch.is_tensor(latent.get("samples")):
raise ValueError(f"{name} must be a LATENT dictionary with Tensor samples")
return latent["samples"]
def _tensor_sha256(value: torch.Tensor) -> str:
tensor = value.detach().to(device="cpu").contiguous()
digest = hashlib.sha256()
digest.update(str(tuple(int(item) for item in tensor.shape)).encode("ascii"))
digest.update(str(tensor.dtype).encode("ascii"))
digest.update(tensor.view(torch.uint8).numpy().tobytes(order="C"))
return digest.hexdigest()
class H3A2AudioOnlyCanary:
"""Non-public bridge that records the A2 GPU acceptance contract."""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model": ("MODEL",),
"clip": ("CLIP",),
"sampler": ("SAMPLER",),
"sigmas": ("SIGMAS",),
"video_latents": ("LATENT",),
"audio_latents": ("LATENT",),
"assembly_plan": ("H3_CONTINUUM_ASSEMBLY_PLAN",),
"refine_seed": (
"INT",
{"default": 3802601, "min": 0, "max": 0xFFFFFFFFFFFFFFFF},
),
"schedule_mode": (["Tail", "External"], {"default": "Tail"}),
"tail_evaluations": (
"INT",
{"default": 6, "min": 1, "max": 100},
),
},
"optional": {
"refine_context": ("H3_CONTINUUM_REFINE_CONTEXT",),
"video_vae": ("VAE",),
},
}
INPUT_IS_LIST = True
RETURN_TYPES = ("LATENT", "LATENT", "H3_CONTINUUM_ASSEMBLY_PLAN", "STRING")
RETURN_NAMES = (
"video_latents",
"refined_audio_latents",
"updated_assembly_plan",
"canary_status",
)
OUTPUT_IS_LIST = (True, True, False, False)
FUNCTION = "run"
CATEGORY = "H3 Continuum Test/A2"
def run(
self,
model,
clip,
sampler,
sigmas,
video_latents,
audio_latents,
assembly_plan,
refine_seed,
schedule_mode,
tail_evaluations,
refine_context=None,
video_vae=None,
):
modules = _active_modules()
source_sigmas = _single("sigmas", sigmas)
mode = str(_single("schedule_mode", schedule_mode))
if mode == "Tail":
schedule = modules.refine_schedule.make_tail_schedule(
source_sigmas,
evaluation_count=int(
_single("tail_evaluations", tail_evaluations)
),
)
else:
schedule = None
input_video_samples = [
_samples(item, name=f"video_latents[{index}]")
for index, item in enumerate(video_latents)
]
input_audio_samples = [
_samples(item, name=f"audio_latents[{index}]")
for index, item in enumerate(audio_latents)
]
input_video_hashes = [_tensor_sha256(item) for item in input_video_samples]
input_audio_hashes = [_tensor_sha256(item) for item in input_audio_samples]
videos, audios, plan, status = (
modules.targeted_second_pass.run_targeted_second_pass_groups(
model=_single("model", model),
clip=_single("clip", clip),
sampler=_single("sampler", sampler),
sigmas=source_sigmas,
video_latents=video_latents,
audio_latents=audio_latents,
assembly_plan=_single("assembly_plan", assembly_plan),
refine_seed=int(_single("refine_seed", refine_seed)),
refine_target=modules.refine_target.MODE_AUDIO_ONLY,
refine_context=(
None
if refine_context is None
else _single("refine_context", refine_context)
),
video_vae=(
None if video_vae is None else _single("video_vae", video_vae)
),
refine_schedule=schedule,
enable_preview=False,
)
)
output_video_samples = [
_samples(item, name=f"output_video_latents[{index}]")
for index, item in enumerate(videos)
]
output_audio_samples = [
_samples(item, name=f"output_audio_latents[{index}]")
for index, item in enumerate(audios)
]
contract = plan["second_pass_contract"]
summary = {
"format": "h3-continuum-a2-audio-only-gpu-canary-v1",
"target": contract["refine_target_contract"],
"execution": contract["refine_execution_contract"],
"second_pass_contract_version": contract.get("version"),
"physical_groups": [
{
"group_id": group.get("group_id"),
"logical_chunks": group.get("logical_chunks"),
"terminal_merged": bool(group.get("terminal_merged")),
"source_latent_t": group.get("source_latent_t"),
"source_audio_shape": group.get("source_audio_shape"),
}
for group in contract.get("physical_groups", [])
],
"refine_group_seeds": contract.get("refine_group_seeds"),
"schedule_mode": contract["refine_schedule"]["mode"],
"schedule_evaluations": contract["refine_schedule"][
"evaluation_count"
],
"schedule_sigma_hash": contract["refine_schedule"]["sigma_hash"],
"video_object_identity": [
output is source
for output, source in zip(videos, video_latents, strict=True)
],
"video_tensor_identity": [
output is source
for output, source in zip(
output_video_samples,
input_video_samples,
strict=True,
)
],
"video_input_sha256": input_video_hashes,
"video_output_sha256": [
_tensor_sha256(item) for item in output_video_samples
],
"audio_input_sha256": input_audio_hashes,
"audio_output_sha256": [
_tensor_sha256(item) for item in output_audio_samples
],
"audio_input_shapes": [list(item.shape) for item in input_audio_samples],
"audio_output_shapes": [list(item.shape) for item in output_audio_samples],
"video_output_policy": contract.get("video_output"),
"video_sampling_policy": contract.get("video_sampling"),
"audio_output_policy": contract.get("audio_output"),
"audio_sampling_policy": contract.get("audio_sampling"),
}
canary_status = status + "\nA2_CANARY_JSON=" + json.dumps(
summary,
sort_keys=True,
separators=(",", ":"),
)
return videos, audios, plan, canary_status
NODE_CLASS_MAPPINGS = {"H3A2AudioOnlyCanary": H3A2AudioOnlyCanary}
NODE_DISPLAY_NAME_MAPPINGS = {
"H3A2AudioOnlyCanary": "H3 A2 Audio Only Canary (Temporary)"
}
@@ -0,0 +1,387 @@
"""Temporary read-only probe for the A3c Selective Refine GPU Matrix."""
from __future__ import annotations
import copy
import hashlib
import json
from typing import Any, Mapping
import torch
def _single(name: str, value: Any) -> Any:
if not isinstance(value, list) or len(value) != 1:
raise ValueError(f"{name} must contain exactly one value")
return value[0]
def _samples(latent: Any, *, name: str) -> torch.Tensor:
if not isinstance(latent, dict) or not torch.is_tensor(latent.get("samples")):
raise ValueError(f"{name} must be a LATENT dictionary with Tensor samples")
return latent["samples"]
def _tensor_sha256(value: torch.Tensor) -> str:
tensor = value.detach().to(device="cpu").contiguous()
digest = hashlib.sha256()
digest.update(str(tuple(int(item) for item in tensor.shape)).encode("ascii"))
digest.update(str(tensor.dtype).encode("ascii"))
digest.update(tensor.view(torch.uint8).numpy().tobytes(order="C"))
return digest.hexdigest()
def _json_safe(value: Any) -> Any:
if value is None or isinstance(value, (bool, int, float, str)):
return value
if isinstance(value, Mapping):
return {
str(key): _json_safe(item)
for key, item in sorted(value.items(), key=lambda pair: str(pair[0]))
}
if isinstance(value, (list, tuple)):
return [_json_safe(item) for item in value]
return f"<{type(value).__module__}.{type(value).__qualname__}>"
def _plan_identity(plan: Any, *, omit_target_identity: bool) -> dict[str, Any]:
normalized = copy.deepcopy(_json_safe(plan))
if omit_target_identity and isinstance(normalized, dict):
contract = normalized.get("second_pass_contract")
if isinstance(contract, dict):
contract.pop("refine_target_contract", None)
contract.pop("refine_execution_contract", None)
encoded = json.dumps(
normalized,
sort_keys=True,
separators=(",", ":"),
ensure_ascii=False,
).encode("utf-8")
return {
"sha256": hashlib.sha256(encoded).hexdigest(),
"normalized": normalized,
}
class H3A3cSelectiveRefineProbe:
"""Compare first-pass and public Second Pass outputs without changing them."""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_video_latents": ("LATENT",),
"input_audio_latents": ("LATENT",),
"output_video_latents": ("LATENT",),
"output_audio_latents": ("LATENT",),
"input_assembly_plan": ("H3_CONTINUUM_ASSEMBLY_PLAN",),
"output_assembly_plan": ("H3_CONTINUUM_ASSEMBLY_PLAN",),
"refine_status": ("STRING", {"forceInput": True}),
"target_label": (
["Legacy V3.5", "Video Only", "Audio Only", "Video + Audio"],
{"default": "Video Only"},
),
}
}
INPUT_IS_LIST = True
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("probe_report",)
FUNCTION = "run"
CATEGORY = "H3 Continuum Test/A3c"
def run(
self,
input_video_latents,
input_audio_latents,
output_video_latents,
output_audio_latents,
input_assembly_plan,
output_assembly_plan,
refine_status,
target_label,
):
if len(input_video_latents) != len(output_video_latents):
raise ValueError("Video physical-group count changed")
if len(input_audio_latents) != len(output_audio_latents):
raise ValueError("Audio physical-group count changed")
input_v = [
_samples(item, name=f"input_video_latents[{index}]")
for index, item in enumerate(input_video_latents)
]
input_a = [
_samples(item, name=f"input_audio_latents[{index}]")
for index, item in enumerate(input_audio_latents)
]
output_v = [
_samples(item, name=f"output_video_latents[{index}]")
for index, item in enumerate(output_video_latents)
]
output_a = [
_samples(item, name=f"output_audio_latents[{index}]")
for index, item in enumerate(output_audio_latents)
]
output_plan = _single("output_assembly_plan", output_assembly_plan)
contract = output_plan.get("second_pass_contract") or {}
status = str(_single("refine_status", refine_status))
target_contract = contract.get("refine_target_contract") or {}
schedule = contract.get("refine_schedule") or {}
physical_groups = [
{
"group_id": group.get("group_id"),
"logical_chunks": group.get("logical_chunks"),
"terminal_merged": bool(group.get("terminal_merged")),
"source_latent_t": group.get("source_latent_t"),
"source_audio_shape": group.get("source_audio_shape"),
}
for group in contract.get("physical_groups", [])
]
summary = {
"format": "h3-continuum-a3c-selective-refine-probe-v1",
"target_label": str(_single("target_label", target_label)),
"target_mode": target_contract.get("mode", "legacy_video_only"),
"seed_namespace": target_contract.get(
"seed_namespace", "h3-continuum-refine-v1"
),
"second_pass_contract_version": contract.get("version"),
"physical_groups": physical_groups,
"refine_group_seeds": contract.get("refine_group_seeds"),
"schedule_mode": schedule.get("mode"),
"schedule_evaluations": schedule.get("evaluation_count"),
"schedule_sigma_hash": schedule.get("sigma_hash"),
"sampling_passes_reported": status.count("sampling_passes=1"),
"input_video_object_identity": [
output is source
for output, source in zip(
output_video_latents,
input_video_latents,
strict=True,
)
],
"input_audio_object_identity": [
output is source
for output, source in zip(
output_audio_latents,
input_audio_latents,
strict=True,
)
],
"input_video_tensor_identity": [
output is source
for output, source in zip(output_v, input_v, strict=True)
],
"input_audio_tensor_identity": [
output is source
for output, source in zip(output_a, input_a, strict=True)
],
"input_video_sha256": [_tensor_sha256(item) for item in input_v],
"output_video_sha256": [_tensor_sha256(item) for item in output_v],
"input_audio_sha256": [_tensor_sha256(item) for item in input_a],
"output_audio_sha256": [_tensor_sha256(item) for item in output_a],
"input_video_shapes": [list(item.shape) for item in input_v],
"output_video_shapes": [list(item.shape) for item in output_v],
"input_audio_shapes": [list(item.shape) for item in input_a],
"output_audio_shapes": [list(item.shape) for item in output_a],
"output_video_finite": [
bool(torch.isfinite(item.float()).all().item()) for item in output_v
],
"output_audio_finite": [
bool(torch.isfinite(item.float()).all().item()) for item in output_a
],
"input_plan": _plan_identity(
_single("input_assembly_plan", input_assembly_plan),
omit_target_identity=False,
),
"output_plan": _plan_identity(
output_plan,
omit_target_identity=False,
),
"output_plan_without_target_identity": _plan_identity(
output_plan,
omit_target_identity=True,
),
}
report = status + "\nA3C_PROBE_JSON=" + json.dumps(
summary,
sort_keys=True,
separators=(",", ":"),
ensure_ascii=False,
)
return (report,)
class H3A3cVideoOnlyParityProbe:
"""Compare legacy and Selective Video Only outputs from one First Pass."""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_audio_latents": ("LATENT",),
"legacy_video_latents": ("LATENT",),
"legacy_audio_latents": ("LATENT",),
"legacy_assembly_plan": ("H3_CONTINUUM_ASSEMBLY_PLAN",),
"legacy_status": ("STRING", {"forceInput": True}),
"selective_video_latents": ("LATENT",),
"selective_audio_latents": ("LATENT",),
"selective_assembly_plan": ("H3_CONTINUUM_ASSEMBLY_PLAN",),
"selective_status": ("STRING", {"forceInput": True}),
}
}
INPUT_IS_LIST = True
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("parity_report",)
FUNCTION = "run"
CATEGORY = "H3 Continuum Test/A3c"
def run(
self,
input_audio_latents,
legacy_video_latents,
legacy_audio_latents,
legacy_assembly_plan,
legacy_status,
selective_video_latents,
selective_audio_latents,
selective_assembly_plan,
selective_status,
):
counts = {
len(input_audio_latents),
len(legacy_video_latents),
len(legacy_audio_latents),
len(selective_video_latents),
len(selective_audio_latents),
}
if len(counts) != 1:
raise ValueError("Video Only parity physical-group counts differ")
input_a = [
_samples(item, name=f"input_audio_latents[{index}]")
for index, item in enumerate(input_audio_latents)
]
legacy_v = [
_samples(item, name=f"legacy_video_latents[{index}]")
for index, item in enumerate(legacy_video_latents)
]
legacy_a = [
_samples(item, name=f"legacy_audio_latents[{index}]")
for index, item in enumerate(legacy_audio_latents)
]
selective_v = [
_samples(item, name=f"selective_video_latents[{index}]")
for index, item in enumerate(selective_video_latents)
]
selective_a = [
_samples(item, name=f"selective_audio_latents[{index}]")
for index, item in enumerate(selective_audio_latents)
]
legacy_plan = _single("legacy_assembly_plan", legacy_assembly_plan)
selective_plan = _single("selective_assembly_plan", selective_assembly_plan)
legacy_contract = legacy_plan.get("second_pass_contract") or {}
selective_contract = selective_plan.get("second_pass_contract") or {}
legacy_text = str(_single("legacy_status", legacy_status))
selective_text = str(_single("selective_status", selective_status))
summary = {
"format": "h3-continuum-a3c-video-only-parity-v1",
"physical_group_count": len(legacy_v),
"legacy_video_sha256": [_tensor_sha256(item) for item in legacy_v],
"selective_video_sha256": [
_tensor_sha256(item) for item in selective_v
],
"legacy_audio_sha256": [_tensor_sha256(item) for item in legacy_a],
"selective_audio_sha256": [
_tensor_sha256(item) for item in selective_a
],
"legacy_video_shapes": [list(item.shape) for item in legacy_v],
"selective_video_shapes": [list(item.shape) for item in selective_v],
"legacy_audio_shapes": [list(item.shape) for item in legacy_a],
"selective_audio_shapes": [list(item.shape) for item in selective_a],
"legacy_video_finite": [
bool(torch.isfinite(item.float()).all().item()) for item in legacy_v
],
"selective_video_finite": [
bool(torch.isfinite(item.float()).all().item())
for item in selective_v
],
"legacy_audio_finite": [
bool(torch.isfinite(item.float()).all().item()) for item in legacy_a
],
"selective_audio_finite": [
bool(torch.isfinite(item.float()).all().item())
for item in selective_a
],
"legacy_audio_object_identity": [
output is source
for output, source in zip(
legacy_audio_latents,
input_audio_latents,
strict=True,
)
],
"selective_audio_object_identity": [
output is source
for output, source in zip(
selective_audio_latents,
input_audio_latents,
strict=True,
)
],
"legacy_audio_tensor_identity": [
output is source
for output, source in zip(legacy_a, input_a, strict=True)
],
"selective_audio_tensor_identity": [
output is source
for output, source in zip(selective_a, input_a, strict=True)
],
"legacy_refine_group_seeds": legacy_contract.get(
"refine_group_seeds"
),
"selective_refine_group_seeds": selective_contract.get(
"refine_group_seeds"
),
"legacy_schedule_sigma_hash": (
legacy_contract.get("refine_schedule") or {}
).get("sigma_hash"),
"selective_schedule_sigma_hash": (
selective_contract.get("refine_schedule") or {}
).get("sigma_hash"),
"legacy_sampling_passes_reported": legacy_text.count(
"sampling_passes=1"
),
"selective_sampling_passes_reported": selective_text.count(
"sampling_passes=1"
),
"legacy_plan_without_target_identity": _plan_identity(
legacy_plan,
omit_target_identity=True,
)["sha256"],
"selective_plan_without_target_identity": _plan_identity(
selective_plan,
omit_target_identity=True,
)["sha256"],
"selective_target_mode": (
selective_contract.get("refine_target_contract") or {}
).get("mode"),
"selective_seed_namespace": (
selective_contract.get("refine_target_contract") or {}
).get("seed_namespace"),
}
report = "A3C_PARITY_JSON=" + json.dumps(
summary,
sort_keys=True,
separators=(",", ":"),
ensure_ascii=False,
)
return (report,)
NODE_CLASS_MAPPINGS = {
"H3A3cSelectiveRefineProbe": H3A3cSelectiveRefineProbe,
"H3A3cVideoOnlyParityProbe": H3A3cVideoOnlyParityProbe,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"H3A3cSelectiveRefineProbe": "H3 A3c Selective Refine Probe (Temporary)",
"H3A3cVideoOnlyParityProbe": "H3 A3c Video Only Parity Probe (Temporary)",
}
+194
View File
@@ -0,0 +1,194 @@
"""Analyze finalized Audio Continuity research outputs without changing them."""
from __future__ import annotations
import argparse
import hashlib
import importlib.util
import json
from pathlib import Path
import subprocess
import sys
from typing import Any
import numpy as np
import torch
from v36_r2_audio_boundary_analyze import _decode_audio, boundary_metrics
ROOT = Path(__file__).resolve().parents[1]
PACKAGE_NAME = "ComfyUI_H3_Continuum_Join"
def _load_package() -> None:
if PACKAGE_NAME in sys.modules:
return
spec = importlib.util.spec_from_file_location(
PACKAGE_NAME,
ROOT / "__init__.py",
submodule_search_locations=[str(ROOT)],
)
if spec is None:
raise RuntimeError("could not create the Continuum package spec")
module = importlib.util.module_from_spec(spec)
module.__path__ = [str(ROOT)]
sys.modules[PACKAGE_NAME] = module
_load_package()
from ComfyUI_H3_Continuum_Join.v2.seam_guard import _correlation # noqa: E402
def _window_rms_floor(value: np.ndarray, *, sample_rate: int) -> float:
frame = max(1, int(round(sample_rate * 0.010)))
usable = len(value) // frame * frame
if usable <= 0:
return 0.0
blocks = value[:usable].reshape(-1, frame, value.shape[1])
rms = np.sqrt(np.mean(np.square(blocks), axis=(1, 2)))
return float(np.percentile(rms, 10))
def _adjacent_alignment(
audio: np.ndarray,
*,
sample_rate: int,
boundary_sample: int,
) -> dict[str, Any]:
mono = torch.from_numpy(audio.mean(axis=1).astype(np.float32, copy=False))
window = max(8, int(round(sample_rate * 0.080)))
max_lag = max(1, int(round(sample_rate * 0.020)))
before = mono[:boundary_sample]
after = mono[boundary_sample:]
if before.numel() < window + max_lag or after.numel() < window + max_lag:
raise ValueError("insufficient decoded audio for adjacent-window alignment")
def correlation_at(lag: int) -> float:
if lag >= 0:
left = before[-window:]
right = after[lag : lag + window]
else:
stop = int(before.numel()) + lag
left = before[stop - window : stop]
right = after[:window]
return float(_correlation(left, right))
baseline = correlation_at(0)
candidates = [(lag, correlation_at(lag)) for lag in range(-max_lag, max_lag + 1)]
best_lag, best_correlation = max(candidates, key=lambda item: item[1])
return {
"window_ms": 80.0,
"search_ms": 20.0,
"correlation_at_zero": baseline,
"best_correlation": best_correlation,
"best_lag_samples": int(best_lag),
"best_lag_ms": float(best_lag * 1000.0 / sample_rate),
}
def _probe(path: Path, ffprobe: Path) -> dict[str, Any]:
completed = subprocess.run(
[
str(ffprobe),
"-v",
"error",
"-count_frames",
"-show_entries",
"stream=index,codec_type,width,height,nb_read_frames,duration,sample_rate,channels",
"-show_entries",
"format=duration",
"-of",
"json",
str(path),
],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
return json.loads(completed.stdout)
def analyze(
path: Path,
*,
ffmpeg: Path,
ffprobe: Path,
sample_rate: int,
fps: float,
boundary_frames: list[int],
expected_frames: int,
) -> dict[str, Any]:
audio = _decode_audio(path, ffmpeg, sample_rate)
pcm = audio.astype("<f4", copy=False).tobytes(order="C")
boundaries: list[dict[str, Any]] = []
long = max(1, int(round(sample_rate * 0.250)))
for frame in boundary_frames:
sample = int(round(frame * sample_rate / fps))
before = audio[max(0, sample - long) : sample]
after = audio[sample : min(len(audio), sample + long)]
boundaries.append(
{
"frame": int(frame),
**boundary_metrics(audio, sample_rate=sample_rate, boundary_sample=sample),
**_adjacent_alignment(
audio,
sample_rate=sample_rate,
boundary_sample=sample,
),
"rms_floor_before_250ms_p10_10ms": _window_rms_floor(
before, sample_rate=sample_rate
),
"rms_floor_after_250ms_p10_10ms": _window_rms_floor(
after, sample_rate=sample_rate
),
}
)
expected_duration = expected_frames / fps
decoded_duration = len(audio) / sample_rate
return {
"format": "h3-continuum-audio-continuity-output-analysis-v1",
"path": str(path),
"sample_rate": int(sample_rate),
"fps": float(fps),
"expected_frames": int(expected_frames),
"expected_duration_seconds": expected_duration,
"decoded_samples_per_channel": int(len(audio)),
"decoded_duration_seconds": decoded_duration,
"decoded_duration_error_seconds": decoded_duration - expected_duration,
"pcm_f32le_md5": hashlib.md5(pcm).hexdigest(),
"finite": bool(np.isfinite(audio).all()),
"probe": _probe(path, ffprobe),
"boundaries": boundaries,
}
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--input", type=Path, required=True)
parser.add_argument("--result", type=Path, required=True)
parser.add_argument("--ffmpeg", type=Path, default=Path(r"C:\FFmpeg\bin\ffmpeg.exe"))
parser.add_argument("--ffprobe", type=Path, default=Path(r"C:\FFmpeg\bin\ffprobe.exe"))
parser.add_argument("--sample-rate", type=int, default=32000)
parser.add_argument("--fps", type=float, default=24.0)
parser.add_argument("--expected-frames", type=int, default=360)
parser.add_argument("--boundary-frames", type=int, nargs="+", default=[124, 243])
args = parser.parse_args()
result = analyze(
args.input,
ffmpeg=args.ffmpeg,
ffprobe=args.ffprobe,
sample_rate=args.sample_rate,
fps=args.fps,
boundary_frames=list(args.boundary_frames),
expected_frames=args.expected_frames,
)
args.result.parent.mkdir(parents=True, exist_ok=True)
args.result.write_text(json.dumps(result, indent=2), encoding="utf-8")
print(json.dumps(result))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+163
View File
@@ -0,0 +1,163 @@
"""Run the research-only Reference 22f/37T vs 22f/40T GPU screen."""
from __future__ import annotations
import argparse
import json
from pathlib import Path
import sys
import time
import uuid
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
from p32_api_runner import submit_and_wait
from v36_r1_stress_gate_runner import (
ResourceMonitor,
_extract_evidence,
configure_prompt,
)
PROMPTS = {
"music": (
"A continuous cinematic performance driven by a steady electronic dance "
"beat at 120 BPM. The percussion, bass pulse, and musical phrase continue "
"without stopping while the camera glides smoothly through a neon studio."
),
"dialogue": (
"A single radio host speaks continuously in a calm natural voice inside a "
"quiet studio, maintaining the same speaker, room tone, cadence, and breath "
"throughout the shot. <d>Tonight we follow the lights along the harbor, and "
"every turn reveals another part of the city.</d>"
),
"ambient": (
"A continuous locked cinematic shot of steady rain falling outside a quiet "
"workshop. Rainfall, a low ventilation hum, and distant machinery form an "
"uninterrupted ambient sound bed with stable texture and level."
),
}
TRANSPORTS = {
"baseline37": "reference_context_v1",
"experimental40": "reference_context_audio40_research_v1",
}
def _configure(source, *, label, transport, seed, output_prefix, nonce):
prompt = configure_prompt(
source,
transport=transport,
output_prefix=output_prefix,
seed=seed,
chunks=3,
synchronize_sampling=True,
diagnostic_nonce=nonce,
)
prompt["188"]["inputs"]["value"] = PROMPTS[label]
prompt["305"]["inputs"].update(
{
"width": 576,
"height": 576,
"audio_continuity": True,
"continuation_transport": transport,
}
)
prompt["306"]["inputs"].update(
{
"audio_seam": "Off",
"video_seam": "Off",
"exact_total_duration": True,
}
)
prompt["191"]["inputs"]["filename_prefix"] = output_prefix
return prompt
def _run_one(args, source, *, content, variant, order_index):
transport = TRANSPORTS[variant]
run_root = args.output_root / f"{order_index:02d}_{content}_{variant}"
run_root.mkdir(parents=True, exist_ok=True)
output_prefix = f"video/AudioResearch_{content}_{variant}_seed{args.seed}"
prompt = _configure(
source,
label=content,
transport=transport,
seed=args.seed,
output_prefix=output_prefix,
nonce=order_index,
)
prompt_path = run_root / "prompt.json"
prompt_path.write_text(json.dumps(prompt, indent=2), encoding="utf-8")
with ResourceMonitor(args.backend_pid) as monitor:
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=prompt,
client_id=f"audio-research-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=2.0,
)
history_path = run_root / "history.json"
history_path.write_text(json.dumps(history, indent=2), encoding="utf-8")
summary = {
"content": content,
"variant": variant,
"transport": transport,
"seed": args.seed,
"prompt_id": prompt_id,
"api_elapsed_seconds": elapsed,
"resources": monitor.as_dict(),
"evidence": _extract_evidence(history),
"output_prefix": output_prefix,
"prompt": str(prompt_path),
"history": str(history_path),
}
(run_root / "summary.json").write_text(
json.dumps(summary, indent=2), encoding="utf-8"
)
return summary
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--prompt", type=Path, required=True)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument("--server", default="http://127.0.0.6:8188")
parser.add_argument("--seed", type=int, default=238985343135609)
parser.add_argument("--backend-pid", type=int, default=None)
parser.add_argument("--timeout-seconds", type=float, default=2400.0)
args = parser.parse_args()
source = json.loads(args.prompt.read_text(encoding="utf-8"))
args.output_root.mkdir(parents=True, exist_ok=True)
schedule = (
("music", "baseline37"),
("music", "experimental40"),
("dialogue", "experimental40"),
("dialogue", "baseline37"),
("ambient", "baseline37"),
("ambient", "experimental40"),
)
started = time.perf_counter()
results = [
_run_one(args, source, content=content, variant=variant, order_index=index)
for index, (content, variant) in enumerate(schedule, start=1)
]
aggregate = {
"format": "h3-continuum-audio-context-research-v1",
"elapsed_seconds": time.perf_counter() - started,
"schedule": [list(item) for item in schedule],
"results": results,
}
(args.output_root / "aggregate.json").write_text(
json.dumps(aggregate, indent=2), encoding="utf-8"
)
print(json.dumps(aggregate, ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())
@@ -0,0 +1,3 @@
from .node import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"]
+646
View File
@@ -0,0 +1,646 @@
"""Observation-only Sampling boundary collector for the V3.8 G4 Gate.
The collector is loaded only on the dedicated diagnostic backend. It wraps the
active Production V3.8 sampler for one node execution, returns the unchanged six
Production outputs, and adds a seventh JSON evidence output. Every monkeypatch
is restored in ``finally`` blocks.
"""
from __future__ import annotations
from collections.abc import Mapping, Sequence
from dataclasses import asdict, is_dataclass
from enum import Enum
import hashlib
import importlib
import json
from pathlib import Path
import sys
import time
from types import SimpleNamespace
from typing import Any
import uuid
import torch
CHECKPOINTS = ("S0", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8")
MODEL_CHECKPOINTS = ("M0", "M1", "M2", "M3")
def _type_name(value: Any) -> str:
cls = value if isinstance(value, type) else type(value)
return f"{cls.__module__}.{cls.__qualname__}"
def _callable_name(value: Any) -> str:
return (
f"{getattr(value, '__module__', '')}."
f"{getattr(value, '__qualname__', type(value).__qualname__)}"
)
def _tensor_fingerprint(tensor: torch.Tensor) -> dict[str, Any]:
if bool(getattr(tensor, "is_nested", False)):
return _nested_fingerprint(tensor)
device_before = str(tensor.device)
value = tensor.detach().contiguous().to("cpu")
shape = [int(part) for part in value.shape]
dtype = str(value.dtype)
raw = value.view(torch.uint8).numpy().tobytes(order="C")
digest = hashlib.sha256()
digest.update(dtype.encode("ascii"))
digest.update(b"\0")
digest.update(json.dumps(shape, separators=(",", ":")).encode("ascii"))
digest.update(b"\0")
digest.update(raw)
legacy_digest = hashlib.sha256()
legacy_digest.update(dtype.encode("ascii"))
legacy_digest.update(json.dumps(shape, separators=(",", ":")).encode("ascii"))
legacy_digest.update(raw)
return {
"kind": "tensor",
"shape": shape,
"dtype": dtype,
"device_before_hash": device_before,
"numel": int(value.numel()),
"raw_bytes": len(raw),
"sha256": digest.hexdigest(),
"legacy_g4_sha256": legacy_digest.hexdigest(),
}
def _nested_fingerprint(value: Any) -> dict[str, Any]:
parts = list(value.unbind())
fingerprints = [_tensor_fingerprint(part) for part in parts]
payload = json.dumps(
[item["sha256"] for item in fingerprints],
separators=(",", ":"),
).encode("ascii")
return {
"kind": "nested_tensor",
"parts": fingerprints,
"sha256": hashlib.sha256(payload).hexdigest(),
}
def _canonicalize(value: Any, unsupported: set[str]) -> Any:
if torch.is_tensor(value):
fingerprint = _tensor_fingerprint(value)
fingerprint.pop("legacy_g4_sha256", None)
return {"__tensor__": fingerprint}
if hasattr(value, "unbind") and not isinstance(value, (str, bytes)):
try:
parts = list(value.unbind())
except Exception:
parts = None
if parts is not None and parts and all(torch.is_tensor(part) for part in parts):
fingerprint = _nested_fingerprint(value)
for part in fingerprint.get("parts", []):
part.pop("legacy_g4_sha256", None)
return {"__nested_tensor__": fingerprint}
if value is None or isinstance(value, (bool, int, float, str)):
return value
if isinstance(value, bytes):
return {
"__bytes__": {
"length": len(value),
"sha256": hashlib.sha256(value).hexdigest(),
}
}
if isinstance(value, (torch.dtype, torch.device, uuid.UUID, Enum)):
return {"__type__": _type_name(value), "value": str(value)}
if is_dataclass(value) and not isinstance(value, type):
return {
"__dataclass__": _type_name(value),
"value": _canonicalize(asdict(value), unsupported),
}
if isinstance(value, Mapping):
return {
str(key): _canonicalize(value[key], unsupported)
for key in sorted(value, key=lambda item: str(item))
}
if isinstance(value, (list, tuple)):
return [_canonicalize(item, unsupported) for item in value]
if isinstance(value, (set, frozenset)):
encoded = [_canonicalize(item, unsupported) for item in value]
return sorted(
encoded,
key=lambda item: json.dumps(item, sort_keys=True, separators=(",", ":")),
)
if callable(value):
return {"__callable__": _callable_name(value)}
unsupported.add(_type_name(value))
return {"__opaque_type__": _type_name(value)}
def _structured_checkpoint(value: Any) -> dict[str, Any]:
unsupported: set[str] = set()
canonical = _canonicalize(value, unsupported)
encoded = json.dumps(
canonical,
sort_keys=True,
separators=(",", ":"),
ensure_ascii=False,
).encode("utf-8")
return {
"kind": "structured",
"sha256": hashlib.sha256(encoded).hexdigest(),
"canonical_bytes": len(encoded),
"unsupported_types": sorted(unsupported),
"payload": canonical,
}
def _value_checkpoint(value: Any) -> dict[str, Any]:
if torch.is_tensor(value):
return _tensor_fingerprint(value)
if hasattr(value, "unbind") and not isinstance(value, (str, bytes)):
try:
parts = list(value.unbind())
except Exception:
parts = None
if parts is not None and parts and all(torch.is_tensor(part) for part in parts):
return _nested_fingerprint(value)
return _structured_checkpoint(value)
def _av_checkpoint(value: Any, state_module: Any) -> dict[str, Any]:
if isinstance(value, Mapping) and "samples" in value:
video, audio = state_module.extract_av_streams(dict(value))
else:
samples = value
parts = list(samples.unbind()) if hasattr(samples, "unbind") else list(samples)
if len(parts) < 2:
raise RuntimeError("expected MiniMax H3 Video/Audio NestedTensor")
video, audio = parts[0], parts[1]
streams = {
"video": _tensor_fingerprint(video),
"audio": _tensor_fingerprint(audio),
}
combined = json.dumps(
{name: item["sha256"] for name, item in streams.items()},
sort_keys=True,
separators=(",", ":"),
).encode("ascii")
return {
"kind": "h3_av",
"streams": streams,
"sha256": hashlib.sha256(combined).hexdigest(),
}
def _runtime_descriptor(value: Any, depth: int = 0) -> Any:
if depth > 7:
return {"__truncated_type__": _type_name(value)}
if value is None or isinstance(value, (bool, int, float, str)):
return value
if isinstance(value, (torch.dtype, torch.device, uuid.UUID, Enum)):
return {"type": _type_name(value), "value": str(value)}
if torch.is_tensor(value):
return {
"type": "tensor",
"shape": [int(part) for part in value.shape],
"dtype": str(value.dtype),
"device": str(value.device),
}
if isinstance(value, Mapping):
return {
str(key): _runtime_descriptor(item, depth + 1)
for key, item in sorted(value.items(), key=lambda item: str(item[0]))
}
if isinstance(value, (list, tuple)):
return [_runtime_descriptor(item, depth + 1) for item in value]
if isinstance(value, (set, frozenset)):
items = [_runtime_descriptor(item, depth + 1) for item in value]
return sorted(items, key=lambda item: json.dumps(item, sort_keys=True))
if callable(value):
return {"callable": _callable_name(value)}
return {"type": _type_name(value)}
def _wrapper_state(patcher: Any) -> dict[str, Any]:
output: dict[str, Any] = {}
for wrapper_type, keyed in sorted(
dict(getattr(patcher, "wrappers", {}) or {}).items(),
key=lambda item: str(item[0]),
):
output[str(wrapper_type)] = {
str(key): [_callable_name(item) for item in list(items or [])]
for key, items in sorted(
dict(keyed or {}).items(), key=lambda item: str(item[0])
)
}
return output
def _patch_value_summary(value: Any) -> Any:
if isinstance(value, (list, tuple)):
return [_patch_value_summary(item) for item in value]
if isinstance(value, Mapping):
return {
str(key): _patch_value_summary(item)
for key, item in sorted(value.items(), key=lambda item: str(item[0]))
}
if callable(value):
return {"callable": _callable_name(value)}
return {"type": _type_name(value)}
def _model_runtime_state(patcher: Any) -> dict[str, Any]:
model = getattr(patcher, "model", None)
current_uuid = getattr(model, "current_weight_patches_uuid", None)
patches = dict(getattr(patcher, "patches", {}) or {})
model_options = dict(getattr(patcher, "model_options", {}) or {})
dtype = None
try:
dtype = str(patcher.model_dtype())
except Exception:
dtype = None
inference_dtype = None
try:
inference_dtype = str(model.get_dtype_inference()) if model is not None else None
except Exception:
inference_dtype = None
observed = {
"patcher_type": _type_name(patcher),
"model_type": None if model is None else _type_name(model),
"diffusion_model_type": (
None
if model is None or getattr(model, "diffusion_model", None) is None
else _type_name(model.diffusion_model)
),
"patch_uuid": str(getattr(patcher, "patches_uuid", "")),
"current_weight_patch_uuid": (
None if current_uuid is None else str(current_uuid)
),
"load_device": str(getattr(patcher, "load_device", "")),
"offload_device": str(getattr(patcher, "offload_device", "")),
"model_device": None if model is None else str(getattr(model, "device", "")),
"model_dtype": dtype,
"inference_dtype": inference_dtype,
"hook_mode": str(getattr(patcher, "hook_mode", "")),
"current_hook_group_type": (
None
if getattr(patcher, "current_hook_group", None) is None
else _type_name(patcher.current_hook_group)
),
"wrappers": _wrapper_state(patcher),
"patches": {
str(key): _patch_value_summary(value)
for key, value in sorted(patches.items(), key=lambda item: str(item[0]))
},
"object_patches": {
str(key): _type_name(value)
for key, value in sorted(
dict(getattr(patcher, "object_patches", {}) or {}).items(),
key=lambda item: str(item[0]),
)
},
"model_options": _runtime_descriptor(model_options),
"torch": {
"deterministic_algorithms": bool(torch.are_deterministic_algorithms_enabled()),
"cudnn_deterministic": bool(torch.backends.cudnn.deterministic),
"cudnn_benchmark": bool(torch.backends.cudnn.benchmark),
"cuda_matmul_allow_tf32": bool(torch.backends.cuda.matmul.allow_tf32),
"cudnn_allow_tf32": bool(torch.backends.cudnn.allow_tf32),
},
}
semantic = dict(observed)
semantic.pop("patch_uuid", None)
semantic.pop("current_weight_patch_uuid", None)
semantic["patch_uuid_present"] = bool(observed["patch_uuid"])
semantic["current_weight_patch_uuid_present"] = (
observed["current_weight_patch_uuid"] is not None
)
checkpoint = _structured_checkpoint(semantic)
checkpoint["observed_state"] = observed
return checkpoint
def _sampler_signature(sampler: Any) -> dict[str, Any]:
value = {
"sampler_type": _type_name(sampler),
"sampler_function": (
None
if getattr(sampler, "sampler_function", None) is None
else _callable_name(sampler.sampler_function)
),
"extra_options": _runtime_descriptor(
dict(getattr(sampler, "extra_options", {}) or {})
),
"inpaint_options": _runtime_descriptor(
dict(getattr(sampler, "inpaint_options", {}) or {})
),
}
return _structured_checkpoint(value)
def _resolve_modules() -> SimpleNamespace:
import nodes as comfy_nodes
sampler_class = comfy_nodes.NODE_CLASS_MAPPINGS.get("H3ContinuumSamplerV38")
if sampler_class is None:
raise RuntimeError("H3ContinuumSamplerV38 is not loaded")
owner = sys.modules.get(sampler_class.__module__)
source = getattr(owner, "__file__", None)
if source is None:
raise RuntimeError("cannot resolve active H3 Continuum package")
module_name = sampler_class.__module__
base_name = (
module_name.split(".v3.", 1)[0]
if ".v3." in module_name
else module_name.rsplit(".", 2)[0]
)
return SimpleNamespace(
sampler_class=sampler_class,
production_root=str(Path(source).resolve().parent.parent),
sequence=importlib.import_module(f"{base_name}.v2.sequence"),
state=importlib.import_module(f"{base_name}.state"),
)
class SamplingCollector:
def __init__(self, modules: SimpleNamespace, *, trace_steps: bool):
self.modules = modules
self.trace_steps = bool(trace_steps)
self.records: list[dict[str, Any]] = []
self.hooks_restored = True
def _capture_model_payload(
self,
record: dict[str, Any],
*,
x: Any,
t: Any,
c_concat: Any,
c_crossattn: Any,
control: Any,
transformer_options: Any,
kwargs: Mapping[str, Any],
output: Any,
) -> None:
record["model"] = {
"M0": _value_checkpoint(x),
"M1": _value_checkpoint(t),
"M2": _structured_checkpoint(
{
"c_concat": c_concat,
"c_crossattn": c_crossattn,
"control": control,
"transformer_options": transformer_options,
"kwargs": dict(kwargs),
}
),
"M3": _value_checkpoint(output),
}
def wrap(self, original_sample):
def observed_sample_chunk(**kwargs):
import comfy.model_base
import comfy.sample
import comfy.samplers
record: dict[str, Any] = {
"physical_call_index": len(self.records) + 1,
"seed": int(kwargs["seed"]),
"checkpoints": {},
"model": {},
"steps": [],
}
checkpoints = record["checkpoints"]
checkpoints["S0"] = _av_checkpoint(kwargs["latent"], self.modules.state)
checkpoints["S3"] = _value_checkpoint(kwargs["latent"].get("noise_mask"))
checkpoints["S4"] = _tensor_fingerprint(kwargs["sigmas"])
checkpoints["S5"] = _structured_checkpoint(int(kwargs["seed"]))
checkpoints["S6"] = _sampler_signature(kwargs["sampler"])
checkpoints["S7"] = _structured_checkpoint(kwargs["conditioning"])
checkpoints["S8"] = _model_runtime_state(kwargs["model"])
original_fix = comfy.sample.fix_empty_latent_channels
original_noise = comfy.sample.prepare_noise
original_apply = comfy.model_base.BaseModel._apply_model
original_guider_sample = comfy.samplers.CFGGuider.sample
fixed_seen = False
noise_seen = False
model_seen = False
pending_model_payload = None
def fix_empty(*args, **inner_kwargs):
nonlocal fixed_seen
result = original_fix(*args, **inner_kwargs)
if not fixed_seen:
fixed_seen = True
checkpoints["S1"] = _av_checkpoint(result, self.modules.state)
return result
def prepare_noise(*args, **inner_kwargs):
nonlocal noise_seen
result = original_noise(*args, **inner_kwargs)
if not noise_seen:
noise_seen = True
checkpoints["S2"] = _av_checkpoint(result, self.modules.state)
return result
def apply_model(
model_self,
x,
t,
c_concat=None,
c_crossattn=None,
control=None,
transformer_options={},
**inner_kwargs,
):
nonlocal model_seen, pending_model_payload
result = original_apply(
model_self,
x,
t,
c_concat,
c_crossattn,
control,
transformer_options,
**inner_kwargs,
)
if not model_seen and model_self.__class__.__name__ == "MiniMaxH3":
model_seen = True
# Retain references only. Hashing CUDA tensors here forces a
# host synchronization between sampler steps and masks the
# ordering defect this Gate is intended to observe.
pending_model_payload = {
"x": x,
"t": t,
"c_concat": c_concat,
"c_crossattn": c_crossattn,
"control": control,
"transformer_options": transformer_options,
"kwargs": dict(inner_kwargs),
"output": result,
}
return result
def guider_sample(guider_self, *args, **inner_kwargs):
existing_callback = inner_kwargs.get("callback")
def chained_callback(step, x0, x, total_steps):
if existing_callback is not None:
existing_callback(step, x0, x, total_steps)
record["steps"].append(
{
"step": int(step),
"total_steps": int(total_steps),
"x0": _value_checkpoint(x0),
"x": _value_checkpoint(x),
}
)
if self.trace_steps:
inner_kwargs["callback"] = chained_callback
record["step_callback_chained"] = existing_callback is not None
return original_guider_sample(guider_self, *args, **inner_kwargs)
comfy.sample.fix_empty_latent_channels = fix_empty
comfy.sample.prepare_noise = prepare_noise
comfy.model_base.BaseModel._apply_model = apply_model
if self.trace_steps:
comfy.samplers.CFGGuider.sample = guider_sample
started = time.perf_counter()
self.records.append(record)
try:
result = original_sample(**kwargs)
if pending_model_payload is not None:
self._capture_model_payload(record, **pending_model_payload)
record["output"] = _av_checkpoint(result, self.modules.state)
record["sampling_elapsed_seconds"] = time.perf_counter() - started
record["model_runtime_after"] = _model_runtime_state(kwargs["model"])
return result
finally:
comfy.sample.fix_empty_latent_channels = original_fix
comfy.sample.prepare_noise = original_noise
comfy.model_base.BaseModel._apply_model = original_apply
comfy.samplers.CFGGuider.sample = original_guider_sample
restored = (
comfy.sample.fix_empty_latent_channels is original_fix
and comfy.sample.prepare_noise is original_noise
and comfy.model_base.BaseModel._apply_model is original_apply
and comfy.samplers.CFGGuider.sample is original_guider_sample
)
record["hooks_restored"] = bool(restored)
record["capture_complete"] = {
"fixed_latent": fixed_seen,
"noise": noise_seen,
"first_model_call": model_seen,
"all_sampling_checkpoints": all(
name in checkpoints for name in CHECKPOINTS
),
"all_model_checkpoints": all(
name in record["model"] for name in MODEL_CHECKPOINTS
),
}
self.hooks_restored = self.hooks_restored and bool(restored)
return observed_sample_chunk
class H3G4SamplingDeterminismDiagnostic:
RETURN_TYPES = (
"LATENT",
"LATENT",
"H3_CONTINUUM_ASSEMBLY_PLAN",
"STRING",
"AUDIO",
"H3_CONTINUUM_REFINE_CONTEXT",
"STRING",
)
RETURN_NAMES = (
"video_latents",
"audio_latents",
"assembly_plan",
"status",
"driving_audio",
"refine_context",
"diagnostic_json",
)
OUTPUT_IS_LIST = (True, True, False, False, False, False, False)
FUNCTION = "run"
CATEGORY = "H3 Continuum/Diagnostics"
OUTPUT_NODE = True
@classmethod
def INPUT_TYPES(cls):
modules = _resolve_modules()
schema = modules.sampler_class.INPUT_TYPES()
required = dict(schema.get("required", {}))
required["diagnostic_label"] = (
"STRING",
{"default": "sampling_diagnostic"},
)
required["diagnostic_nonce"] = (
"INT",
{"default": 0, "min": 0, "max": 0x7FFFFFFF},
)
required["trace_steps"] = ("BOOLEAN", {"default": False})
schema["required"] = required
return schema
@classmethod
def IS_CHANGED(cls, generation_mode="Full Run", **kwargs):
# Preserve the public V3.8 cache contract exactly: Full Run is stable,
# while Review Each Chunk is intentionally non-cacheable.
modules = _resolve_modules()
return modules.sampler_class.IS_CHANGED(generation_mode=generation_mode)
def run(
self,
diagnostic_label,
diagnostic_nonce,
trace_steps,
**kwargs,
):
modules = _resolve_modules()
sequence = modules.sequence
collector = SamplingCollector(modules, trace_steps=bool(trace_steps))
original_sample = sequence.sample_chunk
model = kwargs.get("model")
state_before = _model_runtime_state(model)
sequence.sample_chunk = collector.wrap(original_sample)
started = time.perf_counter()
try:
outputs = modules.sampler_class().run(**kwargs)
state_after = _model_runtime_state(model)
finally:
sequence.sample_chunk = original_sample
restored = sequence.sample_chunk is original_sample and collector.hooks_restored
result = {
"format": "h3-v38-g4-sampling-determinism-v1",
"diagnostic_label": str(diagnostic_label),
"diagnostic_nonce": int(diagnostic_nonce),
"trace_steps": bool(trace_steps),
"production_root": modules.production_root,
"sampler_node_elapsed_seconds": time.perf_counter() - started,
"sampler_model_state_before": state_before,
"sampler_model_state_after": state_after,
"physical_samples": collector.records,
"physical_sample_count": len(collector.records),
"hooks_restored": bool(restored),
}
payload = json.dumps(
result,
sort_keys=True,
separators=(",", ":"),
ensure_ascii=False,
)
return {
"ui": {"text": [payload]},
"result": (*outputs, payload),
}
NODE_CLASS_MAPPINGS = {
"H3G4SamplingDeterminismDiagnostic": H3G4SamplingDeterminismDiagnostic,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"H3G4SamplingDeterminismDiagnostic": "H3 G4 Sampling Determinism Diagnostic",
}
+32
View File
@@ -0,0 +1,32 @@
# H3 Core vs Continuum GPU Diagnostic
This is a standalone diagnostic node. It does not change or register through the
production H3 Continuum package.
## Install for ComfyUI_W
Run from the repository root:
```powershell
.\tools\install_gpu_diagnostic_node.ps1
```
Restart ComfyUI and search for `H3 Core vs Continuum GPU Diagnostic`.
## Connections
- Connect the same direct MODEL used by both tests. Keep Spectrum off.
- Connect the same CLIP, Video VAE, Audio VAE, sampler, and sigmas.
- Connect the exact same First Frame and Last Frame.
- Use 640x640, seed 42, 8 steps, Euler/simple for the baseline comparison.
- Run Storage and Session are not used by this node.
One queue executes Core FL2VA at 243 frames, then Continuum Terminal Merge 2x5s.
Results are written below:
```text
ComfyUI/output/h3_continuum/diagnostics/<timestamp>/
```
The report identifies the first difference across pre-sampler data, first H3
MODEL call, sampled physical latent, split/recombine, and decode/assembly.
+9
View File
@@ -0,0 +1,9 @@
"""Standalone GPU diagnostics for H3 Continuum.
This package is intentionally separate from the production node package. Copy the
directory into ComfyUI/custom_nodes only while diagnostics are needed.
"""
from .nodes import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"]
+302
View File
@@ -0,0 +1,302 @@
"""Deterministic tensor capture and comparison helpers for the GPU runner."""
from __future__ import annotations
from collections.abc import Mapping, Sequence
from dataclasses import asdict, dataclass
from hashlib import sha256
import json
from pathlib import Path
import re
from typing import Any
import torch
STAGE_ORDER = (
"A_pre_sampler",
"B_first_model_call",
"C_physical_sample",
"D_split_recombine",
"E1_decode_243",
"E2_final_240",
)
def _dense_cpu(tensor: torch.Tensor) -> torch.Tensor:
return tensor.detach().to("cpu").contiguous()
def _raw_sha256(tensor: torch.Tensor) -> str:
value = _dense_cpu(tensor)
raw = value.view(torch.uint8).numpy().tobytes()
return sha256(raw).hexdigest()
def tensor_fingerprint(tensor: torch.Tensor) -> dict[str, Any]:
value = _dense_cpu(tensor)
return {
"shape": list(value.shape),
"dtype": str(value.dtype),
"sha256": _raw_sha256(value),
"numel": int(value.numel()),
}
@dataclass(frozen=True)
class TensorDifference:
comparable: bool
bit_exact: bool
allclose: bool
max_abs_diff: float | None
mean_abs_diff: float | None
reason: str | None = None
@property
def matches(self) -> bool:
return self.comparable and (self.bit_exact or self.allclose)
def to_dict(self) -> dict[str, Any]:
value = asdict(self)
value["matches"] = self.matches
return value
def compare_tensors(
left: torch.Tensor,
right: torch.Tensor,
*,
rtol: float = 1.0e-4,
atol: float = 1.0e-5,
) -> TensorDifference:
a = _dense_cpu(left)
b = _dense_cpu(right)
if tuple(a.shape) != tuple(b.shape):
return TensorDifference(False, False, False, None, None, "shape mismatch")
if a.dtype != b.dtype:
return TensorDifference(False, False, False, None, None, "dtype mismatch")
bit_exact = bool(torch.equal(a, b))
if not (a.is_floating_point() or a.is_complex()):
return TensorDifference(True, bit_exact, bit_exact, 0.0 if bit_exact else None, 0.0 if bit_exact else None)
af = a.to(torch.float64)
bf = b.to(torch.float64)
delta = (af - bf).abs()
max_abs = float(delta.max().item()) if delta.numel() else 0.0
mean_abs = float(delta.mean().item()) if delta.numel() else 0.0
close = bool(torch.allclose(af, bf, rtol=rtol, atol=atol, equal_nan=True))
return TensorDifference(True, bit_exact, close, max_abs, mean_abs)
def _safe_tensor_key(value: str) -> str:
return re.sub(r"[^A-Za-z0-9_.-]+", "_", value).strip("_") or "tensor"
def _is_primitive(value: Any) -> bool:
return value is None or isinstance(value, (bool, int, float, str))
def _primitive_sequence(value: Any) -> bool:
return isinstance(value, (list, tuple)) and all(_is_primitive(item) for item in value)
class DiagnosticCapture:
def __init__(self, side: str):
self.side = side
self.manifest: dict[str, Any] = {
"format": "h3-core-continuum-gpu-diagnostic-v1",
"side": side,
"stages": {},
}
self.tensors: dict[str, torch.Tensor] = {}
self.runtime: dict[str, Any] = {}
def _stage(self, stage: str) -> dict[str, Any]:
return self.manifest["stages"].setdefault(stage, {"values": {}, "tensors": {}})
def add_value(self, stage: str, field: str, value: Any) -> None:
if not (_is_primitive(value) or _primitive_sequence(value)):
value = json.loads(json.dumps(value, default=str))
self._stage(stage)["values"][field] = value
def add_tensor(self, stage: str, field: str, tensor: torch.Tensor, *, persist: bool = True) -> None:
if not torch.is_tensor(tensor):
raise TypeError(f"{field} is not a tensor")
if getattr(tensor, "is_nested", False):
parts = list(tensor.unbind())
self.add_value(stage, f"{field}.components", len(parts))
for index, part in enumerate(parts):
self.add_tensor(stage, f"{field}.component_{index}", part, persist=persist)
return
value = _dense_cpu(tensor)
key = _safe_tensor_key(f"{stage}.{field}")
self._stage(stage)["tensors"][field] = {
**tensor_fingerprint(value),
"stored_as": key if persist else None,
}
if persist:
self.tensors[key] = value.clone()
def capture_value(
self,
stage: str,
field: str,
value: Any,
*,
persist_tensors: bool = True,
depth: int = 0,
) -> None:
if depth > 7:
self.add_value(stage, f"{field}.truncated", True)
return
if torch.is_tensor(value):
self.add_tensor(stage, field, value, persist=persist_tensors)
return
if _is_primitive(value):
self.add_value(stage, field, value)
return
if _primitive_sequence(value):
self.add_value(stage, field, list(value))
return
if isinstance(value, Mapping):
keys = sorted(str(key) for key in value.keys())
self.add_value(stage, f"{field}.keys", keys)
for key in keys[:256]:
try:
child = value[key]
except (KeyError, TypeError):
child = next((item for raw, item in value.items() if str(raw) == key), None)
self.capture_value(
stage,
f"{field}.{key}",
child,
persist_tensors=persist_tensors,
depth=depth + 1,
)
return
if isinstance(value, Sequence) and not isinstance(value, (str, bytes, bytearray)):
self.add_value(stage, f"{field}.length", len(value))
for index, child in enumerate(value[:256]):
self.capture_value(
stage,
f"{field}.{index}",
child,
persist_tensors=persist_tensors,
depth=depth + 1,
)
return
if hasattr(value, "unbind") and value.__class__.__name__ == "NestedTensor":
parts = list(value.unbind())
self.add_value(stage, f"{field}.components", len(parts))
for index, child in enumerate(parts):
label = ("video", "audio")[index] if index < 2 else f"component_{index}"
self.capture_value(
stage,
f"{field}.{label}",
child,
persist_tensors=persist_tensors,
depth=depth + 1,
)
return
if all(hasattr(value, name) for name in ("signature", "segments", "position_ids")):
self.add_value(stage, f"{field}.type", value.__class__.__name__)
self.add_value(stage, f"{field}.signature", list(value.signature))
self.add_value(stage, f"{field}.segments", [list(item) for item in value.segments])
for name in ("position_ids", "img_pos", "img_update", "audio_pos", "audio_update"):
child = getattr(value, name, None)
if torch.is_tensor(child):
self.add_tensor(stage, f"{field}.{name}", child, persist=persist_tensors)
return
self.add_value(stage, f"{field}.type", value.__class__.__name__)
def save(self, directory: Path) -> None:
directory.mkdir(parents=True, exist_ok=True)
from safetensors.torch import save_file
save_file(self.tensors, str(directory / "tensors.safetensors"))
(directory / "manifest.json").write_text(
json.dumps(self.manifest, indent=2, ensure_ascii=False),
encoding="utf-8",
)
def compare_stage(
left: DiagnosticCapture,
right: DiagnosticCapture,
stage: str,
) -> dict[str, Any]:
a = left.manifest["stages"].get(stage, {"values": {}, "tensors": {}})
b = right.manifest["stages"].get(stage, {"values": {}, "tensors": {}})
details: dict[str, Any] = {}
first_field: str | None = None
value_fields = list(a["values"].keys()) + [key for key in b["values"] if key not in a["values"]]
for field in value_fields:
av = a["values"].get(field, {"missing": True})
bv = b["values"].get(field, {"missing": True})
matches = av == bv
details[field] = {"kind": "value", "matches": matches, "left": av, "right": bv}
if not matches and first_field is None:
first_field = field
tensor_fields = list(a["tensors"].keys()) + [key for key in b["tensors"] if key not in a["tensors"]]
for field in tensor_fields:
a_meta = a["tensors"].get(field)
b_meta = b["tensors"].get(field)
if a_meta is None or b_meta is None:
result = {"kind": "tensor", "matches": False, "reason": "missing tensor"}
else:
a_key = a_meta.get("stored_as")
b_key = b_meta.get("stored_as")
if a_key in left.tensors and b_key in right.tensors:
result = {"kind": "tensor", **compare_tensors(left.tensors[a_key], right.tensors[b_key]).to_dict()}
else:
matches = a_meta == b_meta
result = {"kind": "fingerprint", "matches": matches, "left": a_meta, "right": b_meta}
details[field] = result
if not result["matches"] and first_field is None:
first_field = field
return {
"status": "MATCH" if first_field is None else "DIFFER",
"first_field": first_field,
"fields": details,
}
def recombine_terminal_streams(
first: torch.Tensor,
second: torch.Tensor,
*,
first_range: tuple[int, int] | list[int],
second_range: tuple[int, int] | list[int],
dim: int,
) -> torch.Tensor:
first_start, first_stop = (int(value) for value in first_range)
second_start, second_stop = (int(value) for value in second_range)
overlap = first_stop - second_start
if first_start != 0 or overlap < 0:
raise ValueError("terminal split ranges do not form an overlapping sequence")
if first.shape[dim] != first_stop - first_start:
raise ValueError("first logical stream length does not match its range")
if second.shape[dim] != second_stop - second_start:
raise ValueError("second logical stream length does not match its range")
if overlap > second.shape[dim]:
raise ValueError("terminal overlap exceeds the second logical stream")
slices = [slice(None)] * second.ndim
slices[dim] = slice(overlap, None)
return torch.cat((first, second[tuple(slices)]), dim=dim).contiguous()
def format_comparison(comparison: Mapping[str, Any], output_directory: Path) -> str:
lines = []
first_divergence = None
for stage in STAGE_ORDER:
result = comparison.get(stage, {"status": "NOT_RUN", "first_field": None})
lines.append(f"{stage}: {result['status']}")
if first_divergence is None and result["status"] == "DIFFER":
field = result.get("first_field")
first_divergence = f"{stage}.{field}" if field else stage
lines.append("")
lines.append(f"First divergence: {first_divergence or 'none'}")
lines.append(f"Output: {output_directory}")
return "\n".join(lines)
@@ -0,0 +1,222 @@
"""Diagnostic-only continuation depth and mask-strength policies for Issue #13."""
from __future__ import annotations
import time
from typing import Any, Callable
import torch
MODE_BASELINE_22 = "Baseline 22f"
MODE_DEPTH_13 = "Video Depth 13f"
MODE_DEPTH_9 = "Video Depth 9f"
MODE_STRENGTH_75 = "Video Strength 75%"
MODE_DEPTH_13_STRENGTH_75 = "Video Depth 13f + Strength 75%"
MODE_OPTIONS = (
MODE_BASELINE_22,
MODE_DEPTH_13,
MODE_DEPTH_9,
MODE_STRENGTH_75,
MODE_DEPTH_13_STRENGTH_75,
)
_PREFIX_SLOTS = 7
_AUDIO_PREFIX_STEPS = 37
_ACTIVE_SLOT_CONTRACT = {
MODE_BASELINE_22: (_PREFIX_SLOTS, 22),
MODE_DEPTH_13: (4, 13),
MODE_DEPTH_9: (3, 9),
MODE_STRENGTH_75: (_PREFIX_SLOTS, 22),
MODE_DEPTH_13_STRENGTH_75: (4, 13),
}
def _mask_values(tensor: torch.Tensor) -> list[float]:
return [float(value) for value in torch.unique(tensor.detach().float()).tolist()]
class R28ContinuationPolicy:
"""Scoped callback used only by the external R2.8 diagnostic node."""
def __init__(
self,
mode: str,
*,
tensor_summary: Callable[[torch.Tensor], dict[str, Any]],
) -> None:
if mode not in MODE_OPTIONS:
raise ValueError(f"unknown R2.8 continuation policy: {mode!r}")
self.mode = str(mode)
self.tensor_summary = tensor_summary
self._prepared: dict[int, dict[str, Any]] = {}
self.sample_calls: list[dict[str, Any]] = []
@property
def active_video_slots(self) -> int:
return int(_ACTIVE_SLOT_CONTRACT[self.mode][0])
@property
def active_video_frames(self) -> int:
return int(_ACTIVE_SLOT_CONTRACT[self.mode][1])
@property
def prefix_denoise_mask_value(self) -> float:
if self.mode in (MODE_STRENGTH_75, MODE_DEPTH_13_STRENGTH_75):
return 0.25
return 0.0
def prepare_masked_latent(
self,
*,
latent: dict[str, Any],
physical_group: int,
logical_chunks: tuple[int, ...],
context_frames: int,
video_context: torch.Tensor,
audio_context: torch.Tensor | None,
) -> dict[str, Any]:
if int(context_frames) != 22:
raise ValueError("R2.8 policy requires the unchanged Balanced 22-frame transport")
mask = latent.get("noise_mask")
if mask is None or not hasattr(mask, "unbind"):
raise ValueError("R2.8 policy requires the native nested AV noise mask")
parts = list(mask.unbind())
if len(parts) < 2:
raise ValueError("R2.8 policy requires Video and Audio masks")
video_mask, audio_mask = parts[0], parts[1]
if int(video_mask.shape[2]) <= _PREFIX_SLOTS:
raise ValueError("R2.8 Video target has no generated region after the prefix")
if int(audio_mask.shape[-1]) <= _AUDIO_PREFIX_STEPS:
raise ValueError("R2.8 Audio target has no generated region after the prefix")
original_video_mask = video_mask.detach().clone()
original_audio_mask = audio_mask.detach().clone()
transformed = latent
if self.mode != MODE_BASELINE_22:
video_mask = video_mask.clone()
oldest_inactive = _PREFIX_SLOTS - self.active_video_slots
if oldest_inactive:
video_mask[:, :, :oldest_inactive] = 1.0
video_mask[
:, :, oldest_inactive:_PREFIX_SLOTS
] = self.prefix_denoise_mask_value
transformed = dict(latent)
transformed["noise_mask"] = mask.__class__((video_mask, audio_mask.clone()))
effective_mask = list(transformed["noise_mask"].unbind())
effective_video_mask, effective_audio_mask = effective_mask[0], effective_mask[1]
self._prepared[int(physical_group)] = {
"physical_group": int(physical_group),
"logical_chunks": [int(value) for value in logical_chunks],
"transport_context_frames": int(context_frames),
"transport_video_prefix_slots": _PREFIX_SLOTS,
"transport_audio_prefix_steps": _AUDIO_PREFIX_STEPS,
"active_video_slots": self.active_video_slots,
"active_video_frames": self.active_video_frames,
"video_prefix_denoise_mask_value": self.prefix_denoise_mask_value,
"video_context": self.tensor_summary(video_context),
"audio_context": self.tensor_summary(audio_context)
if audio_context is not None
else None,
"source_samples_object_passthrough": transformed.get("samples")
is latent.get("samples"),
"input_video_mask_unchanged": bool(
torch.equal(original_video_mask, list(mask.unbind())[0])
),
"input_audio_mask_unchanged": bool(
torch.equal(original_audio_mask, list(mask.unbind())[1])
),
"effective_video_prefix_mask_values": _mask_values(
effective_video_mask[:, :, :_PREFIX_SLOTS]
),
"effective_video_generated_mask_values": _mask_values(
effective_video_mask[:, :, _PREFIX_SLOTS:]
),
"effective_audio_prefix_mask_values": _mask_values(
effective_audio_mask[..., :_AUDIO_PREFIX_STEPS]
),
"effective_audio_generated_mask_values": _mask_values(
effective_audio_mask[..., _AUDIO_PREFIX_STEPS:]
),
}
return transformed
def before_sampling(
self,
*,
physical_group: int,
logical_chunks: tuple[int, ...],
context_frames: int,
latent: dict[str, Any],
conditioning: Any,
seed: int,
sigmas: torch.Tensor,
) -> dict[str, Any]:
samples = list(latent["samples"].unbind())
metadata = conditioning[0][1] if conditioning else {}
record = {
"physical_group": int(physical_group),
"logical_chunks": [int(value) for value in logical_chunks],
"context_frames": int(context_frames),
"seed": int(seed),
"sigmas": self.tensor_summary(sigmas),
"input_video": self.tensor_summary(samples[0]),
"input_audio": self.tensor_summary(samples[1]),
"prompt_embedding": self.tensor_summary(conditioning[0][0])
if conditioning and torch.is_tensor(conditioning[0][0])
else None,
"minimax_frame_count": metadata.get("minimax_frame_count"),
"policy": self._prepared.get(int(physical_group)),
}
return {"started": time.perf_counter(), "record": record}
def after_sampling(
self,
*,
token: dict[str, Any],
physical_group: int,
logical_chunks: tuple[int, ...],
context_frames: int,
latent: dict[str, Any],
sampled: dict[str, Any],
seed: int,
sigmas: torch.Tensor,
) -> None:
record = dict(token["record"])
record["sample_host_elapsed_seconds"] = float(
time.perf_counter() - float(token["started"])
)
source_video, source_audio = list(latent["samples"].unbind())[:2]
output_video, output_audio = list(sampled["samples"].unbind())[:2]
record["output_video"] = self.tensor_summary(output_video)
record["output_audio"] = self.tensor_summary(output_audio)
if int(context_frames) == 22:
source_video_prefix = source_video[:, :, :_PREFIX_SLOTS]
output_video_prefix = output_video[:, :, :_PREFIX_SLOTS]
source_audio_prefix = source_audio[..., :_AUDIO_PREFIX_STEPS]
output_audio_prefix = output_audio[..., :_AUDIO_PREFIX_STEPS]
record["restored_video_prefix_bit_exact"] = bool(
torch.equal(source_video_prefix, output_video_prefix)
)
record["restored_audio_prefix_bit_exact"] = bool(
torch.equal(source_audio_prefix, output_audio_prefix)
)
record["restored_video_prefix"] = self.tensor_summary(output_video_prefix)
record["restored_audio_prefix"] = self.tensor_summary(output_audio_prefix)
self.sample_calls.append(record)
def finalize(self) -> dict[str, Any]:
return {
"format": "h3-continuum-issue13-r28-policy-v1",
"mode": self.mode,
"transport_context_frames": 22,
"transport_video_prefix_slots": _PREFIX_SLOTS,
"transport_audio_prefix_steps": _AUDIO_PREFIX_STEPS,
"active_video_slots": self.active_video_slots,
"active_video_frames": self.active_video_frames,
"video_prefix_denoise_mask_value": self.prefix_denoise_mask_value,
"audio_contract": "unchanged 37T protected prefix",
"sample_calls": list(self.sample_calls),
"sampling_count": len(self.sample_calls),
}
File diff suppressed because it is too large Load Diff
+164
View File
@@ -0,0 +1,164 @@
"""Diagnostic-only one-shot appearance re-anchor for Issue #13 R2.7."""
from __future__ import annotations
from typing import Any
import torch
SOFT_REANCHOR_STRENGTH = 0.5
_EPSILON = 1.0e-6
def _validate_pixels(value: torch.Tensor, *, label: str) -> torch.Tensor:
if not torch.is_tensor(value) or value.ndim != 5 or int(value.shape[-1]) != 3:
raise ValueError(f"{label} must be [B,F,H,W,3] RGB pixels")
measured = value.detach().float()
if not bool(torch.isfinite(measured).all().item()):
raise ValueError(f"{label} contains NaN or Inf")
return measured
def rgb_appearance_summary(value: torch.Tensor) -> dict[str, float]:
"""Return global low-frequency RGB appearance statistics."""
rgb = _validate_pixels(value, label="pixels")
red, green, blue = rgb.unbind(dim=-1)
luma = 0.2126 * red + 0.7152 * green + 0.0722 * blue
maximum = rgb.amax(dim=-1)
minimum = rgb.amin(dim=-1)
saturation = (maximum - minimum) / maximum.clamp_min(_EPSILON)
return {
"luma_mean": float(luma.mean().item()),
"luma_std": float(luma.std(unbiased=False).item()),
"saturation_mean": float(saturation.mean().item()),
"chroma_u_mean": float((blue - luma).mean().item()),
"chroma_u_std": float((blue - luma).std(unbiased=False).item()),
"chroma_v_mean": float((red - luma).mean().item()),
"chroma_v_std": float((red - luma).std(unbiased=False).item()),
"highlight_fraction": float((luma >= (250.0 / 255.0)).float().mean().item()),
}
def _matched_channel(
current: torch.Tensor,
reference: torch.Tensor,
) -> tuple[torch.Tensor, float]:
current_mean = current.mean()
current_std = current.std(unbiased=False).clamp_min(_EPSILON)
reference_mean = reference.mean()
reference_std = reference.std(unbiased=False).clamp_min(_EPSILON)
scale = (reference_std / current_std).clamp(0.5, 2.0)
return (current - current_mean) * scale + reference_mean, float(scale.item())
def soft_match_rgb_appearance(
current_pixels: torch.Tensor,
reference_pixels: torch.Tensor,
*,
strength: float = SOFT_REANCHOR_STRENGTH,
) -> tuple[torch.Tensor, dict[str, Any]]:
"""Match only global luma/chroma statistics; preserve spatial coordinates."""
amount = float(strength)
if not 0.0 <= amount <= 1.0:
raise ValueError("soft re-anchor strength must be in [0,1]")
current = _validate_pixels(current_pixels, label="current_pixels")
reference = _validate_pixels(reference_pixels, label="reference_pixels")
if tuple(current.shape[-3:]) != tuple(reference.shape[-3:]):
raise ValueError("current/reference pixel geometry must match")
current_r, current_g, current_b = current.unbind(dim=-1)
reference_r, reference_g, reference_b = reference.unbind(dim=-1)
current_y = 0.2126 * current_r + 0.7152 * current_g + 0.0722 * current_b
reference_y = (
0.2126 * reference_r + 0.7152 * reference_g + 0.0722 * reference_b
)
current_u = current_b - current_y
current_v = current_r - current_y
reference_u = reference_b - reference_y
reference_v = reference_r - reference_y
matched_y, luma_scale = _matched_channel(current_y, reference_y)
matched_u, chroma_u_scale = _matched_channel(current_u, reference_u)
matched_v, chroma_v_scale = _matched_channel(current_v, reference_v)
matched_r = matched_y + matched_v
matched_b = matched_y + matched_u
matched_g = (matched_y - 0.2126 * matched_r - 0.0722 * matched_b) / 0.7152
fully_matched = torch.stack((matched_r, matched_g, matched_b), dim=-1).clamp(0.0, 1.0)
adjusted = (current + amount * (fully_matched - current)).clamp(0.0, 1.0)
delta = adjusted - current
report = {
"strength": amount,
"method": "global Y/U/V mean+std match; 50% blend; no blur/warp/resize",
"scales": {
"luma": luma_scale,
"chroma_u": chroma_u_scale,
"chroma_v": chroma_v_scale,
},
"reference": rgb_appearance_summary(reference),
"before": rgb_appearance_summary(current),
"fully_matched": rgb_appearance_summary(fully_matched),
"after": rgb_appearance_summary(adjusted),
"pixel_delta_rms": float(delta.square().mean().sqrt().item()),
"pixel_delta_max_abs": float(delta.abs().max().item()),
"geometry_unchanged": tuple(adjusted.shape) == tuple(current.shape),
}
return adjusted.to(dtype=current_pixels.dtype), report
def soft_reanchor_context(
current_context: torch.Tensor,
reference_context: torch.Tensor,
*,
video_vae: Any,
strength: float = SOFT_REANCHOR_STRENGTH,
) -> tuple[torch.Tensor, dict[str, Any]]:
"""Decode/adjust/encode one temporary context copy without mutating inputs."""
if not torch.is_tensor(current_context) or current_context.ndim != 5:
raise ValueError("current_context must be [B,C,T,H,W]")
if not torch.is_tensor(reference_context) or reference_context.ndim != 5:
raise ValueError("reference_context must be [B,C,T,H,W]")
if tuple(current_context.shape) != tuple(reference_context.shape):
raise ValueError("current/reference latent context shapes must match")
if int(current_context.shape[0]) != 1:
raise ValueError("R2.7 diagnostic expects a single context batch")
if not callable(getattr(video_vae, "decode", None)) or not callable(
getattr(video_vae, "encode", None)
):
raise ValueError("R2.7 Soft Re-anchor requires the connected Video VAE")
current_guard = current_context.detach().clone()
reference_guard = reference_context.detach().clone()
reference_pixels = video_vae.decode(reference_context)
current_pixels = video_vae.decode(current_context)
adjusted_pixels, appearance = soft_match_rgb_appearance(
current_pixels,
reference_pixels,
strength=strength,
)
encoded = video_vae.encode(adjusted_pixels[0])
if not torch.is_tensor(encoded) or tuple(encoded.shape) != tuple(current_context.shape):
raise ValueError(
"Soft Re-anchor VAE roundtrip changed the continuation latent shape: "
f"{getattr(encoded, 'shape', None)} vs {tuple(current_context.shape)}"
)
encoded = encoded.to(
device=current_context.device,
dtype=current_context.dtype,
).contiguous()
if not bool(torch.isfinite(encoded.float()).all().item()):
raise ValueError("Soft Re-anchor encoded context contains NaN or Inf")
appearance.update(
{
"current_context_unchanged": bool(torch.equal(current_context, current_guard)),
"reference_context_unchanged": bool(
torch.equal(reference_context, reference_guard)
),
"latent_shape_unchanged": tuple(encoded.shape)
== tuple(current_context.shape),
}
)
return encoded, appearance
+29
View File
@@ -0,0 +1,29 @@
param(
[string]$ComfyUIRoot = 'D:\StabilityMatrix\Data\Packages\ComfyUI_W'
)
$ErrorActionPreference = 'Stop'
$source = Join-Path $PSScriptRoot 'gpu_diagnostic_node'
$customNodes = Join-Path $ComfyUIRoot 'custom_nodes'
$destination = Join-Path $customNodes 'H3_Continuum_GPU_Diagnostics'
if (-not (Test-Path -LiteralPath $source -PathType Container)) {
throw "Diagnostic source was not found: $source"
}
if (-not (Test-Path -LiteralPath $customNodes -PathType Container)) {
throw "ComfyUI custom_nodes directory was not found: $customNodes"
}
New-Item -ItemType Directory -Path $destination -Force | Out-Null
foreach ($name in @('__init__.py', 'capture.py', 'nodes.py', 'README.md')) {
Copy-Item -LiteralPath (Join-Path $source $name) -Destination (Join-Path $destination $name) -Force
}
$sourceCount = (Get-ChildItem -LiteralPath $source -File).Count
$destinationCount = (Get-ChildItem -LiteralPath $destination -File).Count
if ($destinationCount -lt $sourceCount) {
throw "Diagnostic install verification failed: $destinationCount < $sourceCount"
}
Write-Output "Installed standalone diagnostics: $destination"
Write-Output 'Restart ComfyUI before using the diagnostic node.'
+180
View File
@@ -0,0 +1,180 @@
"""Measure chunk-wise color/contrast drift and build an Issue #13 contact sheet."""
from __future__ import annotations
import argparse
import csv
import hashlib
import json
from pathlib import Path
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
def _video_path(output_root: Path, record: dict, video_root: Path) -> Path:
item = record.get("output_video") or {}
return video_root / str(item.get("subfolder", "")) / str(item.get("filename", ""))
def _read_frames(path: Path) -> tuple[list[np.ndarray], float]:
capture = cv2.VideoCapture(str(path))
if not capture.isOpened():
raise RuntimeError(f"cannot open video: {path}")
fps = float(capture.get(cv2.CAP_PROP_FPS) or 24.0)
frames = []
while True:
ok, frame = capture.read()
if not ok:
break
frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
capture.release()
if not frames:
raise RuntimeError(f"video contains no frames: {path}")
return frames, fps
def _frame_metrics(frame: np.ndarray) -> dict[str, float]:
rgb = frame.astype(np.float32) / 255.0
y = 0.2126 * rgb[..., 0] + 0.7152 * rgb[..., 1] + 0.0722 * rgb[..., 2]
hsv = cv2.cvtColor(frame, cv2.COLOR_RGB2HSV).astype(np.float32)
saturation = hsv[..., 1] / 255.0
return {
"luma_mean": float(np.mean(y)),
"luma_std": float(np.std(y)),
"red_mean": float(np.mean(rgb[..., 0])),
"green_mean": float(np.mean(rgb[..., 1])),
"blue_mean": float(np.mean(rgb[..., 2])),
"saturation_mean": float(np.mean(saturation)),
"saturation_p95": float(np.percentile(saturation, 95)),
"contrast_p95_p05": float(np.percentile(y, 95) - np.percentile(y, 5)),
"highlight_clip_fraction": float(np.mean(y >= (250.0 / 255.0))),
"shadow_clip_fraction": float(np.mean(y <= (5.0 / 255.0))),
}
def _chunk_metrics(frames: list[np.ndarray], *, chunks: int, radius: int) -> tuple[list[dict], list[np.ndarray]]:
frame_count = len(frames)
rows = []
representatives = []
for index in range(chunks):
center = int(round((index + 0.5) * frame_count / chunks))
center = min(max(center, 0), frame_count - 1)
start = max(0, center - radius)
end = min(frame_count, center + radius + 1)
values = [_frame_metrics(frame) for frame in frames[start:end]]
merged = {
key: float(np.mean([item[key] for item in values])) for key in values[0]
}
merged.update(
{
"chunk": index + 1,
"center_frame": center,
"sample_start": start,
"sample_end_exclusive": end,
"sample_count": end - start,
}
)
rows.append(merged)
representatives.append(frames[center])
return rows, representatives
def _contact_sheet(rows: list[tuple[str, list[np.ndarray]]], output: Path) -> None:
thumb_w, thumb_h = 256, 256
label_w, top_h = 220, 42
width = label_w + thumb_w * 5
height = top_h + thumb_h * len(rows)
sheet = Image.new("RGB", (width, height), (9, 15, 25))
draw = ImageDraw.Draw(sheet)
font = ImageFont.load_default()
for column in range(5):
draw.text(
(label_w + column * thumb_w + 8, 14),
f"Chunk {column + 1}",
fill=(210, 225, 245),
font=font,
)
for row_index, (label, frames) in enumerate(rows):
y = top_h + row_index * thumb_h
draw.text((10, y + 16), label, fill=(115, 210, 255), font=font)
for column, frame in enumerate(frames):
image = Image.fromarray(frame).resize((thumb_w, thumb_h), Image.Resampling.LANCZOS)
sheet.paste(image, (label_w + column * thumb_w, y))
output.parent.mkdir(parents=True, exist_ok=True)
sheet.save(output)
def run(args: argparse.Namespace) -> dict:
summary = json.loads((args.output_root / "summary.json").read_text(encoding="utf-8"))
all_rows = []
sheets = []
videos = {}
for record in summary["cases"]:
case = str(record["case"])
path = _video_path(args.output_root, record, args.video_root)
frames, fps = _read_frames(path)
metrics, representatives = _chunk_metrics(
frames, chunks=5, radius=args.radius
)
for row in metrics:
row["case"] = case
row["video"] = str(path)
row["fps"] = fps
row["frame_count"] = len(frames)
all_rows.append(row)
sheets.append((case, representatives))
videos[case] = {
"path": str(path),
"sha256": hashlib.sha256(path.read_bytes()).hexdigest(),
"fps": fps,
"frame_count": len(frames),
"metrics": metrics,
"drift_chunk5_minus_chunk1": {
key: metrics[-1][key] - metrics[0][key]
for key in (
"luma_mean",
"luma_std",
"saturation_mean",
"saturation_p95",
"contrast_p95_p05",
"highlight_clip_fraction",
"shadow_clip_fraction",
)
},
}
csv_path = args.output_root / "chunk_metrics.csv"
with csv_path.open("w", encoding="utf-8", newline="") as handle:
writer = csv.DictWriter(handle, fieldnames=list(all_rows[0]))
writer.writeheader()
writer.writerows(all_rows)
contact_path = args.output_root / "contact_sheet.png"
_contact_sheet(sheets, contact_path)
result = {
"format": "h3-continuum-issue13-drift-analysis-v1",
"sample_radius_frames": args.radius,
"videos": videos,
"csv": str(csv_path),
"contact_sheet": str(contact_path),
}
(args.output_root / "analysis.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(json.dumps(result, ensure_ascii=False))
return result
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument(
"--video-root", type=Path, default=Path(r"D:\output\video\comfy_video")
)
parser.add_argument("--radius", type=int, default=12)
return parser
if __name__ == "__main__":
run(_parser().parse_args())
+262
View File
@@ -0,0 +1,262 @@
"""Run the Issue #13 long-form contrast/style drift diagnostic matrix.
This tool is diagnostic-only. It mutates an accepted API prompt in memory and
does not import into or change the Production node path.
"""
from __future__ import annotations
import argparse
import copy
import hashlib
import json
from pathlib import Path
import sys
import uuid
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
from p32_api_runner import submit_and_wait
BASE_PROMPT = (
"A continuous cinematic live-action tracking shot of one woman in an "
"orange jacket riding a skateboard steadily forward along a waterfront "
"promenade. The camera follows her in one smooth direction. Preserve her "
"face, clothing, skateboard, warm sunlight, detailed texture, rich color, "
"and continuous forward motion. Natural skateboard wheels, wind, and "
"distant city ambience are audible."
)
VARIED_PROMPTS = (
BASE_PROMPT,
BASE_PROMPT
+ " Keep the lighting natural and avoid increasing contrast or saturation.",
BASE_PROMPT
+ " Use restrained color, soft highlight rolloff, and matte natural skin texture.",
BASE_PROMPT
+ " Maintain neutral exposure, stable white balance, and gentle midtone contrast.",
BASE_PROMPT
+ " Preserve the original natural color and texture without glossy or oily buildup.",
)
CASE_ORDER = (
"fixed_standard",
"list_same_standard",
"timeline_same_standard",
"list_varied_standard",
"fixed_compatibility",
)
def _script_for_case(case: str) -> tuple[str, str, str]:
if case == "fixed_standard":
return "Fixed", BASE_PROMPT, "Standard"
if case == "list_same_standard":
return "List", "\n---\n".join([BASE_PROMPT] * 5), "Standard"
if case == "timeline_same_standard":
return (
"Timeline",
"\n\n".join(
f"[Chunk {index}]\n{BASE_PROMPT}" for index in range(1, 6)
),
"Standard",
)
if case == "list_varied_standard":
return "List", "\n---\n".join(VARIED_PROMPTS), "Standard"
if case == "fixed_compatibility":
return "Fixed", BASE_PROMPT, "Compatibility"
raise ValueError(f"unknown Issue #13 case: {case}")
def configure_prompt(
source: dict,
*,
case: str,
output_prefix: str,
seed: int,
steps: int,
size: int,
) -> dict:
prompt = copy.deepcopy(source)
prompt_mode, script, backend = _script_for_case(case)
sampler = prompt["305"]
sampler["class_type"] = "H3ContinuumSamplerV36"
sampler["_meta"] = {"title": f"Issue #13 Drift Diagnostic — {case}"}
inputs = sampler["inputs"]
inputs.update(
{
"prompt_mode": prompt_mode,
"chunks": 5,
"chunk_seconds": 5.0,
"width": int(size),
"height": int(size),
"continuity": "Balanced — 22 frames",
"base_seed": int(seed),
"audio_continuity": True,
"diagnostics": "Detailed Report",
"reroll_from_chunk": "Auto",
"reroll_nonce": 0,
"strict_compatibility": False,
"debug": False,
"show_preview": False,
"run_storage": "Off",
"run_name": "",
"project_id": "",
"continuation_backend": backend,
"first_frame": ["119", 0],
}
)
for key in (
"continuation_transport",
"synchronize_sampling",
"diagnostic_nonce",
"last_frame",
):
inputs.pop(key, None)
prompt["188"]["inputs"]["value"] = script
prompt["153"]["inputs"].update(
{"scheduler": "simple", "steps": int(steps), "denoise": 1.0}
)
prompt["297"]["inputs"]["value"] = max(0.05, (float(size) * size) / 1_000_000.0)
prompt["306"]["inputs"].update(
{
"audio_seam": "Off",
"video_seam": "Off",
"buffer_backend": "Auto",
"diagnostics": "Detailed Report",
}
)
prompt["191"]["inputs"]["filename_prefix"] = str(output_prefix)
prompt["249"]["inputs"]["source"] = ["305", 3]
return prompt
def _output_video(history: dict, prompt_id: str) -> dict | None:
entry = history.get(str(prompt_id), {})
outputs = entry.get("outputs", {}) if isinstance(entry, dict) else {}
save = outputs.get("191", {}) if isinstance(outputs, dict) else {}
images = save.get("images", []) if isinstance(save, dict) else []
return images[0] if images else None
def _status_text(history: dict, prompt_id: str) -> str:
entry = history.get(str(prompt_id), {})
outputs = entry.get("outputs", {}) if isinstance(entry, dict) else {}
preview = outputs.get("249", {}) if isinstance(outputs, dict) else {}
values = preview.get("text", []) if isinstance(preview, dict) else []
return str(values[0]) if values else ""
def run(args: argparse.Namespace) -> dict:
source = json.loads(args.prompt.read_text(encoding="utf-8"))
cases = list(args.cases or CASE_ORDER)
unknown = [case for case in cases if case not in CASE_ORDER]
if unknown:
raise SystemExit(f"unknown --cases: {', '.join(unknown)}")
args.output_root.mkdir(parents=True, exist_ok=True)
summary_path = args.output_root / "summary.json"
results = []
if summary_path.exists():
existing = json.loads(summary_path.read_text(encoding="utf-8"))
results = list(existing.get("cases") or [])
completed_cases = {str(item.get("case")) for item in results}
for case in cases:
if case in completed_cases:
print(f"ISSUE13 SKIP completed case={case}", flush=True)
continue
configured = configure_prompt(
source,
case=case,
output_prefix=f"video/issue13/Issue13_{case}_seed{args.seed}",
seed=args.seed,
steps=args.steps,
size=args.size,
)
case_root = args.output_root / case
case_root.mkdir(parents=True, exist_ok=True)
prompt_path = case_root / "prompt.json"
prompt_path.write_text(
json.dumps(configured, indent=2, ensure_ascii=False),
encoding="utf-8",
)
print(f"ISSUE13 START case={case}", flush=True)
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=configured,
client_id=f"issue13-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=3.0,
)
history_path = case_root / "history.json"
history_path.write_text(
json.dumps(history, indent=2, ensure_ascii=False),
encoding="utf-8",
)
text = _status_text(history, prompt_id)
result = {
"case": case,
"prompt_id": prompt_id,
"api_elapsed_seconds": elapsed,
"prompt": str(prompt_path),
"history": str(history_path),
"output_video": _output_video(history, prompt_id),
"status_sha256": hashlib.sha256(text.encode("utf-8")).hexdigest(),
"status": text,
}
(case_root / "summary.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False),
encoding="utf-8",
)
results.append(result)
print(
f"ISSUE13 COMPLETE case={case} prompt_id={prompt_id} "
f"elapsed={elapsed:.3f}s",
flush=True,
)
summary = {
"format": "h3-continuum-issue13-drift-gate-v1",
"server": args.server,
"source_prompt": str(args.prompt),
"seed": int(args.seed),
"steps": int(args.steps),
"size": int(args.size),
"chunks": 5,
"chunk_seconds": 5.0,
"cases": results,
}
summary_path.write_text(
json.dumps(summary, indent=2, ensure_ascii=False),
encoding="utf-8",
)
return summary
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--server", default="http://127.0.0.6:8188")
parser.add_argument("--prompt", type=Path, required=True)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument("--seed", type=int, default=130013)
parser.add_argument("--steps", type=int, default=8)
parser.add_argument("--size", type=int, default=384)
parser.add_argument("--timeout-seconds", type=float, default=3600.0)
parser.add_argument("--cases", nargs="*", choices=CASE_ORDER)
return parser
def main() -> int:
summary = run(_parser().parse_args())
print(json.dumps(summary, ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+408
View File
@@ -0,0 +1,408 @@
"""Run the staged Issue #13 R2.5 cause-isolation GPU gate.
This is evidence tooling only. It uses the external diagnostic node and keeps
the Production sampler, conditioning, Run Storage, and assembly code intact.
"""
from __future__ import annotations
import argparse
import copy
import hashlib
import json
from pathlib import Path
import sys
import uuid
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
from p32_api_runner import submit_and_wait
TURBO_LORA = "minimax\\minimax_h3_fl2v_turbo_4step_v1.0_768p_comfyui_bf16.safetensors"
MOTION_BOOSTER_LORA = "minimax\\H3_Motion_BoosterV2.safetensors"
MOTION_REPAIR_LORA = "minimax\\Motion_Repair.safetensors"
CASE_SPECS = {
"screen_3x5_turbo": {"chunks": 3, "chunk_seconds": 5.0, "steps": 8, "profile": "turbo"},
"screen_3x10_turbo": {"chunks": 3, "chunk_seconds": 10.0, "steps": 8, "profile": "turbo"},
"screen_6x5_turbo": {"chunks": 6, "chunk_seconds": 5.0, "steps": 8, "profile": "turbo"},
"screen_6x5_turbo_fixed_prompt": {
"chunks": 6,
"chunk_seconds": 5.0,
"steps": 8,
"profile": "turbo",
"fixed_prompt": True,
},
"screen_6x5_turbo_fixed_prefix": {
"chunks": 6,
"chunk_seconds": 5.0,
"steps": 8,
"profile": "turbo",
"fixed_prompt": True,
"continuation_source_mode": "Fixed Group 1 Tail",
},
"screen_6x5_turbo_soft_reanchor": {
"chunks": 6,
"chunk_seconds": 5.0,
"steps": 8,
"profile": "turbo",
"fixed_prompt": True,
"continuation_source_mode": "Soft Re-anchor Group 4",
},
"extend_4x10_turbo": {"chunks": 4, "chunk_seconds": 10.0, "steps": 8, "profile": "turbo"},
"extend_5x10_turbo": {"chunks": 5, "chunk_seconds": 10.0, "steps": 8, "profile": "turbo"},
"extend_6x10_turbo": {"chunks": 6, "chunk_seconds": 10.0, "steps": 8, "profile": "turbo"},
"motion_off_3x5_normal": {"chunks": 3, "chunk_seconds": 5.0, "steps": 20, "profile": "motion_off"},
"motion_both_3x5_normal": {"chunks": 3, "chunk_seconds": 5.0, "steps": 20, "profile": "motion_both"},
"motion_booster_3x5_normal": {"chunks": 3, "chunk_seconds": 5.0, "steps": 20, "profile": "motion_booster"},
"motion_repair_3x5_normal": {"chunks": 3, "chunk_seconds": 5.0, "steps": 20, "profile": "motion_repair"},
}
PROMPT_ACTIONS = (
"She holds a calm gaze while a light breeze moves a few strands of hair and a thin curl of smoke rises.",
"Her eyes shift gently toward the camera as her cigarette hand lifts slightly and her hair moves in the breeze.",
"She blinks and turns her head a little while a strand of hair brushes her cheek and the smoke curls upward.",
"Her shoulders settle into a subtly different pose as the cigarette lowers slightly and her bangs move softly.",
"She looks just past the camera and then back while the smoke disperses and the movement of her hair continues.",
"She returns to a steady gaze with her cigarette hand near her shoulder and her hair settling naturally.",
)
DIAGNOSTIC_MARKER = "V3.6-R1 diagnostic: "
def _prompt(action: str, *, motion_trigger: bool) -> str:
trigger = "dynv2, " if motion_trigger else ""
return (
"integrated_multimodal_description: [Shot 1] "
f"{trigger}Live-action close-up of the same blonde woman at a Route 66 gas station, "
"wearing the same white lace camisole and holding the same cigarette. "
"The camera remains static, the daylight exposure stays stable, and her natural skin texture, facial features, clothing, and background remain consistent. "
f"{action}\n\n"
"overall_soundscape: A soft outdoor breeze, distant road ambience, and faint fabric and hair movement remain audible.\n\n"
"non_diegetic_music: N/A"
)
def _prompt_list(
chunks: int, *, motion_trigger: bool, fixed_prompt: bool = False
) -> str:
if fixed_prompt:
return "\n---\n".join(
_prompt(PROMPT_ACTIONS[0], motion_trigger=motion_trigger)
for _ in range(int(chunks))
)
indices = (0, 2, 4) if int(chunks) == 3 else tuple(range(int(chunks)))
return "\n---\n".join(
_prompt(PROMPT_ACTIONS[index], motion_trigger=motion_trigger)
for index in indices
)
def _configure_loras(loader_inputs: dict, profile: str) -> list[dict]:
for name, value in list(loader_inputs.items()):
if name.startswith("lora_") and isinstance(value, dict):
value["on"] = False
selected: list[dict] = []
def set_slot(slot: int, name: str, strength: float = 1.0) -> None:
loader_inputs[f"lora_{slot}"] = {
"on": True,
"lora": name,
"strength": float(strength),
}
selected.append({"name": name, "strength": float(strength)})
if profile == "turbo":
set_slot(1, TURBO_LORA)
elif profile == "motion_both":
set_slot(1, MOTION_BOOSTER_LORA)
set_slot(2, MOTION_REPAIR_LORA)
elif profile == "motion_booster":
set_slot(1, MOTION_BOOSTER_LORA)
elif profile == "motion_repair":
set_slot(1, MOTION_REPAIR_LORA)
elif profile != "motion_off":
raise ValueError(f"unknown LoRA profile: {profile}")
return selected
def configure_prompt(
source: dict,
*,
case: str,
output_prefix: str,
image_name: str,
seed: int,
width: int,
height: int,
diagnostic_nonce: int,
) -> tuple[dict, list[dict]]:
if case not in CASE_SPECS:
raise ValueError(f"unknown Issue #13 R2.5 case: {case}")
spec = CASE_SPECS[case]
chunks = int(spec["chunks"])
chunk_seconds = float(spec["chunk_seconds"])
profile = str(spec["profile"])
prompt = copy.deepcopy(source)
sampler = prompt["305"]
sampler["class_type"] = "H3ContinuumMaskedPrefixR1Diagnostic"
sampler["_meta"] = {"title": f"Issue #13 R2.5 Diagnostic — {case}"}
inputs = sampler["inputs"]
inputs.update(
{
"prompt_mode": "List",
"chunks": chunks,
"chunk_seconds": chunk_seconds,
"width": int(width),
"height": int(height),
"continuity": "Balanced — 22 frames",
"base_seed": int(seed),
"audio_continuity": True,
"diagnostics": "Detailed Report",
"reroll_from_chunk": "Auto",
"reroll_nonce": 0,
"strict_compatibility": False,
"debug": False,
"show_preview": False,
"run_storage": "Off",
"run_name": "",
"project_id": "",
"continuation_transport": "masked_av_prefix_22_v1",
"synchronize_sampling": True,
"diagnostic_nonce": int(diagnostic_nonce),
"continuation_source_mode": str(
spec.get("continuation_source_mode", "Recursive")
),
"first_frame": ["119", 0],
}
)
for key in (
"continuation_backend",
"generation_mode",
"review_action",
"max_new_physical_groups",
"aspect",
"preset",
"custom_mp",
"guide",
"last_frame",
):
inputs.pop(key, None)
motion_profile = profile.startswith("motion_")
prompt["188"]["inputs"]["value"] = _prompt_list(
chunks,
motion_trigger=motion_profile,
fixed_prompt=bool(spec.get("fixed_prompt", False)),
)
prompt["114"]["inputs"]["image"] = str(image_name)
prompt["139"]["inputs"]["sampler_name"] = "euler"
prompt["153"]["inputs"].update(
{"scheduler": "simple", "steps": int(spec["steps"]), "denoise": 1.0}
)
prompt["297"]["inputs"]["value"] = max(
0.05, (float(width) * float(height)) / 1_000_000.0
)
prompt["306"]["inputs"].update(
{
"exact_total_duration": True,
"audio_seam": "Off",
"video_seam": "Off",
"buffer_backend": "Auto",
"diagnostics": "Detailed Report",
}
)
prompt["191"]["inputs"]["filename_prefix"] = str(output_prefix)
prompt["249"]["inputs"]["source"] = ["305", 3]
spectrum = prompt.get("150", {}).get("inputs", {})
if spectrum:
spectrum["enabled"] = False
selected_loras = _configure_loras(prompt["286"]["inputs"], profile)
return prompt, selected_loras
def _relative_lora_path(name: str) -> Path:
return Path(*str(name).replace("\\", "/").split("/"))
def _inventory_loras(lora_root: Path, selected: list[dict]) -> list[dict]:
inventory = []
for item in selected:
path = lora_root / _relative_lora_path(str(item["name"]))
if not path.is_file():
raise FileNotFoundError(f"required LoRA is missing: {path}")
inventory.append(
{
**item,
"path": str(path),
"bytes": path.stat().st_size,
"sha256": hashlib.sha256(path.read_bytes()).hexdigest(),
}
)
return inventory
def _output_video(history: dict, prompt_id: str) -> dict | None:
entry = history.get(str(prompt_id), {})
outputs = entry.get("outputs", {}) if isinstance(entry, dict) else {}
save = outputs.get("191", {}) if isinstance(outputs, dict) else {}
images = save.get("images", []) if isinstance(save, dict) else []
return images[0] if images else None
def _status_text(history: dict, prompt_id: str) -> str:
entry = history.get(str(prompt_id), {})
outputs = entry.get("outputs", {}) if isinstance(entry, dict) else {}
preview = outputs.get("249", {}) if isinstance(outputs, dict) else {}
values = preview.get("text", []) if isinstance(preview, dict) else []
return str(values[0]) if values else ""
def _diagnostic_from_status(status: str) -> dict:
if DIAGNOSTIC_MARKER not in status:
raise RuntimeError("Issue #13 R2.5 diagnostic marker is missing")
value = json.loads(status.rsplit(DIAGNOSTIC_MARKER, 1)[1].strip())
if not isinstance(value, dict):
raise RuntimeError("Issue #13 R2.5 diagnostic payload is not an object")
return value
def run(args: argparse.Namespace) -> dict:
source = json.loads(args.prompt.read_text(encoding="utf-8"))
cases = list(
args.cases
or ("screen_3x5_turbo", "screen_3x10_turbo", "screen_6x5_turbo")
)
image_sha256 = hashlib.sha256(args.image_source.read_bytes()).hexdigest()
args.output_root.mkdir(parents=True, exist_ok=True)
summary_path = args.output_root / "summary.json"
results = []
if summary_path.exists():
results = list(
json.loads(summary_path.read_text(encoding="utf-8")).get("cases") or []
)
completed_cases = {str(item.get("case")) for item in results}
for offset, case in enumerate(cases):
if case in completed_cases:
print(f"ISSUE13-R25 SKIP completed case={case}", flush=True)
continue
configured, selected = configure_prompt(
source,
case=case,
output_prefix=f"video/issue13_r25/Issue13_R25_{case}_seed{args.seed}",
image_name=args.image_name,
seed=args.seed,
width=args.width,
height=args.height,
diagnostic_nonce=args.diagnostic_nonce + offset,
)
lora_inventory = _inventory_loras(args.lora_root, selected)
case_root = args.output_root / case
case_root.mkdir(parents=True, exist_ok=True)
prompt_path = case_root / "prompt.json"
prompt_path.write_text(
json.dumps(configured, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(f"ISSUE13-R25 START case={case}", flush=True)
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=configured,
client_id=f"issue13-r25-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=3.0,
)
history_path = case_root / "history.json"
history_path.write_text(
json.dumps(history, indent=2, ensure_ascii=False), encoding="utf-8"
)
status = _status_text(history, prompt_id)
spec = CASE_SPECS[case]
result = {
"case": case,
"chunks": int(spec["chunks"]),
"chunk_seconds": float(spec["chunk_seconds"]),
"profile": str(spec["profile"]),
"steps": int(spec["steps"]),
"sampler": "euler",
"scheduler": "simple",
"prompt_id": prompt_id,
"api_elapsed_seconds": elapsed,
"prompt": str(prompt_path),
"history": str(history_path),
"output_video": _output_video(history, prompt_id),
"status_sha256": hashlib.sha256(status.encode("utf-8")).hexdigest(),
"lora_inventory": lora_inventory,
"diagnostic": _diagnostic_from_status(status),
}
(case_root / "summary.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
)
results.append(result)
summary = {
"format": "h3-continuum-issue13-r25-gate-v1",
"server": args.server,
"source_prompt": str(args.prompt),
"input_image": {
"source": str(args.image_source),
"runtime_name": args.image_name,
"sha256": image_sha256,
},
"seed": int(args.seed),
"width": int(args.width),
"height": int(args.height),
"sampler": "euler",
"scheduler": "simple",
"continuity": "Balanced — 22 frames",
"audio_continuity": True,
"transport": "masked_av_prefix_22_v1",
"spectrum": False,
"video_seam": "Off",
"audio_seam": "Off",
"cases": results,
}
summary_path.write_text(
json.dumps(summary, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(
f"ISSUE13-R25 COMPLETE case={case} prompt_id={prompt_id} elapsed={elapsed:.3f}s",
flush=True,
)
return json.loads(summary_path.read_text(encoding="utf-8"))
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--server", default="http://127.0.0.6:8190")
parser.add_argument("--prompt", type=Path, required=True)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument("--image-source", type=Path, required=True)
parser.add_argument("--image-name", required=True)
parser.add_argument(
"--lora-root",
type=Path,
default=Path(r"D:\StabilityMatrix\Data\Packages\ComfyUI_W\models\loras"),
)
parser.add_argument("--seed", type=int, default=131325)
parser.add_argument("--width", type=int, default=576)
parser.add_argument("--height", type=int, default=864)
parser.add_argument("--diagnostic-nonce", type=int, default=132500)
parser.add_argument("--timeout-seconds", type=float, default=7200.0)
parser.add_argument("--cases", nargs="*", choices=tuple(CASE_SPECS))
return parser
def main() -> int:
print(json.dumps(run(_parser().parse_args()), ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+292
View File
@@ -0,0 +1,292 @@
"""Compare Issue #13 R2.8 candidates with the accepted recursive 22f baseline."""
from __future__ import annotations
import argparse
import csv
import json
from pathlib import Path
import sys
import cv2
import numpy as np
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
import issue13_r2_analyze as base
DRIFT_KEYS = (
"saturation_mean",
"contrast_p95_p05",
"sharpness_laplacian_variance",
"gradient_mean",
"highlight_clip_fraction",
"skin_highlight_proxy_fraction",
)
def _case_video(record: dict, video_root: Path) -> Path:
value = record.get("output_video") or {}
return video_root / str(value.get("subfolder", "")) / str(value.get("filename", ""))
def _end_metrics(frames: list[np.ndarray], *, chunks: int, radius: int) -> list[dict]:
rows = []
for index in range(int(chunks)):
stop = min(len(frames), int(round((index + 1) * len(frames) / chunks)))
start = max(0, stop - int(radius))
values = [base._frame_metrics(frame) for frame in frames[start:stop]]
merged = {
key: float(np.mean([item[key] for item in values])) for key in values[0]
}
merged.update(
{
"group": index + 1,
"sample_start": start,
"sample_end_exclusive": stop,
"sample_count": stop - start,
}
)
rows.append(merged)
return rows
def _face_crop(frame: np.ndarray) -> tuple[np.ndarray, str]:
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
)
faces = cascade.detectMultiScale(gray, scaleFactor=1.08, minNeighbors=5)
if len(faces):
x, y, width, height = max(faces, key=lambda box: int(box[2]) * int(box[3]))
margin_x = int(round(width * 0.12))
margin_y = int(round(height * 0.12))
x0, y0 = max(0, x - margin_x), max(0, y - margin_y)
x1 = min(frame.shape[1], x + width + margin_x)
y1 = min(frame.shape[0], y + height + margin_y)
return frame[y0:y1, x0:x1], "haar"
height, width = frame.shape[:2]
return frame[
int(height * 0.08) : int(height * 0.68),
int(width * 0.18) : int(width * 0.82),
], "center_fallback"
def _face_descriptor(frame: np.ndarray) -> tuple[np.ndarray, str]:
crop, method = _face_crop(frame)
gray = cv2.cvtColor(crop, cv2.COLOR_RGB2GRAY)
gray = cv2.resize(gray, (64, 64), interpolation=cv2.INTER_AREA).astype(np.float32)
gray = (gray - gray.mean()) / max(float(gray.std()), 1.0e-6)
descriptor = cv2.dct(gray)[:16, :16].reshape(-1)[1:]
descriptor /= max(float(np.linalg.norm(descriptor)), 1.0e-6)
return descriptor, method
def _identity_and_frame_difference(representatives: list[np.ndarray]) -> dict:
descriptors = []
methods = []
crops = []
for frame in representatives:
descriptor, method = _face_descriptor(frame)
crop, _ = _face_crop(frame)
descriptors.append(descriptor)
methods.append(method)
crops.append(
cv2.resize(crop, (128, 128), interpolation=cv2.INTER_AREA).astype(np.float32)
/ 255.0
)
reference = descriptors[0]
similarities = [float((np.dot(reference, value) + 1.0) * 0.5) for value in descriptors]
adjacent_mae = [
float(np.abs(crops[index] - crops[index - 1]).mean())
for index in range(1, len(crops))
]
return {
"contract": "face-structure DCT similarity proxy; not a biometric identity score",
"crop_methods": methods,
"group1_similarity": similarities,
"group1_to_group6_similarity": similarities[-1],
"adjacent_face_frame_mae": adjacent_mae,
"mean_adjacent_face_frame_mae": float(np.mean(adjacent_mae)),
}
def _metric_comparison(baseline: dict, candidate: dict) -> dict:
metrics = {}
reductions = []
for key in DRIFT_KEYS:
baseline_trend = baseline["end_trend"][key]
candidate_trend = candidate["end_trend"][key]
baseline_delta = float(baseline_trend["last_minus_first"])
candidate_delta = float(candidate_trend["last_minus_first"])
baseline_slope = float(baseline_trend["linear_slope_per_chunk"])
candidate_slope = float(candidate_trend["linear_slope_per_chunk"])
reduction = None
slope_reduction = None
if baseline_delta > 0.0:
reduction = (baseline_delta - candidate_delta) / baseline_delta * 100.0
reductions.append(reduction)
if baseline_slope > 0.0:
slope_reduction = (
(baseline_slope - candidate_slope) / baseline_slope * 100.0
)
metrics[key] = {
"baseline_delta": baseline_delta,
"candidate_delta": candidate_delta,
"reduction_percent": reduction,
"baseline_slope_per_continuation": baseline_slope,
"candidate_slope_per_continuation": candidate_slope,
"slope_reduction_percent": slope_reduction,
"scored": baseline_delta > 0.0,
}
baseline_flow = float(
np.mean(
[
row["trajectory_flow_discontinuity"]
for row in baseline["boundary_metrics"]
]
)
)
candidate_flow = float(
np.mean(
[
row["trajectory_flow_discontinuity"]
for row in candidate["boundary_metrics"]
]
)
)
mean_reduction = float(np.mean(reductions)) if reductions else None
sharpness_ok = (
metrics["sharpness_laplacian_variance"]["candidate_delta"]
<= metrics["sharpness_laplacian_variance"]["baseline_delta"]
)
gradient_ok = (
metrics["gradient_mean"]["candidate_delta"]
<= metrics["gradient_mean"]["baseline_delta"]
)
flow_ratio = candidate_flow / max(baseline_flow, 1.0e-9)
numeric_promising = bool(
mean_reduction is not None
and mean_reduction >= 30.0
and sharpness_ok
and gradient_ok
and flow_ratio <= 1.15
)
return {
"metrics": metrics,
"screening_mean_reduction_percent": mean_reduction,
"screening_mean_contract": (
"mean of baseline-positive drift indicators only; visual review remains required"
),
"sharpness_not_worse": sharpness_ok,
"gradient_not_worse": gradient_ok,
"baseline_mean_flow_discontinuity": baseline_flow,
"candidate_mean_flow_discontinuity": candidate_flow,
"flow_ratio": flow_ratio,
"numeric_promising": numeric_promising,
}
def run(args: argparse.Namespace) -> dict:
summary = json.loads((args.output_root / "summary.json").read_text(encoding="utf-8"))
baseline_analysis = json.loads(args.baseline_analysis.read_text(encoding="utf-8"))
baseline_key = args.baseline_case
baseline_record = baseline_analysis["videos"][baseline_key]
baseline_path = Path(baseline_record["path"])
baseline_frames, baseline_fps = base._read_frames(baseline_path)
baseline_center, baseline_representatives = base._chunk_metrics(
baseline_frames, chunks=6, radius=args.radius
)
baseline_end = _end_metrics(baseline_frames, chunks=6, radius=args.end_radius)
cases = {
"baseline_22f_accepted": {
"path": str(baseline_path),
"fps": baseline_fps,
"frame_count": len(baseline_frames),
"center_metrics": baseline_center,
"end_metrics": baseline_end,
"end_trend": base._trend(baseline_end, base.METRIC_KEYS),
"boundary_metrics": base._boundary_metrics(baseline_frames, chunks=6),
"identity_and_frame_difference": _identity_and_frame_difference(
baseline_representatives
),
}
}
sheet_rows = [("A accepted 22f", baseline_representatives)]
end_csv_rows = []
for row in baseline_end:
end_csv_rows.append({"case": "baseline_22f_accepted", **row})
for record in summary["cases"]:
case = str(record["case"])
path = _case_video(record, args.video_root)
frames, fps = base._read_frames(path)
center, representatives = base._chunk_metrics(
frames, chunks=6, radius=args.radius
)
end = _end_metrics(frames, chunks=6, radius=args.end_radius)
cases[case] = {
"path": str(path),
"fps": fps,
"frame_count": len(frames),
"center_metrics": center,
"end_metrics": end,
"end_trend": base._trend(end, base.METRIC_KEYS),
"boundary_metrics": base._boundary_metrics(frames, chunks=6),
"identity_and_frame_difference": _identity_and_frame_difference(
representatives
),
}
sheet_rows.append((case, representatives))
for row in end:
end_csv_rows.append({"case": case, **row})
baseline = cases["baseline_22f_accepted"]
comparisons = {
case: _metric_comparison(baseline, value)
for case, value in cases.items()
if case != "baseline_22f_accepted"
}
csv_path = args.output_root / "group_end_metrics.csv"
with csv_path.open("w", encoding="utf-8", newline="") as handle:
writer = csv.DictWriter(handle, fieldnames=list(end_csv_rows[0]))
writer.writeheader()
writer.writerows(end_csv_rows)
contact_path = args.output_root / "contact_sheet.png"
base._contact_sheet(sheet_rows, contact_path)
result = {
"format": "h3-continuum-issue13-r28-analysis-v1",
"baseline_analysis": str(args.baseline_analysis),
"baseline_case": baseline_key,
"cases": cases,
"comparisons": comparisons,
"group_end_metrics_csv": str(csv_path),
"contact_sheet": str(contact_path),
}
output = args.output_root / "analysis.json"
output.write_text(json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8")
print(json.dumps(result, ensure_ascii=False))
return result
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument("--baseline-analysis", type=Path, required=True)
parser.add_argument(
"--baseline-case", default="screen_6x5_turbo_fixed_prompt"
)
parser.add_argument(
"--video-root", type=Path, default=Path(r"D:\output\video\comfy_video")
)
parser.add_argument("--radius", type=int, default=12)
parser.add_argument("--end-radius", type=int, default=12)
return parser
if __name__ == "__main__":
run(_parser().parse_args())
+258
View File
@@ -0,0 +1,258 @@
"""Run the staged Issue #13 R2.8 continuation depth/strength GPU gate."""
from __future__ import annotations
import argparse
import copy
import hashlib
import json
from pathlib import Path
import sys
import uuid
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
import issue13_r25_gate_runner as r25
from p32_api_runner import submit_and_wait
CASE_SPECS = {
"baseline_22f": {"experiment": "Baseline 22f"},
"depth_13f": {"experiment": "Video Depth 13f"},
"depth_9f": {"experiment": "Video Depth 9f"},
"strength_75": {"experiment": "Video Strength 75%"},
"depth_13f_strength_75": {
"experiment": "Video Depth 13f + Strength 75%"
},
}
DIAGNOSTIC_MARKER = "R2.8 diagnostic: "
def configure_prompt(
source: dict,
*,
case: str,
output_prefix: str,
image_name: str,
seed: int,
width: int,
height: int,
diagnostic_nonce: int,
) -> tuple[dict, list[dict]]:
if case not in CASE_SPECS:
raise ValueError(f"unknown Issue #13 R2.8 case: {case}")
prompt = copy.deepcopy(source)
sampler = prompt["305"]
sampler["class_type"] = "H3ContinuumContinuationR28Diagnostic"
sampler["_meta"] = {"title": f"Issue #13 R2.8 Diagnostic — {case}"}
inputs = sampler["inputs"]
inputs.update(
{
"prompt_mode": "List",
"chunks": 6,
"chunk_seconds": 5.0,
"width": int(width),
"height": int(height),
"continuity": "Balanced — 22 frames",
"base_seed": int(seed),
"audio_continuity": True,
"diagnostics": "Detailed Report",
"reroll_from_chunk": "Auto",
"reroll_nonce": 0,
"strict_compatibility": False,
"debug": False,
"show_preview": False,
"run_storage": "Off",
"run_name": "",
"project_id": "",
"continuation_transport": "masked_av_prefix_22_v1",
"continuation_experiment": CASE_SPECS[case]["experiment"],
"diagnostic_nonce": int(diagnostic_nonce),
"first_frame": ["119", 0],
}
)
for key in (
"continuation_backend",
"continuation_source_mode",
"synchronize_sampling",
"generation_mode",
"review_action",
"max_new_physical_groups",
"aspect",
"preset",
"custom_mp",
"guide",
"last_frame",
):
inputs.pop(key, None)
prompt["188"]["inputs"]["value"] = r25._prompt_list(
6, motion_trigger=False, fixed_prompt=True
)
prompt["114"]["inputs"]["image"] = str(image_name)
prompt["139"]["inputs"]["sampler_name"] = "euler"
prompt["153"]["inputs"].update(
{"scheduler": "simple", "steps": 8, "denoise": 1.0}
)
prompt["297"]["inputs"]["value"] = max(
0.05, (float(width) * float(height)) / 1_000_000.0
)
prompt["306"]["inputs"].update(
{
"exact_total_duration": True,
"audio_seam": "Off",
"video_seam": "Off",
"buffer_backend": "Auto",
"diagnostics": "Detailed Report",
}
)
prompt["191"]["inputs"]["filename_prefix"] = str(output_prefix)
prompt["249"]["inputs"]["source"] = ["305", 3]
spectrum = prompt.get("150", {}).get("inputs", {})
if spectrum:
spectrum["enabled"] = False
selected = r25._configure_loras(prompt["286"]["inputs"], "turbo")
return prompt, selected
def _diagnostic_from_status(status: str) -> dict:
if DIAGNOSTIC_MARKER not in status:
raise RuntimeError("Issue #13 R2.8 diagnostic marker is missing")
value = json.loads(status.rsplit(DIAGNOSTIC_MARKER, 1)[1].strip())
if not isinstance(value, dict):
raise RuntimeError("Issue #13 R2.8 diagnostic payload is not an object")
return value
def run(args: argparse.Namespace) -> dict:
source = json.loads(args.prompt.read_text(encoding="utf-8"))
cases = list(args.cases or ("depth_13f",))
image_sha256 = hashlib.sha256(args.image_source.read_bytes()).hexdigest()
args.output_root.mkdir(parents=True, exist_ok=True)
summary_path = args.output_root / "summary.json"
results = []
if summary_path.exists():
results = list(
json.loads(summary_path.read_text(encoding="utf-8")).get("cases") or []
)
completed = {str(item.get("case")) for item in results}
for offset, case in enumerate(cases):
if case in completed:
print(f"ISSUE13-R28 SKIP completed case={case}", flush=True)
continue
configured, selected = configure_prompt(
source,
case=case,
output_prefix=f"video/issue13_r28/Issue13_R28_{case}_seed{args.seed}",
image_name=args.image_name,
seed=args.seed,
width=args.width,
height=args.height,
diagnostic_nonce=args.diagnostic_nonce + offset,
)
inventory = r25._inventory_loras(args.lora_root, selected)
case_root = args.output_root / case
case_root.mkdir(parents=True, exist_ok=True)
prompt_path = case_root / "prompt.json"
prompt_path.write_text(
json.dumps(configured, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(f"ISSUE13-R28 START case={case}", flush=True)
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=configured,
client_id=f"issue13-r28-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=3.0,
)
history_path = case_root / "history.json"
history_path.write_text(
json.dumps(history, indent=2, ensure_ascii=False), encoding="utf-8"
)
status = r25._status_text(history, prompt_id)
result = {
"case": case,
"chunks": 6,
"chunk_seconds": 5.0,
"profile": "turbo",
"steps": 8,
"sampler": "euler",
"scheduler": "simple",
"prompt_id": prompt_id,
"api_elapsed_seconds": float(elapsed),
"prompt": str(prompt_path),
"history": str(history_path),
"output_video": r25._output_video(history, prompt_id),
"status_sha256": hashlib.sha256(status.encode("utf-8")).hexdigest(),
"lora_inventory": inventory,
"diagnostic": _diagnostic_from_status(status),
}
(case_root / "summary.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
)
results.append(result)
summary = {
"format": "h3-continuum-issue13-r28-gate-v1",
"server": args.server,
"source_prompt": str(args.prompt),
"baseline_summary": str(args.baseline_summary)
if args.baseline_summary
else None,
"input_image": {
"source": str(args.image_source),
"runtime_name": args.image_name,
"sha256": image_sha256,
},
"seed": int(args.seed),
"width": int(args.width),
"height": int(args.height),
"sampler": "euler",
"scheduler": "simple",
"continuity_transport": "Balanced 22f / masked_av_prefix_22_v1",
"audio_continuity": True,
"cases": results,
}
summary_path.write_text(
json.dumps(summary, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(
f"ISSUE13-R28 COMPLETE case={case} prompt_id={prompt_id} elapsed={elapsed:.3f}s",
flush=True,
)
return json.loads(summary_path.read_text(encoding="utf-8"))
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--server", default="http://127.0.0.6:8190")
parser.add_argument("--prompt", type=Path, required=True)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument("--image-source", type=Path, required=True)
parser.add_argument("--image-name", required=True)
parser.add_argument("--baseline-summary", type=Path)
parser.add_argument(
"--lora-root",
type=Path,
default=Path(r"D:\StabilityMatrix\Data\Packages\ComfyUI_W\models\loras"),
)
parser.add_argument("--seed", type=int, default=131325)
parser.add_argument("--width", type=int, default=576)
parser.add_argument("--height", type=int, default=864)
parser.add_argument("--diagnostic-nonce", type=int, default=132800)
parser.add_argument("--timeout-seconds", type=float, default=7200.0)
parser.add_argument("--cases", nargs="*", choices=tuple(CASE_SPECS))
return parser
def main() -> int:
print(json.dumps(run(_parser().parse_args()), ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+462
View File
@@ -0,0 +1,462 @@
"""Analyze Issue #13 R2 decoded video and continuation-latent trends."""
from __future__ import annotations
import argparse
import csv
import hashlib
import json
from pathlib import Path
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
METRIC_KEYS = (
"luma_mean",
"luma_std",
"saturation_mean",
"saturation_p95",
"contrast_p95_p05",
"sharpness_laplacian_variance",
"gradient_mean",
"highlight_clip_fraction",
"skin_highlight_proxy_fraction",
"shadow_clip_fraction",
)
LATENT_KEYS = (
"mean",
"std",
"rms",
"minimum",
"maximum",
"temporal_delta_rms",
)
def _video_path(record: dict, video_root: Path) -> Path:
item = record.get("output_video") or {}
return video_root / str(item.get("subfolder", "")) / str(item.get("filename", ""))
def _read_frames(path: Path) -> tuple[list[np.ndarray], float]:
capture = cv2.VideoCapture(str(path))
if not capture.isOpened():
raise RuntimeError(f"cannot open video: {path}")
fps = float(capture.get(cv2.CAP_PROP_FPS) or 24.0)
frames = []
while True:
ok, frame = capture.read()
if not ok:
break
frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
capture.release()
if not frames:
raise RuntimeError(f"video contains no frames: {path}")
return frames, fps
def _frame_metrics(frame: np.ndarray) -> dict[str, float]:
rgb = frame.astype(np.float32) / 255.0
y = 0.2126 * rgb[..., 0] + 0.7152 * rgb[..., 1] + 0.0722 * rgb[..., 2]
hsv = cv2.cvtColor(frame, cv2.COLOR_RGB2HSV).astype(np.float32)
saturation = hsv[..., 1] / 255.0
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
laplacian = cv2.Laplacian(gray, cv2.CV_64F)
sobel_x = cv2.Sobel(gray, cv2.CV_32F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(gray, cv2.CV_32F, 0, 1, ksize=3)
gradient = cv2.magnitude(sobel_x, sobel_y) / 255.0
ycrcb = cv2.cvtColor(frame, cv2.COLOR_RGB2YCrCb)
skin_mask = (
(ycrcb[..., 1] >= 133)
& (ycrcb[..., 1] <= 173)
& (ycrcb[..., 2] >= 77)
& (ycrcb[..., 2] <= 127)
)
skin_count = int(np.count_nonzero(skin_mask))
skin_highlight = (
float(np.count_nonzero(skin_mask & (y >= (220.0 / 255.0))))
/ float(skin_count)
if skin_count
else 0.0
)
return {
"luma_mean": float(np.mean(y)),
"luma_std": float(np.std(y)),
"red_mean": float(np.mean(rgb[..., 0])),
"green_mean": float(np.mean(rgb[..., 1])),
"blue_mean": float(np.mean(rgb[..., 2])),
"saturation_mean": float(np.mean(saturation)),
"saturation_p95": float(np.percentile(saturation, 95)),
"contrast_p95_p05": float(np.percentile(y, 95) - np.percentile(y, 5)),
"sharpness_laplacian_variance": float(np.var(laplacian)),
"gradient_mean": float(np.mean(gradient)),
"highlight_clip_fraction": float(np.mean(y >= (250.0 / 255.0))),
"skin_highlight_proxy_fraction": skin_highlight,
"shadow_clip_fraction": float(np.mean(y <= (5.0 / 255.0))),
}
def _chunk_metrics(
frames: list[np.ndarray], *, chunks: int, radius: int
) -> tuple[list[dict], list[np.ndarray]]:
frame_count = len(frames)
rows = []
representatives = []
for index in range(chunks):
center = int(round((index + 0.5) * frame_count / chunks))
center = min(max(center, 0), frame_count - 1)
start = max(0, center - radius)
end = min(frame_count, center + radius + 1)
values = [_frame_metrics(frame) for frame in frames[start:end]]
merged = {
key: float(np.mean([item[key] for item in values])) for key in values[0]
}
merged.update(
{
"chunk": index + 1,
"center_frame": center,
"sample_start": start,
"sample_end_exclusive": end,
"sample_count": end - start,
}
)
rows.append(merged)
representatives.append(frames[center])
return rows, representatives
def _normalized_histogram(values: np.ndarray, *, bins: int = 16) -> list[float]:
counts, _ = np.histogram(values, bins=bins, range=(0.0, 1.0))
total = int(counts.sum())
if total <= 0:
return [0.0] * int(bins)
return [float(value) / float(total) for value in counts]
def _continuation_input_rgb_proxy(
frames: list[np.ndarray], *, chunks: int, context_frames: int
) -> dict:
"""Measure decoded source frames immediately before each chunk boundary.
The R2 diagnostic separately proves that each source latent tail is copied
bit-exactly into the following target prefix. This read-only decoded window
is therefore a visual proxy; it does not add a VAE decode to Sampling.
"""
boundaries = []
frame_count = len(frames)
for target_index in range(1, int(chunks)):
boundary = int(round(target_index * frame_count / int(chunks)))
boundary = min(max(boundary, 1), frame_count)
start = max(0, boundary - int(context_frames))
window = frames[start:boundary]
values = [_frame_metrics(frame) for frame in window]
merged = {
key: float(np.mean([item[key] for item in values])) for key in values[0]
}
rgb = np.concatenate(
[frame.astype(np.float32).reshape(-1, 3) / 255.0 for frame in window],
axis=0,
)
luma = 0.2126 * rgb[:, 0] + 0.7152 * rgb[:, 1] + 0.0722 * rgb[:, 2]
saturation = np.concatenate(
[
cv2.cvtColor(frame, cv2.COLOR_RGB2HSV)[..., 1]
.reshape(-1)
.astype(np.float32)
/ 255.0
for frame in window
]
)
merged.update(
{
"source_group": target_index,
"target_group": target_index + 1,
"boundary_frame": boundary,
"window_start": start,
"window_end_exclusive": boundary,
"window_frames": len(window),
"histogram_bins": 16,
"red_histogram": _normalized_histogram(rgb[:, 0]),
"green_histogram": _normalized_histogram(rgb[:, 1]),
"blue_histogram": _normalized_histogram(rgb[:, 2]),
"luma_histogram": _normalized_histogram(luma),
"saturation_histogram": _normalized_histogram(saturation),
}
)
boundaries.append(merged)
return {
"contract": "decoded source-tail proxy; no extra VAE decode during Sampling",
"context_frames": int(context_frames),
"boundaries": boundaries,
"trend": _trend(boundaries, METRIC_KEYS) if boundaries else {},
}
def _trend(rows: list[dict], keys: tuple[str, ...]) -> dict:
x = np.arange(1, len(rows) + 1, dtype=np.float64)
result = {}
for key in keys:
values = np.asarray([float(row[key]) for row in rows], dtype=np.float64)
result[key] = {
"first": float(values[0]),
"last": float(values[-1]),
"last_minus_first": float(values[-1] - values[0]),
"linear_slope_per_chunk": float(np.polyfit(x, values, 1)[0])
if len(values) > 1
else 0.0,
"increase_steps": int(np.count_nonzero(np.diff(values) > 0.0)),
"decrease_steps": int(np.count_nonzero(np.diff(values) < 0.0)),
}
return result
def _flow_mean(first: np.ndarray, second: np.ndarray) -> float:
first_gray = cv2.cvtColor(first, cv2.COLOR_RGB2GRAY)
second_gray = cv2.cvtColor(second, cv2.COLOR_RGB2GRAY)
flow = cv2.calcOpticalFlowFarneback(
first_gray,
second_gray,
None,
0.5,
3,
15,
3,
5,
1.2,
0,
)
return float(np.linalg.norm(flow, axis=-1).mean())
def _boundary_metrics(frames: list[np.ndarray], *, chunks: int) -> list[dict]:
rows = []
frame_count = len(frames)
for target_index in range(1, int(chunks)):
boundary = int(round(target_index * frame_count / int(chunks)))
boundary = min(max(boundary, 2), frame_count - 2)
before2, before, after, after2 = frames[
boundary - 2
], frames[boundary - 1], frames[boundary], frames[boundary + 1]
before_float = before.astype(np.float32) / 255.0
after_float = after.astype(np.float32) / 255.0
before_metrics = _frame_metrics(before)
after_metrics = _frame_metrics(after)
flow_before = _flow_mean(before2, before)
flow_boundary = _flow_mean(before, after)
flow_after = _flow_mean(after, after2)
local_flow = 0.5 * (flow_before + flow_after)
rows.append(
{
"source_group": target_index,
"target_group": target_index + 1,
"boundary_frame": boundary,
"frame_mae": float(np.abs(after_float - before_float).mean()),
"luma_jump_abs": abs(
after_metrics["luma_mean"] - before_metrics["luma_mean"]
),
"saturation_jump_abs": abs(
after_metrics["saturation_mean"]
- before_metrics["saturation_mean"]
),
"gradient_jump_abs": abs(
after_metrics["gradient_mean"]
- before_metrics["gradient_mean"]
),
"flow_before": flow_before,
"flow_boundary": flow_boundary,
"flow_after": flow_after,
"trajectory_flow_discontinuity": abs(flow_boundary - local_flow)
/ max(local_flow, 1.0e-6),
}
)
return rows
def _latent_trend(diagnostic: dict) -> dict:
calls = list(diagnostic.get("sample_calls") or [])
def rows_for(name: str) -> list[dict]:
rows = []
for call in calls:
value = call.get(name)
if not isinstance(value, dict):
continue
row = {"sample_number": int(call.get("sample_number", len(rows) + 1))}
for key in LATENT_KEYS:
if value.get(key) is not None:
row[key] = float(value[key])
rows.append(row)
return rows
streams = {}
for name in ("input_video", "output_video", "output_tail", "source_tail"):
rows = rows_for(name)
available = tuple(key for key in LATENT_KEYS if rows and key in rows[0])
streams[name] = {
"rows": rows,
"trend": _trend(rows, available) if rows and available else {},
}
prefix_pairs = list(diagnostic.get("final_prefix_pairs") or [])
return {
"streams": streams,
"prefix_pair_count": len(prefix_pairs),
"all_video_prefix_pairs_bit_exact": bool(prefix_pairs)
and all(bool(pair.get("bit_exact")) for pair in prefix_pairs),
"all_audio_prefix_pairs_bit_exact": bool(prefix_pairs)
and all(bool(pair.get("audio_bit_exact")) for pair in prefix_pairs),
"maximum_video_prefix_abs_diff": max(
(float(pair.get("max_abs_diff", 0.0)) for pair in prefix_pairs),
default=0.0,
),
"maximum_audio_prefix_abs_diff": max(
(float(pair.get("audio_max_abs_diff", 0.0)) for pair in prefix_pairs),
default=0.0,
),
}
def _contact_sheet(
rows: list[tuple[str, list[np.ndarray]]],
output: Path,
*,
column_labels: list[str] | None = None,
) -> None:
thumb_w, thumb_h = 224, 224
label_w, top_h = 240, 42
columns = max((len(frames) for _, frames in rows), default=0)
width = label_w + thumb_w * columns
height = top_h + thumb_h * len(rows)
sheet = Image.new("RGB", (width, height), (9, 15, 25))
draw = ImageDraw.Draw(sheet)
font = ImageFont.load_default()
for column in range(columns):
draw.text(
(label_w + column * thumb_w + 8, 14),
column_labels[column]
if column_labels is not None and column < len(column_labels)
else f"Chunk {column + 1}",
fill=(210, 225, 245),
font=font,
)
for row_index, (label, frames) in enumerate(rows):
y = top_h + row_index * thumb_h
draw.text((10, y + 16), label, fill=(115, 210, 255), font=font)
for column, frame in enumerate(frames):
image = Image.fromarray(frame).resize(
(thumb_w, thumb_h), Image.Resampling.LANCZOS
)
sheet.paste(image, (label_w + column * thumb_w, y))
output.parent.mkdir(parents=True, exist_ok=True)
sheet.save(output)
def _milestone_frames(
frames: list[np.ndarray], *, chunks: int
) -> tuple[list[str], list[np.ndarray]]:
frame_count = len(frames)
chunk_frames = frame_count / float(chunks)
indices = [
min(frame_count - 1, 12),
min(frame_count - 1, max(0, int(round(3 * chunk_frames)) - 1)),
min(frame_count - 1, int(round(3 * chunk_frames)) + 12),
min(frame_count - 1, int(round(4.5 * chunk_frames))),
frame_count - 1,
]
return (
["Near 0s", "G3 end", "G4 post", "G5", "G6 end"],
[frames[index] for index in indices],
)
def run(args: argparse.Namespace) -> dict:
summary = json.loads((args.output_root / "summary.json").read_text(encoding="utf-8"))
all_rows = []
sheets = []
milestone_sheets = []
milestone_labels = None
videos = {}
for record in summary["cases"]:
case = str(record["case"])
chunks = int(record["chunks"])
path = _video_path(record, args.video_root)
frames, fps = _read_frames(path)
metrics, representatives = _chunk_metrics(
frames, chunks=chunks, radius=args.radius
)
for row in metrics:
row["case"] = case
row["video"] = str(path)
row["fps"] = fps
row["frame_count"] = len(frames)
all_rows.append(row)
sheets.append((case, representatives))
labels, milestones = _milestone_frames(frames, chunks=chunks)
milestone_labels = labels
milestone_sheets.append((case, milestones))
videos[case] = {
"path": str(path),
"sha256": hashlib.sha256(path.read_bytes()).hexdigest(),
"fps": fps,
"frame_count": len(frames),
"duration_seconds": len(frames) / fps,
"chunks": chunks,
"chunk_seconds": float(record["chunk_seconds"]),
"metrics": metrics,
"decoded_trend": _trend(metrics, METRIC_KEYS),
"continuation_input_rgb_proxy": _continuation_input_rgb_proxy(
frames,
chunks=chunks,
context_frames=int(
(record.get("diagnostic") or {}).get("context_frames", 22)
),
),
"boundary_metrics": _boundary_metrics(frames, chunks=chunks),
"latent_trend": _latent_trend(record.get("diagnostic") or {}),
}
if not all_rows:
raise RuntimeError("Issue #13 R2 summary contains no completed cases")
csv_path = args.output_root / "chunk_metrics.csv"
with csv_path.open("w", encoding="utf-8", newline="") as handle:
writer = csv.DictWriter(handle, fieldnames=list(all_rows[0]))
writer.writeheader()
writer.writerows(all_rows)
contact_path = args.output_root / "contact_sheet.png"
_contact_sheet(sheets, contact_path)
milestone_contact_path = args.output_root / "milestone_contact_sheet.png"
_contact_sheet(
milestone_sheets,
milestone_contact_path,
column_labels=milestone_labels,
)
result = {
"format": "h3-continuum-issue13-r2-analysis-v2",
"sample_radius_frames": args.radius,
"videos": videos,
"csv": str(csv_path),
"contact_sheet": str(contact_path),
"milestone_contact_sheet": str(milestone_contact_path),
}
(args.output_root / "analysis.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(json.dumps(result, ensure_ascii=False))
return result
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument(
"--video-root", type=Path, default=Path(r"D:\output\video\comfy_video")
)
parser.add_argument("--radius", type=int, default=12)
return parser
if __name__ == "__main__":
run(_parser().parse_args())
+282
View File
@@ -0,0 +1,282 @@
"""Run the Issue #13 R2 long-duration continuation diagnostic.
This is evidence tooling only. It selects the already accepted Masked AV
transport explicitly through the external GPU diagnostic node and does not
modify the Production sampler, conditioning, or assembly path.
"""
from __future__ import annotations
import argparse
import copy
import hashlib
import json
from pathlib import Path
import sys
import uuid
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
from p32_api_runner import submit_and_wait
BASE_PROMPT = (
"A continuous cinematic live-action tracking shot of one woman in an "
"orange jacket riding a skateboard steadily forward along a waterfront "
"promenade. The camera follows her in one smooth direction. Preserve her "
"face, clothing, skateboard, warm sunlight, detailed texture, rich color, "
"and continuous forward motion. Natural skateboard wheels, wind, and "
"distant city ambience are audible."
)
CASE_SPECS = {
"g1_standard_6x10": {"chunks": 6, "chunk_seconds": 10.0},
"g2_standard_5x5": {"chunks": 5, "chunk_seconds": 5.0},
}
DIAGNOSTIC_MARKER = "V3.6-R1 diagnostic: "
def _same_prompt_list(chunks: int) -> str:
return "\n---\n".join([BASE_PROMPT] * int(chunks))
def configure_prompt(
source: dict,
*,
case: str,
output_prefix: str,
seed: int,
steps: int,
size: int,
diagnostic_nonce: int,
) -> dict:
if case not in CASE_SPECS:
raise ValueError(f"unknown Issue #13 R2 case: {case}")
spec = CASE_SPECS[case]
chunks = int(spec["chunks"])
chunk_seconds = float(spec["chunk_seconds"])
prompt = copy.deepcopy(source)
sampler = prompt["305"]
sampler["class_type"] = "H3ContinuumMaskedPrefixR1Diagnostic"
sampler["_meta"] = {"title": f"Issue #13 R2 Diagnostic — {case}"}
inputs = sampler["inputs"]
inputs.update(
{
"prompt_mode": "List",
"chunks": chunks,
"chunk_seconds": chunk_seconds,
"width": int(size),
"height": int(size),
"continuity": "Balanced — 22 frames",
"base_seed": int(seed),
"audio_continuity": True,
"diagnostics": "Detailed Report",
"reroll_from_chunk": "Auto",
"reroll_nonce": 0,
"strict_compatibility": False,
"debug": False,
"show_preview": False,
"run_storage": "Off",
"run_name": "",
"project_id": "",
"continuation_transport": "masked_av_prefix_22_v1",
"synchronize_sampling": True,
"diagnostic_nonce": int(diagnostic_nonce),
"first_frame": ["119", 0],
}
)
for key in (
"continuation_backend",
"generation_mode",
"review_action",
"max_new_physical_groups",
"aspect",
"preset",
"custom_mp",
"guide",
"last_frame",
):
inputs.pop(key, None)
prompt["188"]["inputs"]["value"] = _same_prompt_list(chunks)
prompt["153"]["inputs"].update(
{"scheduler": "simple", "steps": int(steps), "denoise": 1.0}
)
prompt["297"]["inputs"]["value"] = max(
0.05, (float(size) * float(size)) / 1_000_000.0
)
prompt["306"]["inputs"].update(
{
"exact_total_duration": True,
"audio_seam": "Off",
"video_seam": "Off",
"buffer_backend": "Auto",
"diagnostics": "Detailed Report",
}
)
prompt["191"]["inputs"]["filename_prefix"] = str(output_prefix)
prompt["249"]["inputs"]["source"] = ["305", 3]
spectrum = prompt.get("150", {}).get("inputs", {})
if spectrum:
spectrum["enabled"] = False
lora_loader = prompt.get("286", {}).get("inputs", {})
for name, value in lora_loader.items():
if name.startswith("lora_") and isinstance(value, dict):
value["on"] = False
return prompt
def _output_video(history: dict, prompt_id: str) -> dict | None:
entry = history.get(str(prompt_id), {})
outputs = entry.get("outputs", {}) if isinstance(entry, dict) else {}
save = outputs.get("191", {}) if isinstance(outputs, dict) else {}
images = save.get("images", []) if isinstance(save, dict) else []
return images[0] if images else None
def _status_text(history: dict, prompt_id: str) -> str:
entry = history.get(str(prompt_id), {})
outputs = entry.get("outputs", {}) if isinstance(entry, dict) else {}
preview = outputs.get("249", {}) if isinstance(outputs, dict) else {}
values = preview.get("text", []) if isinstance(preview, dict) else []
return str(values[0]) if values else ""
def _diagnostic_from_status(status: str) -> dict:
if DIAGNOSTIC_MARKER not in status:
raise RuntimeError("Issue #13 R2 diagnostic marker is missing from status")
payload = status.rsplit(DIAGNOSTIC_MARKER, 1)[1].strip()
value = json.loads(payload)
if not isinstance(value, dict):
raise RuntimeError("Issue #13 R2 diagnostic payload is not an object")
return value
def run(args: argparse.Namespace) -> dict:
source = json.loads(args.prompt.read_text(encoding="utf-8"))
cases = list(args.cases or CASE_SPECS)
unknown = [case for case in cases if case not in CASE_SPECS]
if unknown:
raise SystemExit(f"unknown --cases: {', '.join(unknown)}")
args.output_root.mkdir(parents=True, exist_ok=True)
summary_path = args.output_root / "summary.json"
results = []
if summary_path.exists():
existing = json.loads(summary_path.read_text(encoding="utf-8"))
results = list(existing.get("cases") or [])
completed_cases = {str(item.get("case")) for item in results}
for offset, case in enumerate(cases):
if case in completed_cases:
print(f"ISSUE13-R2 SKIP completed case={case}", flush=True)
continue
spec = CASE_SPECS[case]
configured = configure_prompt(
source,
case=case,
output_prefix=f"video/issue13_r2/Issue13_R2_{case}_seed{args.seed}",
seed=args.seed,
steps=args.steps,
size=args.size,
diagnostic_nonce=args.diagnostic_nonce + offset,
)
case_root = args.output_root / case
case_root.mkdir(parents=True, exist_ok=True)
prompt_path = case_root / "prompt.json"
prompt_path.write_text(
json.dumps(configured, indent=2, ensure_ascii=False),
encoding="utf-8",
)
print(f"ISSUE13-R2 START case={case}", flush=True)
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=configured,
client_id=f"issue13-r2-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=3.0,
)
history_path = case_root / "history.json"
history_path.write_text(
json.dumps(history, indent=2, ensure_ascii=False),
encoding="utf-8",
)
status = _status_text(history, prompt_id)
diagnostic = _diagnostic_from_status(status)
result = {
"case": case,
"chunks": int(spec["chunks"]),
"chunk_seconds": float(spec["chunk_seconds"]),
"prompt_id": prompt_id,
"api_elapsed_seconds": elapsed,
"prompt": str(prompt_path),
"history": str(history_path),
"output_video": _output_video(history, prompt_id),
"status_sha256": hashlib.sha256(status.encode("utf-8")).hexdigest(),
"diagnostic": diagnostic,
}
(case_root / "summary.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False),
encoding="utf-8",
)
results.append(result)
print(
f"ISSUE13-R2 COMPLETE case={case} prompt_id={prompt_id} "
f"elapsed={elapsed:.3f}s",
flush=True,
)
summary = {
"format": "h3-continuum-issue13-r2-gate-v1",
"server": args.server,
"source_prompt": str(args.prompt),
"seed": int(args.seed),
"steps": int(args.steps),
"size": int(args.size),
"sampler": "res_multistep",
"scheduler": "simple",
"continuity": "Balanced — 22 frames",
"audio_continuity": True,
"transport": "masked_av_prefix_22_v1",
"spectrum": False,
"lora": False,
"video_seam": "Off",
"audio_seam": "Off",
"cases": results,
}
summary_path.write_text(
json.dumps(summary, indent=2, ensure_ascii=False),
encoding="utf-8",
)
return json.loads(summary_path.read_text(encoding="utf-8"))
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--server", default="http://127.0.0.6:8190")
parser.add_argument("--prompt", type=Path, required=True)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument("--seed", type=int, default=130013)
parser.add_argument("--steps", type=int, default=8)
parser.add_argument("--size", type=int, default=384)
parser.add_argument("--diagnostic-nonce", type=int, default=131300)
parser.add_argument("--timeout-seconds", type=float, default=7200.0)
parser.add_argument("--cases", nargs="*", choices=tuple(CASE_SPECS))
return parser
def main() -> int:
summary = run(_parser().parse_args())
print(json.dumps(summary, ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())
File diff suppressed because it is too large Load Diff
+676
View File
@@ -0,0 +1,676 @@
"""Phase 2A P2-4 probe against the installed ComfyUI execution/cache stack.
The probe uses deterministic decoded CPU IMAGE/AUDIO fixtures. It does not
load a model, run a VAE, or use CUDA. Its purpose is to exercise the real
ComfyUI cache, Core Preview/Save nodes, installed VHS Video Combine, Core ESC
interrupt propagation, and restart-style stale cleanup around the V3.5
Disk-backed assembler.
"""
from __future__ import annotations
import argparse
import gc
import importlib
import importlib.util
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
import time
from types import SimpleNamespace
from typing import Any
import torch
class ProbeFailure(RuntimeError):
pass
class _Server:
def __init__(self):
self.client_id = None
self.last_node_id = None
self.messages: list[tuple[str, dict[str, Any], Any]] = []
def send_sync(self, event, data, client_id):
self.messages.append((event, data, client_id))
def _load_package(package_root: Path):
name = "h3_continuum_p24_probe"
spec = importlib.util.spec_from_file_location(
name,
package_root / "__init__.py",
submodule_search_locations=[str(package_root)],
)
if spec is None or spec.loader is None:
raise ProbeFailure("cannot create Continuum package import specification")
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
return module
def _install_paths(comfy_root: Path) -> None:
for value in (
comfy_root,
comfy_root / "custom_nodes" / "comfyui-videohelpersuite",
):
text = str(value)
if text not in sys.path:
sys.path.insert(0, text)
def _single_plan(package_name: str) -> dict[str, Any]:
hardening = importlib.import_module(f"{package_name}.hardening")
temporal = importlib.import_module(f"{package_name}.temporal")
plan_module = importlib.import_module(f"{package_name}.v3.plan")
total_frames = 124
chunk = {
"sequence_index": 1,
"chunk_index": 1,
"total_frames": total_frames,
"trim_frames": 0,
"net_frames": total_frames,
"context_frames": 0,
"expected_video_latent_t": temporal.video_latent_t(total_frames),
"expected_audio_latent_t": temporal.audio_latent_t(total_frames),
}
return hardening.enrich_assembly_plan(
{
"magic": plan_module.ASSEMBLY_PLAN_MAGIC,
"schema_version": 1,
"fps": 24,
"width": 16,
"height": 16,
"chunk_seconds": 5.0,
"target_frames": 120,
"preserve_final_frame": True,
"chunks": [chunk],
}
)
def _register_probe_nodes(core_nodes, package_module, video_combine):
package_name = package_module.__name__
class P24DecodedFixture:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"nonce": ("INT", {"default": 0}),
"interrupt_before_assembly": (
"BOOLEAN",
{"default": False},
),
}
}
RETURN_TYPES = (
"IMAGE",
"AUDIO",
"H3_CONTINUUM_ASSEMBLY_PLAN",
)
RETURN_NAMES = ("images", "audio", "assembly_plan")
FUNCTION = "build"
CATEGORY = "P2-4 Probe"
def build(self, nonce, interrupt_before_assembly):
total_frames = 124
values = (
torch.arange(total_frames, dtype=torch.float32)
+ float(int(nonce) % 7)
) / float(total_frames + 7)
images = values.reshape(-1, 1, 1, 1).expand(
-1,
16,
16,
3,
)
sample_rate = 32_000
samples = int(round(total_frames / 24 * sample_rate))
audio = {
"waveform": torch.linspace(
-0.05,
0.05,
samples,
dtype=torch.float32,
).reshape(1, 1, -1),
"sample_rate": sample_rate,
}
plan = _single_plan(package_name)
if bool(interrupt_before_assembly):
core_nodes.interrupt_processing(True)
return images, audio, plan
class P24FailOrPass:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"images": ("IMAGE",),
"fail": ("BOOLEAN", {"default": True}),
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "consume"
OUTPUT_NODE = True
CATEGORY = "P2-4 Probe"
def consume(self, images, fail):
if bool(fail):
raise RuntimeError("P2-4 intentional downstream failure")
return {
"ui": {"p24": ["downstream pass"]},
"result": (images,),
}
class P24ClearCacheOutput:
@classmethod
def INPUT_TYPES(cls):
return {"required": {"token": ("INT", {"default": 0})}}
RETURN_TYPES = ("STRING",)
FUNCTION = "finish"
OUTPUT_NODE = True
CATEGORY = "P2-4 Probe"
def finish(self, token):
return (f"clear-{int(token)}",)
core_nodes.NODE_CLASS_MAPPINGS.update(package_module.NODE_CLASS_MAPPINGS)
core_nodes.NODE_CLASS_MAPPINGS.update(
{
"P24DecodedFixture": P24DecodedFixture,
"P24FailOrPass": P24FailOrPass,
"P24ClearCacheOutput": P24ClearCacheOutput,
"P24VHSVideoCombine": video_combine,
}
)
def _base_prompt(*, nonce: int, interrupt_before_assembly: bool) -> dict[str, Any]:
return {
"1": {
"class_type": "P24DecodedFixture",
"inputs": {
"nonce": int(nonce),
"interrupt_before_assembly": bool(interrupt_before_assembly),
},
},
"2": {
"class_type": "H3ContinuumAssembleSeamV35",
"inputs": {
"images": ["1", 0],
"audio": ["1", 1],
"assembly_plan": ["1", 2],
"exact_total_duration": True,
"audio_seam": "Off",
"video_seam": "Off",
"buffer_backend": "Disk-backed",
"diagnostics": "Basic",
},
},
}
def _failure_prompt(*, fail: bool, nonce: int = 0) -> dict[str, Any]:
prompt = _base_prompt(nonce=nonce, interrupt_before_assembly=False)
prompt["3"] = {
"class_type": "P24FailOrPass",
"inputs": {"images": ["2", 0], "fail": bool(fail)},
}
return prompt
def _compatibility_prompt() -> dict[str, Any]:
prompt = _base_prompt(nonce=0, interrupt_before_assembly=False)
prompt.update(
{
"4": {
"class_type": "SaveImage",
"inputs": {
"images": ["2", 0],
"filename_prefix": "P24/Save",
},
},
"5": {
"class_type": "PreviewImage",
"inputs": {"images": ["2", 0]},
},
"6": {
"class_type": "P24VHSVideoCombine",
"inputs": {
"images": ["2", 0],
"audio": ["2", 1],
"frame_rate": 24,
"loop_count": 0,
"filename_prefix": "P24/VHS",
"format": "video/h264-mp4",
"pingpong": False,
"save_output": True,
},
},
}
)
return prompt
def _interrupt_prompt() -> dict[str, Any]:
prompt = _base_prompt(nonce=99, interrupt_before_assembly=True)
prompt["3"] = {
"class_type": "P24FailOrPass",
"inputs": {"images": ["2", 0], "fail": False},
}
return prompt
def _clear_prompt(token: int) -> dict[str, Any]:
return {
"9": {
"class_type": "P24ClearCacheOutput",
"inputs": {"token": int(token)},
}
}
def _cached_nodes(executor) -> set[str]:
for event, data in executor.status_messages:
if event == "execution_cached":
return {str(value) for value in data.get("nodes", [])}
return set()
def _has_event(executor, wanted: str) -> bool:
return any(event == wanted for event, _ in executor.status_messages)
def _execute(executor, prompt, prompt_id: str, outputs: list[str]) -> None:
executor.execute(
prompt,
prompt_id,
extra_data={},
execute_outputs=outputs,
)
def _managed_files(backing_root: Path, suffix: str) -> list[Path]:
return sorted(backing_root.glob(f"h3c-p20-*.{suffix}"))
def _all_output_files(root: Path) -> set[str]:
if not root.exists():
return set()
return {
str(path.relative_to(root))
for path in root.rglob("*")
if path.is_file()
}
def _assert_output_paths(output_root: Path, temp_root: Path, history: dict) -> dict:
outputs = history.get("outputs", {})
save_items = outputs.get("4", {}).get("images", [])
preview_items = outputs.get("5", {}).get("images", [])
gifs = outputs.get("6", {}).get("gifs", [])
if len(save_items) != 120:
raise ProbeFailure(f"Core SaveImage wrote {len(save_items)} frames, expected 120")
if len(preview_items) != 120:
raise ProbeFailure(
f"Core PreviewImage wrote {len(preview_items)} frames, expected 120"
)
if len(gifs) != 1:
raise ProbeFailure(f"VHS returned {len(gifs)} previews, expected 1")
for item in save_items:
path = output_root / item.get("subfolder", "") / item["filename"]
if not path.is_file() or path.stat().st_size <= 0:
raise ProbeFailure(f"Core SaveImage output missing: {path}")
for item in preview_items:
path = temp_root / item.get("subfolder", "") / item["filename"]
if not path.is_file() or path.stat().st_size <= 0:
raise ProbeFailure(f"Core PreviewImage output missing: {path}")
video_path = Path(gifs[0]["fullpath"])
if not video_path.is_file() or video_path.stat().st_size <= 0:
raise ProbeFailure(f"VHS output missing: {video_path}")
ffprobe = shutil.which("ffprobe")
if ffprobe is None:
raise ProbeFailure("ffprobe is required for P2-4 VHS acceptance")
inspected = subprocess.run(
[
ffprobe,
"-v",
"error",
"-count_frames",
"-show_entries",
(
"stream=codec_type,codec_name,width,height,r_frame_rate,"
"nb_read_frames,sample_rate,channels,duration:"
"format=duration,size"
),
"-of",
"json",
str(video_path),
],
check=True,
capture_output=True,
text=True,
)
media = json.loads(inspected.stdout)
video_stream = next(
(item for item in media.get("streams", []) if item.get("codec_type") == "video"),
None,
)
audio_stream = next(
(item for item in media.get("streams", []) if item.get("codec_type") == "audio"),
None,
)
if video_stream is None or audio_stream is None:
raise ProbeFailure("VHS output does not contain both video and audio")
if (
video_stream.get("codec_name") != "h264"
or int(video_stream.get("nb_read_frames", 0)) != 120
or video_stream.get("r_frame_rate") != "24/1"
or int(video_stream.get("width", 0)) != 16
or int(video_stream.get("height", 0)) != 16
or int(audio_stream.get("sample_rate", 0)) != 32_000
):
raise ProbeFailure(f"unexpected VHS media contract: {media}")
return {
"save_frames": len(save_items),
"preview_frames": len(preview_items),
"vhs_file": str(video_path),
"vhs_bytes": int(video_path.stat().st_size),
"vhs_video_frames": int(video_stream["nb_read_frames"]),
"vhs_video_fps": video_stream["r_frame_rate"],
"vhs_video_duration": float(video_stream["duration"]),
"vhs_audio_sample_rate": int(audio_stream["sample_rate"]),
"vhs_container_duration": float(media["format"]["duration"]),
}
def _collect_until_empty(manager, backing_root: Path) -> None:
for _ in range(8):
gc.collect()
manager.collect_ready()
if not _managed_files(backing_root, "bin") and not _managed_files(
backing_root,
"json",
):
return
time.sleep(0.05)
raise ProbeFailure("released cache storage did not become reclaimable")
def _run_child_mode(args, package_root: Path) -> int:
_install_paths(args.comfy_root)
package = _load_package(package_root)
buffers = importlib.import_module(f"{package.__name__}.v3.file_backed_buffer")
root = args.backing_root.resolve()
if args.child_mode == "create-stale":
manager = buffers.get_file_backed_image_manager(root)
allocation = manager.allocate((2, 8, 8, 3), dtype=torch.float32)
tensor = allocation.publish()
tensor.fill_(0.25)
os._exit(0)
if args.child_mode == "clean-stale":
buffers.get_file_backed_image_manager(root)
payload = {
"bin": len(_managed_files(root, "bin")),
"json": len(_managed_files(root, "json")),
}
print(json.dumps(payload, sort_keys=True))
return 0
raise ProbeFailure(f"unknown child mode: {args.child_mode}")
def _restart_stale_probe(args, package_root: Path, stale_root: Path) -> dict:
common = [
sys.executable,
str(Path(__file__).resolve()),
"--comfy-root",
str(args.comfy_root),
"--package-root",
str(package_root),
"--backing-root",
str(stale_root),
]
subprocess.run(
[*common, "--child-mode", "create-stale"],
check=True,
capture_output=True,
text=True,
)
created = (
len(_managed_files(stale_root, "bin")),
len(_managed_files(stale_root, "json")),
)
if created != (1, 1):
raise ProbeFailure(f"crash child left unexpected files: {created}")
cleaned = subprocess.run(
[*common, "--child-mode", "clean-stale"],
check=True,
capture_output=True,
text=True,
)
line = next(
(value for value in reversed(cleaned.stdout.splitlines()) if value.startswith("{")),
"",
)
payload = json.loads(line)
if payload != {"bin": 0, "json": 0}:
raise ProbeFailure(f"restart stale cleanup failed: {payload}")
return {"created_pairs": 1, "remaining_pairs_after_restart": 0}
def run_probe(args, package_root: Path) -> dict:
_install_paths(args.comfy_root)
import folder_paths
import nodes as core_nodes
import server as comfy_server
from execution import CacheType, PromptExecutor
# A normal ComfyUI process installs this singleton before loading custom
# nodes. The headless probe supplies only the queue fields VHS imports;
# Video Combine itself does not use them without a meta-batch.
if not hasattr(comfy_server.PromptServer, "instance"):
comfy_server.PromptServer.instance = SimpleNamespace(
prompt_queue=SimpleNamespace(
currently_running={},
put=lambda _item: None,
),
number=0,
)
from videohelpersuite.nodes import VideoCombine
package = _load_package(package_root)
_register_probe_nodes(core_nodes, package, VideoCombine)
buffers = importlib.import_module(f"{package.__name__}.v3.file_backed_buffer")
work_root = args.work_root.resolve()
output_root = work_root / "output"
temp_root = work_root / "temp"
backing_root = temp_root / "h3-continuum-v35-file-backed"
stale_root = work_root / "restart-stale"
for path in (output_root, temp_root, backing_root, stale_root):
path.mkdir(parents=True, exist_ok=True)
old_output = folder_paths.get_output_directory()
old_temp = folder_paths.get_temp_directory()
folder_paths.set_output_directory(str(output_root))
folder_paths.set_temp_directory(str(temp_root))
prompt_graphs = {
"downstream_error": _failure_prompt(fail=True),
"recovery": _failure_prompt(fail=False),
"compatibility": _compatibility_prompt(),
"interrupt": _interrupt_prompt(),
"clear": _clear_prompt(1),
}
(work_root / "p24_prompt_graphs.json").write_text(
json.dumps(prompt_graphs, indent=2, sort_keys=True),
encoding="utf-8",
)
server = _Server()
executor = PromptExecutor(
server,
cache_type=CacheType.CLASSIC,
cache_args={"ram": 16.0, "ram_inactive": 0.0},
)
manager = buffers.get_file_backed_image_manager(backing_root)
started = time.perf_counter()
try:
_execute(executor, prompt_graphs["downstream_error"], "p24-error", ["3"])
if executor.success or not _has_event(executor, "execution_error"):
raise ProbeFailure("intentional downstream error was not reported")
if (len(_managed_files(backing_root, "bin")), len(_managed_files(backing_root, "json"))) != (1, 1):
raise ProbeFailure("completed assembler output was not retained by Core cache")
retained = manager.collect_ready()
if retained.deferred < 1:
raise ProbeFailure("Core cache did not keep mapped storage alive after error")
_execute(executor, prompt_graphs["recovery"], "p24-recovery", ["3"])
recovery_cached = _cached_nodes(executor)
if not executor.success or "2" not in recovery_cached:
raise ProbeFailure("requeue did not reuse cached V3.5 assembler output")
if len(_managed_files(backing_root, "bin")) != 1:
raise ProbeFailure("same-input requeue created another backing file")
_execute(
executor,
prompt_graphs["compatibility"],
"p24-compatibility",
["4", "5", "6"],
)
if not executor.success or "2" not in _cached_nodes(executor):
raise ProbeFailure("downstream compatibility run did not reuse assembler cache")
downstream = _assert_output_paths(
output_root,
temp_root,
executor.history_result,
)
output_files_before = _all_output_files(output_root)
temp_files_before = _all_output_files(temp_root)
_execute(
executor,
prompt_graphs["compatibility"],
"p24-compatibility-requeue",
["4", "5", "6"],
)
second_cached = _cached_nodes(executor)
if not {"2", "4", "5", "6"}.issubset(second_cached):
raise ProbeFailure(
f"compatibility requeue cache miss: {sorted(second_cached)}"
)
if output_files_before != _all_output_files(output_root):
raise ProbeFailure("cached requeue unexpectedly rewrote output files")
if temp_files_before != _all_output_files(temp_root):
raise ProbeFailure("cached requeue unexpectedly rewrote temp files")
_execute(executor, prompt_graphs["clear"], "p24-clear", ["9"])
gc.collect()
if manager.tracked_count() != 1:
raise ProbeFailure("cache eviction lost lifetime-manager tracking state")
replacement = _failure_prompt(fail=False, nonce=2)
_execute(executor, replacement, "p24-replacement", ["3"])
if not executor.success or len(_managed_files(backing_root, "bin")) != 1:
raise ProbeFailure("post-eviction allocation did not keep backing pairs bounded")
_execute(executor, _clear_prompt(2), "p24-final-clear", ["9"])
_collect_until_empty(manager, backing_root)
_execute(executor, prompt_graphs["interrupt"], "p24-interrupt", ["3"])
if executor.success or not _has_event(executor, "execution_interrupted"):
raise ProbeFailure("Core interrupt was not propagated as execution_interrupted")
if _managed_files(backing_root, "bin") or _managed_files(backing_root, "json"):
raise ProbeFailure("interrupted unpublished allocation left backing files")
_execute(executor, _clear_prompt(3), "p24-interrupt-clear", ["9"])
_collect_until_empty(manager, backing_root)
stale = _restart_stale_probe(args, package_root, stale_root)
elapsed = time.perf_counter() - started
result = {
"phase": "P2-4",
"status": "PASS",
"gpu_used": False,
"fixture": {
"source": "deterministic decoded CPU IMAGE/AUDIO",
"frames": 124,
"assembled_frames": 120,
"width": 16,
"height": 16,
"fps": 24,
"audio_sample_rate": 32_000,
"model": "not applicable",
"lora": "not applicable",
"sampler": "not applicable",
"prompt": "not applicable",
"media_inputs": "none",
},
"cache": {
"implementation": "ComfyUI PromptExecutor CLASSIC / HierarchicalCache",
"downstream_error_retained": True,
"same_input_requeue_cached": True,
"recovery_cached_nodes": sorted(recovery_cached),
"compatibility_requeue_cached_nodes": sorted(second_cached),
"post_eviction_pair_count_bounded": True,
"final_pairs": 0,
},
"downstream": downstream,
"interrupt": {
"core_execution_interrupted": True,
"unpublished_pairs_remaining": 0,
},
"restart_stale_cleanup": stale,
"elapsed_seconds": round(elapsed, 3),
"work_root": str(work_root),
}
(work_root / "p24_result.json").write_text(
json.dumps(result, indent=2, sort_keys=True),
encoding="utf-8",
)
return result
finally:
core_nodes.interrupt_processing(False)
folder_paths.set_output_directory(old_output)
folder_paths.set_temp_directory(old_temp)
def _parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--comfy-root", type=Path, required=True)
parser.add_argument("--package-root", type=Path)
parser.add_argument("--work-root", type=Path)
parser.add_argument("--child-mode", choices=("create-stale", "clean-stale"))
parser.add_argument("--backing-root", type=Path)
return parser.parse_args()
def main() -> int:
args = _parse_args()
args.comfy_root = args.comfy_root.resolve()
package_root = (
args.package_root.resolve()
if args.package_root is not None
else Path(__file__).resolve().parents[1]
)
if args.child_mode is not None:
if args.backing_root is None:
raise ProbeFailure("--backing-root is required in child mode")
return _run_child_mode(args, package_root)
if args.work_root is None:
raise ProbeFailure("--work-root is required")
result = run_probe(args, package_root)
print(json.dumps(result, indent=2, sort_keys=True))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+963
View File
@@ -0,0 +1,963 @@
"""Phase 2A P2-5 GPU/RSS acceptance probe for the V3.5 assembler.
The probe reuses a completed three-chunk Run Storage revision. It restores
the logical AV latents, losslessly reconstructs the accepted Long Terminal
Merge physical decode groups, runs the installed Core Video/Audio VAE Decode
nodes, and compares independent RAM and Disk-backed assembler processes.
No sampler/model/CLIP execution is performed and no browser interaction is
required. Each backend runs in a fresh process so its RSS peak is not
contaminated by the other backend's ComfyUI cache.
"""
from __future__ import annotations
import argparse
import gc
import hashlib
import importlib
import importlib.util
import json
from pathlib import Path
import shutil
import subprocess
import sys
import threading
import time
from types import SimpleNamespace
from typing import Any
import psutil
import torch
from safetensors.torch import load_file
class ProbeFailure(RuntimeError):
pass
class _Server:
def __init__(self):
self.client_id = None
self.last_node_id = None
self.messages: list[tuple[str, dict[str, Any], Any]] = []
def send_sync(self, event, data, client_id):
self.messages.append((event, data, client_id))
class _ResourceMonitor:
def __init__(self, interval: float = 0.05):
self.interval = float(interval)
self.process = psutil.Process()
self.stop_event = threading.Event()
self.thread: threading.Thread | None = None
self.samples = 0
self.peak_rss = 0
self.minimum_system_available = 1 << 63
self.minimum_cuda_free = 1 << 63
def _sample(self) -> None:
try:
self.peak_rss = max(self.peak_rss, int(self.process.memory_info().rss))
self.minimum_system_available = min(
self.minimum_system_available,
int(psutil.virtual_memory().available),
)
if torch.cuda.is_available():
free, _total = torch.cuda.mem_get_info()
self.minimum_cuda_free = min(self.minimum_cuda_free, int(free))
self.samples += 1
except Exception:
pass
def _run(self) -> None:
while not self.stop_event.wait(self.interval):
self._sample()
def start(self) -> None:
self._sample()
self.thread = threading.Thread(target=self._run, daemon=True)
self.thread.start()
def stop(self) -> dict[str, Any]:
self._sample()
self.stop_event.set()
if self.thread is not None:
self.thread.join(timeout=5.0)
return {
"samples": int(self.samples),
"peak_rss_bytes": int(self.peak_rss),
"minimum_system_available_bytes": int(self.minimum_system_available),
"minimum_cuda_free_bytes": (
int(self.minimum_cuda_free)
if self.minimum_cuda_free != 1 << 63
else None
),
}
def _install_paths(comfy_root: Path) -> None:
for value in (
comfy_root,
comfy_root / "custom_nodes" / "comfyui-videohelpersuite",
):
text = str(value)
if text not in sys.path:
sys.path.insert(0, text)
def _load_package(package_root: Path):
name = "h3_continuum_p25_probe"
spec = importlib.util.spec_from_file_location(
name,
package_root / "__init__.py",
submodule_search_locations=[str(package_root)],
)
if spec is None or spec.loader is None:
raise ProbeFailure("cannot create Continuum package import specification")
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
return module
def _hash_tensor(tensor: torch.Tensor, batch: int = 8) -> str:
digest = hashlib.sha256()
value = tensor.detach().cpu()
if value.ndim == 0:
digest.update(value.numpy().tobytes(order="C"))
return digest.hexdigest()
for start in range(0, int(value.shape[0]), int(batch)):
block = value[start : start + int(batch)].contiguous()
digest.update(block.numpy().tobytes(order="C"))
return digest.hexdigest()
def _tensor_is_finite_batched(tensor: torch.Tensor, batch: int = 8) -> bool:
value = tensor.detach().cpu()
if value.ndim == 0:
return bool(torch.isfinite(value).item())
for start in range(0, int(value.shape[0]), int(batch)):
if not bool(torch.isfinite(value[start : start + int(batch)]).all().item()):
return False
return True
def _process_memory_snapshot() -> dict[str, int]:
values = psutil.Process().memory_full_info()._asdict()
return {key: int(value) for key, value in values.items()}
def _managed_files(root: Path, suffix: str) -> list[Path]:
return sorted(root.glob(f"h3c-p20-*.{suffix}"))
def _collect_until_empty(manager, root: Path) -> None:
for _ in range(20):
gc.collect()
manager.collect_ready()
if not _managed_files(root, "bin") and not _managed_files(root, "json"):
return
time.sleep(0.1)
raise ProbeFailure("released Disk-backed output was not reclaimed")
def _register_probe_nodes(
core_nodes,
package_module,
revision_root: Path,
results,
video_combine=None,
stress_size: int = 0,
):
plan_module = importlib.import_module(f"{package_module.__name__}.v3.plan")
class P25StoredLongTerminalFixture:
@classmethod
def INPUT_TYPES(cls):
return {"required": {"nonce": ("INT", {"default": 0})}}
RETURN_TYPES = ("LATENT", "LATENT", "H3_CONTINUUM_ASSEMBLY_PLAN")
RETURN_NAMES = ("video_latents", "audio_latents", "assembly_plan")
OUTPUT_IS_LIST = (True, True, False)
FUNCTION = "load"
CATEGORY = "P2-5 Probe"
def load(self, nonce):
del nonce
manifest_path = revision_root / "manifest.json"
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
records = sorted(
manifest["chunks"],
key=lambda item: int(item["sequence_index"]),
)
entries = []
for record in records:
tensors = load_file(
str(revision_root / "chunks" / record["filename"]),
device="cpu",
)
entry = dict(record["entry"])
entry["video"] = tensors["video"]
entry["audio"] = tensors["audio"]
entry["prompt"] = f"stored:{entry.get('prompt_hash', '')}"
entries.append(entry)
decode_entries, plan = plan_module.prepare_physical_decode_entries(
entries,
chunk_seconds=float(manifest["contract"]["global"]["chunk_seconds"]),
preserve_final_frame=manifest.get("last_frame_hash") not in (None, "none"),
terminal_merged=True,
)
results["fixture"] = {
"revision_id": manifest["revision_id"],
"logical_chunks": len(entries),
"physical_decode_groups": len(decode_entries),
"video_shapes": [list(item["video"].shape) for item in decode_entries],
"audio_shapes": [list(item["audio"].shape) for item in decode_entries],
"width": int(plan["width"]),
"height": int(plan["height"]),
"target_frames": int(plan["target_frames"]),
"prompt_mode": manifest["contract"]["prompt_mode"],
"conditioning_mode": manifest["contract"]["global"]["conditioning_mode"],
"sampler": manifest["contract"]["global"]["sampler"]["function"],
"sigma_count": int(manifest["contract"]["global"]["sigmas"]["shape"][0]),
"base_seed": int(manifest["contract"]["global"]["base_seed"]),
}
return (
[{"samples": item["video"]} for item in decode_entries],
[{"samples": item["audio"]} for item in decode_entries],
plan,
)
class P25DigestOutput:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"images": ("IMAGE",),
"audio": ("AUDIO",),
"report": ("STRING",),
"backend": (["Auto", "RAM", "Disk-backed"],),
}
}
RETURN_TYPES = ("STRING",)
FUNCTION = "consume"
OUTPUT_NODE = True
CATEGORY = "P2-5 Probe"
def consume(self, images, audio, report, backend):
waveform = audio["waveform"]
hash_batch = 1 if results.get("stress") else 8
results["pre_digest_memory"] = _process_memory_snapshot()
results["digest"] = {
"backend": str(backend),
"video_shape": list(images.shape),
"video_dtype": str(images.dtype),
"video_sha256": _hash_tensor(images, batch=hash_batch),
"video_finite": _tensor_is_finite_batched(
images, batch=hash_batch
),
"audio_shape": list(waveform.shape),
"audio_dtype": str(waveform.dtype),
"audio_sha256": _hash_tensor(waveform),
"audio_finite": _tensor_is_finite_batched(waveform),
"audio_sample_rate": int(audio["sample_rate"]),
"report": str(report),
}
return {"ui": {"p25": [str(backend)]}, "result": (str(backend),)}
class P25SyntheticDecodedStressFixture:
"""Large decoded IMAGE fixture with O(H*W), not O(T*H*W), source RAM."""
@classmethod
def INPUT_TYPES(cls):
return {"required": {"nonce": ("INT", {"default": 0})}}
RETURN_TYPES = ("IMAGE", "AUDIO", "H3_CONTINUUM_ASSEMBLY_PLAN")
RETURN_NAMES = ("images", "audio", "assembly_plan")
OUTPUT_IS_LIST = (True, True, False)
FUNCTION = "load"
CATEGORY = "P2-5 Probe"
def load(self, nonce):
del nonce
if stress_size <= 0:
raise ProbeFailure("synthetic stress fixture requires --stress-size")
# Reuse the accepted Long Terminal Merge plan instead of inventing a
# synthetic grouping contract. Only the already-decoded pixel frames
# are replaced. Expanded frame views keep source RAM bounded while
# the assembler must still write every byte of the final IMAGE.
_video, _audio, plan = P25StoredLongTerminalFixture().load(0)
del _video, _audio
gc.collect()
plan = dict(plan)
plan["width"] = int(stress_size)
plan["height"] = int(stress_size)
units = list(plan.get("decode_groups") or plan["chunks"])
axis = torch.linspace(0.0, 1.0, stress_size, dtype=torch.float32)
yy = axis.view(stress_size, 1)
xx = axis.view(1, stress_size)
frames = []
waveforms = []
sample_rate = 32000
for index, unit in enumerate(units):
base = torch.empty(
(1, stress_size, stress_size, 3), dtype=torch.float32
)
base[0, ..., 0].copy_((xx + index * 0.07).remainder(1.0))
base[0, ..., 1].copy_((yy + index * 0.11).remainder(1.0))
base[0, ..., 2].copy_(((xx + yy) * 0.5 + index * 0.13).remainder(1.0))
total_frames = int(unit["total_frames"])
frames.append(base.expand(total_frames, -1, -1, -1))
total_samples = int(round(total_frames / 24.0 * sample_rate))
phase = torch.linspace(
0.0,
(index + 1) * 24.0,
total_samples,
dtype=torch.float32,
)
wave = (0.05 * torch.sin(phase)).reshape(1, 1, -1)
waveforms.append(
{
"waveform": wave.expand(1, 2, -1),
"sample_rate": sample_rate,
}
)
final_bytes = (
int(plan["target_frames"])
* stress_size
* stress_size
* 3
* torch.tensor([], dtype=torch.float32).element_size()
)
results["stress"] = {
"size": int(stress_size),
"target_frames": int(plan["target_frames"]),
"final_image_bytes": int(final_bytes),
"final_image_gib": round(final_bytes / (1024**3), 3),
"source_frame_storage_bytes": int(
sum(frame.untyped_storage().nbytes() for frame in frames)
),
"source_frames_are_expanded_views": all(
int(frame.stride(0)) == 0 for frame in frames
),
}
return frames, waveforms, plan
class P25ClearOutput:
@classmethod
def INPUT_TYPES(cls):
return {"required": {"token": ("INT", {"default": 0})}}
RETURN_TYPES = ("STRING",)
FUNCTION = "finish"
OUTPUT_NODE = True
CATEGORY = "P2-5 Probe"
def finish(self, token):
return (f"clear-{int(token)}",)
class P25InterruptAfterAllocation:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"images": ("IMAGE",),
"audio": ("AUDIO",),
"assembly_plan": ("H3_CONTINUUM_ASSEMBLY_PLAN",),
}
}
RETURN_TYPES = ("IMAGE", "AUDIO", "H3_CONTINUUM_ASSEMBLY_PLAN")
RETURN_NAMES = ("images", "audio", "assembly_plan")
INPUT_IS_LIST = True
OUTPUT_IS_LIST = (True, True, False)
FUNCTION = "arm"
CATEGORY = "P2-5 Probe"
def arm(self, images, audio, assembly_plan):
backing_root = Path(results["backing_root"])
def trigger():
deadline = time.monotonic() + 30.0
while time.monotonic() < deadline:
if _managed_files(backing_root, "bin"):
results["interrupt_observed_allocation"] = True
time.sleep(0.05)
core_nodes.interrupt_processing(True)
return
time.sleep(0.005)
results["interrupt_observed_allocation"] = False
core_nodes.interrupt_processing(True)
threading.Thread(target=trigger, daemon=True).start()
return images, audio, assembly_plan[0]
from comfy_extras.nodes_audio import VAEDecodeAudio
core_nodes.NODE_CLASS_MAPPINGS.update(package_module.NODE_CLASS_MAPPINGS)
core_nodes.NODE_CLASS_MAPPINGS.update(
{
"VAEDecodeAudio": VAEDecodeAudio,
"P25StoredLongTerminalFixture": P25StoredLongTerminalFixture,
"P25SyntheticDecodedStressFixture": P25SyntheticDecodedStressFixture,
"P25DigestOutput": P25DigestOutput,
"P25ClearOutput": P25ClearOutput,
"P25InterruptAfterAllocation": P25InterruptAfterAllocation,
}
)
if video_combine is not None:
core_nodes.NODE_CLASS_MAPPINGS["P25VHSVideoCombine"] = video_combine
def _prompt(
backend: str,
*,
downstream: bool = False,
interrupt_after_allocation: bool = False,
stress: bool = False,
) -> dict[str, Any]:
if stress:
return {
"1": {
"class_type": "P25SyntheticDecodedStressFixture",
"inputs": {"nonce": 0},
},
"6": {
"class_type": "H3ContinuumAssembleSeamV35",
"inputs": {
"images": ["1", 0],
"audio": ["1", 1],
"assembly_plan": ["1", 2],
"exact_total_duration": True,
"audio_seam": "Auto",
"video_seam": "Auto",
"buffer_backend": str(backend),
"diagnostics": "Detailed Report",
},
},
"7": {
"class_type": "P25DigestOutput",
"inputs": {
"images": ["6", 0],
"audio": ["6", 1],
"report": ["6", 2],
"backend": str(backend),
},
},
}
prompt = {
"1": {
"class_type": "P25StoredLongTerminalFixture",
"inputs": {"nonce": 0},
},
"2": {
"class_type": "VAELoader",
"inputs": {"vae_name": "minimax_h3_video_vae_fp16.safetensors"},
},
"3": {
"class_type": "VAELoader",
"inputs": {"vae_name": "minimax_h3_audio_vae_fp32.safetensors"},
},
"4": {
"class_type": "VAEDecode",
"inputs": {"samples": ["1", 0], "vae": ["2", 0]},
},
"5": {
"class_type": "VAEDecodeAudio",
"inputs": {"samples": ["1", 1], "vae": ["3", 0]},
},
"6": {
"class_type": "H3ContinuumAssembleSeamV35",
"inputs": {
"images": ["4", 0],
"audio": ["5", 0],
"assembly_plan": ["1", 2],
"exact_total_duration": True,
"audio_seam": "Auto",
"video_seam": "Auto",
"buffer_backend": str(backend),
"diagnostics": "Detailed Report",
},
},
"7": {
"class_type": "P25DigestOutput",
"inputs": {
"images": ["6", 0],
"audio": ["6", 1],
"report": ["6", 2],
"backend": str(backend),
},
},
}
if interrupt_after_allocation:
prompt["11"] = {
"class_type": "P25InterruptAfterAllocation",
"inputs": {
"images": ["4", 0],
"audio": ["5", 0],
"assembly_plan": ["1", 2],
},
}
prompt["6"]["inputs"].update(
{
"images": ["11", 0],
"audio": ["11", 1],
"assembly_plan": ["11", 2],
}
)
if downstream:
prompt.update(
{
"8": {
"class_type": "SaveImage",
"inputs": {
"images": ["6", 0],
"filename_prefix": "P25/Save",
},
},
"9": {
"class_type": "PreviewImage",
"inputs": {"images": ["6", 0]},
},
"10": {
"class_type": "P25VHSVideoCombine",
"inputs": {
"images": ["6", 0],
"audio": ["6", 1],
"frame_rate": 24,
"loop_count": 0,
"filename_prefix": "P25/VHS",
"format": "video/h264-mp4",
"pingpong": False,
"save_output": True,
},
},
}
)
return prompt
def _downstream_result(history: dict[str, Any]) -> dict[str, Any]:
outputs = history.get("outputs", {})
save_items = outputs.get("8", {}).get("images", [])
preview_items = outputs.get("9", {}).get("images", [])
video_items = outputs.get("10", {}).get("gifs", [])
if len(save_items) != 360 or len(preview_items) != 360 or len(video_items) != 1:
raise ProbeFailure(
"unexpected downstream output counts: "
f"save={len(save_items)} preview={len(preview_items)} vhs={len(video_items)}"
)
video_path = Path(video_items[0]["fullpath"])
if not video_path.is_file() or video_path.stat().st_size <= 0:
raise ProbeFailure(f"VHS output is missing: {video_path}")
ffprobe = shutil.which("ffprobe")
if ffprobe is None:
raise ProbeFailure("ffprobe is required for P2-5 downstream acceptance")
inspected = subprocess.run(
[
ffprobe,
"-v",
"error",
"-count_frames",
"-show_entries",
(
"stream=codec_type,codec_name,width,height,r_frame_rate,"
"nb_read_frames,sample_rate,channels,duration:format=duration,size"
),
"-of",
"json",
str(video_path),
],
check=True,
capture_output=True,
text=True,
)
media = json.loads(inspected.stdout)
video_stream = next(
(item for item in media.get("streams", []) if item.get("codec_type") == "video"),
None,
)
audio_stream = next(
(item for item in media.get("streams", []) if item.get("codec_type") == "audio"),
None,
)
if video_stream is None or audio_stream is None:
raise ProbeFailure("VHS output does not contain both video and audio")
expected = (
int(video_stream.get("width", 0)) == 640
and int(video_stream.get("height", 0)) == 640
and int(video_stream.get("nb_read_frames", 0)) == 360
and video_stream.get("r_frame_rate") == "24/1"
and int(audio_stream.get("sample_rate", 0)) == 32_000
)
if not expected:
raise ProbeFailure(f"unexpected VHS media contract: {media}")
return {
"save_frames": len(save_items),
"preview_frames": len(preview_items),
"vhs_file": str(video_path),
"vhs_bytes": int(video_path.stat().st_size),
"video_codec": video_stream.get("codec_name"),
"video_frames": int(video_stream["nb_read_frames"]),
"video_fps": video_stream["r_frame_rate"],
"video_duration": float(video_stream["duration"]),
"audio_codec": audio_stream.get("codec_name"),
"audio_sample_rate": int(audio_stream["sample_rate"]),
"container_duration": float(media["format"]["duration"]),
}
def _clear_prompt() -> dict[str, Any]:
return {"9": {"class_type": "P25ClearOutput", "inputs": {"token": 1}}}
def _has_event(executor, wanted: str) -> bool:
return any(event == wanted for event, _data in executor.status_messages)
def _cached_nodes(executor) -> set[str]:
for event, data in executor.status_messages:
if event == "execution_cached":
return {str(value) for value in data.get("nodes", [])}
return set()
def _run_backend(args, package_root: Path) -> dict[str, Any]:
_install_paths(args.comfy_root)
import folder_paths
import nodes as core_nodes
import server as comfy_server
from execution import CacheType, PromptExecutor
if not hasattr(comfy_server.PromptServer, "instance"):
comfy_server.PromptServer.instance = SimpleNamespace(
prompt_queue=SimpleNamespace(currently_running={}, put=lambda _item: None),
number=0,
)
work_root = args.work_root.resolve()
output_root = work_root / "output"
temp_root = work_root / "temp"
backing_root = temp_root / "h3-continuum-v35-file-backed"
for path in (output_root, temp_root, backing_root):
path.mkdir(parents=True, exist_ok=True)
package = _load_package(package_root)
results: dict[str, Any] = {"backing_root": str(backing_root)}
video_combine = None
if args.downstream:
from videohelpersuite.nodes import VideoCombine
video_combine = VideoCombine
_register_probe_nodes(
core_nodes,
package,
args.revision_root,
results,
video_combine=video_combine,
stress_size=int(args.stress_size),
)
buffers = importlib.import_module(f"{package.__name__}.v3.file_backed_buffer")
folder_paths.set_output_directory(str(output_root))
folder_paths.set_temp_directory(str(temp_root))
prompt = _prompt(
args.backend,
downstream=bool(args.downstream),
interrupt_after_allocation=bool(args.interrupt_after_allocation),
stress=bool(args.stress_size),
)
(work_root / "p25_prompt_graph.json").write_text(
json.dumps(prompt, indent=2, sort_keys=True),
encoding="utf-8",
)
executor = PromptExecutor(
_Server(),
cache_type=CacheType.CLASSIC,
cache_args={"ram": 64.0, "ram_inactive": 0.0},
)
if torch.cuda.is_available():
torch.cuda.reset_peak_memory_stats()
monitor = _ResourceMonitor()
monitor.start()
started = time.perf_counter()
execute_outputs = ["7", "8", "9", "10"] if args.downstream else ["7"]
executor.execute(
prompt,
f"p25-{args.backend.lower()}",
extra_data={},
execute_outputs=execute_outputs,
)
cache_requeue = None
if args.cache_requeue and executor.success:
first_pairs = (
len(_managed_files(backing_root, "bin")),
len(_managed_files(backing_root, "json")),
)
executor.execute(
prompt,
f"p25-{args.backend.lower()}-requeue",
extra_data={},
execute_outputs=execute_outputs,
)
cached = _cached_nodes(executor)
second_pairs = (
len(_managed_files(backing_root, "bin")),
len(_managed_files(backing_root, "json")),
)
cache_requeue = {
"cached_nodes": sorted(cached),
"assembler_cached": "6" in cached,
"first_pair_count": list(first_pairs),
"second_pair_count": list(second_pairs),
"pair_count_bounded": first_pairs == second_pairs,
}
if not executor.success or not cache_requeue["assembler_cached"]:
raise ProbeFailure(f"large-output requeue cache miss: {cache_requeue}")
if not cache_requeue["pair_count_bounded"]:
raise ProbeFailure(f"large-output requeue grew backing files: {cache_requeue}")
elapsed = time.perf_counter() - started
resources = monitor.stop()
manager = buffers.get_file_backed_image_manager(backing_root)
if args.interrupt_after_allocation:
interrupted = _has_event(executor, "execution_interrupted")
core_nodes.interrupt_processing(False)
executor.execute(_clear_prompt(), "p25-clear", extra_data={}, execute_outputs=["9"])
del executor
_collect_until_empty(manager, backing_root)
result = {
"backend": args.backend,
"stage": "interrupt-after-allocation",
"status": "PASS" if interrupted else "FAIL",
"execution_interrupted": bool(interrupted),
"allocation_observed_before_interrupt": bool(
results.get("interrupt_observed_allocation", False)
),
"fixture": results.get("fixture"),
"elapsed_seconds": round(elapsed, 3),
"managed_bin_after_interrupt": len(_managed_files(backing_root, "bin")),
"managed_json_after_interrupt": len(_managed_files(backing_root, "json")),
"resources": resources,
"work_root": str(work_root),
}
if not interrupted or not result["allocation_observed_before_interrupt"]:
raise ProbeFailure(f"post-allocation interrupt was not proven: {result}")
(work_root / "p25_interrupt_result.json").write_text(
json.dumps(result, indent=2, sort_keys=True),
encoding="utf-8",
)
return result
if not executor.success or _has_event(executor, "execution_error"):
raise ProbeFailure(f"{args.backend} PromptExecutor failed")
if "digest" not in results or "fixture" not in results:
raise ProbeFailure(f"{args.backend} probe outputs were not captured")
bin_files = _managed_files(backing_root, "bin")
json_files = _managed_files(backing_root, "json")
resources.update(
{
"elapsed_seconds": round(elapsed, 3),
"cuda_peak_allocated_bytes": (
int(torch.cuda.max_memory_allocated()) if torch.cuda.is_available() else 0
),
"cuda_peak_reserved_bytes": (
int(torch.cuda.max_memory_reserved()) if torch.cuda.is_available() else 0
),
"backing_bin_count_while_cached": len(bin_files),
"backing_json_count_while_cached": len(json_files),
"backing_bytes_while_cached": sum(item.stat().st_size for item in bin_files),
"pre_digest_memory": results.get("pre_digest_memory"),
}
)
downstream = _downstream_result(executor.history_result) if args.downstream else None
executor.execute(_clear_prompt(), "p25-clear", extra_data={}, execute_outputs=["9"])
del executor
_collect_until_empty(manager, backing_root)
resources["managed_bin_after_cache_release"] = len(
_managed_files(backing_root, "bin")
)
resources["managed_json_after_cache_release"] = len(
_managed_files(backing_root, "json")
)
resources["nonmanaged_root_entries_after_cache_release"] = len(
[
item
for item in backing_root.iterdir()
if item.name != ".h3c-p20.lock"
]
)
result = {
"backend": args.backend,
"status": "PASS",
"fixture": results["fixture"],
"stress": results.get("stress"),
"digest": results["digest"],
"resources": resources,
"cache_requeue": cache_requeue,
"downstream": downstream,
"work_root": str(work_root),
}
(work_root / "p25_backend_result.json").write_text(
json.dumps(result, indent=2, sort_keys=True),
encoding="utf-8",
)
return result
def _last_json(stdout: str) -> dict[str, Any]:
marker = "P25_RESULT_JSON="
line = next(
(value for value in reversed(stdout.splitlines()) if value.startswith(marker)),
"",
)
if not line:
raise ProbeFailure("backend child did not emit a result marker")
return json.loads(line[len(marker) :])
def _summarize_results(work_root: Path, results: dict[str, Any] | None = None):
work_root = work_root.resolve()
if results is None:
results = {
"RAM": json.loads(
(work_root / "ram" / "p25_backend_result.json").read_text(
encoding="utf-8"
)
),
"Disk-backed": json.loads(
(work_root / "disk" / "p25_backend_result.json").read_text(
encoding="utf-8"
)
),
}
ram = results["RAM"]
disk = results["Disk-backed"]
disk_backing_root = (
work_root / "disk" / "temp" / "h3-continuum-v35-file-backed"
)
managed_bin_after = len(_managed_files(disk_backing_root, "bin"))
managed_json_after = len(_managed_files(disk_backing_root, "json"))
disk["resources"]["managed_bin_after_cache_release"] = managed_bin_after
disk["resources"]["managed_json_after_cache_release"] = managed_json_after
comparisons = {
"fixture_equal": ram["fixture"] == disk["fixture"],
"video_shape_equal": ram["digest"]["video_shape"] == disk["digest"]["video_shape"],
"video_sha256_equal": ram["digest"]["video_sha256"] == disk["digest"]["video_sha256"],
"audio_shape_equal": ram["digest"]["audio_shape"] == disk["digest"]["audio_shape"],
"audio_sha256_equal": ram["digest"]["audio_sha256"] == disk["digest"]["audio_sha256"],
"audio_sample_rate_equal": ram["digest"]["audio_sample_rate"] == disk["digest"]["audio_sample_rate"],
"both_finite": bool(
ram["digest"]["video_finite"]
and disk["digest"]["video_finite"]
and ram["digest"]["audio_finite"]
and disk["digest"]["audio_finite"]
),
"disk_backing_reclaimed": managed_bin_after == 0 and managed_json_after == 0,
}
if not all(comparisons.values()):
raise ProbeFailure(f"RAM/Disk-backed comparison failed: {comparisons}")
peak_delta = (
int(ram["resources"]["peak_rss_bytes"])
- int(disk["resources"]["peak_rss_bytes"])
)
result = {
"phase": "P2-5",
"stage": "small-long-terminal-ab",
"status": "PASS",
"gpu_used": True,
"comparisons": comparisons,
"rss_peak_reduction_bytes": peak_delta,
"rss_peak_reduction_percent": round(
peak_delta / max(1, int(ram["resources"]["peak_rss_bytes"])) * 100.0,
3,
),
"backends": results,
"work_root": str(work_root),
}
(work_root / "p25_result.json").write_text(
json.dumps(result, indent=2, sort_keys=True),
encoding="utf-8",
)
return result
def _run_parent(args, package_root: Path) -> dict[str, Any]:
work_root = args.work_root.resolve()
work_root.mkdir(parents=True, exist_ok=True)
common = [
sys.executable,
str(Path(__file__).resolve()),
"--comfy-root",
str(args.comfy_root),
"--package-root",
str(package_root),
"--revision-root",
str(args.revision_root),
]
results = {}
for backend, folder in (("RAM", "ram"), ("Disk-backed", "disk")):
child = subprocess.run(
[*common, "--work-root", str(work_root / folder), "--backend", backend],
check=True,
capture_output=True,
text=True,
)
(work_root / f"{folder}_stdout.txt").write_text(child.stdout, encoding="utf-8")
(work_root / f"{folder}_stderr.txt").write_text(child.stderr, encoding="utf-8")
results[backend] = _last_json(child.stdout)
return _summarize_results(work_root, results)
def _parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--comfy-root", type=Path, required=True)
parser.add_argument("--package-root", type=Path)
parser.add_argument("--revision-root", type=Path, required=True)
parser.add_argument("--work-root", type=Path, required=True)
parser.add_argument("--backend", choices=("Auto", "RAM", "Disk-backed"))
parser.add_argument("--summarize-existing", action="store_true")
parser.add_argument("--downstream", action="store_true")
parser.add_argument("--interrupt-after-allocation", action="store_true")
parser.add_argument("--cache-requeue", action="store_true")
parser.add_argument(
"--stress-size",
type=int,
default=0,
help="Use a synthetic decoded square fixture of this size; 1536 is 9.49 GiB.",
)
return parser.parse_args()
def main() -> int:
args = _parse_args()
args.comfy_root = args.comfy_root.resolve()
args.revision_root = args.revision_root.resolve()
args.work_root = args.work_root.resolve()
package_root = (
args.package_root.resolve()
if args.package_root is not None
else Path(__file__).resolve().parents[1]
)
if args.backend is not None:
result = _run_backend(args, package_root)
print("P25_RESULT_JSON=" + json.dumps(result, sort_keys=True))
return 0
if args.summarize_existing:
result = _summarize_results(args.work_root)
print(json.dumps(result, indent=2, sort_keys=True))
return 0
result = _run_parent(args, package_root)
print(json.dumps(result, indent=2, sort_keys=True))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+189
View File
@@ -0,0 +1,189 @@
"""Submit warm-up and measured Phase 3-2 workflows through the ComfyUI API."""
from __future__ import annotations
import argparse
import json
from pathlib import Path
import sys
import time
import urllib.error
import urllib.request
import uuid
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 p32_gpu_profile_probe import configure_profile, mutate_run
class ApiFailure(RuntimeError):
pass
def _json_request(url: str, payload: Mapping[str, Any] | None = None) -> Any:
data = None
headers = {}
if payload is not None:
data = json.dumps(payload).encode("utf-8")
headers["Content-Type"] = "application/json"
request = urllib.request.Request(url, data=data, headers=headers)
try:
with urllib.request.urlopen(request, timeout=60) as response:
return json.loads(response.read().decode("utf-8"))
except (urllib.error.URLError, TimeoutError, json.JSONDecodeError) as exc:
raise ApiFailure(f"ComfyUI API request failed for {url}: {exc}") from exc
def _history_complete(value: Mapping[str, Any], prompt_id: str) -> bool:
entry = value.get(str(prompt_id))
if not isinstance(entry, Mapping):
return False
status = entry.get("status")
if not isinstance(status, Mapping):
return bool(entry.get("outputs"))
return bool(status.get("completed", False))
def _history_error(value: Mapping[str, Any], prompt_id: str) -> str | None:
entry = value.get(str(prompt_id))
if not isinstance(entry, Mapping):
return None
status = entry.get("status")
messages = status.get("messages", []) if isinstance(status, Mapping) else []
for item in messages:
if isinstance(item, list) and item and item[0] in {
"execution_error",
"execution_interrupted",
}:
return json.dumps(item, ensure_ascii=False)
return None
def submit_and_wait(
*,
server: str,
prompt: Mapping[str, Any],
client_id: str,
timeout_seconds: float,
poll_seconds: float,
) -> tuple[str, dict[str, Any], float]:
started = time.perf_counter()
response = _json_request(
f"{server.rstrip('/')}/prompt",
{"prompt": dict(prompt), "client_id": str(client_id)},
)
prompt_id = str(response.get("prompt_id", ""))
if not prompt_id:
raise ApiFailure(f"ComfyUI did not return a prompt_id: {response}")
while time.perf_counter() - started <= float(timeout_seconds):
history = _json_request(
f"{server.rstrip('/')}/history/{prompt_id}"
)
error = _history_error(history, prompt_id)
if error is not None:
raise ApiFailure(f"ComfyUI prompt {prompt_id} failed: {error}")
if _history_complete(history, prompt_id):
return prompt_id, dict(history), time.perf_counter() - started
time.sleep(float(poll_seconds))
raise ApiFailure(f"ComfyUI prompt {prompt_id} timed out")
def run(args) -> dict[str, Any]:
source = json.loads(args.prompt.read_text(encoding="utf-8"))
configured = configure_profile(source, args.profile)
args.output_root.mkdir(parents=True, exist_ok=True)
client_id = f"p32-{uuid.uuid4().hex}"
results = []
total = 1 + int(args.runs)
for index in range(total):
warmup = index == 0
label = "warmup" if warmup else f"run-{index}"
seed = int(args.seed) + index
prompt = mutate_run(
configured,
seed=seed,
filename_prefix=f"p32/{args.case}_{args.profile}_{label}",
)
prompt_path = args.output_root / f"prompt_{label}.json"
prompt_path.write_text(
json.dumps(prompt, indent=2, ensure_ascii=False),
encoding="utf-8",
)
print(
f"P32 API start case={args.case} profile={args.profile} "
f"label={label} seed={seed}",
flush=True,
)
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=prompt,
client_id=client_id,
timeout_seconds=args.timeout_seconds,
poll_seconds=args.poll_seconds,
)
history_path = args.output_root / f"history_{label}.json"
history_path.write_text(
json.dumps(history, indent=2, ensure_ascii=False),
encoding="utf-8",
)
results.append(
{
"label": label,
"warmup": warmup,
"seed": seed,
"prompt_id": prompt_id,
"api_elapsed_seconds": float(elapsed),
"prompt": str(prompt_path),
"history": str(history_path),
}
)
print(
f"P32 API complete prompt_id={prompt_id} elapsed={elapsed:.3f}s",
flush=True,
)
summary = {
"format": "h3-continuum-phase3-2-api-run-v1",
"case": args.case,
"profile": args.profile,
"server": args.server,
"warmup_excluded": True,
"runs": results,
}
(args.output_root / "api_summary.json").write_text(
json.dumps(summary, indent=2, ensure_ascii=False),
encoding="utf-8",
)
return summary
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--server", default="http://127.0.0.6:8188")
parser.add_argument("--prompt", type=Path, required=True)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument("--case", required=True)
parser.add_argument(
"--profile",
choices=("sage", "sage_sol", "sage_spectrum", "sage_sol_spectrum"),
required=True,
)
parser.add_argument("--runs", type=int, default=3)
parser.add_argument("--seed", type=int, default=32000)
parser.add_argument("--timeout-seconds", type=float, default=3600.0)
parser.add_argument("--poll-seconds", type=float, default=2.0)
return parser
def main() -> int:
args = _parser().parse_args()
if args.runs < 1:
raise SystemExit("--runs must be at least 1")
print(json.dumps(run(args), ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+719
View File
@@ -0,0 +1,719 @@
"""Phase 3-2 diagnostic-only ComfyUI workflow profiler.
This tool runs an API-format prompt through ComfyUI's installed PromptExecutor
in a dedicated process. It does not register a Continuum node or modify the
normal ComfyUI server. CUDA phases are synchronized immediately before and
after each node so GPU work is attributed to the node that launched it.
Two measurement modes are intentionally separate:
``global``
Resets CUDA peak statistics once per workflow execution. The result is
the true process allocator peak for that run. Per-node wall time and
process-memory peaks are still collected, but per-node CUDA peaks are not.
``interval``
Resets CUDA peak statistics before every node. This provides isolated
node allocator peaks, but the workflow-level allocator peak is therefore
not meaningful and is reported as unavailable.
The first execution is a warm-up. Subsequent executions change only the
Continuum base seed and output filename so model/CLIP objects stay warm while
the V3.5 Prompt/CLIP cache remains eligible for a HIT.
"""
from __future__ import annotations
import argparse
import asyncio
import copy
import json
import os
from pathlib import Path
import statistics
import sys
import threading
import time
from typing import Any, Mapping
import psutil
import torch
PROFILE_NAMES = (
"sage",
"sage_sol",
"sage_spectrum",
"sage_sol_spectrum",
)
MEASUREMENT_MODES = ("global", "interval")
_NVML_LOCK = threading.Lock()
_NVML_HANDLE: Any = None
_NVML_UNAVAILABLE = False
_SAMPLER_CLASSES = {
"H3ContinuumSamplerV35",
"H3ContinuumSamplerV34",
}
_VIDEO_DECODE_CLASSES = {"VAEDecode", "VAEDecodeTiled"}
_AUDIO_DECODE_CLASSES = {"VAEDecodeAudio"}
_ASSEMBLY_CLASSES = {"H3ContinuumAssembleSeamV35"}
_VIDEO_CREATE_CLASSES = {"CreateVideo"}
_SAVE_CLASSES = {"SaveVideo", "VHS_VideoCombine"}
class ProbeFailure(RuntimeError):
pass
def _phase_for_class(class_type: str) -> str:
if class_type in _SAMPLER_CLASSES:
return "sampling"
if class_type in _VIDEO_DECODE_CLASSES:
return "video_vae_decode"
if class_type in _AUDIO_DECODE_CLASSES:
return "audio_vae_decode"
if class_type in _ASSEMBLY_CLASSES:
return "assemble_seam"
if class_type in _VIDEO_CREATE_CLASSES:
return "video_create"
if class_type in _SAVE_CLASSES:
return "mp4_encode_save"
return "other"
def _memory_snapshot() -> dict[str, int | None]:
result: dict[str, int | None] = {
"rss_bytes": None,
"private_bytes": None,
"uss_bytes": None,
}
try:
info = psutil.Process().memory_full_info()
result["rss_bytes"] = int(getattr(info, "rss", 0)) or None
private = getattr(info, "private", None)
uss = getattr(info, "uss", None)
result["private_bytes"] = None if private is None else int(private)
result["uss_bytes"] = None if uss is None else int(uss)
except Exception:
pass
return result
def _nvml_free_bytes() -> int | None:
"""Read device-global free VRAM without calling the CUDA runtime.
Calling ``torch.cuda.mem_get_info`` from the polling thread measurably
serialized H3 Sampling on Windows. NVML observes the device externally
and avoids that profiler-induced slowdown.
"""
global _NVML_HANDLE, _NVML_UNAVAILABLE
if _NVML_UNAVAILABLE:
return None
try:
import pynvml
with _NVML_LOCK:
if _NVML_HANDLE is None:
pynvml.nvmlInit()
index = int(torch.cuda.current_device()) if torch.cuda.is_available() else 0
_NVML_HANDLE = pynvml.nvmlDeviceGetHandleByIndex(index)
return int(pynvml.nvmlDeviceGetMemoryInfo(_NVML_HANDLE).free)
except Exception:
_NVML_UNAVAILABLE = True
return None
class ResourceMonitor:
"""Poll process and device counters without changing allocator state."""
def __init__(self, interval: float = 0.05):
self.interval = float(interval)
self.process = psutil.Process()
self.stop_event = threading.Event()
self.thread: threading.Thread | None = None
self.samples = 0
self.peak_rss = 0
self.peak_private: int | None = None
self.peak_uss: int | None = None
self.minimum_system_available = 1 << 63
self.minimum_cuda_free = 1 << 63
def _sample(self) -> None:
try:
# Full process memory inspection is too expensive to poll during
# H3 Sampling on Windows. RSS/private use the cheap counters;
# USS is sampled only at monitor start and stop.
memory = self.process.memory_info()
self.peak_rss = max(self.peak_rss, int(memory.rss))
private = getattr(memory, "private", None)
if private is not None:
self.peak_private = max(self.peak_private or 0, int(private))
self.minimum_system_available = min(
self.minimum_system_available,
int(psutil.virtual_memory().available),
)
free = _nvml_free_bytes()
if free is not None:
self.minimum_cuda_free = min(self.minimum_cuda_free, int(free))
self.samples += 1
except Exception:
pass
def _run(self) -> None:
while not self.stop_event.wait(self.interval):
self._sample()
def start(self) -> None:
full = _memory_snapshot()
uss = full.get("uss_bytes")
if uss is not None:
self.peak_uss = int(uss)
self._sample()
self.thread = threading.Thread(target=self._run, daemon=True)
self.thread.start()
def stop(self) -> dict[str, int | None]:
self._sample()
self.stop_event.set()
if self.thread is not None:
self.thread.join(timeout=5.0)
full = _memory_snapshot()
uss = full.get("uss_bytes")
if uss is not None:
self.peak_uss = max(self.peak_uss or 0, int(uss))
return {
"samples": int(self.samples),
"peak_rss_bytes": int(self.peak_rss),
"peak_private_bytes": self.peak_private,
"peak_uss_bytes": self.peak_uss,
"uss_peak_scope": "monitor_start_end_only",
"minimum_system_available_bytes": (
None
if self.minimum_system_available == 1 << 63
else int(self.minimum_system_available)
),
"minimum_cuda_free_bytes": (
None
if self.minimum_cuda_free == 1 << 63
else int(self.minimum_cuda_free)
),
}
class _ExecutionServer:
def __init__(self):
self.client_id = None
self.last_node_id = None
self.messages: list[tuple[str, dict[str, Any], Any]] = []
def send_sync(self, event, data, client_id):
self.messages.append((str(event), dict(data), client_id))
def _find_nodes(prompt: Mapping[str, Any], class_type: str) -> list[str]:
return [
str(node_id)
for node_id, node in prompt.items()
if str(node.get("class_type")) == class_type
]
def _one_node(prompt: Mapping[str, Any], class_type: str) -> str:
values = _find_nodes(prompt, class_type)
if len(values) != 1:
raise ProbeFailure(
f"expected exactly one {class_type} node, found {len(values)}"
)
return values[0]
def _next_node_id(prompt: Mapping[str, Any]) -> str:
numeric = [int(value) for value in prompt if str(value).isdigit()]
return str((max(numeric) if numeric else 0) + 1)
def _sol_inputs(model_link: list[Any]) -> dict[str, Any]:
return {
"model": list(model_link),
"tau": 1.3,
"start_percent": 0.2,
"end_percent": 0.9,
"min_tokens": 4096,
"int8_qk": True,
"sink_conditioning": "exact_kv_and_rows",
"morton": False,
"morton_curve": "2d_frame",
"int8_pv": True,
"verbose": True,
"use_tma": False,
"dense_blocks": "",
}
def configure_profile(prompt: Mapping[str, Any], profile: str) -> dict[str, Any]:
"""Return a copied prompt with one explicit attention/Spectrum route."""
if profile not in PROFILE_NAMES:
raise ValueError(f"unknown profile: {profile!r}")
result = copy.deepcopy(dict(prompt))
sage = _one_node(result, "MiniMaxH3MemoryEfficientSageAttentionPatch")
spectrum = _one_node(result, "SpectrumApplyMiniMaxH3")
if not isinstance(result[sage].get("inputs", {}).get("model"), list):
raise ProbeFailure("Sage node has no MODEL link")
spectrum_inputs = result[spectrum].setdefault("inputs", {})
upstream = spectrum_inputs.get("model")
if not isinstance(upstream, list) or len(upstream) != 2:
raise ProbeFailure("Spectrum node has no MODEL link")
use_sol = profile in {"sage_sol", "sage_sol_spectrum"}
use_spectrum = profile in {"sage_spectrum", "sage_sol_spectrum"}
if use_sol:
sol_id = _next_node_id(result)
result[sol_id] = {
"class_type": "SolAttnPatch",
"inputs": _sol_inputs(list(upstream)),
"_meta": {"title": "Patch Sol-Attn"},
}
spectrum_inputs["model"] = [sol_id, 0]
spectrum_inputs["enabled"] = bool(use_spectrum)
return result
def mutate_run(
prompt: Mapping[str, Any],
*,
seed: int,
filename_prefix: str,
) -> dict[str, Any]:
result = copy.deepcopy(dict(prompt))
sampler_nodes = _find_nodes(result, "H3ContinuumSamplerV35")
if len(sampler_nodes) != 1:
raise ProbeFailure(
f"expected one H3ContinuumSamplerV35, found {len(sampler_nodes)}"
)
result[sampler_nodes[0]].setdefault("inputs", {})["base_seed"] = int(seed)
save_nodes = _find_nodes(result, "SaveVideo")
if not save_nodes:
save_nodes = _find_nodes(result, "VHS_VideoCombine")
for node_id in save_nodes:
inputs = result[node_id].setdefault("inputs", {})
if "filename_prefix" in inputs:
inputs["filename_prefix"] = str(filename_prefix)
return result
def _route(prompt: Mapping[str, Any], profile: str) -> list[str]:
route = ["MiniMaxH3MemoryEfficientSageAttentionPatch"]
if profile in {"sage_sol", "sage_sol_spectrum"}:
route.append("SolAttnPatch")
if profile in {"sage_spectrum", "sage_sol_spectrum"}:
route.append("SpectrumApplyMiniMaxH3")
return route
def _execute_outputs(prompt: Mapping[str, Any]) -> list[str]:
values = _find_nodes(prompt, "SaveVideo") + _find_nodes(
prompt, "VHS_VideoCombine"
)
if not values:
raise ProbeFailure("prompt has no SaveVideo/VHS output node")
return values
def _median_or_none(values: list[float | int | None]) -> float | None:
clean = [float(value) for value in values if value is not None]
return None if not clean else float(statistics.median(clean))
def summarize_runs(runs: list[Mapping[str, Any]]) -> dict[str, Any]:
phases = sorted(
{
phase
for run in runs
for phase in run.get("phase_totals_seconds", {}).keys()
}
)
return {
"run_count": len(runs),
"median_elapsed_seconds": _median_or_none(
[run.get("elapsed_seconds") for run in runs]
),
"median_phase_seconds": {
phase: _median_or_none(
[run.get("phase_totals_seconds", {}).get(phase) for run in runs]
)
for phase in phases
},
"median_peak_rss_bytes": _median_or_none(
[run.get("resources", {}).get("peak_rss_bytes") for run in runs]
),
"median_peak_private_bytes": _median_or_none(
[run.get("resources", {}).get("peak_private_bytes") for run in runs]
),
"median_peak_uss_bytes": _median_or_none(
[run.get("resources", {}).get("peak_uss_bytes") for run in runs]
),
"median_cuda_global_peak_allocated_bytes": _median_or_none(
[run.get("cuda_global_peak_allocated_bytes") for run in runs]
),
"median_minimum_cuda_free_bytes": _median_or_none(
[run.get("resources", {}).get("minimum_cuda_free_bytes") for run in runs]
),
}
def _install_comfy_paths(comfy_root: Path) -> None:
values = (
comfy_root,
comfy_root / "custom_nodes" / "comfyui-videohelpersuite",
)
for path in values:
text = str(path)
if text not in sys.path:
sys.path.insert(0, text)
def _initialize_comfy(comfy_root: Path):
_install_comfy_paths(comfy_root)
os.chdir(comfy_root)
from comfy.cli_args import args as comfy_args
# Load only the custom-node packages present in the accepted workflows.
# In particular, do not start ComfyUI-Manager's network/cache workers in a
# short-lived diagnostic process.
comfy_args.disable_all_custom_nodes = True
comfy_args.whitelist_custom_nodes = [
"ComfyUI-H3-Continuum",
"ComfyUI-Spectrum-MiniMax-H3",
"ComfyUI-SolAttn_triton",
"comfyui-kjnodes",
"rgthree-comfy",
]
import folder_paths
import nodes
import server
loop = asyncio.new_event_loop()
prompt_server = server.PromptServer(loop)
loop.run_until_complete(nodes.init_extra_nodes())
return folder_paths, nodes, prompt_server, loop
class NodeProfiler:
def __init__(
self,
execution_module,
prompt: Mapping[str, Any],
mode: str,
monitor_interval: float,
):
self.execution = execution_module
self.prompt = prompt
self.mode = mode
self.monitor_interval = float(monitor_interval)
self.records: list[dict[str, Any]] = []
self._original = None
def install(self) -> None:
if self._original is not None:
raise RuntimeError("profiler is already installed")
self._original = self.execution.get_output_data
original = self._original
async def profiled_get_output_data(
prompt_id,
unique_id,
obj,
input_data_all,
execution_block_cb=None,
pre_execute_cb=None,
v3_data=None,
):
node_id = str(unique_id)
class_type = str(
self.prompt.get(node_id, {}).get(
"class_type", type(obj).__name__
)
)
if torch.cuda.is_available():
torch.cuda.synchronize()
if self.mode == "interval":
torch.cuda.reset_peak_memory_stats()
memory_start = _memory_snapshot()
cuda_start_allocated = (
int(torch.cuda.memory_allocated()) if torch.cuda.is_available() else None
)
cuda_start_reserved = (
int(torch.cuda.memory_reserved()) if torch.cuda.is_available() else None
)
monitor = ResourceMonitor(self.monitor_interval)
monitor.start()
started = time.perf_counter()
try:
result = await original(
prompt_id,
unique_id,
obj,
input_data_all,
execution_block_cb=execution_block_cb,
pre_execute_cb=pre_execute_cb,
v3_data=v3_data,
)
if torch.cuda.is_available():
torch.cuda.synchronize()
return result
finally:
elapsed = time.perf_counter() - started
resources = monitor.stop()
memory_end = _memory_snapshot()
self.records.append(
{
"node_id": node_id,
"class_type": class_type,
"phase": _phase_for_class(class_type),
"elapsed_seconds": float(elapsed),
"memory_start": memory_start,
"memory_end": memory_end,
"resources": resources,
"cuda_start_allocated_bytes": cuda_start_allocated,
"cuda_start_reserved_bytes": cuda_start_reserved,
"cuda_end_allocated_bytes": (
int(torch.cuda.memory_allocated())
if torch.cuda.is_available()
else None
),
"cuda_end_reserved_bytes": (
int(torch.cuda.memory_reserved())
if torch.cuda.is_available()
else None
),
"cuda_interval_peak_allocated_bytes": (
int(torch.cuda.max_memory_allocated())
if torch.cuda.is_available() and self.mode == "interval"
else None
),
"cuda_synchronized": bool(torch.cuda.is_available()),
}
)
self.execution.get_output_data = profiled_get_output_data
def uninstall(self) -> None:
if self._original is not None:
self.execution.get_output_data = self._original
self._original = None
def _phase_totals(records: list[Mapping[str, Any]]) -> dict[str, float]:
result: dict[str, float] = {}
for item in records:
phase = str(item.get("phase", "other"))
result[phase] = result.get(phase, 0.0) + float(
item.get("elapsed_seconds", 0.0)
)
return result
def _cached_nodes(executor) -> list[str]:
for event, data in executor.status_messages:
if event == "execution_cached":
return sorted(str(value) for value in data.get("nodes", []))
return []
def _run_once(
*,
executor,
execution_module,
prompt: Mapping[str, Any],
prompt_id: str,
mode: str,
monitor_interval: float,
measured: bool,
) -> dict[str, Any]:
if not torch.cuda.is_available():
raise ProbeFailure("Phase 3-2 GPU profiling requires CUDA")
profiler = NodeProfiler(
execution_module,
prompt,
mode=mode,
monitor_interval=monitor_interval,
)
if measured:
profiler.install()
torch.cuda.synchronize()
if mode == "global":
torch.cuda.reset_peak_memory_stats()
run_monitor = ResourceMonitor(monitor_interval)
run_monitor.start()
started = time.perf_counter()
try:
executor.execute(
dict(prompt),
prompt_id,
extra_data={},
execute_outputs=_execute_outputs(prompt),
)
torch.cuda.synchronize()
finally:
elapsed = time.perf_counter() - started
resources = run_monitor.stop()
profiler.uninstall()
if not executor.success:
errors = [
data
for event, data in executor.status_messages
if event == "execution_error"
]
raise ProbeFailure(f"PromptExecutor failed: {errors[-1:]}")
records = list(profiler.records)
return {
"prompt_id": prompt_id,
"measured": bool(measured),
"elapsed_seconds": float(elapsed),
"phase_totals_seconds": _phase_totals(records),
"nodes": records,
"cached_nodes": _cached_nodes(executor),
"resources": resources,
"cuda_global_peak_allocated_bytes": (
int(torch.cuda.max_memory_allocated()) if mode == "global" else None
),
"cuda_peak_scope": (
"whole_workflow" if mode == "global" else "per_node_only"
),
}
def _validate_classes(nodes_module, prompt: Mapping[str, Any]) -> None:
missing = sorted(
{
str(node.get("class_type"))
for node in prompt.values()
if str(node.get("class_type")) not in nodes_module.NODE_CLASS_MAPPINGS
}
)
if missing:
raise ProbeFailure(f"installed ComfyUI is missing prompt nodes: {missing}")
def run(args) -> dict[str, Any]:
prompt_source = json.loads(args.prompt.read_text(encoding="utf-8"))
configured = configure_profile(prompt_source, args.profile)
folder_paths, nodes_module, _prompt_server, loop = _initialize_comfy(
args.comfy_root
)
_validate_classes(nodes_module, configured)
if args.validate_only:
loop.close()
return {
"status": "PASS",
"validate_only": True,
"profile": args.profile,
"route": _route(configured, args.profile),
"node_count": len(configured),
}
from execution import CacheType, PromptExecutor
import execution as execution_module
work_root = args.work_root.resolve()
output_root = work_root / "output"
temp_root = work_root / "temp"
output_root.mkdir(parents=True, exist_ok=True)
temp_root.mkdir(parents=True, exist_ok=True)
folder_paths.set_output_directory(str(output_root))
folder_paths.set_temp_directory(str(temp_root))
executor = PromptExecutor(
_ExecutionServer(),
cache_type=CacheType.CLASSIC,
cache_args={"ram": float(args.cache_ram_gib), "ram_inactive": 0.0},
)
base_seed = int(args.seed)
warm_prompt = mutate_run(
configured,
seed=base_seed,
filename_prefix=f"p32/{args.case}_{args.profile}_warmup",
)
warmup = _run_once(
executor=executor,
execution_module=execution_module,
prompt=warm_prompt,
prompt_id=f"p32-{args.case}-{args.profile}-warmup",
mode=args.mode,
monitor_interval=args.monitor_interval,
measured=False,
)
measured_runs = []
for index in range(int(args.runs)):
prompt = mutate_run(
configured,
seed=base_seed + index + 1,
filename_prefix=f"p32/{args.case}_{args.profile}_{args.mode}_{index + 1}",
)
measured_runs.append(
_run_once(
executor=executor,
execution_module=execution_module,
prompt=prompt,
prompt_id=f"p32-{args.case}-{args.profile}-{args.mode}-{index + 1}",
mode=args.mode,
monitor_interval=args.monitor_interval,
measured=True,
)
)
result = {
"status": "PASS",
"format": "h3-continuum-phase3-2-profile-v1",
"case": args.case,
"profile": args.profile,
"route": _route(configured, args.profile),
"mode": args.mode,
"cuda_synchronize_before_after_nodes": True,
"warmup_excluded": True,
"warmup": warmup,
"runs": measured_runs,
"summary": summarize_runs(measured_runs),
"prompt": str(args.prompt.resolve()),
"work_root": str(work_root),
}
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(
json.dumps(result, indent=2, ensure_ascii=False),
encoding="utf-8",
)
loop.close()
return result
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--comfy-root", type=Path, required=True)
parser.add_argument("--prompt", type=Path, required=True)
parser.add_argument("--output", type=Path, required=True)
parser.add_argument("--work-root", type=Path, required=True)
parser.add_argument("--case", required=True)
parser.add_argument("--profile", choices=PROFILE_NAMES, required=True)
parser.add_argument("--mode", choices=MEASUREMENT_MODES, default="global")
parser.add_argument("--runs", type=int, default=3)
parser.add_argument("--seed", type=int, default=32000)
parser.add_argument("--cache-ram-gib", type=float, default=64.0)
parser.add_argument("--monitor-interval", type=float, default=0.05)
parser.add_argument("--validate-only", action="store_true")
return parser
def main() -> int:
args = _parser().parse_args()
if args.runs < 1:
raise SystemExit("--runs must be at least 1")
result = run(args)
print(json.dumps(result, ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+300
View File
@@ -0,0 +1,300 @@
"""Launch the installed ComfyUI API with Phase 3-2 diagnostics enabled.
The wrapper imports the normal ComfyUI ``main.py``, installs an in-process
measurement hook, and then starts the unmodified API server. No custom node
or Production file is installed. Every completed prompt appends one JSON
record to ``--profile-output``.
"""
from __future__ import annotations
import argparse
import asyncio
import json
import os
from pathlib import Path
import sys
import threading
import time
from typing import Any
def _bootstrap_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("--comfy-root", type=Path, required=True)
parser.add_argument("--profile-output", type=Path, required=True)
parser.add_argument(
"--profile-mode", choices=("global", "interval"), default="global"
)
parser.add_argument("--profile-monitor-interval", type=float, default=0.05)
return parser
BOOTSTRAP, COMFY_ARGS = _bootstrap_parser().parse_known_args()
COMFY_ROOT = BOOTSTRAP.comfy_root.resolve()
PROFILE_OUTPUT = BOOTSTRAP.profile_output.resolve()
PROFILE_MODE = str(BOOTSTRAP.profile_mode)
MONITOR_INTERVAL = float(BOOTSTRAP.profile_monitor_interval)
sys.argv = [str(COMFY_ROOT / "main.py"), *COMFY_ARGS]
sys.path.insert(0, str(COMFY_ROOT))
os.chdir(COMFY_ROOT)
import torch # noqa: E402
import main as comfy_main # noqa: E402
import execution # noqa: E402
from p32_gpu_profile_probe import ( # noqa: E402
ResourceMonitor,
_memory_snapshot,
_phase_for_class,
_phase_totals,
)
_WRITE_LOCK = threading.Lock()
_CONTEXT_LOCK = threading.Lock()
_CONTEXTS: dict[str, dict[str, Any]] = {}
def _class_type(prompt: dict[str, Any], node_id: str, obj: Any) -> str:
configured = prompt.get(str(node_id), {}).get("class_type")
if configured:
return str(configured)
if isinstance(obj, type):
return str(obj.__name__)
return str(type(obj).__name__)
def _write_record(record: dict[str, Any]) -> None:
PROFILE_OUTPUT.parent.mkdir(parents=True, exist_ok=True)
line = json.dumps(record, ensure_ascii=False, separators=(",", ":"))
with _WRITE_LOCK:
with PROFILE_OUTPUT.open("a", encoding="utf-8") as handle:
handle.write(line + "\n")
handle.flush()
def _install_profiler() -> None:
original_get_output_data = execution.get_output_data
original_execute_async = execution.PromptExecutor.execute_async
async def profiled_get_output_data(
prompt_id,
unique_id,
obj,
input_data_all,
execution_block_cb=None,
pre_execute_cb=None,
v3_data=None,
):
with _CONTEXT_LOCK:
context = _CONTEXTS.get(str(prompt_id))
if context is None:
return await original_get_output_data(
prompt_id,
unique_id,
obj,
input_data_all,
execution_block_cb=execution_block_cb,
pre_execute_cb=pre_execute_cb,
v3_data=v3_data,
)
node_id = str(unique_id)
class_type = _class_type(context["prompt"], node_id, obj)
if torch.cuda.is_available():
torch.cuda.synchronize()
if PROFILE_MODE == "interval":
torch.cuda.reset_peak_memory_stats()
memory_start = _memory_snapshot()
cuda_start_allocated = (
int(torch.cuda.memory_allocated()) if torch.cuda.is_available() else None
)
cuda_start_reserved = (
int(torch.cuda.memory_reserved()) if torch.cuda.is_available() else None
)
monitor = ResourceMonitor(MONITOR_INTERVAL)
monitor.start()
started = time.perf_counter()
error = None
try:
return await original_get_output_data(
prompt_id,
unique_id,
obj,
input_data_all,
execution_block_cb=execution_block_cb,
pre_execute_cb=pre_execute_cb,
v3_data=v3_data,
)
except BaseException as exc:
error = f"{type(exc).__name__}: {exc}"
raise
finally:
try:
if torch.cuda.is_available():
torch.cuda.synchronize()
finally:
elapsed = time.perf_counter() - started
resources = monitor.stop()
record = {
"node_id": node_id,
"class_type": class_type,
"phase": _phase_for_class(class_type),
"elapsed_seconds": float(elapsed),
"memory_start": memory_start,
"memory_end": _memory_snapshot(),
"resources": resources,
"cuda_start_allocated_bytes": cuda_start_allocated,
"cuda_start_reserved_bytes": cuda_start_reserved,
"cuda_end_allocated_bytes": (
int(torch.cuda.memory_allocated())
if torch.cuda.is_available()
else None
),
"cuda_end_reserved_bytes": (
int(torch.cuda.memory_reserved())
if torch.cuda.is_available()
else None
),
"cuda_interval_peak_allocated_bytes": (
int(torch.cuda.max_memory_allocated())
if torch.cuda.is_available() and PROFILE_MODE == "interval"
else None
),
"cuda_synchronized": bool(torch.cuda.is_available()),
"error": error,
}
with _CONTEXT_LOCK:
active = _CONTEXTS.get(str(prompt_id))
if active is not None:
active["nodes"].append(record)
async def profiled_execute_async(
self,
prompt,
prompt_id,
extra_data={},
execute_outputs=[],
):
prompt_key = str(prompt_id)
context: dict[str, Any] = {
"prompt": dict(prompt),
"nodes": [],
"started_at_unix": time.time(),
}
with _CONTEXT_LOCK:
_CONTEXTS[prompt_key] = context
if torch.cuda.is_available():
torch.cuda.synchronize()
if PROFILE_MODE == "global":
torch.cuda.reset_peak_memory_stats()
monitor = ResourceMonitor(MONITOR_INTERVAL)
monitor.start()
started = time.perf_counter()
error = None
try:
return await original_execute_async(
self,
prompt,
prompt_id,
extra_data=extra_data,
execute_outputs=execute_outputs,
)
except BaseException as exc:
error = f"{type(exc).__name__}: {exc}"
raise
finally:
try:
if torch.cuda.is_available():
torch.cuda.synchronize()
finally:
elapsed = time.perf_counter() - started
resources = monitor.stop()
with _CONTEXT_LOCK:
completed = _CONTEXTS.pop(prompt_key, context)
nodes = list(completed["nodes"])
class_types = [str(item["class_type"]) for item in nodes]
spectrum_nodes = [
node
for node in prompt.values()
if str(node.get("class_type")) == "SpectrumApplyMiniMaxH3"
]
spectrum_enabled = any(
bool(node.get("inputs", {}).get("enabled", False))
for node in spectrum_nodes
)
configured_class_types = {
str(node.get("class_type")) for node in prompt.values()
}
_write_record(
{
"format": "h3-continuum-phase3-2-api-profile-v1",
"prompt_id": prompt_key,
"mode": PROFILE_MODE,
"started_at_unix": completed["started_at_unix"],
"elapsed_seconds": float(elapsed),
"phase_totals_seconds": _phase_totals(nodes),
"nodes": nodes,
"resources": resources,
"cuda_global_peak_allocated_bytes": (
int(torch.cuda.max_memory_allocated())
if torch.cuda.is_available() and PROFILE_MODE == "global"
else None
),
"cuda_peak_scope": (
"whole_workflow"
if PROFILE_MODE == "global"
else "per_node_only"
),
"cuda_synchronize_before_after_nodes": bool(
torch.cuda.is_available()
),
"route_observed": {
"sage_node_configured": (
"MiniMaxH3MemoryEfficientSageAttentionPatch"
in configured_class_types
),
"sol_node_configured": "SolAttnPatch"
in configured_class_types,
"spectrum_node_configured": (
"SpectrumApplyMiniMaxH3" in configured_class_types
),
"sage_node_executed": (
"MiniMaxH3MemoryEfficientSageAttentionPatch"
in class_types
),
"sol_node_executed": "SolAttnPatch" in class_types,
"spectrum_node_executed": (
"SpectrumApplyMiniMaxH3" in class_types
),
"spectrum_enabled": bool(spectrum_enabled),
},
"success": bool(getattr(self, "success", False)),
"error": error,
}
)
execution.get_output_data = profiled_get_output_data
execution.PromptExecutor.execute_async = profiled_execute_async
def main() -> int:
_install_profiler()
loop, _server, start_all = comfy_main.start_comfyui()
try:
loop.run_until_complete(start_all())
except KeyboardInterrupt:
pass
finally:
try:
comfy_main.asset_seeder.shutdown()
except Exception:
pass
comfy_main.cleanup_temp()
return 0
if __name__ == "__main__":
raise SystemExit(main())
+37
View File
@@ -0,0 +1,37 @@
param(
[string]$Source = (Split-Path -Parent $PSScriptRoot),
[string]$SnapshotRoot = 'D:\Codex\_snapshots\ComfyUI-H3-Continuum',
[string]$Label = 'pre-change'
)
$ErrorActionPreference = 'Stop'
$sourcePath = (Resolve-Path -LiteralPath $Source).Path
$stamp = Get-Date -Format 'yyyyMMdd_HHmmss'
$safeLabel = $Label -replace '[^A-Za-z0-9._-]', '-'
$destination = Join-Path $SnapshotRoot "$safeLabel-$stamp"
$rootFull = [IO.Path]::GetFullPath($SnapshotRoot)
$destinationFull = [IO.Path]::GetFullPath($destination)
if (-not $destinationFull.StartsWith($rootFull, [StringComparison]::OrdinalIgnoreCase)) {
throw 'Snapshot destination escaped the configured snapshot root.'
}
New-Item -ItemType Directory -Path $destination -Force | Out-Null
Get-ChildItem -LiteralPath $sourcePath -Force |
Where-Object { $_.Name -ne '.git' } |
ForEach-Object { Copy-Item -LiteralPath $_.FullName -Destination $destination -Recurse -Force }
$sourceCount = (Get-ChildItem -LiteralPath $sourcePath -Recurse -Force -File |
Where-Object { $_.FullName -notmatch '[\\/]\.git[\\/]' } |
Measure-Object).Count
$destinationCount = (Get-ChildItem -LiteralPath $destination -Recurse -Force -File | Measure-Object).Count
if ($sourceCount -ne $destinationCount) {
throw "Snapshot verification failed: source=$sourceCount destination=$destinationCount"
}
[pscustomobject]@{
Source = $sourcePath
Snapshot = $destination
Files = $destinationCount
}
+437
View File
@@ -0,0 +1,437 @@
"""Run the private V3.6 FL2VA 3x5 Terminal Merge combined GPU gate."""
from __future__ import annotations
import argparse
import copy
import json
from pathlib import Path
import sys
import time
import uuid
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 p32_api_runner import submit_and_wait
from v36_r1_stress_gate_runner import ResourceMonitor, _extract_evidence
TIMELINE_PROMPT = """[0-5s]
A continuous cinematic live-action tracking shot begins from the supplied First Frame. The woman in the orange jacket rides her skateboard forward along the waterfront promenade while the low camera tracks beside her. Natural rolling skateboard wheels, wind, and distant city traffic.
[5-10s]
She accelerates through smooth continuous S-turns without stopping. Her hair and jacket react to speed while the camera maintains the same forward direction with a steady pan and partial orbit. Preserve her face, orange jacket, white trousers, skateboard, waterfront, and natural contrast.
[10-15s]
Still moving forward, she approaches the blue waterfront sculpture, gradually eases her speed, turns toward the camera, and converges naturally to the supplied Last Frame pose and composition. Continuous city ambience and skateboard sound."""
CONDITIONS = {
"a1_reference_video": ("reference_context_v1", False),
"a2_masked_video": ("masked_video_prefix_v1", False),
"b1_reference_av": ("reference_context_v1", True),
"b2_masked_av": ("masked_av_prefix_22_v1", True),
}
def configure_prompt(
source: Mapping[str, Any],
*,
transport: str,
audio_continuity: bool,
output_prefix: str,
seed: int,
diagnostic_nonce: int,
steps: int = 20,
audio_seam: str = "Auto",
) -> dict[str, Any]:
prompt = copy.deepcopy(dict(source))
prompt["290"] = {
"class_type": "LoadImage",
"_meta": {"title": "Last Frame"},
"inputs": {"image": "exec-758fca62-818a-446e-a279-829cc126d899.png"},
}
prompt["292"] = {
"class_type": "ImageScaleToTotalPixels",
"_meta": {"title": "Scale Last Frame"},
"inputs": {
"upscale_method": "area",
"megapixels": ["297", 0],
"resolution_steps": 32,
"image": ["290", 0],
},
}
sampler = prompt["305"]
sampler["class_type"] = "H3ContinuumMaskedPrefixR1Diagnostic"
sampler["_meta"] = {"title": "V3.6 FL2VA Combined Gate Diagnostic"}
sampler["inputs"].update(
{
"prompt_mode": "Timeline — [0-5s] sections",
"chunks": 3,
"chunk_seconds": 5.0,
"width": 640,
"height": 640,
"continuity": "Balanced — 22 frames",
"base_seed": int(seed),
"audio_continuity": bool(audio_continuity),
"diagnostics": "Detailed Report",
"show_preview": False,
"run_storage": "Off",
"continuation_transport": str(transport),
"synchronize_sampling": True,
"diagnostic_nonce": int(diagnostic_nonce),
"last_frame": ["292", 0],
}
)
sampler["inputs"]["first_frame"] = ["119", 0]
prompt["188"]["inputs"]["value"] = TIMELINE_PROMPT
prompt["153"]["inputs"].update(
{"scheduler": "simple", "steps": int(steps), "denoise": 1.0}
)
prompt["306"]["inputs"].update(
{
"exact_total_duration": True,
"audio_seam": str(audio_seam),
"video_seam": "Off",
"buffer_backend": "Auto",
"diagnostics": "Detailed Report",
}
)
prompt["191"]["inputs"]["filename_prefix"] = str(output_prefix)
return prompt
def _validate_diagnostic(
diagnostic: Mapping[str, Any],
*,
transport: str,
audio_continuity: bool,
) -> None:
calls = list(diagnostic.get("sample_calls") or [])
if len(calls) != 2:
raise RuntimeError(f"expected 2 physical Sampling calls, got {len(calls)}")
if int(diagnostic.get("logical_chunk_count", 0)) != 3:
raise RuntimeError(f"logical chunk count mismatch: {diagnostic.get('logical_chunk_count')}")
if int(diagnostic.get("physical_group_count", 0)) != 2:
raise RuntimeError(f"physical group count mismatch: {diagnostic.get('physical_group_count')}")
if int(diagnostic.get("physical_decode_group_count", 0)) != 2:
raise RuntimeError("physical decode group count mismatch")
groups = list(diagnostic.get("decode_groups") or [])
if len(groups) != 2:
raise RuntimeError(f"expected 2 decode groups, got {len(groups)}")
terminal = groups[1]
expected = {
"logical_chunk_indices": [2, 3],
"terminal_merged": True,
"total_frames": 260,
"trim_frames": 22,
"expected_video_latent_t": 77,
"expected_audio_latent_t": 433,
}
for key, value in expected.items():
if terminal.get(key) != value:
raise RuntimeError(f"terminal contract mismatch {key}: {terminal.get(key)!r}")
first, second = calls
if first.get("minimax_keyframe_indices") != [0]:
raise RuntimeError(f"Group 1 First Frame contract mismatch: {first.get('minimax_keyframe_indices')}")
if second.get("minimax_keyframe_indices") != [259]:
raise RuntimeError(f"Terminal Last Frame contract mismatch: {second.get('minimax_keyframe_indices')}")
if int(second.get("input_video", {}).get("shape", [0, 0, 0])[2]) != 77:
raise RuntimeError("Terminal input Video T is not 77")
masked = transport != "reference_context_v1"
if masked:
if second.get("minimax_ref_kinds"):
raise RuntimeError(f"masked Terminal emitted refs: {second.get('minimax_ref_kinds')}")
if bool(second.get("continuum_interop_emitted")):
raise RuntimeError("masked Terminal emitted old Spectrum interop")
required = {
"source_tail_matches_target_prefix": True,
"video_mask_prefix_zero": True,
"video_mask_generated_one": True,
}
if transport == "masked_av_prefix_22_v1":
required.update(
{
"source_audio_tail_matches_target_prefix": True,
"audio_mask_prefix_zero": True,
"audio_mask_generated_one": True,
}
)
else:
required["audio_mask_all_one"] = True
for key, value in required.items():
if second.get(key) is not value:
raise RuntimeError(f"masked evidence failed {key}: {second.get(key)!r}")
pairs = list(diagnostic.get("final_prefix_pairs") or [])
if len(pairs) != 1:
raise RuntimeError(f"expected one final prefix pair, got {len(pairs)}")
pair = pairs[0]
if pair.get("bit_exact") is not True or float(pair.get("max_abs_diff", 1)) != 0:
raise RuntimeError(f"final Video prefix is not exact: {pair}")
if transport == "masked_av_prefix_22_v1":
if pair.get("audio_bit_exact") is not True or float(
pair.get("audio_max_abs_diff", 1)
) != 0:
raise RuntimeError(f"final Audio prefix is not exact: {pair}")
else:
kinds = list(second.get("minimax_ref_kinds") or [])
if "video" not in kinds and "video_audio" not in kinds:
raise RuntimeError(f"Reference Terminal lacks Video context: {kinds}")
if audio_continuity and "audio" not in kinds and "video_audio" not in kinds:
raise RuntimeError(f"Reference AV Terminal lacks Audio context: {kinds}")
if not bool(second.get("continuum_interop_emitted")):
raise RuntimeError("Reference Terminal lacks Continuum interop")
def _run_prompt(
*,
prompt: dict[str, Any],
run_root: Path,
args,
label: str,
require_diagnostic: bool,
) -> dict[str, Any]:
run_root.mkdir(parents=True, exist_ok=True)
prompt_path = run_root / "prompt.json"
prompt_path.write_text(json.dumps(prompt, indent=2, ensure_ascii=False), encoding="utf-8")
print(f"START {label}", flush=True)
with ResourceMonitor(args.backend_pid, args.monitor_interval) as monitor:
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=prompt,
client_id=f"v36-fl2va-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=args.poll_seconds,
)
history_path = run_root / "history.json"
history_path.write_text(json.dumps(history, indent=2, ensure_ascii=False), encoding="utf-8")
evidence = _extract_evidence(history)
result = {
"label": label,
"prompt_id": prompt_id,
"api_elapsed_seconds": float(elapsed),
"prompt": str(prompt_path),
"history": str(history_path),
"resources": monitor.as_dict(),
**evidence,
}
if require_diagnostic and not result.get("diagnostic"):
raise RuntimeError(f"{label} returned no V3.6 diagnostic evidence")
(run_root / "summary.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(f"DONE {label} elapsed={elapsed:.3f}s", flush=True)
return result
def _run_condition(
*,
source: Mapping[str, Any],
args,
label: str,
transport: str,
audio_continuity: bool,
seed: int,
nonce: int,
cache_only_off: bool,
) -> list[dict[str, Any]]:
output_prefix = f"video/V36_FL2VA_{label}_seed{seed}_Auto"
prompt = configure_prompt(
source,
transport=transport,
audio_continuity=audio_continuity,
output_prefix=output_prefix,
seed=seed,
diagnostic_nonce=nonce,
audio_seam="Auto",
)
result = _run_prompt(
prompt=prompt,
run_root=args.output_root / label,
args=args,
label=label,
require_diagnostic=True,
)
cache = result.get("prompt_clip_cache")
if not cache or int(cache.get("encode_calls", -1)) != 0:
raise RuntimeError(f"measured run was not Prompt/CLIP cache HIT: {cache}")
_validate_diagnostic(
result["diagnostic"],
transport=transport,
audio_continuity=audio_continuity,
)
outputs = [result]
if cache_only_off:
raw = copy.deepcopy(prompt)
raw["306"]["inputs"]["audio_seam"] = "Off"
raw["191"]["inputs"]["filename_prefix"] = (
f"video/V36_FL2VA_{label}_seed{seed}_SeamOff"
)
raw_result = _run_prompt(
prompt=raw,
run_root=args.output_root / f"{label}_seam_off_cache",
args=args,
label=f"{label}_seam_off_cache",
require_diagnostic=False,
)
raw_result["cache_only_expected"] = True
outputs.append(raw_result)
return outputs
def run(args) -> dict[str, Any]:
source = json.loads(args.prompt.read_text(encoding="utf-8"))
args.output_root.mkdir(parents=True, exist_ok=True)
nonce = int(time.time()) & 0x7FFFFFFF
runs: list[dict[str, Any]] = []
if not args.skip_warmup:
warm = configure_prompt(
source,
transport="reference_context_v1",
audio_continuity=False,
output_prefix="video/V36_FL2VA_cache_warmup",
seed=args.seeds[0],
diagnostic_nonce=nonce,
steps=1,
audio_seam="Off",
)
runs.append(
_run_prompt(
prompt=warm,
run_root=args.output_root / "cache_warmup",
args=args,
label="cache_warmup",
require_diagnostic=True,
)
)
schedule: list[tuple[int, str]]
if args.phase == "gate-a":
schedule = [(args.seeds[0], label) for label in CONDITIONS]
elif args.phase == "joint-multiseed":
schedule = [
(args.seeds[0], "b1_reference_av"),
(args.seeds[0], "b2_masked_av"),
(args.seeds[1], "b2_masked_av"),
(args.seeds[1], "b1_reference_av"),
(args.seeds[2], "b1_reference_av"),
(args.seeds[2], "b2_masked_av"),
]
else:
schedule = [
(args.seeds[1], "b1_reference_av"),
(args.seeds[1], "b2_masked_av"),
(args.seeds[2], "b2_masked_av"),
]
for index, (seed, condition) in enumerate(schedule, start=1):
nonce += 1
transport, audio_continuity = CONDITIONS[condition]
label = f"{index:02d}_seed{seed}_{condition}"
runs.extend(
_run_condition(
source=source,
args=args,
label=label,
transport=transport,
audio_continuity=audio_continuity,
seed=seed,
nonce=nonce,
cache_only_off=(
condition.startswith("b")
and (
args.phase == "gate-a"
or (args.phase == "r2-short" and seed == args.seeds[1])
)
),
)
)
measured = [run for run in runs if run.get("diagnostic") and run["label"] != "cache_warmup"]
first_video_hashes = {
run["diagnostic"]["physical_video_groups"][0]["sha256"] for run in measured
}
first_audio_hashes = {
run["diagnostic"]["physical_audio_groups"][0]["sha256"] for run in measured
}
first_seeds_by_requested: dict[int, set[int]] = {}
first_video_hashes_by_requested: dict[int, set[str]] = {}
first_audio_hashes_by_requested: dict[int, set[str]] = {}
for run in measured:
requested = int(run["label"].split("_seed", 1)[1].split("_", 1)[0])
actual = int(run["diagnostic"]["sample_calls"][0]["seed"])
first_seeds_by_requested.setdefault(requested, set()).add(actual)
first_video_hashes_by_requested.setdefault(requested, set()).add(
run["diagnostic"]["physical_video_groups"][0]["sha256"]
)
first_audio_hashes_by_requested.setdefault(requested, set()).add(
run["diagnostic"]["physical_audio_groups"][0]["sha256"]
)
if args.phase == "gate-a" and (len(first_video_hashes) != 1 or len(first_audio_hashes) != 1):
raise RuntimeError("Gate A physical Group 1 hashes differ across conditions")
if any(len(values) != 1 for values in first_seeds_by_requested.values()):
raise RuntimeError(
f"physical Group 1 seeds differ within a Base Seed: {first_seeds_by_requested}"
)
if any(len(values) != 1 for values in first_video_hashes_by_requested.values()):
raise RuntimeError(
"physical Group 1 Video hashes differ within a Base Seed: "
f"{first_video_hashes_by_requested}"
)
if any(len(values) != 1 for values in first_audio_hashes_by_requested.values()):
raise RuntimeError(
"physical Group 1 Audio hashes differ within a Base Seed: "
f"{first_audio_hashes_by_requested}"
)
result = {
"format": "h3-continuum-v36-fl2va-combined-gate-v1",
"phase": args.phase,
"server": args.server,
"seeds": [int(value) for value in args.seeds],
"conditions": CONDITIONS,
"group1_video_hash_count": len(first_video_hashes),
"group1_audio_hash_count": len(first_audio_hashes),
"runs": runs,
}
(args.output_root / "gate_summary.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
)
return result
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--server", default="http://127.0.0.1:8188")
parser.add_argument("--prompt", type=Path, required=True)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument(
"--phase",
choices=("gate-a", "joint-multiseed", "r2-short"),
default="gate-a",
)
parser.add_argument(
"--seeds",
type=int,
nargs=3,
default=(238985343135606, 238985343135607, 238985343135608),
)
parser.add_argument("--backend-pid", type=int)
parser.add_argument("--timeout-seconds", type=float, default=3600.0)
parser.add_argument("--poll-seconds", type=float, default=2.0)
parser.add_argument("--monitor-interval", type=float, default=1.0)
parser.add_argument("--skip-warmup", action="store_true")
return parser
def main() -> int:
result = run(_parser().parse_args())
print(json.dumps(result, ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+167
View File
@@ -0,0 +1,167 @@
"""Run V3.6 PIG-3 Reference Image + Audio with Masked AV continuation."""
from __future__ import annotations
import argparse
import copy
import json
from pathlib import Path
import sys
import time
import uuid
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
from p32_api_runner import submit_and_wait
from v36_r1_stress_gate_runner import _extract_evidence
DEFAULT_SOURCE = Path(
r"D:\Codex\_test_results\ComfyUI-H3-Continuum\v36-fl2va-combined-20260826"
r"\gate-a\04_seed238985343135606_b2_masked_av\prompt.json"
)
DEFAULT_OUTPUT = Path(
r"D:\Codex\_test_results\ComfyUI-H3-Continuum\v36-production-integration"
r"\pig3-reference-masked"
)
PROMPT = (
"A continuous cinematic shot of <Picture 1> walking forward with clear body "
"motion while the camera tracks smoothly. Preserve the person's identity, "
"clothing, colors, and environment. Use <Audio 1> as the persistent voice and "
"sound reference across both five-second chunks."
)
def configure(source: dict, *, seed: int, diagnostic_nonce: int) -> dict:
prompt = copy.deepcopy(source)
prompt["400"] = {
"class_type": "LoadAudio",
"_meta": {"title": "PIG-3 Reference Audio"},
"inputs": {"audio": "V36_PIG3_reference_audio.mp3"},
}
sampler = prompt["305"]
sampler["class_type"] = "H3ContinuumMaskedPrefixR1Diagnostic"
sampler["_meta"] = {"title": "V3.6 PIG-3 Reference + Masked AV"}
sampler["inputs"].update(
{
"prompt_mode": "Fixed",
"chunks": 2,
"chunk_seconds": 5.0,
"width": 384,
"height": 384,
"continuity": "Balanced — 22 frames",
"base_seed": int(seed),
"audio_continuity": True,
"diagnostics": "Detailed Report",
"reroll_from_chunk": "Auto",
"reroll_nonce": 0,
"show_preview": False,
"run_storage": "Off",
"run_name": "",
"project_id": "",
"reference_size": "Match Output",
"continuation_transport": "masked_av_prefix_22_v1",
"synchronize_sampling": True,
"diagnostic_nonce": int(diagnostic_nonce),
"reference_image_1": ["119", 0],
"reference_audio_1": ["400", 0],
"reference_audio_vae": ["136", 0],
}
)
sampler["inputs"].pop("last_frame", None)
prompt["188"]["inputs"]["value"] = PROMPT
prompt["153"]["inputs"].update(
{"scheduler": "simple", "steps": 4, "denoise": 1.0}
)
prompt["306"]["inputs"].update(
{
"exact_total_duration": True,
"audio_seam": "Auto",
"video_seam": "Off",
"buffer_backend": "Auto",
"diagnostics": "Detailed Report",
}
)
prompt["191"]["inputs"]["filename_prefix"] = (
"video/V36_PIG3_Reference_Image_Audio_Masked_AV"
)
return prompt
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--server", default="http://127.0.0.1:8188")
parser.add_argument("--source", type=Path, default=DEFAULT_SOURCE)
parser.add_argument("--output", type=Path, default=DEFAULT_OUTPUT)
parser.add_argument("--seed", type=int, default=238985343135610)
parser.add_argument("--timeout-seconds", type=float, default=1800.0)
args = parser.parse_args()
args.output.mkdir(parents=True, exist_ok=True)
source = json.loads(args.source.read_text(encoding="utf-8"))
prompt = configure(source, seed=args.seed, diagnostic_nonce=9301)
(args.output / "prompt.json").write_text(
json.dumps(prompt, indent=2, ensure_ascii=False), encoding="utf-8"
)
started = time.perf_counter()
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=prompt,
client_id=f"v36-pig3-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=1.0,
)
wall = time.perf_counter() - started
(args.output / "history.json").write_text(
json.dumps(history, indent=2, ensure_ascii=False), encoding="utf-8"
)
evidence = _extract_evidence(history)
diagnostic = evidence.get("diagnostic") or {}
calls = list(diagnostic.get("sample_calls") or [])
if len(calls) != 2:
raise RuntimeError(f"expected two physical groups, got {len(calls)}")
for index, call in enumerate(calls, start=1):
kinds = list(call.get("minimax_ref_kinds") or [])
if kinds.count("image") != 1 or kinds.count("audio") != 1:
raise RuntimeError(f"group {index} Reference kinds mismatch: {kinds}")
if "video" in kinds or "video_audio" in kinds:
raise RuntimeError(f"group {index} emitted old Continuation refs: {kinds}")
continuation = calls[1]
for key in (
"source_tail_matches_target_prefix",
"source_audio_tail_matches_target_prefix",
"video_mask_prefix_zero",
"video_mask_generated_one",
"audio_mask_prefix_zero",
"audio_mask_generated_one",
):
if continuation.get(key) is not True:
raise RuntimeError(f"continuation evidence failed {key}")
pairs = list(diagnostic.get("final_prefix_pairs") or [])
if len(pairs) != 1:
raise RuntimeError(f"expected one finalized prefix pair, got {len(pairs)}")
pair = pairs[0]
if pair.get("bit_exact") is not True or pair.get("audio_bit_exact") is not True:
raise RuntimeError(f"final protected prefixes are not bit-exact: {pair}")
result = {
"gate": "V3.6 PIG-3 Reference + Masked AV",
"status": "PASS",
"prompt_id": prompt_id,
"api_elapsed_seconds": float(elapsed),
"wall_seconds": float(wall),
"seed": int(args.seed),
"prompt": PROMPT,
**evidence,
}
(args.output / "gate_summary.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(json.dumps({"status": "PASS", "output": str(args.output)}))
return 0
if __name__ == "__main__":
raise SystemExit(main())
@@ -0,0 +1,273 @@
"""Run the private V3.6 Production Integration Run Storage GPU gate."""
from __future__ import annotations
import argparse
import copy
import json
from pathlib import Path
import re
import sys
import time
import uuid
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 p32_api_runner import submit_and_wait
from v36_r1_stress_gate_runner import _extract_evidence, _walk_strings
DEFAULT_SOURCE = Path(
r"D:\Codex\_test_results\ComfyUI-H3-Continuum\v36-fl2va-combined-20260826"
r"\gate-a\04_seed238985343135606_b2_masked_av\prompt.json"
)
DEFAULT_OUTPUT_ROOT = Path(
r"D:\Codex\_test_results\ComfyUI-H3-Continuum\v36-production-integration"
)
RUN_STORAGE_ROOT = Path(
r"D:\output\video\comfy_video\h3_continuum\runs"
)
def _run_storage_lines(history: Mapping[str, Any]) -> list[str]:
lines: list[str] = []
for text in _walk_strings(history):
lines.extend(
line for line in text.splitlines() if line.startswith("Run Storage:")
)
return list(dict.fromkeys(lines))
def _summary_counts(lines: list[str]) -> tuple[int, int, str]:
if not lines:
raise RuntimeError("Run Storage summary was not returned")
line = lines[-1]
match = re.search(
r"revision ([0-9a-f]+); (\d+) reused, (\d+) generated", line
)
if not match:
raise RuntimeError(f"unrecognized Run Storage summary: {line}")
return int(match.group(2)), int(match.group(3)), match.group(1)
def configure_prompt(
source: Mapping[str, Any],
*,
run_name: str,
regenerate_from: str,
reroll_nonce: int,
diagnostic_nonce: int,
output_prefix: str,
) -> dict[str, Any]:
prompt = copy.deepcopy(dict(source))
sampler = prompt["305"]
sampler["class_type"] = "H3ContinuumMaskedPrefixR1Diagnostic"
sampler["_meta"] = {"title": "V3.6 Production Integration Gate"}
sampler["inputs"].update(
{
"chunks": 3,
"chunk_seconds": 5.0,
"width": 384,
"height": 384,
"continuity": "Balanced — 22 frames",
"audio_continuity": True,
"diagnostics": "Detailed Report",
"reroll_from_chunk": str(regenerate_from),
"reroll_nonce": int(reroll_nonce),
"show_preview": False,
"run_storage": "Save + Auto Resume",
"run_name": str(run_name),
"project_id": "",
"continuation_transport": "masked_av_prefix_22_v1",
"synchronize_sampling": True,
"diagnostic_nonce": int(diagnostic_nonce),
}
)
prompt["153"]["inputs"].update(
{"scheduler": "simple", "steps": 1, "denoise": 1.0}
)
prompt["306"]["inputs"].update(
{
"exact_total_duration": True,
"audio_seam": "Auto",
"video_seam": "Off",
"buffer_backend": "Auto",
"diagnostics": "Detailed Report",
}
)
prompt["191"]["inputs"]["filename_prefix"] = str(output_prefix)
return prompt
def run_one(
*,
source: Mapping[str, Any],
server: str,
output_root: Path,
run_name: str,
label: str,
regenerate_from: str,
reroll_nonce: int,
diagnostic_nonce: int,
timeout_seconds: float,
) -> dict[str, Any]:
run_root = output_root / label
run_root.mkdir(parents=True, exist_ok=True)
prompt = configure_prompt(
source,
run_name=run_name,
regenerate_from=regenerate_from,
reroll_nonce=reroll_nonce,
diagnostic_nonce=diagnostic_nonce,
output_prefix=f"video/V36_PIG2_{run_name}_{label}",
)
(run_root / "prompt.json").write_text(
json.dumps(prompt, indent=2, ensure_ascii=False), encoding="utf-8"
)
started = time.perf_counter()
prompt_id, history, elapsed = submit_and_wait(
server=server,
prompt=prompt,
client_id=f"v36-pig2-{uuid.uuid4().hex}",
timeout_seconds=timeout_seconds,
poll_seconds=1.0,
)
wall = time.perf_counter() - started
(run_root / "history.json").write_text(
json.dumps(history, indent=2, ensure_ascii=False), encoding="utf-8"
)
evidence = _extract_evidence(history)
storage_lines = _run_storage_lines(history)
reused, generated, revision = _summary_counts(storage_lines)
result = {
"label": label,
"prompt_id": prompt_id,
"api_elapsed_seconds": float(elapsed),
"wall_seconds": float(wall),
"regenerate_from": regenerate_from,
"reroll_nonce": int(reroll_nonce),
"run_storage_lines": storage_lines,
"reused": reused,
"generated": generated,
"revision": revision,
**evidence,
}
(run_root / "summary.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
)
return result
def _manifest_records(run_name: str) -> list[dict[str, Any]]:
run_root = RUN_STORAGE_ROOT / run_name
records: list[dict[str, Any]] = []
for path in sorted(run_root.glob("revisions/*/manifest.json")):
payload = json.loads(path.read_text(encoding="utf-8"))
records.append({"path": str(path), "manifest": payload})
return records
def validate(results: list[dict[str, Any]], manifests: list[dict[str, Any]]) -> None:
expected_counts = [(0, 3, 2), (3, 0, 0), (1, 2, 1), (1, 2, 1)]
for result, (reused, generated, sample_calls) in zip(
results, expected_counts, strict=True
):
if (result["reused"], result["generated"]) != (reused, generated):
raise RuntimeError(
f"{result['label']} storage counts mismatch: "
f"{result['reused']}/{result['generated']}"
)
diagnostic = result.get("diagnostic") or {}
calls = list(diagnostic.get("sample_calls") or [])
if len(calls) != sample_calls:
raise RuntimeError(
f"{result['label']} expected {sample_calls} Sampling calls, got {len(calls)}"
)
if sample_calls == 1:
shape = list(calls[0].get("input_video", {}).get("shape") or [])
audio_shape = list(calls[0].get("input_audio", {}).get("shape") or [])
if len(shape) < 3 or shape[2] != 77:
raise RuntimeError(f"{result['label']} did not regenerate Terminal 77T")
if not audio_shape or audio_shape[-1] != 433:
raise RuntimeError(f"{result['label']} did not regenerate Terminal Audio 433T")
if results[0]["revision"] != results[1]["revision"]:
raise RuntimeError("Auto Resume did not select the saved masked revision")
if not manifests:
raise RuntimeError("Run Storage created no revision manifests")
for record in manifests:
manifest = record["manifest"]
semantics = (
(manifest.get("contract") or {}).get("global", {}).get(
"execution_semantics", {}
)
)
if semantics.get("continuation_transport") != "masked_av_prefix_22_v1":
raise RuntimeError(
f"manifest lacks masked transport identity: {record['path']}"
)
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--server", default="http://127.0.0.1:8188")
parser.add_argument("--source", type=Path, default=DEFAULT_SOURCE)
parser.add_argument("--output-root", type=Path, default=DEFAULT_OUTPUT_ROOT)
parser.add_argument("--timeout-seconds", type=float, default=1800.0)
parser.add_argument("--run-name", default="")
args = parser.parse_args()
stamp = time.strftime("%Y%m%d_%H%M%S")
run_name = args.run_name or f"v36_pig2_masked_{stamp}"
output_root = args.output_root / run_name
output_root.mkdir(parents=True, exist_ok=True)
source = json.loads(args.source.read_text(encoding="utf-8"))
specifications = (
("01_initial_save", "Auto", 0, 9101),
("02_auto_resume", "Auto", 0, 9102),
("03_regenerate_chunk2", "Chunk 2", 1, 9103),
("04_regenerate_chunk3", "Chunk 3", 2, 9104),
)
results = []
for label, regenerate_from, reroll_nonce, diagnostic_nonce in specifications:
print(f"START {label}", flush=True)
result = run_one(
source=source,
server=args.server,
output_root=output_root,
run_name=run_name,
label=label,
regenerate_from=regenerate_from,
reroll_nonce=reroll_nonce,
diagnostic_nonce=diagnostic_nonce,
timeout_seconds=args.timeout_seconds,
)
results.append(result)
print(
f"DONE {label} elapsed={result['api_elapsed_seconds']:.3f}s "
f"reused={result['reused']} generated={result['generated']}",
flush=True,
)
manifests = _manifest_records(run_name)
validate(results, manifests)
summary = {
"gate": "V3.6 PIG-2 Run Storage / Session identity",
"status": "PASS",
"run_name": run_name,
"source_prompt": str(args.source),
"results": results,
"manifests": manifests,
}
(output_root / "gate_summary.json").write_text(
json.dumps(summary, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(json.dumps({"status": "PASS", "output_root": str(output_root)}))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+134
View File
@@ -0,0 +1,134 @@
"""Run one V3.6-R1 masked-prefix A/B case through the ComfyUI API."""
from __future__ import annotations
import argparse
import copy
import json
from pathlib import Path
import sys
import uuid
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
from p32_api_runner import submit_and_wait
def configure_prompt(
source: dict,
*,
transport: str,
output_prefix: str,
seed: int,
) -> dict:
prompt = copy.deepcopy(source)
for node_id in ("250", "292", "302", "304"):
prompt.pop(node_id, None)
sampler = prompt["305"]
sampler["class_type"] = "H3ContinuumMaskedPrefixR1Diagnostic"
sampler["_meta"] = {"title": "H3 Continuum V3.6-R1 Masked Prefix Diagnostic"}
sampler["inputs"].update(
{
"prompt_mode": "Fixed — one prompt",
"chunks": 2,
"chunk_seconds": 5.0,
"width": 640,
"height": 640,
"continuity": "Balanced — 22 frames",
"base_seed": int(seed),
"audio_continuity": False,
"diagnostics": "Detailed Report",
"show_preview": False,
"run_storage": "Off",
"continuation_transport": str(transport),
}
)
prompt["188"]["inputs"]["value"] = (
"A cinematic live-action tracking shot of one skateboarder moving "
"continuously along a waterfront promenade. The subject maintains "
"forward momentum, body pose, direction, and camera-relative speed "
"throughout the shot. Natural city ambience, rolling skateboard wheels, "
"and distant traffic are audible."
)
prompt["226"]["inputs"]["samples"] = ["305", 0]
prompt["227"]["inputs"]["samples"] = ["305", 1]
prompt["306"]["inputs"].update(
{
"audio_seam": "Off",
"video_seam": "Off",
"assembly_plan": ["305", 2],
"driving_audio": ["305", 4],
}
)
prompt["249"]["inputs"]["source"] = ["305", 3]
prompt["191"]["inputs"]["filename_prefix"] = str(output_prefix)
return prompt
def run(args) -> dict:
source = json.loads(args.prompt.read_text(encoding="utf-8"))
configured = configure_prompt(
source,
transport=args.transport,
output_prefix=f"video/V36_R1_{args.transport}_seed{args.seed}",
seed=args.seed,
)
args.output_root.mkdir(parents=True, exist_ok=True)
prompt_path = args.output_root / f"prompt_{args.transport}.json"
prompt_path.write_text(
json.dumps(configured, indent=2, ensure_ascii=False),
encoding="utf-8",
)
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=configured,
client_id=f"v36-r1-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=2.0,
)
history_path = args.output_root / f"history_{args.transport}.json"
history_path.write_text(
json.dumps(history, indent=2, ensure_ascii=False),
encoding="utf-8",
)
summary = {
"format": "h3-continuum-v36-r1-api-run-v1",
"transport": args.transport,
"prompt_id": prompt_id,
"api_elapsed_seconds": elapsed,
"prompt": str(prompt_path),
"history": str(history_path),
}
(args.output_root / f"summary_{args.transport}.json").write_text(
json.dumps(summary, indent=2, ensure_ascii=False),
encoding="utf-8",
)
return summary
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--server", default="http://127.0.0.1:8188")
parser.add_argument("--prompt", type=Path, required=True)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument(
"--transport",
choices=("reference_context_v1", "masked_video_prefix_v1"),
required=True,
)
parser.add_argument("--timeout-seconds", type=float, default=1800.0)
parser.add_argument("--seed", type=int, default=238985343135586)
return parser
def main() -> int:
print(json.dumps(run(_parser().parse_args()), ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+256
View File
@@ -0,0 +1,256 @@
#!/usr/bin/env python3
"""Aggregate V3.6-R1 Stress Gate timings and boundary video metrics."""
from __future__ import annotations
import argparse
import json
import math
import re
from pathlib import Path
import cv2
import numpy as np
SAMPLING_RE = re.compile(r"sampling=([0-9.]+)s")
def _median(values: list[float]) -> float:
values = sorted(values)
middle = len(values) // 2
if len(values) % 2:
return values[middle]
return (values[middle - 1] + values[middle]) / 2.0
def _read_summary(path: Path) -> dict:
return json.loads(path.read_text(encoding="utf-8"))
def _sampling_seconds(run: dict) -> list[float]:
values: list[float] = []
for line in run["performance_lines"]:
if "physical group" not in line:
continue
match = SAMPLING_RE.search(line)
if match:
values.append(float(match.group(1)))
return values
def _video_path(output_prefix: str, output_dir: Path) -> Path:
stem = Path(output_prefix).name
matches = sorted(output_dir.glob(f"{stem}_*.mp4"), key=lambda path: path.stat().st_mtime)
if not matches:
raise FileNotFoundError(f"No output video for {stem}")
return matches[-1]
def _read_frames(path: Path) -> tuple[list[np.ndarray], float]:
capture = cv2.VideoCapture(str(path))
if not capture.isOpened():
raise RuntimeError(f"Unable to open {path}")
fps = float(capture.get(cv2.CAP_PROP_FPS))
frames: list[np.ndarray] = []
while True:
ok, frame = capture.read()
if not ok:
break
frames.append(frame)
capture.release()
return frames, fps
def _safe_cosine(left: np.ndarray, right: np.ndarray) -> float:
denominator = float(np.linalg.norm(left) * np.linalg.norm(right))
if denominator <= 1e-12:
return 1.0
return float(np.dot(left, right) / denominator)
def _boundary_metrics(frames: list[np.ndarray], boundary: int) -> dict:
first = max(0, boundary - 12)
last = min(len(frames) - 1, boundary + 24)
magnitudes: list[float] = []
mean_vectors: list[np.ndarray] = []
for index in range(first, last):
previous = cv2.cvtColor(frames[index], cv2.COLOR_BGR2GRAY)
current = cv2.cvtColor(frames[index + 1], cv2.COLOR_BGR2GRAY)
previous = cv2.resize(previous, (320, 320), interpolation=cv2.INTER_AREA)
current = cv2.resize(current, (320, 320), interpolation=cv2.INTER_AREA)
flow = cv2.calcOpticalFlowFarneback(
previous,
current,
None,
0.5,
3,
21,
3,
5,
1.2,
0,
)
magnitude = np.linalg.norm(flow, axis=2)
magnitudes.append(float(np.median(magnitude)))
mean_vectors.append(np.mean(flow.reshape(-1, 2), axis=0))
transition_index = boundary - first - 1
pre = magnitudes[max(0, transition_index - 6) : max(0, transition_index)]
post = magnitudes[transition_index + 1 : transition_index + 7]
pre_magnitude = float(np.median(pre)) if pre else 0.0
post_magnitude = float(np.median(post)) if post else 0.0
magnitude_ratio = post_magnitude / pre_magnitude if pre_magnitude > 1e-12 else math.inf
pre_vectors = mean_vectors[max(0, transition_index - 6) : max(0, transition_index)]
post_vectors = mean_vectors[transition_index + 1 : transition_index + 7]
pre_vector = np.mean(pre_vectors, axis=0) if pre_vectors else np.zeros(2, dtype=np.float32)
post_vector = np.mean(post_vectors, axis=0) if post_vectors else np.zeros(2, dtype=np.float32)
accelerations = np.diff(np.asarray(magnitudes, dtype=np.float64))
jerks = np.diff(accelerations)
local_jerk = jerks[max(0, transition_index - 3) : transition_index + 4]
before_frames = frames[max(0, boundary - 12) : boundary]
after_frames = frames[boundary : min(len(frames), boundary + 24)]
def color_stats(items: list[np.ndarray]) -> tuple[float, float, float]:
if not items:
return 0.0, 0.0, 0.0
luma: list[float] = []
contrast: list[float] = []
saturation: list[float] = []
for item in items:
gray = cv2.cvtColor(item, cv2.COLOR_BGR2GRAY)
hsv = cv2.cvtColor(item, cv2.COLOR_BGR2HSV)
luma.append(float(gray.mean()))
contrast.append(float(gray.std()))
saturation.append(float(hsv[:, :, 1].mean()))
return float(np.mean(luma)), float(np.mean(contrast)), float(np.mean(saturation))
before_luma, before_contrast, before_saturation = color_stats(before_frames)
after_luma, after_contrast, after_saturation = color_stats(after_frames)
return {
"boundary_frame": boundary,
"flow_magnitude_pre": pre_magnitude,
"flow_magnitude_post": post_magnitude,
"flow_magnitude_ratio_post_over_pre": magnitude_ratio,
"flow_direction_cosine": _safe_cosine(pre_vector, post_vector),
"flow_peak_abs_jerk": float(np.max(np.abs(local_jerk))) if len(local_jerk) else 0.0,
"luma_delta": after_luma - before_luma,
"contrast_delta": after_contrast - before_contrast,
"saturation_delta": after_saturation - before_saturation,
}
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--normal-root", type=Path, required=True)
parser.add_argument("--output-dir", type=Path, required=True)
parser.add_argument("--result", type=Path, required=True)
args = parser.parse_args()
runs = []
for summary_path in sorted(args.normal_root.glob("normal_*/summary.json")):
run = _read_summary(summary_path)
sampling = _sampling_seconds(run)
diagnostics = run["diagnostic"]
calls = diagnostics["sample_calls"]
continuation_calls = calls[1:]
video_path = _video_path(run["output_prefix"], args.output_dir)
frames, fps = _read_frames(video_path)
layouts = [call["packed_layouts"][0] for call in continuation_calls]
runs.append(
{
"label": run["label"],
"seed": run["seed"],
"transport": run["transport"],
"api_elapsed_seconds": run["api_elapsed_seconds"],
"sampling_seconds": sampling,
"continuation_sampling_seconds": sum(sampling[1:]),
"sample_host_continuation_seconds": sum(
float(call["sample_host_elapsed_seconds"]) for call in continuation_calls
),
"packed_seq_len": [layout["seq_len"] for layout in layouts],
"packed_rows_by_kind": [layout["rows_by_kind"] for layout in layouts],
"final_prefix_bit_exact": [
pair["bit_exact"] for pair in diagnostics["final_prefix_pairs"]
],
"final_prefix_max_abs_diff": [
pair["max_abs_diff"] for pair in diagnostics["final_prefix_pairs"]
],
"peak_process_rss_bytes": run["resources"]["peak_process_rss_bytes"],
"peak_process_uss_bytes": run["resources"]["peak_process_uss_bytes"],
"peak_device_used_bytes": run["resources"]["peak_device_used_bytes"],
"video": str(video_path),
"video_width": int(frames[0].shape[1]),
"video_height": int(frames[0].shape[0]),
"video_frames": len(frames),
"video_fps": fps,
"video_duration_seconds": len(frames) / fps,
"boundaries": [_boundary_metrics(frames, boundary) for boundary in (120, 240)],
}
)
pairs = []
for seed in sorted({run["seed"] for run in runs}):
by_transport = {run["transport"]: run for run in runs if run["seed"] == seed}
reference = by_transport["reference_context_v1"]
masked = by_transport["masked_video_prefix_v1"]
pairs.append(
{
"seed": seed,
"reference_api_seconds": reference["api_elapsed_seconds"],
"masked_api_seconds": masked["api_elapsed_seconds"],
"api_masked_faster_seconds": reference["api_elapsed_seconds"]
- masked["api_elapsed_seconds"],
"api_masked_faster_percent": 100.0
* (reference["api_elapsed_seconds"] - masked["api_elapsed_seconds"])
/ reference["api_elapsed_seconds"],
"reference_continuation_sampling_seconds": reference[
"continuation_sampling_seconds"
],
"masked_continuation_sampling_seconds": masked[
"continuation_sampling_seconds"
],
"continuation_masked_faster_percent": 100.0
* (
reference["continuation_sampling_seconds"]
- masked["continuation_sampling_seconds"]
)
/ reference["continuation_sampling_seconds"],
}
)
result = {
"format": "h3-continuum-v36-r1-stress-gate-analysis-v1",
"runs": runs,
"pairs": pairs,
"medians": {
"reference_api_seconds": _median(
[run["api_elapsed_seconds"] for run in runs if run["transport"] == "reference_context_v1"]
),
"masked_api_seconds": _median(
[run["api_elapsed_seconds"] for run in runs if run["transport"] == "masked_video_prefix_v1"]
),
"api_masked_faster_percent": _median(
[pair["api_masked_faster_percent"] for pair in pairs]
),
"reference_continuation_sampling_seconds": _median(
[pair["reference_continuation_sampling_seconds"] for pair in pairs]
),
"masked_continuation_sampling_seconds": _median(
[pair["masked_continuation_sampling_seconds"] for pair in pairs]
),
"continuation_masked_faster_percent": _median(
[pair["continuation_masked_faster_percent"] for pair in pairs]
),
},
}
args.result.parent.mkdir(parents=True, exist_ok=True)
args.result.write_text(json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8")
print(json.dumps({"pairs": pairs, "medians": result["medians"]}, indent=2))
if __name__ == "__main__":
main()
+416
View File
@@ -0,0 +1,416 @@
"""Run the V3.6-R1 three-chunk I2VA Strong Motion Stress Gate."""
from __future__ import annotations
import argparse
import copy
import json
from pathlib import Path
import re
import sys
import threading
import time
import uuid
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 p32_api_runner import submit_and_wait
STRONG_MOTION_PROMPT = (
"A dynamic cinematic live-action shot of the woman in the orange jacket "
"accelerating rapidly on her skateboard along the waterfront promenade. "
"She performs fast continuous S-turns, leaning deeply from side to side "
"while her hair and jacket react to speed. The low tracking camera sweeps "
"alongside her with a strong continuous pan and partial orbit, maintaining "
"motion through the sequence while preserving her face, orange jacket, "
"white trousers, skateboard, city skyline, cool daylight, and natural contrast."
)
TRANSPORTS = ("reference_context_v1", "masked_video_prefix_v1")
def _walk_strings(value: Any):
if isinstance(value, str):
yield value
elif isinstance(value, Mapping):
for item in value.values():
yield from _walk_strings(item)
elif isinstance(value, list):
for item in value:
yield from _walk_strings(item)
def _extract_evidence(history: Mapping[str, Any]) -> dict[str, Any]:
lines: list[str] = []
diagnostic = None
for text in _walk_strings(history):
for line in text.splitlines():
if line.startswith(("Prompt/CLIP cache [V3.5]", "Performance [V3.5")):
lines.append(line)
marker = "V3.6-R1 diagnostic: "
if marker in line:
diagnostic = json.loads(line.split(marker, 1)[1])
cache_line = next(
(line for line in lines if line.startswith("Prompt/CLIP cache [V3.5]")),
None,
)
cache = None
if cache_line:
match = re.search(
r"hits=(\d+), misses=(\d+), bypasses=(\d+), encode_calls=(\d+)",
cache_line,
)
if match:
cache = {
"hits": int(match.group(1)),
"misses": int(match.group(2)),
"bypasses": int(match.group(3)),
"encode_calls": int(match.group(4)),
}
return {
"prompt_clip_cache": cache,
"performance_lines": lines,
"diagnostic": diagnostic,
}
class ResourceMonitor:
def __init__(self, backend_pid: int | None, interval_seconds: float = 1.0):
self.backend_pid = int(backend_pid) if backend_pid else None
self.interval_seconds = float(interval_seconds)
self.peak_process_rss_bytes = 0
self.peak_process_uss_bytes = 0
self.minimum_system_available_bytes: int | None = None
self.peak_device_used_bytes = 0
self.samples = 0
self._stop = threading.Event()
self._thread: threading.Thread | None = None
self._psutil = None
self._process = None
self._nvml = None
self._nvml_handle = None
def __enter__(self):
try:
import psutil
self._psutil = psutil
if self.backend_pid:
self._process = psutil.Process(self.backend_pid)
except Exception:
self._psutil = None
self._process = None
try:
import pynvml
pynvml.nvmlInit()
self._nvml = pynvml
self._nvml_handle = pynvml.nvmlDeviceGetHandleByIndex(0)
except Exception:
self._nvml = None
self._nvml_handle = None
self._sample()
self._thread = threading.Thread(target=self._loop, daemon=True)
self._thread.start()
return self
def __exit__(self, exc_type, exc, tb):
self._stop.set()
if self._thread is not None:
self._thread.join(timeout=max(2.0, self.interval_seconds * 2.0))
self._sample()
if self._nvml is not None:
try:
self._nvml.nvmlShutdown()
except Exception:
pass
def _loop(self):
while not self._stop.wait(self.interval_seconds):
self._sample()
def _sample(self):
self.samples += 1
if self._psutil is not None:
try:
available = int(self._psutil.virtual_memory().available)
if self.minimum_system_available_bytes is None:
self.minimum_system_available_bytes = available
else:
self.minimum_system_available_bytes = min(
self.minimum_system_available_bytes, available
)
except Exception:
pass
if self._process is not None:
try:
self.peak_process_rss_bytes = max(
self.peak_process_rss_bytes,
int(self._process.memory_info().rss),
)
full = self._process.memory_full_info()
self.peak_process_uss_bytes = max(
self.peak_process_uss_bytes,
int(getattr(full, "uss", 0) or 0),
)
except Exception:
pass
if self._nvml is not None and self._nvml_handle is not None:
try:
used = int(self._nvml.nvmlDeviceGetMemoryInfo(self._nvml_handle).used)
self.peak_device_used_bytes = max(self.peak_device_used_bytes, used)
except Exception:
pass
def as_dict(self) -> dict[str, Any]:
return {
"backend_pid": self.backend_pid,
"samples": int(self.samples),
"peak_process_rss_bytes": int(self.peak_process_rss_bytes),
"peak_process_uss_bytes": int(self.peak_process_uss_bytes),
"minimum_system_available_bytes": self.minimum_system_available_bytes,
"peak_device_used_bytes": int(self.peak_device_used_bytes),
}
def configure_prompt(
source: Mapping[str, Any],
*,
transport: str,
output_prefix: str,
seed: int,
chunks: int,
synchronize_sampling: bool,
diagnostic_nonce: int,
) -> dict[str, Any]:
prompt = copy.deepcopy(dict(source))
sampler = prompt["305"]
sampler["class_type"] = "H3ContinuumMaskedPrefixR1Diagnostic"
sampler["_meta"] = {"title": "H3 Continuum V3.6-R1 Stress Diagnostic"}
sampler["inputs"].update(
{
"prompt_mode": "Fixed — one prompt",
"chunks": int(chunks),
"chunk_seconds": 5.0,
"width": 640,
"height": 640,
"continuity": "Balanced — 22 frames",
"base_seed": int(seed),
"audio_continuity": False,
"diagnostics": "Detailed Report",
"show_preview": False,
"run_storage": "Off",
"continuation_transport": str(transport),
"synchronize_sampling": bool(synchronize_sampling),
"diagnostic_nonce": int(diagnostic_nonce),
}
)
prompt["188"]["inputs"]["value"] = STRONG_MOTION_PROMPT
prompt["306"]["inputs"].update(
{
"exact_total_duration": True,
"audio_seam": "Off",
"video_seam": "Off",
"buffer_backend": "Auto",
"diagnostics": "Detailed Report",
}
)
prompt["191"]["inputs"]["filename_prefix"] = str(output_prefix)
return prompt
def _run_one(
*,
source: Mapping[str, Any],
args,
label: str,
transport: str,
seed: int,
chunks: int,
synchronize_sampling: bool,
nonce: int,
warmup: bool,
) -> dict[str, Any]:
run_root = args.output_root / label
run_root.mkdir(parents=True, exist_ok=True)
output_prefix = f"video/V36_R1_Stress_{label}_seed{seed}"
prompt = configure_prompt(
source,
transport=transport,
output_prefix=output_prefix,
seed=seed,
chunks=chunks,
synchronize_sampling=synchronize_sampling,
diagnostic_nonce=nonce,
)
prompt_path = run_root / "prompt.json"
prompt_path.write_text(
json.dumps(prompt, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(
f"START label={label} transport={transport} seed={seed} chunks={chunks} "
f"sync={synchronize_sampling}",
flush=True,
)
with ResourceMonitor(args.backend_pid, args.monitor_interval) as monitor:
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=prompt,
client_id=f"v36-r1-stress-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=args.poll_seconds,
)
history_path = run_root / "history.json"
history_path.write_text(
json.dumps(history, indent=2, ensure_ascii=False), encoding="utf-8"
)
evidence = _extract_evidence(history)
result = {
"label": label,
"warmup": bool(warmup),
"transport": transport,
"seed": int(seed),
"chunks": int(chunks),
"synchronize_sampling": bool(synchronize_sampling),
"prompt_id": prompt_id,
"api_elapsed_seconds": float(elapsed),
"output_prefix": output_prefix,
"prompt": str(prompt_path),
"history": str(history_path),
"resources": monitor.as_dict(),
**evidence,
}
(run_root / "summary.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
)
cache = result.get("prompt_clip_cache")
if not warmup and (not cache or cache.get("encode_calls") != 0):
raise RuntimeError(
f"measured run {label} did not use the required Prompt/CLIP cache HIT: {cache}"
)
diagnostic = result.get("diagnostic") or {}
calls = list(diagnostic.get("sample_calls") or [])
if transport == "masked_video_prefix_v1" and chunks >= 2:
for call in calls[1:]:
layouts = list(call.get("packed_layouts") or [])
if not layouts:
raise RuntimeError(f"masked continuation call has no PackedLayout evidence: {label}")
rows = dict(layouts[0].get("rows_by_kind") or {})
if int(rows.get("cond", 0)) != 0 or int(rows.get("ref_img", 0)) != 0:
raise RuntimeError(
f"masked continuation retained unfair I2VA/reference rows in {label}: {rows}"
)
final_pairs = list(diagnostic.get("final_prefix_pairs") or [])
if len(final_pairs) != chunks - 1 or not all(
bool(item.get("bit_exact")) for item in final_pairs
):
raise RuntimeError(
f"masked final prefix contract failed in {label}: {final_pairs}"
)
print(
f"DONE label={label} elapsed={elapsed:.3f}s cache={cache} "
f"rss={monitor.peak_process_rss_bytes / 2**30:.3f}GiB",
flush=True,
)
return result
def run(args) -> dict[str, Any]:
source = json.loads(args.prompt.read_text(encoding="utf-8"))
args.output_root.mkdir(parents=True, exist_ok=True)
seeds = [int(args.seed) + index for index in range(3)]
if args.mode == "normal":
schedule = [
(seeds[0], "reference_context_v1"),
(seeds[0], "masked_video_prefix_v1"),
(seeds[1], "masked_video_prefix_v1"),
(seeds[1], "reference_context_v1"),
(seeds[2], "reference_context_v1"),
(seeds[2], "masked_video_prefix_v1"),
]
else:
schedule = [
(seeds[0], "reference_context_v1"),
(seeds[0], "masked_video_prefix_v1"),
]
results = []
nonce = int(time.time()) & 0x7FFFFFFF
if not args.skip_warmup:
results.append(
_run_one(
source=source,
args=args,
label=f"warmup_{args.mode}",
transport="masked_video_prefix_v1",
seed=seeds[0],
chunks=int(args.warmup_chunks),
synchronize_sampling=False,
nonce=nonce,
warmup=True,
)
)
nonce += 1
for index, (seed, transport) in enumerate(schedule, start=1):
short = "reference" if transport.startswith("reference") else "masked"
label = f"{args.mode}_{index:02d}_seed{seed}_{short}"
results.append(
_run_one(
source=source,
args=args,
label=label,
transport=transport,
seed=seed,
chunks=3,
synchronize_sampling=args.mode == "sync",
nonce=nonce,
warmup=False,
)
)
nonce += 1
summary = {
"format": "h3-continuum-v36-r1-stress-gate-v1",
"mode": args.mode,
"server": args.server,
"base_seed": int(args.seed),
"warmup_excluded": True,
"pinned_memory_required_disabled": True,
"runs": results,
}
(args.output_root / "stress_gate_summary.json").write_text(
json.dumps(summary, indent=2, ensure_ascii=False), encoding="utf-8"
)
return summary
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--server", default="http://127.0.0.1:8188")
parser.add_argument("--prompt", type=Path, required=True)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument("--mode", choices=("normal", "sync"), default="normal")
parser.add_argument("--seed", type=int, default=238985343135601)
parser.add_argument("--backend-pid", type=int)
parser.add_argument("--timeout-seconds", type=float, default=2400.0)
parser.add_argument("--poll-seconds", type=float, default=2.0)
parser.add_argument("--monitor-interval", type=float, default=1.0)
parser.add_argument("--skip-warmup", action="store_true")
parser.add_argument("--warmup-chunks", type=int, choices=(1, 2), default=1)
return parser
def main() -> int:
args = _parser().parse_args()
print(json.dumps(run(args), ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+170
View File
@@ -0,0 +1,170 @@
"""Measure the R2 39f/65T decoded-audio transition around the chunk boundary."""
from __future__ import annotations
import argparse
import json
import math
from pathlib import Path
import subprocess
from typing import Any
import numpy as np
def _decode_audio(path: Path, ffmpeg: Path, sample_rate: int) -> np.ndarray:
completed = subprocess.run(
[
str(ffmpeg),
"-v",
"error",
"-i",
str(path),
"-map",
"0:a:0",
"-f",
"f32le",
"-acodec",
"pcm_f32le",
"-ac",
"2",
"-ar",
str(sample_rate),
"pipe:1",
],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
values = np.frombuffer(completed.stdout, dtype="<f4")
if values.size % 2:
raise RuntimeError(f"decoded stereo PCM has odd sample count: {values.size}")
return values.reshape(-1, 2).astype(np.float64, copy=False)
def _rms(value: np.ndarray) -> float:
return float(np.sqrt(np.mean(np.square(value)))) if value.size else 0.0
def _spectral_distance(before: np.ndarray, after: np.ndarray) -> float:
count = min(len(before), len(after))
if count < 8:
return math.nan
window = np.hanning(count)[:, None]
left = np.abs(np.fft.rfft(before[-count:] * window, axis=0)).reshape(-1)
right = np.abs(np.fft.rfft(after[:count] * window, axis=0)).reshape(-1)
denominator = float(np.linalg.norm(left) * np.linalg.norm(right))
if denominator <= 1e-20:
return 0.0
return float(1.0 - np.dot(left, right) / denominator)
def _zero_crossing_rate(value: np.ndarray) -> float:
if len(value) < 2:
return 0.0
signs = np.signbit(value)
return float(np.mean(signs[1:] != signs[:-1]))
def boundary_metrics(
audio: np.ndarray,
*,
sample_rate: int,
boundary_sample: int,
) -> dict[str, Any]:
if not 1 <= boundary_sample < len(audio):
raise ValueError(
f"boundary sample {boundary_sample} is outside decoded audio length {len(audio)}"
)
short = max(1, int(round(sample_rate * 0.05)))
long = max(short, int(round(sample_rate * 0.25)))
before_short = audio[boundary_sample - short : boundary_sample]
after_short = audio[boundary_sample : boundary_sample + short]
before_long = audio[boundary_sample - long : boundary_sample]
after_long = audio[boundary_sample : boundary_sample + long]
neighborhood = audio[boundary_sample - long : boundary_sample + long]
differences = np.abs(np.diff(neighborhood, axis=0)).reshape(-1)
global_differences = np.abs(np.diff(audio, axis=0)).reshape(-1)
sample_jump = float(np.max(np.abs(audio[boundary_sample] - audio[boundary_sample - 1])))
p50 = float(np.percentile(differences, 50)) if differences.size else 0.0
p99 = float(np.percentile(differences, 99)) if differences.size else 0.0
before_rms = _rms(before_short)
after_rms = _rms(after_short)
before_peak = float(np.max(np.abs(before_short)))
after_peak = float(np.max(np.abs(after_short)))
dc_jump = float(
np.max(np.abs(np.mean(after_short, axis=0) - np.mean(before_short, axis=0)))
)
return {
"decoded_samples_per_channel": int(len(audio)),
"decoded_duration_seconds": float(len(audio) / sample_rate),
"boundary_sample": int(boundary_sample),
"boundary_seconds": float(boundary_sample / sample_rate),
"sample_jump": sample_jump,
"local_abs_diff_p50": p50,
"local_abs_diff_p99": p99,
"sample_jump_over_local_p99": sample_jump / p99 if p99 > 0 else math.inf,
"global_abs_diff_p99": float(np.percentile(global_differences, 99)),
"global_abs_diff_max": float(np.max(global_differences)),
"sample_jump_global_percentile": float(
100.0 * np.mean(global_differences <= sample_jump)
),
"dc_jump_50ms": dc_jump,
"rms_before_50ms": before_rms,
"rms_after_50ms": after_rms,
"rms_ratio_after_over_before": after_rms / before_rms if before_rms > 0 else math.inf,
"peak_before_50ms": before_peak,
"peak_after_50ms": after_peak,
"peak_ratio_after_over_before": after_peak / before_peak if before_peak > 0 else math.inf,
"zero_crossing_before_250ms": _zero_crossing_rate(before_long),
"zero_crossing_after_250ms": _zero_crossing_rate(after_long),
"spectral_cosine_distance_250ms": _spectral_distance(before_long, after_long),
"finite": bool(np.isfinite(audio).all()),
}
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--reference", type=Path, required=True)
parser.add_argument("--masked", type=Path, required=True)
parser.add_argument("--result", type=Path, required=True)
parser.add_argument("--ffmpeg", type=Path, default=Path(r"C:\FFmpeg\bin\ffmpeg.exe"))
parser.add_argument("--sample-rate", type=int, default=32000)
parser.add_argument("--boundary-frame", type=int, default=124)
parser.add_argument("--fps", type=float, default=24.0)
args = parser.parse_args()
boundary_sample = int(round(args.boundary_frame * args.sample_rate / args.fps))
result = {
"format": "h3-continuum-v36-r2-audio-boundary-v1",
"sample_rate": int(args.sample_rate),
"boundary_frame": int(args.boundary_frame),
"fps": float(args.fps),
"boundary_sample_rounding": "round(frame * sample_rate / fps)",
"reference": {
"path": str(args.reference),
**boundary_metrics(
_decode_audio(args.reference, args.ffmpeg, args.sample_rate),
sample_rate=args.sample_rate,
boundary_sample=boundary_sample,
),
},
"masked": {
"path": str(args.masked),
**boundary_metrics(
_decode_audio(args.masked, args.ffmpeg, args.sample_rate),
sample_rate=args.sample_rate,
boundary_sample=boundary_sample,
),
},
}
args.result.parent.mkdir(parents=True, exist_ok=True)
args.result.write_text(
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(json.dumps(result, indent=2, ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+61
View File
@@ -0,0 +1,61 @@
"""Reassemble an accepted R2 prompt with Audio Seam Auto via ComfyUI cache."""
from __future__ import annotations
import argparse
import json
from pathlib import Path
import sys
import uuid
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
from p32_api_runner import submit_and_wait
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--server", default="http://127.0.0.1:8188")
parser.add_argument("--prompt", type=Path, required=True)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument("--filename-prefix", required=True)
parser.add_argument("--timeout-seconds", type=float, default=300.0)
args = parser.parse_args()
prompt = json.loads(args.prompt.read_text(encoding="utf-8"))
prompt["306"]["inputs"]["audio_seam"] = "Auto"
prompt["191"]["inputs"]["filename_prefix"] = str(args.filename_prefix)
args.output_root.mkdir(parents=True, exist_ok=True)
prompt_path = args.output_root / "prompt.json"
prompt_path.write_text(
json.dumps(prompt, indent=2, ensure_ascii=False), encoding="utf-8"
)
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=prompt,
client_id=f"v36-r2-seam-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=1.0,
)
history_path = args.output_root / "history.json"
history_path.write_text(
json.dumps(history, indent=2, ensure_ascii=False), encoding="utf-8"
)
result = {
"prompt_id": prompt_id,
"api_elapsed_seconds": float(elapsed),
"prompt": str(prompt_path),
"history": str(history_path),
}
(args.output_root / "summary.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(json.dumps(result, ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+292
View File
@@ -0,0 +1,292 @@
"""Run one-seed V3.6-R2 Joint AV GPU structure gates."""
from __future__ import annotations
import argparse
import copy
import json
from pathlib import Path
import sys
import time
import uuid
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 p32_api_runner import submit_and_wait
from v36_r1_stress_gate_runner import ResourceMonitor, _extract_evidence
PROFILES = {
"r2-1-39": {
"continuity": "Strong — 39 frames (Experimental)",
"transport": "masked_av_prefix_39_v1",
"reference_label": "reference_39f",
"masked_label": "masked_av_39f_65t",
"output_tag": "R2_Gate1",
},
"r2-2-22": {
"continuity": "Balanced — 22 frames",
"transport": "masked_av_prefix_22_v1",
"reference_label": "reference_22f",
"masked_label": "masked_av_22f_37t",
"output_tag": "R2_Gate2",
},
}
PROMPT = (
"A continuous cinematic live-action tracking shot of one skateboarder "
"moving steadily along a waterfront promenade while the camera follows at "
"constant speed. Preserve forward motion, body pose, direction, and camera "
"movement through the entire shot. Natural city ambience, rolling skateboard "
"wheels, wind, and distant traffic continue without interruption."
)
def configure_prompt(
source: Mapping[str, Any],
*,
transport: str,
output_prefix: str,
seed: int,
synchronize_sampling: bool,
diagnostic_nonce: int,
chunks: int,
continuity: str,
) -> dict[str, Any]:
prompt = copy.deepcopy(dict(source))
sampler = prompt["305"]
sampler["class_type"] = "H3ContinuumMaskedPrefixR1Diagnostic"
sampler["_meta"] = {"title": "H3 Continuum V3.6-R2 Joint AV Diagnostic"}
sampler["inputs"].pop("first_frame", None)
sampler["inputs"].update(
{
"prompt_mode": "Fixed — one prompt",
"chunks": int(chunks),
"chunk_seconds": 5.0,
"width": 640,
"height": 640,
"continuity": str(continuity),
"base_seed": int(seed),
"audio_continuity": True,
"diagnostics": "Detailed Report",
"show_preview": False,
"run_storage": "Off",
"continuation_transport": str(transport),
"synchronize_sampling": bool(synchronize_sampling),
"diagnostic_nonce": int(diagnostic_nonce),
}
)
prompt["188"]["inputs"]["value"] = PROMPT
prompt["306"]["inputs"].update(
{
"exact_total_duration": True,
"audio_seam": "Off",
"video_seam": "Off",
"buffer_backend": "Auto",
"diagnostics": "Detailed Report",
}
)
prompt["191"]["inputs"]["filename_prefix"] = str(output_prefix)
return prompt
def _validate_masked(diagnostic: Mapping[str, Any]) -> None:
calls = list(diagnostic.get("sample_calls") or [])
if len(calls) != 2:
raise RuntimeError(f"expected two physical Sampling calls, got {len(calls)}")
continuation = calls[1]
if continuation.get("minimax_ref_kinds"):
raise RuntimeError(
f"masked AV continuation unexpectedly emitted refs: "
f"{continuation.get('minimax_ref_kinds')}"
)
if bool(continuation.get("continuum_interop_emitted")):
raise RuntimeError("masked AV continuation emitted old Spectrum interop")
checks = {
"source_tail_matches_target_prefix": True,
"source_audio_tail_matches_target_prefix": True,
"video_mask_prefix_zero": True,
"video_mask_generated_one": True,
"audio_mask_prefix_zero": True,
"audio_mask_generated_one": True,
}
for name, expected in checks.items():
if continuation.get(name) is not expected:
raise RuntimeError(f"masked AV evidence failed {name}: {continuation.get(name)!r}")
pairs = list(diagnostic.get("final_prefix_pairs") or [])
if len(pairs) != 1:
raise RuntimeError(f"expected one finalized AV prefix pair, got {len(pairs)}")
pair = pairs[0]
if not bool(pair.get("bit_exact")) or float(pair.get("max_abs_diff", 1.0)) != 0.0:
raise RuntimeError(f"final video prefix is not bit-exact: {pair}")
if not bool(pair.get("audio_bit_exact")) or float(
pair.get("audio_max_abs_diff", 1.0)
) != 0.0:
raise RuntimeError(f"final audio prefix is not bit-exact: {pair}")
def _validate_reference(diagnostic: Mapping[str, Any]) -> None:
calls = list(diagnostic.get("sample_calls") or [])
if len(calls) != 2:
raise RuntimeError(f"expected two physical Sampling calls, got {len(calls)}")
continuation = calls[1]
kinds = list(continuation.get("minimax_ref_kinds") or [])
separate_av = "video" in kinds and "audio" in kinds
combined_av = "video_audio" in kinds
if not separate_av and not combined_av:
raise RuntimeError(f"39f reference route lacks AV refs: {kinds}")
if not bool(continuation.get("continuum_interop_emitted")):
raise RuntimeError("39f reference route lacks Continuum interop")
def _run_one(
*,
source: Mapping[str, Any],
args,
transport: str,
label: str,
nonce: int,
chunks: int,
warmup: bool,
) -> dict[str, Any]:
run_root = args.output_root / label
run_root.mkdir(parents=True, exist_ok=True)
profile = PROFILES[args.profile]
output_prefix = f"video/V36_{profile['output_tag']}_{label}_seed{args.seed}"
prompt = configure_prompt(
source,
transport=transport,
output_prefix=output_prefix,
seed=args.seed,
synchronize_sampling=not warmup,
diagnostic_nonce=nonce,
chunks=chunks,
continuity=str(profile["continuity"]),
)
prompt_path = run_root / "prompt.json"
prompt_path.write_text(
json.dumps(prompt, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(f"START {label} transport={transport} chunks={chunks}", flush=True)
with ResourceMonitor(args.backend_pid, args.monitor_interval) as monitor:
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=prompt,
client_id=f"v36-r2-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=args.poll_seconds,
)
history_path = run_root / "history.json"
history_path.write_text(
json.dumps(history, indent=2, ensure_ascii=False), encoding="utf-8"
)
evidence = _extract_evidence(history)
result = {
"label": label,
"warmup": bool(warmup),
"transport": transport,
"seed": int(args.seed),
"chunks": int(chunks),
"synchronize_sampling": not warmup,
"prompt_id": prompt_id,
"api_elapsed_seconds": float(elapsed),
"output_prefix": output_prefix,
"prompt": str(prompt_path),
"history": str(history_path),
"resources": monitor.as_dict(),
**evidence,
}
(run_root / "summary.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
)
cache = result.get("prompt_clip_cache")
if not warmup and (not cache or cache.get("encode_calls") != 0):
raise RuntimeError(f"measured run did not use Prompt/CLIP cache HIT: {cache}")
if not warmup:
diagnostic = result.get("diagnostic") or {}
if transport.startswith("masked_av_prefix_"):
_validate_masked(diagnostic)
else:
_validate_reference(diagnostic)
print(f"DONE {label} elapsed={elapsed:.3f}s cache={cache}", flush=True)
return result
def run(args) -> dict[str, Any]:
source = json.loads(args.prompt.read_text(encoding="utf-8"))
profile = PROFILES[args.profile]
args.output_root.mkdir(parents=True, exist_ok=True)
nonce = int(time.time()) & 0x7FFFFFFF
runs = []
schedule = [(str(profile["transport"]), str(profile["masked_label"]))]
if not args.resume_masked_only:
runs.append(
_run_one(
source=source,
args=args,
transport=str(profile["transport"]),
label="warmup",
nonce=nonce,
chunks=1,
warmup=True,
)
)
schedule.insert(0, ("reference_context_v1", str(profile["reference_label"])))
for transport, label in schedule:
nonce += 1
runs.append(
_run_one(
source=source,
args=args,
transport=transport,
label=label,
nonce=nonce,
chunks=2,
warmup=False,
)
)
result = {
"format": "h3-continuum-v36-r2-gpu-structure-gate-v1",
"server": args.server,
"seed": int(args.seed),
"profile": args.profile,
"continuity": str(profile["continuity"]),
"audio_continuity": True,
"synchronized_sampling": True,
"runs": runs,
}
(args.output_root / "gate_summary.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
)
return result
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--server", default="http://127.0.0.1:8188")
parser.add_argument("--prompt", type=Path, required=True)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument("--seed", type=int, default=238985343135604)
parser.add_argument("--profile", choices=tuple(PROFILES), default="r2-1-39")
parser.add_argument("--backend-pid", type=int)
parser.add_argument("--timeout-seconds", type=float, default=2400.0)
parser.add_argument("--poll-seconds", type=float, default=2.0)
parser.add_argument("--monitor-interval", type=float, default=1.0)
parser.add_argument(
"--resume-masked-only",
action="store_true",
help="Resume after a completed Reference run without repeating it.",
)
return parser
def main() -> int:
print(json.dumps(run(_parser().parse_args()), ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+619
View File
@@ -0,0 +1,619 @@
"""Run the three-case V3.8 A2 Audio Only GPU Canary."""
from __future__ import annotations
import argparse
import copy
from dataclasses import asdict, dataclass
import hashlib
import json
from pathlib import Path
import shutil
import subprocess
import sys
from typing import Any, Mapping
import uuid
import numpy as np
import torch
from safetensors.torch import load_file
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
from p32_api_runner import submit_and_wait
from v36_r1_stress_gate_runner import ResourceMonitor
from v38_easy_gpu_gate_runner import _output_media, _probe_media
class GateFailure(RuntimeError):
pass
@dataclass(frozen=True)
class GateCase:
name: str
chunks: int
terminal_merge: bool
expected_physical_groups: int
boundary_seconds: tuple[float, ...]
CASES = (
GateCase("g1_1x5_tail6", 1, False, 1, ()),
GateCase("g2_3x5_balanced22", 3, False, 3, (5.0, 10.0)),
GateCase("g3_terminal_merge", 3, True, 2, (5.0, 10.0)),
)
PROMPT = (
"A continuous cinematic shot follows a woman walking through a modern plaza. "
"Natural footsteps, clothing movement, wind, and steady city ambience remain "
"audible throughout without cuts or silence."
)
FIRST_PASS_SEED = 3802701
REFINE_SEED = 3802702
def _sha256_file(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as stream:
for block in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(block)
return digest.hexdigest()
def _sha256_tensor(value: torch.Tensor) -> str:
tensor = value.detach().to(device="cpu").contiguous()
digest = hashlib.sha256()
digest.update(str(tuple(int(item) for item in tensor.shape)).encode("ascii"))
digest.update(str(tensor.dtype).encode("ascii"))
digest.update(tensor.view(torch.uint8).numpy().tobytes(order="C"))
return digest.hexdigest()
def _find_one(prompt: Mapping[str, Any], class_type: str) -> tuple[str, dict[str, Any]]:
matches = [
(str(node_id), node)
for node_id, node in prompt.items()
if str(node.get("class_type")) == class_type
]
if len(matches) != 1:
raise GateFailure(f"expected one {class_type}, found {len(matches)}")
return matches[0]
def _remove_class(prompt: dict[str, Any], class_type: str) -> None:
for node_id in [
str(key)
for key, node in prompt.items()
if str(node.get("class_type")) == class_type
]:
prompt.pop(node_id, None)
def _configure_prompt(
template: Mapping[str, Any],
*,
case: GateCase,
output_prefix: str,
) -> dict[str, Any]:
prompt = copy.deepcopy(dict(template))
sampler_id, continuum = _find_one(prompt, "H3ContinuumSamplerV38")
inputs = continuum["inputs"]
inputs.update(
{
"prompt_mode": "Fixed",
"chunks": int(case.chunks),
"chunk_seconds": 5.0,
"continuity": "Balanced — 22 frames",
"base_seed": FIRST_PASS_SEED,
"audio_continuity": True,
"diagnostics": "Detailed Report",
"reroll_from_chunk": "Auto",
"reroll_nonce": 0,
"strict_compatibility": False,
"debug": True,
"show_preview": False,
"run_storage": "Off",
"run_name": "",
"project_id": "",
"generation_mode": "Full Run",
"review_action": "Continue / Next",
"aspect": "Square 1:1",
"preset": "Custom",
"custom_mp": 0.331776,
}
)
for name in (
"reference_video_1",
"reference_video_2",
"reference_video_3",
"driving_audio",
):
inputs.pop(name, None)
if not case.terminal_merge:
inputs.pop("first_frame", None)
inputs.pop("last_frame", None)
_remove_class(prompt, "LoadImage")
_remove_class(prompt, "H3ContinuumLoadVideo")
prompt_id = str(inputs["sequence_prompt"][0])
prompt[prompt_id]["inputs"]["value"] = PROMPT
first_scheduler_id = str(inputs["sigmas"][0])
prompt[first_scheduler_id]["inputs"].update(
{"scheduler": "simple", "steps": 8, "denoise": 1.0}
)
scheduler_matches = [
(str(node_id), node)
for node_id, node in prompt.items()
if str(node.get("class_type")) == "BasicScheduler"
and str(node_id) != first_scheduler_id
]
if len(scheduler_matches) != 1:
raise GateFailure(
"expected one refine BasicScheduler, "
f"found {len(scheduler_matches)}"
)
refine_scheduler_id, refine_scheduler = scheduler_matches[0]
refine_scheduler["inputs"].update(
{"scheduler": "simple", "steps": 10, "denoise": 0.35}
)
prompt["302"] = {
"class_type": "H3A2AudioOnlyCanary",
"inputs": {
"model": inputs["model"],
"clip": inputs["clip"],
"sampler": inputs["sampler"],
"sigmas": [refine_scheduler_id, 0],
"video_latents": [sampler_id, 0],
"audio_latents": [sampler_id, 1],
"assembly_plan": [sampler_id, 2],
"refine_context": [sampler_id, 5],
"video_vae": inputs["video_vae"],
"refine_seed": REFINE_SEED,
"schedule_mode": "Tail",
"tail_evaluations": 6,
},
"_meta": {"title": "A2 Audio Only Canary"},
}
for node_id in ("900", "901", "902", "903", "904", "905", "906"):
prompt.pop(node_id, None)
prompt["900"] = {
"class_type": "PreviewAny",
"inputs": {"source": [sampler_id, 3]},
}
prompt["901"] = {
"class_type": "PreviewAny",
"inputs": {"source": ["302", 3]},
}
prompt["902"] = {
"class_type": "SaveLatent",
"inputs": {
"samples": [sampler_id, 0],
"filename_prefix": f"{output_prefix}_first_video",
},
}
prompt["903"] = {
"class_type": "SaveLatent",
"inputs": {
"samples": [sampler_id, 1],
"filename_prefix": f"{output_prefix}_first_audio",
},
}
prompt["904"] = {
"class_type": "SaveLatent",
"inputs": {
"samples": ["302", 0],
"filename_prefix": f"{output_prefix}_refined_video",
},
}
prompt["905"] = {
"class_type": "SaveLatent",
"inputs": {
"samples": ["302", 1],
"filename_prefix": f"{output_prefix}_refined_audio",
},
}
prompt["906"] = {
"class_type": "PreviewAny",
"inputs": {"source": ["302", 2]},
}
_save_id, save_video = _find_one(prompt, "SaveVideo")
save_video["inputs"]["filename_prefix"] = output_prefix
return prompt
def _history_entry(history: Mapping[str, Any], prompt_id: str) -> Mapping[str, Any]:
entry = history.get(prompt_id)
if not isinstance(entry, Mapping):
raise GateFailure(f"history is missing prompt {prompt_id}")
return entry
def _output_text(entry: Mapping[str, Any], node_id: str) -> str:
value = (entry.get("outputs") or {}).get(node_id) or {}
items = value.get("text")
if isinstance(items, list):
return "\n".join(str(item) for item in items)
if isinstance(items, str):
return items
return json.dumps(value, ensure_ascii=False, sort_keys=True)
def _output_latents(
entry: Mapping[str, Any],
node_id: str,
output_root: Path,
) -> list[dict[str, Any]]:
output = (entry.get("outputs") or {}).get(node_id) or {}
records = []
for value in output.get("latents") or []:
path = output_root / str(value.get("subfolder") or "") / str(
value["filename"]
)
latent = load_file(str(path), device="cpu")["latent_tensor"]
records.append(
{
"path": str(path),
"shape": [int(item) for item in latent.shape],
"dtype": str(latent.dtype),
"tensor_sha256": _sha256_tensor(latent),
}
)
return records
def _canary_json(status: str) -> dict[str, Any]:
line = next(
(item for item in status.splitlines() if item.startswith("A2_CANARY_JSON=")),
"",
)
if not line:
raise GateFailure("A2 canary status JSON is missing")
return json.loads(line.split("=", 1)[1])
def _decode_audio(path: Path, ffmpeg: str) -> np.ndarray:
process = subprocess.run(
[
ffmpeg,
"-v",
"error",
"-i",
str(path),
"-map",
"0:a:0",
"-ac",
"2",
"-ar",
"32000",
"-c:a",
"pcm_f32le",
"-f",
"f32le",
"-",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=False,
)
if process.returncode:
raise GateFailure(
"ffmpeg audio decode failed: "
+ process.stderr.decode("utf-8", errors="replace")[-2000:]
)
audio = np.frombuffer(process.stdout, dtype="<f4")
if audio.size == 0 or audio.size % 2:
raise GateFailure("decoded stereo PCM is empty or malformed")
return audio.reshape(-1, 2)
def _rms(value: np.ndarray) -> float:
if value.size == 0:
return 0.0
return float(np.sqrt(np.mean(np.square(value.astype(np.float64)))))
def _audio_health(
media_path: Path,
*,
ffmpeg: str,
expected_seconds: float,
boundaries: tuple[float, ...],
) -> dict[str, Any]:
audio = _decode_audio(media_path, ffmpeg)
sample_rate = 32000
global_rms = _rms(audio)
channel_rms = [_rms(audio[:, channel]) for channel in range(2)]
window = max(1, int(round(0.5 * sample_rate)))
window_rms = [
_rms(audio[start : start + window])
for start in range(0, len(audio), window)
if len(audio[start : start + window]) >= window // 2
]
boundary_metrics = []
for seconds in boundaries:
index = int(round(seconds * sample_rate))
if not 1 <= index < len(audio):
raise GateFailure(f"audio boundary {seconds}s is outside decoded PCM")
radius = int(round(0.1 * sample_rate))
local = audio[max(0, index - radius) : min(len(audio), index + radius)]
local_diff = np.abs(np.diff(local, axis=0))
q999 = float(np.quantile(local_diff, 0.999)) if local_diff.size else 0.0
sample_jump = float(np.max(np.abs(audio[index] - audio[index - 1])))
threshold = max(0.25, q999 * 8.0)
before_rms = _rms(audio[max(0, index - radius) : index])
after_rms = _rms(audio[index : min(len(audio), index + radius)])
boundary_metrics.append(
{
"seconds": seconds,
"sample_index": index,
"sample_jump": sample_jump,
"local_diff_q999": q999,
"click_threshold": threshold,
"no_click_advisory": sample_jump <= threshold,
"rms_before_100ms": before_rms,
"rms_after_100ms": after_rms,
"no_boundary_silence": before_rms > 1e-7 and after_rms > 1e-7,
}
)
expected_samples = int(round(expected_seconds * sample_rate))
checks = {
"finite": bool(np.isfinite(audio).all()),
"not_silent": global_rms > 1e-7,
"channels_not_collapsed": all(value > 1e-7 for value in channel_rms),
"windows_not_collapsed": bool(window_rms)
and min(window_rms) > 1e-8,
"duration_no_drift": abs(len(audio) - expected_samples) <= 1024,
"boundaries_no_click": all(
item["no_click_advisory"] for item in boundary_metrics
),
"boundaries_not_silent": all(
item["no_boundary_silence"] for item in boundary_metrics
),
}
return {
"sample_rate": sample_rate,
"channels": 2,
"samples_per_channel": int(len(audio)),
"duration_seconds": float(len(audio) / sample_rate),
"expected_samples_per_channel": expected_samples,
"global_rms": global_rms,
"channel_rms": channel_rms,
"peak": float(np.max(np.abs(audio))),
"minimum_window_rms": min(window_rms) if window_rms else 0.0,
"boundaries": boundary_metrics,
"checks": checks,
"pass": all(checks.values()),
}
def _run_case(
args: argparse.Namespace,
*,
case: GateCase,
template: Mapping[str, Any],
) -> dict[str, Any]:
case_root = args.evidence_root / case.name
case_root.mkdir(parents=True, exist_ok=True)
output_prefix = f"v38_a2_audio_only/{args.run_tag}/{case.name}"
prompt = _configure_prompt(template, case=case, output_prefix=output_prefix)
prompt_path = case_root / "prompt.json"
prompt_path.write_text(
json.dumps(prompt, indent=2, ensure_ascii=False),
encoding="utf-8",
)
print(f"A2 GPU Canary start: {case.name}", flush=True)
with ResourceMonitor(args.backend_pid, interval_seconds=0.5) as monitor:
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=prompt,
client_id=f"v38-a2-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=2.0,
)
history_path = case_root / "history.json"
history_path.write_text(
json.dumps(history, indent=2, ensure_ascii=False),
encoding="utf-8",
)
entry = _history_entry(history, prompt_id)
first_status = _output_text(entry, "900")
canary_status = _output_text(entry, "901")
canary = _canary_json(canary_status)
first_videos = _output_latents(entry, "902", args.output_root)
first_audios = _output_latents(entry, "903", args.output_root)
refined_videos = _output_latents(entry, "904", args.output_root)
refined_audios = _output_latents(entry, "905", args.output_root)
media_path = _output_media(history, args.output_root)
media = _probe_media(media_path, args.ffprobe)
audio_health = _audio_health(
media_path,
ffmpeg=args.ffmpeg,
expected_seconds=case.chunks * 5.0,
boundaries=case.boundary_seconds,
)
first_video_hashes = [item["tensor_sha256"] for item in first_videos]
refined_video_hashes = [item["tensor_sha256"] for item in refined_videos]
first_audio_hashes = [item["tensor_sha256"] for item in first_audios]
refined_audio_hashes = [item["tensor_sha256"] for item in refined_audios]
groups = canary.get("physical_groups") or []
terminal_check = True
if case.terminal_merge:
terminal_check = (
len(groups) == 2
and groups[-1].get("logical_chunks") == [2, 3]
and groups[-1].get("terminal_merged") is True
)
checks = {
"physical_group_count": len(groups) == case.expected_physical_groups,
"first_video_count": len(first_videos) == case.expected_physical_groups,
"refined_video_count": len(refined_videos) == case.expected_physical_groups,
"first_audio_count": len(first_audios) == case.expected_physical_groups,
"refined_audio_count": len(refined_audios) == case.expected_physical_groups,
"video_sha_exact": first_video_hashes == refined_video_hashes,
"video_object_identity": len(canary.get("video_object_identity") or [])
== case.expected_physical_groups
and all(canary.get("video_object_identity") or []),
"video_tensor_identity": len(canary.get("video_tensor_identity") or [])
== case.expected_physical_groups
and all(canary.get("video_tensor_identity") or []),
"audio_changed": len(first_audio_hashes) == len(refined_audio_hashes)
and all(
before != after
for before, after in zip(
first_audio_hashes,
refined_audio_hashes,
strict=True,
)
),
"audio_shape_preserved": canary.get("audio_input_shapes")
== canary.get("audio_output_shapes"),
"audio_seed_namespace": (
(canary.get("target") or {}).get("seed_namespace")
== "h3-continuum-refine-audio-v1"
),
"tail_6": canary.get("schedule_mode") == "tail"
and canary.get("schedule_evaluations") == 6,
"second_pass_contract_v1": canary.get("second_pass_contract_version") == 1,
"terminal_atomic_group": terminal_check,
"media_duration": abs(float(media["format_duration"]) - case.chunks * 5.0)
<= 0.05,
"media_frames": int(media["frames"]) == case.chunks * 5 * 24,
"media_audio_contract": int(media["audio_sample_rate"]) == 32000
and int(media["audio_channels"]) == 2,
"audio_health": bool(audio_health["pass"]),
"temporary_video_discarded": (
canary_status.count("temporary_video_discarded=true")
== case.expected_physical_groups
),
}
result = {
"case": asdict(case),
"prompt_id": prompt_id,
"api_elapsed_seconds": float(elapsed),
"prompt": str(prompt_path),
"history": str(history_path),
"first_status": first_status,
"canary_status": canary_status,
"canary": canary,
"first_video_latents": first_videos,
"refined_video_latents": refined_videos,
"first_audio_latents": first_audios,
"refined_audio_latents": refined_audios,
"media_path": str(media_path),
"media_sha256": _sha256_file(media_path),
"media": media,
"audio_health": audio_health,
"resources": monitor.as_dict(),
"checks": checks,
"pass": all(checks.values()),
}
(case_root / "result.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False),
encoding="utf-8",
)
(case_root / "canary_status.txt").write_text(
canary_status,
encoding="utf-8",
)
print(
f"A2 GPU Canary end: {case.name}; pass={result['pass']}; "
f"elapsed={elapsed:.3f}s; video_exact={checks['video_sha_exact']}; "
f"audio_changed={checks['audio_changed']}",
flush=True,
)
if not result["pass"]:
failed = [name for name, value in checks.items() if not value]
raise GateFailure(f"{case.name} failed checks: {failed}")
return result
def run(args: argparse.Namespace) -> dict[str, Any]:
args.evidence_root.mkdir(parents=True, exist_ok=True)
t2_template = json.loads(args.t2_template.read_text(encoding="utf-8"))
terminal_template = json.loads(
args.terminal_template.read_text(encoding="utf-8")
)
summary = {
"format": "h3-continuum-v38-a2-audio-only-gpu-canary-v1",
"server": args.server,
"backend_pid": args.backend_pid,
"run_tag": args.run_tag,
"first_pass_seed": FIRST_PASS_SEED,
"refine_seed": REFINE_SEED,
"settings": {
"resolution": "576x576",
"first_pass_steps": 8,
"refine_source_steps": 10,
"refine_denoise": 0.35,
"refine_schedule": "Tail 6",
"continuity": "Balanced — 22 frames",
"audio_continuity": True,
"run_storage": "Off",
"sampler": "res_multistep",
"scheduler": "simple",
"sage": True,
},
"templates": {
"t2": str(args.t2_template),
"terminal": str(args.terminal_template),
},
"results": {},
"pass": False,
"production_status": "HOLD",
}
summary_path = args.evidence_root / "summary.json"
try:
for case in CASES:
template = terminal_template if case.terminal_merge else t2_template
summary["results"][case.name] = _run_case(
args,
case=case,
template=template,
)
summary["pass"] = True
summary["status"] = "GPU Experimental PASS / Production HOLD"
return summary
except Exception as exc:
summary["failure_type"] = type(exc).__name__
summary["failure"] = str(exc)
summary["status"] = "GPU Canary STOP / Production HOLD"
return summary
finally:
summary_path.write_text(
json.dumps(summary, indent=2, ensure_ascii=False),
encoding="utf-8",
)
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--server", default="http://127.0.0.6:8190")
parser.add_argument("--backend-pid", type=int, required=True)
parser.add_argument("--run-tag", required=True)
parser.add_argument("--evidence-root", type=Path, required=True)
parser.add_argument("--t2-template", type=Path, required=True)
parser.add_argument("--terminal-template", type=Path, required=True)
parser.add_argument(
"--output-root",
type=Path,
default=Path(r"D:\output\video\comfy_video"),
)
parser.add_argument("--ffmpeg", default=shutil.which("ffmpeg"))
parser.add_argument("--ffprobe", default=shutil.which("ffprobe"))
parser.add_argument("--timeout-seconds", type=float, default=3600.0)
args = parser.parse_args()
if not args.ffmpeg or not args.ffprobe:
raise SystemExit("ffmpeg and ffprobe are required")
summary = run(args)
print(json.dumps(summary, indent=2, ensure_ascii=False), flush=True)
return 0 if summary["pass"] else 2
if __name__ == "__main__":
raise SystemExit(main())
@@ -0,0 +1,751 @@
"""Run the A3c Selective Refine public-node GPU Matrix."""
from __future__ import annotations
import argparse
import copy
from dataclasses import asdict, dataclass
import hashlib
import json
from pathlib import Path
import shutil
import subprocess
import sys
from typing import Any, Mapping
import uuid
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
from p32_api_runner import submit_and_wait
from v36_r1_stress_gate_runner import ResourceMonitor
from v38_a2_audio_only_gpu_canary import (
_audio_health,
_history_entry,
_output_latents,
_output_text,
)
from v38_easy_gpu_gate_runner import _output_media, _probe_media
class GateFailure(RuntimeError):
pass
@dataclass(frozen=True)
class MatrixCase:
name: str
target: str
chunks: int
terminal_merge: bool
expected_physical_groups: int
boundary_seconds: tuple[float, ...]
G2_TO_G5 = (
MatrixCase("g2_audio_only_1x5", "Audio Only", 1, False, 1, ()),
MatrixCase("g3_video_audio_1x5", "Video + Audio", 1, False, 1, ()),
MatrixCase(
"g4_video_audio_3x5",
"Video + Audio",
3,
False,
3,
(5.0, 10.0),
),
MatrixCase(
"g5_video_audio_terminal",
"Video + Audio",
3,
True,
2,
(5.0, 10.0),
),
)
PROMPT = (
"A continuous cinematic shot follows a woman walking through a modern plaza. "
"Natural footsteps, clothing movement, wind, and steady city ambience remain "
"audible throughout without cuts or silence."
)
FIRST_PASS_SEED = 3802801
REFINE_SEED = 3802802
SELECTIVE_CLASS = "H3ContinuumSelectiveSecondPassExperimental"
LEGACY_CLASS = "H3ContinuumSecondPassV35"
def _find_one(prompt: Mapping[str, Any], class_type: str) -> tuple[str, dict[str, Any]]:
values = [
(str(node_id), node)
for node_id, node in prompt.items()
if str(node.get("class_type")) == class_type
]
if len(values) != 1:
raise GateFailure(f"expected one {class_type}, found {len(values)}")
return values[0]
def _remove_class(prompt: dict[str, Any], class_type: str) -> None:
for node_id in [
str(value)
for value, node in prompt.items()
if str(node.get("class_type")) == class_type
]:
prompt.pop(node_id, None)
def _second_pass_inputs(prompt: Mapping[str, Any], *, target: str | None) -> dict[str, Any]:
sampler_id, continuum = _find_one(prompt, "H3ContinuumSamplerV38")
inputs = continuum["inputs"]
scheduler_matches = [
(str(node_id), node)
for node_id, node in prompt.items()
if str(node.get("class_type")) == "BasicScheduler"
and str(node_id) != str(inputs["sigmas"][0])
]
if len(scheduler_matches) != 1:
raise GateFailure(
f"expected one refine BasicScheduler, found {len(scheduler_matches)}"
)
refine_scheduler_id, _scheduler = scheduler_matches[0]
result = {
"model": inputs["model"],
"clip": inputs["clip"],
"sampler": inputs["sampler"],
"sigmas": [refine_scheduler_id, 0],
"video_latents": [sampler_id, 0],
"audio_latents": [sampler_id, 1],
"assembly_plan": [sampler_id, 2],
"refine_context": [sampler_id, 5],
"video_vae": inputs["video_vae"],
"refine_seed": REFINE_SEED,
}
if target is not None:
result["refine_target"] = target
return result
def _configure_base(
template: Mapping[str, Any],
*,
chunks: int,
terminal_merge: bool,
output_prefix: str,
) -> dict[str, Any]:
prompt = copy.deepcopy(dict(template))
sampler_id, continuum = _find_one(prompt, "H3ContinuumSamplerV38")
inputs = continuum["inputs"]
inputs.update(
{
"prompt_mode": "Fixed",
"chunks": int(chunks),
"chunk_seconds": 5.0,
"continuity": "Balanced — 22 frames",
"base_seed": FIRST_PASS_SEED,
"audio_continuity": True,
"diagnostics": "Detailed Report",
"reroll_from_chunk": "Auto",
"reroll_nonce": 0,
"strict_compatibility": False,
"debug": True,
"show_preview": False,
"run_storage": "Off",
"run_name": "",
"project_id": "",
"generation_mode": "Full Run",
"review_action": "Continue / Next",
"aspect": "Square 1:1",
"preset": "Custom",
"custom_mp": 0.331776,
}
)
for name in (
"reference_video_1",
"reference_video_2",
"reference_video_3",
"driving_audio",
):
inputs.pop(name, None)
if not terminal_merge:
inputs.pop("first_frame", None)
inputs.pop("last_frame", None)
_remove_class(prompt, "LoadImage")
_remove_class(prompt, "H3ContinuumLoadVideo")
prompt_id = str(inputs["sequence_prompt"][0])
prompt[prompt_id]["inputs"]["value"] = PROMPT
first_scheduler_id = str(inputs["sigmas"][0])
prompt[first_scheduler_id]["inputs"].update(
{"scheduler": "simple", "steps": 8, "denoise": 1.0}
)
refine_schedulers = [
node
for node_id, node in prompt.items()
if str(node.get("class_type")) == "BasicScheduler"
and str(node_id) != first_scheduler_id
]
if len(refine_schedulers) != 1:
raise GateFailure(
f"expected one refine BasicScheduler, found {len(refine_schedulers)}"
)
refine_schedulers[0]["inputs"].update(
{"scheduler": "simple", "steps": 10, "denoise": 0.35}
)
for node_id in ("907", "908", "909", "910", "911"):
prompt.pop(node_id, None)
_save_id, save_video = _find_one(prompt, "SaveVideo")
save_video["inputs"]["filename_prefix"] = output_prefix
return prompt
def _redirect_refined_media(prompt: dict[str, Any], node_id: str) -> None:
_video_id, video_decode = _find_one(prompt, "VAEDecode")
_audio_id, audio_decode = _find_one(prompt, "VAEDecodeAudio")
_assemble_id, assemble = _find_one(prompt, "H3ContinuumAssembleSeamV35")
video_decode["inputs"]["samples"] = [node_id, 0]
audio_decode["inputs"]["samples"] = [node_id, 1]
assemble["inputs"]["assembly_plan"] = [node_id, 2]
def _configure_g1(template: Mapping[str, Any], *, output_prefix: str) -> dict[str, Any]:
prompt = _configure_base(
template,
chunks=1,
terminal_merge=False,
output_prefix=output_prefix,
)
sampler_id, _continuum = _find_one(prompt, "H3ContinuumSamplerV38")
prompt["302"] = {
"class_type": LEGACY_CLASS,
"inputs": _second_pass_inputs(prompt, target=None),
"_meta": {"title": "Legacy V3.5 Video Only"},
}
prompt["303"] = {
"class_type": SELECTIVE_CLASS,
"inputs": _second_pass_inputs(prompt, target="Video Only"),
"_meta": {"title": "Selective Video Only"},
}
_redirect_refined_media(prompt, "303")
prompt["904"]["inputs"].update(
{"samples": ["303", 0], "filename_prefix": f"{output_prefix}_selective_video"}
)
prompt["905"]["inputs"].update(
{"samples": ["303", 1], "filename_prefix": f"{output_prefix}_selective_audio"}
)
prompt["909"] = {
"class_type": "SaveLatent",
"inputs": {
"samples": ["302", 0],
"filename_prefix": f"{output_prefix}_legacy_video",
},
}
prompt["910"] = {
"class_type": "SaveLatent",
"inputs": {
"samples": ["302", 1],
"filename_prefix": f"{output_prefix}_legacy_audio",
},
}
prompt["907"] = {
"class_type": "H3A3cVideoOnlyParityProbe",
"inputs": {
"input_audio_latents": [sampler_id, 1],
"legacy_video_latents": ["302", 0],
"legacy_audio_latents": ["302", 1],
"legacy_assembly_plan": ["302", 2],
"legacy_status": ["302", 3],
"selective_video_latents": ["303", 0],
"selective_audio_latents": ["303", 1],
"selective_assembly_plan": ["303", 2],
"selective_status": ["303", 3],
},
}
prompt["908"] = {
"class_type": "PreviewAny",
"inputs": {"source": ["907", 0]},
}
prompt["911"] = {
"class_type": "PreviewAny",
"inputs": {"source": ["303", 3]},
}
return prompt
def _configure_selective(
template: Mapping[str, Any],
*,
case: MatrixCase,
output_prefix: str,
) -> dict[str, Any]:
prompt = _configure_base(
template,
chunks=case.chunks,
terminal_merge=case.terminal_merge,
output_prefix=output_prefix,
)
sampler_id, _continuum = _find_one(prompt, "H3ContinuumSamplerV38")
prompt["302"] = {
"class_type": SELECTIVE_CLASS,
"inputs": _second_pass_inputs(prompt, target=case.target),
"_meta": {"title": f"Selective {case.target}"},
}
_redirect_refined_media(prompt, "302")
prompt["904"]["inputs"].update(
{"samples": ["302", 0], "filename_prefix": f"{output_prefix}_refined_video"}
)
prompt["905"]["inputs"].update(
{"samples": ["302", 1], "filename_prefix": f"{output_prefix}_refined_audio"}
)
prompt["907"] = {
"class_type": "H3A3cSelectiveRefineProbe",
"inputs": {
"input_video_latents": [sampler_id, 0],
"input_audio_latents": [sampler_id, 1],
"output_video_latents": ["302", 0],
"output_audio_latents": ["302", 1],
"input_assembly_plan": [sampler_id, 2],
"output_assembly_plan": ["302", 2],
"refine_status": ["302", 3],
"target_label": case.target,
},
}
prompt["908"] = {
"class_type": "PreviewAny",
"inputs": {"source": ["907", 0]},
}
return prompt
def _marker_json(text: str, marker: str) -> dict[str, Any]:
line = next((value for value in text.splitlines() if value.startswith(marker)), "")
if not line:
raise GateFailure(f"status marker is missing: {marker}")
return json.loads(line.split("=", 1)[1])
def _decoded_stream_hash(media_path: Path, *, ffmpeg: str, stream: str) -> str:
if stream == "video":
arguments = ["-map", "0:v:0", "-pix_fmt", "rgb24", "-f", "rawvideo"]
elif stream == "audio":
arguments = [
"-map",
"0:a:0",
"-ac",
"2",
"-ar",
"32000",
"-c:a",
"pcm_f32le",
"-f",
"f32le",
]
else:
raise ValueError(f"unsupported stream {stream!r}")
process = subprocess.Popen(
[ffmpeg, "-v", "error", "-i", str(media_path), *arguments, "-"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if process.stdout is None or process.stderr is None:
raise GateFailure("ffmpeg pipe creation failed")
digest = hashlib.sha256()
while True:
block = process.stdout.read(1024 * 1024)
if not block:
break
digest.update(block)
stderr = process.stderr.read()
returncode = process.wait()
if returncode:
raise GateFailure(
f"ffmpeg {stream} decode failed: "
+ stderr.decode("utf-8", errors="replace")[-2000:]
)
return digest.hexdigest()
def _execute(
args: argparse.Namespace,
*,
name: str,
prompt: dict[str, Any],
expected_seconds: float,
boundaries: tuple[float, ...],
) -> tuple[dict[str, Any], Mapping[str, Any]]:
case_root = args.evidence_root / name
case_root.mkdir(parents=True, exist_ok=True)
prompt_path = case_root / "prompt.json"
prompt_path.write_text(
json.dumps(prompt, indent=2, ensure_ascii=False),
encoding="utf-8",
)
print(f"A3c start: {name}", flush=True)
with ResourceMonitor(args.backend_pid, interval_seconds=0.5) as monitor:
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=prompt,
client_id=f"v38-a3c-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=2.0,
)
history_path = case_root / "history.json"
history_path.write_text(
json.dumps(history, indent=2, ensure_ascii=False),
encoding="utf-8",
)
entry = _history_entry(history, prompt_id)
media_path = _output_media(history, args.output_root)
media = _probe_media(media_path, args.ffprobe)
audio_health = _audio_health(
media_path,
ffmpeg=args.ffmpeg,
expected_seconds=expected_seconds,
boundaries=boundaries,
)
decoded = {
"video_sha256": _decoded_stream_hash(
media_path,
ffmpeg=args.ffmpeg,
stream="video",
),
"audio_pcm_sha256": _decoded_stream_hash(
media_path,
ffmpeg=args.ffmpeg,
stream="audio",
),
}
result = {
"name": name,
"prompt_id": prompt_id,
"api_elapsed_seconds": float(elapsed),
"prompt": str(prompt_path),
"history": str(history_path),
"media_path": str(media_path),
"media": media,
"decoded": decoded,
"audio_health": audio_health,
"resources": monitor.as_dict(),
}
return result, entry
def _run_g1(
args: argparse.Namespace,
*,
template: Mapping[str, Any],
) -> dict[str, Any]:
output_prefix = f"v38_a3c/{args.run_tag}/g1_video_only_parity"
prompt = _configure_g1(template, output_prefix=output_prefix)
result, entry = _execute(
args,
name="g1_video_only_parity",
prompt=prompt,
expected_seconds=5.0,
boundaries=(),
)
parity_status = _output_text(entry, "908")
parity = _marker_json(parity_status, "A3C_PARITY_JSON=")
first_videos = _output_latents(entry, "902", args.output_root)
first_audios = _output_latents(entry, "903", args.output_root)
selective_videos = _output_latents(entry, "904", args.output_root)
selective_audios = _output_latents(entry, "905", args.output_root)
legacy_videos = _output_latents(entry, "909", args.output_root)
legacy_audios = _output_latents(entry, "910", args.output_root)
group_count = int(parity.get("physical_group_count") or 0)
checks = {
"one_physical_group": group_count == 1,
"first_output_counts": len(first_videos) == len(first_audios) == 1,
"legacy_output_counts": len(legacy_videos) == len(legacy_audios) == 1,
"selective_output_counts": len(selective_videos)
== len(selective_audios)
== 1,
"video_latent_sha_parity": parity.get("legacy_video_sha256")
== parity.get("selective_video_sha256"),
"audio_latent_sha_parity": parity.get("legacy_audio_sha256")
== parity.get("selective_audio_sha256"),
"legacy_audio_passthrough": all(
parity.get("legacy_audio_object_identity") or []
)
and all(parity.get("legacy_audio_tensor_identity") or []),
"selective_audio_passthrough": all(
parity.get("selective_audio_object_identity") or []
)
and all(parity.get("selective_audio_tensor_identity") or []),
"audio_matches_first_pass": parity.get("legacy_audio_sha256")
== [item["tensor_sha256"] for item in first_audios]
== parity.get("selective_audio_sha256"),
"refine_seed_parity": parity.get("legacy_refine_group_seeds")
== parity.get("selective_refine_group_seeds"),
"sigmas_parity": parity.get("legacy_schedule_sigma_hash")
== parity.get("selective_schedule_sigma_hash"),
"sampling_count_parity": parity.get("legacy_sampling_passes_reported")
== parity.get("selective_sampling_passes_reported")
== group_count,
"assembly_plan_parity": parity.get("legacy_plan_without_target_identity")
== parity.get("selective_plan_without_target_identity"),
"target_contract": parity.get("selective_target_mode") == "video_only"
and parity.get("selective_seed_namespace")
== "h3-continuum-refine-v1",
"shape_parity": parity.get("legacy_video_shapes")
== parity.get("selective_video_shapes")
and parity.get("legacy_audio_shapes")
== parity.get("selective_audio_shapes"),
"finite": all(parity.get("legacy_video_finite") or [])
and all(parity.get("selective_video_finite") or [])
and all(parity.get("legacy_audio_finite") or [])
and all(parity.get("selective_audio_finite") or []),
"media": int(result["media"]["frames"]) == 120
and abs(float(result["media"]["format_duration"]) - 5.0) <= 0.05
and int(result["media"]["audio_sample_rate"]) == 32000
and int(result["media"]["audio_channels"]) == 2,
"audio_health": bool(result["audio_health"]["pass"]),
}
result.update(
{
"parity_status": parity_status,
"parity": parity,
"first_video_latents": first_videos,
"first_audio_latents": first_audios,
"legacy_video_latents": legacy_videos,
"legacy_audio_latents": legacy_audios,
"selective_video_latents": selective_videos,
"selective_audio_latents": selective_audios,
"checks": checks,
"pass": all(checks.values()),
}
)
case_root = args.evidence_root / "g1_video_only_parity"
(case_root / "result.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False),
encoding="utf-8",
)
print(f"A3c end: g1_video_only_parity; pass={result['pass']}", flush=True)
if not result["pass"]:
failed = [name for name, value in checks.items() if not value]
raise GateFailure(f"G1 Video Only parity failed: {failed}")
return result
def _run_selective_case(
args: argparse.Namespace,
*,
case: MatrixCase,
template: Mapping[str, Any],
) -> dict[str, Any]:
output_prefix = f"v38_a3c/{args.run_tag}/{case.name}"
prompt = _configure_selective(
template,
case=case,
output_prefix=output_prefix,
)
result, entry = _execute(
args,
name=case.name,
prompt=prompt,
expected_seconds=case.chunks * 5.0,
boundaries=case.boundary_seconds,
)
probe_status = _output_text(entry, "908")
probe = _marker_json(probe_status, "A3C_PROBE_JSON=")
first_videos = _output_latents(entry, "902", args.output_root)
first_audios = _output_latents(entry, "903", args.output_root)
refined_videos = _output_latents(entry, "904", args.output_root)
refined_audios = _output_latents(entry, "905", args.output_root)
groups = list(probe.get("physical_groups") or [])
expected_mode = "audio_only" if case.target == "Audio Only" else "video_audio"
expected_namespace = (
"h3-continuum-refine-audio-v1"
if case.target == "Audio Only"
else "h3-continuum-refine-av-v1"
)
terminal_atomic = True
if case.terminal_merge:
terminal_atomic = (
len(groups) == 2
and groups[-1].get("logical_chunks") == [2, 3]
and groups[-1].get("terminal_merged") is True
)
common = {
"physical_group_count": len(groups) == case.expected_physical_groups,
"latent_counts": len(first_videos)
== len(first_audios)
== len(refined_videos)
== len(refined_audios)
== case.expected_physical_groups,
"target_contract": probe.get("target_mode") == expected_mode
and probe.get("seed_namespace") == expected_namespace,
"contract_version": probe.get("second_pass_contract_version") == 1,
"seed_count": len(probe.get("refine_group_seeds") or [])
== case.expected_physical_groups,
"sigmas_recorded": bool(probe.get("schedule_sigma_hash")),
"sampling_count": probe.get("sampling_passes_reported")
== case.expected_physical_groups,
"video_shape": probe.get("input_video_shapes")
== probe.get("output_video_shapes"),
"audio_shape": probe.get("input_audio_shapes")
== probe.get("output_audio_shapes"),
"finite": all(probe.get("output_video_finite") or [])
and all(probe.get("output_audio_finite") or []),
"terminal_atomic": terminal_atomic,
"media_frames": int(result["media"]["frames"]) == case.chunks * 5 * 24,
"media_duration": abs(
float(result["media"]["format_duration"]) - case.chunks * 5.0
)
<= 0.05,
"media_audio": int(result["media"]["audio_sample_rate"]) == 32000
and int(result["media"]["audio_channels"]) == 2,
"audio_health": bool(result["audio_health"]["pass"]),
}
if case.target == "Audio Only":
target_checks = {
"video_object_passthrough": all(
probe.get("input_video_object_identity") or []
)
and all(probe.get("input_video_tensor_identity") or []),
"video_sha_passthrough": probe.get("input_video_sha256")
== probe.get("output_video_sha256"),
"audio_changed": all(
before != after
for before, after in zip(
probe.get("input_audio_sha256") or [],
probe.get("output_audio_sha256") or [],
strict=True,
)
),
}
else:
target_checks = {
"video_changed": all(
before != after
for before, after in zip(
probe.get("input_video_sha256") or [],
probe.get("output_video_sha256") or [],
strict=True,
)
),
"audio_changed": all(
before != after
for before, after in zip(
probe.get("input_audio_sha256") or [],
probe.get("output_audio_sha256") or [],
strict=True,
)
),
}
checks = {**common, **target_checks}
result.update(
{
"case": asdict(case),
"probe_status": probe_status,
"probe": probe,
"first_video_latents": first_videos,
"first_audio_latents": first_audios,
"refined_video_latents": refined_videos,
"refined_audio_latents": refined_audios,
"checks": checks,
"pass": all(checks.values()),
}
)
case_root = args.evidence_root / case.name
(case_root / "result.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False),
encoding="utf-8",
)
print(f"A3c end: {case.name}; pass={result['pass']}", flush=True)
if not result["pass"]:
failed = [name for name, value in checks.items() if not value]
raise GateFailure(f"{case.name} failed: {failed}")
return result
def run(args: argparse.Namespace) -> dict[str, Any]:
args.evidence_root.mkdir(parents=True, exist_ok=True)
t2_template = json.loads(args.t2_template.read_text(encoding="utf-8"))
terminal_template = json.loads(
args.terminal_template.read_text(encoding="utf-8")
)
summary = {
"format": "h3-continuum-v38-a3c-selective-refine-gpu-matrix-v1",
"server": args.server,
"backend_pid": args.backend_pid,
"run_tag": args.run_tag,
"first_pass_seed": FIRST_PASS_SEED,
"refine_seed": REFINE_SEED,
"settings": {
"resolution": "576x576",
"first_pass_steps": 8,
"refine_steps": 10,
"refine_denoise": 0.35,
"continuity": "Balanced — 22 frames",
"audio_continuity": True,
"run_storage": "Off",
"sampler": "res_multistep",
"scheduler": "simple",
"sage": True,
},
"templates": {
"t2": str(args.t2_template),
"terminal": str(args.terminal_template),
},
"results": {},
"pass": False,
"production_status": "HOLD for audio-targeted modes",
}
summary_path = args.evidence_root / "summary.json"
try:
summary["results"]["g1_video_only_parity"] = _run_g1(
args,
template=t2_template,
)
for case in G2_TO_G5:
template = terminal_template if case.terminal_merge else t2_template
summary["results"][case.name] = _run_selective_case(
args,
case=case,
template=template,
)
summary["pass"] = True
summary["status"] = "Selective Refine GPU Experimental PASS"
return summary
except Exception as exc:
summary["failure_type"] = type(exc).__name__
summary["failure"] = str(exc)
summary["status"] = "A3c STOP / Production HOLD"
return summary
finally:
summary_path.write_text(
json.dumps(summary, indent=2, ensure_ascii=False),
encoding="utf-8",
)
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--server", default="http://127.0.0.6:8190")
parser.add_argument("--backend-pid", type=int, required=True)
parser.add_argument("--run-tag", required=True)
parser.add_argument("--evidence-root", type=Path, required=True)
parser.add_argument("--t2-template", type=Path, required=True)
parser.add_argument("--terminal-template", type=Path, required=True)
parser.add_argument(
"--output-root",
type=Path,
default=Path(r"D:\output\video\comfy_video"),
)
parser.add_argument("--ffmpeg", default=shutil.which("ffmpeg"))
parser.add_argument("--ffprobe", default=shutil.which("ffprobe"))
parser.add_argument("--timeout-seconds", type=float, default=3600.0)
args = parser.parse_args()
if not args.ffmpeg or not args.ffprobe:
raise SystemExit("ffmpeg and ffprobe are required")
summary = run(args)
print(json.dumps(summary, indent=2, ensure_ascii=False), flush=True)
return 0 if summary["pass"] else 2
if __name__ == "__main__":
raise SystemExit(main())
@@ -0,0 +1,524 @@
"""Run the A4 Chunk-scoped Refine GPU Matrix against the public node."""
from __future__ import annotations
import argparse
import copy
from dataclasses import asdict, dataclass
import json
from pathlib import Path
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,
MatrixCase,
_configure_selective,
_execute,
_marker_json,
)
@dataclass(frozen=True)
class ScopeCase:
name: str
scope: str
scope_index: int
terminal_merge: bool
selected_group_index: int | None
selected_logical_chunks: tuple[int, ...]
CASES = (
ScopeCase("g1_all_video_audio_3x5", "All", 1, False, None, (1, 2, 3)),
ScopeCase("g2_physical_group_2", "Physical Group", 2, False, 1, (2,)),
ScopeCase("g3_logical_chunk_2", "Logical Chunk", 2, False, 1, (2,)),
ScopeCase("g4_terminal_logical_chunk_2", "Logical Chunk", 2, True, 1, (2, 3)),
ScopeCase("g5_terminal_logical_chunk_3", "Logical Chunk", 3, True, 1, (2, 3)),
)
def _key_paths(value: Any, prefix: str = "$") -> list[str]:
paths: list[str] = []
if isinstance(value, Mapping):
for key, item in sorted(value.items(), key=lambda pair: str(pair[0])):
path = f"{prefix}.{key}"
paths.append(path)
paths.extend(_key_paths(item, path))
elif isinstance(value, list):
for index, item in enumerate(value):
paths.extend(_key_paths(item, f"{prefix}[]"))
return paths
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 _configure(
template: Mapping[str, Any],
*,
case: ScopeCase,
output_prefix: str,
) -> dict[str, Any]:
base_case = MatrixCase(
name=case.name,
target="Video + Audio",
chunks=3,
terminal_merge=case.terminal_merge,
expected_physical_groups=2 if case.terminal_merge else 3,
boundary_seconds=(5.0, 10.0),
)
prompt = _configure_selective(
template,
case=base_case,
output_prefix=output_prefix,
)
selective = prompt["302"]["inputs"]
selective["refine_scope"] = case.scope
selective["refine_scope_index"] = int(case.scope_index)
prompt["302"]["_meta"]["title"] = (
f"Selective Video + Audio / {case.scope} {case.scope_index}"
)
return prompt
def _load_a3c_baseline(path: Path, result_name: str) -> dict[str, Any]:
summary = json.loads(path.read_text(encoding="utf-8"))
if not bool(summary.get("pass")):
raise GateFailure("A3c baseline summary is not PASS")
result = (summary.get("results") or {}).get(result_name)
if not isinstance(result, dict) or not bool(result.get("pass")):
raise GateFailure(f"A3c baseline {result_name!r} is missing or not PASS")
return result
def _identity_checks(
probe: Mapping[str, Any],
*,
selected_group_index: int,
) -> dict[str, bool]:
count = len(probe.get("physical_groups") or [])
expected_selected = [index == selected_group_index for index in range(count)]
video_object = list(probe.get("input_video_object_identity") or [])
video_tensor = list(probe.get("input_video_tensor_identity") or [])
audio_object = list(probe.get("input_audio_object_identity") or [])
audio_tensor = list(probe.get("input_audio_tensor_identity") or [])
input_video = list(probe.get("input_video_sha256") or [])
output_video = list(probe.get("output_video_sha256") or [])
input_audio = list(probe.get("input_audio_sha256") or [])
output_audio = list(probe.get("output_audio_sha256") or [])
return {
"identity_vector_lengths": all(
len(values) == count
for values in (
video_object,
video_tensor,
audio_object,
audio_tensor,
input_video,
output_video,
input_audio,
output_audio,
)
),
"video_object_passthrough": all(
identity == (not selected)
for identity, selected in zip(
video_object,
expected_selected,
strict=True,
)
),
"video_tensor_passthrough": all(
identity == (not selected)
for identity, selected in zip(
video_tensor,
expected_selected,
strict=True,
)
),
"audio_object_passthrough": all(
identity == (not selected)
for identity, selected in zip(
audio_object,
expected_selected,
strict=True,
)
),
"audio_tensor_passthrough": all(
identity == (not selected)
for identity, selected in zip(
audio_tensor,
expected_selected,
strict=True,
)
),
"video_sha_selection": all(
(before != after) is selected
for before, after, selected in zip(
input_video,
output_video,
expected_selected,
strict=True,
)
),
"audio_sha_selection": all(
(before != after) is selected
for before, after, selected in zip(
input_audio,
output_audio,
expected_selected,
strict=True,
)
),
}
def _run_case(
args: argparse.Namespace,
*,
case: ScopeCase,
template: Mapping[str, Any],
baseline_probe: Mapping[str, Any],
all_result: Mapping[str, Any] | None,
) -> dict[str, Any]:
output_prefix = f"v38_a4/{args.run_tag}/{case.name}"
prompt = _configure(template, case=case, output_prefix=output_prefix)
result, entry = _execute(
args,
name=case.name,
prompt=prompt,
expected_seconds=15.0,
boundaries=(5.0, 10.0),
)
probe_status = _output_text(entry, "908")
probe = _marker_json(probe_status, "A3C_PROBE_JSON=")
first_videos = _output_latents(entry, "902", args.output_root)
first_audios = _output_latents(entry, "903", args.output_root)
refined_videos = _output_latents(entry, "904", args.output_root)
refined_audios = _output_latents(entry, "905", args.output_root)
groups = list(probe.get("physical_groups") or [])
expected_groups = 2 if case.terminal_merge else 3
contract = (probe.get("output_plan") or {}).get("normalized", {}).get(
"second_pass_contract",
{},
)
checks = {
"physical_group_count": len(groups) == expected_groups,
"latent_counts": len(first_videos)
== len(first_audios)
== len(refined_videos)
== len(refined_audios)
== expected_groups,
"target_contract": probe.get("target_mode") == "video_audio"
and probe.get("seed_namespace") == "h3-continuum-refine-av-v1",
"contract_version": probe.get("second_pass_contract_version") == 1,
"schedule_hash": probe.get("schedule_sigma_hash")
== baseline_probe.get("schedule_sigma_hash"),
"plan_scope_field_absent": "refine_scope_contract" not in contract,
"media": int(result["media"]["frames"]) == 360
and abs(float(result["media"]["format_duration"]) - 15.0) <= 0.05
and int(result["media"]["audio_sample_rate"]) == 32000
and int(result["media"]["audio_channels"]) == 2,
"audio_health": bool(result["audio_health"]["pass"]),
}
if case.selected_group_index is None:
current_plan = (probe.get("output_plan") or {}).get("normalized")
baseline_plan = (baseline_probe.get("output_plan") or {}).get("normalized")
checks.update(
{
"all_video_input_parity": probe.get("input_video_sha256")
== baseline_probe.get("input_video_sha256"),
"all_audio_input_parity": probe.get("input_audio_sha256")
== baseline_probe.get("input_audio_sha256"),
"all_video_output_parity": probe.get("output_video_sha256")
== baseline_probe.get("output_video_sha256"),
"all_audio_output_parity": probe.get("output_audio_sha256")
== baseline_probe.get("output_audio_sha256"),
"all_seed_parity": probe.get("refine_group_seeds")
== baseline_probe.get("refine_group_seeds"),
"all_sampling_parity": probe.get("sampling_passes_reported")
== baseline_probe.get("sampling_passes_reported")
== expected_groups,
"all_plan_field_set": _key_paths(current_plan)
== _key_paths(baseline_plan),
"all_plan_parity": _cross_run_plan_identity(current_plan)
== _cross_run_plan_identity(baseline_plan),
"all_decoded_video_parity": result["decoded"]["video_sha256"]
== args.a3c_decoded_video_sha,
"all_decoded_audio_parity": result["decoded"]["audio_pcm_sha256"]
== args.a3c_decoded_audio_sha,
"all_scope_status": "scope=All." in probe_status,
}
)
else:
selected_index = int(case.selected_group_index)
expected_seed = list(baseline_probe.get("refine_group_seeds") or [])[selected_index]
checks.update(
{
"one_sampling": probe.get("sampling_passes_reported") == 1,
"one_seed": probe.get("refine_group_seeds") == [expected_seed],
"scope_status": f"scope={case.scope} {case.scope_index}." in probe_status,
"resolved_groups_status": f"selected_physical_groups=[{selected_index + 1}]"
in probe_status,
"resolved_logical_status": (
"selected_logical_chunks="
+ str(list(case.selected_logical_chunks))
)
in probe_status,
**_identity_checks(
probe,
selected_group_index=selected_index,
),
}
)
if all_result is not None:
all_plan = (all_result.get("probe") or {}).get("output_plan") or {}
checks["assembly_field_set_parity"] = _key_paths(
(probe.get("output_plan") or {}).get("normalized")
) == _key_paths(all_plan.get("normalized"))
result.update(
{
"case": asdict(case),
"probe_status": probe_status,
"probe": probe,
"first_video_latents": first_videos,
"first_audio_latents": first_audios,
"refined_video_latents": refined_videos,
"refined_audio_latents": refined_audios,
"checks": checks,
"pass": all(checks.values()),
}
)
case_root = args.evidence_root / case.name
(case_root / "result.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False),
encoding="utf-8",
)
print(f"A4 end: {case.name}; pass={result['pass']}", flush=True)
if not result["pass"]:
failed = [name for name, passed in checks.items() if not passed]
raise GateFailure(f"{case.name} failed: {failed}")
return result
def _load_resumed_all_result(
path: Path,
*,
baseline_probe: Mapping[str, Any],
) -> dict[str, Any]:
"""Revalidate one completed G1 without repeating its GPU Sampling."""
result = json.loads(path.read_text(encoding="utf-8"))
probe = result.get("probe") or {}
checks = dict(result.get("checks") or {})
current_plan = (probe.get("output_plan") or {}).get("normalized")
baseline_plan = (baseline_probe.get("output_plan") or {}).get("normalized")
checks["all_plan_field_set"] = _key_paths(current_plan) == _key_paths(
baseline_plan
)
checks["all_plan_parity"] = _cross_run_plan_identity(
current_plan
) == _cross_run_plan_identity(baseline_plan)
result["checks"] = checks
result["pass"] = all(checks.values())
result["resumed_without_resampling"] = True
if not result["pass"]:
failed = [name for name, passed in checks.items() if not passed]
raise GateFailure(f"resumed G1 validation failed: {failed}")
path.with_name("result.revalidated.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False),
encoding="utf-8",
)
return result
def _load_resumed_scoped_result(
path: Path,
*,
assembly_reference_probe: Mapping[str, Any],
) -> dict[str, Any]:
"""Revalidate one completed scoped case without repeating GPU Sampling."""
result = json.loads(path.read_text(encoding="utf-8"))
probe = result.get("probe") or {}
checks = dict(result.get("checks") or {})
current_plan = (probe.get("output_plan") or {}).get("normalized")
reference_plan = (assembly_reference_probe.get("output_plan") or {}).get(
"normalized"
)
checks["assembly_field_set_parity"] = _key_paths(current_plan) == _key_paths(
reference_plan
)
result["checks"] = checks
result["pass"] = all(checks.values())
result["resumed_without_resampling"] = True
if not result["pass"]:
failed = [name for name, passed in checks.items() if not passed]
raise GateFailure(f"resumed scoped validation failed: {failed}")
path.with_name("result.revalidated.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False),
encoding="utf-8",
)
return result
def _cross_case_checks(results: Mapping[str, Mapping[str, Any]]) -> dict[str, bool]:
g2 = results[CASES[1].name]
g3 = results[CASES[2].name]
g4 = results[CASES[3].name]
g5 = results[CASES[4].name]
return {
"physical_vs_logical_nonterminal_video": g2["probe"]["output_video_sha256"]
== g3["probe"]["output_video_sha256"],
"physical_vs_logical_nonterminal_audio": g2["probe"]["output_audio_sha256"]
== g3["probe"]["output_audio_sha256"],
"physical_vs_logical_nonterminal_seed": g2["probe"]["refine_group_seeds"]
== g3["probe"]["refine_group_seeds"],
"terminal_2_vs_3_video": g4["probe"]["output_video_sha256"]
== g5["probe"]["output_video_sha256"],
"terminal_2_vs_3_audio": g4["probe"]["output_audio_sha256"]
== g5["probe"]["output_audio_sha256"],
"terminal_2_vs_3_seed": g4["probe"]["refine_group_seeds"]
== g5["probe"]["refine_group_seeds"],
"terminal_2_vs_3_plan": g4["probe"]["output_plan"]["sha256"]
== g5["probe"]["output_plan"]["sha256"],
"terminal_2_vs_3_decoded_video": g4["decoded"]["video_sha256"]
== g5["decoded"]["video_sha256"],
"terminal_2_vs_3_decoded_audio": g4["decoded"]["audio_pcm_sha256"]
== g5["decoded"]["audio_pcm_sha256"],
}
def run(args: argparse.Namespace) -> dict[str, Any]:
args.evidence_root.mkdir(parents=True, exist_ok=True)
t2_template = json.loads(args.t2_template.read_text(encoding="utf-8"))
terminal_template = json.loads(args.terminal_template.read_text(encoding="utf-8"))
baseline_result = _load_a3c_baseline(
args.a3c_baseline,
"g4_video_audio_3x5",
)
terminal_baseline_result = _load_a3c_baseline(
args.a3c_baseline,
"g5_video_audio_terminal",
)
baseline_probe = baseline_result["probe"]
args.a3c_decoded_video_sha = baseline_result["decoded"]["video_sha256"]
args.a3c_decoded_audio_sha = baseline_result["decoded"]["audio_pcm_sha256"]
summary: dict[str, Any] = {
"format": "h3-continuum-v38-a4-chunk-scoped-refine-gpu-matrix-v1",
"server": args.server,
"backend_pid": args.backend_pid,
"run_tag": args.run_tag,
"a3c_baseline": str(args.a3c_baseline),
"results": {},
"pass": False,
"production_status": "HOLD for audio-targeted modes",
}
summary_path = args.evidence_root / "summary.json"
try:
resume_results: dict[str, Path] = {}
for path in args.resume_result or ():
payload = json.loads(path.read_text(encoding="utf-8"))
name = str((payload.get("case") or {}).get("name") or "")
if not name or name in resume_results:
raise GateFailure(f"invalid or duplicate resumed result: {path}")
resume_results[name] = path
all_result: Mapping[str, Any] | None = None
for case in CASES:
assembly_reference_result = (
terminal_baseline_result if case.terminal_merge else all_result
)
if case.name in resume_results:
if case.selected_group_index is None:
result = _load_resumed_all_result(
resume_results[case.name],
baseline_probe=baseline_probe,
)
else:
if assembly_reference_result is None:
raise GateFailure("scoped resume has no Assembly reference")
result = _load_resumed_scoped_result(
resume_results[case.name],
assembly_reference_probe=assembly_reference_result["probe"],
)
else:
template = terminal_template if case.terminal_merge else t2_template
result = _run_case(
args,
case=case,
template=template,
baseline_probe=(
terminal_baseline_result["probe"]
if case.terminal_merge
else baseline_probe
),
all_result=assembly_reference_result,
)
summary["results"][case.name] = result
if case.selected_group_index is None:
all_result = result
summary["cross_case_checks"] = _cross_case_checks(summary["results"])
if not all(summary["cross_case_checks"].values()):
failed = [
name
for name, passed in summary["cross_case_checks"].items()
if not passed
]
raise GateFailure(f"cross-case parity failed: {failed}")
summary["pass"] = True
summary["status"] = "A4 Chunk-scoped Refine GPU Experimental PASS"
return summary
except Exception as exc:
summary["failure_type"] = type(exc).__name__
summary["failure"] = str(exc)
summary["status"] = "A4 STOP / Production HOLD"
return summary
finally:
summary_path.write_text(
json.dumps(summary, indent=2, ensure_ascii=False),
encoding="utf-8",
)
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--server", default="http://127.0.0.6:8190")
parser.add_argument("--backend-pid", type=int, required=True)
parser.add_argument("--run-tag", required=True)
parser.add_argument("--evidence-root", type=Path, required=True)
parser.add_argument("--t2-template", type=Path, required=True)
parser.add_argument("--terminal-template", type=Path, required=True)
parser.add_argument("--a3c-baseline", type=Path, required=True)
parser.add_argument("--resume-result", type=Path, action="append")
parser.add_argument(
"--output-root",
type=Path,
default=Path(r"D:\output\video\comfy_video"),
)
parser.add_argument("--ffmpeg", default=shutil.which("ffmpeg"))
parser.add_argument("--ffprobe", default=shutil.which("ffprobe"))
parser.add_argument("--timeout-seconds", type=float, default=3600.0)
args = parser.parse_args()
if not args.ffmpeg or not args.ffprobe:
raise SystemExit("ffmpeg and ffprobe are required")
summary = run(args)
print(json.dumps(summary, indent=2, ensure_ascii=False), flush=True)
return 0 if summary["pass"] else 2
if __name__ == "__main__":
raise SystemExit(main())
@@ -0,0 +1,698 @@
"""Run the A6 Temporal Window Refine GPU Matrix against the public node."""
from __future__ import annotations
import argparse
import copy
from dataclasses import asdict, dataclass
import importlib.util
import json
from pathlib import Path
import shutil
import sys
import types
from typing import Any, Mapping, Sequence
from safetensors.torch import load_file
import torch
TOOLS_ROOT = Path(__file__).resolve().parent
SOURCE_ROOT = TOOLS_ROOT.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,
MatrixCase,
_configure_g1,
_configure_selective,
_execute,
_marker_json,
)
from v38_a4_chunk_scoped_refine_gpu_matrix import (
_cross_run_plan_identity,
_key_paths,
_load_a3c_baseline,
)
@dataclass(frozen=True)
class WindowCase:
name: str
target: str
chunks: int
terminal_merge: bool
start_sec: float
end_sec: float
selected_group_indices: tuple[int, ...]
boundary_seconds: tuple[float, ...]
WINDOW_CASES = (
WindowCase(
"g2_video_only_partial",
"Video Only",
1,
False,
1.0,
3.0,
(0,),
(),
),
WindowCase(
"g3_video_audio_partial",
"Video + Audio",
1,
False,
1.0,
3.0,
(0,),
(),
),
WindowCase(
"g4_video_audio_cross_group",
"Video + Audio",
3,
False,
4.0,
6.0,
(0, 1),
(5.0, 10.0),
),
WindowCase(
"g5_terminal_one_logical_side",
"Video + Audio",
3,
True,
5.5,
8.0,
(1,),
(5.0, 10.0),
),
)
VIDEO_ONLY_CONTRACT_FIELDS = {
"audio_output",
"audio_sampling",
"conditioning_sources",
"execution",
"physical_groups",
"refine_execution_contract",
"refine_group_seeds",
"refine_schedule",
"refine_schedule_identity",
"refine_seed_base",
"refine_target_contract",
"target_height",
"target_width",
"version",
}
def _load_source_contract():
"""Load only constants/temporal/refine_window under an isolated package."""
package_name = "h3_a6_gate_source"
package = types.ModuleType(package_name)
package.__path__ = [str(SOURCE_ROOT)]
sys.modules[package_name] = package
v3_package = types.ModuleType(f"{package_name}.v3")
v3_package.__path__ = [str(SOURCE_ROOT / "v3")]
sys.modules[f"{package_name}.v3"] = v3_package
def load(name: str, path: Path):
spec = importlib.util.spec_from_file_location(name, path)
if spec is None or spec.loader is None:
raise GateFailure(f"cannot load source module {path}")
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
return module
load(f"{package_name}.constants", SOURCE_ROOT / "constants.py")
temporal = load(f"{package_name}.temporal", SOURCE_ROOT / "temporal.py")
refine_window = load(
f"{package_name}.v3.refine_window",
SOURCE_ROOT / "v3" / "refine_window.py",
)
return temporal, refine_window
TEMPORAL, REFINE_WINDOW = _load_source_contract()
def _load_tensor(record: Mapping[str, Any]) -> torch.Tensor:
return load_file(str(record["path"]), device="cpu")["latent_tensor"]
def _temporal_selection(
tensor: torch.Tensor,
ranges: Sequence[Sequence[int]],
*,
time_dim: int,
) -> tuple[torch.Tensor, torch.Tensor]:
dimension = int(time_dim)
if dimension < 0:
dimension += tensor.ndim
mask = torch.zeros(int(tensor.shape[dimension]), dtype=torch.bool)
for start, stop in ranges:
mask[int(start) : int(stop)] = True
moved = tensor.movedim(dimension, 0)
return moved[mask], moved[~mask]
def _temporal_checks(
before: torch.Tensor,
after: torch.Tensor,
ranges: Sequence[Sequence[int]],
*,
time_dim: int,
expect_inside_change: bool,
) -> dict[str, bool]:
if tuple(before.shape) != tuple(after.shape) or before.dtype != after.dtype:
return {
"shape_dtype": False,
"inside_expected": False,
"outside_exact": False,
}
before_inside, before_outside = _temporal_selection(
before,
ranges,
time_dim=time_dim,
)
after_inside, after_outside = _temporal_selection(
after,
ranges,
time_dim=time_dim,
)
inside_equal = torch.equal(before_inside, after_inside)
return {
"shape_dtype": True,
"inside_expected": (not inside_equal) if expect_inside_change else inside_equal,
"outside_exact": torch.equal(before_outside, after_outside),
}
def _field_set(value: Any) -> set[str]:
return set(_key_paths(value))
def _contains_window_key(value: Any) -> bool:
if isinstance(value, Mapping):
return any(
"window" in str(key).lower() or _contains_window_key(item)
for key, item in value.items()
)
if isinstance(value, list):
return any(_contains_window_key(item) for item in value)
return False
def _configure_parity(template: Mapping[str, Any], *, output_prefix: str) -> dict[str, Any]:
prompt = _configure_g1(template, output_prefix=output_prefix)
prompt["303"]["inputs"].update(
{
"refine_scope": "All",
"refine_scope_index": 1,
"window_start_sec": 0.0,
"window_end_sec": 5.0,
}
)
return prompt
def _configure_window(
template: Mapping[str, Any],
*,
case: WindowCase,
output_prefix: str,
) -> dict[str, Any]:
matrix_case = MatrixCase(
name=case.name,
target=case.target,
chunks=case.chunks,
terminal_merge=case.terminal_merge,
expected_physical_groups=2 if case.terminal_merge else case.chunks,
boundary_seconds=case.boundary_seconds,
)
prompt = _configure_selective(
template,
case=matrix_case,
output_prefix=output_prefix,
)
prompt["302"]["inputs"].update(
{
"refine_scope": "Time Window",
"refine_scope_index": 1,
"window_start_sec": float(case.start_sec),
"window_end_sec": float(case.end_sec),
}
)
prompt["302"]["_meta"]["title"] = (
f"Selective {case.target} / Time Window "
f"[{case.start_sec:g}, {case.end_sec:g})"
)
return prompt
def _run_g1(args: argparse.Namespace, template: Mapping[str, Any]) -> dict[str, Any]:
output_prefix = f"v38_a6/{args.run_tag}/g1_no_window_all_video_only"
prompt = _configure_parity(template, output_prefix=output_prefix)
result, entry = _execute(
args,
name="g1_no_window_all_video_only",
prompt=prompt,
expected_seconds=5.0,
boundaries=(),
)
parity_status = _output_text(entry, "908")
parity = _marker_json(parity_status, "A3C_PARITY_JSON=")
checks = {
"one_physical_group": parity.get("physical_group_count") == 1,
"video_latent_sha_parity": parity.get("legacy_video_sha256")
== parity.get("selective_video_sha256"),
"audio_latent_sha_parity": parity.get("legacy_audio_sha256")
== parity.get("selective_audio_sha256"),
"audio_object_passthrough": all(
parity.get("legacy_audio_object_identity") or []
)
and all(parity.get("selective_audio_object_identity") or []),
"audio_tensor_passthrough": all(
parity.get("legacy_audio_tensor_identity") or []
)
and all(parity.get("selective_audio_tensor_identity") or []),
"seed_parity": parity.get("legacy_refine_group_seeds")
== parity.get("selective_refine_group_seeds"),
"sigmas_parity": parity.get("legacy_schedule_sigma_hash")
== parity.get("selective_schedule_sigma_hash"),
"sampling_parity": parity.get("legacy_sampling_passes_reported")
== parity.get("selective_sampling_passes_reported")
== 1,
"assembly_plan_parity": parity.get("legacy_plan_without_target_identity")
== parity.get("selective_plan_without_target_identity"),
"target_contract": parity.get("selective_target_mode") == "video_only",
"finite": all(parity.get("legacy_video_finite") or [])
and all(parity.get("selective_video_finite") or [])
and all(parity.get("legacy_audio_finite") or [])
and all(parity.get("selective_audio_finite") or []),
"media": int(result["media"]["frames"]) == 120
and abs(float(result["media"]["format_duration"]) - 5.0) <= 0.05
and int(result["media"]["audio_sample_rate"]) == 32000
and int(result["media"]["audio_channels"]) == 2,
"audio_health": bool(result["audio_health"]["pass"]),
}
result.update(
{
"parity_status": parity_status,
"parity": parity,
"checks": checks,
"pass": all(checks.values()),
}
)
case_root = args.evidence_root / "g1_no_window_all_video_only"
(case_root / "result.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False),
encoding="utf-8",
)
if not result["pass"]:
failed = [name for name, passed in checks.items() if not passed]
raise GateFailure(f"A6 G1 parity failed: {failed}")
return result
def _run_window_case(
args: argparse.Namespace,
*,
case: WindowCase,
template: Mapping[str, Any],
baseline_probe: Mapping[str, Any],
expected_seeds: Sequence[int],
expected_contract_fields: set[str],
) -> dict[str, Any]:
output_prefix = f"v38_a6/{args.run_tag}/{case.name}"
prompt = _configure_window(template, case=case, output_prefix=output_prefix)
result, entry = _execute(
args,
name=case.name,
prompt=prompt,
expected_seconds=case.chunks * 5.0,
boundaries=case.boundary_seconds,
)
status = _output_text(entry, "908")
probe = _marker_json(status, "A3C_PROBE_JSON=")
input_video_records = _output_latents(entry, "902", args.output_root)
input_audio_records = _output_latents(entry, "903", args.output_root)
output_video_records = _output_latents(entry, "904", args.output_root)
output_audio_records = _output_latents(entry, "905", args.output_root)
input_videos = [_load_tensor(record) for record in input_video_records]
input_audios = [_load_tensor(record) for record in input_audio_records]
output_videos = [_load_tensor(record) for record in output_video_records]
output_audios = [_load_tensor(record) for record in output_audio_records]
normalized_plan = (probe.get("output_plan") or {}).get("normalized") or {}
window = REFINE_WINDOW.resolve_refine_window(
case.start_sec,
case.end_sec,
normalized_plan,
)
selected = tuple(window.selected_group_indices)
expected_selected = tuple(case.selected_group_indices)
contract = normalized_plan.get("second_pass_contract") or {}
baseline_plan = (baseline_probe.get("output_plan") or {}).get("normalized") or {}
baseline_contract = baseline_plan.get("second_pass_contract") or {}
checks: dict[str, bool] = {
"selected_groups": selected == expected_selected,
"physical_group_count": len(input_videos)
== len(input_audios)
== len(output_videos)
== len(output_audios)
== (2 if case.terminal_merge else case.chunks),
"sampling_count": probe.get("sampling_passes_reported")
== len(expected_selected),
"absolute_seed_parity": probe.get("refine_group_seeds")
== list(expected_seeds),
"sigmas_parity": probe.get("schedule_sigma_hash")
== baseline_probe.get("schedule_sigma_hash"),
"contract_version": probe.get("second_pass_contract_version") == 1,
"window_status": "scope=Time Window" in status
and "timeline=final_visible_output" in status
and "continuation_prefix=protected" in status,
"window_not_in_plan": not _contains_window_key(contract),
"assembly_field_set": set(contract) == set(expected_contract_fields),
"refine_schedule_v1_unchanged": contract.get("refine_schedule")
== baseline_contract.get("refine_schedule"),
"media": int(result["media"]["frames"]) == case.chunks * 120
and abs(
float(result["media"]["format_duration"]) - case.chunks * 5.0
)
<= 0.05
and int(result["media"]["audio_sample_rate"]) == 32000
and int(result["media"]["audio_channels"]) == 2,
"audio_health": bool(result["audio_health"]["pass"]),
"finite": all(probe.get("output_video_finite") or [])
and all(probe.get("output_audio_finite") or []),
}
group_checks: dict[str, dict[str, bool]] = {}
for group_index, (before_v, before_a, after_v, after_a) in enumerate(
zip(input_videos, input_audios, output_videos, output_audios, strict=True)
):
group_window = window.group(group_index)
selected_group = group_index in expected_selected
current: dict[str, bool] = {}
if not selected_group:
current.update(
{
"video_whole_exact": torch.equal(before_v, after_v),
"audio_whole_exact": torch.equal(before_a, after_a),
"video_object_passthrough": bool(
(probe.get("input_video_object_identity") or [])[group_index]
),
"video_tensor_passthrough": bool(
(probe.get("input_video_tensor_identity") or [])[group_index]
),
"audio_object_passthrough": bool(
(probe.get("input_audio_object_identity") or [])[group_index]
),
"audio_tensor_passthrough": bool(
(probe.get("input_audio_tensor_identity") or [])[group_index]
),
}
)
else:
if group_window is None:
raise GateFailure(f"selected group {group_index + 1} has no window")
video_expect_change = case.target in ("Video Only", "Video + Audio")
audio_expect_change = case.target in ("Audio Only", "Video + Audio")
for name, passed in _temporal_checks(
before_v,
after_v,
group_window.video_slot_ranges,
time_dim=2,
expect_inside_change=video_expect_change,
).items():
current[f"video_{name}"] = passed
for name, passed in _temporal_checks(
before_a,
after_a,
group_window.audio_tick_ranges,
time_dim=-1,
expect_inside_change=audio_expect_change,
).items():
current[f"audio_{name}"] = passed
physical_group = contract["physical_groups"][group_index]
trim_frames = int(physical_group.get("trim_prefix_frames", 0))
offsets = TEMPORAL.latent_slot_offsets(int(before_v.shape[2]))
first_video_slot = int(group_window.video_slot_ranges[0][0])
first_audio_tick = int(group_window.audio_tick_ranges[0][0])
current["video_prefix_excluded"] = offsets[first_video_slot] >= trim_frames
current["audio_prefix_excluded"] = (
first_audio_tick >= TEMPORAL.audio_latent_t(trim_frames)
)
group_checks[str(group_index + 1)] = current
checks[f"group_{group_index + 1}"] = all(current.values())
if case.terminal_merge:
terminal = list(probe.get("physical_groups") or [])[-1]
checks["terminal_atomic"] = (
terminal.get("logical_chunks") == [2, 3]
and terminal.get("terminal_merged") is True
and probe.get("sampling_passes_reported") == 1
and selected == (1,)
)
result.update(
{
"case": asdict(case),
"probe_status": status,
"probe": probe,
"window_contract": REFINE_WINDOW.serializable_window_contract(window),
"input_video_latents": input_video_records,
"input_audio_latents": input_audio_records,
"output_video_latents": output_video_records,
"output_audio_latents": output_audio_records,
"group_checks": group_checks,
"checks": checks,
"pass": all(checks.values()),
}
)
case_root = args.evidence_root / case.name
(case_root / "result.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False),
encoding="utf-8",
)
if not result["pass"]:
failed = [name for name, passed in checks.items() if not passed]
raise GateFailure(f"{case.name} failed: {failed}")
return result
def _load_resumed_g1(path: Path) -> dict[str, Any]:
result = json.loads(path.read_text(encoding="utf-8"))
if not bool(result.get("pass")) or not all((result.get("checks") or {}).values()):
raise GateFailure(f"resumed G1 is not PASS: {path}")
result["resumed_without_resampling"] = True
path.with_name("result.revalidated.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False),
encoding="utf-8",
)
return result
def _load_resumed_window_result(
path: Path,
*,
expected_seeds: Sequence[int],
expected_contract_fields: set[str],
) -> dict[str, Any]:
result = json.loads(path.read_text(encoding="utf-8"))
probe = result.get("probe") or {}
contract = (
((probe.get("output_plan") or {}).get("normalized") or {}).get(
"second_pass_contract"
)
or {}
)
checks = dict(result.get("checks") or {})
checks["absolute_seed_parity"] = probe.get("refine_group_seeds") == list(
expected_seeds
)
checks["assembly_field_set"] = set(contract) == set(expected_contract_fields)
result["checks"] = checks
result["pass"] = all(checks.values())
result["resumed_without_resampling"] = True
if not result["pass"]:
failed = [name for name, passed in checks.items() if not passed]
raise GateFailure(f"resumed window validation failed: {failed}")
path.with_name("result.revalidated.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False),
encoding="utf-8",
)
return result
def run(args: argparse.Namespace) -> dict[str, Any]:
args.evidence_root.mkdir(parents=True, exist_ok=True)
t2_template = json.loads(args.t2_template.read_text(encoding="utf-8"))
terminal_template = json.loads(args.terminal_template.read_text(encoding="utf-8"))
baseline_summary = json.loads(args.a3c_baseline.read_text(encoding="utf-8"))
nonterminal_baseline = _load_a3c_baseline(
args.a3c_baseline,
"g4_video_audio_3x5",
)["probe"]
one_group_av_baseline = _load_a3c_baseline(
args.a3c_baseline,
"g3_video_audio_1x5",
)["probe"]
terminal_baseline = _load_a3c_baseline(
args.a3c_baseline,
"g5_video_audio_terminal",
)["probe"]
summary: dict[str, Any] = {
"format": "h3-continuum-v38-a6-temporal-window-refine-gpu-matrix-v1",
"server": args.server,
"backend_pid": args.backend_pid,
"run_tag": args.run_tag,
"a3c_baseline": str(args.a3c_baseline),
"settings": {
"resolution": "576x576",
"first_pass_steps": 8,
"refine_steps": 10,
"refine_denoise": 0.35,
"continuity": "Balanced — 22 frames",
"audio_continuity": True,
"run_storage": "Off",
"sampler": "res_multistep",
"scheduler": "simple",
"sage": True,
},
"results": {},
"pass": False,
"production_status": "Experimental; audio-targeted modes remain HOLD",
}
summary_path = args.evidence_root / "summary.json"
try:
if args.resume_g1 is not None:
summary["results"]["g1_no_window_all_video_only"] = _load_resumed_g1(
args.resume_g1
)
else:
summary["results"]["g1_no_window_all_video_only"] = _run_g1(
args,
t2_template,
)
video_only_seeds = list(
(((baseline_summary.get("results") or {}).get("g1_video_only_parity") or {})
.get("parity", {})
.get("selective_refine_group_seeds")
or [])
)
resume_results: dict[str, Path] = {}
for path in args.resume_result or ():
payload = json.loads(path.read_text(encoding="utf-8"))
name = str((payload.get("case") or {}).get("name") or "")
if not name or name in resume_results:
raise GateFailure(f"invalid or duplicate resumed result: {path}")
resume_results[name] = path
for case in WINDOW_CASES:
template = terminal_template if case.terminal_merge else t2_template
if case.target == "Video Only":
baseline = one_group_av_baseline
expected_seeds = video_only_seeds
expected_fields = VIDEO_ONLY_CONTRACT_FIELDS
elif case.terminal_merge:
baseline = terminal_baseline
expected_seeds = [
list(terminal_baseline.get("refine_group_seeds") or [])[index]
for index in case.selected_group_indices
]
expected_fields = set(
(
(terminal_baseline.get("output_plan") or {})
.get("normalized", {})
.get("second_pass_contract", {})
)
)
elif case.chunks == 1:
baseline = one_group_av_baseline
expected_seeds = list(
one_group_av_baseline.get("refine_group_seeds") or []
)
expected_fields = set(
(
(one_group_av_baseline.get("output_plan") or {})
.get("normalized", {})
.get("second_pass_contract", {})
)
)
else:
baseline = nonterminal_baseline
expected_seeds = [
list(nonterminal_baseline.get("refine_group_seeds") or [])[index]
for index in case.selected_group_indices
]
expected_fields = set(
(
(nonterminal_baseline.get("output_plan") or {})
.get("normalized", {})
.get("second_pass_contract", {})
)
)
if case.name in resume_results:
result = _load_resumed_window_result(
resume_results[case.name],
expected_seeds=expected_seeds,
expected_contract_fields=expected_fields,
)
else:
result = _run_window_case(
args,
case=case,
template=template,
baseline_probe=baseline,
expected_seeds=expected_seeds,
expected_contract_fields=expected_fields,
)
summary["results"][case.name] = result
summary["pass"] = True
summary["status"] = "A6 Temporal Window Refine GPU Experimental PASS"
return summary
except Exception as exc:
summary["failure_type"] = type(exc).__name__
summary["failure"] = str(exc)
summary["status"] = "A6 STOP / Production unchanged"
return summary
finally:
summary_path.write_text(
json.dumps(summary, indent=2, ensure_ascii=False),
encoding="utf-8",
)
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--server", default="http://127.0.0.6:8190")
parser.add_argument("--backend-pid", type=int, required=True)
parser.add_argument("--run-tag", required=True)
parser.add_argument("--evidence-root", type=Path, required=True)
parser.add_argument("--t2-template", type=Path, required=True)
parser.add_argument("--terminal-template", type=Path, required=True)
parser.add_argument("--a3c-baseline", type=Path, required=True)
parser.add_argument("--resume-g1", type=Path)
parser.add_argument("--resume-result", type=Path, action="append")
parser.add_argument(
"--output-root",
type=Path,
default=Path(r"D:\output\video\comfy_video"),
)
parser.add_argument("--ffmpeg", default=shutil.which("ffmpeg"))
parser.add_argument("--ffprobe", default=shutil.which("ffprobe"))
parser.add_argument("--timeout-seconds", type=float, default=3600.0)
args = parser.parse_args()
if not args.ffmpeg or not args.ffprobe:
raise SystemExit("ffmpeg and ffprobe are required")
summary = run(args)
print(json.dumps(summary, indent=2, ensure_ascii=False), flush=True)
return 0 if summary["pass"] else 2
if __name__ == "__main__":
raise SystemExit(main())
+632
View File
@@ -0,0 +1,632 @@
"""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 <Picture 1>, "
"<Picture 2>, and <Picture 3>."
)
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 <Video 1> while preserving continuity."
)
prompt["950"] = {
"class_type": "H3ContinuumLoadVideo",
"inputs": {
"enable_video": True,
"file": reference_video,
"force_rate": 24.0,
},
"_meta": {"title": "A7a High-row Video Guide"},
}
inputs["reference_video_1"] = ["950", 0]
inputs["video_reference_size"] = "Efficient - 0.4 MP"
_video_decode_id, video_decode = _find_one(prompt, "VAEDecode")
_audio_decode_id, audio_decode = _find_one(prompt, "VAEDecodeAudio")
_assemble_id, assemble = _find_one(prompt, "H3ContinuumAssembleSeamV35")
video_decode["inputs"]["samples"] = [sampler_id, 0]
audio_decode["inputs"]["samples"] = [sampler_id, 1]
assemble["inputs"]["assembly_plan"] = [sampler_id, 2]
prompt["900"] = {
"class_type": "PreviewAny",
"inputs": {"source": [sampler_id, 3]},
}
prompt["902"] = {
"class_type": "SaveLatent",
"inputs": {
"samples": [sampler_id, 0],
"filename_prefix": f"{output_prefix}_video",
},
}
prompt["903"] = {
"class_type": "SaveLatent",
"inputs": {
"samples": [sampler_id, 1],
"filename_prefix": f"{output_prefix}_audio",
},
}
prompt["907"] = {
"class_type": "H3A3cSelectiveRefineProbe",
"inputs": {
"input_video_latents": [sampler_id, 0],
"input_audio_latents": [sampler_id, 1],
"output_video_latents": [sampler_id, 0],
"output_audio_latents": [sampler_id, 1],
"input_assembly_plan": [sampler_id, 2],
"output_assembly_plan": [sampler_id, 2],
"refine_status": [sampler_id, 3],
"target_label": "Video Only",
},
}
prompt["908"] = {
"class_type": "PreviewAny",
"inputs": {"source": ["907", 0]},
}
return prompt
def _run_once(
args: argparse.Namespace,
*,
case: FirstPassCase,
template: Mapping[str, Any],
diagnostics: str,
) -> dict[str, Any]:
variant = "basic" if diagnostics == "Basic" else "detailed"
name = f"{case.name}_{variant}"
output_prefix = f"v38_a7a/{args.run_tag}/{name}"
prompt = _configure_first_pass(
template,
case=case,
diagnostics=diagnostics,
output_prefix=output_prefix,
reference_image=args.reference_image,
reference_video=args.reference_video,
)
result, entry = _execute(
args,
name=name,
prompt=prompt,
expected_seconds=case.expected_seconds,
boundaries=tuple(float(index * 5) for index in range(1, case.chunks)),
)
sampler_status = _output_text(entry, "900")
probe_status = _output_text(entry, "908")
probe = _marker_json(probe_status, "A3C_PROBE_JSON=")
video_records = _output_latents(entry, "902", args.output_root)
audio_records = _output_latents(entry, "903", args.output_root)
result.update(
{
"diagnostics": diagnostics,
"sampler_status": sampler_status,
"probe": probe,
"video_latents": video_records,
"audio_latents": audio_records,
"packed_rows": (
_parse_packed_rows(sampler_status)
if diagnostics == "Detailed Report"
else None
),
}
)
checks = {
"latent_count": len(video_records)
== len(audio_records)
== case.expected_groups,
"saved_video_sha_matches_probe": [item["tensor_sha256"] for item in video_records]
== list(probe.get("input_video_sha256") or []),
"saved_audio_sha_matches_probe": [item["tensor_sha256"] for item in audio_records]
== list(probe.get("input_audio_sha256") or []),
"finite": all(probe.get("output_video_finite") or [])
and all(probe.get("output_audio_finite") or []),
"media_frames": int(result["media"]["frames"])
== int(case.expected_seconds * 24),
"media_duration": abs(
float(result["media"]["format_duration"]) - case.expected_seconds
)
<= 0.05,
"media_audio": int(result["media"]["audio_sample_rate"]) == 32000
and int(result["media"]["audio_channels"]) == 2,
"audio_health": bool(result["audio_health"]["pass"]),
"run_storage_off": prompt[_find_one(prompt, "H3ContinuumSamplerV38")[0]][
"inputs"
]["run_storage"]
== "Off",
}
if diagnostics == "Basic":
checks["planner_absent_in_basic"] = "Packed-row plan [A7a" not in sampler_status
else:
packed = result["packed_rows"]
rows = list(packed["groups"])
expected_order = list(range(1, case.expected_groups + 1))
actual_peak = min(
(group["physical_group"] for group in rows if group["total"] == max(item["total"] for item in rows)),
default=None,
)
checks.update(
{
"execution_order": packed["execution_order"] == expected_order,
"max_concurrent_one": packed["max_concurrent_physical_groups"] == 1,
"all_groups_observed": packed["observed_groups"]
== packed["recorded_groups"]
== len(rows)
== case.expected_groups,
"component_sum_equals_seq_len": all(
group["component_sum"] == group["total"] for group in rows
),
"one_valid_target_pair_per_observed_layout": all(
group["target_video"] > 0 and group["target_audio"] > 0
for group in rows
),
"peak_row_candidate": packed["peak_row_candidate"] == actual_peak,
"peak_row_count": packed["peak_total_rows"]
== max((group["total"] for group in rows), default=None),
"memory_observation_only": all(
group["point_in_time_memory_observation"] for group in rows
),
"report_contract": all(packed["report_contract"].values()),
"terminal_atomic": (
not case.terminal_merge
or (
rows[-1]["logical_chunks"] == [2, 3]
and rows[-1]["terminal_atomic"] is True
)
),
}
)
result["checks"] = checks
result["pass"] = all(checks.values())
result_path = args.evidence_root / name / "result.json"
result_path.write_text(
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
)
if not result["pass"]:
failed = [key for key, value in checks.items() if not value]
raise GateFailure(f"{name} failed: {failed}")
return result
def _baseline_from_result(result: Mapping[str, Any]) -> dict[str, Any]:
probe = result["probe"]
return {
"origin": str(result["history"]),
"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": copy.deepcopy(result.get("decoded")),
}
def _load_completed_result(path: Path) -> dict[str, Any]:
result = json.loads(path.read_text(encoding="utf-8"))
checks = result.get("checks") or {}
if not bool(result.get("pass")) or not checks or not all(checks.values()):
raise GateFailure(f"existing result is not a completed run-level PASS: {path}")
result["resumed_without_resampling"] = True
return result
def _parity_checks(
detailed: Mapping[str, Any], baseline: Mapping[str, Any]
) -> dict[str, bool]:
probe = detailed["probe"]
checks = {
"video_latent_sha_parity": list(probe.get("input_video_sha256") or [])
== list(baseline["video_sha256"]),
"audio_latent_sha_parity": list(probe.get("input_audio_sha256") or [])
== list(baseline["audio_sha256"]),
"assembly_plan_identity": _cross_run_plan_identity(
(probe.get("input_plan") or {}).get("normalized")
)
== _cross_run_plan_identity(baseline.get("assembly_normalized")),
}
if baseline.get("decoded") is not None:
checks["decoded_video_parity"] = (
detailed.get("decoded") or {}
).get("video_sha256") == baseline["decoded"].get("video_sha256")
checks["decoded_audio_parity"] = (
detailed.get("decoded") or {}
).get("audio_pcm_sha256") == baseline["decoded"].get("audio_pcm_sha256")
return checks
def run(args: argparse.Namespace) -> dict[str, Any]:
args.evidence_root.mkdir(parents=True, exist_ok=True)
t2_template = json.loads(args.t2_template.read_text(encoding="utf-8"))
terminal_template = json.loads(args.terminal_template.read_text(encoding="utf-8"))
cases = (
FirstPassCase("g1_1x5_t2va", 1, False, "none", "same_runtime_basic"),
FirstPassCase(
"g2_3x5_continuation",
3,
False,
"none",
"accepted_a6",
args.a6_g2_history,
),
FirstPassCase(
"g3_fl2va_terminal",
3,
True,
"none",
"accepted_a6",
args.a6_g3_history,
),
FirstPassCase(
"g4_reference_images_3",
1,
False,
"reference_images_3",
"same_runtime_basic",
),
FirstPassCase(
"g5_reference_video_high_row",
1,
False,
"reference_video",
"same_runtime_basic",
),
)
summary: dict[str, Any] = {
"format": "h3-continuum-v38-a7a-packed-row-gpu-matrix-v1",
"server": args.server,
"backend_pid": args.backend_pid,
"run_tag": args.run_tag,
"settings": {
"resolution": "576x576",
"steps": 8,
"continuity": "Balanced - 22 frames",
"audio_continuity": True,
"run_storage": "Off",
"sampler": "res_multistep",
"scheduler": "simple",
"sage": True,
},
"baseline_policy": {
"g1_g4_g5": "same-runtime Basic vs Detailed",
"g2_g3": "accepted pre-A7 A6 First Pass latent/Assembly evidence",
"decoded_parity": "required where same-runtime Basic baseline exists",
},
"results": {},
"pass": False,
"production_status": "read-only Experimental; A7b execution policy HOLD",
}
summary_path = args.evidence_root / "summary.json"
try:
g1_rows = None
for case in cases:
template = terminal_template if case.terminal_merge else t2_template
basic = None
if case.baseline_kind == "same_runtime_basic":
basic_path = args.evidence_root / f"{case.name}_basic" / "result.json"
basic = (
_load_completed_result(basic_path)
if args.resume_existing and basic_path.exists()
else _run_once(
args,
case=case,
template=template,
diagnostics="Basic",
)
)
baseline = _baseline_from_result(basic)
else:
if case.baseline_history is None:
raise GateFailure(f"accepted baseline path missing for {case.name}")
baseline = _baseline_from_a6(case.baseline_history)
detailed_path = (
args.evidence_root / f"{case.name}_detailed" / "result.json"
)
detailed = (
_load_completed_result(detailed_path)
if args.resume_existing and detailed_path.exists()
else _run_once(
args,
case=case,
template=template,
diagnostics="Detailed Report",
)
)
parity = _parity_checks(detailed, baseline)
extra: dict[str, bool] = {}
current_rows = detailed["packed_rows"]["groups"]
if case.name == "g1_1x5_t2va":
g1_rows = current_rows[0]
elif case.conditioning in {"reference_images_3", "reference_video"}:
if g1_rows is None:
raise GateFailure("G1 row baseline is unavailable")
extra["condition_video_rows_increased_vs_g1"] = (
current_rows[0]["condition_video"] > g1_rows["condition_video"]
)
all_checks = {**parity, **extra}
case_result = {
"case": {
"name": case.name,
"chunks": case.chunks,
"terminal_merge": case.terminal_merge,
"conditioning": case.conditioning,
"baseline_kind": case.baseline_kind,
},
"baseline": baseline,
"basic": basic,
"detailed": detailed,
"parity_checks": parity,
"conditioning_checks": extra,
"pass": all(all_checks.values()),
}
summary["results"][case.name] = case_result
summary_path.write_text(
json.dumps(summary, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(
f"A7a {case.name}: pass={case_result['pass']} "
f"peak-row-candidate={detailed['packed_rows']['peak_row_candidate']} "
f"rows={detailed['packed_rows']['peak_total_rows']}",
flush=True,
)
if not case_result["pass"]:
failed = [key for key, value in all_checks.items() if not value]
raise GateFailure(f"{case.name} parity failed: {failed}")
summary["pass"] = True
summary["status"] = "A7a Packed-row Observability GPU Experimental PASS"
return summary
except Exception as exc:
summary["failure_type"] = type(exc).__name__
summary["failure"] = str(exc)
summary["status"] = "A7a STOP / Production unchanged"
return summary
finally:
summary_path.write_text(
json.dumps(summary, indent=2, ensure_ascii=False), encoding="utf-8"
)
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--server", default="http://127.0.0.6:8190")
parser.add_argument("--backend-pid", type=int, required=True)
parser.add_argument("--run-tag", required=True)
parser.add_argument("--evidence-root", type=Path, required=True)
parser.add_argument("--t2-template", type=Path, required=True)
parser.add_argument("--terminal-template", type=Path, required=True)
parser.add_argument("--a6-g2-history", type=Path, required=True)
parser.add_argument("--a6-g3-history", type=Path, required=True)
parser.add_argument("--reference-image", required=True)
parser.add_argument("--reference-video", required=True)
parser.add_argument("--resume-existing", action="store_true")
parser.add_argument(
"--output-root",
type=Path,
default=Path(r"D:\output\video\comfy_video"),
)
parser.add_argument("--ffmpeg", default=shutil.which("ffmpeg"))
parser.add_argument("--ffprobe", default=shutil.which("ffprobe"))
parser.add_argument("--timeout-seconds", type=float, default=3600.0)
args = parser.parse_args()
if not args.ffmpeg or not args.ffprobe:
raise SystemExit("ffmpeg and ffprobe are required")
summary = run(args)
print(
json.dumps(
{
"status": summary.get("status"),
"pass": summary.get("pass"),
"failure": summary.get("failure"),
"summary": str(args.evidence_root / "summary.json"),
},
ensure_ascii=False,
),
flush=True,
)
return 0 if summary["pass"] else 2
if __name__ == "__main__":
raise SystemExit(main())
+571
View File
@@ -0,0 +1,571 @@
"""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"<Picture {index}>")
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 <Video 1> while "
"preserving continuity."
)
_save_video_id, save_video = _find_one(prompt, "SaveVideo")
save_video["inputs"]["filename_prefix"] = output_prefix
prompt["902"]["inputs"]["filename_prefix"] = output_prefix + "_video"
prompt["903"]["inputs"]["filename_prefix"] = output_prefix + "_audio"
# The accepted A7a template already routes sampler outputs through Core
# decode, assemble, SaveLatent, and the read-only A3c evidence probe.
for node_id in ("900", "902", "903", "907", "908"):
if node_id not in prompt:
raise GateFailure(f"accepted A7a evidence node {node_id} is missing")
prompt["900"]["inputs"]["source"] = [sampler_id, 3]
prompt["902"]["inputs"]["samples"] = [sampler_id, 0]
prompt["903"]["inputs"]["samples"] = [sampler_id, 1]
for name, slot in (
("input_video_latents", 0),
("output_video_latents", 0),
("input_audio_latents", 1),
("output_audio_latents", 1),
("input_assembly_plan", 2),
("output_assembly_plan", 2),
("refine_status", 3),
):
prompt["907"]["inputs"][name] = [sampler_id, slot]
return prompt
def _latest_manifest(output_root: Path, run_name: str) -> dict[str, Any]:
revision_root = output_root / "h3_continuum" / "runs" / run_name / "revisions"
manifests = sorted(
revision_root.glob("*/manifest.json"),
key=lambda path: path.stat().st_mtime_ns,
)
if not manifests:
raise GateFailure(f"Run Storage manifest is missing for {run_name}")
path = manifests[-1]
manifest = json.loads(path.read_text(encoding="utf-8"))
return {"path": str(path), "manifest": manifest}
def _one_seed(status: str) -> int:
match = SEED_PATTERN.search(status)
if match is None:
raise GateFailure("effective chunk seed is missing from sampler status")
return int(match.group(1))
def _image_geometry(status: str) -> tuple[int, int] | None:
match = IMAGE_GEOMETRY_PATTERN.search(status)
return None if match is None else (int(match.group(1)), int(match.group(2)))
def _run_variant(
args: argparse.Namespace,
*,
template: Mapping[str, Any],
case: A7bCase,
action: str,
) -> dict[str, Any]:
variant = "disabled" if action == ACTION_DISABLED else "reduce"
name = f"{case.name}_{variant}"
run_name = f"v38_a7b_{args.run_tag}_{name}"[:96]
output_prefix = f"v38_a7b/{args.run_tag}/{name}"
prompt = _configure_prompt(
template,
case=case,
action=action,
output_prefix=output_prefix,
run_name=run_name,
reference_image=args.reference_image,
)
result, entry = _execute(
args,
name=name,
prompt=prompt,
expected_seconds=5.0,
boundaries=(),
)
status = _output_text(entry, "900")
probe = _marker_json(_output_text(entry, "908"), "A3C_PROBE_JSON=")
video_records = _output_latents(entry, "902", args.output_root)
audio_records = _output_latents(entry, "903", args.output_root)
packed_rows = _parse_packed_rows(status)
storage = _latest_manifest(args.output_root, run_name)
manifest = storage["manifest"]
contract = manifest.get("contract") or {}
global_contract = contract.get("global") or {}
chunk_contracts = contract.get("chunk_contracts") or []
sampler_id, sampler = _find_one(prompt, "H3ContinuumSamplerV38MemoryPolicyExperimental")
checks = {
"one_physical_group": len(video_records) == len(audio_records) == 1,
"saved_video_sha_matches_probe": [item["tensor_sha256"] for item in video_records]
== list(probe.get("input_video_sha256") or []),
"saved_audio_sha_matches_probe": [item["tensor_sha256"] for item in audio_records]
== list(probe.get("input_audio_sha256") or []),
"finite": all(probe.get("output_video_finite") or [])
and all(probe.get("output_audio_finite") or []),
"media": int(result["media"]["frames"]) == 120
and abs(float(result["media"]["format_duration"]) - 5.0) <= 0.05
and int(result["media"]["audio_sample_rate"]) == 32000
and int(result["media"]["audio_channels"]) == 2,
"audio_health": bool(result["audio_health"]["pass"]),
"storage_complete": manifest.get("status") == "complete",
"one_chunk_contract": len(chunk_contracts) == 1,
"packed_rows_valid": packed_rows["observed_groups"] == 1
and len(packed_rows["groups"]) == 1
and packed_rows["groups"][0]["component_sum"]
== packed_rows["groups"][0]["total"],
"experimental_sampler_only": sampler["class_type"]
== "H3ContinuumSamplerV38MemoryPolicyExperimental",
"policy_status": (
POLICY_STATUS not in status
if action == ACTION_DISABLED
else POLICY_STATUS in status
and "automatic MODEL unload was not executed" in status
and "automatic attention/backend switching was not executed" in status
and "external MODEL wrapper was preserved" in status
),
}
result.update(
{
"action": action,
"run_name": run_name,
"sampler_id": sampler_id,
"status": status,
"seed": _one_seed(status),
"image_geometry": _image_geometry(status),
"probe": probe,
"video_latents": video_records,
"audio_latents": audio_records,
"packed_rows": packed_rows,
"run_storage": storage,
"global_hash": (
str(chunk_contracts[0].get("global_hash"))
if chunk_contracts
else ""
),
"sigmas_hash": str(
(global_contract.get("sigmas") or {}).get("exact_sha256") or ""
),
"model_contract_hash": _sha256_json(global_contract.get("model")),
"reference_contract": copy.deepcopy(global_contract.get("reference")),
"reference_video_contract": copy.deepcopy(
global_contract.get("reference_video")
),
"assembly_normalized": copy.deepcopy(
(probe.get("input_plan") or {}).get("normalized")
),
"checks": checks,
"pass": all(checks.values()),
}
)
result_path = args.evidence_root / name / "result.json"
result_path.write_text(
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
)
if not result["pass"]:
failed = [key for key, value in checks.items() if not value]
raise GateFailure(f"{name} failed: {failed}")
return result
def _latent_hashes(result: Mapping[str, Any], stream: str) -> list[str]:
return list((result.get("probe") or {}).get(f"input_{stream}_sha256") or [])
def _plan_without_runtime(value: Any) -> Any:
return _cross_run_plan_identity(value)
def _effective_contract_checks(
case: A7bCase,
disabled: Mapping[str, Any],
reduced: Mapping[str, Any],
) -> dict[str, bool]:
base_rows = disabled["packed_rows"]["groups"][0]
reduced_rows = reduced["packed_rows"]["groups"][0]
checks = {
"global_identity_changed": disabled["global_hash"]
and reduced["global_hash"]
and disabled["global_hash"] != reduced["global_hash"],
"actual_total_rows_decreased": reduced_rows["total"] < base_rows["total"],
"condition_video_rows_decreased": reduced_rows["condition_video"]
< base_rows["condition_video"],
"output_variation_expected": _latent_hashes(disabled, "video")
!= _latent_hashes(reduced, "video")
or _latent_hashes(disabled, "audio") != _latent_hashes(reduced, "audio"),
"assembly_semantics_unchanged": _plan_without_runtime(
disabled["assembly_normalized"]
)
== _plan_without_runtime(reduced["assembly_normalized"]),
"seed_unchanged": disabled["seed"] == reduced["seed"],
"sigmas_unchanged": disabled["sigmas_hash"]
and disabled["sigmas_hash"] == reduced["sigmas_hash"],
"external_model_wrapper_unchanged": disabled["model_contract_hash"]
== reduced["model_contract_hash"],
}
if case.is_image:
base_contract = disabled.get("reference_contract") or {}
reduced_contract = reduced.get("reference_contract") or {}
base_geometry = disabled.get("image_geometry")
reduced_geometry = reduced.get("image_geometry")
checks.update(
{
"image_mode_one_step": base_contract.get("size_mode") == IMAGE_MAX
and reduced_contract.get("size_mode") == IMAGE_MATCH,
"image_contract_hash_changed": base_contract.get("combined_hash")
!= reduced_contract.get("combined_hash"),
"image_geometry_decreased": base_geometry is not None
and reduced_geometry is not None
and reduced_geometry[0] * reduced_geometry[1]
< base_geometry[0] * base_geometry[1],
}
)
elif case.is_video:
base_contract = disabled.get("reference_video_contract") or {}
reduced_contract = reduced.get("reference_video_contract") or {}
checks.update(
{
"video_mode_one_step": base_contract.get("size_mode")
== VIDEO_BALANCED
and reduced_contract.get("size_mode") == VIDEO_EFFICIENT,
"video_contract_hash_changed": base_contract.get("combined_hash")
!= reduced_contract.get("combined_hash"),
"video_geometry_decreased": int(reduced_contract.get("target_width", 0))
* int(reduced_contract.get("target_height", 0))
< int(base_contract.get("target_width", 0))
* int(base_contract.get("target_height", 0)),
"video_frame_count_unchanged": base_contract.get("frame_count")
== reduced_contract.get("frame_count"),
}
)
return checks
def _g1_checks(
result: Mapping[str, Any],
*,
baseline_result: Mapping[str, Any],
baseline_prompt: Mapping[str, Any],
) -> dict[str, bool]:
baseline_probe = baseline_result.get("probe") or {}
current_probe = result.get("probe") or {}
_baseline_sampler_id, baseline_sampler = _find_one(
baseline_prompt, "H3ContinuumSamplerV38"
)
current_prompt = json.loads(Path(result["prompt"]).read_text(encoding="utf-8"))
_current_sampler_id, current_sampler = _find_one(
current_prompt, "H3ContinuumSamplerV38MemoryPolicyExperimental"
)
return {
"video_latent_bit_exact": list(current_probe.get("input_video_sha256") or [])
== list(baseline_probe.get("input_video_sha256") or []),
"audio_latent_bit_exact": list(current_probe.get("input_audio_sha256") or [])
== list(baseline_probe.get("input_audio_sha256") or []),
"assembly_semantics_bit_exact": _plan_without_runtime(
(current_probe.get("input_plan") or {}).get("normalized")
)
== _plan_without_runtime(
(baseline_probe.get("input_plan") or {}).get("normalized")
),
"decoded_video_bit_exact": (result.get("decoded") or {}).get("video_sha256")
== (baseline_result.get("decoded") or {}).get("video_sha256"),
"decoded_audio_bit_exact": (result.get("decoded") or {}).get(
"audio_pcm_sha256"
)
== (baseline_result.get("decoded") or {}).get("audio_pcm_sha256"),
"seed_input_unchanged": current_sampler["inputs"].get("base_seed")
== baseline_sampler["inputs"].get("base_seed"),
"sampler_link_unchanged": current_sampler["inputs"].get("sampler")
== baseline_sampler["inputs"].get("sampler"),
"sigmas_link_unchanged": current_sampler["inputs"].get("sigmas")
== baseline_sampler["inputs"].get("sigmas"),
"no_reference_contract": result.get("reference_contract") is None
and result.get("reference_video_contract") is None,
"disabled_status_no_action": POLICY_STATUS not in str(result.get("status")),
}
def run(args: argparse.Namespace) -> dict[str, Any]:
args.evidence_root.mkdir(parents=True, exist_ok=True)
template = json.loads(args.template.read_text(encoding="utf-8"))
baseline_result = json.loads(args.a7a_g1_result.read_text(encoding="utf-8"))
baseline_prompt = json.loads(
Path(baseline_result["prompt"]).read_text(encoding="utf-8")
)
cases = (
A7bCase("g2_image_1", "image", reference_count=1),
A7bCase("g3_video_short", "video", video_file=args.short_video),
A7bCase("g4_images_3", "image", reference_count=3),
A7bCase("g5_video_high_row", "video", video_file=args.high_video),
)
summary: dict[str, Any] = {
"format": "h3-continuum-v38-a7b-memory-action-gpu-matrix-v1",
"server": args.server,
"backend_pid": args.backend_pid,
"run_tag": args.run_tag,
"settings": {
"resolution": "576x576",
"steps": 8,
"fixed_seed": True,
"run_storage": "Save + Auto Resume with unique run names",
"policy": "explicit opt-in one-step Reference downgrade",
"auto_unload": "not executed; advisory-only",
"backend_switch": "not executed; advisory-only",
},
"results": {},
"pass": False,
"production_status": "unchanged; Experimental path only; A5 HOLD",
}
summary_path = args.evidence_root / "summary.json"
try:
g1_case = A7bCase("g1_disabled", "none")
g1 = _run_variant(
args,
template=template,
case=g1_case,
action=ACTION_DISABLED,
)
g1_parity = _g1_checks(
g1,
baseline_result=baseline_result,
baseline_prompt=baseline_prompt,
)
summary["results"][g1_case.name] = {
"baseline": str(args.a7a_g1_result),
"disabled": g1,
"checks": g1_parity,
"pass": all(g1_parity.values()),
}
summary_path.write_text(
json.dumps(summary, indent=2, ensure_ascii=False), encoding="utf-8"
)
if not all(g1_parity.values()):
failed = [key for key, value in g1_parity.items() if not value]
raise GateFailure(f"G1 Disabled parity failed: {failed}")
print("A7b G1 Disabled parity PASS", flush=True)
for case in cases:
disabled = _run_variant(
args,
template=template,
case=case,
action=ACTION_DISABLED,
)
reduced = _run_variant(
args,
template=template,
case=case,
action=ACTION_REDUCE,
)
checks = _effective_contract_checks(case, disabled, reduced)
case_result = {
"case": {
"name": case.name,
"conditioning": case.conditioning,
"reference_count": case.reference_count,
"video_file": case.video_file,
},
"disabled": disabled,
"reduced": reduced,
"checks": checks,
"pass": all(checks.values()),
}
summary["results"][case.name] = case_result
summary_path.write_text(
json.dumps(summary, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(
f"A7b {case.name}: pass={case_result['pass']} "
f"rows={disabled['packed_rows']['peak_total_rows']}->"
f"{reduced['packed_rows']['peak_total_rows']}",
flush=True,
)
if not case_result["pass"]:
failed = [key for key, value in checks.items() if not value]
raise GateFailure(f"{case.name} failed: {failed}")
summary["pass"] = True
summary["status"] = "A7b v1 Memory Action Policy GPU Experimental PASS"
return summary
except Exception as exc:
summary["failure_type"] = type(exc).__name__
summary["failure"] = str(exc)
summary["status"] = "A7b STOP / Production unchanged / A5 HOLD"
return summary
finally:
summary_path.write_text(
json.dumps(summary, indent=2, ensure_ascii=False), encoding="utf-8"
)
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--server", default="http://127.0.0.6:8190")
parser.add_argument("--backend-pid", type=int, required=True)
parser.add_argument("--run-tag", required=True)
parser.add_argument("--evidence-root", type=Path, required=True)
parser.add_argument("--template", type=Path, required=True)
parser.add_argument("--a7a-g1-result", type=Path, required=True)
parser.add_argument("--reference-image", required=True)
parser.add_argument("--short-video", required=True)
parser.add_argument("--high-video", required=True)
parser.add_argument(
"--output-root",
type=Path,
default=Path(r"D:\output\video\comfy_video"),
)
parser.add_argument("--ffmpeg", default=shutil.which("ffmpeg"))
parser.add_argument("--ffprobe", default=shutil.which("ffprobe"))
parser.add_argument("--timeout-seconds", type=float, default=3600.0)
args = parser.parse_args()
if not args.ffmpeg or not args.ffprobe:
raise SystemExit("ffmpeg and ffprobe are required")
summary = run(args)
print(
json.dumps(
{
"status": summary.get("status"),
"pass": summary.get("pass"),
"failure": summary.get("failure"),
"summary": str(args.evidence_root / "summary.json"),
},
ensure_ascii=False,
),
flush=True,
)
return 0 if summary["pass"] else 2
if __name__ == "__main__":
raise SystemExit(main())
@@ -0,0 +1,788 @@
"""Run the A8a read-only Adaptive Continuity GPU Matrix.
The runner is evidence-only. It compares the public V3.8 Production path with
accepted pre-A8 evidence, exercises the accepted Issue #13 same-prompt stress
path, and splits the Review/Run Storage gate across a real backend restart.
"""
from __future__ import annotations
import argparse
import copy
import importlib.util
import json
from pathlib import Path
import re
import shutil
import sys
from typing import Any, Mapping
import uuid
TOOLS_ROOT = Path(__file__).resolve().parent
REPO_ROOT = TOOLS_ROOT.parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
if str(REPO_ROOT.parent) not in sys.path:
sys.path.insert(0, str(REPO_ROOT.parent))
from v38_a2_audio_only_gpu_canary import _history_entry, _output_latents, _output_text
from v38_a2_audio_only_gpu_canary import _audio_health
from v38_a3c_selective_refine_gpu_matrix import (
GateFailure,
_decoded_stream_hash,
_execute,
_marker_json,
)
from v38_a7a_packed_row_gpu_matrix import (
FirstPassCase,
_configure_first_pass,
_cross_run_plan_identity,
)
from v38_review_phase_f_gpu_gate_runner import _manifest_snapshot
from v38_review_phase_f_recovery_runner import _cached_nodes, _preserved_prefix
from v38_easy_gpu_gate_runner import _output_media, _probe_media
_OBSERVER_SPEC = importlib.util.spec_from_file_location(
"h3_a8a_observer_contract",
REPO_ROOT / "v3" / "adaptive_continuity.py",
)
if _OBSERVER_SPEC is None or _OBSERVER_SPEC.loader is None:
raise RuntimeError("could not load the A8a observer contract")
_OBSERVER_MODULE = importlib.util.module_from_spec(_OBSERVER_SPEC)
_OBSERVER_SPEC.loader.exec_module(_OBSERVER_MODULE)
ADAPTIVE_CONTINUITY_PLANNER_HASH = _OBSERVER_MODULE.ADAPTIVE_CONTINUITY_PLANNER_HASH
SUMMARY_PATTERN = re.compile(
r"planner_hash=([0-9a-f]{64}),\s*observed_groups=(\d+),\s*"
r"execution_applied=false"
)
DECISION_PATTERN = re.compile(
r"physical_group=(\d+),\s*logical_chunks=(\[[^\]]+\]),\s*"
r"boundary=(\d+)/(\d+),\s*motion=([0-9.]+),\s*context=(\d+),\s*"
r"transport=([^,]+),\s*terminal_atomic=(true|false),\s*"
r"reused=(true|false),\s*recommendation=([^,]+),\s*"
r"confidence=([0-9.]+),\s*reason=(.*?),\s*"
r"fallback_reason=(.*?),\s*decision_hash=([0-9a-f]{64}),\s*"
r"execution_applied=false\."
)
def _write_json(path: Path, value: Any) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(value, indent=2, ensure_ascii=False), encoding="utf-8")
def _load_json(path: Path) -> Any:
return json.loads(path.read_text(encoding="utf-8"))
def _load_completed(path: Path) -> dict[str, Any] | None:
if not path.exists():
return None
value = _load_json(path)
checks = value.get("checks") or {}
if "observer_not_persisted" in checks and value.get("run_storage"):
# Early runner revisions searched values too, which incorrectly treated
# the existing report_summary status field as a schema mutation.
manifest = value["run_storage"]["manifest"]
keys: list[str] = []
def collect(current: Any) -> None:
if isinstance(current, Mapping):
for key, item in current.items():
keys.append(str(key).lower())
collect(item)
elif isinstance(current, list):
for item in current:
collect(item)
collect(manifest)
checks.pop("observer_not_persisted", None)
checks["observer_not_persisted_as_schema"] = not any(
key.startswith("adaptive_continuity") or key == "decision_hash"
for key in keys
)
value["checks"] = checks
value["pass"] = all(checks.values())
_write_json(path, value)
if bool(value.get("pass")) and all((value.get("checks") or {}).values()):
value["resumed_without_resampling"] = True
return value
return None
def _parse_observer(status: str) -> dict[str, Any]:
summary_lines = [
line
for line in status.splitlines()
if line.startswith("Adaptive Continuity observer [A8a v1]:")
]
advisory_lines = [
line
for line in status.splitlines()
if line.startswith("Adaptive Continuity advisory [A8a v1]:")
or "Adaptive Continuity observer [A8a v1]: unavailable" in line
]
if len(summary_lines) != 1:
raise GateFailure(f"expected one A8a observer summary, found {len(summary_lines)}")
match = SUMMARY_PATTERN.search(summary_lines[0])
if match is None:
raise GateFailure("A8a observer summary could not be parsed")
records: list[dict[str, Any]] = []
for line in status.splitlines():
if not line.startswith("Adaptive Continuity decision [A8a v1]:"):
continue
current = DECISION_PATTERN.search(line)
if current is None:
raise GateFailure(f"A8a decision could not be parsed: {line}")
fallback = current.group(13)
record = {
"physical_group": int(current.group(1)),
"logical_chunks": json.loads(current.group(2)),
"boundary_index": int(current.group(3)),
"boundary_count": int(current.group(4)),
"observed_motion_score": float(current.group(5)),
"current_context_frames": int(current.group(6)),
"resolved_transport": current.group(7),
"terminal_atomic": current.group(8) == "true",
"reused": current.group(9) == "true",
"recommendation": current.group(10),
"confidence": float(current.group(11)),
"reason": current.group(12),
"fallback_reason": None if fallback == "none" else fallback,
"decision_hash": current.group(14),
"execution_applied": False,
}
# Motion is intentionally formatted to six decimal places in the human
# report while the decision hash retains the full observed float. The
# report can therefore validate hash presence/shape, not recompute it.
record["hash_well_formed"] = bool(re.fullmatch(r"[0-9a-f]{64}", record["decision_hash"]))
records.append(record)
return {
"planner_hash": match.group(1),
"observed_groups": int(match.group(2)),
"records": records,
"advisories": advisory_lines,
"summary": summary_lines[0],
}
def _observer_checks(
observer: Mapping[str, Any],
expected: list[tuple[list[int], int, str, bool, bool]],
*,
total_chunks: int | None = None,
) -> dict[str, bool]:
records = list(observer["records"])
return {
"planner_hash": observer["planner_hash"] == ADAPTIVE_CONTINUITY_PLANNER_HASH,
"one_record_per_physical_group": len(records) == len(expected),
"summary_group_count": int(observer["observed_groups"]) == len(expected),
"physical_order": [item["physical_group"] for item in records]
== list(range(1, len(expected) + 1)),
"logical_groups": [item["logical_chunks"] for item in records]
== [item[0] for item in expected],
"context_frames": [item["current_context_frames"] for item in records]
== [item[1] for item in expected],
"transport": [item["resolved_transport"] for item in records]
== [item[2] for item in expected],
"terminal_atomic": [item["terminal_atomic"] for item in records]
== [item[3] for item in expected],
"reused": [item["reused"] for item in records]
== [item[4] for item in expected],
"boundary_count": all(
int(record["boundary_count"])
== max(
0,
int(total_chunks if total_chunks is not None else sum(len(item[0]) for item in expected)) - 1,
)
for record in records
),
"decision_hashes": all(item["hash_well_formed"] for item in records),
"execution_applied_false": all(not item["execution_applied"] for item in records),
"no_observer_advisory": not observer["advisories"],
}
def _baseline_signature(result: Mapping[str, Any]) -> dict[str, Any]:
probe = result["probe"]
return {
"video": list(probe.get("input_video_sha256") or []),
"audio": list(probe.get("input_audio_sha256") or []),
"assembly": copy.deepcopy((probe.get("input_plan") or {}).get("normalized")),
"decoded": copy.deepcopy(result.get("decoded") or {}),
}
def _run_public_case(
args: argparse.Namespace,
*,
name: str,
case: FirstPassCase,
template_path: Path,
baseline_path: Path,
expected: list[tuple[list[int], int, str, bool, bool]],
) -> dict[str, Any]:
template = _load_json(template_path)
output_prefix = f"v38_a8a/{args.run_tag}/{name}"
prompt = _configure_first_pass(
template,
case=case,
diagnostics="Detailed Report",
output_prefix=output_prefix,
reference_image="unused.png",
reference_video="unused.mp4",
)
result, entry = _execute(
args,
name=name,
prompt=prompt,
expected_seconds=case.expected_seconds,
boundaries=tuple(float(index * 5) for index in range(1, case.chunks)),
)
status = _output_text(entry, "900")
probe = _marker_json(_output_text(entry, "908"), "A3C_PROBE_JSON=")
observer = _parse_observer(status)
baseline = _baseline_signature(_load_json(baseline_path))
current = {
"video": list(probe.get("input_video_sha256") or []),
"audio": list(probe.get("input_audio_sha256") or []),
"assembly": copy.deepcopy((probe.get("input_plan") or {}).get("normalized")),
"decoded": copy.deepcopy(result.get("decoded") or {}),
}
checks = {
**_observer_checks(observer, expected, total_chunks=case.chunks),
"video_latent_sha_parity": current["video"] == baseline["video"],
"audio_latent_sha_parity": current["audio"] == baseline["audio"],
"assembly_plan_parity": _cross_run_plan_identity(current["assembly"])
== _cross_run_plan_identity(baseline["assembly"]),
"decoded_video_parity": current["decoded"].get("video_sha256")
== baseline["decoded"].get("video_sha256"),
"decoded_audio_parity": current["decoded"].get("audio_pcm_sha256")
== baseline["decoded"].get("audio_pcm_sha256"),
"audio_health": bool(result["audio_health"]["pass"]),
"no_reference_video": not any(
key.startswith("reference_video_") for key in prompt["305"]["inputs"]
),
}
payload = {
**result,
"status": status,
"probe": probe,
"observer": observer,
"baseline_path": str(baseline_path),
"baseline": baseline,
"current": current,
"checks": checks,
"pass": all(checks.values()),
}
_write_json(args.evidence_root / name / "result.json", payload)
if not payload["pass"]:
raise GateFailure(f"{name} failed: {[key for key, value in checks.items() if not value]}")
print(f"A8a {name}: PASS ({result['api_elapsed_seconds']:.3f}s)", flush=True)
return payload
def _reachable_prompt(prompt: Mapping[str, Any], roots: tuple[str, ...]) -> dict[str, Any]:
keep: set[str] = set()
def visit(node_id: str) -> None:
if node_id in keep:
return
node = prompt.get(node_id)
if not isinstance(node, Mapping):
raise GateFailure(f"prompt dependency is missing: {node_id}")
keep.add(node_id)
for value in (node.get("inputs") or {}).values():
if isinstance(value, list) and len(value) == 2 and str(value[0]) in prompt:
visit(str(value[0]))
for root in roots:
visit(root)
return {key: copy.deepcopy(value) for key, value in prompt.items() if key in keep}
def _diagnostic_signature(value: Mapping[str, Any]) -> dict[str, Any]:
def tensor_shas(key: str) -> list[str]:
return [str(item.get("sha256") or "") for item in (value.get(key) or [])]
calls = []
for call in value.get("sample_calls") or []:
calls.append(
{
"sample_number": call.get("sample_number"),
"seed": call.get("seed"),
"sigmas": (call.get("sigmas") or {}).get("sha256"),
"prompt_embedding": (call.get("prompt_embedding") or {}).get("sha256"),
"output_video": (call.get("output_video") or {}).get("sha256"),
"output_audio": (call.get("output_audio") or {}).get("sha256"),
}
)
return {
"physical_group_count": value.get("physical_group_count"),
"logical_chunk_count": value.get("logical_chunk_count"),
"physical_decode_group_count": value.get("physical_decode_group_count"),
"production_transport": value.get("production_transport"),
"transport": value.get("transport"),
"context_frames": value.get("context_frames"),
"audio_prefix_steps": value.get("audio_prefix_steps"),
"video_prefix_slots": value.get("video_prefix_slots"),
"synchronize_sampling": value.get("synchronize_sampling"),
"audio_continuity_forced": value.get("audio_continuity_forced"),
"requested_audio_continuity": value.get("requested_audio_continuity"),
"physical_video_sha256": tensor_shas("physical_video_groups"),
"physical_audio_sha256": tensor_shas("physical_audio_groups"),
"sample_calls": calls,
"final_prefix_bit_exact": [
[item.get("bit_exact"), item.get("audio_bit_exact")]
for item in (value.get("final_prefix_pairs") or [])
],
}
def _completed_history_result(
args: argparse.Namespace,
*,
name: str,
expected_seconds: float,
boundaries: tuple[float, ...],
) -> tuple[dict[str, Any], Mapping[str, Any]] | None:
"""Recover evidence after Sampling when only runner post-processing failed."""
history_path = args.evidence_root / name / "history.json"
if not history_path.exists():
return None
history = _load_json(history_path)
if not isinstance(history, Mapping) or len(history) != 1:
return None
entry = _history_entry(history, str(next(iter(history))))
media_path = _output_media(history, args.output_root)
media = _probe_media(media_path, args.ffprobe)
result = {
"name": name,
"prompt_id": str(next(iter(history))),
"api_elapsed_seconds": None,
"prompt": str(args.evidence_root / name / "prompt.json"),
"history": str(history_path),
"media_path": str(media_path),
"media": media,
"decoded": {
"video_sha256": _decoded_stream_hash(
media_path, ffmpeg=args.ffmpeg, stream="video"
),
"audio_pcm_sha256": _decoded_stream_hash(
media_path, ffmpeg=args.ffmpeg, stream="audio"
),
},
"audio_health": _audio_health(
media_path,
ffmpeg=args.ffmpeg,
expected_seconds=expected_seconds,
boundaries=boundaries,
),
"resources": {"salvaged_from_completed_history": True},
"completed_history_reused_without_resampling": True,
}
return result, entry
def _run_issue13(args: argparse.Namespace) -> dict[str, Any]:
prompt = _reachable_prompt(_load_json(args.issue_template), ("191", "249"))
# The accepted R2.6 wrapper later gained this required diagnostic-only
# selector. "Recursive" exactly preserves the accepted R2.6 source path.
prompt["305"]["inputs"]["continuation_source_mode"] = "Recursive"
prompt["191"]["inputs"]["filename_prefix"] = (
f"v38_a8a/{args.run_tag}/g3_issue13_6x5_same_prompt"
)
recovered = _completed_history_result(
args,
name="g3_issue13_6x5_same_prompt",
expected_seconds=30.0,
boundaries=(5.0, 10.0, 15.0, 20.0, 25.0),
)
if recovered is None:
result, entry = _execute(
args,
name="g3_issue13_6x5_same_prompt",
prompt=prompt,
expected_seconds=30.0,
boundaries=(5.0, 10.0, 15.0, 20.0, 25.0),
)
else:
result, entry = recovered
status = _output_text(entry, "249")
diagnostic_line = next(
(
line
for line in status.splitlines()
if line.startswith("V3.6-R1 diagnostic: ")
),
"",
)
if not diagnostic_line:
raise GateFailure("V3.6-R1 diagnostic JSON is missing")
diagnostic = json.loads(diagnostic_line[len("V3.6-R1 diagnostic: ") :])
observer = _parse_observer(status)
baseline_diagnostic = _load_json(args.issue_baseline)["diagnostic"]
baseline = _diagnostic_signature(baseline_diagnostic)
current = _diagnostic_signature(diagnostic)
expected = [
([index], 0 if index == 1 else 22, "initial" if index == 1 else "masked_av_prefix_22_v1", False, False)
for index in range(1, 7)
]
checks = {
**_observer_checks(observer, expected, total_chunks=6),
"accepted_r26_diagnostic_parity": current == baseline,
"six_physical_groups": diagnostic.get("physical_group_count") == 6,
"five_exact_prefix_pairs": len(diagnostic.get("final_prefix_pairs") or []) == 5
and all(
bool(item.get("bit_exact")) and bool(item.get("audio_bit_exact"))
for item in (diagnostic.get("final_prefix_pairs") or [])
),
"same_prompt": len(set(diagnostic.get("logical_prompt_hashes") or [])) <= 1,
"audio_health": bool(result["audio_health"]["pass"]),
"no_reference_video": "reference_video_1" not in prompt["305"]["inputs"],
}
payload = {
**result,
"status": status,
"diagnostic": diagnostic,
"observer": observer,
"baseline_path": str(args.issue_baseline),
"baseline_signature": baseline,
"current_signature": current,
"checks": checks,
"pass": all(checks.values()),
}
_write_json(args.evidence_root / "g3_issue13_6x5_same_prompt" / "result.json", payload)
if not payload["pass"]:
raise GateFailure(
"g3_issue13_6x5_same_prompt failed: "
f"{[key for key, value in checks.items() if not value]}"
)
elapsed = result.get("api_elapsed_seconds")
elapsed_text = "completed-history" if elapsed is None else f"{float(elapsed):.3f}s"
print(f"A8a g3_issue13_6x5_same_prompt: PASS ({elapsed_text})", flush=True)
return payload
def _review_prompt(
args: argparse.Namespace,
*,
output_prefix: str,
run_name: str,
) -> dict[str, Any]:
template = _load_json(args.t2_template)
case = FirstPassCase("g5_review", 3, False, "none", "accepted")
prompt = _configure_first_pass(
template,
case=case,
diagnostics="Detailed Report",
output_prefix=output_prefix,
reference_image="unused.png",
reference_video="unused.mp4",
)
inputs = prompt["305"]["inputs"]
inputs.update(
{
"run_storage": "Save + Auto Resume",
"run_name": run_name,
"project_id": str(uuid.uuid5(uuid.NAMESPACE_URL, run_name)),
"generation_mode": "Review Each Chunk",
"review_action": "Continue / Next",
}
)
return prompt
def _manifest_entry_metadata(storage: Mapping[str, Any]) -> list[dict[str, Any]]:
result = []
for stored in (storage["manifest"].get("chunks") or []):
entry = stored.get("entry") or {}
result.append(
{
"context_frames": int(entry.get("context_frames", 0)),
"motion_score": float(entry.get("motion_score", 0.0)),
}
)
return result
def _review_metadata_checks(
observer: Mapping[str, Any], storage: Mapping[str, Any]
) -> dict[str, bool]:
records = list(observer["records"])
metadata = _manifest_entry_metadata(storage)
count = min(len(records), len(metadata))
schema_keys: list[str] = []
def collect_keys(current: Any) -> None:
if isinstance(current, Mapping):
for key, item in current.items():
schema_keys.append(str(key).lower())
collect_keys(item)
elif isinstance(current, list):
for item in current:
collect_keys(item)
collect_keys(storage["manifest"])
return {
"trace_matches_saved_context": count == len(records)
and all(records[index]["current_context_frames"] == metadata[index]["context_frames"] for index in range(count)),
"trace_matches_saved_motion": count == len(records)
and all(abs(records[index]["observed_motion_score"] - metadata[index]["motion_score"]) <= 0.000001 for index in range(count)),
"observer_not_persisted_as_schema": not any(
key.startswith("adaptive_continuity") or key == "decision_hash"
for key in schema_keys
),
}
def _run_review_queue(
args: argparse.Namespace,
*,
queue_index: int,
run_name: str,
before: Mapping[str, Any] | None,
) -> dict[str, Any]:
prompt = _review_prompt(
args,
output_prefix=f"v38_a8a/{args.run_tag}/g5_review_q{queue_index}",
run_name=run_name,
)
result, entry = _execute(
args,
name=f"g5_review_q{queue_index}",
prompt=prompt,
expected_seconds=float(queue_index * 5),
boundaries=tuple(float(index * 5) for index in range(1, queue_index)),
)
status = _output_text(entry, "900")
observer = _parse_observer(status)
storage = _manifest_snapshot(args.run_storage_root / run_name)
expected = [
([index], 0 if index == 1 else 22, "initial" if index == 1 else "masked_av_prefix_22_v1", False, index < queue_index)
for index in range(1, queue_index + 1)
]
cached = sorted(_cached_nodes(_load_json(Path(result["history"]))))
checks = {
**_observer_checks(observer, expected, total_chunks=3),
**_review_metadata_checks(observer, storage),
"sampler_executed": "305" not in cached,
"chunk_count": len(storage["chunks"]) == queue_index,
"status": storage["status"] == ("complete" if queue_index == 3 else "review_ready"),
"one_new_group": before is None or len(storage["chunks"]) == len(before["chunks"]) + 1,
"prefix_exact_reuse": before is None or _preserved_prefix(before, storage),
"audio_health": bool(result["audio_health"]["pass"]),
"no_reference_video": "reference_video_1" not in prompt["305"]["inputs"],
}
payload = {
**result,
"status_text": status,
"observer": observer,
"run_storage": storage,
"cached_nodes": cached,
"checks": checks,
"pass": all(checks.values()),
}
_write_json(args.evidence_root / f"g5_review_q{queue_index}" / "result.json", payload)
if not payload["pass"]:
raise GateFailure(
f"g5_review_q{queue_index} failed: "
f"{[key for key, value in checks.items() if not value]}"
)
print(f"A8a g5_review_q{queue_index}: PASS ({result['api_elapsed_seconds']:.3f}s)", flush=True)
return payload
def _compact_result(result: Mapping[str, Any]) -> dict[str, Any]:
return {
"pass": result.get("pass"),
"api_elapsed_seconds": result.get("api_elapsed_seconds"),
"resources": result.get("resources"),
"checks": result.get("checks"),
"observer": result.get("observer"),
"result_path": str(Path(result["history"]).parent / "result.json"),
}
def run(args: argparse.Namespace) -> tuple[dict[str, Any], int]:
args.evidence_root.mkdir(parents=True, exist_ok=True)
summary_path = args.evidence_root / "gate_summary.json"
summary = _load_json(summary_path) if summary_path.exists() else {
"format": "h3-continuum-v38-a8a-adaptive-continuity-gpu-matrix-v1",
"run_tag": args.run_tag,
"server": args.server,
"results": {},
"pass": False,
"production_status": "read-only observer; execution_applied=false; A8b HOLD; A5 HOLD",
}
summary["backend_pids"] = list(dict.fromkeys([*(summary.get("backend_pids") or []), args.backend_pid]))
summary.pop("failure_type", None)
summary.pop("failure", None)
try:
if args.stage == "pre-restart":
public_root = args.public_baseline_root
cases = (
(
"g1_1x5",
FirstPassCase("g1", 1, False, "none", "accepted"),
args.t2_template,
public_root / "g1_1x5_t2va_detailed" / "result.json",
[([1], 0, "initial", False, False)],
),
(
"g2_3x5",
FirstPassCase("g2", 3, False, "none", "accepted"),
args.t2_template,
public_root / "g2_3x5_continuation_detailed" / "result.json",
[
([1], 0, "initial", False, False),
([2], 22, "masked_av_prefix_22_v1", False, False),
([3], 22, "masked_av_prefix_22_v1", False, False),
],
),
)
for name, case, template, baseline, expected in cases:
result_path = args.evidence_root / name / "result.json"
current = _load_completed(result_path) or _run_public_case(
args,
name=name,
case=case,
template_path=template,
baseline_path=baseline,
expected=expected,
)
summary["results"][name] = _compact_result(current)
_write_json(summary_path, summary)
issue_path = args.evidence_root / "g3_issue13_6x5_same_prompt" / "result.json"
issue = _load_completed(issue_path) or _run_issue13(args)
summary["results"]["g3_issue13_6x5_same_prompt"] = _compact_result(issue)
_write_json(summary_path, summary)
terminal_path = args.evidence_root / "g4_fl2va_terminal" / "result.json"
terminal = _load_completed(terminal_path) or _run_public_case(
args,
name="g4_fl2va_terminal",
case=FirstPassCase("g4", 3, True, "none", "accepted"),
template_path=args.terminal_template,
baseline_path=public_root / "g3_fl2va_terminal_detailed" / "result.json",
expected=[
([1], 0, "initial", False, False),
([2, 3], 22, "masked_av_prefix_22_v1", True, False),
],
)
summary["results"]["g4_fl2va_terminal"] = _compact_result(terminal)
_write_json(summary_path, summary)
run_name = f"v38_a8a_review_restart_{args.run_tag}"
q1_path = args.evidence_root / "g5_review_q1" / "result.json"
q1 = _load_completed(q1_path) or _run_review_queue(
args,
queue_index=1,
run_name=run_name,
before=None,
)
summary["results"]["g5_review_q1"] = _compact_result(q1)
summary["restart_state"] = {
"run_name": run_name,
"q1_manifest": q1["run_storage"],
"pre_restart_backend_pid": args.backend_pid,
}
summary["status"] = "A8a pre-restart gates PASS; backend restart required"
_write_json(summary_path, summary)
return summary, 0
state = summary.get("restart_state") or {}
if not state.get("run_name") or not state.get("q1_manifest"):
raise GateFailure("post-restart stage requires completed pre-restart state")
if int(state.get("pre_restart_backend_pid")) == int(args.backend_pid):
raise GateFailure("post-restart stage must use a fresh backend PID")
run_name = str(state["run_name"])
before = state["q1_manifest"]
disk_before = _manifest_snapshot(args.run_storage_root / run_name)
if not _preserved_prefix(before, disk_before) or len(disk_before["chunks"]) != 1:
raise GateFailure("Run Storage prefix was not restored exactly after restart")
q2 = _run_review_queue(
args,
queue_index=2,
run_name=run_name,
before=disk_before,
)
summary["results"]["g5_review_q2_after_restart"] = _compact_result(q2)
_write_json(summary_path, summary)
q3 = _run_review_queue(
args,
queue_index=3,
run_name=run_name,
before=q2["run_storage"],
)
summary["results"]["g5_review_q3_complete"] = _compact_result(q3)
summary["restart_state"]["post_restart_backend_pid"] = args.backend_pid
summary["pass"] = all(bool(item.get("pass")) for item in summary["results"].values())
summary["status"] = (
"A8a Adaptive Continuity Observer GPU Experimental PASS"
if summary["pass"]
else "A8a STOP / Production unchanged"
)
_write_json(summary_path, summary)
return summary, 0 if summary["pass"] else 2
except Exception as exc:
summary["pass"] = False
summary["status"] = "A8a STOP / Production unchanged"
summary["failure_type"] = type(exc).__name__
summary["failure"] = str(exc)
_write_json(summary_path, summary)
return summary, 2
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--stage", choices=("pre-restart", "post-restart"), required=True)
parser.add_argument("--server", default="http://127.0.0.6:8190")
parser.add_argument("--backend-pid", type=int, required=True)
parser.add_argument("--run-tag", required=True)
parser.add_argument("--evidence-root", type=Path, required=True)
parser.add_argument("--t2-template", type=Path, required=True)
parser.add_argument("--terminal-template", type=Path, required=True)
parser.add_argument("--public-baseline-root", type=Path, required=True)
parser.add_argument("--issue-template", type=Path, required=True)
parser.add_argument("--issue-baseline", type=Path, required=True)
parser.add_argument(
"--run-storage-root",
type=Path,
default=Path(r"D:\output\video\comfy_video\h3_continuum\runs"),
)
parser.add_argument(
"--output-root",
type=Path,
default=Path(r"D:\output\video\comfy_video"),
)
parser.add_argument("--timeout-seconds", type=float, default=3600.0)
parser.add_argument("--ffmpeg", default=shutil.which("ffmpeg"))
parser.add_argument("--ffprobe", default=shutil.which("ffprobe"))
args = parser.parse_args()
if not args.ffmpeg or not args.ffprobe:
raise SystemExit("ffmpeg and ffprobe are required")
summary, code = run(args)
print(
json.dumps(
{
"status": summary.get("status"),
"pass": summary.get("pass"),
"failure": summary.get("failure"),
"summary": str(args.evidence_root / "gate_summary.json"),
},
ensure_ascii=False,
),
flush=True,
)
return code
if __name__ == "__main__":
raise SystemExit(main())
@@ -0,0 +1,397 @@
"""Run the Easy Load Audio + Driving Audio GPU acceptance gate."""
from __future__ import annotations
import argparse
import copy
from dataclasses import asdict, dataclass
import hashlib
import json
from pathlib import Path
import shutil
import subprocess
import sys
import time
from typing import Any, Mapping
import uuid
import numpy as np
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
from p32_api_runner import _json_request, submit_and_wait
from p32_gpu_profile_probe import configure_profile
from v36_r1_stress_gate_runner import ResourceMonitor
from v38_easy_gpu_gate_runner import _ensure_profile_route
class GateFailure(RuntimeError):
pass
@dataclass(frozen=True)
class GateCase:
name: str
duration: int
enabled: bool
sequence: str
CASES = (
GateCase("g0_10s_off", 10, False, "G0"),
GateCase("g1_10s_on", 10, True, "G1"),
GateCase("g2_20s_on", 20, True, "G2"),
GateCase("g3_10s_on_1", 10, True, "G3 ON-1"),
GateCase("g3_10s_off", 10, False, "G3 OFF"),
GateCase("g3_10s_on_2", 10, True, "G3 ON-2"),
)
def _find_nodes(prompt: Mapping[str, Any], class_type: str) -> list[str]:
return [
str(node_id)
for node_id, node in prompt.items()
if str(node.get("class_type")) == class_type
]
def _one_node(prompt: Mapping[str, Any], class_type: str) -> str:
values = _find_nodes(prompt, class_type)
if len(values) != 1:
raise GateFailure(f"expected one {class_type}, found {len(values)}")
return values[0]
def _next_node_id(prompt: Mapping[str, Any]) -> str:
return str(max(int(value) for value in prompt if str(value).isdigit()) + 1)
def configure_case(
source: Mapping[str, Any],
*,
case: GateCase,
audio_file: str,
seed: int,
) -> dict[str, Any]:
prompt = copy.deepcopy(dict(source))
easy_id = _one_node(prompt, "H3ContinuumEasyV38")
easy_inputs = prompt[easy_id].setdefault("inputs", {})
easy_inputs.update(
{
"duration": int(case.duration),
"seed": int(seed),
"seed_mode": "Fixed",
"preset": "Draft — 0.30 MP",
"custom_mp": 0.30,
"run_storage": "Off",
}
)
helper_id = _one_node(prompt, "H3ContinuumEasyReferences")
prompt[helper_id].setdefault("inputs", {}).clear()
audio_vae_id = None
for candidate in _find_nodes(prompt, "VAELoader"):
vae_name = str(prompt[candidate].get("inputs", {}).get("vae_name", ""))
if "audio" in vae_name.lower():
audio_vae_id = candidate
break
if audio_vae_id is None:
raise GateFailure("source prompt has no Audio VAE loader")
easy_inputs["audio_vae"] = [audio_vae_id, 0]
if case.enabled:
loader_id = _next_node_id(prompt)
prompt[loader_id] = {
"inputs": {"audio": audio_file},
"class_type": "H3EasyLoadAudio",
"_meta": {"title": "H3 Easy Load Audio"},
}
easy_inputs["driving_audio"] = [loader_id, 0]
save_audio_id = str(int(loader_id) + 1)
prompt[save_audio_id] = {
"inputs": {
"audio": [easy_id, 4],
"filename_prefix": f"audio/V38_Easy_Driving/{case.name}",
},
"class_type": "SaveAudio",
"_meta": {"title": "Save Audio (FLAC) (DEPRECATED)"},
}
else:
easy_inputs.pop("driving_audio", None)
scheduler_id = _one_node(prompt, "BasicScheduler")
prompt[scheduler_id].setdefault("inputs", {}).update(
{"scheduler": "simple", "steps": 8, "denoise": 1.0}
)
assembly_id = _one_node(prompt, "H3ContinuumAssembleSeamV35")
prompt[assembly_id].setdefault("inputs", {}).update(
{
"exact_total_duration": True,
"audio_seam": "Auto",
"video_seam": "Auto",
"buffer_backend": "Auto",
"diagnostics": "Detailed Report",
}
)
for save_id in _find_nodes(prompt, "SaveVideo"):
prompt[save_id].setdefault("inputs", {})["filename_prefix"] = (
f"video/V38_Easy_Driving/{case.name}"
)
_ensure_profile_route(prompt)
return configure_profile(prompt, "sage")
def _history_media(
history: Mapping[str, Any], output_root: Path, suffix: str
) -> Path:
for entry in history.values():
outputs = entry.get("outputs", {}) if isinstance(entry, Mapping) else {}
for output in outputs.values():
if not isinstance(output, Mapping):
continue
for field in ("audio", "videos", "gifs", "images"):
for media in output.get(field, []) or []:
filename = str(media.get("filename", ""))
if Path(filename).suffix.lower() != suffix:
continue
path = output_root / str(media.get("subfolder") or "") / filename
if path.is_file():
return path
raise GateFailure(f"history returned no existing {suffix} output")
def _ffprobe(path: Path, ffprobe: str) -> dict[str, Any]:
result = subprocess.run(
[
ffprobe,
"-v",
"error",
"-count_frames",
"-show_entries",
"stream=codec_type,width,height,r_frame_rate,nb_read_frames,sample_rate,channels,duration:format=duration,size",
"-of",
"json",
str(path),
],
check=True,
capture_output=True,
text=True,
)
return json.loads(result.stdout)
def _media_contract(path: Path, ffprobe: str) -> dict[str, Any]:
payload = _ffprobe(path, ffprobe)
streams = list(payload.get("streams") or [])
video = next((x for x in streams if x.get("codec_type") == "video"), {})
audio = next((x for x in streams if x.get("codec_type") == "audio"), {})
return {
"path": str(path),
"sha256": hashlib.sha256(path.read_bytes()).hexdigest(),
"size_bytes": int(path.stat().st_size),
"width": int(video.get("width", 0)),
"height": int(video.get("height", 0)),
"frames": int(video.get("nb_read_frames", 0)),
"fps": str(video.get("r_frame_rate", "")),
"video_duration": float(video.get("duration", 0.0)),
"audio_duration": float(audio.get("duration", 0.0)),
"format_duration": float(payload.get("format", {}).get("duration", 0.0)),
"audio_sample_rate": int(audio.get("sample_rate", 0)),
"audio_channels": int(audio.get("channels", 0)),
}
def _decode_pcm(path: Path, ffmpeg: str, duration: int | None = None) -> np.ndarray:
command = [ffmpeg, "-v", "error", "-i", str(path)]
if duration is not None:
command += ["-t", str(int(duration))]
command += ["-ar", "32000", "-ac", "2", "-f", "f32le", "pipe:1"]
result = subprocess.run(command, check=True, capture_output=True)
return np.frombuffer(result.stdout, dtype="<f4").reshape(-1, 2).copy()
def _audio_evidence(
*,
input_path: Path,
saved_path: Path,
video_path: Path,
duration: int,
ffmpeg: str,
) -> dict[str, Any]:
source = _decode_pcm(input_path, ffmpeg, duration)
saved = _decode_pcm(saved_path, ffmpeg)
final = _decode_pcm(video_path, ffmpeg)
count = min(len(source), len(saved))
source_bytes = source[:count].astype("<f4", copy=False).tobytes()
saved_bytes = saved[:count].astype("<f4", copy=False).tobytes()
boundary = int(10 * 32000)
boundary_delta = None
if duration > 10 and len(final) > boundary:
boundary_delta = float(np.max(np.abs(final[boundary] - final[boundary - 1])))
diffs = np.abs(np.diff(final, axis=0)) if len(final) > 1 else np.zeros((0, 2))
return {
"source_frames": int(len(source)),
"saved_frames": int(len(saved)),
"final_frames": int(len(final)),
"source_prefix_pcm_sha256": hashlib.sha256(source_bytes).hexdigest(),
"saved_pcm_sha256": hashlib.sha256(saved_bytes).hexdigest(),
"saved_pcm_bit_exact": source_bytes == saved_bytes,
"finite": bool(np.isfinite(final).all()),
"max_abs": float(np.max(np.abs(final))) if final.size else 0.0,
"max_adjacent_delta": float(np.max(diffs)) if diffs.size else 0.0,
"p999_adjacent_delta": float(np.quantile(diffs, 0.999)) if diffs.size else 0.0,
"ten_second_boundary_delta": boundary_delta,
}
def _find_backend_pid() -> int | None:
try:
import psutil
except Exception:
return None
candidates = []
for process in psutil.process_iter(["pid", "cmdline", "memory_info"]):
try:
command = " ".join(process.info.get("cmdline") or [])
if "ComfyUI_W" in command and "main.py" in command:
candidates.append((int(process.info["memory_info"].rss), process.pid))
except Exception:
continue
return max(candidates)[1] if candidates else None
def run(args) -> dict[str, Any]:
source = json.loads(args.source_prompt.read_text(encoding="utf-8"))
args.output_root.mkdir(parents=True, exist_ok=True)
(args.output_root / "source_prompt.json").write_text(
json.dumps(source, indent=2, ensure_ascii=False), encoding="utf-8"
)
ffmpeg = shutil.which("ffmpeg")
ffprobe = shutil.which("ffprobe")
if not args.dry_run and (ffmpeg is None or ffprobe is None):
raise GateFailure("ffmpeg and ffprobe are required")
backend_pid = _find_backend_pid()
results = []
for case in CASES:
case_root = args.output_root / case.name
case_root.mkdir(parents=True, exist_ok=True)
prompt = configure_case(
source,
case=case,
audio_file=args.audio_file,
seed=args.seed,
)
prompt_path = case_root / "prompt.json"
prompt_path.write_text(
json.dumps(prompt, indent=2, ensure_ascii=False), encoding="utf-8"
)
record: dict[str, Any] = {
"case": asdict(case),
"prompt": str(prompt_path),
"seed": int(args.seed),
}
if args.dry_run:
record["status"] = "DRY_RUN"
results.append(record)
continue
queue = _json_request(f"{args.server.rstrip('/')}/queue")
if queue.get("queue_running") or queue.get("queue_pending"):
raise GateFailure("ComfyUI queue is not empty")
print(f"START {case.name}", flush=True)
started = time.perf_counter()
with ResourceMonitor(backend_pid, args.monitor_interval) as monitor:
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=prompt,
client_id=f"v38-easy-driving-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=args.poll_seconds,
)
history_path = case_root / "history.json"
history_path.write_text(
json.dumps(history, indent=2, ensure_ascii=False), encoding="utf-8"
)
video_path = _history_media(history, args.media_root, ".mp4")
media = _media_contract(video_path, ffprobe)
expected_frames = int(case.duration * 24)
checks = {
"frames": media["frames"] == expected_frames,
"fps": media["fps"] == "24/1",
"duration": abs(media["format_duration"] - case.duration) <= 0.02,
"audio_rate": media["audio_sample_rate"] == 32000,
"audio_channels": media["audio_channels"] == 2,
}
if not all(checks.values()):
raise GateFailure(f"media contract failed for {case.name}: {checks}")
audio = None
if case.enabled:
saved_path = _history_media(history, args.media_root, ".flac")
audio = _audio_evidence(
input_path=args.input_root / args.audio_file,
saved_path=saved_path,
video_path=video_path,
duration=case.duration,
ffmpeg=ffmpeg,
)
if not audio["finite"]:
raise GateFailure(f"non-finite final audio in {case.name}")
record.update(
{
"prompt_id": prompt_id,
"api_elapsed_seconds": float(elapsed),
"wall_elapsed_seconds": float(time.perf_counter() - started),
"history": str(history_path),
"resources": monitor.as_dict(),
"media": media,
"audio": audio,
"checks": checks,
"status": "PASS",
}
)
(case_root / "summary.json").write_text(
json.dumps(record, indent=2, ensure_ascii=False), encoding="utf-8"
)
results.append(record)
print(f"PASS {case.name} elapsed={elapsed:.3f}s", flush=True)
summary = {
"format": "h3-continuum-v38-easy-driving-audio-gpu-gate-v1",
"server": args.server,
"source_prompt": str(args.source_prompt),
"audio_file": args.audio_file,
"seed": int(args.seed),
"backend_pid": backend_pid,
"dry_run": bool(args.dry_run),
"results": results,
"status": "DRY_RUN" if args.dry_run else "PASS",
}
(args.output_root / "gate_summary.json").write_text(
json.dumps(summary, indent=2, ensure_ascii=False), encoding="utf-8"
)
return summary
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--server", default="http://127.0.0.6:8188")
parser.add_argument("--source-prompt", type=Path, required=True)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument("--media-root", type=Path, default=Path(r"D:\output\video\comfy_video"))
parser.add_argument("--input-root", type=Path, default=Path(r"D:\StabilityMatrix\Data\Packages\ComfyUI_W\input"))
parser.add_argument("--audio-file", required=True)
parser.add_argument("--seed", type=int, default=927966463230733)
parser.add_argument("--timeout-seconds", type=float, default=3600.0)
parser.add_argument("--poll-seconds", type=float, default=2.0)
parser.add_argument("--monitor-interval", type=float, default=0.25)
parser.add_argument("--dry-run", action="store_true")
return parser
def main() -> int:
args = _parser().parse_args()
print(json.dumps(run(args), ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+581
View File
@@ -0,0 +1,581 @@
"""Run the V3.8 Easy Mode Phase 3 GPU acceptance matrix.
The runner starts from one successful ComfyUI history prompt, preserves its
installed model/LoRA/VAE stack, and changes only the Easy inputs, image
presence, attention route, output prefix, and fixed seed needed by the gate.
It writes every submitted API prompt, returned history, resource sample, and
media probe under one evidence directory.
"""
from __future__ import annotations
import argparse
import copy
from dataclasses import asdict, dataclass
import hashlib
import json
from pathlib import Path
import shutil
import subprocess
import sys
import time
from typing import Any, Mapping
import uuid
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
from p32_api_runner import _json_request, submit_and_wait
from p32_gpu_profile_probe import configure_profile
from v36_r1_stress_gate_runner import ResourceMonitor
PROFILES = ("sage", "sage_sol", "sage_spectrum", "sage_sol_spectrum")
REFERENCE_PROFILE_CASE = "i2va_reference1_10s"
class GateFailure(RuntimeError):
pass
@dataclass(frozen=True)
class GateCase:
name: str
duration: int
chunks: int
chunk_seconds: float
first: bool
last: bool
reference_slot: int | None
prompt: str
expected_mode: str
expected_width: int
expected_height: int
CASES = {
REFERENCE_PROFILE_CASE: GateCase(
name=REFERENCE_PROFILE_CASE,
duration=10,
chunks=1,
chunk_seconds=10.0,
first=True,
last=False,
reference_slot=1,
prompt=(
"A continuous cinematic tracking shot begins from the supplied First "
"Image. Use <Picture 2> as the identity, face, clothing, and color "
"reference. The woman walks naturally toward camera through the city, "
"with coherent body motion, stable facial identity, realistic wind, "
"footsteps, and urban ambience."
),
expected_mode="Hybrid I2VA + Reference + Continuation",
expected_width=480,
expected_height=640,
),
"fl2va_reference1_15s": GateCase(
name="fl2va_reference1_15s",
duration=15,
chunks=2,
chunk_seconds=7.5,
first=True,
last=True,
reference_slot=1,
prompt=(
"A continuous cinematic shot starts from the supplied First Image and "
"converges naturally to the supplied Last Image. Use <Picture 3> as the "
"identity, face, clothing, and color reference throughout. The woman "
"walks forward, turns smoothly, and reaches the final pose without a "
"cut. Preserve stable anatomy, natural motion, footsteps, and ambience."
),
expected_mode="Hybrid FL2VA + Reference + Continuation",
expected_width=480,
expected_height=640,
),
"i2va_reference2_only_20s": GateCase(
name="i2va_reference2_only_20s",
duration=20,
chunks=2,
chunk_seconds=10.0,
first=True,
last=False,
reference_slot=2,
prompt=(
"A single continuous cinematic shot begins from the supplied First "
"Image. Use <Picture 2> as the identity, face, clothing, and color "
"reference; the second helper socket is the only connected reference "
"and must compact to the first active reference. The woman walks, looks "
"toward camera, and continues forward with stable identity and ambience."
),
expected_mode="Hybrid I2VA + Reference + Continuation",
expected_width=480,
expected_height=640,
),
"t2va_no_reference_30s": GateCase(
name="t2va_no_reference_30s",
duration=30,
chunks=3,
chunk_seconds=10.0,
first=False,
last=False,
reference_slot=None,
prompt=(
"A single continuous cinematic wide shot follows a woman walking through "
"a modern city plaza. She changes direction smoothly while the camera "
"tracks beside her, preserving natural anatomy, coherent motion, stable "
"lighting, footsteps, wind, and distant urban ambience."
),
expected_mode="T2VA + Continuation",
expected_width=736,
expected_height=416,
),
}
SPECTRUM_DEFAULTS = {
"enabled": False,
"blend_weight": 0.5,
"degree": 1,
"ridge_lambda": 0.1,
"window_size": 2.0,
"flex_window": 0.75,
"warmup_steps": 1,
"tail_actual_steps": 1,
"max_history": 8,
"debug": False,
"history_storage": "system_ram",
"bootstrap_first_forecast": True,
"anchor_residual_feedback": False,
"selective_rollback_correction": False,
"offline_smoothing_replay": True,
"audio_blend_weight": 0.0,
"offline_archive_storage": "system_ram",
"model_aware_mode": "off",
"model_aware_risk_threshold": 0.65,
"model_aware_trust_shrinkage": False,
"model_aware_replay_generic_correction": False,
"generic_correction_mode": "coordinate_rls",
"generic_correction_limiter": "hard_clip",
"generic_correction_limit": 0.4,
"generic_correction_attenuation": "no_attenuation",
}
def _find_nodes(prompt: Mapping[str, Any], class_type: str) -> list[str]:
return [
str(node_id)
for node_id, node in prompt.items()
if str(node.get("class_type")) == class_type
]
def _one_node(prompt: Mapping[str, Any], class_type: str) -> str:
values = _find_nodes(prompt, class_type)
if len(values) != 1:
raise GateFailure(f"expected one {class_type}, found {len(values)}")
return values[0]
def _next_node_id(prompt: Mapping[str, Any]) -> str:
numeric = [int(value) for value in prompt if str(value).isdigit()]
return str((max(numeric) if numeric else 0) + 1)
def _history_prompt(server: str, prompt_id: str) -> tuple[dict[str, Any], Any]:
history = _json_request(f"{server.rstrip('/')}/history/{prompt_id}")
entry = history.get(str(prompt_id))
if not isinstance(entry, Mapping):
raise GateFailure(f"history does not contain prompt {prompt_id}")
raw = entry.get("prompt")
if not isinstance(raw, list) or len(raw) < 3 or not isinstance(raw[2], Mapping):
raise GateFailure("history entry has no API prompt")
return copy.deepcopy(dict(raw[2])), history
def _ensure_load_image(
prompt: dict[str, Any], *, image: str, title: str
) -> list[Any]:
for node_id in _find_nodes(prompt, "LoadImage"):
if prompt[node_id].get("inputs", {}).get("image") == image:
return [node_id, 0]
node_id = _next_node_id(prompt)
prompt[node_id] = {
"class_type": "LoadImage",
"inputs": {"image": str(image)},
"_meta": {"title": str(title)},
}
return [node_id, 0]
def _ensure_profile_route(prompt: dict[str, Any]) -> None:
easy_id = _one_node(prompt, "H3ContinuumEasyV38")
easy_inputs = prompt[easy_id].setdefault("inputs", {})
upstream = easy_inputs.get("model")
if not isinstance(upstream, list) or len(upstream) != 2:
raise GateFailure("Easy node has no MODEL link")
spectrum_nodes = _find_nodes(prompt, "SpectrumApplyMiniMaxH3")
if len(spectrum_nodes) > 1:
raise GateFailure("multiple Spectrum nodes are not supported by this gate")
if spectrum_nodes:
spectrum_id = spectrum_nodes[0]
spectrum_inputs = prompt[spectrum_id].setdefault("inputs", {})
if easy_inputs.get("model") != [spectrum_id, 0]:
spectrum_inputs["model"] = list(upstream)
easy_inputs["model"] = [spectrum_id, 0]
return
spectrum_id = _next_node_id(prompt)
prompt[spectrum_id] = {
"class_type": "SpectrumApplyMiniMaxH3",
"inputs": {**SPECTRUM_DEFAULTS, "model": list(upstream)},
"_meta": {"title": "Spectrum"},
}
easy_inputs["model"] = [spectrum_id, 0]
def configure_case(
source: Mapping[str, Any],
*,
case: GateCase,
profile: str,
seed: int,
output_prefix: str,
reference_image_1: str,
reference_image_2: str,
last_image: str,
) -> dict[str, Any]:
prompt = copy.deepcopy(dict(source))
easy_id = _one_node(prompt, "H3ContinuumEasyV38")
helper_id = _one_node(prompt, "H3ContinuumEasyReferences")
easy_inputs = prompt[easy_id].setdefault("inputs", {})
first_link = easy_inputs.get("first_frame")
if case.first and not isinstance(first_link, list):
raise GateFailure("source prompt has no First Image link")
if case.first:
easy_inputs["first_frame"] = list(first_link)
easy_inputs["aspect"] = "Auto from First Image"
else:
easy_inputs.pop("first_frame", None)
easy_inputs["aspect"] = "Landscape 16:9"
if case.last:
easy_inputs["last_frame"] = _ensure_load_image(
prompt, image=last_image, title="Last Image"
)
else:
easy_inputs.pop("last_frame", None)
helper_inputs = prompt[helper_id].setdefault("inputs", {})
helper_inputs.clear()
if case.reference_slot is not None:
reference_file = (
reference_image_1 if case.reference_slot == 1 else reference_image_2
)
helper_inputs[f"reference_image_{case.reference_slot}"] = _ensure_load_image(
prompt,
image=reference_file,
title=f"Reference Image {case.reference_slot}",
)
easy_inputs.update(
{
"duration": int(case.duration),
"seed": int(seed),
"seed_mode": "Fixed",
"preset": "Draft — 0.30 MP",
"custom_mp": 0.30,
"run_storage": "Off",
}
)
prompt_link = easy_inputs.get("prompt_text")
if not isinstance(prompt_link, list) or len(prompt_link) != 2:
raise GateFailure("Easy node has no Prompt link")
prompt_node = str(prompt_link[0])
prompt[prompt_node].setdefault("inputs", {})["value"] = case.prompt
scheduler_id = _one_node(prompt, "BasicScheduler")
prompt[scheduler_id].setdefault("inputs", {}).update(
{"scheduler": "simple", "steps": 8, "denoise": 1.0}
)
assembly_id = _one_node(prompt, "H3ContinuumAssembleSeamV35")
prompt[assembly_id].setdefault("inputs", {}).update(
{
"exact_total_duration": True,
"audio_seam": "Auto",
"video_seam": "Auto",
"buffer_backend": "Auto",
"diagnostics": "Detailed Report",
}
)
for save_id in _find_nodes(prompt, "SaveVideo"):
prompt[save_id].setdefault("inputs", {})["filename_prefix"] = str(
output_prefix
)
_ensure_profile_route(prompt)
return configure_profile(prompt, profile)
def _walk_strings(value: Any):
if isinstance(value, str):
yield value
elif isinstance(value, Mapping):
for item in value.values():
yield from _walk_strings(item)
elif isinstance(value, (list, tuple)):
for item in value:
yield from _walk_strings(item)
def _output_media(history: Mapping[str, Any], output_root: Path) -> Path:
for value in history.values():
if not isinstance(value, Mapping):
continue
outputs = value.get("outputs")
if not isinstance(outputs, Mapping):
continue
for output in outputs.values():
if not isinstance(output, Mapping):
continue
for field in ("images", "videos", "gifs"):
for media in output.get(field, []) or []:
if not isinstance(media, Mapping):
continue
filename = media.get("filename")
if not filename or Path(str(filename)).suffix.lower() != ".mp4":
continue
path = output_root / str(media.get("subfolder") or "") / str(
filename
)
if path.is_file():
return path
raise GateFailure("history returned no existing MP4 output")
def _probe_media(path: Path, ffprobe: str) -> dict[str, Any]:
result = subprocess.run(
[
ffprobe,
"-v",
"error",
"-count_frames",
"-show_entries",
"stream=codec_type,width,height,r_frame_rate,nb_read_frames,sample_rate,channels,duration:format=duration,size",
"-of",
"json",
str(path),
],
check=True,
capture_output=True,
text=True,
)
payload = json.loads(result.stdout)
streams = list(payload.get("streams") or [])
video = next((item for item in streams if item.get("codec_type") == "video"), None)
audio = next((item for item in streams if item.get("codec_type") == "audio"), None)
if not isinstance(video, Mapping) or not isinstance(audio, Mapping):
raise GateFailure("output lacks video or audio stream")
digest = hashlib.sha256()
with path.open("rb") as stream:
for block in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(block)
return {
"path": str(path),
"sha256": digest.hexdigest(),
"size_bytes": int(path.stat().st_size),
"width": int(video.get("width", 0)),
"height": int(video.get("height", 0)),
"frames": int(video.get("nb_read_frames", 0)),
"fps": str(video.get("r_frame_rate", "")),
"video_duration": float(video.get("duration", 0.0)),
"audio_duration": float(audio.get("duration", 0.0)),
"format_duration": float(payload.get("format", {}).get("duration", 0.0)),
"audio_sample_rate": int(audio.get("sample_rate", 0)),
"audio_channels": int(audio.get("channels", 0)),
}
def _validate_result(
history: Mapping[str, Any], *, case: GateCase, media: Mapping[str, Any]
) -> dict[str, Any]:
text = "\n".join(_walk_strings(history))
required = [
case.expected_mode,
f"/ {case.duration} sec",
f"{case.chunks} chunks",
f"{case.chunk_seconds:.3f}s per chunk",
f"{case.expected_width} x {case.expected_height}",
]
if case.reference_slot is not None:
required.append("Reference conditioning: 1 image(s), size=Match Output")
missing = [value for value in required if value not in text]
if missing:
raise GateFailure(f"status evidence is missing: {missing}")
if "H3C-P103 Warning" in text or "H3C-P102 Warning" in text:
raise GateFailure("Picture numbering produced a reference prompt warning")
expected_frames = int(case.duration * 24)
media_checks = {
"geometry": (media["width"], media["height"])
== (case.expected_width, case.expected_height),
"frames": int(media["frames"]) == expected_frames,
"fps": str(media["fps"]) == "24/1",
"duration": abs(float(media["format_duration"]) - case.duration) <= 0.02,
"audio_rate": int(media["audio_sample_rate"]) == 32000,
"audio_channels": int(media["audio_channels"]) == 2,
}
if not all(media_checks.values()):
raise GateFailure(f"media contract failed: {media_checks}")
return {"required_status": required, "media_checks": media_checks}
def _find_backend_pid() -> int | None:
try:
import psutil
except Exception:
return None
candidates = []
for process in psutil.process_iter(["pid", "cmdline", "memory_info"]):
try:
command = " ".join(process.info.get("cmdline") or [])
if "ComfyUI_W" in command and "main.py" in command:
rss = int(process.info["memory_info"].rss)
candidates.append((rss, int(process.info["pid"])))
except Exception:
continue
return max(candidates)[1] if candidates else None
def run(args) -> dict[str, Any]:
source, source_history = _history_prompt(args.server, args.history_prompt_id)
args.output_root.mkdir(parents=True, exist_ok=True)
(args.output_root / "source_history.json").write_text(
json.dumps(source_history, indent=2, ensure_ascii=False), encoding="utf-8"
)
(args.output_root / "source_prompt.json").write_text(
json.dumps(source, indent=2, ensure_ascii=False), encoding="utf-8"
)
ffprobe = shutil.which("ffprobe")
if not args.dry_run and ffprobe is None:
raise GateFailure("ffprobe is required for GPU acceptance")
backend_pid = _find_backend_pid()
matrix = [
(CASES[REFERENCE_PROFILE_CASE], profile) for profile in PROFILES
] + [
(CASES["fl2va_reference1_15s"], "sage"),
(CASES["i2va_reference2_only_20s"], "sage"),
(CASES["t2va_no_reference_30s"], "sage"),
]
results = []
for case, profile in matrix:
label = f"{case.name}__{profile}"
run_root = args.output_root / label
run_root.mkdir(parents=True, exist_ok=True)
prompt = configure_case(
source,
case=case,
profile=profile,
seed=args.seed,
output_prefix=f"video/V38_Easy_Phase3/{label}",
reference_image_1=args.reference_image_1,
reference_image_2=args.reference_image_2,
last_image=args.last_image,
)
prompt_path = run_root / "prompt.json"
prompt_path.write_text(
json.dumps(prompt, indent=2, ensure_ascii=False), encoding="utf-8"
)
record: dict[str, Any] = {
"label": label,
"case": asdict(case),
"profile": profile,
"seed": int(args.seed),
"prompt": str(prompt_path),
}
if args.dry_run:
record["dry_run"] = True
results.append(record)
continue
queue = _json_request(f"{args.server.rstrip('/')}/queue")
if queue.get("queue_running") or queue.get("queue_pending"):
raise GateFailure("ComfyUI queue is not empty")
print(f"START {label}", flush=True)
started = time.perf_counter()
with ResourceMonitor(backend_pid, args.monitor_interval) as monitor:
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=prompt,
client_id=f"v38-easy-phase3-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=args.poll_seconds,
)
history_path = run_root / "history.json"
history_path.write_text(
json.dumps(history, indent=2, ensure_ascii=False), encoding="utf-8"
)
media = _probe_media(_output_media(history, args.media_root), ffprobe)
validation = _validate_result(history, case=case, media=media)
record.update(
{
"prompt_id": prompt_id,
"api_elapsed_seconds": float(elapsed),
"wall_elapsed_seconds": float(time.perf_counter() - started),
"history": str(history_path),
"resources": monitor.as_dict(),
"media": media,
"validation": validation,
"status": "PASS",
}
)
(run_root / "summary.json").write_text(
json.dumps(record, indent=2, ensure_ascii=False), encoding="utf-8"
)
results.append(record)
print(f"PASS {label} elapsed={elapsed:.3f}s", flush=True)
summary = {
"format": "h3-continuum-v38-easy-phase3-gpu-gate-v1",
"source_history_prompt_id": args.history_prompt_id,
"server": args.server,
"media_root": str(args.media_root),
"seed": int(args.seed),
"backend_pid": backend_pid,
"dry_run": bool(args.dry_run),
"results": results,
"status": "DRY_RUN" if args.dry_run else "PASS",
}
(args.output_root / "gate_summary.json").write_text(
json.dumps(summary, indent=2, ensure_ascii=False), encoding="utf-8"
)
return summary
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--server", default="http://127.0.0.6:8188")
parser.add_argument("--history-prompt-id", required=True)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument(
"--media-root",
type=Path,
default=Path(r"D:\output\video\comfy_video"),
)
parser.add_argument("--seed", type=int, default=927966463230733)
parser.add_argument("--reference-image-1", required=True)
parser.add_argument("--reference-image-2", required=True)
parser.add_argument("--last-image", required=True)
parser.add_argument("--timeout-seconds", type=float, default=3600.0)
parser.add_argument("--poll-seconds", type=float, default=2.0)
parser.add_argument("--monitor-interval", type=float, default=1.0)
parser.add_argument("--dry-run", action="store_true")
return parser
def main() -> int:
args = _parser().parse_args()
summary = run(args)
print(json.dumps({"status": summary["status"]}, ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())
@@ -0,0 +1,345 @@
"""G4 FL2VA conditioning determinism Gate (evidence-only, no Sampling)."""
from __future__ import annotations
import argparse
import hashlib
import json
from pathlib import Path
import sys
from typing import Any, Mapping
import uuid
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
from p32_api_runner import submit_and_wait
CHECKPOINTS = ("P0", "P1", "P2", "P3", "P4", "P5", "P6", "P7")
FIRST_IMAGE = "20260830030614-Anima-Luc_v2_00008_-1635935150-ER SDE.webp"
VIDEO_VAE = "minimax_h3_video_vae_fp16.safetensors"
CLIP = "qwen3vl_32b_minimax_h3_nvfp4_awq.safetensors"
PROMPT = (
"A continuous cinematic shot begins from the supplied First Image. "
"The subject moves naturally while identity, clothing, lighting, camera "
"direction, footsteps, and ambience remain coherent."
)
class GateFailure(RuntimeError):
pass
def _write(path: Path, value: Any) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(value, indent=2, ensure_ascii=False), encoding="utf-8")
def _sha256_file(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as stream:
for block in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(block)
return digest.hexdigest()
def _prompt(*, context: str, focus_mode: str, nonce: int) -> dict[str, Any]:
return {
"1": {
"class_type": "LoadImage",
"inputs": {"image": FIRST_IMAGE},
"_meta": {"title": "LoadImage"},
},
"2": {
"class_type": "VAELoader",
"inputs": {"vae_name": VIDEO_VAE},
"_meta": {"title": "Video VAE"},
},
"3": {
"class_type": "CLIPLoader",
"inputs": {"clip_name": CLIP, "type": "minimax", "device": "default"},
"_meta": {"title": "MiniMax H3 Text Encoder"},
},
"4": {
"class_type": "H3G4ConditioningDeterminismDiagnostic",
"inputs": {
"video_vae": ["2", 0],
"clip": ["3", 0],
"first_image": ["1", 0],
"prompt": PROMPT,
"width": 544,
"height": 544,
"frame_count": 124,
"execution_context": context,
"focus_mode": focus_mode,
"diagnostic_nonce": int(nonce),
},
"_meta": {"title": "G4 Conditioning Determinism Collector"},
},
}
def _extract(history: Mapping[str, Any], prompt_id: str) -> dict[str, Any]:
record = history.get(prompt_id)
if not isinstance(record, Mapping):
raise GateFailure(f"history record missing for {prompt_id}")
outputs = record.get("outputs")
if not isinstance(outputs, Mapping):
raise GateFailure("history outputs are missing")
node = outputs.get("4")
if not isinstance(node, Mapping):
raise GateFailure("diagnostic node output is missing")
text = node.get("text")
if not isinstance(text, list) or len(text) != 1:
raise GateFailure(f"unexpected diagnostic text output: {text!r}")
result = json.loads(str(text[0]))
if result.get("format") != "h3-v38-g4-conditioning-determinism-v1":
raise GateFailure("unexpected diagnostic output format")
missing = [key for key in CHECKPOINTS if key not in result.get("checkpoints", {})]
if missing:
raise GateFailure(f"missing checkpoints: {missing}")
return result
def _run_case(args, *, name: str, context: str, focus_mode: str, nonce: int) -> dict[str, Any]:
case_root = args.evidence_root / args.stage / name
prompt = _prompt(context=context, focus_mode=focus_mode, nonce=nonce)
_write(case_root / "prompt.json", prompt)
print(f"G4 diagnostic start: {name}", flush=True)
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=prompt,
client_id=f"g4diag-{uuid.uuid4().hex}",
timeout_seconds=float(args.timeout_seconds),
poll_seconds=float(args.poll_seconds),
)
_write(case_root / "history.json", history)
result = _extract(history, prompt_id)
_write(case_root / "result.json", result)
output = {
"name": name,
"prompt_id": prompt_id,
"api_elapsed_seconds": elapsed,
"prompt": str(case_root / "prompt.json"),
"history": str(case_root / "history.json"),
"result_path": str(case_root / "result.json"),
"result": result,
}
print(f"G4 diagnostic end: {name}; {elapsed:.3f}s", flush=True)
return output
def _checkpoint_sha(run: Mapping[str, Any], checkpoint: str) -> str:
return str(run["result"]["checkpoints"][checkpoint]["sha256"])
def _compare(left: Mapping[str, Any], right: Mapping[str, Any]) -> dict[str, Any]:
rows = {}
first = None
for checkpoint in CHECKPOINTS:
left_sha = _checkpoint_sha(left, checkpoint)
right_sha = _checkpoint_sha(right, checkpoint)
match = left_sha == right_sha
rows[checkpoint] = {
"left_sha256": left_sha,
"right_sha256": right_sha,
"match": match,
}
if first is None and not match:
first = checkpoint
return {"first_divergence": first, "checkpoints": rows, "all_match": first is None}
def _focus_mode(*firsts: str | None) -> str:
if "P2" in firsts:
return "vae_repeat"
if "P5" in firsts:
return "qwen_repeat"
return "standard"
def _focus_checks(run: Mapping[str, Any]) -> dict[str, Any]:
result = run["result"]
mode = str(result["focus_mode"])
if mode == "vae_repeat":
rows = result["focus"]["vae_repeat"]
hashes = [str(row["sha256"]) for row in rows]
return {"mode": mode, "hashes": hashes, "all_match": len(set(hashes)) == 1}
if mode == "qwen_repeat":
rows = result["focus"]["qwen_repeat"]
token_hashes = [str(row["tokens"]["sha256"]) for row in rows]
cond_hashes = [str(row["conditioning"]["sha256"]) for row in rows]
return {
"mode": mode,
"token_hashes": token_hashes,
"conditioning_hashes": cond_hashes,
"tokens_all_match": len(set(token_hashes)) == 1,
"conditioning_all_match": len(set(cond_hashes)) == 1,
}
return {"mode": "standard"}
def _prepare(args) -> dict[str, Any]:
cold = _run_case(args, name="cold_a", context="Cold A", focus_mode="standard", nonce=1)
warm = _run_case(args, name="warm_b", context="Warm B", focus_mode="standard", nonce=2)
full = _run_case(args, name="full_context", context="Full", focus_mode="standard", nonce=3)
review = _run_case(args, name="review_context", context="Review", focus_mode="standard", nonce=4)
cold_warm = _compare(cold, warm)
full_review = _compare(full, review)
mode = _focus_mode(cold_warm["first_divergence"], full_review["first_divergence"])
focus = None
focus_checks = None
if mode != "standard":
focus = _run_case(args, name=mode, context="Warm B", focus_mode=mode, nonce=5)
focus_checks = _focus_checks(focus)
summary = {
"format": "h3-v38-g4-conditioning-determinism-prepare-v1",
"stage": "prepare",
"server": args.server,
"runner_sha256": _sha256_file(Path(__file__)),
"runs": {"cold_a": cold, "warm_b": warm, "full": full, "review": review, "focus": focus},
"comparisons": {"cold_vs_warm": cold_warm, "full_vs_review": full_review},
"focus_mode": mode,
"focus_checks": focus_checks,
}
_write(args.evidence_root / "prepare_summary.json", summary)
return summary
def _classify(prepare: Mapping[str, Any], restart_compare: Mapping[str, Any]) -> dict[str, Any]:
cold_first = prepare["comparisons"]["cold_vs_warm"]["first_divergence"]
context_first = prepare["comparisons"]["full_vs_review"]["first_divergence"]
restart_first = restart_compare["first_divergence"]
first = context_first or cold_first or restart_first
standalone_first = cold_first or restart_first
if standalone_first is None and context_first is not None:
case = "CASE F"
reason = "standalone repeats match; Full/Review context is the only divergence"
elif first in ("P0", "P1"):
case = "CASE A"
reason = "resize/input path is the first divergence"
elif first == "P2":
case = "CASE B"
reason = "Video VAE identity latent is the first divergence"
elif first in ("P3", "P4", "P5", "P6"):
case = "CASE C"
reason = "Qwen/CLIP image-conditioning path is the first divergence"
elif first == "P7":
case = "CASE D"
reason = "conditioning assembly/keyframe attachment is the first divergence"
else:
case = "CASE E"
reason = "P0-P7 are bit-exact; the G4 hypothesis must move to Sampling or later runtime ordering"
return {"case": case, "first_divergence": first, "reason": reason}
def _short(value: str) -> str:
return value[:12]
def _report_markdown(summary: Mapping[str, Any]) -> str:
prepare = summary["prepare"]
restart = summary["restart_c"]
full = prepare["runs"]["full"]
review = prepare["runs"]["review"]
repeat_cmp = prepare["comparisons"]["cold_vs_warm"]
context_cmp = prepare["comparisons"]["full_vs_review"]
restart_cmp = summary["comparisons"]["cold_vs_restart"]
lines = [
"# G4-Diagnostic FL2VA Conditioning Determinism — STOP Report",
"",
f"Classification: **{summary['classification']['case']}** ",
f"First divergence: **{summary['classification']['first_divergence'] or 'none through P7'}** ",
f"Result: {summary['classification']['reason']}",
"",
"Production code was not changed. GPU Sampling was not executed.",
"",
"| Checkpoint | Full | Review | Full/Review | Repeat | Restart |",
"|---|---:|---:|---|---|---|",
]
for checkpoint in CHECKPOINTS:
full_sha = _checkpoint_sha(full, checkpoint)
review_sha = _checkpoint_sha(review, checkpoint)
lines.append(
f"| {checkpoint} | `{_short(full_sha)}` | `{_short(review_sha)}` | "
f"{'PASS' if context_cmp['checkpoints'][checkpoint]['match'] else 'FAIL'} | "
f"{'PASS' if repeat_cmp['checkpoints'][checkpoint]['match'] else 'FAIL'} | "
f"{'PASS' if restart_cmp['checkpoints'][checkpoint]['match'] else 'FAIL'} |"
)
lines.extend(
[
"",
"## Focused repeat",
"",
"```json",
json.dumps(prepare.get("focus_checks"), indent=2, ensure_ascii=False),
"```",
"",
"## Runtime state",
"",
"Cold, warm, Full-context, Review-context, and restart state snapshots are retained in the JSON evidence. Patch UUID creation differences across restart are recorded but are not treated as tensor divergence by themselves.",
"",
"## Next action",
"",
"STOP. Present the first divergence, impact, and a minimal branch-specific fix proposal for approval. Do not add caches, deterministic-mode forcing, model reloads, dtype/seed changes, or Run Storage contract changes in this Gate.",
"",
]
)
return "\n".join(lines)
def _restart(args) -> dict[str, Any]:
prepare_path = args.evidence_root / "prepare_summary.json"
if not prepare_path.is_file():
raise GateFailure(f"prepare summary missing: {prepare_path}")
prepare = json.loads(prepare_path.read_text(encoding="utf-8"))
restart = _run_case(args, name="restart_c", context="Restart C", focus_mode="standard", nonce=101)
cold_restart = _compare(prepare["runs"]["cold_a"], restart)
classification = _classify(prepare, cold_restart)
summary = {
"format": "h3-v38-g4-conditioning-determinism-final-v1",
"stage": "stopped",
"prepare": prepare,
"restart_c": restart,
"comparisons": {"cold_vs_restart": cold_restart},
"classification": classification,
}
_write(args.evidence_root / "g4_conditioning_determinism_summary.json", summary)
report = _report_markdown(summary)
(args.evidence_root / "G4_CONDITIONING_DETERMINISM_STOP_REPORT.md").write_text(
report,
encoding="utf-8",
)
return summary
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--stage", choices=("prepare", "restart"), required=True)
parser.add_argument("--server", default="http://127.0.0.6:8190")
parser.add_argument(
"--evidence-root",
type=Path,
default=Path(
r"D:\Codex\_test_results\ComfyUI-H3-Continuum"
r"\v38-g4-conditioning-determinism-20260831_150837"
),
)
parser.add_argument("--timeout-seconds", type=float, default=1800.0)
parser.add_argument("--poll-seconds", type=float, default=1.0)
return parser.parse_args()
def main() -> int:
args = parse_args()
args.evidence_root.mkdir(parents=True, exist_ok=True)
summary = _prepare(args) if args.stage == "prepare" else _restart(args)
print(json.dumps(summary.get("classification", {"stage": args.stage}), ensure_ascii=False), flush=True)
return 0
if __name__ == "__main__":
raise SystemExit(main())
+719
View File
@@ -0,0 +1,719 @@
"""Run and compare the G4 Sampling determinism Order Matrix."""
from __future__ import annotations
import argparse
import copy
import hashlib
import json
from pathlib import Path
import sys
from typing import Any, Mapping
import uuid
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
from p32_api_runner import submit_and_wait
from v36_r1_stress_gate_runner import ResourceMonitor
SAMPLING_CHECKPOINTS = ("S0", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8")
MODEL_CHECKPOINTS = ("M0", "M1", "M2", "M3")
ORIGINAL_G4 = {
"full": {
"video": "d6da9472755a991898378543356a1e57960e8aece0803bd9918c20422cb66805",
"audio": "dab3e1ee457cd1dc1d580cba8d47a6679f1a37d40b949a2fac7c63a47a3b7768",
},
"full_then_review": {
"video": "140064e63344d75e535f48ec49e488ca574cc069ee920cdaa507801c3504fb43",
"audio": "4742f4d9eef6ffd64413cce2c052689fe13e4f2f98c914142039ab419aa908cf",
},
}
class GateFailure(RuntimeError):
pass
def _write(path: Path, value: Any) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(value, indent=2, ensure_ascii=False), encoding="utf-8")
def _sha256_file(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as stream:
for block in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(block)
return digest.hexdigest()
def _find_one(prompt: Mapping[str, Any], class_type: str) -> tuple[str, dict[str, Any]]:
matches = [
(str(node_id), node)
for node_id, node in prompt.items()
if str(node.get("class_type")) == class_type
]
if len(matches) != 1:
raise GateFailure(f"expected one {class_type}, found {len(matches)}")
return matches[0]
def _prompt(
template: Mapping[str, Any],
*,
label: str,
mode: str,
run_name: str,
output_prefix: str,
nonce: int,
trace_steps: bool,
) -> tuple[dict[str, Any], str]:
prompt = copy.deepcopy(dict(template))
node_id, sampler = _find_one(prompt, "H3ContinuumSamplerV38")
sampler["class_type"] = "H3G4SamplingDeterminismDiagnostic"
sampler["_meta"] = {"title": "H3 G4 Sampling Determinism Diagnostic"}
sampler["inputs"].update(
{
"aspect": "Square 1:1",
"preset": "Draft — 0.30 MP",
"custom_mp": 0.30,
"chunks": 3,
"chunk_seconds": 5.0,
"continuity": "Balanced — 22 frames",
"base_seed": 927966463230733,
"audio_continuity": True,
"diagnostics": "Basic",
"reroll_from_chunk": "Auto",
"reroll_nonce": 0,
"strict_compatibility": False,
"debug": False,
"show_preview": False,
"run_storage": "Save + Auto Resume",
"run_name": run_name,
"reference_size": "Match Output",
"project_id": str(uuid.uuid5(uuid.NAMESPACE_URL, run_name)),
"video_reference_size": "Efficient - 0.4 MP",
"continuation_backend": "Standard",
"generation_mode": mode,
"review_action": "Continue / Next",
"diagnostic_label": label,
"diagnostic_nonce": int(nonce),
"trace_steps": bool(trace_steps),
}
)
_, save_video = _find_one(prompt, "SaveVideo")
save_video["inputs"]["filename_prefix"] = output_prefix
return prompt, node_id
def _extract(history: Mapping[str, Any], prompt_id: str, node_id: str) -> dict[str, Any]:
record = history.get(prompt_id)
if not isinstance(record, Mapping):
raise GateFailure(f"history record missing for {prompt_id}")
outputs = record.get("outputs")
if not isinstance(outputs, Mapping):
raise GateFailure("history outputs missing")
node = outputs.get(str(node_id))
if not isinstance(node, Mapping):
raise GateFailure(f"diagnostic output missing for node {node_id}")
text = node.get("text")
if not isinstance(text, list) or len(text) != 1:
raise GateFailure(f"unexpected diagnostic text output: {text!r}")
result = json.loads(str(text[0]))
if result.get("format") != "h3-v38-g4-sampling-determinism-v1":
raise GateFailure("unexpected diagnostic format")
if not result.get("hooks_restored"):
raise GateFailure("diagnostic hooks were not restored")
for sample in result.get("physical_samples", []):
complete = sample.get("capture_complete") or {}
if not complete.get("all_sampling_checkpoints"):
raise GateFailure(f"sampling checkpoints incomplete: {complete}")
if not complete.get("all_model_checkpoints"):
raise GateFailure(f"model checkpoints incomplete: {complete}")
return result
def _run_case(
args,
template: Mapping[str, Any],
*,
label: str,
mode: str,
nonce: int,
run_name: str,
) -> dict[str, Any]:
case_root = args.evidence_root / f"stage_{args.stage.lower()}" / label
prompt, node_id = _prompt(
template,
label=label,
mode=mode,
run_name=run_name,
output_prefix=f"v38_g4_sampling_diagnostic/{args.run_tag}/{label}",
nonce=nonce,
trace_steps=args.trace_steps,
)
_write(case_root / "prompt.json", prompt)
print(f"G4 Sampling diagnostic start: {label}", flush=True)
with ResourceMonitor(args.backend_pid, interval_seconds=args.monitor_interval) as monitor:
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=prompt,
client_id=f"g4sampling-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=args.poll_seconds,
)
_write(case_root / "history.json", history)
result = _extract(history, prompt_id, node_id)
_write(case_root / "diagnostic.json", result)
output = {
"label": label,
"mode": mode,
"run_name": run_name,
"prompt_id": prompt_id,
"api_elapsed_seconds": elapsed,
"resources": monitor.as_dict(),
"prompt": str(case_root / "prompt.json"),
"history": str(case_root / "history.json"),
"diagnostic_path": str(case_root / "diagnostic.json"),
"diagnostic": result,
}
_write(case_root / "result.json", output)
print(f"G4 Sampling diagnostic end: {label}; {elapsed:.3f}s", flush=True)
return output
def _stage(args) -> dict[str, Any]:
template = json.loads(args.template_prompt.read_text(encoding="utf-8"))
prefix = f"v38_g4_sampling_{args.run_tag}_{args.stage.lower()}"
definitions = {
"A": [("A_fresh_full", "Full Run", f"{prefix}_full")],
"B": [("B_fresh_review", "Review Each Chunk", f"{prefix}_review")],
"C": [
("C_full", "Full Run", f"{prefix}_full"),
("C_review_after_full", "Review Each Chunk", f"{prefix}_review"),
],
"D": [
("D_review_1", "Review Each Chunk", f"{prefix}_review_1"),
("D_review_2", "Review Each Chunk", f"{prefix}_review_2"),
],
}
runs = []
for offset, (label, mode, run_name) in enumerate(definitions[args.stage], 1):
run_root = args.run_storage_root / run_name
if run_root.exists():
raise GateFailure(f"refusing existing Run Storage: {run_root}")
runs.append(
_run_case(
args,
template,
label=label,
mode=mode,
nonce=args.nonce_base + offset,
run_name=run_name,
)
)
result = {
"format": "h3-v38-g4-sampling-determinism-stage-v1",
"stage": args.stage,
"backend_pid": args.backend_pid,
"backend_log": str(args.backend_log),
"server": args.server,
"run_tag": args.run_tag,
"trace_steps": bool(args.trace_steps),
"runner_sha256": _sha256_file(Path(__file__)),
"template_sha256": _sha256_file(args.template_prompt),
"runs": runs,
}
_write(args.evidence_root / f"stage_{args.stage.lower()}.json", result)
return result
def _first_sample(run: Mapping[str, Any]) -> Mapping[str, Any]:
samples = run["diagnostic"].get("physical_samples") or []
if not samples:
raise GateFailure(f"no physical samples in {run.get('label')}")
return samples[0]
def _normalized_payload(value: Any) -> Any:
if isinstance(value, Mapping):
if value.get("__type__") == "uuid.UUID" and "value" in value:
return {"__type__": "uuid.UUID", "present": True}
return {
str(key): _normalized_payload(item)
for key, item in sorted(value.items(), key=lambda item: str(item[0]))
if str(key) != "legacy_g4_sha256"
}
if isinstance(value, list):
return [_normalized_payload(item) for item in value]
return value
def _comparison_sha(checkpoint: Mapping[str, Any]) -> str:
if checkpoint.get("kind") != "structured" or "payload" not in checkpoint:
return str(checkpoint["sha256"])
payload = _normalized_payload(checkpoint["payload"])
encoded = json.dumps(
payload,
sort_keys=True,
separators=(",", ":"),
ensure_ascii=False,
).encode("utf-8")
return hashlib.sha256(encoded).hexdigest()
def _sha(value: Mapping[str, Any], name: str) -> str:
return _comparison_sha(value[name])
def _compare_samples(left_run: Mapping[str, Any], right_run: Mapping[str, Any]) -> dict[str, Any]:
left = _first_sample(left_run)
right = _first_sample(right_run)
checkpoints: dict[str, Any] = {}
first_sampling = None
for name in SAMPLING_CHECKPOINTS:
left_sha = _sha(left["checkpoints"], name)
right_sha = _sha(right["checkpoints"], name)
match = left_sha == right_sha
checkpoints[name] = {
"left_sha256": left_sha,
"right_sha256": right_sha,
"match": match,
}
if first_sampling is None and not match:
first_sampling = name
model: dict[str, Any] = {}
first_model = None
for name in MODEL_CHECKPOINTS:
left_sha = _sha(left["model"], name)
right_sha = _sha(right["model"], name)
match = left_sha == right_sha
model[name] = {
"left_sha256": left_sha,
"right_sha256": right_sha,
"match": match,
}
if first_model is None and not match:
first_model = name
output = {
"left_sha256": str(left["output"]["sha256"]),
"right_sha256": str(right["output"]["sha256"]),
"match": left["output"]["sha256"] == right["output"]["sha256"],
"left_streams": left["output"]["streams"],
"right_streams": right["output"]["streams"],
}
return {
"left": left_run["label"],
"right": right_run["label"],
"first_sampling_divergence": first_sampling,
"sampling": checkpoints,
"first_model_divergence": first_model,
"model": model,
"output": output,
}
def _classification(comparison: Mapping[str, Any]) -> dict[str, Any]:
first_s = comparison.get("first_sampling_divergence")
if (
first_s is None
and comparison.get("first_model_divergence") is None
and comparison["output"]["match"]
):
return {
"case": "NO DIVERGENCE REPRODUCED",
"reason": "S0-S8, M0-M3, and the final sampled AV latent are exact",
}
if first_s == "S0":
case, reason = "CASE S-A", "target latent construction differs"
elif first_s == "S2":
case, reason = "CASE S-B", "prepare_noise / RNG path differs"
elif first_s == "S3":
case, reason = "CASE S-C", "denoise mask differs"
elif first_s == "S8":
case, reason = "CASE S-D", "model runtime signature differs before Sampling"
elif first_s is not None:
case, reason = "CASE S-OTHER", f"Sampling boundary first differs at {first_s}"
else:
first_m = comparison.get("first_model_divergence")
if first_m is None:
case, reason = "CASE M-E", "M0-M3 match; step trace is required"
elif first_m == "M3" and all(
comparison["model"][name]["match"] for name in ("M0", "M1", "M2")
):
case, reason = (
"CASE M-FORWARD",
"identical first model inputs produce a different first model output",
)
else:
case, reason = "CASE M-INPUT", f"first model-call difference is {first_m}"
return {"case": case, "reason": reason}
def _stream_pair(sample: Mapping[str, Any]) -> dict[str, str]:
streams = sample["output"]["streams"]
return {
"video": str(
streams["video"].get("legacy_g4_sha256", streams["video"]["sha256"])
),
"audio": str(
streams["audio"].get("legacy_g4_sha256", streams["audio"]["sha256"])
),
}
def _short(value: str) -> str:
return value[:12]
def _gib(value: int | float) -> str:
return f"{float(value) / (1024 ** 3):.2f}"
def _report(summary: Mapping[str, Any]) -> str:
lines = [
"# G4 Sampling Determinism Diagnostic — STOP Report",
"",
f"Classification: **{summary['classification']['case']}** ",
f"First divergence: **{summary['first_divergence']}** ",
f"Result: {summary['classification']['reason']}",
"",
"Production code and defaults were not changed.",
"",
"## Order Matrix",
"",
"| Order | Left output | Right output | Result |",
"|---|---:|---:|---|",
]
for name, comparison in summary["order_matrix"].items():
output = comparison["output"]
s8 = comparison["sampling"]["S8"]
lines.append(
f"| {name} | `{_short(output['left_sha256'])}` | "
f"`{_short(output['right_sha256'])}` | "
f"{'PASS' if output['match'] else 'FAIL'} "
f"(S8 state {'exact' if s8['match'] else 'changed'}) |"
)
lines.extend(
[
"",
"The sampled AV latent is exact in every accepted order. In the same-backend "
"C and D comparisons, S8 records the expected warm-residency transition: "
"`model_device` changes from `cpu` to `cuda:0`, and "
"`current_weight_patch_uuid_present` changes from `false` to `true`. "
"M0-M3 and the final AV latent remain exact, so this observed S8 state "
"transition is not causal in the reproduced runs.",
]
)
focus = summary["focus_comparison"]
lines.extend(
[
"",
"## Sampling Boundary",
"",
"| Checkpoint | Left SHA | Right SHA | Result |",
"|---|---:|---:|---|",
]
)
for name in SAMPLING_CHECKPOINTS:
row = focus["sampling"][name]
lines.append(
f"| {name} | `{_short(row['left_sha256'])}` | "
f"`{_short(row['right_sha256'])}` | "
f"{'PASS' if row['match'] else 'FAIL'} |"
)
lines.extend(
[
"",
"## First model call",
"",
"| Checkpoint | Left SHA | Right SHA | Result |",
"|---|---:|---:|---|",
]
)
for name in MODEL_CHECKPOINTS:
row = focus["model"][name]
lines.append(
f"| {name} | `{_short(row['left_sha256'])}` | "
f"`{_short(row['right_sha256'])}` | "
f"{'PASS' if row['match'] else 'FAIL'} |"
)
lines.extend(
[
"",
"S7 equals the previously accepted P7 conditioning hash in all six runs. "
"A step trace was not run because M0-M3 and the final output are exact. "
"Accelerator A/B was not run because no model-forward divergence was observed.",
"",
"## Performance and resources",
"",
"| Run | Sampling | API | Peak VRAM | Peak RSS | Min available RAM |",
"|---|---:|---:|---:|---:|---:|",
]
)
for label, item in summary["performance"].items():
resource = item["resources"]
lines.append(
f"| {label} | {item['sampling_seconds']:.2f}s | "
f"{item['api_seconds']:.2f}s | "
f"{_gib(resource['peak_device_used_bytes'])} GiB | "
f"{_gib(resource['peak_process_rss_bytes'])} GiB | "
f"{_gib(resource['minimum_system_available_bytes'])} GiB |"
)
lines.extend(
[
"",
"## Root-cause assessment and disposition",
"",
"- **Gate disposition:** STOP / inconclusive because the historical Full-to-Review "
"bit divergence is not reproducible in the current exact-order tests.",
"- **First sampled divergence:** None. S0-S7, M0-M3, and final AV are exact. "
"The C/D S8 difference is a non-causal warm model-residency state transition.",
"- **Root cause:** Unresolved transient runtime or numerical state in the historical "
"G4 run. Current evidence excludes target latent, fixed latent, noise, mask, "
"SIGMAS, seed, sampler, conditioning, and the first model call in the reproduced runs.",
"- **Minimal fix:** None is justified. Do not change Production. If further diagnosis "
"is approved, repeat only the exact public-node C order while capturing the startup, "
"extension, driver, kernel, and model-residency signature; enable a deferred/no-sync "
"step trace only after the divergent output is reproduced.",
"- **V3.7 / Full Run impact:** None now because no Production code changed. Speculative "
"model reload, attention, cache, dtype, or deterministic-mode changes could alter "
"V3.7 and Full Run performance or numerical behavior and therefore are not acceptable "
"without a reproduced cause.",
"- **Difficulty:** High. The historical condition is transient, non-reproducible, and "
"sensitive to observation timing.",
"",
"## Control and retained evidence",
"",
"The public-node/no-hook control reproduces the historical Full hash, while its "
"Review Queue 1 now matches Full rather than the historical divergent Review hash. "
"The legacy control runner began Queue 2 after Queue 1 passed; that out-of-scope "
"queue was interrupted immediately and is not a Product failure.",
"",
"```json",
json.dumps(summary["control"], indent=2, ensure_ascii=False),
"```",
"",
"STOP. No Production fix, accelerator change, deterministic-mode forcing, cache, model reload, seed/SIGMAS change, Run Storage change, commit, push, or release was performed.",
"",
]
)
return "\n".join(lines)
def _analyze(args) -> dict[str, Any]:
stages = {}
for stage in "ABCD":
path = args.evidence_root / f"stage_{stage.lower()}.json"
if path.is_file():
stages[stage] = json.loads(path.read_text(encoding="utf-8"))
required = {"A", "B", "C", "D"}
if set(stages) != required:
raise GateFailure(f"analysis requires A-D; found {sorted(stages)}")
a = stages["A"]["runs"][0]
b = stages["B"]["runs"][0]
c_full, c_review = stages["C"]["runs"]
d1, d2 = stages["D"]["runs"]
matrix = {
"Fresh Full vs Fresh Review": _compare_samples(a, b),
"Full -> Review": _compare_samples(c_full, c_review),
"Review -> Review": _compare_samples(d1, d2),
}
focus_name = "Full -> Review" if not matrix["Full -> Review"]["output"]["match"] else "Fresh Full vs Fresh Review"
focus = matrix[focus_name]
first = focus["first_sampling_divergence"] or focus["first_model_divergence"]
if first is None and not focus["output"]["match"]:
first = "Sampler step trace required"
elif first is None:
first = "none through final sampled latent"
classification = _classification(focus)
if all(item["output"]["match"] for item in matrix.values()):
s8_changed = [
name
for name, item in matrix.items()
if not item["sampling"]["S8"]["match"]
]
classification = {
"case": "NO DIVERGENCE REPRODUCED",
"reason": (
"S0-S7, M0-M3, and final sampled AV are exact in all orders; "
"S8 contains only the non-causal warm runtime-state transition "
f"in {', '.join(s8_changed)}"
if s8_changed
else "S0-S8, M0-M3, and final sampled AV are exact in all orders"
),
}
if s8_changed:
first = (
"S8 runtime state only in same-backend C/D; "
"no sampled bit divergence through final latent"
)
c_full_streams = _stream_pair(_first_sample(c_full))
c_review_streams = _stream_pair(_first_sample(c_review))
no_hook = None
if args.no_hook_control.is_file():
no_hook = json.loads(args.no_hook_control.read_text(encoding="utf-8"))
no_hook_full = None
no_hook_review = None
if no_hook is not None:
no_hook_full = {
"video": no_hook["results"]["full"]["run_storage"]["chunks"][0]["video_latent_sha256"],
"audio": no_hook["results"]["full"]["run_storage"]["chunks"][0]["audio_latent_sha256"],
}
no_hook_review = {
"video": no_hook["results"]["review_q1"]["run_storage"]["chunks"][0]["video_latent_sha256"],
"audio": no_hook["results"]["review_q1"]["run_storage"]["chunks"][0]["audio_latent_sha256"],
}
control = {
"hook_noninterference_current_no_hook_full": (
None if no_hook_full is None else c_full_streams == no_hook_full
),
"hook_noninterference_current_no_hook_review": (
None if no_hook_review is None else c_review_streams == no_hook_review
),
"observed_c_full": c_full_streams,
"observed_c_review": c_review_streams,
"current_no_hook_full": no_hook_full,
"current_no_hook_review": no_hook_review,
"historical_g4_full": ORIGINAL_G4["full"],
"historical_g4_divergent_review": ORIGINAL_G4["full_then_review"],
"historical_full_reproduced": c_full_streams == ORIGINAL_G4["full"],
"historical_divergent_review_reproduced": (
c_review_streams == ORIGINAL_G4["full_then_review"]
),
"current_no_hook_chunk_1_matches_full": (
None if no_hook_full is None else no_hook_full == no_hook_review
),
"all_hooks_restored": all(
run["diagnostic"].get("hooks_restored")
for stage in stages.values()
for run in stage["runs"]
),
}
p7 = None
if args.conditioning_summary.is_file():
conditioning = json.loads(args.conditioning_summary.read_text(encoding="utf-8"))
p7 = _comparison_sha(
conditioning["prepare"]["runs"]["full"]["result"]["checkpoints"]["P7"]
)
s7_checks = {}
for run in (a, b, c_full, c_review, d1, d2):
sample = _first_sample(run)
s7 = _comparison_sha(sample["checkpoints"]["S7"])
s7_checks[run["label"]] = {
"s7_sha256": s7,
"p7_sha256": p7,
"matches_p7": None if p7 is None else s7 == p7,
}
resources = {
run["label"]: run["resources"]
for stage in stages.values()
for run in stage["runs"]
}
performance = {
run["label"]: {
"sampling_seconds": float(
run["diagnostic"]["sampler_node_elapsed_seconds"]
),
"api_seconds": float(run["api_elapsed_seconds"]),
"resources": run["resources"],
}
for stage in stages.values()
for run in stage["runs"]
}
summary = {
"format": "h3-v38-g4-sampling-determinism-final-v1",
"stage": "stopped",
"production_changed": False,
"focus": focus_name,
"first_divergence": first,
"classification": classification,
"order_matrix": matrix,
"focus_comparison": focus,
"control": control,
"p7_parity": s7_checks,
"resources": resources,
"performance": performance,
"stages": {
stage: str(args.evidence_root / f"stage_{stage.lower()}.json")
for stage in stages
},
}
_write(args.evidence_root / "g4_sampling_determinism_summary.json", summary)
(args.evidence_root / "G4_SAMPLING_DETERMINISM_STOP_REPORT.md").write_text(
_report(summary),
encoding="utf-8",
)
return summary
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()
parser.add_argument("--stage", choices=("A", "B", "C", "D", "analyze"), required=True)
parser.add_argument("--server", default="http://127.0.0.6:8190")
parser.add_argument("--template-prompt", type=Path)
parser.add_argument("--evidence-root", type=Path, required=True)
parser.add_argument("--run-storage-root", type=Path)
parser.add_argument("--run-tag", default="20260831_154505")
parser.add_argument("--backend-pid", type=int)
parser.add_argument("--backend-log", type=Path)
parser.add_argument("--nonce-base", type=int, default=1000)
parser.add_argument("--trace-steps", action="store_true")
parser.add_argument("--timeout-seconds", type=float, default=2400.0)
parser.add_argument("--poll-seconds", type=float, default=2.0)
parser.add_argument("--monitor-interval", type=float, default=1.0)
parser.add_argument(
"--conditioning-summary",
type=Path,
default=Path(
r"D:\Codex\_test_results\ComfyUI-H3-Continuum"
r"\v38-g4-conditioning-determinism-20260831_150837"
r"\g4_conditioning_determinism_summary.json"
),
)
parser.add_argument(
"--no-hook-control",
type=Path,
default=Path(
r"D:\Codex\_test_results\ComfyUI-H3-Continuum"
r"\v38-g4-sampling-determinism-20260831_154505"
r"\no_hook_control\gpu_r1\g4_summary.json"
),
)
return parser
def main() -> int:
args = _parser().parse_args()
args.evidence_root.mkdir(parents=True, exist_ok=True)
if args.stage != "analyze":
if args.template_prompt is None or args.run_storage_root is None:
raise GateFailure("stage execution requires template prompt and Run Storage root")
if args.backend_pid is None or args.backend_log is None:
raise GateFailure("stage execution requires backend PID and log")
result = _stage(args)
else:
result = _analyze(args)
print(
json.dumps(
{
"stage": result.get("stage"),
"first_divergence": result.get("first_divergence"),
"classification": result.get("classification"),
"summary": str(args.evidence_root / "g4_sampling_determinism_summary.json"),
},
indent=2,
ensure_ascii=False,
),
flush=True,
)
return 0
if __name__ == "__main__":
raise SystemExit(main())
+430
View File
@@ -0,0 +1,430 @@
"""Run the three-case V3.8 main-sampler resolution GPU mini Gate."""
from __future__ import annotations
import argparse
import copy
from dataclasses import asdict, dataclass
import hashlib
import json
from pathlib import Path
import shutil
import subprocess
import sys
from typing import Any, Mapping
import uuid
import numpy as np
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
from p32_api_runner import _json_request, submit_and_wait
from v36_r1_stress_gate_runner import ResourceMonitor
from v38_easy_gpu_gate_runner import _output_media, _probe_media
class GateFailure(RuntimeError):
pass
@dataclass(frozen=True)
class GateCase:
name: str
aspect: str
preset: str
custom_mp: float
first_image: bool
expected_width: int
expected_height: int
prompt: str
CASES = (
GateCase(
name="g1_draft_landscape",
aspect="Landscape 16:9",
preset="Draft — 0.30 MP",
custom_mp=0.30,
first_image=False,
expected_width=736,
expected_height=416,
prompt=(
"A continuous cinematic landscape shot tracks a woman walking across "
"a modern plaza. Natural body motion, stable lighting, footsteps, wind, "
"and distant city ambience continue without a cut."
),
),
GateCase(
name="g2_balanced_portrait",
aspect="Portrait 9:16",
preset="Balanced — 0.60 MP",
custom_mp=0.30,
first_image=False,
expected_width=576,
expected_height=1024,
prompt=(
"A continuous portrait-format cinematic shot follows a woman walking "
"toward camera through a city street. Preserve coherent anatomy, natural "
"motion, stable exposure, footsteps, and urban ambience."
),
),
GateCase(
name="g3_native_auto_first_image",
aspect="Auto from First Image",
preset="Native 768",
custom_mp=0.30,
first_image=True,
expected_width=768,
expected_height=1024,
prompt=(
"A continuous cinematic shot begins from the supplied First Image. The "
"subject moves naturally while identity, clothing, lighting, camera "
"direction, footsteps, and ambience remain coherent."
),
),
)
def _sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as stream:
for block in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(block)
return digest.hexdigest()
def _resolve_origin(
node_id: str,
output_slot: int,
*,
nodes: Mapping[str, Mapping[str, Any]],
links: Mapping[str, list[Any]],
seen: set[tuple[str, int]] | None = None,
) -> list[Any] | None:
seen = set(seen or ())
key = (str(node_id), int(output_slot))
if key in seen:
return None
seen.add(key)
node = nodes.get(str(node_id))
if not isinstance(node, Mapping):
return None
mode = int(node.get("mode", 0) or 0)
if mode == 0:
return [str(node_id), int(output_slot)]
if mode != 4:
return None
outputs = list(node.get("outputs") or [])
output_type = outputs[output_slot].get("type") if output_slot < len(outputs) else None
candidates = []
for input_value in node.get("inputs") or []:
link_id = input_value.get("link")
input_type = input_value.get("type")
if link_id is None:
continue
if output_type not in (None, "*") and input_type not in (output_type, "*"):
continue
candidates.append(str(link_id))
if len(candidates) != 1:
return None
link = links.get(candidates[0])
if not link:
return None
return _resolve_origin(
str(link[1]),
int(link[2]),
nodes=nodes,
links=links,
seen=seen,
)
def _build_prompt(
workflow: Mapping[str, Any],
object_info: Mapping[str, Any],
*,
case: GateCase,
image_name: str,
output_prefix: str,
seed: int,
) -> dict[str, Any]:
nodes = {str(node["id"]): copy.deepcopy(node) for node in workflow["nodes"]}
links = {str(link[0]): list(link) for link in workflow["links"]}
sampler_ids = [node_id for node_id, node in nodes.items() if node["type"] == "H3ContinuumSamplerV37"]
if len(sampler_ids) != 1:
raise GateFailure(f"expected one V3.7 source sampler, found {len(sampler_ids)}")
sampler_id = sampler_ids[0]
nodes[sampler_id]["type"] = "H3ContinuumSamplerV38"
nodes[sampler_id].setdefault("properties", {})["Node name for S&R"] = (
"H3ContinuumSamplerV38"
)
sampler_named = dict(nodes[sampler_id].get("widgets_values_named") or {})
sampler_named.pop("width", None)
sampler_named.pop("height", None)
sampler_named.update(
{
"aspect": case.aspect,
"preset": case.preset,
"custom_mp": case.custom_mp,
"chunks": 1,
"chunk_seconds": 5.0,
"base_seed": int(seed),
"diagnostics": "Detailed Report",
"show_preview": False,
"run_storage": "Off",
"run_name": "",
"project_id": f"v38-resolution-{case.name}-{seed}",
}
)
nodes[sampler_id]["widgets_values_named"] = sampler_named
for node in nodes.values():
if node["type"] == "PrimitiveStringMultiline":
node.setdefault("widgets_values_named", {})["value"] = case.prompt
elif node["type"] == "LoadImage" and int(node["id"]) == 114:
node.setdefault("widgets_values_named", {})["image"] = image_name
elif node["type"] == "SaveVideo":
node.setdefault("widgets_values_named", {})["filename_prefix"] = output_prefix
prompt: dict[str, Any] = {}
def add_node(node_id: str) -> None:
if node_id in prompt:
return
node = nodes[node_id]
if int(node.get("mode", 0) or 0) != 0:
return
class_type = str(node["type"])
info = object_info.get(class_type)
if not isinstance(info, Mapping):
raise GateFailure(f"object_info has no node class {class_type}")
valid_inputs = set()
input_schema = info.get("input") or {}
valid_inputs.update((input_schema.get("required") or {}).keys())
valid_inputs.update((input_schema.get("optional") or {}).keys())
api_inputs: dict[str, Any] = {}
for input_value in node.get("inputs") or []:
name = str(input_value.get("name"))
if name in {"width", "height"}:
continue
if node_id == sampler_id and name == "first_frame" and not case.first_image:
continue
link_id = input_value.get("link")
if link_id is None or name not in valid_inputs:
continue
link = links.get(str(link_id))
if not link:
continue
origin = _resolve_origin(
str(link[1]),
int(link[2]),
nodes=nodes,
links=links,
)
if origin is None:
continue
api_inputs[name] = origin
add_node(str(origin[0]))
for name, value in (node.get("widgets_values_named") or {}).items():
if name in valid_inputs and name not in api_inputs:
api_inputs[str(name)] = value
prompt[node_id] = {
"class_type": class_type,
"inputs": api_inputs,
"_meta": {"title": str(node.get("title") or class_type)},
}
output_ids = [
node_id
for node_id, node in nodes.items()
if node["type"] == "SaveVideo" and int(node.get("mode", 0) or 0) == 0
]
if len(output_ids) != 1:
raise GateFailure(f"expected one active SaveVideo, found {len(output_ids)}")
add_node(output_ids[0])
if sampler_id not in prompt:
raise GateFailure("V3.8 sampler is not connected to the SaveVideo output")
return prompt
def _media_health(path: Path, ffmpeg: str) -> dict[str, Any]:
video_result = subprocess.run(
[
ffmpeg,
"-v",
"error",
"-i",
str(path),
"-vf",
"fps=1,scale=64:64",
"-pix_fmt",
"rgb24",
"-f",
"rawvideo",
"-",
],
check=True,
capture_output=True,
)
frame_size = 64 * 64 * 3
frame_count = len(video_result.stdout) // frame_size
frames = np.frombuffer(video_result.stdout[: frame_count * frame_size], dtype=np.uint8)
frames = frames.reshape(frame_count, 64, 64, 3)
frame_means = frames.mean(axis=(1, 2, 3))
video_std = float(frames.astype(np.float32).std())
audio_result = subprocess.run(
[
ffmpeg,
"-v",
"error",
"-i",
str(path),
"-map",
"0:a:0",
"-f",
"f32le",
"-acodec",
"pcm_f32le",
"-",
],
check=True,
capture_output=True,
)
audio = np.frombuffer(audio_result.stdout, dtype=np.float32)
return {
"sampled_video_frames": int(frame_count),
"video_mean": float(frames.mean()),
"video_std": video_std,
"black_sample_fraction": float(np.mean(frame_means < 2.0)),
"video_not_black_or_collapsed": bool(
frame_count > 0 and video_std > 2.0 and np.any(frame_means >= 2.0)
),
"audio_samples": int(audio.size),
"audio_finite": bool(audio.size > 0 and np.isfinite(audio).all()),
"audio_peak": float(np.max(np.abs(audio))) if audio.size else 0.0,
}
def run(args) -> dict[str, Any]:
workflow = json.loads(args.workflow.read_text(encoding="utf-8"))
object_info = _json_request(f"{args.server.rstrip('/')}/object_info")
if "H3ContinuumSamplerV38" not in object_info:
raise GateFailure("runtime has no H3ContinuumSamplerV38")
ffmpeg = shutil.which("ffmpeg")
ffprobe = shutil.which("ffprobe")
if not ffmpeg or not ffprobe:
raise GateFailure("ffmpeg and ffprobe are required")
args.output_root.mkdir(parents=True, exist_ok=True)
results = []
for index, case in enumerate(CASES, start=1):
case_root = args.output_root / case.name
case_root.mkdir(parents=True, exist_ok=True)
prompt = _build_prompt(
workflow,
object_info,
case=case,
image_name=args.image_name,
output_prefix=f"v38_resolution_gate/{case.name}",
seed=args.seed,
)
prompt_path = case_root / "prompt.json"
prompt_path.write_text(
json.dumps(prompt, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(
f"V3.8 Resolution Gate start {index}/{len(CASES)} {case.name}",
flush=True,
)
if args.dry_run:
results.append({"case": asdict(case), "prompt": str(prompt_path)})
continue
with ResourceMonitor(args.backend_pid, args.monitor_interval) as monitor:
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=prompt,
client_id=f"v38-resolution-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=args.poll_seconds,
)
history_path = case_root / "history.json"
history_path.write_text(
json.dumps(history, indent=2, ensure_ascii=False), encoding="utf-8"
)
media_path = _output_media(history, args.media_root)
media = _probe_media(media_path, ffprobe)
health = _media_health(media_path, ffmpeg)
checks = {
"geometry": (media["width"], media["height"])
== (case.expected_width, case.expected_height),
"frames": int(media["frames"]) == 120,
"fps": str(media["fps"]) == "24/1",
"duration": abs(float(media["format_duration"]) - 5.0) <= 0.02,
"audio_rate": int(media["audio_sample_rate"]) == 32000,
"audio_channels": int(media["audio_channels"]) == 2,
"audio_finite": bool(health["audio_finite"]),
"video_not_black_or_collapsed": bool(
health["video_not_black_or_collapsed"]
),
}
if not all(checks.values()):
raise GateFailure(f"{case.name} failed: {checks}")
result = {
"case": asdict(case),
"prompt_id": prompt_id,
"api_elapsed_seconds": float(elapsed),
"prompt": str(prompt_path),
"history": str(history_path),
"media": media,
"health": health,
"resource": monitor.as_dict(),
"checks": checks,
}
results.append(result)
print(
f"V3.8 Resolution Gate PASS {case.name} "
f"{media['width']}x{media['height']} {elapsed:.3f}s",
flush=True,
)
summary = {
"format": "h3-continuum-v38-main-resolution-gpu-gate-v1",
"server": args.server,
"backend_pid": args.backend_pid,
"workflow": str(args.workflow),
"workflow_sha256": _sha256(args.workflow),
"image_name": args.image_name,
"seed": args.seed,
"dry_run": bool(args.dry_run),
"pass": bool(not args.dry_run and len(results) == len(CASES)),
"results": results,
}
(args.output_root / "gate_summary.json").write_text(
json.dumps(summary, indent=2, ensure_ascii=False), encoding="utf-8"
)
return summary
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--server", default="http://127.0.0.6:8190")
parser.add_argument("--workflow", type=Path, required=True)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument("--media-root", type=Path, required=True)
parser.add_argument("--image-name", required=True)
parser.add_argument("--backend-pid", type=int, default=None)
parser.add_argument("--seed", type=int, default=927966463230733)
parser.add_argument("--timeout-seconds", type=float, default=1200.0)
parser.add_argument("--poll-seconds", type=float, default=2.0)
parser.add_argument("--monitor-interval", type=float, default=1.0)
parser.add_argument("--dry-run", action="store_true")
args = parser.parse_args()
summary = run(args)
print(json.dumps(summary, indent=2, ensure_ascii=False), flush=True)
return 0
if __name__ == "__main__":
raise SystemExit(main())
+415
View File
@@ -0,0 +1,415 @@
"""Run the Phase F Full-vs-Review GPU parity checkpoint.
This first checkpoint intentionally submits identical V3.8 sampler inputs for
successive Review/Continue queues. It therefore exercises the same ComfyUI
cache contract as a fixed-seed workflow instead of adding a test-only cache
nonce that could hide a Production integration defect.
"""
from __future__ import annotations
import argparse
import copy
import hashlib
import json
from pathlib import Path
import shutil
import subprocess
import sys
from typing import Any, Mapping
import uuid
TOOLS_ROOT = Path(__file__).resolve().parent
if str(TOOLS_ROOT) not in sys.path:
sys.path.insert(0, str(TOOLS_ROOT))
from p32_api_runner import submit_and_wait
from v36_r1_stress_gate_runner import ResourceMonitor
from v38_easy_gpu_gate_runner import _output_media, _probe_media
class GateFailure(RuntimeError):
pass
def _sha256_file(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as stream:
for block in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(block)
return digest.hexdigest()
def _find_one(prompt: Mapping[str, Any], class_type: str) -> tuple[str, dict[str, Any]]:
matches = [
(str(node_id), node)
for node_id, node in prompt.items()
if str(node.get("class_type")) == class_type
]
if len(matches) != 1:
raise GateFailure(f"expected one {class_type}, found {len(matches)}")
return matches[0]
def _build_prompt(
template: Mapping[str, Any],
*,
run_name: str,
generation_mode: str,
review_action: str,
output_prefix: str,
seed: int,
) -> dict[str, Any]:
prompt = copy.deepcopy(dict(template))
_, sampler = _find_one(prompt, "H3ContinuumSamplerV38")
inputs = sampler["inputs"]
inputs.update(
{
"aspect": "Landscape 16:9",
"preset": "Draft — 0.30 MP",
"custom_mp": 0.30,
"chunks": 3,
"chunk_seconds": 10.0,
"base_seed": int(seed),
"diagnostics": "Detailed Report",
"reroll_from_chunk": "Auto",
"reroll_nonce": 0,
"strict_compatibility": False,
"debug": True,
"show_preview": False,
"run_storage": "Save + Auto Resume",
"run_name": run_name,
"project_id": str(uuid.uuid5(uuid.NAMESPACE_URL, run_name)),
"generation_mode": generation_mode,
"review_action": review_action,
}
)
_, save_video = _find_one(prompt, "SaveVideo")
save_video["inputs"]["filename_prefix"] = output_prefix
return prompt
def _latest_manifest(run_root: Path) -> tuple[Path, dict[str, Any]]:
candidates = list((run_root / "revisions").glob("*/manifest.json"))
if not candidates:
raise GateFailure(f"Run Storage manifest is missing: {run_root}")
candidates.sort(key=lambda path: path.stat().st_mtime_ns, reverse=True)
path = candidates[0]
return path, json.loads(path.read_text(encoding="utf-8"))
def _tensor_sha256(path: Path, key: str) -> str:
from safetensors import safe_open
with safe_open(path, framework="pt", device="cpu") as handle:
tensor = handle.get_tensor(key).contiguous()
digest = hashlib.sha256()
digest.update(str(tensor.dtype).encode("ascii"))
digest.update(json.dumps(list(tensor.shape), separators=(",", ":")).encode("ascii"))
digest.update(tensor.numpy().tobytes(order="C"))
return digest.hexdigest()
def _manifest_snapshot(run_root: Path) -> dict[str, Any]:
manifest_path, manifest = _latest_manifest(run_root)
revision_root = manifest_path.parent
chunks = []
for stored in manifest.get("chunks") or []:
chunk_path = revision_root / "chunks" / str(stored["filename"])
entry = dict(stored.get("entry") or {})
chunks.append(
{
"sequence_index": int(stored["sequence_index"]),
"filename": str(stored["filename"]),
"file_sha256": _sha256_file(chunk_path),
"manifest_file_sha256": str(stored["file_sha256"]),
"video_latent_sha256": _tensor_sha256(chunk_path, "video"),
"audio_latent_sha256": _tensor_sha256(chunk_path, "audio"),
"seed": int(entry["seed"]),
"prompt_hash": str(entry["prompt_hash"]),
"plan": entry.get("plan"),
}
)
return {
"path": str(manifest_path),
"mtime_ns": manifest_path.stat().st_mtime_ns,
"status": str(manifest.get("status")),
"review_control_version": manifest.get("review_control_version"),
"review_pause_reason": manifest.get("review_pause_reason"),
"review_unit": manifest.get("review_unit"),
"branch_regenerate_from": manifest.get("branch_regenerate_from"),
"effective_reroll_nonce": manifest.get("effective_reroll_nonce"),
"chunks": chunks,
"report_summary": str(manifest.get("report_summary") or ""),
"manifest": manifest,
}
def _stream_sha256(command: list[str]) -> tuple[str, int]:
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if process.stdout is None:
raise GateFailure("failed to open ffmpeg stdout")
digest = hashlib.sha256()
size = 0
while True:
block = process.stdout.read(1024 * 1024)
if not block:
break
digest.update(block)
size += len(block)
stderr = process.stderr.read().decode("utf-8", errors="replace") if process.stderr else ""
code = process.wait()
if code:
raise GateFailure(f"ffmpeg raw decode failed ({code}): {stderr[-2000:]}")
return digest.hexdigest(), size
def _decoded_hashes(media_path: Path, ffmpeg: str, probe: Mapping[str, Any]) -> dict[str, Any]:
video_sha, video_bytes = _stream_sha256(
[
ffmpeg,
"-v",
"error",
"-i",
str(media_path),
"-map",
"0:v:0",
"-pix_fmt",
"rgb24",
"-f",
"rawvideo",
"-",
]
)
pcm_sha, pcm_bytes = _stream_sha256(
[
ffmpeg,
"-v",
"error",
"-i",
str(media_path),
"-map",
"0:a:0",
"-ac",
"2",
"-ar",
"32000",
"-c:a",
"pcm_f32le",
"-f",
"f32le",
"-",
]
)
return {
"raw_rgb24_sha256": video_sha,
"raw_rgb24_bytes": video_bytes,
"pcm_f32le_32k_stereo_sha256": pcm_sha,
"pcm_f32le_32k_stereo_bytes": pcm_bytes,
"decoded_frames": int(probe["frames"]),
"width": int(probe["width"]),
"height": int(probe["height"]),
"fps": str(probe["fps"]),
"audio_sample_rate": int(probe["audio_sample_rate"]),
"audio_channels": int(probe["audio_channels"]),
"format_duration": float(probe["format_duration"]),
}
def _run_queue(
args: argparse.Namespace,
*,
name: str,
prompt: dict[str, Any],
run_root: Path,
) -> dict[str, Any]:
case_root = args.output_root / name
case_root.mkdir(parents=True, exist_ok=True)
prompt_path = case_root / "prompt.json"
prompt_path.write_text(json.dumps(prompt, indent=2, ensure_ascii=False), encoding="utf-8")
print(f"Phase F queue start: {name}", flush=True)
with ResourceMonitor(args.backend_pid, args.monitor_interval) as monitor:
prompt_id, history, elapsed = submit_and_wait(
server=args.server,
prompt=prompt,
client_id=f"v38-phase-f-{uuid.uuid4().hex}",
timeout_seconds=args.timeout_seconds,
poll_seconds=args.poll_seconds,
)
history_path = case_root / "history.json"
history_path.write_text(json.dumps(history, indent=2, ensure_ascii=False), encoding="utf-8")
media_path = _output_media(history, args.media_root)
ffprobe = shutil.which("ffprobe")
ffmpeg = shutil.which("ffmpeg")
if not ffprobe or not ffmpeg:
raise GateFailure("ffmpeg and ffprobe are required")
media = _probe_media(media_path, ffprobe)
decoded = _decoded_hashes(media_path, ffmpeg, media)
storage = _manifest_snapshot(run_root)
result = {
"name": name,
"prompt_id": prompt_id,
"api_elapsed_seconds": float(elapsed),
"prompt": str(prompt_path),
"history": str(history_path),
"media_path": str(media_path),
"media_file_sha256": _sha256_file(media_path),
"media": media,
"decoded": decoded,
"resource": monitor.as_dict(),
"run_storage": storage,
}
(case_root / "summary.json").write_text(
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
)
print(
f"Phase F queue end: {name}; chunks={len(storage['chunks'])}; "
f"status={storage['status']}; {elapsed:.3f}s",
flush=True,
)
return result
def _parity(full: Mapping[str, Any], review: Mapping[str, Any]) -> dict[str, Any]:
full_chunks = full["run_storage"]["chunks"]
review_chunks = review["run_storage"]["chunks"]
fields = ("seed", "prompt_hash", "video_latent_sha256", "audio_latent_sha256", "plan")
chunk_checks = []
for index in range(max(len(full_chunks), len(review_chunks))):
if index >= len(full_chunks) or index >= len(review_chunks):
chunk_checks.append({"index": index + 1, "present": False})
continue
checks = {field: full_chunks[index][field] == review_chunks[index][field] for field in fields}
chunk_checks.append({"index": index + 1, "present": True, "checks": checks})
decoded_fields = (
"raw_rgb24_sha256",
"pcm_f32le_32k_stereo_sha256",
"decoded_frames",
"width",
"height",
"fps",
"audio_sample_rate",
"audio_channels",
"format_duration",
)
decoded_checks = {
field: full["decoded"][field] == review["decoded"][field]
for field in decoded_fields
}
passed = (
len(full_chunks) == len(review_chunks) == 3
and all(item.get("present") and all(item["checks"].values()) for item in chunk_checks)
and all(decoded_checks.values())
)
return {"pass": passed, "chunks": chunk_checks, "decoded": decoded_checks}
def run(args: argparse.Namespace) -> dict[str, Any]:
args.output_root.mkdir(parents=True, exist_ok=True)
template = json.loads(args.template_prompt.read_text(encoding="utf-8"))
stamp = args.run_tag
full_run_name = f"v38_phase_f_g0_full_{stamp}"
review_run_name = f"v38_phase_f_g1_review_{stamp}"
runs_root = args.run_storage_root
summary: dict[str, Any] = {
"format": "h3-continuum-v38-review-phase-f-g0-g1-v1",
"server": args.server,
"backend_pid": args.backend_pid,
"template_prompt": str(args.template_prompt),
"template_prompt_sha256": _sha256_file(args.template_prompt),
"seed": int(args.seed),
"run_names": {"g0": full_run_name, "g1": review_run_name},
"pass": False,
"stopped": False,
"results": {},
}
summary_path = args.output_root / "gate_summary.json"
try:
g0_prompt = _build_prompt(
template,
run_name=full_run_name,
generation_mode="Full Run",
review_action="Continue / Next",
output_prefix=f"v38_phase_f/{stamp}/g0_full",
seed=args.seed,
)
g0 = _run_queue(
args,
name="g0_full",
prompt=g0_prompt,
run_root=runs_root / full_run_name,
)
summary["results"]["g0"] = g0
if len(g0["run_storage"]["chunks"]) != 3 or g0["run_storage"]["status"] != "complete":
raise GateFailure("G0 Full baseline did not complete exactly three chunks")
review_results = []
previous_mtime = None
for queue_index in range(1, 4):
prompt = _build_prompt(
template,
run_name=review_run_name,
generation_mode="Review Each Chunk",
review_action="Continue / Next",
output_prefix=f"v38_phase_f/{stamp}/g1_review_q{queue_index}",
seed=args.seed,
)
result = _run_queue(
args,
name=f"g1_review_q{queue_index}",
prompt=prompt,
run_root=runs_root / review_run_name,
)
review_results.append(result)
summary["results"]["g1"] = review_results
actual_chunks = len(result["run_storage"]["chunks"])
expected_status = "complete" if queue_index == 3 else "review_ready"
if actual_chunks != queue_index or result["run_storage"]["status"] != expected_status:
unchanged = previous_mtime == result["run_storage"]["mtime_ns"]
raise GateFailure(
"G1 Review queue progression failed: "
f"queue={queue_index}, chunks={actual_chunks}, "
f"status={result['run_storage']['status']}, "
f"manifest_unchanged={unchanged}"
)
previous_mtime = result["run_storage"]["mtime_ns"]
parity = _parity(g0, review_results[-1])
summary["parity"] = parity
if not parity["pass"]:
raise GateFailure("G0 Full and G1 Review exact parity failed")
summary["pass"] = True
return summary
except Exception as exc:
summary["stopped"] = True
summary["failure_type"] = type(exc).__name__
summary["failure"] = str(exc)
return summary
finally:
summary_path.write_text(
json.dumps(summary, indent=2, ensure_ascii=False), encoding="utf-8"
)
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--server", default="http://127.0.0.6:8190")
parser.add_argument("--template-prompt", type=Path, required=True)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument("--media-root", type=Path, required=True)
parser.add_argument("--run-storage-root", type=Path, required=True)
parser.add_argument("--run-tag", required=True)
parser.add_argument("--backend-pid", type=int, default=None)
parser.add_argument("--seed", type=int, default=927966463230733)
parser.add_argument("--timeout-seconds", type=float, default=2400.0)
parser.add_argument("--poll-seconds", type=float, default=2.0)
parser.add_argument("--monitor-interval", type=float, default=1.0)
args = parser.parse_args()
summary = run(args)
print(json.dumps(summary, indent=2, ensure_ascii=False), flush=True)
return 0 if summary["pass"] else 2
if __name__ == "__main__":
raise SystemExit(main())
+197
View File
@@ -0,0 +1,197 @@
"""Resume the stopped Phase F G1 run after the Review cache fix.
The failed Queue 2 prompt is submitted unchanged for both recovery queues. This
keeps the sampler inputs (and the complete API graph) identical while proving
that Review mode no longer reuses the sampler's ComfyUI output cache.
"""
from __future__ import annotations
import argparse
import copy
import json
from pathlib import Path
from typing import Any, Mapping
from v38_review_phase_f_gpu_gate_runner import (
GateFailure,
_find_one,
_manifest_snapshot,
_parity,
_run_queue,
_sha256_file,
)
def _cached_nodes(history: Mapping[str, Any]) -> set[str]:
nodes: set[str] = set()
for prompt_history in history.values():
for message in (prompt_history.get("status") or {}).get("messages") or []:
if len(message) != 2 or message[0] != "execution_cached":
continue
nodes.update(str(node_id) for node_id in (message[1].get("nodes") or []))
return nodes
def _chunk_identity(chunk: Mapping[str, Any]) -> dict[str, Any]:
fields = (
"sequence_index",
"file_sha256",
"video_latent_sha256",
"audio_latent_sha256",
"seed",
"prompt_hash",
"plan",
)
return {field: copy.deepcopy(chunk[field]) for field in fields}
def _preserved_prefix(before: Mapping[str, Any], after: Mapping[str, Any]) -> bool:
before_chunks = before["chunks"]
after_chunks = after["chunks"]
return len(after_chunks) >= len(before_chunks) and all(
_chunk_identity(old) == _chunk_identity(after_chunks[index])
for index, old in enumerate(before_chunks)
)
def _sampler_inputs(prompt: Mapping[str, Any]) -> tuple[str, Mapping[str, Any]]:
node_id, sampler = _find_one(prompt, "H3ContinuumSamplerV38")
return node_id, sampler["inputs"]
def run(args: argparse.Namespace) -> dict[str, Any]:
args.output_root.mkdir(parents=True, exist_ok=True)
prior = json.loads(args.prior_summary.read_text(encoding="utf-8"))
g0 = prior["results"]["g0"]
failed_q2 = prior["results"]["g1"][-1]
prompt_path = Path(failed_q2["prompt"])
prompt = json.loads(prompt_path.read_text(encoding="utf-8"))
sampler_id, sampler_inputs = _sampler_inputs(prompt)
review_run_name = str(prior["run_names"]["g1"])
run_root = args.run_storage_root / review_run_name
before = _manifest_snapshot(run_root)
summary: dict[str, Any] = {
"format": "h3-continuum-v38-review-phase-f-r1-recovery-v1",
"server": args.server,
"backend_pid": args.backend_pid,
"prior_summary": str(args.prior_summary),
"failed_queue_prompt": str(prompt_path),
"failed_queue_prompt_sha256": _sha256_file(prompt_path),
"sampler_node_id": sampler_id,
"sampler_inputs": sampler_inputs,
"run_name": review_run_name,
"initial_run_storage": before,
"pass": False,
"stopped": False,
"results": {},
}
summary_path = args.output_root / "recovery_summary.json"
try:
if len(before["chunks"]) != 1 or before["status"] != "review_ready":
raise GateFailure(
"Recovery must start at Chunk 1 review_ready: "
f"chunks={len(before['chunks'])}, status={before['status']}"
)
q2 = _run_queue(
args,
name="g1_recovery_q2",
prompt=prompt,
run_root=run_root,
)
q2_cached = sorted(_cached_nodes(json.loads(Path(q2["history"]).read_text(encoding="utf-8"))))
q2_storage = q2["run_storage"]
q2_checks = {
"exact_failed_prompt_reused": _sha256_file(Path(q2["prompt"]))
== summary["failed_queue_prompt_sha256"],
"sampler_not_cached": sampler_id not in q2_cached,
"status_review_ready": q2_storage["status"] == "review_ready",
"exactly_two_chunks": len(q2_storage["chunks"]) == 2,
"exactly_one_new_physical_chunk": len(q2_storage["chunks"])
== len(before["chunks"]) + 1,
"chunk_1_exactly_reused": _preserved_prefix(before, q2_storage),
"manifest_updated": q2_storage["mtime_ns"] != before["mtime_ns"],
}
summary["results"]["q2"] = {
**q2,
"cached_nodes": q2_cached,
"checks": q2_checks,
}
if not all(q2_checks.values()):
raise GateFailure(f"Recovery Queue 2 failed: {q2_checks}")
q3 = _run_queue(
args,
name="g1_recovery_q3",
prompt=prompt,
run_root=run_root,
)
q3_cached = sorted(_cached_nodes(json.loads(Path(q3["history"]).read_text(encoding="utf-8"))))
q3_storage = q3["run_storage"]
q3_checks = {
"same_prompt_as_queue_2": _sha256_file(Path(q3["prompt"]))
== _sha256_file(Path(q2["prompt"])),
"sampler_not_cached": sampler_id not in q3_cached,
"status_complete": q3_storage["status"] == "complete",
"exactly_three_chunks": len(q3_storage["chunks"]) == 3,
"exactly_one_new_physical_chunk": len(q3_storage["chunks"])
== len(q2_storage["chunks"]) + 1,
"chunks_1_2_exactly_reused": _preserved_prefix(q2_storage, q3_storage),
"manifest_updated": q3_storage["mtime_ns"] != q2_storage["mtime_ns"],
}
parity = _parity(g0, q3)
summary["results"]["q3"] = {
**q3,
"cached_nodes": q3_cached,
"checks": q3_checks,
}
summary["parity"] = parity
if not all(q3_checks.values()):
raise GateFailure(f"Recovery Queue 3 failed: {q3_checks}")
if not parity["pass"]:
raise GateFailure("Recovered G1 does not exactly match the Phase F G0 baseline")
summary["pass"] = True
return summary
except Exception as exc:
summary["stopped"] = True
summary["failure_type"] = type(exc).__name__
summary["failure"] = str(exc)
return summary
finally:
summary_path.write_text(
json.dumps(summary, indent=2, ensure_ascii=False), encoding="utf-8"
)
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--server", default="http://127.0.0.6:8190")
parser.add_argument("--prior-summary", type=Path, required=True)
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument("--media-root", type=Path, required=True)
parser.add_argument("--run-storage-root", type=Path, required=True)
parser.add_argument("--backend-pid", type=int, default=None)
parser.add_argument("--timeout-seconds", type=float, default=2400.0)
parser.add_argument("--poll-seconds", type=float, default=2.0)
parser.add_argument("--monitor-interval", type=float, default=1.0)
args = parser.parse_args()
summary = run(args)
print(
json.dumps(
{
"pass": summary["pass"],
"stopped": summary["stopped"],
"failure": summary.get("failure"),
"summary": str(args.output_root / "recovery_summary.json"),
},
indent=2,
ensure_ascii=False,
),
flush=True,
)
return 0 if summary["pass"] else 2
if __name__ == "__main__":
raise SystemExit(main())
+29
View File
@@ -0,0 +1,29 @@
param(
[string]$Python = 'python',
[string]$ComfyRoot = '',
[switch]$SkipTests,
[switch]$SkipRuntime
)
$ErrorActionPreference = 'Stop'
$repository = Split-Path -Parent $PSScriptRoot
Push-Location $repository
try {
& $Python -m compileall -q .
if ($LASTEXITCODE -ne 0) { throw 'compileall failed' }
if (-not $SkipTests) {
& $Python -m pytest -q
if ($LASTEXITCODE -ne 0) { throw 'pytest failed' }
}
if (-not $SkipRuntime) {
$arguments = @('tools/verify_runtime.py')
if ($ComfyRoot) { $arguments += @('--comfy-root', $ComfyRoot) }
& $Python @arguments
if ($LASTEXITCODE -ne 0) { throw 'runtime verification failed' }
}
}
finally {
Pop-Location
}
+131
View File
@@ -0,0 +1,131 @@
"""Lightweight installation verifier for a local ComfyUI environment."""
from __future__ import annotations
import argparse
import importlib.util
import sys
from pathlib import Path
EXPECTED_PUBLIC_NODE_DISPLAY_NAMES = {
"H3ContinuumSamplerV38": "H3 Continuum Sampler V3.8",
"H3ContinuumSamplerV39": "H3 Continuum Sampler V3.9",
"H3ContinuumReferenceAudios": "H3 Continuum Reference Audios",
"H3ContinuumReferenceImages": "H3 Continuum Reference Images",
"H3ContinuumReferenceImagesV39": "H3 Continuum Reference Images V3.9",
"H3ContinuumAssembleSeamV35": "H3 Continuum Finalize",
"H3EasyLoadImage": "H3 Continuum Load Image",
"H3EasyLoadAudio": "H3 Continuum Load Audio",
"H3ContinuumLoadVideo": "H3 Continuum Load Video",
"H3ContinuumSecondPassV35": "H3 Continuum Second Pass",
"H3DecodeCacheHelper": "Decode Cache Helper",
"H3ContinuumVideoAdapter": "H3 Continuum Video Adapter",
}
def load_package(root: Path):
name = "h3_continuum_join_verify"
spec = importlib.util.spec_from_file_location(
name,
root / "__init__.py",
submodule_search_locations=[str(root)],
)
if spec is None or spec.loader is None:
raise RuntimeError("cannot create package import specification")
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
return module
def public_registration_issues(module) -> list[str]:
"""Return exact public-surface differences for this local candidate."""
expected = EXPECTED_PUBLIC_NODE_DISPLAY_NAMES
actual_classes = set(getattr(module, "NODE_CLASS_MAPPINGS", {}))
expected_classes = set(expected)
issues: list[str] = []
if actual_classes != expected_classes:
missing = sorted(expected_classes - actual_classes)
extra = sorted(actual_classes - expected_classes)
if missing:
issues.append(f"missing public node IDs: {missing}")
if extra:
issues.append(f"unexpected public node IDs: {extra}")
actual_names = dict(getattr(module, "NODE_DISPLAY_NAME_MAPPINGS", {}))
for node_id, display_name in expected.items():
actual_name = actual_names.get(node_id)
if actual_name != display_name:
issues.append(
f"display name mismatch for {node_id}: "
f"expected {display_name!r}, got {actual_name!r}"
)
unexpected_names = sorted(set(actual_names) - expected_classes)
if unexpected_names:
issues.append(f"unexpected display-name IDs: {unexpected_names}")
return issues
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--comfy-root", type=Path, required=True)
args = parser.parse_args()
comfy_root = args.comfy_root.resolve()
package_root = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(comfy_root))
module = load_package(package_root)
from h3_continuum_join_verify.compatibility import (
check_comfy_h3_runtime,
run_native_layout_self_test,
)
from h3_continuum_join_verify.constants import PROMPT_MODE_FIXED
from h3_continuum_join_verify.v2.prompts import make_prompt_plan
from h3_continuum_join_verify.version import PACKAGE_VERSION
issues = check_comfy_h3_runtime()
if issues:
print("H3 Continuum Join runtime verification FAILED:")
for issue in issues:
print(f" - {issue}")
return 1
try:
layout_report = run_native_layout_self_test()
except Exception as exc:
print(f"Native PackedLayout self-test FAILED: {exc}")
return 1
try:
prompt_plan = make_prompt_plan(
mode=PROMPT_MODE_FIXED,
script="Continuum runtime verification prompt",
chunks=3,
chunk_seconds=5.0,
)
if len(prompt_plan["prompts"]) != 3 or len(set(prompt_plan["hashes"])) != 1:
raise RuntimeError("unexpected Fixed prompt-plan result")
except Exception as exc:
print(f"V2 prompt/session core self-test FAILED: {exc}")
return 1
registration_issues = public_registration_issues(module)
if registration_issues:
print("H3 Continuum local public-node verification FAILED:")
for issue in registration_issues:
print(f" - {issue}")
return 1
actual = set(module.NODE_CLASS_MAPPINGS)
print(f"H3 Continuum {PACKAGE_VERSION} runtime verification passed.")
print(layout_report)
print(f"V{PACKAGE_VERSION} Fixed 3x5s prompt-plan self-test passed.")
print("Registered nodes:")
for key in sorted(actual):
print(f" - {module.NODE_DISPLAY_NAME_MAPPINGS[key]}")
print("GPU checkpoint generation was not run by this lightweight verifier.")
return 0
if __name__ == "__main__":
raise SystemExit(main())