Merge commit 'bf8f76505fdd25ea99770f262eb9371509bf226d' as 'vendor/dumas_h3_continuum'
This commit is contained in:
+5
@@ -0,0 +1,5 @@
|
||||
"""Integrated V2 runtime and nodes."""
|
||||
|
||||
from .nodes import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS
|
||||
|
||||
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"]
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
"""Read-only diagnostics for the latent video context passed between chunks."""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
import math
|
||||
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class VideoContextStats:
|
||||
shape: tuple[int, ...]
|
||||
mean: float
|
||||
std: float
|
||||
low_frequency_rms: float
|
||||
high_frequency_rms: float
|
||||
temporal_delta_rms: float
|
||||
outlier_ratio: float
|
||||
|
||||
|
||||
def _rms(value: torch.Tensor) -> float:
|
||||
return float(torch.sqrt(torch.mean(value.square())).item()) if value.numel() else 0.0
|
||||
|
||||
|
||||
def measure_video_context(video: torch.Tensor) -> VideoContextStats:
|
||||
"""Measure a context tensor without modifying or retaining it."""
|
||||
if not torch.is_tensor(video) or video.ndim != 5:
|
||||
raise ValueError("video context must be a 5D tensor")
|
||||
value = video.detach().to(device="cpu", dtype=torch.float32)
|
||||
if not bool(torch.isfinite(value).all().item()):
|
||||
raise ValueError("video context contains NaN or Inf")
|
||||
|
||||
mean = float(value.mean().item())
|
||||
std = float(value.std(unbiased=False).item())
|
||||
low = F.avg_pool3d(
|
||||
value,
|
||||
kernel_size=(1, 3, 3),
|
||||
stride=1,
|
||||
padding=(0, 1, 1),
|
||||
count_include_pad=False,
|
||||
)
|
||||
high = value - low
|
||||
temporal = value[:, :, 1:] - value[:, :, :-1] if value.shape[2] > 1 else value[:, :, :0]
|
||||
if std > 0.0:
|
||||
outlier_ratio = float(((value - mean).abs() > 6.0 * std).float().mean().item())
|
||||
else:
|
||||
outlier_ratio = 0.0
|
||||
return VideoContextStats(
|
||||
shape=tuple(int(part) for part in video.shape),
|
||||
mean=mean,
|
||||
std=std,
|
||||
low_frequency_rms=_rms(low),
|
||||
high_frequency_rms=_rms(high),
|
||||
temporal_delta_rms=_rms(temporal),
|
||||
outlier_ratio=outlier_ratio,
|
||||
)
|
||||
|
||||
|
||||
def _ratio(value: float, reference: float) -> float:
|
||||
if reference == 0.0:
|
||||
return 1.0 if value == 0.0 else math.inf
|
||||
return value / reference
|
||||
|
||||
|
||||
def _format_ratio(value: float) -> str:
|
||||
return f"{value:.4f}" if math.isfinite(value) else "inf"
|
||||
|
||||
|
||||
class ContextDiagnosticsTracker:
|
||||
"""Track first-context and previous-context ratios for one sequence run."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._baseline: VideoContextStats | None = None
|
||||
self._previous: VideoContextStats | None = None
|
||||
|
||||
def observe(
|
||||
self,
|
||||
video: torch.Tensor,
|
||||
*,
|
||||
source_chunk: int,
|
||||
context_frames: int,
|
||||
reused: bool,
|
||||
) -> str:
|
||||
try:
|
||||
stats = measure_video_context(video)
|
||||
except Exception as exc:
|
||||
return f"context diagnostics source chunk {source_chunk}: unavailable ({exc})"
|
||||
|
||||
if self._baseline is None:
|
||||
self._baseline = stats
|
||||
previous = self._previous or stats
|
||||
baseline = self._baseline
|
||||
self._previous = stats
|
||||
return (
|
||||
f"context diagnostics source chunk {source_chunk}: frames={int(context_frames)}, "
|
||||
f"shape={stats.shape}, reused={'yes' if reused else 'no'}, "
|
||||
f"mean={stats.mean:.6f}, std={stats.std:.6f}, "
|
||||
f"hf={stats.high_frequency_rms:.6f}, motion={stats.temporal_delta_rms:.6f}, "
|
||||
f"outliers={stats.outlier_ratio:.6f}, "
|
||||
f"std_prev={_format_ratio(_ratio(stats.std, previous.std))}, "
|
||||
f"std_base={_format_ratio(_ratio(stats.std, baseline.std))}, "
|
||||
f"hf_prev={_format_ratio(_ratio(stats.high_frequency_rms, previous.high_frequency_rms))}, "
|
||||
f"hf_base={_format_ratio(_ratio(stats.high_frequency_rms, baseline.high_frequency_rms))}, "
|
||||
f"motion_prev={_format_ratio(_ratio(stats.temporal_delta_rms, previous.temporal_delta_rms))}, "
|
||||
f"motion_base={_format_ratio(_ratio(stats.temporal_delta_rms, baseline.temporal_delta_rms))}, "
|
||||
"action=observe"
|
||||
)
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
"""Deferred VAE decoding, overlap trimming, assembly, and exact-duration correction."""
|
||||
from __future__ import annotations
|
||||
from typing import Any
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
from ..constants import DIAGNOSTICS_FULL, DIAGNOSTICS_OFF, FPS
|
||||
from ..media import validate_audio
|
||||
from ..state import validate_plan
|
||||
from .diagnostics import format_join_metrics, format_seam_decision, overlap_diagnostics
|
||||
from .seam_guard import correct_audio_seam, correct_seam
|
||||
from .seam_types import SeamDecision
|
||||
from .session import entry_to_latent, validate_chunk_entry
|
||||
|
||||
def decode_video(video_vae:Any,latent:dict[str,Any])->torch.Tensor:
|
||||
samples=latent["samples"]; video=list(samples.unbind())[0] if hasattr(samples,"unbind") else samples[0]; images=video_vae.decode(video)
|
||||
if images.ndim==5: images=images.reshape(-1,images.shape[-3],images.shape[-2],images.shape[-1])
|
||||
if images.ndim!=4: raise ValueError(f"video VAE returned unexpected shape {tuple(images.shape)}")
|
||||
return images.detach().to("cpu").contiguous()
|
||||
def decode_audio(audio_vae:Any,latent:dict[str,Any])->dict[str,Any]:
|
||||
samples=latent["samples"]; audio_latent=list(samples.unbind())[-1] if hasattr(samples,"unbind") else samples[-1]; audio=audio_vae.decode(audio_latent).movedim(-1,1); std=torch.std(audio,dim=[1,2],keepdim=True)*5.0; std[std<1.0]=1.0; audio=(audio/std).detach().to("cpu").contiguous(); sample_rate=getattr(audio_vae,"audio_sample_rate_output",getattr(audio_vae,"audio_sample_rate",32000)); return {"waveform":audio,"sample_rate":int(sample_rate)}
|
||||
def _trim_images(images,plan):
|
||||
plan=validate_plan(plan); total_frames=int(plan["total_frames"]); trim_frames=int(plan["trim_frames"])
|
||||
if images.shape[0]<total_frames: raise ValueError(f"decoded batch has {images.shape[0]} frames; expected at least {total_frames}")
|
||||
return images[:total_frames][trim_frames:].contiguous()
|
||||
def _slice_audio_for_timeline(audio,*,trim_frames,frame_start,frame_stop):
|
||||
waveform,sample_rate=validate_audio(audio); trim_samples=int(round(float(trim_frames)/FPS*sample_rate)); target_start=int(round(float(frame_start)/FPS*sample_rate)); target_stop=int(round(float(frame_stop)/FPS*sample_rate)); wanted=target_stop-target_start
|
||||
if wanted<0: raise ValueError("audio timeline interval is negative")
|
||||
result=waveform[...,trim_samples:trim_samples+wanted]
|
||||
if result.shape[-1]<wanted:
|
||||
missing=wanted-int(result.shape[-1]); result=torch.cat((result,result[...,-1:].expand(*result.shape[:-1],missing).clone()),dim=-1) if result.shape[-1]>0 else F.pad(result,(0,missing))
|
||||
return result.contiguous()
|
||||
def decode_sequence(*,entries,video_vae,audio_vae,diagnostics_full):
|
||||
if not entries: raise ValueError("cannot decode an empty Continuum sequence")
|
||||
checked=[validate_chunk_entry(e) for e in entries]; plans=[validate_plan(e["plan"]) for e in checked]; total_retained_frames=sum(int(p["net_frames"]) for p in plans)
|
||||
if total_retained_frames<1: raise ValueError("Continuum sequence contains no retained frames")
|
||||
image_buffer=None; audio_buffer=None; audio_rate=None; frame_cursor=0; previous_segment_images=None; previous_segment_audio=None; reports=[]
|
||||
for index,(entry,plan) in enumerate(zip(checked,plans),start=1):
|
||||
latent=entry_to_latent(entry); raw_images=decode_video(video_vae,latent); raw_audio=decode_audio(audio_vae,latent)
|
||||
if diagnostics_full and previous_segment_images is not None: reports.append(format_join_metrics(index,overlap_diagnostics(previous_images=previous_segment_images,previous_audio=previous_segment_audio,next_raw_images=raw_images,next_raw_audio=raw_audio,context_frames=int(plan["trim_frames"]))))
|
||||
images=_trim_images(raw_images,plan); segment_frames=int(images.shape[0])
|
||||
if segment_frames!=int(plan["net_frames"]): raise ValueError(f"chunk {index} retained {segment_frames} frames; plan expects {plan['net_frames']}")
|
||||
frame_stop=frame_cursor+segment_frames; waveform,sample_rate=validate_audio(raw_audio)
|
||||
if audio_rate is None: audio_rate=sample_rate
|
||||
elif sample_rate!=audio_rate: raise ValueError(f"audio sample rate changed between chunks: {audio_rate} -> {sample_rate}")
|
||||
segment_waveform=_slice_audio_for_timeline(raw_audio,trim_frames=int(plan["trim_frames"]),frame_start=frame_cursor,frame_stop=frame_stop)
|
||||
if image_buffer is None: image_buffer=torch.empty((total_retained_frames,*images.shape[1:]),dtype=images.dtype,device="cpu")
|
||||
elif tuple(images.shape[1:])!=tuple(image_buffer.shape[1:]): raise ValueError(f"decoded image geometry changed at chunk {index}: {tuple(images.shape[1:])} vs {tuple(image_buffer.shape[1:])}")
|
||||
image_buffer[frame_cursor:frame_stop].copy_(images)
|
||||
if audio_buffer is None: audio_buffer=torch.empty((*waveform.shape[:-1],int(round(total_retained_frames/FPS*sample_rate))),dtype=waveform.dtype,device="cpu")
|
||||
elif tuple(segment_waveform.shape[:-1])!=tuple(audio_buffer.shape[:-1]): raise ValueError("decoded audio batch/channel structure changed between chunks")
|
||||
sample_start=int(round(frame_cursor/FPS*sample_rate)); sample_stop=int(round(frame_stop/FPS*sample_rate)); audio_buffer[...,sample_start:sample_stop].copy_(segment_waveform)
|
||||
next_context=int(plans[index]["trim_frames"]) if index<len(plans) else 0
|
||||
if next_context>0: previous_segment_images=images[-next_context:].contiguous(); previous_segment_audio={"waveform":segment_waveform,"sample_rate":sample_rate}
|
||||
else: previous_segment_images=None; previous_segment_audio=None
|
||||
reports.append(f"decoded chunk {index}: {segment_frames} retained frames, cumulative {frame_stop}/{total_retained_frames}"); frame_cursor=frame_stop
|
||||
del latent,raw_images,raw_audio,images,segment_waveform
|
||||
if image_buffer is None or audio_buffer is None or audio_rate is None: raise RuntimeError("Continuum decoder failed to allocate output buffers")
|
||||
if frame_cursor!=total_retained_frames: raise RuntimeError(f"Continuum decode cursor mismatch: {frame_cursor} != {total_retained_frames}")
|
||||
reports.append(f"assembled {len(entries)} chunks into {total_retained_frames} frames with cumulative sample-boundary alignment")
|
||||
return image_buffer.contiguous(),{"waveform":audio_buffer.contiguous(),"sample_rate":audio_rate},reports
|
||||
def decode_sequence_with_seam(*,entries,video_vae,audio_vae,diagnostics_mode,automatic=False):
|
||||
if not entries: raise ValueError("cannot decode an empty Continuum sequence")
|
||||
checked=[validate_chunk_entry(e) for e in entries]; plans=[validate_plan(e["plan"]) for e in checked]; total_retained_frames=sum(int(p["net_frames"]) for p in plans)
|
||||
if total_retained_frames<1: raise ValueError("Continuum sequence contains no retained frames")
|
||||
image_buffer=None; audio_buffer=None; audio_rate=None; frame_cursor=0; reports=[]
|
||||
for index,(entry,plan) in enumerate(zip(checked,plans),start=1):
|
||||
latent=entry_to_latent(entry); raw_images=decode_video(video_vae,latent); raw_audio=decode_audio(audio_vae,latent); waveform,sample_rate=validate_audio(raw_audio)
|
||||
if audio_rate is None: audio_rate=sample_rate
|
||||
elif sample_rate!=audio_rate: raise ValueError(f"audio sample rate changed between chunks: {audio_rate} -> {sample_rate}")
|
||||
legacy_frames=int(plan["net_frames"]); legacy_frame_stop=frame_cursor+legacy_frames; frame_start=frame_cursor; trim_frames=int(plan["trim_frames"]); decision=None; video_patch=None; audio_patch=None
|
||||
if index==1: images=_trim_images(raw_images,plan)
|
||||
else:
|
||||
try:
|
||||
if image_buffer is None or audio_buffer is None: raise RuntimeError("seam buffers are unavailable")
|
||||
images,decision,video_patch=correct_seam(image_buffer[:frame_cursor],raw_images[:int(plan["total_frames"])],context_frames=trim_frames,automatic=automatic); frame_start=frame_cursor-int(decision.cut_rewind_frames); trim_frames-=int(decision.cut_rewind_frames)
|
||||
if frame_start<0 or trim_frames<0: raise ValueError("adaptive cut produced a negative boundary")
|
||||
if frame_start+int(images.shape[0])!=legacy_frame_stop: raise ValueError("adaptive cut changed the cumulative frame boundary")
|
||||
sample_start=int(round(frame_start/FPS*sample_rate)); cut_sample=int(round(trim_frames/FPS*sample_rate)); audio_patch,audio_metrics,crossfade_samples,level_gain,dc_bias=correct_audio_seam(audio_buffer[...,:sample_start],waveform,sample_rate=sample_rate,cut_sample=cut_sample); decision.audio_metrics=audio_metrics; decision.audio_crossfade_samples=crossfade_samples; decision.audio_level_gain=level_gain; decision.audio_dc_bias=dc_bias
|
||||
except Exception as exc:
|
||||
images=_trim_images(raw_images,plan); frame_start=frame_cursor; trim_frames=int(plan["trim_frames"]); video_patch=None; audio_patch=None; decision=SeamDecision(fallback_reason=f"{type(exc).__name__}: {exc}")
|
||||
segment_frames=int(images.shape[0]); frame_stop=frame_start+segment_frames
|
||||
if frame_stop!=legacy_frame_stop: raise ValueError(f"chunk {index} corrected boundary ends at {frame_stop}; expected {legacy_frame_stop}")
|
||||
segment_waveform=_slice_audio_for_timeline(raw_audio,trim_frames=trim_frames,frame_start=frame_start,frame_stop=frame_stop)
|
||||
if image_buffer is None: image_buffer=torch.empty((total_retained_frames,*images.shape[1:]),dtype=images.dtype,device="cpu")
|
||||
elif tuple(images.shape[1:])!=tuple(image_buffer.shape[1:]): raise ValueError(f"decoded image geometry changed at chunk {index}: {tuple(images.shape[1:])} vs {tuple(image_buffer.shape[1:])}")
|
||||
if video_patch is not None and video_patch.shape[0]>0:
|
||||
patch_start=frame_start-int(video_patch.shape[0])
|
||||
if patch_start<0: raise ValueError("video seam patch starts before the output")
|
||||
image_buffer[patch_start:frame_start].copy_(video_patch)
|
||||
image_buffer[frame_start:frame_stop].copy_(images)
|
||||
if audio_buffer is None: audio_buffer=torch.empty((*waveform.shape[:-1],int(round(total_retained_frames/FPS*sample_rate))),dtype=waveform.dtype,device="cpu")
|
||||
elif tuple(segment_waveform.shape[:-1])!=tuple(audio_buffer.shape[:-1]): raise ValueError("decoded audio batch/channel structure changed between chunks")
|
||||
sample_start=int(round(frame_start/FPS*sample_rate)); sample_stop=int(round(frame_stop/FPS*sample_rate))
|
||||
if audio_patch is not None and audio_patch.shape[-1]>0:
|
||||
patch_start=sample_start-int(audio_patch.shape[-1])
|
||||
if patch_start<0: raise ValueError("audio seam patch starts before the output")
|
||||
audio_buffer[...,patch_start:sample_start].copy_(audio_patch)
|
||||
audio_buffer[...,sample_start:sample_stop].copy_(segment_waveform)
|
||||
if decision is not None and diagnostics_mode!=DIAGNOSTICS_OFF: reports.append(format_seam_decision(index,decision,full=diagnostics_mode==DIAGNOSTICS_FULL))
|
||||
reports.append(f"decoded chunk {index}: {segment_frames} retained/corrected frames, cumulative {frame_stop}/{total_retained_frames}"); frame_cursor=frame_stop
|
||||
del latent,raw_images,raw_audio,images,segment_waveform
|
||||
if image_buffer is None or audio_buffer is None or audio_rate is None: raise RuntimeError("Continuum seam decoder failed to allocate output buffers")
|
||||
if frame_cursor!=total_retained_frames: raise RuntimeError(f"Continuum seam decode cursor mismatch: {frame_cursor} != {total_retained_frames}")
|
||||
reports.append(f"assembled {len(entries)} chunks into {total_retained_frames} frames with V2.1 {'Auto' if automatic else 'Basic'} seam correction and cumulative sample-boundary alignment")
|
||||
return image_buffer.contiguous(),{"waveform":audio_buffer.contiguous(),"sample_rate":audio_rate},reports
|
||||
def enforce_total_frames(images,audio,*,target_frames,preserve_final_frame=False):
|
||||
target_frames=int(target_frames)
|
||||
if target_frames<1: raise ValueError("target_frames must be positive")
|
||||
current=int(images.shape[0]); adjustment=target_frames-current; anchor_preserved=bool(preserve_final_frame and adjustment<0 and target_frames>=2)
|
||||
if adjustment<0: images=torch.cat((images[:target_frames-1],images[-1:]),dim=0).contiguous() if anchor_preserved else images[:target_frames].contiguous()
|
||||
elif adjustment>0:
|
||||
if current==0: raise ValueError("cannot pad an empty image sequence")
|
||||
images=torch.cat((images,images[-1:].expand(adjustment,*images.shape[1:]).clone()),dim=0).contiguous()
|
||||
waveform=audio["waveform"]; sample_rate=int(audio["sample_rate"]); original_samples=int(waveform.shape[-1]); target_samples=int(round(target_frames/FPS*sample_rate)); delta_samples=target_samples-original_samples
|
||||
if delta_samples<0:
|
||||
if anchor_preserved and target_samples>1:
|
||||
tail_samples=min(max(1,int(round(sample_rate/FPS))),target_samples,original_samples); head_samples=target_samples-tail_samples; head=waveform[...,:head_samples]; tail=waveform[...,-tail_samples:].clone(); fade=min(int(round(sample_rate*0.005)),head_samples,tail_samples)
|
||||
if fade>0:
|
||||
start=head[...,-1:].to(dtype=tail.dtype); alpha=torch.linspace(0.0,1.0,fade,dtype=tail.dtype,device=tail.device).reshape(*([1]*(tail.ndim-1)),fade); tail[...,:fade]=start*(1.0-alpha)+tail[...,:fade]*alpha
|
||||
waveform=torch.cat((head,tail),dim=-1).contiguous()
|
||||
else: waveform=waveform[...,:target_samples].contiguous()
|
||||
elif delta_samples>0:
|
||||
waveform=F.pad(waveform,(0,delta_samples)) if waveform.shape[-1]==0 else torch.cat((waveform,waveform[...,-1:].expand(*waveform.shape[:-1],delta_samples).clone()),dim=-1).contiguous()
|
||||
audio={**audio,"waveform":waveform,"sample_rate":sample_rate}; mode="final anchor preserved; pre-tail trim" if anchor_preserved else "tail trim/pad"; return images,audio,f"Exact-duration adjustment: frames {current}->{target_frames} ({adjustment:+d}), audio samples correction {delta_samples:+d}; {mode}."
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
"""Lightweight join diagnostics without external vision/audio models."""
|
||||
from __future__ import annotations
|
||||
import math
|
||||
from typing import Any
|
||||
import torch
|
||||
from ..constants import FPS
|
||||
from .seam_types import SeamDecision
|
||||
def _audio_waveform(audio:dict[str,Any]):
|
||||
waveform=audio.get("waveform"); sample_rate=int(audio.get("sample_rate",0))
|
||||
if not torch.is_tensor(waveform) or waveform.ndim!=3: raise ValueError("audio waveform must be [B,C,S]")
|
||||
if sample_rate<=0: raise ValueError("audio sample_rate is invalid")
|
||||
return waveform.to(dtype=torch.float32),sample_rate
|
||||
def _normalized_correlation(a,b):
|
||||
length=min(int(a.numel()),int(b.numel()))
|
||||
if length<8:return 0.0
|
||||
a=a.reshape(-1)[:length].float(); b=b.reshape(-1)[:length].float(); a=a-a.mean(); b=b-b.mean(); denom=torch.sqrt(torch.sum(a.square())*torch.sum(b.square())).clamp_min(1e-12); value=float((torch.sum(a*b)/denom).item()); return max(-1.0,min(1.0,value)) if math.isfinite(value) else 0.0
|
||||
def overlap_diagnostics(*,previous_images,previous_audio,next_raw_images,next_raw_audio,context_frames):
|
||||
frame_count=min(int(context_frames),int(previous_images.shape[0]),int(next_raw_images.shape[0]))
|
||||
if frame_count<=0:return {"video_overlap_mae":0.0,"video_overlap_psnr":99.0,"audio_overlap_correlation":0.0,"audio_boundary_jump":0.0}
|
||||
prev=previous_images[-frame_count:].detach().to(dtype=torch.float32); nxt=next_raw_images[:frame_count].detach().to(dtype=torch.float32); mse=float(torch.mean((prev-nxt).square()).item()); mae=float(torch.mean(torch.abs(prev-nxt)).item()); psnr=99.0 if mse<=1e-12 else max(0.0,-10.0*math.log10(mse))
|
||||
prev_wave,prev_sr=_audio_waveform(previous_audio); next_wave,next_sr=_audio_waveform(next_raw_audio)
|
||||
if prev_sr!=next_sr: raise ValueError("diagnostic audio sample rates do not match")
|
||||
samples=min(int(round(frame_count/FPS*prev_sr)),int(prev_wave.shape[-1]),int(next_wave.shape[-1])); correlation=_normalized_correlation(prev_wave[...,-samples:],next_wave[...,:samples]) if samples>0 else 0.0; jump=float(torch.mean(torch.abs(prev_wave[...,-1]-next_wave[...,0])).item()) if prev_wave.shape[-1] and next_wave.shape[-1] else 0.0
|
||||
return {"video_overlap_mae":mae,"video_overlap_psnr":psnr,"audio_overlap_correlation":correlation,"audio_boundary_jump":jump}
|
||||
def av_duration_error_samples(images,audio):
|
||||
waveform,sample_rate=_audio_waveform(audio); return int(waveform.shape[-1])-int(round(float(images.shape[0])/FPS*sample_rate))
|
||||
def format_join_metrics(index,metrics): return f"join {index-1}->{index}: video MAE={metrics['video_overlap_mae']:.6f}, PSNR={metrics['video_overlap_psnr']:.2f}dB, audio corr={metrics['audio_overlap_correlation']:.4f}, boundary jump={metrics['audio_boundary_jump']:.6f}"
|
||||
def format_seam_decision(index,decision:SeamDecision,*,full:bool):
|
||||
fallback=decision.fallback_reason or "none"
|
||||
if not full:return f"seam {index-1}->{index}: cut=-{decision.cut_rewind_frames}f, native score {decision.legacy_score:.4f}->{decision.corrected_score:.4f}, video blend={decision.video_blend_frames}f, audio offset={decision.audio_metrics.offset_samples:+d} samples, fallback={fallback}"
|
||||
return (f"Seam {index-1}->{index}\n Native score : {decision.legacy_score:.6f}\n Selected cut : -{decision.cut_rewind_frames} frames\n Cut score : {decision.cut_score:.6f}\n Candidate score : {decision.candidate_score:.6f}\n Final score : {decision.corrected_score:.6f}\n Improvement : {decision.improvement*100.0:.2f}%\n Native 5f peak : {decision.native_window_peak:.6f}\n Candidate 5f peak : {decision.candidate_window_peak:.6f}\n Luminance gain : {decision.luma_gain:.4f}\n Chroma bias : {decision.chroma_bias}\n Video blend : {decision.video_blend_frames} frames\n Audio correlation : {decision.audio_metrics.correlation_before:.4f}->{decision.audio_metrics.correlation_after:.4f}\n Audio jump : {decision.audio_metrics.boundary_jump_before:.6f}->{decision.audio_metrics.boundary_jump_after:.6f}\n Audio offset : {decision.audio_metrics.offset_samples:+d} samples\n Audio crossfade : {decision.audio_crossfade_samples} samples\n Fallback : {fallback}")
|
||||
+362
@@ -0,0 +1,362 @@
|
||||
"""Native MiniMax H3 conditioning and latent construction for V2.
|
||||
|
||||
All expensive image/VAE work is performed before the sampling phase. Prompt
|
||||
conditioning is cached per unique prompt and image presentation.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import OrderedDict
|
||||
import hashlib
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Callable
|
||||
|
||||
import torch
|
||||
|
||||
from ..continuation import clone_conditioning
|
||||
from ..temporal import audio_latent_t, video_latent_t
|
||||
|
||||
|
||||
_PROMPT_CACHE_ATTR = "_h3_continuum_prompt_conditioning_cache_v1"
|
||||
_PROMPT_CACHE_VERSION = 1
|
||||
_PROMPT_CACHE_MAX_ENTRIES = 16
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class IdentityAssets:
|
||||
first_image: torch.Tensor | None
|
||||
first_latent: torch.Tensor | None
|
||||
last_image: torch.Tensor | None
|
||||
last_latent: torch.Tensor | None
|
||||
identity_hash: str
|
||||
|
||||
@property
|
||||
def first_frame_hash(self) -> str:
|
||||
"""Version-neutral alias retained alongside the legacy identity hash."""
|
||||
return self.identity_hash
|
||||
|
||||
@property
|
||||
def last_frame_hash(self) -> str:
|
||||
"""Fingerprint the resized final keyframe independently from identity."""
|
||||
return _tensor_fingerprint(self.last_image)
|
||||
|
||||
|
||||
def _tensor_fingerprint(tensor: torch.Tensor | None) -> str:
|
||||
if tensor is None:
|
||||
return "none"
|
||||
sample = tensor.detach().to("cpu").contiguous()
|
||||
digest = hashlib.sha256()
|
||||
digest.update(str(tuple(int(value) for value in sample.shape)).encode("ascii"))
|
||||
digest.update(str(sample.dtype).encode("ascii"))
|
||||
digest.update(sample.view(torch.uint8).numpy().tobytes(order="C"))
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def _cache_event(callback: Callable[[str], None] | None, event: str) -> None:
|
||||
if callback is None:
|
||||
return
|
||||
try:
|
||||
callback(str(event))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _clone_metadata_containers(value: Any) -> Any:
|
||||
"""Clone mutable metadata containers while sharing immutable tensor payloads."""
|
||||
|
||||
if isinstance(value, dict):
|
||||
return {
|
||||
key: _clone_metadata_containers(item)
|
||||
for key, item in value.items()
|
||||
}
|
||||
if isinstance(value, list):
|
||||
return [_clone_metadata_containers(item) for item in value]
|
||||
if isinstance(value, tuple):
|
||||
return tuple(_clone_metadata_containers(item) for item in value)
|
||||
return value
|
||||
|
||||
|
||||
def _clone_cached_conditioning(conditioning: list) -> list:
|
||||
output = []
|
||||
for index, item in enumerate(conditioning):
|
||||
if not isinstance(item, (tuple, list)) or len(item) != 2:
|
||||
raise ValueError(f"conditioning entry {index} is not [tensor, metadata]")
|
||||
if not isinstance(item[1], dict):
|
||||
raise ValueError(f"conditioning entry {index} metadata is not a dictionary")
|
||||
output.append([item[0], _clone_metadata_containers(item[1])])
|
||||
if not output:
|
||||
raise ValueError("conditioning is empty")
|
||||
return output
|
||||
|
||||
|
||||
def _contains_cuda_tensor(value: Any) -> bool:
|
||||
if torch.is_tensor(value):
|
||||
return value.device.type == "cuda"
|
||||
if isinstance(value, dict):
|
||||
return any(_contains_cuda_tensor(item) for item in value.values())
|
||||
if isinstance(value, (list, tuple)):
|
||||
return any(_contains_cuda_tensor(item) for item in value)
|
||||
return False
|
||||
|
||||
|
||||
def _prompt_cache_clip_state(clip: Any) -> tuple[str, Any] | None:
|
||||
"""Return a conservative state key, or None for CLIP modes we do not cache."""
|
||||
|
||||
patcher = getattr(clip, "patcher", None)
|
||||
patches_uuid = getattr(patcher, "patches_uuid", None)
|
||||
if patcher is None or patches_uuid is None:
|
||||
return None
|
||||
if bool(getattr(clip, "use_clip_schedule", False)):
|
||||
return None
|
||||
if getattr(clip, "apply_hooks_to_conds", None) is not None:
|
||||
return None
|
||||
if getattr(patcher, "forced_hooks", None) is not None:
|
||||
return None
|
||||
if getattr(patcher, "current_hooks", None) is not None:
|
||||
return None
|
||||
if bool(getattr(patcher, "hook_patches", {}) or {}):
|
||||
return None
|
||||
if bool(getattr(clip, "tokenizer_options", {}) or {}):
|
||||
return None
|
||||
return str(patches_uuid), getattr(clip, "layer_idx", None)
|
||||
|
||||
|
||||
def encode_prompt_conditioning_cached(
|
||||
clip: Any,
|
||||
prompt: str,
|
||||
*,
|
||||
first_image: torch.Tensor | None,
|
||||
last_image: torch.Tensor | None,
|
||||
first_image_fingerprint: str,
|
||||
last_image_fingerprint: str,
|
||||
cache_enabled: bool,
|
||||
cache_event: Callable[[str], None] | None = None,
|
||||
) -> list:
|
||||
"""V3.5-only bounded prompt cache; every unsafe state falls back to encode."""
|
||||
|
||||
if not bool(cache_enabled):
|
||||
return encode_prompt_conditioning(
|
||||
clip,
|
||||
prompt,
|
||||
first_image=first_image,
|
||||
last_image=last_image,
|
||||
)
|
||||
state = _prompt_cache_clip_state(clip)
|
||||
if state is None:
|
||||
result = encode_prompt_conditioning(
|
||||
clip,
|
||||
prompt,
|
||||
first_image=first_image,
|
||||
last_image=last_image,
|
||||
)
|
||||
_cache_event(cache_event, "bypass_special_clip")
|
||||
return result
|
||||
key = (
|
||||
_PROMPT_CACHE_VERSION,
|
||||
str(prompt),
|
||||
str(first_image_fingerprint),
|
||||
str(last_image_fingerprint),
|
||||
state,
|
||||
)
|
||||
try:
|
||||
cache = getattr(clip, _PROMPT_CACHE_ATTR, None)
|
||||
if cache is None:
|
||||
cache = OrderedDict()
|
||||
setattr(clip, _PROMPT_CACHE_ATTR, cache)
|
||||
if not isinstance(cache, OrderedDict):
|
||||
raise TypeError("unexpected prompt cache container")
|
||||
if key in cache:
|
||||
cached = cache.pop(key)
|
||||
cache[key] = cached
|
||||
result = _clone_cached_conditioning(cached)
|
||||
_cache_event(cache_event, "hit")
|
||||
return result
|
||||
except Exception:
|
||||
result = encode_prompt_conditioning(
|
||||
clip,
|
||||
prompt,
|
||||
first_image=first_image,
|
||||
last_image=last_image,
|
||||
)
|
||||
_cache_event(cache_event, "bypass_cache_error")
|
||||
return result
|
||||
|
||||
result = encode_prompt_conditioning(
|
||||
clip,
|
||||
prompt,
|
||||
first_image=first_image,
|
||||
last_image=last_image,
|
||||
)
|
||||
if _contains_cuda_tensor(result):
|
||||
_cache_event(cache_event, "bypass_cuda_output")
|
||||
return result
|
||||
try:
|
||||
cache[key] = _clone_cached_conditioning(result)
|
||||
cache.move_to_end(key)
|
||||
while len(cache) > _PROMPT_CACHE_MAX_ENTRIES:
|
||||
cache.popitem(last=False)
|
||||
_cache_event(cache_event, "miss")
|
||||
except Exception:
|
||||
_cache_event(cache_event, "bypass_cache_error")
|
||||
return result
|
||||
|
||||
|
||||
def resize_h3_image(image: torch.Tensor, width: int, height: int, crop: str) -> torch.Tensor:
|
||||
try:
|
||||
import comfy.utils
|
||||
except Exception as exc: # pragma: no cover - ComfyUI runtime only
|
||||
raise RuntimeError(f"ComfyUI resize helper unavailable: {exc}") from exc
|
||||
if not torch.is_tensor(image) or image.ndim != 4 or image.shape[-1] < 3:
|
||||
raise ValueError("H3 image input must be [B,H,W,C] with at least three channels")
|
||||
samples = image[:1, ..., :3].movedim(-1, 1)
|
||||
resized = comfy.utils.common_upscale(samples, int(width), int(height), "lanczos", crop)
|
||||
return resized.movedim(1, -1).contiguous()
|
||||
|
||||
|
||||
def prepare_identity_assets(
|
||||
video_vae: Any,
|
||||
*,
|
||||
width: int,
|
||||
height: int,
|
||||
first_frame: torch.Tensor | None,
|
||||
last_frame: torch.Tensor | None,
|
||||
encode_latents: bool = True,
|
||||
) -> IdentityAssets:
|
||||
first_image = first_latent = last_image = last_latent = None
|
||||
if first_frame is not None:
|
||||
first_image = resize_h3_image(first_frame, width, height, "disabled")
|
||||
if encode_latents:
|
||||
first_latent = video_vae.encode(first_image)
|
||||
if last_frame is not None:
|
||||
last_image = resize_h3_image(last_frame, width, height, "center")
|
||||
if encode_latents:
|
||||
last_latent = video_vae.encode(last_image)
|
||||
return IdentityAssets(
|
||||
first_image=first_image,
|
||||
first_latent=first_latent,
|
||||
last_image=last_image,
|
||||
last_latent=last_latent,
|
||||
identity_hash=_tensor_fingerprint(first_image),
|
||||
)
|
||||
|
||||
|
||||
def encode_prompt_conditioning(
|
||||
clip: Any,
|
||||
prompt: str,
|
||||
*,
|
||||
first_image: torch.Tensor | None,
|
||||
last_image: torch.Tensor | None,
|
||||
reference_audio_assets=None,
|
||||
timeline_video_assets=None,
|
||||
) -> list:
|
||||
images = []
|
||||
if first_image is not None:
|
||||
images.append(first_image)
|
||||
if last_image is not None:
|
||||
images.append(last_image)
|
||||
tokenize_options = {"images": images}
|
||||
if reference_audio_assets is not None or timeline_video_assets is not None:
|
||||
from ..reference_audio import (
|
||||
reference_audio_asset_items,
|
||||
reference_audio_item,
|
||||
)
|
||||
|
||||
ref_items = [
|
||||
*({"type": "image", "data": image} for image in images),
|
||||
]
|
||||
if timeline_video_assets is not None:
|
||||
ref_items.append(dict(timeline_video_assets.item))
|
||||
if reference_audio_assets is not None:
|
||||
ref_items.extend(
|
||||
reference_audio_item()
|
||||
for _ in reference_audio_asset_items(reference_audio_assets)
|
||||
)
|
||||
tokenize_options = {
|
||||
"minimax_ref_items": ref_items
|
||||
}
|
||||
tokens = clip.tokenize(str(prompt), **tokenize_options)
|
||||
conditioning = clip.encode_from_tokens_scheduled(tokens)
|
||||
if reference_audio_assets is None and timeline_video_assets is None:
|
||||
return conditioning
|
||||
refs = []
|
||||
if timeline_video_assets is not None:
|
||||
refs.append(dict(timeline_video_assets.block))
|
||||
if reference_audio_assets is not None:
|
||||
from ..reference_audio import reference_audio_asset_items, reference_audio_block
|
||||
|
||||
refs.extend(
|
||||
dict(reference_audio_block(item))
|
||||
for item in reference_audio_asset_items(reference_audio_assets)
|
||||
)
|
||||
return [
|
||||
[tensor, {**dict(metadata), "minimax_refs": list(refs)}]
|
||||
for tensor, metadata in conditioning
|
||||
]
|
||||
|
||||
|
||||
def attach_keyframes(
|
||||
conditioning: list,
|
||||
*,
|
||||
frame_count: int,
|
||||
first_latent: torch.Tensor | None,
|
||||
last_latent: torch.Tensor | None,
|
||||
) -> list:
|
||||
output = clone_conditioning(conditioning)
|
||||
keyframes: list[dict[str, Any]] = []
|
||||
if first_latent is not None:
|
||||
keyframes.append({"resolved_frame_index": 0, "latent": first_latent})
|
||||
if last_latent is not None:
|
||||
keyframes.append(
|
||||
{"resolved_frame_index": int(frame_count) - 1, "latent": last_latent}
|
||||
)
|
||||
for _, metadata in output:
|
||||
if keyframes:
|
||||
metadata["minimax_keyframes"] = [dict(item) for item in keyframes]
|
||||
metadata["minimax_frame_count"] = int(frame_count)
|
||||
else:
|
||||
metadata.pop("minimax_keyframes", None)
|
||||
metadata["minimax_frame_count"] = int(frame_count)
|
||||
return output
|
||||
|
||||
|
||||
def empty_h3_latent(width: int, height: int, frame_count: int) -> dict[str, Any]:
|
||||
try:
|
||||
import comfy.model_management
|
||||
import comfy.nested_tensor
|
||||
except Exception as exc: # pragma: no cover - ComfyUI runtime only
|
||||
raise RuntimeError(f"ComfyUI H3 latent helpers unavailable: {exc}") from exc
|
||||
width = int(width)
|
||||
height = int(height)
|
||||
frame_count = int(frame_count)
|
||||
if width <= 0 or height <= 0 or width % 32 or height % 32:
|
||||
raise ValueError("H3 width and height must be positive multiples of 32")
|
||||
device = comfy.model_management.intermediate_device()
|
||||
video = torch.zeros(
|
||||
(1, 24, video_latent_t(frame_count), height // 16, width // 16),
|
||||
device=device,
|
||||
dtype=torch.float32,
|
||||
)
|
||||
audio = torch.zeros(
|
||||
(1, 32, 2, audio_latent_t(frame_count)),
|
||||
device=device,
|
||||
dtype=torch.float32,
|
||||
)
|
||||
return {"samples": comfy.nested_tensor.NestedTensor((video, audio))}
|
||||
|
||||
|
||||
def encode_identity_latents(video_vae: Any, assets: IdentityAssets) -> IdentityAssets:
|
||||
"""Encode already-resized identity images without changing their fingerprint."""
|
||||
|
||||
first_latent = assets.first_latent
|
||||
last_latent = assets.last_latent
|
||||
if first_latent is None and assets.first_image is not None:
|
||||
first_latent = video_vae.encode(assets.first_image)
|
||||
if last_latent is None and assets.last_image is not None:
|
||||
last_latent = video_vae.encode(assets.last_image)
|
||||
return IdentityAssets(
|
||||
first_image=assets.first_image,
|
||||
first_latent=first_latent,
|
||||
last_image=assets.last_image,
|
||||
last_latent=last_latent,
|
||||
identity_hash=assets.identity_hash,
|
||||
)
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
"""Conservative latent-motion analysis for automatic context selection."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from typing import Any
|
||||
|
||||
import torch
|
||||
|
||||
from ..constants import (
|
||||
CONTINUITY_FRAMES,
|
||||
V2_CONTINUITY_AUTO,
|
||||
normalize_continuity_mode,
|
||||
)
|
||||
from ..state import validate_state
|
||||
|
||||
# Conservative defaults. Borderline values intentionally fall back to 22 frames.
|
||||
_AUTO_STATIC_MAX = 0.035
|
||||
_AUTO_STRONG_MIN = 0.120
|
||||
|
||||
|
||||
def latent_motion_score(state: dict[str, Any]) -> float:
|
||||
state = validate_state(state)
|
||||
video = state["video_tail"].detach().to(dtype=torch.float32)
|
||||
if video.shape[2] < 2:
|
||||
return 0.0
|
||||
temporal_delta = video[:, :, 1:] - video[:, :, :-1]
|
||||
delta_rms = torch.sqrt(torch.mean(temporal_delta.square()))
|
||||
signal_rms = torch.sqrt(torch.mean(video.square())).clamp_min(1e-6)
|
||||
score = float((delta_rms / signal_rms).item())
|
||||
if not math.isfinite(score):
|
||||
return _AUTO_STRONG_MIN
|
||||
return max(0.0, min(score, 1000.0))
|
||||
|
||||
|
||||
def choose_context_frames(mode: str, state: dict[str, Any]) -> tuple[int, float, str]:
|
||||
mode = normalize_continuity_mode(mode)
|
||||
state = validate_state(state)
|
||||
capacity = int(state["capacity_frames"])
|
||||
score = latent_motion_score(state)
|
||||
|
||||
if mode != V2_CONTINUITY_AUTO:
|
||||
if mode not in CONTINUITY_FRAMES:
|
||||
raise ValueError(f"unknown continuity mode: {mode!r}")
|
||||
requested = int(CONTINUITY_FRAMES[mode])
|
||||
if requested > capacity:
|
||||
raise ValueError(
|
||||
f"state retains {capacity} frames but continuity requests {requested}"
|
||||
)
|
||||
return requested, score, "fixed profile"
|
||||
|
||||
if capacity < 22:
|
||||
return 5, score, "state capacity below 22 frames"
|
||||
if score <= _AUTO_STATIC_MAX:
|
||||
return 5, score, "low latent motion"
|
||||
if score >= _AUTO_STRONG_MIN and capacity >= 39:
|
||||
return 39, score, "high latent motion"
|
||||
return 22, score, "conservative balanced fallback"
|
||||
+933
@@ -0,0 +1,933 @@
|
||||
"""ComfyUI node layer for H3 Continuum V2."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from ..constants import (
|
||||
CHUNK_SECONDS_DEFAULT,
|
||||
CHUNK_SECONDS_MAX,
|
||||
CHUNK_SECONDS_MIN,
|
||||
CHUNK_SECONDS_STEP,
|
||||
CHUNK_SECONDS_TOOLTIP,
|
||||
DIAGNOSTICS_ACCEPTED_OPTIONS,
|
||||
DIAGNOSTICS_OPTIONS,
|
||||
PROMPT_FORMAT_OPTIONS,
|
||||
PROMPT_MODE_OPTIONS,
|
||||
SEAM_CORRECTION_AUTO,
|
||||
SEAM_CORRECTION_OFF,
|
||||
SEAM_CORRECTION_OPTIONS,
|
||||
V2_CONTINUITY_OPTIONS,
|
||||
normalize_diagnostics_mode,
|
||||
)
|
||||
from .prompts import (
|
||||
apply_prompt_overrides,
|
||||
build_sampler_prompt_plan,
|
||||
make_prompt_plan,
|
||||
parse_sparse_prompt_overrides,
|
||||
prompt_plan_report,
|
||||
validate_sparse_prompt_overrides,
|
||||
)
|
||||
from .sequence import run_sequence
|
||||
from .session import entry_to_state, session_summary, validate_session
|
||||
from .session_io import (
|
||||
load_session,
|
||||
save_session,
|
||||
session_mtime,
|
||||
session_paths,
|
||||
)
|
||||
|
||||
CATEGORY = "MiniMax H3/Continuum"
|
||||
LOG = logging.getLogger("h3_continuum_join")
|
||||
SPARSE_OVERRIDE_SCHEMA_VERSION = 1
|
||||
|
||||
|
||||
def _repair_v200_example_widget_shift(
|
||||
*,
|
||||
audio_continuity,
|
||||
exact_total_duration,
|
||||
diagnostics,
|
||||
reroll_from_chunk,
|
||||
reroll_nonce,
|
||||
strict_compatibility,
|
||||
debug,
|
||||
):
|
||||
"""Repair the malformed bundled 2.0.0 example workflow in memory.
|
||||
|
||||
``base_seed`` uses ComfyUI's ``control_after_generate`` option, which owns an
|
||||
additional serialized widget. The original hand-authored V2 example omitted
|
||||
that widget, shifting every following widget by one slot. A distinctive
|
||||
signature is therefore ``exact_total_duration == 'Basic'/'Full'/'Off'`` and
|
||||
numeric ``diagnostics``. Valid workflows never have a diagnostics label in
|
||||
the BOOLEAN exact-duration input, so this migration is narrow and safe.
|
||||
|
||||
The missing exact-duration value cannot be recovered from the malformed JSON;
|
||||
restore its documented V2 default (True). The remaining values can be shifted
|
||||
back losslessly.
|
||||
"""
|
||||
|
||||
if isinstance(exact_total_duration, str) and exact_total_duration in DIAGNOSTICS_ACCEPTED_OPTIONS:
|
||||
if isinstance(diagnostics, (int, float, bool)) and not isinstance(diagnostics, str):
|
||||
LOG.warning(
|
||||
"H3 Continuum V2 detected the malformed 2.0.0 bundled workflow widget "
|
||||
"layout; repairing shifted settings in memory. exact_total_duration "
|
||||
"is restored to its default True. Save the workflow again after this run."
|
||||
)
|
||||
return (
|
||||
bool(audio_continuity),
|
||||
True,
|
||||
normalize_diagnostics_mode(exact_total_duration),
|
||||
int(diagnostics),
|
||||
int(reroll_from_chunk),
|
||||
bool(reroll_nonce),
|
||||
bool(strict_compatibility),
|
||||
)
|
||||
return (
|
||||
bool(audio_continuity),
|
||||
bool(exact_total_duration),
|
||||
normalize_diagnostics_mode(diagnostics),
|
||||
int(reroll_from_chunk),
|
||||
int(reroll_nonce),
|
||||
bool(strict_compatibility),
|
||||
bool(debug),
|
||||
)
|
||||
|
||||
|
||||
class H3ContinuumSamplerV2:
|
||||
DESCRIPTION = (
|
||||
"Integrated N-chunk MiniMax H3 continuation sampler. Precomputes prompt/image "
|
||||
"conditioning, isolates accelerator runtime state per chunk, captures "
|
||||
"latent state before decoding, then decodes and assembles the final AV stream."
|
||||
)
|
||||
SEARCH_ALIASES = [
|
||||
"H3 long video sampler",
|
||||
"MiniMax H3 integrated continuation",
|
||||
"H3 Continuum V2",
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def VALIDATE_INPUTS(cls, diagnostics):
|
||||
return True if diagnostics in DIAGNOSTICS_ACCEPTED_OPTIONS else f"unknown report detail: {diagnostics!r}"
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"model": ("MODEL",),
|
||||
"clip": ("CLIP",),
|
||||
"video_vae": ("VAE",),
|
||||
"audio_vae": ("VAE",),
|
||||
"sampler": ("SAMPLER",),
|
||||
"sigmas": ("SIGMAS",),
|
||||
"prompt_mode": (
|
||||
PROMPT_FORMAT_OPTIONS,
|
||||
{
|
||||
"default": PROMPT_FORMAT_OPTIONS[0],
|
||||
"tooltip": (
|
||||
"Auto detects Fixed text, --- separated List text, or "
|
||||
"[0-5s] / [Chunk 1] Timeline sections."
|
||||
),
|
||||
},
|
||||
),
|
||||
"prompt_script": (
|
||||
"STRING",
|
||||
{
|
||||
"default": "Describe one continuous MiniMax H3 shot with audio.",
|
||||
"multiline": True,
|
||||
"dynamicPrompts": True,
|
||||
"tooltip": (
|
||||
"Fixed: one prompt. List: separate chunks with a line containing ---. "
|
||||
"Timeline: use [0-5s], [5-10s], ... or [Chunk 1] headers."
|
||||
),
|
||||
},
|
||||
),
|
||||
"chunks": (
|
||||
"INT",
|
||||
{"default": 3, "min": 1, "max": 16, "step": 1},
|
||||
),
|
||||
"chunk_seconds": (
|
||||
"FLOAT",
|
||||
{
|
||||
"default": CHUNK_SECONDS_DEFAULT,
|
||||
"min": CHUNK_SECONDS_MIN,
|
||||
"max": CHUNK_SECONDS_MAX,
|
||||
"step": CHUNK_SECONDS_STEP,
|
||||
"tooltip": CHUNK_SECONDS_TOOLTIP,
|
||||
},
|
||||
),
|
||||
"width": (
|
||||
"INT",
|
||||
{"default": 1344, "min": 32, "max": 16384, "step": 32},
|
||||
),
|
||||
"height": (
|
||||
"INT",
|
||||
{"default": 768, "min": 32, "max": 16384, "step": 32},
|
||||
),
|
||||
"continuity": (
|
||||
V2_CONTINUITY_OPTIONS,
|
||||
{
|
||||
"default": V2_CONTINUITY_OPTIONS[1],
|
||||
"tooltip": (
|
||||
"Balanced 22f reproduces the V1 default. Auto uses a conservative "
|
||||
"latent-motion score and falls back to 22f near thresholds."
|
||||
),
|
||||
},
|
||||
),
|
||||
"base_seed": (
|
||||
"INT",
|
||||
{
|
||||
"default": 0,
|
||||
"min": 0,
|
||||
"max": 0xFFFFFFFFFFFFFFFF,
|
||||
"control_after_generate": True,
|
||||
},
|
||||
),
|
||||
"audio_continuity": ("BOOLEAN", {"default": True}),
|
||||
"exact_total_duration": (
|
||||
"BOOLEAN",
|
||||
{
|
||||
"default": True,
|
||||
"tooltip": "Trim or minimally pad the assembled result to chunks × seconds × 24fps.",
|
||||
},
|
||||
),
|
||||
"diagnostics": (
|
||||
DIAGNOSTICS_OPTIONS,
|
||||
{
|
||||
"default": DIAGNOSTICS_OPTIONS[0],
|
||||
"display_name": "Report Detail",
|
||||
"tooltip": "Controls report verbosity only; it does not change generation or seam correction.",
|
||||
},
|
||||
),
|
||||
"reroll_from_chunk": (
|
||||
"INT",
|
||||
{
|
||||
"default": 0,
|
||||
"min": 0,
|
||||
"max": 16,
|
||||
"step": 1,
|
||||
"tooltip": (
|
||||
"0 resumes/reuses every matching accepted chunk from the optional session. "
|
||||
"N preserves chunks before N and regenerates N onward."
|
||||
),
|
||||
},
|
||||
),
|
||||
"reroll_nonce": (
|
||||
"INT",
|
||||
{
|
||||
"default": 0,
|
||||
"min": 0,
|
||||
"max": 0xFFFFFFFF,
|
||||
"step": 1,
|
||||
"tooltip": "Change this to derive new seeds for regenerated chunks only.",
|
||||
},
|
||||
),
|
||||
"strict_compatibility": ("BOOLEAN", {"default": False}),
|
||||
"debug": ("BOOLEAN", {"default": False}),
|
||||
"seam_correction": (
|
||||
SEAM_CORRECTION_OPTIONS,
|
||||
{
|
||||
"default": SEAM_CORRECTION_AUTO,
|
||||
"tooltip": (
|
||||
"Auto keeps native video unless a no-blend candidate improves the boundary "
|
||||
"and five-frame transition without component regression; audio is guarded "
|
||||
"independently. Off preserves the decode path. Basic applies the decoded "
|
||||
"A/V correction including conservative video blend."
|
||||
),
|
||||
},
|
||||
),
|
||||
},
|
||||
"optional": {
|
||||
"sequence_prompt": (
|
||||
"STRING",
|
||||
{
|
||||
"forceInput": True,
|
||||
"tooltip": (
|
||||
"Connect one Text (Multiline). Prompt Format parses it for "
|
||||
"the complete sequence without changing this socket."
|
||||
),
|
||||
},
|
||||
),
|
||||
"first_frame": ("IMAGE",),
|
||||
"last_frame": ("IMAGE",),
|
||||
"session": ("H3_CONTINUUM_SESSION",),
|
||||
"initial_state": ("H3_CONTINUUM_STATE",),
|
||||
"prompt_plan": ("H3_CONTINUUM_PROMPT_PLAN",),
|
||||
"show_preview": (
|
||||
"BOOLEAN",
|
||||
{
|
||||
"default": True,
|
||||
"tooltip": "Show the in-progress latent thumbnail for this sampler node.",
|
||||
},
|
||||
),
|
||||
**{
|
||||
f"clip_{index}_prompt": (
|
||||
"STRING",
|
||||
{
|
||||
"forceInput": True,
|
||||
"tooltip": f"Optional prompt override for Clip {index}.",
|
||||
},
|
||||
)
|
||||
for index in range(1, 17)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = (
|
||||
"IMAGE",
|
||||
"AUDIO",
|
||||
"H3_CONTINUUM_STATE",
|
||||
"H3_CONTINUUM_SESSION",
|
||||
"STRING",
|
||||
)
|
||||
RETURN_NAMES = ("images", "audio", "last_state", "session", "report")
|
||||
FUNCTION = "run"
|
||||
CATEGORY = CATEGORY
|
||||
|
||||
def run(
|
||||
self,
|
||||
model,
|
||||
clip,
|
||||
video_vae,
|
||||
audio_vae,
|
||||
sampler,
|
||||
sigmas,
|
||||
prompt_mode,
|
||||
prompt_script,
|
||||
chunks,
|
||||
chunk_seconds,
|
||||
width,
|
||||
height,
|
||||
continuity,
|
||||
base_seed,
|
||||
audio_continuity,
|
||||
exact_total_duration,
|
||||
diagnostics,
|
||||
reroll_from_chunk,
|
||||
reroll_nonce,
|
||||
strict_compatibility,
|
||||
debug,
|
||||
seam_correction=SEAM_CORRECTION_OFF,
|
||||
first_frame=None,
|
||||
last_frame=None,
|
||||
session=None,
|
||||
initial_state=None,
|
||||
prompt_plan=None,
|
||||
sequence_prompt=None,
|
||||
show_preview=True,
|
||||
latent_only=False,
|
||||
reference_assets=None,
|
||||
reference_audio_source=None,
|
||||
reference_audio_vae=None,
|
||||
driving_audio_source=None,
|
||||
driving_audio_vae=None,
|
||||
reference_video_source=None,
|
||||
timeline_video_source=None,
|
||||
guide_source=None,
|
||||
capture_refine_context=False,
|
||||
memory_attribution=False,
|
||||
prompt_conditioning_cache=False,
|
||||
reference_encode_cache=False,
|
||||
continuation_transport="reference_context_v1",
|
||||
max_new_physical_groups=None,
|
||||
_diagnostic_continuation_policy=None,
|
||||
reference_routing_request=None,
|
||||
**clip_prompt_inputs,
|
||||
):
|
||||
(
|
||||
audio_continuity,
|
||||
exact_total_duration,
|
||||
diagnostics,
|
||||
reroll_from_chunk,
|
||||
reroll_nonce,
|
||||
strict_compatibility,
|
||||
debug,
|
||||
) = _repair_v200_example_widget_shift(
|
||||
audio_continuity=audio_continuity,
|
||||
exact_total_duration=exact_total_duration,
|
||||
diagnostics=diagnostics,
|
||||
reroll_from_chunk=reroll_from_chunk,
|
||||
reroll_nonce=reroll_nonce,
|
||||
strict_compatibility=strict_compatibility,
|
||||
debug=debug,
|
||||
)
|
||||
|
||||
# V2.0.2 workflows stored show_preview in this newly inserted widget slot.
|
||||
if isinstance(seam_correction, bool):
|
||||
seam_correction = SEAM_CORRECTION_OFF
|
||||
|
||||
prompt_plan = build_sampler_prompt_plan(
|
||||
prompt_mode=prompt_mode,
|
||||
prompt_script=prompt_script,
|
||||
sequence_prompt=sequence_prompt,
|
||||
prompt_plan=prompt_plan,
|
||||
chunks=int(chunks),
|
||||
chunk_seconds=float(chunk_seconds),
|
||||
)
|
||||
prompt_plan = apply_prompt_overrides(
|
||||
prompt_plan,
|
||||
[
|
||||
clip_prompt_inputs.get(f"clip_{index}_prompt")
|
||||
for index in range(1, int(chunks) + 1)
|
||||
],
|
||||
)
|
||||
sequence_runner = run_sequence
|
||||
if reference_routing_request is not None:
|
||||
from .sequence import (
|
||||
_terminal_flf_merge_enabled,
|
||||
run_sequence_with_reference_routing,
|
||||
)
|
||||
from ..v3.reference_routing import compile_reference_routing_schedule
|
||||
from ..v3.reference_runtime import (
|
||||
ReferenceRoutingRequest,
|
||||
ReferenceRoutingRuntime,
|
||||
)
|
||||
if not isinstance(reference_routing_request, ReferenceRoutingRequest):
|
||||
raise ValueError("invalid private Reference Routing request")
|
||||
routing_schedule = compile_reference_routing_schedule(
|
||||
total_chunks=int(chunks),
|
||||
terminal_merge_enabled=_terminal_flf_merge_enabled(
|
||||
multi_chunk_flf=bool(
|
||||
int(chunks) >= 2 and first_frame is not None and last_frame is not None
|
||||
),
|
||||
chunks=int(chunks),
|
||||
chunk_seconds=float(chunk_seconds),
|
||||
prompt_hashes=list(prompt_plan["hashes"]),
|
||||
timeline_video_source=timeline_video_source,
|
||||
),
|
||||
mode="Custom",
|
||||
selectors_by_slot=reference_routing_request.selectors_by_slot,
|
||||
)
|
||||
if not routing_schedule.valid:
|
||||
details = "; ".join(issue.message for issue in routing_schedule.issues)
|
||||
raise ValueError(f"Reference Routing is invalid: {details}")
|
||||
routing_runtime = ReferenceRoutingRuntime(
|
||||
schedule=routing_schedule,
|
||||
inputs=reference_routing_request.inputs,
|
||||
)
|
||||
reference_routing_request.runtime = routing_runtime
|
||||
sequence_runner = lambda **values: run_sequence_with_reference_routing(
|
||||
reference_routing_runtime=routing_runtime, **values
|
||||
)
|
||||
return sequence_runner(
|
||||
model=model,
|
||||
clip=clip,
|
||||
video_vae=video_vae,
|
||||
audio_vae=audio_vae,
|
||||
sampler=sampler,
|
||||
sigmas=sigmas,
|
||||
first_frame=first_frame,
|
||||
last_frame=last_frame,
|
||||
prompt_plan=prompt_plan,
|
||||
width=int(width),
|
||||
height=int(height),
|
||||
continuity=continuity,
|
||||
base_seed=int(base_seed),
|
||||
audio_continuity=bool(audio_continuity),
|
||||
exact_total_duration=bool(exact_total_duration),
|
||||
diagnostics_mode=diagnostics,
|
||||
reroll_from_chunk=int(reroll_from_chunk),
|
||||
reroll_nonce=int(reroll_nonce),
|
||||
strict_compatibility=False,
|
||||
debug=bool(debug),
|
||||
seam_correction=seam_correction,
|
||||
enable_preview=bool(show_preview),
|
||||
session=session,
|
||||
initial_state=initial_state,
|
||||
latent_only=bool(latent_only),
|
||||
reference_assets=reference_assets,
|
||||
reference_audio_source=reference_audio_source,
|
||||
reference_audio_vae=reference_audio_vae,
|
||||
driving_audio_source=driving_audio_source,
|
||||
driving_audio_vae=driving_audio_vae,
|
||||
reference_video_source=reference_video_source,
|
||||
timeline_video_source=timeline_video_source,
|
||||
guide_source=guide_source,
|
||||
capture_refine_context=bool(capture_refine_context),
|
||||
memory_attribution=bool(memory_attribution),
|
||||
prompt_conditioning_cache=bool(prompt_conditioning_cache),
|
||||
reference_encode_cache=bool(reference_encode_cache),
|
||||
continuation_transport=str(continuation_transport),
|
||||
max_new_physical_groups=max_new_physical_groups,
|
||||
_diagnostic_continuation_policy=_diagnostic_continuation_policy,
|
||||
)
|
||||
|
||||
|
||||
class H3ContinuumPromptPlanPreview:
|
||||
DESCRIPTION = "Validate and preview Fixed/List/Timeline prompt parsing without sampling."
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"prompt_mode": (
|
||||
PROMPT_FORMAT_OPTIONS,
|
||||
{
|
||||
"default": PROMPT_FORMAT_OPTIONS[0],
|
||||
"tooltip": (
|
||||
"Auto detects Fixed text, --- separated List text, or "
|
||||
"[0-5s] / [Chunk 1] Timeline sections."
|
||||
),
|
||||
},
|
||||
),
|
||||
"prompt_script": ("STRING", {"default": "Prompt", "multiline": True}),
|
||||
"chunks": ("INT", {"default": 3, "min": 1, "max": 16, "step": 1}),
|
||||
"chunk_seconds": (
|
||||
"FLOAT",
|
||||
{
|
||||
"default": CHUNK_SECONDS_DEFAULT,
|
||||
"min": CHUNK_SECONDS_MIN,
|
||||
"max": CHUNK_SECONDS_MAX,
|
||||
"step": CHUNK_SECONDS_STEP,
|
||||
"tooltip": CHUNK_SECONDS_TOOLTIP,
|
||||
},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("H3_CONTINUUM_PROMPT_PLAN", "STRING")
|
||||
RETURN_NAMES = ("prompt_plan", "report")
|
||||
FUNCTION = "build"
|
||||
CATEGORY = CATEGORY
|
||||
|
||||
def build(self, prompt_mode, prompt_script, chunks, chunk_seconds):
|
||||
plan = make_prompt_plan(
|
||||
mode=prompt_mode,
|
||||
script=prompt_script,
|
||||
chunks=int(chunks),
|
||||
chunk_seconds=float(chunk_seconds),
|
||||
)
|
||||
report = prompt_plan_report(plan) + "\n" + "\n\n".join(
|
||||
f"Chunk {index + 1}:\n{prompt}" for index, prompt in enumerate(plan["prompts"])
|
||||
)
|
||||
return plan, report
|
||||
|
||||
|
||||
class H3ContinuumSaveSession:
|
||||
DESCRIPTION = "Atomically save every accepted V2 chunk latent plus session metadata."
|
||||
OUTPUT_NODE = True
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"session": ("H3_CONTINUUM_SESSION",),
|
||||
"prefix": ("STRING", {"default": "h3_continuum_session"}),
|
||||
"slot": ("INT", {"default": 1, "min": 1, "max": 9999, "step": 1}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("H3_CONTINUUM_SESSION", "STRING")
|
||||
RETURN_NAMES = ("session", "path")
|
||||
FUNCTION = "save"
|
||||
CATEGORY = CATEGORY
|
||||
|
||||
def save(self, session, prefix, slot):
|
||||
tensor_path, _json_path = save_session(session, prefix=prefix, slot=int(slot))
|
||||
return session, str(tensor_path)
|
||||
|
||||
|
||||
class H3ContinuumLoadSession:
|
||||
DESCRIPTION = "Load an explicitly numbered V2 session for resume or reroll."
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"prefix": ("STRING", {"default": "h3_continuum_session"}),
|
||||
"slot": ("INT", {"default": 1, "min": 1, "max": 9999, "step": 1}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("H3_CONTINUUM_SESSION", "STRING")
|
||||
RETURN_NAMES = ("session", "path")
|
||||
FUNCTION = "load"
|
||||
CATEGORY = CATEGORY
|
||||
|
||||
@classmethod
|
||||
def IS_CHANGED(cls, prefix, slot):
|
||||
return session_mtime(prefix=prefix, slot=int(slot))
|
||||
|
||||
def load(self, prefix, slot):
|
||||
session = load_session(prefix=prefix, slot=int(slot))
|
||||
tensor_path, _json_path = session_paths(prefix, int(slot))
|
||||
return session, str(tensor_path)
|
||||
|
||||
|
||||
class H3ContinuumSessionInfo:
|
||||
DESCRIPTION = "Inspect a V2 session and expose its final V1-compatible continuation state."
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {"required": {"session": ("H3_CONTINUUM_SESSION",)}}
|
||||
|
||||
RETURN_TYPES = ("H3_CONTINUUM_SESSION", "H3_CONTINUUM_STATE", "STRING")
|
||||
RETURN_NAMES = ("session", "last_state", "report")
|
||||
FUNCTION = "inspect"
|
||||
CATEGORY = CATEGORY
|
||||
|
||||
def inspect(self, session):
|
||||
session = validate_session(session)
|
||||
state = entry_to_state(session["chunks"][-1])
|
||||
report = session_summary(session)
|
||||
return session, state, report
|
||||
|
||||
|
||||
class H3ContinuumClipOverrides:
|
||||
DEPRECATED = True
|
||||
"""Build a static prompt-override pack without per-clip sockets."""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"prompt_mode": (
|
||||
PROMPT_FORMAT_OPTIONS,
|
||||
{
|
||||
"default": PROMPT_FORMAT_OPTIONS[0],
|
||||
"display_name": "Prompt Format",
|
||||
"tooltip": (
|
||||
"Retained for V2.1.7 workflow compatibility. Sparse overrides "
|
||||
"use explicit [Clip N] sections."
|
||||
),
|
||||
},
|
||||
),
|
||||
"override_script": (
|
||||
"STRING",
|
||||
{
|
||||
"forceInput": True,
|
||||
"display_name": "Override Script",
|
||||
"tooltip": (
|
||||
"Connect one Text (Multiline). Define only the clips to replace "
|
||||
"with [Clip N] or [Chunk N] sections."
|
||||
),
|
||||
},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("H3_CONTINUUM_CLIP_OVERRIDES",)
|
||||
RETURN_NAMES = ("prompt_overrides",)
|
||||
FUNCTION = "build"
|
||||
CATEGORY = CATEGORY
|
||||
|
||||
def build(self, prompt_mode, override_script):
|
||||
return (
|
||||
{
|
||||
"schema_version": SPARSE_OVERRIDE_SCHEMA_VERSION,
|
||||
"overrides": parse_sparse_prompt_overrides(override_script),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class H3ContinuumAdvanced:
|
||||
"""Package optional continuation inputs and infrequent sampler settings."""
|
||||
|
||||
@classmethod
|
||||
def VALIDATE_INPUTS(cls, diagnostics):
|
||||
return True if diagnostics in DIAGNOSTICS_ACCEPTED_OPTIONS else f"unknown report detail: {diagnostics!r}"
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"audio_continuity": ("BOOLEAN", {"default": True}),
|
||||
"exact_total_duration": ("BOOLEAN", {"default": True}),
|
||||
"diagnostics": (
|
||||
DIAGNOSTICS_OPTIONS,
|
||||
{
|
||||
"default": DIAGNOSTICS_OPTIONS[0],
|
||||
"display_name": "Report Detail",
|
||||
"tooltip": "Controls report verbosity only; it does not change generation or seam correction.",
|
||||
},
|
||||
),
|
||||
"reroll_from_chunk": (
|
||||
"INT",
|
||||
{"default": 0, "min": 0, "max": 16, "step": 1},
|
||||
),
|
||||
"reroll_nonce": (
|
||||
"INT",
|
||||
{"default": 0, "min": 0, "max": 0xFFFFFFFF, "step": 1},
|
||||
),
|
||||
"strict_compatibility": ("BOOLEAN", {"default": False}),
|
||||
"debug": ("BOOLEAN", {"default": False}),
|
||||
"show_preview": ("BOOLEAN", {"default": True}),
|
||||
},
|
||||
"optional": {
|
||||
"last_frame": ("IMAGE",),
|
||||
"session": ("H3_CONTINUUM_SESSION",),
|
||||
"initial_state": ("H3_CONTINUUM_STATE",),
|
||||
"prompt_plan": ("H3_CONTINUUM_PROMPT_PLAN",),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("H3_CONTINUUM_ADVANCED",)
|
||||
RETURN_NAMES = ("advanced",)
|
||||
FUNCTION = "build"
|
||||
CATEGORY = CATEGORY
|
||||
|
||||
def build(
|
||||
self,
|
||||
audio_continuity,
|
||||
exact_total_duration,
|
||||
diagnostics,
|
||||
reroll_from_chunk,
|
||||
reroll_nonce,
|
||||
strict_compatibility,
|
||||
debug,
|
||||
show_preview,
|
||||
last_frame=None,
|
||||
session=None,
|
||||
initial_state=None,
|
||||
prompt_plan=None,
|
||||
):
|
||||
return (
|
||||
{
|
||||
"audio_continuity": bool(audio_continuity),
|
||||
"exact_total_duration": bool(exact_total_duration),
|
||||
"diagnostics": normalize_diagnostics_mode(diagnostics),
|
||||
"reroll_from_chunk": int(reroll_from_chunk),
|
||||
"reroll_nonce": int(reroll_nonce),
|
||||
"strict_compatibility": False,
|
||||
"debug": bool(debug),
|
||||
"show_preview": bool(show_preview),
|
||||
"last_frame": last_frame,
|
||||
"session": session,
|
||||
"initial_state": initial_state,
|
||||
"prompt_plan": prompt_plan,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class H3ContinuumSampler:
|
||||
"""Stable, compact facade over the fully compatible V2 sampler core."""
|
||||
|
||||
DESCRIPTION = (
|
||||
"Compact H3 Continuum sampler facade. Uses the legacy V2 execution core "
|
||||
"without dynamic sockets or frontend display patches."
|
||||
)
|
||||
SEARCH_ALIASES = ["H3 Continuum", "MiniMax H3 long video"]
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"model": ("MODEL",),
|
||||
"clip": ("CLIP",),
|
||||
"video_vae": ("VAE",),
|
||||
"audio_vae": ("VAE",),
|
||||
"sampler": ("SAMPLER",),
|
||||
"sigmas": ("SIGMAS",),
|
||||
"sequence_prompt": (
|
||||
"STRING",
|
||||
{
|
||||
"forceInput": True,
|
||||
"display_name": "Sequence Prompt",
|
||||
"tooltip": "Connect one Text (Multiline) for the complete sequence.",
|
||||
},
|
||||
),
|
||||
"prompt_mode": (
|
||||
PROMPT_FORMAT_OPTIONS,
|
||||
{
|
||||
"default": PROMPT_FORMAT_OPTIONS[0],
|
||||
"display_name": "Prompt Format",
|
||||
},
|
||||
),
|
||||
"chunks": ("INT", {"default": 3, "min": 1, "max": 16, "step": 1}),
|
||||
"chunk_seconds": (
|
||||
"FLOAT",
|
||||
{
|
||||
"default": CHUNK_SECONDS_DEFAULT,
|
||||
"min": CHUNK_SECONDS_MIN,
|
||||
"max": CHUNK_SECONDS_MAX,
|
||||
"step": CHUNK_SECONDS_STEP,
|
||||
"tooltip": CHUNK_SECONDS_TOOLTIP,
|
||||
},
|
||||
),
|
||||
"width": ("INT", {"default": 1344, "min": 32, "max": 16384, "step": 32}),
|
||||
"height": ("INT", {"default": 768, "min": 32, "max": 16384, "step": 32}),
|
||||
"continuity": (
|
||||
V2_CONTINUITY_OPTIONS,
|
||||
{"default": V2_CONTINUITY_OPTIONS[1]},
|
||||
),
|
||||
"base_seed": (
|
||||
"INT",
|
||||
{
|
||||
"default": 0,
|
||||
"min": 0,
|
||||
"max": 0xFFFFFFFFFFFFFFFF,
|
||||
"control_after_generate": True,
|
||||
},
|
||||
),
|
||||
"seam_correction": (
|
||||
SEAM_CORRECTION_OPTIONS,
|
||||
{
|
||||
"default": SEAM_CORRECTION_AUTO,
|
||||
"tooltip": (
|
||||
"Auto is native-first: video correction is adopted only when the boundary "
|
||||
"and five-frame transition clearly improve; audio is guarded independently."
|
||||
),
|
||||
},
|
||||
),
|
||||
},
|
||||
"optional": {
|
||||
"first_frame": ("IMAGE",),
|
||||
"prompt_overrides": ("H3_CONTINUUM_CLIP_OVERRIDES",),
|
||||
"advanced": ("H3_CONTINUUM_ADVANCED",),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE", "AUDIO", "H3_CONTINUUM_RESULT")
|
||||
RETURN_NAMES = ("images", "audio", "result")
|
||||
FUNCTION = "run"
|
||||
CATEGORY = CATEGORY
|
||||
|
||||
def run(
|
||||
self,
|
||||
model,
|
||||
clip,
|
||||
video_vae,
|
||||
audio_vae,
|
||||
sampler,
|
||||
sigmas,
|
||||
sequence_prompt,
|
||||
prompt_mode,
|
||||
chunks,
|
||||
chunk_seconds,
|
||||
width,
|
||||
height,
|
||||
continuity,
|
||||
base_seed,
|
||||
seam_correction,
|
||||
first_frame=None,
|
||||
prompt_overrides=None,
|
||||
advanced=None,
|
||||
):
|
||||
if prompt_overrides is not None and not isinstance(prompt_overrides, dict):
|
||||
raise TypeError("prompt_overrides must be an H3_CONTINUUM_CLIP_OVERRIDES pack")
|
||||
if advanced is not None and not isinstance(advanced, dict):
|
||||
advanced = None
|
||||
|
||||
advanced_values = {
|
||||
"audio_continuity": True,
|
||||
"exact_total_duration": True,
|
||||
"diagnostics": DIAGNOSTICS_OPTIONS[0],
|
||||
"reroll_from_chunk": 0,
|
||||
"reroll_nonce": 0,
|
||||
"strict_compatibility": False,
|
||||
"debug": False,
|
||||
"show_preview": True,
|
||||
"last_frame": None,
|
||||
"session": None,
|
||||
"initial_state": None,
|
||||
"prompt_plan": None,
|
||||
}
|
||||
if advanced:
|
||||
advanced_values.update(advanced)
|
||||
advanced_values["strict_compatibility"] = False
|
||||
|
||||
clip_prompt_inputs = {}
|
||||
if prompt_overrides:
|
||||
if (
|
||||
type(prompt_overrides.get("schema_version")) is not int
|
||||
or prompt_overrides.get("schema_version") != SPARSE_OVERRIDE_SCHEMA_VERSION
|
||||
):
|
||||
prompt_overrides = None
|
||||
if prompt_overrides is not None:
|
||||
try:
|
||||
sparse_overrides = validate_sparse_prompt_overrides(
|
||||
prompt_overrides.get("overrides"), chunks=int(chunks)
|
||||
)
|
||||
except (TypeError, ValueError):
|
||||
sparse_overrides = {}
|
||||
for index, prompt in sparse_overrides.items():
|
||||
clip_prompt_inputs[f"clip_{index}_prompt"] = prompt
|
||||
|
||||
images, audio, last_state, session, report = H3ContinuumSamplerV2().run(
|
||||
model=model,
|
||||
clip=clip,
|
||||
video_vae=video_vae,
|
||||
audio_vae=audio_vae,
|
||||
sampler=sampler,
|
||||
sigmas=sigmas,
|
||||
prompt_mode=prompt_mode,
|
||||
prompt_script=sequence_prompt,
|
||||
chunks=chunks,
|
||||
chunk_seconds=chunk_seconds,
|
||||
width=width,
|
||||
height=height,
|
||||
continuity=continuity,
|
||||
base_seed=base_seed,
|
||||
audio_continuity=advanced_values["audio_continuity"],
|
||||
exact_total_duration=advanced_values["exact_total_duration"],
|
||||
diagnostics=advanced_values["diagnostics"],
|
||||
reroll_from_chunk=advanced_values["reroll_from_chunk"],
|
||||
reroll_nonce=advanced_values["reroll_nonce"],
|
||||
strict_compatibility=False,
|
||||
debug=advanced_values["debug"],
|
||||
seam_correction=seam_correction,
|
||||
first_frame=first_frame,
|
||||
last_frame=advanced_values["last_frame"],
|
||||
session=advanced_values["session"],
|
||||
initial_state=advanced_values["initial_state"],
|
||||
prompt_plan=advanced_values["prompt_plan"],
|
||||
sequence_prompt=sequence_prompt,
|
||||
show_preview=advanced_values["show_preview"],
|
||||
**clip_prompt_inputs,
|
||||
)
|
||||
return (
|
||||
images,
|
||||
audio,
|
||||
{
|
||||
"last_state": last_state,
|
||||
"session": session,
|
||||
"report": report,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class H3ContinuumResult:
|
||||
"""Expand the compact facade result pack only when advanced outputs are needed."""
|
||||
|
||||
DEPRECATED = True
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {"required": {"result": ("H3_CONTINUUM_RESULT",)}}
|
||||
|
||||
RETURN_TYPES = ("H3_CONTINUUM_STATE", "H3_CONTINUUM_SESSION", "STRING")
|
||||
RETURN_NAMES = ("last_state", "session", "report")
|
||||
FUNCTION = "unpack"
|
||||
CATEGORY = CATEGORY
|
||||
|
||||
def unpack(self, result):
|
||||
if not isinstance(result, dict):
|
||||
raise TypeError("result must be an H3_CONTINUUM_RESULT pack")
|
||||
missing = {"last_state", "session", "report"} - set(result)
|
||||
if missing:
|
||||
raise ValueError("H3_CONTINUUM_RESULT is missing: " + ", ".join(sorted(missing)))
|
||||
return result["last_state"], result["session"], str(result["report"])
|
||||
|
||||
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"H3ContinuumSampler": H3ContinuumSampler,
|
||||
"H3ContinuumClipOverrides": H3ContinuumClipOverrides,
|
||||
"H3ContinuumAdvanced": H3ContinuumAdvanced,
|
||||
"H3ContinuumResult": H3ContinuumResult,
|
||||
"H3ContinuumSamplerV2": H3ContinuumSamplerV2,
|
||||
"H3ContinuumPromptPlanPreview": H3ContinuumPromptPlanPreview,
|
||||
"H3ContinuumSaveSession": H3ContinuumSaveSession,
|
||||
"H3ContinuumLoadSession": H3ContinuumLoadSession,
|
||||
"H3ContinuumSessionInfo": H3ContinuumSessionInfo,
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"H3ContinuumSampler": "H3 Continuum Sampler",
|
||||
"H3ContinuumClipOverrides": "H3 Continuum Clip Overrides",
|
||||
"H3ContinuumAdvanced": "H3 Continuum Advanced",
|
||||
"H3ContinuumResult": "H3 Continuum Result",
|
||||
"H3ContinuumSamplerV2": "H3 Continuum Sampler V2",
|
||||
"H3ContinuumPromptPlanPreview": "H3 Continuum Prompt Plan",
|
||||
"H3ContinuumSaveSession": "H3 Continuum Save Session",
|
||||
"H3ContinuumLoadSession": "H3 Continuum Load Session",
|
||||
"H3ContinuumSessionInfo": "H3 Continuum Session Info",
|
||||
}
|
||||
+251
@@ -0,0 +1,251 @@
|
||||
"""Prompt planning for the integrated H3 Continuum sampler.
|
||||
|
||||
The parser is intentionally deterministic and side-effect free so saved workflows
|
||||
can be reproduced without depending on UI state.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
import hashlib, json, re
|
||||
from typing import Any
|
||||
from ..constants import (
|
||||
CHUNK_SECONDS_MAX,CHUNK_SECONDS_MIN,
|
||||
PROMPT_FORMAT_AUTO,PROMPT_FORMAT_FIXED,PROMPT_FORMAT_LIST,PROMPT_FORMAT_TIMELINE,
|
||||
PROMPT_MODE_FIXED,PROMPT_MODE_LIST,PROMPT_MODE_TIMELINE,PROMPT_PLAN_MAGIC,
|
||||
)
|
||||
from ..version import PROMPT_PLAN_SCHEMA_VERSION
|
||||
_TIMELINE_HEADER=re.compile(r"^\s*\[\s*(?P<start>\d+(?:\.\d+)?)\s*(?:s|sec|seconds)?\s*[-–—]\s*(?P<end>\d+(?:\.\d+)?)\s*(?:s|sec|seconds)?\s*\]\s*$",re.IGNORECASE)
|
||||
_CHUNK_HEADER=re.compile(r"^\s*\[\s*(?:chunk|clip)\s*(?P<index>\d+)\s*\]\s*$",re.IGNORECASE)
|
||||
_TIMELINE_LIKE_HEADER=re.compile(r"^\s*\[\s*(?:(?:chunk|clip)\b|\d+(?:\.\d+)?[^\]]*(?:[-–—]|\bto\b))",re.IGNORECASE)
|
||||
_LIST_SEPARATOR=re.compile(r"^\s*---+\s*$",re.MULTILINE)
|
||||
class PromptPlanError(ValueError): pass
|
||||
def _prompt_error(code,reason,*,line_number=None,source=None,suggested_fix):
|
||||
location=f" line {line_number}" if line_number is not None else ""
|
||||
lines=[f"{code}{location}: {reason}"]
|
||||
if source is not None: lines.append(f"Source: {source}")
|
||||
if suggested_fix: lines.append(f"Suggested fix: {suggested_fix}")
|
||||
raise PromptPlanError("\n".join(lines))
|
||||
def prompt_hash(prompt:str)->str: return hashlib.sha256(prompt.encode("utf-8")).hexdigest()
|
||||
def _normalize_prompt(value:str,*,label:str)->str:
|
||||
return "" if value is None else str(value).strip()
|
||||
def _parse_json_list(script:str):
|
||||
stripped=script.lstrip()
|
||||
if not stripped.startswith("["): return None
|
||||
try: value=json.loads(script)
|
||||
except json.JSONDecodeError: return None
|
||||
if not isinstance(value,list) or not all(isinstance(item,str) for item in value): return None
|
||||
return [_normalize_prompt(item,label=f"prompt {index+1}") for index,item in enumerate(value)]
|
||||
def _parse_list(script,chunks):
|
||||
values=_parse_json_list(script)
|
||||
if values is None: values=[_normalize_prompt(part,label=f"prompt {index+1}") for index,part in enumerate(_LIST_SEPARATOR.split(script)) if part.strip()]
|
||||
if not values: values=[str(script)]
|
||||
source_count=len(values); notes=[]
|
||||
if len(values)<chunks: notes.append(f"repeated the last prompt for {chunks-len(values)} chunk(s)"); values.extend([values[-1]]*(chunks-len(values)))
|
||||
elif len(values)>chunks: notes.append(f"ignored {len(values)-chunks} extra prompt(s)"); values=values[:chunks]
|
||||
sources=[min(index+1,source_count) for index in range(len(values))]
|
||||
return values,notes,sources
|
||||
def _timeline_preamble(script,*,ignore_list_separators=False):
|
||||
body=[]
|
||||
for line in str(script).splitlines():
|
||||
if _TIMELINE_HEADER.match(line) or _CHUNK_HEADER.match(line): break
|
||||
if ignore_list_separators and _LIST_SEPARATOR.fullmatch(line): continue
|
||||
body.append(line)
|
||||
return "\n".join(body).strip()
|
||||
def _parse_timeline_sections(script,*,allow_preamble=True,ignore_list_separators=False):
|
||||
preamble=_timeline_preamble(script,ignore_list_separators=ignore_list_separators)
|
||||
sections=[]; current=None; body=[]
|
||||
def finish():
|
||||
nonlocal current,body
|
||||
if current is None:
|
||||
if any(line.strip() for line in body) and not allow_preamble: raise PromptPlanError("timeline text before the first [0-5s] or [Chunk 1] header is not allowed")
|
||||
body=[]; return
|
||||
if not any(line.strip() for line in body):
|
||||
_prompt_error("H3C-P005","timeline section body is empty",line_number=current["line_number"],source=current["source"],suggested_fix="add prompt text below this header")
|
||||
prompt=_normalize_prompt("\n".join(body),label="timeline section")
|
||||
current["prompt"]=f"{preamble}\n\n{prompt}" if preamble else prompt
|
||||
sections.append(current); current=None; body=[]
|
||||
for line_number,line in enumerate(str(script).splitlines(),start=1):
|
||||
time_match=_TIMELINE_HEADER.match(line); chunk_match=_CHUNK_HEADER.match(line)
|
||||
if time_match or chunk_match:
|
||||
finish()
|
||||
if time_match:
|
||||
start=float(time_match.group("start")); end=float(time_match.group("end"))
|
||||
if not end>start: _prompt_error("H3C-P002","timeline end must be greater than its start",line_number=line_number,source=line.strip(),suggested_fix="use an increasing range such as [0-5s]")
|
||||
current={"kind":"time","start":start,"end":end,"line_number":line_number,"source":line.strip()}
|
||||
else:
|
||||
index=int(chunk_match.group("index"))
|
||||
if index<1: _prompt_error("H3C-P004","chunk numbers are one-based",line_number=line_number,source=line.strip(),suggested_fix="use [Chunk 1] or a larger one-based chunk number")
|
||||
current={"kind":"chunk","index":index,"line_number":line_number,"source":line.strip()}
|
||||
continue
|
||||
if _TIMELINE_LIKE_HEADER.match(line):
|
||||
_prompt_error("H3C-P001","invalid timeline header",line_number=line_number,source=line.strip(),suggested_fix="use [0-5s] or [Chunk 1]")
|
||||
if ignore_list_separators and _LIST_SEPARATOR.fullmatch(line):
|
||||
continue
|
||||
body.append(line)
|
||||
finish()
|
||||
if not sections: raise PromptPlanError("timeline contains no sections")
|
||||
return sections
|
||||
def parse_sparse_prompt_overrides(script):
|
||||
sections=_parse_timeline_sections(script,allow_preamble=False); overrides={}
|
||||
for item in sections:
|
||||
if item["kind"]!="chunk": raise PromptPlanError("Sparse Clip Overrides only accepts [Clip N] or [Chunk N] sections")
|
||||
index=int(item["index"])
|
||||
if index in overrides: raise PromptPlanError(f"Sparse Clip Overrides defines Clip {index} more than once")
|
||||
overrides[index]=item["prompt"]
|
||||
return overrides
|
||||
def validate_sparse_prompt_overrides(overrides,*,chunks):
|
||||
chunks=int(chunks)
|
||||
if not isinstance(overrides,dict): raise PromptPlanError("Sparse Clip Overrides must be a dictionary")
|
||||
validated={}
|
||||
for index,prompt in overrides.items():
|
||||
if type(index) is not int: raise PromptPlanError("Sparse Clip Override numbers must be integers")
|
||||
if not 1<=index<=chunks: raise PromptPlanError(f"Sparse Clip Override {index} is outside the configured 1-{chunks} chunks")
|
||||
validated[index]=_normalize_prompt(prompt,label=f"Clip {index} Override")
|
||||
if not validated: raise PromptPlanError("Sparse Clip Overrides contains no overrides")
|
||||
return validated
|
||||
def _parse_timeline(script,chunks,chunk_seconds,*,ignore_list_separators=False):
|
||||
sections=_parse_timeline_sections(script,ignore_list_separators=ignore_list_separators); prompts=[]; notes=[]; diagnostics=[]
|
||||
if _timeline_preamble(script,ignore_list_separators=ignore_list_separators): notes.append("applied timeline preamble to all chunks")
|
||||
seen_chunks={}; total_end=chunks*chunk_seconds; usable=[]
|
||||
for item in sections:
|
||||
if item["kind"]=="chunk":
|
||||
index=int(item["index"])
|
||||
if index in seen_chunks:
|
||||
_prompt_error("H3C-P003",f"timeline defines Chunk {index} more than once",line_number=item["line_number"],source=item["source"],suggested_fix=f"keep only one [Chunk {index}] section")
|
||||
seen_chunks[index]=item
|
||||
if index<=chunks: usable.append(item)
|
||||
else: diagnostics.append({"level":"warning","code":"H3C-P104","message":f"ignored {item['source']} because the configured sequence has only {chunks} chunk(s)"})
|
||||
elif min(total_end,item["end"])-max(0.0,item["start"])>0:
|
||||
usable.append(item)
|
||||
else:
|
||||
diagnostics.append({"level":"warning","code":"H3C-P104","message":f"ignored {item['source']} because it is outside 0-{total_end:.3f}s"})
|
||||
if not usable:
|
||||
_prompt_error("H3C-P005","timeline contains no usable prompt section for the configured sequence",suggested_fix="add a section such as [0-5s] with non-empty prompt text")
|
||||
previous_prompt=None; previous_source=None
|
||||
for chunk_index in range(1,chunks+1):
|
||||
chunk_sections=[item for item in sections if item["kind"]=="chunk" and item["index"]==chunk_index]
|
||||
if chunk_sections:
|
||||
selected=chunk_sections[0]; prompts.append(selected["prompt"]); previous_prompt=selected["prompt"]; previous_source=f"{selected['source']} at line {selected['line_number']}"
|
||||
diagnostics.append({"level":"info","code":"H3C-P000","message":f"Chunk {chunk_index} source: {previous_source}"}); continue
|
||||
start=(chunk_index-1)*chunk_seconds; end=chunk_index*chunk_seconds; scored=[]
|
||||
for order,item in enumerate(sections):
|
||||
if item["kind"]!="time": continue
|
||||
overlap=max(0.0,min(end,item["end"])-max(start,item["start"]))
|
||||
if overlap>0: scored.append((overlap,-order,item))
|
||||
if not scored:
|
||||
if previous_prompt is None:
|
||||
selected=usable[0]; fallback_reason=f"used earliest valid prompt from {selected['source']}"
|
||||
else:
|
||||
selected=None; fallback_reason=f"reused previous prompt from {previous_source}"
|
||||
prompt=selected["prompt"] if selected is not None else previous_prompt
|
||||
source=f"{selected['source']} at line {selected['line_number']}" if selected is not None else previous_source
|
||||
prompts.append(prompt); previous_prompt=prompt; previous_source=source
|
||||
diagnostics.append({"level":"warning","code":"H3C-P101","message":f"Timeline does not cover Chunk {chunk_index} ({start:.3f}-{end:.3f}s); {fallback_reason}."})
|
||||
diagnostics.append({"level":"info","code":"H3C-P000","message":f"Chunk {chunk_index} source: fallback to {source}"}); continue
|
||||
scored.sort(reverse=True,key=lambda row:(row[0],row[1])); selected=scored[0][2]; prompts.append(selected["prompt"]); previous_prompt=selected["prompt"]; previous_source=f"{selected['source']} at line {selected['line_number']}"
|
||||
diagnostics.append({"level":"info","code":"H3C-P000","message":f"Chunk {chunk_index} source: {previous_source}"})
|
||||
if len(scored)>1 and abs(scored[0][0]-scored[1][0])<1e-9: notes.append(f"chunk {chunk_index} had an equal-overlap timeline tie; used the earlier section")
|
||||
return prompts,notes,diagnostics
|
||||
def _has_timeline_header(script):
|
||||
return any(_TIMELINE_HEADER.match(line) or _CHUNK_HEADER.match(line) for line in str(script).splitlines())
|
||||
def _has_list_separator(script):
|
||||
return _LIST_SEPARATOR.search(str(script)) is not None
|
||||
def _mixed_syntax_diagnostic(*,timeline_active):
|
||||
message="Mixed Timeline and List prompt syntax detected. Use either [Chunk N] / [0-5s] sections or --- separators, not both."
|
||||
if timeline_active: message+=" Standalone --- separators were ignored because Timeline syntax is active."
|
||||
return {"level":"warning","code":"H3C-P105","message":message}
|
||||
def detect_prompt_mode(script):
|
||||
text=str(script)
|
||||
for line in text.splitlines():
|
||||
if _TIMELINE_HEADER.match(line) or _CHUNK_HEADER.match(line): return PROMPT_MODE_TIMELINE
|
||||
if _TIMELINE_LIKE_HEADER.match(line): return PROMPT_MODE_FIXED
|
||||
if _parse_json_list(text) is not None or _LIST_SEPARATOR.search(text): return PROMPT_MODE_LIST
|
||||
return PROMPT_MODE_FIXED
|
||||
def resolve_prompt_mode(mode,script):
|
||||
explicit={PROMPT_FORMAT_FIXED:PROMPT_MODE_FIXED,PROMPT_FORMAT_LIST:PROMPT_MODE_LIST,PROMPT_FORMAT_TIMELINE:PROMPT_MODE_TIMELINE}
|
||||
if mode==PROMPT_FORMAT_AUTO: return detect_prompt_mode(script)
|
||||
if mode in explicit: return explicit[mode]
|
||||
if mode in (PROMPT_MODE_FIXED,PROMPT_MODE_LIST,PROMPT_MODE_TIMELINE): return mode
|
||||
return PROMPT_MODE_FIXED
|
||||
def make_prompt_plan(*,mode,script,chunks,chunk_seconds):
|
||||
chunks=int(chunks); chunk_seconds=float(chunk_seconds)
|
||||
if not 1<=chunks<=16: raise PromptPlanError("chunks must be between 1 and 16")
|
||||
if not CHUNK_SECONDS_MIN<=chunk_seconds<=CHUNK_SECONDS_MAX: raise PromptPlanError(f"chunk_seconds must be between {CHUNK_SECONDS_MIN:.1f} and {CHUNK_SECONDS_MAX:.1f}")
|
||||
notes=[]; diagnostics=[]
|
||||
try:
|
||||
resolved_mode=resolve_prompt_mode(mode,script)
|
||||
if mode==PROMPT_FORMAT_AUTO:
|
||||
detected={PROMPT_MODE_FIXED:PROMPT_FORMAT_FIXED,PROMPT_MODE_LIST:PROMPT_FORMAT_LIST,PROMPT_MODE_TIMELINE:PROMPT_FORMAT_TIMELINE}[resolved_mode]; notes.append(f"Auto detected {detected}")
|
||||
if resolved_mode==PROMPT_MODE_FIXED: prompt=_normalize_prompt(script,label="fixed prompt"); prompts=[prompt]*chunks
|
||||
elif resolved_mode==PROMPT_MODE_LIST:
|
||||
prompts,parse_notes,list_sources=_parse_list(script,chunks); notes.extend(parse_notes)
|
||||
if _has_timeline_header(script):
|
||||
diagnostics.append(_mixed_syntax_diagnostic(timeline_active=False))
|
||||
diagnostics.extend({"level":"info","code":"H3C-P000","message":f"Chunk {index+1} source: List section {source}"} for index,source in enumerate(list_sources))
|
||||
elif resolved_mode==PROMPT_MODE_TIMELINE:
|
||||
mixed_syntax=_has_list_separator(script)
|
||||
prompts,parse_notes,diagnostics=_parse_timeline(script,chunks,chunk_seconds,ignore_list_separators=mixed_syntax); notes.extend(parse_notes)
|
||||
if mixed_syntax: diagnostics.insert(0,_mixed_syntax_diagnostic(timeline_active=True))
|
||||
else: raise PromptPlanError(f"unknown prompt mode: {resolved_mode!r}")
|
||||
except (PromptPlanError,TypeError,ValueError) as exc:
|
||||
resolved_mode=PROMPT_MODE_FIXED; prompts=[_normalize_prompt(script,label="fixed prompt")]*chunks
|
||||
notes.append("Prompt syntax was not recognized; used Fixed prompt fallback")
|
||||
diagnostics=[{"level":"warning","code":"H3C-P100","message":str(exc)}]
|
||||
result={"magic":PROMPT_PLAN_MAGIC,"schema_version":PROMPT_PLAN_SCHEMA_VERSION,"mode":resolved_mode,"chunks":chunks,"chunk_seconds":chunk_seconds,"prompts":prompts,"hashes":[prompt_hash(p) for p in prompts],"notes":notes}
|
||||
if diagnostics: result["diagnostics"]=diagnostics
|
||||
return result
|
||||
def validate_prompt_plan(plan):
|
||||
if not isinstance(plan,dict) or plan.get("magic")!=PROMPT_PLAN_MAGIC: raise PromptPlanError("invalid H3 Continuum prompt plan")
|
||||
if int(plan.get("schema_version",-1))!=PROMPT_PLAN_SCHEMA_VERSION: raise PromptPlanError(f"unsupported prompt-plan schema {plan.get('schema_version')}; expected {PROMPT_PLAN_SCHEMA_VERSION}")
|
||||
chunks=int(plan.get("chunks",0)); prompts=plan.get("prompts"); hashes=plan.get("hashes")
|
||||
if not 1<=chunks<=16: raise PromptPlanError("prompt-plan chunks are invalid")
|
||||
try: chunk_seconds=float(plan.get("chunk_seconds"))
|
||||
except (TypeError,ValueError): raise PromptPlanError("prompt-plan chunk_seconds is invalid") from None
|
||||
if not CHUNK_SECONDS_MIN<=chunk_seconds<=CHUNK_SECONDS_MAX: raise PromptPlanError(f"prompt-plan chunk_seconds must be between {CHUNK_SECONDS_MIN:.1f} and {CHUNK_SECONDS_MAX:.1f}")
|
||||
if not isinstance(prompts,list) or len(prompts)!=chunks: raise PromptPlanError("prompt-plan prompt count does not match chunks")
|
||||
if not isinstance(hashes,list) or len(hashes)!=chunks: raise PromptPlanError("prompt-plan hash count does not match chunks")
|
||||
for index,prompt in enumerate(prompts):
|
||||
_normalize_prompt(prompt,label=f"prompt {index+1}")
|
||||
if hashes[index]!=prompt_hash(prompt): raise PromptPlanError(f"prompt-plan hash mismatch at chunk {index+1}")
|
||||
diagnostics=plan.get("diagnostics")
|
||||
if diagnostics is not None and (not isinstance(diagnostics,list) or not all(isinstance(item,dict) for item in diagnostics)): raise PromptPlanError("prompt-plan diagnostics are invalid")
|
||||
return plan
|
||||
def build_sampler_prompt_plan(*,prompt_mode,prompt_script,sequence_prompt,prompt_plan,chunks,chunk_seconds):
|
||||
chunks=int(chunks); chunk_seconds=float(chunk_seconds)
|
||||
if sequence_prompt is not None: return make_prompt_plan(mode=prompt_mode,script=sequence_prompt,chunks=chunks,chunk_seconds=chunk_seconds)
|
||||
if prompt_plan is not None:
|
||||
plan=validate_prompt_plan(prompt_plan)
|
||||
prompts=list(plan["prompts"])
|
||||
if len(prompts)<chunks: prompts.extend([prompts[-1] if prompts else ""]*(chunks-len(prompts)))
|
||||
elif len(prompts)>chunks: prompts=prompts[:chunks]
|
||||
result=dict(plan); result["chunks"]=chunks; result["chunk_seconds"]=chunk_seconds; result["prompts"]=prompts; result["hashes"]=[prompt_hash(p) for p in prompts]
|
||||
if int(plan["chunks"])!=chunks or abs(float(plan["chunk_seconds"])-chunk_seconds)>1e-6: result["notes"]=list(plan.get("notes") or [])+["adapted connected prompt plan to Sampler chunk settings"]
|
||||
return validate_prompt_plan(result)
|
||||
return make_prompt_plan(mode=prompt_mode,script=prompt_script,chunks=chunks,chunk_seconds=chunk_seconds)
|
||||
def apply_prompt_overrides(plan,overrides):
|
||||
plan=validate_prompt_plan(plan); prompts=list(plan["prompts"]); replaced=[]
|
||||
for index,value in enumerate(overrides[:int(plan["chunks"])]):
|
||||
if value is None: continue
|
||||
prompts[index]=_normalize_prompt(value,label=f"Clip {index+1} Prompt"); replaced.append(index+1)
|
||||
if not replaced: return plan
|
||||
result=dict(plan); result["mode"]=PROMPT_MODE_LIST; result["prompts"]=prompts; result["hashes"]=[prompt_hash(p) for p in prompts]; result["notes"]=list(plan.get("notes") or [])+["external Clip Prompt input(s): "+", ".join(map(str,replaced))]; result.pop("diagnostics",None)
|
||||
return validate_prompt_plan(result)
|
||||
def prompt_plan_report(plan):
|
||||
plan=validate_prompt_plan(plan); unique=len(set(plan["hashes"])); note="; ".join(plan.get("notes") or [])
|
||||
result=f"Prompt plan: {plan['mode']}; {plan['chunks']} chunks; {unique} unique prompt(s); {plan['chunk_seconds']:.3f}s per chunk."
|
||||
if note: result+=" "+note+"."
|
||||
diagnostics=list(plan.get("diagnostics") or [])
|
||||
if not diagnostics: return result+"\nPrompt Preflight: OK."
|
||||
warnings=[item for item in diagnostics if str(item.get("level","info")).lower()=="warning"]
|
||||
info=[item for item in diagnostics if str(item.get("level","info")).lower()!="warning"]
|
||||
lines=[]
|
||||
if warnings:
|
||||
lines.append("PROMPT PREFLIGHT WARNING")
|
||||
for item in warnings:
|
||||
code=str(item.get("code","H3C-P000")); message=str(item.get("message",""))
|
||||
lines.append(f"- WARNING {code}: {message}")
|
||||
lines.append("")
|
||||
lines.extend([result,"Prompt Preflight:"])
|
||||
for item in info:
|
||||
level=str(item.get("level","info")).upper(); code=str(item.get("code","H3C-P000")); message=str(item.get("message",""))
|
||||
lines.append(f"- {level} {code}: {message}")
|
||||
if not info: lines.append("- See warning(s) above.")
|
||||
return "\n".join(lines)
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
"""ComfyUI-native chunk sampling helpers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
import torch
|
||||
|
||||
from ..state import extract_av_streams
|
||||
|
||||
|
||||
class SamplingRuntimeError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
def _make_basic_guider(model: Any, conditioning: list):
|
||||
try:
|
||||
import comfy.samplers
|
||||
except Exception as exc: # pragma: no cover
|
||||
raise SamplingRuntimeError(f"ComfyUI sampler API unavailable: {exc}") from exc
|
||||
|
||||
class _BasicGuider(comfy.samplers.CFGGuider):
|
||||
def set_positive(self, positive):
|
||||
self.inner_set_conds({"positive": positive})
|
||||
|
||||
guider = _BasicGuider(model)
|
||||
guider.set_positive(conditioning)
|
||||
return guider
|
||||
|
||||
|
||||
def _prepare_noise(latent: dict[str, Any], seed: int):
|
||||
try:
|
||||
import comfy.sample
|
||||
except Exception as exc: # pragma: no cover
|
||||
raise SamplingRuntimeError(f"ComfyUI noise API unavailable: {exc}") from exc
|
||||
batch_inds = latent.get("batch_index")
|
||||
return comfy.sample.prepare_noise(latent["samples"], int(seed), batch_inds)
|
||||
|
||||
|
||||
def sample_chunk(
|
||||
*,
|
||||
model: Any,
|
||||
conditioning: list,
|
||||
latent: dict[str, Any],
|
||||
sampler: Any,
|
||||
sigmas: torch.Tensor,
|
||||
seed: int,
|
||||
enable_preview: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
"""Run one H3 chunk using the same path as SamplerCustomAdvanced."""
|
||||
|
||||
try:
|
||||
import comfy.model_management
|
||||
import comfy.sample
|
||||
import comfy.utils
|
||||
import latent_preview
|
||||
except Exception as exc: # pragma: no cover
|
||||
raise SamplingRuntimeError(f"ComfyUI sampling runtime unavailable: {exc}") from exc
|
||||
|
||||
if not isinstance(latent, dict) or "samples" not in latent:
|
||||
raise SamplingRuntimeError("latent must be a ComfyUI LATENT dictionary")
|
||||
if not torch.is_tensor(sigmas) or sigmas.ndim != 1 or sigmas.numel() < 2:
|
||||
raise SamplingRuntimeError("sigmas must contain at least two values")
|
||||
|
||||
working = latent.copy()
|
||||
latent_image = working["samples"]
|
||||
latent_image = comfy.sample.fix_empty_latent_channels(
|
||||
model,
|
||||
latent_image,
|
||||
working.get("downscale_ratio_spacial"),
|
||||
working.get("downscale_ratio_temporal"),
|
||||
)
|
||||
working["samples"] = latent_image
|
||||
noise_mask = working.get("noise_mask")
|
||||
guider = _make_basic_guider(model, conditioning)
|
||||
noise = _prepare_noise(working, seed)
|
||||
|
||||
x0_output: dict[str, Any] = {}
|
||||
callback = None
|
||||
if enable_preview:
|
||||
callback = latent_preview.prepare_callback(model, int(sigmas.shape[-1]) - 1, x0_output)
|
||||
disable_pbar = not comfy.utils.PROGRESS_BAR_ENABLED
|
||||
samples = guider.sample(
|
||||
noise,
|
||||
latent_image,
|
||||
sampler,
|
||||
sigmas,
|
||||
denoise_mask=noise_mask,
|
||||
callback=callback,
|
||||
disable_pbar=disable_pbar,
|
||||
seed=int(seed),
|
||||
)
|
||||
samples = samples.to(comfy.model_management.intermediate_device())
|
||||
output = working.copy()
|
||||
output.pop("downscale_ratio_spacial", None)
|
||||
output.pop("downscale_ratio_temporal", None)
|
||||
output["samples"] = samples
|
||||
# Validate the nested AV shape immediately so a bad sampler output cannot be
|
||||
# committed into a session and fail much later during continuation.
|
||||
extract_av_streams(output)
|
||||
return output
|
||||
|
||||
|
||||
def latent_to_cpu(latent: dict[str, Any]) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
video, audio = extract_av_streams(latent)
|
||||
video_cpu = video.detach().to("cpu").contiguous().clone()
|
||||
audio_cpu = audio.detach().to("cpu").contiguous().clone()
|
||||
if not bool(torch.isfinite(video_cpu.float()).all().item()):
|
||||
raise SamplingRuntimeError("sampled video latent contains NaN or Inf")
|
||||
if not bool(torch.isfinite(audio_cpu.float()).all().item()):
|
||||
raise SamplingRuntimeError("sampled audio latent contains NaN or Inf")
|
||||
return video_cpu, audio_cpu
|
||||
|
||||
|
||||
def latent_from_cpu(video: torch.Tensor, audio: torch.Tensor) -> dict[str, Any]:
|
||||
try:
|
||||
import comfy.nested_tensor
|
||||
except Exception as exc: # pragma: no cover
|
||||
raise SamplingRuntimeError(f"ComfyUI NestedTensor API unavailable: {exc}") from exc
|
||||
return {
|
||||
"samples": comfy.nested_tensor.NestedTensor(
|
||||
(video.contiguous(), audio.contiguous())
|
||||
)
|
||||
}
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
"""Conservative CPU-only decoded seam correction for H3 Continuum V2.1."""
|
||||
from __future__ import annotations
|
||||
import math
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
from .seam_types import AudioSeamMetrics, SeamDecision, VideoSeamMetrics
|
||||
COMPARE_SIZE=96; MAX_CUT_REWIND=3; MIN_RELATIVE_IMPROVEMENT=0.10; MIN_ABSOLUTE_IMPROVEMENT=0.003; SAFE_SCORE_LIMIT=0.35
|
||||
AUTO_MIN_RELATIVE_IMPROVEMENT=0.20; AUTO_WINDOW_FRAMES=5; AUTO_MIN_WINDOW_RELATIVE_IMPROVEMENT=0.05; AUTO_MIN_WINDOW_ABSOLUTE_IMPROVEMENT=0.0005
|
||||
AUTO_COMPONENT_RELATIVE_TOLERANCE=0.01; AUTO_COMPONENT_ABSOLUTE_TOLERANCE=0.0001
|
||||
WEIGHT_PIXEL=0.35; WEIGHT_LUMA=0.20; WEIGHT_CHROMA=0.10; WEIGHT_EDGE=0.15; WEIGHT_TEMPORAL=0.20
|
||||
|
||||
def _video_shape(frames,label):
|
||||
if not torch.is_tensor(frames) or frames.ndim!=4 or frames.shape[-1]<3: raise ValueError(f"{label} must be an IMAGE tensor [T,H,W,C]")
|
||||
def _downsample_rgb(frames):
|
||||
_video_shape(frames,"video seam frames"); rgb=frames[...,:3].detach().to(device="cpu",dtype=torch.float32).permute(0,3,1,2)
|
||||
if tuple(rgb.shape[-2:])!=(COMPARE_SIZE,COMPARE_SIZE): rgb=F.interpolate(rgb,size=(COMPARE_SIZE,COMPARE_SIZE),mode="bilinear",align_corners=False)
|
||||
rgb=rgb.permute(0,2,3,1).clamp(0.0,1.0)
|
||||
if not bool(torch.isfinite(rgb).all().item()): raise ValueError("video seam comparison contains NaN or Inf")
|
||||
return rgb
|
||||
def _luma(frame): return torch.sum(frame*frame.new_tensor((0.2126,0.7152,0.0722)),dim=-1)
|
||||
def _boundary_metrics(previous_images,next_images):
|
||||
if previous_images.shape[0]<2 or next_images.shape[0]<2: raise ValueError("video seam scoring needs two frames on each side")
|
||||
prev=_downsample_rgb(previous_images[-2:]); nxt=_downsample_rgb(next_images[:2]); prev_before,prev_edge=prev[0],prev[1]; next_edge,next_after=nxt[0],nxt[1]
|
||||
pixel=torch.mean(torch.abs(prev_edge-next_edge)).clamp(0,1); prev_y=_luma(prev_edge); next_y=_luma(next_edge); luma=torch.mean(torch.abs(prev_y-next_y)).clamp(0,1)
|
||||
prev_chroma=prev_edge-prev_y.unsqueeze(-1); next_chroma=next_edge-next_y.unsqueeze(-1); chroma=(torch.mean(torch.abs(prev_chroma-next_chroma))/2.0).clamp(0,1)
|
||||
prev_gx=prev_y[:,1:]-prev_y[:,:-1]; next_gx=next_y[:,1:]-next_y[:,:-1]; prev_gy=prev_y[1:,:]-prev_y[:-1,:]; next_gy=next_y[1:,:]-next_y[:-1,:]
|
||||
edge=((torch.mean(torch.abs(prev_gx-next_gx))+torch.mean(torch.abs(prev_gy-next_gy)))/4.0).clamp(0,1)
|
||||
prev_delta=prev_edge-prev_before; next_delta=next_after-next_edge; temporal=(torch.mean(torch.abs(prev_delta-next_delta))/2.0).clamp(0,1)
|
||||
motion=max(float(torch.mean(torch.abs(prev_delta)).item()),float(torch.mean(torch.abs(next_delta)).item()))
|
||||
score=WEIGHT_PIXEL*float(pixel.item())+WEIGHT_LUMA*float(luma.item())+WEIGHT_CHROMA*float(chroma.item())+WEIGHT_EDGE*float(edge.item())+WEIGHT_TEMPORAL*float(temporal.item())
|
||||
if not math.isfinite(score): raise ValueError("video seam score is not finite")
|
||||
return VideoSeamMetrics(float(pixel.item()),float(luma.item()),float(chroma.item()),float(edge.item()),float(temporal.item()),motion,max(0.0,min(1.0,score)))
|
||||
def analyze_video_seam(previous_images,next_raw_images,*,context_frames,rewind_frames=0):
|
||||
context_frames=int(context_frames); rewind_frames=int(rewind_frames); cut_index=context_frames-rewind_frames; previous_stop=int(previous_images.shape[0])-rewind_frames
|
||||
if rewind_frames<0 or rewind_frames>MAX_CUT_REWIND: raise ValueError("rewind_frames is outside the V2.1 safe range")
|
||||
if cut_index<0 or previous_stop<2 or cut_index+2>next_raw_images.shape[0]: raise ValueError("insufficient frames for the requested adaptive cut")
|
||||
return _boundary_metrics(previous_images[previous_stop-2:previous_stop],next_raw_images[cut_index:cut_index+2])
|
||||
def choose_adaptive_cut(previous_images,next_raw_images,*,context_frames):
|
||||
legacy=analyze_video_seam(previous_images,next_raw_images,context_frames=context_frames,rewind_frames=0); best_rewind=0; best=legacy
|
||||
maximum=min(MAX_CUT_REWIND,max(0,int(context_frames)),max(0,int(previous_images.shape[0])-2))
|
||||
for rewind in range(1,maximum+1):
|
||||
try: candidate=analyze_video_seam(previous_images,next_raw_images,context_frames=context_frames,rewind_frames=rewind)
|
||||
except ValueError: continue
|
||||
if candidate.score<best.score: best_rewind,best=rewind,candidate
|
||||
absolute=legacy.score-best.score; relative=absolute/max(legacy.score,1e-12)
|
||||
accepted=best_rewind>0 and relative>=MIN_RELATIVE_IMPROVEMENT and absolute>=MIN_ABSOLUTE_IMPROVEMENT and best.score<=SAFE_SCORE_LIMIT
|
||||
return (best_rewind,legacy,best) if accepted else (0,legacy,legacy)
|
||||
def apply_color_guard(previous_images,current_images):
|
||||
before=_boundary_metrics(previous_images[-2:],current_images[:2]); count=min(4,int(current_images.shape[0]))
|
||||
if count<2: return current_images,1.0,(0.0,0.0,0.0),before
|
||||
prev_rgb=_downsample_rgb(previous_images[-2:]); current_rgb=_downsample_rgb(current_images[:2]); prev_luma=float(_luma(prev_rgb).mean().item()); current_luma=float(_luma(current_rgb).mean().item()); requested_gain=prev_luma/max(current_luma,1e-6)
|
||||
if not 0.85<=requested_gain<=1.15: return current_images,1.0,(0.0,0.0,0.0),before
|
||||
gain=max(0.94,min(1.06,requested_gain)); prev_y=_luma(prev_rgb).unsqueeze(-1); current_y=_luma(current_rgb).unsqueeze(-1); requested_bias=(prev_rgb-prev_y).mean(dim=(0,1,2))-(current_rgb-current_y).mean(dim=(0,1,2))
|
||||
if float(torch.max(torch.abs(requested_bias)).item())>0.08: return current_images,1.0,(0.0,0.0,0.0),before
|
||||
bias=requested_bias.clamp(-0.02,0.02); result=current_images.clone().contiguous(); original=result[:count].clone(); fade=result.new_tensor((1.0,0.65,0.30,0.0))[:count]; luma_weights=result.new_tensor((0.2126,0.7152,0.0722))
|
||||
for index in range(count):
|
||||
weight=fade[index]; rgb=result[index,...,:3]; y=torch.sum(rgb*luma_weights,dim=-1,keepdim=True); chroma=rgb-y; frame_gain=1.0+(gain-1.0)*weight; frame_bias=bias.to(dtype=rgb.dtype)*weight; result[index,...,:3]=(y*frame_gain+chroma+frame_bias).clamp(0.0,1.0)
|
||||
after=_boundary_metrics(previous_images[-2:],result[:2])
|
||||
if after.score>before.score*1.01: result[:count].copy_(original); return result,1.0,(0.0,0.0,0.0),before
|
||||
return result,gain,tuple(float(v) for v in bias.tolist()),after
|
||||
def choose_video_blend(metrics):
|
||||
if metrics.motion<=0.015: return 2
|
||||
if metrics.motion<=0.040: return 1
|
||||
return 0
|
||||
def apply_video_blend(previous_frames,aligned_next_frames,*,blend_frames):
|
||||
blend_frames=min(int(blend_frames),int(previous_frames.shape[0]),int(aligned_next_frames.shape[0]),2)
|
||||
if blend_frames<=0: return previous_frames[:0].clone()
|
||||
previous=previous_frames[-blend_frames:].clone(); current=aligned_next_frames[-blend_frames:].to(dtype=previous.dtype)
|
||||
for index in range(blend_frames):
|
||||
progress=float(index+1)/float(blend_frames+1); alpha=0.5-0.5*math.cos(math.pi*progress); previous[index].mul_(1.0-alpha).add_(current[index],alpha=alpha)
|
||||
return previous
|
||||
def _seam_window_peak(previous_images,next_images,*,window_frames=AUTO_WINDOW_FRAMES):
|
||||
if previous_images.shape[0]<1 or next_images.shape[0]<2: raise ValueError("video seam window scoring needs one previous and two next frames")
|
||||
count=min(max(2,int(window_frames)),int(next_images.shape[0])); sequence=torch.cat((previous_images[-1:],next_images[:count]),dim=0); rgb=_downsample_rgb(sequence)
|
||||
pixel_delta=torch.mean(torch.abs(rgb[1:]-rgb[:-1]),dim=(1,2,3)); mean_luma=_luma(rgb).mean(dim=(1,2)); luma_delta=torch.abs(mean_luma[1:]-mean_luma[:-1]); combined=0.75*pixel_delta+0.25*luma_delta
|
||||
peak=float(torch.max(combined).item())
|
||||
if not math.isfinite(peak): raise ValueError("video seam window score is not finite")
|
||||
return max(0.0,min(1.0,peak))
|
||||
def _auto_rejection_reason(legacy,candidate,*,improvement,absolute_improvement,native_window_peak,candidate_window_peak):
|
||||
if candidate.score>SAFE_SCORE_LIMIT: return "Auto kept native video; candidate score exceeded the safe limit"
|
||||
if improvement<AUTO_MIN_RELATIVE_IMPROVEMENT or absolute_improvement<MIN_ABSOLUTE_IMPROVEMENT: return "Auto kept native video; candidate improvement was below the conservative threshold"
|
||||
for label in ("pixel_mae_norm","luma_norm","chroma_norm","edge_norm","temporal_norm","motion"):
|
||||
before=float(getattr(legacy,label)); after=float(getattr(candidate,label)); limit=before*(1.0+AUTO_COMPONENT_RELATIVE_TOLERANCE)+AUTO_COMPONENT_ABSOLUTE_TOLERANCE
|
||||
if after>limit: return f"Auto kept native video; {label} regressed"
|
||||
window_absolute=native_window_peak-candidate_window_peak; window_relative=window_absolute/max(native_window_peak,1e-12)
|
||||
if window_relative<AUTO_MIN_WINDOW_RELATIVE_IMPROVEMENT or window_absolute<AUTO_MIN_WINDOW_ABSOLUTE_IMPROVEMENT: return "Auto kept native video; the five-frame transition peak did not clearly improve"
|
||||
return None
|
||||
def correct_seam(previous_images,next_raw_images,*,context_frames,automatic=False):
|
||||
native_current=next_raw_images[int(context_frames):]; native_window_peak=_seam_window_peak(previous_images,native_current); rewind,legacy,selected=choose_adaptive_cut(previous_images,next_raw_images,context_frames=context_frames); cut_index=int(context_frames)-rewind; previous_stop=int(previous_images.shape[0])-rewind
|
||||
if previous_stop<2 or cut_index<0: raise ValueError("adaptive cut would exhaust a seam side")
|
||||
current=next_raw_images[cut_index:].clone().contiguous(); kept_previous=previous_images[:previous_stop]; current,gain,bias,corrected=apply_color_guard(kept_previous,current)
|
||||
blend_frames=0 if automatic else min(choose_video_blend(selected),cut_index,previous_stop); video_patch=None; score_previous=kept_previous[-2:].clone()
|
||||
if blend_frames>0:
|
||||
video_patch=apply_video_blend(kept_previous[-blend_frames:],next_raw_images[cut_index-blend_frames:cut_index],blend_frames=blend_frames); replaced=min(blend_frames,2); score_previous[-replaced:].copy_(video_patch[-replaced:]); corrected=_boundary_metrics(score_previous,current[:2])
|
||||
candidate_window_peak=_seam_window_peak(score_previous,current); improvement=(legacy.score-corrected.score)/max(legacy.score,1e-12); absolute_improvement=legacy.score-corrected.score
|
||||
if corrected.score>legacy.score*1.01:
|
||||
return native_current.clone().contiguous(),SeamDecision(legacy_score=legacy.score,cut_score=selected.score,candidate_score=corrected.score,corrected_score=legacy.score,native_window_peak=native_window_peak,candidate_window_peak=candidate_window_peak,fallback_reason="corrected score was worse than native video"),None
|
||||
if automatic:
|
||||
fallback_reason=_auto_rejection_reason(legacy,corrected,improvement=improvement,absolute_improvement=absolute_improvement,native_window_peak=native_window_peak,candidate_window_peak=candidate_window_peak)
|
||||
if fallback_reason is not None:
|
||||
return native_current.clone().contiguous(),SeamDecision(legacy_score=legacy.score,cut_score=selected.score,candidate_score=corrected.score,corrected_score=legacy.score,improvement=improvement,native_window_peak=native_window_peak,candidate_window_peak=candidate_window_peak,fallback_reason=fallback_reason),None
|
||||
return current,SeamDecision(cut_rewind_frames=rewind,legacy_score=legacy.score,cut_score=selected.score,candidate_score=corrected.score,corrected_score=corrected.score,improvement=improvement,native_window_peak=native_window_peak,candidate_window_peak=candidate_window_peak,video_blend_frames=blend_frames,luma_gain=gain,chroma_bias=bias),video_patch
|
||||
|
||||
def _audio_mid(waveform):
|
||||
if not torch.is_tensor(waveform) or waveform.ndim!=3: raise ValueError("audio seam waveform must be [B,C,S]")
|
||||
value=waveform.detach().to(device="cpu",dtype=torch.float32)
|
||||
if not bool(torch.isfinite(value).all().item()): raise ValueError("audio seam waveform contains NaN or Inf")
|
||||
return value.mean(dim=(0,1))
|
||||
def _correlation(a,b):
|
||||
length=min(int(a.numel()),int(b.numel()))
|
||||
if length<8: return 0.0
|
||||
a=a[:length]-a[:length].mean(); b=b[:length]-b[:length].mean(); denominator=torch.sqrt(torch.sum(a.square())*torch.sum(b.square())).clamp_min(1e-12); value=float((torch.sum(a*b)/denominator).item()); return max(-1.0,min(1.0,value)) if math.isfinite(value) else 0.0
|
||||
def analyze_audio_seam(previous_waveform,next_raw_waveform,*,sample_rate,cut_sample):
|
||||
sample_rate=int(sample_rate); cut_sample=int(cut_sample); previous=_audio_mid(previous_waveform); current=_audio_mid(next_raw_waveform); max_lag=max(1,int(round(sample_rate*0.020))); window=min(max(8,int(round(sample_rate*0.080))),int(previous.shape[-1]),max(0,cut_sample-max_lag))
|
||||
if sample_rate<=0 or window<8: raise ValueError("insufficient audio for seam alignment")
|
||||
previous_tail=previous[-window:]
|
||||
def correlation_at(lag):
|
||||
stop=cut_sample+int(lag); start=stop-window
|
||||
if start<0 or stop>current.shape[-1]: return None
|
||||
return _correlation(previous_tail,current[start:stop])
|
||||
baseline=correlation_at(0)
|
||||
if baseline is None: raise ValueError("audio seam cut is outside the decoded waveform")
|
||||
best_lag,best_correlation=0,baseline
|
||||
for lag in range(-max_lag,max_lag+1):
|
||||
corr=correlation_at(lag)
|
||||
if corr is not None and corr>best_correlation: best_lag,best_correlation=lag,corr
|
||||
if best_correlation<0.20 or best_correlation-baseline<0.02: best_lag,best_correlation=0,baseline
|
||||
else:
|
||||
center=max(0,min(int(current.shape[-1])-1,cut_sample+best_lag)); radius=max(1,int(round(sample_rate*0.001))); start=max(0,center-radius); stop=min(int(current.shape[-1]),center+radius+1)
|
||||
if stop>start: best_lag=int(torch.argmin(torch.abs(current[start:stop])).item())+start-cut_sample
|
||||
before_jump=float(torch.mean(torch.abs(previous_waveform[...,-1]-next_raw_waveform[...,cut_sample])).item())
|
||||
return AudioSeamMetrics(baseline,best_correlation,before_jump,before_jump,int(best_lag))
|
||||
def _audio_crossfade_ms(previous_waveform,next_raw_waveform,*,cut_sample,sample_rate):
|
||||
probe=max(8,int(round(sample_rate*0.020)))
|
||||
if previous_waveform.shape[-1]<probe or cut_sample<probe: return 10
|
||||
previous=_audio_mid(previous_waveform)[-probe:]; current=_audio_mid(next_raw_waveform)[cut_sample-probe:cut_sample]; rms=torch.sqrt(torch.mean(torch.cat((previous,current)).square())).clamp_min(1e-6); transient=max(float(torch.mean(torch.abs(torch.diff(previous))).item()),float(torch.mean(torch.abs(torch.diff(current))).item()))/float(rms.item())
|
||||
if transient>0.80:return 10
|
||||
if transient>0.35:return 20
|
||||
if transient>0.15:return 40
|
||||
return 60
|
||||
def apply_audio_seam(previous_waveform,next_raw_waveform,metrics,*,sample_rate,cut_sample):
|
||||
if metrics.correlation_after<0.20:return None,metrics,0,1.0,0.0
|
||||
fade_ms=_audio_crossfade_ms(previous_waveform,next_raw_waveform,cut_sample=cut_sample,sample_rate=sample_rate); fade_samples=min(int(round(sample_rate*fade_ms/1000.0)),int(previous_waveform.shape[-1]),int(cut_sample))
|
||||
if fade_samples<2:return None,metrics,0,1.0,0.0
|
||||
progress=torch.linspace(0.0,1.0,fade_samples,dtype=torch.float32,device="cpu"); base_indices=torch.arange(cut_sample-fade_samples,cut_sample,dtype=torch.long); offsets=torch.round(float(metrics.offset_samples)*(1.0-progress)).to(dtype=torch.long); indices=(base_indices+offsets).clamp(0,int(next_raw_waveform.shape[-1])-1)
|
||||
current=next_raw_waveform.detach().to("cpu").index_select(-1,indices); previous=previous_waveform[...,-fade_samples:].detach().to("cpu"); previous_rms=torch.sqrt(torch.mean(previous.float().square())).clamp_min(1e-6); current_rms=torch.sqrt(torch.mean(current.float().square())).clamp_min(1e-6); requested_db=20.0*math.log10(float((previous_rms/current_rms).item())); level_db=max(-1.5,min(1.5,requested_db)) if abs(requested_db)<=6.0 else 0.0; level_gain=10.0**(level_db/20.0); requested_dc=float((previous.float().mean()-current.float().mean()).item()); dc_bias=max(-0.02,min(0.02,requested_dc)) if abs(requested_dc)<=0.10 else 0.0; current=current*level_gain+dc_bias
|
||||
shape=[1]*(previous.ndim-1)+[fade_samples]; fade_in=torch.sin(progress*(math.pi/2.0)).reshape(shape); fade_out=torch.cos(progress*(math.pi/2.0)).reshape(shape); patch=previous*fade_out+current.to(dtype=previous.dtype)*fade_in; input_peak=max(float(previous.abs().max().item()),float(current.abs().max().item()),1e-6); patch_peak=float(patch.abs().max().item())
|
||||
if patch_peak>input_peak*1.05: patch=patch*((input_peak*1.05)/patch_peak)
|
||||
after_jump=float(torch.mean(torch.abs(patch[...,-1]-next_raw_waveform[...,cut_sample])).item()); updated=AudioSeamMetrics(metrics.correlation_before,metrics.correlation_after,metrics.boundary_jump_before,after_jump,metrics.offset_samples); return patch.contiguous(),updated,fade_samples,level_gain,dc_bias
|
||||
def correct_audio_seam(previous_waveform,next_raw_waveform,*,sample_rate,cut_sample):
|
||||
metrics=analyze_audio_seam(previous_waveform,next_raw_waveform,sample_rate=sample_rate,cut_sample=cut_sample); return apply_audio_seam(previous_waveform,next_raw_waveform,metrics,sample_rate=sample_rate,cut_sample=cut_sample)
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
"""Transient types used by V2.1 decoded seam correction."""
|
||||
from __future__ import annotations
|
||||
from dataclasses import dataclass
|
||||
@dataclass(frozen=True)
|
||||
class VideoSeamMetrics:
|
||||
pixel_mae_norm: float
|
||||
luma_norm: float
|
||||
chroma_norm: float
|
||||
edge_norm: float
|
||||
temporal_norm: float
|
||||
motion: float
|
||||
score: float
|
||||
@dataclass(frozen=True)
|
||||
class AudioSeamMetrics:
|
||||
correlation_before: float = 0.0
|
||||
correlation_after: float = 0.0
|
||||
boundary_jump_before: float = 0.0
|
||||
boundary_jump_after: float = 0.0
|
||||
offset_samples: int = 0
|
||||
@dataclass
|
||||
class SeamDecision:
|
||||
cut_rewind_frames: int = 0
|
||||
legacy_score: float = 0.0
|
||||
cut_score: float = 0.0
|
||||
candidate_score: float = 0.0
|
||||
corrected_score: float = 0.0
|
||||
improvement: float = 0.0
|
||||
native_window_peak: float = 0.0
|
||||
candidate_window_peak: float = 0.0
|
||||
video_blend_frames: int = 0
|
||||
luma_gain: float = 1.0
|
||||
chroma_bias: tuple[float,float,float] = (0.0,0.0,0.0)
|
||||
audio_metrics: AudioSeamMetrics = AudioSeamMetrics()
|
||||
audio_crossfade_samples: int = 0
|
||||
audio_level_gain: float = 1.0
|
||||
audio_dc_bias: float = 0.0
|
||||
fallback_reason: str | None = None
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
"""Deterministic per-chunk seed derivation."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
_MASK64 = (1 << 64) - 1
|
||||
_GOLDEN = 0x9E3779B97F4A7C15
|
||||
_MIX_A = 0xBF58476D1CE4E5B9
|
||||
_MIX_B = 0x94D049BB133111EB
|
||||
|
||||
|
||||
def _splitmix64(value: int) -> int:
|
||||
value = (value + _GOLDEN) & _MASK64
|
||||
value = ((value ^ (value >> 30)) * _MIX_A) & _MASK64
|
||||
value = ((value ^ (value >> 27)) * _MIX_B) & _MASK64
|
||||
return (value ^ (value >> 31)) & _MASK64
|
||||
|
||||
|
||||
def derive_chunk_seed(base_seed: int, chunk_index: int, reroll_nonce: int = 0) -> int:
|
||||
"""Return a stable 64-bit seed for a zero-based chunk index.
|
||||
|
||||
The index and reroll nonce are mixed independently so rerolling chunk N does
|
||||
not alter accepted chunks before N.
|
||||
"""
|
||||
|
||||
if isinstance(base_seed, bool) or isinstance(chunk_index, bool) or isinstance(reroll_nonce, bool):
|
||||
raise TypeError("seed inputs must be integers, not booleans")
|
||||
chunk_index = int(chunk_index)
|
||||
reroll_nonce = int(reroll_nonce)
|
||||
if chunk_index < 0:
|
||||
raise ValueError("chunk_index must be non-negative")
|
||||
if reroll_nonce < 0:
|
||||
raise ValueError("reroll_nonce must be non-negative")
|
||||
value = int(base_seed) & _MASK64
|
||||
value ^= _splitmix64(chunk_index + 1)
|
||||
value ^= _splitmix64((reroll_nonce + 1) * 0xD6E8FEB86659FD93)
|
||||
return _splitmix64(value)
|
||||
+1806
File diff suppressed because it is too large
Load Diff
+265
@@ -0,0 +1,265 @@
|
||||
"""In-memory session model for accepted chunks, resume, and branch-safe rerolls."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import hashlib
|
||||
import json
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
import torch
|
||||
|
||||
from ..constants import SESSION_MAGIC, STATE_MAGIC
|
||||
from ..state import validate_plan
|
||||
from ..temporal import (
|
||||
audio_grid_offset,
|
||||
audio_latent_t,
|
||||
context_slots,
|
||||
is_valid_frame_count,
|
||||
pixel_frames_for_latent_t,
|
||||
video_latent_t,
|
||||
)
|
||||
from ..version import PACKAGE_VERSION, SESSION_SCHEMA_VERSION, STATE_SCHEMA_VERSION
|
||||
from .sampling import latent_from_cpu, latent_to_cpu
|
||||
|
||||
|
||||
class SessionValidationError(ValueError):
|
||||
pass
|
||||
|
||||
|
||||
def _now_iso() -> str:
|
||||
return datetime.now(timezone.utc).replace(microsecond=0).isoformat()
|
||||
|
||||
|
||||
def model_fingerprint(
|
||||
model: Any, *, extra_wrapper_keys: tuple[str, ...] = ()
|
||||
) -> str:
|
||||
base = getattr(model, "model", None)
|
||||
inner = getattr(base, "diffusion_model", None)
|
||||
options = getattr(model, "model_options", {}) or {}
|
||||
transformer = options.get("transformer_options", {}) or {}
|
||||
wrappers = getattr(model, "wrappers", {}) or {}
|
||||
wrapper_keys: list[str] = []
|
||||
if isinstance(wrappers, dict):
|
||||
for group in wrappers.values():
|
||||
if isinstance(group, dict):
|
||||
wrapper_keys.extend(str(key) for key in group)
|
||||
if extra_wrapper_keys:
|
||||
wrapper_keys = sorted(set(wrapper_keys).union(map(str, extra_wrapper_keys)))
|
||||
descriptor = {
|
||||
"base": f"{type(base).__module__}.{type(base).__name__}" if base is not None else "missing",
|
||||
"inner": f"{type(inner).__module__}.{type(inner).__name__}" if inner is not None else "missing",
|
||||
"dtype": str(getattr(model, "model_dtype", lambda: "unknown")()),
|
||||
"model_size": int(getattr(model, "model_size", lambda: 0)() or 0),
|
||||
"wrappers": sorted(wrapper_keys),
|
||||
"has_attention_override": transformer.get("optimized_attention_override") is not None,
|
||||
"has_sol_compose": transformer.get("sol_compose") is not None,
|
||||
}
|
||||
raw = json.dumps(descriptor, sort_keys=True, separators=(",", ":"))
|
||||
return hashlib.sha256(raw.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def make_chunk_entry(
|
||||
*,
|
||||
latent: dict[str, Any],
|
||||
plan: dict[str, Any],
|
||||
prompt: str,
|
||||
prompt_hash: str,
|
||||
seed: int,
|
||||
context_frames: int,
|
||||
motion_score: float,
|
||||
reused: bool,
|
||||
) -> dict[str, Any]:
|
||||
plan = validate_plan(plan)
|
||||
video, audio = latent_to_cpu(latent)
|
||||
actual_frames = pixel_frames_for_latent_t(int(video.shape[2]))
|
||||
if actual_frames != int(plan["total_frames"]):
|
||||
raise SessionValidationError(
|
||||
f"chunk latent represents {actual_frames} frames but plan declares {plan['total_frames']}"
|
||||
)
|
||||
return {
|
||||
"sequence_index": 0, # populated by make_session
|
||||
"clip_index": int(plan["clip_index"]),
|
||||
"prompt": str(prompt),
|
||||
"prompt_hash": str(prompt_hash),
|
||||
"seed": int(seed),
|
||||
"context_frames": int(context_frames),
|
||||
"motion_score": float(motion_score),
|
||||
"reused": bool(reused),
|
||||
"plan": copy.deepcopy(plan),
|
||||
"video": video,
|
||||
"audio": audio,
|
||||
}
|
||||
|
||||
|
||||
def validate_chunk_entry(entry: dict[str, Any]) -> dict[str, Any]:
|
||||
if not isinstance(entry, dict):
|
||||
raise SessionValidationError("session chunk must be a dictionary")
|
||||
video, audio = entry.get("video"), entry.get("audio")
|
||||
if not torch.is_tensor(video) or video.ndim != 5 or tuple(video.shape[:2]) != (1, 24):
|
||||
raise SessionValidationError("session chunk video must be [1,24,T,H,W]")
|
||||
if not torch.is_tensor(audio) or audio.ndim != 4 or tuple(audio.shape[:3]) != (1, 32, 2):
|
||||
raise SessionValidationError("session chunk audio must be [1,32,2,T]")
|
||||
if video.device.type != "cpu" or audio.device.type != "cpu":
|
||||
raise SessionValidationError("session tensors must remain on CPU")
|
||||
if not bool(torch.isfinite(video.float()).all().item()):
|
||||
raise SessionValidationError("session video contains NaN or Inf")
|
||||
if not bool(torch.isfinite(audio.float()).all().item()):
|
||||
raise SessionValidationError("session audio contains NaN or Inf")
|
||||
plan = validate_plan(entry.get("plan"))
|
||||
actual_frames = pixel_frames_for_latent_t(int(video.shape[2]))
|
||||
if actual_frames != int(plan["total_frames"]):
|
||||
raise SessionValidationError("session chunk video length does not match its plan")
|
||||
if not isinstance(entry.get("prompt"), str):
|
||||
raise SessionValidationError("session chunk prompt is not a string")
|
||||
if not isinstance(entry.get("prompt_hash"), str) or len(entry["prompt_hash"]) != 64:
|
||||
raise SessionValidationError("session chunk prompt hash is invalid")
|
||||
if int(entry.get("seed", -1)) < 0:
|
||||
raise SessionValidationError("session chunk seed is invalid")
|
||||
if int(entry.get("context_frames", 0)) not in (0, 5, 22, 39):
|
||||
raise SessionValidationError("session chunk context_frames is invalid")
|
||||
return entry
|
||||
|
||||
|
||||
def make_session(
|
||||
*,
|
||||
chunks: list[dict[str, Any]],
|
||||
width: int,
|
||||
height: int,
|
||||
chunk_seconds: float,
|
||||
identity_hash: str,
|
||||
model_fingerprint_value: str,
|
||||
parent_session_id: str | None,
|
||||
reroll_from_chunk: int,
|
||||
settings: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
session_id = uuid.uuid4().hex
|
||||
normalized: list[dict[str, Any]] = []
|
||||
for index, entry in enumerate(chunks, start=1):
|
||||
item = dict(entry)
|
||||
item["plan"] = copy.deepcopy(entry["plan"])
|
||||
item["sequence_index"] = index
|
||||
validate_chunk_entry(item)
|
||||
normalized.append(item)
|
||||
return {
|
||||
"magic": SESSION_MAGIC,
|
||||
"schema_version": SESSION_SCHEMA_VERSION,
|
||||
"package_version": PACKAGE_VERSION,
|
||||
"session_id": session_id,
|
||||
"parent_session_id": parent_session_id,
|
||||
"created_utc": _now_iso(),
|
||||
"reroll_from_chunk": int(reroll_from_chunk),
|
||||
"width": int(width),
|
||||
"height": int(height),
|
||||
"chunk_seconds": float(chunk_seconds),
|
||||
"identity_hash": str(identity_hash),
|
||||
"model_fingerprint": str(model_fingerprint_value),
|
||||
"settings": copy.deepcopy(settings),
|
||||
"chunks": normalized,
|
||||
}
|
||||
|
||||
|
||||
def validate_session(session: dict[str, Any]) -> dict[str, Any]:
|
||||
if not isinstance(session, dict) or session.get("magic") != SESSION_MAGIC:
|
||||
raise SessionValidationError("invalid H3 Continuum session")
|
||||
if int(session.get("schema_version", -1)) != SESSION_SCHEMA_VERSION:
|
||||
raise SessionValidationError(
|
||||
f"unsupported session schema {session.get('schema_version')}; "
|
||||
f"expected {SESSION_SCHEMA_VERSION}"
|
||||
)
|
||||
if int(session.get("width", 0)) <= 0 or int(session.get("height", 0)) <= 0:
|
||||
raise SessionValidationError("session dimensions are invalid")
|
||||
if float(session.get("chunk_seconds", 0.0)) <= 0:
|
||||
raise SessionValidationError("session chunk_seconds is invalid")
|
||||
chunks = session.get("chunks")
|
||||
if not isinstance(chunks, list) or not chunks:
|
||||
raise SessionValidationError("session contains no chunks")
|
||||
previous_clip_index = None
|
||||
for index, entry in enumerate(chunks, start=1):
|
||||
validate_chunk_entry(entry)
|
||||
if int(entry.get("sequence_index", index)) != index:
|
||||
raise SessionValidationError("session chunk sequence indices are not contiguous")
|
||||
clip_index = int(entry["plan"]["clip_index"])
|
||||
if previous_clip_index is not None and clip_index != previous_clip_index + 1:
|
||||
raise SessionValidationError("session clip indices are not contiguous")
|
||||
previous_clip_index = clip_index
|
||||
video = entry["video"]
|
||||
if int(video.shape[-1]) * 16 != int(session["width"]):
|
||||
raise SessionValidationError("session width does not match chunk latent")
|
||||
if int(video.shape[-2]) * 16 != int(session["height"]):
|
||||
raise SessionValidationError("session height does not match chunk latent")
|
||||
return session
|
||||
|
||||
|
||||
def entry_to_latent(entry: dict[str, Any]) -> dict[str, Any]:
|
||||
entry = validate_chunk_entry(entry)
|
||||
return latent_from_cpu(entry["video"], entry["audio"])
|
||||
|
||||
|
||||
def entry_to_state(entry: dict[str, Any], *, capacity_frames: int | None = None) -> dict[str, Any]:
|
||||
"""Build a V1-compatible continuation state directly from a CPU chunk.
|
||||
|
||||
This deliberately avoids reconstructing a ComfyUI ``NestedTensor``. Session
|
||||
inspection, persistence tests, and branch selection therefore remain usable
|
||||
outside a live ComfyUI process, while the returned state is byte-for-byte
|
||||
compatible with the V1 state contract.
|
||||
"""
|
||||
|
||||
entry = validate_chunk_entry(entry)
|
||||
plan = entry["plan"]
|
||||
video = entry["video"]
|
||||
audio = entry["audio"]
|
||||
source_frames = int(plan["total_frames"])
|
||||
clip_index = int(plan["clip_index"])
|
||||
capacity = int(capacity_frames or plan["state_capacity_frames"])
|
||||
|
||||
actual_video_t = int(video.shape[2])
|
||||
actual_frames = pixel_frames_for_latent_t(actual_video_t)
|
||||
if actual_frames != source_frames:
|
||||
raise SessionValidationError(
|
||||
f"chunk plan says {source_frames} frames but latent represents {actual_frames}"
|
||||
)
|
||||
if not is_valid_frame_count(actual_frames) or video_latent_t(actual_frames) != actual_video_t:
|
||||
raise SessionValidationError(
|
||||
f"chunk video latent T={actual_video_t} is not on the native H3 temporal grid"
|
||||
)
|
||||
|
||||
slots = context_slots(capacity)
|
||||
audio_steps = audio_latent_t(capacity)
|
||||
if int(video.shape[2]) < slots or int(audio.shape[-1]) < audio_steps:
|
||||
raise SessionValidationError(
|
||||
f"chunk is too short for a {capacity}-frame continuation state"
|
||||
)
|
||||
grid_offset = audio_grid_offset(source_frames, int(audio.shape[-1]))
|
||||
if not (-0.500001 <= grid_offset <= 0.500001):
|
||||
raise SessionValidationError(
|
||||
f"unexpected signed audio-grid offset {grid_offset:.6f}"
|
||||
)
|
||||
|
||||
video_tail = video[:, :, -slots:].contiguous().clone()
|
||||
audio_tail = audio[..., -audio_steps:].contiguous().clone()
|
||||
return {
|
||||
"magic": STATE_MAGIC,
|
||||
"schema_version": STATE_SCHEMA_VERSION,
|
||||
"clip_index": clip_index,
|
||||
"source_frame_count": source_frames,
|
||||
"capacity_frames": capacity,
|
||||
"width": int(video.shape[-1]) * 16,
|
||||
"height": int(video.shape[-2]) * 16,
|
||||
"video_tail": video_tail,
|
||||
"audio_tail": audio_tail,
|
||||
"audio_overhang": float(grid_offset),
|
||||
"source_mode": "latent_direct",
|
||||
}
|
||||
|
||||
|
||||
def session_summary(session: dict[str, Any]) -> str:
|
||||
session = validate_session(session)
|
||||
reused = sum(1 for item in session["chunks"] if item.get("reused"))
|
||||
frames = sum(int(item["plan"]["net_frames"]) for item in session["chunks"])
|
||||
return (
|
||||
f"Session {session['session_id'][:8]}: {len(session['chunks'])} chunks, "
|
||||
f"{frames} retained frames, {reused} reused, {session['width']}x{session['height']}."
|
||||
)
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
"""Atomic safetensors + JSON persistence for V2 sessions."""
|
||||
from __future__ import annotations
|
||||
import json, logging, os, re, uuid
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from safetensors import safe_open
|
||||
from safetensors.torch import save_file
|
||||
from .session import validate_session
|
||||
LOG=logging.getLogger("h3_continuum_join")
|
||||
_SAFE_PREFIX=re.compile(r"[^A-Za-z0-9._-]+")
|
||||
_HEADER_KEY="h3_continuum_session_json"
|
||||
def sanitize_prefix(prefix:str)->str:
|
||||
value=_SAFE_PREFIX.sub("_",str(prefix).strip()).strip("._"); return value or "h3_continuum_session"
|
||||
def session_directory()->Path:
|
||||
try:
|
||||
import folder_paths; root=Path(folder_paths.get_output_directory())
|
||||
except Exception: root=Path.cwd()
|
||||
path=root/"h3_continuum_sessions"; path.mkdir(parents=True,exist_ok=True); return path
|
||||
def session_paths(prefix:str,slot:int)->tuple[Path,Path]:
|
||||
slot=int(slot)
|
||||
if not 1<=slot<=9999: raise ValueError("session slot must be between 1 and 9999")
|
||||
name=f"{sanitize_prefix(prefix)}_slot{slot:04d}"; root=session_directory(); return root/f"{name}.safetensors",root/f"{name}.json"
|
||||
def _fsync_file(path):
|
||||
try:
|
||||
with path.open("rb") as handle: os.fsync(handle.fileno())
|
||||
except OSError: pass
|
||||
def _fsync_directory(path):
|
||||
if os.name=="nt": return
|
||||
try:
|
||||
descriptor=os.open(str(path),os.O_RDONLY)
|
||||
try: os.fsync(descriptor)
|
||||
finally: os.close(descriptor)
|
||||
except OSError: pass
|
||||
def _metadata_without_tensors(session):
|
||||
metadata={k:v for k,v in session.items() if k!="chunks"}; chunks=[]
|
||||
for entry in session["chunks"]: chunks.append({k:v for k,v in entry.items() if k not in ("video","audio")})
|
||||
metadata["chunks"]=chunks; return metadata
|
||||
def save_session(session,*,prefix,slot):
|
||||
session=validate_session(session); tensor_path,json_path=session_paths(prefix,slot); token=uuid.uuid4().hex
|
||||
tensor_tmp=tensor_path.with_name(f".{tensor_path.name}.{token}.tmp"); json_tmp=json_path.with_name(f".{json_path.name}.{token}.tmp")
|
||||
tensors={}
|
||||
for index,entry in enumerate(session["chunks"],start=1):
|
||||
tensors[f"chunk_{index:04d}_video"]=entry["video"].detach().to("cpu").contiguous(); tensors[f"chunk_{index:04d}_audio"]=entry["audio"].detach().to("cpu").contiguous()
|
||||
metadata=_metadata_without_tensors(session); metadata["_session_commit_id"]=token
|
||||
serialized=json.dumps(metadata,ensure_ascii=False,sort_keys=True,separators=(",",":"))
|
||||
try:
|
||||
save_file(tensors,str(tensor_tmp),metadata={_HEADER_KEY:serialized}); _fsync_file(tensor_tmp)
|
||||
with json_tmp.open("w",encoding="utf-8",newline="\n") as handle:
|
||||
json.dump(metadata,handle,ensure_ascii=False,indent=2,sort_keys=True); handle.write("\n"); handle.flush(); os.fsync(handle.fileno())
|
||||
os.replace(json_tmp,json_path); os.replace(tensor_tmp,tensor_path); _fsync_directory(tensor_path.parent)
|
||||
finally:
|
||||
for path in (tensor_tmp,json_tmp):
|
||||
try: path.unlink(missing_ok=True)
|
||||
except OSError: pass
|
||||
return tensor_path,json_path
|
||||
def load_session(*,prefix,slot):
|
||||
tensor_path,json_path=session_paths(prefix,slot)
|
||||
if not tensor_path.is_file(): raise FileNotFoundError(f"session slot does not exist: {tensor_path.name}")
|
||||
with safe_open(str(tensor_path),framework="pt",device="cpu") as handle:
|
||||
serialized=(handle.metadata() or {}).get(_HEADER_KEY)
|
||||
if serialized is None: raise ValueError("session safetensors has no embedded Continuum metadata")
|
||||
session=json.loads(serialized)
|
||||
for index,entry in enumerate(session.get("chunks") or [],start=1):
|
||||
entry["video"]=handle.get_tensor(f"chunk_{index:04d}_video"); entry["audio"]=handle.get_tensor(f"chunk_{index:04d}_audio")
|
||||
if json_path.is_file():
|
||||
try:
|
||||
with json_path.open("r",encoding="utf-8") as handle: mirror=json.load(handle)
|
||||
if mirror.get("_session_commit_id")!=session.get("_session_commit_id"): LOG.warning("Ignoring stale Continuum session JSON mirror: %s",json_path.name)
|
||||
except (OSError,ValueError,TypeError) as exc: LOG.warning("Ignoring unreadable session JSON mirror %s: %s",json_path.name,exc)
|
||||
return validate_session(session)
|
||||
def session_mtime(*,prefix,slot):
|
||||
tensor_path,json_path=session_paths(prefix,slot); return (tensor_path.stat().st_mtime if tensor_path.exists() else -1.0,json_path.stat().st_mtime if json_path.exists() else -1.0)
|
||||
Reference in New Issue
Block a user