diff --git a/AGENTS.md b/AGENTS.md index 7119147..a970c95 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,7 +1,7 @@ # AGENTS.md ## Scope -MiniMax H3 Continuum V3.5.1 for ComfyUI. Treat V3.5.1 as the current public baseline; preserve V3.4 node IDs, public contracts, and saved-workflow loading without letting legacy controls change current behavior. +MiniMax H3 Continuum V3.5.2 for ComfyUI. Treat V3.5.2 as the current public baseline; preserve V3.4 node IDs, public contracts, and saved-workflow loading without letting legacy controls change current behavior. ## Non-negotiable rules - Follow ComfyUI Core behavior for user-facing validation. Continuum-only prompt restrictions, model allowlists, compatibility gates, and policy-based execution stops are prohibited. diff --git a/CHANGELOG.md b/CHANGELOG.md index affca32..162deba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## 3.5.2 + +- Added a conservative V3.5-only Prompt/CLIP cross-run LRU cache for unchanged T2VA, I2VA, and FL2VA conditioning. Reference, scheduled, tokenizer-option, and hook-modified CLIP paths bypass the cache; V3.4 is unchanged. +- Verified cache HIT execution with `encode_calls=0` and bit-identical decoded video and audio PCM. The optimization removes repeated Prompt/CLIP work only; Sampling still runs normally. +- Reduced long Video Guide preprocessing memory by hashing and finite-checking the complete source in eight-frame CPU chunks while retaining only the prefix required by H3 conditioning. +- Preserved the exact Video Guide source SHA-256, Run Storage identity, `17k+5` frame alignment, final-frame padding, VAE input, Qwen input, Sampling seed, decoded video, and decoded audio. +- Measured the deterministic CPU comparison at approximately 71% lower additional peak RSS (`396.8 MiB` to `114.7 MiB`) and 59% lower retained storage (`225 MiB` to `93 MiB`) with effectively unchanged preprocessing time. +- Completed the Phase 3/4 performance and feature-overhead audit. No speculative Sampling, Driving Audio lazy-decode, Audio hash, Assembly, Seam, Session, or V3.4 optimization was adopted. +- Preserved all V3.4/V3.5 node IDs, backend socket keys, saved-workflow compatibility, Sampling, Conditioning, Terminal Merge, Assembly, Seam, and Run Storage contracts. + ## 3.5.1 - Added `H3 Continuum Conditioning Bridge V3.5`, exposing one complete Core-compatible MODEL and CONDITIONING object per Continuum physical group without flattening CONDITIONING entries. diff --git a/MANIFEST.sha256 b/MANIFEST.sha256 index 9ac79fd..ccf7d22 100644 --- a/MANIFEST.sha256 +++ b/MANIFEST.sha256 @@ -1,8 +1,8 @@ dca65fb1393e67f59c6f81f9411e3e745eb8f1097e9a2adf7ffc6f4e3901ca9f __init__.py 4f6feeb2f63a9285b0e76fb2e2618058c7d7c279af1632e1a6ccabcc9ec9a45a .github/workflows/tests.yml 0a2f69d4cec89d440f5d5aa1ec68a7c9a741382d313f80dcddf96d13f70b3e68 .gitignore -a9abe27341cb35cf7cee932a0fe4c20966b498c8fb6496c94c079d145a4eab6c AGENTS.md -e2615b31bf86d7cb338e4e0e22f789fc40dfbafc71c27b4637fbc14ef2ec99d5 CHANGELOG.md +a7cfeb430af9d510110b5e2fb6c024a954fa83b856f985ce27da608fc508bab6 AGENTS.md +7f0a4b24a14abae40a98bbc9f66112e74fd4b80a8820f7f15ec6a9dd1ab5455e CHANGELOG.md e97e47c7585e81d9ec5fd32c0ca0b64699fd4610cd03e9d6575a91c30a10ca4e compatibility.py 4d62508f900af9f93ec320afb900c27cc9a300d1e0c0a94714258498c5ede5fc conditioning.py 71e3df269e226b1ecad597f9831c2a10b2215c8a3783652e9e9f536e99ce46c9 constants.py @@ -29,6 +29,7 @@ a53796f7a904d675e80a2276c20d68502f03a3958529278e6ce9465baaf1c4af docs/images/v3 04a34b1b55b627553d08b7b10de71295d55ea9e4ac9815b6dd8ce6d268098851 docs/images/v351-lbh-conditioning-bridge-flow.svg b16d63242b45fe1abef0a4dddd4fc5b9306aa30c2f84bbc3ddf334a1c0832957 docs/images/v351-reference-audio-permanent.png ed189c7f16ba1f6e48f557312066fdb5e1313b5ea7df853a99467fd0d7532ef5 docs/images/v351-video-guide-frames.png +0c1dfd5acff349b98ecdfb57ae50941f6da26a462dfd2aaa9b7c7b0a34abf304 docs/images/v352-stabilization-optimization.png 93f07aa727b34802bce3d333426b8ba2af522500882a6d222d00cf577ca82e49 docs/SEAM_TEST_SOURCE_JA.md 2c13aefd137db92a3dbf5576e9442d58012b1f0de1240c07d039ceeb702e7c1f docs/V2_1_4_UI_SIMPLIFICATION.md b08022df068576c759c8bf80ac6b6519693f3c65c917f1822aeb06ea359aa5b1 docs/V2_1_5_UI_SERIALIZATION_STABILITY.md @@ -65,18 +66,18 @@ d74073e7ee292ebeba913af4288284f9f197cd91fceed6c62bf6c66b784c7cec install_window 6aa3f5780baa7bb0c0672b5f01a2f77e5f209da93f6373468cb274ea2f643d4b layout_adapter.py c44abbd789091bbead01250dbd5453e60539d2665c69f162335fdbd2936019c2 LICENSE d3e213005670e94dee33a240349a4d245cf9a3b6602099f11ed30cf5a32e2c21 media.py -06d05824778039ec2ba3bae7b62d85c2f0c7e2a60355e29d56cb8400a9792b5a metadata.ini +73d8520121ae6e22c58e8455d09c6e9dd09949469f8b2a5485d200c7ee3244f8 metadata.ini ef705a476e3f5bbd05559e7a0e7d3dfb9275badef0f8f65fb1a36ce27fbcb537 model_patch.py 420f00c8193d527017720b26d6f7011d69c8f68a157b21c0102246cc98ad3a13 nodes.py -5c4f885c754813bc4500376335c33f0b1b97f25bc738ec8b0e8b12b4b6d78935 PACKAGE_INFO_JA.txt -15e55d9bcc22607a7322d5b817da4db0476b4c2ecc256bf613b96442a1e23bfe PACKAGE_VALIDATION.txt +7f1869c2eac19f79b1aae6a65362b4eb84dba8b812028abf7d3d29d780cac4c2 PACKAGE_INFO_JA.txt +9a52ebfe476c666633c3dac690a1cb4e0e4d34b2591f23e05fe1100a997c668a PACKAGE_VALIDATION.txt b470819f16fc179654dc1600721a63f0a5a14e777f4c004d574b499da6886ba5 public_api.py -4015160058bc975f4ec85f59d41a6b57e95163090328e129df6b74738743cdad pyproject.toml +5ee4d404553ea773c58aee9714e29ab4f23f01a9281911c8ef5b59bb323eb12f pyproject.toml 94fdc2d545b2d4077a4d70229fd22ba3ccac21d49d1097d2873f76d04e9a3c7a pytest.ini -d3404ebed5e2fbb2979bf04b32ce3b66a6a27dba6d3b59cc37997facc9b7480d README_JA.md -44108437546c406509228e04ac48b0440079f1e082c10f1ef0c7ee9489a94161 README.md +5eccc59d235902f0c8b26e155cb012132d29c8a13ec9f10c444ccca9f08b0128 README_JA.md +436e388894537812c9a478286332f864efaa04728e694011b4cf878016e54da6 README.md 5d89c366b4f076fa73a2695557e0af749bf95df1dad0d55dbb2fe5b1c2519ad7 reference_audio.py -8f1744dc99f1a3a222b7d6e986f6dac1edcc0abbbe8b61a1d2606b1ca3569b3d reference_video.py +a0266ab4434b8e9844219d2b6dd8a168d198dd8f142775be01a39b81dc196b91 reference_video.py 2995924fcb7bac27b548b01187d8276d2ab103c6f43416cf85400de3846c6297 reference.py 467edb2c188d024c2392aafe7423b3f835e16b10f570da76e2f7dda8a962c1c5 requirements.txt e02b523b5b1c1272c912c16d547050cc9bb9feb6c348743015bb5d059a4ead06 run_storage.py @@ -91,12 +92,14 @@ f9faa77aef448971ae8ebd3447c52db2611f1dbab55e8a41b9c7fea82ba7e0bf tests/test_lay e480c599daf0ede07bba8b90efbae627461c1797a5ebacf228630fd74ebc48d4 tests/test_model_patch.py 45e8839cd82fd955df344b3bc6efa20092b8870ea378b0c898b073665d3e47d7 tests/test_nodes_logic.py b635d404ca67df69894c4fcf80d8782cc5699e946bfb55ae4c44fff9d930b018 tests/test_p20_file_backed_tensor_poc.py -c3ba92e6dc653ba9252a68e54df68a677132ab79f597e74005c9c0c746b01545 tests/test_p21_memory_attribution.py +69a6155d3719d75bc9783d55c327100ca3097b2a8d71e6b82c1e5daf5191a568 tests/test_p21_memory_attribution.py 2b097971258dd10f0df33e0f8cb5843ee6cd3e9ef9e7abb80b890c66baf22908 tests/test_p22_file_backed_buffer.py d09d2bae2fac04a058ebce22fa0e761d90b267107923ab8cd01a99cc547624e6 tests/test_p23_assembly_v35.py d2a02c120e01d360ddca17a92d5bc53b12c9f0734d06440b3fc2fad2e754e549 tests/test_p23_video_seam_patches.py +114530f8bc4b0a6cd416581636c85d494a07e5dcca93afe9c82f4a5aaaed9dc7 tests/test_p31_prompt_conditioning_cache.py +403a0ff300330ac1f97642bf0681e80b83eec18e8911c03952ddf203ce794e5a tests/test_p46a_reference_video_memory.py 1ae0369943ba2fb13f21ad0cf56ee58621ba09bf20174379595e157a1775c7fd tests/test_public_api.py -bb9b52a0981d7f453d6002a04fafe8788ea1ff8f16d1b43dbc08f1770e56192b tests/test_release_metadata.py +bc66ab71ea7dcb81aa856fcf254841f402ab150964f6133a6de765a3ff5c9f65 tests/test_release_metadata.py d4c53d2c260b635d70fa211029d608f63776e0a56c20253475fa47c58363f8a7 tests/test_state_io.py 98a33c75b33ff0c10a68e04bd856e4e43b3e70b6ae1ea458a2b21b4e4d74f46d tests/test_state.py 58c06925ccf06c4feee19b927b3515c540c0536aa5f3f8d29be35b8e919915a7 tests/test_temporal.py @@ -109,17 +112,15 @@ d4c53d2c260b635d70fa211029d608f63776e0a56c20253475fa47c58363f8a7 tests/test_sta b42bcc0983147462d96c6b71cc4a38e846995dafa6eebefc3ae0d9bb36caf359 tests/test_v2_sequence_orchestration.py 0b5b20a8318862d167f135720e0489b8d4f93406d67c6646e052e35b03dcb876 tests/test_v2_session.py 58438c3ed814adf40cefde098a37cae00ddcb703b7e5548bbbb295e54bed5a6d tests/test_v2_widget_migration.py -e96edf54cec3e4be0f7731be422e6a3e4a789b4443f15ac0c140e3aa3a27c1da tests/test_v217_stable_facade.py +419c358962225d7c8893bb426b653d1dc175197d2567b0af60b38bdbb549e011 tests/test_v217_stable_facade.py 78bc478a5e4edda902c80849c911782a99e75c06dfb04127391b6cc5b27b28e1 tests/test_v3_hardening.py eb5b27f8f0d79529c56ef344a5601703642c3cba5388c9768116223e2ed58a36 tests/test_v3_video_seam.py 1f5310587d05eb5058ec94d9985138d25aa22b22fb0dd5c68518f2f2053116a4 tests/test_v31_run_storage.py 3af78ab85c8e19dee8d0a98bfd11a2c0e88c30d87256da1f9699c49a19a684e9 tests/test_v32_reference.py 72719bcd6686d84f518d80751e3754b875d34c2aa72e2183dfa05ac9158a9eb0 tests/test_v34_driving_audio.py -5003c5f5ce1a4b4c77deb47011431e4a15fe0cecaeb6d689e0d0878567c42f5a tests/test_v351_reference_audio.py -63d18643701c7d8be7b45046e6dc76616565572180afa06b7582600bd6c32431 tests/test_v351_reference_audio_ui.py -cf1ca6342a89e4cb02b398b32edbe91b6c1353535383e9f51d459466305a0114 tests/test_v35_conditioning_bridge.py 8b75c23dccfd1ac302596187f2fbbd1c9b71fddcb5e666aa4f9eedfd8b5b4b93 tests/test_v35_compact_ui_frontend.py -b7267778fd1e77bd12d7c6e71c200fd0a4367cd8ae3276823ddeba16e015cfa6 tests/test_v35_context_sampler.py +cf1ca6342a89e4cb02b398b32edbe91b6c1353535383e9f51d459466305a0114 tests/test_v35_conditioning_bridge.py +7c04f6bcdb62ac0faae911cbe4414dff859b0dac79380ec7053b3e3130f434d6 tests/test_v35_context_sampler.py 1de83989a361cd23af8a38686e27740eafd62299123c71a110dd5c0a337d136e tests/test_v35_hires_fix.py 9ceac8c3c6e4e57ec8b53b48f3b547c204df36a3f6281328aa9ab5ebc2e6a0b6 tests/test_v35_latent_resize.py 59a07f2172014dfdc0a447775324d7cd049f20b823276dfaf9b7b3f79628f5a8 tests/test_v35_pixel_vae_resize.py @@ -127,6 +128,8 @@ b7267778fd1e77bd12d7c6e71c200fd0a4367cd8ae3276823ddeba16e015cfa6 tests/test_v35 8ae5173eaec35adf9efcde2c464c95651c848248bdbdd6396ed2e3c3c1ac7965 tests/test_v35_refine_sampling.py 98a9006ebc4cab2b61b38904e0043fab24e088a25b28ffd38012c29991b8550c tests/test_v35_second_pass_contract.py 167d19efa7194c4dc52e39c3068d501023891e9a1dfba2ccf52440e7ca0d12fc tests/test_v35_second_pass_sampling.py +63d18643701c7d8be7b45046e6dc76616565572180afa06b7582600bd6c32431 tests/test_v351_reference_audio_ui.py +5003c5f5ce1a4b4c77deb47011431e4a15fe0cecaeb6d689e0d0878567c42f5a tests/test_v351_reference_audio.py 0dbe8a5f71c92f3523942343b40787104c8b96781461ba3ba82934a6bdbe5834 THIRD_PARTY_NOTICES.md f218e1a93c42467c6057c5c1d04d57cc82f6331b97ec9257e4e2dfb9c831876a timeline_video.py e5a84569953ee63b9625f4f2e3195ec368204fddcfc746564cfbd0e8449bb8e9 tools/p20_file_backed_tensor_poc.py @@ -135,27 +138,27 @@ ffd61f3f31c9b6430e1169007ee906c8f5000eba63ee44e2976ad4f5f1112671 v2/__init__.py 2003902c87bba09cfa143f5a9e4248f3a78c0fb544449a75fd40ba2cacb35655 v2/context_diagnostics.py cde27db19b677e318cc4dfb4235be7a11429c19945f4083052a91fe117e74c82 v2/decoder.py 31012ec48a64c7467ad869c79982b7f8502c1785c33f51ee1b5727ce8b4243fb v2/diagnostics.py -87b054be8754ebfcd85e106a7b221604611d3bf05de03380565ee3562d1c823b v2/h3_builder.py +8fe649acb25c36361c042807b828c545d8febf760f9ba0187fbec3e69698064e v2/h3_builder.py aea01a48a8c03a25ed97ea05e5b137f89e388832d1be7d38f2da2be3d1b9830b v2/motion.py -080e9d597e2586eb88c448c7db32b9e3661f5d21a4c270f163696966ffa6c7e0 v2/nodes.py +ecc54e49e727bdaa9c2c3d8a31a8c04912a9d66b87d8f519c5512d033ac9c10c v2/nodes.py 3c2145d47924b53f8f6ec50806f1b7113120ac314142d8b7809c4f2064ad6aef v2/prompts.py 4080f1875b958b316e8e76852c4be5527a6de5c71d604e7642d3098478eb4a36 v2/sampling.py 2fd811794c4cc0f2ce050250b80484876454926b9f20fce6af3a97d728e5acdf v2/seam_guard.py be0d8585fa57e0ccd6d02577bbc7299629e5bfe20e186747f571fccc98b67d7e v2/seam_types.py 12cfcc0c71ca087fe39a5ce2922959a62b2bb311bfe664f05c4480affce03708 v2/seeds.py -e6c15105fe66b9d7786602c8e6d51dd256428d80635cee686b0e35d66ab3b794 v2/sequence.py +81f3438f01e8619b696d7c669a1025f3edab77085ee337e778afe10816b1ddd3 v2/sequence.py 13213e7709fd02bb810af3c18e4cd7d010f8cdfb121a4b99866a9f2333331089 v2/session_io.py 3ea23dbce6fcd217b6e81235855479b9b16fed7a21693cf156f9014555c4fe13 v2/session.py 78c830cdf49034c6e3b2cd3da5914d5cfc5638eb63ec6dbd1bab13c50d6d4caf v3/__init__.py b3dc61503d35d9ef0a0dc7fed1c9b2fdc00fcb2072d4e39f64ae0826f9ec7dca v3/assembly_v35.py 7815230e0a9540bc2edeaf818e940b7a1fdd817fd217ba8a4f9cb21aaf6a308e v3/assembly.py 3f62fdb35e804f42d7cbf0828b4e87e69a2b99246c3f7467fe4fa698f1d803c9 v3/conditioning_bridge_nodes.py -12e00ac8f00b04724be04f68b1406e62b550c202360676028c4248d97643cb50 v3/driving_nodes.py +9d152ca205b223dd2dca7c3084a8104656c3922592ab5a383d2be58e2161beb8 v3/driving_nodes.py 6cc7ed74666084c55beb9075304e92a9defafd81b9c6678239ca3859a08cddd7 v3/file_backed_buffer.py 039f03431c341aa0229f749699fd92272c77097e00b08649aded8328dfd83970 v3/hires_fix_nodes.py 0858487e2b8c0060a856273ffc65d23165b2f253d183f506e27e419aea0da561 v3/latent_resize_nodes.py -45bd1945b984b56369234053be2edb8455d4cff7bf976f7d9c7bef77a762b7eb v3/memory_attribution.py -f8708118f70fa2aae22951e025ed3b9499951088988c817c28f6ef7f388f2570 v3/nodes.py +85477badd85281cfb77582fdd5b34228a9f6b68376f1b7743af2ca7d4b55aa36 v3/memory_attribution.py +baac3d52d5d8f96b77ebb505e270c0c63c39b3758dcca26795d375d2c813bad7 v3/nodes.py b7c4a35a5e73f528492bc6946b97b687c412f1d3f5f976c19d45c6dc20288c9a v3/pixel_vae_resize.py 34ea38c03b6322288d88fc8595e3655a7a7afddb51709142a954eb7349f71ac2 v3/plan.py 10eaab14eb8a70dea3345bf8bb20069d78ec5464aef5542a9a80f1ccfdeeb9b0 v3/refine_context.py @@ -164,6 +167,6 @@ da5edf3b4392c41e12ef73a0cd21799be42a327b18bd5a6989447b75163dcab1 v3/second_pass 23a412ce55797cbe6ea19c4379053855ff21bca233e3c373577225b761cc03d0 v3/second_pass.py 4178dcfa442abdb06ce276e05bd7bcbaa6da28469ba5e57d3da3c663de4e4a9d v3/video_seam_v35.py 2f07ace1462644f0bee553827b3571535de4d0c2d9ef847bd9e0ca73a4eed5cb v3/video_seam.py -e981f75faff574b8e9ab3e9be8b68559e3e7a22f6220d3d073e7769f5fc0dc74 version.py +631055f46ad8be92f560c51aa33c6278ff2ce368a69293f6f510714b53ef1acc version.py 96b2f9aa1d20677433c46d225b8da26ab5379e4f0419e6d5e091a5c821898bb3 web/project_id.js 0cf8d49bcb71b30437dc893f6218dc4cfb1cd244b647e4fbc2861dea2271a33a web/reference_audio_ui.js diff --git a/PACKAGE_INFO_JA.txt b/PACKAGE_INFO_JA.txt index cdb3ea5..588f8b9 100644 --- a/PACKAGE_INFO_JA.txt +++ b/PACKAGE_INFO_JA.txt @@ -1,11 +1,17 @@ -ComfyUI-H3-Continuum V3.5.1 +ComfyUI-H3-Continuum V3.5.2 MiniMax H3の5秒ChunkをAV latent contextで継続し、長尺映像として組み立てるComfyUIカスタムノードです。 +V3.5.2: +- 同一Prompt / CLIP条件の繰り返し生成でCPU conditioning結果を再利用 +- Referenceや特殊CLIP経路は安全側でcache bypass +- 長尺Video Guideの全source identityとfinite検査を維持し、必要prefixだけを保持 +- 検証条件でVideo GuideのPeak追加RSSを約71%、保持Storageを約59%削減 +- Sampling、Conditioning payload、Terminal Merge、Assembly、Seam、Run Storage、標準Seed動作は不変 + V3.5.1: - physical groupごとに完全なMODEL / CONDITIONINGを公開するConditioning Bridge V3.5 - 生成音声を置換しない任意のReference Audio conditioningと常設socket -- RandomizeからFixedへ戻す際のLast Queued Seed Reuse - Video loaderのIMAGE frame batchを示すVideo Guide Frames / Video Guide Size表示名 - backend socket key、既存workflow接続、V3.4 sampling契約を維持 diff --git a/PACKAGE_VALIDATION.txt b/PACKAGE_VALIDATION.txt index a73f906..15111cb 100644 --- a/PACKAGE_VALIDATION.txt +++ b/PACKAGE_VALIDATION.txt @@ -1,6 +1,6 @@ -ComfyUI-H3-Continuum V3.5.1 package validation +ComfyUI-H3-Continuum V3.5.2 package validation -Status: V3.5.1 release. Stable V3.5 paths, Conditioning Bridge, and Reference Audio are validated. Seed widgets use standard ComfyUI behavior. Integrated Hi-Res Fix remains Experimental. +Status: V3.5.2 stabilization and optimization release. Stable V3.5 paths, Conditioning Bridge, Reference Audio, Prompt/CLIP cache reuse, and Video Guide memory optimization are validated. Seed widgets use standard ComfyUI behavior. Integrated Hi-Res Fix remains Experimental. Required automated checks: - python -m compileall -q . @@ -30,7 +30,7 @@ Validated runtime paths include: - Audio Seam Auto - Video Seam Auto guarded transient and micro-flash correction -Latest verified automated result: 450 passed. +Latest verified automated result: 485 passed. Hi-Res Fix and Auto 2 remain experimental. Main 3x5 2x is not accepted on the tested RTX 5060 Ti 16 GiB because the terminal 77T Second Pass group exceeded VRAM; longer Reference/Hybrid acceptance remains pending. diff --git a/PROJECT_STATE.md b/PROJECT_STATE.md index 4a47e23..a425555 100644 --- a/PROJECT_STATE.md +++ b/PROJECT_STATE.md @@ -10,7 +10,72 @@ - E2-8A Contract, E2-8B1 CPU/static validation, E2-8B2 external LBH GPU PoC, and E2-8C Second Pass GPU acceptance are formally PASS. The E2-8C CPU baseline is `260 passed`. - External LATENT nodes may discard custom dictionary metadata. Identically shaped group reordering therefore cannot be cryptographically detected; count, position, T, geometry, and audio-shape checks are enforced. -Updated: 2026-08-25 +Updated: 2026-08-26 + +## V3.5.2 final release gate (2026-08-26) + +- V3.5.2 is the release-candidate baseline for the Stabilization & Optimization Update. It adds no generation mode and preserves all V3.5.x workflow contracts plus V3.4 Node IDs/backend socket keys. +- Phase 3-1 Prompt/CLIP cross-run cache and Phase 4-6A Video Guide memory optimization are the only adopted Production optimizations. Sampling, Conditioning payloads, Terminal Merge, Assembly, Seam, Run Storage, and standard ComfyUI Seed behavior remain unchanged. +- Final pre-release T2VA API smoke at 576x576 / 1x5 seconds / 20 steps confirmed MISS `encode_calls=1` and HIT `encode_calls=0`; Prompt/CLIP time changed from `8.454306s` to `0.000038s`. Both runs used derived Sampling seed `5684542245315818407` and produced identical decoded video MD5 `40e378d7c80e6bebd9d64b322c13e677` and audio PCM MD5 `4fb0a699227c4f834f7a52ba4d395ae8`. +- The accepted Video Guide GPU A/B remains the Phase 4-4 result: full source SHA-256, identity, 124-frame prefix, VAE/Qwen inputs, Sampling seed, decoded video MD5, and audio PCM MD5 all match exactly between old and optimized preprocessing. +- Final post-sync gate is PASS: `485 passed`, compileall exit `0`, V3.5.2 runtime verifier PASS, all 172 Manifest hashes valid, `git diff --check` exit `0`, required node registration complete, and the API queue empty. ComfyUI_W restarted with `H3 Continuum 3.5.2 loaded`; source, ComfyUI_W, and ComfyUI_WAN hashes match for `version.py`, `metadata.ini`, and the accepted `reference_video.py`. +- Public result image is `docs/images/v352-stabilization-optimization.png`, copied bit-exact from the user-supplied `D:\SD_PIC\Neo\Clip_91.png` (SHA-256 `0c1dfd5acff349b98ecdfb57ae50941f6da26a462dfd2aaa9b7c7b0a34abf304`). README text is the canonical environment description where the supplied image contains a different ComfyUI minor-version label. +- Exact release-preparation snapshot: `pre-v352-release-preparation-exact-20260826_022016`, revision `b8ab766eb364063f8af88781e7be70ee7286fba0`, 198 source files plus the four active runtime metadata files. + +## V3.5.1 stabilization Phase 4-6A Video Guide memory optimization (2026-08-26) + +- Phase 4 Feature Tax & Redundancy Audit is PASS. No evidence-backed Production speed optimization was selected. Driving Audio generated-audio Decode/Assembly and same-rate Audio hashing remain documented `SIMPLIFY` candidates below the 1% adoption threshold; Sampling, Session/State validation, Refine Context, Run Storage, Assembly Plan validation, and V3.4 remain unchanged. +- Phase 4-6A is formally PASS, including Phase 4-4 GPU acceptance. `reference_video.py` now normalizes, finite-checks, and hashes the complete Video Guide in eight-frame CPU float32 RGB chunks while retaining only the prefix used by the one-chunk H3 conditioning contract. Full source shape/dtype/raw-byte order and SHA-256 remain exact, so unused-tail changes and non-finite values still affect identity/validation. +- The primary ComfyUI_W runtime contained a pre-existing uncommitted Video Guide preprocess v3 that was newer than the repository v2. With explicit approval, its H3 `17k+5` frame alignment and final-frame padding behavior was integrated into the authoritative source before applying the memory optimization. The active v3 contract, source SHA-256, combined hash, frame count, and Run Storage identity are preserved by dedicated regressions. +- Deterministic 300x256x256 RGB / 124-frame-prefix comparison against the pre-change ComfyUI_W v3 produced the same `source_sha256` and complete contract. Preprocessing peak RSS above the already allocated input fell from `396.758 MiB` to `114.695 MiB`; retained Video Guide storage fell from `225.0 MiB` / 300 frames to `93.0 MiB` / 124 frames. Host time was `0.246672s` versus `0.257602s`, so this is accepted as a memory optimization rather than a speed claim. +- Dedicated Video Guide plus existing Driving Audio regressions are `17 passed`; canonical full pytest is `485 passed`. Source runtime verification, 171 Manifest hashes, syntax, and `git diff --check` pass. Only the known `pynvml` and inaccessible repository pytest-cache warnings remain. +- Phase 4-4 compared the pre-change v3 implementation and the optimized implementation through the normal ComfyUI API with the same 15-second / 360-frame 800x800 Video Guide, V3.5 one-chunk 5-second workflow, 576x576 output, 20 steps, seed input `46001`, and Hi-Res Fix disabled. Both resolved the guide to 640x640 / 124 frames, used one physical group and the same derived Sampling seed `3288707263902886519`, and assembled 124->120 frames at 24 fps / 5.000 seconds with 32 kHz stereo audio. +- The actual VHS-loaded source SHA-256 is `ea03aa0cb216213b3a7320f7fa262f226e8e9075d31f69bdb184eec7ea189f5d`; old/new combined identity, 124-frame prefix, 640x640 VAE input, and Qwen input are byte-identical. Decoded video MD5 is `53cd381aff184c2f8aa5910ab62be342` and decoded audio PCM MD5 is `3653c0ec89947a928075d4ba056ff624` on both paths. Representative frames show no gross artifact or collapse. Runtime was restored to the optimized file and its SHA-256 matches source (`a0266ab4434b8e9844219d2b6dd8a168d198dd8f142775be01a39b81dc196b91`). +- Accepted pre-change snapshot is `pre-phase4-6a-video-guide-memory-exact-20260826_012227` (6 exact source/runtime files, revision `b8ab766eb364063f8af88781e7be70ee7286fba0`). The standard full snapshot attempt stopped only on the known ACL-inaccessible `.pytest_cache`. + +## V3.5.1 stabilization Phase 3-2 synchronized GPU rebaseline (2026-08-26) + +- Phase 3-2 is formally PASS and is measurement-only. No Production node, schema, Sampling/Conditioning payload, Terminal Merge, Assembly, Seam, Run Storage, or V3.4 behavior changed. Diagnostics run from repository tools against the normal ComfyUI API and are not installed into `custom_nodes`. +- After one excluded warm-up and with the Phase 3-1 Prompt/CLIP cache already hot (`encode_calls=0`), the three-run API medians are `168.069s` for 1x5 T2VA at 576x576 and `379.765s` for 3x5 FL2VA Long Terminal Merge at 640x640. Synchronized in-process medians are respectively `166.986s` / `378.107s`, with Sampling `120.516s` / `295.216s`, Video VAE Decode `19.154s` / `51.634s`, Audio VAE Decode `0.466s` / `1.040s`, Assemble + Seam `0.130s` / `0.619s`, and MP4 save `2.583s` / `7.248s`. +- True whole-workflow PyTorch CUDA allocator peaks are `1.456 GiB` T2VA and `4.129 GiB` FL2VA. They are allocator observations, not total device VRAM usage and do not include every model-weight, ComfyUI staging, driver, or external allocation. Separate per-node peak runs confirm Sampling as the CUDA maximum; Video VAE Decode peaks are `0.290 GiB` / `0.319 GiB`. Whole-workflow Windows RSS/private peaks are about `29.15/45.53 GiB` T2VA and `30.82/46.18 GiB` FL2VA. These process peaks include Core/model staging and mapped/output pages and are attribution data, not Continuum-retained memory claims. +- T2VA external-attention API medians are: Sage `168.069s`; Sage + Sol-Attn `162.090s` (`-3.56%`); Sage + Spectrum `124.830s` (`-25.73%`); Sage + Sol-Attn + Spectrum `121.147s` (`-27.92%`). Sol's sparse Triton kernel was verified active with zero fallback after moving its writable cache to the test evidence root. Spectrum emitted its Continuum API acceptance marker. These routes may change floating-point results and are not bit-exact Continuum A/B claims. +- The fastest combined route also passed one measured FL2VA Long Terminal Merge run at `288.548s` API (`-24.02%` versus the Sage median): 3 logical / 2 physical groups (`[1]`, `[2,3]`), terminal seed/trim contract intact, 640x640, 360 frames, 24 fps, 15.000s, and audio present. All measured T2VA routes produced 576x576, 120 frames, 24 fps, 5.000s, and audio. Representative contact-sheet review found no gross line/plate artifact or collapse; this does not turn the external plugins into Continuum-owned quality guarantees. +- The new rebaseline leaves no evidence-backed Continuum Production optimization candidate for Phase 3-3. Sampling is dominant but contract-sensitive; Video/Audio VAE Decode and MP4 encoding are external Core/downstream work; Assemble + Seam is below the 1% adoption threshold. Phase 3-1 remains frozen rather than broadening its cache scope. +- Reusable diagnostics are `tools/p32_profile_server.py`, `tools/p32_api_runner.py`, and `tools/p32_gpu_profile_probe.py`. Accepted evidence is under `D:\Codex\_test_results\ComfyUI-H3-Continuum\stabilization-phase3-2-20260825`. The accepted pre-change snapshot is `pre-stabilization-phase3-2-profile-accepted-20260825_220920` (479 files, revision `b8ab766eb364063f8af88781e7be70ee7286fba0`). +- Phase 3-2 focused validation is `13 passed`; canonical full pytest is `480 passed`. Diagnostic-file `py_compile`, all 170 Manifest hashes, source runtime verification, and `git diff --check` pass. Only the known `pynvml` FutureWarning remains. + +## V3.5.1 stabilization Phase 3-1 Prompt/CLIP cross-run cache (2026-08-25) + +- V3.5 now enables one private, bounded Prompt/CLIP conditioning cache. V3.4 public schema and execution remain unchanged. The cache belongs to the exact CLIP object, holds at most 16 LRU entries, and keys prompt text, exact resized First/Last image fingerprints, Last Frame presence, CLIP patch UUID, and CLIP layer. +- Reference Image, Reference Audio, Video Reference, Timeline Video, CLIP schedules, tokenizer options, and hook-modified CLIP states conservatively bypass the cache. Any cache error falls back to a normal encode and does not stop generation. Cached tensor payloads may be shared, but mutable metadata containers are copied for every consumer. CUDA-resident conditioning is never stored. +- Detailed Report records `hits`, `misses`, `bypasses`, and the actual Prompt/CLIP `encode_calls`. CPU regressions cover bit-exact payloads, mutable metadata isolation, every key invalidation, special-state bypass, CUDA-output rejection, fail-soft behavior, and 17th-entry LRU eviction. +- GPU/API 1x5 T2VA at 576x576 passed. MISS was `encode_calls=1`, Prompt/CLIP `5.898398s`; HIT was `encode_calls=0`, Prompt/CLIP `0.000028s`. Both same-seed outputs contain the same 120 decoded frame MD5 sequence and identical decoded audio PCM MD5. A separate base-seed-only change also remained a HIT with `encode_calls=0`, produced a different derived Sampling seed, and completed at 120 frames / 5.000s. +- GPU/API 3x5 FL2VA Long Terminal Merge at 640x640 passed with 3 logical / 2 physical groups (`[1]`, `[2,3]`). MISS was `encode_calls=4`, Prompt/CLIP `21.642687s`; HIT was `encode_calls=0`, Prompt/CLIP `0.007128s`. Both same-seed outputs contain the same 360 decoded frame MD5 sequence, identical decoded audio PCM MD5, and 15.000s duration. A separate base-seed-only change also retained four HITs / zero encode calls and the same physical grouping with newly derived physical seeds. +- No retained CUDA regression was observed: HIT completion CUDA allocated was lower than the adjacent MISS in both cases, and retained Refine Context GPU / observed GPU storage remained zero. The warm FL2VA HIT completed with about 21.3 MiB higher RSS than the adjacent MISS, consistent with the intentionally retained four-entry CPU conditioning cache and bounded by the 16-entry LRU. +- Focused CPU/static validation is `25 passed`; canonical full pytest is `467 passed`. Syntax, source/runtime verifier, source/runtime SHA-256 equality for the five changed Production files, and `git diff --check` pass. Accepted evidence is under `D:\Codex\_test_results\ComfyUI-H3-Continuum\stabilization-phase3-1-20260825`. + +## V3.5.1 stabilization Phase 2-2 Conditioning attribution (2026-08-25) + +- Extended the existing V3.5 Detailed Report timing only; no node, socket, UI, Sampling, Conditioning payload, Terminal Merge, Assembly, Seam, or Run Storage contract changed. Basic/Off reports and every V3.4 path remain unchanged. +- Conditioning is now split into First/Last image VAE, Reference Image VAE, Reference Audio VAE, Driving Audio VAE, Video Reference VAE, Timeline Video VAE, and Prompt/CLIP. Unconnected paths report `n/a`, and counts distinguish an absent path from a measured near-zero path. Timing remains `time.perf_counter()` only with no CUDA synchronization or allocator mutation. +- CPU/static validation is `29 passed` focused and `453 passed` full. The first full run was blocked only by the existing Windows ACL on pytest's default temp root; the clean rerun with an explicit writable `--basetemp` passed. The remaining warnings are the known `pynvml` deprecation and inaccessible repository `.pytest_cache` cache-write warning. +- Accepted GPU/API 1x5 T2VA at 576x576 took 158.701s API / 135.970s Sampler host wall. Conditioning was 6.004392s, of which Prompt/CLIP was 6.004374s; identity video VAE was 0.000006s. A preceding warm-up independently measured 6.283131s Conditioning / 6.283109s Prompt/CLIP. A cached 3.398s replay is explicitly excluded. +- Accepted 3x5 FL2VA Long Terminal Merge at 640x640 took 410.727s API / 346.197s Sampler host wall and preserved 3 logical / 2 physical groups (`[1]`, `[2,3]`), 360 frames, 15.000s, and Exact Duration `362 -> 360`. Conditioning was 23.644903s: Prompt/CLIP 22.873708s (96.7%) and First/Last image VAE 0.771176s (3.3%). Sampling remained dominant at 322.335226s. +- No optimization is adopted. The measured Continuum bookkeeping remains negligible, and Prompt/CLIP time is required Core model encoding across the distinct physical prompts, including the Terminal Merge prompt. Reference-specific paths are instrumented but were not connected in these accepted runs, so their GPU cost remains unmeasured rather than assumed. +- Accepted evidence is under `D:\Codex\_test_results\ComfyUI-H3-Continuum\stabilization-phase2-2-20260825`. + +## V3.5.1 stabilization Phase 2-1 performance attribution (2026-08-25) + +- Added measurement-only host timing to the existing V3.5 Detailed Report path. V3.4, Basic/Off reports, node schemas, UI, Sampling inputs/outputs, Terminal Merge, Assembly, Seam, and Run Storage contracts are unchanged. +- The report records total Preparation with Conditioning as an identified subset; each physical group's preparation, Sampling, CPU commit/finite validation, and Refine Context capture; plus Finalization and total host wall time. Terminal Merge is reported in physical order, so a 3-logical FL2VA run remains two measured groups: `[1]` and `[2,3]`. +- Timing uses `time.perf_counter()` only. It does not call `torch.cuda.synchronize()`, reset allocator peaks, clear caches, or change the normal asynchronous execution contract. Sampling values are host-observed elapsed time, not CUDA-event kernel timing. +- Core Video/Audio VAE Decode remains external and is not attributed to the Sampler. V3.5 Assembly continues to report its existing workflow elapsed time separately. +- CPU/static validation is `27 passed` focused and `451 passed` full with only the known `pynvml` FutureWarning. Runtime verification and source/runtime hashes pass. +- GPU/API measurement is PASS on the RTX 5060 Ti 16 GB system. Warm 1x5 T2VA at 576x576 took 143.888s API wall time; Sampler host wall was 134.736s, including 128.737s Sampling (95.5%), 5.963s Conditioning (4.4%), 0.013s CPU commit/finite validation, and 0.001s Refine Context capture. Output is 576x576, 120 frames, 5.000s, 32 kHz. +- The 3x5 FL2VA Long Terminal Merge run took 396.477s API wall time and retained 3 logical / 2 physical groups (`[1]`, `[2,3]`). Sampler host wall was 348.047s: 323.835s Sampling (93.0%), 24.003s Conditioning (6.9%), 0.050s CPU commit/finite validation, and 0.010s Refine Context capture. Output is 640x640, 360 frames, 15.000s, 32 kHz. +- The 1x5 Hi-Res Fix 1.2x run took 330.005s and produced 704x704, 120 frames, 5.000s, 32 kHz. Against the warm non-Hi-Res baseline, the integrated Resize/Second Pass/Decode path added about 186.1s, 0.49 GiB peak Working Set, and 1.34 GiB peak Private Bytes. Global observed VRAM usage was about 15.4-15.6 GiB in all three runs and is not a per-node allocator measurement. +- No optimization has been selected or implemented. Sampling dominates and the measured CPU commit, finite validation, Refine Context, and group preparation paths are rejected as optimization targets. Conditioning exceeds the 1% investigation threshold but must be split into CLIP/VAE/reference subphases before any change is justified. +- Accepted evidence is under `D:\Codex\_test_results\ComfyUI-H3-Continuum\stabilization-phase2-20260825`. The first 1x5 attempt reached an older already-running ComfyUI process and is retained only as excluded diagnostic evidence; all accepted runs used the restarted updated listener. ## V3.5.1 stabilization Phase 0 CI portability candidate (2026-08-25) @@ -66,7 +131,7 @@ Updated: 2026-08-25 ## Baseline -- Current public baseline: V3.5.1 +- Release-candidate baseline: V3.5.2 (published baseline remains V3.5.1 until commit/push) - Authoritative working repository: `D:\Codex\_git_push_work\ComfyUI-H3-Continuum` - Primary tested runtime: `D:\StabilityMatrix\Data\Packages\ComfyUI_W\custom_nodes\ComfyUI-H3-Continuum` - Secondary runtime: `D:\StabilityMatrix\Data\Packages\ComfyUI_WAN\custom_nodes\ComfyUI-H3-Continuum` diff --git a/README.md b/README.md index 9caf598..31937a8 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,46 @@ -# ComfyUI-H3-Continuum 3.5.1 - -## V3.5.1 Emergency Compatibility Hotfix - -Reference Audio sockets are now permanently defined by Python `INPUT_TYPES`. Dynamic socket changes and the UI-only `Hidden / Show` control were removed to prevent workflow save/reload value shifts. - -Node IDs, backend keys, Sampling, Conditioning, Seed handling, and other widgets are unchanged. +# ComfyUI-H3-Continuum 3.5.2 Long-form MiniMax H3 video generation for ComfyUI with Continuum-aware Second Pass refinement, optional Hi-Res Fix, low-memory disk-backed assembly, restartable chunks, persistent references, and optional Spectrum interoperability. -> **V3.5.1 is the current release.** All V3.4 node IDs and backend socket keys remain registered intentionally for saved-workflow compatibility. Existing V3.4/V3.5 workflows continue to load; V3.5.1 clarifies the displayed input names without rewriting saved links. +## V3.5.2 Stabilization & Optimization Update -## What's new in V3.5.1 +V3.5.2 adds no new generation mode. It stabilizes the V3.5.x release, removes repeated Prompt/CLIP work when safe, and lowers temporary memory for long Video Guide inputs while preserving existing workflow and generation contracts. -V3.5.1 completes two focused additions without changing the V3.4 Sampling, Conditioning, Terminal Merge, Assembly, Seam, or Run Storage contracts: +![H3 Continuum V3.5.2 stabilization and optimization results](docs/images/v352-stabilization-optimization.png) -1. **Optional Reference Audio** — native H3 audio conditioning that does not replace the generated final audio. Both sockets remain permanently visible to preserve workflow save/reload compatibility. -2. **Conditioning Bridge V3.5** — exposes one complete Core-compatible `MODEL` and one complete `CONDITIONING` object per Continuum physical group for an external sampler workflow. +The figure records the accepted optimization measurements. The final packaging gate additionally passed on ComfyUI 0.33.3 with a 172-entry Manifest, including this image. + +### What changed + +1. **Repeated-run Prompt/CLIP cache** — unchanged T2VA, I2VA, and FL2VA conditioning can reuse CPU-cached Prompt/CLIP results. Reference, scheduled, tokenizer-option, and hook-modified CLIP paths conservatively bypass the cache. Sampling still runs normally. +2. **Video Guide memory optimization** — the complete source remains part of finite validation and SHA-256 identity, but only the required prefix remains stored for VAE/conditioning work. +3. **Stabilization audit** — measured Sampling, Decode, Assembly, saving, optional-feature tax, and retained memory. No speculative Sampling, Driving Audio, Audio hash, Assembly, Seam, Session, or V3.4 optimization was adopted. + +### Measured results + +| Area | Before | V3.5.2 | Result | +|---|---:|---:|---:| +| T2VA repeated Prompt/CLIP | 5.898398 s | 0.000028 s | Cache HIT, `encode_calls=0` | +| FL2VA repeated Prompt/CLIP | 21.642687 s | 0.007128 s | Cache HIT, `encode_calls=0` | +| Video Guide peak additional RSS | 396.8 MiB | 114.7 MiB | About 71% lower | +| Video Guide retained storage | 225 MiB | 93 MiB | About 59% lower | + +Prompt/CLIP figures measure only the conditioning subphase, not total generation time. The Video Guide memory benchmark uses deterministic 300×256×256 RGB input with a 124-frame required prefix. A separate GPU A/B used an actual 15-second / 360-frame Video Guide and produced identical decoded video and audio PCM. + +The measured Sage-only production baselines on the tested RTX 5060 Ti 16 GB / 64 GB system were 168.069 seconds for 1×5-second 576×576 T2VA and 379.765 seconds for 3×5-second 640×640 FL2VA Long Terminal Merge. These are configuration-specific baselines, not universal speed guarantees. Sampling remained the dominant cost; Continuum Assemble + Seam stayed below 1%. + +> **V3.5.2 is the current release.** V3.4 node IDs, backend socket keys, and saved-workflow loading remain intentionally supported. Sampling, Conditioning, Terminal Merge, Assembly, Seam, Run Storage, and standard ComfyUI seed behavior are unchanged. The withdrawn experimental Last Queued Seed override is not included. + +## V3.5.1 Reference Audio & Compatibility Update + +V3.5.1 added two focused features without changing the V3.4 Sampling, Conditioning, Terminal Merge, Assembly, Seam, or Run Storage contracts: + +1. **Optional Reference Audio** — native H3 audio conditioning that does not replace the generated final audio. +2. **Conditioning Bridge V3.5** — one complete Core-compatible `MODEL` and `CONDITIONING` object per Continuum physical group for external sampler workflows. + +The Reference Audio sockets are permanently defined by Python `INPUT_TYPES`. Dynamic socket changes and the UI-only `Hidden / Show` control were removed to prevent workflow save/reload value shifts. Node IDs, backend keys, Sampling, Conditioning, standard Seed handling, and other widgets are unchanged. + +All V3.4 node IDs and backend socket keys remain registered intentionally for saved-workflow compatibility. Existing V3.4/V3.5 workflows continue to load; V3.5.1 clarified the displayed input names without rewriting saved links. ### Reference inputs at a glance @@ -576,7 +601,7 @@ Use a 24 fps source for `Video Guide Frames`. `Load Video (Upload)` may accept f ## Current validation status -The V3.5.1 release passes `452` automated tests, source/runtime registration verification, native PackedLayout, Fixed 3x5 prompt planning, JavaScript UI harnesses, and release metadata consistency. Representative GPU acceptance includes the V3.5 Conditioning Bridge path, Reference Audio/Image retention, Second Pass/Hi-Res paths, and accepted 3x5 FL2VA Long Terminal Merge latents through Core Video/Audio VAE Decode and Assemble V3.5 Auto. +The V3.5.2 release passes `485` automated tests, source/runtime registration verification, native PackedLayout, Fixed 3x5 prompt planning, JavaScript UI harnesses, release metadata consistency, Prompt/CLIP MISS-to-HIT output equality, and Video Guide bit-exact GPU A/B acceptance. Earlier representative GPU acceptance also covers the V3.5 Conditioning Bridge path, Reference Audio/Image retention, Second Pass/Hi-Res paths, and accepted 3x5 FL2VA Long Terminal Merge latents through Core Video/Audio VAE Decode and Assemble V3.5 Auto. V3.4 compatibility paths have been exercised locally with: diff --git a/README_JA.md b/README_JA.md index 9cea8f8..8f28901 100644 --- a/README_JA.md +++ b/README_JA.md @@ -1,16 +1,45 @@ -# ComfyUI-H3-Continuum 3.5.1 - -**V3.5.1正式版:** V3.4互換を維持し、Reference AudioとConditioning Bridgeを追加します。V3.4のNode IDとbackend socket keyは保存済みワークフロー互換のため意図的に残しており、表示名を変更しても既存V3.4/V3.5ワークフローの接続は維持されます。 +# ComfyUI-H3-Continuum 3.5.2 MiniMax H3を複数チャンクで連続生成し、直前チャンク末尾の**映像latent / 音声latentを直接**次チャンクへ継承するComfyUIカスタムノードです。チャンク間でVideo/Audio VAEのDecode→Encodeは行いません。 -## V3.5.1の追加機能 +## V3.5.2 Stabilization & Optimization Update -V3.5.1では、V3.4のSampling、Conditioning、Terminal Merge、Assembly、Seam、Run Storage契約を変更せず、次の2点を追加します。 +V3.5.2は新しい生成モードを追加する版ではありません。V3.5.xを安定化し、安全な条件で繰り返しPrompt/CLIP処理を省略し、長いVideo Guide入力の一時メモリを削減します。既存Workflowと生成契約は維持します。 + +![H3 Continuum V3.5.2の安定化・最適化結果](docs/images/v352-stabilization-optimization.png) + +画像は受入済み最適化の実測値です。最終パッケージゲートでは、さらにComfyUI 0.33.3と、この画像を含む172項目のManifestでPASSを確認しています。 + +### 主な変更 + +1. **繰り返し実行Prompt/CLIP cache**: 条件が変わらないT2VA、I2VA、FL2VAではCPU上のPrompt/CLIP結果を再利用できます。Reference、schedule、tokenizer option、hook変更CLIPは安全側でbypassします。Sampling自体は通常どおり再実行します。 +2. **Video Guideメモリ最適化**: source全体をfinite検査とSHA-256 identityへ含めたまま、VAE/conditioningに必要なprefixだけを保持します。 +3. **安定化監査**: Sampling、Decode、Assembly、保存、任意機能の負荷、保持memoryを実測しました。根拠のないSampling、Driving Audio、Audio hash、Assembly、Seam、Session、V3.4最適化は採用していません。 + +### 実測結果 + +| 項目 | 変更前 | V3.5.2 | 結果 | +|---|---:|---:|---:| +| T2VA繰り返しPrompt/CLIP | 5.898398秒 | 0.000028秒 | Cache HIT、`encode_calls=0` | +| FL2VA繰り返しPrompt/CLIP | 21.642687秒 | 0.007128秒 | Cache HIT、`encode_calls=0` | +| Video Guide Peak追加RSS | 396.8 MiB | 114.7 MiB | 約71%削減 | +| Video Guide保持Storage | 225 MiB | 93 MiB | 約59%削減 | + +Prompt/CLIPの数値はconditioning区間だけで、総生成時間ではありません。Video Guideメモリ比較は300×256×256 RGB入力/必要prefix 124 framesの固定CPU条件です。別のGPU A/Bでは実際の15秒/360-frame Video Guideを使い、decoded videoとaudio PCMの完全一致を確認しています。 + +RTX 5060 Ti 16 GB/RAM 64 GBの検証環境で測定したSage-only Production baselineは、576×576 T2VA 1×5秒が168.069秒、640×640 FL2VA Long Terminal Merge 3×5秒が379.765秒です。環境・設定固有の測定値であり、すべての環境に対する速度保証ではありません。Samplingが最大コストで、Continuum Assemble + Seamは1%未満でした。 + +**V3.5.2が現在の正式版です。** V3.4 Node ID、backend socket key、保存済みWorkflow読込は維持します。Sampling、Conditioning、Terminal Merge、Assembly、Seam、Run Storage、ComfyUI標準Seed動作は変更していません。撤回済みの実験的Last Queued Seed overrideは含まれていません。 + +## V3.5.1 Reference Audio/互換性更新 + +V3.5.1ではV3.4のSampling、Conditioning、Terminal Merge、Assembly、Seam、Run Storage契約を変更せず、次の2点を追加しました。 1. **Reference Audio(任意)**: 生成音声を置換しない、H3 nativeの音声conditioningです。Workflowの保存・再読込互換を守るため、2つのsocketは常時表示します。 2. **Conditioning Bridge V3.5**: Continuumのphysical groupごとに、完全なCore互換`MODEL`と`CONDITIONING`を外部Samplerへ公開します。 +Reference Audio socketはPython `INPUT_TYPES`で常設し、保存・再読込時の値ずれを防ぐため動的socket変更とUI-onlyの`Hidden`/`Show` controlを削除しました。V3.4のNode IDとbackend socket keyは保存済みWorkflow互換のため維持しており、既存V3.4/V3.5 Workflowの接続は変わりません。 + ### Reference系入力の違い 表示名は役割の違いを示しています。名前が似ていても同じ組み合わせではありません。 @@ -221,7 +250,7 @@ ZIPを展開して、`ComfyUI-H3-Continuum`フォルダーを`ComfyUI/custom_nod ## 検査 -V3.5.1正式版は全`452 passed`、source/runtime登録検証、PackedLayout、Fixed 3×5 Prompt Plan、JavaScript UI harnessを通過しています。代表GPU受入にはConditioning Bridge、Reference Audio/Image保持、Second Pass/Hi-Res経路、3×5秒FL2VA Long Terminal MergeからCore Video/Audio VAE Decode、Assemble V3.5 Autoまでを含みます。 +V3.5.2正式版は全`485 passed`、source/runtime登録検証、PackedLayout、Fixed 3×5 Prompt Plan、JavaScript UI harness、release metadata、Prompt/CLIP MISS→HIT出力一致、Video Guide bit-exact GPU A/Bを通過しています。以前の代表GPU受入にはConditioning Bridge、Reference Audio/Image保持、Second Pass/Hi-Res経路、3×5秒FL2VA Long Terminal MergeからCore Video/Audio VAE Decode、Assemble V3.5 Autoまでを含みます。 Main Hi-Res Fixの3×5秒2xは、RTX 5060 Ti 16 GiBで37T groupの1152×1152 Second Pass完了後、Terminal Mergeの77T group最初の推論時にCUDA OOMとなり未受入です。Reference/Hybrid固有の1×5秒Second Passは受入済みですが、長尺Reference/Hybridは未確認です。Disk-backedが保証する低メモリ範囲はContinuum Assemblyであり、Core Decodeや下流ノードが別の全量copyを作る可能性は残ります。 diff --git a/WORKLOG.md b/WORKLOG.md index 211600e..d0f8ee1 100644 --- a/WORKLOG.md +++ b/WORKLOG.md @@ -1,5 +1,82 @@ # Work Log +## 2026-08-26 - V3.5.2 README final audit and publication authorization + +- Re-audited English/Japanese README chapter order, historical/current-version wording, withdrawn controls, local links, image references, and validation claims before publication. +- Moved the Japanese product summary to the document opening and merged the duplicated English V3.5.1 Hotfix/What's New sections into one chronological `Reference Audio & Compatibility Update` section. +- Explicitly documented that the withdrawn experimental Last Queued Seed override is not included and that V3.5.2 retains standard ComfyUI Seed behavior. +- Kept the user-supplied infographic unchanged and added a precise caption distinguishing its accepted measurement labels from the final ComfyUI 0.33.3 / 172-entry packaging gate. +- Regenerated the 172-entry Manifest and verified all local README links. Post-audit full pytest is `485 passed`; Manifest, runtime verifier, node registration, source/runtime hashes, queue state, and `git diff --check` remain PASS. +- The user explicitly authorized committing and pushing the complete V3.5.2 release state to the main repository after this audit. + +## 2026-08-26 - V3.5.2 final release gate + +- Completed the final Stabilization & Optimization release gate without adding a generation mode or changing V3.4/V3.5 Sampling, Conditioning payloads, Terminal Merge, Assembly, Seam, Run Storage, or standard ComfyUI Seed behavior. +- Created exact pre-change snapshot `pre-v352-release-preparation-exact-20260826_022016` at revision `b8ab766eb364063f8af88781e7be70ee7286fba0`, containing 198 authoritative files plus the four active ComfyUI_W/ComfyUI_WAN metadata files. The standard snapshot attempt stopped only on the known repository `.pytest_cache` ACL. +- Ran a final 576x576 / 1x5-second / 20-step T2VA API cache smoke before the release metadata change. MISS reported one encode call and `8.454306s` Prompt/CLIP; HIT reported zero encode calls and `0.000038s`. Both used derived seed `5684542245315818407` and produced identical decoded video MD5 `40e378d7c80e6bebd9d64b322c13e677` and audio PCM MD5 `4fb0a699227c4f834f7a52ba4d395ae8`. +- Reused the accepted Phase 4-4 Video Guide GPU A/B because no Production path changed afterward: complete source identity, 124-frame prefix, VAE/Qwen inputs, Sampling seed, decoded video, and audio PCM remain exact between legacy-v3 and optimized preprocessing. +- Bumped package, project, and runtime metadata to `3.5.2`; added the Stabilization & Optimization sections to English/Japanese README and Changelog; removed the stale withdrawn Last Queued Seed claim from package information; and updated package validation to the canonical `485 passed` result. +- Copied the user-supplied infographic `D:\SD_PIC\Neo\Clip_91.png` to `docs/images/v352-stabilization-optimization.png` bit-exact (SHA-256 `0c1dfd5acff349b98ecdfb57ae50941f6da26a462dfd2aaa9b7c7b0a34abf304`) and embedded it near the top of both READMEs. +- Synchronized only `version.py` and `metadata.ini` to ComfyUI_W and ComfyUI_WAN; all four destination hashes match the source. ComfyUI_W restarted successfully and logged `H3 Continuum 3.5.2 loaded` on ComfyUI `0.33.3`. +- Final validation: compileall exit `0`; full pytest `485 passed` with only the known `pynvml` FutureWarning; V3.5.2 runtime verifier PASS; all 172 Manifest hashes PASS; five required V3.5 nodes registered; source/runtime hashes PASS; queue `0/0`; and `git diff --check` PASS. Commit, push, and release were intentionally not performed at this gate. + +## 2026-08-26 - Stabilization Phase 4-6A Video Guide memory optimization + +- Completed the read-only Phase 4 Feature Tax & Redundancy Audit and selected only Video Guide long-input memory as an implementation candidate. No Production Sampling speed change, Driving Audio lazy Decode, Audio hash special case, Session/State relaxation, Refine Context change, or V3.4 change was adopted. +- The mandatory standard snapshot stopped on the known `.pytest_cache` ACL. Created verified exact snapshot `pre-phase4-6a-video-guide-memory-exact-20260826_012227` with six authoritative/runtime files at revision `b8ab766eb364063f8af88781e7be70ee7286fba0`. +- Pre-change verification found that ComfyUI_W alone contained an unrecorded Video Guide preprocess v3, while the repository and ComfyUI_WAN still had v2-equivalent content. With explicit approval, preserved and integrated the active v3 `17k+5` alignment and final-frame padding behavior into the repository before optimizing memory. +- Replaced whole-source float32 RGB materialization, finite scan, and one-shot `bytes` hashing with eight-frame normalization/validation/hash updates. The source object now owns only the used prefix. The complete normalized source still contributes to the exact same SHA-256 and unused-tail NaN/Inf detection. +- Deterministic 300x256x256 RGB comparison against the original ComfyUI_W v3 retained an identical full contract and hash. Peak RSS above the allocated input changed from `396.758 MiB` to `114.695 MiB`, retained storage from `225.0 MiB` to `93.0 MiB`, and host time from `0.246672s` to `0.257602s`. +- Added five CPU regressions for legacy-v3 hash byte order, full-tail identity, unused-tail finite rejection, prefix ownership, frame alignment/padding, and encoding behavior. Focused result is `17 passed`; full result is `485 passed`. Source runtime verification and all 171 Manifest entries pass. +- Completed Phase 4-4 GPU A/B through the normal API using the same 15-second / 360-frame 800x800 Video Guide, V3.5 one-chunk workflow, 576x576 output, 20 steps, seed input `46001`, Sage Attention, and Hi-Res Fix disabled. Legacy and optimized paths both used one physical group, guide contract 640x640 / 124 frames, derived Sampling seed `3288707263902886519`, exact-duration 124->120 frames, 24 fps / 5.000 seconds, and 32 kHz stereo audio. +- Reloaded the exact VHS input independently and confirmed old/new source SHA-256 and combined identity, retained 124-frame prefix, resized VAE input, and Qwen input are byte-identical. Decoded video MD5 and audio PCM MD5 are also identical (`53cd381aff184c2f8aa5910ab62be342` / `3653c0ec89947a928075d4ba056ff624`). Representative start/middle/end frames show no gross artifact. Phase 4-6A is formally accepted; runtime was restored to the optimized source-identical file and the API queue is empty. + +## 2026-08-26 - Stabilization Phase 3-2 synchronized GPU rebaseline + +- Phase 3-2 is formally PASS. The accepted decision is to make no speculative Phase 3-3 Production optimization: measured Continuum Assembly is negligible, Core VAE/MP4 work is external, and existing Spectrum/Sol-Attn routes provide materially larger gains without changing Continuum's Sampling contract. +- Final-pass documentation snapshot: `pre-phase3-2-formal-pass-docs-20260826_004900` (2 files, revision `b8ab766eb364063f8af88781e7be70ee7286fba0`). +- Preserved all Phase 3-1 and earlier uncommitted work. Accepted snapshot `pre-stabilization-phase3-2-profile-accepted-20260825_220920` contains 479 files at revision `b8ab766eb364063f8af88781e7be70ee7286fba0`; the standard snapshot attempt stopped only on the known ACL-inaccessible `.pytest_cache`. +- Added repository-only API diagnostics that synchronize CUDA before/after measured nodes, separate true whole-workflow allocator peaks from per-node reset peaks, poll lightweight RSS/private and NVML free VRAM during each interval, and sample USS at interval boundaries. Early probes that queried CUDA from a polling thread or called `memory_full_info()` at 10 Hz measurably slowed Sampling and are explicitly excluded. +- Rebaselined Phase 3-1 cache-HIT Production behavior after an excluded warm-up and three measured runs. API medians are T2VA `168.069s` and FL2VA `379.765s`; synchronized phase medians show Sampling `120.516s` / `295.216s`, Video Decode `19.154s` / `51.634s`, Audio Decode `0.466s` / `1.040s`, Assembly `0.130s` / `0.619s`, and MP4 save `2.583s` / `7.248s`. +- Whole-workflow CUDA allocator peaks are `1.456 GiB` T2VA and `4.129 GiB` FL2VA. Per-node runs independently identify Sampling as the maximum. The FL2VA contract remained 3 logical / 2 physical groups (`[1]`, `[2,3]`), 360 frames, 24 fps, 15.000s; T2VA remained 120 frames / 5.000s. +- Compared external attention routes on T2VA with one warm-up excluded and three-run API medians: Sage `168.069s`; Sage + Sol-Attn `162.090s`; Sage + Spectrum `124.830s`; Sage + Sol-Attn + Spectrum `121.147s`. The initial Sol attempt failed only because the diagnostic process could not write its default Triton cache and was interrupted/excluded. The accepted rerun used a writable test cache, logged active sparse kernels, and had zero fallback. +- The fastest combined route additionally passed one measured FL2VA run at `288.548s` API with 3 logical / 2 physical groups, terminal seed/trim unchanged, 640x640, 360 frames, 24 fps, 15.000s, and audio. `ffprobe` validated every selected output; contact sheets showed no gross persistent artifact. External accelerator outputs are intentionally not required to be MD5-identical. +- No Continuum Production optimization was adopted. Sampling remains contract-sensitive, Core VAE Decode and MP4 save are external, and Assemble + Seam is below the 1% threshold. The diagnostic wrapper was removed from the running process and the normal ComfyUI API was restored at `127.0.0.6:8188` with output `D:\output\video\comfy_video`. No Production runtime synchronization was required. Evidence is under `D:\Codex\_test_results\ComfyUI-H3-Continuum\stabilization-phase3-2-20260825`; commit/push were not performed. +- Focused diagnostics are `13 passed`; full pytest is `480 passed`. Diagnostic `py_compile`, all 170 Manifest hashes, source runtime verification, and `git diff --check` pass with only the known `pynvml` FutureWarning. + +## 2026-08-25 - Stabilization Phase 3-1 Prompt/CLIP cross-run cache + +- Created exact source/runtime snapshot `pre-stabilization-phase3-1-prompt-cache-20260825_211303` with 19 files and preserved all earlier uncommitted stabilization changes. +- Added a V3.5-only private Prompt/CLIP conditioning cache attached to the exact CLIP object. It is capped at 16 LRU entries and invalidates on prompt, exact resized First/Last pixels/shape/dtype, Last Frame presence, patch UUID, or CLIP layer. V3.4, public schemas, Sampling, Terminal Merge, Assembly, Seam, and Run Storage were not changed. +- Conservatively bypassed Reference Image/Audio/Video/Timeline and CLIP schedule/tokenizer/hook states. Cache exceptions and CUDA-resident outputs fall back to uncached encode. Tensor payloads are shared only when CPU-resident; metadata dict/list/tuple containers are copied per use. +- Added Detailed Report evidence with real `hits/misses/bypasses/encode_calls`. Dedicated and integrated CPU result is `25 passed`; full pytest is `467 passed` with only the known `pynvml` and inaccessible repository pytest-cache warnings. +- Synchronized only `v2/h3_builder.py`, `v2/sequence.py`, `v2/nodes.py`, `v3/nodes.py`, and `v3/driving_nodes.py` to ComfyUI_W; every SHA-256 matched. ComfyUI_W was restarted with its recorded arguments before GPU tests. +- GPU/API T2VA A/B PASS: 576x576, 120 frames / 5.000s. Prompt/CLIP changed from `5.898398s` (`misses=1`, `encode_calls=1`) to `0.000028s` (`hits=1`, `encode_calls=0`). Decoded video frame MD5 sequence and decoded audio PCM MD5 are identical. A separate base-seed-only change produced a new derived Sampling seed while retaining one HIT / zero encode calls. +- GPU/API FL2VA Long Terminal Merge A/B PASS: 640x640, 360 frames / 15.000s, 3 logical / 2 physical groups (`[1]`, `[2,3]`). Prompt/CLIP changed from `21.642687s` (`misses=4`, `encode_calls=4`) to `0.007128s` (`hits=4`, `encode_calls=0`). Decoded video frame MD5 sequence and decoded audio PCM MD5 are identical. A separate base-seed-only change retained four HITs / zero encode calls, changed both normal and terminal physical seeds, and preserved the group contract. +- No CUDA retention regression was observed. FL2VA warm completion RSS was 6095.8 MiB MISS versus 6117.1 MiB HIT, a bounded approximately 21.3 MiB CPU cost for four retained conditioning entries. Evidence is under `D:\Codex\_test_results\ComfyUI-H3-Continuum\stabilization-phase3-1-20260825`. Commit and push were not performed. + +## 2026-08-25 - Stabilization Phase 2-2 Conditioning attribution + +- Created exact source/runtime snapshot `pre-stabilization-phase2-2-conditioning-20260825_200800` with 15 files including snapshot metadata. Existing uncommitted Phase 2-1 and all prior V3.5.1 work were preserved. +- Split Detailed Report Conditioning timing into identity video VAE, Reference Image/Audio VAE, Driving Audio VAE, Video/Timeline Reference VAE, and Prompt/CLIP. The instrumentation is inactive outside the existing V3.5 Detailed Report path and never synchronizes CUDA. +- Focused validation is `29 passed`; full validation is `453 passed` using a writable explicit pytest `--basetemp`. The initial full attempt produced setup errors only because the default `pytest-of-links` directory is ACL-inaccessible. Manifest, syntax, and related regressions pass. +- Synchronized only `v2/sequence.py`, `v3/memory_attribution.py`, the two related test files, and `MANIFEST.sha256` to ComfyUI_W; source/runtime SHA-256 matched before GPU execution. +- GPU/API 1x5 T2VA accepted result: 576x576, 120 frames / 5.000s, 158.701s API, 135.970s Sampler host wall, 129.935s Sampling, 6.004392s Conditioning, and 6.004374s Prompt/CLIP. Warm-up independently measured 6.283131s Conditioning and 6.283109s Prompt/CLIP. One 3.398s execution-cache replay was excluded. +- GPU/API 3x5 FL2VA Long Terminal Merge accepted result: 640x640, 3 logical / 2 physical groups (`[1]`, `[2,3]`), 360 frames / 15.000s, 410.727s API, 346.197s Sampler host wall, 322.335s Sampling, 23.644903s Conditioning, 22.873708s Prompt/CLIP, and 0.771176s First/Last image VAE. Decode and Assemble completed with Exact Duration `362 -> 360`. +- No optimization was implemented. Prompt/CLIP accounts for effectively all T2VA Conditioning and 96.7% of FL2VA Conditioning; the remaining measured Continuum-side work is not an optimization target. Reference-specific branches remain instrumented but require separate connected GPU evidence before any conclusion. +- Evidence is under `D:\Codex\_test_results\ComfyUI-H3-Continuum\stabilization-phase2-2-20260825`. Commit and push were not performed. + +## 2026-08-25 - Stabilization Phase 2-1 performance attribution instrumentation + +- Attempted the mandatory full snapshot first; it stopped only on the known ACL-inaccessible `.pytest_cache`. Accepted exact-file snapshot `pre-stabilization-phase2-exact-20260825_190303` contains nine source/runtime target files at revision `b8ab766eb364063f8af88781e7be70ee7286fba0`, with source/runtime Production hashes equal before modification. +- Extended the existing V3.5 Detailed Report collector with non-synchronizing host timings for Preparation/Conditioning, physical-group preparation/Sampling/CPU commit plus finite validation/Refine Context capture, and Finalization. No new node, socket, widget, schema, or normal-report overhead was added. +- Terminal Merge regression confirms exactly two performance group records for logical `[1]` and `[2,3]`. Dedicated tests also prove the collector never calls CUDA synchronization. +- Focused CPU/static validation is `27 passed`; full validation is `451 passed`; only the known `pynvml` FutureWarning remains. Runtime verification, Manifest validation, `py_compile`, `git diff --check`, and source/runtime SHA-256 equality pass. +- The first API run accidentally reached the pre-existing 18:46 ComfyUI listener; the newly started process had exited on port conflict. That result is explicitly excluded. After confirming an empty queue, the exact old listener PID was stopped and the synchronized runtime was restarted with the recorded Stability Matrix arguments. +- Accepted GPU/API measurements: warm 1x5 T2VA 576x576 = 143.888s API / 134.736s Sampler host wall (Sampling 128.737s); 3x5 FL2VA Long Terminal Merge 640x640 = 396.477s API / 348.047s Sampler host wall (Sampling 323.835s, physical `[1]` + `[2,3]`); 1x5 Hi-Res 1.2x = 330.005s and 704x704. All outputs are 24 fps with exact 5.000s or 15.000s duration and 32 kHz audio. +- Warm baseline versus Hi-Res peak Working Set was 6.882 versus 7.374 GiB; peak Private Bytes was 20.655 versus 21.998 GiB. The 3x5 peaks were 9.409 GiB Working Set and 21.159 GiB Private Bytes. Global VRAM used peaked around 15.4-15.6 GiB and is recorded only as a process-external observation. +- Sampling accounts for 93.0-95.5% of measured Sampler time. CPU commit/finite validation, Refine Context, and group preparation are below the adoption threshold and will not be optimized. Conditioning is 4.4-6.9% and is eligible only for finer attribution, not an optimization change. Evidence is under `D:\Codex\_test_results\ComfyUI-H3-Continuum\stabilization-phase2-20260825`. + ## 2026-08-25 - Stabilization Phase 0 local CI portability fix - Froze V3.5.1 feature work and began stabilization with a test-only change. Production Sampling, Layout, Session, Run Storage, Refine Context, UI, and runtime files were not changed. diff --git a/docs/images/v352-stabilization-optimization.png b/docs/images/v352-stabilization-optimization.png new file mode 100644 index 0000000..693ed55 Binary files /dev/null and b/docs/images/v352-stabilization-optimization.png differ diff --git a/metadata.ini b/metadata.ini index ff7d5a7..3f21d1d 100644 --- a/metadata.ini +++ b/metadata.ini @@ -1,4 +1,4 @@ [project] name = H3 Continuum -version = 3.5.1 +version = 3.5.2 description = Native MiniMax H3 continuation with Second Pass, experimental Hi-Res Fix, and disk-backed assembly. diff --git a/pyproject.toml b/pyproject.toml index 8bd197a..5610cfd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "comfyui-h3-continuum" -version = "3.5.1" +version = "3.5.2" description = "Integrated N-chunk continuation for ComfyUI native MiniMax H3 video and audio" readme = "README.md" requires-python = ">=3.10" diff --git a/reference_video.py b/reference_video.py index 420e115..3e7c391 100644 --- a/reference_video.py +++ b/reference_video.py @@ -10,9 +10,11 @@ from typing import Any import torch +from .temporal import align_frame_count_up + REFERENCE_VIDEO_CONTRACT_VERSION = 1 -REFERENCE_VIDEO_PREPROCESS_VERSION = 2 +REFERENCE_VIDEO_PREPROCESS_VERSION = 3 REFERENCE_VIDEO_SIZE_EFFICIENT = "Efficient - 0.4 MP" REFERENCE_VIDEO_SIZE_BALANCED = "Balanced - 0.6 MP" REFERENCE_VIDEO_SIZE_MATCH_OUTPUT = "Match Output" @@ -25,6 +27,7 @@ _CANVAS_MULTIPLE = 32 _EFFICIENT_AREA = 400_000 _BALANCED_AREA = 600_000 _SOURCE_FPS = 24 +_HASH_CHUNK_FRAMES = 8 class ReferenceVideoError(ValueError): @@ -39,11 +42,15 @@ def _canonical_hash(value: Any) -> str: def _tensor_hash(value: torch.Tensor) -> str: - tensor = value.detach().to(device="cpu").contiguous() + tensor = value.detach() digest = hashlib.sha256() digest.update(str(tuple(int(v) for v in tensor.shape)).encode("ascii")) digest.update(str(tensor.dtype).encode("ascii")) - digest.update(tensor.numpy().tobytes(order="C")) + for start in range(0, int(tensor.shape[0]), _HASH_CHUNK_FRAMES): + chunk = tensor[start : start + _HASH_CHUNK_FRAMES].to( + device="cpu" + ).contiguous() + digest.update(chunk.numpy().tobytes(order="C")) return digest.hexdigest() @@ -121,6 +128,66 @@ class ReferenceVideoAssets: block: dict[str, Any] +def _resolve_reference_frame_count(source_frames: int, target_frames: int) -> int: + source_count = int(source_frames) + target_count = int(target_frames) + if source_count < 5 or target_count < 5: + raise ReferenceVideoError("Video Reference requires at least 5 frames") + target_cap = align_frame_count_up(target_count) + available_count = min(source_count, target_cap) + return min(align_frame_count_up(available_count), target_cap) + + +def _fit_reference_frames(frames: torch.Tensor, frame_count: int) -> torch.Tensor: + requested_count = int(frame_count) + current_count = int(frames.shape[0]) + if current_count >= requested_count: + return frames[:requested_count].contiguous() + if current_count < 1: + raise ReferenceVideoError("Video Reference contains no frames") + padding = frames[-1:].expand(requested_count - current_count, -1, -1, -1) + return torch.cat((frames, padding), dim=0).contiguous() + + +def _prepare_reference_frames( + value: torch.Tensor, + *, + frame_count: int, +) -> tuple[torch.Tensor, tuple[int, ...], str, str]: + """Hash the full normalized source while retaining only its used prefix.""" + + source_shape = ( + int(value.shape[0]), + int(value.shape[1]), + int(value.shape[2]), + 3, + ) + source_dtype = str(torch.float32) + retained_count = min(int(source_shape[0]), int(frame_count)) + retained = torch.empty( + (retained_count, *source_shape[1:]), + dtype=torch.float32, + device="cpu", + ) + digest = hashlib.sha256() + digest.update(str(source_shape).encode("ascii")) + digest.update(source_dtype.encode("ascii")) + + for start in range(0, source_shape[0], _HASH_CHUNK_FRAMES): + stop = min(start + _HASH_CHUNK_FRAMES, source_shape[0]) + chunk = value[start:stop, :, :, :3].detach().to( + device="cpu", dtype=torch.float32 + ).contiguous() + if not bool(torch.isfinite(chunk).all().item()): + raise ReferenceVideoError("Video Reference contains NaN or infinity") + digest.update(chunk.numpy().tobytes(order="C")) + if start < retained_count: + retained_stop = min(stop, retained_count) + retained[start:retained_stop].copy_(chunk[: retained_stop - start]) + + return retained, source_shape, source_dtype, digest.hexdigest() + + def prepare_reference_video_source( reference_video_1: Any, *, @@ -137,26 +204,22 @@ def prepare_reference_video_source( ) if int(reference_video_1.shape[-1]) < 3: raise ReferenceVideoError("Video Reference must contain RGB channels") - frames = reference_video_1[:, :, :, :3].detach().to( - device="cpu", dtype=torch.float32 - ).contiguous() - if not bool(torch.isfinite(frames).all().item()): - raise ReferenceVideoError("Video Reference contains NaN or infinity") - source_shape = tuple(int(value) for value in frames.shape) - source_sha256 = _tensor_hash(frames) - frame_count = min(int(frames.shape[0]), int(target_frames)) - if frame_count < 5: - raise ReferenceVideoError("Video Reference requires at least 5 frames") - while frame_count > 5 and frame_count % 17 != 5: - frame_count -= 1 + frame_count = _resolve_reference_frame_count( + int(reference_video_1.shape[0]), + int(target_frames), + ) + frames, source_shape, source_dtype, source_sha256 = _prepare_reference_frames( + reference_video_1, + frame_count=frame_count, + ) normalized_size_mode = ( str(size_mode) if str(size_mode) in REFERENCE_VIDEO_SIZE_OPTIONS else REFERENCE_VIDEO_SIZE_EFFICIENT ) target_width, target_height = _resolved_size( - int(frames.shape[2]), - int(frames.shape[1]), + int(source_shape[2]), + int(source_shape[1]), output_width=int(output_width), output_height=int(output_height), size_mode=normalized_size_mode, @@ -164,7 +227,7 @@ def prepare_reference_video_source( contract_base = { "reference_video_contract_version": REFERENCE_VIDEO_CONTRACT_VERSION, "source_shape": list(source_shape), - "source_dtype": str(frames.dtype), + "source_dtype": source_dtype, "source_sha256": source_sha256, "source_fps": _SOURCE_FPS, "size_mode": normalized_size_mode, @@ -176,7 +239,7 @@ def prepare_reference_video_source( return ReferenceVideoSource( frames=frames, source_shape=source_shape, - source_dtype=str(frames.dtype), + source_dtype=source_dtype, source_sha256=source_sha256, size_mode=normalized_size_mode, target_width=target_width, @@ -190,7 +253,8 @@ def encode_reference_video( video_vae: Any, source: ReferenceVideoSource, ) -> ReferenceVideoAssets: - frames = source.frames[: source.frame_count] + available_count = min(int(source.frames.shape[0]), int(source.frame_count)) + frames = source.frames[:available_count] if ( int(frames.shape[2]) != source.target_width or int(frames.shape[1]) != source.target_height @@ -208,6 +272,7 @@ def encode_reference_video( "lanczos", "disabled", ).movedim(1, -1).contiguous() + frames = _fit_reference_frames(frames, source.frame_count) try: latent = video_vae.encode(frames).detach().to("cpu").contiguous() except Exception as exc: diff --git a/tests/test_p21_memory_attribution.py b/tests/test_p21_memory_attribution.py index 942650a..fe738c0 100644 --- a/tests/test_p21_memory_attribution.py +++ b/tests/test_p21_memory_attribution.py @@ -216,6 +216,88 @@ def test_cuda_snapshot_reads_counters_without_allocator_mutation(monkeypatch): assert "CUDA_peak_allocated_since_external_reset=0.0 MiB" in line +def test_performance_attribution_reports_group_and_summary_without_cuda_sync( + monkeypatch, +): + ticks = iter((0.0, 0.0, 1.0, 2.0, 4.0, 5.0, 9.0, 10.0, 11.0, 12.0)) + monkeypatch.setattr(memory.time, "perf_counter", lambda: next(ticks)) + monkeypatch.setattr( + memory.torch.cuda, + "synchronize", + lambda *args, **kwargs: (_ for _ in ()).throw( + AssertionError("performance attribution must not synchronize CUDA") + ), + ) + + collector = memory.MemoryAttributionCollector() + collector.capture_sequence_start() + collector.finish_phase(phase="preparation") + collector.start_phase( + phase="group_prepare", physical_group=1, logical_chunks=(1,) + ) + collector.finish_phase( + phase="group_prepare", physical_group=1, logical_chunks=(1,) + ) + collector.start_phase( + phase="sampling", physical_group=1, logical_chunks=(1,) + ) + collector.finish_phase( + phase="sampling", physical_group=1, logical_chunks=(1,) + ) + collector.start_phase( + phase="cpu_commit_validation", physical_group=1, logical_chunks=(1,) + ) + collector.finish_phase( + phase="cpu_commit_validation", physical_group=1, logical_chunks=(1,) + ) + collector.capture_sequence_complete(entries=[]) + + group_line = next( + line + for line in collector.lines + if line.startswith("Performance [V3.5 physical group 1") + ) + summary = next( + line + for line in collector.lines + if line.startswith("Performance [V3.5 summary]") + ) + assert "group_prepare=2.000000s" in group_line + assert "sampling=4.000000s" in group_line + assert "cpu_commit_validation=1.000000s" in group_line + assert "refine_context=n/a" in group_line + assert "host_wall_including_diagnostics=12.000000s" in summary + assert "preparation=1.000000s" in summary + assert "CUDA_synchronize=not_used" in summary + + +def test_performance_attribution_reports_conditioning_subphases_without_flattening(): + collector = memory.MemoryAttributionCollector() + collector.capture_sequence_start() + collector.start_phase(phase="conditioning") + collector.start_phase(phase="conditioning_identity_video_vae") + collector.finish_phase(phase="conditioning_identity_video_vae") + collector.start_phase(phase="conditioning_reference_audio_vae") + collector.finish_phase(phase="conditioning_reference_audio_vae") + collector.start_phase(phase="conditioning_prompt_clip") + collector.finish_phase(phase="conditioning_prompt_clip") + collector.finish_phase(phase="conditioning") + collector.capture_sequence_complete(entries=[]) + + summary = next( + line + for line in collector.lines + if line.startswith("Performance [V3.5 summary]") + ) + assert "conditioning_identity_video_vae=" in summary + assert "conditioning_reference_audio_vae=" in summary + assert "conditioning_prompt_clip=" in summary + assert "conditioning_reference_image_vae=n/a" in summary + assert "conditioning_identity_video_vae=1" in summary + assert "conditioning_reference_audio_vae=1" in summary + assert "conditioning_prompt_clip=1" in summary + + @pytest.mark.parametrize( "method", ( diff --git a/tests/test_p31_prompt_conditioning_cache.py b/tests/test_p31_prompt_conditioning_cache.py new file mode 100644 index 0000000..1c68426 --- /dev/null +++ b/tests/test_p31_prompt_conditioning_cache.py @@ -0,0 +1,149 @@ +from __future__ import annotations + +from types import SimpleNamespace + +import pytest +import torch + +from ComfyUI_H3_Continuum_Join.v2 import h3_builder + + +class _Clip: + def __init__(self): + self.calls = 0 + self.layer_idx = None + self.use_clip_schedule = False + self.tokenizer_options = {} + self.apply_hooks_to_conds = None + self.patcher = SimpleNamespace( + patches_uuid="patch-a", + forced_hooks=None, + current_hooks=None, + hook_patches={}, + ) + + def tokenize(self, prompt, **kwargs): + return prompt, kwargs + + def encode_from_tokens_scheduled(self, _tokens): + self.calls += 1 + return [[ + torch.tensor([[[1.0, 2.0]]]), + { + "pooled_output": torch.tensor([[3.0]]), + "nested": {"items": ["original"]}, + }, + ]] + + +def _encode(clip, prompt="prompt", first=None, last=None, events=None): + return h3_builder.encode_prompt_conditioning_cached( + clip, + prompt, + first_image=first, + last_image=last, + first_image_fingerprint=h3_builder._tensor_fingerprint(first), + last_image_fingerprint=h3_builder._tensor_fingerprint(last), + cache_enabled=True, + cache_event=None if events is None else events.append, + ) + + +def test_cache_hit_is_bit_exact_and_only_metadata_containers_are_copied(): + clip = _Clip() + first = _encode(clip) + second = _encode(clip) + + assert clip.calls == 1 + assert torch.equal(first[0][0], second[0][0]) + assert first[0][0] is second[0][0] + assert first is not second + assert first[0][1] is not second[0][1] + assert first[0][1]["nested"] is not second[0][1]["nested"] + assert first[0][1]["nested"]["items"] is not second[0][1]["nested"]["items"] + second[0][1]["nested"]["items"].append("mutation") + third = _encode(clip) + assert third[0][1]["nested"]["items"] == ["original"] + + +def test_prompt_first_last_clip_patch_and_layer_changes_are_misses(): + clip = _Clip() + first = torch.zeros((1, 4, 4, 3)) + last = torch.ones((1, 4, 4, 3)) + _encode(clip, first=first, last=last) + _encode(clip, prompt="changed", first=first, last=last) + _encode(clip, prompt="changed", first=first.clone().fill_(0.5), last=last) + _encode(clip, prompt="changed", first=first.clone().fill_(0.5), last=None) + clip.patcher.patches_uuid = "patch-b" + _encode(clip, prompt="changed", first=first.clone().fill_(0.5), last=None) + clip.layer_idx = -2 + _encode(clip, prompt="changed", first=first.clone().fill_(0.5), last=None) + assert clip.calls == 6 + + +def test_image_fingerprint_includes_shape_dtype_and_exact_content(): + base = torch.zeros((1, 4, 4, 3), dtype=torch.float32) + changed = base.clone() + changed[0, 0, 0, 0] = 1.0 + fingerprints = { + h3_builder._tensor_fingerprint(base), + h3_builder._tensor_fingerprint(base.reshape(1, 2, 8, 3)), + h3_builder._tensor_fingerprint(base.to(torch.float64)), + h3_builder._tensor_fingerprint(changed), + } + assert len(fingerprints) == 4 + + +@pytest.mark.parametrize( + "mutate", + [ + lambda clip: setattr(clip, "use_clip_schedule", True), + lambda clip: setattr(clip, "tokenizer_options", {"weight": "custom"}), + lambda clip: setattr(clip, "apply_hooks_to_conds", object()), + lambda clip: setattr(clip.patcher, "forced_hooks", object()), + lambda clip: setattr(clip.patcher, "current_hooks", object()), + lambda clip: setattr(clip.patcher, "hook_patches", {"hook": object()}), + ], +) +def test_special_clip_modes_bypass_cache(mutate): + clip = _Clip() + events = [] + mutate(clip) + _encode(clip, events=events) + _encode(clip, events=events) + assert clip.calls == 2 + assert events == ["bypass_special_clip", "bypass_special_clip"] + + +def test_lru_keeps_only_sixteen_entries_and_evicts_oldest(): + clip = _Clip() + for index in range(17): + _encode(clip, prompt=f"prompt-{index}") + cache = getattr(clip, h3_builder._PROMPT_CACHE_ATTR) + assert len(cache) == 16 + assert clip.calls == 17 + _encode(clip, prompt="prompt-0") + assert clip.calls == 18 + assert len(cache) == 16 + + +def test_cuda_conditioning_is_not_retained(monkeypatch): + clip = _Clip() + events = [] + monkeypatch.setattr(h3_builder, "_contains_cuda_tensor", lambda _value: True) + _encode(clip, events=events) + _encode(clip, events=events) + assert clip.calls == 2 + assert events == ["bypass_cuda_output", "bypass_cuda_output"] + cache = getattr(clip, h3_builder._PROMPT_CACHE_ATTR) + assert len(cache) == 0 + + +def test_cache_container_failure_falls_back_to_uncached_encode(): + clip = _Clip() + events = [] + setattr(clip, h3_builder._PROMPT_CACHE_ATTR, {}) + _encode(clip, events=events) + _encode(clip, events=events) + assert clip.calls == 2 + assert events == ["bypass_cache_error", "bypass_cache_error"] diff --git a/tests/test_p32_api_runner.py b/tests/test_p32_api_runner.py new file mode 100644 index 0000000..ce83618 --- /dev/null +++ b/tests/test_p32_api_runner.py @@ -0,0 +1,35 @@ +from __future__ import annotations + +import importlib.util +from pathlib import Path + + +MODULE_PATH = Path(__file__).parents[1] / "tools" / "p32_api_runner.py" +SPEC = importlib.util.spec_from_file_location("h3_p32_api_runner", MODULE_PATH) +assert SPEC is not None and SPEC.loader is not None +runner = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(runner) + + +def test_history_complete_accepts_completed_status(): + assert runner._history_complete( + {"abc": {"status": {"completed": True}, "outputs": {}}}, "abc" + ) + assert not runner._history_complete({}, "abc") + + +def test_history_error_extracts_execution_failure(): + history = { + "abc": { + "status": { + "messages": [ + ["execution_start", {"prompt_id": "abc"}], + ["execution_error", {"exception_message": "bad"}], + ] + } + } + } + value = runner._history_error(history, "abc") + assert value is not None + assert "execution_error" in value + assert runner._history_error({}, "abc") is None diff --git a/tests/test_p32_gpu_profile_probe.py b/tests/test_p32_gpu_profile_probe.py new file mode 100644 index 0000000..1758b41 --- /dev/null +++ b/tests/test_p32_gpu_profile_probe.py @@ -0,0 +1,141 @@ +from __future__ import annotations + +import importlib.util +from pathlib import Path + +import pytest + + +MODULE_PATH = Path(__file__).parents[1] / "tools" / "p32_gpu_profile_probe.py" +SPEC = importlib.util.spec_from_file_location("h3_p32_profile_probe", MODULE_PATH) +assert SPEC is not None and SPEC.loader is not None +probe = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(probe) + + +def _base_prompt(): + return { + "1": {"class_type": "UNETLoader", "inputs": {}}, + "2": { + "class_type": "MiniMaxH3MemoryEfficientSageAttentionPatch", + "inputs": {"model": ["1", 0]}, + }, + "3": { + "class_type": "SpectrumApplyMiniMaxH3", + "inputs": {"model": ["2", 0], "enabled": False}, + }, + "4": { + "class_type": "H3ContinuumSamplerV35", + "inputs": {"model": ["3", 0], "base_seed": 1}, + }, + "5": { + "class_type": "SaveVideo", + "inputs": {"filename_prefix": "original", "video": ["4", 0]}, + }, + } + + +@pytest.mark.parametrize( + ("profile", "spectrum_enabled", "has_sol"), + ( + ("sage", False, False), + ("sage_sol", False, True), + ("sage_spectrum", True, False), + ("sage_sol_spectrum", True, True), + ), +) +def test_configure_profile_builds_exact_route(profile, spectrum_enabled, has_sol): + source = _base_prompt() + result = probe.configure_profile(source, profile) + assert source["3"]["inputs"]["enabled"] is False + assert result["3"]["inputs"]["enabled"] is spectrum_enabled + sol_nodes = probe._find_nodes(result, "SolAttnPatch") + assert bool(sol_nodes) is has_sol + if has_sol: + sol_id = sol_nodes[0] + assert result[sol_id]["inputs"]["model"] == ["2", 0] + assert result["3"]["inputs"]["model"] == [sol_id, 0] + assert result[sol_id]["inputs"]["verbose"] is True + else: + assert result["3"]["inputs"]["model"] == ["2", 0] + + +def test_mutate_run_changes_only_seed_and_filename(): + source = _base_prompt() + result = probe.mutate_run(source, seed=99, filename_prefix="p32/test") + assert source["4"]["inputs"]["base_seed"] == 1 + assert source["5"]["inputs"]["filename_prefix"] == "original" + assert result["4"]["inputs"]["base_seed"] == 99 + assert result["5"]["inputs"]["filename_prefix"] == "p32/test" + + +def test_summarize_runs_uses_median_and_keeps_missing_cuda_peak(): + runs = [ + { + "elapsed_seconds": 9.0, + "phase_totals_seconds": {"sampling": 7.0, "video_vae_decode": 1.0}, + "resources": { + "peak_rss_bytes": 90, + "peak_private_bytes": 70, + "peak_uss_bytes": 60, + "minimum_cuda_free_bytes": 40, + }, + "cuda_global_peak_allocated_bytes": None, + }, + { + "elapsed_seconds": 5.0, + "phase_totals_seconds": {"sampling": 3.0, "video_vae_decode": 0.5}, + "resources": { + "peak_rss_bytes": 50, + "peak_private_bytes": 30, + "peak_uss_bytes": 20, + "minimum_cuda_free_bytes": 10, + }, + "cuda_global_peak_allocated_bytes": None, + }, + { + "elapsed_seconds": 7.0, + "phase_totals_seconds": {"sampling": 5.0, "video_vae_decode": 0.8}, + "resources": { + "peak_rss_bytes": 70, + "peak_private_bytes": 50, + "peak_uss_bytes": 40, + "minimum_cuda_free_bytes": 20, + }, + "cuda_global_peak_allocated_bytes": None, + }, + ] + result = probe.summarize_runs(runs) + assert result["median_elapsed_seconds"] == 7.0 + assert result["median_phase_seconds"]["sampling"] == 5.0 + assert result["median_peak_rss_bytes"] == 70.0 + assert result["median_peak_private_bytes"] == 50.0 + assert result["median_peak_uss_bytes"] == 40.0 + assert result["median_cuda_global_peak_allocated_bytes"] is None + + +def test_phase_classification_is_explicit(): + assert probe._phase_for_class("H3ContinuumSamplerV35") == "sampling" + assert probe._phase_for_class("VAEDecode") == "video_vae_decode" + assert probe._phase_for_class("VAEDecodeAudio") == "audio_vae_decode" + assert probe._phase_for_class("H3ContinuumAssembleSeamV35") == "assemble_seam" + assert probe._phase_for_class("CreateVideo") == "video_create" + assert probe._phase_for_class("SaveVideo") == "mp4_encode_save" + assert probe._phase_for_class("UnknownNode") == "other" + + +def test_resource_monitor_does_not_poll_cuda_runtime(): + source = MODULE_PATH.read_text(encoding="utf-8") + assert "torch.cuda.mem_get_info" not in source.replace( + '``torch.cuda.mem_get_info``', "" + ) + assert "pynvml.nvmlDeviceGetMemoryInfo" in source + + +def test_resource_monitor_does_not_poll_full_process_info(): + source = MODULE_PATH.read_text(encoding="utf-8") + sample_body = source.split("def _sample(self) -> None:", 1)[1].split( + "def _run(self) -> None:", 1 + )[0] + assert "_memory_snapshot()" not in sample_body + assert "self.process.memory_info()" in sample_body diff --git a/tests/test_p32_profile_server_static.py b/tests/test_p32_profile_server_static.py new file mode 100644 index 0000000..f7ee450 --- /dev/null +++ b/tests/test_p32_profile_server_static.py @@ -0,0 +1,32 @@ +from pathlib import Path + + +SERVER_PATH = Path(__file__).parents[1] / "tools" / "p32_profile_server.py" + + +def test_phase32_api_profiler_is_diagnostic_only_and_synchronized(): + source = SERVER_PATH.read_text(encoding="utf-8") + assert "torch.cuda.synchronize()" in source + assert "torch.cuda.reset_peak_memory_stats()" in source + assert 'PROFILE_MODE == "global"' in source + assert 'PROFILE_MODE == "interval"' in source + assert "original_execute_async" in source + assert "original_get_output_data" in source + assert "NODE_CLASS_MAPPINGS" not in source + assert "class H3" not in source + + +def test_phase32_api_profiler_records_required_pipeline_phases(): + source = SERVER_PATH.read_text(encoding="utf-8") + ( + Path(__file__).parents[1] / "tools" / "p32_gpu_profile_probe.py" + ).read_text(encoding="utf-8") + for value in ( + "phase_totals_seconds", + "cuda_global_peak_allocated_bytes", + "peak_private_bytes", + "peak_uss_bytes", + "minimum_cuda_free_bytes", + "route_observed", + "spectrum_enabled", + ): + assert value in source diff --git a/tests/test_p46a_reference_video_memory.py b/tests/test_p46a_reference_video_memory.py new file mode 100644 index 0000000..f0ea91f --- /dev/null +++ b/tests/test_p46a_reference_video_memory.py @@ -0,0 +1,100 @@ +from __future__ import annotations + +import hashlib + +import pytest +import torch + +from ComfyUI_H3_Continuum_Join import reference_video + + +def _legacy_v3_hash(value: torch.Tensor) -> str: + normalized = value[:, :, :, :3].detach().to( + device="cpu", dtype=torch.float32 + ).contiguous() + digest = hashlib.sha256() + digest.update(str(tuple(int(item) for item in normalized.shape)).encode("ascii")) + digest.update(str(normalized.dtype).encode("ascii")) + digest.update(normalized.numpy().tobytes(order="C")) + return digest.hexdigest() + + +def _source(value: torch.Tensor, *, target_frames: int = 22): + return reference_video.prepare_reference_video_source( + value, + target_frames=target_frames, + output_width=32, + output_height=32, + ) + + +def test_chunked_source_hash_is_exactly_legacy_v3_for_full_normalized_input(): + torch.manual_seed(4601) + value = torch.rand((43, 32, 48, 4), dtype=torch.float64) + + source = _source(value) + + assert source.source_sha256 == _legacy_v3_hash(value) + assert source.source_shape == (43, 32, 48, 3) + assert source.source_dtype == "torch.float32" + assert source.contract["preprocess_version"] == 3 + + +def test_only_used_prefix_storage_is_retained_but_full_tail_remains_in_identity(): + torch.manual_seed(4602) + value = torch.rand((80, 32, 32, 3), dtype=torch.float32) + changed_tail = value.clone() + changed_tail[-1, 0, 0, 0] += 0.25 + + original = _source(value) + changed = _source(changed_tail) + + assert original.frame_count == 22 + assert original.frames.shape == (22, 32, 32, 3) + assert original.frames.untyped_storage().nbytes() == original.frames.numel() * 4 + assert torch.equal(original.frames, value[:22]) + assert torch.equal(original.frames, changed.frames) + assert original.source_sha256 != changed.source_sha256 + assert original.combined_hash != changed.combined_hash + assert reference_video.combine_reference_video_identity("visual", original) != ( + reference_video.combine_reference_video_identity("visual", changed) + ) + + +def test_nonfinite_value_in_unused_tail_is_still_rejected(): + value = torch.zeros((80, 32, 32, 3), dtype=torch.float32) + value[-1, 0, 0, 0] = torch.nan + + with pytest.raises(reference_video.ReferenceVideoError, match="NaN or infinity"): + _source(value) + + +def test_short_source_keeps_v3_alignment_and_final_frame_padding_contract(): + value = torch.arange(6 * 32 * 32 * 3, dtype=torch.float32).reshape( + 6, 32, 32, 3 + ) + source = _source(value, target_frames=124) + captured = {} + + class VideoVAE: + def encode(self, frames): + captured["frames"] = frames.clone() + return torch.zeros((1, 24, 7, 2, 2), dtype=torch.float32) + + assets = reference_video.encode_reference_video(VideoVAE(), source) + + assert source.frame_count == 22 + assert source.frames.shape[0] == 6 + assert captured["frames"].shape == (22, 32, 32, 3) + assert torch.equal(captured["frames"][:6], value) + assert torch.equal( + captured["frames"][6:], value[-1:].expand(16, -1, -1, -1) + ) + assert assets.block["latent_t"] == 7 + + +def test_chunked_tensor_hash_matches_legacy_byte_order(): + torch.manual_seed(4603) + value = torch.rand((19, 7, 9, 3), dtype=torch.float32) + + assert reference_video._tensor_hash(value) == _legacy_v3_hash(value) diff --git a/tests/test_release_metadata.py b/tests/test_release_metadata.py index 4ef9daf..fb3fcbe 100644 --- a/tests/test_release_metadata.py +++ b/tests/test_release_metadata.py @@ -6,7 +6,7 @@ import tomllib ROOT = Path(__file__).resolve().parents[1] -EXPECTED_VERSION = "3.5.1" +EXPECTED_VERSION = "3.5.2" def test_v35_release_metadata_is_consistent(): diff --git a/tests/test_v217_stable_facade.py b/tests/test_v217_stable_facade.py index 96ad021..12c71b5 100644 --- a/tests/test_v217_stable_facade.py +++ b/tests/test_v217_stable_facade.py @@ -145,4 +145,4 @@ def test_v217_result_pack_expands_legacy_outputs(): def test_v217_has_no_frontend_display_controller(): assert not (ROOT / "web" / "h3_continuum_v2.js").exists() version_source = (ROOT / "version.py").read_text(encoding="utf-8") - assert 'PACKAGE_VERSION = "3.5.1"' in version_source + assert 'PACKAGE_VERSION = "3.5.2"' in version_source diff --git a/tests/test_v35_context_sampler.py b/tests/test_v35_context_sampler.py index f23ece9..e51051b 100644 --- a/tests/test_v35_context_sampler.py +++ b/tests/test_v35_context_sampler.py @@ -35,10 +35,24 @@ class _Nested: class _Clip: + def __init__(self): + self.encode_calls = 0 + self.layer_idx = None + self.use_clip_schedule = False + self.tokenizer_options = {} + self.apply_hooks_to_conds = None + self.patcher = SimpleNamespace( + patches_uuid="test-patch-a", + forced_hooks=None, + current_hooks=None, + hook_patches={}, + ) + def tokenize(self, prompt, **_kwargs): return prompt def encode_from_tokens_scheduled(self, _tokens): + self.encode_calls += 1 return [[torch.zeros((1, 1, 2)), {}]] @@ -73,6 +87,8 @@ def _capture_sequence( session=None, diagnostics=DIAGNOSTICS_BASIC, memory_attribution=False, + prompt_conditioning_cache=False, + clip=None, ): width, height = 96, 64 first_image = torch.zeros((1, height, width, 3)) if terminal else None @@ -134,7 +150,7 @@ def _capture_sequence( ) return sequence.run_sequence( model=_model(), - clip=_Clip(), + clip=clip or _Clip(), video_vae=object(), audio_vae=None, sampler=object(), @@ -159,6 +175,7 @@ def _capture_sequence( reference_audio_vae=reference_audio_vae, capture_refine_context=True, memory_attribution=memory_attribution, + prompt_conditioning_cache=prompt_conditioning_cache, ) @@ -210,6 +227,7 @@ def test_v35_runtime_keeps_driving_audio_fifth_and_context_sixth(monkeypatch): def fake_production_run(_self, **kwargs): captured["capture"] = kwargs["capture_refine_context"] captured["memory"] = kwargs.get("memory_attribution", False) + captured["prompt_cache"] = kwargs.get("prompt_conditioning_cache", False) base = ("video", "audio", {"plan": True}, "status") return (*base, "context") if kwargs["capture_refine_context"] else base @@ -222,6 +240,7 @@ def test_v35_runtime_keeps_driving_audio_fifth_and_context_sixth(monkeypatch): ) assert captured["capture"] is False assert captured["memory"] is False + assert captured["prompt_cache"] is False assert v34_outputs == ( "video", "audio", @@ -237,6 +256,7 @@ def test_v35_runtime_keeps_driving_audio_fifth_and_context_sixth(monkeypatch): ) assert captured["capture"] is True assert captured["memory"] is True + assert captured["prompt_cache"] is True assert outputs == ( "video", "audio", @@ -361,6 +381,41 @@ def test_v35_full_memory_events_follow_physical_terminal_group_order(monkeypatch "Memory [V3.5 physical group 2 logical=[2,3] after CPU commit]", "Memory [V3.5 sequence complete]", ] + performance_lines = [ + line + for line in outputs[3].splitlines() + if line.startswith("Performance [V3.5") + ] + assert len(performance_lines) == 3 + assert performance_lines[0].startswith( + "Performance [V3.5 physical group 1 logical=[1]]" + ) + assert performance_lines[1].startswith( + "Performance [V3.5 physical group 2 logical=[2,3]]" + ) + assert performance_lines[2].startswith("Performance [V3.5 summary]") + assert "sampling=2" in performance_lines[2] + assert "conditioning_identity_video_vae=1" in performance_lines[2] + assert "conditioning_prompt_clip=1" in performance_lines[2] + assert "conditioning_reference_audio_vae=0" in performance_lines[2] + assert "CUDA_synchronize=not_used" in performance_lines[2] + + +def test_v35_reference_audio_conditioning_attribution_is_reported(monkeypatch): + outputs = _capture_sequence( + monkeypatch, + terminal=False, + reference_audio=True, + diagnostics=DIAGNOSTICS_FULL, + memory_attribution=True, + ) + summary = next( + line + for line in outputs[3].splitlines() + if line.startswith("Performance [V3.5 summary]") + ) + assert "conditioning_reference_audio_vae=" in summary + assert "conditioning_reference_audio_vae=1" in summary def test_memory_attribution_is_absent_when_private_flag_is_off(monkeypatch): @@ -373,3 +428,48 @@ def test_memory_attribution_is_absent_when_private_flag_is_off(monkeypatch): assert "Memory [V3.5" not in outputs[3] assert "Memory [sequence start]" in outputs[3] assert "Memory [sequence complete]" in outputs[3] + + +def test_v35_prompt_cache_reports_real_encode_elision_across_runs(monkeypatch): + clip = _Clip() + first = _capture_sequence( + monkeypatch, + terminal=False, + diagnostics=DIAGNOSTICS_FULL, + prompt_conditioning_cache=True, + clip=clip, + ) + assert clip.encode_calls == 1 + assert "Prompt/CLIP cache [V3.5]: hits=0, misses=1, bypasses=0, encode_calls=1." in first[3] + + second = _capture_sequence( + monkeypatch, + terminal=False, + diagnostics=DIAGNOSTICS_FULL, + prompt_conditioning_cache=True, + clip=clip, + ) + assert clip.encode_calls == 1 + assert "Prompt/CLIP cache [V3.5]: hits=1, misses=0, bypasses=0, encode_calls=0." in second[3] + + +def test_v35_terminal_prompt_cache_preserves_physical_prompt_set(monkeypatch): + clip = _Clip() + _capture_sequence( + monkeypatch, + terminal=True, + diagnostics=DIAGNOSTICS_FULL, + prompt_conditioning_cache=True, + clip=clip, + ) + assert clip.encode_calls == 4 + + second = _capture_sequence( + monkeypatch, + terminal=True, + diagnostics=DIAGNOSTICS_FULL, + prompt_conditioning_cache=True, + clip=clip, + ) + assert clip.encode_calls == 4 + assert "Prompt/CLIP cache [V3.5]: hits=4, misses=0, bypasses=0, encode_calls=0." in second[3] diff --git a/tools/p32_api_runner.py b/tools/p32_api_runner.py new file mode 100644 index 0000000..f81494a --- /dev/null +++ b/tools/p32_api_runner.py @@ -0,0 +1,189 @@ +"""Submit warm-up and measured Phase 3-2 workflows through the ComfyUI API.""" + +from __future__ import annotations + +import argparse +import json +from pathlib import Path +import sys +import time +import urllib.error +import urllib.request +import uuid +from typing import Any, Mapping + +TOOLS_ROOT = Path(__file__).resolve().parent +if str(TOOLS_ROOT) not in sys.path: + sys.path.insert(0, str(TOOLS_ROOT)) + +from p32_gpu_profile_probe import configure_profile, mutate_run + + +class ApiFailure(RuntimeError): + pass + + +def _json_request(url: str, payload: Mapping[str, Any] | None = None) -> Any: + data = None + headers = {} + if payload is not None: + data = json.dumps(payload).encode("utf-8") + headers["Content-Type"] = "application/json" + request = urllib.request.Request(url, data=data, headers=headers) + try: + with urllib.request.urlopen(request, timeout=60) as response: + return json.loads(response.read().decode("utf-8")) + except (urllib.error.URLError, TimeoutError, json.JSONDecodeError) as exc: + raise ApiFailure(f"ComfyUI API request failed for {url}: {exc}") from exc + + +def _history_complete(value: Mapping[str, Any], prompt_id: str) -> bool: + entry = value.get(str(prompt_id)) + if not isinstance(entry, Mapping): + return False + status = entry.get("status") + if not isinstance(status, Mapping): + return bool(entry.get("outputs")) + return bool(status.get("completed", False)) + + +def _history_error(value: Mapping[str, Any], prompt_id: str) -> str | None: + entry = value.get(str(prompt_id)) + if not isinstance(entry, Mapping): + return None + status = entry.get("status") + messages = status.get("messages", []) if isinstance(status, Mapping) else [] + for item in messages: + if isinstance(item, list) and item and item[0] in { + "execution_error", + "execution_interrupted", + }: + return json.dumps(item, ensure_ascii=False) + return None + + +def submit_and_wait( + *, + server: str, + prompt: Mapping[str, Any], + client_id: str, + timeout_seconds: float, + poll_seconds: float, +) -> tuple[str, dict[str, Any], float]: + started = time.perf_counter() + response = _json_request( + f"{server.rstrip('/')}/prompt", + {"prompt": dict(prompt), "client_id": str(client_id)}, + ) + prompt_id = str(response.get("prompt_id", "")) + if not prompt_id: + raise ApiFailure(f"ComfyUI did not return a prompt_id: {response}") + while time.perf_counter() - started <= float(timeout_seconds): + history = _json_request( + f"{server.rstrip('/')}/history/{prompt_id}" + ) + error = _history_error(history, prompt_id) + if error is not None: + raise ApiFailure(f"ComfyUI prompt {prompt_id} failed: {error}") + if _history_complete(history, prompt_id): + return prompt_id, dict(history), time.perf_counter() - started + time.sleep(float(poll_seconds)) + raise ApiFailure(f"ComfyUI prompt {prompt_id} timed out") + + +def run(args) -> dict[str, Any]: + source = json.loads(args.prompt.read_text(encoding="utf-8")) + configured = configure_profile(source, args.profile) + args.output_root.mkdir(parents=True, exist_ok=True) + client_id = f"p32-{uuid.uuid4().hex}" + results = [] + total = 1 + int(args.runs) + for index in range(total): + warmup = index == 0 + label = "warmup" if warmup else f"run-{index}" + seed = int(args.seed) + index + prompt = mutate_run( + configured, + seed=seed, + filename_prefix=f"p32/{args.case}_{args.profile}_{label}", + ) + prompt_path = args.output_root / f"prompt_{label}.json" + prompt_path.write_text( + json.dumps(prompt, indent=2, ensure_ascii=False), + encoding="utf-8", + ) + print( + f"P32 API start case={args.case} profile={args.profile} " + f"label={label} seed={seed}", + flush=True, + ) + prompt_id, history, elapsed = submit_and_wait( + server=args.server, + prompt=prompt, + client_id=client_id, + timeout_seconds=args.timeout_seconds, + poll_seconds=args.poll_seconds, + ) + history_path = args.output_root / f"history_{label}.json" + history_path.write_text( + json.dumps(history, indent=2, ensure_ascii=False), + encoding="utf-8", + ) + results.append( + { + "label": label, + "warmup": warmup, + "seed": seed, + "prompt_id": prompt_id, + "api_elapsed_seconds": float(elapsed), + "prompt": str(prompt_path), + "history": str(history_path), + } + ) + print( + f"P32 API complete prompt_id={prompt_id} elapsed={elapsed:.3f}s", + flush=True, + ) + summary = { + "format": "h3-continuum-phase3-2-api-run-v1", + "case": args.case, + "profile": args.profile, + "server": args.server, + "warmup_excluded": True, + "runs": results, + } + (args.output_root / "api_summary.json").write_text( + json.dumps(summary, indent=2, ensure_ascii=False), + encoding="utf-8", + ) + return summary + + +def _parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--server", default="http://127.0.0.6:8188") + parser.add_argument("--prompt", type=Path, required=True) + parser.add_argument("--output-root", type=Path, required=True) + parser.add_argument("--case", required=True) + parser.add_argument( + "--profile", + choices=("sage", "sage_sol", "sage_spectrum", "sage_sol_spectrum"), + required=True, + ) + parser.add_argument("--runs", type=int, default=3) + parser.add_argument("--seed", type=int, default=32000) + parser.add_argument("--timeout-seconds", type=float, default=3600.0) + parser.add_argument("--poll-seconds", type=float, default=2.0) + return parser + + +def main() -> int: + args = _parser().parse_args() + if args.runs < 1: + raise SystemExit("--runs must be at least 1") + print(json.dumps(run(args), ensure_ascii=False)) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/p32_gpu_profile_probe.py b/tools/p32_gpu_profile_probe.py new file mode 100644 index 0000000..ed5f499 --- /dev/null +++ b/tools/p32_gpu_profile_probe.py @@ -0,0 +1,720 @@ +"""Phase 3-2 diagnostic-only ComfyUI workflow profiler. + +This tool runs an API-format prompt through ComfyUI's installed PromptExecutor +in a dedicated process. It does not register a Continuum node or modify the +normal ComfyUI server. CUDA phases are synchronized immediately before and +after each node so GPU work is attributed to the node that launched it. + +Two measurement modes are intentionally separate: + +``global`` + Resets CUDA peak statistics once per workflow execution. The result is + the true process allocator peak for that run. Per-node wall time and + process-memory peaks are still collected, but per-node CUDA peaks are not. + +``interval`` + Resets CUDA peak statistics before every node. This provides isolated + node allocator peaks, but the workflow-level allocator peak is therefore + not meaningful and is reported as unavailable. + +The first execution is a warm-up. Subsequent executions change only the +Continuum base seed and output filename so model/CLIP objects stay warm while +the V3.5 Prompt/CLIP cache remains eligible for a HIT. +""" + +from __future__ import annotations + +import argparse +import asyncio +import copy +import json +import os +from pathlib import Path +import statistics +import sys +import threading +import time +from typing import Any, Mapping + +import psutil +import torch + + +PROFILE_NAMES = ( + "sage", + "sage_sol", + "sage_spectrum", + "sage_sol_spectrum", +) +MEASUREMENT_MODES = ("global", "interval") + +_NVML_LOCK = threading.Lock() +_NVML_HANDLE: Any = None +_NVML_UNAVAILABLE = False + +_SAMPLER_CLASSES = { + "H3ContinuumSamplerV35", + "H3ContinuumSamplerV34", +} +_VIDEO_DECODE_CLASSES = {"VAEDecode", "VAEDecodeTiled"} +_AUDIO_DECODE_CLASSES = {"VAEDecodeAudio"} +_ASSEMBLY_CLASSES = {"H3ContinuumAssembleSeamV35"} +_VIDEO_CREATE_CLASSES = {"CreateVideo"} +_SAVE_CLASSES = {"SaveVideo", "VHS_VideoCombine"} + + +class ProbeFailure(RuntimeError): + pass + + +def _phase_for_class(class_type: str) -> str: + if class_type in _SAMPLER_CLASSES: + return "sampling" + if class_type in _VIDEO_DECODE_CLASSES: + return "video_vae_decode" + if class_type in _AUDIO_DECODE_CLASSES: + return "audio_vae_decode" + if class_type in _ASSEMBLY_CLASSES: + return "assemble_seam" + if class_type in _VIDEO_CREATE_CLASSES: + return "video_create" + if class_type in _SAVE_CLASSES: + return "mp4_encode_save" + return "other" + + +def _memory_snapshot() -> dict[str, int | None]: + result: dict[str, int | None] = { + "rss_bytes": None, + "private_bytes": None, + "uss_bytes": None, + } + try: + info = psutil.Process().memory_full_info() + result["rss_bytes"] = int(getattr(info, "rss", 0)) or None + private = getattr(info, "private", None) + uss = getattr(info, "uss", None) + result["private_bytes"] = None if private is None else int(private) + result["uss_bytes"] = None if uss is None else int(uss) + except Exception: + pass + return result + + +def _nvml_free_bytes() -> int | None: + """Read device-global free VRAM without calling the CUDA runtime. + + Calling ``torch.cuda.mem_get_info`` from the polling thread measurably + serialized H3 Sampling on Windows. NVML observes the device externally + and avoids that profiler-induced slowdown. + """ + + global _NVML_HANDLE, _NVML_UNAVAILABLE + if _NVML_UNAVAILABLE: + return None + try: + import pynvml + + with _NVML_LOCK: + if _NVML_HANDLE is None: + pynvml.nvmlInit() + index = int(torch.cuda.current_device()) if torch.cuda.is_available() else 0 + _NVML_HANDLE = pynvml.nvmlDeviceGetHandleByIndex(index) + return int(pynvml.nvmlDeviceGetMemoryInfo(_NVML_HANDLE).free) + except Exception: + _NVML_UNAVAILABLE = True + return None + + +class ResourceMonitor: + """Poll process and device counters without changing allocator state.""" + + def __init__(self, interval: float = 0.05): + self.interval = float(interval) + self.process = psutil.Process() + self.stop_event = threading.Event() + self.thread: threading.Thread | None = None + self.samples = 0 + self.peak_rss = 0 + self.peak_private: int | None = None + self.peak_uss: int | None = None + self.minimum_system_available = 1 << 63 + self.minimum_cuda_free = 1 << 63 + + def _sample(self) -> None: + try: + # Full process memory inspection is too expensive to poll during + # H3 Sampling on Windows. RSS/private use the cheap counters; + # USS is sampled only at monitor start and stop. + memory = self.process.memory_info() + self.peak_rss = max(self.peak_rss, int(memory.rss)) + private = getattr(memory, "private", None) + if private is not None: + self.peak_private = max(self.peak_private or 0, int(private)) + self.minimum_system_available = min( + self.minimum_system_available, + int(psutil.virtual_memory().available), + ) + free = _nvml_free_bytes() + if free is not None: + self.minimum_cuda_free = min(self.minimum_cuda_free, int(free)) + self.samples += 1 + except Exception: + pass + + def _run(self) -> None: + while not self.stop_event.wait(self.interval): + self._sample() + + def start(self) -> None: + full = _memory_snapshot() + uss = full.get("uss_bytes") + if uss is not None: + self.peak_uss = int(uss) + self._sample() + self.thread = threading.Thread(target=self._run, daemon=True) + self.thread.start() + + def stop(self) -> dict[str, int | None]: + self._sample() + self.stop_event.set() + if self.thread is not None: + self.thread.join(timeout=5.0) + full = _memory_snapshot() + uss = full.get("uss_bytes") + if uss is not None: + self.peak_uss = max(self.peak_uss or 0, int(uss)) + return { + "samples": int(self.samples), + "peak_rss_bytes": int(self.peak_rss), + "peak_private_bytes": self.peak_private, + "peak_uss_bytes": self.peak_uss, + "uss_peak_scope": "monitor_start_end_only", + "minimum_system_available_bytes": ( + None + if self.minimum_system_available == 1 << 63 + else int(self.minimum_system_available) + ), + "minimum_cuda_free_bytes": ( + None + if self.minimum_cuda_free == 1 << 63 + else int(self.minimum_cuda_free) + ), + } + + +class _ExecutionServer: + def __init__(self): + self.client_id = None + self.last_node_id = None + self.messages: list[tuple[str, dict[str, Any], Any]] = [] + + def send_sync(self, event, data, client_id): + self.messages.append((str(event), dict(data), client_id)) + + +def _find_nodes(prompt: Mapping[str, Any], class_type: str) -> list[str]: + return [ + str(node_id) + for node_id, node in prompt.items() + if str(node.get("class_type")) == class_type + ] + + +def _one_node(prompt: Mapping[str, Any], class_type: str) -> str: + values = _find_nodes(prompt, class_type) + if len(values) != 1: + raise ProbeFailure( + f"expected exactly one {class_type} node, found {len(values)}" + ) + return values[0] + + +def _next_node_id(prompt: Mapping[str, Any]) -> str: + numeric = [int(value) for value in prompt if str(value).isdigit()] + return str((max(numeric) if numeric else 0) + 1) + + +def _sol_inputs(model_link: list[Any]) -> dict[str, Any]: + return { + "model": list(model_link), + "tau": 1.3, + "start_percent": 0.2, + "end_percent": 0.9, + "min_tokens": 4096, + "int8_qk": True, + "sink_conditioning": "exact_kv_and_rows", + "morton": False, + "morton_curve": "2d_frame", + "int8_pv": True, + "verbose": True, + "use_tma": False, + "dense_blocks": "", + } + + +def configure_profile(prompt: Mapping[str, Any], profile: str) -> dict[str, Any]: + """Return a copied prompt with one explicit attention/Spectrum route.""" + + if profile not in PROFILE_NAMES: + raise ValueError(f"unknown profile: {profile!r}") + result = copy.deepcopy(dict(prompt)) + sage = _one_node(result, "MiniMaxH3MemoryEfficientSageAttentionPatch") + spectrum = _one_node(result, "SpectrumApplyMiniMaxH3") + if not isinstance(result[sage].get("inputs", {}).get("model"), list): + raise ProbeFailure("Sage node has no MODEL link") + spectrum_inputs = result[spectrum].setdefault("inputs", {}) + upstream = spectrum_inputs.get("model") + if not isinstance(upstream, list) or len(upstream) != 2: + raise ProbeFailure("Spectrum node has no MODEL link") + + use_sol = profile in {"sage_sol", "sage_sol_spectrum"} + use_spectrum = profile in {"sage_spectrum", "sage_sol_spectrum"} + if use_sol: + sol_id = _next_node_id(result) + result[sol_id] = { + "class_type": "SolAttnPatch", + "inputs": _sol_inputs(list(upstream)), + "_meta": {"title": "Patch Sol-Attn"}, + } + spectrum_inputs["model"] = [sol_id, 0] + spectrum_inputs["enabled"] = bool(use_spectrum) + return result + + +def mutate_run( + prompt: Mapping[str, Any], + *, + seed: int, + filename_prefix: str, +) -> dict[str, Any]: + result = copy.deepcopy(dict(prompt)) + sampler_nodes = _find_nodes(result, "H3ContinuumSamplerV35") + if len(sampler_nodes) != 1: + raise ProbeFailure( + f"expected one H3ContinuumSamplerV35, found {len(sampler_nodes)}" + ) + result[sampler_nodes[0]].setdefault("inputs", {})["base_seed"] = int(seed) + save_nodes = _find_nodes(result, "SaveVideo") + if not save_nodes: + save_nodes = _find_nodes(result, "VHS_VideoCombine") + for node_id in save_nodes: + inputs = result[node_id].setdefault("inputs", {}) + if "filename_prefix" in inputs: + inputs["filename_prefix"] = str(filename_prefix) + return result + + +def _route(prompt: Mapping[str, Any], profile: str) -> list[str]: + route = ["MiniMaxH3MemoryEfficientSageAttentionPatch"] + if profile in {"sage_sol", "sage_sol_spectrum"}: + route.append("SolAttnPatch") + if profile in {"sage_spectrum", "sage_sol_spectrum"}: + route.append("SpectrumApplyMiniMaxH3") + return route + + +def _execute_outputs(prompt: Mapping[str, Any]) -> list[str]: + values = _find_nodes(prompt, "SaveVideo") + _find_nodes( + prompt, "VHS_VideoCombine" + ) + if not values: + raise ProbeFailure("prompt has no SaveVideo/VHS output node") + return values + + +def _median_or_none(values: list[float | int | None]) -> float | None: + clean = [float(value) for value in values if value is not None] + return None if not clean else float(statistics.median(clean)) + + +def summarize_runs(runs: list[Mapping[str, Any]]) -> dict[str, Any]: + phases = sorted( + { + phase + for run in runs + for phase in run.get("phase_totals_seconds", {}).keys() + } + ) + return { + "run_count": len(runs), + "median_elapsed_seconds": _median_or_none( + [run.get("elapsed_seconds") for run in runs] + ), + "median_phase_seconds": { + phase: _median_or_none( + [run.get("phase_totals_seconds", {}).get(phase) for run in runs] + ) + for phase in phases + }, + "median_peak_rss_bytes": _median_or_none( + [run.get("resources", {}).get("peak_rss_bytes") for run in runs] + ), + "median_peak_private_bytes": _median_or_none( + [run.get("resources", {}).get("peak_private_bytes") for run in runs] + ), + "median_peak_uss_bytes": _median_or_none( + [run.get("resources", {}).get("peak_uss_bytes") for run in runs] + ), + "median_cuda_global_peak_allocated_bytes": _median_or_none( + [run.get("cuda_global_peak_allocated_bytes") for run in runs] + ), + "median_minimum_cuda_free_bytes": _median_or_none( + [run.get("resources", {}).get("minimum_cuda_free_bytes") for run in runs] + ), + } + + +def _install_comfy_paths(comfy_root: Path) -> None: + values = ( + comfy_root, + comfy_root / "custom_nodes" / "comfyui-videohelpersuite", + ) + for path in values: + text = str(path) + if text not in sys.path: + sys.path.insert(0, text) + + +def _initialize_comfy(comfy_root: Path): + _install_comfy_paths(comfy_root) + os.chdir(comfy_root) + from comfy.cli_args import args as comfy_args + + # Load only the custom-node packages present in the accepted workflows. + # In particular, do not start ComfyUI-Manager's network/cache workers in a + # short-lived diagnostic process. + comfy_args.disable_all_custom_nodes = True + comfy_args.whitelist_custom_nodes = [ + "ComfyUI-H3-Continuum", + "ComfyUI-Spectrum-MiniMax-H3", + "ComfyUI-SolAttn_triton", + "comfyui-easy-use", + "comfyui-kjnodes", + "rgthree-comfy", + ] + import folder_paths + import nodes + import server + + loop = asyncio.new_event_loop() + prompt_server = server.PromptServer(loop) + loop.run_until_complete(nodes.init_extra_nodes()) + return folder_paths, nodes, prompt_server, loop + + +class NodeProfiler: + def __init__( + self, + execution_module, + prompt: Mapping[str, Any], + mode: str, + monitor_interval: float, + ): + self.execution = execution_module + self.prompt = prompt + self.mode = mode + self.monitor_interval = float(monitor_interval) + self.records: list[dict[str, Any]] = [] + self._original = None + + def install(self) -> None: + if self._original is not None: + raise RuntimeError("profiler is already installed") + self._original = self.execution.get_output_data + original = self._original + + async def profiled_get_output_data( + prompt_id, + unique_id, + obj, + input_data_all, + execution_block_cb=None, + pre_execute_cb=None, + v3_data=None, + ): + node_id = str(unique_id) + class_type = str( + self.prompt.get(node_id, {}).get( + "class_type", type(obj).__name__ + ) + ) + if torch.cuda.is_available(): + torch.cuda.synchronize() + if self.mode == "interval": + torch.cuda.reset_peak_memory_stats() + memory_start = _memory_snapshot() + cuda_start_allocated = ( + int(torch.cuda.memory_allocated()) if torch.cuda.is_available() else None + ) + cuda_start_reserved = ( + int(torch.cuda.memory_reserved()) if torch.cuda.is_available() else None + ) + monitor = ResourceMonitor(self.monitor_interval) + monitor.start() + started = time.perf_counter() + try: + result = await original( + prompt_id, + unique_id, + obj, + input_data_all, + execution_block_cb=execution_block_cb, + pre_execute_cb=pre_execute_cb, + v3_data=v3_data, + ) + if torch.cuda.is_available(): + torch.cuda.synchronize() + return result + finally: + elapsed = time.perf_counter() - started + resources = monitor.stop() + memory_end = _memory_snapshot() + self.records.append( + { + "node_id": node_id, + "class_type": class_type, + "phase": _phase_for_class(class_type), + "elapsed_seconds": float(elapsed), + "memory_start": memory_start, + "memory_end": memory_end, + "resources": resources, + "cuda_start_allocated_bytes": cuda_start_allocated, + "cuda_start_reserved_bytes": cuda_start_reserved, + "cuda_end_allocated_bytes": ( + int(torch.cuda.memory_allocated()) + if torch.cuda.is_available() + else None + ), + "cuda_end_reserved_bytes": ( + int(torch.cuda.memory_reserved()) + if torch.cuda.is_available() + else None + ), + "cuda_interval_peak_allocated_bytes": ( + int(torch.cuda.max_memory_allocated()) + if torch.cuda.is_available() and self.mode == "interval" + else None + ), + "cuda_synchronized": bool(torch.cuda.is_available()), + } + ) + + self.execution.get_output_data = profiled_get_output_data + + def uninstall(self) -> None: + if self._original is not None: + self.execution.get_output_data = self._original + self._original = None + + +def _phase_totals(records: list[Mapping[str, Any]]) -> dict[str, float]: + result: dict[str, float] = {} + for item in records: + phase = str(item.get("phase", "other")) + result[phase] = result.get(phase, 0.0) + float( + item.get("elapsed_seconds", 0.0) + ) + return result + + +def _cached_nodes(executor) -> list[str]: + for event, data in executor.status_messages: + if event == "execution_cached": + return sorted(str(value) for value in data.get("nodes", [])) + return [] + + +def _run_once( + *, + executor, + execution_module, + prompt: Mapping[str, Any], + prompt_id: str, + mode: str, + monitor_interval: float, + measured: bool, +) -> dict[str, Any]: + if not torch.cuda.is_available(): + raise ProbeFailure("Phase 3-2 GPU profiling requires CUDA") + profiler = NodeProfiler( + execution_module, + prompt, + mode=mode, + monitor_interval=monitor_interval, + ) + if measured: + profiler.install() + torch.cuda.synchronize() + if mode == "global": + torch.cuda.reset_peak_memory_stats() + run_monitor = ResourceMonitor(monitor_interval) + run_monitor.start() + started = time.perf_counter() + try: + executor.execute( + dict(prompt), + prompt_id, + extra_data={}, + execute_outputs=_execute_outputs(prompt), + ) + torch.cuda.synchronize() + finally: + elapsed = time.perf_counter() - started + resources = run_monitor.stop() + profiler.uninstall() + if not executor.success: + errors = [ + data + for event, data in executor.status_messages + if event == "execution_error" + ] + raise ProbeFailure(f"PromptExecutor failed: {errors[-1:]}") + records = list(profiler.records) + return { + "prompt_id": prompt_id, + "measured": bool(measured), + "elapsed_seconds": float(elapsed), + "phase_totals_seconds": _phase_totals(records), + "nodes": records, + "cached_nodes": _cached_nodes(executor), + "resources": resources, + "cuda_global_peak_allocated_bytes": ( + int(torch.cuda.max_memory_allocated()) if mode == "global" else None + ), + "cuda_peak_scope": ( + "whole_workflow" if mode == "global" else "per_node_only" + ), + } + + +def _validate_classes(nodes_module, prompt: Mapping[str, Any]) -> None: + missing = sorted( + { + str(node.get("class_type")) + for node in prompt.values() + if str(node.get("class_type")) not in nodes_module.NODE_CLASS_MAPPINGS + } + ) + if missing: + raise ProbeFailure(f"installed ComfyUI is missing prompt nodes: {missing}") + + +def run(args) -> dict[str, Any]: + prompt_source = json.loads(args.prompt.read_text(encoding="utf-8")) + configured = configure_profile(prompt_source, args.profile) + folder_paths, nodes_module, _prompt_server, loop = _initialize_comfy( + args.comfy_root + ) + _validate_classes(nodes_module, configured) + if args.validate_only: + loop.close() + return { + "status": "PASS", + "validate_only": True, + "profile": args.profile, + "route": _route(configured, args.profile), + "node_count": len(configured), + } + + from execution import CacheType, PromptExecutor + import execution as execution_module + + work_root = args.work_root.resolve() + output_root = work_root / "output" + temp_root = work_root / "temp" + output_root.mkdir(parents=True, exist_ok=True) + temp_root.mkdir(parents=True, exist_ok=True) + folder_paths.set_output_directory(str(output_root)) + folder_paths.set_temp_directory(str(temp_root)) + + executor = PromptExecutor( + _ExecutionServer(), + cache_type=CacheType.CLASSIC, + cache_args={"ram": float(args.cache_ram_gib), "ram_inactive": 0.0}, + ) + base_seed = int(args.seed) + warm_prompt = mutate_run( + configured, + seed=base_seed, + filename_prefix=f"p32/{args.case}_{args.profile}_warmup", + ) + warmup = _run_once( + executor=executor, + execution_module=execution_module, + prompt=warm_prompt, + prompt_id=f"p32-{args.case}-{args.profile}-warmup", + mode=args.mode, + monitor_interval=args.monitor_interval, + measured=False, + ) + measured_runs = [] + for index in range(int(args.runs)): + prompt = mutate_run( + configured, + seed=base_seed + index + 1, + filename_prefix=f"p32/{args.case}_{args.profile}_{args.mode}_{index + 1}", + ) + measured_runs.append( + _run_once( + executor=executor, + execution_module=execution_module, + prompt=prompt, + prompt_id=f"p32-{args.case}-{args.profile}-{args.mode}-{index + 1}", + mode=args.mode, + monitor_interval=args.monitor_interval, + measured=True, + ) + ) + result = { + "status": "PASS", + "format": "h3-continuum-phase3-2-profile-v1", + "case": args.case, + "profile": args.profile, + "route": _route(configured, args.profile), + "mode": args.mode, + "cuda_synchronize_before_after_nodes": True, + "warmup_excluded": True, + "warmup": warmup, + "runs": measured_runs, + "summary": summarize_runs(measured_runs), + "prompt": str(args.prompt.resolve()), + "work_root": str(work_root), + } + args.output.parent.mkdir(parents=True, exist_ok=True) + args.output.write_text( + json.dumps(result, indent=2, ensure_ascii=False), + encoding="utf-8", + ) + loop.close() + return result + + +def _parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--comfy-root", type=Path, required=True) + parser.add_argument("--prompt", type=Path, required=True) + parser.add_argument("--output", type=Path, required=True) + parser.add_argument("--work-root", type=Path, required=True) + parser.add_argument("--case", required=True) + parser.add_argument("--profile", choices=PROFILE_NAMES, required=True) + parser.add_argument("--mode", choices=MEASUREMENT_MODES, default="global") + parser.add_argument("--runs", type=int, default=3) + parser.add_argument("--seed", type=int, default=32000) + parser.add_argument("--cache-ram-gib", type=float, default=64.0) + parser.add_argument("--monitor-interval", type=float, default=0.05) + parser.add_argument("--validate-only", action="store_true") + return parser + + +def main() -> int: + args = _parser().parse_args() + if args.runs < 1: + raise SystemExit("--runs must be at least 1") + result = run(args) + print(json.dumps(result, ensure_ascii=False)) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/p32_profile_server.py b/tools/p32_profile_server.py new file mode 100644 index 0000000..6ae7a91 --- /dev/null +++ b/tools/p32_profile_server.py @@ -0,0 +1,300 @@ +"""Launch the installed ComfyUI API with Phase 3-2 diagnostics enabled. + +The wrapper imports the normal ComfyUI ``main.py``, installs an in-process +measurement hook, and then starts the unmodified API server. No custom node +or Production file is installed. Every completed prompt appends one JSON +record to ``--profile-output``. +""" + +from __future__ import annotations + +import argparse +import asyncio +import json +import os +from pathlib import Path +import sys +import threading +import time +from typing import Any + + +def _bootstrap_parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser(add_help=False) + parser.add_argument("--comfy-root", type=Path, required=True) + parser.add_argument("--profile-output", type=Path, required=True) + parser.add_argument( + "--profile-mode", choices=("global", "interval"), default="global" + ) + parser.add_argument("--profile-monitor-interval", type=float, default=0.05) + return parser + + +BOOTSTRAP, COMFY_ARGS = _bootstrap_parser().parse_known_args() +COMFY_ROOT = BOOTSTRAP.comfy_root.resolve() +PROFILE_OUTPUT = BOOTSTRAP.profile_output.resolve() +PROFILE_MODE = str(BOOTSTRAP.profile_mode) +MONITOR_INTERVAL = float(BOOTSTRAP.profile_monitor_interval) +sys.argv = [str(COMFY_ROOT / "main.py"), *COMFY_ARGS] +sys.path.insert(0, str(COMFY_ROOT)) +os.chdir(COMFY_ROOT) + +import torch # noqa: E402 + +import main as comfy_main # noqa: E402 +import execution # noqa: E402 + +from p32_gpu_profile_probe import ( # noqa: E402 + ResourceMonitor, + _memory_snapshot, + _phase_for_class, + _phase_totals, +) + + +_WRITE_LOCK = threading.Lock() +_CONTEXT_LOCK = threading.Lock() +_CONTEXTS: dict[str, dict[str, Any]] = {} + + +def _class_type(prompt: dict[str, Any], node_id: str, obj: Any) -> str: + configured = prompt.get(str(node_id), {}).get("class_type") + if configured: + return str(configured) + if isinstance(obj, type): + return str(obj.__name__) + return str(type(obj).__name__) + + +def _write_record(record: dict[str, Any]) -> None: + PROFILE_OUTPUT.parent.mkdir(parents=True, exist_ok=True) + line = json.dumps(record, ensure_ascii=False, separators=(",", ":")) + with _WRITE_LOCK: + with PROFILE_OUTPUT.open("a", encoding="utf-8") as handle: + handle.write(line + "\n") + handle.flush() + + +def _install_profiler() -> None: + original_get_output_data = execution.get_output_data + original_execute_async = execution.PromptExecutor.execute_async + + async def profiled_get_output_data( + prompt_id, + unique_id, + obj, + input_data_all, + execution_block_cb=None, + pre_execute_cb=None, + v3_data=None, + ): + with _CONTEXT_LOCK: + context = _CONTEXTS.get(str(prompt_id)) + if context is None: + return await original_get_output_data( + prompt_id, + unique_id, + obj, + input_data_all, + execution_block_cb=execution_block_cb, + pre_execute_cb=pre_execute_cb, + v3_data=v3_data, + ) + + node_id = str(unique_id) + class_type = _class_type(context["prompt"], node_id, obj) + if torch.cuda.is_available(): + torch.cuda.synchronize() + if PROFILE_MODE == "interval": + torch.cuda.reset_peak_memory_stats() + memory_start = _memory_snapshot() + cuda_start_allocated = ( + int(torch.cuda.memory_allocated()) if torch.cuda.is_available() else None + ) + cuda_start_reserved = ( + int(torch.cuda.memory_reserved()) if torch.cuda.is_available() else None + ) + monitor = ResourceMonitor(MONITOR_INTERVAL) + monitor.start() + started = time.perf_counter() + error = None + try: + return await original_get_output_data( + prompt_id, + unique_id, + obj, + input_data_all, + execution_block_cb=execution_block_cb, + pre_execute_cb=pre_execute_cb, + v3_data=v3_data, + ) + except BaseException as exc: + error = f"{type(exc).__name__}: {exc}" + raise + finally: + try: + if torch.cuda.is_available(): + torch.cuda.synchronize() + finally: + elapsed = time.perf_counter() - started + resources = monitor.stop() + record = { + "node_id": node_id, + "class_type": class_type, + "phase": _phase_for_class(class_type), + "elapsed_seconds": float(elapsed), + "memory_start": memory_start, + "memory_end": _memory_snapshot(), + "resources": resources, + "cuda_start_allocated_bytes": cuda_start_allocated, + "cuda_start_reserved_bytes": cuda_start_reserved, + "cuda_end_allocated_bytes": ( + int(torch.cuda.memory_allocated()) + if torch.cuda.is_available() + else None + ), + "cuda_end_reserved_bytes": ( + int(torch.cuda.memory_reserved()) + if torch.cuda.is_available() + else None + ), + "cuda_interval_peak_allocated_bytes": ( + int(torch.cuda.max_memory_allocated()) + if torch.cuda.is_available() and PROFILE_MODE == "interval" + else None + ), + "cuda_synchronized": bool(torch.cuda.is_available()), + "error": error, + } + with _CONTEXT_LOCK: + active = _CONTEXTS.get(str(prompt_id)) + if active is not None: + active["nodes"].append(record) + + async def profiled_execute_async( + self, + prompt, + prompt_id, + extra_data={}, + execute_outputs=[], + ): + prompt_key = str(prompt_id) + context: dict[str, Any] = { + "prompt": dict(prompt), + "nodes": [], + "started_at_unix": time.time(), + } + with _CONTEXT_LOCK: + _CONTEXTS[prompt_key] = context + if torch.cuda.is_available(): + torch.cuda.synchronize() + if PROFILE_MODE == "global": + torch.cuda.reset_peak_memory_stats() + monitor = ResourceMonitor(MONITOR_INTERVAL) + monitor.start() + started = time.perf_counter() + error = None + try: + return await original_execute_async( + self, + prompt, + prompt_id, + extra_data=extra_data, + execute_outputs=execute_outputs, + ) + except BaseException as exc: + error = f"{type(exc).__name__}: {exc}" + raise + finally: + try: + if torch.cuda.is_available(): + torch.cuda.synchronize() + finally: + elapsed = time.perf_counter() - started + resources = monitor.stop() + with _CONTEXT_LOCK: + completed = _CONTEXTS.pop(prompt_key, context) + nodes = list(completed["nodes"]) + class_types = [str(item["class_type"]) for item in nodes] + spectrum_nodes = [ + node + for node in prompt.values() + if str(node.get("class_type")) == "SpectrumApplyMiniMaxH3" + ] + spectrum_enabled = any( + bool(node.get("inputs", {}).get("enabled", False)) + for node in spectrum_nodes + ) + configured_class_types = { + str(node.get("class_type")) for node in prompt.values() + } + _write_record( + { + "format": "h3-continuum-phase3-2-api-profile-v1", + "prompt_id": prompt_key, + "mode": PROFILE_MODE, + "started_at_unix": completed["started_at_unix"], + "elapsed_seconds": float(elapsed), + "phase_totals_seconds": _phase_totals(nodes), + "nodes": nodes, + "resources": resources, + "cuda_global_peak_allocated_bytes": ( + int(torch.cuda.max_memory_allocated()) + if torch.cuda.is_available() and PROFILE_MODE == "global" + else None + ), + "cuda_peak_scope": ( + "whole_workflow" + if PROFILE_MODE == "global" + else "per_node_only" + ), + "cuda_synchronize_before_after_nodes": bool( + torch.cuda.is_available() + ), + "route_observed": { + "sage_node_configured": ( + "MiniMaxH3MemoryEfficientSageAttentionPatch" + in configured_class_types + ), + "sol_node_configured": "SolAttnPatch" + in configured_class_types, + "spectrum_node_configured": ( + "SpectrumApplyMiniMaxH3" in configured_class_types + ), + "sage_node_executed": ( + "MiniMaxH3MemoryEfficientSageAttentionPatch" + in class_types + ), + "sol_node_executed": "SolAttnPatch" in class_types, + "spectrum_node_executed": ( + "SpectrumApplyMiniMaxH3" in class_types + ), + "spectrum_enabled": bool(spectrum_enabled), + }, + "success": bool(getattr(self, "success", False)), + "error": error, + } + ) + + execution.get_output_data = profiled_get_output_data + execution.PromptExecutor.execute_async = profiled_execute_async + + +def main() -> int: + _install_profiler() + loop, _server, start_all = comfy_main.start_comfyui() + try: + loop.run_until_complete(start_all()) + except KeyboardInterrupt: + pass + finally: + try: + comfy_main.asset_seeder.shutdown() + except Exception: + pass + comfy_main.cleanup_temp() + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/v2/h3_builder.py b/v2/h3_builder.py index 588a4ef..2740417 100644 --- a/v2/h3_builder.py +++ b/v2/h3_builder.py @@ -6,9 +6,10 @@ 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 +from typing import Any, Callable import torch @@ -16,6 +17,11 @@ 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 @@ -46,6 +52,155 @@ def _tensor_fingerprint(tensor: torch.Tensor | None) -> str: 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 diff --git a/v2/nodes.py b/v2/nodes.py index b06c070..a719a00 100644 --- a/v2/nodes.py +++ b/v2/nodes.py @@ -313,6 +313,7 @@ class H3ContinuumSamplerV2: timeline_video_source=None, capture_refine_context=False, memory_attribution=False, + prompt_conditioning_cache=False, **clip_prompt_inputs, ): ( @@ -387,6 +388,7 @@ class H3ContinuumSamplerV2: timeline_video_source=timeline_video_source, capture_refine_context=bool(capture_refine_context), memory_attribution=bool(memory_attribution), + prompt_conditioning_cache=bool(prompt_conditioning_cache), ) diff --git a/v2/sequence.py b/v2/sequence.py index b96b3f5..84ec719 100644 --- a/v2/sequence.py +++ b/v2/sequence.py @@ -35,7 +35,7 @@ from ..temporal import ( from ..version import PACKAGE_VERSION from .decoder import decode_sequence, decode_sequence_with_seam, enforce_total_frames from .context_diagnostics import ContextDiagnosticsTracker -from .h3_builder import attach_keyframes, empty_h3_latent, encode_identity_latents, encode_prompt_conditioning, prepare_identity_assets +from .h3_builder import attach_keyframes, empty_h3_latent, encode_identity_latents, encode_prompt_conditioning, encode_prompt_conditioning_cached, prepare_identity_assets from .motion import choose_context_frames from .prompts import prompt_plan_report, validate_prompt_plan from .sampling import latent_from_cpu, latent_to_cpu, sample_chunk @@ -99,16 +99,44 @@ def _conditioning_cache_key(prompt, *, include_last, reference_assets=None): return (prompt, False if reference_assets is not None else bool(include_last)) -def _conditioning_cache(*,clip,prompts,assets,final_has_last_frame,reference_assets=None,reference_audio_assets=None,timeline_video_assets=None): +def _record_prompt_cache_event(callback, event): + if callback is None: + return + try: + callback(str(event)) + except Exception: + pass + + +def _prompt_cache_diagnostics(events): + hits=sum(event=="hit" for event in events) + misses=sum(event=="miss" for event in events) + bypasses=len(events)-hits-misses + return ( + "Prompt/CLIP cache [V3.5]: " + f"hits={hits}, misses={misses}, bypasses={bypasses}, " + f"encode_calls={misses+bypasses}." + ) + + +def _conditioning_cache(*,clip,prompts,assets,final_has_last_frame,reference_assets=None,reference_audio_assets=None,timeline_video_assets=None,prompt_conditioning_cache=False,prompt_cache_event=None): cache={}; final_index=len(prompts)-1 for index,prompt in enumerate(prompts): include_last=bool(final_has_last_frame and index==final_index); key=_conditioning_cache_key(prompt,include_last=include_last,reference_assets=reference_assets) if key in cache: continue if reference_assets is not None: from ..reference import encode_reference_prompt + if bool(prompt_conditioning_cache): + _record_prompt_cache_event(prompt_cache_event,"bypass_reference_inputs") cache[key]=encode_reference_prompt(clip,prompt,reference_assets,first_image=assets.first_image,last_image=assets.last_image,reference_audio_assets=reference_audio_assets,timeline_video_assets=timeline_video_assets) else: - cache[key]=encode_prompt_conditioning(clip,prompt,first_image=assets.first_image,last_image=assets.last_image if include_last else None,reference_audio_assets=reference_audio_assets,timeline_video_assets=timeline_video_assets) + cache_allowed=bool(prompt_conditioning_cache and reference_audio_assets is None and timeline_video_assets is None) + if bool(prompt_conditioning_cache) and not cache_allowed: + _record_prompt_cache_event(prompt_cache_event,"bypass_reference_inputs") + if cache_allowed: + cache[key]=encode_prompt_conditioning_cached(clip,prompt,first_image=assets.first_image,last_image=assets.last_image if include_last else None,first_image_fingerprint=assets.identity_hash,last_image_fingerprint=assets.last_frame_hash if include_last else "none",cache_enabled=True,cache_event=prompt_cache_event) + else: + cache[key]=encode_prompt_conditioning(clip,prompt,first_image=assets.first_image,last_image=assets.last_image if include_last else None,reference_audio_assets=reference_audio_assets,timeline_video_assets=timeline_video_assets) return cache @@ -339,12 +367,14 @@ def _attach_terminal_flf_keyframes( metadata["minimax_frame_count"] = int(frame_count) return conditioning -def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,sigmas:torch.Tensor,first_frame:torch.Tensor|None,last_frame:torch.Tensor|None,prompt_plan:dict[str,Any],width:int,height:int,continuity:str,base_seed:int,audio_continuity:bool,exact_total_duration:bool,diagnostics_mode:str,reroll_from_chunk:int,reroll_nonce:int,strict_compatibility:bool,debug:bool,seam_correction:str=SEAM_CORRECTION_OFF,enable_preview:bool=True,session:dict[str,Any]|None=None,initial_state:dict[str,Any]|None=None,latent_only:bool=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,capture_refine_context:bool=False,memory_attribution:bool=False,_memory_attribution_collector:Any=None): +def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,sigmas:torch.Tensor,first_frame:torch.Tensor|None,last_frame:torch.Tensor|None,prompt_plan:dict[str,Any],width:int,height:int,continuity:str,base_seed:int,audio_continuity:bool,exact_total_duration:bool,diagnostics_mode:str,reroll_from_chunk:int,reroll_nonce:int,strict_compatibility:bool,debug:bool,seam_correction:str=SEAM_CORRECTION_OFF,enable_preview:bool=True,session:dict[str,Any]|None=None,initial_state:dict[str,Any]|None=None,latent_only:bool=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,capture_refine_context:bool=False,memory_attribution:bool=False,prompt_conditioning_cache:bool=False,_memory_attribution_collector:Any=None): from ..conditioning import detect_conditioning_mode, conditioning_display_label from ..run_storage import get_active_run_storage storage_controller=get_active_run_storage() capture_refine_context=bool(capture_refine_context and latent_only) memory_attribution=bool(memory_attribution and capture_refine_context) + prompt_conditioning_cache=bool(prompt_conditioning_cache and capture_refine_context and latent_only) + prompt_cache_events=[] memory_collector=None refine_groups=[] make_refine_group=make_refine_context=None @@ -447,25 +477,52 @@ def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,si if old_fingerprint and old_fingerprint!=current_model_fingerprint: reuse_notes.append("model/accelerator fingerprint differs from the saved session; accepted chunks were kept") cache={} if len(preserved)0 and sequence_index+1>=int(reroll_from_chunk) else 0; seed=derive_chunk_seed(base_seed,sequence_index,effective_reroll_nonce); motion_score=0.0; video_context=None; audio_context=None; context_before=None timeline_video_assets=reference_video_assets chunk_cache=cache if timeline_video_source is not None: from ..timeline_video import encode_timeline_video_chunk + if memory_collector is not None: + capture_memory(memory_collector, "start_phase", phase="conditioning") + capture_memory(memory_collector, "start_phase", phase="conditioning_timeline_video_vae") timeline_video_assets=encode_timeline_video_chunk(video_vae,timeline_video_source,sequence_index) + if memory_collector is not None: + capture_memory(memory_collector, "finish_phase", phase="conditioning_timeline_video_vae") chunk_assets=assets if timeline_video_source is not None: - chunk_cache=_conditioning_cache(clip=clip,prompts=[prompt],assets=assets,final_has_last_frame=bool(last_frame is not None and is_final),reference_assets=reference_assets,reference_audio_assets=reference_audio_assets,timeline_video_assets=timeline_video_assets) + if memory_collector is not None: + capture_memory(memory_collector, "start_phase", phase="conditioning_prompt_clip") + chunk_cache=_conditioning_cache(clip=clip,prompts=[prompt],assets=assets,final_has_last_frame=bool(last_frame is not None and is_final),reference_assets=reference_assets,reference_audio_assets=reference_audio_assets,timeline_video_assets=timeline_video_assets,prompt_conditioning_cache=prompt_conditioning_cache,prompt_cache_event=prompt_cache_events.append) + if memory_collector is not None: + capture_memory(memory_collector, "finish_phase", phase="conditioning_prompt_clip") + capture_memory(memory_collector, "finish_phase", phase="conditioning") if previous_state is None: 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) @@ -515,6 +586,9 @@ def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,si driving_audio_latent=slice_driving_audio_latent(driving_audio_assets,cumulative_retained_before=retained_frames,total_frames=int(chunk_plan["total_frames"]),trim_frames=int(chunk_plan["trim_frames"]),fps=FPS) conditioning=attach_driving_audio(conditioning,driving_audio_latent) chunk_model=clone_model_for_chunk(model,strict=bool(strict_compatibility),debug=bool(debug),chunk_index=clip_index,context_frames=context_frames if previous_state is not None else None) + if memory_collector is not None: + capture_memory(memory_collector, "finish_phase", phase="group_prepare", physical_group=sequence_index+1, logical_chunks=(sequence_index+1,)) + capture_memory(memory_collector, "start_phase", phase="refine_context", physical_group=sequence_index+1, logical_chunks=(sequence_index+1,)) if bool(capture_refine_context): from ..state import extract_av_streams source_video,_=extract_av_streams(latent) @@ -532,6 +606,8 @@ def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,si last_image=assets.last_image if include_chunk_last else None, )) del source_video,_ + if memory_collector is not None: + capture_memory(memory_collector, "finish_phase", phase="refine_context", physical_group=sequence_index+1, logical_chunks=(sequence_index+1,)) if memory_collector is not None: capture_memory(memory_collector, "capture_group", physical_group=sequence_index+1, @@ -541,7 +617,11 @@ def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,si observed_latent=latent, retained_refine_context=refine_groups, ) + if memory_collector is not None: + capture_memory(memory_collector, "start_phase", phase="sampling", physical_group=sequence_index+1, logical_chunks=(sequence_index+1,)) sampled=sample_chunk(model=chunk_model,conditioning=conditioning,latent=latent,sampler=sampler,sigmas=sigmas,seed=seed,enable_preview=bool(enable_preview)) + if memory_collector is not None: + capture_memory(memory_collector, "finish_phase", phase="sampling", physical_group=sequence_index+1, logical_chunks=(sequence_index+1,)) if memory_collector is not None: capture_memory(memory_collector, "capture_group", physical_group=sequence_index+1, @@ -551,10 +631,14 @@ def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,si observed_latent=sampled, retained_refine_context=refine_groups, ) + if memory_collector is not None: + capture_memory(memory_collector, "start_phase", phase="cpu_commit_validation", physical_group=sequence_index+1, logical_chunks=(sequence_index+1,)) if context_before is not None and video_context is not None: assert_context_unchanged(video_context,audio_context,context_before) entry=make_chunk_entry(latent=sampled,plan=chunk_plan,prompt=prompt,prompt_hash=prompt_hash_value,seed=seed,context_frames=context_frames,motion_score=motion_score,reused=False); previous_state=entry_to_state(entry); entries.append(entry) _record_context_diagnostics(tracker=context_diagnostics,reports=sampling_reports,state=previous_state,continuity=continuity,reused=False) if storage_controller is not None: storage_controller.commit_chunk(entry, position=sequence_index) + if memory_collector is not None: + capture_memory(memory_collector, "finish_phase", phase="cpu_commit_validation", physical_group=sequence_index+1, logical_chunks=(sequence_index+1,)) retained_frames+=int(chunk_plan["net_frames"]) sampling_reports.append(f"chunk {sequence_index+1}/{chunks}: seed={seed}, frames={chunk_plan['total_frames']}, trim={chunk_plan['trim_frames']}, retained_total={retained_frames}, context={context_frames} ({reason}), motion={motion_score:.6f}, "+(f"interop=emitted actual_prefix={CONTINUUM_ACTUAL_PREFIX_STEPS} consumer=not_observable" if context_before is not None else "interop=not_emitted")) del sampled,latent,conditioning,chunk_model,chunk_cache @@ -571,6 +655,10 @@ def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,si pair_start=chunks-2 if len(entries)!=pair_start: raise SequenceRuntimeError(f"terminal merged pair expected {pair_start} completed chunks, got {len(entries)}") + terminal_physical_group=pair_start+1 + terminal_logical_chunks=(pair_start+1,chunks) + if memory_collector is not None: + capture_memory(memory_collector, "start_phase", phase="group_prepare", physical_group=terminal_physical_group, logical_chunks=terminal_logical_chunks) prompt=terminal_prompt seed_nonce=int(reroll_nonce) if int(reroll_from_chunk)>0 else 0 terminal_seed_plan=_terminal_physical_seed_plan(base_seed=base_seed,physical_window_start_index=pair_start,reroll_nonce=seed_nonce) @@ -597,6 +685,9 @@ def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,si driving_audio_latent=slice_driving_audio_latent(driving_audio_assets,cumulative_retained_before=retained_frames,total_frames=physical_frames,trim_frames=physical_context_frames,fps=FPS) conditioning=attach_driving_audio(conditioning,driving_audio_latent) chunk_model=clone_model_for_chunk(model,strict=bool(strict_compatibility),debug=bool(debug),chunk_index=physical_clip_index,context_frames=physical_context_frames if not initial_pair else None) + if memory_collector is not None: + capture_memory(memory_collector, "finish_phase", phase="group_prepare", physical_group=terminal_physical_group, logical_chunks=terminal_logical_chunks) + capture_memory(memory_collector, "start_phase", phase="refine_context", physical_group=terminal_physical_group, logical_chunks=terminal_logical_chunks) if bool(capture_refine_context): from ..state import extract_av_streams source_video,_=extract_av_streams(latent) @@ -614,8 +705,8 @@ def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,si last_image=assets.last_image, )) del source_video,_ - terminal_physical_group=pair_start+1 - terminal_logical_chunks=(pair_start+1,chunks) + if memory_collector is not None: + capture_memory(memory_collector, "finish_phase", phase="refine_context", physical_group=terminal_physical_group, logical_chunks=terminal_logical_chunks) if memory_collector is not None: capture_memory(memory_collector, "capture_group", physical_group=terminal_physical_group, @@ -625,7 +716,11 @@ def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,si observed_latent=latent, retained_refine_context=refine_groups, ) + if memory_collector is not None: + capture_memory(memory_collector, "start_phase", phase="sampling", physical_group=terminal_physical_group, logical_chunks=terminal_logical_chunks) sampled=sample_chunk(model=chunk_model,conditioning=conditioning,latent=latent,sampler=sampler,sigmas=sigmas,seed=physical_seed,enable_preview=bool(enable_preview)) + if memory_collector is not None: + capture_memory(memory_collector, "finish_phase", phase="sampling", physical_group=terminal_physical_group, logical_chunks=terminal_logical_chunks) if memory_collector is not None: capture_memory(memory_collector, "capture_group", physical_group=terminal_physical_group, @@ -635,6 +730,8 @@ def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,si observed_latent=sampled, retained_refine_context=refine_groups, ) + if memory_collector is not None: + capture_memory(memory_collector, "start_phase", phase="cpu_commit_validation", physical_group=terminal_physical_group, logical_chunks=terminal_logical_chunks) if context_before is not None and video_context is not None: assert_context_unchanged(video_context,audio_context,context_before) sampled_video,sampled_audio=latent_to_cpu(sampled) @@ -659,6 +756,8 @@ def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,si retained_frames+=int(chunk_plan["net_frames"]) sampling_reports.append(f"chunk {sequence_index+1}/{chunks}: seed={logical_seed}, frames={total_frames}, trim={trim_frames}, retained_total={retained_frames}, context={trim_frames}, shared_physical_sample=terminal_10s_seed_v2") del logical_latent + if memory_collector is not None: + capture_memory(memory_collector, "finish_phase", phase="cpu_commit_validation", physical_group=terminal_physical_group, logical_chunks=terminal_logical_chunks) del sampled,sampled_video,sampled_audio,logical_parts,latent,conditioning,chunk_model del video_part,audio_part if memory_collector is not None: @@ -669,6 +768,8 @@ def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,si retained_entries=entries, retained_refine_context=refine_groups, ) + if memory_collector is not None: + capture_memory(memory_collector, "start_phase", phase="finalization") if len(entries)!=chunks: raise SequenceRuntimeError(f"internal sequence length mismatch: expected {chunks}, got {len(entries)}") if latent_only: last_state=entry_to_state(entries[-1]); parent_id=session.get("session_id") if session is not None else None @@ -681,6 +782,8 @@ def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,si 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) 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] if diagnostics_mode!=DIAGNOSTICS_OFF: report_lines.extend(sampling_reports) + if prompt_conditioning_cache and diagnostics_mode==DIAGNOSTICS_FULL: + report_lines.append(_prompt_cache_diagnostics(prompt_cache_events)) report_lines.extend([session_summary(new_session),f"Output: {len(entries)} raw AV latent chunk(s); connect Core VAE Decode nodes, then H3 Continuum Assemble V3."]) outputs=(entries,last_state,new_session,"\n".join(report_lines)) if bool(capture_refine_context): @@ -701,7 +804,11 @@ def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,si complete=complete, notes=notes, ) + if memory_collector is not None: + capture_memory(memory_collector, "finish_phase", phase="finalization") return (*outputs,raw_refine_context) + if memory_collector is not None: + capture_memory(memory_collector, "finish_phase", phase="finalization") return outputs if seam_correction==SEAM_CORRECTION_OFF: images,audio,decode_reports=decode_sequence(entries=entries,video_vae=video_vae,audio_vae=audio_vae,diagnostics_full=diagnostics_mode==DIAGNOSTICS_FULL) 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) diff --git a/v3/driving_nodes.py b/v3/driving_nodes.py index 1ae5c07..303e706 100644 --- a/v3/driving_nodes.py +++ b/v3/driving_nodes.py @@ -255,10 +255,12 @@ class H3ContinuumSamplerV35(H3ContinuumSamplerV34): def run(self, *args, **kwargs): kwargs.pop("capture_refine_context", None) kwargs.pop("memory_attribution", None) + kwargs.pop("prompt_conditioning_cache", None) return super().run( *args, capture_refine_context=True, memory_attribution=True, + prompt_conditioning_cache=True, **kwargs, ) diff --git a/v3/memory_attribution.py b/v3/memory_attribution.py index 7d6cf8e..a227e06 100644 --- a/v3/memory_attribution.py +++ b/v3/memory_attribution.py @@ -4,6 +4,7 @@ from __future__ import annotations from dataclasses import dataclass import math +import time from typing import Any, Mapping import torch @@ -16,6 +17,16 @@ from .plan import validate_assembly_plan _MIB = 1024**2 +_CONDITIONING_SUBPHASES = ( + "conditioning_identity_video_vae", + "conditioning_reference_image_vae", + "conditioning_reference_audio_vae", + "conditioning_driving_audio_vae", + "conditioning_reference_video_vae", + "conditioning_timeline_video_vae", + "conditioning_prompt_clip", +) + def _mib(value: int | None) -> str: if value is None: @@ -120,9 +131,119 @@ class MemoryAttributionCollector: def __init__(self): self.lines: list[str] = [] + self._sequence_started_at: float | None = None + self._phase_started_at: dict[tuple[str, int | None, tuple[int, ...]], float] = {} + self._phase_totals: dict[str, float] = {} + self._phase_counts: dict[str, int] = {} + self._group_totals: dict[ + tuple[int, tuple[int, ...]], dict[str, float] + ] = {} def capture_sequence_start(self) -> None: + self._sequence_started_at = time.perf_counter() self.lines.append(format_attribution_snapshot("V3.5 sequence start")) + self.start_phase(phase="preparation") + + def start_phase( + self, + *, + phase: str, + physical_group: int | None = None, + logical_chunks: tuple[int, ...] = (), + ) -> None: + key = ( + str(phase), + None if physical_group is None else int(physical_group), + tuple(int(value) for value in logical_chunks), + ) + self._phase_started_at[key] = time.perf_counter() + + def finish_phase( + self, + *, + phase: str, + physical_group: int | None = None, + logical_chunks: tuple[int, ...] = (), + ) -> None: + phase = str(phase) + group = None if physical_group is None else int(physical_group) + logical = tuple(int(value) for value in logical_chunks) + key = (phase, group, logical) + started = self._phase_started_at.pop(key, None) + if started is None: + return + elapsed = time.perf_counter() - started + if not math.isfinite(elapsed) or elapsed < 0.0: + return + self._phase_totals[phase] = self._phase_totals.get(phase, 0.0) + elapsed + self._phase_counts[phase] = self._phase_counts.get(phase, 0) + 1 + if group is not None: + phases = self._group_totals.setdefault((group, logical), {}) + phases[phase] = phases.get(phase, 0.0) + elapsed + + @staticmethod + def _format_seconds(value: float | None) -> str: + return "n/a" if value is None else f"{float(value):.6f}s" + + def _performance_lines(self) -> list[str]: + now = time.perf_counter() + host_wall = None + if self._sequence_started_at is not None: + candidate = now - self._sequence_started_at + if math.isfinite(candidate) and candidate >= 0.0: + host_wall = candidate + ordered = ( + "preparation", + "conditioning", + *_CONDITIONING_SUBPHASES, + "group_prepare", + "sampling", + "cpu_commit_validation", + "refine_context", + "finalization", + ) + lines = [] + for (group, logical), phases in sorted(self._group_totals.items()): + logical_label = ",".join(str(value) for value in logical) + fields = ", ".join( + f"{name}={self._format_seconds(phases.get(name))}" + for name in ( + "group_prepare", + "sampling", + "cpu_commit_validation", + "refine_context", + ) + ) + lines.append( + f"Performance [V3.5 physical group {group} " + f"logical=[{logical_label}]]: {fields}" + ) + totals = ", ".join( + f"{name}={self._format_seconds(self._phase_totals.get(name))}" + for name in ordered + ) + counts = ", ".join( + f"{name}={int(self._phase_counts.get(name, 0))}" + for name in ( + "group_prepare", + "sampling", + "cpu_commit_validation", + "refine_context", + ) + ) + conditioning_counts = ", ".join( + f"{name}={int(self._phase_counts.get(name, 0))}" + for name in _CONDITIONING_SUBPHASES + ) + lines.append( + "Performance [V3.5 summary]: " + f"host_wall_including_diagnostics={self._format_seconds(host_wall)}, " + f"{totals}; " + f"phase_counts({counts}); " + f"conditioning_counts({conditioning_counts}); " + "CUDA_synchronize=not_used" + ) + return lines def capture_group( self, @@ -150,6 +271,7 @@ class MemoryAttributionCollector: entries: Any, retained_refine_context: Any = None, ) -> None: + self.lines.extend(self._performance_lines()) self.lines.append( format_attribution_snapshot( "V3.5 sequence complete", diff --git a/v3/nodes.py b/v3/nodes.py index 0cb3c6c..40a107e 100644 --- a/v3/nodes.py +++ b/v3/nodes.py @@ -258,6 +258,7 @@ class H3ContinuumSamplerV3: timeline_video_source=None, capture_refine_context=False, memory_attribution=False, + prompt_conditioning_cache=False, ): if prompt_overrides is not None and not isinstance(prompt_overrides, dict): prompt_overrides = None @@ -339,6 +340,7 @@ class H3ContinuumSamplerV3: timeline_video_source=timeline_video_source, capture_refine_context=bool(capture_refine_context), memory_attribution=bool(memory_attribution), + prompt_conditioning_cache=bool(prompt_conditioning_cache), **clip_prompt_inputs, ) if bool(capture_refine_context): @@ -663,6 +665,7 @@ class H3ContinuumSamplerProduction(H3ContinuumSamplerV3): timeline_video_source=None, capture_refine_context=False, memory_attribution=False, + prompt_conditioning_cache=False, ): runtime_started_at = time.perf_counter() from ..reference import prepare_reference_assets @@ -714,6 +717,7 @@ class H3ContinuumSamplerProduction(H3ContinuumSamplerV3): timeline_video_source=timeline_video_source, capture_refine_context=bool(capture_refine_context), memory_attribution=bool(memory_attribution), + prompt_conditioning_cache=bool(prompt_conditioning_cache), advanced={ "audio_continuity": bool(audio_continuity), "diagnostics": diagnostics, diff --git a/version.py b/version.py index 91fc1c9..e8732d3 100644 --- a/version.py +++ b/version.py @@ -1,6 +1,6 @@ """Package version and public schema identifiers.""" -PACKAGE_VERSION = "3.5.1" +PACKAGE_VERSION = "3.5.2" STATE_SCHEMA_VERSION = 1 PLAN_SCHEMA_VERSION = 1 PUBLIC_API_VERSION = 3