diff --git a/constants.py b/constants.py index 8abd6f8..f43c0db 100644 --- a/constants.py +++ b/constants.py @@ -9,11 +9,11 @@ FRAME_RESCALE = AUDIO_LATENT_FPS / FPS FRAME_PER_TOKEN = (1, 4, 4, 4, 4) CHUNK_SECONDS_DEFAULT = 5.0 -CHUNK_SECONDS_MIN = 4.0 +CHUNK_SECONDS_MIN = 4.1 CHUNK_SECONDS_MAX = 30.0 CHUNK_SECONDS_STEP = 0.1 CHUNK_SECONDS_TOOLTIP = ( - "Target duration shared by every chunk. 5–15 seconds is the recommended " + "Target duration for each chunk. 5–15 seconds is the recommended " "and validated range. Values above 15 seconds are supported, but VRAM " "use and processing time can increase substantially, especially at high " "resolution." diff --git a/docs/USER_GUIDE.md b/docs/USER_GUIDE.md index 4d00142..fb32d8a 100644 --- a/docs/USER_GUIDE.md +++ b/docs/USER_GUIDE.md @@ -59,6 +59,8 @@ For the supplied `2 × 10 s` workflow, put each time header on its own line and Use the actual subjects and actions from your images; this is a syntax example, not a saved prompt in the downloadable workflow. Do not place prompt text on the same line as `[0-10s]`: that is not a Timeline header. A Reference can still influence a chunk without an explicit `@R` mention when its checkbox is enabled. +Timeline ranges can use different durations. For example, `[0-10s]`, `[10-15s]`, and `[15-25s]` create 10-second, 5-second, and 10-second chunks. The prompt plan reports those durations and the Sampler uses them during generation. Each duration must be at least 4.1 seconds; the normal recommended range remains 5–15 seconds. + The [official V3.9 workflow](../examples/workflows/MiniMax_H3_Continuum_V39.json) is the latest user-supplied JSON, copied without editing; its [single-workflow ZIP](../examples/workflows/MiniMax_H3_Continuum_V39.zip) is also available. It preserves the full V38X2 model/LoRA, Decode Cache, Finalize and Save chain. Six image loaders are prewired (initially bypassed); to use Image 7–9, add `H3 Continuum Load Image` nodes and connect them to the matching helper slots. The Sequence Prompt input remains connected but is intentionally blank: write your own prompt before Queue. The saved graph uses `Timeline`, two 10-second chunks, `Full Run` (displayed as Generate Full Video), First Image + Draft 0.30 MP size selection, and a separate Project ID. The earlier [Reference Inline migration fixture](../examples/workflows/MiniMax_H3_Continuum_V39_Reference_Inline.json) remains available unchanged. See the [step-by-step Reference Images guide](V39_REFERENCE_INLINE.md). Replace local media and model filenames as needed before Queue; enabling an image requires choosing a real file. Seven to nine active references can be particularly memory-intensive: start with fewer, smaller source images. A full ComfyUI backend restart and browser hard-refresh are required before the new node definitions appear. Browser save/reload and GPU acceptance of this unchanged default graph are still pending; a GitHub Release does not imply these gates or a Registry package have been completed. **Compatibility:** Existing V3.8X2 workflows still use the V3.8 Sampler and old Reference Images 4–9 helper. V3.9 needs its new helper and connection. Open the new V3.9 workflow instead of only changing the Sampler type in an old graph: older V3.9 prototype workflows with direct 1–3 connections and Sampler-side routing widgets need manual rewiring, and their saved widget positions may differ. Accepted V3.8 Runs/Takes are not automatically migrated. Keep a copy of the previous workflow and use a new Run Name for V3.9. @@ -307,7 +309,7 @@ Every `H3 Continuum Load Image` has `Enable Image`. `ON` loads through ComfyUI C | `Control After Generate` | What ComfyUI does to `Base Seed` after a Queue | `fixed` for `Review Each Chunk`, resume, and controlled comparisons | | `Audio Continuity` | Carries generated-audio context across boundaries | `true` for normal audiovisual generation | | `Chunks` | Planned total chunk count, from 1 to 16 | Enter the final total, not the number to add next | -| `Seconds per Chunk` | Duration shared by every chunk | 5–15 seconds is the validated range; see duration notes below | +| `Seconds per Chunk` | Fallback duration when the prompt does not define per-chunk durations | 5–15 seconds is the validated range; see duration notes below | | `Total Length` | Read-only `Chunks × Seconds per Chunk` | Check only; it never edits either input | | `Size Source` | `First Image` or `Manual` output geometry | First Image for I2VA/FL2VA; Manual for T2VA or exact dimensions | | `Resolution` | First-Image sizing preset | `Draft — 0.30 MP` for tests, `Balanced — 0.60 MP` for more detail, or `Native 768` for the H3-native short edge | diff --git a/hardening.py b/hardening.py index 44e5001..d50753c 100644 --- a/hardening.py +++ b/hardening.py @@ -81,7 +81,10 @@ def validate_assembly_plan_contract(plan: Mapping[str, Any]) -> None: raise ValueError("assembly_plan.chunk_seconds must be finite and positive") target_frames = _strict_int("assembly_plan.target_frames", plan.get("target_frames"), 1) - expected_target = int(round(len(chunks) * chunk_seconds * fps)) + durations = plan.get("chunk_durations") or [chunk_seconds] * len(chunks) + if not isinstance(durations, list) or len(durations) != len(chunks): + raise ValueError("assembly_plan.chunk_durations does not match chunks") + expected_target = int(round(sum(float(value) for value in durations) * fps)) if target_frames != expected_target: raise ValueError( "assembly_plan.target_frames does not match chunks * chunk_seconds * fps" diff --git a/run_storage.py b/run_storage.py index 5ec5cd2..2d1f0c3 100644 --- a/run_storage.py +++ b/run_storage.py @@ -1463,6 +1463,7 @@ def build_sampling_contract( "sigmas": _tensor_exact(sigmas), "width": int(width), "height": int(height), "chunk_seconds": float(chunk_seconds), + "chunk_durations": [float(value) for value in (prompt_plan.get("chunk_durations") or [chunk_seconds] * chunks)], "continuity": continuity_storage_mode(continuity), "audio_continuity": bool(audio_continuity), "base_seed": int(base_seed), @@ -3367,7 +3368,9 @@ class RunStorageController: ) return make_session( chunks=best_entries, width=int(width), height=int(height), - chunk_seconds=float(chunk_seconds), identity_hash=str(identity_hash), + chunk_seconds=float(chunk_seconds), + chunk_durations=list(prompt_plan.get("chunk_durations") or [chunk_seconds] * len(best_entries)), + identity_hash=str(identity_hash), model_fingerprint_value=str(model_fingerprint_value), parent_session_id=None, reroll_from_chunk=0, settings=session_settings, diff --git a/tests/test_v36_chunk_duration.py b/tests/test_v36_chunk_duration.py index 8529f36..dad1697 100644 --- a/tests/test_v36_chunk_duration.py +++ b/tests/test_v36_chunk_duration.py @@ -12,6 +12,7 @@ from ComfyUI_H3_Continuum_Join.v3.nodes import ( H3ContinuumSamplerProduction, H3ContinuumSamplerV3, ) +from ComfyUI_H3_Continuum_Join.v2.prompts import make_prompt_plan def _chunk_seconds_schema(node_class): @@ -31,7 +32,7 @@ def test_all_shared_sampler_and_prompt_plan_schemas_allow_thirty_seconds(): for node_class in node_classes: schema = _chunk_seconds_schema(node_class) assert schema["default"] == 5.0 - assert schema["min"] == 4.0 + assert schema["min"] == 4.1 assert schema["max"] == 30.0 assert schema["step"] == 0.1 assert schema["tooltip"] == CHUNK_SECONDS_TOOLTIP @@ -42,4 +43,23 @@ def test_chunk_seconds_tooltip_describes_validated_and_high_cost_ranges(): assert "above 15 seconds are supported" in CHUNK_SECONDS_TOOLTIP assert "VRAM" in CHUNK_SECONDS_TOOLTIP assert "high resolution" in CHUNK_SECONDS_TOOLTIP - assert "shared by every chunk" in CHUNK_SECONDS_TOOLTIP + assert "for each chunk" in CHUNK_SECONDS_TOOLTIP + + +def test_timeline_prompt_can_define_per_chunk_durations(): + plan = make_prompt_plan( + mode="Timeline", + script="[0-10s]\nfirst\n\n[10-15s]\nsecond\n\n[15-25s]\nthird", + chunks=3, + chunk_seconds=5.0, + ) + assert plan["chunk_durations"] == [10.0, 5.0, 10.0] + assert plan["prompts"] == ["first", "second", "third"] + + +def test_timeline_prompt_rejects_four_second_chunks_with_fixed_fallback(): + plan = make_prompt_plan( + mode="Timeline", script="[0-4s]\nshort", chunks=1, chunk_seconds=5.0 + ) + assert plan["chunk_durations"] == [5.0] + assert any(item.get("code") == "H3C-P100" for item in plan["diagnostics"]) diff --git a/v2/nodes.py b/v2/nodes.py index ebe8648..de2325c 100644 --- a/v2/nodes.py +++ b/v2/nodes.py @@ -137,7 +137,7 @@ class H3ContinuumSamplerV2: "dynamicPrompts": True, "tooltip": ( "Fixed: one prompt. List: separate chunks with a line containing ---. " - "Timeline: use [0-5s], [5-10s], ... or [Chunk 1] headers." + "Timeline: use [0-5s], [5-10s], ... for equal chunks, or use contiguous ranges such as [0-10s], [10-15s] for per-chunk durations." ), }, ), @@ -393,7 +393,7 @@ class H3ContinuumSamplerV2: chunk_seconds=float(chunk_seconds), prompt_hashes=list(prompt_plan["hashes"]), timeline_video_source=timeline_video_source, - ), + ) and len(set(prompt_plan.get("chunk_durations") or [float(chunk_seconds)] * int(chunks))) == 1, mode="Custom", selectors_by_slot=reference_routing_request.selectors_by_slot, ) diff --git a/v2/prompts.py b/v2/prompts.py index 17e5aa6..829f97d 100644 --- a/v2/prompts.py +++ b/v2/prompts.py @@ -143,7 +143,27 @@ def _parse_timeline(script,chunks,chunk_seconds,*,ignore_list_separators=False): 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 + time_sections = [item for item in sections if item["kind"] == "time"] + durations = None + if len(time_sections) >= chunks: + candidate = sorted(time_sections, key=lambda item: (item["start"], item["end"]))[:chunks] + contiguous = abs(candidate[0]["start"]) <= 1e-6 and all( + abs(candidate[index]["start"] - candidate[index - 1]["end"]) <= 1e-6 + for index in range(1, len(candidate)) + ) + if contiguous: + durations = [round(item["end"] - item["start"], 6) for item in candidate] + if any(value < CHUNK_SECONDS_MIN or value > CHUNK_SECONDS_MAX for value in durations): + raise PromptPlanError( + f"timeline chunk durations must be between {CHUNK_SECONDS_MIN:.1f} and {CHUNK_SECONDS_MAX:.1f} seconds" + ) + prompts = [item["prompt"] for item in candidate] + diagnostics.extend( + {"level": "info", "code": "H3C-P000", "message": f"Chunk {index + 1} source: {item['source']}"} + for index, item in enumerate(candidate) + ) + notes.append("derived per-chunk durations from timeline headers") + return prompts,notes,diagnostics,durations 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): @@ -168,28 +188,28 @@ def resolve_prompt_mode(mode,script): 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=[] + 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=[]; durations=[chunk_seconds]*chunks 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 + if resolved_mode==PROMPT_MODE_FIXED: prompt=_normalize_prompt(script,label="fixed prompt"); prompts=[prompt]*chunks; durations=[chunk_seconds]*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) + elif resolved_mode==PROMPT_MODE_TIMELINE: + mixed_syntax=_has_list_separator(script) + prompts,parse_notes,diagnostics,durations=_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 + resolved_mode=PROMPT_MODE_FIXED; prompts=[_normalize_prompt(script,label="fixed prompt")]*chunks; durations=[chunk_seconds]*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} + result={"magic":PROMPT_PLAN_MAGIC,"schema_version":PROMPT_PLAN_SCHEMA_VERSION,"mode":resolved_mode,"chunks":chunks,"chunk_seconds":chunk_seconds,"chunk_durations":durations,"prompts":prompts,"hashes":[prompt_hash(p) for p in prompts],"notes":notes} if diagnostics: result["diagnostics"]=diagnostics return result def validate_prompt_plan(plan): @@ -197,9 +217,14 @@ def validate_prompt_plan(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")) + 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 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}") + durations=plan.get("chunk_durations") + if durations is None: durations=[chunk_seconds]*chunks + if not isinstance(durations,list) or len(durations)!=chunks: raise PromptPlanError("prompt-plan chunk duration count does not match chunks") + durations=[float(value) for value in durations] + if any(valueCHUNK_SECONDS_MAX for value in durations): raise PromptPlanError("prompt-plan contains an invalid chunk duration") 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): @@ -207,7 +232,8 @@ def validate_prompt_plan(plan): 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 + plan["chunk_durations"]=durations + 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) @@ -216,7 +242,8 @@ def build_sampler_prompt_plan(*,prompt_mode,prompt_script,sequence_prompt,prompt prompts=list(plan["prompts"]) if 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] + result=dict(plan); result["chunks"]=chunks; result["chunk_seconds"]=chunk_seconds; result["chunk_durations"]=list(plan.get("chunk_durations") or [chunk_seconds]*int(plan["chunks"]))[:chunks]; result["prompts"]=prompts; result["hashes"]=[prompt_hash(p) for p in prompts] + if len(result["chunk_durations"])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) @@ -230,7 +257,8 @@ def apply_prompt_overrides(plan,overrides): 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." + durations=plan.get("chunk_durations") or [plan["chunk_seconds"]]*plan["chunks"] + result=f"Prompt plan: {plan['mode']}; {plan['chunks']} chunks; durations [{', '.join(f'{value:g}s' for value in durations)}]; total {sum(durations):g}s; {unique} unique prompt(s)." if note: result+=" "+note+"." diagnostics=list(plan.get("diagnostics") or []) if not diagnostics: return result+"\nPrompt Preflight: OK." diff --git a/v2/sequence.py b/v2/sequence.py index 25b7043..bac3912 100644 --- a/v2/sequence.py +++ b/v2/sequence.py @@ -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 dict[str, Any]: raise SessionValidationError("session dimensions are invalid") if float(session.get("chunk_seconds", 0.0)) <= 0: raise SessionValidationError("session chunk_seconds is invalid") + durations=session.get("chunk_durations") + if durations is not None and (not isinstance(durations,list) or len(durations)!=len(session.get("chunks") or [])): + raise SessionValidationError("session chunk_durations are invalid") chunks = session.get("chunks") if not isinstance(chunks, list) or not chunks: raise SessionValidationError("session contains no chunks") diff --git a/v3/driving_nodes.py b/v3/driving_nodes.py index df6fb20..0a5770f 100644 --- a/v3/driving_nodes.py +++ b/v3/driving_nodes.py @@ -227,9 +227,16 @@ class H3ContinuumSamplerV34(H3ContinuumSamplerProduction): from ..reference_video import prepare_reference_video_source from ..temporal import align_frame_count_up - target_frames = round( - int(kwargs["chunks"]) * float(kwargs["chunk_seconds"]) * FPS - ) + from ..v2.prompts import build_sampler_prompt_plan + prompt_plan = build_sampler_prompt_plan( + prompt_mode=kwargs["prompt_mode"], + prompt_script=kwargs.get("prompt_script", ""), + sequence_prompt=kwargs.get("sequence_prompt"), + prompt_plan=None, + chunks=int(kwargs["chunks"]), + chunk_seconds=float(kwargs["chunk_seconds"]), + ) + target_frames = round(sum(prompt_plan.get("chunk_durations") or [float(kwargs["chunk_seconds"])] * int(kwargs["chunks"])) * FPS) source = prepare_driving_audio_source( driving_audio, audio_vae, diff --git a/v3/nodes.py b/v3/nodes.py index de7e747..d1ad4ff 100644 --- a/v3/nodes.py +++ b/v3/nodes.py @@ -197,7 +197,7 @@ def _apply_review_decode_scope( from ..v2.sequence import _terminal_flf_merge_enabled from .plan import prepare_physical_decode_entries - terminal_merged = sequence_complete and _terminal_flf_merge_enabled( + terminal_merged = sequence_complete and len(set(assembly_plan.get("chunk_durations") or [float(chunk_seconds)] * int(configured_chunks))) == 1 and _terminal_flf_merge_enabled( multi_chunk_flf=( first_frame is not None and last_frame is not None @@ -643,7 +643,8 @@ class H3ContinuumSamplerV3: configured_chunks = int(chunks) completed_chunks = len(entries) sequence_complete = completed_chunks == configured_chunks - terminal_merged = sequence_complete and _terminal_flf_merge_enabled( + prompt_durations = [float(entry.get("plan", {}).get("requested_extend_seconds", chunk_seconds)) for entry in entries] + terminal_merged = sequence_complete and len(set(prompt_durations)) == 1 and _terminal_flf_merge_enabled( multi_chunk_flf=( first_frame is not None and advanced_values["last_frame"] is not None diff --git a/v3/plan.py b/v3/plan.py index 137f15c..395cfad 100644 --- a/v3/plan.py +++ b/v3/plan.py @@ -19,11 +19,12 @@ class AssemblyPlanError(ValueError): pass -def make_assembly_plan( - entries: list[dict[str, Any]], - *, - chunk_seconds: float, - preserve_final_frame: bool, +def make_assembly_plan( + entries: list[dict[str, Any]], + *, + chunk_seconds: float, + chunk_durations: list[float] | None = None, + preserve_final_frame: bool, ) -> dict[str, Any]: if not entries: raise AssemblyPlanError("cannot create an assembly plan for an empty sequence") @@ -61,14 +62,18 @@ def make_assembly_plan( } ) - result = { + durations = [float(value) for value in (chunk_durations or [chunk_seconds] * len(chunks))] + if len(durations) != len(chunks): + raise AssemblyPlanError("chunk duration count does not match chunks") + result = { "magic": ASSEMBLY_PLAN_MAGIC, "schema_version": ASSEMBLY_PLAN_SCHEMA_VERSION, "fps": FPS, "width": int(width), "height": int(height), - "chunk_seconds": float(chunk_seconds), - "target_frames": int(round(len(chunks) * float(chunk_seconds) * FPS)), + "chunk_seconds": float(chunk_seconds), + "chunk_durations": durations, + "target_frames": int(round(sum(durations) * FPS)), "preserve_final_frame": bool(preserve_final_frame), "chunks": chunks, } @@ -89,8 +94,11 @@ def validate_assembly_plan(plan: Any) -> dict[str, Any]: raise AssemblyPlanError("assembly plan FPS must be 24") if int(plan.get("width", 0)) <= 0 or int(plan.get("height", 0)) <= 0: raise AssemblyPlanError("assembly plan dimensions are invalid") - if float(plan.get("chunk_seconds", 0.0)) <= 0: - raise AssemblyPlanError("assembly plan chunk duration is invalid") + if float(plan.get("chunk_seconds", 0.0)) <= 0: + raise AssemblyPlanError("assembly plan chunk duration is invalid") + durations = plan.get("chunk_durations") or [float(plan["chunk_seconds"])] * len(plan.get("chunks") or []) + if not isinstance(durations, list) or len(durations) != len(plan.get("chunks") or []): + raise AssemblyPlanError("assembly plan chunk durations are invalid") if int(plan.get("target_frames", 0)) <= 0: raise AssemblyPlanError("assembly plan target frame count is invalid") @@ -356,6 +364,7 @@ def prepare_physical_decode_entries( entries, *, chunk_seconds: float, + chunk_durations: list[float] | None = None, preserve_final_frame: bool, terminal_merged: bool, terminal_initial_pair: bool | None = None, @@ -363,9 +372,15 @@ def prepare_physical_decode_entries( """Return external-VAE decode units while preserving logical plan metadata.""" logical_entries = list(entries) + if chunk_durations is None: + chunk_durations = [ + float(entry.get("plan", {}).get("requested_extend_seconds", chunk_seconds)) + for entry in logical_entries + ] plan = make_assembly_plan( - logical_entries, - chunk_seconds=float(chunk_seconds), + logical_entries, + chunk_seconds=float(chunk_seconds), + chunk_durations=chunk_durations, preserve_final_frame=bool(preserve_final_frame), ) logical_chunk_indices = [ diff --git a/version.py b/version.py index 1cee36b..b180084 100644 --- a/version.py +++ b/version.py @@ -5,4 +5,4 @@ STATE_SCHEMA_VERSION = 1 PLAN_SCHEMA_VERSION = 1 PUBLIC_API_VERSION = 3 SESSION_SCHEMA_VERSION = 1 -PROMPT_PLAN_SCHEMA_VERSION = 1 +PROMPT_PLAN_SCHEMA_VERSION = 1