Support per-chunk timeline durations
tests / test (push) Canceled after 0s
tests / windows-storage (push) Canceled after 0s

This commit is contained in:
2026-11-10 11:46:19 +00:00
parent 3716b02a57
commit 92135c0db0
13 changed files with 153 additions and 58 deletions
+28 -17
View File
@@ -80,26 +80,35 @@ def _record_context_diagnostics(*,tracker,reports,state,continuity,reused):
except Exception as exc:
reports.append(f"context diagnostics source chunk {state.get('clip_index','?')}: unavailable ({exc})")
def _check_decode_memory_budget(*,width,height,chunks,chunk_seconds):
target_frames=max(1,int(round(chunks*chunk_seconds*FPS))); max_chunk_frames=max(5,int(round(chunk_seconds*FPS))+39)
def _check_decode_memory_budget(*,width,height,total_seconds,max_chunk_seconds):
target_frames=max(1,int(round(total_seconds*FPS))); max_chunk_frames=max(5,int(round(max_chunk_seconds*FPS))+39)
image_bytes=target_frames*width*height*3*4; transient_bytes=max_chunk_frames*width*height*3*4
required_bytes=image_bytes+int(transient_bytes*1.5)+1*1024**3; estimate_gib=required_bytes/1024**3; available_gib=None
try:
import psutil
available=int(psutil.virtual_memory().available); available_gib=available/1024**3
except ImportError: pass
return estimate_gib,available_gib
return estimate_gib,available_gib
def _plan_durations(plan: dict[str, Any]) -> list[float]:
chunks = int(plan["chunks"])
fallback = float(plan["chunk_seconds"])
values = plan.get("chunk_durations") or [fallback] * chunks
if len(values) != chunks:
raise SequenceRuntimeError("prompt plan duration count does not match chunks")
return [float(value) for value in values]
def _clone_entry_for_reuse(entry):
entry=validate_chunk_entry(entry); result=dict(entry); result["plan"]=copy.deepcopy(entry["plan"]); result["reused"]=True; return result
def _preserved_prefix(*,session,prompt_hashes,chunks,reroll_from_chunk,width,height,chunk_seconds,identity_hash,last_frame_hash,reference_storage_plan=None):
def _preserved_prefix(*,session,prompt_hashes,chunks,reroll_from_chunk,width,height,chunk_seconds,chunk_durations,identity_hash,last_frame_hash,reference_storage_plan=None):
if session is None: return [],[]
notes=[]
try: session=validate_session(session)
except SessionValidationError as exc: return [],[f"saved session was ignored; generated a fresh run ({exc})"]
if int(session["width"])!=int(width) or int(session["height"])!=int(height): return [],["saved session resolution differs; generated a fresh run"]
if abs(float(session["chunk_seconds"])-float(chunk_seconds))>1e-6: return [],["saved session chunk duration differs; generated a fresh run"]
saved_durations=session.get("chunk_durations") or [float(session["chunk_seconds"])]*len(session["chunks"])
if len(saved_durations)!=len(chunk_durations) or any(abs(float(a)-float(b))>1e-6 for a,b in zip(saved_durations,chunk_durations)): return [],["saved session chunk durations differ; generated a fresh run"]
if str(session.get("identity_hash","none"))!=str(identity_hash): return [],["saved session identity differs; generated a fresh run"]
routed_limit=None
if reference_storage_plan is not None:
@@ -526,8 +535,8 @@ def _run_runtime_internal(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampl
if memory_collector is not None:
from ..v3.memory_attribution import capture_attribution_fail_soft
capture_memory=capture_attribution_fail_soft
plan=validate_prompt_plan(prompt_plan); chunks=int(plan["chunks"]); chunk_seconds=float(plan["chunk_seconds"]); prompts=list(plan["prompts"]); prompt_hashes=list(plan["hashes"]); width,height=int(width),int(height)
visible_target_frames=int(round(chunks*chunk_seconds*FPS))
plan=validate_prompt_plan(prompt_plan); chunks=int(plan["chunks"]); chunk_seconds=float(plan["chunk_seconds"]); durations=_plan_durations(plan); prompts=list(plan["prompts"]); prompt_hashes=list(plan["hashes"]); width,height=int(width),int(height)
visible_target_frames=int(round(sum(durations)*FPS))
validate_guide_visible_range(guide_source,target_frames=visible_target_frames)
continuation_transport=str(continuation_transport)
if continuation_transport not in CONTINUATION_TRANSPORTS:
@@ -572,11 +581,11 @@ def _run_runtime_internal(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampl
)
timeline_video_warning=validate_timeline_video_prompts(prompts,timeline_video_source)
decode_estimate_gib=0.0; available_ram_gib=None
if not latent_only: decode_estimate_gib,available_ram_gib=_check_decode_memory_budget(width=width,height=height,chunks=chunks,chunk_seconds=chunk_seconds)
if not latent_only: decode_estimate_gib,available_ram_gib=_check_decode_memory_budget(width=width,height=height,total_seconds=sum(durations),max_chunk_seconds=max(durations))
issues=check_comfy_h3_runtime()
assets=prepare_identity_assets(video_vae,width=width,height=height,first_frame=first_frame,last_frame=last_frame,encode_latents=False)
multi_chunk_flf=bool(chunks>=2 and first_frame is not None and last_frame is not None)
terminal_merge_enabled=_terminal_flf_merge_enabled(multi_chunk_flf=multi_chunk_flf,chunks=chunks,chunk_seconds=chunk_seconds,prompt_hashes=prompt_hashes,timeline_video_source=timeline_video_source)
terminal_merge_enabled=_terminal_flf_merge_enabled(multi_chunk_flf=multi_chunk_flf,chunks=chunks,chunk_seconds=chunk_seconds,prompt_hashes=prompt_hashes,timeline_video_source=timeline_video_source) and len(set(durations)) == 1
if _reference_routing_runtime is not None:
try:
_reference_routing_runtime.check_physical_contract(
@@ -669,7 +678,7 @@ def _run_runtime_internal(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampl
except Exception as exc:
adaptive_observer_unavailable=f"{type(exc).__name__}: {exc}"
effective_reroll_from_chunk=0 if storage_controller is not None and session is not None and bool((session.get("settings") or {}).get("run_storage_validated_prefix")) else int(reroll_from_chunk)
preserved,reuse_notes=_preserved_prefix(session=session,prompt_hashes=prompt_hashes,chunks=chunks,reroll_from_chunk=effective_reroll_from_chunk,width=width,height=height,chunk_seconds=chunk_seconds,identity_hash=sequence_identity_hash,last_frame_hash=assets.last_frame_hash,reference_storage_plan=reference_storage_plan)
preserved,reuse_notes=_preserved_prefix(session=session,prompt_hashes=prompt_hashes,chunks=chunks,reroll_from_chunk=effective_reroll_from_chunk,width=width,height=height,chunk_seconds=chunk_seconds,chunk_durations=durations,identity_hash=sequence_identity_hash,last_frame_hash=assets.last_frame_hash,reference_storage_plan=reference_storage_plan)
if _terminal_strategy_mismatch(session,merge_enabled=terminal_merge_enabled):
preserved=[]
reuse_notes.append("FLF chunk contract changed; regenerated the requested FLF sequence.")
@@ -941,7 +950,7 @@ def _run_runtime_internal(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampl
capture_memory(memory_collector, "finish_phase", phase="conditioning_prompt_clip")
if memory_collector is not None:
capture_memory(memory_collector, "finish_phase", phase="conditioning")
initial_frame_count=align_frame_count_up(int(round(chunk_seconds*FPS))); retained_frames=sum(int(entry["plan"]["net_frames"]) for entry in entries); sampling_reports=[]
retained_frames=sum(int(entry["plan"]["net_frames"]) for entry in entries); sampling_reports=[]
context_diagnostics=ContextDiagnosticsTracker() if bool(debug) else None
if context_diagnostics is not None:
if entries:
@@ -982,6 +991,8 @@ def _run_runtime_internal(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampl
if memory_collector is not None:
capture_memory(memory_collector, "finish_phase", phase="preparation")
for sequence_index in normal_indices:
chunk_seconds_for_chunk=durations[sequence_index]
initial_frame_count=align_frame_count_up(int(round(chunk_seconds_for_chunk*FPS)))
reusing_group=sequence_index<reused_count
if memory_collector is not None:
capture_memory(memory_collector, "start_phase", phase="group_prepare", physical_group=sequence_index+1, logical_chunks=(sequence_index+1,))
@@ -1044,9 +1055,9 @@ def _run_runtime_internal(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampl
physical_group=sequence_index+1,
)
conditioning=attach_keyframes(chunk_cache[conditioning_key],frame_count=total_frames,first_latent=chunk_assets.first_latent,last_latent=chunk_assets.last_latent if include_chunk_last else None); clip_index=1; context_frames=0
chunk_plan=make_plan(continuation=False,clip_index=clip_index,total_frames=total_frames,trim_frames=0,width=width,height=height,context_frames=5,state_capacity_frames=largest_context_capacity(total_frames),requested_extend_seconds=chunk_seconds,debug=debug); reason="initial clip"
chunk_plan=make_plan(continuation=False,clip_index=clip_index,total_frames=total_frames,trim_frames=0,width=width,height=height,context_frames=5,state_capacity_frames=largest_context_capacity(total_frames),requested_extend_seconds=chunk_seconds_for_chunk,debug=debug); reason="initial clip"
else:
context_frames,motion_score,reason=choose_context_frames(continuity,previous_state); desired_cumulative=int(round((sequence_index+1)*chunk_seconds*FPS)); requested_new_frames=max(1,desired_cumulative-retained_frames); shape=make_extension_shape(context_frames,requested_new_frames/FPS); latent=empty_h3_latent(width,height,shape.total_frames)
context_frames,motion_score,reason=choose_context_frames(continuity,previous_state); desired_cumulative=int(round(sum(durations[:sequence_index+1])*FPS)); requested_new_frames=max(1,desired_cumulative-retained_frames); shape=make_extension_shape(context_frames,requested_new_frames/FPS); latent=empty_h3_latent(width,height,shape.total_frames)
include_chunk_last=bool(last_frame is not None and is_final)
conditioning_key=_conditioning_cache_key(prompt,include_last=include_chunk_last,reference_assets=reference_assets)
if following_video:
@@ -1100,7 +1111,7 @@ def _run_runtime_internal(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampl
video_context,audio_context,grid_offset=select_context(previous_state,context_frames,include_audio=bool(audio_continuity)); context_before=context_fingerprint(video_context,audio_context)
conditioning=prepare_conditioning(base_conditioning,video_context=video_context,audio_context=audio_context,audio_grid_offset=grid_offset,context_frames=context_frames,new_frame_count=shape.total_frames,first_frame_policy=POLICY_REPLACE,preserve_last_frame=True)
context_interop_emitted=True
clip_index=int(previous_state["clip_index"])+1; chunk_plan=make_plan(continuation=True,clip_index=clip_index,total_frames=shape.total_frames,trim_frames=context_frames,width=width,height=height,context_frames=context_frames,state_capacity_frames=largest_context_capacity(shape.net_new_frames),requested_extend_seconds=chunk_seconds,debug=debug)
clip_index=int(previous_state["clip_index"])+1; chunk_plan=make_plan(continuation=True,clip_index=clip_index,total_frames=shape.total_frames,trim_frames=context_frames,width=width,height=height,context_frames=context_frames,state_capacity_frames=largest_context_capacity(shape.net_new_frames),requested_extend_seconds=chunk_seconds_for_chunk,debug=debug)
if follow_slice_contract is not None:
chunk_plan[FOLLOW_PLAN_KEY] = follow_slice_contract
if adaptive_observer_event is not None and not reusing_group:
@@ -1673,7 +1684,7 @@ def _run_runtime_internal(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampl
if timeline_video_source is not None: settings["timeline_video_contract"]=timeline_video_source.contract
if guide_source is not None: settings["guide_contract"]=dict(guide_source.contract)
if reference_storage_plan is not None: settings["reference_routing_v1"]=routed_session_settings
new_session=make_session(chunks=entries,width=width,height=height,chunk_seconds=chunk_seconds,identity_hash=sequence_identity_hash,model_fingerprint_value=current_model_fingerprint,parent_session_id=parent_id,reroll_from_chunk=int(reroll_from_chunk),settings=settings)
new_session=make_session(chunks=entries,width=width,height=height,chunk_seconds=chunk_seconds,chunk_durations=durations,identity_hash=sequence_identity_hash,model_fingerprint_value=current_model_fingerprint,parent_session_id=parent_id,reroll_from_chunk=int(reroll_from_chunk),settings=settings)
report_lines=[f"H3 Continuum V3 {PACKAGE_VERSION}",f"Conditioning mode: {conditioning_display}.",prompt_plan_report(plan),"Decode: external ComfyUI Core VAE nodes; full raw AV chunks retained.",accelerators,"Execution: conditioning precomputed; single call-local MODEL clone per chunk; no internal VAE decode.",*reuse_notes,*routing_warnings]
if max_new_physical_groups is not None:
report_lines.append(
@@ -1726,14 +1737,14 @@ def _run_runtime_internal(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampl
else: images,audio,decode_reports=decode_sequence_with_seam(entries=entries,video_vae=video_vae,audio_vae=audio_vae,diagnostics_mode=diagnostics_mode,automatic=seam_correction==SEAM_CORRECTION_AUTO)
duration_report=""
if exact_total_duration:
target_frames=int(round(chunks*chunk_seconds*FPS)); images,audio,duration_report=enforce_total_frames(images,audio,target_frames=target_frames,preserve_final_frame=last_frame is not None)
target_frames=int(round(sum(durations)*FPS)); images,audio,duration_report=enforce_total_frames(images,audio,target_frames=target_frames,preserve_final_frame=last_frame is not None)
last_state=entry_to_state(entries[-1]); parent_id=session.get("session_id") if session is not None else None
settings={"continuity":continuity,"audio_continuity":bool(audio_continuity),"exact_total_duration":bool(exact_total_duration),"prompt_mode":plan["mode"],"base_seed":int(base_seed),"reroll_nonce":int(reroll_nonce),"diagnostics_mode":diagnostics_mode,"initial_state_source":initial_state is not None,"first_frame_hash":assets.first_frame_hash,"last_frame_hash":assets.last_frame_hash,"reference_contract":reference_assets.contract if reference_assets is not None else None}
if multi_chunk_flf: settings["flf_execution"]=FLF_STRATEGY
if continuation_transport!=REFERENCE_CONTEXT_V1: settings["continuation_transport"]=continuation_transport
if guide_source is not None: settings["guide_contract"]=dict(guide_source.contract)
if reference_storage_plan is not None: settings["reference_routing_v1"]=routed_session_settings
new_session=make_session(chunks=entries,width=width,height=height,chunk_seconds=chunk_seconds,identity_hash=sequence_identity_hash,model_fingerprint_value=current_model_fingerprint,parent_session_id=parent_id,reroll_from_chunk=int(reroll_from_chunk),settings=settings)
new_session=make_session(chunks=entries,width=width,height=height,chunk_seconds=chunk_seconds,chunk_durations=durations,identity_hash=sequence_identity_hash,model_fingerprint_value=current_model_fingerprint,parent_session_id=parent_id,reroll_from_chunk=int(reroll_from_chunk),settings=settings)
decoded_gib=float(images.shape[0])*float(width)*float(height)*3.0*4.0/(1024.0**3)
report_lines=[f"H3 Continuum V2 {PACKAGE_VERSION}",prompt_plan_report(plan),f"Seam correction: {seam_correction}.",accelerators,"Execution: conditioning precomputed; single call-local MODEL clone per chunk; decode deferred until sampling completed.",*reuse_notes,*routing_warnings]
if diagnostics_mode!=DIAGNOSTICS_OFF: report_lines.extend([f"Decode RAM estimate: {decode_estimate_gib:.2f} GiB including transient headroom"+(f"; available at start {available_ram_gib:.2f} GiB." if available_ram_gib is not None else "."),*sampling_reports,*decode_reports])