Skip to content

Make videos on the computer you already own. FreeVideo runs MiniMax H3 in as little as 8 GB of VRAM and 16 GB of RAM, and adapts its acceleration path to your hardware.

FreeVideo: MiniMax H3 on consumer GPUs

Bowen Xue2d19c7cb37c0
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..cd93830
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,12 @@
+root = true
+
+[*]
+charset = utf-8
+end_of_line = lf
+insert_final_newline = true
+trim_trailing_whitespace = true
+indent_style = space
+indent_size = 4
+
+[*.yml]
+indent_size = 2
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..534c5b7
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,8 @@
+*.py text eol=lf
+*.sh text eol=lf
+*.ps1 text eol=crlf
+*.cmd text eol=crlf
+/freevideo text eol=lf
+*.md text eol=lf
+*.json text eol=lf
+*.yml text eol=lf
diff --git a/.github/ISSUE_TEMPLATE/bug_engine.yml b/.github/ISSUE_TEMPLATE/bug_engine.yml
new file mode 100644
index 0000000..0861f78
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/bug_engine.yml
@@ -0,0 +1,19 @@
+name: Video generation / 视频生成
+description: Errors, output quality or performance / 生成报错、画面质量或速度问题
+body:
+ - type: input
+ id: version
+ attributes:
+ label: FreeVideo version / FreeVideo 版本
+ - type: textarea
+ id: problem
+ attributes:
+ label: What happened? / 遇到了什么问题?
+ description: Describe what you expected and what happened. / 简单说明预期效果和实际情况。
+ validations:
+ required: true
+ - type: textarea
+ id: report
+ attributes:
+ label: Error or local report (optional) / 错误详情或本地报告(选填)
+ description: Attach video.debug.json if available; it includes hardware and timing information. / 如有 video.debug.json,可拖入此处;其中已包含硬件和耗时信息。
diff --git a/.github/ISSUE_TEMPLATE/bug_launcher.yml b/.github/ISSUE_TEMPLATE/bug_launcher.yml
new file mode 100644
index 0000000..ccb485c
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/bug_launcher.yml
@@ -0,0 +1,19 @@
+name: Installation or launcher / 安装或启动器
+description: Help with installation, updates or startup / 安装、更新或启动问题
+body:
+ - type: input
+ id: version
+ attributes:
+ label: FreeVideo version / FreeVideo 版本
+ - type: textarea
+ id: problem
+ attributes:
+ label: What happened? / 遇到了什么问题?
+ description: Describe what you did and what happened. / 简单说明操作和出现的问题。
+ validations:
+ required: true
+ - type: textarea
+ id: report
+ attributes:
+ label: Error or local report (optional) / 错误详情或本地报告(选填)
+ description: Paste the error or drag a local diagnostic report here. / 粘贴错误详情,或拖入本地诊断报告。
diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml
new file mode 100644
index 0000000..7da7eb6
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/config.yml
@@ -0,0 +1,11 @@
+blank_issues_enabled: true
+contact_links:
+ - name: Discord
+ url: https://discord.gg/MsA277cJzZ
+ about: Community chat / 社区交流
+ - name: QQ Group / QQ 群
+ url: https://freevideo-community.pages.dev/qq
+ about: Community chat / 社区交流
+ - name: WeChat Group / 微信群
+ url: https://freevideo-community.pages.dev/wechat
+ about: Community chat / 社区交流
diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml
new file mode 100644
index 0000000..93255e9
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/feature_request.yml
@@ -0,0 +1,9 @@
+name: Suggestion / 建议
+description: Tell us what would help / 说说你希望改进什么
+body:
+ - type: textarea
+ id: idea
+ attributes:
+ label: What would you like to do? / 你希望实现什么?
+ validations:
+ required: true
diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md
new file mode 100644
index 0000000..9a5afa5
--- /dev/null
+++ b/.github/PULL_REQUEST_TEMPLATE.md
@@ -0,0 +1,7 @@
+## Change / 改动
+
+<!-- What changed and why? / 改了什么,解决什么问题? -->
+
+## Validation / 验证
+
+<!-- How did you check it? For performance changes, include a comparison with the same settings. / 如何验证?性能改动可附相同配置下的前后对比。 -->
diff --git a/.github/workflows/install.yml b/.github/workflows/install.yml
new file mode 100644
index 0000000..61bcd8a
--- /dev/null
+++ b/.github/workflows/install.yml
@@ -0,0 +1,269 @@
+name: Installation and Windows launcher
+on:
+ push:
+ branches: [main]
+ paths-ignore:
+ - '**/*.md'
+ pull_request:
+ paths-ignore:
+ - '**/*.md'
+ workflow_dispatch:
+ inputs:
+ defender_baseline:
+ description: 'Also diagnose the currently published package before rebuilding'
+ type: boolean
+ default: false
+permissions:
+ contents: read
+concurrency:
+ group: freevideo-public-${{ github.event.pull_request.number || github.ref }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
+jobs:
+ installation:
+ runs-on: ubuntu-24.04
+ timeout-minutes: 10
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
+ with:
+ python-version: '3.12'
+ - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
+ with:
+ node-version: '22'
+ - name: Check every ComfyUI browser module
+ run: |
+ for file in web/*.js; do node --input-type=module --check < "$file"; done
+ node scripts/test_freevideo_entry.mjs
+ node scripts/test_prompt_guide.mjs
+ - name: Check CLI, ComfyUI entry and packaged resources
+ run: |
+ python scripts/check_installation.py
+ bash -n setup.sh test.sh freevideo scripts/bootstrap_linux.sh
+ ./setup.sh --help
+ ./test.sh --help
+ ./freevideo --help
+ - name: Build installable Python packages
+ run: |
+ python -m pip install build
+ python -m build
+ windows:
+ needs: [installation]
+ runs-on: windows-2025
+ timeout-minutes: 60
+ permissions:
+ contents: write
+ concurrency:
+ group: windows-launcher-release-${{ github.ref }}
+ cancel-in-progress: false
+ env:
+ PYTHONUTF8: '1'
+ PYTHONIOENCODING: utf-8
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
+ with:
+ python-version: '3.12'
+ cache: pip
+ cache-dependency-path: constraints/windows-launcher.txt
+ - run: python scripts/check_installation.py
+ - run: python -m pip install -r constraints/windows-launcher.txt
+ - name: Diagnose previous release with real-time Defender
+ if: github.event_name == 'workflow_dispatch' && inputs.defender_baseline
+ env:
+ GH_TOKEN: ${{ github.token }}
+ shell: pwsh
+ run: |
+ gh release download windows-preview --repo FlashML-org/FreeVideo --pattern FreeVideo.exe --pattern FreeVideo-Windows-folder.zip --dir build/previous-release
+ if ($LASTEXITCODE -ne 0) { throw 'Could not retrieve the previous release for comparison' }
+ python scripts/scan_windows_release.py --file build/previous-release/FreeVideo.exe --file build/previous-release/FreeVideo-Windows-folder.zip --verify-launches build/previous-release/FreeVideo.exe build/previous-release/FreeVideo-Windows-folder.zip --report antivirus/previous-release.json
+ $baselineExit = $LASTEXITCODE
+ if (Test-Path antivirus/previous-release.json) { Get-Content antivirus/previous-release.json -Raw }
+ Write-Output "Previous-release diagnosis exit code: $baselineExit"
+ # The old package is evidence, not the replacement candidate. The
+ # new final bytes must pass the mandatory protection gate below.
+ exit 0
+ - name: Build Windows launcher
+ run: |
+ python scripts/build_windows.py --onefolder --out build/windows-folder
+ if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
+ python scripts/build_windows.py --out build/windows-exe
+ if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
+ Copy-Item build/windows-folder/dist/FreeVideo-Windows-folder.zip build/windows-exe/dist/
+ - name: Sign Windows launchers when a certificate is configured
+ shell: pwsh
+ env:
+ WINDOWS_CODESIGN_PFX_B64: ${{ secrets.WINDOWS_CODESIGN_PFX_B64 }}
+ WINDOWS_CODESIGN_PFX_PASSWORD: ${{ secrets.WINDOWS_CODESIGN_PFX_PASSWORD }}
+ WINDOWS_CODESIGN_TIMESTAMP_URL: ${{ secrets.WINDOWS_CODESIGN_TIMESTAMP_URL }}
+ run: |
+ ./scripts/sign_windows_release.ps1 `
+ -OneFile build/windows-exe/dist/FreeVideo.exe `
+ -FolderZip build/windows-exe/dist/FreeVideo-Windows-folder.zip `
+ -FolderExecutable build/windows-folder/dist/FreeVideo/FreeVideo.exe
+ - name: Hash final Windows launchers
+ shell: pwsh
+ run: |
+ python -c "from pathlib import Path; import hashlib; p=Path('build/windows-exe/dist'); (p/'SHA256SUMS.txt').write_text(''.join(hashlib.sha256(f.read_bytes()).hexdigest()+' '+f.name+'\n' for f in (p/'FreeVideo.exe', p/'FreeVideo-Windows-folder.zip')))"
+ # Keep the full report in the job log; a blocked or inconclusive scan
+ # must stop publication instead of making a suspicious build public.
+ - name: Open both packaged launchers and scan with real-time Defender
+ timeout-minutes: 20
+ shell: pwsh
+ run: |
+ python scripts/scan_windows_release.py --file build/windows-exe/dist/FreeVideo.exe --file build/windows-folder/dist/FreeVideo/FreeVideo.exe --file build/windows-exe/dist/FreeVideo-Windows-folder.zip --verify-launches build/windows-exe/dist/FreeVideo.exe build/windows-exe/dist/FreeVideo-Windows-folder.zip --report antivirus/report.json
+ $scanExit = $LASTEXITCODE
+ if (Test-Path antivirus/report.json) { Get-Content antivirus/report.json -Raw }
+ exit $scanExit
+ - name: Print retained protection and startup checks on failure
+ if: failure()
+ shell: pwsh
+ run: |
+ python -c "import json; from scripts.scan_windows_release import threat_history; print(json.dumps(threat_history(), indent=2))"
+ Get-ChildItem antivirus -File -ErrorAction SilentlyContinue | ForEach-Object { Write-Output $_.Name; Get-Content $_.FullName -Tail 100 }
+ Get-ChildItem (Join-Path $env:USERPROFILE 'Downloads/FreeVideo-release-check-*') -Recurse -File -ErrorAction SilentlyContinue |
+ Where-Object { $_.Name -eq 'smoke.json' } | ForEach-Object { Get-Content $_.FullName -Raw }
+ # Release assets use Releases storage, not the Actions artifact quota.
+ # This step still requires all installation/build/startup checks above.
+ - name: Publish Windows launcher
+ if: github.repository == 'FlashML-org/FreeVideo' && github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
+ working-directory: build/windows-exe/dist
+ env:
+ GH_TOKEN: ${{ github.token }}
+ GH_REPO: ${{ github.repository }}
+ shell: bash
+ run: |
+ set -euo pipefail
+ # Python writes CRLF on Windows; strip line endings before GNU checks.
+ tr -d '\r' < SHA256SUMS.txt | sha256sum --check -
+ # BEGIN RETRY HELPERS
+ gh_retry() {
+ local label="$1"; shift
+ local attempt code
+ for attempt in 1 2 3; do
+ echo "$label (attempt $attempt/3)" >&2
+ if timeout 300s "$@"; then return 0; else code=$?; fi
+ if [ "$attempt" -lt 3 ]; then sleep "$((attempt * 2))"; fi
+ done
+ echo "::error::$label failed after 3 attempts (exit $code)" >&2
+ return "$code"
+ }
+ gh_json() {
+ local label="$1"; shift
+ local attempt result
+ result=$(mktemp)
+ for attempt in 1 2 3; do
+ echo "$label (attempt $attempt/3)" >&2
+ if timeout 60s gh "$@" > "$result"; then
+ if python -m json.tool "$result" > /dev/null; then
+ cat "$result"; rm -f "$result"; return 0
+ fi
+ echo "$label returned empty or incomplete JSON; retrying the read" >&2
+ fi
+ if [ "$attempt" -lt 3 ]; then sleep "$((attempt * 2))"; fi
+ done
+ rm -f "$result"
+ echo "::error::$label failed: GitHub did not return a complete JSON response" >&2
+ return 1
+ }
+ # END RETRY HELPERS
+ # A single moving preview avoids retaining old development snapshots.
+ gh_json 'Read current main' api "repos/$GH_REPO/git/ref/heads/main" > main.json
+ current_main=$(python -c 'import json; print(json.load(open("main.json"))["object"]["sha"])' | tr -d '\r')
+ if [ "$current_main" != "$GITHUB_SHA" ]; then
+ # README editing must not discard a checked installer. Publish the
+ # exact built revision only when main is its README-only descendant.
+ # Runtime/workflow changes and force-push divergence still stop it.
+ gh_json 'Check changes after this build' api "repos/$GH_REPO/compare/$GITHUB_SHA...$current_main" > comparison.json
+ if ! python - <<'PY_CURRENT'
+ import json, os, sys
+ comparison = json.load(open('comparison.json'))
+ files = comparison.get('files')
+ allowed = {'README.md', 'README.zh-CN.md'}
+ safe = (comparison.get('status') == 'ahead'
+ and comparison.get('merge_base_commit', {}).get('sha') == os.environ['GITHUB_SHA']
+ and isinstance(files, list) and len(files) <= len(allowed)
+ and all(row.get('filename') in allowed
+ and row.get('previous_filename', row.get('filename')) in allowed
+ for row in files))
+ sys.exit(0 if safe else 1)
+ PY_CURRENT
+ then
+ exit 0
+ fi
+ fi
+ preview_tag=windows-preview
+ cat > release-notes.md <<'NOTES'
+ <div align="center">
+ <picture>
+ <source media="(prefers-color-scheme: dark)" srcset="https://freevideo-community.pages.dev/logo-dark.svg">
+ <img alt="FreeVideo" src="https://freevideo-community.pages.dev/logo-light.svg" width="55%">
+ </picture>
+ </div>
+
+ Run MiniMax H3 on your own GPU, with FreeVideo inside ComfyUI.
+
+ 1. [Download FreeVideo.exe](https://github.com/FlashML-org/FreeVideo/releases/download/windows-preview/FreeVideo.exe) and run it.
+ 2. Select an existing ComfyUI folder or install a new one. Add existing model folders to reuse your files; missing models download automatically.
+ 3. Click **Install & launch** to open the FreeVideo workspace in your browser.
+
+ **Offline installation:** Get the packages from [Quark](https://pan.quark.cn/s/c51235b84618) and drag the ZIP files into the launcher. No extraction needed. See the [installation guide](https://github.com/FlashML-org/FreeVideo#getting-started) for package choices.
+
+ ### 中文
+
+ FreeVideo 让 MiniMax H3 在消费级显卡上本地运行,打开 ComfyUI 即可创作。
+
+ 1. [下载 FreeVideo.exe](https://github.com/FlashML-org/FreeVideo/releases/download/windows-preview/FreeVideo.exe) 并运行。
+ 2. 选择已有的 ComfyUI 或安装新的 ComfyUI。可添加模型目录以复用文件,缺失的模型会自动下载。
+ 3. 点击 **安装并启动**,浏览器中会打开 FreeVideo 创作面板。
+
+ **离线安装:** 从[夸克网盘](https://pan.quark.cn/s/c51235b84618)下载离线包,将 ZIP 直接拖入启动器,无需解压。包的选择见[安装说明](https://github.com/FlashML-org/FreeVideo/blob/main/README.zh-CN.md#开始使用)。
+
+ [Discord](https://discord.gg/MsA277cJzZ) · [QQ Group / QQ 群](https://freevideo-community.pages.dev/qq) · [WeChat Group / 微信群](https://freevideo-community.pages.dev/wechat)
+ NOTES
+ # A failed read is not evidence that a tag or release is missing.
+ gh_json 'Read preview tags' api "repos/$GH_REPO/git/matching-refs/tags/$preview_tag" > tags.json
+ tag_exists=$(python -c 'import json; print(int(any(r["ref"] == "refs/tags/windows-preview" for r in json.load(open("tags.json")))))' | tr -d '\r')
+ gh_json 'Read releases including drafts' api --paginate --slurp "repos/$GH_REPO/releases?per_page=100" > releases.json
+ release_exists=$(python -c 'import json; print(int(any(r["tag_name"] == "windows-preview" for page in json.load(open("releases.json")) for r in page)))' | tr -d '\r')
+ if [ "$tag_exists" = 1 ]; then
+ gh_retry 'Update preview tag' gh api -X PATCH "repos/$GH_REPO/git/refs/tags/$preview_tag" -f sha="$GITHUB_SHA" -F force=true > /dev/null
+ else
+ gh_retry 'Create preview tag' gh api -X POST "repos/$GH_REPO/git/refs" -f ref="refs/tags/$preview_tag" -f sha="$GITHUB_SHA" > /dev/null
+ fi
+ if [ "$release_exists" = 0 ]; then
+ gh_retry 'Create draft release' gh release create "$preview_tag" --verify-tag --draft --prerelease --latest=false --title 'FreeVideo for Windows' --notes-file release-notes.md
+ fi
+ gh_retry 'Upload launcher' gh release upload "$preview_tag" FreeVideo.exe FreeVideo-Windows-folder.zip SHA256SUMS.txt --clobber
+ # Publish metadata last, bound to the immutable executable asset ID.
+ # Readers reject a mismatched pair while a moving preview is updating.
+ gh_json 'Locate release including drafts' release view "$preview_tag" --json databaseId > release-id.json
+ release_id=$(python -c 'import json; print(json.load(open("release-id.json"))["databaseId"])' | tr -d '\r')
+ gh_json 'Read uploaded asset metadata' api "repos/$GH_REPO/releases/$release_id" > release.json
+ python - <<'PY'
+ import hashlib, json
+ from pathlib import Path
+ build = json.loads(Path('launcher-build.json').read_text())
+ release = json.loads(Path('release.json').read_text())
+ asset = next(a for a in release['assets'] if a['name'] == 'FreeVideo.exe')
+ binary = Path('FreeVideo.exe')
+ assert asset['size'] == binary.stat().st_size
+ build['asset'] = dict(id=asset['id'], bytes=binary.stat().st_size,
+ sha256=hashlib.sha256(binary.read_bytes()).hexdigest())
+ Path('update.json').write_text(json.dumps(build, indent=2))
+ PY
+ gh_retry 'Upload update manifest' gh release upload "$preview_tag" update.json --clobber
+ gh_retry 'Publish release' gh release edit "$preview_tag" --draft=false --prerelease --latest=false --title 'FreeVideo for Windows' --notes-file release-notes.md
+ # Previous development snapshots have been archived privately.
+ # Remove their release/source-archive references only after replacement.
+ python - <<'PY_CLEANUP'
+ import json
+ from pathlib import Path
+ releases = json.loads(Path('releases.json').read_text())
+ tags = [r['tag_name'] for page in releases for r in page
+ if r['tag_name'].startswith('windows-preview-')]
+ Path('previous-previews.txt').write_text(''.join(tag + '\n' for tag in tags))
+ PY_CLEANUP
+ while IFS= read -r previous; do
+ previous=${previous%$'\r'}
+ gh_retry 'Remove archived preview' gh release delete "$previous" --cleanup-tag --yes
+ done < previous-previews.txt
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..83019f5
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,59 @@
+__pycache__/
+*.py[cod]
+.pytest_cache/
+.venv/
+.gui-build/
+ci-artifacts/
+*.egg-info/
+build/
+dist/
+.env
+.env.*
+!.env.example
+*.log
+*.partial
+models/
+outputs/
+.cache/
+*.safetensors
+*.pt
+*.pth
+*.mp4
+*.wav
+*.whl
+*.zip
+*.tar*
+*.bundle
+*.trace.json
+*.jsonl
+*.npy
+*.npz
+.freevideo/
+test-runs/
+resource-history.sqlite3*
+setup-runs/
+envs/
+vendor/
+downloads/
+prepared/
+kernel-cache/
+wheels/
+machine.json
+prepared-cache.json
+verified-models.json
+verified-cache.json
+encoder-paths.yaml
+setup.lock
+engine.lock
+# One lease per device, beside the machine lease.
+engine-gpu-*.lock
+/python/
+/tools/
+/tuning/
+/input-cache/
+/comfyui.json
+/optimization-runs/
+
+# Optional local prompt models, runtime binaries and private rewrite caches.
+/optional/
+*.gguf
diff --git a/CITATION.cff b/CITATION.cff
new file mode 100644
index 0000000..bca16db
--- /dev/null
+++ b/CITATION.cff
@@ -0,0 +1,49 @@
+cff-version: 1.2.0
+message: "FreeVideo runs VDN-H3. If you use FreeVideo in your research, please cite the Video DeltaNet paper."
+title: FreeVideo
+type: software
+license: Apache-2.0
+repository-code: "https://github.com/FlashML-org/FreeVideo"
+authors:
+ - given-names: Bowen
+ family-names: Xue
+ - given-names: Shuo
+ family-names: Yang
+ - given-names: Haocheng
+ family-names: Xi
+ - given-names: Xiaoze
+ family-names: Fan
+ - given-names: Chenfeng
+ family-names: Xu
+preferred-citation:
+ type: article
+ title: "Video DeltaNet: A Video-Native Hybrid Attention for Livestream Video Generation"
+ journal: "arXiv preprint arXiv:2609.20744"
+ year: 2026
+ url: "https://arxiv.org/abs/2609.20744"
+ identifiers:
+ - type: other
+ value: "arXiv:2609.20744"
+ authors:
+ - given-names: Haocheng
+ family-names: Xi
+ - given-names: Yiming
+ family-names: Xie
+ - given-names: Hexu
+ family-names: Zhao
+ - given-names: Yiwen
+ family-names: Zhang
+ - given-names: Michael
+ family-names: Liu
+ - given-names: Thomas
+ family-names: Creavin
+ - given-names: Kurt
+ family-names: Keutzer
+ - given-names: Xiuyu
+ family-names: Li
+ - given-names: Zhaoyang
+ family-names: Lv
+ - given-names: Chenfeng
+ family-names: Xu
+ - given-names: Haiwen
+ family-names: Feng
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 0000000..a01f99f
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1,19 @@
+# Contributing / 参与贡献
+
+Bug reports, suggestions, documentation and code are welcome, in English or Chinese.
+
+- **Feedback:** open an [issue](https://github.com/FlashML-org/FreeVideo/issues) with a short description and the FreeVideo version. A local diagnostic report helps.
+- **Pull requests:** explain the change and how you checked it. For performance changes, include before/after measurements with the same settings.
+
+AI-assisted contributions are welcome. Please understand the changes you submit.
+
+Contact: [Discord](https://discord.gg/MsA277cJzZ) · [QQ Group](https://freevideo-community.pages.dev/qq) · [WeChat Group](https://freevideo-community.pages.dev/wechat).
+
+欢迎用中文或英文反馈问题、提出建议、改进文档或提交代码。
+
+- **反馈问题:**在 [Issues](https://github.com/FlashML-org/FreeVideo/issues) 简单说明问题和 FreeVideo 版本。如方便,可附本地诊断报告。
+- **提交改动:**说明改了什么、如何验证。性能改动可附相同配置下的前后对比。
+
+欢迎使用 AI 辅助贡献,请理解自己提交的改动。
+
+联系:[Discord](https://discord.gg/MsA277cJzZ) · [QQ 群](https://freevideo-community.pages.dev/qq) · [微信群](https://freevideo-community.pages.dev/wechat)。
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..d645695
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
diff --git a/MANIFEST.in b/MANIFEST.in
new file mode 100644
index 0000000..e203af5
--- /dev/null
+++ b/MANIFEST.in
@@ -0,0 +1,21 @@
+include setup.sh test.sh freevideo LICENSE .gitignore .gitattributes .editorconfig
+include setup.ps1 test.ps1 freevideo.ps1 setup.cmd test.cmd freevideo.cmd
+include __init__.py README.zh-CN.md
+include NOTICE THIRD_PARTY_NOTICES.md CONTRIBUTING.md SECURITY.md CITATION.cff
+recursive-include example_workflows *.json
+recursive-include web *.js *.css
+recursive-include web/assets *.svg *.png
+recursive-include freevideo_engine/assets *.png *.ico
+recursive-include freevideo_engine/launcher *.qml
+recursive-include freevideo_engine/launcher/licenses *.txt
+recursive-include freevideo_engine/licenses *.txt
+recursive-include constraints *.txt
+include scripts/bootstrap_linux.sh scripts/build_windows.py scripts/windows_app.py scripts/check_installation.py
+include scripts/scan_windows_release.py
+include scripts/build_launcher_notices.py
+prune docs
+prune tests
+prune benchmarks
+prune distribution
+prune .github
+exclude AGENTS.md CHANGELOG.md
diff --git a/NOTICE b/NOTICE
new file mode 100644
index 0000000..8e4a569
--- /dev/null
+++ b/NOTICE
@@ -0,0 +1,9 @@
+FreeVideo
+Copyright 2026 FreeVideo contributors.
+
+FreeVideo is licensed under the Apache License, Version 2.0 (see LICENSE).
+Third-party components are listed in THIRD_PARTY_NOTICES.md.
+
+FreeVideo runs VDN-H3, derived from MiniMax H3. Model weights retain their own license:
+MiniMax H3 is licensed under the MiniMax H3 Community License Agreement, Copyright © 2026 MiniMax. All Rights Reserved.
+https://huggingface.co/OpenVDN/vdn-minimax-h3-edge/blob/main/LICENSE
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e7a920c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,113 @@
+<div align="center">
+ <picture>
+ <source media="(prefers-color-scheme: dark)" srcset="web/assets/freevideo.svg">
+ <img alt="FreeVideo" src="web/assets/freevideo-light.svg" width="65%">
+ </picture>
+</div>
+
+<p align="center">
+| <a href="https://github.com/FlashML-org/FreeVideo/releases/download/windows-preview/FreeVideo.exe"><b>Download</b></a> | <a href="https://discord.gg/MsA277cJzZ"><b>Discord</b></a> | <a href="https://freevideo-community.pages.dev/qq"><b>QQ Group</b></a> | <a href="https://freevideo-community.pages.dev/wechat"><b>WeChat Group</b></a> |
+</p>
+
+<p align="center">English · <a href="README.zh-CN.md">中文</a></p>
+
+Make videos on the computer you already own. FreeVideo runs MiniMax H3 in as little as 8 GB of VRAM and 16 GB of RAM, and adapts its acceleration path to your&nbsp;hardware.
+
+https://github.com/user-attachments/assets/4d391114-1b21-47d9-ba61-3feaf4ff9fe0
+
+## About
+
+FreeVideo is a local inference engine for running MiniMax H3 on consumer GPUs. Built on OpenVDN's 8-step [VDN-H3](https://huggingface.co/OpenVDN/vdn-minimax-h3) model, it schedules VRAM, system memory and disk as a single memory hierarchy, and plans weight placement, compute precision and attention kernels according to the GPU architecture and available resources. FreeVideo is delivered as a ComfyUI plugin: the Windows launcher installs and starts ComfyUI, and on Linux FreeVideo can also be used from the command line. Its core features include:
+
+- **Hardware-adaptive execution**: Chooses the FP8 compute path for each GPU architecture, either native FP8 or FP8 storage with BF16 compute, and automatically probes the available attention kernels.
+- **Low-memory inference**: Weight streaming, asynchronous prefetching and chunked computation keep peak memory low, enabling inference with as little as 8 GB of VRAM and 16 GB of RAM.
+- **Multimodal inputs**: Text prompts, first and last frames, and image, video and audio references.
+- **ComfyUI integration**: A dedicated creative workspace inside ComfyUI that supports two-pass sampling and batch generation and keeps a history of past creations. For finer control, switch to the node view to add LoRAs or customize the workflow.
+- **One-click deployment**: The Windows launcher sets up ComfyUI, the runtime environment and the models, reuses existing models, and supports offline installation.
+
+## Getting Started
+
+### Windows
+
+1. [Download FreeVideo.exe](https://github.com/FlashML-org/FreeVideo/releases/download/windows-preview/FreeVideo.exe) and run it.
+2. Select an existing ComfyUI folder or install a new one. Existing model folders can be added for reuse; missing models are downloaded automatically.
+3. Click **Install & launch**. ComfyUI opens in the browser with the FreeVideo workspace.
+
+<div align="center">
+ <img alt="FreeVideo creative workspace" src="https://github.com/user-attachments/assets/7647a6f8-4306-403c-b147-45d7a393e18d" width="92%">
+</div>
+
+**Offline installation:** Download the packages from [Quark](https://pan.quark.cn/s/c51235b84618) and drag the ZIP files into the launcher without extracting them. The common models and the model pack for your GPU (30/40 series or 50 series) are required; a new ComfyUI installation also requires the environment package.
+
+### Existing ComfyUI
+
+Install FreeVideo as a custom node:
+
+```bash
+cd ComfyUI/custom_nodes
+git clone https://github.com/FlashML-org/FreeVideo.git
+```
+
+Restart ComfyUI, open **Workflow → Browse Templates → FreeVideo → FreeVideo-All-in-One**, and complete the setup in FreeVideo **Settings**.
+
+### Linux
+
+Install:
+
+```bash
+git clone https://github.com/FlashML-org/FreeVideo.git && cd FreeVideo
+./setup.sh
+```
+
+Generate a video from a prompt file:
+
+```bash
+./freevideo generate --prompt-file prompt.txt --out video.mp4
+```
+
+### More details
+
+- [FreeVideo Adaptive Execution Planner](docs/execution-planning.md)
+
+### Support
+
+Report bugs in [GitHub Issues](https://github.com/FlashML-org/FreeVideo/issues), or ask questions on [Discord](https://discord.gg/MsA277cJzZ), [QQ](https://freevideo-community.pages.dev/qq) or [WeChat](https://freevideo-community.pages.dev/wechat).
+
+## Citation
+
+FreeVideo is based on VDN-H3. If you use FreeVideo in your research, please cite the [Video DeltaNet paper](https://arxiv.org/abs/2609.20744):
+
+```bibtex
+@article{xi2026videodeltanet,
+ title={Video DeltaNet: A Video-Native Hybrid Attention for Livestream Video Generation},
+ author={Xi, Haocheng and Xie, Yiming and Zhao, Hexu and Zhang, Yiwen and Liu, Michael and Creavin, Thomas and Keutzer, Kurt and Li, Xiuyu and Lv, Zhaoyang and Xu, Chenfeng and Feng, Haiwen},
+ journal={arXiv preprint arXiv:2609.20744},
+ year={2026}
+}
+```
+
+## Team
+
+### Project Team
+
+[Bowen Xue](https://github.com/KBRASK) · [Shuo Yang](https://github.com/andy-yang-1) · [Haocheng Xi](https://github.com/haochengxi) · [Xiaoze Fan](https://github.com/jason-fxz) · [Chenfeng Xu](https://github.com/chenfengxu714)
+
+### Special Thanks
+
+Special thanks to [**AIwood爱屋研究室**](https://space.bilibili.com/503934057) and [**T8star-Aix**](https://space.bilibili.com/385085361) for testing the project and providing valuable feedback.
+
+*Listed in chronological order of participation.*
+
+## Acknowledgment
+
+FreeVideo is built on [MiniMax H3](https://huggingface.co/MiniMaxAI/MiniMax-H3) and [VDN-H3](https://github.com/OpenVDN/vdn-minimax-h3), and uses the following projects:
+[ComfyUI](https://github.com/Comfy-Org/ComfyUI),
+[Diffusers](https://github.com/huggingface/diffusers),
+[SageAttention](https://github.com/thu-ml/SageAttention),
+the [MiniMax H3 latent upscaler](https://huggingface.co/LBH-123-AI/Minimax_h3_latent_Upscaler),
+the [H3 text encoder for ComfyUI](https://huggingface.co/t8star/Vdn-Minimax-H3-Comfy) and
+[Qt for Python](https://doc.qt.io/qtforpython-6/).
+
+## License
+
+The code is released under the [Apache License 2.0](LICENSE). The model weights are licensed under the [MiniMax H3 Community License](https://huggingface.co/OpenVDN/vdn-minimax-h3-edge/blob/main/LICENSE), which includes territorial and acceptable-use restrictions.
diff --git a/README.zh-CN.md b/README.zh-CN.md
new file mode 100644
index 0000000..bf631e7
--- /dev/null
+++ b/README.zh-CN.md
@@ -0,0 +1,113 @@
+<div align="center">
+ <picture>
+ <source media="(prefers-color-scheme: dark)" srcset="web/assets/freevideo.svg">
+ <img alt="FreeVideo" src="web/assets/freevideo-light.svg" width="65%">
+ </picture>
+</div>
+
+<p align="center">
+| <a href="https://github.com/FlashML-org/FreeVideo/releases/download/windows-preview/FreeVideo.exe"><b>下载</b></a> | <a href="https://discord.gg/MsA277cJzZ"><b>Discord</b></a> | <a href="https://freevideo-community.pages.dev/qq"><b>QQ 群</b></a> | <a href="https://freevideo-community.pages.dev/wechat"><b>微信群</b></a> |
+</p>
+
+<p align="center"><a href="README.md">English</a> · 中文</p>
+
+FreeVideo 让 MiniMax H3 能够在消费级显卡上本地运行,最低只需 8GB 显存和 16GB 内存,并会根据硬件配置自动选择合适的加速路径。
+
+https://github.com/user-attachments/assets/4d391114-1b21-47d9-ba61-3feaf4ff9fe0
+
+## 简介
+
+FreeVideo 是在消费级显卡上运行 MiniMax H3 的本地推理引擎。它基于 OpenVDN 的 8 步模型 [VDN-H3](https://huggingface.co/OpenVDN/vdn-minimax-h3),将显存、内存与磁盘作为统一的分级存储进行调度,并根据显卡架构与可用资源规划权重放置、计算精度和注意力内核。FreeVideo 以 ComfyUI 插件的形式提供:Windows 启动器用于安装和启动 ComfyUI,Linux 上也可以通过命令行使用。主要特性包括:
+
+- **硬件自适应**:针对不同显卡架构选择 FP8 计算路径(原生 FP8,或 FP8 存储配合 BF16 计算),并自动探测可用的注意力内核,无需手动配置。
+- **低显存推理**:通过权重流式加载、异步预取与分块计算降低峰值显存,最低只需 8GB 显存和 16GB 内存。
+- **多模态输入**:支持文本、首帧、尾帧,以及图像、视频、音频参考输入。
+- **ComfyUI 集成**:在 ComfyUI 中提供专门的创作面板,支持二次采样和批量生成,并可浏览历史作品;需要更精细的控制时,可切换到节点视图,接入 LoRA 或自定义工作流。
+- **一键部署**:Windows 启动器自动完成 ComfyUI、运行环境与模型的部署,可复用已有模型,并支持离线安装。
+
+## 开始使用
+
+### Windows
+
+1. [下载 FreeVideo.exe](https://github.com/FlashML-org/FreeVideo/releases/download/windows-preview/FreeVideo.exe) 并运行。
+2. 选择已有的 ComfyUI 目录或安装新的 ComfyUI。可添加已有的模型目录以复用文件,缺失的模型会自动下载。
+3. 点击 **安装并启动**,浏览器中会打开带有 FreeVideo 创作面板的 ComfyUI。
+
+<div align="center">
+ <img alt="FreeVideo 创作面板" src="https://github.com/user-attachments/assets/f4719079-bb48-4836-9c64-2473477cd1a0" width="92%">
+</div>
+
+**离线安装:** 从[夸克网盘](https://pan.quark.cn/s/c51235b84618)下载离线包,将 ZIP 文件直接拖入启动器,无需解压。需要「公用模型」包和对应显卡的模型包(30/40 系或 50 系);全新安装 ComfyUI 时还需要「运行环境」包。
+
+### 已有 ComfyUI
+
+以自定义节点方式安装:
+
+```bash
+cd ComfyUI/custom_nodes
+git clone https://github.com/FlashML-org/FreeVideo.git
+```
+
+重启 ComfyUI,打开 **工作流 → 浏览模板 → FreeVideo → FreeVideo-All-in-One**,然后在 FreeVideo 的 **设置** 中完成安装。
+
+### Linux
+
+安装:
+
+```bash
+git clone https://github.com/FlashML-org/FreeVideo.git && cd FreeVideo
+./setup.sh
+```
+
+从提示词文件生成视频:
+
+```bash
+./freevideo generate --prompt-file prompt.txt --out video.mp4
+```
+
+### 更多文档
+
+- [FreeVideo Adaptive Execution Planner](docs/zh-CN/execution-planning.md)
+
+### 问题反馈
+
+问题和建议请提交至 [GitHub Issues](https://github.com/FlashML-org/FreeVideo/issues),也可以在 [Discord](https://discord.gg/MsA277cJzZ)、[QQ 群](https://freevideo-community.pages.dev/qq)或[微信群](https://freevideo-community.pages.dev/wechat)中交流。
+
+## 引用
+
+FreeVideo 基于 VDN-H3。如在研究中使用 FreeVideo,请引用 [Video DeltaNet 论文](https://arxiv.org/abs/2609.20744):
+
+```bibtex
+@article{xi2026videodeltanet,
+ title={Video DeltaNet: A Video-Native Hybrid Attention for Livestream Video Generation},
+ author={Xi, Haocheng and Xie, Yiming and Zhao, Hexu and Zhang, Yiwen and Liu, Michael and Creavin, Thomas and Keutzer, Kurt and Li, Xiuyu and Lv, Zhaoyang and Xu, Chenfeng and Feng, Haiwen},
+ journal={arXiv preprint arXiv:2609.20744},
+ year={2026}
+}
+```
+
+## 团队
+
+### 项目团队
+
+[Bowen Xue](https://github.com/KBRASK) · [Shuo Yang](https://github.com/andy-yang-1) · [Haocheng Xi](https://github.com/haochengxi) · [Xiaoze Fan](https://github.com/jason-fxz) · [Chenfeng Xu](https://github.com/chenfengxu714)
+
+### 特别感谢
+
+特别感谢 [**AIwood爱屋研究室**](https://space.bilibili.com/503934057) 和 [**T8star-Aix**](https://space.bilibili.com/385085361) 参与项目测试并提供宝贵反馈。
+
+*按参与时间先后排序。*
+
+## 致谢
+
+FreeVideo 基于 [MiniMax H3](https://huggingface.co/MiniMaxAI/MiniMax-H3) 和 [VDN-H3](https://github.com/OpenVDN/vdn-minimax-h3) 构建,并使用了以下项目:
+[ComfyUI](https://github.com/Comfy-Org/ComfyUI)、
+[Diffusers](https://github.com/huggingface/diffusers)、
+[SageAttention](https://github.com/thu-ml/SageAttention)、
+[MiniMax H3 潜空间放大模型](https://huggingface.co/LBH-123-AI/Minimax_h3_latent_Upscaler)、
+[ComfyUI 版 H3 文本编码器](https://huggingface.co/t8star/Vdn-Minimax-H3-Comfy)和
+[Qt for Python](https://doc.qt.io/qtforpython-6/)。
+
+## 许可证
+
+代码采用 [Apache License 2.0](LICENSE) 授权。模型权重适用 [MiniMax H3 社区许可证](https://huggingface.co/OpenVDN/vdn-minimax-h3-edge/blob/main/LICENSE),该许可证对使用地区和用途有所限制。
diff --git a/SECURITY.md b/SECURITY.md
new file mode 100644
index 0000000..7a91bbf
--- /dev/null
+++ b/SECURITY.md
@@ -0,0 +1,5 @@
+# Security / 安全反馈
+
+For security issues, contact a maintainer privately via [Discord](https://discord.gg/MsA277cJzZ), [QQ Group](https://freevideo-community.pages.dev/qq) or [WeChat Group](https://freevideo-community.pages.dev/wechat).
+
+安全问题请通过 [Discord](https://discord.gg/MsA277cJzZ)、[QQ 群](https://freevideo-community.pages.dev/qq) 或[微信群](https://freevideo-community.pages.dev/wechat)私信维护者。
diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md
new file mode 100644
index 0000000..9335523
--- /dev/null
+++ b/THIRD_PARTY_NOTICES.md
@@ -0,0 +1,17 @@
+# Third-party notices
+
+FreeVideo code uses [Apache-2.0](LICENSE). Its dependencies and models retain their own licenses.
+
+| Component | License |
+| --- | --- |
+| Qt, PySide6 and Shiboken6 | LGPL-3.0; [component notices](freevideo_engine/launcher/licenses/NOTICE.txt) |
+| PyInstaller bootloader | GPL-2.0 with the bootloader exception |
+| CPython / psutil | PSF / BSD-3-Clause |
+| [ComfyUI](https://github.com/Comfy-Org/ComfyUI), including the H3 text encoder code | GPL-3.0 |
+| [VDN-H3](https://github.com/OpenVDN/vdn-minimax-h3), [Diffusers](https://github.com/huggingface/diffusers), [SageAttention](https://github.com/thu-ml/SageAttention) | Apache-2.0 |
+| [VDN-H3 and H3 model weights](https://huggingface.co/OpenVDN/vdn-minimax-h3-edge/blob/main/LICENSE) | MiniMax H3 Community License |
+| [MiniMax H3 latent upscaler](https://huggingface.co/LBH-123-AI/Minimax_h3_latent_Upscaler) | [MIT](freevideo_engine/licenses/latent-upscaler-MIT.txt) |
+
+Original launcher license texts are included in Windows packages. Other runtime dependencies are listed in [constraints](constraints), with their license information in the installed packages.
+
+`decode_stream.py`, `lora_cache.py` and `reference_sampler.py` include adaptations of upstream code; their source headers retain the attribution.
diff --git a/__init__.py b/__init__.py
new file mode 100644
index 0000000..b64ed3d
--- /dev/null
+++ b/__init__.py
@@ -0,0 +1,17 @@
+"""Optional ComfyUI custom-node entry point; the engine also runs standalone."""
+WEB_DIRECTORY = './web'
+
+
+async def comfy_entrypoint():
+ from .freevideo_engine.comfy_progress import register as progress_routes
+ progress_routes()
+ from .freevideo_engine.comfy_upload import register
+ register()
+ from .freevideo_engine.comfy_setup import register as setup_routes
+ setup_routes()
+ from .freevideo_engine.comfy_launcher_api import register as launcher_routes
+ launcher_routes()
+ from .freevideo_engine.comfy_library import register as library_routes
+ library_routes()
+ from .freevideo_engine.comfy_nodes import FreeVideoExtension
+ return FreeVideoExtension()
diff --git a/constraints/encoder-runtime.txt b/constraints/encoder-runtime.txt
new file mode 100644
index 0000000..1775455
--- /dev/null
+++ b/constraints/encoder-runtime.txt
@@ -0,0 +1,19 @@
+# Native H3 text-encoder library only; no ComfyUI app, frontend, nodes or server.
+# Versions are fixed by unified.txt or encoder.txt for the selected layout.
+# Torch is installed from its CUDA index first.
+torchsde
+numpy
+einops
+transformers
+tokenizers
+sentencepiece
+safetensors
+pyyaml
+pillow
+scipy
+tqdm
+psutil
+comfy-kitchen
+comfy-aimdo
+requests
+av
diff --git a/constraints/encoder.txt b/constraints/encoder.txt
new file mode 100644
index 0000000..f4fcf82
--- /dev/null
+++ b/constraints/encoder.txt
@@ -0,0 +1,69 @@
+# Pinned standalone native H3 encoder dependencies; no UI or server packages.
+annotated-doc==0.0.5
+anyio==4.15.1
+av==18.1.0
+certifi==2026.7.22
+charset-normalizer==3.5.1
+click==8.5.0
+comfy-aimdo==0.5.3
+comfy-kitchen==0.2.33
+cuda-bindings==13.3.1
+cuda-pathfinder==1.6.0
+cuda-toolkit==13.0.3.0
+einops==0.8.2
+filelock==3.32.3
+fsspec==2026.7.0
+h11==0.16.0
+hf-xet==1.6.0
+httpcore==1.0.9
+httpx==0.28.1
+huggingface_hub==1.30.0
+idna==3.19
+Jinja2==3.1.6
+markdown-it-py==4.2.0
+MarkupSafe==3.0.3
+mdurl==0.1.2
+mpmath==1.3.0
+networkx==3.6.1
+numpy==2.5.2
+nvidia-cublas==13.1.1.3
+nvidia-cuda-cupti==13.0.85
+nvidia-cuda-nvrtc==13.0.88
+nvidia-cuda-runtime==13.0.96
+nvidia-cudnn-cu13==9.24.0.43
+nvidia-cufft==12.0.0.61
+nvidia-cufile==1.15.1.6
+nvidia-curand==10.4.0.35
+nvidia-cusolver==12.0.4.66
+nvidia-cusparse==12.6.3.3
+nvidia-cusparselt-cu13==0.8.1
+nvidia-nccl-cu13==2.30.7
+nvidia-nvjitlink==13.3.33
+nvidia-nvshmem-cu13==3.4.5
+nvidia-nvtx==13.0.85
+packaging==26.3
+pillow==12.3.0
+psutil==7.2.2
+Pygments==2.21.0
+PyYAML==6.0.3
+regex==2026.9.3
+requests==2.34.2
+rich==15.0.0
+safetensors==0.8.0
+scipy==1.18.1
+sentencepiece==0.2.2
+setuptools==78.1.0
+shellingham==1.5.4
+sympy==1.14.0
+tokenizers==0.23.2
+torch==2.14.0+cu130
+torchaudio==2.11.0+cu130
+torchsde==0.2.6
+torchvision==0.29.0+cu130
+tqdm==4.70.0
+trampoline==0.1.2
+transformers==5.16.1
+triton==3.8.0
+typer==0.27.2
+typing_extensions==4.16.0
+urllib3==2.7.0
diff --git a/constraints/engine.txt b/constraints/engine.txt
new file mode 100644
index 0000000..7f56b90
--- /dev/null
+++ b/constraints/engine.txt
@@ -0,0 +1,84 @@
+# Validated Engine environment; Torch is installed separately from the CUDA index.
+accelerate==1.14.0
+annotated-doc==0.0.5
+antlr4-python3-runtime==4.9.3
+anyio==4.15.1
+apache-tvm-ffi==0.1.14rc3
+av==18.1.0
+certifi==2026.7.22
+cffi==2.1.1
+cryptography==50.0.1
+charset-normalizer==3.5.1
+click==8.5.0
+cuda-bindings==12.9.7
+cuda-pathfinder==1.6.0
+cuda-python==12.9.7
+cuda-toolkit==12.9.1
+einops==0.8.2
+filelock==3.32.3
+fsspec==2026.7.0
+h11==0.16.0
+hf-xet==1.6.1a0
+httpcore==1.0.9
+httpx==0.28.1
+huggingface-hub==1.30.0
+modelscope-hub==0.4.2
+pycparser==3.0
+pysocks==1.7.1
+socksio==1.0.0
+idna==3.19
+importlib-metadata==9.0.1
+jinja2==3.1.6
+markdown-it-py==4.2.0
+markupsafe==3.0.3
+mdurl==0.1.2
+mpmath==1.3.0
+networkx==3.6.1
+ninja==1.13.2
+numpy==2.5.2
+nvidia-cublas-cu12==12.9.1.4
+nvidia-cuda-cupti-cu12==12.9.79
+nvidia-cuda-nvrtc-cu12==12.9.86
+nvidia-cuda-runtime-cu12==12.9.79
+nvidia-cudnn-cu12==9.20.0.48
+nvidia-cufft-cu12==11.4.1.4
+nvidia-cufile-cu12==1.14.1.1
+nvidia-curand-cu12==10.3.10.19
+nvidia-cusolver-cu12==11.7.5.82
+nvidia-cusparse-cu12==12.5.10.65
+nvidia-cusparselt-cu12==0.8.1
+nvidia-cutlass-dsl-libs-base==4.6.0.dev0
+nvidia-cutlass-dsl==4.6.0.dev0
+nvidia-nccl-cu12==2.29.7
+nvidia-nvjitlink-cu12==12.9.86
+nvidia-nvshmem-cu12==3.4.5
+nvidia-nvtx-cu12==12.9.79
+omegaconf==2.3.1
+packaging==26.3
+peft==0.20.0
+pillow==12.3.0
+pip==26.2.1
+protobuf==6.33.6
+psutil==7.2.2
+pygments==2.21.0
+pyyaml==6.0.3
+quack-kernels==0.5.3
+regex==2026.9.3
+requests==2.34.2
+rich==15.0.0
+safetensors==0.8.0
+setuptools==78.1.0
+shellingham==1.5.4
+sympy==1.14.0
+tokenizers==0.23.0rc0
+torch-c-dlpack-ext==0.1.5
+torch==2.13.0+cu129
+torchvision==0.28.0+cu129
+tqdm==4.70.0
+transformers==5.15.0
+triton==3.7.1
+typer==0.27.2
+typing-extensions==4.16.0
+urllib3==2.7.0
+wheel==0.48.0
+zipp==4.1.0
diff --git a/constraints/unified.txt b/constraints/unified.txt
new file mode 100644
index 0000000..882a9d6
--- /dev/null
+++ b/constraints/unified.txt
@@ -0,0 +1,85 @@
+# Shared Engine and native H3 encoder; Torch wheels come from the cu130 index.
+# Measured together; native source, Diffusers and Sage2 are pinned separately.
+accelerate==1.14.0
+annotated-doc==0.0.5
+antlr4-python3-runtime==4.9.3
+anyio==4.15.1
+av==18.1.0
+certifi==2026.7.22
+cffi==2.1.1
+charset-normalizer==3.5.1
+click==8.5.0
+comfy-aimdo==0.5.3
+comfy-kitchen==0.2.33
+cuda-bindings==13.3.1
+cuda-pathfinder==1.6.0
+cuda-toolkit==13.0.3.0
+cryptography==50.0.1
+einops==0.8.2
+filelock==3.32.3
+fsspec==2026.7.0
+h11==0.16.0
+hf-xet==1.6.1a0
+httpcore==1.0.9
+httpx==0.28.1
+huggingface-hub==1.30.0
+idna==3.19
+importlib-metadata==9.0.1
+jinja2==3.1.6
+markdown-it-py==4.2.0
+markupsafe==3.0.3
+mdurl==0.1.2
+mpmath==1.3.0
+modelscope-hub==0.4.2
+networkx==3.6.1
+ninja==1.13.2
+numpy==2.5.2
+nvidia-cublas==13.1.1.3
+nvidia-cuda-cupti==13.0.85
+nvidia-cuda-nvrtc==13.0.88
+nvidia-cuda-runtime==13.0.96
+nvidia-cudnn-cu13==9.20.0.48
+nvidia-cufft==12.0.0.61
+nvidia-cufile==1.15.1.6
+nvidia-curand==10.4.0.35
+nvidia-cusolver==12.0.4.66
+nvidia-cusparse==12.6.3.3
+nvidia-cusparselt-cu13==0.8.1
+nvidia-nccl-cu13==2.29.7
+nvidia-nvjitlink==13.3.33
+nvidia-nvshmem-cu13==3.4.5
+nvidia-nvtx==13.0.85
+omegaconf==2.3.1
+packaging==26.3
+peft==0.20.0
+pillow==12.3.0
+pip==26.2.1
+psutil==7.2.2
+pygments==2.21.0
+pycparser==3.0
+pysocks==1.7.1
+pyyaml==6.0.3
+regex==2026.9.3
+requests==2.34.2
+rich==15.0.0
+safetensors==0.8.0
+scipy==1.18.1
+sentencepiece==0.2.2
+setuptools==78.1.0
+shellingham==1.5.4
+sympy==1.14.0
+socksio==1.0.0
+tokenizers==0.23.0rc0
+torch==2.13.0+cu130
+torchaudio==2.11.0+cu130
+torchsde==0.2.6
+torchvision==0.28.0+cu130
+tqdm==4.70.0
+trampoline==0.1.2
+transformers==5.15.0
+triton==3.7.1
+typer==0.27.2
+typing-extensions==4.16.0
+urllib3==2.7.0
+wheel==0.48.0
+zipp==4.1.0
diff --git a/constraints/windows-launcher.txt b/constraints/windows-launcher.txt
new file mode 100644
index 0000000..d78c725
--- /dev/null
+++ b/constraints/windows-launcher.txt
@@ -0,0 +1,4 @@
+# Native launcher build dependencies; inference runs in its managed environment.
+pyinstaller==6.22.3
+psutil==7.2.2
+PySide6-Essentials==6.11.2
diff --git a/constraints/windows-portable.txt b/constraints/windows-portable.txt
new file mode 100644
index 0000000..5d57608
--- /dev/null
+++ b/constraints/windows-portable.txt
@@ -0,0 +1,105 @@
+# Native Windows portable runtime, including pinned ComfyUI dependencies.
+# Diffusers is built from the patched source in dependencies.json, never PyPI.
+accelerate==1.14.0
+aiohappyeyeballs==2.7.1
+aiohttp==3.14.3
+aiosignal==1.4.0
+alembic==1.20.0
+annotated-doc==0.0.5
+annotated-types==0.8.0
+antlr4-python3-runtime==4.9.3
+anyio==4.15.1
+attrs==26.1.0
+av==18.1.0
+blake3==1.0.9
+certifi==2026.7.22
+cffi==2.1.1
+charset-normalizer==3.5.1
+click==8.5.0
+colorama==0.4.6
+comfy-aimdo==0.5.3
+comfy-angle==0.1.1
+comfy-kitchen==0.2.33
+comfyui-embedded-docs==0.5.11
+comfyui-frontend-package==1.51.10
+comfyui-workflow-templates==0.11.55
+comfyui-workflow-templates-core==0.3.333
+comfyui-workflow-templates-json==0.1.68
+comfyui-workflow-templates-media-api==0.3.84
+comfyui-workflow-templates-media-assets-01==0.1.39
+comfyui-workflow-templates-media-image==0.3.160
+comfyui-workflow-templates-media-other==0.3.229
+comfyui-workflow-templates-media-video==0.3.101
+cryptography==50.0.1
+diffusers==0.40.0.dev0
+einops==0.8.2
+filelock==3.32.3
+frozenlist==1.8.0
+fsspec==2026.7.0
+greenlet==3.5.5
+h11==0.16.0
+hf-xet==1.6.1a0
+httpcore==1.0.9
+httpx==0.28.1
+huggingface-hub==1.30.0
+idna==3.19
+importlib-metadata==9.0.1
+jinja2==3.1.6
+kornia==0.8.3
+kornia-rs==0.1.14
+mako==1.4.1
+markdown-it-py==4.2.0
+markupsafe==3.0.3
+mdurl==0.1.2
+modelscope-hub==0.4.2
+mpmath==1.3.0
+multidict==6.8.0
+networkx==3.6.1
+ninja==1.13.2
+numpy==2.5.2
+omegaconf==2.3.1
+packaging==26.3
+peft==0.20.0
+pillow==12.3.0
+pip==26.2.1
+propcache==0.5.2
+psutil==7.2.2
+pycparser==3.0
+pydantic==2.13.5
+pydantic-core==2.46.5
+pydantic-settings==2.15.0
+pygments==2.21.0
+pyopengl==3.1.10
+pysocks==1.7.1
+python-dotenv==1.2.3
+pyyaml==6.0.3
+regex==2026.9.3
+requests==2.34.2
+rich==15.0.0
+safetensors==0.8.0
+sageattention==2.2.0+cu130torch2.10.0andhigher.post6
+scipy==1.18.1
+sentencepiece==0.2.2
+setuptools==78.1.0
+shellingham==1.5.4
+simpleeval==1.0.8
+socksio==1.0.0
+spandrel==0.4.2
+sqlalchemy==2.0.52
+sympy==1.14.0
+tokenizers==0.23.0rc0
+torch==2.13.0+cu130
+torchaudio==2.11.0+cu130
+torchsde==0.2.6
+torchvision==0.28.0+cu130
+tqdm==4.70.0
+trampoline==0.1.2
+transformers==5.15.0
+triton-windows==3.7.1.post27
+typer==0.27.2
+typing-extensions==4.16.0
+typing-inspection==0.4.4
+urllib3==2.7.0
+wheel==0.48.0
+yarl==1.24.5
+zipp==4.1.0
diff --git a/constraints/windows-unified.txt b/constraints/windows-unified.txt
new file mode 100644
index 0000000..f369953
--- /dev/null
+++ b/constraints/windows-unified.txt
@@ -0,0 +1,67 @@
+# Native Windows unified environment; Torch wheels come from the cu130 index.
+# Consumer-GPU results are supplied by users; Windows Sage2 wheel is pinned in bootstrap_versions.json.
+accelerate==1.14.0
+annotated-doc==0.0.5
+antlr4-python3-runtime==4.9.3
+anyio==4.15.1
+av==18.1.0
+certifi==2026.7.22
+cffi==2.1.1
+cryptography==50.0.1
+charset-normalizer==3.5.1
+click==8.5.0
+comfy-aimdo==0.5.3
+comfy-kitchen==0.2.33
+einops==0.8.2
+filelock==3.32.3
+fsspec==2026.7.0
+h11==0.16.0
+hf-xet==1.6.1a0
+httpcore==1.0.9
+httpx==0.28.1
+huggingface-hub==1.30.0
+modelscope-hub==0.4.2
+pycparser==3.0
+pysocks==1.7.1
+socksio==1.0.0
+idna==3.19
+importlib-metadata==9.0.1
+jinja2==3.1.6
+markdown-it-py==4.2.0
+markupsafe==3.0.3
+mdurl==0.1.2
+mpmath==1.3.0
+networkx==3.6.1
+ninja==1.13.2
+numpy==2.5.2
+omegaconf==2.3.1
+packaging==26.3
+peft==0.20.0
+pillow==12.3.0
+pip==26.2.1
+psutil==7.2.2
+pygments==2.21.0
+pyyaml==6.0.3
+regex==2026.9.3
+requests==2.34.2
+rich==15.0.0
+safetensors==0.8.0
+scipy==1.18.1
+sentencepiece==0.2.2
+setuptools==78.1.0
+shellingham==1.5.4
+sympy==1.14.0
+tokenizers==0.23.0rc0
+torch==2.13.0+cu130
+torchaudio==2.11.0+cu130
+torchsde==0.2.6
+torchvision==0.28.0+cu130
+tqdm==4.70.0
+trampoline==0.1.2
+transformers==5.15.0
+typer==0.27.2
+typing-extensions==4.16.0
+urllib3==2.7.0
+wheel==0.48.0
+zipp==4.1.0
+triton-windows==3.7.1.post27
diff --git a/docs/assets/capacity-blocks.en.svg b/docs/assets/capacity-blocks.en.svg
new file mode 100644
index 0000000..3f0d2b5
--- /dev/null
+++ b/docs/assets/capacity-blocks.en.svg
@@ -0,0 +1,1494 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="988" height="620" viewBox="0 0 988 620">
+<style>text{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI','Noto Sans','PingFang SC','Microsoft YaHei','Noto Sans CJK SC',Helvetica,Arial,sans-serif}.v{font-size:10.5px;text-anchor:middle}.t{font-size:11px;fill:#57606a}.a{font-size:12.5px;fill:#24292f;text-anchor:middle}</style>
+<rect width="988" height="620" fill="#ffffff"/>
+<text class="t" x="68" y="27.0" text-anchor="end">32</text>
+<rect x="76" y="12" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="26.6" fill="#24292f">0</text>
+<rect x="112" y="12" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="26.6" fill="#24292f">1</text>
+<rect x="148" y="12" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="26.6" fill="#24292f">3</text>
+<rect x="184" y="12" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="26.6" fill="#24292f">1</text>
+<rect x="220" y="12" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="26.6" fill="#24292f">3</text>
+<rect x="256" y="12" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="26.6" fill="#24292f">6</text>
+<rect x="292" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="26.6" fill="#24292f">8</text>
+<rect x="328" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="26.6" fill="#24292f">8</text>
+<rect x="364" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="26.6" fill="#24292f">8</text>
+<rect x="400" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="26.6" fill="#24292f">8</text>
+<rect x="436" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="26.6" fill="#24292f">8</text>
+<rect x="472" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="26.6" fill="#24292f">8</text>
+<rect x="508" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="26.6" fill="#24292f">8</text>
+<rect x="544" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="26.6" fill="#24292f">8</text>
+<rect x="580" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="26.6" fill="#24292f">8</text>
+<rect x="616" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="26.6" fill="#24292f">8</text>
+<rect x="652" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="26.6" fill="#24292f">8</text>
+<rect x="688" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="26.6" fill="#24292f">8</text>
+<rect x="724" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="26.6" fill="#24292f">8</text>
+<rect x="760" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="26.6" fill="#24292f">8</text>
+<rect x="796" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="26.6" fill="#24292f">8</text>
+<rect x="832" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="26.6" fill="#24292f">8</text>
+<rect x="868" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="26.6" fill="#24292f">8</text>
+<rect x="904" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="26.6" fill="#24292f">8</text>
+<rect x="940" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="26.6" fill="#24292f">8</text>
+<text class="t" x="68" y="49.0" text-anchor="end">31</text>
+<rect x="76" y="34" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="48.6" fill="#24292f">0</text>
+<rect x="112" y="34" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="48.6" fill="#24292f">1</text>
+<rect x="148" y="34" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="48.6" fill="#24292f">3</text>
+<rect x="184" y="34" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="48.6" fill="#24292f">1</text>
+<rect x="220" y="34" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="48.6" fill="#24292f">3</text>
+<rect x="256" y="34" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="48.6" fill="#24292f">6</text>
+<rect x="292" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="48.6" fill="#24292f">8</text>
+<rect x="328" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="48.6" fill="#24292f">8</text>
+<rect x="364" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="48.6" fill="#24292f">8</text>
+<rect x="400" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="48.6" fill="#24292f">8</text>
+<rect x="436" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="48.6" fill="#24292f">8</text>
+<rect x="472" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="48.6" fill="#24292f">8</text>
+<rect x="508" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="48.6" fill="#24292f">8</text>
+<rect x="544" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="48.6" fill="#24292f">8</text>
+<rect x="580" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="48.6" fill="#24292f">8</text>
+<rect x="616" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="48.6" fill="#24292f">8</text>
+<rect x="652" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="48.6" fill="#24292f">8</text>
+<rect x="688" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="48.6" fill="#24292f">8</text>
+<rect x="724" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="48.6" fill="#24292f">8</text>
+<rect x="760" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="48.6" fill="#24292f">8</text>
+<rect x="796" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="48.6" fill="#24292f">8</text>
+<rect x="832" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="48.6" fill="#24292f">8</text>
+<rect x="868" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="48.6" fill="#24292f">8</text>
+<rect x="904" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="48.6" fill="#24292f">8</text>
+<rect x="940" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="48.6" fill="#24292f">8</text>
+<text class="t" x="68" y="71.0" text-anchor="end">30</text>
+<rect x="76" y="56" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="70.6" fill="#24292f">0</text>
+<rect x="112" y="56" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="70.6" fill="#24292f">1</text>
+<rect x="148" y="56" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="70.6" fill="#24292f">3</text>
+<rect x="184" y="56" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="70.6" fill="#24292f">1</text>
+<rect x="220" y="56" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="70.6" fill="#24292f">3</text>
+<rect x="256" y="56" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="70.6" fill="#24292f">6</text>
+<rect x="292" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="70.6" fill="#24292f">8</text>
+<rect x="328" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="70.6" fill="#24292f">8</text>
+<rect x="364" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="70.6" fill="#24292f">8</text>
+<rect x="400" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="70.6" fill="#24292f">8</text>
+<rect x="436" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="70.6" fill="#24292f">8</text>
+<rect x="472" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="70.6" fill="#24292f">8</text>
+<rect x="508" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="70.6" fill="#24292f">8</text>
+<rect x="544" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="70.6" fill="#24292f">8</text>
+<rect x="580" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="70.6" fill="#24292f">8</text>
+<rect x="616" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="70.6" fill="#24292f">8</text>
+<rect x="652" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="70.6" fill="#24292f">8</text>
+<rect x="688" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="70.6" fill="#24292f">8</text>
+<rect x="724" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="70.6" fill="#24292f">8</text>
+<rect x="760" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="70.6" fill="#24292f">8</text>
+<rect x="796" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="70.6" fill="#24292f">8</text>
+<rect x="832" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="70.6" fill="#24292f">8</text>
+<rect x="868" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="70.6" fill="#24292f">8</text>
+<rect x="904" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="70.6" fill="#24292f">8</text>
+<rect x="940" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="70.6" fill="#24292f">8</text>
+<text class="t" x="68" y="93.0" text-anchor="end">29</text>
+<rect x="76" y="78" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="92.6" fill="#24292f">0</text>
+<rect x="112" y="78" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="92.6" fill="#24292f">1</text>
+<rect x="148" y="78" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="92.6" fill="#24292f">3</text>
+<rect x="184" y="78" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="92.6" fill="#24292f">1</text>
+<rect x="220" y="78" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="92.6" fill="#24292f">3</text>
+<rect x="256" y="78" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="92.6" fill="#24292f">6</text>
+<rect x="292" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="92.6" fill="#24292f">8</text>
+<rect x="328" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="92.6" fill="#24292f">8</text>
+<rect x="364" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="92.6" fill="#24292f">8</text>
+<rect x="400" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="92.6" fill="#24292f">8</text>
+<rect x="436" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="92.6" fill="#24292f">8</text>
+<rect x="472" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="92.6" fill="#24292f">8</text>
+<rect x="508" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="92.6" fill="#24292f">8</text>
+<rect x="544" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="92.6" fill="#24292f">8</text>
+<rect x="580" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="92.6" fill="#24292f">8</text>
+<rect x="616" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="92.6" fill="#24292f">8</text>
+<rect x="652" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="92.6" fill="#24292f">8</text>
+<rect x="688" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="92.6" fill="#24292f">8</text>
+<rect x="724" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="92.6" fill="#24292f">8</text>
+<rect x="760" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="92.6" fill="#24292f">8</text>
+<rect x="796" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="92.6" fill="#24292f">8</text>
+<rect x="832" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="92.6" fill="#24292f">8</text>
+<rect x="868" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="92.6" fill="#24292f">8</text>
+<rect x="904" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="92.6" fill="#24292f">8</text>
+<rect x="940" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="92.6" fill="#24292f">8</text>
+<text class="t" x="68" y="115.0" text-anchor="end">28</text>
+<rect x="76" y="100" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="114.6" fill="#24292f">0</text>
+<rect x="112" y="100" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="114.6" fill="#24292f">1</text>
+<rect x="148" y="100" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="114.6" fill="#24292f">3</text>
+<rect x="184" y="100" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="114.6" fill="#24292f">1</text>
+<rect x="220" y="100" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="114.6" fill="#24292f">3</text>
+<rect x="256" y="100" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="114.6" fill="#24292f">6</text>
+<rect x="292" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="114.6" fill="#24292f">8</text>
+<rect x="328" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="114.6" fill="#24292f">8</text>
+<rect x="364" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="114.6" fill="#24292f">8</text>
+<rect x="400" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="114.6" fill="#24292f">8</text>
+<rect x="436" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="114.6" fill="#24292f">8</text>
+<rect x="472" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="114.6" fill="#24292f">8</text>
+<rect x="508" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="114.6" fill="#24292f">8</text>
+<rect x="544" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="114.6" fill="#24292f">8</text>
+<rect x="580" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="114.6" fill="#24292f">8</text>
+<rect x="616" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="114.6" fill="#24292f">8</text>
+<rect x="652" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="114.6" fill="#24292f">8</text>
+<rect x="688" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="114.6" fill="#24292f">8</text>
+<rect x="724" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="114.6" fill="#24292f">8</text>
+<rect x="760" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="114.6" fill="#24292f">8</text>
+<rect x="796" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="114.6" fill="#24292f">8</text>
+<rect x="832" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="114.6" fill="#24292f">8</text>
+<rect x="868" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="114.6" fill="#24292f">8</text>
+<rect x="904" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="114.6" fill="#24292f">8</text>
+<rect x="940" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="114.6" fill="#24292f">8</text>
+<text class="t" x="68" y="137.0" text-anchor="end">27</text>
+<rect x="76" y="122" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="136.6" fill="#24292f">0</text>
+<rect x="112" y="122" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="136.6" fill="#24292f">1</text>
+<rect x="148" y="122" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="136.6" fill="#24292f">3</text>
+<rect x="184" y="122" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="136.6" fill="#24292f">1</text>
+<rect x="220" y="122" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="136.6" fill="#24292f">3</text>
+<rect x="256" y="122" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="136.6" fill="#24292f">6</text>
+<rect x="292" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="136.6" fill="#24292f">8</text>
+<rect x="328" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="136.6" fill="#24292f">8</text>
+<rect x="364" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="136.6" fill="#24292f">8</text>
+<rect x="400" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="136.6" fill="#24292f">8</text>
+<rect x="436" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="136.6" fill="#24292f">8</text>
+<rect x="472" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="136.6" fill="#24292f">8</text>
+<rect x="508" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="136.6" fill="#24292f">8</text>
+<rect x="544" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="136.6" fill="#24292f">8</text>
+<rect x="580" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="136.6" fill="#24292f">8</text>
+<rect x="616" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="136.6" fill="#24292f">8</text>
+<rect x="652" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="136.6" fill="#24292f">8</text>
+<rect x="688" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="136.6" fill="#24292f">8</text>
+<rect x="724" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="136.6" fill="#24292f">8</text>
+<rect x="760" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="136.6" fill="#24292f">8</text>
+<rect x="796" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="136.6" fill="#24292f">8</text>
+<rect x="832" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="136.6" fill="#24292f">8</text>
+<rect x="868" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="136.6" fill="#24292f">8</text>
+<rect x="904" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="136.6" fill="#24292f">8</text>
+<rect x="940" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="136.6" fill="#24292f">8</text>
+<text class="t" x="68" y="159.0" text-anchor="end">26</text>
+<rect x="76" y="144" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="158.6" fill="#24292f">0</text>
+<rect x="112" y="144" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="158.6" fill="#24292f">1</text>
+<rect x="148" y="144" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="158.6" fill="#24292f">3</text>
+<rect x="184" y="144" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="158.6" fill="#24292f">1</text>
+<rect x="220" y="144" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="158.6" fill="#24292f">3</text>
+<rect x="256" y="144" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="158.6" fill="#24292f">6</text>
+<rect x="292" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="158.6" fill="#24292f">8</text>
+<rect x="328" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="158.6" fill="#24292f">8</text>
+<rect x="364" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="158.6" fill="#24292f">8</text>
+<rect x="400" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="158.6" fill="#24292f">8</text>
+<rect x="436" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="158.6" fill="#24292f">8</text>
+<rect x="472" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="158.6" fill="#24292f">8</text>
+<rect x="508" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="158.6" fill="#24292f">8</text>
+<rect x="544" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="158.6" fill="#24292f">8</text>
+<rect x="580" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="158.6" fill="#24292f">8</text>
+<rect x="616" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="158.6" fill="#24292f">8</text>
+<rect x="652" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="158.6" fill="#24292f">8</text>
+<rect x="688" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="158.6" fill="#24292f">8</text>
+<rect x="724" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="158.6" fill="#24292f">8</text>
+<rect x="760" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="158.6" fill="#24292f">8</text>
+<rect x="796" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="158.6" fill="#24292f">8</text>
+<rect x="832" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="158.6" fill="#24292f">8</text>
+<rect x="868" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="158.6" fill="#24292f">8</text>
+<rect x="904" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="158.6" fill="#24292f">8</text>
+<rect x="940" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="158.6" fill="#24292f">8</text>
+<text class="t" x="68" y="181.0" text-anchor="end">25</text>
+<rect x="76" y="166" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="180.6" fill="#24292f">0</text>
+<circle cx="105" cy="171" r="2" fill="#24292f"/>
+<rect x="112" y="166" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="180.6" fill="#24292f">1</text>
+<rect x="148" y="166" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="180.6" fill="#24292f">3</text>
+<rect x="184" y="166" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="180.6" fill="#24292f">1</text>
+<rect x="220" y="166" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="180.6" fill="#24292f">3</text>
+<rect x="256" y="166" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="180.6" fill="#24292f">6</text>
+<rect x="292" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="180.6" fill="#24292f">8</text>
+<rect x="328" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="180.6" fill="#24292f">8</text>
+<rect x="364" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="180.6" fill="#24292f">8</text>
+<rect x="400" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="180.6" fill="#24292f">8</text>
+<rect x="436" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="180.6" fill="#24292f">8</text>
+<rect x="472" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="180.6" fill="#24292f">8</text>
+<rect x="508" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="180.6" fill="#24292f">8</text>
+<rect x="544" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="180.6" fill="#24292f">8</text>
+<rect x="580" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="180.6" fill="#24292f">8</text>
+<rect x="616" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="180.6" fill="#24292f">8</text>
+<rect x="652" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="180.6" fill="#24292f">8</text>
+<rect x="688" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="180.6" fill="#24292f">8</text>
+<rect x="724" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="180.6" fill="#24292f">8</text>
+<rect x="760" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="180.6" fill="#24292f">8</text>
+<rect x="796" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="180.6" fill="#24292f">8</text>
+<rect x="832" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="180.6" fill="#24292f">8</text>
+<rect x="868" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="180.6" fill="#24292f">8</text>
+<rect x="904" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="180.6" fill="#24292f">8</text>
+<rect x="940" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="180.6" fill="#24292f">8</text>
+<text class="t" x="68" y="203.0" text-anchor="end">24</text>
+<rect x="76" y="188" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="202.6" fill="#24292f">0</text>
+<circle cx="105" cy="193" r="2" fill="#24292f"/>
+<rect x="112" y="188" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="202.6" fill="#24292f">1</text>
+<circle cx="141" cy="193" r="2" fill="#24292f"/>
+<rect x="148" y="188" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="202.6" fill="#24292f">3</text>
+<circle cx="177" cy="193" r="2" fill="#24292f"/>
+<rect x="184" y="188" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="202.6" fill="#24292f">1</text>
+<rect x="220" y="188" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="202.6" fill="#24292f">3</text>
+<rect x="256" y="188" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="202.6" fill="#24292f">6</text>
+<rect x="292" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="202.6" fill="#24292f">8</text>
+<rect x="328" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="202.6" fill="#24292f">8</text>
+<rect x="364" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="202.6" fill="#24292f">8</text>
+<rect x="400" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="202.6" fill="#24292f">8</text>
+<rect x="436" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="202.6" fill="#24292f">8</text>
+<rect x="472" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="202.6" fill="#24292f">8</text>
+<rect x="508" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="202.6" fill="#24292f">8</text>
+<rect x="544" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="202.6" fill="#24292f">8</text>
+<rect x="580" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="202.6" fill="#24292f">8</text>
+<rect x="616" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="202.6" fill="#24292f">8</text>
+<rect x="652" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="202.6" fill="#24292f">8</text>
+<rect x="688" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="202.6" fill="#24292f">8</text>
+<rect x="724" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="202.6" fill="#24292f">8</text>
+<rect x="760" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="202.6" fill="#24292f">8</text>
+<rect x="796" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="202.6" fill="#24292f">8</text>
+<rect x="832" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="202.6" fill="#24292f">8</text>
+<rect x="868" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="202.6" fill="#24292f">8</text>
+<rect x="904" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="202.6" fill="#24292f">8</text>
+<rect x="940" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="202.6" fill="#24292f">8</text>
+<text class="t" x="68" y="225.0" text-anchor="end">23</text>
+<rect x="76" y="210" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="224.6" fill="#24292f">0</text>
+<circle cx="105" cy="215" r="2" fill="#24292f"/>
+<rect x="112" y="210" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="224.6" fill="#24292f">1</text>
+<circle cx="141" cy="215" r="2" fill="#24292f"/>
+<rect x="148" y="210" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="224.6" fill="#24292f">3</text>
+<circle cx="177" cy="215" r="2" fill="#24292f"/>
+<rect x="184" y="210" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="224.6" fill="#24292f">1</text>
+<circle cx="213" cy="215" r="2" fill="#24292f"/>
+<rect x="220" y="210" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="224.6" fill="#24292f">3</text>
+<rect x="256" y="210" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="224.6" fill="#24292f">6</text>
+<rect x="292" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="224.6" fill="#24292f">8</text>
+<rect x="328" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="224.6" fill="#24292f">8</text>
+<rect x="364" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="224.6" fill="#24292f">8</text>
+<rect x="400" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="224.6" fill="#24292f">8</text>
+<rect x="436" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="224.6" fill="#24292f">8</text>
+<rect x="472" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="224.6" fill="#24292f">8</text>
+<rect x="508" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="224.6" fill="#24292f">8</text>
+<rect x="544" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="224.6" fill="#24292f">8</text>
+<rect x="580" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="224.6" fill="#24292f">8</text>
+<rect x="616" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="224.6" fill="#24292f">8</text>
+<rect x="652" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="224.6" fill="#24292f">8</text>
+<rect x="688" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="224.6" fill="#24292f">8</text>
+<rect x="724" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="224.6" fill="#24292f">8</text>
+<rect x="760" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="224.6" fill="#24292f">8</text>
+<rect x="796" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="224.6" fill="#24292f">8</text>
+<rect x="832" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="224.6" fill="#24292f">8</text>
+<rect x="868" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="224.6" fill="#24292f">8</text>
+<rect x="904" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="224.6" fill="#24292f">8</text>
+<rect x="940" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="224.6" fill="#24292f">8</text>
+<text class="t" x="68" y="247.0" text-anchor="end">22</text>
+<rect x="76" y="232" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="246.6" fill="#24292f">0</text>
+<circle cx="105" cy="237" r="2" fill="#24292f"/>
+<rect x="112" y="232" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="246.6" fill="#24292f">1</text>
+<circle cx="141" cy="237" r="2" fill="#24292f"/>
+<rect x="148" y="232" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="246.6" fill="#24292f">3</text>
+<circle cx="177" cy="237" r="2" fill="#24292f"/>
+<rect x="184" y="232" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="246.6" fill="#24292f">1</text>
+<circle cx="213" cy="237" r="2" fill="#24292f"/>
+<rect x="220" y="232" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="246.6" fill="#24292f">3</text>
+<circle cx="249" cy="237" r="2" fill="#24292f"/>
+<rect x="256" y="232" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="246.6" fill="#24292f">6</text>
+<rect x="292" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="246.6" fill="#24292f">8</text>
+<rect x="328" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="246.6" fill="#24292f">8</text>
+<rect x="364" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="246.6" fill="#24292f">8</text>
+<rect x="400" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="246.6" fill="#24292f">8</text>
+<rect x="436" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="246.6" fill="#24292f">8</text>
+<rect x="472" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="246.6" fill="#24292f">8</text>
+<rect x="508" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="246.6" fill="#24292f">8</text>
+<rect x="544" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="246.6" fill="#24292f">8</text>
+<rect x="580" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="246.6" fill="#24292f">8</text>
+<rect x="616" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="246.6" fill="#24292f">8</text>
+<rect x="652" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="246.6" fill="#24292f">8</text>
+<rect x="688" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="246.6" fill="#24292f">8</text>
+<rect x="724" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="246.6" fill="#24292f">8</text>
+<rect x="760" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="246.6" fill="#24292f">8</text>
+<rect x="796" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="246.6" fill="#24292f">8</text>
+<rect x="832" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="246.6" fill="#24292f">8</text>
+<rect x="868" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="246.6" fill="#24292f">8</text>
+<rect x="904" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="246.6" fill="#24292f">8</text>
+<rect x="940" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="246.6" fill="#24292f">8</text>
+<text class="t" x="68" y="269.0" text-anchor="end">21</text>
+<rect x="76" y="254" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="268.6" fill="#24292f">0</text>
+<circle cx="105" cy="259" r="2" fill="#24292f"/>
+<rect x="112" y="254" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="268.6" fill="#24292f">1</text>
+<circle cx="141" cy="259" r="2" fill="#24292f"/>
+<rect x="148" y="254" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="268.6" fill="#24292f">3</text>
+<circle cx="177" cy="259" r="2" fill="#24292f"/>
+<rect x="184" y="254" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="268.6" fill="#24292f">1</text>
+<circle cx="213" cy="259" r="2" fill="#24292f"/>
+<rect x="220" y="254" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="268.6" fill="#24292f">3</text>
+<circle cx="249" cy="259" r="2" fill="#24292f"/>
+<rect x="256" y="254" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="268.6" fill="#24292f">6</text>
+<circle cx="285" cy="259" r="2" fill="#24292f"/>
+<rect x="292" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="268.6" fill="#24292f">8</text>
+<rect x="328" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="268.6" fill="#24292f">8</text>
+<rect x="364" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="268.6" fill="#24292f">8</text>
+<rect x="400" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="268.6" fill="#24292f">8</text>
+<rect x="436" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="268.6" fill="#24292f">8</text>
+<rect x="472" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="268.6" fill="#24292f">8</text>
+<rect x="508" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="268.6" fill="#24292f">8</text>
+<rect x="544" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="268.6" fill="#24292f">8</text>
+<rect x="580" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="268.6" fill="#24292f">8</text>
+<rect x="616" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="268.6" fill="#24292f">8</text>
+<rect x="652" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="268.6" fill="#24292f">8</text>
+<rect x="688" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="268.6" fill="#24292f">8</text>
+<rect x="724" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="268.6" fill="#24292f">8</text>
+<rect x="760" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="268.6" fill="#24292f">8</text>
+<rect x="796" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="268.6" fill="#24292f">8</text>
+<rect x="832" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="268.6" fill="#24292f">8</text>
+<rect x="868" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="268.6" fill="#24292f">8</text>
+<rect x="904" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="268.6" fill="#24292f">8</text>
+<rect x="940" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="268.6" fill="#24292f">8</text>
+<text class="t" x="68" y="291.0" text-anchor="end">20</text>
+<rect x="76" y="276" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="290.6" fill="#24292f">0</text>
+<circle cx="105" cy="281" r="2" fill="#24292f"/>
+<rect x="112" y="276" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="290.6" fill="#24292f">1</text>
+<circle cx="141" cy="281" r="2" fill="#24292f"/>
+<rect x="148" y="276" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="290.6" fill="#24292f">3</text>
+<circle cx="177" cy="281" r="2" fill="#24292f"/>
+<rect x="184" y="276" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="290.6" fill="#24292f">1</text>
+<circle cx="213" cy="281" r="2" fill="#24292f"/>
+<rect x="220" y="276" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="290.6" fill="#24292f">3</text>
+<circle cx="249" cy="281" r="2" fill="#24292f"/>
+<rect x="256" y="276" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="290.6" fill="#24292f">6</text>
+<circle cx="285" cy="281" r="2" fill="#24292f"/>
+<rect x="292" y="276" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="290.6" fill="#24292f">8</text>
+<circle cx="321" cy="281" r="2" fill="#24292f"/>
+<rect x="328" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="345.5" y="290.6" fill="#24292f">10</text>
+<rect x="364" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="381.5" y="290.6" fill="#24292f">10</text>
+<rect x="400" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="417.5" y="290.6" fill="#24292f">10</text>
+<rect x="436" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="453.5" y="290.6" fill="#24292f">10</text>
+<rect x="472" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="489.5" y="290.6" fill="#24292f">10</text>
+<rect x="508" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="525.5" y="290.6" fill="#24292f">10</text>
+<rect x="544" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="561.5" y="290.6" fill="#24292f">10</text>
+<rect x="580" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="597.5" y="290.6" fill="#24292f">10</text>
+<rect x="616" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="633.5" y="290.6" fill="#24292f">10</text>
+<rect x="652" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="669.5" y="290.6" fill="#24292f">10</text>
+<rect x="688" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="705.5" y="290.6" fill="#24292f">10</text>
+<rect x="724" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="741.5" y="290.6" fill="#24292f">10</text>
+<rect x="760" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="777.5" y="290.6" fill="#24292f">10</text>
+<rect x="796" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="813.5" y="290.6" fill="#24292f">10</text>
+<rect x="832" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="849.5" y="290.6" fill="#24292f">10</text>
+<rect x="868" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="885.5" y="290.6" fill="#24292f">10</text>
+<rect x="904" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="921.5" y="290.6" fill="#24292f">10</text>
+<rect x="940" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="957.5" y="290.6" fill="#24292f">10</text>
+<text class="t" x="68" y="313.0" text-anchor="end">19</text>
+<rect x="76" y="298" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="312.6" fill="#24292f">0</text>
+<circle cx="105" cy="303" r="2" fill="#24292f"/>
+<rect x="112" y="298" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="312.6" fill="#24292f">1</text>
+<circle cx="141" cy="303" r="2" fill="#24292f"/>
+<rect x="148" y="298" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="312.6" fill="#24292f">3</text>
+<circle cx="177" cy="303" r="2" fill="#24292f"/>
+<rect x="184" y="298" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="312.6" fill="#24292f">1</text>
+<circle cx="213" cy="303" r="2" fill="#24292f"/>
+<rect x="220" y="298" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="312.6" fill="#24292f">3</text>
+<circle cx="249" cy="303" r="2" fill="#24292f"/>
+<rect x="256" y="298" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="312.6" fill="#24292f">6</text>
+<circle cx="285" cy="303" r="2" fill="#24292f"/>
+<rect x="292" y="298" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="312.6" fill="#24292f">8</text>
+<circle cx="321" cy="303" r="2" fill="#24292f"/>
+<rect x="328" y="298" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="312.6" fill="#24292f">11</text>
+<circle cx="357" cy="303" r="2" fill="#24292f"/>
+<rect x="364" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="381.5" y="312.6" fill="#24292f">12</text>
+<rect x="400" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="417.5" y="312.6" fill="#24292f">12</text>
+<rect x="436" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="453.5" y="312.6" fill="#24292f">12</text>
+<rect x="472" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="489.5" y="312.6" fill="#24292f">12</text>
+<rect x="508" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="525.5" y="312.6" fill="#24292f">12</text>
+<rect x="544" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="561.5" y="312.6" fill="#24292f">12</text>
+<rect x="580" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="597.5" y="312.6" fill="#24292f">12</text>
+<rect x="616" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="633.5" y="312.6" fill="#24292f">12</text>
+<rect x="652" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="669.5" y="312.6" fill="#24292f">12</text>
+<rect x="688" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="705.5" y="312.6" fill="#24292f">12</text>
+<rect x="724" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="741.5" y="312.6" fill="#24292f">12</text>
+<rect x="760" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="777.5" y="312.6" fill="#24292f">12</text>
+<rect x="796" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="813.5" y="312.6" fill="#24292f">12</text>
+<rect x="832" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="849.5" y="312.6" fill="#24292f">12</text>
+<rect x="868" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="885.5" y="312.6" fill="#24292f">12</text>
+<rect x="904" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="921.5" y="312.6" fill="#24292f">12</text>
+<rect x="940" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="957.5" y="312.6" fill="#24292f">12</text>
+<text class="t" x="68" y="335.0" text-anchor="end">18</text>
+<rect x="76" y="320" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="334.6" fill="#24292f">0</text>
+<circle cx="105" cy="325" r="2" fill="#24292f"/>
+<rect x="112" y="320" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="334.6" fill="#24292f">1</text>
+<circle cx="141" cy="325" r="2" fill="#24292f"/>
+<rect x="148" y="320" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="334.6" fill="#24292f">3</text>
+<circle cx="177" cy="325" r="2" fill="#24292f"/>
+<rect x="184" y="320" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="334.6" fill="#24292f">1</text>
+<circle cx="213" cy="325" r="2" fill="#24292f"/>
+<rect x="220" y="320" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="334.6" fill="#24292f">3</text>
+<circle cx="249" cy="325" r="2" fill="#24292f"/>
+<rect x="256" y="320" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="334.6" fill="#24292f">6</text>
+<circle cx="285" cy="325" r="2" fill="#24292f"/>
+<rect x="292" y="320" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="334.6" fill="#24292f">8</text>
+<circle cx="321" cy="325" r="2" fill="#24292f"/>
+<rect x="328" y="320" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="334.6" fill="#24292f">11</text>
+<circle cx="357" cy="325" r="2" fill="#24292f"/>
+<rect x="364" y="320" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="334.6" fill="#24292f">13</text>
+<circle cx="393" cy="325" r="2" fill="#24292f"/>
+<rect x="400" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="417.5" y="334.6" fill="#24292f">15</text>
+<rect x="436" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="453.5" y="334.6" fill="#24292f">15</text>
+<rect x="472" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="489.5" y="334.6" fill="#24292f">15</text>
+<rect x="508" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="525.5" y="334.6" fill="#24292f">15</text>
+<rect x="544" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="561.5" y="334.6" fill="#24292f">15</text>
+<rect x="580" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="597.5" y="334.6" fill="#24292f">15</text>
+<rect x="616" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="633.5" y="334.6" fill="#24292f">15</text>
+<rect x="652" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="669.5" y="334.6" fill="#24292f">15</text>
+<rect x="688" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="705.5" y="334.6" fill="#24292f">15</text>
+<rect x="724" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="741.5" y="334.6" fill="#24292f">15</text>
+<rect x="760" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="777.5" y="334.6" fill="#24292f">15</text>
+<rect x="796" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="813.5" y="334.6" fill="#24292f">15</text>
+<rect x="832" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="849.5" y="334.6" fill="#24292f">15</text>
+<rect x="868" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="885.5" y="334.6" fill="#24292f">15</text>
+<rect x="904" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="921.5" y="334.6" fill="#24292f">15</text>
+<rect x="940" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="957.5" y="334.6" fill="#24292f">15</text>
+<text class="t" x="68" y="357.0" text-anchor="end">17</text>
+<rect x="76" y="342" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="356.6" fill="#24292f">0</text>
+<circle cx="105" cy="347" r="2" fill="#24292f"/>
+<rect x="112" y="342" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="356.6" fill="#24292f">1</text>
+<circle cx="141" cy="347" r="2" fill="#24292f"/>
+<rect x="148" y="342" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="356.6" fill="#24292f">3</text>
+<circle cx="177" cy="347" r="2" fill="#24292f"/>
+<rect x="184" y="342" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="356.6" fill="#24292f">1</text>
+<circle cx="213" cy="347" r="2" fill="#24292f"/>
+<rect x="220" y="342" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="356.6" fill="#24292f">3</text>
+<circle cx="249" cy="347" r="2" fill="#24292f"/>
+<rect x="256" y="342" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="356.6" fill="#24292f">6</text>
+<circle cx="285" cy="347" r="2" fill="#24292f"/>
+<rect x="292" y="342" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="356.6" fill="#24292f">8</text>
+<circle cx="321" cy="347" r="2" fill="#24292f"/>
+<rect x="328" y="342" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="356.6" fill="#24292f">11</text>
+<circle cx="357" cy="347" r="2" fill="#24292f"/>
+<rect x="364" y="342" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="356.6" fill="#24292f">13</text>
+<circle cx="393" cy="347" r="2" fill="#24292f"/>
+<rect x="400" y="342" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="356.6" fill="#24292f">16</text>
+<circle cx="429" cy="347" r="2" fill="#24292f"/>
+<rect x="436" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="453.5" y="356.6" fill="#24292f">17</text>
+<rect x="472" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="489.5" y="356.6" fill="#24292f">17</text>
+<rect x="508" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="525.5" y="356.6" fill="#24292f">17</text>
+<rect x="544" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="561.5" y="356.6" fill="#24292f">17</text>
+<rect x="580" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="597.5" y="356.6" fill="#24292f">17</text>
+<rect x="616" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="633.5" y="356.6" fill="#24292f">17</text>
+<rect x="652" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="669.5" y="356.6" fill="#24292f">17</text>
+<rect x="688" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="705.5" y="356.6" fill="#24292f">17</text>
+<rect x="724" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="741.5" y="356.6" fill="#24292f">17</text>
+<rect x="760" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="777.5" y="356.6" fill="#24292f">17</text>
+<rect x="796" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="813.5" y="356.6" fill="#24292f">17</text>
+<rect x="832" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="849.5" y="356.6" fill="#24292f">17</text>
+<rect x="868" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="885.5" y="356.6" fill="#24292f">17</text>
+<rect x="904" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="921.5" y="356.6" fill="#24292f">17</text>
+<rect x="940" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="957.5" y="356.6" fill="#24292f">17</text>
+<text class="t" x="68" y="379.0" text-anchor="end">16</text>
+<rect x="76" y="364" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="378.6" fill="#24292f">0</text>
+<circle cx="105" cy="369" r="2" fill="#24292f"/>
+<rect x="112" y="364" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="378.6" fill="#24292f">1</text>
+<circle cx="141" cy="369" r="2" fill="#24292f"/>
+<rect x="148" y="364" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="378.6" fill="#24292f">3</text>
+<circle cx="177" cy="369" r="2" fill="#24292f"/>
+<rect x="184" y="364" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="378.6" fill="#24292f">1</text>
+<circle cx="213" cy="369" r="2" fill="#24292f"/>
+<rect x="220" y="364" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="378.6" fill="#24292f">3</text>
+<circle cx="249" cy="369" r="2" fill="#24292f"/>
+<rect x="256" y="364" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="378.6" fill="#24292f">6</text>
+<circle cx="285" cy="369" r="2" fill="#24292f"/>
+<rect x="292" y="364" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="378.6" fill="#24292f">8</text>
+<circle cx="321" cy="369" r="2" fill="#24292f"/>
+<rect x="328" y="364" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="378.6" fill="#24292f">11</text>
+<circle cx="357" cy="369" r="2" fill="#24292f"/>
+<rect x="364" y="364" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="378.6" fill="#24292f">13</text>
+<circle cx="393" cy="369" r="2" fill="#24292f"/>
+<rect x="400" y="364" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="378.6" fill="#24292f">16</text>
+<circle cx="429" cy="369" r="2" fill="#24292f"/>
+<rect x="436" y="364" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="378.6" fill="#24292f">18</text>
+<circle cx="465" cy="369" r="2" fill="#24292f"/>
+<rect x="472" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="489.5" y="378.6" fill="#24292f">19</text>
+<rect x="508" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="525.5" y="378.6" fill="#24292f">19</text>
+<rect x="544" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="561.5" y="378.6" fill="#24292f">19</text>
+<rect x="580" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="597.5" y="378.6" fill="#24292f">19</text>
+<rect x="616" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="633.5" y="378.6" fill="#24292f">19</text>
+<rect x="652" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="669.5" y="378.6" fill="#24292f">19</text>
+<rect x="688" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="705.5" y="378.6" fill="#24292f">19</text>
+<rect x="724" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="741.5" y="378.6" fill="#24292f">19</text>
+<rect x="760" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="777.5" y="378.6" fill="#24292f">19</text>
+<rect x="796" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="813.5" y="378.6" fill="#24292f">19</text>
+<rect x="832" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="849.5" y="378.6" fill="#24292f">19</text>
+<rect x="868" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="885.5" y="378.6" fill="#24292f">19</text>
+<rect x="904" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="921.5" y="378.6" fill="#24292f">19</text>
+<rect x="940" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="957.5" y="378.6" fill="#24292f">19</text>
+<text class="t" x="68" y="401.0" text-anchor="end">15</text>
+<rect x="76" y="386" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="400.6" fill="#24292f">0</text>
+<circle cx="105" cy="391" r="2" fill="#24292f"/>
+<rect x="112" y="386" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="400.6" fill="#24292f">1</text>
+<circle cx="141" cy="391" r="2" fill="#24292f"/>
+<rect x="148" y="386" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="400.6" fill="#24292f">3</text>
+<circle cx="177" cy="391" r="2" fill="#24292f"/>
+<rect x="184" y="386" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="400.6" fill="#24292f">1</text>
+<circle cx="213" cy="391" r="2" fill="#24292f"/>
+<rect x="220" y="386" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="400.6" fill="#24292f">3</text>
+<circle cx="249" cy="391" r="2" fill="#24292f"/>
+<rect x="256" y="386" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="400.6" fill="#24292f">6</text>
+<circle cx="285" cy="391" r="2" fill="#24292f"/>
+<rect x="292" y="386" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="400.6" fill="#24292f">8</text>
+<circle cx="321" cy="391" r="2" fill="#24292f"/>
+<rect x="328" y="386" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="400.6" fill="#24292f">11</text>
+<circle cx="357" cy="391" r="2" fill="#24292f"/>
+<rect x="364" y="386" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="400.6" fill="#24292f">13</text>
+<circle cx="393" cy="391" r="2" fill="#24292f"/>
+<rect x="400" y="386" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="400.6" fill="#24292f">16</text>
+<circle cx="429" cy="391" r="2" fill="#24292f"/>
+<rect x="436" y="386" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="400.6" fill="#24292f">18</text>
+<circle cx="465" cy="391" r="2" fill="#24292f"/>
+<rect x="472" y="386" width="35" height="21" fill="#95c5df"/>
+<text class="v" x="489.5" y="400.6" fill="#24292f">20</text>
+<circle cx="501" cy="391" r="2" fill="#24292f"/>
+<rect x="508" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="525.5" y="400.6" fill="#24292f">22</text>
+<rect x="544" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="561.5" y="400.6" fill="#24292f">22</text>
+<rect x="580" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="597.5" y="400.6" fill="#24292f">22</text>
+<rect x="616" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="633.5" y="400.6" fill="#24292f">22</text>
+<rect x="652" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="669.5" y="400.6" fill="#24292f">22</text>
+<rect x="688" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="705.5" y="400.6" fill="#24292f">22</text>
+<rect x="724" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="741.5" y="400.6" fill="#24292f">22</text>
+<rect x="760" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="777.5" y="400.6" fill="#24292f">22</text>
+<rect x="796" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="813.5" y="400.6" fill="#24292f">22</text>
+<rect x="832" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="849.5" y="400.6" fill="#24292f">22</text>
+<rect x="868" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="885.5" y="400.6" fill="#24292f">22</text>
+<rect x="904" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="921.5" y="400.6" fill="#24292f">22</text>
+<rect x="940" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="957.5" y="400.6" fill="#24292f">22</text>
+<text class="t" x="68" y="423.0" text-anchor="end">14</text>
+<rect x="76" y="408" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="422.6" fill="#24292f">0</text>
+<circle cx="105" cy="413" r="2" fill="#24292f"/>
+<rect x="112" y="408" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="422.6" fill="#24292f">1</text>
+<circle cx="141" cy="413" r="2" fill="#24292f"/>
+<rect x="148" y="408" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="422.6" fill="#24292f">3</text>
+<circle cx="177" cy="413" r="2" fill="#24292f"/>
+<rect x="184" y="408" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="422.6" fill="#24292f">1</text>
+<circle cx="213" cy="413" r="2" fill="#24292f"/>
+<rect x="220" y="408" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="422.6" fill="#24292f">3</text>
+<circle cx="249" cy="413" r="2" fill="#24292f"/>
+<rect x="256" y="408" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="422.6" fill="#24292f">6</text>
+<circle cx="285" cy="413" r="2" fill="#24292f"/>
+<rect x="292" y="408" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="422.6" fill="#24292f">8</text>
+<circle cx="321" cy="413" r="2" fill="#24292f"/>
+<rect x="328" y="408" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="422.6" fill="#24292f">11</text>
+<circle cx="357" cy="413" r="2" fill="#24292f"/>
+<rect x="364" y="408" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="422.6" fill="#24292f">13</text>
+<circle cx="393" cy="413" r="2" fill="#24292f"/>
+<rect x="400" y="408" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="422.6" fill="#24292f">16</text>
+<circle cx="429" cy="413" r="2" fill="#24292f"/>
+<rect x="436" y="408" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="422.6" fill="#24292f">18</text>
+<circle cx="465" cy="413" r="2" fill="#24292f"/>
+<rect x="472" y="408" width="35" height="21" fill="#95c5df"/>
+<text class="v" x="489.5" y="422.6" fill="#24292f">20</text>
+<circle cx="501" cy="413" r="2" fill="#24292f"/>
+<rect x="508" y="408" width="35" height="21" fill="#7cb8da"/>
+<text class="v" x="525.5" y="422.6" fill="#24292f">23</text>
+<circle cx="537" cy="413" r="2" fill="#24292f"/>
+<rect x="544" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="561.5" y="422.6" fill="#24292f">24</text>
+<rect x="580" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="597.5" y="422.6" fill="#24292f">24</text>
+<rect x="616" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="633.5" y="422.6" fill="#24292f">24</text>
+<rect x="652" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="669.5" y="422.6" fill="#24292f">24</text>
+<rect x="688" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="705.5" y="422.6" fill="#24292f">24</text>
+<rect x="724" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="741.5" y="422.6" fill="#24292f">24</text>
+<rect x="760" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="777.5" y="422.6" fill="#24292f">24</text>
+<rect x="796" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="813.5" y="422.6" fill="#24292f">24</text>
+<rect x="832" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="849.5" y="422.6" fill="#24292f">24</text>
+<rect x="868" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="885.5" y="422.6" fill="#24292f">24</text>
+<rect x="904" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="921.5" y="422.6" fill="#24292f">24</text>
+<rect x="940" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="957.5" y="422.6" fill="#24292f">24</text>
+<text class="t" x="68" y="445.0" text-anchor="end">13</text>
+<rect x="76" y="430" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="444.6" fill="#24292f">0</text>
+<circle cx="105" cy="435" r="2" fill="#24292f"/>
+<rect x="112" y="430" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="444.6" fill="#24292f">1</text>
+<circle cx="141" cy="435" r="2" fill="#24292f"/>
+<rect x="148" y="430" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="444.6" fill="#24292f">3</text>
+<circle cx="177" cy="435" r="2" fill="#24292f"/>
+<rect x="184" y="430" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="444.6" fill="#24292f">1</text>
+<circle cx="213" cy="435" r="2" fill="#24292f"/>
+<rect x="220" y="430" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="444.6" fill="#24292f">3</text>
+<circle cx="249" cy="435" r="2" fill="#24292f"/>
+<rect x="256" y="430" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="444.6" fill="#24292f">6</text>
+<circle cx="285" cy="435" r="2" fill="#24292f"/>
+<rect x="292" y="430" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="444.6" fill="#24292f">8</text>
+<circle cx="321" cy="435" r="2" fill="#24292f"/>
+<rect x="328" y="430" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="444.6" fill="#24292f">11</text>
+<circle cx="357" cy="435" r="2" fill="#24292f"/>
+<rect x="364" y="430" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="444.6" fill="#24292f">13</text>
+<circle cx="393" cy="435" r="2" fill="#24292f"/>
+<rect x="400" y="430" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="444.6" fill="#24292f">16</text>
+<circle cx="429" cy="435" r="2" fill="#24292f"/>
+<rect x="436" y="430" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="444.6" fill="#24292f">18</text>
+<circle cx="465" cy="435" r="2" fill="#24292f"/>
+<rect x="472" y="430" width="35" height="21" fill="#95c5df"/>
+<text class="v" x="489.5" y="444.6" fill="#24292f">20</text>
+<circle cx="501" cy="435" r="2" fill="#24292f"/>
+<rect x="508" y="430" width="35" height="21" fill="#7cb8da"/>
+<text class="v" x="525.5" y="444.6" fill="#24292f">23</text>
+<circle cx="537" cy="435" r="2" fill="#24292f"/>
+<rect x="544" y="430" width="35" height="21" fill="#6cafd6"/>
+<text class="v" x="561.5" y="444.6" fill="#24292f">25</text>
+<circle cx="573" cy="435" r="2" fill="#24292f"/>
+<rect x="580" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="597.5" y="444.6" fill="#24292f">27</text>
+<rect x="616" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="633.5" y="444.6" fill="#24292f">27</text>
+<rect x="652" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="669.5" y="444.6" fill="#24292f">27</text>
+<rect x="688" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="705.5" y="444.6" fill="#24292f">27</text>
+<rect x="724" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="741.5" y="444.6" fill="#24292f">27</text>
+<rect x="760" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="777.5" y="444.6" fill="#24292f">27</text>
+<rect x="796" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="813.5" y="444.6" fill="#24292f">27</text>
+<rect x="832" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="849.5" y="444.6" fill="#24292f">27</text>
+<rect x="868" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="885.5" y="444.6" fill="#24292f">27</text>
+<rect x="904" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="921.5" y="444.6" fill="#24292f">27</text>
+<rect x="940" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="957.5" y="444.6" fill="#24292f">27</text>
+<text class="t" x="68" y="467.0" text-anchor="end">12</text>
+<rect x="76" y="452" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="466.6" fill="#24292f">0</text>
+<circle cx="105" cy="457" r="2" fill="#24292f"/>
+<rect x="112" y="452" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="466.6" fill="#24292f">1</text>
+<circle cx="141" cy="457" r="2" fill="#24292f"/>
+<rect x="148" y="452" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="466.6" fill="#24292f">3</text>
+<circle cx="177" cy="457" r="2" fill="#24292f"/>
+<rect x="184" y="452" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="466.6" fill="#24292f">1</text>
+<circle cx="213" cy="457" r="2" fill="#24292f"/>
+<rect x="220" y="452" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="466.6" fill="#24292f">3</text>
+<circle cx="249" cy="457" r="2" fill="#24292f"/>
+<rect x="256" y="452" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="466.6" fill="#24292f">6</text>
+<circle cx="285" cy="457" r="2" fill="#24292f"/>
+<rect x="292" y="452" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="466.6" fill="#24292f">8</text>
+<circle cx="321" cy="457" r="2" fill="#24292f"/>
+<rect x="328" y="452" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="466.6" fill="#24292f">11</text>
+<circle cx="357" cy="457" r="2" fill="#24292f"/>
+<rect x="364" y="452" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="466.6" fill="#24292f">13</text>
+<circle cx="393" cy="457" r="2" fill="#24292f"/>
+<rect x="400" y="452" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="466.6" fill="#24292f">16</text>
+<circle cx="429" cy="457" r="2" fill="#24292f"/>
+<rect x="436" y="452" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="466.6" fill="#24292f">18</text>
+<circle cx="465" cy="457" r="2" fill="#24292f"/>
+<rect x="472" y="452" width="35" height="21" fill="#95c5df"/>
+<text class="v" x="489.5" y="466.6" fill="#24292f">20</text>
+<circle cx="501" cy="457" r="2" fill="#24292f"/>
+<rect x="508" y="452" width="35" height="21" fill="#7cb8da"/>
+<text class="v" x="525.5" y="466.6" fill="#24292f">23</text>
+<circle cx="537" cy="457" r="2" fill="#24292f"/>
+<rect x="544" y="452" width="35" height="21" fill="#6cafd6"/>
+<text class="v" x="561.5" y="466.6" fill="#24292f">25</text>
+<circle cx="573" cy="457" r="2" fill="#24292f"/>
+<rect x="580" y="452" width="35" height="21" fill="#58a1cf"/>
+<text class="v" x="597.5" y="466.6" fill="#24292f">28</text>
+<circle cx="609" cy="457" r="2" fill="#24292f"/>
+<rect x="616" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="633.5" y="466.6" fill="#24292f">29</text>
+<rect x="652" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="669.5" y="466.6" fill="#24292f">29</text>
+<rect x="688" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="705.5" y="466.6" fill="#24292f">29</text>
+<rect x="724" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="741.5" y="466.6" fill="#24292f">29</text>
+<rect x="760" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="777.5" y="466.6" fill="#24292f">29</text>
+<rect x="796" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="813.5" y="466.6" fill="#24292f">29</text>
+<rect x="832" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="849.5" y="466.6" fill="#24292f">29</text>
+<rect x="868" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="885.5" y="466.6" fill="#24292f">29</text>
+<rect x="904" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="921.5" y="466.6" fill="#24292f">29</text>
+<rect x="940" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="957.5" y="466.6" fill="#24292f">29</text>
+<text class="t" x="68" y="489.0" text-anchor="end">11</text>
+<rect x="76" y="474" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="488.6" fill="#24292f">0</text>
+<circle cx="105" cy="479" r="2" fill="#24292f"/>
+<rect x="112" y="474" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="488.6" fill="#24292f">1</text>
+<circle cx="141" cy="479" r="2" fill="#24292f"/>
+<rect x="148" y="474" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="488.6" fill="#24292f">3</text>
+<circle cx="177" cy="479" r="2" fill="#24292f"/>
+<rect x="184" y="474" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="488.6" fill="#24292f">1</text>
+<circle cx="213" cy="479" r="2" fill="#24292f"/>
+<rect x="220" y="474" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="488.6" fill="#24292f">3</text>
+<circle cx="249" cy="479" r="2" fill="#24292f"/>
+<rect x="256" y="474" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="488.6" fill="#24292f">6</text>
+<circle cx="285" cy="479" r="2" fill="#24292f"/>
+<rect x="292" y="474" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="488.6" fill="#24292f">8</text>
+<circle cx="321" cy="479" r="2" fill="#24292f"/>
+<rect x="328" y="474" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="488.6" fill="#24292f">11</text>
+<circle cx="357" cy="479" r="2" fill="#24292f"/>
+<rect x="364" y="474" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="488.6" fill="#24292f">13</text>
+<circle cx="393" cy="479" r="2" fill="#24292f"/>
+<rect x="400" y="474" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="488.6" fill="#24292f">16</text>
+<circle cx="429" cy="479" r="2" fill="#24292f"/>
+<rect x="436" y="474" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="488.6" fill="#24292f">18</text>
+<circle cx="465" cy="479" r="2" fill="#24292f"/>
+<rect x="472" y="474" width="35" height="21" fill="#95c5df"/>
+<text class="v" x="489.5" y="488.6" fill="#24292f">20</text>
+<circle cx="501" cy="479" r="2" fill="#24292f"/>
+<rect x="508" y="474" width="35" height="21" fill="#7cb8da"/>
+<text class="v" x="525.5" y="488.6" fill="#24292f">23</text>
+<circle cx="537" cy="479" r="2" fill="#24292f"/>
+<rect x="544" y="474" width="35" height="21" fill="#6cafd6"/>
+<text class="v" x="561.5" y="488.6" fill="#24292f">25</text>
+<circle cx="573" cy="479" r="2" fill="#24292f"/>
+<rect x="580" y="474" width="35" height="21" fill="#58a1cf"/>
+<text class="v" x="597.5" y="488.6" fill="#24292f">28</text>
+<circle cx="609" cy="479" r="2" fill="#24292f"/>
+<rect x="616" y="474" width="35" height="21" fill="#4b98ca"/>
+<text class="v" x="633.5" y="488.6" fill="#24292f">30</text>
+<circle cx="645" cy="479" r="2" fill="#24292f"/>
+<rect x="652" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="669.5" y="488.6" fill="#24292f">32</text>
+<rect x="688" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="705.5" y="488.6" fill="#24292f">32</text>
+<rect x="724" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="741.5" y="488.6" fill="#24292f">32</text>
+<rect x="760" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="777.5" y="488.6" fill="#24292f">32</text>
+<rect x="796" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="813.5" y="488.6" fill="#24292f">32</text>
+<rect x="832" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="849.5" y="488.6" fill="#24292f">32</text>
+<rect x="868" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="885.5" y="488.6" fill="#24292f">32</text>
+<rect x="904" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="921.5" y="488.6" fill="#24292f">32</text>
+<rect x="940" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="957.5" y="488.6" fill="#24292f">32</text>
+<text class="t" x="68" y="511.0" text-anchor="end">10</text>
+<rect x="76" y="496" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="510.6" fill="#24292f">0</text>
+<circle cx="105" cy="501" r="2" fill="#24292f"/>
+<rect x="112" y="496" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="510.6" fill="#24292f">1</text>
+<circle cx="141" cy="501" r="2" fill="#24292f"/>
+<rect x="148" y="496" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="510.6" fill="#24292f">3</text>
+<circle cx="177" cy="501" r="2" fill="#24292f"/>
+<rect x="184" y="496" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="510.6" fill="#24292f">1</text>
+<circle cx="213" cy="501" r="2" fill="#24292f"/>
+<rect x="220" y="496" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="510.6" fill="#24292f">3</text>
+<circle cx="249" cy="501" r="2" fill="#24292f"/>
+<rect x="256" y="496" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="510.6" fill="#24292f">6</text>
+<circle cx="285" cy="501" r="2" fill="#24292f"/>
+<rect x="292" y="496" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="510.6" fill="#24292f">8</text>
+<circle cx="321" cy="501" r="2" fill="#24292f"/>
+<rect x="328" y="496" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="510.6" fill="#24292f">11</text>
+<circle cx="357" cy="501" r="2" fill="#24292f"/>
+<rect x="364" y="496" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="510.6" fill="#24292f">13</text>
+<circle cx="393" cy="501" r="2" fill="#24292f"/>
+<rect x="400" y="496" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="510.6" fill="#24292f">16</text>
+<circle cx="429" cy="501" r="2" fill="#24292f"/>
+<rect x="436" y="496" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="510.6" fill="#24292f">18</text>
+<circle cx="465" cy="501" r="2" fill="#24292f"/>
+<rect x="472" y="496" width="35" height="21" fill="#95c5df"/>
+<text class="v" x="489.5" y="510.6" fill="#24292f">20</text>
+<circle cx="501" cy="501" r="2" fill="#24292f"/>
+<rect x="508" y="496" width="35" height="21" fill="#7cb8da"/>
+<text class="v" x="525.5" y="510.6" fill="#24292f">23</text>
+<circle cx="537" cy="501" r="2" fill="#24292f"/>
+<rect x="544" y="496" width="35" height="21" fill="#6cafd6"/>
+<text class="v" x="561.5" y="510.6" fill="#24292f">25</text>
+<circle cx="573" cy="501" r="2" fill="#24292f"/>
+<rect x="580" y="496" width="35" height="21" fill="#58a1cf"/>
+<text class="v" x="597.5" y="510.6" fill="#24292f">28</text>
+<circle cx="609" cy="501" r="2" fill="#24292f"/>
+<rect x="616" y="496" width="35" height="21" fill="#4b98ca"/>
+<text class="v" x="633.5" y="510.6" fill="#24292f">30</text>
+<circle cx="645" cy="501" r="2" fill="#24292f"/>
+<rect x="652" y="496" width="35" height="21" fill="#3a8ac2"/>
+<text class="v" x="669.5" y="510.6" fill="#ffffff">33</text>
+<circle cx="681" cy="501" r="2" fill="#ffffff"/>
+<rect x="688" y="496" width="35" height="21" fill="#3484bf"/>
+<text class="v" x="705.5" y="510.6" fill="#ffffff">34</text>
+<rect x="724" y="496" width="35" height="21" fill="#3484bf"/>
+<text class="v" x="741.5" y="510.6" fill="#ffffff">34</text>
+<rect x="760" y="496" width="35" height="21" fill="#3484bf"/>
+<text class="v" x="777.5" y="510.6" fill="#ffffff">34</text>
+<rect x="796" y="496" width="35" height="21" fill="#3484bf"/>
+<text class="v" x="813.5" y="510.6" fill="#ffffff">34</text>
+<rect x="832" y="496" width="35" height="21" fill="#3484bf"/>
+<text class="v" x="849.5" y="510.6" fill="#ffffff">34</text>
+<rect x="868" y="496" width="35" height="21" fill="#3484bf"/>
+<text class="v" x="885.5" y="510.6" fill="#ffffff">34</text>
+<rect x="904" y="496" width="35" height="21" fill="#3484bf"/>
+<text class="v" x="921.5" y="510.6" fill="#ffffff">34</text>
+<rect x="940" y="496" width="35" height="21" fill="#3484bf"/>
+<text class="v" x="957.5" y="510.6" fill="#ffffff">34</text>
+<text class="t" x="68" y="533.0" text-anchor="end">9</text>
+<rect x="76" y="518" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="532.6" fill="#24292f">0</text>
+<circle cx="105" cy="523" r="2" fill="#24292f"/>
+<rect x="112" y="518" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="532.6" fill="#24292f">1</text>
+<circle cx="141" cy="523" r="2" fill="#24292f"/>
+<rect x="148" y="518" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="532.6" fill="#24292f">3</text>
+<circle cx="177" cy="523" r="2" fill="#24292f"/>
+<rect x="184" y="518" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="532.6" fill="#24292f">1</text>
+<circle cx="213" cy="523" r="2" fill="#24292f"/>
+<rect x="220" y="518" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="532.6" fill="#24292f">3</text>
+<circle cx="249" cy="523" r="2" fill="#24292f"/>
+<rect x="256" y="518" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="532.6" fill="#24292f">6</text>
+<circle cx="285" cy="523" r="2" fill="#24292f"/>
+<rect x="292" y="518" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="532.6" fill="#24292f">8</text>
+<circle cx="321" cy="523" r="2" fill="#24292f"/>
+<rect x="328" y="518" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="532.6" fill="#24292f">11</text>
+<circle cx="357" cy="523" r="2" fill="#24292f"/>
+<rect x="364" y="518" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="532.6" fill="#24292f">13</text>
+<circle cx="393" cy="523" r="2" fill="#24292f"/>
+<rect x="400" y="518" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="532.6" fill="#24292f">16</text>
+<circle cx="429" cy="523" r="2" fill="#24292f"/>
+<rect x="436" y="518" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="532.6" fill="#24292f">18</text>
+<circle cx="465" cy="523" r="2" fill="#24292f"/>
+<rect x="472" y="518" width="35" height="21" fill="#95c5df"/>
+<text class="v" x="489.5" y="532.6" fill="#24292f">20</text>
+<circle cx="501" cy="523" r="2" fill="#24292f"/>
+<rect x="508" y="518" width="35" height="21" fill="#7cb8da"/>
+<text class="v" x="525.5" y="532.6" fill="#24292f">23</text>
+<circle cx="537" cy="523" r="2" fill="#24292f"/>
+<rect x="544" y="518" width="35" height="21" fill="#6cafd6"/>
+<text class="v" x="561.5" y="532.6" fill="#24292f">25</text>
+<circle cx="573" cy="523" r="2" fill="#24292f"/>
+<rect x="580" y="518" width="35" height="21" fill="#58a1cf"/>
+<text class="v" x="597.5" y="532.6" fill="#24292f">28</text>
+<circle cx="609" cy="523" r="2" fill="#24292f"/>
+<rect x="616" y="518" width="35" height="21" fill="#4b98ca"/>
+<text class="v" x="633.5" y="532.6" fill="#24292f">30</text>
+<circle cx="645" cy="523" r="2" fill="#24292f"/>
+<rect x="652" y="518" width="35" height="21" fill="#3a8ac2"/>
+<text class="v" x="669.5" y="532.6" fill="#ffffff">33</text>
+<circle cx="681" cy="523" r="2" fill="#ffffff"/>
+<rect x="688" y="518" width="35" height="21" fill="#2f7fbc"/>
+<text class="v" x="705.5" y="532.6" fill="#ffffff">35</text>
+<circle cx="717" cy="523" r="2" fill="#ffffff"/>
+<rect x="724" y="518" width="35" height="21" fill="#2575b7"/>
+<text class="v" x="741.5" y="532.6" fill="#ffffff">37</text>
+<rect x="760" y="518" width="35" height="21" fill="#2575b7"/>
+<text class="v" x="777.5" y="532.6" fill="#ffffff">37</text>
+<rect x="796" y="518" width="35" height="21" fill="#2575b7"/>
+<text class="v" x="813.5" y="532.6" fill="#ffffff">37</text>
+<rect x="832" y="518" width="35" height="21" fill="#2575b7"/>
+<text class="v" x="849.5" y="532.6" fill="#ffffff">37</text>
+<rect x="868" y="518" width="35" height="21" fill="#2575b7"/>
+<text class="v" x="885.5" y="532.6" fill="#ffffff">37</text>
+<rect x="904" y="518" width="35" height="21" fill="#2575b7"/>
+<text class="v" x="921.5" y="532.6" fill="#ffffff">37</text>
+<rect x="940" y="518" width="35" height="21" fill="#2575b7"/>
+<text class="v" x="957.5" y="532.6" fill="#ffffff">37</text>
+<text class="t" x="68" y="555.0" text-anchor="end">8</text>
+<rect x="76" y="540" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="554.6" fill="#24292f">0</text>
+<circle cx="105" cy="545" r="2" fill="#24292f"/>
+<rect x="112" y="540" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="554.6" fill="#24292f">1</text>
+<circle cx="141" cy="545" r="2" fill="#24292f"/>
+<rect x="148" y="540" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="554.6" fill="#24292f">3</text>
+<circle cx="177" cy="545" r="2" fill="#24292f"/>
+<rect x="184" y="540" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="554.6" fill="#24292f">1</text>
+<circle cx="213" cy="545" r="2" fill="#24292f"/>
+<rect x="220" y="540" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="554.6" fill="#24292f">3</text>
+<circle cx="249" cy="545" r="2" fill="#24292f"/>
+<rect x="256" y="540" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="554.6" fill="#24292f">6</text>
+<circle cx="285" cy="545" r="2" fill="#24292f"/>
+<rect x="292" y="540" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="554.6" fill="#24292f">8</text>
+<circle cx="321" cy="545" r="2" fill="#24292f"/>
+<rect x="328" y="540" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="554.6" fill="#24292f">11</text>
+<circle cx="357" cy="545" r="2" fill="#24292f"/>
+<rect x="364" y="540" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="554.6" fill="#24292f">13</text>
+<circle cx="393" cy="545" r="2" fill="#24292f"/>
+<rect x="400" y="540" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="554.6" fill="#24292f">16</text>
+<circle cx="429" cy="545" r="2" fill="#24292f"/>
+<rect x="436" y="540" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="554.6" fill="#24292f">18</text>
+<circle cx="465" cy="545" r="2" fill="#24292f"/>
+<rect x="472" y="540" width="35" height="21" fill="#95c5df"/>
+<text class="v" x="489.5" y="554.6" fill="#24292f">20</text>
+<circle cx="501" cy="545" r="2" fill="#24292f"/>
+<rect x="508" y="540" width="35" height="21" fill="#7cb8da"/>
+<text class="v" x="525.5" y="554.6" fill="#24292f">23</text>
+<circle cx="537" cy="545" r="2" fill="#24292f"/>
+<rect x="544" y="540" width="35" height="21" fill="#6cafd6"/>
+<text class="v" x="561.5" y="554.6" fill="#24292f">25</text>
+<circle cx="573" cy="545" r="2" fill="#24292f"/>
+<rect x="580" y="540" width="35" height="21" fill="#58a1cf"/>
+<text class="v" x="597.5" y="554.6" fill="#24292f">28</text>
+<circle cx="609" cy="545" r="2" fill="#24292f"/>
+<rect x="616" y="540" width="35" height="21" fill="#4b98ca"/>
+<text class="v" x="633.5" y="554.6" fill="#24292f">30</text>
+<circle cx="645" cy="545" r="2" fill="#24292f"/>
+<rect x="652" y="540" width="35" height="21" fill="#3a8ac2"/>
+<text class="v" x="669.5" y="554.6" fill="#ffffff">33</text>
+<circle cx="681" cy="545" r="2" fill="#ffffff"/>
+<rect x="688" y="540" width="35" height="21" fill="#2f7fbc"/>
+<text class="v" x="705.5" y="554.6" fill="#ffffff">35</text>
+<circle cx="717" cy="545" r="2" fill="#ffffff"/>
+<rect x="724" y="540" width="35" height="21" fill="#2575b7"/>
+<text class="v" x="741.5" y="554.6" fill="#ffffff">37</text>
+<circle cx="753" cy="545" r="2" fill="#ffffff"/>
+<rect x="760" y="540" width="35" height="21" fill="#1c68ae"/>
+<text class="v" x="777.5" y="554.6" fill="#ffffff">39</text>
+<rect x="796" y="540" width="35" height="21" fill="#1c68ae"/>
+<text class="v" x="813.5" y="554.6" fill="#ffffff">39</text>
+<rect x="832" y="540" width="35" height="21" fill="#1c68ae"/>
+<text class="v" x="849.5" y="554.6" fill="#ffffff">39</text>
+<rect x="868" y="540" width="35" height="21" fill="#1c68ae"/>
+<text class="v" x="885.5" y="554.6" fill="#ffffff">39</text>
+<rect x="904" y="540" width="35" height="21" fill="#1c68ae"/>
+<text class="v" x="921.5" y="554.6" fill="#ffffff">39</text>
+<rect x="940" y="540" width="35" height="21" fill="#1c68ae"/>
+<text class="v" x="957.5" y="554.6" fill="#ffffff">39</text>
+<text class="t" x="93.5" y="578" text-anchor="middle">8</text>
+<text class="t" x="129.5" y="578" text-anchor="middle">9</text>
+<text class="t" x="165.5" y="578" text-anchor="middle">10</text>
+<text class="t" x="201.5" y="578" text-anchor="middle">11</text>
+<text class="t" x="237.5" y="578" text-anchor="middle">12</text>
+<text class="t" x="273.5" y="578" text-anchor="middle">13</text>
+<text class="t" x="309.5" y="578" text-anchor="middle">14</text>
+<text class="t" x="345.5" y="578" text-anchor="middle">15</text>
+<text class="t" x="381.5" y="578" text-anchor="middle">16</text>
+<text class="t" x="417.5" y="578" text-anchor="middle">17</text>
+<text class="t" x="453.5" y="578" text-anchor="middle">18</text>
+<text class="t" x="489.5" y="578" text-anchor="middle">19</text>
+<text class="t" x="525.5" y="578" text-anchor="middle">20</text>
+<text class="t" x="561.5" y="578" text-anchor="middle">21</text>
+<text class="t" x="597.5" y="578" text-anchor="middle">22</text>
+<text class="t" x="633.5" y="578" text-anchor="middle">23</text>
+<text class="t" x="669.5" y="578" text-anchor="middle">24</text>
+<text class="t" x="705.5" y="578" text-anchor="middle">25</text>
+<text class="t" x="741.5" y="578" text-anchor="middle">26</text>
+<text class="t" x="777.5" y="578" text-anchor="middle">27</text>
+<text class="t" x="813.5" y="578" text-anchor="middle">28</text>
+<text class="t" x="849.5" y="578" text-anchor="middle">29</text>
+<text class="t" x="885.5" y="578" text-anchor="middle">30</text>
+<text class="t" x="921.5" y="578" text-anchor="middle">31</text>
+<text class="t" x="957.5" y="578" text-anchor="middle">32</text>
+<text class="a" x="526.0" y="606">VRAM (GiB)</text>
+<text class="a" x="18" y="287.0" transform="rotate(-90 18 287.0)">System memory (GiB)</text>
+</svg>
diff --git a/docs/assets/capacity-blocks.zh.svg b/docs/assets/capacity-blocks.zh.svg
new file mode 100644
index 0000000..2f94f2b
--- /dev/null
+++ b/docs/assets/capacity-blocks.zh.svg
@@ -0,0 +1,1494 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="988" height="620" viewBox="0 0 988 620">
+<style>text{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI','Noto Sans','PingFang SC','Microsoft YaHei','Noto Sans CJK SC',Helvetica,Arial,sans-serif}.v{font-size:10.5px;text-anchor:middle}.t{font-size:11px;fill:#57606a}.a{font-size:12.5px;fill:#24292f;text-anchor:middle}</style>
+<rect width="988" height="620" fill="#ffffff"/>
+<text class="t" x="68" y="27.0" text-anchor="end">32</text>
+<rect x="76" y="12" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="26.6" fill="#24292f">0</text>
+<rect x="112" y="12" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="26.6" fill="#24292f">1</text>
+<rect x="148" y="12" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="26.6" fill="#24292f">3</text>
+<rect x="184" y="12" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="26.6" fill="#24292f">1</text>
+<rect x="220" y="12" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="26.6" fill="#24292f">3</text>
+<rect x="256" y="12" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="26.6" fill="#24292f">6</text>
+<rect x="292" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="26.6" fill="#24292f">8</text>
+<rect x="328" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="26.6" fill="#24292f">8</text>
+<rect x="364" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="26.6" fill="#24292f">8</text>
+<rect x="400" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="26.6" fill="#24292f">8</text>
+<rect x="436" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="26.6" fill="#24292f">8</text>
+<rect x="472" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="26.6" fill="#24292f">8</text>
+<rect x="508" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="26.6" fill="#24292f">8</text>
+<rect x="544" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="26.6" fill="#24292f">8</text>
+<rect x="580" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="26.6" fill="#24292f">8</text>
+<rect x="616" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="26.6" fill="#24292f">8</text>
+<rect x="652" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="26.6" fill="#24292f">8</text>
+<rect x="688" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="26.6" fill="#24292f">8</text>
+<rect x="724" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="26.6" fill="#24292f">8</text>
+<rect x="760" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="26.6" fill="#24292f">8</text>
+<rect x="796" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="26.6" fill="#24292f">8</text>
+<rect x="832" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="26.6" fill="#24292f">8</text>
+<rect x="868" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="26.6" fill="#24292f">8</text>
+<rect x="904" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="26.6" fill="#24292f">8</text>
+<rect x="940" y="12" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="26.6" fill="#24292f">8</text>
+<text class="t" x="68" y="49.0" text-anchor="end">31</text>
+<rect x="76" y="34" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="48.6" fill="#24292f">0</text>
+<rect x="112" y="34" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="48.6" fill="#24292f">1</text>
+<rect x="148" y="34" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="48.6" fill="#24292f">3</text>
+<rect x="184" y="34" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="48.6" fill="#24292f">1</text>
+<rect x="220" y="34" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="48.6" fill="#24292f">3</text>
+<rect x="256" y="34" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="48.6" fill="#24292f">6</text>
+<rect x="292" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="48.6" fill="#24292f">8</text>
+<rect x="328" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="48.6" fill="#24292f">8</text>
+<rect x="364" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="48.6" fill="#24292f">8</text>
+<rect x="400" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="48.6" fill="#24292f">8</text>
+<rect x="436" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="48.6" fill="#24292f">8</text>
+<rect x="472" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="48.6" fill="#24292f">8</text>
+<rect x="508" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="48.6" fill="#24292f">8</text>
+<rect x="544" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="48.6" fill="#24292f">8</text>
+<rect x="580" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="48.6" fill="#24292f">8</text>
+<rect x="616" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="48.6" fill="#24292f">8</text>
+<rect x="652" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="48.6" fill="#24292f">8</text>
+<rect x="688" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="48.6" fill="#24292f">8</text>
+<rect x="724" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="48.6" fill="#24292f">8</text>
+<rect x="760" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="48.6" fill="#24292f">8</text>
+<rect x="796" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="48.6" fill="#24292f">8</text>
+<rect x="832" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="48.6" fill="#24292f">8</text>
+<rect x="868" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="48.6" fill="#24292f">8</text>
+<rect x="904" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="48.6" fill="#24292f">8</text>
+<rect x="940" y="34" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="48.6" fill="#24292f">8</text>
+<text class="t" x="68" y="71.0" text-anchor="end">30</text>
+<rect x="76" y="56" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="70.6" fill="#24292f">0</text>
+<rect x="112" y="56" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="70.6" fill="#24292f">1</text>
+<rect x="148" y="56" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="70.6" fill="#24292f">3</text>
+<rect x="184" y="56" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="70.6" fill="#24292f">1</text>
+<rect x="220" y="56" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="70.6" fill="#24292f">3</text>
+<rect x="256" y="56" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="70.6" fill="#24292f">6</text>
+<rect x="292" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="70.6" fill="#24292f">8</text>
+<rect x="328" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="70.6" fill="#24292f">8</text>
+<rect x="364" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="70.6" fill="#24292f">8</text>
+<rect x="400" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="70.6" fill="#24292f">8</text>
+<rect x="436" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="70.6" fill="#24292f">8</text>
+<rect x="472" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="70.6" fill="#24292f">8</text>
+<rect x="508" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="70.6" fill="#24292f">8</text>
+<rect x="544" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="70.6" fill="#24292f">8</text>
+<rect x="580" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="70.6" fill="#24292f">8</text>
+<rect x="616" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="70.6" fill="#24292f">8</text>
+<rect x="652" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="70.6" fill="#24292f">8</text>
+<rect x="688" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="70.6" fill="#24292f">8</text>
+<rect x="724" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="70.6" fill="#24292f">8</text>
+<rect x="760" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="70.6" fill="#24292f">8</text>
+<rect x="796" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="70.6" fill="#24292f">8</text>
+<rect x="832" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="70.6" fill="#24292f">8</text>
+<rect x="868" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="70.6" fill="#24292f">8</text>
+<rect x="904" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="70.6" fill="#24292f">8</text>
+<rect x="940" y="56" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="70.6" fill="#24292f">8</text>
+<text class="t" x="68" y="93.0" text-anchor="end">29</text>
+<rect x="76" y="78" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="92.6" fill="#24292f">0</text>
+<rect x="112" y="78" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="92.6" fill="#24292f">1</text>
+<rect x="148" y="78" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="92.6" fill="#24292f">3</text>
+<rect x="184" y="78" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="92.6" fill="#24292f">1</text>
+<rect x="220" y="78" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="92.6" fill="#24292f">3</text>
+<rect x="256" y="78" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="92.6" fill="#24292f">6</text>
+<rect x="292" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="92.6" fill="#24292f">8</text>
+<rect x="328" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="92.6" fill="#24292f">8</text>
+<rect x="364" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="92.6" fill="#24292f">8</text>
+<rect x="400" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="92.6" fill="#24292f">8</text>
+<rect x="436" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="92.6" fill="#24292f">8</text>
+<rect x="472" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="92.6" fill="#24292f">8</text>
+<rect x="508" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="92.6" fill="#24292f">8</text>
+<rect x="544" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="92.6" fill="#24292f">8</text>
+<rect x="580" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="92.6" fill="#24292f">8</text>
+<rect x="616" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="92.6" fill="#24292f">8</text>
+<rect x="652" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="92.6" fill="#24292f">8</text>
+<rect x="688" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="92.6" fill="#24292f">8</text>
+<rect x="724" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="92.6" fill="#24292f">8</text>
+<rect x="760" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="92.6" fill="#24292f">8</text>
+<rect x="796" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="92.6" fill="#24292f">8</text>
+<rect x="832" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="92.6" fill="#24292f">8</text>
+<rect x="868" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="92.6" fill="#24292f">8</text>
+<rect x="904" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="92.6" fill="#24292f">8</text>
+<rect x="940" y="78" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="92.6" fill="#24292f">8</text>
+<text class="t" x="68" y="115.0" text-anchor="end">28</text>
+<rect x="76" y="100" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="114.6" fill="#24292f">0</text>
+<rect x="112" y="100" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="114.6" fill="#24292f">1</text>
+<rect x="148" y="100" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="114.6" fill="#24292f">3</text>
+<rect x="184" y="100" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="114.6" fill="#24292f">1</text>
+<rect x="220" y="100" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="114.6" fill="#24292f">3</text>
+<rect x="256" y="100" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="114.6" fill="#24292f">6</text>
+<rect x="292" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="114.6" fill="#24292f">8</text>
+<rect x="328" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="114.6" fill="#24292f">8</text>
+<rect x="364" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="114.6" fill="#24292f">8</text>
+<rect x="400" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="114.6" fill="#24292f">8</text>
+<rect x="436" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="114.6" fill="#24292f">8</text>
+<rect x="472" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="114.6" fill="#24292f">8</text>
+<rect x="508" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="114.6" fill="#24292f">8</text>
+<rect x="544" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="114.6" fill="#24292f">8</text>
+<rect x="580" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="114.6" fill="#24292f">8</text>
+<rect x="616" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="114.6" fill="#24292f">8</text>
+<rect x="652" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="114.6" fill="#24292f">8</text>
+<rect x="688" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="114.6" fill="#24292f">8</text>
+<rect x="724" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="114.6" fill="#24292f">8</text>
+<rect x="760" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="114.6" fill="#24292f">8</text>
+<rect x="796" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="114.6" fill="#24292f">8</text>
+<rect x="832" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="114.6" fill="#24292f">8</text>
+<rect x="868" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="114.6" fill="#24292f">8</text>
+<rect x="904" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="114.6" fill="#24292f">8</text>
+<rect x="940" y="100" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="114.6" fill="#24292f">8</text>
+<text class="t" x="68" y="137.0" text-anchor="end">27</text>
+<rect x="76" y="122" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="136.6" fill="#24292f">0</text>
+<rect x="112" y="122" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="136.6" fill="#24292f">1</text>
+<rect x="148" y="122" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="136.6" fill="#24292f">3</text>
+<rect x="184" y="122" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="136.6" fill="#24292f">1</text>
+<rect x="220" y="122" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="136.6" fill="#24292f">3</text>
+<rect x="256" y="122" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="136.6" fill="#24292f">6</text>
+<rect x="292" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="136.6" fill="#24292f">8</text>
+<rect x="328" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="136.6" fill="#24292f">8</text>
+<rect x="364" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="136.6" fill="#24292f">8</text>
+<rect x="400" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="136.6" fill="#24292f">8</text>
+<rect x="436" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="136.6" fill="#24292f">8</text>
+<rect x="472" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="136.6" fill="#24292f">8</text>
+<rect x="508" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="136.6" fill="#24292f">8</text>
+<rect x="544" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="136.6" fill="#24292f">8</text>
+<rect x="580" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="136.6" fill="#24292f">8</text>
+<rect x="616" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="136.6" fill="#24292f">8</text>
+<rect x="652" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="136.6" fill="#24292f">8</text>
+<rect x="688" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="136.6" fill="#24292f">8</text>
+<rect x="724" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="136.6" fill="#24292f">8</text>
+<rect x="760" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="136.6" fill="#24292f">8</text>
+<rect x="796" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="136.6" fill="#24292f">8</text>
+<rect x="832" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="136.6" fill="#24292f">8</text>
+<rect x="868" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="136.6" fill="#24292f">8</text>
+<rect x="904" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="136.6" fill="#24292f">8</text>
+<rect x="940" y="122" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="136.6" fill="#24292f">8</text>
+<text class="t" x="68" y="159.0" text-anchor="end">26</text>
+<rect x="76" y="144" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="158.6" fill="#24292f">0</text>
+<rect x="112" y="144" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="158.6" fill="#24292f">1</text>
+<rect x="148" y="144" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="158.6" fill="#24292f">3</text>
+<rect x="184" y="144" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="158.6" fill="#24292f">1</text>
+<rect x="220" y="144" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="158.6" fill="#24292f">3</text>
+<rect x="256" y="144" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="158.6" fill="#24292f">6</text>
+<rect x="292" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="158.6" fill="#24292f">8</text>
+<rect x="328" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="158.6" fill="#24292f">8</text>
+<rect x="364" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="158.6" fill="#24292f">8</text>
+<rect x="400" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="158.6" fill="#24292f">8</text>
+<rect x="436" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="158.6" fill="#24292f">8</text>
+<rect x="472" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="158.6" fill="#24292f">8</text>
+<rect x="508" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="158.6" fill="#24292f">8</text>
+<rect x="544" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="158.6" fill="#24292f">8</text>
+<rect x="580" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="158.6" fill="#24292f">8</text>
+<rect x="616" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="158.6" fill="#24292f">8</text>
+<rect x="652" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="158.6" fill="#24292f">8</text>
+<rect x="688" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="158.6" fill="#24292f">8</text>
+<rect x="724" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="158.6" fill="#24292f">8</text>
+<rect x="760" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="158.6" fill="#24292f">8</text>
+<rect x="796" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="158.6" fill="#24292f">8</text>
+<rect x="832" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="158.6" fill="#24292f">8</text>
+<rect x="868" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="158.6" fill="#24292f">8</text>
+<rect x="904" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="158.6" fill="#24292f">8</text>
+<rect x="940" y="144" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="158.6" fill="#24292f">8</text>
+<text class="t" x="68" y="181.0" text-anchor="end">25</text>
+<rect x="76" y="166" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="180.6" fill="#24292f">0</text>
+<circle cx="105" cy="171" r="2" fill="#24292f"/>
+<rect x="112" y="166" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="180.6" fill="#24292f">1</text>
+<rect x="148" y="166" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="180.6" fill="#24292f">3</text>
+<rect x="184" y="166" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="180.6" fill="#24292f">1</text>
+<rect x="220" y="166" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="180.6" fill="#24292f">3</text>
+<rect x="256" y="166" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="180.6" fill="#24292f">6</text>
+<rect x="292" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="180.6" fill="#24292f">8</text>
+<rect x="328" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="180.6" fill="#24292f">8</text>
+<rect x="364" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="180.6" fill="#24292f">8</text>
+<rect x="400" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="180.6" fill="#24292f">8</text>
+<rect x="436" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="180.6" fill="#24292f">8</text>
+<rect x="472" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="180.6" fill="#24292f">8</text>
+<rect x="508" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="180.6" fill="#24292f">8</text>
+<rect x="544" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="180.6" fill="#24292f">8</text>
+<rect x="580" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="180.6" fill="#24292f">8</text>
+<rect x="616" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="180.6" fill="#24292f">8</text>
+<rect x="652" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="180.6" fill="#24292f">8</text>
+<rect x="688" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="180.6" fill="#24292f">8</text>
+<rect x="724" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="180.6" fill="#24292f">8</text>
+<rect x="760" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="180.6" fill="#24292f">8</text>
+<rect x="796" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="180.6" fill="#24292f">8</text>
+<rect x="832" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="180.6" fill="#24292f">8</text>
+<rect x="868" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="180.6" fill="#24292f">8</text>
+<rect x="904" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="180.6" fill="#24292f">8</text>
+<rect x="940" y="166" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="180.6" fill="#24292f">8</text>
+<text class="t" x="68" y="203.0" text-anchor="end">24</text>
+<rect x="76" y="188" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="202.6" fill="#24292f">0</text>
+<circle cx="105" cy="193" r="2" fill="#24292f"/>
+<rect x="112" y="188" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="202.6" fill="#24292f">1</text>
+<circle cx="141" cy="193" r="2" fill="#24292f"/>
+<rect x="148" y="188" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="202.6" fill="#24292f">3</text>
+<circle cx="177" cy="193" r="2" fill="#24292f"/>
+<rect x="184" y="188" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="202.6" fill="#24292f">1</text>
+<rect x="220" y="188" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="202.6" fill="#24292f">3</text>
+<rect x="256" y="188" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="202.6" fill="#24292f">6</text>
+<rect x="292" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="202.6" fill="#24292f">8</text>
+<rect x="328" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="202.6" fill="#24292f">8</text>
+<rect x="364" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="202.6" fill="#24292f">8</text>
+<rect x="400" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="202.6" fill="#24292f">8</text>
+<rect x="436" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="202.6" fill="#24292f">8</text>
+<rect x="472" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="202.6" fill="#24292f">8</text>
+<rect x="508" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="202.6" fill="#24292f">8</text>
+<rect x="544" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="202.6" fill="#24292f">8</text>
+<rect x="580" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="202.6" fill="#24292f">8</text>
+<rect x="616" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="202.6" fill="#24292f">8</text>
+<rect x="652" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="202.6" fill="#24292f">8</text>
+<rect x="688" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="202.6" fill="#24292f">8</text>
+<rect x="724" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="202.6" fill="#24292f">8</text>
+<rect x="760" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="202.6" fill="#24292f">8</text>
+<rect x="796" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="202.6" fill="#24292f">8</text>
+<rect x="832" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="202.6" fill="#24292f">8</text>
+<rect x="868" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="202.6" fill="#24292f">8</text>
+<rect x="904" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="202.6" fill="#24292f">8</text>
+<rect x="940" y="188" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="202.6" fill="#24292f">8</text>
+<text class="t" x="68" y="225.0" text-anchor="end">23</text>
+<rect x="76" y="210" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="224.6" fill="#24292f">0</text>
+<circle cx="105" cy="215" r="2" fill="#24292f"/>
+<rect x="112" y="210" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="224.6" fill="#24292f">1</text>
+<circle cx="141" cy="215" r="2" fill="#24292f"/>
+<rect x="148" y="210" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="224.6" fill="#24292f">3</text>
+<circle cx="177" cy="215" r="2" fill="#24292f"/>
+<rect x="184" y="210" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="224.6" fill="#24292f">1</text>
+<circle cx="213" cy="215" r="2" fill="#24292f"/>
+<rect x="220" y="210" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="224.6" fill="#24292f">3</text>
+<rect x="256" y="210" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="224.6" fill="#24292f">6</text>
+<rect x="292" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="224.6" fill="#24292f">8</text>
+<rect x="328" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="224.6" fill="#24292f">8</text>
+<rect x="364" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="224.6" fill="#24292f">8</text>
+<rect x="400" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="224.6" fill="#24292f">8</text>
+<rect x="436" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="224.6" fill="#24292f">8</text>
+<rect x="472" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="224.6" fill="#24292f">8</text>
+<rect x="508" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="224.6" fill="#24292f">8</text>
+<rect x="544" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="224.6" fill="#24292f">8</text>
+<rect x="580" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="224.6" fill="#24292f">8</text>
+<rect x="616" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="224.6" fill="#24292f">8</text>
+<rect x="652" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="224.6" fill="#24292f">8</text>
+<rect x="688" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="224.6" fill="#24292f">8</text>
+<rect x="724" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="224.6" fill="#24292f">8</text>
+<rect x="760" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="224.6" fill="#24292f">8</text>
+<rect x="796" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="224.6" fill="#24292f">8</text>
+<rect x="832" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="224.6" fill="#24292f">8</text>
+<rect x="868" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="224.6" fill="#24292f">8</text>
+<rect x="904" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="224.6" fill="#24292f">8</text>
+<rect x="940" y="210" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="224.6" fill="#24292f">8</text>
+<text class="t" x="68" y="247.0" text-anchor="end">22</text>
+<rect x="76" y="232" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="246.6" fill="#24292f">0</text>
+<circle cx="105" cy="237" r="2" fill="#24292f"/>
+<rect x="112" y="232" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="246.6" fill="#24292f">1</text>
+<circle cx="141" cy="237" r="2" fill="#24292f"/>
+<rect x="148" y="232" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="246.6" fill="#24292f">3</text>
+<circle cx="177" cy="237" r="2" fill="#24292f"/>
+<rect x="184" y="232" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="246.6" fill="#24292f">1</text>
+<circle cx="213" cy="237" r="2" fill="#24292f"/>
+<rect x="220" y="232" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="246.6" fill="#24292f">3</text>
+<circle cx="249" cy="237" r="2" fill="#24292f"/>
+<rect x="256" y="232" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="246.6" fill="#24292f">6</text>
+<rect x="292" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="246.6" fill="#24292f">8</text>
+<rect x="328" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="246.6" fill="#24292f">8</text>
+<rect x="364" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="246.6" fill="#24292f">8</text>
+<rect x="400" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="246.6" fill="#24292f">8</text>
+<rect x="436" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="246.6" fill="#24292f">8</text>
+<rect x="472" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="246.6" fill="#24292f">8</text>
+<rect x="508" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="246.6" fill="#24292f">8</text>
+<rect x="544" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="246.6" fill="#24292f">8</text>
+<rect x="580" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="246.6" fill="#24292f">8</text>
+<rect x="616" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="246.6" fill="#24292f">8</text>
+<rect x="652" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="246.6" fill="#24292f">8</text>
+<rect x="688" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="246.6" fill="#24292f">8</text>
+<rect x="724" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="246.6" fill="#24292f">8</text>
+<rect x="760" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="246.6" fill="#24292f">8</text>
+<rect x="796" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="246.6" fill="#24292f">8</text>
+<rect x="832" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="246.6" fill="#24292f">8</text>
+<rect x="868" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="246.6" fill="#24292f">8</text>
+<rect x="904" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="246.6" fill="#24292f">8</text>
+<rect x="940" y="232" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="246.6" fill="#24292f">8</text>
+<text class="t" x="68" y="269.0" text-anchor="end">21</text>
+<rect x="76" y="254" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="268.6" fill="#24292f">0</text>
+<circle cx="105" cy="259" r="2" fill="#24292f"/>
+<rect x="112" y="254" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="268.6" fill="#24292f">1</text>
+<circle cx="141" cy="259" r="2" fill="#24292f"/>
+<rect x="148" y="254" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="268.6" fill="#24292f">3</text>
+<circle cx="177" cy="259" r="2" fill="#24292f"/>
+<rect x="184" y="254" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="268.6" fill="#24292f">1</text>
+<circle cx="213" cy="259" r="2" fill="#24292f"/>
+<rect x="220" y="254" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="268.6" fill="#24292f">3</text>
+<circle cx="249" cy="259" r="2" fill="#24292f"/>
+<rect x="256" y="254" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="268.6" fill="#24292f">6</text>
+<circle cx="285" cy="259" r="2" fill="#24292f"/>
+<rect x="292" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="268.6" fill="#24292f">8</text>
+<rect x="328" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="345.5" y="268.6" fill="#24292f">8</text>
+<rect x="364" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="381.5" y="268.6" fill="#24292f">8</text>
+<rect x="400" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="417.5" y="268.6" fill="#24292f">8</text>
+<rect x="436" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="453.5" y="268.6" fill="#24292f">8</text>
+<rect x="472" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="489.5" y="268.6" fill="#24292f">8</text>
+<rect x="508" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="525.5" y="268.6" fill="#24292f">8</text>
+<rect x="544" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="561.5" y="268.6" fill="#24292f">8</text>
+<rect x="580" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="597.5" y="268.6" fill="#24292f">8</text>
+<rect x="616" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="633.5" y="268.6" fill="#24292f">8</text>
+<rect x="652" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="669.5" y="268.6" fill="#24292f">8</text>
+<rect x="688" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="705.5" y="268.6" fill="#24292f">8</text>
+<rect x="724" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="741.5" y="268.6" fill="#24292f">8</text>
+<rect x="760" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="777.5" y="268.6" fill="#24292f">8</text>
+<rect x="796" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="813.5" y="268.6" fill="#24292f">8</text>
+<rect x="832" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="849.5" y="268.6" fill="#24292f">8</text>
+<rect x="868" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="885.5" y="268.6" fill="#24292f">8</text>
+<rect x="904" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="921.5" y="268.6" fill="#24292f">8</text>
+<rect x="940" y="254" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="957.5" y="268.6" fill="#24292f">8</text>
+<text class="t" x="68" y="291.0" text-anchor="end">20</text>
+<rect x="76" y="276" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="290.6" fill="#24292f">0</text>
+<circle cx="105" cy="281" r="2" fill="#24292f"/>
+<rect x="112" y="276" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="290.6" fill="#24292f">1</text>
+<circle cx="141" cy="281" r="2" fill="#24292f"/>
+<rect x="148" y="276" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="290.6" fill="#24292f">3</text>
+<circle cx="177" cy="281" r="2" fill="#24292f"/>
+<rect x="184" y="276" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="290.6" fill="#24292f">1</text>
+<circle cx="213" cy="281" r="2" fill="#24292f"/>
+<rect x="220" y="276" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="290.6" fill="#24292f">3</text>
+<circle cx="249" cy="281" r="2" fill="#24292f"/>
+<rect x="256" y="276" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="290.6" fill="#24292f">6</text>
+<circle cx="285" cy="281" r="2" fill="#24292f"/>
+<rect x="292" y="276" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="290.6" fill="#24292f">8</text>
+<circle cx="321" cy="281" r="2" fill="#24292f"/>
+<rect x="328" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="345.5" y="290.6" fill="#24292f">10</text>
+<rect x="364" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="381.5" y="290.6" fill="#24292f">10</text>
+<rect x="400" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="417.5" y="290.6" fill="#24292f">10</text>
+<rect x="436" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="453.5" y="290.6" fill="#24292f">10</text>
+<rect x="472" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="489.5" y="290.6" fill="#24292f">10</text>
+<rect x="508" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="525.5" y="290.6" fill="#24292f">10</text>
+<rect x="544" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="561.5" y="290.6" fill="#24292f">10</text>
+<rect x="580" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="597.5" y="290.6" fill="#24292f">10</text>
+<rect x="616" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="633.5" y="290.6" fill="#24292f">10</text>
+<rect x="652" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="669.5" y="290.6" fill="#24292f">10</text>
+<rect x="688" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="705.5" y="290.6" fill="#24292f">10</text>
+<rect x="724" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="741.5" y="290.6" fill="#24292f">10</text>
+<rect x="760" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="777.5" y="290.6" fill="#24292f">10</text>
+<rect x="796" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="813.5" y="290.6" fill="#24292f">10</text>
+<rect x="832" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="849.5" y="290.6" fill="#24292f">10</text>
+<rect x="868" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="885.5" y="290.6" fill="#24292f">10</text>
+<rect x="904" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="921.5" y="290.6" fill="#24292f">10</text>
+<rect x="940" y="276" width="35" height="21" fill="#d0e2f2"/>
+<text class="v" x="957.5" y="290.6" fill="#24292f">10</text>
+<text class="t" x="68" y="313.0" text-anchor="end">19</text>
+<rect x="76" y="298" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="312.6" fill="#24292f">0</text>
+<circle cx="105" cy="303" r="2" fill="#24292f"/>
+<rect x="112" y="298" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="312.6" fill="#24292f">1</text>
+<circle cx="141" cy="303" r="2" fill="#24292f"/>
+<rect x="148" y="298" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="312.6" fill="#24292f">3</text>
+<circle cx="177" cy="303" r="2" fill="#24292f"/>
+<rect x="184" y="298" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="312.6" fill="#24292f">1</text>
+<circle cx="213" cy="303" r="2" fill="#24292f"/>
+<rect x="220" y="298" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="312.6" fill="#24292f">3</text>
+<circle cx="249" cy="303" r="2" fill="#24292f"/>
+<rect x="256" y="298" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="312.6" fill="#24292f">6</text>
+<circle cx="285" cy="303" r="2" fill="#24292f"/>
+<rect x="292" y="298" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="312.6" fill="#24292f">8</text>
+<circle cx="321" cy="303" r="2" fill="#24292f"/>
+<rect x="328" y="298" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="312.6" fill="#24292f">11</text>
+<circle cx="357" cy="303" r="2" fill="#24292f"/>
+<rect x="364" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="381.5" y="312.6" fill="#24292f">12</text>
+<rect x="400" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="417.5" y="312.6" fill="#24292f">12</text>
+<rect x="436" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="453.5" y="312.6" fill="#24292f">12</text>
+<rect x="472" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="489.5" y="312.6" fill="#24292f">12</text>
+<rect x="508" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="525.5" y="312.6" fill="#24292f">12</text>
+<rect x="544" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="561.5" y="312.6" fill="#24292f">12</text>
+<rect x="580" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="597.5" y="312.6" fill="#24292f">12</text>
+<rect x="616" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="633.5" y="312.6" fill="#24292f">12</text>
+<rect x="652" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="669.5" y="312.6" fill="#24292f">12</text>
+<rect x="688" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="705.5" y="312.6" fill="#24292f">12</text>
+<rect x="724" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="741.5" y="312.6" fill="#24292f">12</text>
+<rect x="760" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="777.5" y="312.6" fill="#24292f">12</text>
+<rect x="796" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="813.5" y="312.6" fill="#24292f">12</text>
+<rect x="832" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="849.5" y="312.6" fill="#24292f">12</text>
+<rect x="868" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="885.5" y="312.6" fill="#24292f">12</text>
+<rect x="904" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="921.5" y="312.6" fill="#24292f">12</text>
+<rect x="940" y="298" width="35" height="21" fill="#c8dcf0"/>
+<text class="v" x="957.5" y="312.6" fill="#24292f">12</text>
+<text class="t" x="68" y="335.0" text-anchor="end">18</text>
+<rect x="76" y="320" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="334.6" fill="#24292f">0</text>
+<circle cx="105" cy="325" r="2" fill="#24292f"/>
+<rect x="112" y="320" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="334.6" fill="#24292f">1</text>
+<circle cx="141" cy="325" r="2" fill="#24292f"/>
+<rect x="148" y="320" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="334.6" fill="#24292f">3</text>
+<circle cx="177" cy="325" r="2" fill="#24292f"/>
+<rect x="184" y="320" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="334.6" fill="#24292f">1</text>
+<circle cx="213" cy="325" r="2" fill="#24292f"/>
+<rect x="220" y="320" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="334.6" fill="#24292f">3</text>
+<circle cx="249" cy="325" r="2" fill="#24292f"/>
+<rect x="256" y="320" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="334.6" fill="#24292f">6</text>
+<circle cx="285" cy="325" r="2" fill="#24292f"/>
+<rect x="292" y="320" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="334.6" fill="#24292f">8</text>
+<circle cx="321" cy="325" r="2" fill="#24292f"/>
+<rect x="328" y="320" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="334.6" fill="#24292f">11</text>
+<circle cx="357" cy="325" r="2" fill="#24292f"/>
+<rect x="364" y="320" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="334.6" fill="#24292f">13</text>
+<circle cx="393" cy="325" r="2" fill="#24292f"/>
+<rect x="400" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="417.5" y="334.6" fill="#24292f">15</text>
+<rect x="436" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="453.5" y="334.6" fill="#24292f">15</text>
+<rect x="472" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="489.5" y="334.6" fill="#24292f">15</text>
+<rect x="508" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="525.5" y="334.6" fill="#24292f">15</text>
+<rect x="544" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="561.5" y="334.6" fill="#24292f">15</text>
+<rect x="580" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="597.5" y="334.6" fill="#24292f">15</text>
+<rect x="616" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="633.5" y="334.6" fill="#24292f">15</text>
+<rect x="652" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="669.5" y="334.6" fill="#24292f">15</text>
+<rect x="688" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="705.5" y="334.6" fill="#24292f">15</text>
+<rect x="724" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="741.5" y="334.6" fill="#24292f">15</text>
+<rect x="760" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="777.5" y="334.6" fill="#24292f">15</text>
+<rect x="796" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="813.5" y="334.6" fill="#24292f">15</text>
+<rect x="832" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="849.5" y="334.6" fill="#24292f">15</text>
+<rect x="868" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="885.5" y="334.6" fill="#24292f">15</text>
+<rect x="904" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="921.5" y="334.6" fill="#24292f">15</text>
+<rect x="940" y="320" width="35" height="21" fill="#b7d4ea"/>
+<text class="v" x="957.5" y="334.6" fill="#24292f">15</text>
+<text class="t" x="68" y="357.0" text-anchor="end">17</text>
+<rect x="76" y="342" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="356.6" fill="#24292f">0</text>
+<circle cx="105" cy="347" r="2" fill="#24292f"/>
+<rect x="112" y="342" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="356.6" fill="#24292f">1</text>
+<circle cx="141" cy="347" r="2" fill="#24292f"/>
+<rect x="148" y="342" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="356.6" fill="#24292f">3</text>
+<circle cx="177" cy="347" r="2" fill="#24292f"/>
+<rect x="184" y="342" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="356.6" fill="#24292f">1</text>
+<circle cx="213" cy="347" r="2" fill="#24292f"/>
+<rect x="220" y="342" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="356.6" fill="#24292f">3</text>
+<circle cx="249" cy="347" r="2" fill="#24292f"/>
+<rect x="256" y="342" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="356.6" fill="#24292f">6</text>
+<circle cx="285" cy="347" r="2" fill="#24292f"/>
+<rect x="292" y="342" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="356.6" fill="#24292f">8</text>
+<circle cx="321" cy="347" r="2" fill="#24292f"/>
+<rect x="328" y="342" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="356.6" fill="#24292f">11</text>
+<circle cx="357" cy="347" r="2" fill="#24292f"/>
+<rect x="364" y="342" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="356.6" fill="#24292f">13</text>
+<circle cx="393" cy="347" r="2" fill="#24292f"/>
+<rect x="400" y="342" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="356.6" fill="#24292f">16</text>
+<circle cx="429" cy="347" r="2" fill="#24292f"/>
+<rect x="436" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="453.5" y="356.6" fill="#24292f">17</text>
+<rect x="472" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="489.5" y="356.6" fill="#24292f">17</text>
+<rect x="508" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="525.5" y="356.6" fill="#24292f">17</text>
+<rect x="544" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="561.5" y="356.6" fill="#24292f">17</text>
+<rect x="580" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="597.5" y="356.6" fill="#24292f">17</text>
+<rect x="616" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="633.5" y="356.6" fill="#24292f">17</text>
+<rect x="652" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="669.5" y="356.6" fill="#24292f">17</text>
+<rect x="688" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="705.5" y="356.6" fill="#24292f">17</text>
+<rect x="724" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="741.5" y="356.6" fill="#24292f">17</text>
+<rect x="760" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="777.5" y="356.6" fill="#24292f">17</text>
+<rect x="796" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="813.5" y="356.6" fill="#24292f">17</text>
+<rect x="832" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="849.5" y="356.6" fill="#24292f">17</text>
+<rect x="868" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="885.5" y="356.6" fill="#24292f">17</text>
+<rect x="904" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="921.5" y="356.6" fill="#24292f">17</text>
+<rect x="940" y="342" width="35" height="21" fill="#aacfe5"/>
+<text class="v" x="957.5" y="356.6" fill="#24292f">17</text>
+<text class="t" x="68" y="379.0" text-anchor="end">16</text>
+<rect x="76" y="364" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="378.6" fill="#24292f">0</text>
+<circle cx="105" cy="369" r="2" fill="#24292f"/>
+<rect x="112" y="364" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="378.6" fill="#24292f">1</text>
+<circle cx="141" cy="369" r="2" fill="#24292f"/>
+<rect x="148" y="364" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="378.6" fill="#24292f">3</text>
+<circle cx="177" cy="369" r="2" fill="#24292f"/>
+<rect x="184" y="364" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="378.6" fill="#24292f">1</text>
+<circle cx="213" cy="369" r="2" fill="#24292f"/>
+<rect x="220" y="364" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="378.6" fill="#24292f">3</text>
+<circle cx="249" cy="369" r="2" fill="#24292f"/>
+<rect x="256" y="364" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="378.6" fill="#24292f">6</text>
+<circle cx="285" cy="369" r="2" fill="#24292f"/>
+<rect x="292" y="364" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="378.6" fill="#24292f">8</text>
+<circle cx="321" cy="369" r="2" fill="#24292f"/>
+<rect x="328" y="364" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="378.6" fill="#24292f">11</text>
+<circle cx="357" cy="369" r="2" fill="#24292f"/>
+<rect x="364" y="364" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="378.6" fill="#24292f">13</text>
+<circle cx="393" cy="369" r="2" fill="#24292f"/>
+<rect x="400" y="364" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="378.6" fill="#24292f">16</text>
+<circle cx="429" cy="369" r="2" fill="#24292f"/>
+<rect x="436" y="364" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="378.6" fill="#24292f">18</text>
+<circle cx="465" cy="369" r="2" fill="#24292f"/>
+<rect x="472" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="489.5" y="378.6" fill="#24292f">19</text>
+<rect x="508" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="525.5" y="378.6" fill="#24292f">19</text>
+<rect x="544" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="561.5" y="378.6" fill="#24292f">19</text>
+<rect x="580" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="597.5" y="378.6" fill="#24292f">19</text>
+<rect x="616" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="633.5" y="378.6" fill="#24292f">19</text>
+<rect x="652" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="669.5" y="378.6" fill="#24292f">19</text>
+<rect x="688" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="705.5" y="378.6" fill="#24292f">19</text>
+<rect x="724" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="741.5" y="378.6" fill="#24292f">19</text>
+<rect x="760" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="777.5" y="378.6" fill="#24292f">19</text>
+<rect x="796" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="813.5" y="378.6" fill="#24292f">19</text>
+<rect x="832" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="849.5" y="378.6" fill="#24292f">19</text>
+<rect x="868" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="885.5" y="378.6" fill="#24292f">19</text>
+<rect x="904" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="921.5" y="378.6" fill="#24292f">19</text>
+<rect x="940" y="364" width="35" height="21" fill="#9dc9e1"/>
+<text class="v" x="957.5" y="378.6" fill="#24292f">19</text>
+<text class="t" x="68" y="401.0" text-anchor="end">15</text>
+<rect x="76" y="386" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="400.6" fill="#24292f">0</text>
+<circle cx="105" cy="391" r="2" fill="#24292f"/>
+<rect x="112" y="386" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="400.6" fill="#24292f">1</text>
+<circle cx="141" cy="391" r="2" fill="#24292f"/>
+<rect x="148" y="386" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="400.6" fill="#24292f">3</text>
+<circle cx="177" cy="391" r="2" fill="#24292f"/>
+<rect x="184" y="386" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="400.6" fill="#24292f">1</text>
+<circle cx="213" cy="391" r="2" fill="#24292f"/>
+<rect x="220" y="386" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="400.6" fill="#24292f">3</text>
+<circle cx="249" cy="391" r="2" fill="#24292f"/>
+<rect x="256" y="386" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="400.6" fill="#24292f">6</text>
+<circle cx="285" cy="391" r="2" fill="#24292f"/>
+<rect x="292" y="386" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="400.6" fill="#24292f">8</text>
+<circle cx="321" cy="391" r="2" fill="#24292f"/>
+<rect x="328" y="386" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="400.6" fill="#24292f">11</text>
+<circle cx="357" cy="391" r="2" fill="#24292f"/>
+<rect x="364" y="386" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="400.6" fill="#24292f">13</text>
+<circle cx="393" cy="391" r="2" fill="#24292f"/>
+<rect x="400" y="386" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="400.6" fill="#24292f">16</text>
+<circle cx="429" cy="391" r="2" fill="#24292f"/>
+<rect x="436" y="386" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="400.6" fill="#24292f">18</text>
+<circle cx="465" cy="391" r="2" fill="#24292f"/>
+<rect x="472" y="386" width="35" height="21" fill="#95c5df"/>
+<text class="v" x="489.5" y="400.6" fill="#24292f">20</text>
+<circle cx="501" cy="391" r="2" fill="#24292f"/>
+<rect x="508" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="525.5" y="400.6" fill="#24292f">22</text>
+<rect x="544" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="561.5" y="400.6" fill="#24292f">22</text>
+<rect x="580" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="597.5" y="400.6" fill="#24292f">22</text>
+<rect x="616" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="633.5" y="400.6" fill="#24292f">22</text>
+<rect x="652" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="669.5" y="400.6" fill="#24292f">22</text>
+<rect x="688" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="705.5" y="400.6" fill="#24292f">22</text>
+<rect x="724" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="741.5" y="400.6" fill="#24292f">22</text>
+<rect x="760" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="777.5" y="400.6" fill="#24292f">22</text>
+<rect x="796" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="813.5" y="400.6" fill="#24292f">22</text>
+<rect x="832" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="849.5" y="400.6" fill="#24292f">22</text>
+<rect x="868" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="885.5" y="400.6" fill="#24292f">22</text>
+<rect x="904" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="921.5" y="400.6" fill="#24292f">22</text>
+<rect x="940" y="386" width="35" height="21" fill="#84bcdc"/>
+<text class="v" x="957.5" y="400.6" fill="#24292f">22</text>
+<text class="t" x="68" y="423.0" text-anchor="end">14</text>
+<rect x="76" y="408" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="422.6" fill="#24292f">0</text>
+<circle cx="105" cy="413" r="2" fill="#24292f"/>
+<rect x="112" y="408" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="422.6" fill="#24292f">1</text>
+<circle cx="141" cy="413" r="2" fill="#24292f"/>
+<rect x="148" y="408" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="422.6" fill="#24292f">3</text>
+<circle cx="177" cy="413" r="2" fill="#24292f"/>
+<rect x="184" y="408" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="422.6" fill="#24292f">1</text>
+<circle cx="213" cy="413" r="2" fill="#24292f"/>
+<rect x="220" y="408" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="422.6" fill="#24292f">3</text>
+<circle cx="249" cy="413" r="2" fill="#24292f"/>
+<rect x="256" y="408" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="422.6" fill="#24292f">6</text>
+<circle cx="285" cy="413" r="2" fill="#24292f"/>
+<rect x="292" y="408" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="422.6" fill="#24292f">8</text>
+<circle cx="321" cy="413" r="2" fill="#24292f"/>
+<rect x="328" y="408" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="422.6" fill="#24292f">11</text>
+<circle cx="357" cy="413" r="2" fill="#24292f"/>
+<rect x="364" y="408" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="422.6" fill="#24292f">13</text>
+<circle cx="393" cy="413" r="2" fill="#24292f"/>
+<rect x="400" y="408" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="422.6" fill="#24292f">16</text>
+<circle cx="429" cy="413" r="2" fill="#24292f"/>
+<rect x="436" y="408" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="422.6" fill="#24292f">18</text>
+<circle cx="465" cy="413" r="2" fill="#24292f"/>
+<rect x="472" y="408" width="35" height="21" fill="#95c5df"/>
+<text class="v" x="489.5" y="422.6" fill="#24292f">20</text>
+<circle cx="501" cy="413" r="2" fill="#24292f"/>
+<rect x="508" y="408" width="35" height="21" fill="#7cb8da"/>
+<text class="v" x="525.5" y="422.6" fill="#24292f">23</text>
+<circle cx="537" cy="413" r="2" fill="#24292f"/>
+<rect x="544" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="561.5" y="422.6" fill="#24292f">24</text>
+<rect x="580" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="597.5" y="422.6" fill="#24292f">24</text>
+<rect x="616" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="633.5" y="422.6" fill="#24292f">24</text>
+<rect x="652" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="669.5" y="422.6" fill="#24292f">24</text>
+<rect x="688" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="705.5" y="422.6" fill="#24292f">24</text>
+<rect x="724" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="741.5" y="422.6" fill="#24292f">24</text>
+<rect x="760" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="777.5" y="422.6" fill="#24292f">24</text>
+<rect x="796" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="813.5" y="422.6" fill="#24292f">24</text>
+<rect x="832" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="849.5" y="422.6" fill="#24292f">24</text>
+<rect x="868" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="885.5" y="422.6" fill="#24292f">24</text>
+<rect x="904" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="921.5" y="422.6" fill="#24292f">24</text>
+<rect x="940" y="408" width="35" height="21" fill="#74b3d8"/>
+<text class="v" x="957.5" y="422.6" fill="#24292f">24</text>
+<text class="t" x="68" y="445.0" text-anchor="end">13</text>
+<rect x="76" y="430" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="444.6" fill="#24292f">0</text>
+<circle cx="105" cy="435" r="2" fill="#24292f"/>
+<rect x="112" y="430" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="444.6" fill="#24292f">1</text>
+<circle cx="141" cy="435" r="2" fill="#24292f"/>
+<rect x="148" y="430" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="444.6" fill="#24292f">3</text>
+<circle cx="177" cy="435" r="2" fill="#24292f"/>
+<rect x="184" y="430" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="444.6" fill="#24292f">1</text>
+<circle cx="213" cy="435" r="2" fill="#24292f"/>
+<rect x="220" y="430" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="444.6" fill="#24292f">3</text>
+<circle cx="249" cy="435" r="2" fill="#24292f"/>
+<rect x="256" y="430" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="444.6" fill="#24292f">6</text>
+<circle cx="285" cy="435" r="2" fill="#24292f"/>
+<rect x="292" y="430" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="444.6" fill="#24292f">8</text>
+<circle cx="321" cy="435" r="2" fill="#24292f"/>
+<rect x="328" y="430" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="444.6" fill="#24292f">11</text>
+<circle cx="357" cy="435" r="2" fill="#24292f"/>
+<rect x="364" y="430" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="444.6" fill="#24292f">13</text>
+<circle cx="393" cy="435" r="2" fill="#24292f"/>
+<rect x="400" y="430" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="444.6" fill="#24292f">16</text>
+<circle cx="429" cy="435" r="2" fill="#24292f"/>
+<rect x="436" y="430" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="444.6" fill="#24292f">18</text>
+<circle cx="465" cy="435" r="2" fill="#24292f"/>
+<rect x="472" y="430" width="35" height="21" fill="#95c5df"/>
+<text class="v" x="489.5" y="444.6" fill="#24292f">20</text>
+<circle cx="501" cy="435" r="2" fill="#24292f"/>
+<rect x="508" y="430" width="35" height="21" fill="#7cb8da"/>
+<text class="v" x="525.5" y="444.6" fill="#24292f">23</text>
+<circle cx="537" cy="435" r="2" fill="#24292f"/>
+<rect x="544" y="430" width="35" height="21" fill="#6cafd6"/>
+<text class="v" x="561.5" y="444.6" fill="#24292f">25</text>
+<circle cx="573" cy="435" r="2" fill="#24292f"/>
+<rect x="580" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="597.5" y="444.6" fill="#24292f">27</text>
+<rect x="616" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="633.5" y="444.6" fill="#24292f">27</text>
+<rect x="652" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="669.5" y="444.6" fill="#24292f">27</text>
+<rect x="688" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="705.5" y="444.6" fill="#24292f">27</text>
+<rect x="724" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="741.5" y="444.6" fill="#24292f">27</text>
+<rect x="760" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="777.5" y="444.6" fill="#24292f">27</text>
+<rect x="796" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="813.5" y="444.6" fill="#24292f">27</text>
+<rect x="832" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="849.5" y="444.6" fill="#24292f">27</text>
+<rect x="868" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="885.5" y="444.6" fill="#24292f">27</text>
+<rect x="904" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="921.5" y="444.6" fill="#24292f">27</text>
+<rect x="940" y="430" width="35" height="21" fill="#5fa6d1"/>
+<text class="v" x="957.5" y="444.6" fill="#24292f">27</text>
+<text class="t" x="68" y="467.0" text-anchor="end">12</text>
+<rect x="76" y="452" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="466.6" fill="#24292f">0</text>
+<circle cx="105" cy="457" r="2" fill="#24292f"/>
+<rect x="112" y="452" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="466.6" fill="#24292f">1</text>
+<circle cx="141" cy="457" r="2" fill="#24292f"/>
+<rect x="148" y="452" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="466.6" fill="#24292f">3</text>
+<circle cx="177" cy="457" r="2" fill="#24292f"/>
+<rect x="184" y="452" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="466.6" fill="#24292f">1</text>
+<circle cx="213" cy="457" r="2" fill="#24292f"/>
+<rect x="220" y="452" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="466.6" fill="#24292f">3</text>
+<circle cx="249" cy="457" r="2" fill="#24292f"/>
+<rect x="256" y="452" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="466.6" fill="#24292f">6</text>
+<circle cx="285" cy="457" r="2" fill="#24292f"/>
+<rect x="292" y="452" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="466.6" fill="#24292f">8</text>
+<circle cx="321" cy="457" r="2" fill="#24292f"/>
+<rect x="328" y="452" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="466.6" fill="#24292f">11</text>
+<circle cx="357" cy="457" r="2" fill="#24292f"/>
+<rect x="364" y="452" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="466.6" fill="#24292f">13</text>
+<circle cx="393" cy="457" r="2" fill="#24292f"/>
+<rect x="400" y="452" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="466.6" fill="#24292f">16</text>
+<circle cx="429" cy="457" r="2" fill="#24292f"/>
+<rect x="436" y="452" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="466.6" fill="#24292f">18</text>
+<circle cx="465" cy="457" r="2" fill="#24292f"/>
+<rect x="472" y="452" width="35" height="21" fill="#95c5df"/>
+<text class="v" x="489.5" y="466.6" fill="#24292f">20</text>
+<circle cx="501" cy="457" r="2" fill="#24292f"/>
+<rect x="508" y="452" width="35" height="21" fill="#7cb8da"/>
+<text class="v" x="525.5" y="466.6" fill="#24292f">23</text>
+<circle cx="537" cy="457" r="2" fill="#24292f"/>
+<rect x="544" y="452" width="35" height="21" fill="#6cafd6"/>
+<text class="v" x="561.5" y="466.6" fill="#24292f">25</text>
+<circle cx="573" cy="457" r="2" fill="#24292f"/>
+<rect x="580" y="452" width="35" height="21" fill="#58a1cf"/>
+<text class="v" x="597.5" y="466.6" fill="#24292f">28</text>
+<circle cx="609" cy="457" r="2" fill="#24292f"/>
+<rect x="616" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="633.5" y="466.6" fill="#24292f">29</text>
+<rect x="652" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="669.5" y="466.6" fill="#24292f">29</text>
+<rect x="688" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="705.5" y="466.6" fill="#24292f">29</text>
+<rect x="724" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="741.5" y="466.6" fill="#24292f">29</text>
+<rect x="760" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="777.5" y="466.6" fill="#24292f">29</text>
+<rect x="796" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="813.5" y="466.6" fill="#24292f">29</text>
+<rect x="832" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="849.5" y="466.6" fill="#24292f">29</text>
+<rect x="868" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="885.5" y="466.6" fill="#24292f">29</text>
+<rect x="904" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="921.5" y="466.6" fill="#24292f">29</text>
+<rect x="940" y="452" width="35" height="21" fill="#529dcc"/>
+<text class="v" x="957.5" y="466.6" fill="#24292f">29</text>
+<text class="t" x="68" y="489.0" text-anchor="end">11</text>
+<rect x="76" y="474" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="488.6" fill="#24292f">0</text>
+<circle cx="105" cy="479" r="2" fill="#24292f"/>
+<rect x="112" y="474" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="488.6" fill="#24292f">1</text>
+<circle cx="141" cy="479" r="2" fill="#24292f"/>
+<rect x="148" y="474" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="488.6" fill="#24292f">3</text>
+<circle cx="177" cy="479" r="2" fill="#24292f"/>
+<rect x="184" y="474" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="488.6" fill="#24292f">1</text>
+<circle cx="213" cy="479" r="2" fill="#24292f"/>
+<rect x="220" y="474" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="488.6" fill="#24292f">3</text>
+<circle cx="249" cy="479" r="2" fill="#24292f"/>
+<rect x="256" y="474" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="488.6" fill="#24292f">6</text>
+<circle cx="285" cy="479" r="2" fill="#24292f"/>
+<rect x="292" y="474" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="488.6" fill="#24292f">8</text>
+<circle cx="321" cy="479" r="2" fill="#24292f"/>
+<rect x="328" y="474" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="488.6" fill="#24292f">11</text>
+<circle cx="357" cy="479" r="2" fill="#24292f"/>
+<rect x="364" y="474" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="488.6" fill="#24292f">13</text>
+<circle cx="393" cy="479" r="2" fill="#24292f"/>
+<rect x="400" y="474" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="488.6" fill="#24292f">16</text>
+<circle cx="429" cy="479" r="2" fill="#24292f"/>
+<rect x="436" y="474" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="488.6" fill="#24292f">18</text>
+<circle cx="465" cy="479" r="2" fill="#24292f"/>
+<rect x="472" y="474" width="35" height="21" fill="#95c5df"/>
+<text class="v" x="489.5" y="488.6" fill="#24292f">20</text>
+<circle cx="501" cy="479" r="2" fill="#24292f"/>
+<rect x="508" y="474" width="35" height="21" fill="#7cb8da"/>
+<text class="v" x="525.5" y="488.6" fill="#24292f">23</text>
+<circle cx="537" cy="479" r="2" fill="#24292f"/>
+<rect x="544" y="474" width="35" height="21" fill="#6cafd6"/>
+<text class="v" x="561.5" y="488.6" fill="#24292f">25</text>
+<circle cx="573" cy="479" r="2" fill="#24292f"/>
+<rect x="580" y="474" width="35" height="21" fill="#58a1cf"/>
+<text class="v" x="597.5" y="488.6" fill="#24292f">28</text>
+<circle cx="609" cy="479" r="2" fill="#24292f"/>
+<rect x="616" y="474" width="35" height="21" fill="#4b98ca"/>
+<text class="v" x="633.5" y="488.6" fill="#24292f">30</text>
+<circle cx="645" cy="479" r="2" fill="#24292f"/>
+<rect x="652" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="669.5" y="488.6" fill="#24292f">32</text>
+<rect x="688" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="705.5" y="488.6" fill="#24292f">32</text>
+<rect x="724" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="741.5" y="488.6" fill="#24292f">32</text>
+<rect x="760" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="777.5" y="488.6" fill="#24292f">32</text>
+<rect x="796" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="813.5" y="488.6" fill="#24292f">32</text>
+<rect x="832" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="849.5" y="488.6" fill="#24292f">32</text>
+<rect x="868" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="885.5" y="488.6" fill="#24292f">32</text>
+<rect x="904" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="921.5" y="488.6" fill="#24292f">32</text>
+<rect x="940" y="474" width="35" height="21" fill="#3f8fc4"/>
+<text class="v" x="957.5" y="488.6" fill="#24292f">32</text>
+<text class="t" x="68" y="511.0" text-anchor="end">10</text>
+<rect x="76" y="496" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="510.6" fill="#24292f">0</text>
+<circle cx="105" cy="501" r="2" fill="#24292f"/>
+<rect x="112" y="496" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="510.6" fill="#24292f">1</text>
+<circle cx="141" cy="501" r="2" fill="#24292f"/>
+<rect x="148" y="496" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="510.6" fill="#24292f">3</text>
+<circle cx="177" cy="501" r="2" fill="#24292f"/>
+<rect x="184" y="496" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="510.6" fill="#24292f">1</text>
+<circle cx="213" cy="501" r="2" fill="#24292f"/>
+<rect x="220" y="496" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="510.6" fill="#24292f">3</text>
+<circle cx="249" cy="501" r="2" fill="#24292f"/>
+<rect x="256" y="496" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="510.6" fill="#24292f">6</text>
+<circle cx="285" cy="501" r="2" fill="#24292f"/>
+<rect x="292" y="496" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="510.6" fill="#24292f">8</text>
+<circle cx="321" cy="501" r="2" fill="#24292f"/>
+<rect x="328" y="496" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="510.6" fill="#24292f">11</text>
+<circle cx="357" cy="501" r="2" fill="#24292f"/>
+<rect x="364" y="496" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="510.6" fill="#24292f">13</text>
+<circle cx="393" cy="501" r="2" fill="#24292f"/>
+<rect x="400" y="496" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="510.6" fill="#24292f">16</text>
+<circle cx="429" cy="501" r="2" fill="#24292f"/>
+<rect x="436" y="496" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="510.6" fill="#24292f">18</text>
+<circle cx="465" cy="501" r="2" fill="#24292f"/>
+<rect x="472" y="496" width="35" height="21" fill="#95c5df"/>
+<text class="v" x="489.5" y="510.6" fill="#24292f">20</text>
+<circle cx="501" cy="501" r="2" fill="#24292f"/>
+<rect x="508" y="496" width="35" height="21" fill="#7cb8da"/>
+<text class="v" x="525.5" y="510.6" fill="#24292f">23</text>
+<circle cx="537" cy="501" r="2" fill="#24292f"/>
+<rect x="544" y="496" width="35" height="21" fill="#6cafd6"/>
+<text class="v" x="561.5" y="510.6" fill="#24292f">25</text>
+<circle cx="573" cy="501" r="2" fill="#24292f"/>
+<rect x="580" y="496" width="35" height="21" fill="#58a1cf"/>
+<text class="v" x="597.5" y="510.6" fill="#24292f">28</text>
+<circle cx="609" cy="501" r="2" fill="#24292f"/>
+<rect x="616" y="496" width="35" height="21" fill="#4b98ca"/>
+<text class="v" x="633.5" y="510.6" fill="#24292f">30</text>
+<circle cx="645" cy="501" r="2" fill="#24292f"/>
+<rect x="652" y="496" width="35" height="21" fill="#3a8ac2"/>
+<text class="v" x="669.5" y="510.6" fill="#ffffff">33</text>
+<circle cx="681" cy="501" r="2" fill="#ffffff"/>
+<rect x="688" y="496" width="35" height="21" fill="#3484bf"/>
+<text class="v" x="705.5" y="510.6" fill="#ffffff">34</text>
+<rect x="724" y="496" width="35" height="21" fill="#3484bf"/>
+<text class="v" x="741.5" y="510.6" fill="#ffffff">34</text>
+<rect x="760" y="496" width="35" height="21" fill="#3484bf"/>
+<text class="v" x="777.5" y="510.6" fill="#ffffff">34</text>
+<rect x="796" y="496" width="35" height="21" fill="#3484bf"/>
+<text class="v" x="813.5" y="510.6" fill="#ffffff">34</text>
+<rect x="832" y="496" width="35" height="21" fill="#3484bf"/>
+<text class="v" x="849.5" y="510.6" fill="#ffffff">34</text>
+<rect x="868" y="496" width="35" height="21" fill="#3484bf"/>
+<text class="v" x="885.5" y="510.6" fill="#ffffff">34</text>
+<rect x="904" y="496" width="35" height="21" fill="#3484bf"/>
+<text class="v" x="921.5" y="510.6" fill="#ffffff">34</text>
+<rect x="940" y="496" width="35" height="21" fill="#3484bf"/>
+<text class="v" x="957.5" y="510.6" fill="#ffffff">34</text>
+<text class="t" x="68" y="533.0" text-anchor="end">9</text>
+<rect x="76" y="518" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="532.6" fill="#24292f">0</text>
+<circle cx="105" cy="523" r="2" fill="#24292f"/>
+<rect x="112" y="518" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="532.6" fill="#24292f">1</text>
+<circle cx="141" cy="523" r="2" fill="#24292f"/>
+<rect x="148" y="518" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="532.6" fill="#24292f">3</text>
+<circle cx="177" cy="523" r="2" fill="#24292f"/>
+<rect x="184" y="518" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="532.6" fill="#24292f">1</text>
+<circle cx="213" cy="523" r="2" fill="#24292f"/>
+<rect x="220" y="518" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="532.6" fill="#24292f">3</text>
+<circle cx="249" cy="523" r="2" fill="#24292f"/>
+<rect x="256" y="518" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="532.6" fill="#24292f">6</text>
+<circle cx="285" cy="523" r="2" fill="#24292f"/>
+<rect x="292" y="518" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="532.6" fill="#24292f">8</text>
+<circle cx="321" cy="523" r="2" fill="#24292f"/>
+<rect x="328" y="518" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="532.6" fill="#24292f">11</text>
+<circle cx="357" cy="523" r="2" fill="#24292f"/>
+<rect x="364" y="518" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="532.6" fill="#24292f">13</text>
+<circle cx="393" cy="523" r="2" fill="#24292f"/>
+<rect x="400" y="518" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="532.6" fill="#24292f">16</text>
+<circle cx="429" cy="523" r="2" fill="#24292f"/>
+<rect x="436" y="518" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="532.6" fill="#24292f">18</text>
+<circle cx="465" cy="523" r="2" fill="#24292f"/>
+<rect x="472" y="518" width="35" height="21" fill="#95c5df"/>
+<text class="v" x="489.5" y="532.6" fill="#24292f">20</text>
+<circle cx="501" cy="523" r="2" fill="#24292f"/>
+<rect x="508" y="518" width="35" height="21" fill="#7cb8da"/>
+<text class="v" x="525.5" y="532.6" fill="#24292f">23</text>
+<circle cx="537" cy="523" r="2" fill="#24292f"/>
+<rect x="544" y="518" width="35" height="21" fill="#6cafd6"/>
+<text class="v" x="561.5" y="532.6" fill="#24292f">25</text>
+<circle cx="573" cy="523" r="2" fill="#24292f"/>
+<rect x="580" y="518" width="35" height="21" fill="#58a1cf"/>
+<text class="v" x="597.5" y="532.6" fill="#24292f">28</text>
+<circle cx="609" cy="523" r="2" fill="#24292f"/>
+<rect x="616" y="518" width="35" height="21" fill="#4b98ca"/>
+<text class="v" x="633.5" y="532.6" fill="#24292f">30</text>
+<circle cx="645" cy="523" r="2" fill="#24292f"/>
+<rect x="652" y="518" width="35" height="21" fill="#3a8ac2"/>
+<text class="v" x="669.5" y="532.6" fill="#ffffff">33</text>
+<circle cx="681" cy="523" r="2" fill="#ffffff"/>
+<rect x="688" y="518" width="35" height="21" fill="#2f7fbc"/>
+<text class="v" x="705.5" y="532.6" fill="#ffffff">35</text>
+<circle cx="717" cy="523" r="2" fill="#ffffff"/>
+<rect x="724" y="518" width="35" height="21" fill="#2575b7"/>
+<text class="v" x="741.5" y="532.6" fill="#ffffff">37</text>
+<rect x="760" y="518" width="35" height="21" fill="#2575b7"/>
+<text class="v" x="777.5" y="532.6" fill="#ffffff">37</text>
+<rect x="796" y="518" width="35" height="21" fill="#2575b7"/>
+<text class="v" x="813.5" y="532.6" fill="#ffffff">37</text>
+<rect x="832" y="518" width="35" height="21" fill="#2575b7"/>
+<text class="v" x="849.5" y="532.6" fill="#ffffff">37</text>
+<rect x="868" y="518" width="35" height="21" fill="#2575b7"/>
+<text class="v" x="885.5" y="532.6" fill="#ffffff">37</text>
+<rect x="904" y="518" width="35" height="21" fill="#2575b7"/>
+<text class="v" x="921.5" y="532.6" fill="#ffffff">37</text>
+<rect x="940" y="518" width="35" height="21" fill="#2575b7"/>
+<text class="v" x="957.5" y="532.6" fill="#ffffff">37</text>
+<text class="t" x="68" y="555.0" text-anchor="end">8</text>
+<rect x="76" y="540" width="35" height="21" fill="#f7fbff"/>
+<text class="v" x="93.5" y="554.6" fill="#24292f">0</text>
+<circle cx="105" cy="545" r="2" fill="#24292f"/>
+<rect x="112" y="540" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="129.5" y="554.6" fill="#24292f">1</text>
+<circle cx="141" cy="545" r="2" fill="#24292f"/>
+<rect x="148" y="540" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="165.5" y="554.6" fill="#24292f">3</text>
+<circle cx="177" cy="545" r="2" fill="#24292f"/>
+<rect x="184" y="540" width="35" height="21" fill="#f3f8fe"/>
+<text class="v" x="201.5" y="554.6" fill="#24292f">1</text>
+<circle cx="213" cy="545" r="2" fill="#24292f"/>
+<rect x="220" y="540" width="35" height="21" fill="#ebf3fb"/>
+<text class="v" x="237.5" y="554.6" fill="#24292f">3</text>
+<circle cx="249" cy="545" r="2" fill="#24292f"/>
+<rect x="256" y="540" width="35" height="21" fill="#dfecf7"/>
+<text class="v" x="273.5" y="554.6" fill="#24292f">6</text>
+<circle cx="285" cy="545" r="2" fill="#24292f"/>
+<rect x="292" y="540" width="35" height="21" fill="#d7e7f5"/>
+<text class="v" x="309.5" y="554.6" fill="#24292f">8</text>
+<circle cx="321" cy="545" r="2" fill="#24292f"/>
+<rect x="328" y="540" width="35" height="21" fill="#ccdff1"/>
+<text class="v" x="345.5" y="554.6" fill="#24292f">11</text>
+<circle cx="357" cy="545" r="2" fill="#24292f"/>
+<rect x="364" y="540" width="35" height="21" fill="#c3daee"/>
+<text class="v" x="381.5" y="554.6" fill="#24292f">13</text>
+<circle cx="393" cy="545" r="2" fill="#24292f"/>
+<rect x="400" y="540" width="35" height="21" fill="#b0d2e7"/>
+<text class="v" x="417.5" y="554.6" fill="#24292f">16</text>
+<circle cx="429" cy="545" r="2" fill="#24292f"/>
+<rect x="436" y="540" width="35" height="21" fill="#a3cce3"/>
+<text class="v" x="453.5" y="554.6" fill="#24292f">18</text>
+<circle cx="465" cy="545" r="2" fill="#24292f"/>
+<rect x="472" y="540" width="35" height="21" fill="#95c5df"/>
+<text class="v" x="489.5" y="554.6" fill="#24292f">20</text>
+<circle cx="501" cy="545" r="2" fill="#24292f"/>
+<rect x="508" y="540" width="35" height="21" fill="#7cb8da"/>
+<text class="v" x="525.5" y="554.6" fill="#24292f">23</text>
+<circle cx="537" cy="545" r="2" fill="#24292f"/>
+<rect x="544" y="540" width="35" height="21" fill="#6cafd6"/>
+<text class="v" x="561.5" y="554.6" fill="#24292f">25</text>
+<circle cx="573" cy="545" r="2" fill="#24292f"/>
+<rect x="580" y="540" width="35" height="21" fill="#58a1cf"/>
+<text class="v" x="597.5" y="554.6" fill="#24292f">28</text>
+<circle cx="609" cy="545" r="2" fill="#24292f"/>
+<rect x="616" y="540" width="35" height="21" fill="#4b98ca"/>
+<text class="v" x="633.5" y="554.6" fill="#24292f">30</text>
+<circle cx="645" cy="545" r="2" fill="#24292f"/>
+<rect x="652" y="540" width="35" height="21" fill="#3a8ac2"/>
+<text class="v" x="669.5" y="554.6" fill="#ffffff">33</text>
+<circle cx="681" cy="545" r="2" fill="#ffffff"/>
+<rect x="688" y="540" width="35" height="21" fill="#2f7fbc"/>
+<text class="v" x="705.5" y="554.6" fill="#ffffff">35</text>
+<circle cx="717" cy="545" r="2" fill="#ffffff"/>
+<rect x="724" y="540" width="35" height="21" fill="#2575b7"/>
+<text class="v" x="741.5" y="554.6" fill="#ffffff">37</text>
+<circle cx="753" cy="545" r="2" fill="#ffffff"/>
+<rect x="760" y="540" width="35" height="21" fill="#1c68ae"/>
+<text class="v" x="777.5" y="554.6" fill="#ffffff">39</text>
+<rect x="796" y="540" width="35" height="21" fill="#1c68ae"/>
+<text class="v" x="813.5" y="554.6" fill="#ffffff">39</text>
+<rect x="832" y="540" width="35" height="21" fill="#1c68ae"/>
+<text class="v" x="849.5" y="554.6" fill="#ffffff">39</text>
+<rect x="868" y="540" width="35" height="21" fill="#1c68ae"/>
+<text class="v" x="885.5" y="554.6" fill="#ffffff">39</text>
+<rect x="904" y="540" width="35" height="21" fill="#1c68ae"/>
+<text class="v" x="921.5" y="554.6" fill="#ffffff">39</text>
+<rect x="940" y="540" width="35" height="21" fill="#1c68ae"/>
+<text class="v" x="957.5" y="554.6" fill="#ffffff">39</text>
+<text class="t" x="93.5" y="578" text-anchor="middle">8</text>
+<text class="t" x="129.5" y="578" text-anchor="middle">9</text>
+<text class="t" x="165.5" y="578" text-anchor="middle">10</text>
+<text class="t" x="201.5" y="578" text-anchor="middle">11</text>
+<text class="t" x="237.5" y="578" text-anchor="middle">12</text>
+<text class="t" x="273.5" y="578" text-anchor="middle">13</text>
+<text class="t" x="309.5" y="578" text-anchor="middle">14</text>
+<text class="t" x="345.5" y="578" text-anchor="middle">15</text>
+<text class="t" x="381.5" y="578" text-anchor="middle">16</text>
+<text class="t" x="417.5" y="578" text-anchor="middle">17</text>
+<text class="t" x="453.5" y="578" text-anchor="middle">18</text>
+<text class="t" x="489.5" y="578" text-anchor="middle">19</text>
+<text class="t" x="525.5" y="578" text-anchor="middle">20</text>
+<text class="t" x="561.5" y="578" text-anchor="middle">21</text>
+<text class="t" x="597.5" y="578" text-anchor="middle">22</text>
+<text class="t" x="633.5" y="578" text-anchor="middle">23</text>
+<text class="t" x="669.5" y="578" text-anchor="middle">24</text>
+<text class="t" x="705.5" y="578" text-anchor="middle">25</text>
+<text class="t" x="741.5" y="578" text-anchor="middle">26</text>
+<text class="t" x="777.5" y="578" text-anchor="middle">27</text>
+<text class="t" x="813.5" y="578" text-anchor="middle">28</text>
+<text class="t" x="849.5" y="578" text-anchor="middle">29</text>
+<text class="t" x="885.5" y="578" text-anchor="middle">30</text>
+<text class="t" x="921.5" y="578" text-anchor="middle">31</text>
+<text class="t" x="957.5" y="578" text-anchor="middle">32</text>
+<text class="a" x="526.0" y="606">显存(GiB)</text>
+<text class="a" x="18" y="287.0" transform="rotate(-90 18 287.0)">内存(GiB)</text>
+</svg>
diff --git a/docs/assets/capacity-speed.en.svg b/docs/assets/capacity-speed.en.svg
new file mode 100644
index 0000000..c435310
--- /dev/null
+++ b/docs/assets/capacity-speed.en.svg
@@ -0,0 +1,1306 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="988" height="620" viewBox="0 0 988 620">
+<style>text{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI','Noto Sans','PingFang SC','Microsoft YaHei','Noto Sans CJK SC',Helvetica,Arial,sans-serif}.v{font-size:10.5px;text-anchor:middle}.t{font-size:11px;fill:#57606a}.a{font-size:12.5px;fill:#24292f;text-anchor:middle}</style>
+<rect width="988" height="620" fill="#ffffff"/>
+<text class="t" x="68" y="27.0" text-anchor="end">32</text>
+<rect x="76" y="12" width="35" height="21" fill="#ec7014"/>
+<text class="v" x="93.5" y="26.6" fill="#24292f">12.7</text>
+<rect x="112" y="12" width="35" height="21" fill="#fed471"/>
+<text class="v" x="129.5" y="26.6" fill="#24292f">10.1</text>
+<rect x="148" y="12" width="35" height="21" fill="#fed675"/>
+<text class="v" x="165.5" y="26.6" fill="#24292f">10.1</text>
+<rect x="184" y="12" width="35" height="21" fill="#ffefaa"/>
+<text class="v" x="201.5" y="26.6" fill="#24292f">9.21</text>
+<rect x="220" y="12" width="35" height="21" fill="#ffeea9"/>
+<text class="v" x="237.5" y="26.6" fill="#24292f">9.23</text>
+<rect x="256" y="12" width="35" height="21" fill="#ffeea8"/>
+<text class="v" x="273.5" y="26.6" fill="#24292f">9.25</text>
+<rect x="292" y="12" width="35" height="21" fill="#ffeea8"/>
+<text class="v" x="309.5" y="26.6" fill="#24292f">9.24</text>
+<rect x="328" y="12" width="35" height="21" fill="#fff6bb"/>
+<text class="v" x="345.5" y="26.6" fill="#24292f">8.90</text>
+<rect x="364" y="12" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="381.5" y="26.6" fill="#24292f">9.02</text>
+<rect x="400" y="12" width="35" height="21" fill="#fff1b0"/>
+<text class="v" x="417.5" y="26.6" fill="#24292f">9.10</text>
+<rect x="436" y="12" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="453.5" y="26.6" fill="#24292f">9.00</text>
+<rect x="472" y="12" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="489.5" y="26.6" fill="#24292f">9.08</text>
+<rect x="508" y="12" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="525.5" y="26.6" fill="#24292f">9.00</text>
+<rect x="544" y="12" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="561.5" y="26.6" fill="#24292f">8.95</text>
+<rect x="580" y="12" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="597.5" y="26.6" fill="#24292f">8.96</text>
+<rect x="616" y="12" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="633.5" y="26.6" fill="#24292f">8.95</text>
+<rect x="652" y="12" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="669.5" y="26.6" fill="#24292f">9.01</text>
+<rect x="688" y="12" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="705.5" y="26.6" fill="#24292f">8.97</text>
+<rect x="724" y="12" width="35" height="21" fill="#fff7bb"/>
+<text class="v" x="741.5" y="26.6" fill="#24292f">8.89</text>
+<rect x="760" y="12" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="777.5" y="26.6" fill="#24292f">9.01</text>
+<rect x="796" y="12" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="813.5" y="26.6" fill="#24292f">9.01</text>
+<rect x="832" y="12" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="849.5" y="26.6" fill="#24292f">9.02</text>
+<rect x="868" y="12" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="885.5" y="26.6" fill="#24292f">8.94</text>
+<rect x="904" y="12" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="921.5" y="26.6" fill="#24292f">8.98</text>
+<rect x="940" y="12" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="957.5" y="26.6" fill="#24292f">8.93</text>
+<text class="t" x="68" y="49.0" text-anchor="end">31</text>
+<rect x="76" y="34" width="35" height="21" fill="#e66a11"/>
+<text class="v" x="93.5" y="48.6" fill="#ffffff">12.9</text>
+<rect x="112" y="34" width="35" height="21" fill="#fed676"/>
+<text class="v" x="129.5" y="48.6" fill="#24292f">10.1</text>
+<rect x="148" y="34" width="35" height="21" fill="#fed674"/>
+<text class="v" x="165.5" y="48.6" fill="#24292f">10.1</text>
+<rect x="184" y="34" width="35" height="21" fill="#fff0ac"/>
+<text class="v" x="201.5" y="48.6" fill="#24292f">9.17</text>
+<rect x="220" y="34" width="35" height="21" fill="#feeca4"/>
+<text class="v" x="237.5" y="48.6" fill="#24292f">9.32</text>
+<rect x="256" y="34" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="273.5" y="48.6" fill="#24292f">9.15</text>
+<rect x="292" y="34" width="35" height="21" fill="#ffefaa"/>
+<text class="v" x="309.5" y="48.6" fill="#24292f">9.21</text>
+<rect x="328" y="34" width="35" height="21" fill="#fff1b0"/>
+<text class="v" x="345.5" y="48.6" fill="#24292f">9.09</text>
+<rect x="364" y="34" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="381.5" y="48.6" fill="#24292f">8.95</text>
+<rect x="400" y="34" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="417.5" y="48.6" fill="#24292f">8.93</text>
+<rect x="436" y="34" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="453.5" y="48.6" fill="#24292f">8.96</text>
+<rect x="472" y="34" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="489.5" y="48.6" fill="#24292f">9.09</text>
+<rect x="508" y="34" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="525.5" y="48.6" fill="#24292f">9.04</text>
+<rect x="544" y="34" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="561.5" y="48.6" fill="#24292f">8.91</text>
+<rect x="580" y="34" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="597.5" y="48.6" fill="#24292f">8.94</text>
+<rect x="616" y="34" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="633.5" y="48.6" fill="#24292f">8.94</text>
+<rect x="652" y="34" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="669.5" y="48.6" fill="#24292f">8.94</text>
+<rect x="688" y="34" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="705.5" y="48.6" fill="#24292f">8.97</text>
+<rect x="724" y="34" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="741.5" y="48.6" fill="#24292f">9.01</text>
+<rect x="760" y="34" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="777.5" y="48.6" fill="#24292f">9.08</text>
+<rect x="796" y="34" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="813.5" y="48.6" fill="#24292f">9.02</text>
+<rect x="832" y="34" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="849.5" y="48.6" fill="#24292f">8.98</text>
+<rect x="868" y="34" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="885.5" y="48.6" fill="#24292f">9.06</text>
+<rect x="904" y="34" width="35" height="21" fill="#fff3b2"/>
+<text class="v" x="921.5" y="48.6" fill="#24292f">9.05</text>
+<rect x="940" y="34" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="957.5" y="48.6" fill="#24292f">9.00</text>
+<text class="t" x="68" y="71.0" text-anchor="end">30</text>
+<rect x="76" y="56" width="35" height="21" fill="#ed7215"/>
+<text class="v" x="93.5" y="70.6" fill="#24292f">12.6</text>
+<rect x="112" y="56" width="35" height="21" fill="#fed069"/>
+<text class="v" x="129.5" y="70.6" fill="#24292f">10.2</text>
+<rect x="148" y="56" width="35" height="21" fill="#fed675"/>
+<text class="v" x="165.5" y="70.6" fill="#24292f">10.1</text>
+<rect x="184" y="56" width="35" height="21" fill="#ffeea9"/>
+<text class="v" x="201.5" y="70.6" fill="#24292f">9.24</text>
+<rect x="220" y="56" width="35" height="21" fill="#ffefaa"/>
+<text class="v" x="237.5" y="70.6" fill="#24292f">9.21</text>
+<rect x="256" y="56" width="35" height="21" fill="#ffefaa"/>
+<text class="v" x="273.5" y="70.6" fill="#24292f">9.21</text>
+<rect x="292" y="56" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="309.5" y="70.6" fill="#24292f">9.16</text>
+<rect x="328" y="56" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="345.5" y="70.6" fill="#24292f">8.97</text>
+<rect x="364" y="56" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="381.5" y="70.6" fill="#24292f">9.08</text>
+<rect x="400" y="56" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="417.5" y="70.6" fill="#24292f">8.92</text>
+<rect x="436" y="56" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="453.5" y="70.6" fill="#24292f">8.93</text>
+<rect x="472" y="56" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="489.5" y="70.6" fill="#24292f">8.95</text>
+<rect x="508" y="56" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="525.5" y="70.6" fill="#24292f">9.01</text>
+<rect x="544" y="56" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="561.5" y="70.6" fill="#24292f">8.97</text>
+<rect x="580" y="56" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="597.5" y="70.6" fill="#24292f">9.02</text>
+<rect x="616" y="56" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="633.5" y="70.6" fill="#24292f">9.04</text>
+<rect x="652" y="56" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="669.5" y="70.6" fill="#24292f">8.96</text>
+<rect x="688" y="56" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="705.5" y="70.6" fill="#24292f">8.97</text>
+<rect x="724" y="56" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="741.5" y="70.6" fill="#24292f">9.02</text>
+<rect x="760" y="56" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="777.5" y="70.6" fill="#24292f">8.98</text>
+<rect x="796" y="56" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="813.5" y="70.6" fill="#24292f">8.94</text>
+<rect x="832" y="56" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="849.5" y="70.6" fill="#24292f">9.07</text>
+<rect x="868" y="56" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="885.5" y="70.6" fill="#24292f">8.92</text>
+<rect x="904" y="56" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="921.5" y="70.6" fill="#24292f">9.04</text>
+<rect x="940" y="56" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="957.5" y="70.6" fill="#24292f">9.06</text>
+<text class="t" x="68" y="93.0" text-anchor="end">29</text>
+<rect x="76" y="78" width="35" height="21" fill="#e86c12"/>
+<text class="v" x="93.5" y="92.6" fill="#24292f">12.8</text>
+<rect x="112" y="78" width="35" height="21" fill="#fed574"/>
+<text class="v" x="129.5" y="92.6" fill="#24292f">10.1</text>
+<rect x="148" y="78" width="35" height="21" fill="#fed676"/>
+<text class="v" x="165.5" y="92.6" fill="#24292f">10.1</text>
+<rect x="184" y="78" width="35" height="21" fill="#feeda5"/>
+<text class="v" x="201.5" y="92.6" fill="#24292f">9.30</text>
+<rect x="220" y="78" width="35" height="21" fill="#ffefab"/>
+<text class="v" x="237.5" y="92.6" fill="#24292f">9.20</text>
+<rect x="256" y="78" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="273.5" y="92.6" fill="#24292f">9.15</text>
+<rect x="292" y="78" width="35" height="21" fill="#fff2b0"/>
+<text class="v" x="309.5" y="92.6" fill="#24292f">9.09</text>
+<rect x="328" y="78" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="345.5" y="92.6" fill="#24292f">8.99</text>
+<rect x="364" y="78" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="381.5" y="92.6" fill="#24292f">8.91</text>
+<rect x="400" y="78" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="417.5" y="92.6" fill="#24292f">8.96</text>
+<rect x="436" y="78" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="453.5" y="92.6" fill="#24292f">8.94</text>
+<rect x="472" y="78" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="489.5" y="92.6" fill="#24292f">8.94</text>
+<rect x="508" y="78" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="525.5" y="92.6" fill="#24292f">9.08</text>
+<rect x="544" y="78" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="561.5" y="92.6" fill="#24292f">8.91</text>
+<rect x="580" y="78" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="597.5" y="92.6" fill="#24292f">8.97</text>
+<rect x="616" y="78" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="633.5" y="92.6" fill="#24292f">8.97</text>
+<rect x="652" y="78" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="669.5" y="92.6" fill="#24292f">9.03</text>
+<rect x="688" y="78" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="705.5" y="92.6" fill="#24292f">8.97</text>
+<rect x="724" y="78" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="741.5" y="92.6" fill="#24292f">9.04</text>
+<rect x="760" y="78" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="777.5" y="92.6" fill="#24292f">8.97</text>
+<rect x="796" y="78" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="813.5" y="92.6" fill="#24292f">8.97</text>
+<rect x="832" y="78" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="849.5" y="92.6" fill="#24292f">8.97</text>
+<rect x="868" y="78" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="885.5" y="92.6" fill="#24292f">9.00</text>
+<rect x="904" y="78" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="921.5" y="92.6" fill="#24292f">9.06</text>
+<rect x="940" y="78" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="957.5" y="92.6" fill="#24292f">8.95</text>
+<text class="t" x="68" y="115.0" text-anchor="end">28</text>
+<rect x="76" y="100" width="35" height="21" fill="#de610c"/>
+<text class="v" x="93.5" y="114.6" fill="#ffffff">13.2</text>
+<rect x="112" y="100" width="35" height="21" fill="#fed068"/>
+<text class="v" x="129.5" y="114.6" fill="#24292f">10.3</text>
+<rect x="148" y="100" width="35" height="21" fill="#fed573"/>
+<text class="v" x="165.5" y="114.6" fill="#24292f">10.1</text>
+<rect x="184" y="100" width="35" height="21" fill="#feeda6"/>
+<text class="v" x="201.5" y="114.6" fill="#24292f">9.28</text>
+<rect x="220" y="100" width="35" height="21" fill="#ffefaa"/>
+<text class="v" x="237.5" y="114.6" fill="#24292f">9.21</text>
+<rect x="256" y="100" width="35" height="21" fill="#ffeea8"/>
+<text class="v" x="273.5" y="114.6" fill="#24292f">9.24</text>
+<rect x="292" y="100" width="35" height="21" fill="#ffeea9"/>
+<text class="v" x="309.5" y="114.6" fill="#24292f">9.24</text>
+<rect x="328" y="100" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="345.5" y="114.6" fill="#24292f">9.15</text>
+<rect x="364" y="100" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="381.5" y="114.6" fill="#24292f">8.94</text>
+<rect x="400" y="100" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="417.5" y="114.6" fill="#24292f">9.02</text>
+<rect x="436" y="100" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="453.5" y="114.6" fill="#24292f">9.01</text>
+<rect x="472" y="100" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="489.5" y="114.6" fill="#24292f">8.96</text>
+<rect x="508" y="100" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="525.5" y="114.6" fill="#24292f">8.92</text>
+<rect x="544" y="100" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="561.5" y="114.6" fill="#24292f">9.02</text>
+<rect x="580" y="100" width="35" height="21" fill="#fff4b4"/>
+<text class="v" x="597.5" y="114.6" fill="#24292f">9.01</text>
+<rect x="616" y="100" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="633.5" y="114.6" fill="#24292f">8.99</text>
+<rect x="652" y="100" width="35" height="21" fill="#fff4b4"/>
+<text class="v" x="669.5" y="114.6" fill="#24292f">9.01</text>
+<rect x="688" y="100" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="705.5" y="114.6" fill="#24292f">8.97</text>
+<rect x="724" y="100" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="741.5" y="114.6" fill="#24292f">9.09</text>
+<rect x="760" y="100" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="777.5" y="114.6" fill="#24292f">9.03</text>
+<rect x="796" y="100" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="813.5" y="114.6" fill="#24292f">9.02</text>
+<rect x="832" y="100" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="849.5" y="114.6" fill="#24292f">8.93</text>
+<rect x="868" y="100" width="35" height="21" fill="#fff6bb"/>
+<text class="v" x="885.5" y="114.6" fill="#24292f">8.89</text>
+<rect x="904" y="100" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="921.5" y="114.6" fill="#24292f">9.00</text>
+<rect x="940" y="100" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="957.5" y="114.6" fill="#24292f">8.93</text>
+<text class="t" x="68" y="137.0" text-anchor="end">27</text>
+<rect x="76" y="122" width="35" height="21" fill="#e96d13"/>
+<text class="v" x="93.5" y="136.6" fill="#24292f">12.8</text>
+<rect x="112" y="122" width="35" height="21" fill="#fed26d"/>
+<text class="v" x="129.5" y="136.6" fill="#24292f">10.2</text>
+<rect x="148" y="122" width="35" height="21" fill="#fed675"/>
+<text class="v" x="165.5" y="136.6" fill="#24292f">10.1</text>
+<rect x="184" y="122" width="35" height="21" fill="#ffeda7"/>
+<text class="v" x="201.5" y="136.6" fill="#24292f">9.26</text>
+<rect x="220" y="122" width="35" height="21" fill="#feeda6"/>
+<text class="v" x="237.5" y="136.6" fill="#24292f">9.28</text>
+<rect x="256" y="122" width="35" height="21" fill="#ffeea9"/>
+<text class="v" x="273.5" y="136.6" fill="#24292f">9.22</text>
+<rect x="292" y="122" width="35" height="21" fill="#ffefab"/>
+<text class="v" x="309.5" y="136.6" fill="#24292f">9.18</text>
+<rect x="328" y="122" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="345.5" y="136.6" fill="#24292f">9.00</text>
+<rect x="364" y="122" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="381.5" y="136.6" fill="#24292f">8.99</text>
+<rect x="400" y="122" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="417.5" y="136.6" fill="#24292f">8.94</text>
+<rect x="436" y="122" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="453.5" y="136.6" fill="#24292f">8.96</text>
+<rect x="472" y="122" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="489.5" y="136.6" fill="#24292f">9.02</text>
+<rect x="508" y="122" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="525.5" y="136.6" fill="#24292f">8.99</text>
+<rect x="544" y="122" width="35" height="21" fill="#fff2b0"/>
+<text class="v" x="561.5" y="136.6" fill="#24292f">9.09</text>
+<rect x="580" y="122" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="597.5" y="136.6" fill="#24292f">8.99</text>
+<rect x="616" y="122" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="633.5" y="136.6" fill="#24292f">8.97</text>
+<rect x="652" y="122" width="35" height="21" fill="#fff1b0"/>
+<text class="v" x="669.5" y="136.6" fill="#24292f">9.10</text>
+<rect x="688" y="122" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="705.5" y="136.6" fill="#24292f">9.03</text>
+<rect x="724" y="122" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="741.5" y="136.6" fill="#24292f">8.95</text>
+<rect x="760" y="122" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="777.5" y="136.6" fill="#24292f">8.99</text>
+<rect x="796" y="122" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="813.5" y="136.6" fill="#24292f">9.04</text>
+<rect x="832" y="122" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="849.5" y="136.6" fill="#24292f">9.01</text>
+<rect x="868" y="122" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="885.5" y="136.6" fill="#24292f">9.00</text>
+<rect x="904" y="122" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="921.5" y="136.6" fill="#24292f">9.03</text>
+<rect x="940" y="122" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="957.5" y="136.6" fill="#24292f">9.04</text>
+<text class="t" x="68" y="159.0" text-anchor="end">26</text>
+<rect x="76" y="144" width="35" height="21" fill="#e76a11"/>
+<text class="v" x="93.5" y="158.6" fill="#ffffff">12.9</text>
+<rect x="112" y="144" width="35" height="21" fill="#fed069"/>
+<text class="v" x="129.5" y="158.6" fill="#24292f">10.2</text>
+<rect x="148" y="144" width="35" height="21" fill="#fed26d"/>
+<text class="v" x="165.5" y="158.6" fill="#24292f">10.2</text>
+<rect x="184" y="144" width="35" height="21" fill="#ffefab"/>
+<text class="v" x="201.5" y="158.6" fill="#24292f">9.19</text>
+<rect x="220" y="144" width="35" height="21" fill="#ffefac"/>
+<text class="v" x="237.5" y="158.6" fill="#24292f">9.18</text>
+<rect x="256" y="144" width="35" height="21" fill="#ffefac"/>
+<text class="v" x="273.5" y="158.6" fill="#24292f">9.18</text>
+<rect x="292" y="144" width="35" height="21" fill="#fff0ae"/>
+<text class="v" x="309.5" y="158.6" fill="#24292f">9.13</text>
+<rect x="328" y="144" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="345.5" y="158.6" fill="#24292f">8.94</text>
+<rect x="364" y="144" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="381.5" y="158.6" fill="#24292f">8.97</text>
+<rect x="400" y="144" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="417.5" y="158.6" fill="#24292f">8.96</text>
+<rect x="436" y="144" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="453.5" y="158.6" fill="#24292f">8.97</text>
+<rect x="472" y="144" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="489.5" y="158.6" fill="#24292f">8.93</text>
+<rect x="508" y="144" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="525.5" y="158.6" fill="#24292f">8.93</text>
+<rect x="544" y="144" width="35" height="21" fill="#fff2b0"/>
+<text class="v" x="561.5" y="158.6" fill="#24292f">9.09</text>
+<rect x="580" y="144" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="597.5" y="158.6" fill="#24292f">8.95</text>
+<rect x="616" y="144" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="633.5" y="158.6" fill="#24292f">9.01</text>
+<rect x="652" y="144" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="669.5" y="158.6" fill="#24292f">8.92</text>
+<rect x="688" y="144" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="705.5" y="158.6" fill="#24292f">9.03</text>
+<rect x="724" y="144" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="741.5" y="158.6" fill="#24292f">8.95</text>
+<rect x="760" y="144" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="777.5" y="158.6" fill="#24292f">8.98</text>
+<rect x="796" y="144" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="813.5" y="158.6" fill="#24292f">9.06</text>
+<rect x="832" y="144" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="849.5" y="158.6" fill="#24292f">8.91</text>
+<rect x="868" y="144" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="885.5" y="158.6" fill="#24292f">9.02</text>
+<rect x="904" y="144" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="921.5" y="158.6" fill="#24292f">9.04</text>
+<rect x="940" y="144" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="957.5" y="158.6" fill="#24292f">8.90</text>
+<text class="t" x="68" y="181.0" text-anchor="end">25</text>
+<rect x="76" y="166" width="35" height="21" fill="#ec7014"/>
+<text class="v" x="93.5" y="180.6" fill="#24292f">12.7</text>
+<rect x="112" y="166" width="35" height="21" fill="#fed16a"/>
+<text class="v" x="129.5" y="180.6" fill="#24292f">10.2</text>
+<rect x="148" y="166" width="35" height="21" fill="#fed26e"/>
+<text class="v" x="165.5" y="180.6" fill="#24292f">10.2</text>
+<rect x="184" y="166" width="35" height="21" fill="#feeca4"/>
+<text class="v" x="201.5" y="180.6" fill="#24292f">9.34</text>
+<rect x="220" y="166" width="35" height="21" fill="#ffeea8"/>
+<text class="v" x="237.5" y="180.6" fill="#24292f">9.25</text>
+<rect x="256" y="166" width="35" height="21" fill="#fff1af"/>
+<text class="v" x="273.5" y="180.6" fill="#24292f">9.12</text>
+<rect x="292" y="166" width="35" height="21" fill="#feeda6"/>
+<text class="v" x="309.5" y="180.6" fill="#24292f">9.28</text>
+<rect x="328" y="166" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="345.5" y="180.6" fill="#24292f">8.95</text>
+<rect x="364" y="166" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="381.5" y="180.6" fill="#24292f">8.93</text>
+<rect x="400" y="166" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="417.5" y="180.6" fill="#24292f">8.94</text>
+<rect x="436" y="166" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="453.5" y="180.6" fill="#24292f">8.93</text>
+<rect x="472" y="166" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="489.5" y="180.6" fill="#24292f">8.97</text>
+<rect x="508" y="166" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="525.5" y="180.6" fill="#24292f">8.98</text>
+<rect x="544" y="166" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="561.5" y="180.6" fill="#24292f">8.94</text>
+<rect x="580" y="166" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="597.5" y="180.6" fill="#24292f">8.96</text>
+<rect x="616" y="166" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="633.5" y="180.6" fill="#24292f">9.02</text>
+<rect x="652" y="166" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="669.5" y="180.6" fill="#24292f">8.96</text>
+<rect x="688" y="166" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="705.5" y="180.6" fill="#24292f">9.08</text>
+<rect x="724" y="166" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="741.5" y="180.6" fill="#24292f">9.04</text>
+<rect x="760" y="166" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="777.5" y="180.6" fill="#24292f">9.03</text>
+<rect x="796" y="166" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="813.5" y="180.6" fill="#24292f">9.01</text>
+<rect x="832" y="166" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="849.5" y="180.6" fill="#24292f">9.03</text>
+<rect x="868" y="166" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="885.5" y="180.6" fill="#24292f">9.03</text>
+<rect x="904" y="166" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="921.5" y="180.6" fill="#24292f">9.03</text>
+<rect x="940" y="166" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="957.5" y="180.6" fill="#24292f">8.94</text>
+<text class="t" x="68" y="203.0" text-anchor="end">24</text>
+<rect x="76" y="188" width="35" height="21" fill="#ec7114"/>
+<text class="v" x="93.5" y="202.6" fill="#24292f">12.6</text>
+<rect x="112" y="188" width="35" height="21" fill="#fed573"/>
+<text class="v" x="129.5" y="202.6" fill="#24292f">10.1</text>
+<rect x="148" y="188" width="35" height="21" fill="#fed675"/>
+<text class="v" x="165.5" y="202.6" fill="#24292f">10.1</text>
+<rect x="184" y="188" width="35" height="21" fill="#feeca5"/>
+<text class="v" x="201.5" y="202.6" fill="#24292f">9.31</text>
+<rect x="220" y="188" width="35" height="21" fill="#ffefac"/>
+<text class="v" x="237.5" y="202.6" fill="#24292f">9.18</text>
+<rect x="256" y="188" width="35" height="21" fill="#ffeea8"/>
+<text class="v" x="273.5" y="202.6" fill="#24292f">9.25</text>
+<rect x="292" y="188" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="309.5" y="202.6" fill="#24292f">9.15</text>
+<rect x="328" y="188" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="345.5" y="202.6" fill="#24292f">8.94</text>
+<rect x="364" y="188" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="381.5" y="202.6" fill="#24292f">8.92</text>
+<rect x="400" y="188" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="417.5" y="202.6" fill="#24292f">9.03</text>
+<rect x="436" y="188" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="453.5" y="202.6" fill="#24292f">9.01</text>
+<rect x="472" y="188" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="489.5" y="202.6" fill="#24292f">8.92</text>
+<rect x="508" y="188" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="525.5" y="202.6" fill="#24292f">8.94</text>
+<rect x="544" y="188" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="561.5" y="202.6" fill="#24292f">9.01</text>
+<rect x="580" y="188" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="597.5" y="202.6" fill="#24292f">9.03</text>
+<rect x="616" y="188" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="633.5" y="202.6" fill="#24292f">8.98</text>
+<rect x="652" y="188" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="669.5" y="202.6" fill="#24292f">8.95</text>
+<rect x="688" y="188" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="705.5" y="202.6" fill="#24292f">8.92</text>
+<rect x="724" y="188" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="741.5" y="202.6" fill="#24292f">8.97</text>
+<rect x="760" y="188" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="777.5" y="202.6" fill="#24292f">9.02</text>
+<rect x="796" y="188" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="813.5" y="202.6" fill="#24292f">8.97</text>
+<rect x="832" y="188" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="849.5" y="202.6" fill="#24292f">9.02</text>
+<rect x="868" y="188" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="885.5" y="202.6" fill="#24292f">8.98</text>
+<rect x="904" y="188" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="921.5" y="202.6" fill="#24292f">8.99</text>
+<rect x="940" y="188" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="957.5" y="202.6" fill="#24292f">8.99</text>
+<text class="t" x="68" y="225.0" text-anchor="end">23</text>
+<rect x="76" y="210" width="35" height="21" fill="#e56810"/>
+<text class="v" x="93.5" y="224.6" fill="#ffffff">12.9</text>
+<rect x="112" y="210" width="35" height="21" fill="#fece64"/>
+<text class="v" x="129.5" y="224.6" fill="#24292f">10.3</text>
+<rect x="148" y="210" width="35" height="21" fill="#fecc61"/>
+<text class="v" x="165.5" y="224.6" fill="#24292f">10.4</text>
+<rect x="184" y="210" width="35" height="21" fill="#fee89d"/>
+<text class="v" x="201.5" y="224.6" fill="#24292f">9.47</text>
+<rect x="220" y="210" width="35" height="21" fill="#ffeda7"/>
+<text class="v" x="237.5" y="224.6" fill="#24292f">9.27</text>
+<rect x="256" y="210" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="273.5" y="224.6" fill="#24292f">9.16</text>
+<rect x="292" y="210" width="35" height="21" fill="#fff0ac"/>
+<text class="v" x="309.5" y="224.6" fill="#24292f">9.17</text>
+<rect x="328" y="210" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="345.5" y="224.6" fill="#24292f">9.02</text>
+<rect x="364" y="210" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="381.5" y="224.6" fill="#24292f">9.03</text>
+<rect x="400" y="210" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="417.5" y="224.6" fill="#24292f">8.92</text>
+<rect x="436" y="210" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="453.5" y="224.6" fill="#24292f">9.00</text>
+<rect x="472" y="210" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="489.5" y="224.6" fill="#24292f">9.02</text>
+<rect x="508" y="210" width="35" height="21" fill="#fff4b4"/>
+<text class="v" x="525.5" y="224.6" fill="#24292f">9.01</text>
+<rect x="544" y="210" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="561.5" y="224.6" fill="#24292f">8.95</text>
+<rect x="580" y="210" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="597.5" y="224.6" fill="#24292f">9.00</text>
+<rect x="616" y="210" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="633.5" y="224.6" fill="#24292f">8.96</text>
+<rect x="652" y="210" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="669.5" y="224.6" fill="#24292f">9.03</text>
+<rect x="688" y="210" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="705.5" y="224.6" fill="#24292f">8.95</text>
+<rect x="724" y="210" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="741.5" y="224.6" fill="#24292f">8.95</text>
+<rect x="760" y="210" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="777.5" y="224.6" fill="#24292f">8.93</text>
+<rect x="796" y="210" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="813.5" y="224.6" fill="#24292f">8.97</text>
+<rect x="832" y="210" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="849.5" y="224.6" fill="#24292f">9.06</text>
+<rect x="868" y="210" width="35" height="21" fill="#fff1b0"/>
+<text class="v" x="885.5" y="224.6" fill="#24292f">9.10</text>
+<rect x="904" y="210" width="35" height="21" fill="#fff1b0"/>
+<text class="v" x="921.5" y="224.6" fill="#24292f">9.10</text>
+<rect x="940" y="210" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="957.5" y="224.6" fill="#24292f">8.99</text>
+<text class="t" x="68" y="247.0" text-anchor="end">22</text>
+<rect x="76" y="232" width="35" height="21" fill="#e56810"/>
+<text class="v" x="93.5" y="246.6" fill="#ffffff">12.9</text>
+<rect x="112" y="232" width="35" height="21" fill="#fecc61"/>
+<text class="v" x="129.5" y="246.6" fill="#24292f">10.4</text>
+<rect x="148" y="232" width="35" height="21" fill="#fed26c"/>
+<text class="v" x="165.5" y="246.6" fill="#24292f">10.2</text>
+<rect x="184" y="232" width="35" height="21" fill="#fee89b"/>
+<text class="v" x="201.5" y="246.6" fill="#24292f">9.51</text>
+<rect x="220" y="232" width="35" height="21" fill="#feeba2"/>
+<text class="v" x="237.5" y="246.6" fill="#24292f">9.36</text>
+<rect x="256" y="232" width="35" height="21" fill="#fff0ae"/>
+<text class="v" x="273.5" y="246.6" fill="#24292f">9.14</text>
+<rect x="292" y="232" width="35" height="21" fill="#ffefab"/>
+<text class="v" x="309.5" y="246.6" fill="#24292f">9.19</text>
+<rect x="328" y="232" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="345.5" y="246.6" fill="#24292f">8.97</text>
+<rect x="364" y="232" width="35" height="21" fill="#fff4b4"/>
+<text class="v" x="381.5" y="246.6" fill="#24292f">9.01</text>
+<rect x="400" y="232" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="417.5" y="246.6" fill="#24292f">9.00</text>
+<rect x="436" y="232" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="453.5" y="246.6" fill="#24292f">8.92</text>
+<rect x="472" y="232" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="489.5" y="246.6" fill="#24292f">9.02</text>
+<rect x="508" y="232" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="525.5" y="246.6" fill="#24292f">8.96</text>
+<rect x="544" y="232" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="561.5" y="246.6" fill="#24292f">8.94</text>
+<rect x="580" y="232" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="597.5" y="246.6" fill="#24292f">9.07</text>
+<rect x="616" y="232" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="633.5" y="246.6" fill="#24292f">9.01</text>
+<rect x="652" y="232" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="669.5" y="246.6" fill="#24292f">8.95</text>
+<rect x="688" y="232" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="705.5" y="246.6" fill="#24292f">9.02</text>
+<rect x="724" y="232" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="741.5" y="246.6" fill="#24292f">8.96</text>
+<rect x="760" y="232" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="777.5" y="246.6" fill="#24292f">8.95</text>
+<rect x="796" y="232" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="813.5" y="246.6" fill="#24292f">8.96</text>
+<rect x="832" y="232" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="849.5" y="246.6" fill="#24292f">9.02</text>
+<rect x="868" y="232" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="885.5" y="246.6" fill="#24292f">8.95</text>
+<rect x="904" y="232" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="921.5" y="246.6" fill="#24292f">8.93</text>
+<rect x="940" y="232" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="957.5" y="246.6" fill="#24292f">9.04</text>
+<text class="t" x="68" y="269.0" text-anchor="end">21</text>
+<rect x="76" y="254" width="35" height="21" fill="#e2650f"/>
+<text class="v" x="93.5" y="268.6" fill="#ffffff">13.0</text>
+<rect x="112" y="254" width="35" height="21" fill="#fecb5d"/>
+<text class="v" x="129.5" y="268.6" fill="#24292f">10.4</text>
+<rect x="148" y="254" width="35" height="21" fill="#fecd62"/>
+<text class="v" x="165.5" y="268.6" fill="#24292f">10.3</text>
+<rect x="184" y="254" width="35" height="21" fill="#fee697"/>
+<text class="v" x="201.5" y="268.6" fill="#24292f">9.57</text>
+<rect x="220" y="254" width="35" height="21" fill="#feea9f"/>
+<text class="v" x="237.5" y="268.6" fill="#24292f">9.42</text>
+<rect x="256" y="254" width="35" height="21" fill="#ffeda7"/>
+<text class="v" x="273.5" y="268.6" fill="#24292f">9.27</text>
+<rect x="292" y="254" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="309.5" y="268.6" fill="#24292f">9.16</text>
+<rect x="328" y="254" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="345.5" y="268.6" fill="#24292f">9.02</text>
+<rect x="364" y="254" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="381.5" y="268.6" fill="#24292f">8.94</text>
+<rect x="400" y="254" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="417.5" y="268.6" fill="#24292f">8.90</text>
+<rect x="436" y="254" width="35" height="21" fill="#fff7bb"/>
+<text class="v" x="453.5" y="268.6" fill="#24292f">8.88</text>
+<rect x="472" y="254" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="489.5" y="268.6" fill="#24292f">8.96</text>
+<rect x="508" y="254" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="525.5" y="268.6" fill="#24292f">9.01</text>
+<rect x="544" y="254" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="561.5" y="268.6" fill="#24292f">9.02</text>
+<rect x="580" y="254" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="597.5" y="268.6" fill="#24292f">8.94</text>
+<rect x="616" y="254" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="633.5" y="268.6" fill="#24292f">8.90</text>
+<rect x="652" y="254" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="669.5" y="268.6" fill="#24292f">8.98</text>
+<rect x="688" y="254" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="705.5" y="268.6" fill="#24292f">8.98</text>
+<rect x="724" y="254" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="741.5" y="268.6" fill="#24292f">8.95</text>
+<rect x="760" y="254" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="777.5" y="268.6" fill="#24292f">9.01</text>
+<rect x="796" y="254" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="813.5" y="268.6" fill="#24292f">8.95</text>
+<rect x="832" y="254" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="849.5" y="268.6" fill="#24292f">8.90</text>
+<rect x="868" y="254" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="885.5" y="268.6" fill="#24292f">8.97</text>
+<rect x="904" y="254" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="921.5" y="268.6" fill="#24292f">8.96</text>
+<rect x="940" y="254" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="957.5" y="268.6" fill="#24292f">9.02</text>
+<text class="t" x="68" y="291.0" text-anchor="end">20</text>
+<rect x="76" y="276" width="35" height="21" fill="#dc5e0b"/>
+<text class="v" x="93.5" y="290.6" fill="#ffffff">13.2</text>
+<rect x="112" y="276" width="35" height="21" fill="#febe49"/>
+<text class="v" x="129.5" y="290.6" fill="#24292f">10.7</text>
+<rect x="148" y="276" width="35" height="21" fill="#fec857"/>
+<text class="v" x="165.5" y="290.6" fill="#24292f">10.5</text>
+<rect x="184" y="276" width="35" height="21" fill="#fedf87"/>
+<text class="v" x="201.5" y="290.6" fill="#24292f">9.82</text>
+<rect x="220" y="276" width="35" height="21" fill="#fee18d"/>
+<text class="v" x="237.5" y="290.6" fill="#24292f">9.75</text>
+<rect x="256" y="276" width="35" height="21" fill="#fee79a"/>
+<text class="v" x="273.5" y="290.6" fill="#24292f">9.52</text>
+<rect x="292" y="276" width="35" height="21" fill="#feeba3"/>
+<text class="v" x="309.5" y="290.6" fill="#24292f">9.35</text>
+<rect x="328" y="276" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="345.5" y="290.6" fill="#24292f">8.93</text>
+<rect x="364" y="276" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="381.5" y="290.6" fill="#24292f">9.01</text>
+<rect x="400" y="276" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="417.5" y="290.6" fill="#24292f">8.99</text>
+<rect x="436" y="276" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="453.5" y="290.6" fill="#24292f">8.92</text>
+<rect x="472" y="276" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="489.5" y="290.6" fill="#24292f">8.91</text>
+<rect x="508" y="276" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="525.5" y="290.6" fill="#24292f">9.02</text>
+<rect x="544" y="276" width="35" height="21" fill="#fff4b7"/>
+<text class="v" x="561.5" y="290.6" fill="#24292f">8.97</text>
+<rect x="580" y="276" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="597.5" y="290.6" fill="#24292f">8.94</text>
+<rect x="616" y="276" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="633.5" y="290.6" fill="#24292f">8.94</text>
+<rect x="652" y="276" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="669.5" y="290.6" fill="#24292f">9.03</text>
+<rect x="688" y="276" width="35" height="21" fill="#fff1ae"/>
+<text class="v" x="705.5" y="290.6" fill="#24292f">9.13</text>
+<rect x="724" y="276" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="741.5" y="290.6" fill="#24292f">8.93</text>
+<rect x="760" y="276" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="777.5" y="290.6" fill="#24292f">9.05</text>
+<rect x="796" y="276" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="813.5" y="290.6" fill="#24292f">8.98</text>
+<rect x="832" y="276" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="849.5" y="290.6" fill="#24292f">8.96</text>
+<rect x="868" y="276" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="885.5" y="290.6" fill="#24292f">9.01</text>
+<rect x="904" y="276" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="921.5" y="290.6" fill="#24292f">9.02</text>
+<rect x="940" y="276" width="35" height="21" fill="#ffefac"/>
+<text class="v" x="957.5" y="290.6" fill="#24292f">9.18</text>
+<text class="t" x="68" y="313.0" text-anchor="end">19</text>
+<rect x="76" y="298" width="35" height="21" fill="#d95a09"/>
+<text class="v" x="93.5" y="312.6" fill="#ffffff">13.4</text>
+<rect x="112" y="298" width="35" height="21" fill="#feb744"/>
+<text class="v" x="129.5" y="312.6" fill="#24292f">10.9</text>
+<rect x="148" y="298" width="35" height="21" fill="#fec04c"/>
+<text class="v" x="165.5" y="312.6" fill="#24292f">10.7</text>
+<rect x="184" y="298" width="35" height="21" fill="#fed16b"/>
+<text class="v" x="201.5" y="312.6" fill="#24292f">10.2</text>
+<rect x="220" y="298" width="35" height="21" fill="#fed97c"/>
+<text class="v" x="237.5" y="312.6" fill="#24292f">9.98</text>
+<rect x="256" y="298" width="35" height="21" fill="#fee494"/>
+<text class="v" x="273.5" y="312.6" fill="#24292f">9.64</text>
+<rect x="292" y="298" width="35" height="21" fill="#fee89c"/>
+<text class="v" x="309.5" y="312.6" fill="#24292f">9.49</text>
+<rect x="328" y="298" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="345.5" y="312.6" fill="#24292f">8.94</text>
+<rect x="364" y="298" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="381.5" y="312.6" fill="#24292f">8.93</text>
+<rect x="400" y="298" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="417.5" y="312.6" fill="#24292f">9.01</text>
+<rect x="436" y="298" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="453.5" y="312.6" fill="#24292f">9.00</text>
+<rect x="472" y="298" width="35" height="21" fill="#fff6bb"/>
+<text class="v" x="489.5" y="312.6" fill="#24292f">8.90</text>
+<rect x="508" y="298" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="525.5" y="312.6" fill="#24292f">8.97</text>
+<rect x="544" y="298" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="561.5" y="312.6" fill="#24292f">8.97</text>
+<rect x="580" y="298" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="597.5" y="312.6" fill="#24292f">9.01</text>
+<rect x="616" y="298" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="633.5" y="312.6" fill="#24292f">9.01</text>
+<rect x="652" y="298" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="669.5" y="312.6" fill="#24292f">9.04</text>
+<rect x="688" y="298" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="705.5" y="312.6" fill="#24292f">8.96</text>
+<rect x="724" y="298" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="741.5" y="312.6" fill="#24292f">9.01</text>
+<rect x="760" y="298" width="35" height="21" fill="#fff1b0"/>
+<text class="v" x="777.5" y="312.6" fill="#24292f">9.10</text>
+<rect x="796" y="298" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="813.5" y="312.6" fill="#24292f">8.99</text>
+<rect x="832" y="298" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="849.5" y="312.6" fill="#24292f">9.05</text>
+<rect x="868" y="298" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="885.5" y="312.6" fill="#24292f">9.04</text>
+<rect x="904" y="298" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="921.5" y="312.6" fill="#24292f">9.02</text>
+<rect x="940" y="298" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="957.5" y="312.6" fill="#24292f">8.99</text>
+<text class="t" x="68" y="335.0" text-anchor="end">18</text>
+<rect x="76" y="320" width="35" height="21" fill="#d25306"/>
+<text class="v" x="93.5" y="334.6" fill="#ffffff">13.6</text>
+<rect x="112" y="320" width="35" height="21" fill="#feb441"/>
+<text class="v" x="129.5" y="334.6" fill="#24292f">11.0</text>
+<rect x="148" y="320" width="35" height="21" fill="#febd49"/>
+<text class="v" x="165.5" y="334.6" fill="#24292f">10.8</text>
+<rect x="184" y="320" width="35" height="21" fill="#fec756"/>
+<text class="v" x="201.5" y="334.6" fill="#24292f">10.5</text>
+<rect x="220" y="320" width="35" height="21" fill="#fed471"/>
+<text class="v" x="237.5" y="334.6" fill="#24292f">10.1</text>
+<rect x="256" y="320" width="35" height="21" fill="#fedb80"/>
+<text class="v" x="273.5" y="334.6" fill="#24292f">9.93</text>
+<rect x="292" y="320" width="35" height="21" fill="#fee18d"/>
+<text class="v" x="309.5" y="334.6" fill="#24292f">9.75</text>
+<rect x="328" y="320" width="35" height="21" fill="#fff3b2"/>
+<text class="v" x="345.5" y="334.6" fill="#24292f">9.05</text>
+<rect x="364" y="320" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="381.5" y="334.6" fill="#24292f">9.03</text>
+<rect x="400" y="320" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="417.5" y="334.6" fill="#24292f">9.00</text>
+<rect x="436" y="320" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="453.5" y="334.6" fill="#24292f">9.02</text>
+<rect x="472" y="320" width="35" height="21" fill="#fff4b7"/>
+<text class="v" x="489.5" y="334.6" fill="#24292f">8.97</text>
+<rect x="508" y="320" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="525.5" y="334.6" fill="#24292f">8.91</text>
+<rect x="544" y="320" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="561.5" y="334.6" fill="#24292f">9.03</text>
+<rect x="580" y="320" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="597.5" y="334.6" fill="#24292f">8.95</text>
+<rect x="616" y="320" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="633.5" y="334.6" fill="#24292f">9.09</text>
+<rect x="652" y="320" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="669.5" y="334.6" fill="#24292f">8.96</text>
+<rect x="688" y="320" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="705.5" y="334.6" fill="#24292f">8.94</text>
+<rect x="724" y="320" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="741.5" y="334.6" fill="#24292f">9.03</text>
+<rect x="760" y="320" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="777.5" y="334.6" fill="#24292f">8.97</text>
+<rect x="796" y="320" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="813.5" y="334.6" fill="#24292f">8.98</text>
+<rect x="832" y="320" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="849.5" y="334.6" fill="#24292f">9.06</text>
+<rect x="868" y="320" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="885.5" y="334.6" fill="#24292f">8.97</text>
+<rect x="904" y="320" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="921.5" y="334.6" fill="#24292f">8.93</text>
+<rect x="940" y="320" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="957.5" y="334.6" fill="#24292f">8.97</text>
+<text class="t" x="68" y="357.0" text-anchor="end">17</text>
+<rect x="76" y="342" width="35" height="21" fill="#d65707"/>
+<text class="v" x="93.5" y="356.6" fill="#ffffff">13.5</text>
+<rect x="112" y="342" width="35" height="21" fill="#feb340"/>
+<text class="v" x="129.5" y="356.6" fill="#24292f">11.0</text>
+<rect x="148" y="342" width="35" height="21" fill="#feb744"/>
+<text class="v" x="165.5" y="356.6" fill="#24292f">10.9</text>
+<rect x="184" y="342" width="35" height="21" fill="#fec755"/>
+<text class="v" x="201.5" y="356.6" fill="#24292f">10.5</text>
+<rect x="220" y="342" width="35" height="21" fill="#fed16a"/>
+<text class="v" x="237.5" y="356.6" fill="#24292f">10.2</text>
+<rect x="256" y="342" width="35" height="21" fill="#fedc83"/>
+<text class="v" x="273.5" y="356.6" fill="#24292f">9.89</text>
+<rect x="292" y="342" width="35" height="21" fill="#fee08b"/>
+<text class="v" x="309.5" y="356.6" fill="#24292f">9.77</text>
+<rect x="328" y="342" width="35" height="21" fill="#fff1b0"/>
+<text class="v" x="345.5" y="356.6" fill="#24292f">9.09</text>
+<rect x="364" y="342" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="381.5" y="356.6" fill="#24292f">8.98</text>
+<rect x="400" y="342" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="417.5" y="356.6" fill="#24292f">9.03</text>
+<rect x="436" y="342" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="453.5" y="356.6" fill="#24292f">9.02</text>
+<rect x="472" y="342" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="489.5" y="356.6" fill="#24292f">9.04</text>
+<rect x="508" y="342" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="525.5" y="356.6" fill="#24292f">8.94</text>
+<rect x="544" y="342" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="561.5" y="356.6" fill="#24292f">8.94</text>
+<rect x="580" y="342" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="597.5" y="356.6" fill="#24292f">8.95</text>
+<rect x="616" y="342" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="633.5" y="356.6" fill="#24292f">9.01</text>
+<rect x="652" y="342" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="669.5" y="356.6" fill="#24292f">8.94</text>
+<rect x="688" y="342" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="705.5" y="356.6" fill="#24292f">8.95</text>
+<rect x="724" y="342" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="741.5" y="356.6" fill="#24292f">9.01</text>
+<rect x="760" y="342" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="777.5" y="356.6" fill="#24292f">8.95</text>
+<rect x="796" y="342" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="813.5" y="356.6" fill="#24292f">9.03</text>
+<rect x="832" y="342" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="849.5" y="356.6" fill="#24292f">9.01</text>
+<rect x="868" y="342" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="885.5" y="356.6" fill="#24292f">9.04</text>
+<rect x="904" y="342" width="35" height="21" fill="#fff3b2"/>
+<text class="v" x="921.5" y="356.6" fill="#24292f">9.05</text>
+<rect x="940" y="342" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="957.5" y="356.6" fill="#24292f">9.05</text>
+<text class="t" x="68" y="379.0" text-anchor="end">16</text>
+<rect x="76" y="364" width="35" height="21" fill="#ce4e03"/>
+<text class="v" x="93.5" y="378.6" fill="#ffffff">13.8</text>
+<rect x="112" y="364" width="35" height="21" fill="#fc9527"/>
+<text class="v" x="129.5" y="378.6" fill="#24292f">11.7</text>
+<rect x="148" y="364" width="35" height="21" fill="#fd9828"/>
+<text class="v" x="165.5" y="378.6" fill="#24292f">11.6</text>
+<rect x="184" y="364" width="35" height="21" fill="#fea736"/>
+<text class="v" x="201.5" y="378.6" fill="#24292f">11.2</text>
+<rect x="220" y="364" width="35" height="21" fill="#feb23f"/>
+<text class="v" x="237.5" y="378.6" fill="#24292f">11.0</text>
+<rect x="256" y="364" width="35" height="21" fill="#fec856"/>
+<text class="v" x="273.5" y="378.6" fill="#24292f">10.5</text>
+<rect x="292" y="364" width="35" height="21" fill="#fecf67"/>
+<text class="v" x="309.5" y="378.6" fill="#24292f">10.3</text>
+<rect x="328" y="364" width="35" height="21" fill="#fee493"/>
+<text class="v" x="345.5" y="378.6" fill="#24292f">9.65</text>
+<rect x="364" y="364" width="35" height="21" fill="#feeaa1"/>
+<text class="v" x="381.5" y="378.6" fill="#24292f">9.39</text>
+<rect x="400" y="364" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="417.5" y="378.6" fill="#24292f">8.98</text>
+<rect x="436" y="364" width="35" height="21" fill="#fff7bb"/>
+<text class="v" x="453.5" y="378.6" fill="#24292f">8.89</text>
+<rect x="472" y="364" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="489.5" y="378.6" fill="#24292f">9.15</text>
+<rect x="508" y="364" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="525.5" y="378.6" fill="#24292f">8.97</text>
+<rect x="544" y="364" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="561.5" y="378.6" fill="#24292f">9.02</text>
+<rect x="580" y="364" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="597.5" y="378.6" fill="#24292f">8.93</text>
+<rect x="616" y="364" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="633.5" y="378.6" fill="#24292f">9.00</text>
+<rect x="652" y="364" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="669.5" y="378.6" fill="#24292f">9.01</text>
+<rect x="688" y="364" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="705.5" y="378.6" fill="#24292f">9.02</text>
+<rect x="724" y="364" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="741.5" y="378.6" fill="#24292f">8.91</text>
+<rect x="760" y="364" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="777.5" y="378.6" fill="#24292f">9.03</text>
+<rect x="796" y="364" width="35" height="21" fill="#fff1b0"/>
+<text class="v" x="813.5" y="378.6" fill="#24292f">9.10</text>
+<rect x="832" y="364" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="849.5" y="378.6" fill="#24292f">8.93</text>
+<rect x="868" y="364" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="885.5" y="378.6" fill="#24292f">9.07</text>
+<rect x="904" y="364" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="921.5" y="378.6" fill="#24292f">8.98</text>
+<rect x="940" y="364" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="957.5" y="378.6" fill="#24292f">8.97</text>
+<text class="t" x="68" y="401.0" text-anchor="end">15</text>
+<rect x="76" y="386" width="35" height="21" fill="#cf4f04"/>
+<text class="v" x="93.5" y="400.6" fill="#ffffff">13.7</text>
+<rect x="112" y="386" width="35" height="21" fill="#fe9c2c"/>
+<text class="v" x="129.5" y="400.6" fill="#24292f">11.5</text>
+<rect x="148" y="386" width="35" height="21" fill="#fea533"/>
+<text class="v" x="165.5" y="400.6" fill="#24292f">11.3</text>
+<rect x="184" y="386" width="35" height="21" fill="#feb23f"/>
+<text class="v" x="201.5" y="400.6" fill="#24292f">11.0</text>
+<rect x="220" y="386" width="35" height="21" fill="#febe4a"/>
+<text class="v" x="237.5" y="400.6" fill="#24292f">10.7</text>
+<rect x="256" y="386" width="35" height="21" fill="#feca5b"/>
+<text class="v" x="273.5" y="400.6" fill="#24292f">10.4</text>
+<rect x="292" y="386" width="35" height="21" fill="#fed068"/>
+<text class="v" x="309.5" y="400.6" fill="#24292f">10.3</text>
+<rect x="328" y="386" width="35" height="21" fill="#fee697"/>
+<text class="v" x="345.5" y="400.6" fill="#24292f">9.58</text>
+<rect x="364" y="386" width="35" height="21" fill="#feeaa0"/>
+<text class="v" x="381.5" y="400.6" fill="#24292f">9.41</text>
+<rect x="400" y="386" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="417.5" y="400.6" fill="#24292f">9.03</text>
+<rect x="436" y="386" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="453.5" y="400.6" fill="#24292f">8.95</text>
+<rect x="472" y="386" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="489.5" y="400.6" fill="#24292f">8.95</text>
+<rect x="508" y="386" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="525.5" y="400.6" fill="#24292f">9.02</text>
+<rect x="544" y="386" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="561.5" y="400.6" fill="#24292f">8.90</text>
+<rect x="580" y="386" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="597.5" y="400.6" fill="#24292f">8.95</text>
+<rect x="616" y="386" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="633.5" y="400.6" fill="#24292f">8.94</text>
+<rect x="652" y="386" width="35" height="21" fill="#fff7bc"/>
+<text class="v" x="669.5" y="400.6" fill="#24292f">8.87</text>
+<rect x="688" y="386" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="705.5" y="400.6" fill="#24292f">8.94</text>
+<rect x="724" y="386" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="741.5" y="400.6" fill="#24292f">8.91</text>
+<rect x="760" y="386" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="777.5" y="400.6" fill="#24292f">8.95</text>
+<rect x="796" y="386" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="813.5" y="400.6" fill="#24292f">9.00</text>
+<rect x="832" y="386" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="849.5" y="400.6" fill="#24292f">9.02</text>
+<rect x="868" y="386" width="35" height="21" fill="#ffefab"/>
+<text class="v" x="885.5" y="400.6" fill="#24292f">9.19</text>
+<rect x="904" y="386" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="921.5" y="400.6" fill="#24292f">8.97</text>
+<rect x="940" y="386" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="957.5" y="400.6" fill="#24292f">9.03</text>
+<text class="t" x="68" y="423.0" text-anchor="end">14</text>
+<rect x="76" y="408" width="35" height="21" fill="#d05004"/>
+<text class="v" x="93.5" y="422.6" fill="#ffffff">13.7</text>
+<rect x="112" y="408" width="35" height="21" fill="#fe9d2d"/>
+<text class="v" x="129.5" y="422.6" fill="#24292f">11.5</text>
+<rect x="148" y="408" width="35" height="21" fill="#fea231"/>
+<text class="v" x="165.5" y="422.6" fill="#24292f">11.4</text>
+<rect x="184" y="408" width="35" height="21" fill="#feb340"/>
+<text class="v" x="201.5" y="422.6" fill="#24292f">11.0</text>
+<rect x="220" y="408" width="35" height="21" fill="#feb642"/>
+<text class="v" x="237.5" y="422.6" fill="#24292f">10.9</text>
+<rect x="256" y="408" width="35" height="21" fill="#febf4b"/>
+<text class="v" x="273.5" y="422.6" fill="#24292f">10.7</text>
+<rect x="292" y="408" width="35" height="21" fill="#fecc61"/>
+<text class="v" x="309.5" y="422.6" fill="#24292f">10.3</text>
+<rect x="328" y="408" width="35" height="21" fill="#fee391"/>
+<text class="v" x="345.5" y="422.6" fill="#24292f">9.70</text>
+<rect x="364" y="408" width="35" height="21" fill="#fee596"/>
+<text class="v" x="381.5" y="422.6" fill="#24292f">9.61</text>
+<rect x="400" y="408" width="35" height="21" fill="#feeda6"/>
+<text class="v" x="417.5" y="422.6" fill="#24292f">9.29</text>
+<rect x="436" y="408" width="35" height="21" fill="#fff0ae"/>
+<text class="v" x="453.5" y="422.6" fill="#24292f">9.14</text>
+<rect x="472" y="408" width="35" height="21" fill="#fff6bb"/>
+<text class="v" x="489.5" y="422.6" fill="#24292f">8.89</text>
+<rect x="508" y="408" width="35" height="21" fill="#fff7bb"/>
+<text class="v" x="525.5" y="422.6" fill="#24292f">8.89</text>
+<rect x="544" y="408" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="561.5" y="422.6" fill="#24292f">8.96</text>
+<rect x="580" y="408" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="597.5" y="422.6" fill="#24292f">9.02</text>
+<rect x="616" y="408" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="633.5" y="422.6" fill="#24292f">8.94</text>
+<rect x="652" y="408" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="669.5" y="422.6" fill="#24292f">8.92</text>
+<rect x="688" y="408" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="705.5" y="422.6" fill="#24292f">8.96</text>
+<rect x="724" y="408" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="741.5" y="422.6" fill="#24292f">9.02</text>
+<rect x="760" y="408" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="777.5" y="422.6" fill="#24292f">8.96</text>
+<rect x="796" y="408" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="813.5" y="422.6" fill="#24292f">8.93</text>
+<rect x="832" y="408" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="849.5" y="422.6" fill="#24292f">8.93</text>
+<rect x="868" y="408" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="885.5" y="422.6" fill="#24292f">8.91</text>
+<rect x="904" y="408" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="921.5" y="422.6" fill="#24292f">8.92</text>
+<rect x="940" y="408" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="957.5" y="422.6" fill="#24292f">9.06</text>
+<text class="t" x="68" y="445.0" text-anchor="end">13</text>
+<rect x="76" y="430" width="35" height="21" fill="#b34003"/>
+<text class="v" x="93.5" y="444.6" fill="#ffffff">14.3</text>
+<rect x="112" y="430" width="35" height="21" fill="#ef7818"/>
+<text class="v" x="129.5" y="444.6" fill="#24292f">12.5</text>
+<rect x="148" y="430" width="35" height="21" fill="#f3801c"/>
+<text class="v" x="165.5" y="444.6" fill="#24292f">12.2</text>
+<rect x="184" y="430" width="35" height="21" fill="#f6861f"/>
+<text class="v" x="201.5" y="444.6" fill="#24292f">12.1</text>
+<rect x="220" y="430" width="35" height="21" fill="#fb9225"/>
+<text class="v" x="237.5" y="444.6" fill="#24292f">11.8</text>
+<rect x="256" y="430" width="35" height="21" fill="#fea130"/>
+<text class="v" x="273.5" y="444.6" fill="#24292f">11.4</text>
+<rect x="292" y="430" width="35" height="21" fill="#feb441"/>
+<text class="v" x="309.5" y="444.6" fill="#24292f">11.0</text>
+<rect x="328" y="430" width="35" height="21" fill="#feca5b"/>
+<text class="v" x="345.5" y="444.6" fill="#24292f">10.4</text>
+<rect x="364" y="430" width="35" height="21" fill="#fed26d"/>
+<text class="v" x="381.5" y="444.6" fill="#24292f">10.2</text>
+<rect x="400" y="430" width="35" height="21" fill="#fedf88"/>
+<text class="v" x="417.5" y="444.6" fill="#24292f">9.81</text>
+<rect x="436" y="430" width="35" height="21" fill="#fee696"/>
+<text class="v" x="453.5" y="444.6" fill="#24292f">9.59</text>
+<rect x="472" y="430" width="35" height="21" fill="#ffeda7"/>
+<text class="v" x="489.5" y="444.6" fill="#24292f">9.27</text>
+<rect x="508" y="430" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="525.5" y="444.6" fill="#24292f">9.00</text>
+<rect x="544" y="430" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="561.5" y="444.6" fill="#24292f">8.99</text>
+<rect x="580" y="430" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="597.5" y="444.6" fill="#24292f">9.00</text>
+<rect x="616" y="430" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="633.5" y="444.6" fill="#24292f">9.02</text>
+<rect x="652" y="430" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="669.5" y="444.6" fill="#24292f">9.02</text>
+<rect x="688" y="430" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="705.5" y="444.6" fill="#24292f">9.02</text>
+<rect x="724" y="430" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="741.5" y="444.6" fill="#24292f">9.06</text>
+<rect x="760" y="430" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="777.5" y="444.6" fill="#24292f">8.92</text>
+<rect x="796" y="430" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="813.5" y="444.6" fill="#24292f">8.95</text>
+<rect x="832" y="430" width="35" height="21" fill="#fff7bb"/>
+<text class="v" x="849.5" y="444.6" fill="#24292f">8.89</text>
+<rect x="868" y="430" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="885.5" y="444.6" fill="#24292f">9.01</text>
+<rect x="904" y="430" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="921.5" y="444.6" fill="#24292f">8.99</text>
+<rect x="940" y="430" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="957.5" y="444.6" fill="#24292f">8.91</text>
+<text class="t" x="68" y="467.0" text-anchor="end">12</text>
+<rect x="76" y="452" width="35" height="21" fill="#c04602"/>
+<text class="v" x="93.5" y="466.6" fill="#ffffff">14.1</text>
+<rect x="112" y="452" width="35" height="21" fill="#f88a21"/>
+<text class="v" x="129.5" y="466.6" fill="#24292f">12.0</text>
+<rect x="148" y="452" width="35" height="21" fill="#fb9326"/>
+<text class="v" x="165.5" y="466.6" fill="#24292f">11.7</text>
+<rect x="184" y="452" width="35" height="21" fill="#fd9728"/>
+<text class="v" x="201.5" y="466.6" fill="#24292f">11.6</text>
+<rect x="220" y="452" width="35" height="21" fill="#fea332"/>
+<text class="v" x="237.5" y="466.6" fill="#24292f">11.3</text>
+<rect x="256" y="452" width="35" height="21" fill="#feb23f"/>
+<text class="v" x="273.5" y="466.6" fill="#24292f">11.0</text>
+<rect x="292" y="452" width="35" height="21" fill="#febc48"/>
+<text class="v" x="309.5" y="466.6" fill="#24292f">10.8</text>
+<rect x="328" y="452" width="35" height="21" fill="#fed069"/>
+<text class="v" x="345.5" y="466.6" fill="#24292f">10.2</text>
+<rect x="364" y="452" width="35" height="21" fill="#fed778"/>
+<text class="v" x="381.5" y="466.6" fill="#24292f">10.0</text>
+<rect x="400" y="452" width="35" height="21" fill="#fee08a"/>
+<text class="v" x="417.5" y="466.6" fill="#24292f">9.80</text>
+<rect x="436" y="452" width="35" height="21" fill="#fede86"/>
+<text class="v" x="453.5" y="466.6" fill="#24292f">9.84</text>
+<rect x="472" y="452" width="35" height="21" fill="#fee89c"/>
+<text class="v" x="489.5" y="466.6" fill="#24292f">9.48</text>
+<rect x="508" y="452" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="525.5" y="466.6" fill="#24292f">9.15</text>
+<rect x="544" y="452" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="561.5" y="466.6" fill="#24292f">8.97</text>
+<rect x="580" y="452" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="597.5" y="466.6" fill="#24292f">8.91</text>
+<rect x="616" y="452" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="633.5" y="466.6" fill="#24292f">8.99</text>
+<rect x="652" y="452" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="669.5" y="466.6" fill="#24292f">9.04</text>
+<rect x="688" y="452" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="705.5" y="466.6" fill="#24292f">9.01</text>
+<rect x="724" y="452" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="741.5" y="466.6" fill="#24292f">9.16</text>
+<rect x="760" y="452" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="777.5" y="466.6" fill="#24292f">8.96</text>
+<rect x="796" y="452" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="813.5" y="466.6" fill="#24292f">9.01</text>
+<rect x="832" y="452" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="849.5" y="466.6" fill="#24292f">8.92</text>
+<rect x="868" y="452" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="885.5" y="466.6" fill="#24292f">9.00</text>
+<rect x="904" y="452" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="921.5" y="466.6" fill="#24292f">9.04</text>
+<rect x="940" y="452" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="957.5" y="466.6" fill="#24292f">8.93</text>
+<text class="t" x="68" y="489.0" text-anchor="end">11</text>
+<rect x="76" y="474" width="35" height="21" fill="#bb4403"/>
+<text class="v" x="93.5" y="488.6" fill="#ffffff">14.2</text>
+<rect x="112" y="474" width="35" height="21" fill="#f17b19"/>
+<text class="v" x="129.5" y="488.6" fill="#24292f">12.4</text>
+<rect x="148" y="474" width="35" height="21" fill="#f5851f"/>
+<text class="v" x="165.5" y="488.6" fill="#24292f">12.1</text>
+<rect x="184" y="474" width="35" height="21" fill="#f5851f"/>
+<text class="v" x="201.5" y="488.6" fill="#24292f">12.1</text>
+<rect x="220" y="474" width="35" height="21" fill="#f98f24"/>
+<text class="v" x="237.5" y="488.6" fill="#24292f">11.8</text>
+<rect x="256" y="474" width="35" height="21" fill="#feb23f"/>
+<text class="v" x="273.5" y="488.6" fill="#24292f">11.0</text>
+<rect x="292" y="474" width="35" height="21" fill="#fea735"/>
+<text class="v" x="309.5" y="488.6" fill="#24292f">11.3</text>
+<rect x="328" y="474" width="35" height="21" fill="#febe4a"/>
+<text class="v" x="345.5" y="488.6" fill="#24292f">10.7</text>
+<rect x="364" y="474" width="35" height="21" fill="#fec857"/>
+<text class="v" x="381.5" y="488.6" fill="#24292f">10.5</text>
+<rect x="400" y="474" width="35" height="21" fill="#fed16a"/>
+<text class="v" x="417.5" y="488.6" fill="#24292f">10.2</text>
+<rect x="436" y="474" width="35" height="21" fill="#feda7d"/>
+<text class="v" x="453.5" y="488.6" fill="#24292f">9.96</text>
+<rect x="472" y="474" width="35" height="21" fill="#fee28e"/>
+<text class="v" x="489.5" y="488.6" fill="#24292f">9.74</text>
+<rect x="508" y="474" width="35" height="21" fill="#fee99d"/>
+<text class="v" x="525.5" y="488.6" fill="#24292f">9.46</text>
+<rect x="544" y="474" width="35" height="21" fill="#fff0ae"/>
+<text class="v" x="561.5" y="488.6" fill="#24292f">9.14</text>
+<rect x="580" y="474" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="597.5" y="488.6" fill="#24292f">8.95</text>
+<rect x="616" y="474" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="633.5" y="488.6" fill="#24292f">8.99</text>
+<rect x="652" y="474" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="669.5" y="488.6" fill="#24292f">9.09</text>
+<rect x="688" y="474" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="705.5" y="488.6" fill="#24292f">8.96</text>
+<rect x="724" y="474" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="741.5" y="488.6" fill="#24292f">8.93</text>
+<rect x="760" y="474" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="777.5" y="488.6" fill="#24292f">8.97</text>
+<rect x="796" y="474" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="813.5" y="488.6" fill="#24292f">9.01</text>
+<rect x="832" y="474" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="849.5" y="488.6" fill="#24292f">8.94</text>
+<rect x="868" y="474" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="885.5" y="488.6" fill="#24292f">9.01</text>
+<rect x="904" y="474" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="921.5" y="488.6" fill="#24292f">8.95</text>
+<rect x="940" y="474" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="957.5" y="488.6" fill="#24292f">9.05</text>
+<text class="t" x="68" y="511.0" text-anchor="end">10</text>
+<rect x="76" y="496" width="35" height="21" fill="#b84203"/>
+<text class="v" x="93.5" y="510.6" fill="#ffffff">14.2</text>
+<rect x="112" y="496" width="35" height="21" fill="#ed7316"/>
+<text class="v" x="129.5" y="510.6" fill="#24292f">12.6</text>
+<rect x="148" y="496" width="35" height="21" fill="#f07818"/>
+<text class="v" x="165.5" y="510.6" fill="#24292f">12.4</text>
+<rect x="184" y="496" width="35" height="21" fill="#f27e1b"/>
+<text class="v" x="201.5" y="510.6" fill="#24292f">12.3</text>
+<rect x="220" y="496" width="35" height="21" fill="#f37f1c"/>
+<text class="v" x="237.5" y="510.6" fill="#24292f">12.3</text>
+<rect x="256" y="496" width="35" height="21" fill="#f88b22"/>
+<text class="v" x="273.5" y="510.6" fill="#24292f">11.9</text>
+<rect x="292" y="496" width="35" height="21" fill="#fd9627"/>
+<text class="v" x="309.5" y="510.6" fill="#24292f">11.7</text>
+<rect x="328" y="496" width="35" height="21" fill="#feb03d"/>
+<text class="v" x="345.5" y="510.6" fill="#24292f">11.1</text>
+<rect x="364" y="496" width="35" height="21" fill="#feb643"/>
+<text class="v" x="381.5" y="510.6" fill="#24292f">10.9</text>
+<rect x="400" y="496" width="35" height="21" fill="#fec654"/>
+<text class="v" x="417.5" y="510.6" fill="#24292f">10.5</text>
+<rect x="436" y="496" width="35" height="21" fill="#fecb5f"/>
+<text class="v" x="453.5" y="510.6" fill="#24292f">10.4</text>
+<rect x="472" y="496" width="35" height="21" fill="#fed573"/>
+<text class="v" x="489.5" y="510.6" fill="#24292f">10.1</text>
+<rect x="508" y="496" width="35" height="21" fill="#fee28f"/>
+<text class="v" x="525.5" y="510.6" fill="#24292f">9.72</text>
+<rect x="544" y="496" width="35" height="21" fill="#feea9f"/>
+<text class="v" x="561.5" y="510.6" fill="#24292f">9.42</text>
+<rect x="580" y="496" width="35" height="21" fill="#fff1af"/>
+<text class="v" x="597.5" y="510.6" fill="#24292f">9.11</text>
+<rect x="616" y="496" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="633.5" y="510.6" fill="#24292f">9.04</text>
+<rect x="652" y="496" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="669.5" y="510.6" fill="#24292f">9.03</text>
+<rect x="688" y="496" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="705.5" y="510.6" fill="#24292f">8.93</text>
+<rect x="724" y="496" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="741.5" y="510.6" fill="#24292f">8.92</text>
+<rect x="760" y="496" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="777.5" y="510.6" fill="#24292f">8.94</text>
+<rect x="796" y="496" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="813.5" y="510.6" fill="#24292f">9.06</text>
+<rect x="832" y="496" width="35" height="21" fill="#fff3b2"/>
+<text class="v" x="849.5" y="510.6" fill="#24292f">9.05</text>
+<rect x="868" y="496" width="35" height="21" fill="#fff4b4"/>
+<text class="v" x="885.5" y="510.6" fill="#24292f">9.01</text>
+<rect x="904" y="496" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="921.5" y="510.6" fill="#24292f">9.02</text>
+<rect x="940" y="496" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="957.5" y="510.6" fill="#24292f">8.98</text>
+<text class="t" x="68" y="533.0" text-anchor="end">9</text>
+<rect x="76" y="518" width="35" height="21" fill="#9f3603"/>
+<text class="v" x="93.5" y="532.6" fill="#ffffff">14.7</text>
+<rect x="112" y="518" width="35" height="21" fill="#e46710"/>
+<text class="v" x="129.5" y="532.6" fill="#ffffff">12.9</text>
+<rect x="148" y="518" width="35" height="21" fill="#eb6f14"/>
+<text class="v" x="165.5" y="532.6" fill="#24292f">12.7</text>
+<rect x="184" y="518" width="35" height="21" fill="#ed7215"/>
+<text class="v" x="201.5" y="532.6" fill="#24292f">12.6</text>
+<rect x="220" y="518" width="35" height="21" fill="#f07a19"/>
+<text class="v" x="237.5" y="532.6" fill="#24292f">12.4</text>
+<rect x="256" y="518" width="35" height="21" fill="#f5851f"/>
+<text class="v" x="273.5" y="532.6" fill="#24292f">12.1</text>
+<rect x="292" y="518" width="35" height="21" fill="#fb9125"/>
+<text class="v" x="309.5" y="532.6" fill="#24292f">11.8</text>
+<rect x="328" y="518" width="35" height="21" fill="#fea735"/>
+<text class="v" x="345.5" y="532.6" fill="#24292f">11.3</text>
+<rect x="364" y="518" width="35" height="21" fill="#feb542"/>
+<text class="v" x="381.5" y="532.6" fill="#24292f">10.9</text>
+<rect x="400" y="518" width="35" height="21" fill="#fec34e"/>
+<text class="v" x="417.5" y="532.6" fill="#24292f">10.6</text>
+<rect x="436" y="518" width="35" height="21" fill="#fec95a"/>
+<text class="v" x="453.5" y="532.6" fill="#24292f">10.4</text>
+<rect x="472" y="518" width="35" height="21" fill="#fed16b"/>
+<text class="v" x="489.5" y="532.6" fill="#24292f">10.2</text>
+<rect x="508" y="518" width="35" height="21" fill="#fedc81"/>
+<text class="v" x="525.5" y="532.6" fill="#24292f">9.91</text>
+<rect x="544" y="518" width="35" height="21" fill="#fee391"/>
+<text class="v" x="561.5" y="532.6" fill="#24292f">9.70</text>
+<rect x="580" y="518" width="35" height="21" fill="#feeca5"/>
+<text class="v" x="597.5" y="532.6" fill="#24292f">9.31</text>
+<rect x="616" y="518" width="35" height="21" fill="#ffefac"/>
+<text class="v" x="633.5" y="532.6" fill="#24292f">9.18</text>
+<rect x="652" y="518" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="669.5" y="532.6" fill="#24292f">8.93</text>
+<rect x="688" y="518" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="705.5" y="532.6" fill="#24292f">8.93</text>
+<rect x="724" y="518" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="741.5" y="532.6" fill="#24292f">9.01</text>
+<rect x="760" y="518" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="777.5" y="532.6" fill="#24292f">9.01</text>
+<rect x="796" y="518" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="813.5" y="532.6" fill="#24292f">8.92</text>
+<rect x="832" y="518" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="849.5" y="532.6" fill="#24292f">8.93</text>
+<rect x="868" y="518" width="35" height="21" fill="#fff2b0"/>
+<text class="v" x="885.5" y="532.6" fill="#24292f">9.09</text>
+<rect x="904" y="518" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="921.5" y="532.6" fill="#24292f">9.00</text>
+<rect x="940" y="518" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="957.5" y="532.6" fill="#24292f">8.90</text>
+<text class="t" x="68" y="555.0" text-anchor="end">8</text>
+<rect x="76" y="540" width="35" height="21" fill="#8c2d04"/>
+<text class="v" x="93.5" y="554.6" fill="#ffffff">15.1</text>
+<rect x="112" y="540" width="35" height="21" fill="#a53903"/>
+<text class="v" x="129.5" y="554.6" fill="#ffffff">14.6</text>
+<rect x="148" y="540" width="35" height="21" fill="#bf4602"/>
+<text class="v" x="165.5" y="554.6" fill="#ffffff">14.1</text>
+<rect x="184" y="540" width="35" height="21" fill="#a03703"/>
+<text class="v" x="201.5" y="554.6" fill="#ffffff">14.7</text>
+<rect x="220" y="540" width="35" height="21" fill="#c74a02"/>
+<text class="v" x="237.5" y="554.6" fill="#ffffff">13.9</text>
+<rect x="256" y="540" width="35" height="21" fill="#d55607"/>
+<text class="v" x="273.5" y="554.6" fill="#ffffff">13.5</text>
+<rect x="292" y="540" width="35" height="21" fill="#de600c"/>
+<text class="v" x="309.5" y="554.6" fill="#ffffff">13.2</text>
+<rect x="328" y="540" width="35" height="21" fill="#df610d"/>
+<text class="v" x="345.5" y="554.6" fill="#ffffff">13.1</text>
+<rect x="364" y="540" width="35" height="21" fill="#ea6d13"/>
+<text class="v" x="381.5" y="554.6" fill="#24292f">12.8</text>
+<rect x="400" y="540" width="35" height="21" fill="#e76a11"/>
+<text class="v" x="417.5" y="554.6" fill="#ffffff">12.9</text>
+<rect x="436" y="540" width="35" height="21" fill="#ec7014"/>
+<text class="v" x="453.5" y="554.6" fill="#24292f">12.7</text>
+<rect x="472" y="540" width="35" height="21" fill="#f27e1b"/>
+<text class="v" x="489.5" y="554.6" fill="#24292f">12.3</text>
+<rect x="508" y="540" width="35" height="21" fill="#fea02f"/>
+<text class="v" x="525.5" y="554.6" fill="#24292f">11.4</text>
+<rect x="544" y="540" width="35" height="21" fill="#feac3a"/>
+<text class="v" x="561.5" y="554.6" fill="#24292f">11.1</text>
+<rect x="580" y="540" width="35" height="21" fill="#fea837"/>
+<text class="v" x="597.5" y="554.6" fill="#24292f">11.2</text>
+<rect x="616" y="540" width="35" height="21" fill="#feb844"/>
+<text class="v" x="633.5" y="554.6" fill="#24292f">10.9</text>
+<rect x="652" y="540" width="35" height="21" fill="#fec44f"/>
+<text class="v" x="669.5" y="554.6" fill="#24292f">10.6</text>
+<rect x="688" y="540" width="35" height="21" fill="#fecf66"/>
+<text class="v" x="705.5" y="554.6" fill="#24292f">10.3</text>
+<rect x="724" y="540" width="35" height="21" fill="#feda7f"/>
+<text class="v" x="741.5" y="554.6" fill="#24292f">9.94</text>
+<rect x="760" y="540" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="777.5" y="554.6" fill="#24292f">9.01</text>
+<rect x="796" y="540" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="813.5" y="554.6" fill="#24292f">8.93</text>
+<rect x="832" y="540" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="849.5" y="554.6" fill="#24292f">8.94</text>
+<rect x="868" y="540" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="885.5" y="554.6" fill="#24292f">8.95</text>
+<rect x="904" y="540" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="921.5" y="554.6" fill="#24292f">8.94</text>
+<rect x="940" y="540" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="957.5" y="554.6" fill="#24292f">8.96</text>
+<text class="t" x="93.5" y="578" text-anchor="middle">8</text>
+<text class="t" x="129.5" y="578" text-anchor="middle">9</text>
+<text class="t" x="165.5" y="578" text-anchor="middle">10</text>
+<text class="t" x="201.5" y="578" text-anchor="middle">11</text>
+<text class="t" x="237.5" y="578" text-anchor="middle">12</text>
+<text class="t" x="273.5" y="578" text-anchor="middle">13</text>
+<text class="t" x="309.5" y="578" text-anchor="middle">14</text>
+<text class="t" x="345.5" y="578" text-anchor="middle">15</text>
+<text class="t" x="381.5" y="578" text-anchor="middle">16</text>
+<text class="t" x="417.5" y="578" text-anchor="middle">17</text>
+<text class="t" x="453.5" y="578" text-anchor="middle">18</text>
+<text class="t" x="489.5" y="578" text-anchor="middle">19</text>
+<text class="t" x="525.5" y="578" text-anchor="middle">20</text>
+<text class="t" x="561.5" y="578" text-anchor="middle">21</text>
+<text class="t" x="597.5" y="578" text-anchor="middle">22</text>
+<text class="t" x="633.5" y="578" text-anchor="middle">23</text>
+<text class="t" x="669.5" y="578" text-anchor="middle">24</text>
+<text class="t" x="705.5" y="578" text-anchor="middle">25</text>
+<text class="t" x="741.5" y="578" text-anchor="middle">26</text>
+<text class="t" x="777.5" y="578" text-anchor="middle">27</text>
+<text class="t" x="813.5" y="578" text-anchor="middle">28</text>
+<text class="t" x="849.5" y="578" text-anchor="middle">29</text>
+<text class="t" x="885.5" y="578" text-anchor="middle">30</text>
+<text class="t" x="921.5" y="578" text-anchor="middle">31</text>
+<text class="t" x="957.5" y="578" text-anchor="middle">32</text>
+<text class="a" x="526.0" y="606">VRAM (GiB)</text>
+<text class="a" x="18" y="287.0" transform="rotate(-90 18 287.0)">System memory (GiB)</text>
+</svg>
diff --git a/docs/assets/capacity-speed.zh.svg b/docs/assets/capacity-speed.zh.svg
new file mode 100644
index 0000000..964d72c
--- /dev/null
+++ b/docs/assets/capacity-speed.zh.svg
@@ -0,0 +1,1306 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="988" height="620" viewBox="0 0 988 620">
+<style>text{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI','Noto Sans','PingFang SC','Microsoft YaHei','Noto Sans CJK SC',Helvetica,Arial,sans-serif}.v{font-size:10.5px;text-anchor:middle}.t{font-size:11px;fill:#57606a}.a{font-size:12.5px;fill:#24292f;text-anchor:middle}</style>
+<rect width="988" height="620" fill="#ffffff"/>
+<text class="t" x="68" y="27.0" text-anchor="end">32</text>
+<rect x="76" y="12" width="35" height="21" fill="#ec7014"/>
+<text class="v" x="93.5" y="26.6" fill="#24292f">12.7</text>
+<rect x="112" y="12" width="35" height="21" fill="#fed471"/>
+<text class="v" x="129.5" y="26.6" fill="#24292f">10.1</text>
+<rect x="148" y="12" width="35" height="21" fill="#fed675"/>
+<text class="v" x="165.5" y="26.6" fill="#24292f">10.1</text>
+<rect x="184" y="12" width="35" height="21" fill="#ffefaa"/>
+<text class="v" x="201.5" y="26.6" fill="#24292f">9.21</text>
+<rect x="220" y="12" width="35" height="21" fill="#ffeea9"/>
+<text class="v" x="237.5" y="26.6" fill="#24292f">9.23</text>
+<rect x="256" y="12" width="35" height="21" fill="#ffeea8"/>
+<text class="v" x="273.5" y="26.6" fill="#24292f">9.25</text>
+<rect x="292" y="12" width="35" height="21" fill="#ffeea8"/>
+<text class="v" x="309.5" y="26.6" fill="#24292f">9.24</text>
+<rect x="328" y="12" width="35" height="21" fill="#fff6bb"/>
+<text class="v" x="345.5" y="26.6" fill="#24292f">8.90</text>
+<rect x="364" y="12" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="381.5" y="26.6" fill="#24292f">9.02</text>
+<rect x="400" y="12" width="35" height="21" fill="#fff1b0"/>
+<text class="v" x="417.5" y="26.6" fill="#24292f">9.10</text>
+<rect x="436" y="12" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="453.5" y="26.6" fill="#24292f">9.00</text>
+<rect x="472" y="12" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="489.5" y="26.6" fill="#24292f">9.08</text>
+<rect x="508" y="12" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="525.5" y="26.6" fill="#24292f">9.00</text>
+<rect x="544" y="12" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="561.5" y="26.6" fill="#24292f">8.95</text>
+<rect x="580" y="12" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="597.5" y="26.6" fill="#24292f">8.96</text>
+<rect x="616" y="12" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="633.5" y="26.6" fill="#24292f">8.95</text>
+<rect x="652" y="12" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="669.5" y="26.6" fill="#24292f">9.01</text>
+<rect x="688" y="12" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="705.5" y="26.6" fill="#24292f">8.97</text>
+<rect x="724" y="12" width="35" height="21" fill="#fff7bb"/>
+<text class="v" x="741.5" y="26.6" fill="#24292f">8.89</text>
+<rect x="760" y="12" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="777.5" y="26.6" fill="#24292f">9.01</text>
+<rect x="796" y="12" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="813.5" y="26.6" fill="#24292f">9.01</text>
+<rect x="832" y="12" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="849.5" y="26.6" fill="#24292f">9.02</text>
+<rect x="868" y="12" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="885.5" y="26.6" fill="#24292f">8.94</text>
+<rect x="904" y="12" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="921.5" y="26.6" fill="#24292f">8.98</text>
+<rect x="940" y="12" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="957.5" y="26.6" fill="#24292f">8.93</text>
+<text class="t" x="68" y="49.0" text-anchor="end">31</text>
+<rect x="76" y="34" width="35" height="21" fill="#e66a11"/>
+<text class="v" x="93.5" y="48.6" fill="#ffffff">12.9</text>
+<rect x="112" y="34" width="35" height="21" fill="#fed676"/>
+<text class="v" x="129.5" y="48.6" fill="#24292f">10.1</text>
+<rect x="148" y="34" width="35" height="21" fill="#fed674"/>
+<text class="v" x="165.5" y="48.6" fill="#24292f">10.1</text>
+<rect x="184" y="34" width="35" height="21" fill="#fff0ac"/>
+<text class="v" x="201.5" y="48.6" fill="#24292f">9.17</text>
+<rect x="220" y="34" width="35" height="21" fill="#feeca4"/>
+<text class="v" x="237.5" y="48.6" fill="#24292f">9.32</text>
+<rect x="256" y="34" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="273.5" y="48.6" fill="#24292f">9.15</text>
+<rect x="292" y="34" width="35" height="21" fill="#ffefaa"/>
+<text class="v" x="309.5" y="48.6" fill="#24292f">9.21</text>
+<rect x="328" y="34" width="35" height="21" fill="#fff1b0"/>
+<text class="v" x="345.5" y="48.6" fill="#24292f">9.09</text>
+<rect x="364" y="34" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="381.5" y="48.6" fill="#24292f">8.95</text>
+<rect x="400" y="34" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="417.5" y="48.6" fill="#24292f">8.93</text>
+<rect x="436" y="34" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="453.5" y="48.6" fill="#24292f">8.96</text>
+<rect x="472" y="34" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="489.5" y="48.6" fill="#24292f">9.09</text>
+<rect x="508" y="34" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="525.5" y="48.6" fill="#24292f">9.04</text>
+<rect x="544" y="34" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="561.5" y="48.6" fill="#24292f">8.91</text>
+<rect x="580" y="34" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="597.5" y="48.6" fill="#24292f">8.94</text>
+<rect x="616" y="34" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="633.5" y="48.6" fill="#24292f">8.94</text>
+<rect x="652" y="34" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="669.5" y="48.6" fill="#24292f">8.94</text>
+<rect x="688" y="34" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="705.5" y="48.6" fill="#24292f">8.97</text>
+<rect x="724" y="34" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="741.5" y="48.6" fill="#24292f">9.01</text>
+<rect x="760" y="34" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="777.5" y="48.6" fill="#24292f">9.08</text>
+<rect x="796" y="34" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="813.5" y="48.6" fill="#24292f">9.02</text>
+<rect x="832" y="34" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="849.5" y="48.6" fill="#24292f">8.98</text>
+<rect x="868" y="34" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="885.5" y="48.6" fill="#24292f">9.06</text>
+<rect x="904" y="34" width="35" height="21" fill="#fff3b2"/>
+<text class="v" x="921.5" y="48.6" fill="#24292f">9.05</text>
+<rect x="940" y="34" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="957.5" y="48.6" fill="#24292f">9.00</text>
+<text class="t" x="68" y="71.0" text-anchor="end">30</text>
+<rect x="76" y="56" width="35" height="21" fill="#ed7215"/>
+<text class="v" x="93.5" y="70.6" fill="#24292f">12.6</text>
+<rect x="112" y="56" width="35" height="21" fill="#fed069"/>
+<text class="v" x="129.5" y="70.6" fill="#24292f">10.2</text>
+<rect x="148" y="56" width="35" height="21" fill="#fed675"/>
+<text class="v" x="165.5" y="70.6" fill="#24292f">10.1</text>
+<rect x="184" y="56" width="35" height="21" fill="#ffeea9"/>
+<text class="v" x="201.5" y="70.6" fill="#24292f">9.24</text>
+<rect x="220" y="56" width="35" height="21" fill="#ffefaa"/>
+<text class="v" x="237.5" y="70.6" fill="#24292f">9.21</text>
+<rect x="256" y="56" width="35" height="21" fill="#ffefaa"/>
+<text class="v" x="273.5" y="70.6" fill="#24292f">9.21</text>
+<rect x="292" y="56" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="309.5" y="70.6" fill="#24292f">9.16</text>
+<rect x="328" y="56" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="345.5" y="70.6" fill="#24292f">8.97</text>
+<rect x="364" y="56" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="381.5" y="70.6" fill="#24292f">9.08</text>
+<rect x="400" y="56" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="417.5" y="70.6" fill="#24292f">8.92</text>
+<rect x="436" y="56" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="453.5" y="70.6" fill="#24292f">8.93</text>
+<rect x="472" y="56" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="489.5" y="70.6" fill="#24292f">8.95</text>
+<rect x="508" y="56" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="525.5" y="70.6" fill="#24292f">9.01</text>
+<rect x="544" y="56" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="561.5" y="70.6" fill="#24292f">8.97</text>
+<rect x="580" y="56" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="597.5" y="70.6" fill="#24292f">9.02</text>
+<rect x="616" y="56" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="633.5" y="70.6" fill="#24292f">9.04</text>
+<rect x="652" y="56" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="669.5" y="70.6" fill="#24292f">8.96</text>
+<rect x="688" y="56" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="705.5" y="70.6" fill="#24292f">8.97</text>
+<rect x="724" y="56" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="741.5" y="70.6" fill="#24292f">9.02</text>
+<rect x="760" y="56" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="777.5" y="70.6" fill="#24292f">8.98</text>
+<rect x="796" y="56" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="813.5" y="70.6" fill="#24292f">8.94</text>
+<rect x="832" y="56" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="849.5" y="70.6" fill="#24292f">9.07</text>
+<rect x="868" y="56" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="885.5" y="70.6" fill="#24292f">8.92</text>
+<rect x="904" y="56" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="921.5" y="70.6" fill="#24292f">9.04</text>
+<rect x="940" y="56" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="957.5" y="70.6" fill="#24292f">9.06</text>
+<text class="t" x="68" y="93.0" text-anchor="end">29</text>
+<rect x="76" y="78" width="35" height="21" fill="#e86c12"/>
+<text class="v" x="93.5" y="92.6" fill="#24292f">12.8</text>
+<rect x="112" y="78" width="35" height="21" fill="#fed574"/>
+<text class="v" x="129.5" y="92.6" fill="#24292f">10.1</text>
+<rect x="148" y="78" width="35" height="21" fill="#fed676"/>
+<text class="v" x="165.5" y="92.6" fill="#24292f">10.1</text>
+<rect x="184" y="78" width="35" height="21" fill="#feeda5"/>
+<text class="v" x="201.5" y="92.6" fill="#24292f">9.30</text>
+<rect x="220" y="78" width="35" height="21" fill="#ffefab"/>
+<text class="v" x="237.5" y="92.6" fill="#24292f">9.20</text>
+<rect x="256" y="78" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="273.5" y="92.6" fill="#24292f">9.15</text>
+<rect x="292" y="78" width="35" height="21" fill="#fff2b0"/>
+<text class="v" x="309.5" y="92.6" fill="#24292f">9.09</text>
+<rect x="328" y="78" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="345.5" y="92.6" fill="#24292f">8.99</text>
+<rect x="364" y="78" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="381.5" y="92.6" fill="#24292f">8.91</text>
+<rect x="400" y="78" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="417.5" y="92.6" fill="#24292f">8.96</text>
+<rect x="436" y="78" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="453.5" y="92.6" fill="#24292f">8.94</text>
+<rect x="472" y="78" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="489.5" y="92.6" fill="#24292f">8.94</text>
+<rect x="508" y="78" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="525.5" y="92.6" fill="#24292f">9.08</text>
+<rect x="544" y="78" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="561.5" y="92.6" fill="#24292f">8.91</text>
+<rect x="580" y="78" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="597.5" y="92.6" fill="#24292f">8.97</text>
+<rect x="616" y="78" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="633.5" y="92.6" fill="#24292f">8.97</text>
+<rect x="652" y="78" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="669.5" y="92.6" fill="#24292f">9.03</text>
+<rect x="688" y="78" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="705.5" y="92.6" fill="#24292f">8.97</text>
+<rect x="724" y="78" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="741.5" y="92.6" fill="#24292f">9.04</text>
+<rect x="760" y="78" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="777.5" y="92.6" fill="#24292f">8.97</text>
+<rect x="796" y="78" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="813.5" y="92.6" fill="#24292f">8.97</text>
+<rect x="832" y="78" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="849.5" y="92.6" fill="#24292f">8.97</text>
+<rect x="868" y="78" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="885.5" y="92.6" fill="#24292f">9.00</text>
+<rect x="904" y="78" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="921.5" y="92.6" fill="#24292f">9.06</text>
+<rect x="940" y="78" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="957.5" y="92.6" fill="#24292f">8.95</text>
+<text class="t" x="68" y="115.0" text-anchor="end">28</text>
+<rect x="76" y="100" width="35" height="21" fill="#de610c"/>
+<text class="v" x="93.5" y="114.6" fill="#ffffff">13.2</text>
+<rect x="112" y="100" width="35" height="21" fill="#fed068"/>
+<text class="v" x="129.5" y="114.6" fill="#24292f">10.3</text>
+<rect x="148" y="100" width="35" height="21" fill="#fed573"/>
+<text class="v" x="165.5" y="114.6" fill="#24292f">10.1</text>
+<rect x="184" y="100" width="35" height="21" fill="#feeda6"/>
+<text class="v" x="201.5" y="114.6" fill="#24292f">9.28</text>
+<rect x="220" y="100" width="35" height="21" fill="#ffefaa"/>
+<text class="v" x="237.5" y="114.6" fill="#24292f">9.21</text>
+<rect x="256" y="100" width="35" height="21" fill="#ffeea8"/>
+<text class="v" x="273.5" y="114.6" fill="#24292f">9.24</text>
+<rect x="292" y="100" width="35" height="21" fill="#ffeea9"/>
+<text class="v" x="309.5" y="114.6" fill="#24292f">9.24</text>
+<rect x="328" y="100" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="345.5" y="114.6" fill="#24292f">9.15</text>
+<rect x="364" y="100" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="381.5" y="114.6" fill="#24292f">8.94</text>
+<rect x="400" y="100" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="417.5" y="114.6" fill="#24292f">9.02</text>
+<rect x="436" y="100" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="453.5" y="114.6" fill="#24292f">9.01</text>
+<rect x="472" y="100" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="489.5" y="114.6" fill="#24292f">8.96</text>
+<rect x="508" y="100" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="525.5" y="114.6" fill="#24292f">8.92</text>
+<rect x="544" y="100" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="561.5" y="114.6" fill="#24292f">9.02</text>
+<rect x="580" y="100" width="35" height="21" fill="#fff4b4"/>
+<text class="v" x="597.5" y="114.6" fill="#24292f">9.01</text>
+<rect x="616" y="100" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="633.5" y="114.6" fill="#24292f">8.99</text>
+<rect x="652" y="100" width="35" height="21" fill="#fff4b4"/>
+<text class="v" x="669.5" y="114.6" fill="#24292f">9.01</text>
+<rect x="688" y="100" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="705.5" y="114.6" fill="#24292f">8.97</text>
+<rect x="724" y="100" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="741.5" y="114.6" fill="#24292f">9.09</text>
+<rect x="760" y="100" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="777.5" y="114.6" fill="#24292f">9.03</text>
+<rect x="796" y="100" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="813.5" y="114.6" fill="#24292f">9.02</text>
+<rect x="832" y="100" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="849.5" y="114.6" fill="#24292f">8.93</text>
+<rect x="868" y="100" width="35" height="21" fill="#fff6bb"/>
+<text class="v" x="885.5" y="114.6" fill="#24292f">8.89</text>
+<rect x="904" y="100" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="921.5" y="114.6" fill="#24292f">9.00</text>
+<rect x="940" y="100" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="957.5" y="114.6" fill="#24292f">8.93</text>
+<text class="t" x="68" y="137.0" text-anchor="end">27</text>
+<rect x="76" y="122" width="35" height="21" fill="#e96d13"/>
+<text class="v" x="93.5" y="136.6" fill="#24292f">12.8</text>
+<rect x="112" y="122" width="35" height="21" fill="#fed26d"/>
+<text class="v" x="129.5" y="136.6" fill="#24292f">10.2</text>
+<rect x="148" y="122" width="35" height="21" fill="#fed675"/>
+<text class="v" x="165.5" y="136.6" fill="#24292f">10.1</text>
+<rect x="184" y="122" width="35" height="21" fill="#ffeda7"/>
+<text class="v" x="201.5" y="136.6" fill="#24292f">9.26</text>
+<rect x="220" y="122" width="35" height="21" fill="#feeda6"/>
+<text class="v" x="237.5" y="136.6" fill="#24292f">9.28</text>
+<rect x="256" y="122" width="35" height="21" fill="#ffeea9"/>
+<text class="v" x="273.5" y="136.6" fill="#24292f">9.22</text>
+<rect x="292" y="122" width="35" height="21" fill="#ffefab"/>
+<text class="v" x="309.5" y="136.6" fill="#24292f">9.18</text>
+<rect x="328" y="122" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="345.5" y="136.6" fill="#24292f">9.00</text>
+<rect x="364" y="122" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="381.5" y="136.6" fill="#24292f">8.99</text>
+<rect x="400" y="122" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="417.5" y="136.6" fill="#24292f">8.94</text>
+<rect x="436" y="122" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="453.5" y="136.6" fill="#24292f">8.96</text>
+<rect x="472" y="122" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="489.5" y="136.6" fill="#24292f">9.02</text>
+<rect x="508" y="122" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="525.5" y="136.6" fill="#24292f">8.99</text>
+<rect x="544" y="122" width="35" height="21" fill="#fff2b0"/>
+<text class="v" x="561.5" y="136.6" fill="#24292f">9.09</text>
+<rect x="580" y="122" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="597.5" y="136.6" fill="#24292f">8.99</text>
+<rect x="616" y="122" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="633.5" y="136.6" fill="#24292f">8.97</text>
+<rect x="652" y="122" width="35" height="21" fill="#fff1b0"/>
+<text class="v" x="669.5" y="136.6" fill="#24292f">9.10</text>
+<rect x="688" y="122" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="705.5" y="136.6" fill="#24292f">9.03</text>
+<rect x="724" y="122" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="741.5" y="136.6" fill="#24292f">8.95</text>
+<rect x="760" y="122" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="777.5" y="136.6" fill="#24292f">8.99</text>
+<rect x="796" y="122" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="813.5" y="136.6" fill="#24292f">9.04</text>
+<rect x="832" y="122" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="849.5" y="136.6" fill="#24292f">9.01</text>
+<rect x="868" y="122" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="885.5" y="136.6" fill="#24292f">9.00</text>
+<rect x="904" y="122" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="921.5" y="136.6" fill="#24292f">9.03</text>
+<rect x="940" y="122" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="957.5" y="136.6" fill="#24292f">9.04</text>
+<text class="t" x="68" y="159.0" text-anchor="end">26</text>
+<rect x="76" y="144" width="35" height="21" fill="#e76a11"/>
+<text class="v" x="93.5" y="158.6" fill="#ffffff">12.9</text>
+<rect x="112" y="144" width="35" height="21" fill="#fed069"/>
+<text class="v" x="129.5" y="158.6" fill="#24292f">10.2</text>
+<rect x="148" y="144" width="35" height="21" fill="#fed26d"/>
+<text class="v" x="165.5" y="158.6" fill="#24292f">10.2</text>
+<rect x="184" y="144" width="35" height="21" fill="#ffefab"/>
+<text class="v" x="201.5" y="158.6" fill="#24292f">9.19</text>
+<rect x="220" y="144" width="35" height="21" fill="#ffefac"/>
+<text class="v" x="237.5" y="158.6" fill="#24292f">9.18</text>
+<rect x="256" y="144" width="35" height="21" fill="#ffefac"/>
+<text class="v" x="273.5" y="158.6" fill="#24292f">9.18</text>
+<rect x="292" y="144" width="35" height="21" fill="#fff0ae"/>
+<text class="v" x="309.5" y="158.6" fill="#24292f">9.13</text>
+<rect x="328" y="144" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="345.5" y="158.6" fill="#24292f">8.94</text>
+<rect x="364" y="144" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="381.5" y="158.6" fill="#24292f">8.97</text>
+<rect x="400" y="144" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="417.5" y="158.6" fill="#24292f">8.96</text>
+<rect x="436" y="144" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="453.5" y="158.6" fill="#24292f">8.97</text>
+<rect x="472" y="144" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="489.5" y="158.6" fill="#24292f">8.93</text>
+<rect x="508" y="144" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="525.5" y="158.6" fill="#24292f">8.93</text>
+<rect x="544" y="144" width="35" height="21" fill="#fff2b0"/>
+<text class="v" x="561.5" y="158.6" fill="#24292f">9.09</text>
+<rect x="580" y="144" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="597.5" y="158.6" fill="#24292f">8.95</text>
+<rect x="616" y="144" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="633.5" y="158.6" fill="#24292f">9.01</text>
+<rect x="652" y="144" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="669.5" y="158.6" fill="#24292f">8.92</text>
+<rect x="688" y="144" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="705.5" y="158.6" fill="#24292f">9.03</text>
+<rect x="724" y="144" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="741.5" y="158.6" fill="#24292f">8.95</text>
+<rect x="760" y="144" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="777.5" y="158.6" fill="#24292f">8.98</text>
+<rect x="796" y="144" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="813.5" y="158.6" fill="#24292f">9.06</text>
+<rect x="832" y="144" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="849.5" y="158.6" fill="#24292f">8.91</text>
+<rect x="868" y="144" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="885.5" y="158.6" fill="#24292f">9.02</text>
+<rect x="904" y="144" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="921.5" y="158.6" fill="#24292f">9.04</text>
+<rect x="940" y="144" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="957.5" y="158.6" fill="#24292f">8.90</text>
+<text class="t" x="68" y="181.0" text-anchor="end">25</text>
+<rect x="76" y="166" width="35" height="21" fill="#ec7014"/>
+<text class="v" x="93.5" y="180.6" fill="#24292f">12.7</text>
+<rect x="112" y="166" width="35" height="21" fill="#fed16a"/>
+<text class="v" x="129.5" y="180.6" fill="#24292f">10.2</text>
+<rect x="148" y="166" width="35" height="21" fill="#fed26e"/>
+<text class="v" x="165.5" y="180.6" fill="#24292f">10.2</text>
+<rect x="184" y="166" width="35" height="21" fill="#feeca4"/>
+<text class="v" x="201.5" y="180.6" fill="#24292f">9.34</text>
+<rect x="220" y="166" width="35" height="21" fill="#ffeea8"/>
+<text class="v" x="237.5" y="180.6" fill="#24292f">9.25</text>
+<rect x="256" y="166" width="35" height="21" fill="#fff1af"/>
+<text class="v" x="273.5" y="180.6" fill="#24292f">9.12</text>
+<rect x="292" y="166" width="35" height="21" fill="#feeda6"/>
+<text class="v" x="309.5" y="180.6" fill="#24292f">9.28</text>
+<rect x="328" y="166" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="345.5" y="180.6" fill="#24292f">8.95</text>
+<rect x="364" y="166" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="381.5" y="180.6" fill="#24292f">8.93</text>
+<rect x="400" y="166" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="417.5" y="180.6" fill="#24292f">8.94</text>
+<rect x="436" y="166" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="453.5" y="180.6" fill="#24292f">8.93</text>
+<rect x="472" y="166" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="489.5" y="180.6" fill="#24292f">8.97</text>
+<rect x="508" y="166" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="525.5" y="180.6" fill="#24292f">8.98</text>
+<rect x="544" y="166" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="561.5" y="180.6" fill="#24292f">8.94</text>
+<rect x="580" y="166" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="597.5" y="180.6" fill="#24292f">8.96</text>
+<rect x="616" y="166" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="633.5" y="180.6" fill="#24292f">9.02</text>
+<rect x="652" y="166" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="669.5" y="180.6" fill="#24292f">8.96</text>
+<rect x="688" y="166" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="705.5" y="180.6" fill="#24292f">9.08</text>
+<rect x="724" y="166" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="741.5" y="180.6" fill="#24292f">9.04</text>
+<rect x="760" y="166" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="777.5" y="180.6" fill="#24292f">9.03</text>
+<rect x="796" y="166" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="813.5" y="180.6" fill="#24292f">9.01</text>
+<rect x="832" y="166" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="849.5" y="180.6" fill="#24292f">9.03</text>
+<rect x="868" y="166" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="885.5" y="180.6" fill="#24292f">9.03</text>
+<rect x="904" y="166" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="921.5" y="180.6" fill="#24292f">9.03</text>
+<rect x="940" y="166" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="957.5" y="180.6" fill="#24292f">8.94</text>
+<text class="t" x="68" y="203.0" text-anchor="end">24</text>
+<rect x="76" y="188" width="35" height="21" fill="#ec7114"/>
+<text class="v" x="93.5" y="202.6" fill="#24292f">12.6</text>
+<rect x="112" y="188" width="35" height="21" fill="#fed573"/>
+<text class="v" x="129.5" y="202.6" fill="#24292f">10.1</text>
+<rect x="148" y="188" width="35" height="21" fill="#fed675"/>
+<text class="v" x="165.5" y="202.6" fill="#24292f">10.1</text>
+<rect x="184" y="188" width="35" height="21" fill="#feeca5"/>
+<text class="v" x="201.5" y="202.6" fill="#24292f">9.31</text>
+<rect x="220" y="188" width="35" height="21" fill="#ffefac"/>
+<text class="v" x="237.5" y="202.6" fill="#24292f">9.18</text>
+<rect x="256" y="188" width="35" height="21" fill="#ffeea8"/>
+<text class="v" x="273.5" y="202.6" fill="#24292f">9.25</text>
+<rect x="292" y="188" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="309.5" y="202.6" fill="#24292f">9.15</text>
+<rect x="328" y="188" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="345.5" y="202.6" fill="#24292f">8.94</text>
+<rect x="364" y="188" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="381.5" y="202.6" fill="#24292f">8.92</text>
+<rect x="400" y="188" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="417.5" y="202.6" fill="#24292f">9.03</text>
+<rect x="436" y="188" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="453.5" y="202.6" fill="#24292f">9.01</text>
+<rect x="472" y="188" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="489.5" y="202.6" fill="#24292f">8.92</text>
+<rect x="508" y="188" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="525.5" y="202.6" fill="#24292f">8.94</text>
+<rect x="544" y="188" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="561.5" y="202.6" fill="#24292f">9.01</text>
+<rect x="580" y="188" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="597.5" y="202.6" fill="#24292f">9.03</text>
+<rect x="616" y="188" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="633.5" y="202.6" fill="#24292f">8.98</text>
+<rect x="652" y="188" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="669.5" y="202.6" fill="#24292f">8.95</text>
+<rect x="688" y="188" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="705.5" y="202.6" fill="#24292f">8.92</text>
+<rect x="724" y="188" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="741.5" y="202.6" fill="#24292f">8.97</text>
+<rect x="760" y="188" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="777.5" y="202.6" fill="#24292f">9.02</text>
+<rect x="796" y="188" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="813.5" y="202.6" fill="#24292f">8.97</text>
+<rect x="832" y="188" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="849.5" y="202.6" fill="#24292f">9.02</text>
+<rect x="868" y="188" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="885.5" y="202.6" fill="#24292f">8.98</text>
+<rect x="904" y="188" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="921.5" y="202.6" fill="#24292f">8.99</text>
+<rect x="940" y="188" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="957.5" y="202.6" fill="#24292f">8.99</text>
+<text class="t" x="68" y="225.0" text-anchor="end">23</text>
+<rect x="76" y="210" width="35" height="21" fill="#e56810"/>
+<text class="v" x="93.5" y="224.6" fill="#ffffff">12.9</text>
+<rect x="112" y="210" width="35" height="21" fill="#fece64"/>
+<text class="v" x="129.5" y="224.6" fill="#24292f">10.3</text>
+<rect x="148" y="210" width="35" height="21" fill="#fecc61"/>
+<text class="v" x="165.5" y="224.6" fill="#24292f">10.4</text>
+<rect x="184" y="210" width="35" height="21" fill="#fee89d"/>
+<text class="v" x="201.5" y="224.6" fill="#24292f">9.47</text>
+<rect x="220" y="210" width="35" height="21" fill="#ffeda7"/>
+<text class="v" x="237.5" y="224.6" fill="#24292f">9.27</text>
+<rect x="256" y="210" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="273.5" y="224.6" fill="#24292f">9.16</text>
+<rect x="292" y="210" width="35" height="21" fill="#fff0ac"/>
+<text class="v" x="309.5" y="224.6" fill="#24292f">9.17</text>
+<rect x="328" y="210" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="345.5" y="224.6" fill="#24292f">9.02</text>
+<rect x="364" y="210" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="381.5" y="224.6" fill="#24292f">9.03</text>
+<rect x="400" y="210" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="417.5" y="224.6" fill="#24292f">8.92</text>
+<rect x="436" y="210" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="453.5" y="224.6" fill="#24292f">9.00</text>
+<rect x="472" y="210" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="489.5" y="224.6" fill="#24292f">9.02</text>
+<rect x="508" y="210" width="35" height="21" fill="#fff4b4"/>
+<text class="v" x="525.5" y="224.6" fill="#24292f">9.01</text>
+<rect x="544" y="210" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="561.5" y="224.6" fill="#24292f">8.95</text>
+<rect x="580" y="210" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="597.5" y="224.6" fill="#24292f">9.00</text>
+<rect x="616" y="210" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="633.5" y="224.6" fill="#24292f">8.96</text>
+<rect x="652" y="210" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="669.5" y="224.6" fill="#24292f">9.03</text>
+<rect x="688" y="210" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="705.5" y="224.6" fill="#24292f">8.95</text>
+<rect x="724" y="210" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="741.5" y="224.6" fill="#24292f">8.95</text>
+<rect x="760" y="210" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="777.5" y="224.6" fill="#24292f">8.93</text>
+<rect x="796" y="210" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="813.5" y="224.6" fill="#24292f">8.97</text>
+<rect x="832" y="210" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="849.5" y="224.6" fill="#24292f">9.06</text>
+<rect x="868" y="210" width="35" height="21" fill="#fff1b0"/>
+<text class="v" x="885.5" y="224.6" fill="#24292f">9.10</text>
+<rect x="904" y="210" width="35" height="21" fill="#fff1b0"/>
+<text class="v" x="921.5" y="224.6" fill="#24292f">9.10</text>
+<rect x="940" y="210" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="957.5" y="224.6" fill="#24292f">8.99</text>
+<text class="t" x="68" y="247.0" text-anchor="end">22</text>
+<rect x="76" y="232" width="35" height="21" fill="#e56810"/>
+<text class="v" x="93.5" y="246.6" fill="#ffffff">12.9</text>
+<rect x="112" y="232" width="35" height="21" fill="#fecc61"/>
+<text class="v" x="129.5" y="246.6" fill="#24292f">10.4</text>
+<rect x="148" y="232" width="35" height="21" fill="#fed26c"/>
+<text class="v" x="165.5" y="246.6" fill="#24292f">10.2</text>
+<rect x="184" y="232" width="35" height="21" fill="#fee89b"/>
+<text class="v" x="201.5" y="246.6" fill="#24292f">9.51</text>
+<rect x="220" y="232" width="35" height="21" fill="#feeba2"/>
+<text class="v" x="237.5" y="246.6" fill="#24292f">9.36</text>
+<rect x="256" y="232" width="35" height="21" fill="#fff0ae"/>
+<text class="v" x="273.5" y="246.6" fill="#24292f">9.14</text>
+<rect x="292" y="232" width="35" height="21" fill="#ffefab"/>
+<text class="v" x="309.5" y="246.6" fill="#24292f">9.19</text>
+<rect x="328" y="232" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="345.5" y="246.6" fill="#24292f">8.97</text>
+<rect x="364" y="232" width="35" height="21" fill="#fff4b4"/>
+<text class="v" x="381.5" y="246.6" fill="#24292f">9.01</text>
+<rect x="400" y="232" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="417.5" y="246.6" fill="#24292f">9.00</text>
+<rect x="436" y="232" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="453.5" y="246.6" fill="#24292f">8.92</text>
+<rect x="472" y="232" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="489.5" y="246.6" fill="#24292f">9.02</text>
+<rect x="508" y="232" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="525.5" y="246.6" fill="#24292f">8.96</text>
+<rect x="544" y="232" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="561.5" y="246.6" fill="#24292f">8.94</text>
+<rect x="580" y="232" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="597.5" y="246.6" fill="#24292f">9.07</text>
+<rect x="616" y="232" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="633.5" y="246.6" fill="#24292f">9.01</text>
+<rect x="652" y="232" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="669.5" y="246.6" fill="#24292f">8.95</text>
+<rect x="688" y="232" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="705.5" y="246.6" fill="#24292f">9.02</text>
+<rect x="724" y="232" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="741.5" y="246.6" fill="#24292f">8.96</text>
+<rect x="760" y="232" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="777.5" y="246.6" fill="#24292f">8.95</text>
+<rect x="796" y="232" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="813.5" y="246.6" fill="#24292f">8.96</text>
+<rect x="832" y="232" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="849.5" y="246.6" fill="#24292f">9.02</text>
+<rect x="868" y="232" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="885.5" y="246.6" fill="#24292f">8.95</text>
+<rect x="904" y="232" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="921.5" y="246.6" fill="#24292f">8.93</text>
+<rect x="940" y="232" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="957.5" y="246.6" fill="#24292f">9.04</text>
+<text class="t" x="68" y="269.0" text-anchor="end">21</text>
+<rect x="76" y="254" width="35" height="21" fill="#e2650f"/>
+<text class="v" x="93.5" y="268.6" fill="#ffffff">13.0</text>
+<rect x="112" y="254" width="35" height="21" fill="#fecb5d"/>
+<text class="v" x="129.5" y="268.6" fill="#24292f">10.4</text>
+<rect x="148" y="254" width="35" height="21" fill="#fecd62"/>
+<text class="v" x="165.5" y="268.6" fill="#24292f">10.3</text>
+<rect x="184" y="254" width="35" height="21" fill="#fee697"/>
+<text class="v" x="201.5" y="268.6" fill="#24292f">9.57</text>
+<rect x="220" y="254" width="35" height="21" fill="#feea9f"/>
+<text class="v" x="237.5" y="268.6" fill="#24292f">9.42</text>
+<rect x="256" y="254" width="35" height="21" fill="#ffeda7"/>
+<text class="v" x="273.5" y="268.6" fill="#24292f">9.27</text>
+<rect x="292" y="254" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="309.5" y="268.6" fill="#24292f">9.16</text>
+<rect x="328" y="254" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="345.5" y="268.6" fill="#24292f">9.02</text>
+<rect x="364" y="254" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="381.5" y="268.6" fill="#24292f">8.94</text>
+<rect x="400" y="254" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="417.5" y="268.6" fill="#24292f">8.90</text>
+<rect x="436" y="254" width="35" height="21" fill="#fff7bb"/>
+<text class="v" x="453.5" y="268.6" fill="#24292f">8.88</text>
+<rect x="472" y="254" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="489.5" y="268.6" fill="#24292f">8.96</text>
+<rect x="508" y="254" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="525.5" y="268.6" fill="#24292f">9.01</text>
+<rect x="544" y="254" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="561.5" y="268.6" fill="#24292f">9.02</text>
+<rect x="580" y="254" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="597.5" y="268.6" fill="#24292f">8.94</text>
+<rect x="616" y="254" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="633.5" y="268.6" fill="#24292f">8.90</text>
+<rect x="652" y="254" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="669.5" y="268.6" fill="#24292f">8.98</text>
+<rect x="688" y="254" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="705.5" y="268.6" fill="#24292f">8.98</text>
+<rect x="724" y="254" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="741.5" y="268.6" fill="#24292f">8.95</text>
+<rect x="760" y="254" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="777.5" y="268.6" fill="#24292f">9.01</text>
+<rect x="796" y="254" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="813.5" y="268.6" fill="#24292f">8.95</text>
+<rect x="832" y="254" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="849.5" y="268.6" fill="#24292f">8.90</text>
+<rect x="868" y="254" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="885.5" y="268.6" fill="#24292f">8.97</text>
+<rect x="904" y="254" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="921.5" y="268.6" fill="#24292f">8.96</text>
+<rect x="940" y="254" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="957.5" y="268.6" fill="#24292f">9.02</text>
+<text class="t" x="68" y="291.0" text-anchor="end">20</text>
+<rect x="76" y="276" width="35" height="21" fill="#dc5e0b"/>
+<text class="v" x="93.5" y="290.6" fill="#ffffff">13.2</text>
+<rect x="112" y="276" width="35" height="21" fill="#febe49"/>
+<text class="v" x="129.5" y="290.6" fill="#24292f">10.7</text>
+<rect x="148" y="276" width="35" height="21" fill="#fec857"/>
+<text class="v" x="165.5" y="290.6" fill="#24292f">10.5</text>
+<rect x="184" y="276" width="35" height="21" fill="#fedf87"/>
+<text class="v" x="201.5" y="290.6" fill="#24292f">9.82</text>
+<rect x="220" y="276" width="35" height="21" fill="#fee18d"/>
+<text class="v" x="237.5" y="290.6" fill="#24292f">9.75</text>
+<rect x="256" y="276" width="35" height="21" fill="#fee79a"/>
+<text class="v" x="273.5" y="290.6" fill="#24292f">9.52</text>
+<rect x="292" y="276" width="35" height="21" fill="#feeba3"/>
+<text class="v" x="309.5" y="290.6" fill="#24292f">9.35</text>
+<rect x="328" y="276" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="345.5" y="290.6" fill="#24292f">8.93</text>
+<rect x="364" y="276" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="381.5" y="290.6" fill="#24292f">9.01</text>
+<rect x="400" y="276" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="417.5" y="290.6" fill="#24292f">8.99</text>
+<rect x="436" y="276" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="453.5" y="290.6" fill="#24292f">8.92</text>
+<rect x="472" y="276" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="489.5" y="290.6" fill="#24292f">8.91</text>
+<rect x="508" y="276" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="525.5" y="290.6" fill="#24292f">9.02</text>
+<rect x="544" y="276" width="35" height="21" fill="#fff4b7"/>
+<text class="v" x="561.5" y="290.6" fill="#24292f">8.97</text>
+<rect x="580" y="276" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="597.5" y="290.6" fill="#24292f">8.94</text>
+<rect x="616" y="276" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="633.5" y="290.6" fill="#24292f">8.94</text>
+<rect x="652" y="276" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="669.5" y="290.6" fill="#24292f">9.03</text>
+<rect x="688" y="276" width="35" height="21" fill="#fff1ae"/>
+<text class="v" x="705.5" y="290.6" fill="#24292f">9.13</text>
+<rect x="724" y="276" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="741.5" y="290.6" fill="#24292f">8.93</text>
+<rect x="760" y="276" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="777.5" y="290.6" fill="#24292f">9.05</text>
+<rect x="796" y="276" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="813.5" y="290.6" fill="#24292f">8.98</text>
+<rect x="832" y="276" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="849.5" y="290.6" fill="#24292f">8.96</text>
+<rect x="868" y="276" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="885.5" y="290.6" fill="#24292f">9.01</text>
+<rect x="904" y="276" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="921.5" y="290.6" fill="#24292f">9.02</text>
+<rect x="940" y="276" width="35" height="21" fill="#ffefac"/>
+<text class="v" x="957.5" y="290.6" fill="#24292f">9.18</text>
+<text class="t" x="68" y="313.0" text-anchor="end">19</text>
+<rect x="76" y="298" width="35" height="21" fill="#d95a09"/>
+<text class="v" x="93.5" y="312.6" fill="#ffffff">13.4</text>
+<rect x="112" y="298" width="35" height="21" fill="#feb744"/>
+<text class="v" x="129.5" y="312.6" fill="#24292f">10.9</text>
+<rect x="148" y="298" width="35" height="21" fill="#fec04c"/>
+<text class="v" x="165.5" y="312.6" fill="#24292f">10.7</text>
+<rect x="184" y="298" width="35" height="21" fill="#fed16b"/>
+<text class="v" x="201.5" y="312.6" fill="#24292f">10.2</text>
+<rect x="220" y="298" width="35" height="21" fill="#fed97c"/>
+<text class="v" x="237.5" y="312.6" fill="#24292f">9.98</text>
+<rect x="256" y="298" width="35" height="21" fill="#fee494"/>
+<text class="v" x="273.5" y="312.6" fill="#24292f">9.64</text>
+<rect x="292" y="298" width="35" height="21" fill="#fee89c"/>
+<text class="v" x="309.5" y="312.6" fill="#24292f">9.49</text>
+<rect x="328" y="298" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="345.5" y="312.6" fill="#24292f">8.94</text>
+<rect x="364" y="298" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="381.5" y="312.6" fill="#24292f">8.93</text>
+<rect x="400" y="298" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="417.5" y="312.6" fill="#24292f">9.01</text>
+<rect x="436" y="298" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="453.5" y="312.6" fill="#24292f">9.00</text>
+<rect x="472" y="298" width="35" height="21" fill="#fff6bb"/>
+<text class="v" x="489.5" y="312.6" fill="#24292f">8.90</text>
+<rect x="508" y="298" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="525.5" y="312.6" fill="#24292f">8.97</text>
+<rect x="544" y="298" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="561.5" y="312.6" fill="#24292f">8.97</text>
+<rect x="580" y="298" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="597.5" y="312.6" fill="#24292f">9.01</text>
+<rect x="616" y="298" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="633.5" y="312.6" fill="#24292f">9.01</text>
+<rect x="652" y="298" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="669.5" y="312.6" fill="#24292f">9.04</text>
+<rect x="688" y="298" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="705.5" y="312.6" fill="#24292f">8.96</text>
+<rect x="724" y="298" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="741.5" y="312.6" fill="#24292f">9.01</text>
+<rect x="760" y="298" width="35" height="21" fill="#fff1b0"/>
+<text class="v" x="777.5" y="312.6" fill="#24292f">9.10</text>
+<rect x="796" y="298" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="813.5" y="312.6" fill="#24292f">8.99</text>
+<rect x="832" y="298" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="849.5" y="312.6" fill="#24292f">9.05</text>
+<rect x="868" y="298" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="885.5" y="312.6" fill="#24292f">9.04</text>
+<rect x="904" y="298" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="921.5" y="312.6" fill="#24292f">9.02</text>
+<rect x="940" y="298" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="957.5" y="312.6" fill="#24292f">8.99</text>
+<text class="t" x="68" y="335.0" text-anchor="end">18</text>
+<rect x="76" y="320" width="35" height="21" fill="#d25306"/>
+<text class="v" x="93.5" y="334.6" fill="#ffffff">13.6</text>
+<rect x="112" y="320" width="35" height="21" fill="#feb441"/>
+<text class="v" x="129.5" y="334.6" fill="#24292f">11.0</text>
+<rect x="148" y="320" width="35" height="21" fill="#febd49"/>
+<text class="v" x="165.5" y="334.6" fill="#24292f">10.8</text>
+<rect x="184" y="320" width="35" height="21" fill="#fec756"/>
+<text class="v" x="201.5" y="334.6" fill="#24292f">10.5</text>
+<rect x="220" y="320" width="35" height="21" fill="#fed471"/>
+<text class="v" x="237.5" y="334.6" fill="#24292f">10.1</text>
+<rect x="256" y="320" width="35" height="21" fill="#fedb80"/>
+<text class="v" x="273.5" y="334.6" fill="#24292f">9.93</text>
+<rect x="292" y="320" width="35" height="21" fill="#fee18d"/>
+<text class="v" x="309.5" y="334.6" fill="#24292f">9.75</text>
+<rect x="328" y="320" width="35" height="21" fill="#fff3b2"/>
+<text class="v" x="345.5" y="334.6" fill="#24292f">9.05</text>
+<rect x="364" y="320" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="381.5" y="334.6" fill="#24292f">9.03</text>
+<rect x="400" y="320" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="417.5" y="334.6" fill="#24292f">9.00</text>
+<rect x="436" y="320" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="453.5" y="334.6" fill="#24292f">9.02</text>
+<rect x="472" y="320" width="35" height="21" fill="#fff4b7"/>
+<text class="v" x="489.5" y="334.6" fill="#24292f">8.97</text>
+<rect x="508" y="320" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="525.5" y="334.6" fill="#24292f">8.91</text>
+<rect x="544" y="320" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="561.5" y="334.6" fill="#24292f">9.03</text>
+<rect x="580" y="320" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="597.5" y="334.6" fill="#24292f">8.95</text>
+<rect x="616" y="320" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="633.5" y="334.6" fill="#24292f">9.09</text>
+<rect x="652" y="320" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="669.5" y="334.6" fill="#24292f">8.96</text>
+<rect x="688" y="320" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="705.5" y="334.6" fill="#24292f">8.94</text>
+<rect x="724" y="320" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="741.5" y="334.6" fill="#24292f">9.03</text>
+<rect x="760" y="320" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="777.5" y="334.6" fill="#24292f">8.97</text>
+<rect x="796" y="320" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="813.5" y="334.6" fill="#24292f">8.98</text>
+<rect x="832" y="320" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="849.5" y="334.6" fill="#24292f">9.06</text>
+<rect x="868" y="320" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="885.5" y="334.6" fill="#24292f">8.97</text>
+<rect x="904" y="320" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="921.5" y="334.6" fill="#24292f">8.93</text>
+<rect x="940" y="320" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="957.5" y="334.6" fill="#24292f">8.97</text>
+<text class="t" x="68" y="357.0" text-anchor="end">17</text>
+<rect x="76" y="342" width="35" height="21" fill="#d65707"/>
+<text class="v" x="93.5" y="356.6" fill="#ffffff">13.5</text>
+<rect x="112" y="342" width="35" height="21" fill="#feb340"/>
+<text class="v" x="129.5" y="356.6" fill="#24292f">11.0</text>
+<rect x="148" y="342" width="35" height="21" fill="#feb744"/>
+<text class="v" x="165.5" y="356.6" fill="#24292f">10.9</text>
+<rect x="184" y="342" width="35" height="21" fill="#fec755"/>
+<text class="v" x="201.5" y="356.6" fill="#24292f">10.5</text>
+<rect x="220" y="342" width="35" height="21" fill="#fed16a"/>
+<text class="v" x="237.5" y="356.6" fill="#24292f">10.2</text>
+<rect x="256" y="342" width="35" height="21" fill="#fedc83"/>
+<text class="v" x="273.5" y="356.6" fill="#24292f">9.89</text>
+<rect x="292" y="342" width="35" height="21" fill="#fee08b"/>
+<text class="v" x="309.5" y="356.6" fill="#24292f">9.77</text>
+<rect x="328" y="342" width="35" height="21" fill="#fff1b0"/>
+<text class="v" x="345.5" y="356.6" fill="#24292f">9.09</text>
+<rect x="364" y="342" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="381.5" y="356.6" fill="#24292f">8.98</text>
+<rect x="400" y="342" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="417.5" y="356.6" fill="#24292f">9.03</text>
+<rect x="436" y="342" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="453.5" y="356.6" fill="#24292f">9.02</text>
+<rect x="472" y="342" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="489.5" y="356.6" fill="#24292f">9.04</text>
+<rect x="508" y="342" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="525.5" y="356.6" fill="#24292f">8.94</text>
+<rect x="544" y="342" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="561.5" y="356.6" fill="#24292f">8.94</text>
+<rect x="580" y="342" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="597.5" y="356.6" fill="#24292f">8.95</text>
+<rect x="616" y="342" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="633.5" y="356.6" fill="#24292f">9.01</text>
+<rect x="652" y="342" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="669.5" y="356.6" fill="#24292f">8.94</text>
+<rect x="688" y="342" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="705.5" y="356.6" fill="#24292f">8.95</text>
+<rect x="724" y="342" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="741.5" y="356.6" fill="#24292f">9.01</text>
+<rect x="760" y="342" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="777.5" y="356.6" fill="#24292f">8.95</text>
+<rect x="796" y="342" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="813.5" y="356.6" fill="#24292f">9.03</text>
+<rect x="832" y="342" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="849.5" y="356.6" fill="#24292f">9.01</text>
+<rect x="868" y="342" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="885.5" y="356.6" fill="#24292f">9.04</text>
+<rect x="904" y="342" width="35" height="21" fill="#fff3b2"/>
+<text class="v" x="921.5" y="356.6" fill="#24292f">9.05</text>
+<rect x="940" y="342" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="957.5" y="356.6" fill="#24292f">9.05</text>
+<text class="t" x="68" y="379.0" text-anchor="end">16</text>
+<rect x="76" y="364" width="35" height="21" fill="#ce4e03"/>
+<text class="v" x="93.5" y="378.6" fill="#ffffff">13.8</text>
+<rect x="112" y="364" width="35" height="21" fill="#fc9527"/>
+<text class="v" x="129.5" y="378.6" fill="#24292f">11.7</text>
+<rect x="148" y="364" width="35" height="21" fill="#fd9828"/>
+<text class="v" x="165.5" y="378.6" fill="#24292f">11.6</text>
+<rect x="184" y="364" width="35" height="21" fill="#fea736"/>
+<text class="v" x="201.5" y="378.6" fill="#24292f">11.2</text>
+<rect x="220" y="364" width="35" height="21" fill="#feb23f"/>
+<text class="v" x="237.5" y="378.6" fill="#24292f">11.0</text>
+<rect x="256" y="364" width="35" height="21" fill="#fec856"/>
+<text class="v" x="273.5" y="378.6" fill="#24292f">10.5</text>
+<rect x="292" y="364" width="35" height="21" fill="#fecf67"/>
+<text class="v" x="309.5" y="378.6" fill="#24292f">10.3</text>
+<rect x="328" y="364" width="35" height="21" fill="#fee493"/>
+<text class="v" x="345.5" y="378.6" fill="#24292f">9.65</text>
+<rect x="364" y="364" width="35" height="21" fill="#feeaa1"/>
+<text class="v" x="381.5" y="378.6" fill="#24292f">9.39</text>
+<rect x="400" y="364" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="417.5" y="378.6" fill="#24292f">8.98</text>
+<rect x="436" y="364" width="35" height="21" fill="#fff7bb"/>
+<text class="v" x="453.5" y="378.6" fill="#24292f">8.89</text>
+<rect x="472" y="364" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="489.5" y="378.6" fill="#24292f">9.15</text>
+<rect x="508" y="364" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="525.5" y="378.6" fill="#24292f">8.97</text>
+<rect x="544" y="364" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="561.5" y="378.6" fill="#24292f">9.02</text>
+<rect x="580" y="364" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="597.5" y="378.6" fill="#24292f">8.93</text>
+<rect x="616" y="364" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="633.5" y="378.6" fill="#24292f">9.00</text>
+<rect x="652" y="364" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="669.5" y="378.6" fill="#24292f">9.01</text>
+<rect x="688" y="364" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="705.5" y="378.6" fill="#24292f">9.02</text>
+<rect x="724" y="364" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="741.5" y="378.6" fill="#24292f">8.91</text>
+<rect x="760" y="364" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="777.5" y="378.6" fill="#24292f">9.03</text>
+<rect x="796" y="364" width="35" height="21" fill="#fff1b0"/>
+<text class="v" x="813.5" y="378.6" fill="#24292f">9.10</text>
+<rect x="832" y="364" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="849.5" y="378.6" fill="#24292f">8.93</text>
+<rect x="868" y="364" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="885.5" y="378.6" fill="#24292f">9.07</text>
+<rect x="904" y="364" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="921.5" y="378.6" fill="#24292f">8.98</text>
+<rect x="940" y="364" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="957.5" y="378.6" fill="#24292f">8.97</text>
+<text class="t" x="68" y="401.0" text-anchor="end">15</text>
+<rect x="76" y="386" width="35" height="21" fill="#cf4f04"/>
+<text class="v" x="93.5" y="400.6" fill="#ffffff">13.7</text>
+<rect x="112" y="386" width="35" height="21" fill="#fe9c2c"/>
+<text class="v" x="129.5" y="400.6" fill="#24292f">11.5</text>
+<rect x="148" y="386" width="35" height="21" fill="#fea533"/>
+<text class="v" x="165.5" y="400.6" fill="#24292f">11.3</text>
+<rect x="184" y="386" width="35" height="21" fill="#feb23f"/>
+<text class="v" x="201.5" y="400.6" fill="#24292f">11.0</text>
+<rect x="220" y="386" width="35" height="21" fill="#febe4a"/>
+<text class="v" x="237.5" y="400.6" fill="#24292f">10.7</text>
+<rect x="256" y="386" width="35" height="21" fill="#feca5b"/>
+<text class="v" x="273.5" y="400.6" fill="#24292f">10.4</text>
+<rect x="292" y="386" width="35" height="21" fill="#fed068"/>
+<text class="v" x="309.5" y="400.6" fill="#24292f">10.3</text>
+<rect x="328" y="386" width="35" height="21" fill="#fee697"/>
+<text class="v" x="345.5" y="400.6" fill="#24292f">9.58</text>
+<rect x="364" y="386" width="35" height="21" fill="#feeaa0"/>
+<text class="v" x="381.5" y="400.6" fill="#24292f">9.41</text>
+<rect x="400" y="386" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="417.5" y="400.6" fill="#24292f">9.03</text>
+<rect x="436" y="386" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="453.5" y="400.6" fill="#24292f">8.95</text>
+<rect x="472" y="386" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="489.5" y="400.6" fill="#24292f">8.95</text>
+<rect x="508" y="386" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="525.5" y="400.6" fill="#24292f">9.02</text>
+<rect x="544" y="386" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="561.5" y="400.6" fill="#24292f">8.90</text>
+<rect x="580" y="386" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="597.5" y="400.6" fill="#24292f">8.95</text>
+<rect x="616" y="386" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="633.5" y="400.6" fill="#24292f">8.94</text>
+<rect x="652" y="386" width="35" height="21" fill="#fff7bc"/>
+<text class="v" x="669.5" y="400.6" fill="#24292f">8.87</text>
+<rect x="688" y="386" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="705.5" y="400.6" fill="#24292f">8.94</text>
+<rect x="724" y="386" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="741.5" y="400.6" fill="#24292f">8.91</text>
+<rect x="760" y="386" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="777.5" y="400.6" fill="#24292f">8.95</text>
+<rect x="796" y="386" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="813.5" y="400.6" fill="#24292f">9.00</text>
+<rect x="832" y="386" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="849.5" y="400.6" fill="#24292f">9.02</text>
+<rect x="868" y="386" width="35" height="21" fill="#ffefab"/>
+<text class="v" x="885.5" y="400.6" fill="#24292f">9.19</text>
+<rect x="904" y="386" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="921.5" y="400.6" fill="#24292f">8.97</text>
+<rect x="940" y="386" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="957.5" y="400.6" fill="#24292f">9.03</text>
+<text class="t" x="68" y="423.0" text-anchor="end">14</text>
+<rect x="76" y="408" width="35" height="21" fill="#d05004"/>
+<text class="v" x="93.5" y="422.6" fill="#ffffff">13.7</text>
+<rect x="112" y="408" width="35" height="21" fill="#fe9d2d"/>
+<text class="v" x="129.5" y="422.6" fill="#24292f">11.5</text>
+<rect x="148" y="408" width="35" height="21" fill="#fea231"/>
+<text class="v" x="165.5" y="422.6" fill="#24292f">11.4</text>
+<rect x="184" y="408" width="35" height="21" fill="#feb340"/>
+<text class="v" x="201.5" y="422.6" fill="#24292f">11.0</text>
+<rect x="220" y="408" width="35" height="21" fill="#feb642"/>
+<text class="v" x="237.5" y="422.6" fill="#24292f">10.9</text>
+<rect x="256" y="408" width="35" height="21" fill="#febf4b"/>
+<text class="v" x="273.5" y="422.6" fill="#24292f">10.7</text>
+<rect x="292" y="408" width="35" height="21" fill="#fecc61"/>
+<text class="v" x="309.5" y="422.6" fill="#24292f">10.3</text>
+<rect x="328" y="408" width="35" height="21" fill="#fee391"/>
+<text class="v" x="345.5" y="422.6" fill="#24292f">9.70</text>
+<rect x="364" y="408" width="35" height="21" fill="#fee596"/>
+<text class="v" x="381.5" y="422.6" fill="#24292f">9.61</text>
+<rect x="400" y="408" width="35" height="21" fill="#feeda6"/>
+<text class="v" x="417.5" y="422.6" fill="#24292f">9.29</text>
+<rect x="436" y="408" width="35" height="21" fill="#fff0ae"/>
+<text class="v" x="453.5" y="422.6" fill="#24292f">9.14</text>
+<rect x="472" y="408" width="35" height="21" fill="#fff6bb"/>
+<text class="v" x="489.5" y="422.6" fill="#24292f">8.89</text>
+<rect x="508" y="408" width="35" height="21" fill="#fff7bb"/>
+<text class="v" x="525.5" y="422.6" fill="#24292f">8.89</text>
+<rect x="544" y="408" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="561.5" y="422.6" fill="#24292f">8.96</text>
+<rect x="580" y="408" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="597.5" y="422.6" fill="#24292f">9.02</text>
+<rect x="616" y="408" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="633.5" y="422.6" fill="#24292f">8.94</text>
+<rect x="652" y="408" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="669.5" y="422.6" fill="#24292f">8.92</text>
+<rect x="688" y="408" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="705.5" y="422.6" fill="#24292f">8.96</text>
+<rect x="724" y="408" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="741.5" y="422.6" fill="#24292f">9.02</text>
+<rect x="760" y="408" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="777.5" y="422.6" fill="#24292f">8.96</text>
+<rect x="796" y="408" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="813.5" y="422.6" fill="#24292f">8.93</text>
+<rect x="832" y="408" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="849.5" y="422.6" fill="#24292f">8.93</text>
+<rect x="868" y="408" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="885.5" y="422.6" fill="#24292f">8.91</text>
+<rect x="904" y="408" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="921.5" y="422.6" fill="#24292f">8.92</text>
+<rect x="940" y="408" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="957.5" y="422.6" fill="#24292f">9.06</text>
+<text class="t" x="68" y="445.0" text-anchor="end">13</text>
+<rect x="76" y="430" width="35" height="21" fill="#b34003"/>
+<text class="v" x="93.5" y="444.6" fill="#ffffff">14.3</text>
+<rect x="112" y="430" width="35" height="21" fill="#ef7818"/>
+<text class="v" x="129.5" y="444.6" fill="#24292f">12.5</text>
+<rect x="148" y="430" width="35" height="21" fill="#f3801c"/>
+<text class="v" x="165.5" y="444.6" fill="#24292f">12.2</text>
+<rect x="184" y="430" width="35" height="21" fill="#f6861f"/>
+<text class="v" x="201.5" y="444.6" fill="#24292f">12.1</text>
+<rect x="220" y="430" width="35" height="21" fill="#fb9225"/>
+<text class="v" x="237.5" y="444.6" fill="#24292f">11.8</text>
+<rect x="256" y="430" width="35" height="21" fill="#fea130"/>
+<text class="v" x="273.5" y="444.6" fill="#24292f">11.4</text>
+<rect x="292" y="430" width="35" height="21" fill="#feb441"/>
+<text class="v" x="309.5" y="444.6" fill="#24292f">11.0</text>
+<rect x="328" y="430" width="35" height="21" fill="#feca5b"/>
+<text class="v" x="345.5" y="444.6" fill="#24292f">10.4</text>
+<rect x="364" y="430" width="35" height="21" fill="#fed26d"/>
+<text class="v" x="381.5" y="444.6" fill="#24292f">10.2</text>
+<rect x="400" y="430" width="35" height="21" fill="#fedf88"/>
+<text class="v" x="417.5" y="444.6" fill="#24292f">9.81</text>
+<rect x="436" y="430" width="35" height="21" fill="#fee696"/>
+<text class="v" x="453.5" y="444.6" fill="#24292f">9.59</text>
+<rect x="472" y="430" width="35" height="21" fill="#ffeda7"/>
+<text class="v" x="489.5" y="444.6" fill="#24292f">9.27</text>
+<rect x="508" y="430" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="525.5" y="444.6" fill="#24292f">9.00</text>
+<rect x="544" y="430" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="561.5" y="444.6" fill="#24292f">8.99</text>
+<rect x="580" y="430" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="597.5" y="444.6" fill="#24292f">9.00</text>
+<rect x="616" y="430" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="633.5" y="444.6" fill="#24292f">9.02</text>
+<rect x="652" y="430" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="669.5" y="444.6" fill="#24292f">9.02</text>
+<rect x="688" y="430" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="705.5" y="444.6" fill="#24292f">9.02</text>
+<rect x="724" y="430" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="741.5" y="444.6" fill="#24292f">9.06</text>
+<rect x="760" y="430" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="777.5" y="444.6" fill="#24292f">8.92</text>
+<rect x="796" y="430" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="813.5" y="444.6" fill="#24292f">8.95</text>
+<rect x="832" y="430" width="35" height="21" fill="#fff7bb"/>
+<text class="v" x="849.5" y="444.6" fill="#24292f">8.89</text>
+<rect x="868" y="430" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="885.5" y="444.6" fill="#24292f">9.01</text>
+<rect x="904" y="430" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="921.5" y="444.6" fill="#24292f">8.99</text>
+<rect x="940" y="430" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="957.5" y="444.6" fill="#24292f">8.91</text>
+<text class="t" x="68" y="467.0" text-anchor="end">12</text>
+<rect x="76" y="452" width="35" height="21" fill="#c04602"/>
+<text class="v" x="93.5" y="466.6" fill="#ffffff">14.1</text>
+<rect x="112" y="452" width="35" height="21" fill="#f88a21"/>
+<text class="v" x="129.5" y="466.6" fill="#24292f">12.0</text>
+<rect x="148" y="452" width="35" height="21" fill="#fb9326"/>
+<text class="v" x="165.5" y="466.6" fill="#24292f">11.7</text>
+<rect x="184" y="452" width="35" height="21" fill="#fd9728"/>
+<text class="v" x="201.5" y="466.6" fill="#24292f">11.6</text>
+<rect x="220" y="452" width="35" height="21" fill="#fea332"/>
+<text class="v" x="237.5" y="466.6" fill="#24292f">11.3</text>
+<rect x="256" y="452" width="35" height="21" fill="#feb23f"/>
+<text class="v" x="273.5" y="466.6" fill="#24292f">11.0</text>
+<rect x="292" y="452" width="35" height="21" fill="#febc48"/>
+<text class="v" x="309.5" y="466.6" fill="#24292f">10.8</text>
+<rect x="328" y="452" width="35" height="21" fill="#fed069"/>
+<text class="v" x="345.5" y="466.6" fill="#24292f">10.2</text>
+<rect x="364" y="452" width="35" height="21" fill="#fed778"/>
+<text class="v" x="381.5" y="466.6" fill="#24292f">10.0</text>
+<rect x="400" y="452" width="35" height="21" fill="#fee08a"/>
+<text class="v" x="417.5" y="466.6" fill="#24292f">9.80</text>
+<rect x="436" y="452" width="35" height="21" fill="#fede86"/>
+<text class="v" x="453.5" y="466.6" fill="#24292f">9.84</text>
+<rect x="472" y="452" width="35" height="21" fill="#fee89c"/>
+<text class="v" x="489.5" y="466.6" fill="#24292f">9.48</text>
+<rect x="508" y="452" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="525.5" y="466.6" fill="#24292f">9.15</text>
+<rect x="544" y="452" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="561.5" y="466.6" fill="#24292f">8.97</text>
+<rect x="580" y="452" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="597.5" y="466.6" fill="#24292f">8.91</text>
+<rect x="616" y="452" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="633.5" y="466.6" fill="#24292f">8.99</text>
+<rect x="652" y="452" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="669.5" y="466.6" fill="#24292f">9.04</text>
+<rect x="688" y="452" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="705.5" y="466.6" fill="#24292f">9.01</text>
+<rect x="724" y="452" width="35" height="21" fill="#fff0ad"/>
+<text class="v" x="741.5" y="466.6" fill="#24292f">9.16</text>
+<rect x="760" y="452" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="777.5" y="466.6" fill="#24292f">8.96</text>
+<rect x="796" y="452" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="813.5" y="466.6" fill="#24292f">9.01</text>
+<rect x="832" y="452" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="849.5" y="466.6" fill="#24292f">8.92</text>
+<rect x="868" y="452" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="885.5" y="466.6" fill="#24292f">9.00</text>
+<rect x="904" y="452" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="921.5" y="466.6" fill="#24292f">9.04</text>
+<rect x="940" y="452" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="957.5" y="466.6" fill="#24292f">8.93</text>
+<text class="t" x="68" y="489.0" text-anchor="end">11</text>
+<rect x="76" y="474" width="35" height="21" fill="#bb4403"/>
+<text class="v" x="93.5" y="488.6" fill="#ffffff">14.2</text>
+<rect x="112" y="474" width="35" height="21" fill="#f17b19"/>
+<text class="v" x="129.5" y="488.6" fill="#24292f">12.4</text>
+<rect x="148" y="474" width="35" height="21" fill="#f5851f"/>
+<text class="v" x="165.5" y="488.6" fill="#24292f">12.1</text>
+<rect x="184" y="474" width="35" height="21" fill="#f5851f"/>
+<text class="v" x="201.5" y="488.6" fill="#24292f">12.1</text>
+<rect x="220" y="474" width="35" height="21" fill="#f98f24"/>
+<text class="v" x="237.5" y="488.6" fill="#24292f">11.8</text>
+<rect x="256" y="474" width="35" height="21" fill="#feb23f"/>
+<text class="v" x="273.5" y="488.6" fill="#24292f">11.0</text>
+<rect x="292" y="474" width="35" height="21" fill="#fea735"/>
+<text class="v" x="309.5" y="488.6" fill="#24292f">11.3</text>
+<rect x="328" y="474" width="35" height="21" fill="#febe4a"/>
+<text class="v" x="345.5" y="488.6" fill="#24292f">10.7</text>
+<rect x="364" y="474" width="35" height="21" fill="#fec857"/>
+<text class="v" x="381.5" y="488.6" fill="#24292f">10.5</text>
+<rect x="400" y="474" width="35" height="21" fill="#fed16a"/>
+<text class="v" x="417.5" y="488.6" fill="#24292f">10.2</text>
+<rect x="436" y="474" width="35" height="21" fill="#feda7d"/>
+<text class="v" x="453.5" y="488.6" fill="#24292f">9.96</text>
+<rect x="472" y="474" width="35" height="21" fill="#fee28e"/>
+<text class="v" x="489.5" y="488.6" fill="#24292f">9.74</text>
+<rect x="508" y="474" width="35" height="21" fill="#fee99d"/>
+<text class="v" x="525.5" y="488.6" fill="#24292f">9.46</text>
+<rect x="544" y="474" width="35" height="21" fill="#fff0ae"/>
+<text class="v" x="561.5" y="488.6" fill="#24292f">9.14</text>
+<rect x="580" y="474" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="597.5" y="488.6" fill="#24292f">8.95</text>
+<rect x="616" y="474" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="633.5" y="488.6" fill="#24292f">8.99</text>
+<rect x="652" y="474" width="35" height="21" fill="#fff2b1"/>
+<text class="v" x="669.5" y="488.6" fill="#24292f">9.09</text>
+<rect x="688" y="474" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="705.5" y="488.6" fill="#24292f">8.96</text>
+<rect x="724" y="474" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="741.5" y="488.6" fill="#24292f">8.93</text>
+<rect x="760" y="474" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="777.5" y="488.6" fill="#24292f">8.97</text>
+<rect x="796" y="474" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="813.5" y="488.6" fill="#24292f">9.01</text>
+<rect x="832" y="474" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="849.5" y="488.6" fill="#24292f">8.94</text>
+<rect x="868" y="474" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="885.5" y="488.6" fill="#24292f">9.01</text>
+<rect x="904" y="474" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="921.5" y="488.6" fill="#24292f">8.95</text>
+<rect x="940" y="474" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="957.5" y="488.6" fill="#24292f">9.05</text>
+<text class="t" x="68" y="511.0" text-anchor="end">10</text>
+<rect x="76" y="496" width="35" height="21" fill="#b84203"/>
+<text class="v" x="93.5" y="510.6" fill="#ffffff">14.2</text>
+<rect x="112" y="496" width="35" height="21" fill="#ed7316"/>
+<text class="v" x="129.5" y="510.6" fill="#24292f">12.6</text>
+<rect x="148" y="496" width="35" height="21" fill="#f07818"/>
+<text class="v" x="165.5" y="510.6" fill="#24292f">12.4</text>
+<rect x="184" y="496" width="35" height="21" fill="#f27e1b"/>
+<text class="v" x="201.5" y="510.6" fill="#24292f">12.3</text>
+<rect x="220" y="496" width="35" height="21" fill="#f37f1c"/>
+<text class="v" x="237.5" y="510.6" fill="#24292f">12.3</text>
+<rect x="256" y="496" width="35" height="21" fill="#f88b22"/>
+<text class="v" x="273.5" y="510.6" fill="#24292f">11.9</text>
+<rect x="292" y="496" width="35" height="21" fill="#fd9627"/>
+<text class="v" x="309.5" y="510.6" fill="#24292f">11.7</text>
+<rect x="328" y="496" width="35" height="21" fill="#feb03d"/>
+<text class="v" x="345.5" y="510.6" fill="#24292f">11.1</text>
+<rect x="364" y="496" width="35" height="21" fill="#feb643"/>
+<text class="v" x="381.5" y="510.6" fill="#24292f">10.9</text>
+<rect x="400" y="496" width="35" height="21" fill="#fec654"/>
+<text class="v" x="417.5" y="510.6" fill="#24292f">10.5</text>
+<rect x="436" y="496" width="35" height="21" fill="#fecb5f"/>
+<text class="v" x="453.5" y="510.6" fill="#24292f">10.4</text>
+<rect x="472" y="496" width="35" height="21" fill="#fed573"/>
+<text class="v" x="489.5" y="510.6" fill="#24292f">10.1</text>
+<rect x="508" y="496" width="35" height="21" fill="#fee28f"/>
+<text class="v" x="525.5" y="510.6" fill="#24292f">9.72</text>
+<rect x="544" y="496" width="35" height="21" fill="#feea9f"/>
+<text class="v" x="561.5" y="510.6" fill="#24292f">9.42</text>
+<rect x="580" y="496" width="35" height="21" fill="#fff1af"/>
+<text class="v" x="597.5" y="510.6" fill="#24292f">9.11</text>
+<rect x="616" y="496" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="633.5" y="510.6" fill="#24292f">9.04</text>
+<rect x="652" y="496" width="35" height="21" fill="#fff3b3"/>
+<text class="v" x="669.5" y="510.6" fill="#24292f">9.03</text>
+<rect x="688" y="496" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="705.5" y="510.6" fill="#24292f">8.93</text>
+<rect x="724" y="496" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="741.5" y="510.6" fill="#24292f">8.92</text>
+<rect x="760" y="496" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="777.5" y="510.6" fill="#24292f">8.94</text>
+<rect x="796" y="496" width="35" height="21" fill="#fff2b2"/>
+<text class="v" x="813.5" y="510.6" fill="#24292f">9.06</text>
+<rect x="832" y="496" width="35" height="21" fill="#fff3b2"/>
+<text class="v" x="849.5" y="510.6" fill="#24292f">9.05</text>
+<rect x="868" y="496" width="35" height="21" fill="#fff4b4"/>
+<text class="v" x="885.5" y="510.6" fill="#24292f">9.01</text>
+<rect x="904" y="496" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="921.5" y="510.6" fill="#24292f">9.02</text>
+<rect x="940" y="496" width="35" height="21" fill="#fff4b6"/>
+<text class="v" x="957.5" y="510.6" fill="#24292f">8.98</text>
+<text class="t" x="68" y="533.0" text-anchor="end">9</text>
+<rect x="76" y="518" width="35" height="21" fill="#9f3603"/>
+<text class="v" x="93.5" y="532.6" fill="#ffffff">14.7</text>
+<rect x="112" y="518" width="35" height="21" fill="#e46710"/>
+<text class="v" x="129.5" y="532.6" fill="#ffffff">12.9</text>
+<rect x="148" y="518" width="35" height="21" fill="#eb6f14"/>
+<text class="v" x="165.5" y="532.6" fill="#24292f">12.7</text>
+<rect x="184" y="518" width="35" height="21" fill="#ed7215"/>
+<text class="v" x="201.5" y="532.6" fill="#24292f">12.6</text>
+<rect x="220" y="518" width="35" height="21" fill="#f07a19"/>
+<text class="v" x="237.5" y="532.6" fill="#24292f">12.4</text>
+<rect x="256" y="518" width="35" height="21" fill="#f5851f"/>
+<text class="v" x="273.5" y="532.6" fill="#24292f">12.1</text>
+<rect x="292" y="518" width="35" height="21" fill="#fb9125"/>
+<text class="v" x="309.5" y="532.6" fill="#24292f">11.8</text>
+<rect x="328" y="518" width="35" height="21" fill="#fea735"/>
+<text class="v" x="345.5" y="532.6" fill="#24292f">11.3</text>
+<rect x="364" y="518" width="35" height="21" fill="#feb542"/>
+<text class="v" x="381.5" y="532.6" fill="#24292f">10.9</text>
+<rect x="400" y="518" width="35" height="21" fill="#fec34e"/>
+<text class="v" x="417.5" y="532.6" fill="#24292f">10.6</text>
+<rect x="436" y="518" width="35" height="21" fill="#fec95a"/>
+<text class="v" x="453.5" y="532.6" fill="#24292f">10.4</text>
+<rect x="472" y="518" width="35" height="21" fill="#fed16b"/>
+<text class="v" x="489.5" y="532.6" fill="#24292f">10.2</text>
+<rect x="508" y="518" width="35" height="21" fill="#fedc81"/>
+<text class="v" x="525.5" y="532.6" fill="#24292f">9.91</text>
+<rect x="544" y="518" width="35" height="21" fill="#fee391"/>
+<text class="v" x="561.5" y="532.6" fill="#24292f">9.70</text>
+<rect x="580" y="518" width="35" height="21" fill="#feeca5"/>
+<text class="v" x="597.5" y="532.6" fill="#24292f">9.31</text>
+<rect x="616" y="518" width="35" height="21" fill="#ffefac"/>
+<text class="v" x="633.5" y="532.6" fill="#24292f">9.18</text>
+<rect x="652" y="518" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="669.5" y="532.6" fill="#24292f">8.93</text>
+<rect x="688" y="518" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="705.5" y="532.6" fill="#24292f">8.93</text>
+<rect x="724" y="518" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="741.5" y="532.6" fill="#24292f">9.01</text>
+<rect x="760" y="518" width="35" height="21" fill="#fff3b4"/>
+<text class="v" x="777.5" y="532.6" fill="#24292f">9.01</text>
+<rect x="796" y="518" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="813.5" y="532.6" fill="#24292f">8.92</text>
+<rect x="832" y="518" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="849.5" y="532.6" fill="#24292f">8.93</text>
+<rect x="868" y="518" width="35" height="21" fill="#fff2b0"/>
+<text class="v" x="885.5" y="532.6" fill="#24292f">9.09</text>
+<rect x="904" y="518" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="921.5" y="532.6" fill="#24292f">9.00</text>
+<rect x="940" y="518" width="35" height="21" fill="#fff6ba"/>
+<text class="v" x="957.5" y="532.6" fill="#24292f">8.90</text>
+<text class="t" x="68" y="555.0" text-anchor="end">8</text>
+<rect x="76" y="540" width="35" height="21" fill="#8c2d04"/>
+<text class="v" x="93.5" y="554.6" fill="#ffffff">15.1</text>
+<rect x="112" y="540" width="35" height="21" fill="#a53903"/>
+<text class="v" x="129.5" y="554.6" fill="#ffffff">14.6</text>
+<rect x="148" y="540" width="35" height="21" fill="#bf4602"/>
+<text class="v" x="165.5" y="554.6" fill="#ffffff">14.1</text>
+<rect x="184" y="540" width="35" height="21" fill="#a03703"/>
+<text class="v" x="201.5" y="554.6" fill="#ffffff">14.7</text>
+<rect x="220" y="540" width="35" height="21" fill="#c74a02"/>
+<text class="v" x="237.5" y="554.6" fill="#ffffff">13.9</text>
+<rect x="256" y="540" width="35" height="21" fill="#d55607"/>
+<text class="v" x="273.5" y="554.6" fill="#ffffff">13.5</text>
+<rect x="292" y="540" width="35" height="21" fill="#de600c"/>
+<text class="v" x="309.5" y="554.6" fill="#ffffff">13.2</text>
+<rect x="328" y="540" width="35" height="21" fill="#df610d"/>
+<text class="v" x="345.5" y="554.6" fill="#ffffff">13.1</text>
+<rect x="364" y="540" width="35" height="21" fill="#ea6d13"/>
+<text class="v" x="381.5" y="554.6" fill="#24292f">12.8</text>
+<rect x="400" y="540" width="35" height="21" fill="#e76a11"/>
+<text class="v" x="417.5" y="554.6" fill="#ffffff">12.9</text>
+<rect x="436" y="540" width="35" height="21" fill="#ec7014"/>
+<text class="v" x="453.5" y="554.6" fill="#24292f">12.7</text>
+<rect x="472" y="540" width="35" height="21" fill="#f27e1b"/>
+<text class="v" x="489.5" y="554.6" fill="#24292f">12.3</text>
+<rect x="508" y="540" width="35" height="21" fill="#fea02f"/>
+<text class="v" x="525.5" y="554.6" fill="#24292f">11.4</text>
+<rect x="544" y="540" width="35" height="21" fill="#feac3a"/>
+<text class="v" x="561.5" y="554.6" fill="#24292f">11.1</text>
+<rect x="580" y="540" width="35" height="21" fill="#fea837"/>
+<text class="v" x="597.5" y="554.6" fill="#24292f">11.2</text>
+<rect x="616" y="540" width="35" height="21" fill="#feb844"/>
+<text class="v" x="633.5" y="554.6" fill="#24292f">10.9</text>
+<rect x="652" y="540" width="35" height="21" fill="#fec44f"/>
+<text class="v" x="669.5" y="554.6" fill="#24292f">10.6</text>
+<rect x="688" y="540" width="35" height="21" fill="#fecf66"/>
+<text class="v" x="705.5" y="554.6" fill="#24292f">10.3</text>
+<rect x="724" y="540" width="35" height="21" fill="#feda7f"/>
+<text class="v" x="741.5" y="554.6" fill="#24292f">9.94</text>
+<rect x="760" y="540" width="35" height="21" fill="#fff4b5"/>
+<text class="v" x="777.5" y="554.6" fill="#24292f">9.01</text>
+<rect x="796" y="540" width="35" height="21" fill="#fff6b9"/>
+<text class="v" x="813.5" y="554.6" fill="#24292f">8.93</text>
+<rect x="832" y="540" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="849.5" y="554.6" fill="#24292f">8.94</text>
+<rect x="868" y="540" width="35" height="21" fill="#fff5b8"/>
+<text class="v" x="885.5" y="554.6" fill="#24292f">8.95</text>
+<rect x="904" y="540" width="35" height="21" fill="#fff5b9"/>
+<text class="v" x="921.5" y="554.6" fill="#24292f">8.94</text>
+<rect x="940" y="540" width="35" height="21" fill="#fff5b7"/>
+<text class="v" x="957.5" y="554.6" fill="#24292f">8.96</text>
+<text class="t" x="93.5" y="578" text-anchor="middle">8</text>
+<text class="t" x="129.5" y="578" text-anchor="middle">9</text>
+<text class="t" x="165.5" y="578" text-anchor="middle">10</text>
+<text class="t" x="201.5" y="578" text-anchor="middle">11</text>
+<text class="t" x="237.5" y="578" text-anchor="middle">12</text>
+<text class="t" x="273.5" y="578" text-anchor="middle">13</text>
+<text class="t" x="309.5" y="578" text-anchor="middle">14</text>
+<text class="t" x="345.5" y="578" text-anchor="middle">15</text>
+<text class="t" x="381.5" y="578" text-anchor="middle">16</text>
+<text class="t" x="417.5" y="578" text-anchor="middle">17</text>
+<text class="t" x="453.5" y="578" text-anchor="middle">18</text>
+<text class="t" x="489.5" y="578" text-anchor="middle">19</text>
+<text class="t" x="525.5" y="578" text-anchor="middle">20</text>
+<text class="t" x="561.5" y="578" text-anchor="middle">21</text>
+<text class="t" x="597.5" y="578" text-anchor="middle">22</text>
+<text class="t" x="633.5" y="578" text-anchor="middle">23</text>
+<text class="t" x="669.5" y="578" text-anchor="middle">24</text>
+<text class="t" x="705.5" y="578" text-anchor="middle">25</text>
+<text class="t" x="741.5" y="578" text-anchor="middle">26</text>
+<text class="t" x="777.5" y="578" text-anchor="middle">27</text>
+<text class="t" x="813.5" y="578" text-anchor="middle">28</text>
+<text class="t" x="849.5" y="578" text-anchor="middle">29</text>
+<text class="t" x="885.5" y="578" text-anchor="middle">30</text>
+<text class="t" x="921.5" y="578" text-anchor="middle">31</text>
+<text class="t" x="957.5" y="578" text-anchor="middle">32</text>
+<text class="a" x="526.0" y="606">显存(GiB)</text>
+<text class="a" x="18" y="287.0" transform="rotate(-90 18 287.0)">内存(GiB)</text>
+</svg>
diff --git a/docs/execution-planning.md b/docs/execution-planning.md
new file mode 100644
index 0000000..d496730
--- /dev/null
+++ b/docs/execution-planning.md
@@ -0,0 +1,65 @@
+# FreeVideo Adaptive Execution Planner
+
+The FreeVideo Adaptive Execution Planner computes an execution plan for every request from live device and host measurements. The plan fixes the residency of the 50 transformer blocks across VRAM, pinned host memory and disk, the transfer schedule, the FP8 GEMM path, the attention backend and head grouping, activation staging and the VAE decoder placement, within the measured VRAM and host-memory budgets.
+
+## Inputs
+
+| Input | Source | Determines |
+| --- | --- | --- |
+| Architecture and compute capability | CUDA device properties | FP8 GEMM path and kernel set |
+| Free VRAM | `torch.cuda.mem_get_info` | VRAM budget |
+| Available host memory | OS memory counters, cgroup v1/v2 limit, Windows commit headroom | Host-memory budget |
+| Attention backends | On-device kernel probes | Backend selection |
+| Request geometry | Width, height and frames, converted to video tokens | Activation working set |
+
+VRAM budget: free VRAM minus a reserve of 2.5% of free VRAM, clamped to 0.5–1 GiB (0.25 GiB on Windows). Host-memory budget: available memory minus a reserve of 5% of available memory, clamped to 1–4 GiB (2–4 GiB on Windows); the Windows page file is not counted.
+
+## Plan
+
+| Component | Decision |
+| --- | --- |
+| Transformer residency | Resident blocks = min(⌊(VRAM budget − activation working set) / 432.5 MB⌋, 44), then reduced to max(8, blocks the host budget cannot retain). Non-resident blocks are kept in host memory, pinned up to 22 GB, and copied to the GPU at every step. |
+| Disk streaming | Enabled when the non-resident blocks plus host working memory exceed the host budget. A bounded pinned subset is retained; the remaining blocks are re-read from disk at every step. |
+| Transfer schedule | Two transfer slots (prefetch) at a VRAM budget of 14 GiB or more, with lower thresholds on Windows Blackwell and Ampere; one slot otherwise. |
+| Attention | The first backend that passes its probe, in the order SageAttention 2, PyTorch flash attention, cuDNN, FlashAttention 2, FlashAttention 4. Head group: the widest of 16, 8 and 4 heads whose measured activation footprint at the request's token count fits the VRAM budget. |
+| Activation staging | Below a 10 GiB VRAM budget, the residual stream and attention outputs are staged in host buffers when the on-device activation path does not fit. |
+| FP8 GEMM | Per-tensor scales on Blackwell (SM120), per-channel scales on Ada (SM89) and Hopper (SM90), FP8 weights with BF16 compute on Ampere (SM80, SM86). |
+| Chunking | Feed-forward chunk 2048, projection chunk 1024, window batch 4 (1 on Ampere). |
+| Text encoder | Runs in a separate process that exits before the transformer is loaded. |
+| VAE decoder | Below a 20 GiB VRAM budget, ⌊(VRAM budget − 3.25 GiB) / 268.6 MB⌋ of its 36 blocks stay resident and the rest are streamed; temporal clips are decoded in sequence with the original tiles and blending. |
+| Two-pass sampling | The half-resolution first pass gets its own plan. Blocks copied to the GPU during that pass stay cached in free VRAM, up to the first-pass resident count on Linux and up to the live VRAM budget on Windows. |
+
+## Runtime adaptation
+
+- Budgets are re-measured before every request.
+- Completed requests store their timings and memory peaks in `resource-history.sqlite3`. The history drives the forecast shown before each request and `./freevideo predict`, and replaces the placement when an alternative is predicted to be at least 2% faster.
+- If the minimum working set does not fit, the request is rejected before the transformer loads, and the shortfall is reported.
+- An out-of-memory failure is retried in a fresh process with a lighter placement, up to two times (`--resource-retries`).
+- `./freevideo optimize` searches compute settings for the machine and applies them only after a complete validation run.
+
+## Inspection
+
+- `./freevideo plan --vram-gib 8 --ram-gib 16` prints the plan for the given capacities without loading weights; `--width`, `--height` and `--seconds` set the request.
+- `<output>.request.json` records the measured hardware, the selected plan with a note for each decision, stage timings and memory peaks.
+
+## Capacity grid
+
+Measured on NVIDIA H200 for every combination of 8–32 GiB of VRAM and 8–32 GiB of host memory, with VRAM capped by an MPS client limit and host memory by a cgroup limit without swap. Request: 1344 × 768, 243 frames, 8 single-pass full-resolution steps (`--no-two-pass`).
+
+Seconds per sampling step:
+
+![Seconds per sampling step](assets/capacity-speed.en.svg)
+
+Transformer blocks kept in VRAM; a dot marks configurations where the remaining blocks are read from disk at every step:
+
+![Transformer blocks kept in VRAM](assets/capacity-blocks.en.svg)
+
+## End to end
+
+Windows, 1344 × 768, 10 seconds, two-pass sampling:
+
+| GPU | VRAM + RAM | Time |
+| --- | --- | --- |
+| GeForce RTX 5090 | 32 GB + 64 GB | 122 s |
+| GeForce RTX 5060 Ti | 16 GB + 32 GB | 486 s |
+| GeForce RTX 4060 Ti | 16 GB + 32 GB | 558 s |
diff --git a/docs/zh-CN/execution-planning.md b/docs/zh-CN/execution-planning.md
new file mode 100644
index 0000000..bb9e65b
--- /dev/null
+++ b/docs/zh-CN/execution-planning.md
@@ -0,0 +1,65 @@
+# FreeVideo Adaptive Execution Planner
+
+FreeVideo Adaptive Execution Planner(自适应执行规划器)根据设备与主机的实时测量结果,为每个请求计算执行计划。执行计划在实测的显存与内存预算内确定:50 个 Transformer 块在显存、锁页内存与磁盘之间的驻留位置,传输调度,FP8 GEMM 路径,注意力后端与头分组,激活暂存,以及 VAE 解码器的放置。
+
+## 输入
+
+| 输入 | 来源 | 决定 |
+| --- | --- | --- |
+| 架构与计算能力 | CUDA 设备属性 | FP8 GEMM 路径与内核集 |
+| 空闲显存 | `torch.cuda.mem_get_info` | 显存预算 |
+| 可用内存 | 系统内存计数、cgroup v1/v2 上限、Windows 提交余量 | 内存预算 |
+| 注意力后端 | 设备上的内核探测 | 后端选择 |
+| 请求几何 | 宽、高、帧数换算的视频 token 数 | 激活工作集 |
+
+显存预算:空闲显存减去预留,预留为空闲显存的 2.5%,限制在 0.5–1 GiB(Windows 为 0.25 GiB)。内存预算:可用内存减去预留,预留为可用内存的 5%,限制在 1–4 GiB(Windows 为 2–4 GiB);Windows 页面文件不计入。
+
+## 执行计划
+
+| 组件 | 决策 |
+| --- | --- |
+| Transformer 驻留 | 常驻块数 = min(⌊(显存预算 − 激活工作集) / 432.5 MB⌋, 44),再降到 max(8, 内存预算无法保留的块数)。非常驻块保存在内存中,锁页上限 22 GB,每一步复制到显卡。 |
+| 磁盘流式读取 | 非常驻块与主机工作内存之和超过内存预算时启用:保留有界的锁页子集,其余块每一步从磁盘重新读取。 |
+| 传输调度 | 显存预算不低于 14 GiB 时使用两个传输槽(预取),Windows Blackwell 与 Ampere 的门槛更低;否则使用一个传输槽。 |
+| 注意力 | 按 SageAttention 2、PyTorch flash attention、cuDNN、FlashAttention 2、FlashAttention 4 的顺序选择第一个通过探测的后端。头分组:在 16、8、4 头中选择该请求 token 数下实测激活占用不超过显存预算的最宽分组。 |
+| 激活暂存 | 显存预算低于 10 GiB 且设备上的激活路径放不下时,把残差流和注意力输出暂存在主机缓冲区。 |
+| FP8 GEMM | Blackwell(SM120)逐张量缩放,Ada(SM89)与 Hopper(SM90)逐通道缩放,Ampere(SM80、SM86)以 FP8 存储权重、BF16 计算。 |
+| 分块 | 前馈分块 2048,投影分块 1024,窗口批次 4(Ampere 为 1)。 |
+| 文本编码器 | 在独立进程中运行,Transformer 加载前退出。 |
+| VAE 解码器 | 显存预算低于 20 GiB 时,36 个块中 ⌊(显存预算 − 3.25 GiB) / 268.6 MB⌋ 个常驻,其余流式加载;按时间片段依次解码,分块与融合方式与原解码器相同。 |
+| 二次采样 | 半分辨率的第一轮单独规划。第一轮复制到显卡的块缓存在空闲显存中:Linux 上最多到第一轮的常驻块数,Windows 上最多到实时显存预算。 |
+
+## 运行时调整
+
+- 每个请求前重新测量预算。
+- 完成的请求把耗时和内存峰值写入 `resource-history.sqlite3`,用于请求前的预测和 `./freevideo predict`;预计另一种放置方案快 2% 以上时,替换当前方案。
+- 最小工作集放不下时,请求在 Transformer 加载前被拒绝,并报告缺口。
+- 内存不足的失败会在新进程中以更轻的放置方案重试,最多两次(`--resource-retries`)。
+- `./freevideo optimize` 为本机搜索计算设置,只有完整验证运行通过后才启用。
+
+## 查看执行计划
+
+- `./freevideo plan --vram-gib 8 --ram-gib 16` 不加载权重即可输出指定容量下的执行计划;`--width`、`--height`、`--seconds` 指定请求。
+- `<输出>.request.json` 记录测得的硬件信息、所选执行计划及每项决策的说明、各阶段耗时和内存峰值。
+
+## 容量网格
+
+在 NVIDIA H200 上测量 8–32 GiB 显存与 8–32 GiB 内存的每一种组合:显存用 MPS 客户端限制,内存用 cgroup 限制,不使用交换空间。请求:1344 × 768、243 帧、全分辨率单次 8 步(`--no-two-pass`)。
+
+每个采样步的耗时(秒):
+
+![每个采样步的耗时](../assets/capacity-speed.zh.svg)
+
+留在显存的 Transformer 块数,圆点表示其余块每一步从磁盘读取:
+
+![留在显存的 Transformer 块数](../assets/capacity-blocks.zh.svg)
+
+## 端到端
+
+Windows,1344 × 768、10 秒、二次采样:
+
+| 显卡 | 显存 + 内存 | 时间 |
+| --- | --- | --- |
+| GeForce RTX 5090 | 32GB + 64GB | 122 秒 |
+| GeForce RTX 5060 Ti | 16GB + 32GB | 486 秒 |
+| GeForce RTX 4060 Ti | 16GB + 32GB | 558 秒 |
diff --git a/example_workflows/FreeVideo-All-in-One.json b/example_workflows/FreeVideo-All-in-One.json
new file mode 100644
index 0000000..cfaa979
--- /dev/null
+++ b/example_workflows/FreeVideo-All-in-One.json
@@ -0,0 +1,214 @@
+{
+ "id": "a3c7cc4b-1d79-4e1a-90f4-c216c6a7a913",
+ "revision": 3,
+ "last_node_id": 2,
+ "last_link_id": 1,
+ "nodes": [
+ {
+ "id": 1,
+ "type": "FreeVideoGenerate",
+ "pos": [
+ 620,
+ 160
+ ],
+ "size": [
+ 650,
+ 790
+ ],
+ "flags": {},
+ "order": 0,
+ "mode": 0,
+ "inputs": [
+ {
+ "name": "text",
+ "type": "STRING",
+ "widget": {
+ "name": "text"
+ },
+ "link": null
+ },
+ {
+ "name": "width",
+ "type": "INT",
+ "widget": {
+ "name": "width"
+ },
+ "link": null
+ },
+ {
+ "name": "height",
+ "type": "INT",
+ "widget": {
+ "name": "height"
+ },
+ "link": null
+ },
+ {
+ "name": "seconds",
+ "type": "FLOAT",
+ "widget": {
+ "name": "seconds"
+ },
+ "link": null
+ },
+ {
+ "name": "seed",
+ "type": "INT",
+ "widget": {
+ "name": "seed"
+ },
+ "link": null
+ },
+ {
+ "name": "first",
+ "type": "IMAGE",
+ "link": null
+ },
+ {
+ "name": "last",
+ "type": "IMAGE",
+ "link": null
+ },
+ {
+ "name": "references",
+ "type": "FREEVIDEO_REFERENCES",
+ "link": null
+ },
+ {
+ "name": "loras",
+ "type": "FREEVIDEO_LORAS",
+ "link": null
+ },
+ {
+ "name": "conditioning",
+ "type": "CONDITIONING",
+ "link": null
+ },
+ {
+ "name": "media",
+ "type": "FREEVIDEO_MEDIA",
+ "link": 1
+ },
+ {
+ "name": "two_pass",
+ "type": "BOOLEAN",
+ "widget": {
+ "name": "two_pass"
+ },
+ "link": null
+ }
+ ],
+ "outputs": [
+ {
+ "name": "video",
+ "type": "VIDEO",
+ "links": null,
+ "slot_index": 0
+ },
+ {
+ "name": "report",
+ "type": "STRING",
+ "links": null,
+ "slot_index": 1
+ }
+ ],
+ "properties": {
+ "Node name for S&R": "FreeVideoGenerate"
+ },
+ "widgets_values": [
+ "integrated_multimodal_description: [Shot 1] A premium cinematic, photorealistic wide shot opens above an immense black-glass desert at blue hour. A lone explorer in a deep cobalt coat stands on a mirror-bright salt basin while a colossal ringed observatory hangs in the clouds ahead, its bronze arcs catching the first edge of sunrise. Fine dust moves in deliberate ribbons across the reflective ground. The camera glides forward at low altitude, then rises toward the observatory as distant thunder rolls through the valley; every surface remains sharp, physically coherent, and richly detailed. [Shot 2] At 00:03.000, the observatory awakens in one fluid transformation. Its concentric rings rotate into alignment and release a luminous river of gold and turquoise particles. The camera accelerates through the moving rings in a single uninterrupted arc, passing close to engraved bronze, translucent glass, and drifting sparks. The light river climbs into the storm and becomes a vast aurora that reflects across the salt basin; the explorer remains the same scale and position as the camera sweeps around them. [Shot 3] At 00:07.000, the camera completes the orbit and settles into a majestic centered composition: the observatory, aurora, and mirrored desert form one continuous horizon beneath a clearing sky. The aurora folds into a simple luminous circle above the explorer, then the image resolves smoothly into a restrained dark title card. During the final second, clean luminous lettering reads exactly “FreeVideo”, with no other words or logos. Hold the final frame long enough to read; no subtitles, no glitches, no extra text.\n\noverall_soundscape: Begin with a high open wind, fine salt skittering over glass, and distant thunder. As the rings align, add precise metallic resonance and a rising electrical shimmer; the particle river becomes a broad airy rush that travels around the camera. Let the final aurora fade into a clean, quiet tail beneath the title.\n\nnon_diegetic_music: A restrained cinematic pulse begins with low strings and glassy harmonics, expands into full strings and deep brass as the observatory opens, then resolves into one bright, confident chord under the final title.",
+ 1344,
+ 768,
+ 10,
+ 2026090903,
+ "randomize",
+ true
+ ],
+ "title": "FreeVideo · Generate & Preview"
+ },
+ {
+ "id": 2,
+ "type": "FreeVideoMedia",
+ "pos": [
+ 60,
+ 160
+ ],
+ "size": [
+ 460,
+ 460
+ ],
+ "flags": {},
+ "order": 1,
+ "mode": 0,
+ "inputs": [
+ {
+ "name": "assets",
+ "type": "STRING",
+ "widget": {
+ "name": "assets"
+ },
+ "link": null
+ },
+ {
+ "name": "first",
+ "type": "IMAGE",
+ "link": null
+ },
+ {
+ "name": "last",
+ "type": "IMAGE",
+ "link": null
+ },
+ {
+ "name": "reference",
+ "type": "IMAGE",
+ "link": null
+ },
+ {
+ "name": "reference_audio",
+ "type": "AUDIO",
+ "link": null
+ }
+ ],
+ "outputs": [
+ {
+ "name": "media",
+ "type": "FREEVIDEO_MEDIA",
+ "links": [
+ 1
+ ],
+ "slot_index": 0
+ }
+ ],
+ "properties": {
+ "Node name for S&R": "FreeVideoMedia"
+ },
+ "widgets_values": [
+ "[]"
+ ]
+ }
+ ],
+ "links": [
+ [
+ 1,
+ 2,
+ 0,
+ 1,
+ 10,
+ "FREEVIDEO_MEDIA"
+ ]
+ ],
+ "groups": [],
+ "config": {},
+ "extra": {
+ "ds": {
+ "scale": 0.85,
+ "offset": [
+ 0,
+ 0
+ ]
+ },
+ "freevideo_studio": true
+ },
+ "version": 0.4
+}
diff --git a/freevideo b/freevideo
new file mode 100755
index 0000000..adbcb31
--- /dev/null
+++ b/freevideo
@@ -0,0 +1,8 @@
+#!/usr/bin/env bash
+set -euo pipefail
+FREEVIDEO_SOURCE_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
+export PYTHONPATH="$FREEVIDEO_SOURCE_DIR${PYTHONPATH:+:$PYTHONPATH}"
+source "$FREEVIDEO_SOURCE_DIR/scripts/bootstrap_linux.sh"
+freevideo_bootstrap_linux cli "$@"
+(( FREEVIDEO_BOOTSTRAP_DONE == 0 )) || exit 0
+exec "$FREEVIDEO_BOOTSTRAP_PYTHON" -B -X utf8 -m freevideo_engine.managed "${FREEVIDEO_BOOTSTRAP_ARGS[@]}"
diff --git a/freevideo.cmd b/freevideo.cmd
new file mode 100644
index 0000000..3e1880b
--- /dev/null
+++ b/freevideo.cmd
@@ -0,0 +1,3 @@
+@echo off
+powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -File "%~dp0freevideo.ps1" %*
+exit /b %ERRORLEVEL%
diff --git a/freevideo.ps1 b/freevideo.ps1
new file mode 100644
index 0000000..30605b4
--- /dev/null
+++ b/freevideo.ps1
@@ -0,0 +1,165 @@
+# Windows PowerShell 5.1 / PowerShell 7. No activation or machine-wide PATH changes.
+$ErrorActionPreference = 'Stop'
+$freevideoArguments = @($args)
+$freevideoExit = 1
+$freevideoConsoleEncoding = [Console]::OutputEncoding
+$freevideoSavedEnvironment = @{}
+foreach ($name in @('PYTHONPATH', 'PYTHONUTF8', 'PYTHONIOENCODING', 'UV_PYTHON_INSTALL_DIR',
+ 'UV_PYTHON_INSTALL_MIRROR', 'UV_HTTP_TIMEOUT', 'UV_HTTP_RETRIES', 'UV_NO_CONFIG',
+ 'FREEVIDEO_HOME', 'FREEVIDEO_BOOTSTRAP_ROOT', 'FREEVIDEO_PROXY_MODE')) {
+ $freevideoSavedEnvironment[$name] = [Environment]::GetEnvironmentVariable($name, 'Process')
+}
+try {
+ if ($env:OS -ne 'Windows_NT' -or -not [Environment]::Is64BitOperatingSystem) {
+ throw 'Use native Windows x64, or the Linux ./freevideo entry point.'
+ }
+ . (Join-Path $PSScriptRoot 'freevideo_engine\windows_bootstrap.ps1')
+ $env:PYTHONPATH = $PSScriptRoot
+ [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding
+ $env:PYTHONUTF8 = '1'
+ $env:PYTHONIOENCODING = 'utf-8'
+ $freevideoRoot = if ($env:FREEVIDEO_HOME) { $env:FREEVIDEO_HOME } elseif (Test-Path -LiteralPath (Join-Path $PSScriptRoot 'launcher-source.json')) {
+ Join-Path $env:LOCALAPPDATA 'FreeVideo'
+ } else { $PSScriptRoot }
+ for ($i = 0; $i -lt $freevideoArguments.Count; $i++) {
+ if ($freevideoArguments[$i] -eq '--root' -and $i + 1 -lt $freevideoArguments.Count) {
+ $freevideoRoot = $freevideoArguments[$i + 1]
+ } elseif ($freevideoArguments[$i].StartsWith('--root=')) {
+ $freevideoRoot = $freevideoArguments[$i].Substring(7)
+ }
+ }
+ $env:FREEVIDEO_HOME = $freevideoRoot
+ $downloadSettings = Join-Path $freevideoRoot 'download-settings.json'
+ if (Test-Path -LiteralPath $downloadSettings) {
+ $preferences = Get-Content -LiteralPath $downloadSettings -Raw -Encoding UTF8 | ConvertFrom-Json
+ if ($preferences.proxy_mode) { $env:FREEVIDEO_PROXY_MODE = $preferences.proxy_mode }
+ }
+ if ($env:FREEVIDEO_PROXY_MODE -and $env:FREEVIDEO_PROXY_MODE -notin @('auto', 'proxy', 'direct')) {
+ throw 'Invalid download connection mode.'
+ }
+ $bootstrapRoot = if ($env:FREEVIDEO_BOOTSTRAP_ROOT) { $env:FREEVIDEO_BOOTSTRAP_ROOT } else { Join-Path $freevideoRoot '.freevideo\bootstrap' }
+ $env:FREEVIDEO_BOOTSTRAP_ROOT = $bootstrapRoot
+ $freevideoPython = $null
+ $freevideoPythonPrefix = @()
+ $freevideoCandidates = @()
+ if ($env:FREEVIDEO_PYTHON) { $freevideoCandidates += ,@($env:FREEVIDEO_PYTHON) }
+ $freevideoCandidates += ,@((Join-Path $freevideoRoot 'envs\unified\Scripts\python.exe'))
+ $pyCommand = Get-Command py.exe -ErrorAction SilentlyContinue
+ if ($pyCommand) { $freevideoCandidates += ,@($pyCommand.Source, '-3') }
+ foreach ($name in @('python.exe', 'python3.exe')) {
+ $command = Get-Command $name -ErrorAction SilentlyContinue
+ if ($command -and $command.Source -notlike '*\Microsoft\WindowsApps\*') {
+ $freevideoCandidates += ,@($command.Source)
+ }
+ }
+ if ($env:FREEVIDEO_FORCE_BOOTSTRAP -ne '1') {
+ foreach ($candidate in $freevideoCandidates) {
+ if (-not (Test-Path -LiteralPath $candidate[0] -PathType Leaf)) { continue }
+ $prefix = @($candidate | Select-Object -Skip 1)
+ try {
+ & $candidate[0] @prefix -B -X utf8 -c 'import sys; sys.exit(0 if sys.version_info >= (3, 9) else 1)' 2>$null
+ if ($LASTEXITCODE -eq 0) {
+ $freevideoPython = $candidate[0]
+ $freevideoPythonPrefix = $prefix
+ break
+ }
+ } catch { continue }
+ }
+ }
+ if (-not $freevideoPython) {
+ # Keep the bootstrap inside the selected installation, including source clones.
+ # The Python setup still reviews hardware, resources and licenses before
+ # creating an environment or downloading any models.
+ [IO.Directory]::CreateDirectory($bootstrapRoot) | Out-Null
+ $bootstrapLog = Join-Path $bootstrapRoot 'bootstrap.log'
+ Write-Host 'Preparing isolated Python for setup. Model/environment installation requires the setup confirmation.'
+ Write-Host "Bootstrap cache and log: $bootstrapRoot"
+ $bootstrapLock = [IO.File]::Open((Join-Path $bootstrapRoot 'bootstrap.lock'), 'OpenOrCreate', 'ReadWrite', 'None')
+ try {
+ $versions = Get-Content -LiteralPath (Join-Path $PSScriptRoot 'freevideo_engine\bootstrap_versions.json') -Raw -Encoding UTF8 | ConvertFrom-Json
+ $uvSpec = $versions.windows.uv
+ $archive = Join-Path $bootstrapRoot 'uv.zip'
+ $partial = "$archive.partial"
+ $curl = Get-Command curl.exe -ErrorAction SilentlyContinue
+ if (-not $curl) { throw 'curl.exe is missing. Repair the Windows curl installation, or install Python 3.9+ and rerun.' }
+ $officialOnly = $freevideoArguments -contains '--network=official'
+ for ($i = 0; $i -lt $freevideoArguments.Count - 1; $i++) {
+ if ($freevideoArguments[$i] -eq '--network' -and $freevideoArguments[$i + 1] -eq 'official') { $officialOnly = $true }
+ }
+ $routes = @('https://github.com')
+ if (-not $officialOnly) { $routes += 'https://ghfast.top/https://github.com' }
+ $proxyRoutes = @('inherited')
+ if ($env:FREEVIDEO_PROXY_MODE -eq 'direct') { $proxyRoutes = @('direct') }
+ elseif ($env:FREEVIDEO_PROXY_MODE -ne 'proxy' -and ($env:HTTP_PROXY -or $env:HTTPS_PROXY -or $env:ALL_PROXY)) { $proxyRoutes += 'direct' }
+ $valid = (Test-Path -LiteralPath $archive) -and ((Get-FileHash -LiteralPath $archive -Algorithm SHA256).Hash -eq $uvSpec.sha256)
+ if (-not $valid -and (Test-Path -LiteralPath $archive)) {
+ Move-Item -LiteralPath $archive -Destination "$archive.rejected-$([DateTime]::UtcNow.Ticks)"
+ }
+ foreach ($route in $routes) {
+ foreach ($proxyRoute in $proxyRoutes) {
+ if ($valid) { break }
+ $url = $uvSpec.url.Replace('https://github.com', $route)
+ Write-Progress -Activity 'FreeVideo bootstrap' -Status 'Download and verify package manager'
+ $curlArgs = @('--disable', '--fail', '--location', '--silent', '--show-error',
+ '--proto', '=https', '--proto-redir', '=https', '--connect-timeout', '5',
+ '--max-time', '120', '--retry', '1', '--speed-limit', '1024', '--speed-time', '20',
+ '--output', $partial, $url)
+ $curlExit = Invoke-FreeVideoBootstrapCommand -Executable $curl.Source -Arguments $curlArgs `
+ -LogPath $bootstrapLog -Label "Download package manager ($proxyRoute connection)" -Direct:($proxyRoute -eq 'direct')
+ if ($curlExit -eq 0 -and (Test-Path -LiteralPath $partial) -and
+ (Get-FileHash -LiteralPath $partial -Algorithm SHA256).Hash -eq $uvSpec.sha256) {
+ Move-Item -LiteralPath $partial -Destination $archive
+ $valid = $true
+ } elseif (Test-Path -LiteralPath $partial) {
+ Move-Item -LiteralPath $partial -Destination "$partial.retained-$([DateTime]::UtcNow.Ticks)"
+ }
+ }
+ }
+ if (-not $valid) { throw "Package manager download failed integrity/network checks. Retry the same command; see $bootstrapLog" }
+ $toolsDirectory = Join-Path $bootstrapRoot ('uv-' + $uvSpec.version)
+ Expand-Archive -LiteralPath $archive -DestinationPath $toolsDirectory -Force
+ $uv = Join-Path $toolsDirectory $uvSpec.executable
+ $env:UV_PYTHON_INSTALL_DIR = Join-Path $bootstrapRoot 'python'
+ $env:UV_HTTP_TIMEOUT = '20'
+ $env:UV_HTTP_RETRIES = '1'
+ $env:UV_NO_CONFIG = '1'
+ $installed = $false
+ $pythonRoutes = @($routes | ForEach-Object { "$_/astral-sh/python-build-standalone/releases/download" })
+ if (-not $officialOnly -and $freevideoSavedEnvironment['UV_PYTHON_INSTALL_MIRROR']) {
+ $pythonRoutes = @($freevideoSavedEnvironment['UV_PYTHON_INSTALL_MIRROR']) + $pythonRoutes
+ }
+ $attempt = 0
+ foreach ($route in $pythonRoutes) {
+ foreach ($proxyRoute in $proxyRoutes) {
+ if ($installed) { break }
+ $attempt++
+ $env:UV_PYTHON_INSTALL_MIRROR = $route
+ Write-Progress -Activity 'FreeVideo bootstrap' -Status ('Install isolated Python ' + $versions.python)
+ $pythonExit = Invoke-FreeVideoBootstrapCommand -Executable $uv `
+ -Arguments @('python', 'install', '--no-bin', '--no-registry', '--no-progress', '--color', 'never', $versions.python) `
+ -LogPath $bootstrapLog -Label ("Install Python $($versions.python), attempt $attempt ($proxyRoute connection)") `
+ -Direct:($proxyRoute -eq 'direct')
+ if ($pythonExit -eq 0) { $installed = $true; break }
+ Write-Host "Python install source $attempt returned exit code $pythonExit. Details retained in $bootstrapLog"
+ }
+ }
+ if (-not $installed) { throw "Python installation failed (uv exit $pythonExit). Check your network or connection mode in Downloads. See $bootstrapLog and retry." }
+ $freevideoPython = (& $uv python find --managed-python $versions.python | Select-Object -Last 1).Trim()
+ if ($LASTEXITCODE -ne 0 -or -not (Test-Path -LiteralPath $freevideoPython)) { throw 'Managed Python could not be located.' }
+ } finally {
+ $bootstrapLock.Dispose()
+ Write-Progress -Activity 'FreeVideo bootstrap' -Completed
+ }
+ }
+ & $freevideoPython @freevideoPythonPrefix -B -X utf8 -m freevideo_engine.managed @freevideoArguments
+ $freevideoExit = $LASTEXITCODE
+} catch {
+ Write-Host ("FreeVideo could not start: " + $_.Exception.Message) -ForegroundColor Red
+ Write-Host 'Existing files are retained. Retry the same command after resolving the reported issue.'
+} finally {
+ [Console]::OutputEncoding = $freevideoConsoleEncoding
+ foreach ($name in $freevideoSavedEnvironment.Keys) {
+ [Environment]::SetEnvironmentVariable($name, $freevideoSavedEnvironment[$name], 'Process')
+ }
+}
+exit $freevideoExit
diff --git a/freevideo_engine/__init__.py b/freevideo_engine/__init__.py
new file mode 100644
index 0000000..b55665f
--- /dev/null
+++ b/freevideo_engine/__init__.py
@@ -0,0 +1,8 @@
+"""FreeVideo inference policies layered over the pinned VDN model."""
+__version__ = "2026.9.16.41444"
+
+# Release builds stamp one UTC version into both the GUI and deployed engine.
+from pathlib import Path as _Path
+_build_version = _Path(__file__).with_name('build-version.txt')
+if _build_version.is_file():
+ __version__ = _build_version.read_text(encoding='utf-8').strip()
diff --git a/freevideo_engine/__main__.py b/freevideo_engine/__main__.py
new file mode 100644
index 0000000..9ae637f
--- /dev/null
+++ b/freevideo_engine/__main__.py
@@ -0,0 +1,4 @@
+from .cli import main
+
+if __name__ == "__main__":
+ main()
diff --git a/freevideo_engine/activation_staging.py b/freevideo_engine/activation_staging.py
new file mode 100644
index 0000000..9d2f2cd
--- /dev/null
+++ b/freevideo_engine/activation_staging.py
@@ -0,0 +1,40 @@
+"""Contiguous D2H head groups with double-buffered CPU projection tiles."""
+import torch
+
+
+class HeadReadouts:
+ def __init__(self, rows, head_widths, dtype, tile_rows):
+ self.capacity = (rows + 1023) // 1024 * 1024
+ self.widths = tuple(head_widths)
+ self.dtype = dtype
+ self.tile_rows = tile_rows
+ self.groups = [torch.empty((self.capacity, width), dtype=dtype, pin_memory=True) for width in head_widths]
+ self.tiles = [torch.empty((tile_rows, sum(head_widths)), dtype=dtype, pin_memory=True) for _ in range(2)]
+ self.events = [None, None]
+
+ def supports(self, rows, head_widths, dtype, tile_rows):
+ return rows <= self.capacity and tuple(head_widths) == self.widths and dtype == self.dtype and tile_rows == self.tile_rows
+
+ def store(self, group, values):
+ if values.shape[1] != self.widths[group] or not values.is_contiguous():
+ raise ValueError('Head readouts must be contiguous [rows, head channels]')
+ self.groups[group][:len(values)].copy_(values, non_blocking=True)
+
+ def project(self, module, output, rows):
+ # The caller synchronizes once after all D2H groups have been written.
+ stream = torch.cuda.current_stream(output.device)
+ for index, start in enumerate(range(0, rows, self.tile_rows)):
+ slot = index % 2
+ if self.events[slot] is not None:
+ self.events[slot].synchronize()
+ count = min(self.tile_rows, rows - start)
+ tile = self.tiles[slot][:count]
+ offset = 0
+ for group, width in zip(self.groups, self.widths):
+ tile[:, offset:offset + width].copy_(group[start:start + count])
+ offset += width
+ device_values = tile.to(output.device, non_blocking=True)
+ done = torch.cuda.Event()
+ done.record(stream)
+ self.events[slot] = done
+ yield slice(start, start + count), module(device_values)
diff --git a/freevideo_engine/adaln.py b/freevideo_engine/adaln.py
new file mode 100644
index 0000000..9e9d524
--- /dev/null
+++ b/freevideo_engine/adaln.py
@@ -0,0 +1,188 @@
+"""Schedule-specific AdaLN tables, evaluated with the original per-step GEMMs.
+
+The optimization is inspired by NVIDIA's Sol-H3 AdaLN precompute: timesteps are
+known before sampling, so retain the small modulation outputs instead of 26 GB
+of immutable projection weights. FL2VA has a separate keyframe timestep schedule.
+"""
+import hashlib
+import json
+from pathlib import Path
+
+from safetensors.torch import load_file, save_file
+import torch
+
+
+class ScheduleCursor:
+ def __init__(self, timesteps):
+ self.timesteps = timesteps
+ self.index = -1
+
+ def before(self, model, args, kwargs):
+ index = self.index + 1
+ if index >= len(self.timesteps):
+ raise ValueError('Reset the AdaLN schedule before each request')
+ if not torch.equal(kwargs['timestep'], self.timesteps[index]):
+ raise ValueError('The request changed the precomputed AdaLN schedule')
+ self.index = index
+
+ def reset(self, start_index=0):
+ if type(start_index) is not int or not 0 <= start_index < len(self.timesteps):
+ raise ValueError('AdaLN start index must select an existing schedule row')
+ self.index = start_index - 1
+
+
+class CachedModulation(torch.nn.Module):
+ def __init__(self, values, cursor):
+ super().__init__()
+ self.cursor = cursor
+ self.steps = len(values)
+ for index, values_at_step in enumerate(values):
+ self.register_buffer(f'step_{index}', torch.cat(values_at_step, dim=-1))
+
+ def forward(self, temb):
+ if self.cursor.index < 0:
+ raise RuntimeError('AdaLN schedule has not started')
+ return getattr(self, f'step_{self.cursor.index}').chunk(6, dim=-1)
+
+
+class TableCache:
+ """Persist fixed modulation constants by weights, schedule and layout."""
+ def __init__(self, root, source_id, steps, task='t2va', *, manifest=None,
+ timesteps=None, channels=5376, device='cuda'):
+ from . import adaln_assets as assets
+ from .monitoring import save
+ times = schedule_timesteps(steps, device='cpu', task=task) if timesteps is None else timesteps
+ rows = [value.detach().float().cpu().tolist() for value in times]
+ self.identity = assets.identity(assets.weight_identity(manifest) if manifest is not None else source_id,
+ rows, channels)
+ self.device = device
+ self.asset = next((table for table in (manifest or {}).get('adaln_tables', [])
+ if table['identity'] == self.identity), None)
+ self.root = Path(root) / assets.directory(self.identity)
+ self.producer = None
+ if self.asset is not None:
+ if self.asset['directory'] != self.root.name:
+ raise ValueError('AdaLN asset identity/path mismatch')
+ self.producer = self.asset.get('producer')
+ return
+ # Upgrade known legacy tables without rereading 26 GB of projections.
+ # This is a one-time semantic-format migration; future v2 reuse does
+ # not depend on these implementation hashes or the producer machine.
+ if manifest is not None and not (self.root / 'identity.json').exists():
+ from .export_slim import LEGACY_CONTRACTS, LEGACY_MODEL_CONTRACTS
+ count = len(assets.projection_groups(manifest))
+ for candidate in sorted(Path(root).glob('adaln-tables-*')):
+ marker = candidate / 'identity.json'
+ try:
+ old = json.loads(marker.read_text(encoding='utf-8'))
+ if (old.get('implementation') not in LEGACY_CONTRACTS or
+ old.get('original_implementation') not in LEGACY_MODEL_CONTRACTS or
+ old.get('source_id') != source_id or old.get('steps') != steps or
+ old.get('video_shift') != 12. or old.get('audio_shift') != 3.):
+ continue
+ if old.get('task') != 't2va':
+ from src.inference.render import KEYFRAME_NOISE_AUG
+ if old.get('keyframe_noise_aug') != KEYFRAME_NOISE_AUG:
+ continue
+ legacy_times = [t.tolist() for t in schedule_timesteps(steps, device='cpu', task=old['task'])]
+ if legacy_times != rows:
+ continue
+ files = []
+ for index in range(count):
+ path = candidate / f'{index:02d}.safetensors'
+ record = json.loads(path.with_suffix('.json').read_text(encoding='utf-8'))
+ files.append(dict(index=index, file=path.name, bytes=path.stat().st_size, sha256=record['sha256']))
+ if not files:
+ continue
+ except (OSError, ValueError, KeyError, TypeError):
+ continue
+ self.root = candidate
+ self.producer = dict(old, migrated_from='verified-legacy-contract-v1')
+ self.asset = dict(identity=self.identity, directory=candidate.name, files=files, producer=self.producer)
+ return
+ self.root.mkdir(parents=True, exist_ok=True)
+ marker = self.root / 'identity.json'
+ if marker.exists():
+ if json.loads(marker.read_text(encoding='utf-8')) != self.identity:
+ raise ValueError('AdaLN table cache identity mismatch')
+ else:
+ save(marker, self.identity)
+
+ def load(self, index, steps):
+ from . import adaln_assets as assets
+ if steps != len(self.identity['timesteps']):
+ raise ValueError('AdaLN cache schedule length mismatch')
+ path = self.root / f'{index:02d}.safetensors'
+ marker = path.with_suffix('.json')
+ if self.asset is not None:
+ row = next((r for r in self.asset['files'] if r['index'] == index), None)
+ if row is None:
+ raise ValueError('Incomplete AdaLN model asset')
+ elif not path.is_file() or not marker.is_file():
+ return None
+ else:
+ row = json.loads(marker.read_text(encoding='utf-8'))
+ assets.check_table(path, row, self.identity)
+ # These are small immutable constants; their producer's GPU/runtime is
+ # deliberately irrelevant. Validate before transferring to this device.
+ state = load_file(path, device=self.device)
+ if any(not bool(torch.isfinite(tensor).all()) for tensor in state.values()):
+ raise ValueError('Nonfinite AdaLN model asset')
+ return [state[f'step_{i}'].chunk(6, dim=-1) for i in range(steps)]
+
+ def save(self, index, module):
+ from . import adaln_assets as assets
+ from .monitoring import save
+ if self.asset is not None:
+ raise ValueError('Published AdaLN model assets are immutable')
+ path = self.root / f'{index:02d}.safetensors'
+ temporary = path.with_suffix('.partial')
+ state = {name: tensor.cpu() for name, tensor in module.state_dict().items()}
+ if any(not bool(torch.isfinite(tensor).all()) for tensor in state.values()):
+ raise ValueError('Cannot save nonfinite AdaLN constants')
+ save_file(state, temporary)
+ row = {'bytes': temporary.stat().st_size, 'sha256': assets.file_hash(temporary)}
+ assets.check_table(temporary, row, self.identity, verify_hash=False)
+ temporary.replace(path)
+ save(path.with_suffix('.json'), row)
+ provenance = self.root / 'producer.json'
+ if not provenance.exists():
+ from diffusers.models.transformers import transformer_minimax_h3
+ save(provenance, {'torch': str(torch.__version__), 'cuda': torch.version.cuda,
+ 'gpu': torch.cuda.get_device_name() if str(self.device).startswith('cuda') else 'cpu',
+ 'implementation': assets.file_hash(__file__),
+ 'original_implementation': assets.file_hash(transformer_minimax_h3.__file__),
+ 'matmul_precision': torch.get_float32_matmul_precision(),
+ 'tf32': torch.backends.cuda.matmul.allow_tf32,
+ 'bf16_reduced_reduction': torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction})
+
+
+def schedule_timesteps(steps, video_shift=12., audio_shift=3., device='cuda', task='t2va'):
+ from .media_request import TASKS
+ if task not in TASKS:
+ raise ValueError('Unsupported AdaLN task')
+ from diffusers import MiniMaxH3Scheduler
+ video, audio = MiniMaxH3Scheduler(shift=video_shift), MiniMaxH3Scheduler(shift=audio_shift)
+ video.set_timesteps(steps, device=device)
+ audio.set_timesteps(steps, device=device)
+ timesteps = []
+ for video_t, audio_t in zip(video.timesteps, audio.timesteps):
+ values = [video_t.float(), audio_t.float()]
+ if task in ('i2va', 'l2va', 'fl2va', 'ref2va', 'ref2va_av'):
+ from src.inference.render import KEYFRAME_NOISE_AUG
+ values.append(video_t.new_tensor(max(float(video_t), KEYFRAME_NOISE_AUG), dtype=torch.float32))
+ if task in ('ref2va_audio', 'ref2va_av'):
+ values.append(video_t.new_tensor(0., dtype=torch.float32))
+ timesteps.append(torch.unique(torch.stack(values), sorted=True))
+ return timesteps
+
+
+def schedule_embeddings(model, steps, video_shift=12., audio_shift=3., device='cuda', task='t2va'):
+ timesteps = schedule_timesteps(steps, video_shift, audio_shift, device, task)
+ embeddings = []
+ for times in timesteps:
+ projected = model.time_proj(times).to(model.time_embedder.linear_1.weight.dtype)
+ embeddings.append(model.time_embedder(projected))
+ return ScheduleCursor(timesteps), embeddings
diff --git a/freevideo_engine/adaln_assets.py b/freevideo_engine/adaln_assets.py
new file mode 100644
index 0000000..918c2c6
--- /dev/null
+++ b/freevideo_engine/adaln_assets.py
@@ -0,0 +1,198 @@
+"""Portable AdaLN model assets. Validation and installation stay Torch-free.
+
+Producer hardware/software describe where constants were evaluated; they are
+not dependencies of a stored tensor. The contract, weights, exact timestep rows
+and tensor layout are dependencies. Never substitute a different schedule.
+"""
+import hashlib
+import json
+import math
+from pathlib import Path, PurePosixPath
+import re
+import shutil
+import struct
+
+CONTRACT = 'minimax-h3-adaln-silu-linear-3x6-v1'
+FORMAT = 'freevideo-adaln-v2'
+SLIM_FORMAT = 'freevideo-fp8-slim-v1'
+
+
+def digest(value):
+ return hashlib.sha256(json.dumps(value, sort_keys=True, separators=(',', ':'),
+ allow_nan=False).encode()).hexdigest()
+
+
+def file_hash(path):
+ h = hashlib.sha256()
+ with Path(path).open('rb') as stream:
+ for block in iter(lambda: stream.read(8 * 1024 * 1024), b''):
+ h.update(block)
+ return h.hexdigest()
+
+
+def asset_path(root, name):
+ if not isinstance(name, str):
+ raise ValueError('Invalid AdaLN asset path')
+ parts = PurePosixPath(name)
+ if (not isinstance(name, str) or not name or '\\' in name or ':' in name
+ or parts.is_absolute() or any(p in ('..', '.') for p in name.split('/'))):
+ raise ValueError('Invalid AdaLN asset path')
+ root = Path(root).resolve()
+ path = root.joinpath(*parts.parts)
+ try:
+ path.resolve().relative_to(root)
+ except ValueError as error:
+ raise ValueError('AdaLN asset escapes the cache') from error
+ return path
+
+
+def tensor_header(path):
+ """Bounded structural check, including payload length, before any mapping."""
+ with Path(path).open('rb') as stream:
+ prefix = stream.read(8)
+ if len(prefix) != 8:
+ raise ValueError('Truncated AdaLN tensor header')
+ size = struct.unpack('<Q', prefix)[0]
+ if not 2 <= size <= 16 * 1024 * 1024:
+ raise ValueError('Invalid AdaLN tensor header size')
+ header = json.loads(stream.read(size))
+ payload = Path(path).stat().st_size - 8 - size
+ end = 0
+ entries = {k: v for k, v in header.items() if k != '__metadata__'}
+ for row in sorted(entries.values(), key=lambda v: v['data_offsets'][0]):
+ begin, stop = row['data_offsets']
+ shape = row['shape']
+ width = {'BF16': 2, 'F32': 4, 'F16': 2}.get(row['dtype'])
+ if (width is None or any(type(v) is not int or v < 1 for v in shape)
+ or begin != end or stop - begin != math.prod(shape) * width):
+ raise ValueError('Invalid AdaLN tensor layout')
+ end = stop
+ if not entries or end != payload:
+ raise ValueError('Truncated or oversized AdaLN tensor payload')
+ return entries
+
+
+def projection_groups(manifest):
+ sources = manifest.get('adaln_sources', {})
+ rows = sources.get('groups', [r for r in manifest['groups'] if r['group'].startswith('adaln/')])
+ return sorted(rows, key=lambda r: r['group'])
+
+
+def weight_identity(manifest):
+ roots = [r for r in manifest['groups'] if r['group'] == 'root']
+ if len(roots) != 1:
+ raise ValueError('AdaLN needs one root weight group')
+ # Export records the hash of just the time embedding tensors. Legacy caches
+ # conservatively bind their whole root group until an export supplies it.
+ embedding = manifest.get('adaln_sources', {}).get('embedding_sha256', roots[0]['sha256'])
+ return digest({'embedding': embedding, 'projections':
+ [(r['group'], r['sha256']) for r in projection_groups(manifest)]})
+
+
+def identity(weights, timesteps, channels, dtype='BF16'):
+ if (not timesteps or type(channels) is not int or channels <= 0 or dtype not in ('BF16', 'F32')
+ or any(not row or row != sorted(set(row)) or any(not math.isfinite(v) for v in row)
+ for row in timesteps)):
+ raise ValueError('Invalid AdaLN schedule or layout')
+ return dict(format=FORMAT, contract=CONTRACT, weights=weights,
+ timesteps=timesteps, channels=channels, dtype=dtype,
+ modality_rows=3, components=6)
+
+
+def directory(value):
+ return 'adaln-tables-v2-' + digest(value)[:24]
+
+
+def check_table(path, row, expected, *, verify_hash=True):
+ if not Path(path).is_file() or Path(path).stat().st_size != row['bytes']:
+ raise ValueError('AdaLN table integrity failure (missing or wrong size): ' + str(path))
+ if verify_hash and file_hash(path) != row['sha256']:
+ raise ValueError('AdaLN table integrity failure: ' + str(path))
+ header = tensor_header(path)
+ steps = expected['timesteps']
+ if set(header) != {'step_%d' % i for i in range(len(steps))}:
+ raise ValueError('AdaLN table schedule length mismatch')
+ for i, times in enumerate(steps):
+ item = header['step_%d' % i]
+ if (item['dtype'] != expected['dtype'] or
+ item['shape'] != [len(times) * expected['modality_rows'], expected['channels'] * 6]):
+ raise ValueError('AdaLN table shape or dtype mismatch')
+
+
+def validate_catalog(manifest, count):
+ """Return every required asset row; no file I/O, Torch or device probing."""
+ tables = manifest.get('adaln_tables', [])
+ slim = manifest.get('format') == SLIM_FORMAT
+ sources = projection_groups(manifest)
+ for row in sources:
+ if (row['file'] != row['group'] + '.safetensors' or
+ not re.fullmatch(r'adaln/[0-9]{2}', row['group']) or
+ not isinstance(row.get('sha256'), str) or not re.fullmatch('[0-9a-f]{64}', row['sha256']) or
+ type(row.get('bytes')) is not int or row['bytes'] <= 0):
+ raise ValueError('Invalid original AdaLN source identity')
+ if slim and ({r['group'] for r in sources} != {'adaln/%02d' % i for i in range(count)}
+ or len(sources) != count or not tables):
+ raise ValueError('Slim model is missing AdaLN source identities or complete tables')
+ result, seen = [], set()
+ for table in tables:
+ value = table['identity']
+ expected = identity(weight_identity(manifest), value['timesteps'], value['channels'], value['dtype'])
+ if value != expected or table['directory'] != directory(value) or table['directory'] in seen:
+ raise ValueError('AdaLN model asset identity mismatch')
+ seen.add(table['directory'])
+ files = table['files']
+ if len(files) != count or {r['index'] for r in files} != set(range(count)):
+ raise ValueError('Incomplete AdaLN model asset set')
+ for row in files:
+ if (row['file'] != table['directory'] + '/%02d.safetensors' % row['index']
+ or not re.fullmatch('[0-9a-f]{64}', row['sha256']) or row['bytes'] <= 0):
+ raise ValueError('Invalid AdaLN model asset receipt')
+ result.append((row, value))
+ return result
+
+
+def restore_projections(cache, manifest):
+ """Opt-in by requesting an unsupported schedule or an AdaLN-changing LoRA.
+
+ The normal eight-step model never calls this. Restore only the optional
+ projection files from an immutable published revision, with bounded writes.
+ Existing bytes and original package metadata are never overwritten.
+ """
+ from . import network
+ rows = projection_groups(manifest)
+ if not rows:
+ raise ValueError('This model has no original AdaLN projection source')
+ missing = []
+ for row in rows:
+ path = asset_path(cache, row['file'])
+ if path.exists():
+ if path.stat().st_size != row['bytes'] or file_hash(path) != row['sha256']:
+ raise ValueError('Optional AdaLN source changed; retained: ' + str(path))
+ else:
+ missing.append(row)
+ if not missing:
+ return rows
+ remote = manifest.get('adaln_sources', {}).get('download', {})
+ if (not re.fullmatch(r'[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+', remote.get('repo', ''))
+ or not re.fullmatch('[0-9a-f]{40}', remote.get('revision', ''))):
+ raise ValueError('This schedule/LoRA needs original AdaLN weights. Reuse a full prepared cache; '
+ 'the slim package has no pinned recovery source.')
+ needed = sum(r['bytes'] for r in missing)
+ if shutil.disk_usage(cache).free < needed + 1024**3:
+ raise ValueError('This schedule/LoRA needs %.2f GiB of optional AdaLN weights; '
+ 'insufficient disk space. Existing model and request retained.' % (needed / 1024**3))
+ print(json.dumps({'event': 'adaln_sources_required', 'bytes': needed,
+ 'reason': 'Requested schedule or LoRA changes precomputed modulation',
+ 'files': len(missing), 'revision': remote['revision']}), flush=True)
+ from huggingface_hub import get_token
+ token = get_token()
+ headers = ['Authorization: Bearer ' + token] if token else []
+ prefix = remote.get('prefix', 'cache')
+ asset_path(cache, prefix) # Validate before constructing a remote URL.
+ for row in missing:
+ url = 'https://huggingface.co/%s/resolve/%s/%s/%s' % (
+ remote['repo'], remote['revision'], prefix, row['file'])
+ network.download([('official', url)], asset_path(cache, row['file']), row['sha256'],
+ size=row['bytes'], headers_for=lambda source: headers if source == 'official' else [],
+ category='models')
+ return rows
diff --git a/freevideo_engine/adaptive.py b/freevideo_engine/adaptive.py
new file mode 100644
index 0000000..fd58052
--- /dev/null
+++ b/freevideo_engine/adaptive.py
@@ -0,0 +1,540 @@
+"""Torch-free attempt admission, recovery and complete-request observations.
+
+Placement can be retried without changing arithmetic. Chunk changes are search
+candidates and need local full-request equivalence, not an OOM exception waiver.
+"""
+import copy
+import hashlib
+import json
+import math
+import os
+from pathlib import Path
+import platform
+import subprocess
+import time
+
+from .paths import data_root
+
+
+class WorkerExit(RuntimeError):
+ def __init__(self, code, log):
+ self.returncode, self.log = code, Path(log)
+ super().__init__('Worker exited %s; see %s\n%s' %
+ (code, log, self.log.read_text(encoding='utf-8', errors='replace')[-4000:]))
+
+
+def classify_failure(error, metrics=None):
+ """Only positively identified resource failures are retriable.
+
+ A GPU error may be the first synchronization point, not the fault origin.
+ An unexplained native exit is not reclassified using post-reboot health.
+ """
+ metrics = metrics if isinstance(metrics, dict) else {}
+ details = metrics.get('failure', {})
+ details = details if isinstance(details, dict) else {}
+ cleanup = metrics.get('cleanup_errors', [])
+ cleanup = list(cleanup) if isinstance(cleanup, list) else [cleanup]
+ attached = getattr(error, 'cleanup_errors', [])
+ cleanup += attached if isinstance(attached, list) else [attached]
+ text = '\n'.join([str(error), str(metrics.get('error', ''))] + [str(item) for item in cleanup]).lower()
+ if any(part in text for part in ('cuda error: unknown error', 'device is lost', 'device lost',
+ 'gpu is lost', 'illegal memory access', 'device-side assert', 'unspecified launch failure',
+ 'driver shutting down', 'device has been removed', 'cuda error: launch timeout',
+ 'the launch timed out', 'cudaerrorillegaladdress', 'cudaerrorunknown')) or details.get('kind') == 'cuda_error':
+ kind, outcome, retry = 'cuda_error', 'discarded', False
+ elif getattr(error, 'worker_stop_confirmed', None) is False:
+ kind, outcome, retry = 'worker_stop_unconfirmed', 'discarded', False
+ elif isinstance(error, KeyboardInterrupt):
+ kind, outcome, retry = 'cancelled', 'cancelled', False
+ elif isinstance(error, subprocess.TimeoutExpired):
+ kind, outcome, retry = 'timeout', 'cancelled', False
+ elif details.get('kind') == 'gpu_oom' or 'cuda out of memory' in text or 'cuda error: out of memory' in text:
+ kind, outcome, retry = 'gpu_oom', 'resource_failure', True
+ elif isinstance(error, MemoryError) or details.get('kind') == 'ram_pressure':
+ kind, outcome, retry = 'ram_pressure', 'resource_failure', True
+ elif isinstance(error, WorkerExit) and not (metrics or {}).get('error'):
+ kind, outcome, retry = 'unknown_worker_exit', 'discarded', False
+ else:
+ kind, outcome, retry = 'code_error', 'code_failure', False
+ result = dict(kind=kind, outcome=outcome, retryable=retry, reason=str(error))
+ allocation = getattr(error, 'freevideo_allocation', details.get('allocation'))
+ if isinstance(allocation, dict) and type(allocation.get('fp8_ff_activation_stash_bytes')) is int and allocation['fp8_ff_activation_stash_bytes'] > 0:
+ result['allocation'] = {'fp8_ff_activation_stash_bytes': allocation['fp8_ff_activation_stash_bytes']}
+ from .diagnostic_resources import exception_details
+ captured = exception_details(error)
+ result['exception'] = details.get('exception') or captured
+ if details.get('exception') and captured:
+ result['controller_exception'] = captured
+ # Preserve the worker's failure-time evidence when the controller wraps its
+ # exit; the controller's CUDA context cannot reconstruct these readings.
+ for key in ('gpu', 'memory', 'memory_collection_error'):
+ if key in details:
+ result[key] = details[key]
+ device_memory = metrics.get('device_memory', details.get('device_memory'))
+ if kind == 'gpu_oom' and isinstance(device_memory, dict):
+ result['device_memory'] = device_memory
+ telemetry = getattr(error, 'telemetry', None)
+ if kind == 'ram_pressure' and isinstance(telemetry, dict) and isinstance(telemetry.get('ram_guard'), dict):
+ # Preserve the actual monitor decision; recovery must not parse rounded
+ # GiB values out of an exception or mistake commit/RSS for working RAM.
+ result['ram_guard'] = {key: telemetry['ram_guard'].get(key) for key in
+ ('reasons', 'working_bytes', 'budget_bytes', 'available_bytes', 'emergency_floor_bytes',
+ 'physical_available_bytes', 'commit_available_bytes') if key in telemetry['ram_guard']}
+ if cleanup:
+ result['cleanup_evidence'] = [str(item) for item in cleanup]
+ if getattr(error, 'worker_stop_confirmed', None) is False:
+ result['worker_stop_error'] = getattr(error, 'worker_stop_error', 'Worker exit could not be confirmed')
+ return result
+
+
+def history_path():
+ return data_root() / 'resource-history.sqlite3'
+
+
+def benchmark_allocator_limit(profile, explicit_gib, hardware):
+ """Resolve the actually enforced optional cap before any model is loaded.
+
+ An explicit CLI value overrides a saved profile. Profile byte values stay
+ integers; silently rounding malformed metadata could fingerprint a cap that
+ the worker does not enforce. This helper performs no device calls.
+ """
+ if explicit_gib is not None:
+ if (type(explicit_gib) not in (int, float) or not math.isfinite(explicit_gib)
+ or explicit_gib <= 0):
+ raise ValueError('--allocator-limit-gib must be positive and finite')
+ requested = explicit_gib * 2**30
+ if not math.isfinite(requested):
+ raise ValueError('--allocator-limit-gib exceeds representable memory capacity')
+ value = round(requested)
+ else:
+ value = profile.get('benchmark_allocator_limit_bytes')
+ if value is None:
+ return None
+ if type(value) is not int or value <= 0:
+ raise ValueError('Benchmark allocator limit must be positive integer bytes')
+ observed = hardware.to_dict() if hasattr(hardware, 'to_dict') else hardware
+ total = observed.get('vram_total')
+ if type(total) not in (int, float) or not math.isfinite(total) or total <= 0:
+ raise ValueError('Detected physical VRAM is required for an explicit allocator cap')
+ if value > total:
+ raise ValueError('Benchmark allocator limit exceeds detected physical VRAM')
+ return value
+
+
+def demonstrated_ram_bytes(history, identity, canvas):
+ """Largest RAM peak a complete verified request reached on this machine.
+
+ Only the weight cache may use this, and only because it is a measurement
+ of this machine rather than an assumption about it. Every fence is already
+ enforced elsewhere and relied on here:
+
+ * ``observations`` filters ``state='success'`` and exact-matches the whole
+ software identity -- GPU UUID and driver, system, capability, package
+ versions, kernel overlay, model manifest, and the digest of all 34
+ compute source files. A different build or device cannot contribute.
+ * ``complete_observation`` is the only writer, and it refuses anything
+ that is not a finished sampling plan with matching geometry and
+ verified retained frames and audio: "A probe or incomplete sample
+ cannot become a resource observation."
+ * The geometry is matched here as well, because a RAM peak scales with
+ the request.
+ * The recorded RAM metric must match the one this platform reports now.
+ Windows measures a private working set and Linux a guard PSS; a peak
+ read under one metric says nothing about capacity under the other, and
+ 'unknown' says nothing at all.
+
+ Returns None when this machine has no such evidence, which leaves the
+ plan exactly as it would have been.
+ """
+ if history is None or identity is None or canvas is None:
+ return None
+ keys = ('width', 'height', 'frames', 'fps')
+ try:
+ from .ram import ProcessMemory
+ metric = ProcessMemory().sample(os.getpid()).get('ram_guard_metric')
+ rows = history.observations(identity=identity, limit=512, include_details=False)
+ except Exception:
+ # Planning must not fail because the local history or a live sample is
+ # unreadable; it falls back to the reported availability.
+ return None
+ if not metric or metric == 'unknown':
+ return None
+ peaks = []
+ for row in rows:
+ geometry = row.get('geometry') or {}
+ if any(geometry.get(key) != canvas.get(key) for key in keys):
+ continue
+ observation = row.get('observation') or {}
+ if observation.get('ram_metric') != metric:
+ continue
+ peak = observation.get('ram_peak_bytes')
+ if type(peak) is int and peak > 0:
+ peaks.append(peak)
+ return max(peaks) if peaks else None
+
+
+def demonstrated_local_ram(hardware, args, canvas):
+ """Best-effort wrapper: never let this reduce a plan to an exception.
+
+ Planning has to work on a machine with no history, no readable history, no
+ prepared cache and no stable GPU UUID. Every one of those simply means
+ there is no evidence, which is the same as a first run.
+ """
+ cache = getattr(args, 'cache', None)
+ if cache is None:
+ return None
+ try:
+ from .resource_history import ResourceHistory
+ return demonstrated_ram_bytes(ResourceHistory(history_path()),
+ local_identity(hardware, cache), canvas)
+ except Exception:
+ return None
+
+
+def local_identity(hardware, cache):
+ """Exact software identity for comparable performance measurements."""
+ from .tuning import COMPUTE_FILES, digest, package_versions
+ from .kernel_capabilities import identity as kernel_identity
+ observed = hardware.to_dict()
+ uuid = observed.get('gpu_uuid')
+ if not uuid:
+ visible = os.environ.get('CUDA_VISIBLE_DEVICES', '').split(',')[0]
+ if visible.startswith('GPU-'):
+ uuid = visible
+ if not uuid:
+ raise ValueError('A stable GPU UUID is required to retain local failure history.')
+ package = Path(__file__).parent
+ kernels = kernel_identity(hardware)
+ return dict(gpu={'uuid': uuid, 'name': observed['gpu_name'],
+ 'driver_version': observed.get('driver_version', '')},
+ system=observed.get('system', platform.system()), capability=list(observed['capability']),
+ identity_schema=1, dependency_manifest_sha256=digest(package/'dependencies.json'),
+ manifest_sha256=digest(Path(cache)/'manifest.json'),
+ engine_packages=package_versions(),
+ kernel_packages=kernels['packages'], fa4_overlay_sha256=kernels['fa4_overlay_sha256'],
+ source_sha256={name: digest(package/name) for name in COMPUTE_FILES})
+
+
+def attempt_geometry(canvas, engine, text_tokens=None):
+ value = dict(canvas, steps=engine.get('steps', 8), task=engine.get('task', 't2va'))
+ if text_tokens is not None:
+ value['text_tokens'] = text_tokens
+ return value
+
+
+def reduced_pinning(profile, failure, phase, previous_ram_retries):
+ """One measured partial reduction before the minimal-memory fallback."""
+ guard = failure.get('ram_guard')
+ if previous_ram_retries or phase not in ('load', 'sample') or not isinstance(guard, dict):
+ return None
+ if guard.get('reasons') != ['working_memory_budget']:
+ return None
+ keys = ('working_bytes', 'budget_bytes', 'available_bytes', 'emergency_floor_bytes')
+ if any(type(guard.get(k)) not in (int, float) or not math.isfinite(guard[k]) or guard[k] < 0 for k in keys):
+ return None
+ engine = profile['engine']
+ pin = engine.get('pin_host_gb', 0.)
+ if type(pin) not in (int, float) or not math.isfinite(pin) or pin <= 0:
+ return None
+ pin *= 1e9
+ deficit = guard['working_bytes'] - guard['budget_bytes']
+ if (deficit <= 0 or deficit > .25 * guard['budget_bytes']
+ or guard['available_bytes'] < guard['emergency_floor_bytes'] + 2 * 2**30):
+ return None
+ # Leave useful weights resident in host memory; do not jump from a 0.11
+ # GiB overflow to zero pinned weights on a 56 GiB Windows host. A complete
+ # layer is the unit of pinning. This is a retry candidate, not a fit proof.
+ from .policy import BLOCK_BYTES
+ release = math.ceil(max(2 * 2**30, deficit + 2**30, .15 * pin) / BLOCK_BYTES) * BLOCK_BYTES
+ remaining = max(0, pin - release)
+ if remaining < 2 * BLOCK_BYTES:
+ return None
+ return remaining / 1e9, dict(mode='partial-pinning-reduction', observed_overflow_bytes=deficit,
+ planned_release_bytes=release, retained_pin_budget_bytes=remaining,
+ reason='Only the working-memory budget was exceeded and system headroom remains. '
+ 'Retain host caches with a smaller pin budget; a second RAM failure uses the bounded streaming fallback.')
+
+
+def next_placement(profile, failure, phase, canvas=None, *, previous_ram_retries=0, previous_gpu_retries=0,
+ sampling_complete=False):
+ """A bounded neutral fallback; never change request, backend or chunking.
+
+ Free activation space in one useful step instead of spending many full
+ requests lowering residency two blocks at a time. This is recovery only;
+ success does not declare the fallback faster than the initial candidate.
+ """
+ value = copy.deepcopy(profile)
+ engine, decoder = value['engine'], value['decoder']
+ changes = {}
+ resume_finalization = phase == 'sample_finalize' and sampling_complete
+ ram_recovery = None
+ allocation = failure.get('allocation')
+ stash_bytes = allocation.get('fp8_ff_activation_stash_bytes') if isinstance(allocation, dict) else None
+ recompute_recovery = (failure['kind'] == 'gpu_oom' and phase == 'sample'
+ and type(stash_bytes) is int and stash_bytes > 0
+ and engine.get('inference_kernels') and engine.get('ff_chunk', 0) > 0
+ and engine.get('linear_compute', 'native-fp8') == 'native-fp8' and not engine.get('fp8_ff_recompute'))
+ def change(section, name, new):
+ options = engine if section == 'engine' else decoder
+ if options.get(name) != new:
+ changes[section+'.'+name] = {'from': options.get(name), 'to': new}
+ options[name] = new
+ if failure['kind'] == 'gpu_oom':
+ if recompute_recovery:
+ # Keep the up/down GEMM row shapes, FP8 global scale and requested
+ # output. Only regenerate identical FF tiles instead of stashing
+ # the full intermediate matrix which actually failed to allocate.
+ change('engine', 'fp8_ff_recompute', True)
+ elif phase == 'decode' or resume_finalization:
+ change('decoder', 'offload', True)
+ change('decoder', 'prefetch', False)
+ change('decoder', 'stream_output', True)
+ if decoder.get('resident_blocks', 0):
+ change('decoder', 'resident_blocks', 0)
+ if value['inference_ram_budget_gb'] * 1e9 < 14 * 2**30:
+ change('decoder', 'stream_weights', True)
+ change('decoder', 'pin_weights', False)
+ change('decoder', 'preload', False)
+ else:
+ if engine.get('head_parallelism', 1) > 1:
+ # First remove extra simultaneous workspaces. Keep all GPU
+ # weights and the original arithmetic partitions on this retry.
+ change('engine', 'head_parallelism', 1)
+ else:
+ memory = failure.get('device_memory', {})
+ limit = memory.get('effective_allocator_limit_bytes')
+ resident = engine.get('resident_blocks', 0)
+ from .policy import BLOCK_BYTES
+ partial = (not previous_gpu_retries and memory.get('windows_allocator_limit_enforced') is True
+ and type(limit) is int and limit > 0 and type(resident) is int and resident > 0)
+ if partial:
+ # The first allocator OOM frees a useful workspace, not all
+ # 50 weights. Full residency gains transfer slots when it is
+ # reduced: budget those too. This is a measured-failure
+ # recovery candidate, not a claim about the optimal count.
+ blocks = math.ceil(max(2 * 2**30, .1 * limit) / BLOCK_BYTES)
+ if resident == 50:
+ blocks += 2
+ change('engine', 'resident_blocks', max(0, resident - blocks))
+ else:
+ change('engine', 'resident_blocks', 0)
+ change('engine', 'prefetch', False)
+ # This route retains the exact arithmetic partitions used by
+ # the bounded small-card profile; only residual storage moves.
+ # Unknown/other chunk configurations remain independent trials.
+ if (engine.get('inference_kernels') and engine.get('head_chunk') == 2
+ and engine.get('ff_chunk') == 512 and engine.get('projection_chunk') == 512
+ and engine.get('attention_cpu_outputs') and engine.get('grouped_attention_outputs')
+ and engine.get('window_batch', 1) == 1):
+ change('engine', 'residual_offload', True)
+ # Offloading more GPU weights must not turn the next attempt
+ # into a whole-model RAM allocation on a 16 GiB machine.
+ from .system import weight_cache_headroom, residual_host_headroom
+ ram = value['inference_ram_budget_gb'] * 1e9
+ weights = (50 - engine['resident_blocks']) * BLOCK_BYTES
+ working = (2 if engine.get('attention_cpu_outputs') else .5) * 2**30
+ residual_host = residual_host_headroom(canvas) if engine.get('residual_offload') else 0
+ working += residual_host
+ if weights + working + 2 * 2**30 > ram:
+ change('engine', 'stream_weights', True)
+ host = weight_cache_headroom(system=value.get('policy', {}).get('hardware', {}).get('system', 'unknown'),
+ streamed=True, cpu_outputs=engine.get('attention_cpu_outputs', False), canvas=canvas)
+ host += residual_host
+ limit = max(0, min(weights, ram - host)) / 1e9
+ change('engine', 'pin_host_gb', min(engine.get('pin_host_gb', 0.), limit))
+ for name in ('pin_host_weights', 'preload_host'):
+ if engine.get(name):
+ change('engine', name, False)
+ elif partial and not engine.get('preload_host') and not engine.get('pin_host_weights'):
+ from .system import HOST_WEIGHT_HEADROOM
+ added = (resident - engine['resident_blocks']) * BLOCK_BYTES
+ pins = engine.get('pin_host_gb', 0.) * 1e9 + added
+ change('engine', 'pin_host_gb', round(max(0, min(weights, ram - HOST_WEIGHT_HEADROOM, pins))/1e9, 3))
+ elif failure['kind'] == 'ram_pressure':
+ partial = reduced_pinning(value, failure, phase, previous_ram_retries)
+ if partial:
+ amount, ram_recovery = partial
+ change('engine', 'pin_host_gb', amount)
+ # Legacy all-host modes would bypass a bounded partial budget.
+ for name in ('preload_host', 'pin_host_weights'):
+ if engine.get(name):
+ change('engine', name, False)
+ else:
+ for name in ('pin_host_gb', 'preload_host', 'pin_host_weights'):
+ if name in engine:
+ change('engine', name, 0. if name == 'pin_host_gb' else False)
+ change('engine', 'prefetch', False)
+ change('engine', 'stream_weights', True)
+ for name in ('pin_weights', 'preload', 'prefetch'):
+ if name in decoder:
+ change('decoder', name, False)
+ change('decoder', 'stream_output', True)
+ if decoder.get('offload'):
+ change('decoder', 'stream_weights', True)
+ if not changes and resume_finalization and failure['kind'] in ('gpu_oom', 'ram_pressure'):
+ # A fresh decode worker never loads the transformer whose finalization
+ # failed. This single transition remains within the normal retry bound.
+ return value, dict(reason='Reuse completed sampling in a fresh decoder worker after sampler finalization failed.',
+ changes={}, numerical_class='placement-only', measurement=failure,
+ ram_recovery=ram_recovery)
+ if not changes:
+ return None, {'reason': 'No further numerical-neutral placement recovery is available; '
+ 'chunk changes require local complete-request equivalence.', 'changes': {}}
+ if 'policy' in value:
+ value['policy'].update(engine=dict(engine), decoder=dict(decoder))
+ return value, dict(reason='Fresh worker after %s; preserve geometry, steps, seed, precision, '
+ 'attention and chunk arithmetic.' % failure['kind'], changes=changes,
+ numerical_class='same-shape-ff-recompute' if recompute_recovery else 'placement-only',
+ measurement=failure, ram_recovery=ram_recovery)
+
+
+def execute_attempts(history, identity, profile, canvas, launch, *,
+ validate, archive, report, persist, max_retries=2, automatic=True,
+ prepare=None, on_retry=None):
+ """Run one complete request in fresh children, retaining every failed attempt.
+
+ Callbacks make process death, OOM and promotion testable without a tensor
+ runtime. ``begin`` commits before ``launch`` is invoked. No exception handler
+ uses failed measurements to certify a profile.
+ """
+ current = copy.deepcopy(profile)
+ from .diagnostic_resources import attempt_metrics
+ if type(max_retries) is not int or not 0 <= max_retries <= 2:
+ raise ValueError('Resource retries must be an integer between zero and two.')
+ report.setdefault('resource_attempts', [])
+ evidence = {'reason': 'Initial automatic placement' if automatic else 'Explicit reproducible profile'}
+ for index in range(max_retries + 1):
+ if prepare is not None:
+ prepare(current)
+ evidence['request_artifacts'] = report.get('artifacts')
+ if report.get('resource_prediction') is not None:
+ evidence['prediction'] = copy.deepcopy(report['resource_prediction'])
+ geometry = attempt_geometry(canvas, current['engine'])
+ config = current
+ attempt = history.begin(identity, config, geometry, purpose='generation',
+ evidence=evidence)
+ row = dict(id=attempt, profile=copy.deepcopy(current), state='started', decision=evidence)
+ report['resource_attempts'].append(row)
+ report['profile'] = current
+ persist()
+ started = time.perf_counter()
+ try:
+ metrics, telemetry = launch(current, attempt)
+ observation = validate(metrics, telemetry, current)
+ except BaseException as error:
+ metrics = getattr(error, 'metrics', None)
+ failure = classify_failure(error, metrics)
+ details = dict(failure)
+ if isinstance(metrics, dict):
+ details['failed_phase'] = metrics.get('phase')
+ telemetry = getattr(error, 'telemetry', None)
+ if isinstance(telemetry, dict):
+ details['incomplete_resource_observations'] = telemetry
+ row['resources'] = telemetry
+ row['phase'] = metrics.get('phase', 'unknown') if isinstance(metrics, dict) else 'unknown'
+ row['seconds'] = time.perf_counter() - started
+ if isinstance(metrics, dict):
+ row['metrics'] = attempt_metrics(metrics)
+ history.finish(attempt, failure['outcome'], details=details)
+ row.update(state=failure['outcome'], failure=failure)
+ persist()
+ if not automatic or not failure['retryable'] or index == max_retries:
+ raise
+ previous_ram_retries = sum(r.get('failure', {}).get('kind') == 'ram_pressure'
+ for r in report['resource_attempts'][:-1])
+ previous_gpu_retries = sum(r.get('failure', {}).get('kind') == 'gpu_oom'
+ for r in report['resource_attempts'][:-1])
+ from .decode_resume import completed_sampling
+ updated, decision = next_placement(current, failure, (metrics or {}).get('phase', 'sample'), canvas,
+ previous_ram_retries=previous_ram_retries,
+ previous_gpu_retries=previous_gpu_retries,
+ sampling_complete=completed_sampling(metrics))
+ if updated is None:
+ row['recovery_note'] = decision['reason']
+ persist()
+ raise
+ row['retained'] = archive(index, row)
+ current, evidence = updated, decision
+ persist()
+ if on_retry is not None:
+ on_retry(index, row, current, decision)
+ continue
+ # Failure to commit success must not trigger another GPU attempt.
+ history.finish(attempt, 'success', observation=observation,
+ details={'reason': 'Complete request, retained output and metrics verified.',
+ 'prediction_error': report.get('resource_prediction_error')})
+ row.update(state='success', phase='complete', observation=observation, resources=telemetry,
+ seconds=time.perf_counter() - started, metrics=attempt_metrics(metrics))
+ report['profile'] = current
+ persist()
+ return metrics, telemetry, current
+
+
+def complete_observation(metrics, canvas, output, artifacts, telemetry):
+ """Validate existing results without rerunning a model or decoding video again."""
+ from .system import memory_complete, memory_peak
+ from .validation import _retained_array_header, finite_number
+ expected = canvas['frames']
+ if (metrics.get('success') is not True or metrics.get('phase') != 'complete'
+ or metrics.get('finite_latents') is not True or metrics.get('frames') != expected
+ or any(metrics.get('geometry', {}).get(k) != canvas[k] for k in ('width','height','frames','fps'))):
+ raise ValueError('A successful complete request with matching geometry is required.')
+ steps = metrics.get('config', {}).get('steps')
+ times = metrics.get('step_seconds', [])
+ from .two_pass import steps as sampling_steps, same_strategy
+ total_steps = sampling_steps(metrics.get('sampling_plan'))
+ if (type(steps) is not int or steps != 8 or len(times) != total_steps
+ or not same_strategy(metrics, canvas) or not all(finite_number(t) and t > 0 for t in times)):
+ raise ValueError('A probe or incomplete sample cannot become a resource observation.')
+ rgb_shape, dtype = _retained_array_header(Path(artifacts)/'rgb.npy')
+ if str(dtype) != 'uint8' or rgb_shape != (expected, canvas['height'], canvas['width'], 3):
+ raise ValueError('Retained video frames do not match the complete request.')
+ audio_shape, audio_dtype = _retained_array_header(Path(artifacts)/'audio.npy')
+ if (str(audio_dtype) != 'float32' or len(audio_shape) != 2 or audio_shape[0] != 2
+ or abs(audio_shape[1]/32000. - expected/canvas['fps']) > 1/canvas['fps']):
+ raise ValueError('Retained stereo audio does not match the request duration.')
+ import av
+ with av.open(str(output)) as container:
+ video, audio = container.streams.video, container.streams.audio
+ if len(video) != 1 or len(audio) != 1:
+ raise ValueError('The completed MP4 must contain video and audio.')
+ stream = video[0]
+ if ((stream.width, stream.height, stream.frames) != (canvas['width'],canvas['height'],expected)
+ or float(stream.average_rate) != canvas['fps'] or audio[0].codec_context.channels != 2):
+ raise ValueError('MP4 metadata does not match the requested output.')
+ if metrics.get('sampling_reused') is True:
+ # The output is validated, but these observations cover only a decoder
+ # retry. Do not teach placement/ETA that a full request cost this little
+ # time or occupied only the decoder's memory.
+ return None
+ ram, gpu = telemetry.get('ram', {}), telemetry.get('gpu', {})
+ counts = (ram.get('ram_observation_samples'), gpu.get('samples'))
+ if (not memory_complete(ram) or any(type(n) is not int or n <= 0 for n in counts)
+ or gpu.get('sampling_errors')):
+ raise ValueError('Complete request finished but resource observations are incomplete.')
+ if any(type(value) is not int or value <= 0 for value in (memory_peak(ram), gpu.get('gpu_peak_bytes'))):
+ raise ValueError('Resource observations require positive measured RAM and GPU peaks.')
+ peaks = [metrics.get(name) for name in ('load_peak_reserved_bytes','torch_peak_reserved_bytes',
+ 'final_stage_peak_reserved_bytes')]
+ if any(type(x) is not int or x <= 0 for x in peaks):
+ raise ValueError('Missing full-phase GPU allocator peaks.')
+ stages = {name: metrics.get(name) for name in ('load_seconds','sample_seconds','vae_load_seconds',
+ 'video_decode_seconds','audio_load_decode_seconds','encode_seconds','decode_save_seconds','work_seconds')}
+ if not all(finite_number(t) for t in stages.values()):
+ raise ValueError('Incomplete per-stage timings.')
+ result = dict(full_request=True, validated=True, completed_steps=total_steps,
+ completed_frames=expected, media_verified=True, metrics_complete=True,
+ peak_reserved_bytes=max(peaks), whole_gpu_peak_bytes=gpu['gpu_peak_bytes'],
+ ram_peak_bytes=memory_peak(ram), ram_metric=ram.get('ram_guard_metric', 'unknown'),
+ step_seconds=times, stage_seconds=stages,
+ execution_context={
+ 'adaln_table_cache_hits': metrics.get('config', {}).get('adaln_table_cache_hits'),
+ 'host_threads': metrics.get('execution_context', {}).get('host_threads'),
+ 'allocator_limit_bytes': metrics.get('device_memory', {}).get('benchmark_allocator_limit_bytes')},
+ load_breakdown=metrics.get('load_breakdown', {}),
+ evidence='Completed original request; existing array and MP4 metadata verified. '
+ 'This is a resource observation, not proof of numerical equivalence to another configuration.')
+ shape = metrics.get('conditioning_shape')
+ if shape and len(shape) == 2 and shape[1] == 5120:
+ result['text_tokens'] = shape[0]
+ return result
diff --git a/freevideo_engine/assets/icon.ico b/freevideo_engine/assets/icon.ico
new file mode 100644
index 0000000..24e2935
Binary files /dev/null and b/freevideo_engine/assets/icon.ico differ
diff --git a/freevideo_engine/assets/icon.png b/freevideo_engine/assets/icon.png
new file mode 100644
index 0000000..b646ab5
Binary files /dev/null and b/freevideo_engine/assets/icon.png differ
diff --git a/freevideo_engine/assets/wordmark.png b/freevideo_engine/assets/wordmark.png
new file mode 100644
index 0000000..59fc258
Binary files /dev/null and b/freevideo_engine/assets/wordmark.png differ
diff --git a/freevideo_engine/attention.py b/freevideo_engine/attention.py
new file mode 100644
index 0000000..337b40f
--- /dev/null
+++ b/freevideo_engine/attention.py
@@ -0,0 +1,202 @@
+"""Official decomposed attention or portable cuDNN/Sage window calls."""
+import types
+import importlib.metadata
+from collections import defaultdict
+
+import torch
+import torch.nn.functional as F
+from torch.nn.attention import SDPBackend, sdpa_kernel
+
+from .paths import add_vdn
+add_vdn()
+from src.models.softmax_attention.decomposed import _plan, window_softmax_decomposed
+
+
+ALIASES = {'original': 'cudnn/fa4', 'dense': 'cudnn/cudnn',
+ 'sage2': 'sage2/sage2', 'sage2-window': 'cudnn/sage2',
+ 'fa2-window': 'cudnn/fa2', 'sdpa': 'torch-flash/torch-flash'}
+BACKENDS = ('cudnn', 'torch-flash', 'sage2', 'fa2', 'fa4')
+
+
+def split_backend(backend):
+ parts = ALIASES.get(backend, backend).split('/')
+ if len(parts) == 1:
+ parts *= 2
+ if len(parts) != 2 or any(p not in BACKENDS for p in parts):
+ raise ValueError('Attention must be a backend or global/window pair: ' + ', '.join(BACKENDS))
+ return tuple(parts)
+
+
+class WindowAttention:
+ def __init__(self, backend, query_chunk=0, window_batch=1, window_varlen=False,
+ varlen_smooth_k=True):
+ if window_batch < 1:
+ raise ValueError('Window batch must be positive')
+ if window_varlen and query_chunk:
+ raise ValueError('Packed varlen windows do not support a query-chunk override')
+ self.backend = backend
+ self.query_chunk = query_chunk
+ self.window_batch = window_batch
+ # One packed call replaces the per-window Python loop. Sage2 smooths
+ # keys over the whole packed batch, while the loop smooths each window
+ # separately, so this is a reported numerical change, never implied.
+ self.window_varlen = window_varlen
+ self.varlen_smooth_k = varlen_smooth_k
+ self.current_plan = None
+ self.offsets = None
+ self.batches = None
+ self.calls = 0
+ self.window_calls = 0
+ self.backend_calls = {part + '_' + name: 0 for part in ('global', 'window')
+ for name in (*BACKENDS, 'fa2_varlen', 'fa4_varlen', 'sage2_varlen')}
+ self.select_backend(backend)
+
+ def batched(self, q, k, v, scale, *, window=False):
+ leg = self.window_backend if window else self.global_backend
+ self.calls += 1
+ self.backend_calls[('window_' if window else 'global_') + leg] += 1
+ if leg == 'sage2':
+ return self.attention(q, k, v, tensor_layout='NHD', is_causal=False, sm_scale=scale)
+ if leg == 'fa2':
+ from flash_attn.flash_attn_interface import flash_attn_func
+ return flash_attn_func(q, k, v, softmax_scale=scale, causal=False)
+ if leg == 'fa4':
+ from flash_attn.cute import flash_attn_func
+ result = flash_attn_func(q, k, v, softmax_scale=scale, causal=False)
+ return result[0] if isinstance(result, tuple) else result
+ backend = SDPBackend.CUDNN_ATTENTION if leg == 'cudnn' else SDPBackend.FLASH_ATTENTION
+ with sdpa_kernel(backend):
+ result = F.scaled_dot_product_attention(q.transpose(1, 2), k.transpose(1, 2),
+ v.transpose(1, 2), scale=scale)
+ return result.transpose(1, 2)
+
+ def dense(self, q, k, v, scale, *, window=False):
+ return self.batched(q.unsqueeze(0), k.unsqueeze(0), v.unsqueeze(0), scale, window=window)[0]
+
+ def select_backend(self, backend):
+ self.global_backend, self.window_backend = split_backend(backend)
+ if 'fa4' in (self.global_backend, self.window_backend):
+ from .fa4_guard import check_selected
+ check_selected()
+ if self.window_backend in ('fa2', 'fa4') and self.query_chunk:
+ raise ValueError('Packed varlen windows do not support a query-chunk override')
+ if self.window_varlen and self.window_backend not in ('sage2', 'fa2', 'fa4'):
+ raise ValueError('Packed varlen windows require a sage2, fa2 or fa4 window backend')
+ if 'sage2' in (self.global_backend, self.window_backend):
+ if not importlib.metadata.version('sageattention').startswith('2.'):
+ raise ImportError('The sage2 route requires SageAttention 2.x; the tested version is 2.2.0.')
+ from sageattention import sageattn
+ self.attention = sageattn
+ if self.window_varlen and self.window_backend == 'sage2':
+ from sageattention import sageattn_varlen
+ self.attention_varlen = sageattn_varlen
+ if 'fa2' in (self.global_backend, self.window_backend):
+ if not importlib.metadata.version('flash-attn').startswith('2.'):
+ raise ImportError('The fa2 route requires flash-attn 2.x.')
+ from flash_attn.flash_attn_interface import flash_attn_varlen_func
+ self.backend = backend
+
+ def _window_batches(self, plan):
+ # Windows may share shapes but never keys/softmax normalization. The
+ # batch dimension preserves each exact mask, including anchor columns.
+ # Cache only indices; activations are gathered one bounded batch at a time.
+ groups = defaultdict(list)
+ q_offsets, k_offsets = self.offsets
+ for qa, qb, ka, kb in zip(q_offsets, q_offsets[1:], k_offsets, k_offsets[1:]):
+ chunk = self.query_chunk or qb - qa
+ for start in range(qa, qb, chunk):
+ end = min(start + chunk, qb)
+ groups[end - start, kb - ka].append((start, end, ka, kb))
+ batches = []
+ for windows in groups.values():
+ for start in range(0, len(windows), self.window_batch):
+ selected = windows[start:start + self.window_batch]
+ rows = torch.stack([plan.win_q[qa:qb] for qa, qb, _, _ in selected])
+ keys = torch.stack([plan.kv_gather[ka:kb] for _, _, ka, kb in selected])
+ batches.append((rows, keys))
+ return batches
+
+ def prepare(self, layout, bounds, device, anchor_frames='none', *, batches=False):
+ """Create shared indices on the caller stream before parallel readers."""
+ plan = _plan(layout, bounds, anchor_frames, device)
+ if plan is not self.current_plan:
+ self.offsets = (plan.cu_q.tolist(), plan.cu_k.tolist()) if plan.has_windows else ([], [])
+ self.current_plan = plan
+ self.batches = None
+ if (batches and plan.has_windows and self.window_batch > 1 and not self.window_varlen
+ and self.window_backend not in ('fa2', 'fa4') and self.batches is None):
+ self.batches = self._window_batches(plan)
+ return plan
+
+ def __call__(self, q, k, v, layout, bounds, scale, anchor_frames='none'):
+ if (self.global_backend, self.window_backend) == ('cudnn', 'fa4'):
+ plan = _plan(layout, bounds, anchor_frames, q.device)
+ self.backend_calls['global_cudnn'] += bool(len(plan.dense_q))
+ self.backend_calls['window_fa4_varlen'] += bool(plan.has_windows)
+ self.window_calls += 1
+ return window_softmax_decomposed(q, k, v, layout, bounds, scale, anchor_frames)
+ plan = self.prepare(layout, bounds, q.device, anchor_frames)
+ out = torch.empty_like(q)
+ if len(plan.dense_q):
+ chunk = self.query_chunk or len(plan.dense_q)
+ for start in range(0, len(plan.dense_q), chunk):
+ rows = plan.dense_q[start:start + chunk]
+ out[rows] = self.dense(q[rows], k, v, scale)
+ if plan.has_windows:
+ if self.window_varlen and self.window_backend == 'sage2':
+ # Every window in one kernel: no Python loop, no per-window
+ # gather temporaries. Keys are smoothed across the packed batch.
+ out[plan.win_q] = self.attention_varlen(
+ q[plan.win_q], k[plan.kv_gather], v[plan.kv_gather],
+ cu_seqlens_q=plan.cu_q, cu_seqlens_k=plan.cu_k,
+ max_seqlen_q=plan.max_q, max_seqlen_k=plan.max_k,
+ sm_scale=scale, is_causal=False, smooth_k=self.varlen_smooth_k)
+ self.backend_calls['window_sage2_varlen'] += 1
+ elif self.window_backend in ('fa2', 'fa4'):
+ if self.window_backend == 'fa2':
+ from flash_attn.flash_attn_interface import flash_attn_varlen_func
+ else:
+ from flash_attn.cute import flash_attn_varlen_func
+ result = flash_attn_varlen_func(q[plan.win_q], k[plan.kv_gather], v[plan.kv_gather],
+ cu_seqlens_q=plan.cu_q, cu_seqlens_k=plan.cu_k,
+ max_seqlen_q=plan.max_q, max_seqlen_k=plan.max_k,
+ softmax_scale=scale, causal=False)
+ out[plan.win_q] = result[0] if isinstance(result, tuple) else result
+ self.backend_calls['window_' + self.window_backend + '_varlen'] += 1
+ elif self.window_batch > 1:
+ if self.batches is None:
+ self.batches = self._window_batches(plan)
+ for rows, keys in self.batches:
+ # NHD with an explicit batch dimension. Do not concatenate
+ # sequence lengths: that would mix unrelated attention masks.
+ qw, kw, vw = q[rows], k[keys], v[keys]
+ result = self.batched(qw, kw, vw, scale, window=True)
+ out[rows] = result
+ del qw, kw, vw, result
+ self.window_calls += 1
+ return out
+ else:
+ q_offsets, k_offsets = self.offsets
+ for qa, qb, ka, kb in zip(q_offsets, q_offsets[1:], k_offsets, k_offsets[1:]):
+ keys = plan.kv_gather[ka:kb]
+ kw, vw = k[keys], v[keys]
+ chunk = self.query_chunk or qb - qa
+ for start in range(qa, qb, chunk):
+ rows = plan.win_q[start:min(start + chunk, qb)]
+ out[rows] = self.dense(q[rows], kw, vw, scale, window=True)
+ del kw, vw
+ self.window_calls += 1
+ return out
+
+ def install(self, transformer):
+ from src.models.hybrid_transform import iter_hybrids
+ policy = self
+
+ def window(attn, q, k, v, layout, bounds, scale, inference):
+ return policy(q, k, v, layout, bounds, scale, attn.anchor_frames)
+
+ count = 0
+ for attn in iter_hybrids(transformer):
+ attn._window_softmax = types.MethodType(window, attn)
+ count += 1
+ return count
diff --git a/freevideo_engine/benchmark.py b/freevideo_engine/benchmark.py
new file mode 100644
index 0000000..d961165
--- /dev/null
+++ b/freevideo_engine/benchmark.py
@@ -0,0 +1,284 @@
+"""Matched backend experiments. Sampling and full-request timing stay separate."""
+import argparse
+import copy
+import hashlib
+import importlib.metadata
+import json
+import os
+from pathlib import Path
+import statistics
+import subprocess
+import sys
+import time
+import traceback
+
+from .hardware import detect
+from .geometry import geometry
+from .kernel_capabilities import available_backends
+from .policy import choose
+from .monitoring import save
+from .locking import runtime_lock, LOCK_ENV
+from . import processes
+from .system import windows
+
+
+def main(arguments=None):
+ parser = argparse.ArgumentParser(prog='freevideo bench', description=__doc__)
+ parser.add_argument('--cache', type=Path, required=True)
+ parser.add_argument('--conditioning', type=Path, required=True)
+ parser.add_argument('--profile', type=Path)
+ parser.add_argument('--base', type=Path)
+ parser.add_argument('--checkpoint', type=Path)
+ parser.add_argument('--out', type=Path, required=True)
+ parser.add_argument('--backends', nargs='+', help='global/window pairs; default compares installed candidates')
+ parser.add_argument('--repeats', type=int, default=2)
+ parser.add_argument('--warmup-steps', type=int, default=1)
+ parser.add_argument('--mode', choices=['sampling', 'full'], default='sampling')
+ parser.add_argument('--require-ram-limit', action='store_true', help='Require a dedicated zero-swap cgroup in full mode')
+ parser.add_argument('--vram-gib', type=float, help='Optional capacity cap; default detects current GPU resources')
+ parser.add_argument('--ram-gib', type=float, help='Optional capacity cap; default detects current RAM resources')
+ parser.add_argument('--seed', type=int, default=2026090901)
+ parser.add_argument('--save-best-profile', type=Path, help='Export the fastest measured sampling policy for --profile')
+ args = parser.parse_args(arguments)
+ if args.repeats < 1 or not 1 <= args.warmup_steps <= 2:
+ parser.error('Use at least one repeat and one or two warmup steps')
+ if args.require_ram_limit and args.mode != 'full':
+ parser.error('--require-ram-limit is for full mode; use an outer capacity harness for sampling')
+ if windows() and args.require_ram_limit:
+ parser.error('Native Windows reports observed RAM; Linux cgroup hard limits are unavailable.')
+ if args.save_best_profile and args.mode != 'sampling':
+ parser.error('--save-best-profile currently exports sampling comparisons')
+ if args.save_best_profile and args.save_best_profile.exists():
+ parser.error('--save-best-profile already exists')
+ args.out.mkdir(parents=True, exist_ok=False)
+ hardware = detect()
+ available = available_backends(hardware)
+ if args.backends is None:
+ candidates = ['cudnn/fa4', 'cudnn/sage2', 'sage2/sage2', 'cudnn/cudnn', 'torch-flash/torch-flash']
+ if 'fa2' in available:
+ candidates += ['cudnn/fa2', 'fa2/fa2']
+ args.backends = [name for name in candidates if all(leg in available for leg in name.split('/'))]
+ if args.profile:
+ profile = json.loads(args.profile.read_text(encoding='utf-8'))
+ if 'gpu_budget_bytes' in profile:
+ profile = dict(engine=profile['engine'], decoder=profile['decoder'],
+ gpu_budget_gb=profile['gpu_budget_bytes'] / 1e9,
+ inference_ram_budget_gb=profile['ram_budget_bytes'] / 1e9,
+ allocator_config=profile['allocator_config'], policy=profile)
+ else:
+ profile = choose(hardware, vram_gib=args.vram_gib, ram_gib=args.ram_gib,
+ available_backends=available, canvas=geometry(frames=243)).legacy_profile()
+ if args.base:
+ profile['engine']['base'] = str(args.base.resolve())
+ if args.checkpoint:
+ profile['engine']['checkpoint'] = str(args.checkpoint.resolve())
+ os.environ['PYTORCH_ALLOC_CONF'] = profile['allocator_config']
+ os.environ['PYTORCH_CUDA_ALLOC_CONF'] = profile['allocator_config']
+ report = {'mode': args.mode, 'hardware': hardware.to_dict(), 'profile': profile,
+ 'requested_backends': args.backends,
+ 'source_sha256': {p.name: hashlib.sha256(p.read_bytes()).hexdigest()
+ for p in sorted(Path(__file__).parent.glob('*.py'))},
+ 'versions': {},
+ 'seed': args.seed, 'frames': 243, 'resolution': [1344, 768], 'steps': 8,
+ 'conditioning_sha256': hashlib.sha256(args.conditioning.read_bytes()).hexdigest(),
+ 'manifest_sha256': hashlib.sha256((args.cache / 'manifest.json').read_bytes()).hexdigest(),
+ 'scope': 'Identical model/cache/conditioning/seed and resource policy; only attention changes. Sampling excludes encoding/loading/decode. Full mode includes loading/decode, but uses cached conditioning.',
+ 'capacity_scope': 'Placement budgets plus observed memory. Hard-limit claims require a matching outer MPS/cgroup measurement.',
+ 'rows': [], 'warmups': [], 'success': False}
+ for name in ('torch', 'triton', 'triton-windows', 'sageattention', 'flash-attn', 'flash-attn-4', 'nvidia-cudnn-cu12',
+ 'nvidia-cutlass-dsl', 'cuda-bindings', 'cuda-core'):
+ try:
+ report['versions'][name] = importlib.metadata.version(name)
+ except importlib.metadata.PackageNotFoundError:
+ report['versions'][name] = None
+ save(args.out / 'comparison.json', report)
+ if args.mode == 'sampling':
+ sampling(args, profile, report)
+ else:
+ full(args, profile, report)
+ if not report['success']:
+ raise SystemExit(1)
+ if args.save_best_profile:
+ eligible = {name: seconds for name, seconds in report['sampling_medians_seconds'].items()
+ if all(row['gpu_peak_bytes'] <= profile['gpu_budget_gb'] * 1e9
+ for row in report['rows'] if row['backend'] == name)}
+ if not eligible:
+ raise RuntimeError('No backend stayed within the requested observed GPU budget')
+ selected = copy.deepcopy(profile)
+ selected['engine']['attention'] = min(eligible, key=eligible.get)
+ if 'policy' in selected:
+ selected['policy']['engine'] = copy.deepcopy(selected['engine'])
+ selected['selection'] = {'comparison': str((args.out / 'comparison.json').resolve()),
+ 'hardware': hardware.to_dict(), 'versions': report['versions'],
+ 'scope': 'Measured sampling only; complete-video capacity/quality validation remains separate.'}
+ save(args.save_best_profile, selected)
+
+
+def sampling(args, profile, report):
+ import torch
+ from .runtime import Engine
+ from .locking import runtime_lock
+ from .monitoring import Monitor
+ torch.set_num_threads(8)
+ options = copy.deepcopy(profile['engine'])
+ options['attention'] = args.backends[0]
+ monitor = Monitor(args.out / 'gpu.csv').start()
+ engine = None
+ try:
+ with runtime_lock():
+ report['current_attempt'] = {'phase': 'loading', 'backend': args.backends[0]}
+ save(args.out / 'comparison.json', report)
+ engine = Engine(args.cache, **dict(options, canvas=geometry(frames=243)))
+ report['load_seconds'] = engine.load_seconds
+ report['load_gpu_peak_bytes'] = monitor.peak
+ working = []
+ for backend in args.backends:
+ row = {'backend': backend, 'completed_steps': args.warmup_steps}
+ report['current_attempt'] = {'phase': 'warmup', 'backend': backend}
+ save(args.out / 'comparison.json', report)
+ try:
+ engine.attention.select_backend(backend)
+ _warmup(engine, args.conditioning, args.seed, args.warmup_steps)
+ row['status'] = 'complete'
+ working.append(backend)
+ except Exception as error:
+ row.update(status='error', error=f'{type(error).__name__}: {error}')
+ # CUDA execution failures can poison a context; don't rank
+ # later work performed in a failed context as valid evidence.
+ if not isinstance(error, (ImportError, ModuleNotFoundError, ValueError)):
+ report['warmups'].append(row)
+ save(args.out / 'comparison.json', report)
+ raise
+ report['warmups'].append(row)
+ save(args.out / 'comparison.json', report)
+ for repeat in range(args.repeats):
+ order = working if repeat % 2 == 0 else list(reversed(working))
+ for backend in order:
+ report['current_attempt'] = {'phase': 'sampling', 'backend': backend, 'repeat': repeat}
+ save(args.out / 'comparison.json', report)
+ engine.attention.select_backend(backend)
+ engine.config['attention'] = backend
+ monitor.peak = monitor.sample()[0]
+ video, audio, metrics = engine.sample(args.conditioning, args.seed)
+ finite = bool(torch.isfinite(video).all() and torch.isfinite(audio).all())
+ if not finite:
+ raise RuntimeError('Non-finite generated latents for ' + backend)
+ del video, audio
+ row = dict(backend=backend, repeat=repeat, status='complete', metrics=metrics,
+ gpu_peak_bytes=monitor.peak, actual_config=dict(engine.config),
+ finite_latents=finite)
+ report['rows'].append(row)
+ save(args.out / 'comparison.json', report)
+ print(json.dumps({'event': 'sample_complete', 'backend': backend, 'repeat': repeat,
+ 'seconds': metrics['sample_seconds'], 'gpu_peak_bytes': monitor.peak}), flush=True)
+ medians = {backend: statistics.median(r['metrics']['sample_seconds'] for r in report['rows'] if r['backend'] == backend)
+ for backend in working}
+ report['sampling_medians_seconds'] = medians
+ report['fastest_measured_backend'] = min(medians, key=medians.get) if medians else None
+ report['success'] = len(working) == len(args.backends) and bool(working)
+ report['current_attempt'] = None
+ except BaseException as error:
+ report['error'] = f'{type(error).__name__}: {error}'
+ report['traceback'] = traceback.format_exc()
+ raise
+ finally:
+ if engine is not None:
+ engine.close()
+ report['monitor'] = monitor.stop()
+ report['monitor']['gpu_peak_bytes'] = max([report.get('load_gpu_peak_bytes', 0)] + [r['gpu_peak_bytes'] for r in report['rows']] + [monitor.peak])
+ save(args.out / 'comparison.json', report)
+
+
+def _warmup(engine, conditioning, seed, steps):
+ class Warmed(Exception):
+ pass
+ count = 0
+ def before(module, inputs):
+ nonlocal count
+ count += 1
+ if count > steps:
+ raise Warmed()
+ hook = engine.transformer.register_forward_pre_hook(before, prepend=True)
+ try:
+ try:
+ engine.sample(conditioning, seed)
+ except Warmed:
+ pass
+ else:
+ raise RuntimeError('Warmup did not stop at the requested step boundary')
+ finally:
+ hook.remove()
+
+
+def full(args, profile, report):
+ with runtime_lock() as descriptor:
+ return full_locked(args, profile, report, descriptor)
+
+
+def full_locked(args, profile, report, descriptor):
+ from .paths import base_path
+ base = args.base or Path(profile['engine'].get('base', base_path()))
+ report['cache_policy'] = ('Windows: natural OS file cache; no eviction or cold-cache claim. Fresh compiler directories per request.' if windows() else
+ 'Model file pages evicted and checked before every request; separate fresh compiler directories for each request.')
+ try:
+ for repeat in range(args.repeats):
+ order = args.backends if repeat % 2 == 0 else list(reversed(args.backends))
+ for backend in order:
+ root = args.out / f'{repeat}-{backend.replace("/", "_")}'
+ root.mkdir()
+ selected = copy.deepcopy(profile)
+ selected['engine']['attention'] = backend
+ save(root / 'profile.json', selected)
+ cold = [sys.executable, '-m', 'freevideo_engine.cold_pages', '--cache', str(args.cache),
+ '--base', str(base), '--out', str(root / 'cold-pages.json')]
+ env = dict(os.environ, **{LOCK_ENV: str(descriptor)})
+ if not windows():
+ processes.run(cold, env=env, pass_fds=(descriptor,), check=True)
+ for variable, name in [('TORCHINDUCTOR_CACHE_DIR', 'inductor'), ('TRITON_CACHE_DIR', 'triton'), ('CUTE_DSL_CACHE_DIR', 'cute')]:
+ env[variable] = str(root / 'compiler' / name)
+ command = [sys.executable, '-m', 'freevideo_engine', 'generate', '--cache', str(args.cache),
+ '--conditioning', str(args.conditioning), '--profile', str(root / 'profile.json'),
+ '--out', str(root / 'video.mp4'), '--seed', str(args.seed)]
+ harness = [sys.executable, '-m', 'freevideo_engine.capacity',
+ '--budget-gb', str(profile['gpu_budget_gb']),
+ '--allocator-config', profile['allocator_config'], '--out', str(root / 'measurement')]
+ if args.require_ram_limit:
+ harness += ['--ram-gb', str(profile['inference_ram_budget_gb'])]
+ before = time.perf_counter()
+ if windows():
+ from .testing import run_process
+ observed = run_process(command, root, env, env.get('CUDA_VISIBLE_DEVICES', '0'),
+ timeout=10800, minimum_ram=2 * 2**30, pass_fds=(descriptor,),
+ maximum_gpu=profile['gpu_budget_gb'] * 1e9,
+ maximum_working_ram=profile['inference_ram_budget_gb'] * 1e9)
+ from .system import memory_complete
+ if observed['status'] == 'complete' and (observed['gpu'].get('gpu_peak_bytes') is None
+ or observed['gpu'].get('sampling_errors') or not memory_complete(observed['ram'])):
+ observed.update(status='measurement_failed', error='Complete GPU/RAM telemetry is required for a valid benchmark.')
+ measurement = root / 'measurement'
+ measurement.mkdir()
+ save(measurement / 'result.json', observed)
+ code, status = observed['returncode'], observed['status']
+ else:
+ command = harness + ['--', *command]
+ with (root / 'worker.log').open('w', encoding='utf-8') as log:
+ result = processes.run(command, env=env, stdout=log, stderr=subprocess.STDOUT, pass_fds=(descriptor,))
+ code, status = result.returncode, 'complete' if result.returncode == 0 else 'error'
+ row = {'backend': backend, 'repeat': repeat, 'status': status,
+ 'process_seconds': time.perf_counter() - before, 'exit_code': code, 'output': str(root)}
+ metrics = root / 'video.request.json'
+ if metrics.exists():
+ row['request'] = json.loads(metrics.read_text(encoding='utf-8'))
+ measured = root / 'measurement/result.json'
+ if measured.exists():
+ row['measurement'] = json.loads(measured.read_text(encoding='utf-8'))
+ report['rows'].append(row)
+ save(args.out / 'comparison.json', report)
+ print(json.dumps({'event': 'full_request_complete', **row}), flush=True)
+ report['success'] = all(r['status'] == 'complete' for r in report['rows'])
+ finally:
+ save(args.out / 'comparison.json', report)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freevideo_engine/blocks.py b/freevideo_engine/blocks.py
new file mode 100644
index 0000000..2f1af53
--- /dev/null
+++ b/freevideo_engine/blocks.py
@@ -0,0 +1,39 @@
+"""Release consumed activations and reuse branch outputs in fused H3 blocks."""
+import types
+import torch
+
+
+def _post_into_branch(residual, gate, indices, branch):
+ branch.copy_(residual + gate.index_select(0, indices) * branch)
+ return branch
+
+
+def install_bounded_blocks(model, residual_offload=False):
+ from src.models.ops.fused_block import _compiled, _pre_ref
+ pre = _compiled('pre', _pre_ref)
+ # Autotuning a mutating kernel clones its full branch input. That extra
+ # ~758 MiB copy defeats reuse under small memory limits. This elementwise
+ # kernel needs no shape search or reduction; use the fixed launch policy.
+ post = torch.compile(_post_into_branch, dynamic=False,
+ options={'triton.autotune_pointwise': False})
+
+ def forward(self, hidden_states, temb, adaln_indices, rotary_emb, attention_mask=None):
+ if torch.is_grad_enabled():
+ raise RuntimeError('Buffer reuse requires inference without autograd')
+ hidden, indices = hidden_states, adaln_indices
+ shift_a, scale_a, gate_a, shift_f, scale_f, gate_f = self.adaln_proj(temb)
+ normalized = pre(hidden, self.norm1.weight, self.norm1.eps, scale_a, shift_a, indices)
+ branch = self.attn(normalized, rotary_emb, attention_mask)
+ del normalized
+ hidden = post(hidden, gate_a, indices, branch)
+ del branch
+ normalized = pre(hidden, self.norm2.weight, self.norm2.eps, scale_f, shift_f, indices)
+ branch = self.ff(normalized)
+ del normalized
+ return post(hidden, gate_f, indices, branch)
+
+ if residual_offload:
+ from .residual import residual_forward
+ forward = residual_forward(pre, post)
+ for block in model.transformer_blocks:
+ block.forward = types.MethodType(forward, block)
diff --git a/freevideo_engine/bootstrap.py b/freevideo_engine/bootstrap.py
new file mode 100644
index 0000000..c6a8ee1
--- /dev/null
+++ b/freevideo_engine/bootstrap.py
@@ -0,0 +1,1331 @@
+"""Linux/Windows bootstrap. Python preflight uses stdlib and curl, without installation writes."""
+from __future__ import annotations
+import argparse
+import csv
+from contextlib import ExitStack
+import hashlib
+import json
+import os
+from pathlib import Path
+import platform
+import shutil
+import signal
+import subprocess
+import sys
+import tarfile
+import time
+import threading
+import urllib.request
+import zipfile
+from urllib.parse import unquote, urlsplit
+
+from .hardware import Hardware, GiB, cgroup_capacity
+from . import __version__
+from .policy import choose
+from .monitoring import save
+from .install_tuning import build_parallelism, required_models, cache_compatible, wheel_key
+from .terminal_ui import TerminalUI, LogProgress
+from .locking import runtime_lock, LOCK_ENV
+from .environments import ENVIRONMENTS, environment_names, select_layout, role_pythons, constraints_file, bootstrap_versions
+from . import network
+from . import processes
+from .system import install_root, venv_python, system_memory, nvidia_smi, memory_sample
+
+PACKAGE = Path(__file__).resolve().parent
+SOURCE = PACKAGE.parent
+DEFAULT_ROOT = install_root()
+
+
+def _permission_limited_memory_sample(row):
+ """Whether only descendant process queries were denied by Windows.
+
+ Windows can deny PROCESS_QUERY_LIMITED_INFORMATION for a child owned by a
+ different security context even though the install process and the system
+ memory counters remain readable. In that case the process-tree number is
+ incomplete, but the global emergency floor is still a valid safety guard.
+ Require a real sampled process count so synthetic/no-sample rows and a
+ failed process snapshot continue to fail closed.
+ """
+ if type(row.get('processes')) is not int or row['processes'] <= 0:
+ return False
+ pids = row.get('unreadable_memory_pids', row.get('unreadable_pss_pids', []))
+ errors = row.get('memory_read_errors', [])
+ if not pids or not errors:
+ return False
+ if not all(type(pid) is int and pid > 0 for pid in pids):
+ return False
+ return all(type(error) is dict and error.get('winerror') == 5 for error in errors)
+
+
+def setup_memory_status(row, verbose=False):
+ available = row.get('effective_available_bytes', row['system_available_bytes'])
+ if memory_sample(row) is None:
+ if row.get('monitor_status') == 'degraded':
+ return 'Process RAM partially unavailable · %.1f GiB available · continuing with system floor' % (available/GiB)
+ return 'Process RAM reading unavailable · %.1f GiB available · retrying' % (available/GiB)
+ if row.get('private_commit_bytes') is not None:
+ # Commit includes nonresident reservations; displaying it as RAM hid
+ # the cause of Windows file-mapping failures while physical RAM was free.
+ return ('RAM %.1f GiB · Commit %.1f GiB · Free RAM %.1f / commit %.1f GiB' %
+ (row['rss_bytes']/GiB, row['private_commit_bytes']/GiB,
+ row['system_physical_available_bytes']/GiB, row['system_commit_available_bytes']/GiB))
+ result = ('RAM %s %.2f GiB · system available %.2f GiB' %
+ (row.get('guard_metric', 'PSS'), (memory_sample(row) or 0)/GiB, available/GiB)
+ if verbose else 'Setup processes %.1f GiB · %.1f GiB available' %
+ ((memory_sample(row) or 0)/GiB, available/GiB))
+ if (row.get('cgroup_memory') or {}).get('limit_bytes') is not None:
+ result += ' within container limit'
+ return result
+
+
+def dependency_status(root, layout='unified', system=None):
+ """Inspect distribution metadata only: no tensor imports or downloads."""
+ result = {}
+ system = system or platform.system()
+ packages = {
+ 'engine': ['torch', 'torchvision', 'triton', 'transformers', 'accelerate', 'peft', 'safetensors',
+ 'huggingface-hub', 'omegaconf', 'pyyaml', 'einops', 'numpy', 'pillow', 'av',
+ 'psutil', 'tqdm', 'importlib-metadata', 'requests', 'httpx', 'socksio', 'pysocks',
+ 'modelscope-hub', 'hf-xet', 'sageattention', 'diffusers', 'freevideo-engine'],
+ 'encoder': ['torch', 'torchvision', 'torchaudio', 'freevideo-engine'] +
+ [s.strip() for s in (SOURCE / 'constraints/encoder-runtime.txt').read_text(encoding='utf-8').splitlines()
+ if s.strip() and not s.startswith('#')]}
+ packages['unified'] = sorted(set(packages['engine'] + packages['encoder']))
+ if system == 'Windows':
+ for name in packages:
+ packages[name] = ['triton-windows' if p == 'triton' else p for p in packages[name]]
+ for name in environment_names(layout):
+ names = packages[name]
+ expected = dict(line.strip().lower().split('==', 1) for line in
+ constraints_file(name, system).read_text(encoding='utf-8').splitlines() if '==' in line)
+ expected = {name.replace('_', '-'): version for name, version in expected.items()}
+ expected.update({'freevideo-engine': __version__, 'sageattention': '2.2.0'})
+ if system == 'Windows':
+ expected['sageattention'] = json.loads((PACKAGE / 'bootstrap_versions.json').read_text(encoding='utf-8'))['windows']['sageattention']['version']
+ python = venv_python(root / 'envs' / name, system)
+ row = {'python': str(python), 'exists': python.exists(), 'installed': {}, 'missing': sorted(set(names)), 'mismatched': {}}
+ if python.exists():
+ code = ('import importlib.metadata as m,json; '
+ 'print(json.dumps({d.metadata["Name"].lower().replace("_","-"):d.version for d in m.distributions()}))')
+ check = subprocess.run([str(python), '-B', '-c', code], capture_output=True, text=True)
+ if check.returncode:
+ row['error'] = check.stderr[-1000:]
+ else:
+ installed = json.loads(check.stdout)
+ row['installed'] = {p: installed.get(p) for p in sorted(set(names))}
+ row['missing'] = [p for p in sorted(set(names)) if p not in installed]
+ row['mismatched'] = {p: {'installed': installed[p], 'required': expected[p]}
+ for p in names if p in installed and p in expected and installed[p] != expected[p]}
+ result[name] = row
+ return result
+
+
+def digest(path, algorithm='sha256', git_blob=False):
+ h = hashlib.new(algorithm)
+ if git_blob:
+ h.update(('blob %d\0' % path.stat().st_size).encode())
+ with path.open('rb') as stream:
+ for chunk in iter(lambda: stream.read(8 * 1024 * 1024), b''):
+ h.update(chunk)
+ return h.hexdigest()
+
+
+def inventory(gpu=None):
+ smi = nvidia_smi()
+ query = 'index,uuid,name,compute_cap,memory.total,memory.free,driver_version,pci.bus_id'
+ result = subprocess.run([smi, '--query-gpu=' + query, '--format=csv,noheader,nounits'],
+ check=True, capture_output=True, text=True, timeout=20)
+ rows = [dict(zip(query.split(','), map(str.strip, row))) for row in csv.reader(result.stdout.splitlines())]
+ selector = gpu if gpu is not None else os.environ.get('CUDA_VISIBLE_DEVICES', '0').split(',')[0]
+ matches = [r for r in rows if r['index'] == selector or r['uuid'] == selector]
+ if len(matches) != 1:
+ raise ValueError('Select one physical GPU by nvidia-smi index or full UUID with --gpu. MIG is not supported.')
+ selected = matches[0]
+ ram = system_memory()
+ limit, available = cgroup_capacity()
+ hardware = Hardware(selected['name'], tuple(map(int, selected['compute_cap'].split('.'))),
+ int(float(selected['memory.total']) * 2**20), int(float(selected['memory.free']) * 2**20),
+ ram['total_bytes'], min(ram['available_bytes'], available) if available is not None else ram['available_bytes'],
+ platform.system(), cgroup_ram_limit=limit)
+ return {'hardware': hardware.to_dict(), 'selected_gpu': selected, 'gpus': rows,
+ 'kernel': platform.release(), 'platform': platform.platform(), 'machine': platform.machine(),
+ 'cpu_threads': os.cpu_count(), 'swap_total_bytes': ram.get('swap_total_bytes'),
+ 'swap_free_bytes': ram.get('swap_free_bytes'), 'system_memory': ram,
+ 'compiler': shutil.which('cl' if platform.system() == 'Windows' else 'g++'), 'git': shutil.which('git')}
+
+
+def existing_parent(path):
+ while not path.exists():
+ path = path.parent
+ return path
+
+
+def model_target(row, model_dir, encoder_dir, prepared_dir=None):
+ if row.get('role') == 'latent_upscaler':
+ return model_dir / 'latent_upscaler' / Path(row['file']).name
+ if row.get('prepared'):
+ if prepared_dir is None:
+ raise ValueError('Prepared model download has no destination directory')
+ from .adaln_assets import asset_path
+ return asset_path(prepared_dir, row['file'])
+ return (model_dir if row['repo'].startswith('OpenVDN/') else encoder_dir) / row['file']
+
+
+def plan(args, *, local_progress=None):
+ root = args.root.expanduser().resolve()
+ saved = json.loads((root / 'machine.json').read_text(encoding='utf-8')) if (root / 'machine.json').is_file() else {}
+ storage = getattr(args, 'storage', None) or saved.get('storage', 'compact')
+ layout = select_layout(getattr(args, 'environment', None), saved)
+ # v0.2.0 stored the encoder directory only in its retained setup plan.
+ prior_path = Path(saved['setup_run']) / 'plan.json' if saved.get('setup_run') else None
+ prior = json.loads(prior_path.read_text(encoding='utf-8')) if prior_path and prior_path.is_file() else {}
+ # Hardware detection is the default for fresh installs, updates and retries.
+ # Saved benchmark caps must never silently follow a user onto a larger GPU.
+ keep_limits = getattr(args, 'keep_resource_limits', False)
+ vram_gib = args.vram_gib if args.vram_gib is not None else saved.get('vram_gib') if keep_limits else None
+ ram_gib = args.ram_gib if args.ram_gib is not None else saved.get('ram_gib') if keep_limits else None
+ if not args.hardware_json and (platform.system() not in ('Linux', 'Windows') or platform.machine().lower() not in ('x86_64', 'amd64')):
+ raise RuntimeError('One-click setup supports Linux x86_64 and native Windows x64.')
+ snapshot = json.loads(args.hardware_json.read_text(encoding='utf-8')) if args.hardware_json else inventory(args.gpu)
+ hardware = Hardware.from_dict(snapshot['hardware'])
+ errors = []
+ if snapshot.get('machine', '').lower() not in ('x86_64', 'amd64') or hardware.system not in ('Linux', 'Windows'):
+ errors.append('This installer supports Linux x86_64 and native Windows x64.')
+ windows_target = hardware.system == 'Windows'
+ if windows_target and layout != 'unified':
+ errors.append('Native Windows uses the unified environment. Rerun with --environment unified.')
+ layout = 'unified'
+ if windows_target and getattr(args, 'rebuild_sage', False):
+ errors.append('Windows uses the pinned, verified Sage2 wheel; --rebuild-sage is a Linux source-build option.')
+ if int(snapshot['selected_gpu']['driver_version'].split('.')[0]) < 580:
+ errors.append('NVIDIA driver 580 or newer is required by the pinned CUDA 13 encoder; update the driver first.')
+ if hardware.capability not in ((8, 0), (8, 6), (8, 9), (9, 0), (12, 0)):
+ errors.append('The one-click profiles currently target SM80/86, SM89, SM90 and SM120.')
+ for name in () if windows_target else ('git', 'compiler'):
+ if not snapshot.get(name):
+ errors.append('Missing %s. Run ./setup.sh interactively to install basic tools, then review the engine plan.' % name)
+ if not args.hardware_json and not shutil.which('curl'):
+ errors.append('Missing curl for bounded downloads and HTTP/SOCKS proxy support. Run ./setup.sh to install it.')
+ policy = None
+ try:
+ policy = choose(hardware, vram_gib=vram_gib, ram_gib=ram_gib,
+ available_backends={'sage2', 'torch-flash', 'cudnn'},
+ allow_capacity_trial=True).to_dict()
+ except ValueError as error:
+ errors.append(str(error))
+ model_dir = Path(args.models or saved.get('model_root') or root / 'models' / 'vdn').expanduser().resolve()
+ encoder_dir = Path(args.encoder_models or saved.get('encoder_model_root') or prior.get('encoder_dir') or root / 'models' / 'encoder').expanduser().resolve()
+ reuse_cache = args.cache.expanduser().resolve() if args.cache else None
+ if reuse_cache is None and not getattr(args, 'rebuild_cache', False):
+ revision = json.loads((PACKAGE / 'dependencies.json').read_text(encoding='utf-8'))['models']['vdn_revision']
+ for name in ('machine.json', 'prepared-cache.json'):
+ cache_record = root / name
+ if cache_record.is_file():
+ record = json.loads(cache_record.read_text(encoding='utf-8'))
+ candidate = record.get('cache')
+ if record.get('model_revision') == revision and candidate and cache_compatible(candidate, hardware.capability):
+ reuse_cache = Path(candidate)
+ break
+ if reuse_cache is None and not getattr(args, 'rebuild_cache', False):
+ from .install_tuning import discover_prepared
+ reuse_cache = discover_prepared(getattr(args, 'reuse_models', None), hardware.capability)
+ from . import prepared_model
+ prepared = None
+ model_source = getattr(args, 'model_source', 'prepared')
+ if reuse_cache is None and not getattr(args, 'rebuild_cache', False) and model_source == 'prepared':
+ prepared = prepared_model.select(hardware.capability, root)
+ prepared_dir = prepared['directory'] if prepared else None
+ files = required_models(json.loads((PACKAGE / 'model_files.json').read_text(encoding='utf-8')), reuse_cache or prepared)
+ files += prepared_model.files(prepared)
+ local_reuse = None
+ local_folder = getattr(args, 'reuse_models', None)
+ local_manifest = getattr(args, 'reuse_models_manifest', None)
+ if local_folder or local_manifest:
+ from .local_models import scan, scan_many, can_link, key as local_key
+ previous_reuse = None
+ approved = getattr(args, 'approved_plan', None)
+ if approved:
+ previous_reuse = json.loads(Path(approved).read_text(encoding='utf-8')).get('local_models')
+ needed = [row for row in files if not model_target(row, model_dir, encoder_dir, prepared_dir).exists()]
+ if local_manifest:
+ if Path(local_manifest).stat().st_size > 512 * 1024:
+ raise ValueError('Model library manifest is too large')
+ libraries = json.loads(Path(local_manifest).read_text(encoding='utf-8'))
+ if not isinstance(libraries, dict) or libraries.get('version') != 1 or not isinstance(libraries.get('roots'), list):
+ raise ValueError('Invalid model library manifest')
+ directories = ([str(local_folder)] if local_folder else []) + libraries['roots']
+ local_reuse = scan_many(directories, needed, prior=previous_reuse, callback=local_progress)
+ else:
+ local_reuse = scan(local_folder, needed, prior=previous_reuse, callback=local_progress)
+ local_reuse['copy_mode'] = bool(getattr(args, 'copy_existing_models', False))
+ local_reuse['copy_bytes'] = local_reuse['linked_bytes'] = 0
+ for row in needed:
+ record = local_reuse['matches'].get(local_key(row))
+ if record:
+ parent = existing_parent(model_target(row, model_dir, encoder_dir, prepared_dir).parent)
+ record['method'] = 'hardlink' if not local_reuse['copy_mode'] and can_link(record['source'], parent) else 'copy'
+ local_reuse['linked_bytes' if record['method'] == 'hardlink' else 'copy_bytes'] += row['bytes']
+ groups = {}
+ present = 0
+ model_entries = []
+ for row in files:
+ path = model_target(row, model_dir, encoder_dir, prepared_dir)
+ size_matches = path.is_file() and path.stat().st_size == row['bytes']
+ present += row['bytes'] if size_matches else 0
+ device = existing_parent(path.parent)
+ entry = groups.setdefault(str(device), {'path': str(device), 'needed_bytes': 0,
+ 'free_bytes': shutil.disk_usage(device).free})
+ local = (local_reuse or {}).get('matches', {}).get(row['repo'] + '/' + row['file'])
+ model_entries.append((row, 'found' if size_matches else 'verified' if local else 'download'))
+ entry['needed_bytes'] += 0 if size_matches or local and local['method'] == 'hardlink' else row['bytes']
+ # Stream directly into final FP8 groups. The pinned model occupies ~45.3
+ # GiB; allow 52 GiB including the largest group in progress. Never credit
+ # future source deletion toward the space needed to complete conversion.
+ dependencies = dependency_status(root, layout, hardware.system)
+ environments_ready = all(r['exists'] and not r['missing'] and not r['mismatched'] for r in dependencies.values())
+ extra = (0 if reuse_cache or prepared else 52) + (5 if environments_ready else 30 if layout == 'unified' else 45) + 10
+ path = existing_parent(root)
+ entry = groups.setdefault(str(path), {'path': str(path), 'needed_bytes': 0,
+ 'free_bytes': shutil.disk_usage(path).free})
+ entry['needed_bytes'] += extra * GiB
+ # Aggregate paths sharing a filesystem; never count one disk's free space twice.
+ disks = {}
+ for entry in groups.values():
+ key = str(os.stat(entry['path']).st_dev)
+ merged = disks.setdefault(key, dict(paths=[], needed_bytes=0, free_bytes=entry['free_bytes']))
+ merged['paths'].append(entry['path'])
+ merged['needed_bytes'] += entry['needed_bytes']
+ for disk in disks.values():
+ if disk['needed_bytes'] > disk['free_bytes']:
+ errors.append('Insufficient disk space for retained models, environments and caches: ' + ', '.join(disk['paths']))
+ if reuse_cache and not cache_compatible(reuse_cache, hardware.capability):
+ errors.append('--cache must contain a prepared FP8 cache compatible with this GPU scale format.')
+ from .download_settings import read as download_preferences
+ networking = network.plan(json.loads((PACKAGE / 'dependencies.json').read_text(encoding='utf-8')),
+ bootstrap_versions(json.loads((PACKAGE / 'bootstrap_versions.json').read_text(encoding='utf-8')), hardware.system), layout,
+ mode=getattr(args, 'network', 'auto'), timeout=getattr(args, 'network_timeout', 5),
+ offline=bool(args.hardware_json or errors),
+ proxy_mode=download_preferences(root / 'download-settings.json')['proxy_mode'])
+ networking['download_settings_path'] = str(root / 'download-settings.json')
+ prepared_missing = prepared and any(
+ not model_target(row, model_dir, encoder_dir, prepared_dir).is_file()
+ for row in files if row.get('prepared') and not
+ (local_reuse or {}).get('matches', {}).get(row['repo'] + '/' + row['file']))
+ if prepared_missing and not args.hardware_json and not errors:
+ error = prepared_model.access_error(prepared, networking)
+ if error:
+ errors.append(error)
+ from .model_transfer import policy as transfer_policy
+ build = build_parallelism(policy['ram_budget_bytes'], snapshot.get('cpu_threads') or 1) if policy and not windows_target else None
+ transfers = transfer_policy(policy['ram_budget_bytes'], build['estimated_peak_bytes'] if build else 0) if policy else None
+ from .model_status import inventory as model_inventory
+ return {'schema_version': 1, 'engine_version': __version__, 'root': str(root), 'inventory': snapshot, 'policy_estimate': policy,
+ 'storage': storage,
+ 'storage_preparation': ('Download verified slim FP8 weights and fixed AdaLN tables; no original transformer or local conversion'
+ if prepared else 'Stream CPU merge directly to FP8 groups; no complete BF16 intermediate cache'),
+ 'storage_cleanup': ('After cache verification and GPU probes, remove only unchanged conversion-only weights downloaded/copied into this installation; borrowed originals and outputs are retained'
+ if storage == 'compact' else 'Retain original conversion weights for future re-quantization'),
+ 'network': networking,
+ 'model_transfer': transfers,
+ 'model_downloader': getattr(args, 'model_downloader', 'auto'),
+ 'allow_model_restart': getattr(args, 'allow_model_restart', False),
+ 'local_models': local_reuse,
+ 'model_groups': model_inventory(model_entries),
+ 'environment_layout': layout,
+ 'environment_count': len(environment_names(layout)),
+ 'vram_gib': vram_gib, 'ram_gib': ram_gib,
+ 'resource_mode': 'auto' if vram_gib is None and ram_gib is None else 'capacity-limits',
+ 'dependencies': dependencies,
+ 'model_dir': str(model_dir), 'encoder_dir': str(encoder_dir),
+ 'reuse_cache': str(reuse_cache) if reuse_cache else None,
+ 'prepared_model': prepared,
+ 'model_source': 'prepared' if prepared else 'existing' if reuse_cache else 'source',
+ 'model_source_reason': ('Pinned slim model, matching the existing GPU precision policy' if prepared else
+ 'Reuse existing compatible cache' if reuse_cache else
+ 'Original model explicitly selected' if model_source == 'source' or getattr(args, 'rebuild_cache', False) else
+ 'No verified prebuilt artifact for this GPU scale format; preserve its original precision policy'),
+ 'verification': getattr(args, 'verify', 'auto'),
+ 'build': build,
+ 'kernel_install': 'Pinned Windows Triton / Sage2 wheels; actual kernels checked before readiness' if windows_target else 'Local Sage2 source build / ABI-keyed wheel cache',
+ 'git_install': 'Reuse detected Git' if snapshot.get('git') else 'Install verified portable MinGit locally after confirmation' if windows_target else 'Git required',
+ 'wheel_cache': str(Path(getattr(args, 'wheel_cache', None) or saved.get('wheel_cache') or root / 'wheels').expanduser().resolve()),
+ 'rebuild_sage': getattr(args, 'rebuild_sage', False),
+ 'model_download_bytes': sum(r['bytes'] for r in files) - present - (local_reuse or {}).get('reused_bytes', 0),
+ 'existing_model_bytes_size_matched': present, 'disks': list(disks.values()),
+ 'additional_environment_cache_safety_gib': extra,
+ 'preparation_ram_estimate_gib': ('Bounded hash/header verification; no transformer loaded or converted' if prepared else
+ '4–8 GiB working set plus reclaimable source file cache; measured by setup'),
+ 'test_artifacts_estimate_gib': '5–8 GiB for the standard five-case suite; all outputs retained',
+ 'first_generation_note': 'Portable AdaLN tables follow model weights and the exact schedule, across GPUs and Torch/CUDA versions. New schedules or modulation-changing LoRAs require preparation. Existing environments stay on disk when switching.',
+ 'estimates_are_not_capacity_guarantees': True, 'errors': errors,
+ 'steps': ['Install isolated uv/Python and ' + ('one shared CUDA environment' if layout == 'unified' else 'two pinned CUDA environments'),
+ 'Clone pinned VDN, patched Diffusers and the native H3 text-encoder library',
+ 'Install verified Windows Triton / Sage2 wheels' if windows_target else 'Install a local CUDA compiler and build Sage2 for the detected GPU only',
+ 'Download and verify pinned base, eight-step checkpoint and encoder weights',
+ 'Prepare or verify the architecture-compatible FP8 cache',
+ 'Execute small attention/linear kernel probes and save machine configuration'],
+ 'licenses': ['https://huggingface.co/OpenVDN/vdn-minimax-h3/blob/751739ee5b9e3ac802dca5d5111075fdaeb47885/LICENSE',
+ 'https://huggingface.co/t8star/Vdn-Minimax-H3-Comfy',
+ 'https://docs.nvidia.com/cuda/eula/index.html']}
+
+
+def display(value, ui=None, *, verbose=False):
+ ui = ui or TerminalUI('Setup')
+ if verbose:
+ return display_details(value, ui)
+ h = value['inventory']['hardware']
+ usable_ram = min(h['ram_total'], h.get('cgroup_ram_limit') or h['ram_total'])
+ rows = [('GPU', '%s · %.1f GiB / %.1f GiB free' % (h['gpu_name'], h['vram_total']/GiB, h['vram_free']/GiB)),
+ ('RAM', '%.1f GiB / %.1f GiB available%s' % (usable_ram/GiB, min(usable_ram, h['ram_available'])/GiB,
+ ' · container limit' if usable_ram < h['ram_total'] else '')),
+ ('Install in', value['root']),
+ ('Environment', 'One shared Python environment · missing dependencies installed automatically'
+ if value['environment_layout'] == 'unified' else 'Two separate Python environments · keeping your selected layout')]
+ for key, default, label in (('model_dir', 'vdn', 'Model directory'), ('encoder_dir', 'encoder', 'Encoder directory')):
+ if Path(value[key]) != Path(value['root']) / 'models' / default:
+ rows.append((label, value[key]))
+ rows.append(('Model download', '~%.1f GiB; Python / GPU packages are additional' % (value['model_download_bytes']/GiB)
+ if value['model_download_bytes'] else 'No new model files expected · verify existing files'))
+ if value.get('local_models'):
+ local = value['local_models']
+ rows.append(('Reuse models', '%.1f GiB verified · %.1f GiB copied locally · source files kept' %
+ (local['reused_bytes']/GiB, local['copy_bytes']/GiB)))
+ rows.append(('Download recovery', 'May restart incomplete files if resume fails; earlier data retained' if value.get('allow_model_restart')
+ else 'Retry the same source; preserve progress; pause if a restart would be required'))
+ for disk in value['disks']:
+ label = 'Peak disk space' if len(value['disks']) == 1 else 'Disk ' + disk['paths'][0]
+ rows.append((label, '~%.1f GiB additional needed · %.1f GiB free' % (disk['needed_bytes']/GiB, disk['free_bytes']/GiB)))
+ prepared = value.get('prepared_model')
+ rows.append(('Storage', 'Download slim FP8 model · no local conversion' if prepared else
+ 'Reuse prepared FP8 cache' if value['reuse_cache'] else 'Prepare compact FP8 model'))
+ if prepared:
+ rows.append(('Prepared model', prepared['repo'] + ' · ' + prepared['scale_granularity'] +
+ (' · private, authorized HF token required' if prepared.get('private') else '')))
+ elif value.get('model_source') == 'source':
+ rows.append(('Model source', value['model_source_reason']))
+ rows.append(('Original weights', 'Not downloaded; fixed AdaLN tables included' if prepared else
+ 'Remove verified conversion inputs downloaded/copied here; keep borrowed originals'
+ if value.get('storage', 'compact') == 'compact' else 'Keep original weights for future conversion'))
+ setup_ram = 'Bounded model verification; no FP8 conversion' if prepared else 'About 4–8 GiB for model preparation'
+ if value.get('build'):
+ setup_ram += ' · up to ~%.1f GiB for compilation' % (value['build']['estimated_peak_bytes']/GiB)
+ rows.append(('Setup RAM', setup_ram))
+ if value.get('model_transfer'):
+ rows.append(('Model downloads', 'Native parallel · %.1f GiB process budget + disk cache managed separately' % (value['model_transfer']['ram_guard_bytes']/GiB)))
+ if value.get('model_downloader') == 'xet':
+ rows.append(('Download mode', 'Require HF Xet for large weights · stop on failure · curl partials retained separately'))
+ limits = [value.get(key) for key in ('vram_gib', 'ram_gib')]
+ rows.append(('Optimization', 'Automatic · reserves GPU and system memory' if all(v is None for v in limits)
+ else 'Capacity limits · GPU %s / RAM %s' % tuple('auto' if v is None else '%g GiB' % v for v in limits)))
+ networking = value.get('network', {})
+ route = ('Models via HF Xet only · package mirrors remain automatic' if value.get('model_downloader') == 'xet' and networking.get('mode') != 'official' else
+ 'Official sources only' if networking.get('mode') == 'official' else
+ 'VDN via ModelScope · retry alternatives' if network.ordered(networking, 'vdn-models')[0] == 'modelscope' else
+ 'Fastest available mirrors · retry alternatives')
+ if prepared and prepared.get('private'):
+ route = 'Prepared model via authenticated Hugging Face · other models use ranked sources'
+ elif prepared and value.get('model_downloader') != 'xet' and networking.get('mode') != 'official':
+ family = network.model_family(dict(repo=prepared['repo'], revision=prepared['revision']))
+ source = network.ordered(networking, family)[0]
+ label = {'modelscope': 'ModelScope', 'official': 'Hugging Face', 'hf-mirror': 'HF Mirror',
+ 'user': 'your Hub endpoint'}.get(source, source)
+ route = 'Public Edge via ' + label + ' · verified mirrors · resume retained files'
+ rows.append(('Network', route
+ + (' · compare proxy / direct' if networking.get('proxy_configured') or networking.get('git_proxy_configured') else '')))
+ ui.panel('FreeVideo / ' + h['system'] + ' setup', rows)
+ ui.write('Space and memory are estimates. Setup checks your GPU before marking it ready.\n')
+ ui.write('Model / toolkit licenses: MiniMax H3, H3 text encoder, NVIDIA CUDA.\n')
+ ui.write('Use --verbose for full details and license links, or enter d at confirmation.\n')
+ for error in value['errors']:
+ ui.write('BLOCKED: ' + str(error) + '\n')
+
+
+def display_details(value, ui=None):
+ ui = ui or TerminalUI('Setup')
+ h = value['inventory']['hardware']
+ rows = [('GPU', '%s · SM %s' % (h['gpu_name'], '.'.join(map(str, h['capability'])))),
+ ('VRAM', '%.2f GiB total / %.2f GiB free' % (h['vram_total']/GiB, h['vram_free']/GiB)),
+ ('RAM', '%.2f GiB total / %.2f GiB available' % (h['ram_total']/GiB, h['ram_available']/GiB))]
+ limits = [value.get(key) for key in ('vram_gib', 'ram_gib')]
+ rows.append(('Resources', 'Automatic · current available VRAM/RAM' if all(v is None for v in limits)
+ else 'Capacity limits · VRAM %s / RAM %s' % tuple('auto' if v is None else '%g GiB' % v for v in limits)))
+ p = value['policy_estimate']
+ if p:
+ rows += [('Budgets', 'GPU %.2f GiB / RAM %.2f GiB · system reserves %.2f / %.2f GiB' %
+ tuple(p[k]/GiB for k in ('gpu_budget_bytes', 'ram_budget_bytes', 'gpu_system_reserve_bytes', 'ram_system_reserve_bytes'))),
+ ('Strategy', '%s · %s · %d resident blocks · %.2f GB pinned weights' %
+ (p['engine']['linear_compute'], p['engine']['attention'], p['engine']['resident_blocks'], p['engine']['pin_host_gb']))]
+ rows.append(('Install root', value['root']))
+ rows.append(('Environment', value['environment_layout'] + ' · text encoder exits before video models load'))
+ rows.append(('Kernel installation', value.get('kernel_install', 'Local Sage2 build')))
+ rows.append(('Git', value.get('git_install', 'Use detected Git')))
+ networking = value.get('network', {})
+ rows.append(('Connection', {'auto': 'Auto', 'proxy': 'Proxy only', 'direct': 'Direct only'}[
+ networking.get('proxy_mode', 'auto')]))
+ for name, entries in networking.get('sources', {}).items():
+ selected = entries[0]
+ detail = (' · %.2fs Git availability' % selected['seconds'] if name == 'git' else
+ ' · %.2f MiB/s sample' % (selected['bytes_per_second']/2**20) if selected.get('bytes_per_second')
+ else ' · connected') if selected.get('ok') else ' · unverified, downloads will retry alternatives'
+ rows.append((name, selected['id'] + detail))
+ for name, row in value['dependencies'].items():
+ if not row['exists']:
+ rows.append((name.capitalize(), 'Create isolated environment and install dependencies'))
+ else:
+ rows.append((name.capitalize(), 'Missing: %s · version mismatches: %s' %
+ (', '.join(row['missing']) or 'none', ', '.join(row['mismatched']) or 'none')))
+ rows.append(('Model download', '%.2f GiB · existing files verified after confirmation' % (value['model_download_bytes']/GiB)))
+ rows.append(('Storage', value.get('storage', 'compact') + ' · ' + value.get('storage_preparation', 'streamed FP8 preparation without a BF16 disk copy')))
+ if value.get('prepared_model'):
+ prepared = value['prepared_model']
+ rows.append(('Prepared model', prepared['repo'] + '@' + prepared['revision'] + ' · ' + prepared['scale_granularity']))
+ rows.append(('Access', 'Private repository; HF_TOKEN or hf auth login with an authorized account' if prepared.get('private') else 'Public repository'))
+ rows.append(('Source weights', value.get('storage_cleanup', 'Retain source weights')))
+ if value['reuse_cache']:
+ rows.append(('Prepared cache', 'Reuse FP8 · skip original BF16/LoRA downloads and scans'))
+ rows.append(('Verification', value['verification'] + ' · auto reuses unchanged pinned receipts; full rereads tensors'))
+ if value['build']:
+ rows.append(('Sage2 build', 'Up to %d compiler jobs · ~%.1f GiB RAM estimate · reusable ABI/GPU wheel cache' %
+ (value['build']['jobs'], value['build']['estimated_peak_bytes']/GiB)))
+ for disk in value['disks']:
+ rows.append(('Disk', '~%.1f GiB additional / %.1f GiB free · %s' %
+ (disk['needed_bytes']/GiB, disk['free_bytes']/GiB, disk['paths'][0])))
+ rows += [('Preparation RAM', value['preparation_ram_estimate_gib']), ('Test outputs', value['test_artifacts_estimate_gib'])]
+ rows.append(('First generation', value['first_generation_note']))
+ ui.panel('FreeVideo / ' + h['system'] + ' setup plan', rows)
+ print('Resource numbers are conservative estimates. The test suite measures actual usage.')
+ for i, step in enumerate(value['steps'], 1):
+ print('%d. %s' % (i, step))
+ print('Licenses:\n' + '\n'.join(value['licenses']))
+ for error in value['errors']:
+ print('BLOCKED: ' + error)
+
+
+def confirmed(args, value, ask=input, ui=None):
+ reviewed_path = getattr(args, 'approved_plan', None)
+ if reviewed_path:
+ if not args.yes or not args.accept_model_license:
+ raise ValueError('--approved-plan requires explicit plan/license acceptance.')
+ reviewed = json.loads(Path(reviewed_path).read_text(encoding='utf-8'))
+ keys = ('root', 'engine_version', 'environment_layout', 'model_dir', 'encoder_dir', 'vram_gib', 'ram_gib', 'reuse_cache', 'storage', 'storage_cleanup', 'model_downloader', 'prepared_model', 'model_source')
+ changed = any(reviewed.get(key) != value.get(key) for key in keys)
+ changed |= reviewed.get('inventory', {}).get('selected_gpu', {}).get('uuid') != value['inventory']['selected_gpu']['uuid']
+ changed |= value['model_download_bytes'] > reviewed.get('model_download_bytes', -1)
+ changed |= bool(reviewed.get('allow_model_restart')) != bool(value.get('allow_model_restart'))
+ for key in ('root', 'roots', 'copy_mode', 'copy_bytes'):
+ changed |= (reviewed.get('local_models') or {}).get(key) != (value.get('local_models') or {}).get(key)
+ changed |= sum(disk['needed_bytes'] for disk in value['disks']) > sum(disk['needed_bytes'] for disk in reviewed.get('disks', []))
+ if changed or reviewed.get('errors'):
+ raise ValueError('The installation plan changed. Detect configuration again and review the new plan before installing.')
+ if args.yes:
+ if not args.accept_model_license:
+ raise ValueError('Unattended installation requires --yes --accept-model-license.')
+ return True
+ if not sys.stdin.isatty():
+ raise ValueError('No interactive input. Review --plan, then use --yes --accept-model-license.')
+ while True:
+ try:
+ reply = ask('Install and accept the model/toolkit licenses? [Y/n/d] (Enter = yes, d = details): ').strip().lower()
+ except EOFError:
+ return False
+ if reply in ('', 'y', 'yes'):
+ return True
+ if reply in ('n', 'no'):
+ return False
+ if reply in ('d', 'details'):
+ display(value, ui, verbose=True)
+ else:
+ print('Press Enter to install, n to cancel, or d to review details.')
+
+
+def download(url, path, sha, progress=None, *, networking=None, env=None):
+ networking = networking or {}
+ family = 'github' if url.startswith(network.SOURCES['github']['official'] + '/') else 'cuda'
+ return network.download(network.urls(networking, family, url, env), path, sha, progress, network=networking, env=env, category=family)
+
+
+def unpack(archive, destination):
+ destination.mkdir(parents=True, exist_ok=True)
+ if zipfile.is_zipfile(archive):
+ with zipfile.ZipFile(archive) as zipped:
+ for member in zipped.infolist():
+ name = member.filename.replace('\\', '/')
+ path = (destination / name).resolve()
+ if (not path.is_relative_to(destination.resolve()) or ':' in name or
+ (member.external_attr >> 16) & 0o170000 == 0o120000):
+ raise ValueError('Unsafe ZIP member: ' + member.filename)
+ zipped.extractall(destination)
+ return
+ with tarfile.open(archive) as tar:
+ # Python 3.9-compatible containment checks, including link targets.
+ for member in tar.getmembers():
+ path = (destination / member.name).resolve()
+ if not path.is_relative_to(destination.resolve()) or member.isdev() or member.isfifo():
+ raise ValueError('Unsafe archive member: ' + member.name)
+ if member.issym() or member.islnk():
+ target = (path.parent if member.issym() else destination) / member.linkname
+ if not target.resolve().is_relative_to(destination.resolve()):
+ raise ValueError('Unsafe archive link: ' + member.name)
+ tar.extractall(destination)
+
+
+def setup_task(label):
+ """Describe the work independently of internal command IDs and log paths."""
+ routes = {'official': 'official source', 'nju': 'Nanjing University mirror',
+ 'tuna': 'Tsinghua mirror', 'ghfast': 'GitHub mirror',
+ 'user-python': 'your Python mirror', 'user': 'your configured source'}
+ source = next((name for name in routes if label.endswith('-' + name)), None)
+ key = label[:-(len(source) + 1)] if source else label
+ for prefix in ('unified-', 'engine-', 'encoder-'):
+ if key.startswith(prefix):
+ key = key[len(prefix):]
+ break
+ tasks = {
+ 'python': ('Install Python', 'Download and prepare the private Python runtime'),
+ 'python-bootstrap-reuse': ('Use existing Python', 'Check the Python already prepared by the launcher'),
+ 'venv': ('Create Python environment', 'Prepare an isolated environment for FreeVideo'),
+ 'torch': ('Install PyTorch + CUDA', 'Download and install the GPU runtime packages'),
+ 'packages': ('Install engine dependencies', 'Download and install video and text encoding libraries'),
+ 'engine': ('Install encoder support', 'Connect the encoder to FreeVideo'),
+ 'vdn': ('Install model support', 'Install the verified model code'),
+ 'vdn-source': ('Download model code', 'Prepare model source files while runtime packages download'),
+ 'library-check': ('Check text encoder', 'Verify that the text encoding library loads'),
+ 'models': ('Download model weights', 'Download and verify the video model and text encoder'),
+ 'prepare': ('Optimize model storage', 'Prepare compact FP8 weights for your GPU'),
+ 'kernels': ('Test GPU acceleration', 'Run small checks on your GPU'),
+ 'storage': ('Finish model storage', 'Verify prepared weights and apply your storage choice'),
+ 'dependency-check': ('Check installed packages', 'Verify dependency compatibility'),
+ 'freeze': ('Save installed versions', 'Record versions for future diagnostics'),
+ 'sage2-toolchain-check': ('Check build tools', 'Verify the compiler before building GPU acceleration'),
+ 'sage2-build': ('Build SageAttention 2', 'Compile acceleration for your GPU · the first build takes longer'),
+ 'sage2-wheel-install': ('Install SageAttention 2', 'Install the prepared GPU acceleration package'),
+ 'sage2-windows-wheel-install': ('Install SageAttention 2', 'Install the verified Windows acceleration package'),
+ 'windows-runtime-check': ('Check GPU runtime', 'Verify CUDA and the Windows runtime libraries'),
+ }
+ title, detail = tasks.get(key, ('Prepare dependencies', 'Prepare verified source files'))
+ for prefix, name in (('h3-text-encoder-', 'text encoder'), ('SageAttention-', 'SageAttention 2')):
+ if key.startswith(prefix):
+ title = 'Download ' + name if '-fetch' in key else 'Prepare ' + name
+ detail = 'Download verified source code' if '-fetch' in key else 'Prepare source files for installation'
+ if source:
+ detail += ' · ' + routes[source]
+ return title, detail
+
+
+def reusable_kernel_receipt(path, hardware_data):
+ """Return a matching successful GPU probe receipt, or ``None``.
+
+ The receipt is deliberately tied to the same identity used by runtime
+ backend selection. A changed driver, Torch/CUDA build, installed backend,
+ or engine source invalidates it and forces a fresh isolated probe.
+ """
+ from .hardware import Hardware, installed_backends
+ from .kernel_capabilities import identity, readiness
+ try:
+ receipt = json.loads(Path(path).read_text(encoding='utf-8'))
+ expected = identity(Hardware.from_dict(hardware_data))
+ probes = receipt.get('kernel_probes')
+ if (receipt.get('identity') != expected
+ or sorted(receipt.get('installed_attention_packages', ())) != sorted(installed_backends())
+ or not isinstance(probes, list) or not readiness(probes).get('ready')):
+ return None
+ return receipt
+ except (OSError, ValueError, KeyError, TypeError, ImportError):
+ return None
+
+
+class Installer:
+ def __init__(self, value, ui=None):
+ self.locks = ExitStack()
+ try:
+ self.initialize(value, ui)
+ except BaseException:
+ self.locks.close()
+ raise
+
+ def initialize(self, value, ui):
+ self.plan = value
+ self.root = Path(value['root'])
+ self.layout = value.get('environment_layout', 'unified')
+ self.system = value['inventory'].get('hardware', {}).get('system', platform.system())
+ self.pythons = role_pythons(self.root, self.layout, self.system)
+ self.root.mkdir(parents=True, exist_ok=True)
+ self.lock = self.locks.enter_context(runtime_lock(self.root / 'setup.lock', inherit=False))
+ lock_path = os.environ.get('FREEVIDEO_LOCK_PATH', str(self.root / 'engine.lock'))
+ self.runtime_fd = self.locks.enter_context(runtime_lock(lock_path))
+ self.run_dir = self.root / 'setup-runs' / (time.strftime('%Y%m%dT%H%M%SZ', time.gmtime()) + '-' + str(os.getpid()))
+ self.run_dir.mkdir(parents=True, exist_ok=False)
+ save(self.run_dir / 'plan.json', value)
+ previous = self.root / 'machine.json'
+ saved = {}
+ if previous.is_file():
+ shutil.copyfile(previous, self.run_dir / 'machine.before.json')
+ saved = json.loads(previous.read_text(encoding='utf-8'))
+ # Persist a confirmed choice for interrupted first installs and upgrades.
+ # The former complete configuration is retained in machine.before.json.
+ save(previous, dict(saved, root=str(self.root), ready=False, setup_run=str(self.run_dir),
+ storage=value.get('storage', 'compact'),
+ pending_environment_layout=self.layout, model_root=value['model_dir'],
+ encoder_model_root=value.get('encoder_dir', saved.get('encoder_model_root')),
+ wheel_cache=value.get('wheel_cache', saved.get('wheel_cache')),
+ vram_gib=value.get('vram_gib', saved.get('vram_gib')),
+ ram_gib=value.get('ram_gib', saved.get('ram_gib'))))
+ self.env = dict(os.environ, FREEVIDEO_HOME=str(self.root),
+ FREEVIDEO_VDN_ROOT=str(self.root / 'vendor' / 'vdn'),
+ FREEVIDEO_MODEL_ROOT=value['model_dir'],
+ FREEVIDEO_COMFY_ROOT=str(self.root / 'vendor' / 'h3-text-encoder'),
+ FREEVIDEO_COMFY_PYTHON=str(self.pythons['encoder']),
+ FREEVIDEO_LOCK_PATH=lock_path,
+ PATH=str(self.pythons['engine'].parent) + os.pathsep + os.environ.get('PATH', ''),
+ CUDA_VISIBLE_DEVICES=value['inventory']['selected_gpu']['uuid'],
+ UV_CACHE_DIR=str(self.root / 'downloads' / 'uv-cache'),
+ UV_PYTHON_INSTALL_DIR=str(self.root / 'python'), UV_LINK_MODE='hardlink',
+ HF_HOME=str(self.root / 'downloads' / 'huggingface'), HF_HUB_DISABLE_TELEMETRY='1',
+ TRITON_CACHE_DIR=str(self.root / 'kernel-cache' / 'triton'),
+ CUDA_CACHE_PATH=str(self.root / 'kernel-cache' / 'cuda'),
+ TORCHINDUCTOR_CACHE_DIR=str(self.root / 'kernel-cache' / 'inductor'),
+ HF_HUB_OFFLINE='0', TRANSFORMERS_OFFLINE='0',
+ PIP_DISABLE_PIP_VERSION_CHECK='1',
+ PYTHONUNBUFFERED='1', PYTHONUTF8='1', PYTHONIOENCODING='utf-8', PYTHONPATH=str(SOURCE), OMP_NUM_THREADS='4', MKL_NUM_THREADS='4')
+ self.env[LOCK_ENV] = str(self.runtime_fd)
+ self.env.update(FREEVIDEO_NETWORK_PLAN=str(self.run_dir / 'plan.json'),
+ FREEVIDEO_NETWORK_EVENTS=str(self.run_dir / 'network.jsonl'))
+ self.env = network.proxy_environment(self.env)
+ self.network = dict(value.get('network', {}), events_path=str(self.run_dir / 'network.jsonl'), quiet=True)
+ self.state = {'status': 'running', 'steps': [], 'plan': value}
+ self.state_lock = threading.Lock()
+ self.cancel = threading.Event()
+ self.versions = bootstrap_versions(json.loads((PACKAGE / 'bootstrap_versions.json').read_text(encoding='utf-8')), self.system)
+ self.spec = json.loads((PACKAGE / 'dependencies.json').read_text(encoding='utf-8'))
+ self.ui = ui or TerminalUI('Setup', plain=True)
+ self.monitor_stop = threading.Event()
+ self.monitor_thread = None
+ self.started = time.monotonic()
+
+ def start_monitor(self):
+ from .ram import ProcessMemory
+ memory = ProcessMemory()
+ def sample():
+ unreadable = failures = recoveries = 0
+ try:
+ with (self.run_dir / 'ram.jsonl').open('w', buffering=1, encoding='utf-8') as stream:
+ while not self.monitor_stop.is_set():
+ try:
+ row = memory.sample(os.getpid())
+ except OSError as error:
+ if self.system != 'Windows':
+ raise
+ # The process snapshot itself can fail, too. Still
+ # obtain fresh physical/commit headroom; never reuse
+ # the previous reading or invent process usage.
+ system = system_memory()
+ row = dict(guard_bytes=None,
+ system_available_bytes=system['available_bytes'],
+ system_physical_available_bytes=system['physical_available_bytes'],
+ system_commit_available_bytes=system['commit_available_bytes'],
+ memory_read_errors=[dict(error=str(error), winerror=getattr(error, 'winerror', None))])
+ used = memory_sample(row)
+ if used is None:
+ unreadable += 1
+ failures += 1
+ # A live worker can have descendants for which
+ # Windows returns ERROR_ACCESS_DENIED (5). Keep
+ # the sample and continue under the global RAM/
+ # commit floor; abort only when the whole process
+ # snapshot is unavailable or the root cannot be
+ # sampled. This avoids killing installs merely
+ # because a helper has a stricter token.
+ degraded = (self.system == 'Windows' and
+ _permission_limited_memory_sample(row))
+ if degraded:
+ self.state['resource_warning'] = (
+ 'Some Windows child processes denied memory queries (WinError 5); '
+ 'continuing with the global RAM/commit floor.')
+ row = dict(row, monitor_status='degraded' if degraded else 'retrying',
+ consecutive_unreadable=unreadable)
+ else:
+ if unreadable:
+ recoveries += 1
+ row = dict(row, monitor_status='recovered' if unreadable else 'complete',
+ consecutive_unreadable=0)
+ unreadable = 0
+ stream.write(json.dumps(dict(row, elapsed_seconds=time.monotonic()-self.started, epoch_seconds=time.time())) + '\n')
+ available = row.get('effective_available_bytes', row['system_available_bytes'])
+ self.ui.resource = setup_memory_status(row, self.ui.verbose)
+ floor = (2 if self.system == 'Windows' else 1) * GiB
+ # Windows helpers can exit/change while their tree is
+ # sampled. Allow two resamples, while checking the live
+ # system floor on every attempt. A complete over-budget
+ # reading and persistent failures still stop the work.
+ if available < floor or (used is not None and used > self.plan['policy_estimate']['ram_budget_bytes']):
+ self.state['resource_guard'] = 'Installation crossed its RAM budget or the %d GiB emergency system floor.' % (floor / GiB)
+ if row.get('private_commit_bytes') is not None:
+ self.state['resource_guard'] += (' ' + setup_memory_status(row) +
+ ' · Process budget %.1f GiB (working set; commit is diagnostic).' %
+ (self.plan['policy_estimate']['ram_budget_bytes']/GiB))
+ self.cancel.set()
+ break
+ if (used is None and (self.system != 'Windows' or unreadable >= 3)
+ and row.get('monitor_status') != 'degraded'):
+ details = json.dumps(dict(
+ unreadable_memory_pids=row.get('unreadable_memory_pids', row.get('unreadable_pss_pids', [])),
+ memory_read_errors=row.get('memory_read_errors', []),
+ available_bytes=available), ensure_ascii=False)
+ raise RuntimeError('Cannot read process-tree memory after %d consecutive samples. '
+ 'Download progress is retained; retry installation to resume. '
+ 'RAM sample: %s; full samples: %s' % (unreadable, details, self.run_dir / 'ram.jsonl'))
+ self.monitor_stop.wait(1)
+ except BaseException as error:
+ self.state['resource_guard'] = 'Installation RAM monitoring failed: ' + repr(error)
+ self.cancel.set()
+ finally:
+ self.state['memory'] = memory.result()
+ self.state['memory'].update(monitor_unreadable_samples=failures,
+ monitor_recoveries=recoveries, monitor_consecutive_unreadable=unreadable)
+ if failures:
+ self.state['memory']['process_tree_guard_complete'] = False
+ self.monitor_thread = threading.Thread(target=sample, name='setup-memory', daemon=True)
+ self.monitor_thread.start()
+
+ def fetch(self, url, path, sha):
+ key = 'download-' + path.name
+ title = ('Prepare download tools' if path.name.startswith('uv') else
+ 'Download GPU acceleration' if path.suffix == '.whl' else
+ 'Prepare Git' if 'git' in path.name.lower() else 'Download CUDA build tools')
+ self.ui.begin(key, title, detail=path.name if self.ui.verbose else 'Download and verify required files')
+ def progress(done, total, speed):
+ if self.cancel.is_set():
+ raise RuntimeError('Installation cancelled; partial download retained.')
+ self.ui.update(key, done=done, total=total, rate=speed, unit='bytes', scope=path.name,
+ detail=('%.1f MiB / %.1f MiB' % (done/2**20, total/2**20) if total else '%.1f MiB downloaded' % (done/2**20)) + ' · %.1f MiB/s' % (speed/2**20))
+ try:
+ feedback = lambda row: self.ui.update(key, detail='Source: %s · %s%s' %
+ (row['source'], row['action'], ' · ' + row['reason'] if row.get('reason') else ''))
+ download(url, path, sha, progress, networking=dict(self.network, event_callback=feedback), env=self.env)
+ except BaseException:
+ self.ui.end(key, success=False)
+ raise
+ self.ui.end(key, detail='Verified and ready')
+
+ def command(self, label, args, env=None, cwd=None):
+ from .ram import ProcessMemory
+ from .package_progress import PackageOutput
+ if self.cancel.is_set():
+ raise RuntimeError(self.state.get('resource_guard', 'Installation cancelled.'))
+ with self.state_lock:
+ index = len(self.state['steps'])
+ log = self.run_dir / ('%02d-%s.log' % (index, label))
+ row = {'label': label, 'command': list(map(str, args)), 'log': str(log), 'status': 'running'}
+ self.state['steps'].append(row)
+ save(self.run_dir / 'status.json', self.state)
+ key = str(index) + '-' + label
+ title, detail = setup_task(label)
+ if label == 'models' and self.plan.get('local_models'):
+ title, detail = 'Reuse models / download missing files', 'Import verified local models; source files stay in place'
+ self.ui.begin(key, title, detail=str(log) if self.ui.verbose else detail)
+ progress = LogProgress(log)
+ parallel_tasks = set()
+ def update_progress(*, final=False):
+ self.ui.update(key, **progress.read(final=final))
+ if progress.model_groups is not None:
+ self.ui.event('models', groups=progress.model_groups)
+ progress.model_groups = None
+ for name, details in progress.take_tasks().items():
+ subkey = key + '-' + name
+ label, state = details.pop('label'), details.pop('state')
+ if subkey not in parallel_tasks:
+ self.ui.begin(subkey, label)
+ parallel_tasks.add(subkey)
+ self.ui.update(subkey, **details)
+ if state != 'running':
+ self.ui.end(subkey, success=state == 'complete', detail=details['detail'])
+ tick = time.monotonic()
+ memory = ProcessMemory()
+ child = None
+ try:
+ with log.open('w', encoding='utf-8') as stream:
+ with PackageOutput(row['command'], stream, env or self.env) as output:
+ child = processes.popen(row['command'], env=output.env, cwd=cwd, stdout=output.stdout, stderr=subprocess.STDOUT,
+ start_new_session=True, pass_fds=(self.runtime_fd,), supervise=True)
+ output.spawned()
+ try:
+ while child.poll() is None:
+ if output.error is not None:
+ raise RuntimeError('Could not retain package installation output') from output.error
+ if self.cancel.is_set():
+ raise RuntimeError('Another parallel installation step failed; cancelling this step.')
+ memory.sample(child.pid)
+ try:
+ child.wait(timeout=.5)
+ except subprocess.TimeoutExpired:
+ pass
+ update_progress()
+ except BaseException:
+ if child.poll() is None:
+ processes.stop(child)
+ raise
+ except BaseException as error:
+ row['error'] = repr(error)
+ if child is not None and child.poll() is None:
+ processes.stop(child)
+ raise
+ finally:
+ update_progress(final=True)
+ success = child is not None and child.poll() == 0 and 'error' not in row
+ for subkey in parallel_tasks:
+ if self.ui.tasks[subkey]['state'] == 'running':
+ self.ui.end(subkey, success=success, detail='Ready' if success else 'Stopped; files retained')
+ try:
+ with self.state_lock:
+ row.update(seconds=time.monotonic()-tick, returncode=child.poll() if child else None, memory=memory.result(),
+ status='complete' if success else 'failed')
+ save(self.run_dir / 'status.json', self.state)
+ finally:
+ self.ui.end(key, success=success, detail=(getattr(progress, 'last_notice', None) or 'Log: ' + str(log))
+ if self.ui.verbose or not success else 'Ready')
+ if child.returncode:
+ raise RuntimeError('%s failed; all files retained. See %s\n%s' % (label, log, log.read_text(errors='replace', encoding='utf-8')[-2500:]))
+ return log
+
+ def component_tasks(self, uv):
+ """Dependencies, not serial UI stages, determine when work can start."""
+ tasks = {}
+ def task(name, run, after=(), writes=()):
+ tasks[name] = dict(run=run, after=tuple(after), writes=tuple(writes))
+ task('python', lambda: self.python_install(uv))
+ task('git', self.install_git_windows if self.system == 'Windows' else lambda: None)
+ # This entry imports only stdlib engine code from the bootstrap Python.
+ # It can fetch the exact source trees while large Torch wheels download.
+ task('vdn-source', lambda: self.command('vdn-source', [sys.executable, '-c',
+ 'from freevideo_engine.install import install; install()']), after=('git',))
+ task('encoder-source', lambda: self.clone('h3-text-encoder',
+ self.spec['encoder']['comfy_url'], self.spec['encoder']['comfy_commit'],
+ sparse=['/comfy/', '/utils/', '/folder_paths.py', '/node_helpers.py', '/LICENSE']), after=('git',))
+ pythons = {}
+ for name in environment_names(self.layout):
+ spec = ENVIRONMENTS[name]
+ venv = self.root / 'envs' / name
+ python = venv_python(venv, self.system)
+ pythons[name] = python
+ writer = ('environment:' + name,)
+ def environment(folder=venv, executable=python, label=name):
+ if not executable.exists():
+ return self.packages(label + '-venv', [uv, 'venv', '--seed', '--python', self.versions['python'], folder])
+ task(name + '-venv', environment, ('python',), writer)
+ command = [uv, 'pip', 'install', '--python', python, *spec['torch'], '-c', constraints_file(name, self.system)]
+ family = 'torch-' + spec['cuda'] + '-' + spec['torch'][0].split('==')[1]
+ task(name + '-torch', lambda label=name, argv=command, group=family: self.packages(label + '-torch', argv, group),
+ (name + '-venv',), writer)
+ name = 'unified' if self.layout == 'unified' else 'engine'
+ python, encoder_python = self.pythons['engine'], self.pythons['encoder']
+ command = [uv, 'pip', 'install', '--python', python, '-e', str(SOURCE) + '[runtime]',
+ '-c', constraints_file(name, self.system), 'pip', 'ninja', 'packaging', 'wheel', 'setuptools']
+ if self.layout == 'unified':
+ command += ['-r', SOURCE / 'constraints/encoder-runtime.txt']
+ task('runtime', lambda: self.packages(name + '-packages', command), (name + '-torch',), ('environment:' + name,))
+ # Enqueue downloads as soon as their SDKs exist, before waiting for
+ # source setup, encoder checks or acceleration compilation.
+ task('models', lambda: self.command('models', [python, '-m', 'freevideo_engine.provision',
+ '--plan', self.run_dir / 'plan.json']), ('runtime',))
+ task('vdn-install', lambda: self.command('vdn', [python, '-m', 'freevideo_engine', 'setup', '--install-packages']),
+ ('runtime', 'vdn-source'), ('environment:' + name,))
+ encoder_after = 'runtime'
+ if self.layout == 'dual':
+ def encoder_packages():
+ self.packages('encoder-packages', [uv, 'pip', 'install', '--python', encoder_python,
+ '-r', SOURCE / 'constraints/encoder-runtime.txt', '-c', constraints_file('encoder', self.system)])
+ self.packages('encoder-engine', [uv, 'pip', 'install', '--python', encoder_python, '--no-deps', '-e', SOURCE])
+ task('encoder-runtime', encoder_packages, ('encoder-torch',), ('environment:encoder',))
+ encoder_after = 'encoder-runtime'
+ comfy = self.root / 'vendor' / 'h3-text-encoder'
+ task('encoder-check', lambda: self.command('encoder-library-check', [encoder_python, '-m',
+ 'freevideo_engine.encode_worker', '--comfy-root', comfy, '--check-library']),
+ (encoder_after, 'encoder-source'), ('environment:unified' if self.layout == 'unified' else 'environment:encoder',))
+ if self.system == 'Windows':
+ task('windows-check', lambda: self.command('windows-runtime-check', [python, '-c',
+ 'import torch,triton,safetensors,comfy_kitchen,comfy_aimdo; '
+ 'assert torch.cuda.is_available(), "CUDA unavailable; check NVIDIA driver and Windows DLL errors above"; '
+ 'print("Windows runtime imports passed; GPU kernel probes follow")']), ('runtime',), ('environment:' + name,))
+ overlap = (self.plan.get('model_transfer') or {}).get('overlap_build', True)
+ task('sage', lambda: self.install_sage(uv, python), ('runtime',) if overlap else ('runtime', 'models'),
+ ('environment:' + name,))
+ return tasks, pythons, comfy
+
+ def clone(self, name, url, commit, sparse=None):
+ return network.clone(self.root / 'vendor' / name, url, commit, sparse=sparse, run=self.command,
+ network=self.network, env=self.env)
+
+ def packages(self, label, args, family='pypi'):
+ return network.package_command(self.network, family,
+ lambda env, source: self.command(label if source == 'official' else label + '-' + source, args, env=env), self.env)
+
+ def python_install(self, uv):
+ from .system import bootstrap_root
+ prepared = bootstrap_root(self.root) / 'python'
+ current = Path(self.env['UV_PYTHON_INSTALL_DIR'])
+ # A from-zero launcher already installed this exact Python. Reuse its
+ # base interpreter when creating the single compute venv; do not download
+ # and unpack another copy. Existing installations keep their own base.
+ if prepared.is_dir() and not any(current.glob('cpython-' + self.versions['python'] + '-*')):
+ env = dict(self.env, UV_PYTHON_INSTALL_DIR=str(prepared))
+ found = subprocess.run([str(uv), '--no-config', 'python', 'find', '--managed-python',
+ '--no-python-downloads', self.versions['python']],
+ env=env, capture_output=True, text=True, timeout=30)
+ if found.returncode == 0:
+ candidate = Path(found.stdout.strip())
+ if candidate.is_file() and candidate.resolve().is_relative_to(prepared.resolve()):
+ check = subprocess.run([str(candidate), '-I', '-B', '-c',
+ 'import platform; print(platform.python_version())'], capture_output=True, text=True, timeout=30)
+ if check.returncode == 0 and check.stdout.strip() == self.versions['python']:
+ self.env['UV_PYTHON_INSTALL_DIR'] = str(prepared)
+ self.command('python-bootstrap-reuse', [candidate, '-I', '-B', '-c',
+ 'import sys; print("Reusing verified bootstrap Python: " + sys.executable)'])
+ return
+ custom = self.env.get('UV_PYTHON_INSTALL_MIRROR') if self.network.get('mode') != 'official' else None
+ sources = (['user-python'] if custom else []) + network.ordered(self.network, 'github')
+ isolation = ['--no-bin', '--no-registry']
+ for source in sources:
+ for route in network.route_order(self.network, 'github', source, self.env):
+ env = network.route_environment(self.network, 'github', source, self.env, route)
+ env.update(UV_HTTP_TIMEOUT='20', UV_HTTP_RETRIES='1')
+ env['UV_PYTHON_INSTALL_MIRROR'] = custom if source == 'user-python' else network.source_url('github', source, self.env) + '/astral-sh/python-build-standalone/releases/download'
+ try:
+ result = self.command('python-' + source + '-' + route, [uv, 'python', 'install', *isolation, self.versions['python']], env=env)
+ network.route_health(self.network, 'github', source, route)
+ return result
+ except RuntimeError as error:
+ if not network.retryable(error):
+ raise
+ network.event(self.network, category='python', source=source, route=route, action='fallback', reason='network-download-failed')
+ if not network.connection_failure(error):
+ break
+ raise RuntimeError('Python download failed on every route; see retained logs')
+
+ def install_sage(self, uv, python):
+ if self.system == 'Windows':
+ return self.install_sage_windows(uv, python)
+ code = ('import json,platform,sys,torch; print(json.dumps(dict('
+ 'python="cp"+str(sys.version_info.major)+str(sys.version_info.minor), '
+ 'torch=str(torch.__version__),cuda=torch.version.cuda,cxx11_abi=torch.compiled_with_cxx11_abi(), '
+ 'machine=platform.machine(),glibc=platform.libc_ver())))')
+ identity = json.loads(subprocess.check_output([str(python), '-c', code], env=self.env, text=True))
+ identity.update(arch='.'.join(map(str, self.plan['inventory']['hardware']['capability'])),
+ source=self.spec['sageattention']['commit'], compiler=subprocess.check_output(
+ ['g++', '--version'], text=True).splitlines()[0])
+ wheel_dir = Path(self.plan['wheel_cache']) / wheel_key(identity)
+ manifest = wheel_dir / 'manifest.json'
+ record = json.loads(manifest.read_text(encoding='utf-8')) if manifest.is_file() else None
+ cached = False
+ if record and not self.plan['rebuild_sage']:
+ filename = record['filename']
+ if Path(filename).name != filename or not filename.endswith('.whl'):
+ raise ValueError('Invalid wheel cache filename')
+ wheel = wheel_dir / filename
+ cached = record['identity'] == identity and wheel.is_file() and digest(wheel) == record['sha256']
+ if not cached:
+ # Keep old wheels/build objects when forcing a fresh measured build.
+ if wheel_dir.exists():
+ wheel_dir.rename(wheel_dir.with_name(wheel_dir.name + '.previous-' + self.run_dir.name))
+ wheel_dir.mkdir(parents=True)
+ toolkit = self.install_toolkit(identity['cuda'])
+ sage = self.clone('SageAttention', self.spec['sageattention']['url'], self.spec['sageattention']['commit'])
+ stamp = sage / '.freevideo-build.json'
+ previous_identity = json.loads(stamp.read_text(encoding='utf-8')) if stamp.is_file() else None
+ if (self.plan['rebuild_sage'] or previous_identity != identity) and (sage / 'build').exists():
+ (sage / 'build').rename(sage / ('build.previous-' + self.run_dir.name))
+ save(stamp, identity)
+ header_code = ('from pathlib import Path; import site; '
+ 'print(":".join(str(p) for s in site.getsitepackages() for p in (Path(s)/"nvidia").glob("*/include")))')
+ headers = subprocess.check_output([str(python), '-c', header_code], env=self.env, text=True).strip()
+ build = self.plan['build']
+ build_env = dict(self.env, CUDA_HOME=str(toolkit), PATH=str(toolkit / 'bin') + os.pathsep + self.env['PATH'],
+ CPATH=headers, TORCH_CUDA_ARCH_LIST=identity['arch'], MAX_JOBS=str(build['jobs']),
+ EXT_PARALLEL='1', NVCC_APPEND_FLAGS='--threads=' + str(build['nvcc_threads']))
+ self.command('sage2-toolchain-check', [python, '-c',
+ 'from torch.utils.cpp_extension import is_ninja_available; '
+ 'assert is_ninja_available(), "Ninja must be visible in the build PATH; serial fallback is refused"'], env=build_env)
+ self.command('sage2-build', [python, '-m', 'pip', 'wheel', '--verbose', '--no-build-isolation', '--no-deps',
+ '--wheel-dir', wheel_dir, sage], env=build_env)
+ wheels = list(wheel_dir.glob('*.whl'))
+ if len(wheels) != 1:
+ raise ValueError('Expected one locally built Sage2 wheel')
+ wheel = wheels[0]
+ record = {'identity': identity, 'filename': wheel.name, 'sha256': digest(wheel),
+ 'bytes': wheel.stat().st_size, 'build_run': str(self.run_dir), 'kernel_validation': 'pending'}
+ save(manifest, record)
+ # Install from the verified wheel path, avoiding architecture-blind source caches.
+ self.command('sage2-wheel-install', [uv, 'pip', 'install', '--python', python, '--no-deps',
+ '--reinstall-package', 'sageattention', wheel])
+ save(self.run_dir / 'sage-wheel.json', dict(record, reused=cached, cache=str(wheel_dir)))
+ return manifest
+
+ def install_sage_windows(self, uv, python):
+ spec = self.versions['windows']['sageattention']
+ directory = Path(self.plan['wheel_cache']) / 'windows-sage2' / spec['sha256'][:16]
+ wheel = directory / unquote(urlsplit(spec['url']).path.rsplit('/', 1)[-1])
+ reused = wheel.is_file()
+ self.fetch(spec['url'], wheel, spec['sha256'])
+ self.command('sage2-windows-wheel-install', [uv, 'pip', 'install', '--python', python,
+ '--no-deps', '--reinstall-package', 'sageattention', wheel])
+ record = dict(spec, filename=wheel.name, reused=reused, cache=str(directory),
+ build_run=str(self.run_dir), kernel_validation='pending',
+ architecture=self.plan['inventory']['hardware']['capability'])
+ manifest = directory / 'manifest.json'
+ save(manifest, record)
+ save(self.run_dir / 'sage-wheel.json', record)
+ return manifest
+
+ def install_git_windows(self):
+ if shutil.which('git', path=self.env['PATH']):
+ return
+ spec = self.versions['windows']['git']
+ archive = self.root / 'downloads' / ('MinGit-' + spec['version'] + '.zip')
+ self.fetch(spec['url'], archive, spec['sha256'])
+ directory = self.root / 'tools' / ('git-' + spec['version'])
+ unpack(archive, directory)
+ executable = directory / spec['executable']
+ if not executable.is_file():
+ raise RuntimeError('Portable Git archive is incomplete: ' + str(executable))
+ self.env['PATH'] = str(executable.parent) + os.pathsep + self.env['PATH']
+ self.command('portable-git-check', [executable, '--version'])
+ self.env['FREEVIDEO_GIT'] = str(executable)
+
+ def install_toolkit(self, cuda):
+ spec = self.versions['cuda_toolkits'][cuda]
+ toolkit = self.root / 'tools' / ('cuda-' + cuda)
+ receipt = toolkit / '.freevideo-toolkit.json'
+ complete = lambda: all((toolkit / name).is_file() and (toolkit / name).stat().st_size
+ for name in spec['required_files'])
+ if receipt.is_file() and json.loads(receipt.read_text(encoding='utf-8')) == spec and complete():
+ return toolkit
+ # CUDA 13 splits CRT and NVVM into additional archives. An nvcc binary
+ # alone is not a usable compiler, especially after interrupted setup.
+ toolkit.mkdir(parents=True, exist_ok=True)
+ for component in spec['components'].values():
+ archive = self.root / 'downloads' / Path(component['relative_path']).name
+ self.fetch(self.versions['cuda_redist_base'] + component['relative_path'], archive, component['sha256'])
+ unpack(archive, self.root / 'tools' / 'cuda-components')
+ unpacked = self.root / 'tools' / 'cuda-components' / archive.name.removesuffix('.tar.xz')
+ shutil.copytree(unpacked, toolkit, dirs_exist_ok=True, symlinks=True)
+ if not (toolkit / 'lib64').exists() and not (toolkit / 'lib64').is_symlink():
+ (toolkit / 'lib64').symlink_to('lib', target_is_directory=True)
+ if not complete():
+ raise RuntimeError('CUDA toolkit is incomplete: ' + str(toolkit))
+ save(receipt, spec)
+ return toolkit
+
+ def execute(self):
+ self.ui.phase('Prepare Python', 0, 8)
+ uv_spec = self.versions['uv']
+ archive = self.root / 'downloads' / ('uv-windows.zip' if self.system == 'Windows' else 'uv.tar.gz')
+ from .system import bootstrap_root
+ previous = bootstrap_root(self.root) / ('uv.zip' if self.system == 'Windows' else 'uv-' + uv_spec['version'] + '.tar.gz')
+ if not archive.exists() and previous.is_file() and digest(previous) == uv_spec['sha256']:
+ archive.parent.mkdir(parents=True, exist_ok=True)
+ try:
+ os.link(previous, archive)
+ except OSError:
+ shutil.copyfile(previous, archive)
+ self.fetch(uv_spec['url'], archive, uv_spec['sha256'])
+ unpack(archive, self.root / 'tools')
+ uv = (self.root / 'tools' / uv_spec['executable'] if self.system == 'Windows' else
+ self.root / 'tools' / 'uv-x86_64-unknown-linux-gnu' / 'uv')
+ self.env['FREEVIDEO_UV'] = str(uv)
+ from .install_schedule import run
+ tasks, pythons, comfy = self.component_tasks(uv)
+ total = len(tasks) + 4 # Bootstrap, preparation, checks and final readiness.
+ def scheduling(name, status, completed, count):
+ with self.state_lock:
+ self.state.setdefault('schedule', {})[name] = dict(status=status, after=tasks[name]['after'],
+ exclusive_writers=tasks[name]['writes'], epoch=time.time())
+ save(self.run_dir / 'status.json', self.state)
+ self.ui.phase('Install components in parallel', 1 + completed, total)
+ self.ui.phase('Install components in parallel', 1, total)
+ results = run(tasks, self.cancel, progress=scheduling)
+ sage_manifest = results['sage']
+ python, encoder_python = self.pythons['engine'], self.pythons['encoder']
+ prepared = self.run_dir / 'prepared.json'
+ self.ui.phase('Optimize model storage', total - 3, total)
+ self.command('prepare', [python, '-m', 'freevideo_engine.provision', '--plan', self.run_dir / 'plan.json',
+ '--prepare', '--out', prepared])
+ self.ui.phase('Verify installation on your GPU', total - 2, total)
+ for name, executable in pythons.items():
+ self.command(name + '-dependency-check', [uv, 'pip', 'check', '--python', executable])
+ self.command(name + '-freeze', [uv, 'pip', 'freeze', '--python', executable])
+ kernel_report = self.run_dir / 'kernel-capabilities.json'
+ # Kernel probes execute real CUDA work and take roughly a minute on a
+ # consumer GPU. A completed receipt is reusable only when every
+ # input that can change the result still matches: GPU identity,
+ # driver, Torch/CUDA, installed attention packages, and the engine
+ # source hashes. Keep the run-local copy so diagnostics retain the
+ # exact receipt used by this installation.
+ cached_kernel = self.root / 'kernel-capabilities.json'
+ hardware_data = self.plan.get('inventory', {}).get('hardware')
+ receipt = (reusable_kernel_receipt(cached_kernel, hardware_data)
+ if isinstance(hardware_data, dict) else None)
+ if receipt is None:
+ self.command('kernels', [python, '-m', 'freevideo_engine', 'doctor', '--probe', '--require-paths', '--out', kernel_report])
+ else:
+ save(kernel_report, receipt)
+ self.ui.event('setup_cache', key='kernels', detail='Reused matching GPU kernel checks')
+ self.command('storage', [python, '-m', 'freevideo_engine.provision', '--plan', self.run_dir / 'plan.json',
+ '--cleanup', '--out', self.run_dir / 'storage.json'])
+ sage_record = json.loads(sage_manifest.read_text(encoding='utf-8'))
+ kernels = json.loads(kernel_report.read_text(encoding='utf-8'))
+ sage_record['kernel_validation'] = ('Small Sage2 kernels passed on ' + self.plan['inventory']['selected_gpu']['uuid']
+ if 'sage2' in kernels['usable_attention_backends'] else 'Sage2 probe failed; see ' + str(kernel_report))
+ save(sage_manifest, sage_record)
+ self.ui.phase('Finish setup', total - 1, total)
+ configuration = {'schema_version': 1, 'root': str(self.root), 'source': str(SOURCE),
+ 'storage': self.plan.get('storage', 'compact'),
+ 'environment_layout': self.layout,
+ 'system': self.system, 'engine_version': __version__,
+ 'git': self.env.get('FREEVIDEO_GIT') or shutil.which('git', path=self.env['PATH']),
+ 'platform_validation': 'Local dependency and small GPU probes passed; full consumer-GPU validation comes from user test reports.',
+ 'kernel_capabilities': str(self.root / 'kernel-capabilities.json'),
+ 'python': str(python), 'comfy_python': str(encoder_python), 'comfy_root': str(comfy),
+ 'vdn_root': str(self.root / 'vendor' / 'vdn'), 'model_root': self.plan['model_dir'],
+ 'encoder_model_root': self.plan['encoder_dir'], 'wheel_cache': self.plan['wheel_cache'],
+ 'base': str(Path(self.plan['model_dir']) / 'h3-base'),
+ 'checkpoint': str(Path(self.plan['model_dir']) / 'stage-dmd-step-250'),
+ 'cache': json.loads(prepared.read_text(encoding='utf-8'))['cache'],
+ 'encoder': self.spec['models']['encoder_file'].split('/')[-1],
+ 'model_paths': str(self.root / 'encoder-paths.yaml'),
+ 'gpu_uuid': self.plan['inventory']['selected_gpu']['uuid'],
+ 'vram_gib': self.plan.get('vram_gib'), 'ram_gib': self.plan.get('ram_gib'),
+ 'model_revision': self.spec['models']['vdn_revision'],
+ 'setup_run': str(self.run_dir), 'ready': True}
+ # Commit readiness only after all steps pass. Failed/rerun setup files
+ # remain available, and no model/output cleanup runs automatically.
+ self.monitor_stop.set()
+ self.monitor_thread.join(timeout=5)
+ if self.monitor_thread.is_alive() or self.cancel.is_set():
+ raise RuntimeError(self.state.get('resource_guard', 'Installation monitoring did not stop cleanly.'))
+ save(self.root / 'machine.json', configuration)
+ self.ui.phase('Setup complete', total, total)
+ return configuration
+
+
+def main(argv=None):
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--root', type=Path, default=Path(os.environ.get('FREEVIDEO_HOME', DEFAULT_ROOT)))
+ parser.add_argument('--gpu', help='Physical nvidia-smi index or full GPU UUID')
+ parser.add_argument('--environment', choices=('unified', 'dual'),
+ help='New installs default to unified; updates retain their saved layout. Existing environments are kept when switching.')
+ parser.add_argument('--models', type=Path, help='Reuse/download official model files in this directory')
+ parser.add_argument('--model-source', choices=('prepared', 'source'), default='prepared',
+ help='Default: download a pinned slim model matching the GPU format. source: explicitly download original weights and convert locally')
+ parser.add_argument('--encoder-models', type=Path, help='Directory containing text_encoders/')
+ parser.add_argument('--reuse-models', type=Path, help='Scan an existing model library, verify pinned content and import matching files; only missing models are downloaded')
+ parser.add_argument('--reuse-models-manifest', type=Path, help='Versioned JSON containing model library roots, including ComfyUI extra model paths')
+ parser.add_argument('--copy-existing-models', action='store_true', help='Copy verified local models instead of using same-disk hardlinks; source files are kept')
+ parser.add_argument('--allow-model-restart', action='store_true', help='Permit a fresh download when an incomplete model cannot resume; retains old data and may use extra download/disk space')
+ cache_options = parser.add_mutually_exclusive_group()
+ cache_options.add_argument('--cache', type=Path, help='Verify and reuse an existing FP8 cache')
+ cache_options.add_argument('--rebuild-cache', action='store_true', help='Recreate FP8 from pinned source weights, downloading missing sources; preserve failed caches')
+ parser.add_argument('--storage', choices=('compact', 'retain'), help='Default compact: stream FP8 and remove installer-owned conversion sources after verification; retain keeps sources for re-quantization')
+ parser.add_argument('--verify', choices=('auto', 'full'), default='auto', help='auto reuses unchanged pinned receipts; full rereads every required tensor')
+ parser.add_argument('--wheel-cache', type=Path, help='Reusable local Sage2 wheel cache, keyed by architecture and ABI')
+ parser.add_argument('--rebuild-sage', action='store_true', help='Measure a fresh source build, retaining previous build artifacts')
+ parser.add_argument('--vram-gib', type=float, help='Save an explicit nominal VRAM cap; default uses detected resources')
+ parser.add_argument('--ram-gib', type=float, help='Save an explicit nominal RAM cap; default uses detected resources')
+ resources = parser.add_mutually_exclusive_group()
+ resources.add_argument('--keep-resource-limits', action='store_true', help='Reuse saved VRAM/RAM caps on update or retry; explicit capacity options override them')
+ resources.add_argument('--auto-resources', action='store_true', help='Compatibility alias for the default: detect resources without reusing saved capacity caps')
+ parser.add_argument('--network', choices=('auto', 'official'), default='auto', help='Auto: rank sources; official: original upstream sources only. Compare configured proxy and temporary direct routes')
+ parser.add_argument('--network-timeout', type=float, default=5, help='Per-connection/probe timeout in seconds (1–30); stalled transfers retry other sources')
+ parser.add_argument('--model-downloader', choices=['auto', 'xet'], default='auto',
+ help='xet: require official HF Xet for large weights; no ModelScope/HTTP fallback. Small configs use HTTP; existing files/partials retained')
+ parser.add_argument('--plan', '--check', action='store_true', help='Read-only preflight with small network samples; no installation files are written')
+ parser.add_argument('--json', action='store_true', help='Print preflight as JSON')
+ parser.add_argument('--plain', action='store_true', help='Disable live terminal rendering; append readable log lines')
+ parser.add_argument('--verbose', action='store_true', help='Show full setup details, license links and per-step log paths')
+ parser.add_argument('--no-color', action='store_true', help='Disable colors (also respects NO_COLOR)')
+ parser.add_argument('--hardware-json', type=Path, help='Offline inventory fixture, only with --plan')
+ parser.add_argument('--yes', action='store_true')
+ parser.add_argument('--accept-model-license', action='store_true')
+ parser.add_argument('--approved-plan', type=Path, help='GUI confirmation receipt; changed paths, models or larger disk/download requirements require a new review')
+ args = parser.parse_args(argv)
+ if not 1 <= args.network_timeout <= 30:
+ parser.error('--network-timeout must be between 1 and 30 seconds')
+ if args.hardware_json and not args.plan:
+ parser.error('--hardware-json is only permitted with --plan')
+ if args.json and not args.plan:
+ parser.error('--json requires --plan')
+ if args.auto_resources and (args.vram_gib is not None or args.ram_gib is not None):
+ parser.error('--auto-resources cannot be combined with --vram-gib or --ram-gib')
+ if args.copy_existing_models and not (args.reuse_models or args.reuse_models_manifest):
+ parser.error('--copy-existing-models requires --reuse-models or --reuse-models-manifest')
+ ui = TerminalUI(platform.system() + ' setup', plain=args.plain, no_color=args.no_color,
+ verbose=args.verbose, show_location=args.verbose)
+ try:
+ from .local_models import progress_output
+ value = plan(args, local_progress=progress_output if os.environ.get('FREEVIDEO_UI_EVENTS') == '1' or not args.json else None)
+ except (OSError, RuntimeError, ValueError, subprocess.CalledProcessError) as error:
+ print('Preflight failed: ' + str(error), file=sys.stderr)
+ return 1
+ print(json.dumps(value, indent=2)) if args.json else display(value, ui, verbose=args.verbose)
+ if args.plan:
+ return 1 if value['errors'] else 0
+ if value['errors']:
+ return 1
+ try:
+ accepted = confirmed(args, value, ui=ui)
+ except KeyboardInterrupt:
+ print('\nCancelled. No engine or model installation was started.')
+ return 130
+ except ValueError as error:
+ print(str(error), file=sys.stderr)
+ return 1
+ if not accepted:
+ print('Cancelled. No engine or model installation was started.')
+ return 0
+ try:
+ installer = Installer(value, ui)
+ except BlockingIOError:
+ print('Setup, testing or generation is using this installation/GPU lock. Retry after it finishes.', file=sys.stderr)
+ from .locking import lock_holders
+ owners = lock_holders([Path(value['root']) / 'setup.lock', os.environ.get('FREEVIDEO_LOCK_PATH', str(Path(value['root']) / 'engine.lock'))])
+ if owners:
+ print('Processes with open lock handles: %s. Stop the old task before retrying; do not delete lock files.' % ', '.join(map(str, owners)), file=sys.stderr)
+ return 1
+ except (OSError, ValueError) as error:
+ print('Setup could not start: ' + str(error), file=sys.stderr)
+ return 1
+ def interrupted(signum, frame):
+ raise KeyboardInterrupt('Setup interrupted by signal %s' % signum)
+ previous = processes.termination_handler(interrupted)
+ try:
+ ui.start(installer.run_dir)
+ installer.start_monitor()
+ installer.execute()
+ installer.state['status'] = 'complete'
+ except BaseException as error:
+ installer.state.update(status='failed', error=repr(error))
+ ui.phase('Setup interrupted' if isinstance(error, KeyboardInterrupt) else 'Setup failed', ui.done, ui.total)
+ finally:
+ try:
+ installer.monitor_stop.set()
+ if installer.monitor_thread:
+ installer.monitor_thread.join(timeout=5)
+ installer.state['wall_seconds'] = time.monotonic() - installer.started
+ save(installer.run_dir / 'status.json', installer.state)
+ except OSError as error:
+ installer.state.update(status='failed', error='Could not save final setup status: ' + repr(error))
+ finally:
+ processes.restore_handlers(previous)
+ installer.locks.close()
+ ui.close()
+ if installer.state['status'] == 'complete':
+ entry = '.\\test.ps1' if platform.system() == 'Windows' else './test.sh'
+ print('\nSetup complete. Next: %s --root "%s"' % (entry, installer.root))
+ if args.verbose:
+ print('Configuration: %s' % (installer.root / 'machine.json'))
+ return 0
+ print('\nSetup failed: %s\nAll files retained. Logs: %s\nRerun the same setup command to resume.' %
+ (installer.state.get('resource_guard') or installer.state['error'], installer.run_dir), file=sys.stderr)
+ return 1
+
+
+if __name__ == '__main__':
+ raise SystemExit(main())
diff --git a/freevideo_engine/bootstrap_versions.json b/freevideo_engine/bootstrap_versions.json
new file mode 100644
index 0000000..6eeb30d
--- /dev/null
+++ b/freevideo_engine/bootstrap_versions.json
@@ -0,0 +1,107 @@
+{
+ "python": "3.12.3",
+ "windows": {
+ "uv": {
+ "version": "0.9.0",
+ "url": "https://github.com/astral-sh/uv/releases/download/0.9.0/uv-x86_64-pc-windows-msvc.zip",
+ "sha256": "4bad2c47dafb52ad774072fb7a260e5c9f3dbf73441658db1128eb2aec5818eb",
+ "executable": "uv.exe"
+ },
+ "triton": "3.7.1.post27",
+ "git": {
+ "version": "2.55.0.5",
+ "url": "https://github.com/git-for-windows/git/releases/download/v2.55.0.windows.5/MinGit-2.55.0.5-64-bit.zip",
+ "sha256": "56d7b226b7693196cfc71fef26568f536c4a021ab6c37ff2db4287bed908e96e",
+ "bytes": 38989688,
+ "executable": "cmd/git.exe"
+ },
+ "sageattention": {
+ "version": "2.2.0+cu130torch2.10.0andhigher.post6",
+ "url": "https://github.com/woct0rdho/SageAttention/releases/download/v2.2.0-windows.post6/sageattention-2.2.0%2Bcu130torch2.10.0andhigher.post6-cp310-abi3-win_amd64.whl",
+ "sha256": "1635283f5c01ec3cda58a784d0d7eabbcaffaf9511d1b263db4750e1ed7958bb",
+ "bytes": 16656067,
+ "source": "https://github.com/woct0rdho/SageAttention/releases/tag/v2.2.0-windows.post6",
+ "scope": "Windows port of SageAttention 2.2.0; distinct build from the Linux source pin. Actual kernels must pass setup probes."
+ }
+ },
+ "uv": {
+ "version": "0.9.0",
+ "url": "https://github.com/astral-sh/uv/releases/download/0.9.0/uv-x86_64-unknown-linux-gnu.tar.gz",
+ "sha256": "4dadaa5ff5009ccd6a0a43f6ccfa32bf36ed2eff18df7011275a9b1d81950e7b"
+ },
+ "cuda_redist_base": "https://developer.download.nvidia.com/compute/cuda/redist/",
+ "cuda_toolkits": {
+ "12.9": {
+ "components": {
+ "cuda_nvcc": {
+ "relative_path": "cuda_nvcc/linux-x86_64/cuda_nvcc-linux-x86_64-12.9.86-archive.tar.xz",
+ "sha256": "7a1a5b652e5ef85c82b721d10672fc9a2dbaab44e9bd3c65a69517bf53998c35",
+ "md5": "ac1871c955070267e117bd985f6dbb36",
+ "size": "81100244"
+ },
+ "cuda_cudart": {
+ "relative_path": "cuda_cudart/linux-x86_64/cuda_cudart-linux-x86_64-12.9.79-archive.tar.xz",
+ "sha256": "1f6ad42d4f530b24bfa35894ccf6b7209d2354f59101fd62ec4a6192a184ce99",
+ "md5": "6153e2b6a43389e5be3d68b04c6488f5",
+ "size": "1514676"
+ },
+ "cuda_cccl": {
+ "relative_path": "cuda_cccl/linux-x86_64/cuda_cccl-linux-x86_64-12.9.27-archive.tar.xz",
+ "sha256": "8b1a5095669e94f2f9afd7715533314d418179e9452be61e2fde4c82a3e542aa",
+ "md5": "59e3cb66aeb96423dcca73218c85bc02",
+ "size": "997888"
+ }
+ },
+ "required_files": [
+ "bin/nvcc",
+ "include/cuda_runtime.h",
+ "include/crt/host_config.h",
+ "include/cub/cub.cuh",
+ "nvvm/bin/cicc",
+ "nvvm/libdevice/libdevice.10.bc"
+ ]
+ },
+ "13.0": {
+ "components": {
+ "cuda_nvcc": {
+ "relative_path": "cuda_nvcc/linux-x86_64/cuda_nvcc-linux-x86_64-13.0.88-archive.tar.xz",
+ "sha256": "48e35be3cfbf4b4fbc16828eaec8a7048ee789403049dc409f7b643d6259cf7a",
+ "md5": "e3e925c644055b9306aec9c9b84d1975",
+ "size": "26657716"
+ },
+ "cuda_cudart": {
+ "relative_path": "cuda_cudart/linux-x86_64/cuda_cudart-linux-x86_64-13.0.96-archive.tar.xz",
+ "sha256": "25b8071951baba827be1580b841d363464f6ee6c39f48d33a81646f90cc95ed1",
+ "md5": "26ebf8fd2c0fe1f303d5d27c49624d5e",
+ "size": "1487960"
+ },
+ "cuda_cccl": {
+ "relative_path": "cuda_cccl/linux-x86_64/cuda_cccl-linux-x86_64-13.0.85-archive.tar.xz",
+ "sha256": "ed845eae8c1767706b6ee91e40c608a03f6f633551a849b63f7346d32d73ee60",
+ "md5": "c0dd26e0c41dc35bd73435a4799c7508",
+ "size": "991284"
+ },
+ "cuda_crt": {
+ "relative_path": "cuda_crt/linux-x86_64/cuda_crt-linux-x86_64-13.0.88-archive.tar.xz",
+ "sha256": "5a3279a049ffc1cdb951c44cb95206acfdde9e9ae5e87825fc18d7e4a6878bb0",
+ "md5": "f90b38d702a29668d97e8f94a399ea31",
+ "size": "79800"
+ },
+ "libnvvm": {
+ "relative_path": "libnvvm/linux-x86_64/libnvvm-linux-x86_64-13.0.88-archive.tar.xz",
+ "sha256": "17ef1665b63670887eeba7d908da5669fa8c66bb73b5b4c1367f49929c086353",
+ "md5": "816927093ffbdc10fddd751f045396b1",
+ "size": "43645408"
+ }
+ },
+ "required_files": [
+ "bin/nvcc",
+ "include/cuda_runtime.h",
+ "include/crt/host_config.h",
+ "include/cccl/cub/cub.cuh",
+ "nvvm/bin/cicc",
+ "nvvm/libdevice/libdevice.10.bc"
+ ]
+ }
+ }
+}
diff --git a/freevideo_engine/branding.py b/freevideo_engine/branding.py
new file mode 100644
index 0000000..81a998c
--- /dev/null
+++ b/freevideo_engine/branding.py
@@ -0,0 +1,368 @@
+"""Shared native UI palette, type scale and bundled artwork.
+
+One table defines every surface, hue, text size, radius and gap the native
+views may use, so a new panel picks an existing step instead of inventing a
+shade. No imaging dependency at runtime.
+"""
+import math
+from pathlib import Path
+
+# Surfaces, from the window backdrop up to a hovered control. A navy tint
+# runs through every step; the content pane sits one step above the window.
+BACKGROUND = '#111720' # Window, sidebar and input wells.
+CANVAS = '#161e29' # Content pane of the launcher.
+SURFACE = '#1a222e'
+RAISED = '#212a37'
+HOVER = '#2a3341'
+BORDER = '#2a3443'
+SHEEN = '#35404e' # Control outlines; one-pixel lift on a card's edge.
+
+# Ink. MUTED carries secondary copy; DISABLED never states anything new.
+TEXT = '#e7edf7'
+MUTED = '#a5b3c6'
+DISABLED = '#7f8ea3'
+
+# One hue per meaning. A SUBTLE value is the backdrop for its own status only;
+# it is not decoration for unrelated controls. DANGER matches the web theme's
+# --fv-danger so a failure reads the same in both interfaces.
+ACCENT = '#6db8fa'
+ACCENT_HOVER = '#91cbff'
+ACCENT_RAMP = '#82dcd3' # Gradient destination for a progress fill.
+ACCENT_DIM = '#406c93' # A meter that is subordinate to another one.
+ACCENT_SUBTLE = '#1d3248'
+ACCENT_DISABLED = '#243a52'
+ACCENT_DISABLED_TEXT = '#8ba0b6'
+SUCCESS = '#76d9b3'
+SUCCESS_SUBTLE = '#1f3636'
+WARNING = '#e0b062'
+DANGER = '#f09aa2'
+DANGER_SUBTLE = '#30212a'
+
+# Type: five steps, not one per widget. BODY stays at Segoe UI's native
+# interface size. Raising it reflows every view, and the launcher must still
+# fit its 680x540 minimum, where the real-Tk tests assert that the primary
+# button stays inside the window; rerun those before changing BODY.
+FAMILY = 'Segoe UI'
+MICRO, BODY, STRONG, SECTION, HERO = 9, 10, 12, 15, 22
+MONO = 'Consolas' # Retained log output only.
+
+# Three radii and one spacing unit. Gaps are multiples of STEP.
+RADIUS_SM, RADIUS_MD, RADIUS_LG = 8, 12, 16
+STEP = 4
+
+
+def space(units):
+ """Vertical and horizontal gaps come from the same four-pixel rhythm."""
+ return STEP * units
+
+
+def rgb(value):
+ return tuple(int(value[index:index + 2], 16) for index in (1, 3, 5))
+
+
+def _mix(under, over, alpha):
+ return tuple(round(a + (b - a) * alpha) for a, b in zip(under, over))
+
+
+def _area(x, y, rect, radius, inset, samples=4):
+ """Antialiased share of one pixel covered by a rounded rectangle.
+
+ Tk's boolean per-pixel transparency cannot smooth a corner, so shapes are
+ baked over the surface they sit on and the coverage is supersampled here.
+ The rectangle is explicit, so an image may carry a gap beside its shape.
+ """
+ x0, y0, x1, y1 = rect
+ cx, cy = (x0 + x1)/2., (y0 + y1)/2.
+ half_w, half_h = (x1 - x0)/2. - inset, (y1 - y0)/2. - inset
+ r = max(0., radius - inset)
+ covered = 0
+ for row in range(samples):
+ py = abs(y + (row + .5)/samples - cy)
+ for column in range(samples):
+ px = abs(x + (column + .5)/samples - cx)
+ if px > half_w or py > half_h:
+ continue
+ dx, dy = px - (half_w - r), py - (half_h - r)
+ covered += 1 if dx <= 0 or dy <= 0 else dx*dx + dy*dy <= r*r
+ return covered / float(samples*samples)
+
+
+def _stroke(x, y, points, thickness, samples=4):
+ """Antialiased coverage of a polyline, for the check inside an indicator."""
+ half = thickness/2.
+ covered = 0
+ for row in range(samples):
+ py = y + (row + .5)/samples
+ for column in range(samples):
+ px = x + (column + .5)/samples
+ best = 1e9
+ for (x0, y0), (x1, y1) in zip(points, points[1:]):
+ dx, dy = x1-x0, y1-y0
+ length = dx*dx + dy*dy
+ t = 0. if not length else max(0., min(1., ((px-x0)*dx + (py-y0)*dy) / length))
+ best = min(best, math.hypot(px - (x0 + t*dx), py - (y0 + t*dy)))
+ covered += max(0., min(1., half + .5 - best))
+ return covered / float(samples*samples)
+
+
+def rounded_photo(master, width, height, radius, fill, backdrop, outline=None, check=None, rect=None):
+ """A rounded rectangle, and optionally a check mark, baked over a backdrop."""
+ import tkinter as tk
+ under, face = rgb(backdrop), rgb(fill)
+ edge = rgb(outline) if outline else face
+ mark = rgb(check[0]) if check else None
+ points = check[1] if check else ()
+ rect = (0, 0, width, height) if rect is None else rect
+ rows = []
+ for y in range(height):
+ row = []
+ for x in range(width):
+ colour = _mix(under, edge, _area(x, y, rect, radius, 0.))
+ colour = _mix(colour, face, _area(x, y, rect, radius, 1.))
+ if mark:
+ colour = _mix(colour, mark, _stroke(x, y, points, 2.2))
+ row.append('#%02x%02x%02x' % colour)
+ rows.append('{' + ' '.join(row) + '}')
+ image = tk.PhotoImage(master=master, width=width, height=height)
+ image.put(' '.join(rows))
+ return image
+
+
+def _retain(master, images):
+ """Tk discards an image whose Python object is collected."""
+ kept = getattr(master, 'freevideo_images', None)
+ if kept is None:
+ kept = master.freevideo_images = []
+ kept.extend(images)
+
+
+def _image_element(style, master, element, faces, order, *, radius, backdrop, padding=0):
+ """Replace a themed border with a 9-slice rounded image, one per state.
+
+ ttk cannot round a corner. Substituting the outermost element of an
+ existing layout keeps every ttk state, padding and label behaviour.
+ `order` lists state specifications most specific first, as ttk matches
+ them in order; the default face is drawn when none applies.
+ """
+ import tkinter as tk
+ size = 2*radius + 6
+ images = {name: rounded_photo(master, size, size, radius, fill, backdrop, outline)
+ for name, (fill, outline) in faces.items()}
+ _retain(master, images.values())
+ # Each entry stays one (state..., image) tuple; ttk unpacks it that way.
+ specs = [tuple(list(states) + [images[name]]) for states, name in order]
+ try:
+ style.element_create(element, 'image', images['normal'], *specs,
+ # border is the 9-slice split only. Left implicit,
+ # ttk also insets the children by it and every
+ # control grows; a tab needs its own padding back,
+ # because the element it replaces carried it.
+ border=radius, padding=padding, sticky='nsew')
+ except tk.TclError:
+ return False
+ return True
+
+
+BUTTON_STATES = ((('disabled',), 'disabled'), (('pressed',), 'pressed'),
+ (('active',), 'active'), (('focus',), 'focus'))
+
+
+def _rounded_buttons(style, master):
+ """Round every ttk button: on the window, on a card, and the accent one."""
+ faces = lambda fill, hover, pressed, off, edge=BORDER: dict(
+ normal=(fill, edge), active=(hover, edge), pressed=(pressed, ACCENT),
+ disabled=(off, edge), focus=(fill, ACCENT))
+ plans = (
+ ('FreeVideo.button', 'TButton', BACKGROUND, faces(RAISED, HOVER, RAISED, SURFACE)),
+ ('FreeVideoCard.button', 'Card.TButton', SURFACE, faces(RAISED, HOVER, RAISED, SURFACE)),
+ ('FreeVideoPrimary.button', 'Primary.TButton', BACKGROUND,
+ dict(normal=(ACCENT, ACCENT), active=(ACCENT_HOVER, ACCENT_HOVER), pressed=(ACCENT, ACCENT),
+ disabled=(ACCENT_DISABLED, ACCENT_DISABLED), focus=(ACCENT, TEXT))),
+ ('FreeVideoQuiet.button', 'Quiet.TButton', BACKGROUND,
+ dict(normal=(BACKGROUND, BACKGROUND), active=(RAISED, RAISED), pressed=(RAISED, RAISED),
+ disabled=(BACKGROUND, BACKGROUND), focus=(BACKGROUND, ACCENT))),
+ )
+ for element, target, backdrop, states in plans:
+ if _image_element(style, master, element, states, BUTTON_STATES,
+ radius=RADIUS_SM, backdrop=backdrop):
+ style.layout(target, [(element, {'sticky': 'nsew', 'children': [
+ ('Button.focus', {'sticky': 'nsew', 'children': [
+ ('Button.padding', {'sticky': 'nsew', 'children': [
+ ('Button.label', {'sticky': 'nsew'})]})]})]})])
+
+
+def _rounded_fields(style, master):
+ """A text field is a rounded recess whose edge lights up on focus."""
+ order = ((('disabled',), 'disabled'), (('focus',), 'focus'))
+ plans = (('FreeVideo.field', 'TEntry', BACKGROUND, SURFACE),
+ ('FreeVideoWell.field', 'Well.TEntry', SURFACE, BACKGROUND))
+ for element, target, backdrop, fill in plans:
+ faces = dict(normal=(fill, BORDER), focus=(fill, ACCENT), disabled=(backdrop, BORDER))
+ if _image_element(style, master, element, faces, order,
+ radius=RADIUS_SM, backdrop=backdrop):
+ style.layout(target, [(element, {'sticky': 'nsew', 'children': [
+ ('Entry.padding', {'sticky': 'nsew', 'children': [
+ ('Entry.textarea', {'sticky': 'nsew'})]})]})])
+
+
+def _rounded_combobox(style, master):
+ """clam hangs the arrow outside the field, which reads as a second square
+ box beside a rounded one. Round the field and move the arrow inside it."""
+ element = 'FreeVideo.combofield'
+ faces = dict(normal=(SURFACE, BORDER), focus=(SURFACE, ACCENT),
+ active=(SURFACE, HOVER), disabled=(BACKGROUND, BORDER))
+ order = ((('disabled',), 'disabled'), (('focus',), 'focus'), (('hover',), 'active'))
+ if _image_element(style, master, element, faces, order, radius=RADIUS_SM, backdrop=BACKGROUND):
+ style.layout('TCombobox', [(element, {'sticky': 'nswe', 'children': [
+ ('Combobox.downarrow', {'side': 'right', 'sticky': 'ns'}),
+ ('Combobox.padding', {'sticky': 'nswe', 'children': [
+ ('Combobox.textarea', {'sticky': 'nswe'})]})]})])
+
+
+def _rounded_tabs(style, master):
+ """Notebook tabs read as one segmented control rather than filing tabs."""
+ element = 'FreeVideo.tab'
+ faces = dict(normal=(BACKGROUND, BACKGROUND), selected=(SURFACE, BORDER),
+ active=(RAISED, RAISED), disabled=(BACKGROUND, BACKGROUND))
+ order = ((('disabled',), 'disabled'), (('selected',), 'selected'), (('active',), 'active'))
+ if _image_element(style, master, element, faces, order, radius=RADIUS_SM, backdrop=BACKGROUND,
+ padding=(space(5), space(2) + 1)):
+ style.layout('TNotebook.Tab', [(element, {'sticky': 'nswe', 'children': [
+ ('Notebook.padding', {'side': 'top', 'sticky': 'nswe', 'children': [
+ ('Notebook.focus', {'side': 'top', 'sticky': 'nswe', 'children': [
+ ('Notebook.label', {'side': 'top', 'sticky': ''})]})]})]})])
+
+
+def _rounded_checkbutton(style, master):
+ """An accent box with a drawn check, instead of clam's flat square."""
+ import tkinter as tk
+ size, radius, gap = 17, 5, space(2)
+ # An image element ignores its own padding here, so the gap before the
+ # label is part of the picture: the strip is simply the window colour.
+ box = (0, 0, size, size)
+ tick = ((.26*size, .54*size), (.43*size, .72*size), (.76*size, .30*size))
+ draw = lambda fill, edge, check=None: rounded_photo(
+ master, size + gap, size, radius, fill, BACKGROUND, edge, check, rect=box)
+ states = dict(off=draw(BACKGROUND, BORDER), hover=draw(BACKGROUND, ACCENT),
+ on=draw(ACCENT, ACCENT, (BACKGROUND, tick)),
+ off_disabled=draw(BACKGROUND, DISABLED),
+ on_disabled=draw(ACCENT_DISABLED, ACCENT_DISABLED, (ACCENT_DISABLED_TEXT, tick)))
+ _retain(master, states.values())
+ try:
+ style.element_create('FreeVideo.checkindicator', 'image', states['off'],
+ ('disabled', 'selected', states['on_disabled']),
+ ('disabled', states['off_disabled']),
+ ('selected', states['on']),
+ ('active', states['hover']), sticky='')
+ except tk.TclError:
+ return
+ for target in ('TCheckbutton', 'TRadiobutton'):
+ style.layout(target, [(target.replace('T', '') + '.padding', {'sticky': 'nswe', 'children': [
+ ('FreeVideo.checkindicator', {'side': 'left', 'sticky': ''}),
+ (target.replace('T', '') + '.focus', {'side': 'left', 'sticky': 'w', 'children': [
+ (target.replace('T', '') + '.label', {'sticky': 'nswe'})]})]})])
+
+
+def theme(window):
+ import tkinter as tk
+ from tkinter import ttk
+ window.configure(bg=BACKGROUND)
+ window.freevideo_icon = tk.PhotoImage(master=window, file=str(Path(__file__).with_name('assets') / 'icon.png'))
+ window.iconphoto(True, window.freevideo_icon)
+ style = ttk.Style(window)
+ style.theme_use('clam')
+ style.configure('.', background=BACKGROUND, foreground=TEXT, font=(FAMILY, BODY),
+ bordercolor=BORDER, lightcolor=BORDER, darkcolor=BORDER, focuscolor=ACCENT)
+ style.configure('TFrame', background=BACKGROUND)
+ style.configure('TLabel', background=BACKGROUND)
+ style.configure('Title.TLabel', font=(FAMILY, HERO, 'bold'))
+ style.configure('Section.TLabel', font=(FAMILY, SECTION, 'bold'))
+ style.configure('Muted.TLabel', foreground=MUTED)
+ style.configure('Meta.TLabel', foreground=MUTED, font=(FAMILY, MICRO))
+ style.configure('Danger.TLabel', foreground=DANGER)
+ # Text placed on a card repeats the card's fill; clam draws no inheritance.
+ style.configure('Card.TFrame', background=SURFACE)
+ # A retained failure gets its own surface so it reads as the page's
+ # subject rather than as loose text under the progress card.
+ style.configure('Alert.Card.TFrame', background=DANGER_SUBTLE)
+ style.configure('Card.TLabel', background=SURFACE)
+ style.configure('Heading.Card.TLabel', background=SURFACE, font=(FAMILY, STRONG, 'bold'))
+ style.configure('Hero.Card.TLabel', background=SURFACE, font=(FAMILY, HERO, 'bold'))
+ style.configure('ProgressTitle.Card.TLabel', background=SURFACE, font=(FAMILY, SECTION, 'bold'))
+ style.configure('Muted.Card.TLabel', background=SURFACE, foreground=MUTED, font=(FAMILY, BODY))
+ style.configure('Meta.Card.TLabel', background=SURFACE, foreground=MUTED, font=(FAMILY, MICRO))
+ style.configure('Accent.Card.TLabel', background=SURFACE, foreground=ACCENT, font=(FAMILY, BODY, 'bold'))
+ style.configure('Stat.Card.TLabel', background=SURFACE, font=(FAMILY, SECTION, 'bold'))
+ # Retained for the tabbed engine panel's progress cards.
+ style.configure('Progress.TLabel', background=SURFACE, font=(FAMILY, BODY, 'bold'))
+ style.configure('ProgressMeta.TLabel', background=SURFACE, foreground=MUTED, font=(FAMILY, MICRO))
+ style.configure('TButton', padding=(space(4), space(2) + 1), background=RAISED, borderwidth=1, relief='flat')
+ style.map('TButton', background=[('disabled', SURFACE), ('active', HOVER)],
+ foreground=[('disabled', DISABLED), ('active', TEXT)])
+ style.configure('Quiet.TButton', background=BACKGROUND, borderwidth=0, padding=(space(2), space(2)))
+ style.map('Quiet.TButton', background=[('disabled', BACKGROUND), ('active', RAISED)],
+ foreground=[('disabled', DISABLED), ('active', TEXT)])
+ # A link carries no border or fill, so only its colour reacts.
+ style.configure('Link.TButton', background=BACKGROUND, foreground=ACCENT, borderwidth=0,
+ padding=(0, space(1)), font=(FAMILY, BODY))
+ style.map('Link.TButton', background=[('active', BACKGROUND), ('disabled', BACKGROUND)],
+ foreground=[('active', ACCENT_HOVER), ('disabled', DISABLED)])
+ style.configure('Primary.TButton', padding=(space(5), space(3)), foreground=BACKGROUND, background=ACCENT,
+ bordercolor=ACCENT, lightcolor=ACCENT, darkcolor=ACCENT)
+ style.map('Primary.TButton', background=[('disabled', ACCENT_DISABLED), ('active', ACCENT_HOVER)],
+ foreground=[('disabled', ACCENT_DISABLED_TEXT), ('active', BACKGROUND)])
+ style.configure('TEntry', padding=space(2), fieldbackground=SURFACE, insertcolor=TEXT)
+ style.map('TEntry', bordercolor=[('focus', ACCENT)], lightcolor=[('focus', ACCENT)], darkcolor=[('focus', ACCENT)])
+ # An input that sits on a card reads as a recess, so it goes a step darker
+ # than the card rather than matching it and losing its own edge.
+ style.configure('Well.TEntry', padding=space(2), fieldbackground=BACKGROUND, insertcolor=TEXT)
+ style.map('Well.TEntry', fieldbackground=[('disabled', SURFACE)], bordercolor=[('focus', ACCENT)],
+ lightcolor=[('focus', ACCENT)], darkcolor=[('focus', ACCENT)])
+ # clam gives the arrow its own bordered box. Blend it into the field so the
+ # control reads as one rounded shape.
+ style.configure('TCombobox', padding=space(2) - 2, fieldbackground=SURFACE, arrowcolor=MUTED,
+ background=SURFACE, bordercolor=SURFACE, lightcolor=SURFACE, darkcolor=SURFACE,
+ relief='flat', arrowsize=space(3) + 1)
+ style.map('TCombobox', fieldbackground=[('readonly', SURFACE)], foreground=[('readonly', TEXT)],
+ background=[('readonly', SURFACE), ('disabled', BACKGROUND), ('active', SURFACE)],
+ bordercolor=[('readonly', SURFACE), ('disabled', BACKGROUND), ('active', SURFACE)],
+ lightcolor=[('readonly', SURFACE), ('disabled', BACKGROUND), ('active', SURFACE)],
+ darkcolor=[('readonly', SURFACE), ('disabled', BACKGROUND), ('active', SURFACE)],
+ arrowcolor=[('disabled', DISABLED), ('active', ACCENT), ('!disabled', MUTED)])
+ for name in ('TCheckbutton', 'TRadiobutton'):
+ style.configure(name, background=BACKGROUND, indicatorbackground=RAISED, indicatorforeground=ACCENT)
+ style.map(name, background=[('active', BACKGROUND)], foreground=[('disabled', DISABLED)],
+ indicatorbackground=[('selected', ACCENT), ('active', BORDER)])
+ style.configure('TNotebook', borderwidth=0)
+ style.configure('TNotebook.Tab', padding=(space(5) + 2, space(3)), background=BACKGROUND)
+ style.map('TNotebook.Tab', background=[('selected', SURFACE)], foreground=[('selected', ACCENT)])
+ for name in ('Horizontal.TProgressbar', 'FreeVideo.Horizontal.TProgressbar'):
+ style.configure(name, background=ACCENT, troughcolor=RAISED, borderwidth=0,
+ lightcolor=ACCENT, darkcolor=ACCENT, thickness=6)
+ style.configure('Vertical.TScrollbar', background=RAISED, troughcolor=BACKGROUND,
+ arrowcolor=MUTED, borderwidth=0)
+ _rounded_buttons(style, window)
+ _rounded_fields(style, window)
+ _rounded_combobox(style, window)
+ _rounded_tabs(style, window)
+ _rounded_checkbutton(style, window)
+ # A thumb without stepper arrows; the wizard scrolls with the wheel.
+ style.layout('Wizard.Vertical.TScrollbar', [('Vertical.Scrollbar.trough', {'sticky': 'ns', 'children': [
+ ('Vertical.Scrollbar.thumb', {'expand': '1', 'sticky': 'nswe'})]})])
+ style.configure('Wizard.Vertical.TScrollbar', width=space(2) - 1, background=RAISED, troughcolor=BACKGROUND,
+ borderwidth=0, relief='flat', lightcolor=RAISED, darkcolor=RAISED, bordercolor=BACKGROUND)
+ style.map('Wizard.Vertical.TScrollbar', background=[('active', BORDER), ('!active', RAISED)])
+ return style
+
+
+def wordmark(parent, width=228):
+ import tkinter as tk
+ from tkinter import ttk
+ # This PNG is a rasterization of web/assets/freevideo.svg. Tk reads PNG
+ # directly, including in the standalone EXE; Pillow/Torch are not imported.
+ original = tk.PhotoImage(master=parent, file=str(Path(__file__).with_name('assets') / 'wordmark.png'))
+ ratio = max(1, round(original.width() / width))
+ photo = original.subsample(ratio)
+ label = ttk.Label(parent, image=photo, text='FreeVideo')
+ label.image = photo # Tk does not retain the Python image object.
+ return label
diff --git a/freevideo_engine/calibration.py b/freevideo_engine/calibration.py
new file mode 100644
index 0000000..d3e3d41
--- /dev/null
+++ b/freevideo_engine/calibration.py
@@ -0,0 +1,398 @@
+"""Measure the placement questions this machine can settle, and report them.
+
+Some placement choices are gated to one platform because the measurement
+behind them is from one platform. The engine cannot guess the others: on
+Windows PyTorch has no expandable segments, so the same allocations can need
+more *reserved* memory, and the desktop compositor takes VRAM while a request
+runs. So the gates stay closed until a machine of that kind reports back.
+
+This runs the pair that would open a gate. It plans for the live card with no
+simulated cap, keeps everything identical except the named fields, and records
+what each variant reserved and how long its steps took. Nothing is decided
+here; the report is the evidence for deciding.
+
+Only questions that apply to this machine's own plan run, so a machine whose
+plan has no open question does nothing and says so.
+"""
+import argparse
+import copy
+import hashlib
+import json
+import os
+from pathlib import Path
+import statistics
+import subprocess
+import sys
+import time
+import traceback
+import uuid
+
+from .hardware import detect
+from .geometry import geometry
+from .kernel_capabilities import available_backends
+from .policy import choose
+from .monitoring import save
+from .system import windows
+from . import processes
+
+# Each question names the fields it changes and why the answer is not already
+# known. `applies` reads the plan this machine would run, so a question only
+# costs time on a machine that can answer it.
+# Every placement question worth a consumer machine's time, in the order the
+# answers matter. Each names one field to change against the machine's own
+# plan, so a question costs one run rather than a pair, and a question that
+# does not apply to this plan costs nothing.
+QUESTIONS = (
+ # No residual_staging question. It asked whether staging lowers the
+ # whole-device peak or only costs time, and the RTX PRO 6000 answered it:
+ # 0.95 GiB of peak, and the unstaged configuration dies in the first
+ # sampling step. Asking a user's card again would spend half an hour
+ # reproducing an OOM, and on Windows the unstaged run can spill silently
+ # and look merely slow -- which is the reading that turned staging off in
+ # the first place. benchmarks/pro6000-verification/results-pro6000.md.
+ dict(name='attention_outputs', flip='attention_cpu_outputs',
+ applies=lambda engine: 'attention_cpu_outputs' in engine,
+ asks='What the full-length attention output buffer costs on the card instead of '
+ 'in host memory.',
+ decides='Four Linux corners put it at 0.04 to 0.23 GiB of peak for 19 to 36% of '
+ 'sampling time. If this platform agrees, the band can buy it back with '
+ 'residency, which measured under 1%.'),
+ dict(name='ff_recompute', flip='fp8_ff_recompute',
+ applies=lambda engine: 'fp8_ff_recompute' in engine,
+ asks='Whether recomputing the feed-forward intermediates is worth the compute it '
+ 'costs on a card this size.',
+ decides='One RTX 4080 Laptop measured recompute off as 1.071x faster with a lower '
+ 'reserved peak -- the opposite of what a datacentre card prefers, where '
+ 'compute is nearly free. One machine is not a rule.'),
+ dict(name='pinned_host_cache', override=dict(pin_host_gb=0.),
+ applies=lambda engine: (engine.get('pin_host_gb') or 0) > 0,
+ asks='Whether the pinned host weight cache earns its locked pages.',
+ decides='The same 4080 record measured 12.75 GB pinned as slower than none. '
+ 'Locked pages are scarcer on a desktop than on a server.'),
+ dict(name='window_batch', override=dict(window_batch=1),
+ applies=lambda engine: (engine.get('window_batch') or 1) > 1,
+ asks='What batching the attention windows costs in peak and buys in time.',
+ decides='Ampere already runs one. Elsewhere four is planned on a Linux '
+ 'measurement, and in this band it changed no peak at three budgets while '
+ 'costing up to 18%, which would make one the better default here too.'),
+ dict(name='resident_blocks', override=dict(resident_blocks=0),
+ applies=lambda engine: (engine.get('resident_blocks') or 0) > 0,
+ asks='Whether blocks held on the card buy any time on this platform.',
+ decides='Linux measured residency at under 1%, and the 4080 record measured five '
+ 'resident blocks as faster than nine. If it buys nothing here either, '
+ 'that VRAM is better spent on the activation path.'),
+)
+
+
+def host_peak_bytes():
+ """This process's own high-water host memory, or None where unreadable.
+
+ Two of the questions -- the pinned host cache and residency -- are trades
+ against host memory, so a report that only carries device peaks answers
+ half of each. Linux keeps a true high-water mark in VmHWM; Windows keeps a
+ peak working set, and both are read rather than sampled so a peak between
+ samples cannot be missed.
+ """
+ try:
+ if os.name == 'nt':
+ from .win32 import process_memory
+ row = process_memory(os.getpid())
+ return int(row['peak_rss_bytes']) if row and row.get('peak_rss_bytes') else (
+ int(row['rss_bytes']) if row else None)
+ for line in Path('/proc/self/status').read_text(encoding='utf-8').splitlines():
+ if line.startswith('VmHWM:'):
+ return int(line.split()[1]) * 1024
+ except (OSError, ValueError, KeyError, ImportError, AttributeError):
+ return None
+ return None
+
+
+def override_for(question, engine):
+ """What this question changes, given the plan it is changing.
+
+ A `flip` question reads the plan's own value and inverts it, so one entry
+ serves a machine that turns the field on and one that turns it off. Without
+ that, a card whose plan already declines the host attention buffer had
+ nothing to say about what the buffer costs.
+ """
+ if 'flip' in question:
+ field = question['flip']
+ return {field: not bool(engine.get(field))}
+ return dict(question['override'])
+
+
+def open_questions(engine):
+ """Every question this plan can answer, in the order their answers matter."""
+ return [question for question in QUESTIONS if question['applies'](engine)]
+
+
+def plan_variants(hardware, canvas, available, **caps):
+ """This machine's plan, and one run per open question plus a repeated baseline.
+
+ Every question compares against the same baseline, so N questions cost N+1
+ runs rather than 2N. The baseline runs first and again last: a sweep can
+ take hours, and a machine whose thermals or desktop load drift would
+ otherwise hand its later questions a quietly different comparison.
+ """
+ policy = choose(hardware, available_backends=available, canvas=canvas, **caps)
+ questions = open_questions(policy.engine)
+ if not questions:
+ return policy, []
+ baseline = dict(question='baseline', variant='as_planned', overrides={},
+ engine=copy.deepcopy(policy.engine))
+ rows = [baseline]
+ for question in questions:
+ override = override_for(question, policy.engine)
+ rows.append(dict(question=question['name'], variant='changed', overrides=override,
+ engine=dict(copy.deepcopy(policy.engine), **override)))
+ rows.append(dict(baseline, variant='as_planned_again'))
+ return policy, rows
+
+
+def _sequence(rows, repeats):
+ """The sweep, repeated, with the order reversed on alternate passes.
+
+ The sweep already carries its own baseline at both ends, so a repeat is
+ another whole pass rather than an interleaving; reversing it means no
+ question is always measured on a warmer or busier machine than another.
+ """
+ order = []
+ for index in range(repeats):
+ order.extend(rows if index % 2 == 0 else list(reversed(rows)))
+ return order
+
+
+def run(cache, conditioning, policy, rows, out, *, seed, repeats, warmup_steps, canvas):
+ import torch
+ from .runtime import Engine
+ from .locking import runtime_lock
+ from .monitoring import Monitor
+ report = dict(calibration_id=uuid.uuid4().hex, schema_version=1)
+ results = []
+ monitor = Monitor(Path(out) / 'gpu.csv').start()
+ try:
+ with runtime_lock():
+ for attempt, row in enumerate(_sequence(rows, repeats)):
+ engine = None
+ torch.cuda.reset_peak_memory_stats()
+ monitor.peak = monitor.sample()[0]
+ record = dict(question=row['question'], variant=row['variant'],
+ overrides=row['overrides'], attempt=attempt)
+ started = time.perf_counter()
+ try:
+ engine = Engine(cache, **dict(row['engine'], canvas=canvas))
+ record['load_seconds'] = round(engine.load_seconds, 3)
+ for _ in range(warmup_steps):
+ engine.sample(conditioning, seed)
+ torch.cuda.reset_peak_memory_stats()
+ video, audio, metrics = engine.sample(conditioning, seed)
+ finite = bool(torch.isfinite(video).all() and torch.isfinite(audio).all())
+ del video, audio
+ steps = [round(float(value), 4) for value in metrics['step_seconds']]
+ record.update(status='complete', finite_latents=finite,
+ sample_seconds=round(metrics['sample_seconds'], 3),
+ step_seconds=steps,
+ steady_seconds_per_step=(round(statistics.median(steps[2:]), 4)
+ if len(steps) > 2 else None),
+ residual_staged_steps=len(metrics.get('residual_offload_steps') or []),
+ torch_peak_reserved_bytes=int(torch.cuda.max_memory_reserved()),
+ torch_peak_allocated_bytes=int(torch.cuda.max_memory_allocated()),
+ whole_gpu_peak_bytes=int(monitor.peak),
+ host_peak_bytes=host_peak_bytes(),
+ actual_config={key: dict(engine.config).get(key) for key in row['engine']})
+ except Exception as error:
+ record.update(status='error', error='%s: %s' % (type(error).__name__, error))
+ finally:
+ if engine is not None:
+ engine.close()
+ record['elapsed_seconds'] = round(time.perf_counter() - started, 3)
+ results.append(record)
+ save(Path(out) / 'calibration.json', dict(report, results=results))
+ finally:
+ report['monitor'] = monitor.stop()
+ report.update(results=results, budget_bytes=policy.gpu_budget_bytes,
+ gpu_reserve_bytes=policy.gpu_system_reserve_bytes)
+ return report
+
+
+def summarise(report):
+ """Each question against the shared baseline, and how far the baseline moved.
+
+ The comparison is only worth as much as the baseline is stable, so the
+ drift between the sweep's first and last baseline runs is reported beside
+ the answers rather than left for a reader to notice.
+ """
+ complete = [row for row in report['results'] if row.get('status') == 'complete']
+ baselines = [row for row in complete if row['question'] == 'baseline']
+ def steady(rows):
+ values = [row['steady_seconds_per_step'] for row in rows if row.get('steady_seconds_per_step')]
+ return round(statistics.median(values), 4) if values else None
+ def peak(rows, key):
+ values = [row[key] for row in rows if row.get(key)]
+ return max(values) if values else None
+ base_steady = steady(baselines)
+ summary = dict(baseline=dict(runs=len(baselines), steady_seconds_per_step=base_steady,
+ torch_peak_reserved_bytes=peak(baselines, 'torch_peak_reserved_bytes'),
+ whole_gpu_peak_bytes=peak(baselines, 'whole_gpu_peak_bytes'),
+ host_peak_bytes=peak(baselines, 'host_peak_bytes')))
+ first_last = [steady([row]) for row in baselines if steady([row])]
+ summary['baseline']['drift'] = (round(max(first_last) / min(first_last) - 1, 4)
+ if len(first_last) > 1 else None)
+ questions = {}
+ for question in [row['question'] for row in complete if row['question'] != 'baseline']:
+ if question in questions:
+ continue
+ rows = [row for row in complete if row['question'] == question]
+ value, reserved = steady(rows), peak(rows, 'torch_peak_reserved_bytes')
+ whole = peak(rows, 'whole_gpu_peak_bytes')
+ base_reserved = summary['baseline']['torch_peak_reserved_bytes']
+ base_whole = summary['baseline']['whole_gpu_peak_bytes']
+ host = peak(rows, 'host_peak_bytes')
+ base_host = summary['baseline']['host_peak_bytes']
+ questions[question] = dict(
+ runs=len(rows), overrides=rows[0].get('overrides') or {},
+ steady_seconds_per_step=value, torch_peak_reserved_bytes=reserved,
+ whole_gpu_peak_bytes=whole, host_peak_bytes=host,
+ host_delta_bytes=(host - base_host if host and base_host else None),
+ faster_than_baseline=(round(base_steady / value, 4)
+ if value and base_steady else None),
+ reserved_delta_bytes=(reserved - base_reserved
+ if reserved and base_reserved else None),
+ whole_gpu_delta_bytes=(whole - base_whole if whole and base_whole else None),
+ over_budget=bool(whole and whole > report['budget_bytes']))
+ failed = [dict(question=row['question'], variant=row['variant'], error=row.get('error'))
+ for row in report['results'] if row.get('status') != 'complete']
+ return dict(summary, questions=questions, failed=failed)
+
+
+def main(argv=None):
+ parser = argparse.ArgumentParser(
+ prog='freevideo calibrate',
+ description='Measure the placement questions this machine can settle, and report them')
+ parser.add_argument('--cache', type=Path, required=True)
+ source = parser.add_mutually_exclusive_group(required=True)
+ source.add_argument('--conditioning', type=Path,
+ help='Preencoded conditioning, so text encoding is not in the comparison')
+ source.add_argument('--prompt-file', type=Path,
+ help='Encode this prompt once first, then measure with it')
+ for name in ('--encoder-python', '--encoder-root', '--model-paths', '--encoder'):
+ parser.add_argument(name)
+ parser.add_argument('--out', type=Path, required=True)
+ parser.add_argument('--repeats', type=int, default=2)
+ parser.add_argument('--warmup-steps', type=int, default=1)
+ parser.add_argument('--frames', type=int, default=243)
+ parser.add_argument('--width', type=int, default=1344)
+ parser.add_argument('--height', type=int, default=768)
+ parser.add_argument('--seed', type=int, default=2026090901)
+ args = parser.parse_args(argv)
+ if args.repeats < 1 or not 0 <= args.warmup_steps <= 2:
+ parser.error('Use at least one repeat and at most two warmup steps')
+ canvas = geometry(args.width, args.height, frames=args.frames)
+ # One directory for the whole run, made once. The encode below writes
+ # into it, so a second exclusive mkdir after it raised FileExistsError
+ # on every run that encodes its own prompt, which is every run the
+ # panel starts. Reusing a directory is deliberate: the encode above is
+ # skipped when its output is already there, so a retry keeps it.
+ args.out.mkdir(parents=True, exist_ok=True)
+ hardware = detect()
+
+ def deliver(report):
+ """Keep measurements and failures in the local report."""
+ from .diagnostics import Redactor
+ cleaned = Redactor([(str(args.out), '<RUN>')]).data(json.dumps(report).encode('utf-8'), '.json')
+ save(args.out / 'calibration.json', json.loads(cleaned))
+
+ if args.prompt_file is not None:
+ # One encode, before any measurement, so every variant reads one input
+ # and none of them pays for text encoding. A separate process because
+ # the encoder is a separate runtime, exactly as generation runs it.
+ args.conditioning = args.out / 'conditioning.pt'
+ command = [sys.executable, '-B', '-m', 'freevideo_engine', 'encode',
+ '--prompt-file', str(args.prompt_file), '--out', str(args.conditioning)]
+ for name in ('encoder_python', 'encoder_root', 'model_paths', 'encoder'):
+ value = getattr(args, name, None)
+ if value:
+ command += ['--' + name.replace('_', '-'), str(value)]
+ log = args.out / 'encode.log'
+ print(json.dumps({'event': 'calibration_encode', 'out': str(args.conditioning),
+ 'log': str(log)}), flush=True)
+ if not args.conditioning.exists():
+ # Hand the child this lease instead of hiding it from it. The
+ # managed CLI takes the machine lease exclusively and holds it for
+ # the whole command, so a child that opens the lock file itself
+ # blocks on its own parent: the first Windows placement test died
+ # in msvcrt.locking as 'Another process holds the installation
+ # lease' with nothing else running, and no restart could clear it.
+ # Removing the two variables, which is what stopped the EBADF
+ # before that, was the wrong half of the fix -- a Windows handle
+ # number is only valid in the process it was made inheritable for,
+ # so the handle has to be duplicated into the child, which popen
+ # does for pass_fds.
+ lease = processes.descriptors()
+ environment = dict(os.environ)
+ if windows() and lease:
+ # popen gives the child its own handle; a descriptor number
+ # left beside it belongs to this process and only reproduces
+ # the EBADF above.
+ environment.pop('FREEVIDEO_RUNTIME_LOCK_FD', None)
+ with log.open('wb') as stream:
+ with processes.popen(command, env=environment, pass_fds=lease,
+ cwd=str(Path(__file__).resolve().parent.parent),
+ stdin=subprocess.DEVNULL, stdout=stream,
+ stderr=subprocess.STDOUT) as child:
+ returncode = child.wait()
+ if returncode:
+ # Print it and report it. This failure is upstream of every
+ # measurement, so the report carries no results; its value is
+ # the engine frames a reader can parse out of the child's
+ # traceback, which is how this bug was found -- by hand, from a
+ # console the machine's owner had to copy out.
+ output = log.read_text(encoding='utf-8', errors='replace')
+ print(output[-8000:], flush=True)
+ deliver(dict(schema_version=1, calibration_id=uuid.uuid4().hex,
+ hardware=hardware.to_dict(), geometry=dict(canvas),
+ status='error', results=[], traceback=output[-4000:],
+ error='encode: freevideo encode exited with code %d' % returncode))
+ # Not parser.error: a usage dump buries the child's traceback,
+ # which is the only thing here that says what went wrong.
+ raise SystemExit(json.dumps({
+ 'event': 'calibration_failed', 'stage': 'encode',
+ 'returncode': returncode, 'log': str(log),
+ 'if_lease_held': 'A held installation lease means another FreeVideo '
+ 'process is using the card. Finish or cancel it, then '
+ 'run the placement test again.'}))
+ policy, rows = plan_variants(hardware, canvas, available_backends(hardware))
+ if not rows:
+ result = dict(schema_version=1, questions=[], hardware=hardware.to_dict(),
+ note='This machine\'s plan has no open placement question; nothing to measure.')
+ save(args.out / 'calibration.json', result)
+ print(json.dumps(result, indent=1))
+ return 0
+ os.environ['PYTORCH_ALLOC_CONF'] = policy.allocator_config
+ os.environ['PYTORCH_CUDA_ALLOC_CONF'] = policy.allocator_config
+ context = dict(schema_version=1, calibration_id=uuid.uuid4().hex,
+ hardware=hardware.to_dict(), geometry=dict(canvas), seed=args.seed,
+ plan=dict(policy.engine), budget_bytes=policy.gpu_budget_bytes,
+ gpu_reserve_bytes=policy.gpu_system_reserve_bytes,
+ questions=[dict(name=q['name'], asks=q['asks'], decides=q['decides'])
+ for q in open_questions(policy.engine)])
+ try:
+ report = run(args.cache, args.conditioning, policy, rows, args.out,
+ seed=args.seed, repeats=args.repeats,
+ warmup_steps=args.warmup_steps, canvas=canvas)
+ except BaseException as error:
+ deliver(dict(context, status='error', results=[],
+ error='%s: %s' % (type(error).__name__, error),
+ traceback=traceback.format_exc()[-4000:]))
+ raise
+ report.update(
+ context, status='complete',
+ conditioning_sha256=hashlib.sha256(args.conditioning.read_bytes()).hexdigest(),
+ summary=summarise(report),
+ scope=('Live card with no simulated cap. Identical model, conditioning, seed and '
+ 'geometry; only the named fields differ. Sampling only, so loading and '
+ 'decode are excluded.'))
+ print(json.dumps(dict(calibration_id=report['calibration_id'],
+ summary=report['summary']), indent=1))
+ deliver(report)
+ return 0
diff --git a/freevideo_engine/capacity.py b/freevideo_engine/capacity.py
new file mode 100644
index 0000000..e85d2a7
--- /dev/null
+++ b/freevideo_engine/capacity.py
@@ -0,0 +1,389 @@
+#!/usr/bin/env python3
+"""Measure an isolated GPU experiment natively or with optional enforced limits."""
+import argparse
+import csv
+import datetime
+import hashlib
+import json
+import math
+import os
+from pathlib import Path
+import signal
+import subprocess
+import sys
+import tempfile
+import time
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+from freevideo_engine.monitoring import Monitor, save
+from freevideo_engine.locking import runtime_lock, device_lock_path, LOCK_ENV
+
+
+# A harness stop is a known controller stop. Left pending, the attempt looks
+# like an abandoned GPU crash to the next request on that device, which raises
+# a compatibility incident and caps every later request there to the smallest
+# work groups. optimize.py settles its own stops for exactly this reason.
+CONTROLLED_STOPS = ('whole_gpu_budget_exceeded', 'timeout', 'ram_cgroup_oom',
+ 'monitor_failed', 'harness_error')
+
+
+def settle_controlled_stop(reason, since, device):
+ """Settle attempts this run left pending, so they are not read as crashes."""
+ try:
+ from freevideo_engine.paths import data_root
+ from freevideo_engine.resource_history import ResourceHistory
+ path = data_root() / 'resource-history.sqlite3'
+ if not path.is_file():
+ return []
+ history = ResourceHistory(path)
+ settled = []
+ for attempt in history.attempts():
+ # The device lease is held exclusively, so a pending attempt
+ # started after this run began belongs to this run.
+ if attempt.get('outcome') != 'pending' or (attempt.get('started') or 0) < since:
+ continue
+ settled.append(history.finish(attempt['id'], 'cancelled', details=dict(
+ reason='Capacity harness stopped this attempt: ' + str(reason),
+ controlled_worker_stop=True, capacity_device=str(device))))
+ return settled
+ except Exception as error:
+ # This runs while the measurement is being written out; a settlement
+ # problem is recorded, never allowed to replace the result.
+ return [dict(error='%s: %s' % (type(error).__name__, error))]
+
+
+def mps_client_device(pipe, log, device):
+ """Which GPU an MPS client of this daemon actually gets.
+
+ An MPS client does not choose its device: CUDA_VISIBLE_DEVICES applies to
+ the control daemon, and clients receive the device set the daemon was
+ started with. Attaching several measurements for different devices to one
+ shared daemon therefore sends all of them to the same GPU, silently. Ask a
+ throwaway client which device it received and compare.
+ """
+ code = ('import torch;'
+ 'p = torch.cuda.get_device_properties(torch.cuda.current_device());'
+ 'u = str(getattr(p, "uuid", ""));'
+ 'print(u if u.startswith(("GPU-", "MIG-")) else "GPU-" + u)')
+ environment = dict(os.environ, CUDA_VISIBLE_DEVICES=str(device),
+ CUDA_MPS_PIPE_DIRECTORY=str(pipe), CUDA_MPS_LOG_DIRECTORY=str(log))
+ result = subprocess.run([sys.executable, '-c', code], env=environment,
+ capture_output=True, text=True, timeout=180)
+ if result.returncode:
+ raise RuntimeError('An MPS client of ' + str(pipe) + ' could not start: '
+ + (result.stderr or '')[-400:])
+ return result.stdout.strip().splitlines()[-1]
+
+def stop_process(proc):
+ if proc.poll() is None:
+ os.killpg(proc.pid, signal.SIGTERM)
+ try:
+ proc.wait(timeout=15)
+ except subprocess.TimeoutExpired:
+ os.killpg(proc.pid, signal.SIGKILL)
+ proc.wait(timeout=15)
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--budget-gb', type=float, required=True)
+ parser.add_argument('--out', type=Path, required=True)
+ parser.add_argument('--timeout', type=float, default=10800)
+ parser.add_argument('--allocator-config')
+ parser.add_argument('--ram-gb', type=float, help='Require a dedicated cgroup hard physical RAM limit (zero swap by default)')
+ parser.add_argument('--swap-gb', type=float, default=0, help='Explicit separate swap allowance in decimal GB; requires --ram-gb and a preconfigured bounded cgroup')
+ parser.add_argument('--no-mps', action='store_true', help='Use native CUDA; monitor the whole GPU without imposing an MPS client limit')
+ parser.add_argument('--mps-pipe', type=Path,
+ help='Attach to an MPS control daemon started outside this run. One daemon '
+ 'per user per host serves every device, so measuring several devices at '
+ 'once requires sharing it; the per-client memory limit stays per device.')
+ parser.add_argument('command', nargs=argparse.REMAINDER)
+ args = parser.parse_args()
+ if os.name == 'nt':
+ parser.error('The MPS/cgroup capacity harness requires Linux. On Windows use test or bench --mode full for observed memory.')
+ command = args.command[1:] if args.command[:1] == ['--'] else args.command
+ if not command or args.budget_gb < 2:
+ parser.error('A command and at least 2 GB are required')
+ if not math.isfinite(args.swap_gb) or args.swap_gb < 0 or (args.swap_gb and args.ram_gb is None):
+ parser.error('--swap-gb must be finite and nonnegative, and requires --ram-gb')
+ if args.mps_pipe is not None and args.no_mps:
+ parser.error('--mps-pipe attaches to a daemon; it cannot be combined with --no-mps')
+ if os.environ.get(LOCK_ENV):
+ # A caller already granted a lease; reuse it exactly as before.
+ with runtime_lock() as descriptor:
+ return measure(args, command, descriptor)
+ # Otherwise own the leases directly. The device is chosen the same way the
+ # NVML monitor chooses it, before any CUDA context exists. Hold the machine
+ # lease shared, so setup and whole-host tests still exclude us, and this
+ # device exclusively, so two measurements never share a GPU while the
+ # other devices on the host stay usable.
+ device = os.environ.get('CUDA_VISIBLE_DEVICES', '0').split(',')[0]
+ lease = device_lock_path(device)
+ with runtime_lock(shared=True, inherit=False), \
+ runtime_lock(lease, inherit=False) as descriptor:
+ # The worker validates its inherited descriptor against this path.
+ os.environ['FREEVIDEO_LOCK_PATH'] = str(lease)
+ return measure(args, command, descriptor)
+
+
+def measure(args, command, descriptor):
+ root = args.out.resolve()
+ root.mkdir(parents=True, exist_ok=False)
+ sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+ from freevideo_engine.ram import CgroupMemory, ProcessMemory
+ observation = ProcessMemory()
+ ram = None
+ if args.ram_gb is not None:
+ if Path('/proc/self/cgroup').read_text(encoding='utf-8').strip() != '0::/':
+ raise RuntimeError('RAM harness requires a dedicated cgroup namespace/container')
+ ram = CgroupMemory(int(args.ram_gb * 1e9), swap_budget_bytes=int(getattr(args, 'swap_gb', 0) * 1e9))
+ # Resolved before the occupancy check, which tolerates this daemon's server.
+ shared_pipe = args.mps_pipe.resolve() if args.mps_pipe is not None else None
+ if shared_pipe is not None and not shared_pipe.is_dir():
+ raise RuntimeError('MPS pipe directory does not exist: ' + str(shared_pipe))
+ monitor = Monitor(root / 'gpu.csv')
+ # Occupancy is scoped to the device being measured. A machine-wide check
+ # makes this harness unusable on any multi-GPU host, including one
+ # measuring several independent budgets on separate devices at once.
+ identity = str(monitor.device)
+ if not identity.startswith('GPU-'):
+ identity = subprocess.check_output(
+ ['nvidia-smi', '--query-gpu=uuid', '--format=csv,noheader', '-i', identity],
+ text=True).strip()
+ listed = subprocess.check_output(
+ ['nvidia-smi', '--query-compute-apps=pid,process_name,used_gpu_memory,gpu_uuid',
+ '--format=csv,noheader,nounits'], text=True)
+ rows = [[field.strip() for field in row.split(',')] for row in listed.splitlines() if row.strip()]
+ # Without a usable UUID fall back to the whole host, rather than admit a
+ # measurement that another process could be perturbing unseen.
+ scoped = identity.startswith('GPU-')
+ here = [row for row in rows if row[-1] == identity] if scoped else rows
+ # The caller's own MPS server is expected when several devices share one
+ # control daemon; it reserves device memory before our client starts.
+ # Anything else on this device is still another workload and still refuses.
+ ours, foreign, reserved = [], [], 0
+ for row in here:
+ if shared_pipe is not None and Path(row[1]).name == 'nvidia-cuda-mps-server':
+ ours.append(row)
+ try:
+ reserved += int(float(row[2])) * 2**20
+ except (ValueError, IndexError):
+ pass
+ else:
+ foreign.append(row)
+ # NVML v1 includes driver-reserved memory even when nvidia-smi has no clients.
+ observed = max(0, monitor.sample()[0] - reserved)
+ if foreign or observed > 1024 * 2**20:
+ raise RuntimeError('Device %s is occupied; stop other GPU work on it before measuring'
+ % (identity if scoped else monitor.device))
+ plan_identity = identity if scoped else None
+ plan_scope = 'device' if scoped else 'host'
+ if shared_pipe is not None and scoped:
+ received = mps_client_device(shared_pipe, root / 'mps', monitor.device)
+ if received != identity:
+ raise RuntimeError(
+ 'The shared MPS daemon at %s places clients on %s, not the requested %s. '
+ 'An MPS client cannot choose its device; the daemon decides. Start one daemon '
+ 'per device set, or measure this device with --no-mps.'
+ % (shared_pipe, received, identity))
+ plan_tolerated = dict(mps_server_processes=len(ours), mps_server_reserved_bytes=reserved,
+ observed_before_start_bytes=observed)
+ budget = int(args.budget_gb * 1_000_000_000)
+ # Leave room for server/driver allocations outside a client's accounting.
+ # One GiB covers a daemon serving a single device. A daemon shared across
+ # devices costs more per device: an 8 GiB client under a shared daemon
+ # reached a 9.46 GiB whole-device peak against a 9.00 GiB budget on an
+ # H200, so reserve two GiB there instead of aborting on the server's own
+ # growth. The client limit, not this reserve, is what bounds the engine.
+ server_reserve = (2048 if shared_pipe is not None else 1024) * 2**20
+ client_mib = (budget - server_reserve) // 2**20
+ # PIDs can repeat across containers and after restarts. Never reuse an old
+ # daemon's socket directory; native CUDA needs no MPS directory at all.
+ # A shared daemon is supplied by the caller, which also owns its lifetime.
+ pipe = shared_pipe or (Path(tempfile.mkdtemp(prefix='freevideo-mps-')) if not args.no_mps else None)
+ (root / 'mps').mkdir()
+ env = dict(os.environ,
+ CUDA_VISIBLE_DEVICES=monitor.device,
+ CUDA_MPS_PIPE_DIRECTORY=str(pipe),
+ CUDA_MPS_LOG_DIRECTORY=str(root / 'mps'),
+ CUDA_MPS_PINNED_DEVICE_MEM_LIMIT=f'0={client_mib}M',
+ HF_HUB_OFFLINE='1', TRANSFORMERS_OFFLINE='1',
+ PYTHONUNBUFFERED='1')
+ env[LOCK_ENV] = str(descriptor)
+ if args.no_mps:
+ for key in ('CUDA_MPS_PIPE_DIRECTORY', 'CUDA_MPS_LOG_DIRECTORY', 'CUDA_MPS_PINNED_DEVICE_MEM_LIMIT'):
+ env.pop(key, None)
+ if args.allocator_config is not None:
+ env['PYTORCH_ALLOC_CONF'] = args.allocator_config
+ env['PYTORCH_CUDA_ALLOC_CONF'] = args.allocator_config
+ plan = {'started_at': datetime.datetime.now(datetime.timezone.utc).isoformat(),
+ 'selected_gpu': monitor.device, 'selected_gpu_uuid': plan_identity,
+ 'occupancy_check_scope': plan_scope, 'occupancy_tolerated': plan_tolerated,
+ 'mps_pipe_directory': str(pipe) if pipe else None,
+ 'budget_bytes': budget, 'budget_unit': 'decimal GB',
+ 'mps_client_limit_mib': None if args.no_mps else client_mib, 'command': command,
+ 'mps_server_reserve_bytes': None if args.no_mps else server_reserve,
+ 'mps_daemon': 'none' if args.no_mps else 'shared' if shared_pipe else 'own',
+ 'gpu_limit_mode': 'NVML monitor, native CUDA' if args.no_mps else
+ 'MPS client limit and whole-GPU NVML monitor',
+ 'allocator_config': env.get('PYTORCH_ALLOC_CONF', env.get('PYTORCH_CUDA_ALLOC_CONF', 'default')),
+ 'compiler_cache_environment': {key: env[key] for key in
+ ('TORCHINDUCTOR_CACHE_DIR', 'TRITON_CACHE_DIR', 'CUTE_DSL_CACHE_DIR', 'XDG_CACHE_HOME') if key in env},
+ 'diagnostic_environment': {key: env[key] for key in ('CUDA_LAUNCH_BLOCKING', 'CUBLAS_WORKSPACE_CONFIG') if key in env},
+ 'hardware_simulation': 'Memory capacity only; GPU compute and bandwidth unchanged',
+ 'timeout_seconds': args.timeout,
+ 'ram_limit_enforced': ram is not None,
+ 'execution_environment': 'container' if Path('/.dockerenv').exists() else 'host',
+ 'scope': 'Process start through exit, including imports, load, generate, decode and save'}
+ if ram is not None:
+ plan.update(ram_budget_bytes=ram.budget_bytes, ram_limit_enforced=True,
+ swap_budget_bytes=ram.swap_budget_bytes,
+ memory_mode='bounded_swap' if ram.swap_budget_bytes else 'no_swap',
+ ram_cgroup_initial=ram.initial)
+ repo = Path(__file__).resolve().parents[1]
+ source_files = sorted((repo / 'freevideo_engine').glob('*.py')) + sorted((repo / 'scripts').glob('*.py'))
+ source_files += sorted((repo / 'scripts').glob('*.sh'))
+ source_files += sorted((repo / 'tests').glob('*.py'))
+ source_files += sorted((repo / 'comfy_nodes').rglob('*.py')) + sorted((repo / 'profiles').rglob('*.json'))
+ source_files += sorted((repo / 'freevideo_engine').glob('*.json'))
+ plan['source_sha256'] = {}
+ for path in source_files:
+ relative = path.relative_to(repo)
+ data = path.read_bytes()
+ plan['source_sha256'][str(relative)] = hashlib.sha256(data).hexdigest()
+ destination = root / 'source' / relative
+ destination.parent.mkdir(parents=True, exist_ok=True)
+ destination.write_bytes(data)
+ plan['engine_source_snapshot'] = str(root / 'source')
+ # Python otherwise prepends the working/script directory ahead of PYTHONPATH.
+ # -P semantics ensure imports really use the frozen experiment source.
+ env['PYTHONSAFEPATH'] = '1'
+ env['PYTHONPATH'] = os.pathsep.join((str(root / 'source'), str(root / 'source/setup'), env.get('PYTHONPATH', '')))
+ plan['python_safe_path'] = True
+ if len(command) > 1 and command[1].endswith('.py'):
+ script = Path(command[1]).resolve()
+ if script.is_relative_to(repo):
+ snapshot = root / 'source' / script.relative_to(repo)
+ if snapshot.is_file():
+ plan['requested_command'] = list(command)
+ command = [command[0], str(snapshot), *command[2:]]
+ plan['command'] = command
+ save(root / 'plan.json', plan)
+ daemon_started = False
+ proc = None
+ result = dict(plan, status='starting')
+ cpu_peak_rss = 0
+ disk_read_bytes = 0
+ ram_log = (root / 'ram.csv').open('w', buffering=1, encoding='utf-8')
+ ram_writer = csv.writer(ram_log)
+ ram_writer.writerow(['elapsed_seconds', 'tree_rss_bytes', 'tree_pss_bytes', 'system_available_bytes', 'processes'])
+ def interrupted(signum, frame):
+ raise KeyboardInterrupt(f'Capacity measurement interrupted by signal {signum}')
+ previous_signal = signal.signal(signal.SIGTERM, interrupted)
+ monitor.start()
+ try:
+ if not args.no_mps and shared_pipe is None:
+ daemon = subprocess.run(['nvidia-cuda-mps-control', '-d'], env=env,
+ capture_output=True, text=True, timeout=20)
+ (root / 'mps-start.log').write_text(daemon.stdout + daemon.stderr, encoding='utf-8')
+ daemon.check_returncode()
+ daemon_started = True
+ started = time.monotonic()
+ wall_start = time.time()
+ with (root / 'worker.log').open('w', encoding='utf-8') as log:
+ proc = subprocess.Popen(command, env=env, stdout=log, stderr=subprocess.STDOUT,
+ start_new_session=True, pass_fds=(descriptor,))
+ save(root / 'process.json', {'pid': proc.pid})
+ reason = None
+ while proc.poll() is None:
+ elapsed = time.monotonic() - started
+ ram_state = ram.snapshot() if ram is not None else None
+ memory_observed = observation.sample(proc.pid)
+ ram_writer.writerow([elapsed, memory_observed['rss_bytes'], memory_observed['pss_bytes'],
+ memory_observed['system_available_bytes'], memory_observed['processes']])
+ try:
+ process_status = (Path('/proc') / str(proc.pid) / 'status').read_text(encoding='utf-8')
+ rss = next(int(line.split()[1]) * 1024 for line in process_status.splitlines() if line.startswith('VmRSS:'))
+ cpu_peak_rss = max(cpu_peak_rss, rss)
+ io = (Path('/proc') / str(proc.pid) / 'io').read_text(encoding='utf-8')
+ disk_read_bytes = max(disk_read_bytes, next(int(line.split()[1]) for line in io.splitlines() if line.startswith('read_bytes:')))
+ except (OSError, StopIteration):
+ pass
+ if monitor.errors:
+ reason = 'monitor_failed'
+ elif monitor.peak > budget:
+ reason = 'whole_gpu_budget_exceeded'
+ elif ram_state and ram.events_since_start(ram_state).get('oom_kill', 0):
+ reason = 'ram_cgroup_oom'
+ elif elapsed > args.timeout:
+ reason = 'timeout'
+ if reason:
+ stop_process(proc)
+ break
+ save(root / 'status.json', {'event': 'running', 'elapsed_seconds': elapsed,
+ 'gpu_used_bytes': monitor.last,
+ 'gpu_peak_bytes': monitor.peak,
+ 'worker_peak_rss_bytes': cpu_peak_rss,
+ 'worker_disk_read_bytes': disk_read_bytes,
+ 'ram_pss_peak_bytes': observation.peak_pss,
+ 'ram_tree_rss_peak_bytes': observation.peak_rss,
+ 'ram_observed': memory_observed,
+ 'ram_cgroup': ram_state})
+ try:
+ proc.wait(timeout=0.5)
+ except subprocess.TimeoutExpired:
+ pass
+ result.update(exit_code=proc.returncode, process_seconds=time.monotonic() - started,
+ status=reason or ('success' if proc.returncode == 0 else 'error'))
+ except BaseException as exc:
+ result.update(status='harness_error', error=f'{type(exc).__name__}: {exc}')
+ raise
+ finally:
+ if proc is not None:
+ stop_process(proc)
+ # Children can start their own sessions. A crashed wrapper must not
+ # leave its known server/worker alive with our inherited benchmark lock.
+ for (pid, start_tick) in observation.process_io:
+ try:
+ fields = Path(f'/proc/{pid}/stat').read_text(encoding='utf-8').rsplit(')', 1)[1].split()
+ if fields[19] == start_tick and fields[0] != 'Z':
+ os.kill(pid, signal.SIGTERM)
+ except (OSError, IndexError):
+ pass
+ if daemon_started:
+ try:
+ stopped = subprocess.run(['nvidia-cuda-mps-control'], input='quit\n', env=env,
+ capture_output=True, text=True, timeout=20)
+ (root / 'mps-stop.log').write_text(stopped.stdout + stopped.stderr, encoding='utf-8')
+ if stopped.returncode:
+ result['cleanup_error'] = 'MPS shutdown returned ' + str(stopped.returncode)
+ except subprocess.TimeoutExpired as error:
+ result['cleanup_error'] = str(error)
+ if result.get('cleanup_error') and result.get('status') == 'success':
+ result['status'] = 'cleanup_error'
+ result.update(monitor.stop())
+ ram_log.close()
+ result.update(observation.result())
+ result.update(worker_peak_rss_bytes=cpu_peak_rss, worker_disk_read_bytes=disk_read_bytes,
+ worker_memory_scope='Main worker RSS includes file-backed pages; compiler child processes excluded')
+ result['within_budget'] = result['gpu_peak_bytes'] <= budget
+ if ram is not None:
+ result.update(ram.result())
+ result['within_budget'] = result['within_budget'] and result['ram_within_budget']
+ if not result['ram_within_budget'] and result['status'] == 'success':
+ result['status'] = 'ram_budget_failed'
+ if result.get('status') in CONTROLLED_STOPS:
+ result['settled_attempts'] = settle_controlled_stop(
+ result['status'], locals().get('wall_start', 0), monitor.device)
+ result['attempt_completed_successfully'] = result['status'] == 'success'
+ result['ram_limit_attempt_passed'] = bool(ram is not None and result['status'] == 'success'
+ and result['within_budget'])
+ save(root / 'result.json', result)
+ save(root / 'status.json', dict(result, event='finished'))
+ print(json.dumps(result, indent=2), flush=True)
+ signal.signal(signal.SIGTERM, previous_signal)
+ return 0 if result['status'] == 'success' and result['within_budget'] else 1
+
+
+if __name__ == '__main__':
+ raise SystemExit(main())
diff --git a/freevideo_engine/cli.py b/freevideo_engine/cli.py
new file mode 100644
index 0000000..b5e3247
--- /dev/null
+++ b/freevideo_engine/cli.py
@@ -0,0 +1,161 @@
+"""Public CLI. Planning and help do not import the model runtime."""
+import argparse
+import json
+import os
+from pathlib import Path
+import sys
+from .hardware import Hardware, detect
+from .kernel_capabilities import available_backends
+from .policy import choose
+
+
+def write_json(value, path=None):
+ text = json.dumps(value, ensure_ascii=False, indent=2) + '\n'
+ if path:
+ Path(path).parent.mkdir(parents=True, exist_ok=True)
+ Path(path).write_text(text, encoding='utf-8')
+ print(text, end='')
+
+
+def resource_arguments(parser):
+ parser.add_argument('--vram-gib', type=float, help='Nominal capacity, bounded by detected VRAM')
+ parser.add_argument('--ram-gib', type=float, help='Nominal system RAM, bounded by detected RAM')
+ parser.add_argument('--gpu-reserve-gib', type=float,
+ help='Explicit foreground VRAM growth reserve (minimum 0.2 GiB); automatic planning may shrink its default reserve for a tight request')
+ parser.add_argument('--ram-reserve-gib', type=float, help='Override headroom kept beyond current system RAM usage')
+ parser.add_argument('--attention', default='auto', help='auto, sage2, cudnn, torch-flash, fa2, fa4, or global/window')
+
+
+def main():
+ if len(sys.argv) > 1 and sys.argv[1] == 'predict':
+ from .prediction_cli import main as predict
+ raise SystemExit(predict(sys.argv[2:]))
+ if len(sys.argv) > 1 and sys.argv[1] == 'resource-history':
+ from .resource_cli import main as resource_history
+ raise SystemExit(resource_history(sys.argv[2:]))
+ if len(sys.argv) > 1 and sys.argv[1] == 'optimize':
+ from .optimize import main as optimize
+ raise SystemExit(optimize(sys.argv[2:]))
+ if len(sys.argv) > 1 and sys.argv[1] == 'diagnose':
+ from .diagnostics import main as diagnose
+ raise SystemExit(diagnose(sys.argv[2:]))
+ if len(sys.argv) > 1 and sys.argv[1] == 'bench':
+ from .benchmark import main as benchmark
+ return benchmark(sys.argv[2:])
+ if len(sys.argv) > 1 and sys.argv[1] == 'calibrate':
+ from .calibration import main as calibrate
+ return calibrate(sys.argv[2:])
+ parser = argparse.ArgumentParser(prog='freevideo', description='VDN Engine: video and audio with bounded memory')
+ sub = parser.add_subparsers(dest='command', required=True)
+ plan = sub.add_parser('plan', help='Show a resource policy without loading models')
+ resource_arguments(plan)
+ plan.add_argument('--hardware-json', type=Path, help='Inspect a fixture, not a hardware benchmark')
+ plan.add_argument('--out', type=Path)
+ plan.add_argument('--width', type=int, default=1344)
+ plan.add_argument('--height', type=int, default=768)
+ plan_length = plan.add_mutually_exclusive_group()
+ plan_length.add_argument('--frames', type=int)
+ plan_length.add_argument('--seconds', type=float)
+ doctor = sub.add_parser('doctor', help='Check paths, versions and installed attention kernels')
+ doctor.add_argument('--probe', action='store_true', help='Execute small kernels in isolated processes')
+ doctor.add_argument('--require-paths', action='store_true', help='Fail if model or source directories are missing')
+ doctor.add_argument('--out', type=Path, help='Save the complete kernel report, including optional failures')
+ sub.add_parser('diagnose', help='Collect bounded local diagnostics, even after a failed setup')
+ sub.add_parser('optimize', help='Reuse a test report and validate bounded local tuning')
+ sub.add_parser('resource-history', help='Inspect local performance and resource measurements (CPU only)')
+ sub.add_parser('predict', help='Preview phase time and memory from local complete request history')
+ setup = sub.add_parser('setup', help='Install the pinned upstream model-code dependency')
+ setup.add_argument('--vdn-root', type=Path)
+ setup.add_argument('--install-packages', action='store_true')
+ setup.add_argument('--fa4-guard', action='store_true', help='Prepare the validated isolated FA4 b26 SM120 fix')
+ prepare = sub.add_parser('prepare', help='Prepare official merged weights and an FP8 cache')
+ prepare.add_argument('--base', type=Path, required=True)
+ prepare.add_argument('--checkpoint', type=Path, required=True)
+ prepare.add_argument('--cache-root', type=Path, required=True)
+ prepare.add_argument('--bf16-source', type=Path)
+ encode = sub.add_parser('encode', help='Save reusable text conditioning for generation and matched benchmarks')
+ resource_arguments(encode)
+ encode.add_argument('--prompt-file', type=Path, required=True)
+ encode.add_argument('--out', type=Path, required=True)
+ generate = sub.add_parser('generate', help='Generate an MP4 from text or cached conditioning')
+ resource_arguments(generate)
+ source = generate.add_mutually_exclusive_group(required=True)
+ source.add_argument('--prompt-file', type=Path)
+ source.add_argument('--conditioning', type=Path)
+ generate.add_argument('--cache', type=Path, required=True)
+ generate.add_argument('--base', type=Path)
+ generate.add_argument('--checkpoint', type=Path)
+ generate.add_argument('--profile', type=Path, help='Explicit policy/profile override')
+ from .media_request import TASKS
+ generate.add_argument('--task', choices=TASKS, help='Input task; inferred when --media is provided')
+ generate.add_argument('--media', type=Path, help='Versioned keyframe/reference/LoRA request JSON; local files only')
+ generate.add_argument('--allocator-limit-gib', type=float,
+ help='Explicit benchmark cap on PyTorch CUDA allocations; not a whole-device memory limit')
+ generate.add_argument('--no-history-placement', action='store_true', help='Keep the starting placement for a matched baseline; still record measurements')
+ generate.add_argument('--recover-placement', action='store_true', help='Allow numerical-neutral resource recovery with an explicit profile; automatic profiles already recover')
+ generate.add_argument('--resource-retries', type=int, choices=(0, 1, 2), default=2,
+ help='Fresh-worker placement retries after confirmed resource exhaustion (default: 2)')
+ generate.add_argument('--no-tuning', action='store_true', help='Bypass saved tuning and conditioning reuse for this request')
+ generate.add_argument('--seed', type=int, default=2026090901)
+ generate.add_argument('--two-pass', action=argparse.BooleanOptionalAction, default=True,
+ help='Automatic smaller-canvas 8 steps + latent upscale + 2 tail steps (default: on, including media conditioning); --no-two-pass restores full-resolution 8 steps')
+ generate.add_argument('--out', type=Path, required=True)
+ generate.add_argument('--width', type=int, default=1344)
+ generate.add_argument('--height', type=int, default=768)
+ length = generate.add_mutually_exclusive_group()
+ length.add_argument('--frames', type=int)
+ length.add_argument('--seconds', type=float)
+ for command in (generate, encode):
+ command.add_argument('--encoder-python', '--comfy-python', dest='comfy_python',
+ default=os.environ.get('FREEVIDEO_COMFY_PYTHON', sys.executable))
+ command.add_argument('--encoder-root', '--comfy-root', dest='comfy_root', type=Path)
+ command.add_argument('--model-paths', type=Path, help='Optional ComfyUI extra_model_paths.yaml')
+ command.add_argument('--encoder', default='qwen3vl_32b_minimax_h3_nvfp4_awq.safetensors')
+ bench = sub.add_parser('bench', help='Compare attention backends under one explicit policy')
+ bench.add_argument('arguments', nargs=argparse.REMAINDER)
+ calibrate = sub.add_parser('calibrate',
+ help='Measure the placement questions this machine can settle')
+ calibrate.add_argument('arguments', nargs=argparse.REMAINDER)
+ args = parser.parse_args()
+ if args.command == 'plan':
+ from .geometry import geometry
+ canvas = geometry(args.width, args.height, frames=args.frames, seconds=args.seconds)
+ hardware = Hardware.from_dict(json.loads(args.hardware_json.read_text(encoding='utf-8'))) if args.hardware_json else detect()
+ available = {'sage2', 'cudnn', 'torch-flash', 'fa2', 'fa4'} if args.hardware_json else available_backends(hardware, probe_missing=False)
+ policy = choose(hardware, **{key: getattr(args, key) for key in
+ ['vram_gib', 'ram_gib', 'attention', 'gpu_reserve_gib', 'ram_reserve_gib']},
+ available_backends=available, canvas=canvas)
+ write_json(policy.to_dict(), args.out)
+ elif args.command == 'doctor':
+ from .doctor import doctor
+ result = doctor(probe=args.probe)
+ if args.out:
+ from .monitoring import save
+ save(args.out, result)
+ write_json(result)
+ if args.probe and not result['ready']:
+ raise SystemExit(1)
+ if args.require_paths and not all(row['exists'] for row in result['paths'].values()):
+ raise SystemExit(1)
+ elif args.command == 'setup':
+ from .install import install
+ install(args.vdn_root, packages=args.install_packages)
+ if args.fa4_guard:
+ from .fa4_guard import prepare
+ write_json({'fa4_overlay': str(prepare())})
+ elif args.command == 'prepare':
+ from .paths import add_vdn
+ add_vdn()
+ from .weights import prepare as prepare_bf16
+ from .fp8 import prepare as prepare_fp8
+ source = args.bf16_source or prepare_bf16(args.base, args.checkpoint, args.cache_root)
+ write_json({'cache': str(prepare_fp8(source, args.cache_root, base=args.base, checkpoint=args.checkpoint))})
+ elif args.command == 'generate':
+ from .generate import run
+ run(args)
+ elif args.command == 'encode':
+ from .encode import run
+ run(args)
+ else:
+ from .benchmark import main as benchmark
+ benchmark(args.arguments)
diff --git a/freevideo_engine/cold_pages.py b/freevideo_engine/cold_pages.py
new file mode 100644
index 0000000..f1f9694
--- /dev/null
+++ b/freevideo_engine/cold_pages.py
@@ -0,0 +1,121 @@
+"""Prepare a final cold-model RAM test without dropping the system page cache.
+
+Only immutable model files in the selected FP8 cache, VAEs and shared encoder
+are advised away. fincore or libc mincore verifies the result because
+POSIX_FADV_DONTNEED is advisory. No file contents, global drop_caches setting or
+unrelated caches change.
+See https://man7.org/linux/man-pages/man2/posix_fadvise.2.html and mincore(2).
+"""
+import argparse
+import ctypes
+import json
+import mmap
+import os
+from pathlib import Path
+import shutil
+import subprocess
+import time
+
+from freevideo_engine.locking import runtime_lock
+from freevideo_engine.paths import base_path
+
+
+def residency(paths):
+ if shutil.which('fincore') is None:
+ return mincore_residency(paths)
+ result = subprocess.run(['fincore', '--json', '--bytes', '--output', 'RES,SIZE,FILE',
+ *map(str, paths)], check=True, text=True, capture_output=True)
+ return json.loads(result.stdout)['fincore']
+
+
+def mincore_residency(paths):
+ """Query Linux page residency without touching pages or requiring util-linux."""
+ libc = ctypes.CDLL(None, use_errno=True)
+ libc.mmap.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_int, ctypes.c_int,
+ ctypes.c_int, ctypes.c_longlong]
+ libc.mmap.restype = ctypes.c_void_p
+ libc.mincore.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.POINTER(ctypes.c_ubyte)]
+ libc.mincore.restype = ctypes.c_int
+ libc.munmap.argtypes = [ctypes.c_void_p, ctypes.c_size_t]
+ libc.munmap.restype = ctypes.c_int
+ rows = []
+ for path in paths:
+ size = path.stat().st_size
+ resident = 0
+ if size:
+ with path.open('rb', buffering=0) as file:
+ address = libc.mmap(None, size, mmap.PROT_READ, mmap.MAP_SHARED, file.fileno(), 0)
+ if address == ctypes.c_void_p(-1).value:
+ error = ctypes.get_errno()
+ raise OSError(error, os.strerror(error), str(path))
+ try:
+ pages = (ctypes.c_ubyte * ((size + mmap.PAGESIZE - 1) // mmap.PAGESIZE))()
+ if libc.mincore(address, size, pages):
+ error = ctypes.get_errno()
+ raise OSError(error, os.strerror(error), str(path))
+ resident = sum(value & 1 for value in pages) * mmap.PAGESIZE
+ finally:
+ if libc.munmap(address, size):
+ error = ctypes.get_errno()
+ raise OSError(error, os.strerror(error), str(path))
+ rows.append({'res': resident, 'size': size, 'file': str(path)})
+ return rows
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--cache', type=Path)
+ parser.add_argument('--model-files', nargs='+', type=Path, help='Explicit immutable model set for another pipeline')
+ parser.add_argument('--base', type=Path, default=base_path())
+ parser.add_argument('--encoder', type=Path)
+ parser.add_argument('--out', required=True, type=Path)
+ args = parser.parse_args()
+ if args.model_files:
+ if args.cache:
+ parser.error('Select a prepared FP8 cache or explicit model files')
+ paths = args.model_files
+ else:
+ if args.cache is None:
+ parser.error('--cache or --model-files is required')
+ paths = list(args.cache.rglob('*.safetensors'))
+ paths += list((args.base / 'vae').glob('*.safetensors'))
+ paths += list((args.base / 'audio_vae').glob('*.safetensors'))
+ if args.encoder:
+ paths.append(args.encoder)
+ paths = sorted({p.resolve() for p in paths})
+ if not paths or any(not p.is_file() or p.suffix != '.safetensors' for p in paths):
+ raise ValueError('Every selected file must exist in the immutable model store')
+ if any(p.stat().st_uid != os.geteuid() for p in paths) and os.geteuid() != 0:
+ raise PermissionError('Accurate mincore residency requires ownership of the model files')
+ before_stat = {str(p): (p.stat().st_size, p.stat().st_mtime_ns) for p in paths}
+ started = time.perf_counter()
+ with runtime_lock():
+ clients = subprocess.check_output(['nvidia-smi', '--query-compute-apps=pid', '--format=csv,noheader'], text=True).strip()
+ if clients:
+ raise RuntimeError('Wait for all model workers to exit before evicting their file pages')
+ before = residency(paths)
+ for path in paths:
+ with path.open('rb', buffering=0) as file:
+ os.fsync(file.fileno())
+ os.posix_fadvise(file.fileno(), 0, 0, os.POSIX_FADV_DONTNEED)
+ after = residency(paths)
+ if any((p.stat().st_size, p.stat().st_mtime_ns) != before_stat[str(p)] for p in paths):
+ raise RuntimeError('A model file changed during cache preparation')
+ result = {'scope': __doc__, 'file_count': len(paths), 'before': before, 'after': after,
+ 'residency_verifier': 'fincore' if shutil.which('fincore') else 'libc mincore',
+ 'resident_before_bytes': sum(int(row['res']) for row in before),
+ 'resident_after_bytes': sum(int(row['res']) for row in after),
+ 'file_bytes': sum(int(row['size']) for row in after),
+ 'seconds': time.perf_counter() - started,
+ 'file_content_written': False, 'global_drop_caches_used': False}
+ # Partial final pages may remain. Report them rather than claiming zero.
+ result['cold_model_pages_verified'] = result['resident_after_bytes'] <= 16 * 1024 * 1024
+ args.out.parent.mkdir(parents=True, exist_ok=True)
+ args.out.write_text(json.dumps(result, indent=2) + '\n', encoding='utf-8')
+ print(json.dumps({k: v for k, v in result.items() if k not in ('before', 'after', 'scope')}))
+ if not result['cold_model_pages_verified']:
+ raise RuntimeError('Too many model pages remain resident for the planned cold-model test')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freevideo_engine/comfy_assets.py b/freevideo_engine/comfy_assets.py
new file mode 100644
index 0000000..1a3ba82
--- /dev/null
+++ b/freevideo_engine/comfy_assets.py
@@ -0,0 +1,157 @@
+"""File-backed ComfyUI media bundles; no tensor or model imports."""
+import json
+import logging
+import math
+from pathlib import Path, PurePosixPath, PureWindowsPath
+
+
+def saved_video(output, output_directory):
+ """Publish the same file to ComfyUI's preview/history and native assets."""
+ output = Path(output).resolve()
+ relative = output.relative_to(Path(output_directory).resolve())
+ if not output.is_file():
+ raise FileNotFoundError(output)
+ entry = dict(filename=relative.name, subfolder=relative.parent.as_posix(), type='output')
+ try:
+ from comfy.cli_args import args
+ if getattr(args, 'enable_assets', False):
+ # ComfyUI enriches output entries itself when global assets are on.
+ return entry
+ from app.assets.services.ingest import register_file_in_place
+ registered = register_file_in_place(abs_path=str(output), name=output.name, tags=['output'])
+ entry['id'] = registered.ref.id
+ except ImportError:
+ logging.getLogger(__name__).info('Native asset registration is unavailable in this ComfyUI; video remains in output history.')
+ except Exception:
+ # An unavailable asset database must never invalidate a saved video.
+ logging.getLogger(__name__).warning('FreeVideo video was saved, but ComfyUI asset registration failed: %s', output, exc_info=True)
+ return entry
+
+
+EXTENSIONS = {
+ 'image': {'.png', '.jpg', '.jpeg', '.webp', '.bmp', '.tif', '.tiff'},
+ 'video': {'.mp4', '.mov', '.webm', '.mkv', '.m4v'},
+ 'audio': {'.wav', '.mp3', '.flac', '.ogg', '.m4a', '.aac', '.opus'},
+}
+
+
+def resolve_loras(serialized, names, resolve, previous=None):
+ """Resolve a UI stack through Comfy's registered libraries, never raw paths."""
+ if not isinstance(serialized, str) or len(serialized) > 128 * 1024:
+ raise ValueError('Invalid LoRA selection')
+ rows = json.loads(serialized)
+ result = list(previous or [])
+ if not isinstance(rows, list) or len(rows) + len(result) > 32:
+ raise ValueError('Use at most 32 LoRAs per request')
+ available = set(names)
+ for row in rows:
+ if not isinstance(row, dict) or set(row) - {'name', 'strength', 'enabled'}:
+ raise ValueError('Invalid LoRA item')
+ if type(row.get('enabled', True)) is not bool:
+ raise ValueError('LoRA enabled must be a boolean')
+ if not row.get('enabled', True):
+ continue
+ name, strength = row.get('name'), row.get('strength', 1.)
+ if (isinstance(strength, bool) or not isinstance(strength, (int, float))
+ or not math.isfinite(strength) or not -4 <= strength <= 4):
+ raise ValueError('LoRA strength must be a finite number between -4 and 4')
+ if not isinstance(name, str) or name not in available or not name.lower().endswith('.safetensors'):
+ raise ValueError('Select an available safetensors LoRA from your ComfyUI model folders')
+ if strength != 0:
+ result.append({'path': str(Path(resolve(name)).resolve()), 'strength': strength})
+ return result
+
+
+def media_kind(filename):
+ suffix = Path(filename).suffix.lower()
+ for kind, extensions in EXTENSIONS.items():
+ if suffix in extensions:
+ return kind
+ raise ValueError('Choose a supported image, video or audio file')
+
+
+def resolve_assets(serialized, input_directory):
+ if not isinstance(serialized, str) or len(serialized) > 128 * 1024:
+ raise ValueError('Invalid media selection')
+ rows = json.loads(serialized)
+ if not isinstance(rows, list) or len(rows) > 32:
+ raise ValueError('Use at most 32 media items')
+ root = Path(input_directory).resolve()
+ result = {}
+ for row in rows:
+ if not isinstance(row, dict) or set(row) - {'file', 'role', 'enabled'}:
+ raise ValueError('Invalid media item')
+ if type(row.get('enabled', True)) is not bool:
+ raise ValueError('Media enabled must be a boolean')
+ if not row.get('enabled', True):
+ continue
+ name, role = row.get('file'), row.get('role')
+ if (not isinstance(name, str) or not name or '\x00' in name or '\\' in name
+ or PurePosixPath(name).is_absolute() or PureWindowsPath(name).drive
+ or '..' in PurePosixPath(name).parts or ':' in name):
+ raise ValueError('Media must be a relative file inside ComfyUI input')
+ path = (root / name).resolve()
+ try:
+ path.relative_to(root)
+ except ValueError:
+ raise ValueError('Media link points outside ComfyUI input') from None
+ if not path.is_file():
+ raise ValueError('Upload or select the missing media file: ' + name)
+ kind = media_kind(name)
+ if role in ('first', 'last'):
+ if kind != 'image':
+ raise ValueError('First and last frames must be images')
+ if role in result:
+ raise ValueError('Select only one ' + role + ' frame')
+ result[role] = str(path)
+ elif role == 'reference':
+ result.setdefault('references', []).append({'kind': kind, 'path': str(path)})
+ else:
+ raise ValueError('Media role must be first, last or reference')
+ if result.get('references') and any(name in result for name in ('first', 'last')):
+ raise ValueError('Use first/last frames or references in this request. Disable unused media cards.')
+ return result
+
+
+def connect_assets(assets, *, first=None, last=None, reference=None, reference_audio=None):
+ """Keep connected IMAGE/AUDIO values native until request input export."""
+ result = dict(assets)
+ connected = {name: value for name, value in (('first', first), ('last', last),
+ ('reference', reference), ('reference_audio', reference_audio))
+ if value is not None}
+ for name in ('first', 'last'):
+ if name in result and name in connected:
+ raise ValueError('Choose the connected ' + name + ' frame or the uploaded one; disable the duplicate')
+ count = len(result.get('references', [])) + int(reference is not None) + int(reference_audio is not None)
+ if count > 32:
+ raise ValueError('Use at most 32 references, including connected inputs')
+ if count and any(
+ name in result or name in connected for name in ('first', 'last')):
+ raise ValueError('Choose keyframes or references; disable the unused inputs')
+ if connected:
+ result['_connected'] = connected
+ return result
+
+
+def output_summary(report, relative_video):
+ video = report.get('video', {})
+ encoding = report.get('encoding', {})
+ ram = report.get('resources', {}).get('ram', {})
+ encoding_ram = report.get('encoding_resources', {}).get('ram', {})
+ metric = ram.get('ram_guard_metric')
+ values = [r.get('process_tree_peak_guard_bytes') for r in (ram, encoding_ram)
+ if r.get('ram_guard_metric') == metric]
+ values = [v for v in values if isinstance(v, (int, float)) and v >= 0]
+ gpu_values = [phase.get('torch_peak_reserved_bytes') for phase in (video, encoding)]
+ gpu_values = [v for v in gpu_values if isinstance(v, (int, float)) and v >= 0]
+ return dict(sample_seconds=video.get('sample_seconds'), request_seconds=report.get('request_seconds'),
+ vram_peak_bytes=max(gpu_values) if gpu_values else None,
+ ram_peak_bytes=max(values) if values else None, ram_metric=metric,
+ conditioning_cache_hit=report.get('encoding', {}).get('cache_hit', False),
+ gpu_budget_bytes=report.get('profile', {}).get('policy', {}).get('gpu_budget_bytes'),
+ gpu_total_bytes=report.get('profile', {}).get('policy', {}).get('hardware', {}).get('vram_total'),
+ geometry=report.get('geometry', {}),
+ sampling_plan=report.get('sampling_plan'),
+ video=relative_video.as_posix(), report=relative_video.with_suffix(
+ '.debug.json' if report.get('diagnostic_file') == relative_video.with_suffix('.debug.json').name
+ else '.request.json').as_posix())
diff --git a/freevideo_engine/comfy_bridge.py b/freevideo_engine/comfy_bridge.py
new file mode 100644
index 0000000..134bb7e
--- /dev/null
+++ b/freevideo_engine/comfy_bridge.py
@@ -0,0 +1,554 @@
+"""ComfyUI orchestration without importing Torch or a model into its process.
+
+The installed engine owns resource policy, input caches, retries and history.
+This bridge only retains a request, supervises that CLI and forwards progress.
+"""
+import json
+import math
+import os
+from pathlib import Path
+import subprocess
+import time
+import uuid
+
+from . import processes
+from .geometry import geometry
+from .comfy_environment import isolated_environment
+from .monitoring import save
+
+
+def _timing_seconds(value):
+ """Read a finite positive forecast value without inventing a duration."""
+ if isinstance(value, dict):
+ value = value.get('estimate')
+ return value if type(value) in (int, float) and math.isfinite(value) and value > 0 else None
+
+
+def progress_history_forecast(root, machine, canvas):
+ """Approximate UI timing from recent complete videos on this device.
+
+ Placement can change with free memory, so these durations only seed the
+ waiting animation. They never feed the resource policy or capacity checks;
+ the current request's layer/step timings replace the sampling estimate.
+ Read the existing history without creating or migrating a database.
+ """
+ import platform
+ import sqlite3
+ from statistics import median
+ path = Path(root) / 'resource-history.sqlite3'
+ if not machine.get('gpu_uuid') or not path.is_file():
+ return {}
+ try:
+ with sqlite3.connect(path.resolve().as_uri() + '?mode=ro', uri=True, timeout=.1) as db:
+ rows = db.execute("SELECT identity_json,geometry_json,observation_json FROM attempts "
+ "WHERE state='success' AND observation_json IS NOT NULL "
+ "ORDER BY started DESC LIMIT 64").fetchall()
+ samples = []
+ from .two_pass import same_strategy
+ for raw_identity, raw_canvas, raw_observation in rows:
+ identity, shape, observation = map(json.loads, (raw_identity, raw_canvas, raw_observation))
+ if not all(isinstance(value, dict) for value in (identity, shape, observation)):
+ continue
+ gpu = identity.get('gpu', {})
+ gpu_uuid = gpu.get('uuid') if isinstance(gpu, dict) else None
+ if ((gpu_uuid or identity.get('gpu_uuid')) != machine['gpu_uuid']
+ or identity.get('system') != platform.system()
+ or any(shape.get(key) != canvas.get(key) for key in ('width', 'height', 'frames'))
+ or not same_strategy(shape, canvas)
+ or observation.get('validated') is not True or observation.get('full_request') is not True):
+ continue
+ stages = observation.get('stage_seconds')
+ if (isinstance(stages, dict)
+ and all(_timing_seconds(stages.get(key)) is not None
+ for key in ('load_seconds', 'sample_seconds', 'decode_save_seconds'))):
+ samples.append(stages)
+ if len(samples) == 3:
+ break
+ return {'stages': {key: median(row[key] for row in samples)
+ for key in ('load_seconds', 'sample_seconds', 'decode_save_seconds')}} if samples else {}
+ except (OSError, sqlite3.Error, ValueError, TypeError):
+ return {} # A progress estimate cannot prevent generation.
+
+
+class WholeVideoProgress:
+ """Annotate phase events with one report-weighted whole-video progress.
+
+ Local reports take precedence. Without history, live sampling supplies a
+ rough decoder estimate which real decoded-frame timings then replace.
+ These presentation estimates never change resource admission or placement.
+ """
+ def __init__(self, clock=time.perf_counter):
+ self.clock = clock
+ self.started = clock()
+ self.phase_started = self.started
+ self.phase = None
+ self.completed = set()
+ self.weights = {}
+ self.measured = {}
+ self.total = 0.
+ self.forecast_ready = False
+ self.failed = False
+ self.sampling_plan = {}
+ self.decode_from_sampling = False
+ self.sampling_time_weighted = False
+ self.decode_tail = 0.
+
+ def _sampling_estimate(self, message, now):
+ estimate = _timing_seconds(message.get('estimated_step_seconds'))
+ done, total = message.get('done'), message.get('total')
+ if estimate is None or type(done) is not int or type(total) is not int or not 0 <= done <= total:
+ return
+ full_step = estimate
+ remaining = (total - done) * estimate
+ if message.get('uniform_remaining_steps') is False:
+ plan = self.sampling_plan
+ first, second = plan.get('first', {}), plan.get('second') or {}
+ area = first.get('width', 0) * first.get('height', 0)
+ if not area or not second.get('width') or not second.get('height'):
+ return # Do not treat the two larger refinement NFEs as small NFEs.
+ full_step *= second['width'] * second['height'] / area
+ remaining = max(0, plan['base_steps'] - done) * estimate + plan['refine_steps'] * full_step
+ remaining = max(0., remaining - (_timing_seconds(message.get('step_elapsed_seconds')) or 0.))
+ self.weights['sampling'] = max(.001, now - self.phase_started + remaining)
+ self.sampling_time_weighted = True
+ if 'decode' not in self.weights or self.decode_from_sampling:
+ # Windows 1344x768x243 measurements: decode/save was 1.71, 1.90
+ # and 2.01 final-resolution warm NFEs on 4060 Ti / 5060 Ti / 5090.
+ # This is an approximate first-run ruler, not a GPU speed claim.
+ # It scales with this request's work, then yields to frame timing.
+ self.weights['decode'] = 1.9 * full_step
+ self.decode_from_sampling = True
+ self.forecast_ready = True
+
+ @staticmethod
+ def _forecast_weights(forecast):
+ stages = forecast.get('stages') if isinstance(forecast, dict) else None
+ if not isinstance(stages, dict):
+ return {}
+ values = {
+ 'encoding': _timing_seconds(stages.get('text_encode_seconds')),
+ 'load': _timing_seconds(stages.get('load_seconds')),
+ 'sampling': _timing_seconds(stages.get('sample_seconds')),
+ 'decode': _timing_seconds(stages.get('decode_save_seconds')),
+ }
+ values = {key: value for key, value in values.items() if value is not None}
+ # Sparse phase forecasts expose work_seconds plus its independently
+ # predicted components. Keep the measured residual as the final
+ # save/cleanup tail instead of silently dropping it or double-counting
+ # the worker total.
+ work = _timing_seconds(stages.get('work_seconds'))
+ residual = work - sum(values.get(key, 0.) for key in ('load', 'sampling', 'decode')) if work is not None else 0.
+ if residual > 0:
+ values['decode'] = values.get('decode', 0.) + residual
+ return values
+
+ def forecast(self, value):
+ value = value.get('forecast') if isinstance(value, dict) and 'forecast' in value else value
+ weights = self._forecast_weights(value)
+ if weights:
+ # The prediction is emitted after prompt encoding has completed.
+ # Close that phase before the first video-model event so its
+ # measured weight is credited instead of starting a second timer.
+ if self.phase == 'encoding':
+ self.measured['encoding'] = max(.001, self.clock() - self.phase_started)
+ self.completed.add('encoding')
+ self.phase = None
+ self.weights.update(weights)
+ if 'decode' in weights:
+ self.decode_from_sampling = False
+ self.weights.update(self.measured)
+ self.total = sum(self.weights.values())
+ self.forecast_ready = all(name in self.weights for name in ('sampling', 'decode'))
+
+ def _base(self):
+ return sum(self.weights.get(name, 0.) for name in self.completed)
+
+ def _message_phase(self, message):
+ if message.get('timing_phase') in ('encoding', 'load', 'sampling', 'decode'):
+ return message['timing_phase']
+ phase = message.get('phase')
+ return {'sampling': 'sampling', 'sample_finalize': 'decode',
+ 'complete': 'decode'}.get(phase)
+
+ def annotate(self, message):
+ message = dict(message)
+ if isinstance(message.get('sampling_plan'), dict):
+ self.sampling_plan = message['sampling_plan']
+ if isinstance(message.get('prediction'), dict):
+ self.forecast(message['prediction'])
+ retry = message.get('retry') if isinstance(message.get('retry'), dict) else {}
+ if retry.get('reuse_sampling') is True:
+ self.completed.add('sampling')
+ phase = self._message_phase(message)
+ now = self.clock()
+ if phase and phase != self.phase:
+ if self.phase is not None:
+ self.completed.add(self.phase)
+ self.measured[self.phase] = max(.001, now - self.phase_started)
+ self.weights[self.phase] = self.measured[self.phase]
+ self.phase, self.phase_started = phase, now
+ if phase == 'sampling':
+ self._sampling_estimate(message, now)
+ if phase == 'decode' and message.get('stage') == 'video':
+ done, total = message.get('done'), message.get('total')
+ elapsed = _timing_seconds(message.get('elapsed_seconds'))
+ if elapsed and type(done) is int and type(total) is int and 0 < done <= total:
+ video_seconds = elapsed * total / done
+ # Reports put audio/MP4/post-decode cleanup at about 6-8% of
+ # video decode time. Retain this tail until audio/save starts.
+ self.decode_tail = video_seconds * .08
+ self.weights['decode'] = now - self.phase_started + video_seconds - elapsed + self.decode_tail
+ self.forecast_ready = True
+ elif phase == 'decode':
+ self.decode_tail = 0.
+ self.total = sum(self.weights.values())
+ if message.get('phase') == 'complete' or message.get('overall', {}).get('status') == 'complete':
+ message['overall'] = {'status': 'complete', 'fraction': 1., 'estimated': False,
+ 'elapsed_seconds': now - self.started, 'remaining_seconds': 0.}
+ return message
+ overall = {'status': 'running', 'estimated': self.forecast_ready,
+ 'elapsed_seconds': now - self.started}
+ if self.forecast_ready and self.total > 0:
+ start = self._base() / self.total
+ weight = self.weights.get(self.phase, 0.)
+ elapsed = max(0., now - self.phase_started)
+ seconds = self.weights.get(self.phase)
+ fraction = start
+ if weight:
+ fraction += weight / self.total * min(.95, elapsed / max(seconds, 1e-9))
+ remaining = max(0., self.total - self._base() - min(elapsed, seconds or 0.))
+ future = sum(value for name, value in self.weights.items()
+ if name not in self.completed and name != self.phase)
+ overall.update(fraction=fraction, phase_start_fraction=start,
+ phase_weight=weight / self.total if self.total else 0.,
+ phase_elapsed_seconds=elapsed, phase_seconds=seconds,
+ remaining_seconds=remaining,
+ remaining_floor_seconds=max(future, self.decode_tail),
+ sampling_time_weighted=self.sampling_time_weighted)
+ message['overall'] = overall
+ return message
+
+
+def source_root():
+ return Path(__file__).resolve().parents[1]
+
+
+def installation_root(source=None, environ=None):
+ source = Path(source or source_root())
+ environ = os.environ if environ is None else environ
+ selected = environ.get('FREEVIDEO_HOME')
+ config = source / 'comfyui.json'
+ if not selected and config.is_file():
+ selected = json.loads(config.read_text(encoding='utf-8')).get('installation')
+ if not isinstance(selected, str) or not selected.strip():
+ raise ValueError('comfyui.json must contain an installation directory.')
+ root = Path(selected).expanduser() if selected else source
+ if not root.is_absolute():
+ root = source / root
+ return root.resolve()
+
+
+def installation(source=None, environ=None):
+ root = installation_root(source, environ)
+ try:
+ machine = json.loads((root / 'machine.json').read_text(encoding='utf-8'))
+ except FileNotFoundError as error:
+ raise ValueError('FreeVideo setup is missing. Open FreeVideo Settings and click "Install / repair", '
+ 'or run setup.cmd (Windows) or setup.sh (Linux) '
+ 'in the FreeVideo folder, or set FREEVIDEO_HOME to your existing installation. '
+ 'A prepared engine installation is required.') from error
+ required = ('root', 'python', 'cache', 'base', 'checkpoint', 'comfy_python',
+ 'comfy_root', 'vdn_root', 'model_paths', 'model_root', 'gpu_uuid', 'encoder')
+ if not isinstance(machine, dict) or not machine.get('ready') or any(not machine.get(k) for k in required):
+ raise ValueError('FreeVideo setup is incomplete. Open FreeVideo Settings and click "Install / repair" to repair %s; existing files are reused.' % root)
+ if Path(machine['root']).expanduser().resolve() != root:
+ raise ValueError('The FreeVideo installation was moved. Rerun setup in the selected folder.')
+ if not Path(machine['python']).is_file():
+ raise ValueError('The FreeVideo Python environment is missing. Rerun setup in %s.' % root)
+ return root, machine
+
+
+def validate_request(prompt, width, height, seconds, seed):
+ if not isinstance(prompt, str) or not prompt.strip():
+ raise ValueError('Enter a prompt describing the video and its audio.')
+ if type(seed) is not int or not 0 <= seed <= 2**53 - 1:
+ raise ValueError('Seed must be an integer between 0 and 9007199254740991.')
+ if isinstance(seconds, bool) or not isinstance(seconds, (float, int)) or not math.isfinite(seconds):
+ raise ValueError('Duration must be a positive number of seconds.')
+ return geometry(width, height, seconds=seconds)
+
+
+class EventTail:
+ """Read bounded complete JSON lines; do not hold worker log handles open."""
+ def __init__(self, path):
+ self.path = Path(path)
+ self.offset = 0
+ self.identity = None
+ self.pending = b''
+
+ def reset(self):
+ self.offset = 0
+ self.identity = None
+ self.pending = b''
+
+ def events(self):
+ try:
+ with self.path.open('rb') as stream:
+ stat = os.fstat(stream.fileno())
+ identity = (stat.st_dev, stat.st_ino)
+ if identity != self.identity or stat.st_size < self.offset:
+ self.reset()
+ self.identity = identity
+ stream.seek(self.offset)
+ data = stream.read(64 * 1024)
+ self.offset += len(data)
+ except FileNotFoundError:
+ return []
+ lines = (self.pending + data).split(b'\n')
+ self.pending = lines.pop()
+ if len(self.pending) > 64 * 1024:
+ self.pending = b''
+ result = []
+ for line in lines:
+ if not line.startswith(b'{'):
+ continue
+ try:
+ event = json.loads(line)
+ if isinstance(event, dict):
+ result.append(event)
+ except (ValueError, UnicodeError):
+ pass
+ return result
+
+
+def progress_message(event):
+ from .sampling_progress import progress_message as sampling_message
+ sampling = sampling_message(event)
+ if sampling is not None:
+ sampling['timing_phase'] = 'sampling'
+ return sampling
+ name = event.get('event')
+ if name == 'sampling_plan':
+ first, second = event.get('first', {}), event.get('second')
+ if event.get('enabled') and second:
+ return dict(label='Two-pass · %d × %d → %d × %d · 8 + 2 steps' %
+ (first['width'], first['height'], second['width'], second['height']),
+ detail='Audio is preserved from the first pass', sampling_plan=event)
+ return dict(label='Single-pass · 8 steps', detail=event.get('reason'), sampling_plan=event)
+ if name == 'latent_upscaler_prepare':
+ return dict(label='Preparing two-pass upscaler', timing_phase='load')
+ if name == 'latent_upscale':
+ return dict(label='Upscaling before the second pass', phase='sampling', stage='latent_upscale',
+ done=8, total=10, display_fraction=.8, timing_phase='sampling',
+ estimated_step_seconds=None, step_elapsed_seconds=0., remaining_seconds=None,
+ uniform_remaining_steps=False)
+ if name == 'ram_budget_warning':
+ # Kept in resource logs and diagnostics, not in generation progress:
+ # crossing a soft estimate while memory is available needs no action.
+ return None
+ if name == 'compatibility':
+ return {'label': 'Compatibility level %s · smaller work groups' % event.get('level'),
+ 'compatibility': event}
+ if name == 'compute_device':
+ return {'label': 'Compute device · %s · %s' % (event.get('backend', ''), event.get('name', '')),
+ 'device': event}
+ if name == 'resource_retry':
+ retry = event.get('retry') if isinstance(event.get('retry'), dict) else {}
+ return {'phase': 'recovery', 'label': 'Adjusting memory placement and retrying', 'retry': retry}
+ labels = {'encoding_start': 'Encoding prompt', 'encoder_load_start': 'Loading text encoder',
+ 'encoder_cache_lookup': 'Checking text encoder cache',
+ 'encoder_cache_hit': 'Reusing text encoder',
+ 'release_idle_cache': 'Releasing idle models and checking available memory again',
+ 'encoder_tokenize_start': 'Preparing text and image tokens',
+ 'encoder_device_load_start': 'Loading text encoder onto GPU',
+ 'encoder_device_reuse': 'Using text encoder already on GPU',
+ 'encoder_compute_start': 'Encoding text and images',
+ 'encoder_oom': 'Releasing encoder weights after insufficient GPU memory',
+ 'encoder_retry': 'Retrying text encoding with more GPU workspace',
+ 'conditioning_cache_hit': 'Reusing prompt cache', 'encoder_complete': 'Prompt ready',
+ 'video_start': 'Loading video model',
+ 'decode_resume': 'Reusing completed sampling · retrying video and audio decoding',
+ 'lora_prepare_start': 'Preparing or reusing the LoRA FP8 variant',
+ 'resource_retry': 'Adjusting memory placement and retrying'}
+ if name == 'decode_phase':
+ return {'label': str(event.get('phase', 'Decoding video and audio')), 'timing_phase': 'decode'}
+ if name == 'decode_progress':
+ return dict(label='Decoding video tiles', timing_phase='decode', phase='decode', stage='video',
+ done=event.get('done'), total=event.get('total'), unit='frames',
+ elapsed_seconds=event.get('elapsed_seconds'))
+ if name == 'media_encode_phase':
+ return {'label': str(event.get('phase', 'Encoding input media')), 'timing_phase': 'encoding'}
+ if name == 'prepared_blocks':
+ return {'label': 'Loading cached video model · %s / 50 blocks' % event.get('blocks', '?'),
+ 'timing_phase': 'load'}
+ if name == 'loaded':
+ return {'label': 'Video model ready', 'timing_phase': 'load'}
+ if name == 'decode_resume':
+ return {'label': 'Reusing completed sampling · retrying video and audio decoding',
+ 'timing_phase': 'decode'}
+ if name == 'model_load_phase':
+ label = str(event.get('phase', 'Loading video model'))
+ if event.get('total'):
+ label += ' · %s / %s blocks' % (event.get('done', 0), event['total'])
+ return {'label': label, 'timing_phase': 'load'}
+ if name == 'lora_prepare':
+ return {'label': 'Preparing LoRA variant · %s / %s groups' % (event.get('done', '?'), event.get('total', '?')),
+ 'timing_phase': 'load'}
+ if name in labels:
+ phase = ('encoding' if name.startswith('encoding') or name.startswith('encoder_')
+ or name in ('conditioning_cache_hit', 'release_idle_cache') else
+ 'load' if name in ('video_start', 'prepared_blocks', 'lora_prepare_start') else None)
+ result = {'label': labels[name]}
+ if phase:
+ result['timing_phase'] = phase
+ return result
+ if name == 'freevideo_ui' and event.get('kind') == 'prediction':
+ return {'label': event.get('detail') or event.get('label') or 'Resource forecast ready',
+ 'prediction': event}
+ return None
+
+
+def generate(prompt, width, height, seconds, seed, output_directory, *,
+ source=None, environ=None, metadata=None, progress=None, interrupted=None,
+ release_models=None, export_inputs=None, two_pass=True, encoder_prewarm=None):
+ if type(two_pass) is not bool:
+ raise ValueError('Two-pass generation must be a boolean')
+ canvas = validate_request(prompt, width, height, seconds, seed)
+ source = Path(source or source_root()).resolve()
+ root, machine = installation(source, environ)
+ environment = isolated_environment(root, source, environ)
+ environment.update(FREEVIDEO_HOME=str(root), PYTHONPATH=str(source), PYTHONUNBUFFERED='1',
+ PYTHONUTF8='1', PYTHONIOENCODING='utf-8', NO_COLOR='1', FREEVIDEO_UI_EVENTS='1')
+ for key in ('FREEVIDEO_RUNTIME_LOCK_FD', 'FREEVIDEO_RUNTIME_LOCK_HANDLE'):
+ environment.pop(key, None)
+ # No attention, placement or capacity override: managed CLI uses the
+ # installation's explicit caps, otherwise its live automatic policy.
+ from .resource_settings import read as read_resources
+ resources = read_resources(root)
+ run = Path(output_directory).resolve() / 'FreeVideo' / time.strftime('%Y-%m-%d', time.gmtime()) / uuid.uuid4().hex
+ run.mkdir(parents=True, exist_ok=False)
+ output = run / 'video.mp4'
+ (run / 'prompt.txt').write_text(prompt, encoding='utf-8')
+ if metadata is not None:
+ save(run / 'workflow.json', metadata)
+ command = [machine['python'], '-B', '-m', 'freevideo_engine.managed', '--root', str(root),
+ 'generate', '--prompt-file', str(run / 'prompt.txt'), '--out', str(output),
+ '--width', str(width), '--height', str(height), '--seconds', str(seconds), '--seed', str(seed),
+ '--two-pass' if two_pass else '--no-two-pass']
+ if resources['gpu_reserve_gib'] is not None:
+ command += ['--gpu-reserve-gib', str(resources['gpu_reserve_gib'])]
+ state = {'status': 'starting', 'geometry': canvas, 'seed': seed, 'installation': str(root),
+ 'command': command, 'output': str(output), 'source': str(source), 'resources': resources}
+ from .diagnostic_resources import encoder_prewarm as prewarm_summary
+ state['encoder_prewarm'] = prewarm_summary(encoder_prewarm)
+ save(run / 'comfy-request.json', state)
+ started = time.monotonic()
+ whole_progress = WholeVideoProgress()
+ def send_progress(message):
+ if progress:
+ progress(whole_progress.annotate(message))
+ process = None
+ tails = [EventTail(run / name) for name in ('generate.log', 'video.encoding.log', 'video.engine.log', 'video.lora.log')]
+ def poll():
+ if interrupted:
+ interrupted()
+ for index, tail in enumerate(tails):
+ for event in tail.events():
+ if index == 0 and event.get('event') == 'resource_retry':
+ tails[2].reset()
+ message = progress_message(event)
+ if message:
+ send_progress(message)
+ try:
+ if interrupted:
+ interrupted()
+ extra = {}
+ if export_inputs:
+ send_progress({'label': 'Retaining and checking input media', 'timing_phase': 'encoding'})
+ extra = export_inputs(run, canvas) or {}
+ if extra:
+ if extra.get('conditioning'):
+ position = command.index('--prompt-file')
+ command[position:position + 2] = ['--conditioning', str(extra['conditioning'])]
+ if extra.get('media'):
+ media_path = run / 'media.json'
+ save(media_path, extra['media'])
+ command += ['--media', str(media_path)]
+ state['command'] = command
+ from .media_request import task_for
+ from .two_pass import plan
+ planned = plan(canvas, two_pass, task_for(extra.get('media', {})))
+ whole_progress.sampling_plan = planned
+ whole_progress.forecast(progress_history_forecast(root, machine, dict(canvas, sampling_plan=planned)))
+ send_progress({'label': 'Preparing %.3f s video + audio · %d × %d' %
+ (canvas['seconds'], width, height)})
+ if release_models:
+ release_models()
+ if (source/'freevideo_engine'/'resident_worker.py').is_file():
+ from .resident_process import OWNER, ENV
+ if environment.get('FREEVIDEO_KEEP_MODELS', 'auto').lower() in ('0', 'off', 'false'):
+ OWNER.close()
+ environment.pop(ENV, None)
+ else:
+ environment[ENV] = OWNER.start(root, source, machine['python'], environment)
+ OWNER.active = True
+ with (run / 'generate.log').open('wb') as log:
+ process = processes.popen(command, env=environment, cwd=source, stdin=subprocess.DEVNULL,
+ stdout=log, stderr=subprocess.STDOUT, start_new_session=True, supervise=True)
+ state.update(status='running', pid=process.pid)
+ save(run / 'comfy-request.json', state)
+ while process.poll() is None:
+ poll()
+ try:
+ process.wait(timeout=.25)
+ except subprocess.TimeoutExpired:
+ pass
+ poll()
+ if process.returncode:
+ from .failure_details import generation_failure
+ raise RuntimeError(generation_failure(run, process.returncode))
+ report = json.loads(output.with_suffix('.request.json').read_text(encoding='utf-8'))
+ engine = json.loads(output.with_suffix('.engine.json').read_text(encoding='utf-8'))
+ from .two_pass import steps, plan
+ completed_steps = steps(report.get('sampling_plan'))
+ expected_plan = plan(canvas, two_pass, report.get('profile', {}).get('engine', {}).get('task', 't2va'))
+ if (report.get('success') is not True or engine.get('success') is not True
+ or not output.is_file() or not output.stat().st_size
+ or len(engine.get('step_seconds', [])) != completed_steps
+ or engine.get('sampling_plan') != report.get('sampling_plan')
+ or report.get('sampling_plan') != expected_plan
+ or any(engine.get('geometry', {}).get(k) != canvas[k] for k in ('width', 'height', 'frames'))):
+ from .failure_details import generation_failure
+ raise RuntimeError('FreeVideo did not complete the requested video.\n' + generation_failure(run))
+ state.update(status='complete', request_seconds=report.get('request_seconds'),
+ engine_report=str(output.with_suffix('.engine.json')))
+ send_progress({'label': 'Video + audio saved', 'phase': 'complete', 'done': completed_steps, 'total': completed_steps})
+ return output
+ except BaseException as error:
+ cancelled = isinstance(error, KeyboardInterrupt) or type(error).__name__ in ('InterruptProcessingException', 'CancelledError')
+ state.update(status='cancelled' if cancelled else 'failed', error=repr(error))
+ try:
+ if cancelled:
+ # Windows may terminate the CLI before its finally block runs.
+ # Persist intent before stopping it so history cannot mistake
+ # this deliberate cancellation for an unexplained device exit.
+ save(run / 'comfy-request.json', state)
+ finally:
+ try:
+ if process is not None:
+ processes.stop(process)
+ finally:
+ if cancelled:
+ # The resident worker belongs to ComfyUI, not to the managed
+ # request's process tree. Confirm it exits too before offering
+ # another generation; cancellation should release its memory.
+ from .resident_process import OWNER
+ OWNER.close()
+ raise
+ finally:
+ from .resident_process import OWNER
+ OWNER.active = False
+ state['bridge_seconds'] = time.monotonic() - started
+ save(run / 'comfy-request.json', state)
+ from .support_report import write as write_debug
+ write_debug(output, bridge=state)
diff --git a/freevideo_engine/comfy_console.py b/freevideo_engine/comfy_console.py
new file mode 100644
index 0000000..8501bb1
--- /dev/null
+++ b/freevideo_engine/comfy_console.py
@@ -0,0 +1,89 @@
+"""Expose ComfyUI's process output to the local embedded terminal."""
+import atexit
+import logging
+import os
+from pathlib import Path
+import sys
+import threading
+import time
+
+
+class Stream:
+ def __init__(self, original, capture):
+ self.original, self.capture = original, capture
+
+ def write(self, value):
+ try:
+ if self.original is not None:
+ self.original.write(value)
+ except (OSError, ValueError):
+ pass
+ finally:
+ self.capture.write(value)
+ return len(value)
+
+ def flush(self):
+ if self.original is not None:
+ try: self.original.flush()
+ except (OSError, ValueError): pass
+
+ def isatty(self):
+ return False # Use readable counters instead of terminal cursor controls.
+
+ def __getattr__(self, name):
+ return getattr(self.original, name)
+
+
+class Capture:
+ def __init__(self, path):
+ self.path = Path(path)
+ self.path.parent.mkdir(parents=True, exist_ok=True)
+ self.lock = threading.Lock()
+ self.stream = self.path.open('a', encoding='utf-8', buffering=1)
+ self.stdout, self.stderr = sys.stdout, sys.stderr
+ sys.stdout, sys.stderr = Stream(self.stdout, self), Stream(self.stderr, self)
+ self.handlers = []
+ loggers = [logging.getLogger()] + [v for v in list(logging.Logger.manager.loggerDict.values()) if isinstance(v, logging.Logger)]
+ for logger in loggers:
+ for handler in logger.handlers:
+ previous = getattr(handler, 'stream', None)
+ if previous is not None and (previous is self.stdout or previous is self.stderr):
+ self.handlers.append((handler, previous))
+ handler.stream = sys.stdout if previous is self.stdout else sys.stderr
+ self.handler = None
+ if not any(handler in logging.getLogger().handlers for handler, _ in self.handlers):
+ self.handler = logging.StreamHandler(sys.stderr)
+ logging.getLogger().addHandler(self.handler)
+
+ def write(self, value):
+ with self.lock:
+ try:
+ self.stream.write(value)
+ self.stream.flush()
+ except (OSError, ValueError):
+ pass # A failed log disk must not crash another node's execution.
+
+ def close(self):
+ if isinstance(sys.stdout, Stream) and sys.stdout.capture is self: sys.stdout = self.stdout
+ if isinstance(sys.stderr, Stream) and sys.stderr.capture is self: sys.stderr = self.stderr
+ for handler, original in self.handlers:
+ if isinstance(handler.stream, Stream) and handler.stream.capture is self:
+ handler.stream = original
+ if self.handler is not None: logging.getLogger().removeHandler(self.handler)
+ self.stream.close()
+
+
+def install(engine):
+ root = Path(engine).resolve() / 'launcher/comfy-runs'
+ managed = os.environ.get('FREEVIDEO_COMFY_LOG')
+ if managed:
+ path = Path(managed).resolve()
+ path.relative_to(root) # Only the launcher's own output file is reusable.
+ return path
+ capture = getattr(sys, '_freevideo_console', None)
+ if capture is None:
+ path = root / ('connected-%s-%s' % (time.time_ns(), os.getpid())) / 'comfy.log'
+ capture = Capture(path)
+ sys._freevideo_console = capture
+ atexit.register(capture.close)
+ return capture.path
diff --git a/freevideo_engine/comfy_environment.py b/freevideo_engine/comfy_environment.py
new file mode 100644
index 0000000..34d4443
--- /dev/null
+++ b/freevideo_engine/comfy_environment.py
@@ -0,0 +1,37 @@
+"""Keep ComfyUI process configuration out of managed engine children."""
+import os
+from pathlib import Path
+import sys
+
+
+def isolated_environment(root, source, environ=None):
+ """Remove inherited Python/package targets only in the engine's child."""
+ from .proxy import inherited
+ from .download_settings import read
+ env = inherited(environ)
+ proxy_mode = read(Path(root) / 'download-settings.json')['proxy_mode']
+ prefixes = [env.get(name) for name in ('VIRTUAL_ENV', 'CONDA_PREFIX')]
+ prefixes.append(sys.prefix)
+ # Do not remove /usr/bin when Comfy itself uses a system interpreter.
+ prefixes = [Path(p).resolve() for p in prefixes if p and Path(p).resolve() not in
+ (Path('/usr'), Path('/usr/local'), Path('/'))]
+ for name in ('PATH', 'LD_LIBRARY_PATH'):
+ if name not in env:
+ continue
+ kept = []
+ for entry in env[name].split(os.pathsep):
+ if not entry:
+ continue
+ path = Path(entry).resolve()
+ if not any(path == prefix or prefix in path.parents for prefix in prefixes):
+ kept.append(entry)
+ env[name] = os.pathsep.join(kept)
+ for name in list(env):
+ if (name.startswith(('PYTHON', 'CONDA_', 'UV_', 'PIP_')) and name != 'PIP_PROXY' or name == 'VIRTUAL_ENV'
+ or name.startswith('FREEVIDEO_BOOTSTRAP_') or name in
+ ('FREEVIDEO_PYTHON', 'FREEVIDEO_FORCE_BOOTSTRAP', 'FREEVIDEO_RUNTIME_LOCK_FD',
+ 'FREEVIDEO_RUNTIME_LOCK_HANDLE')):
+ env.pop(name, None)
+ env.update(FREEVIDEO_HOME=str(root), FREEVIDEO_PROXY_MODE=proxy_mode, PYTHONPATH=str(source), PYTHONNOUSERSITE='1',
+ PYTHONUTF8='1', PYTHONIOENCODING='utf-8', NO_COLOR='1', FREEVIDEO_UI_EVENTS='1')
+ return env
diff --git a/freevideo_engine/comfy_host.py b/freevideo_engine/comfy_host.py
new file mode 100644
index 0000000..7cf3a83
--- /dev/null
+++ b/freevideo_engine/comfy_host.py
@@ -0,0 +1,166 @@
+"""Prepare an optional ComfyUI environment, never install into the user's Python."""
+import argparse
+import hashlib
+import importlib.metadata
+import json
+import os
+from pathlib import Path
+import re
+import subprocess
+import sys
+import time
+
+from . import network, processes
+from .comfy_bridge import installation
+from .comfy_environment import isolated_environment
+from .locking import runtime_lock
+from .monitoring import save
+from .package_progress import PackageOutput
+from .terminal_ui import LogProgress, TerminalUI
+
+
+def prepare(root, comfy, run=None, download=False):
+ root, comfy = Path(root).resolve(), Path(comfy).resolve()
+ _, machine = installation(environ={'FREEVIDEO_HOME': str(root)})
+ with runtime_lock(root / 'launcher' / 'host-setup.lock', inherit=False):
+ return _prepare(root, comfy, machine, run, download)
+
+
+def _prepare(root, comfy, machine, run, download):
+ source = Path(__file__).resolve().parents[1]
+ env = isolated_environment(root, source)
+ if machine.get('git'):
+ env['FREEVIDEO_GIT'] = machine['git']
+ env['PATH'] = str(Path(machine['git']).parent) + os.pathsep + env.get('PATH', '')
+ env.update(UV_LINK_MODE='hardlink', UV_CACHE_DIR=str(root / 'downloads' / 'uv-cache'), PYTHONUNBUFFERED='1')
+ directory = root / 'launcher' / 'host-runs' / (time.strftime('%Y%m%dT%H%M%S') + '-' + str(time.time_ns()))
+ directory.mkdir(parents=True, exist_ok=False)
+ try:
+ plan = json.loads((Path(machine['setup_run']) / 'plan.json').read_text(encoding='utf-8')).get('network', {})
+ except (KeyError, OSError, ValueError):
+ plan = {}
+ plan = dict(plan, quiet=True, events_path=str(directory / 'network.jsonl'),
+ download_settings_path=str(root / 'download-settings.json'))
+ ui = TerminalUI('Prepare ComfyUI', plain=True)
+ stage_offset, stage_total = int(bool(download)), 5 + int(bool(download))
+ count = 0
+ package_logs = []
+
+ def log_tag(value):
+ value = re.sub(r'[^A-Za-z0-9_.-]+', '_', str(value or 'unknown')).strip('._')
+ return value[:48] or 'unknown'
+
+ def execute(command, environment, label='Prepare ComfyUI environment'):
+ nonlocal count
+ if run:
+ return run(command, environment)
+ count += 1
+ key = 'comfy-host-' + str(count)
+ package_source = environment.get('FREEVIDEO_PACKAGE_SOURCE')
+ package_route = environment.get('FREEVIDEO_PACKAGE_ROUTE')
+ if package_source:
+ log = directory / ('%02d-command-%s-%s.log' %
+ (count, log_tag(package_source), log_tag(package_route)))
+ package_logs.append(str(log))
+ else:
+ log = directory / ('%02d-command.log' % count)
+ progress, child, success = LogProgress(log), None, False
+ ui.begin(key, label)
+ try:
+ with log.open('w', encoding='utf-8') as stream:
+ with PackageOutput(list(map(str, command)), stream, environment) as output:
+ child = processes.popen(list(map(str, command)), env=output.env,
+ cwd=comfy if comfy.is_dir() else root, stdin=subprocess.DEVNULL,
+ stdout=output.stdout, stderr=subprocess.STDOUT, supervise=True, start_new_session=True)
+ output.spawned()
+ while child.poll() is None:
+ if output.error is not None:
+ raise RuntimeError('Could not retain ComfyUI package output') from output.error
+ try:
+ child.wait(timeout=.3)
+ except subprocess.TimeoutExpired:
+ pass
+ ui.update(key, **progress.read())
+ ui.update(key, **progress.read())
+ if child.returncode:
+ with log.open('rb') as stream:
+ stream.seek(max(0, log.stat().st_size - 8192))
+ tail = stream.read().decode('utf-8', errors='replace')
+ raise RuntimeError(tail + '\nLog: ' + str(log))
+ success = True
+ finally:
+ if child is not None and child.poll() is None:
+ processes.stop(child)
+ ui.end(key, success=success, detail=None if success else 'Log: ' + str(log))
+
+ if download:
+ ui.phase('Download ComfyUI', 0, stage_total)
+ from .comfy_source import download as download_source
+ def git_run(label, command, env):
+ if 'fetch' in command:
+ command = list(command)
+ command.insert(command.index('fetch') + 1, '--progress')
+ return execute(command, env, 'Download ComfyUI' if 'fetch' in command else 'Prepare ComfyUI files')
+ download_source(comfy, plan=plan, env=env, run=git_run)
+
+ ui.phase('Create ComfyUI Python environment', stage_offset, stage_total)
+ requirements = comfy / 'requirements.txt'
+ if not requirements.is_file():
+ raise ValueError('ComfyUI requirements.txt is missing')
+ # An environment is reused only for this exact ComfyUI dependency contract.
+ identity = dict(comfy=str(comfy), requirements=hashlib.sha256(requirements.read_bytes()).hexdigest(),
+ engine_python=machine['python'], python=sys.version.split()[0])
+ key = hashlib.sha256(json.dumps(identity, sort_keys=True).encode()).hexdigest()[:16]
+ env_root = root / 'envs' / ('comfyui-' + key)
+ python = env_root / ('Scripts/python.exe' if os.name == 'nt' else 'bin/python')
+ receipt = env_root / 'freevideo-host.json'
+ if not (python.is_file() and receipt.is_file() and json.loads(receipt.read_text(encoding='utf-8')) == identity):
+ uv = root / 'tools' / ('uv.exe' if os.name == 'nt' else 'uv-x86_64-unknown-linux-gnu/uv')
+ if not uv.is_file():
+ raise ValueError('The engine download tool is missing. Run engine repair first.')
+ if not python.is_file():
+ execute([uv, 'venv', '--python', machine['python'], env_root], env, 'Create ComfyUI Python environment')
+ # Match the already validated CUDA packages. Hardlinked uv cache
+ # entries share their disk bytes without sharing a writable env.
+ pins = directory / 'cuda-constraints.txt'
+ rows = [name + '==' + importlib.metadata.version(name) for name in ('torch', 'torchvision', 'torchaudio')]
+ pins.write_text('\n'.join(rows) + '\n', encoding='utf-8')
+ torch_version = importlib.metadata.version('torch')
+ cuda = torch_version.split('+', 1)[1] if '+' in torch_version else 'cpu'
+ ui.phase('Prepare ComfyUI GPU packages', stage_offset + 1, stage_total)
+ def package_install(family, command, label):
+ try:
+ return network.package_command(plan, family, command, env)
+ except network.PackageSourcesError as error:
+ error.log_files = list(package_logs)
+ error.args = (str(error) + '. Attempt logs: ' + ', '.join(package_logs),)
+ raise
+
+ package_install('torch-' + cuda,
+ lambda environment, _: execute([uv, 'pip', 'install', '--python', python, *rows], environment, 'Prepare ComfyUI GPU packages'),
+ 'Prepare ComfyUI GPU packages')
+ ui.phase('Install ComfyUI packages', stage_offset + 2, stage_total)
+ package_install('pypi', lambda environment, _: execute(
+ [uv, 'pip', 'install', '--python', python, '-r', requirements, '-c', pins], environment, 'Install ComfyUI packages'),
+ 'Install ComfyUI packages')
+ ui.phase('Check ComfyUI dependencies', stage_offset + 3, stage_total)
+ execute([uv, 'pip', 'check', '--python', python], env, 'Check ComfyUI dependencies')
+ save(receipt, identity)
+ ui.phase('Finish ComfyUI setup', stage_offset + 4, stage_total)
+ result = dict(python=str(python), environment=str(env_root), identity=identity)
+ save(root / 'launcher' / 'comfy-host.json', result)
+ ui.phase('ComfyUI ready', stage_total, stage_total)
+ return result
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--root', type=Path, required=True)
+ parser.add_argument('--comfy', type=Path, required=True)
+ parser.add_argument('--download-comfy', action='store_true', help='Download the pinned ComfyUI application into a new folder')
+ args = parser.parse_args()
+ prepare(args.root, args.comfy, download=args.download_comfy)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freevideo_engine/comfy_launcher.py b/freevideo_engine/comfy_launcher.py
new file mode 100644
index 0000000..0900f8b
--- /dev/null
+++ b/freevideo_engine/comfy_launcher.py
@@ -0,0 +1,843 @@
+"""Small native launcher: choose ComfyUI, prepare FreeVideo, open its workflow."""
+import json
+import locale
+import os
+from pathlib import Path
+import sys
+import time
+import traceback
+import webbrowser
+from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
+
+from . import __version__
+from . import branding
+from .comfy_launcher_runtime import Controller
+from .desktop_runtime import launcher_root, materialize_source
+from .monitoring import save
+from .terminal_ui import clean, duration
+from .setup_progress import number, progress_text
+
+
+class Launcher:
+ def __init__(self, window, source=None, controller=None, update_client=None):
+ import tkinter as tk
+ from tkinter import ttk
+ self.tk, self.ttk, self.window = tk, ttk, window
+ self.source = Path(source or materialize_source())
+ self.controller = controller or Controller(self.source)
+ from .launcher_settings import Store
+ self.settings_store = Store(launcher_root(), documents=launcher_root() / 'smoke-documents' if '--smoke-test' in sys.argv else None)
+ self.settings_path = self.settings_store.primary
+ settings = self.settings_store.read()
+ self.zh = settings.get('language', (locale.getlocale()[0] or '').lower()).startswith('zh')
+ self.comfy = tk.StringVar(value=settings.get('comfy', ''))
+ self.mode = tk.StringVar(value='new' if settings.get('new_comfy', not bool(settings.get('comfy'))) else 'existing')
+ default_folder = Path(os.environ.get('USERPROFILE') or os.environ.get('HOME') or launcher_root().parent) / 'FreeVideo'
+ self.destination = tk.StringVar(value=settings.get('destination', str(default_folder)))
+ self.engine = tk.StringVar(value=os.environ.get('FREEVIDEO_HOME') or settings.get('engine', ''))
+ self.model_dirs = settings.get('model_dirs', [])
+ if not isinstance(self.model_dirs, list) or any(not isinstance(v, str) for v in self.model_dirs):
+ self.model_dirs = []
+ if isinstance(settings.get('models'), str) and settings['models'].strip():
+ self.model_dirs = list(dict.fromkeys(self.model_dirs + [settings['models']]))
+ self.models = tk.StringVar() # Legacy callers can still provide one folder.
+ self.model_method = tk.StringVar(value=settings.get('model_method', 'reuse' if self.model_dirs else 'auto'))
+ self.wizard_step = 0
+ self.launch_selection = settings.get('installation')
+ self.save_timer = None
+ self.loading_settings = False
+ self.saved_setup = settings.get('setup', {})
+ self.last_model_groups = []
+ self.error_text = ''
+ self.python = tk.StringVar(value=settings.get('python', ''))
+ self.url = tk.StringVar(value=settings.get('url', 'http://127.0.0.1:8188'))
+ self.token = tk.StringVar() # Never stored in settings, commands or receipts.
+ self.separate = tk.BooleanVar(value=settings.get('separate', False))
+ self.repair, self.accept = tk.BooleanVar(), tk.BooleanVar()
+ self.status = tk.StringVar()
+ self.details = tk.StringVar()
+ self.meta = tk.StringVar()
+ self.phase = tk.StringVar()
+ self.overall_text = tk.StringVar()
+ self.current_text = tk.StringVar()
+ self.folder_label, self.folder_hint = tk.StringVar(), tk.StringVar()
+ self.last_state = None
+ self.opened = False
+ self.browser_attempted = False
+ self.browser_error = ''
+ self.browser_address = tk.StringVar()
+ self.closing = False
+ self.started = None
+ self.labels = []
+ self.terminal_operation = None
+ self.options = None
+ self.options_inputs = []
+ self.download_dialog = None
+ self.download_probes = {}
+ if self.controller.restore(dict(settings, engine=self.engine.get())):
+ self.launch_selection = dict(self.controller.selection)
+ self.wizard_step = 3
+ elif self.saved_setup.get('action') == 'install' and self.saved_setup.get('status') in ('running', 'failed', 'cancelled'):
+ self.wizard_step = 2
+ self.controller.state = dict(status='cancelled')
+ from .launcher_view import build
+ build(self)
+ self.updates = None
+ if update_client is None and '--smoke-test' not in sys.argv:
+ from .launcher_update import current_build, UpdateClient
+ identity = current_build()
+ if identity:
+ update_client = UpdateClient(identity, launcher_root())
+ if update_client is not None:
+ from .launcher_update_ui import UpdateUI
+ self.updates = UpdateUI(self, update_client, self.update_row)
+ for variable in (self.mode, self.destination, self.comfy, self.engine, self.models, self.model_method, self.python, self.url, self.token, self.separate, self.repair):
+ variable.trace_add('write', self.changed)
+ window.protocol('WM_DELETE_WINDOW', self.close)
+ window.bind('<Destroy>', self.cancel_timers, add='+')
+ window.report_callback_exception = self.callback_error
+ window.bind('<Return>', lambda _: self.act() if 'disabled' not in self.primary.state() else None)
+ self.refresh()
+ self.poll_timer = window.after(120, self.poll)
+ self.update_timer = None
+ if self.updates:
+ self.update_timer = window.after(300, self.updates.start)
+ self.compatibility_timer = window.after(500, self.compatibility_notice)
+ self.reported_setup = None
+
+ def t(self, en, zh):
+ return zh if self.zh else en
+
+ def label(self, parent, en, zh, **kwargs):
+ widget = self.ttk.Label(parent, text=self.t(en, zh), **kwargs)
+ self.labels.append((widget, en, zh))
+ return widget
+
+ def button(self, parent, en, zh, command):
+ widget = self.ttk.Button(parent, text=self.t(en, zh), command=command)
+ self.labels.append((widget, en, zh))
+ return widget
+
+ def language(self):
+ self.zh = not self.zh
+ for widget, en, zh in self.labels:
+ if widget.winfo_exists():
+ widget.configure(text=self.t(en, zh))
+ self.refresh()
+ self.queue_save()
+
+ def values(self):
+ return dict({name: getattr(self, name).get() for name in ('comfy', 'destination', 'engine', 'models', 'python', 'url', 'token', 'separate', 'repair')},
+ new_comfy=self.mode.get() == 'new', model_dirs=list(self.model_dirs), model_method=self.model_method.get())
+
+ def model_download_link(self, component):
+ from .model_guidance import show_component
+ show_component(self, component)
+
+ def select_model_source(self, _=None):
+ from .download_settings import CHOICES, preference
+ try:
+ root = self.compatibility_root()
+ if root is None:
+ return
+ preference(root, CHOICES[self.model_source.current()])
+ self.model_source_status.configure(text=self.t('Saved. You can change sources during download too.', '已保存,下载过程中也可切换。'))
+ except (OSError, ValueError) as error:
+ self.model_source_status.configure(text=str(error))
+
+ def refresh_model_choices(self):
+ from .download_settings import CHOICES, read
+ method = self.model_method.get()
+ instructions = {
+ 'auto': ('FreeVideo selects compatible models, reuses matches in ComfyUI and downloads only missing files.',
+ '自动选择适配模型、复用 ComfyUI 内已有模型,只下载缺少的部分。'),
+ 'manual': ('Choose a component below to open its download links. After downloading, extract ZIP files and select their folder.',
+ '点击下方组件获取下载链接。下载完成后先解压 ZIP,再选择模型文件夹。'),
+ 'reuse': ('Choose your model folders. Subfolders are scanned automatically; the next page shows what is reusable and what is still missing.',
+ '选择已有模型的总目录,会自动扫描子文件夹。下一页会列出可复用和仍缺少的组件。'),
+ }
+ self.model_help.configure(text=self.t(*instructions[method]))
+ if method == 'auto':
+ self.model_source_row.pack(fill='x', pady=(8, 0), before=self.model_components)
+ else:
+ self.model_source_row.pack_forget()
+ if method == 'reuse':
+ self.model_library.pack(fill='x', pady=(12, 0))
+ self.model_reuse_button.pack_forget()
+ else:
+ self.model_library.pack_forget()
+ self.model_reuse_button.configure(text=self.t('Downloaded / already have models · choose folders…', '已下载或已有模型,选择文件夹…'))
+ self.model_reuse_button.pack(fill='x', pady=(12, 0))
+ self.model_source.configure(values=(self.t('Automatic', '自动选择'), 'Hugging Face', 'HF Mirror', 'ModelScope'))
+ try:
+ root = self.compatibility_root()
+ self.model_source.current(CHOICES.index(read(root / 'download-settings.json')['source']) if root else 0)
+ except (OSError, ValueError):
+ self.model_source.current(0)
+ for widget in self.model_method_buttons:
+ widget.state(['disabled'] if self.controller.busy else ['!disabled'])
+
+ def selected_folder(self):
+ return self.destination if self.mode.get() == 'new' else self.comfy
+
+ def changed(self, *_):
+ if self.loading_settings:
+ return
+ self.queue_save()
+ if self.controller.busy:
+ return
+ self.controller.selection = None
+ self.controller.state = dict(status='idle')
+ self.accept.set(False)
+ self.reset_browser()
+ self.last_model_groups = []
+ if self.wizard_step == 2:
+ self.wizard_step = 1
+ elif self.wizard_step == 3:
+ self.wizard_step = 0
+ self.refresh()
+
+ def browse(self, variable, file=False):
+ from tkinter import filedialog
+ parent = self.options if self.options and self.options.winfo_exists() else self.window
+ path = (filedialog.askopenfilename if file else filedialog.askdirectory)(parent=parent)
+ if path:
+ variable.set(path)
+
+ def show_options(self):
+ if self.options and self.options.winfo_exists():
+ self.options.lift(); return
+ dialog = self.tk.Toplevel(self.window); self.options = dialog
+ dialog.title(self.t('FreeVideo · Options', 'FreeVideo · 选项'))
+ dialog.transient(self.window)
+ dialog.geometry('640x620'); dialog.minsize(580, 560)
+ body = self.ttk.Frame(dialog, padding=24); body.pack(fill='both', expand=True)
+ self.options_inputs = []
+ fixed_environment = getattr(self.controller, 'fixed_environment', False)
+ for name, en, zh in (
+ ('engine', 'Engine folder (blank = automatic)', '引擎目录(留空自动选择)'),
+ ('python', 'ComfyUI Python (auto-detected when blank)', 'ComfyUI Python(留空自动检测)'),
+ ('url', 'ComfyUI address', 'ComfyUI 地址')):
+ if fixed_environment and name != 'url':
+ continue
+ self.label(body, en, zh).pack(anchor='w', pady=(0, 4))
+ row = self.ttk.Frame(body); row.pack(fill='x', pady=(0, 10))
+ entry = self.ttk.Entry(row, textvariable=getattr(self, name), show='•' if name == 'token' else '')
+ entry.pack(side='left', fill='x', expand=True)
+ self.options_inputs.append(entry)
+ if name in ('engine', 'models', 'python'):
+ browse = self.button(row, '…', '…', lambda n=name: self.browse(getattr(self, n), n == 'python'))
+ browse.pack(side='right', padx=(6, 0)); self.options_inputs.append(browse)
+ for variable, en, zh in (
+ (self.separate, 'Use a separate ComfyUI environment', '使用独立 ComfyUI 环境'),
+ (self.repair, 'Repair the engine environment', '修复引擎环境')):
+ if fixed_environment:
+ continue
+ check = self.ttk.Checkbutton(body, variable=variable, text=self.t(en, zh))
+ self.labels.append((check, en, zh)); check.pack(anchor='w', pady=(3, 3))
+ self.options_inputs.append(check)
+ self.button(body, 'Done', '完成', dialog.destroy).pack(side='bottom', anchor='e', pady=(10, 0))
+ compatibility = self.button(body, 'Compatibility settings', '兼容性设置', self.compatibility_settings)
+ compatibility.pack(side='bottom', anchor='w'); self.options_inputs.append(compatibility)
+ self.button(body, 'Download sources', '下载源', self.download_settings).pack(side='bottom', anchor='w')
+ self.label(body, 'Saved settings: ', '设置保存位置:', style='Muted.TLabel').pack(anchor='w', pady=(8, 0))
+ self.ttk.Label(body, text=str(self.settings_path), style='Muted.TLabel', wraplength=580).pack(fill='x')
+ for widget in self.options_inputs:
+ widget.state(['disabled'] if self.controller.busy else ['!disabled'])
+
+ def download_settings(self):
+ from .download_settings import Probe
+ from .download_settings_ui import DownloadSettings
+ from tkinter import messagebox
+ try:
+ selected = self.controller.selection
+ root = Path(selected['engine']) if selected else self.compatibility_root()
+ if root is None:
+ raise ValueError(self.t('Choose the installation folder first.', '请先选择安装位置。'))
+ root = root.resolve()
+ old = self.download_dialog
+ if old and old.window.winfo_exists():
+ if old.root == root:
+ old.window.lift(); return
+ old.window.destroy()
+ probe = self.download_probes.setdefault(root, Probe())
+ self.download_dialog = DownloadSettings(self.window, root, self.zh, probe,
+ token=self.token, busy=lambda: self.controller.busy)
+ except (OSError, ValueError) as error:
+ messagebox.showerror('FreeVideo', str(error), parent=self.window)
+
+ def compatibility_root(self):
+ if self.engine.get().strip():
+ return Path(self.engine.get()).expanduser()
+ if self.mode.get() == 'new':
+ return Path(self.destination.get())/'FreeVideo-engine' if self.destination.get().strip() else None
+ from .comfy_launcher_runtime import layout
+ if self.comfy.get().strip():
+ return Path(layout(self.comfy.get())['root'])/'FreeVideo-engine'
+ return None
+
+ def compatibility_notice(self):
+ from .compatibility_ui import notify
+ try:
+ root = self.compatibility_root()
+ except (OSError, ValueError):
+ return
+ if root is not None:
+ notify(self.window, root, self.zh)
+
+ def compatibility_settings(self):
+ from .compatibility_ui import open_settings
+ from tkinter import messagebox
+ try:
+ root = self.compatibility_root()
+ if root is None or not (root/'machine.json').is_file():
+ raise ValueError(self.t('Choose an installed FreeVideo engine first.', '请先选择已安装的 FreeVideo 引擎。'))
+ open_settings(self.window, root, self.zh)
+ except (OSError, ValueError) as error:
+ messagebox.showerror('FreeVideo', str(error), parent=self.window)
+
+ def save_settings(self):
+ if self.save_timer is not None:
+ self.window.after_cancel(self.save_timer)
+ self.save_timer = None
+ row = self.controller.state
+ if self.launch_selection and self.mode.get() == 'existing':
+ selected = self.launch_selection
+ if (self.comfy.get() == selected.get('root') and self.engine.get() == selected.get('engine')):
+ from .comfy_launcher_runtime import local_url
+ try:
+ selected['url'] = local_url(self.url.get())
+ if self.python.get() and Path(self.python.get()).is_file():
+ selected['python'] = self.python.get()
+ except ValueError:
+ pass # Keep an editable draft without breaking the saved launcher.
+ if row.get('action') == 'install':
+ self.saved_setup = dict(status=row.get('status', ''), action='install',
+ phase=row.get('overall', {}).get('label', ''))
+ written, failures = self.settings_store.write(self.values(), 'zh' if self.zh else 'en',
+ self.launch_selection, self.saved_setup)
+ self.settings_path = written[0]
+
+ def queue_save(self):
+ if self.save_timer is not None:
+ self.window.after_cancel(self.save_timer)
+ self.save_timer = self.window.after(200, self.persist_settings)
+
+ def persist_settings(self):
+ try:
+ self.save_settings()
+ except (OSError, ValueError) as error:
+ self.callback_error(type(error), error, error.__traceback__)
+
+ def remember_installation(self, selection):
+ if not selection or not selection.get('ready'):
+ return
+ self.launch_selection = dict(selection)
+ self.loading_settings = True
+ try:
+ for name, key in (('comfy', 'root'), ('engine', 'engine'), ('python', 'python'), ('url', 'url')):
+ getattr(self, name).set(selection.get(key) or '')
+ self.mode.set('existing')
+ self.separate.set(selection.get('separate', False))
+ self.repair.set(False)
+ finally:
+ self.loading_settings = False
+ self.persist_settings()
+
+ def show_launcher(self):
+ if self.controller.busy or not self.launch_selection:
+ return
+ if not self.controller.restore({'installation': self.launch_selection}):
+ self.controller.state = dict(status='failed', error=self.t(
+ 'The saved installation is unavailable. Return to Installation settings to locate or repair it.',
+ '保存的安装暂时不可用,请返回安装设置重新定位或修复。'))
+ self.wizard_step = 3
+ self.refresh()
+
+ def update_engine(self):
+ if self.controller.busy or not self.launch_selection:
+ return
+ self.remember_installation(self.launch_selection)
+ self.wizard_step = 2
+ self.controller.state = dict(status='idle')
+ self.act()
+
+ def add_model_folder(self, path=None):
+ if self.controller.busy:
+ return
+ if path is None:
+ from tkinter import filedialog
+ path = filedialog.askdirectory(parent=self.window, title=self.t('Choose a model folder', '选择模型总目录'))
+ if path:
+ from .local_models import library_roots
+ try:
+ self.model_dirs = library_roots(self.model_dirs + [str(path)])
+ self.model_method.set('reuse')
+ self.changed()
+ except (OSError, ValueError) as error:
+ self.status.set(str(error))
+
+ def remove_model_folders(self):
+ if self.controller.busy:
+ return
+ selected = set(self.model_list.curselection())
+ self.model_dirs = [p for i, p in enumerate(self.model_dirs) if i not in selected]
+ self.changed()
+
+ def back(self):
+ if not self.controller.busy and self.wizard_step:
+ if self.wizard_step == 3:
+ self.wizard_step = 0
+ self.controller.state = dict(status='idle')
+ else:
+ self.wizard_step -= 1
+ self.accept.set(False)
+ self.refresh()
+
+ def act(self):
+ if self.controller.busy:
+ return
+ state = self.controller.state.get('status')
+ try:
+ if self.wizard_step == 3:
+ self.terminal.show()
+ self.reset_browser()
+ self.save_settings()
+ self.controller.run('launch', {'installation': self.launch_selection})
+ self.refresh()
+ return
+ if self.wizard_step == 0:
+ from .comfy_launcher_runtime import layout, new_layout
+ if self.mode.get() == 'new':
+ new_layout(self.destination.get())
+ else:
+ layout(self.comfy.get())
+ self.wizard_step = 1
+ self.refresh()
+ return
+ self.terminal.show()
+ if self.wizard_step == 2 and state == 'review':
+ if not self.accept.get() or self.controller.state.get('errors'):
+ return
+ self.reset_browser()
+ self.controller.run('install', True)
+ elif self.wizard_step == 2 and state in ('restart-required', 'open'):
+ self.reset_browser()
+ self.controller.run('connect')
+ else:
+ if not self.selected_folder().get().strip():
+ self.browse(self.selected_folder())
+ if not self.selected_folder().get().strip():
+ return
+ values = self.values()
+ self.save_settings()
+ self.last_model_groups = []
+ self.controller.run('inspect', values)
+ self.wizard_step = 2
+ self.started = time.monotonic()
+ self.bar.stop(); self.bar.configure(mode='determinate', maximum=1, value=0)
+ self.overall_bar.stop(); self.overall_bar.configure(mode='determinate', maximum=1, value=0)
+ self.refresh()
+ except (ValueError, OSError) as error:
+ self.status.set(str(error))
+
+ def reset_browser(self):
+ self.opened = self.browser_attempted = False
+ self.browser_error = ''
+
+ def open_browser(self):
+ if self.controller.state.get('status') != 'open':
+ return
+ from .windows_ux import open_browser
+ self.browser_attempted = True
+ self.browser_error = ''
+ address = self.controller.state['url']
+ if self.zh:
+ parsed = urlsplit(address)
+ query = dict(parse_qsl(parsed.query, keep_blank_values=True))
+ query['freevideo_lang'] = 'zh'
+ address = urlunsplit((parsed.scheme, parsed.netloc, parsed.path,
+ urlencode(query), parsed.fragment))
+ try:
+ self.opened = bool(open_browser(address))
+ if not self.opened:
+ raise OSError(self.t('The system did not accept the browser request.', '系统未接受打开浏览器的请求。'))
+ except Exception as error:
+ # Browser failures must not terminate the GUI poll loop or claim
+ # the server failed. Keep the URL and a one-click manual retry.
+ self.opened = False
+ self.browser_error = self.t('ComfyUI is ready, but the browser did not open. Use Open browser or copy the address.\n',
+ 'ComfyUI 已就绪,但浏览器未能打开。请点击“打开浏览器”,或复制地址手动打开。\n') + str(error)
+ self.refresh()
+
+ def copy_browser_address(self):
+ self.window.clipboard_clear()
+ self.window.clipboard_append(self.browser_address.get())
+
+ def create_shortcut(self):
+ if self.controller.busy or not self.controller.selection:
+ return
+ self.controller.run('shortcut', self.controller.state.get('status'))
+ self.refresh()
+
+ def refresh(self):
+ from .launcher_view import show_page
+ row = self.controller.state
+ state, busy = row.get('status', 'idle'), self.controller.busy
+ fresh = self.mode.get() == 'new'
+ self.entry.configure(textvariable=self.selected_folder())
+ self.folder_label.set(self.t('Install location', '安装位置') if fresh else self.t('Your ComfyUI folder', 'ComfyUI 安装目录'))
+ self.folder_hint.set(self.t('ComfyUI, Python and FreeVideo install here.', 'ComfyUI、Python 和 FreeVideo 都安装在这里。') if fresh else
+ self.t('Your ComfyUI folder, or its portable package folder.', '选择 ComfyUI 或整合包所在文件夹。'))
+ headings = [('Set up FreeVideo', '开始使用 FreeVideo'), ('Get your models', '准备模型'),
+ ('Ready to install', '准备安装'), ('FreeVideo', 'FreeVideo')]
+ # One line each. What a control does is stated once, next to the control.
+ descriptions = [('Choose how you want to use ComfyUI.', '选择 ComfyUI 的使用方式。'),
+ ('Download automatically, download yourself, or reuse existing models.', '自动下载、手动下载,或复用已有模型。'),
+ ('Review, then install.', '确认后开始安装。'),
+ ('Start ComfyUI and open the FreeVideo workflow.', '启动 ComfyUI 并打开 FreeVideo 工作流。')]
+ if self.wizard_step == 3:
+ descriptions[3] = {
+ 'running': ('Starting ComfyUI…', '正在启动 ComfyUI…'),
+ 'open': ('ComfyUI is ready. Open FreeVideo in your browser.', 'ComfyUI 已就绪,可在浏览器中打开 FreeVideo。'),
+ 'failed': ('Could not start. Details below.', '启动未完成,原因见下方。'),
+ 'restart-required': ('Restart your running ComfyUI, then connect again.', '请重启正在运行的 ComfyUI,然后重新连接。'),
+ 'cancelled': ('Startup stopped. Your installation is retained.', '已停止启动,安装文件已保留。'),
+ }.get(state, descriptions[3])
+ if busy and row.get('action') == 'shortcut':
+ descriptions[3] = ('Creating desktop shortcut…', '正在创建桌面快捷方式…')
+ if self.wizard_step == 2:
+ headings[2], descriptions[2] = {
+ 'running': (('Finding your models', '正在查找模型'), ('Scanning folders…', '正在扫描模型目录…')) if row.get('action') == 'inspect' else
+ (('Installing FreeVideo', '正在安装 FreeVideo'), ('Preparing models and dependencies.', '正在准备模型和运行环境。')),
+ 'failed': (('Installation paused', '安装已暂停'), ('Files retained. Details below.', '文件已保留,原因见下方。')),
+ 'cancelled': (('Installation paused', '安装已暂停'), ('Downloaded files are reused.', '已下载文件会复用。')),
+ 'open': (('You’re ready', '准备完成'), ('ComfyUI is ready. Open FreeVideo in your browser.', 'ComfyUI 已就绪,可在浏览器中打开 FreeVideo。')),
+ 'restart-required': (('One last step', '最后一步'), ('Restart ComfyUI, then connect below.', '请重启 ComfyUI,然后点击下方连接。')),
+ }.get(state, (headings[2], descriptions[2]))
+ self.heading.configure(text=self.t(*headings[self.wizard_step]))
+ self.status.set(self.t(*descriptions[self.wizard_step]))
+ self.model_list.delete(0, 'end')
+ for folder in self.model_dirs:
+ self.model_list.insert('end', folder)
+ # As tall as the folders it holds, so an empty or short list is not
+ # five rows of blank well.
+ self.model_list.configure(height=max(2, min(6, len(self.model_dirs))))
+ self.error_text = '\n'.join(str(e) for e in row.get('errors', []))
+ if row.get('error'):
+ self.error_text += ('\n' if self.error_text else '') + str(row['error'])
+ if self.browser_error:
+ self.error_text += ('\n' if self.error_text else '') + self.browser_error
+ if row.get('shortcut', {}).get('status') == 'failed':
+ self.error_text += ('\n' if self.error_text else '') + self.t(
+ row['shortcut']['error'], '安装已完成,但无法创建桌面快捷方式。可继续使用启动器。\n' + row['shortcut']['error'])
+ if self.error_text:
+ self.copy_error_button.pack(side='left', padx=6)
+ self.review_card.pack(fill='x', pady=12)
+ else:
+ self.copy_error_button.pack_forget(); self.review_card.pack_forget()
+ self.review.configure(state='normal'); self.review.delete('1.0', 'end')
+ self.review.insert('1.0', self.error_text)
+ # Only as tall as the retained text, so a one-line failure is one line.
+ self.review.configure(height=min(4, max(1, len(self.error_text.splitlines()))), state='disabled')
+ self.launch_error.configure(text=self.error_text)
+ if row.get('engine_update_available'):
+ self.engine_update_row.pack(fill='x', pady=(12, 0))
+ else:
+ self.engine_update_row.pack_forget()
+ self.engine_update_button.state(['disabled'] if busy else ['!disabled'])
+ selected = self.launch_selection or {}
+ self.launch_paths.configure(text='ComfyUI · ' + selected.get('root', '') + '\n' +
+ self.t('Engine', '引擎') + ' · ' + selected.get('engine', ''))
+ self.browser_address.set(row.get('url', '') if state == 'open' else '')
+ if state == 'open':
+ self.browser_controls.pack(fill='x', pady=(16, 0))
+ else:
+ self.browser_controls.pack_forget()
+ self.browser_button.state(['disabled'] if busy else ['!disabled'])
+ self.shortcut_button.state(['!disabled'] if not busy and self.controller.selection and
+ self.controller.selection.get('ready') else ['disabled'])
+ shortcut = row.get('shortcut', {})
+ self.shortcut_status.configure(text=self.t('Desktop shortcut ready', '桌面快捷方式已就绪')
+ if shortcut.get('status') in ('created', 'present') else '')
+ if self.wizard_step == 3:
+ action = self.t('Start & open FreeVideo', '启动并打开 FreeVideo')
+ elif self.wizard_step < 2:
+ action = self.t('Next →', '下一步 →') if self.wizard_step == 0 else self.t('Scan & continue →', '扫描并继续 →')
+ else:
+ action = self.t('Install & open ComfyUI', '安装并打开 ComfyUI') if state == 'review' else self.t('Connect', '连接') if state == 'restart-required' else self.t('Open FreeVideo', '打开 FreeVideo') if state == 'open' else self.t('Check & resume', '检查并继续')
+ self.primary.configure(text=action)
+ if busy:
+ self.primary.configure(text=self.t('Starting…', '启动中…') if self.wizard_step == 3 else
+ self.t('Checking…', '检查中…') if row.get('action') == 'inspect' else self.t('Installing…', '安装中…'))
+ allowed = not busy and (self.wizard_step != 2 or state != 'review' or (self.accept.get() and not row.get('errors')))
+ self.primary.state(['!disabled'] if allowed else ['disabled'])
+ self.back_button.state(['disabled'] if busy or self.wizard_step == 0 else ['!disabled'])
+ self.back_button.configure(text=self.t('Installation settings', '安装设置') if self.wizard_step == 3 else self.t('Back', '上一步'))
+ if self.launch_selection and self.wizard_step != 3:
+ self.launcher_button.pack(side='left', padx=12)
+ self.launcher_button.state(['disabled'] if busy else ['!disabled'])
+ else:
+ self.launcher_button.pack_forget()
+ self.stop_button.configure(text=self.t('Stop startup', '停止启动') if self.wizard_step == 3 else self.t('Pause installation', '暂停安装'))
+ for widget in (self.entry, self.browse_button, self.add_model_button, self.remove_model_button, *self.mode_buttons, *self.options_inputs):
+ if widget.winfo_exists():
+ widget.state(['disabled'] if busy else ['!disabled'])
+ if busy:
+ self.stop_button.pack(side='right', padx=(0, 10))
+ else:
+ self.stop_button.pack_forget()
+ self.consent.configure(text=self.t('I accept the installation plan and model / toolkit licenses.', '我同意本安装计划及模型/工具许可证。'))
+ if state == 'review' and not busy and self.wizard_step == 2:
+ self.consent.pack(anchor='w', pady=(0, 12), before=self.update_row)
+ else:
+ self.consent.pack_forget()
+ plan = row.get('plan', {})
+ hardware = plan.get('inventory', {}).get('hardware', {})
+ summary = []
+ if hardware.get('gpu_name'):
+ summary.append(hardware['gpu_name'])
+ if 'model_download_bytes' in plan:
+ summary.append(self.t('To download ', '需下载 ') + '%.1f GiB' % (plan['model_download_bytes']/2**30))
+ if state == 'review' and row.get('disks'):
+ needed = sum(d.get('needed_bytes', 0) for d in row['disks'])
+ summary.append(self.t('Peak disk space ~', '预计磁盘峰值 ~') + '%.1f GiB' % (needed/2**30))
+ self.summary.configure(text=' · '.join(summary))
+ if state == 'review':
+ self.progress_card.pack_forget()
+ elif not self.progress_card.winfo_manager():
+ self.progress_card.pack(fill='x', before=self.review_card if self.review_card.winfo_manager() else None)
+ self.render_models(row)
+ self.refresh_model_choices()
+ show_page(self)
+
+ def render_models(self, row):
+ groups = row.get('task', {}).get('model_groups') or row.get('model_groups')
+ if groups:
+ self.last_model_groups = groups
+ groups = self.last_model_groups or row.get('plan', {}).get('model_groups', [])
+ by_name = {item['id']: item for item in groups}
+ ready = row.get('selection', {}).get('ready') or row.get('status') == 'open'
+ for name, card in self.model_cards.items():
+ card.update(by_name.get(name), ready=ready)
+
+ def copy_error(self):
+ if self.error_text:
+ self.window.clipboard_clear()
+ self.window.clipboard_append(self.error_text)
+
+ def show_details(self):
+ dialog = self.tk.Toplevel(self.window); dialog.title(self.t('FreeVideo · Details', 'FreeVideo · 详情'))
+ dialog.configure(bg=branding.BACKGROUND)
+ dialog.geometry('720x440')
+ text = self.tk.Text(dialog, wrap='word', padx=16, pady=16, font=('Consolas', 10),
+ bg=branding.SURFACE, fg=branding.TEXT, relief='flat',
+ selectbackground=branding.RAISED, selectforeground=branding.TEXT)
+ text.pack(fill='both', expand=True)
+ row = self.controller.state
+ safe = {k: v for k, v in row.items() if k != 'host'}
+ text.insert('1.0', json.dumps(safe, ensure_ascii=False, indent=2)); text.configure(state='disabled')
+ self.button(dialog, 'Model licenses', '模型许可证', lambda: webbrowser.open('https://huggingface.co/OpenVDN/vdn-minimax-h3-edge/blob/main/LICENSE')).pack(pady=8)
+
+ def poll(self):
+ if self.poll_timer is not None:
+ self.window.after_cancel(self.poll_timer)
+ self.poll_timer = None
+ row = self.controller.state
+ if self.controller.busy and row.get('action') != 'shortcut' and id(self.controller.thread) != self.terminal_operation:
+ self.terminal_operation = id(self.controller.thread)
+ self.terminal.show()
+ self.terminal.poll()
+ task = row.get('task', {})
+ progress = task.get('progress', {})
+ self.render_progress(row, task, progress)
+ self.render_models(row)
+ if not self.controller.busy:
+ self.bar.stop()
+ if self.closing:
+ self.controller.close()
+ self.window.destroy(); return
+ selected = row.get('selection')
+ if (selected and selected.get('ready') and
+ (row.get('deployed') or row.get('status') in ('open', 'restart-required')) and
+ row.get('status') != 'running'):
+ if selected != self.launch_selection:
+ self.remember_installation(selected)
+ if row.get('action') == 'install' or row.get('status') in ('open', 'restart-required'):
+ self.wizard_step = 3
+ if row.get('status') == 'open':
+ self.bar.configure(mode='determinate', maximum=1, value=1)
+ if not self.browser_attempted:
+ self.open_browser()
+ # Include busy separately: the task's final state arrives just before
+ # the thread exits, and the action must become enabled at that boundary.
+ key = (row.get('status'), self.controller.busy, row.get('error'), id(row.get('plan')), row.get('overall', {}).get('label'))
+ if key != self.last_state:
+ self.last_state = key
+ if row.get('action') == 'install':
+ self.queue_save()
+ self.refresh()
+ if row.get('action') == 'install' and not self.controller.busy and self.started and row.get('status') in ('open','failed','cancelled','restart-required'):
+ event_key = 'setup-%s-%s' % (self.started, row['status'])
+ if event_key != self.reported_setup:
+ self.reported_setup = event_key
+ from .installation_diagnostics import write
+ write(self.compatibility_root(), time.time()-(time.monotonic()-self.started),
+ {'summary': {'status': 'complete' if row['status'] in ('open','restart-required') else 'incomplete',
+ 'hardware': row.get('plan', {}).get('inventory', {}).get('hardware', {}),
+ 'stages': [{'stage':'installation', 'seconds':time.monotonic()-self.started}]},
+ 'request': {'exception': row.get('exception', []), 'phase': self.controller.section or 'installation'},
+ 'log_tails': {'installation': row.get('error', '')}})
+ self.poll_timer = self.window.after(120, self.poll)
+
+ def render_progress(self, row, task, progress):
+ from .launcher_copy import display
+ overall = row.get('overall') or task.get('phase_progress') or dict(done=0, total=1)
+ total, done = overall.get('total'), overall.get('done')
+ valid = number(total) and total > 0 and number(done) and done <= total
+ if valid:
+ self.overall_bar.stop()
+ self.overall_bar.configure(mode='determinate', maximum=total, value=done)
+ self.overall_text.set('%d / %d ' % (done, total) + self.t('stages', '阶段') +
+ ' · %.0f%%' % (100 * done / total))
+ else:
+ # An unmeasured plan sweeps rather than showing an empty track.
+ self.overall_bar.start()
+ self.overall_text.set('')
+ label = clean(progress.get('label') or overall.get('label') or self.t('Checking configuration', '正在检查配置'))
+ self.phase.set(display(label, self.zh))
+ self.current_text.set(progress_text(progress, self.zh))
+ total, done = progress.get('total'), progress.get('done')
+ valid = number(total) and total > 0 and number(done) and done <= total
+ self.bar.stop()
+ self.bar.configure(mode='determinate', maximum=total if valid else 1, value=done if valid else 0)
+ details = [display(progress.get('detail', ''), self.zh)]
+ others = [display(p.get('label', ''), self.zh) for p in task.get('active_tasks', [])[1:]]
+ if others:
+ details.append('+ ' + ' / '.join(others[:2]))
+ self.details.set(' · '.join(p for p in details if p))
+ if self.started:
+ self.meta.set(self.t('Elapsed ', '已用 ') + duration(time.monotonic() - self.started))
+
+ def close(self):
+ try:
+ self.persist_settings()
+ finally:
+ self.controller.close()
+ for timer in (self.update_timer, self.compatibility_timer):
+ if timer is not None:
+ self.window.after_cancel(timer)
+ if self.updates:
+ self.updates.stop()
+ if self.controller.busy:
+ self.closing = True
+ self.controller.cancel()
+ self.status.set(self.t('Stopping installation; retaining files…', '正在停止安装,保留文件…'))
+ else:
+ if self.poll_timer is not None:
+ self.window.after_cancel(self.poll_timer)
+ self.poll_timer = None
+ self.bar.stop()
+ self.window.destroy()
+
+ def cancel_timers(self, event):
+ if event.widget != self.window:
+ return
+ self.controller.close()
+ for name in ('poll_timer', 'save_timer', 'compatibility_timer', 'update_timer', 'layout_timer'):
+ timer = getattr(self, name, None)
+ if timer is not None:
+ try:
+ self.window.after_cancel(timer)
+ except self.tk.TclError:
+ pass
+ setattr(self, name, None)
+
+ def callback_error(self, kind, value, trace):
+ path = launcher_root() / 'errors' / ('launcher-' + str(time.time_ns()) + '.log')
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(''.join(traceback.format_exception(kind, value, trace)), encoding='utf-8')
+ self.status.set(str(value))
+
+
+def legacy_main():
+ if '--smoke-test' not in sys.argv:
+ from .launcher_update import current_build, forward_approved
+ identity = current_build()
+ if identity and forward_approved(identity, launcher_root()):
+ return
+ import tkinter as tk
+ if os.name == 'nt':
+ import ctypes
+ try:
+ ctypes.windll.shcore.SetProcessDpiAwareness(2)
+ except (AttributeError, OSError):
+ pass
+ lib = ctypes.windll.kernel32
+ lib.GetConsoleWindow.restype = ctypes.c_void_p
+ if not lib.GetConsoleWindow():
+ lib.AllocConsole(); ctypes.windll.user32.ShowWindow(ctypes.c_void_p(lib.GetConsoleWindow()), 0)
+ window = tk.Tk()
+ app = Launcher(window)
+ if '--smoke-test' in sys.argv:
+ import queue
+ from .desktop_runtime import Runner, check_launcher_payload, check_launcher_reopen
+ destination = Path(sys.argv[sys.argv.index('--smoke-test') + 1])
+ destination.mkdir(parents=True, exist_ok=True)
+ try:
+ payload = check_launcher_payload(app.source, destination)
+ payload['reopen'] = check_launcher_reopen(destination)
+ for step in range(len(app.pages)):
+ app.wizard_step = step
+ app.refresh(); window.update_idletasks()
+ if not app.pages[step].winfo_ismapped():
+ raise ValueError('Packaged setup wizard page did not open')
+ payload['wizard_pages'] = 3
+ payload['launcher_page'] = True
+ app.wizard_step = 0
+ app.refresh()
+ # Exercise frozen-only imports before any real installation exists.
+ app.engine.set(str(destination / 'engine'))
+ app.download_settings()
+ if app.download_dialog is None or not app.download_dialog.window.winfo_exists():
+ raise ValueError('Packaged download controls could not open')
+ payload['download_controls'] = True
+ app.download_dialog.window.destroy()
+ from .launcher_update import current_build
+ build = current_build()
+ if getattr(sys, 'frozen', False) and build is None:
+ raise ValueError('Packaged launcher update identity is missing')
+ except Exception:
+ save(destination / 'smoke.json', dict(success=False, error=traceback.format_exc(),
+ interface='ComfyUI launcher', model_modules_imported=[]))
+ window.destroy()
+ return
+ events = queue.Queue(); runner = Runner(app.source, events, destination / 'help')
+ runner.start('help', destination / 'engine', ['--help'])
+ deadline = time.monotonic() + 240
+ def finish():
+ window.update_idletasks()
+ if runner.busy and time.monotonic() < deadline:
+ window.after(100, finish); return
+ result = None
+ while not events.empty():
+ kind, value = events.get_nowait()
+ if kind == 'done': result = value
+ imported = [p for p in ('torch', 'triton', 'transformers') if p in sys.modules]
+ save(destination / 'smoke.json', dict(success=bool(result and result['status'] == 'complete' and not imported),
+ task=result, interface='ComfyUI launcher', window_geometry=window.geometry(),
+ build=build,
+ source=str(app.source), payload=payload, model_modules_imported=imported))
+ if runner.busy: runner.cancel()
+ window.destroy()
+ window.after(100, finish)
+ window.mainloop()
+
+
+def main():
+ from .modern_launcher import main as launch
+ launch()
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freevideo_engine/comfy_launcher_api.py b/freevideo_engine/comfy_launcher_api.py
new file mode 100644
index 0000000..71d9ecb
--- /dev/null
+++ b/freevideo_engine/comfy_launcher_api.py
@@ -0,0 +1,34 @@
+"""Read-only launcher handshake and the bundled default workflow."""
+import json
+from pathlib import Path
+
+
+def register():
+ from aiohttp import web
+ import folder_paths
+ from server import PromptServer
+ from .comfy_bridge import source_root, installation_root
+ server = PromptServer.instance
+ if server is None or getattr(server, '_freevideo_launcher', None):
+ return
+ # Capture the loaded source now. Replacing files does not make an old live
+ # server eligible for opening a new-version workflow without restarting.
+ source = source_root().resolve()
+ root = Path(folder_paths.__file__).resolve().parent
+ info = dict(protocol=1, source=str(source), engine_root=str(installation_root(source)),
+ comfy_root=str(root), data_root=str(Path(folder_paths.base_path).resolve()))
+ from .comfy_console import install
+ try:
+ info['console_log'] = str(install(info['engine_root']))
+ except (OSError, ValueError) as error:
+ info['console_error'] = 'Could not capture ComfyUI output: ' + str(error)
+ server._freevideo_launcher = info
+
+ @server.routes.get('/freevideo/launcher')
+ async def launcher(request):
+ return web.json_response(info)
+
+ @server.routes.get('/freevideo/launcher/workflow')
+ async def workflow(request):
+ value = json.loads((source / 'example_workflows' / 'FreeVideo-All-in-One.json').read_text(encoding='utf-8'))
+ return web.json_response(value)
diff --git a/freevideo_engine/comfy_launcher_runtime.py b/freevideo_engine/comfy_launcher_runtime.py
new file mode 100644
index 0000000..2af41f6
--- /dev/null
+++ b/freevideo_engine/comfy_launcher_runtime.py
@@ -0,0 +1,596 @@
+"""ComfyUI launcher control plane. No Torch, package installs in the host, or models.
+
+The existing Setup service owns the engine plan, approval, downloads and repair.
+Only our node entry point and our template are installed in the selected ComfyUI.
+An existing server is never stopped by this launcher.
+"""
+import hashlib
+import json
+import os
+from pathlib import Path
+import secrets
+import shutil
+import socket
+import subprocess
+import sys
+import threading
+import time
+from types import SimpleNamespace
+from urllib.parse import urlsplit, urlunsplit
+from urllib.request import ProxyHandler, Request, build_opener
+
+from .comfy_bridge import installation
+from .comfy_environment import isolated_environment
+from .comfy_setup import Setup, SetupRunner
+from .comfy_source import SOURCE_DISK_BYTES, new_layout, validate_target
+from .desktop_runtime import materialize_source
+from .monitoring import save
+from . import processes
+
+PROTOCOL = 1
+TEMPLATE = 'FreeVideo-All-in-One.json'
+
+
+def disk_review(plan, engine, comfy, *, separate, new_comfy):
+ """Add frontend costs to the right volume, including cross-drive installs."""
+ def existing(path):
+ path = Path(path)
+ while not path.exists() and path != path.parent:
+ path = path.parent
+ return path
+ disks = {}
+ for disk in plan.get('disks', []):
+ parent = existing(disk['paths'][0])
+ disks[parent.stat().st_dev] = dict(disk, paths=list(disk['paths']))
+ extra = 0
+ for path, amount in ((engine, 12 * 2**30 if separate else 0), (comfy, SOURCE_DISK_BYTES if new_comfy else 0)):
+ if not amount:
+ continue
+ parent = existing(path)
+ disk = disks.setdefault(parent.stat().st_dev, dict(paths=[str(parent)], needed_bytes=0))
+ disk['free_bytes'] = shutil.disk_usage(parent).free
+ disk['needed_bytes'] += amount
+ extra += amount
+ errors = ['Insufficient disk space for ComfyUI and the engine: ' + ', '.join(d['paths'])
+ for d in disks.values() if d['needed_bytes'] > d['free_bytes']]
+ return extra, list(disks.values()), errors
+
+
+def layout(path):
+ selected = Path(path).expanduser().resolve()
+ root = selected / 'ComfyUI' if (selected / 'ComfyUI' / 'main.py').is_file() else selected
+ if not (root / 'main.py').is_file() or not (root / 'folder_paths.py').is_file():
+ raise ValueError('Select the ComfyUI folder, or the portable folder containing ComfyUI.')
+ if not (root / 'comfy_api' / 'latest').is_dir():
+ raise ValueError('This ComfyUI is too old for native VIDEO / V3 nodes. Update ComfyUI first; its files have not been changed.')
+ candidates = []
+ for parent in (root, root.parent):
+ for name in ('python_embeded', 'python_embedded', '.venv', 'venv', 'env'):
+ directory = parent / name
+ candidates.extend([directory / 'python.exe', directory / 'Scripts/python.exe', directory / 'bin/python'])
+ python = next((p.resolve() for p in candidates if p.is_file()), None)
+ return dict(root=str(root), python=str(python) if python else None,
+ portable=bool(python and python.parent.name in ('python_embeded', 'python_embedded')))
+
+
+def managed_python(engine, comfy):
+ """Find our previously completed frontend without requiring its full disk budget again."""
+ try:
+ record = json.loads((engine / 'launcher/comfy-host.json').read_text(encoding='utf-8'))
+ identity = record['identity']
+ environment, python = Path(record['environment']), Path(record['python'])
+ machine = json.loads((engine / 'machine.json').read_text(encoding='utf-8'))
+ if (identity['comfy'] != str(comfy) or identity['engine_python'] != machine['python']
+ or identity['requirements'] != hashlib.sha256((comfy / 'requirements.txt').read_bytes()).hexdigest()
+ or environment.resolve().parent != (engine / 'envs').resolve()
+ or python != environment / ('Scripts/python.exe' if os.name == 'nt' else 'bin/python')
+ or json.loads((environment / 'freevideo-host.json').read_text(encoding='utf-8')) != identity
+ or not python.is_file()):
+ return None
+ return str(python)
+ except (OSError, ValueError, KeyError, TypeError):
+ return None
+
+
+# Run with the host's Python, without importing Torch or creating __pycache__.
+# Reading the same folder registry supports extra_model_paths.yaml correctly.
+HOST_PROBE = r'''
+import importlib.util, json, pathlib, sys
+root = pathlib.Path(sys.argv[1]); sys.path.insert(0, str(root))
+needed = ('aiohttp', 'yaml', 'numpy', 'torch', 'safetensors', 'comfyui_frontend_package')
+missing = [n for n in needed if importlib.util.find_spec(n) is None]
+libraries = [str(root / 'models')]; error = None
+try:
+ import folder_paths
+ from utils.extra_config import load_extra_path_config
+ extra = root / 'extra_model_paths.yaml'
+ if extra.is_file(): load_extra_path_config(str(extra))
+ for name in ('diffusion_models', 'unet', 'checkpoints', 'text_encoders', 'clip', 'vae'):
+ try: libraries.extend(folder_paths.get_folder_paths(name))
+ except KeyError: pass
+except Exception as e: error = str(e)
+print('FREEVIDEO_HOST=' + json.dumps(dict(python=sys.executable, version=list(sys.version_info[:2]), missing=missing, libraries=list(dict.fromkeys(libraries)), library_error=error)))
+'''
+
+
+def probe_host(descriptor, python=None):
+ python = python or descriptor.get('python')
+ if not python:
+ return dict(ready=False, python=None, libraries=[str(Path(descriptor['root']) / 'models')],
+ reason='No ComfyUI Python found; prepare a separate environment.')
+ env = dict(os.environ, PYTHONUTF8='1', PYTHONDONTWRITEBYTECODE='1')
+ # SystemRoot and Windows crypto variables must survive even an isolated test.
+ for name in ('PYTHONPATH', 'PYTHONHOME', 'VIRTUAL_ENV', 'CONDA_PREFIX'):
+ env.pop(name, None)
+ try:
+ from .windows_ux import external_python
+ with external_python():
+ result = subprocess.run([str(python), '-I', '-B', '-c', HOST_PROBE, descriptor['root']],
+ cwd=descriptor['root'], env=env, capture_output=True, text=True,
+ encoding='utf-8', errors='replace', timeout=45,
+ **({'creationflags': subprocess.CREATE_NO_WINDOW} if os.name == 'nt' else {}))
+ line = next(s for s in reversed(result.stdout.splitlines()) if s.startswith('FREEVIDEO_HOST='))
+ data = json.loads(line.split('=', 1)[1])
+ data['ready'] = result.returncode == 0 and not data['missing'] and tuple(data['version']) >= (3, 10)
+ return data
+ except (OSError, subprocess.SubprocessError, ValueError, StopIteration) as error:
+ return dict(ready=False, python=str(python), libraries=[str(Path(descriptor['root']) / 'models')],
+ reason='ComfyUI Python could not start: ' + str(error))
+
+
+def local_url(value):
+ value = value.strip() or 'http://127.0.0.1:8188'
+ parsed = urlsplit(value if '://' in value else 'http://' + value)
+ if (parsed.scheme != 'http' or parsed.hostname not in ('localhost', '127.0.0.1', '::1')
+ or parsed.username or parsed.password or parsed.query or parsed.fragment or parsed.path not in ('', '/')):
+ raise ValueError('Use a local ComfyUI address, for example http://127.0.0.1:8188.')
+ port = parsed.port or 80
+ if not 1 <= port <= 65535:
+ raise ValueError('Invalid ComfyUI port')
+ return urlunsplit(('http', parsed.netloc, '', '', ''))
+
+
+def get_json(url, timeout=2):
+ # Proxy settings must never send loopback requests to an external proxy.
+ with build_opener(ProxyHandler({})).open(Request(url, headers={'Accept': 'application/json'}), timeout=timeout) as reply:
+ value = reply.read(2 * 1024 * 1024 + 1)
+ if len(value) > 2 * 1024 * 1024:
+ raise ValueError('ComfyUI response is too large')
+ return json.loads(value)
+
+
+def server_info(url):
+ try:
+ value = get_json(url + '/freevideo/launcher')
+ if (isinstance(value, dict) and value.get('protocol') == PROTOCOL
+ and all(isinstance(value.get(k), str) and value[k] for k in ('source', 'engine_root', 'comfy_root'))):
+ return dict(value, status='freevideo')
+ except (OSError, ValueError):
+ pass
+ try:
+ value = get_json(url + '/system_stats')
+ if isinstance(value, dict) and 'system' in value:
+ return dict(status='restart-required')
+ except (OSError, ValueError):
+ pass
+ return dict(status='offline')
+
+
+def matches_server(info, root, engine, source):
+ return (info.get('status') == 'freevideo'
+ and all(info.get(k) and Path(info[k]).resolve() == Path(p).resolve()
+ for k, p in (('comfy_root', root), ('engine_root', engine), ('source', source))))
+
+
+def node_target(root):
+ parent = Path(root) / 'custom_nodes'
+ found = []
+ if parent.is_dir():
+ for path in parent.iterdir():
+ if path.name.endswith('.disabled') or not path.is_dir():
+ continue
+ code = path / 'freevideo_engine' / 'comfy_nodes.py'
+ if (path / 'freevideo-launcher.json').is_file() or (code.is_file() and 'FreeVideoGenerate' in code.read_text(encoding='utf-8')):
+ found.append(path)
+ if len(found) > 1:
+ raise ValueError('Multiple FreeVideo node installations found. Keep one enabled before installing.')
+ target = found[0] if found else parent / 'FreeVideo'
+ if target.exists() and not found and any(target.iterdir()):
+ raise ValueError('custom_nodes/FreeVideo already contains unrelated files. Choose another ComfyUI or rename that folder first.')
+ return target
+
+
+def deploy(root, source, engine):
+ """Update only our entry point; preserve source edits, libraries and templates."""
+ root, source, engine = map(lambda p: Path(p).resolve(), (root, source, engine))
+ target = node_target(root)
+ target.mkdir(parents=True, exist_ok=True)
+ receipt = target / 'freevideo-launcher.json'
+ previous = json.loads(receipt.read_text(encoding='utf-8')) if receipt.is_file() else {}
+ entry = target / '__init__.py'
+ old = entry.read_bytes() if entry.is_file() else None
+ if old and previous.get('entry_sha256') and hashlib.sha256(old).hexdigest() != previous['entry_sha256']:
+ raise ValueError('The managed FreeVideo entry point was edited. Changes are retained; restore it before updating.')
+ # The small loader may be replaced; all original checkout files remain intact.
+ code = ("# Installed by FreeVideo launcher. Original entry points are retained in the engine launcher/backups folder.\n"
+ "import importlib.util as _util\nimport json as _json\nfrom pathlib import Path as _Path\nimport sys as _sys\n"
+ "_record = _json.loads((_Path(__file__).parent / 'freevideo-launcher.json').read_text(encoding='utf-8'))\n"
+ "_source = _Path(_record['source'])\n"
+ "_spec = _util.spec_from_file_location(__name__ + '._engine', _source / '__init__.py', submodule_search_locations=[str(_source)])\n"
+ "_engine = _util.module_from_spec(_spec)\n_sys.modules[_spec.name] = _engine\n_spec.loader.exec_module(_engine)\n"
+ "WEB_DIRECTORY = str(_source / 'web')\ncomfy_entrypoint = _engine.comfy_entrypoint\n").encode()
+ if old and old != code:
+ backup = engine / 'launcher' / 'backups' / (target.name + '-' + hashlib.sha256(old).hexdigest()[:12] + '-init.py')
+ backup.parent.mkdir(parents=True, exist_ok=True)
+ if not backup.exists():
+ backup.write_bytes(old)
+ save(source / 'comfyui.json', dict(installation=str(engine)))
+ save(receipt, dict(protocol=PROTOCOL, source=str(source), engine_root=str(engine), entry_sha256=hashlib.sha256(code).hexdigest()))
+ temporary = entry.with_suffix('.py.launcher-tmp')
+ temporary.write_bytes(code)
+ os.replace(temporary, entry)
+ template = (source / 'example_workflows' / TEMPLATE).read_bytes()
+ digest = hashlib.sha256(template).hexdigest()[:12]
+ saved = []
+ for parent in (target / 'example_workflows', root / 'user' / 'default' / 'workflows' / 'FreeVideo'):
+ parent.mkdir(parents=True, exist_ok=True)
+ file = parent / TEMPLATE
+ if file.exists() and file.read_bytes() != template:
+ file = parent / ('FreeVideo-All-in-One-' + digest + '.json')
+ if not file.exists():
+ file.write_bytes(template)
+ saved.append(str(file))
+ return dict(node=str(target), workflows=saved)
+
+
+class LauncherRunner(SetupRunner):
+ def command(self, root, arguments):
+ if arguments and arguments[0] == 'comfy-host':
+ _, machine = installation(self.source, {'FREEVIDEO_HOME': str(root)})
+ code = "import runpy,sys;sys.path.insert(0,sys.argv.pop(1));runpy.run_module('freevideo_engine.comfy_host',run_name='__main__')"
+ return [machine['python'], '-B', '-c', code, str(self.source), '--root', str(root), *arguments[1:]]
+ return super().command(root, arguments)
+
+
+class Controller:
+ def __init__(self, source, *, child_environment=None):
+ self.source = Path(source)
+ self.child_environment = child_environment
+ self.state = dict(status='idle')
+ self.selection = self.setup = None
+ self.thread = None
+ self.cancelled = threading.Event()
+ self.server = None
+ self._server_lock = threading.RLock()
+ self._closed = False
+ self.server_log = None
+ self.sections = []
+ self.section = None
+
+ def terminal_sources(self):
+ sources = []
+ setup_log = self.setup.state.get('log') if self.setup else None
+ if setup_log:
+ sources.append(('setup', str(Path(setup_log) / 'launcher.log')))
+ if self.server_log:
+ sources.append(('comfy', str(self.server_log)))
+ return sources
+
+ def terminal_running(self, path):
+ if path and self.server_log and Path(path) == self.server_log:
+ return self.server is None or self.server.poll() is None
+ return self.busy
+
+ def restore_terminal(self, engine):
+ # This is log viewing only: a persisted path grants no process-control
+ # authority over an existing server. Never scan entire run directories.
+ self.server_log = None
+ try:
+ root = Path(engine) / 'launcher'
+ value = json.loads((root / 'comfy-console.json').read_text(encoding='utf-8'))
+ path = (root / value['log']).resolve()
+ path.relative_to((root / 'comfy-runs').resolve())
+ if path.is_file():
+ self.server_log = path
+ except (OSError, ValueError, KeyError, TypeError):
+ pass
+
+ def attach_console(self, info):
+ if info.get('console_log'):
+ root = Path(self.selection['engine']) / 'launcher'
+ path = Path(info['console_log']).resolve()
+ path.relative_to((root / 'comfy-runs').resolve())
+ self.server_log = path
+ save(root / 'comfy-console.json', dict(log=str(path.relative_to(root.resolve()))))
+ elif info.get('console_error'):
+ self.state = dict(self.state, error=info['console_error'])
+
+ def restore(self, values):
+ """Read a completed installation without probing a GPU or installing anything."""
+ record = values.get('installation') or {}
+ try:
+ if record:
+ selected_root, selected_engine = record['root'], record['engine']
+ elif values.get('new_comfy'):
+ selected_root = str(Path(values['destination']) / 'ComfyUI')
+ selected_engine = values.get('engine') or str(Path(values['destination']) / 'FreeVideo-engine')
+ else:
+ selected_root = values.get('comfy')
+ if not selected_root:
+ return False
+ selected_engine = values.get('engine') or str(Path(layout(selected_root)['root']) / 'FreeVideo-engine')
+ descriptor = layout(selected_root)
+ engine = Path(selected_engine).expanduser().resolve()
+ receipt_path = node_target(descriptor['root']) / 'freevideo-launcher.json'
+ receipt = json.loads(receipt_path.read_text(encoding='utf-8'))
+ source = Path(receipt['source']).resolve()
+ if (receipt.get('protocol') != PROTOCOL or Path(receipt['engine_root']).resolve() != engine
+ or not (source / 'freevideo_engine/comfy_nodes.py').is_file()
+ or not (source / '__init__.py').is_file()
+ or hashlib.sha256((receipt_path.parent / '__init__.py').read_bytes()).hexdigest() != receipt.get('entry_sha256')):
+ return False
+ installation(source, {'FREEVIDEO_HOME': str(engine)})
+ python = (record.get('python') or values.get('python') or
+ managed_python(engine, Path(descriptor['root'])) or descriptor.get('python'))
+ if not python or not Path(python).is_file():
+ return False
+ self.selection = dict(descriptor, source=str(source), engine=str(engine), python=str(python),
+ url=local_url(record.get('url') or values.get('url', '')), ready=True,
+ separate=record.get('separate', values.get('separate', False)))
+ from .desktop_runtime import matching_source
+ self.state = dict(status='ready', engine_update_available=not matching_source(self.source, source),
+ selection=dict(self.selection))
+ self.restore_terminal(engine)
+ return True
+ except (OSError, ValueError, KeyError, TypeError):
+ return False
+
+ def _launch(self, values):
+ if not self.restore(values):
+ raise ValueError('The saved installation is unavailable or incomplete. Return to Installation settings to locate or repair it; saved paths and model folders are retained.')
+ self.state = dict(self.state, status='running', action='launch')
+ self._connect()
+
+ def stage(self, name, *, done=0, total=1, label=''):
+ self.section = name
+ offset = 0
+ for key, count in self.sections:
+ if key == name:
+ fraction = min(1., max(0., done / total)) if total else 0.
+ self.state = dict(self.state, overall=dict(done=offset + count * fraction,
+ total=sum(row[1] for row in self.sections), label=label))
+ return
+ offset += count
+
+ @property
+ def busy(self):
+ return self.thread is not None and self.thread.is_alive()
+
+ def run(self, operation, *args):
+ if self._closed:
+ raise RuntimeError('The launcher is closing')
+ if self.busy:
+ raise ValueError('A launcher task is already running')
+ if operation not in ('inspect', 'install', 'connect', 'launch', 'shortcut'):
+ raise ValueError('Unknown launcher action')
+ if operation == 'install' and (self.state.get('status') != 'review' or self.state.get('errors')):
+ raise ValueError('Inspect and review a valid installation plan first')
+ self.cancelled.clear()
+ self.state = dict(self.state, status='running', action=operation, error=None, exception=None, task={}, overall={},
+ model_groups=[] if operation == 'inspect' else self.state.get('model_groups', []))
+ if operation in ('connect', 'launch'):
+ self.sections = [('open', 1)]
+ def work():
+ try:
+ getattr(self, '_' + operation)(*args)
+ except Exception as error:
+ from .diagnostic_resources import exception_details
+ self.state = dict(self.state, status='cancelled' if self.cancelled.is_set() else 'failed',
+ error=str(error), exception=exception_details(error))
+ self.thread = threading.Thread(target=work, daemon=True)
+ self.thread.start()
+
+ def cancel(self):
+ self.cancelled.set()
+ if self.setup:
+ self.setup.runner.cancel()
+
+ def close(self):
+ """Stop only the process tree created by this launcher instance."""
+ with self._server_lock:
+ self._closed = True
+ try:
+ self.cancel()
+ finally:
+ with self._server_lock:
+ if self.server is not None:
+ processes.stop(self.server, grace=3)
+ self.server = None
+
+ def _wait_setup(self):
+ # The Setup service and its runner stay the single source of truth.
+ while True:
+ row = self.setup.status()
+ self.state = dict(self.state, task=row,
+ model_groups=row.get('model_groups') or self.state.get('model_groups', []))
+ phase = row.get('phase_progress', {})
+ if phase.get('total') and self.section:
+ self.stage(self.section, done=phase.get('done', 0), total=phase['total'], label=phase.get('label', ''))
+ if self.cancelled.is_set():
+ self.setup.runner.cancel()
+ if not row['busy'] and row['status'] != 'running':
+ if row['status'] != 'complete' and not (row.get('action') == 'plan' and row.get('plan')):
+ raise RuntimeError(row.get('error') or row.get('tail', '')[-2000:] or 'Installation stopped; completed files are retained.')
+ if self.cancelled.is_set():
+ raise RuntimeError('Stopped; files retained')
+ return row
+ self.cancelled.wait(.1)
+
+ def _inspect(self, values):
+ self.selection = None
+ self.sections, self.section = [], None
+ self.state = dict(status='running', action='inspect')
+ fresh = values.get('new_comfy', False)
+ descriptor = new_layout(values.get('destination', '')) if fresh else layout(values['comfy'])
+ node_target(descriptor['root'])
+ default_parent = Path(descriptor['root']).parent if fresh else Path(descriptor['root'])
+ engine = Path(values.get('engine') or default_parent / 'FreeVideo-engine').expanduser().resolve()
+ if fresh and Path(descriptor['root']) in engine.parents:
+ raise ValueError('Place the engine beside the new ComfyUI folder, so ComfyUI can be downloaded atomically.')
+ cached_python = managed_python(engine, Path(descriptor['root']))
+ host = (dict(ready=False, python=None, libraries=[str(Path(descriptor['root']) / 'models')])
+ if fresh and not cached_python else probe_host(descriptor, values.get('python') or cached_python))
+ descriptor.update(python=host.get('python'), separate=values.get('separate', False) or not host.get('ready'))
+ # Validate before copying even the small launcher payload.
+ folders = SimpleNamespace(base_path=descriptor['root'], models_dir=str(Path(descriptor['root']) / 'models'),
+ get_folder_paths=lambda _: host.get('libraries', []))
+ validator = Setup(self.source, folders, LauncherRunner)
+ validator.validate_root(str(engine))
+ source = materialize_source(self.source, engine / 'launcher' / 'source')
+ self.setup = Setup(source, folders, LauncherRunner)
+ from .hf_auth import validate
+ self.setup.runner.token = validate(values.get('token', ''))
+ url = local_url(values.get('url', ''))
+ ready = False
+ if not values.get('repair'):
+ try:
+ installation(source, {'FREEVIDEO_HOME': str(engine)})
+ ready = True
+ except (OSError, ValueError):
+ pass
+ self.selection = dict(descriptor, engine=str(engine), source=str(source), url=url, ready=ready)
+ self.state = dict(self.state, selection=dict(self.selection), host=host)
+ if not ready:
+ extra = values.get('model_dirs', [])
+ if not isinstance(extra, list) or any(not isinstance(p, str) for p in extra):
+ raise ValueError('Model folders must be a list of directory paths')
+ extra = extra + ([values['models']] if values.get('models') else [])
+ self.setup.inspect(dict(root=str(engine), extra_libraries=extra, copy=False))
+ row = self._wait_setup()
+ self.state = dict(self.state, plan=row['plan'])
+ extra_disk, disks, disk_errors = disk_review(self.state.get('plan', {}), engine, descriptor['root'],
+ separate=descriptor['separate'], new_comfy=fresh and not Path(descriptor['root']).exists())
+ errors = list(self.state.get('plan', {}).get('errors', []))
+ self.state = dict(self.state, status='review', extra_disk_bytes=extra_disk, disks=disks, errors=errors + disk_errors)
+
+ def _install(self, accepted):
+ if not accepted or not self.selection:
+ raise ValueError('Review and accept the current installation plan first')
+ selected = dict(self.selection)
+ self.sections = ([('engine', 8)] if not selected['ready'] else []) + (
+ [('comfy', 6 if selected.get('new_comfy') else 5)] if selected['separate'] else []) + [('nodes', 1), ('open', 1)]
+ if self.cancelled.is_set():
+ raise RuntimeError('Stopped; files retained')
+ if selected.get('new_comfy'):
+ validate_target(selected['root'])
+ if not selected['ready']:
+ self.stage('engine', label='Prepare FreeVideo')
+ current = self.setup.status()
+ self.setup.install(dict(plan_id=current.get('plan_id'), accept_licenses=True))
+ self._wait_setup()
+ if selected['separate']:
+ if self.cancelled.is_set():
+ raise RuntimeError('Stopped; files retained')
+ self.setup.state = dict(status='running', action='comfy-host')
+ self.setup.events.reset()
+ self.stage('comfy', label='Prepare ComfyUI')
+ arguments = ['comfy-host', '--comfy', selected['root']]
+ if selected.get('new_comfy'):
+ arguments.append('--download-comfy')
+ self.setup.runner.start('comfy-host', selected['engine'], arguments)
+ self._wait_setup()
+ descriptor = json.loads((Path(selected['engine']) / 'launcher' / 'comfy-host.json').read_text(encoding='utf-8'))
+ selected['python'] = descriptor['python']
+ if self.cancelled.is_set():
+ raise RuntimeError('Stopped; files retained')
+ self.stage('nodes', label='Install FreeVideo workflow')
+ deployed = deploy(selected['root'], selected['source'], selected['engine'])
+ selected['ready'] = True
+ self.selection = selected
+ self.state = dict(self.state, selection=dict(selected), deployed=deployed)
+ self._connect()
+
+ def ensure_shortcut(self):
+ from .desktop_shortcut import create_after_install
+ self.state = dict(self.state, shortcut=create_after_install(
+ self.selection['engine'], self.selection['source'], portable_root=getattr(self, 'shortcut_root', None)))
+
+ def _shortcut(self, previous_status='ready'):
+ if not self.selection or not self.selection.get('ready'):
+ raise ValueError('A completed installation is required to create its shortcut')
+ self.ensure_shortcut()
+ self.state = dict(self.state, status='open' if previous_status == 'open' else 'ready')
+
+ def _connect(self):
+ if not self.selection or not self.selection['ready']:
+ raise ValueError('Choose and inspect ComfyUI first')
+ selected = self.selection
+ from .desktop_runtime import matching_source
+ self.state = dict(self.state, engine_update_available=not matching_source(self.source, selected['source']))
+ # Covers old installations, deleted links and directly connecting to an
+ # existing server, as well as the first installation.
+ self.ensure_shortcut()
+ self.stage('open', label='Open ComfyUI')
+ url = selected['url']
+ info = server_info(url)
+ if matches_server(info, selected['root'], selected['engine'], selected['source']):
+ self.attach_console(info)
+ self.stage('open', done=1, label='Ready')
+ self.state = dict(self.state, status='open', url=url + '/?freevideo=launch')
+ return
+ if info['status'] != 'offline':
+ self.state = dict(self.state, status='restart-required', url=url,
+ error='ComfyUI is already running. Restart it once to load the installed FreeVideo nodes, then click Connect. Existing jobs are left running.')
+ return
+ parsed = urlsplit(url)
+ with socket.socket(socket.AF_INET6 if parsed.hostname == '::1' else socket.AF_INET, socket.SOCK_STREAM) as check:
+ try:
+ check.bind((parsed.hostname, parsed.port or 80))
+ except OSError as error:
+ raise ValueError('This port is in use by another application. Set a different local ComfyUI address.') from error
+ if not selected.get('python') or not Path(selected['python']).is_file():
+ raise ValueError('ComfyUI Python is missing. Inspect again to prepare a separate environment.')
+ directory = Path(selected['engine']) / 'launcher' / 'comfy-runs' / (time.strftime('%Y%m%dT%H%M%S') + '-' + secrets.token_hex(4))
+ directory.mkdir(parents=True)
+ self.server_log = directory / 'comfy.log'
+ save(Path(selected['engine']) / 'launcher/comfy-console.json',
+ dict(log=str(self.server_log.relative_to(Path(selected['engine']) / 'launcher'))))
+ env = (self.child_environment() if self.child_environment is not None else
+ isolated_environment(selected['engine'], selected['source']))
+ env.pop('PYTHONPATH', None)
+ env['PYTHONDONTWRITEBYTECODE'] = '1'
+ env.update(PYTHONUNBUFFERED='1', PYTHONUTF8='1', PYTHONIOENCODING='utf-8')
+ env['FREEVIDEO_COMFY_LOG'] = str(self.server_log)
+ command = [selected['python'], '-u', '-B', str(Path(selected['root']) / 'main.py'), '--listen', parsed.hostname,
+ '--port', str(parsed.port or 80), '--disable-auto-launch']
+ if selected.get('portable') and not selected['separate']:
+ command.append('--windows-standalone-build')
+ from .windows_ux import external_python
+ with self._server_lock, external_python(), self.server_log.open('wb') as log:
+ if self._closed or self.cancelled.is_set():
+ raise RuntimeError('ComfyUI startup cancelled')
+ # Keep durable embedded output, but own the whole tree: Windows
+ # Job / Linux supervisor also reclaim independently grouped model
+ # workers when the launcher dies without running Python cleanup.
+ server = processes.popen(command, cwd=selected['root'], env=env, stdin=subprocess.DEVNULL,
+ stdout=log, stderr=subprocess.STDOUT, start_new_session=True, supervise=True)
+ self.server = server
+ save(directory / 'launch.json', dict(command=command, pid=server.pid, root=selected['root'], url=url))
+ self.state = dict(self.state, task=dict(progress=dict(label='Starting ComfyUI', detail='Loading nodes and the web interface'), log=str(directory)))
+ deadline = time.monotonic() + 180
+ while time.monotonic() < deadline:
+ if self.cancelled.is_set():
+ with self._server_lock:
+ processes.stop(server, grace=3)
+ raise RuntimeError('ComfyUI startup cancelled')
+ if server.poll() is not None:
+ from .failure_details import startup_failure
+ raise RuntimeError(startup_failure('ComfyUI could not start.', directory / 'comfy.log'))
+ info = server_info(url)
+ if matches_server(info, selected['root'], selected['engine'], selected['source']):
+ self.stage('open', done=1, label='Ready')
+ self.state = dict(self.state, status='open', url=url + '/?freevideo=launch')
+ return
+ self.cancelled.wait(.5)
+ from .failure_details import startup_failure
+ raise RuntimeError(startup_failure('ComfyUI is still starting or could not load FreeVideo; retry Connect.', directory / 'comfy.log'))
diff --git a/freevideo_engine/comfy_library.py b/freevideo_engine/comfy_library.py
new file mode 100644
index 0000000..638f235
--- /dev/null
+++ b/freevideo_engine/comfy_library.py
@@ -0,0 +1,144 @@
+"""Browse completed FreeVideo outputs across ComfyUI/browser restarts."""
+import asyncio
+import io
+import json
+import re
+from datetime import datetime, timezone
+from pathlib import Path
+
+from .comfy_assets import output_summary
+
+_ID = re.compile(r'\d{4}-\d{2}-\d{2}/[0-9a-f]{32}')
+
+
+def _video(root, identity):
+ if not isinstance(identity, str) or not _ID.fullmatch(identity):
+ raise ValueError('Invalid saved video')
+ root = Path(root).resolve()
+ path = (root / 'FreeVideo' / identity / 'video.mp4').resolve()
+ if not path.is_relative_to(root / 'FreeVideo'):
+ raise ValueError('Saved video is outside the output folder')
+ return path
+
+
+def _report(path):
+ # Read only the small request summary, never tensors, prompts or media.
+ with path.with_suffix('.request.json').open('rb') as stream:
+ data = stream.read(4 * 1024 * 1024 + 1)
+ if len(data) > 4 * 1024 * 1024:
+ raise ValueError('Request summary is too large')
+ report = json.loads(data)
+ if not isinstance(report, dict) or report.get('success') is not True:
+ raise ValueError('Video is not complete')
+ return report
+
+
+def list_videos(output_directory, *, before=None, limit=24):
+ """Return a stable newest-first page, with only relative output paths."""
+ if not 1 <= limit <= 48:
+ raise ValueError('Invalid page size')
+ cursor = None
+ if before:
+ stamp, identity = before.split(':', 1)
+ if not stamp.isdigit() or not _ID.fullmatch(identity):
+ raise ValueError('Invalid page cursor')
+ cursor = (int(stamp), identity)
+ root = Path(output_directory).resolve()
+ candidates = []
+ for path in (root / 'FreeVideo').glob('*/*/video.mp4'):
+ identity = path.parent.relative_to(root / 'FreeVideo').as_posix()
+ try:
+ path = _video(root, identity)
+ info = path.stat()
+ key = (info.st_mtime_ns, identity)
+ if info.st_size and (cursor is None or key < cursor):
+ candidates.append((key, path, info))
+ except (OSError, ValueError):
+ continue
+ rows = []
+ for key, path, info in sorted(candidates, key=lambda item: item[0], reverse=True):
+ try:
+ report = _report(path)
+ relative = Path('FreeVideo') / key[1] / 'video.mp4'
+ summary = output_summary(report, relative)
+ # Geometry may gain input metadata in future reports. Keep this API
+ # an explicit allowlist, not an export of the retained report.
+ summary['geometry'] = {k: v for k, v in summary['geometry'].items()
+ if k in ('width', 'height', 'frames', 'fps', 'seconds')
+ and type(v) in (int, float)}
+ if not (root / summary['report']).is_file():
+ summary['report'] = None
+ rows.append(dict(summary, id=key[1], bytes=info.st_size,
+ created_at=datetime.fromtimestamp(info.st_mtime, timezone.utc).isoformat(),
+ cursor=str(key[0]) + ':' + key[1]))
+ except (OSError, ValueError, TypeError, AttributeError):
+ # A partially written or old malformed report must not hide the
+ # rest of the library or publish an unfinished generation.
+ continue
+ if len(rows) > limit:
+ break
+ more = len(rows) > limit
+ rows = rows[:limit]
+ next_cursor = rows[-1]['cursor'] if more else None
+ for row in rows:
+ row.pop('cursor')
+ return dict(items=rows, next=next_cursor)
+
+
+def thumbnail(output_directory, identity):
+ path = _video(output_directory, identity)
+ _report(path)
+ saved = path.with_suffix('.thumbnail.jpg')
+ if saved.is_file() and saved.stat().st_mtime_ns >= path.stat().st_mtime_ns:
+ data = saved.read_bytes()
+ if data.startswith(b'\xff\xd8') and len(data) <= 512 * 1024:
+ return data
+ # CPU decoding only. Grid thumbnails must not take CUDA memory from a
+ # generation, and we only decode the first frame of each requested video.
+ import av
+ from PIL import Image
+ with av.open(str(path)) as container:
+ stream = container.streams.video[0]
+ stream.thread_count = 1
+ image = next(container.decode(stream)).to_image()
+ image.thumbnail((384, 256), Image.Resampling.LANCZOS)
+ buffer = io.BytesIO()
+ image.save(buffer, format='JPEG', quality=80)
+ data = buffer.getvalue()
+ try:
+ saved.write_bytes(data)
+ except OSError:
+ pass # A read-only output directory still supports browsing.
+ return data
+
+
+def register():
+ from aiohttp import web
+ import folder_paths
+ from server import PromptServer
+ server = PromptServer.instance
+ if server is None or getattr(server, '_freevideo_library', False):
+ return
+ server._freevideo_library = True
+ thumbnails = asyncio.Semaphore(1)
+
+ @server.routes.get('/freevideo/library')
+ async def library(request):
+ try:
+ rows = await asyncio.to_thread(list_videos, folder_paths.get_output_directory(),
+ before=request.query.get('before'),
+ limit=int(request.query.get('limit', '24')))
+ return web.json_response(rows, headers={'Cache-Control': 'no-store'})
+ except ValueError as error:
+ raise web.HTTPBadRequest(text=str(error)) from error
+
+ @server.routes.get('/freevideo/library/thumbnail')
+ async def preview(request):
+ async with thumbnails:
+ try:
+ data = await asyncio.to_thread(thumbnail, folder_paths.get_output_directory(), request.query.get('id'))
+ except Exception:
+ # The full video remains playable if its thumbnail is missing.
+ raise web.HTTPNotFound(text='Thumbnail unavailable') from None
+ return web.Response(body=data, content_type='image/jpeg',
+ headers={'Cache-Control': 'private, max-age=86400'})
diff --git a/freevideo_engine/comfy_media.py b/freevideo_engine/comfy_media.py
new file mode 100644
index 0000000..773f96d
--- /dev/null
+++ b/freevideo_engine/comfy_media.py
@@ -0,0 +1,102 @@
+"""Native Comfy sockets -> retained local files, without model duplication."""
+from pathlib import Path
+
+
+def _image(value, path):
+ import torch
+ from PIL import Image
+ if not isinstance(value, torch.Tensor) or value.ndim != 4 or value.shape[0] != 1 or value.shape[-1] not in (3, 4):
+ raise ValueError('Connect one RGB IMAGE per keyframe/reference; select a frame from a batch first')
+ if not bool(torch.isfinite(value).all()):
+ raise ValueError('Input image contains nonfinite pixels')
+ image = (value[0, ..., :3].detach().to('cpu').clamp(0, 1) * 255).round().to(torch.uint8).numpy()
+ Image.fromarray(image).save(path, format='PNG')
+
+
+def _audio(value, path):
+ import av
+ import numpy as np
+ import torch
+ from fractions import Fraction
+ if not isinstance(value, dict):
+ raise ValueError('Expected a finite mono/stereo Comfy AUDIO input')
+ waveform, rate = value.get('waveform'), value.get('sample_rate')
+ if (not isinstance(waveform, torch.Tensor) or waveform.ndim != 3 or waveform.shape[0] != 1
+ or not waveform.is_floating_point() or waveform.shape[-1] == 0
+ or waveform.shape[1] not in (1, 2) or type(rate) is not int or not 8000 <= rate <= 192000
+ or not bool(torch.isfinite(waveform).all())):
+ raise ValueError('Expected a finite mono/stereo Comfy AUDIO input')
+ if waveform.shape[-1] > rate * 15:
+ raise ValueError('Trim reference audio to 15 s or less before generation')
+ samples = waveform[0].detach().float().cpu().contiguous().numpy()
+ layout = 'mono' if len(samples) == 1 else 'stereo'
+ with av.open(str(path), 'w', format='wav') as container:
+ stream = container.add_stream('pcm_f32le', rate=rate)
+ stream.layout = layout
+ for offset in range(0, samples.shape[1], 65536):
+ # WAV packed float preserves the input's float32 values.
+ packed = np.ascontiguousarray(samples[:, offset:offset + 65536].T.reshape(1, -1))
+ frame = av.AudioFrame.from_ndarray(packed, format='flt', layout=layout)
+ frame.sample_rate, frame.pts, frame.time_base = rate, offset, Fraction(1, rate)
+ for packet in stream.encode(frame):
+ container.mux(packet)
+ for packet in stream.encode(None):
+ container.mux(packet)
+
+
+def export(run, canvas, *, first=None, last=None, references=None, loras=None, conditioning=None, assets=None):
+ media = {'version': 1}
+ if assets:
+ if conditioning is not None or first is not None or last is not None or references:
+ raise ValueError('Use the Media panel or direct media/conditioning sockets; disable duplicate inputs')
+ connected = assets.get('_connected', {})
+ media.update({key: value for key, value in assets.items() if key != '_connected'})
+ first, last = connected.get('first'), connected.get('last')
+ references = [{'kind': kind, 'value': connected[name]}
+ for name, kind in (('reference', 'image'), ('reference_audio', 'audio'))
+ if connected.get(name) is not None]
+ if conditioning is not None and (first is not None or last is not None or references):
+ raise ValueError('Preencoded conditioning already includes media; do not connect a second set')
+ if references and (first is not None or last is not None):
+ raise ValueError('Choose keyframes or experimental references; mixed layouts are not supported')
+ result = {}
+ run = Path(run)
+ for anchor, value in (('first', first), ('last', last)):
+ if value is not None:
+ path = run / (anchor + '.png')
+ _image(value, path)
+ media[anchor] = str(path)
+ if references:
+ media['references'] = list(media.get('references', []))
+ for index, reference in enumerate(references):
+ kind, value = reference['kind'], reference['value']
+ path = run / ('reference-%02d' % index)
+ if kind == 'image':
+ path = path.with_suffix('.png')
+ _image(value, path)
+ elif kind == 'video':
+ if value.get_duration() > 15.05:
+ raise ValueError('Trim reference video to 15 s or less before generation')
+ path = path.with_suffix('.mp4')
+ # Native save_to honors crops/trims and remuxes compatible file
+ # inputs without expanding all frames into host float tensors.
+ value.save_to(str(path))
+ elif kind == 'audio':
+ path = path.with_suffix('.wav')
+ _audio(value, path)
+ else:
+ raise ValueError('Unknown reference type')
+ media['references'].append({'kind': kind, 'path': str(path)})
+ if loras:
+ media['loras'] = [dict(row) for row in loras]
+ if conditioning is not None:
+ import torch
+ from .media_conditioning import from_comfy
+ value, info = from_comfy(conditioning, canvas['width'], canvas['height'], canvas['frames'])
+ path = run / 'input-conditioning.pt'
+ torch.save(value, path)
+ media['conditioning_info'] = info
+ result['conditioning'] = str(path)
+ if len(media) > 1:
+ result['media'] = media
+ return result
diff --git a/freevideo_engine/comfy_nodes.py b/freevideo_engine/comfy_nodes.py
new file mode 100644
index 0000000..02ae54e
--- /dev/null
+++ b/freevideo_engine/comfy_nodes.py
@@ -0,0 +1,290 @@
+"""Optional ComfyUI V3 nodes. Never import these from the standalone CLI."""
+from pathlib import Path
+
+from comfy_api.latest import ComfyExtension, InputImpl, io, ui
+
+from . import comfy_bridge
+from .default_prompt import DEFAULT_PROMPT
+
+References = io.Custom('FREEVIDEO_REFERENCES')
+LoRAs = io.Custom('FREEVIDEO_LORAS')
+Media = io.Custom('FREEVIDEO_MEDIA')
+NO_LORA = 'None'
+
+class FreeVideoGenerate(io.ComfyNode):
+ @classmethod
+ def define_schema(cls):
+ return io.Schema(
+ node_id='FreeVideoGenerate', display_name='FreeVideo · Video + Audio',
+ category='FreeVideo', search_aliases=['VDN', 'H3', 'i2v', 'fl2v', 'ref2v'],
+ description='Generate a video with audio using your FreeVideo installation. Memory placement, '
+ 'caches and validated optimizations are automatic. Outputs and diagnostics are retained.',
+ inputs=[
+ io.String.Input('text', display_name='Prompt', multiline=True, default=DEFAULT_PROMPT,
+ tooltip='Describe the scene, motion, dialogue, sound and music.'),
+ io.Int.Input('width', default=1344, min=256, max=4096, step=32),
+ io.Int.Input('height', default=768, min=256, max=4096, step=32),
+ io.Float.Input('seconds', display_name='Duration (s)', default=10., min=1.625, max=60., step=.1,
+ tooltip='24 fps. H3 rounds up to its 17n+5 frame grid: 10 s becomes 10.125 s. '
+ 'Longer than 15 s is experimental.'),
+ io.Int.Input('seed', default=2026090903, min=0, max=2**53-1, control_after_generate=True),
+ io.Image.Input('first', display_name='First frame', optional=True,
+ tooltip='One image. Center-cropped to the selected canvas; enables I2VA.'),
+ io.Image.Input('last', display_name='Last frame', optional=True,
+ tooltip='One image. With first frame enables FL2VA; alone enables L2VA.'),
+ References.Input('references', optional=True,
+ tooltip='Experimental Ref2VA. Use FreeVideo Reference nodes. VDN is not reference-trained.'),
+ LoRAs.Input('loras', display_name='LoRAs', optional=True),
+ io.Conditioning.Input('conditioning', optional=True,
+ tooltip='Native H3 layer-50 conditioning including keyframe/reference metadata. '
+ 'Replaces prompt/media encoding. Generic CLIP conditioning is incompatible.'),
+ Media.Input('media', optional=True, tooltip='Unified Media panel: keyframes or ordered references.'),
+ io.Boolean.Input('two_pass', display_name='Two-pass sampling', default=True, optional=True,
+ tooltip='Generate the scene, then refine it at the target resolution.'),
+ ],
+ outputs=[io.Video.Output('video'), io.String.Output('report', display_name='Report JSON')],
+ hidden=[io.Hidden.unique_id, io.Hidden.extra_pnginfo],
+ is_output_node=True, not_idempotent=True,
+ )
+
+ @classmethod
+ def validate_inputs(cls, text, width, height, seconds, seed, **kwargs):
+ try:
+ comfy_bridge.validate_request(text, width, height, seconds, seed)
+ comfy_bridge.installation()
+ except (OSError, ValueError, KeyError) as error:
+ return str(error)
+ return True
+
+ @classmethod
+ def fingerprint_inputs(cls, **kwargs):
+ # Queueing a fixed seed is a new measured request. Disk conditioning and
+ # engine input caches are still reused; Comfy does not return an old MP4.
+ return float('nan')
+
+ @classmethod
+ def execute(cls, text, width, height, seconds, seed, first=None, last=None,
+ references=None, loras=None, conditioning=None, media=None, two_pass=True):
+ from .comfy_media import export
+ import folder_paths
+ import comfy.model_management as memory
+ from comfy.utils import ProgressBar
+ from server import PromptServer
+ from .comfy_progress import publish
+ from .encoder_prewarm import IDLE
+ cpu_prewarm = IDLE.stop()
+ from .resident_process import OWNER
+ gpu_prewarm = OWNER.stop_prewarm()
+ node_id = cls.hidden.unique_id
+ bar = ProgressBar(8, node_id=node_id)
+ last_message = [None]
+ last_count = [None]
+ def progress(message):
+ label = message['label']
+ count = (message.get('done'), message.get('total'))
+ if message.get('done') is not None and count != last_count[0]:
+ bar.update_absolute(message['done'], message['total'])
+ last_count[0] = count
+ if message != last_message[0]:
+ server = PromptServer.instance
+ publish(server, node_id, message)
+ if server is not None:
+ detail = message.get('detail')
+ server.send_progress_text(label + (' · ' + detail if detail else ''), node_id)
+ last_message[0] = dict(message)
+ def release():
+ from .comfy_residency import keep_engine_cache
+ with keep_engine_cache():
+ memory.unload_all_models()
+ memory.soft_empty_cache()
+ metadata = cls.hidden.extra_pnginfo or {}
+ output_root = Path(folder_paths.get_output_directory()).resolve()
+ publish(PromptServer.instance, node_id, {'label': 'Preparing video', 'new_request': True})
+ try:
+ output = comfy_bridge.generate(text, width, height, seconds, seed, output_root,
+ metadata=metadata.get('workflow', metadata), progress=progress, two_pass=two_pass,
+ encoder_prewarm=gpu_prewarm or cpu_prewarm,
+ interrupted=memory.throw_exception_if_processing_interrupted, release_models=release,
+ export_inputs=lambda run, canvas: export(run, canvas, first=first, last=last,
+ references=references, loras=loras, conditioning=conditioning, assets=media))
+ relative = output.relative_to(output_root)
+ report = output.with_suffix('.request.json').read_text(encoding='utf-8')
+ import json
+ from .comfy_assets import output_summary, saved_video
+ preview = ui.PreviewVideo([saved_video(output, output_root)]).as_dict()
+ preview['freevideo_summary'] = [output_summary(json.loads(report), relative)]
+ except BaseException as error:
+ cancelled = isinstance(error, KeyboardInterrupt) or type(error).__name__ in ('InterruptProcessingException', 'CancelledError')
+ phase = 'cancelled' if cancelled else 'failed'
+ publish(PromptServer.instance, node_id, {'label': 'Generation cancelled' if cancelled else 'Generation stopped',
+ 'phase': phase, 'overall': {'status': phase}})
+ raise
+ publish(PromptServer.instance, node_id, {'label': 'Video saved', 'phase': 'complete',
+ 'overall': {'status': 'complete', 'fraction': 1.}, 'result': preview['freevideo_summary'][0]})
+ measured = json.loads(report)
+ encoder_path = measured.get('encoding', {}).get('encoder')
+ from .resident_process import OWNER
+ if encoder_path:
+ server = PromptServer.instance
+ client = server.client_id if server is not None else None
+ def busy():
+ return server is not None and server.prompt_queue.get_tasks_remaining() > 0
+ def warmed(value):
+ if server is not None:
+ server.send_sync('freevideo_prewarm', dict(value, node=node_id), sid=client)
+ try:
+ if OWNER.process is not None:
+ root, machine = comfy_bridge.installation()
+ environment = comfy_bridge.isolated_environment(root, comfy_bridge.source_root())
+ environment.update(FREEVIDEO_HOME=str(root), PYTHONPATH=str(comfy_bridge.source_root()))
+ OWNER.warm_encoder(output, machine['python'], environment, busy=busy, notify=warmed)
+ else:
+ IDLE.schedule(encoder_path, output.with_suffix('.prewarm.json'),
+ ram_budget=measured.get('profile', {}).get('inference_ram_budget_gb', 0) * 1e9,
+ busy=busy, notify=warmed)
+ except (OSError, RuntimeError):
+ pass # A saved video remains successful if idle warming cannot start.
+ return io.NodeOutput(InputImpl.VideoFromFile(str(output)), report, ui=preview)
+
+
+class FreeVideoMedia(io.ComfyNode):
+ @classmethod
+ def define_schema(cls):
+ return io.Schema(node_id='FreeVideoMedia', display_name='FreeVideo · Media', category='FreeVideo',
+ description='Connect IMAGE or AUDIO outputs from generation, editing or loading nodes, or upload files. '
+ 'Choose first/last frames or experimental references. Empty media means text to video.',
+ inputs=[io.String.Input('assets', default='[]'),
+ io.Image.Input('first', display_name='First frame', optional=True,
+ tooltip='Connect any compatible IMAGE output. Select one frame from a batch first.'),
+ io.Image.Input('last', display_name='Last frame', optional=True,
+ tooltip='Connect an IMAGE output for the last frame.'),
+ io.Image.Input('reference', display_name='Reference image', optional=True,
+ tooltip='One reference image; appended after uploaded references. Experimental.'),
+ io.Audio.Input('reference_audio', display_name='Reference audio', optional=True,
+ tooltip='Voice, music or rhythm reference, up to 15 seconds. Connect a Load Audio '
+ 'or other AUDIO output. Describe its role using <Audio 1> in the prompt. '
+ 'Experimental; does not lock the output soundtrack or lip sync.')],
+ outputs=[Media.Output('media')])
+
+ @classmethod
+ def validate_inputs(cls, assets, **kwargs):
+ from .comfy_assets import resolve_assets
+ import folder_paths
+ try:
+ resolve_assets(assets, folder_paths.get_input_directory())
+ except (OSError, ValueError) as error:
+ return str(error)
+ return True
+
+ @classmethod
+ def fingerprint_inputs(cls, **kwargs):
+ # Resolve current files again; the engine content-checks reusable inputs.
+ return float('nan')
+
+ @classmethod
+ def execute(cls, assets, first=None, last=None, reference=None, reference_audio=None):
+ from .comfy_assets import resolve_assets, connect_assets
+ import folder_paths
+ return io.NodeOutput(connect_assets(resolve_assets(assets, folder_paths.get_input_directory()),
+ first=first, last=last, reference=reference,
+ reference_audio=reference_audio))
+
+
+class FreeVideoReference(io.ComfyNode):
+ @classmethod
+ def define_schema(cls):
+ return io.Schema(node_id='FreeVideoReference', display_name='FreeVideo · Reference (experimental)',
+ category='FreeVideo', is_experimental=True,
+ description='Append one image, video or audio reference in order. Clips must be trimmed to 15 s or less. '
+ 'Connect the stack to Generate. VDN reference quality is experimental.',
+ inputs=[io.Image.Input('image', optional=True), io.Video.Input('video', optional=True),
+ io.Audio.Input('audio', optional=True), References.Input('previous', optional=True)],
+ outputs=[References.Output('references')])
+
+ @classmethod
+ def execute(cls, image=None, video=None, audio=None, previous=None):
+ values = [(kind, value) for kind, value in (('image', image), ('video', video), ('audio', audio)) if value is not None]
+ if len(values) != 1:
+ raise ValueError('Connect exactly one image, video or audio to each Reference node')
+ result = list(previous or [])
+ if len(result) >= 32:
+ raise ValueError('At most 32 ordered references; memory is checked against the actual request')
+ kind, value = values[0]
+ return io.NodeOutput(result + [{'kind': kind, 'value': value}])
+
+
+class FreeVideoLoRA(io.ComfyNode):
+ @classmethod
+ def define_schema(cls):
+ import folder_paths
+ names = [name for name in folder_paths.get_filename_list('loras') if name.lower().endswith('.safetensors')]
+ return io.Schema(node_id='FreeVideoLoRA', display_name='FreeVideo · LoRA', category='FreeVideo',
+ description='FreeVideo needs no acceleration LoRA.',
+ inputs=[io.Combo.Input('lora', options=[NO_LORA, *names]),
+ io.Float.Input('strength', default=1., min=-4., max=4., step=.05),
+ LoRAs.Input('previous', optional=True)], outputs=[LoRAs.Output('loras')])
+
+ @classmethod
+ def execute(cls, lora, strength, previous=None):
+ if lora == NO_LORA:
+ return io.NodeOutput(list(previous or []))
+ import folder_paths
+ path = folder_paths.get_full_path_or_raise('loras', lora)
+ return io.NodeOutput(list(previous or []) + [{'path': str(Path(path).resolve()), 'strength': strength}])
+
+
+class FreeVideoLoRAStack(io.ComfyNode):
+ @classmethod
+ def define_schema(cls):
+ return io.Schema(node_id='FreeVideoLoRAStack', display_name='FreeVideo · LoRAs', category='FreeVideo',
+ description='FreeVideo needs no acceleration LoRA.',
+ inputs=[io.String.Input('adapters', default='[]'), LoRAs.Input('previous', optional=True)],
+ outputs=[LoRAs.Output('loras')])
+
+ @classmethod
+ def execute(cls, adapters, previous=None):
+ from .comfy_assets import resolve_loras
+ import folder_paths
+ return io.NodeOutput(resolve_loras(adapters, folder_paths.get_filename_list('loras'),
+ lambda name: folder_paths.get_full_path_or_raise('loras', name), previous))
+
+ @classmethod
+ def fingerprint_inputs(cls, **kwargs):
+ return float('nan') # The engine verifies current adapter content on each request.
+
+
+class FreeVideoFrames(io.ComfyNode):
+ @classmethod
+ def define_schema(cls):
+ return io.Schema(node_id='FreeVideoFrames', display_name='FreeVideo · First / Last Frame', category='FreeVideo',
+ description='Extract just the first and last frames for image processing or the next video segment. '
+ 'The full decoded movie is not retained in RAM.',
+ inputs=[io.Video.Input('video')], outputs=[io.Image.Output('first'), io.Image.Output('last')])
+
+ @classmethod
+ def execute(cls, video):
+ import av
+ import torch
+ import tempfile
+ # save_to preserves native trims and crops; a temporary remux avoids
+ # ignoring edits when get_stream_source points at the untrimmed file.
+ with tempfile.TemporaryDirectory(prefix='FreeVideo frames ') as directory:
+ path = Path(directory) / 'frames.mp4'
+ video.save_to(str(path))
+ first = last = None
+ with av.open(str(path)) as container:
+ for frame in container.decode(video=0):
+ last = frame.to_ndarray(format='rgb24')
+ if first is None:
+ first = last
+ if first is None:
+ raise ValueError('The video contains no frames')
+ return io.NodeOutput(*(torch.from_numpy(frame.copy()).float().div_(255)[None] for frame in (first, last)))
+
+
+class FreeVideoExtension(ComfyExtension):
+ async def get_node_list(self):
+ from .comfy_residency import install
+ import comfy.model_management
+ install(comfy.model_management)
+ return [FreeVideoGenerate, FreeVideoMedia, FreeVideoReference, FreeVideoLoRA, FreeVideoLoRAStack, FreeVideoFrames]
diff --git a/freevideo_engine/comfy_progress.py b/freevideo_engine/comfy_progress.py
new file mode 100644
index 0000000..28fa5f7
--- /dev/null
+++ b/freevideo_engine/comfy_progress.py
@@ -0,0 +1,102 @@
+"""Transient progress snapshots for reopened ComfyUI tabs; no request inputs."""
+from copy import deepcopy
+import threading
+import time
+import uuid
+
+
+class ProgressState:
+ def __init__(self, clock=time.monotonic):
+ self.clock = clock
+ self.stream_id = uuid.uuid4().hex
+ self.sequence = 0
+ self.rows = {}
+ self.lock = threading.Lock()
+
+ def publish(self, node, message):
+ # Only presentation fields. In particular, do not retain the prompt,
+ # workflow, paths, device identity or full resource forecast here.
+ keys = ('label', 'detail', 'phase', 'stage', 'timing_phase', 'done', 'total',
+ 'unit', 'block', 'blocks', 'elapsed_seconds', 'step_elapsed_seconds',
+ 'estimated_step_seconds', 'remaining_seconds', 'display_fraction',
+ 'estimated', 'uniform_remaining_steps', 'overall', 'retry', 'warning', 'kernel_cache_note',
+ 'new_request', 'reset', 'result')
+ value = deepcopy({key: message[key] for key in keys if key in message})
+ node = str(node)
+ with self.lock:
+ now = self.clock()
+ old = self.rows.get(node)
+ if old and value.get('warning'):
+ # A memory warning must not replace the current sampling step.
+ value = dict(old['message'], warning=value['warning'])
+ measured = old['measured']
+ else:
+ measured = now
+ if old and not value.get('new_request') and old['message'].get('retry'):
+ value.setdefault('retry', old['message']['retry'])
+ self.sequence += 1
+ value.update(node=node, stream_id=self.stream_id, sequence=self.sequence)
+ self.rows[node] = {'message': value, 'measured': measured}
+ while len(self.rows) > 32:
+ oldest = min(self.rows, key=lambda key: self.rows[key]['message']['sequence'])
+ del self.rows[oldest]
+ return dict(value, age_seconds=max(0., now - measured))
+
+ def snapshot(self):
+ with self.lock:
+ now = self.clock()
+ return [dict(deepcopy(row['message']), age_seconds=max(0., now-row['measured']))
+ for row in self.rows.values()]
+
+
+STATE = ProgressState()
+
+
+def publish(server, node, message):
+ value = STATE.publish(node, message)
+ if server is not None:
+ # Progress belongs to the running node, not to the tab that queued it.
+ server.send_sync('freevideo_progress', value)
+ return value
+
+
+def cancel_request(queue, prompt_id, node_id, interrupt):
+ """Remove or interrupt exactly this FreeVideo request under the queue lock."""
+ with queue.mutex:
+ running, pending = queue.get_current_queue()
+ def matches(row):
+ return (row[1] == prompt_id
+ and row[2].get(str(node_id), {}).get('class_type') == 'FreeVideoGenerate')
+ if any(matches(row) for row in pending):
+ queue.delete_queue_item(matches)
+ return 'cancelled'
+ if any(matches(row) for row in running):
+ # Hold the same lock used by task_done/get: the next request cannot
+ # start between checking the ID and setting Comfy's interrupt flag.
+ interrupt()
+ return 'cancelling'
+ return 'finished'
+
+
+def register():
+ from aiohttp import web
+ from server import PromptServer
+ server = PromptServer.instance
+ if server is None or getattr(server, '_freevideo_progress', False):
+ return
+ server._freevideo_progress = True
+
+ @server.routes.get('/freevideo/progress')
+ async def progress(request):
+ return web.json_response({'progress': STATE.snapshot()},
+ headers={'Cache-Control': 'no-store'})
+
+ @server.routes.post('/freevideo/cancel')
+ async def cancel(request):
+ value = await request.json()
+ prompt_id, node_id = value.get('prompt_id'), value.get('node_id')
+ if not isinstance(prompt_id, str) or not prompt_id or not isinstance(node_id, str):
+ return web.json_response({'error': 'A request and node ID are required'}, status=400)
+ import nodes
+ return web.json_response({'status': cancel_request(server.prompt_queue, prompt_id,
+ node_id, nodes.interrupt_processing)})
diff --git a/freevideo_engine/comfy_residency.py b/freevideo_engine/comfy_residency.py
new file mode 100644
index 0000000..c9062d9
--- /dev/null
+++ b/freevideo_engine/comfy_residency.py
@@ -0,0 +1,38 @@
+"""Cooperate with ComfyUI's native memory manager without changing its models."""
+from contextlib import contextmanager
+from functools import wraps
+import threading
+
+from .resident_process import OWNER
+
+_local = threading.local()
+
+
+@contextmanager
+def keep_engine_cache():
+ old = getattr(_local, 'keep', False)
+ _local.keep = True
+ try:
+ yield
+ finally:
+ _local.keep = old
+
+
+def install(memory):
+ if getattr(memory.free_memory, '_freevideo_resident_cooperation', False) is True:
+ return
+ original = memory.free_memory
+
+ @wraps(original)
+ def free_memory(memory_required, device, *args, **kwargs):
+ # FreeVideo's own handoff releases native Comfy models only. Other
+ # nodes, and Comfy's explicit unload command, can reclaim our idle
+ # process before choosing a lower-memory native loading strategy.
+ if (not getattr(_local, 'keep', False) and not OWNER.active and
+ OWNER.process is not None and OWNER.process.poll() is None):
+ if device is None or memory_required > memory.get_free_memory(device):
+ OWNER.close()
+ return original(memory_required, device, *args, **kwargs)
+
+ free_memory._freevideo_resident_cooperation = True
+ memory.free_memory = free_memory
diff --git a/freevideo_engine/comfy_setup.py b/freevideo_engine/comfy_setup.py
new file mode 100644
index 0000000..c1e3460
--- /dev/null
+++ b/freevideo_engine/comfy_setup.py
@@ -0,0 +1,421 @@
+"""Reuse Comfy's registered libraries and provision a separate engine environment.
+
+Import/GET discovery is read-only. Only explicit inspect/install actions start
+children. The existing installer owns content checks, consent, download recovery,
+resource locks and readiness; this module does not run pip in ComfyUI.
+"""
+from collections import deque
+import json
+import os
+from pathlib import Path
+import secrets
+import sys
+import threading
+import uuid
+
+from .comfy_bridge import installation, installation_root, source_root
+from .environments import ENVIRONMENTS
+from .desktop_runtime import Runner, preflight_json
+from .comfy_environment import isolated_environment
+from .local_models import library_roots
+from .monitoring import save
+from .setup_progress import ProgressState
+
+
+def discover_libraries(folder_paths, environ=None):
+ env = os.environ if environ is None else environ
+ candidates = [str(folder_paths.models_dir)]
+ # folder_paths already incorporates extra_model_paths.yaml, including paths
+ # registered by other nodes. No second, subtly different YAML parser.
+ for name in ('diffusion_models', 'unet', 'checkpoints', 'text_encoders', 'clip', 'vae'):
+ try:
+ candidates.extend(folder_paths.get_folder_paths(name))
+ except KeyError:
+ pass
+ cache = env.get('HF_HUB_CACHE') or env.get('HUGGINGFACE_HUB_CACHE')
+ if not cache and env.get('HF_HOME'):
+ cache = str(Path(env['HF_HOME']) / 'hub')
+ modelscope = env.get('MODELSCOPE_CACHE')
+ if not cache or not modelscope:
+ # Optional cache discovery must also work in portable/service Windows
+ # environments without USERPROFILE. Evaluate defaults only if needed.
+ try:
+ home_cache = Path.home() / '.cache'
+ except (OSError, RuntimeError):
+ pass
+ else:
+ cache = cache or str(home_cache / 'huggingface/hub')
+ modelscope = modelscope or str(home_cache / 'modelscope/hub')
+ candidates.extend(value for value in (cache, modelscope) if value)
+ existing = []
+ for value in candidates:
+ try:
+ path = Path(value).expanduser()
+ if path.is_dir():
+ existing.append(str(path))
+ except (OSError, RuntimeError):
+ pass
+ return library_roots(existing)
+
+
+# The calibration encodes this once and every variant reads it, so the
+# comparison excludes text encoding and both halves see one input.
+CALIBRATION_PROMPT = ('A neon-lit alley after heavy rain. The camera drifts forward past '
+ 'reflected signage while distant thunder rolls and footsteps echo '
+ 'on wet stone.')
+
+
+class SetupRunner(Runner):
+ token = ''
+
+ def command(self, root, arguments):
+ if os.name == 'nt':
+ return super().command(root, arguments)
+ return ['/bin/bash', str(self.source / 'freevideo'), '--root', str(root), *arguments]
+
+ def environment(self, root):
+ from .hf_auth import environment
+ return environment(self.token, isolated_environment(root, self.source))
+
+
+class Events:
+ """A closed browser must not block a worker or grow a queue indefinitely."""
+ def __init__(self):
+ self.lock = threading.Lock()
+ self.rows = deque(maxlen=128)
+ self.progress = ProgressState()
+
+ def put(self, row):
+ with self.lock:
+ if row[0] == 'progress':
+ self.progress.update(row[1])
+ self.rows.append(row)
+
+ def reset(self):
+ with self.lock:
+ self.rows.clear()
+ self.progress = ProgressState()
+
+ def snapshot(self):
+ with self.lock:
+ return self.progress.snapshot()
+
+ def drain(self):
+ with self.lock:
+ rows = list(self.rows)
+ self.rows.clear()
+ return rows
+
+
+class Setup:
+ def __init__(self, source, folder_paths, runner_factory=SetupRunner):
+ self.source = Path(source).resolve()
+ self.folder_paths = folder_paths
+ self.events = Events()
+ self.logs = self.source / '.freevideo' / 'comfy-setup'
+ self.runner = runner_factory(self.source, self.events, self.logs)
+ self.state = {'status': 'idle'}
+ self.plan = self.selection = None
+ self.token = secrets.token_urlsafe(32)
+ from .download_settings import Probe
+ self.download_probe = Probe()
+
+ def discovery(self, download_root=None):
+ root = Path(self.selection['root']) if self.runner.busy and self.selection else installation_root(self.source)
+ ready, detail = False, ''
+ try:
+ installation(self.source)
+ ready = True
+ except (OSError, ValueError) as error:
+ detail = str(error)
+ return dict(root=str(root), ready=ready, detail=detail,
+ libraries=discover_libraries(self.folder_paths), token=self.token,
+ environment='Managed FreeVideo environment; ComfyUI packages are kept',
+ restart_required=False, downloads=self.downloads(download_root or root),
+ resources=self.resource_settings())
+
+
+ def resource_settings(self, value=None):
+ from .resource_settings import read, write
+ root = installation_root(self.source)
+ if value is None:
+ return read(root)
+ if 'gpu_reserve_gib' not in value:
+ raise ValueError('Specify automatic (null) or reserved VRAM in GiB')
+ return write(root, value['gpu_reserve_gib'])
+
+ def downloads(self, root=None):
+ from .download_settings import read
+ return dict(preferences=read(Path(root or installation_root(self.source))/'download-settings.json'),
+ probe=self.download_probe.snapshot(), hf_token_set=bool(getattr(self.runner, 'token', '')))
+
+ def download_token(self, value):
+ from .hf_auth import validate
+ if self.runner.busy:
+ raise ValueError('Stop setup before changing the token, then resume with retained downloads.')
+ self.runner.token = validate(value.get('hf_token', ''))
+ self.plan = None
+ self.state.pop('plan_id', None)
+ self.state.pop('plan', None)
+ return dict(hf_token_set=bool(self.runner.token))
+
+ def network_settings(self, value, *, probe=False):
+ root=self.validate_root(value.get('root'))
+ if self.runner.busy and self.selection and root!=Path(self.selection['root']):
+ raise ValueError('Choose the active installation folder to switch its download')
+ if probe:
+ self.download_probe.start(root, getattr(self.runner, 'token', ''))
+ else:
+ self.download_probe.select(root,value.get('source'),proxy_mode=value.get('proxy_mode'))
+ return self.downloads(root)
+
+ def compatibility_settings(self, value, action):
+ from .compatibility import Store, installed_identity, check_installation
+ root = self.validate_root(value.get('root') or str(installation_root(self.source)))
+ identity = installed_identity(root)
+ store = Store(root)
+ if action == 'compatibility-check':
+ result = check_installation(root)
+ elif action == 'compatibility-save':
+ result = store.set(identity, value.get('level'), value.get('automatic'))
+ elif action == 'compatibility-ack':
+ result = store.acknowledge(identity, value.get('notice_id'))
+ else:
+ raise ValueError('Unknown compatibility action')
+ return result
+
+ def validate_root(self, value):
+ if not isinstance(value, str) or not value.strip() or len(value) > 4096:
+ raise ValueError('Choose an engine installation folder')
+ root = Path(value).expanduser().resolve()
+ host = Path(self.folder_paths.base_path).resolve()
+ if root == Path(root.anchor) or root == host or root in host.parents:
+ raise ValueError('Use a dedicated FreeVideo folder, not the ComfyUI root or its parent')
+ prefix = Path(sys.prefix).resolve()
+ # Refuse to pip into the interpreter that is running, and nothing more.
+ # The earlier test asked whether the live interpreter lived anywhere
+ # under `root/envs`, which is true of every working installation the
+ # product builds: comfy_host puts ComfyUI in `root/envs/comfyui-<key>`
+ # beside the engine's own env. So updating an installed FreeVideo from
+ # inside its own ComfyUI was refused as "the active environment", with
+ # nothing wrong and nothing to change. Only the engine's own
+ # environment names are its install target; a ComfyUI sibling is not.
+ engine_envs = {(root / 'envs' / name).resolve() for name in ENVIRONMENTS}
+ if root == prefix or prefix in root.parents or prefix in engine_envs:
+ raise ValueError('FreeVideo cannot install packages into the environment it is '
+ 'running in. Launch ComfyUI from its own environment, or run '
+ 'setup from the FreeVideo launcher instead of this panel.')
+ selected = os.environ.get('FREEVIDEO_HOME')
+ if selected and installation_root(self.source) != root:
+ raise ValueError('FREEVIDEO_HOME selects another installation. Change it before starting ComfyUI.')
+ return root
+
+ def inspect(self, value):
+ self.status()
+ if self.runner.busy:
+ raise ValueError('FreeVideo setup is already running')
+ root = self.validate_root(value.get('root'))
+ extra = value.get('extra_libraries', [])
+ if not isinstance(extra, list) or not isinstance(value.get('copy', False), bool):
+ raise ValueError('Invalid setup choices')
+ libraries = library_roots(discover_libraries(self.folder_paths) + extra)
+ self.logs.mkdir(parents=True, exist_ok=True)
+ manifest = self.logs / ('libraries-' + uuid.uuid4().hex + '.json')
+ save(manifest, dict(version=1, roots=libraries))
+ arguments = ['setup', '--reuse-models-manifest', str(manifest)]
+ if value.get('copy'):
+ arguments.append('--copy-existing-models')
+ self.selection = dict(root=str(root), arguments=arguments)
+ self.plan = None
+ self.state = dict(status='running', action='plan', libraries=libraries)
+ self.events.reset()
+ self.runner.start('plan', root, [*arguments, '--plan', '--json'])
+ return self.status()
+
+ def bind(self, root):
+ root = self.validate_root(root)
+ installation(self.source, dict(os.environ, FREEVIDEO_HOME=str(root)))
+ config = self.source / 'comfyui.json'
+ value = json.loads(config.read_text(encoding='utf-8')) if config.is_file() else {}
+ value['installation'] = str(root)
+ save(config, value)
+
+ def install(self, value):
+ self.status()
+ if self.runner.busy or not self.plan or value.get('plan_id') != self.state.get('plan_id'):
+ raise ValueError('Inspect this installation again before installing')
+ if value.get('accept_licenses') is not True or self.plan.get('errors'):
+ raise ValueError('Review the plan and accept its model/toolkit licenses first')
+ # Inputs are held server-side; the browser cannot swap paths after consent.
+ receipt = self.logs / ('approved-' + uuid.uuid4().hex + '.json')
+ save(receipt, self.plan)
+ selection = self.selection
+ self.state = dict(status='running', action='setup')
+ self.events.reset()
+ self.plan = None # Consent cannot be replayed, even if a worker fails.
+ self.runner.start('setup', selection['root'], [*selection['arguments'], '--plain',
+ '--yes', '--accept-model-license', '--approved-plan', str(receipt)])
+ return self.status()
+
+ def calibrate(self, value):
+ """Start the placement sweep on the same worker the installer uses.
+
+ It is a measurement, not a change: nothing it produces alters this
+ installation's placement. It can run for hours, so it goes to the
+ background runner and reports through the same status the panel already
+ polls, and it refuses to start beside queued generation work rather
+ than competing with it for the card.
+ """
+ state = self.status()
+ # Order matters: a machine with no installation cannot calibrate no
+ # matter how idle the worker is, and being told to wait for a task it
+ # never started sends it looking for the wrong thing.
+ root = installation_root(self.source)
+ if not root or not Path(root).is_dir():
+ raise ValueError('Install FreeVideo first. The placement test measures an '
+ 'installed engine; it cannot run before setup finishes.')
+ if self.runner.busy:
+ # Name what is running. The panel shows its progress, and a user
+ # who reads only this message otherwise has nothing to wait for.
+ running = {'plan': 'the installation check', 'setup': 'the installation',
+ 'calibrate': 'a placement test'}.get(
+ state.get('action'), state.get('action') or 'a setup task')
+ raise ValueError('Wait for %s to finish; its progress is in this panel.' % running)
+ repeats = value.get('repeats', 1)
+ if type(repeats) is not int or not 1 <= repeats <= 4:
+ raise ValueError('Repeats must be a whole number from 1 to 4')
+ prompt = str(value.get('prompt') or CALIBRATION_PROMPT).strip()
+ if not prompt:
+ raise ValueError('Enter a prompt for the calibration to encode')
+ run = self.logs / ('calibration-' + uuid.uuid4().hex)
+ run.mkdir(parents=True, exist_ok=True)
+ (run / 'prompt.txt').write_text(prompt, encoding='utf-8')
+ arguments = ['calibrate', '--prompt-file', str(run / 'prompt.txt'),
+ '--out', str(run / 'report'), '--repeats', str(repeats)]
+ # Hand the child a free card and a free lease, exactly as generation
+ # does. The resident worker holds the lease while it works and the idle
+ # prewarmer takes it between requests, and ComfyUI keeps its own models
+ # resident: releasing only the first two left a 16 GiB RTX 5060 Ti with
+ # ComfyUI's weights still on the card, and the text encoder died in
+ # dequantize_int8_embedding with CUDA out of memory before any
+ # measurement ran. The same prompt encodes in two and a half minutes on
+ # a card that size when it is actually free. Best effort and in this
+ # order: the workers exit first, so emptying the cache afterwards
+ # returns what they held.
+ released = []
+ for name, release in (('idle_prewarm', self._stop_idle_prewarm),
+ ('resident_worker', self._close_resident_worker),
+ ('comfy_models', self._release_comfy_models)):
+ try:
+ release()
+ released.append(name)
+ except Exception as error:
+ released.append('%s failed: %s' % (name, type(error).__name__))
+ self.state = dict(status='running', action='calibrate', run=str(run),
+ released=released)
+ self.events.reset()
+ self.runner.start('calibrate', str(root), arguments)
+ return self.status()
+
+ def _stop_idle_prewarm(self):
+ from .encoder_prewarm import IDLE
+ IDLE.stop()
+
+ def _close_resident_worker(self):
+ from .resident_process import OWNER
+ OWNER.close()
+
+ def _release_comfy_models(self):
+ # Not keep_engine_cache(): that exists so a generation handoff keeps
+ # the resident worker alive, and this one has already closed it.
+ from comfy import model_management as memory
+ memory.unload_all_models()
+ memory.soft_empty_cache()
+
+ def status(self):
+ for kind, value in self.events.drain():
+ if kind == 'started':
+ self.state['log'] = value
+ elif kind == 'progress':
+ self.state['progress'] = value
+ elif kind == 'log':
+ self.state['tail'] = (self.state.get('tail', '') + value)[-8192:]
+ elif kind == 'done':
+ self.state.update({key: value[key] for key in ('status', 'action', 'error', 'wall_seconds', 'log')})
+ if value['action'] == 'plan' and value['status'] in ('complete', 'failed'):
+ try:
+ self.plan = preflight_json(value['output'])
+ self.state.update(plan=self.plan, plan_id=uuid.uuid4().hex)
+ except ValueError:
+ self.state.update(status='failed', error='Setup inspection failed; see retained details')
+ elif value['action'] == 'setup' and value['status'] == 'complete':
+ try:
+ self.bind(value['root'])
+ self.state['ready'] = True
+ except (OSError, ValueError) as error:
+ self.state.update(status='failed', error=str(error))
+ return dict(self.state, **self.events.snapshot(), busy=self.runner.busy)
+
+
+def register():
+ from aiohttp import web
+ import folder_paths
+ from server import PromptServer
+ server = PromptServer.instance
+ if server is None or getattr(server, '_freevideo_setup', None) is not None:
+ return
+ setup = Setup(source_root(), folder_paths)
+ server._freevideo_setup = setup
+
+ @server.routes.get('/freevideo/setup')
+ async def info(request):
+ try:
+ root=setup.validate_root(request.query['root']) if request.query.get('root') else None
+ return web.json_response(dict(discovery=setup.discovery(root), task=setup.status()))
+ except (OSError, ValueError) as error:
+ return web.json_response({'error': str(error)}, status=400)
+
+ @server.routes.post('/freevideo/setup/{action}')
+ async def action(request):
+ # Same-origin clients must first read the per-process token. Blind
+ # cross-site forms cannot trigger filesystem changes or installations.
+ if not secrets.compare_digest(request.headers.get('X-FreeVideo-Setup', ''), setup.token):
+ return web.json_response({'error': 'Reload the setup panel'}, status=403)
+ try:
+ value = await request.json()
+ if not isinstance(value, dict):
+ raise ValueError('Invalid setup request')
+ name = request.match_info['action']
+ if name == 'inspect':
+ result = setup.inspect(value)
+ elif name in ('network-probe','network-source'):
+ result = setup.network_settings(value,probe=name=='network-probe')
+ elif name == 'network-token':
+ result = setup.download_token(value)
+ elif name == 'resources':
+ result = setup.resource_settings(value)
+ elif name == 'calibrate':
+ running, pending = server.prompt_queue.get_current_queue()
+ if running or pending:
+ raise ValueError('Finish or cancel queued ComfyUI requests before calibrating')
+ result = setup.calibrate(value)
+ elif name in ('compatibility-check', 'compatibility-save', 'compatibility-ack'):
+ result = setup.compatibility_settings(value, name)
+ elif name == 'install':
+ running, pending = server.prompt_queue.get_current_queue()
+ if running or pending:
+ raise ValueError('Finish or cancel queued ComfyUI requests before installing')
+ result = setup.install(value)
+ elif name == 'use':
+ if setup.runner.busy:
+ raise ValueError('Wait for setup to finish')
+ setup.bind(value.get('root', ''))
+ result = setup.discovery()
+ elif name == 'cancel':
+ setup.runner.cancel()
+ result = setup.status()
+ else:
+ raise ValueError('Unknown setup action')
+ return web.json_response(result)
+ except (OSError, ValueError, RuntimeError) as error:
+ return web.json_response({'error': str(error)}, status=400)
diff --git a/freevideo_engine/comfy_source.py b/freevideo_engine/comfy_source.py
new file mode 100644
index 0000000..aa5f6f7
--- /dev/null
+++ b/freevideo_engine/comfy_source.py
@@ -0,0 +1,60 @@
+"""Install a pinned ComfyUI application into a newly selected workspace."""
+import json
+from pathlib import Path
+
+from . import network
+from .monitoring import save
+
+RECEIPT = '.freevideo-comfy-source.json'
+SOURCE_DISK_BYTES = 2**30
+
+
+def specification():
+ # The same revision already supplies the native H3 encoder. Fetch the full
+ # application separately; never expand or modify the encoder's sparse tree.
+ spec = json.loads((Path(__file__).parent / 'dependencies.json').read_text(encoding='utf-8'))['encoder']
+ return dict(url=spec['comfy_url'], commit=spec['comfy_commit'])
+
+
+def validate_target(target):
+ target = Path(target)
+ if not target.exists() and not target.is_symlink():
+ return
+ try:
+ owned = json.loads((target / RECEIPT).read_text(encoding='utf-8')) == specification()
+ except (OSError, ValueError):
+ owned = False
+ if not owned or target.is_symlink():
+ raise ValueError('This folder already contains ComfyUI or other files. Choose Use existing ComfyUI or another installation folder: ' + str(target))
+
+
+def new_layout(directory):
+ if not isinstance(directory, str) or not directory.strip():
+ raise ValueError('Choose a folder for the new ComfyUI installation')
+ parent = Path(directory).expanduser().resolve()
+ if parent.exists() and not parent.is_dir():
+ raise ValueError('The installation folder is a file: ' + str(parent))
+ target = parent / 'ComfyUI'
+ validate_target(target)
+ return dict(root=str(target), python=None, portable=False, separate=True,
+ new_comfy=True, source_spec=specification())
+
+
+def download(target, *, plan, env, run):
+ """Publish a complete checkout with its receipt; retain interrupted stages."""
+ target = Path(target)
+ validate_target(target)
+ spec = specification()
+ if target.exists():
+ return network.clone(target, spec['url'], spec['commit'], network=plan, env=env, run=run)
+ stage = target.with_name(target.name + '.freevideo-download')
+ # A finished checkout survives cancellation before publication. Git fetch's
+ # own incomplete attempts are retained beside this directory by network.clone.
+ network.clone(stage, spec['url'], spec['commit'], network=plan, env=env, run=run)
+ if not all((stage / name).is_file() for name in ('main.py', 'folder_paths.py', 'requirements.txt', 'LICENSE')) or not (stage / 'comfy_api/latest').is_dir():
+ raise ValueError('The downloaded ComfyUI application is incomplete; files retained at ' + str(stage))
+ save(stage / RECEIPT, spec)
+ if target.exists() or target.is_symlink():
+ raise ValueError('The ComfyUI destination changed during installation; the downloaded files are retained: ' + str(stage))
+ stage.rename(target)
+ return target
diff --git a/freevideo_engine/comfy_upload.py b/freevideo_engine/comfy_upload.py
new file mode 100644
index 0000000..e8edcaa
--- /dev/null
+++ b/freevideo_engine/comfy_upload.py
@@ -0,0 +1,60 @@
+"""Stream browser media to ComfyUI input without buffering a clip in RAM."""
+from pathlib import Path, PureWindowsPath
+import uuid
+
+from .comfy_assets import media_kind
+
+MAX_BYTES = 2 * 1024 ** 3
+CHUNK_BYTES = 256 * 1024
+
+
+async def receive(reader, input_directory):
+ part = await reader.next()
+ if part is None or part.name != 'file' or not part.filename:
+ raise ValueError('Choose a media file')
+ name = PureWindowsPath(part.filename).name
+ if not name or name in ('.', '..') or any(c in name for c in ('/', '\\', ':', '\x00')):
+ raise ValueError('Invalid media filename')
+ media_kind(name)
+ base = Path(input_directory).resolve()
+ folder = base / 'freevideo' / uuid.uuid4().hex
+ folder.resolve().relative_to(base)
+ folder.mkdir(parents=True, exist_ok=False)
+ temporary = folder / (name + '.partial')
+ destination = folder / name
+ size = 0
+ with temporary.open('xb') as output:
+ while True:
+ chunk = await part.read_chunk(size=CHUNK_BYTES)
+ if not chunk:
+ break
+ size += len(chunk)
+ if size > MAX_BYTES:
+ raise ValueError('Media file exceeds 2 GiB; trim the clip before uploading')
+ output.write(chunk)
+ if size == 0:
+ raise ValueError('Media file is empty')
+ if await reader.next() is not None:
+ raise ValueError('Upload one media file at a time')
+ from .file_ops import publish
+ publish(temporary, destination)
+ return {'file': destination.relative_to(base).as_posix(), 'bytes': size}
+
+
+def register():
+ from aiohttp import web
+ import folder_paths
+ from server import PromptServer
+ server = PromptServer.instance
+ if server is None or getattr(server, '_freevideo_upload_registered', False):
+ return
+
+ @server.routes.post('/freevideo/media/upload')
+ async def upload(request):
+ try:
+ result = await receive(await request.multipart(), folder_paths.get_input_directory())
+ except (OSError, ValueError) as error:
+ return web.json_response({'error': str(error)}, status=400)
+ return web.json_response(result)
+
+ server._freevideo_upload_registered = True
diff --git a/freevideo_engine/compatibility.py b/freevideo_engine/compatibility.py
new file mode 100644
index 0000000..da1d060
--- /dev/null
+++ b/freevideo_engine/compatibility.py
@@ -0,0 +1,248 @@
+"""User-controlled workload compatibility after an unsettled GPU request.
+
+An interrupted owner/worker pair is evidence of an unfinished request, not a
+diagnosis of power, temperature, a bluescreen or a defective kernel. This state
+never bans execution and never treats OOM or a normal cancellation as a crash.
+"""
+from contextlib import contextmanager, closing
+import copy
+from functools import lru_cache
+import hashlib
+import json
+from pathlib import Path
+import sqlite3
+import sys
+import time
+
+
+LEVELS = (
+ dict(level=0, en='Off · automatic performance', zh='关闭 · 自动性能'),
+ dict(level=1, en='Light · heads ≤ 4, window 1', zh='轻度 · head ≤ 4,window 1', head=4),
+ dict(level=2, en='Compatible · heads ≤ 2, window 1', zh='兼容 · head ≤ 2,window 1', head=2),
+ dict(level=3, en='Strong · head 1, window 1, no prefetch', zh='加强 · head 1,window 1,关闭预取', head=1),
+)
+
+
+@lru_cache(maxsize=1)
+def policy_revision():
+ """Same identity in the frozen launcher and its deployed engine, without Torch.
+
+ Build versions expire automatic settings on release upgrades; source hashes
+ also cover a git checkout whose package version has not changed.
+ """
+ from . import __version__
+ root = Path(__file__).parent
+ if getattr(sys, 'frozen', False) and getattr(sys, '_MEIPASS', None):
+ root = Path(sys._MEIPASS) / 'engine-source' / 'freevideo_engine'
+ digest = hashlib.sha256(__version__.encode('utf-8'))
+ for name in ('compatibility.py', 'policy.py', 'resident_models.py', 'worker.py',
+ 'runtime.py', 'attention.py', 'adaptive.py', 'resource_history.py'):
+ digest.update(name.encode('utf-8'))
+ # An incomplete install must not reuse another build's automatic cap.
+ try:
+ content = (root / name).read_bytes()
+ except OSError:
+ content = b'missing'
+ digest.update(content.replace(b'\r\n', b'\n'))
+ return digest.hexdigest()
+
+
+def current_settings(value):
+ value = copy.deepcopy(value)
+ revision = policy_revision()
+ # Older releases only recorded the automatic checkbox. Preserve their
+ # explicit opt-outs; new rows track manual choices independently of it.
+ origin = value.get('origin', 'automatic' if value.get('automatic', True) else 'manual')
+ manual_level = value.get('manual_level', value['level'] if origin == 'manual' else 0)
+ if value.get('policy_revision') != revision:
+ if origin == 'automatic' and value['level']:
+ value['reset'] = dict(reason='policy_updated', previous_level=value['level'],
+ previous_revision=value.get('policy_revision'), revision=revision,
+ previous_trigger=value.get('trigger') or value.get('notice'))
+ value['level'] = manual_level
+ origin = 'manual' if manual_level else 'automatic'
+ value['notice'] = None
+ value.pop('trigger', None)
+ value.update(origin=origin, manual_level=manual_level, policy_revision=revision)
+ return value
+
+
+def device_key(identity):
+ """Missing optional metadata disables compatibility, never invents a device."""
+ if not isinstance(identity, dict):
+ return None
+ gpu = identity.get('gpu')
+ uuid = (gpu.get('uuid') if isinstance(gpu, dict) else None) or identity.get('gpu_uuid')
+ system = identity.get('system')
+ if any(not isinstance(value, str) or not value.strip() for value in (uuid, system)):
+ return None
+ return hashlib.sha256(json.dumps([uuid, system]).encode()).hexdigest()
+
+
+def unavailable():
+ return dict(available=False, reason='device_identity_unavailable',
+ level=0, automatic=False, notice=None, levels=LEVELS)
+
+
+class Store:
+ def __init__(self, root):
+ self.path = Path(root) / 'compatibility.sqlite3'
+
+ @contextmanager
+ def transaction(self):
+ self.path.parent.mkdir(parents=True, exist_ok=True)
+ with closing(sqlite3.connect(str(self.path), timeout=10, isolation_level=None)) as db:
+ db.row_factory = sqlite3.Row
+ db.execute('PRAGMA synchronous=FULL')
+ db.execute('BEGIN IMMEDIATE')
+ try:
+ db.execute('CREATE TABLE IF NOT EXISTS settings (device TEXT PRIMARY KEY, value TEXT NOT NULL)')
+ db.execute('CREATE TABLE IF NOT EXISTS incidents (id TEXT PRIMARY KEY, device TEXT NOT NULL, time REAL, value TEXT NOT NULL)')
+ yield db
+ db.commit()
+ except BaseException:
+ db.rollback()
+ raise
+
+ @staticmethod
+ def read_row(db, key):
+ row = db.execute('SELECT value FROM settings WHERE device=?', (key,)).fetchone()
+ return json.loads(row[0]) if row else dict(level=0, automatic=True, notice=None)
+
+ @staticmethod
+ def write_row(db, key, value):
+ db.execute('INSERT OR REPLACE INTO settings VALUES (?,?)', (key, json.dumps(value)))
+
+ def status(self, identity, *, persist=False):
+ key = device_key(identity)
+ if key is None:
+ return unavailable()
+ if not self.path.exists():
+ return dict(current_settings(dict(level=0, automatic=True, notice=None)), levels=LEVELS, available=True)
+ with closing(sqlite3.connect(self.path.resolve().as_uri()+'?mode=ro', uri=True, timeout=10)) as db:
+ value = self.read_row(db, key)
+ selected = current_settings(value)
+ if persist and selected != value:
+ with self.transaction() as db:
+ # Re-read under the lock so a concurrent user edit always wins.
+ selected = current_settings(self.read_row(db, key))
+ self.write_row(db, key, selected)
+ return dict(selected, levels=LEVELS, available=True)
+
+ def set(self, identity, level, automatic):
+ if type(level) is not int or not 0 <= level <= 3 or type(automatic) is not bool:
+ raise ValueError('Compatibility level must be 0–3; automatic must be a boolean')
+ key = device_key(identity)
+ if key is None:
+ return unavailable()
+ with self.transaction() as db:
+ value = dict(level=level, automatic=automatic, notice=None, origin='manual',
+ manual_level=level, policy_revision=policy_revision())
+ self.write_row(db, key, value)
+ return dict(value, levels=LEVELS, available=True)
+
+ def acknowledge(self, identity, notice_id):
+ key = device_key(identity)
+ if key is None:
+ return unavailable()
+ with self.transaction() as db:
+ value = current_settings(self.read_row(db, key))
+ if value.get('notice') and value['notice']['id'] == notice_id:
+ value['notice'] = None
+ self.write_row(db, key, value)
+ return dict(value, levels=LEVELS, available=True)
+
+ def incident(self, row, reason):
+ # Only a launched video worker can be a load-related interrupted attempt.
+ if row.get('purpose') != 'generation' or not row.get('worker'):
+ return
+ identity = row.get('identity')
+ key = device_key(identity)
+ if key is None:
+ return
+ with self.transaction() as db:
+ if db.execute('SELECT 1 FROM incidents WHERE id=?', (row['id'],)).fetchone():
+ return
+ value = current_settings(self.read_row(db, key))
+ revision = (row.get('details') or {}).get('compatibility_revision')
+ incident = dict(id=row['id'], reason=reason, phase=row.get('phase'),
+ config=row['config'], geometry=row['geometry'], diagnosis='unknown', time=time.time(),
+ policy_revision=revision, applicable=revision == policy_revision())
+ db.execute('INSERT INTO incidents VALUES (?,?,?,?)', (row['id'], key, incident['time'], json.dumps(incident)))
+ # Do not re-enable a user's explicit opt-out. One incident takes the
+ # default to 2; another while compatible takes it to 3, at most once.
+ if value['automatic'] and incident['applicable']:
+ value['level'] = min(3, max(2, value['level'] + 1))
+ value['origin'] = 'automatic'
+ value.pop('reset', None)
+ value['notice'] = dict(id=row['id'], level=value['level'], reason=reason,
+ phase=row.get('phase'), diagnosis='unknown', policy_revision=revision)
+ value['trigger'] = dict(value['notice'])
+ self.write_row(db, key, value)
+ db.execute('DELETE FROM incidents WHERE device=? AND id NOT IN '
+ '(SELECT id FROM incidents WHERE device=? ORDER BY time DESC LIMIT 64)', (key, key))
+
+
+def apply(profile, state):
+ """Cap work grouping, never the request or backend; settings are explicit consent."""
+ value = copy.deepcopy(profile)
+ level = state['level']
+ if type(level) is not int or not 0 <= level <= 3:
+ raise ValueError('Invalid compatibility level')
+ changes = {}
+ if level:
+ engine = value['engine']
+ for name, cap in (('head_chunk', LEVELS[level]['head']), ('window_batch', 1), ('head_parallelism', 1)):
+ prior = engine.get(name, 0 if name == 'head_chunk' else 1)
+ selected = cap if prior == 0 else min(prior, cap)
+ if selected != prior:
+ engine[name] = selected
+ changes['engine.'+name] = dict(before=prior, after=selected)
+ if level == 3:
+ for section in ('engine', 'decoder'):
+ if value.get(section, {}).get('prefetch', True):
+ value.setdefault(section, {})['prefetch'] = False
+ changes[section+'.prefetch'] = dict(before=True, after=False)
+ # Never let a policy copy or a saved profile silently undo the cap.
+ if isinstance(value.get('policy'), dict):
+ value['policy'].update(engine=copy.deepcopy(value['engine']), decoder=copy.deepcopy(value.get('decoder', {})))
+ decision = dict(level=level, automatic=state['automatic'], changes=changes,
+ reason='User compatibility setting; smaller work groups and optional serialized transfers. '
+ 'No power-spike or crash-prevention guarantee; geometry, steps and backend unchanged.',
+ numerical_class='chunking-may-change-floating-point-results' if changes else 'unchanged')
+ for field in ('origin', 'policy_revision', 'reset', 'notice', 'trigger'):
+ if field in state:
+ decision[field] = copy.deepcopy(state[field])
+ if state.get('origin') == 'automatic' and level:
+ decision['reason'] = ('Automatic compatibility after an unfinished request or reported GPU error '
+ 'on this policy revision; the cause is unknown. Geometry, steps and backend unchanged.')
+ if state.get('available') is False:
+ decision.update(available=False, reason='Compatibility unavailable: device identity is missing; profile unchanged.')
+ return value, decision
+
+
+def installed_identity(root):
+ try:
+ machine = json.loads((Path(root)/'machine.json').read_text(encoding='utf-8'))
+ except (OSError, ValueError):
+ # Setup may not yet have written this optional settings metadata.
+ return {}
+ if not isinstance(machine, dict):
+ return {}
+ gpu_uuid = machine.get('gpu_uuid')
+ # OS is an installation property here, never a GPU probe.
+ import platform
+ return dict(gpu_uuid=gpu_uuid, system=platform.system())
+
+
+def check_installation(root):
+ """Called at app startup, without querying or initializing a GPU."""
+ root = Path(root)
+ identity = installed_identity(root)
+ if device_key(identity) is None:
+ return unavailable()
+ history = root/'resource-history.sqlite3'
+ if history.is_file():
+ from .resource_history import ResourceHistory
+ ResourceHistory(history).recover_pending()
+ return Store(root).status(identity, persist=True)
diff --git a/freevideo_engine/compatibility_ui.py b/freevideo_engine/compatibility_ui.py
new file mode 100644
index 0000000..dd3dde0
--- /dev/null
+++ b/freevideo_engine/compatibility_ui.py
@@ -0,0 +1,77 @@
+"""Small native compatibility controls shared by the EXE and portable launcher."""
+from pathlib import Path
+
+from .compatibility import Store, installed_identity, check_installation, LEVELS
+
+
+def notify(window, root, zh=True):
+ from tkinter import messagebox
+ root = Path(root)
+ if not (root/'machine.json').is_file():
+ return
+ try:
+ value = check_installation(root)
+ if value.get('notice'):
+ level = value['level']
+ messagebox.showinfo('FreeVideo',
+ ('上次生成未正常结束,已自动开启兼容性设置(第 %d 档)。\n'
+ '可在“兼容性设置”中调节或关闭。分辨率、时长和步数保持不变。' % level) if zh else
+ ('The previous generation ended unexpectedly. Compatibility level %d is now enabled.\n'
+ 'You can adjust or disable it in Compatibility settings. Resolution, duration and steps are unchanged.' % level), parent=window)
+ Store(root).acknowledge(installed_identity(root), value['notice']['id'])
+ except Exception as error:
+ messagebox.showerror('FreeVideo', str(error), parent=window)
+
+
+def open_settings(window, root, zh=True):
+ import tkinter as tk
+ from tkinter import ttk, messagebox
+ root = Path(root)
+ try:
+ identity = installed_identity(root)
+ state = check_installation(root)
+ except Exception as error:
+ messagebox.showerror('FreeVideo', str(error), parent=window)
+ return
+ if not state['available']:
+ messagebox.showinfo('FreeVideo',
+ '尚未识别到设备信息,兼容性设置暂不可用。可继续正常使用 FreeVideo。' if zh else
+ 'Device information is unavailable. Compatibility settings are disabled; you can continue using FreeVideo.',
+ parent=window)
+ return
+ dialog = tk.Toplevel(window)
+ dialog.title('FreeVideo · '+('兼容性设置' if zh else 'Compatibility'))
+ dialog.transient(window)
+ dialog.geometry('520x330')
+ body = ttk.Frame(dialog, padding=24); body.pack(fill='both', expand=True)
+ text = tk.StringVar()
+ level = tk.DoubleVar(value=state['level'])
+ automatic = tk.BooleanVar(value=state['automatic'])
+ ttk.Label(body, textvariable=text, anchor='center').pack(fill='x', pady=(8, 14))
+ def changed(value=None):
+ selected = int(round(level.get()))
+ text.set(LEVELS[selected]['zh' if zh else 'en'])
+ if value is not None and selected == 0:
+ automatic.set(False)
+ scale = ttk.Scale(body, from_=0, to=3, variable=level, command=changed)
+ scale.pack(fill='x', pady=(0, 16)); changed()
+ def released(_):
+ level.set(round(level.get())); changed()
+ if int(level.get()) == 0: automatic.set(False)
+ scale.bind('<ButtonRelease-1>', released)
+ ttk.Checkbutton(body, variable=automatic, text='异常中断后自动启用/提高兼容档' if zh else
+ 'Enable / increase compatibility after an unexpected interruption').pack(anchor='w')
+ ttk.Label(body, text=('仅调整分块和预取,可能降低速度并产生浮点差异。\n'
+ '不改变分辨率、时长、步数或 attention 后端。\n'
+ '不能保证避免设备故障;设置从下一次生成生效。') if zh else
+ ('Adjusts grouping and prefetch; may reduce speed and change floating-point results.\n'
+ 'Keeps resolution, duration, steps and attention.\n'
+ 'Cannot guarantee hardware stability. Applies to the next generation.'),
+ style='Muted.TLabel', wraplength=460).pack(fill='x', pady=16)
+ def save():
+ try:
+ Store(root).set(identity, int(round(level.get())), automatic.get())
+ dialog.destroy()
+ except Exception as error:
+ messagebox.showerror('FreeVideo', str(error), parent=dialog)
+ ttk.Button(body, text='保存' if zh else 'Save', command=save, style='Primary.TButton').pack(anchor='e')
diff --git a/freevideo_engine/compilation_diagnostics.py b/freevideo_engine/compilation_diagnostics.py
new file mode 100644
index 0000000..1e032c2
--- /dev/null
+++ b/freevideo_engine/compilation_diagnostics.py
@@ -0,0 +1,73 @@
+"""Read existing PyTorch compiler counters; never import or profile torch."""
+import math
+import sys
+
+
+TIMERS = {
+ 'frame_compile': '_compile.compile_inner',
+ 'torch_build_hash': 'inductor_codecache_torch_key',
+ 'backend_compile': 'compile_fx_inner',
+ 'graph_lowering': 'GraphLowering.run',
+ 'code_generation': 'GraphLowering.codegen',
+ 'module_load': 'PyCodeCache.load_by_key_path',
+ 'kernel_reload': 'reload_kernel_in_parent',
+ 'async_compile_wait': 'async_compile.wait',
+ 'autograd_cache_load': 'AOTAutogradCache.inductor_load',
+ 'autotune': 'CachingAutotuner.benchmark_all_configs',
+}
+CACHES = {
+ 'graph_hits': ('inductor', 'fxgraph_cache_hit'),
+ 'graph_misses': ('inductor', 'fxgraph_cache_miss'),
+ 'graph_bypasses': ('inductor', 'fxgraph_cache_bypass'),
+ 'autograd_hits': ('aot_autograd', 'autograd_cache_hit'),
+ 'autograd_misses': ('aot_autograd', 'autograd_cache_miss'),
+ 'unique_graphs': ('stats', 'unique_graphs'),
+}
+SCOPE = ('Existing PyTorch host timers, including tracing, cache lookup/loading and code generation. '
+ 'Timers nest and may overlap GPU work; do not add them together. '
+ 'Missing fields are unavailable, not evidence of zero compilation.')
+
+
+def _number(value):
+ return type(value) in (float, int) and math.isfinite(value) and value >= 0
+
+
+def sanitize(value):
+ """Keep only fixed numeric fields in diagnostic reports."""
+ if not isinstance(value, dict):
+ return {}
+ result = {'available': value.get('available') is True}
+ for group, allowed in (('time_seconds', TIMERS), ('cache_counts', CACHES)):
+ values = value.get(group)
+ if isinstance(values, dict):
+ result[group] = {key: values[key] for key in allowed if _number(values.get(key))}
+ return result
+
+
+def snapshot():
+ module = sys.modules.get('torch._dynamo.utils')
+ timers = getattr(module, 'compilation_time_metrics', None)
+ if not isinstance(timers, dict):
+ return {'available': False}
+ result = dict(available=True, time_seconds={}, cache_counts={})
+ for label, key in TIMERS.items():
+ values = timers.get(key)
+ if isinstance(values, list) and all(_number(value) for value in values):
+ result['time_seconds'][label] = sum(values)
+ counters = getattr(module, 'counters', {})
+ for label, (group, key) in CACHES.items():
+ values = counters.get(group, {}) if isinstance(counters, dict) else {}
+ value = values.get(key) if isinstance(values, dict) else None
+ if _number(value):
+ result['cache_counts'][label] = value
+ return result
+
+
+def difference(before, after):
+ result = {'available': after.get('available') is True}
+ for group in ('time_seconds', 'cache_counts'):
+ previous = before.get(group, {})
+ result[group] = {key: value - previous.get(key, 0)
+ for key, value in after.get(group, {}).items()
+ if value >= previous.get(key, 0)}
+ return result
diff --git a/freevideo_engine/compute_budget.py b/freevideo_engine/compute_budget.py
new file mode 100644
index 0000000..449c1ed
--- /dev/null
+++ b/freevideo_engine/compute_budget.py
@@ -0,0 +1,87 @@
+"""Torch-free proposals for spending measured spare memory on compute.
+
+These are trial bounds, not predictions of speed or certified peak memory.
+No device name/nominal-capacity tier enables a candidate on another machine.
+"""
+import math
+
+GiB = 2**30
+CHOICES = {'head_parallelism': (1, 2, 4), 'head_chunk': (2, 4, 8, 16),
+ 'window_batch': (1, 2, 4, 8, 16, 32),
+ 'ff_chunk': (512, 1024, 2048, 4096, 8192, 16384),
+ 'projection_chunk': (512, 1024, 2048, 4096, 8192)}
+
+
+def valid_parallelism(engine):
+ count = engine.get('head_parallelism', 1)
+ return (type(count) is int and count in CHOICES['head_parallelism'] and
+ (count == 1 or (engine.get('resident_blocks') == 50 and
+ not engine.get('attention_cpu_outputs') and
+ not engine.get('grouped_attention_outputs') and
+ type(engine.get('head_chunk')) is int and engine['head_chunk'] > 0)))
+
+
+def spare_compute_trials(row, engine, peak_bytes, headroom_bytes):
+ """Keep consumer defaults; nominate larger work only from a full baseline.
+
+ The caller authenticates matching complete geometry/configuration and clamps
+ headroom against live memory. Each proposal still needs local full-request
+ timing, memory and raw output validation before it can become a default.
+ """
+ if (not valid_parallelism(engine) or engine.get('resident_blocks') != 50 or
+ engine.get('attention_cpu_outputs') or
+ engine.get('grouped_attention_outputs') or
+ type(engine.get('head_chunk')) is not int or engine['head_chunk'] <= 0 or
+ any(type(v) not in (int, float) or not math.isfinite(v) or v <= 0
+ for v in (peak_bytes, headroom_bytes))):
+ return []
+ measured = row.get('engine', {})
+ weights = measured.get('config', {}).get('resident_weight_bytes')
+ if type(weights) not in (int, float) or not math.isfinite(weights) or not 0 < weights < peak_bytes:
+ # A byte-exact resident-weight measurement avoids treating a nominal
+ # block-size guess as extra activation memory on another model.
+ return []
+ lanes = engine.get('head_parallelism', 1)
+ workspace = max(GiB // 2, (peak_bytes - weights) / lanes)
+ affordable = [n for n in CHOICES['head_parallelism'] if n > lanes and
+ (n - lanes) * workspace <= headroom_bytes]
+ result = []
+ if affordable:
+ result.append(({'head_parallelism': max(affordable)},
+ dict(estimated_extra_bytes=int((max(affordable) - lanes) * workspace),
+ workspace_per_lane_bytes=int(workspace),
+ reason='Overlap unchanged head groups; extra live workspace uses measured spare VRAM')))
+ canvas = row.get('geometry', {})
+ rows = canvas.get('video_tokens')
+ if type(rows) is not int or rows <= 0:
+ return result
+ # Bound rows using all known conditioning. Missing token metadata disables
+ # row-based proposals, rather than guessing that reference inputs are free.
+ shape = measured.get('conditioning_shape')
+ if not isinstance(shape, (tuple, list)) or not shape or type(shape[0]) is not int or shape[0] <= 0:
+ return result
+ rows += shape[0]
+ for field in ('reference_video_tokens', 'reference_audio_tokens'):
+ count = measured.get('conditioning_info', {}).get(field, canvas.get(field, 0))
+ if type(count) is not int or count < 0:
+ return result
+ rows += count
+ # Deliberately bounded hypotheses, not a full-run peak extrapolated from
+ # one-step probes. GEMM row shapes may change rounding, so they remain
+ # arithmetic trials even when their temporary buffers appear affordable.
+ for name, per_item in (('ff_chunk', 128 * 1024), ('projection_chunk', 64 * 1024),
+ ('window_batch', 4 * rows * max(1, engine['head_chunk']) * 128 * 2)):
+ if name == 'ff_chunk' and engine.get('linear_compute') != 'native-fp8':
+ continue
+ if name == 'window_batch' and (engine.get('window_varlen') or
+ engine['attention'].split('/')[-1] in ('fa2', 'fa4')):
+ continue
+ current = engine.get(name, 0)
+ upper = rows if name != 'window_batch' else canvas.get('latent_frames', 1)
+ values = [n for n in CHOICES[name] if current < n <= upper and
+ (n - current) * per_item <= headroom_bytes]
+ if values:
+ value = max(values)
+ result.append(({name: value}, dict(estimated_extra_bytes=(value-current)*per_item,
+ reason='Test fewer serial chunks within measured spare VRAM; output equivalence is unverified')))
+ return result
diff --git a/freevideo_engine/conditioning.py b/freevideo_engine/conditioning.py
new file mode 100644
index 0000000..bf08a32
--- /dev/null
+++ b/freevideo_engine/conditioning.py
@@ -0,0 +1,27 @@
+"""The shared native H3 text-conditioning contract; no encoder implementation."""
+import torch
+
+
+def to_cache(conditioning, prompt=None, task='t2va'):
+ from .media_request import TASKS
+ if task not in TASKS:
+ raise ValueError('Unsupported H3 conditioning task')
+ if len(conditioning) != 1:
+ raise ValueError('FreeVideo expects one H3 text conditioning sequence')
+ embeds, extra = conditioning[0]
+ tags = extra.get('minimax_token_tags')
+ if embeds.ndim != 3 or embeds.shape[0] != 1 or embeds.shape[-1] != 5120 or not embeds.shape[1]:
+ raise ValueError('Use H3 layer-50 text conditioning [1,L,5120]')
+ if not embeds.is_floating_point() or not bool(torch.isfinite(embeds).all()):
+ raise ValueError('H3 conditioning must contain finite embeddings')
+ if tags is None or tags.ndim != 1 or len(tags) != embeds.shape[1]:
+ raise ValueError('H3 conditioning requires one token tag per embedding')
+ if not bool(((tags == 0) | (tags == 1)).all()) or (task == 't2va' and not bool((tags == 1).all())):
+ raise ValueError('Unexpected H3 modality tags for ' + task)
+ if task in ('i2va', 'l2va', 'fl2va') and not bool((tags == 0).any()):
+ raise ValueError('Keyframe tasks require vision conditioning')
+ result = {'prompt_embeds': embeds[0].detach().to('cpu', torch.bfloat16),
+ 'text_token_tags': tags.detach().to('cpu', torch.long)}
+ if prompt is not None:
+ result['prompt'] = prompt
+ return result
diff --git a/freevideo_engine/decode.py b/freevideo_engine/decode.py
new file mode 100644
index 0000000..052ba94
--- /dev/null
+++ b/freevideo_engine/decode.py
@@ -0,0 +1,276 @@
+"""Decode with original tiles and FP32 normalization; optional Linear compute cache."""
+import gc
+import json
+from pathlib import Path
+import time
+
+import torch
+
+from .offload import LayerOffloader, pin_layer_weights
+from .vae_tiles import TileDecoder
+
+
+def decode_to_file(latents, audio_latents, out_path, *, base, offload=False, prefetch=True, tile_group=False,
+ preload=False, pin_weights=False, artifacts_dir=None, phase_callback=None,
+ stream_output=False, stream_weights=False, resident_blocks=0, model_cache=None,
+ linear_compute_cache=False):
+ from diffusers import AutoencoderKLMiniMaxH3, AutoencoderKLMiniMaxH3Audio
+ from diffusers.utils.export_utils import encode_video
+ from src.inference.render import PIXEL_MEAN, PIXEL_STD, FPS
+
+ started = time.perf_counter()
+ def phase(name):
+ if phase_callback is not None:
+ phase_callback(name)
+ phase('vae_load')
+ print(json.dumps({'event': 'decode_phase', 'phase': 'Loading video VAE'}), flush=True)
+ vae = None
+ weight_source = None
+ compute_cache_metrics = None
+ if type(linear_compute_cache) is not bool:
+ raise ValueError('linear_compute_cache must be a boolean')
+ if type(resident_blocks) is not int or resident_blocks < 0:
+ raise ValueError('VAE resident block count is outside this decoder')
+ if resident_blocks and not offload:
+ raise ValueError('Partial VAE residency requires decoder offloading')
+ if pin_weights and not offload:
+ raise ValueError('Pinned VAE weights require layer offloading')
+ if stream_weights and (not offload or pin_weights or preload):
+ raise ValueError('Streamed VAE weights require offloading without host preload or whole-model pinning')
+ if model_cache is not None and not offload:
+ from .resident_models import key, file_identity
+ vae_key = key([file_identity(path) for path in sorted((Path(base)/'vae').iterdir()) if path.is_file()])
+ if linear_compute_cache:
+ vae_key = key([vae_key, 'decoder-fp16-linear-v1'])
+ vae = model_cache.take('video_vae', vae_key)
+ vae_cache_hit = vae is not None
+ if vae is None:
+ if linear_compute_cache:
+ from .vae_weights import load_video_decoder
+ layers = AutoencoderKLMiniMaxH3.load_config(
+ str(Path(base)/'vae'), local_files_only=True)['decoder_num_layers']
+ count = resident_blocks if offload else layers
+ if stream_weights:
+ from .streamed_weights import SafetensorLayers
+ weight_source = SafetensorLayers((Path(base)/'vae').glob('*.safetensors'),
+ [f'decoder.transformer_blocks.{index}.' for index in range(count, layers)],
+ intermediate_dtype=torch.float32)
+ vae, compute_cache_metrics = load_video_decoder(base, resident_blocks=count,
+ weight_source=weight_source)
+ else:
+ # Diffusers' default safetensors loader uses mmap. Clean mappings
+ # are reclaimable on Linux, but Windows charges the section view
+ # against system commit until every tensor/view is gone. Decode
+ # happens after sampling has already consumed most of the budget,
+ # so make this one-shot VAE load bounded on Windows as well.
+ from .system import windows
+ options = {'disable_mmap': True} if windows() else {}
+ try:
+ vae = AutoencoderKLMiniMaxH3.from_pretrained(
+ str(base), subfolder='vae', local_files_only=True, **options)
+ except TypeError as error:
+ if options and 'disable_mmap' in str(error):
+ raise RuntimeError(
+ 'The Windows runtime needs a Diffusers build with disable_mmap support; rerun setup.') from error
+ raise
+ vae.eval().requires_grad_(False)
+ if type(resident_blocks) is not int or not 0 <= resident_blocks <= len(vae.decoder.transformer_blocks):
+ raise ValueError('VAE resident block count is outside this decoder')
+ vae.encoder = None
+ vae.quant_conv = None
+ if stream_weights and weight_source is None:
+ from .streamed_weights import SafetensorLayers
+ weight_source = SafetensorLayers((Path(base)/'vae').glob('*.safetensors'),
+ [f'decoder.transformer_blocks.{index}.' for index in range(resident_blocks, len(vae.decoder.transformer_blocks))])
+ # Generation only uses the decoder; release unused encoder mappings.
+ vae.post_quant_conv.to('cuda')
+ if offload:
+ if resident_blocks and not linear_compute_cache:
+ from .streamed_weights import SafetensorLayers
+ from .offload import cpu_weights
+ # Read through short-lived mappings. Reading from the loader's
+ # shared shard mapping would keep already-uploaded pages resident
+ # on the host while unrelated offloaded tensors still refer to it.
+ resident_source = SafetensorLayers((Path(base)/'vae').glob('*.safetensors'),
+ [f'decoder.transformer_blocks.{index}.' for index in range(resident_blocks)])
+ for index, block in enumerate(list(vae.decoder.transformer_blocks)[:resident_blocks]):
+ layout = [(value, name, value.dtype, 0, value.numel(), value.shape)
+ for name, value in cpu_weights(block)]
+ with resident_source.layer(index, layout) as weights:
+ for parameter, name, *_ in layout:
+ parameter.data = weights[name].to('cuda')
+ del resident_source, layout, weights, parameter, block
+ for name, child in vae.decoder.named_children():
+ if name != 'transformer_blocks':
+ child.to('cuda')
+ vae.decoder.register_tokens.data = vae.decoder.register_tokens.data.to('cuda')
+ offloaded_blocks = list(vae.decoder.transformer_blocks)[resident_blocks:]
+ pinned_bytes = pin_layer_weights(offloaded_blocks) if pin_weights else 0
+ if pin_weights:
+ # The text-to-video decoder never calls these modules. Releasing
+ # their mappings also drops references to shards shared with the
+ # now-pinned decoder weights.
+ vae.encoder = None
+ vae.quant_conv = None
+ gc.collect()
+ tile_decoder = TileDecoder(vae.decoder, prefetch=prefetch, weight_source=weight_source,
+ resident_blocks=resident_blocks) if tile_group else None
+ if tile_decoder is not None:
+ tile_decoder.install(vae)
+ offloader = tile_decoder.offloader if tile_decoder is not None else LayerOffloader(offloaded_blocks, prefetch=prefetch, weight_source=weight_source)
+ else:
+ pinned_bytes = 0
+ vae.decoder.to('cuda')
+ offloader = None
+ tile_decoder = None
+ # Release unused encoder views of the checkpoint shards. Otherwise
+ # they keep CPU mappings alive after all decoder weights move to GPU.
+ vae.encoder = None
+ vae.quant_conv = None
+ gc.collect()
+ if model_cache is not None:
+ model_cache.put('video_vae', vae_key, vae)
+ preload_seconds = 0.
+ if offload and preload:
+ # Touch the immutable files sequentially after allocating transfer
+ # buffers, avoiding small random page faults in the offload thread.
+ read_started = time.perf_counter()
+ buffer = bytearray(8 * 1024 * 1024)
+ for path in sorted((Path(base) / 'vae').glob('*.safetensors')):
+ with path.open('rb', buffering=0) as stream:
+ while stream.readinto(buffer):
+ pass
+ del buffer
+ preload_seconds = time.perf_counter() - read_started
+ mean = torch.tensor(vae.config.latents_mean, device='cuda').view(1, -1, 1, 1, 1)
+ std = torch.tensor(vae.config.latents_std, device='cuda').view(1, -1, 1, 1, 1)
+ torch.cuda.synchronize()
+ load_seconds = time.perf_counter() - started
+ decode_started = time.perf_counter()
+ phase('video_decode')
+ print(json.dumps({'event': 'decode_phase', 'phase': 'Decoding video tiles'}), flush=True)
+ offload_stats = None
+ rgb_mapping = None
+ streamed_timings = {}
+ try:
+ # Autocast follows the original renderer. The opt-in Linear cache stores
+ # its existing FP16 compute values once; all other weights stay FP32.
+ with torch.autocast(device_type='cuda', dtype=torch.float16, cache_enabled=not offload):
+ if stream_output:
+ from .decode_stream import render_rgb
+ directory = Path(artifacts_dir) if artifacts_dir else Path(out_path).with_suffix('.artifacts')
+ def decoded(done, total, elapsed):
+ print(json.dumps(dict(event='decode_progress', done=done, total=total,
+ elapsed_seconds=elapsed)), flush=True)
+ frames, rgb_mapping, streamed_timings = render_rgb(vae, latents * std + mean,
+ PIXEL_MEAN, PIXEL_STD, directory/'rgb.npy', progress=decoded)
+ else:
+ video = vae.decode(latents * std + mean, return_dict=False)[0]
+ video = video.cpu()
+ if offloader is not None:
+ offload_stats = offloader.stats()
+ finally:
+ if offloader is not None:
+ offloader.close()
+ if tile_decoder is not None:
+ tile_decoder.close()
+ try:
+ del vae, offloader, tile_decoder, mean, std
+ if offload:
+ del offloaded_blocks
+ gc.collect()
+ torch.cuda.empty_cache()
+ video_decode_seconds = streamed_timings.get('video_decode_seconds', time.perf_counter() - decode_started)
+ phase('video_postprocess')
+ # Bound postprocessing memory by frame chunks; use the same FP32 operations
+ # and rounding before converting to uint8. Do this on CPU after decoding.
+ if not stream_output:
+ frames = torch.empty((video.shape[2], video.shape[3], video.shape[4], 3), dtype=torch.uint8)
+ pixel_mean = torch.tensor(PIXEL_MEAN).view(1, -1, 1, 1, 1)
+ pixel_std = torch.tensor(PIXEL_STD).view(1, -1, 1, 1, 1)
+ for start in range(0, video.shape[2], 8):
+ chunk = (video[:, :, start:start + 8].float() * pixel_std + pixel_mean).clamp_(0, 1)
+ frames[start:start + chunk.shape[2]] = (chunk[0].permute(1, 2, 3, 0) * 255).round().to(torch.uint8)
+ del video
+ artifact_seconds = streamed_timings.get('decoded_artifact_save_seconds', 0.)
+ if artifacts_dir:
+ import numpy as np
+ phase('rgb_save')
+ tick = time.perf_counter()
+ artifacts_dir = Path(artifacts_dir)
+ artifacts_dir.mkdir(parents=True, exist_ok=True)
+ if not stream_output:
+ np.save(artifacts_dir / 'rgb.npy', frames.numpy(), allow_pickle=False)
+ artifact_seconds += time.perf_counter() - tick
+ audio_start = time.perf_counter()
+ phase('audio_load_decode')
+ print(json.dumps({'event': 'decode_phase', 'phase': 'Loading and decoding audio'}), flush=True)
+ audio_vae = None
+ if model_cache is not None:
+ from .resident_models import key, file_identity
+ audio_key = key([file_identity(path) for path in sorted((Path(base)/'audio_vae').iterdir()) if path.is_file()])
+ audio_vae = model_cache.take('audio_vae', audio_key)
+ model_cache.make_room('audio_vae', 1*2**30, model=audio_vae, ram_need=256*2**20)
+ audio_cache_hit = audio_vae is not None
+ if audio_vae is None:
+ from .system import windows
+ options = {'disable_mmap': True} if windows() else {}
+ try:
+ audio_vae = AutoencoderKLMiniMaxH3Audio.from_pretrained(
+ str(base), subfolder='audio_vae', local_files_only=True, **options).to('cuda')
+ except TypeError as error:
+ if options and 'disable_mmap' in str(error):
+ raise RuntimeError(
+ 'The Windows runtime needs a Diffusers build with disable_mmap support; rerun setup.') from error
+ raise
+ if model_cache is not None:
+ model_cache.put('audio_vae', audio_key, audio_vae)
+ audio_vae.eval().requires_grad_(False)
+ audio_mean = torch.tensor(audio_vae.config.latents_mean, device='cuda').view(1, -1, 1)
+ audio_std = torch.tensor(audio_vae.config.latents_std, device='cuda').view(1, -1, 1)
+ audio = audio_vae.decode(audio_latents * audio_std + audio_mean, return_dict=False)[0]
+ audio = audio.float().permute(1, 0, 2)[0].cpu()
+ rate = audio_vae.config.sampling_rate
+ del audio_vae, audio_mean, audio_std
+ gc.collect()
+ torch.cuda.empty_cache()
+ audio_seconds = time.perf_counter() - audio_start
+ if artifacts_dir:
+ import wave
+ phase('audio_save')
+ tick = time.perf_counter()
+ np.save(artifacts_dir / 'audio.npy', audio.numpy(), allow_pickle=False)
+ # Preserve exact float audio above; WAV is an independently playable copy.
+ pcm = (audio.clamp(-1, 1).t().contiguous().numpy() * 32767).round().astype('<i2')
+ with wave.open(str(artifacts_dir / 'audio.wav'), 'wb') as wav:
+ wav.setnchannels(audio.shape[0])
+ wav.setsampwidth(2)
+ wav.setframerate(rate)
+ wav.writeframes(pcm.tobytes())
+ artifact_seconds += time.perf_counter() - tick
+ destination = Path(out_path)
+ destination.parent.mkdir(parents=True, exist_ok=True)
+ temporary = destination.with_name(destination.stem + '.partial' + destination.suffix)
+ encode_started = time.perf_counter()
+ phase('mp4_save')
+ print(json.dumps({'event': 'decode_phase', 'phase': 'Saving MP4 and audio'}), flush=True)
+ encode_video(frames, fps=FPS, output_path=str(temporary), audio=audio, audio_sample_rate=rate)
+ temporary.replace(destination)
+ frame_count = len(frames)
+ finally:
+ if rgb_mapping is not None:
+ del frames
+ rgb_mapping._mmap.close()
+ return {'vae_load_seconds': load_seconds, 'video_decode_seconds': video_decode_seconds,
+ 'vae_linear_compute_cache': linear_compute_cache, 'vae_compute_cache_preparation': compute_cache_metrics,
+ 'resident_vae_cache_hit': vae_cache_hit, 'resident_audio_vae_cache_hit': audio_cache_hit,
+ 'vae_preload_seconds': preload_seconds,
+ 'audio_load_decode_seconds': audio_seconds, 'encode_seconds': time.perf_counter() - encode_started,
+ 'decoded_artifact_save_seconds': artifact_seconds,
+ 'streamed_video_output': stream_output, 'streamed_vae_weights': stream_weights,
+ 'vae_resident_blocks': resident_blocks,
+ 'video_postprocess_seconds': streamed_timings.get('video_postprocess_seconds'),
+ 'vae_pinned_weight_bytes': pinned_bytes,
+ 'decode_save_seconds': time.perf_counter() - started,
+ 'vae_offload': offload_stats, 'frames': frame_count, 'output': str(destination)}
diff --git a/freevideo_engine/decode_prefetch.py b/freevideo_engine/decode_prefetch.py
new file mode 100644
index 0000000..56dcc83
--- /dev/null
+++ b/freevideo_engine/decode_prefetch.py
@@ -0,0 +1,116 @@
+"""Overlap the final sampling steps with bounded decoder checkpoint reads.
+
+No Torch imports, CUDA allocations, model objects, pinned copies or encoder
+weights. Only reclaimable OS file cache retains the read data. Native decoding
+ still validates and loads the original checkpoint after sampling releases VRAM.
+The sampler starts this reader before its last two steps; the reader can still
+stop immediately when the live RAM/Commit allowance is insufficient.
+"""
+import json
+import os
+from pathlib import Path
+import threading
+import time
+
+from .encoder_prewarm import CHUNK, GiB, available_memory
+
+
+def decoder_ranges(base):
+ plans = []
+ for path in sorted((Path(base) / 'vae').glob('*.safetensors')):
+ size = path.stat().st_size
+ with path.open('rb') as file:
+ length = int.from_bytes(file.read(8), 'little')
+ if not 2 <= length <= min(16 * 2**20, size - 8):
+ raise ValueError('Invalid VAE safetensors header')
+ header = json.loads(file.read(length))
+ spans = []
+ for name, tensor in header.items():
+ if not name.startswith(('decoder.', 'post_quant_conv.')):
+ continue
+ lo, hi = tensor['data_offsets']
+ if type(lo) is not int or type(hi) is not int or not 0 <= lo <= hi <= size - length - 8:
+ raise ValueError('Invalid VAE tensor range')
+ spans.append((lo + length + 8, hi + length + 8))
+ merged = []
+ for lo, hi in sorted(spans):
+ if merged and lo <= merged[-1][1]:
+ merged[-1] = (merged[-1][0], max(hi, merged[-1][1]))
+ else:
+ merged.append((lo, hi))
+ if merged:
+ plans.append((path, tuple(merged), (size, path.stat().st_mtime_ns)))
+ return plans
+
+
+class DecoderReadAhead:
+ def __init__(self, base, *, ram_budget_bytes=None):
+ self.base = base
+ self.ram_budget_bytes = ram_budget_bytes
+ self.cancelled = threading.Event()
+ self.thread = None
+ self.result = dict(state='not-started', read_bytes=0, elapsed_seconds=0.,
+ gpu_allocation_bytes=0, private_buffer_bytes=CHUNK,
+ scope='Decoder-only checkpoint pages; reclaimable OS cache, no GPU residency')
+
+ def start(self):
+ if self.thread is not None:
+ return
+ if os.environ.get('FREEVIDEO_DECODE_READ_AHEAD', 'auto').lower() in ('off', '0', 'false'):
+ self.result.update(state='skipped', reason='Disabled by FREEVIDEO_DECODE_READ_AHEAD')
+ return
+ self.thread = threading.Thread(target=self._run, name='FreeVideo decoder read-ahead', daemon=True)
+ self.thread.start()
+
+ def _run(self):
+ started = time.monotonic()
+ try:
+ plans = decoder_ranges(self.base)
+ total = sum(hi - lo for _, spans, _ in plans for lo, hi in spans)
+ available, complete = available_memory()
+ allowance = max(0, available - 2 * GiB)
+ if self.ram_budget_bytes is not None:
+ from .ram import ProcessMemory
+ from .system import memory_sample
+ sample = ProcessMemory().sample(os.getpid())
+ used = memory_sample(sample)
+ complete = complete and type(used) is int and used >= 0
+ allowance = min(allowance, max(0, self.ram_budget_bytes - (used or 0) - 2 * GiB))
+ self.result.update(total_bytes=total, allowance_bytes=allowance)
+ if not complete or not 0 < total <= allowance:
+ self.result.update(state='skipped', reason='Live RAM cannot retain decoder pages alongside sampling')
+ return
+ self.result.update(state='reading', reason='Final denoising steps can overlap decoder disk reads')
+ scratch = bytearray(CHUNK)
+ for path, spans, stamp in plans:
+ with path.open('rb', buffering=0) as file:
+ for lo, hi in spans:
+ file.seek(lo)
+ remaining = hi - lo
+ while remaining:
+ available, complete = available_memory()
+ if self.cancelled.is_set() or not complete or available < 2 * GiB + CHUNK:
+ self.result.update(state='stopped', reason='Sampling finished, cancellation, or live RAM pressure')
+ return
+ count = file.readinto(memoryview(scratch)[:min(CHUNK, remaining)])
+ if not count:
+ raise ValueError('VAE checkpoint truncated during read-ahead')
+ remaining -= count
+ self.result['read_bytes'] += count
+ current = path.stat()
+ if (current.st_size, current.st_mtime_ns) != stamp:
+ raise ValueError('VAE checkpoint changed during read-ahead')
+ self.result.update(state='ready', reason='Decoder pages read while sampling; normal loader reuses surviving OS cache')
+ except Exception as error:
+ # Speculative I/O must not replace the normal loader's validation.
+ self.result.update(state='failed', reason=type(error).__name__ + ': ' + str(error))
+ finally:
+ self.result['elapsed_seconds'] = time.monotonic() - started
+
+ def close(self):
+ self.cancelled.set()
+ if self.thread is not None:
+ self.thread.join(timeout=1.)
+ result = dict(self.result)
+ result['reader_still_stopping'] = bool(self.thread and self.thread.is_alive())
+ return result
diff --git a/freevideo_engine/decode_resume.py b/freevideo_engine/decode_resume.py
new file mode 100644
index 0000000..52890f2
--- /dev/null
+++ b/freevideo_engine/decode_resume.py
@@ -0,0 +1,158 @@
+"""Validate retained sampling results before a decoder-only retry; Torch-free IO."""
+import copy
+import hashlib
+import json
+import math
+import os
+from pathlib import Path
+import shutil
+import tempfile
+
+
+# These change where immutable weights live, not the requested sampling math.
+ENGINE_PLACEMENT = frozenset(('resident_blocks', 'pin_host_gb', 'pin_host_weights',
+ 'preload_host', 'stream_weights', 'prefetch', 'offload_refiner', 'adaln_disk_cache'))
+DECODER_PLACEMENT = frozenset(('offload', 'prefetch', 'preload', 'pin_weights',
+ 'stream_output', 'stream_weights', 'resident_blocks'))
+SAMPLE_FIELDS = ('config', 'sample_seconds', 'step_seconds', 'finite_latents', 'geometry',
+ 'conditioning_info', 'conditioning_shape', 'offload', 'residual_offload_steps',
+ 'attention_backend_calls', 'head_execution', 'text_refinement', 'sampling_memory',
+ 'load_seconds', 'load_breakdown', 'load_peak_allocated_bytes', 'load_peak_reserved_bytes',
+ 'torch_peak_allocated_bytes', 'torch_peak_reserved_bytes', 'latent_save_seconds', 'sample_metrics_scope',
+ 'sampling_plan', 'sampling_passes', 'latent_upscale', 'refinement', 'fp8_kernel_calls')
+
+
+def completed_sampling(metrics):
+ """A retained completion receipt, including interrupted sampler cleanup."""
+ if not isinstance(metrics, dict) or metrics.get('success') is not False:
+ return False
+ phase = metrics.get('phase')
+ if phase not in ('decode', 'sample_finalize'):
+ return False
+ if phase == 'sample_finalize' and metrics.get('sampling_checkpoint_complete') is not True:
+ return False
+ config, steps = metrics.get('config'), metrics.get('step_seconds')
+ from .two_pass import steps as expected_steps
+ try:
+ total = expected_steps(metrics.get('sampling_plan'))
+ except (ValueError, AttributeError, TypeError):
+ return False
+ return (metrics.get('finite_latents') is True and isinstance(config, dict)
+ and type(config.get('steps')) is int and config['steps'] == 8
+ and isinstance(steps, list) and len(steps) == total
+ and all(type(value) in (int, float) and math.isfinite(value) and value > 0 for value in steps)
+ and isinstance(metrics.get('sampling_provenance'), dict) and bool(metrics['sampling_provenance']))
+
+
+def save_sampling(value, artifacts, torch):
+ """Expose a complete archive atomically; a failed save cannot replace it."""
+ artifacts = Path(artifacts)
+ artifacts.mkdir(parents=True, exist_ok=True)
+ with tempfile.NamedTemporaryFile(prefix='latents.', suffix='.tmp', dir=artifacts, delete=False) as stream:
+ temporary = Path(stream.name)
+ try:
+ torch.save(value, temporary)
+ temporary.replace(artifacts / 'latents.pt')
+ finally:
+ temporary.unlink(missing_ok=True)
+
+
+def _digest(path):
+ digest = hashlib.sha256()
+ with Path(path).open('rb') as stream:
+ for block in iter(lambda: stream.read(1024 * 1024), b''):
+ digest.update(block)
+ return digest.hexdigest()
+
+
+def sampling_provenance(request):
+ """Bind the result to the exact conditioning, model manifest and math."""
+ from .paths import base_path
+ options = request['engine_options']
+ if (type(request.get('seed')) is not int or not isinstance(request.get('geometry'), dict)
+ or not isinstance(options, dict) or not isinstance(request.get('decoder_options'), dict)):
+ raise ValueError('Invalid sampling request identity')
+ result = dict(version=1, seed=request['seed'], geometry=request['geometry'],
+ cache=str(Path(request['cache']).resolve()),
+ cache_manifest_sha256=_digest(Path(request['cache']) / 'manifest.json'),
+ conditioning=str(Path(request['conditioning']).resolve()),
+ conditioning_sha256=_digest(request['conditioning']),
+ base=str(Path(options.get('base') or base_path()).resolve()),
+ engine_math={key: value for key, value in options.items() if key not in ENGINE_PLACEMENT},
+ decoder_math={key: value for key, value in request['decoder_options'].items() if key not in DECODER_PLACEMENT})
+ if 'sampling_plan' in request:
+ from .two_pass import steps
+ steps(request['sampling_plan'])
+ result.update(version=2, sampling_plan=request['sampling_plan'])
+ if request['sampling_plan'].get('upscaler_sha256'):
+ result['upscaler_sha256'] = _digest(request['upscaler_checkpoint'])
+ if result['upscaler_sha256'] != request['sampling_plan']['upscaler_sha256']:
+ raise ValueError('Latent upscaler differs from the planned checkpoint')
+ return result
+
+
+def load_saved_sampling(request, torch):
+ """Load verified CPU tensors; caller controls CUDA admission and transfer."""
+ resume = request['resume_decode']
+ if not isinstance(resume, dict) or any(not isinstance(resume.get(key), str) or not resume[key]
+ for key in ('latents', 'metrics', 'request')):
+ raise ValueError('Invalid decoder retry source')
+ paths = {key: Path(resume[key]).resolve() for key in ('latents', 'metrics', 'request')}
+ if len({path.parent for path in paths.values()}) != 1 or len(set(paths.values())) != 3:
+ raise ValueError('Decoder retry artifacts must belong to one retained attempt')
+ prior = json.loads(paths['request'].read_text(encoding='utf-8'))
+ previous = json.loads(paths['metrics'].read_text(encoding='utf-8'))
+ if not isinstance(prior, dict) or not isinstance(previous, dict):
+ raise ValueError('Invalid retained sampling records')
+ if not completed_sampling(previous):
+ raise ValueError('Decoder retry requires all planned finite sampling steps')
+ provenance = sampling_provenance(request)
+ if provenance != sampling_provenance(prior) or provenance != previous.get('sampling_provenance'):
+ raise ValueError('Retained sampling does not match this request and its current inputs')
+ canvas = request['geometry']
+ if any(previous.get('geometry', {}).get(key) != canvas[key] for key in ('width', 'height', 'frames', 'fps')):
+ raise ValueError('Retained sampling geometry differs from the request')
+ value = torch.load(str(paths['latents']), map_location='cpu', weights_only=True)
+ if (not isinstance(value, dict) or type(value.get('seed')) is not int or value['seed'] != request['seed']
+ or value.get('geometry') != canvas or value.get('sampling_provenance') != provenance):
+ raise ValueError('Retained latent provenance differs from the sampling receipt')
+ width, height, frames, fps = (canvas[key] for key in ('width', 'height', 'frames', 'fps'))
+ if (any(type(n) is not int or n <= 0 for n in (width, height, frames, fps))
+ or width % 32 or height % 32 or frames % 17 != 5 or fps != 24):
+ raise ValueError('Invalid retained H3 sampling geometry')
+ # Pinned H3 contract: video C=24, spatial stride=16; stereo audio C=32
+ # with 40 latent frames/s. These are also the official sampler's layouts.
+ shapes = {'video': (1, 24, (frames - 5) // 17 * 5 + 2, height // 16, width // 16),
+ 'audio': (2, 32, round(frames / fps * 40))}
+ for key, shape in shapes.items():
+ tensor = value.get(key)
+ if (not isinstance(tensor, torch.Tensor) or tensor.device.type != 'cpu'
+ or tuple(tensor.shape) != shape or not tensor.is_floating_point()
+ or not bool(torch.isfinite(tensor).all())):
+ raise ValueError('Retained %s latents are malformed or non-finite' % key)
+ source = previous.get('sampling_source_metrics')
+ if not isinstance(source, dict):
+ source = {key: previous[key] for key in SAMPLE_FIELDS if key in previous}
+ metrics = {key: copy.deepcopy(previous[key]) for key in SAMPLE_FIELDS if key in previous
+ and not key.startswith(('load_', 'torch_peak_')) and key != 'latent_save_seconds'}
+ metrics.update(sampling_reused=True, sampling_source_attempt=previous.get('sampling_source_attempt',
+ prior.get('resource_attempt')), sampling_provenance=provenance,
+ sampling_source_metrics=copy.deepcopy(source), load_seconds=0.,
+ sampling_metrics_scope='Reused completed sampling; sampling times and peaks belong to the source attempt.',
+ work_seconds_scope='Current decoder retry, including retained latent validation and transfer only.')
+ return value['video'], value['audio'], metrics, Path(provenance['base'])
+
+
+def retain_latents(source, artifacts):
+ """Keep the source attempt intact and satisfy final artifact validation."""
+ source, artifacts = Path(source), Path(artifacts)
+ artifacts.mkdir(parents=True, exist_ok=True)
+ destination = artifacts / 'latents.pt'
+ try:
+ os.link(source, destination)
+ except FileExistsError:
+ if not os.path.samefile(source, destination):
+ raise
+ except OSError:
+ with source.open('rb') as incoming, destination.open('xb') as outgoing:
+ shutil.copyfileobj(incoming, outgoing, length=1024 * 1024)
diff --git a/freevideo_engine/decode_stream.py b/freevideo_engine/decode_stream.py
new file mode 100644
index 0000000..34fef33
--- /dev/null
+++ b/freevideo_engine/decode_stream.py
@@ -0,0 +1,137 @@
+# Copyright 2026 The MiniMax and HuggingFace Teams. All rights reserved.
+# Copyright 2026 FreeVideo contributors.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy at http://www.apache.org/licenses/LICENSE-2.0
+"""Stream the pinned H3 VAE's original temporal chunks and overlap arithmetic.
+
+Adapted from AutoencoderKLMiniMaxH3._decode at OpenVDN
+30b6b380c2482f3519469350810c2955d8847fd9. Spatial decoding and blending still
+call the original VAE. Only storage lifetime changes: completed frames leave
+the GPU before the next clip, without concatenating a whole floating video.
+"""
+
+
+def pad_temporal(z, count):
+ import torch
+ return torch.cat([z, z[:, :, -1:].repeat(1, 1, count, 1, 1)], dim=2) if count else z
+
+
+def decoded_frames(vae, count):
+ tokens = vae.tokens_chunk_size
+ drop = vae.config.token_drop
+ ratio = vae.temporal_compression_ratio
+ padding = (-(count + drop)) % tokens
+ clips = (count + drop + padding) // tokens - int(drop > 0)
+ tail = vae.config.clip_length % ratio
+ trim = sum(tail if tail and (count + k) % tokens == 0 else ratio for k in range(padding))
+ return clips * (tokens * ratio - vae.frame_pre_padding) + (vae.frame_overlap if drop > 0 else 0) - trim
+
+
+def iter_decoded_chunks(vae, z):
+ tokens = vae.tokens_chunk_size
+ drop = vae.config.token_drop
+ ratio = vae.temporal_compression_ratio
+ count = z.shape[2]
+ padding = (-(count + drop)) % tokens
+ clips = (count + drop + padding) // tokens - int(drop > 0)
+ if clips < 1:
+ raise ValueError('No temporal VAE clip for the supplied latents')
+ tail = vae.config.clip_length % ratio
+ trim = sum(tail if tail and (count + k) % tokens == 0 else ratio for k in range(padding))
+ z = pad_temporal(z, padding)
+ overlap = None
+ # Hold only the final candidate chunk until trailing padding is known to
+ # be removed. This also handles a trim spanning more than one short chunk.
+ pending = []
+ pending_frames = 0
+ for i in range(clips):
+ start = i * tokens
+ clip = vae._decode_clip(z[:, :, start:start + tokens + vae.token_overlap])
+ for j in range(int(drop > 0) + 1):
+ chunk = clip[:, :, j*tokens*ratio:(j+1)*tokens*ratio]
+ chunk = chunk[:, :, vae.frame_pre_padding:]
+ if j == 0:
+ if overlap is not None:
+ chunk = vae._blend(overlap, chunk, vae.frame_overlap, dim=-3)
+ pending.append(chunk)
+ pending_frames += chunk.shape[2]
+ while pending and pending_frames - pending[0].shape[2] >= trim:
+ item = pending.pop(0)
+ pending_frames -= item.shape[2]
+ yield item
+ del item
+ else:
+ overlap = chunk
+ del clip, chunk
+ if overlap is not None:
+ pending.append(overlap)
+ pending_frames += overlap.shape[2]
+ keep = pending_frames - trim
+ if keep < 0:
+ raise ValueError('VAE temporal padding exceeds the retained output')
+ for chunk in pending:
+ count = min(keep, chunk.shape[2])
+ if count:
+ yield chunk[:, :, :count]
+ keep -= count
+
+
+def render_rgb(vae, latents, pixel_mean, pixel_std, path, progress=None):
+ """Write exact uint8 output a temporal clip at a time; retain partial data."""
+ import time
+ from pathlib import Path
+ import numpy as np
+ import torch
+ path = Path(path)
+ path.parent.mkdir(parents=True, exist_ok=True)
+ temporary = path.with_name(path.stem + '.partial.npy')
+ count = decoded_frames(vae, latents.shape[2])
+ if count <= 0 or latents.shape[0] != 1:
+ raise ValueError('Streamed video export requires one nonempty video')
+ shape = (count, latents.shape[-2] * vae.spatial_compression_ratio,
+ latents.shape[-1] * vae.spatial_compression_ratio, vae.config.out_channels)
+ mapping = np.lib.format.open_memmap(temporary, mode='w+', dtype=np.uint8, shape=shape)
+ mean = torch.tensor(pixel_mean).view(1, -1, 1, 1, 1)
+ std = torch.tensor(pixel_std).view(1, -1, 1, 1, 1)
+ timings = dict(video_decode_seconds=0., video_postprocess_seconds=0., decoded_artifact_save_seconds=0.)
+ offset = 0
+ iterator = iter_decoded_chunks(vae, latents)
+ started = time.perf_counter()
+ try:
+ while True:
+ tick = time.perf_counter()
+ try:
+ chunk = next(iterator)
+ except StopIteration:
+ break
+ video = chunk.cpu()
+ del chunk
+ timings['video_decode_seconds'] += time.perf_counter() - tick
+ tick = time.perf_counter()
+ for start in range(0, video.shape[2], 8):
+ part = (video[:, :, start:start + 8].float() * std + mean).clamp_(0, 1)
+ rgb = (part[0].permute(1, 2, 3, 0) * 255).round().to(torch.uint8)
+ mapping[offset:offset + len(rgb)] = rgb.numpy()
+ offset += len(rgb)
+ del part, rgb
+ del video
+ timings['video_postprocess_seconds'] += time.perf_counter() - tick
+ tick = time.perf_counter()
+ mapping.flush()
+ timings['decoded_artifact_save_seconds'] += time.perf_counter() - tick
+ if progress is not None:
+ # The existing chunk.cpu() already waits for these frames.
+ # Reporting adds no CUDA fence or extra tensor allocation.
+ progress(offset, count, time.perf_counter() - started)
+ if offset != count:
+ raise ValueError('Streamed VAE output length differs from its original temporal plan')
+ finally:
+ iterator.close()
+ mapping.flush()
+ mapping._mmap.close()
+ temporary.replace(path)
+ # Keep one mapped uint8 plane for the original encoder; no full float video
+ # or second RGB copy is materialized. The caller owns this mapping's life.
+ mapping = np.load(path, mmap_mode='r+', allow_pickle=False)
+ return torch.from_numpy(mapping), mapping, timings
diff --git a/freevideo_engine/default_prompt.py b/freevideo_engine/default_prompt.py
new file mode 100644
index 0000000..66997b0
--- /dev/null
+++ b/freevideo_engine/default_prompt.py
@@ -0,0 +1,37 @@
+"""FreeVideo's ten-second T2VA starter, shared with the bundled workflows.
+
+Written against MiniMax H3's official h3-prompt-writing/SKILL.md and
+references/base-en.txt. This is creative copy, not a benchmark input.
+"""
+
+# Used only as a new-node default; saved and connected prompts stay authoritative.
+DEFAULT_PROMPT = (
+ 'integrated_multimodal_description: [Shot 1] A premium cinematic, photorealistic '
+ 'wide shot opens above an immense black-glass desert at blue hour. A lone explorer '
+ 'in a deep cobalt coat stands on a mirror-bright salt basin while a colossal ringed '
+ 'observatory hangs in the clouds ahead, its bronze arcs catching the first edge of '
+ 'sunrise. Fine dust moves in deliberate ribbons across the reflective ground. The '
+ 'camera glides forward at low altitude, then rises toward the observatory as distant '
+ 'thunder rolls through the valley; every surface remains sharp, physically coherent, '
+ 'and richly detailed. [Shot 2] At 00:03.000, the observatory awakens in one fluid '
+ 'transformation. Its concentric rings rotate into alignment and release a luminous '
+ 'river of gold and turquoise particles. The camera accelerates through the moving '
+ 'rings in a single uninterrupted arc, passing close to engraved bronze, translucent '
+ 'glass, and drifting sparks. The light river climbs into the storm and becomes a '
+ 'vast aurora that reflects across the salt basin; the explorer remains the same '
+ 'scale and position as the camera sweeps around them. [Shot 3] At 00:07.000, the '
+ 'camera completes the orbit and settles into a majestic centered composition: the '
+ 'observatory, aurora, and mirrored desert form one continuous horizon beneath a '
+ 'clearing sky. The aurora folds into a simple luminous circle above the explorer, '
+ 'then the image resolves smoothly into a restrained dark title card. During the '
+ 'final second, clean luminous lettering reads exactly “FreeVideo”, with no other '
+ 'words or logos. Hold the final frame long enough to read; '
+ 'no subtitles, no glitches, no extra text.\n\n'
+ 'overall_soundscape: Begin with a high open wind, fine salt skittering over glass, '
+ 'and distant thunder. As the rings align, add precise metallic resonance and a rising '
+ 'electrical shimmer; the particle river becomes a broad airy rush that travels around '
+ 'the camera. Let the final aurora fade into a clean, quiet tail beneath the title.\n\n'
+ 'non_diegetic_music: A restrained cinematic pulse begins with low strings and glassy '
+ 'harmonics, expands into full strings and deep brass as the observatory opens, then '
+ 'resolves into one bright, confident chord under the final title.'
+)
diff --git a/freevideo_engine/dependencies.json b/freevideo_engine/dependencies.json
new file mode 100644
index 0000000..0dec872
--- /dev/null
+++ b/freevideo_engine/dependencies.json
@@ -0,0 +1,81 @@
+{
+ "vdn": {
+ "url": "https://github.com/OpenVDN/vdn-minimax-h3.git",
+ "commit": "30b6b380c2482f3519469350810c2955d8847fd9",
+ "diffusers_subdirectory": "diffusers",
+ "license": "Apache-2.0"
+ },
+ "diffusers": {
+ "url": "https://github.com/huggingface/diffusers.git",
+ "base": "3a2f35d4efa4c059c8bfb3bc0d6c906264895c81",
+ "patched_tree": "37068ab7331d8b28f4cf718dba7015c742a306d2",
+ "patches_from": "pinned VDN diffusers_patches/*.patch"
+ },
+ "encoder": {
+ "comfy_url": "https://github.com/comfy-org/ComfyUI.git",
+ "comfy_commit": "421a1c245c682c04d4325ba365f40c834c66f5b0",
+ "implementation": "ComfyUI native H3 encoder only; no UI server or T8 custom node"
+ },
+ "sageattention": {
+ "url": "https://github.com/thu-ml/SageAttention.git",
+ "commit": "d1a57a546c3d395b1ffcbeecc66d81db76f3b4b5",
+ "version": "2.2.0",
+ "tested_sm120_wheel_sha256": "a89dc581440ec804408da6f6cdd84172950f79523e7dc2656fdfdc9b72a26170",
+ "tested_sm120_wheel_scope": "Historical CUDA 12.9 wheel; setup builds or verifies a local wheel for the selected Torch/CUDA/GPU."
+ },
+ "models": {
+ "vdn_repo": "OpenVDN/vdn-minimax-h3",
+ "vdn_revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "vdn_modelscope": {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "3674e3a5183da3782bdd7e57b83adb8b99a759ba",
+ "hf_revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "probe_file": "h3-base/transformer/diffusion_pytorch_model-00001-of-00014.safetensors"
+ },
+ "encoder_repo": "t8star/Vdn-Minimax-H3-Comfy",
+ "encoder_revision": "d6aa5dfc669a749dc9364a36f3b6d4d1545a53e8",
+ "encoder_file": "text_encoders/qwen3vl_32b_minimax_h3_nvfp4_awq.safetensors",
+ "encoder_sha256": "35a88d51044231fe332301d7a62aa81e3f2cba62febeb446e2c1e3e0ef76f2c6",
+ "edge_modelscope": {
+ "repo": "OpenVDN/vdn-minimax-h3-edge",
+ "revision": "7832055a557c968ba86fa000374337560193adcf",
+ "hf_revision": "5e6ecc39f472f98ca7493000df49a4f712efb61a",
+ "hf_snapshot_revision": "d95329e1441404e95082053b148ba5f8d3dd754f",
+ "probe_file": "cache/blocks/00.safetensors"
+ },
+ "edge_modelscope_variants": {
+ "rowwise": {
+ "repo": "OpenVDN/vdn-minimax-h3-edge",
+ "revision": "5be1ff90c02c9b1c1ce7300d6a803931732e6ab0",
+ "hf_revision": "ae041e5aec51f8516a3416bd86d579cb29c4c796",
+ "probe_file": "rowwise/cache/blocks/00.safetensors"
+ }
+ },
+ "latent_upscaler": {
+ "repo": "LBH-123-AI/Minimax_h3_latent_Upscaler",
+ "revision": "3f941d5d182014dd5c0a5e16330420ee2d4aa0c6",
+ "file": "minimax_h3_latent_upscaler_3d_conv_v1/minimax_h3_latent_upscaler_3d_conv_v1_fp16.safetensors",
+ "bytes": 690592672,
+ "sha256": "043e5a48e161610ef6c3ea974645220354d06fa618abca15f76d084812eb55c2",
+ "role": "latent_upscaler",
+ "license": "MIT"
+ }
+ },
+ "tested": {
+ "python": "3.12.3",
+ "torch": "2.13.0+cu130",
+ "triton": "3.7.1",
+ "sageattention": "2.2.0",
+ "transformers": "5.15.0",
+ "environment_layout": "unified",
+ "scope": "SM120 kernel checks and full requests; see benchmarks/2026-09-10-unified-environment.json"
+ },
+ "model_license": "MiniMax H3 Community License; weights are downloaded separately",
+ "tested_legacy": {
+ "python": "3.12.3",
+ "torch": "2.13.0+cu129",
+ "triton": "3.7.1",
+ "sageattention": "2.2.0",
+ "flash-attn-4": "4.0.0b26"
+ }
+}
diff --git a/freevideo_engine/desktop.py b/freevideo_engine/desktop.py
new file mode 100644
index 0000000..1d92e7c
--- /dev/null
+++ b/freevideo_engine/desktop.py
@@ -0,0 +1,752 @@
+"""Native desktop views. Rendering is separate from installer/test orchestration."""
+import json
+import locale
+import os
+from pathlib import Path
+import queue
+import sys
+import time
+import traceback
+import uuid
+import webbrowser
+
+from . import __version__, branding
+from .desktop_runtime import Runner, launcher_root, materialize_source, preflight_json
+from .monitoring import save
+from .system import install_root, windows
+from .terminal_ui import duration, clipped
+from .widgets import Card, Progress, Stat
+
+
+class Desktop:
+ def __init__(self, window, source=None):
+ import tkinter as tk
+ from tkinter import ttk
+ self.tk, self.ttk, self.window = tk, ttk, window
+ self.source = Path(source or materialize_source())
+ self.events = queue.Queue()
+ self.runner = Runner(self.source, self.events)
+ self.plan = None
+ self.last_log = None
+ self.closing = False
+ self.running = False
+ self.progress_rows = {}
+ self.zh = (locale.getlocale()[0] or '').lower().startswith('zh')
+ try:
+ settings = json.loads((launcher_root() / 'settings.json').read_text(encoding='utf-8'))
+ if not isinstance(settings, dict):
+ settings = {}
+ except (OSError, ValueError):
+ settings = {}
+ if settings.get('language') in ('en', 'zh'):
+ self.zh = settings['language'] == 'zh'
+ saved_root = settings.get('root') if isinstance(settings.get('root'), str) else None
+ self.install_path = tk.StringVar(value=str(os.environ.get('FREEVIDEO_HOME') or saved_root or install_root()))
+ self.reuse_path = tk.StringVar(value=settings.get('reuse_models') if isinstance(settings.get('reuse_models'), str) else '')
+ self.copy_existing = tk.BooleanVar(value=settings.get('copy_existing_models') is True)
+ self.allow_restart = tk.BooleanVar(value=False)
+ self.setup_retry = False
+ self.accept = tk.BooleanVar(value=False)
+ self.resolution = tk.StringVar(value='1344 × 768')
+ self.duration = tk.StringVar(value='10.125')
+ self.status = tk.StringVar(value=self.t('Ready', '准备就绪'))
+ self.note = tk.StringVar()
+ self.phase_status = tk.StringVar(value=self.t('Installation progress', '安装进度'))
+ self.resource = tk.StringVar(value=self.t('Detect your hardware to see the installation plan.', '先检测配置,查看安装计划。'))
+ self.buttons = []
+ self.translated = []
+ window.title('FreeVideo')
+ from .windows_ux import window_size
+ width, height = window_size(window)
+ window.geometry('%dx%d' % (width, height))
+ window.minsize(min(760, width), min(560, height))
+ style = branding.theme(window)
+ body = ttk.Frame(window, padding=28)
+ body.pack(fill='both', expand=True)
+ header = ttk.Frame(body)
+ header.pack(fill='x', pady=(0, 20))
+ self.logo = branding.wordmark(header)
+ self.logo.pack(side='left')
+ ttk.Label(header, text=' ENGINE / ' + __version__, style='Muted.TLabel').pack(side='left', pady=(12, 0))
+ self.button(header, '中文', 'EN', self.language, managed=False).configure(style='Quiet.TButton')
+ self.translated[-1][0].pack(side='right')
+ self.tabs = ttk.Notebook(body)
+ self.tabs.pack(fill='both', expand=True)
+ self.pages = []
+ for en, zh in [('Setup', '安装'), ('Generate', '生成'), ('Test & tune', '测试与优化'), ('Reports', '报告')]:
+ page = ttk.Frame(self.tabs, padding=22)
+ self.tabs.add(page, text=self.t(en, zh))
+ self.pages.append((page, en, zh))
+ self.setup_page(self.pages[0][0])
+ self.generate_page(self.pages[1][0])
+ self.test_page(self.pages[2][0])
+ self.reports_page(self.pages[3][0])
+ footer = ttk.Frame(body, padding=(0, 20, 0, 0))
+ # Reserve task controls before the expanding notebook, so a second
+ # parallel task cannot be clipped below the window's bottom edge.
+ footer.pack(side='bottom', fill='x', before=self.tabs)
+ status_line = ttk.Frame(footer)
+ status_line.pack(fill='x')
+ ttk.Label(status_line, textvariable=self.status, wraplength=650).pack(side='left', fill='x', expand=True)
+ self.cancel_button = self.button(status_line, 'Stop', '停止', self.cancel, managed=False)
+ self.cancel_button.pack(side='right')
+ self.cancel_button.state(['disabled'])
+ ttk.Label(footer, textvariable=self.phase_status, style='Muted.TLabel').pack(anchor='w', pady=(8, 0))
+ self.progress = Progress(footer, background=branding.BACKGROUND, height=branding.space(2))
+ self.progress.configure(mode='indeterminate')
+ self.progress.pack(fill='x', pady=(branding.space(2), branding.space(2)))
+ self.task_area = ttk.Frame(footer)
+ self.task_area.pack(fill='x')
+ self.button(footer, 'Show / hide details', '展开/收起详情', self.toggle_log, managed=False).pack(anchor='w')
+ # Details must not take height away from the setup viewport. On small
+ # Windows desktops, two task cards plus inline logs could unmap the
+ # canvas completely; scrolling an unmapped canvas cannot reveal actions.
+ self.log_window = tk.Toplevel(window)
+ self.log_window.withdraw()
+ self.log_window.title(self.t('FreeVideo · Details', 'FreeVideo · 详情'))
+ self.log_window.geometry('%dx%d' % (min(860, width), min(440, height)))
+ self.log_window.transient(window)
+ self.log_window.protocol('WM_DELETE_WINDOW', self.log_window.withdraw)
+ self.log_frame = ttk.Frame(self.log_window, padding=12)
+ self.log_frame.pack(fill='both', expand=True)
+ self.log = tk.Text(self.log_frame, height=6, font=(branding.MONO, branding.MICRO),
+ bg=branding.SURFACE, fg=branding.TEXT, relief='flat', wrap='word',
+ padx=branding.space(3), pady=branding.space(2), state='disabled')
+ scrollbar = ttk.Scrollbar(self.log_frame, command=self.log.yview,
+ style='Wizard.Vertical.TScrollbar')
+ self.log.configure(yscrollcommand=scrollbar.set)
+ self.log.pack(side='left', fill='both', expand=True)
+ scrollbar.pack(side='right', fill='y')
+ self.compact_layout = None
+ def resize_layout(event):
+ if event.widget != window:
+ return
+ compact = event.height < 760
+ if compact == self.compact_layout:
+ return
+ self.compact_layout = compact
+ # Reduce whitespace, not text size or the space needed by controls.
+ # Real Windows/Linux fonts can otherwise leave a viewport shorter
+ # than the installation button, even with details in another window.
+ body.configure(padding=12 if compact else 28)
+ header.pack_configure(pady=(0, 8 if compact else 20))
+ footer.configure(padding=(0, 8 if compact else 20, 0, 0))
+ for page, _, _ in self.pages:
+ page.configure(padding=12 if compact else 22)
+ window.bind('<Configure>', resize_layout, add='+')
+ window.protocol('WM_DELETE_WINDOW', self.close)
+ window.bind('<Destroy>', self.cancel_poll, add='+')
+ window.report_callback_exception = self.callback_error
+ self.install_path.trace_add('write', self.root_changed)
+ self.reuse_path.trace_add('write', self.root_changed)
+ self.copy_existing.trace_add('write', self.root_changed)
+ self.allow_restart.trace_add('write', self.root_changed)
+ self.update_buttons()
+ self.poll_timer = window.after(100, self.poll)
+
+ def t(self, english, chinese):
+ return chinese if self.zh else english
+
+ def label(self, parent, english, chinese, **kwargs):
+ widget = self.ttk.Label(parent, text=self.t(english, chinese), **kwargs)
+ self.translated.append((widget, english, chinese))
+ return widget
+
+ def button(self, parent, english, chinese, command, primary=False, managed=True):
+ widget = self.ttk.Button(parent, text=self.t(english, chinese), command=command,
+ style='Primary.TButton' if primary else 'TButton')
+ self.translated.append((widget, english, chinese))
+ if managed:
+ self.buttons.append(widget)
+ return widget
+
+ def scrolled(self, page, index):
+ """Every tab scrolls. A second live task grows the footer, which used to
+ squeeze the notebook and clip whichever tab was open; only Setup could
+ scroll, so the Generate tab lost its own action buttons.
+
+ The scrollbar shows itself only while the content does not fit.
+ """
+ canvas = self.tk.Canvas(page, highlightthickness=0, background=branding.BACKGROUND)
+ scroll = self.ttk.Scrollbar(page, orient='vertical', command=canvas.yview,
+ style='Wizard.Vertical.TScrollbar')
+ canvas.pack(side='left', fill='both', expand=True)
+ def reach(first, last):
+ scroll.set(first, last)
+ if float(first) == 0 and float(last) == 1:
+ scroll.pack_forget()
+ elif not scroll.winfo_manager():
+ scroll.pack(side='right', fill='y', before=canvas)
+ canvas.configure(yscrollcommand=reach)
+ content = self.ttk.Frame(canvas, padding=(0, 0, branding.space(3), branding.space(3)))
+ item = canvas.create_window(0, 0, anchor='nw', window=content)
+ content.bind('<Configure>', lambda _: canvas.configure(scrollregion=canvas.bbox('all')))
+ def resize(event):
+ canvas.itemconfigure(item, width=event.width)
+ if index == 0 and hasattr(self, 'plan_text'):
+ self.plan_text.configure(wraplength=max(280, event.width-branding.space(6)))
+ canvas.bind('<Configure>', resize)
+ def wheel(event):
+ if (self.tabs.select() == str(self.pages[index][0])
+ and canvas.winfo_rooty() <= event.y_root < canvas.winfo_rooty()+canvas.winfo_height()):
+ canvas.yview_scroll(-1 if event.delta > 0 else 1, 'units')
+ for name in ('<MouseWheel>', '<Button-4>', '<Button-5>'):
+ self.window.bind(name, wheel, add='+')
+ return canvas, content
+
+ def setup_page(self, page):
+ # Keep consent/actions reachable on smaller Windows laptop screens.
+ self.setup_canvas, page = self.scrolled(page, 0)
+ self.label(page, 'Installation folder', '安装目录').pack(anchor='w')
+ row = self.ttk.Frame(page)
+ row.pack(fill='x', pady=(8, 16))
+ self.root_entry = self.ttk.Entry(row, textvariable=self.install_path)
+ self.root_entry.pack(side='left', fill='x', expand=True)
+ self.button(row, 'Browse', '浏览', self.browse).pack(side='right', padx=(8, 0))
+ self.label(page, 'Existing model folder (optional)', '已有模型文件夹(可选)').pack(anchor='w')
+ row = self.ttk.Frame(page)
+ row.pack(fill='x', pady=(8, 6))
+ self.reuse_entry = self.ttk.Entry(row, textvariable=self.reuse_path)
+ self.reuse_entry.pack(side='left', fill='x', expand=True)
+ self.button(row, 'Browse', '浏览', self.browse_models).pack(side='right', padx=(8, 0))
+ self.button(row, 'Clear', '清除', lambda: self.reuse_path.set('')).pack(side='right', padx=(8, 0))
+ self.label(page, 'Subfolders are scanned. Missing files download during setup.',
+ '自动扫描子目录,缺少的文件在安装时下载。',
+ style='Meta.TLabel', wraplength=700).pack(anchor='w')
+ self.copy_check = self.ttk.Checkbutton(page, variable=self.copy_existing)
+ self.copy_check.configure(text=self.t('Create independent copies', '创建独立副本'))
+ self.copy_check.pack(anchor='w', pady=(8, 14))
+ self.restart_check = self.ttk.Checkbutton(page, variable=self.allow_restart)
+ self.restart_check.configure(text=self.t('Allow restarting incomplete downloads',
+ '允许重新下载无法续传的文件'))
+ self.restart_check.pack(anchor='w', pady=(0, 14))
+ self.button(page, 'Detect configuration', '检测配置', self.inspect, primary=True).pack(anchor='w')
+ self.plan_text = self.ttk.Label(page, textvariable=self.resource, wraplength=790,
+ justify='left', padding=(0, branding.space(4)))
+ self.plan_text.pack(fill='x')
+ self.plan_grid = self.ttk.Frame(page)
+ self.plan_grid.columnconfigure((0, 1, 2), weight=1, uniform='plan')
+ self.plan_tiles = []
+ self.plan_note = self.ttk.Label(page, textvariable=self.note, style='Muted.TLabel',
+ wraplength=790, justify='left')
+ self.consent = self.ttk.Checkbutton(page, variable=self.accept, command=self.update_buttons)
+ self.consent.configure(text=self.t('I accept the displayed installation plan and model licenses.', '我接受所展示的安装计划和模型许可证。'))
+ self.consent.pack(anchor='w', pady=(8, 12))
+ row = self.ttk.Frame(page)
+ row.pack(fill='x')
+ self.install_button = self.button(row, 'Install / repair', '安装/修复', self.install, primary=True)
+ self.install_button.pack(side='left')
+ self.button(row, 'Model licenses', '查看模型许可证', self.licenses, managed=False).pack(side='left', padx=10)
+
+ def generate_page(self, page):
+ _, page = self.scrolled(page, 1)
+ self.label(page, 'Describe your video', '描述你想生成的视频').pack(anchor='w', pady=(0, 10))
+ prompt_card = Card(page, padding=branding.space(3), radius=branding.RADIUS_MD)
+ prompt_card.pack(fill='x')
+ self.prompt = self.tk.Text(prompt_card.body, height=6, font=(branding.FAMILY, branding.STRONG),
+ relief='flat', padx=branding.space(3), pady=branding.space(3),
+ highlightthickness=0, borderwidth=0, bg=branding.BACKGROUND,
+ fg=branding.TEXT, insertbackground=branding.TEXT, wrap='word')
+ self.prompt.pack(fill='x')
+ row = self.ttk.Frame(page)
+ row.pack(fill='x', pady=16)
+ self.label(row, 'Resolution', '分辨率').pack(side='left')
+ self.ttk.Combobox(row, textvariable=self.resolution, state='readonly', width=18,
+ values=('1344 × 768', '1024 × 576', '768 × 448')).pack(side='left', padx=(10, 24))
+ self.label(row, 'Seconds', '时长(秒)').pack(side='left')
+ self.ttk.Combobox(row, textvariable=self.duration, state='readonly', width=10,
+ values=('10.125', '5')).pack(side='left', padx=10)
+ self.label(page, 'Strategy follows your available VRAM and RAM.', '策略随当前可用显存与内存自动调整。',
+ style='Meta.TLabel').pack(anchor='w')
+ self.prediction_status = self.tk.StringVar(value=self.t('Forecasts improve as you generate.',
+ '生成越多,耗时与内存预测越准。'))
+ self.ttk.Label(page, textvariable=self.prediction_status, style='Muted.TLabel', wraplength=780).pack(anchor='w', pady=(10, 0))
+ actions = self.ttk.Frame(page)
+ actions.pack(anchor='w', pady=20)
+ self.button(actions, 'Generate video', '生成视频', self.generate, primary=True).pack(side='left')
+ self.button(actions, 'Estimate time / memory', '预估耗时/内存', self.predict).pack(side='left', padx=12)
+
+ def test_page(self, page):
+ _, page = self.scrolled(page, 2)
+ self.label(page, 'Find out how your machine performs.', '看看这台机器的实际表现。',
+ style='Section.TLabel').pack(anchor='w')
+ self.label(page, 'Five requests: first, repeat, new prompt, resolution, duration.',
+ '连续五轮:首次、重复、更换提示词、分辨率、时长。',
+ style='Muted.TLabel', wraplength=730).pack(anchor='w', pady=branding.space(4))
+ row = self.ttk.Frame(page)
+ row.pack(fill='x')
+ self.button(row, 'Standard test', '标准测试', lambda: self.test('standard'), primary=True).pack(side='left')
+ self.button(row, 'Quick test', '快速测试', lambda: self.test('quick')).pack(side='left', padx=10)
+ self.label(page, 'Tune once, then test again. Only validated settings are kept.',
+ '优化一次,再测一次;仅保存通过验证的设置。',
+ style='Muted.TLabel', wraplength=730).pack(anchor='w', pady=(branding.space(7), branding.space(4)))
+ self.button(page, 'Optimize once', '优化一次', lambda: self.ready_action('optimize', ['optimize', '--plain'])).pack(anchor='w')
+
+ def reports_page(self, page):
+ _, page = self.scrolled(page, 3)
+ self.label(page, 'Your results stay on this computer.', '结果始终保留在本机。',
+ style='Section.TLabel').pack(anchor='w')
+ self.label(page, 'Open a report, or export diagnostics when something fails.',
+ '打开报告;遇到问题时导出诊断包。',
+ style='Muted.TLabel', wraplength=730).pack(anchor='w', pady=branding.space(4))
+ for en, zh, command in [('Open latest video report', '打开最近视频报告', self.open_report),
+ ('Play latest generated video', '播放最近生成的视频', self.open_video),
+ ('Open output folder', '打开产物目录', self.open_outputs),
+ ('Export diagnostics', '导出诊断包', self.diagnose),
+ ('Open launcher logs', '打开启动器日志', self.open_logs)]:
+ self.button(page, en, zh, command, managed=en == 'Export diagnostics').pack(anchor='w', pady=6)
+
+ def root(self):
+ if not self.install_path.get().strip():
+ raise ValueError(self.t('Choose an installation folder.', '请选择安装目录。'))
+ return Path(self.install_path.get()).expanduser().resolve()
+
+ def root_changed(self, *_):
+ self.plan = None
+ self.setup_retry = False
+ self.accept.set(False)
+ self.clear_plan_view()
+ self.resource.set(self.t('Detect configuration for this installation folder.', '请检测此目录对应的安装配置。'))
+ self.update_buttons()
+
+ def clear_plan_view(self):
+ """The controller tests build a view-less Desktop, so skip if unbuilt."""
+ if not hasattr(self, 'plan_grid'):
+ return
+ self.note.set('')
+ for widget in (self.plan_grid, self.plan_note):
+ widget.pack_forget()
+ self.plan_text.pack_configure(pady=(0, branding.space(4)))
+
+ def update_buttons(self):
+ self.install_button.configure(text=self.t('Continue installation / retry', '继续安装/重试') if self.setup_retry
+ else self.t('Install / repair', '安装/修复'))
+ for button in self.buttons:
+ button.state(['disabled'] if self.running else ['!disabled'])
+ self.root_entry.state(['disabled'] if self.running else ['!disabled'])
+ self.reuse_entry.state(['disabled'] if self.running else ['!disabled'])
+ self.copy_check.state(['disabled'] if self.running else ['!disabled'])
+ self.restart_check.state(['disabled'] if self.running else ['!disabled'])
+ self.consent.state(['disabled'] if self.running else ['!disabled'])
+ if self.running or not self.plan or self.plan.get('errors') or not self.accept.get():
+ self.install_button.state(['disabled'])
+
+ def language(self):
+ self.zh = not self.zh
+ self.log_window.title(self.t('FreeVideo · Details', 'FreeVideo · 详情'))
+ for widget, en, zh in self.translated:
+ widget.configure(text=self.t(en, zh))
+ for page, en, zh in self.pages:
+ self.tabs.tab(page, text=self.t(en, zh))
+ self.consent.configure(text=self.t('I accept the displayed installation plan and model licenses.', '我接受所展示的安装计划和模型许可证。'))
+ self.copy_check.configure(text=self.t('Create independent copies', '创建独立副本'))
+ self.restart_check.configure(text=self.t('Allow restarting incomplete downloads',
+ '允许重新下载无法续传的文件'))
+ if self.plan:
+ self.show_plan()
+ self.update_buttons()
+ self.preferences()
+
+ def preferences(self):
+ if self.install_path.get().strip():
+ save(launcher_root() / 'settings.json', {'root': self.install_path.get(), 'language': 'zh' if self.zh else 'en',
+ 'reuse_models': self.reuse_path.get(), 'copy_existing_models': self.copy_existing.get()})
+
+ def browse(self):
+ from tkinter import filedialog
+ value = filedialog.askdirectory(parent=self.window, title=self.t('Installation folder', '安装目录'))
+ if value:
+ self.install_path.set(value)
+
+ def browse_models(self):
+ from tkinter import filedialog
+ value = filedialog.askdirectory(parent=self.window, title=self.t('Existing model folder', '已有模型文件夹'))
+ if value:
+ self.reuse_path.set(value)
+
+ def model_arguments(self):
+ folder = self.reuse_path.get().strip()
+ arguments = (['--reuse-models', folder] + (['--copy-existing-models'] if self.copy_existing.get() else [])) if folder else []
+ if self.allow_restart.get():
+ arguments.append('--allow-model-restart')
+ return arguments
+
+ def begin(self, action, arguments):
+ if self.running:
+ return
+ self.running = True
+ for row in self.progress_rows.values():
+ row['bar'].stop()
+ row['frame'].destroy()
+ self.progress_rows.clear()
+ self.phase_status.set(self.t('Preparing…', '准备中…'))
+ self.status.set(self.t('Working…', '正在处理…'))
+ self.progress.configure(mode='indeterminate', value=0)
+ self.progress.start(15)
+ self.cancel_button.state(['!disabled'])
+ self.update_buttons()
+ try:
+ self.preferences()
+ self.runner.start(action, self.root(), arguments)
+ except BaseException:
+ self.running = False
+ self.progress.stop()
+ self.cancel_button.state(['disabled'])
+ self.update_buttons()
+ raise
+
+ def inspect(self):
+ self.plan = None
+ self.accept.set(False)
+ self.begin('plan', ['setup', '--plan', '--json', *self.model_arguments()])
+
+ def install(self):
+ if self.plan and not self.plan['errors'] and self.accept.get():
+ receipt = launcher_root() / 'plans' / ('approved-' + uuid.uuid4().hex + '.json')
+ save(receipt, self.plan)
+ self.begin('setup', ['setup', '--plain', '--yes', '--accept-model-license', '--approved-plan', str(receipt), *self.model_arguments()])
+
+ def ready_action(self, action, arguments):
+ from tkinter import messagebox
+ try:
+ machine = json.loads((self.root() / 'machine.json').read_text(encoding='utf-8'))
+ ready = machine.get('ready') is True and machine.get('source') == str(self.source)
+ except (OSError, ValueError):
+ ready = False
+ if not ready:
+ messagebox.showinfo('FreeVideo', self.t('Run setup/repair with this launcher first.', '请先使用此版本启动器完成安装/修复。'), parent=self.window)
+ self.tabs.select(0)
+ return False
+ self.begin(action, arguments)
+ return True
+
+ def generate(self):
+ from tkinter import messagebox
+ prompt = self.prompt.get('1.0', 'end-1c').strip()
+ if not prompt:
+ messagebox.showinfo('FreeVideo', self.t('Describe the video first.', '请先填写视频描述。'), parent=self.window)
+ return
+ run = self.root() / 'outputs' / ('gui-' + time.strftime('%Y%m%d-%H%M%S') + '-' + uuid.uuid4().hex[:6])
+ # Prompt text stays local and is excluded from the launcher command/log.
+ run.mkdir(parents=True)
+ (run / 'prompt.txt').write_text(prompt, encoding='utf-8')
+ width, height = self.resolution.get().split(' × ')
+ self.ready_action('generate', ['generate', '--prompt-file', str(run / 'prompt.txt'),
+ '--width', width, '--height', height, '--seconds', self.duration.get(), '--out', str(run / 'video.mp4')])
+
+ def predict(self):
+ width, height = self.resolution.get().split(' × ')
+ self.ready_action('predict', ['predict', '--width', width, '--height', height,
+ '--seconds', self.duration.get()])
+
+ def test(self, suite):
+ self.ready_action('test', ['test', '--suite', suite, '--plain'])
+
+ def diagnose(self):
+ from tkinter import filedialog
+ path = filedialog.asksaveasfilename(parent=self.window, defaultextension='.zip',
+ initialfile='freevideo-diagnostics-' + time.strftime('%Y%m%d-%H%M%S') + '.zip', filetypes=[('ZIP', '*.zip')])
+ if path:
+ self.begin('diagnose', ['diagnose', '--full', '--out', path])
+
+ def licenses(self):
+ from tkinter import messagebox
+ if not self.plan:
+ messagebox.showinfo('FreeVideo', self.t('Detect configuration to load the pinned model licenses.', '先检测配置,获取固定模型的许可证。'), parent=self.window)
+ return
+ popup = self.tk.Toplevel(self.window)
+ popup.title(self.t('Model licenses', '模型许可证'))
+ popup.transient(self.window)
+ self.label(popup, 'Read the licenses before confirming installation.', '确认安装前请阅读对应许可证。', padding=20).pack()
+ for url in self.plan['licenses']:
+ self.ttk.Button(popup, text=url, command=lambda value=url: webbrowser.open(value)).pack(fill='x', padx=20, pady=6)
+
+ def show_plan(self):
+ """Figures go in tiles; only what a figure cannot say stays as text."""
+ gib = lambda value: '%.1f GiB' % (value/2**30)
+ h = self.plan['inventory']['hardware']
+ estimate = self.plan['policy_estimate']
+ local = self.plan.get('local_models')
+ tiles = [(self.t('VRAM free', '空闲显存'), '%s / %s' % (gib(h['vram_free']), gib(h['vram_total']))),
+ (self.t('RAM available', '可用内存'), '%s / %s' % (gib(h['ram_available']), gib(h['ram_total']))),
+ (self.t('To download', '需下载'), gib(self.plan['model_download_bytes']))]
+ if estimate:
+ tiles.append((self.t('Work budget · GPU / RAM', '运行预算 · GPU/RAM'), '%.1f / %.1f GiB' % (
+ estimate['gpu_budget_bytes']/2**30, estimate['ram_budget_bytes']/2**30)))
+ for disk in self.plan['disks'][:1]:
+ tiles.append((self.t('Disk needed', '磁盘需求'),
+ '%s / %s' % (gib(disk['needed_bytes']), gib(disk['free_bytes']))))
+ if local:
+ tiles.append((self.t('Reused locally', '本地复用'),
+ '%d · %s' % (len(local['matches']), gib(local['reused_bytes']))))
+ while len(self.plan_tiles) < len(tiles):
+ self.plan_tiles.append(Stat(self.plan_grid, '', ''))
+ for index, tile in enumerate(self.plan_tiles):
+ if index < len(tiles):
+ tile.show(*tiles[index])
+ tile.grid(row=index//3, column=index % 3, sticky='nsew',
+ padx=(0 if index % 3 == 0 else branding.space(2), 0),
+ pady=(0, branding.space(2)))
+ else:
+ tile.grid_forget()
+ notes = [h['gpu_name']]
+ if self.plan.get('prepared_model'):
+ notes.append('FP8 · ' + self.plan['prepared_model']['repo'])
+ if self.plan['prepared_model'].get('private'):
+ notes.append(self.t('Hugging Face access required', '需要 Hugging Face 访问权限'))
+ if self.plan.get('allow_model_restart'):
+ notes.append(self.t('incomplete files may restart', '未完成文件可能重下'))
+ if local:
+ notes.append(self.t('originals stay in place', '原文件保留在原目录'))
+ if self.plan.get('storage') == 'retain':
+ notes.append(self.t('original weights retained', '保留原始权重'))
+ self.note.set(' · '.join(notes))
+ self.resource.set('\n'.join(self.plan['errors']))
+ for widget in (self.plan_grid, self.plan_note):
+ if not widget.winfo_manager():
+ widget.pack(fill='x', pady=(0, branding.space(3)), before=self.consent)
+ self.plan_text.pack_configure(pady=(0, branding.space(2)) if self.plan['errors'] else 0)
+
+ def open_path(self, path):
+ from tkinter import messagebox
+ if path and Path(path).exists():
+ os.startfile(str(path))
+ else:
+ messagebox.showinfo('FreeVideo', self.t('No result yet.', '目前还没有结果。'), parent=self.window)
+
+ def open_report(self):
+ reports = sorted((self.root() / 'test-runs').glob('*/report.html'))
+ self.open_path(reports[-1] if reports else None)
+
+ def open_outputs(self):
+ self.open_path(self.root())
+
+ def open_video(self):
+ videos = sorted((self.root() / 'outputs').glob('gui-*/video.mp4'))
+ self.open_path(videos[-1] if videos else None)
+
+ def open_logs(self):
+ self.open_path(self.last_log or launcher_root() / 'runs')
+
+ def toggle_log(self):
+ if self.log_window.state() != 'withdrawn':
+ self.log_window.withdraw()
+ else:
+ self.log_window.deiconify()
+ self.log_window.lift()
+
+ def append_log(self, line):
+ self.log.configure(state='normal')
+ self.log.insert('end', line)
+ if int(self.log.index('end-1c').split('.')[0]) > 600:
+ self.log.delete('1.0', '100.0')
+ self.log.see('end')
+ self.log.configure(state='disabled')
+
+ def progress_event(self, value):
+ """Separate installation stages from parallel task/file progress."""
+ if value.get('kind') == 'prediction':
+ from .prediction_ui import summary
+ self.prediction_status.set(summary(value.get('forecast', {}), zh=self.zh))
+ return
+ if value.get('kind') == 'phase':
+ total, done = value.get('total') or 0, value.get('done') or 0
+ self.progress.stop()
+ self.progress.configure(mode='determinate', maximum=max(1, total), value=done)
+ self.phase_status.set(self.t('Stages', '阶段') + ' %d / %d · %s' % (done, total, value.get('label', '')))
+ return
+ key = value.get('key') or value.get('label', 'task')
+ if key not in self.progress_rows:
+ for prior, row in list(self.progress_rows.items()):
+ if row['value'].get('state') in ('complete', 'failed', 'cancelled'):
+ row['bar'].stop()
+ row['frame'].destroy()
+ del self.progress_rows[prior]
+ card = Card(self.task_area, padding=branding.space(3), radius=branding.RADIUS_MD)
+ card.pack(fill='x', pady=(0, branding.space(2)))
+ frame = card.body
+ title, meta, detail = (self.tk.StringVar() for _ in range(3))
+ header = self.ttk.Frame(frame, style='Card.TFrame')
+ header.pack(fill='x')
+ self.ttk.Label(header, textvariable=title, style='Progress.TLabel').pack(side='left')
+ self.ttk.Label(header, textvariable=meta, style='ProgressMeta.TLabel').pack(side='right')
+ progress = Progress(frame, height=branding.space(1) + 2)
+ progress.pack(fill='x', pady=(branding.space(1), branding.space(1)))
+ self.ttk.Label(frame, textvariable=detail, style='ProgressMeta.TLabel', wraplength=790).pack(anchor='w')
+ self.progress_rows[key] = dict(frame=card, title=title, meta=meta, detail=detail, bar=progress)
+ row = self.progress_rows[key]
+ row.update(value=dict(value), received=time.monotonic())
+ row['title'].set(clipped(value.get('label', ''), 40))
+ detail = value.get('detail', '')
+ if self.zh:
+ for en, zh in [('Retrying same source', '正在重连原下载源'), ('Resume unavailable; checking another source', '此源无法续传,检查其他源'),
+ ('Paused; existing data kept', '下载已停止,进度已保留'), ('Restart explicitly allowed', '已允许重新下载未完成文件'),
+ ('Cannot resume this format; enable restart in Setup', '此格式无法续传;如需重下,请勾选允许重新下载并重新检测'),
+ ('MiB retained', 'MiB 已保留'), ('Continue installation / retry', '继续安装/重试')]:
+ detail = detail.replace(en, zh)
+ row['detail'].set(detail[:180])
+ total, done = value.get('total') or 0, value.get('done') or 0
+ bar = row['bar']
+ finished = value.get('state') in ('complete', 'failed', 'cancelled')
+ mode = 'determinate' if total or finished else 'indeterminate'
+ if str(bar['mode']) != mode or finished:
+ bar.stop()
+ bar.configure(mode=mode)
+ if mode == 'indeterminate':
+ bar.start(20)
+ if mode == 'determinate':
+ bar.configure(maximum=max(1, total), value=(total or 1) if value.get('state') == 'complete' else done)
+ if value.get('resource'):
+ self.status.set(value['resource'])
+ self.refresh_progress_times()
+
+ def refresh_progress_times(self):
+ for row in self.progress_rows.values():
+ value, age = row['value'], time.monotonic() - row['received']
+ finished = value.get('state') in ('complete', 'failed', 'cancelled')
+ elapsed = (value.get('elapsed_seconds') or 0) + (0 if finished else age)
+ pieces = [self.t('Elapsed ', '已用 ') + duration(elapsed)]
+ if value.get('total'):
+ pieces.insert(0, '%.0f%%' % min(100, max(0, 100*(value.get('done') or 0)/value['total'])))
+ remaining = value.get('remaining_seconds') if age <= 10 else None
+ if finished:
+ pieces.append(self.t('Complete', '完成') if value['state'] == 'complete' else self.t('Stopped', '已停止'))
+ elif remaining == 0:
+ pieces.append(self.t('Finalizing', '正在收尾'))
+ elif remaining is not None:
+ pieces.append(self.t('ETA ~', '预计剩余约 ') + duration(remaining))
+ else:
+ pieces.append(self.t('Estimating…', '估算中…'))
+ row['meta'].set(' · '.join(pieces))
+
+ def poll(self):
+ self.cancel_poll()
+ try:
+ for _ in range(150):
+ kind, value = self.events.get_nowait()
+ if kind == 'started':
+ self.last_log = value
+ elif kind == 'log':
+ self.append_log(value)
+ elif kind == 'progress':
+ self.progress_event(value)
+ elif kind == 'done':
+ self.running = False
+ if value['action'] == 'setup':
+ self.setup_retry = value['status'] != 'complete'
+ self.progress.stop()
+ if value['status'] == 'complete':
+ self.progress.configure(mode='determinate', maximum=1, value=1)
+ for key, row in list(self.progress_rows.items()):
+ if row['value'].get('state') not in ('complete', 'failed', 'cancelled'):
+ self.progress_event(dict(row['value'], key=key, state=value['status']))
+ self.cancel_button.state(['disabled'])
+ if value['action'] == 'plan':
+ try:
+ self.plan = preflight_json(value['output'])
+ self.show_plan()
+ except ValueError as error:
+ reason = value.get('error') or '\n'.join(value['output'].strip().splitlines()[-6:])
+ self.resource.set(self.t('Configuration check failed:', '配置检测未完成:') + '\n' + (reason or str(error))[-1200:])
+ labels = {'complete': self.t('Complete', '已完成'), 'failed': self.t('Failed — open details or export diagnostics', '未完成,可查看详情或导出诊断'),
+ 'cancelled': self.t('Stopped. Files retained.', '已停止,文件已保留。')}
+ self.status.set(labels[value['status']] + ' · %.1f s' % value['wall_seconds'])
+ if value['action'] == 'setup' and self.setup_retry:
+ self.status.set(self.t('Installation stopped. Use Continue installation / retry; completed files are reused.',
+ '安装已停止。点击「继续安装/重试」,复用已完成文件。'))
+ self.update_buttons()
+ if self.closing:
+ self.stop_progress()
+ self.window.destroy()
+ return
+ except queue.Empty:
+ pass
+ self.refresh_progress_times()
+ self.poll_timer = self.window.after(100, self.poll)
+
+ def cancel_poll(self, event=None):
+ if event is not None and event.widget != self.window:
+ return
+ timer = getattr(self, 'poll_timer', None)
+ if timer is not None:
+ self.window.after_cancel(timer)
+ self.poll_timer = None
+
+ def cancel(self):
+ self.status.set(self.t('Stopping workers; retaining files…', '正在停止工作进程并保留文件…'))
+ self.cancel_button.state(['disabled'])
+ self.runner.cancel()
+
+ def close(self):
+ from tkinter import messagebox
+ if self.running:
+ if not messagebox.askyesno('FreeVideo', self.t('Stop the current task and close? All files are retained.', '停止当前任务并退出?所有文件会保留。'), parent=self.window):
+ return
+ self.closing = True
+ self.cancel()
+ else:
+ self.stop_progress()
+ self.window.destroy()
+
+ def stop_progress(self):
+ self.progress.stop()
+ for row in self.progress_rows.values():
+ row['bar'].stop()
+
+ def callback_error(self, kind, value, trace):
+ from tkinter import messagebox
+ detail = ''.join(traceback.format_exception(kind, value, trace))
+ self.append_log(detail)
+ path = launcher_root() / 'errors' / ('error-' + uuid.uuid4().hex + '.log')
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(detail, encoding='utf-8')
+ messagebox.showerror('FreeVideo', str(value) + '\n' + str(path), parent=self.window)
+
+
+def main():
+ import tkinter as tk
+ if windows():
+ import ctypes
+ try:
+ ctypes.windll.shcore.SetProcessDpiAwareness(2)
+ except (AttributeError, OSError):
+ pass
+ # A hidden console gives Ctrl+Break a real console group while the GUI
+ # remains windowed. Workers can finalize reports before forced cleanup.
+ lib = ctypes.windll.kernel32
+ lib.GetConsoleWindow.restype = ctypes.c_void_p
+ if not lib.GetConsoleWindow():
+ lib.AllocConsole()
+ ctypes.windll.user32.ShowWindow(ctypes.c_void_p(lib.GetConsoleWindow()), 0)
+ window = tk.Tk()
+ app = Desktop(window)
+ if '--smoke-test' in sys.argv:
+ destination = Path(sys.argv[sys.argv.index('--smoke-test') + 1])
+ destination.mkdir(parents=True, exist_ok=True)
+ app.begin('help', ['--help'])
+ deadline = time.monotonic() + 240
+ from .ram import ProcessMemory
+ observation = ProcessMemory()
+ def finished():
+ observation.sample(os.getpid())
+ if app.running and time.monotonic() < deadline:
+ window.after(100, finished)
+ return
+ if app.running:
+ app.runner.cancel()
+ save(destination / 'smoke.json', {'success': False, 'error': 'Launcher smoke timeout'})
+ else:
+ status = json.loads((Path(app.last_log) / 'status.json').read_text(encoding='utf-8'))
+ from .system import system_memory
+ save(destination / 'smoke.json', {'success': status['status'] == 'complete',
+ 'window_geometry': window.geometry(), 'task': status, 'source': str(app.source),
+ 'model_modules_imported': [p for p in ('torch', 'triton', 'transformers') if p in sys.modules],
+ 'launcher_process_tree': observation.result(),
+ 'memory': system_memory()})
+ window.destroy()
+ window.after(100, finished)
+ window.mainloop()
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freevideo_engine/desktop_runtime.py b/freevideo_engine/desktop_runtime.py
new file mode 100644
index 0000000..3e91804
--- /dev/null
+++ b/freevideo_engine/desktop_runtime.py
@@ -0,0 +1,281 @@
+"""Desktop orchestration and durable source deployment; independent of widgets."""
+import hashlib
+import json
+import os
+from pathlib import Path
+import queue
+import shutil
+import subprocess
+import sys
+import tempfile
+import threading
+import time
+import traceback
+import uuid
+
+from . import __version__, processes
+from .monitoring import save
+from .system import install_root, windows
+
+
+def launcher_root():
+ if os.environ.get('FREEVIDEO_LAUNCHER_HOME'):
+ return Path(os.environ['FREEVIDEO_LAUNCHER_HOME'])
+ home = os.environ.get('LOCALAPPDATA') or str(Path.home())
+ return Path(home) / 'FreeVideo-launcher'
+
+
+def source_files(root):
+ names = ['pyproject.toml', 'README.md', 'LICENSE', 'NOTICE', 'THIRD_PARTY_NOTICES.md', 'MANIFEST.in', '__init__.py',
+ 'freevideo', 'setup.sh', 'test.sh',
+ 'freevideo.ps1', 'setup.ps1', 'test.ps1', 'freevideo.cmd', 'setup.cmd', 'test.cmd',
+ 'scripts/bootstrap_linux.sh', 'scripts/windows_app.py', 'scripts/build_windows.py',
+ 'scripts/check_installation.py', 'scripts/build_launcher_notices.py']
+ files = [root / name for name in names]
+ files += [p for folder in ('freevideo_engine', 'constraints', 'web', 'example_workflows')
+ for p in (root / folder).rglob('*')
+ if p.is_file() and p.suffix in ('.py', '.json', '.txt', '.ps1', '.sh', '.js', '.css', '.svg', '.png', '.ico', '.qml')]
+ return sorted(files)
+
+
+def package_data_files(root):
+ """Resources also read by the frozen GUI itself, outside engine-source."""
+ return sorted(p for p in (root / 'freevideo_engine').rglob('*')
+ if p.is_file() and (p.suffix in ('.json', '.ps1', '.svg', '.png', '.ico', '.qml')
+ or p.name == 'build-version.txt' or (p.suffix == '.txt' and 'licenses' in p.parts)))
+
+
+def matching_source(bundled, installed):
+ """Compare deployed code, ignoring the per-installation ComfyUI binding.
+
+ Read source only: neither editable metadata nor an old receipt is proof
+ that the running launcher and the selected engine contain the same code.
+ """
+ bundled, installed = Path(bundled).resolve(), Path(installed).resolve()
+ if bundled == installed:
+ return True
+ try:
+ for path in source_files(bundled):
+ if path.name == 'comfyui.json':
+ continue
+ other = installed / path.relative_to(bundled)
+ if not other.is_file() or path.read_bytes() != other.read_bytes():
+ return False
+ return True
+ except OSError:
+ return False
+
+
+def check_launcher_dependencies():
+ """Exercise the native process extension, including inside the frozen EXE."""
+ import psutil
+ process = psutil.Process(os.getpid())
+ if process.create_time() <= 0 or process.memory_info().rss <= 0:
+ raise RuntimeError('Launcher process inspection returned invalid counters')
+ return dict(psutil_version=psutil.__version__, process_inspection=True)
+
+
+def check_launcher_reopen(destination):
+ """Smoke an existing installation's history path without user files or GPU work."""
+ dependencies = check_launcher_dependencies()
+ from .compatibility import check_installation
+ from .resource_history import ResourceHistory
+ import platform
+ with tempfile.TemporaryDirectory(prefix='reopen-', dir=destination) as folder:
+ root = Path(folder)
+ identity = dict(gpu_uuid='GPU-LAUNCHER-CPU-SMOKE', system=platform.system())
+ save(root / 'machine.json', identity)
+ history = ResourceHistory(root / 'resource-history.sqlite3')
+ attempt = history.begin(identity, {'engine': {'head_chunk': 2, 'steps': 8}},
+ dict(width=1344, height=768, frames=243, steps=8))
+ state = check_installation(root)
+ if state['level'] != 0 or history.attempt(attempt) is None:
+ raise RuntimeError('Launcher reopening misclassified its live process')
+ history.finish(attempt, 'cancelled')
+ return dict(dependencies, installed_history=True)
+
+
+def check_launcher_payload(source, destination):
+ """Exercise installation metadata in the actual EXE, without installing anything."""
+ from . import comfy_source, network
+ package = Path(__file__).parent
+ files = package_data_files(package.parent)
+ # A missing manifest must fail even if the frozen package contains no data.
+ for name in ('dependencies.json', 'bootstrap_versions.json', 'model_files.json', 'prepared_models.json', 'test_prompts.json'):
+ json.loads((package / name).read_text(encoding='utf-8'))
+ for path in files:
+ relative = path.relative_to(package.parent)
+ if path.read_bytes() != (source / relative).read_bytes():
+ raise ValueError('Packaged resource differs from installation source: ' + str(relative))
+ layout = comfy_source.new_layout(str(destination / 'new-install'))
+ if Path(layout['root']).exists():
+ raise ValueError('Launcher smoke inspection created a ComfyUI installation')
+ return dict(resources=[p.relative_to(package.parent).as_posix() for p in files],
+ comfy_source=layout['source_spec'], model_repo=network.model_spec()['vdn_repo'])
+
+
+def materialize_source(bundle=None, destination=None):
+ """A one-file executable's temporary extraction must never back an editable install."""
+ if bundle is None and not getattr(sys, 'frozen', False):
+ return Path(__file__).resolve().parents[1]
+ bundle = Path(bundle or Path(sys._MEIPASS) / 'engine-source')
+ rows = {p.relative_to(bundle).as_posix(): hashlib.sha256(p.read_bytes()).hexdigest() for p in source_files(bundle)}
+ identity = hashlib.sha256(json.dumps(rows, sort_keys=True).encode()).hexdigest()[:16]
+ parent = Path(destination or launcher_root() / 'source')
+ target = parent / (__version__ + '-' + identity)
+ if target.is_dir() and all((target / p).is_file() and hashlib.sha256((target / p).read_bytes()).hexdigest() == digest
+ for p, digest in rows.items()):
+ return target
+ parent.mkdir(parents=True, exist_ok=True)
+ stage = Path(tempfile.mkdtemp(prefix='source-', dir=parent))
+ for relative in rows:
+ output = stage / relative
+ output.parent.mkdir(parents=True, exist_ok=True)
+ shutil.copyfile(bundle / relative, output)
+ save(stage / 'launcher-source.json', {'version': __version__, 'sha256': rows})
+ # A changed source or concurrent launcher is retained, never overwritten.
+ if target.exists():
+ target = target.with_name(target.name + '-' + uuid.uuid4().hex[:8])
+ stage.rename(target)
+ return target
+
+
+def preflight_json(output):
+ decoder = json.JSONDecoder()
+ for offset, character in enumerate(output):
+ if character == '{':
+ try:
+ value, _ = decoder.raw_decode(output[offset:])
+ if isinstance(value, dict) and 'inventory' in value and 'policy_estimate' in value:
+ return value
+ except ValueError:
+ pass
+ raise ValueError('No complete setup plan was returned; inspect the retained launcher log.')
+
+
+def local_model_ui(message):
+ phase = message.get('phase')
+ done, total = message.get('done_bytes', 0), message.get('total_bytes', 0)
+ rate = message.get('bytes_per_second')
+ label = {'scan': 'Find existing models', 'verify': 'Verify local model', 'copy': 'Copy local model',
+ 'link': 'Reuse local model', 'ready': 'Existing models checked'}.get(phase, 'Check local models')
+ detail = message.get('file', '')
+ if phase == 'scan':
+ detail = '%d files / folders scanned' % done
+ done = 0
+ elif phase in ('verify', 'copy'):
+ detail = '%.1f / %.1f MiB · %.1f MiB/s · %s' % (done/2**20, total/2**20, (rate or 0)/2**20, detail)
+ return dict(kind='task_end' if phase == 'ready' else 'progress', key='local-models',
+ label=label, detail=detail, state='complete' if phase == 'ready' else 'running',
+ done=done, total=total, elapsed_seconds=message.get('elapsed_seconds', 0),
+ remaining_seconds=(total-done)/rate if total and rate and phase in ('verify', 'copy') else None)
+
+
+class Runner:
+ def __init__(self, source, events, logs=None):
+ self.source, self.events = Path(source), events
+ self.logs = Path(logs or launcher_root() / 'runs')
+ self.process = self.thread = None
+ self.cancelled = threading.Event()
+ self.lock = threading.Lock()
+
+ @property
+ def busy(self):
+ return self.thread is not None and self.thread.is_alive()
+
+ def start(self, action, root, arguments):
+ if self.busy:
+ raise RuntimeError('A launcher task is already running')
+ self.cancelled.clear()
+ self.thread = threading.Thread(target=self._run, args=(action, Path(root), list(arguments)), daemon=True)
+ self.thread.start()
+
+ def cancel(self):
+ self.cancelled.set()
+ def stop():
+ with self.lock:
+ if self.process is not None:
+ processes.stop(self.process)
+ threading.Thread(target=stop, daemon=True).start()
+
+ def _run(self, action, root, arguments):
+ from .windows_ux import awake
+ with awake():
+ self._run_awake(action, root, arguments)
+
+ def command(self, root, arguments):
+ powershell = Path(os.environ.get('SystemRoot', r'C:\Windows')) / 'System32/WindowsPowerShell/v1.0/powershell.exe'
+ return [str(powershell), '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File',
+ str(self.source / 'freevideo.ps1'), '--root', str(root), *arguments]
+
+ def environment(self, root):
+ return dict(os.environ, PYTHONUTF8='1', PYTHONIOENCODING='utf-8', NO_COLOR='1', FREEVIDEO_UI_EVENTS='1')
+
+ def _run_awake(self, action, root, arguments):
+ run = self.logs / (time.strftime('%Y%m%dT%H%M%SZ', time.gmtime()) + '-' + uuid.uuid4().hex[:8])
+ code, output, error = None, '', None
+ started = time.monotonic()
+ try:
+ run.mkdir(parents=True)
+ command = self.command(root, arguments)
+ save(run / 'status.json', dict(action=action, command=command, root=str(root), status='running'))
+ self.events.put(('started', str(run)))
+ env = self.environment(root)
+ with (run / 'launcher.log').open('w', encoding='utf-8', buffering=1) as log:
+ with self.lock:
+ # External Python must not inherit the frozen GUI's DLL
+ # directory. Restore it immediately for subsequent Tk loads.
+ dll_directory = None
+ if getattr(sys, 'frozen', False) and windows():
+ import ctypes
+ buffer = ctypes.create_unicode_buffer(32768)
+ ctypes.windll.kernel32.GetDllDirectoryW(len(buffer), buffer)
+ dll_directory = buffer.value
+ ctypes.windll.kernel32.SetDllDirectoryW(None)
+ try:
+ self.process = processes.popen(command, cwd=self.source, env=env, stdin=subprocess.DEVNULL,
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, encoding='utf-8', errors='replace',
+ start_new_session=True, supervise=True)
+ finally:
+ if dll_directory is not None:
+ ctypes.windll.kernel32.SetDllDirectoryW(dll_directory)
+ process = self.process
+ if self.cancelled.is_set():
+ processes.stop(process)
+ for line in process.stdout:
+ log.write(line)
+ output = (output + line)[-2 * 1024 * 1024:]
+ self.events.put(('log', line))
+ if line.startswith('{'):
+ try:
+ message = json.loads(line)
+ if message.get('event') == 'freevideo_ui':
+ self.events.put(('progress', message))
+ elif message.get('event') == 'local_model_progress':
+ self.events.put(('progress', local_model_ui(message)))
+ except (ValueError, AttributeError):
+ pass
+ code = process.wait(timeout=15)
+ process.stdout.close()
+ except BaseException as failure:
+ error = repr(failure)
+ detail = '\n' + traceback.format_exc()
+ output += detail
+ try:
+ with (run / 'launcher.log').open('a', encoding='utf-8') as log:
+ log.write(detail)
+ except OSError:
+ pass
+ self.events.put(('log', error + '\n'))
+ if self.process is not None:
+ processes.stop(self.process)
+ finally:
+ result = dict(action=action, status='cancelled' if self.cancelled.is_set() else 'complete' if code == 0 else 'failed',
+ returncode=code, error=error, wall_seconds=time.monotonic()-started, root=str(root), log=str(run))
+ try:
+ save(run / 'status.json', result)
+ except OSError as failure:
+ result.update(status='failed', error=str(failure))
+ with self.lock:
+ self.process = None
+ self.events.put(('done', dict(result, output=output)))
diff --git a/freevideo_engine/desktop_shortcut.py b/freevideo_engine/desktop_shortcut.py
new file mode 100644
index 0000000..2819eaa
--- /dev/null
+++ b/freevideo_engine/desktop_shortcut.py
@@ -0,0 +1,149 @@
+"""A durable, per-user Windows shortcut; no shell scripts or registry writes."""
+import hashlib
+import json
+import os
+from pathlib import Path
+import shutil
+import subprocess
+import sys
+import tempfile
+import uuid
+
+from .monitoring import save
+from .system import windows
+
+
+def digest(path):
+ value = hashlib.sha256()
+ with Path(path).open('rb') as stream:
+ for chunk in iter(lambda: stream.read(1024 * 1024), b''):
+ value.update(chunk)
+ return value.hexdigest()
+
+
+def desktop_directory():
+ import ctypes
+ from ctypes import wintypes
+ buffer = ctypes.create_unicode_buffer(32768)
+ call = ctypes.WinDLL('shell32').SHGetFolderPathW
+ call.argtypes = [wintypes.HWND, ctypes.c_int, wintypes.HANDLE, wintypes.DWORD, wintypes.LPWSTR]
+ call.restype = ctypes.c_long
+ if call(None, 0x8010, None, 0, buffer) != 0 or not buffer.value:
+ raise OSError('Windows could not locate your desktop folder')
+ return Path(buffer.value)
+
+
+def write_link(path, target, arguments, working_directory, icon):
+ """Use IShellLinkW / IPersistFile, including Unicode and redirected desktops."""
+ import ctypes as c
+ class GUID(c.Structure):
+ _fields_ = [('data', c.c_ubyte * 16)]
+ def __init__(self, value):
+ super().__init__()
+ self.data[:] = uuid.UUID(value).bytes_le
+ ole = c.WinDLL('ole32')
+ ole.CoInitializeEx.argtypes = [c.c_void_p, c.c_uint32]; ole.CoInitializeEx.restype = c.c_long
+ ole.CoUninitialize.argtypes = []; ole.CoUninitialize.restype = None
+ ole.CoCreateInstance.argtypes = [c.POINTER(GUID), c.c_void_p, c.c_uint32, c.POINTER(GUID), c.POINTER(c.c_void_p)]
+ ole.CoCreateInstance.restype = c.c_long
+ initialized = ole.CoInitializeEx(None, 2)
+ if initialized < 0 and initialized != -2147417850: # Already initialized in another COM mode is usable.
+ raise OSError('Windows shortcut initialization failed: 0x%08x' % (initialized & 0xffffffff))
+ link, persist = c.c_void_p(), c.c_void_p()
+ def checked(result):
+ if result < 0:
+ raise OSError('Windows shortcut failed: 0x%08x' % (result & 0xffffffff))
+ def method(obj, index, types, *values):
+ table = c.cast(obj, c.POINTER(c.POINTER(c.c_void_p))).contents
+ result = c.WINFUNCTYPE(c.c_long, c.c_void_p, *types)(table[index])(obj, *values)
+ checked(result)
+ try:
+ cls = GUID('00021401-0000-0000-c000-000000000046')
+ iid = GUID('000214f9-0000-0000-c000-000000000046')
+ checked(ole.CoCreateInstance(c.byref(cls), None, 1, c.byref(iid), c.byref(link)))
+ method(link, 20, [c.c_wchar_p], str(target))
+ method(link, 11, [c.c_wchar_p], subprocess.list2cmdline(arguments))
+ method(link, 9, [c.c_wchar_p], str(working_directory))
+ method(link, 7, [c.c_wchar_p], 'FreeVideo')
+ method(link, 17, [c.c_wchar_p, c.c_int], str(icon), 0)
+ persist_iid = GUID('0000010b-0000-0000-c000-000000000046')
+ method(link, 0, [c.POINTER(GUID), c.POINTER(c.c_void_p)], c.byref(persist_iid), c.byref(persist))
+ method(persist, 6, [c.c_wchar_p, c.c_int], str(path), 1)
+ finally:
+ if persist: method(persist, 2, [])
+ if link: method(link, 2, [])
+ if initialized >= 0: ole.CoUninitialize()
+
+
+def launcher_target(engine, source, portable_root=None):
+ if portable_root:
+ target = Path(portable_root) / 'FreeVideo.exe'
+ if not target.is_file():
+ raise FileNotFoundError('Bundle launcher is missing: ' + str(target))
+ return target, [], target
+ if getattr(sys, 'frozen', False):
+ executable = Path(sys.executable).resolve()
+ from .launcher_update import current_build
+ folder = (current_build() or {}).get('packaging') == 'onedir'
+ fingerprint = digest(executable)
+ parent = Path(engine) / 'launcher/application'
+ root = parent / fingerprint
+ target = root / 'FreeVideo.exe'
+ if not target.is_file() or digest(target) != fingerprint or (folder and not (root / '_internal').is_dir()):
+ parent.mkdir(parents=True, exist_ok=True)
+ stage = Path(tempfile.mkdtemp(prefix='launcher-', dir=parent))
+ shutil.copy2(executable, stage / 'FreeVideo.exe')
+ if folder:
+ shutil.copytree(executable.parent / '_internal', stage / '_internal')
+ if digest(stage / 'FreeVideo.exe') != fingerprint:
+ raise OSError('Launcher changed while preparing the desktop shortcut; retry installation')
+ if root.exists():
+ root = root.with_name(fingerprint + '-' + uuid.uuid4().hex[:8])
+ stage.rename(root)
+ target = root / 'FreeVideo.exe'
+ return target, [], target
+ python = Path(sys.executable).resolve()
+ if python.with_name('pythonw.exe').is_file():
+ python = python.with_name('pythonw.exe')
+ code = "import runpy,sys;sys.path.insert(0,sys.argv[1]);runpy.run_module('freevideo_engine.comfy_launcher',run_name='__main__')"
+ return python, ['-c', code, str(Path(source).resolve())], Path(source) / 'freevideo_engine/assets/icon.ico'
+
+
+def create(engine, source, portable_root=None):
+ if not windows():
+ return dict(status='not-applicable')
+ engine = Path(engine)
+ receipt = engine / 'launcher/desktop-shortcut.json'
+ target, arguments, icon = launcher_target(engine, source, portable_root)
+ desktop = desktop_directory()
+ path = desktop / 'FreeVideo.lnk'
+ try:
+ previous = json.loads(receipt.read_text(encoding='utf-8'))
+ except (OSError, ValueError):
+ previous = {}
+ old = Path(previous.get('path', str(path)))
+ if old.parent == desktop and old.is_file() and digest(old) == previous.get('sha256'):
+ path = old
+ if previous.get('target') == str(target) and previous.get('arguments') == arguments:
+ return dict(previous, status='present')
+ else:
+ index = 2
+ while path.exists():
+ path = desktop / ('FreeVideo (%d).lnk' % index); index += 1
+ stage = desktop / ('FreeVideo-' + uuid.uuid4().hex + '.lnk')
+ try:
+ write_link(stage, target, arguments, target.parent, icon)
+ os.replace(stage, path)
+ finally:
+ if stage.exists(): stage.unlink()
+ result = dict(status='created', path=str(path), target=str(target), arguments=arguments, sha256=digest(path))
+ save(receipt, result)
+ return result
+
+
+def create_after_install(engine, source, portable_root=None):
+ # An unwritable desktop must not undo a successful model installation.
+ try:
+ return create(engine, source, portable_root)
+ except (OSError, ValueError) as error:
+ return dict(status='failed', error='Installed successfully, but the desktop shortcut could not be created: ' + str(error))
diff --git a/freevideo_engine/diagnostic_resources.py b/freevideo_engine/diagnostic_resources.py
new file mode 100644
index 0000000..47ada0e
--- /dev/null
+++ b/freevideo_engine/diagnostic_resources.py
@@ -0,0 +1,675 @@
+"""Bounded diagnostic evidence without tensor imports, paths or exception text."""
+import math
+import re
+import sys
+
+
+RAM_PEAKS = ('process_tree_peak_guard_bytes', 'process_tree_peak_rss_bytes', 'process_tree_peak_pss_bytes',
+ 'process_tree_peak_private_commit_bytes', 'process_tree_peak_private_working_set_bytes',
+ 'system_min_available_bytes', 'system_min_physical_available_bytes', 'system_min_commit_available_bytes',
+ 'effective_min_available_bytes', 'inference_min_available_bytes', 'inference_peak_working_pss_bytes', 'ram_observation_samples', 'process_tree_disk_read_bytes',
+ 'process_tree_disk_write_bytes')
+RAM_SAMPLE = ('rss_bytes', 'pss_bytes', 'nonfile_pss_bytes', 'inference_guard_bytes', 'inference_available_bytes', 'private_commit_bytes', 'private_working_set_bytes', 'guard_bytes',
+ 'reclaimable_mapped_bytes', 'effective_available_bytes', 'system_available_bytes',
+ 'system_physical_available_bytes', 'system_commit_available_bytes', 'processes')
+GUARD = ('working_bytes', 'budget_bytes', 'available_bytes', 'emergency_floor_bytes',
+ 'physical_available_bytes', 'commit_available_bytes')
+HARDWARE_MEMORY = ('ram_total', 'ram_available', 'vram_total', 'vram_free', 'cgroup_ram_limit')
+ENGINE_KNOBS = ('resident_blocks', 'pin_host_gb', 'head_chunk', 'window_batch', 'head_parallelism',
+ 'ff_chunk', 'projection_chunk', 'steps', 'prefetch', 'stream_weights', 'attention_cpu_outputs',
+ 'grouped_attention_outputs', 'residual_offload', 'fp8_ff_recompute', 'query_chunk', 'fp8_linears',
+ 'resident_weight_bytes', 'pinned_model_bytes', 'pinned_host_allocated_bytes', 'inference_kernels',
+ 'window_varlen', 'varlen_smooth_k', 'reuse_block_outputs', 'preload_host', 'pin_host_weights',
+ 'cache_refined_text')
+ATTENTION_BACKENDS = frozenset(
+ 'global_cudnn global_torch-flash global_sage2 global_fa2 global_fa4 '
+ 'window_cudnn window_torch-flash window_sage2 window_fa2 window_fa4 '
+ 'window_fa2_varlen window_fa4_varlen window_sage2_varlen'.split())
+DECODER_KNOBS = ('resident_blocks', 'offload', 'prefetch', 'stream_weights', 'stream_output',
+ 'pin_weights', 'preload', 'tile_group', 'linear_compute_cache')
+DECODE_PHASES = ('decoder_admission', 'vae_load', 'video_decode', 'video_postprocess', 'rgb_save',
+ 'audio_load_decode', 'audio_save', 'mp4_save', 'latent_load', 'latent_transfer')
+SAMPLE_FINALIZE_PHASES = ('latent_validation', 'latent_save', 'offload_release', 'transformer_release')
+HOST_SAMPLE = ('pinned_allocated_bytes', 'pinned_active_bytes', 'pinned_cached_bytes',
+ 'physical_available_bytes', 'commit_available_bytes', 'torch_threads', 'torch_interop_threads')
+PROCESS_DELTA = ('cpu_user_seconds', 'cpu_system_seconds', 'read_bytes', 'write_bytes')
+WEIGHT_PLACEMENT = ('transfers', 'h2d_bytes', 'host_stage_seconds', 'h2d_seconds',
+ 'host_buffer_wait_seconds', 'prefetch_wait_seconds', 'pinned_model_bytes', 'pinned_host_allocated_bytes',
+ 'pinned_layer_count', 'streamed_layer_count', 'pinned_buffer_bytes', 'pageable_buffer_bytes', 'cuda_buffer_bytes',
+ 'direct_read_layers', 'direct_read_bytes',
+ 'host_prefetch_reads', 'host_prefetch_wait_seconds', 'host_prefetch_buffer_bytes',
+ 'pass_cache_budget_bytes', 'pass_cache_bytes', 'pass_cache_hits', 'pass_cache_layer_count')
+ERROR_CATEGORIES = ('cuda_out_of_memory', 'torch_allocator_out_of_memory',
+ 'host_memory_allocation', 'cuda_error', 'checkpoint_changed', 'other')
+ENCODER_DTYPES = ('uint8', 'int8', 'int16', 'int32', 'int64', 'float16', 'bfloat16',
+ 'float32', 'float64', 'float8_e4m3fn', 'float8_e5m2')
+TENSOR_VARIABLES = ('input', 'output', 'x', 'q', 'scale', 'indices', 'qdata', 'weight', 'mat1', 'mat2')
+
+
+def error_details(error):
+ """Extract fixed classifications and numbers, never the exception message.
+
+ CUDA runtime allocation errors and PyTorch allocator OOMs are distinct:
+ only the latter normally include the failed allocation's size.
+ """
+ result = numbers({'error_code': getattr(error, 'error_code', None)}, ('error_code',))
+ message = str(error)
+ if 'CUDA error: out of memory' in message or 'cudaErrorMemoryAllocation' in message:
+ result.update(category='cuda_out_of_memory', error_code=2)
+ elif 'CUDA out of memory' in message:
+ result['category'] = 'torch_allocator_out_of_memory'
+ elif isinstance(error, MemoryError) or getattr(error, 'winerror', None) == 1455:
+ result['category'] = 'host_memory_allocation'
+ elif 'CUDA error:' in message:
+ result['category'] = 'cuda_error'
+ elif message.startswith('Checkpoint changed during streamed inference:'):
+ result['category'] = 'checkpoint_changed'
+ result['checkpoint_change'] = checkpoint_change(getattr(error, 'checkpoint_change', None))
+ else:
+ result['category'] = 'other'
+ size = re.search(r'\bTried to allocate (\d+(?:\.\d+)?) (bytes|KiB|MiB|GiB)\b', message)
+ if size:
+ amount = float(size.group(1)) * {'bytes': 1, 'KiB': 2**10, 'MiB': 2**20, 'GiB': 2**30}[size.group(2)]
+ if math.isfinite(amount):
+ result['requested_allocation_bytes'] = int(amount)
+ return result
+
+
+def checkpoint_change(value):
+ """Fixed file-change evidence only; no paths, names or timestamp values."""
+ value = mapping(value)
+ result = {}
+ if value.get('source') in ('path', 'handle'):
+ result['source'] = value['source']
+ fields = value.get('fields', ())
+ result['fields'] = [field for field in ('st_dev', 'st_ino', 'st_size', 'st_mtime_ns', 'st_ctime_ns')
+ if field in sequence(fields)]
+ return result
+
+
+def token_summary(value):
+ return {key: count for key, count in numbers(value, ('sequences', 'max_sequence_tokens',
+ 'total_tokens', 'image_count', 'video_count')).items() if type(count) is int}
+
+
+def encoder_timing(value):
+ from .encoder_diagnostics import STAGES
+ value = mapping(value)
+ result = numbers(value, ('version', 'elapsed_seconds', 'diagnostic_seconds'))
+ if type(value.get('complete')) is bool:
+ result['complete'] = value['complete']
+ stages = numbers(value.get('stage_seconds'), STAGES)
+ if stages:
+ result['stage_seconds'] = stages
+ return result
+
+
+def encoder_runtime(value):
+ value = mapping(value)
+ return {key: value[key] for key in ('resident_worker', 'torch_imported_at_start',
+ 'cuda_initialized_at_start', 'native_imported_at_start') if type(value.get(key)) is bool}
+
+
+def encoder_prewarm(value):
+ value = mapping(value)
+ result = numbers(value, ('elapsed_seconds', 'seconds', 'bytes', 'done_bytes', 'total_bytes',
+ 'gpu_bytes_before', 'gpu_bytes_after', 'gpu_budget_bytes', 'age_seconds'))
+ if value.get('state') in ('waiting', 'loading', 'ready', 'partial', 'skipped', 'cancelled', 'released', 'failed', 'complete'):
+ result['state'] = value['state']
+ for key in ('gpu_preload', 'worker_released', 'receipt_available'):
+ if type(value.get(key)) is bool:
+ result[key] = value[key]
+ return result
+
+
+def embedding(value):
+ """Tensor metadata only: no indices, tensor values, prompt or media content."""
+ value = mapping(value)
+ result = numbers(value, ('group_size',))
+ for key in ('indices_shape', 'weight_shape', 'scale_shape', 'output_shape'):
+ shape = value.get(key)
+ if isinstance(shape, (tuple, list)) and len(shape) <= 8 and all(type(n) is int and n >= 0 for n in shape):
+ result[key] = list(shape)
+ if value.get('dtype') in ENCODER_DTYPES:
+ result['dtype'] = value['dtype']
+ if value.get('device') in ('cpu', 'cuda', 'meta'):
+ result['device'] = value['device']
+ return result
+
+
+def tensor_metadata(value):
+ """Only fixed tensor names and metadata, sanitized again before upload."""
+ result = {}
+ for key in TENSOR_VARIABLES:
+ source = mapping(mapping(value).get(key))
+ item = embedding(dict(indices_shape=source.get('shape'), dtype=source.get('dtype'),
+ device=source.get('device')))
+ if 'indices_shape' not in item:
+ continue
+ item['shape'] = item.pop('indices_shape')
+ result[key] = item
+ if len(result) == 4:
+ break
+ return result
+
+
+def _frame_tensors(frame):
+ # Never import torch on the control plane. Exception collection can inspect
+ # metadata if a worker already imported it, without copying tensor contents.
+ torch = sys.modules.get('torch')
+ tensor_type = getattr(torch, 'Tensor', None)
+ if not isinstance(tensor_type, type):
+ return {}
+ result = {}
+ for key in TENSOR_VARIABLES:
+ tensor = frame.f_locals.get(key)
+ if not issubclass(type(tensor), tensor_type):
+ continue
+ try:
+ result[key] = dict(shape=list(tensor.shape), dtype=str(tensor.dtype).removeprefix('torch.'),
+ device=tensor.device.type)
+ except (AttributeError, RuntimeError, TypeError):
+ continue
+ if len(result) == 4:
+ break
+ return tensor_metadata(result)
+
+
+def mapping(value):
+ return value if isinstance(value, dict) else {}
+
+
+def sequence(value):
+ return value if isinstance(value, (list, tuple)) else []
+
+
+def attempt_metrics(value):
+ """Retain completed work before retry artifacts move or a new worker starts."""
+ value = mapping(value)
+ result = {key: value[key] for key in ('config', 'device_memory', 'work_seconds', 'decode_phase',
+ 'sampling_reused', 'sampling_metrics_scope', 'resident_admission',
+ 'sample_finalize_phase', 'sampling_checkpoint_complete', 'sample_metrics_scope',
+ 'load_seconds', 'sample_seconds', 'latent_save_seconds', 'decode_save_seconds',
+ 'load_peak_allocated_bytes', 'load_peak_reserved_bytes', 'torch_peak_allocated_bytes',
+ 'torch_peak_reserved_bytes', 'final_stage_peak_allocated_bytes', 'final_stage_peak_reserved_bytes',
+ 'step_seconds', 'sampling_memory', 'sampling_passes', 'latent_upscale') if key in value}
+ if 'failure_cleanup' in value:
+ result['failure_cleanup'] = failure_cleanup(value['failure_cleanup'])
+ if 'decoder_read_ahead' in value:
+ result['decoder_read_ahead'] = decoder_read_ahead(value.get('decoder_read_ahead'))
+ decoded = decoder_measurements(value)
+ if decoded:
+ result['decoder_measurements'] = decoded
+ kernels = kernel_receipt(value)
+ if kernels:
+ result['kernels'] = kernels
+ result['completed_steps'] = len(sequence(mapping(value.get('sampling_memory')).get('steps')))
+ return result
+
+
+def decoder_measurements(value):
+ """Observed VAE work/placement, never checkpoint paths or tensor contents."""
+ value = mapping(value)
+ result = numbers(value, ('vae_load_seconds', 'video_decode_seconds',
+ 'audio_load_decode_seconds', 'video_postprocess_seconds', 'encode_seconds',
+ 'decoded_artifact_save_seconds', 'decode_save_seconds', 'vae_resident_blocks',
+ 'vae_pinned_weight_bytes'))
+ for key in ('vae_linear_compute_cache', 'resident_vae_cache_hit', 'resident_audio_vae_cache_hit',
+ 'streamed_video_output', 'streamed_vae_weights'):
+ if type(value.get(key)) is bool:
+ result[key] = value[key]
+ prepared = numbers(value.get('vae_compute_cache_preparation'),
+ ('linear_parameter_count', 'prepared_parameter_bytes',
+ 'streamed_parameter_count', 'streamed_parameter_bytes'))
+ if prepared:
+ result['vae_compute_cache_preparation'] = prepared
+ transfers = weight_placement(value.get('vae_offload'))
+ transfers.update(numbers(value.get('vae_offload'), ('slots',)))
+ if transfers:
+ result['vae_offload'] = transfers
+ return result
+
+
+def failure_cleanup(value):
+ value = mapping(value)
+ result = numbers(value, ('frames_cleared',))
+ if type(value.get('complete')) is bool:
+ result['complete'] = value['complete']
+ for key in ('before', 'after'):
+ if isinstance(value.get(key), dict):
+ result[key] = gpu_snapshot(value[key])
+ if value.get('errors'):
+ result['errors'] = [trace(row) for row in sequence(value['errors'])[:2]]
+ return result
+
+
+def decoder_read_ahead(value):
+ """Keep bounded decoder overlap evidence without paths or free-form text."""
+ value = mapping(value)
+ result = numbers(value, ('read_bytes', 'total_bytes', 'allowance_bytes',
+ 'elapsed_seconds', 'private_buffer_bytes',
+ 'gpu_allocation_bytes'))
+ state = value.get('state')
+ if state in ('not-started', 'reading', 'ready', 'skipped', 'stopped', 'failed'):
+ result['state'] = state
+ sampling = mapping(value.get('sampling'))
+ if sampling:
+ result['sampling'] = numbers(sampling, ('trigger_step', 'steps_remaining',
+ 'estimated_remaining_seconds',
+ 'trigger_elapsed_seconds'))
+ return result
+
+
+def kernel_receipt(value):
+ """Keep backend call counts and parallel execution counters only."""
+ value = mapping(value)
+ result = {}
+ calls = mapping(value.get('attention_backend_calls'))
+ if calls:
+ result['attention_backend_calls'] = {
+ key: value for key, value in calls.items()
+ if key in ATTENTION_BACKENDS and type(value) is int and value >= 0
+ }
+ fp8 = mapping(value.get('fp8_kernel_calls'))
+ if fp8:
+ result['fp8_kernel_calls'] = {
+ key: count for key, count in fp8.items()
+ if key in ('torch_scaled_mm', 'cublas_fp8', 'rescale', 'triton_ff_up')
+ and type(count) is int and count >= 0
+ }
+ execution = mapping(value.get('head_execution'))
+ if execution:
+ item = numbers(execution, ('requested_parallelism', 'parallel_head_calls', 'parallel_head_warmups'))
+ if item:
+ result['head_execution'] = item
+ return result
+
+
+def numbers(value, keys):
+ value = mapping(value)
+ return {k: value[k] for k in keys if type(value.get(k)) in (int, float)
+ and math.isfinite(value[k]) and value[k] >= 0}
+
+
+def weight_placement(value):
+ value = mapping(value)
+ result = numbers(value, WEIGHT_PLACEMENT)
+ if type(value.get('host_prefetch')) is bool:
+ result['host_prefetch'] = value['host_prefetch']
+ reason = value.get('host_prefetch_disabled_reason')
+ if reason in ('host_headroom', 'host_allocation_refused', 'live_host_pressure'):
+ result['host_prefetch_disabled_reason'] = reason
+ return result
+
+
+def encoder_discard(value):
+ """Report only the observed discard mode, never native decision text."""
+ modes = [row['encoder_discarded_without_cpu_copy']
+ for raw in sequence(mapping(value).get('resident_admission'))
+ for row in (mapping(raw),) if row.get('action') == 'evict' and row.get('role') == 'encoder'
+ and type(row.get('encoder_discarded_without_cpu_copy')) is bool]
+ return {'encoder_discarded_without_cpu_copy': all(modes)} if modes else {}
+
+
+def sampling_checkpoint(value):
+ """Keep completion and cleanup evidence without paths or free-form text."""
+ value = mapping(value)
+ result = {}
+ if value.get('sample_finalize_phase') in SAMPLE_FINALIZE_PHASES:
+ result['phase'] = value['sample_finalize_phase']
+ if type(value.get('sampling_checkpoint_complete')) is bool:
+ result['complete'] = value['sampling_checkpoint_complete']
+ if value.get('sample_metrics_scope') == 'Completed sampling before offloader cleanup; finalization is excluded.':
+ result['finalization_excluded'] = True
+ return {'sampling_checkpoint': result} if result else {}
+
+
+def name(value):
+ return value if isinstance(value, str) and re.fullmatch(r'[A-Za-z0-9_.-]{1,96}', value) else None
+
+
+def exception_details(error):
+ """Capture locations and bounded tensor metadata, never source or values."""
+ chain, seen = [], set()
+ while isinstance(error, BaseException) and id(error) not in seen and len(chain) < 4:
+ seen.add(id(error))
+ frames, tb = [], error.__traceback__
+ while tb is not None:
+ code = tb.tb_frame.f_code
+ frames.append(dict(file=code.co_filename.replace('\\', '/').rsplit('/', 1)[-1],
+ module=tb.tb_frame.f_globals.get('__name__'), function=code.co_name, line=tb.tb_lineno,
+ tensor_metadata=_frame_tensors(tb.tb_frame)))
+ tb = tb.tb_next
+ tensor_frames = [frame for frame in frames if frame.get('tensor_metadata')]
+ for frame in tensor_frames[:-3]:
+ frame.pop('tensor_metadata', None)
+ chain.append(dict(exception_type=type(error).__name__, frames=frames[-32:], **error_details(error),
+ **numbers({'errno': getattr(error, 'errno', None),
+ 'winerror': getattr(error, 'winerror', None)}, ('errno', 'winerror'))))
+ error = error.__cause__ or (None if error.__suppress_context__ else error.__context__)
+ return [trace(row) for row in chain]
+
+
+def trace(value):
+ value = mapping(value)
+ result = numbers(value, ('errno', 'winerror', 'error_code', 'requested_allocation_bytes'))
+ if value.get('category') in ERROR_CATEGORIES:
+ result['category'] = value['category']
+ if value.get('category') == 'checkpoint_changed':
+ result['checkpoint_change'] = checkpoint_change(value.get('checkpoint_change'))
+ if name(value.get('exception_type')):
+ result['exception_type'] = value['exception_type']
+ result['frames'] = []
+ for frame in sequence(value.get('frames'))[-24:]:
+ frame = mapping(frame)
+ item = numbers(frame, ('line',))
+ for key in ('file', 'module', 'function'):
+ if name(frame.get(key)):
+ item[key] = frame[key]
+ tensors = tensor_metadata(frame.get('tensor_metadata'))
+ if tensors:
+ item['tensor_metadata'] = tensors
+ if 'line' in item and ('file' in item or 'module' in item):
+ result['frames'].append(item)
+ tensor_frames = [frame for frame in result['frames'] if frame.get('tensor_metadata')]
+ for frame in tensor_frames[:-3]:
+ frame.pop('tensor_metadata', None)
+ return result
+
+
+def gpu_snapshot(value):
+ from .encoder_memory import GPU_FIELDS
+ result = numbers(value, GPU_FIELDS)
+ errors = mapping(mapping(value).get('collection_errors'))
+ clean = {}
+ for section in ('cuda_memory', 'allocator_stats', 'memory_fraction', 'host_pinning',
+ 'native_placement', 'windows_memory'):
+ rows = sequence(errors.get(section))
+ if not rows:
+ continue
+ item = trace(rows[0])
+ # Optional counter failures need their error code and location, not
+ # repeated activation metadata from the primary model exception.
+ item['frames'] = [{key: val for key, val in frame.items() if key != 'tensor_metadata'}
+ for frame in item['frames'][-3:]]
+ clean[section] = [item]
+ if clean:
+ result['collection_errors'] = clean
+ return result
+
+
+def guard(value):
+ value = mapping(value)
+ result = numbers(value, GUARD)
+ result['reasons'] = [r for r in sequence(value.get('reasons'))[:4]
+ if r in ('working_memory_budget', 'system_or_commit_pressure')]
+ if type(value.get('budget_is_estimate')) is bool:
+ result['budget_is_estimate'] = value['budget_is_estimate']
+ return result
+
+
+def resources(value):
+ value = mapping(value)
+ result = dict(ram=numbers(value.get('ram'), RAM_PEAKS), ram_guard=guard(value.get('ram_guard')))
+ ram = mapping(value.get('ram'))
+ for key in ('process_tree_guard_complete', 'process_tree_pss_complete', 'process_tree_io_complete'):
+ if type(ram.get(key)) is bool:
+ result['ram'][key] = ram[key]
+ if isinstance(value.get('ram_budget_warning'), dict):
+ result['ram_budget_warning'] = guard(value['ram_budget_warning'])
+ for key in ('ram_start', 'ram_last'):
+ result[key] = numbers(value.get(key), RAM_SAMPLE)
+ gpu = mapping(value.get('gpu'))
+ result['gpu'] = numbers(gpu, ('gpu_peak_bytes', 'gpu_baseline_bytes', 'gpu_last_bytes', 'samples',
+ 'max_temperature_c', 'max_power_mw', 'busy_util_threshold_percent', 'sampling_interval_seconds'))
+ # Error strings can contain paths; preserve a count, not their text.
+ if isinstance(gpu.get('sampling_errors'), list):
+ result['gpu']['sampling_error_count'] = len(gpu['sampling_errors'])
+ from .monitoring import Activity
+ for key in ('activity', 'busy_activity'):
+ if not isinstance(gpu.get(key), dict):
+ continue
+ observed = gpu[key]
+ clean = numbers(observed, ('samples', 'clock_reason_samples', 'clock_reason_mask'))
+ for field in Activity.fields:
+ if isinstance(observed.get(field), dict):
+ clean[field] = numbers(observed[field], ('samples', 'minimum', 'maximum', 'mean'))
+ if isinstance(observed.get('clock_reason_counts'), dict):
+ clean['clock_reason_counts'] = numbers(observed['clock_reason_counts'], tuple(Activity.clock_reasons))
+ result['gpu'][key] = clean
+ return result
+
+
+WINDOWS_BUDGET = ('peak_usage_bytes', 'minimum_budget_bytes', 'maximum_budget_bytes',
+ 'first_budget_bytes', 'last_budget_bytes', 'budget_changes', 'over_budget_samples', 'samples')
+
+
+def dynamic_budget(value):
+ value = mapping(value)
+ result = numbers(value, ('base_reserve_bytes', 'maximum_allocator_bytes', 'minimum_limit_bytes',
+ 'maximum_limit_bytes', 'observations'))
+ if type(value.get('enabled')) is bool:
+ result['enabled'] = value['enabled']
+ def clean(row):
+ row = mapping(row)
+ item = numbers(row, ('elapsed_seconds', 'previous_limit_bytes', 'candidate_limit_bytes',
+ 'allocator_limit_bytes', 'live_free_bytes', 'reserved_bytes', 'non_torch_local_bytes', 'driver_budget_bytes'))
+ for key, allowed in (('stage', ('load', 'sample', 'upscale', 'decode')),
+ ('action', ('hold', 'pending', 'shrink', 'grow'))):
+ if row.get(key) in allowed:
+ item[key] = row[key]
+ if type(row.get('driver_available')) is bool:
+ item['driver_available'] = row['driver_available']
+ return item
+ result['last'] = clean(value.get('last'))
+ result['transitions'] = [clean(row) for row in sequence(value.get('transitions'))[:64]]
+ return result
+
+
+def allocator(value):
+ value = mapping(value)
+ result = numbers(value, ('budget_bytes', 'device_total_bytes', 'effective_allocator_limit_bytes'))
+ for key in ('enforced', 'windows_allocator_limit_enforced', 'benchmark_allocator_limit_enforced',
+ 'capacity_trial_limit_enforced'):
+ if type(value.get(key)) is bool:
+ result[key] = value[key]
+ decision = mapping(value.get('admission'))
+ result['admission'] = numbers(decision, ('allocator_limit_bytes', 'observed_non_torch_local_bytes',
+ 'owned_allocator_reserved_bytes', 'live_free_bytes', 'growth_reserve_bytes'))
+ result['admission']['bounds'] = numbers(decision.get('bounds'), ('planning_budget_bytes',
+ 'physical_capacity_bytes', 'live_pool_capacity_bytes', 'wddm_pool_capacity_bytes'))
+ observed = mapping(value.get('windows_memory'))
+ result['windows_memory'] = {segment: numbers(observed.get(segment), ('budget_bytes', 'usage_bytes'))
+ for segment in ('local', 'nonlocal')}
+ if value.get('dynamic_budget'):
+ result['dynamic_budget'] = dynamic_budget(value['dynamic_budget'])
+ return result
+
+
+def sampling(value):
+ from .compilation_diagnostics import sanitize
+ value = mapping(value)
+ result = dict(steps=[])
+ for row in sequence(value.get('steps'))[:10]:
+ row = mapping(row)
+ item = numbers(row, ('step', 'seconds', 'allocated_bytes', 'reserved_bytes',
+ 'cumulative_peak_allocated_bytes', 'cumulative_peak_reserved_bytes',
+ 'inactive_split_bytes', 'allocation_retries', 'allocator_ooms'))
+ item['host'] = numbers(row.get('host'), HOST_SAMPLE)
+ item['process_delta'] = numbers(row.get('process_delta'), PROCESS_DELTA)
+ item['compiler'] = sanitize(row.get('compiler'))
+ observed = mapping(row.get('windows'))
+ for segment in ('local', 'nonlocal'):
+ item[segment] = numbers(observed.get(segment),
+ WINDOWS_BUDGET)
+ result['steps'].append(item)
+ observed = mapping(value.get('incomplete_step_windows'))
+ result['incomplete_step'] = {segment: numbers(observed.get(segment),
+ WINDOWS_BUDGET)
+ for segment in ('local', 'nonlocal')}
+ result['incomplete_step_compiler'] = sanitize(value.get('incomplete_step_compiler'))
+ return result
+
+
+def sampling_passes(value):
+ """Per-canvas execution evidence with no paths, prompts or tensor values."""
+ result = []
+ for index, row in enumerate(sequence(value)[:2]):
+ row = mapping(row)
+ config = mapping(row.get('compute_configuration'))
+ item = dict(index=index + 1,
+ geometry=numbers(row.get('geometry'), ('width', 'height', 'frames', 'video_tokens',
+ 'reference_video_tokens', 'reference_audio_tokens')),
+ timing=numbers(row, ('sample_seconds', 'torch_peak_allocated_bytes', 'torch_peak_reserved_bytes')),
+ config=numbers(config, ENGINE_KNOBS), offload=weight_placement(row.get('offload')),
+ cache_admission=numbers(row.get('pass_cache_admission'), ('gpu_budget_bytes', 'admitted_bytes',
+ 'measured_peak_reserved_bytes', 'live_free_bytes', 'commit_available_bytes',
+ 'requested_resident_blocks', 'workspace_peak_bytes', 'peak_cache_bytes')),
+ kernels=kernel_receipt(row))
+ item['config'].update({key: config[key] for key in ENGINE_KNOBS if type(config.get(key)) is bool})
+ item['step_seconds'] = [duration for duration in sequence(row.get('step_seconds'))[:8]
+ if type(duration) in (int, float) and math.isfinite(duration) and duration >= 0]
+ result.append(item)
+ return result
+
+
+def latent_upscale(value):
+ value = mapping(value)
+ result = numbers(value, ('load_seconds', 'compute_seconds', 'stage_seconds', 'scale',
+ 'estimated_workspace_bytes', 'available_workspace_bytes', 'allocation_retries',
+ 'torch_peak_allocated_bytes', 'torch_peak_reserved_bytes'))
+ for key in ('transformer_reloaded', 'temporal_chunking', 'attempted_below_estimate', 'buffer_reuse'):
+ if type(value.get(key)) is bool:
+ result[key] = value[key]
+ failures = []
+ for row in sequence(value.get('allocation_failures'))[:2]:
+ row = mapping(row)
+ if row.get('category') in ERROR_CATEGORIES:
+ failures.append(dict(category=row['category'],
+ **numbers(row, ('error_code', 'requested_allocation_bytes'))))
+ if failures:
+ result['allocation_failures'] = failures
+ return result
+
+
+def failure(value):
+ value = mapping(value)
+ result = dict(ram_guard=guard(value.get('ram_guard')))
+ if isinstance(value.get('gpu'), dict):
+ result['gpu'] = gpu_snapshot(value['gpu'])
+ if isinstance(value.get('memory'), dict):
+ result['memory'] = numbers(value['memory'], RAM_SAMPLE)
+ if isinstance(value.get('memory_collection_error'), list):
+ result['memory_collection_error'] = [trace(row) for row in value['memory_collection_error'][:4]]
+ result['allocation'] = numbers(value.get('allocation'), ('fp8_ff_activation_stash_bytes',))
+ if value.get('kind') in ('gpu_oom', 'ram_pressure', 'cuda_error', 'timeout', 'cancelled',
+ 'code_error', 'unknown_worker_exit', 'worker_stop_unconfirmed'):
+ result['kind'] = value['kind']
+ for key in ('exception', 'controller_exception'):
+ result[key] = [trace(row) for row in sequence(value.get(key))[:4]]
+ return result
+
+
+def planning(rows):
+ result = []
+ for row in sequence(rows)[:8]:
+ row = mapping(row)
+ item = {}
+ for key in ('stage', 'status'):
+ if name(row.get(key)):
+ item[key] = row[key]
+ if type(row.get('capacity_trial')) is bool:
+ item['capacity_trial'] = row['capacity_trial']
+ for key in ('raw', 'adjusted'):
+ item[key] = numbers(row.get(key), HARDWARE_MEMORY)
+ item['system_memory'] = numbers(row.get('system_memory'),
+ ('total_bytes', 'available_bytes', 'physical_available_bytes', 'commit_available_bytes'))
+ for key in ('budget_bytes', 'reserve_bytes'):
+ item[key] = numbers(row.get(key), ('gpu', 'ram'))
+ item['resource_error'] = numbers(row.get('resource_error'), tuple(resource + '_' + field
+ for resource in ('gpu', 'ram') for field in
+ ('capacity_bytes', 'available_bytes', 'reserve_bytes', 'budget_bytes', 'minimum_bytes')))
+ idle = mapping(row.get('idle_cache'))
+ item['idle_cache'] = dict(present=bool(idle), **numbers(idle, ('reclaimable_gpu_bytes',
+ 'reclaimable_ram_bytes', 'retained_pinned_ram_bytes')))
+ item['idle_cache']['memory'] = numbers(idle.get('memory'), RAM_SAMPLE)
+ item['idle_cache']['gpu_capacity'] = numbers(idle.get('gpu_capacity'),
+ ('allocator_limit_bytes', 'owned_allocator_reserved_bytes', 'live_free_bytes',
+ 'observed_non_torch_local_bytes', 'growth_reserve_bytes'))
+ item['idle_cache']['accounting'] = numbers(idle.get('ram_accounting'),
+ ('raw_available_bytes', 'credited_available_bytes', 'pinned_model_bytes',
+ 'reused_encoder_working_bytes'))
+ result.append(item)
+ return result
+
+
+def attempts(rows):
+ result = []
+ rows = sequence(rows)
+ indices = list(range(len(rows))) if len(rows) <= 3 else [0, 1, len(rows)-1]
+ for index in indices:
+ row = rows[index]
+ row = mapping(row)
+ profile, metrics = mapping(row.get('profile')), mapping(row.get('metrics'))
+ item = dict(index=index + 1, failure=failure(row.get('failure')), resources=resources(row.get('resources')))
+ item['resources'].update(encoder_discard(metrics))
+ item['resources'].update(sampling_checkpoint(metrics))
+ if 'failure_cleanup' in metrics:
+ item['failure_cleanup'] = failure_cleanup(metrics['failure_cleanup'])
+ for key in ('state', 'phase'):
+ if name(row.get(key)):
+ item[key] = row[key]
+ item['budgets'] = {key: val*1e9 for key, field in (('gpu', 'gpu_budget_gb'), ('ram', 'inference_ram_budget_gb'))
+ for val in numbers(profile, (field,)).values() if math.isfinite(val*1e9)}
+ config = mapping(metrics.get('config')) or mapping(profile.get('engine'))
+ item['config'] = numbers(config, ENGINE_KNOBS)
+ item['config'].update({k: config[k] for k in ENGINE_KNOBS if type(config.get(k)) is bool})
+ item['timing'] = numbers(metrics, ('work_seconds', 'load_seconds', 'sample_seconds',
+ 'latent_save_seconds', 'decode_save_seconds'))
+ if 'seconds' in numbers(row, ('seconds',)):
+ item['timing']['controller_seconds'] = row['seconds']
+ item['memory'] = numbers(metrics, ('load_peak_allocated_bytes', 'load_peak_reserved_bytes',
+ 'torch_peak_allocated_bytes', 'torch_peak_reserved_bytes',
+ 'final_stage_peak_allocated_bytes', 'final_stage_peak_reserved_bytes'))
+ item['allocator'] = allocator(metrics.get('device_memory'))
+ item['sampling'] = sampling(metrics.get('sampling_memory'))
+ item['sampling_passes'] = sampling_passes(metrics.get('sampling_passes'))
+ item['latent_upscale'] = latent_upscale(metrics.get('latent_upscale'))
+ kernels = kernel_receipt(metrics)
+ if kernels:
+ item['kernels'] = kernels
+ item['completed_nfe'] = max(len(sequence(metrics.get('step_seconds'))), numbers(metrics, ('completed_steps',)).get('completed_steps', 0))
+ if type(metrics.get('sampling_reused')) is bool:
+ item['sampling_reused'] = metrics['sampling_reused']
+ decoder = mapping(profile.get('decoder'))
+ item['decoder'] = numbers(decoder, DECODER_KNOBS)
+ item['decoder'].update({key: decoder[key] for key in DECODER_KNOBS if type(decoder.get(key)) is bool})
+ if metrics.get('decode_phase') in DECODE_PHASES:
+ item['decoder']['phase'] = metrics['decode_phase']
+ decoded = decoder_measurements(metrics.get('decoder_measurements') or metrics)
+ if decoded:
+ item['decoder']['measurements'] = decoded
+ recovery = mapping(row.get('recovery'))
+ if recovery:
+ following = mapping(recovery.get('next_profile'))
+ item['recovery'] = numbers(recovery, ('attempt', 'max_attempts'))
+ if type(recovery.get('reuse_sampling')) is bool:
+ item['recovery']['reuse_sampling'] = recovery['reuse_sampling']
+ if recovery.get('failed_phase') in ('load', 'sample', 'sampling', 'sample_finalize', 'decode', 'save', 'complete'):
+ item['recovery']['phase'] = recovery['failed_phase']
+ for section, keys in (('engine', ENGINE_KNOBS), ('decoder', DECODER_KNOBS)):
+ before, after = mapping(profile.get(section)), mapping(following.get(section))
+ planned = numbers(after, keys)
+ planned.update({key: after[key] for key in keys if type(after.get(key)) is bool})
+ changes = {}
+ for key, value in planned.items():
+ old = before.get(key)
+ if value != old and (type(old) is bool or numbers({key: old}, (key,))):
+ changes[key] = dict(before=old, after=value)
+ item['recovery']['next_' + section] = planned
+ item['recovery'][section + '_changes'] = changes
+ item['recovery']['next_budgets'] = {key: val * 1e9
+ for key, field in (('gpu', 'gpu_budget_gb'), ('ram', 'inference_ram_budget_gb'))
+ for val in numbers(following, (field,)).values() if math.isfinite(val * 1e9)}
+ result.append(item)
+ return result
diff --git a/freevideo_engine/diagnostic_summary.py b/freevideo_engine/diagnostic_summary.py
new file mode 100644
index 0000000..c531c6e
--- /dev/null
+++ b/freevideo_engine/diagnostic_summary.py
@@ -0,0 +1,324 @@
+"""Compact local report formatting; no network access or background work."""
+import hashlib
+import json
+import math
+from pathlib import Path
+import re
+
+from . import __version__
+
+
+def _number(value):
+ return value if type(value) in (int, float) and math.isfinite(value) and value >= 0 else None
+
+
+def _measurements(value, names):
+ value = value if isinstance(value, dict) else {}
+ return {k:v for k in names if (v := _number(value.get(k))) is not None}
+
+
+def _label(value):
+ return value if isinstance(value, str) and re.fullmatch(r'[A-Za-z0-9 _().+-]{1,96}', value) else None
+
+
+def summary_report(report):
+ """Fresh structure, not a redacted copy of arbitrary diagnostic text."""
+ from . import diagnostic_resources as diagnostic
+ from .compilation_diagnostics import sanitize as compiler_summary
+ summary = report.get('summary') or {}
+ engine = report.get('engine') or {}
+ hardware = summary.get('hardware') or {}
+ config = summary.get('effective_engine_config') or {}
+ result = dict(kind='generation', status=summary.get('status') if summary.get('status') in
+ ('complete', 'retrying') else 'incomplete',
+ version=__version__, hardware={}, config={}, geometry={}, memory={}, stages=[], steps=[], errors=[])
+ for k in ('gpu_name', 'system', 'architecture', 'driver_version', 'torch_version', 'cuda_version'):
+ if _label(hardware.get(k)):
+ result['hardware'][k] = hardware[k]
+ capability = hardware.get('capability')
+ if isinstance(capability, (tuple,list)) and len(capability)==2 and all(type(x) is int and x>=0 for x in capability):
+ result['hardware']['capability'] = list(capability)
+ result['hardware'].update(_measurements(hardware, ('vram_total','vram_free','ram_total','ram_available')))
+ result['offload'] = diagnostic.weight_placement(summary.get('weight_placement'))
+ result['sampling_passes'] = diagnostic.sampling_passes(summary.get('sampling_passes') or engine.get('sampling_passes'))
+ result['latent_upscale'] = diagnostic.latent_upscale(summary.get('latent_upscale') or engine.get('latent_upscale'))
+ decoder = summary.get('decoder_options') or {}
+ result['decoder'] = _measurements(decoder, ('resident_blocks',))
+ for key in ('offload','prefetch','stream_weights','stream_output','pin_weights','preload','tile_group',
+ 'linear_compute_cache'):
+ if type(decoder.get(key)) is bool:
+ result['decoder'][key] = decoder[key]
+ if engine.get('decode_phase') in diagnostic.DECODE_PHASES:
+ result['decoder']['phase'] = engine['decode_phase']
+ decoded = diagnostic.decoder_measurements(engine)
+ if decoded:
+ result['decoder']['measurements'] = decoded
+ result['decoder_read_ahead'] = diagnostic.decoder_read_ahead(summary.get('decoder_read_ahead'))
+ kernels = diagnostic.kernel_receipt(summary.get('kernels') or engine)
+ if kernels:
+ result['kernels'] = kernels
+ for key in ('conditioning_cache_hit','preencoded'):
+ if type(summary.get(key)) is bool:
+ result[key] = summary[key]
+ result['geometry'] = _measurements(summary.get('geometry'), ('width','height','frames','fps','video_tokens','reference_video_tokens','reference_audio_tokens'))
+ shape = engine.get('conditioning_shape')
+ if isinstance(shape, (list, tuple)) and len(shape) == 2 and all(type(x) is int and x > 0 for x in shape):
+ result['geometry']['text_tokens'] = shape[0]
+ for k in ('resident_blocks','pin_host_gb','head_chunk','window_batch','head_parallelism','ff_chunk','projection_chunk',
+ 'query_chunk','fp8_linears','resident_weight_bytes','pinned_model_bytes','pinned_host_allocated_bytes','steps'):
+ if _number(config.get(k)) is not None:
+ result['config'][k] = config[k]
+ for k in ('prefetch','stream_weights','attention_cpu_outputs','grouped_attention_outputs','residual_offload','fp8_ff_recompute',
+ 'inference_kernels','window_varlen','varlen_smooth_k','reuse_block_outputs','preload_host','pin_host_weights','cache_refined_text'):
+ if type(config.get(k)) is bool:
+ result['config'][k] = config[k]
+ for k, allowed in (('task', ('t2va','fl2va','ref2va')), ('linear_compute',('native-fp8','bf16-weight-only')),
+ ('adaln_mode', ('portable-model-asset','local-precompute','original-projections'))):
+ if config.get(k) in allowed:
+ result['config'][k] = config[k]
+ for k, allowed in (('fp8_gemm', ('torch','scaled-mm-epilogue','triton')),
+ ('fp8_scale_granularity', ('per_tensor','rowwise')), ('precision', ('fp8','bf16','fp16'))):
+ if config.get(k) in allowed:
+ result['config'][k] = config[k]
+ attention = config.get('attention', '')
+ if isinstance(attention, str) and all(x in ('sage2','cudnn','torch-flash','fa2','fa4','dense') for x in attention.split('/')):
+ result['config']['attention'] = attention
+ if isinstance(report.get('runtime_code'), dict):
+ result['build_id'] = hashlib.sha256(json.dumps(report['runtime_code'],sort_keys=True).encode()).hexdigest()
+ result.update(_measurements(summary, ('request_seconds','seconds_per_completed_nfe')))
+ for row in (summary.get('stages') or [])[:16]:
+ if row.get('stage') in ('text_encoding','model_load','sampling','latent_save','decode_save','other_or_unmeasured','installation'):
+ result['stages'].append(dict(stage=row['stage'], **_measurements(row, ('seconds',))))
+ if type(row.get('reused_from_attempt')) is bool:
+ result['stages'][-1]['reused_from_attempt'] = row['reused_from_attempt']
+ result['step_seconds'] = [x for x in (summary.get('step_seconds') or [])[:128] if _number(x) is not None]
+ result['memory'] = _measurements(summary.get('observed_memory'), (
+ 'sampling_allocated_bytes','sampling_reserved_bytes','final_stage_allocated_bytes','final_stage_reserved_bytes',
+ 'whole_gpu_peak_bytes','working_ram_peak_bytes','rss_peak_bytes','pss_peak_bytes','private_commit_peak_bytes'))
+ for phase in ('video_ram', 'encoding_ram'):
+ result['memory'][phase] = _measurements((summary.get('observed_memory') or {}).get(phase), (
+ 'process_tree_peak_guard_bytes','process_tree_peak_rss_bytes','process_tree_peak_pss_bytes',
+ 'process_tree_peak_private_commit_bytes','system_min_available_bytes',
+ 'system_min_physical_available_bytes','system_min_commit_available_bytes'))
+ result['budgets'] = _measurements(summary.get('budget_bytes'), ('gpu','ram'))
+ device = summary.get('device_memory') or engine.get('device_memory') or {}
+ result['allocator'] = _measurements(device, ('budget_bytes','device_total_bytes','effective_allocator_limit_bytes'))
+ if type(device.get('windows_allocator_limit_enforced')) is bool:
+ result['allocator']['windows_allocator_limit_enforced'] = device['windows_allocator_limit_enforced']
+ if device.get('dynamic_budget'):
+ result['allocator']['dynamic_budget'] = diagnostic.dynamic_budget(device['dynamic_budget'])
+ for row in ((summary.get('sampling_memory') or {}).get('steps') or [])[:128]:
+ item = _measurements(row, ('step','seconds','elapsed_seconds','allocated_bytes','reserved_bytes','cumulative_peak_allocated_bytes',
+ 'cumulative_peak_reserved_bytes','inactive_split_bytes','allocation_retries','allocator_ooms'))
+ item['host'] = diagnostic.numbers(row.get('host'), diagnostic.HOST_SAMPLE)
+ item['process_delta'] = diagnostic.numbers(row.get('process_delta'), diagnostic.PROCESS_DELTA)
+ item['compiler'] = compiler_summary(row.get('compiler'))
+ for segment in ('local','nonlocal'):
+ item[segment] = _measurements((row.get('windows') or {}).get(segment),
+ diagnostic.WINDOWS_BUDGET)
+ result['steps'].append(item)
+ sampling = summary.get('sampling_memory') or {}
+ if sampling.get('windows_status') in ('observing','unavailable','not-applicable','reader-still-stopping'):
+ result['windows_status'] = sampling['windows_status']
+ result['incomplete_step_memory'] = dict(compiler=compiler_summary(sampling.get('incomplete_step_compiler')))
+ for segment in ('local','nonlocal'):
+ result['incomplete_step_memory'][segment] = _measurements((sampling.get('incomplete_step_windows') or {}).get(segment),
+ diagnostic.WINDOWS_BUDGET)
+ failures = [engine.get('failure') or {}] + [r.get('failure') or {} for r in (summary.get('failed_attempts') or [])[:3]]
+ for row in failures:
+ kind = row.get('kind')
+ if kind in ('gpu_oom','ram_pressure','cuda_error','timeout','cancelled','code_error','unknown_worker_exit'):
+ result['errors'].append(dict(kind=kind, ram_guard=_measurements(row.get('ram_guard'),
+ ('working_bytes','budget_bytes','available_bytes','emergency_floor_bytes'))))
+ # Frame names from our own source help locate failures without retaining
+ # arbitrary exception messages (which may contain a prompt or access key).
+ known = {p.name for p in Path(__file__).parent.glob('*.py')}
+ frames = []
+ for text in (report.get('log_tails') or {}).values():
+ if not isinstance(text, str):
+ continue
+ for filename, line, function in re.findall(r'freevideo_engine[\\/]([a-z_]+\.py)"?, line (\d+), in ([A-Za-z_0-9]+)', text[-8192:]):
+ if filename in known:
+ frames.append(dict(file=filename, line=int(line), function=function[:64]))
+ result['stack_frames'] = frames[-24:]
+ request = diagnostic.mapping(report.get('request'))
+ result['resource_planning'] = diagnostic.planning(request.get('resource_planning'))
+ result['attempts'] = diagnostic.attempts(request.get('resource_attempts'))
+ result['exception'] = [diagnostic.trace(row) for row in diagnostic.sequence(request.get('exception'))[:4]]
+ result['encoder_failure'] = diagnostic.failure(diagnostic.mapping(request.get('encoding_failure')).get('failure'))
+ result['encoder_resources'] = (diagnostic.resources(diagnostic.mapping(report.get('memory')).get('encoding'))
+ if not summary.get('conditioning_cache_hit') and not summary.get('preencoded') else {})
+ previous_prewarm = diagnostic.encoder_prewarm(diagnostic.mapping(report.get('bridge')).get('encoder_prewarm'))
+ if previous_prewarm:
+ result['encoder_resources']['previous_prewarm'] = previous_prewarm
+ if not summary.get('conditioning_cache_hit') and not summary.get('preencoded'):
+ encoding = diagnostic.mapping(report.get('encoding'))
+ from .encoder_diagnostics import STAGES
+ result['encoder_resources']['timing'] = diagnostic.encoder_timing(encoding.get('timing'))
+ result['encoder_resources']['runtime'] = diagnostic.encoder_runtime(encoding.get('runtime'))
+ result['encoder_resources'].update({key: encoding[key] for key in
+ ('resident_encoder_cache_hit', 'resident_encoder_gpu_ready_at_start') if type(encoding.get(key)) is bool})
+ if encoding.get('phase') in STAGES:
+ result['encoder_resources']['phase'] = encoding['phase']
+ checkpoint = diagnostic.mapping(encoding.get('checkpoint'))
+ if checkpoint.get('state') in ('read_only_mmap', 'native_mmap', 'resident_reuse'):
+ result['encoder_resources']['checkpoint'] = dict(
+ diagnostic.numbers(checkpoint, ('bytes',)), state=checkpoint['state'])
+ result['encoder_resources']['load_stages'] = []
+ result['encoder_resources']['gpu'] = dict(result['encoder_resources'].get('gpu', {}),
+ **diagnostic.gpu_snapshot(encoding.get('gpu')))
+ result['encoder_resources']['token_summary'] = diagnostic.token_summary(encoding.get('token_summary'))
+ result['encoder_resources']['embedding'] = diagnostic.embedding(encoding.get('embedding'))
+ retry = diagnostic.mapping(encoding.get('diagnostic_retry'))
+ if type(retry.get('index')) is int and 1 <= retry['index'] <= 3:
+ result['encoder_resources']['retry'] = dict(index=retry['index'], final_attempt=retry.get('final_attempt') is True)
+ result['encoder_resources']['oom_attempts'] = []
+ for row in diagnostic.sequence(encoding.get('encoder_attempts'))[:3]:
+ row = diagnostic.mapping(row)
+ result['encoder_resources']['oom_attempts'].append(dict(
+ diagnostic.gpu_snapshot(row), **diagnostic.numbers(row, ('attempt',)),
+ exception=[diagnostic.trace(trace) for trace in diagnostic.sequence(row.get('exception'))[:4]],
+ embedding=diagnostic.embedding(row.get('embedding'))))
+ for row in diagnostic.sequence(encoding.get('load_stages'))[-48:]:
+ row = diagnostic.mapping(row)
+ if row.get('stage') in STAGES:
+ result['encoder_resources']['load_stages'].append(dict(
+ diagnostic.numbers(row, ('seconds', 'duration_seconds')), stage=row['stage'],
+ process_delta=diagnostic.numbers(row.get('process_delta'), diagnostic.PROCESS_DELTA),
+ memory=diagnostic.numbers(row.get('memory'), diagnostic.RAM_SAMPLE),
+ gpu=diagnostic.gpu_snapshot(row.get('gpu'))))
+ result['worker_resources'] = diagnostic.resources(diagnostic.mapping(report.get('memory')).get('video'))
+ result['worker_resources'].update(diagnostic.encoder_discard(engine))
+ result['worker_resources'].update(diagnostic.sampling_checkpoint(engine))
+ if 'failure_cleanup' in engine:
+ result['worker_resources']['failure_cleanup'] = diagnostic.failure_cleanup(engine['failure_cleanup'])
+ # Keep the driver activity next to the kernel/backend receipt so a
+ # performance review can correlate the actual backend and chunking with
+ # utilization, clocks, power and allocator evidence. This is whole-worker
+ # activity (sampling plus decode), never a claim that it is sampling-only.
+ if result.get('kernels') and result['worker_resources'].get('gpu'):
+ result['kernels']['gpu_activity'] = result['worker_resources']['gpu']
+ result['hardware_stage'] = 'request_start'
+ result['diagnostic_revision'] = 9
+ phase = (engine.get('phase') or diagnostic.mapping(request.get('encoding_failure')).get('phase')
+ or request.get('phase'))
+ if result['status'] != 'complete' and diagnostic.name(phase):
+ result['failure_stage'] = phase
+ if result['attempts']:
+ result['errors'] = [dict(attempt=row['index'], phase=row.get('phase', 'unknown'),
+ kind=row['failure']['kind'], ram_guard=row['failure']['ram_guard'])
+ for row in result['attempts'] if row['failure'].get('kind')]
+ peaks = [diagnostic.mapping(value.get('ram')) for value in
+ [result['encoder_resources'], result['worker_resources']] + [r['resources'] for r in result['attempts']]]
+ result['request_memory_peaks'] = {key: max(row[key] for row in peaks if key in row)
+ for key in diagnostic.RAM_PEAKS if key.startswith('process_tree_peak_') and any(key in row for row in peaks)}
+ result['coverage'] = dict(planning=bool(result['resource_planning']), attempts=bool(result['attempts']),
+ encoder_timing=bool(result['encoder_resources'].get('timing', {}).get('stage_seconds')),
+ encoder_memory=bool(result['encoder_resources'].get('ram')), worker_memory=bool(result['worker_resources'].get('ram')),
+ structured_exception=bool(result['exception']) or bool(result['encoder_failure'].get('exception'))
+ or any(row['failure'].get('exception') for row in result['attempts']))
+ result['coverage'].update(attempts_observed=len(diagnostic.sequence(request.get('resource_attempts'))),
+ attempts_retained=len(result['attempts']), planning_observed=len(diagnostic.sequence(request.get('resource_planning'))),
+ planning_retained=len(result['resource_planning']))
+ if type(engine.get('sampling_reused')) is bool:
+ result['coverage']['sampling_reused'] = engine['sampling_reused']
+ return result
+
+
+PLACEMENT_FIELDS = ('head_chunk', 'window_batch', 'resident_blocks', 'pin_host_gb',
+ 'ff_chunk', 'projection_chunk', 'prefetch', 'stream_weights',
+ 'attention_cpu_outputs', 'residual_offload', 'fp8_ff_recompute')
+CALIBRATION_MEASUREMENTS = ('sample_seconds', 'steady_seconds_per_step', 'load_seconds',
+ 'elapsed_seconds', 'torch_peak_reserved_bytes',
+ 'torch_peak_allocated_bytes', 'whole_gpu_peak_bytes',
+ 'host_peak_bytes', 'residual_staged_steps', 'attempt')
+
+
+def _placement(value):
+ from . import diagnostic_resources as diagnostic
+ value = diagnostic.mapping(value)
+ result = diagnostic.numbers(value, PLACEMENT_FIELDS)
+ result.update({key: value[key] for key in PLACEMENT_FIELDS if type(value.get(key)) is bool})
+ return result
+
+
+def _failure_kind(message):
+ # A message can quote the prompt or a credential, even when it contains
+ # only ordinary ASCII. Classify it without transmitting the original text.
+ from .adaptive import classify_failure
+ return classify_failure(str(message or ''))['kind']
+
+
+_CALIBRATION_QUESTIONS = ('baseline', 'attention_outputs', 'ff_recompute',
+ 'pinned_host_cache', 'window_batch', 'resident_blocks', 'residual_staging')
+_CALIBRATION_VARIANTS = ('as_planned', 'as_planned_again', 'changed', 'no_staging', 'with_staging')
+
+
+def calibration_report(report):
+ """Compact local placement comparison without prompts, paths or media."""
+ from . import diagnostic_resources as diagnostic
+ hardware = _measurements(report.get('hardware'), ('vram_total', 'vram_free', 'ram_total', 'ram_available'))
+ source = diagnostic.mapping(report.get('hardware'))
+ for name in ('gpu_name', 'system', 'architecture', 'torch_version', 'cuda_version', 'driver_version'):
+ # Keep absent or invalid labels unknown.
+ if _label(source.get(name)):
+ hardware[name] = source[name][:64]
+ if isinstance(source.get('capability'), (list, tuple)) and len(source['capability']) == 2:
+ hardware['capability'] = [_number(value) for value in source['capability']]
+ result = dict(hardware=hardware,
+ calibration_id=report.get('calibration_id') if re.fullmatch(r'[0-9a-f]{32}', str(report.get('calibration_id', ''))) else None,
+ questions=[row['name'] for row in (report.get('questions') or [])
+ if isinstance(row, dict) and row.get('name') in _CALIBRATION_QUESTIONS][:8],
+ geometry=_measurements(report.get('geometry'),
+ ('width', 'height', 'frames', 'video_tokens')),
+ plan=_placement(report.get('plan')),
+ budget_bytes=_number(report.get('budget_bytes')),
+ gpu_reserve_bytes=_number(report.get('gpu_reserve_bytes')),
+ status=report.get('status') if report.get('status') in ('complete', 'error') else 'incomplete',
+ diagnostic_revision=5, results=[], summary={})
+ if report.get('status') == 'error':
+ result['error'] = _failure_kind(report.get('error'))
+ result['failure_stage'] = 'encode' if str(report.get('error', '')).startswith('encode:') else 'calibration'
+ frames = []
+ for line in str(report.get('traceback') or '').splitlines():
+ found = re.search(r'freevideo_engine[\\/]([a-z_]+\.py)", line (\d+), in ([A-Za-z_0-9]+)', line)
+ if found:
+ frames.append(dict(file=found.group(1), line=int(found.group(2)),
+ function=found.group(3)[:64]))
+ result['traceback'] = frames[-8:]
+ for row in (report.get('results') or [])[:16]:
+ row = diagnostic.mapping(row)
+ entry = dict(_measurements(row, CALIBRATION_MEASUREMENTS),
+ question=row.get('question') if row.get('question') in _CALIBRATION_QUESTIONS else None,
+ variant=row.get('variant') if row.get('variant') in _CALIBRATION_VARIANTS else None,
+ status=row.get('status') if row.get('status') in ('complete', 'error') else 'incomplete')
+ if row.get('status') != 'complete':
+ entry['error'] = _failure_kind(row.get('error'))
+ if type(row.get('finite_latents')) is bool:
+ entry['finite_latents'] = row['finite_latents']
+ entry['overrides'] = _placement(row.get('overrides'))
+ entry['step_seconds'] = [x for x in (row.get('step_seconds') or [])[:32]
+ if _number(x) is not None]
+ result['results'].append(entry)
+ # summarise() keeps the baseline beside the questions, not inside them.
+ source = diagnostic.mapping(report.get('summary'))
+ variants = dict(diagnostic.mapping(source.get('questions')))
+ variants['baseline'] = diagnostic.mapping(source.get('baseline'))
+ for question, value in variants.items():
+ name = question
+ if name not in _CALIBRATION_QUESTIONS:
+ continue
+ value = diagnostic.mapping(value)
+ row = _measurements(value, ('runs', 'steady_seconds_per_step', 'drift',
+ 'torch_peak_reserved_bytes', 'whole_gpu_peak_bytes',
+ 'host_peak_bytes', 'faster_than_baseline'))
+ # Express signed memory changes as explicitly named savings or costs.
+ for field in ('host', 'reserved', 'whole_gpu'):
+ delta = value.get(field + '_delta_bytes')
+ if type(delta) in (int, float) and math.isfinite(delta):
+ row[field + ('_added_bytes' if delta > 0 else '_saved_bytes')] = abs(delta)
+ row['over_budget'] = value.get('over_budget') is True
+ result['summary'][name] = row
+ if isinstance(source.get('failed'), list):
+ result['summary']['failed_variants'] = dict(count=len(source['failed']))
+ return result
diff --git a/freevideo_engine/diagnostics.py b/freevideo_engine/diagnostics.py
new file mode 100644
index 0000000..604cf07
--- /dev/null
+++ b/freevideo_engine/diagnostics.py
@@ -0,0 +1,365 @@
+"""Collect bounded local support files, even when setup or CUDA cannot start.
+
+Python 3.9 stdlib only. Nothing is uploaded; original reports and media are kept.
+"""
+import argparse
+import hashlib
+import json
+import os
+from pathlib import Path
+import re
+import shutil
+import stat
+import subprocess
+import sys
+import time
+import zipfile
+import platform
+
+from . import __version__
+from .bootstrap import DEFAULT_ROOT
+from .system import windows, venv_python, system_memory, nvidia_smi, bootstrap_root
+
+
+FILE_LIMIT = 512 * 1024
+TOTAL_LIMIT = 12 * 1024 * 1024
+FILE_COUNT = 256
+NAMES = {'report.json', 'report.csv', 'inventory.json', 'case.json', 'status.json',
+ 'plan.json', 'machine.before.json', 'comparison.json', 'profile.json',
+ 'result.json', 'gpu.csv', 'ram.jsonl', 'network.jsonl', 'optimization.json', 'storage.json', 'kernel-capabilities.json'}
+SENSITIVE = re.compile(r'(?:^|[_-])(?:token|password|secret|credential|api[_-]?key)(?:$|[_-])', re.I)
+# Path-bearing fields are retained only as installation-relative names (when a
+# known report root is available) or as a basename under ``<PATH>``. Reports
+# are often collected from an installation whose model/config paths live
+# elsewhere, so replacing only the current installation root is insufficient.
+PATH_KEYS = frozenset(('base', 'checkpoint', 'encoder', 'output', 'directory',
+ 'retained', 'sample_csv', 'path', 'root', 'command'))
+ABSOLUTE_PATH = re.compile(
+ # The preceding character guard prevents URL slashes from being treated
+ # as local paths while still matching a drive path after prose such as
+ # ``at C:\\Users\\...`` or a quoted traceback location.
+ r'''(?<![A-Za-z0-9_:/])(?:(?:[A-Za-z]:[\\/])|(?:\\\\[^\\/\s"'<>]+[\\/])|/(?!/))[^"'<>;\r\n,\)\]}]*''')
+
+
+class Redactor:
+ def __init__(self, replacements=()):
+ self.replacements = sorted(((str(a), b) for a, b in replacements if len(str(a)) > 1),
+ key=lambda pair: len(pair[0]), reverse=True)
+ self.prompts = set()
+ self.secrets = set()
+
+ def structured(self, value):
+ if isinstance(value, dict):
+ result = {}
+ for key, item in value.items():
+ if key.lower() in ('prompt', 'negative_prompt', 'prompt_text'):
+ if isinstance(item, str) and item:
+ self.prompts.add(item)
+ result[key] = '<PROMPT REMOVED>'
+ elif SENSITIVE.search(key) or key.lower() in (
+ 'gpu_uuid', 'machine_id', 'device_uuid', 'hostname', 'username', 'computer_name'):
+ if isinstance(item, str) and len(item) >= 8:
+ self.secrets.add(item)
+ result[key] = '<REDACTED>'
+ elif self._path_key(key) and isinstance(item, str):
+ result[key] = self.path(item)
+ else:
+ result[key] = self.structured(item)
+ return result
+ if isinstance(value, list):
+ return [self.structured(item) for item in value]
+ return value
+
+ def text(self, value):
+ value = str(value)
+ for prompt in sorted(self.prompts, key=len, reverse=True):
+ value = value.replace(prompt, '<PROMPT REMOVED>')
+ for secret in sorted(self.secrets, key=len, reverse=True):
+ value = value.replace(secret, '<REDACTED>')
+ for source, target in self.replacements:
+ value = value.replace(source, target)
+ value = re.sub(r'(?i)(https?://)[^\s/@]+:[^\s/@]+@', r'\1<REDACTED>@', value)
+ value = re.sub(r'\bhf_[A-Za-z0-9_-]{8,}\b', '<REDACTED>', value)
+ value = re.sub(r'(?i)(Bearer\s+)[A-Za-z0-9._~+/-]+', r'\1<REDACTED>', value)
+ value = re.sub(r'(?i)(\b[\w-]{0,64}(?:token|password|secret|api[_-]?key)\s*[=:]\s*)[^\s,;&]+',
+ r'\1<REDACTED>', value)
+ # Keep diagnostics useful without exposing arbitrary user directories.
+ # The callback returns only a stable placeholder and the final path
+ # component, which is enough to identify a binary/config/log while
+ # removing usernames, drive letters, mounts and parent directories.
+ value = ABSOLUTE_PATH.sub(lambda match: self._path_placeholder(match.group(0)), value)
+ return value
+
+ @staticmethod
+ def _path_key(key):
+ lowered = str(key).lower()
+ return lowered in PATH_KEYS or lowered.endswith(('_path', '_paths', '_dir', '_root', '_report'))
+
+ @staticmethod
+ def _path_placeholder(value):
+ normalized = str(value).rstrip('\\/')
+ name = re.split(r'[\\/]', normalized)[-1]
+ return '<PATH>/' + name if name else '<PATH>'
+
+ def path(self, value):
+ """Return a slash-normalized relative path safe for a shared report."""
+ cleaned = self.text(value)
+ # ``text`` already handles arbitrary absolute paths. Keep known
+ # placeholders such as <RUN>/video.mp4 and ordinary relative paths.
+ return cleaned.replace('\\', '/')
+
+ def data(self, raw, suffix):
+ # Windows PowerShell 5.1 redirects native output as UTF-16 by default.
+ encoding = 'utf-16' if raw.startswith((b'\xff\xfe', b'\xfe\xff')) else 'utf-8-sig'
+ value = raw.decode(encoding, errors='replace')
+ def clean(item):
+ if isinstance(item, dict):
+ return {self.text(key): (self.path(v) if self._path_key(key) and isinstance(v, str)
+ else clean(v)) for key, v in item.items()}
+ if isinstance(item, list):
+ return [clean(v) for v in item]
+ return self.text(item) if isinstance(item, str) else item
+ if suffix in ('.json', '.jsonl'):
+ try:
+ if suffix == '.json':
+ value = json.dumps(clean(self.structured(json.loads(value))), ensure_ascii=False, indent=2) + '\n'
+ else:
+ rows = [self.structured(json.loads(line)) for line in value.splitlines()]
+ value = '\n'.join(json.dumps(clean(row)) for row in rows) + '\n'
+ return value.encode('utf-8')
+ except (ValueError, TypeError):
+ # Keep corrupt failure reports readable; do not discard the evidence.
+ value = re.sub(r'(?i)("(?:negative_)?prompt"\s*:\s*)"(?:[^"\\]|\\.)*"',
+ r'\1"<PROMPT REMOVED>"', value)
+ return self.text(value).encode('utf-8')
+
+
+def read_bounded(path, limit=FILE_LIMIT):
+ if windows():
+ from .win32 import open_regular
+ descriptor = open_regular(path)
+ else:
+ descriptor = os.open(path, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK)
+ with os.fdopen(descriptor, 'rb') as stream:
+ info = os.fstat(stream.fileno())
+ if not stat.S_ISREG(info.st_mode):
+ raise ValueError('Not a regular file')
+ if info.st_size <= limit:
+ return stream.read(limit), False
+ # Logs/telemetry retain both startup and the final failure, with a visible gap.
+ head = stream.read(limit // 2)
+ stream.seek(max(limit // 2, info.st_size - limit // 2))
+ return head + b'\n<TRUNCATED: middle omitted>\n' + stream.read(limit // 2), True
+
+
+def read_complete(path):
+ """Read one regular diagnostic file in full for an explicit user export."""
+ if windows():
+ from .win32 import open_regular
+ descriptor = open_regular(path)
+ else:
+ descriptor = os.open(path, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK)
+ with os.fdopen(descriptor, 'rb') as stream:
+ info = os.fstat(stream.fileno())
+ if not stat.S_ISREG(info.st_mode):
+ raise ValueError('Not a regular file')
+ return stream.read(), False
+
+
+def is_link(path):
+ try:
+ info = path.lstat()
+ return stat.S_ISLNK(info.st_mode) or bool(getattr(info, 'st_file_attributes', 0) & 0x400)
+ except OSError:
+ return True
+
+
+def report_files(directory):
+ for current, directories, files in os.walk(directory, followlinks=False):
+ directories[:] = sorted(name for name in directories
+ if not name.startswith('.') and not name.endswith('.artifacts')
+ and not is_link(Path(current) / name)
+ and name not in ('models', 'envs', 'vendor', 'downloads', 'compiler', 'kernel-cache'))
+ for name in sorted(files, key=lambda n: (not n.endswith('.json'), n)):
+ # Include every small diagnostic sidecar written by a generation
+ # worker. Media, tensors, model weights and prompt.txt stay out
+ # of the bundle through the directory and suffix filters above.
+ if name in NAMES or name.endswith(('.log', '.jsonl', '.request.json', '.debug.json',
+ '.encoding.json', '.engine.json', '.memory.json',
+ '.gpu.json', '.sampling-memory.json', '.comfy-request.json',
+ 'comfy-request.json')):
+ yield Path(current) / name
+
+
+def latest(root, category):
+ parent = root / category
+ if not parent.is_dir() or is_link(parent):
+ return None
+ directories = [p for p in parent.iterdir() if p.is_dir() and not is_link(p)]
+ return max(directories, key=lambda p: p.name) if directories else None
+
+
+def collect(root, config, run, output, *, complete=False):
+ root, config = root.expanduser().resolve(), config.expanduser().absolute()
+ if run is not None and (not run.is_dir() or is_link(run)):
+ raise ValueError('--run must be a real report directory')
+ if output.suffix.lower() != '.zip':
+ raise ValueError('--out must name a new .zip file')
+ # Refuse overwriting evidence before invoking any discovery commands.
+ output.parent.mkdir(parents=True, exist_ok=True)
+ setup = latest(root, 'setup-runs')
+ selected = run.resolve() if run else latest(root, 'test-runs')
+ optimized = latest(root, 'optimization-runs')
+ redactor = Redactor([(Path.home(), '<HOME>'), (root, '<INSTALL>'),
+ *((p, '<' + label.upper() + '>') for label, p in [('setup', setup), ('run', selected), ('optimization', optimized)] if p)])
+ manifest = {'schema_version': 1, 'engine_version': __version__, 'created_epoch': time.time(),
+ 'files': [], 'omitted': [], 'errors': [], 'payload_bytes': 0,
+ 'scope': ('Complete eligible local diagnostics. ' if complete else 'Local bounded diagnostics. ') +
+ 'No media, tensors, model weights, environment dump or automatic upload. '
+ 'Archive entries are relative labels. Absolute paths, prompts and credentials are redacted; review before sharing. ' +
+ ('No eligible report files are truncated; originals are unchanged.' if complete else
+ 'Truncated files contain a marker; originals are unchanged.')}
+ with zipfile.ZipFile(output, 'x', compression=zipfile.ZIP_DEFLATED) as archive:
+ def add(name, raw, truncated=False):
+ data = redactor.data(raw, Path(name).suffix)
+ if (not complete and (len(manifest['files']) >= FILE_COUNT or
+ manifest['payload_bytes'] + len(data) > TOTAL_LIMIT)):
+ manifest['omitted'].append({'file': name, 'reason': 'Diagnostic size/file limit reached'})
+ return
+ archive.writestr(name, data)
+ manifest['files'].append({'file': name, 'bytes': len(data), 'truncated': truncated,
+ 'sha256': hashlib.sha256(data).hexdigest()})
+ manifest['payload_bytes'] += len(data)
+
+ def add_path(name, path):
+ try:
+ data, truncated = read_complete(path) if complete else read_bounded(path)
+ add(name, data, truncated)
+ except (OSError, ValueError) as error:
+ manifest['errors'].append({'file': name, 'error': redactor.text(str(error))})
+
+ machine = {}
+ try:
+ raw, truncated = read_complete(config) if complete else read_bounded(config)
+ if not truncated:
+ machine = json.loads(raw)
+ if not isinstance(machine, dict):
+ machine = {}
+ add('installation/machine.json', raw, truncated)
+ except (OSError, ValueError) as error:
+ manifest['errors'].append({'file': 'machine.json', 'error': redactor.text(str(error))})
+ if config.is_file() and not is_link(config):
+ add_path('installation/machine.json', config)
+ tuning_state = root / 'tuning/state.json'
+ kernels = root / 'kernel-capabilities.json'
+ if kernels.is_file():
+ add_path('installation/kernel-capabilities.json', kernels)
+ if tuning_state.is_file():
+ add_path('installation/tuning.json', tuning_state)
+ download_ownership = root / '.freevideo/downloaded-models.json'
+ if download_ownership.is_file():
+ add_path('installation/downloaded-models.json', download_ownership)
+ for label, directory in [('setup', setup), ('run', selected), ('optimization', optimized)]:
+ if directory:
+ for path in report_files(directory):
+ if not complete and (len(manifest['files']) >= FILE_COUNT or manifest['payload_bytes'] >= TOTAL_LIMIT):
+ manifest['omitted'].append({'file': label + '/*', 'reason': 'Collection limit reached'})
+ break
+ add_path(label + '/' + path.relative_to(directory).as_posix(), path)
+ # No CUDA initialization or ready runtime is needed. Failures become evidence.
+ try:
+ smi = nvidia_smi()
+ except RuntimeError:
+ smi = 'nvidia-smi'
+ git = machine.get('git') or 'git'
+ commands = [('nvidia-smi', [smi, '-q']),
+ ('git-head', [git, '-C', str(Path(__file__).resolve().parent.parent), 'rev-parse', 'HEAD']),
+ ('git-status', [git, '-C', str(Path(__file__).resolve().parent.parent), 'status', '--porcelain'])]
+ pythons = {str(p) for key in ('python', 'comfy_python') if (p := machine.get(key))}
+ pythons.update(str(p) for name in ('unified', 'engine', 'encoder')
+ if (p := venv_python(root / 'envs' / name)).is_file())
+ code = 'import importlib.metadata as m,json,sys; print(json.dumps({"python":sys.version,"packages":{d.metadata["Name"]:d.version for d in m.distributions()}}))'
+ for index, python in enumerate(sorted(pythons)):
+ commands.append(('packages-' + str(index), [python, '-B', '-c', code]))
+ for name, command in commands:
+ try:
+ result = subprocess.run(command, capture_output=True, text=True, timeout=15, encoding='utf-8', errors='replace')
+ value = {'returncode': result.returncode,
+ 'stdout': result.stdout if complete else result.stdout[-FILE_LIMIT:],
+ 'stderr': result.stderr if complete else result.stderr[-FILE_LIMIT:]}
+ if result.returncode:
+ manifest['errors'].append({'command': name, 'returncode': result.returncode})
+ except (OSError, subprocess.TimeoutExpired) as error:
+ value = {'error': str(error)}
+ manifest['errors'].append({'command': name, 'error': redactor.text(str(error))})
+ add('current/' + name + '.json', json.dumps(value).encode())
+ bootstrap = bootstrap_root(root)
+ if not is_link(bootstrap) and (bootstrap / 'bootstrap.log').is_file():
+ add_path('bootstrap/bootstrap.log', bootstrap / 'bootstrap.log')
+ if windows():
+ from .desktop_runtime import launcher_root
+ gui_runs = launcher_root() / 'runs'
+ if gui_runs.is_dir() and not is_link(gui_runs):
+ recent = sorted((p for p in gui_runs.iterdir() if p.is_dir() and not is_link(p)), key=lambda p: p.name)[-3:]
+ for gui_run in recent:
+ for path in report_files(gui_run):
+ add_path('launcher/' + gui_run.name + '/' + path.relative_to(gui_run).as_posix(), path)
+ try:
+ add('current/windows.json', json.dumps({'platform': platform.platform(),
+ 'windows_version': platform.win32_ver(), 'machine': platform.machine(),
+ 'memory': system_memory(), 'cpu': platform.processor(),
+ 'executables': {name: shutil.which(name) for name in ('git', 'curl', 'cl', 'nvcc')},
+ 'cuda_paths': {k: v for k, v in os.environ.items() if k == 'CUDA_PATH' or k.startswith('CUDA_PATH_V')},
+ 'bootstrap_log': str(bootstrap / 'bootstrap.log')}).encode())
+ except OSError as error:
+ manifest['errors'].append({'command': 'windows-memory', 'error': redactor.text(str(error))})
+ else:
+ for name, path in [('meminfo.txt', '/proc/meminfo'), ('vmstat.txt', '/proc/vmstat'),
+ ('os-release.txt', '/etc/os-release')]:
+ add_path('current/' + name, Path(path).resolve())
+ # ComfyUI host setup has its own retained run directory. It is not
+ # part of ``setup-runs`` and used to be omitted entirely, leaving only
+ # the generic "all package sources failed" line in a support report.
+ # Include the most recent bounded runs on every platform; package
+ # manager output is already redacted and capped by ``report_files``.
+ host_runs = root / 'launcher' / 'host-runs'
+ if host_runs.is_dir() and not is_link(host_runs):
+ recent = sorted((p for p in host_runs.iterdir() if p.is_dir() and not is_link(p)),
+ key=lambda p: p.name)[-3:]
+ for host_run in recent:
+ for path in report_files(host_run):
+ add_path('launcher/host-runs/' + host_run.name + '/' +
+ path.relative_to(host_run).as_posix(), path)
+ add_path('source/dependencies.json', Path(__file__).with_name('dependencies.json'))
+ source = {p.name: hashlib.sha256(p.read_bytes()).hexdigest() for p in sorted(Path(__file__).parent.glob('*.py'))}
+ add('source/hashes.json', json.dumps(source).encode())
+ disk = shutil.disk_usage(output.parent)
+ add('current/collector.json', json.dumps({'python': sys.version, 'disk_free_bytes': disk.free,
+ 'selected_run': str(selected) if selected else None}).encode())
+ archive.writestr('manifest.json', json.dumps(manifest, indent=2))
+ return manifest
+
+
+def main(argv=None):
+ parser = argparse.ArgumentParser(prog='./freevideo diagnose', description=__doc__)
+ parser.add_argument('--root', type=Path, default=Path(os.environ.get('FREEVIDEO_HOME', DEFAULT_ROOT)))
+ parser.add_argument('--config', type=Path)
+ parser.add_argument('--run', type=Path, help='Test/benchmark/generation report directory; default latest test run')
+ parser.add_argument('--out', type=Path, help='New ZIP; default <installation>/.freevideo/diagnostics/')
+ parser.add_argument('--full', action='store_true',
+ help='Include every eligible report/log sidecar in full; media, tensors, weights, prompts and credentials remain excluded')
+ args = parser.parse_args(argv)
+ if args.out is None:
+ args.out = args.root / '.freevideo' / 'diagnostics' / ('freevideo-diagnostics-' + time.strftime('%Y%m%dT%H%M%SZ', time.gmtime()) + '.zip')
+ try:
+ result = collect(args.root, args.config or args.root / 'machine.json', args.run, args.out, complete=args.full)
+ except (OSError, ValueError) as error:
+ print('Diagnostics could not be written: ' + str(error), file=sys.stderr)
+ return 1
+ print('Diagnostics: %s\nFiles: %d · payload %.2f MiB · collection errors: %d\nLocal only; review before sharing. Original artifacts retained.' %
+ (args.out.resolve(), len(result['files']), result['payload_bytes'] / 2**20, len(result['errors'])))
+ return 0
+
+
+if __name__ == '__main__':
+ raise SystemExit(main())
diff --git a/freevideo_engine/doctor.py b/freevideo_engine/doctor.py
new file mode 100644
index 0000000..a6e5f34
--- /dev/null
+++ b/freevideo_engine/doctor.py
@@ -0,0 +1,55 @@
+"""Readiness checks with an isolated CUDA detector."""
+import json
+import os
+import subprocess
+import sys
+from .hardware import detect, installed_backends
+from .paths import vdn_root, base_path, checkpoint_path, comfy_root
+from .locking import LOCK_ENV, runtime_lock
+from .kernel_capabilities import identity, package_versions, readiness, receipt_path
+from .monitoring import save
+from . import processes
+
+
+def doctor(*, probe=False, hardware=None, backends=None):
+ versions = package_versions()
+ hardware = hardware or detect()
+ backends = set(installed_backends() if backends is None else backends)
+ paths = {'vdn': vdn_root(), 'base': base_path(), 'checkpoint': checkpoint_path(), 'comfy': comfy_root()}
+ result = {'hardware': hardware.to_dict(), 'versions': versions,
+ 'installed_attention_packages': sorted(backends),
+ 'paths': {k: {'path': str(p), 'exists': p.exists()} for k, p in paths.items()},
+ 'validation': 'Package and path discovery only. Run bench to check actual kernels and performance.'}
+ if probe:
+ result['kernel_probes'] = []
+ with runtime_lock() as descriptor:
+ env = dict(os.environ, **{LOCK_ENV: str(descriptor)})
+ for backend in [*sorted(backends), 'linear']:
+ command = [sys.executable, '-m', 'freevideo_engine.probe', backend]
+ try:
+ child = processes.run(command, capture_output=True, text=True, timeout=180,
+ env=env, pass_fds=(descriptor,))
+ try:
+ row = json.loads(child.stdout.strip().splitlines()[-1])
+ if not isinstance(row, dict) or row.get('backend') != backend:
+ raise ValueError('Malformed kernel probe result')
+ except (ValueError, IndexError):
+ row = {'backend': backend, 'status': 'error', 'error': 'Kernel probe produced no valid result'}
+ if child.returncode or row.get('status') != 'complete':
+ row.update(status='error', returncode=child.returncode,
+ stdout_tail=child.stdout[-4000:], stderr_tail=child.stderr[-8000:])
+ elif child.stderr:
+ row['warnings'] = child.stderr[-8000:]
+ except subprocess.TimeoutExpired as error:
+ def tail(value):
+ return (value.decode('utf-8', errors='replace') if isinstance(value, bytes) else value or '')[-8000:]
+ row = {'backend': backend, 'status': 'error', 'error': 'Kernel probe exceeded 180 seconds',
+ 'stdout_tail': tail(error.stdout), 'stderr_tail': tail(error.stderr)}
+ except OSError as error:
+ row = {'backend': backend, 'status': 'error', 'error': 'Could not start kernel probe: ' + str(error)}
+ result['kernel_probes'].append(row)
+ result.update(readiness(result['kernel_probes']), identity=identity(hardware, versions))
+ result['receipt'] = str(receipt_path())
+ result['validation'] = 'Small kernels executed on the listed GPU; this does not validate full-video speed, quality or capacity.'
+ save(receipt_path(), result)
+ return result
diff --git a/freevideo_engine/download_cache.py b/freevideo_engine/download_cache.py
new file mode 100644
index 0000000..df28010
--- /dev/null
+++ b/freevideo_engine/download_cache.py
@@ -0,0 +1,138 @@
+"""Release this download's disk cache without deleting retained data.
+
+Linux file cache is outside process PSS. Sync in a separate thread so slow disks
+do not stop the RAM/cancellation monitor. This is advisory, not a kernel cap.
+"""
+import os
+from pathlib import Path
+import re
+import stat
+import threading
+
+MiB = 1 << 20
+
+
+def mount_type(path, mountinfo=Path('/proc/self/mountinfo')):
+ if os.name != 'posix' or not mountinfo.is_file():
+ return None
+ target = Path(path).resolve()
+ matches = []
+ for line in mountinfo.read_text(encoding='utf-8').splitlines():
+ before, after = line.split(' - ', 1)
+ mount = Path(re.sub(r'\\([0-7]{3})', lambda m: chr(int(m[1], 8)), before.split()[4]))
+ if target == mount or mount in target.parents:
+ matches.append((len(mount.parts), after.split()[0]))
+ return max(matches)[1] if matches else None
+
+
+def discard_read_cache(fd, offset, length):
+ """Drop only already-read clean pages; never flush or alter file contents."""
+ if hasattr(os, 'posix_fadvise'):
+ try:
+ os.posix_fadvise(fd, offset, length, os.POSIX_FADV_DONTNEED)
+ except OSError:
+ pass # Some filesystems do not implement this advisory operation.
+
+
+class DownloadCache:
+ def __init__(self, paths, window_bytes=256 * MiB, interval=.5):
+ self.paths = paths
+ self.window_bytes = window_bytes
+ self.interval = interval
+ self.enabled = hasattr(os, 'posix_fadvise') and hasattr(os, 'fdatasync')
+ self.stop = threading.Event()
+ self.thread = None
+ self.error = None
+ self.synced = {}
+ self.syncs = self.advised_bytes = 0
+
+ def sweep(self, force=False):
+ pending, changed = 0, []
+ for path in self.paths():
+ try:
+ current = path.lstat()
+ except FileNotFoundError:
+ continue
+ if not stat.S_ISREG(current.st_mode) or current.st_nlink != 1 or current.st_size < MiB:
+ continue
+ key = (current.st_dev, current.st_ino)
+ stamp = (current.st_size, current.st_mtime_ns)
+ previous = self.synced.get(key, (0, 0))
+ if force or stamp != previous:
+ # Xet can preallocate then write out of order without growth.
+ pending += max(0, stamp[0] - previous[0]) or self.window_bytes
+ changed.append((path, key, stamp))
+ if not force and pending < self.window_bytes:
+ return
+ for path, key, stamp in changed:
+ try:
+ fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK)
+ except FileNotFoundError:
+ continue # SDK parts may have been merged and removed.
+ try:
+ current = os.fstat(fd)
+ if ((current.st_dev, current.st_ino) != key or current.st_nlink != 1
+ or not stat.S_ISREG(current.st_mode)):
+ continue
+ os.fdatasync(fd) # DONTNEED alone does not release dirty pages.
+ discard_read_cache(fd, 0, current.st_size)
+ self.synced[key] = stamp
+ self.syncs += 1
+ self.advised_bytes += current.st_size
+ finally:
+ os.close(fd)
+
+ def check(self):
+ if self.error is not None:
+ raise OSError('Cannot flush model download to disk; retained files are unchanged') from self.error
+
+ def __enter__(self):
+ if self.enabled:
+ def run():
+ try:
+ while not self.stop.is_set():
+ self.sweep()
+ self.stop.wait(self.interval)
+ self.sweep(force=True)
+ except Exception as error:
+ self.error = error
+ self.thread = threading.Thread(target=run, name='model-disk-cache', daemon=True)
+ self.thread.start()
+ return self
+
+ def __exit__(self, kind, value, traceback):
+ self.stop.set()
+ if self.thread is not None:
+ # The outer setup UI/monitor keeps running while disk writes finish.
+ self.thread.join(timeout=30)
+ if self.thread.is_alive():
+ raise OSError('Model disk writeback did not finish within 30 seconds; files retained')
+ if kind is None:
+ self.check()
+
+ def result(self):
+ return dict(enabled=self.enabled, window_bytes=self.window_bytes, syncs=self.syncs,
+ advised_bytes=self.advised_bytes,
+ error_type=type(self.error).__name__ if self.error is not None else None,
+ scope='Owned download files only; writeback and cache advice, not a hard cache limit')
+
+
+def transfer_paths(path, row):
+ """Only this file's native stages and streaming attempts; never symlinks."""
+ path = Path(path)
+ base = path
+ for _ in Path(row['file']).parts:
+ base = base.parent
+ identity = (row.get('sha256') or row['git_blob'])[:16]
+ stages = base / '.freevideo-downloads'
+ if stages.is_dir() and not stages.is_symlink():
+ for stage in stages.glob(identity + '-*'):
+ if not stage.is_dir() or stage.is_symlink():
+ continue
+ for root, directories, files in os.walk(stage):
+ directories[:] = [name for name in directories if not (Path(root) / name).is_symlink()]
+ for name in files:
+ yield Path(root) / name
+ if path.parent.is_dir():
+ yield path
+ yield from path.parent.glob(path.name + '.partial*')
diff --git a/freevideo_engine/download_settings.py b/freevideo_engine/download_settings.py
new file mode 100644
index 0000000..374f103
--- /dev/null
+++ b/freevideo_engine/download_settings.py
@@ -0,0 +1,143 @@
+"""Model mirror preferences and explicit commands for active download workers."""
+import json
+import math
+from pathlib import Path
+import threading
+import time
+import uuid
+from .monitoring import save
+from .proxy import MODES
+
+CHOICES = ('auto', 'official', 'hf-mirror', 'modelscope')
+FAMILIES = ('edge-models', 'vdn-models', 'models')
+
+
+def speed_text(row, zh=False):
+ if not row.get('ok'):
+ return '未连通' if zh else 'Unavailable'
+ rate = row.get('bytes_per_second')
+ if (row.get('speed_measured') is False or not isinstance(rate, (int, float))
+ or not math.isfinite(rate) or rate <= 0):
+ return '已连通,样本不足' if zh else 'Connected; insufficient sample'
+ speed = rate / 2**20
+ return '<0.01 MiB/s' if speed < .005 else (('%.2f' if speed < 1 else '%.1f') % speed + ' MiB/s')
+
+
+def read(path):
+ try:
+ value=json.loads(Path(path).read_text(encoding='utf-8'))
+ except FileNotFoundError:
+ return dict(source='auto', proxy_mode='auto')
+ if not isinstance(value,dict) or value.get('source') not in CHOICES:
+ raise ValueError('Invalid model download settings: '+str(path))
+ value.setdefault('proxy_mode', 'auto')
+ if value['proxy_mode'] not in MODES:
+ raise ValueError('Invalid download connection mode')
+ return value
+
+
+def preference(root, source=None, *, proxy_mode=None):
+ if source is not None and source not in CHOICES:
+ raise ValueError('Choose Auto, Hugging Face, HF Mirror or ModelScope')
+ if proxy_mode is not None and proxy_mode not in MODES:
+ raise ValueError('Choose Auto, Proxy only or Direct only')
+ path=Path(root)/'download-settings.json'
+ value=read(path)
+ if source is not None:
+ value['source'] = source
+ if proxy_mode is not None:
+ value['proxy_mode'] = proxy_mode
+ value.update(updated_at=time.time(),switch_id=uuid.uuid4().hex)
+ save(path,value)
+ return value
+
+
+class SwitchRequested(Exception):
+ """Separate user intent from a failed source; never retry the old source."""
+ def __init__(self, value):
+ self.value=value
+ super().__init__('Download settings changed')
+
+
+def current(network):
+ path=network.get('download_settings_path')
+ return read(path) if path else dict(source='auto', proxy_mode=network.get('proxy_mode', 'auto'))
+
+
+def order(network, family, names):
+ if network.get('mode')=='official':
+ return ['official']
+ path=network.get('download_settings_path')
+ if not path:
+ return names
+ value=read(path)
+ rows=value.get('probe',{}).get('sources',{}).get(family,[])
+ fresh=(time.time()-value.get('probe',{}).get('measured_at',0) < 86400 and
+ value.get('probe',{}).get('proxy_mode', 'auto') == value['proxy_mode'])
+ if fresh:
+ measured=list(dict.fromkeys(r['id'] for r in rows if r.get('ok') and r.get('id') in names))
+ names=measured+[n for n in names if n not in measured]
+ selected=value['source']
+ # ModelScope has no verified encoder mapping. That family retains its
+ # supported HF sources instead of constructing an invalid download URL.
+ return [selected]+[n for n in names if n!=selected] if family in FAMILIES and selected in names else names
+
+
+class Probe:
+ def __init__(self):
+ self.lock=threading.Lock()
+ self.state=dict(status='idle')
+ self.thread=None
+
+ def snapshot(self):
+ with self.lock:
+ return dict(self.state)
+
+ def start(self, root, hf_token=''):
+ from .hf_auth import environment
+ env = environment(hf_token)
+ root=Path(root)
+ with self.lock:
+ if self.thread is not None and self.thread.is_alive():
+ return dict(self.state)
+ self.state=dict(status='running',root=str(root))
+ selected_mode = read(root/'download-settings.json')['proxy_mode']
+ def work():
+ try:
+ from . import network
+ from .environments import bootstrap_versions
+ package=Path(__file__).parent
+ spec=json.loads((package/'dependencies.json').read_text())
+ versions=bootstrap_versions(json.loads((package/'bootstrap_versions.json').read_text()))
+ def progress(value):
+ with self.lock:
+ self.state.update(progress=value)
+ result=network.plan(spec,versions,timeout=5,env=env,proxy_mode=selected_mode,
+ measure_speed=True,progress=progress)
+ fields=('id','route','ok','bytes_per_second','seconds','bytes','speed_measured',
+ 'startup_seconds','ttfb_seconds','transfer_seconds','measured_bytes','stop_reason',
+ 'score', 'body_stalled')
+ unavailable = not any(r.get('ok') for rows in result['sources'].values() for r in rows)
+ result=dict(measured_at=time.time(),method='http-body-v2',proxy_mode=selected_mode,network_unavailable=unavailable,sources={family:[{k:r[k] for k in fields if k in r}
+ for r in rows] for family,rows in result['sources'].items()})
+ # Keep any preference chosen while probes were in flight.
+ with self.lock:
+ value=read(root/'download-settings.json')
+ if value['proxy_mode'] != selected_mode:
+ self.state=dict(status='idle',root=str(root))
+ return
+ value['probe']=result
+ save(root/'download-settings.json',value)
+ self.state=dict(status='complete',root=str(root),**result)
+ except Exception as error:
+ with self.lock:
+ # Provider exceptions can contain request headers/URLs.
+ self.state=dict(status='failed',root=str(root),
+ error='Source speed test failed (' + type(error).__name__ + '). Retry the speed test.')
+ self.thread=threading.Thread(target=work,daemon=True)
+ self.thread.start()
+ return dict(self.state)
+
+ def select(self, root, source=None, *, proxy_mode=None):
+ with self.lock:
+ return preference(root,source,proxy_mode=proxy_mode)
diff --git a/freevideo_engine/download_settings_ui.py b/freevideo_engine/download_settings_ui.py
new file mode 100644
index 0000000..362814e
--- /dev/null
+++ b/freevideo_engine/download_settings_ui.py
@@ -0,0 +1,154 @@
+"""Native download controls, usable before ComfyUI or the engine is installed."""
+from pathlib import Path
+
+from .download_settings import CHOICES, MODES, Probe, read, speed_text
+
+
+class DownloadSettings:
+ def __init__(self, parent, root, zh=False, probe=None, token=None, busy=None):
+ import tkinter as tk
+ from tkinter import ttk
+ self.root, self.zh = Path(root), zh
+ self.probe = probe or Probe()
+ self.token = token if token is not None else tk.StringVar(parent)
+ self.busy = busy or (lambda: False)
+ self.window = window = tk.Toplevel(parent)
+ window.title(self.t('FreeVideo · Download sources', 'FreeVideo · 下载源'))
+ window.transient(parent)
+ window.geometry('640x770'); window.minsize(550, 710)
+ self.timer = None
+ body = ttk.Frame(window, padding=24); body.pack(fill='both', expand=True)
+ ttk.Label(body, text=self.t('Connection', '连接模式')).pack(anchor='w')
+ self.proxy_mode = ttk.Combobox(body, state='readonly', values=(
+ self.t('Auto (recommended)', '自动(推荐)'), self.t('Proxy only', '仅代理'), self.t('Direct only', '仅直连')))
+ self.proxy_mode.pack(fill='x', pady=(8, 4))
+ self.proxy_mode.bind('<<ComboboxSelected>>', self.select_mode)
+ self.mode_help = tk.StringVar()
+ ttk.Label(body, textvariable=self.mode_help, wraplength=560, style='Muted.TLabel').pack(fill='x', pady=(0, 12))
+ ttk.Label(body, text=self.t('Model download source', '模型下载源')).pack(anchor='w')
+ self.names = (self.t('Automatic', '自动选择'), 'Hugging Face', 'HF Mirror', 'ModelScope')
+ self.source = ttk.Combobox(body, values=self.names, state='readonly')
+ self.source.pack(fill='x', pady=(8, 12))
+ self.source.bind('<<ComboboxSelected>>', self.select)
+ from .hf_auth import HELP_EN, HELP_ZH, TOKEN_URL
+ import webbrowser
+ auth = ttk.LabelFrame(body, text=self.t('Hugging Face token · optional', 'Hugging Face Token · 可选'), padding=12)
+ auth.pack(fill='x', pady=(0, 12))
+ ttk.Label(auth, text=self.t(HELP_EN, HELP_ZH), wraplength=520, style='Muted.TLabel').pack(fill='x')
+ self.token_input = tk.StringVar(value=self.token.get())
+ self.token_entry = ttk.Entry(auth, textvariable=self.token_input, show='•')
+ self.token_entry.pack(fill='x', pady=8)
+ actions = ttk.Frame(auth); actions.pack(fill='x')
+ self.token_apply = ttk.Button(actions, text=self.t('Apply token', '应用 Token'), command=self.apply_token)
+ self.token_apply.pack(side='left')
+ ttk.Button(actions, text=self.t('Get a read token ↗', '获取读取 Token ↗'),
+ command=lambda: webbrowser.open(TOKEN_URL)).pack(side='left', padx=8)
+ self.token_status = tk.StringVar()
+ ttk.Label(auth, textvariable=self.token_status, wraplength=520, style='Muted.TLabel').pack(fill='x', pady=(6, 0))
+ self.status = tk.StringVar()
+ self.probe_button = ttk.Button(body, text=self.t('Test source speeds', '重新测速'), command=self.start_probe)
+ self.probe_button.pack(anchor='w')
+ ttk.Label(body, textvariable=self.status, wraplength=560, style='Muted.TLabel').pack(fill='x', pady=8)
+ self.speeds = ttk.Treeview(body, columns=('model', 'source', 'speed'), show='headings', height=4)
+ for key, en, cn, width in (('model', 'Files', '文件', 120), ('source', 'Source / route', '来源/连接', 235),
+ ('speed', 'HTTP transfer', 'HTTP 传输速度', 150)):
+ self.speeds.heading(key, text=self.t(en, cn)); self.speeds.column(key, width=width, minwidth=80)
+ self.speeds.pack(fill='both', expand=True)
+ ttk.Label(body, text=self.t(
+ 'Switch applies to the current download. Compatible fragments resume; otherwise this file restarts, keeping old fragments on disk. Testing speeds does not switch sources.',
+ '切换立即作用于当前下载。兼容断点会续传;不兼容时当前文件重新下载,旧片段保留。仅测速不会切换。'),
+ wraplength=560, style='Muted.TLabel').pack(fill='x', pady=(12, 4))
+ ttk.Label(body, text=str(self.root), wraplength=560, style='Muted.TLabel').pack(fill='x')
+ ttk.Button(body, text=self.t('Done', '完成'), command=window.destroy).pack(anchor='e', pady=(8, 0))
+ window.bind('<Destroy>', self.destroyed, add='+')
+ self.rendered = None
+ self.poll()
+
+ def t(self, en, zh):
+ return zh if self.zh else en
+
+ def select(self, _=None):
+ try:
+ self.probe.select(self.root, CHOICES[self.source.current()])
+ self.status.set(self.t('Source saved; switching any active model download…', '已保存,正在切换当前模型下载…'))
+ except (OSError, ValueError) as error:
+ self.status.set(str(error))
+
+ def start_probe(self):
+ self.probe.start(self.root, self.token.get())
+ self.refresh()
+
+ def select_mode(self, _=None):
+ try:
+ self.probe.select(self.root, proxy_mode=MODES[self.proxy_mode.current()])
+ self.refresh()
+ except (OSError, ValueError) as error:
+ self.status.set(str(error))
+
+ def apply_token(self):
+ from .hf_auth import validate
+ if self.busy():
+ self.token_status.set(self.t('Stop setup before changing the token, then resume.', '先停止安装,再修改 Token 并继续安装。'))
+ return
+ try:
+ self.token.set(validate(self.token_input.get()))
+ self.token_status.set(self.t('Applied for this session. Review the installation again to continue.',
+ '已应用到本次会话,重新检测安装即可继续。') if self.token.get() else
+ self.t('Session token cleared. Existing environment credentials still apply.', '已清除本次填写的 Token;仍可使用环境中已有的凭据。'))
+ except ValueError:
+ self.token_status.set(self.t('Enter a token beginning with hf_, or leave it empty.', '请输入以 hf_ 开头的 Token,或留空。'))
+
+ def refresh(self):
+ value = read(self.root / 'download-settings.json')
+ self.source.current(CHOICES.index(value['source']))
+ mode = value['proxy_mode']
+ self.proxy_mode.current(MODES.index(mode))
+ self.mode_help.set(self.t(*{
+ 'auto': ('Compare proxy and direct connections automatically.', '自动测速,择优使用代理或直连。'),
+ 'proxy': ('Use the current proxy. No direct fallback.', '沿用当前代理,不尝试直连。'),
+ 'direct': ('Download directly, ignoring proxies.', '忽略代理,直接下载。')}[mode]))
+ state = self.probe.snapshot()
+ busy = state['status'] == 'running'
+ installing = self.busy()
+ self.token_apply.state(['disabled'] if installing else ['!disabled'])
+ self.token_entry.state(['disabled'] if installing else ['!disabled'])
+ if installing:
+ self.token_status.set(self.t('Stop setup to change credentials; downloaded files will be kept.', '如需修改 Token,请先停止安装;已下载文件会保留。'))
+ self.probe_button.state(['disabled'] if busy else ['!disabled'])
+ if busy:
+ progress = state.get('progress', {})
+ self.status.set(self.t('Testing sources… ', '正在测速… ') +
+ ('%s/%s · %s' % (progress.get('done', 0), progress.get('total', '?'), progress.get('source', ''))))
+ elif state.get('error'):
+ self.status.set(state['error'])
+ elif state.get('network_unavailable'):
+ self.status.set(self.t('No sources reachable. Check your network or connection mode.', '下载源均无法连接,请检查网络或切换连接模式。'))
+ measured = value.get('probe')
+ if measured != self.rendered:
+ self.rendered = measured
+ self.speeds.delete(*self.speeds.get_children())
+ families = {'edge-models': self.t('Video model', '视频模型'), 'vdn-models': self.t('Decoder', '解码器'),
+ 'models': self.t('Text encoder', '文本编码器'), 'pypi': self.t('Python packages', 'Python 依赖'),
+ 'github': self.t('Tools', '安装工具'), 'git': 'Git', 'cuda': 'CUDA'}
+ for family, rows in (measured or {}).get('sources', {}).items():
+ if family.startswith('torch-'):
+ families[family] = self.t('GPU packages', 'GPU 依赖')
+ for row in rows:
+ speed = speed_text(row, self.zh)
+ route = self.t('direct', '直连') if row.get('route') == 'direct' else self.t('current connection', '当前连接')
+ self.speeds.insert('', 'end', values=(families.get(family, family), row['id'] + ' · ' + route, speed))
+ if measured and not busy and not state.get('network_unavailable'):
+ self.status.set(self.t('HTTP transfer measured without connection wait. Parallel/Xet downloads may differ. Choose a source above to switch.',
+ '测速已扣除连接等待;多线程/Xet 下载速度可能不同。在上方选择下载源即可切换。'))
+
+ def poll(self):
+ try:
+ self.refresh()
+ except (OSError, ValueError) as error:
+ self.status.set(str(error))
+ self.timer = self.window.after(400, self.poll)
+
+ def destroyed(self, event):
+ if event.widget == self.window and self.timer is not None:
+ self.window.after_cancel(self.timer)
+ self.timer = None
diff --git a/freevideo_engine/encode.py b/freevideo_engine/encode.py
new file mode 100644
index 0000000..0a621bd
--- /dev/null
+++ b/freevideo_engine/encode.py
@@ -0,0 +1,51 @@
+"""Reusable native H3 conditioning with a short-lived text-encoder process."""
+import json
+import os
+from pathlib import Path
+import signal
+import tempfile
+from .generate import child
+from .hardware import detect
+from .kernel_capabilities import available_backends
+from .locking import runtime_lock, LOCK_ENV
+from .paths import comfy_root
+from .policy import choose
+from . import processes
+from .system import ram_budget_is_estimate, inference_emergency_floor
+
+
+def run(args):
+ destination = args.out.resolve()
+ if destination.suffix != '.pt':
+ raise ValueError('--out must name a .pt conditioning file')
+ if destination.exists():
+ raise FileExistsError(destination)
+ destination.parent.mkdir(parents=True, exist_ok=True)
+ hardware = detect()
+ policy = choose(hardware, **{key: getattr(args, key) for key in
+ ['vram_gib', 'ram_gib', 'attention', 'gpu_reserve_gib', 'ram_reserve_gib']},
+ available_backends=available_backends(hardware), allow_capacity_trial=True)
+ def interrupted(signum, frame):
+ raise KeyboardInterrupt(f'Encoding interrupted by signal {signum}')
+ previous = processes.termination_handler(interrupted)
+ try:
+ with runtime_lock() as descriptor, tempfile.TemporaryDirectory(prefix='freevideo-encode-', dir=destination.parent) as directory:
+ request = Path(directory) / 'request.json'
+ request.write_text(json.dumps({'prompt': args.prompt_file.read_text(encoding='utf-8'), 'encoder': args.encoder,
+ 'comfy_root': str(args.comfy_root or comfy_root()),
+ 'model_paths': str(args.model_paths.resolve()) if args.model_paths else None,
+ 'output': str(destination), 'gpu_budget_gb': policy.gpu_budget_bytes / 1e9,
+ 'metrics': str(destination.with_suffix('.encoding.json'))}) + '\n', encoding='utf-8')
+ env = dict(os.environ, PYTHONPATH=str(Path(__file__).resolve().parents[1]),
+ PYTORCH_ALLOC_CONF=policy.allocator_config,
+ PYTORCH_CUDA_ALLOC_CONF=policy.allocator_config,
+ HF_HUB_OFFLINE='1', TRANSFORMERS_OFFLINE='1', PYTHONUNBUFFERED='1')
+ env[LOCK_ENV] = str(descriptor)
+ child([args.comfy_python, '-m', 'freevideo_engine.encode_worker', '--request', str(request)],
+ env, descriptor, destination.with_suffix('.encoding.log'), ram_budget_bytes=policy.ram_budget_bytes,
+ ram_budget_is_estimate=ram_budget_is_estimate(args),
+ minimum_available_bytes=inference_emergency_floor(policy.ram_budget_bytes,
+ getattr(args, 'ram_reserve_gib', None)))
+ finally:
+ processes.restore_handlers(previous)
+ print(json.dumps({'conditioning': str(destination), 'policy': policy.to_dict()}), flush=True)
diff --git a/freevideo_engine/encode_worker.py b/freevideo_engine/encode_worker.py
new file mode 100644
index 0000000..209d92f
--- /dev/null
+++ b/freevideo_engine/encode_worker.py
@@ -0,0 +1,438 @@
+"""Run the native H3 encoder library in the isolated compute environment.
+
+Standalone encoding exits before video; an interactive worker may retain a
+compatible encoder while live resources permit. Qwen weights and conditioning are
+shared with the pinned ComfyUI model library, without deploying its application.
+"""
+import argparse
+import json
+import os
+from pathlib import Path
+import sys
+import time
+from .monitoring import save
+from .processes import worker_signals
+
+
+def encoder_on_gpu(clip):
+ patcher = clip.patcher
+ return (getattr(patcher.current_loaded_device(), 'type', None) == 'cuda' and
+ patcher.loaded_size() >= patcher.model_size() > 0)
+
+
+def preload(clip, request, resident):
+ """Only weight placement; independent of the next prompt or input media."""
+ import torch
+ import comfy.model_management as memory
+ from .idle_encoder import PreloadCancelled, interruptible_load
+ from .resident_models import gpu_bytes, GiB
+ started = time.perf_counter()
+ cancel = Path(request['cancel'])
+ before = gpu_bytes(clip)
+ result = dict(success=True, state='loading', gpu_bytes_before=before,
+ scope='GPU weights only; no prompt encoding or inference',
+ gpu_budget_bytes=resident.gpu_budget,
+ resource_decision=request.get('idle_resource_decision'))
+ updated = [started]
+ def check():
+ if cancel.exists():
+ raise PreloadCancelled('New foreground work takes priority')
+ if resident.available() < 256*1024**2 or not resident.ram_fits(256*1024**2):
+ raise MemoryError('Live memory pressure; stop speculative placement')
+ now = time.perf_counter()
+ if now - updated[0] >= .5:
+ result.update(gpu_bytes_after=gpu_bytes(clip), elapsed_seconds=now-started)
+ save(request['metrics'], result)
+ updated[0] = now
+ try:
+ if encoder_on_gpu(clip):
+ result.update(state='ready', reason='Encoder already fully resident')
+ elif resident.available() < 2*GiB:
+ result.update(state='skipped', reason='Keep useful resident models; insufficient spare GPU workspace')
+ else:
+ check()
+ save(request['metrics'], result)
+ _, total = torch.cuda.mem_get_info()
+ # The native planner already honors the live budget; the allocator
+ # additionally bounds an unexpected speculative allocation.
+ torch.cuda.set_per_process_memory_fraction(min(1., resident.gpu_budget/total))
+ try:
+ with interruptible_load(clip.patcher, check):
+ memory.load_models_gpu([clip.patcher], memory_required=0)
+ torch.cuda.synchronize()
+ finally:
+ torch.cuda.set_per_process_memory_fraction(1.)
+ result.update(state='ready' if encoder_on_gpu(clip) else 'partial',
+ reason='Prepared weights in spare VRAM without encoding a prompt')
+ except (PreloadCancelled, MemoryError, torch.OutOfMemoryError) as error:
+ resident.drop('encoder', 'Speculative preload interrupted; foreground work wins')
+ result.update(state='cancelled' if isinstance(error, PreloadCancelled) else 'released', reason=str(error))
+ result.update(gpu_bytes_after=gpu_bytes(clip), elapsed_seconds=time.perf_counter()-started,
+ torch_peak_allocated_bytes=torch.cuda.max_memory_allocated(),
+ torch_peak_reserved_bytes=torch.cuda.max_memory_reserved())
+ save(request['metrics'], result)
+ print(json.dumps(dict(event='encoder_prewarm_complete', **result)), flush=True)
+ return result
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--request', type=Path)
+ parser.add_argument('--comfy-root', type=Path)
+ parser.add_argument('--check-library', action='store_true', help='Import the native encoder without UI or model weights')
+ args = parser.parse_args()
+ if not args.check_library and not args.request:
+ parser.error('--request is required for encoding')
+ request = json.loads(args.request.read_text(encoding='utf-8')) if args.request else {}
+ started = time.perf_counter()
+ if request.get('metrics'):
+ save(request['metrics'], {'success': False, 'phase': 'encoder_import'})
+ try:
+ with worker_signals():
+ encode(args, request)
+ except BaseException as error:
+ if request.get('metrics'):
+ record = json.loads(Path(request['metrics']).read_text(encoding='utf-8'))
+ record.update(success=False, error=repr(error), work_seconds=time.perf_counter() - started)
+ from .adaptive import classify_failure
+ record['failure'] = classify_failure(error)
+ save(request['metrics'], record)
+ if 'torch' in sys.modules and sys.modules['torch'].cuda.is_initialized():
+ from .encoder_memory import failure_resources
+ record['failure'].update(failure_resources(sys.modules['torch'],
+ query_cuda=record['failure']['kind'] != 'cuda_error'))
+ record['gpu'] = record['failure']['gpu']
+ for target, source in (('torch_peak_allocated_bytes', 'peak_allocated_bytes'),
+ ('torch_peak_reserved_bytes', 'peak_reserved_bytes')):
+ if source in record['gpu']:
+ record[target] = record['gpu'][source]
+ save(request['metrics'], record)
+ raise
+
+
+def encode(args, request, resident=None, diagnostics=None):
+ from .encoder_diagnostics import EncoderTrace
+ trace = diagnostics or EncoderTrace(request, resident=resident is not None)
+ try:
+ metrics = _encode(args, request, resident, trace)
+ except BaseException:
+ trace.finish(False)
+ raise
+ result = trace.finish(True, metrics)
+ if not args.check_library and not request.get('idle_preload'):
+ print(json.dumps({'event': 'encoder_complete', **result}), flush=True)
+ return result
+
+
+def _encode(args, request, resident, trace):
+ loading = trace.data
+ def phase(name, **metrics):
+ trace.stage(name, **metrics)
+ if name in ('encoder_oom', 'encoder_retry'):
+ print(json.dumps(dict(event=name, attempt=len(loading.get('encoder_attempts', [])))), flush=True)
+ if name == 'encoder_oom' and request.get('metrics') and request.get('diagnostic_output'):
+ from .support_report import write_encoder_retry
+ write_encoder_retry(request['diagnostic_output'], loading,
+ index=len(loading.get('encoder_attempts', [])))
+
+ phase('encoder_torch_import')
+ from .paths import comfy_root
+ args.comfy_root = args.comfy_root or Path(request.get('comfy_root') or comfy_root())
+ idle = request.get('idle_preload') is True
+ if idle and resident is None:
+ raise ValueError('GPU preloading requires the managed resident worker')
+ prompt = request.get('prompt', 'library check')
+ if not isinstance(prompt, str) or not prompt.strip():
+ raise ValueError('A nonempty prompt is required')
+ started = time.perf_counter()
+ import torch
+ torch.set_num_threads(8)
+ torch.set_grad_enabled(False)
+ phase('encoder_cuda_setup')
+ if idle:
+ from .idle_encoder import gpu_preload_budget
+ free, total = torch.cuda.mem_get_info()
+ decision = gpu_preload_budget(resident.gpu_budget, request.get('idle_resources'),
+ free=free, total=total, reserved=torch.cuda.memory_reserved())
+ request['idle_resource_decision'] = decision
+ resident.configure(decision['budget_bytes'], resident.ram_budget,
+ gpu_reserve=decision['system_reserve_bytes'])
+ request['gpu_budget_gb'] = decision['budget_bytes'] / 1e9
+ if decision['budget_bytes'] < 4e9 or Path(request['cancel']).exists():
+ return dict(state='skipped', resource_decision=decision,
+ reason='Foreground work or insufficient live memory for idle preloading')
+ phase('encoder_options')
+ sys.path.insert(0, str(args.comfy_root.resolve()))
+ import comfy.options
+ comfy.options.enable_args_parsing()
+ # The child has no UI cache, custom-node initialization or HTTP server.
+ sys.argv = [str(args.comfy_root / 'main.py'), '--cache-none', '--disable-all-custom-nodes',
+ '--disable-dynamic-vram', '--disable-comfy-compiler', '--disable-cuda-graphs']
+ budget = request.get('gpu_budget_gb')
+ reserve_gib = None
+ phase('encoder_cuda_setup')
+ if budget is not None:
+ budget = float(budget)
+ if budget < 4:
+ raise ValueError('GPU budget must be at least 4 decimal GB')
+ available, total = torch.cuda.mem_get_info()
+ # A budget above the device's capacity is the automatic policy on a
+ # large card, not a bad request: an H200's own plan is about 145 GB and
+ # a fixed 128 GB ceiling refused the request before encoding started.
+ # Clamp to the capacity the reserve is computed against.
+ budget = min(budget, total / 1e9)
+ # Let the native model manager leave space for CUDA and live activations.
+ # This is placement guidance, not an enforced capacity result.
+ reserve_gib = max(0.5, (available - budget * 1e9 + 1e9) / 2**30)
+ sys.argv += ['--reserve-vram', str(reserve_gib)]
+ phase('encoder_path_config')
+ import folder_paths
+ from utils.extra_config import load_extra_path_config
+ if request.get('model_paths'):
+ load_extra_path_config(request['model_paths'])
+ phase('encoder_native_import')
+ import comfy.sd
+ if args.check_library:
+ forbidden = {'nodes', 'server', 'execution'} & set(sys.modules)
+ if forbidden:
+ raise RuntimeError('Unexpected application modules: ' + repr(forbidden))
+ print(json.dumps({'native_encoder_library_import': True, 'application_modules_loaded': [],
+ 'torch': str(torch.__version__), 'source': str(args.comfy_root)}), flush=True)
+ return
+ phase('encoder_media_prepare')
+ keyframes = request.get('keyframes')
+ images = None
+ normalized = None
+ media_kwargs = {}
+ task = 't2va'
+ if request.get('media'):
+ from .media_request import task_for
+ from .media_encoding import prepare
+ task = task_for(request['media'])
+ if task != 't2va':
+ normalized, media_kwargs = prepare(request['media'], request['geometry'], Path(request['output']).parent / 'media')
+ if keyframes is not None:
+ if not isinstance(keyframes, dict) or set(keyframes) != {'first', 'last'}:
+ raise ValueError('FL2VA encoding requires keyframes.first and keyframes.last')
+ if not request.get('base'):
+ raise ValueError('FL2VA encoding requires the H3 base for VAE conditioning')
+ from PIL import Image
+ from .paths import vdn_root
+ sys.path.insert(0, str(vdn_root()))
+ from src.inference.encode_keyframes import put_on_canvas
+ with Image.open(keyframes['first']) as first, Image.open(keyframes['last']) as last:
+ images, height, width = put_on_canvas([first, last])
+ from freevideo_engine.conditioning import to_cache
+ phase('encoder_lookup')
+ name = request.get('encoder', 'qwen3vl_32b_minimax_h3_nvfp4_awq.safetensors')
+ path = folder_paths.get_full_path_or_raise('text_encoders', name)
+ loading['checkpoint'] = dict(state='read_only_mmap' if sys.platform == 'win32' else 'native_mmap',
+ bytes=Path(path).stat().st_size)
+ print(json.dumps({'event': 'encoder_cache_lookup', 'encoder': name}), flush=True)
+ phase('encoder_load')
+ # Same native loader and model type as CLIPLoader; CPU initialization prevents
+ # eager full-GPU loading before the native memory planner runs.
+ clip = None
+ if resident is not None:
+ from .resident_models import file_identity, key
+ encoder_key = key(dict(file=file_identity(path), library=str(args.comfy_root.resolve()), type='MINIMAX'))
+ clip = resident.take('encoder', encoder_key)
+ estimated = Path(path).stat().st_size * 1.4 + 2*2**30
+ if not idle and clip is None:
+ resident.make_room('encoder', min(resident.gpu_budget, estimated), model=clip, ram_need=2*2**30)
+ elif idle and (Path(request['cancel']).exists() or not resident.ram_fits() or resident.available() < 2*2**30):
+ return dict(state='skipped', reason='No spare live memory, or a foreground request arrived')
+ import comfy.model_management
+ real_free = torch.cuda.mem_get_info()[0] + torch.cuda.memory_reserved() - torch.cuda.memory_allocated()
+ reserve_gib = max(.5, (real_free - resident.available() + 1e9) / 2**30)
+ comfy.model_management.EXTRA_RESERVED_VRAM = reserve_gib * 2**30
+ encoder_cache_hit = clip is not None
+ loading['resident_encoder_cache_hit'] = encoder_cache_hit
+ if clip is None:
+ from .encoder_checkpoint import load_clip
+ print(json.dumps({'event': 'encoder_load_start', 'encoder': name}), flush=True)
+ clip = load_clip(ckpt_paths=[path], embedding_directory=folder_paths.get_folder_paths('embeddings'),
+ clip_type=comfy.sd.CLIPType.MINIMAX,
+ model_options={'initial_device': torch.device('cpu')}, progress=phase)
+ else:
+ loading['checkpoint']['state'] = 'resident_reuse'
+ print(json.dumps({'event': 'encoder_cache_hit', 'encoder': name, 'gpu_ready': encoder_on_gpu(clip)}), flush=True)
+ if resident is not None:
+ resident.put('encoder', encoder_key, clip)
+ if idle:
+ from .encoder_pinning import readonly_pinning
+ import comfy.model_management as memory
+ phase('encoder_idle_preload')
+ with readonly_pinning(torch, memory):
+ return preload(clip, request, resident)
+ gpu_ready_at_start = encoder_on_gpu(clip)
+ loading['resident_encoder_gpu_ready_at_start'] = gpu_ready_at_start
+ load_seconds = time.perf_counter() - started
+ phase('encoder_tokenize', load_seconds=load_seconds)
+ # Call the native encoder library directly. Importing nodes would load the
+ # ComfyUI application/API graph, which independent Engine requests do not use.
+ encode_started = time.perf_counter()
+ vision_kwargs = dict(media_kwargs)
+ if images is not None:
+ import numpy as np
+ vision_kwargs['images'] = [torch.from_numpy(np.array(image)).float().div_(255)[None] for image in images]
+ print(json.dumps({'event': 'encoder_tokenize_start'}), flush=True)
+ tokens = clip.tokenize(prompt, **vision_kwargs)
+ from .encoder_memory import token_counts
+ loading['token_summary'] = token_counts(tokens)
+ tokenize_seconds = time.perf_counter() - encode_started
+ if resident is not None and encoder_cache_hit:
+ resident.encoder_room(clip, tokens, estimated)
+ loading['resident_admission'] = list(resident.decisions)
+ real_free = torch.cuda.mem_get_info()[0] + torch.cuda.memory_reserved() - torch.cuda.memory_allocated()
+ comfy.model_management.EXTRA_RESERVED_VRAM = max(.5*2**30, real_free - resident.available() + 1e9)
+ # Time the loader the native encode path itself invokes. Wrapping it keeps
+ # call order, token-dependent memory planning and arithmetic unchanged.
+ native_load = clip.load_model
+ transfer_seconds = [0.]
+ checkpoint_paths = [path]
+ def timed_load(*values, **kwargs):
+ phase('encoder_device_load', load_seconds=load_seconds)
+ print(json.dumps({'event': 'encoder_device_reuse' if encoder_on_gpu(clip) else 'encoder_device_load_start'}), flush=True)
+ tick = time.perf_counter()
+ result = native_load(*values, **kwargs)
+ torch.cuda.synchronize()
+ transfer_seconds[0] += time.perf_counter() - tick
+ phase('encoder_page_release', device_load_seconds=transfer_seconds[0])
+ # The checkpoint's pages are dead weight once its weights are on the
+ # device, and they are the encoder's whole host footprint. Measured on
+ # an H200 at three GPU budgets, the split of the encoder's own resident
+ # memory after this load:
+ #
+ # budget 6.45 GiB 7.82 GiB total = 5.88 mapped + 1.94 anonymous
+ # budget 9.50 GiB 11.06 GiB total = 8.51 mapped + 2.54 anonymous
+ # budget 120 GiB 17.85 GiB total = 15.00 mapped + 2.84 anonymous
+ #
+ # So the model's real hold is under 3 GiB and the rest is clean file
+ # cache the transfer walked through. A guard that credits clean pages
+ # sees past it, but a hard cgroup or commit limit does not wait for
+ # reclaim: a 10 GiB limit killed the encoder at 9.84 GiB with zero
+ # disk reads recorded. Advise those pages away, as the transformer's
+ # streamed reader already does for its own weights. Anything still
+ # mapped and needed refaults from an immutable read-only file; that
+ # costs a read, where keeping it costs the request.
+ #
+ # It has to be madvise on the mapping, not fadvise on the file:
+ # fadvise only drops pages no process maps, and a tensor maps these
+ # for as long as it exists. Advising the file moved the resident total
+ # by 0.00 GiB at all three budgets. Advising the mapping moved it from
+ # 7.82 to 2.33 GiB at the 6.45 GiB budget, 6.10 to 3.46 at 9.50, and
+ # 9.44 to 2.91 at 120 -- in every case down to the model's real hold
+ # plus 0.39 GiB of mapping.
+ from .encoder_checkpoint import release_mapped_pages
+ released = release_mapped_pages(checkpoint_paths)
+ if released:
+ print(json.dumps(dict(event='encoder_pages_released', **released)), flush=True)
+ from .encoder_memory import snapshot
+ import comfy.model_management as memory
+ phase('encoder_compute', load_seconds=load_seconds, device_load_seconds=transfer_seconds[0],
+ gpu=snapshot(torch, clip, memory))
+ print(json.dumps({'event': 'encoder_compute_start'}), flush=True)
+ return result
+ clip.load_model = timed_load
+ try:
+ from .encoder_memory import encode_with_recovery
+ from .encoder_pinning import readonly_pinning
+ import comfy.model_management as memory
+ with readonly_pinning(torch, memory):
+ encoded, attempts = encode_with_recovery(clip, tokens, memory, torch, phase)
+ loading['encoder_attempts'] = attempts
+ # Finish async forward work at the existing conditioning boundary, so
+ # it is not attributed to packing/saving on the host.
+ torch.cuda.synchronize()
+ from .encoder_memory import snapshot
+ phase('encoder_conditioning_pack', gpu=snapshot(torch, clip, memory))
+ finally:
+ del clip.load_model
+ del timed_load, native_load
+ del tokens
+ task = 'fl2va' if images is not None else task
+ value = to_cache(encoded, prompt, task=task)
+ value['task'] = task
+ torch.cuda.synchronize()
+ prompt_encode_seconds = time.perf_counter() - encode_started
+ keyframe_metrics = {}
+ if images is not None:
+ phase('keyframe_vae')
+ import gc
+ import comfy.model_management
+ del clip, encoded, vision_kwargs
+ if resident is None:
+ comfy.model_management.unload_all_models()
+ gc.collect()
+ comfy.model_management.soft_empty_cache()
+ from diffusers import AutoencoderKLMiniMaxH3
+ from diffusers.modular_pipelines.minimax_h3.encoders import encode_vae_condition
+ from src.inference.render import PIXEL_MEAN, PIXEL_STD
+ vae_started = time.perf_counter()
+ if request.get('metrics'):
+ save(request['metrics'], {**loading, 'success': False, 'phase': 'keyframe_vae',
+ 'load_seconds': load_seconds, 'prompt_encode_seconds': prompt_encode_seconds})
+ vae = AutoencoderKLMiniMaxH3.from_pretrained(request['base'], subfolder='vae')
+ vae.decoder = None
+ vae.post_quant_conv = None
+ if resident is not None:
+ resident.input_vae_room(vae, dict(width=width, height=height))
+ vae.to('cuda')
+ vae.eval().requires_grad_(False)
+ torch.cuda.synchronize()
+ keyframe_metrics['keyframe_vae_load_seconds'] = time.perf_counter() - vae_started
+ vae_started = time.perf_counter()
+ with torch.no_grad():
+ conditions = [encode_vae_condition(vae,
+ torch.from_numpy(np.array(image)).to('cuda').permute(2, 0, 1)[None, :, None],
+ PIXEL_MEAN, PIXEL_STD, 42).cpu() for image in images]
+ torch.cuda.synchronize()
+ keyframe_metrics['keyframe_vae_encode_seconds'] = time.perf_counter() - vae_started
+ value.update(keyframe_anchors=['first', 'last'], condition_latents=conditions,
+ keyframe_files=[keyframes['first'], keyframes['last']], width=width, height=height)
+ from .keyframes import validate_conditioning
+ validate_conditioning(value['prompt_embeds'], value['text_token_tags'],
+ (value['keyframe_anchors'], conditions), 'fl2va', width, height)
+ if normalized is not None:
+ phase('media_vae')
+ import gc
+ import comfy.model_management
+ del clip, encoded, vision_kwargs, media_kwargs
+ if resident is None:
+ comfy.model_management.unload_all_models()
+ gc.collect()
+ comfy.model_management.soft_empty_cache()
+ from .paths import add_vdn
+ add_vdn()
+ from .media_encoding import encode_latents
+ tick = time.perf_counter()
+ info = encode_latents(normalized, value, request['base'], request['geometry'], resident=resident)
+ keyframe_metrics.update(media_vae_encode_seconds=time.perf_counter() - tick, conditioning_info=info)
+ task = info['task']
+ phase('encoder_save')
+ output = Path(request['output'])
+ output.parent.mkdir(parents=True, exist_ok=True)
+ temporary = output.with_suffix('.partial')
+ torch.save(value, temporary)
+ temporary.replace(output)
+ metrics = {'scope': 'Native H3 encoder library in an isolated worker; no UI/server',
+ **loading,
+ 'resident_encoder_cache_hit': encoder_cache_hit,
+ 'resident_encoder_gpu_ready_at_start': gpu_ready_at_start,
+ 'encoder': str(Path(path).resolve()), 'precision': 'native shared NVFP4 checkpoint',
+ 'initial_device': 'cpu', 'reserve_vram_gib': reserve_gib,
+ 'load_seconds': load_seconds, 'work_seconds': time.perf_counter() - started,
+ 'prompt_encode_seconds': prompt_encode_seconds, **keyframe_metrics,
+ 'tokenize_seconds': tokenize_seconds,
+ 'device_load_seconds': transfer_seconds[0],
+ 'encoder_compute_seconds': max(0., prompt_encode_seconds - tokenize_seconds - transfer_seconds[0]),
+ 'task': task,
+ 'conditioning_shape': list(value['prompt_embeds'].shape), 'output': str(output),
+ 'torch_peak_allocated_bytes': torch.cuda.max_memory_allocated(),
+ 'torch_peak_reserved_bytes': torch.cuda.max_memory_reserved(), 'success': True}
+ return metrics
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freevideo_engine/encoder_checkpoint.py b/freevideo_engine/encoder_checkpoint.py
new file mode 100644
index 0000000..0e9bf9e
--- /dev/null
+++ b/freevideo_engine/encoder_checkpoint.py
@@ -0,0 +1,201 @@
+"""Read-only native encoder weights without Windows whole-file COW commit."""
+import json
+import math
+import mmap
+import os
+import struct
+import sys
+import warnings
+import weakref
+
+
+def read_only_state_dict(path, dtypes):
+ """Only for immutable inference weights; tensors retain their mapping.
+
+ safetensors' Torch mmap path briefly owns two whole-file copy-on-write
+ mappings. Windows charges commit for both before any tensor is accessed.
+ A read-only map needs no private backing for untouched checkpoint pages.
+ In-place weight edits must operate on a clone, as with Comfy's native
+ read-only model loader. No quantization or dtype conversion happens here.
+ """
+ import torch
+
+ def unique(pairs):
+ value = {}
+ for key, item in pairs:
+ if key in value:
+ raise ValueError('Duplicate safetensors header key')
+ value[key] = item
+ return value
+
+ with open(path, 'rb') as stream:
+ size = os.fstat(stream.fileno()).st_size
+ prefix = stream.read(8)
+ if len(prefix) != 8:
+ raise ValueError('Incomplete safetensors header')
+ header_size = struct.unpack('<Q', prefix)[0]
+ if not 2 <= header_size <= min(100_000_000, size - 8):
+ raise ValueError('Invalid safetensors header size')
+ header = json.loads(stream.read(header_size), object_pairs_hook=unique)
+ if not isinstance(header, dict):
+ raise ValueError('Invalid safetensors header')
+ metadata = header.pop('__metadata__', {})
+ if not isinstance(metadata, dict) or any(not isinstance(k, str) or not isinstance(v, str)
+ for k, v in metadata.items()):
+ raise ValueError('Invalid safetensors metadata')
+ base = 8 + header_size
+ specs = []
+ for name, info in header.items():
+ if not isinstance(info, dict):
+ raise ValueError('Invalid safetensors tensor specification')
+ dtype_name = info.get('dtype')
+ dtype = dtypes.get(dtype_name) if isinstance(dtype_name, str) else None
+ shape, offsets = info.get('shape'), info.get('data_offsets')
+ if dtype is None or not isinstance(shape, list) or any(type(n) is not int or n < 0 for n in shape):
+ raise ValueError('Invalid or unsupported safetensors tensor dtype/shape')
+ if not isinstance(offsets, list) or len(offsets) != 2 or any(type(n) is not int for n in offsets):
+ raise ValueError('Invalid safetensors tensor offsets')
+ begin, end = offsets
+ count = math.prod(shape)
+ if not 0 <= begin <= end <= size - base or count * dtype.itemsize != end - begin:
+ raise ValueError('Safetensors tensor shape/offsets do not match its bytes')
+ specs.append((begin, end, name, dtype, shape, count))
+ # Include empty tensors, reject overlaps, holes and trailing data before
+ # exposing any tensor to the native model constructor.
+ cursor = 0
+ for begin, end, *_ in sorted(specs):
+ if begin != cursor:
+ raise ValueError('Non-contiguous or overlapping safetensors data')
+ cursor = end
+ if cursor != size - base or sys.byteorder != 'little':
+ raise ValueError('Invalid safetensors data length or unsupported byte order')
+ mapping = mmap.mmap(stream.fileno(), 0, access=mmap.ACCESS_READ)
+ state = {}
+ try:
+ with warnings.catch_warnings():
+ warnings.filterwarnings('ignore', message='The given buffer is not writable')
+ for begin, end, name, dtype, shape, count in specs:
+ state[name] = (torch.frombuffer(mapping, dtype=dtype, count=count, offset=base+begin).reshape(shape)
+ if count else torch.empty(shape, dtype=dtype))
+ except BaseException:
+ state.clear()
+ mapping.close()
+ raise
+ # frombuffer retains the buffer owner, including across Parameter/views.
+ if not state or not any(spec[-1] for spec in specs):
+ mapping.close()
+ return state, metadata
+ # Hand back the mapping so a caller that has finished with the weights can
+ # advise its pages away. fadvise cannot: it only drops pages no process
+ # maps, and these are mapped for as long as a tensor points into them --
+ # advising a consumed 14.61 GiB checkpoint moved the resident total by
+ # 0.00 GiB at three GPU budgets. madvise on the mapping itself does drop
+ # them, and a read-only immutable file refaults correctly.
+ MAPPINGS.setdefault(os.path.realpath(path), []).append(weakref.ref(mapping))
+ begin, _, name, _, _, _ = next(spec for spec in specs if spec[-1])
+ address = state[name].data_ptr() - (base + begin)
+ READ_ONLY_RANGES.append((weakref.ref(mapping), address, address + len(mapping)))
+ return state, metadata
+
+
+# Read-only checkpoint mappings by path, so their pages can be released after
+# their weights have been transferred. Weak references only: the tensors own
+# the mapping and the last of them must still be what closes it. A strong
+# reference here kept every checkpoint mapped for the life of the process.
+MAPPINGS = {}
+READ_ONLY_RANGES = []
+
+
+def owns_readonly_tensor(tensor):
+ """Recognize mapped weights after native Parameter, view or quantized wraps.
+
+ Pointer ranges, not Python tensor attributes, survive those wrappers. The
+ weak mmap owner prevents recycled addresses being mistaken for old weights.
+ """
+ if getattr(getattr(tensor, 'device', None), 'type', None) != 'cpu':
+ return False
+ try:
+ start, size = tensor.data_ptr(), tensor.nbytes
+ except (AttributeError, RuntimeError, TypeError):
+ return False
+ if type(start) is not int or type(size) is not int or size <= 0:
+ return False
+ found = False
+ for item in list(READ_ONLY_RANGES):
+ reference, begin, end = item
+ mapping = reference()
+ if mapping is None or mapping.closed:
+ READ_ONLY_RANGES.remove(item)
+ elif begin <= start and start + size <= end:
+ found = True
+ return found
+
+
+def release_mapped_pages(paths):
+ """Advise away the pages of checkpoints already transferred elsewhere.
+
+ Read-only immutable weights, so a dropped page refaults from the file. The
+ mapping stays open because tensors still point into it; only the resident
+ pages go. Returns what it advised, or None when mapping advice is unavailable
+ (including Windows) or there are no live mappings to advise.
+ """
+ if not hasattr(mmap.mmap, 'madvise') or not hasattr(mmap, 'MADV_DONTNEED'):
+ return None
+ advised, errors = 0, []
+ for item in paths or ():
+ live = MAPPINGS.get(os.path.realpath(item), [])
+ for reference in list(live):
+ mapping = reference()
+ if mapping is None:
+ live.remove(reference) # its tensors are gone; so is it
+ continue
+ try:
+ mapping.madvise(mmap.MADV_DONTNEED)
+ advised += len(mapping)
+ except ValueError:
+ # Closed, so its pages are already gone with it. A weak
+ # reference stays alive while something else still holds the
+ # closed object, so drop the entry rather than report it.
+ live.remove(reference)
+ except OSError as error:
+ errors.append('%s: %s' % (os.path.basename(item), error))
+ return dict(advised_bytes=advised, errors=errors) if advised or errors else None
+
+
+def load_clip(ckpt_paths, embedding_directory=None, clip_type=None, model_options=None,
+ disable_dynamic=False, progress=None):
+ """Keep the native CLIP construction and quantization, replace only file IO."""
+ import comfy.sd
+ import comfy.utils
+ options = {} if model_options is None else model_options
+ # Every platform now. The read-only map was written for Windows, where
+ # safetensors' Torch path briefly owns two whole-file copy-on-write
+ # mappings and commit is charged for both. It earns its place on Linux for
+ # a different reason: it is the only path that keeps a handle on the
+ # mapping, and without one the checkpoint's consumed pages cannot be
+ # released after the weights reach the device. Those pages are the whole
+ # host footprint -- 5.87 of 7.82 GiB resident at a 6.45 GiB GPU budget --
+ # and a hard cgroup or commit limit does not wait for reclaim: a 10 GiB
+ # limit killed this encoder at 9.84 GiB with no disk read recorded. The
+ # native construction and quantization are unchanged either way; only the
+ # file IO differs.
+ states = []
+ for path in ckpt_paths:
+ if progress:
+ progress('encoder_checkpoint_map')
+ state, metadata = read_only_state_dict(path, comfy.utils._TYPES)
+ if options.get('custom_operations') is None:
+ state, metadata = comfy.utils.convert_old_quants(state, model_prefix='', metadata=metadata)
+ states.append(state)
+ if progress:
+ progress('encoder_construct')
+ clip = comfy.sd.load_text_encoder_state_dicts(states, embedding_directory=embedding_directory,
+ clip_type=clip_type, model_options=options, disable_dynamic=disable_dynamic)
+ # Native patcher recreation must use the same reader, not return to COW.
+ clip.patcher.cached_patcher_init = (load_clip_model_patcher,
+ (ckpt_paths, embedding_directory, clip_type, options, disable_dynamic))
+ return clip
+
+
+def load_clip_model_patcher(*args, **kwargs):
+ return load_clip(*args, **kwargs).patcher
diff --git a/freevideo_engine/encoder_diagnostics.py b/freevideo_engine/encoder_diagnostics.py
new file mode 100644
index 0000000..22d7d41
--- /dev/null
+++ b/freevideo_engine/encoder_diagnostics.py
@@ -0,0 +1,122 @@
+"""Encoder phase receipts, including startup before the tensor runtime loads.
+
+Phase times are exclusive wall times, not GPU kernel timings. Native forward
+includes streamed weight transfers; device placement is timed separately.
+Only phase boundaries are sampled, with no CUDA synchronization or tensor hooks.
+"""
+import os
+import json
+from pathlib import Path
+import sys
+import time
+
+from .monitoring import save
+
+
+STAGES = ('worker_import', 'worker_cuda_setup', 'encoder_torch_import',
+ 'encoder_options', 'encoder_cuda_setup', 'encoder_path_config', 'encoder_native_import',
+ 'encoder_media_prepare', 'encoder_lookup', 'encoder_load', 'encoder_checkpoint_map',
+ 'encoder_construct', 'encoder_tokenize', 'encoder_device_load', 'encoder_page_release',
+ 'encoder_compute', 'encoder_oom', 'encoder_retry', 'encoder_conditioning_pack',
+ 'keyframe_vae', 'media_vae', 'encoder_save', 'encoder_idle_preload')
+
+
+def cached_metrics(value):
+ """Retain the original receipt as provenance, never current-request work."""
+ timings = {key: value.pop(key) for key in tuple(value) if key.endswith('_seconds')}
+ value['cache_source_timings'] = timings
+ value.update({key: 0. for key in timings})
+ value['cache_source_metrics'] = {key: value.pop(key) for key in ('timing', 'runtime',
+ 'load_stages', 'gpu', 'checkpoint', 'encoder_attempts', 'token_summary',
+ 'resident_encoder_cache_hit', 'resident_encoder_gpu_ready_at_start') if key in value}
+ value.pop('phase', None)
+ return value
+
+
+def prewarm_receipt(path, *, gpu):
+ if path is None:
+ return {}
+ from .diagnostic_resources import encoder_prewarm
+ result = dict(gpu_preload=gpu, receipt_available=False)
+ try:
+ path = Path(path)
+ result.update(encoder_prewarm(json.loads(path.read_text(encoding='utf-8'))),
+ receipt_available=True, age_seconds=max(0., time.time()-path.stat().st_mtime))
+ except (OSError, ValueError):
+ pass
+ return result
+
+
+class EncoderTrace:
+ def __init__(self, request, *, resident=False):
+ self.path = request.get('metrics')
+ self.started = self.tick = time.perf_counter()
+ self.current = None
+ self.counters = {}
+ self.finished = False
+ torch = sys.modules.get('torch')
+ initialized = bool(torch is not None and torch.cuda.is_initialized())
+ self.data = dict(success=False, load_stages=[],
+ runtime=dict(resident_worker=resident, torch_imported_at_start=torch is not None,
+ cuda_initialized_at_start=initialized, native_imported_at_start='comfy.sd' in sys.modules),
+ timing=dict(version=1, stage_seconds={}, diagnostic_seconds=0., complete=False))
+
+ @staticmethod
+ def process_counters():
+ from .sampling_memory import SamplingMemory
+ return SamplingMemory.process_counters()
+
+ def _close_stage(self, now):
+ if self.current is None:
+ return
+ duration = max(0., now - self.tick)
+ timing = self.data['timing']['stage_seconds']
+ timing[self.current] = timing.get(self.current, 0.) + duration
+ row = self.data['load_stages'][-1]
+ row['duration_seconds'] = duration
+ counters = self.process_counters()
+ row['process_delta'] = {key: value - self.counters[key] for key, value in counters.items()
+ if key in self.counters and value >= self.counters[key]}
+
+ def persist(self):
+ if self.path:
+ save(self.path, self.data)
+
+ def stage(self, name, **metrics):
+ if name not in STAGES:
+ raise ValueError('Unknown encoder diagnostic stage')
+ now = time.perf_counter()
+ self._close_stage(now)
+ self.current = name
+ self.data.update(metrics, phase=name)
+ row = dict(stage=name, seconds=now-self.started)
+ # Early startup must not initialize CUDA merely to measure it.
+ torch = sys.modules.get('torch')
+ if torch is not None and torch.cuda.is_initialized():
+ from .encoder_memory import snapshot
+ row['gpu'] = metrics.get('gpu') or snapshot(torch)
+ try:
+ from .ram import ProcessMemory
+ row['memory'] = ProcessMemory().sample(os.getpid())
+ except (OSError, RuntimeError):
+ pass
+ self.data['load_stages'].append(row)
+ if len(self.data['load_stages']) > 48:
+ self.data['load_stages'][1:-47] = []
+ self.data['timing']['elapsed_seconds'] = now-self.started
+ self.persist()
+ self.counters = self.process_counters()
+ self.tick = time.perf_counter()
+ self.data['timing']['diagnostic_seconds'] += self.tick-now
+
+ def finish(self, success, metrics=None):
+ if self.finished:
+ return self.data
+ now = time.perf_counter()
+ self._close_stage(now)
+ self.data.update(metrics or {})
+ self.data['success'] = success
+ self.data['timing'].update(elapsed_seconds=now-self.started, complete=success)
+ self.persist()
+ self.finished = True
+ return self.data
diff --git a/freevideo_engine/encoder_memory.py b/freevideo_engine/encoder_memory.py
new file mode 100644
index 0000000..3735536
--- /dev/null
+++ b/freevideo_engine/encoder_memory.py
@@ -0,0 +1,187 @@
+"""Bounded encoder OOM recovery without changing tokens or model arithmetic."""
+import gc
+import os
+import sys
+
+GiB = 2**30
+GPU_FIELDS = ('free_bytes', 'total_bytes', 'allocated_bytes', 'reserved_bytes',
+ 'peak_allocated_bytes', 'peak_reserved_bytes', 'loaded_weight_bytes',
+ 'model_weight_bytes', 'extra_reserved_bytes', 'allocator_ooms',
+ 'allocator_retries', 'inactive_split_bytes', 'active_bytes',
+ 'requested_bytes', 'memory_fraction', 'pinned_bytes', 'pinned_limit_bytes',
+ 'readonly_pin_attempts', 'readonly_pin_successes', 'readonly_pin_failures',
+ 'readonly_pin_skips', 'readonly_pin_last_error_code', 'readonly_pin_flags',
+ 'readonly_pin_capacity_bytes', 'readonly_pin_memory_skips', 'readonly_pin_memory_query_failures',
+ 'host_pinned_allocated_bytes', 'host_pinned_active_bytes', 'host_pinned_peak_bytes',
+ 'local_budget_bytes', 'local_usage_bytes', 'nonlocal_budget_bytes', 'nonlocal_usage_bytes')
+
+
+def snapshot(torch, clip=None, manager=None, *, windows_memory=False):
+ """Independent observations: one failed CUDA query must not hide the rest.
+
+ Native registered model pages and PyTorch's host allocator are separate
+ counters. Neither is inferred from free VRAM, RSS, or a pinning budget.
+ This reads counters only; it never allocates tensors or synchronizes CUDA.
+ """
+ from .diagnostic_resources import exception_details
+ result = {}
+ def failed(section, error):
+ details = exception_details(error)[:1]
+ for row in details:
+ row['frames'] = [] # The named counter identifies this optional query.
+ result.setdefault('collection_errors', {}).setdefault(section, details)
+ try:
+ free, total = torch.cuda.mem_get_info()
+ result.update(free_bytes=int(free), total_bytes=int(total))
+ except Exception as error:
+ failed('cuda_memory', error)
+ try:
+ stats = torch.cuda.memory_stats()
+ for target, source in (
+ ('allocated_bytes', 'allocated_bytes.all.current'), ('reserved_bytes', 'reserved_bytes.all.current'),
+ ('peak_allocated_bytes', 'allocated_bytes.all.peak'), ('peak_reserved_bytes', 'reserved_bytes.all.peak'),
+ ('allocator_ooms', 'num_ooms'), ('allocator_retries', 'num_alloc_retries'),
+ ('inactive_split_bytes', 'inactive_split_bytes.all.current'), ('active_bytes', 'active_bytes.all.current'),
+ ('requested_bytes', 'requested_bytes.all.current')):
+ if type(stats.get(source)) is int and stats[source] >= 0:
+ result[target] = stats[source]
+ except Exception as error:
+ failed('allocator_stats', error)
+ try:
+ result['memory_fraction'] = float(torch.cuda.get_per_process_memory_fraction())
+ except Exception as error:
+ failed('memory_fraction', error)
+ try:
+ stats = torch.cuda.memory.host_memory_stats()
+ for target, source in (('host_pinned_allocated_bytes', 'allocated_bytes.current'),
+ ('host_pinned_active_bytes', 'active_bytes.current'),
+ ('host_pinned_peak_bytes', 'allocated_bytes.peak')):
+ if type(stats.get(source)) is int and stats[source] >= 0:
+ result[target] = stats[source]
+ except Exception as error:
+ failed('host_pinning', error)
+ try:
+ if clip is not None:
+ result.update(loaded_weight_bytes=int(clip.patcher.loaded_size()),
+ model_weight_bytes=int(clip.patcher.model_size()))
+ manager = manager or sys.modules.get('comfy.model_management')
+ if manager is not None:
+ result['extra_reserved_bytes'] = int(manager.EXTRA_RESERVED_VRAM)
+ for target, source in (('pinned_bytes', 'TOTAL_PINNED_MEMORY'), ('pinned_limit_bytes', 'MAX_PINNED_MEMORY'),
+ ('readonly_pin_attempts', 'FREEVIDEO_READONLY_PIN_ATTEMPTS'),
+ ('readonly_pin_successes', 'FREEVIDEO_READONLY_PIN_SUCCESSES'),
+ ('readonly_pin_failures', 'FREEVIDEO_READONLY_PIN_FAILURES'),
+ ('readonly_pin_skips', 'FREEVIDEO_READONLY_PIN_SKIPS'),
+ ('readonly_pin_last_error_code', 'FREEVIDEO_READONLY_PIN_LAST_ERROR_CODE'),
+ ('readonly_pin_flags', 'FREEVIDEO_READONLY_PIN_FLAGS'),
+ ('readonly_pin_capacity_bytes', 'FREEVIDEO_READONLY_PIN_CAPACITY_BYTES'),
+ ('readonly_pin_memory_skips', 'FREEVIDEO_READONLY_PIN_MEMORY_SKIPS'),
+ ('readonly_pin_memory_query_failures', 'FREEVIDEO_READONLY_PIN_MEMORY_QUERY_FAILURES')):
+ value = getattr(manager, source, None)
+ if type(value) in (int, float):
+ result[target] = max(0, int(value))
+ except Exception as error:
+ failed('native_placement', error)
+ if windows_memory and sys.platform == 'win32':
+ reader = None
+ try:
+ from .windows_gpu_memory import AdapterMemory
+ reader = AdapterMemory()
+ observed = reader.sample()
+ for segment in ('local', 'nonlocal'):
+ for field in ('budget_bytes', 'usage_bytes'):
+ value = observed.get(segment, {}).get(field)
+ if type(value) is int and value >= 0:
+ result[segment + '_' + field] = value
+ except Exception as error:
+ failed('windows_memory', error)
+ finally:
+ if reader is not None:
+ try:
+ reader.close()
+ except Exception as error:
+ failed('windows_memory', error)
+ return result
+
+
+def failure_resources(torch, *, query_cuda=True):
+ """Capture the failure endpoint before model cleanup releases its memory."""
+ result = {'gpu': snapshot(torch, windows_memory=True) if query_cuda else {}}
+ try:
+ from .ram import ProcessMemory
+ result['memory'] = ProcessMemory().sample(os.getpid())
+ except Exception as error:
+ from .diagnostic_resources import exception_details
+ result['memory_collection_error'] = exception_details(error)
+ return result
+
+
+def token_counts(tokens):
+ """The native H3 token container's lengths, never token IDs or content."""
+ if not isinstance(tokens, dict):
+ return {}
+ sequences = tokens.get('qwen3vl_32b')
+ if not isinstance(sequences, (list, tuple)) or not all(isinstance(row, (list, tuple)) for row in sequences):
+ return {}
+ lengths = [len(row) for row in sequences]
+ return dict(sequences=len(lengths), max_sequence_tokens=max(lengths, default=0), total_tokens=sum(lengths))
+
+
+def encode_with_recovery(clip, tokens, manager, torch, phase, *, max_attempts=3):
+ from .adaptive import classify_failure
+ original_reserve = manager.EXTRA_RESERVED_VRAM
+ attempts = []
+ try:
+ for index in range(max_attempts):
+ try:
+ return clip.encode_from_tokens_scheduled(tokens), attempts
+ except Exception as error:
+ failure = classify_failure(error)
+ native_is_oom = getattr(manager, 'is_oom', None)
+ runtime_code = getattr(error, 'error_code', None)
+ runtime_oom = type(runtime_code) is int and runtime_code == 2
+ if failure['kind'] == 'cuda_error' or (type(runtime_code) is int and runtime_code not in (0, 2)):
+ raise
+ if failure['kind'] != 'gpu_oom' and not (runtime_oom and callable(native_is_oom)):
+ raise
+ # Capture before native is_oom: AcceleratorError handling can
+ # allocate/synchronize to clear the CUDA runtime error state.
+ failed = snapshot(torch, clip, manager, windows_memory=True)
+ failed.update(attempt=index + 1, kind='gpu_oom', exception=failure['exception'])
+ attempts.append(failed)
+ phase('encoder_oom', gpu=failed, encoder_attempts=list(attempts))
+ if index + 1 == max_attempts:
+ raise
+ retry_error = error
+ # Retain the original exception for cleanup failures, but release
+ # completed forward frames after their metadata has been recorded.
+ tb = retry_error.__traceback__
+ while tb is not None:
+ try:
+ tb.tb_frame.clear()
+ except RuntimeError:
+ pass # The recovery function itself is still executing.
+ else:
+ # Diagnostics materialized f_locals. On Python 3.9/3.12,
+ # frame.clear() alone leaves that cached dict holding tensors.
+ tb.tb_frame.f_locals.clear()
+ tb = tb.tb_next
+ try:
+ gc.collect()
+ native_oom = native_is_oom(retry_error) if callable(native_is_oom) else False
+ except Exception as cleanup:
+ retry_error.cleanup_errors = [repr(cleanup)]
+ raise retry_error from cleanup
+ if failure['kind'] != 'gpu_oom' and not native_oom:
+ raise retry_error
+ try:
+ manager.unload_all_models()
+ manager.soft_empty_cache()
+ except Exception as cleanup:
+ retry_error.cleanup_errors = [repr(cleanup)]
+ raise retry_error from cleanup
+ del retry_error
+ manager.EXTRA_RESERVED_VRAM = original_reserve + (index + 1) * GiB
+ phase('encoder_retry', gpu=snapshot(torch, clip, manager), encoder_attempts=list(attempts))
+ finally:
+ manager.EXTRA_RESERVED_VRAM = original_reserve
diff --git a/freevideo_engine/encoder_pinning.py b/freevideo_engine/encoder_pinning.py
new file mode 100644
index 0000000..598c385
--- /dev/null
+++ b/freevideo_engine/encoder_pinning.py
@@ -0,0 +1,169 @@
+"""Native encoder registration for our immutable read-only checkpoint maps."""
+from contextlib import contextmanager
+import ctypes
+import sys
+
+from .encoder_checkpoint import owns_readonly_tensor
+
+
+READ_ONLY_SUPPORTED = 113 # cudaDevAttrHostRegisterReadOnlySupported
+PORTABLE_READ_ONLY = 0x01 | 0x08
+
+
+def _registration_flags():
+ """Query the existing CUDA context, without allocating or changing devices."""
+ try:
+ driver = (ctypes.WinDLL('nvcuda.dll') if sys.platform == 'win32'
+ else ctypes.CDLL('libcuda.so.1'))
+ current = driver.cuCtxGetDevice
+ current.argtypes, current.restype = [ctypes.POINTER(ctypes.c_int)], ctypes.c_int
+ attribute = driver.cuDeviceGetAttribute
+ attribute.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.c_int, ctypes.c_int]
+ attribute.restype = ctypes.c_int
+ device = ctypes.c_int()
+ if current(ctypes.byref(device)):
+ return None
+ supported = ctypes.c_int()
+ if attribute(ctypes.byref(supported), READ_ONLY_SUPPORTED, device.value) == 0 and supported.value:
+ return PORTABLE_READ_ONLY
+ host_tables = ctypes.c_int()
+ if attribute(ctypes.byref(host_tables), 100, device.value) == 0 and host_tables.value:
+ return 0x01 # Host page tables support read-only mappings directly.
+ except (AttributeError, OSError):
+ pass
+ return None
+
+
+def _count(manager, name):
+ field = 'FREEVIDEO_READONLY_PIN_' + name
+ setattr(manager, field, getattr(manager, field, 0) + 1)
+
+
+def _windows_pin_limit(manager):
+ """Bound new registrations by free physical RAM and commit, not total RAM.
+
+ Registered read-only maps can become resident only during the forward.
+ Reserve their bytes up front even when the initial RSS is still small.
+ Existing registrations stay native-owned and remain available for unpin.
+ """
+ from .system import system_memory
+ owned = max(0, int(manager.TOTAL_PINNED_MEMORY))
+ try:
+ memory = system_memory()
+ free = memory.get('physical_available_bytes', memory['available_bytes'])
+ commit = memory.get('commit_available_bytes')
+ if commit is not None:
+ free = min(free, commit)
+ return min(int(manager.MAX_PINNED_MEMORY), owned + max(0, free - 2 * 2**30))
+ except (OSError, RuntimeError, TypeError, KeyError, ValueError):
+ _count(manager, 'MEMORY_QUERY_FAILURES')
+ return owned # Registration is optional; keep pageable inference usable.
+
+
+@contextmanager
+def readonly_pinning(torch, manager):
+ """Adapt native pinning only while the isolated encoder is running.
+
+ CUDA requires ReadOnly for read-only CPU mappings on platforms without
+ host page tables. Native writable tensors keep the original code path;
+ native registries and unpinning continue to own successful registrations.
+ """
+ original = manager.pin_memory
+ if getattr(original, '_freevideo_readonly_adapter', False) is True:
+ yield
+ return
+ unsupported = False
+ support_checked = False
+ flags = None
+ pin_limit = _windows_pin_limit(manager) if sys.platform == 'win32' else None
+ if pin_limit is not None:
+ manager.FREEVIDEO_READONLY_PIN_CAPACITY_BYTES = pin_limit
+
+ def clear_error(runtime):
+ # Failed registration can also leave an asynchronous CUDA error. Use
+ # Comfy's own drain before returning to its pageable transfer path.
+ drain = getattr(manager, 'discard_cuda_async_error', None)
+ if callable(drain):
+ drain()
+ else:
+ clear = getattr(runtime, 'cudaGetLastError', None)
+ if callable(clear):
+ clear()
+
+ def failed(runtime, code):
+ nonlocal unsupported
+ _count(manager, 'FAILURES')
+ manager.FREEVIDEO_READONLY_PIN_LAST_ERROR_CODE = code
+ if code in (2, 712, 801):
+ clear_error(runtime)
+ if code == 801:
+ unsupported = True
+ # Capacity, an overlapping registered page, or unavailable support.
+ # No copy is needed: native offload also accepts pageable weights.
+ return False
+ description = {700: 'illegal memory access', 702: 'launch timeout',
+ 710: 'device-side assert', 719: 'unspecified launch failure',
+ 999: 'unknown error'}.get(code, 'host registration failed')
+ error = RuntimeError('CUDA error: %s (read-only encoder registration, code %s)' % (description, code))
+ error.error_code = code
+ raise error
+
+ def pin(tensor):
+ nonlocal unsupported, support_checked, flags
+ if not owns_readonly_tensor(tensor):
+ return original(tensor)
+ if unsupported or manager.MAX_PINNED_MEMORY <= 0 or not tensor.is_contiguous() or tensor.is_pinned():
+ _count(manager, 'SKIPS')
+ return False
+ size, pointer = tensor.nbytes, tensor.data_ptr()
+ if not pointer:
+ _count(manager, 'SKIPS')
+ return False
+ if pin_limit is not None:
+ live_limit = min(pin_limit, _windows_pin_limit(manager))
+ if manager.TOTAL_PINNED_MEMORY + size > live_limit:
+ _count(manager, 'MEMORY_SKIPS')
+ _count(manager, 'SKIPS')
+ return False
+ memory = getattr(getattr(manager, 'comfy', None), 'memory_management', None)
+ release = getattr(memory, 'extra_ram_release', None)
+ if callable(release):
+ release(memory.RAM_CACHE_HEADROOM)
+ registerable = getattr(manager, 'ensure_pin_registerable', None)
+ if callable(registerable):
+ admitted = registerable(size)
+ else:
+ admitted = manager.TOTAL_PINNED_MEMORY + size <= manager.MAX_PINNED_MEMORY
+ if not admitted:
+ _count(manager, 'SKIPS')
+ return False
+ if not support_checked:
+ support_checked = True
+ flags = _registration_flags()
+ manager.FREEVIDEO_READONLY_PIN_FLAGS = flags or 0
+ if flags is None:
+ unsupported = True
+ _count(manager, 'SKIPS')
+ return False
+ runtime = torch.cuda.cudart()
+ _count(manager, 'ATTEMPTS')
+ try:
+ code = int(runtime.cudaHostRegister(pointer, size, flags))
+ except RuntimeError as error:
+ code = getattr(error, 'error_code', None)
+ if code not in (2, 712, 801):
+ raise
+ return failed(runtime, code)
+ if code:
+ return failed(runtime, code)
+ manager.PINNED_MEMORY[pointer] = size
+ manager.TOTAL_PINNED_MEMORY += size
+ _count(manager, 'SUCCESSES')
+ return True
+
+ pin._freevideo_readonly_adapter = True
+ manager.pin_memory = pin
+ try:
+ yield
+ finally:
+ manager.pin_memory = original
diff --git a/freevideo_engine/encoder_prewarm.py b/freevideo_engine/encoder_prewarm.py
new file mode 100644
index 0000000..775318f
--- /dev/null
+++ b/freevideo_engine/encoder_prewarm.py
@@ -0,0 +1,128 @@
+"""Idle, interruptible encoder file-cache prefetch for interactive sessions.
+
+The native encoder maps its checkpoint. Reading it while idle warms the same OS
+pages without keeping another Torch process, private model copy or CUDA context.
+The OS can reclaim all warmed pages. This is not GPU model residency.
+"""
+import os
+from pathlib import Path
+import threading
+import time
+
+from .hardware import cgroup_memory
+from .monitoring import save
+from .system import system_memory
+
+GiB = 1 << 30
+CHUNK = 8 * 1024 * 1024
+
+
+def plan(size, available, *, ram_budget=None, complete=True):
+ reserve = 2 * GiB
+ limit = max(0, available - reserve)
+ if ram_budget is not None:
+ limit = min(limit, max(0, int(ram_budget)))
+ return dict(enabled=bool(complete and size > 0 and size <= limit), bytes=size,
+ memory_allowance_bytes=limit, system_reserve_bytes=reserve,
+ scope='Reclaimable OS file cache; no private model copy or GPU allocation',
+ reason=('Idle RAM covers the checkpoint plus system headroom' if complete and 0 < size <= limit
+ else 'Leave memory for active applications; encoder loads on demand'))
+
+
+def available_memory():
+ memory = system_memory()
+ group = cgroup_memory()
+ available = memory['available_bytes']
+ if group.get('available_bytes') is not None:
+ # Use actual remaining cgroup capacity, not host RAM or an assumption
+ # that the kernel will reclaim other applications' pages in time.
+ available = min(available, group['available_bytes'])
+ return available, group.get('complete', True)
+
+
+def prefetch(path, selected, cancelled, *, busy=None, notify=None, availability=available_memory):
+ path = Path(path)
+ result = dict(selected, state='skipped', done_bytes=0)
+ started = time.monotonic()
+ if not selected['enabled']:
+ return result
+ initial = path.stat()
+ result['state'] = 'reading'
+ buffer = bytearray(CHUNK)
+ last = 0.
+ with path.open('rb', buffering=0) as stream:
+ while result['done_bytes'] < selected['bytes']:
+ if cancelled.is_set() or busy and busy():
+ result.update(state='cancelled', reason='A new request takes priority')
+ break
+ available, complete = availability()
+ if not complete or available < selected['system_reserve_bytes'] + CHUNK:
+ result.update(state='released', reason='Live RAM pressure; warmed pages remain reclaimable')
+ break
+ count = stream.readinto(buffer)
+ if not count:
+ raise ValueError('Encoder checkpoint changed during prefetch')
+ result['done_bytes'] += count
+ elapsed = time.monotonic() - started
+ result.update(elapsed_seconds=elapsed, bytes_per_second=result['done_bytes'] / max(.001, elapsed))
+ if notify and elapsed - last >= 1:
+ notify(dict(result))
+ last = elapsed
+ else:
+ current = path.stat()
+ if (current.st_size, current.st_mtime_ns) != (initial.st_size, initial.st_mtime_ns):
+ raise ValueError('Encoder checkpoint changed during prefetch')
+ result.update(state='ready', reason='Checkpoint pages read; native loader can reuse them if still cached')
+ result['elapsed_seconds'] = time.monotonic() - started
+ return result
+
+
+class IdlePrewarmer:
+ def __init__(self):
+ self.thread = None
+ self.cancelled = threading.Event()
+ self.guard = threading.Lock()
+ self.report = None
+
+ def stop(self):
+ self.cancelled.set()
+ thread = self.thread
+ if thread and thread is not threading.current_thread():
+ thread.join(timeout=1.)
+ from .encoder_diagnostics import prewarm_receipt
+ return prewarm_receipt(self.report, gpu=False)
+
+ def schedule(self, path, report, *, ram_budget=None, busy=None, notify=None):
+ with self.guard:
+ self.stop()
+ if self.thread and self.thread.is_alive():
+ return False # At most one bounded read, even on a stalled disk.
+ self.cancelled = threading.Event()
+ self.report = Path(report)
+ cancel = self.cancelled
+ def work():
+ result = {'state': 'cancelled', 'reason': 'A new request takes priority'}
+ try:
+ # Return the completed video to the user before doing work.
+ if cancel.wait(2.) or busy and busy():
+ return
+ available, complete = available_memory()
+ selected = plan(Path(path).stat().st_size, available, ram_budget=ram_budget, complete=complete)
+ if os.environ.get('FREEVIDEO_ENCODER_PREWARM', 'auto').lower() in ('0', 'off', 'false'):
+ selected.update(enabled=False, reason='Disabled by FREEVIDEO_ENCODER_PREWARM')
+ result = prefetch(path, selected, cancel, busy=busy, notify=notify)
+ except Exception as error:
+ result = {'state': 'failed', 'reason': str(error)}
+ finally:
+ try:
+ save(report, dict(result, checkpoint=str(path), completed_epoch=time.time()))
+ if notify:
+ notify(result)
+ except (OSError, RuntimeError):
+ pass # Optional idle preparation cannot fail a saved video.
+ self.thread = threading.Thread(target=work, name='FreeVideo encoder prefetch', daemon=True)
+ self.thread.start()
+ return True
+
+
+IDLE = IdlePrewarmer()
diff --git a/freevideo_engine/environments.py b/freevideo_engine/environments.py
new file mode 100644
index 0000000..fc7b313
--- /dev/null
+++ b/freevideo_engine/environments.py
@@ -0,0 +1,47 @@
+"""Pinned installation layouts; model lifetimes are independent of venv count."""
+from pathlib import Path
+import platform
+
+ENVIRONMENTS = {
+ 'unified': {'cuda': 'cu130', 'torch': ['torch==2.13.0', 'torchvision==0.28.0', 'torchaudio==2.11.0']},
+ 'engine': {'cuda': 'cu129', 'torch': ['torch==2.13.0', 'torchvision==0.28.0']},
+ 'encoder': {'cuda': 'cu130', 'torch': ['torch==2.14.0', 'torchvision==0.29.0', 'torchaudio==2.11.0']},
+}
+
+
+def constraints_file(name, system=None):
+ prefix = 'windows-' if (system or platform.system()) == 'Windows' else ''
+ return Path(__file__).resolve().parent.parent / 'constraints' / (prefix + name + '.txt')
+
+
+def bootstrap_versions(value, system=None):
+ result = dict(value, target_system=system or platform.system())
+ if result['target_system'] == 'Windows':
+ result['uv'] = result['windows']['uv']
+ return result
+
+
+def environment_names(layout):
+ if layout == 'unified':
+ return ('unified',)
+ if layout == 'dual':
+ return ('engine', 'encoder')
+ raise ValueError('Unknown environment layout: ' + str(layout))
+
+
+def select_layout(requested=None, saved=None):
+ saved = saved or {}
+ layout = requested or saved.get('pending_environment_layout') or saved.get('environment_layout')
+ if layout is None and saved.get('python'):
+ # v0.2.0/0.2.1 receipts did not record a layout. Preserve them on update.
+ layout = 'unified' if saved.get('python') == saved.get('comfy_python') else 'dual'
+ layout = layout or 'unified'
+ environment_names(layout)
+ return layout
+
+
+def role_pythons(root, layout, system=None):
+ from .system import venv_python
+ names = environment_names(layout)
+ return {'engine': venv_python(root / 'envs' / names[0], system),
+ 'encoder': venv_python(root / 'envs' / names[-1], system)}
diff --git a/freevideo_engine/execution_config.py b/freevideo_engine/execution_config.py
new file mode 100644
index 0000000..75c9b9b
--- /dev/null
+++ b/freevideo_engine/execution_config.py
@@ -0,0 +1,48 @@
+"""Torch-free executable defaults for request identity, never policy selection.
+
+The values mirror Engine.__init__ and decode_to_file, checked by CPU AST tests.
+They describe what omitted options execute, not recommended settings. Callers
+keep the user's original profile for reporting and execution. Future changes to
+these defaults require reviewing the durable-ledger schema migration as well.
+"""
+from types import MappingProxyType
+
+
+ENGINE_DEFAULTS = MappingProxyType({
+ 'attention': 'dense', 'prefetch': True, 'adaln_cache': False,
+ 'inference_kernels': False, 'query_chunk': 0, 'ff_chunk': 0, 'head_chunk': 0,
+ 'offload_refiner': False, 'attention_cpu_outputs': False, 'preload_host': False,
+ 'pin_host_weights': False, 'adaln_disk_cache': False, 'projection_chunk': 0,
+ 'grouped_attention_outputs': False, 'fp8_ff_recompute': False,
+ 'resident_blocks': 0, 'pin_host_gb': 0., 'cache_refined_text': False, 'steps': 8,
+ 'window_batch': 1, 'linear_compute': 'native-fp8', 'fp8_gemm': 'auto',
+ 'window_varlen': False, 'varlen_smooth_k': True, 'task': 't2va',
+ 'stream_weights': False, 'head_parallelism': 1, 'residual_offload': False,
+})
+DECODER_DEFAULTS = MappingProxyType({
+ 'offload': False, 'prefetch': True, 'tile_group': False,
+ 'preload': False, 'pin_weights': False,
+ 'stream_output': False, 'stream_weights': False,
+ 'resident_blocks': 0, 'linear_compute_cache': False,
+})
+
+
+def engine_options(options):
+ if not isinstance(options, dict):
+ raise ValueError('Engine options must be a mapping')
+ return _resolved(ENGINE_DEFAULTS, options)
+
+
+def decoder_options(options):
+ if not isinstance(options, dict):
+ raise ValueError('Decoder options must be a mapping')
+ return _resolved(DECODER_DEFAULTS, options)
+
+
+def _resolved(defaults, options):
+ result = dict(defaults)
+ for key, value in options.items():
+ # JSON may spell the same default as 0/0.0 or True/1. Retain the
+ # declared representation when equality means identical execution.
+ result[key] = defaults[key] if key in defaults and value == defaults[key] else value
+ return result
diff --git a/freevideo_engine/export_slim.py b/freevideo_engine/export_slim.py
new file mode 100644
index 0000000..5812f83
--- /dev/null
+++ b/freevideo_engine/export_slim.py
@@ -0,0 +1,167 @@
+"""Export verified legacy/v2 AdaLN tables and FP8 weights as a portable model.
+
+This is an explicit model publication operation. It never runs inference, casts
+weights, deletes originals, or uses the producer GPU as a compatibility key.
+"""
+import argparse
+import json
+import os
+from pathlib import Path
+import shutil
+
+from . import adaln_assets as assets
+from .monitoring import save
+
+# Explicit legacy contract migration, not a live-code hash in the v2 key.
+LEGACY_CONTRACTS = {'ddfb84aa2970cd8169b22c18f2005e2098a1d7273ff257e92d49566de9141706'}
+LEGACY_MODEL_CONTRACTS = {'0f94a5583d5f3f16f9e2d10f0955eb2f55a92a0b6501f339d2b6445f38614841'}
+
+
+def embedding_hash(path):
+ """Hash only time-embedding tensors, in bounded reads, without Torch."""
+ import hashlib
+ import struct
+ h = hashlib.sha256()
+ with Path(path).open('rb') as stream:
+ length = struct.unpack('<Q', stream.read(8))[0]
+ if length > 16 * 1024**2:
+ raise ValueError('Invalid root tensor header')
+ header = json.loads(stream.read(length))
+ keys = sorted(k for k in header if k.startswith(('time_embedder.', 'time_proj.')))
+ if not keys:
+ raise ValueError('No time embedding tensors in model root')
+ for key in keys:
+ row = header[key]
+ h.update(json.dumps([key, row['dtype'], row['shape']], separators=(',', ':')).encode())
+ start, stop = row['data_offsets']
+ stream.seek(8 + length + start)
+ remaining = stop - start
+ while remaining:
+ block = stream.read(min(remaining, 8 * 1024**2))
+ if not block:
+ raise ValueError('Truncated time embedding weight')
+ h.update(block)
+ remaining -= len(block)
+ return h.hexdigest()
+
+
+def share(source, destination, sha256):
+ destination.parent.mkdir(parents=True, exist_ok=True)
+ if destination.exists():
+ if assets.file_hash(destination) != sha256:
+ raise ValueError('Export destination changed; retained: ' + str(destination))
+ return
+ try:
+ os.link(source, destination)
+ except OSError:
+ if shutil.disk_usage(destination.parent).free < source.stat().st_size + 1024**3:
+ raise ValueError('Insufficient disk space for export copy')
+ temporary = destination.with_suffix('.partial')
+ if temporary.exists():
+ raise ValueError('Interrupted export retained: ' + str(temporary))
+ shutil.copyfile(source, temporary)
+ if assets.file_hash(temporary) != sha256:
+ raise ValueError('Export copy failed verification')
+ temporary.replace(destination)
+
+
+def export(cache, output, tables, *, channels=5376, download=None):
+ from .adaln import schedule_timesteps
+ cache, output = Path(cache).resolve(), Path(output).resolve()
+ if output == cache or cache in output.parents:
+ raise ValueError('Export to a separate directory; originals are never modified')
+ original = json.loads((cache / 'manifest.json').read_text(encoding='utf-8'))
+ groups = [r for r in original['groups'] if not r['group'].startswith('adaln/')]
+ count = len(groups) - 1
+ if {r['group'] for r in groups} != {'root'} | {'blocks/%02d' % i for i in range(count)}:
+ raise ValueError('Export needs every transformer block and one root group')
+ sources = dict(embedding_sha256=embedding_hash(cache / 'root.safetensors'),
+ groups=assets.projection_groups(original))
+ if download:
+ sources['download'] = download
+ manifest = dict(original, format=assets.SLIM_FORMAT, groups=groups, adaln_sources=sources,
+ adaln_tables=[], modification='Original AdaLN projections omitted; fixed portable modulation constants included. FP8 tensor bytes unchanged.')
+ weights = assets.weight_identity(manifest)
+ for table_path in tables:
+ table_path = Path(table_path).resolve()
+ producer = json.loads((table_path / 'identity.json').read_text(encoding='utf-8'))
+ if producer.get('format') == assets.FORMAT:
+ value = producer
+ if value['weights'] != weights:
+ raise ValueError('Portable table belongs to different modulation weights')
+ provenance = json.loads((table_path / 'producer.json').read_text(encoding='utf-8'))
+ else:
+ if (producer.get('implementation') not in LEGACY_CONTRACTS
+ or producer.get('original_implementation') not in LEGACY_MODEL_CONTRACTS
+ or producer.get('source_id') != original['source_id']
+ or producer.get('video_shift') != 12. or producer.get('audio_shift') != 3.):
+ raise ValueError('Legacy table is not a verified export contract')
+ from src.inference.render import KEYFRAME_NOISE_AUG
+ if producer.get('task') != 't2va' and producer.get('keyframe_noise_aug') != KEYFRAME_NOISE_AUG:
+ raise ValueError('Legacy table keyframe schedule differs')
+ times = schedule_timesteps(producer['steps'], device='cpu', task=producer['task'])
+ value = assets.identity(weights, [t.tolist() for t in times], channels)
+ provenance = dict(producer, migrated_from='legacy-device-cache-v1')
+ name = assets.directory(value)
+ previous = next((row for row in manifest['adaln_tables'] if row['directory'] == name), None)
+ records = []
+ for index in range(count):
+ path = table_path / ('%02d.safetensors' % index)
+ receipt = json.loads(path.with_suffix('.json').read_text(encoding='utf-8'))
+ row = dict(index=index, file=name + '/' + path.name,
+ bytes=path.stat().st_size, sha256=receipt['sha256'])
+ assets.check_table(path, row, value)
+ if previous:
+ # Equal schedules may deduplicate only if the actual tensor
+ # values are equal, never on a similarity threshold.
+ from safetensors.torch import load_file
+ import torch
+ left, right = load_file(path), load_file(output / row['file'])
+ if any(not torch.equal(left[k], right[k]) for k in left):
+ raise ValueError('Conflicting fixed tables for the same schedule')
+ del left, right
+ continue
+ share(path, output / row['file'], row['sha256'])
+ save((output / row['file']).with_suffix('.json'), dict(bytes=row['bytes'], sha256=row['sha256']))
+ records.append(row)
+ if not previous:
+ save(output / name / 'identity.json', value)
+ save(output / name / 'producer.json', provenance)
+ manifest['adaln_tables'].append(dict(identity=value, directory=name, files=records, producer=provenance))
+ assets.validate_catalog(manifest, count)
+ # Hash every source before sharing its immutable inode. The export never
+ # credits timestamps as proof of a user-supplied model's content.
+ for row in groups:
+ source = assets.asset_path(cache, row['file'])
+ if source.stat().st_size != row['bytes'] or assets.file_hash(source) != row['sha256']:
+ raise ValueError('FP8 export source changed: ' + row['file'])
+ share(source, output / row['file'], row['sha256'])
+ print(json.dumps({'event': 'export_slim_group', 'group': row['group']}), flush=True)
+ manifest['total_bytes'] = sum(r['bytes'] for r in groups) + sum(
+ r['bytes'] for t in manifest['adaln_tables'] for r in t['files'])
+ manifest['omitted_projection_bytes'] = sum(r['bytes'] for r in sources['groups'])
+ save(output / 'manifest.json', manifest)
+ return manifest
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--cache', required=True, type=Path)
+ parser.add_argument('--out', required=True, type=Path)
+ parser.add_argument('--tables', required=True, type=Path, nargs='+')
+ parser.add_argument('--original-repo')
+ parser.add_argument('--original-revision')
+ args = parser.parse_args()
+ from .paths import add_vdn
+ add_vdn()
+ download = None
+ if args.original_repo or args.original_revision:
+ if not args.original_repo or not args.original_revision:
+ parser.error('Both original source repo and immutable revision are required')
+ download = dict(repo=args.original_repo, revision=args.original_revision, prefix='cache')
+ result = export(args.cache, args.out, args.tables, download=download)
+ print(json.dumps({'bytes': result['total_bytes'], 'omitted_bytes': result['omitted_projection_bytes']}))
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freevideo_engine/fa4_guard.py b/freevideo_engine/fa4_guard.py
new file mode 100644
index 0000000..0146f1f
--- /dev/null
+++ b/freevideo_engine/fa4_guard.py
@@ -0,0 +1,89 @@
+"""Isolated, hash-checked backport for FA4 b26's SM120 invalid-tile read."""
+import hashlib
+import importlib
+import importlib.metadata
+import json
+import os
+from pathlib import Path
+import shutil
+import sys
+import tempfile
+from .paths import data_root
+
+ORIGINAL = 'e9d890e10611ce48dc57c40570a7b65d1ab802c508f785e5536fb10044db65f3'
+PATCHED = '0b6c45fbacf31dfbc6bca600e4efa21892f95a4ad3e21a163c04a74ffa076a12'
+
+
+def location():
+ return data_root() / 'vendor' / 'fa4-b26-valid-tile'
+
+
+def prepare():
+ distribution = importlib.metadata.distribution('flash-attn-4')
+ source = Path(distribution.locate_file('flash_attn/cute/flash_fwd.py'))
+ content = source.read_bytes()
+ if distribution.version != '4.0.0b26' or hashlib.sha256(content).hexdigest() != ORIGINAL:
+ raise RuntimeError('This guard requires the verified unmodified FA4 4.0.0b26; no package was changed.')
+ target = location()
+ if target.exists():
+ validate(target)
+ return target
+ text = content.decode()
+ marker = ' m_block, num_head, batch_size, _ = work_tile.tile_idx\n'
+ if text.count(marker) != 1:
+ raise RuntimeError('Unexpected FA4 source layout')
+ start = text.index(marker)
+ end = text.index(' @cute.jit\n def compute_one_n_block', start)
+ text = text[:start] + ' if work_tile.is_valid_tile:\n' + ''.join(
+ ' ' + line if line.strip() else line for line in text[start:end].splitlines(keepends=True)) + text[end:]
+ if hashlib.sha256(text.encode()).hexdigest() != PATCHED:
+ raise RuntimeError('FA4 guard output differs from the validated patch')
+ target.parent.mkdir(parents=True, exist_ok=True)
+ with tempfile.TemporaryDirectory(dir=target.parent, prefix='fa4-guard-') as temporary:
+ temp = Path(temporary) / 'overlay'
+ shutil.copytree(source.parent, temp / 'flash_attn/cute', ignore=shutil.ignore_patterns('__pycache__', '*.pyc'))
+ (temp / 'flash_attn/cute/flash_fwd.py').write_text(text, encoding='utf-8')
+ (temp / 'manifest.json').write_text(json.dumps({'distribution': distribution.version,
+ 'source_sha256': ORIGINAL, 'patched_sha256': PATCHED, 'installed_package_modified': False,
+ 'upstream_reference': 'https://github.com/Dao-AILab/flash-attention/blob/main/flash_attn/cute/flash_fwd.py'}, indent=2) + '\n', encoding='utf-8')
+ try:
+ os.rename(temp, target)
+ except OSError:
+ if not target.exists():
+ raise
+ validate(target) # Another setup process may have completed first.
+ return target
+
+
+def validate(root):
+ target = Path(root) / 'flash_attn/cute/flash_fwd.py'
+ if hashlib.sha256(target.read_bytes()).hexdigest() != PATCHED:
+ raise RuntimeError('FA4 overlay failed its source check')
+
+
+def activate():
+ root = location()
+ if not root.exists():
+ return None
+ validate(root)
+ if 'flash_attn.cute' in sys.modules:
+ current = Path(sys.modules['flash_attn.cute'].__file__).resolve()
+ if root.resolve() not in current.parents:
+ raise RuntimeError('FA4 was imported before the guard; start a fresh Engine process')
+ # Extend the package path instead of replacing FA2's __init__.py. This works
+ # with both the FA4 namespace package and a separately installed FA2 package.
+ package = importlib.import_module('flash_attn')
+ paths = [str(root / 'flash_attn'), *list(package.__path__)]
+ package.__path__ = list(dict.fromkeys(paths))
+ return root
+
+
+def check_selected():
+ """Refuse the known faulty SM120 kernel before any attention execution."""
+ import torch
+ if torch.cuda.get_device_capability()[0] != 12:
+ return
+ module = importlib.import_module('flash_attn.cute.flash_fwd')
+ digest = hashlib.sha256(Path(module.__file__).read_bytes()).hexdigest()
+ if digest == ORIGINAL:
+ raise RuntimeError('FA4 b26 needs its SM120 valid-tile fix. Run freevideo setup --fa4-guard, then start a fresh process.')
diff --git a/freevideo_engine/failure_cleanup.py b/freevideo_engine/failure_cleanup.py
new file mode 100644
index 0000000..da90b0d
--- /dev/null
+++ b/freevideo_engine/failure_cleanup.py
@@ -0,0 +1,33 @@
+"""Release failed inference frames after their diagnostic evidence is saved."""
+
+
+def release_exception_frames(error):
+ pending, seen, frames = [error], set(), set()
+ cleared = 0
+ while pending:
+ current = pending.pop()
+ if not isinstance(current, BaseException) or id(current) in seen:
+ continue
+ seen.add(id(current))
+ pending.extend((current.__cause__, current.__context__))
+ children = getattr(current, 'exceptions', ())
+ if isinstance(children, (tuple, list)):
+ pending.extend(children)
+ tb = current.__traceback__
+ current.__traceback__ = current.__cause__ = current.__context__ = None
+ while tb is not None:
+ frame = tb.tb_frame
+ if id(frame) not in frames:
+ frames.add(id(frame))
+ try:
+ frame.clear()
+ except RuntimeError:
+ pass # The caller can still be executing; never clear it.
+ else:
+ # Diagnostics inspect f_locals for tensor shapes. On
+ # CPython 3.9/3.12 that dictionary can itself retain tensors
+ # after frame.clear(), until explicitly emptied.
+ frame.f_locals.clear()
+ cleared += 1
+ tb = tb.tb_next
+ return cleared
diff --git a/freevideo_engine/failure_details.py b/freevideo_engine/failure_details.py
new file mode 100644
index 0000000..b017f70
--- /dev/null
+++ b/freevideo_engine/failure_details.py
@@ -0,0 +1,132 @@
+"""Bounded, copyable request failures for interactive callers. No GPU imports."""
+import json
+from pathlib import Path
+import re
+
+from .diagnostics import Redactor
+
+
+def launcher_failure(value, *, zh=False):
+ """Short guidance for known failures; callers retain the full error separately."""
+ if not value:
+ return dict(title='', detail='', action='', kind='')
+ text = str(value)[-65536:]
+ rules = (
+ (r'Installation RAM monitoring failed|Cannot read process-tree memory', 'memory-monitor',
+ ('Memory usage could not be read', '暂时无法读取进程内存'),
+ ('Download progress is saved. Check and continue to retry; copy the details if it happens again.',
+ '下载进度已保留。点击“检查并继续”重试;若再次出现,请复制详情反馈。')),
+ (r'No space left on device|WinError 112|disk (?:is )?full', 'disk',
+ ('Not enough disk space', '磁盘空间不足'),
+ ('Free up space on the installation drive, then continue.', '清理安装盘空间后,点击继续。')),
+ (r'Model download paused|unexpected-transfer-size|ConnectionError|ConnectTimeout|ReadTimeout|HTTP probe failed|Could not resolve host|SSL certificate|Every download source failed|All Git sources failed|All package sources failed|Python download failed on every route', 'download',
+ ('Download interrupted', '下载中断了'),
+ ('Check your network or change the source or connection mode in Settings → Downloads, then retry.', '检查网络,或在“设置 → 下载”中切换下载源、连接模式后重试。')),
+ (r'PermissionError|Access is denied|Permission denied|WinError 5\b', 'permission',
+ ('This folder is not writable', '无法写入这个目录'),
+ ('Choose an installation folder you can write to, then retry.', '选择有写入权限的安装目录后重试。')),
+ (r'ModuleNotFoundError|No module named|DLL load failed', 'dependencies',
+ ('The environment needs repair', '运行环境需要修复'),
+ ('Return to Installation and check the same folder to restore missing dependencies.', '返回安装设置,检查当前安装目录,补齐运行依赖。')),
+ (r'CUDA (?:error: )?out of memory|torch\.OutOfMemoryError|Insufficient currently available memory|crossed its RAM budget|paging file is too small|WinError 1455', 'memory',
+ ('Memory is unavailable for this step', '这一步可用内存不足'),
+ ('Close other memory-heavy applications and retry. Copy the details if it continues.', '关闭占用内存较多的程序后重试;仍失败时可复制详情反馈。')),
+ )
+ for pattern, kind, title, action in rules:
+ if re.search(pattern, text, re.I):
+ return dict(title=title[zh], detail='', action=action[zh], kind=kind)
+ # Validation messages are already actionable. Show the cause, not a stack
+ # trace or a guessed hardware diagnosis, and leave long details expandable.
+ lines = [line.strip() for line in text.splitlines() if line.strip()]
+ detail = next((line for line in reversed(lines) if not line.startswith(('File ', 'Traceback', '^'))), '')
+ detail = re.sub(r'^(?:ValueError|RuntimeError|OSError):\s*', '', detail)
+ return dict(title='暂时未能完成' if zh else 'Couldn’t complete this step',
+ detail=detail[:400], action='', kind='unknown')
+
+
+def _read(path, limit, *, tail=False):
+ try:
+ with path.open('rb') as stream:
+ if tail:
+ stream.seek(0, 2)
+ start = max(0, stream.tell() - limit)
+ stream.seek(start)
+ value = stream.read(limit).decode('utf-8', errors='replace')
+ return value.partition('\n')[2] if start and '\n' in value else value
+ raw = stream.read(limit + 1)
+ return raw.decode('utf-8', errors='replace') if len(raw) <= limit else ''
+ except OSError:
+ return ''
+
+
+def generation_failure(run, exit_code=None):
+ """Surface the cause, including a child traceback, even without a report."""
+ run = Path(run)
+ redactor = Redactor()
+ prompt = _read(run / 'prompt.txt', 128*1024).strip()
+ if prompt:
+ redactor.prompts.add(prompt)
+ lines = ['FreeVideo generation failed' + (' (exit %s)' % exit_code if exit_code is not None else '')]
+ raw = _read(run / 'video.request.json', 512*1024)
+ try:
+ report = json.loads(raw)
+ except (ValueError, TypeError):
+ report = {}
+ if not isinstance(report, dict):
+ report = {}
+ redactor.structured(report) # Collect prompt values without showing the request.
+ error = report.get('error_message') or report.get('error')
+ if isinstance(error, str):
+ lines.append(error[:8192])
+ planning = report.get('resource_planning')
+ encoding_failure = report.get('encoding_failure')
+ phase = encoding_failure.get('phase') if isinstance(encoding_failure, dict) else None
+ phase = phase or report.get('phase')
+ if phase:
+ lines.append('Stage: ' + str(phase))
+ if isinstance(planning, list) and planning and isinstance(planning[-1], dict):
+ last = planning[-1]
+ if not phase:
+ lines.append('Stage: ' + str(last.get('stage', 'unknown')))
+ state = last.get('idle_cache') or {}
+ accounting = state.get('ram_accounting', {}) if isinstance(state, dict) else {}
+ if not isinstance(accounting, dict):
+ accounting = {}
+ for key, label in (('raw_available_bytes', 'System reported available RAM'),
+ ('credited_available_bytes', 'Available RAM including reclaimable idle cache')):
+ value = accounting.get(key)
+ if type(value) is int and value >= 0:
+ lines.append('%s: %.2f GiB' % (label, value / 2**30))
+ memory = state.get('memory') if isinstance(state, dict) else None
+ if isinstance(memory, dict):
+ for key, label in (('system_physical_available_bytes', 'Windows free physical RAM'),
+ ('system_commit_available_bytes', 'Windows commit headroom'),
+ ('reclaimable_mapped_bytes', 'Worker reclaimable mappings'),
+ ('guard_bytes', 'Worker private working RAM')):
+ value = memory.get(key)
+ if type(value) is int and value >= 0:
+ lines.append('%s: %.2f GiB' % (label, value / 2**30))
+ recovery = next((r.get('recovery') for r in reversed(planning) if isinstance(r, dict) and r.get('recovery')), None)
+ if recovery:
+ lines.append('Idle cache release: ' + str(recovery.get('reason', recovery) if isinstance(recovery, dict) else recovery))
+ # Child logs carry the actual CUDA/encoder error when the parent only knows
+ # an exit code. Read bounded tails; do not read model data or entire logs.
+ for name in ('generate.log', 'video.engine.log', 'video.encoding.log'):
+ value = _read(run / name, 8192, tail=True)
+ if not value:
+ continue
+ rows = [line for line in value.splitlines() if line.strip() and not line.lstrip().startswith('{"event":')]
+ trace = '\n'.join(rows[-45:])
+ if trace:
+ lines.extend(['\n' + name + ':', trace])
+ if len(lines) == 1:
+ lines.append('The worker ended without a readable error report.')
+ lines.append('\nRetained outputs: ' + str(run))
+ value = redactor.text('\n'.join(lines))
+ value = re.sub(r'\x1b(?:\[[0-?]*[ -/]*[@-~]|\][^\x07\x1b]*(?:\x07|\x1b\\))', '', value)
+ return re.sub(r'[\x00-\x08\x0b-\x1f\x7f]', '', value)
+
+
+def startup_failure(message, log):
+ detail = _read(Path(log), 8192, tail=True).strip()
+ return Redactor().text(message + ('\n\n' + detail if detail else '\nNo readable process output was produced.') + '\n\nLog: ' + str(log))
diff --git a/freevideo_engine/file_ops.py b/freevideo_engine/file_ops.py
new file mode 100644
index 0000000..594f94e
--- /dev/null
+++ b/freevideo_engine/file_ops.py
@@ -0,0 +1,20 @@
+"""Publish closed files without restarting downloads on Windows sharing locks."""
+from pathlib import Path
+import time
+from .system import windows
+
+
+def publish(source, destination):
+ source, destination = Path(source), Path(destination)
+ for attempt in range(8):
+ if destination.exists() or destination.is_symlink():
+ raise FileExistsError('Destination already exists; both files retained: ' + str(destination))
+ try:
+ source.rename(destination)
+ return
+ except OSError as error:
+ if not windows() or getattr(error, 'winerror', None) not in (5, 32, 33) or attempt == 7:
+ raise
+ # Defender/indexers can briefly open a just-closed model. Retry only
+ # publication, never the transfer. Permanent permission errors stop.
+ time.sleep(.1 * (attempt + 1))
diff --git a/freevideo_engine/fp8.py b/freevideo_engine/fp8.py
new file mode 100644
index 0000000..917f3fe
--- /dev/null
+++ b/freevideo_engine/fp8.py
@@ -0,0 +1,402 @@
+"""Prepared storage for the official VDN FP8 arithmetic and scale granularity.
+
+Quantization calls the upstream Fp8Linear constructor on one CUDA matrix at a
+time. Loading binds those exact bytes to the original Fp8Linear class. The BF16
+backup is omitted, as in the separately verified compact-storage wrapper.
+"""
+import argparse
+from contextlib import closing
+import gc
+import hashlib
+import json
+import os
+from pathlib import Path
+import time
+
+from safetensors.torch import save_file
+import torch
+
+from .weights import sha256, skeleton, Sources, group_name
+from .preparation import cache_location, expected_groups, reusable_group, source_paths
+from .tensor_io import open_tensors
+
+
+def input_dtype(module):
+ """Input dtype of an ordinary Linear or a cached official FP8 Linear."""
+ return module.input_dtype if hasattr(module, 'input_dtype') else module.weight.dtype
+
+
+def quantize_group(values, eligible, official):
+ """Same official quantization for both retained-BF16 and streamed setup."""
+ converted, linears = {}, {}
+ items = values.items() if hasattr(values, 'items') else values
+ for name, value in items:
+ prefix, _, suffix = name.rpartition('.')
+ if prefix not in eligible:
+ converted[name] = value
+ elif suffix == 'bias':
+ converted[prefix + '.original.bias'] = value
+ elif suffix == 'weight':
+ module = torch.nn.Linear(value.shape[1], value.shape[0], bias=False, device='meta', dtype=value.dtype)
+ module.weight = torch.nn.Parameter(value.to('cuda'), requires_grad=False)
+ quantized = official.Fp8Linear(module)
+ converted[prefix + '.weight_fp8'] = quantized.weight_fp8.cpu()
+ converted[prefix + '.weight_scale'] = quantized.weight_scale.cpu()
+ linears[prefix] = {'weight_shape': list(value.shape),
+ 'scale_shape': list(quantized.weight_scale.shape),
+ 'input_dtype': str(value.dtype).removeprefix('torch.')}
+ del quantized, module
+ else:
+ raise ValueError('Unknown eligible Linear state: ' + name)
+ # Do not keep the preceding matrix while the iterator merges/loads the
+ # next one. Only this group's converted output survives until saving.
+ del value
+ return converted, linears
+
+
+def _merged_tensors(sources, entries):
+ for name, parameter in entries:
+ final_dtype = torch.bfloat16 if '.attn.' in name and name.startswith('transformer_blocks.') else parameter.dtype
+ value = sources.tensor(name, parameter.dtype, final_dtype)
+ try:
+ if value.shape != parameter.shape:
+ raise ValueError('Weight shape mismatch: ' + name)
+ yield name, value
+ finally:
+ del value
+
+
+def _file_tensors(path):
+ with open_tensors(path) as stream:
+ for name in stream.keys():
+ value = stream.get_tensor(name)
+ try:
+ yield name, value
+ finally:
+ del value
+
+
+def prepare_streamed(base, checkpoint, cache_root):
+ """Merge and quantize one group at a time, without a second BF16 disk cache.
+
+ Sources.tensor retains the original CPU LoRA merge order and dtypes. The
+ final group is hashed and checkpointed before advancing; retries reuse only
+ verified complete groups. Original source files remain read-only here.
+ """
+ from . import weights
+ from .paths import add_vdn
+ add_vdn()
+ from src.models.ops import fp8_linear as official
+ base, checkpoint, cache_root = map(Path, (base, checkpoint, cache_root))
+ started = time.monotonic()
+ identity = {'preparation': 'streamed_cpu_merge_official_cuda_fp8',
+ 'implementation_sha256': sha256(__file__), 'merge_implementation_sha256': sha256(weights.__file__),
+ 'official_fp8_sha256': sha256(official.__file__),
+ 'base_config_sha256': sha256(base / 'transformer/config.json'),
+ 'model_spec_sha256': sha256(checkpoint / 'model_spec.json'),
+ 'torch': torch.__version__, 'scale_granularity': 'per_tensor' if official.per_tensor_gemm() else 'rowwise',
+ 'min_width': official.MIN_WIDTH, 'skip_end_blocks': 0,
+ 'sources': [{'path': str(p), 'bytes': p.stat().st_size, 'mtime_ns': p.stat().st_mtime_ns}
+ for p in source_paths(base, checkpoint)]}
+ output, identity, key = cache_location(cache_root, 'vdn-fp8-streamed-', identity)
+ output.mkdir(parents=True, exist_ok=True)
+ marker = output / 'source.json'
+ if marker.exists() and json.loads(marker.read_text(encoding='utf-8')) != identity:
+ raise ValueError('Streamed FP8 cache identity conflict')
+ if not marker.exists():
+ marker.write_text(json.dumps(identity, indent=2) + '\n', encoding='utf-8')
+ group_names = expected_groups(base)
+ # Even the meta model and source-key index are unnecessary on a full hit.
+ groups = sources = eligible = None
+ records, linears = [], {}
+ reused_groups = source_tensors_loaded = 0
+ for index, group in enumerate(group_names, 1):
+ destination = output / (group + '.safetensors')
+ destination.parent.mkdir(parents=True, exist_ok=True)
+ progress = destination.with_suffix('.json')
+ print(json.dumps({'event': 'prepare_fp8_phase', 'phase': 'verify_cache', 'group': group,
+ 'index': index, 'total': len(group_names)}), flush=True)
+ saved = reusable_group(output, group)
+ if saved is not None:
+ reused_groups += 1
+ records.append(saved)
+ linears.update(saved['linears'])
+ print(json.dumps({'event': 'prepared_fp8_group', 'preparation': 'streamed', 'group': group,
+ 'index': index, 'total': len(group_names), 'reused': True}), flush=True)
+ continue
+ if destination.is_file() and progress.is_file():
+ rejected = '.hash-rejected-' + str(time.time_ns())
+ destination.rename(destination.with_name(destination.name + rejected))
+ progress.rename(progress.with_name(progress.name + rejected))
+ print(json.dumps({'event': 'rebuild_fp8_group', 'group': group,
+ 'reason': 'Corrupt group retained before rebuilding'}), flush=True)
+ print(json.dumps({'event': 'prepare_fp8_phase', 'phase': 'read_source_weights', 'group': group,
+ 'index': index, 'total': len(group_names)}), flush=True)
+ if groups is None:
+ model, sources = skeleton(base, checkpoint), Sources(base, checkpoint)
+ params = dict(model.named_parameters())
+ if params.keys() != sources.weights.keys():
+ raise ValueError({'missing': sorted(params.keys() - sources.weights.keys()),
+ 'unused': sorted(sources.weights.keys() - params.keys())})
+ eligible = {name for name, module in model.named_modules()
+ if isinstance(module, torch.nn.Linear)
+ and min(module.in_features, module.out_features) >= official.MIN_WIDTH}
+ groups = {}
+ for name, parameter in params.items():
+ if not parameter.is_meta:
+ raise ValueError('Preparation skeleton materialized a parameter: ' + name)
+ groups.setdefault(group_name(name), []).append((name, parameter))
+ if sorted(groups) != group_names:
+ raise ValueError('Unexpected preparation group layout')
+ del model, params, parameter
+ with closing(_merged_tensors(sources, groups[group])) as tensors:
+ converted, local_linears = quantize_group(tensors, eligible, official)
+ source_tensors_loaded += len(groups[group])
+ temporary = destination.with_suffix('.partial')
+ if temporary.exists():
+ # A previous interrupted group's partial is evidence, not a cache hit.
+ temporary.rename(temporary.with_name(temporary.name + '.retained-' + str(time.time_ns())))
+ save_file(converted, temporary)
+ tensor_count = len(converted)
+ del tensors, converted
+ gc.collect()
+ if destination.exists():
+ # Interrupted between final rename and receipt commit: keep the
+ # orphaned file while rebuilding this one group from pinned sources.
+ destination.rename(destination.with_name(destination.name + '.orphaned-' + str(time.time_ns())))
+ temporary.replace(destination)
+ row = {'group': group, 'file': group + '.safetensors', 'bytes': destination.stat().st_size,
+ 'sha256': sha256(destination), 'tensors': tensor_count, 'linears': local_linears}
+ pending = progress.with_suffix('.json.partial')
+ pending.write_text(json.dumps(row, indent=2) + '\n', encoding='utf-8')
+ pending.replace(progress)
+ records.append(row)
+ linears.update(local_linears)
+ torch.cuda.empty_cache()
+ print(json.dumps({'event': 'prepared_fp8_group', 'preparation': 'streamed', 'group': group,
+ 'index': index, 'total': len(group_names), 'reused': False,
+ 'bytes': row['bytes'], 'linears': len(local_linears),
+ 'seconds': time.monotonic() - started}), flush=True)
+ if not linears or (eligible is not None and set(linears) != eligible):
+ raise ValueError('Streamed FP8 cache omitted eligible Linears')
+ record = {'source_id': key, 'precision': 'fp8', 'scale_granularity': identity['scale_granularity'],
+ 'groups': records, 'linears': linears, 'identity': identity,
+ 'preparation_implementation_sha256': sha256(__file__),
+ 'merge_preparation_implementation_sha256': sha256(weights.__file__),
+ 'reused_groups': reused_groups, 'source_tensors_loaded': source_tensors_loaded,
+ 'preparation_seconds': time.monotonic() - started,
+ 'total_bytes': sum(row['bytes'] for row in records), 'bf16_intermediate_bytes': 0}
+ pending = output / 'manifest.partial'
+ pending.write_text(json.dumps(record, indent=2) + '\n', encoding='utf-8')
+ pending.replace(output / 'manifest.json')
+ print(json.dumps({'event': 'fp8_cache_ready', 'cache': str(output), 'bytes': record['total_bytes'],
+ 'bf16_intermediate_bytes': 0, 'reused_groups': reused_groups,
+ 'source_tensors_loaded': source_tensors_loaded}), flush=True)
+ return output
+
+
+def install_cached_linears(model, linears, *, weight_only=False):
+ from src.models.ops.fp8_linear import Fp8Linear, FP8_DTYPE
+ from .weight_only import WeightOnlyLinear
+ for name, spec in linears.items():
+ original = model.get_submodule(name)
+ if not isinstance(original, torch.nn.Linear):
+ raise ValueError('FP8 cache target is not a Linear: ' + name)
+ if list(original.weight.shape) != spec['weight_shape']:
+ raise ValueError('FP8 cache shape differs from the official model: ' + name)
+ kind = WeightOnlyLinear if weight_only else Fp8Linear
+ replacement = kind.__new__(kind)
+ torch.nn.Module.__init__(replacement)
+ replacement.register_buffer('weight_fp8', torch.empty(spec['weight_shape'], dtype=FP8_DTYPE, device='meta'))
+ replacement.register_buffer('weight_scale', torch.empty(spec['scale_shape'], dtype=torch.float32, device='meta'))
+ replacement.original = torch.nn.Module()
+ replacement.original.register_parameter('bias', original.bias)
+ replacement.in_features = original.in_features
+ replacement.out_features = original.out_features
+ # Newly injected hybrid modules start as FP32 in the meta skeleton;
+ # official assembly casts them to BF16 before FP8 conversion.
+ input_name = spec.get('input_dtype', str(original.weight.dtype).removeprefix('torch.'))
+ if input_name not in ('bfloat16', 'float16', 'float32'):
+ raise ValueError('Unexpected FP8 input dtype in cache: ' + input_name)
+ replacement.input_dtype = getattr(torch, input_name)
+ prefix, _, child = name.rpartition('.')
+ setattr(model.get_submodule(prefix) if prefix else model, child, replacement)
+
+
+def export_compact_model(model, cache_root, *, base, checkpoint):
+ """Save the already assembled official FP8 model, one group at a time.
+
+ This avoids loading and quantizing the same 70 GB again after a reference
+ render. No arithmetic or weight conversion is performed during export.
+ """
+ from src.models.ops import fp8_linear as official
+ if model.training or any(p.requires_grad for p in model.parameters()):
+ raise ValueError('Export requires a frozen inference model')
+ state = model.state_dict()
+ if any(name.endswith('.original.weight') for name in state):
+ raise ValueError('Export requires compact FP8 without the BF16 backups')
+ linears = {name: {'weight_shape': list(module.weight_fp8.shape),
+ 'scale_shape': list(module.weight_scale.shape), 'input_dtype': 'bfloat16'}
+ for name, module in model.named_modules() if isinstance(module, official.Fp8Linear)}
+ if not linears:
+ raise ValueError('No official FP8 matrices to export')
+ sources = Sources(Path(base), Path(checkpoint))
+ identity = {'origin': 'official assembled FP8; exact registered tensors',
+ 'implementation_sha256': sha256(__file__),
+ 'official_fp8_sha256': sha256(official.__file__),
+ 'base_config_sha256': sha256(Path(base) / 'transformer/config.json'),
+ 'model_spec_sha256': sha256(Path(checkpoint) / 'model_spec.json'),
+ 'torch': torch.__version__, 'scale_granularity': 'per_tensor' if official.per_tensor_gemm() else 'rowwise',
+ 'linears': linears,
+ 'sources': [{'path': str(p), 'bytes': p.stat().st_size, 'mtime_ns': p.stat().st_mtime_ns}
+ for p in sorted(sources.paths)]}
+ key = hashlib.sha256(json.dumps(identity, sort_keys=True).encode()).hexdigest()
+ output = Path(cache_root) / ('vdn-fp8-official-' + key[:16])
+ output.mkdir(parents=True, exist_ok=True)
+ marker = output / 'source.json'
+ if marker.exists() and json.loads(marker.read_text(encoding='utf-8')) != identity:
+ raise ValueError('Export cache identity mismatch')
+ marker.write_text(json.dumps(identity, indent=2) + '\n', encoding='utf-8')
+ groups = {}
+ for name, tensor in state.items():
+ groups.setdefault(group_name(name), {})[name] = tensor
+ records = []
+ started = time.perf_counter()
+ for group, tensors in sorted(groups.items()):
+ destination = output / (group + '.safetensors')
+ destination.parent.mkdir(parents=True, exist_ok=True)
+ progress = destination.with_suffix('.json')
+ record = json.loads(progress.read_text(encoding='utf-8')) if progress.exists() else None
+ if record and destination.is_file() and destination.stat().st_size == record['bytes'] and sha256(destination) == record['sha256']:
+ records.append(record)
+ continue
+ cpu = {name: tensor.detach().cpu().contiguous() for name, tensor in tensors.items()}
+ temporary = destination.with_suffix('.partial')
+ save_file(cpu, temporary)
+ temporary.replace(destination)
+ del cpu
+ record = {'group': group, 'file': group + '.safetensors', 'bytes': destination.stat().st_size,
+ 'sha256': sha256(destination), 'tensors': len(tensors)}
+ progress.write_text(json.dumps(record) + '\n', encoding='utf-8')
+ records.append(record)
+ print(json.dumps({'event': 'exported_official_fp8_group', 'group': group,
+ 'seconds': time.perf_counter() - started}), flush=True)
+ manifest = {'source_id': key, 'precision': 'fp8', 'scale_granularity': identity['scale_granularity'],
+ 'groups': records, 'linears': linears, 'total_bytes': sum(r['bytes'] for r in records),
+ 'preparation_seconds': time.perf_counter() - started, 'identity': identity}
+ temporary = output / 'manifest.partial'
+ temporary.write_text(json.dumps(manifest, indent=2) + '\n', encoding='utf-8')
+ temporary.replace(output / 'manifest.json')
+ return output
+
+
+def prepare(source, cache_root, *, base, checkpoint):
+ from .paths import add_vdn
+ add_vdn()
+ from src.models.ops import fp8_linear as official
+ source, cache_root = Path(source), Path(cache_root)
+ manifest = json.loads((source / 'manifest.json').read_text(encoding='utf-8'))
+ if manifest.get('precision', 'bf16') != 'bf16':
+ raise ValueError('FP8 preparation requires the verified BF16 merged cache')
+ per_tensor = official.per_tensor_gemm()
+ identity = {'source_id': manifest['source_id'],
+ 'source_manifest_sha256': sha256(source / 'manifest.json'),
+ 'implementation_sha256': sha256(__file__),
+ 'official_fp8_sha256': sha256(official.__file__),
+ 'torch': torch.__version__, 'scale_granularity': 'per_tensor' if per_tensor else 'rowwise',
+ 'min_width': official.MIN_WIDTH, 'skip_end_blocks': 0,
+ 'memory_policy': 'discard BF16 backup after official CUDA quantization; immutable per-group files'}
+ output, identity, key = cache_location(cache_root, 'vdn-fp8-', identity)
+ output.mkdir(parents=True, exist_ok=True)
+ marker = output / 'source.json'
+ if marker.exists() and json.loads(marker.read_text(encoding='utf-8')) != identity:
+ raise ValueError('FP8 cache identity conflict')
+ marker.write_text(json.dumps(identity, indent=2) + '\n', encoding='utf-8')
+ eligible = None
+ linears, groups = {}, []
+ started = time.perf_counter()
+ if sorted(g['group'] for g in manifest['groups']) != expected_groups(Path(base)):
+ raise ValueError('Unexpected BF16 preparation group layout')
+ for index, group in enumerate(manifest['groups'], 1):
+ if group['file'] != group['group'] + '.safetensors':
+ raise ValueError('Unexpected source group path')
+ destination = output / group['file']
+ destination.parent.mkdir(parents=True, exist_ok=True)
+ progress = destination.with_suffix('.json')
+ saved = reusable_group(output, group['group'])
+ if saved is not None:
+ groups.append(saved)
+ linears.update(saved['linears'])
+ print(json.dumps({'event': 'prepared_fp8_group', 'group': group['group'], 'index': index,
+ 'total': len(manifest['groups']), 'reused': True}), flush=True)
+ continue
+ if destination.is_file() and progress.is_file():
+ rejected = '.hash-rejected-' + str(time.time_ns())
+ destination.rename(destination.with_name(destination.name + rejected))
+ progress.rename(progress.with_name(progress.name + rejected))
+ path = source / group['file']
+ if path.stat().st_size != group['bytes'] or sha256(path) != group['sha256']:
+ raise ValueError('Source cache failed integrity check: ' + str(path))
+ if eligible is None:
+ model = skeleton(Path(base), Path(checkpoint))
+ if any(not parameter.is_meta for parameter in model.parameters()):
+ raise ValueError('Preparation skeleton materialized parameters')
+ eligible = {name for name, module in model.named_modules()
+ if isinstance(module, torch.nn.Linear)
+ and min(module.in_features, module.out_features) >= official.MIN_WIDTH}
+ del model
+ with closing(_file_tensors(path)) as values:
+ converted, local_linears = quantize_group(values, eligible, official)
+ temporary = destination.with_suffix('.partial')
+ if temporary.exists():
+ temporary.rename(temporary.with_name(temporary.name + '.retained-' + str(time.time_ns())))
+ if not local_linears:
+ # AdaLN's input is below the official FP8 threshold. Preserve its
+ # immutable BF16 files without making another 26 GB disk copy.
+ os.link(path, temporary)
+ else:
+ save_file(converted, temporary)
+ if destination.exists():
+ destination.rename(destination.with_name(destination.name + '.orphaned-' + str(time.time_ns())))
+ temporary.replace(destination)
+ saved = {'group': group['group'], 'file': group['file'],
+ 'bytes': destination.stat().st_size, 'sha256': sha256(destination),
+ 'tensors': len(converted), 'linears': local_linears}
+ progress.write_text(json.dumps(saved, indent=2) + '\n', encoding='utf-8')
+ groups.append(saved)
+ linears.update(local_linears)
+ del values, converted
+ gc.collect()
+ torch.cuda.empty_cache()
+ print(json.dumps({'event': 'prepared_fp8_group', 'group': group['group'],
+ 'bytes': saved['bytes'], 'linears': len(local_linears),
+ 'seconds': time.perf_counter() - started}), flush=True)
+ if not linears or eligible is not None and set(linears) != eligible:
+ raise ValueError('FP8 cache omitted eligible Linears')
+ record = {'source_id': key, 'bf16_source_id': manifest['source_id'], 'precision': 'fp8',
+ 'scale_granularity': identity['scale_granularity'], 'groups': groups, 'linears': linears,
+ 'preparation_implementation_sha256': sha256(__file__),
+ 'preparation_seconds': time.perf_counter() - started,
+ 'total_bytes': sum(group['bytes'] for group in groups), 'identity': identity}
+ (output / 'manifest.json').write_text(json.dumps(record, indent=2) + '\n', encoding='utf-8')
+ print(json.dumps({'event': 'fp8_cache_ready', 'cache': str(output), 'linears': len(linears),
+ 'bytes': record['total_bytes']}), flush=True)
+ return output
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--source', required=True)
+ parser.add_argument('--cache-root', required=True)
+ parser.add_argument('--base', required=True)
+ parser.add_argument('--checkpoint', required=True)
+ args = parser.parse_args()
+ torch.set_num_threads(8)
+ prepare(args.source, args.cache_root, base=args.base, checkpoint=args.checkpoint)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freevideo_engine/fp8_gemm.py b/freevideo_engine/fp8_gemm.py
new file mode 100644
index 0000000..3d2e890
--- /dev/null
+++ b/freevideo_engine/fp8_gemm.py
@@ -0,0 +1,137 @@
+"""Bounded FP8 GEMM for wheels without PyTorch's rowwise CUTLASS build.
+
+PyTorch 2.13 excludes RowwiseScaledMM.cu on Windows, including SM89.
+cuBLAS scalar-scale FP8 GEMM is still available. Keep the official quantized
+operands and apply their row/column scales to its FP32 accumulator in tiles.
+This is FP8 compute, with no BF16 weight copy or scale recalibration.
+"""
+from functools import lru_cache
+import types
+
+import torch
+import triton
+import triton.language as tl
+
+from .kernel_capabilities import fp8_implementation
+
+ACCUMULATOR_BYTES = 32 * 1024 * 1024
+_CALLS = dict(torch_scaled_mm=0, cublas_fp8=0, rescale=0, triton_ff_up=0)
+
+
+def execution_counts():
+ """Submitted kernel calls, without synchronizing or retaining tensors."""
+ return dict(_CALLS)
+
+
+@lru_cache(maxsize=16)
+def _fused_ff_device(device):
+ return torch.cuda.get_device_capability(device) == (8, 9)
+
+
+def _fused_rowwise_ff_up(X, W, XS, WS, OUT, M: tl.constexpr, N: tl.constexpr,
+ K: tl.constexpr, BM: tl.constexpr, BN: tl.constexpr,
+ BK: tl.constexpr):
+ rows = tl.program_id(0) * BM + tl.arange(0, BM)
+ cols = tl.program_id(1) * BN + tl.arange(0, BN)
+ kk = tl.arange(0, BK)
+ xp = X + rows[:, None] * K + kk[None, :]
+ wp = W + cols[None, :] * K + kk[:, None]
+ accum = tl.full((BM, BN), 0, tl.float32)
+ for k in range(tl.cdiv(K, BK)):
+ x = tl.load(xp, (rows[:, None] < M) & (kk[None, :] + k * BK < K), other=0.)
+ w = tl.load(wp, (cols[None, :] < N) & (kk[:, None] + k * BK < K), other=0.)
+ accum = tl.dot(x, w, accum)
+ xp += BK
+ wp += BK
+ xs = tl.load(XS + rows, rows < M, other=1.)
+ ws = tl.load(WS + cols, cols < N, other=1.)
+ result = (accum * xs[:, None]) * ws[None, :]
+ tl.store(OUT + rows[:, None] * N + cols[None, :], result.to(OUT.dtype.element_ty),
+ (rows[:, None] < M) & (cols[None, :] < N))
+
+
+def _rescale(A, X_SCALE, W_SCALE, OUT, COUNT: tl.constexpr, WIDTH: tl.constexpr,
+ X_ROWWISE: tl.constexpr, W_COLUMNWISE: tl.constexpr, BLOCK: tl.constexpr):
+ offsets = tl.program_id(0).to(tl.int64) * BLOCK + tl.arange(0, BLOCK)
+ valid = offsets < COUNT
+ value = tl.load(A + offsets, valid, other=0.)
+ xs = tl.load(X_SCALE + offsets // WIDTH, valid, other=1.) if X_ROWWISE else tl.load(X_SCALE)
+ ws = tl.load(W_SCALE + offsets % WIDTH, valid, other=1.) if W_COLUMNWISE else tl.load(W_SCALE)
+ # Match the CUTLASS epilogue order; cast before the caller adds bias.
+ value = (value * xs) * ws
+ tl.store(OUT + offsets, value.to(OUT.dtype.element_ty), valid)
+
+
+def scaled_mm(x, weight_t, scale_a, scale_b, *, out_dtype, implementation='torch',
+ accumulator_bytes=ACCUMULATOR_BYTES):
+ if implementation == 'torch':
+ _CALLS['torch_scaled_mm'] += 1
+ return torch._scaled_mm(x, weight_t, scale_a=scale_a, scale_b=scale_b,
+ out_dtype=out_dtype, use_fast_accum=True)
+ if implementation != 'scaled-mm-epilogue':
+ raise ValueError('Unknown FP8 GEMM implementation: ' + implementation)
+ rows, width = x.shape[0], weight_t.shape[1]
+ if (x.ndim != 2 or weight_t.ndim != 2 or x.shape[1] != weight_t.shape[0]
+ or scale_a.numel() not in (1, rows) or scale_b.numel() not in (1, width)
+ or scale_a.dtype != torch.float32 or scale_b.dtype != torch.float32):
+ raise ValueError('Expected FP8 matrices with FP32 scalar or row/column scales')
+ tile_rows = accumulator_bytes // (4 * width) if width else 0
+ if tile_rows < 1:
+ raise ValueError('FP8 accumulator budget cannot hold one output row')
+ # The full H3 FF-up tile benefits on native Windows Ada; its small tail
+ # does not. Preserve the other shapes and native Torch dispatch. Fuse the
+ # same FP32 row/column scaling and BF16 rounding, before the caller's bias,
+ # without allocating an FP32 accumulator in global memory.
+ if (out_dtype == torch.bfloat16 and x.shape == (2048, 5376)
+ and weight_t.shape == (5376, 28672) and x.is_cuda
+ and x.dtype == weight_t.dtype == torch.float8_e4m3fn
+ and x.is_contiguous() and weight_t.t().is_contiguous()
+ and scale_a.numel() == rows and scale_b.numel() == width
+ and _fused_ff_device(x.device)):
+ output = torch.empty((rows, width), dtype=out_dtype, device=x.device)
+ _fused_rowwise_ff_up[(16, 224)](
+ x, weight_t.t(), scale_a.contiguous(), scale_b.contiguous(), output,
+ rows, width, x.shape[1], BM=128, BN=128, BK=64,
+ num_warps=8, num_stages=3, enable_fp_fusion=False)
+ _CALLS['triton_ff_up'] += 1
+ return output
+ # Keep normal tiles aligned; PyTorch also accepts the final partial tile.
+ tile_rows = tile_rows // 16 * 16 if tile_rows >= 16 else tile_rows
+ output = torch.empty((rows, width), dtype=out_dtype, device=x.device)
+ unit = torch.ones((1, 1), dtype=torch.float32, device=x.device)
+ scale_a, scale_b = scale_a.contiguous(), scale_b.contiguous()
+ for start in range(0, rows, tile_rows):
+ stop = min(rows, start + tile_rows)
+ raw = torch._scaled_mm(x[start:stop], weight_t, scale_a=unit, scale_b=unit,
+ out_dtype=torch.float32, use_fast_accum=True)
+ _CALLS['cublas_fp8'] += 1
+ xs = scale_a if scale_a.numel() == 1 else scale_a.reshape(-1)[start:stop]
+ _rescale[(triton.cdiv(raw.numel(), 1024),)](
+ raw, xs, scale_b, output[start:stop], raw.numel(), width,
+ scale_a.numel() != 1, scale_b.numel() != 1, BLOCK=1024,
+ enable_fp_fusion=False)
+ _CALLS['rescale'] += 1
+ del raw, xs
+ return output
+
+
+def _forward_quantized(module, x_fp8, x_scale, out_dtype=None):
+ value = scaled_mm(x_fp8, module.weight_fp8.t(), x_scale, module.weight_scale,
+ out_dtype=out_dtype or torch.bfloat16,
+ implementation=module.freevideo_fp8_gemm)
+ return value if module.bias is None else value + module.bias
+
+
+def install(model, scale_granularity, *, requested='auto', system=None):
+ """Bind every official projection, including fused FF and sliced heads."""
+ import platform
+ from src.models.ops.fp8_linear import Fp8Linear
+ selected = fp8_implementation(system or platform.system(), scale_granularity, requested)
+ for module in model.modules():
+ if isinstance(module, Fp8Linear):
+ module.freevideo_fp8_gemm = selected
+ if selected != 'torch':
+ module.forward_quantized = types.MethodType(_forward_quantized, module)
+ return selected
diff --git a/freevideo_engine/fp8_ops.py b/freevideo_engine/fp8_ops.py
new file mode 100644
index 0000000..f14ef6b
--- /dev/null
+++ b/freevideo_engine/fp8_ops.py
@@ -0,0 +1,133 @@
+"""Bounded projections without changing the official FP8 activation scale scope."""
+import types
+
+import torch
+import triton
+import triton.language as tl
+
+from src.models.ops import fp8_linear as official
+
+
+def _cast_with_scale(X, Y, S, N, K: tl.constexpr, ROWWISE: tl.constexpr, BLOCK: tl.constexpr):
+ offsets = tl.program_id(0).to(tl.int64) * BLOCK + tl.arange(0, BLOCK)
+ mask = offsets < N
+ scale = tl.load(S + offsets // K, mask=mask, other=1.) if ROWWISE else tl.load(S)
+ value = tl.load(X + offsets, mask=mask, other=0.).to(tl.float32) / scale
+ value = tl.minimum(tl.maximum(value, -448.), 448.)
+ tl.store(Y + offsets, value.to(Y.dtype.element_ty), mask=mask)
+
+
+def quantize_fixed_scale(value, scale):
+ rows = value.reshape(-1, value.shape[-1]).contiguous()
+ if scale.numel() not in (1, len(rows)):
+ raise ValueError('Expected a global scale or one scale for every row')
+ result = torch.empty_like(rows, dtype=official.FP8_DTYPE)
+ _cast_with_scale[(triton.cdiv(rows.numel(), 8192),)](
+ rows, result, scale.contiguous(), rows.numel(), rows.shape[1], scale.numel() != 1,
+ BLOCK=8192, num_warps=8)
+ return result, scale
+
+
+class ColumnScale:
+ """Accumulate the scale of a matrix generated as disjoint head columns."""
+ def __init__(self, rows, device):
+ self.per_tensor = official.per_tensor_gemm()
+ self.amax = torch.zeros(1 if self.per_tensor else rows, device=device, dtype=torch.float32)
+
+ def update(self, columns):
+ columns = columns.contiguous()
+ if self.per_tensor:
+ official._absmax_kernel[(triton.cdiv(columns.numel(), 8192),)](
+ columns.view(-1), self.amax, columns.numel(), BLOCK=8192, num_warps=8)
+ else:
+ self.amax.copy_(torch.maximum(self.amax, columns.abs().amax(dim=1).float()))
+
+ def finish(self):
+ return (self.amax / official._FP8_MAX).clamp_min(1e-12).reshape(-1, 1)
+
+
+def row_scale(scale, rows):
+ return scale if scale.numel() == 1 else scale[rows].contiguous()
+
+
+def project_with_scale(module, value, scale=None):
+ if scale is None or not isinstance(module, official.Fp8Linear):
+ return module(value)
+ return module.forward_quantized(*quantize_fixed_scale(value, scale), out_dtype=value.dtype)
+
+
+def sliced_projection(module, value, channels, quantized=None):
+ from .weight_only import WeightOnlyLinear
+ if isinstance(module, WeightOnlyLinear):
+ return module.project(value, channels)
+ if not isinstance(module, official.Fp8Linear):
+ return torch.nn.functional.linear(value, module.weight[channels],
+ None if module.bias is None else module.bias[channels])
+ quantized = official.quantize_activation(value) if quantized is None else quantized
+ x_fp8, scale = quantized
+ weight_scale = module.weight_scale if module.weight_scale.numel() == 1 else module.weight_scale[:, channels].contiguous()
+ from .fp8_gemm import scaled_mm
+ result = scaled_mm(x_fp8, module.weight_fp8[channels].t(), scale, weight_scale,
+ out_dtype=value.dtype, implementation=getattr(module, 'freevideo_fp8_gemm', 'torch'))
+ return result if module.bias is None else result + module.bias[channels]
+
+
+def install_chunked_ff(module, chunk, recompute=False):
+ """Keep global scales on SM100+; optionally recompute tiles to avoid a 2 GB stash.
+
+ Rowwise-scale devices can finish each row tile immediately. A change in GEMM
+ row shape may still alter rounding, and is validated as a numerical ablation.
+ """
+ if chunk < 1:
+ raise ValueError('FP8 FF chunk must be positive')
+ up, _, down = module.net
+ if not isinstance(up.proj, official.Fp8Linear) or not isinstance(down, official.Fp8Linear):
+ raise ValueError('Chunked FP8 FF requires both official FP8 projections')
+
+ def forward(self, hidden):
+ shape = hidden.shape
+ rows = hidden.reshape(-1, shape[-1])
+ x_fp8, x_scale = official.quantize_activation(rows)
+ width = self.net[2].weight_fp8.shape[1]
+ output = torch.empty_like(rows)
+ if not official.per_tensor_gemm():
+ for start in range(0, len(rows), chunk):
+ section = slice(start, start + chunk)
+ h = self.net[0].proj.forward_quantized(x_fp8[section], row_scale(x_scale, section), out_dtype=rows.dtype)
+ output[section] = self.net[2].forward_quantized(*official.swiglu_quantize(h), out_dtype=rows.dtype)
+ return output.reshape(shape)
+ try:
+ activation = None if recompute else torch.empty((len(rows), width), device=rows.device, dtype=rows.dtype)
+ except torch.cuda.OutOfMemoryError as error:
+ # Identify this allocation precisely so automatic recovery can use
+ # the existing same-shape, global-scale recompute path. Other CUDA
+ # errors and unrelated OOMs are not relabelled as stash failures.
+ error.freevideo_allocation = dict(fp8_ff_activation_stash_bytes=len(rows)*width*rows.element_size())
+ raise
+ maxima = torch.empty(len(rows), device=rows.device, dtype=torch.float32)
+ for start in range(0, len(rows), chunk):
+ section = slice(start, min(start + chunk, len(rows)))
+ h = self.net[0].proj.forward_quantized(x_fp8[section], x_scale, out_dtype=rows.dtype)
+ tile = torch.empty((len(h), width), device=rows.device, dtype=rows.dtype) if recompute else activation[section]
+ official._swiglu_rowmax_kernel[(len(h),)](h, tile, maxima[section], width, BLOCK_K=2048, num_warps=16)
+ del h, tile
+ scale = (maxima.amax() / official._FP8_MAX).clamp_min(1e-12).reshape(1, 1)
+ del maxima
+ if not recompute:
+ del x_fp8, x_scale
+ for start in range(0, len(rows), chunk):
+ section = slice(start, min(start + chunk, len(rows)))
+ if recompute:
+ h = self.net[0].proj.forward_quantized(x_fp8[section], x_scale, out_dtype=rows.dtype)
+ tile = torch.empty((len(h), width), device=rows.device, dtype=rows.dtype)
+ unused_maxima = torch.empty(len(h), device=rows.device, dtype=torch.float32)
+ official._swiglu_rowmax_kernel[(len(h),)](h, tile, unused_maxima, width, BLOCK_K=2048, num_warps=16)
+ del h, unused_maxima
+ else:
+ tile = activation[section]
+ output[section] = self.net[2].forward_quantized(*quantize_fixed_scale(tile, scale), out_dtype=rows.dtype)
+ del tile
+ return output.reshape(shape)
+
+ module.forward = types.MethodType(forward, module)
diff --git a/freevideo_engine/generate.py b/freevideo_engine/generate.py
new file mode 100644
index 0000000..f524a3c
--- /dev/null
+++ b/freevideo_engine/generate.py
@@ -0,0 +1,705 @@
+"""One local FP8 request, reusing the native ComfyUI encoder in a separate child.
+
+Use a saved profile or select placement from current available resources.
+The parent imports no tensor runtime. Standalone requests use fresh workers;
+ComfyUI can reuse an isolated worker and reclaim its idle models. Both paths
+share the exclusive GPU-work lock and monitor the actual compute process.
+"""
+import argparse
+import json
+import os
+from pathlib import Path
+import signal
+import subprocess
+import sys
+import shutil
+import time
+from contextlib import nullcontext
+
+from .locking import runtime_lock, LOCK_ENV
+from .paths import vdn_root, comfy_root
+from .monitoring import save
+from . import tuning
+from . import processes
+from .system import (inference_headroom, inference_memory_sample, inference_environment, memory_sample,
+ windows, inference_pressure, inference_emergency_floor, ram_budget_is_estimate)
+from .adaptive import (WorkerExit, classify_failure, local_identity, history_path,
+ attempt_geometry, execute_attempts, complete_observation, benchmark_allocator_limit)
+from .resource_history import ResourceHistory
+
+
+def child(command, env, descriptor, log, *, ram_budget_bytes=None, gpu=None, on_start=None,
+ minimum_available_bytes=None, ram_budget_is_estimate=False):
+ from .ram import ProcessMemory
+ memory = ProcessMemory() if ram_budget_bytes is not None else None
+ telemetry = {}
+ monitor = None
+ with log.open('w', encoding='utf-8') as stream:
+ from .resident_process import ENV, RemoteProcess, SessionRestarted
+ module = command[command.index('-m')+1] if '-m' in command else None
+ process = None
+ if env.get(ENV) and module in ('freevideo_engine.worker', 'freevideo_engine.encode_worker'):
+ try:
+ process = RemoteProcess(env[ENV], command, env, descriptor, log)
+ except SessionRestarted as error:
+ env = dict(env)
+ env.pop(ENV, None)
+ os.environ.pop(ENV, None)
+ print(json.dumps({'event': 'resident_session_restart', 'reason': str(error)}), flush=True)
+ if process is None:
+ process = processes.popen(command, env=env, stdout=stream, stderr=subprocess.STDOUT,
+ pass_fds=(descriptor,), start_new_session=True)
+ pending_error = None
+ try:
+ if on_start:
+ on_start(process.pid)
+ if gpu:
+ from .monitoring import Monitor
+ monitor = Monitor(log.with_suffix('.gpu.csv'), device=gpu).start()
+ while process.poll() is None:
+ if memory:
+ sample = memory.sample(process.pid)
+ telemetry.setdefault('ram_start', sample)
+ telemetry['ram_last'] = sample
+ pressure = inference_pressure(sample, ram_budget_bytes, minimum_available_bytes,
+ budget_is_estimate=ram_budget_is_estimate)
+ telemetry['ram_guard'] = pressure
+ if pressure['reasons'] and not pressure['enforced_reasons']:
+ # Keep the first crossing even when later samples return
+ # below the estimate. Let the OS reclaim/page memory; do
+ # not trim another application's working set or alter its
+ # pagefile settings. One event per worker, not per sample.
+ if 'ram_budget_warning' not in telemetry:
+ telemetry['ram_budget_warning'] = dict(pressure)
+ print(json.dumps(dict(event='ram_budget_warning', **pressure)), flush=True)
+ if pressure['enforced_reasons']:
+ detail = ''
+ if pressure.get('commit_available_bytes') is not None:
+ if pressure.get('physical_available_bytes') is not None:
+ detail = ' Physical RAM available %.2f GiB;' % (pressure['physical_available_bytes']/2**30)
+ detail += ' Windows commit headroom %.3f GiB.' % (pressure['commit_available_bytes']/2**30)
+ if pressure['commit_available_bytes'] <= pressure['emergency_floor_bytes']:
+ detail += ' Windows commit headroom exhausted; this is separate from physical RAM usage.'
+ raise MemoryError(('RAM guard: %s. Working RAM %.2f GiB / budget %.2f GiB; '
+ 'available physical/commit headroom %.2f GiB / emergency floor %.3f GiB.' %
+ (', '.join(pressure['enforced_reasons']), pressure['working_bytes']/2**30,
+ ram_budget_bytes/2**30, pressure['available_bytes']/2**30,
+ pressure['emergency_floor_bytes']/2**30)) + detail)
+ try:
+ process.wait(timeout=.5)
+ except subprocess.TimeoutExpired:
+ pass
+ code = process.returncode
+ if code:
+ raise WorkerExit(code, log)
+ except BaseException as error:
+ pending_error = error
+ try:
+ processes.stop(process)
+ except BaseException as stop_error:
+ # A stop timeout is not a controlled timeout. The worker may
+ # still own CUDA; never convert this into an ordinary retry.
+ error.worker_stop_confirmed = False
+ error.worker_stop_error = repr(stop_error)
+ error.telemetry = telemetry
+ raise
+ finally:
+ cleanup_errors = []
+ for name, observed, suffix in (('ram', memory, '.memory.json'), ('gpu', monitor, '.gpu.json')):
+ if observed is not None:
+ try:
+ telemetry[name] = observed.result() if name == 'ram' else observed.stop()
+ save(log.with_suffix(suffix), telemetry[name])
+ except BaseException as error:
+ cleanup_errors.append(error)
+ if cleanup_errors:
+ if pending_error is None:
+ raise cleanup_errors[0]
+ pending_error.cleanup_errors = [repr(error) for error in cleanup_errors]
+ return telemetry
+
+
+def main():
+ from .cli import main as cli
+ sys.argv.insert(1, 'generate')
+ cli()
+
+
+def automatic_profile(args, canvas, *, stage, evidence, descriptor=None, environment=None):
+ """Plan with idle-cache accounting; reclaim once if it prevents admission."""
+ from .hardware import detect
+ from .policy import choose, ResourceBudgetError
+ from .kernel_capabilities import available_backends
+ from .resident_process import credit, release_idle_cache, ENV, SessionRestarted
+ environment = os.environ if environment is None else environment
+ for attempt in range(2):
+ raw = detect()
+ encoding_stage = stage == 'before-encoding' and bool(getattr(args, 'prompt_file', None))
+ hardware, state = credit(raw, environment, reuse_encoder=encoding_stage)
+ row = dict(stage=stage, raw=raw.to_dict(), adjusted=hardware.to_dict(), idle_cache=state)
+ from .system import system_memory
+ try:
+ row['system_memory'] = system_memory()
+ except (OSError, RuntimeError):
+ pass
+ evidence.append(row)
+ try:
+ # What this machine has demonstrably held, for the weight cache
+ # only. None on a first run, which leaves the plan unchanged.
+ from .adaptive import demonstrated_local_ram
+ options = {key: getattr(args, key) for key in
+ ('vram_gib', 'ram_gib', 'attention', 'gpu_reserve_gib', 'ram_reserve_gib')}
+ options.update(available_backends=available_backends(hardware, probe_missing=False), canvas=canvas,
+ demonstrated_ram_bytes=demonstrated_local_ram(hardware, args, canvas))
+ if encoding_stage:
+ options['stage'] = 'encoding'
+ try:
+ selected = choose(hardware, **options)
+ except ResourceBudgetError as estimate:
+ details = estimate.details
+ cache_cannot_resolve = (attempt or not state or not environment.get(ENV) or
+ details['gpu_capacity_bytes'] - details['gpu_reserve_bytes'] < details['gpu_minimum_bytes'])
+ if (details['insufficient'] != ['gpu'] or details.get('gpu_requirement') != 'estimated_working_set'
+ or not cache_cannot_resolve):
+ raise
+ selected = choose(hardware, **options, allow_capacity_trial=True)
+ row.update(capacity_trial=True, resource_error=details)
+ row['status'] = 'admitted'
+ profile = selected.legacy_profile()
+ row['budget_bytes'] = dict(gpu=profile['gpu_budget_gb']*1e9, ram=profile['inference_ram_budget_gb']*1e9)
+ row['reserve_bytes'] = dict(gpu=profile.get('policy', {}).get('gpu_system_reserve_bytes'),
+ ram=profile.get('policy', {}).get('ram_system_reserve_bytes'))
+ # Encoding is finished. Do not keep its process if its retained
+ # working set already leaves less than the model loader's existing
+ # 2 GiB increment inside the new whole-process RAM budget. Release
+ # under the lease, then actually measure recovered commit/physical
+ # capacity; never invent commit credit for GPU or mapped weights.
+ memory = (state.get('memory') or {}) if state else {}
+ used = memory.get('guard_bytes')
+ mapped = memory.get('reclaimable_mapped_bytes')
+ commit = memory.get('system_commit_available_bytes')
+ # A read-only text-encoder checkpoint is cheap in the working-set
+ # guard, but Windows still charges its section view to Commit until
+ # the resident worker exits. The old test only looked at
+ # ``guard_bytes`` and therefore retained a 14--16 GiB mapping on a
+ # 30 GiB machine: the next stage then had a 5.5 GiB budget and
+ # reopened roughly 20 GiB of transformer weights on every step.
+ # Releasing the idle worker is safe after conditioning is saved and
+ # is preferable to letting Commit fall through the emergency floor.
+ # Require both measurements so a missing/incomplete Windows sample
+ # never causes a speculative restart. On machines with genuine
+ # commit headroom the resident encoder remains reusable.
+ # Only Windows section views consume Commit while looking small in
+ # the working-set guard. Linux mappings are already represented by
+ # MemAvailable/cgroup accounting and must not trigger a Windows
+ # style encoder restart.
+ mapped_commit_pressure = (
+ hardware.system == 'Windows' and
+ type(mapped) is int and mapped >= 4 * 2**30 and
+ type(commit) is int and commit >= 0 and
+ commit < mapped + 2 * 2**30)
+ encoder_idle = state and any(m.get('role') == 'encoder' for m in state.get('models', []))
+ reclaim = (stage == 'after-encoding' and not attempt and encoder_idle and environment.get(ENV)
+ and ((type(used) is int and used >= 0
+ and used + 2*2**30 > row['budget_bytes']['ram'])
+ or mapped_commit_pressure))
+ if not reclaim:
+ return hardware, state, profile
+ reason = ('Idle encoder working set leaves insufficient model-loading room'
+ if type(used) is int and used >= 0
+ and used + 2*2**30 > row['budget_bytes']['ram']
+ else 'Idle encoder mapping leaves insufficient Windows Commit headroom')
+ row.update(status='reclaim-before-load', reason=reason,
+ mapped_bytes=mapped if type(mapped) is int else None,
+ commit_available_bytes=commit if type(commit) is int else None)
+ except ResourceBudgetError as error:
+ row.update(status='rejected', resource_error=error.details)
+ error.resource_planning = evidence
+ impossible_capacity = any(error.details[name+'_capacity_bytes'] - error.details[name+'_reserve_bytes']
+ < error.details[name+'_minimum_bytes'] for name in error.details['insufficient'])
+ if attempt or impossible_capacity or not state or not environment.get(ENV):
+ raise
+ print(json.dumps(dict(event='release_idle_cache', stage=stage,
+ reason='Cached models prevent next-stage loading; releasing them and measuring again',
+ resources=row.get('resource_error', row.get('budget_bytes')))), flush=True)
+ lease_scope = runtime_lock() if descriptor is None else nullcontext(descriptor)
+ with lease_scope as lease:
+ try:
+ row['recovery'] = release_idle_cache(environment, lease)
+ except SessionRestarted:
+ row['recovery'] = dict(released=True, reason='Old worker already confirmed stopped')
+ environment.pop(ENV, None)
+ os.environ.pop(ENV, None)
+
+
+def run(args):
+ """Also retain admission/import failures before the main request starts."""
+ output = Path(args.out).expanduser().resolve()
+ preexisting = any(path.exists() for path in (output, output.with_suffix('.request.json'),
+ output.with_suffix('.debug.json'), output.with_suffix('.artifacts')))
+ started = time.perf_counter()
+ try:
+ return _run(args)
+ except BaseException as error:
+ if not preexisting and not getattr(error, 'diagnostic_recorded', False):
+ from .diagnostic_resources import exception_details
+ from .support_report import write as write_debug, code_identity
+ planning = getattr(error, 'resource_planning', [])
+ record = dict(success=False, request_seconds=time.perf_counter()-started,
+ phase=planning[-1]['stage'] if planning else 'preflight', resource_planning=planning,
+ error_type=type(error).__name__, exception=exception_details(error),
+ error_message=str(error), resource_error=getattr(error, 'details', None), runtime_code=code_identity())
+ try:
+ save(output.with_suffix('.request.json'), record)
+ write_debug(output, record)
+ except (OSError, ValueError):
+ pass
+ raise
+
+
+def _run(args):
+ from .hardware import detect
+ from .geometry import geometry
+ canvas = geometry(getattr(args, 'width', 1344), getattr(args, 'height', 768),
+ frames=getattr(args, 'frames', None), seconds=getattr(args, 'seconds', None))
+ from .media_request import read as read_media, task_for, cache_key
+ media = read_media(getattr(args, 'media', None))
+ active_loras = [row for row in media.get('loras', []) if row['strength'] != 0]
+ if media:
+ inferred_task = task_for(media)
+ structural = any(media.get(k) for k in ('first', 'last', 'references', 'conditioning_info'))
+ if structural and getattr(args, 'task', None) and args.task != inferred_task:
+ raise ValueError('Explicit task disagrees with the media input')
+ args.task = inferred_task if structural or not getattr(args, 'task', None) else args.task
+ if args.conditioning and any(media.get(k) for k in ('first', 'last', 'references')):
+ raise ValueError('Preencoded conditioning already includes media; use conditioning_info, not another set of files')
+ base_cache = args.cache
+ from .resident_process import credit
+ planning = []
+ if args.profile:
+ hardware, resident_credit = credit(detect())
+ profile = json.loads(args.profile.read_text(encoding='utf-8'))
+ else:
+ hardware, resident_credit, profile = automatic_profile(args, canvas, stage='before-encoding', evidence=planning)
+ print(json.dumps(dict(event='compute_device', backend='CUDA', name=getattr(hardware, 'gpu_name', None),
+ uuid=getattr(hardware, 'gpu_uuid', None), vram_total_bytes=getattr(hardware, 'vram_total', None),
+ vram_free_bytes=getattr(hardware, 'vram_free', None))), flush=True)
+ allocator_limit_bytes = benchmark_allocator_limit(profile, getattr(args, 'allocator_limit_gib', None), hardware)
+ if 'gpu_budget_bytes' in profile:
+ profile = dict(gpu_budget_gb=profile['gpu_budget_bytes'] / 1e9,
+ inference_ram_budget_gb=profile['ram_budget_bytes'] / 1e9,
+ engine=profile['engine'], decoder=profile['decoder'],
+ allocator_config=profile['allocator_config'], policy=profile)
+ if args.base:
+ profile['engine']['base'] = str(args.base.resolve())
+ if args.checkpoint:
+ profile['engine']['checkpoint'] = str(args.checkpoint.resolve())
+ if getattr(args, 'task', None):
+ profile['engine']['task'] = args.task
+ if allocator_limit_bytes is not None:
+ profile['benchmark_allocator_limit_bytes'] = allocator_limit_bytes
+ if args.out.suffix.lower() != '.mp4':
+ raise ValueError('--out must name an MP4 file')
+ manifest = json.loads((args.cache / 'manifest.json').read_text(encoding='utf-8'))
+ if manifest.get('precision') != 'fp8':
+ raise ValueError('Select the prepared official FP8 cache')
+ destination = args.out.resolve()
+ if destination.exists():
+ raise FileExistsError(destination)
+ destination.parent.mkdir(parents=True, exist_ok=True)
+ artifacts = destination.with_suffix('.artifacts')
+ artifacts.mkdir(exist_ok=False)
+ if args.prompt_file:
+ shutil.copyfile(args.prompt_file, artifacts / 'prompt.txt')
+ repo = Path(__file__).resolve().parents[1]
+ report = {'profile': profile, 'seed': args.seed, 'success': False, 'geometry': canvas,
+ 'resource_planning': planning,
+ 'artifacts': str(artifacts),
+ 'idle_resources': dict(automatic=not bool(args.profile), system=hardware.system,
+ vram_gib=getattr(args, 'vram_gib', None),
+ gpu_reserve_gib=getattr(args, 'gpu_reserve_gib', None),
+ allocator_limit_bytes=allocator_limit_bytes),
+ 'reclaimable_resident_models': resident_credit,
+ 'encoder_mode': 'native H3 encoder library child' if args.prompt_file else 'preencoded shared conditioning'}
+ from .support_report import code_identity, write as write_debug
+ report['runtime_hardware'] = hardware.to_dict()
+ report['runtime_code'] = code_identity()
+ started = time.perf_counter()
+ applied = None
+ def interrupted(signum, frame):
+ raise KeyboardInterrupt(f'Request interrupted by signal {signum}')
+ previous = processes.termination_handler(interrupted)
+ try:
+ with runtime_lock() as descriptor:
+ if active_loras:
+ lora_request = artifacts / 'lora-request.json'
+ lora_result = artifacts / 'lora-result.json'
+ save(lora_request, {'cache': str(args.cache.resolve()), 'adapters': active_loras, 'result': str(lora_result)})
+ lora_env = dict(os.environ, PYTHONPATH=str(repo), CUDA_VISIBLE_DEVICES='', PYTHONUNBUFFERED='1')
+ lora_env[LOCK_ENV] = str(descriptor)
+ print(json.dumps({'event': 'lora_prepare_start'}), flush=True)
+ report['lora_resources'] = child([sys.executable, '-m', 'freevideo_engine.lora_cache', '--request', str(lora_request)],
+ lora_env, descriptor, destination.with_suffix('.lora.log'),
+ ram_budget_bytes=profile['inference_ram_budget_gb'] * 1e9)
+ prepared = json.loads(lora_result.read_text(encoding='utf-8'))
+ args.cache = Path(prepared['cache'])
+ report['lora'] = prepared['report']
+ resource_identity = local_identity(hardware, args.cache)
+ history = ResourceHistory(history_path())
+ history.recover_pending()
+ report['resource_history'] = str(history.path)
+ state = None
+ input_cache = None
+ report['tuning'] = {'profile_id': None, 'conditioning_cache_hit': False}
+ report['input_cache'] = {'enabled': False, 'conditioning_hit': False}
+ if not args.profile and not getattr(args, 'no_tuning', False):
+ machine_path = tuning.state_path().parents[1] / 'machine.json'
+ if machine_path.is_file():
+ try:
+ machine = json.loads(machine_path.read_text(encoding='utf-8'))
+ same_inputs = (base_cache.resolve() == Path(machine['cache']).resolve()
+ and args.encoder == machine['encoder']
+ and Path(args.comfy_python).absolute() == Path(machine['comfy_python']).absolute()
+ and (args.comfy_root or comfy_root()).resolve() == Path(machine['comfy_root']).resolve()
+ and args.model_paths and args.model_paths.resolve() == Path(machine['model_paths']).resolve())
+ if same_inputs:
+ identity = tuning.identity(machine, hardware)
+ state, reason = tuning.load_state(identity)
+ if active_loras:
+ state, reason = None, 'LoRA uses its own model history; encoder cache remains reusable'
+ from .input_cache import InputCache
+ input_cache = InputCache(identity)
+ report['input_cache'].update(enabled=True, directory=str(input_cache.root))
+ report['tuning']['note'] = reason
+ else:
+ report['tuning']['note'] = 'Explicit model/encoder paths differ from the optimized installation'
+ except (OSError, ValueError, KeyError, subprocess.SubprocessError) as error:
+ report['tuning']['note'] = 'Could not validate saved tuning: ' + str(error)
+ else:
+ report['tuning']['note'] = 'Saved optimization bypassed by --no-tuning or --profile'
+ if state:
+ report['tuning']['baseline_report'] = state.get('baseline_report')
+ report['tuning']['optimization_report'] = state.get('optimization_report')
+ save(destination.with_suffix('.request.json'), report)
+ temporary = artifacts
+ allocator = profile.get('allocator_config', os.environ.get('PYTORCH_ALLOC_CONF',
+ os.environ.get('PYTORCH_CUDA_ALLOC_CONF', 'backend:native,pinned_max_round_threshold_mb:128')))
+ profile['allocator_config'] = allocator
+ env = dict(os.environ, HF_HUB_OFFLINE='1', TRANSFORMERS_OFFLINE='1', PYTHONUNBUFFERED='1',
+ PYTORCH_ALLOC_CONF=allocator, PYTORCH_CUDA_ALLOC_CONF=allocator)
+ env[LOCK_ENV] = str(descriptor)
+ condition = temporary / 'conditioning.pt'
+ if args.conditioning:
+ shutil.copyfile(args.conditioning, condition)
+ if args.prompt_file:
+ encoding = {'prompt': args.prompt_file.read_text(encoding='utf-8'), 'encoder': args.encoder,
+ 'comfy_root': str(args.comfy_root or comfy_root()),
+ 'model_paths': str(args.model_paths.resolve()) if args.model_paths else None,
+ 'output': str(condition), 'gpu_budget_gb': profile['gpu_budget_gb'],
+ 'ram_budget_bytes': round(profile['inference_ram_budget_gb'] * 1e9),
+ 'metrics': str(destination.with_suffix('.encoding.json')),
+ 'diagnostic_output': str(destination)}
+ if media:
+ encoding.update(media=media, geometry=canvas, base=profile['engine'].get('base'))
+ conditioning_key = cache_key(encoding['prompt'], media, canvas)
+ encode_request = temporary / 'encode.json'
+ encode_request.write_text(json.dumps(encoding) + '\n', encoding='utf-8')
+ cached = input_cache.get(conditioning_key, condition) if input_cache else None
+ if cached and (cached.get('success') is not True or cached.get('conditioning_shape', [])[1:] != [5120]):
+ condition.rename(condition.with_name('conditioning.cache-rejected-' + str(time.time_ns()) + '.pt'))
+ cached = None
+ if not cached and state and task_for(media) == 't2va':
+ cached = tuning.cached_conditioning(state, encoding['prompt'], condition)
+ if cached:
+ save(destination.with_suffix('.encoding.json'), cached)
+ destination.with_suffix('.encoding.log').write_text('Reused validated local text conditioning; encoder model was not loaded.\n', encoding='utf-8')
+ report['tuning']['conditioning_cache_hit'] = True
+ report['input_cache']['conditioning_hit'] = True
+ print(json.dumps({'event': 'conditioning_cache_hit'}), flush=True)
+ else:
+ encoder_env = dict(env, PYTHONPATH=str(repo))
+ print(json.dumps({'event': 'encoding_start'}), flush=True)
+ encoder_attempt = history.begin(resource_identity, {'encoder': args.encoder},
+ attempt_geometry(canvas, profile['engine']), purpose='encoding',
+ evidence={'reason': 'Text conditioning cache miss',
+ 'request_artifacts': str(artifacts)})
+ report['encoding_attempt'] = encoder_attempt
+ save(destination.with_suffix('.request.json'), report)
+ try:
+ report['encoding_resources'] = child(
+ [args.comfy_python, '-m', 'freevideo_engine.encode_worker', '--request', str(encode_request)],
+ encoder_env, descriptor, destination.with_suffix('.encoding.log'),
+ ram_budget_bytes=profile['inference_ram_budget_gb'] * 1e9,
+ ram_budget_is_estimate=ram_budget_is_estimate(args),
+ minimum_available_bytes=inference_emergency_floor(profile['inference_ram_budget_gb'] * 1e9,
+ getattr(args, 'ram_reserve_gib', None)),
+ on_start=lambda pid: history.attach_worker(encoder_attempt, pid, phase='encoding'))
+ encoded = json.loads(destination.with_suffix('.encoding.json').read_text(encoding='utf-8'))
+ if encoded.get('success') is not True or not condition.is_file():
+ raise ValueError('Encoder exited without verified conditioning')
+ except BaseException as error:
+ try:
+ failed_encoding = json.loads(destination.with_suffix('.encoding.json').read_text(encoding='utf-8'))
+ except (OSError, ValueError):
+ failed_encoding = {}
+ failure = classify_failure(error, failed_encoding)
+ report['encoding_resources'] = getattr(error, 'telemetry', {})
+ report['encoding_failure'] = dict(phase=failed_encoding.get('phase', 'encoding'), failure=failure)
+ history.finish(encoder_attempt, failure['outcome'], details=failure)
+ raise
+ history.finish(encoder_attempt, 'success', details={'reason': 'Encoder exited with conditioning',
+ 'encoding': encoded, 'resources': report.get('encoding_resources'),
+ 'prompt_characters': len(encoding['prompt'])})
+ report['encoding'] = json.loads(destination.with_suffix('.encoding.json').read_text(encoding='utf-8'))
+ report['encoder_process_exited_before_video'] = not bool(os.environ.get('FREEVIDEO_RESIDENT_SESSION'))
+ if input_cache and not cached:
+ try:
+ report['input_cache']['conditioning_saved'] = input_cache.put(conditioning_key, condition, report['encoding'])
+ if input_cache.note:
+ report['input_cache']['note'] = input_cache.note
+ except (OSError, ValueError) as error:
+ report['input_cache']['note'] = str(error)
+ if state and not cached and task_for(media) == 't2va':
+ try:
+ tuning.remember_conditioning(state, encoding['prompt'], condition, report['encoding'])
+ tuning.save_state(state)
+ except (OSError, ValueError) as error:
+ report['tuning']['cache_note'] = str(error)
+ info = report.get('encoding', {}).get('conditioning_info') or media.get('conditioning_info')
+ if info:
+ if any(info.get(k) != canvas[k] for k in ('width', 'height', 'frames')):
+ raise ValueError('Encoded media does not match the requested geometry')
+ for name in ('reference_video_tokens', 'reference_audio_tokens'):
+ count = info.get(name, 0)
+ if type(count) is not int or count < 0:
+ raise ValueError('Invalid reference token count')
+ canvas[name] = count
+ profile['engine']['task'] = info['task']
+ if not condition.is_file():
+ raise FileNotFoundError(condition)
+ from .two_pass import plan as plan_sampling, ensure_checkpoint
+ sampling_plan = plan_sampling(canvas, getattr(args, 'two_pass', True),
+ profile['engine'].get('task', 't2va'))
+ report['sampling_plan'] = sampling_plan
+ if sampling_plan['enabled']:
+ if profile['engine'].get('steps', 8) != 8:
+ raise ValueError('Automatic two-pass generation requires the original DMD8 schedule')
+ canvas['sampling_plan'] = sampling_plan
+ print(json.dumps(dict(event='sampling_plan', **sampling_plan)), flush=True)
+ save(destination.with_suffix('.request.json'), report)
+ upscaler_checkpoint = None
+ if sampling_plan.get('upscaler_sha256'):
+ print(json.dumps(dict(event='latent_upscaler_prepare')), flush=True)
+ upscaler_checkpoint = str(ensure_checkpoint())
+ if not args.profile:
+ # Encoding can take minutes; always recheck current free memory,
+ # even before any optimization has been saved.
+ hardware, resident_credit, live = automatic_profile(args, canvas, stage='after-encoding',
+ evidence=planning, descriptor=descriptor, environment=env)
+ report['reclaimable_resident_models'] = resident_credit
+ live['engine'].update({k: profile['engine'][k] for k in ('base', 'checkpoint', 'task') if k in profile['engine']})
+ profile = live
+ report['profile'] = profile
+ from .prediction import predict
+ from .prediction_ui import show as show_prediction, errors as prediction_errors
+ condition_hash = tuning.digest(condition)
+ observations = history.observations(identity=resource_identity, limit=512, include_details=False)
+ tokens = info.get('text_tokens') if info else report.get('encoding', {}).get('conditioning_shape', [None])[0]
+ if tokens is None:
+ tokens = next((row['observation'].get('text_tokens', row.get('geometry', {}).get('text_tokens'))
+ for row in reversed(observations)
+ if row['observation'].get('conditioning_sha256') == condition_hash), None)
+ if state and args.attention == 'auto' and tokens is not None:
+ profile, applied = tuning.apply_profile(state, profile, canvas, tokens)
+ report['profile'] = profile
+ report['tuning']['profile_id'] = applied
+ if allocator_limit_bytes is not None:
+ profile['benchmark_allocator_limit_bytes'] = allocator_limit_bytes
+ if not args.profile and not getattr(args, 'no_history_placement', False):
+ from .prediction_policy import select
+ profile, decision = select(profile, canvas, hardware, observations, resource_identity,
+ text_tokens=tokens)
+ report['history_placement'] = decision
+ report['profile'] = profile
+ from .compatibility import Store as CompatibilityStore, apply as apply_compatibility
+ compatibility_state = CompatibilityStore(history.path.parent).status(resource_identity, persist=True)
+ profile, report['compatibility'] = apply_compatibility(profile, compatibility_state)
+ report['profile'] = profile
+ if compatibility_state['level']:
+ print(json.dumps(dict(event='compatibility', **report['compatibility'])), flush=True)
+ def forecast(selected):
+ if not args.profile and report.get('resource_attempts'):
+ # A failed resident worker may have exited and returned tens
+ # of GiB. Refresh limits in both directions; keep the chosen
+ # recovery options so a replan cannot undo offload/recompute.
+ _, _, live = automatic_profile(args, canvas,
+ stage='retry-%d' % (len(report['resource_attempts']) + 1), evidence=planning,
+ descriptor=descriptor, environment=engine_env)
+ for key in ('gpu_budget_gb', 'inference_ram_budget_gb'):
+ selected[key] = live[key]
+ if 'policy' in selected:
+ selected['policy'].update({k: v for k, v in live.get('policy', {}).items()
+ if k not in ('engine', 'decoder')})
+ report['resource_prediction'] = predict(observations, resource_identity, selected, canvas, text_tokens=tokens)
+ show_prediction(report['resource_prediction'])
+ def validate(metrics, telemetry, selected):
+ observation = complete_observation(metrics, canvas, destination, artifacts, telemetry)
+ if observation is not None:
+ observation['conditioning_sha256'] = condition_hash
+ report['resource_prediction_error'] = prediction_errors(report['resource_prediction'], observation)
+ return observation
+ # A live policy may have replaced the initial profile after text
+ # encoding. Record and execute that exact allocator consistently.
+ allocator = profile.get('allocator_config', allocator)
+ profile['allocator_config'] = allocator
+ env.update(PYTORCH_ALLOC_CONF=allocator, PYTORCH_CUDA_ALLOC_CONF=allocator)
+ save(destination.with_suffix('.request.json'), report)
+ request = {'conditioning': str(condition), 'seed': args.seed, 'cache': str(args.cache.resolve()),
+ 'input_cache_dir': str(input_cache.root.parent.parent) if input_cache else None,
+ 'gpu_budget_bytes': round(profile['gpu_budget_gb'] * 1e9), 'engine_options': profile['engine'],
+ 'decoder_options': profile['decoder'], 'output': str(destination),
+ 'geometry': canvas, 'artifacts': str(artifacts),
+ 'sampling_plan': sampling_plan, 'upscaler_checkpoint': upscaler_checkpoint,
+ 'automatic_pass_cache': not bool(args.profile),
+ 'metrics': str(destination.with_suffix('.engine.json'))}
+ if allocator_limit_bytes is not None:
+ request['allocator_limit_bytes'] = allocator_limit_bytes
+ report['benchmark_limit'] = {'pytorch_allocator_bytes': request['allocator_limit_bytes'],
+ 'scope': 'PyTorch CUDA allocator only; other CUDA allocations and whole-device usage are measured separately.'}
+ request_path = temporary / 'video.json'
+ resume_decode = None
+ # Keep explicit dependency overlays while keeping the snapshotted engine first.
+ engine_env = dict(env, PYTHONPATH=os.pathsep.join((str(repo), str(vdn_root()),
+ env.get('PYTHONPATH', ''))))
+ def persist():
+ save(destination.with_suffix('.request.json'), report)
+ def launch(selected, attempt):
+ selected_env = inference_environment(engine_env, selected['inference_ram_budget_gb'] * 1e9)
+ request.update(engine_options=selected['engine'], decoder_options=selected['decoder'],
+ ram_budget_bytes=round(selected['inference_ram_budget_gb'] * 1e9),
+ gpu_budget_bytes=round(selected['gpu_budget_gb'] * 1e9),
+ gpu_reserve_bytes=selected.get('policy', {}).get('gpu_system_reserve_bytes', 0),
+ capacity_trial=bool(selected.get('policy', {}).get('capacity_trial')),
+ resource_attempt=attempt)
+ request.pop('first_pass_policy', None)
+ request['automatic_pass_cache'] = not bool(args.profile) and len(report.get('resource_attempts', [])) <= 1
+ # Explicit profiles and recovery placements retain their cap.
+ # Automatic Windows requests can use a later driver-budget rise
+ # without exceeding a user/benchmark capacity constraint.
+ request['dynamic_gpu_budget'] = (hardware.system == 'Windows' and
+ request['automatic_pass_cache'] and getattr(args, 'gpu_reserve_gib', None) is None)
+ request['gpu_capacity_bytes'] = min(hardware.vram_total,
+ int(args.vram_gib * 2**30) if args.vram_gib else hardware.vram_total)
+ if request['automatic_pass_cache']:
+ from .two_pass import first_pass_policy
+ first_policy = first_pass_policy(selected, canvas, sampling_plan)
+ if first_policy is not None:
+ first_policy['profile'], _ = apply_compatibility(first_policy['profile'], compatibility_state)
+ request['first_pass_policy'] = first_policy
+ report['first_pass_policy'] = first_policy
+ if resume_decode is not None:
+ request['resume_decode'] = dict(resume_decode)
+ else:
+ request.pop('resume_decode', None)
+ save(request_path, request)
+ print(json.dumps({'event': 'decode_resume' if resume_decode else 'video_start', 'resource_attempt': attempt,
+ 'engine': selected['engine'], 'decoder': selected['decoder']}), flush=True)
+ try:
+ telemetry = child([sys.executable, '-m', 'freevideo_engine.worker', '--request', str(request_path)],
+ selected_env, descriptor, destination.with_suffix('.engine.log'),
+ ram_budget_bytes=selected['inference_ram_budget_gb'] * 1e9,
+ ram_budget_is_estimate=ram_budget_is_estimate(args),
+ minimum_available_bytes=inference_emergency_floor(selected['inference_ram_budget_gb'] * 1e9,
+ getattr(args, 'ram_reserve_gib', None)),
+ gpu=resource_identity['gpu']['uuid'],
+ on_start=lambda pid: history.attach_worker(attempt, pid, phase='video'))
+ except BaseException as error:
+ try:
+ error.metrics = json.loads(destination.with_suffix('.engine.json').read_text(encoding='utf-8'))
+ except (OSError, ValueError):
+ pass
+ if isinstance(getattr(error, 'metrics', None), dict) and not error.metrics.get('sampling_memory'):
+ try:
+ from .diagnostics import read_bounded
+ raw, truncated = read_bounded(destination.with_suffix('.sampling-memory.json'), 2 * 2**20)
+ observed = json.loads(raw) if not truncated else None
+ if isinstance(observed, dict):
+ error.metrics['sampling_memory'] = observed
+ except (OSError, ValueError):
+ pass
+ raise
+ return json.loads(destination.with_suffix('.engine.json').read_text(encoding='utf-8')), telemetry
+ def archive(index, row):
+ nonlocal resume_decode
+ retained = artifacts / 'attempts' / ('%02d-%s' % (index + 1, row['id']))
+ retained.mkdir(parents=True, exist_ok=False)
+ paths = [destination] + [destination.with_suffix('.engine.' + ext)
+ for ext in ('json','log','memory.json','gpu.json','gpu.csv')]
+ paths.append(destination.with_suffix('.sampling-memory.json'))
+ paths += [artifacts/name for name in ('latents.pt','rgb.npy','audio.npy','audio.wav','video.json')]
+ for path in paths:
+ if path.exists():
+ path.rename(retained / path.name)
+ save(retained / 'attempt.json', row)
+ # Reuse only completed sampling from this request, never a
+ # partial sample. The worker validates provenance and tensor
+ # metadata again before loading the saved video/audio latents.
+ # The controller's durable row can still say ``sample`` when
+ # the worker has already written a complete sampling receipt
+ # and is failing while releasing streamed weights. The
+ # worker receipt is authoritative: inspect it regardless of
+ # the parent monitor phase, then require completed_sampling()
+ # to validate all planned finite steps, provenance, and the
+ # retained latent checkpoint before switching to decode-only.
+ if resume_decode is None:
+ try:
+ sample_metrics = retained / destination.with_suffix('.engine.json').name
+ sampled = json.loads(sample_metrics.read_text(encoding='utf-8'))
+ from .decode_resume import completed_sampling
+ if (completed_sampling(sampled)
+ and (retained / 'latents.pt').is_file() and (retained / 'latents.pt').stat().st_size > 0):
+ resume_decode = dict(latents=str(retained / 'latents.pt'),
+ metrics=str(sample_metrics), request=str(retained / 'video.json'))
+ except (OSError, ValueError, TypeError):
+ pass # Older/incomplete artifacts retain full-request recovery.
+ return str(retained)
+ def retry(index, row, selected, decision):
+ retry_state = dict(attempt=index + 2, max_attempts=getattr(args, 'resource_retries', 2) + 1,
+ failed_phase=row.get('phase'), kind=row['failure']['kind'],
+ reuse_sampling=resume_decode is not None)
+ row['recovery'] = dict(decision, next_profile=selected, **retry_state)
+ persist()
+ print(json.dumps(dict(event='resource_retry', retained=row['retained'], retry=retry_state)), flush=True)
+ from .support_report import write_retry
+ write_retry(destination, report, index=index + 1, retained=row['retained'],
+ next_profile=selected, decision=row['recovery'])
+ report['video'], report['resources'], profile = execute_attempts(
+ history, resource_identity, profile, canvas, launch,
+ validate=validate, prepare=forecast,
+ archive=archive, report=report, persist=persist, on_retry=retry,
+ automatic=not args.profile or getattr(args, 'recover_placement', False),
+ max_retries=getattr(args, 'resource_retries', 2))
+ report['success'] = True
+ except BaseException as error:
+ from .diagnostic_resources import exception_details
+ report['exception'] = exception_details(error)
+ error.diagnostic_recorded = True
+ report['error'] = repr(error)
+ report['error_message'] = str(error)
+ report['error_type'] = type(error).__name__
+ if hasattr(error, 'details'):
+ report['resource_error'] = error.details
+ if applied:
+ report['tuning']['disabled_after_failure'] = tuning.disable_profile(applied, error)
+ raise
+ finally:
+ processes.restore_handlers(previous)
+ report['request_seconds'] = time.perf_counter() - started
+ diagnostic = write_debug(destination, report)
+ if diagnostic:
+ report['diagnostic_file'] = diagnostic.name
+ save(destination.with_suffix('.request.json'), report)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freevideo_engine/geometry.py b/freevideo_engine/geometry.py
new file mode 100644
index 0000000..285ae44
--- /dev/null
+++ b/freevideo_engine/geometry.py
@@ -0,0 +1,38 @@
+"""Request geometry without importing CUDA; follow the pinned H3 VAE grid."""
+import inspect
+import math
+import types
+
+
+def geometry(width=1344, height=768, frames=None, seconds=None):
+ if any(not isinstance(n, int) or n < 256 or n % 32 for n in (width, height)):
+ raise ValueError('Width and height must be multiples of 32, at least 256 pixels.')
+ if frames is not None and seconds is not None:
+ raise ValueError('Specify frames or seconds, not both.')
+ if seconds is not None and (not math.isfinite(seconds) or seconds <= 0):
+ raise ValueError('Duration must be a positive finite number.')
+ requested = math.ceil(seconds * 24) if seconds is not None else (243 if frames is None else frames)
+ if not isinstance(requested, int) or requested < 1:
+ raise ValueError('Frame count must be a positive integer.')
+ aligned = requested + (5 - requested) % 17
+ latent_frames = (aligned - 5) // 17 * 5 + 2
+ if latent_frames < 12:
+ raise ValueError('H3 hybrid window inference needs at least 39 aligned frames (1.625 seconds).')
+ return dict(width=width, height=height, fps=24, requested_frames=requested,
+ requested_seconds=seconds, frames=aligned, seconds=aligned / 24,
+ latent_frames=latent_frames, video_tokens=latent_frames * (height // 32) * (width // 32),
+ alignment='Pinned H3 VAE: round up to 17*n+5 frames; no resampling or hidden resolution reduction.')
+
+
+def sampler_for_canvas(sampler, width, height):
+ """Bind canvas constants in an isolated globals dict, preserving upstream code.
+
+ Engine.sample supplies no_grad. Never edit the upstream file or mutate its
+ module globals: two Engine instances can use different canvases safely.
+ """
+ original = inspect.unwrap(sampler)
+ bound = types.FunctionType(original.__code__,
+ dict(original.__globals__, LATENT_H=height // 16, LATENT_W=width // 16),
+ original.__name__, original.__defaults__, original.__closure__)
+ bound.__kwdefaults__ = original.__kwdefaults__
+ return bound
diff --git a/freevideo_engine/gpu_budget.py b/freevideo_engine/gpu_budget.py
new file mode 100644
index 0000000..41ac94c
--- /dev/null
+++ b/freevideo_engine/gpu_budget.py
@@ -0,0 +1,236 @@
+"""Windows allocator admission; no Torch import or device probing at import time."""
+import math
+import time
+from .system import windows
+
+_trial_allocator_owner = None
+
+
+def owned_pool_capacity(torch):
+ """Read the idle owner's pool capacity before policy subtracts its reserve.
+
+ A fresh Windows CUDA context's free-memory reading cannot tell us how much
+ of the resident worker's pool has already been included by the driver.
+ Use the same owner/context and bounds as allocator admission instead.
+ """
+ total = torch.cuda.get_device_properties(0).total_memory
+ free, _ = torch.cuda.mem_get_info()
+ reserved = torch.cuda.memory_reserved()
+ local, reader = None, None
+ try:
+ from .windows_gpu_memory import AdapterMemory
+ reader = AdapterMemory()
+ local = reader.sample().get('local')
+ except (OSError, RuntimeError, AttributeError, ValueError):
+ pass
+ finally:
+ if reader is not None:
+ try:
+ reader.close()
+ except (OSError, RuntimeError):
+ pass # A failed optional reader close does not invalidate CUDA's own measurement.
+ return allocator_ceiling(total, total, free, reserved, 0, local)
+
+
+def allocator_ceiling(budget, total, free, reserved, reserve, local=None):
+ """Bound the *whole process allocator*, crediting its own pool exactly once.
+
+ The policy budget already excludes growth headroom. Live free memory and
+ the WDDM process budget are independent bounds, each with that same reserve,
+ not deductions from the already reduced policy budget. WDDM is not a PCIe
+ traffic counter and allocations outside Torch remain outside this ceiling.
+ """
+ for value in (budget, total, free, reserved, reserve):
+ if type(value) not in (int, float) or not math.isfinite(value) or value < 0:
+ raise ValueError('GPU allocator admission requires finite nonnegative byte counts')
+ if not budget or not total:
+ raise ValueError('GPU allocator admission requires positive capacity and budget')
+ bounds = dict(planning_budget_bytes=int(budget), physical_capacity_bytes=int(total),
+ live_pool_capacity_bytes=max(0, int(free + reserved - reserve)))
+ native = None
+ if isinstance(local, dict):
+ driver_budget, usage = local.get('budget_bytes'), local.get('usage_bytes')
+ if (type(driver_budget) is int and driver_budget > 0 and
+ type(usage) is int and usage >= 0):
+ native = max(0, usage - reserved)
+ bounds['wddm_pool_capacity_bytes'] = max(0, int(driver_budget - native - reserve))
+ return dict(allocator_limit_bytes=min(bounds.values()), bounds=bounds,
+ observed_non_torch_local_bytes=native,
+ owned_allocator_reserved_bytes=int(reserved), live_free_bytes=int(free),
+ growth_reserve_bytes=int(reserve))
+
+
+def configure(torch, budget_bytes, explicit_limit=None, *, reserve_bytes=0, system=None, capacity_trial=False):
+ """Bound Windows, a low-memory trial, or an explicit benchmark allocation.
+
+ Reclaimed pool blocks can still be reused by the caching allocator. Its own
+ OOM is recoverable by the controller, unlike silently relying on WDDM to
+ back excessive GPU allocation with system memory. This is not a whole-device
+ limit and cannot prevent every instance of driver paging.
+ """
+ global _trial_allocator_owner
+ from .policy import memory_fraction
+ total = torch.cuda.get_device_properties(0).total_memory
+ planned = memory_fraction(budget_bytes, total)
+ if explicit_limit is not None and (type(explicit_limit) is not int or not 0 < explicit_limit <= total):
+ raise ValueError('Benchmark allocator limit must be positive and no larger than physical VRAM')
+ desktop = windows() if system is None else system == 'Windows'
+ report = dict(device_total_bytes=total, budget_bytes=budget_bytes, planned_fraction=planned,
+ enforced=False, benchmark_allocator_limit_bytes=explicit_limit,
+ benchmark_allocator_limit_enforced=explicit_limit is not None,
+ capacity_trial_limit_enforced=False,
+ windows_allocator_limit_enforced=False,
+ scope='PyTorch caching allocator only; other CUDA allocations and whole-device usage are not capped.')
+ limit = explicit_limit
+ if desktop:
+ # Initialize CUDA before asking the driver for a context-matched LUID.
+ torch.cuda.init()
+ free, _ = torch.cuda.mem_get_info()
+ reserved = torch.cuda.memory_reserved()
+ reader, local = None, None
+ try:
+ from .windows_gpu_memory import AdapterMemory
+ reader = AdapterMemory()
+ observed = reader.sample()
+ local = observed.get('local')
+ if not isinstance(local, dict) or type(local.get('budget_bytes')) is not int or local['budget_bytes'] <= 0:
+ raise ValueError('WDDM returned no usable local GPU budget')
+ report['windows_memory'] = dict(status='available', **observed)
+ except (OSError, RuntimeError, AttributeError, ValueError) as error:
+ report['windows_memory'] = dict(status='unavailable', reason=str(error))
+ finally:
+ if reader is not None:
+ try:
+ reader.close()
+ except Exception as error:
+ report['windows_memory_cleanup_error'] = str(error)
+ elif capacity_trial:
+ free, _ = torch.cuda.mem_get_info()
+ reserved = torch.cuda.memory_reserved()
+ local = None
+ if desktop or capacity_trial:
+ decision = allocator_ceiling(budget_bytes, total, free, reserved, reserve_bytes, local)
+ report['admission'] = decision
+ limit = min(decision['allocator_limit_bytes'], limit if limit is not None else total)
+ if limit <= 0:
+ raise torch.cuda.OutOfMemoryError('CUDA out of memory: no local GPU allocator capacity remains after live budgets')
+ report.update(enforced=True, windows_allocator_limit_enforced=desktop,
+ capacity_trial_limit_enforced=bool(capacity_trial),
+ reason='Keep the CUDA allocator within the live local-memory budget; '
+ 'recover with controlled weight offload instead of permitting allocator oversubscription.')
+ # A resident process may already have a larger *unused* pool. A new
+ # fraction affects future allocation, so release that pool once now.
+ if reserved > limit:
+ torch.cuda.empty_cache()
+ report['released_cached_pool_before_request'] = True
+ if limit is not None:
+ torch.cuda.set_per_process_memory_fraction(limit / total)
+ _trial_allocator_owner = torch if capacity_trial and not desktop else None
+ elif _trial_allocator_owner is torch:
+ # A later Linux request can regain ordinary capacity in the same
+ # resident worker. Do not leave yesterday's temporary trial cap active.
+ torch.cuda.set_per_process_memory_fraction(1.)
+ _trial_allocator_owner = None
+ report['released_capacity_trial_limit'] = True
+ report['effective_allocator_limit_bytes'] = limit
+ return report
+
+
+class LiveGPUBudget:
+ """Refresh a Windows allocator ceiling at idle CUDA stage/step boundaries.
+
+ The 256 MiB reserve is growth headroom, in addition to observed allocations
+ outside Torch. A larger driver budget must survive two observations before
+ use. Pressure takes effect immediately, reclaiming optional weights first.
+ No background thread changes CUDA limits or frees tensors in active kernels.
+ """
+ def __init__(self, torch, report, maximum_bytes, reserve_bytes, *, reader_factory=None):
+ self.torch, self.report = torch, report
+ self.total = report['device_total_bytes']
+ self.limit = report['effective_allocator_limit_bytes']
+ self.maximum = min(maximum_bytes, self.total,
+ report.get('benchmark_allocator_limit_bytes') or self.total)
+ self.reserve = reserve_bytes
+ self.pending = None
+ self.started = time.monotonic()
+ self.reader = None
+ self.receipt = report['dynamic_budget'] = dict(enabled=True,
+ base_reserve_bytes=reserve_bytes, maximum_allocator_bytes=self.maximum,
+ minimum_limit_bytes=self.limit, maximum_limit_bytes=self.limit,
+ observations=0, transitions=[], errors=[])
+ try:
+ if reader_factory is None:
+ from .windows_gpu_memory import AdapterMemory
+ reader_factory = AdapterMemory
+ self.reader = reader_factory()
+ except (OSError, RuntimeError, AttributeError, ValueError) as error:
+ self._error(error)
+
+ def _error(self, error):
+ errors = self.receipt['errors']
+ if len(errors) < 8 and str(error) not in errors:
+ errors.append(str(error))
+
+ def refresh(self, stage, reclaim=None):
+ cuda = self.torch.cuda
+ free, _ = cuda.mem_get_info()
+ reserved = cuda.memory_reserved()
+ local = None
+ if self.reader is not None:
+ try:
+ local = self.reader.sample().get('local')
+ except (OSError, RuntimeError, AttributeError, ValueError) as error:
+ self._error(error)
+ decision = allocator_ceiling(self.maximum, self.total, free, reserved, self.reserve, local)
+ candidate = decision['allocator_limit_bytes']
+ # An unavailable driver reading never grants extra residency. CUDA can
+ # still report less live capacity and trigger reclamation.
+ known = 'wddm_pool_capacity_bytes' in decision['bounds']
+ if not known:
+ candidate = min(candidate, self.limit)
+ target, action = self.limit, 'hold'
+ if candidate < self.limit:
+ target, action, self.pending = candidate, 'shrink', None
+ elif candidate >= self.limit + 64 * 2**20:
+ if self.pending is not None:
+ target, action = min(candidate, self.pending), 'grow'
+ self.pending = None
+ else:
+ self.pending, action = candidate, 'pending'
+ else:
+ self.pending = None
+ row = dict(stage=stage, elapsed_seconds=time.monotonic()-self.started,
+ previous_limit_bytes=self.limit, candidate_limit_bytes=candidate,
+ allocator_limit_bytes=target, action=action, driver_available=known,
+ live_free_bytes=free, reserved_bytes=reserved,
+ non_torch_local_bytes=decision['observed_non_torch_local_bytes'],
+ driver_budget_bytes=local.get('budget_bytes') if known else None)
+ self.receipt['observations'] += 1
+ self.receipt['last'] = row
+ if action in ('shrink', 'grow'):
+ if len(self.receipt['transitions']) < 64:
+ self.receipt['transitions'].append(row)
+ if action == 'shrink':
+ if reclaim is not None:
+ reclaim(target)
+ if cuda.memory_reserved() > target:
+ cuda.empty_cache()
+ # The setter only governs future allocations. If active tensors
+ # cannot fit after reclamation, use the existing controlled OOM
+ # recovery rather than assume that setting a fraction evicts them.
+ cuda.set_per_process_memory_fraction(max(0, target) / self.total)
+ self.limit = target
+ self.report['effective_allocator_limit_bytes'] = target
+ self.receipt['minimum_limit_bytes'] = min(self.receipt['minimum_limit_bytes'], target)
+ self.receipt['maximum_limit_bytes'] = max(self.receipt['maximum_limit_bytes'], target)
+ if target <= 0 or cuda.memory_allocated() > target:
+ raise cuda.OutOfMemoryError('CUDA out of memory: live local GPU budget decreased below active allocations')
+ return self.limit
+
+ def close(self):
+ reader, self.reader = self.reader, None
+ if reader is not None:
+ try:
+ reader.close()
+ except (OSError, RuntimeError) as error:
+ self._error(error)
diff --git a/freevideo_engine/hardware.py b/freevideo_engine/hardware.py
new file mode 100644
index 0000000..d248c69
--- /dev/null
+++ b/freevideo_engine/hardware.py
@@ -0,0 +1,200 @@
+"""Observed capacity, including live usage and enclosing Linux RAM limits."""
+from __future__ import annotations
+from dataclasses import asdict, dataclass
+import importlib.util
+import platform
+import re
+from pathlib import Path
+
+GiB = 1 << 30
+
+
+@dataclass(frozen=True)
+class Hardware:
+ gpu_name: str
+ capability: tuple[int, int]
+ vram_total: int
+ vram_free: int
+ ram_total: int
+ ram_available: int
+ system: str = 'Linux'
+ torch_version: str = ''
+ cuda_version: str = ''
+ cgroup_ram_limit: int | None = None
+ gpu_uuid: str = ''
+ driver_version: str = ''
+
+ @property
+ def architecture(self):
+ if self.capability in ((8, 0), (8, 6)):
+ return 'ampere'
+ if self.capability == (8, 9):
+ return 'ada'
+ if self.capability == (9, 0):
+ return 'hopper'
+ if self.capability[0] == 12:
+ return 'blackwell-rtx'
+ return 'other'
+
+ def to_dict(self):
+ return dict(asdict(self), architecture=self.architecture)
+
+ @classmethod
+ def from_dict(cls, value):
+ fields = {k: v for k, v in value.items() if k != 'architecture'}
+ fields['capability'] = tuple(fields['capability'])
+ return cls(**fields)
+
+
+def cgroup_memory(proc=Path('/proc')):
+ """Live v1/v2 usage, including file cache and every visible ancestor cap."""
+ result = dict(limit_bytes=None, available_bytes=None, reclaimable_available_bytes=None,
+ groups=[], complete=True)
+ if platform.system() != 'Linux':
+ return result
+ try:
+ groups = {}
+ for line in (proc / 'self/cgroup').read_text(encoding='utf-8').splitlines():
+ hierarchy, controllers, path = line.split(':', 2)
+ if hierarchy == '0' and not controllers:
+ groups[2] = Path(path)
+ elif 'memory' in controllers.split(','):
+ groups[1] = Path(path)
+ mounts = (proc / 'self/mountinfo').read_text(encoding='utf-8').splitlines()
+ seen = set()
+ for line in mounts:
+ before, after = line.split(' - ', 1)
+ fields, filesystem = before.split(), after.split()
+ version = 2 if filesystem[0] == 'cgroup2' else 1 if filesystem[0] == 'cgroup' and 'memory' in filesystem[2].split(',') else None
+ if version not in groups:
+ continue
+ decode = lambda value: Path(re.sub(r'\\([0-7]{3})', lambda m: chr(int(m[1], 8)), value))
+ mount_root, root = decode(fields[3]), decode(fields[4])
+ group = groups[version]
+ relative = group.relative_to(mount_root) if group == mount_root or mount_root in group.parents else Path(str(group).lstrip('/'))
+ current = root / relative
+ for directory in (current, *current.parents):
+ if (directory != root and root not in directory.parents) or directory in seen:
+ continue
+ seen.add(directory)
+ limit_file = directory / ('memory.max' if version == 2 else 'memory.limit_in_bytes')
+ if not limit_file.is_file():
+ continue
+ raw = limit_file.read_text(encoding='utf-8').strip()
+ limit = None if raw == 'max' or int(raw) >= 1 << 60 else int(raw)
+ usage_file = directory / ('memory.current' if version == 2 else 'memory.usage_in_bytes')
+ try:
+ used = int(usage_file.read_text(encoding='utf-8'))
+ except (OSError, ValueError):
+ used = None
+ result['complete'] = False
+ try:
+ stats = dict((key, int(value)) for key, value in
+ (line.split() for line in (directory / 'memory.stat').read_text(encoding='utf-8').splitlines()))
+ except (OSError, ValueError):
+ stats = {}
+ def metric(v2, v1):
+ return stats.get(v2) if version == 2 else stats.get('total_' + v1, stats.get(v1))
+ entry = dict(version=version, limit_bytes=limit, current_bytes=used,
+ available_bytes=max(0, limit-used) if limit is not None and used is not None else 0 if limit is not None else None,
+ file_cache_bytes=metric('file', 'cache'), anonymous_bytes=metric('anon', 'rss'),
+ shmem_bytes=metric('shmem', 'shmem'), dirty_bytes=metric('file_dirty', 'dirty'),
+ writeback_bytes=metric('file_writeback', 'writeback'),
+ unevictable_bytes=metric('unevictable', 'unevictable'))
+ # A memory.max charge is not all irreclaimable working memory:
+ # immutable mapped model pages can be reclaimed and read again.
+ # Keep raw headroom for download throttling and hard accounting.
+ # Inference may credit only clean filesystem pages; missing
+ # counters, tmpfs/shared pages, dirty data and locked pages earn
+ # no credit. This estimate never changes the kernel's limit.
+ names = ('file_cache_bytes', 'shmem_bytes', 'dirty_bytes',
+ 'writeback_bytes', 'unevictable_bytes')
+ complete = used is not None and all(type(entry[key]) is int and entry[key] >= 0 for key in names)
+ clean = max(0, min(used, entry['file_cache_bytes']) - sum(entry[key] for key in names[1:])) if complete else 0
+ entry['reclaimable_file_bytes'] = clean
+ entry['reclaimable_available_bytes'] = (min(limit, entry['available_bytes'] + clean)
+ if limit is not None else None)
+ result['groups'].append(entry)
+ except (OSError, ValueError, IndexError):
+ result['complete'] = False
+ limited = [group for group in result['groups'] if group['limit_bytes'] is not None]
+ if limited:
+ result.update(limit_bytes=min(group['limit_bytes'] for group in limited),
+ available_bytes=min(group['available_bytes'] for group in limited),
+ reclaimable_available_bytes=min(group['reclaimable_available_bytes'] for group in limited))
+ return result
+
+
+def cgroup_capacity(*, include_reclaimable=False):
+ state = cgroup_memory()
+ return state['limit_bytes'], state.get('reclaimable_available_bytes', state['available_bytes']) if include_reclaimable else state['available_bytes']
+
+
+def detect():
+ # End the detector's CUDA context before starting the model worker. The
+ # lightweight parent must not keep another GPU context or Torch heap alive.
+ import json
+ import subprocess
+ import sys
+ code = 'import json; from freevideo_engine.hardware import _detect_local; print(json.dumps(_detect_local().to_dict()))'
+ result = subprocess.run([sys.executable, '-c', code], capture_output=True, text=True)
+ if result.returncode:
+ raise RuntimeError('Hardware detection failed: ' + result.stderr[-2000:])
+ observed = Hardware.from_dict(json.loads(result.stdout.strip().splitlines()[-1]))
+ # The isolated torch probe has exited. Its temporary Python/CUDA host heap
+ # must not be subtracted again from the future worker's whole-process RAM
+ # allowance. Refresh RAM in the lightweight controller after that exit.
+ from dataclasses import replace
+ from .system import system_memory
+ memory = system_memory()
+ limit, available = cgroup_capacity(include_reclaimable=True)
+ return replace(observed, ram_total=memory['total_bytes'],
+ ram_available=min(memory['available_bytes'], available) if available is not None else memory['available_bytes'],
+ cgroup_ram_limit=limit)
+
+
+def _detect_local():
+ from .system import system_memory, nvidia_smi
+ import subprocess
+ import torch
+ if not torch.cuda.is_available():
+ raise RuntimeError('A CUDA GPU is required for VDN generation; plan --hardware-json can inspect presets offline.')
+ index = torch.cuda.current_device()
+ free, total = torch.cuda.mem_get_info(index)
+ ram = system_memory()
+ cap, available = cgroup_capacity(include_reclaimable=True)
+ uuid = str(getattr(torch.cuda.get_device_properties(index), 'uuid', ''))
+ if uuid and not uuid.startswith(('GPU-', 'MIG-')):
+ uuid = 'GPU-' + uuid
+ driver = ''
+ try:
+ query = [nvidia_smi(), '--query-gpu=driver_version', '--format=csv,noheader']
+ if uuid:
+ query += ['--id=' + uuid]
+ driver = subprocess.check_output(query, text=True, stderr=subprocess.DEVNULL, timeout=5).strip().splitlines()[0]
+ except (OSError, RuntimeError, subprocess.SubprocessError, IndexError):
+ pass
+ return Hardware(torch.cuda.get_device_name(index), torch.cuda.get_device_capability(index),
+ total, free, ram['total_bytes'], min(ram['available_bytes'], available) if available is not None else ram['available_bytes'],
+ platform.system(), str(torch.__version__), str(torch.version.cuda), cap, uuid, driver)
+
+
+def installed_backends():
+ import importlib.metadata
+ result = {'cudnn', 'torch-flash'}
+ for name, module, distribution in [('sage2', 'sageattention', 'sageattention'), ('fa2', 'flash_attn_2_cuda', 'flash-attn')]:
+ try:
+ if importlib.metadata.version(distribution).startswith('2.') and importlib.util.find_spec(module) is not None:
+ result.add(name)
+ except (ImportError, ModuleNotFoundError, ValueError):
+ pass
+ spec = importlib.util.find_spec('flash_attn')
+ if spec and any((Path(p) / 'cute' / 'interface.py').is_file() for p in spec.submodule_search_locations or []):
+ result.add('fa4')
+ try:
+ distribution = importlib.metadata.distribution('flash-attn-4')
+ if Path(distribution.locate_file('flash_attn/cute/interface.py')).is_file():
+ result.add('fa4')
+ except importlib.metadata.PackageNotFoundError:
+ pass
+ return result
diff --git a/freevideo_engine/head_chunk.py b/freevideo_engine/head_chunk.py
new file mode 100644
index 0000000..a5f3bd3
--- /dev/null
+++ b/freevideo_engine/head_chunk.py
@@ -0,0 +1,187 @@
+"""Bound H3 activations with head groups; optional locally validated overlap.
+
+Reuses upstream's head-sliced linear branch (originally used by Ulysses).
+No heads, windows, text states or scan frames are dropped. Sliced GEMMs can
+change floating-point reduction order and must be reported as an ablation.
+"""
+from contextlib import nullcontext
+import types
+import torch
+import torch.nn.functional as F
+
+
+def install_head_chunks(transformer, policy, chunk, cpu_outputs=False, projection_chunk=1024, grouped_outputs=False,
+ parallelism=1):
+ if chunk < 1 or projection_chunk < 1:
+ raise ValueError('Head and attention-output chunks must be positive')
+ if type(parallelism) is not int or parallelism not in (1, 2, 4):
+ raise ValueError('Head parallelism must be 1, 2 or 4')
+ if parallelism > 1 and (cpu_outputs or grouped_outputs):
+ raise ValueError('Parallel head groups require GPU attention outputs')
+ from src.models.hybrid_transform import iter_hybrids
+ from src.models.softmax_attention.kernels import _qk_prep
+ from src.models.ops.fp8_linear import Fp8Linear, quantize_activation
+ from .fp8_ops import ColumnScale, project_with_scale, row_scale, sliced_projection
+
+ def forward(attn, x, rotary):
+ layout = attn.layout
+ if layout is None or not attn.inference_mode:
+ raise ValueError('Head chunks require an explicit packed layout and inference kernels')
+ bounds = attn._bounds(layout)
+ if all(lo <= 0 and hi >= layout.num_frames - 1 for lo, hi in bounds):
+ raise ValueError('This profile is for the VDN hybrid window layout')
+ orig, branch = attn.orig, attn.linear_attention
+ projections = (orig.to_q, orig.to_k, orig.to_v)
+ quantized = quantize_activation(x) if all(isinstance(p, Fp8Linear) for p in projections) else None
+ video = slice(layout.video_start, layout.video_end)
+ text = slice(*layout.text_range)
+ frame_mean = x[video].view(layout.num_frames, layout.tokens_per_frame, -1).mean(1, dtype=torch.float32)
+ gate_hidden = x[video] if branch.output_gate.down is None else branch.output_gate.down(x[video])
+ soft_shape = (len(x), attn.num_heads * attn.head_dim)
+ linear_shape = (layout.num_frames * layout.tokens_per_frame, attn.num_heads * branch.head_dim)
+ soft_scale = ColumnScale(len(x), x.device) if cpu_outputs and isinstance(orig.to_out[0], Fp8Linear) else None
+ linear_scale = ColumnScale(linear_shape[0], x.device) if cpu_outputs and isinstance(attn.to_out_linear, Fp8Linear) else None
+ output_width = orig.to_out[0].weight_fp8.shape[0] if isinstance(orig.to_out[0], Fp8Linear) else orig.to_out[0].out_features
+ if grouped_outputs:
+ from .activation_staging import HeadReadouts
+ widths = [min(chunk, attn.num_heads - start) * attn.head_dim for start in range(0, attn.num_heads, chunk)]
+ stores = getattr(policy, 'group_readouts', None)
+ if stores is None or any(not store.supports(rows, widths, x.dtype, projection_chunk)
+ for store, rows in zip(stores, (len(x), linear_shape[0]))):
+ policy.group_readouts = tuple(HeadReadouts(rows, widths, x.dtype, projection_chunk)
+ for rows in (len(x), linear_shape[0]))
+ soft, linear = policy.group_readouts
+ elif cpu_outputs:
+ key = (soft_shape, linear_shape, x.dtype)
+ if getattr(policy, 'host_output_key', None) != key:
+ policy.host_outputs = (torch.empty(soft_shape, dtype=x.dtype, pin_memory=True),
+ torch.empty(linear_shape, dtype=x.dtype, pin_memory=True))
+ policy.host_output_key = key
+ soft, linear = policy.host_outputs
+ else:
+ soft, linear = x.new_empty(soft_shape), x.new_empty(linear_shape)
+ def group(start):
+ end = min(start + chunk, attn.num_heads)
+ heads = slice(start, end)
+ channels = slice(start * attn.head_dim, end * attn.head_dim)
+ raw = tuple(sliced_projection(proj, x, channels, quantized)
+ .view(len(x), end - start, attn.head_dim) for proj in projections)
+ q = _qk_prep(raw[0], orig.norm_q.weight, orig.norm_q.eps, *rotary)
+ k = _qk_prep(raw[1], orig.norm_k.weight, orig.norm_k.eps, *rotary)
+ attended = policy(q, k, raw[2], layout, bounds, attn.head_dim ** -.5, attn.anchor_frames)
+ del q, k
+ if attn.enable_softmax_gate:
+ gate_module = attn.softmax_gate
+ gate_x = x if gate_module.down is None else gate_module.down(x)
+ gate = torch.sigmoid(F.linear(gate_x, gate_module.up.weight[heads], gate_module.up.bias[heads]))
+ attended.mul_(gate[..., None])
+ if soft_scale is not None:
+ soft_scale.update(attended.flatten(1))
+ if grouped_outputs:
+ soft.store(start // chunk, attended.flatten(1))
+ else:
+ soft[:, channels].copy_(attended.flatten(1), non_blocking=cpu_outputs)
+ del attended
+ beta = torch.sigmoid(F.linear(x[video], branch.beta_proj.weight[heads]))
+ text_beta = torch.sigmoid(F.linear(x[text], branch.beta_proj.weight[heads])) if attn.enable_text_state else None
+ linear_channels = slice(start * branch.head_dim, end * branch.head_dim)
+ gate = torch.sigmoid(F.linear(gate_hidden, branch.output_gate.up.weight[linear_channels],
+ branch.output_gate.up.bias[linear_channels]))
+ gate = gate.view(-1, end - start, branch.head_dim)
+ readout = branch(None, layout.num_frames, layout.tokens_per_frame, bounds,
+ qkv_raw=tuple(t[video] for t in raw), frame_size=layout.frame_size,
+ skip_ends=attn.anchor_frames == 'both',
+ text_qkv_raw=tuple(t[text] for t in raw) if attn.enable_text_state else None,
+ inference=True, heads=heads, beta=beta, gate=gate,
+ frame_mean=frame_mean, text_beta=text_beta)
+ if linear_scale is not None:
+ linear_scale.update(readout)
+ if grouped_outputs:
+ linear.store(start // chunk, readout.contiguous())
+ else:
+ linear[:, linear_channels].copy_(readout, non_blocking=cpu_outputs)
+ del raw, readout, gate, beta, text_beta
+
+ # Keep the exact projection/head/window shapes. Merely widening the
+ # head chunk changes GEMM rounding; independent streams can overlap
+ # these original groups without changing their arithmetic partition.
+ # First execute this shape on the calling stream: upstream attention
+ # and scan caches lazily create shared device indices/compiled kernels.
+ warm_key = (tuple(x.shape), x.dtype, str(x.device), chunk, layout, tuple(bounds),
+ attn.anchor_frames, attn.enable_text_state, policy.window_batch,
+ policy.backend, policy.window_varlen)
+ concurrent = parallelism > 1 and getattr(attn, '_freevideo_parallel_warm', None) == warm_key
+ parent = torch.cuda.current_stream(x.device) if concurrent else None
+ streams = []
+ if concurrent:
+ policy.prepare(layout, bounds, x.device, attn.anchor_frames, batches=True)
+ from src.models.linear_attention.scan import _gather_indices
+ _gather_indices(bounds, layout.num_frames, x.device)
+ stream_key = (str(x.device), parallelism)
+ if getattr(policy, 'head_stream_key', None) != stream_key:
+ policy.head_streams = [torch.cuda.Stream(device=x.device) for _ in range(parallelism)]
+ policy.head_stream_key = stream_key
+ streams = policy.head_streams
+ # Inputs and disjoint output planes belong to the parent allocator.
+ # record_stream also protects their lifetime if a group raises OOM.
+ shared = [x, frame_mean, gate_hidden, soft, linear, *rotary]
+ if quantized is not None:
+ shared.extend(quantized)
+ for stream in streams:
+ stream.wait_stream(parent)
+ for tensor in shared:
+ tensor.record_stream(stream)
+ try:
+ for index, start in enumerate(range(0, attn.num_heads, chunk)):
+ with torch.cuda.stream(streams[index % len(streams)]) if streams else nullcontext():
+ group(start)
+ finally:
+ # Output projection, offload cleanup and the next block cannot
+ # race unfinished groups, including after a recoverable OOM.
+ for stream in streams:
+ parent.wait_stream(stream)
+ if parallelism > 1:
+ attn._freevideo_parallel_warm = warm_key
+ counter = 'parallel_head_calls' if concurrent else 'parallel_head_warmups'
+ setattr(policy, counter, getattr(policy, counter, 0) + 1)
+ del quantized
+ soft_scale = soft_scale.finish() if soft_scale is not None else None
+ linear_scale = linear_scale.finish() if linear_scale is not None else None
+ if grouped_outputs:
+ torch.cuda.current_stream(x.device).synchronize()
+ out = x.new_empty((len(x), output_width))
+ for rows, part in soft.project(lambda part: part, out, len(x)):
+ scale = row_scale(soft_scale, rows) if soft_scale is not None else None
+ out[rows] = orig.to_out[1](project_with_scale(orig.to_out[0], part, scale))
+ for rows, part in linear.project(lambda part: part, out, linear_shape[0]):
+ scale = row_scale(linear_scale, rows) if linear_scale is not None else None
+ out[video][rows].add_(project_with_scale(attn.to_out_linear, part, scale))
+ return out
+ if cpu_outputs:
+ # All D2H writes must finish before the pinned outputs are read by H2D.
+ torch.cuda.current_stream(x.device).synchronize()
+ # Project bounded row slices directly from the host readout planes.
+ # Copying whole planes back would retain >2 GB of temporary buffers.
+ out = x.new_empty((len(x), output_width))
+ for row in range(0, len(x), projection_chunk):
+ rows = slice(row, row + projection_chunk)
+ part = soft[rows].to(x.device, non_blocking=True)
+ scale = row_scale(soft_scale, rows) if soft_scale is not None else None
+ out[rows] = orig.to_out[1](project_with_scale(orig.to_out[0], part, scale))
+ del part
+ for row in range(0, len(linear), projection_chunk):
+ rows = slice(row, row + projection_chunk)
+ part = linear[rows].to(x.device, non_blocking=True)
+ scale = row_scale(linear_scale, rows) if linear_scale is not None else None
+ out[video][rows].add_(project_with_scale(attn.to_out_linear, part, scale))
+ del part
+ return out
+ out = orig.to_out[1](orig.to_out[0](soft))
+ del soft
+ out[video] += attn.to_out_linear(linear)
+ return out
+
+ for attn in iter_hybrids(transformer):
+ if attn.head_dim != attn.linear_attention.head_dim:
+ raise ValueError('The shared-QKV profile requires matching branch head dimensions')
+ attn._hybrid_forward = types.MethodType(forward, attn)
diff --git a/freevideo_engine/hf_auth.py b/freevideo_engine/hf_auth.py
new file mode 100644
index 0000000..0b43569
--- /dev/null
+++ b/freevideo_engine/hf_auth.py
@@ -0,0 +1,27 @@
+"""Optional session credentials; never written to a settings or receipt file."""
+import os
+import re
+
+TOKEN_URL = 'https://huggingface.co/settings/tokens'
+HELP_EN = ('Optional: add a Hugging Face read token to use your account’s download quota. '
+ 'This can reduce anonymous throttling; it does not guarantee higher speed. '
+ 'Kept for this session only. You can continue without it.')
+HELP_ZH = ('可选:填写 Hugging Face 读取 Token,使用账号下载额度,减少匿名限流。'
+ '可能更快,但不保证提速。仅本次打开有效,不填写也能继续。')
+
+
+def validate(value):
+ if not isinstance(value, str):
+ raise ValueError('Enter a Hugging Face token beginning with hf_, or leave it empty.')
+ value = value.strip()
+ if value and not re.fullmatch(r'hf_[A-Za-z0-9]{1,4093}', value):
+ raise ValueError('Enter a Hugging Face token beginning with hf_, or leave it empty.')
+ return value
+
+
+def environment(token='', environ=None):
+ env = dict(os.environ if environ is None else environ)
+ token = validate(token)
+ if token:
+ env['HF_TOKEN'] = token
+ return env
diff --git a/freevideo_engine/idle_encoder.py b/freevideo_engine/idle_encoder.py
new file mode 100644
index 0000000..a6db641
--- /dev/null
+++ b/freevideo_engine/idle_encoder.py
@@ -0,0 +1,297 @@
+"""Supervise speculative encoder placement after a saved interactive request.
+
+This controller imports no tensor runtime. The existing resident worker owns
+CUDA, and a new foreground request cancels this lower-priority work first.
+"""
+import argparse
+from contextlib import contextmanager
+import json
+import os
+import signal
+from pathlib import Path
+import subprocess
+import sys
+import threading
+import time
+
+from . import processes
+from .monitoring import save
+
+
+def gpu_preload_budget(previous, limits, *, free, total, reserved):
+ """Reconsider idle placement from live bytes, preserving explicit limits.
+
+ Only our idle allocator's storage earns credit. Another application's
+ allocations and the CUDA context are absent from ``reserved``. No compute
+ profile, quality setting, or foreground placement changes here.
+ """
+ from .hardware import GiB
+ limits = limits or {}
+ capacity = total
+ if limits.get('vram_gib') is not None:
+ capacity = min(capacity, int(limits['vram_gib'] * GiB))
+ available = max(0, min(capacity, free + reserved))
+ reserve = limits.get('gpu_reserve_gib')
+ if reserve is None:
+ reserve = 1. if limits.get('system') == 'Windows' else max(.5, min(1., available / GiB * .025))
+ reserve = max(.5, reserve) * GiB
+ budget = max(0, available - reserve)
+ if limits.get('automatic') is not True:
+ budget = min(budget, previous)
+ if limits.get('allocator_limit_bytes') is not None:
+ budget = min(budget, limits['allocator_limit_bytes'])
+ return dict(budget_bytes=int(budget), live_free_bytes=free, owned_reserved_bytes=reserved,
+ capacity_bytes=capacity, system_reserve_bytes=int(reserve), previous_budget_bytes=previous,
+ reason='Refresh idle GPU allowance from live free bytes plus owned cache; keep explicit caps and system headroom')
+
+
+class PreloadCancelled(Exception):
+ """A completed video stays successful when speculative work is cancelled."""
+
+
+@contextmanager
+def interruptible_load(patcher, check):
+ """Yield between native module transfers, with the original loader intact.
+
+ CPU cleanup must remain callable after cancellation. Speculative loading
+ also avoids pinning all offloaded weights merely to warm a few GPU layers.
+ No forward, tokenization, patched arithmetic or warm-up inference runs here.
+ """
+ saved = []
+ def replace(obj, name, value):
+ saved.append((obj, name, name in vars(obj), vars(obj).get(name)))
+ setattr(obj, name, value)
+ try:
+ for module in patcher.model.modules():
+ original = module.to
+ def move(*args, _original=original, **kwargs):
+ device = kwargs.get('device', args[0] if args else None)
+ if str(device).startswith('cuda'):
+ check()
+ return _original(*args, **kwargs)
+ replace(module, 'to', move)
+ original_patch = patcher.patch_weight_to_device
+ def patch(*args, **kwargs):
+ device = kwargs.get('device_to', args[1] if len(args) > 1 else None)
+ if str(device).startswith('cuda'):
+ check()
+ return original_patch(*args, **kwargs)
+ replace(patcher, 'patch_weight_to_device', patch)
+ replace(patcher, 'pin_weight_to_device', lambda *args, **kwargs: check())
+ yield
+ finally:
+ for obj, name, existed, value in reversed(saved):
+ if existed:
+ setattr(obj, name, value)
+ else:
+ delattr(obj, name)
+
+
+class IdleEncoder:
+ def __init__(self):
+ self.thread = None
+ self.cancelled = threading.Event()
+ self.process = None
+ self.cancel_path = None
+ self.report = None
+
+ def stop(self):
+ self.cancelled.set()
+ if self.cancel_path is not None:
+ try:
+ self.cancel_path.touch(exist_ok=True)
+ except OSError:
+ if self.process is not None and self.process.poll() is None:
+ processes.stop(self.process, grace=1.)
+ if self.thread is not None:
+ self.thread.join(timeout=3.)
+ if self.thread.is_alive():
+ # A blocked disk read/native copy is not indefinitely allowed
+ # to delay the next prompt. The controller stops the real GPU
+ # worker on termination; SessionOwner confirms it is idle/dead.
+ if self.process is not None:
+ processes.stop(self.process, grace=1.)
+ self.thread.join(timeout=2.)
+ return not self.thread.is_alive()
+ return True
+
+ def schedule(self, endpoint, output, python, environment, *, busy=None, notify=None):
+ if not self.stop():
+ return False
+ self.cancelled = threading.Event()
+ cancel = self.cancelled
+ report = Path(output).with_suffix('.prewarm.json')
+ self.report = report
+ self.cancel_path = Path(output).with_suffix('.prewarm.cancel')
+ request_path = Path(output).with_suffix('.prewarm-request.json')
+ request = dict(endpoint=str(endpoint), previous=str(Path(output).with_suffix('.request.json')),
+ cancel=str(self.cancel_path), metrics=str(report))
+ save(request_path, request)
+ env = dict(environment)
+ env.pop('FREEVIDEO_RUNTIME_LOCK_FD', None)
+ env.pop(processes.HANDLE_ENV, None)
+ self.process = None
+
+ def work():
+ try:
+ # Let Comfy publish the video before optional background work.
+ if cancel.wait(1.):
+ save(report, dict(state='cancelled', reason='A queued request takes priority'))
+ return
+ # Comfy may still be saving/displaying this video's downstream
+ # nodes. Wait for the graph to finish instead of permanently
+ # skipping preloading because it was busy at one instant.
+ while busy and busy():
+ if cancel.wait(.1):
+ save(report, dict(state='cancelled', reason='A queued request takes priority'))
+ return
+ with report.with_suffix('.log').open('wb') as log:
+ self.process = processes.popen([str(python), '-B', '-m',
+ 'freevideo_engine.idle_encoder', '--request', str(request_path)],
+ env=env, cwd=Path(__file__).resolve().parents[1], stdin=subprocess.DEVNULL,
+ stdout=log, stderr=subprocess.STDOUT, start_new_session=True, supervise=True)
+ previous = None
+ while self.process.poll() is None:
+ if cancel.is_set() or busy and busy():
+ cancel.set()
+ self.cancel_path.touch(exist_ok=True)
+ try:
+ current = json.loads(report.read_text(encoding='utf-8'))
+ if current != previous and notify:
+ try:
+ notify(current)
+ except Exception:
+ pass # A disconnected UI does not orphan the controller.
+ previous = current
+ except (OSError, ValueError):
+ pass
+ try:
+ self.process.wait(timeout=.1)
+ except subprocess.TimeoutExpired:
+ pass
+ if notify and report.is_file():
+ notify(json.loads(report.read_text(encoding='utf-8')))
+ except Exception as error:
+ if self.process is not None and self.process.poll() is None:
+ processes.stop(self.process, grace=1.)
+ save(report, dict(state='failed', reason=str(error)))
+
+ self.thread = threading.Thread(target=work, name='FreeVideo idle encoder', daemon=True)
+ self.thread.start()
+ return True
+
+
+def run(request):
+ from .adaptive import classify_failure
+ from .locking import runtime_lock
+ from .resource_history import ResourceHistory
+ from .resident_process import ENV, RemoteProcess
+ from .ram import ProcessMemory
+ from .system import inference_headroom, inference_memory_sample, windows
+
+ result_path = Path(request['metrics'])
+ cancelled = Path(request['cancel'])
+ worker = None
+ attempt = None
+ history = None
+ result = dict(state='skipped', scope='Idle GPU encoder weights; no prompt encoding')
+ def interrupted(signum, frame):
+ cancelled.touch(exist_ok=True)
+ raise KeyboardInterrupt('Foreground work takes priority over encoder preloading')
+ previous_handler = processes.termination_handler(interrupted)
+ try:
+ if cancelled.exists():
+ raise PreloadCancelled('A new request takes priority')
+ previous = json.loads(Path(request['previous']).read_text(encoding='utf-8'))
+ attempts = previous.get('resource_attempts', [])
+ if not previous.get('success') or not attempts or not previous.get('encoding'):
+ result['reason'] = 'No completed interactive encoder request to prepare again'
+ return result
+ history = ResourceHistory(previous['resource_history'])
+ measured = history.attempt(attempts[-1]['id'])
+ if measured is None or measured['state'] != 'success':
+ raise ValueError('Previous request has no settled success in local history')
+ original = json.loads((Path(previous['artifacts'])/'encode.json').read_text(encoding='utf-8'))
+ # Deliberately do not forward text, images, seed or other next-request
+ # inputs. Preloading uses only the already validated encoder recipe.
+ encoding = {k: original[k] for k in ('encoder', 'comfy_root', 'model_paths') if k in original}
+ profile = previous['profile']
+ budget = profile['gpu_budget_gb']*1e9
+ if profile.get('benchmark_allocator_limit_bytes') is not None:
+ budget = min(budget, profile['benchmark_allocator_limit_bytes'])
+ encoding.update(idle_preload=True, cancel=str(cancelled), metrics=str(result_path),
+ idle_resources=previous.get('idle_resources'),
+ gpu_budget_gb=budget/1e9,
+ ram_budget_bytes=round(profile['inference_ram_budget_gb']*1e9))
+ worker_request = result_path.with_name(result_path.stem+'-worker.json')
+ save(worker_request, encoding)
+ environment = dict(os.environ)
+ environment[ENV] = request['endpoint']
+ allocator = profile.get('allocator_config', '')
+ environment.update(PYTORCH_ALLOC_CONF=allocator, PYTORCH_CUDA_ALLOC_CONF=allocator)
+ memory = ProcessMemory()
+ with runtime_lock(inherit=False) as descriptor:
+ if cancelled.exists():
+ raise PreloadCancelled('A new request takes priority')
+ attempt = history.begin(measured['identity'],
+ dict(encoder=encoding['encoder'], operation='idle-preload'),
+ measured['geometry'], purpose='encoding',
+ evidence=dict(reason='Saved video returned; speculative weights only', previous=measured['id']))
+ worker = RemoteProcess(request['endpoint'],
+ [sys.executable, '-B', '-m', 'freevideo_engine.encode_worker', '--request', str(worker_request)],
+ environment, descriptor, result_path.with_name(result_path.stem+'-worker.log'),
+ required_capabilities=('encoder-weight-preload',))
+ history.attach_worker(attempt, worker.pid, phase='idle-encoder-load')
+ cancel_started = None
+ while worker.poll() is None:
+ sample = memory.sample(worker.pid)
+ used = inference_memory_sample(sample)
+ if used is None or used > encoding['ram_budget_bytes'] or inference_headroom(sample) < (2 if windows() else 1)*2**30:
+ cancelled.touch(exist_ok=True)
+ result.update(state='released', reason='Live RAM pressure interrupted speculative loading')
+ if cancelled.exists():
+ cancel_started = cancel_started or time.monotonic()
+ if time.monotonic()-cancel_started > 1.:
+ raise PreloadCancelled('Preload did not yield promptly; stop the worker before foreground work')
+ try:
+ worker.wait(timeout=.1)
+ except subprocess.TimeoutExpired:
+ pass
+ loaded = json.loads(result_path.read_text(encoding='utf-8')) if result_path.is_file() else {}
+ if worker.returncode or not loaded.get('success'):
+ from .adaptive import WorkerExit
+ raise WorkerExit(worker.returncode, result_path.with_name(result_path.stem+'-worker.log'))
+ result.update(loaded, resources=memory.result())
+ history.finish(attempt, 'cancelled' if result.get('state') in ('cancelled', 'released') else 'success', details=result)
+ attempt = None
+ except BaseException as error:
+ if worker is not None:
+ try:
+ worker.stop_request(grace=.5)
+ except BaseException as cleanup:
+ error.worker_stop_confirmed = False
+ error.worker_stop_error = repr(cleanup)
+ failure = classify_failure(KeyboardInterrupt(str(error)) if isinstance(error, PreloadCancelled) else error)
+ if getattr(error, 'worker_stop_confirmed', None) is False:
+ failure = classify_failure(error)
+ result.update(state='cancelled' if failure['outcome'] == 'cancelled' else 'skipped' if isinstance(error, BlockingIOError) else 'failed',
+ reason=str(error), failure=failure)
+ if attempt is not None:
+ history.finish(attempt, failure['outcome'], details=result)
+ finally:
+ for signum, handler in previous_handler.items():
+ signal.signal(signum, handler)
+ result['completed_epoch'] = time.time()
+ save(result_path, result)
+ return result
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--request', type=Path, required=True)
+ args = parser.parse_args()
+ run(json.loads(args.request.read_text(encoding='utf-8')))
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freevideo_engine/input_cache.py b/freevideo_engine/input_cache.py
new file mode 100644
index 0000000..8878075
--- /dev/null
+++ b/freevideo_engine/input_cache.py
@@ -0,0 +1,123 @@
+"""Small persistent inputs, separate from optional performance tuning.
+
+Entries are content checked before reuse. Old namespaces and damaged files stay
+on disk; a miss never deletes user artifacts or substitutes a finished video.
+"""
+import copy
+import hashlib
+import json
+from pathlib import Path
+import shutil
+import time
+import uuid
+
+from .monitoring import save
+from .paths import data_root
+
+
+def digest(path):
+ value = hashlib.sha256()
+ with Path(path).open('rb') as stream:
+ for block in iter(lambda: stream.read(1024 * 1024), b''):
+ value.update(block)
+ return value.hexdigest()
+
+
+class InputCache:
+ """One receipt per input: no global index, eviction, or RAM-sized cache."""
+ def __init__(self, identity, *, root=None, kind='conditioning'):
+ encoded = json.dumps(identity, sort_keys=True, separators=(',', ':')).encode()
+ self.identity = hashlib.sha256(encoded).hexdigest()
+ self.root = Path(root or data_root() / 'input-cache') / kind / self.identity
+ self.note = None
+
+ def receipt(self, key):
+ return self.root / (hashlib.sha256(key.encode()).hexdigest() + '.json')
+
+ def lookup(self, key):
+ try:
+ entry = json.loads(self.receipt(key).read_text(encoding='utf-8'))
+ if (entry.get('identity') != self.identity or not isinstance(entry.get('file'), str)
+ or Path(entry['file']).name != entry['file']
+ or type(entry.get('bytes')) is not int or not 0 < entry['bytes'] <= 128 * 2**20
+ or not isinstance(entry.get('metrics'), dict)):
+ return None
+ source = self.root / entry['file']
+ if source.stat().st_size != entry['bytes'] or digest(source) != entry['sha256']:
+ self.note = 'Cache content changed; retained and recomputed'
+ return None
+ return source, entry
+ except FileNotFoundError:
+ return None
+ except (OSError, ValueError, KeyError, TypeError, AttributeError) as error:
+ self.note = type(error).__name__ + ': input cache unavailable; compute normally'
+ return None
+
+ def get(self, key, destination):
+ started = time.perf_counter()
+ found = self.lookup(key)
+ if found is None:
+ return None
+ source, entry = found
+ try:
+ destination = Path(destination)
+ destination.parent.mkdir(parents=True, exist_ok=True)
+ if destination.exists() or destination.is_symlink():
+ raise FileExistsError(destination)
+ temporary = destination.with_name(destination.name + '.cache-partial-' + uuid.uuid4().hex)
+ with temporary.open('xb') as output, source.open('rb') as stream:
+ shutil.copyfileobj(stream, output, length=1024 * 1024)
+ if digest(temporary) != entry['sha256']:
+ # Keep partial/corrupted copies separate from the usable input.
+ self.note = 'Cache changed during reuse; retained and recomputed'
+ return None
+ from .file_ops import publish
+ publish(temporary, destination)
+ result = copy.deepcopy(entry['metrics'])
+ # A receipt describes the original encoding. Keep its timings as
+ # provenance, not work performed by this cache-hit request.
+ from .encoder_diagnostics import cached_metrics
+ cached_metrics(result)
+ result.update(output=str(destination), cache_hit=True, cache_sha256=entry['sha256'],
+ cache_receipt=str(self.receipt(key)), load_seconds=0.,
+ work_seconds=time.perf_counter() - started,
+ torch_peak_allocated_bytes=0, torch_peak_reserved_bytes=0)
+ return result
+ except (OSError, ValueError, KeyError, TypeError, AttributeError) as error:
+ self.note = type(error).__name__ + ': input cache unavailable; compute normally'
+ return None
+
+ def put(self, key, source, metrics, *, move=False):
+ if metrics.get('success') is not True:
+ raise ValueError('Only successful inputs may be cached')
+ source = Path(source)
+ size = source.stat().st_size
+ if not 0 < size <= 128 * 2**20:
+ self.note = 'Input exceeds the small-input cache limit; original artifact retained'
+ return False
+ self.root.mkdir(parents=True, exist_ok=True)
+ if shutil.disk_usage(self.root).free < size + 2**30:
+ self.note = 'Low disk space; existing caches retained, new input not cached'
+ return False
+ checksum = digest(source)
+ target = self.root / (checksum + '.pt')
+ if target.exists() and digest(target) != checksum:
+ target = self.root / (checksum + '-' + uuid.uuid4().hex + '.pt')
+ if not target.exists():
+ temporary = source if move else target.with_suffix('.partial-' + uuid.uuid4().hex)
+ if not move:
+ with temporary.open('xb') as output, source.open('rb') as stream:
+ shutil.copyfileobj(stream, output, length=1024 * 1024)
+ if digest(temporary) != checksum:
+ raise ValueError('Input changed during cache copy; partial retained')
+ from .file_ops import publish
+ publish(temporary, target)
+ receipt = self.receipt(key)
+ if receipt.exists():
+ # Keep old receipts too, including malformed ones, for diagnostics.
+ prior = receipt.read_bytes()
+ if not (self.root / (receipt.stem + '-' + hashlib.sha256(prior).hexdigest()[:16] + '.previous.json')).exists():
+ (self.root / (receipt.stem + '-' + hashlib.sha256(prior).hexdigest()[:16] + '.previous.json')).write_bytes(prior)
+ save(receipt, {'identity': self.identity, 'file': target.name, 'bytes': size,
+ 'sha256': checksum, 'metrics': metrics, 'created_epoch': time.time()})
+ return True
diff --git a/freevideo_engine/install.py b/freevideo_engine/install.py
new file mode 100644
index 0000000..ae2f028
--- /dev/null
+++ b/freevideo_engine/install.py
@@ -0,0 +1,68 @@
+"""Install model code at a pinned commit without changing the user's models."""
+import json
+import importlib.util
+import os
+from pathlib import Path
+import subprocess
+import sys
+import time
+from .paths import vdn_root
+from . import network
+
+
+def install(root=None, *, packages=False):
+ dependencies = json.loads((Path(__file__).resolve().parent / 'dependencies.json').read_text(encoding='utf-8'))
+ spec = dependencies['vdn']
+ root = (root or vdn_root()).expanduser().resolve()
+ network.clone(root, spec['url'], spec['commit'])
+ head = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=root, text=True).strip()
+ dirty = subprocess.check_output(['git', 'status', '--porcelain', '--untracked-files=no'], cwd=root, text=True)
+ if head != spec['commit'] or dirty:
+ raise RuntimeError('Existing VDN checkout does not match the clean pinned dependency; use another --vdn-root.')
+ diffusers = prepare_diffusers(root, dependencies)
+ if packages:
+ # Install Torch explicitly first; preserve that CUDA build here.
+ if not os.environ.get('FREEVIDEO_UV') and importlib.util.find_spec('pip') is None:
+ subprocess.run([sys.executable, '-m', 'ensurepip', '--upgrade'], check=True)
+ def install_packages(env, source):
+ # The managed installer already has uv, including native SOCKS
+ # support; avoid pip's optional PySocks dependency during bootstrap.
+ command = ([env['FREEVIDEO_UV'], 'pip', 'install', '--python', sys.executable]
+ if env.get('FREEVIDEO_UV') else [sys.executable, '-m', 'pip', 'install'])
+ result = subprocess.run([*command, '-e', str(diffusers), '--no-deps'],
+ env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
+ print(result.stdout, flush=True)
+ if result.returncode:
+ raise RuntimeError(result.stdout[-2500:])
+ network.package_command(network.installed_plan(), 'pypi', install_packages)
+ # src is imported from this pinned checkout by paths.add_vdn(). Its
+ # training-package metadata unconditionally requires FA4, whereas the
+ # Engine also runs without that optional attention backend.
+ print(json.dumps({'vdn_root': str(root), 'commit': head, 'packages_installed': packages,
+ 'diffusers_tree': dependencies['diffusers']['patched_tree'],
+ 'models_downloaded': False, 'environment_variable': 'FREEVIDEO_VDN_ROOT'}))
+
+
+def prepare_diffusers(root, dependencies):
+ """Build the official two-patch tree atomically; never reset a user checkout."""
+ spec = dependencies['diffusers']
+ target = root / dependencies['vdn']['diffusers_subdirectory']
+ if not target.exists():
+ checkout = root / ('engine-diffusers-' + str(time.time_ns()))
+ network.clone(checkout, spec['url'], spec['base'])
+ patches = sorted((root / 'diffusers_patches').glob('*.patch'))
+ if len(patches) != 2:
+ raise RuntimeError('Expected the two pinned VDN Diffusers patches')
+ for path in patches:
+ subprocess.run(['git', 'apply', '--index', str(path)], cwd=checkout, check=True)
+ tree = subprocess.check_output(['git', 'write-tree'], cwd=checkout, text=True).strip()
+ if tree != spec['patched_tree']:
+ raise RuntimeError('Patched Diffusers source tree does not match the tested dependency')
+ subprocess.run(['git', '-c', 'user.name=VDN Engine dependency setup', '-c', 'user.email=engine@localhost',
+ 'commit', '--quiet', '-m', 'Apply pinned OpenVDN MiniMax H3 patches'], cwd=checkout, check=True)
+ checkout.rename(target)
+ tree = subprocess.check_output(['git', 'rev-parse', 'HEAD^{tree}'], cwd=target, text=True).strip()
+ dirty = subprocess.check_output(['git', 'status', '--porcelain', '--untracked-files=no'], cwd=target, text=True)
+ if tree != spec['patched_tree'] or dirty:
+ raise RuntimeError('Existing Diffusers differs from the clean tested tree; use another VDN dependency directory.')
+ return target
diff --git a/freevideo_engine/install_schedule.py b/freevideo_engine/install_schedule.py
new file mode 100644
index 0000000..ae9d246
--- /dev/null
+++ b/freevideo_engine/install_schedule.py
@@ -0,0 +1,45 @@
+"""Run ready installation tasks with bounded workers and exclusive writers."""
+from concurrent.futures import ThreadPoolExecutor, wait, FIRST_COMPLETED, CancelledError
+
+
+def run(tasks, cancel, *, workers=3, progress=None):
+ names = set(tasks)
+ for name, task in tasks.items():
+ if not set(task.get('after', ())).issubset(names):
+ raise ValueError('Unknown installation prerequisite: ' + name)
+ pending = set(names)
+ # Validate the whole graph before doing any installation writes.
+ while pending:
+ ready = {name for name in pending if not set(tasks[name].get('after', ())) & pending}
+ if not ready:
+ raise ValueError('Cyclic installation prerequisites')
+ pending -= ready
+ pending, active, results, held = dict(tasks), {}, {}, set()
+ with ThreadPoolExecutor(max_workers=workers, thread_name_prefix='install') as pool:
+ try:
+ while pending or active:
+ if cancel.is_set():
+ raise CancelledError('Installation cancelled; completed files retained')
+ for name, task in list(pending.items()):
+ resources = set(task.get('writes', ()))
+ if (len(active) == workers or not set(task.get('after', ())).issubset(results)
+ or resources & held):
+ continue
+ held.update(resources)
+ del pending[name]
+ if progress:
+ progress(name, 'running', len(results), len(tasks))
+ active[pool.submit(task['run'])] = (name, resources)
+ done, _ = wait(active, timeout=.1, return_when=FIRST_COMPLETED)
+ for future in done:
+ name, resources = active.pop(future)
+ results[name] = future.result()
+ held -= resources
+ if progress:
+ progress(name, 'complete', len(results), len(tasks))
+ except BaseException:
+ cancel.set()
+ for future in active:
+ future.cancel()
+ raise
+ return results
diff --git a/freevideo_engine/install_tuning.py b/freevideo_engine/install_tuning.py
new file mode 100644
index 0000000..7518b99
--- /dev/null
+++ b/freevideo_engine/install_tuning.py
@@ -0,0 +1,74 @@
+"""Bound installation work by live CPU/RAM and reuse immutable artifacts."""
+from __future__ import annotations
+import hashlib
+import json
+from pathlib import Path
+from .storage import conversion_source, fingerprint
+
+GiB = 1 << 30
+
+
+def build_parallelism(ram_budget_bytes, cpu_threads):
+ # Measured serial Sage2 build: 3.70 GiB peak PSS. Allow 4 GiB per
+ # compiler, 2 GiB coordination space and another 20% budget margin.
+ jobs = max(1, min(8, max(1, cpu_threads // 2), int((ram_budget_bytes * .8 - 2 * GiB) // (4 * GiB))))
+ return {'jobs': jobs, 'nvcc_threads': 2, 'parallel_extensions': 1,
+ 'estimated_peak_bytes': (jobs * 4 + 2) * GiB,
+ 'ram_budget_bytes': ram_budget_bytes, 'cpu_threads': cpu_threads}
+
+
+def required_models(rows, reuse_cache):
+ if not reuse_cache:
+ return rows
+ # Runtime consumes the prepared transformer, its configs, and original
+ # decoders. Original BF16/LoRA tensors are only needed for conversion.
+ return [r for r in rows if not conversion_source(r)]
+
+
+def cache_compatible(path, capability):
+ try:
+ value = json.loads((Path(path) / 'manifest.json').read_text(encoding='utf-8'))
+ if value.get('format') == 'freevideo-fp8-slim-v1':
+ from .adaln_assets import validate_catalog
+ validate_catalog(value, sum(r['group'].startswith('blocks/') for r in value['groups']))
+ native_format = 'per_tensor' if capability[0] >= 10 else 'rowwise'
+ return (value.get('precision') == 'fp8' and bool(value.get('groups')) and
+ (tuple(capability) < (8, 9) or value.get('scale_granularity') == native_format))
+ except (OSError, ValueError, TypeError):
+ return False
+
+
+def discover_prepared(folder, capability):
+ """Recognize a downloaded engine bundle in the user's selected model folder."""
+ if not folder:
+ return None
+ root = Path(folder).expanduser().resolve()
+ candidates = [root, root / 'cache', root / 'vdn-h3-edge/cache', root / 'vdn-minimax-h3-edge/cache']
+ for candidate in candidates:
+ if cache_compatible(candidate, capability):
+ return candidate
+ return None
+
+
+def unchanged_before(path, receipt):
+ """Receipts only apply to files unchanged since that receipt was written."""
+ identity = fingerprint(path)
+ changed = identity.get('change_time_ns', identity['ctime_ns'])
+ # Equal timestamps cannot establish ordering (notably in fast Windows
+ # writes). Unknown ChangeTime must not fall back to creation time.
+ return changed is not None and max(identity['mtime_ns'], changed) < receipt.stat().st_mtime_ns
+
+
+def hf_receipt(path, row, directory):
+ receipt = directory / '.cache/huggingface/download' / (row['file'] + '.metadata')
+ try:
+ revision, etag, completed = receipt.read_text(encoding='utf-8').splitlines()[:3]
+ return (revision == row['revision'] and etag == (row.get('sha256') or row.get('git_blob')) and
+ float(completed) > 0 and unchanged_before(path, receipt) and
+ max(path.stat().st_mtime, path.stat().st_ctime) <= float(completed))
+ except (OSError, ValueError):
+ return False
+
+
+def wheel_key(identity):
+ return hashlib.sha256(json.dumps(identity, sort_keys=True).encode()).hexdigest()[:24]
diff --git a/freevideo_engine/installation_diagnostics.py b/freevideo_engine/installation_diagnostics.py
new file mode 100644
index 0000000..ea859df
--- /dev/null
+++ b/freevideo_engine/installation_diagnostics.py
@@ -0,0 +1,166 @@
+"""Collect the current installation's evidence into a local, redacted report."""
+import json
+from pathlib import Path
+
+from . import diagnostic_resources as d
+
+
+def _json(path, limit=2*1024*1024):
+ with path.open('rb') as stream:
+ raw = stream.read(limit+1)
+ return d.mapping(json.loads(raw)) if len(raw) <= limit else {}
+
+
+def collect(root, since):
+ """Never pick a different/old installation merely because it is the latest."""
+ try:
+ root = Path(root).resolve()
+ run = Path(_json(root/'machine.json')['setup_run']).resolve()
+ if run.parent != (root/'setup-runs').resolve() or (run/'plan.json').stat().st_mtime < since-1:
+ return {}
+ state = _json(run/'status.json')
+ rows = []
+ try:
+ with (run/'ram.jsonl').open('rb') as stream:
+ size = stream.seek(0, 2)
+ stream.seek(max(0, size-65536))
+ lines = stream.read(65536).splitlines()
+ if size > 65536:
+ lines = lines[1:]
+ for line in lines[-32:]:
+ try:
+ rows.append(d.mapping(json.loads(line)))
+ except (ValueError, UnicodeError):
+ pass
+ except OSError:
+ pass
+ # ComfyUI dependency preparation runs in launcher/host-runs rather
+ # than setup-runs. Keep only structured package failure events; the
+ # command output remains available in the local manual bundle.
+ package_attempts = []
+ host_root = root / 'launcher' / 'host-runs'
+ if host_root.is_dir():
+ candidates = sorted((path for path in host_root.iterdir() if path.is_dir()),
+ key=lambda path: path.name)[-3:]
+ for host in candidates:
+ try:
+ if host.stat().st_mtime < since - 1:
+ continue
+ with (host / 'network.jsonl').open('rb') as stream:
+ lines = stream.read(256 * 1024).splitlines()[-64:]
+ for line in lines:
+ try:
+ item = d.mapping(json.loads(line))
+ except (ValueError, UnicodeError):
+ continue
+ if item.get('action') != 'package-failed':
+ continue
+ package_attempts.append({key: item[key] for key in
+ ('source', 'route', 'error_type', 'returncode', 'summary') if key in item})
+ except OSError:
+ continue
+ return dict(state=state, samples=rows, package_attempts=package_attempts[-16:])
+ except (OSError, ValueError, KeyError, TypeError):
+ return {}
+
+
+def _errors(rows):
+ result = []
+ for row in d.sequence(rows)[:16]:
+ row = d.mapping(row)
+ item = d.numbers(row, ('pid', 'parent_pid', 'winerror', 'errno'))
+ if type(row.get('exited')) is bool:
+ item['exited'] = row['exited']
+ # Keep stable error codes separate from the redacted error details.
+ item['function'] = ('ProcessVmCounters' if 'ProcessVmCounters' in str(row.get('error', ''))
+ else 'process_memory')
+ result.append(item)
+ return result
+
+
+def resources(value):
+ state = d.mapping(d.mapping(value).get('state'))
+ memory = d.mapping(state.get('memory'))
+ result = d.resources(dict(ram=memory))
+ result['ram'].update(d.numbers(memory, ('monitor_unreadable_samples', 'monitor_recoveries',
+ 'monitor_consecutive_unreadable')))
+ result['memory_read_errors'] = _errors(memory.get('process_tree_memory_errors'))
+ samples = [d.mapping(row) for row in d.sequence(d.mapping(value).get('samples'))]
+ # Preserve the failure endpoint plus a few preceding read failures.
+ selected = [row for row in samples[:-1] if row.get('guard_bytes', row.get('pss_bytes')) is None][-7:]
+ if samples:
+ selected.append(samples[-1])
+ result['ram_last'] = d.numbers(samples[-1], d.RAM_SAMPLE)
+ result['ram_samples'] = []
+ for row in selected:
+ item = d.numbers(row, d.RAM_SAMPLE + ('elapsed_seconds', 'consecutive_unreadable'))
+ item['sample_complete'] = row.get('guard_bytes', row.get('pss_bytes')) is not None
+ item['unreadable_memory_pids'] = [pid for pid in d.sequence(row.get('unreadable_memory_pids'))[:16]
+ if type(pid) is int and pid >= 0]
+ item['memory_read_errors'] = _errors(row.get('memory_read_errors'))
+ result['ram_samples'].append(item)
+ return result
+
+
+def enrich(result, value):
+ """Summarize installation resources, stages and failures."""
+ value = d.mapping(value)
+ if not value:
+ result['coverage']['installation_memory'] = False
+ return result
+ state = d.mapping(value.get('state'))
+ result['worker_resources'] = resources(value)
+ result['request_memory_peaks'] = {key: amount for key, amount in result['worker_resources']['ram'].items()
+ if key.startswith('process_tree_peak_') and type(amount) in (int, float)}
+ result['hardware_stage'] = 'installation_start'
+ result['coverage']['installation_memory'] = bool(result['worker_resources']['ram'] or
+ result['worker_resources']['ram_samples'])
+ result['coverage']['worker_memory'] = bool(result['worker_resources']['ram'])
+ estimate = d.mapping(d.mapping(state.get('plan')).get('policy_estimate'))
+ result['budgets'].update(d.numbers({'ram': estimate.get('ram_budget_bytes')}, ('ram',)))
+ for row in d.sequence(state.get('steps'))[-32:]:
+ row = d.mapping(row)
+ # Step labels are implementation-owned, but still use an explicit list.
+ stage = row.get('label') if row.get('label') in (
+ 'models', 'prepare', 'kernels', 'storage', 'torch', 'packages', 'unified-packages',
+ 'encoder-packages', 'engine-packages', 'dependency-check', 'freeze') else 'installation'
+ item = dict(stage=stage, **d.numbers(row, ('seconds',)))
+ item['resources'] = d.resources(dict(ram=row.get('memory')))
+ item['state'] = row.get('status') if row.get('status') in ('running', 'complete', 'failed') else 'unknown'
+ result['stages'].append(item)
+ if item['state'] == 'failed':
+ result['failure_stage'] = stage
+ reason = str(state.get('resource_guard', ''))
+ if 'RAM monitoring failed' in reason:
+ result['errors'].append(dict(kind='memory_monitor_unavailable', phase=result.get('failure_stage', 'installation')))
+ elif 'crossed its RAM budget' in reason:
+ result['errors'].append(dict(kind='memory_pressure', phase=result.get('failure_stage', 'installation')))
+ for row in d.sequence(value.get('package_attempts'))[-16:]:
+ row = d.mapping(row)
+ item = dict(kind='package_source_failure')
+ for key in ('source', 'route', 'error_type', 'returncode', 'summary'):
+ if key in row and (key == 'returncode' or isinstance(row[key], (str, int, float, bool))):
+ item[key] = row[key]
+ result['errors'].append(item)
+ result['diagnostic_revision'] = 6
+ return result
+
+
+def write(root, since, report):
+ """Retain complete structured evidence without changing installation status."""
+ try:
+ from .diagnostics import Redactor
+ from .diagnostic_summary import summary_report
+ from .monitoring import save
+ root = Path(root)
+ evidence = collect(root, since)
+ payload = dict(report, schema_version=1, kind='installation', installation=evidence)
+ redactor = Redactor([(str(root), '<INSTALL>')])
+ cleaned = json.loads(redactor.data(json.dumps(payload).encode('utf-8'), '.json'))
+ cleaned['analysis'] = enrich(summary_report(cleaned), cleaned['installation'])
+ cleaned['analysis']['kind'] = 'installation'
+ target = root / 'launcher' / 'installation.debug.json'
+ save(target, cleaned)
+ return target
+ except Exception:
+ return None
diff --git a/freevideo_engine/kernel_capabilities.py b/freevideo_engine/kernel_capabilities.py
new file mode 100644
index 0000000..e6ce866
--- /dev/null
+++ b/freevideo_engine/kernel_capabilities.py
@@ -0,0 +1,98 @@
+"""Execution receipts for automatic backend selection, without importing CUDA."""
+import hashlib
+import importlib.metadata
+import json
+from pathlib import Path
+import sys
+
+from .hardware import installed_backends
+from .paths import data_root
+
+PACKAGES = ('torch', 'triton', 'triton-windows', 'diffusers', 'sageattention', 'flash-attn', 'flash-attn-4')
+COMPUTE_FILES = ('attention.py', 'probe.py', 'fp8_gemm.py', 'fp8_ops.py', 'weight_only.py',
+ 'kernel_capabilities.py', 'doctor.py', 'fa4_guard.py', 'runtime.py',
+ 'head_chunk.py', 'blocks.py', 'packing.py', 'dependencies.json')
+
+
+def package_versions():
+ result = {}
+ for name in PACKAGES:
+ try:
+ result[name] = importlib.metadata.version(name)
+ except importlib.metadata.PackageNotFoundError:
+ result[name] = None
+ for distribution in importlib.metadata.distributions():
+ name = (distribution.metadata.get('Name') or '').lower().replace('_', '-')
+ if name.startswith(('nvidia-', 'cuda-')):
+ result[name] = distribution.version
+ return result
+
+
+def identity(hardware, versions=None):
+ package = Path(__file__).parent
+ overlay = data_root() / 'vendor/fa4-b26-valid-tile/flash_attn/cute/flash_fwd.py'
+ return dict(schema_version=1,
+ hardware={k: v for k, v in hardware.to_dict().items() if k in (
+ 'gpu_name', 'capability', 'vram_total', 'system', 'torch_version',
+ 'cuda_version', 'gpu_uuid', 'driver_version')},
+ python=str(Path(sys.executable).resolve()),
+ fa4_overlay_sha256=hashlib.sha256(overlay.read_bytes()).hexdigest() if overlay.is_file() else None,
+ packages=versions if versions is not None else package_versions(),
+ source_sha256={name: hashlib.sha256((package / name).read_bytes()).hexdigest()
+ for name in COMPUTE_FILES})
+
+
+def receipt_path():
+ return data_root() / 'kernel-capabilities.json'
+
+
+def readiness(rows):
+ passed = {row['backend'] for row in rows if row.get('status') == 'complete'}
+ attention = sorted(passed - {'linear'})
+ return dict(ready='linear' in passed and bool(attention), usable_attention_backends=attention,
+ failed_optional_backends=[row['backend'] for row in rows
+ if row.get('status') != 'complete' and row['backend'] != 'linear'])
+
+
+def available_backends(hardware, *, discovered=None, probe_missing=True):
+ """Reuse matching checks; re-probe after a GPU, driver or package/code change.
+
+ Read-only planning may opt out of probing. Execution callers never turn an
+ unavailable requested backend into another backend or a math SDPA kernel.
+ """
+ discovered = set(installed_backends() if discovered is None else discovered)
+ expected = identity(hardware)
+ # Normalize tuples exactly as on disk.
+ expected = json.loads(json.dumps(expected))
+ try:
+ receipt = json.loads(receipt_path().read_text(encoding='utf-8'))
+ if receipt['identity'] != expected or set(receipt['installed_attention_packages']) != discovered:
+ receipt = None
+ elif (not isinstance(receipt.get('kernel_probes'), list)
+ or {row.get('backend') for row in receipt['kernel_probes'] if isinstance(row, dict)} != discovered | {'linear'}
+ or len(receipt['kernel_probes']) != len(discovered) + 1
+ or any(not isinstance(row, dict) or row.get('status') not in ('complete', 'error')
+ for row in receipt['kernel_probes'])):
+ receipt = None
+ except (OSError, ValueError, KeyError, TypeError):
+ receipt = None
+ if receipt is None:
+ if not probe_missing:
+ return discovered
+ from .doctor import doctor
+ receipt = doctor(probe=True, hardware=hardware, backends=discovered)
+ checked = readiness(receipt['kernel_probes'])
+ if not checked['ready']:
+ raise RuntimeError('GPU kernel checks did not find a working linear and attention path. '
+ 'Run freevideo doctor --probe; details: ' + str(receipt_path()))
+ return discovered.intersection(checked['usable_attention_backends'])
+
+
+def fp8_implementation(system, scale_granularity, requested='auto'):
+ if requested not in ('auto', 'torch', 'scaled-mm-epilogue'):
+ raise ValueError('Unknown FP8 GEMM implementation: ' + requested)
+ if requested == 'scaled-mm-epilogue' and scale_granularity != 'rowwise':
+ raise ValueError('The FP8 compatibility implementation is only for rowwise caches; use native Torch for tensorwise scales')
+ if requested != 'auto':
+ return requested
+ return 'scaled-mm-epilogue' if system == 'Windows' and scale_granularity == 'rowwise' else 'torch'
diff --git a/freevideo_engine/keyframes.py b/freevideo_engine/keyframes.py
new file mode 100644
index 0000000..9b9bbf7
--- /dev/null
+++ b/freevideo_engine/keyframes.py
@@ -0,0 +1,53 @@
+"""Validate the pinned VDN first/last-frame conditioning contract."""
+import torch
+
+
+def first_pass_conditions(conditions, target, canvas):
+ """Align target keyframe latents to the smaller first-pass canvas.
+
+ Pad the same border that will be cropped after upscaling, then resample
+ spatially with antialiasing. The original encoded anchors are untouched
+ and used at full resolution for the tail. This also supports native
+ ComfyUI conditioning, where the source pixels are no longer available.
+ """
+ from torch.nn import functional as F
+ from .two_pass import plan
+ sampling = plan(target)
+ if any(canvas[key] != sampling['first'][key] for key in ('width', 'height', 'frames')):
+ raise ValueError('Keyframe resize must match the planned first-pass canvas')
+ anchors, latents = conditions
+ left, top = (sampling['crop'][key] // 16 for key in ('left', 'top'))
+ size = (canvas['height'] // 16, canvas['width'] // 16)
+ resized = []
+ for latent in latents:
+ image = latent.squeeze(2)
+ if left or top:
+ image = F.pad(image, (left, left, top, top), mode='replicate')
+ resized.append(F.interpolate(image, size=size, mode='bilinear',
+ align_corners=False, antialias=True).unsqueeze(2).contiguous())
+ return anchors, resized
+
+
+def validate_conditioning(prompt, tags, conditions, task, width, height):
+ if prompt.ndim != 2 or prompt.shape[1] != 5120 or not prompt.shape[0]:
+ raise ValueError('Expected H3 layer-50 prompt embeddings [L,5120]')
+ if tags.ndim != 1 or tags.shape[0] != prompt.shape[0] or not bool(((tags == 0) | (tags == 1)).all()):
+ raise ValueError('Invalid H3 text/vision token tags')
+ if task == 't2va':
+ if conditions or not bool((tags == 1).all()):
+ raise ValueError('Keyframe input requires an Engine with task=fl2va')
+ return
+ expected_anchors = {'i2va': ('first',), 'l2va': ('last',), 'fl2va': ('first', 'last')}
+ if task not in expected_anchors or not conditions:
+ raise ValueError('Keyframe task requires its encoded anchors')
+ anchors, latents = conditions
+ if tuple(anchors) != expected_anchors[task] or len(latents) != len(anchors):
+ raise ValueError(task + ' requires anchors ' + repr(expected_anchors[task]))
+ if not bool((tags == 0).any()):
+ raise ValueError('Keyframes require prompt embeddings encoded with their images')
+ expected = (1, 24, 1, height // 16, width // 16)
+ for value in latents:
+ if not isinstance(value, torch.Tensor) or tuple(value.shape) != expected:
+ raise ValueError('Keyframe latent shape does not match the requested canvas')
+ if value.dtype != torch.float32 or not bool(torch.isfinite(value).all()):
+ raise ValueError('Keyframe latents must be finite float32 tensors')
diff --git a/freevideo_engine/latent_upscale.py b/freevideo_engine/latent_upscale.py
new file mode 100644
index 0000000..94b8611
--- /dev/null
+++ b/freevideo_engine/latent_upscale.py
@@ -0,0 +1,262 @@
+"""Optional normalized H3 latent upscaler; no Comfy imports or automatic downloads.
+
+Network adapted from LBH-123-AI/Comfyui_Minimax_h3_latent_Upscaler,
+commit 40316cf008b2fd8663263270669eb4da23f89d2c (MIT).
+Copyright (c) 2026 LBH-123-AI. Full notice: licenses/latent-upscaler-MIT.txt.
+FreeVideo changes: independent loader, meta initialization, pinned checkpoint
+validation, normalized-space API and explicit model cleanup.
+"""
+import gc
+import hashlib
+from pathlib import Path
+import time
+
+import torch
+import torch.nn as nn
+import torch.nn.functional as F
+from einops import rearrange
+
+from .two_pass import UPSCALER
+CHECKPOINT_SHA256 = UPSCALER['sha256']
+CHECKPOINT_BYTES = UPSCALER['bytes']
+
+# Checkpoint-specific transform ON TOP OF H3 sampling normalization. The
+# published weights need the same transform as the original node wrapper.
+# Omitting it produces severe color/grid artifacts (upstream issue #60).
+LATENTS_MEAN = (0.858090341091156, -0.9606591463088989, 1.0661640167236328, -0.5090325474739075, -0.2727581858634949, -1.3675414323806763, -0.2553254961967468, -0.26907554268836975, -0.5376840829849243, -0.0464097298681736, 0.6657370328903198, 0.19690127670764923, -0.5460608005523682, -0.4035342037677765, -0.23683024942874908, 0.25928452610969543, -0.30133944749832153, 0.211341992020607, -1.1206848621368408, 0.3581933379173279, -0.04225143790245056, 0.2604829967021942, 0.22864092886447906, 0.7056031823158264)
+LATENTS_STD = (1.2223774194717407, 1.2767263650894165, 1.6831774711608887, 1.7549455165863037, 1.5636216402053833, 2.194143533706665, 0.9653137922286987, 1.0569885969161987, 0.841948926448822, 0.7729952931404114, 1.8955937623977661, 0.946841835975647, 0.7996809482574463, 0.44988900423049927, 0.7197399735450745, 0.6936293244361877, 2.961095094680786, 2.7694199085235596, 3.0496184825897217, 2.1088054180145264, 3.276226282119751, 3.1627357006073, 2.2816812992095947, 2.6127843856811523)
+
+
+def upscale(video, checkpoint, width, height, *, memory_saving=False):
+ """Preserve normalized latent/time axes and return a float32 spatial lift."""
+ if (not isinstance(video, torch.Tensor) or video.ndim != 5 or tuple(video.shape[:2]) != (1,24)
+ or not video.is_floating_point() or not bool(torch.isfinite(video).all())):
+ raise ValueError('Latent upscaling requires finite batch-one, 24-channel video latents')
+ if any(type(value) is not int or value % 32 for value in (width,height)):
+ raise ValueError('Upscale target dimensions must be multiples of 32')
+ source_h, source_w = video.shape[-2:]
+ target_h, target_w = height//16, width//16
+ if target_h < source_h or target_w < source_w or target_h*source_w != target_w*source_h:
+ raise ValueError('Latent upscaling must preserve the aspect ratio without shrinking')
+ scale = target_w/source_w
+ if not 1. <= scale <= 4.:
+ raise ValueError('Latent upscaler supports factors from 1 to 4')
+ if scale == 1.:
+ return video, dict(load_seconds=0.,compute_seconds=0.,scale=1.,noop=True)
+ from .streamed_weights import _open_safetensors
+ checkpoint = Path(checkpoint)
+ tick = time.perf_counter()
+ if checkpoint.stat().st_size != CHECKPOINT_BYTES:
+ raise ValueError('Incomplete latent upscaler checkpoint')
+ with checkpoint.open('rb') as stream:
+ digest = hashlib.file_digest(stream,'sha256').hexdigest()
+ if digest != CHECKPOINT_SHA256:
+ raise ValueError('The selected file is not the supported H3 3D v1 upscaler')
+ model = None
+ try:
+ with torch.device('meta'):
+ model = LatentResizer3D(reuse_buffers=memory_saving)
+ # Keep the same bounded Windows reader as the main engine; do not
+ # create a new copy-on-write mapping/Commit charge for optional weights.
+ with _open_safetensors(checkpoint, framework='pt', device='cpu') as handle:
+ state = {name: handle.get_tensor(name) for name in handle.keys()}
+ model.load_state_dict({key.removeprefix('upscaler.'):value for key,value in state.items()},
+ strict=True,assign=True)
+ del state
+ model = model.eval().requires_grad_(False).to(device=video.device,dtype=torch.float16)
+ if video.is_cuda:torch.cuda.synchronize(video.device)
+ loaded = time.perf_counter()-tick
+ tick = time.perf_counter()
+ mean = video.new_tensor(LATENTS_MEAN, dtype=torch.float16).view(1,-1,1,1,1)
+ std = video.new_tensor(LATENTS_STD, dtype=torch.float16).view(1,-1,1,1,1)
+ normalized = (video.to(torch.float16) - mean) / std
+ result = model(normalized,scale=scale,
+ target_size=(video.shape[2],target_h,target_w),enable_chunking=False)
+ result = (result * std + mean).float()
+ if video.is_cuda:torch.cuda.synchronize(video.device)
+ computed = time.perf_counter()-tick
+ if not bool(torch.isfinite(result).all()):
+ raise RuntimeError('Latent upscaler produced nonfinite values')
+ return result, dict(load_seconds=loaded,compute_seconds=computed,scale=scale,
+ checkpoint_sha256=digest,temporal_chunking=False,normalized_input=True,
+ buffer_reuse=memory_saving,checkpoint_transform='extra_per_channel_v1')
+ finally:
+ del model
+ gc.collect()
+ if video.is_cuda:torch.cuda.empty_cache()
+
+def normalization(channels):
+ return nn.GroupNorm(32, channels)
+
+def zero_module(module):
+ for p in module.parameters():
+ p.detach().zero_()
+ return module
+
+class AttnBlock3D(nn.Module):
+
+ def __init__(self, in_channels):
+ super().__init__()
+ self.norm = normalization(in_channels)
+ self.q = nn.Conv3d(in_channels, in_channels, 1)
+ self.k = nn.Conv3d(in_channels, in_channels, 1)
+ self.v = nn.Conv3d(in_channels, in_channels, 1)
+ self.proj_out = nn.Conv3d(in_channels, in_channels, 1)
+
+ def forward(self, x):
+ h = self.norm(x)
+ q = rearrange(self.q(h), 'b c t h w -> b 1 (t h w) c')
+ k = rearrange(self.k(h), 'b c t h w -> b 1 (t h w) c')
+ v = rearrange(self.v(h), 'b c t h w -> b 1 (t h w) c')
+ h = F.scaled_dot_product_attention(q, k, v)
+ h = rearrange(h, 'b 1 (t h w) c -> b c t h w', t=x.shape[2], h=x.shape[3], w=x.shape[4])
+ return x + self.proj_out(h)
+
+class ResBlockEmb3D(nn.Module):
+
+ def __init__(self, channels, emb_channels, dropout=0, out_channels=None, reuse_buffers=False):
+ super().__init__()
+ self.out_channels = out_channels or channels
+ self.reuse_buffers = reuse_buffers
+ self.in_layers = nn.Sequential(normalization(channels), nn.SiLU(inplace=reuse_buffers), nn.Conv3d(channels, self.out_channels, 3, padding=1))
+ self.emb_layers = nn.Sequential(nn.SiLU(), nn.Linear(emb_channels, 2 * self.out_channels))
+ self.out_norm = normalization(self.out_channels)
+ self.out_layers = nn.Sequential(nn.SiLU(inplace=reuse_buffers), nn.Dropout(p=dropout), zero_module(nn.Conv3d(self.out_channels, self.out_channels, 3, padding=1)))
+ self.skip = nn.Conv3d(channels, self.out_channels, 1) if self.out_channels != channels else nn.Identity()
+
+ def forward(self, x, emb):
+ h = self.in_layers(x)
+ emb_out = self.emb_layers(emb).type(h.dtype)
+ while len(emb_out.shape) < len(h.shape):
+ emb_out = emb_out[..., None]
+ (scale, shift) = torch.chunk(emb_out, 2, dim=1)
+ if self.reuse_buffers:
+ # Preserve the original FP16 rounding after each operation. Only
+ # overwrite the consumed normalization output, never the residual.
+ h = self.out_norm(h)
+ h.mul_(1 + scale).add_(shift)
+ else:
+ h = self.out_norm(h) * (1 + scale) + shift
+ h = self.out_layers(h)
+ return h.add_(self.skip(x)) if self.reuse_buffers else self.skip(x) + h
+
+class TemporalConv(nn.Module):
+
+ def __init__(self, channels, kernel_size=5, reuse_buffers=False):
+ super().__init__()
+ padding = kernel_size // 2
+ self.norm = normalization(channels)
+ self.reuse_buffers = reuse_buffers
+ self.dwconv = nn.Conv3d(channels, channels, kernel_size=(kernel_size, 1, 1), padding=(padding, 0, 0), groups=channels)
+ self.pwconv = nn.Conv3d(channels, channels, kernel_size=1)
+ nn.init.zeros_(self.pwconv.weight)
+ nn.init.zeros_(self.pwconv.bias)
+
+ def forward(self, x):
+ identity = x
+ h = self.norm(x)
+ h = F.silu(h, inplace=self.reuse_buffers)
+ h = self.dwconv(h)
+ h = self.pwconv(h)
+ return h.add_(identity) if self.reuse_buffers else identity + h
+
+class LatentResizer3D(nn.Module):
+
+ def __init__(self, in_channels=24, in_blocks=12, out_blocks=12, channels=512, dropout=0.1, attn=False, temporal_every=2, temporal_kernel=5, reuse_buffers=False):
+ super().__init__()
+ self.conv_in = nn.Conv3d(in_channels, channels, 3, padding=1)
+ embed_dim = 64
+ self.embed = nn.Sequential(nn.Linear(1, embed_dim), nn.SiLU(), nn.Linear(embed_dim, embed_dim))
+ self.in_blocks = nn.ModuleList()
+ for b in range(in_blocks):
+ if (b == 1 or b == in_blocks - 1) and attn:
+ self.in_blocks.append(AttnBlock3D(channels))
+ self.in_blocks.append(ResBlockEmb3D(channels, embed_dim, dropout, reuse_buffers=reuse_buffers))
+ if temporal_every > 0 and b % temporal_every == 0:
+ self.in_blocks.append(TemporalConv(channels, temporal_kernel, reuse_buffers=reuse_buffers))
+ self.out_blocks = nn.ModuleList()
+ for b in range(out_blocks):
+ if (b == 1 or b == out_blocks - 1) and attn:
+ self.out_blocks.append(AttnBlock3D(channels))
+ self.out_blocks.append(ResBlockEmb3D(channels, embed_dim, dropout, reuse_buffers=reuse_buffers))
+ if temporal_every > 0 and b % temporal_every == 0:
+ self.out_blocks.append(TemporalConv(channels, temporal_kernel, reuse_buffers=reuse_buffers))
+ self.norm_out = normalization(channels)
+ self.conv_out = nn.Conv3d(channels, in_channels, 3, padding=1)
+
+ def forward(self, x, scale=None, target_size=None, enable_chunking=True):
+ if target_size is not None:
+ size = target_size
+ elif scale is not None:
+ size = tuple((int(round(s * scale)) for s in x.shape[-3:]))
+ else:
+ return x
+ if size == x.shape[-3:]:
+ return x
+ (B, C, T, H, W) = x.shape
+ tk = 0
+ for b in self.in_blocks:
+ if isinstance(b, TemporalConv):
+ tk = b.dwconv.weight.shape[2]
+ break
+ overlap = tk
+ chunk = 32
+ if not enable_chunking or T <= chunk:
+ return self._forward_seg(x, scale, size)
+ print(f'[MinimaxH3-3D] temporal chunking: T={T} chunks={(T + chunk - 1) // chunk} overlap={overlap}')
+ x_padded = F.pad(x, (0, 0, 0, 0, overlap, overlap), mode='replicate')
+ out_full = torch.zeros(B, C, T, size[-2], size[-1], device=x.device, dtype=x.dtype)
+ weight_full = torch.zeros(1, 1, T, 1, 1, device=x.device, dtype=x.dtype)
+ start = 0
+ while start < T:
+ seg_start = start
+ seg_end = min(T, start + chunk)
+ out_start = max(0, seg_start - overlap)
+ out_end = min(T, seg_end + overlap)
+ lo = max(0, out_start - overlap)
+ hi = min(T + 2 * overlap, out_end + overlap)
+ seg = x_padded[:, :, lo:hi].contiguous()
+ seg_size = (hi - lo, size[-2], size[-1])
+ seg_out = self._forward_seg(seg, scale, seg_size)
+ s0 = out_start + overlap - lo
+ s1 = s0 + (out_end - out_start)
+ valid_out = seg_out[:, :, s0:s1]
+ n_valid = out_end - out_start
+ weight = torch.ones(n_valid, device=x.device, dtype=x.dtype)
+ if seg_start > out_start:
+ blend_len = seg_start - out_start
+ weight[:blend_len] = torch.arange(1, blend_len + 1, device=x.device, dtype=x.dtype) / (blend_len + 1)
+ if out_end > seg_end:
+ blend_len = out_end - seg_end
+ weight[-blend_len:] = torch.arange(blend_len, 0, -1, device=x.device, dtype=x.dtype) / (blend_len + 1)
+ out_full[:, :, out_start:out_end] += valid_out * weight.view(1, 1, n_valid, 1, 1)
+ weight_full[:, :, out_start:out_end] += weight.view(1, 1, n_valid, 1, 1)
+ start += chunk
+ del seg, seg_out, valid_out
+ if start % (chunk * 4) == 0:
+ gc.collect()
+ out_full = out_full / weight_full.clamp(min=1e-08)
+ return out_full
+
+ def _forward_seg(self, x, scale, size):
+ scale_emb = torch.tensor([scale - 1 if scale is not None else 0.0], dtype=x.dtype, device=x.device).unsqueeze(0)
+ emb = self.embed(scale_emb)
+ x = self.conv_in(x)
+ for b in self.in_blocks:
+ if isinstance(b, ResBlockEmb3D):
+ emb_t = emb.expand(x.shape[0], -1)
+ x = b(x, emb_t)
+ else:
+ x = b(x)
+ x = F.interpolate(x, size=size, mode='trilinear', align_corners=False)
+ for b in self.out_blocks:
+ if isinstance(b, ResBlockEmb3D):
+ emb_t = emb.expand(x.shape[0], -1)
+ x = b(x, emb_t)
+ else:
+ x = b(x)
+ x = self.norm_out(x)
+ x = F.silu(x)
+ x = self.conv_out(x)
+ return x
diff --git a/freevideo_engine/launcher/FButton.qml b/freevideo_engine/launcher/FButton.qml
new file mode 100644
index 0000000..b90f147
--- /dev/null
+++ b/freevideo_engine/launcher/FButton.qml
@@ -0,0 +1,34 @@
+import QtQuick
+import QtQuick.Controls.Basic
+
+Button {
+ id: control
+ property bool primary: false
+ property bool selected: false
+ property bool danger: false
+ implicitHeight: Math.max(theme.height, contentItem.implicitHeight + 14)
+ implicitWidth: Math.max(80, contentItem.implicitWidth + leftPadding + rightPadding)
+ leftPadding: 16; rightPadding: 16
+ topPadding: 7; bottomPadding: 7
+ font.pixelSize: theme.body; font.weight: primary ? Font.DemiBold : Font.Medium
+ hoverEnabled: true
+ opacity: enabled ? 1 : 0.42
+ contentItem: Text {
+ text: control.text; font: control.font
+ color: control.primary ? theme.bg : control.danger ? theme.danger : control.selected ? theme.accent
+ : control.flat && !control.hovered ? theme.muted : theme.text
+ horizontalAlignment: Text.AlignHCenter; verticalAlignment: Text.AlignVCenter
+ wrapMode: Text.Wrap
+ Behavior on color { ColorAnimation { duration: 110 } }
+ }
+ background: Rectangle {
+ radius: theme.radiusSm
+ color: control.primary ? (control.down ? theme.accentDim : control.hovered ? theme.accentHover : theme.accent) :
+ control.selected ? theme.accentSubtle : control.down ? theme.sheen : control.hovered ? theme.hover :
+ control.flat ? "transparent" : theme.raised
+ border.width: control.activeFocus && control.visualFocus ? 2 : control.primary || control.flat ? 0 : 1
+ border.color: control.activeFocus && control.visualFocus ? theme.accentHover : control.selected ? theme.accentDim : theme.border
+ Behavior on color { ColorAnimation { duration: 110 } }
+ }
+ Accessible.name: text
+}
diff --git a/freevideo_engine/launcher/FCard.qml b/freevideo_engine/launcher/FCard.qml
new file mode 100644
index 0000000..fa65fd9
--- /dev/null
+++ b/freevideo_engine/launcher/FCard.qml
@@ -0,0 +1,21 @@
+import QtQuick
+import QtQuick.Layouts
+
+Rectangle {
+ id: card
+ default property alias contents: body.data
+ property int padding: 24
+ property alias spacing: body.spacing
+ // Cards that come and go with state (not with the page) settle in.
+ property bool reveal: false
+ implicitHeight: body.implicitHeight + padding * 2
+ radius: theme.radiusMd; color: theme.surface; border.color: theme.border
+ transform: Translate { id: shift }
+ onVisibleChanged: if (visible && reveal) appear.restart()
+ ParallelAnimation {
+ id: appear
+ NumberAnimation { target: card; property: "opacity"; from: 0; to: 1; duration: 220; easing.type: Easing.OutCubic }
+ NumberAnimation { target: shift; property: "y"; from: 6; to: 0; duration: 300; easing.type: Easing.OutCubic }
+ }
+ ColumnLayout { id: body; x: card.padding; y: card.padding; width: parent.width-card.padding*2; spacing: 16 }
+}
diff --git a/freevideo_engine/launcher/FCheck.qml b/freevideo_engine/launcher/FCheck.qml
new file mode 100644
index 0000000..aa2ddf3
--- /dev/null
+++ b/freevideo_engine/launcher/FCheck.qml
@@ -0,0 +1,40 @@
+import QtQuick
+import QtQuick.Controls.Basic
+
+// Themed check box; the label wraps and stays aligned with the first line.
+CheckBox {
+ id: control
+ spacing: 10
+ padding: 4
+ hoverEnabled: true
+ opacity: enabled ? 1 : 0.5
+ indicator: Rectangle {
+ x: control.leftPadding; y: control.topPadding + 1
+ implicitWidth: 18; implicitHeight: 18; radius: theme.radiusXs + 1
+ color: control.checked ? theme.accent : control.hovered ? theme.raised : "transparent"
+ border.width: control.checked ? 0 : 1.5
+ border.color: control.visualFocus ? theme.accentHover : control.hovered ? theme.muted : theme.disabled
+ Behavior on color { ColorAnimation { duration: 110 } }
+ Canvas {
+ anchors.fill: parent
+ opacity: control.checked ? 1 : 0; scale: control.checked ? 1 : 0.5
+ Behavior on opacity { NumberAnimation { duration: 120 } }
+ Behavior on scale { NumberAnimation { duration: 200; easing.type: Easing.OutBack } }
+ onPaint: {
+ var c = getContext("2d"); c.reset()
+ c.strokeStyle = theme.bg; c.lineWidth = 2; c.lineCap = "round"; c.lineJoin = "round"
+ c.beginPath(); c.moveTo(4.5, 9.5); c.lineTo(7.8, 12.6); c.lineTo(13.5, 5.8); c.stroke()
+ }
+ }
+ Rectangle {
+ visible: control.visualFocus; anchors.fill: parent; anchors.margins: -3
+ radius: theme.radiusXs + 4; color: "transparent"; border.width: 2; border.color: theme.accentHover
+ }
+ }
+ contentItem: FText {
+ text: control.text
+ leftPadding: control.indicator.width + control.spacing
+ font.pixelSize: theme.body - 1
+ color: control.enabled ? theme.text : theme.disabled
+ }
+}
diff --git a/freevideo_engine/launcher/FChoice.qml b/freevideo_engine/launcher/FChoice.qml
new file mode 100644
index 0000000..ee69939
--- /dev/null
+++ b/freevideo_engine/launcher/FChoice.qml
@@ -0,0 +1,38 @@
+import QtQuick
+import QtQuick.Controls.Basic
+import QtQuick.Layouts
+
+Button {
+ id: control
+ property string detail: ""
+ checkable: true
+ autoExclusive: true
+ implicitHeight: contentItem.implicitHeight + 32
+ padding: 16
+ hoverEnabled: true
+ opacity: enabled ? 1 : 0.5
+ contentItem: RowLayout {
+ spacing: 12
+ Rectangle {
+ Layout.alignment: Qt.AlignTop; Layout.topMargin: 2
+ width: 18; height: 18; radius: 9; color: "transparent"
+ border.width: control.checked ? 5 : 1.5
+ border.color: control.checked ? theme.accent : theme.disabled
+ Behavior on border.width { NumberAnimation { duration: 120 } }
+ }
+ ColumnLayout {
+ Layout.fillWidth: true; spacing: 4
+ FText { text: control.text; font.pixelSize: theme.strong; font.weight: Font.DemiBold; Layout.fillWidth: true }
+ FText { text: control.detail; font.pixelSize: theme.micro; color: theme.muted; Layout.fillWidth: true }
+ }
+ }
+ background: Rectangle {
+ radius: theme.radiusMd; color: control.checked ? theme.accentSubtle : control.hovered ? theme.raised : theme.surface
+ border.color: control.checked || control.activeFocus ? theme.accent : control.hovered ? theme.sheen : theme.border
+ border.width: control.activeFocus && control.visualFocus ? 2 : 1
+ Behavior on color { ColorAnimation { duration: 110 } }
+ }
+ Accessible.role: Accessible.RadioButton
+ Accessible.name: text
+ Accessible.description: detail
+}
diff --git a/freevideo_engine/launcher/FDialog.qml b/freevideo_engine/launcher/FDialog.qml
new file mode 100644
index 0000000..5c5668e
--- /dev/null
+++ b/freevideo_engine/launcher/FDialog.qml
@@ -0,0 +1,32 @@
+import QtQuick
+import QtQuick.Layouts
+import QtQuick.Controls.Basic
+
+// A short question with one confirming action.
+FPopup {
+ id: dialog
+ property string title: ""
+ property string text: ""
+ property string acceptText: "OK"
+ property string rejectText: ""
+ property bool destructive: false
+ signal accepted()
+ signal rejected()
+ // Escape or an outside click counts as the non-confirming answer.
+ property bool answered: false
+ onAboutToShow: answered = false
+ onClosed: if (!answered) rejected()
+ width: Math.min(460, (parent ? parent.width : 500) - 40)
+ closePolicy: rejectText ? Popup.CloseOnEscape : Popup.NoAutoClose
+ contentItem: ColumnLayout {
+ spacing: 12
+ FText { text: dialog.title; font.pixelSize: theme.section; font.weight: Font.DemiBold; Layout.fillWidth: true }
+ FText { text: dialog.text; color: theme.muted; Layout.fillWidth: true }
+ RowLayout {
+ Layout.fillWidth: true; Layout.topMargin: 12; spacing: 8
+ Item { Layout.fillWidth: true }
+ FButton { visible: !!dialog.rejectText; text: dialog.rejectText; flat: true; onClicked: dialog.close() }
+ FButton { text: dialog.acceptText; primary: !dialog.destructive; danger: dialog.destructive; onClicked: { dialog.answered = true; dialog.accepted(); dialog.close() } }
+ }
+ }
+}
diff --git a/freevideo_engine/launcher/FDivider.qml b/freevideo_engine/launcher/FDivider.qml
new file mode 100644
index 0000000..e005032
--- /dev/null
+++ b/freevideo_engine/launcher/FDivider.qml
@@ -0,0 +1,5 @@
+import QtQuick
+import QtQuick.Layouts
+
+// Hairline between rows of a group or sections of a card.
+Rectangle { Layout.fillWidth: true; implicitHeight: 1; color: theme.border }
diff --git a/freevideo_engine/launcher/FField.qml b/freevideo_engine/launcher/FField.qml
new file mode 100644
index 0000000..c619e6e
--- /dev/null
+++ b/freevideo_engine/launcher/FField.qml
@@ -0,0 +1,19 @@
+import QtQuick
+import QtQuick.Controls.Basic
+
+TextField {
+ id: control
+ implicitHeight: theme.height + 4
+ color: theme.text; placeholderTextColor: theme.disabled
+ selectionColor: theme.accentDim; selectedTextColor: theme.text
+ font.pixelSize: theme.body
+ leftPadding: 14; rightPadding: 14
+ selectByMouse: true
+ opacity: enabled ? 1 : 0.5
+ background: Rectangle {
+ radius: theme.radiusSm; color: theme.bg
+ border.width: control.activeFocus ? 2 : 1
+ border.color: control.activeFocus ? theme.accent : control.hovered ? theme.sheen : theme.border
+ Behavior on border.color { ColorAnimation { duration: 140 } }
+ }
+}
diff --git a/freevideo_engine/launcher/FGroup.qml b/freevideo_engine/launcher/FGroup.qml
new file mode 100644
index 0000000..e2ea906
--- /dev/null
+++ b/freevideo_engine/launcher/FGroup.qml
@@ -0,0 +1,16 @@
+import QtQuick
+import QtQuick.Layouts
+
+// A titled group of settings rows. Put FDivider between rows.
+ColumnLayout {
+ id: group
+ default property alias contents: body.data
+ property string title: ""
+ spacing: 8
+ FText { visible: !!group.title; text: group.title; font.pixelSize: theme.micro; font.weight: Font.DemiBold; color: theme.muted; Layout.leftMargin: 4 }
+ Rectangle {
+ Layout.fillWidth: true; implicitHeight: body.implicitHeight + 28
+ radius: theme.radiusMd; color: theme.surface; border.color: theme.border
+ ColumnLayout { id: body; x: 16; y: 14; width: parent.width - 32; spacing: 12 }
+ }
+}
diff --git a/freevideo_engine/launcher/FIcon.qml b/freevideo_engine/launcher/FIcon.qml
new file mode 100644
index 0000000..f1a8fb7
--- /dev/null
+++ b/freevideo_engine/launcher/FIcon.qml
@@ -0,0 +1,24 @@
+import QtQuick
+
+Canvas {
+ property string kind: "folder"
+ property color ink: theme.accent
+ property color knockout: theme.bg
+ implicitWidth: 24; implicitHeight: 24
+ onKindChanged: requestPaint()
+ onInkChanged: requestPaint()
+ onKnockoutChanged: requestPaint()
+ onPaint: {
+ var c = getContext("2d"); c.reset(); c.scale(width/24, height/24)
+ c.strokeStyle = ink; c.lineWidth = 1.6; c.lineCap = "round"; c.lineJoin = "round"; c.beginPath()
+ if (kind === "folder") { c.moveTo(3,7); c.lineTo(3,19); c.lineTo(21,19); c.lineTo(21,7); c.lineTo(12,7); c.lineTo(10,4); c.lineTo(3,4); c.closePath() }
+ else if (kind === "play") { c.moveTo(8,4); c.lineTo(20,12); c.lineTo(8,20); c.closePath() }
+ else if (kind === "terminal") { c.rect(2,4,20,16); c.moveTo(6,9); c.lineTo(9,12); c.lineTo(6,15); c.moveTo(12,15); c.lineTo(17,15) }
+ else if (kind === "download") { c.moveTo(12,3); c.lineTo(12,15); c.moveTo(7,10); c.lineTo(12,15); c.lineTo(17,10); c.moveTo(4,16); c.lineTo(4,21); c.lineTo(20,21); c.lineTo(20,16) }
+ else if (kind === "settings") { c.moveTo(5,3); c.lineTo(5,21); c.moveTo(12,3); c.lineTo(12,21); c.moveTo(19,3); c.lineTo(19,21); c.stroke(); c.beginPath(); c.fillStyle=knockout; c.rect(2,7,6,4); c.rect(9,14,6,4); c.rect(16,6,6,4); c.fill() }
+ else if (kind === "text") { c.moveTo(5,5); c.lineTo(19,5); c.moveTo(12,5); c.lineTo(12,20); c.moveTo(8,20); c.lineTo(16,20) }
+ else if (kind === "decoder") { c.rect(3,5,18,14); c.moveTo(8,9); c.lineTo(5,12); c.lineTo(8,15); c.moveTo(16,9); c.lineTo(19,12); c.lineTo(16,15) }
+ else { c.rect(3,4,18,16); c.moveTo(9,8); c.lineTo(16,12); c.lineTo(9,16); c.closePath() }
+ c.stroke()
+ }
+}
diff --git a/freevideo_engine/launcher/FMeter.qml b/freevideo_engine/launcher/FMeter.qml
new file mode 100644
index 0000000..5f0b224
--- /dev/null
+++ b/freevideo_engine/launcher/FMeter.qml
@@ -0,0 +1,84 @@
+import QtQuick
+
+// Capsule progress. `fraction` is the reported value, -1 while unknown.
+// `shown` eases toward it, so the fill and any percentage bound to `shown`
+// move together instead of jumping between status refreshes. While work
+// runs, a soft sheen crosses the fill; unknown progress sweeps one way, as
+// on macOS; a finished bar settles into the success colour.
+Rectangle {
+ id: meter
+ property real fraction: -1
+ property bool active: false
+ property bool subdued: false
+ readonly property bool known: fraction >= 0
+ readonly property bool complete: known && fraction >= 1
+ property real shown: Math.max(0, Math.min(1, fraction))
+ Behavior on shown { NumberAnimation { duration: 520; easing.type: Easing.OutCubic } }
+ implicitHeight: subdued ? 4 : 8
+ radius: height / 2; color: theme.raised
+ clip: true
+
+ // The fill stays the first child: callers and tests read its width.
+ Rectangle {
+ id: fill
+ visible: meter.known
+ height: parent.height; radius: parent.radius
+ // Never narrower than it is tall, so a small value is still a capsule.
+ width: meter.shown > 0 ? Math.max(height, meter.shown * parent.width) : 0
+ clip: true
+ gradient: Gradient {
+ orientation: Gradient.Horizontal
+ GradientStop {
+ position: 0
+ color: meter.complete ? theme.success : meter.subdued ? theme.accentDim : theme.accent
+ Behavior on color { ColorAnimation { duration: 420 } }
+ }
+ GradientStop {
+ position: 1
+ color: meter.complete ? Qt.lighter(theme.success, 1.14) : meter.subdued ? theme.accent : theme.accentRamp
+ Behavior on color { ColorAnimation { duration: 420 } }
+ }
+ }
+ // Inset by the end radius, so the sheen never squares off the capsule.
+ Item {
+ x: fill.radius; width: Math.max(0, fill.width - 2 * fill.radius); height: fill.height
+ clip: true
+ Rectangle {
+ id: sheen
+ visible: meter.active && !meter.complete && !meter.subdued && fill.width > meter.height * 4
+ width: Math.max(48, meter.width * 0.24); height: parent.height
+ gradient: Gradient {
+ orientation: Gradient.Horizontal
+ GradientStop { position: 0; color: "#00ffffff" }
+ GradientStop { position: 0.5; color: "#38ffffff" }
+ GradientStop { position: 1; color: "#00ffffff" }
+ }
+ SequentialAnimation on x {
+ running: sheen.visible; loops: Animation.Infinite
+ NumberAnimation { from: -sheen.width; to: fill.width + sheen.width; duration: 1700; easing.type: Easing.InOutSine }
+ PauseAnimation { duration: 1100 }
+ }
+ }
+ }
+ }
+ // Unknown progress: one segment travels left to right, growing through
+ // the middle of the track and easing at both ends.
+ Rectangle {
+ id: sweep
+ visible: meter.active && !meter.known && width > 0
+ property real t: 0
+ readonly property real span: meter.width * (0.14 + 0.2 * Math.sin(Math.PI * t))
+ readonly property real start: -span + t * (meter.width + span)
+ // Clamped to the track, so both ends stay round as it enters and leaves.
+ x: Math.max(0, start)
+ width: Math.max(0, Math.min(meter.width, start + span) - x)
+ height: parent.height; radius: parent.radius
+ // Fades in and out at the ends instead of showing a hairline sliver.
+ opacity: Math.min(1, width / (height * 2))
+ color: meter.subdued ? theme.accentDim : theme.accent
+ NumberAnimation on t {
+ running: meter.active && !meter.known; loops: Animation.Infinite
+ from: 0; to: 1; duration: 1500; easing.type: Easing.InOutCubic
+ }
+ }
+}
diff --git a/freevideo_engine/launcher/FNav.qml b/freevideo_engine/launcher/FNav.qml
new file mode 100644
index 0000000..3ab190f
--- /dev/null
+++ b/freevideo_engine/launcher/FNav.qml
@@ -0,0 +1,35 @@
+import QtQuick
+import QtQuick.Controls.Basic
+import QtQuick.Layouts
+
+// Sidebar entry: icon and label, left aligned; the current page is marked.
+Button {
+ id: control
+ property string glyph: "play"
+ property bool selected: false
+ implicitHeight: theme.height + 2
+ leftPadding: 12; rightPadding: 12
+ hoverEnabled: true
+ opacity: enabled ? 1 : 0.45
+ contentItem: RowLayout {
+ spacing: 11
+ FIcon {
+ kind: control.glyph; ink: control.selected ? theme.accent : theme.muted
+ knockout: control.selected ? theme.raised : control.hovered ? theme.surface : theme.bg
+ Layout.preferredWidth: 18; Layout.preferredHeight: 18
+ }
+ FText {
+ text: control.text; Layout.fillWidth: true; elide: Text.ElideRight; maximumLineCount: 1
+ font.weight: control.selected ? Font.DemiBold : Font.Medium
+ color: control.selected ? theme.text : control.hovered ? theme.text : theme.muted
+ }
+ }
+ background: Rectangle {
+ radius: theme.radiusSm
+ color: control.selected ? theme.raised : control.hovered ? theme.surface : "transparent"
+ border.width: control.visualFocus ? 2 : 0; border.color: theme.accentHover
+ Behavior on color { ColorAnimation { duration: 140 } }
+ Rectangle { visible: control.selected; width: 3; height: 18; radius: 2; color: theme.accent; x: 0; anchors.verticalCenter: parent.verticalCenter }
+ }
+ Accessible.name: text
+}
diff --git a/freevideo_engine/launcher/FPopup.qml b/freevideo_engine/launcher/FPopup.qml
new file mode 100644
index 0000000..6aec07d
--- /dev/null
+++ b/freevideo_engine/launcher/FPopup.qml
@@ -0,0 +1,16 @@
+import QtQuick
+import QtQuick.Controls.Basic
+
+// Modal sheet shared by every launcher dialog.
+Popup {
+ anchors.centerIn: Overlay.overlay
+ modal: true; padding: 28
+ background: Rectangle { radius: theme.radiusLg; color: theme.canvas; border.color: theme.sheen }
+ Overlay.modal: Rectangle { color: theme.scrim }
+ enter: Transition { ParallelAnimation {
+ NumberAnimation { property: "opacity"; from: 0; to: 1; duration: 160; easing.type: Easing.OutCubic }
+ NumberAnimation { property: "scale"; from: 0.98; to: 1; duration: 200; easing.type: Easing.OutCubic } } }
+ exit: Transition { ParallelAnimation {
+ NumberAnimation { property: "opacity"; to: 0; duration: 130; easing.type: Easing.InCubic }
+ NumberAnimation { property: "scale"; to: 0.98; duration: 130; easing.type: Easing.InCubic } } }
+}
diff --git a/freevideo_engine/launcher/FSegmented.qml b/freevideo_engine/launcher/FSegmented.qml
new file mode 100644
index 0000000..c3fc4a2
--- /dev/null
+++ b/freevideo_engine/launcher/FSegmented.qml
@@ -0,0 +1,65 @@
+import QtQuick
+import QtQuick.Controls.Basic
+import QtQuick.Layouts
+
+// One choice from a short list: [{value, label}]. Emits picked(value).
+// Segments share the width equally; a raised pill slides to the current one.
+Rectangle {
+ id: group
+ property var options: []
+ property string current: ""
+ property bool compact: false
+ signal picked(string value)
+ readonly property int currentIndex: {
+ for (var i = 0; i < options.length; ++i)
+ if (options[i].value === current) return i
+ return -1
+ }
+ implicitHeight: compact ? theme.heightSm - 4 : theme.heightSm + 2
+ implicitWidth: Math.max(1, options.length) * (compact ? 48 : 104) + 6
+ radius: theme.radiusSm + 1; color: theme.bg; border.color: theme.border
+ Rectangle {
+ id: thumb
+ visible: group.currentIndex >= 0
+ readonly property real slot: (row.width - row.spacing * (group.options.length - 1)) / Math.max(1, group.options.length)
+ x: row.x + group.currentIndex * (slot + row.spacing); y: row.y
+ width: slot; height: row.height
+ radius: theme.radiusSm - 2; color: theme.hover
+ border.color: theme.sheen
+ Behavior on x { NumberAnimation { duration: 180; easing.type: Easing.OutCubic } }
+ }
+ RowLayout {
+ id: row
+ anchors.fill: parent; anchors.margins: 3; spacing: 2
+ Repeater {
+ model: group.options
+ delegate: Button {
+ id: segment
+ required property var modelData
+ required property int index
+ readonly property bool active: group.currentIndex === index
+ objectName: group.objectName ? group.objectName + "-" + modelData.value : ""
+ Layout.fillWidth: true; Layout.fillHeight: true; Layout.preferredWidth: 1
+ padding: 0; leftPadding: group.compact ? 8 : 12; rightPadding: leftPadding
+ hoverEnabled: true
+ contentItem: Text {
+ text: segment.modelData.label
+ font.pixelSize: group.compact ? theme.micro : theme.body - 1
+ font.weight: segment.active ? Font.DemiBold : Font.Medium
+ color: segment.active || segment.hovered ? theme.text : theme.muted
+ horizontalAlignment: Text.AlignHCenter; verticalAlignment: Text.AlignVCenter
+ elide: Text.ElideRight
+ Behavior on color { ColorAnimation { duration: 120 } }
+ }
+ background: Rectangle {
+ radius: theme.radiusSm - 2; color: "transparent"
+ border.width: segment.visualFocus ? 2 : 0; border.color: theme.accent
+ }
+ onClicked: group.picked(modelData.value)
+ Accessible.role: Accessible.RadioButton
+ Accessible.name: modelData.label
+ Accessible.checked: active
+ }
+ }
+ }
+}
diff --git a/freevideo_engine/launcher/FSlider.qml b/freevideo_engine/launcher/FSlider.qml
new file mode 100644
index 0000000..0455b14
--- /dev/null
+++ b/freevideo_engine/launcher/FSlider.qml
@@ -0,0 +1,32 @@
+import QtQuick
+import QtQuick.Controls.Basic
+
+// Discrete slider: a dot marks each step so the levels are visible.
+Slider {
+ id: control
+ hoverEnabled: true
+ opacity: enabled ? 1 : 0.45
+ implicitHeight: 28
+ background: Item {
+ x: control.leftPadding; y: control.topPadding + control.availableHeight / 2 - 2
+ width: control.availableWidth; height: 4
+ Rectangle { anchors.fill: parent; radius: 2; color: theme.raised }
+ Rectangle { width: control.visualPosition * parent.width; height: parent.height; radius: 2; color: theme.accent }
+ Repeater {
+ model: control.stepSize > 0 ? Math.round((control.to - control.from) / control.stepSize) + 1 : 0
+ delegate: Rectangle {
+ required property int index
+ readonly property real at: index / Math.max(1, Math.round((control.to - control.from) / control.stepSize))
+ x: at * (parent.width - 2) - 2; y: -2; width: 6; height: 6; radius: 3
+ color: at <= control.visualPosition ? theme.accent : theme.sheen
+ }
+ }
+ }
+ handle: Rectangle {
+ x: control.leftPadding + control.visualPosition * (control.availableWidth - width)
+ y: control.topPadding + control.availableHeight / 2 - height / 2
+ width: 18; height: 18; radius: 9
+ color: control.pressed ? theme.accentHover : theme.text
+ border.width: control.visualFocus ? 3 : 0; border.color: theme.accent
+ }
+}
diff --git a/freevideo_engine/launcher/FSwitch.qml b/freevideo_engine/launcher/FSwitch.qml
new file mode 100644
index 0000000..99d64d7
--- /dev/null
+++ b/freevideo_engine/launcher/FSwitch.qml
@@ -0,0 +1,37 @@
+import QtQuick
+import QtQuick.Controls.Basic
+import QtQuick.Layouts
+
+// A setting that is on or off: title and optional detail on the left, the
+// switch on the right, as in a settings list. Consent stays an FCheck.
+Switch {
+ id: control
+ property string detail: ""
+ spacing: 16
+ padding: 2
+ hoverEnabled: true
+ opacity: enabled ? 1 : 0.5
+ indicator: Rectangle {
+ x: control.width - width - control.rightPadding
+ y: control.topPadding + (control.availableHeight - height) / 2
+ implicitWidth: 38; implicitHeight: 22; radius: 11
+ color: control.checked ? theme.accent : theme.hover
+ border.width: control.checked ? 0 : 1; border.color: theme.sheen
+ Behavior on color { ColorAnimation { duration: 140 } }
+ Rectangle {
+ width: 18; height: 18; radius: 9; y: 2
+ x: control.checked ? parent.width - width - 2 : 2
+ color: control.checked ? "#ffffff" : theme.text
+ Behavior on x { NumberAnimation { duration: 160; easing.type: Easing.OutCubic } }
+ }
+ Rectangle {
+ visible: control.visualFocus; anchors.fill: parent; anchors.margins: -3
+ radius: 14; color: "transparent"; border.width: 2; border.color: theme.accentHover
+ }
+ }
+ contentItem: ColumnLayout {
+ spacing: 2
+ FText { text: control.text; Layout.fillWidth: true; Layout.rightMargin: control.indicator.width + control.spacing; color: control.enabled ? theme.text : theme.disabled }
+ FText { visible: !!control.detail; text: control.detail; font.pixelSize: theme.micro; color: theme.muted; Layout.fillWidth: true; Layout.rightMargin: control.indicator.width + control.spacing }
+ }
+}
diff --git a/freevideo_engine/launcher/FText.qml b/freevideo_engine/launcher/FText.qml
new file mode 100644
index 0000000..7663c89
--- /dev/null
+++ b/freevideo_engine/launcher/FText.qml
@@ -0,0 +1,9 @@
+import QtQuick
+
+Text {
+ color: theme.text
+ font.pixelSize: theme.body
+ wrapMode: Text.Wrap
+ lineHeight: 1.2
+ textFormat: Text.PlainText
+}
diff --git a/freevideo_engine/launcher/Main.qml b/freevideo_engine/launcher/Main.qml
new file mode 100644
index 0000000..230498c
--- /dev/null
+++ b/freevideo_engine/launcher/Main.qml
@@ -0,0 +1,765 @@
+import QtQuick
+import QtQuick.Controls.Basic
+import QtQuick.Layouts
+
+ApplicationWindow {
+ id: win
+ title: "FreeVideo"
+ color: theme.bg
+ width: 1280; height: 860
+ property var s: initialState
+ Connections { target: backend; function onChanged() { win.s = backend.state } }
+ property bool settingsOpen: false
+ property bool terminalOpen: false
+ property string settingsTab: "downloads"
+ property string modelInfo: "video"
+ property bool modelInfoOpen: false
+ property bool closePending: false
+ property bool accepted: false
+ property bool manualUpdate: false
+ property string previousStatus: ""
+ property string previousReview: ""
+ property bool compact: width < 1000
+ property bool shortWindow: height < 700
+ property bool otherModelLinksOpen: false
+ readonly property bool offlineSelected: s.form.model_method === "manual"
+ readonly property bool needsPackages: s.page === "models" && offlineSelected && !s.offline.runtime && s.offline.models === 0 && s.form.model_dirs.length === 0
+ readonly property int step: s.page === "comfy" ? 0 : s.page === "models" ? 1 : 2
+ property string previousPage: ""
+ property bool errorDetailsOpen: false
+ property string previousError: ""
+ function t(en, zh) { return s.zh ? zh : en }
+ function sourceName(value) { return ({"auto": t("Automatic", "自动选择"), "official": "Hugging Face", "hf-mirror": t("HF Mirror", "HF 镜像"), "modelscope": t("ModelScope", "魔搭")})[value] || value }
+ function number(n) { return typeof n === "number" && isFinite(n) }
+ function fraction(row) { return row && number(row.total) && row.total > 0 && number(row.done) && row.done <= row.total ? row.done / row.total : -1 }
+ function bytes(n) { return n >= 1073741824 ? (n/1073741824).toFixed(1)+" GiB" : (n/1048576).toFixed(1)+" MiB" }
+ function primaryText() {
+ if (s.page === "comfy") return t("Continue", "继续")
+ if (needsPackages) return t("Choose offline packages", "选择离线包")
+ if (s.page === "models") return t("Check & continue", "检查并继续")
+ if (s.page === "launcher") return s.status === "open" ? t("Open FreeVideo", "打开 FreeVideo") : s.status === "restart-required" ? t("Connect again", "重新连接") : t("Launch FreeVideo", "启动 FreeVideo")
+ return s.status === "review" ? t("Install & launch", "安装并启动") : s.status === "restart-required" ? t("Connect again", "重新连接") : t("Check & resume", "检查并继续")
+ }
+ onSChanged: {
+ if (s.page !== previousPage) {
+ Qt.callLater(function() { scroll.contentItem.contentY = 0 })
+ if (previousPage) pageEnter.restart()
+ }
+ previousPage = s.page
+ if (s.status !== previousStatus || s.review_id !== previousReview) accepted = false
+ previousStatus = s.status; previousReview = s.review_id
+ if (s.error !== previousError) {
+ errorDetailsOpen = false
+ if (s.error) Qt.callLater(function() { scroll.contentItem.contentY = 0 })
+ }
+ previousError = s.error
+ }
+ // A new page settles in rather than snapping.
+ ParallelAnimation {
+ id: pageEnter
+ NumberAnimation { target: page; property: "opacity"; from: 0; to: 1; duration: 220; easing.type: Easing.OutCubic }
+ NumberAnimation { target: pageShift; property: "y"; from: 8; to: 0; duration: 260; easing.type: Easing.OutCubic }
+ }
+ onClosing: function(event) {
+ event.accepted = false
+ if (s.busy) closePending = true
+ else backend.close()
+ }
+
+ Rectangle {
+ id: sidebar
+ width: win.compact ? 200 : 232
+ anchors.top: parent.top; anchors.bottom: parent.bottom; anchors.left: parent.left
+ color: theme.bg
+ Rectangle { width: 1; color: theme.border; anchors.right: parent.right; height: parent.height }
+ ColumnLayout {
+ anchors.fill: parent; anchors.margins: 16; spacing: 4
+ RowLayout {
+ Layout.topMargin: 10; Layout.leftMargin: 6; Layout.bottomMargin: 28; spacing: 10
+ Image { source: "../assets/icon.png"; sourceSize.width: 64; sourceSize.height: 64; Layout.preferredWidth: 32; Layout.preferredHeight: 32; smooth: true }
+ FText { text: "FreeVideo"; font.pixelSize: theme.section; font.weight: Font.DemiBold; font.letterSpacing: -0.4 }
+ }
+ FNav { objectName: "navLauncher"; glyph: "play"; text: t("Launch", "启动"); Layout.fillWidth: true; selected: s.page === "launcher"; enabled: s.selected && !s.busy; onClicked: backend.action("launcher", false) }
+ FNav { objectName: "navSetup"; glyph: "download"; text: t("Installation", "安装"); Layout.fillWidth: true; selected: s.page !== "launcher"; enabled: !s.busy && !s.portable; onClicked: backend.action("setup", false) }
+ Item { Layout.fillHeight: true }
+ Rectangle {
+ Layout.fillWidth: true; Layout.bottomMargin: 8; implicitHeight: busyBody.implicitHeight + 28
+ radius: theme.radiusMd; color: theme.surface; border.color: theme.border
+ opacity: s.busy ? 1 : 0; visible: opacity > 0
+ Behavior on opacity { NumberAnimation { duration: 240; easing.type: Easing.OutCubic } }
+ ColumnLayout {
+ id: busyBody; x: 14; y: 14; width: parent.width - 28; spacing: 8
+ RowLayout {
+ Layout.fillWidth: true
+ FText { text: t("Working", "正在处理"); font.pixelSize: theme.micro; font.weight: Font.DemiBold; Layout.fillWidth: true }
+ FText { text: s.elapsed; font.pixelSize: theme.micro; color: theme.muted }
+ }
+ FMeter { Layout.fillWidth: true; fraction: win.fraction(s.overall); active: true; subdued: true }
+ FText { text: s.overall.label || t("Preparing…", "正在准备…"); font.pixelSize: theme.micro; color: theme.muted; Layout.fillWidth: true; maximumLineCount: 1; elide: Text.ElideRight }
+ }
+ }
+ FNav { glyph: "terminal"; text: t("Terminal", "终端"); Layout.fillWidth: true; selected: terminalOpen; onClicked: terminalOpen = !terminalOpen }
+ FNav { objectName: "settingsButton"; glyph: "settings"; text: t("Settings", "设置"); Layout.fillWidth: true; onClicked: settingsOpen = true }
+ FText { text: s.version; color: theme.disabled; font.pixelSize: 11; Layout.leftMargin: 12; Layout.topMargin: 12 }
+ }
+ }
+
+ Rectangle {
+ id: content
+ anchors.left: sidebar.right; anchors.right: parent.right; anchors.top: parent.top; anchors.bottom: terminalPanel.top
+ color: theme.canvas
+ Item {
+ id: header; height: shortWindow ? 52 : 64; anchors.top: parent.top; width: parent.width
+ // The setup guide sits over the centre of the page column; on a
+ // narrow window it gives way to the controls on the right.
+ Row {
+ id: steps
+ objectName: "setupSteps"
+ visible: s.page !== "launcher"
+ spacing: win.compact ? 8 : 12
+ anchors.verticalCenter: parent.verticalCenter
+ x: Math.max(20, Math.min((parent.width - width) / 2, headerTools.x - width - 16))
+ Repeater {
+ model: [t("ComfyUI", "ComfyUI"), t("Models", "模型"), t("Install", "安装")]
+ delegate: Row {
+ required property string modelData; required property int index
+ spacing: win.compact ? 8 : 12
+ Item {
+ visible: index > 0; width: win.compact ? 14 : 32; height: 2; anchors.verticalCenter: parent.verticalCenter
+ Rectangle { anchors.verticalCenter: parent.verticalCenter; width: parent.width; height: 1; color: theme.border }
+ Rectangle {
+ anchors.verticalCenter: parent.verticalCenter; height: 1; color: theme.accentDim
+ width: index <= win.step ? parent.width : 0
+ Behavior on width { NumberAnimation { duration: 420; easing.type: Easing.OutCubic } }
+ }
+ }
+ Row {
+ spacing: 8; anchors.verticalCenter: parent.verticalCenter
+ Rectangle {
+ width: 20; height: 20; radius: 10; anchors.verticalCenter: parent.verticalCenter
+ color: index < win.step ? theme.accentSubtle : index === win.step ? theme.accent : "transparent"
+ border.width: index > win.step ? 1 : 0; border.color: theme.sheen
+ scale: index === win.step ? 1 : 0.9
+ Behavior on color { ColorAnimation { duration: 220 } }
+ Behavior on scale { NumberAnimation { duration: 320; easing.type: Easing.OutBack } }
+ FText { anchors.centerIn: parent; text: index < win.step ? "✓" : String(index + 1); font.pixelSize: 11; font.weight: Font.DemiBold
+ color: index === win.step ? theme.bg : index < win.step ? theme.accent : theme.muted }
+ }
+ FText { anchors.verticalCenter: parent.verticalCenter; text: modelData; font.pixelSize: theme.micro + 1
+ font.weight: index === win.step ? Font.DemiBold : Font.Normal; color: index === win.step ? theme.text : theme.muted }
+ }
+ }
+ }
+ }
+ Row {
+ id: headerTools
+ anchors.right: parent.right; anchors.rightMargin: 20; anchors.verticalCenter: parent.verticalCenter
+ spacing: 12
+ Rectangle {
+ visible: s.status === "open"; anchors.verticalCenter: parent.verticalCenter
+ height: 26; width: connected.implicitWidth + 30; radius: 13; color: theme.successSubtle
+ Rectangle {
+ id: liveDot; x: 11; anchors.verticalCenter: parent.verticalCenter; width: 7; height: 7; radius: 4; color: theme.success
+ SequentialAnimation on opacity {
+ running: liveDot.visible; loops: Animation.Infinite
+ NumberAnimation { to: 0.35; duration: 1400; easing.type: Easing.InOutSine }
+ NumberAnimation { to: 1; duration: 1400; easing.type: Easing.InOutSine }
+ }
+ }
+ FText { id: connected; x: 23; anchors.verticalCenter: parent.verticalCenter; text: t("ComfyUI connected", "ComfyUI 已连接"); font.pixelSize: theme.micro; color: theme.success }
+ }
+ // Both languages stay visible; the current one is marked.
+ FSegmented {
+ objectName: "languageSwitch"; compact: true; anchors.verticalCenter: parent.verticalCenter
+ width: 104; current: s.zh ? "zh" : "en"
+ options: [{value: "zh", label: "中文"}, {value: "en", label: "EN"}]
+ onPicked: function(value) { if (value !== current) backend.edit("language", value) }
+ Accessible.name: t("Language", "语言")
+ }
+ }
+ }
+ ScrollView {
+ id: scroll; objectName: "pageScroll"
+ anchors.top: header.bottom; anchors.bottom: footer.top; width: parent.width
+ clip: true; contentWidth: availableWidth
+ ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
+ ColumnLayout {
+ id: page
+ width: Math.min(scroll.availableWidth - (win.compact ? 36 : 64), 820)
+ x: (scroll.availableWidth-width)/2
+ spacing: shortWindow ? 14 : 20
+ transform: Translate { id: pageShift }
+ Item { height: shortWindow ? 0 : 16 }
+ ColumnLayout {
+ Layout.fillWidth: true; spacing: 8; Layout.bottomMargin: shortWindow ? 0 : 10
+ FText { objectName: "pageHeading"; horizontalAlignment: Text.AlignHCenter; text: s.page === "launcher" ? t("Your workspace", "你的工作空间") : s.page === "comfy" ? t("Set up FreeVideo", "安装 FreeVideo") : s.page === "models" ? t("Set up your models", "准备模型") : s.status === "review" ? t("Ready to install", "确认安装") : s.busy ? t("Setting things up", "正在准备 FreeVideo") : t("Continue your setup", "继续安装"); font.pixelSize: win.compact ? 26 : theme.hero; font.weight: Font.DemiBold; font.letterSpacing: -0.6; Layout.fillWidth: true }
+ }
+
+ Rectangle {
+ id: failureCard
+ objectName: "failureCard"; visible: !!s.error; Layout.fillWidth: true
+ implicitHeight: failureBody.implicitHeight + 36; radius: theme.radiusMd
+ color: theme.dangerSubtle; border.color: theme.dangerLine
+ // A new problem settles in above the page, like other state cards.
+ transform: Translate { id: failureShift }
+ onVisibleChanged: if (visible) failureIn.restart()
+ ParallelAnimation {
+ id: failureIn
+ NumberAnimation { target: failureCard; property: "opacity"; from: 0; to: 1; duration: 220; easing.type: Easing.OutCubic }
+ NumberAnimation { target: failureShift; property: "y"; from: -6; to: 0; duration: 300; easing.type: Easing.OutCubic }
+ }
+ ColumnLayout {
+ id: failureBody; x: 18; y: 18; width: parent.width - 36; spacing: 10
+ FText { text: s.failure.title || t("Something went wrong", "出现问题"); color: theme.danger; font.weight: Font.DemiBold; Layout.fillWidth: true }
+ FText { visible: !!(s.failure.detail || s.failure.action); text: s.failure.detail || s.failure.action; color: theme.text; Layout.fillWidth: true }
+ RowLayout {
+ Layout.fillWidth: true; spacing: 8
+ FButton { visible: s.failure.kind === "download"; text: t("Change source", "切换下载源"); onClicked: { settingsTab = "downloads"; settingsOpen = true } }
+ FButton { objectName: "copyError"; text: t("Copy details", "复制详情"); onClicked: backend.copy(s.error) }
+ FButton { objectName: "showError"; text: errorDetailsOpen ? t("Hide details", "收起详情") : t("Show details", "查看详情"); flat: true; onClicked: errorDetailsOpen = !errorDetailsOpen }
+ }
+ ScrollView {
+ visible: errorDetailsOpen; Layout.fillWidth: true; Layout.preferredHeight: Math.min(160, errorText.implicitHeight+10); clip: true
+ TextArea { id: errorText; objectName: "failureDetails"; text: s.error; readOnly: true; selectByMouse: true; wrapMode: Text.Wrap; color: theme.muted; font.family: theme.mono; font.pixelSize: theme.micro; background: null; textFormat: TextEdit.PlainText }
+ }
+ }
+ }
+
+ ColumnLayout {
+ objectName: "comfyPage"
+ visible: s.page === "comfy"; Layout.fillWidth: true; spacing: shortWindow ? 14 : 20
+ RowLayout {
+ Layout.fillWidth: true; spacing: 12
+ FChoice {
+ objectName: "newComfyMethod"; Layout.fillWidth: true; Layout.minimumWidth: 0; Layout.preferredWidth: 1; Layout.fillHeight: true
+ text: t("Install ComfyUI", "帮我安装 ComfyUI")
+ detail: t("Everything needed to get started.", "自动准备所需环境。")
+ checked: s.form.new_comfy; enabled: !s.busy
+ onClicked: backend.edit("new_comfy", true)
+ }
+ FChoice {
+ objectName: "existingComfyMethod"; Layout.fillWidth: true; Layout.minimumWidth: 0; Layout.preferredWidth: 1; Layout.fillHeight: true
+ text: t("Use existing ComfyUI", "使用已有 ComfyUI")
+ detail: t("Connect your existing installation.", "选择已有安装目录。")
+ checked: !s.form.new_comfy; enabled: !s.busy
+ onClicked: backend.edit("new_comfy", false)
+ }
+ }
+ FCard {
+ Layout.fillWidth: true; padding: shortWindow ? 16 : 22; spacing: 10
+ FText { text: s.form.new_comfy ? t("Install location", "安装位置") : t("ComfyUI folder", "ComfyUI 目录"); font.weight: Font.DemiBold }
+ RowLayout {
+ Layout.fillWidth: true; spacing: 8
+ FField { objectName: "installationPath"; Layout.fillWidth: true; text: s.form.new_comfy ? s.form.destination : s.form.comfy; placeholderText: t("Choose a folder", "选择文件夹"); enabled: !s.busy; onEditingFinished: backend.edit(s.form.new_comfy ? "destination" : "comfy", text) }
+ FButton { text: t("Browse…", "浏览…"); implicitHeight: theme.height + 4; onClicked: backend.browse(s.form.new_comfy ? "destination" : "comfy") }
+ }
+ FText { visible: !s.form.new_comfy; text: t("A separate environment is prepared if needed.", "必要时会准备独立环境。"); color: theme.muted; font.pixelSize: theme.micro; Layout.fillWidth: true }
+ }
+ }
+
+ ColumnLayout {
+ objectName: "modelsPage"
+ visible: s.page === "models"; Layout.fillWidth: true; spacing: 16
+ FCard {
+ objectName: "modelLibraries"; Layout.fillWidth: true; padding: 20; spacing: 10
+ RowLayout {
+ Layout.fillWidth: true; spacing: 12
+ ColumnLayout {
+ Layout.fillWidth: true; spacing: 4
+ FText { text: t("Reuse models · optional", "复用已有模型(可选)"); font.weight: Font.DemiBold; Layout.fillWidth: true }
+ FText { text: t("Add folders to find matching models in their subfolders.", "添加总目录,自动匹配子文件夹中的模型。"); color: theme.muted; font.pixelSize: theme.micro; Layout.fillWidth: true }
+ }
+ FButton { objectName: "addModelFolder"; text: t("Add folder", "添加目录"); enabled: !s.busy; onClicked: backend.browse("model_dirs") }
+ }
+ Repeater {
+ model: s.form.model_dirs
+ delegate: Rectangle {
+ required property string modelData; required property int index
+ Layout.fillWidth: true; implicitHeight: 42; radius: theme.radiusSm; color: theme.bg
+ RowLayout {
+ anchors.fill: parent; anchors.leftMargin: 12; anchors.rightMargin: 4; spacing: 10
+ FIcon { kind: "folder"; ink: theme.muted; Layout.preferredWidth: 16; Layout.preferredHeight: 16 }
+ FText { text: modelData; font.pixelSize: theme.micro + 1; Layout.fillWidth: true; elide: Text.ElideMiddle; maximumLineCount: 1 }
+ FButton { text: t("Remove", "移除"); flat: true; implicitHeight: theme.heightSm; font.pixelSize: theme.micro; enabled: !s.busy; onClicked: backend.removeFolder(index) }
+ }
+ }
+ }
+ }
+ FText { text: t("How would you like to get the rest?", "选择下载方式"); font.pixelSize: theme.strong; font.weight: Font.DemiBold; Layout.fillWidth: true; Layout.topMargin: 6 }
+ RowLayout {
+ Layout.fillWidth: true; spacing: 12
+ FChoice {
+ objectName: "automaticMethod"; Layout.fillWidth: true; Layout.minimumWidth: 0; Layout.preferredWidth: 1; Layout.fillHeight: true
+ text: t("Automatic download", "自动下载")
+ detail: t("Download only what's missing.", "自动补齐缺少的文件。")
+ checked: !offlineSelected; enabled: !s.busy
+ onClicked: backend.edit("model_method", "auto")
+ }
+ FChoice {
+ objectName: "offlineMethod"; Layout.fillWidth: true; Layout.minimumWidth: 0; Layout.preferredWidth: 1; Layout.fillHeight: true
+ text: t("Offline packages", "离线包安装")
+ detail: t("Download from Quark or the web, then import.", "从夸克或网页下载后导入。")
+ checked: offlineSelected; enabled: !s.busy
+ onClicked: backend.edit("model_method", "manual")
+ }
+ }
+ FCard {
+ objectName: "automaticDownloadCard"; visible: !offlineSelected; reveal: true; Layout.fillWidth: true; padding: 20; spacing: 6
+ RowLayout {
+ Layout.fillWidth: true; spacing: 12
+ ColumnLayout {
+ Layout.fillWidth: true; spacing: 4
+ FText { text: t("Download source", "下载源"); font.weight: Font.DemiBold; Layout.fillWidth: true }
+ }
+ FButton { objectName: "downloadSource"; text: s.source_name + " ›"; flat: true; implicitHeight: theme.heightSm; font.pixelSize: theme.micro + 1; onClicked: { settingsTab = "downloads"; settingsOpen = true } }
+ }
+ }
+ FCard {
+ objectName: "offlineImportCard"; visible: offlineSelected; reveal: true; Layout.fillWidth: true; padding: 20; spacing: 14
+ GridLayout {
+ Layout.fillWidth: true; columns: win.compact ? 1 : 2; columnSpacing: 24; rowSpacing: 18
+ ColumnLayout {
+ Layout.fillWidth: true; Layout.preferredWidth: 1; Layout.alignment: Qt.AlignTop; spacing: 10
+ FText { text: t("1. Download your packages", "1. 下载离线包"); font.weight: Font.DemiBold; Layout.fillWidth: true }
+ Repeater {
+ model: cloudLinks
+ delegate: FButton { required property var modelData; required property int index; objectName: "offlineShare-" + index; text: t("Download from Quark ↗", "打开夸克网盘 ↗"); onClicked: backend.link(modelData.url) }
+ }
+ FText { text: t("Get Common models and the pack for your GPU (RTX 30/40 or RTX 50).", "下载「公用模型」和对应显卡包(30/40 系或 50 系)。") + (s.form.new_comfy ? "\n" + t("For a new installation, also get the Environment package.", "全新安装还需「运行环境」包。") : ""); color: theme.muted; font.pixelSize: theme.micro; Layout.fillWidth: true }
+ FButton { objectName: "otherModelLinks"; text: t("Alternative download sources", "其他下载渠道") + (otherModelLinksOpen ? " −" : " +"); flat: true; implicitHeight: theme.heightSm - 2; leftPadding: 0; font.pixelSize: theme.micro + 1; onClicked: otherModelLinksOpen = !otherModelLinksOpen }
+ FText { visible: otherModelLinksOpen; text: t("The same models are also available from Hugging Face or ModelScope.", "同一套模型,也可从 Hugging Face 或魔搭下载。"); color: theme.muted; font.pixelSize: theme.micro; Layout.fillWidth: true }
+ Flow {
+ visible: otherModelLinksOpen; Layout.fillWidth: true; spacing: 8
+ Repeater {
+ model: ["video", "encoder", "decoder"]
+ delegate: FButton {
+ required property string modelData
+ text: modelData === "video" ? t("Video model ↗", "视频模型 ↗") : modelData === "encoder" ? t("Text encoder ↗", "文本编码器 ↗") : t("Video & audio decoders ↗", "音视频解码器 ↗")
+ implicitHeight: theme.heightSm; font.pixelSize: theme.micro + 1; onClicked: { modelInfo = modelData; modelInfoOpen = true }
+ }
+ }
+ }
+ }
+ ColumnLayout {
+ Layout.fillWidth: true; Layout.preferredWidth: 1; Layout.alignment: Qt.AlignTop; spacing: 10
+ FText { text: t("2. Import when downloaded", "2. 下载完成后导入"); font.weight: Font.DemiBold; Layout.fillWidth: true }
+ Rectangle {
+ Layout.fillWidth: true; implicitHeight: Math.max(130, dropContents.implicitHeight + 32); radius: theme.radiusMd
+ color: packageDrop.containsDrag ? theme.accentSubtle : theme.bg
+ border.color: packageDrop.containsDrag ? theme.accent : theme.sheen; border.width: packageDrop.containsDrag ? 2 : 1
+ DropArea {
+ id: packageDrop; objectName: "packageDrop"; anchors.fill: parent; enabled: !s.busy
+ onDropped: function(drop) { if (drop.hasUrls) { backend.importPackages(drop.urls); drop.acceptProposedAction() } }
+ }
+ ColumnLayout {
+ id: dropContents; anchors.centerIn: parent; width: parent.width - 28; spacing: 10
+ FIcon { kind: "download"; ink: packageDrop.containsDrag ? theme.accent : theme.muted; Layout.alignment: Qt.AlignHCenter; Layout.preferredWidth: 22; Layout.preferredHeight: 22 }
+ FText { text: t("Drop ZIPs here — no extraction needed", "将 ZIP 拖到这里,无需解压"); Layout.fillWidth: true; horizontalAlignment: Text.AlignHCenter; font.pixelSize: theme.micro; color: theme.muted }
+ FButton { objectName: "importPackages"; text: t("Choose packages…", "选择离线包…"); enabled: !s.busy; Layout.alignment: Qt.AlignHCenter; Layout.maximumWidth: parent.width; implicitHeight: theme.height; onClicked: backend.browsePackages() }
+ }
+ }
+ }
+ }
+ FMeter { visible: ["running", "preparing"].indexOf(s.offline.status) >= 0; Layout.fillWidth: true; fraction: win.fraction(s.offline); active: visible }
+ FText { visible: !!s.offline.detail; text: s.offline.detail + (number(s.offline.total) ? " · " + bytes(s.offline.done) + " / " + bytes(s.offline.total) : ""); color: theme.muted; font.pixelSize: theme.micro; Layout.fillWidth: true }
+ }
+ FText { visible: s.offline.runtime || s.offline.models > 0; text: "✓ " + (s.offline.runtime ? t("Environment imported", "环境包已导入") + " · " : "") + t("Model packages: ", "已导入模型包:") + s.offline.models; color: theme.success; font.pixelSize: theme.micro; Layout.fillWidth: true }
+ }
+
+ FCard {
+ visible: s.page === "progress" && (s.busy || s.status === "review"); reveal: true
+ Layout.fillWidth: true; padding: 22; spacing: 14
+ RowLayout {
+ Layout.fillWidth: true; spacing: 16
+ ColumnLayout {
+ Layout.fillWidth: true; spacing: 4
+ FText { text: s.status === "review" ? t("Installation plan", "安装计划") : s.overall.label || s.progress.label || t("Checking your installation", "正在检查安装"); font.pixelSize: theme.strong + 1; font.weight: Font.DemiBold; Layout.fillWidth: true }
+ FText { visible: !!s.summary; text: s.summary; color: theme.muted; font.pixelSize: theme.micro + 1; Layout.fillWidth: true }
+ }
+ FText { visible: s.busy; opacity: overallMeter.known ? 1 : 0; Behavior on opacity { NumberAnimation { duration: 200 } } text: Math.round(overallMeter.shown*100)+"%"; font.features: { "tnum": 1 }; font.pixelSize: 28; font.weight: Font.DemiBold; font.letterSpacing: -0.5; Layout.alignment: Qt.AlignTop }
+ }
+ ColumnLayout {
+ Layout.fillWidth: true; spacing: 10; visible: s.busy
+ FMeter { id: overallMeter; objectName: "overallProgress"; Layout.fillWidth: true; fraction: win.fraction(s.overall); active: s.busy }
+ RowLayout {
+ Layout.fillWidth: true
+ FText { text: t("Overall progress", "整体进度") + (number(s.overall.total) ? " · " + Math.floor(s.overall.done || 0) + " / " + s.overall.total : ""); font.pixelSize: theme.micro; color: theme.muted; Layout.fillWidth: true }
+ FText { visible: !!s.elapsed; text: t("Elapsed ", "已用时 ") + s.elapsed; font.pixelSize: theme.micro; color: theme.muted }
+ }
+ Rectangle { Layout.fillWidth: true; Layout.topMargin: 4; Layout.bottomMargin: 4; height: 1; color: theme.border }
+ FText { text: s.detail; visible: !!s.detail; Layout.fillWidth: true; font.pixelSize: theme.micro + 1 }
+ FMeter { Layout.fillWidth: true; fraction: win.fraction(s.progress); active: s.busy; subdued: true }
+ FText { visible: !!s.progress_text; text: s.progress_text; color: theme.muted; font.pixelSize: theme.micro; Layout.fillWidth: true }
+ }
+ ColumnLayout {
+ visible: s.status === "review"; Layout.fillWidth: true; spacing: 12
+ GridLayout {
+ Layout.fillWidth: true; columns: 2; columnSpacing: 16; rowSpacing: 8
+ FText { text: "ComfyUI"; color: theme.muted; font.pixelSize: theme.micro + 1 }
+ FText { text: s.review.comfy; font.pixelSize: theme.micro + 1; Layout.fillWidth: true; elide: Text.ElideMiddle; maximumLineCount: 1 }
+ FText { text: "FreeVideo"; color: theme.muted; font.pixelSize: theme.micro + 1 }
+ FText { text: s.review.engine; font.pixelSize: theme.micro + 1; Layout.fillWidth: true; elide: Text.ElideMiddle; maximumLineCount: 1 }
+ }
+ Rectangle { Layout.fillWidth: true; height: 1; color: theme.border }
+ FCheck { objectName: "installConsent"; text: s.consent; checked: accepted; onToggled: accepted = checked; Layout.fillWidth: true }
+ FButton { text: t("Read licenses ↗", "查看许可证 ↗"); flat: true; implicitHeight: theme.heightSm - 2; leftPadding: 32; font.pixelSize: theme.micro + 1; onClicked: backend.link("https://huggingface.co/OpenVDN/vdn-minimax-h3-edge/blob/main/LICENSE") }
+ }
+ }
+
+ FCard {
+ Layout.fillWidth: true; visible: s.page === "progress"
+ padding: 20; spacing: 0
+ Repeater {
+ // Keep the rows alive when counters or elapsed time change.
+ // Replacing a JS-array model recreates every meter and
+ // restarts its fill animation on each status refresh.
+ model: s.models.length
+ delegate: ColumnLayout {
+ required property int index
+ property var modelData: win.s.models[index]
+ Layout.fillWidth: true; spacing: 10
+ Rectangle { visible: index > 0; Layout.fillWidth: true; height: 1; color: theme.border; Layout.topMargin: 14; Layout.bottomMargin: 12 }
+ RowLayout {
+ Layout.fillWidth: true; spacing: 14
+ Rectangle {
+ implicitWidth: 38; implicitHeight: 38; radius: theme.radiusSm
+ color: modelData.state === "ready" ? theme.successSubtle : theme.raised
+ FIcon { anchors.centerIn: parent; width: 20; height: 20; ink: modelData.state === "ready" ? theme.success : theme.accent; kind: modelData.id === "text" ? "text" : modelData.id === "decoder" ? "decoder" : "video" }
+ }
+ ColumnLayout {
+ spacing: 3; Layout.fillWidth: true
+ FText { text: modelData.title; font.weight: Font.DemiBold; Layout.fillWidth: true }
+ FText { text: modelData.detail + (modelData.rate ? " · " + modelData.rate : ""); font.pixelSize: theme.micro; Layout.fillWidth: true; color: modelData.state === "ready" ? theme.success : theme.muted }
+ }
+ FText { visible: modelData.total > 0; text: modelData.state === "ready" ? bytes(modelData.total) : bytes(modelData.done) + " / " + bytes(modelData.total); font.pixelSize: theme.micro; color: theme.muted; font.features: { "tnum": 1 } }
+ FButton { text: t("Download links ↗", "下载地址 ↗"); implicitHeight: theme.heightSm; flat: true; font.pixelSize: theme.micro + 1; visible: s.form.model_method === "manual"; onClicked: { modelInfo = modelData.id === "text" ? "encoder" : modelData.id; modelInfoOpen = true } }
+ }
+ FMeter { objectName: "modelProgress-" + modelData.id; visible: modelData.total > 0 && modelData.state !== "ready"; Layout.fillWidth: true; Layout.leftMargin: 52; fraction: modelData.done / Math.max(1,modelData.total); subdued: true }
+ }
+ }
+ }
+
+ ColumnLayout {
+ objectName: "launcherPage"
+ visible: s.page === "launcher"; Layout.fillWidth: true; spacing: 16
+ FCard {
+ Layout.fillWidth: true; padding: 26; spacing: 18
+ RowLayout {
+ spacing: 20; Layout.fillWidth: true
+ Item {
+ Layout.preferredWidth: 64; Layout.preferredHeight: 64
+ Image { anchors.fill: parent; source: "../assets/icon.png"; sourceSize.width: 128; sourceSize.height: 128 }
+ // Ready: a check settles onto the icon.
+ Rectangle {
+ width: 24; height: 24; radius: 12; color: theme.success
+ border.width: 3; border.color: theme.surface
+ x: parent.width - width + 4; y: parent.height - height + 4
+ scale: s.status === "open" ? 1 : 0; visible: scale > 0
+ Behavior on scale { NumberAnimation { duration: 380; easing.type: Easing.OutBack } }
+ FText { anchors.centerIn: parent; text: "✓"; color: theme.bg; font.pixelSize: 12; font.weight: Font.Bold }
+ }
+ }
+ ColumnLayout {
+ Layout.fillWidth: true; spacing: 6
+ FText { text: s.status === "open" ? t("FreeVideo is ready", "FreeVideo 已就绪") : s.busy ? t("Starting ComfyUI", "正在启动 ComfyUI") : s.status === "restart-required" ? t("Restart ComfyUI", "请重启 ComfyUI") : s.status === "failed" ? t("Startup interrupted", "启动未完成") : t("Ready to launch", "准备就绪"); font.pixelSize: theme.section + 2; font.weight: Font.DemiBold; Layout.fillWidth: true }
+ FText { text: s.status === "open" ? s.url : s.status === "restart-required" ? t("Restart your running ComfyUI to load the updated nodes, then connect again.", "重启正在运行的 ComfyUI 以载入更新后的节点,再点击重新连接。") : t("Your models and environment are connected.", "已连接你的模型与运行环境。"); color: theme.muted; Layout.fillWidth: true }
+ }
+ }
+ FMeter { visible: s.busy; Layout.fillWidth: true; fraction: win.fraction(s.overall); active: s.busy }
+ RowLayout {
+ visible: s.engine_update_available; Layout.fillWidth: true; spacing: 12
+ FText { text: t("Engine update available", "引擎有可用更新"); color: theme.muted; font.pixelSize: theme.micro; Layout.fillWidth: true }
+ FButton { objectName: "engineUpdateButton"; text: t("Update", "更新"); flat: true; enabled: !s.busy; onClicked: backend.action("update-engine", false) }
+ }
+ RowLayout {
+ visible: s.status === "open"; Layout.fillWidth: true; spacing: 8
+ FButton { text: t("Copy address", "复制地址"); onClicked: backend.copy(s.url) }
+ FButton { text: t("Show terminal", "查看终端"); flat: true; onClicked: terminalOpen = true }
+ }
+ }
+ FCard {
+ Layout.fillWidth: true; padding: 22; spacing: 12
+ FText { text: t("Installation", "安装信息"); font.weight: Font.DemiBold }
+ GridLayout {
+ Layout.fillWidth: true; columns: 2; columnSpacing: 16; rowSpacing: 8
+ FText { text: "ComfyUI"; color: theme.muted; font.pixelSize: theme.micro + 1 }
+ FText { text: s.form.comfy; font.pixelSize: theme.micro + 1; Layout.fillWidth: true; elide: Text.ElideMiddle; maximumLineCount: 1 }
+ FText { text: "FreeVideo"; color: theme.muted; font.pixelSize: theme.micro + 1 }
+ FText { text: s.form.engine; font.pixelSize: theme.micro + 1; Layout.fillWidth: true; elide: Text.ElideMiddle; maximumLineCount: 1 }
+ }
+ RowLayout {
+ spacing: 12; Layout.topMargin: 4
+ FButton { text: t("Create desktop shortcut", "创建桌面快捷方式"); implicitHeight: theme.height; font.pixelSize: theme.micro + 1; enabled: s.can_shortcut && !s.busy; onClicked: backend.action("shortcut", false) }
+ FText { text: ["created","present"].indexOf(s.shortcut.status) >= 0 ? "✓ " + t("Shortcut ready", "快捷方式已就绪") : ""; color: theme.success; font.pixelSize: theme.micro }
+ }
+ FCheck { visible: s.needs_consent; text: s.consent; checked: accepted; onToggled: accepted = checked; Layout.fillWidth: true }
+ FButton { visible: s.needs_consent; text: t("Read licenses ↗", "查看许可证 ↗"); flat: true; implicitHeight: theme.heightSm - 2; leftPadding: 32; font.pixelSize: theme.micro + 1; onClicked: backend.link("https://huggingface.co/OpenVDN/vdn-minimax-h3-edge/blob/main/LICENSE") }
+ }
+ }
+
+ Item { height: 18 }
+ }
+ }
+ Rectangle {
+ id: footer; height: shortWindow ? 64 : 76; anchors.bottom: parent.bottom; width: parent.width; color: theme.canvas
+ Rectangle { height: 1; width: parent.width; color: theme.border }
+ RowLayout {
+ anchors.fill: parent; anchors.leftMargin: 24; anchors.rightMargin: 24; spacing: 10
+ FButton { text: t("Back", "上一步"); flat: true; visible: s.page === "models" || s.page === "progress"; enabled: !s.busy; onClicked: backend.action("back", false) }
+ Item { Layout.fillWidth: true }
+ FButton { visible: s.busy; text: t("Pause", "暂停"); onClicked: backend.action("stop", false) }
+ FButton {
+ objectName: "primaryButton"; primary: true; implicitWidth: Math.max(160, contentItem.implicitWidth+40); implicitHeight: theme.heightLg
+ text: s.busy ? t("Working…", "正在处理…") : primaryText()
+ enabled: !s.busy && !(s.needs_consent && !accepted) && !(s.status === "review" && (!accepted || !!s.error))
+ onClicked: { if (needsPackages) { backend.browsePackages(); return } backend.action(s.page === "launcher" && s.status === "open" ? "browser" : "primary", accepted); if (s.busy && s.page === "launcher") terminalOpen = true }
+ }
+ }
+ }
+ }
+
+ Rectangle {
+ id: terminalPanel; visible: height > 0; color: theme.bg; clip: true
+ anchors.bottom: parent.bottom; anchors.left: sidebar.right; anchors.right: parent.right
+ height: terminalOpen ? Math.min(300, win.height * 0.36) : 0
+ Behavior on height { NumberAnimation { duration: 260; easing.type: Easing.OutCubic } }
+ Rectangle { height: 1; width: parent.width; color: theme.border }
+ ColumnLayout {
+ anchors.fill: parent; anchors.margins: 14; anchors.topMargin: 10; spacing: 8
+ RowLayout {
+ Layout.fillWidth: true; spacing: 6
+ FIcon { kind: "terminal"; ink: theme.muted; Layout.preferredWidth: 16; Layout.preferredHeight: 16 }
+ FText { text: t("Terminal", "终端"); font.pixelSize: theme.micro; font.weight: Font.DemiBold; color: theme.muted; Layout.rightMargin: 8 }
+ ComboBox {
+ visible: s.logs.length > 1; model: s.logs; textRole: "label"; Layout.fillWidth: true; Layout.maximumWidth: 260; implicitHeight: 30
+ font.pixelSize: theme.micro
+ palette.button: theme.raised; palette.buttonText: theme.text; palette.window: theme.surface; palette.windowText: theme.text
+ palette.base: theme.surface; palette.text: theme.text; palette.highlight: theme.accentSubtle; palette.highlightedText: theme.text; palette.dark: theme.muted; palette.mid: theme.border; palette.light: theme.raised
+ onActivated: backend.terminal(s.logs[currentIndex].path)
+ }
+ Item { Layout.fillWidth: true }
+ FButton { text: t("Copy", "复制"); flat: true; implicitHeight: 28; implicitWidth: 64; leftPadding: 8; rightPadding: 8; font.pixelSize: theme.micro; onClicked: backend.copy(s.log) }
+ FButton { text: t("Clear", "清空"); flat: true; implicitHeight: 28; implicitWidth: 64; leftPadding: 8; rightPadding: 8; font.pixelSize: theme.micro; onClicked: backend.clearTerminal() }
+ FButton { text: "×"; Accessible.name: t("Close terminal", "收起终端"); flat: true; implicitWidth: 30; implicitHeight: 28; leftPadding: 4; rightPadding: 4; onClicked: terminalOpen = false }
+ }
+ ScrollView {
+ id: terminalScroll; Layout.fillWidth: true; Layout.fillHeight: true; clip: true
+ TextArea {
+ id: terminalText; objectName: "terminalText"; text: s.log || t("Process output will appear here.", "进程启动后,输出会显示在这里。"); textFormat: TextEdit.PlainText
+ readOnly: true; selectByMouse: true; wrapMode: TextEdit.Wrap; color: s.log ? theme.text : theme.disabled; font.family: theme.mono; font.pixelSize: 12; background: null
+ selectionColor: theme.accentDim
+ onTextChanged: if (!activeFocus) cursorPosition = length
+ }
+ }
+ }
+ }
+
+ FPopup {
+ id: settings; objectName: "settingsDialog"
+ visible: settingsOpen; onClosed: settingsOpen = false
+ width: Math.min(700, win.width-50); height: Math.min(700, win.height-50)
+ closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
+ ColumnLayout {
+ anchors.fill: parent; spacing: 18
+ RowLayout { Layout.fillWidth: true; FText { text: t("Settings", "设置"); font.pixelSize: theme.section + 2; font.weight: Font.DemiBold; Layout.fillWidth: true } FButton { text: "×"; Accessible.name: t("Close settings", "关闭设置"); implicitWidth: 36; implicitHeight: theme.height; leftPadding: 4; rightPadding: 4; flat: true; onClicked: settingsOpen = false } }
+ FSegmented {
+ Layout.fillWidth: true; current: settingsTab; onPicked: function(value) { settingsTab = value }
+ options: [{value: "downloads", label: t("Downloads", "下载")}, {value: "general", label: t("Preferences", "偏好")}, {value: "advanced", label: t("Environment", "环境")}]
+ }
+ ScrollView {
+ id: settingsScroll
+ Layout.fillWidth: true; Layout.fillHeight: true; contentWidth: availableWidth; clip: true
+ ColumnLayout {
+ width: settingsScroll.availableWidth; spacing: 18
+ ColumnLayout {
+ Layout.fillWidth: true; visible: settingsTab === "downloads"; spacing: 20
+ FGroup {
+ Layout.fillWidth: true; title: t("Connection", "连接模式")
+ FSegmented {
+ objectName: "proxyMode"; Layout.fillWidth: true; current: s.proxy_mode
+ onPicked: function(value) { backend.selectProxyMode(value) }
+ options: [{value: "auto", label: t("Auto (recommended)", "自动(推荐)")}, {value: "proxy", label: t("Proxy only", "仅代理")}, {value: "direct", label: t("Direct only", "仅直连")}]
+ }
+ FText {
+ text: s.proxy_mode === "proxy" ? t("Use the current proxy. No direct fallback.", "沿用当前代理,不尝试直连。") : s.proxy_mode === "direct" ? t("Download directly, ignoring proxies.", "忽略代理,直接下载。") : t("Compare proxy and direct connections automatically.", "自动测速,择优使用代理或直连。")
+ color: theme.muted; font.pixelSize: theme.micro; Layout.fillWidth: true
+ }
+ }
+ FGroup {
+ Layout.fillWidth: true; title: t("Download source", "下载源")
+ FSegmented {
+ Layout.fillWidth: true; current: s.source; onPicked: function(value) { backend.selectSource(value) }
+ options: [{value: "auto", label: t("Automatic", "自动选择")}, {value: "official", label: "Hugging Face"}, {value: "hf-mirror", label: t("HF Mirror", "HF 镜像")}, {value: "modelscope", label: t("ModelScope", "魔搭")}]
+ }
+ FText { text: t("Switching also moves the current download. Verified partial data is retained wherever resuming is supported.", "切换会应用于当前下载;支持续传时,继续使用已校验的片段。"); color: theme.muted; font.pixelSize: theme.micro; Layout.fillWidth: true }
+ FDivider {}
+ RowLayout {
+ Layout.fillWidth: true; spacing: 12
+ ColumnLayout {
+ Layout.fillWidth: true; spacing: 2
+ FText { text: t("Connection speed", "连接速度"); Layout.fillWidth: true }
+ FText { text: s.probe.status === "running" ? t("Testing sources: ", "正在测速:") + (s.probe.progress?.done || 0) + "/" + (s.probe.progress?.total || "…") : t("Compare sources on this computer.", "比较本机下载源速度。"); color: theme.muted; font.pixelSize: theme.micro; Layout.fillWidth: true }
+ }
+ FButton { text: s.probe.status === "running" ? t("Testing…", "测速中…") : t("Test speed", "测速"); implicitHeight: theme.heightSm; font.pixelSize: theme.micro + 1; enabled: s.probe.status !== "running"; onClicked: backend.speedTest() }
+ }
+ FText { visible: !!s.probe.error; text: s.probe.error || ""; font.pixelSize: theme.micro; color: theme.danger; Layout.fillWidth: true }
+ FText { visible: !!s.probe.network_unavailable; text: t("No sources reachable. Check your network or connection mode.", "下载源均无法连接,请检查网络或切换连接模式。"); font.pixelSize: theme.micro; color: theme.danger; Layout.fillWidth: true }
+ Repeater {
+ model: s.speeds
+ delegate: RowLayout {
+ required property var modelData
+ Layout.fillWidth: true; Layout.preferredHeight: 26; spacing: 12
+ FText { text: modelData.group; color: theme.muted; font.pixelSize: theme.micro; Layout.preferredWidth: 96 }
+ FText { text: modelData.source; font.pixelSize: theme.micro; Layout.fillWidth: true; elide: Text.ElideRight; maximumLineCount: 1 }
+ FText { text: modelData.ok ? modelData.rate : t("Unavailable", "不可用"); color: modelData.ok ? theme.success : theme.disabled; font.pixelSize: theme.micro; font.weight: Font.Medium }
+ }
+ }
+ }
+ FGroup {
+ Layout.fillWidth: true; title: t("Hugging Face token · optional", "Hugging Face 令牌 · 可选")
+ FField { Layout.fillWidth: true; echoMode: TextInput.Password; placeholderText: s.token_set ? t("Token set for this session", "本次已设置令牌") : "hf_…"; enabled: !s.busy; onEditingFinished: backend.edit("token", text) }
+ RowLayout {
+ Layout.fillWidth: true; spacing: 12
+ FText { text: t("Use your account's download quota. Kept for this session only.", "使用账号下载额度,仅本次打开有效。"); color: theme.muted; font.pixelSize: theme.micro; Layout.fillWidth: true }
+ FButton { text: t("Get a token ↗", "获取令牌 ↗"); flat: true; implicitHeight: theme.heightSm - 2; font.pixelSize: theme.micro + 1; onClicked: backend.link("https://huggingface.co/settings/tokens") }
+ }
+ }
+ }
+ ColumnLayout {
+ Layout.fillWidth: true; visible: settingsTab === "general"; spacing: 20
+ FGroup {
+ Layout.fillWidth: true; title: t("General", "通用")
+ RowLayout {
+ Layout.fillWidth: true; spacing: 12
+ FText { text: t("Language", "语言"); Layout.fillWidth: true }
+ FSegmented {
+ Layout.preferredWidth: 200; current: s.zh ? "zh" : "en"; onPicked: function(value) { if (value !== current) backend.edit("language", value) }
+ options: [{value: "zh", label: "简体中文"}, {value: "en", label: "English"}]
+ }
+ }
+ FDivider {}
+ RowLayout {
+ Layout.fillWidth: true; spacing: 12
+ ColumnLayout {
+ Layout.fillWidth: true; spacing: 2
+ FText { text: t("Updates", "更新"); Layout.fillWidth: true }
+ FText { text: t("Settings are kept in Documents across updates.", "设置保存在文档目录中,更新后保留。"); color: theme.muted; font.pixelSize: theme.micro; Layout.fillWidth: true }
+ }
+ FButton { objectName: "checkUpdatesButton"; text: t("Check now", "检查更新"); implicitHeight: theme.heightSm; font.pixelSize: theme.micro + 1; onClicked: { manualUpdate = true; backend.checkUpdates("") } }
+ }
+ }
+ FGroup {
+ Layout.fillWidth: true; title: t("Compatibility", "兼容性")
+ RowLayout {
+ Layout.fillWidth: true
+ FText { text: t("Level", "档位"); Layout.fillWidth: true }
+ FText { text: s.compatibility.available ? String(s.compatibility.level || 0) + " / 3" : "—"; color: s.compatibility.available ? theme.accent : theme.disabled; font.weight: Font.DemiBold; font.features: { "tnum": 1 } }
+ }
+ FSlider { Layout.fillWidth: true; from: 0; to: 3; stepSize: 1; value: s.compatibility.level || 0; enabled: s.compatibility.available && !s.busy; snapMode: Slider.SnapAlways; onMoved: backend.compatibility(Math.round(value), s.compatibility.automatic) }
+ FText { text: s.compatibility.available ? t("Higher levels lower memory peaks and may generate more slowly.", "档位越高,瞬时负载越低,生成可能变慢。") : t("Available after hardware setup.", "完成硬件检查后可调整。"); color: theme.muted; font.pixelSize: theme.micro; Layout.fillWidth: true }
+ FDivider {}
+ FSwitch { Layout.fillWidth: true; text: t("Adjust automatically", "自动调整"); detail: t("Raise the level after an unexpected interruption.", "异常中断后自动提高档位。"); checked: !!s.compatibility.automatic; enabled: s.compatibility.available && !s.busy; onToggled: backend.compatibility(s.compatibility.level, checked) }
+ }
+ FButton { text: t("Qt notices & licenses ↗", "Qt 组件与许可证 ↗"); flat: true; implicitHeight: theme.heightSm; leftPadding: 4; font.pixelSize: theme.micro; onClicked: backend.link("https://doc.qt.io/qt-6/licenses-used-in-qt.html") }
+ }
+ ColumnLayout {
+ Layout.fillWidth: true; visible: settingsTab === "advanced"; spacing: 20; enabled: !s.busy && !s.portable
+ FGroup {
+ Layout.fillWidth: true; title: t("ComfyUI address", "ComfyUI 地址")
+ FField { Layout.fillWidth: true; text: s.form.url; onEditingFinished: backend.edit("url", text) }
+ }
+ FGroup {
+ Layout.fillWidth: true; title: t("Folders · optional", "目录 · 可选")
+ FText { text: t("Engine folder", "引擎目录"); font.pixelSize: theme.micro; color: theme.muted }
+ RowLayout { Layout.fillWidth: true; spacing: 8; FField { Layout.fillWidth: true; text: s.form.engine; placeholderText: t("Automatic", "自动选择"); onEditingFinished: backend.edit("engine", text) } FButton { text: t("Browse…", "浏览…"); implicitHeight: theme.height + 4; onClicked: backend.browse("engine") } }
+ FText { text: t("ComfyUI Python", "ComfyUI Python"); font.pixelSize: theme.micro; color: theme.muted; Layout.topMargin: 4 }
+ FField { Layout.fillWidth: true; text: s.form.python; placeholderText: t("Path to python.exe", "python.exe 的路径"); onEditingFinished: backend.edit("python", text) }
+ }
+ FGroup {
+ Layout.fillWidth: true; title: t("Maintenance", "维护")
+ FSwitch { Layout.fillWidth: true; text: t("Create a separate ComfyUI environment", "创建独立的 ComfyUI 环境"); checked: s.form.separate; onToggled: backend.edit("separate", checked) }
+ FDivider {}
+ FSwitch { Layout.fillWidth: true; text: t("Repair the engine installation", "修复引擎安装"); checked: s.form.repair; onToggled: backend.edit("repair", checked) }
+ }
+ }
+ Rectangle {
+ visible: !!s.error; Layout.fillWidth: true; implicitHeight: settingsError.implicitHeight + 28; radius: theme.radiusMd; color: theme.dangerSubtle; border.color: theme.dangerLine
+ ColumnLayout {
+ id: settingsError; x: 14; y: 14; width: parent.width - 28; spacing: 8
+ FText { text: s.failure.title + "\n" + (s.failure.action || s.failure.detail); color: theme.danger; font.pixelSize: theme.micro + 1; Layout.fillWidth: true }
+ FButton { text: t("Copy details", "复制详情"); implicitHeight: theme.heightSm; font.pixelSize: theme.micro + 1; onClicked: backend.copy(s.error) }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ FPopup {
+ id: modelDialog; objectName: "modelDialog"
+ visible: modelInfoOpen; onClosed: modelInfoOpen = false
+ width: Math.min(540, win.width-50)
+ height: Math.min(modelContents.implicitHeight + padding*2, win.height-48)
+ contentItem: ScrollView {
+ id: modelScroll; clip: true; contentWidth: availableWidth
+ ColumnLayout {
+ id: modelContents; width: modelScroll.availableWidth
+ spacing: 12
+ FText { text: t("Download models", "下载模型"); font.pixelSize: theme.section + 2; font.weight: Font.DemiBold }
+ FText { text: modelInfo === "video" ? t("Choose your GPU variant on the model page and keep its folder structure.", "按模型页说明选择显卡版本,保留下载的目录结构。") : modelInfo === "decoder" ? t("Download the vae and audio_vae folders.", "下载 vae 和 audio_vae 文件夹。") : t("Download the text encoder to your model folder.", "下载文本编码器,放入模型目录。"); color: theme.muted; Layout.fillWidth: true; Layout.bottomMargin: 4 }
+ Repeater { model: modelLinks[modelInfo]; delegate: FButton { required property var modelData; required property int index; objectName: "modelLink-" + index; text: t(modelData.label, modelData.label_zh) + " ↗"; Layout.fillWidth: true; onClicked: backend.link(modelData.url) } }
+ Repeater { model: cloudLinks; delegate: FButton { required property var modelData; text: t("Quark · ", "夸克 · ") + modelData.label + " ↗"; Layout.fillWidth: true; onClicked: backend.link(modelData.url) } }
+ FText { text: t("When your download finishes, import a FreeVideo ZIP or choose the folder containing your models.", "下载完成后,导入 FreeVideo ZIP 或选择存放模型的文件夹。"); color: theme.muted; font.pixelSize: theme.micro; Layout.fillWidth: true; Layout.topMargin: 6 }
+ RowLayout {
+ Layout.fillWidth: true; spacing: 8
+ FButton { text: t("Close", "关闭"); flat: true; onClicked: modelInfoOpen = false }
+ Item { Layout.fillWidth: true }
+ FButton { text: t("Choose model folder", "选择模型目录"); onClicked: { modelInfoOpen = false; backend.browse("model_dirs") } }
+ FButton { text: t("Import ZIPs", "导入 ZIP"); primary: true; onClicked: { modelInfoOpen = false; backend.browsePackages() } }
+ }
+ }
+ }
+ }
+
+ FPopup {
+ id: updateDialog; visible: !s.busy && (s.update.remind || manualUpdate)
+ objectName: "updateDialog"
+ width: Math.min(540,win.width-40); closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
+ onClosed: { manualUpdate = false; backend.dismissUpdate() }
+ height: Math.min(updateContents.implicitHeight + padding*2, win.height-48)
+ contentItem: ScrollView {
+ id: updateScroll; clip: true; contentWidth: availableWidth
+ ColumnLayout {
+ id: updateContents; width: updateScroll.availableWidth
+ spacing: 14
+ FText { text: s.update.candidate ? t("Update available", "有可用更新") : t("Updates", "更新"); font.pixelSize: theme.section + 2; font.weight: Font.DemiBold }
+ FText { text: s.update.candidate ? t("A new version is available. Updating keeps your models and settings.", "发现新版本,更新会保留模型和设置。") : s.update.status === "current" ? t("You're up to date.", "已是最新版本。") : s.update.status === "development" ? t("Running from source. Update with Git.", "当前从源码运行,请通过 Git 更新。") : s.update.status === "error" ? t("Couldn't check for updates. Try again below.", "暂时无法检查更新,请重试。") : t("Checking the latest release…", "正在检查最新版本…"); color: theme.muted; Layout.fillWidth: true }
+ FMeter { Layout.fillWidth: true; visible: s.update.status === "downloading"; active: true; fraction: s.update.progress && s.update.progress.total ? s.update.progress.done/s.update.progress.total : -1 }
+ FText { visible: !!s.update.error; text: s.update.error || ""; color: theme.danger; Layout.fillWidth: true; font.pixelSize: theme.micro }
+ FField { id: githubToken; visible: !!s.update.error; Layout.fillWidth: true; echoMode: TextInput.Password; placeholderText: t("GitHub token · optional", "GitHub Token · 可选") }
+ RowLayout {
+ Layout.fillWidth: true; Layout.topMargin: 8
+ FButton { objectName: "updateLaterButton"; text: s.update.candidate ? t("Later", "稍后更新") : t("Close", "关闭"); flat: true; onClicked: { manualUpdate = false; backend.dismissUpdate() } }
+ Item { Layout.fillWidth: true }
+ FButton { objectName: "updateNowButton"; text: s.update.status === "ready" ? t("Restart & update", "重启并更新") : s.update.candidate ? t("Update now", "立即更新") : t("Check again", "重新检查"); primary: true; enabled: ["checking","downloading"].indexOf(s.update.status) < 0; onClicked: { manualUpdate = true; backend.update(githubToken.text) } }
+ }
+ }
+ }
+ }
+
+ FDialog {
+ visible: closePending
+ title: t("Pause and close?", "暂停并退出?")
+ text: t("The current task will stop. Downloaded files and your setup are retained for next time.", "会停止当前任务,已下载文件和安装进度会保留,下次可以继续。")
+ acceptText: t("Pause and close", "暂停并退出"); rejectText: t("Keep working", "继续处理"); destructive: true
+ onAccepted: backend.close()
+ onRejected: closePending = false
+ }
+ FDialog {
+ visible: !!s.notice
+ title: t("Compatibility settings", "兼容性设置")
+ text: s.notice || ""
+ acceptText: t("OK", "知道了")
+ onAccepted: backend.dismissNotice()
+ }
+}
diff --git a/freevideo_engine/launcher/licenses/GPL-3.0-only.txt b/freevideo_engine/launcher/licenses/GPL-3.0-only.txt
new file mode 100644
index 0000000..94a9ed0
--- /dev/null
+++ b/freevideo_engine/launcher/licenses/GPL-3.0-only.txt
@@ -0,0 +1,674 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The GNU General Public License is a free, copyleft license for
+software and other kinds of works.
+
+ The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works. By contrast,
+the GNU General Public License is intended to guarantee your freedom to
+share and change all versions of a program--to make sure it remains free
+software for all its users. We, the Free Software Foundation, use the
+GNU General Public License for most of our software; it applies also to
+any other work released this way by its authors. You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+them if you wish), that you receive source code or can get it if you
+want it, that you can change the software or use pieces of it in new
+free programs, and that you know you can do these things.
+
+ To protect your rights, we need to prevent others from denying you
+these rights or asking you to surrender the rights. Therefore, you have
+certain responsibilities if you distribute copies of the software, or if
+you modify it: responsibilities to respect the freedom of others.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must pass on to the recipients the same
+freedoms that you received. You must make sure that they, too, receive
+or can get the source code. And you must show them these terms so they
+know their rights.
+
+ Developers that use the GNU GPL protect your rights with two steps:
+(1) assert copyright on the software, and (2) offer you this License
+giving you legal permission to copy, distribute and/or modify it.
+
+ For the developers' and authors' protection, the GPL clearly explains
+that there is no warranty for this free software. For both users' and
+authors' sake, the GPL requires that modified versions be marked as
+changed, so that their problems will not be attributed erroneously to
+authors of previous versions.
+
+ Some devices are designed to deny users access to install or run
+modified versions of the software inside them, although the manufacturer
+can do so. This is fundamentally incompatible with the aim of
+protecting users' freedom to change the software. The systematic
+pattern of such abuse occurs in the area of products for individuals to
+use, which is precisely where it is most unacceptable. Therefore, we
+have designed this version of the GPL to prohibit the practice for those
+products. If such problems arise substantially in other domains, we
+stand ready to extend this provision to those domains in future versions
+of the GPL, as needed to protect the freedom of users.
+
+ Finally, every program is threatened constantly by software patents.
+States should not allow patents to restrict development and use of
+software on general-purpose computers, but in those that do, we wish to
+avoid the special danger that patents applied to a free program could
+make it effectively proprietary. To prevent this, the GPL assures that
+patents cannot be used to render the program non-free.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ TERMS AND CONDITIONS
+
+ 0. Definitions.
+
+ "This License" refers to version 3 of the GNU General Public License.
+
+ "Copyright" also means copyright-like laws that apply to other kinds of
+works, such as semiconductor masks.
+
+ "The Program" refers to any copyrightable work licensed under this
+License. Each licensee is addressed as "you". "Licensees" and
+"recipients" may be individuals or organizations.
+
+ To "modify" a work means to copy from or adapt all or part of the work
+in a fashion requiring copyright permission, other than the making of an
+exact copy. The resulting work is called a "modified version" of the
+earlier work or a work "based on" the earlier work.
+
+ A "covered work" means either the unmodified Program or a work based
+on the Program.
+
+ To "propagate" a work means to do anything with it that, without
+permission, would make you directly or secondarily liable for
+infringement under applicable copyright law, except executing it on a
+computer or modifying a private copy. Propagation includes copying,
+distribution (with or without modification), making available to the
+public, and in some countries other activities as well.
+
+ To "convey" a work means any kind of propagation that enables other
+parties to make or receive copies. Mere interaction with a user through
+a computer network, with no transfer of a copy, is not conveying.
+
+ An interactive user interface displays "Appropriate Legal Notices"
+to the extent that it includes a convenient and prominently visible
+feature that (1) displays an appropriate copyright notice, and (2)
+tells the user that there is no warranty for the work (except to the
+extent that warranties are provided), that licensees may convey the
+work under this License, and how to view a copy of this License. If
+the interface presents a list of user commands or options, such as a
+menu, a prominent item in the list meets this criterion.
+
+ 1. Source Code.
+
+ The "source code" for a work means the preferred form of the work
+for making modifications to it. "Object code" means any non-source
+form of a work.
+
+ A "Standard Interface" means an interface that either is an official
+standard defined by a recognized standards body, or, in the case of
+interfaces specified for a particular programming language, one that
+is widely used among developers working in that language.
+
+ The "System Libraries" of an executable work include anything, other
+than the work as a whole, that (a) is included in the normal form of
+packaging a Major Component, but which is not part of that Major
+Component, and (b) serves only to enable use of the work with that
+Major Component, or to implement a Standard Interface for which an
+implementation is available to the public in source code form. A
+"Major Component", in this context, means a major essential component
+(kernel, window system, and so on) of the specific operating system
+(if any) on which the executable work runs, or a compiler used to
+produce the work, or an object code interpreter used to run it.
+
+ The "Corresponding Source" for a work in object code form means all
+the source code needed to generate, install, and (for an executable
+work) run the object code and to modify the work, including scripts to
+control those activities. However, it does not include the work's
+System Libraries, or general-purpose tools or generally available free
+programs which are used unmodified in performing those activities but
+which are not part of the work. For example, Corresponding Source
+includes interface definition files associated with source files for
+the work, and the source code for shared libraries and dynamically
+linked subprograms that the work is specifically designed to require,
+such as by intimate data communication or control flow between those
+subprograms and other parts of the work.
+
+ The Corresponding Source need not include anything that users
+can regenerate automatically from other parts of the Corresponding
+Source.
+
+ The Corresponding Source for a work in source code form is that
+same work.
+
+ 2. Basic Permissions.
+
+ All rights granted under this License are granted for the term of
+copyright on the Program, and are irrevocable provided the stated
+conditions are met. This License explicitly affirms your unlimited
+permission to run the unmodified Program. The output from running a
+covered work is covered by this License only if the output, given its
+content, constitutes a covered work. This License acknowledges your
+rights of fair use or other equivalent, as provided by copyright law.
+
+ You may make, run and propagate covered works that you do not
+convey, without conditions so long as your license otherwise remains
+in force. You may convey covered works to others for the sole purpose
+of having them make modifications exclusively for you, or provide you
+with facilities for running those works, provided that you comply with
+the terms of this License in conveying all material for which you do
+not control copyright. Those thus making or running the covered works
+for you must do so exclusively on your behalf, under your direction
+and control, on terms that prohibit them from making any copies of
+your copyrighted material outside their relationship with you.
+
+ Conveying under any other circumstances is permitted solely under
+the conditions stated below. Sublicensing is not allowed; section 10
+makes it unnecessary.
+
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+
+ No covered work shall be deemed part of an effective technological
+measure under any applicable law fulfilling obligations under article
+11 of the WIPO copyright treaty adopted on 20 December 1996, or
+similar laws prohibiting or restricting circumvention of such
+measures.
+
+ When you convey a covered work, you waive any legal power to forbid
+circumvention of technological measures to the extent such circumvention
+is effected by exercising rights under this License with respect to
+the covered work, and you disclaim any intention to limit operation or
+modification of the work as a means of enforcing, against the work's
+users, your or third parties' legal rights to forbid circumvention of
+technological measures.
+
+ 4. Conveying Verbatim Copies.
+
+ You may convey verbatim copies of the Program's source code as you
+receive it, in any medium, provided that you conspicuously and
+appropriately publish on each copy an appropriate copyright notice;
+keep intact all notices stating that this License and any
+non-permissive terms added in accord with section 7 apply to the code;
+keep intact all notices of the absence of any warranty; and give all
+recipients a copy of this License along with the Program.
+
+ You may charge any price or no price for each copy that you convey,
+and you may offer support or warranty protection for a fee.
+
+ 5. Conveying Modified Source Versions.
+
+ You may convey a work based on the Program, or the modifications to
+produce it from the Program, in the form of source code under the
+terms of section 4, provided that you also meet all of these conditions:
+
+ a) The work must carry prominent notices stating that you modified
+ it, and giving a relevant date.
+
+ b) The work must carry prominent notices stating that it is
+ released under this License and any conditions added under section
+ 7. This requirement modifies the requirement in section 4 to
+ "keep intact all notices".
+
+ c) You must license the entire work, as a whole, under this
+ License to anyone who comes into possession of a copy. This
+ License will therefore apply, along with any applicable section 7
+ additional terms, to the whole of the work, and all its parts,
+ regardless of how they are packaged. This License gives no
+ permission to license the work in any other way, but it does not
+ invalidate such permission if you have separately received it.
+
+ d) If the work has interactive user interfaces, each must display
+ Appropriate Legal Notices; however, if the Program has interactive
+ interfaces that do not display Appropriate Legal Notices, your
+ work need not make them do so.
+
+ A compilation of a covered work with other separate and independent
+works, which are not by their nature extensions of the covered work,
+and which are not combined with it such as to form a larger program,
+in or on a volume of a storage or distribution medium, is called an
+"aggregate" if the compilation and its resulting copyright are not
+used to limit the access or legal rights of the compilation's users
+beyond what the individual works permit. Inclusion of a covered work
+in an aggregate does not cause this License to apply to the other
+parts of the aggregate.
+
+ 6. Conveying Non-Source Forms.
+
+ You may convey a covered work in object code form under the terms
+of sections 4 and 5, provided that you also convey the
+machine-readable Corresponding Source under the terms of this License,
+in one of these ways:
+
+ a) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by the
+ Corresponding Source fixed on a durable physical medium
+ customarily used for software interchange.
+
+ b) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by a
+ written offer, valid for at least three years and valid for as
+ long as you offer spare parts or customer support for that product
+ model, to give anyone who possesses the object code either (1) a
+ copy of the Corresponding Source for all the software in the
+ product that is covered by this License, on a durable physical
+ medium customarily used for software interchange, for a price no
+ more than your reasonable cost of physically performing this
+ conveying of source, or (2) access to copy the
+ Corresponding Source from a network server at no charge.
+
+ c) Convey individual copies of the object code with a copy of the
+ written offer to provide the Corresponding Source. This
+ alternative is allowed only occasionally and noncommercially, and
+ only if you received the object code with such an offer, in accord
+ with subsection 6b.
+
+ d) Convey the object code by offering access from a designated
+ place (gratis or for a charge), and offer equivalent access to the
+ Corresponding Source in the same way through the same place at no
+ further charge. You need not require recipients to copy the
+ Corresponding Source along with the object code. If the place to
+ copy the object code is a network server, the Corresponding Source
+ may be on a different server (operated by you or a third party)
+ that supports equivalent copying facilities, provided you maintain
+ clear directions next to the object code saying where to find the
+ Corresponding Source. Regardless of what server hosts the
+ Corresponding Source, you remain obligated to ensure that it is
+ available for as long as needed to satisfy these requirements.
+
+ e) Convey the object code using peer-to-peer transmission, provided
+ you inform other peers where the object code and Corresponding
+ Source of the work are being offered to the general public at no
+ charge under subsection 6d.
+
+ A separable portion of the object code, whose source code is excluded
+from the Corresponding Source as a System Library, need not be
+included in conveying the object code work.
+
+ A "User Product" is either (1) a "consumer product", which means any
+tangible personal property which is normally used for personal, family,
+or household purposes, or (2) anything designed or sold for incorporation
+into a dwelling. In determining whether a product is a consumer product,
+doubtful cases shall be resolved in favor of coverage. For a particular
+product received by a particular user, "normally used" refers to a
+typical or common use of that class of product, regardless of the status
+of the particular user or of the way in which the particular user
+actually uses, or expects or is expected to use, the product. A product
+is a consumer product regardless of whether the product has substantial
+commercial, industrial or non-consumer uses, unless such uses represent
+the only significant mode of use of the product.
+
+ "Installation Information" for a User Product means any methods,
+procedures, authorization keys, or other information required to install
+and execute modified versions of a covered work in that User Product from
+a modified version of its Corresponding Source. The information must
+suffice to ensure that the continued functioning of the modified object
+code is in no case prevented or interfered with solely because
+modification has been made.
+
+ If you convey an object code work under this section in, or with, or
+specifically for use in, a User Product, and the conveying occurs as
+part of a transaction in which the right of possession and use of the
+User Product is transferred to the recipient in perpetuity or for a
+fixed term (regardless of how the transaction is characterized), the
+Corresponding Source conveyed under this section must be accompanied
+by the Installation Information. But this requirement does not apply
+if neither you nor any third party retains the ability to install
+modified object code on the User Product (for example, the work has
+been installed in ROM).
+
+ The requirement to provide Installation Information does not include a
+requirement to continue to provide support service, warranty, or updates
+for a work that has been modified or installed by the recipient, or for
+the User Product in which it has been modified or installed. Access to a
+network may be denied when the modification itself materially and
+adversely affects the operation of the network or violates the rules and
+protocols for communication across the network.
+
+ Corresponding Source conveyed, and Installation Information provided,
+in accord with this section must be in a format that is publicly
+documented (and with an implementation available to the public in
+source code form), and must require no special password or key for
+unpacking, reading or copying.
+
+ 7. Additional Terms.
+
+ "Additional permissions" are terms that supplement the terms of this
+License by making exceptions from one or more of its conditions.
+Additional permissions that are applicable to the entire Program shall
+be treated as though they were included in this License, to the extent
+that they are valid under applicable law. If additional permissions
+apply only to part of the Program, that part may be used separately
+under those permissions, but the entire Program remains governed by
+this License without regard to the additional permissions.
+
+ When you convey a copy of a covered work, you may at your option
+remove any additional permissions from that copy, or from any part of
+it. (Additional permissions may be written to require their own
+removal in certain cases when you modify the work.) You may place
+additional permissions on material, added by you to a covered work,
+for which you have or can give appropriate copyright permission.
+
+ Notwithstanding any other provision of this License, for material you
+add to a covered work, you may (if authorized by the copyright holders of
+that material) supplement the terms of this License with terms:
+
+ a) Disclaiming warranty or limiting liability differently from the
+ terms of sections 15 and 16 of this License; or
+
+ b) Requiring preservation of specified reasonable legal notices or
+ author attributions in that material or in the Appropriate Legal
+ Notices displayed by works containing it; or
+
+ c) Prohibiting misrepresentation of the origin of that material, or
+ requiring that modified versions of such material be marked in
+ reasonable ways as different from the original version; or
+
+ d) Limiting the use for publicity purposes of names of licensors or
+ authors of the material; or
+
+ e) Declining to grant rights under trademark law for use of some
+ trade names, trademarks, or service marks; or
+
+ f) Requiring indemnification of licensors and authors of that
+ material by anyone who conveys the material (or modified versions of
+ it) with contractual assumptions of liability to the recipient, for
+ any liability that these contractual assumptions directly impose on
+ those licensors and authors.
+
+ All other non-permissive additional terms are considered "further
+restrictions" within the meaning of section 10. If the Program as you
+received it, or any part of it, contains a notice stating that it is
+governed by this License along with a term that is a further
+restriction, you may remove that term. If a license document contains
+a further restriction but permits relicensing or conveying under this
+License, you may add to a covered work material governed by the terms
+of that license document, provided that the further restriction does
+not survive such relicensing or conveying.
+
+ If you add terms to a covered work in accord with this section, you
+must place, in the relevant source files, a statement of the
+additional terms that apply to those files, or a notice indicating
+where to find the applicable terms.
+
+ Additional terms, permissive or non-permissive, may be stated in the
+form of a separately written license, or stated as exceptions;
+the above requirements apply either way.
+
+ 8. Termination.
+
+ You may not propagate or modify a covered work except as expressly
+provided under this License. Any attempt otherwise to propagate or
+modify it is void, and will automatically terminate your rights under
+this License (including any patent licenses granted under the third
+paragraph of section 11).
+
+ However, if you cease all violation of this License, then your
+license from a particular copyright holder is reinstated (a)
+provisionally, unless and until the copyright holder explicitly and
+finally terminates your license, and (b) permanently, if the copyright
+holder fails to notify you of the violation by some reasonable means
+prior to 60 days after the cessation.
+
+ Moreover, your license from a particular copyright holder is
+reinstated permanently if the copyright holder notifies you of the
+violation by some reasonable means, this is the first time you have
+received notice of violation of this License (for any work) from that
+copyright holder, and you cure the violation prior to 30 days after
+your receipt of the notice.
+
+ Termination of your rights under this section does not terminate the
+licenses of parties who have received copies or rights from you under
+this License. If your rights have been terminated and not permanently
+reinstated, you do not qualify to receive new licenses for the same
+material under section 10.
+
+ 9. Acceptance Not Required for Having Copies.
+
+ You are not required to accept this License in order to receive or
+run a copy of the Program. Ancillary propagation of a covered work
+occurring solely as a consequence of using peer-to-peer transmission
+to receive a copy likewise does not require acceptance. However,
+nothing other than this License grants you permission to propagate or
+modify any covered work. These actions infringe copyright if you do
+not accept this License. Therefore, by modifying or propagating a
+covered work, you indicate your acceptance of this License to do so.
+
+ 10. Automatic Licensing of Downstream Recipients.
+
+ Each time you convey a covered work, the recipient automatically
+receives a license from the original licensors, to run, modify and
+propagate that work, subject to this License. You are not responsible
+for enforcing compliance by third parties with this License.
+
+ An "entity transaction" is a transaction transferring control of an
+organization, or substantially all assets of one, or subdividing an
+organization, or merging organizations. If propagation of a covered
+work results from an entity transaction, each party to that
+transaction who receives a copy of the work also receives whatever
+licenses to the work the party's predecessor in interest had or could
+give under the previous paragraph, plus a right to possession of the
+Corresponding Source of the work from the predecessor in interest, if
+the predecessor has it or can get it with reasonable efforts.
+
+ You may not impose any further restrictions on the exercise of the
+rights granted or affirmed under this License. For example, you may
+not impose a license fee, royalty, or other charge for exercise of
+rights granted under this License, and you may not initiate litigation
+(including a cross-claim or counterclaim in a lawsuit) alleging that
+any patent claim is infringed by making, using, selling, offering for
+sale, or importing the Program or any portion of it.
+
+ 11. Patents.
+
+ A "contributor" is a copyright holder who authorizes use under this
+License of the Program or a work on which the Program is based. The
+work thus licensed is called the contributor's "contributor version".
+
+ A contributor's "essential patent claims" are all patent claims
+owned or controlled by the contributor, whether already acquired or
+hereafter acquired, that would be infringed by some manner, permitted
+by this License, of making, using, or selling its contributor version,
+but do not include claims that would be infringed only as a
+consequence of further modification of the contributor version. For
+purposes of this definition, "control" includes the right to grant
+patent sublicenses in a manner consistent with the requirements of
+this License.
+
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
+patent license under the contributor's essential patent claims, to
+make, use, sell, offer for sale, import and otherwise run, modify and
+propagate the contents of its contributor version.
+
+ In the following three paragraphs, a "patent license" is any express
+agreement or commitment, however denominated, not to enforce a patent
+(such as an express permission to practice a patent or covenant not to
+sue for patent infringement). To "grant" such a patent license to a
+party means to make such an agreement or commitment not to enforce a
+patent against the party.
+
+ If you convey a covered work, knowingly relying on a patent license,
+and the Corresponding Source of the work is not available for anyone
+to copy, free of charge and under the terms of this License, through a
+publicly available network server or other readily accessible means,
+then you must either (1) cause the Corresponding Source to be so
+available, or (2) arrange to deprive yourself of the benefit of the
+patent license for this particular work, or (3) arrange, in a manner
+consistent with the requirements of this License, to extend the patent
+license to downstream recipients. "Knowingly relying" means you have
+actual knowledge that, but for the patent license, your conveying the
+covered work in a country, or your recipient's use of the covered work
+in a country, would infringe one or more identifiable patents in that
+country that you have reason to believe are valid.
+
+ If, pursuant to or in connection with a single transaction or
+arrangement, you convey, or propagate by procuring conveyance of, a
+covered work, and grant a patent license to some of the parties
+receiving the covered work authorizing them to use, propagate, modify
+or convey a specific copy of the covered work, then the patent license
+you grant is automatically extended to all recipients of the covered
+work and works based on it.
+
+ A patent license is "discriminatory" if it does not include within
+the scope of its coverage, prohibits the exercise of, or is
+conditioned on the non-exercise of one or more of the rights that are
+specifically granted under this License. You may not convey a covered
+work if you are a party to an arrangement with a third party that is
+in the business of distributing software, under which you make payment
+to the third party based on the extent of your activity of conveying
+the work, and under which the third party grants, to any of the
+parties who would receive the covered work from you, a discriminatory
+patent license (a) in connection with copies of the covered work
+conveyed by you (or copies made from those copies), or (b) primarily
+for and in connection with specific products or compilations that
+contain the covered work, unless you entered into that arrangement,
+or that patent license was granted, prior to 28 March 2007.
+
+ Nothing in this License shall be construed as excluding or limiting
+any implied license or other defenses to infringement that may
+otherwise be available to you under applicable patent law.
+
+ 12. No Surrender of Others' Freedom.
+
+ If conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot convey a
+covered work so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you may
+not convey it at all. For example, if you agree to terms that obligate you
+to collect a royalty for further conveying from those to whom you convey
+the Program, the only way you could satisfy both those terms and this
+License would be to refrain entirely from conveying the Program.
+
+ 13. Use with the GNU Affero General Public License.
+
+ Notwithstanding any other provision of this License, you have
+permission to link or combine any covered work with a work licensed
+under version 3 of the GNU Affero General Public License into a single
+combined work, and to convey the resulting work. The terms of this
+License will continue to apply to the part which is the covered work,
+but the special requirements of the GNU Affero General Public License,
+section 13, concerning interaction through a network will apply to the
+combination as such.
+
+ 14. Revised Versions of this License.
+
+ The Free Software Foundation may publish revised and/or new versions of
+the GNU General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Program specifies that a certain numbered version of the GNU General
+Public License "or any later version" applies to it, you have the
+option of following the terms and conditions either of that numbered
+version or of any later version published by the Free Software
+Foundation. If the Program does not specify a version number of the
+GNU General Public License, you may choose any version ever published
+by the Free Software Foundation.
+
+ If the Program specifies that a proxy can decide which future
+versions of the GNU General Public License can be used, that proxy's
+public statement of acceptance of a version permanently authorizes you
+to choose that version for the Program.
+
+ Later license versions may give you additional or different
+permissions. However, no additional obligations are imposed on any
+author or copyright holder as a result of your choosing to follow a
+later version.
+
+ 15. Disclaimer of Warranty.
+
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
+APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
+HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
+OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
+IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
+ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. Limitation of Liability.
+
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
+THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
+GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
+USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
+DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
+PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
+EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGES.
+
+ 17. Interpretation of Sections 15 and 16.
+
+ If the disclaimer of warranty and limitation of liability provided
+above cannot be given local legal effect according to their terms,
+reviewing courts shall apply local law that most closely approximates
+an absolute waiver of all civil liability in connection with the
+Program, unless a warranty or assumption of liability accompanies a
+copy of the Program in return for a fee.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+state the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+Also add information on how to contact you by electronic and paper mail.
+
+ If the program does terminal interaction, make it output a short
+notice like this when it starts in an interactive mode:
+
+ <program> Copyright (C) <year> <name of author>
+ This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, your program's commands
+might be different; for a GUI interface, you would use an "about box".
+
+ You should also get your employer (if you work as a programmer) or school,
+if any, to sign a "copyright disclaimer" for the program, if necessary.
+For more information on this, and how to apply and follow the GNU GPL, see
+<http://www.gnu.org/licenses/>.
+
+ The GNU General Public License does not permit incorporating your program
+into proprietary programs. If your program is a subroutine library, you
+may consider it more useful to permit linking proprietary applications with
+the library. If this is what you want to do, use the GNU Lesser General
+Public License instead of this License. But first, please read
+<http://www.gnu.org/philosophy/why-not-lgpl.html>.
diff --git a/freevideo_engine/launcher/licenses/LGPL-3.0-only.txt b/freevideo_engine/launcher/licenses/LGPL-3.0-only.txt
new file mode 100644
index 0000000..65c5ca8
--- /dev/null
+++ b/freevideo_engine/launcher/licenses/LGPL-3.0-only.txt
@@ -0,0 +1,165 @@
+ GNU LESSER GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+
+ This version of the GNU Lesser General Public License incorporates
+the terms and conditions of version 3 of the GNU General Public
+License, supplemented by the additional permissions listed below.
+
+ 0. Additional Definitions.
+
+ As used herein, "this License" refers to version 3 of the GNU Lesser
+General Public License, and the "GNU GPL" refers to version 3 of the GNU
+General Public License.
+
+ "The Library" refers to a covered work governed by this License,
+other than an Application or a Combined Work as defined below.
+
+ An "Application" is any work that makes use of an interface provided
+by the Library, but which is not otherwise based on the Library.
+Defining a subclass of a class defined by the Library is deemed a mode
+of using an interface provided by the Library.
+
+ A "Combined Work" is a work produced by combining or linking an
+Application with the Library. The particular version of the Library
+with which the Combined Work was made is also called the "Linked
+Version".
+
+ The "Minimal Corresponding Source" for a Combined Work means the
+Corresponding Source for the Combined Work, excluding any source code
+for portions of the Combined Work that, considered in isolation, are
+based on the Application, and not on the Linked Version.
+
+ The "Corresponding Application Code" for a Combined Work means the
+object code and/or source code for the Application, including any data
+and utility programs needed for reproducing the Combined Work from the
+Application, but excluding the System Libraries of the Combined Work.
+
+ 1. Exception to Section 3 of the GNU GPL.
+
+ You may convey a covered work under sections 3 and 4 of this License
+without being bound by section 3 of the GNU GPL.
+
+ 2. Conveying Modified Versions.
+
+ If you modify a copy of the Library, and, in your modifications, a
+facility refers to a function or data to be supplied by an Application
+that uses the facility (other than as an argument passed when the
+facility is invoked), then you may convey a copy of the modified
+version:
+
+ a) under this License, provided that you make a good faith effort to
+ ensure that, in the event an Application does not supply the
+ function or data, the facility still operates, and performs
+ whatever part of its purpose remains meaningful, or
+
+ b) under the GNU GPL, with none of the additional permissions of
+ this License applicable to that copy.
+
+ 3. Object Code Incorporating Material from Library Header Files.
+
+ The object code form of an Application may incorporate material from
+a header file that is part of the Library. You may convey such object
+code under terms of your choice, provided that, if the incorporated
+material is not limited to numerical parameters, data structure
+layouts and accessors, or small macros, inline functions and templates
+(ten or fewer lines in length), you do both of the following:
+
+ a) Give prominent notice with each copy of the object code that the
+ Library is used in it and that the Library and its use are
+ covered by this License.
+
+ b) Accompany the object code with a copy of the GNU GPL and this license
+ document.
+
+ 4. Combined Works.
+
+ You may convey a Combined Work under terms of your choice that,
+taken together, effectively do not restrict modification of the
+portions of the Library contained in the Combined Work and reverse
+engineering for debugging such modifications, if you also do each of
+the following:
+
+ a) Give prominent notice with each copy of the Combined Work that
+ the Library is used in it and that the Library and its use are
+ covered by this License.
+
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
+ document.
+
+ c) For a Combined Work that displays copyright notices during
+ execution, include the copyright notice for the Library among
+ these notices, as well as a reference directing the user to the
+ copies of the GNU GPL and this license document.
+
+ d) Do one of the following:
+
+ 0) Convey the Minimal Corresponding Source under the terms of this
+ License, and the Corresponding Application Code in a form
+ suitable for, and under terms that permit, the user to
+ recombine or relink the Application with a modified version of
+ the Linked Version to produce a modified Combined Work, in the
+ manner specified by section 6 of the GNU GPL for conveying
+ Corresponding Source.
+
+ 1) Use a suitable shared library mechanism for linking with the
+ Library. A suitable mechanism is one that (a) uses at run time
+ a copy of the Library already present on the user's computer
+ system, and (b) will operate properly with a modified version
+ of the Library that is interface-compatible with the Linked
+ Version.
+
+ e) Provide Installation Information, but only if you would otherwise
+ be required to provide such information under section 6 of the
+ GNU GPL, and only to the extent that such information is
+ necessary to install and execute a modified version of the
+ Combined Work produced by recombining or relinking the
+ Application with a modified version of the Linked Version. (If
+ you use option 4d0, the Installation Information must accompany
+ the Minimal Corresponding Source and Corresponding Application
+ Code. If you use option 4d1, you must provide the Installation
+ Information in the manner specified by section 6 of the GNU GPL
+ for conveying Corresponding Source.)
+
+ 5. Combined Libraries.
+
+ You may place library facilities that are a work based on the
+Library side by side in a single library together with other library
+facilities that are not Applications and are not covered by this
+License, and convey such a combined library under terms of your
+choice, if you do both of the following:
+
+ a) Accompany the combined library with a copy of the same work based
+ on the Library, uncombined with any other library facilities,
+ conveyed under the terms of this License.
+
+ b) Give prominent notice with the combined library that part of it
+ is a work based on the Library, and explaining where to find the
+ accompanying uncombined form of the same work.
+
+ 6. Revised Versions of the GNU Lesser General Public License.
+
+ The Free Software Foundation may publish revised and/or new versions
+of the GNU Lesser General Public License from time to time. Such new
+versions will be similar in spirit to the present version, but may
+differ in detail to address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Library as you received it specifies that a certain numbered version
+of the GNU Lesser General Public License "or any later version"
+applies to it, you have the option of following the terms and
+conditions either of that published version or of any later version
+published by the Free Software Foundation. If the Library as you
+received it does not specify a version number of the GNU Lesser
+General Public License, you may choose any version of the GNU Lesser
+General Public License ever published by the Free Software Foundation.
+
+ If the Library as you received it specifies that a proxy can decide
+whether future versions of the GNU Lesser General Public License shall
+apply, that proxy's public statement of acceptance of any version is
+permanent authorization for you to choose that version for the
+Library.
diff --git a/freevideo_engine/launcher/licenses/NOTICE.txt b/freevideo_engine/launcher/licenses/NOTICE.txt
new file mode 100644
index 0000000..a7034de
--- /dev/null
+++ b/freevideo_engine/launcher/licenses/NOTICE.txt
@@ -0,0 +1,21 @@
+FreeVideo desktop components
+
+The desktop application uses unmodified Qt, PySide6 Essentials and Shiboken6
+6.11.2 from the Qt Project. The applicable open source license texts are
+included in this directory. The Qt libraries are dynamically linked.
+
+Qt and third-party component notices:
+https://doc.qt.io/qt-6/licenses-used-in-qt.html
+
+Corresponding component source:
+https://code.qt.io/cgit/qt/qtbase.git/tree/?h=v6.11.2
+https://code.qt.io/cgit/qt/qtdeclarative.git/tree/?h=v6.11.2
+https://code.qt.io/cgit/qt/qtshadertools.git/tree/?h=v6.11.2
+https://code.qt.io/cgit/pyside/pyside-setup.git/tree/?h=v6.11.2
+
+FreeVideo source and build instructions:
+https://github.com/FlashML-org/FreeVideo
+Install constraints/windows-launcher.txt and run scripts/build_windows.py
+with --onefolder to build a distribution with replaceable Qt libraries.
+FreeVideo does not restrict modification of these libraries or reverse
+engineering for debugging modifications to them.
diff --git a/freevideo_engine/launcher_copy.py b/freevideo_engine/launcher_copy.py
new file mode 100644
index 0000000..e427e8f
--- /dev/null
+++ b/freevideo_engine/launcher_copy.py
@@ -0,0 +1,146 @@
+"""Translate launcher presentation without changing worker events or logs."""
+import re
+
+from .terminal_ui import clean
+
+
+SOURCES = {
+ 'auto': ('Automatic', '自动选择'), 'official': ('Hugging Face', 'Hugging Face'),
+ 'hf-mirror': ('HF Mirror', 'HF 镜像'), 'modelscope': ('ModelScope', '魔搭'),
+ 'user': ('Custom source', '自定义源'),
+}
+
+ZH = dict([
+ ('Prepare Python', '准备 Python'), ('Install Python', '安装 Python'),
+ ('Use existing Python', '复用 Python'), ('Create Python environment', '创建运行环境'),
+ ('Install PyTorch + CUDA', '安装 GPU 运行环境'),
+ ('Install engine dependencies', '安装生成组件'), ('Install encoder support', '安装编码组件'),
+ ('Install model support', '安装模型组件'), ('Download model code', '下载模型组件'),
+ ('Check text encoder', '检查文本编码器'), ('Download model weights', '下载所需模型'),
+ ('Reuse models / download missing files', '复用模型并补齐文件'),
+ ('Verify / reuse local models', '检查并复用已有模型'),
+ ('Verify existing models', '校验已有模型'), ('Check existing model folders', '查找已有模型'),
+ ('Find existing models', '查找已有模型'), ('Verify local model', '校验已有模型'),
+ ('Copy local model', '复制模型'), ('Reuse local model', '复用模型'),
+ ('Existing models checked', '已有模型检查完成'), ('Check local models', '检查已有模型'),
+ ('Local models verified', '已有模型校验完成'),
+ ('Optimize model storage', '准备模型'), ('Finish model storage', '完成模型准备'),
+ ('Test GPU acceleration', '检查 GPU 加速'), ('Check GPU runtime', '检查 GPU 运行环境'),
+ ('Check installed packages', '检查运行环境'), ('Save installed versions', '保存安装信息'),
+ ('Check build tools', '检查编译工具'), ('Build SageAttention 2', '准备 GPU 加速'),
+ ('Install SageAttention 2', '安装 GPU 加速组件'), ('Prepare dependencies', '准备运行组件'),
+ ('Install components in parallel', '安装运行组件'),
+ ('Install acceleration and download models', '安装加速组件和下载模型'),
+ ('Verify installation on your GPU', '检查 GPU 加速'), ('Finish setup', '完成安装'),
+ ('Setup complete', '安装完成'), ('Setup interrupted', '安装已暂停'), ('Setup failed', '安装未完成'),
+ ('Download ComfyUI', '下载 ComfyUI'), ('Create ComfyUI Python environment', '创建 ComfyUI 环境'),
+ ('Prepare ComfyUI GPU packages', '准备 ComfyUI 加速组件'),
+ ('Install ComfyUI packages', '安装 ComfyUI 组件'), ('Check ComfyUI dependencies', '检查 ComfyUI 环境'),
+ ('Finish ComfyUI setup', '完成 ComfyUI 安装'), ('ComfyUI ready', 'ComfyUI 已就绪'),
+ ('Prepare ComfyUI', '准备 ComfyUI'), ('Install FreeVideo workflow', '安装 FreeVideo 工作流'),
+ ('Open ComfyUI', '打开 ComfyUI'), ('Starting ComfyUI', '启动 ComfyUI'),
+ ('Loading nodes and the web interface', '加载节点与界面'),
+ ('Checking configuration', '检查配置'), ('Check installation', '检查安装'),
+ ('Verify bundled files', '校验离线包'), ('Check local files', '检查本地文件'),
+ ('All bundled files verified', '离线包校验完成'),
+ ('Detecting your GPU and testing acceleration', '检测显卡与加速组件'),
+ ('Start ComfyUI', '启动 ComfyUI'), ('Open FreeVideo workflow', '打开 FreeVideo 工作流'),
+ ('Ready', '已就绪'), ('Verified and ready', '已就绪'),
+ ('Stopped; files retained', '已暂停,下载进度已保留'),
+ ('Local verification stopped; files retained', '校验已暂停,文件已保留'),
+ ('Download and prepare the private Python runtime', '准备 Python 运行环境'),
+ ('Check the Python already prepared by the launcher', '检查已有 Python'),
+ ('Prepare an isolated environment for FreeVideo', '创建 FreeVideo 运行环境'),
+ ('Download and install the GPU runtime packages', '下载并安装 GPU 运行组件'),
+ ('Download and install video and text encoding libraries', '下载并安装生成组件'),
+ ('Connect the encoder to FreeVideo', '准备编码器'), ('Install the verified model code', '安装模型组件'),
+ ('Prepare model source files while runtime packages download', '下载模型组件'),
+ ('Verify that the text encoding library loads', '检查文本编码器'),
+ ('Download and verify the video model and text encoder', '下载并校验模型'),
+ ('Prepare compact FP8 weights for your GPU', '准备适配显卡的模型'),
+ ('Run small checks on your GPU', '检查 GPU 加速是否可用'),
+ ('Verify prepared weights and apply your storage choice', '校验模型文件'),
+ ('Verify dependency compatibility', '检查运行组件'),
+ ('Record versions for future diagnostics', '保存安装信息'),
+ ('Verify the compiler before building GPU acceleration', '检查编译工具'),
+ ('Compile acceleration for your GPU', '编译 GPU 加速组件'),
+ ('the first build takes longer', '首次准备需要一些时间'),
+ ('Install the prepared GPU acceleration package', '安装 GPU 加速组件'),
+ ('Install the verified Windows acceleration package', '安装 Windows 加速组件'),
+ ('Verify CUDA and the Windows runtime libraries', '检查 CUDA 与 Windows 运行组件'),
+ ('Prepare verified source files', '准备组件文件'), ('Download verified source code', '下载组件文件'),
+ ('Prepare source files for installation', '准备安装文件'),
+ ('Download and verify required files', '下载并校验所需文件'),
+ ('Dependencies resolved', '依赖检查完成'), ('preparing downloads', '准备下载'),
+ ('Downloads ready', '下载完成'), ('Downloading packages', '下载组件'), ('installing packages', '安装组件'),
+ ('Packages installed', '组件已安装'), ('finishing checks', '完成检查'),
+ ('Installing packages', '安装组件'), ('Compiling CUDA kernels', '编译 GPU 加速组件'),
+ ('Load cached blocks and upload resident weights', '加载模型'), ('Finish model placement', '完成模型加载'),
+ ('Verify cached weights', '校验模型缓存'), ('Read and convert source weights', '转换模型格式'),
+ ('Storage ready', '模型文件已就绪'), ('Download', '下载'), ('Model', '模型'),
+ ('Video model', '视频模型'), ('Video decoder', '视频解码器'), ('text encoder', '文本编码器'),
+ ('ModelScope', '魔搭'), ('ModelScope parallel', '魔搭'), ('HF Mirror', 'HF 镜像'),
+ ('HF Xet', 'Hugging Face'), ('HF HTTP', 'Hugging Face'), ('Parallel HTTP', '并行下载'),
+ ('Custom source', '自定义源'), ('Model source', '模型下载源'),
+ ('official source', '官方源'), ('Nanjing University mirror', '南京大学镜像'),
+ ('Tsinghua mirror', '清华镜像'), ('GitHub mirror', 'GitHub 镜像'),
+ ('your Python mirror', '自定义 Python 镜像'), ('your configured source', '自定义源'),
+ ('Retrying same source', '正在重试'),
+ ('Retrying same source with direct connection', '通过直连重试'),
+ ('Retrying same source with configured proxy', '通过代理重试'),
+ ('Resume unavailable; checking another source', '正在尝试其他下载源'),
+ ('Paused; existing data kept', '已暂停,下载进度已保留'),
+ ('Restart explicitly allowed', '重新下载此文件'),
+ ('Restarting this file; incompatible fragments retained on disk', '重新下载此文件,旧片段已保留'),
+ ('Cannot resume this format; enable restart in Setup', '此文件无法续传,请允许重新下载'),
+ ('Download response too large', '下载数据异常'), ('switching source', '正在切换下载源'),
+ ('Download memory budget reached', '正在调整下载方式'), ('switching to streaming', '继续下载'),
+ ('Download source failed', '下载源暂不可用'), ('trying another source', '正在尝试其他下载源'),
+ ('Download counter mismatch', '下载数据异常'),
+ ('setup', '安装'), ('initialize', '初始化'), ('launch', '启动'),
+ ('Hugging Face · Video', 'Hugging Face · 视频'), ('Hugging Face · Audio', 'Hugging Face · 音频'),
+])
+
+
+def source_name(value, zh=False):
+ return SOURCES.get(value, (value, value))[bool(zh)]
+
+
+def display(value, zh=False):
+ value = clean(value)
+ if not zh:
+ return {'检查配套包': 'Checking packages'}.get(value, value)
+ if value in ZH:
+ return ZH[value]
+ if value in SOURCES:
+ return source_name(value, True)
+ if ' · ' in value:
+ return ' · '.join(display(part, True) for part in value.split(' · '))
+ for prefix, translated in (
+ ('Switched current download to ', '已切换至 '), ('Connecting to ', '正在连接 '),
+ ('Assemble downloaded parts', '合并已下载文件'), ('Reuse verified FP8 ', '复用模型缓存 '),
+ ('Prepare FP8 ', '准备模型缓存 '), ('Verified ', '已校验 '), ('Verify ', '正在校验 '),
+ ('Download ', '下载 '), ('Prepare ', '准备 '), ('Source: ', '下载源:'), ('Log: ', '日志:'),
+ ):
+ if value.startswith(prefix):
+ return translated + display(value[len(prefix):], True)
+ for pattern, translated in (
+ (r'Downloading packages · about (.+) across (\d+) files', r'下载组件 · 约 \1,共 \2 个文件'),
+ (r'about (.+) across (\d+) files', r'约 \1,共 \2 个文件'),
+ (r'(\d+) files / folders scanned', r'已扫描 \1 个文件和目录'),
+ (r'(\d+ / \d+) files', r'\1 个文件'), (r'shard (\d+ / \d+)', r'分片 \1'),
+ (r'([\d.]+ MiB) retained', r'已保留 \1'), (r'([\d.]+ GiB) released', r'已释放 \1'),
+ (r'([\d.]+ MiB) downloaded', r'已下载 \1'),
+ (r'Resuming ([\d.]+ MiB) retained', r'继续下载,已保留 \1'),
+ (r'retry (\d+) in (\d+)s', r'\2 秒后第 \1 次重试'),
+ (r'Receiving objects:.*', '正在下载组件'), (r'Resolving deltas:.*', '正在整理组件'),
+ (r'Updating files:.*', '正在准备文件'),
+ ):
+ if re.fullmatch(pattern, value):
+ return re.sub(pattern, translated, value)
+ return value
+
+
+def progress_view(event, zh=False):
+ return {key: display(value, zh) if key in ('label', 'detail') else value
+ for key, value in event.items()}
diff --git a/freevideo_engine/launcher_session.py b/freevideo_engine/launcher_session.py
new file mode 100644
index 0000000..61497db
--- /dev/null
+++ b/freevideo_engine/launcher_session.py
@@ -0,0 +1,472 @@
+"""Widget-independent state for the Qt launcher; uses the existing installer."""
+import locale
+import os
+from pathlib import Path
+import time
+from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
+
+from . import __version__
+from .comfy_launcher_runtime import Controller, layout, local_url, new_layout
+from .desktop_runtime import launcher_root, materialize_source
+from .download_settings import Probe, read as download_preferences, speed_text
+from .launcher_settings import Store
+from .launcher_terminal import Tail
+from .model_status import FAMILIES, NAMES
+from .setup_progress import progress_text
+from .terminal_ui import clean, duration
+
+
+class Session:
+ def __init__(self, source=None, *, controller=None, store=None, updater=None, smoke=False):
+ from .offline_packages import Importer
+ self.importer = Importer()
+ self.imported_batch = None
+ self.source = Path(source or materialize_source())
+ self.controller = controller or Controller(self.source)
+ self.store = store or Store(launcher_root())
+ saved = self.store.read()
+ home = Path(os.environ.get('USERPROFILE') or os.environ.get('HOME') or launcher_root().parent)
+ self.form = dict(comfy='', destination=str(home / 'FreeVideo'), engine='', python='',
+ url='http://127.0.0.1:8188', models='', model_dirs=[], model_method='auto',
+ separate=False, repair=False, new_comfy=True, offline_runtime='', offline_models=[])
+ self.form.update({k: v for k, v in saved.items() if k in self.form})
+ if self.form['model_method'] == 'reuse':
+ self.form['model_method'] = 'manual' if self.form['offline_runtime'] else 'auto'
+ self.form['engine'] = os.environ.get('FREEVIDEO_HOME') or self.form['engine']
+ if saved.get('comfy') and 'new_comfy' not in saved:
+ self.form['new_comfy'] = False
+ self.language = saved.get('language', (locale.getlocale()[0] or '').lower())
+ self.selected = saved.get('installation')
+ self.saved_setup = saved.get('setup', {})
+ self.page = 'comfy'
+ self.token = ''
+ self.error = ''
+ self.notice = ''
+ self.compatibility = dict(available=False, level=0, automatic=False)
+ self.probe = Probe()
+ self.tail = Tail()
+ self.log_source = None
+ self.model_groups = []
+ self.browser_attempted = False
+ self.browser_error = ''
+ self.started = None
+ self.closing = False
+ self.smoke = smoke
+ self.reported_setup = None
+ self.update_snoozed = False
+ if self.controller.restore(dict(saved, engine=self.form['engine'])):
+ self.remember(self.controller.selection, persist=False)
+ self.page = 'launcher'
+ elif self.saved_setup.get('status') in ('running', 'failed', 'cancelled'):
+ self.page = 'progress'
+ self.updater = updater
+ if updater is None and not smoke:
+ from .launcher_update import current_build, UpdateClient
+ identity = current_build()
+ if identity:
+ self.updater = UpdateClient(identity, launcher_root())
+ if self.updater and not self.updater.busy:
+ self.updater.run('check')
+ if self.form['offline_runtime'] and not controller:
+ root = Path(self.form['offline_runtime'])
+ if (root / 'portable.json').is_file():
+ self.activate_offline(root)
+ self.inspect_compatibility()
+
+ def t(self, en, zh):
+ return zh if self.language.startswith('zh') else en
+
+ def engine_root(self):
+ if self.form['engine'].strip():
+ return Path(self.form['engine']).expanduser().resolve()
+ if self.form['new_comfy']:
+ return Path(self.form['destination']).expanduser().resolve() / 'FreeVideo-engine'
+ if self.form['comfy'].strip():
+ return Path(layout(self.form['comfy'])['root']) / 'FreeVideo-engine'
+ raise ValueError(self.t('Choose your installation folder first.', '请先选择安装位置。'))
+
+ def persist(self):
+ self.store.write(self.form, self.language, self.selected, self.saved_setup)
+
+ def remember(self, selection, persist=True):
+ self.selected = dict(selection)
+ self.form.update(comfy=selection['root'], engine=selection['engine'],
+ python=selection.get('python') or '', url=selection['url'],
+ separate=selection.get('separate', False), new_comfy=False, repair=False)
+ if persist:
+ self.persist()
+
+ def edit(self, key, value):
+ if key == 'language':
+ self.language = str(value); self.persist(); return
+ if key == 'token':
+ if self.controller.busy:
+ raise ValueError(self.t('Pause installation before changing the token.', '请先暂停安装,再修改 Token。'))
+ from .hf_auth import validate
+ self.token = validate(str(value)); return
+ if key not in self.form or self.controller.busy or self.importer.busy:
+ return
+ if key in ('separate', 'repair', 'new_comfy'):
+ value = bool(value)
+ if key == 'model_method' and value not in ('auto', 'manual', 'reuse'):
+ raise ValueError('Unknown model download method')
+ if self.form[key] == value:
+ return
+ self.form[key] = value
+ self.controller.selection = None
+ self.controller.state = dict(status='idle')
+ self.browser_attempted = False
+ self.browser_error = self.error = ''
+ self.persist()
+
+ def add_folder(self, folder):
+ from .local_models import library_roots
+ self.edit('model_dirs', library_roots(self.form['model_dirs'] + [folder]))
+
+ def remove_folder(self, index):
+ removed = self.form['model_dirs'][index]
+ self.edit('model_dirs', [p for i, p in enumerate(self.form['model_dirs']) if i != index])
+ self.edit('offline_models', [p for p in self.form['offline_models']
+ if Path(p) / 'models' != Path(removed)])
+
+ def clear_runtime(self):
+ # Detach it from this plan; imported files remain available for reuse.
+ self.edit('offline_runtime', '')
+
+
+ def import_packages(self, paths):
+ if self.controller.busy or self.importer.busy:
+ return
+ if not paths:
+ return
+ self.edit('model_method', 'manual')
+ destination = (Path(self.form['destination']).expanduser().resolve() if self.form['new_comfy']
+ else self.engine_root().parent)
+ self.importer.start(paths, destination)
+ self.imported_batch = None
+ self.error = ''
+
+ def activate_offline(self, root):
+ from .portable_launcher import PortableController
+ controller = PortableController(root)
+ controller.restore({'url': self.form['url']})
+ self.controller.close()
+ self.controller = controller
+ self.form['offline_runtime'] = str(root)
+ self.remember(controller.selection)
+ self.page = 'launcher'
+ self.persist()
+
+ def action(self, name, accepted=False):
+ if self.importer.busy:
+ if name == 'stop':
+ self.importer.cancelled.set()
+ return
+ if self.controller.busy:
+ if name == 'stop':
+ self.controller.cancel()
+ return
+ self.error = ''
+ if name == 'setup':
+ if getattr(self.controller, 'fixed_environment', False) is True:
+ self.controller.close()
+ self.controller = Controller(self.source)
+ self.page = 'comfy'; return
+ if name == 'launcher' and self.selected:
+ if not self.controller.restore({'installation': self.selected}):
+ raise ValueError(self.t('Locate or repair this installation in Setup.', '请在安装设置中重新定位或修复这份安装。'))
+ self.page = 'launcher'
+ return
+ if name == 'update-engine' and self.selected:
+ self.persist()
+ self.controller.run('inspect', dict(self.form, token=self.token))
+ self.page = 'progress'
+ self.model_groups = []
+ self.started = time.monotonic()
+ return
+ if name == 'back':
+ self.page = 'comfy' if self.page == 'models' else 'models'
+ return
+ if name == 'browser':
+ self.open_browser(); return
+ if name == 'shortcut':
+ self.controller.run('shortcut', self.controller.state.get('status')); return
+ if name not in ('primary', 'launch'):
+ return
+ state = self.controller.state.get('status')
+ if (self.page == 'models' and self.form['new_comfy'] and self.form['offline_runtime']
+ and self.form['model_method'] == 'manual'):
+ self.importer.prepare(self.form['offline_runtime'], self.form['offline_models'], self.source)
+ return
+ if self.page == 'comfy':
+ new_layout(self.form['destination']) if self.form['new_comfy'] else layout(self.form['comfy'])
+ self.persist(); self.page = 'models'; return
+ self.browser_attempted = False
+ self.browser_error = ''
+ if self.page == 'launcher' and getattr(self.controller, 'fixed_environment', False) is True:
+ consent = self.controller.root / 'engine/portable-consent.json'
+ if not consent.exists():
+ if not accepted:
+ raise ValueError(self.t('Accept the installation plan and licenses.', '请先同意安装计划及许可证。'))
+ from .monitoring import save
+ save(consent, dict(accepted_at=time.time()))
+ self.controller.run('launch', {'url': self.form['url']})
+ elif self.page == 'launcher':
+ self.controller.run('launch', {'installation': self.selected})
+ elif self.page == 'progress' and state == 'review':
+ if not accepted or self.controller.state.get('errors'):
+ raise ValueError(self.t('Accept the reviewed installation plan to continue.', '请先同意本次安装计划及许可证。'))
+ self.controller.run('install', True)
+ elif state in ('open', 'restart-required'):
+ self.controller.run('connect')
+ else:
+ self.persist()
+ self.controller.run('inspect', dict(self.form, token=self.token))
+ self.page = 'progress'
+ self.model_groups = []
+ self.started = time.monotonic()
+
+ def _browser_url(self, address):
+ """Return the URL used by the browser, with the launcher locale hint."""
+ if not address or not self.language.startswith('zh'):
+ return address
+ parsed = urlsplit(address)
+ query = dict(parse_qsl(parsed.query, keep_blank_values=True))
+ query['freevideo_lang'] = 'zh'
+ return urlunsplit((parsed.scheme, parsed.netloc, parsed.path,
+ urlencode(query), parsed.fragment))
+
+ def open_browser(self):
+ if self.controller.state.get('status') != 'open':
+ return
+ from .windows_ux import open_browser
+ self.browser_attempted = True
+ address = self._browser_url(self.controller.state['url'])
+ # ComfyUI itself may follow the browser's language, but the FreeVideo
+ # panels need an explicit choice when the launcher language differs
+ # from the operating system. Keep the controller's canonical URL
+ # unchanged so saved installations and existing integrations remain
+ # compatible; only the browser hint is added at launch time.
+ try:
+ if not open_browser(address):
+ raise OSError('The system did not accept the browser request')
+ self.browser_error = ''
+ except Exception as error:
+ self.browser_error = self.t('ComfyUI is ready. Open the browser again or copy its address.\n',
+ 'ComfyUI 已就绪,请重试打开浏览器,或复制地址手动打开。\n') + str(error)
+
+ def select_source(self, name):
+ self.probe.select(self.engine_root(), name)
+
+ def select_proxy_mode(self, mode):
+ self.probe.select(self.engine_root(), proxy_mode=mode)
+
+ def speed_test(self):
+ self.probe.start(self.engine_root(), self.token)
+
+ def inspect_compatibility(self):
+ from .compatibility import check_installation
+ try:
+ root = self.engine_root()
+ if (root / 'machine.json').is_file():
+ self.compatibility = check_installation(root)
+ if self.compatibility.get('notice'):
+ self.notice = self.t('Compatibility settings were enabled after an interrupted generation. You can change them in Settings.',
+ '上次生成中断后已开启兼容性设置,可在设置中调整或关闭。')
+ except (OSError, ValueError, RuntimeError):
+ self.compatibility = dict(available=False, level=0, automatic=False)
+
+ def set_compatibility(self, level, automatic):
+ from .compatibility import Store, installed_identity
+ root = self.engine_root()
+ Store(root).set(installed_identity(root), level, automatic)
+ self.inspect_compatibility()
+
+ def dismiss_notice(self):
+ from .compatibility import Store, installed_identity
+ if self.compatibility.get('notice'):
+ root = self.engine_root()
+ Store(root).acknowledge(installed_identity(root), self.compatibility['notice']['id'])
+ self.notice = ''
+
+ def dismiss_update(self):
+ self.update_snoozed = True
+ if self.updater and self.updater.state['status'] == 'downloading':
+ self.updater.cancelled.set()
+
+ def check_update(self, token=''):
+ if self.updater and not self.updater.busy:
+ if token.strip():
+ self.updater.token = token.strip()
+ self.updater.run('check')
+
+ def update(self, token=''):
+ if not self.updater or self.updater.busy or self.controller.busy or self.importer.busy:
+ return
+ from .launcher_update import launch_download, RELEASE_PAGE
+ from .windows_ux import open_browser
+ row = self.updater.state
+ if row['status'] == 'ready':
+ self.persist()
+ launch_download(row['candidate'], self.updater.root, token=self.updater.token)
+ self.closing = True
+ elif row.get('candidate'):
+ if self.updater.current.get('packaging') == 'onedir':
+ open_browser(RELEASE_PAGE)
+ else:
+ self.updater.run('download', row['candidate'])
+ else:
+ self.updater.token = token.strip()
+ self.updater.run('check')
+
+ def close(self):
+ self.importer.cancelled.set()
+ if getattr(self, '_closed', False):
+ return
+ try:
+ self.persist()
+ finally:
+ self.closing = True
+ if self.updater:
+ self.updater.cancelled.set()
+ self.controller.close()
+ self._closed = True
+
+ def tick(self):
+ imported = self.importer.state
+ if not self.importer.busy and imported is not self.imported_batch:
+ self.imported_batch = imported
+ for package in imported.get('packages', []):
+ if package['kind'] == 'runtime':
+ self.form['offline_runtime'] = package['root']
+ else:
+ roots = self.form['offline_models']
+ if package['root'] not in roots:
+ self.form['offline_models'] = roots + [package['root']]
+ self.add_folder(str(Path(package['root']) / 'models'))
+ if imported.get('packages'):
+ self.persist()
+ if imported['status'] == 'error':
+ self.error = imported['error']
+ elif imported['status'] == 'prepared':
+ self.activate_offline(imported['ready_root'])
+
+ row = self.controller.state
+ busy = self.controller.busy
+ groups = row.get('task', {}).get('model_groups') or row.get('model_groups') or row.get('plan', {}).get('model_groups')
+ if groups:
+ self.model_groups = groups
+ selection = row.get('selection')
+ if not busy and selection and selection.get('ready') and (row.get('deployed') or row.get('status') in ('open', 'restart-required')):
+ if selection != self.selected:
+ self.remember(selection)
+ if row.get('action') == 'install' or row.get('status') in ('open', 'restart-required'):
+ self.page = 'launcher'
+ if row.get('action') == 'install':
+ saved = dict(status=row.get('status', ''), action='install', phase=row.get('overall', {}).get('label', ''))
+ if self.saved_setup != saved:
+ self.saved_setup = saved; self.persist()
+ if not busy and self.started and row.get('status') in ('open', 'failed', 'cancelled', 'restart-required'):
+ key = 'setup-%s-%s' % (self.started, row['status'])
+ if self.reported_setup != key and not self.smoke:
+ self.reported_setup = key
+ from .installation_diagnostics import write
+ write(self.engine_root(), time.time()-(time.monotonic()-self.started),
+ {'summary': {'status': 'complete' if row['status'] in ('open', 'restart-required') else 'incomplete',
+ 'hardware': row.get('plan', {}).get('inventory', {}).get('hardware', {}),
+ 'stages': [{'stage': 'installation', 'seconds': time.monotonic()-self.started}]},
+ 'request': {'exception': row.get('exception', []), 'phase': self.controller.section or 'installation'},
+ 'log_tails': {'installation': row.get('error', '')}})
+ if not busy and not self.closing and row.get('status') == 'open' and not self.browser_attempted:
+ self.open_browser()
+ sources = self.controller.terminal_sources()
+ if sources:
+ selected = next((p for _, p in sources if p == self.log_source), sources[-1][1])
+ self.tail.select(selected)
+ self.tail.read(final=not self.controller.terminal_running(selected))
+
+ def snapshot(self):
+ from .diagnostics import Redactor
+ from .failure_details import launcher_failure
+ from .launcher_copy import display, progress_view, source_name
+ zh = self.language.startswith('zh')
+ row = self.controller.state
+ task = row.get('task', {})
+ progress = progress_view(task.get('progress') or {}, zh)
+ overall = progress_view(row.get('overall') or task.get('phase_progress') or {}, zh)
+ errors = [self.error, self.browser_error, row.get('error', ''), *row.get('errors', [])]
+ shortcut = row.get('shortcut') or {}
+ if shortcut.get('status') == 'failed':
+ errors.append(shortcut.get('error', ''))
+ error = Redactor([(self.token, '<REDACTED>')] if self.token else []).text('\n'.join(str(e) for e in errors if e))
+ by_id = {r['id']: r for r in self.model_groups}
+ ready = bool(row.get('selection', {}).get('ready') or row.get('status') == 'open')
+ models = []
+ states = {'ready': ('Ready locally', '本地已就绪'), 'waiting': ('Waiting for scan', '等待检查'),
+ 'pending': ('Download needed', '需要下载'), 'downloading': ('Downloading', '正在下载'),
+ 'verifying': ('Transfer complete · verifying (no re-download)', '传输完成 · 正在校验(不会重复下载)'),
+ 'paused': ('Paused', '已暂停')}
+ for name in FAMILIES:
+ item = dict(by_id.get(name, {}))
+ state = 'ready' if ready else item.get('state', 'waiting')
+ if state == 'waiting' and item.get('download_bytes'):
+ state = 'pending'
+ total = item.get('total_bytes', 0)
+ done = total if state == 'ready' else min(total,
+ item.get('verified_bytes', 0)+item.get('downloaded_bytes', 0))
+ models.append(dict(id=name, title=self.t(*NAMES[name]), state=state,
+ detail=self.t(*states.get(state, states['waiting'])), done=done, total=total,
+ found=item.get('existing_bytes', 0), download=item.get('download_bytes', 0),
+ rate='%.1f MiB/s' % (item['bytes_per_second']/2**20) if item.get('bytes_per_second') else ''))
+ try:
+ preferences = download_preferences(self.engine_root() / 'download-settings.json')
+ except (OSError, ValueError):
+ preferences = dict(source='auto', proxy_mode='auto')
+ source = preferences['source']
+ plan = row.get('plan', {})
+ estimate = []
+ gpu = plan.get('inventory', {}).get('hardware', {}).get('gpu_name')
+ if gpu:
+ estimate.append(gpu)
+ if 'model_download_bytes' in plan:
+ estimate.append(self.t('Download ', '需下载 ')+'%.1f GiB' % (plan['model_download_bytes']/2**30))
+ if row.get('disks'):
+ estimate.append(self.t('Peak disk ~', '磁盘峰值约 ')+'%.1f GiB' % (sum(d.get('needed_bytes', 0) for d in row['disks'])/2**30))
+ update = dict(self.updater.state) if self.updater else dict(status='development')
+ probe = self.probe.snapshot()
+ speeds = []
+ for group, entries in probe.get('sources', {}).items():
+ for entry in entries:
+ names = {'edge-models': ('Video model', '视频模型'), 'vdn-models': ('Decoder', '解码器'),
+ 'models': ('Text encoder', '文本编码器'), 'pypi': ('Python packages', 'Python 依赖'),
+ 'github': ('Tools', '安装工具'), 'git': ('Git', 'Git'), 'cuda': ('CUDA', 'CUDA')}
+ if group.startswith('torch-'):
+ names[group] = ('GPU packages', 'GPU 依赖')
+ route = self.t('direct', '直连') if entry.get('route') == 'direct' else self.t('current connection', '当前连接')
+ speeds.append(dict(source=source_name(entry['id'], zh)+' · '+route, group=self.t(*names.get(group, (group, group))),
+ ok=entry.get('ok', False), rate=speed_text(entry, self.language.startswith('zh'))))
+ # Only display fields from the update manifest, never access tokens.
+ update = {k: update[k] for k in ('status', 'error', 'progress', 'candidate') if k in update}
+ update['remind'] = bool(update.get('candidate') and not self.update_snoozed
+ and update['status'] in ('available', 'ready', 'error'))
+ return dict(version=__version__, zh=self.language.startswith('zh'), form=dict(self.form),
+ page=self.page, status=row.get('status', 'idle'), busy=self.controller.busy or self.importer.busy,
+ offline=dict(progress_view(self.importer.state, zh), runtime=bool(self.form['offline_runtime']), models=len(self.form['offline_models'])),
+ selected=bool(self.selected), error=error, notice=self.notice, compatibility=self.compatibility,
+ models=models, overall=overall, progress=progress, detail=clean(progress.get('detail', '')),
+ progress_text=progress_text(progress, self.language.startswith('zh')),
+ elapsed=duration(time.monotonic()-self.started) if self.started else '', summary=' · '.join(estimate),
+ failure=launcher_failure(error, zh=self.language.startswith('zh')),
+ source=source, source_name=source_name(source, zh), proxy_mode=preferences['proxy_mode'],
+ probe=probe, speeds=speeds, token_set=bool(self.token),
+ log=self.tail.text, logs=[dict(label=display(n, zh), path=str(p)) for n, p in self.controller.terminal_sources()],
+ url=(self._browser_url(row.get('url', '')) if row.get('status') == 'open' else ''), shortcut=shortcut,
+ can_shortcut=bool(self.controller.selection and self.controller.selection.get('ready')),
+ update=update, engine_update_available=bool(row.get('engine_update_available')),
+ settings_path=str(self.store.primary),
+ review_id=row.get('plan', {}).get('plan_id', ''),
+ consent=self.t('I accept the installation plan and model / toolkit licenses.', '我同意安装计划及模型/工具包许可证。'),
+ portable=False, needs_consent=bool(getattr(self.controller, 'fixed_environment', False) is True
+ and not (self.controller.root / 'engine/portable-consent.json').exists()),
+ review=dict(engine=str(row.get('selection', {}).get('engine', self.form['engine'])),
+ comfy=str(row.get('selection', {}).get('root', self.form['comfy']))))
diff --git a/freevideo_engine/launcher_settings.py b/freevideo_engine/launcher_settings.py
new file mode 100644
index 0000000..e8952a0
--- /dev/null
+++ b/freevideo_engine/launcher_settings.py
@@ -0,0 +1,97 @@
+"""Durable launcher preferences, independent of an EXE's unpack directory."""
+import json
+import os
+from pathlib import Path
+import time
+
+from .monitoring import save
+
+TEXT = ('comfy', 'destination', 'engine', 'models', 'python', 'url', 'language', 'offline_runtime')
+SELECTION = ('root', 'engine', 'source', 'python', 'url')
+
+
+def sanitize(value):
+ value = value if isinstance(value, dict) else {}
+ result = {k: value[k] for k in TEXT if isinstance(value.get(k), str)}
+ for key in ('separate', 'new_comfy'):
+ if type(value.get(key)) is bool:
+ result[key] = value[key]
+ if value.get('model_method') in ('auto', 'manual', 'reuse'):
+ result['model_method'] = value['model_method']
+ folders = value.get('model_dirs')
+ if isinstance(folders, list) and all(isinstance(p, str) for p in folders):
+ result['model_dirs'] = list(dict.fromkeys(folders))
+ offline = value.get('offline_models')
+ if isinstance(offline, list) and all(isinstance(p, str) for p in offline):
+ result['offline_models'] = list(dict.fromkeys(offline))
+ record = value.get('installation')
+ if isinstance(record, dict) and all(isinstance(record.get(k), str) and record[k] for k in SELECTION):
+ result['installation'] = {k: record[k] for k in SELECTION}
+ result['installation']['separate'] = record.get('separate') is True
+ if type(value.get('saved_at_ns')) is int:
+ result['saved_at_ns'] = value['saved_at_ns']
+ resume = value.get('setup')
+ if isinstance(resume, dict):
+ result['setup'] = {k: resume[k] for k in ('status', 'action', 'phase') if isinstance(resume.get(k), str)}
+ return result
+
+
+def read(path):
+ try:
+ return sanitize(json.loads(Path(path).read_text(encoding='utf-8')))
+ except (OSError, ValueError):
+ return {}
+
+
+def write(path, values, language, selection=None):
+ payload = dict(values, language=language, installation=selection)
+ save(path, sanitize(payload))
+
+
+def documents_directory():
+ override = os.environ.get('FREEVIDEO_DOCUMENTS_HOME')
+ if override:
+ return Path(override).expanduser()
+ if os.name == 'nt':
+ # CSIDL_PERSONAL honors redirected/OneDrive Documents and localized names.
+ import ctypes
+ from ctypes import wintypes
+ buffer = ctypes.create_unicode_buffer(32768)
+ function = ctypes.windll.shell32.SHGetFolderPathW
+ function.argtypes = [wintypes.HWND, ctypes.c_int, wintypes.HANDLE, wintypes.DWORD, wintypes.LPWSTR]
+ function.restype = ctypes.c_long
+ if function(None, 5, None, 0, buffer) == 0 and buffer.value:
+ return Path(buffer.value)
+ return Path.home() / 'Documents'
+
+
+class Store:
+ def __init__(self, legacy_root, documents=None):
+ self.local = Path(legacy_root) / 'comfy-launcher.json'
+ try:
+ self.primary = (Path(documents) if documents is not None else documents_directory()) / 'FreeVideo' / 'launcher.json'
+ except (OSError, RuntimeError, AttributeError):
+ self.primary = self.local
+ self.paths = list(dict.fromkeys((self.primary, self.local)))
+
+ def read(self):
+ candidates = [read(p) for path in self.paths for p in (path, path.with_suffix('.previous.json'))]
+ candidates = [value for value in candidates if value]
+ return max(candidates, key=lambda v: v.get('saved_at_ns', 0)) if candidates else {}
+
+ def write(self, values, language, selection=None, setup=None):
+ payload = sanitize(dict(values, language=language, installation=selection,
+ setup=setup, saved_at_ns=time.time_ns()))
+ failures, written = [], []
+ for path in self.paths:
+ try:
+ previous = read(path)
+ if previous:
+ save(path.with_suffix('.previous.json'), previous)
+ save(path, payload)
+ written.append(path)
+ except OSError as error:
+ failures.append((path, error))
+ if not written:
+ raise failures[0][1]
+ return written, failures
diff --git a/freevideo_engine/launcher_terminal.py b/freevideo_engine/launcher_terminal.py
new file mode 100644
index 0000000..d8d78c2
--- /dev/null
+++ b/freevideo_engine/launcher_terminal.py
@@ -0,0 +1,207 @@
+"""Embedded, bounded process output. Workers keep writing to durable log files."""
+import codecs
+import json
+from pathlib import Path
+
+from .terminal_ui import ESCAPES
+
+
+class Tail:
+ READ_BYTES = 64 * 1024
+ DISPLAY_CHARS = 192 * 1024
+
+ def __init__(self):
+ from .diagnostics import Redactor
+ self.redactor = Redactor()
+ self.path = None
+ self.reset()
+
+ def reset(self):
+ self.revision = getattr(self, 'revision', 0) + 1
+ self.offset = 0
+ self.identity = None
+ self.pending = ''
+ self.discard_line = False
+ self.decoder = codecs.getincrementaldecoder('utf-8')('replace')
+ self.text = ''
+
+ def select(self, path):
+ path = Path(path) if path else None
+ if path != self.path:
+ self.path = path
+ self.reset()
+
+ def clear(self):
+ # Clear only the view; never truncate a worker's retained log.
+ self.text = ''
+ self.pending = ''
+ self.decoder.reset()
+ self.discard_line = False
+ if self.path is not None:
+ try:
+ info = self.path.stat()
+ self.identity = (info.st_dev, info.st_ino)
+ self.offset = info.st_size
+ except OSError:
+ pass
+
+ def read(self, final=False):
+ if self.path is None:
+ return ''
+ try:
+ with self.path.open('rb') as stream:
+ import os
+ info = os.fstat(stream.fileno())
+ identity = (info.st_dev, info.st_ino)
+ if self.identity != identity or info.st_size < self.offset:
+ self.reset()
+ self.identity = identity
+ # Reopening a multi-day log must not load it into GUI RAM.
+ self.offset = max(0, info.st_size - self.DISPLAY_CHARS)
+ self.discard_line = self.offset > 0
+ stream.seek(self.offset)
+ raw = stream.read(self.READ_BYTES)
+ self.offset += len(raw)
+ except OSError:
+ return '' # A log can appear after startup; try again next poll.
+ value = self.pending + self.decoder.decode(raw).replace('\r', '\n')
+ if final and self.offset == info.st_size:
+ value += self.decoder.decode(b'', final=True)
+ if value and not value.endswith('\n'):
+ value += '\n'
+ rows = value.split('\n')
+ self.pending = rows.pop()
+ if self.discard_line and rows:
+ rows.pop(0)
+ self.discard_line = False
+ output = []
+ for line in rows:
+ if not line.strip():
+ continue
+ try:
+ event = json.loads(line)
+ except ValueError:
+ event = None
+ if isinstance(event, dict) and event.get('event') == 'freevideo_ui':
+ line = ' · '.join(str(event[k]) for k in ('label', 'detail') if event.get(k))
+ line = ESCAPES.sub('', line)
+ line = ''.join(c for c in line if c.isprintable() or c == '\t')
+ output.append(self.redactor.text(line))
+ if len(self.pending) > self.READ_BYTES:
+ self.pending = ''
+ self.discard_line = True
+ output.append('[Long output line omitted; original retained in the log]')
+ addition = '\n'.join(output) + ('\n' if output else '')
+ self.text = (self.text + addition)[-self.DISPLAY_CHARS:]
+ return addition
+
+
+class Terminal:
+ """A collapsible panel in the main window, sharing the launcher's poll loop."""
+ def __init__(self, parent, app, footer):
+ from tkinter import ttk
+ from . import branding as b
+ self.app, self.footer = app, footer
+ self.tail = Tail()
+ self.sources = []
+ self.follow = app.tk.BooleanVar(value=True)
+ self.frame = ttk.Frame(parent, padding=(0, b.space(2), 0, 0))
+ head = ttk.Frame(self.frame); head.pack(fill='x', pady=(0, b.space(1)))
+ app.label(head, 'Terminal', '终端', style='Meta.TLabel').pack(side='left')
+ self.selector = ttk.Combobox(head, state='readonly', width=18)
+ self.selector.pack(side='left', padx=b.space(2))
+ self.selector.bind('<<ComboboxSelected>>', self.select)
+ app.button(head, 'Hide', '收起', self.hide).pack(side='right')
+ app.button(head, 'Clear', '清屏', self.clear).pack(side='right', padx=b.space(1))
+ app.button(head, 'Copy', '复制', self.copy).pack(side='right')
+ follow = ttk.Checkbutton(head, text=app.t('Follow', '跟随'), variable=self.follow)
+ app.labels.append((follow, 'Follow', '跟随')); follow.pack(side='right', padx=b.space(2))
+ body = ttk.Frame(self.frame); body.pack(fill='both', expand=True)
+ self.output = app.tk.Text(body, height=8, wrap='word', relief='flat', borderwidth=0,
+ font=(b.MONO, 10), background=b.SURFACE, foreground=b.TEXT, padx=10, pady=8,
+ selectbackground=b.ACCENT, selectforeground=b.TEXT, state='disabled')
+ scroll = ttk.Scrollbar(body, command=self.output.yview)
+ scroll.pack(side='right', fill='y'); self.output.pack(fill='both', expand=True)
+ self.output.configure(yscrollcommand=scroll.set)
+ self.output.bind('<Control-c>', self.copy_selection)
+
+ def show(self):
+ if not self.frame.winfo_manager():
+ self.frame.pack(side='bottom', fill='x', after=self.footer)
+ self.poll()
+
+ def hide(self):
+ self.frame.pack_forget()
+
+ def toggle(self):
+ self.hide() if self.frame.winfo_manager() else self.show()
+
+ def select(self, _=None):
+ index = self.selector.current()
+ self.tail.select(self.sources[index][1] if 0 <= index < len(self.sources) else None)
+ self.render()
+
+ def poll(self):
+ if not self.frame.winfo_manager():
+ return
+ sources = getattr(self.app.controller, 'terminal_sources', lambda: [])()
+ labels = {'setup': self.app.t('Installation', '安装'), 'comfy': 'ComfyUI',
+ 'initialize': self.app.t('Bundle setup', '整合包初始化')}
+ if sources != self.sources:
+ previous = self.sources
+ self.sources = sources
+ self.selector.configure(values=[labels.get(name, name) for name, _ in sources])
+ if sources:
+ # A newly started stage follows automatically; a manual choice
+ # remains selected while the available sources are unchanged.
+ index = next((i for i in range(len(sources)-1, -1, -1) if sources[i] not in previous), len(sources)-1)
+ self.selector.current(index)
+ else:
+ self.selector.set('')
+ self.select()
+ else:
+ self.selector.configure(values=[labels.get(name, name) for name, _ in sources])
+ revision = self.tail.revision
+ running = getattr(self.app.controller, 'terminal_running', lambda path: self.app.controller.busy)
+ addition = self.tail.read(final=not running(self.tail.path))
+ if revision != self.tail.revision:
+ self.render()
+ elif addition:
+ self.append(addition)
+ elif not sources:
+ self.render(self.app.t('Installation and ComfyUI output will appear here.',
+ '安装和 ComfyUI 的输出会显示在这里。'))
+
+ def append(self, addition):
+ self.output.configure(state='normal')
+ self.output.insert('end', addition)
+ excess = int(self.output.count('1.0', 'end-1c', 'chars')[0]) - self.tail.DISPLAY_CHARS
+ if excess > 0:
+ self.output.delete('1.0', '1.0+%dc' % excess)
+ self.output.configure(state='disabled')
+ if self.follow.get():
+ self.output.see('end')
+
+ def render(self, value=None):
+ position = self.output.yview()[0]
+ self.output.configure(state='normal')
+ self.output.delete('1.0', 'end')
+ self.output.insert('1.0', self.tail.text if value is None else value)
+ self.output.configure(state='disabled')
+ self.output.see('end') if self.follow.get() else self.output.yview_moveto(position)
+
+ def copy(self):
+ self.app.window.clipboard_clear()
+ self.app.window.clipboard_append(self.tail.text)
+
+ def copy_selection(self, _=None):
+ try:
+ value = self.output.get('sel.first', 'sel.last')
+ except self.app.tk.TclError:
+ return 'break'
+ self.app.window.clipboard_clear(); self.app.window.clipboard_append(value)
+ return 'break'
+
+ def clear(self):
+ self.tail.clear()
+ self.render()
diff --git a/freevideo_engine/launcher_update.py b/freevideo_engine/launcher_update.py
new file mode 100644
index 0000000..1800aee
--- /dev/null
+++ b/freevideo_engine/launcher_update.py
@@ -0,0 +1,283 @@
+"""Background launcher updates. Standard library only; no model installation.
+
+Executables live in a versioned user cache. The original EXE stays intact and
+forwards to a newer, explicitly approved download on subsequent starts.
+"""
+import hashlib
+import json
+import os
+from pathlib import Path
+import re
+import subprocess
+import sys
+import threading
+import time
+import urllib.error
+import urllib.parse
+import urllib.request
+
+from .diagnostics import Redactor
+from .monitoring import save
+
+REPOSITORY = 'FlashML-org/FreeVideo'
+CHANNEL = 'windows-preview'
+API = 'https://api.github.com/repos/' + REPOSITORY
+RELEASE_PAGE = 'https://github.com/' + REPOSITORY + '/releases/tag/' + CHANNEL
+MAX_EXE_BYTES = 512 * 2**20
+
+
+def build_identity(value):
+ if (not isinstance(value, dict) or value.get('repository') != REPOSITORY
+ or value.get('channel') != CHANNEL or value.get('schema') != 1
+ or not re.fullmatch(r'[0-9a-f]{40,64}', str(value.get('revision', '')))
+ or type(value.get('built_at')) is not int or value['built_at'] <= 0
+ or not isinstance(value.get('version'), str) or len(value['version']) > 64):
+ raise ValueError('Invalid launcher build identity')
+ result = {k: value[k] for k in ('schema', 'repository', 'channel', 'revision', 'built_at', 'version')}
+ if value.get('packaging') == 'onedir':
+ result['packaging'] = 'onedir'
+ return result
+
+
+def current_build():
+ if not getattr(sys, 'frozen', False):
+ return None
+ try:
+ return build_identity(json.loads((Path(sys._MEIPASS) / 'launcher-build.json').read_text(encoding='utf-8')))
+ except (OSError, ValueError, TypeError):
+ return None
+
+
+def manifest(value):
+ result = build_identity(value)
+ asset = value.get('asset', {})
+ if (not isinstance(asset, dict) or type(asset.get('id')) is not int or asset['id'] <= 0
+ or type(asset.get('bytes')) is not int or not 2 <= asset['bytes'] <= MAX_EXE_BYTES
+ or not re.fullmatch(r'[0-9a-f]{64}', str(asset.get('sha256', '')))):
+ raise ValueError('Invalid launcher update asset')
+ result['asset'] = {k: asset[k] for k in ('id', 'bytes', 'sha256')}
+ return result
+
+
+def newer(candidate, current):
+ return candidate['revision'] != current['revision'] and candidate['built_at'] > current['built_at']
+
+
+def _trusted_url(url):
+ parsed = urllib.parse.urlsplit(url)
+ if (parsed.scheme != 'https' or parsed.username or parsed.password or parsed.port not in (None, 443)
+ or parsed.hostname not in ('api.github.com', 'github.com', 'release-assets.githubusercontent.com',
+ 'objects.githubusercontent.com', 'github-releases.githubusercontent.com')):
+ raise ValueError('Update redirect is not an approved HTTPS GitHub download')
+
+
+class UpdateRedirect(urllib.request.HTTPRedirectHandler):
+ def redirect_request(self, request, fp, code, msg, headers, newurl):
+ _trusted_url(newurl)
+ redirected = super().redirect_request(request, fp, code, msg, headers, newurl)
+ if urllib.parse.urlsplit(newurl).hostname != 'api.github.com':
+ redirected.remove_header('Authorization')
+ return redirected
+
+
+def open_download(url, token='', *, binary=False):
+ # One inherited route, then one direct route. Do not mutate shell, Git or
+ # registry proxies. Never retry a slow-but-active binary on another source.
+ if not url.startswith(API + '/'):
+ raise ValueError('Unexpected update API address')
+ for direct in (False, True):
+ opener = urllib.request.build_opener(urllib.request.ProxyHandler({} if direct else None), UpdateRedirect())
+ headers = {'User-Agent': 'FreeVideo-launcher', 'Accept': 'application/octet-stream' if binary else 'application/vnd.github+json'}
+ if token:
+ headers['Authorization'] = 'Bearer ' + token
+ try:
+ return opener.open(urllib.request.Request(url, headers=headers), timeout=5)
+ except (OSError, urllib.error.URLError):
+ if direct:
+ raise
+
+
+def _bytes(response, limit, cancel=None, seconds=15):
+ deadline = time.monotonic() + seconds
+ done = 0
+ read = getattr(response, 'read1', response.read)
+ while True:
+ if cancel is not None and cancel.is_set():
+ raise InterruptedError('Update download stopped; existing installation is unchanged')
+ if time.monotonic() > deadline:
+ raise TimeoutError('Update transfer exceeded its time limit; retry when the connection is available')
+ part = read(min(256*1024, limit-done+1))
+ if not part:
+ return
+ done += len(part)
+ if done > limit:
+ raise ValueError('Update response exceeds the expected size')
+ yield part
+
+
+def _json(url, token, *, binary=False, limit=2**20):
+ with open_download(url, token, binary=binary) as response:
+ return json.loads(b''.join(_bytes(response, limit)))
+
+
+def check(current, token=''):
+ build_identity(current)
+ release = _json(API + '/releases/tags/' + CHANNEL, token)
+ if not isinstance(release, dict) or release.get('draft') or release.get('tag_name') != CHANNEL:
+ raise ValueError('No published Windows update is available')
+ assets = {a.get('name'): a for a in release.get('assets', []) if isinstance(a, dict)}
+ metadata, executable = assets.get('update.json', {}), assets.get('FreeVideo.exe', {})
+ if type(metadata.get('id')) is not int:
+ raise ValueError('Update metadata is not published yet; check the release page')
+ candidate = manifest(_json(API + '/releases/assets/' + str(metadata['id']), token, binary=True, limit=65536))
+ if candidate['asset']['id'] != executable.get('id') or candidate['asset']['bytes'] != executable.get('size'):
+ raise ValueError('A new release is being published; check again shortly')
+ return candidate if newer(candidate, current) else None
+
+
+def checksum(path):
+ digest = hashlib.sha256()
+ with path.open('rb') as stream:
+ for block in iter(lambda: stream.read(256*1024), b''):
+ digest.update(block)
+ return digest.hexdigest()
+
+
+def verified(path, asset):
+ try:
+ return (path.is_file() and not path.is_symlink() and path.stat().st_size == asset['bytes']
+ and checksum(path) == asset['sha256'])
+ except OSError:
+ return False
+
+
+def executable_path(root, candidate):
+ return Path(root).absolute().resolve() / 'updates' / candidate['asset']['sha256'] / 'FreeVideo.exe'
+
+
+def download(candidate, root, token='', *, progress=None, cancel=None):
+ candidate = manifest(candidate)
+ asset = candidate['asset']
+ target = executable_path(root, candidate)
+ if verified(target, asset):
+ return target
+ target.parent.mkdir(parents=True, exist_ok=True)
+ # Unique staging keeps a cancelled/failed transfer and concurrent downloads
+ # separate. The original launcher and models are never overwritten.
+ import tempfile
+ import shutil
+ if shutil.disk_usage(target.parent).free < asset['bytes'] + 16*2**20:
+ raise OSError('Not enough disk space to download the launcher update')
+ start, done = time.monotonic(), 0
+ with open_download(API + '/releases/assets/' + str(asset['id']), token, binary=True) as response:
+ with tempfile.NamedTemporaryFile(prefix='download-', suffix='.partial', dir=target.parent, delete=False) as stream:
+ stage = Path(stream.name)
+ digest = hashlib.sha256()
+ for block in _bytes(response, asset['bytes'], cancel, seconds=1800):
+ stream.write(block); digest.update(block); done += len(block)
+ if progress:
+ elapsed = time.monotonic()-start
+ speed = done/max(.001, elapsed)
+ progress(dict(done=done, total=asset['bytes'], bytes_per_second=speed,
+ remaining_seconds=(asset['bytes']-done)/speed))
+ stream.flush(); os.fsync(stream.fileno())
+ if done != asset['bytes'] or digest.hexdigest() != asset['sha256']:
+ raise ValueError('Launcher update failed size/content verification; original EXE retained')
+ if cancel is not None and cancel.is_set():
+ raise InterruptedError('Update stopped before activation')
+ stage.replace(target)
+ return target
+
+
+def launch_download(candidate, root, *, token='', popen=None):
+ candidate = manifest(candidate)
+ path = executable_path(root, candidate)
+ if not verified(path, candidate['asset']):
+ raise ValueError('Downloaded launcher is missing or changed; download again')
+ env = dict(os.environ, FREEVIDEO_LAUNCHER_HOME=str(Path(root).absolute().resolve()))
+ if token:
+ env['GITHUB_TOKEN'] = token # Session only; never stored in a receipt or command.
+ kwargs = dict(cwd=path.parent, env=env, close_fds=True)
+ if os.name == 'nt':
+ kwargs['creationflags'] = subprocess.DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP
+ else:
+ kwargs['start_new_session'] = True
+ process = (popen or subprocess.Popen)([str(path)], **kwargs)
+ # Start first: a spawn error must not replace the approved startup receipt.
+ save(Path(root) / 'updates' / 'active.json', dict(approved=True, candidate=candidate))
+ return process
+
+
+def forward_approved(current, root):
+ """Opening the original shortcut starts the last approved newer EXE."""
+ if current.get('packaging') == 'onedir':
+ return False # Keep the user's folder distribution; do not switch to onefile.
+ try:
+ receipt = json.loads((Path(root)/'updates/active.json').read_text(encoding='utf-8'))
+ if receipt.get('approved') is not True:
+ return False
+ candidate = manifest(receipt.get('candidate'))
+ if not newer(candidate, current):
+ return False
+ if executable_path(root, candidate).resolve() == Path(sys.executable).resolve():
+ return False # A bad release identity must never recursively launch itself.
+ launch_download(candidate, root)
+ return True
+ except (OSError, ValueError, TypeError, AttributeError):
+ return False # A missing/corrupt cached update leaves the original app usable.
+
+
+class UpdateClient:
+ def __init__(self, current, root, token=None):
+ # Python 3.9 on Windows can leave a nonexistent relative path relative
+ # in resolve(). Updates must survive changing cwd to the EXE directory.
+ self.current, self.root = build_identity(current), Path(root).absolute().resolve()
+ self.token = token if token is not None else os.environ.get('GITHUB_TOKEN') or os.environ.get('GH_TOKEN', '')
+ self.state = dict(status='idle')
+ self.available = None
+ # Older launchers saved this as a mandatory update. Read it only as a
+ # cached reminder; a known newer version never prevents using this one.
+ for name in ('available.json', 'required.json'):
+ try:
+ candidate = manifest(json.loads((self.root / 'updates' / name).read_text(encoding='utf-8')))
+ if newer(candidate, self.current) and (self.available is None or newer(candidate, self.available)):
+ self.available = candidate
+ except (OSError, ValueError, TypeError):
+ pass
+ self.thread = None
+ self.cancelled = threading.Event()
+
+ @property
+ def busy(self):
+ return self.thread is not None and self.thread.is_alive()
+
+ def run(self, operation, candidate=None):
+ if self.busy:
+ return
+ if operation not in ('check', 'download'):
+ raise ValueError('Unknown update operation')
+ if operation == 'download' and self.current.get('packaging') == 'onedir':
+ raise ValueError('Download and extract the updated folder ZIP from the release page')
+ self.cancelled.clear()
+ self.state = dict(status='checking' if operation == 'check' else 'downloading', candidate=candidate)
+ def work():
+ try:
+ if operation == 'check':
+ found = check(self.current, self.token)
+ if found and (self.available is None or newer(found, self.available)):
+ self.available = found
+ save(self.root / 'updates/available.json', found)
+ self.state = dict(status='available' if self.available else 'current', candidate=self.available)
+ else:
+ def progress(value):
+ self.state = dict(status='downloading', candidate=candidate, progress=value)
+ download(candidate, self.root, self.token, progress=progress, cancel=self.cancelled)
+ self.state = dict(status='ready', candidate=candidate)
+ except Exception as error:
+ message = Redactor([(self.token, '<REDACTED>')] if self.token else []).text(str(error))
+ if isinstance(error, urllib.error.HTTPError) and error.code in (401, 403, 404):
+ message = 'GitHub update access unavailable. A private repository needs a GitHub token with read access; also check API rate limits.'
+ self.state = dict(status='cancelled' if self.cancelled.is_set() else 'error', error=message,
+ candidate=candidate or self.available)
+ self.thread = threading.Thread(target=work, daemon=True)
+ self.thread.start()
diff --git a/freevideo_engine/launcher_update_ui.py b/freevideo_engine/launcher_update_ui.py
new file mode 100644
index 0000000..dc63738
--- /dev/null
+++ b/freevideo_engine/launcher_update_ui.py
@@ -0,0 +1,161 @@
+"""Tk update prompt; networking is delegated to a background client."""
+import time
+import webbrowser
+
+from . import branding
+from .launcher_update import RELEASE_PAGE, launch_download
+
+
+class UpdateUI:
+ def __init__(self, app, client, footer):
+ self.app, self.client = app, client
+ self.dialog = None
+ self.notified = None
+ self.last_state = None
+ self.closed = False
+ self.timer = None
+ self.snoozed = False
+ self.button = app.ttk.Button(footer, text=app.t('Check updates', '检查更新'), command=self.open)
+ self.button.pack(side='left')
+ app.window.bind('<Destroy>', self.destroyed, add='+')
+
+ def start(self):
+ self.client.run('check')
+ self.poll()
+
+ def open(self):
+ app = self.app
+ if self.dialog and self.dialog.winfo_exists():
+ self.dialog.lift(); return
+ d = self.dialog = app.tk.Toplevel(app.window)
+ d.title(app.t('FreeVideo · Update', 'FreeVideo · 更新')); d.transient(app.window)
+ d.geometry('560x380'); d.minsize(440, 340)
+ d.configure(bg=branding.BACKGROUND)
+ body = app.ttk.Frame(d, padding=24); body.pack(fill='both', expand=True)
+ self.heading = app.ttk.Label(body, text='', wraplength=480)
+ self.heading.pack(anchor='w', pady=(0, 10))
+ self.detail = app.tk.Text(body, height=5, wrap='word', relief='flat',
+ bg=branding.BACKGROUND, fg=branding.TEXT, state='disabled')
+ self.detail.pack(fill='both', expand=True)
+ self.progress = app.ttk.Progressbar(body, maximum=1); self.progress.pack(fill='x', pady=8)
+ self.rate = app.ttk.Label(body, text=''); self.rate.pack(anchor='w')
+ self.access = app.ttk.Frame(body)
+ app.ttk.Label(self.access, text=app.t('GitHub token · private updates · this session only', 'GitHub Token · 私有更新 · 仅本次会话使用')).pack(anchor='w')
+ self.token = app.tk.StringVar(value=self.client.token)
+ app.ttk.Entry(self.access, textvariable=self.token, show='•').pack(fill='x')
+ actions = app.ttk.Frame(body); actions.pack(fill='x', pady=(14, 0))
+ self.primary = app.ttk.Button(actions, command=self.accept); self.primary.pack(side='left')
+ self.dismiss_button = app.ttk.Button(actions, command=self.dismiss)
+ self.dismiss_button.pack(side='left', padx=8)
+ app.ttk.Button(actions, text=app.t('Release page', '下载页'), command=lambda: webbrowser.open(RELEASE_PAGE)).pack(side='right')
+ d.protocol('WM_DELETE_WINDOW', self.dismiss)
+ self.render()
+ if self.client.state['status'] in ('idle', 'current', 'cancelled'):
+ self.client.run('check')
+
+ def dismiss(self):
+ self.snoozed = True
+ if self.client.state['status'] == 'downloading':
+ self.client.cancelled.set()
+ if self.dialog:
+ self.dialog.destroy(); self.dialog = None
+
+ def accept(self):
+ app, row = self.app, self.client.state
+ if self.client.busy or app.controller.busy:
+ return
+ try:
+ if row['status'] == 'ready':
+ app.save_settings()
+ launch_download(row['candidate'], self.client.root, token=self.client.token)
+ self.closed = True
+ app.close()
+ elif row['status'] == 'available' or (row['status'] in ('error', 'cancelled') and row.get('candidate')):
+ if getattr(self.client, 'current', {}).get('packaging') == 'onedir':
+ webbrowser.open(RELEASE_PAGE)
+ else:
+ self.client.run('download', row['candidate'])
+ else:
+ self.client.token = self.token.get().strip()
+ self.client.run('check')
+ except Exception as error:
+ from .diagnostics import Redactor
+ self.client.state = dict(status='error', error=Redactor([(self.client.token, '<REDACTED>')]).text(str(error)),
+ candidate=row.get('candidate'))
+
+ def render(self):
+ if not self.dialog or not self.dialog.winfo_exists():
+ return
+ app, row = self.app, self.client.state
+ state = row['status']
+ headings = {'checking': ('Checking for updates…', '正在检查更新…'),
+ 'available': ('A FreeVideo update is available', '发现 FreeVideo 新版本'),
+ 'downloading': ('Downloading update…', '正在下载更新…'),
+ 'ready': ('Update ready', '更新已就绪'),
+ 'current': ('You have the current release', '当前已是最新发布版本'),
+ 'error': ('Update check / download failed', '更新检查/下载失败'),
+ 'cancelled': ('Update paused', '更新已停止')}
+ self.heading.configure(text=app.t(*headings.get(state, headings['checking'])))
+ candidate = row.get('candidate')
+ description = row.get('error', '')
+ if candidate:
+ description += ('\n\n' if description else '') + '%s · %s · %s\n%.1f MiB\n\n%s' % (candidate['version'],
+ time.strftime('%Y-%m-%d %H:%M UTC', time.gmtime(candidate['built_at'])), candidate['revision'][:8],
+ candidate['asset']['bytes']/2**20,
+ app.t('Your models, environments and settings are kept. Restart the launcher after downloading.',
+ '保留已有模型、环境和设置。下载后重启启动器即可。'))
+ self.notified = candidate['revision']
+ if getattr(self.client, 'current', {}).get('packaging') == 'onedir':
+ description += '\n' + app.t('Download and extract the updated folder ZIP; keep all files together.',
+ '请下载并解压新版文件夹包,保留包内完整文件。')
+ if app.controller.busy:
+ description += '\n' + app.t('Finish the current installation before updating.', '请等待当前安装完成后再更新。')
+ self.detail.configure(state='normal'); self.detail.delete('1.0', 'end')
+ self.detail.insert('1.0', description); self.detail.configure(state='disabled')
+ progress = row.get('progress', {})
+ self.progress.configure(maximum=progress.get('total', 1), value=progress.get('done', 0))
+ self.rate.configure(text=('%.0f%% · %.1f MiB/s · ~%ds' %
+ (100*progress['done']/progress['total'], progress['bytes_per_second']/2**20,
+ progress['remaining_seconds'])) if progress else '')
+ if state == 'error':
+ self.access.pack(fill='x', pady=8, before=self.primary.master)
+ else:
+ self.access.pack_forget()
+ downloadable = state == 'available' or (state in ('error', 'cancelled') and candidate)
+ self.primary.configure(text=app.t('Restart & update', '重启并更新') if state == 'ready' else
+ app.t('Download folder ZIP', '下载文件夹版') if downloadable and
+ getattr(self.client, 'current', {}).get('packaging') == 'onedir' else
+ app.t('Download update', '下载更新') if downloadable else app.t('Check again', '重新检查'))
+ self.primary.state(['disabled'] if self.client.busy or app.controller.busy else ['!disabled'])
+ self.dismiss_button.configure(text=app.t('Later', '稍后更新') if candidate else app.t('Close', '关闭'))
+
+ def poll(self):
+ if self.timer is not None:
+ self.app.window.after_cancel(self.timer)
+ self.timer = None
+ if self.closed:
+ return
+ row = self.client.state
+ state = row['status']
+ self.button.configure(text=self.app.t('Update available', '有可用更新') if row.get('candidate') else
+ self.app.t('Update check failed', '更新检查失败') if state == 'error' else self.app.t('Check updates', '检查更新'))
+ if (row.get('candidate') and state in ('available', 'ready', 'error') and not self.snoozed and not self.client.busy and not self.app.controller.busy
+ and row['candidate']['revision'] != self.notified):
+ self.open()
+ key = (id(row), self.client.busy, self.app.controller.busy, self.app.zh)
+ if key != self.last_state:
+ self.last_state = key
+ self.app.refresh()
+ self.render()
+ self.timer = self.app.window.after(150, self.poll)
+
+ def stop(self):
+ self.closed = True
+ self.client.cancelled.set()
+ if self.timer is not None:
+ self.app.window.after_cancel(self.timer)
+ self.timer = None
+
+ def destroyed(self, event):
+ if event.widget == self.app.window:
+ self.stop()
diff --git a/freevideo_engine/launcher_view.py b/freevideo_engine/launcher_view.py
new file mode 100644
index 0000000..9d0a03e
--- /dev/null
+++ b/freevideo_engine/launcher_view.py
@@ -0,0 +1,403 @@
+"""Native setup wizard: compact navigation, rounded cards and live model status.
+
+Every size, gap and hue comes from `branding`. Copy stays short here: a card
+states what it needs and nothing else, and the full explanation lives in the
+dialog the user opens for it.
+"""
+import tkinter as tk
+from tkinter import ttk
+
+from . import branding as b
+from .model_status import FAMILIES, NAMES
+from .widgets import Card, PillButton, Progress, Radio, StepRail
+
+
+class Choice(Card):
+ """One of two mutually exclusive setups. The dot sits beside its title so
+ the card stays one text block tall on a 480-pixel-high window."""
+
+ def __init__(self, parent, app, value, title, detail):
+ super().__init__(parent, takefocus=1, cursor='hand2')
+ self.app, self.value, self.disabled, self.hovered = app, value, False, False
+ head = ttk.Frame(self.body, style='Card.TFrame')
+ head.pack(fill='x')
+ self.mark = Radio(head)
+ self.mark.pack(side='left', padx=(0, b.space(2)))
+ app.label(head, *title, style='Heading.Card.TLabel').pack(side='left')
+ label = app.label(self.body, *detail, style='Muted.Card.TLabel', wraplength=310)
+ label.pack(fill='x', pady=(b.space(1), 0))
+ self.body.bind('<Configure>', lambda e: label.configure(wraplength=max(100, e.width)), add='+')
+ for widget in (self, self.body, head, self.mark, *self.body.winfo_children(), *head.winfo_children()):
+ widget.bind('<Button-1>', self.choose)
+ widget.bind('<Enter>', self.hover, add='+')
+ widget.bind('<Leave>', self.leave, add='+')
+ self.bind('<Return>', self.choose); self.bind('<space>', self.choose)
+ self.bind('<FocusIn>', lambda _: self.state()); self.bind('<FocusOut>', lambda _: self.state())
+
+ def choose(self, _=None):
+ if not self.disabled:
+ self.app.mode.set(self.value)
+ self.focus_set()
+ return 'break'
+
+ def hover(self, _=None):
+ self.hovered = True
+ self.state()
+
+ def leave(self, _=None):
+ # Tk reports Leave when the pointer crosses onto a child of this card,
+ # so confirm it actually left the card before dropping the highlight.
+ x, y = self.winfo_pointerxy()
+ if (self.winfo_rootx() <= x < self.winfo_rootx() + self.winfo_width()
+ and self.winfo_rooty() <= y < self.winfo_rooty() + self.winfo_height()):
+ return
+ self.hovered = False
+ self.state()
+
+ def state(self, states=None):
+ if states is not None:
+ self.disabled = 'disabled' in states
+ selected = self.app.mode.get() == self.value
+ focused = self.focus_get() is self
+ self.tint(b.ACCENT if selected or focused else b.HOVER if self.hovered and not self.disabled else b.BORDER)
+ self.mark.select(selected, self.disabled)
+ self.resize()
+ return ('disabled',) if self.disabled else ()
+
+
+class ModelCard(Card):
+ """One model family. The state line names the phase; a single metadata
+ line carries the numbers, so three cards stay one block tall."""
+
+ STATES = {'waiting': b.BORDER, 'pending': b.ACCENT, 'downloading': b.ACCENT,
+ 'verifying': b.ACCENT, 'ready': b.SUCCESS, 'paused': b.WARNING}
+
+ def __init__(self, parent, app, name):
+ super().__init__(parent, padding=b.space(4), radius=b.RADIUS_MD)
+ self.resize_timer = None
+ self.bind('<Destroy>', self.cancel_resize, add='+')
+ self.app, self.name = app, name
+ self.title = app.label(self.body, *NAMES[name], style='Heading.Card.TLabel')
+ self.title.pack(anchor='w')
+ self.state = ttk.Label(self.body, style='Accent.Card.TLabel')
+ self.state.pack(anchor='w', pady=(b.space(1), b.space(2)))
+ self.bar = Progress(self.body, height=b.space(1) + 2); self.bar.pack(fill='x')
+ self.detail = ttk.Label(self.body, style='Meta.Card.TLabel', justify='left')
+ self.detail.pack(fill='x', pady=(b.space(2), 0))
+ self.body.bind('<Configure>', self.wrap, add='+')
+
+ def wrap(self, event):
+ for label in (self.title, self.state, self.detail):
+ label.configure(wraplength=max(100, event.width))
+ # Wrapping changes the required height, so measure again once Tk has
+ # applied it; otherwise a two-line title clips the line below it.
+ self.cancel_resize()
+ self.resize_timer = self.after_idle(self.finish_wrap)
+
+ def finish_wrap(self):
+ self.resize_timer = None
+ self.resize()
+
+ def cancel_resize(self, event=None):
+ if event is not None and event.widget != self:
+ return
+ if self.resize_timer is not None:
+ self.after_cancel(self.resize_timer)
+ self.resize_timer = None
+
+ def update(self, item, *, ready=False):
+ t = self.app.t
+ size = lambda n: ('%.1f GiB' % (n/2**30)) if n >= 2**30 else ('%.0f MiB' % (n/2**20))
+ if not item or ready and item.get('state') != 'ready':
+ self.state.configure(text=t('Ready locally', '本地已就绪') if ready else t('Waiting for scan', '等待扫描'),
+ foreground=b.SUCCESS if ready else b.MUTED)
+ self.detail.configure(text='' if ready else t('Scan first', '先查找已有模型'))
+ self.bar.configure(maximum=1, value=1 if ready else 0, tint=b.SUCCESS if ready else None)
+ self.tint(b.SUCCESS if ready else b.BORDER)
+ return
+ state = item['state']
+ labels = {'waiting': ('To download', '待下载') if item['download_bytes'] else ('Found locally', '已在本地找到'),
+ 'pending': ('Waiting for remaining files', '等待补齐剩余文件'),
+ 'downloading': ('Downloading', '下载中'),
+ 'verifying': ('Transfer complete · verifying (no re-download)', '传输完成 · 正在校验(不会重复下载)'),
+ 'ready': ('Ready', '已就绪'), 'paused': ('Paused · files retained', '已暂停 · 文件已保留')}
+ hue = self.STATES.get(state, b.ACCENT)
+ self.state.configure(text=t(*labels[state]), foreground=b.MUTED if state == 'waiting' else hue)
+ self.tint(hue)
+ existing, total, done = item['existing_bytes'], item['download_bytes'], item['downloaded_bytes']
+ # The state line already names the phase, so these are numbers only.
+ parts = []
+ if state == 'ready':
+ parts.append(size(item['total_bytes']))
+ else:
+ if total:
+ parts.append('%s / %s' % (size(done), size(total)))
+ elif existing:
+ parts.append(size(existing))
+ if item.get('ready_files'):
+ parts.append(t('%d / %d files', '%d / %d 个文件') % (item['ready_files'], item['files']))
+ rate = item.get('bytes_per_second')
+ if rate:
+ parts.append('%.1f MiB/s' % (rate/2**20))
+ elif state == 'waiting' and existing > item.get('verified_bytes', 0):
+ parts.append(t('verify first', '待校验'))
+ if state == 'verifying':
+ parts.append(t('Checksum is running; the next file may already be downloading',
+ '正在校验完整性;下一个文件可能已经开始下载'))
+ self.detail.configure(text=' · '.join(parts))
+ maximum = item['total_bytes'] or 1
+ self.bar.configure(maximum=maximum, value=maximum if state == 'ready' else
+ min(maximum, item.get('verified_bytes', 0)+done),
+ tint=b.WARNING if state == 'paused' else None)
+
+
+def build(app):
+ window, t = app.window, app.t
+ from .windows_ux import window_size, work_area
+ width, height = window_size(window)
+ left, top, screen_width, screen_height = work_area(window)
+ window.title('FreeVideo')
+ window.geometry('%dx%d%+d%+d' % (width, height, left+(screen_width-width)//2, top+(screen_height-height)//2))
+ # Tall enough for the pages whose content is fixed: both choice pages and
+ # the review page. At 540 every one of them clipped its last line, review
+ # by 34 px, and a wizard that hides part of what it asks you to accept is
+ # not a wizard. Installing, paused and ready pages still scroll: live task
+ # lists, a retained error and the install path have no bounded height.
+ window.minsize(min(680, width), min(600, height))
+ b.theme(window)
+ shell = ttk.Frame(window, padding=(b.space(7), b.space(4))); shell.pack(fill='both', expand=True)
+ header = ttk.Frame(shell); header.pack(fill='x', pady=(0, b.space(4)))
+ # Sized so the three header actions still fit their labels at 680 px,
+ # the narrowest window this wizard allows.
+ app.logo = b.wordmark(header, 170); app.logo.pack(side='left')
+ app.language_button = app.button(header, '中文', 'EN', app.language)
+ app.language_button.configure(style='Quiet.TButton'); app.language_button.pack(side='right')
+ app.option_button = app.button(header, 'Settings', '设置', app.show_options)
+ app.option_button.configure(style='Quiet.TButton'); app.option_button.pack(side='right', padx=b.space(1))
+ app.download_button = app.button(header, 'Download sources', '下载源', app.download_settings)
+ app.download_button.configure(style='Quiet.TButton'); app.download_button.pack(side='right', padx=b.space(1))
+ app.steps_frame = StepRail(shell, app, (('ComfyUI', 'ComfyUI'), ('Models', '模型')))
+ app.steps_frame.pack(fill='x', pady=(0, b.space(3)))
+ # A hairline, not ttk's two-pixel separator: the rail already divides.
+ app.steps_separator = tk.Frame(shell, height=1, background=b.BORDER)
+ app.steps_separator.pack(fill='x')
+ footer = ttk.Frame(shell, padding=(0, b.space(2), 0, 0)); footer.pack(side='bottom', fill='x')
+ app.consent = ttk.Checkbutton(footer, variable=app.accept, command=app.refresh)
+ actions = app.action_row = ttk.Frame(footer); actions.pack(side='bottom', fill='x')
+ app.back_button = app.button(actions, 'Back', '上一步', app.back)
+ app.back_button.configure(style='Quiet.TButton'); app.back_button.pack(side='left')
+ app.launcher_button = app.button(actions, 'Return to launcher', '返回启动器', app.show_launcher)
+ app.launcher_button.configure(style='Quiet.TButton')
+ app.primary = PillButton(actions, app.act)
+ app.primary.pack(side='right')
+ app.stop_button = app.button(actions, 'Pause installation', '暂停安装', app.controller.cancel)
+ app.stop_button.configure(style='Quiet.TButton')
+ app.stop_button.pack(side='right', padx=(0, b.space(2)))
+ tools = ttk.Frame(footer); tools.pack(side='bottom', fill='x', pady=(0, b.space(1)))
+ app.detail_button = app.button(tools, 'Details', '详情', app.show_details)
+ app.detail_button.configure(style='Quiet.TButton'); app.detail_button.pack(side='left')
+ app.copy_error_button = app.button(tools, 'Copy error', '复制错误', app.copy_error)
+ app.copy_error_button.configure(style='Quiet.TButton')
+ from . import __version__
+ ttk.Label(tools, text=__version__, style='Meta.TLabel').pack(side='right')
+ app.update_row = tools
+ from .launcher_terminal import Terminal
+ app.terminal = Terminal(shell, app, footer)
+ app.terminal_button = app.button(tools, 'Terminal', '终端', app.terminal.toggle)
+ app.terminal_button.configure(style='Quiet.TButton'); app.terminal_button.pack(side='left', padx=b.space(1))
+ viewport = ttk.Frame(shell); viewport.pack(fill='both', expand=True, pady=(b.space(4), 0))
+ app.canvas = canvas = tk.Canvas(viewport, background=b.BACKGROUND, highlightthickness=0, width=1)
+ scrollbar = ttk.Scrollbar(viewport, command=canvas.yview, style='Wizard.Vertical.TScrollbar')
+ scrollbar.pack(side='right', fill='y'); canvas.pack(side='left', fill='both', expand=True)
+ def scroll_state(first, last):
+ scrollbar.set(first, last)
+ if float(first) == 0 and float(last) == 1:
+ scrollbar.pack_forget()
+ elif not scrollbar.winfo_manager():
+ scrollbar.pack(side='right', fill='y', before=canvas)
+ canvas.configure(yscrollcommand=scroll_state)
+ app.content = content = ttk.Frame(canvas)
+ app.layout_timer = None
+ handle = canvas.create_window(0, 0, window=content, anchor='nw')
+ def place_content(_=None):
+ if app.layout_timer is not None:
+ window.after_cancel(app.layout_timer)
+ app.layout_timer = None
+ width = max(1, canvas.winfo_width())
+ canvas.itemconfigure(handle, width=width)
+ # Centre a page that fits; a taller one starts at the top and scrolls.
+ height, needed = canvas.winfo_height(), content.winfo_reqheight()
+ top = max(0, (height - needed)//2)
+ canvas.coords(handle, 0, top)
+ # Anchor the region at zero. Taking it from bbox('all') starts it at
+ # the centring offset, and yview_moveto(0) then scrolls that offset
+ # away, so a page was centred or flush to the top depending on
+ # whether it had been shown before.
+ canvas.configure(scrollregion=(0, 0, width, max(height, top + needed)))
+ app.place_content = place_content
+ canvas.bind('<Configure>', place_content)
+ content.bind('<Configure>', place_content)
+ def wheel(event):
+ if str(event.widget).startswith(str(content)) or event.widget == canvas:
+ canvas.yview_scroll(-1 if getattr(event, 'num', 0) == 4 else 1 if getattr(event, 'num', 0) == 5 else -int(event.delta/120), 'units')
+ for name in ('<MouseWheel>', '<Button-4>', '<Button-5>'):
+ window.bind(name, wheel, add='+')
+ app.heading = ttk.Label(content, style='Title.TLabel', anchor='center')
+ app.heading.pack(fill='x')
+ app.status_label = ttk.Label(content, textvariable=app.status, style='Muted.TLabel',
+ anchor='center', justify='center', wraplength=800)
+ # Leave room for font-dependent wrapping at the declared minimum size.
+ app.status_label.pack(fill='x', pady=(b.space(1), b.space(3)))
+ content.bind('<Configure>', lambda e: app.status_label.configure(wraplength=max(100, e.width)), add='+')
+ app.pages = [ttk.Frame(content) for _ in range(4)]
+ app.inputs = app.pages[0]
+ choices = ttk.Frame(app.inputs); choices.pack(fill='x', pady=(0, b.space(4)))
+ choices.columnconfigure((0, 1), weight=1, uniform='choices')
+ app.mode_buttons = []
+ for index, (value, title, detail) in enumerate((
+ ('existing', ('I have ComfyUI', '我已安装 ComfyUI'), ('Connect your installation.', '接入现有安装。')),
+ ('new', ('Install ComfyUI for me', '帮我安装 ComfyUI'), ('ComfyUI, Python and FreeVideo together.', '一并准备 ComfyUI、Python 和 FreeVideo。')))):
+ choice = Choice(choices, app, value, title, detail)
+ choice.grid(row=0, column=index, sticky='nsew', padx=(0, b.space(2)) if index == 0 else (b.space(2), 0))
+ app.mode_buttons.append(choice)
+ folder = Card(app.inputs); folder.pack(fill='x')
+ ttk.Label(folder.body, textvariable=app.folder_label, style='Heading.Card.TLabel').pack(anchor='w')
+ row = ttk.Frame(folder.body, style='Card.TFrame'); row.pack(fill='x', pady=(b.space(3), b.space(2)))
+ app.entry = ttk.Entry(row, textvariable=app.selected_folder(), style='Well.TEntry')
+ app.entry.pack(side='left', fill='x', expand=True)
+ app.browse_button = app.button(row, 'Browse…', '浏览…', lambda: app.browse(app.selected_folder()))
+ app.browse_button.configure(style='Card.TButton')
+ app.browse_button.pack(side='right', padx=(b.space(2), 0))
+ ttk.Label(folder.body, textvariable=app.folder_hint, style='Meta.Card.TLabel', wraplength=760).pack(fill='x')
+ models = Card(app.pages[1], padding=b.space(4)); models.pack(fill='x')
+ methods = ttk.Frame(models.body, style='Card.TFrame'); methods.pack(fill='x')
+ app.model_method_buttons = []
+ for value, en, zh in (('auto', 'Automatic download', '自动下载'),
+ ('manual', 'Download myself', '手动下载'), ('reuse', 'Use existing models', '复用已有模型')):
+ button = ttk.Radiobutton(methods, text=app.t(en, zh), value=value, variable=app.model_method)
+ button.pack(side='left', padx=(0, b.space(3)))
+ app.labels.append((button, en, zh)); app.model_method_buttons.append(button)
+ app.model_help = ttk.Label(models.body, style='Meta.Card.TLabel', wraplength=660)
+ app.model_help.pack(fill='x', pady=(b.space(2), b.space(1)))
+ models.body.bind('<Configure>', lambda e: app.model_help.configure(wraplength=max(100, e.width)), add='+')
+ app.model_source_row = ttk.Frame(models.body, style='Card.TFrame')
+ app.label(app.model_source_row, 'Download source', '下载源', style='Meta.Card.TLabel').pack(side='left', padx=(0, 8))
+ app.model_source = ttk.Combobox(app.model_source_row, state='readonly', width=18)
+ app.model_source.pack(side='left'); app.model_source.bind('<<ComboboxSelected>>', app.select_model_source)
+ app.token_link = app.button(app.model_source_row, 'Test speeds / settings', '测速/设置', app.download_settings)
+ app.token_link.configure(style='Card.TButton'); app.token_link.pack(side='left', padx=8)
+ app.model_source_status = ttk.Label(models.body, style='Meta.Card.TLabel', wraplength=660)
+ app.model_source_status.pack(fill='x')
+ app.model_components = ttk.Frame(models.body, style='Card.TFrame')
+ app.model_components.pack(fill='x', pady=(b.space(3), 0))
+ app.label(app.model_components, 'Download links by component', '按组件查看下载链接', style='Meta.Card.TLabel').pack(anchor='w', pady=(0, 6))
+ component_row = ttk.Frame(app.model_components, style='Card.TFrame'); component_row.pack(fill='x')
+ app.component_buttons = {}
+ for component, en, zh in (('video', 'Video model ↗', '视频模型 ↗'),
+ ('encoder', 'Text encoder ↗', '文本编码器 ↗'), ('decoder', 'Decoders ↗', '解码器 ↗')):
+ button = app.button(component_row, en, zh, lambda name=component: app.model_download_link(name))
+ button.configure(style='Card.TButton'); button.pack(side='left', padx=(0, 8))
+ app.component_buttons[component] = button
+ app.model_reuse_button = app.button(models.body, 'Choose model folders…', '选择模型文件夹…',
+ lambda: app.model_method.set('reuse'))
+ app.model_reuse_button.configure(style='Card.TButton')
+ app.model_library = library = ttk.Frame(models.body, style='Card.TFrame')
+ app.model_list = tk.Listbox(library, height=2, selectmode='extended', background=b.BACKGROUND,
+ foreground=b.TEXT, selectbackground=b.ACCENT_SUBTLE, selectforeground=b.TEXT,
+ highlightthickness=0, relief='flat', borderwidth=0,
+ font=(b.FAMILY, b.BODY), activestyle='none', exportselection=False)
+ app.model_list.pack(fill='x', ipady=b.space(1))
+ row = ttk.Frame(library, style='Card.TFrame'); row.pack(fill='x', pady=(8, 0))
+ app.add_model_button = app.button(row, '+ Add folder', '+ 添加目录', app.add_model_folder)
+ app.add_model_button.configure(style='Card.TButton'); app.add_model_button.pack(side='left')
+ app.remove_model_button = app.button(row, 'Remove selected', '移除所选', app.remove_model_folders)
+ app.remove_model_button.configure(style='Card.TButton')
+ app.remove_model_button.pack(side='left', padx=b.space(2))
+ # The plan figures and the disclosure share the centred title axis.
+ app.summary = ttk.Label(app.pages[2], style='Muted.TLabel', wraplength=800,
+ anchor='center', justify='center')
+ app.summary.pack(fill='x', pady=(0, b.space(2)))
+ grid = ttk.Frame(app.pages[2]); grid.pack(fill='x', pady=(0, b.space(3)))
+ grid.columnconfigure((0, 1, 2), weight=1, uniform='models')
+ app.model_cards = {}
+ for index, name in enumerate(FAMILIES):
+ card = ModelCard(grid, app, name)
+ card.grid(row=0, column=index, sticky='nsew',
+ padx=(0 if index == 0 else b.space(2), 0 if index == 2 else b.space(2)))
+ app.model_cards[name] = card
+ # The phase is the headline; stage counts and elapsed time are metadata.
+ app.progress_card = Card(app.pages[2], padding=b.space(4)); app.progress_card.pack(fill='x')
+ app.progress_box = app.progress_card.body
+ ttk.Label(app.progress_box, textvariable=app.phase, style='ProgressTitle.Card.TLabel',
+ anchor='center', wraplength=760).pack(fill='x')
+ app.overall_bar = Progress(app.progress_box, height=b.space(2)); app.overall_bar.pack(fill='x', pady=(b.space(3), b.space(2)))
+ counters = ttk.Frame(app.progress_box, style='Card.TFrame'); counters.pack(fill='x')
+ ttk.Label(counters, textvariable=app.overall_text, style='Meta.Card.TLabel').pack(side='left')
+ ttk.Label(counters, textvariable=app.meta, style='Meta.Card.TLabel').pack(side='right')
+ app.bar = Progress(app.progress_box, height=b.space(1) - 1, subdued=True)
+ app.bar.pack(fill='x', pady=(b.space(4), 0))
+ current = ttk.Frame(app.progress_box, style='Card.TFrame'); current.pack(fill='x', pady=(b.space(2), 0))
+ ttk.Label(current, textvariable=app.current_text, style='Meta.Card.TLabel').pack(side='left')
+ ttk.Label(current, textvariable=app.details, style='Meta.Card.TLabel').pack(side='right')
+ app.review_card = Card(app.pages[2], padding=b.space(4), radius=b.RADIUS_MD,
+ fill=b.DANGER_SUBTLE, body_style='Alert.Card.TFrame')
+ app.review_card.tint(b.DANGER)
+ app.review = tk.Text(app.review_card.body, height=3, wrap='word', relief='flat',
+ background=b.DANGER_SUBTLE, foreground=b.DANGER, highlightthickness=0,
+ borderwidth=0, font=(b.FAMILY, b.BODY), state='disabled')
+ app.review.pack(fill='x')
+ launch = Card(app.pages[3], padding=b.space(7)); launch.pack(fill='x', pady=(b.space(3), b.space(5)))
+ badge = tk.Canvas(launch.body, width=44, height=44, bg=b.SURFACE, highlightthickness=0)
+ badge.pack(pady=(0, b.space(4)))
+ badge.create_oval(1, 1, 43, 43, fill=b.SUCCESS_SUBTLE, outline='')
+ badge.create_line(14, 23, 20, 30, 31, 16, fill=b.SUCCESS, width=3, capstyle='round', joinstyle='round')
+ app.label(launch.body, 'Ready to launch', '安装已就绪', style='Hero.Card.TLabel', anchor='center').pack(fill='x', pady=(0, b.space(4)))
+ app.launch_paths = ttk.Label(launch.body, style='Meta.Card.TLabel', anchor='center', justify='center', wraplength=680)
+ app.launch_paths.pack(fill='x', pady=(0, b.space(3)))
+ app.label(launch.body, 'Already running ComfyUI? FreeVideo connects automatically.',
+ 'ComfyUI 已在运行?会自动连接。', style='Meta.Card.TLabel', anchor='center', justify='center', wraplength=680).pack(fill='x')
+ app.browser_controls = ttk.Frame(launch.body, style='Card.TFrame')
+ ttk.Entry(app.browser_controls, textvariable=app.browser_address, state='readonly', justify='center').pack(fill='x')
+ browser_buttons = ttk.Frame(app.browser_controls, style='Card.TFrame'); browser_buttons.pack(pady=(8, 0))
+ app.browser_button = app.button(browser_buttons, 'Open browser', '打开浏览器', app.open_browser)
+ app.browser_button.pack(side='left', padx=4)
+ app.button(browser_buttons, 'Copy address', '复制地址', app.copy_browser_address).pack(side='left', padx=4)
+ shortcuts = ttk.Frame(launch.body, style='Card.TFrame'); shortcuts.pack(pady=(16, 0))
+ app.shortcut_button = app.button(shortcuts, 'Create desktop shortcut', '创建桌面快捷方式', app.create_shortcut)
+ app.shortcut_button.pack(side='left', padx=4)
+ app.shortcut_status = ttk.Label(shortcuts, style='Meta.Card.TLabel'); app.shortcut_status.pack(side='left', padx=4)
+ launch.body.bind('<Configure>', lambda e: app.launch_paths.configure(wraplength=max(100, e.width)))
+ app.launch_error = ttk.Label(app.pages[3], style='Danger.TLabel', wraplength=760, justify='left')
+ app.launch_error.pack(fill='x')
+ app.engine_update_row = ttk.Frame(app.pages[3])
+ app.label(app.engine_update_row, 'Engine update available', '引擎有可用更新').pack(side='left')
+ app.engine_update_button = app.button(app.engine_update_row, 'Update', '更新', app.update_engine)
+ app.engine_update_button.pack(side='right')
+ app.pages[3].bind('<Configure>', lambda e: app.launch_error.configure(wraplength=max(100, e.width)))
+
+
+def show_page(app):
+ if app.wizard_step == 3:
+ app.primary.configure(width=32)
+ app.primary.pack(side='top', anchor='center', pady=(0, b.space(2)), before=app.back_button)
+ app.back_button.pack_configure(side='top', anchor='center')
+ app.steps_frame.pack_forget()
+ else:
+ app.primary.configure(width=0)
+ app.primary.pack(side='right', pady=0)
+ app.back_button.pack_configure(side='left', anchor='w')
+ if app.wizard_step != 3 and not app.steps_frame.winfo_manager():
+ app.steps_frame.pack(fill='x', pady=(0, b.space(3)), before=app.steps_separator)
+ app.heading.pack_configure(fill='x')
+ for index, page in enumerate(app.pages):
+ if index == app.wizard_step:
+ if not page.winfo_manager():
+ page.pack(fill='x'); app.canvas.yview_moveto(0)
+ else:
+ page.pack_forget()
+ app.steps_frame.show(min(app.wizard_step, 1))
+ if app.layout_timer is not None:
+ app.window.after_cancel(app.layout_timer)
+ # The root owns this callback so its Destroy handler can cancel it before
+ # Tk deletes the registered command, even when a page closes before idle.
+ app.layout_timer = app.window.after_idle(app.place_content)
diff --git a/freevideo_engine/licenses/latent-upscaler-MIT.txt b/freevideo_engine/licenses/latent-upscaler-MIT.txt
new file mode 100644
index 0000000..4e34e9f
--- /dev/null
+++ b/freevideo_engine/licenses/latent-upscaler-MIT.txt
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2026 LBH-123-AI
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/freevideo_engine/linux_process_host.py b/freevideo_engine/linux_process_host.py
new file mode 100644
index 0000000..fb51c06
--- /dev/null
+++ b/freevideo_engine/linux_process_host.py
@@ -0,0 +1,76 @@
+"""Own an install step's descendants, including SDKs that create new sessions.
+
+The supervisor adopts orphaned grandchildren and runs cleanup if the controller
+dies. It needs no root privileges, cgroup delegation, or third-party packages.
+"""
+import ctypes
+import os
+from pathlib import Path
+import signal
+import subprocess
+import sys
+import time
+
+
+def children():
+ return [int(pid) for pid in Path('/proc/self/task/%d/children' % os.getpid()).read_text().split()]
+
+
+def cleanup():
+ deadline = time.monotonic() + 1.5
+ hard_deadline = deadline + 3
+ terminated = set()
+ while True:
+ try:
+ while os.waitpid(-1, os.WNOHANG)[0]:
+ pass
+ except ChildProcessError:
+ return
+ pending = children()
+ if not pending:
+ return
+ for pid in pending:
+ try:
+ if time.monotonic() >= deadline:
+ os.kill(pid, signal.SIGKILL)
+ elif pid not in terminated:
+ os.kill(pid, signal.SIGTERM)
+ terminated.add(pid)
+ except ProcessLookupError:
+ pass
+ if time.monotonic() > hard_deadline:
+ raise RuntimeError('Install descendants did not exit after SIGKILL; retain the lock and inspect blocked disk I/O')
+ time.sleep(.05)
+
+
+def main():
+ parent, inherited, *command = sys.argv[1:]
+ stopping = []
+ for name in (signal.SIGTERM, signal.SIGINT, signal.SIGHUP):
+ signal.signal(name, lambda *_: stopping.append(True))
+ libc = ctypes.CDLL(None, use_errno=True)
+ for option, value in ((36, 1),): # SUBREAPER
+ if libc.prctl(option, value, 0, 0, 0):
+ raise OSError(ctypes.get_errno(), 'Linux install process supervision is unavailable')
+ if os.getppid() != int(parent) or stopping:
+ return 130 # Close the race where the controller died during startup.
+ try:
+ child = subprocess.Popen(command, pass_fds=tuple(int(fd) for fd in inherited.split(',') if fd))
+ # PDEATHSIG follows the creating *thread*, which may return as soon as
+ # ComfyUI is ready. Follow the parent process instead; reparenting on
+ # its exit cannot be confused with a later reuse of the original PID.
+ while child.poll() is None and not stopping:
+ if os.getppid() != int(parent):
+ stopping.append(True)
+ break
+ try:
+ child.wait(timeout=.1)
+ except subprocess.TimeoutExpired:
+ pass
+ return 130 if stopping else child.returncode if child.returncode >= 0 else 128-child.returncode
+ finally:
+ cleanup()
+
+
+if __name__ == '__main__':
+ raise SystemExit(main())
diff --git a/freevideo_engine/local_models.py b/freevideo_engine/local_models.py
new file mode 100644
index 0000000..eb69ac8
--- /dev/null
+++ b/freevideo_engine/local_models.py
@@ -0,0 +1,264 @@
+"""Find pinned models in an existing library without changing that library."""
+import json
+import os
+from pathlib import Path
+import stat
+import time
+
+from .download_cache import discard_read_cache
+from .storage import fingerprint
+
+BLOCK = 4 * 2**20
+MAX_ENTRIES = 200_000
+MAX_CANDIDATES = 2048
+MAX_ROOTS = 64
+
+
+def library_roots(values):
+ """Normalize explicitly selected libraries without walking entire drives."""
+ if not isinstance(values, list) or len(values) > MAX_ROOTS:
+ raise ValueError('Select at most 64 model libraries')
+ roots = []
+ for value in values:
+ if not isinstance(value, str) or not value.strip() or len(value) > 4096:
+ raise ValueError('Invalid model library path')
+ path = Path(value).expanduser().resolve()
+ if not path.is_dir() or path == Path(path.anchor):
+ raise ValueError('Choose an existing model folder, not an entire drive: ' + str(path))
+ if path not in roots:
+ roots.append(path)
+ # Explicit symlink/junction roots are resolved above. Internal directory
+ # links are still skipped by files(); never follow an arbitrary graph.
+ return [str(path) for path in roots if not any(parent in roots for parent in path.parents)]
+
+
+def scan_many(directories, rows, *, prior=None, callback=None):
+ roots = library_roots(directories)
+ result = dict(root=roots[0] if roots else None, roots=roots, matches={},
+ candidate_files=0, rejected_count=0, rejected=[], reused_bytes=0)
+ for directory in roots:
+ remaining = [row for row in rows if key(row) not in result['matches']]
+ if not remaining:
+ break
+ previous = dict(root=directory, matches=(prior or {}).get('matches', {}))
+ found = scan(directory, remaining, prior=previous, callback=callback)
+ result['matches'].update(found['matches'])
+ result['candidate_files'] += found['candidate_files']
+ result['rejected_count'] += found['rejected_count']
+ result['rejected'] = (result['rejected'] + found['rejected'])[:20]
+ result['reused_bytes'] += found['reused_bytes']
+ return result
+
+
+def key(row):
+ return row['repo'] + '/' + row['file']
+
+
+def expected(row):
+ return row.get('sha256') or row['git_blob']
+
+
+def can_link(source, destination_parent):
+ """A read-only plan: never create a probe file before consent."""
+ source_device = Path(source).stat().st_dev
+ if not source_device or source_device != Path(destination_parent).stat().st_dev:
+ return False
+ if os.name != 'nt':
+ return True
+ import ctypes
+ from ctypes import wintypes
+ kernel = ctypes.WinDLL('kernel32', use_last_error=True)
+ kernel.GetVolumePathNameW.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD]
+ kernel.GetVolumeInformationW.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD,
+ ctypes.POINTER(wintypes.DWORD), ctypes.POINTER(wintypes.DWORD), ctypes.POINTER(wintypes.DWORD),
+ wintypes.LPWSTR, wintypes.DWORD]
+ volume, filesystem = ctypes.create_unicode_buffer(32768), ctypes.create_unicode_buffer(64)
+ if not kernel.GetVolumePathNameW(str(destination_parent), volume, len(volume)):
+ return False
+ if not kernel.GetVolumeInformationW(volume.value, None, 0, None, None, None, filesystem, len(filesystem)):
+ return False
+ return filesystem.value.upper() == 'NTFS'
+
+
+def progress_output(value):
+ print(json.dumps(dict(event='local_model_progress', **value)), flush=True)
+
+
+class Progress:
+ def __init__(self, callback=None):
+ self.callback = callback
+ self.started = self.tick = time.monotonic()
+
+ def send(self, phase, path='', done=0, total=0, *, force=False):
+ now = time.monotonic()
+ if self.callback and (force or now-self.tick >= .5):
+ elapsed = now-self.started
+ self.callback(dict(phase=phase, file=Path(path).name if path else '', done_bytes=done,
+ total_bytes=total, elapsed_seconds=elapsed,
+ bytes_per_second=done/elapsed if elapsed and total else None))
+ self.tick = now
+
+
+def hash_source(path, row, progress=None, output=None):
+ """Bounded RAM hashing, optionally copying in the same sequential read."""
+ import hashlib
+ before = fingerprint(path)
+ if before['bytes'] != row['bytes'] or not stat.S_ISREG(path.stat().st_mode):
+ raise ValueError('Local model size/type changed: ' + str(path))
+ digest = hashlib.new('sha256' if row.get('sha256') else 'sha1')
+ if not row.get('sha256'):
+ digest.update(('blob %d\0' % row['bytes']).encode())
+ done = 0
+ phase = 'copy' if output is not None else 'verify'
+ progress = progress or Progress()
+ progress.started = time.monotonic()
+ progress.send(phase, path, total=row['bytes'], force=True)
+ with path.open('rb') as stream:
+ for block in iter(lambda: stream.read(BLOCK), b''):
+ if done+len(block) > row['bytes']:
+ raise ValueError('Local model grew while being read: ' + str(path))
+ digest.update(block)
+ if output is not None:
+ output.write(block)
+ discard_read_cache(stream.fileno(), done, len(block))
+ done += len(block)
+ progress.send(phase, path, done, row['bytes'])
+ if fingerprint(path) != before or done != row['bytes']:
+ raise ValueError('Local model changed while being read: ' + str(path))
+ progress.send(phase, path, done, row['bytes'], force=True)
+ return digest.hexdigest(), before
+
+
+def files(directory, progress):
+ seen = 0
+ def failed(error):
+ raise OSError('Cannot read part of the selected model folder; choose an accessible folder') from error
+ for root, directories, names in os.walk(directory, followlinks=False, onerror=failed):
+ kept = []
+ for name in sorted(directories):
+ path = Path(root) / name
+ info = path.lstat()
+ if (name not in ('.git', '.venv', 'envs', 'node_modules', '__pycache__')
+ and not stat.S_ISLNK(info.st_mode) and not getattr(info, 'st_file_attributes', 0) & 0x400):
+ kept.append(name)
+ directories[:] = kept
+ seen += len(names) + len(directories)
+ if seen > MAX_ENTRIES:
+ raise ValueError('Too many files to scan. Select a narrower model folder.')
+ progress.send('scan', done=seen)
+ for name in sorted(names):
+ path = Path(root) / name
+ try:
+ # HF snapshot file symlinks are valid; directory links/junctions
+ # are not traversed. Later reads use the resolved regular file.
+ resolved = path.resolve(strict=True)
+ info = resolved.stat()
+ if stat.S_ISREG(info.st_mode):
+ yield resolved, info
+ except (OSError, RuntimeError):
+ continue
+
+
+def scan(directory, rows, *, prior=None, callback=None):
+ directory = Path(directory).expanduser().resolve()
+ if not rows:
+ return dict(root=str(directory), matches={}, candidate_files=0, rejected_count=0, rejected=[], reused_bytes=0)
+ if not directory.is_dir():
+ raise ValueError('Existing model folder is missing or unreadable: ' + str(directory))
+ progress = Progress(callback)
+ progress.send('scan', force=True)
+ by_size = {}
+ for row in rows:
+ by_size.setdefault(row['bytes'], []).append(row)
+ candidates, seen = [], set()
+ for path, info in files(directory, progress):
+ identity = (info.st_dev, info.st_ino) if info.st_ino else str(path)
+ if info.st_size in by_size and identity not in seen:
+ candidates.append(path)
+ seen.add(identity)
+ if len(candidates) > MAX_CANDIDATES:
+ raise ValueError('Too many possible models. Select a narrower model folder.')
+ # Prefer expected names/known HF blobs; still verify the actual content.
+ names = {Path(row['file']).name for row in rows} | {expected(row) for row in rows}
+ candidates.sort(key=lambda path: (path.name not in names, str(path)))
+ previous = (prior or {}).get('matches', {}) if (prior or {}).get('root') == str(directory) else {}
+ old_paths = {value.get('source'): value for value in previous.values() if isinstance(value, dict)}
+ matches, rejected = {}, []
+ for path in candidates:
+ current = fingerprint(path)
+ options = [row for row in by_size.get(current['bytes'], []) if key(row) not in matches]
+ if not options:
+ continue
+ # One file can match multiple identical configs. Hash once per algorithm.
+ computed = {}
+ old = old_paths.get(str(path), {})
+ for row in options:
+ algorithm = 'sha256' if row.get('sha256') else 'git_blob'
+ if algorithm not in computed:
+ reliable = ('change_time_ns' not in current or current['change_time_ns'] is not None)
+ settled = max(current['mtime_ns'], current.get('change_time_ns') or current['ctime_ns']) < time.time_ns()-10**9
+ if (reliable and settled and old.get('fingerprint') == current
+ and old.get('digest') == expected(row) and old.get('algorithm') == algorithm):
+ computed[algorithm] = (old['digest'], current)
+ else:
+ computed[algorithm] = hash_source(path, row, progress)
+ digest, stamp = computed[algorithm]
+ if digest == expected(row):
+ matches[key(row)] = dict(source=str(path), fingerprint=stamp, digest=digest,
+ algorithm=algorithm, bytes=row['bytes'])
+ if not any(record['source'] == str(path) for record in matches.values()):
+ rejected.append(dict(file=path.name, reason='Content does not match this engine revision'))
+ progress.send('ready', done=len(rows), total=len(rows), force=True)
+ return dict(root=str(directory), matches=matches, candidate_files=len(candidates),
+ rejected_count=len(rejected), rejected=rejected[:20],
+ reused_bytes=sum(record['bytes'] for record in matches.values()))
+
+
+def import_file(row, target, record, *, callback=None):
+ """Create an installation-owned reference/copy; never modify the source."""
+ source, target = Path(record['source']), Path(target)
+ if record.get('digest') != expected(row) or record.get('bytes') != row['bytes']:
+ raise ValueError('Local model selection no longer matches the pinned manifest')
+ progress = Progress(callback)
+ current = fingerprint(source)
+ if current != record.get('fingerprint'):
+ digest, current = hash_source(source, row, progress)
+ if digest != expected(row):
+ raise ValueError('Selected local model changed; inspect the folder again: ' + str(source))
+ if target.exists() or target.is_symlink():
+ raise ValueError('Model destination already exists; both files retained: ' + str(target))
+ target.parent.mkdir(parents=True, exist_ok=True)
+ temporary = target.with_name(target.name + '.local-import-' + str(time.time_ns()))
+ method = record['method']
+ if method == 'hardlink':
+ try:
+ os.link(source, temporary)
+ except OSError as error:
+ raise OSError('Cannot link the selected model on this filesystem. Enable "Copy existing models" and inspect the plan again; source retained.') from error
+ # Link creation changes ctime/link count, but must not change bytes or
+ # mtime. A later setup verification still checks the imported file.
+ linked = fingerprint(temporary)
+ if any(linked[field] != current[field] for field in ('device', 'inode', 'bytes', 'mtime_ns')):
+ raise ValueError('Local model changed while linking; both files retained')
+ digest, _ = hash_source(temporary, row, progress)
+ if digest != expected(row):
+ raise ValueError('Local linked model failed verification; source and reference retained')
+ progress.send('link', source, row['bytes'], row['bytes'], force=True)
+ elif method == 'copy':
+ from .download_cache import DownloadCache
+ with DownloadCache(lambda: [temporary]):
+ with temporary.open('xb') as output:
+ digest, _ = hash_source(source, row, progress, output)
+ output.flush()
+ os.fsync(output.fileno())
+ if digest != expected(row):
+ raise ValueError('Local copy failed integrity verification; source and partial copy retained')
+ else:
+ raise ValueError('Unknown local model import method')
+ # On Windows rename refuses an existing destination. On Linux the setup
+ # lease excludes another engine installer; never intentionally overwrite.
+ if target.exists() or target.is_symlink():
+ raise ValueError('Model destination appeared during import; files retained')
+ from .file_ops import publish
+ publish(temporary, target)
+ return method
diff --git a/freevideo_engine/locking.py b/freevideo_engine/locking.py
new file mode 100644
index 0000000..9110dd8
--- /dev/null
+++ b/freevideo_engine/locking.py
@@ -0,0 +1,106 @@
+"""Exclude setup, tests and GPU work; children retain their parent's lease.
+
+GPU work is device-scoped, so it takes the machine lease shared and its own
+device lease exclusively. Setup still takes the machine lease exclusively and
+therefore still excludes every device. Without this split one measurement
+occupied an entire multi-GPU host.
+"""
+from contextlib import contextmanager
+import os
+from pathlib import Path
+import re
+import sys
+
+
+from .paths import data_root
+from .system import windows
+from .processes import inherited_fd
+LOCK_PATH = Path(os.environ.get('FREEVIDEO_LOCK_PATH', data_root() / 'engine.lock'))
+LOCK_ENV = 'FREEVIDEO_RUNTIME_LOCK_FD'
+
+
+def lock_holders(paths):
+ """Read-only Linux diagnostics, including orphans retaining inherited FDs."""
+ if windows() or not Path('/proc/self/fd').is_dir():
+ return []
+ identities = set()
+ for path in paths:
+ try:
+ item = Path(path).stat()
+ identities.add((item.st_dev, item.st_ino))
+ except OSError:
+ pass
+ holders = []
+ for directory in Path('/proc').iterdir():
+ if not directory.name.isdigit():
+ continue
+ try:
+ for fd in (directory / 'fd').iterdir():
+ try:
+ item = fd.stat()
+ if (item.st_dev, item.st_ino) in identities:
+ holders.append(int(directory.name))
+ break
+ except OSError:
+ continue
+ except OSError:
+ continue
+ return sorted(holders)
+
+
+def device_lock_path(device, path=None):
+ """The lease for one device, beside the machine lease it shares a root with."""
+ base = Path(path or os.environ.get('FREEVIDEO_LOCK_PATH', data_root() / 'engine.lock'))
+ token = re.sub(r'[^A-Za-z0-9-]', '_', str(device))[:64] or 'unknown'
+ return base.with_name(base.stem + '-gpu-' + token + base.suffix)
+
+
+@contextmanager
+def runtime_lock(path=None, *, inherit=True, shared=False):
+ path = Path(path or os.environ.get('FREEVIDEO_LOCK_PATH', data_root() / 'engine.lock'))
+ path.parent.mkdir(parents=True, exist_ok=True)
+ inherited = inherited_fd() if inherit else None
+ if inherited is None:
+ stream = path.open('a+b')
+ else:
+ descriptor = int(inherited)
+ actual, expected = os.fstat(descriptor), path.stat()
+ if (actual.st_dev, actual.st_ino) != (expected.st_dev, expected.st_ino):
+ raise ValueError('Inherited runtime lock descriptor does not match the GPU-work lock')
+ stream = os.fdopen(os.dup(descriptor), 'a+b')
+ try:
+ # An inherited descriptor shares the parent's open-file description.
+ # Close our descriptor on exit; do not unlock the parent's live work.
+ if windows():
+ if inherited is None:
+ import msvcrt
+ if path.stat().st_size == 0:
+ stream.write(b'\0')
+ stream.flush()
+ stream.seek(0)
+ try:
+ msvcrt.locking(stream.fileno(),
+ msvcrt.LK_NBRLCK if shared else msvcrt.LK_NBLCK, 1)
+ except OSError as error:
+ raise BlockingIOError('Another process holds the installation lease') from error
+ # The parent holds the byte-range lock; its Job Object owns this
+ # worker tree. Closing a duplicated child handle must not unlock it.
+ else:
+ import fcntl
+ fcntl.flock(stream, (fcntl.LOCK_SH if shared else fcntl.LOCK_EX) | fcntl.LOCK_NB)
+ yield stream.fileno()
+ finally:
+ # Releasing the lease must not discard completed work. Taking it still
+ # fails loudly, but once the request has run, a close that fails has
+ # nothing left to protect: the kernel drops the descriptor when this
+ # process exits, and the lease is advisory, so the next holder waits on
+ # the file rather than on this handle. Observed once in 120 concurrent
+ # eight-GPU measurements on a WekaFS share, as OSError EBADFD raised by
+ # the close itself after a full video had been sampled, decoded and
+ # written; the exit code threw that video away.
+ try:
+ stream.close()
+ except OSError as error:
+ print('Could not release the runtime lease at %s: %s. The request is '
+ 'unaffected; the descriptor is dropped when this process exits.'
+ % (path, error), file=sys.stderr, flush=True)
diff --git a/freevideo_engine/lora_cache.py b/freevideo_engine/lora_cache.py
new file mode 100644
index 0000000..f5a970c
--- /dev/null
+++ b/freevideo_engine/lora_cache.py
@@ -0,0 +1,343 @@
+"""Prepare immutable FP8 adapter variants without per-NFE LoRA matmuls.
+
+Adapters modify the *prepared FP8 model*: dequantize affected matrices, add
+FP32 B@A deltas in the input weight dtype, and requantize with the same scale
+granularity. This is not a claim of equivalence to merging a BF16 checkpoint.
+Unchanged files are hardlinked. Original H3 QKV splitting and SwiGLU half order
+follow OpenVDN src/inference/utils/lora.py; foreign architectures fail closed.
+"""
+import argparse
+import gc
+import hashlib
+import json
+import math
+import os
+from pathlib import Path
+import re
+import shutil
+import time
+
+from safetensors.torch import save_file
+import torch
+
+from .tensor_io import open_tensors
+from .media_request import digest, verify_files
+from .monitoring import save
+from .storage import fingerprint
+
+
+def _targets(name):
+ prefixes = ('base_model.model.', 'diffusion_model.', 'model.diffusion_model.', 'transformer.')
+ while any(name.startswith(prefix) for prefix in prefixes):
+ prefix = next(prefix for prefix in prefixes if name.startswith(prefix))
+ name = name[len(prefix):]
+ name = re.sub(r'^blocks\.', 'transformer_blocks.', name)
+ name = name.replace('token_refiner.blocks.', 'token_refiner.refiner_blocks.')
+ name = name.replace('final_layer.adaln_proj.linear', 'norm_out.linear')
+ # Original H3 fc1 uses [gate; value], including token-refiner MLPs.
+ # Diffusers adapters already name ff.net.0.proj and need no swap.
+ swap = '.mlp.fc1' in name
+ for before, after in (('.mlp.fc1', '.ff.net.0.proj'), ('.mlp.fc2', '.ff.net.2'), ('.attn.out_proj', '.attn.to_out.0')):
+ name = name.replace(before, after)
+ if '.attn.qkv_proj' in name:
+ return [(name.replace('.attn.qkv_proj', '.attn.' + suffix), i, False) for i, suffix in enumerate(('to_q', 'to_k', 'to_v'))]
+ return [(name, None, swap)]
+
+
+def index_adapter(row, targets):
+ pairs = {}
+ full = []
+ with open_tensors(row['path']) as source:
+ metadata = source.metadata() or {}
+ keys = source.keys()
+ for key in keys:
+ canonical = key.replace('.default.', '.').replace('.lora_down.weight', '.lora_A.weight').replace('.lora_up.weight', '.lora_B.weight')
+ match = re.fullmatch(r'(.+)\.lora_([AB])\.weight', canonical)
+ if match:
+ pair = pairs.setdefault(match[1], {})
+ if match[2] in pair:
+ raise ValueError('Duplicate LoRA factor: ' + key)
+ pair[match[2]] = key
+ elif canonical.endswith('.alpha'):
+ pairs.setdefault(canonical[:-6], {})['alpha'] = key
+ elif canonical.endswith(('.diff', '.diff_b')):
+ bias = canonical.endswith('.diff_b')
+ full.append((canonical[:-7] if bias else canonical[:-5], key, bias))
+ else:
+ raise ValueError('Unsupported LoRA tensor (no patches are ignored): ' + key)
+ if not pairs and not full:
+ raise ValueError('Adapter contains no LoRA factors')
+ global_alpha = row.get('alpha') is not None or 'alpha' in metadata
+ if global_alpha:
+ ranks = {source.get_slice(pair['A']).get_shape()[0] for pair in pairs.values() if 'A' in pair
+ and (row.get('alpha') is not None or 'alpha' not in pair)}
+ if len(ranks) > 1:
+ raise ValueError('A global alpha cannot identify mixed-rank H3 scaling; provide per-target alpha tensors')
+ results = []
+ for name, pair in pairs.items():
+ if not {'A', 'B'} <= pair.keys():
+ raise ValueError('Incomplete LoRA pair: ' + name)
+ ashape, bshape = (source.get_slice(pair[k]).get_shape() for k in ('A', 'B'))
+ if len(ashape) != 2 or len(bshape) != 2 or ashape[0] != bshape[1] or ashape[0] <= 0:
+ raise ValueError('Expected matching 2-D LoRA factors: ' + name)
+ alpha = row.get('alpha')
+ if alpha is None:
+ alpha = source.get_tensor(pair['alpha']).item() if 'alpha' in pair else float(metadata.get('alpha', ashape[0]))
+ if not math.isfinite(alpha):
+ raise ValueError('Nonfinite LoRA alpha')
+ for target, split, swap in _targets(name):
+ if target not in targets and '.attn.' in target:
+ target = target.replace('.attn.', '.attn.orig.', 1)
+ expected = targets.get(target)
+ rows = bshape[0] if split is None else bshape[0] // 3
+ if (expected is None or (split is not None and bshape[0] % 3)
+ or (swap and rows % 2) or expected['shape'] != [rows, ashape[1]]):
+ raise ValueError('LoRA target/shape incompatible with this VDN model (including pruned AdaLN): ' + target)
+ results.append(dict(pair, target=target, split=split, swap=swap,
+ scale=float(row['strength']) * alpha / ashape[0], file=row['path']))
+ for name, key, bias in full:
+ shape = source.get_slice(key).get_shape()
+ for target, split, swap in _targets(name):
+ target += '#bias' if bias else ''
+ if target not in targets and '.attn.' in target:
+ target = target.replace('.attn.', '.attn.orig.', 1)
+ expected = targets.get(target)
+ actual = list(shape)
+ if split is not None:
+ if not actual or actual[0] % 3:
+ raise ValueError('Fused QKV difference must have three equal row groups')
+ actual[0] //= 3
+ if not expected or actual != expected['shape'] or (swap and (not actual or actual[0] % 2)):
+ raise ValueError('Full-difference LoRA target/shape incompatible with this VDN model: ' + target)
+ results.append(dict(full=key, target=target, split=split, swap=swap,
+ scale=float(row['strength']), file=row['path']))
+ if len({item['target'] for item in results}) != len(results):
+ raise ValueError('Adapter has overlapping patches for the same target; merge intent is ambiguous')
+ return results
+
+
+def _merge(weight, patches):
+ for patch in patches:
+ if patch['scale'] == 0:
+ continue
+ with open_tensors(patch['file']) as source:
+ if 'full' in patch:
+ value = source.get_tensor(patch['full'])
+ if patch['split'] is not None:
+ value = value.chunk(3, dim=0)[patch['split']]
+ if patch['swap']:
+ value = torch.cat(value.chunk(2, dim=0)[::-1], dim=0)
+ if not bool(torch.isfinite(value).all()):
+ raise ValueError('LoRA contains a nonfinite full difference')
+ # Native Comfy diff/diff_b convention: strength * delta in the
+ # destination dtype. Alpha scales low-rank factors only.
+ weight.add_(value.to(weight.dtype) * patch['scale'])
+ del value
+ continue
+ a, b = (source.get_tensor(patch[k]).float() for k in ('A', 'B'))
+ if patch['split'] is not None:
+ b = b.chunk(3, dim=0)[patch['split']]
+ if patch['swap']:
+ b = torch.cat(b.chunk(2, dim=0)[::-1], dim=0)
+ if not bool(torch.isfinite(a).all() and torch.isfinite(b).all()):
+ raise ValueError('LoRA contains nonfinite factors')
+ # A complete delta matrix can be hundreds of MiB. Bound it to rows.
+ for begin in range(0, weight.shape[0], 128):
+ delta = (b[begin:begin + 128] @ a) * patch['scale']
+ weight[begin:begin + 128].add_(delta.to(weight.dtype))
+ del delta
+ del a, b
+ if not bool(torch.isfinite(weight).all()):
+ raise ValueError('LoRA merge overflowed the model weight dtype')
+ return weight
+
+
+def _quantize(weight, rowwise):
+ # Fp8Linear's weight convention, including the division in weight dtype.
+ scale = (weight.abs().amax(dim=1, keepdim=True).float() if rowwise else weight.abs().amax().float())
+ scale = (scale / 448.).clamp_min(1e-12)
+ result = (weight / scale.to(weight.dtype)).to(torch.float8_e4m3fn)
+ if not bool(torch.isfinite(result.float()).all()):
+ raise ValueError('LoRA FP8 conversion produced nonfinite weights')
+ return result, scale.reshape(1, -1).contiguous()
+
+
+def prepare(cache, adapters, output_root=None):
+ cache = Path(cache).resolve()
+ adapters = [row for row in adapters if row['strength'] != 0]
+ if not adapters:
+ return cache, {'enabled': False, 'reason': 'No nonzero adapters; original FP8 bytes unchanged'}
+ verify_files({'loras': adapters})
+ manifest_path = cache / 'manifest.json'
+ manifest = json.loads(manifest_path.read_text(encoding='utf-8'))
+ if manifest.get('precision') != 'fp8':
+ raise ValueError('LoRA variants require the prepared FP8 model')
+ from .adaln_assets import SLIM_FORMAT, restore_projections
+ from .export_slim import embedding_hash, share
+ changes_modulation = False
+ changes_embedding = False
+ for adapter in adapters:
+ with open_tensors(adapter['path']) as stream:
+ for name in stream.keys():
+ for target, _, _ in _targets(name):
+ changes_embedding |= target.startswith(('time_embedder.', 'time_proj.'))
+ changes_modulation |= bool(re.match(r'transformer_blocks\.\d+\.adaln_proj\.', target))
+ identity = {'source_manifest_sha256': digest(manifest_path), 'implementation_sha256': digest(__file__),
+ 'torch': str(torch.__version__), 'adapters': [{k: v for k, v in row.items() if k != 'path'} for row in adapters],
+ 'arithmetic': 'prepared FP8 dequantization; FP32 delta, weight-dtype merge; original FP8 granularity'}
+ key = hashlib.sha256(json.dumps(identity, sort_keys=True).encode()).hexdigest()
+ output = Path(output_root or cache.parent) / ('vdn-fp8-lora-' + key[:24])
+ marker = output / 'manifest.json'
+ if marker.is_file():
+ saved = json.loads(marker.read_text(encoding='utf-8'))
+ if saved.get('lora_identity') != identity:
+ raise ValueError('Adapter cache identity mismatch')
+ for group in saved['groups']:
+ path = output / group['file']
+ stamp = saved['lora_file_stamps'].get(group['file'])
+ current = fingerprint(path)
+ reliable = current.get('change_time_ns', current['ctime_ns']) not in (None, current['mtime_ns'])
+ if (stamp != current or not reliable) and digest(path) != group['sha256']:
+ raise ValueError('Adapter cache changed: ' + str(path))
+ return output, {'enabled': True, 'cache_hit': True, 'cache': str(output), 'identity': identity}
+ if manifest.get('format') == SLIM_FORMAT and (changes_modulation or changes_embedding):
+ # Ordinary attention/FF LoRAs keep the fixed tables. Only adapters that
+ # actually change modulation need the optional original projection data.
+ originals = restore_projections(cache, manifest)
+ manifest = dict(manifest, groups=list(manifest['groups']) + originals)
+ manifest.pop('format', None)
+ manifest.pop('adaln_tables', None)
+ elif changes_modulation or changes_embedding:
+ manifest = dict(manifest)
+ manifest.pop('adaln_tables', None)
+ # Header-only indexing; no full model state dict or persistent mmap.
+ targets = {}
+ for group in manifest['groups']:
+ if group['file'] != group['group'] + '.safetensors' or Path(group['file']).is_absolute() or '..' in Path(group['file']).parts:
+ raise ValueError('Invalid prepared group path')
+ with open_tensors(cache / group['file']) as stream:
+ for name in stream.keys():
+ if name.endswith('.weight'):
+ shape = stream.get_slice(name).get_shape()
+ targets[name[:-7]] = {'shape': shape, 'group': group['file'], 'key': name}
+ elif name.endswith('.bias'):
+ target = name[:-14] if name.endswith('.original.bias') else name[:-5]
+ targets[target + '#bias'] = {'shape': stream.get_slice(name).get_shape(), 'group': group['file'], 'key': name}
+ for name, spec in manifest['linears'].items():
+ group = next((g for g in manifest['groups'] if name in g.get('linears', {})), None)
+ if group is None:
+ # Older official export receipts store the linear index globally.
+ from .weights import group_name
+ file = group_name(name + '.weight') + '.safetensors'
+ else:
+ file = group['file']
+ targets[name] = {'shape': spec['weight_shape'], 'group': file, 'key': name + '.weight_fp8'}
+ patches = {}
+ for adapter in adapters:
+ for patch in index_adapter(adapter, targets):
+ patches.setdefault(targets[patch['target']]['group'], []).append(patch)
+ output.mkdir(parents=True, exist_ok=True)
+ required = sum(row['bytes'] for row in manifest['groups'] if row['file'] in patches)
+ if shutil.disk_usage(output).free < required + 1024**3:
+ raise ValueError('Insufficient disk for the separate LoRA variant; original model retained')
+ records = []
+ started = time.perf_counter()
+ for index, group in enumerate(manifest['groups']):
+ path, destination = cache / group['file'], output / group['file']
+ destination.parent.mkdir(parents=True, exist_ok=True)
+ progress = destination.with_suffix('.lora.json')
+ if destination.is_file() and progress.is_file():
+ record = json.loads(progress.read_text(encoding='utf-8'))
+ if destination.stat().st_size == record['bytes'] and digest(destination) == record['sha256']:
+ records.append(record)
+ continue
+ raise ValueError('Interrupted adapter group changed; files retained: ' + str(destination))
+ if destination.exists():
+ destination.rename(destination.with_name(destination.name + '.orphaned-' + str(time.time_ns())))
+ if path.stat().st_size != group['bytes']:
+ raise ValueError('Prepared source group is incomplete')
+ if group['file'] not in patches:
+ try:
+ os.link(path, destination)
+ except OSError as error:
+ raise RuntimeError('Adapter variants require hardlinks on the model volume; no unbudgeted full-model copy is made') from error
+ record = dict(group)
+ else:
+ if digest(path) != group['sha256']:
+ raise ValueError('Prepared source group failed hash verification')
+ values = {}
+ by_target = {}
+ for patch in patches[group['file']]:
+ by_target.setdefault(patch['target'], []).append(patch)
+ with open_tensors(path) as stream:
+ for name in stream.keys():
+ if name.endswith('.weight_scale') and name[:-13] in by_target:
+ continue
+ target = name[:-11] if name.endswith('.weight_fp8') else name[:-7] if name.endswith('.weight') else None
+ if name.endswith('.bias'):
+ target = (name[:-14] if name.endswith('.original.bias') else name[:-5]) + '#bias'
+ value = stream.get_tensor(name)
+ if target in by_target:
+ if name.endswith('.weight_fp8'):
+ spec = manifest['linears'][target]
+ scale = stream.get_tensor(target + '.weight_scale')
+ weight = (value.float() * scale.reshape(-1, 1)).to(getattr(torch, spec.get('input_dtype', 'bfloat16')))
+ del value, scale
+ weight = _merge(weight, by_target[target])
+ value, scale = _quantize(weight, manifest['scale_granularity'] == 'rowwise')
+ values[target + '.weight_scale'] = scale
+ del weight
+ else:
+ value = _merge(value.clone(), by_target[target])
+ values[name] = value
+ del value
+ temporary = destination.with_suffix('.partial')
+ if temporary.exists():
+ temporary.rename(temporary.with_name(temporary.name + '.retained-' + str(time.time_ns())))
+ save_file(values, temporary)
+ del values
+ gc.collect()
+ temporary.replace(destination)
+ record = dict(group, bytes=destination.stat().st_size, sha256=digest(destination))
+ save(progress, record)
+ records.append(record)
+ print(json.dumps({'event': 'lora_prepare', 'done': index + 1, 'total': len(manifest['groups']),
+ 'group': group['group'], 'modified': group['file'] in patches}), flush=True)
+ verify_files({'loras': adapters})
+ stamps = {}
+ for record in records:
+ stamps[record['file']] = fingerprint(output / record['file'])
+ if manifest.get('adaln_tables'):
+ for table in manifest['adaln_tables']:
+ for row in table['files']:
+ share(cache / row['file'], output / row['file'], row['sha256'])
+ if manifest.get('adaln_sources'):
+ manifest = dict(manifest, adaln_sources=dict(manifest['adaln_sources']))
+ if changes_modulation:
+ manifest['adaln_sources']['groups'] = [r for r in records if r['group'].startswith('adaln/')]
+ if changes_embedding:
+ manifest['adaln_sources']['embedding_sha256'] = embedding_hash(output / 'root.safetensors')
+ # Modified projection files are local variant data, never recover them
+ # from the unmodified upstream's optional-download revision.
+ if changes_embedding or changes_modulation:
+ manifest['adaln_sources'].pop('download', None)
+ save(marker, dict(manifest, source_id=key, groups=records, lora_identity=identity, lora_file_stamps=stamps,
+ total_bytes=sum(row['bytes'] for row in records) +
+ sum(row['bytes'] for table in manifest.get('adaln_tables', []) for row in table['files'])))
+ return output, {'enabled': True, 'cache_hit': False, 'cache': str(output), 'identity': identity,
+ 'modified_groups': sorted(patches), 'prepare_seconds': time.perf_counter() - started}
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--request', required=True, type=Path)
+ args = parser.parse_args()
+ request = json.loads(args.request.read_text(encoding='utf-8'))
+ torch.set_num_threads(8)
+ output, report = prepare(request['cache'], request['adapters'])
+ save(request['result'], {'cache': str(output), 'report': report})
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freevideo_engine/managed.py b/freevideo_engine/managed.py
new file mode 100644
index 0000000..4a8af78
--- /dev/null
+++ b/freevideo_engine/managed.py
@@ -0,0 +1,128 @@
+"""Use the setup environment and model paths without shell activation."""
+import argparse
+from contextlib import nullcontext
+import json
+import os
+from pathlib import Path
+import sys
+
+from .bootstrap import DEFAULT_ROOT
+from . import __version__
+from .locking import runtime_lock, LOCK_ENV
+from . import processes
+from .system import windows
+
+
+def command_for(machine, command, arguments):
+ defaults = {}
+ if command in ('generate', 'bench', 'predict', 'calibrate'):
+ defaults.update(cache=machine['cache'])
+ if command in ('generate', 'predict'):
+ defaults.update(base=machine['base'], checkpoint=machine['checkpoint'])
+ if command in ('generate', 'encode', 'calibrate'):
+ defaults.update(encoder_python=machine['comfy_python'], encoder_root=machine['comfy_root'],
+ model_paths=machine['model_paths'], encoder=machine['encoder'])
+ if command in ('generate', 'encode', 'plan', 'bench', 'predict'):
+ defaults.update(vram_gib=machine.get('vram_gib'), ram_gib=machine.get('ram_gib'))
+ given = {argument.split('=', 1)[0] for argument in arguments if argument.startswith('--')}
+ # Saved profiles already include budgets; explicit command arguments win.
+ if '--profile' in given:
+ defaults.pop('vram_gib', None)
+ defaults.pop('ram_gib', None)
+ aliases = {'encoder_python': '--comfy-python', 'encoder_root': '--comfy-root'}
+ extra = []
+ for key, value in defaults.items():
+ flag = '--' + key.replace('_', '-')
+ if value is not None and flag not in given and aliases.get(key) not in given:
+ extra += [flag, str(value)]
+ return [machine['python'], '-m', 'freevideo_engine', command, *extra, *arguments]
+
+
+def _main(argv=None):
+ argv = list(sys.argv[1:] if argv is None else argv)
+ parser = argparse.ArgumentParser(prog='./freevideo', add_help=False)
+ parser.add_argument('--root', type=Path, default=Path(os.environ.get('FREEVIDEO_HOME', DEFAULT_ROOT)))
+ parser.add_argument('--config', type=Path)
+ args, rest = parser.parse_known_args(argv)
+ if not rest or rest[0] in ('-h', '--help'):
+ print('FreeVideo Engine ' + __version__ + '\n\nUsage: ./freevideo [--root PATH] COMMAND [OPTIONS]\n\n'
+ ' setup Detect hardware, review and install from scratch\n'
+ ' test Run full video/audio tests with a live dashboard\n'
+ ' optimize Reuse test results and validate lightweight local tuning\n'
+ ' generate Generate from --prompt-file or --conditioning\n'
+ ' encode Save reusable text conditioning\n'
+ ' plan Show current inference resource policy\n'
+ ' predict Forecast time and memory for another resolution or length\n'
+ ' resource-history Inspect attempts and acknowledged retries\n'
+ ' doctor Check dependencies and GPU kernels\n'
+ ' diagnose Collect a small local report, including failed installs\n'
+ ' bench Compare attention backends\n\n'
+ 'Examples:\n ./freevideo setup --plan\n ./freevideo test --suite quick\n'
+ ' ./freevideo generate --prompt-file prompt.txt --out video.mp4\n\n'
+ 'Use COMMAND --help for options. Windows: .\\setup.ps1 / .\\test.ps1 / .\\freevideo.ps1; Linux: ./setup.sh / ./test.sh / ./freevideo.')
+ return 0
+ command, tail = rest[0], rest[1:]
+ if command == 'setup':
+ from .bootstrap import main as setup
+ return setup(['--root', str(args.root), *tail])
+ if command == 'diagnose':
+ from .diagnostics import main as diagnose
+ return diagnose(['--root', str(args.root), *(['--config', str(args.config)] if args.config else []), *tail])
+ if command == 'resource-history':
+ from .resource_cli import main as history
+ if not any(flag.split('=', 1)[0] == '--history' for flag in tail):
+ tail += ['--history', str(args.root / 'resource-history.sqlite3')]
+ return history(tail)
+ if command not in ('test', 'optimize', 'generate', 'encode', 'plan', 'doctor', 'bench',
+ 'prepare', 'predict', 'calibrate'):
+ parser.error('Unknown command: ' + command)
+ if command == 'test' and any(flag in tail for flag in ('-h', '--help')):
+ from .testing import main as testing
+ return testing(['--help'])
+ if command == 'optimize' and any(flag in tail for flag in ('-h', '--help')):
+ from .optimize import main as optimize
+ return optimize(['--help'])
+ config = (args.config or args.root / 'machine.json').expanduser().resolve()
+ if not config.is_file():
+ parser.error('Run ./setup.sh first or select the installation with --root (missing %s).' % config)
+ # Acquire before reading readiness: setup cannot replace dependencies between
+ # this check and exec. Keep the lease through every encoder/video child.
+ readonly = (command in ('plan', 'predict') or (command in ('test', 'optimize') and '--plan' in tail)
+ or (command == 'doctor' and '--probe' not in tail) or '--help' in tail or '-h' in tail)
+ snapshot = json.loads(config.read_text(encoding='utf-8'))
+ lock_path = os.environ.get('FREEVIDEO_LOCK_PATH', str(Path(snapshot['root']) / 'engine.lock'))
+ with nullcontext() if readonly else runtime_lock(lock_path) as descriptor:
+ machine = json.loads(config.read_text(encoding='utf-8'))
+ if machine != snapshot:
+ raise ValueError('Installation changed while starting. Rerun the command.')
+ if not machine.get('ready'):
+ parser.error('Setup is incomplete. Rerun ./setup.sh with the same --root; saved files are reused.')
+ from .testing import environment
+ env = environment(machine)
+ if descriptor is not None:
+ os.set_inheritable(descriptor, True)
+ env[LOCK_ENV] = str(descriptor)
+ env['FREEVIDEO_LOCK_PATH'] = lock_path
+ executable = ([machine['python'], '-m', 'freevideo_engine.' + ('testing' if command == 'test' else 'optimize'), '--config', str(config), *tail]
+ if command in ('test', 'optimize') else command_for(machine, command, tail))
+ if windows():
+ return processes.run(executable, env=env, pass_fds=() if descriptor is None else (descriptor,)).returncode
+ os.execve(machine['python'], executable, env)
+
+
+def main(argv=None):
+ try:
+ return _main(argv)
+ except BlockingIOError:
+ print('Setup, testing or generation is already using this installation/GPU lock. Retry after it finishes.', file=sys.stderr)
+ return 1
+ except KeyboardInterrupt:
+ print('Cancelled. Workers stopped; existing outputs and reports retained.', file=sys.stderr)
+ return 130
+ except (OSError, ValueError, KeyError) as error:
+ print('Engine could not start: ' + str(error), file=sys.stderr)
+ return 1
+
+
+if __name__ == '__main__':
+ raise SystemExit(main())
diff --git a/freevideo_engine/media.py b/freevideo_engine/media.py
new file mode 100644
index 0000000..272a294
--- /dev/null
+++ b/freevideo_engine/media.py
@@ -0,0 +1,96 @@
+"""Decode real benchmark media natively with PyAV; no browser or Docker required."""
+import argparse
+import hashlib
+import json
+from pathlib import Path
+
+import av
+import numpy as np
+from PIL import Image
+
+
+def inspect(path, *, width=1344, height=768, frames=243, fps=24):
+ selected = {}
+ indices = tuple(round((frames - 1) * i / 3) for i in range(4))
+ thumb_height = round(448 * height / width)
+ count = 0
+ with av.open(str(path)) as container:
+ stream = container.streams.video[0]
+ geometry = (stream.width, stream.height, str(stream.average_rate))
+ video_info = {'width': stream.width, 'height': stream.height, 'avg_frame_rate': str(stream.average_rate),
+ 'start_time': float((stream.start_time or 0) * stream.time_base),
+ 'duration': float(stream.duration * stream.time_base), 'codec': stream.codec_context.name}
+ stream.codec_context.thread_count = 2
+ for frame in container.decode(stream):
+ if count in indices:
+ selected[count] = frame.to_image().resize((448, thumb_height))
+ count += 1
+ if geometry != (width, height, str(fps)) or count != frames:
+ raise ValueError(f'Unexpected actual video geometry: {geometry}, {count} frames')
+ samples, squares, peak = 0, 0., 0.
+ with av.open(str(path)) as container:
+ stream = container.streams.audio[0]
+ audio_info = {'sample_rate': stream.rate, 'channels': stream.codec_context.channels,
+ 'start_time': float((stream.start_time or 0) * stream.time_base),
+ 'duration': float(stream.duration * stream.time_base), 'codec': stream.codec_context.name}
+ for frame in container.decode(stream):
+ values = frame.to_ndarray()
+ if np.issubdtype(values.dtype, np.integer):
+ values = values.astype(np.float64) / (np.iinfo(values.dtype).max + 1)
+ else:
+ values = values.astype(np.float64)
+ if not np.isfinite(values).all():
+ raise ValueError('Non-finite decoded audio')
+ samples += values.size
+ squares += np.square(values).sum().item()
+ peak = max(peak, np.abs(values).max().item())
+ rms = (squares / samples) ** .5 if samples else 0.
+ if rms <= 1e-8:
+ raise ValueError('Decoded audio is silent or missing')
+ offset = audio_info['start_time'] - video_info['start_time']
+ duration_delta = audio_info['duration'] - video_info['duration']
+ # Allow up to one video frame or two AAC packets for container padding.
+ # This checks timestamps, not perceptual alignment of sound and action.
+ tolerance = max(1 / fps, 2048 / audio_info['sample_rate'])
+ if abs(offset) > tolerance or abs(duration_delta) > tolerance:
+ raise ValueError('Audio/video timestamps differ beyond codec tolerance: offset=%s, duration_delta=%s' % (offset, duration_delta))
+ sheet = Image.new('RGB', (448 * 4, thumb_height))
+ for column, index in enumerate(indices):
+ sheet.paste(selected[index], (column * 448, 0))
+ contact = path.with_name(path.stem + '-contact.jpg')
+ sheet.save(contact, quality=92)
+ selected[0].save(path.with_suffix('.poster.jpg'), quality=90)
+ return {'id': path.stem, 'file': str(path), 'sha256': hashlib.sha256(path.read_bytes()).hexdigest(),
+ 'video': dict(video_info, nb_read_frames=count), 'audio': audio_info,
+ 'audio_rms': rms, 'audio_peak': peak, 'contact_sheet': str(contact),
+ 'audio_video_start_offset_seconds': offset,
+ 'audio_minus_video_duration_seconds': duration_delta,
+ 'timestamp_tolerance_seconds': tolerance, 'timestamp_alignment_pass': True,
+ 'geometry_frames_fps_pass': True,
+ 'quality_scope': 'Every frame decoded, non-silent finite audio, timestamps and sampled frames; not a perceptual or semantic sync score'}
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('directory', type=Path)
+ parser.add_argument('--ids', nargs='+', default=['city_surfing', 'dawn', 'anime_romance'])
+ parser.add_argument('--report', default='media-validation-native.json')
+ parser.add_argument('--refresh', action='store_true')
+ args = parser.parse_args()
+ report = args.directory / args.report
+ previous = json.loads(report.read_text(encoding='utf-8')) if report.is_file() else {}
+ records = {r['id']: r for r in previous.get('videos', [])}
+ for name in args.ids:
+ path = args.directory / (name + '.mp4')
+ cached = records.get(name)
+ if (not args.refresh and cached and previous.get('decoder') == 'PyAV ' + av.__version__
+ and cached['sha256'] == hashlib.sha256(path.read_bytes()).hexdigest()
+ and Path(cached['contact_sheet']).is_file() and path.with_suffix('.poster.jpg').is_file()):
+ continue
+ records[name] = inspect(path)
+ report.write_text(json.dumps({'decoder': 'PyAV ' + av.__version__, 'videos': list(records.values())}, indent=2) + '\n', encoding='utf-8')
+ print(json.dumps([{'id': r['id'], 'frames': r['video']['nb_read_frames'], 'audio_rms': r['audio_rms']} for r in records.values()]))
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freevideo_engine/media_conditioning.py b/freevideo_engine/media_conditioning.py
new file mode 100644
index 0000000..341f215
--- /dev/null
+++ b/freevideo_engine/media_conditioning.py
@@ -0,0 +1,97 @@
+"""Explicit adapter for native ComfyUI H3 conditioning and reference latents.
+
+No generic CLIP embeddings or MODEL patches can masquerade as H3 inputs. The
+native model's layout metadata is retained and checked before CUDA loading.
+"""
+import math
+import torch
+from .conditioning import to_cache
+from .keyframes import validate_conditioning
+
+
+def _tensor(value, shape, name):
+ if not isinstance(value, torch.Tensor) or value.ndim != len(shape):
+ raise ValueError('Invalid ' + name + ' tensor')
+ if any(expected is not None and value.shape[i] != expected for i, expected in enumerate(shape)):
+ raise ValueError('Invalid ' + name + ' shape: ' + str(tuple(value.shape)))
+ if not value.is_floating_point() or not value.numel() or not bool(torch.isfinite(value).all()):
+ raise ValueError(name + ' must contain finite floating point values')
+ return value.detach().to(device='cpu', dtype=torch.float32).contiguous()
+
+
+def describe(value, width, height, frames):
+ task = value.get('task', 'fl2va' if value.get('keyframe_anchors') else 't2va')
+ prompt, tags = value['prompt_embeds'], value['text_token_tags']
+ refs = value.get('references', [])
+ if not task.startswith('ref2va'):
+ if refs:
+ raise ValueError('Reference latents require task=ref2va')
+ conditions = (value['keyframe_anchors'], value['condition_latents']) if value.get('keyframe_anchors') else None
+ validate_conditioning(prompt, tags, conditions, task, width, height)
+ count = len(value.get('keyframe_anchors', [])) * (height // 32) * (width // 32)
+ return {'task': task, 'text_tokens': len(tags), 'reference_video_tokens': count, 'reference_audio_tokens': 0,
+ 'width': width, 'height': height, 'frames': frames}
+ if value.get('keyframe_anchors') or not isinstance(refs, list) or not 1 <= len(refs) <= 32:
+ raise ValueError('Ref2VA needs reference latents and no keyframe anchors')
+ # Reuse the shared prompt/token validation without pretending references are anchors.
+ to_cache([[prompt[None], {'minimax_token_tags': tags}]], task='ref2va')
+ video_rows = audio_rows = 0
+ for ref in refs:
+ kind = ref.get('kind')
+ if kind not in ('image', 'video', 'audio'):
+ raise ValueError('Unknown reference modality')
+ if kind in ('image', 'video'):
+ latent = _tensor(ref.get('latent'), (1, 24, 1 if kind == 'image' else None, None, None), 'reference video')
+ if latent.shape[2] < 1 or any(n < 2 or n % 2 for n in latent.shape[-2:]):
+ raise ValueError('Reference latent dimensions must fit H3 patches')
+ video_rows += math.prod(latent.shape[2:]) // 4
+ if kind == 'audio' or ref.get('audio_latent') is not None:
+ audio = _tensor(ref.get('audio_latent'), (None, 32), 'reference audio rows')
+ if audio.shape[0] % 2:
+ raise ValueError('Reference audio must contain both channels')
+ audio_rows += audio.shape[0]
+ if video_rows and not bool((tags == 0).any()):
+ raise ValueError('Visual references need jointly encoded vision tokens')
+ task = 'ref2va_av' if video_rows and audio_rows else 'ref2va_audio' if audio_rows else 'ref2va'
+ value['task'] = task
+ return {'task': task, 'text_tokens': len(tags), 'reference_video_tokens': video_rows,
+ 'reference_audio_tokens': audio_rows, 'width': width, 'height': height, 'frames': frames}
+
+
+def from_comfy(conditioning, width, height, frames):
+ if len(conditioning) != 1:
+ raise ValueError('One H3 conditioning sequence is required')
+ extra = conditioning[0][1]
+ # These modify sampling semantics; ignoring them would silently change a workflow.
+ allowed = {'minimax_token_tags', 'minimax_keyframes', 'minimax_refs', 'pooled_output'}
+ unknown = set(extra) - allowed
+ if unknown:
+ raise ValueError('Unsupported conditioning modifiers: ' + ', '.join(sorted(unknown)))
+ keyframes, refs = extra.get('minimax_keyframes', []), extra.get('minimax_refs', [])
+ if keyframes and refs:
+ raise ValueError('Combined keyframe/reference conditioning is not an official VDN layout')
+ anchors = []
+ for item in keyframes:
+ index = item.get('resolved_frame_index')
+ if index not in (0, frames - 1):
+ raise ValueError('Only first and last keyframe anchors are supported')
+ anchors.append('first' if index == 0 else 'last')
+ task = ('ref2va' if refs else 'fl2va' if len(anchors) == 2 else
+ 'i2va' if anchors == ['first'] else 'l2va' if anchors == ['last'] else 't2va')
+ value = to_cache(conditioning, task=task)
+ value.update(task=task, width=width, height=height)
+ if anchors:
+ value.update(keyframe_anchors=anchors, condition_latents=[
+ _tensor(row['latent'], (1, 24, 1, height // 16, width // 16), 'keyframe') for row in keyframes])
+ if refs:
+ value['references'] = []
+ for row in refs:
+ kind = row['kind']
+ ref = {'kind': 'video' if kind == 'video_audio' else kind}
+ if kind in ('image', 'video', 'video_audio'):
+ ref['latent'] = _tensor(row.get('latent'), (1, 24, None, None, None), 'reference video')
+ if kind in ('audio', 'video_audio'):
+ audio = _tensor(row.get('audio_latent'), (1, 32, 2, None), 'native reference audio')
+ ref['audio_latent'] = audio[0].permute(1, 2, 0).reshape(-1, 32).contiguous()
+ value['references'].append(ref)
+ return value, describe(value, width, height, frames)
diff --git a/freevideo_engine/media_encoding.py b/freevideo_engine/media_encoding.py
new file mode 100644
index 0000000..162a469
--- /dev/null
+++ b/freevideo_engine/media_encoding.py
@@ -0,0 +1,216 @@
+"""Bounded media normalization and native H3 encoder inputs.
+
+Reference video pixels stay uint8 on disk; only the Qwen 2 fps sample and one
+VAE input are materialized. Neither ComfyUI nor a whole float RGB movie is kept
+in the generation process. Reference clips must be explicitly trimmed to 15 s.
+"""
+import json
+import math
+from pathlib import Path
+
+
+def reference_size(width, height, canvas):
+ scale = min(1., math.sqrt(canvas['width'] * canvas['height'] / (width * height)))
+ return max(32, round(width * scale / 32) * 32), max(32, round(height * scale / 32) * 32)
+
+
+def encode_visual(vae, pixels, *, device='cuda'):
+ """The pinned VAE chunk/padding/posterior recipe with one live pixel chunk.
+
+ Posterior sampling still occurs once on the original device with seed 42;
+ sampling separately per chunk would change the conditioning noise.
+ """
+ import numpy as np
+ import torch
+ from diffusers.models.autoencoders.vae import DiagonalGaussianDistribution
+ from src.inference.render import PIXEL_MEAN, PIXEL_STD
+ mean = torch.tensor(PIXEL_MEAN, device=device).view(1, -1, 1, 1, 1)
+ std = torch.tensor(PIXEL_STD, device=device).view(1, -1, 1, 1, 1)
+ clip = vae.config.clip_length
+ moments = []
+ for begin in range(0, len(pixels), clip):
+ current = torch.from_numpy(np.array(pixels[begin:begin + clip])).to(device).permute(3, 0, 1, 2)[None]
+ current = (current.float().div(255.) - mean) / std
+ if len(pixels) != 1 and current.shape[2] < clip:
+ current = torch.cat([current, current[:, :, -1:].repeat(1, 1, clip - current.shape[2], 1, 1)], dim=2)
+ moments.append(vae._encode_clip(current).cpu())
+ del current
+ moments = torch.cat(moments, dim=2).to(device)
+ if len(pixels) > 1 and vae.config.token_drop:
+ moments = moments[:, :, :-vae.config.token_drop]
+ latent = DiagonalGaussianDistribution(moments).sample(generator=torch.Generator().manual_seed(42))
+ latent = latent.to(torch.float16).float().cpu()
+ mean = torch.tensor(vae.config.latents_mean).view(1, -1, 1, 1, 1)
+ std = torch.tensor(vae.config.latents_std).view(1, -1, 1, 1, 1)
+ return (latent - mean) / std
+
+
+def _audio(path, destination):
+ import av
+ import numpy as np
+ chunks = []
+ with av.open(str(path)) as container:
+ if not container.streams.audio:
+ return None
+ resampler = av.AudioResampler(format='fltp', layout='stereo', rate=32000)
+ count = 0
+ for frame in container.decode(audio=0):
+ for item in resampler.resample(frame):
+ data = item.to_ndarray()
+ count += data.shape[1]
+ if count > 15 * 32000:
+ raise ValueError('Reference audio exceeds 15 s; trim it explicitly before generation')
+ chunks.append(data)
+ for item in resampler.resample(None):
+ chunks.append(item.to_ndarray())
+ if not chunks:
+ raise ValueError('Reference audio is empty')
+ data = np.concatenate(chunks, axis=1)
+ if data.shape[1] > 15 * 32000 or not np.isfinite(data).all():
+ raise ValueError('Invalid reference audio length or samples')
+ np.save(destination, data, allow_pickle=False)
+ return str(destination)
+
+
+def prepare(media, canvas, directory):
+ import numpy as np
+ import torch
+ from PIL import Image, ImageOps
+ from .media_request import verify_files
+ verify_files(media)
+ directory = Path(directory)
+ directory.mkdir(parents=True, exist_ok=True)
+ result, key_images, ref_items = [], [], []
+ for anchor in ('first', 'last'):
+ if not media.get(anchor):
+ continue
+ with Image.open(media[anchor]['path']) as source:
+ image = ImageOps.fit(ImageOps.exif_transpose(source).convert('RGB'),
+ (canvas['width'], canvas['height']), method=Image.Resampling.LANCZOS)
+ target = directory / (anchor + '.npy')
+ np.save(target, np.array(image)[None], allow_pickle=False)
+ key_images.append(torch.from_numpy(np.array(image)).float().div_(255)[None])
+ result.append({'kind': 'image', 'anchor': anchor, 'pixels': str(target), 'frames': 1})
+ for index, row in enumerate(media.get('references', [])):
+ kind, path = row['kind'], row['path']
+ ref = {'kind': kind}
+ target = directory / ('reference-%02d.npy' % index)
+ if kind == 'image':
+ with Image.open(path) as source:
+ image = ImageOps.exif_transpose(source).convert('RGB')
+ image = image.resize(reference_size(*image.size, canvas), Image.Resampling.LANCZOS)
+ np.save(target, np.array(image)[None], allow_pickle=False)
+ ref.update(pixels=str(target), frames=1)
+ ref_items.append({'type': 'image', 'data': torch.from_numpy(np.array(image)).float().div_(255)[None]})
+ elif kind == 'video':
+ import av
+ with av.open(path) as container:
+ if not container.streams.video:
+ raise ValueError('Reference video has no video stream')
+ stream = container.streams.video[0]
+ width, height = reference_size(stream.width, stream.height, canvas)
+ if stream.duration and float(stream.duration * stream.time_base) > 15.05:
+ raise ValueError('Reference video exceeds 15 s; trim it explicitly')
+ pixels = np.lib.format.open_memmap(target, mode='w+', dtype=np.uint8, shape=(360, height, width, 3))
+ origin = None
+ count = 0
+ previous = None
+ fps = float(stream.average_rate or 24)
+ for index_frame, frame in enumerate(container.decode(video=0)):
+ timestamp = float(frame.time) if frame.time is not None else index_frame / fps
+ if origin is None:
+ origin = timestamp
+ timestamp -= origin
+ if timestamp > 15.05:
+ raise ValueError('Reference video exceeds 15 s; trim it explicitly')
+ image = frame.reformat(width=width, height=height, format='rgb24').to_ndarray()
+ # Explicit 24 fps reference normalization, independent of output geometry.
+ while count / 24. <= timestamp + 1e-6 and count < 360:
+ pixels[count] = image if previous is None or abs(count / 24. - timestamp) < .5 / fps else previous
+ count += 1
+ previous = image
+ del previous
+ count = count - (count - 5) % 17
+ if count < 22:
+ raise ValueError('Reference video needs at least 22 normalized frames for H3/Qwen temporal encoding')
+ pixels.flush()
+ ref.update(pixels=str(target), frames=count, normalized_fps=24)
+ soundtrack = _audio(path, directory / ('reference-%02d-audio.npy' % index))
+ if soundtrack:
+ ref['audio'] = soundtrack
+ ref_items.append({'type': 'audio'})
+ selected = list(range(0, count, 12))
+ sampled = torch.from_numpy(np.array(pixels[selected])).float().div_(255)
+ ref_items.append({'type': 'video', 'data': sampled, 'timestamps': [i / 2. for i in range(len(selected))]})
+ del pixels
+ else:
+ ref['audio'] = _audio(path, target)
+ if ref['audio'] is None:
+ raise ValueError('Reference audio file has no audio stream')
+ ref_items.append({'type': 'audio'})
+ result.append(ref)
+ (directory / 'normalization.json').write_text(json.dumps(result, indent=2) + '\n', encoding='utf-8')
+ return result, {'images': key_images} if key_images else {'minimax_ref_items': ref_items} if ref_items else {}
+
+
+def encode_latents(normalized, value, base, canvas, *, resident=None):
+ import gc
+ import numpy as np
+ import torch
+ from diffusers import AutoencoderKLMiniMaxH3, AutoencoderKLMiniMaxH3Audio
+ from diffusers.modular_pipelines.minimax_h3.encoders import encode_vae_condition
+ from src.inference.render import PIXEL_MEAN, PIXEL_STD
+ refs = [{'kind': row['kind']} for row in normalized]
+ if any(row.get('pixels') for row in normalized):
+ print(json.dumps({'event': 'media_encode_phase', 'phase': 'Encoding image/video references'}), flush=True)
+ vae = AutoencoderKLMiniMaxH3.from_pretrained(base, subfolder='vae')
+ # Conditioning never decodes. Drop unused decoder weights before CUDA
+ # placement; the independent output decoder is loaded after sampling.
+ vae.decoder = None
+ vae.post_quant_conv = None
+ if resident is not None:
+ resident.input_vae_room(vae, canvas)
+ vae.to('cuda')
+ vae.eval().requires_grad_(False)
+ for row, ref in zip(normalized, refs):
+ if row.get('pixels'):
+ mapped = np.load(row['pixels'], mmap_mode='r', allow_pickle=False)
+ if row['frames'] == 1:
+ pixels = torch.from_numpy(np.array(mapped[:1])).to('cuda').permute(3, 0, 1, 2)[None]
+ ref['latent'] = encode_vae_condition(vae, pixels, PIXEL_MEAN, PIXEL_STD, 42).cpu()
+ del pixels
+ else:
+ ref['latent'] = encode_visual(vae, mapped[:row['frames']])
+ del mapped
+ from .streamed_weights import release_file_pages
+ release_file_pages([Path(row['pixels'])])
+ del vae
+ gc.collect()
+ torch.cuda.empty_cache()
+ if any(row.get('audio') for row in normalized):
+ print(json.dumps({'event': 'media_encode_phase', 'phase': 'Encoding reference audio'}), flush=True)
+ vae = AutoencoderKLMiniMaxH3Audio.from_pretrained(base, subfolder='audio_vae')
+ if resident is not None:
+ resident.input_vae_room(vae, canvas, audio=True)
+ vae.to('cuda')
+ vae.eval().requires_grad_(False)
+ mean = torch.tensor(vae.config.latents_mean).view(1, 1, -1)
+ std = torch.tensor(vae.config.latents_std).view(1, 1, -1)
+ if vae.config.sampling_rate != 32000:
+ raise ValueError('Reference audio normalization requires the pinned 32 kHz H3 VAE')
+ for row, ref in zip(normalized, refs):
+ if row.get('audio'):
+ audio = torch.from_numpy(np.load(row['audio'], allow_pickle=False)).to('cuda')
+ latent = vae.encode(audio[:, None], return_dict=False)[0].mode().float().cpu().transpose(1, 2)
+ ref['audio_latent'] = ((latent - mean) / std).reshape(-1, 32).contiguous()
+ del audio, latent
+ del vae
+ gc.collect()
+ torch.cuda.empty_cache()
+ if any(row.get('anchor') for row in normalized):
+ value.update(keyframe_anchors=[row['anchor'] for row in normalized],
+ condition_latents=[row['latent'] for row in refs])
+ else:
+ value['references'] = refs
+ from .media_conditioning import describe
+ return describe(value, canvas['width'], canvas['height'], canvas['frames'])
diff --git a/freevideo_engine/media_request.py b/freevideo_engine/media_request.py
new file mode 100644
index 0000000..70e0abf
--- /dev/null
+++ b/freevideo_engine/media_request.py
@@ -0,0 +1,110 @@
+"""Small, Torch-free media/adapter request contract shared by CLI and ComfyUI."""
+import hashlib
+import json
+import math
+from pathlib import Path
+
+
+TASKS = ('t2va', 'i2va', 'l2va', 'fl2va', 'ref2va', 'ref2va_audio', 'ref2va_av')
+
+
+def digest(path):
+ value = hashlib.sha256()
+ with Path(path).open('rb') as stream:
+ for block in iter(lambda: stream.read(1024 * 1024), b''):
+ value.update(block)
+ return value.hexdigest()
+
+
+def read(path):
+ if path is None:
+ return {}
+ path = Path(path).resolve()
+ if path.stat().st_size > 1024 * 1024:
+ raise ValueError('Media request JSON exceeds 1 MiB')
+ value = json.loads(path.read_text(encoding='utf-8'))
+ if not isinstance(value, dict) or value.get('version') != 1:
+ raise ValueError('Expected a version 1 FreeVideo media request')
+ if set(value) - {'version', 'first', 'last', 'references', 'loras', 'conditioning_info'}:
+ raise ValueError('Unknown media request fields')
+ result = {'version': 1}
+ def file_info(name):
+ if not isinstance(name, str) or not name:
+ raise ValueError('Expected a media/LoRA file path')
+ source = Path(name).expanduser()
+ if not source.is_absolute():
+ source = path.parent / source
+ source = source.resolve(strict=True)
+ if not source.is_file() or not source.stat().st_size:
+ raise ValueError('Input file is empty or not a file: ' + str(source))
+ return {'path': str(source), 'sha256': digest(source), 'bytes': source.stat().st_size}
+ for anchor in ('first', 'last'):
+ if value.get(anchor):
+ result[anchor] = file_info(value[anchor])
+ for field in ('references', 'loras'):
+ entries = value.get(field, [])
+ if not isinstance(entries, list) or len(entries) > 32:
+ raise ValueError('At most 32 ' + field + ' per request')
+ result[field] = []
+ for entry in entries:
+ if not isinstance(entry, dict):
+ raise ValueError('Invalid ' + field + ' entry')
+ allowed = {'path', 'kind'} if field == 'references' else {'path', 'strength', 'alpha'}
+ if set(entry) - allowed:
+ raise ValueError('Unknown ' + field + ' fields')
+ row = file_info(entry.get('path'))
+ if field == 'references':
+ if entry.get('kind') not in ('image', 'video', 'audio'):
+ raise ValueError('Reference kind must be image, video or audio')
+ row['kind'] = entry['kind']
+ else:
+ if Path(row['path']).suffix.lower() != '.safetensors':
+ raise ValueError('LoRA requires safetensors; pickle adapters are not loaded')
+ for name, default in (('strength', 1.), ('alpha', None)):
+ number = entry.get(name, default)
+ if (number is None and name == 'strength') or (number is not None and
+ (isinstance(number, bool) or not isinstance(number, (int, float)) or not math.isfinite(number))):
+ raise ValueError('LoRA ' + name + ' must be finite')
+ row[name] = number
+ result[field].append(row)
+ if result['references'] and any(result.get(k) for k in ('first', 'last')):
+ raise ValueError('Reference and keyframe layouts cannot be mixed; use separate requests')
+ if value.get('conditioning_info') is not None:
+ info = value['conditioning_info']
+ if not isinstance(info, dict) or info.get('task') not in TASKS:
+ raise ValueError('Invalid preencoded conditioning information')
+ result['conditioning_info'] = info
+ return result
+
+
+def task_for(media):
+ if media.get('conditioning_info'):
+ return media['conditioning_info']['task']
+ if media.get('references'):
+ return 'ref2va'
+ if media.get('first') and media.get('last'):
+ return 'fl2va'
+ return 'i2va' if media.get('first') else 'l2va' if media.get('last') else 't2va'
+
+
+def cache_key(prompt, media, canvas):
+ # LoRA affects the denoiser, not the encoder. Paths do not identify content.
+ if not any(media.get(k) for k in ('first', 'last', 'references')):
+ return prompt
+ identity = {'version': 1, 'prompt': prompt, 'task': task_for(media),
+ 'width': canvas['width'], 'height': canvas['height'], 'frames': canvas['frames']}
+ for key in ('first', 'last', 'references'):
+ value = media.get(key)
+ if value:
+ identity[key] = [{k: v for k, v in row.items() if k != 'path'}
+ for row in value] if isinstance(value, list) else value['sha256']
+ return 'multimodal:' + json.dumps(identity, sort_keys=True, ensure_ascii=False)
+
+
+def verify_files(media):
+ rows = [media[k] for k in ('first', 'last') if media.get(k)]
+ rows += media.get('references', []) + media.get('loras', [])
+ for row in rows:
+ path = Path(row['path'])
+ if path.stat().st_size != row['bytes'] or digest(path) != row['sha256']:
+ raise ValueError('Input changed after inspection: ' + str(path))
diff --git a/freevideo_engine/model_files.json b/freevideo_engine/model_files.json
new file mode 100644
index 0000000..51a4513
--- /dev/null
+++ b/freevideo_engine/model_files.json
@@ -0,0 +1,323 @@
+[
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "LICENSE",
+ "bytes": 17604,
+ "sha256": null,
+ "git_blob": "c389b45855337ccec8ddeb389f4fe902abcd0b19"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/LICENSE",
+ "bytes": 17604,
+ "sha256": null,
+ "git_blob": "c389b45855337ccec8ddeb389f4fe902abcd0b19"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/README.md",
+ "bytes": 38391,
+ "sha256": null,
+ "git_blob": "aa9bba6233ed8d0b486fb43e1a35b4fb5ede2a60"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/audio_scheduler/scheduler_config.json",
+ "bytes": 96,
+ "sha256": null,
+ "git_blob": "28a3d4ed602d925031c7e5d6eeb07df8958ff0f1"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/audio_vae/config.json",
+ "bytes": 2271,
+ "sha256": null,
+ "git_blob": "15df6fe0b288b260dca0f2aebf1cd81334d77e96"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/audio_vae/diffusion_pytorch_model.safetensors",
+ "bytes": 605429340,
+ "sha256": "52c59e67ba8de5477c81bfbced0327aabf500f1bfdeefd5ee754529241cb26cb",
+ "git_blob": "4a381c6f4b9a67db97c25d250f9d00942d17a49e"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/model_index.json",
+ "bytes": 2936,
+ "sha256": null,
+ "git_blob": "454be19164878173a962c39c81633e4377880fca"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/modular_model_index.json",
+ "bytes": 2935,
+ "sha256": null,
+ "git_blob": "5b69f1e801bd0a7e9733c48704c732339f2002fe"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/scheduler/scheduler_config.json",
+ "bytes": 97,
+ "sha256": null,
+ "git_blob": "1d44f95b955a648ad4fab455e4fe11053888c30b"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/transformer/config.json",
+ "bytes": 546,
+ "sha256": null,
+ "git_blob": "c251a4133c81852a150c6668a570e142dc776b6e"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/transformer/diffusion_pytorch_model-00001-of-00014.safetensors",
+ "bytes": 4825958704,
+ "sha256": "2d847200c45c09dd7f973c1b096663068408ef851ee0b3711d059b6dc5dcd028",
+ "git_blob": "78e7c04f1246f7fd29127ba802f4c1fd65776404"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/transformer/diffusion_pytorch_model-00002-of-00014.safetensors",
+ "bytes": 4702158032,
+ "sha256": "2c4d362eddd2802180ac9c744849eb9ba8d9c8b984bdf9822cb02ed004b29184",
+ "git_blob": "4a79d68cef731db07fafbe78d9d5c6d5ddf9f395"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/transformer/diffusion_pytorch_model-00003-of-00014.safetensors",
+ "bytes": 4933368192,
+ "sha256": "949c5aafbbfa5654da730a6a7fafd75adb164d0857b095a30e8bb6d390887d69",
+ "git_blob": "f69102656be6332afea3376b76a81a2190b2aa89"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/transformer/diffusion_pytorch_model-00004-of-00014.safetensors",
+ "bytes": 4567069608,
+ "sha256": "eef7616790105ee839766bb2027203bf2c0d87c6aa038dca84145a8675f5ce28",
+ "git_blob": "663bd8d4ca0dfa722c99ca01d54049dd7efa0515"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/transformer/diffusion_pytorch_model-00005-of-00014.safetensors",
+ "bytes": 4702158080,
+ "sha256": "43fdf42d638e8bc6745f713fae80c93bb301807a1a5ae7249344ce28e202a494",
+ "git_blob": "338b0e9ce0fadd8aa27a253e50a3dbd565ded8ec"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/transformer/diffusion_pytorch_model-00006-of-00014.safetensors",
+ "bytes": 4933368232,
+ "sha256": "6442510b34d173653f0cce5c964b935395a8f7accf0b9cc0aa31aec59805239d",
+ "git_blob": "aff5d426da7d9972f3adb29f02cffaf62f94f9c1"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/transformer/diffusion_pytorch_model-00007-of-00014.safetensors",
+ "bytes": 4567069608,
+ "sha256": "29f48f535c91dac76496ca821eeb16ca24bc4caf3f0cae8b920a89b1f966da6d",
+ "git_blob": "e23672b7490bcd26fcb50976b89903acd85fc6ff"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/transformer/diffusion_pytorch_model-00008-of-00014.safetensors",
+ "bytes": 4702158080,
+ "sha256": "c711b096c764bd60f0b8b6ad49518bfab6d614fb788c725add8741c0674a4cd8",
+ "git_blob": "a7c89c8303ae2cace7518f9d995522e00872788e"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/transformer/diffusion_pytorch_model-00009-of-00014.safetensors",
+ "bytes": 4933368232,
+ "sha256": "44428defe3976cbb87635ad200b958199e739986697cd29fdf27aeb7294b5944",
+ "git_blob": "a63bcdf9be4d78b32124fcc032fbc1245f39ec05"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/transformer/diffusion_pytorch_model-00010-of-00014.safetensors",
+ "bytes": 4567069608,
+ "sha256": "3d44939c374c9da382e9c6877e1946adf7b84e08c7a881c068f228d6849411c9",
+ "git_blob": "e3b94e045a76f7cc87b17add5983165a800214a5"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/transformer/diffusion_pytorch_model-00011-of-00014.safetensors",
+ "bytes": 4702158080,
+ "sha256": "224d24430b58127a5577721084e0e704a0e74ec96dd7c35bc6fc0994ebd87c33",
+ "git_blob": "72a25189806a418d770b37ae8efc9b0d53fc312b"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/transformer/diffusion_pytorch_model-00012-of-00014.safetensors",
+ "bytes": 4933368232,
+ "sha256": "48fa2bd8fe134eef565ab2464f1c2589a6657cba0d14283dfc06b532f8961f3c",
+ "git_blob": "9545750c42b012ae13089df388d1569d10ad2a72"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/transformer/diffusion_pytorch_model-00013-of-00014.safetensors",
+ "bytes": 4567069608,
+ "sha256": "be5b4b1809f9d546ffd4b3fcf41e5c1e02b819125caa6bc105c109b04c051bd3",
+ "git_blob": "5827d7644a1ebc1ce9fcf40607857257f14f5659"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/transformer/diffusion_pytorch_model-00014-of-00014.safetensors",
+ "bytes": 4644161920,
+ "sha256": "8fbd5e6c1fb1df7ce988ca90f3d59e7610e465c7517e4b344eda4a214ba4b97d",
+ "git_blob": "5c97657ddfa2a1cf31bb6460a25c1caeccfe5a0c"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/transformer/diffusion_pytorch_model.safetensors.index.json",
+ "bytes": 64488,
+ "sha256": null,
+ "git_blob": "da038def25abb5a5e4be509816ee55a0b7105e09"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/vae/config.json",
+ "bytes": 2011,
+ "sha256": null,
+ "git_blob": "ed4874a5886972f4029fcd99cbbe72de0177b2c9"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/vae/diffusion_pytorch_model-00001-of-00003.safetensors",
+ "bytes": 5061033024,
+ "sha256": "72f4c6be84ac0674f27398cde991dd9d719762f3952c4921aa66b2ce542f6374",
+ "git_blob": "73f63f98715bce2897a36685ee6e3338f1a43416"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/vae/diffusion_pytorch_model-00002-of-00003.safetensors",
+ "bytes": 4955986528,
+ "sha256": "2e05e8bc23fa4071043e17fd242be8acd0685e781a43987432b2eae925be4198",
+ "git_blob": "d8083e40d1b26ed6ed02afd3bd4390fff32f6af5"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/vae/diffusion_pytorch_model-00003-of-00003.safetensors",
+ "bytes": 398539336,
+ "sha256": "c05d6ac4b1a33de372799d708531da6320f6a3ce6d1ce6d895e770988e004a39",
+ "git_blob": "1c31fd275aa880a3fb6ac88ae62ac02e79d9a61f"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "h3-base/vae/diffusion_pytorch_model.safetensors.index.json",
+ "bytes": 74228,
+ "sha256": null,
+ "git_blob": "05dfb255c44daa4f1ac487f57f18210fa5c19439"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "stage-dmd-step-250/adapters/default/adapter_config.json",
+ "bytes": 415,
+ "sha256": null,
+ "git_blob": "ef3c876ab5a596a1b6b4b28f3443bbe30c5455d8"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "stage-dmd-step-250/adapters/default/adapter_model.safetensors",
+ "bytes": 334026912,
+ "sha256": "58558fef506f88bb41649242de9b9b3a365da806b51b2e96afbbe1625222058a",
+ "git_blob": "d10c0e3d5b3f422a1a7a2bed521228e26c4aa8a6"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "stage-dmd-step-250/adapters/turbo/adapter_config.json",
+ "bytes": 22264,
+ "sha256": null,
+ "git_blob": "dbd7be544c9f3092b229f5cb6487f8da2f4ccc05"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "stage-dmd-step-250/adapters/turbo/adapter_model.safetensors",
+ "bytes": 851452696,
+ "sha256": "24fc93c82fe84dc45d0627f4e72c637bc387d282ba18f60ed3b7f8c81089392c",
+ "git_blob": "92e48f53129fef0ac0007972e45fb6ffd30626e3"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "stage-dmd-step-250/linear_branch/config.json",
+ "bytes": 465,
+ "sha256": null,
+ "git_blob": "314502a7c9d403f28493ac1cb5054032c8c7b545"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "stage-dmd-step-250/linear_branch/model.safetensors",
+ "bytes": 4279428112,
+ "sha256": "dec6981c7874f5b3bc92d1a02e256b673a3b3499dc1a124714bb3b19da602855",
+ "git_blob": "010d3a81dbb9015604e518a65ba3b86b7ef4f871"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "stage-dmd-step-250/metadata.json",
+ "bytes": 463,
+ "sha256": null,
+ "git_blob": "27ec152079222e3f2a83b8ee0bc634a77da0df01"
+ },
+ {
+ "repo": "OpenVDN/vdn-minimax-h3",
+ "revision": "751739ee5b9e3ac802dca5d5111075fdaeb47885",
+ "file": "stage-dmd-step-250/model_spec.json",
+ "bytes": 25705,
+ "sha256": null,
+ "git_blob": "e039c13987797f91e11a3477c38fa8fd1f6a506f"
+ },
+ {
+ "repo": "t8star/Vdn-Minimax-H3-Comfy",
+ "revision": "d6aa5dfc669a749dc9364a36f3b6d4d1545a53e8",
+ "file": "text_encoders/qwen3vl_32b_minimax_h3_nvfp4_awq.safetensors",
+ "bytes": 15687142551,
+ "sha256": "35a88d51044231fe332301d7a62aa81e3f2cba62febeb446e2c1e3e0ef76f2c6",
+ "git_blob": null
+ },
+ {
+ "repo": "LBH-123-AI/Minimax_h3_latent_Upscaler",
+ "revision": "3f941d5d182014dd5c0a5e16330420ee2d4aa0c6",
+ "file": "minimax_h3_latent_upscaler_3d_conv_v1/minimax_h3_latent_upscaler_3d_conv_v1_fp16.safetensors",
+ "bytes": 690592672,
+ "sha256": "043e5a48e161610ef6c3ea974645220354d06fa618abca15f76d084812eb55c2",
+ "role": "latent_upscaler",
+ "git_blob": null
+ }
+]
diff --git a/freevideo_engine/model_guidance.py b/freevideo_engine/model_guidance.py
new file mode 100644
index 0000000..5d1e8f6
--- /dev/null
+++ b/freevideo_engine/model_guidance.py
@@ -0,0 +1,71 @@
+"""User-facing model download destinations, from the installation manifests."""
+import json
+from pathlib import Path
+from urllib.parse import quote, urlsplit
+
+
+def links():
+ root = Path(__file__).parent
+ spec = json.loads((root / 'dependencies.json').read_text(encoding='utf-8'))['models']
+ edge = json.loads((root / 'prepared_models.json').read_text(encoding='utf-8'))
+ return {
+ 'video': [('Hugging Face', 'https://huggingface.co/' + edge['repo']),
+ ('ModelScope', 'https://modelscope.ai/models/' + spec['edge_modelscope']['repo'])],
+ 'encoder': [('Hugging Face', 'https://huggingface.co/' + spec['encoder_repo'] + '/blob/' +
+ spec['encoder_revision'] + '/' + quote(spec['encoder_file'], safe='/'))],
+ 'decoder': [('Hugging Face · Video', 'https://huggingface.co/' + spec['vdn_repo'] + '/tree/' +
+ spec['vdn_revision'] + '/h3-base/vae'),
+ ('Hugging Face · Audio', 'https://huggingface.co/' + spec['vdn_repo'] + '/tree/' +
+ spec['vdn_revision'] + '/h3-base/audio_vae'),
+ ('ModelScope', 'https://modelscope.ai/models/' + spec['vdn_modelscope']['repo'])],
+ }
+
+
+def cloud_models():
+ """Only publisher-approved or release-verified shares become download links."""
+ path = Path(__file__).with_name('model_shares.json')
+ if not path.exists():
+ return []
+ value = json.loads(path.read_text(encoding='utf-8'))
+ result = []
+ for row in value.get('models', []):
+ url = urlsplit(row.get('url', ''))
+ if ((row.get('publisher_approved') is True or row.get('verified') is True)
+ and url.scheme == 'https' and url.hostname == 'pan.quark.cn'
+ and url.path.startswith('/s/') and not url.username and not url.password):
+ result.append(row)
+ return result
+
+
+def show_component(app, component):
+ import webbrowser
+ from . import branding as b
+ from .launcher_copy import display
+ titles = {'video': ('Video model', '视频模型'), 'encoder': ('Text encoder', '文本编码器'),
+ 'decoder': ('Video & audio decoders', '视频与音频解码器')}
+ instructions = {
+ 'video': ('Choose your GPU variant on the model page and keep its folder structure.',
+ '按模型页说明选择显卡版本,保留下载的目录结构。'),
+ 'encoder': ('Download the text encoder to your model folder.',
+ '下载文本编码器,放入模型目录。'),
+ 'decoder': ('Download the vae and audio_vae folders.',
+ '下载 vae 和 audio_vae 文件夹。'),
+ }
+ dialog = app.tk.Toplevel(app.window)
+ dialog.title('FreeVideo · ' + app.t(*titles[component])); dialog.transient(app.window)
+ dialog.configure(bg=b.BACKGROUND)
+ body = app.ttk.Frame(dialog, padding=24); body.pack(fill='both', expand=True)
+ app.ttk.Label(body, text=app.t(*titles[component]), style='Heading.TLabel').pack(anchor='w')
+ app.ttk.Label(body, text=app.t(*instructions[component]), wraplength=490).pack(fill='x', pady=12)
+ for title, url in links()[component]:
+ app.ttk.Button(body, text=display(title, app.zh) + ' ↗', command=lambda u=url: webbrowser.open(u)).pack(fill='x', pady=4)
+ for row in cloud_models():
+ app.ttk.Button(body, text=app.t('Quark model pack · ', '夸克模型包 · ') + row['label'] + ' ↗',
+ command=lambda u=row['url']: webbrowser.open(u)).pack(fill='x', pady=4)
+ app.ttk.Label(body, text=app.t('After downloading, extract ZIP files and choose the model folder below. Subfolders are scanned automatically.',
+ '下载完成后先解压 ZIP,再选择模型所在文件夹;会自动扫描子文件夹。'), wraplength=490).pack(fill='x', pady=12)
+ def choose():
+ dialog.destroy()
+ app.model_method.set('reuse')
+ app.add_model_folder()
+ app.ttk.Button(body, text=app.t('Downloaded · choose folder…', '已下载,选择文件夹…'), command=choose).pack(fill='x')
diff --git a/freevideo_engine/model_shares.json b/freevideo_engine/model_shares.json
new file mode 100644
index 0000000..76fb098
--- /dev/null
+++ b/freevideo_engine/model_shares.json
@@ -0,0 +1,9 @@
+{
+ "models": [
+ {
+ "label": "FreeVideo",
+ "url": "https://pan.quark.cn/s/c51235b84618",
+ "publisher_approved": true
+ }
+ ]
+}
diff --git a/freevideo_engine/model_status.py b/freevideo_engine/model_status.py
new file mode 100644
index 0000000..2c46cad
--- /dev/null
+++ b/freevideo_engine/model_status.py
@@ -0,0 +1,70 @@
+"""User-facing model families and byte progress, independent of file names."""
+import math
+import threading
+
+FAMILIES = ('video', 'text', 'decoder')
+NAMES = {'video': ('Video model', '视频模型'), 'text': ('Text encoder', '文本编码器'),
+ 'decoder': ('Video & audio decoder', '视频与音频解码器')}
+
+
+def family(row):
+ if not row['repo'].startswith('OpenVDN/'):
+ return 'text'
+ parts = row['file'].replace('\\', '/').split('/')
+ return 'decoder' if any(p in ('vae', 'audio_vae', 'vocoder') for p in parts) else 'video'
+
+
+def inventory(entries):
+ """Entries pair a manifest row with download / found / verified placement.
+
+ Found means only size-matched, not integrity-verified. A scanned external
+ library has already been hashed; a file at the installation path has not.
+ """
+ result = {name: dict(id=name, total_bytes=0, existing_bytes=0, verified_bytes=0,
+ download_bytes=0, downloaded_bytes=0, ready_bytes=0, files=0, ready_files=0,
+ state='waiting', bytes_per_second=None) for name in FAMILIES}
+ for row, status in entries:
+ item, size = result[family(row)], row['bytes']
+ item['total_bytes'] += size
+ item['files'] += 1
+ item['download_bytes' if status == 'download' else 'existing_bytes'] += size
+ if status == 'verified':
+ item['verified_bytes'] += size
+ return [result[name] for name in FAMILIES if result[name]['files']]
+
+
+class ModelProgress:
+ """Keep one counter per manifest file; retries replace, never add counters."""
+ def __init__(self, entries, emit):
+ self.lock, self.emit = threading.RLock(), emit
+ self.files = {(row['repo'], row['file']): dict(row=row, origin=status, phase='waiting', done=0, rate=None)
+ for row, status in entries}
+
+ def update(self, row=None, *, phase=None, done=None, rate=None, local=False):
+ with self.lock:
+ if row is not None:
+ item = self.files[(row['repo'], row['file'])]
+ if local:
+ item['origin'] = 'found'
+ item['phase'] = phase
+ valid = isinstance(done, (int, float)) and math.isfinite(done) and 0 <= done <= row['bytes']
+ if done is not None:
+ item['done'] = done if valid else 0
+ item['rate'] = rate if valid and isinstance(rate, (int, float)) and math.isfinite(rate) and rate > 0 else None
+ elif phase == 'paused':
+ for item in self.files.values():
+ if item['phase'] in ('downloading', 'verifying'):
+ item.update(phase='paused', rate=None)
+ groups = inventory([(f['row'], f['origin']) for f in self.files.values()])
+ for group in groups:
+ items = [f for f in self.files.values() if family(f['row']) == group['id']]
+ group['downloaded_bytes'] = sum(f['done'] for f in items if f['origin'] == 'download')
+ group['ready_bytes'] = sum(f['row']['bytes'] for f in items if f['phase'] == 'ready')
+ group['ready_files'] = sum(f['phase'] == 'ready' for f in items)
+ group['verified_bytes'] = sum(f['row']['bytes'] for f in items
+ if f['origin'] != 'download' and f['phase'] == 'ready')
+ phases = {f['phase'] for f in items}
+ group['state'] = next((s for s in ('paused', 'downloading', 'verifying') if s in phases),
+ 'ready' if phases == {'ready'} else 'pending' if 'ready' in phases else 'waiting')
+ group['bytes_per_second'] = next((f['rate'] for f in items if f['phase'] == 'downloading' and f['rate']), None)
+ self.emit('model_groups', groups=groups)
diff --git a/freevideo_engine/model_transfer.py b/freevideo_engine/model_transfer.py
new file mode 100644
index 0000000..87fd802
--- /dev/null
+++ b/freevideo_engine/model_transfer.py
@@ -0,0 +1,760 @@
+"""Provider-native model downloads with bounded resources and retained failures.
+
+SDKs run in disposable workers so a stalled transfer can be stopped on Linux and
+Windows. Small files and unsupported routes retain the verified curl path.
+"""
+import argparse
+import json
+import os
+from pathlib import Path
+import re
+import subprocess
+import sys
+import threading
+import time
+
+from . import network, processes
+from .monitoring import save
+from .system import system_memory
+from .storage import fingerprint
+
+GiB = 1 << 30
+MiB = 1 << 20
+MIN_SDK_BYTES = 64 * MiB
+STALL_SECONDS = 240
+SAME_SOURCE_ATTEMPTS = 3
+
+
+def stage_directory(source, row, path):
+ base = Path(path)
+ for _ in Path(row['file']).parts:
+ base = base.parent
+ return base / '.freevideo-downloads' / ((row.get('sha256') or row['git_blob'])[:16] + '-' + source)
+
+
+def retained_payload(stage, row):
+ """Ignore metadata and quarantined attempts; count only active model files."""
+ total = 0
+ for path in stage.rglob('*') if stage.is_dir() else ():
+ if any(marker in str(path.relative_to(stage)) for marker in
+ ('.rejected-', '.hash-rejected-', '.interrupted-merge-', '.restart-approved-')):
+ continue
+ if (path == stage / row['file'] or path.name.endswith(('.incomplete', '.partial', '.parallel_tmp'))
+ or re.fullmatch(re.escape(Path(row['file']).name) + r'_\d+_\d+', path.name)):
+ try:
+ total += path.stat().st_size
+ except FileNotFoundError:
+ pass
+ return total
+
+
+def policy(ram_budget, build_peak=0):
+ """Reserve process overhead in addition to Xet's native buffer limit."""
+ guard = min(2 * GiB, max(GiB, int(ram_budget) // 8))
+ buffer_limit = min(GiB, max(256 * MiB, guard - GiB))
+ streams = 8 if buffer_limit < GiB else 16
+ # A single file already uses range/concurrent streams. Whole-file
+ # concurrency is therefore deliberately more conservative: account for
+ # the compiler that may run beside model setup and leave the default at
+ # one worker on small machines. ``provision.models`` uses this as a
+ # bounded scheduler limit; it never changes the per-file stream count.
+ overlap = build_peak + guard + GiB <= ram_budget
+ free_for_files = max(0, int(ram_budget) - (int(build_peak) if overlap else 0))
+ # The second slot is the verifier; a third transfer would only compete
+ # for the same network and native range buffers.
+ file_workers = 1 if free_for_files < 16 * GiB else 2
+ return dict(ram_guard_bytes=guard, buffer_limit_bytes=buffer_limit,
+ buffer_bytes=buffer_limit // 2, per_file_buffer_bytes=min(256 * MiB, buffer_limit // 2),
+ prefetch_bytes=min(256 * MiB, buffer_limit // 2), max_streams=streams,
+ modelscope_streams=4, part_bytes=64 * MiB, file_cache_window_bytes=256 * MiB,
+ overlap_build=overlap, file_workers=file_workers,
+ mode='native-adaptive-bounded', high_performance=False,
+ scope='Native buffer limits plus sampled process guard; not a kernel RAM cap')
+
+
+def worker_environment(source, directory, selected, environ=None):
+ env = network.proxy_environment(environ)
+ # These settings are owned by setup and read by SDKs at import time.
+ for key in list(env):
+ if key.startswith('HF_XET_') or key in ('HF_HUB_ENABLE_HF_TRANSFER', 'HF_HUB_OFFLINE', 'HF_HUB_DISABLE_XET'):
+ env.pop(key)
+ if source not in ('official', 'user'):
+ for key in ('HF_TOKEN', 'HUGGING_FACE_HUB_TOKEN', 'HF_TOKEN_PATH'):
+ env.pop(key, None)
+ env.update(HF_HUB_DISABLE_IMPLICIT_TOKEN='1', HF_HUB_DISABLE_TELEMETRY='1',
+ HF_HUB_DISABLE_UPDATE_CHECK='1', HF_HUB_DISABLE_PROGRESS_BARS='0',
+ HF_HUB_DOWNLOAD_TIMEOUT='60', HF_HUB_ETAG_TIMEOUT='15',
+ HF_XET_HIGH_PERFORMANCE='0', HF_XET_CHUNK_CACHE_SIZE_BYTES='0',
+ HF_XET_CACHE=str(directory / 'xet-cache'),
+ HF_XET_RECONSTRUCTION_DOWNLOAD_BUFFER_LIMIT=str(selected['buffer_limit_bytes']),
+ HF_XET_RECONSTRUCTION_DOWNLOAD_BUFFER_SIZE=str(selected['buffer_bytes']),
+ HF_XET_RECONSTRUCTION_DOWNLOAD_BUFFER_PERFILE_SIZE=str(selected['per_file_buffer_bytes']),
+ HF_XET_RECONSTRUCTION_MIN_PREFETCH_BUFFER=str(selected['prefetch_bytes']),
+ HF_XET_RECONSTRUCTION_MIN_RECONSTRUCTION_FETCH_SIZE=str(64 * MiB),
+ HF_XET_RECONSTRUCTION_MAX_RECONSTRUCTION_FETCH_SIZE=str(selected['buffer_bytes']),
+ HF_XET_CLIENT_ENABLE_ADAPTIVE_CONCURRENCY='true',
+ HF_XET_CLIENT_AC_MAX_DOWNLOAD_CONCURRENCY=str(selected['max_streams']),
+ HF_XET_CLIENT_CONNECT_TIMEOUT='10s', HF_XET_CLIENT_READ_TIMEOUT='60s',
+ HF_XET_CLIENT_RETRY_MAX_ATTEMPTS='3', HF_XET_CLIENT_RETRY_MAX_DURATION='180s',
+ MODELSCOPE_HOME=str(directory / 'ms-home'), MODELSCOPE_CACHE=str(directory / 'ms-cache'),
+ MODELSCOPE_DOWNLOAD_PARALLEL_WORKERS=str(selected['modelscope_streams']),
+ MODELSCOPE_DOWNLOAD_PARALLEL_THRESHOLD_MB='32', MODELSCOPE_DOWNLOAD_PART_SIZE_MB='64',
+ MODELSCOPE_DOWNLOAD_MAX_RETRIES='2', MODELSCOPE_DOWNLOAD_TIMEOUT='60',
+ MODELSCOPE_API_TIMEOUT='20', MODELSCOPE_API_CONNECT_TIMEOUT='5', MODELSCOPE_API_MAX_RETRIES='1',
+ MODELSCOPE_DOWNLOAD_INTRA_CLOUD='false', MODELSCOPE_LOG_LEVEL='ERROR')
+ env.pop('MODELSCOPE_API_TOKEN', None) # This integration downloads public VDN only.
+ return env
+
+
+def emit(**value):
+ sys.stdout.write(json.dumps(dict(event='model_transfer', **value)) + '\n')
+ sys.stdout.flush()
+
+
+class Progress:
+ def __init__(self, total):
+ self.total = total
+ self.done = self.transferred = 0
+ self.last = 0.
+ self.started = time.monotonic()
+ self.lock = threading.Lock()
+ self.completed = None
+
+ def update(self, amount, *, transfer=False):
+ with self.lock:
+ if transfer:
+ self.transferred += amount
+ else:
+ self.done += amount
+ now = time.monotonic()
+ if now - self.last >= .5:
+ if self.completed is not None:
+ self.done = min(self.total, self.completed())
+ emit(phase='download', done_bytes=self.done, total_bytes=self.total,
+ transfer_bytes=self.transferred, elapsed_seconds=now-self.started)
+ self.last = now
+
+
+def verify(path, row):
+ if path.stat().st_size != row['bytes']:
+ raise ValueError('Downloaded size differs from the pinned manifest')
+ return network.hash_file(path, 'sha256' if row.get('sha256') else 'sha1', not row.get('sha256'), discard_cache=True) == (row.get('sha256') or row['git_blob'])
+
+
+def checked_range_response(response, requested, total, progress):
+ """Validate SDK ranges before writing, including MS's 200 + Content-Range.
+
+ The pinned SDK resumes part files but does not validate their ranges. Keep
+ its streaming/retry implementation while rejecting ignored or wrong ranges.
+ """
+ match = re.fullmatch(r'bytes=(\d+)-(\d+)', requested)
+ if not match:
+ response.close()
+ raise ValueError('Invalid model range request')
+ start, end = map(int, match.groups())
+ expected = end - start + 1
+ content_range = response.headers.get('Content-Range', '')
+ length = response.headers.get('Content-Length')
+ if (response.status_code not in (200, 206) or
+ content_range != 'bytes %d-%d/%d' % (start, end, total) or
+ (length is not None and length != str(expected))):
+ response.close()
+ raise ValueError('Model source did not honor the requested byte range')
+ original = response.iter_content
+
+ def chunks(*args, **kwargs):
+ received = 0
+ try:
+ for chunk in original(*args, **kwargs):
+ received += len(chunk)
+ if received > expected:
+ raise ValueError('Model range exceeded its expected size')
+ progress.update(len(chunk), transfer=True)
+ yield chunk
+ if received != expected:
+ raise OSError('Model range ended before its expected size')
+ finally:
+ response.close()
+
+ response.iter_content = chunks
+ return response
+
+
+def sdk_worker(request):
+ row, source = request['row'], request['source']
+ stage = Path(request['stage'])
+ target = stage / row['file']
+ progress = Progress(row['bytes'])
+ backend = []
+ started = time.monotonic()
+ try:
+ if target.is_file():
+ emit(phase='verify')
+ if verify(target, row):
+ emit(phase='ready', backend='verified-stage', seconds=time.monotonic()-started, fingerprint=fingerprint(target))
+ return 0
+ network.retain_partial(target, 'hash-rejected')
+ emit(phase='connect')
+ if source == 'modelscope' or request.get('manual_ranges'):
+ from modelscope_hub import HubApi
+ from modelscope_hub import _download
+ from modelscope_hub.constants import DOWNLOAD_PARALLELS, DOWNLOAD_PART_SIZE
+ mirror = network.modelscope_mapping(row)
+ if not mirror and not request.get('manual_ranges'):
+ raise ValueError('No verified ModelScope mapping')
+ # SDK retry callbacks include already retained bytes again. Derive
+ # completion from the part files, and count wire bytes separately.
+ from .provider_resume import ms_completed
+ progress.completed = lambda: ms_completed(target, row['bytes'], DOWNLOAD_PART_SIZE)
+
+ class Callback:
+ def __init__(self, filename, total):
+ pass
+ def update(self, amount):
+ progress.update(0)
+ def end(self):
+ emit(phase='reconstruct', done_bytes=row['bytes'], total_bytes=row['bytes'])
+
+ original_get, original_parallel = _download.requests.get, getattr(_download, '_parallel_download', None)
+
+ def get(*args, **kwargs):
+ response = original_get(*args, **kwargs)
+ requested = (kwargs.get('headers') or {}).get('Range')
+ return checked_range_response(response, requested, row['bytes'], progress) if requested else response
+
+ method='http-ranges' if request.get('manual_ranges') else 'modelscope-ranges'
+ emit(phase='backend', backend=method, streams=DOWNLOAD_PARALLELS, part_bytes=DOWNLOAD_PART_SIZE)
+ backend.append(method)
+ # FreeVideo owns final integrity handling. Passing no SDK hash
+ # avoids its delete-and-retry behavior for a rejected download.
+ _download.requests.get = get
+ from .provider_resume import ms_parallel
+ _download._parallel_download = lambda *a, **k: ms_parallel(_download, *a, **k)
+ try:
+ if request.get('manual_ranges'):
+ headers={}
+ if source in ('official','user'):
+ from huggingface_hub import get_token
+ token=get_token()
+ if token:headers['Authorization']='Bearer '+token
+ ms_parallel(_download,network.model_url(row,source),target,row['bytes'],headers,None,
+ [Callback(row['file'],row['bytes'])])
+ else:
+ HubApi(endpoint=network.source_url(mirror['family'], source), token='').downloader.download_file(
+ mirror['repo'], 'model', row['file'], revision=mirror['revision'], local_dir=stage,
+ file_size=row['bytes'], expected_sha256=None, progress_callbacks=[Callback])
+ finally:
+ _download.requests.get = original_get
+ if original_parallel is not None:
+ _download._parallel_download = original_parallel
+ else:
+ from huggingface_hub import hf_hub_download, get_token, file_download
+ from hf_xet import XetConfig
+ config = dict(XetConfig().items())
+ requested = request['policy']
+ if config['reconstruction.download_buffer_limit'] > requested['buffer_limit_bytes']:
+ raise RuntimeError('Xet did not apply its requested buffer limit')
+ emit(phase='configuration', buffer_limit_bytes=config['reconstruction.download_buffer_limit'],
+ max_streams=config['client.ac_max_download_concurrency'])
+ originals = (file_download.xet_get, file_download.http_get, getattr(file_download, '_download_to_tmp_and_move', None))
+
+ def xet(*args, **kwargs):
+ backend.append('hf-xet')
+ emit(phase='backend', backend='hf-xet')
+ return originals[0](*args, **kwargs)
+
+ def http(*args, **kwargs):
+ if request.get('require_xet'):
+ raise RuntimeError('HF selected HTTP but --model-downloader xet requires Xet; no fallback performed')
+ backend.append('hf-http')
+ emit(phase='backend', backend='hf-http')
+ return originals[1](*args, **kwargs)
+
+ class Bar:
+ def __init__(self, *args, total=None, initial=0, **kwargs):
+ self.total, self.n = total, initial
+ def __enter__(self):
+ return self
+ def __exit__(self, *args):
+ pass
+ def update(self, amount=1):
+ self.n += amount or 0
+ progress.update(amount or 0)
+ def update_transfer(self, amount):
+ progress.update(amount, transfer=True)
+ def set_postfix_str(self, *args, **kwargs):
+ pass
+ def set_transfer_postfix_str(self, *args, **kwargs):
+ pass
+ def refresh(self, *args, **kwargs):
+ pass
+ def close(self):
+ pass
+
+ file_download.xet_get, file_download.http_get = xet, http
+ from .provider_resume import hf_download
+ def durable_download(*args, **kwargs):
+ def actual(name):
+ backend.append(name)
+ emit(phase='backend', backend=name)
+ def report(done, total, rate):
+ emit(phase='download', done_bytes=done, total_bytes=total,
+ bytes_per_second=rate, transfer_bytes=0, elapsed_seconds=time.monotonic()-started)
+ return hf_download(request, file_download, report, *args, backend_callback=actual, **kwargs)
+ file_download._download_to_tmp_and_move = durable_download
+ token = (get_token() or False) if source in ('official', 'user') else False
+ try:
+ hf_hub_download(row['repo'], row['file'], revision=row['revision'], token=token,
+ endpoint=network.source_url('models', source), local_dir=stage,
+ cache_dir=stage / 'hub-cache', tqdm_class=Bar)
+ finally:
+ file_download.xet_get, file_download.http_get = originals[:2]
+ if originals[2] is not None:
+ file_download._download_to_tmp_and_move = originals[2]
+ # The provider has returned and closed its staged output. This is a
+ # safe hand-off point for the installer to start the next file while
+ # this worker performs the pinned digest check below. Do not emit
+ # this before the provider call returns: progress reaching the byte
+ # count alone does not prove that the file descriptor is closed.
+ emit(phase='transfer_complete', done_bytes=row['bytes'], total_bytes=row['bytes'])
+ downloaded = time.monotonic()
+ emit(phase='verify')
+ if not verify(target, row):
+ raise ValueError('Downloaded hash differs from the pinned manifest')
+ emit(phase='ready', backend=backend[-1] if backend else 'hf-cache',
+ download_seconds=downloaded-started, verification_seconds=time.monotonic()-downloaded,
+ done_bytes=row['bytes'], total_bytes=row['bytes'], transfer_bytes=progress.transferred,
+ fingerprint=fingerprint(target))
+ return 0
+ except Exception as error:
+ # Signed CDN URLs and authentication details can appear in SDK errors.
+ if target.is_file():
+ network.retain_partial(target, 'rejected')
+ import traceback
+ traceback.print_exc()
+ emit(phase='failed', error_type=type(error).__name__, message=safe_log(str(error)))
+ return 1
+
+
+def safe_log(line):
+ from .diagnostics import Redactor
+ # SDK exceptions sometimes include signed CDN queries or proxy userinfo.
+ line = re.sub(r'https?://[^\s\"<>]+', '<download URL>', line)
+ return Redactor().text(line)
+
+
+def stage_overflow(directory, row, part_bytes):
+ """Check each active payload against its own range, not a moving disk sum.
+
+ During merge a part and its durable prefix temporarily own the same bytes.
+ Walking the directory is not an atomic snapshot: it can see every part
+ before unlink and the completed prefix afterwards. Summing those sizes can
+ double count an entire valid model. Metadata and retained rejected attempts
+ are not new transfer payload either. Final size AND digest checks still
+ authorize publication; this check only stops an overlong active writer.
+ """
+ target = directory / row['file']
+ prefixes = (target, target.with_suffix(target.suffix + '.partial'),
+ target.with_suffix(target.suffix + '.parallel_tmp'), directory / 'model.xet.incomplete')
+ ignored = ('.rejected-', '.hash-rejected-', '.interrupted-merge-',
+ '.restart-approved-', '.switch-retained-', '.oversized-', '.oversized-response-')
+ for root, directories, files in os.walk(directory):
+ directories[:] = [name for name in directories if name not in ('xet-cache', 'ms-cache', 'ms-home', 'hub-cache')
+ and not any(marker in name for marker in ignored)]
+ for name in files:
+ if any(marker in name for marker in ignored):
+ continue
+ path = Path(root) / name
+ limit = row['bytes'] if path in prefixes or name.endswith('.incomplete') else None
+ if path.parent == target.parent:
+ match = re.fullmatch(re.escape(target.name) + r'_(\d+)_(\d+)', name)
+ if match:
+ start, end = map(int, match.groups())
+ limit = (end - start + 1 if start % part_bytes == 0 and
+ 0 <= start <= end < row['bytes'] and
+ end == min(row['bytes'], start + part_bytes) - 1 else -1)
+ if limit is None:
+ continue
+ try:
+ size = path.stat().st_size
+ except FileNotFoundError:
+ continue # Merged/published since the directory was enumerated.
+ if size > limit:
+ return dict(file=str(path.relative_to(directory)), observed_bytes=size,
+ allowed_bytes=max(0, limit), expected_model_bytes=row['bytes'],
+ reason='invalid-range' if limit < 0 else 'file-exceeds-range')
+ return None
+
+
+class TransferSizeError(RuntimeError):
+ def __init__(self, details):
+ self.details = details
+ super().__init__('unexpected-transfer-size')
+
+
+def run_sdk(source, row, path, plan, progress_callback, networking, transfer_complete=None):
+ from .ram import ProcessMemory
+ from .hardware import cgroup_capacity
+ selected = dict(plan.get('model_transfer') or policy(plan.get('policy_estimate', {}).get('ram_budget_bytes', 16*GiB)))
+ # The compiler and desktop can consume RAM after preflight. Recheck here.
+ available = system_memory()['available_bytes']
+ _, group_available = cgroup_capacity()
+ if group_available is not None:
+ available = min(available, group_available)
+ if available < 3 * GiB:
+ raise RuntimeError('low-live-memory-use-streaming')
+ if available < selected['ram_guard_bytes'] + 2 * GiB:
+ selected = policy(max(GiB, available - 2 * GiB))
+ stage = stage_directory('manual', row, path) if plan.get('manual_ranges') else stage_directory(source, row, path)
+ stage.mkdir(parents=True, exist_ok=True)
+ if plan.get('manual_ranges'):
+ save(stage/'manual-source.json',dict(source=source,expected=row.get('sha256') or row['git_blob']))
+ events_path = networking.get('events_path') or os.environ.get('FREEVIDEO_NETWORK_EVENTS')
+ reports = Path(events_path).parent if events_path else Path(plan['root']) / 'transfer-reports'
+ report = reports / 'model-transfers' / (str(time.time_ns()) + '-' + source)
+ report.mkdir(parents=True)
+ request = dict(row=row, source=source, stage=str(stage), policy=selected,
+ proxy_route=networking.get('active_route', network.route_order(networking, network.model_family(row), source)[0]),
+ require_xet=plan.get('model_downloader') == 'xet',
+ allow_model_restart=plan.get('allow_model_restart', False))
+ if plan.get('manual_ranges'):
+ request['manual_ranges']=True
+ save(report / 'request.json', request)
+ state = dict(phase='connect', last_activity=time.monotonic(), done_bytes=0, transfer_bytes=0)
+ state_lock = threading.Lock()
+ memory = ProcessMemory()
+ process = None
+ reader = None
+ failure = None
+ size_error = None
+ transfer_signalled = False
+
+ def read_output():
+ try:
+ with (report / 'worker.log').open('w', encoding='utf-8') as log:
+ for line in iter(process.stdout.readline, ''):
+ log.write(safe_log(line))
+ log.flush()
+ try:
+ event = json.loads(line)
+ except ValueError:
+ continue
+ if isinstance(event, dict) and event.get('event') == 'network':
+ network.event(networking, **{k: v for k, v in event.items() if k not in ('event', 'epoch')})
+ if event.get('action') == 'verifying':
+ with state_lock:
+ state.update(phase='verify', last_activity=time.monotonic())
+ continue
+ if not isinstance(event, dict) or event.get('event') != 'model_transfer':
+ continue
+ with state_lock:
+ phase = event.get('phase')
+ if (event.get('done_bytes', 0) > state['done_bytes'] or
+ event.get('transfer_bytes', 0) > state['transfer_bytes'] or phase != state['phase']):
+ state['last_activity'] = time.monotonic()
+ state.update(event)
+ except Exception as error:
+ with state_lock:
+ state['reader_error'] = type(error).__name__
+
+ started = time.monotonic()
+ try:
+ process = processes.popen([sys.executable, '-m', 'freevideo_engine.model_transfer', '--worker', str(report / 'request.json')],
+ env=worker_environment(source, stage, selected,
+ network.route_environment(networking, network.model_family(row), source, route=request['proxy_route'])),
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
+ text=True, encoding='utf-8', errors='replace', start_new_session=True, pass_fds=processes.descriptors(), supervise=True)
+ reader = threading.Thread(target=read_output, daemon=True)
+ reader.start()
+ last_emit = 0.
+ prior_phase = None
+ prior_backend = None
+ prior_io = 0
+ while process.poll() is None:
+ if networking.get('resource_check'):
+ networking['resource_check']()
+ sample = memory.sample(process.pid)
+ guard = sample['guard_bytes']
+ if guard is None or guard > selected['ram_guard_bytes'] or sample.get('effective_available_bytes', sample['system_available_bytes']) < 2*GiB:
+ failure = 'memory-guard'
+ break
+ with state_lock:
+ disk_io = sum(memory.result().get(key, 0) or 0 for key in
+ ('process_tree_disk_write_bytes', 'process_tree_disk_read_bytes'))
+ if disk_io > prior_io and state['phase'] in ('verify', 'reconstruct'):
+ state['last_activity'] = time.monotonic()
+ prior_io = disk_io
+ current = dict(state)
+ if current.get('reader_error'):
+ failure = 'progress-reader'
+ break
+ # Xet reconstruction can lag network transfer; either counter counts
+ # as activity. Verification gets a size-aware disk allowance.
+ timeout = max(STALL_SECONDS, row['bytes'] / (8*MiB)) if current['phase'] in ('verify', 'reconstruct') else STALL_SECONDS
+ if time.monotonic() - current['last_activity'] > timeout:
+ failure = 'stalled-transfer'
+ break
+ size_error = stage_overflow(stage, row, selected['part_bytes'])
+ if size_error:
+ failure = 'unexpected-transfer-size'
+ network.event(networking, category=network.model_family(row), source=source,
+ action='invalid-transfer-size', **size_error)
+ break
+ if time.monotonic() - last_emit >= .5:
+ if current.get('backend') and current['backend'] != prior_backend:
+ network.event(networking, category=network.model_family(row), source=source,
+ file=path.name, action='backend', method=current['backend'])
+ prior_backend = current['backend']
+ if current['phase'] == 'verify' and prior_phase != 'verify':
+ sys.stdout.write(json.dumps(dict(event='verify_model', file=str(path), bytes=row['bytes'])) + '\n')
+ sys.stdout.flush()
+ elif current['phase'] == 'reconstruct' and prior_phase != 'reconstruct':
+ sys.stdout.write(json.dumps(dict(event='assemble_model', file=str(path), bytes=row['bytes'])) + '\n')
+ sys.stdout.flush()
+ elif current['phase'] == 'download':
+ elapsed = max(.001, current.get('elapsed_seconds', time.monotonic()-started))
+ progress_callback(min(row['bytes'], current['done_bytes']), row['bytes'], current.get('bytes_per_second', current['done_bytes']/elapsed),
+ network_bytes_per_second=current['transfer_bytes']/elapsed if current['transfer_bytes'] else None,
+ method=current.get('backend'))
+ elif current['phase'] == 'transfer_complete':
+ progress_callback(row['bytes'], row['bytes'], 0,
+ network_bytes_per_second=current.get('transfer_bytes', 0) /
+ max(.001, current.get('elapsed_seconds', time.monotonic()-started)),
+ method=current.get('backend'))
+ prior_phase = current['phase']
+ last_emit = time.monotonic()
+ # The SDK reports the transfer complete before it starts its final
+ # integrity check. Signal outside the throttled UI emission
+ # branch so a short file that exits quickly cannot lose the
+ # pipeline hand-off. Keep this callback one-shot: retries and
+ # duplicate progress lines must not create duplicate workers.
+ if (transfer_complete is not None and not transfer_signalled and
+ current.get('phase') == 'transfer_complete' and
+ current.get('done_bytes', 0) >= row['bytes']):
+ transfer_signalled = True
+ transfer_complete()
+ try:
+ process.wait(timeout=.2)
+ except subprocess.TimeoutExpired:
+ pass
+ except BaseException as error:
+ failure = type(error).__name__
+ raise
+ finally:
+ if process is not None and process.poll() is None:
+ processes.stop(process, grace=6)
+ if reader is not None:
+ reader.join(timeout=3)
+ if reader.is_alive():
+ failure = failure or 'progress-reader-did-not-exit'
+ if process is not None:
+ process.stdout.close()
+ with state_lock:
+ final = dict(state)
+ final.pop('last_activity', None)
+ receipt = dict(source=source, proxy_route=request['proxy_route'], file=row['file'], policy=selected, state=final, promoted=False,
+ failure=failure, returncode=process.returncode if process else None,
+ seconds=time.monotonic()-started, memory=memory.result())
+ if size_error:
+ receipt['size_error'] = size_error
+ save(report / 'result.json', receipt)
+ try:
+ if size_error:
+ raise TransferSizeError(size_error)
+ if failure or process.returncode or final.get('phase') != 'ready':
+ raise RuntimeError(failure or final.get('error_type') or 'incomplete-sdk-transfer')
+ staged = stage / row['file']
+ if fingerprint(staged) != final.get('fingerprint'):
+ raise RuntimeError('verified-stage-changed-before-promotion')
+ if path.exists():
+ raise ValueError('Download destination appeared while downloading; both files retained')
+ path.parent.mkdir(parents=True, exist_ok=True)
+ from .file_ops import publish
+ publish(staged, path) # Same filesystem: no second full-file copy.
+ except Exception as error:
+ receipt['failure'] = safe_log(str(error))
+ save(report / 'result.json', receipt)
+ raise
+ receipt['promoted'] = True
+ save(report / 'result.json', receipt)
+ return final
+
+
+def download(row, path, plan, progress_callback, headers_for, *, transfer_complete=None):
+ from .download_cache import DownloadCache, mount_type, transfer_paths
+ if row['bytes'] >= max(MIN_SDK_BYTES, 64 * MiB) and mount_type(path) in ('tmpfs', 'ramfs'):
+ raise ValueError('Model directory is on a RAM-backed filesystem. Choose a disk directory with --models /path/on/disk (and --encoder-models for the encoder). Existing files retained.')
+ selected = plan.get('model_transfer') or policy(16 * GiB)
+ # A transport-complete notification is intentionally separate from the
+ # function's return: the latter means the pinned hash has been checked.
+ # Keep the notification once per file even when a source/route retry is
+ # needed after the first body finished.
+ if transfer_complete is not None:
+ callback_lock = threading.Lock()
+ callback_state = [False]
+ original_transfer_complete = transfer_complete
+ def transfer_complete():
+ with callback_lock:
+ if callback_state[0]:
+ return
+ callback_state[0] = True
+ original_transfer_complete()
+ cache = DownloadCache(lambda: transfer_paths(path, row), selected.get('file_cache_window_bytes', 256 * MiB))
+ parent_check = (plan.get('network') or {}).get('resource_check')
+ from .download_settings import current, SwitchRequested
+ active=current(plan.get('network') or {})
+ def resource_check():
+ selected=current(plan.get('network') or {})
+ if selected.get('switch_id') != active.get('switch_id'):
+ raise SwitchRequested(selected)
+ if parent_check:
+ parent_check()
+ cache.check()
+ networking = dict(plan.get('network', {}), resource_check=resource_check)
+ retained_manual=retained_payload(stage_directory('manual',row,path),row)
+ if active.get('source') not in (None,'auto') or retained_manual:
+ candidates=network.model_urls(networking,row)
+ # On a rerun, honor the user's last explicit switch rather than the
+ # normal affinity ordering intended for automatic recovery.
+ selected_source=active['source']
+ if selected_source=='auto' and retained_manual:
+ receipt=stage_directory('manual',row,path)/'manual-source.json'
+ previous=json.loads(receipt.read_text()) if receipt.is_file() else {}
+ selected_source=previous.get('source') if previous.get('expected')==(row.get('sha256') or row['git_blob']) else candidates[0][0]
+ if selected_source in [name for name,_ in candidates]:
+ from .transfer_handoff import migrate
+ retained=migrate(row,path,candidates,allow_restart=plan.get('allow_model_restart',False))
+ if path.is_file() and retained['bytes']==row['bytes']:
+ return
+ networking.update(manual_source_switch=True,manual_source=selected_source)
+ plan=dict(plan,manual_ranges=retained['ranges'],model_downloader='auto')
+ try:
+ with cache:
+ while True:
+ try:
+ kwargs = ({'transfer_complete': transfer_complete}
+ if transfer_complete is not None else {})
+ _download(row, path, dict(plan, network=networking), progress_callback, headers_for, **kwargs)
+ break
+ except SwitchRequested as switch:
+ active=switch.value
+ candidates=network.model_urls(networking,row)
+ selected=active['source']
+ if selected=='auto':selected=candidates[0][0]
+ if selected not in [name for name,_ in candidates]:
+ raise RuntimeError('The selected source has no compatible file for '+row['file']+'. Current download stopped; all fragments retained.')
+ networking.update(manual_source_switch=True,manual_source=selected)
+ # run_sdk/curl have joined their workers before this point.
+ # Reuse known byte ranges, including disjoint MS parts.
+ from .transfer_handoff import migrate
+ # An explicit switch applies to this file even when the
+ # old provider has no portable resume map. Preserve those
+ # fragments on disk and report the restart separately.
+ retained=migrate(row,path,candidates,allow_restart=True)
+ plan=dict(plan,manual_ranges=retained['ranges'],model_downloader='auto',allow_model_restart=True)
+ network.event(networking,category=network.model_family(row),source=selected,
+ file=path.name,action='manual-switch',resume_bytes=retained['bytes'],
+ retained_unmapped_bytes=retained.get('unmapped_bytes',0),
+ reason='User selected a new source; stopped the previous worker')
+ if path.is_file() and retained['bytes']==row['bytes']:
+ break
+ finally:
+ network.event(networking, category=network.model_family(row), source='local', file=path.name,
+ action='disk-cache', **cache.result())
+
+
+def _download(row, path, plan, progress_callback, headers_for, *, transfer_complete=None):
+ networking = dict(plan.get('network', {}), allow_model_restart=plan.get('allow_model_restart', False))
+ candidates = network.model_urls(networking, row)
+ family = network.model_family(row)
+ require_xet = plan.get('model_downloader') == 'xet' and row['bytes'] >= MIN_SDK_BYTES
+ if require_xet:
+ candidates = network.model_urls(dict(networking, mode='official'), row)
+ # A new speed probe must not strand yesterday's partially downloaded file.
+ if networking.get('manual_source_switch'):
+ candidates.sort(key=lambda item: item[0] != networking['manual_source'])
+ else:
+ candidates.sort(key=lambda item: -retained_payload(stage_directory(item[0], row, path), row))
+ # Existing contiguous curl partials can be resumed without discarding bytes.
+ partial = path.with_suffix(path.suffix + '.partial')
+ if plan.get('manual_ranges') or (row['bytes'] >= MIN_SDK_BYTES and (require_xet or not partial.exists())):
+ for source_index, (name, _) in enumerate(candidates):
+ resource_failure = False
+ stage = stage_directory('manual', row, path) if plan.get('manual_ranges') else stage_directory(name, row, path)
+ routes = network.route_order(networking, family, name)
+ route_index = 0
+ for attempt in range(SAME_SOURCE_ATTEMPTS):
+ route = routes[route_index]
+ network.event(networking, category=family, source=name, file=path.name, action='attempt',
+ route=route, method='native-sdk', resume_bytes=retained_payload(stage, row))
+ try:
+ kwargs = ({'transfer_complete': transfer_complete}
+ if transfer_complete is not None else {})
+ final = run_sdk(name, row, path, plan, progress_callback,
+ dict(networking, active_route=route), **kwargs)
+ network.source_health(networking, family, name, True)
+ network.route_health(networking, family, name, route)
+ network.event(networking, category=family, source=name, file=path.name, action='verified',
+ method=final.get('backend'), bytes=row['bytes'])
+ return
+ except RuntimeError as error:
+ reason = str(error)
+ retained = retained_payload(stage, row)
+ resource_failure = reason in ('memory-guard', 'low-live-memory-use-streaming')
+ unresumable = reason == 'ResumeUnavailable' or (stage / 'model.xet.incomplete').exists()
+ permanent = reason in ('ValueError', 'ModuleNotFoundError', 'ImportError', 'unexpected-transfer-size')
+ if reason in ('PermissionError', 'OSError'):
+ network.pause_download(networking, name, path, retained, 'local disk or file access failed')
+ if (not resource_failure and not permanent and not (unresumable and retained)
+ and route_index + 1 < len(routes) and attempt + 1 < SAME_SOURCE_ATTEMPTS
+ and (network.connection_failure(reason) or reason in
+ ('ConnectError', 'ConnectTimeout', 'ReadTimeout', 'HTTPError', 'RuntimeError', 'InvalidURL', 'stalled'))):
+ route_index += 1
+ network.event(networking, category=family, source=name, file=path.name, action='route-retry',
+ route=routes[route_index], resume_bytes=retained, reason='connection-failed')
+ continue
+ if require_xet:
+ network.event(networking, category=family, source=name, file=path.name,
+ action='failed', method='hf-xet', reason=reason)
+ raise RuntimeError('Required HF Xet download failed (%s); no fallback performed; all files retained' % error) from error
+ next_source = source_index + 1 < len(candidates)
+ if not resource_failure and not unresumable and not permanent and not next_source and attempt+1 < SAME_SOURCE_ATTEMPTS:
+ network.retry_wait(networking, attempt, category=family, source=name, file=path.name,
+ resume_bytes=retained, reason=reason)
+ continue
+ if retained:
+ if next_source and not (resource_failure or unresumable or permanent):
+ from .transfer_handoff import migrate
+ fragments = migrate(row, path, candidates, allow_restart=False)
+ if path.is_file() and fragments['bytes'] == row['bytes']:
+ return
+ plan = dict(plan, manual_ranges=fragments['ranges'])
+ elif not plan.get('allow_model_restart') or resource_failure:
+ network.pause_download(networking, name, path, retained, 'xet-or-legacy-resume-unavailable' if unresumable else reason,
+ details=getattr(error, 'details', None))
+ else:
+ network.event(networking, category=family, source=name, file=path.name,
+ action='restart-approved', retained_bytes=retained, reason=reason)
+ network.retain_partial(stage, 'restart-approved')
+ if not resource_failure:
+ network.source_health(networking, family, name, False)
+ network.event(networking, category=family, source=name, file=path.name, action='fallback',
+ method='native-sdk', reason=reason)
+ break
+ if resource_failure:
+ break # Another SDK cannot solve the same machine RAM pressure.
+ # The curl fallback verifies and publishes before returning, so there is
+ # no separate closed-but-unverified boundary to expose here. Keep the
+ # callback limited to the SDK path, where the worker emits
+ # ``transfer_complete`` after its provider has closed the staged file.
+ network.download(candidates, path, row.get('sha256') or row['git_blob'], progress_callback,
+ network=networking, size=row['bytes'], headers_for=headers_for,
+ algorithm='sha256' if row.get('sha256') else 'sha1', git_blob=not row.get('sha256'), category=family)
+
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--worker', type=Path, required=True)
+ args = parser.parse_args()
+ with processes.worker_signals():
+ sys.exit(sdk_worker(json.loads(args.worker.read_text(encoding='utf-8'))))
diff --git a/freevideo_engine/modern_launcher.py b/freevideo_engine/modern_launcher.py
new file mode 100644
index 0000000..84b47f7
--- /dev/null
+++ b/freevideo_engine/modern_launcher.py
@@ -0,0 +1,344 @@
+"""Qt Quick entry point. Importing this module does not import Qt or Torch."""
+import json
+import os
+from pathlib import Path
+import sys
+import time
+import traceback
+
+
+def theme():
+ """branding.py's palette for QML, so the launcher and ComfyUI views match.
+
+ QML reads '#AARRGGBB'; translucent values are derived from the table
+ rather than added as new shades. Pixel sizes are Qt Quick's own scale.
+ """
+ from . import branding as b
+ return dict(
+ bg=b.BACKGROUND, canvas=b.CANVAS, surface=b.SURFACE, raised=b.RAISED, hover=b.HOVER,
+ border=b.BORDER, sheen=b.SHEEN, text=b.TEXT, muted=b.MUTED, disabled=b.DISABLED,
+ accent=b.ACCENT, accentHover=b.ACCENT_HOVER, accentRamp=b.ACCENT_RAMP,
+ accentDim=b.ACCENT_DIM, accentSubtle=b.ACCENT_SUBTLE,
+ success=b.SUCCESS, successSubtle=b.SUCCESS_SUBTLE, warning=b.WARNING,
+ danger=b.DANGER, dangerSubtle=b.DANGER_SUBTLE, dangerLine='#66' + b.DANGER[1:],
+ scrim='#cc' + b.BACKGROUND[1:], shadow='#66000000',
+ micro=12, body=14, strong=16, section=20, hero=30,
+ # Radius grows with the object: glyph, control, card, sheet; heights
+ # are compact, standard and the one main action, as in theme.css.
+ radiusXs=3, radiusSm=b.RADIUS_SM, radiusMd=b.RADIUS_MD, radiusLg=b.RADIUS_LG,
+ heightSm=32, height=36, heightLg=44, mono=b.MONO)
+
+
+def create_ui(session, *, show=True):
+ from .windows_ux import taskbar_identity
+ taskbar_identity()
+ from PySide6.QtCore import QObject, Property, QTimer, QUrl, Signal, Slot, Qt
+ from PySide6.QtGui import QFont, QIcon
+ from PySide6.QtWidgets import QApplication, QFileDialog
+ from PySide6.QtQml import QQmlApplicationEngine
+ from PySide6.QtQuick import QQuickWindow, QSGRendererInterface
+ from PySide6.QtQuickControls2 import QQuickStyle
+ # The launcher must not consume the GPU used for video generation.
+ QQuickWindow.setGraphicsApi(QSGRendererInterface.Software)
+ QQuickStyle.setStyle('Basic')
+ app = QApplication.instance() or QApplication([sys.argv[0]])
+ app.setApplicationName('FreeVideo')
+ app.setOrganizationName('FreeVideo')
+ app.styleHints().setColorScheme(Qt.ColorScheme.Dark)
+ font = QFont()
+ font.setFamilies(['Segoe UI', 'Microsoft YaHei UI', 'Noto Sans', 'Noto Sans CJK SC', 'sans-serif'])
+ font.setPointSize(10)
+ app.setFont(font)
+ icon = QIcon(str(Path(__file__).parent / 'assets/icon.ico'))
+ if icon.isNull():
+ icon = QIcon(str(Path(__file__).parent / 'assets/icon.png'))
+ app.setWindowIcon(icon)
+
+ class Bridge(QObject):
+ changed = Signal()
+ def __init__(self):
+ super().__init__(engine)
+ self.value = session.snapshot()
+ self.timer = QTimer(self)
+ self.timer.setInterval(250)
+ self.timer.timeout.connect(self.refresh)
+ self.timer.start()
+
+ @Property('QVariantMap', notify=changed)
+ def state(self):
+ return self.value
+
+ def invoke(self, function, *args):
+ try:
+ function(*args)
+ except Exception:
+ session.error = traceback.format_exc()
+ self.refresh()
+
+ @Slot()
+ def refresh(self):
+ try:
+ session.tick()
+ value = session.snapshot()
+ if value != self.value:
+ self.value = value
+ self.changed.emit()
+ if session.closing:
+ self.timer.stop()
+ app.quit()
+ except Exception:
+ # A failed status read must remain copyable and not kill the UI.
+ session.error = traceback.format_exc()
+ self.value = dict(self.value, error=session.error)
+ self.changed.emit()
+
+ @Slot(str, 'QVariant')
+ def edit(self, key, value):
+ self.invoke(session.edit, key, value)
+
+ @Slot(str, bool)
+ def action(self, name, accepted=False):
+ self.invoke(session.action, name, accepted)
+
+ @Slot(str)
+ def browse(self, key):
+ if session.controller.busy:
+ return
+ path = QFileDialog.getExistingDirectory(None, session.t('Choose folder', '选择文件夹'),
+ str(session.form.get(key) or ''), QFileDialog.ShowDirsOnly)
+ if path:
+ self.invoke(session.add_folder, path) if key == 'model_dirs' else self.invoke(session.edit, key, path)
+
+ @Slot()
+ def browsePackages(self):
+ if session.controller.busy or session.importer.busy:
+ return
+ paths, _ = QFileDialog.getOpenFileNames(None, session.t('Import offline packages', '导入离线包'),
+ '', 'FreeVideo (*.zip)')
+ if paths:
+ self.invoke(session.import_packages, paths)
+
+ @Slot()
+ def clearRuntime(self):
+ self.invoke(session.clear_runtime)
+
+ @Slot('QVariantList')
+ def importPackages(self, urls):
+ paths = []
+ for value in urls:
+ url = value if isinstance(value, QUrl) else QUrl(str(value))
+ if not url.isLocalFile():
+ session.error = session.t('Drop downloaded ZIP files here.', '请拖入已下载到本机的 ZIP 文件。')
+ self.refresh()
+ return
+ paths.append(url.toLocalFile())
+ if paths:
+ self.invoke(session.import_packages, paths)
+
+ @Slot(int)
+ def removeFolder(self, index):
+ self.invoke(session.remove_folder, index)
+
+ @Slot(str)
+ def copy(self, value):
+ app.clipboard().setText(value)
+
+ @Slot(str)
+ def link(self, value):
+ from .windows_ux import open_browser
+ self.invoke(open_browser, value)
+
+ @Slot(str)
+ def selectSource(self, value):
+ self.invoke(session.select_source, value)
+
+ @Slot(str)
+ def selectProxyMode(self, value):
+ self.invoke(session.select_proxy_mode, value)
+
+ @Slot()
+ def speedTest(self):
+ self.invoke(session.speed_test)
+
+ @Slot(int, bool)
+ def compatibility(self, level, automatic):
+ self.invoke(session.set_compatibility, level, automatic)
+
+ @Slot(str)
+ def update(self, token):
+ self.invoke(session.update, token)
+
+ @Slot()
+ def dismissUpdate(self):
+ self.invoke(session.dismiss_update)
+
+ @Slot(str)
+ def checkUpdates(self, token):
+ self.invoke(session.check_update, token)
+
+ @Slot(str)
+ def terminal(self, path):
+ session.log_source = path
+ self.refresh()
+
+ @Slot()
+ def clearTerminal(self):
+ session.tail.clear()
+ self.refresh()
+
+ @Slot()
+ def dismissNotice(self):
+ self.invoke(session.dismiss_notice)
+
+ @Slot()
+ def close(self):
+ self.invoke(session.close)
+
+ @Slot()
+ def shutdown(self):
+ self.timer.stop()
+ session.close()
+
+ from .model_guidance import links, cloud_models
+ from .launcher_copy import display
+ engine = QQmlApplicationEngine()
+ bridge = Bridge()
+ engine.rootContext().setContextProperty('backend', bridge)
+ engine.rootContext().setContextProperty('initialState', bridge.value)
+ # Nested Python tuples remain opaque objects in QML. Named maps become
+ # QVariantMaps, so both the displayed label and clicked URL are available.
+ engine.rootContext().setContextProperty('modelLinks', {
+ component: [dict(label=label, label_zh=display(label, True), url=url) for label, url in rows]
+ for component, rows in links().items()})
+ engine.rootContext().setContextProperty('cloudLinks', cloud_models())
+ engine.rootContext().setContextProperty('theme', theme())
+ engine.load(QUrl.fromLocalFile(str(Path(__file__).parent / 'launcher/Main.qml')))
+ if not engine.rootObjects():
+ bridge.timer.stop()
+ raise RuntimeError('FreeVideo desktop layout could not be loaded')
+ window = engine.rootObjects()[0]
+ window.setIcon(icon)
+ available = app.primaryScreen().availableGeometry()
+ width = min(1440, int(available.width() * .86))
+ height = min(1020, int(available.height() * .88))
+ window.setMinimumWidth(min(780, width))
+ window.setMinimumHeight(min(540, height))
+ window.resize(width, height)
+ window.setPosition(available.x()+(available.width()-width)//2, available.y()+(available.height()-height)//2)
+ window.setVisible(show)
+ app.aboutToQuit.connect(bridge.shutdown)
+ # Keep Python-owned objects alive as long as the QML engine.
+ engine.bridge = bridge
+ engine.session = session
+ return app, engine, window, bridge
+
+
+def smoke_test(app, engine, window, bridge, destination):
+ """Exercise the packaged QML, manifests and managed-Python entry point."""
+ import queue
+ from PySide6.QtCore import QTimer
+ from .desktop_runtime import check_launcher_payload, check_launcher_reopen
+ from .comfy_launcher_runtime import LauncherRunner
+ from .launcher_update import current_build
+ from .monitoring import save
+ destination = Path(destination)
+ destination.mkdir(parents=True, exist_ok=True)
+ session = engine.session
+ try:
+ payload = check_launcher_payload(session.source, destination)
+ payload['reopen'] = check_launcher_reopen(destination)
+ from PySide6.QtQuick import QQuickItem
+ for page in ('comfy', 'models', 'progress', 'launcher'):
+ session.page = page
+ bridge.refresh()
+ app.processEvents()
+ primary = window.findChild(QQuickItem, 'primaryButton')
+ if primary is None or not primary.isVisible() or primary.width() < 40:
+ raise RuntimeError('Packaged launcher action is not visible: ' + page)
+ window.setProperty('settingsOpen', True)
+ app.processEvents()
+ window.setProperty('settingsOpen', False)
+ session.page = 'comfy'
+ bridge.refresh()
+ payload.update(wizard_pages=3, launcher_page=True, download_controls=True, renderer='Qt Quick/software')
+ build = current_build()
+ if getattr(sys, 'frozen', False) and build is None:
+ raise ValueError('Packaged launcher update identity is missing')
+ events = queue.Queue()
+ runner = LauncherRunner(session.source, events, destination / 'help')
+ runner.start('help', destination / 'engine', ['--help'])
+ except Exception:
+ save(destination / 'smoke.json', dict(success=False, error=traceback.format_exc()))
+ app.quit()
+ return
+ deadline = time.monotonic() + 240
+ timer = QTimer(bridge)
+ def finish():
+ if runner.busy and time.monotonic() < deadline:
+ return
+ result = None
+ while not events.empty():
+ kind, value = events.get_nowait()
+ if kind == 'done':
+ result = value
+ imported = [p for p in ('torch', 'triton', 'transformers', 'tkinter') if p in sys.modules]
+ save(destination / 'smoke.json', dict(success=bool(result and result['status'] == 'complete' and not imported),
+ task=result, interface='Qt Quick launcher', build=build, source=str(session.source),
+ payload=payload, model_modules_imported=imported))
+ window.grabWindow().save(str(destination / 'launcher.png'))
+ if runner.busy:
+ runner.cancel()
+ timer.stop()
+ app.quit()
+ timer.timeout.connect(finish)
+ timer.start(100)
+
+
+def main(session=None):
+ # Linux process supervision reexecutes sys.executable. A frozen GUI must
+ # dispatch this helper before creating another application window.
+ if (getattr(sys, 'frozen', False) and sys.platform == 'linux'
+ and sys.argv[1:4] == ['-B', '-m', 'freevideo_engine.linux_process_host']):
+ from .linux_process_host import main as supervise
+ sys.argv = [sys.argv[3], *sys.argv[4:]]
+ raise SystemExit(supervise())
+ smoke = '--smoke-test' in sys.argv
+ from .desktop_runtime import launcher_root
+ portable_root = Path(sys.executable).parent
+ if session is None and getattr(sys, 'frozen', False) and (portable_root / 'portable.json').is_file() and not smoke:
+ from .portable_launcher import portable_session
+ session = portable_session(portable_root)
+ if not smoke and session is None:
+ from .launcher_update import current_build, forward_approved
+ identity = current_build()
+ if identity and forward_approved(identity, launcher_root()):
+ return
+ if os.name == 'nt':
+ # Console helpers inherit one hidden console instead of opening CMD windows.
+ import ctypes
+ lib = ctypes.windll.kernel32
+ lib.GetConsoleWindow.restype = ctypes.c_void_p
+ if not lib.GetConsoleWindow():
+ lib.AllocConsole()
+ ctypes.windll.user32.ShowWindow(ctypes.c_void_p(lib.GetConsoleWindow()), 0)
+ from .launcher_session import Session
+ from .launcher_settings import Store
+ if session is None:
+ session = Session(smoke=smoke, store=Store(launcher_root(),
+ documents=launcher_root() / 'smoke-documents' if smoke else None))
+ app, engine, window, bridge = create_ui(session)
+ if smoke:
+ from PySide6.QtCore import QTimer
+ QTimer.singleShot(0, lambda: smoke_test(app, engine, window, bridge,
+ sys.argv[sys.argv.index('--smoke-test')+1]))
+ try:
+ app.exec()
+ finally:
+ bridge.shutdown()
+ # Destroy the QML tree before its Python context properties.
+ engine.deleteLater()
+ app.processEvents()
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freevideo_engine/monitoring.py b/freevideo_engine/monitoring.py
new file mode 100644
index 0000000..5b2b31f
--- /dev/null
+++ b/freevideo_engine/monitoring.py
@@ -0,0 +1,197 @@
+"""Whole-device NVML sampling; values include driver reservations."""
+import csv
+import ctypes
+import json
+import os
+from pathlib import Path
+import threading
+import time
+import tempfile
+from .system import windows
+
+
+def save(path, value):
+ path = Path(path)
+ path.parent.mkdir(parents=True, exist_ok=True)
+ # Unique, closed staging file; Windows readers/antivirus may briefly hold
+ # the destination. Keep the previous good report and the failed staging file.
+ with tempfile.NamedTemporaryFile(mode='w', encoding='utf-8', newline='\n',
+ prefix=path.name + '.', suffix='.tmp', dir=path.parent, delete=False) as stream:
+ temporary = Path(stream.name)
+ stream.write(json.dumps(value, ensure_ascii=False, indent=2) + '\n')
+ for attempt in range(8):
+ try:
+ temporary.replace(path)
+ return
+ except PermissionError:
+ if not windows() or attempt == 7:
+ raise
+ time.sleep(.1 * (attempt + 1))
+
+
+class Memory(ctypes.Structure):
+ _fields_ = [('total', ctypes.c_ulonglong), ('free', ctypes.c_ulonglong),
+ ('used', ctypes.c_ulonglong)]
+
+
+class Utilization(ctypes.Structure):
+ _fields_ = [('gpu', ctypes.c_uint), ('memory', ctypes.c_uint)]
+
+
+class Activity:
+ """Constant-space driver observations; unsupported sensors remain unknown."""
+ fields = ('gpu_util_percent', 'temperature_c', 'power_mw', 'sm_clock_mhz', 'memory_clock_mhz')
+ clock_reasons = {'sw_power_cap': 0x04, 'hw_slowdown': 0x08,
+ 'sw_thermal': 0x20, 'hw_thermal': 0x40, 'hw_power_brake': 0x80}
+
+ def __init__(self):
+ self.samples = 0
+ self.values = {}
+ self.reason_samples = 0
+ self.reason_counts = {key: 0 for key in self.clock_reasons}
+ self.reason_mask = 0
+
+ def add(self, util, auxiliary, reasons):
+ self.samples += 1
+ for name, value in zip(self.fields, (util, *auxiliary)):
+ if type(value) not in (int, float) or not 0 <= value < float('inf'):
+ continue
+ row = self.values.setdefault(name, dict(samples=0, total=0, minimum=value, maximum=value))
+ row['samples'] += 1
+ row['total'] += value
+ row['minimum'] = min(row['minimum'], value)
+ row['maximum'] = max(row['maximum'], value)
+ if type(reasons) is int and reasons >= 0:
+ self.reason_samples += 1
+ self.reason_mask |= reasons
+ for key, mask in self.clock_reasons.items():
+ self.reason_counts[key] += bool(reasons & mask)
+
+ def result(self):
+ result = dict(samples=self.samples, clock_reason_samples=self.reason_samples)
+ for name, row in self.values.items():
+ result[name] = dict(samples=row['samples'], minimum=row['minimum'], maximum=row['maximum'],
+ mean=row['total']/row['samples'])
+ if self.reason_samples:
+ result['clock_reason_mask'] = self.reason_mask
+ result['clock_reason_counts'] = dict(self.reason_counts)
+ return result
+
+
+class Monitor:
+ def __init__(self, path, interval=0.05, device=None):
+ if windows():
+ candidates = [Path(os.environ.get('SystemRoot', r'C:\Windows')) / 'System32/nvml.dll',
+ Path(os.environ.get('ProgramFiles', r'C:\Program Files')) / 'NVIDIA Corporation/NVSMI/nvml.dll']
+ library = next((p for p in candidates if p.is_file()), None)
+ if library is None:
+ raise RuntimeError('NVML DLL is missing from the NVIDIA driver installation; repair the driver and rerun test.')
+ self.lib = ctypes.CDLL(str(library))
+ else:
+ self.lib = ctypes.CDLL('libnvidia-ml.so.1')
+ rc = self.lib.nvmlInit_v2()
+ if rc:
+ raise RuntimeError(f'NVML initialization returned {rc}')
+ self.handle = ctypes.c_void_p()
+ device = str(device if device is not None else os.environ.get('CUDA_VISIBLE_DEVICES', '0').split(',')[0])
+ if device.startswith('GPU-'):
+ rc = self.lib.nvmlDeviceGetHandleByUUID(device.encode(), ctypes.byref(self.handle))
+ else:
+ rc = self.lib.nvmlDeviceGetHandleByIndex_v2(int(device), ctypes.byref(self.handle))
+ if rc:
+ self.lib.nvmlShutdown()
+ raise RuntimeError(f'NVML could not select {device}: {rc}')
+ self.device = device
+ self.path, self.interval = Path(path), interval
+ self.stop_event = threading.Event()
+ self.peak = 0
+ self.baseline = 0
+ self.last = 0
+ self.count = 0
+ self.errors = []
+ self.max_temperature = None
+ self.max_power_mw = None
+ self.activity, self.busy_activity = Activity(), Activity()
+
+ def sample(self):
+ m, u = Memory(), Utilization()
+ rc = self.lib.nvmlDeviceGetMemoryInfo(self.handle, ctypes.byref(m))
+ if rc:
+ raise RuntimeError(f'NVML memory read returned {rc}')
+ if m.used > m.total or not m.total:
+ raise RuntimeError('Driver returned unavailable NVML device memory; no zero/estimated peak will be reported.')
+ try:
+ rc = self.lib.nvmlDeviceGetUtilizationRates(self.handle, ctypes.byref(u))
+ except AttributeError:
+ rc = -1
+ return m.used, m.total, u.gpu if rc == 0 and u.gpu <= 100 else None
+
+ def start(self):
+ self.baseline = self.sample()[0]
+ self.peak = self.baseline
+ self.thread = threading.Thread(target=self.loop, daemon=True)
+ self.thread.start()
+ return self
+
+ def auxiliary(self):
+ values = []
+ for name, extra in [('nvmlDeviceGetTemperature', (0,)), ('nvmlDeviceGetPowerUsage', ()),
+ ('nvmlDeviceGetClockInfo', (1,)), ('nvmlDeviceGetClockInfo', (2,))]:
+ value = ctypes.c_uint()
+ try:
+ rc = getattr(self.lib, name)(self.handle, *extra, ctypes.byref(value))
+ values.append(value.value if rc == 0 else None)
+ except AttributeError:
+ values.append(None)
+ return values
+
+ def clock_reasons(self):
+ for name in ('nvmlDeviceGetCurrentClocksEventReasons', 'nvmlDeviceGetCurrentClocksThrottleReasons'):
+ try:
+ value = ctypes.c_ulonglong()
+ if getattr(self.lib, name)(self.handle, ctypes.byref(value)) == 0:
+ return value.value
+ except AttributeError:
+ pass
+ return None
+
+ def loop(self):
+ with self.path.open('w', buffering=1, encoding='utf-8') as f:
+ writer = csv.writer(f)
+ writer.writerow(['epoch_seconds', 'monotonic_seconds', 'used_bytes', 'total_bytes', 'gpu_util_percent',
+ 'temperature_c', 'power_mw', 'sm_clock_mhz', 'memory_clock_mhz', 'clock_reason_mask'])
+ while not self.stop_event.is_set():
+ try:
+ used, total, util = self.sample()
+ self.peak = max(self.peak, used)
+ self.last = used
+ self.count += 1
+ auxiliary = self.auxiliary()
+ reasons = self.clock_reasons()
+ self.activity.add(util, auxiliary, reasons)
+ if util is not None and util >= 50:
+ self.busy_activity.add(util, auxiliary, reasons)
+ if auxiliary[0] is not None:
+ self.max_temperature = max(self.max_temperature or 0, auxiliary[0])
+ if auxiliary[1] is not None:
+ self.max_power_mw = max(self.max_power_mw or 0, auxiliary[1])
+ writer.writerow([time.time(), time.monotonic(), used, total, util, *auxiliary, reasons])
+ except Exception as e:
+ if str(e) not in self.errors and len(self.errors) < 20:
+ self.errors.append(str(e))
+ self.stop_event.wait(self.interval)
+
+ def stop(self):
+ self.stop_event.set()
+ self.thread.join(timeout=5)
+ self.lib.nvmlShutdown()
+ return {'device': self.device, 'gpu_peak_bytes': self.peak, 'gpu_peak_mib': self.peak / 2**20,
+ 'gpu_baseline_bytes': self.baseline, 'gpu_last_bytes': self.last,
+ 'max_temperature_c': self.max_temperature, 'max_power_mw': self.max_power_mw,
+ 'activity': self.activity.result(), 'busy_activity': self.busy_activity.result(),
+ 'busy_util_threshold_percent': 50,
+ 'activity_scope': 'Whole device during this worker request, including load and decode. '
+ 'Sample means, not duration-weighted measurements. Busy subset has GPU utilization >= 50%; '
+ 'clock reasons are driver observations, not attribution of elapsed time.',
+ 'sampling_interval_seconds': self.interval, 'samples': self.count,
+ 'sampling_errors': self.errors, 'sample_csv': str(self.path)}
diff --git a/freevideo_engine/network.py b/freevideo_engine/network.py
new file mode 100644
index 0000000..f1cb0ea
--- /dev/null
+++ b/freevideo_engine/network.py
@@ -0,0 +1,881 @@
+"""Bounded route probes, ranked mirrors and resumable verified downloads.
+
+Only curl and Python stdlib are needed before setup. Proxy and direct routes are
+compared without changing user settings. Reports never include proxy credentials.
+"""
+from concurrent.futures import ThreadPoolExecutor, as_completed
+import hashlib
+import html
+import json
+import math
+import os
+import sys
+from pathlib import Path
+import re
+import shutil
+import subprocess
+from .windows_ux import hidden_console
+import threading
+import time
+from urllib.parse import quote, unquote, urlencode, urljoin, urlsplit
+from . import proxy
+
+
+SOURCES = {
+ 'pypi': {'official': 'https://pypi.org/simple', 'tuna': 'https://pypi.tuna.tsinghua.edu.cn/simple'},
+ 'torch': {'official': 'https://download.pytorch.org/whl', 'nju': 'https://mirrors.nju.edu.cn/pytorch/whl'},
+ 'models': {'official': 'https://huggingface.co', 'hf-mirror': 'https://hf-mirror.com'},
+ 'vdn-models': {'official': 'https://huggingface.co', 'hf-mirror': 'https://hf-mirror.com',
+ 'modelscope': 'https://www.modelscope.cn'},
+ 'edge-models': {'official': 'https://huggingface.co', 'hf-mirror': 'https://hf-mirror.com',
+ 'modelscope': 'https://modelscope.ai'},
+ 'github': {'official': 'https://github.com', 'ghfast': 'https://ghfast.top/https://github.com'},
+ 'cuda': {'official': 'https://developer.download.nvidia.com/compute/cuda/redist',
+ 'nvidia-cn': 'https://developer.download.nvidia.cn/compute/cuda/redist'},
+}
+PROXY_KEYS = proxy.PROXY_KEYS
+SAMPLE_BYTES = 256 * 1024
+SPEED_SAMPLE_BYTES = 32 * 2**20
+SPEED_SAMPLE_SECONDS = 3.
+MIN_TRANSFER_LIMIT = 1024 * 1024
+
+
+def proxy_environment(environ=None):
+ return proxy.inherited(environ)
+
+
+def configured_source(family, env):
+ names = {'pypi': ('UV_DEFAULT_INDEX', 'UV_INDEX_URL', 'PIP_INDEX_URL'),
+ 'models': ('HF_ENDPOINT',), 'vdn-models': ('HF_ENDPOINT',), 'edge-models': ('HF_ENDPOINT',),
+ 'github': ('FREEVIDEO_GITHUB_MIRROR',)}.get(family, ())
+ return next((env[k].rstrip('/') for k in names if env.get(k)), None)
+
+
+def source_url(family, name, env=None):
+ family = 'torch' if family.startswith('torch-') else 'github' if family == 'git' else family
+ env = os.environ if env is None else env
+ if name == 'user':
+ value = configured_source(family, env)
+ if not value:
+ raise ValueError('The user-configured source is no longer set in the environment')
+ return value
+ return SOURCES[family][name]
+
+
+def model_spec(spec=None):
+ return (spec if spec is not None else
+ json.loads(Path(__file__).with_name('dependencies.json').read_text(encoding='utf-8')))['models']
+
+
+def modelscope_mapping(row, spec=None):
+ """Return only a published, verified HF-to-ModelScope revision pair."""
+ settings = model_spec(spec)
+ # A ModelScope commit is not a Hugging Face commit. Only the verified pair
+ # shares paths/content; an encoder or a future HF pin needs its own route.
+ for prefix in ('vdn', 'edge'):
+ mirror = settings.get(prefix + '_modelscope', {})
+ if (mirror and row['repo'] == settings.get(prefix + '_repo', mirror['repo']) and
+ row['revision'] == settings.get(prefix + '_revision', mirror['hf_revision']) == mirror['hf_revision']):
+ return dict(mirror, family=prefix + '-models')
+ # Additional formats in the same repository keep independent immutable
+ # pins. Never move old installations just because a new variant exists.
+ for variant in settings.get(prefix + '_modelscope_variants', {}).values():
+ if row['repo'] == variant['repo'] and row['revision'] == variant['hf_revision']:
+ return dict(variant, family=prefix + '-models')
+ return None
+
+
+def model_family(row, spec=None):
+ mirror = modelscope_mapping(row, spec)
+ return mirror['family'] if mirror else 'models'
+
+
+def model_url(row, name, env=None, *, spec=None):
+ if name == 'modelscope':
+ mirror = modelscope_mapping(row, spec)
+ if not mirror:
+ raise ValueError('No verified ModelScope mapping for this model revision')
+ return (source_url(mirror['family'], name, env) + '/api/v1/models/' + quote(mirror['repo'], safe='/') +
+ '/repo?' + urlencode({'Revision': mirror['revision'], 'FilePath': row['file']}))
+ return (source_url('models', name, env) + '/' + quote(row['repo'], safe='/') +
+ '/resolve/' + quote(row['revision'], safe='') + '/' + quote(row['file'], safe='/'))
+
+
+def model_urls(network, row, env=None, *, spec=None):
+ family = model_family(row, spec)
+ # Plans made before ModelScope support keep their existing HF ordering.
+ if family not in network.get('sources', {}):
+ family = 'models'
+ names = ['official'] if network.get('mode') == 'official' or row.get('official_only') else ordered(network, family)
+ return [(name, model_url(row, name, env, spec=spec)) for name in names]
+
+
+def curl_config(url, headers=()):
+ def quote(value):
+ if any(c in str(value) for c in ('\n', '\r', '\x00')):
+ raise ValueError('Invalid newline in network configuration')
+ return '"' + str(value).replace('\\', '\\\\').replace('"', '\\"') + '"'
+ return ('url = ' + quote(url) + '\n' + ''.join('header = ' + quote(h) + '\n' for h in headers)).encode()
+
+
+def curl_command(timeout, env=None):
+ if not shutil.which('curl'):
+ raise RuntimeError('Network setup needs curl (Ubuntu/Debian: sudo apt install curl).')
+ command = ['curl', '--config', '-', '--silent', '--show-error', '--no-location-trusted', '--location',
+ '--max-redirs', '5', '--fail', '--proto', '=https,http', '--proto-redir', '=https,http',
+ '--connect-timeout', str(timeout), '--user-agent', 'FreeVideo-Setup/1']
+ if (env or {}).get('FREEVIDEO_PROXY_ROUTE') == 'direct':
+ command += ['--proxy', '', '--noproxy', '*']
+ return command
+
+
+def sample(url, timeout=5, limit=SAMPLE_BYTES, *, ranged=True, env=None, headers=()):
+ """Read at most limit bytes; a hard curl deadline bounds DNS and slow reads."""
+ command = curl_command(timeout, env) + ['--max-time', str(timeout)]
+ if url.startswith('https://'):
+ command += ['--proto-redir', '=https']
+ if ranged:
+ command += ['--range', '0-' + str(limit - 1)]
+ config = curl_config(url, headers)
+ started = time.monotonic()
+ process = subprocess.Popen(command, env=proxy_environment(env), stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, **hidden_console())
+ try:
+ process.stdin.write(config)
+ process.stdin.close()
+ data = process.stdout.read(limit)
+ bounded = len(data) == limit
+ if bounded and process.poll() is None:
+ process.terminate()
+ code = process.wait(timeout=timeout + 2)
+ if (code and not bounded) or not data:
+ raise RuntimeError('HTTP probe failed (curl %s)' % code)
+ return data, time.monotonic() - started
+ finally:
+ if process.poll() is None:
+ process.kill()
+ process.wait()
+ process.stdout.close()
+
+
+def speed_sample(url, timeout=5, limit=SPEED_SAMPLE_BYTES, duration=SPEED_SAMPLE_SECONDS, *, env=None, headers=()):
+ """Time a bounded body transfer separately from connection/redirect startup.
+
+ Retain only the first chunk for content validation. Curl bounds startup;
+ a timer also ends a stalled body without relying on another byte arriving.
+ A time/byte-limited sample is intentional, a disconnected transfer is not.
+ """
+ command = curl_command(timeout, env) + ['--no-buffer', '--max-time', str(timeout + duration),
+ '--range', '0-' + str(limit - 1)]
+ if url.startswith('https://'):
+ command += ['--proto-redir', '=https']
+ started = time.monotonic()
+ process = subprocess.Popen(command, env=proxy_environment(env), stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, **hidden_console())
+ timer = None
+ stopped = []
+
+ def stop(reason):
+ if not stopped:
+ stopped.append((reason, time.monotonic()))
+ if process.poll() is None:
+ try:
+ process.terminate()
+ except OSError:
+ pass # The process may have exited between poll and terminate.
+
+ try:
+ process.stdin.write(curl_config(url, headers))
+ process.stdin.close()
+ first = process.stdout.read(1)
+ body_started = time.monotonic()
+ prefix = first
+ count = len(first)
+ last_body = body_started
+ timer = threading.Timer(duration, stop, args=('time-limit',))
+ timer.daemon = True
+ timer.start()
+ while count < limit:
+ block = process.stdout.read(min(64 * 1024, limit - count))
+ if not block:
+ break
+ count += len(block)
+ if len(prefix) < 64 * 1024:
+ prefix += block[:64 * 1024-len(prefix)]
+ last_body = time.monotonic()
+ finished = time.monotonic()
+ if count == limit:
+ stop('byte-limit')
+ code = process.wait(timeout=2)
+ if not prefix or (code and not stopped and code != 28):
+ raise RuntimeError('HTTP speed probe failed (curl %s)' % code)
+ ended = min(finished, stopped[0][1]) if stopped else finished
+ seconds = max(0., ended - body_started)
+ measured = count - len(first)
+ # Tiny files/bursts establish connectivity but cannot rank bandwidth.
+ reliable = measured >= 256 * 1024 and (seconds >= .2 or measured >= 4 * 2**20)
+ return prefix, dict(bytes=count, startup_seconds=body_started-started, ttfb_seconds=body_started-started,
+ transfer_seconds=seconds, seconds=finished-started, measured_bytes=measured,
+ bytes_per_second=measured / max(.001, seconds) if reliable else None,
+ body_stalled=ended-last_body > max(.5, duration/2),
+ speed_measured=reliable, stop_reason=stopped[0][0] if stopped else ('deadline' if code == 28 else 'eof'),
+ scope='Bounded single-connection HTTP body transfer; startup excluded; SDK/Xet concurrency not measured')
+ finally:
+ if timer is not None:
+ timer.cancel()
+ timer.join()
+ if process.poll() is None:
+ process.kill()
+ process.wait()
+ if not process.stdin.closed:
+ process.stdin.close()
+ process.stdout.close()
+
+
+def wheel_link(index, pattern, timeout, env):
+ data, _ = sample(index, timeout, 2 * 2**20, ranged=False, env=env)
+ links = [urljoin(index, html.unescape(value)) for value in re.findall(r'href=[\'"]([^\'"]+)', data.decode(errors='replace'))]
+ matched = [link for link in links if re.search(pattern, unquote(urlsplit(link).path).rsplit('/', 1)[-1])]
+ if not matched:
+ raise RuntimeError('Mirror does not list the required wheel')
+ return matched[-1]
+
+
+def probe(family, name, spec, versions, timeout, env, *, measure_speed=False):
+ base = source_url(family, name, env)
+ started = time.monotonic()
+ kind = 'artifact range'
+ try:
+ if family == 'pypi':
+ url = wheel_link(base + '/packaging/', r'^packaging-26\.3-py3-none-any\.whl$', timeout, env)
+ elif family.startswith('torch-'):
+ _, cuda, version = family.split('-')
+ target = r'win_amd64' if versions.get('target_system') == 'Windows' else r'(?:manylinux[^/]*|linux)_x86_64'
+ url = wheel_link(base + '/' + cuda + '/torch/', r'^torch-' + re.escape(version + '+' + cuda) + r'-cp312-cp312-' + target + r'\.whl$', timeout, env)
+ elif family == 'models':
+ settings = spec['models']
+ url = model_url({'repo': settings['encoder_repo'], 'revision': settings['encoder_revision'],
+ 'file': settings['encoder_file']}, name, env, spec=spec)
+ elif family in ('vdn-models', 'edge-models'):
+ settings = spec['models']
+ mirror = settings[family.split('-')[0] + '_modelscope']
+ url = model_url({'repo': mirror['repo'], 'revision': mirror['hf_revision'],
+ 'file': mirror['probe_file']}, name, env, spec=spec)
+ elif family == 'github':
+ url = versions['uv']['url'].replace(SOURCES['github']['official'], base, 1)
+ elif family == 'git':
+ endpoint = spec['vdn']['url'].replace(SOURCES['github']['official'], base, 1)
+ kind = 'Git remote availability with effective Git configuration'
+ # curl does not read Git's global/URL-scoped proxy settings. Probe
+ # with Git itself, keeping custom URL credentials out of argv.
+ import uuid
+ remote = 'freevideo-probe-' + uuid.uuid4().hex
+ settings = {'remote.' + remote + '.url': endpoint}
+ # remote.url is multivalued: reusing this checkout's origin can
+ # select its existing SSH URL instead of the HTTPS URL being tested.
+ # A private temporary name avoids that; carry origin's proxy policy
+ # so the probe still matches the subsequent dependency fetch.
+ origin_proxy = subprocess.run(['git', 'config', '--get', 'remote.origin.proxy'], env=env,
+ stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, timeout=3, **hidden_console())
+ if origin_proxy.returncode == 0:
+ settings['remote.' + remote + '.proxy'] = origin_proxy.stdout.decode().rstrip('\r\n')
+ elif origin_proxy.returncode != 1:
+ raise RuntimeError('Git proxy configuration could not be read')
+ git_env = proxy.git_environment(env, settings)
+ git_env['GIT_TERMINAL_PROMPT'] = '0'
+ from . import processes
+ result = processes.run(['git', 'ls-remote', '--exit-code', remote, 'HEAD'],
+ env=git_env, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, timeout=timeout, supervise=True)
+ if result.returncode or not re.match(rb'[0-9a-f]{40,64}\s+HEAD', result.stdout):
+ raise RuntimeError('Git connection probe failed')
+ seconds = time.monotonic() - started
+ return dict(id=name, ok=True, seconds=seconds, probe_seconds=seconds, bytes=len(result.stdout), scope=kind)
+ else:
+ component = versions['cuda_toolkits']['13.0']['components']['cuda_cudart']
+ url = base + '/' + component['relative_path']
+ headers = ()
+ if (family in ('models', 'vdn-models', 'edge-models') and name == 'official'
+ and urlsplit(url).scheme == 'https' and urlsplit(url).netloc == 'huggingface.co'):
+ token = env.get('HF_TOKEN') or env.get('HUGGING_FACE_HUB_TOKEN')
+ if token:
+ # curl drops Authorization across redirect hosts; never use
+ # --location-trusted. The secret is passed through stdin.
+ headers = ('Authorization: Bearer ' + token,)
+ measurement = None
+ if measure_speed:
+ data, measurement = speed_sample(url, timeout, env=env, headers=headers)
+ seconds = measurement['seconds']
+ else:
+ data, seconds = sample(url, timeout, env=env, **({'headers': headers} if headers else {}))
+ if family in ('pypi', 'github', 'cuda') or family.startswith('torch-'):
+ if not data.startswith((b'PK', b'\x1f\x8b', b'\xfd7zXZ')):
+ raise RuntimeError('Source returned a page instead of an archive')
+ elif len(data) < 8 or int.from_bytes(data[:8], 'little') > 100 * 2**20:
+ raise RuntimeError('Source did not return the pinned safetensors file')
+ result = {'id': name, 'ok': True, 'seconds': seconds, 'probe_seconds': time.monotonic() - started,
+ 'bytes': len(data), 'bytes_per_second': len(data) / max(.001, seconds), 'scope': kind}
+ if measurement is not None:
+ result.update(measurement)
+ return result
+ except (OSError, ValueError, RuntimeError, subprocess.SubprocessError) as error:
+ # Do not persist URLs, proxy errors, access tokens or redirect queries.
+ return {'id': name, 'ok': False, 'probe_seconds': time.monotonic() - started,
+ 'error': str(error) if isinstance(error, RuntimeError) else type(error).__name__, 'scope': kind}
+
+
+def probe_score(row, family):
+ """Expected bounded transfer rate, including startup and observed stalls."""
+ if not row.get('ok'):
+ return 0.
+ latency = row.get('ttfb_seconds', row.get('startup_seconds', row.get('seconds', 0.))) or 0.
+ if not isinstance(latency, (int, float)) or not math.isfinite(latency):
+ latency = 0.
+ latency = max(0., latency)
+ if family == 'git':
+ return 1. / max(.001, row.get('seconds') or .001)
+ rate = row.get('bytes_per_second')
+ if (row.get('speed_measured') is False or not isinstance(rate, (int, float))
+ or not math.isfinite(rate) or rate <= 0):
+ return 0.
+ score = SPEED_SAMPLE_BYTES / (latency + SPEED_SAMPLE_BYTES/rate)
+ return score * (.2 if row.get('body_stalled') else 1.)
+
+
+def plan(spec, versions, layout='unified', *, mode='auto', timeout=5, offline=False, env=None,
+ model_only=False, measure_speed=True, progress=None, proxy_mode='auto'):
+ from .environments import ENVIRONMENTS, environment_names
+ env = proxy_environment(env)
+ started = time.monotonic()
+ if proxy_mode not in proxy.MODES:
+ raise ValueError('Unknown download connection mode')
+ proxy_configured = proxy.configured(env)
+ git_keys = [] if offline or model_only else proxy.git_proxy_keys(env)
+ families = ['pypi', 'models', 'vdn-models', 'github', 'git', 'cuda']
+ if spec['models'].get('edge_modelscope'):
+ families.insert(3, 'edge-models')
+ if versions.get('target_system') == 'Windows':
+ families.remove('cuda')
+ for name in environment_names(layout):
+ role = ENVIRONMENTS[name]
+ family = 'torch-' + role['cuda'] + '-' + role['torch'][0].split('==')[1]
+ if family not in families:
+ families.append(family)
+ if model_only:
+ families=[family for family in families if family in ('models','vdn-models','edge-models')]
+ selections, jobs = {}, []
+ for family in families:
+ group = 'torch' if family.startswith('torch-') else 'github' if family == 'git' else family
+ names = ['official'] if mode == 'official' else list(SOURCES[group])
+ if mode != 'official' and configured_source(group, env):
+ names.insert(0, 'user')
+ selections[family] = []
+ routes = proxy.routes(env, git=family == 'git', mode=proxy_mode)
+ jobs += [(family, name, route) for name in names for route in routes]
+ if offline:
+ for family, name, route in jobs:
+ selections[family].append({'id': name, 'route': route, 'ok': None, 'scope': 'Offline fixture; not measured'})
+ else:
+ if progress:
+ progress(dict(done=0, total=len(jobs)))
+ # Bound parallel traffic and memory. Measure real body bytes, keeping
+ # first-byte latency separate from throughput; no regional assumptions.
+ with ThreadPoolExecutor(max_workers=8) as pool:
+ futures = {pool.submit(probe, family, name, spec, versions, timeout,
+ proxy.direct(env, git=family == 'git', keys=git_keys) if route == 'direct' else env,
+ measure_speed=measure_speed): (family, name, route) for family, name, route in jobs}
+ for index, future in enumerate(as_completed(futures), 1):
+ family, name, route = futures[future]
+ result = dict(future.result(), route=route)
+ result['score'] = probe_score(result, family)
+ selections[family].append(result)
+ if progress:
+ progress(dict(done=index, total=len(jobs), family=family, source=name, route=route))
+ for family, rows in selections.items():
+ rows.sort(key=lambda r: (not r['ok'], -r['score'],
+ r.get('ttfb_seconds', r.get('seconds', float('inf'))), r['id'], r['route']))
+ return {'schema_version': 3, 'mode': mode, 'proxy_mode': proxy_mode,
+ 'proxy_variables': [k for k in PROXY_KEYS if env.get(k)],
+ 'proxy_configured': proxy_configured,
+ 'git_proxy_configured': bool(git_keys or env.get('GIT_PROXY_COMMAND')),
+ 'proxy_policy': 'Per-download connection mode; system proxy settings unchanged',
+ 'timeout_seconds': timeout, 'sources': selections, 'probe_seconds': time.monotonic() - started,
+ 'scope': 'Concurrent bounded samples: connectivity, first-byte latency and body transfer. No location lookup; pinned files unchanged.'}
+
+
+def installed_plan(env=None):
+ env = os.environ if env is None else env
+ path = env.get('FREEVIDEO_NETWORK_PLAN')
+ return json.loads(Path(path).read_text(encoding='utf-8')).get('network', {}) if path else {}
+
+
+def ordered(network, family):
+ rows = network.get('sources', {}).get(family)
+ names=list(dict.fromkeys(row['id'] for row in rows)) if rows else ['official']
+ from .download_settings import order
+ names = order(network,family,names)
+ failed = {r['id'] for r in (rows or []) if r.get('last_transfer_success') is False}
+ return sorted(names, key=lambda name: name in failed)
+
+
+def source_health(network, family, name, success):
+ rows = network.get('sources', {}).get(family, [])
+ selected = [r for r in rows if r['id'] == name]
+ for row in selected:
+ row['last_transfer_success'] = success
+ others = [r for r in rows if r['id'] != name]
+ rows[:] = selected + others if success else others + selected
+
+
+def route_order(network, family, name, env=None):
+ from .download_settings import current
+ preferences = current(network)
+ mode = preferences['proxy_mode']
+ allowed = proxy.routes(env, git=family == 'git', mode=mode)
+ rows = [r for r in network.get('sources', {}).get(family, []) if r['id'] == name]
+ measured = preferences.get('probe', {})
+ if (time.time()-measured.get('measured_at', 0) < 86400 and
+ measured.get('proxy_mode', 'auto') == mode):
+ rows = [r for r in measured.get('sources', {}).get(family, [])
+ if r.get('id') == name and r.get('ok')] + rows
+ preferred = [r['last_successful_route'] for r in rows if r.get('last_successful_route') in allowed]
+ tested = [r['route'] for r in rows if r.get('route') in allowed]
+ return list(dict.fromkeys(preferred + tested + allowed))
+
+
+def route_health(network, family, name, route):
+ for row in network.get('sources', {}).get(family, []):
+ if row['id'] == name:
+ row['last_successful_route'] = route
+
+
+def route_environment(network, family, name, env=None, route=None):
+ allowed = route_order(network, family, name, env)
+ selected = route if route in allowed else allowed[0]
+ return proxy.environment(env, selected, git=family == 'git')
+
+
+def urls(network, family, original, env=None):
+ origin = SOURCES[family]['official']
+ if not original.startswith(origin + '/'):
+ return [('original', original)]
+ return [(name, source_url(family, name, env) + original[len(origin):]) for name in ordered(network, family)]
+
+
+def event(network, **details):
+ row = dict(event='network', epoch=time.time(), **details)
+ if network.get('event_callback'):
+ network['event_callback'](row)
+ elif not network.get('quiet'):
+ sys.stdout.write(json.dumps(row) + '\n')
+ sys.stdout.flush()
+ path = network.get('events_path') or os.environ.get('FREEVIDEO_NETWORK_EVENTS')
+ if path:
+ # One bounded line per attempt, no URLs or credentials.
+ with Path(path).open('a', encoding='utf-8') as stream:
+ stream.write(json.dumps(row) + '\n')
+
+
+def retryable(message):
+ message = str(message).lower()
+ return any(s in message for s in ('timed out', 'timeout', 'connection', 'could not resolve', 'failed to download',
+ 'failed to fetch', 'network is unreachable', 'ssl', 'tls', 'http status', 'status code',
+ 'requested url returned error', 'no solution found', 'no matching distribution', 'could not find a version',
+ 'rpc failed', 'early eof', 'unable to access', 'remote end hung up', 'couldn\'t connect', 'proxy'))
+
+
+def connection_failure(message):
+ return retryable(message) and not any(s in str(message).lower() for s in
+ ('no solution found', 'no matching distribution', 'could not find a version'))
+
+
+class PackageSourcesError(RuntimeError):
+ """A package install failed on every retained source.
+
+ The old error only named the package family, which made a setup report
+ impossible to act on. Keep the structured attempts on the exception so
+ callers can render them locally while the message remains short enough for
+ the launcher UI.
+ """
+
+ def __init__(self, family, attempts):
+ self.family = family
+ self.attempts = [dict(row) for row in attempts]
+ summaries = []
+ for row in self.attempts:
+ detail = row.get('summary') or row.get('detail') or row.get('error_type') or 'unknown failure'
+ summaries.append('%s/%s: %s' % (row.get('source', 'unknown'),
+ row.get('route', 'unknown'), detail))
+ message = 'All package sources failed for %s. Check your network or connection mode in Downloads. Attempts: %s' % (family, '; '.join(summaries))
+ super().__init__(message)
+
+
+_ANSI_ESCAPE = re.compile(r'\x1b(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
+
+
+def _package_failure(error):
+ """Return a bounded, report-safe package error detail.
+
+ Package managers frequently echo index URLs and environment-derived
+ credentials. Preserve useful stderr while removing credentials, bearer
+ tokens and machine-specific absolute paths before it reaches events or a
+ shared diagnostic report.
+ """
+ value = getattr(error, 'stderr', None) or getattr(error, 'stdout', None) or str(error)
+ if isinstance(value, bytes):
+ value = value.decode('utf-8', errors='replace')
+ value = _ANSI_ESCAPE.sub('', str(value)).replace('\x00', '')
+ value = re.sub(r'(?i)(https?://)[^\s/@]+:[^\s/@]+@', r'\1<REDACTED>@', value)
+ value = re.sub(r'(?i)(Bearer\s+)[A-Za-z0-9._~+/-]+', r'\1<REDACTED>', value)
+ value = re.sub(r'(?i)(\b[\w-]{0,64}(?:token|password|secret|api[_-]?key)\s*[=:]\s*)[^\s,;&]+',
+ r'\1<REDACTED>', value)
+ # Keep a stable path marker without exposing usernames, drives or mounts.
+ value = re.sub(r'(?<![A-Za-z0-9_:/])(?:[A-Za-z]:[\\/]|/)([^\s"\'<>;,)\]}]*)',
+ lambda match: '<PATH>/' + re.split(r'[\\/]', match.group(1).rstrip('\\/'))[-1], value)
+ lines = [line.strip() for line in value.splitlines() if line.strip()]
+ detail = '\n'.join(lines[-12:])[-4096:] if lines else 'no package-manager output'
+ summary = next((line for line in lines if line), detail).strip()[:320]
+ return detail, summary
+
+
+def _package_attempt(error, source, route):
+ detail, summary = _package_failure(error)
+ return dict(source=source, route=route, error_type=type(error).__name__,
+ returncode=getattr(error, 'returncode', None), detail=detail, summary=summary)
+
+
+def package_environment(network, family, name, environ=None, *, route=None):
+ env = route_environment(network, family, name, environ, route)
+ endpoint = source_url(family, name, environ)
+ if family.startswith('torch-'):
+ endpoint += '/' + family.split('-')[1]
+ env.update(UV_DEFAULT_INDEX=endpoint, UV_INDEX_URL=endpoint, PIP_INDEX_URL=endpoint,
+ UV_HTTP_TIMEOUT='20', UV_HTTP_RETRIES='1', PIP_DEFAULT_TIMEOUT='20', PIP_RETRIES='1',
+ UV_NO_CONFIG='1')
+ for key in ('UV_INDEX', 'UV_EXTRA_INDEX_URL', 'PIP_EXTRA_INDEX_URL'):
+ env.pop(key, None)
+ return env
+
+
+def package_command(network, family, run, env=None):
+ attempts = []
+ for name in ordered(network, family):
+ for route in route_order(network, family, name, env):
+ event(network, category=family, source=name, route=route, action='package-attempt')
+ try:
+ attempt_env = package_environment(network, family, name, env, route=route)
+ # The host setup uses these private markers to retain one log
+ # per source/route instead of overwriting the first failure.
+ # They are never included in a report or sent to a package
+ # manager as configuration.
+ attempt_env['FREEVIDEO_PACKAGE_SOURCE'] = name
+ attempt_env['FREEVIDEO_PACKAGE_ROUTE'] = route
+ result = run(attempt_env, name)
+ source_health(network, family, name, True)
+ route_health(network, family, name, route)
+ return result
+ except (RuntimeError, subprocess.CalledProcessError) as error:
+ attempt = _package_attempt(error, name, route)
+ attempts.append(attempt)
+ event(network, category=family, source=name, route=route, action='package-failed',
+ error_type=attempt['error_type'], returncode=attempt['returncode'],
+ detail=attempt['detail'], summary=attempt['summary'])
+ detail = attempt['detail']
+ if not retryable(detail):
+ # Preserve the original exception type and traceback for
+ # build/configuration errors, but make the attempt data
+ # available to the local report collector.
+ try:
+ error.package_attempts = attempts
+ except Exception:
+ pass
+ raise
+ event(network, category=family, source=name, route=route, action='package-fallback', reason='network-or-unavailable-pinned-package')
+ if not connection_failure(detail):
+ break
+ source_health(network, family, name, False)
+ raise PackageSourcesError(family, attempts)
+
+
+def hash_file(path, algorithm='sha256', git_blob=False, *, discard_cache=False, progress=None):
+ value = hashlib.new(algorithm)
+ if git_blob:
+ value.update(('blob %d\0' % path.stat().st_size).encode())
+ with path.open('rb') as stream:
+ offset = 0
+ total = path.stat().st_size
+ if progress:
+ progress(0, total)
+ for block in iter(lambda: stream.read(4 * 2**20), b''):
+ value.update(block)
+ if discard_cache:
+ from .download_cache import discard_read_cache
+ discard_read_cache(stream.fileno(), offset, len(block))
+ offset += len(block)
+ if progress:
+ progress(offset, total)
+ return value.hexdigest()
+
+
+def retain_partial(path, reason):
+ retained = path.with_name(path.name + '.' + reason + '-' + str(time.time_ns()))
+ path.rename(retained)
+ return retained
+
+
+RETRY_DELAYS = (2, 5)
+
+
+def retry_wait(network, attempt, **details):
+ delay = RETRY_DELAYS[min(attempt, len(RETRY_DELAYS)-1)]
+ event(network, action='retry', retry_number=attempt+1, wait_seconds=delay, **details)
+ deadline = time.monotonic() + delay
+ while time.monotonic() < deadline:
+ if network.get('resource_check'):
+ network['resource_check']()
+ time.sleep(min(.2, max(0, deadline-time.monotonic())))
+
+
+def pause_download(network, name, path, retained, reason, *, details=None):
+ event(network, category='models', source=name, file=Path(path).name, action='paused',
+ retained_bytes=retained, reason=reason, **({'size_error': details} if details else {}))
+ explanation = ''
+ if details:
+ explanation = '%s: observed %d bytes; allowed %d bytes; complete model %d bytes. ' % (
+ details['file'], details['observed_bytes'], details['allowed_bytes'], details['expected_model_bytes'])
+ raise RuntimeError('Model download paused: %s (%s). Temporary files retained. '
+ '%s'
+ 'Retry setup to continue the same source. If resume is unavailable, enable '
+ '"Allow restarting incomplete downloads" or --allow-model-restart to permit a fresh download.' %
+ (Path(path).name, reason, explanation))
+
+
+def download(candidates, path, expected, progress=None, *, network=None, env=None, size=None,
+ algorithm='sha256', git_blob=False, headers_for=None, stall_seconds=None, category='download'):
+ """Resume across ranked sources; hash before accepting, retain rejected bytes."""
+ network = network or {}
+ path = Path(path)
+ model = category in ('models', 'vdn-models', 'edge-models')
+ stall_seconds = stall_seconds if stall_seconds is not None else 120 if model else 20
+
+ def matches(candidate, source='local'):
+ if size is not None and candidate.stat().st_size != size:
+ return False
+ event(network, category=category, source=source, file=path.name, action='verifying', algorithm=algorithm)
+ return hash_file(candidate, algorithm, git_blob, discard_cache=category in ('models', 'vdn-models')) == expected
+ if path.is_file():
+ if matches(path):
+ return
+ raise ValueError('Existing file failed integrity check; retained without overwrite: ' + str(path))
+ path.parent.mkdir(parents=True, exist_ok=True)
+ partial = path.with_suffix(path.suffix + '.partial')
+ if partial.is_file() and (size is None or partial.stat().st_size == size):
+ if matches(partial):
+ from .file_ops import publish
+ publish(partial, path)
+ return
+ if size is not None:
+ # A complete-but-corrupt file is not a resumable prefix.
+ retain_partial(partial, 'hash-rejected')
+ timeout = network.get('timeout_seconds', 5)
+ affinity = partial.with_name(partial.name + '.source.json')
+ candidates = [(name, url, route) for name, url in candidates
+ for route in route_order(network, category, name, env)]
+ if not candidates:
+ raise RuntimeError('No download sources configured; existing files retained')
+ if model and not network.get('manual_source_switch') and partial.exists() and affinity.is_file():
+ try:
+ prior = json.loads(affinity.read_text(encoding='utf-8'))
+ if prior.get('expected') == expected:
+ candidates.sort(key=lambda row: row[0] != prior.get('source'))
+ except (OSError, ValueError, AttributeError):
+ pass
+ # Try the next ranked candidate promptly. The final candidate gets bounded
+ # retries; every mirror must honor the retained prefix, with no silent reset.
+ for cycle in range(2):
+ for candidate_index, (name, url, route) in enumerate(candidates):
+ for restart in range(3 if model else 2):
+ offset = partial.stat().st_size if partial.exists() else 0
+ if size and offset > size:
+ retain_partial(partial, 'oversized')
+ offset = 0
+ remembered = False
+ def remember_source():
+ nonlocal remembered
+ if model and not remembered:
+ from .monitoring import save
+ save(affinity, dict(source=name, expected=expected))
+ remembered = True
+ if model and offset == 0:
+ remember_source()
+ event(network, category=category, source=name, route=route, file=path.name, action='attempt', method='curl', cycle=cycle, resume_bytes=offset)
+ attempt_env = route_environment(network, category, name, env, route)
+ command = curl_command(timeout, attempt_env) + ['--output', str(partial), '--write-out', '%{http_code}']
+ if not model:
+ command += ['--speed-limit', '1024', '--speed-time', str(stall_seconds)]
+ if url.startswith('https://'):
+ command += ['--proto-redir', '=https']
+ if size:
+ # curl also applies this cap to intermediate redirect
+ # bodies (e.g. HF Mirror's 307 for a 96-byte JSON file).
+ # Keep transfers bounded; the final file must still match
+ # its exact pinned size AND content hash below.
+ # The cap applies to this response, excluding an existing
+ # prefix. A broken Range reply must not append a full file.
+ command += ['--max-filesize', str(max(size - offset, MIN_TRANSFER_LIMIT))]
+ if offset:
+ command += ['--continue-at', str(offset)]
+ process = subprocess.Popen(command, env=attempt_env, stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, **hidden_console())
+ started = time.monotonic()
+ oversized = None
+ stalled = False
+ last_body, previous_done = started, offset
+ try:
+ process.stdin.write(curl_config(url, headers_for(name) if headers_for else ()))
+ process.stdin.close()
+ while process.poll() is None:
+ if network.get('resource_check'):
+ network['resource_check']()
+ done = partial.stat().st_size if partial.exists() else 0
+ if done > previous_done:
+ previous_done, last_body = done, time.monotonic()
+ remember_source()
+ if model and time.monotonic()-last_body > stall_seconds:
+ stalled = True
+ break
+ if size is not None and done > size:
+ # Also cover chunked responses and curl versions
+ # whose size cap relies on Content-Length. Never
+ # turn these excess bytes into progress or an ETA.
+ oversized = done
+ break
+ if progress:
+ progress(done, size or 0, max(0, done - offset) / max(.001, time.monotonic() - started))
+ try:
+ process.wait(timeout=.25)
+ except subprocess.TimeoutExpired:
+ pass
+ finally:
+ if process.poll() is None:
+ process.terminate()
+ try:
+ process.wait(timeout=2)
+ except subprocess.TimeoutExpired:
+ process.kill()
+ process.wait()
+ status = process.stdout.read(16).decode(errors='replace')
+ process.stdout.close()
+ current_size = partial.stat().st_size if partial.exists() else 0
+ if model and current_size > offset:
+ remember_source()
+ if oversized is not None:
+ retain_partial(partial, 'oversized-response')
+ source_health(network, category, name, False)
+ event(network, category=category, source=name, file=path.name, action='fallback',
+ method='curl', reason='response-size-exceeded', observed_bytes=oversized,
+ expected_bytes=size, resume_bytes=offset)
+ break
+ if process.returncode == 0:
+ if matches(partial, name):
+ from .file_ops import publish
+ publish(partial, path)
+ source_health(network, category, name, True)
+ route_health(network, category, name, route)
+ event(network, category=category, source=name, route=route, file=path.name, action='verified', method='curl', bytes=path.stat().st_size)
+ return
+ retain_partial(partial, 'hash-rejected')
+ source_health(network, category, name, False)
+ event(network, category=category, source=name, file=path.name, action='fallback', reason='integrity-mismatch')
+ break
+ if offset and (process.returncode == 33 or process.returncode == 22 and status == '416') and restart == 0:
+ if model:
+ event(network, category=category, source=name, file=path.name,
+ action='resume-refused', resume_bytes=offset, reason='source-does-not-support-range')
+ break
+ # A mirror lacking Range must start over, with previous bytes kept.
+ retain_partial(partial, 'resume-unavailable')
+ continue
+ transient = stalled or process.returncode in (5, 6, 7, 18, 28, 35, 52, 55, 56, 92, 95) or status in ('429', '500', '502', '503', '504')
+ if model and transient and restart < 2 and candidate_index + 1 == len(candidates) and process.returncode not in (5, 7):
+ retry_wait(network, restart, category=category, source=name, file=path.name,
+ resume_bytes=current_size, reason='no-progress' if stalled else 'connection-interrupted')
+ continue
+ alternatives = route_order(network, category, name, env)
+ if alternatives.index(route) + 1 < len(alternatives):
+ event(network, category=category, source=name, route=alternatives[alternatives.index(route)+1],
+ file=path.name, action='route-retry', reason='curl-' + str(process.returncode), resume_bytes=current_size)
+ else:
+ source_health(network, category, name, False)
+ event(network, category=category, source=name, route=route, file=path.name, action='fallback', reason='curl-' + str(process.returncode), http_status=status)
+ break
+ if model and partial.exists() and partial.stat().st_size:
+ if not network.get('allow_model_restart'):
+ pause_download(network, name, path, partial.stat().st_size, 'sources could not continue the retained prefix')
+ event(network, category=category, source=name, file=path.name, action='restart-approved',
+ retained_bytes=partial.stat().st_size, reason='resume-unavailable')
+ retain_partial(partial, 'restart-approved')
+ raise RuntimeError('Every download source failed for %s. Check your network or choose another connection mode in Downloads. Partial files retained' % path.name)
+
+
+def clone(target, url, commit, *, sparse=None, run=None, network=None, env=None):
+ """Fetch only the pinned commit, atomically; never reset existing checkouts."""
+ target = Path(target)
+ network = installed_plan(env) if network is None else network
+ env = proxy_environment(env)
+ env.update(GIT_TERMINAL_PROMPT='0', GIT_HTTP_LOW_SPEED_LIMIT='1024', GIT_HTTP_LOW_SPEED_TIME='20')
+ git = env.get('FREEVIDEO_GIT') or 'git'
+ def execute(label, command):
+ command = [git, *command[1:]]
+ if run:
+ return run(label, command, env=env)
+ result = subprocess.run(list(map(str, command)), env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, **hidden_console())
+ if result.returncode:
+ raise RuntimeError(result.stdout[-2500:])
+ if target.exists():
+ head = subprocess.check_output([git, '-C', str(target), 'rev-parse', 'HEAD'], env=env, text=True, **hidden_console()).strip()
+ dirty = subprocess.check_output([git, '-C', str(target), 'status', '--porcelain', '--untracked-files=no'], env=env, text=True, **hidden_console())
+ if head != commit or dirty:
+ raise ValueError('Existing dependency differs from the pinned clean source: ' + str(target))
+ return target
+ target.parent.mkdir(parents=True, exist_ok=True)
+ candidates = urls(dict(network, sources=dict(network.get('sources', {}), github=network.get('sources', {}).get('git', [{'id': 'official'}]))), 'github', url, env)
+ configured_env = env
+ for name, endpoint, route in [(name, endpoint, route) for name, endpoint in candidates
+ for route in route_order(network, 'git', name, configured_env)]:
+ env = route_environment(network, 'git', name, configured_env, route)
+ temporary = target.with_name(target.name + '.partial-' + str(time.time_ns()))
+ event(network, category='git', source=name, route=route, file=target.name, action='attempt', commit=commit)
+ try:
+ execute(target.name + '-init-' + name, ['git', 'init', '--quiet', temporary])
+ # Never depend on a user's global CRLF conversion or long-path
+ # settings when constructing the exact official patched tree.
+ execute(target.name + '-line-endings', ['git', '-C', temporary, 'config', 'core.autocrlf', 'false'])
+ execute(target.name + '-long-paths', ['git', '-C', temporary, 'config', 'core.longpaths', 'true'])
+ # URL via environment keeps credentials out of process arguments/logs.
+ config_index = int(env.get('GIT_CONFIG_COUNT', 0))
+ fetch_env = dict(env, GIT_CONFIG_COUNT=str(config_index + 1))
+ fetch_env.update({'GIT_CONFIG_KEY_' + str(config_index): 'remote.origin.url',
+ 'GIT_CONFIG_VALUE_' + str(config_index): endpoint})
+ original = env
+ env = fetch_env
+ try:
+ execute(target.name + '-fetch-' + name, ['git', '-C', temporary, '-c', 'fetch.fsckObjects=true', 'fetch', '--depth=1', '--no-tags', 'origin', commit])
+ if sparse:
+ execute(target.name + '-sparse', ['git', '-C', temporary, 'sparse-checkout', 'set', '--no-cone', *sparse])
+ execute(target.name + '-checkout', ['git', '-C', temporary, 'checkout', '--detach', commit])
+ finally:
+ env = original
+ actual = subprocess.check_output([git, '-C', str(temporary), 'rev-parse', 'HEAD'], env=env, text=True, **hidden_console()).strip()
+ if actual != commit:
+ raise ValueError('Downloaded source did not match its pinned commit')
+ temporary.rename(target)
+ source_health(network, 'git', name, True)
+ route_health(network, 'git', name, route)
+ event(network, category='git', source=name, route=route, file=target.name, action='verified', commit=commit)
+ return target
+ except RuntimeError as error:
+ if not retryable(error):
+ raise
+ source_health(network, 'git', name, False)
+ event(network, category='git', source=name, route=route, file=target.name, action='fallback', reason='network-fetch-failed')
+ raise RuntimeError('All Git sources failed. Check your network or connection mode in Downloads. Incomplete checkouts retained beside ' + str(target))
diff --git a/freevideo_engine/offline_packages.py b/freevideo_engine/offline_packages.py
new file mode 100644
index 0000000..1ba56bf
--- /dev/null
+++ b/freevideo_engine/offline_packages.py
@@ -0,0 +1,227 @@
+"""Import user-downloaded ZIPs. No downloader, model imports or subprocesses."""
+import hashlib
+import json
+import os
+from pathlib import Path
+import shutil
+import stat
+import threading
+import zipfile
+
+from .monitoring import save
+from .portable import inside
+
+PREFIX = 'FreeVideo-Windows/'
+MAX_BYTES = 300 * 2**30
+
+
+def dependency_id():
+ root = Path(__file__).resolve().parent
+ digest = hashlib.sha256()
+ for path in (root / 'dependencies.json', root / 'bootstrap_versions.json',
+ root.parent / 'constraints/windows-portable.txt'):
+ digest.update(path.read_bytes())
+ return digest.hexdigest()
+
+
+def inventory(rows):
+ seen = set()
+ for row in rows:
+ name = row['path']
+ inside(Path('/unused'), name)
+ # Windows strips trailing dots/spaces and recognizes device names.
+ for part in name.split('/'):
+ if (part.rstrip(' .') != part or part.split('.')[0].upper() in
+ ('CON', 'PRN', 'AUX', 'NUL', *('COM%d' % n for n in range(1, 10)),
+ *('LPT%d' % n for n in range(1, 10)))):
+ raise ValueError('Invalid Windows package path: ' + name)
+ if (name.casefold() in seen or type(row['bytes']) is not int or row['bytes'] < 0
+ or not isinstance(row['sha256'], str) or len(row['sha256']) != 64
+ or any(c not in '0123456789abcdef' for c in row['sha256'])):
+ raise ValueError('Invalid package inventory: ' + name)
+ seen.add(name.casefold())
+ if sum(r['bytes'] for r in rows) > MAX_BYTES:
+ raise ValueError('Offline package exceeds supported size')
+
+
+def inspect_archive(path):
+ """Read only bounded metadata; file contents are checked during import."""
+ with zipfile.ZipFile(path) as archive:
+ names = archive.namelist()
+ marker = next((name for name in ('offline-package.json', 'runtime.json')
+ if PREFIX + name in names), None)
+ if marker is None:
+ raise ValueError('不是 FreeVideo 离线包,请选择公用模型、显卡模型或运行环境 ZIP。')
+ info = archive.getinfo(PREFIX + marker)
+ if info.file_size > 16 * 2**20:
+ raise ValueError('Oversized offline inventory')
+ raw = archive.read(info)
+ value = json.loads(raw)
+ if marker == 'runtime.json':
+ if value.get('schema_version') != 2 or value.get('dependency_id') != dependency_id():
+ raise ValueError('运行环境包版本不匹配,请从下载页面获取配套的运行环境包。')
+ value = dict(value, kind='runtime', variant=None)
+ elif value.get('schema_version') != 1 or value.get('kind') != 'models' or value.get('variant') not in ('common', 'rowwise', 'per_tensor'):
+ raise ValueError('Invalid model package')
+ rows = value['files']
+ if not isinstance(rows, list) or not rows:
+ raise ValueError('Empty offline package')
+ inventory(rows)
+ if value['kind'] == 'models' and any(not r['path'].startswith('models/') for r in rows):
+ raise ValueError('Model packages may contain only models')
+ if value['kind'] == 'runtime' and any(r['path'].startswith('models/') or r['path'].lower().endswith('freevideo.exe') for r in rows):
+ raise ValueError('环境包不得包含模型或 FreeVideo.exe')
+ permitted = {PREFIX + r['path']: r for r in rows}
+ extra = {PREFIX + marker, PREFIX + '模型包使用说明.txt', PREFIX + '运行环境使用说明.txt'}
+ seen = set()
+ for entry in archive.infolist():
+ if entry.is_dir():
+ continue
+ name = entry.filename
+ if name.casefold() in seen or (name not in permitted and name not in extra):
+ raise ValueError('Unexpected or duplicate ZIP entry: ' + name)
+ seen.add(name.casefold())
+ mode = entry.external_attr >> 16
+ if stat.S_IFMT(mode) not in (0, stat.S_IFREG) or entry.flag_bits & 1:
+ raise ValueError('Encrypted files and links are not supported')
+ if name in permitted and entry.file_size != permitted[name]['bytes']:
+ raise ValueError('Incorrect package file size: ' + name)
+ if not set(permitted).issubset(names):
+ raise ValueError('离线包缺少文件,请重新下载这个 ZIP。')
+ return dict(value, marker=marker, raw=raw, id=hashlib.sha256(raw).hexdigest())
+
+
+def import_archive(path, destination, progress=lambda **kw: None, cancelled=lambda: False):
+ from .network import hash_file
+ path = Path(path).resolve()
+ value = inspect_archive(path)
+ root = Path(destination).resolve() / '.freevideo-packages' / value['id'] / 'FreeVideo-Windows'
+ total = sum(r['bytes'] for r in value['files'])
+ missing = sum(r['bytes'] for r in value['files'] if not inside(root, r['path']).is_file())
+ parent = root
+ while not parent.exists():
+ parent = parent.parent
+ if shutil.disk_usage(parent).free < missing + 64 * 2**20:
+ raise ValueError('磁盘空间不足,导入此包还需约 %.1f GiB。' % (missing / 2**30))
+ done = 0
+ root.mkdir(parents=True, exist_ok=True)
+ with zipfile.ZipFile(path) as archive:
+ for row in value['files']:
+ if cancelled():
+ raise InterruptedError('已暂停导入,ZIP 与已校验文件保留。')
+ target = inside(root, row['path'])
+ if target.is_file():
+ if target.stat().st_size != row['bytes'] or hash_file(target) != row['sha256']:
+ raise ValueError('已有导入文件已改变,未覆盖:' + str(target))
+ done += row['bytes']
+ progress(done=done, total=total, detail=path.name)
+ continue
+ target.parent.mkdir(parents=True, exist_ok=True)
+ partial = target.with_name(target.name + '.importing')
+ digest = hashlib.sha256()
+ with archive.open(PREFIX + row['path']) as source, partial.open('wb') as output:
+ while True:
+ if cancelled():
+ raise InterruptedError('已暂停导入,ZIP 与已校验文件保留。')
+ data = source.read(4 * 2**20)
+ if not data:
+ break
+ output.write(data); digest.update(data); done += len(data)
+ progress(done=done, total=total, detail=path.name)
+ if partial.stat().st_size != row['bytes'] or digest.hexdigest() != row['sha256']:
+ raise ValueError('离线包校验失败,已保留文件:' + path.name)
+ partial.replace(target)
+ inside(root, value['marker']).write_bytes(value['raw'])
+ return dict(root=str(root), kind=value['kind'], variant=value['variant'], name=path.name)
+
+
+def assemble(runtime, models, source):
+ """Bind imported components to the current EXE's engine, without running it."""
+ from .desktop_runtime import materialize_source
+ runtime = Path(runtime)
+ config = json.loads((runtime / 'runtime.json').read_text(encoding='utf-8'))
+ if config.get('schema_version') != 2 or config.get('dependency_id') != dependency_id():
+ raise ValueError('运行环境包与安装器不匹配')
+ candidates = []
+ for root in models:
+ path = Path(root) / 'models/model-pack.json'
+ if path.is_file():
+ candidates.append((Path(root), json.loads(path.read_text(encoding='utf-8'))))
+ variants = {value['variant'] for _, value in candidates}
+ if not candidates:
+ raise ValueError('还需导入公用模型包和对应显卡的模型包。')
+ if len(variants) != 1:
+ raise ValueError('请只导入本机对应的一个显卡模型包;无需同时导入两种。')
+ model = candidates[0][1]
+ inventory(model['files'])
+ if any(not r['path'].startswith('models/') for r in model['files']):
+ raise ValueError('Invalid model inventory')
+ from .network import hash_file
+ for row in model['files']:
+ target = inside(runtime, row['path'])
+ sources = [inside(Path(p), row['path']) for p in models]
+ src = next((p for p in sources if p.is_file() and p.stat().st_size == row['bytes']
+ and hash_file(p) == row['sha256']), None)
+ if src is None:
+ raise ValueError('还缺少公用模型或显卡模型包中的文件:' + row['path'])
+ if target.exists():
+ if hash_file(target) != row['sha256']:
+ raise ValueError('离线安装已有不同模型,未覆盖:' + str(target))
+ else:
+ target.parent.mkdir(parents=True, exist_ok=True)
+ try:
+ os.link(src, target)
+ except OSError:
+ shutil.copyfile(src, target)
+ code = materialize_source(source, runtime / 'engine/launcher/source')
+ variant = model['variant']
+ value = dict(config['configuration'], schema_version=1, variant=variant,
+ source=str(code.relative_to(runtime).as_posix()),
+ source_commit=code.name, cache='models/edge/' + ('rowwise/cache' if variant == 'rowwise' else 'cache'),
+ files=config['files'] + model['files'])
+ inventory(value['files'])
+ save(runtime / 'portable.json', value)
+ return runtime
+
+
+class Importer:
+ def __init__(self):
+ self.thread = None
+ self.cancelled = threading.Event()
+ self.state = dict(status='idle', done=0, total=None, detail='', packages=[])
+
+ @property
+ def busy(self):
+ return self.thread is not None and self.thread.is_alive()
+
+ def prepare(self, runtime, models, source):
+ if self.busy:
+ return
+ self.state = dict(status='preparing', done=0, total=None, detail='检查配套包', packages=[])
+ def work():
+ try:
+ root = assemble(runtime, models, source)
+ self.state = dict(self.state, status='prepared', ready_root=str(root))
+ except Exception as error:
+ self.state = dict(self.state, status='error', error=str(error))
+ self.thread = threading.Thread(target=work, name='freevideo-offline-prepare', daemon=True)
+ self.thread.start()
+
+ def start(self, paths, destination):
+ if self.busy:
+ return
+ self.cancelled.clear()
+ self.state = dict(status='running', done=0, total=None, detail='', packages=[])
+ def update(**row):
+ self.state = dict(self.state, **row)
+ def work():
+ try:
+ packages = []
+ for path in paths:
+ packages.append(import_archive(path, destination, update, self.cancelled.is_set))
+ update(packages=list(packages))
+ update(status='complete')
+ except Exception as error:
+ update(status='error', error=str(error))
+ self.thread = threading.Thread(target=work, name='freevideo-package-import', daemon=True)
+ self.thread.start()
diff --git a/freevideo_engine/offload.py b/freevideo_engine/offload.py
new file mode 100644
index 0000000..a8063dd
--- /dev/null
+++ b/freevideo_engine/offload.py
@@ -0,0 +1,684 @@
+"""Sequential module offloading with bounded pinned-host and CUDA buffers."""
+from concurrent.futures import ThreadPoolExecutor
+from dataclasses import dataclass
+from contextlib import contextmanager, nullcontext
+import json
+import os
+import re
+import time
+import weakref
+
+import torch
+
+
+def cpu_weights(layer):
+ """Frozen parameters and CPU buffers (including official FP8 weights/scales).
+
+ Precomputed modulation tables already resident on CUDA are left in place.
+ Only immutable buffers may be included in an offloaded layer.
+ """
+ for name, value in layer.named_parameters():
+ if value.device.type != 'cpu' or value.requires_grad:
+ raise ValueError('Offloaded parameters must be frozen CPU tensors')
+ yield name, value
+ for name, value in layer.named_buffers():
+ if value.device.type == 'cpu':
+ if value.requires_grad:
+ raise ValueError('Offloaded buffers must be frozen')
+ yield name, value
+
+
+def _pin_reservation(sizes):
+ """Charge the host allocator's configured rounding, not only tensor bytes."""
+ settings = os.environ.get('PYTORCH_ALLOC_CONF', os.environ.get('PYTORCH_CUDA_ALLOC_CONF', ''))
+ threshold = float('inf')
+ for option in ('pinned_max_round_threshold_mb', 'pinned_max_cached_size_mb'):
+ match = re.search(r'(?:^|,)\s*' + option + r':\s*(\d+)(?:,|$)', settings)
+ if match:
+ threshold = min(threshold, int(match[1]) * 2**20)
+ return sum((1 << (size - 1).bit_length()) if 0 < size <= threshold else size for size in sizes)
+
+
+def pin_layer_weights(layers, max_bytes=None, *, headroom_bytes=None):
+ """Pack immutable CPU parameters into persistent pinned planes, one per dtype.
+
+ This optional mode trades pageable/mapped storage for locked host residency.
+ Rebinding preserves parameter values and permits direct asynchronous H2D.
+ """
+ layers = list(layers)
+ layer_groups = []
+ for layer in layers:
+ by_dtype = {}
+ for name, parameter in cpu_weights(layer):
+ if not isinstance(parameter, torch.nn.Parameter) and parameter._base is not None:
+ # safetensors buffers are views. Reassigning their .data alone
+ # keeps the old view base (and mapped FP8 file) alive alongside
+ # the pinned copy. Detach the registered buffer first; this
+ # shares the bytes without retaining view metadata.
+ parent, _, leaf = name.rpartition('.')
+ owner = layer.get_submodule(parent) if parent else layer
+ parameter = parameter.detach()
+ setattr(owner, leaf, parameter)
+ by_dtype.setdefault(parameter.dtype, []).append(parameter)
+ layer_groups.append(list(by_dtype.items()))
+
+ # Charge free physical memory only. Effective availability credits this
+ # tree's reclaimable mapped pages, which is right for measuring pressure
+ # but wrong for deciding how much to lock: locked pages cannot be
+ # reclaimed, so space that only exists after reclaim cannot back them.
+ # Crediting them raised the cap to about 15 GiB and cudaHostAlloc failed
+ # with cudaErrorMemoryAllocation during load. On Windows, pinned pages
+ # also consume system commit, so physical headroom alone can be wildly
+ # optimistic when the pagefile is nearly full. Cap against both counters;
+ # Linux reports no separate commit budget here and remains unchanged.
+ #
+ # The planned budget came from availability measured before text encoding
+ # ran, and a plan made at 22.70 GiB free pinned 10.88 GiB and left 1.73 GiB,
+ # under the emergency floor, because encoding had taken the host to
+ # 2.77 GiB. Cap against live availability so whole groups drop out and keep
+ # their mapped storage instead of failing the request.
+ from .system import system_memory, HOST_WEIGHT_HEADROOM
+ if headroom_bytes is None:
+ headroom_bytes = HOST_WEIGHT_HEADROOM
+ if type(headroom_bytes) is not int or headroom_bytes < 2 * 2**30:
+ raise ValueError('Pinning must preserve at least 2 GiB of host working space')
+ memory = system_memory()
+ available = memory.get('physical_available_bytes') or memory['available_bytes']
+ commit_available = memory.get('commit_available_bytes')
+ if commit_available is not None:
+ available = min(available, commit_available)
+ live_cap = available - headroom_bytes
+ max_bytes = live_cap if max_bytes is None else min(max_bytes, live_cap)
+
+ groups, sizes, reserved = [], [], 0
+ for entries in layer_groups:
+ current_sizes = [sum(p.numel() * p.element_size() for p in parameters) for _, parameters in entries]
+ current_reserve = _pin_reservation(current_sizes)
+ if reserved + current_reserve > max_bytes:
+ continue
+ groups.extend(entries)
+ sizes.extend(current_sizes)
+ reserved += current_reserve
+ required = sum(sizes)
+ # Charge the container's non-reclaimable working set, not clean file cache:
+ # mmap-backed weights are replaced group by group while pinning, and their
+ # clean pages can be reclaimed. Leave a separate 2 GiB process allowance.
+ from pathlib import Path
+ cgroup = Path('/sys/fs/cgroup')
+ if (cgroup / 'memory.max').is_file():
+ limit = (cgroup / 'memory.max').read_text(encoding='utf-8').strip()
+ if limit != 'max':
+ stat = {key: int(value) for key, value in (line.split() for line in (cgroup / 'memory.stat').read_text(encoding='utf-8').splitlines())}
+ clean_file = max(0, stat.get('file', 0) - sum(stat.get(k, 0) for k in ('shmem', 'file_dirty', 'file_writeback', 'unevictable')))
+ available_cgroup = int(limit) - int((cgroup / 'memory.current').read_text(encoding='utf-8')) + clean_file
+ if available_cgroup < reserved + 2 * 2**30:
+ raise MemoryError('Pinned weights exceed cgroup working-set allowance; select a smaller pinned budget')
+ pinned = 0
+ for index, (dtype, parameters) in enumerate(groups):
+ try:
+ plane = torch.empty(sum(p.numel() for p in parameters), dtype=dtype, pin_memory=True)
+ except (RuntimeError, MemoryError) as error:
+ # Pinning is an optimization, so a host allocator that stops short
+ # must cost transfers, not the request. Locked host memory has a
+ # platform ceiling independent of free RAM: this machine refused
+ # every attempt past 12.60 GiB with 25.1, 10.9 and 9.9 GiB free,
+ # while the plan asked for 15.05 GiB because the budget scales with
+ # available RAM. Whatever is not pinned keeps its mapped storage.
+ print(json.dumps({'event': 'host_pinning_truncated', 'pinned_bytes': pinned,
+ 'planned_bytes': required, 'groups_pinned': index,
+ 'groups_planned': len(groups),
+ 'reason': str(error).splitlines()[0][:200]}), flush=True)
+ return pinned
+ offset = 0
+ for parameter in parameters:
+ view = plane[offset:offset + parameter.numel()].view(parameter.shape)
+ view.copy_(parameter)
+ offset += parameter.numel()
+ parameter.data = view
+ pinned += sum(p.numel() * p.element_size() for p in parameters)
+ return pinned
+
+
+@dataclass
+class Slot:
+ host: dict
+ device: dict
+ copy_done: object = None
+ compute_done: object = None
+
+
+def _host_prefetch_fits(extra_bytes):
+ """Speculative staging must fit both live physical RAM and Windows commit."""
+ from .system import system_memory, HOST_WEIGHT_HEADROOM
+ try:
+ memory = system_memory()
+ available = memory.get('physical_available_bytes')
+ if available is None:
+ available = memory['available_bytes']
+ commit = memory.get('commit_available_bytes')
+ if commit is not None:
+ available = min(available, commit)
+ return available >= HOST_WEIGHT_HEADROOM + extra_bytes
+ except (OSError, ValueError, TypeError, KeyError):
+ return False
+
+
+@dataclass
+class _StreamedLayout:
+ """Metadata for reusing an unloaded layer, without owning any weight storage."""
+
+ source: object
+ index: int
+ entries: tuple
+
+ def shapes(self, source, index, weights):
+ if self.source() is not source or source is None or self.index != index:
+ raise ValueError('Unloaded streamed layers require their original checkpoint source and order')
+ if len(weights) != len(self.entries):
+ raise ValueError('Unloaded streamed layer tensors changed; reload the model')
+ shapes = {}
+ for (name, parameter), (reference, saved_name, dtype, count, shape) in zip(weights, self.entries):
+ if (reference() is not parameter or name != saved_name or parameter.dtype != dtype
+ or parameter.numel() != 0 or tuple(parameter.shape) != (0,)):
+ raise ValueError('Unloaded streamed layer tensors changed; reload the model')
+ shapes[name] = count, shape
+ return shapes
+
+
+def initialize_streamed_layer(layer, source, index):
+ """Bind an unmaterialized layer to a checkpoint without reading its weights."""
+ tensors = ([(name, value, True) for name, value in layer.named_parameters()]
+ + [(name, value, False) for name, value in layer.named_buffers()])
+ if source is None or any(not value.is_meta for _, value, _ in tensors):
+ raise ValueError('Streamed initialization requires a checkpoint and only meta tensors')
+ entries = []
+ for name, value, parameter in tensors:
+ count, shape, dtype = value.numel(), tuple(value.shape), value.dtype
+ empty = torch.empty(0, dtype=dtype, device='cpu')
+ if parameter:
+ empty = torch.nn.Parameter(empty, requires_grad=False)
+ parent, _, leaf = name.rpartition('.')
+ owner = layer.get_submodule(parent) if parent else layer
+ setattr(owner, leaf, empty)
+ entries.append((weakref.ref(empty), name, dtype, count, shape))
+ layer._freevideo_streamed_layout = _StreamedLayout(weakref.ref(source), index, tuple(entries))
+
+
+def unload_streamed_layer(layer, source, index):
+ """Keep a newly loaded layer's layout without retaining its checkpoint bytes.
+
+ Safetensors buffers are tensor views, including with the pread reader.
+ Emptying their .data does not clear _base: it would retain the entire CPU
+ tensor until sampling constructs an offloader. Detach registered buffers
+ first, then record the new identities used to restore the exact layout.
+ """
+ entries = []
+ for name, parameter in cpu_weights(layer):
+ if not isinstance(parameter, torch.nn.Parameter) and parameter._base is not None:
+ parent, _, leaf = name.rpartition('.')
+ owner = layer.get_submodule(parent) if parent else layer
+ parameter = parameter.detach()
+ setattr(owner, leaf, parameter)
+ entries.append((weakref.ref(parameter), name, parameter.dtype,
+ parameter.numel(), tuple(parameter.shape)))
+ parameter.data = torch.empty(0, dtype=parameter.dtype)
+ layer._freevideo_streamed_layout = _StreamedLayout(weakref.ref(source), index, tuple(entries))
+
+
+def prepare_streamed_layer(layer, source, index, *, pin_budget_bytes, headroom_bytes):
+ """Retain one affordable pinned layer or release it before loading the next.
+
+ Return logical pinned bytes and the charged allocator reservation. A partial
+ host-allocation failure keeps no half-pinned layer: the streaming offloader
+ would discard it anyway, and it must not consume the next layer's budget.
+ """
+ sizes = {}
+ for _, value in cpu_weights(layer):
+ sizes[value.dtype] = sizes.get(value.dtype, 0) + value.numel() * value.element_size()
+ reserved = _pin_reservation(sizes.values())
+ pinned = 0
+ if 0 < reserved <= pin_budget_bytes:
+ pinned = pin_layer_weights([layer], max_bytes=pin_budget_bytes, headroom_bytes=headroom_bytes)
+ if pinned == sum(sizes.values()) and all(value.is_pinned() for _, value in cpu_weights(layer)):
+ return pinned, reserved
+ unload_streamed_layer(layer, source, index)
+ if pinned:
+ from .torch_compat import empty_host_cache
+ empty_host_cache(torch)
+ return 0, 0
+
+
+class LayerOffloader:
+ """Keep CPU weights and stream a sequential layer list through one or two slots.
+
+ CUDA events protect both host-buffer reuse and device-buffer overwrite. No
+ parameter is changed numerically; hooks rebind its storage only while it runs.
+ This requires inference without autograd and one request at a time.
+
+ Streamed layers retain zero-storage placeholders after close. Reuse them with
+ the same checkpoint source and layer order; only in-memory sources are restored.
+ """
+
+ def __init__(self, layers, device='cuda', prefetch=True, manage_hooks=True, weight_source=None,
+ host_prefetch=None):
+ if torch.is_grad_enabled():
+ raise RuntimeError('LayerOffloader requires gradients disabled')
+ self.layers = list(layers)
+ self.device = torch.device(device)
+ self.prefetch = prefetch
+ self.weight_source = weight_source
+ self.cpu = []
+ self.layout = []
+ sizes = {}
+ for index, layer in enumerate(self.layers):
+ weights, layout, offsets = {}, [], {}
+ parameters = list(cpu_weights(layer))
+ saved = getattr(layer, '_freevideo_streamed_layout', None)
+ shapes = saved.shapes(weight_source, index, parameters) if saved is not None else {}
+ for name, parameter in parameters:
+ if weight_source is not None and not isinstance(parameter, torch.nn.Parameter) and parameter._base is not None:
+ parent, _, leaf = name.rpartition('.')
+ owner = layer.get_submodule(parent) if parent else layer
+ parameter = parameter.detach()
+ setattr(owner, leaf, parameter)
+ dtype = parameter.dtype
+ offset = offsets.get(dtype, 0)
+ count, shape = shapes.get(name, (parameter.numel(), parameter.shape))
+ weights[name] = parameter.detach()
+ layout.append((parameter, name, dtype, offset, count, shape))
+ offsets[dtype] = offset + count
+ for dtype, count in offsets.items():
+ sizes[dtype] = max(sizes.get(dtype, 0), count)
+ self.cpu.append(weights)
+ self.layout.append(layout)
+ del parameters
+ self.pinned_layers = [all(value.is_pinned() for value in weights.values()) for weights in self.cpu]
+ if weight_source is not None:
+ # Keep module objects and their metadata, but no full-checkpoint
+ # tensor views. The pre-hook restores exact shapes before a layer
+ # executes. Offloaded placeholders own zero bytes between calls.
+ for index, (layer, pinned, weights, layout) in enumerate(zip(
+ self.layers, self.pinned_layers, self.cpu, self.layout)):
+ if pinned:
+ continue # Keep a bounded pinned subset; reopen only the rest.
+ layer._freevideo_streamed_layout = _StreamedLayout(weakref.ref(weight_source), index, tuple(
+ (weakref.ref(parameter), name, dtype, count, shape)
+ for parameter, name, dtype, _, count, shape in layout))
+ for parameter, name, dtype, *_ in layout:
+ empty = torch.empty(0, dtype=dtype)
+ parameter.data = empty
+ weights[name] = empty
+ self.pinned_sources = all(self.pinned_layers)
+ # Staging buffers are an optimization over pageable copies, so a host
+ # allocator that refuses them must cost transfer speed, not the
+ # request. Weight pinning had already taken 14.51 GiB of locked memory
+ # and left nothing here, failing load with cudaErrorMemoryAllocation.
+ self.pinned_staging = True
+
+ def staging(count, dtype):
+ if self.pinned_sources:
+ return None
+ try:
+ return torch.empty(count, dtype=dtype, pin_memory=True)
+ except (RuntimeError, MemoryError):
+ self.pinned_staging = False
+ return torch.empty(count, dtype=dtype)
+
+ self.slots = [Slot(
+ host={} if self.pinned_sources else {dtype: staging(count, dtype) for dtype, count in sizes.items()},
+ device={dtype: torch.empty(count, dtype=dtype, device=self.device) for dtype, count in sizes.items()},
+ ) for _ in range(2 if prefetch else 1)]
+ self.copy_stream = torch.cuda.Stream(device=self.device)
+ self.executor = ThreadPoolExecutor(max_workers=1) if prefetch else None
+ self.pending = {}
+ self.transfers = []
+ self.host_buffer_wait_seconds = 0.
+ self.prefetch_wait_seconds = 0.
+ self.direct_read_layers = 0
+ self.direct_read_bytes = 0
+ self.hooks = []
+ self.expected = 0
+ self.closed = False
+ self.cached = {}
+ self.cache_candidates = set()
+ self.cache_budget_bytes = 0
+ self.cache_hits = 0
+ self.read_ahead_before_cache = None
+ self.read_ahead = (weight_source.read_ahead(self.layout, self.pinned_layers)
+ if weight_source is not None else None)
+ # A second CUDA slot is not affordable on every card. With enough RAM,
+ # prepare one future layer on the CPU while the current layer computes;
+ # upload it only at the next normal boundary through the SAME GPU slot.
+ # Keep the existing full prefetch path when two device slots are funded.
+ from .system import windows
+ requested = windows() if host_prefetch is None else host_prefetch
+ self.host_prefetch = False
+ self.host_prefetch_disabled_reason = None
+ self.host_slots = []
+ self.host_executor = None
+ self.host_pending = {}
+ self.host_cursor = 0
+ self.host_prefetch_reads = 0
+ self.host_prefetch_wait_seconds = 0.
+ self.host_memory_checked = 0.
+ if (requested and not prefetch and sum(not p for p in self.pinned_layers) > 1
+ and getattr(weight_source, 'direct_read', False)
+ and getattr(weight_source, 'intermediate_dtype', None) is None
+ and self.pinned_staging):
+ extra = _pin_reservation([plane.numel() * plane.element_size()
+ for plane in self.slots[0].host.values()])
+ if _host_prefetch_fits(extra):
+ host = {}
+ try:
+ for dtype, count in sizes.items():
+ host[dtype] = torch.empty(count, dtype=dtype, pin_memory=True)
+ except (RuntimeError, MemoryError):
+ host.clear()
+ self.host_prefetch_disabled_reason = 'host_allocation_refused'
+ from .torch_compat import empty_host_cache
+ empty_host_cache(torch)
+ else:
+ self.host_slots = [Slot(host=self.slots[0].host, device={}), Slot(host=host, device={})]
+ self.host_executor = ThreadPoolExecutor(max_workers=1,
+ thread_name_prefix='freevideo-host-stage')
+ self.host_prefetch = True
+ else:
+ self.host_prefetch_disabled_reason = 'host_headroom'
+ for index, layer in enumerate(self.layers if manage_hooks else []):
+ self.hooks.append(layer.register_forward_pre_hook(
+ lambda module, args, index=index: self._before(index)))
+ self.hooks.append(layer.register_forward_hook(
+ lambda module, args, result, index=index: self._after(index), always_call=True))
+
+ def cache_between_steps(self, max_bytes, *, max_layers=None, allow_growth=True):
+ """Retain affordable device planes until this sampling pass closes.
+
+ Admit only at a completed step boundary, after measuring real workspace.
+ Prefer disk-backed layers: caching them also removes repeated host reads.
+ Loading remains incremental through the existing staging buffers.
+ With no later reuse, retain only already-filled caches: filling a new
+ plane on the final forward just spends memory before discarding it.
+ """
+ if self.expected or self.pending or self.host_pending or self.closed:
+ raise RuntimeError('Pass cache admission requires an idle step boundary')
+ remaining = self.cache_budget_bytes = max(0, int(max_bytes))
+ candidates = set()
+ for index in sorted(range(len(self.layers)), key=lambda i: self.pinned_layers[i]):
+ if not allow_growth and index not in self.cached:
+ continue
+ if max_layers is not None and len(candidates) >= max_layers:
+ break
+ size = sum(count * parameter.element_size() for parameter, _, _, _, count, _ in self.layout[index])
+ if 0 < size <= remaining:
+ candidates.add(index)
+ remaining -= size
+ for index in set(self.cached) - candidates:
+ slot = self.cached.pop(index)
+ for event in (slot.compute_done, slot.copy_done):
+ if event is not None:
+ event.synchronize()
+ slot.device.clear()
+ changed = candidates != self.cache_candidates
+ self.cache_candidates = candidates
+ if changed and self.read_ahead is not None:
+ self.read_ahead.close()
+ self.read_ahead_before_cache = self.read_ahead.stats()
+ self.read_ahead = self.weight_source.read_ahead(self.layout,
+ [pinned or index in self.cache_candidates for index, pinned in enumerate(self.pinned_layers)])
+
+ def cached_bytes(self):
+ return sum(t.numel() * t.element_size() for slot in self.cached.values() for t in slot.device.values())
+
+ @contextmanager
+ def layer(self, index):
+ """Keep one layer resident while a caller evaluates several independent tiles."""
+ if self.hooks:
+ raise RuntimeError('Explicit layer contexts require manage_hooks=False')
+ self._before(index)
+ try:
+ yield self.layers[index]
+ finally:
+ self._after(index)
+
+ def _fill_host(self, index, slot):
+ # CPU writes must wait for the previous H2D read of this host plane.
+ # They need not wait for GPU computation using the separate device plane.
+ with torch.cuda.device(self.device):
+ if slot.copy_done is not None:
+ tick = time.monotonic()
+ slot.copy_done.synchronize()
+ self.host_buffer_wait_seconds += time.monotonic() - tick
+ started = time.monotonic()
+ if self.read_ahead is not None:
+ self.read_ahead.before(index)
+ direct = self.weight_source.stage_into(index, self.layout[index], slot.host)
+ counts = {}
+ if not direct:
+ with self.weight_source.layer(index, self.layout[index]) as weights:
+ for _, name, dtype, offset, count, _ in self.layout[index]:
+ slot.host[dtype][offset:offset + count].copy_(weights[name].reshape(-1))
+ for _, _, dtype, offset, count, _ in self.layout[index]:
+ counts[dtype] = offset + count
+ if direct:
+ self.direct_read_bytes += count * slot.host[dtype].element_size()
+ if direct:
+ self.direct_read_layers += 1
+ if self.read_ahead is not None:
+ self.read_ahead.consumed(index)
+ return slot, counts, time.monotonic() - started
+
+ def _stop_host_prefetch(self):
+ if self.host_executor is not None:
+ for _, future in self.host_pending.values():
+ future.cancel()
+ self.host_executor.shutdown(wait=True, cancel_futures=True)
+ self.host_executor = None
+ for slot in self.host_slots:
+ if slot.copy_done is not None:
+ slot.copy_done.synchronize()
+ self.host_pending.clear()
+ self.host_slots.clear()
+ self.host_prefetch = False
+
+ def _queue_host(self, start):
+ if not self.host_prefetch or self.host_pending:
+ return
+ now = time.monotonic()
+ if now - self.host_memory_checked >= 1.:
+ self.host_memory_checked = now
+ if not _host_prefetch_fits(0):
+ self.host_prefetch_disabled_reason = 'live_host_pressure'
+ self._stop_host_prefetch()
+ from .torch_compat import empty_host_cache
+ empty_host_cache(torch)
+ return
+ for index in range(start, len(self.layers)):
+ if self.pinned_layers[index] or index in self.cached:
+ continue
+ slot = self.host_slots[self.host_cursor % 2]
+ self.host_cursor += 1
+ self.host_pending[index] = (slot, self.host_executor.submit(self._fill_host, index, slot))
+ self.host_prefetch_reads += 1
+ break
+
+ def _stage(self, index):
+ slot = self.slots[index % len(self.slots)]
+ with torch.cuda.device(self.device):
+ if index in self.cached:
+ if self.read_ahead is not None:
+ self.read_ahead.consumed(index)
+ self.cache_hits += 1
+ cached = self.cached[index]
+ return cached, cached.copy_done
+ # A previous H2D transfer must finish before the CPU writes this slot.
+ pinned = self.pinned_layers[index]
+ host_slot = slot
+ staged = self.host_prefetch and not pinned
+ if slot.copy_done is not None and not pinned and not staged:
+ wait_started = time.monotonic()
+ slot.copy_done.synchronize()
+ self.host_buffer_wait_seconds += time.monotonic() - wait_started
+ started = time.monotonic()
+ if self.read_ahead is not None and not staged:
+ self.read_ahead.before(index)
+ counts = {}
+ direct = (not staged and self.weight_source is not None and not pinned
+ and self.weight_source.stage_into(index, self.layout[index], slot.host))
+ if staged:
+ if index in self.host_pending:
+ _, future = self.host_pending.pop(index)
+ tick = time.monotonic()
+ host_slot, counts, host_seconds = future.result()
+ self.host_prefetch_wait_seconds += time.monotonic() - tick
+ else:
+ host_slot = self.host_slots[self.host_cursor % 2]
+ self.host_cursor += 1
+ host_slot, counts, host_seconds = self._fill_host(index, host_slot)
+ elif direct:
+ self.direct_read_layers += 1
+ for _, _, dtype, offset, count, _ in self.layout[index]:
+ counts[dtype] = offset + count
+ self.direct_read_bytes += count * slot.host[dtype].element_size()
+ else:
+ source = (self.weight_source.layer(index, self.layout[index]) if self.weight_source is not None and not pinned
+ else nullcontext(self.cpu[index]))
+ with source as weights:
+ for _, name, dtype, offset, count, _ in self.layout[index]:
+ if not pinned:
+ slot.host[dtype][offset:offset + count].copy_(weights[name].reshape(-1))
+ counts[dtype] = offset + count
+ if self.read_ahead is not None and not staged:
+ self.read_ahead.consumed(index)
+ if not staged:
+ host_seconds = time.monotonic() - started
+ begin, end = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True)
+ with torch.cuda.stream(self.copy_stream):
+ destination = slot
+ if index in self.cache_candidates:
+ # Allocate on the copying stream so reused allocator blocks
+ # cannot race outstanding work from another CUDA stream.
+ planes = {}
+ try:
+ for dtype, count in counts.items():
+ planes[dtype] = torch.empty(count, dtype=dtype, device=self.device)
+ except torch.cuda.OutOfMemoryError:
+ planes.clear()
+ self.cache_candidates.discard(index)
+ else:
+ destination = Slot(host={}, device=planes)
+ self.cached[index] = destination
+ # Forward kernels may still be reading the device slot from two
+ # layers ago even though Python already left that forward call.
+ if destination.compute_done is not None:
+ self.copy_stream.wait_event(destination.compute_done)
+ begin.record()
+ if pinned:
+ for _, name, dtype, offset, count, _ in self.layout[index]:
+ destination.device[dtype][offset:offset + count].copy_(self.cpu[index][name].reshape(-1), non_blocking=True)
+ else:
+ for dtype, count in counts.items():
+ destination.device[dtype][:count].copy_(host_slot.host[dtype][:count], non_blocking=True)
+ end.record()
+ slot.copy_done = end
+ host_slot.copy_done = end
+ destination.copy_done = end
+ self.transfers.append((index, host_seconds, begin, end,
+ sum(count * slot.device[dtype].element_size() for dtype, count in counts.items())))
+ return destination, end
+
+ def _before(self, index):
+ if index != self.expected:
+ raise RuntimeError(f'Expected layer {self.expected}, got {index}; concurrent/reordered execution is unsupported')
+ if index in self.pending:
+ wait_started = time.monotonic()
+ slot, done = self.pending.pop(index).result()
+ self.prefetch_wait_seconds += time.monotonic() - wait_started
+ else:
+ slot, done = self._stage(index)
+ torch.cuda.current_stream(self.device).wait_event(done)
+ for parameter, _, dtype, offset, count, shape in self.layout[index]:
+ parameter.data = slot.device[dtype][offset:offset + count].view(shape)
+ if self.prefetch and index + 1 < len(self.layers):
+ self.pending[index + 1] = self.executor.submit(self._stage, index + 1)
+ self._queue_host(index + 1)
+
+ def _after(self, index):
+ slot = self.cached.get(index, self.slots[index % len(self.slots)])
+ done = torch.cuda.Event()
+ done.record(torch.cuda.current_stream(self.device))
+ slot.compute_done = done
+ for parameter, name, *_ in self.layout[index]:
+ parameter.data = self.cpu[index][name]
+ self.expected = (index + 1) % len(self.layers)
+
+ def stats(self):
+ self.copy_stream.synchronize()
+ return {'prefetch': self.prefetch, 'slots': len(self.slots),
+ 'streamed_checkpoint_layers': self.weight_source is not None,
+ 'streamed_layer_count': sum(not pinned for pinned in self.pinned_layers) if self.weight_source is not None else 0,
+ 'pinned_model_sources': self.pinned_sources,
+ 'pinned_layer_count': sum(self.pinned_layers),
+ 'pinned_layer_indices': [index for index, pinned in enumerate(self.pinned_layers) if pinned],
+ 'cuda_buffer_bytes': sum(t.numel() * t.element_size() for s in self.slots for t in s.device.values()),
+ 'pass_cache_budget_bytes': self.cache_budget_bytes,
+ 'pass_cache_bytes': sum(t.numel() * t.element_size() for s in self.cached.values() for t in s.device.values()),
+ 'pass_cache_layer_count': len(self.cached),
+ 'pass_cache_layer_indices': sorted(self.cached), 'pass_cache_hits': self.cache_hits,
+ 'pinned_buffer_bytes': sum(t.numel() * t.element_size()
+ for s in [*self.slots, *self.host_slots[1:]] for t in s.host.values() if t.is_pinned()),
+ 'pageable_buffer_bytes': sum(t.numel() * t.element_size() for s in self.slots for t in s.host.values() if not t.is_pinned()),
+ 'transfers': len(self.transfers),
+ 'h2d_bytes': sum(row[4] for row in self.transfers),
+ 'host_stage_seconds': sum(row[1] for row in self.transfers),
+ 'host_buffer_wait_seconds': self.host_buffer_wait_seconds,
+ 'prefetch_wait_seconds': self.prefetch_wait_seconds,
+ 'h2d_seconds': sum(row[2].elapsed_time(row[3]) for row in self.transfers) / 1000,
+ 'direct_read_layers': self.direct_read_layers,
+ 'direct_read_bytes': self.direct_read_bytes,
+ 'host_prefetch': self.host_prefetch,
+ 'host_prefetch_reads': self.host_prefetch_reads,
+ 'host_prefetch_wait_seconds': self.host_prefetch_wait_seconds,
+ 'host_prefetch_buffer_bytes': sum(t.numel() * t.element_size()
+ for slot in self.host_slots[1:] for t in slot.host.values()),
+ 'host_prefetch_disabled_reason': self.host_prefetch_disabled_reason,
+ 'disk_read_ahead': self.read_ahead.stats() if self.read_ahead is not None else {'enabled': False},
+ 'disk_read_ahead_before_cache': self.read_ahead_before_cache,
+ 'timing_note': 'Host staging, buffer waits and H2D may overlap compute. Prefetch wait can include '
+ 'buffer waiting and host staging; do not add these counters to wall time.'}
+
+ def close(self):
+ if self.closed:
+ return
+ if self.executor is not None:
+ self.executor.shutdown(wait=True)
+ self._stop_host_prefetch()
+ if self.read_ahead is not None:
+ self.read_ahead.close()
+ self.copy_stream.synchronize()
+ for slot in [*self.slots, *self.cached.values()]:
+ if slot.compute_done is not None:
+ slot.compute_done.synchronize()
+ for handle in self.hooks:
+ handle.remove()
+ for index, entries in enumerate(self.layout):
+ # Streamed CPU entries are zero-storage placeholders. Reopening all
+ # checkpoint mappings here defeats streaming and can exhaust Windows
+ # commit after the final step, before the caller can save its latents.
+ # Retained layout metadata reconstructs the next request's slots.
+ for parameter, name, *_ in entries:
+ parameter.data = self.cpu[index][name]
+ self.pending.clear()
+ self.cached.clear()
+ self.cache_candidates.clear()
+ self.slots.clear()
+ self.closed = True
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *_):
+ self.close()
diff --git a/freevideo_engine/optimize.py b/freevideo_engine/optimize.py
new file mode 100644
index 0000000..a367ad7
--- /dev/null
+++ b/freevideo_engine/optimize.py
@@ -0,0 +1,808 @@
+"""Measure a complete local baseline when needed, then run a bounded search."""
+import argparse
+import copy
+from contextlib import nullcontext
+import html
+import json
+import math
+import os
+from pathlib import Path
+import shutil
+import signal
+import sys
+import time
+import traceback
+
+from .bootstrap import DEFAULT_ROOT
+from .diagnostics import latest
+from .hardware import detect, GiB
+from .kernel_capabilities import available_backends
+from .locking import runtime_lock, LOCK_ENV
+from . import processes
+from .system import memory_complete, memory_peak, windows
+from .monitoring import save
+from .policy import choose
+from .terminal_ui import TerminalUI
+from . import tuning
+
+
+def capacity_flags(command):
+ result = {}
+ for index, argument in enumerate(command):
+ for name in ('vram_gib', 'ram_gib'):
+ flag = '--' + name.replace('_', '-')
+ if argument == flag:
+ result[name] = float(command[index + 1])
+ elif argument.startswith(flag + '='):
+ result[name] = float(argument.split('=', 1)[1])
+ return result
+
+
+class NoCompleteBaseline(ValueError):
+ """Inputs may still be reusable; failed measurements never are a baseline."""
+
+
+def artifact_entries(manifest):
+ """Read portable paths and native Windows paths from earlier reports."""
+ files = manifest.get('files') if isinstance(manifest, dict) else manifest
+ if not isinstance(files, list):
+ raise ValueError('Invalid reference artifact manifest')
+ entries = {}
+ for item in files:
+ if not isinstance(item, dict) or not isinstance(item.get('path'), str):
+ raise ValueError('Invalid reference artifact path')
+ name = item['path'].replace('\\', '/')
+ if name in entries:
+ raise ValueError('Duplicate reference artifact path: ' + name)
+ entries[name] = item
+ return entries
+
+
+def read_baseline(path, expected):
+ directory = path.parent if path.is_file() else path
+ report = json.loads((directory / 'report.json').read_text(encoding='utf-8'))
+ if report.get('baseline_identity') is not None:
+ errors = [] if report['baseline_identity'] == expected else ['Bootstrap baseline identity changed']
+ else:
+ inventory = json.loads((directory / 'inventory.json').read_text(encoding='utf-8'))
+ errors = tuning.report_identity_errors(inventory, expected)
+ if errors:
+ raise ValueError('Run test again on this installation before optimizing: ' + '; '.join(errors))
+ from .validation import validate_metrics
+ usable = []
+ for row in report.get('cases', []):
+ if row.get('status') != 'complete':
+ continue
+ preencoded = row.get('input_mode') == 'preencoded'
+ validate_metrics(row, row['geometry'], preencoded=preencoded)
+ if row['engine'].get('sampling_plan', {}).get('enabled'):
+ continue # Optimizer compares an unchanged 8-step sampler, not a two-pass output.
+ if not isinstance(row['id'], str) or Path(row['id']).name != row['id'] or row['id'] in ('.', '..'):
+ raise ValueError('Invalid baseline case identifier')
+ case = directory / row['id']
+ manifest = json.loads((case / 'artifacts.json').read_text(encoding='utf-8'))
+ # The test's hashes bind the exact prompt/conditioning/latent reference.
+ entries = artifact_entries(manifest)
+ names = ['video.artifacts/conditioning.pt', 'video.artifacts/latents.pt']
+ if not preencoded:
+ names.append('prompt.txt')
+ for name in names:
+ if name not in entries or tuning.digest(case / name) != entries[name]['sha256']:
+ raise ValueError('Reference artifact is missing or changed: ' + str(case / name))
+ if not preencoded and (case / 'prompt.txt').read_text(encoding='utf-8') != row['prompt']:
+ raise ValueError('Test prompt changed')
+ from .validation import validate_artifacts
+ validate_artifacts(case, row['geometry'], preencoded=preencoded)
+ usable.append(row)
+ if not usable:
+ raise NoCompleteBaseline('No complete baseline; a fresh full request is required, not a passing probe')
+ return directory, report, usable
+
+
+
+def capacity_attention(command):
+ for index, argument in enumerate(command):
+ if argument == '--attention':
+ return command[index + 1]
+ if argument.startswith('--attention='):
+ return argument.split('=', 1)[1]
+ raise ValueError('Recovery input has no recorded attention choice')
+
+
+def recovery_input(path, expected):
+ """Reuse only an intact input of a recoverable failure, never its metrics."""
+ from .adaptive import classify_failure
+ directory = path.parent if path.is_file() else path
+ report = json.loads((directory / 'report.json').read_text(encoding='utf-8'))
+ if report.get('baseline_identity') is not None:
+ errors = [] if report['baseline_identity'] == expected else ['Bootstrap baseline identity changed']
+ else:
+ inventory = json.loads((directory / 'inventory.json').read_text(encoding='utf-8'))
+ errors = tuning.report_identity_errors(inventory, expected)
+ if errors:
+ raise ValueError('Failed-run input identity changed: ' + '; '.join(errors))
+ for row in reversed(report.get('cases', [])):
+ status = row.get('status')
+ failure = classify_failure(row.get('error', ''), row.get('engine'))
+ two_pass_input = status == 'complete' and row.get('engine', {}).get('sampling_plan', {}).get('enabled')
+ if not two_pass_input and status not in ('ram_guard', 'trial_memory_guard', 'timeout') and not failure['retryable']:
+ continue
+ reason = ('Verified input from two-pass generation; measuring a separate single-pass optimization baseline'
+ if two_pass_input else 'Verified input from a recoverable failed request')
+ name = row.get('id')
+ if not isinstance(name, str) or Path(name).name != name or name in ('.', '..'):
+ raise ValueError('Invalid failed-run case identifier')
+ case = directory / name
+ manifest = json.loads((case / 'artifacts.json').read_text(encoding='utf-8'))
+ entries = artifact_entries(manifest)
+ if row.get('input_mode') == 'preencoded':
+ copied = case / 'input-conditioning.pt'
+ if (copied.is_file() and tuning.digest(copied) == row.get('input_sha256')
+ and tuning.digest(copied) == entries.get('input-conditioning.pt', {}).get('sha256')):
+ return dict(conditioning=copied, geometry=row['geometry'], seed=row['seed'],
+ attention=capacity_attention(row['command']), source=str(case),
+ reason=reason)
+ # Prefer encoding already paid for, but only if a successful encoder
+ # receipt and its original artifact digest both survive.
+ encoding = row.get('encoding', {})
+ condition = case / 'video.artifacts/conditioning.pt'
+ entry = entries.get('video.artifacts/conditioning.pt', {})
+ if (encoding.get('success') is True and encoding.get('conditioning_shape', [])[1:] == [5120]
+ and condition.is_file() and tuning.digest(condition) == entry.get('sha256')):
+ return dict(conditioning=condition, geometry=row['geometry'], seed=row['seed'],
+ attention=row['request']['profile']['engine']['attention'],
+ source=str(case), reason=reason)
+ prompt = case / 'prompt.txt'
+ if (prompt.is_file() and tuning.digest(prompt) == entries.get('prompt.txt', {}).get('sha256')
+ and prompt.read_text(encoding='utf-8') == row.get('prompt')):
+ return dict(prompt_file=prompt, geometry=row['geometry'], seed=row['seed'],
+ attention=row['request']['profile']['engine']['attention'],
+ source=str(case), reason=reason)
+ raise ValueError('No recoverable failed request has verified inputs. Pass --prompt-file or --conditioning for a new baseline.')
+
+
+def settle_bootstrap_attempts(machine, case, measured):
+ """Known controller stops must not masquerade as an abandoned GPU crash."""
+ from .resource_history import ResourceHistory
+ path = Path(machine['root']) / 'resource-history.sqlite3'
+ if not path.is_file() or measured.get('status') == 'complete':
+ return []
+ try:
+ request = json.loads((case / 'video.request.json').read_text(encoding='utf-8'))
+ except (OSError, ValueError):
+ request = {}
+ identifiers = {item.get('id') for item in request.get('resource_attempts', []) if isinstance(item, dict)}
+ identifiers.add(request.get('encoding_attempt'))
+ artifacts = str(case / 'video.artifacts')
+ history = ResourceHistory(path)
+ settled = []
+ for attempt in history.attempts():
+ evidence = attempt.get('details', {}).get('decision_evidence', {})
+ if (attempt['outcome'] != 'pending' or
+ (attempt['id'] not in identifiers and evidence.get('request_artifacts') != artifacts)):
+ continue
+ status = measured.get('status')
+ if status in ('timeout', 'interrupted'):
+ outcome = 'cancelled'
+ elif status in ('ram_guard', 'trial_memory_guard'):
+ outcome = 'resource_failure'
+ else:
+ outcome = 'discarded'
+ settled.append(history.finish(attempt['id'], outcome, details={
+ 'reason': 'Bootstrap controller stopped: ' + str(status),
+ 'controlled_worker_stop': outcome != 'discarded', 'bootstrap_case': str(case)}))
+ return settled
+
+
+def bootstrap_baseline(spec, machine, expected, destination, seconds, env, ui):
+ """A new full request, with its own artifacts, within the total GPU budget."""
+ from .testing import run_process, artifact_manifest, artifact_estimate
+ from .validation import validate_metrics, validate_artifacts
+ root = destination / 'baseline'
+ case = root / '01-bootstrap'
+ case.mkdir(parents=True, exist_ok=False)
+ canvas = spec['geometry']
+ if shutil.disk_usage(case).free < artifact_estimate([{k: canvas[k] for k in ('width', 'height', 'frames')}]):
+ raise ValueError('Insufficient disk space for a retained full baseline request')
+ preencoded = bool(spec.get('conditioning'))
+ input_path = Path(spec['conditioning'] if preencoded else spec['prompt_file']).resolve()
+ source_hash = tuning.digest(input_path)
+ copied = case / ('input-conditioning.pt' if preencoded else 'prompt.txt')
+ shutil.copyfile(input_path, copied)
+ if tuning.digest(copied) != source_hash or tuning.digest(input_path) != source_hash:
+ raise ValueError('Baseline input changed while it was copied')
+ command = [machine['python'], '-m', 'freevideo_engine', 'generate',
+ '--conditioning' if preencoded else '--prompt-file', str(copied),
+ '--cache', machine['cache'], '--base', machine['base'], '--checkpoint', machine['checkpoint'],
+ '--encoder-python', machine['comfy_python'], '--encoder-root', machine['comfy_root'],
+ '--model-paths', machine['model_paths'], '--encoder', machine['encoder'],
+ '--out', str(case / 'video.mp4'), '--seed', str(spec['seed']),
+ '--width', str(canvas['width']), '--height', str(canvas['height']), '--frames', str(canvas['frames']),
+ '--attention', spec['attention'], '--no-tuning', '--no-two-pass']
+ for key in ('vram_gib', 'ram_gib'):
+ if machine.get(key) is not None:
+ command += ['--' + key.replace('_', '-'), str(machine[key])]
+ row = dict(id=case.name, geometry=canvas, seed=spec['seed'], command=command,
+ input_mode='preencoded' if preencoded else 'prompt', status='running',
+ input_sha256=source_hash, input_source=str(input_path))
+ if not preencoded:
+ row['prompt'] = copied.read_text(encoding='utf-8')
+ result = dict(schema_version=1, status='running', baseline_identity=expected, cases=[row],
+ scope='New complete baseline, not failed/probe metrics promoted to success',
+ max_seconds=seconds, input_reason=spec.get('reason', 'Explicit input'))
+ save(root / 'report.json', result)
+ descriptors = (int(os.environ[LOCK_ENV]),)
+ environment = dict(env, **{LOCK_ENV: os.environ[LOCK_ENV],
+ 'PYTHONPATH': os.pathsep.join((str(Path(__file__).resolve().parents[1]), env.get('PYTHONPATH', '')))})
+ started = time.perf_counter()
+ try:
+ ui.begin(case.name, 'Measure a complete baseline', detail='Full request; retained artifacts; shared total time budget')
+ row.update(run_process(command, case, env=environment, gpu=machine['gpu_uuid'], timeout=seconds,
+ minimum_ram=(2 if windows() else 1)*GiB, pass_fds=descriptors, ui=ui))
+ result['attempt_settlements'] = settle_bootstrap_attempts(machine, case, row)
+ if row['status'] != 'complete':
+ result['status'] = 'baseline-budget-exhausted' if row['status'] == 'timeout' else 'baseline-failed'
+ return root, result, [], time.perf_counter() - started
+ for key in ('request', 'engine') + (() if preencoded else ('encoding',)):
+ row[key] = json.loads((case / ('video.' + key + '.json')).read_text(encoding='utf-8'))
+ validate_metrics(row, canvas, preencoded=preencoded)
+ row['artifacts'] = validate_artifacts(case, canvas, preencoded=preencoded)
+ from .media import inspect
+ row['media'] = inspect(case / 'video.mp4', **{k: canvas[k] for k in ('width', 'height', 'frames', 'fps')})
+ shape = row['engine'].get('conditioning_shape') if preencoded else row['encoding'].get('conditioning_shape')
+ if not isinstance(shape, list) or len(shape) != 2 or type(shape[0]) is not int or shape[0] < 1 or shape[1] != 5120:
+ raise ValueError('A completed baseline must report its actual conditioning shape')
+ row['text_tokens'] = shape[0]
+ result['status'] = 'complete'
+ return root, result, [row], time.perf_counter() - started
+ except BaseException as error:
+ row.update(status='validation_failed', error=repr(error))
+ result.update(status='baseline-failed', error=repr(error))
+ raise
+ finally:
+ result['wall_seconds'] = time.perf_counter() - started
+ save(root / 'report.json', result)
+ save(case / 'case.json', row)
+ artifact_manifest(case)
+ ui.end(case.name, success=result['status'] == 'complete', detail=result['status'])
+
+
+def text_tokens(row):
+ return row.get('text_tokens') or row.get('encoding', {}).get('conditioning_shape', [None])[0]
+
+
+
+def settle_search_attempts(history, work, measured, trial, *, validation=None, error=None):
+ """Settle only this stopped worker's active attempt, including hard exits.
+
+ The journal write can precede optimization.json, so match its retained report
+ path rather than depending on the worker having emitted another message.
+ """
+ from .adaptive import classify_failure
+ settled = []
+ report = str(work / 'optimization.json')
+ for attempt in history.attempts():
+ if attempt['outcome'] != 'pending' or attempt.get('details', {}).get('decision_evidence', {}).get('report') != report:
+ continue
+ status = measured.get('status')
+ if status in ('timeout', 'interrupted'):
+ outcome, details = 'cancelled', {'reason': status, 'controlled_worker_stop': True}
+ elif status in ('ram_guard', 'trial_memory_guard'):
+ outcome, details = 'resource_failure', {'reason': status, 'controlled_worker_stop': True}
+ elif validation is not None:
+ outcome, details = 'success', dict(validation)
+ elif error is not None:
+ failure = classify_failure(error)
+ outcome, details = failure['outcome'], {'failure': failure, 'error': repr(error)}
+ elif status != 'complete' or not trial.get('success'):
+ failure = trial.get('failure')
+ if isinstance(failure, dict) and failure.get('outcome'):
+ outcome, details = failure['outcome'], {'failure': failure}
+ else:
+ outcome, details = 'discarded', {'reason': 'Worker exited without a settled attempt or structured failure'}
+ else:
+ # A completed full worker still awaits the parent's media checks.
+ continue
+ settled.append(history.finish(attempt['id'], outcome, details=details))
+ return settled
+
+
+def compare_arrays(first, second):
+ import numpy as np
+ results = {}
+ for name in ('rgb.npy', 'audio.npy'):
+ a, b = [np.load(root / name, mmap_mode='r', allow_pickle=False) for root in (first, second)]
+ same = a.shape == b.shape and a.dtype == b.dtype
+ if same:
+ for start in range(len(a)):
+ if not np.array_equal(a[start:start + 1].view(np.uint8), b[start:start + 1].view(np.uint8)):
+ same = False
+ break
+ results[name] = bool(same)
+ return results
+
+
+def write_report(directory, result):
+ save(directory / 'report.json', result)
+ search = result.get('search', {})
+ def number(value, divisor=1):
+ return '—' if value is None else '%.3f' % (value / divisor)
+ rows = ''.join('<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>' %
+ (html.escape(json.dumps(row['patch'])), html.escape(row.get('status', 'measured')),
+ number(row.get('seconds')), number(row.get('recheck_seconds')),
+ number(row.get('gpu', {}).get('gpu_peak_bytes'), GiB),
+ number(memory_peak(row.get('ram', {})), GiB)) for row in search.get('trials', []))
+ selected_directory = result.get('search_directory', 'search')
+ if (not isinstance(selected_directory, str) or selected_directory in ('.', '..')
+ or Path(selected_directory).name != selected_directory):
+ selected_directory = 'search'
+ media_path = selected_directory + '/video.mp4'
+ media = '<video controls src="%s"></video>' % html.escape(media_path, quote=True) if (directory / media_path).is_file() else ''
+ attempts = []
+ for item in result.get('search_attempts', []):
+ recovery = item.get('recovery') or {}
+ cells = [Path(item['directory']).name, item.get('status', 'unknown'),
+ json.dumps(recovery.get('next_patch', {})), recovery.get('reason', ''),
+ number(item.get('wall_seconds'))]
+ attempts.append('<tr>' + ''.join('<td>' + html.escape(str(cell)) + '</td>' for cell in cells) + '</tr>')
+ attempt_table = ('<h2>Retained search attempts</h2><table><tr><th>Directory</th><th>Status</th>'
+ '<th>Retry configuration</th><th>Reason</th><th>Total elapsed seconds</th></tr>'
+ + ''.join(attempts) + '</table>') if attempts else ''
+ plans = [('Initial search', result.get('candidate_plan', {}))]
+ plans += [('Replanned search %d' % (index + 1), plan)
+ for index, plan in enumerate(search.get('candidate_plans', []))]
+ reasons = []
+ for label, plan in plans:
+ for row in plan.get('candidates', []) + plan.get('rejected', []):
+ cells = [label, json.dumps(row.get('patch', {}), ensure_ascii=False),
+ row.get('reason', ''), json.dumps(row.get('observation', {}), ensure_ascii=False),
+ row.get('risk', ''), row.get('rejected_reason', 'Candidate; full local validation required')]
+ reasons.append('<tr>' + ''.join('<td>' + html.escape(str(cell)) + '</td>' for cell in cells) + '</tr>')
+ explanations = ('<details open><summary>Why these candidates</summary><table><tr><th>Plan</th><th>Change</th>'
+ '<th>Reason</th><th>Measured evidence</th><th>Risk</th><th>Admission</th></tr>' + ''.join(reasons) +
+ '</table><p>Other-machine evidence ranks a hypothesis; only a complete local request can validate it.</p></details>') if reasons else ''
+ bootstrap = result.get('bootstrap') or {}
+ remaining = result.get('remaining_search_seconds')
+ if remaining is None and result.get('max_seconds') is not None:
+ remaining = max(0., result['max_seconds'] - bootstrap.get('wall_seconds', 0.))
+ budget = ('<p>Total worker budget: ' + number(result.get('max_seconds')) + ' s. New baseline used: ' +
+ number(bootstrap.get('wall_seconds', 0.)) + ' s. Remaining search budget: ' +
+ number(remaining) + ' s.</p>')
+ if bootstrap:
+ budget += '<p>New full baseline: ' + html.escape(str(bootstrap.get('status', 'unknown'))) + '</p>'
+ (directory / 'report.html').write_text('<!doctype html><meta charset="utf-8"><title>FreeVideo optimization</title>'
+ '<style>body{font:16px system-ui;background:#111827;color:#e5e7eb;margin:32px}a{color:#93c5fd}'
+ 'td,th{padding:10px;border:1px solid #4b5563}table{border-collapse:collapse}video{max-width:800px;width:100%}</style>'
+ '<h1>FreeVideo local optimization</h1><p>Status: ' + html.escape(result['status']) + '</p>'
+ '<p>Validated prompts cached: ' + str(result.get('cached_prompts', 0)) + '. Sampling profile activated: ' +
+ html.escape(str(result.get('activated_profile') or 'none')) + '</p>'
+ + budget + explanations + attempt_table +
+ '<p>Single-step probes below preserve the eight-step schedule and full geometry; they are not complete video times.</p>'
+ '<p>Baseline step measurements: ' + html.escape(str(search.get('baseline_step_seconds', []))) + '</p>'
+ '<table><tr><th>Candidate</th><th>Status</th><th>Step seconds</th><th>Recheck seconds</th><th>Peak VRAM GiB</th><th>Peak RAM GiB</th></tr>' + rows + '</table>'
+ '<p>Run test again for complete request time, text-cache hits, sampling, VRAM/RAM and matched baseline comparisons. '
+ 'OS/JIT and file cache warmth can affect results. Every original artifact is retained.</p>'
+ '<p><a href="report.json">Full report</a> · <a href="baseline.json">Baseline metrics</a></p>' + media, encoding='utf-8')
+
+
+def repeated_gain(baseline, candidate, recheck):
+ values = [*baseline, candidate, recheck]
+ return (len(baseline) >= 2 and all(isinstance(v, (int, float)) and math.isfinite(v) and v > 0 for v in values)
+ and max(candidate, recheck) < min(baseline) * .95)
+
+
+def retry_search_request(request, trial):
+ """Retain an OOM trial, then nominate one alternative in a fresh worker.
+
+ A failed probe neither certifies capacity nor blacklists a configuration.
+ Exclusions live only in this bounded search. Production quality gates and
+ the original complete reference remain in force.
+ """
+ if trial.get('status') != 'resource-rejected':
+ return None
+ failure = trial.get('failure', {})
+ if failure.get('kind') not in ('gpu_oom', 'ram_pressure'):
+ return None
+ failed = next((item.get('patch') for item in reversed(trial.get('trials', []))
+ if item.get('status') == 'memory-rejected'), None)
+ if not isinstance(failed, dict) or not failed or not set(failed).issubset(tuning.PATCH_KEYS):
+ return None
+ rejected = list(request.get('rejected_candidates', []))
+ for item in trial.get('trials', []):
+ patch = item.get('patch')
+ if isinstance(patch, dict) and set(patch).issubset(tuning.PATCH_KEYS) and patch not in rejected:
+ rejected.append(patch)
+ base = request['profile']
+ row = request.get('complete_baseline', {})
+ engine = dict(base['engine'], **failed)
+ rescue = None
+ if failure['kind'] == 'gpu_oom' and engine.get('resident_blocks', 0) > 0:
+ # Spend existing weight residency before shrinking a worthwhile head
+ # group. Only nominate this when the complete baseline's RAM accounting
+ # covers every extra offloaded weight; no unbudgeted host copy is implied.
+ ram = row.get('ram', {})
+ peak = memory_peak(ram)
+ extra = engine['resident_blocks'] * tuning.BLOCK_BYTES
+ if (tuning._complete_peak(row, base)[0] is not None and memory_complete(ram) and tuning._positive(peak)
+ and peak + extra <= base['inference_ram_budget_gb'] * 1e9):
+ rescue = dict(failed, resident_blocks=0, pin_host_gb=engine.get('pin_host_gb', 0.))
+ elif failure['kind'] == 'ram_pressure' and engine.get('pin_host_gb', 0) > 0:
+ rescue = dict(failed, pin_host_gb=0., resident_blocks=engine['resident_blocks'])
+ choices = ([rescue] if rescue and rescue not in rejected else [])
+ choices += tuning.candidates(row, base, maximum=20,
+ reject=lambda patch: 'Resource failure in this search' if patch in rejected else None)
+ if not choices:
+ return None
+ result = copy.deepcopy(request)
+ result.update(candidates=[choices[0]], adaptive_candidates=False,
+ rejected_candidates=rejected, max_placement_trials=1,
+ recovery={'reason': 'Fresh worker after recoverable memory failure; try another configuration within the same time budget',
+ 'failure': failure, 'failed_patch': failed, 'next_patch': choices[0]})
+ return result
+
+
+def run_search_attempts(command, work, request, *, seconds, env, gpu, ui, maximum_working_ram, history=None):
+ """At most two fresh-worker retries, all inside the original search budget."""
+ from .testing import run_process, artifact_manifest
+ if not tuning._positive(seconds):
+ raise ValueError('A positive finite search time budget is required')
+ started = time.monotonic()
+ attempts = []
+ first = work
+ measured, trial = {'status': 'timeout', 'wall_seconds': 0.}, {}
+ for index in range(3):
+ remaining = seconds - (time.monotonic() - started)
+ if remaining <= 0:
+ break
+ if index:
+ work = first.with_name(first.name + '-retry-%02d' % index)
+ work.mkdir()
+ artifacts = work / 'video.artifacts'
+ artifacts.mkdir()
+ shutil.copyfile(request['conditioning'], artifacts / 'conditioning.pt')
+ request.update(out=str(work), conditioning=str(artifacts / 'conditioning.pt'))
+ save(artifacts / 'video.json', request)
+ save(work / 'request.json', request)
+ ui.begin('search', 'Retry with another configuration', detail=request['recovery']['reason'])
+ argv = list(command)
+ argv[-1] = str(work / 'request.json')
+ # Preparing a retry also spends wall time; never reset or extend the
+ # worker deadline while copying retained inputs and writing receipts.
+ remaining = seconds - (time.monotonic() - started)
+ if remaining <= 0:
+ break
+ measured, trial = {}, {}
+ try:
+ measured = run_process(argv, work, env=env, gpu=gpu, timeout=remaining,
+ minimum_ram=(2 if windows() else 1)*GiB, pass_fds=(int(env[LOCK_ENV]),), ui=ui,
+ maximum_working_ram=maximum_working_ram)
+ trial_path = work / 'optimization.json'
+ trial = json.loads(trial_path.read_text(encoding='utf-8')) if trial_path.is_file() else {}
+ except BaseException as error:
+ settlements = settle_search_attempts(history, work, measured, trial, error=error) if history is not None else []
+ attempts.append(dict(directory=str(work), measurement=measured, status='failed', error=repr(error),
+ recovery=request.get('recovery'), settlements=settlements,
+ wall_seconds=time.monotonic() - started))
+ save(first.parent / 'search-attempts.json', attempts)
+ raise
+ settlements = settle_search_attempts(history, work, measured, trial) if history is not None else []
+ attempts.append(dict(directory=str(work), measurement=measured,
+ status=trial.get('status'), recovery=request.get('recovery'),
+ settlements=settlements,
+ wall_seconds=time.monotonic() - started))
+ save(first.parent / 'search-attempts.json', attempts)
+ artifact_manifest(work)
+ if measured['status'] != 'complete' or index == 2:
+ break
+ next_request = retry_search_request(request, trial)
+ if next_request is None:
+ break
+ request = next_request
+ return work, measured, trial, attempts
+
+
+def search_plan(row, base, maximum, seconds, *, reject=None):
+ """Count reloads only for placement trials; keep cheap trials on slow disks."""
+ pool = tuning.candidates(row, base, maximum=20, reject=reject)
+ while True:
+ selected = pool[:maximum]
+ placements = sum(bool(set(patch) & tuning.PLACEMENT_KEYS) for patch in selected)
+ # A placement can require returning to the best previous state. Reserve
+ # baseline/winner reloads too; chunk-only trials keep the same model.
+ loads = 1 if not placements else 3 + 2 * placements
+ estimate = (row['engine']['load_seconds'] * loads + row['engine']['decode_save_seconds']
+ + row['engine']['sample_seconds'] * (12 + 2 * len(selected)) / 8)
+ if not selected or estimate <= seconds:
+ return selected, estimate, placements
+ expensive = next((p for p in selected if set(p) & tuning.PLACEMENT_KEYS), selected[-1])
+ pool.remove(expensive)
+
+
+def run(args, machine, ui):
+ from .testing import environment, run_process, artifact_manifest, artifact_estimate
+ env = environment(machine)
+ os.environ.update(env)
+ if args.disable:
+ state, reason = tuning.load_state()
+ if state:
+ state.update(enabled=False, disabled_epoch=time.time())
+ tuning.save_state(state)
+ print('Local optimization disabled. Saved reports and conditioning files are retained.')
+ return 0
+ hardware = detect()
+ expected = tuning.identity(machine, hardware)
+ from .adaptive import local_identity
+ resource_identity = local_identity(hardware, machine['cache'])
+ selected = args.report or latest(Path(machine['root']), 'test-runs')
+ destination = (args.out or Path(machine['root']) / 'optimization-runs' /
+ (time.strftime('%Y%m%dT%H%M%SZ', time.gmtime()) + '-' + str(os.getpid()))).resolve()
+ bootstrap_seconds, bootstrap = 0., None
+ spec = None
+ if getattr(args, 'conditioning', None) or getattr(args, 'prompt_file', None):
+ from .geometry import geometry
+ spec = dict(geometry=geometry(args.width, args.height, frames=args.frames), seed=args.seed,
+ attention=args.attention, conditioning=args.conditioning, prompt_file=args.prompt_file)
+ elif selected:
+ try:
+ source, baseline, usable = read_baseline(selected.resolve(), expected)
+ except NoCompleteBaseline:
+ spec = recovery_input(selected.resolve(), expected)
+ else:
+ raise ValueError('No baseline found. Pass --prompt-file PATH or --conditioning PATH to measure a full baseline within --max-seconds.')
+ if spec is not None:
+ ui.panel('FreeVideo / establish a complete baseline', [
+ ('Input', spec.get('conditioning') or spec.get('prompt_file')),
+ ('Request', '%dx%d, %d frames, 8 steps, attention %s' %
+ (spec['geometry']['width'], spec['geometry']['height'], spec['geometry']['frames'], spec['attention'])),
+ ('Budget', 'At most %.0fs for baseline AND search; complete-request time is not yet known' % args.max_seconds),
+ ('Validation', 'A new complete request is required. Failed runs and probes are not a baseline.'),
+ ('Outputs', destination)])
+ if args.plan:
+ return 0
+ destination.mkdir(parents=True, exist_ok=False)
+ ui.start(destination)
+ try:
+ source, baseline, usable, bootstrap_seconds = bootstrap_baseline(
+ spec, machine, expected, destination, args.max_seconds, env, ui)
+ finally:
+ ui.close()
+ bootstrap = dict(report=str(source / 'report.json'), wall_seconds=bootstrap_seconds, status=baseline['status'])
+ if not usable:
+ write_report(destination, dict(status=baseline['status'], bootstrap=bootstrap,
+ max_seconds=args.max_seconds, search_note='No failed metrics were accepted. Retained inputs may be retried with an explicit larger budget.'))
+ return 1
+ remaining_seconds = max(0., args.max_seconds - bootstrap_seconds)
+ if bootstrap is not None:
+ hardware = detect() # Re-read live memory after the complete child exits.
+ # Prefer the most frequently tested geometry, then a later warm request.
+ geometry_key = lambda r: tuple(r['geometry'][k] for k in ('width', 'height', 'frames'))
+ counts = {geometry_key(r): sum(geometry_key(s) == geometry_key(r) for s in usable) for r in usable}
+ row = max(enumerate(usable), key=lambda pair: (counts[geometry_key(pair[1])], pair[0]))[1]
+ resources = {k: machine.get(k) for k in ('vram_gib', 'ram_gib')}
+ resources.update(capacity_flags(row['command']))
+ base = choose(hardware, **resources, attention=row['request']['profile']['engine']['attention'],
+ available_backends=available_backends(hardware, probe_missing=False), canvas=row['geometry']).legacy_profile()
+ base['engine'].update(base=str(Path(machine['base']).resolve()), checkpoint=str(Path(machine['checkpoint']).resolve()))
+ automatic = copy.deepcopy(base)
+ if baseline.get('baseline_identity') is not None:
+ # A recovered request is the actual validated baseline. Reintroducing
+ # the original failed placement here would recreate the cold-start loop.
+ completed = row['request']['profile']
+ base['engine'] = copy.deepcopy(completed['engine'])
+ base['decoder'] = copy.deepcopy(completed['decoder'])
+ for budget in ('gpu_budget_gb', 'inference_ram_budget_gb'):
+ base[budget] = min(base[budget], completed[budget])
+ previous, _ = tuning.load_state(expected)
+ base, starting_profile = tuning.apply_profile(previous or {}, base, row['geometry'], text_tokens(row))
+ placement_baseline = None
+ if starting_profile is None:
+ base, placement_baseline = tuning.completed_placement_baseline(row, base)
+ same_placement = base['engine'] == row['request']['profile']['engine'] and base['decoder'] == row['request']['profile']['decoder']
+ from .resource_history import ResourceHistory
+ history_path = Path(machine['root']) / 'resource-history.sqlite3'
+ # Read-only planning must not create a database. Execution requires a
+ # durable journal before any worker starts.
+ history = ResourceHistory(history_path) if not args.plan or history_path.exists() else None
+ plan = tuning.candidate_plan(row, base, maximum=20)
+ candidates, estimated, placement_trials = search_plan(row, base, args.max_trials, remaining_seconds)
+ ui.panel('FreeVideo / local optimization', [('Baseline', source), ('GPU', hardware.gpu_name),
+ ('Text reuse', 'Import %d validated requests; cache later prompts after successful encoding' % len(usable)),
+ ('Sampling search', '%d candidates · at most %.0fs · identical backend, precision, resolution and steps' % (len(candidates), args.max_seconds)),
+ ('Validation', 'Repeat speed gain, memory budgets, full eight steps, identical video/audio latents and decoded output'),
+ ('Outputs', destination)])
+ if args.plan:
+ return 0
+ if bootstrap is None:
+ destination.mkdir(parents=True, exist_ok=False)
+ required_disk = artifact_estimate([{k: row['geometry'][k] for k in ('width', 'height', 'frames')}]) if candidates else 128 * 2**20
+ if shutil.disk_usage(destination).free < required_disk:
+ raise ValueError('Insufficient free disk space for retained optimization artifacts')
+ state = copy.deepcopy(previous) if previous else {'schema': tuning.SCHEMA, 'enabled': True, 'identity': expected,
+ 'conditioning': {}, 'profiles': []}
+ state.update(enabled=True, baseline_report=str(source / 'report.json'), optimization_report=str(destination / 'report.json'))
+ result = {'schema_version': 1, 'status': 'running', 'baseline_report': str(source / 'report.json'),
+ 'identity': expected, 'selected_case': row['id'], 'candidates': candidates, 'candidate_plan': plan,
+ 'scope': 'Persistent inputs plus bounded, measured placement/chunk/prefetch search. No model/precision/backend or quality reduction.',
+ 'starting_profile': starting_profile, 'fresh_placement_measurement': not same_placement,
+ 'placement_baseline': placement_baseline,
+ 'max_seconds': args.max_seconds, 'remaining_search_seconds': remaining_seconds, 'bootstrap': bootstrap,
+ 'capacity_scope': 'Placement budgets and sampled guards; no kernel hard limits created.'}
+ save(destination / 'baseline.json', baseline)
+ save(destination / 'report.json', result)
+ ui.start(destination)
+ started = time.perf_counter()
+ work, measured, trial = None, {}, {}
+ try:
+ ui.phase('Reuse validated text conditioning', 0, 2)
+ for item in usable:
+ if item.get('input_mode') == 'preencoded':
+ continue # No invented prompt/encoder receipt for an opaque input.
+ tuning.remember_conditioning(state, item['prompt'], source / item['id'] / 'video.artifacts/conditioning.pt', item['encoding'])
+ tuning.save_state(state)
+ result['cached_prompts'] = len(state['conditioning'])
+ result['status'] = 'conditioning-cache-ready'
+ if not candidates:
+ result['search_note'] = 'No candidate fits the bounded search plan or max-trials is zero; previous settings retained.'
+ return 0
+ ui.phase('Measure candidates and validate a winner', 1, 2)
+ work = destination / 'search'
+ work.mkdir()
+ artifacts = work / 'video.artifacts'
+ artifacts.mkdir()
+ condition = artifacts / 'conditioning.pt'
+ shutil.copyfile(source / row['id'] / 'video.artifacts/conditioning.pt', condition)
+ (artifacts / 'prompt.txt').write_text(row.get('prompt', ''), encoding='utf-8')
+ from .resource_history import process_identity
+ request = {'out': str(work), 'cache': machine['cache'], 'profile': base, 'geometry': row['geometry'],
+ 'input_cache_dir': str(Path(machine['root']) / 'input-cache'),
+ 'conditioning': str(condition), 'reference_latents': str(source / row['id'] / 'video.artifacts/latents.pt'),
+ 'seed': row.get('seed', 2026090901), 'candidates': candidates, 'adaptive_candidates': True,
+ 'complete_baseline': row,
+ 'max_placement_trials': placement_trials,
+ 'resource_history': str(Path(machine['root']) / 'resource-history.sqlite3'),
+ 'resource_identity': resource_identity, 'resource_owner': process_identity()}
+ save(work / 'request.json', request)
+ save(artifacts / 'video.json', request)
+ ui.begin('search', 'Bounded sampling search', detail='Measure live headroom; reload only for placement trials; validate one full winner')
+ from .system import inference_environment
+ search_env = inference_environment(dict(env, **{LOCK_ENV: os.environ[LOCK_ENV],
+ 'PYTORCH_ALLOC_CONF': base['allocator_config'], 'PYTORCH_CUDA_ALLOC_CONF': base['allocator_config']}),
+ base['inference_ram_budget_gb'] * 1e9)
+ work, measured, trial, result['search_attempts'] = run_search_attempts(
+ [machine['python'], '-m', 'freevideo_engine.optimize_worker', '--request', str(work / 'request.json')],
+ work, request, env=search_env, gpu=machine['gpu_uuid'], seconds=remaining_seconds, ui=ui,
+ maximum_working_ram=base['inference_ram_budget_gb'] * 1e9, history=history)
+ result['search_directory'] = work.name
+ # GPU placement budget is not an allocator/telemetry cutoff. A complete
+ # request can reserve slightly more; actual OOM and full-run margins
+ # determine rejection without terminating a working request mid-step.
+ ui.end('search', success=measured['status'] == 'complete', detail=measured['status'])
+ result['measurement'] = measured
+ result['search'] = trial
+ result['attempt_settlements'] = settle_search_attempts(history, work, measured, trial)
+ artifact_manifest(work)
+ if measured['status'] != 'complete' or not trial.get('success'):
+ result['status'] = 'conditioning-cache-ready-search-failed'
+ return 1
+ if trial['status'] not in ('validated-candidate', 'numerical-change-rejected'):
+ result['status'] = 'conditioning-cache-ready-no-sampling-change'
+ return 0
+ from .media import inspect
+ from .validation import validate_metrics, validate_artifacts
+ engine_metrics = json.loads((work / 'video.engine.json').read_text(encoding='utf-8'))
+ full = dict(request=dict(success=True, geometry=row['geometry'], request_seconds=measured['wall_seconds']),
+ engine=engine_metrics, gpu=measured['gpu'], ram=measured['ram'])
+ validate_metrics(full, row['geometry'], preencoded=True)
+ validate_artifacts(work, row['geometry'], preencoded=True)
+ media = inspect(work / 'video.mp4', **{k: row['geometry'][k] for k in ('width', 'height', 'frames', 'fps')})
+ output_equal = compare_arrays(work / 'video.artifacts', source / row['id'] / 'video.artifacts')
+ result['media'] = media
+ result['decoded_output_equal'] = output_equal
+ full_validation = dict(full_request=True, completed_steps=8, media_verified=True,
+ bitwise_latents=trial.get('validation', {}).get('bitwise_latents') is True,
+ decoded_output_equal=output_equal, memory_checked=True,
+ scope='Full local request; successful completion is separate from numerical equivalence')
+ result['attempt_settlements'] += settle_search_attempts(history, work, measured, trial, validation=full_validation)
+ gpu, ram = measured['gpu'], measured['ram']
+ good_memory = not gpu.get('sampling_errors') and gpu.get('samples') and memory_complete(ram) and ram.get('ram_observation_samples')
+ if trial['status'] != 'validated-candidate' or not all(output_equal.values()) or not good_memory or trial['full_sample_seconds'] >= row['engine']['sample_seconds']:
+ result['status'] = 'conditioning-cache-ready-full-validation-rejected'
+ return 0
+ selected_engine = dict(base['engine'], **trial['selected_patch'])
+ selected_patch = {k: selected_engine[k] for k in tuning.PATCH_KEYS
+ if k in tuning.PLACEMENT_KEYS or selected_engine[k] != automatic['engine'][k]}
+ winner = next(item for item in trial['trials'] if item.get('patch') == trial['selected_patch'])
+ entry = {'id': tuning.key({'identity': expected, 'run': str(destination), 'patch': selected_patch}),
+ 'enabled': True, 'base_engine': automatic['engine'], 'decoder': base['decoder'],
+ 'geometry': {k: row['geometry'][k] for k in ('width', 'height', 'frames')},
+ 'text_tokens': text_tokens(row), 'patch': selected_patch,
+ 'required_gpu_bytes': gpu['gpu_peak_bytes'] + max(64 * 2**20, int(gpu['gpu_peak_bytes'] * .01)),
+ 'required_ram_bytes': memory_peak(ram) + (2 if windows() else 1)*GiB,
+ 'performance': {'baseline_step_seconds': trial['baseline_step_seconds'],
+ 'candidate_step_seconds': [winner['seconds'], winner['recheck_seconds']],
+ 'baseline_full_sample_seconds': row['engine']['sample_seconds'],
+ 'candidate_full_sample_seconds': trial['full_sample_seconds']},
+ 'validation': dict(trial['validation'], decoded_output_equal=output_equal, media=media,
+ memory_checked=True, full_request=True, completed_steps=8),
+ 'report': str(destination / 'report.json')}
+ if not tuning.compatible_profile(entry, automatic, row['geometry'], entry['text_tokens']):
+ result['status'] = 'conditioning-cache-ready-insufficient-margin'
+ return 0
+ state['profiles'].append(entry)
+ tuning.save_state(state)
+ result.update(status='optimized', activated_profile=entry['id'])
+ return 0
+ except BaseException as error:
+ result.update(status='failed', error=repr(error), traceback=traceback.format_exc())
+ if work is not None:
+ result.setdefault('attempt_settlements', []).extend(
+ settle_search_attempts(history, work, measured, trial, error=error))
+ raise
+ finally:
+ result['wall_seconds'] = bootstrap_seconds + time.perf_counter() - started
+ try:
+ write_report(destination, result)
+ ui.phase(result['status'], 2, 2)
+ finally:
+ ui.close()
+ print('Optimization report: %s\nStatus: %s\nRun ./test.sh again to measure the result.' % (destination / 'report.html', result['status']))
+
+
+def main(argv=None):
+ parser = argparse.ArgumentParser(prog='./freevideo optimize', description=__doc__)
+ parser.add_argument('--config', type=Path)
+ parser.add_argument('--root', type=Path, default=Path(os.environ.get('FREEVIDEO_HOME', DEFAULT_ROOT)))
+ parser.add_argument('--report', type=Path, help='Existing test report.json or its directory; default latest test')
+ source = parser.add_mutually_exclusive_group()
+ source.add_argument('--conditioning', type=Path, help='Measure a new full baseline from existing conditioning')
+ source.add_argument('--prompt-file', type=Path, help='Measure a new full baseline from a UTF-8 prompt')
+ parser.add_argument('--width', type=int, default=1344)
+ parser.add_argument('--height', type=int, default=768)
+ parser.add_argument('--frames', type=int, default=243)
+ parser.add_argument('--seed', type=int, default=2026090901)
+ parser.add_argument('--attention', default='auto')
+ parser.add_argument('--out', type=Path)
+ parser.add_argument('--max-trials', type=int, default=2, help='0–4 neighboring configurations; default 2')
+ parser.add_argument('--max-seconds', type=float, default=600, help='Maximum total GPU worker time: new baseline (if needed), loading, probes and full winner generation')
+ parser.add_argument('--plan', action='store_true')
+ parser.add_argument('--disable', action='store_true', help='Disable automatic reuse; retain every saved file')
+ parser.add_argument('--plain', action='store_true')
+ parser.add_argument('--no-color', action='store_true')
+ args = parser.parse_args(argv)
+ if not 0 <= args.max_trials <= 4 or not math.isfinite(args.max_seconds) or args.max_seconds < 1:
+ parser.error('Use 0–4 candidates and a positive finite time budget')
+ if args.plan and args.disable:
+ parser.error('--plan and --disable cannot be combined')
+ config = args.config or args.root / 'machine.json'
+ def interrupted(signum, frame):
+ raise KeyboardInterrupt('Optimization interrupted')
+ previous_signal = processes.termination_handler(interrupted)
+ try:
+ machine = json.loads(config.read_text(encoding='utf-8'))
+ if not machine.get('ready'):
+ raise ValueError('Setup is incomplete')
+ from .testing import environment
+ os.environ.update(environment(machine))
+ with nullcontext() if args.plan else runtime_lock() as descriptor:
+ previous = os.environ.get(LOCK_ENV)
+ if descriptor is not None:
+ os.environ[LOCK_ENV] = str(descriptor)
+ try:
+ return run(args, machine, TerminalUI('Local optimization', plain=args.plain, no_color=args.no_color))
+ finally:
+ if previous is None:
+ os.environ.pop(LOCK_ENV, None)
+ else:
+ os.environ[LOCK_ENV] = previous
+ except (OSError, ValueError, RuntimeError) as error:
+ print('Optimization could not complete: ' + str(error), file=sys.stderr)
+ return 1
+ except KeyboardInterrupt:
+ print('Optimization cancelled; reports and artifacts retained.', file=sys.stderr)
+ return 130
+ finally:
+ processes.restore_handlers(previous_signal)
+
+
+if __name__ == '__main__':
+ raise SystemExit(main())
diff --git a/freevideo_engine/optimize_worker.py b/freevideo_engine/optimize_worker.py
new file mode 100644
index 0000000..f91622b
--- /dev/null
+++ b/freevideo_engine/optimize_worker.py
@@ -0,0 +1,362 @@
+"""Small measured trials; reuse compute state, reload only for placement changes."""
+import argparse
+import copy
+from contextlib import contextmanager
+import gc
+import hashlib
+import json
+from pathlib import Path
+import statistics
+import time
+import traceback
+import os
+import threading
+
+import torch
+
+from .decode import decode_to_file
+from .locking import runtime_lock
+from .monitoring import save, Monitor
+from .ram import ProcessMemory
+from .runtime import Engine
+from .tuning import PATCH_KEYS, PLACEMENT_KEYS
+from .processes import worker_signals
+from .system import inference_headroom
+
+
+def configure(engine, base, patch):
+ if not set(patch).issubset(PATCH_KEYS):
+ raise ValueError('Unsupported tuning parameter')
+ options = dict(base, **patch)
+ options.setdefault('head_parallelism', 1)
+ from .compute_budget import valid_parallelism
+ if not valid_parallelism(options):
+ raise ValueError('Parallel head groups require full GPU residency and GPU outputs')
+ if engine.closed or any(engine.config.get(name) != options[name] for name in PLACEMENT_KEYS):
+ cache, inputs, canvas = engine.cache, engine.input_cache_dir, engine.canvas
+ engine.close()
+ # Do not keep old pinned weights while allocating the candidate. Model
+ # files and AdaLN tables stay on disk and are reused by the next load.
+ return Engine(cache, input_cache_dir=inputs, **dict(options, canvas=canvas))
+ engine.prefetch = options['prefetch']
+ engine.attention.window_batch = options['window_batch']
+ engine.attention.batches = None
+ from .head_chunk import install_head_chunks
+ install_head_chunks(engine.transformer, engine.attention, options['head_chunk'],
+ cpu_outputs=options['attention_cpu_outputs'], projection_chunk=options['projection_chunk'],
+ grouped_outputs=options['grouped_attention_outputs'], parallelism=options['head_parallelism'])
+ if options['linear_compute'] == 'native-fp8':
+ from .fp8_ops import install_chunked_ff
+ for block in engine.transformer.transformer_blocks:
+ install_chunked_ff(block.ff, options['ff_chunk'], recompute=options['fp8_ff_recompute'])
+ elif options['ff_chunk'] != base['ff_chunk']:
+ raise ValueError('This bounded search keeps Ampere FF chunking fixed')
+ from .packing import install_streamed_forward
+ install_streamed_forward(engine.transformer, options['projection_chunk'])
+ engine.config.update({name: options[name] for name in PATCH_KEYS})
+ return engine
+
+
+def measured_probe(engine, request, label):
+ """Whole-device VRAM plus the worker's physical/commit RAM during a probe."""
+ folder = Path(request['out']) / 'probes'
+ folder.mkdir(exist_ok=True)
+ gpu = Monitor(folder / (label + '.csv')).start()
+ ram, stop, errors = ProcessMemory(), threading.Event(), []
+ def sample_ram():
+ while not stop.is_set():
+ try:
+ ram.sample(os.getpid())
+ except Exception as error:
+ errors.append(str(error))
+ stop.wait(.1)
+ thread = threading.Thread(target=sample_ram, daemon=True)
+ thread.start()
+ try:
+ measured = probe(engine, request['conditioning'], request['seed'], request['geometry'], fingerprint=True)
+ finally:
+ stop.set()
+ thread.join()
+ gpu_result = gpu.stop()
+ result = {'gpu': gpu_result, 'ram': ram.result(), 'engine': {'sample_seconds': measured['seconds']},
+ 'prediction_fingerprint': measured['fingerprint']}
+ from .system import memory_complete
+ if (errors or gpu_result.get('sampling_errors') or not gpu_result.get('samples')
+ or not memory_complete(result['ram']) or not result['ram'].get('ram_observation_samples')):
+ raise RuntimeError('Incomplete optimization memory observations: ' + repr(errors))
+ save(folder / (label + '.json'), result)
+ return result
+
+
+def probe(engine, conditioning, seed, canvas, *, fingerprint=False):
+ """One complete model forward from the original eight-step schedule.
+
+ Probe timings exclude text refinement/loading and are never reported as a
+ generated eight-step video. The best candidate still needs full validation.
+ """
+ class Finished(Exception):
+ pass
+ times, started, fingerprints = [], [], []
+ def before(module, arguments):
+ if times:
+ raise Finished()
+ torch.cuda.synchronize()
+ started.append(time.perf_counter())
+ def after(module, arguments, output):
+ torch.cuda.synchronize()
+ times.append(time.perf_counter() - started[-1])
+ values = (output.sample, output.audio_sample) if hasattr(output, 'sample') else output
+ if not all(bool(torch.isfinite(value).all()) for value in values):
+ raise RuntimeError('Non-finite trial prediction')
+ if fingerprint:
+ # Reject a changed prediction cheaply; a matching first step still
+ # cannot certify full-video equivalence or memory capacity. Hash
+ # after timing, retain no reference tensors or GPU allocations.
+ for value in values:
+ host = value.detach().cpu().contiguous()
+ raw = memoryview(host.view(torch.uint8).numpy()).cast('B')
+ fingerprints.append(dict(shape=list(value.shape), dtype=str(value.dtype),
+ sha256=hashlib.sha256(raw).hexdigest()))
+ del raw, host
+ handles = [engine.transformer.register_forward_pre_hook(before), engine.transformer.register_forward_hook(after)]
+ original_error = None
+ try:
+ engine.sample(conditioning, seed, **{k: canvas[k] for k in ('frames', 'width', 'height')})
+ raise RuntimeError('Trial did not stop at its documented step boundary')
+ except Finished:
+ if len(times) != 1:
+ raise RuntimeError('Expected exactly one measured model step')
+ return dict(seconds=times[0], fingerprint=fingerprints) if fingerprint else times[0]
+ except BaseException as error:
+ original_error = error
+ raise
+ finally:
+ cleanup_errors = []
+ for operation in [handle.remove for handle in handles] + [gc.collect, torch.cuda.empty_cache]:
+ try:
+ operation()
+ except BaseException as error:
+ cleanup_errors.append(error)
+ if cleanup_errors:
+ if original_error is None:
+ raise cleanup_errors[0]
+ original_error.cleanup_errors = [repr(error) for error in cleanup_errors]
+
+
+def run(request):
+ torch.set_num_threads(8)
+ root = Path(request['out'])
+ result = {'success': False, 'status': 'running', 'trials': [], 'selected_patch': None,
+ 'scope': 'Single-step trials at the full requested geometry and eight-step schedule; '
+ 'only a full eight-step, bitwise-latent and media-validated winner may be activated.'}
+ base, canvas = request['profile']['engine'], request['geometry']
+ engine = None
+ original_error = None
+ started = time.perf_counter()
+ from .resource_history import ResourceHistory
+ from .adaptive import classify_failure
+ history = ResourceHistory(request['resource_history'])
+ identity = request['resource_identity']
+
+ @contextmanager
+ def attempt(phase, patch=None, full=False):
+ profile = dict(request['profile'], engine=dict(base, **(patch or {})))
+ identifier = history.begin(identity, profile, canvas,
+ purpose='validation' if full else 'probe', owner=request.get('resource_owner'),
+ evidence={'phase': phase, 'report': str(root / 'optimization.json'), 'patch': patch or {}})
+ history.attach_worker(identifier, os.getpid(), phase=phase)
+ result['active_attempt'] = identifier
+ result.setdefault('attempt_ids', []).append(identifier)
+ save(root / 'optimization.json', result)
+ try:
+ yield
+ except BaseException as error:
+ failure = classify_failure(error, dict(result, cleanup_errors=result.get('cleanup_errors', []) + getattr(error, 'cleanup_errors', [])))
+ history.finish(identifier, failure['outcome'], details={'phase': phase, 'failure': failure, 'error': repr(error),
+ 'cleanup_errors': getattr(error, 'cleanup_errors', [])})
+ result['active_attempt'] = None
+ raise
+ else:
+ if full:
+ # The controller owns media/raw-array/memory validation after
+ # this worker exits. A passing probe cannot write calibration.
+ result['full_attempt'] = identifier
+ else:
+ history.finish(identifier, 'success', details={'phase': phase, 'full_request': False})
+ result['active_attempt'] = None
+ finally:
+ save(root / 'optimization.json', result)
+
+ def record(phase, **extra):
+ result.update(phase=phase, **extra)
+ save(root / 'optimization.json', result)
+ save(root / 'video.engine.json', {'phase': 'load' if phase == 'load' else 'sample', 'optimization_phase': phase})
+ print(json.dumps({'event': 'optimization', 'phase': phase, **extra}), flush=True)
+ try:
+ with attempt('baseline-load-and-warmup'):
+ record('load')
+ # Placement is a plan, not an allocator ceiling; use the same
+ # allocator behavior as a normal complete request.
+ engine = Engine(request['cache'], input_cache_dir=request.get('input_cache_dir'),
+ **dict(base, canvas=canvas))
+ result['load_seconds'] = engine.load_seconds
+ record('baseline-warmup')
+ probe(engine, request['conditioning'], request['seed'], canvas)
+ with attempt('baseline-measurement'):
+ record('baseline-measurement')
+ observed = measured_probe(engine, request, 'baseline')
+ baseline = [observed['engine']['sample_seconds']]
+ baseline_fingerprint = observed['prediction_fingerprint']
+ result['baseline_prediction_fingerprint'] = baseline_fingerprint
+ combined = {}
+ placement_trials = 0
+ for index in range(len(request['candidates'])):
+ if request.get('adaptive_candidates'):
+ from .tuning import candidate_plan
+ memory = ProcessMemory().sample(os.getpid())
+ available = inference_headroom(memory)
+ current = dict(request['profile'], engine=dict(base, **combined))
+ # Keep the complete baseline for memory admission until the
+ # arithmetic changes. A later probe cannot replace its peak or
+ # certify headroom for a combined, unmeasured configuration.
+ memory_row = request.get('complete_baseline', observed) if not combined else observed
+ planned = candidate_plan(memory_row, current, maximum=20,
+ live_gpu_free=torch.cuda.mem_get_info()[0], live_ram_available=available,
+ live_ram_sample=memory)
+ result.setdefault('candidate_plans', []).append(planned)
+ choices = [item['patch'] for item in planned['candidates']]
+ patches = [dict(combined, **choice) for choice in choices]
+ if placement_trials >= request.get('max_placement_trials', len(request['candidates'])):
+ patches = [p for p in patches if all(engine.config[k] == p.get(k, base[k]) for k in PLACEMENT_KEYS)]
+ patch = next((p for p in patches if not any(t['patch'] == p for t in result['trials'])), None)
+ if patch is None:
+ break
+ else:
+ patch = request['candidates'][index]
+ try:
+ with attempt('candidate-%d' % index, patch):
+ record('candidate-warmup', candidate=index, patch=patch)
+ if any(engine.config[k] != patch.get(k, base[k]) for k in PLACEMENT_KEYS):
+ placement_trials += 1
+ engine = configure(engine, base, patch)
+ probe(engine, request['conditioning'], request['seed'], canvas)
+ record('candidate-measurement', candidate=index, patch=patch)
+ measured = measured_probe(engine, request, 'candidate-%d' % index)
+ seconds = measured['engine']['sample_seconds']
+ equal = measured['prediction_fingerprint'] == baseline_fingerprint
+ result['trials'].append({'patch': patch, 'seconds': seconds, 'finite_prediction': True,
+ 'status': 'measured' if equal else 'numerical-change-rejected-probe',
+ 'first_prediction_equal': equal,
+ 'prediction_fingerprint': measured['prediction_fingerprint'],
+ 'gpu': measured['gpu'], 'ram': measured['ram']})
+ if equal and seconds < observed['engine']['sample_seconds']:
+ combined, observed = patch, measured
+ except (torch.cuda.OutOfMemoryError, MemoryError) as error:
+ result['trials'].append({'patch': patch, 'status': 'memory-rejected', 'error': str(error)})
+ # End this CUDA worker after exhaustion. Normal generation
+ # retries placement in fresh workers; a probe failure cannot
+ # authorize a configuration or retain a poisoned partial state.
+ result.update(success=True, status='resource-rejected', error=repr(error),
+ failure=classify_failure(error, result))
+ return
+ finally:
+ save(root / 'optimization.json', result)
+ with attempt('baseline-recheck'):
+ engine = configure(engine, base, {})
+ record('baseline-recheck')
+ baseline.append(measured_probe(engine, request, 'baseline-recheck')['engine']['sample_seconds'])
+ result['baseline_step_seconds'] = baseline
+ eligible = [row for row in result['trials'] if row.get('status') == 'measured']
+ if not eligible:
+ result.update(success=True, status='no-safe-candidates')
+ return
+ winner = min(eligible, key=lambda row: row['seconds'])
+ if winner['seconds'] >= min(baseline) * .95:
+ result.update(success=True, status='no-stable-sampling-gain')
+ return
+ with attempt('winner-recheck', winner['patch']):
+ engine = configure(engine, base, winner['patch'])
+ record('winner-recheck', patch=winner['patch'])
+ verified = probe(engine, request['conditioning'], request['seed'], canvas)
+ winner['recheck_seconds'] = verified
+ from .optimize import repeated_gain
+ if not repeated_gain(baseline, winner['seconds'], verified):
+ result.update(success=True, status='gain-did-not-repeat')
+ return
+ result['selected_patch'] = winner['patch']
+ result['measured_step_gain_fraction'] = 1 - statistics.mean([verified, winner['seconds']]) / statistics.mean(baseline)
+ with attempt('full-eight-step-validation', winner['patch'], full=True):
+ record('full-eight-step-validation', patch=winner['patch'])
+ video, audio, metrics = engine.sample(request['conditioning'], request['seed'], **{k: canvas[k] for k in ('frames', 'width', 'height')})
+ if len(metrics.get('step_seconds', [])) != 8 or engine.config.get('steps') != 8:
+ raise RuntimeError('Full validation did not complete the requested eight steps')
+ if not bool(torch.isfinite(video).all() and torch.isfinite(audio).all()):
+ raise RuntimeError('Non-finite full video/audio latents')
+ actual = {'video': video.cpu(), 'audio': audio.cpu(), 'seed': request['seed'], 'geometry': canvas}
+ reference = torch.load(request['reference_latents'], map_location='cpu', weights_only=True)
+ equality = {name: actual[name].shape == reference[name].shape and actual[name].dtype == reference[name].dtype
+ and bool(torch.equal(actual[name].contiguous().view(torch.uint8), reference[name].contiguous().view(torch.uint8)))
+ for name in ('video', 'audio')}
+ result['validation'] = {'bitwise_latents': all(equality.values()), 'equality': equality,
+ 'reference': request['reference_latents']}
+ del reference
+ artifacts = root / 'video.artifacts'
+ artifacts.mkdir(exist_ok=True)
+ torch.save(actual, artifacts / 'latents.pt')
+ config, base_path = copy.deepcopy(engine.config), engine.base
+ engine.close()
+ engine = None
+ metrics.update(config=config, load_seconds=result['load_seconds'], finite_latents=True,
+ transformer_released_before_decode=True, phase='decode')
+ save(root / 'video.engine.json', metrics)
+ metrics.update(decode_to_file(video, audio, root / 'video.mp4', base=base_path,
+ artifacts_dir=artifacts, **request['profile']['decoder']))
+ metrics.update(success=True, phase='complete', work_seconds=time.perf_counter() - started)
+ save(root / 'video.engine.json', metrics)
+ result.update(success=True, status='validated-candidate' if all(equality.values()) else 'numerical-change-rejected',
+ full_sample_seconds=metrics['sample_seconds'])
+ except BaseException as error:
+ original_error = error
+ result.update(success=False, status='failed', error=repr(error), failure=classify_failure(error, result), traceback=traceback.format_exc())
+ save(root / 'optimization.json', result)
+ raise
+ finally:
+ cleanup_error = None
+ if engine is not None and not engine.closed:
+ if result.get('failure', {}).get('kind') == 'cuda_error':
+ # End the failed CUDA context; future requests remain eligible.
+ result['cleanup_skipped'] = 'Known unsafe CUDA failure; no additional GPU cleanup calls'
+ else:
+ # The last completed probe is not the end of the GPU lifecycle.
+ # A crash/free failure here must retain a pending attempt for
+ # the actual last configuration, not the original baseline.
+ cleanup_patch = {key: engine.config[key] for key in base
+ if key in engine.config and engine.config[key] != base[key]}
+ try:
+ with attempt('cleanup', cleanup_patch):
+ engine.close()
+ except BaseException as error:
+ cleanup_error = error
+ result.setdefault('cleanup_errors', []).append(repr(error))
+ if cleanup_error is not None:
+ primary = original_error or cleanup_error
+ result.update(success=False, status='failed',
+ error=result.get('error', repr(primary)), failure=classify_failure(primary, result))
+ result['wall_seconds'] = time.perf_counter() - started
+ save(root / 'optimization.json', result)
+ if cleanup_error is not None and original_error is None:
+ raise cleanup_error
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--request', type=Path, required=True)
+ args = parser.parse_args()
+ with worker_signals(), runtime_lock():
+ run(json.loads(args.request.read_text(encoding='utf-8')))
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freevideo_engine/package_progress.py b/freevideo_engine/package_progress.py
new file mode 100644
index 0000000..d1ba062
--- /dev/null
+++ b/freevideo_engine/package_progress.py
@@ -0,0 +1,146 @@
+"""Capture uv's native transfer counters without exposing its terminal redraws.
+
+Linux uses a private pseudo-terminal for child output only. The existing process
+group, cancellation, environment, proxy and package resolver remain in control.
+Other platforms retain the ordinary log stream and phase reporting.
+"""
+import codecs
+import errno
+import json
+import os
+from pathlib import Path
+import re
+import select
+import threading
+import time
+
+from .terminal_ui import ESCAPES, clean
+
+TRANSFER = re.compile(r'([\w.+-]+)\s+[-=#>━─█░ ]+\s+([\d.]+)\s*(B|KiB|MiB|GiB)\s*/\s*([\d.]+)\s*(B|KiB|MiB|GiB)')
+UNITS = {'B': 1, 'KiB': 2**10, 'MiB': 2**20, 'GiB': 2**30}
+
+
+def transfer_counter(line):
+ match = TRANSFER.fullmatch(clean(line))
+ if not match:
+ return None
+ name, done, done_unit, total, total_unit = match.groups()
+ try:
+ done, total = round(float(done) * UNITS[done_unit]), round(float(total) * UNITS[total_unit])
+ except (ValueError, OverflowError):
+ return None
+ if total <= 0 or done < 0:
+ return None
+ return name, min(done, total), total
+
+
+def is_uv_install(command):
+ return (len(command) > 1 and Path(str(command[0])).name in ('uv', 'uv.exe') and
+ (list(map(str, command[1:3])) in (['pip', 'install'], ['python', 'install'])
+ or str(command[1]) == 'venv'))
+
+
+class PackageOutput:
+ def __init__(self, command, log, env, *, enabled=None):
+ self.log, self.env, self.stdout = log, env, log
+ self.enabled = os.name == 'posix' and (is_uv_install(command) if enabled is None else enabled)
+ self.master = self.slave = None
+ self.thread = None
+ self.error = None
+ self.previous = {}
+ self.stop = threading.Event()
+
+ def __enter__(self):
+ if self.enabled:
+ import fcntl
+ import pty
+ import struct
+ import termios
+ try:
+ self.master, self.slave = pty.openpty()
+ # Enough rows for concurrent uv downloads; no user terminal is
+ # resized or attached to the child's input.
+ fcntl.ioctl(self.slave, termios.TIOCSWINSZ, struct.pack('HHHH', 128, 140, 0, 0))
+ except OSError:
+ self.close_fds()
+ else:
+ self.stdout = self.slave
+ self.env = dict(self.env, TERM='xterm-256color', UV_NO_PROGRESS='false')
+ self.env.pop('JPY_SESSION_NAME', None) # uv's notebook mode suppresses byte counters
+ return self
+
+ def spawned(self):
+ if self.master is not None:
+ os.close(self.slave)
+ self.slave = None
+ self.thread = threading.Thread(target=self.read, name='package-progress', daemon=True)
+ self.thread.start()
+
+ def emit(self, line):
+ text = ''.join(c for c in ESCAPES.sub('', line) if c.isprintable() or c == '\t').rstrip()
+ if not text.strip():
+ return
+ counter = transfer_counter(text)
+ if counter:
+ name, done, total = counter
+ now = time.monotonic()
+ prior = self.previous.get(name)
+ if prior and now - prior[0] < .5:
+ return
+ rate = ((done - prior[1]) / (now - prior[0])
+ if prior and total == prior[2] and done >= prior[1] else 0.)
+ self.previous[name] = (now, done, total)
+ text = json.dumps({'event': 'download_progress', 'description': name,
+ 'done_bytes': done, 'total_bytes': total,
+ 'bytes_per_second': rate, 'counter_source': 'uv-terminal',
+ 'scope': 'Current file; uv byte counters are rounded for display'})
+ self.log.write(text + '\n')
+ self.log.flush()
+
+ def read(self):
+ decoder = codecs.getincrementaldecoder('utf-8')(errors='replace')
+ pending = ''
+ try:
+ while not self.stop.is_set():
+ if not select.select([self.master], [], [], .2)[0]:
+ continue
+ try:
+ chunk = os.read(self.master, 65536)
+ except OSError as error:
+ if error.errno == errno.EIO: # PTY writer exited
+ break
+ raise
+ if not chunk:
+ break
+ rows = (pending + decoder.decode(chunk)).replace('\r', '\n').split('\n')
+ pending = rows.pop()
+ for row in rows:
+ self.emit(row)
+ # Guard against an invalid child that never writes line breaks.
+ if len(pending) > 65536:
+ self.log.write(clean(pending) + '\n')
+ self.log.flush()
+ pending = ''
+ self.emit(pending + decoder.decode(b'', final=True))
+ except BaseException as error:
+ self.error = error
+
+ def close_fds(self):
+ for name in ('slave', 'master'):
+ fd = getattr(self, name)
+ if fd is not None:
+ os.close(fd)
+ setattr(self, name, None)
+
+ def __exit__(self, exc_type, exc, tb):
+ if self.slave is not None:
+ os.close(self.slave)
+ self.slave = None
+ if self.thread:
+ self.thread.join(timeout=2)
+ if self.thread.is_alive():
+ self.stop.set()
+ self.thread.join(timeout=1)
+ self.close_fds()
+ if self.error is not None and exc_type is None:
+ raise RuntimeError('Could not retain package installation output') from self.error
diff --git a/freevideo_engine/packing.py b/freevideo_engine/packing.py
new file mode 100644
index 0000000..3a23ad5
--- /dev/null
+++ b/freevideo_engine/packing.py
@@ -0,0 +1,82 @@
+# Copyright 2026 The MiniMax and HuggingFace Teams. All rights reserved.
+# Copyright 2026 FreeVideo contributors.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy at http://www.apache.org/licenses/LICENSE-2.0
+# Unless required by applicable law or agreed to in writing, software distributed
+# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+# CONDITIONS OF ANY KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations under the License.
+"""Bound packed-input and output-head intermediates without dropping any rows.
+
+Adapted from the pinned Diffusers MiniMaxH3Transformer3DModel.forward. Scattering
+is in-place under inference, modality projections are released immediately, and
+the FP32 projections/output normalization run over bounded row chunks. GEMM
+shape changes can alter rounding; this is an explicit optimization profile.
+"""
+import types
+import torch
+from .fp8 import input_dtype
+
+from diffusers.models.transformers.transformer_minimax_h3 import MiniMaxH3TransformerOutput
+
+
+def install_streamed_forward(model, chunk=1024, residual_offload=False):
+ if chunk < 1:
+ raise ValueError('Projection chunk must be positive')
+
+ def forward(self, hidden_states, audio_hidden_states, encoder_hidden_states,
+ timestep, timestep_indices, token_tags, position_ids,
+ video_indices, audio_indices, text_indices, attention_kwargs=None, return_dict=True):
+ if torch.is_grad_enabled():
+ raise RuntimeError('Streamed packing requires inference without autograd')
+ if attention_kwargs:
+ raise ValueError('This profile expects already merged adapters without attention kwargs')
+ length = position_ids.shape[0]
+ if position_ids.shape != (length, 3) or token_tags.shape != (length,) or timestep_indices.shape != (length,):
+ raise ValueError('Invalid packed sequence geometry')
+ rotary = self.rope(position_ids)
+ text = getattr(self, '_freevideo_refined_text', None)
+ if text is None:
+ text = self.context_embedder(encoder_hidden_states.to(input_dtype(self.context_embedder)))
+ text = self.token_refiner(text)
+ packed = text.new_zeros((text.shape[0], length, text.shape[-1]))
+ packed.index_copy_(1, text_indices, text)
+ del text
+ for source, indices, projection in ((hidden_states, video_indices, self.proj_in),
+ (audio_hidden_states, audio_indices, self.audio_proj_in)):
+ for start in range(0, source.shape[1], chunk):
+ projected = projection(source[:, start:start + chunk].to(projection.weight.dtype))
+ packed.index_copy_(1, indices[start:start + chunk], projected.to(packed.dtype))
+ del projected
+ del source
+ temb = self.time_embedder(self.time_proj(timestep).to(self.time_embedder.linear_1.weight.dtype))
+ adaln_indices = timestep_indices * 3 + token_tags
+ if residual_offload:
+ from .residual import ResidualState
+ state = ResidualState(packed)
+ del packed
+ try:
+ for block in self.transformer_blocks:
+ state = block(state, temb, adaln_indices, rotary)
+ packed = state.take()
+ self._freevideo_residual_stats = state.stats()
+ finally:
+ state.close()
+ else:
+ for block in self.transformer_blocks:
+ packed = block(packed, temb, adaln_indices, rotary)
+ video_all = torch.empty((packed.shape[0], length, self.proj_out.out_features),
+ dtype=self.proj_out.weight.dtype, device=packed.device)
+ audio_all = torch.empty((packed.shape[0], length, self.audio_proj_out.out_features),
+ dtype=self.audio_proj_out.weight.dtype, device=packed.device)
+ for start in range(0, length, chunk):
+ normalized = self.norm_out(packed[:, start:start + chunk], temb, timestep_indices[start:start + chunk])
+ normalized = normalized.to(self.proj_out.weight.dtype)
+ video_all[:, start:start + chunk] = self.proj_out(normalized)
+ audio_all[:, start:start + chunk] = self.audio_proj_out(normalized)
+ video = video_all.index_select(1, video_indices)
+ audio = audio_all.index_select(1, audio_indices)
+ return MiniMaxH3TransformerOutput(sample=video, audio_sample=audio) if return_dict else (video, audio)
+
+ model.forward = types.MethodType(forward, model)
diff --git a/freevideo_engine/pass_configuration.py b/freevideo_engine/pass_configuration.py
new file mode 100644
index 0000000..913e536
--- /dev/null
+++ b/freevideo_engine/pass_configuration.py
@@ -0,0 +1,74 @@
+"""Change pass-local compute tiling while preserving loaded model weights."""
+from contextlib import contextmanager
+
+
+COMPUTE_KEYS = ('head_chunk', 'window_batch', 'ff_chunk', 'projection_chunk',
+ 'attention_cpu_outputs', 'grouped_attention_outputs',
+ 'fp8_ff_recompute', 'residual_offload', 'prefetch')
+
+
+@contextmanager
+def configuration(engine, selected):
+ if not selected:
+ yield
+ return
+ previous = engine.config
+ values = {key: selected.get(key, previous.get(key)) for key in COMPUTE_KEYS}
+ changed = {key for key, value in values.items() if value != previous.get(key)}
+ if not changed:
+ yield
+ return
+ model, attention = engine.transformer, engine.attention
+ if not previous.get('inference_kernels') or not previous.get('head_chunk'):
+ raise ValueError('Pass configuration requires the bounded inference path')
+ from src.models.hybrid_transform import iter_hybrids
+ methods = [(module, name, getattr(module, name)) for module, name in
+ [(model, 'forward')] + [(block, 'forward') for block in model.transformer_blocks]
+ + [(block.ff, 'forward') for block in model.transformer_blocks]
+ + [(module, '_hybrid_forward') for module in iter_hybrids(model)]]
+ old_prefetch, old_window = engine.prefetch, attention.window_batch
+ try:
+ if changed.intersection(('head_chunk', 'attention_cpu_outputs', 'grouped_attention_outputs', 'projection_chunk')):
+ from .head_chunk import install_head_chunks
+ install_head_chunks(model, attention, values['head_chunk'],
+ cpu_outputs=values['attention_cpu_outputs'], projection_chunk=values['projection_chunk'],
+ grouped_outputs=values['grouped_attention_outputs'], parallelism=previous.get('head_parallelism', 1))
+ if changed.intersection(('ff_chunk', 'fp8_ff_recompute')):
+ if previous.get('precision') == 'fp8' and previous.get('linear_compute') == 'native-fp8':
+ from .fp8_ops import install_chunked_ff
+ for block in model.transformer_blocks:
+ install_chunked_ff(block.ff, values['ff_chunk'], recompute=values['fp8_ff_recompute'])
+ elif 'ff_chunk' in changed:
+ # Ampere keeps its existing weight-only arithmetic. Replace
+ # the outer row tiling without nesting the old tiling wrapper.
+ import types
+ import torch
+ chunk = values['ff_chunk']
+ for block in model.transformer_blocks:
+ original = block.ff._freevideo_unchunked_forward
+ def forward(self, hidden, *args, original=original, **kwargs):
+ output = torch.empty_like(hidden)
+ for start in range(0, hidden.shape[-2], chunk):
+ output[..., start:start + chunk, :] = original(hidden[..., start:start + chunk, :], *args, **kwargs)
+ return output
+ block.ff.forward = types.MethodType(forward, block.ff)
+ if changed.intersection(('projection_chunk', 'residual_offload')):
+ from .packing import install_streamed_forward
+ from .blocks import install_bounded_blocks
+ install_streamed_forward(model, values['projection_chunk'], residual_offload=values['residual_offload'])
+ install_bounded_blocks(model, residual_offload=values['residual_offload'])
+ engine.config = dict(previous, **values)
+ engine.prefetch, attention.window_batch = values['prefetch'], values['window_batch']
+ attention.current_plan = attention.offsets = attention.batches = None
+ yield
+ finally:
+ for module, name, method in methods:
+ setattr(module, name, method)
+ engine.config = previous
+ engine.prefetch, attention.window_batch = old_prefetch, old_window
+ attention.current_plan = attention.offsets = attention.batches = None
+ # Canvas-specific host buffers must not survive alongside the next
+ # pass's differently shaped buffers. Immutable weights remain untouched.
+ for name in ('host_outputs', 'host_output_key', 'group_readouts'):
+ if hasattr(attention, name):
+ delattr(attention, name)
diff --git a/freevideo_engine/paths.py b/freevideo_engine/paths.py
new file mode 100644
index 0000000..8db2e48
--- /dev/null
+++ b/freevideo_engine/paths.py
@@ -0,0 +1,49 @@
+"""User paths; importing the CLI or planner never initializes CUDA."""
+import os
+from pathlib import Path
+import sys
+from .system import install_root
+
+
+def data_root():
+ return install_root()
+
+
+def vdn_root():
+ return Path(os.environ.get('FREEVIDEO_VDN_ROOT', data_root() / 'vendor' / 'vdn')).expanduser()
+
+
+def model_root():
+ return Path(os.environ.get('FREEVIDEO_MODEL_ROOT', data_root() / 'models')).expanduser()
+
+
+def add_vdn():
+ root = vdn_root().resolve()
+ if not (root / 'src/models/hybrid_attention.py').is_file():
+ raise RuntimeError('VDN dependency is missing. Run freevideo setup or set FREEVIDEO_VDN_ROOT.')
+ if str(root) not in sys.path:
+ sys.path.insert(0, str(root))
+ # Editable-install .pth files retain the original absolute checkout path.
+ # Resolve the pinned sibling source explicitly when a workspace is mounted
+ # or moved under a different prefix (for example a capacity container).
+ diffusers = root / 'diffusers' / 'src'
+ if not (diffusers / 'diffusers/__init__.py').is_file():
+ raise RuntimeError('Patched Diffusers dependency is missing. Run freevideo setup --install-packages.')
+ if str(diffusers) not in sys.path:
+ sys.path.insert(0, str(diffusers))
+ from .fa4_guard import activate
+ activate()
+ return root
+
+
+def base_path():
+ return model_root() / 'h3-base'
+
+
+def checkpoint_path():
+ return model_root() / 'stage-dmd-step-250'
+
+
+def comfy_root():
+ value = os.environ.get('FREEVIDEO_COMFY_ROOT')
+ return Path(value).expanduser() if value else data_root() / 'vendor' / 'ComfyUI'
diff --git a/freevideo_engine/policy.py b/freevideo_engine/policy.py
new file mode 100644
index 0000000..33204d4
--- /dev/null
+++ b/freevideo_engine/policy.py
@@ -0,0 +1,1032 @@
+"""Geometry-aware resource placement, with local history and measured tuning.
+
+Capacity fields use bytes, CLI sizes use GiB. Predictions are not hard limits or
+evidence of measured speed on an untested GPU. A process OOM remains a failure.
+"""
+from __future__ import annotations
+from dataclasses import asdict, dataclass
+import math
+from .hardware import Hardware, GiB
+from .system import HOST_WEIGHT_HEADROOM, weight_cache_headroom, residual_host_headroom
+
+# One prepared FP8 transformer block, as reserved on the host allocator.
+BLOCK_BYTES = 432_500_000
+
+# GPU bytes a request needs beyond resident block weights under the sub-10 GiB
+# chunking profile: activations, the block-output cache, the transfer slot, root
+# weights and allocator slack. From one complete 1344x768, 243-frame, 8-step run
+# on a 12 GiB RTX 4080 Laptop at resident_blocks 9, where reserved VRAM peaked at
+# 11.06 GiB; subtracting the nine resident blocks leaves 7.43 GiB. head_chunk 4
+# then measured 0.73 GiB more than head_chunk 2 over three warm steps, and this
+# keeps a small margin above the sum. window_batch stays at 1 in this band, so
+# nothing is added for batched windows.
+#
+# Use the whole-run peak, not a slope. Reserved VRAM climbs 0.74 GiB per step
+# for the first steps and then flattens, so extrapolating three probe steps
+# predicted 11.82 GiB against the 11.06 GiB actually measured over eight.
+# Compare against reserved rather than allocated: reserved is what occupies the
+# device, and expandable segments are unavailable on Windows.
+#
+# Earlier interrupted runs reported a 5.15 GiB peak and are not evidence: the
+# RAM guard stopped them inside the first sampling step, before the peak.
+# Measured on Ada only; larger profiles keep their own estimates.
+#
+# Reference arithmetic for small-memory requests. Larger head groups are
+# optimizer candidates; apply them after complete local output comparison.
+# Head group and its activation allowance for the band under 10 GiB. Four
+# heads cost exactly what two do. Measured on an H200 at three corners of the
+# band with the card as the ceiling, changing nothing but the group:
+#
+# 8 GiB, 243 frames 45.41 -> 37.22 s/step peak 7.34 GiB both
+# 9 GiB, 345 frames 72.11 -> 58.26 s/step peak 8.34 GiB both
+# 10 GiB, 345 frames 56.11 -> 47.31 s/step peak 9.39 GiB both
+#
+# Those corners span residual offload on and off, the host attention buffer on
+# and off, and 0 to 2 resident blocks, and the peak did not move in any of
+# them. The arithmetic bound agrees: a slice is tokens x heads x 128 x 2
+# bytes, so doubling the group adds 151 MiB of QKV at 345 frames with all
+# three tensors live, against 0.66 GiB of measured spare. That bound is what
+# carries this to architectures with no measurement here -- every corner above
+# is SM90. This needs no arithmetic of its own -- unlike eight heads, which
+# cost 0.00, 0.41 and 0.84 GiB at the same three corners and overran a 10 GiB
+# card at 345 frames. That interacts with where the attention outputs live and
+# would need its own accounting and more corners than three.
+# Eight heads in this band, where the budget covers them. Measured on an
+# H200 at 1344x768x243 with the card as the ceiling, changing only the group:
+#
+# 9 GiB card, budget 8.50 13.03 -> 10.43 s/step peak 8.34 -> 8.43
+# 10 GiB card, budget 9.50 13.00 -> 10.37 s/step peak 9.33 -> 9.33
+#
+# So 20% for 0.09 GiB at one corner and nothing at the other. Two corners
+# refuse it, and 8.5 GiB is the reserve that reproduces all four: an 8 GiB
+# card's 7.50 budget does not reach it, and the group peaked 8.02 there,
+# over the card by 0.02 with every window and both chunk sizes. A 10 GiB
+# card at 345 frames needs 10.42 after the token scaling below against a
+# 9.50 budget, and the group peaked 10.30 there, over by 0.30.
+#
+# Where the outputs live interacts with this: a 9 GiB card at 345 frames
+# stages residuals and outputs on the host, and eight heads did fit there
+# at a 8.34 GiB peak. This reserve declines that corner, which costs the
+# 15% it measured and keeps one rule for the band. The token axis is under
+# measurement and is what would separate them.
+#
+# The entry is 8.2, not 8.5, because both peaks above were measured with the
+# attention outputs on the GPU and GPU_ATTENTION_OUTPUT_BYTES adds that 0.3
+# back after selection. Carrying 8.5 here charged it twice, which pushed a
+# 9 GiB card's outputs to the host -- and eight heads with host outputs
+# measured 102.88 s/step against 13.03, an eightfold loss. A reserve must
+# not be able to buy the wider group by giving up the outputs.
+SMALL_ACTIVATION_RESERVES = ((8, int(8.2 * GiB)), (4, int(7.6 * GiB)))
+# The 8 GiB Windows path need not keep four heads at every token count.
+# PRO 6000 full two-pass runs at 9720 first-pass rows measured head 4/8 at
+# 11.78/9.81 s per warm step and 3.955/3.957 GiB reserved. At 18144 rows,
+# the pair measured 7.72/6.13 s with both reserving 4.328 GiB. Native Windows
+# reports 202/203 cover head-4 baselines through 18144 rows. Allow eight only
+# in that small-canvas interval through the measured 243 frames, with its
+# existing larger workspace allowance and GPU outputs fully funded. References
+# count toward the same interval; equal token counts at longer durations do
+# not prove that the linear-attention scan workspace remains the same.
+# Larger groups can change floating-point rounding; bit identity is not a
+# requirement for a measured chunking policy. Sixteen was no faster here.
+WINDOWS_SMALL_HEAD8_TOKENS = 18144
+WINDOWS_SMALL_HEAD8_FRAMES = 243
+# Keeping the attention outputs on the GPU instead of in host memory costs this
+# much extra peak. See benchmarks/2026-09-15-consumer-capacity-grid.json. Measured at four corners of the band that uses the host
+# buffer: 0.15 GiB at 8 GiB/1344x768x243, 0.23 at 8 GiB/1024x1024x243, 0.22 at
+# 9 GiB/1344x768x345 and 0.04 at 10 GiB/1344x768x345. Rounded up past the worst
+# of those so residency, not the reserve's slack, pays for it.
+GPU_ATTENTION_OUTPUT_BYTES = int(.3 * GiB)
+# Whole-device peak of running the bottom of the band without residual storage:
+# head 4, no resident weights, attention outputs on the GPU. Measured over
+# complete eight-step requests on an 8 GiB card at 1344x768x243: 7.68 GiB at
+# 32 GiB of RAM and 7.70 at 16 GiB with one window, and 7.69 with four on the
+# same card. Rounded up past all three, because what it replaces -- moving the
+# residual stream to the host -- measured 2.6 times slower.
+SMALL_NO_WEIGHTS_PEAK = int(7.8 * GiB)
+# The same path one card size down, with the attention outputs in host memory
+# because the budget cannot also cover them on the card. Whole-device peak over
+# complete eight-step requests on a 7 GiB card at 1344x768x243: 6.33 GiB at 8,
+# 12, 16 and 32 GiB of RAM. The plan streams weights at the first three of
+# those and holds them at the last, and the peak did not move, so this is the
+# activation path rather than an allocator accident. Rounded up past it by the
+# 0.30 GiB of margin the figure above keeps, which also keeps a 6 GiB card out:
+# that card fits the same path at 5.90 of its 6.00, and 0.10 GiB is half the
+# thinnest margin any Windows request in the field has survived.
+SMALL_HOST_OUTPUT_PEAK = int(6.7 * GiB)
+SMALL_ACTIVATION_RESERVE = SMALL_ACTIVATION_RESERVES[-1][1]
+
+# Residency ceiling. Blocks held on the GPU do not buy time, and past a
+# point they cost it. Measured at the same cap and geometry, changing only
+# how many blocks are held:
+#
+# 24 GiB cap 29 blocks 9.24 s/step peak 22.11 GiB
+# 8 blocks 9.20 s/step peak 16.61 GiB -0.4%, frees 5.50
+# 32 GiB cap 48 blocks 9.28 s/step peak 29.75 GiB
+# 8 blocks 9.15 s/step peak 16.61 GiB -1.3%, frees 13.14
+#
+# Holding all 50 and dropping prefetch to afford them was worse still: 9.206 s
+# at 31.95 GiB against 9.047 s at 30.22 GiB for 44 plus prefetch. Streaming
+# the remainder moved 19.3 GiB in 0.44 s of a 72 s request over a link
+# measured at 47.7 to 54.1 GB/s, so the transfers are not the cost; the
+# resident copies are. Hold the smallest number that was measured, and spend
+# nothing further on memory that buys no time.
+# See benchmarks/2026-09-15-consumer-capacity-grid.json.
+# Blocks the GPU holds when host RAM does not need it to hold more. Zero is
+# not measured, so keep the smallest count that is.
+RESIDENT_TARGET = 8
+
+# Above this the resident copies displace nothing useful, so it bounds the
+# VRAM-side arithmetic before host RAM narrows it further.
+RESIDENT_VRAM_CEILING = 44
+
+# A streamed transformer uses one device transfer slot without prefetch. The
+# read-ahead slot adds one prepared block (BLOCK_BYTES) and lets host staging
+# overlap the previous block's compute. The old 14 GiB gate left the lower
+# part of the 16 GiB Windows tier (for example a 13.7 GiB live budget) on the
+# synchronous path even though that tier still has enough allocator headroom
+# for the second slot. Keep the 12 GiB tier unchanged; its measured budget
+# is 11.5 GiB and remains below this threshold.
+PREFETCH_GPU_BUDGET = int(13.5 * GiB)
+
+
+def prefetch_for_budget(gpu_budget, *, ram_budget=None, twelve=False, ampere=False,
+ system='Linux', architecture=None):
+ """Whether a second weight transfer slot fits the selected GPU budget.
+
+ Native Windows Blackwell also benefits at 13.5--14 GiB with partial host
+ retention. Residency below includes the second device slot in its complete
+ token-scaled workspace; this never spends an unaccounted extra block.
+ """
+ # This extra lower-16-GiB rule was measured on Windows WDDM. Linux has
+ # different page-cache and allocator behaviour, so do not silently apply
+ # a Windows-only transfer overlap threshold there. The established
+ # high-capacity and Ampere paths below remain cross-platform.
+ low_host = (system == 'Windows' and ram_budget is not None
+ and ram_budget < 8 * GiB)
+ # Preserve the established high-capacity path. The new lower-band rule
+ # is an additional case for the 16 GiB tier; it must not turn prefetch off
+ # for the already-validated 20/24/32 GiB profiles.
+ return (gpu_budget >= 14 * GiB
+ or (gpu_budget >= PREFETCH_GPU_BUDGET and low_host)
+ or (gpu_budget >= PREFETCH_GPU_BUDGET and system == 'Windows'
+ and architecture == 'blackwell-rtx')
+ or (twelve and ampere))
+
+# Whole-device bytes a request needs beside the resident blocks, by head
+# group and token count, with the attention outputs on the GPU. A head-group
+# slice is tokens x heads x 128 x 2 bytes, so this scales with the request
+# and cannot be one number per group: the same 10 GiB card takes sixteen
+# heads at 41472 tokens and not even eight at 102816.
+#
+# Read off complete eight-step runs as peak minus the blocks the run held,
+# from the cases where residency had been reduced until the plan just fit --
+# those are the ones at their true minimum. Four independent cards agreed on
+# head 16 at 72576 tokens to a hundredth of a GiB:
+#
+# 11 GiB card, 2 blocks peak 10.86 -> 10.05
+# 12 GiB card, 4 blocks peak 11.66 -> 10.05
+# 13 GiB card, 7 blocks peak 12.86 -> 10.04
+# 13 GiB card, 6 blocks peak 12.46 -> 10.05
+#
+# Cards with slack report more, because the allocator keeps a larger pool;
+# those readings describe the allocator, not the requirement.
+#
+# Selecting the widest group whose requirement fits the budget reproduces
+# every measured boundary: sixteen heads overran a 10 GiB card at 72576
+# tokens by 1.09 GiB and fit an 11 GiB card by 0.14; eight overran an 8 GiB
+# card by 0.02 and fit a 9 GiB card; at 102816 tokens eight overran 10 GiB
+# by 0.44 and fit 12, and sixteen overran 12 by 0.87 and fit 16.
+# See benchmarks/2026-09-15-consumer-capacity-grid.json.
+ACTIVATION_BY_TOKENS = {
+ 4: ((41472, 6.11), (72576, 7.69), (102816, 9.97)),
+ 8: ((41472, 6.11), (72576, 8.03), (102816, 10.53)),
+ 16: ((41472, 6.75), (72576, 10.05), (102816, 14.21)),
+}
+
+
+def activation_bytes(head, tokens):
+ """Measured GPU bytes a head group needs beside the resident blocks.
+
+ Piecewise linear through the measured token counts, extrapolated with the
+ nearest measured slope.
+ """
+ tokens = 72576 if tokens is None else tokens
+
+ def interpolate(group):
+ anchors = ACTIVATION_BY_TOKENS[group]
+ for (low, at_low), (high, at_high) in zip(anchors, anchors[1:]):
+ if tokens <= high or (high, at_high) == anchors[-1]:
+ slope = (at_high - at_low) / (high - low)
+ return int((at_low + slope * (tokens - low)) * GiB)
+ raise AssertionError('unreachable: anchors are non-empty and ordered')
+
+ # A wider group cannot need less. Below the measured range the two slopes
+ # crossed, which is the extrapolation talking, not the requirement.
+ return max(interpolate(group) for group in ACTIVATION_BY_TOKENS if group <= head)
+
+# Host memory the text encoder holds while it runs, which happens before the
+# transformer is placed at all and was not modelled here. Measured on an H200
+# at three GPU budgets, after its weights reach the device and its consumed
+# checkpoint pages are released:
+#
+# GPU budget 6.45 GiB 2.33 GiB resident = 1.94 anonymous + 0.39 mapped
+# GPU budget 9.50 GiB 3.46 GiB = 3.07 + 0.39
+# GPU budget 120 GiB 2.91 GiB = 2.52 + 0.39
+#
+# Before that release the same measurements were 7.82, 6.10 and 9.44 GiB,
+# almost all of it clean page cache the transfer walked through. A guard that
+# credits clean pages sees past it; a hard cgroup or commit limit does not
+# wait for reclaim, and a 10 GiB limit killed the encoder at 9.84 GiB with no
+# disk read recorded, before a single sampling step. Rounded up past the
+# largest of the three.
+#
+# The encoder and the transformer never run at once -- the encode child exits
+# before sampling -- so this is a floor on admission, not an addition to the
+# request's budget. Cached or preencoded conditioning skips the encoder
+# entirely and this does not apply.
+ENCODER_HOST_BYTES = int(3.6 * GiB)
+
+# Residual placement keeps the same head/FF/projection kernels. Full-capacity
+# qualification and its platform scope are recorded in docs/POLICY.md. This
+# lower estimate applies only when the original small path cannot fit; spare
+# memory on larger cards must not trigger additional residual transfers.
+RESIDUAL_ACTIVATION_RESERVE = int(6.4 * GiB)
+
+# What that path actually needed on the card that was refused for wanting 6.4.
+# An RTX PRO 6000 request was refused at a 6.267 GiB budget, 0.133 short. Run
+# there with the plan this branch builds -- staging on, attention outputs in
+# host memory, no resident blocks -- it completed all eight steps at 36.86
+# s/NFE with a 5.246 GiB sampling Torch reserved peak and a 6.712 GiB
+# whole-device NVML peak, of which 0.533 is the MPS server's own allocation:
+# 6.179 GiB of client memory against a 6.267 budget. So the refusal was a
+# false negative by about 0.2 GiB, and admission is separated from the reserve
+# the plan is then built with, which stays at 6.4 so no already-admitted card
+# changes placement.
+#
+# Linux only, like every other measurement in this band. The same run has not
+# been done on native Windows, where exceeding the card is a silent WDDM spill
+# rather than an error, and 0.09 GiB of measured margin is not enough to
+# assume it. benchmarks/pro6000-verification/results-pro6000.md.
+RESIDUAL_ADMISSION_FLOOR = int(6.25 * GiB)
+MIN_GPU_RESERVE_GIB = .2
+
+
+def windows_gpu_output_workspace(video_tokens, *, prefetch=False):
+ """Starting workspace for head-8 GPU outputs with the full per-tensor FF stash.
+
+ Two complete Windows 5060 Ti requests constrain this estimate: 896-square
+ / 243 frames used 11.14 GiB reserved with zero resident blocks; 1344x768
+ / 243 used 13.75 GiB with eight blocks and FF recomputation. Subtracting
+ those weights and adding the avoided 1.96 GiB stash gives 12.49 GiB.
+ The 6.5 + 6 * token-ratio envelope covers both anchors. It is a cold-start
+ estimate, not a fitted throughput model or proof of capacity elsewhere.
+ The OS growth reserve has already been subtracted from the budget.
+ """
+ tokens = 72576 if video_tokens is None else video_tokens
+ return int((6.5 + 6. * tokens / 72576) * GiB) + (BLOCK_BYTES if prefetch else 0)
+
+
+class ResourceBudgetError(ValueError):
+ """A rejected plan with the exact capacities, reserves and shortfalls."""
+ def __init__(self, hardware, gpu_total, ram_total, free_gpu, free_ram, reserve_gpu, reserve_ram,
+ *, gpu_minimum_bytes=5*GiB, gpu_requirement=None, canvas=None,
+ ram_minimum_bytes=4*GiB, ram_requirement=None):
+ self.details = dict(gpu=hardware.gpu_name, system=hardware.system,
+ gpu_total_bytes=hardware.vram_total, gpu_capacity_bytes=gpu_total,
+ gpu_available_bytes=free_gpu, gpu_reserve_bytes=int(reserve_gpu * GiB),
+ gpu_budget_bytes=free_gpu-int(reserve_gpu * GiB), gpu_minimum_bytes=gpu_minimum_bytes,
+ ram_total_bytes=hardware.ram_total, ram_capacity_bytes=ram_total,
+ ram_available_bytes=free_ram, ram_reserve_bytes=int(reserve_ram * GiB),
+ ram_budget_bytes=free_ram-int(reserve_ram * GiB), ram_minimum_bytes=ram_minimum_bytes,
+ cgroup_ram_limit_bytes=hardware.cgroup_ram_limit)
+ failed = [name for name in ('gpu', 'ram')
+ if self.details[name+'_budget_bytes'] < self.details[name+'_minimum_bytes']]
+ self.details['insufficient'] = failed
+ for name in ('gpu', 'ram'):
+ self.details[name+'_shortfall_bytes'] = max(
+ 0, self.details[name+'_minimum_bytes'] - self.details[name+'_budget_bytes'])
+ if gpu_requirement is not None:
+ self.details['gpu_requirement'] = gpu_requirement
+ if ram_requirement is not None:
+ self.details['ram_requirement'] = ram_requirement
+ # The geometry belongs to the refusal, not to whichever of the two
+ # budgets happened to fail: a VRAM refusal is the one a user most
+ # needs it for, because the way out is a smaller request.
+ self.details['geometry'] = dict(canvas) if canvas is not None else None
+ lines = ['FreeVideo resource planning could not admit this request: ' + ', '.join(name.upper() for name in failed),
+ 'GPU: %s (%s)' % (hardware.gpu_name, hardware.system)]
+ if ram_requirement == 'text_encoder_host':
+ lines.append('The text encoder holds %.2f GiB of host memory while it runs, and this '
+ 'machine offers %.2f GiB. It runs before sampling, so the request would '
+ 'stop there. Reuse cached text conditioning, or supply it preencoded, '
+ 'and the encoder is not loaded at all.'
+ % (self.details['ram_minimum_bytes'] / GiB,
+ self.details['ram_budget_bytes'] / GiB))
+ if gpu_requirement == 'estimated_working_set':
+ lines.append('The smallest-profile VRAM estimate includes activations, fixed weights and '
+ 'transfer/work buffers, with CPU residual staging and no resident transformer blocks. '
+ 'This is a planning estimate, not a measured OOM.')
+ if canvas is not None:
+ lines.append('Request: %d x %d, %d frames (%.3f s); %d video tokens, %d reference tokens.' %
+ (canvas['width'], canvas['height'], canvas['frames'], canvas['frames']/24,
+ canvas['video_tokens'], canvas.get('reference_video_tokens', 0) +
+ canvas.get('reference_audio_tokens', 0)))
+ for name, label in (('gpu', 'VRAM'), ('ram', 'RAM')):
+ lines.append('%s: %.2f GiB available, %.2f GiB reserved for system growth, '
+ '%.2f GiB usable; minimum %.2f GiB. Capacity %.2f / %.2f GiB.' %
+ (label, self.details[name+'_available_bytes']/GiB,
+ self.details[name+'_reserve_bytes']/GiB, self.details[name+'_budget_bytes']/GiB,
+ self.details[name+'_minimum_bytes']/GiB, self.details[name+'_capacity_bytes']/GiB,
+ self.details[name+'_total_bytes']/GiB))
+ if self.details[name+'_shortfall_bytes']:
+ lines.append('%s shortfall: %.2f GiB.' % (label, self.details[name+'_shortfall_bytes']/GiB))
+ lines.append('Available memory includes validated idle-cache credit, if present. Resolution, frames and steps were not reduced.')
+ super().__init__('\n'.join(lines))
+
+
+
+def memory_fraction(budget_bytes, total_bytes):
+ """Share of the device a budget represents, or None if it covers all of it.
+
+ Pure arithmetic, so it lives with the budgets rather than in the worker:
+ the CPU test job has no torch, and importing the worker there pulled it in.
+ Pure planning arithmetic. The Windows worker applies a separate live
+ allocator ceiling; Linux only enforces explicit benchmark ceilings.
+ """
+ if not budget_bytes or not total_bytes or budget_bytes <= 0:
+ raise ValueError('A positive GPU budget and device capacity are required')
+ if budget_bytes >= total_bytes:
+ return None
+ return budget_bytes / total_bytes
+
+
+@dataclass
+class Policy:
+ schema_version: int
+ hardware: dict
+ gpu_budget_bytes: int
+ ram_budget_bytes: int
+ gpu_system_reserve_bytes: int
+ ram_system_reserve_bytes: int
+ allocator_config: str
+ engine: dict
+ decoder: dict
+ evidence: str
+ notes: list[str]
+ capacity_trial: bool = False
+
+ def to_dict(self):
+ return asdict(self)
+
+ def legacy_profile(self):
+ return dict(name='Automatic VDN Engine policy', gpu_budget_gb=self.gpu_budget_bytes / 1e9,
+ inference_ram_budget_gb=self.ram_budget_bytes / 1e9,
+ allocator_config=self.allocator_config, engine=self.engine, decoder=self.decoder,
+ policy=self.to_dict())
+
+
+def choose(hardware: Hardware, *, vram_gib=None, ram_gib=None, attention='auto',
+ gpu_reserve_gib=None, ram_reserve_gib=None, available_backends=None, canvas=None,
+ demonstrated_ram_bytes=None, allow_capacity_trial=False, stage='generation'):
+ from .geometry import geometry
+ if stage not in ('encoding', 'generation'):
+ raise ValueError('Resource planning stage must be encoding or generation')
+ if canvas is not None:
+ checked = geometry(canvas['width'], canvas['height'], frames=canvas['frames'])
+ if any(canvas.get(k) != checked[k] for k in ('width', 'height', 'frames', 'video_tokens')):
+ raise ValueError('Resource policy requires aligned, consistent request geometry')
+ reference_rows = 0
+ if canvas is not None:
+ for name in ('reference_video_tokens', 'reference_audio_tokens'):
+ count = canvas.get(name, 0)
+ if type(count) is not int or count < 0:
+ raise ValueError('Reference token counts must be nonnegative integers')
+ reference_rows += count
+ effective_tokens = canvas['video_tokens'] + reference_rows if canvas is not None else None
+ if hardware.capability < (8, 0):
+ raise ValueError('This engine targets Ampere and newer NVIDIA GPUs.')
+ # Saved policies store whole bytes. Validate in that same unit so the
+ # minimum reserve survives the bytes -> GiB round trip between passes.
+ if gpu_reserve_gib is not None and (not math.isfinite(gpu_reserve_gib) or
+ gpu_reserve_gib * GiB < int(MIN_GPU_RESERVE_GIB * GiB)):
+ raise ValueError('Keep at least 0.2 GiB GPU reserve for the system.')
+ for name, value in [('VRAM', vram_gib), ('RAM', ram_gib)]:
+ if value is not None and (not math.isfinite(value) or value <= 0):
+ raise ValueError(name + ' capacity must be positive')
+ gpu_total = min(hardware.vram_total, int(vram_gib * GiB)) if vram_gib else hardware.vram_total
+ ram_total = min(hardware.ram_total, int(ram_gib * GiB)) if ram_gib else hardware.ram_total
+ desktop = hardware.system == 'Windows'
+ # Available memory already excludes the OS and other applications. Reserve
+ # modest growth headroom from that live remainder, not a second fixed share
+ # of nominal capacity. Explicit capacities remain upper bounds, never extra
+ # physical memory or pagefile capacity.
+ free_gpu = max(0, min(gpu_total, hardware.vram_free))
+ free_ram = max(0, min(ram_total, hardware.ram_available))
+ if hardware.cgroup_ram_limit is not None:
+ free_ram = min(free_ram, hardware.cgroup_ram_limit)
+ reserve_gpu = (gpu_reserve_gib if gpu_reserve_gib is not None else
+ .25 if desktop else max(.5, min(1., free_gpu / GiB * .025)))
+ reserve_ram = ram_reserve_gib if ram_reserve_gib is not None else max(2. if desktop else 1., min(4., free_ram / GiB * .05))
+ if not math.isfinite(reserve_ram) or reserve_ram < 0:
+ raise ValueError('System RAM headroom must be finite and nonnegative.')
+ gpu_budget = free_gpu - int(reserve_gpu * GiB)
+ ram_budget = free_ram - int(reserve_ram * GiB)
+ # What this machine has actually held, for the weight cache only.
+ #
+ # Retaining weights is worth a 20 GB disk read per step, and only while
+ # the retained pages stay physically resident: a retained block paged out
+ # is a slower disk, not a saving. So the ceiling on retention is physical
+ # RAM this machine can hold, which is not what an availability API
+ # reports. GlobalMemoryStatusEx counts free and standby pages only, so a
+ # tree's own resident mapped weights are missing from it -- a request
+ # using 4.30 GiB was once refused against an 8.50 GiB budget while the
+ # tree held a 21.85 GiB working set. The runtime guard already credits
+ # those pages back; planning did not, so a machine that had just held the
+ # model saw a small availability figure and retained nothing. On a 31.6
+ # GiB laptop reporting 7.5 GiB available that meant no retention at all
+ # and 21.6 GB read from disk every step.
+ #
+ # The credit is a measurement, not an assumption about other processes:
+ # the caller passes the largest RAM peak a complete, verified run reached
+ # on this machine under the same compute build and geometry, or None. A
+ # first run gets no credit and behaves exactly as before. It cannot raise
+ # the guard's budget or admission -- only what the weight cache may keep.
+ retention_budget = ram_budget
+ if demonstrated_ram_bytes is not None:
+ if type(demonstrated_ram_bytes) is not int or demonstrated_ram_bytes < 0:
+ raise ValueError('Demonstrated RAM must be a nonnegative byte count')
+ retention_budget = max(ram_budget, min(demonstrated_ram_bytes,
+ ram_total - int(reserve_ram * GiB)))
+ small_adjustment = (int(4.6 * GiB * (effective_tokens / (72 * 1008) - 1.))
+ if canvas is not None else 0)
+ residual_need = max(5 * GiB, RESIDUAL_ACTIVATION_RESERVE + small_adjustment)
+ residual_floor = (residual_need if desktop else
+ max(5 * GiB, RESIDUAL_ADMISSION_FLOOR + small_adjustment))
+ original_reserve_gpu = reserve_gpu
+ # Let a foreground request spend growth headroom before rejecting it.
+ # This is live free memory, not the card's nominal capacity: a busy larger
+ # card benefits too. Keep admitted placements and explicit user reserves
+ # unchanged. Below 10 GiB even the reduced reserve stays in the bounded
+ # small-card path. Neither the working-set estimate nor RAM is relaxed.
+ # The reduced floor is the same on Windows. A larger one there is
+ # tempting, because over-allocating VRAM is backed by system memory
+ # instead of failing and the result is a collapse in speed rather than a
+ # recoverable error -- but the field does not support it and a refusal is
+ # its own failure. Three complete Windows requests peaked at 10.50 GiB of
+ # local usage against 10.70 GiB of live free VRAM, 0.20 GiB of margin,
+ # with no over-budget sample in any of them. Raising the floor to 0.50
+ # turned a 6.8 GiB card from admitted into refused and bought nothing the
+ # field had measured. What protects Windows instead is the allocator
+ # ceiling, which makes the caching allocator OOM rather than leaning on
+ # WDDM, and the two bounds above: the head group may not exceed its
+ # measured requirement, and the no-weights path stays on the platform
+ # whose margin was measured.
+ if not desktop and gpu_reserve_gib is None and free_gpu < 10 * GiB and gpu_budget < residual_need:
+ reserve_gpu = MIN_GPU_RESERVE_GIB
+ gpu_budget = free_gpu - int(reserve_gpu * GiB)
+ # The encoding phase already has a measured host floor. Do not discard a
+ # preloaded encoder because the later video stage needs a larger minimum;
+ # automatic_profile replans generation after conditioning has been saved.
+ ram_minimum = ENCODER_HOST_BYTES if stage == 'encoding' else 4*GiB
+ if gpu_budget < 5 * GiB or ram_budget < ram_minimum:
+ raise ResourceBudgetError(hardware, gpu_total, ram_total, free_gpu, free_ram, reserve_gpu, reserve_ram,
+ ram_minimum_bytes=ram_minimum)
+ if ram_budget < ENCODER_HOST_BYTES:
+ # Say so here rather than let a request spend a minute loading a
+ # 14.6 GiB encoder and die before the first sampling step, which is
+ # what a 10 GiB host did. Cached conditioning skips the encoder, so
+ # this is what the note below offers.
+ raise ResourceBudgetError(hardware, gpu_total, ram_total, free_gpu, free_ram,
+ reserve_gpu, reserve_ram, canvas=canvas,
+ ram_minimum_bytes=ENCODER_HOST_BYTES,
+ ram_requirement='text_encoder_host')
+ # A foreground generation may try the existing bounded path below a
+ # measured estimate after reclaiming owned idle caches. This does not add
+ # capacity or spend the user's reserve; real allocator OOMs remain bounded
+ # recoverable failures. Strict estimates remain the default for callers;
+ # installation may preview this candidate without claiming it fits.
+ capacity_trial = bool(allow_capacity_trial and gpu_budget < residual_floor)
+ available = set(available_backends if available_backends is not None else {'cudnn', 'torch-flash'})
+ if attention == 'auto':
+ attention = next((name for name in ('sage2', 'torch-flash', 'cudnn', 'fa2', 'fa4') if name in available), '')
+ legs = attention.split('/')
+ if len(legs) == 1:
+ legs *= 2
+ if len(legs) != 2 or any(leg not in available for leg in legs):
+ raise ValueError('Requested attention backend is unavailable or failed its kernel probe: ' + attention)
+ # Small cards trade extra transfers for lower activation storage. The
+ # 12 GiB native-FP8 preset comes from the matched 12/32 capacity experiments.
+ small = gpu_budget < 10 * GiB
+ # Lowering Windows headroom can lift a 12 GiB laptop across the old
+ # 10 GiB boundary. Keep its affordable head-4 path in that narrow band
+ # until the larger path's actual workspace fits; entering that path only
+ # to fall back to CPU residuals would waste the newly available memory.
+ if (desktop and hardware.architecture != 'ampere' and gpu_budget < 11 * GiB
+ and gpu_budget < windows_gpu_output_workspace(effective_tokens, prefetch=False)):
+ small = True
+ # No gap between the small and twelve bands. A 0.25 GiB sliver used to fall
+ # through both and take an unmeasured branch with three resident blocks,
+ # which made residency drop as the budget grew: three blocks at 10.1 GiB
+ # against one at 10.3 GiB.
+ twelve = not small and gpu_budget < 14 * GiB
+ ampere = hardware.architecture == 'ampere'
+ prefetch = prefetch_for_budget(gpu_budget, ram_budget=ram_budget,
+ twelve=twelve, ampere=ampere,
+ system=hardware.system, architecture=hardware.architecture)
+ # The larger head group is worth about 10% at no extra peak at all. Paying
+ # for its wider activation allowance out of residency, measured on an H200
+ # at 1344x768x243 with the card as the ceiling:
+ #
+ # cap 15 GiB head 8 -> 16 9.93 -> 9.08 s/step peak 14.68 both
+ # cap 16 GiB head 8 -> 16 9.96 -> 8.99 s/step peak 15.83 both
+ # cap 18 GiB head 8 -> 16 10.05 -> 9.01 s/step peak 16.61 both
+ # cap 20 GiB head 8 -> 16 10.13 -> 9.10 s/step peak 17.72/17.70
+ #
+ # The peaks are equal to the hundredth of a GiB: five fewer resident blocks
+ # is 2.01 GiB, which is what the wider allowance asks for. The old 20 GiB
+ # floor cost every card between there and the twelve band about a tenth of
+ # its sampling time for nothing.
+ #
+ # Windows keeps the old floor. Its GPU output workspace below is written
+ # for head 8 and was added for a 15.9 GiB card, which is the band this
+ # moves; no Windows measurement covers the wider group there. The floor is
+ # the twelve band's own boundary rather than the 14.5 GiB budget measured
+ # above, so that no half-GiB sliver falls between two bands the way a
+ # 0.25 GiB sliver once did below.
+ if desktop:
+ head = 8 if twelve or gpu_budget < 20 * GiB else 16
+ # The band alone never checked whether the group's activations fit,
+ # and on Windows nothing downstream would tell us: exceeding the card
+ # is backed by system memory instead of failing, which is a silent
+ # collapse in speed rather than an error the controller can recover.
+ # The measured requirement is SM90's, and Windows needs at least that
+ # much -- no expandable segments, and a compositor taking VRAM during
+ # the run -- so it is a lower bound here and may only narrow the group,
+ # never widen it. A 11 GiB card at 345 frames took head 8 against a
+ # 10.00 GiB budget for a group measuring 10.53.
+ while head > 4 and activation_bytes(head, effective_tokens) > gpu_budget:
+ head //= 2
+ else:
+ # The widest group the request can afford at its own token count.
+ # A fixed budget threshold cannot express this: 41472 tokens take
+ # sixteen heads on an 8 GiB card and 102816 do not take eight on a
+ # 10 GiB one, and the group is worth 13 to 51% where it fits.
+ head = next((group for group in sorted(ACTIVATION_BY_TOKENS, reverse=True)
+ if gpu_budget >= activation_bytes(group, effective_tokens)), 4)
+ resident = (4 if ampere else 1) if twelve else 3
+ residual_offload = False
+ cpu_outputs = False
+ no_weights = False
+ if small:
+ # Match the reference activation allowance to the actual geometry,
+ # then spend remaining memory on useful residency.
+ # Full-resolution Windows requests keep their existing conservative
+ # path. Small canvases can use the wider measured group, paying its
+ # larger allowance out of residency, never out of system reserves or
+ # by moving attention outputs to the host just to widen the group.
+ windows_small_head8 = (desktop and not ampere and legs == ['sage2', 'sage2']
+ and effective_tokens is not None
+ and canvas['frames'] <= WINDOWS_SMALL_HEAD8_FRAMES
+ and effective_tokens <= WINDOWS_SMALL_HEAD8_TOKENS)
+ if not desktop and head >= 8:
+ # The measured requirement for the group this request can afford,
+ # already scaled by its token count, so the band's own reference
+ # allowance and its linear correction do not apply. The figure
+ # includes the GPU attention outputs; the allowance added after
+ # selection puts them back, so take them off here rather than
+ # charge them twice -- doing that once bought a wider group by
+ # moving the outputs to the host, which measured eight times
+ # slower.
+ selected = (head, activation_bytes(head, effective_tokens) - GPU_ATTENTION_OUTPUT_BYTES)
+ else:
+ available = [(heads, need) for heads, need in SMALL_ACTIVATION_RESERVES
+ if heads <= 4 or not desktop or (windows_small_head8
+ and gpu_budget >= need + small_adjustment + GPU_ATTENTION_OUTPUT_BYTES)]
+ selected = next(((heads, need + small_adjustment) for heads, need in available
+ if gpu_budget >= need + small_adjustment), None)
+ if (selected is None and not desktop
+ and free_gpu >= SMALL_NO_WEIGHTS_PEAK + small_adjustment):
+ # Hold no weights at all rather than move the residual stream to
+ # the host. The card can take the whole activation path; what it
+ # cannot take is that path plus a resident block. On an 8 GiB card
+ # at 1344x768x243, against the residual-storage plan this replaces:
+ #
+ # 32 GiB RAM 34.76 -> 13.49 s/step peak 7.68 GiB of 8.00
+ # 16 GiB RAM 34.17 -> 14.75 s/step peak 7.70 GiB of 8.00
+ #
+ # One resident block does not fit beside it -- 8.04 and 8.02 GiB,
+ # over the card at both RAM values and both chunk sizes -- and the
+ # block is worth under 1% against this path's 2.6x. Eight
+ # heads do not fit either, and the head group rather than the
+ # batching is why: window 1, 2 and 4 and both chunk sizes all
+ # peaked exactly 8.02 GiB.
+ #
+ # The gate is live free VRAM rather than the budget, because the
+ # growth headroom is what pays: the peak leaves 0.30 GiB of an
+ # 8 GiB card where the reserve would hold back 0.50. That is the
+ # same trade the reserve reduction below makes, for 2.6x instead
+ # of for admission.
+ #
+ # Linux only. This is a measured whole-device peak with 0.20 to
+ # 0.32 GiB of margin, not an arithmetic bound, and three things
+ # stop that margin transferring to Windows: PyTorch has no
+ # expandable segments there, so a tight allocation fragments
+ # instead of growing a segment; the desktop compositor takes VRAM
+ # during the run, so the live figure this gate reads can shrink
+ # under it; and exceeding the card does not fail there. WDDM backs
+ # the excess with system memory silently, which is catastrophically
+ # slow rather than an error the controller can recover. A platform
+ # with no error to catch needs a margin it has measured.
+ selected = (4, SMALL_ACTIVATION_RESERVE + small_adjustment)
+ no_weights = True
+ if (selected is None and not desktop
+ and reserve_gpu == original_reserve_gpu
+ and free_gpu >= SMALL_HOST_OUTPUT_PEAK + small_adjustment
+ and gpu_budget < SMALL_HOST_OUTPUT_PEAK + GPU_ATTENTION_OUTPUT_BYTES):
+ # The same no-weights path one card size down, where the budget
+ # cannot also cover the attention outputs, so they stay in host
+ # memory. Leaving the reserve below the budget keeps them there and
+ # holds no blocks, which is the configuration measured.
+ #
+ # The second condition is what keeps this to that configuration. An
+ # 8 GiB card with 7.5 GiB live has a 7.00 GiB budget, which does
+ # cover the outputs on the card -- and then the peak to beat is the
+ # 7.68-7.70 GiB the branch above measured, against 7.5 of live
+ # VRAM. That card keeps staging, as its own test says.
+ #
+ # The reserve condition is the other half of that. This route
+ # trades peak for speed, and a card whose growth reserve has
+ # already been reduced to be admitted at all has nothing left to
+ # trade: a 6.8 GiB free card reaches a 6.60 budget only by
+ # spending 0.30 of its reserve, and stacking a higher-peak route
+ # on top of that is two risks for one gain. Those keep staging.
+ #
+ # This is what the 7 GiB band should have been running. It reached
+ # the residual-staging route below because that route's gate asks
+ # for 6.4 GiB of budget while the no-weights gate above asks for
+ # 7.8 GiB of live VRAM -- and 7.8 was measured with the outputs on
+ # the card. With them in host memory the same path peaks 6.33 GiB
+ # on a 7 GiB card, which the gate above never asked about. The
+ # staging it replaces moves 158 GB over PCIe every step for
+ # nothing: measured at 1344x768x243, whole-device peak 6.33 either
+ # way, at 8, 12, 16 and 32 GiB of RAM.
+ #
+ # staging on 39.07 38.37 -- 38.25 s/NFE
+ # staging off 22.64 21.65 21.09 19.04
+ #
+ # Also measured at consumer compute, because this cluster's card
+ # is not the target: at an MPS thread share of 6%, calibrated to
+ # reproduce an RTX 4080 Laptop's measured 101.6 s/NFE, the pair is
+ # 136.78 against 118.77 -- 1.15x rather than 2.05x, since the
+ # staging is PCIe-bound and does not scale with the cards. A
+ # consumer card's narrower bus makes 1.15x the floor, not the
+ # estimate. Linux only, for the reasons the branch above gives.
+ selected = (4, SMALL_HOST_OUTPUT_PEAK + small_adjustment)
+ if selected is None and (gpu_budget >= residual_floor or capacity_trial):
+ # Four heads here too: the 8 GiB corner above takes this path and
+ # measured the same peak as two with 18% less sampling time.
+ selected = (4, residual_need)
+ # Staging stays on here, and the measurement that said otherwise
+ # was reading an allocator rather than a requirement. On an H200
+ # with the capacity simulated, complete eight-step requests at
+ # three budgets peaked identically with staging on and off --
+ # 6.267 -> 6.098 both ways, 35.03 against 19.12 s/NFE -- so
+ # staging looked like pure cost. It is not. Both configurations
+ # were then run at that budget on the RTX PRO 6000 the budget came
+ # from, and with the outputs in host memory and no staging the
+ # first sampling step died trying to allocate 112 MiB with 18.86
+ # MiB free, at a 7.665 GiB whole-device peak. With staging the
+ # same request completed at 6.712 GiB and 36.86 s/NFE. Staging is
+ # worth 0.95 GiB of peak in this band; expandable_segments only
+ # made the H200's peak track its budget because nothing there
+ # enforced one.
+ #
+ # This mattered in production, not in theory: every Linux card
+ # with 6.6 to 7.2 GiB of visible free memory -- the 8 GiB consumer
+ # band -- was being handed exactly the configuration that died.
+ # Windows always staged, so it was never exposed.
+ # benchmarks/pro6000-verification/results-pro6000.md.
+ residual_offload = True
+ if selected is None:
+ # Use the same recoverable admission error as the coarse floor.
+ # A plain ValueError bypasses the controller's one idle-cache
+ # release/remeasurement and loses the actual request shortfall.
+ raise ResourceBudgetError(hardware, gpu_total, ram_total, free_gpu, free_ram,
+ reserve_gpu, reserve_ram, gpu_minimum_bytes=residual_floor,
+ gpu_requirement='estimated_working_set', canvas=canvas)
+ head, reserve = selected
+ # The host attention-output buffer was tied to this band with no
+ # arithmetic behind it. It saves 0.04 to 0.23 GiB of peak and costs 19%
+ # to 36% of sampling time -- 400 host round trips per request, 50
+ # blocks by 8 steps -- measured at four corners of the band. Keep the
+ # outputs on the GPU when the budget covers their peak, and let
+ # residency pay for them: residency buys under 1% here, so one block is
+ # a good price for a fifth to a third of the sampling time. When the
+ # budget cannot cover them the host buffer stays, which is what the
+ # 9 GiB/1344x768x345 corner needs.
+ cpu_outputs = not no_weights and gpu_budget < reserve + GPU_ATTENTION_OUTPUT_BYTES
+ if not cpu_outputs:
+ reserve += GPU_ATTENTION_OUTPUT_BYTES
+ # Outputs in host memory means the budget could not cover them on the
+ # card, which is exactly the regime the PRO 6000 run measured: there,
+ # not staging cost 0.95 GiB of peak and died in the first sampling
+ # step. So every branch in this band that moves the outputs to the
+ # host stages as well, not just the one that computes residual_need.
+ # Without this the host-output branch alone still handed the dead
+ # configuration to every Linux card with 6.7 to 7.2 GiB free.
+ residual_offload = residual_offload or cpu_outputs
+ if desktop and not residual_offload:
+ # The old linear geometry discount may undercut the measured
+ # token table at intermediate canvases. Residency must fund that
+ # difference instead of spending the smaller OS reserve.
+ reserve = max(reserve, activation_bytes(head, effective_tokens))
+ # Residency comes last because it buys under 1% here: measured H2D
+ # weight traffic is 6.78 s of 878 s sampling, and block 45 on mapped
+ # weights ran no slower than block 5 on pinned weights. It still
+ # relieves host RAM, so the leftover goes to it rather than staying idle.
+ resident = max(0, min(RESIDENT_VRAM_CEILING, int((gpu_budget - reserve) / BLOCK_BYTES)))
+ if not small:
+ # One measured requirement for the group in use, so the block count is
+ # continuous across the band edges rather than restarting at each.
+ resident = max(0, min(RESIDENT_VRAM_CEILING,
+ int((gpu_budget - activation_bytes(head, effective_tokens)) / BLOCK_BYTES)))
+ # Placement alone is numerically neutral. Scale the activation allowance
+ # with the actual token count before spending memory on resident weights.
+ # The small staging path is anchored to the complete 243-frame Ada run;
+ # the larger path to the complete 345-frame Linux capacity run. This is
+ # a starting estimate, not a peak extrapolated from incomplete steps.
+ geometry_adjustment = 0
+ if canvas is not None:
+ reference_tokens = (72 if small else 102) * 1008
+ variable_gib = 4.6 if small else (6. if head == 8 else 7.5)
+ geometry_adjustment = int(variable_gib * GiB * (effective_tokens / reference_tokens - 1.))
+ # No geometry credit on top of the measured requirement: that is
+ # already a function of the token count, so crediting a shorter
+ # request again counted the same discount twice. At 72576 tokens
+ # against the old 102816-token reference it added five blocks to a
+ # 15 GiB card, and the plan then peaked 16.65 GiB against a 14.50
+ # budget. The small path keeps its own correction, which applies to
+ # a reference figure rather than to a measurement.
+ windows_workspace = None
+ windows_activation_limited = False
+ if desktop and not ampere and not small:
+ # The twelve-band formula predates GPU attention outputs and the full
+ # FF stash. In addition, crediting a shorter geometry against its old
+ # 345-frame anchor added five blocks to a request that already OOMed.
+ # Budget the current execution path before host placement is planned.
+ windows_workspace = windows_gpu_output_workspace(effective_tokens, prefetch=prefetch)
+ windows_workspace += max(0, activation_bytes(head, effective_tokens) - activation_bytes(8, effective_tokens))
+ if (prefetch and gpu_budget < 14 * GiB
+ and hardware.architecture == 'blackwell-rtx'):
+ # Saving a transfer slot must not turn fully retained host weights
+ # into per-step disk reads. Compare both placements, including the
+ # full FF workspace, before spending that slot.
+ with_slot = min(resident, max(0, int((gpu_budget - windows_workspace) / BLOCK_BYTES)))
+ without_slot = min(resident, max(0, int((gpu_budget - windows_workspace + BLOCK_BYTES) / BLOCK_BYTES)))
+ host_need = (50 - with_slot) * BLOCK_BYTES + int(2.5 * GiB)
+ if host_need > ram_budget >= (50 - without_slot) * BLOCK_BYTES + int(2.5 * GiB):
+ prefetch = False
+ windows_workspace -= BLOCK_BYTES
+ resident = min(resident, max(0, int((gpu_budget - windows_workspace) / BLOCK_BYTES)))
+ # Do not enable FF recomputation merely to retain all host mappings.
+ # Native 5060 Ti measurements show that doing so makes the same layer
+ # 18% slower. Reopening an unpinned layer can hit the OS file cache and
+ # overlap compute; it is not proof of physical disk traffic. Keep the
+ # full FF workspace and bound host storage below. Recompute remains a
+ # capacity/recovery option and an explicitly measurable tuning option.
+ if windows_workspace > gpu_budget:
+ # Zero resident weights is not a solution when activations alone
+ # exceed the budget. Report 177 still admitted head 8 at 107856
+ # tokens: 15.42 GiB workspace against 12.99 GiB, then OOMed with
+ # 59 GiB of physical RAM and 303 GiB of commit still available.
+ # Use the existing bounded path before the first sampling step.
+ # In particular, do not credit FF recomputation here: Ada rowwise
+ # FF already tiles its activations, so that flag saves nothing.
+ if gpu_budget < residual_floor and not capacity_trial:
+ raise ResourceBudgetError(hardware, gpu_total, ram_total, free_gpu, free_ram,
+ reserve_gpu, reserve_ram, gpu_minimum_bytes=residual_floor,
+ gpu_requirement='estimated_working_set', canvas=canvas)
+ head = 4
+ cpu_outputs = residual_offload = True
+ prefetch = False
+ # Do not immediately spend the saved activation space on weights.
+ # A complete local request can supply measured placement later.
+ resident = 0
+ windows_workspace = residual_need
+ windows_activation_limited = True
+ if capacity_trial:
+ head, resident, prefetch = 4, 0, False
+ cpu_outputs = residual_offload = True
+ pin = max(0, min(22_000_000_000, retention_budget - HOST_WEIGHT_HEADROOM)) / 1e9
+ pin = min(pin, (50 - resident) * BLOCK_BYTES / 1e9)
+ if small:
+ # Only locking every offloaded layer removes the offloader's own
+ # staging buffers, so a partial pin pays full memory for both: a
+ # 16.2 GB plan locked 14.51 GiB, left nothing for those buffers and
+ # failed the request. All or nothing here.
+ #
+ # Locking all of them is worth about 3%. The figure this used to cite
+ # -- 0.57%, from 4.97 s of host staging in 878 s of sampling -- was
+ # measured when a step took 110 s. A step is now 12.6 s, and the same
+ # comparison at that speed, changing only the lock:
+ #
+ # 8 GiB card 12.89 -> 12.53 s/step peak 7.69 -> 7.72
+ # 9 GiB card 13.03 -> 12.62 peak 8.34 -> 8.34
+ # 10 GiB card 13.00 -> 12.61 peak 9.33 -> 9.33
+ #
+ # So 2.8 to 3.1% for at most 0.03 GiB of VRAM. Prefetching on top of
+ # it took another 2.5% at 10 GiB but overran an 8 GiB card by 0.04,
+ # so that stays on its own budget threshold.
+ #
+ # The streaming branch below recomputes the pin against its own host
+ # headroom, so the all-or-nothing test is applied after it rather
+ # than here.
+ pass
+ # Reopening a layer releases its mapping after the transfer slot is filled.
+ # Use this only when keeping all offloaded mappings would exceed live RAM.
+ # Larger machines keep the faster persistent mappings and optional pins.
+ residual_host = residual_host_headroom(canvas) if residual_offload else 0
+ host_activation = (2. if small or cpu_outputs else .5) * GiB + residual_host
+ # Residency is not a way to spend spare VRAM: 8 blocks measured faster
+ # than 29 or 48 at the same cap. What it is for is keeping the rest of the
+ # model in RAM, because rereading weights from disk every step dominates a
+ # request. So hold the smallest number RAM asks for, and no more -- which
+ # makes the two capacities covariates rather than independent axes:
+ #
+ # RAM 32 GiB the host takes all 50 -> hold 8
+ # RAM 24 GiB the host takes all 50 -> hold 8
+ # RAM 20 GiB the host takes 40 -> hold 10
+ # RAM 16 GiB the host takes 31 -> hold 19
+ #
+ # A card too small for what RAM asks still streams from disk; the retained
+ # subset below is what bounds that case.
+ host_room = retention_budget - host_activation - 2 * GiB
+ ram_wants = max(0, 50 - max(0, int(host_room // BLOCK_BYTES)))
+ if not small:
+ resident = min(resident, max(RESIDENT_TARGET, ram_wants))
+ streamed_weights = (50 - resident) * BLOCK_BYTES + host_activation + 2 * GiB > retention_budget
+ if streamed_weights:
+ # When the remaining weights exceed RAM, a bounded retained subset
+ # prevents rereading those bytes every step. This addresses disk I/O,
+ # unlike pinning already-cached mappings (under 1% in the Ada trace).
+ # Weight rereads dominate a disk-backed request. Reserve the working
+ # memory of this execution path, not the unrelated Windows CPU-output
+ # path's 8 GiB. Runtime still clamps against live physical/cgroup RAM.
+ # Name the decision, not the band. `small` used to imply the host
+ # attention buffer, so passing it here described the execution path;
+ # now the buffer follows the budget, and a small-band request that
+ # keeps its outputs on the GPU was still reserving the host-output
+ # allowance -- 5.00 GiB instead of 3.50 at this geometry, so 1.5 GiB
+ # of pinning it could have used, about four blocks it re-read instead.
+ host_headroom = (weight_cache_headroom(system=hardware.system, streamed=True,
+ cpu_outputs=cpu_outputs, canvas=canvas)
+ if ram_budget >= 8 * GiB else HOST_WEIGHT_HEADROOM)
+ host_headroom += residual_host
+ pin = max(0, min((50 - resident) * BLOCK_BYTES, retention_budget - host_headroom)) / 1e9
+ if small and not streamed_weights and pin * 1e9 < (50 - resident) * BLOCK_BYTES:
+ # All or nothing only when the weights already sit in RAM. On a
+ # disk-backed request the partial pin is not a transfer optimisation
+ # at all -- it is the bounded retained subset that stops those bytes
+ # being reread every step, which is the larger cost there.
+ pin = 0.
+ engine = dict(attention='/'.join(legs), prefetch=prefetch,
+ adaln_cache=True, adaln_disk_cache=True, inference_kernels=True,
+ # One chunk size for every band, on neutrality rather than
+ # speed. The small band's 512 measured 13.3% slower at a
+ # 10 GiB cap -- 32.95 against 28.58 s/step -- but only while
+ # the attention outputs still went to host memory. With them
+ # kept on the GPU the host round trips are gone, the FF loop
+ # is off the critical path, and the same caps measure within
+ # 1.5%: 36.55, 20.35 and 20.51 s/step at 8, 9 and 10 GiB.
+ # So this claims no speed. It is kept because the peak is
+ # identical at all twelve verified caps, because a slice is
+ # chunk x width x 2 bytes and so costs a fixed 47 MiB at any
+ # geometry, and because it drops a conditional tied to a band
+ # whose other couplings proved mispriced. The head-dependent
+ # 1024 it replaces was unreachable: only the small band had a
+ # head group under 8, and it took the 512.
+ ff_chunk=2048, head_chunk=head,
+ head_parallelism=1, # Increased only by a locally validated tuning profile.
+ projection_chunk=1024, offload_refiner=True,
+ attention_cpu_outputs=cpu_outputs, grouped_attention_outputs=cpu_outputs,
+ residual_offload=residual_offload,
+ # Do not recompute unconditionally. Earlier runs saved no
+ # peak VRAM: reserved
+ # matched the recomputing run to the byte across three probe
+ # steps (6.63, 7.37, 8.11 GiB) because those activations are
+ # freed within each block, while it cost 5% of sampling time.
+ fp8_ff_recompute=(capacity_trial and hardware.capability[0] >= 10), cache_refined_text=True,
+ resident_blocks=resident, pin_host_gb=round(pin, 3),
+ stream_weights=streamed_weights,
+ # Larger looped batches are optimization candidates; full
+ # request comparison validates numerical equivalence.
+ # Window batching is close to free at the bottom too. On an
+ # 8 GiB card, window 1 measured 13.75 s/step at a 7.68 GiB
+ # peak and window 4 measured 12.88 at 7.69, so 6.3% for
+ # 0.01 GiB. The allowance derivation above assumed window 1
+ # and added nothing for batched windows; 0.01 GiB is inside
+ # the margin it keeps. Ampere alone keeps the single window,
+ # as it does in the twelve band: no measurement here covers
+ # Ampere with the larger batch. Platform is not a gate; the
+ # twelve band already batches four on Windows.
+ # Four windows everywhere except Ampere, which has no
+ # measurement with the larger batch. Above the twelve band
+ # it measured 9.23 -> 9.10 s/step at a 16 GiB cap and
+ # 9.24 -> 9.11 at 20 GiB, with the whole-device peak
+ # unchanged at 15.83 and 18.09 GiB. Dropping to two at a
+ # 12 GiB cap cost 0.2%, so one rule covers every band.
+ window_batch=1 if ampere else 4,
+ linear_compute='bf16-weight-only' if ampere else 'native-fp8')
+ notes = ['Request resolution, frame count, steps and attention backend are preserved.',
+ 'Budgets use current free VRAM and available physical/commit RAM, with growth headroom; OS usage is already excluded.',
+ 'Automatic execution uses matching GPU kernel probes; read-only plans may use package discovery. Small probes do not prove full-video capacity.',
+ 'Budgets guide placement; cgroup/MPS enforcement belongs to capacity benchmarks.']
+ if canvas is not None:
+ notes.append('Geometry: %dx%d, %d frames, %d video tokens; resident-weight allowance adjusted by %.3f GiB for activation storage.' %
+ (canvas['width'], canvas['height'], canvas['frames'], canvas['video_tokens'], geometry_adjustment / GiB))
+ if desktop and small and head == 8:
+ notes.append('Use eight-head groups for this small Windows canvas (%d generated and reference rows). '
+ 'Its larger workspace allowance and GPU attention outputs fit inside the current budget; '
+ 'weight residency pays for the wider group. Each pass is planned at its own total token count. '
+ 'Measured speed benefit is from PRO 6000 Linux; native Windows throughput remains unmeasured.'
+ % effective_tokens)
+ if capacity_trial:
+ notes.append('The %.2f GiB live budget is below the %.2f GiB working-set estimate. '
+ 'Attempt the bounded host-output/residual path with no resident blocks; '
+ 'per-tensor FP8 recomputes FF tiles instead of retaining the full activation stash. '
+ 'Keep resolution, duration, steps, backend and reserves unchanged. '
+ 'This is a capacity attempt, not measured proof that this GPU fits.' %
+ (gpu_budget / GiB, residual_floor / GiB))
+ if windows_activation_limited:
+ notes.append('The Windows GPU-output activation estimate exceeds the live budget even with zero resident blocks. Start this token count with four-head groups, host attention outputs and one host residual buffer; estimated workspace %.2f GiB. Resolution, duration, steps, weights and attention backend are preserved. This is request-specific, not a persistent compatibility downgrade.' % (windows_workspace / GiB))
+ elif windows_workspace is not None:
+ notes.append('Windows head-8 GPU-output workspace estimate: %.2f GiB for the selected FF storage path, caches and transfer buffers; remaining live budget permits %d resident blocks. Based on complete Windows measurements, not a capacity guarantee. Head/window/FF tile shapes and system reserves are unchanged.' %
+ (windows_workspace / GiB, resident))
+ if residual_offload:
+ notes.append('The original small path exceeds the live VRAM budget: stage residuals in one reusable CPU buffer, preserving kernel shapes, dtype and every requested row. Host weight caching leaves an additional %.2f GiB for this buffer. Extra transfers cost time; this is a capacity route, not a claimed speedup.' % (residual_host / GiB))
+ if streamed_weights:
+ notes.append('Live RAM cannot retain all offloaded mappings: keep at most %.3f GB pinned, reopen only remaining layers per transfer; no arithmetic or attention change.' % pin)
+ notes.append('Host weight cache keeps %.2f GiB for this path\'s working memory, inside the separate OS growth reserve; the runtime rechecks available RAM.' % (host_headroom / GiB))
+ if hardware.architecture == 'ampere':
+ notes.append('Ampere keeps FP8 weight storage and uses bounded BF16 compute; no native FP8 tensor-core claim.')
+ if desktop:
+ notes.append('Windows keeps %.2f GiB beyond current GPU usage and %.2f GiB beyond current available RAM/commit; pagefile capacity is not added to RAM.' % (reserve_gpu, reserve_ram))
+ if retention_budget > ram_budget:
+ notes.append('Weight caching may keep %.2f GiB rather than the %.2f GiB currently reported available: a complete verified request on this machine, under this compute build and geometry, held that much. The request budget and admission still use the reported figure.'
+ % (retention_budget / GiB, ram_budget / GiB))
+ if reserve_gpu < original_reserve_gpu:
+ notes.append('Foreground VRAM growth reserve reduced from %.2f to %.2f GiB: %.2f GiB live free minus the original reserve could not cover the %.2f GiB smallest-profile estimate. The request can try the available capacity; this is not measured proof that it fits.' %
+ (original_reserve_gpu, reserve_gpu, free_gpu / GiB, residual_need / GiB))
+ # The transformer is released before decode. Full-GPU VAE decode measured
+ # below 14 GiB on SM120; keep additional margin for other devices/backends.
+ decoder_offload = gpu_budget < 20 * GiB
+ if reference_rows:
+ notes.append('Reference conditioning adds %d packed rows; activation placement scales with total rows. This is a starting estimate, not a verified reference-capacity result.' % reference_rows)
+ # A full floating video plus its RGB copy grows with output geometry.
+ # The temporal streaming path uses the original decoder tiles and blend;
+ # completed uint8 frames go to the retained disk artifact immediately.
+ output_bytes = (canvas['width'] * canvas['height'] * canvas['frames'] * 3
+ if canvas is not None else 1344 * 768 * 243 * 3)
+ stream_output = decoder_offload or ram_budget < 8 * GiB + 3 * output_bytes
+ decoder_stream_weights = decoder_offload and ram_budget < 14 * GiB
+ # A decoder block contains 268,574,720 bytes of original FP32 weights.
+ # Temporal streaming bounds the live clip workspace, so use spare VRAM for
+ # these repeatedly-read weights before spending RAM on host copies. At the
+ # reference canvas a streamed/offloaded clip measured 3.07 GiB whole-GPU;
+ # 3.25 GiB includes the transfer slot and a small workspace allowance.
+ area_ratio = (canvas['width'] * canvas['height'] / (1344 * 768)) if canvas is not None else 1.
+ vae_workspace = int(3.25 * GiB * max(1., area_ratio))
+ vae_resident = max(0, min(36, int((gpu_budget - vae_workspace) / 268574720))) if decoder_offload else 0
+ # The bounded decoder loader stores Linear weights in the FP16 dtype that
+ # autocast would use anyway. This reduces streamed transfer volume and
+ # avoids repeated conversion in a fully resident decoder. Both paths are
+ # bit-identical on Linux RTX PRO 6000 Blackwell and native Windows
+ # RTX 4060 Ti / RTX 5060 Ti. The Windows Blackwell cache also avoids
+ # materializing a full FP32 checkpoint on the host before decode.
+ # Windows also avoids the whole-checkpoint CPU load before uploading the
+ # resident prefix. Keep residency/headroom unchanged: this spends less
+ # memory on the same placement, rather than filling the reclaimed space.
+ decoder_linear_cache = ((hardware.architecture == 'blackwell-rtx'
+ and hardware.system == 'Linux')
+ or (hardware.architecture in ('ada', 'blackwell-rtx')
+ and hardware.system == 'Windows'))
+ if decoder_offload:
+ weight_format = 'FP16 Linear / FP32 other' if decoder_linear_cache else 'FP32'
+ notes.append('Decoder keeps %d/36 %s blocks on GPU using live free VRAM; streams only the remainder, with original tiles and blending.' % (vae_resident, weight_format))
+ if stream_output:
+ notes.append('Decode streams original temporal clips into rgb.npy; full floating video and duplicate RGB storage are avoided.')
+ return Policy(2, hardware.to_dict(), gpu_budget, ram_budget, int(reserve_gpu * GiB),
+ int(reserve_ram * GiB), 'backend:native,pinned_max_round_threshold_mb:1,expandable_segments:True',
+ # 504 serialized VAE weight transfers fill 124.8 s of a
+ # 140.4 s decode here, with one slot and no overlap, so a
+ # second slot is worth measuring. It stays off until a
+ # complete request verifies it: nothing ships on probe
+ # evidence alone in this band.
+ engine, dict(offload=decoder_offload, prefetch=decoder_offload and not small,
+ tile_group=decoder_offload, preload=False,
+ pin_weights=decoder_offload and ram_budget >= 16 * GiB,
+ stream_output=stream_output, stream_weights=decoder_stream_weights,
+ resident_blocks=vae_resident,
+ linear_compute_cache=decoder_linear_cache),
+ 'Architecture-specific candidate; validate this exact policy on the target GPU.', notes,
+ capacity_trial=capacity_trial)
diff --git a/freevideo_engine/portable.py b/freevideo_engine/portable.py
new file mode 100644
index 0000000..8b1f759
--- /dev/null
+++ b/freevideo_engine/portable.py
@@ -0,0 +1,233 @@
+"""Relocatable offline bundle initialization; the GUI never imports Torch."""
+import argparse
+from contextlib import contextmanager
+import json
+import os
+from pathlib import Path, PurePosixPath
+import subprocess
+import sys
+import time
+import traceback
+
+from .monitoring import save
+
+
+@contextmanager
+def child_lease(descriptor):
+ from .locking import LOCK_ENV
+ previous = os.environ.get(LOCK_ENV)
+ os.environ[LOCK_ENV] = str(descriptor)
+ try:
+ yield
+ finally:
+ if previous is None:
+ os.environ.pop(LOCK_ENV, None)
+ else:
+ os.environ[LOCK_ENV] = previous
+
+
+def inside(root, name):
+ if (not isinstance(name, str) or not name or '\\' in name or ':' in name
+ or PurePosixPath(name).is_absolute() or any(p in ('', '.', '..') for p in name.split('/'))):
+ raise ValueError('Invalid bundle path: ' + str(name))
+ root = Path(root).resolve()
+ path = root / name
+ if any(p.is_symlink() or (hasattr(p, 'is_junction') and p.is_junction())
+ for p in [path, *path.parents] if p != root and root in p.parents):
+ raise ValueError('Bundle files must not redirect outside their directory: ' + name)
+ if root not in path.resolve().parents:
+ raise ValueError('Bundle path escapes its directory: ' + name)
+ return path
+
+
+def manifest(root):
+ value = json.loads((Path(root) / 'portable.json').read_text(encoding='utf-8'))
+ if value.get('schema_version') != 1 or value.get('variant') not in ('rowwise', 'per_tensor'):
+ raise ValueError('Invalid FreeVideo portable bundle')
+ seen = set()
+ for row in value['files']:
+ inside(root, row['path'])
+ if row['path'].casefold() in seen or type(row['bytes']) is not int or row['bytes'] < 0:
+ raise ValueError('Invalid or duplicate bundle file')
+ seen.add(row['path'].casefold())
+ if len(row['sha256']) != 64 or any(c not in '0123456789abcdef' for c in row['sha256']):
+ raise ValueError('Invalid bundle checksum')
+ for name in ('python', 'source', 'comfy', 'vdn', 'model_root', 'encoder_root', 'cache'):
+ inside(root, value[name])
+ return value
+
+
+def verify(root, value, progress=lambda **kw: None):
+ """Stream bytes once; only settled file identities can reuse verification."""
+ from .storage import fingerprint
+ from .network import hash_file
+ root = Path(root)
+ ledger = root / 'engine' / 'portable-verified.json'
+ try:
+ prior = json.loads(ledger.read_text(encoding='utf-8'))
+ except (OSError, ValueError):
+ prior = {}
+ stamps = {}
+ total = sum(r['bytes'] for r in value['files'])
+ done, started, last_event = 0, time.monotonic(), [0.]
+ for row in value['files']:
+ path = inside(root, row['path'])
+ if not path.is_file() or path.stat().st_size != row['bytes']:
+ raise ValueError('Missing or incomplete bundled file: ' + row['path'])
+ stamp = dict(fingerprint(path), sha256=row['sha256'])
+ settled = ('change_time_ns' not in stamp or
+ (stamp['change_time_ns'] is not None and max(stamp['mtime_ns'], stamp['change_time_ns']) < time.time_ns()-10**9))
+ if not settled or prior.get(row['path']) != stamp:
+ def update(current, length):
+ elapsed = time.monotonic()-started
+ if elapsed-last_event[0] < .2:
+ return
+ last_event[0] = elapsed
+ progress(stage='verify', done=done+current, total=total, detail=row['path'],
+ elapsed_seconds=elapsed, bytes_per_second=(done+current)/max(.001, elapsed))
+ if hash_file(path, discard_cache=True, progress=update) != row['sha256']:
+ raise ValueError('Bundled file failed verification: ' + row['path'])
+ if stamp != dict(fingerprint(path), sha256=row['sha256']):
+ raise ValueError('Bundled file changed during verification: ' + row['path'])
+ stamps[row['path']] = stamp
+ done += row['bytes']
+ save(ledger, stamps)
+ progress(stage='verify', done=total, total=total, detail='All bundled files verified')
+
+
+def environment(root, value, environ=None):
+ from .comfy_environment import isolated_environment
+ root = Path(root).resolve()
+ env = isolated_environment(root/'engine', inside(root, value['source']), environ)
+ env.update(FREEVIDEO_MODEL_ROOT=str(inside(root, value['model_root'])),
+ FREEVIDEO_VDN_ROOT=str(inside(root, value['vdn'])),
+ FREEVIDEO_COMFY_ROOT=str(inside(root, value['comfy'])),
+ TORCHINDUCTOR_CACHE_DIR=str(root/'engine/cache/inductor'),
+ TRITON_CACHE_DIR=str(root/'engine/cache/triton'),
+ HF_HOME=str(root/'engine/cache/huggingface'),
+ HF_HUB_OFFLINE='1', TRANSFORMERS_OFFLINE='1')
+ # Bundled Python/Triton provide their DLLs and compiler. An unrelated CUDA
+ # installation or build compiler must not override this sealed runtime.
+ for key in ('PYTHONHOME', 'CUDA_HOME', 'CUDA_PATH', 'CC', 'CXX'):
+ env.pop(key, None)
+ python = inside(root, value['python']).parent
+ env['PATH'] = os.pathsep.join([str(python), str(python/'Scripts'), str(python/'Lib/site-packages/bin'), env.get('PATH', '')])
+ return env
+
+
+def configuration(root, value, hardware):
+ root = Path(root).resolve()
+ expected = 'per_tensor' if hardware.capability[0] >= 10 else 'rowwise'
+ if hardware.capability < (8, 0) or value['variant'] != expected:
+ raise ValueError('This GPU needs the %s bundle; this archive contains %s. No models were downloaded or changed.' % (expected, value['variant']))
+ from . import __version__
+ spec = json.loads((Path(__file__).with_name('dependencies.json')).read_text(encoding='utf-8'))
+ models = inside(root, value['model_root'])
+ return dict(schema_version=1, root=str(root/'engine'), source=str(inside(root, value['source'])),
+ environment_layout='unified', system=hardware.system, engine_version=__version__,
+ python=str(inside(root, value['python'])), comfy_python=str(inside(root, value['python'])),
+ comfy_root=str(inside(root, value['comfy'])), vdn_root=str(inside(root, value['vdn'])),
+ model_root=str(models), encoder_model_root=str(inside(root, value['encoder_root'])),
+ base=str(models/'h3-base'), checkpoint=str(models/'stage-dmd-step-250'),
+ cache=str(inside(root, value['cache'])), encoder=spec['models']['encoder_file'].split('/')[-1],
+ model_paths=str(root/'engine/encoder-paths.yaml'), kernel_capabilities=str(root/'engine/kernel-capabilities.json'),
+ gpu_uuid=hardware.gpu_uuid, gpu_name=hardware.gpu_name, model_revision=spec['models']['vdn_revision'],
+ vram_gib=None, ram_gib=None, ready=False, portable=True,
+ portable_revision=value['source_commit'], setup_run=str(root/'engine/portable-runs'),
+ platform_validation='Local kernel checks only; full-video diagnostics remain separate.')
+
+
+def initialize(root, progress=lambda **kw: None):
+ root = Path(root).resolve()
+ value = manifest(root)
+ os.environ.update(environment(root, value))
+ from .locking import runtime_lock
+ with runtime_lock(root/'engine/engine.lock', inherit=False) as descriptor, child_lease(descriptor):
+ verify(root, value, progress)
+ progress(stage='gpu', detail='Detecting your GPU and testing acceleration', done=0, total=1)
+ from .hardware import detect
+ hardware = detect()
+ machine = configuration(root, value, hardware)
+ save(root/'engine/machine.json', machine)
+ save(root/'engine/encoder-paths.yaml', {'freevideo': {
+ 'base_path': machine['encoder_model_root'], 'text_encoders': 'text_encoders/'}})
+ # Reuse only this device/driver/runtime's local kernel receipt. No
+ # foreign ready machine.json or precompiled GPU cache ships in a ZIP.
+ from .kernel_capabilities import available_backends
+ available_backends(hardware)
+ machine['ready'] = True
+ save(root/'engine/machine.json', machine)
+ save(root/'engine/prepared-cache.json', dict(cache=machine['cache'],
+ model_revision=machine['model_revision'], verified_at_epoch=time.time()))
+ progress(stage='gpu', detail=hardware.gpu_name, done=1, total=1)
+ return machine
+
+
+def connect(root, value, machine, progress=lambda **kw: None, cancelled=None, *, url='http://127.0.0.1:8188', on_controller=None):
+ """Use the existing ComfyUI connection/startup behavior without installing."""
+ from .comfy_launcher_runtime import Controller
+ controller = Controller(machine['source'], child_environment=lambda: environment(root, value))
+ controller.shortcut_root = root
+ if not machine['gpu_uuid'].startswith(('GPU-', 'MIG-')):
+ raise ValueError('The CUDA device did not provide a valid GPU identity; rerun the GPU check.')
+ # CUDA UUIDs survive a Windows display-adapter order change. The host and
+ # its engine children must see the same device that passed initialization.
+ os.environ['CUDA_VISIBLE_DEVICES'] = machine['gpu_uuid']
+ if cancelled is not None:
+ controller.cancelled = cancelled
+ if cancelled.is_set():
+ raise RuntimeError('Startup cancelled')
+ controller.selection = dict(ready=True, root=machine['comfy_root'], engine=machine['root'],
+ source=machine['source'], python=machine['python'], url=url,
+ portable=False, separate=False)
+ controller.restore_terminal(machine['root'])
+ if on_controller:
+ on_controller(controller)
+ controller.stage = lambda name, **kw: progress(stage='comfy', detail=kw.get('label', name))
+ controller._connect()
+ if controller.state.get('status') != 'open':
+ raise RuntimeError(controller.state.get('error', 'ComfyUI could not open'))
+ return controller.state['url']
+
+
+def gui(root):
+ from .portable_launcher import gui as launcher_gui
+ launcher_gui(root)
+
+
+def main(root=None, argv=None):
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--initialize', action='store_true')
+ parser.add_argument('--smoke', action='store_true', help='Check native imports and GUI without touching a GPU or downloading models')
+ args = parser.parse_args(argv)
+ root = Path(root or Path(sys.argv[0]).resolve().parent)
+ if args.smoke:
+ value = manifest(root)
+ os.environ.update(environment(root,value))
+ os.environ['CUDA_VISIBLE_DEVICES']=''
+ import importlib
+ import importlib.metadata
+ packages={}
+ for name in ('torch','torchvision','torchaudio','triton','sageattention','av','numpy','safetensors','tkinter'):
+ importlib.import_module(name)
+ packages[name]='imported'
+ import torch
+ if torch.cuda.is_initialized():raise RuntimeError('Smoke check must not initialize CUDA')
+ from .paths import add_vdn
+ add_vdn()
+ from diffusers import MiniMaxH3Transformer3DModel
+ import tkinter as tk
+ from . import branding
+ window=tk.Tk();branding.theme(window);branding.wordmark(window).pack();window.update();window.destroy()
+ save(root/'engine/portable-smoke.json',dict(success=True,python=sys.version,executable=sys.executable,
+ system=sys.platform,root=str(root.resolve()),imports=packages,cuda_initialized=False,
+ scope='Native imports and Tk window only; no models or GPU generation'))
+ return 0
+ if args.initialize:
+ try:
+ initialize(root, lambda **kw: print(json.dumps(kw), flush=True))
+ except BaseException:
+ traceback.print_exc(); return 1
+ return 0
+ gui(root)
+ return 0
diff --git a/freevideo_engine/portable_launcher.py b/freevideo_engine/portable_launcher.py
new file mode 100644
index 0000000..539ca3e
--- /dev/null
+++ b/freevideo_engine/portable_launcher.py
@@ -0,0 +1,259 @@
+"""Offline bundle adapter for the shared launcher, settings and progress UI."""
+import json
+import os
+from pathlib import Path
+import subprocess
+import time
+
+from . import portable, processes
+from .comfy_launcher import Launcher
+from .comfy_launcher_runtime import Controller, local_url, matches_server, server_info
+from .monitoring import save
+
+
+class PortableController(Controller):
+ def __init__(self, root):
+ self.root = Path(root).resolve()
+ self.bundle = portable.manifest(self.root)
+ super().__init__(portable.inside(self.root, self.bundle['source']))
+ self.child = None
+ self.initialization_log = None
+ self.comfy_controller = None
+ self.fixed_environment = True
+
+ def restore(self, values):
+ # Restoring the launcher is read-only. A bundled deployment is not yet
+ # a GPU-ready machine; only initialize() may certify local kernels.
+ url = (values.get('installation') or {}).get('url') or values.get('url')
+ self.selection = dict(root=str(portable.inside(self.root, self.bundle['comfy'])),
+ engine=str(self.root/'engine'), source=str(self.source),
+ python=str(portable.inside(self.root, self.bundle['python'])),
+ url=local_url(url or 'http://127.0.0.1:8188'), ready=False, separate=False)
+ self.state = dict(status='bundled', selection=dict(self.selection))
+ self.restore_terminal(self.selection['engine'])
+ return True
+
+ def terminal_sources(self):
+ sources = super().terminal_sources()
+ if self.initialization_log:
+ sources.insert(0, ('initialize', str(self.initialization_log)))
+ if self.comfy_controller:
+ sources = [row for row in sources if row[0] != 'comfy'] + self.comfy_controller.terminal_sources()
+ return sources
+
+ def terminal_running(self, path):
+ if self.comfy_controller and path and Path(path) == self.comfy_controller.server_log:
+ return self.comfy_controller.terminal_running(path)
+ return super().terminal_running(path)
+
+ def progress(self, row):
+ stage = row.get('stage')
+ label = {'verify': 'Verify bundled files', 'gpu': 'Test GPU acceleration',
+ 'comfy': 'Start ComfyUI'}.get(stage, 'Prepare FreeVideo')
+ progress = dict(row, label=label)
+ done, total = row.get('done', 0), row.get('total')
+ fraction = min(1., max(0., done/total)) if total else 0.
+ offset = {'verify': 0, 'gpu': 1, 'comfy': 2}.get(stage, 0)
+ self.state = dict(self.state, task=dict(progress=progress),
+ overall=dict(done=offset+fraction, total=3, label=label))
+
+ def _launch(self, values):
+ self.restore(values)
+ self.state = dict(self.state, status='running', action='launch')
+ url = self.selection['url']
+ info = server_info(url)
+ if matches_server(info, self.selection['root'], self.selection['engine'], self.source):
+ self.selection['ready'] = True
+ self.ensure_shortcut()
+ self.attach_console(info)
+ self.state = dict(self.state, status='open', url=url+'/?freevideo=launch', selection=dict(self.selection))
+ return
+ log_path = self.root/'engine/portable-runs'/('%s-%s.log' % (time.time_ns(), os.getpid()))
+ log_path.parent.mkdir(parents=True, exist_ok=True)
+ self.initialization_log = log_path
+ command = [self.selection['python'], '-I', '-u', str(self.root/'start_portable.py'), '--initialize']
+ if not (self.root/'start_portable.py').is_file():
+ # Independent EXE + environment ZIP: use this launcher's materialized
+ # source, without requiring an older launcher inside the archive.
+ command = [self.selection['python'], '-I', '-u', '-c',
+ 'import sys;sys.path.insert(0,sys.argv[1]);'
+ 'from freevideo_engine.portable import main;'
+ 'sys.exit(main(sys.argv[2], ["--initialize"]))', str(self.source), str(self.root)]
+ tail = ''
+ self.progress(dict(stage='verify', done=0, total=None, detail='Check local files'))
+ try:
+ if self.cancelled.is_set():
+ raise RuntimeError('Startup cancelled; all files retained')
+ with log_path.open('w', encoding='utf-8') as log:
+ with self._server_lock:
+ if self._closed or self.cancelled.is_set():
+ raise RuntimeError('Startup cancelled; all files retained')
+ self.child = processes.popen(command, env=portable.environment(self.root, self.bundle),
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, encoding='utf-8',
+ start_new_session=True, supervise=True)
+ for line in self.child.stdout:
+ log.write(line); log.flush()
+ tail = (tail + line)[-12000:]
+ try:
+ row = json.loads(line)
+ if isinstance(row, dict) and row.get('stage'):
+ self.progress(row)
+ except ValueError:
+ pass
+ if self.cancelled.is_set():
+ processes.stop(self.child)
+ break
+ code = self.child.wait()
+ if self.cancelled.is_set():
+ raise RuntimeError('Startup cancelled; all files retained')
+ if code:
+ raise RuntimeError('Bundle startup failed (exit %s)\n%s\nLog: %s' % (code, tail, log_path))
+ finally:
+ if self.child is not None:
+ if self.child.poll() is None:
+ processes.stop(self.child)
+ if self.child.stdout:
+ self.child.stdout.close()
+ self.child = None
+ machine = json.loads((self.root/'engine/machine.json').read_text(encoding='utf-8'))
+ self.progress(dict(stage='comfy', detail='Open FreeVideo workflow'))
+ opened = portable.connect(self.root, self.bundle, machine, self.progress_keywords,
+ self.cancelled, url=url, on_controller=self.retain_comfy_controller)
+ self.selection['ready'] = True
+ self.ensure_shortcut()
+ self.state = dict(self.state, status='open', url=opened, selection=dict(self.selection),
+ overall=dict(done=3, total=3, label='Ready'))
+
+ def ensure_shortcut(self):
+ from .desktop_shortcut import create_after_install
+ self.state = dict(self.state, shortcut=create_after_install(
+ self.selection['engine'], self.source,
+ portable_root=self.root if (self.root/'FreeVideo.exe').is_file() else None))
+
+ def retain_comfy_controller(self, controller):
+ with self._server_lock:
+ if self._closed or self.cancelled.is_set():
+ controller.close()
+ raise RuntimeError('Startup cancelled; all files retained')
+ self.comfy_controller = controller
+
+ def close(self):
+ try:
+ super().close()
+ finally:
+ with self._server_lock:
+ if self.comfy_controller:
+ self.comfy_controller.close()
+ self.comfy_controller = None
+
+ def progress_keywords(self, **row):
+ self.progress(row)
+
+ def cancel(self):
+ super().cancel()
+ if self.child is not None:
+ processes.stop(self.child)
+
+
+class PortableLauncher(Launcher):
+ def __init__(self, window, root):
+ self.bundle_root = Path(root).resolve()
+ self.consent_path = self.bundle_root/'engine/portable-consent.json'
+ controller = PortableController(self.bundle_root)
+ super().__init__(window, controller.source, controller=controller)
+ self.loading_settings = True
+ for name, key in (('comfy', 'root'), ('engine', 'engine'), ('python', 'python'), ('url', 'url')):
+ getattr(self, name).set(controller.selection[key])
+ self.loading_settings = False
+ from .launcher_view import Card, Progress
+ from . import branding as b
+ self.bundle_progress = Card(self.pages[3], padding=b.space(4))
+ body = self.bundle_progress.body
+ self.ttk.Label(body, textvariable=self.phase, style='ProgressTitle.Card.TLabel', anchor='center').pack(fill='x')
+ self.overall_bar = Progress(body); self.overall_bar.pack(fill='x', pady=b.space(2))
+ self.ttk.Label(body, textvariable=self.overall_text, style='Meta.Card.TLabel', anchor='center').pack(fill='x')
+ self.bar = Progress(body, subdued=True); self.bar.pack(fill='x', pady=b.space(2))
+ self.ttk.Label(body, textvariable=self.details, style='Meta.Card.TLabel', wraplength=530).pack(fill='x')
+ self.ttk.Label(body, textvariable=self.current_text, style='Meta.Card.TLabel').pack(fill='x')
+ self.bundle_note = self.ttk.Label(self.pages[3], style='Meta.TLabel', wraplength=600, anchor='center')
+ self.bundle_note.pack(fill='x')
+ self.refresh()
+
+ def changed(self, *_):
+ if self.loading_settings:
+ return
+ self.queue_save()
+ if not self.controller.busy:
+ self.controller.restore({'url': self.url.get()})
+ self.launch_selection = dict(self.controller.selection)
+ self.reset_browser()
+ self.refresh()
+
+ def refresh(self):
+ self.wizard_step = 3
+ super().refresh()
+ self.back_button.configure(text=self.t('Settings', '设置'))
+ if not self.consent_path.exists() and not self.controller.busy:
+ self.primary.configure(text=self.t('Accept licenses & start', '同意许可证并启动'))
+ if hasattr(self, 'bundle_progress'):
+ if self.controller.busy:
+ self.bundle_progress.pack(fill='x', before=self.bundle_note)
+ else:
+ self.bundle_progress.pack_forget()
+ note = self.t('Python, ComfyUI and models are included.', '已包含 Python、ComfyUI 和模型。')
+ if not self.consent_path.exists():
+ note += '\n' + self.t('Licenses: models/edge/LICENSE', '许可证:models/edge/LICENSE')
+ self.bundle_note.configure(text=note)
+
+ def act(self):
+ if self.controller.busy:
+ return
+ if not self.consent_path.exists():
+ save(self.consent_path, dict(source_commit=self.controller.bundle['source_commit'], accepted_at=time.time()))
+ self.started = time.monotonic()
+ super().act()
+
+ def back(self):
+ self.show_options()
+
+
+
+def gui(root):
+ from .modern_launcher import main
+ main(portable_session(root))
+
+
+def portable_session(root):
+ from .launcher_session import Session
+ root = Path(root).resolve()
+ os.environ.update(portable.environment(root, portable.manifest(root)))
+ os.environ['FREEVIDEO_LAUNCHER_HOME'] = str(root/'engine/launcher')
+ class PortableSession(Session):
+ def __init__(self):
+ controller = PortableController(root)
+ self.consent_path = root/'engine/portable-consent.json'
+ super().__init__(controller.source, controller=controller, smoke=True)
+ self.smoke = False
+ self.page = 'launcher'
+
+ def edit(self, key, value):
+ if key == 'url' and not self.controller.busy:
+ value = local_url(value)
+ self.controller.restore({'url': value})
+ self.remember(self.controller.selection)
+ self.browser_attempted = False
+ elif key in ('language', 'token'):
+ super().edit(key, value)
+
+ def action(self, name, accepted=False):
+ if name in ('primary', 'launch') and not self.controller.busy:
+ if not self.consent_path.exists():
+ if not accepted:
+ raise ValueError(self.t('Accept the included licenses to continue.', '请先同意随包附带的许可证。'))
+ save(self.consent_path, dict(source_commit=self.controller.bundle['source_commit'], accepted_at=time.time()))
+ if name != 'setup':
+ super().action(name, accepted)
+
+ def snapshot(self):
+ return dict(super().snapshot(), portable=True, needs_consent=not self.consent_path.exists())
+ return PortableSession()
diff --git a/freevideo_engine/portable_parts.py b/freevideo_engine/portable_parts.py
new file mode 100644
index 0000000..9947909
--- /dev/null
+++ b/freevideo_engine/portable_parts.py
@@ -0,0 +1,52 @@
+"""Join independently distributed model and runtime manifests before startup."""
+import hashlib
+import json
+from pathlib import Path
+
+from .monitoring import save
+from .portable import inside
+
+
+def assemble_manifest(root):
+ """No imports of models, GPU queries, downloads, or changes to existing installs."""
+ root = Path(root).resolve()
+ runtime = json.loads(inside(root, 'runtime.json').read_text(encoding='utf-8'))
+ if runtime.get('schema_version') != 1:
+ raise ValueError('Unsupported FreeVideo runtime package')
+ marker = inside(root, 'models/model-pack.json')
+ if not marker.is_file():
+ raise ValueError('尚未放入模型包。请将对应显卡的纯模型包解压到同一位置,'
+ '让 models、python、ComfyUI 位于同一个 FreeVideo-Windows 文件夹内。')
+ raw = marker.read_bytes()
+ model = json.loads(raw)
+ variant = model.get('variant')
+ digest = hashlib.sha256(raw).hexdigest()
+ if (model.get('schema_version') != 1 or variant not in ('rowwise', 'per_tensor')
+ or runtime.get('model_packs', {}).get(variant) != digest):
+ raise ValueError('模型包与运行环境包不匹配。请使用同一批发布的配套包。')
+ rows = runtime['files'] + model['files'] + [dict(path='models/model-pack.json',
+ bytes=len(raw), sha256=digest)]
+ seen = set()
+ for row in rows:
+ inside(root, row['path'])
+ if (row['path'].casefold() in seen or type(row['bytes']) is not int or row['bytes'] < 0
+ or len(row['sha256']) != 64
+ or any(c not in '0123456789abcdef' for c in row['sha256'])):
+ raise ValueError('Invalid component inventory')
+ seen.add(row['path'].casefold())
+ if any(not r['path'].startswith('models/') for r in model['files']):
+ raise ValueError('Model package contains non-model paths')
+ if any(r['path'].startswith('models/') for r in runtime['files']):
+ raise ValueError('Runtime package contains model paths')
+ value = dict(runtime['configuration'], variant=variant, files=rows,
+ cache='models/edge/' + ('rowwise/cache' if variant == 'rowwise' else 'cache'),
+ component_model_sha256=digest)
+ for key in ('python', 'source', 'comfy', 'vdn', 'model_root', 'encoder_root', 'cache'):
+ inside(root, value[key])
+ destination = inside(root, 'portable.json')
+ if destination.exists():
+ if json.loads(destination.read_text(encoding='utf-8')) != value:
+ raise ValueError('此文件夹已有另一版本的整合包。请在新文件夹中解压配套包,现有文件已保留。')
+ else:
+ save(destination, value)
+ return value
diff --git a/freevideo_engine/prediction.py b/freevideo_engine/prediction.py
new file mode 100644
index 0000000..90a6d18
--- /dev/null
+++ b/freevideo_engine/prediction.py
@@ -0,0 +1,532 @@
+"""Small local, torch-free forecasts from verified complete requests.
+
+Predictions are measurements generalized to another input, not permission to
+change arithmetic or evidence that a workload fits. Identity and actual placement
+must match. Repeated requests at one geometry count as one geometry for model
+selection, including its leave-one-geometry-out validation.
+"""
+import hashlib
+import json
+import math
+from statistics import median
+
+from .geometry import geometry as resolve_geometry
+from .execution_config import engine_options, decoder_options
+
+
+STAGES = ('load_seconds', 'sample_seconds', 'vae_load_seconds', 'video_decode_seconds',
+ 'audio_load_decode_seconds', 'encode_seconds', 'decode_save_seconds', 'work_seconds',
+ 'text_encode_seconds')
+RESOURCES = ('peak_reserved_bytes', 'whole_gpu_peak_bytes', 'ram_peak_bytes')
+_ENGINE_METADATA = frozenset(('base', 'checkpoint', 'cache', 'source_id', 'fa4_dependency',
+ 'resident_weight_bytes', 'pinned_model_bytes', 'pinned_host_allocated_bytes', 'fp8_linears',
+ 'adaln_table_cache_hits', 'reuse_block_outputs', 'name', 'description'))
+_BUDGETS = frozenset(('gpu_budget_bytes', 'ram_budget_bytes', 'gpu_budget_gb',
+ 'inference_ram_budget_gb', 'gpu_reserve_gib', 'ram_reserve_gib', 'policy', 'evidence'))
+_CACHE_SENSITIVE_STAGES = ('load_seconds', 'work_seconds')
+
+
+def _allocator_limit(value):
+ if value is None:
+ return None
+ if type(value) is not int or value <= 0:
+ raise ValueError('Benchmark allocator limit must be positive integer bytes, or None')
+ return value
+
+
+def _cache_state(context):
+ if not isinstance(context, dict):
+ return 'unknown'
+ explicit = context.get('adaln_cache_state')
+ if explicit in ('cold', 'warm', 'partial'):
+ return explicit
+ hits = context.get('adaln_table_cache_hits')
+ if type(hits) is int and 0 <= hits <= 50:
+ return 'cold' if hits == 0 else 'warm' if hits == 50 else 'partial'
+ return 'unknown'
+
+
+def profile_options(profile):
+ """Executable profile identity, retaining placement and unknown compute knobs."""
+ if not isinstance(profile, dict):
+ raise ValueError('Forecast profile must be a mapping')
+ wrapped = 'engine' in profile or 'decoder' in profile
+ engine = profile.get('engine', {}) if wrapped else profile
+ decoder = profile.get('decoder', {}) if wrapped else {}
+ if not isinstance(engine, dict) or not isinstance(decoder, dict):
+ raise ValueError('Forecast engine/decoder must be mappings')
+ engine = {key: value for key, value in engine_options(engine).items()
+ if key not in _ENGINE_METADATA and key not in _BUDGETS
+ and key not in ('allocator_config', 'benchmark_allocator_limit_bytes')}
+ return {'engine': engine, 'decoder': decoder_options(decoder),
+ 'benchmark_allocator_limit_bytes': _allocator_limit(profile.get('benchmark_allocator_limit_bytes')),
+ 'allocator_config': profile.get('allocator_config',
+ profile.get('engine', {}).get('allocator_config') if wrapped else None)}
+
+
+def profile_key(profile):
+ return hashlib.sha256(json.dumps(profile_options(profile), sort_keys=True,
+ separators=(',', ':'), allow_nan=False).encode()).hexdigest()
+
+
+def _finite(value, positive=False):
+ try:
+ return type(value) in (int, float) and math.isfinite(value) and (value > 0 if positive else value >= 0)
+ except (OverflowError, ValueError):
+ return False
+
+
+def _canvas(value, options, text_tokens=None):
+ width, height = value.get('width', 1344), value.get('height', 768)
+ canvas = resolve_geometry(width, height, frames=value.get('frames'),
+ seconds=value.get('seconds') if value.get('frames') is None else None)
+ canvas['steps'] = options['engine']['steps']
+ canvas['task'] = options['engine']['task']
+ if value.get('sampling_plan', {}).get('enabled'):
+ canvas['sampling_plan'] = value['sampling_plan']
+ if type(canvas['steps']) is not int or canvas['steps'] <= 0:
+ raise ValueError('Forecast steps must be a positive integer')
+ from .media_request import TASKS
+ if canvas['task'] not in TASKS:
+ raise ValueError('Unknown forecast task')
+ for key in ('steps', 'task'):
+ if key in value and value[key] != canvas[key]:
+ raise ValueError('Forecast geometry and engine disagree on ' + key)
+ text = text_tokens if text_tokens is not None else value.get('text_tokens')
+ if text is not None and (type(text) is not int or text < 0):
+ raise ValueError('Forecast text token count must be a nonnegative integer')
+ canvas['text_tokens'] = text
+ for name in ('reference_video_tokens', 'reference_audio_tokens'):
+ count = value.get(name, 0)
+ if type(count) is not int or count < 0:
+ raise ValueError('Invalid reference token count')
+ canvas[name] = count
+ return canvas
+
+
+def _features(canvas, assumed_text):
+ p = (canvas['height'] // 32) * (canvas['width'] // 32)
+ l = canvas['latent_frames']
+ n = p * l
+ text = canvas['text_tokens'] if canvas['text_tokens'] is not None else assumed_text
+ # These are regression features, not claimed exact kernel FLOP counts.
+ # Video windows are linear in sequence length at fixed spatial/window size;
+ # their per-frame work grows with spatial tokens. Global-query work can be
+ # quadratic; text length and audio duration are represented independently.
+ global_queries = text + canvas['frames'] + canvas.get('reference_video_tokens', 0) + canvas.get('reference_audio_tokens', 0)
+ return {'constant': 1., 'tokens': n / 72576., 'quadratic': (n / 72576.) ** 2,
+ 'window': n * p * min(l, 15) / (72576. * 1008 * 15),
+ 'global': global_queries * (n + global_queries) / (610. * (72576 + 610)),
+ 'pixels': canvas['width'] * canvas['height'] * canvas['frames'] / (1344. * 768 * 243),
+ 'spatial': p / 1008., 'length': canvas['frames'] / 243.,
+ 'text': ((text + 1) / 368.) ** 2,
+ 'text_length': text + 1., 'latent_length': l,
+ 'reference_rows': 1. + canvas.get('reference_video_tokens', 0) + canvas.get('reference_audio_tokens', 0)}
+
+
+def _geometry_key(canvas, assumed_text):
+ return (canvas['width'], canvas['height'], canvas['frames'],
+ canvas['text_tokens'] if canvas['text_tokens'] is not None else assumed_text,
+ canvas.get('reference_video_tokens', 0), canvas.get('reference_audio_tokens', 0))
+
+
+def _distance(a, b):
+ return sum(abs(math.log(max(a[key], 1e-9) / max(b[key], 1e-9)))
+ for key in ('spatial', 'latent_length', 'text_length', 'reference_rows'))
+
+
+def _solve(matrix, target):
+ size = len(target)
+ rows = [list(row) + [target[index]] for index, row in enumerate(matrix)]
+ for col in range(size):
+ pivot = max(range(col, size), key=lambda index: abs(rows[index][col]))
+ if abs(rows[pivot][col]) < 1e-10:
+ return None
+ rows[col], rows[pivot] = rows[pivot], rows[col]
+ scale = rows[col][col]
+ rows[col] = [value / scale for value in rows[col]]
+ for index in range(size):
+ if index == col:
+ continue
+ scale = rows[index][col]
+ rows[index] = [a - scale * b for a, b in zip(rows[index], rows[col])]
+ answer = [row[-1] for row in rows]
+ return answer if all(math.isfinite(value) for value in answer) else None
+
+
+def _fit(points, names):
+ # Three small Huber reweighting rounds reduce one unusual cold/thermal run's
+ # influence. Group medians already prevent identical repeats dominating fit.
+ weights = [1.] * len(points)
+ coefficients = None
+ for _ in range(3):
+ matrix = [[sum(weight * point['features'][a] * point['features'][b]
+ for point, weight in zip(points, weights))
+ for b in names] for a in names]
+ target = [sum(weight * point['features'][a] * point['value']
+ for point, weight in zip(points, weights)) for a in names]
+ coefficients = _solve(matrix, target)
+ if coefficients is None:
+ return None
+ residuals = [abs(point['value'] - _evaluate(coefficients, names, point['features'])) for point in points]
+ scale = max(median(residuals) * 1.5, median(point['value'] for point in points) * .005, 1e-9)
+ weights = [min(1., scale / max(error, 1e-9)) for error in residuals]
+ return coefficients
+
+
+def _evaluate(coefficients, names, features):
+ return sum(coefficient * features[name] for coefficient, name in zip(coefficients, names))
+
+
+def _bases(name):
+ if name in ('sample_seconds', 'work_seconds'):
+ return [('constant', 'tokens'), ('constant', 'tokens', 'window'),
+ ('constant', 'tokens', 'quadratic'), ('constant', 'tokens', 'window', 'global'),
+ ('constant', 'tokens', 'quadratic', 'global')]
+ if name in RESOURCES:
+ return [('constant', 'tokens'), ('constant', 'spatial'),
+ ('constant', 'tokens', 'spatial'), ('constant', 'tokens', 'length'),
+ ('constant', 'tokens', 'global')]
+ if name == 'text_encode_seconds':
+ return [('constant', 'text'), ('constant', 'text', 'spatial')]
+ if name in ('load_seconds', 'vae_load_seconds'):
+ return [('constant',)]
+ if name == 'audio_load_decode_seconds':
+ return [('constant', 'length')]
+ return [('constant', 'pixels'), ('constant', 'pixels', 'spatial')]
+
+
+def _validated_model(points, name):
+ if len(points) < 6:
+ return None
+ candidates = []
+ for names in _bases(name):
+ if len(points) < len(names) + 3:
+ continue
+ errors = []
+ for index, held_out in enumerate(points):
+ trained = _fit(points[:index] + points[index + 1:], names)
+ if trained is None:
+ break
+ forecast = _evaluate(trained, names, held_out['features'])
+ if not _finite(forecast):
+ break
+ errors.append(abs(forecast - held_out['value']) / max(held_out['value'], 1e-9))
+ if len(errors) != len(points):
+ continue
+ middle, worst = median(errors), max(errors)
+ if middle > .3 or worst > .75:
+ continue
+ coefficients = _fit(points, names)
+ if coefficients is not None:
+ candidates.append({'names': names, 'coefficients': coefficients,
+ 'median_error': middle, 'max_error': worst,
+ 'score': middle + .15 * worst + .003 * len(names)})
+ return min(candidates, key=lambda item: item['score']) if candidates else None
+
+
+def _sparse_scale(name, source, target):
+ ratio = lambda key: target[key] / max(source[key], 1e-9)
+ if name in ('load_seconds', 'vae_load_seconds'):
+ choices = [1.]
+ elif name in RESOURCES:
+ choices = [1., ratio('tokens'), ratio('spatial'), ratio('global'), ratio('text_length')]
+ return .6 + .3 * ratio('tokens') + .1 * ratio('text_length'), min(choices), max(choices)
+ elif name in ('sample_seconds', 'work_seconds'):
+ choices = [ratio('tokens'), ratio('window'), ratio('quadratic'), ratio('global')]
+ center = .45 * choices[0] + .4 * choices[1] + .15 * choices[3]
+ return center, min(choices), max(choices)
+ elif name == 'text_encode_seconds':
+ choices = [1., ratio('text')]
+ elif name == 'audio_load_decode_seconds':
+ choices = [1., ratio('length')]
+ elif name == 'unattributed_seconds':
+ choices = [1., ratio('pixels')]
+ else:
+ choices = [ratio('pixels')]
+ return sum(choices) / len(choices), min(choices), max(choices)
+
+
+def _unknown(unit, reason):
+ return {'estimate': None, 'lower': None, 'upper': None, 'unit': unit,
+ 'confidence': 'unknown', 'method': 'unavailable', 'evidence_count': 0,
+ 'geometry_count': 0, 'reason': reason}
+
+
+def _predict_metric(records, target, name, assumed_text, unknown_text):
+ unit = 'bytes' if name in RESOURCES else 'seconds'
+ grouped = {}
+ for record in records:
+ observation = record['observation']
+ stages = observation.get('stage_seconds')
+ value = observation.get(name) if name in RESOURCES else (stages.get(name) if isinstance(stages, dict) else None)
+ if not _finite(value, positive=name in RESOURCES):
+ continue
+ key = _geometry_key(record['canvas'], assumed_text)
+ point = grouped.setdefault(key, {'features': _features(record['canvas'], assumed_text),
+ 'values': [], 'ids': []})
+ point['values'].append(float(value))
+ point['ids'].append(record['id'])
+ points = list(grouped.values())
+ if not points:
+ return _unknown(unit, 'No matching complete local request supplies this metric')
+ for point in points:
+ point['value'] = median(point['values'])
+ features = _features(target, assumed_text)
+ exact = grouped.get(_geometry_key(target, assumed_text))
+ nearest = min(points, key=lambda point: _distance(point['features'], features))
+ distance = _distance(nearest['features'], features)
+ outside = any(features[key] < min(point['features'][key] for point in points) - 1e-9
+ or features[key] > max(point['features'][key] for point in points) + 1e-9
+ for key in ('spatial', 'latent_length', 'text_length', 'reference_rows'))
+ extrapolation = outside or distance > .6
+ model_points = sorted(points, key=lambda point: _distance(point['features'], features))[:24]
+ model = None if exact else _validated_model(model_points, name)
+ validation = None
+ weak_fit = None
+ ids = []
+ if exact:
+ center = exact['value']
+ spread = max(abs(value - center) for value in exact['values']) / max(center, 1e-9)
+ fraction = max(.15 if len(exact['values']) >= 3 else .35, spread)
+ low, high = center * (1 - fraction), center * (1 + fraction)
+ method = 'same_geometry_observations'
+ confidence = 'moderate' if len(exact['values']) >= 3 and spread <= .25 else 'weak'
+ ids = exact['ids']
+ elif model:
+ center = _evaluate(model['coefficients'], model['names'], features)
+ if not _finite(center):
+ model = None
+ else:
+ scaled, _, _ = _sparse_scale(name, nearest['features'], features)
+ # Blend only well-supported interpolation. Large extrapolation is
+ # left explicitly weak; it never becomes capacity certification.
+ center = .9 * center + .1 * nearest['value'] * scaled
+ observed_spread = max(abs(value - point['value']) / max(point['value'], 1e-9)
+ for point in model_points for value in point['values'])
+ fraction = max(.2, 1.5 * model['max_error'], observed_spread) + (.5 + .3 * distance if extrapolation else 0)
+ low, high = center * (1 - fraction), center * (1 + fraction)
+ method = 'geometry_model_with_neighbor_blend'
+ confidence = 'weak' if extrapolation or observed_spread > .3 else ('strong' if len(model_points) >= 8 and model['max_error'] < .1 else 'moderate')
+ validation = {'method': 'leave_one_geometry_out', 'geometry_count': len(model_points),
+ 'median_relative_error': model['median_error'], 'max_relative_error': model['max_error'],
+ 'features': list(model['names'])}
+ ids = [identifier for point in model_points for identifier in point['ids']]
+ if not exact and model is None:
+ scaled, least, most = _sparse_scale(name, nearest['features'], features)
+ center = nearest['value'] * scaled
+ uncertainty = .4 + min(1.5, distance * .5)
+ low = min(nearest['values']) * least * max(0., 1 - uncertainty)
+ high = max(nearest['values']) * most * (1 + uncertainty)
+ method, confidence, ids = 'sparse_geometry_scaling', 'weak', nearest['ids']
+ # Two complete geometries can expose a fixed sampling cost that a
+ # zero-intercept FLOP ratio misses. This is an unvalidated hypothesis,
+ # not a substitute for the held-out model above. Keep both the broad
+ # geometry-feature envelope and empirical variation in its interval.
+ if name == 'sample_seconds' and len({point['features']['tokens'] for point in model_points}) >= 2:
+ names = ('constant', 'tokens')
+ fitted = _fit(model_points, names)
+ if fitted is not None and fitted[0] >= 0 and fitted[1] > 0:
+ empirical = _evaluate(fitted, names, features)
+ if _finite(empirical):
+ spread = max(abs(value - _evaluate(fitted, names, point['features'])) /
+ max(point['value'], 1e-9)
+ for point in model_points for value in point['values'])
+ fraction = max(.5, spread) + (.25 * distance if extrapolation else 0.)
+ low, high = min(low, empirical * max(0., 1 - fraction)), max(high, empirical * (1 + fraction))
+ center, method = empirical, 'weak_affine_token_model'
+ ids = [identifier for point in model_points for identifier in point['ids']]
+ weak_fit = {'features': list(names), 'coefficients': fitted,
+ 'geometry_count': len(model_points), 'held_out_validated': False,
+ 'assumption': 'Nonnegative fixed sampling cost plus positive cost per video token; spatial/window/input ambiguity remains in the wide range'}
+ if unknown_text and name in ('sample_seconds', 'work_seconds', 'text_encode_seconds') + RESOURCES:
+ confidence = 'weak'
+ low, high = min(low, center * .5), max(high, center * 1.5)
+ result = {'estimate': max(0., center), 'lower': max(0., min(low, center)),
+ 'upper': max(high, center), 'unit': unit, 'method': method,
+ 'confidence': confidence, 'evidence_count': sum(len(point['values']) for point in points),
+ 'geometry_count': len(points), 'extrapolation': extrapolation,
+ 'evidence_ids': ids, 'validation': validation}
+ if weak_fit is not None:
+ result['weak_fit'] = weak_fit
+ if unit == 'bytes':
+ for key in ('estimate', 'lower', 'upper'):
+ result[key] = round(result[key])
+ return result
+
+
+def _work_from_phases(records, target, assumed_text, unknown_text):
+ """A sparse full-worker estimate must not scale fixed loading by geometry.
+
+ Use only rows with all disjoint phases and a nonnegative measured remainder.
+ The remainder includes retained-artifact I/O and cleanup; it is not secretly
+ treated as sampling. Summed bounds are empirical uncertainty, not a coverage
+ guarantee, and preserve cold/thermal variation in the component models.
+ """
+ names = ('load_seconds', 'sample_seconds', 'decode_save_seconds')
+ complete = []
+ for record in records:
+ stages = record['observation'].get('stage_seconds')
+ if not isinstance(stages, dict) or not all(_finite(stages.get(name)) for name in names + ('work_seconds',)):
+ continue
+ accounted = sum(stages[name] for name in names)
+ residual = stages['work_seconds'] - accounted
+ if residual < -max(1e-6, accounted * 1e-8):
+ continue
+ observation = dict(record['observation'], stage_seconds=dict(stages, unattributed_seconds=max(0., residual)))
+ complete.append(dict(record, observation=observation))
+ if not complete:
+ return _unknown('seconds', 'Sparse worker forecast needs measured load, sampling, decode/save and total time from the same complete requests')
+ components = {name: _predict_metric(complete, target, name, assumed_text, unknown_text)
+ for name in names + ('unattributed_seconds',)}
+ if any(value['estimate'] is None for value in components.values()):
+ return _unknown('seconds', 'A required worker phase cannot be estimated')
+ rank = {'unknown': 0, 'weak': 1, 'moderate': 2, 'strong': 3}
+ result = {key: sum(value[key] for value in components.values()) for key in ('estimate', 'lower', 'upper')}
+ result.update(unit='seconds', method='phase_sum_with_observed_overhead',
+ confidence=min((value['confidence'] for value in components.values()), key=rank.__getitem__),
+ evidence_count=len(complete), geometry_count=len({_geometry_key(row['canvas'], assumed_text) for row in complete}),
+ evidence_ids=list(dict.fromkeys(identifier for value in components.values() for identifier in value['evidence_ids'])),
+ extrapolation=any(value['extrapolation'] for value in components.values()), validation=None,
+ components=components,
+ reason='Sum independently forecast load, sampling, decode/save and measured residual; fixed model loading is not scaled by video size')
+ return result
+
+
+def predict(observations, identity, profile, geometry, *, text_tokens=None, execution_context=None):
+ """Forecast this exact configuration on an unseen canvas/input using local history.
+
+ Timings are the named stored phases: encode_seconds is media encoding, not
+ text encoding. work_seconds is the video worker, not the complete text-to-MP4
+ request. Missing text-encoder data stays unknown. Returned ranges describe
+ empirical/model uncertainty, not statistical coverage guarantees.
+ """
+ options = profile_options(profile)
+ target = _canvas(geometry, options, text_tokens)
+ cache_state = _cache_state(execution_context)
+ if execution_context is not None and not isinstance(execution_context, dict):
+ raise ValueError('Forecast execution context must be a mapping')
+ if execution_context and 'allocator_limit_bytes' in execution_context:
+ if _allocator_limit(execution_context['allocator_limit_bytes']) != options['benchmark_allocator_limit_bytes']:
+ raise ValueError('Forecast execution context and profile disagree on allocator limit')
+ key = profile_key(profile)
+ records, rejected = [], {}
+ def reject(reason):
+ rejected[reason] = rejected.get(reason, 0) + 1
+ for index, row in enumerate(observations):
+ try:
+ if row.get('identity') != identity:
+ reject('different_local_hardware_or_software')
+ continue
+ observation = row.get('observation')
+ observed_context = observation.get('execution_context', {}) if isinstance(observation, dict) else {}
+ if not isinstance(observed_context, dict):
+ reject('malformed_execution_context')
+ continue
+ recorded_profile = dict(row.get('config', {}))
+ if 'allocator_limit_bytes' in observed_context:
+ cap = _allocator_limit(observed_context['allocator_limit_bytes'])
+ if recorded_profile.get('benchmark_allocator_limit_bytes') not in (None, cap):
+ reject('inconsistent_allocator_cap_evidence')
+ continue
+ recorded_profile['benchmark_allocator_limit_bytes'] = cap
+ if profile_key(recorded_profile) != key:
+ reject('different_execution_configuration')
+ continue
+ if (row.get('outcome', row.get('state')) != 'success' or not isinstance(observation, dict)
+ or row.get('purpose') not in ('generation', 'validation', 'full_request')
+ or any(observation.get(flag) is not True for flag in
+ ('full_request', 'validated', 'media_verified', 'metrics_complete'))):
+ reject('not_a_verified_complete_request')
+ continue
+ original = row.get('geometry', {})
+ enriched = dict(original)
+ enriched.update(observation.get('geometry', {}))
+ if any(type(enriched.get(key)) is not int or enriched[key] <= 0 for key in ('width', 'height', 'frames')):
+ reject('missing_observed_geometry')
+ continue
+ if any(key in original and key in enriched and original[key] != enriched[key]
+ for key in ('width', 'height', 'frames', 'steps', 'task', 'reference_video_tokens', 'reference_audio_tokens')):
+ reject('inconsistent_observed_geometry')
+ continue
+ canvas = _canvas(enriched, options, observation.get('text_tokens'))
+ from .two_pass import same_strategy, steps as sampling_steps
+ if not same_strategy(canvas, target):
+ reject('different_sampling_plan')
+ continue
+ total_steps = sampling_steps(canvas['sampling_plan']) if canvas.get('sampling_plan') else canvas['steps']
+ times = observation.get('step_seconds')
+ if (observation.get('completed_frames') != canvas['frames']
+ or observation.get('completed_steps') != total_steps
+ or not isinstance(times, list) or len(times) != total_steps
+ or not all(_finite(value, positive=True) for value in times)):
+ reject('incomplete_requested_steps_or_frames')
+ continue
+ records.append({'id': row.get('id', 'record-' + str(index)), 'canvas': canvas,
+ 'observation': observation, 'cache_state': _cache_state(observed_context),
+ 'host_threads': observed_context.get('host_threads')})
+ except (TypeError, ValueError, AttributeError, KeyError, OverflowError):
+ reject('malformed_record')
+ known_text = [record['canvas']['text_tokens'] for record in records if record['canvas']['text_tokens'] is not None]
+ assumed_text = round(median(known_text)) if known_text else 367
+ unknown_text = target['text_tokens'] is None
+ stages = {}
+ regimes = {}
+ for record in records:
+ regimes[record['cache_state']] = regimes.get(record['cache_state'], 0) + 1
+ thread_contexts = []
+ for record in records:
+ context = record['host_threads']
+ if context not in thread_contexts:
+ thread_contexts.append(context)
+ for name in STAGES:
+ same_context = (name in _CACHE_SENSITIVE_STAGES and cache_state != 'unknown')
+ relevant = [record for record in records if record['cache_state'] == cache_state] if same_context else records
+ value = _predict_metric(relevant, target, name, assumed_text, unknown_text)
+ if name == 'work_seconds' and value['method'] == 'sparse_geometry_scaling':
+ value = _work_from_phases(relevant, target, assumed_text, unknown_text)
+ if name in _CACHE_SENSITIVE_STAGES:
+ value['cache_context_matched'] = same_context and value['estimate'] is not None
+ value['cache_state'] = cache_state
+ if same_context and value['estimate'] is None:
+ value['reason'] = 'No complete local observation matches the requested %s AdaLN cache regime' % cache_state
+ stages[name] = value
+ # Different platform counters must never form one RAM response variable.
+ ram_metrics = {record['observation'].get('ram_metric')
+ if isinstance(record['observation'].get('ram_metric'), str) else None
+ for record in records if _finite(record['observation'].get('ram_peak_bytes'), positive=True)}
+ resources = {name: _predict_metric(records, target, name, assumed_text, unknown_text) for name in RESOURCES}
+ if len(ram_metrics) != 1 or next(iter(ram_metrics), None) in (None, '', 'unknown'):
+ resources['ram_peak_bytes'] = _unknown('bytes', 'No single explicit platform RAM metric is available')
+ else:
+ resources['ram_peak_bytes']['ram_metric'] = next(iter(ram_metrics))
+ evidence = [item for item in list(stages.values()) + list(resources.values()) if item['estimate'] is not None]
+ ranks = {'unknown': 0, 'weak': 1, 'moderate': 2, 'strong': 3}
+ confidence = min((item['confidence'] for item in evidence), key=lambda level: ranks[level]) if evidence else 'unknown'
+ reasons = ['Only verified complete requests with the same local hardware/software, backend, task, arithmetic and placement are used.',
+ 'Timing ranges include historical cache/thermal variability; cache warmth is not certified.',
+ 'Ranges are forecasts, not allocator limits, quality validation or a proof of capacity.',
+ 'work_seconds excludes text encoding; encode_seconds is media encoding. Missing text-encoder time remains unknown.']
+ if cache_state == 'unknown':
+ reasons.append('AdaLN cache state was not supplied: load/work forecasts retain the observed cache-regime mixture and do not establish a warm-start estimate.')
+ else:
+ reasons.append('Load/work timings use only the explicitly requested %s AdaLN cache regime; absence remains unknown.' % cache_state)
+ reasons.append('Host CPU thread/environment settings are measured context, not established cache effects; unknown or mixed thread settings limit load/work comparisons.')
+ if options['benchmark_allocator_limit_bytes'] is not None:
+ reasons.append('Only observations with this explicit benchmark allocator cap are eligible; uncapped or unrecorded-cap data are excluded.')
+ if unknown_text:
+ reasons.append('Input token length is not known yet; timing features assume %d text tokens from %s.' %
+ (assumed_text, 'matching local history' if known_text else 'an unmeasured reference shape'))
+ if not records:
+ reasons.append('No compatible complete local observations; run a full request to create the first data point.')
+ return {'status': 'estimated' if evidence else 'unknown', 'confidence': confidence,
+ 'evidence_count': len(records), 'geometry': target, 'profile_key': key,
+ 'extrapolation': any(item.get('extrapolation', False) for item in evidence),
+ 'input_token_assumption': assumed_text if unknown_text else None,
+ 'execution_context': {'adaln_cache_state': cache_state,
+ 'allocator_limit_bytes': options['benchmark_allocator_limit_bytes'],
+ 'observed_cache_regimes': regimes,
+ 'observed_host_threads': thread_contexts},
+ 'stages': stages, 'resources': resources, 'rejected_records': rejected,
+ 'reasons': reasons, 'capacity_certified': False, 'numerical_equivalence_certified': False,
+ 'scope': 'local_complete_video_worker_forecast'}
diff --git a/freevideo_engine/prediction_cli.py b/freevideo_engine/prediction_cli.py
new file mode 100644
index 0000000..7eb8a36
--- /dev/null
+++ b/freevideo_engine/prediction_cli.py
@@ -0,0 +1,206 @@
+"""Preview local runtime/resource forecasts without starting inference or probes.
+
+The parent stays torch-free. Hardware detection uses the existing short-lived
+read-only detector; this command never runs model workers or kernel probes.
+"""
+import argparse
+import copy
+import json
+import os
+from pathlib import Path
+import subprocess
+import sys
+
+from .adaptive import attempt_geometry, benchmark_allocator_limit, history_path, local_identity
+from .geometry import geometry
+from .hardware import detect
+from .kernel_capabilities import available_backends
+from .paths import data_root
+from .policy import choose
+from .prediction import predict
+from .prediction_policy import select
+from .prediction_ui import show
+from .resource_history import ResourceHistory, ResourceHistoryError
+from .tuning import digest
+from . import tuning
+
+
+def _emit(value, stream):
+ output = json.dumps(value, ensure_ascii=False, indent=2, allow_nan=False)
+ encoding = getattr(stream, 'encoding', None)
+ if encoding:
+ try:
+ output.encode(encoding)
+ except UnicodeEncodeError:
+ output = json.dumps(value, ensure_ascii=True, indent=2, allow_nan=False)
+ print(output, file=stream)
+
+
+def _selected_cache(explicit):
+ if explicit is not None:
+ return explicit.expanduser()
+ machine = data_root() / 'machine.json'
+ if not machine.is_file():
+ return None
+ value = json.loads(machine.read_text(encoding='utf-8'))
+ return Path(value['cache']).expanduser() if value.get('cache') else None
+
+
+def _conditioning(path, observations):
+ if path is None:
+ return {'sha256': None, 'text_tokens': None, 'source': 'unknown input token length'}
+ checksum = digest(path.expanduser())
+ counts = set()
+ for row in observations:
+ observed = row.get('observation') or {}
+ if observed.get('conditioning_sha256') != checksum:
+ continue
+ count = observed.get('text_tokens')
+ if count is None:
+ count = (observed.get('geometry') or {}).get('text_tokens')
+ if count is None:
+ count = (row.get('geometry') or {}).get('text_tokens')
+ if type(count) is int and count >= 0:
+ counts.add(count)
+ count = next(iter(counts)) if len(counts) == 1 else None
+ source = ('matching conditioning SHA256 in complete local history' if count is not None
+ else 'conflicting historical token lengths' if len(counts) > 1
+ else 'conditioning SHA256 has no matching token observation')
+ return {'sha256': checksum, 'text_tokens': count, 'source': source}
+
+
+def _tuned_profile(profile, cache, hardware, canvas, tokens, enabled):
+ decision = {'profile_id': None, 'input_tokens_known': tokens is not None}
+ if not enabled:
+ decision['reason'] = 'Saved tuning bypassed by explicit profile, backend or --no-tuning'
+ return profile, decision
+ if tokens is None:
+ decision['reason'] = ('Initial automatic policy: input token length is unknown. '
+ 'Generation may apply validated tuning after text encoding.')
+ return profile, decision
+ if cache is None or not tuning.state_path().is_file():
+ decision['reason'] = 'No saved local tuning available for this preview'
+ return profile, decision
+ try:
+ machine = json.loads((data_root() / 'machine.json').read_text(encoding='utf-8'))
+ if cache.resolve() != Path(machine['cache']).resolve():
+ decision['reason'] = 'Prepared cache differs from the optimized installation'
+ return profile, decision
+ identity = tuning.identity(machine, hardware)
+ state, reason = tuning.load_state(identity)
+ if state:
+ profile, applied = tuning.apply_profile(state, profile, canvas, tokens)
+ decision.update(profile_id=applied, reason=('Apply the locally validated tuning profile' if applied
+ else 'No validated tuning profile matches this input, geometry and current resources'))
+ else:
+ decision['reason'] = reason
+ except (OSError, ValueError, KeyError, TypeError, subprocess.SubprocessError) as error:
+ decision['reason'] = 'Could not validate saved tuning: ' + str(error)
+ return profile, decision
+
+
+def main(argv=None):
+ parser = argparse.ArgumentParser(prog='freevideo predict',
+ description='Preview phase times and memory from complete local history; no generation or kernel probes.')
+ parser.add_argument('--history', type=Path, help='Attempt database; defaults to this installation')
+ parser.add_argument('--cache', type=Path, help='Prepared model cache; defaults to the setup machine configuration')
+ parser.add_argument('--base', type=Path, help='Same base model path as the intended generation')
+ parser.add_argument('--checkpoint', type=Path, help='Same checkpoint path as the intended generation')
+ parser.add_argument('--profile', type=Path, help='Forecast an explicit profile without changing its placement')
+ parser.add_argument('--conditioning', type=Path, help='Optional existing input: look up its token length by SHA256, without loading tensors')
+ parser.add_argument('--width', type=int, default=1344)
+ parser.add_argument('--height', type=int, default=768)
+ length = parser.add_mutually_exclusive_group()
+ length.add_argument('--frames', type=int)
+ length.add_argument('--seconds', type=float)
+ parser.add_argument('--task', choices=('t2va', 'i2va', 'l2va', 'fl2va', 'ref2va', 'ref2va_audio', 'ref2va_av'), help='Override the profile task (default: t2va)')
+ parser.add_argument('--cache-state', choices=('cold', 'warm', 'unknown'), default='unknown',
+ help='Explicit AdaLN cache regime for load/worker time; never inferred from history')
+ parser.add_argument('--allocator-limit-gib', type=float,
+ help='Forecast the same explicit PyTorch benchmark cap; capped and ordinary history stay separate')
+ parser.add_argument('--attention', default='auto', help='Requested backend for automatic profiles; never probes kernels')
+ parser.add_argument('--vram-gib', type=float)
+ parser.add_argument('--ram-gib', type=float)
+ parser.add_argument('--gpu-reserve-gib', type=float)
+ parser.add_argument('--ram-reserve-gib', type=float)
+ parser.add_argument('--no-tuning', action='store_true', help='Preview without saved numerical tuning, matching generate --no-tuning')
+ parser.add_argument('--no-history-placement', action='store_true', help='Preview current policy without history placement selection')
+ parser.add_argument('--json', dest='json_output', action='store_true', help='Print complete machine-readable forecast and selection evidence')
+ args = parser.parse_args(argv)
+ try:
+ canvas = geometry(args.width, args.height, frames=args.frames, seconds=args.seconds)
+ hardware = detect()
+ if args.profile:
+ profile = json.loads(args.profile.expanduser().read_text(encoding='utf-8'))
+ if 'gpu_budget_bytes' in profile:
+ cap = profile.get('benchmark_allocator_limit_bytes')
+ profile = dict(gpu_budget_gb=profile['gpu_budget_bytes'] / 1e9,
+ inference_ram_budget_gb=profile['ram_budget_bytes'] / 1e9,
+ engine=profile['engine'], decoder=profile['decoder'],
+ allocator_config=profile['allocator_config'], policy=profile)
+ if cap is not None:
+ profile['benchmark_allocator_limit_bytes'] = cap
+ else:
+ profile = choose(hardware, **{name: getattr(args, name) for name in
+ ('vram_gib', 'ram_gib', 'attention', 'gpu_reserve_gib', 'ram_reserve_gib')},
+ available_backends=available_backends(hardware, probe_missing=False), canvas=canvas).legacy_profile()
+ profile = copy.deepcopy(profile)
+ for name in ('base', 'checkpoint'):
+ value = getattr(args, name)
+ if value is not None:
+ profile['engine'][name] = str(value.expanduser().resolve())
+ if args.task:
+ profile['engine']['task'] = args.task
+ limit = benchmark_allocator_limit(profile, args.allocator_limit_gib, hardware)
+ if limit is not None:
+ profile['benchmark_allocator_limit_bytes'] = limit
+ context = {'adaln_cache_state': args.cache_state}
+ profile['allocator_config'] = profile.get('allocator_config', os.environ.get('PYTORCH_ALLOC_CONF',
+ os.environ.get('PYTORCH_CUDA_ALLOC_CONF', 'backend:native,pinned_max_round_threshold_mb:128')))
+ cache = _selected_cache(args.cache)
+ identity = local_identity(hardware, cache) if cache is not None else None
+ selected_history = args.history.expanduser() if args.history else history_path()
+ history = ResourceHistory(selected_history) if selected_history.exists() else None
+ observations = (history.observations(identity=identity, limit=512, include_details=False)
+ if history is not None and identity is not None else [])
+ conditioning = _conditioning(args.conditioning, observations)
+ profile, tuning_decision = _tuned_profile(profile, cache, hardware, canvas, conditioning['text_tokens'],
+ not args.profile and not args.no_tuning and args.attention == 'auto')
+ # No model identity is guessed when setup has not supplied a cache.
+ forecast_identity = identity if identity is not None else {'unverified_model_identity': True}
+ if args.profile or args.no_history_placement:
+ decision = {'changed': False, 'reason': 'Explicit profile or --no-history-placement: preview preserves placement and backend'}
+ elif observations:
+ profile, decision = select(profile, canvas, hardware, observations, forecast_identity,
+ text_tokens=conditioning['text_tokens'], execution_context=context)
+ else:
+ decision = {'changed': False, 'reason': 'No compatible complete history; show the current automatic policy'}
+ compatibility = None
+ if identity is not None:
+ from .compatibility import Store, apply as apply_compatibility
+ profile, compatibility = apply_compatibility(profile, Store(selected_history.parent).status(identity))
+ forecast = predict(observations, forecast_identity, profile, canvas, text_tokens=conditioning['text_tokens'],
+ execution_context=context)
+ forecast['tuning_preview'] = tuning_decision
+ forecast['reasons'].append(tuning_decision['reason'])
+ if identity is None:
+ forecast['reasons'].append('No prepared model cache selected; run setup or supply --cache for exact local model/software identity.')
+ result = {'forecast': forecast, 'profile': profile, 'selection': decision,
+ 'compatibility': compatibility,
+ 'history': str(selected_history), 'history_exists': history is not None,
+ 'model_identity_verified': identity is not None, 'conditioning': conditioning, 'tuning': tuning_decision,
+ 'scope': 'Preview only; video-engine timings exclude text encoding. No inference or kernel probe was started.'}
+ if args.json_output:
+ _emit(result, sys.stdout)
+ else:
+ show(forecast)
+ if identity is None:
+ print('Select a prepared cache with --cache or complete setup to match local history.')
+ return 0
+ except (OSError, ValueError, TypeError, KeyError, RuntimeError, ResourceHistoryError) as error:
+ _emit({'error': str(error), 'inference_started': False, 'kernel_probes_started': False}, sys.stderr)
+ return 2
+
+
+if __name__ == '__main__':
+ raise SystemExit(main())
diff --git a/freevideo_engine/prediction_policy.py b/freevideo_engine/prediction_policy.py
new file mode 100644
index 0000000..99252fa
--- /dev/null
+++ b/freevideo_engine/prediction_policy.py
@@ -0,0 +1,331 @@
+"""Consume complete local placements and the existing same-shape FF recovery.
+
+The core policy remains the cold-start candidate. This helper considers at most
+three existing candidates and never turns a forecast into numerical validation
+or a capacity certificate. It does not allocate memory or import Torch.
+"""
+import copy
+from collections import deque
+import math
+
+from .hardware import GiB
+
+
+NEUTRAL_KEYS = frozenset(('resident_blocks', 'pin_host_gb', 'prefetch', 'stream_weights'))
+HISTORY_LIMIT = 256
+MINIMUM_GAIN = .02
+
+
+def _number(value):
+ return type(value) in (int, float) and math.isfinite(value) and value >= 0
+
+
+def _tokens(canvas):
+ if not isinstance(canvas, dict):
+ return None
+ width, height, frames = [canvas.get(name) for name in ('width', 'height', 'frames')]
+ if any(type(value) is not int or value <= 0 for value in (width, height, frames)):
+ return None
+ if width % 32 or height % 32 or frames < 5 or (frames - 5) % 17:
+ return None
+ return ((frames - 5) // 17 * 5 + 2) * (width // 32) * (height // 32)
+
+
+def _arithmetic(options):
+ return dict({key: value for key, value in options.items() if key != 'engine'},
+ engine={key: value for key, value in options['engine'].items() if key not in NEUTRAL_KEYS})
+
+
+def _patch(options):
+ engine = options['engine']
+ resident, pin, prefetch = [engine.get(key) for key in ('resident_blocks', 'pin_host_gb', 'prefetch')]
+ streamed = engine.get('stream_weights', False)
+ if (type(resident) is not int or not 0 <= resident <= 50 or not _number(pin)
+ or type(prefetch) is not bool or type(streamed) is not bool):
+ return None
+ return dict(resident_blocks=resident, pin_host_gb=pin, prefetch=prefetch, stream_weights=streamed)
+
+
+def _same_request_recompute(current, observed, canvas, measured, text_tokens, observation):
+ """Reuse only the already shipped FF recovery on a complete matching input shape.
+
+ Success at a different chunk/backend/decoder is not numerical validation.
+ Do not extrapolate a recompute recovery to unknown text or reference shapes.
+ """
+ engine = current['engine']
+ if (engine.get('fp8_ff_recompute', False) is not False
+ or observed['engine'].get('fp8_ff_recompute') is not True
+ or engine.get('inference_kernels') is not True
+ or engine.get('linear_compute', 'native-fp8') != 'native-fp8'
+ or type(engine.get('ff_chunk')) is not int or engine['ff_chunk'] <= 0):
+ return False
+ if (type(text_tokens) is not int or text_tokens <= 0
+ or observation.get('text_tokens') != text_tokens
+ or any(canvas.get(key, 0) != measured.get(key, 0) for key in
+ ('width', 'height', 'frames', 'fps', 'reference_video_tokens', 'reference_audio_tokens'))):
+ return False
+ expected = copy.deepcopy(_arithmetic(current))
+ expected['engine']['fp8_ff_recompute'] = True
+ return _arithmetic(observed) == expected
+
+
+def _resource(forecast, name, statistic):
+ value = forecast.get('resources', {}).get(name, {}).get(statistic)
+ return value if _number(value) and value > 0 else None
+
+
+def _seconds(forecast):
+ stages = forecast.get('stages', {})
+ sample = stages.get('sample_seconds', {}).get('estimate')
+ if not _number(sample) or sample <= 0:
+ return None
+ return sample
+
+
+def _work_seconds(forecast, canvas):
+ value = forecast.get('stages', {}).get('work_seconds', {})
+ numbers = [value.get(key) for key in ('lower', 'estimate', 'upper')]
+ if (not all(_number(number) for number in numbers) or not 0 <= numbers[0] <= numbers[1] <= numbers[2]
+ or numbers[1] <= 0):
+ return None
+ context = forecast.get('geometry', {})
+ if not isinstance(context, dict) or any(context.get(key) != canvas[key] for key in ('width', 'height', 'frames')):
+ return None
+ return numbers[1]
+
+
+def _work_context(forecast):
+ context = forecast.get('execution_context')
+ if not isinstance(context, dict):
+ return None, False
+ known = ('cold', 'warm', 'partial')
+ matched = forecast.get('stages', {}).get('work_seconds', {}).get('cache_context_matched') is True
+ if matched and context.get('adaln_cache_state') in known:
+ return context['adaln_cache_state'], True
+ counts = context.get('observed_cache_regimes', {})
+ states = ([state for state, count in counts.items() if _number(count) and count > 0]
+ if isinstance(counts, dict) else [])
+ if len(states) == 1 and states[0] in known:
+ return states[0], False
+ return None, False
+
+
+def _work_threads(forecast):
+ context = forecast.get('execution_context')
+ values = context.get('observed_host_threads') if isinstance(context, dict) else None
+ if values is None or values == [] or values == [None]:
+ return 'unknown', None
+ if isinstance(values, list) and len(values) == 1 and isinstance(values[0], dict) and values[0]:
+ return 'measured', values[0]
+ return 'mixed', None
+
+
+def select(base, canvas, hardware, history_rows, identity, *, reject=None, text_tokens=None,
+ execution_context=None):
+ """Return ``(profile, decision)`` from at most three complete local candidates.
+
+ ``reject(profile)`` supplies an optional caller resource constraint.
+ Live memory remains part of placement selection. Only a local complete
+ observation supplies a candidate. The existing FF-recompute recovery may
+ be reused for an identical input shape; it is labeled separately from
+ neutral placement. No new head/window/chunk size or backend is applied.
+ """
+ from .prediction import predict, profile_options
+ rows = list(deque(history_rows if history_rows is not None else (), maxlen=HISTORY_LIMIT))
+ current = profile_options(base)
+ arithmetic = _arithmetic(current)
+ target_tokens = _tokens(canvas)
+ if not target_tokens:
+ raise ValueError('Prediction policy needs the original aligned H3 geometry')
+ candidates = [{'profile': copy.deepcopy(base), 'origin': 'current-policy', 'source_attempt': None}]
+ eligible = []
+ seen = set()
+ from .two_pass import same_strategy, steps as sampling_steps
+ total_steps = sampling_steps(canvas.get('sampling_plan'))
+ for row in reversed(rows):
+ if (not isinstance(row, dict) or row.get('identity') != identity
+ or row.get('outcome', row.get('state')) != 'success'
+ or row.get('purpose') not in ('generation', 'validation', 'full_request')):
+ continue
+ observation = row.get('observation')
+ if (not isinstance(observation, dict) or observation.get('full_request') is not True
+ or observation.get('validated') is not True or type(observation.get('completed_steps')) is not int
+ or observation['completed_steps'] != total_steps
+ or observation.get('media_verified') is not True or observation.get('metrics_complete') is not True):
+ continue
+ times = observation.get('step_seconds')
+ if not isinstance(times, list) or len(times) != total_steps or any(not _number(value) or value <= 0 for value in times):
+ continue
+ measured_canvas = observation.get('geometry', row.get('geometry', {}))
+ if not same_strategy(measured_canvas, canvas):
+ continue
+ count = _tokens(measured_canvas)
+ if not count or observation.get('completed_frames') != measured_canvas.get('frames'):
+ continue
+ original_canvas = row.get('geometry', {})
+ if (not isinstance(original_canvas, dict) or any(original_canvas.get(key) != measured_canvas.get(key)
+ for key in ('width', 'height', 'frames'))):
+ continue
+ try:
+ options = profile_options(row.get('config', {}))
+ patch = _patch(options)
+ if patch is None:
+ continue
+ recovery = _same_request_recompute(current, options, canvas, original_canvas, text_tokens, observation)
+ if _arithmetic(options) != arithmetic and not recovery:
+ continue
+ if recovery:
+ patch['fp8_ff_recompute'] = True
+ except (KeyError, TypeError, ValueError, AttributeError):
+ continue
+ marker = tuple(sorted(patch.items()))
+ if marker in seen:
+ continue
+ stages = observation.get('stage_seconds')
+ if not isinstance(stages, dict):
+ continue
+ sample = stages.get('sample_seconds')
+ peak = observation.get('peak_reserved_bytes')
+ if not _number(sample) or sample <= 0 or not _number(peak) or peak <= 0:
+ continue
+ seen.add(marker)
+ selected = copy.deepcopy(base)
+ selected['engine'].update(patch)
+ if isinstance(selected.get('policy'), dict):
+ selected['policy']['engine'] = copy.deepcopy(selected['engine'])
+ # Distance chooses relevant historical candidates, not a memory/speed
+ # model. The independent forecaster evaluates their requested geometry.
+ distance = (abs(math.log(count / target_tokens)) +
+ abs(math.log(measured_canvas['width'] / canvas['width'])) +
+ abs(math.log(measured_canvas['height'] / canvas['height'])))
+ work = stages.get('work_seconds')
+ eligible.append(dict(profile=selected, origin='complete-local-ff-recovery' if recovery else 'complete-local-placement', source_attempt=row.get('id'),
+ distance=distance, sample_rate=sample / count,
+ work_rate=work / count if _number(work) and work > 0 else None, peak=peak))
+ fastest_sample = sorted(eligible, key=lambda item: (item['distance'], item['sample_rate']))
+ fastest_work = sorted((item for item in eligible if item['work_rate'] is not None),
+ key=lambda item: (item['distance'], item['work_rate']))
+ lightest = sorted(eligible, key=lambda item: (item['distance'], item['peak']))
+ # Useful placement beats occupancy: never synthesize additional residency
+ # from free bytes alone. Both increases and decreases may win if measured.
+ for item in fastest_work[:1] + fastest_sample[:1] + lightest[:1] + fastest_work[1:] + fastest_sample[1:]:
+ if any(profile_options(item['profile']) == profile_options(existing['profile']) for existing in candidates):
+ continue
+ candidates.append(item)
+ if len(candidates) == 3:
+ break
+ observed = hardware.to_dict() if hasattr(hardware, 'to_dict') else hardware
+ policy = base.get('policy', {})
+ desktop = observed.get('system') == 'Windows'
+ gpu_reserve = policy.get('gpu_system_reserve_bytes', (1 if desktop else .5) * GiB)
+ ram_reserve = policy.get('ram_system_reserve_bytes', (2 if desktop else 1) * GiB)
+ gpu_limit = max(0, min(base['gpu_budget_gb'] * 1e9, observed['vram_free'] - gpu_reserve))
+ if current.get('benchmark_allocator_limit_bytes') is not None:
+ gpu_limit = min(gpu_limit, current['benchmark_allocator_limit_bytes'])
+ ram_limit = max(0, min(base['inference_ram_budget_gb'] * 1e9, observed['ram_available'] - ram_reserve))
+ # Live memory below the budget the plan was built against is the one case
+ # where an unmeasured placement is genuinely suspect: the arithmetic behind
+ # it no longer holds. A byte of slack absorbs the GB float round trip.
+ live_shortfall = (gpu_limit + 1 < base['gpu_budget_gb'] * 1e9
+ or ram_limit + 1 < base['inference_ram_budget_gb'] * 1e9)
+ evaluated = []
+ for candidate in candidates:
+ profile = candidate['profile']
+ blocked = reject(profile) if reject is not None else None
+ forecast = predict(rows, identity, profile, canvas, text_tokens=text_tokens, execution_context=execution_context)
+ gpu_point = _resource(forecast, 'peak_reserved_bytes', 'estimate')
+ ram_point = _resource(forecast, 'ram_peak_bytes', 'estimate')
+ gpu_upper = _resource(forecast, 'peak_reserved_bytes', 'upper')
+ ram_upper = _resource(forecast, 'ram_peak_bytes', 'upper')
+ fits = (not blocked and forecast.get('status') == 'estimated' and gpu_point is not None
+ and ram_point is not None and gpu_point <= gpu_limit and ram_point <= ram_limit)
+ interval_fits = (fits and gpu_upper is not None and ram_upper is not None
+ and gpu_upper <= gpu_limit and ram_upper <= ram_limit)
+ # "No local measurement" is not "will not fit". The forecaster reports
+ # 'unknown' when this machine has no evidence for a placement, so a
+ # placement it has never run can never be shown to fit. Only a caller
+ # blocker, a forecast built on evidence, or live memory below what the
+ # plan was budgeted against is evidence against a candidate.
+ unmeasured = not blocked and forecast.get('status') != 'estimated'
+ over_live_budget = bool(blocked) or (not fits and not unmeasured) or (unmeasured and live_shortfall)
+ cache_state, cache_matched = _work_context(forecast)
+ thread_state, threads = _work_threads(forecast)
+ evaluated.append(dict(profile=profile, origin=candidate['origin'], source_attempt=candidate['source_attempt'],
+ prediction=forecast, sampling_seconds=_seconds(forecast), work_seconds=_work_seconds(forecast, canvas),
+ work_cache_state=cache_state, work_cache_context_matched=cache_matched,
+ work_host_thread_state=thread_state, work_host_threads=threads,
+ point_within_live_budget=bool(fits),
+ interval_within_live_budget=bool(interval_fits),
+ forecast_unmeasured=bool(unmeasured),
+ forecast_over_live_budget=bool(over_live_budget),
+ resource_estimate=dict(gpu_bytes=gpu_point, ram_bytes=ram_point),
+ resource_upper=dict(gpu_bytes=gpu_upper, ram_bytes=ram_upper),
+ rejected_reason=str(blocked) if blocked else None,
+ reason='Neutral placement admission uses the point estimate; interval crossings remain uncertainty, not extra reserved memory'))
+ baseline = evaluated[0]
+ feasible = [item for item in evaluated if item['point_within_live_budget']]
+ chosen = baseline
+ reason = 'Keep current policy: no supported faster compatible placement forecast fits current memory'
+ work_options = [item for item in feasible if item['work_seconds'] is not None and baseline['work_cache_state'] is not None
+ and item['work_cache_state'] == baseline['work_cache_state']
+ and baseline['work_host_thread_state'] != 'mixed'
+ and item['work_host_thread_state'] == baseline['work_host_thread_state']
+ and item['work_host_threads'] == baseline['work_host_threads']]
+ sample_options = [item for item in feasible if item['sampling_seconds'] is not None]
+ has_work_comparison = bool(work_options) and (len(evaluated) == 1 or any(item is not baseline for item in work_options))
+ objective = 'work_seconds' if baseline['work_seconds'] is not None and has_work_comparison else 'sample_seconds'
+ ranking = work_options if objective == 'work_seconds' else sample_options
+ key = 'work_seconds' if objective == 'work_seconds' else 'sampling_seconds'
+ objective_reason = ('Full video-engine load, sampling and decode time; valid forecast intervals, assuming the same observed %s cache regime.' % baseline['work_cache_state']
+ if objective == 'work_seconds' else 'Sampling-only fallback: a matching full-work forecast is unavailable; no full-request gain is claimed.')
+ if objective == 'work_seconds':
+ objective_reason += (' Host thread settings match the measured history.' if baseline['work_host_thread_state'] == 'measured'
+ else ' Host thread settings were not recorded; this remains an unverified performance assumption.')
+ if ranking:
+ fastest = min(ranking, key=lambda item: item[key])
+ if baseline['forecast_over_live_budget']:
+ # Memory recovery is a different objective from paying sampling
+ # time for a hoped-for load saving. A fitting placement may be
+ # slower than the predicted-over-budget baseline; report that trade.
+ chosen = fastest
+ reason = 'Use a completed local neutral placement whose resource estimate fits the current request and live memory; this is not a claimed speedup over an over-budget or unmeasured baseline'
+ elif baseline['forecast_unmeasured']:
+ # Ranking needs both sides measured. Swapping in history here would
+ # freeze whichever placement this machine completed first: the new
+ # plan stays unmeasured, so it loses this comparison every time,
+ # and a grown budget keeps spending a smaller budget's placement.
+ # The same-shape FF recovery is the exception: it is a memory
+ # recovery this machine already needed for this exact request, and
+ # reusing it avoids rediscovering it through an OOM.
+ recovery = next((item for item in ranking if item['origin'] == 'complete-local-ff-recovery'), None)
+ if recovery is not None:
+ chosen = recovery
+ else:
+ reason = ('Keep current policy: its placement has no local measurement to forecast, '
+ 'and live memory still covers the budget it was planned against')
+ elif baseline[key] is None:
+ chosen = fastest
+ reason = 'Use a completed local neutral placement whose resource estimate fits the current request and live memory; this is not a claimed speedup over an over-budget or unmeasured baseline'
+ elif fastest[key] < baseline[key] * (1 - MINIMUM_GAIN):
+ chosen = fastest
+ reason = 'Use the faster predicted local placement; %s improvement exceeds the 2%% switch threshold' % objective
+ else:
+ reason = 'Keep current placement: %s improvement is below 2%%; occupying more memory is not a benefit' % objective
+ result = copy.deepcopy(chosen['profile'])
+ reused_recompute = chosen['origin'] == 'complete-local-ff-recovery'
+ if reused_recompute:
+ reason = ('Reuse the completed local FF-recompute recovery for the same request shape and text length; '
+ 'its measured resource forecast fits live budgets. Avoid rediscovering this recovery through OOM; '
+ 'this is the existing same-shape recovery path, not a new numerical-equivalence certificate.')
+ decision = dict(changed=profile_options(result) != profile_options(base), reason=reason,
+ source_attempt=chosen['source_attempt'], prediction=chosen['prediction'],
+ candidates=evaluated, history_rows_considered=len(rows),
+ objective=objective, objective_reason=objective_reason,
+ limits=dict(gpu_bytes=gpu_limit, ram_bytes=ram_limit),
+ upper_bound_crosses_budget=(None if any(value is None for value in chosen['resource_upper'].values())
+ else chosen['resource_upper']['gpu_bytes'] > gpu_limit or chosen['resource_upper']['ram_bytes'] > ram_limit),
+ recovery='Resource guards and fresh-worker placement recovery remain required; an interval is not a guarantee.',
+ numerical_class='same-shape-ff-recompute' if reused_recompute else 'placement-only', capacity_certified=False,
+ scope=('Existing same-shape FF recovery and original request. Historical predictions remain bounded by live resources.'
+ if reused_recompute else 'Same arithmetic and original request. Historical predictions remain bounded by live resources.'))
+ return result, decision
diff --git a/freevideo_engine/prediction_ui.py b/freevideo_engine/prediction_ui.py
new file mode 100644
index 0000000..fdf3bcc
--- /dev/null
+++ b/freevideo_engine/prediction_ui.py
@@ -0,0 +1,81 @@
+"""Expose forecast ranges and retain pre-request prediction errors, torch-free."""
+import json
+import math
+import os
+
+from .terminal_ui import TerminalUI, duration
+
+
+def estimate_text(value, *, memory=False):
+ if not isinstance(value, dict) or value.get('estimate') is None:
+ return 'unknown'
+ low, high, point = value.get('lower'), value.get('upper'), value.get('estimate')
+ if (any(type(n) not in (int, float) or not math.isfinite(n) or n < 0 for n in (low, high, point))
+ or not low <= point <= high):
+ return 'unknown'
+ return ('%.1f–%.1f GiB' % (low / 2**30, high / 2**30)) if memory else duration(low) + '–' + duration(high)
+
+
+def summary(forecast, *, zh=False):
+ if not isinstance(forecast, dict) or forecast.get('status') != 'estimated':
+ return ('暂无匹配的完整运行历史;完成后自动学习。' if zh else
+ 'No matching complete history yet; this request will teach the estimator.')
+ stages, resources = forecast.get('stages') or {}, forecast.get('resources') or {}
+ if not isinstance(stages, dict) or not isinstance(resources, dict):
+ return summary({}, zh=zh)
+ total = estimate_text(stages.get('work_seconds'))
+ sample = estimate_text(stages.get('sample_seconds'))
+ decode = estimate_text(stages.get('decode_save_seconds'))
+ vram = estimate_text(resources.get('whole_gpu_peak_bytes'), memory=True)
+ ram = estimate_text(resources.get('ram_peak_bytes'), memory=True)
+ preview = forecast.get('tuning_preview') or {}
+ pending_input = isinstance(preview, dict) and preview.get('input_tokens_known') is False
+ scope = (' · 不含文字编码' if zh else ' · excludes text encoding')
+ if pending_input:
+ scope += (';提示词编码后配置可能调整' if zh else '; the profile may change after prompt encoding')
+ if zh:
+ return ('预计引擎 %s · 采样 %s · 解码保存 %s · 显存 %s · RAM %s · %s 条历史,可信度 %s%s' %
+ (total, sample, decode, vram, ram, forecast.get('evidence_count', 0),
+ {'weak': '较低', 'moderate': '中等', 'strong': '较高'}.get(forecast.get('confidence'), '未知'),
+ '(外推)' if forecast.get('extrapolation') else '')) + scope
+ return ('Engine %s · sampling %s · decode/save %s · VRAM %s · RAM %s · %s histories, %s confidence%s' %
+ (total, sample, decode, vram, ram, forecast.get('evidence_count', 0), forecast.get('confidence', 'unknown'),
+ ' (extrapolated)' if forecast.get('extrapolation') else '')) + scope
+
+
+def show(forecast, *, stage='video'):
+ if os.environ.get('FREEVIDEO_UI_EVENTS') == '1':
+ print(json.dumps({'event': 'freevideo_ui', 'kind': 'prediction', 'key': 'request-prediction',
+ 'label': 'History forecast', 'detail': summary(forecast), 'forecast': forecast,
+ 'stage': stage}), flush=True)
+ else:
+ stages = forecast.get('stages') or {}
+ TerminalUI('FreeVideo', plain=True).panel('Request forecast', [
+ ('Estimate', summary(forecast)),
+ ('Model load', estimate_text(stages.get('load_seconds'))),
+ ('Sampling', estimate_text(stages.get('sample_seconds'))),
+ ('Decode / save', estimate_text(stages.get('decode_save_seconds'))),
+ ('Scope', 'Video-engine time; text encoding is separate. Ranges are estimates, not capacity guarantees.')])
+
+
+def errors(forecast, observation):
+ result = {'scope': 'Forecast saved before this request; actual request excluded from its own prediction.',
+ 'stages': {}, 'resources': {}}
+ forecast = forecast if isinstance(forecast, dict) else {}
+ observation = observation if isinstance(observation, dict) else {}
+ for group, actuals in (('stages', observation.get('stage_seconds') or {}), ('resources', observation)):
+ values = forecast.get(group) or {}
+ if not isinstance(values, dict) or not isinstance(actuals, dict):
+ continue
+ for key, predicted in values.items():
+ actual = actuals.get(key)
+ if not isinstance(predicted, dict):
+ continue
+ expected, low, high = (predicted.get(n) for n in ('estimate', 'lower', 'upper'))
+ if (any(type(n) not in (int, float) or not math.isfinite(n) or n < 0 for n in (actual, expected, low, high))
+ or not low <= expected <= high):
+ continue
+ result[group][key] = dict(predicted=expected, actual=actual, error=actual-expected,
+ relative_error=(actual-expected)/expected if expected > 0 else None,
+ within_interval=low <= actual <= high)
+ return result
diff --git a/freevideo_engine/preparation.py b/freevideo_engine/preparation.py
new file mode 100644
index 0000000..4da6782
--- /dev/null
+++ b/freevideo_engine/preparation.py
@@ -0,0 +1,95 @@
+"""Metadata-only resume checks for prepared model groups; no Torch import."""
+import hashlib
+import json
+from pathlib import Path
+
+from .network import hash_file
+
+
+# An audited, byte-identical I/O migration, pinned at BOTH ends. Editing either
+# loader invalidates this permission until its arithmetic is checked again.
+# Every other identity field (source, upstream quantizer, Torch, format) matches
+# exactly. The source marker retains the original producer's truthful identity.
+AUDITED_FP8_IMPLEMENTATION = 'b60f9c865fbc8725bd3389e37fed58972b0557879dbb51bf537b05e7f07318b2'
+AUDITED_WEIGHTS_IMPLEMENTATION = 'fb818cff05ec5d7e87a07ffba9998156e4cd6c7ca2f7ee7db0c2d749685d34e1'
+COMPATIBLE_FP8_IMPLEMENTATIONS = ('9d5f0fce6eb100b50bd5b669f96cb45677d4141235e66492dbf6a962853165ee',)
+COMPATIBLE_WEIGHTS_IMPLEMENTATIONS = ('0e9d79b94265c99cb144fe0d57f46e95c4befca3d7430579a0cc0bc424565e19',)
+
+
+def source_paths(base, checkpoint):
+ return sorted(set((base / 'transformer').glob('*.safetensors')) |
+ {checkpoint / 'linear_branch/model.safetensors'} |
+ set((checkpoint / 'adapters').glob('*/adapter_model.safetensors')))
+
+
+def expected_groups(base):
+ count = json.loads((base / 'transformer/config.json').read_text(encoding='utf-8'))['num_layers']
+ if type(count) is not int or count < 1:
+ raise ValueError('Invalid transformer layer count')
+ return sorted(['root'] + [kind + '/%02d' % i for kind in ('adaln', 'blocks') for i in range(count)])
+
+
+def cache_location(root, prefix, identity):
+ identities = [identity]
+ if identity['implementation_sha256'] == AUDITED_FP8_IMPLEMENTATION:
+ if (prefix == 'vdn-fp8-streamed-'
+ and identity.get('merge_implementation_sha256') == AUDITED_WEIGHTS_IMPLEMENTATION):
+ identities += [dict(identity, implementation_sha256=fp8, merge_implementation_sha256=weights)
+ for fp8, weights in zip(COMPATIBLE_FP8_IMPLEMENTATIONS, COMPATIBLE_WEIGHTS_IMPLEMENTATIONS)]
+ elif prefix == 'vdn-fp8-':
+ identities += [dict(identity, implementation_sha256=old) for old in COMPATIBLE_FP8_IMPLEMENTATIONS]
+ elif prefix == 'vdn-bf16-' and identity['implementation_sha256'] == AUDITED_WEIGHTS_IMPLEMENTATION:
+ identities += [dict(identity, implementation_sha256=old) for old in COMPATIBLE_WEIGHTS_IMPLEMENTATIONS]
+ candidates = []
+ for selected in identities:
+ key = hashlib.sha256(json.dumps(selected, sort_keys=True).encode()).hexdigest()
+ output = Path(root) / (prefix + key[:16])
+ candidates.append((output, selected, key))
+ marker = output / 'source.json'
+ if marker.is_file():
+ try:
+ if json.loads(marker.read_text(encoding='utf-8')) == selected:
+ return output, selected, key
+ except (ValueError, UnicodeError):
+ pass
+ if output.exists() and selected is identity:
+ # Never work around a conflicting current identity by selecting an
+ # older directory; the caller must report that conflict.
+ return output, selected, key
+ return candidates[0]
+
+
+def reusable_group(output, group):
+ """Hash bounded chunks and inspect headers, never deserialize tensor data."""
+ destination = output / (group + '.safetensors')
+ progress = destination.with_suffix('.json')
+ if not destination.is_file() or not progress.is_file():
+ return None
+ from safetensors import SafetensorError, safe_open
+ try:
+ saved = json.loads(progress.read_text(encoding='utf-8'))
+ if (not isinstance(saved, dict) or saved.get('group') != group
+ or saved.get('file') != group + '.safetensors'
+ or not isinstance(saved.get('linears'), dict)
+ or destination.stat().st_size != saved.get('bytes')
+ or hash_file(destination, discard_cache=True) != saved.get('sha256')):
+ return None
+ # NumPy framework keeps this path independent of Torch and its storage
+ # constructors. get_slice().get_shape() reads metadata only, also for FP8.
+ with safe_open(destination, framework='np') as stream:
+ keys = set(stream.keys())
+ if saved.get('tensors') != len(keys):
+ return None
+ prefixes = {name.removesuffix('.weight_fp8') for name in keys if name.endswith('.weight_fp8')}
+ if set(saved['linears']) != prefixes:
+ return None
+ for name, spec in saved['linears'].items():
+ if (not isinstance(spec, dict) or spec.get('input_dtype') not in ('bfloat16', 'float32')
+ or spec.get('weight_shape') != stream.get_slice(name + '.weight_fp8').get_shape()
+ or spec.get('scale_shape') != stream.get_slice(name + '.weight_scale').get_shape()
+ or stream.get_slice(name + '.weight_fp8').get_dtype() != 'F8_E4M3'
+ or stream.get_slice(name + '.weight_scale').get_dtype() != 'F32'):
+ return None
+ return saved
+ except (OSError, ValueError, TypeError, KeyError, SafetensorError):
+ return None
diff --git a/freevideo_engine/prepared_model.py b/freevideo_engine/prepared_model.py
new file mode 100644
index 0000000..20cb003
--- /dev/null
+++ b/freevideo_engine/prepared_model.py
@@ -0,0 +1,135 @@
+"""Pinned prebuilt model selection and access checks, without Torch or Hub SDKs.
+
+The catalog is shipped with the engine, never learned from an unverified remote
+manifest. Each architecture keeps its existing FP8 quantization granularity.
+"""
+import json
+import os
+from pathlib import Path, PurePosixPath
+import re
+import subprocess
+
+from . import network
+
+CATALOG = Path(__file__).with_name('prepared_models.json')
+
+
+def catalog():
+ value = json.loads(CATALOG.read_text(encoding='utf-8'))
+ if (value.get('schema_version') != 1 or
+ not re.fullmatch(r'[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+', value.get('repo', '')) or
+ not re.fullmatch('[0-9a-f]{40}', value.get('revision', ''))):
+ raise ValueError('Invalid pinned prepared model catalog')
+ for variant in value['variants'].values():
+ if not re.fullmatch('[0-9a-f]{40}', variant.get('revision', value['revision'])):
+ raise ValueError('Invalid prepared variant revision')
+ names = set()
+ for row in variant['files']:
+ name = row.get('file', '')
+ if (not name or '\\' in name or ':' in name or PurePosixPath(name).is_absolute() or
+ any(p in ('', '.', '..') for p in name.split('/')) or name in names or
+ type(row.get('bytes')) is not int or row['bytes'] <= 0 or
+ not re.fullmatch('[0-9a-f]{64}', row.get('sha256', ''))):
+ raise ValueError('Invalid prepared model file record')
+ names.add(name)
+ prefix = variant['cache_prefix']
+ if prefix + '/manifest.json' not in names:
+ raise ValueError('Prepared variant is missing its pinned cache manifest')
+ return value
+
+
+def select(capability, root):
+ value = catalog()
+ scale = 'per_tensor' if capability[0] >= 10 else 'rowwise'
+ variant = value['variants'].get(scale)
+ if variant is None:
+ return None # Never change quantization to make an artifact fit a GPU.
+ # Adding another format must not move an existing installation to a new
+ # directory or redownload its unchanged model. Variants may pin separately.
+ revision = variant.get('revision', value['revision'])
+ folder = Path(root).expanduser().resolve() / 'prepared' / ('edge-' + revision[:16])
+ return dict(repo=value['repo'], revision=revision, scale_granularity=scale,
+ directory=str(folder), cache=str(folder / variant['cache_prefix']),
+ private=value.get('private', False), format=value['format'])
+
+
+def files(selection):
+ if not selection:
+ return []
+ value = catalog()
+ if any(selection.get(k) != value[k] for k in ('repo', 'format')):
+ raise ValueError('Prepared model selection changed; review setup again')
+ variant = value['variants'].get(selection['scale_granularity'])
+ if variant is None or Path(selection['cache']).resolve() != (Path(selection['directory']) / variant['cache_prefix']).resolve():
+ raise ValueError('Invalid prepared model cache path')
+ revision = variant.get('revision', value['revision'])
+ if selection.get('revision') != revision:
+ raise ValueError('Prepared model selection changed; review setup again')
+ return [dict(row, repo=value['repo'], revision=revision, prepared=True,
+ official_only=bool(value.get('private'))) for row in variant['files']]
+
+
+def token(environ=None):
+ """Read the same ordinary login/env token locations as HF, never serialize it."""
+ env = os.environ if environ is None else environ
+ direct = env.get('HF_TOKEN') or env.get('HUGGING_FACE_HUB_TOKEN')
+ if direct:
+ return direct.strip()
+ try:
+ location = env.get('HF_TOKEN_PATH')
+ if not location:
+ home = env.get('HF_HOME')
+ if not home:
+ home = Path(env.get('XDG_CACHE_HOME') or Path.home() / '.cache') / 'huggingface'
+ location = Path(home) / 'token'
+ path = Path(location).expanduser()
+ with path.open('r', encoding='utf-8') as stream:
+ value = stream.read(4097).strip()
+ return value if len(value) <= 4096 else None
+ except (OSError, RuntimeError):
+ return None
+
+
+def access_error(selection, networking, *, environ=None):
+ """Bounded authenticated HEAD before large dependency/model installation.
+
+ Public mirrors are tried in their measured order. Only the official Hub
+ gets credentials; proxy/direct retries retain the existing policy.
+ """
+ env = os.environ if environ is None else environ
+ secret = token(env)
+ if selection.get('private') and not secret:
+ return ('The prepared model repository is private: ' + selection['repo'] +
+ '. Set HF_TOKEN for an account with access, or run hf auth login, then retry. '
+ 'Use --model-source source only if you want the larger original-model conversion.')
+ manifest = next(r for r in files(selection) if r['file'].endswith('/manifest.json'))
+ family = network.model_family(manifest)
+ for source, url in network.model_urls(networking, manifest, env):
+ headers = ['Authorization: Bearer ' + secret] if secret and source == 'official' else []
+ for route in network.route_order(networking, family, source, env):
+ try:
+ result = subprocess.run([
+ 'curl', '--disable', '--silent', '--show-error', '--location', '--head',
+ '--output', os.devnull, '--write-out', '%{http_code}', '--connect-timeout', '5',
+ '--max-time', '12', '--config', '-'],
+ input=network.curl_config(url, headers), capture_output=True, timeout=15,
+ env=network.route_environment(networking, family, source, env, route))
+ except (OSError, subprocess.TimeoutExpired):
+ continue
+ status = result.stdout.decode('ascii', errors='ignore').strip()
+ if result.returncode == 0 and status == '200':
+ network.route_health(networking, family, source, route)
+ return None
+ if selection.get('private') and status in ('401', '403', '404'):
+ return ('Cannot access the pinned prepared model in ' + selection['repo'] +
+ '. Check your Hugging Face token and repository access; no large downloads started.')
+ return ('Could not reach the pinned prepared model sources. Check the proxy/network and retry; '
+ 'existing files are retained. No automatic switch to the much larger source model.')
+
+
+def verify_manifest(selection):
+ from .adaln_assets import file_hash, asset_path
+ row = next(r for r in files(selection) if r['file'].endswith('/manifest.json'))
+ path = asset_path(selection['directory'], row['file'])
+ if not path.is_file() or path.stat().st_size != row['bytes'] or file_hash(path) != row['sha256']:
+ raise ValueError('Prepared model manifest failed its published checksum; files retained')
diff --git a/freevideo_engine/prepared_models.json b/freevideo_engine/prepared_models.json
new file mode 100644
index 0000000..0318219
--- /dev/null
+++ b/freevideo_engine/prepared_models.json
@@ -0,0 +1,2797 @@
+{
+ "schema_version": 1,
+ "repo": "OpenVDN/vdn-minimax-h3-edge",
+ "revision": "5e6ecc39f472f98ca7493000df49a4f712efb61a",
+ "format": "freevideo-fp8-slim-v1",
+ "private": false,
+ "ready_revision": "d95329e1441404e95082053b148ba5f8d3dd754f",
+ "variants": {
+ "per_tensor": {
+ "cache_prefix": "cache",
+ "files": [
+ {
+ "file": "LICENSE",
+ "bytes": 17604,
+ "sha256": "59b99642b95ea21630e311198ddbfffbfe05aadba0c2f5d884cbdf4efcc90f44"
+ },
+ {
+ "file": "MODIFICATIONS.md",
+ "bytes": 1321,
+ "sha256": "d842d9419af674b8ab46439ea5a3fd98b4c4ab04df85a10e2027d81057445071"
+ },
+ {
+ "file": "NOTICE",
+ "bytes": 2320,
+ "sha256": "6b6671af343af83d5c8bff1b13875e9fe8ffd338f43d4343fa9efa96f49cf81f"
+ },
+ {
+ "file": "OPENVDN-CODE-LICENSE",
+ "bytes": 11358,
+ "sha256": "cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30"
+ },
+ {
+ "file": "README.md",
+ "bytes": 7051,
+ "sha256": "6234781b7570fd5fee4169ac6538c8493b39a9dc3c0f9329df8f68226ec55e06"
+ },
+ {
+ "file": "SHA256SUMS",
+ "bytes": 32355,
+ "sha256": "60035e5f87672e93cabc46627698b5ecf3617821fc400a13551b1404dbfd34da"
+ },
+ {
+ "file": "bundle.json",
+ "bytes": 176411,
+ "sha256": "3958f6b5271882c15cbca23c4cabae6535550c7b7e11485a0065fc1f3ae57cac"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/00.json",
+ "bytes": 103,
+ "sha256": "9f4e7a1a78227586e374f8400e5e43691d7acddea4a7025b8285faa29e72a166"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/00.safetensors",
+ "bytes": 4456040,
+ "sha256": "5f8393a55e4508b4f03a2325848cf74fa854dfdf84a0d949e9614079acd2bf80"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/01.json",
+ "bytes": 103,
+ "sha256": "df9828c32a00ad365559189433b3f9fb00fa5eb5ba8305479c8ad19f4e16e12e"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/01.safetensors",
+ "bytes": 4456040,
+ "sha256": "77bb900f64a824f59337498db5734c60f5c63517dca576c1b41a08db81cdc122"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/02.json",
+ "bytes": 103,
+ "sha256": "a1d674d4625648e4bfa7d49c41ccb1df365bf8547c2a838e29b3ec1ec6773ac0"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/02.safetensors",
+ "bytes": 4456040,
+ "sha256": "14e9486da0c80467e24aeb11ac95c0cef3c8de3ce6726d65c4568ab5a50c1011"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/03.json",
+ "bytes": 103,
+ "sha256": "b57783199d8bcb461b988fb4d1c3099493a8a002bb6c451f291942269887b9e8"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/03.safetensors",
+ "bytes": 4456040,
+ "sha256": "4d3bad61fc4cb241a4c4ffca7be68fdcf6d39df2d92696ee509a84f35cb2d7fa"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/04.json",
+ "bytes": 103,
+ "sha256": "c01364b637d1e4b287c063e236e1537f293bb4f0f10b624b2ff0b8aef019526d"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/04.safetensors",
+ "bytes": 4456040,
+ "sha256": "6d70b16a07e8567cb46662515d50ace7cadda46a1e261d209a6c87fad9fe7526"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/05.json",
+ "bytes": 103,
+ "sha256": "53dfefeaffe68c2c3cc4690189d0e55ff7048019cfa99536f2a089f954bc0c8e"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/05.safetensors",
+ "bytes": 4456040,
+ "sha256": "9a24951daec182dddb6ce26bd6d126f61dc6d2bf18b8b79d4a70acf58a4170e9"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/06.json",
+ "bytes": 103,
+ "sha256": "430fcab5a7d27589337f052f453a185c96ad863b86ab4539ec0bf140fbf811e2"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/06.safetensors",
+ "bytes": 4456040,
+ "sha256": "85914eb7fce42adcd50099cea5b94ce2bad2b5cb79801a35ee50a9937adc2417"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/07.json",
+ "bytes": 103,
+ "sha256": "8c7b6a8f88b924d7bddb0e7065dc18c8647911fe2d9bca6af135801fc8a59bd3"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/07.safetensors",
+ "bytes": 4456040,
+ "sha256": "70c91870a43f75433a138d1b25512101411d9be9b8cb0ea698c80d608fdf35ce"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/08.json",
+ "bytes": 103,
+ "sha256": "0657f0bf73d8e83a389f90036399fd709d5cf54be0e125b5014016b77e8912ec"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/08.safetensors",
+ "bytes": 4456040,
+ "sha256": "6ea4f9049e4d122f4b2a3605693303a4d641eee51761a40c2051cbe55e4a272e"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/09.json",
+ "bytes": 103,
+ "sha256": "bfb87bf7c84f37b28a46c8c7dd0c3b234e859f472c50b4d8bbc92098cf159f9a"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/09.safetensors",
+ "bytes": 4456040,
+ "sha256": "27cf9a8e782978097d4f2f03dd1850c7fab6ca5fd26b07c862ff0ea2ca38f434"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/10.json",
+ "bytes": 103,
+ "sha256": "5bec30a89a118f2df88647854e21cbebdbdce4aaea94c946e7eec2268e1c97eb"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/10.safetensors",
+ "bytes": 4456040,
+ "sha256": "c1ec60a934fc204428cd9024055d00f7fa47d3559df59d2d7027ebf7ccb7701e"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/11.json",
+ "bytes": 103,
+ "sha256": "be4c002eb1dc8147b97e4d26bdfae0454a48a429658832efa84a5664cf4d72e3"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/11.safetensors",
+ "bytes": 4456040,
+ "sha256": "b8e355d21647fa4e469751adaea39f06d62ed199a2992ac72ac5c90770d1d568"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/12.json",
+ "bytes": 103,
+ "sha256": "6b00e52bd9cd5a31bf8874f1f15354e5b9bc61e4b1da57576d9514c4085def57"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/12.safetensors",
+ "bytes": 4456040,
+ "sha256": "a5d354c48003a089291691bf56d4331706a472f648bb62912cc8af409c25578e"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/13.json",
+ "bytes": 103,
+ "sha256": "ee3d624e7d3f960c09a455bd3234c442e6bffccd5077c8fd386d943c73c4e02d"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/13.safetensors",
+ "bytes": 4456040,
+ "sha256": "4122d04f791a8016ff49dfd231882f74687b82b1c9a06a10a880ef59bf2a7d69"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/14.json",
+ "bytes": 103,
+ "sha256": "7b291de7d7c8e41af72c58365021eb056a522769f1f5469cb26215f528355185"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/14.safetensors",
+ "bytes": 4456040,
+ "sha256": "ee95aa1af4119eae75a1a0ccfd2025e027c86d8b7d2b3df3250c25a8bd138b74"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/15.json",
+ "bytes": 103,
+ "sha256": "46af01a80546e13333e5c953a7bd3357ad29753ab3feab23a3e9169c1dd3c039"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/15.safetensors",
+ "bytes": 4456040,
+ "sha256": "b89596e56262b0df121edb2326a28686440648ddcc1127412338a0d55d98899d"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/16.json",
+ "bytes": 103,
+ "sha256": "1032ebb5fd59ad41ec0e3dec041c97fd04eed75beacb0cb4b9226f8109ccde02"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/16.safetensors",
+ "bytes": 4456040,
+ "sha256": "1e7be09355ee83361a728cd36c8afb8ab8ab37ebe4b51b52f8229a4cb3d1f1ed"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/17.json",
+ "bytes": 103,
+ "sha256": "d233a342c828941dcfd5f2348b0ce913bc758b55d5c642504adb544bab98bb97"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/17.safetensors",
+ "bytes": 4456040,
+ "sha256": "78244289bb051cceb4dace8bce6e067142084cee5e8d553a762295d5cb9f334f"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/18.json",
+ "bytes": 103,
+ "sha256": "97c77fe5aa17f48076237ebb6f6d8544ede7cd8502f8d2ffd688961b2f9dedd5"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/18.safetensors",
+ "bytes": 4456040,
+ "sha256": "59ca9c3c668d2e9ed20604795a174ae6ddee215fee748e487dc82ee23b534ad2"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/19.json",
+ "bytes": 103,
+ "sha256": "3cf14069cf7c8a943966dd4e8c50097796027b916cd54c18d4c8e589295cd41f"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/19.safetensors",
+ "bytes": 4456040,
+ "sha256": "d0a9deb167d4ac5d7f411a5dd14909138b09c8061755f84e5e3f1378b9dcf894"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/20.json",
+ "bytes": 103,
+ "sha256": "d56ddff6182dae0858ebecf44f8f787858d9ad238eaf7a1563b4e7797323bb6b"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/20.safetensors",
+ "bytes": 4456040,
+ "sha256": "40feb488ac29ba6e99cf1fff1b9d4b9078a422449e648f24553d3ec93fee6530"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/21.json",
+ "bytes": 103,
+ "sha256": "069977435cb785cbfb0c3a64f7f21f6b25ad3db546e906808f2caf8bcae58742"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/21.safetensors",
+ "bytes": 4456040,
+ "sha256": "cd1a3c5737e3a82f15d79d3f37ba3df3c0430c3a6df832752826e786b6893b59"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/22.json",
+ "bytes": 103,
+ "sha256": "e5393d760b3a4c7637b7360fc900ae791c7186257fc9bac82f20f35eb80462da"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/22.safetensors",
+ "bytes": 4456040,
+ "sha256": "935c5019a9862e16f94920090040d7b09ba7ef786678ca453aacc91bd7e2d67e"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/23.json",
+ "bytes": 103,
+ "sha256": "a4f90e63f9d5f3d493922fd380f74eb69b58dbbb2e6862df9129d2687ffa15ae"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/23.safetensors",
+ "bytes": 4456040,
+ "sha256": "5215c662929c9a036155a21726d9b6775644bccd0cefaca79477b4c65fadf3b6"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/24.json",
+ "bytes": 103,
+ "sha256": "2aaa013e47a920bafc20e8b61095fd07f9d49bfd5e7e96b312cc4d7c0e6ef133"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/24.safetensors",
+ "bytes": 4456040,
+ "sha256": "8c651e18158f3c70e9e335b18962ee0a1f1843b6d040630dceb3e979440c657c"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/25.json",
+ "bytes": 103,
+ "sha256": "11cf276b0e9d79ea7336b63c9a0ac31aa09aec2687612024da948e11dc47f3d8"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/25.safetensors",
+ "bytes": 4456040,
+ "sha256": "530db57adcf090f8fe18e282373fca02fa3ad13820a2075c804344d6bad732ed"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/26.json",
+ "bytes": 103,
+ "sha256": "dc5db4988090533757f84bc254feb444b67a18f775cd7c4290f0004ae690bbaa"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/26.safetensors",
+ "bytes": 4456040,
+ "sha256": "de0e8a74e0b02a9d2122f0d461311b0f6b8645aef362ff37aab529ef92c3070a"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/27.json",
+ "bytes": 103,
+ "sha256": "35b1e9adc2427f82940094336727e241139044f6e2b98e12941566da2c6e1807"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/27.safetensors",
+ "bytes": 4456040,
+ "sha256": "51b073c5b5c60fa7154c94ac5e7e1c52fbdb9b1078c889d9a73654dbf6973141"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/28.json",
+ "bytes": 103,
+ "sha256": "0fd01ed5a6f838430fd4aa1a131ec57427c999a031acb3ecb40a1b100173f0a2"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/28.safetensors",
+ "bytes": 4456040,
+ "sha256": "6ea5a5e0449409acc444a0913d563118da5ead01020a89ad125b98f9bebacfdd"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/29.json",
+ "bytes": 103,
+ "sha256": "c13df43492f01fa37a048d1a7f93651dbffc5ed4aa7c7982fe002d22dfdea6e0"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/29.safetensors",
+ "bytes": 4456040,
+ "sha256": "a0c4032012c322636c3eb6b7450e54b9533aa2ed068f5508d39eb43ae85d5b9e"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/30.json",
+ "bytes": 103,
+ "sha256": "0d63f1bca1a2ec1ce1e276a9016f3c7bd953b9fee41ee7d7aea6c8f924db2b52"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/30.safetensors",
+ "bytes": 4456040,
+ "sha256": "97ca1ccf7058ffa78e9c1e189dd15be748b642f3a27a719d646a2c24d2c7a441"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/31.json",
+ "bytes": 103,
+ "sha256": "19a92b4de6d7f29bf5fe75cc1848d46729df61c72ec959d78958d4ed55741e5e"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/31.safetensors",
+ "bytes": 4456040,
+ "sha256": "3ed5af4d246b86a3476d5fc2c26f8d62a0d3b656e912842a5dfa062b4d9c8110"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/32.json",
+ "bytes": 103,
+ "sha256": "ef3e4309ab2218c75e7ce25e5c31275bc7c149517d7dd88e18b7e2cb136ea108"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/32.safetensors",
+ "bytes": 4456040,
+ "sha256": "a3ca9911208514fdffc270d697fd9b83fc9cc16acdbeac52cfe632eddbb399b5"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/33.json",
+ "bytes": 103,
+ "sha256": "cc38c662c44da10e78882b7e7aab6147cd84b08286004425be6deba9752752bc"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/33.safetensors",
+ "bytes": 4456040,
+ "sha256": "bb2206782ec88c8ec191f269ed39053c80199b4ca16397a036166b30998c6c00"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/34.json",
+ "bytes": 103,
+ "sha256": "07e21ca84b90eb7f3c193758e101b412cd6c7c5ca088754fa967f79948dfb583"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/34.safetensors",
+ "bytes": 4456040,
+ "sha256": "50b7ed03cdf37d7fcf504c5fa8cf04155d2c1d2e196f55b74eeec081cdf3a183"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/35.json",
+ "bytes": 103,
+ "sha256": "7bd4289495b52ad01de6512d3101dc8303b02f1398a079adc93ba3ada8b81198"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/35.safetensors",
+ "bytes": 4456040,
+ "sha256": "b9b4716db16d3c7e9cc858336c78035f9f768ffac8e65edbd1694769077f0940"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/36.json",
+ "bytes": 103,
+ "sha256": "a9b26ddf7e07b93186beebf2d2911ca42890eef6ce87d0fa8e64fbf42a835368"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/36.safetensors",
+ "bytes": 4456040,
+ "sha256": "4c273b5502e98cd1cc9450b95b6c5dc51ab3fe280458bc330fec629ea1a73119"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/37.json",
+ "bytes": 103,
+ "sha256": "d7577501165ec702c5a1c89336adfb38ae0e825d17cda6492d54d1f5f07b93ac"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/37.safetensors",
+ "bytes": 4456040,
+ "sha256": "f360e96b1e937c01890be5920e759ee1247b61c064bccd29788e17af8fbab8b5"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/38.json",
+ "bytes": 103,
+ "sha256": "1662ea2e2407ea033ffb6d89798de30915fdde34af816e757ef71257f5a9d232"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/38.safetensors",
+ "bytes": 4456040,
+ "sha256": "f0e9d4f1beb3630b929b21cd42bbc034d3ac511a6d043d211932fb3dc0ef11d7"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/39.json",
+ "bytes": 103,
+ "sha256": "c3b4f1cf9230fd68c39f88371b7b84e7bdd1dd3dcd06b7a9a43953271b2011e5"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/39.safetensors",
+ "bytes": 4456040,
+ "sha256": "594aa08ea6e0bef7e8a2796de5bac73c37995bdffc7606ad8dd4a291c77fbf2d"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/40.json",
+ "bytes": 103,
+ "sha256": "7f06906a48dd0be4f76b7db7c3366600aa771231b995dad056a05c0cc9d2ea5a"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/40.safetensors",
+ "bytes": 4456040,
+ "sha256": "cde25e4972edab376eb033fd5dbe5426fb0e6e53c428f4b1055e4a8ad6426b83"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/41.json",
+ "bytes": 103,
+ "sha256": "b66e7f7622a8835620bf38da55518215715f0ad5a153995668329dcdb941dfbb"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/41.safetensors",
+ "bytes": 4456040,
+ "sha256": "dca8e3a48637bc8325b0ea9443640c9e0274be695da6ab6da0a956b87f76e548"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/42.json",
+ "bytes": 103,
+ "sha256": "0bee835ba2ad2fd4955a22a3d133f11916c1dd4f718c20b4d4c428982e0926f5"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/42.safetensors",
+ "bytes": 4456040,
+ "sha256": "7ac8988dfbcab4ca00327f57d465d42b8d46731100d9a134e0f8df821692df6a"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/43.json",
+ "bytes": 103,
+ "sha256": "3d4390a37490e08e0745fc6bb471d1d2b8ebd513da99e7053356a9912334da79"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/43.safetensors",
+ "bytes": 4456040,
+ "sha256": "812e95c1bcbabc5de610263565ac6f2c2b58dfa0914f40185e23d220db8d7804"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/44.json",
+ "bytes": 103,
+ "sha256": "43830b47c2bfd74350574e9cecce467a2879acdadea63ed937462da8ab6fd4c8"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/44.safetensors",
+ "bytes": 4456040,
+ "sha256": "0f9e2d100da4f13603d0c039e06c4b6e7cb7a5bd60c96d686eb0f38af47d4702"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/45.json",
+ "bytes": 103,
+ "sha256": "204d32d3ceb7f2f520640673867ab62a4c51d2b9f076d328f1509b76c17f5ca2"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/45.safetensors",
+ "bytes": 4456040,
+ "sha256": "6d3135c8ed28bae050426ed074e86ad2a5a3352f43a2156dc4fd22992a5d7f8c"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/46.json",
+ "bytes": 103,
+ "sha256": "30dd134e38e4082c1f913d62636c52f0570f3810e2c0376f27c48d10efdbf65f"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/46.safetensors",
+ "bytes": 4456040,
+ "sha256": "fc2f7f62f2b1d170bb39fdf79d915286a6df287f387bc3608efa7c2d35240728"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/47.json",
+ "bytes": 103,
+ "sha256": "ffb19c1e9b3516309a79a1c2d46f1a22ad781895dd8ee3de995ba44f5ebd7223"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/47.safetensors",
+ "bytes": 4456040,
+ "sha256": "fc7a88951bf1957030413bb74a646f9b90b9b278486321eccc20b223d0216137"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/48.json",
+ "bytes": 103,
+ "sha256": "335b485652d0abcba0abf2af3366c4fc6523d6be3c56b2c6a16db135293f0441"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/48.safetensors",
+ "bytes": 4456040,
+ "sha256": "059fdd282e83c3ddff605d1b4ad3df1bac9711a02eed0c4a61dc1d86200fe0c2"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/49.json",
+ "bytes": 103,
+ "sha256": "a7a009e98a609361662c5499988c747aaf232a68fe9f1b0d65bf78ad2ba5dcb3"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/49.safetensors",
+ "bytes": 4456040,
+ "sha256": "ab8a45b126e168cc7ae3ba863a68419a9ca874ffedee961260a27722d162f616"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/identity.json",
+ "bytes": 931,
+ "sha256": "e25d680c1c6fe8942d8c063dee5304dadbb68023296cbd7ff7dae4daa115af66"
+ },
+ {
+ "file": "cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/producer.json",
+ "bytes": 673,
+ "sha256": "6f5d42ef915bc1e5b58a7def1048c93381705344a89306a2ca14fb4619c3948c"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/00.json",
+ "bytes": 103,
+ "sha256": "f9e6e6b8eb252c9aa82f858604d7dc8a7b5550c4f86b46c067f1a9eae36f50b9"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/00.safetensors",
+ "bytes": 2907752,
+ "sha256": "504965de57302f0b18a5ea0b01d021635f26d47feac6e07d72ef7225657893e3"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/01.json",
+ "bytes": 103,
+ "sha256": "667288e0dbdf9a2e4dd8c2edbbd4b440c32f05f53a5dcf95f0781ff8f73c9e24"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/01.safetensors",
+ "bytes": 2907752,
+ "sha256": "3933338a466004538a5279fb32b1ee92c7694bb45a5d8e442ec9988a89ff50bf"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/02.json",
+ "bytes": 103,
+ "sha256": "ee2abbfab26a5ff44891b78d13280b920bb9903ef20ea74cd24eec01c37669c8"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/02.safetensors",
+ "bytes": 2907752,
+ "sha256": "76e929aa66455d87b31db7352892b0bdd3b7b0827d7359796b67915ab168cbac"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/03.json",
+ "bytes": 103,
+ "sha256": "d874fb4f15f188faade2edbaa89051eec7ae524ce33de96c30c13f133cf1da55"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/03.safetensors",
+ "bytes": 2907752,
+ "sha256": "e46b70ffbd459f848e056e1f0cfa9bdc6787f1096628baefa04bbcb5a2c7e496"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/04.json",
+ "bytes": 103,
+ "sha256": "1b21fee106806e5391c35dbe38e1ef7cae164afc6afd1aadf7156a939b509463"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/04.safetensors",
+ "bytes": 2907752,
+ "sha256": "cca36815e9a66e63b9ef0cbdcf3ad27b0013d858cd8077b7aee8fdff349074cb"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/05.json",
+ "bytes": 103,
+ "sha256": "143e16c046d56cac85ea87c6ad07f6ffad28b6225ef7b333c6cd7100857cb31f"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/05.safetensors",
+ "bytes": 2907752,
+ "sha256": "91a93b8ebd7efb2b313cccdaadca4334c851cf91fde9ef15be7a2c2bd30fcaab"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/06.json",
+ "bytes": 103,
+ "sha256": "403be70cb9593e105e36901bac8ace3b30a4ad20802cf6edf822e0ff6e8a370e"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/06.safetensors",
+ "bytes": 2907752,
+ "sha256": "c0cdf2f5afdc975ea53e5bb519ced8796e91c1fcdc0ea95142e5075134e727a2"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/07.json",
+ "bytes": 103,
+ "sha256": "6104c665d45e661dd85e0a5298ab1d99cdb487cb0bbb08dbaba42b0feefac69d"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/07.safetensors",
+ "bytes": 2907752,
+ "sha256": "056e7f109ab6affd7d970d3b41288cef1648c68b2d4626c208b554b7cbb9c5c1"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/08.json",
+ "bytes": 103,
+ "sha256": "23fd49a92fd34a9fa429f3fa5ca824c8034ffc4ebf214a5aa6bc27c73f2df7fc"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/08.safetensors",
+ "bytes": 2907752,
+ "sha256": "616d1c9354fd077aee1b650ccadbb8646eca871bd023b20c313a96b4af21d175"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/09.json",
+ "bytes": 103,
+ "sha256": "78e1088d5210dac80c7681bbf3bdbcae4d54314bf033002007bb78d64b132c87"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/09.safetensors",
+ "bytes": 2907752,
+ "sha256": "7ef4add104c271dd30c7330573ed239648190d47fcec80a0dc5a892a9912ac45"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/10.json",
+ "bytes": 103,
+ "sha256": "3b47a8cf90f7ee2d7098b065ee4418a7182dee88497e1f312dbde645fef49e5d"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/10.safetensors",
+ "bytes": 2907752,
+ "sha256": "3dd6c92a39d73f3c6e20c62ce472b0ef13a78d39495a1434e19464e9ce8c26bb"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/11.json",
+ "bytes": 103,
+ "sha256": "8d74197e07d83725fc1fc35ca35512a7695c25d4e110091d8873ab1ca7afc069"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/11.safetensors",
+ "bytes": 2907752,
+ "sha256": "6e9cb9bd31dcfff944232584a170fe69ee7976dee3b08ec8d96ecfca76d5dae3"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/12.json",
+ "bytes": 103,
+ "sha256": "912a5e1701d8c6a47f94ab661cb789896f6bd58f11b9ade74aef6a6243496c93"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/12.safetensors",
+ "bytes": 2907752,
+ "sha256": "3259bb9332233444035ad0d7d638ec1b545970e61c75423ad03f80f8e0144930"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/13.json",
+ "bytes": 103,
+ "sha256": "3757baf3ce4b46d5446f703d364001f71faf3f4bb6a2695c44e54b8713722540"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/13.safetensors",
+ "bytes": 2907752,
+ "sha256": "3a37bf16fea5f480c76aef7d40a9fa81060315277e5fd4f4665f2b0a6873f665"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/14.json",
+ "bytes": 103,
+ "sha256": "8db7dc32ab897398f56ac03512ab34f5c5c0135f488c9560a684c8294bd085bb"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/14.safetensors",
+ "bytes": 2907752,
+ "sha256": "2c73459bb7162d80fbf26c8dd9b8ec86e70f738ab16b9af3c32271ae4906b4c6"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/15.json",
+ "bytes": 103,
+ "sha256": "aa61f9f24888d56ccb0bc2881bd153ff94711c385f7ef837e899ffcff66f2b65"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/15.safetensors",
+ "bytes": 2907752,
+ "sha256": "2ccd0199c9a4fbae4e2de5310458eeb82499d4af08bbed855e1b49c15976376c"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/16.json",
+ "bytes": 103,
+ "sha256": "d7561665bc1d3ed3e09b2da3afd4c2b4f041203d9f6563e26a90ff3d6af38c11"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/16.safetensors",
+ "bytes": 2907752,
+ "sha256": "2fc98d5537b68b075f03b08ad0672c761a8704fa9d6396274223ec8af5678c37"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/17.json",
+ "bytes": 103,
+ "sha256": "07184db3712a2bc45cc73c136cf6b3afb60aaa004f5f818e6eeb8822dc71d4ae"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/17.safetensors",
+ "bytes": 2907752,
+ "sha256": "1192769428faf5d9825387c74beba5382bdde6beb8954801eecc0edec64f999d"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/18.json",
+ "bytes": 103,
+ "sha256": "e199d6d72ce9daff49fe318bf7610301de7aa9d59d2a56db6feb514989e4de9a"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/18.safetensors",
+ "bytes": 2907752,
+ "sha256": "456f5adeb6dd619eed554fe5a875cd1cdcbeaad326284a403eede95ae51c477a"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/19.json",
+ "bytes": 103,
+ "sha256": "4638884349dc0fe19444c4e351ccc13ce841f1c02a98f86d17fb55f1bd6fd295"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/19.safetensors",
+ "bytes": 2907752,
+ "sha256": "9dedbc8647b730c39625ed3d5fa2a77748415fbeb1870cd91428522ed3bd6f53"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/20.json",
+ "bytes": 103,
+ "sha256": "4fa40d8a400ff82f8246b29fce691237a6150181c28d87d1932c3a39d3a22bcc"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/20.safetensors",
+ "bytes": 2907752,
+ "sha256": "298dd809a0f26089cf8d6ea1a6f140e343fa9d923c55a89842efe97f51d717b5"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/21.json",
+ "bytes": 103,
+ "sha256": "d41c0a23a803aff40774435d078ecae0b9fac1938dc6a76db2cd2cb8306b6efe"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/21.safetensors",
+ "bytes": 2907752,
+ "sha256": "17d9e0e93144063e28d77b4ec937c088a2657918e55dd8c2c5f382475d1a3d7e"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/22.json",
+ "bytes": 103,
+ "sha256": "21d6ac56d4f9e4481c6ff48d5f668c9928ff8b99848ad9d63d6a9c3a56059033"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/22.safetensors",
+ "bytes": 2907752,
+ "sha256": "89d6a15a3e9fca27681133ce2cf7d4f831aa0c53ace7923a8fcf2e0cf1d1809f"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/23.json",
+ "bytes": 103,
+ "sha256": "cbdaeb28f128d2f42919eab76a8f846b1917bc00edc0500c56815d51b3cb2665"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/23.safetensors",
+ "bytes": 2907752,
+ "sha256": "c1e77d41f521abbe35c124b46fb0636a3ef8c7ad4197bbfb95feaa00020ca6d3"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/24.json",
+ "bytes": 103,
+ "sha256": "810e04bca0e409c93ac24564c48ede12086c2eb5bfda8074aad8615e180edc86"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/24.safetensors",
+ "bytes": 2907752,
+ "sha256": "7c591cbafc2d41483bb6f9decd773ed274c5b4032a89888d600bcf1a46829a3b"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/25.json",
+ "bytes": 103,
+ "sha256": "0c0f112fc2b35f10ad0706d180be4f61e8136e19f07064d7472c9fedddfa31b0"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/25.safetensors",
+ "bytes": 2907752,
+ "sha256": "3e03ae0f82c3b2b7e4fe146bfbaa6605bc636823fcc5e282ffcb9abf7216deec"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/26.json",
+ "bytes": 103,
+ "sha256": "4c1061b6bffd9173aec8d113e5538a359dc4d5917d028997a41b50d93bffee23"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/26.safetensors",
+ "bytes": 2907752,
+ "sha256": "7ab35cc41e65d0431db53a06f4884c97eeb3f688ed841105b8dba4d58106f287"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/27.json",
+ "bytes": 103,
+ "sha256": "a30980f0c1671db04a68014a210b68dfc5b97bd71b7e4d570c74528a727bd4e1"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/27.safetensors",
+ "bytes": 2907752,
+ "sha256": "990084df00fc135b5456873456b7d6c2b31a389bec157cddfbfaff0dd5624e57"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/28.json",
+ "bytes": 103,
+ "sha256": "237bf4dacb03d5a36f7cb7c4e56ce90c35ff52e68285425bb1023089f63f4348"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/28.safetensors",
+ "bytes": 2907752,
+ "sha256": "3e7bf8dcdf9b98af8dee62e742ec02071e79c6fa6abe717767155fecf40dcae0"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/29.json",
+ "bytes": 103,
+ "sha256": "d65b87005794c9fee3f880a5b70126d9452df1165f72f2d2ad366e6a372a67fa"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/29.safetensors",
+ "bytes": 2907752,
+ "sha256": "348a7edf18bb195f0972abde268560f6b6d90acd0872dc1808650a2c5ed82b5b"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/30.json",
+ "bytes": 103,
+ "sha256": "52048b02c2bac938404e5a8e8ff2f65719269592bdf681d0753747e41c6f5326"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/30.safetensors",
+ "bytes": 2907752,
+ "sha256": "f7dc17603c0b257e480479fcb45b35dc261099cbf9f1c08e5c72f83b82f1328c"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/31.json",
+ "bytes": 103,
+ "sha256": "c6ad32ea9a7c5b56456436563f007b84e481270a704ac7439be25de4676f4837"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/31.safetensors",
+ "bytes": 2907752,
+ "sha256": "9af528ffbd8e1ea01a7f183cce0a30c561add5bca4d095e1826a1ffff935cf51"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/32.json",
+ "bytes": 103,
+ "sha256": "b45013ed66d521ab7a4b8e7a8a86bb47f571d19c3ed5c85adf1116189cfb8260"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/32.safetensors",
+ "bytes": 2907752,
+ "sha256": "2b49977f572aa9b4efcd2f6da30d8125ddfc2f84604a821283791790856bfebd"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/33.json",
+ "bytes": 103,
+ "sha256": "990ef6d77bbc989b9bd14d37ba1f644eaf36fff8dd65461f9f72fa84d8b0fa9c"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/33.safetensors",
+ "bytes": 2907752,
+ "sha256": "0d842ee71fd2ae4d911a09aacdf5984fe6ac8665ebc4f0b77b867abf39e9ec08"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/34.json",
+ "bytes": 103,
+ "sha256": "29b152efa3120d46cf7c26eaf201b246028488023c85efb3baf968576708f3dd"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/34.safetensors",
+ "bytes": 2907752,
+ "sha256": "763c6e88f93285283f4f7d7aa60ac9c5863d3436080404384599f4a5eb051e8b"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/35.json",
+ "bytes": 103,
+ "sha256": "e0054f4705847c3b611101d855e0039be9fcbf31b5db8160a2ee1f24d0a63799"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/35.safetensors",
+ "bytes": 2907752,
+ "sha256": "a3addbac96f3444e752ce21e8279deff0806633ae08e4a7c676b40c876bc366a"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/36.json",
+ "bytes": 103,
+ "sha256": "a47f7adfdf1fd286a64098263fa5dee759fde7ae6c49efe3267a53a4f4d4efae"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/36.safetensors",
+ "bytes": 2907752,
+ "sha256": "006c53def3a3af3b6cfdc5e4c3a98d122a5af2b8f88601ab9de804a38c559b84"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/37.json",
+ "bytes": 103,
+ "sha256": "f9272e762e683689bac14b2f9ad9b8cb72453ebad82312cc336358100cb623d8"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/37.safetensors",
+ "bytes": 2907752,
+ "sha256": "5a5a07ca72d25c3304df588aeb24e5ed81ca9ff029e398f3965e14e53ff5dc4f"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/38.json",
+ "bytes": 103,
+ "sha256": "a83d91338fb3003d1fd2135d18c3db27336d698c4e6d3d48ca7dae46cdff69b8"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/38.safetensors",
+ "bytes": 2907752,
+ "sha256": "52d97b816432d3036f44f551532ca3e1703c3d00a57a23b42e1bcb3f3992790d"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/39.json",
+ "bytes": 103,
+ "sha256": "d7d4cc02a77e8ed4d22c9ad13d62f8c3fc65fb27bec04de1bbfaa7fcaf487754"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/39.safetensors",
+ "bytes": 2907752,
+ "sha256": "fce501da767380acdcdc207a57e4fd320424b89593ae4f2a6dabfa21aa3de778"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/40.json",
+ "bytes": 103,
+ "sha256": "31c0ad0dde0029f66701774c819e5f3194e2ad8d30901d1936c92f79fafa54bf"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/40.safetensors",
+ "bytes": 2907752,
+ "sha256": "99943d87d442079dcd0188c125ee625923d126ed2170a798ca79224dd13fc6bc"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/41.json",
+ "bytes": 103,
+ "sha256": "0a5fcac7bd22c378fef0e3241d02be62e5c0fff38af41700bcf4ed2cb858c781"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/41.safetensors",
+ "bytes": 2907752,
+ "sha256": "f9cd78e7d7eb32236cc9d23136298e7957aedb7b86d54725f953fb4998a7b696"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/42.json",
+ "bytes": 103,
+ "sha256": "1b8afea6c9d080d637e7d6051612774803e602ebce550bb37c9b785e464ef3e6"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/42.safetensors",
+ "bytes": 2907752,
+ "sha256": "4e40a578184f9f5eb20698ed5e21dcf9245cf8e5bf519bd1457059facb59f780"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/43.json",
+ "bytes": 103,
+ "sha256": "69c73f5564ae252170f99aec65d4cc1bf1720f1745492f09717e1f383fe5fe22"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/43.safetensors",
+ "bytes": 2907752,
+ "sha256": "fc6928999a88ca8e7e952bd3156971ff83515332b77beb6a32219c14e218d455"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/44.json",
+ "bytes": 103,
+ "sha256": "7501a07a200d4203f29c283609ddc9d3ba3f2bf27026c2b2dffaa7632a25a666"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/44.safetensors",
+ "bytes": 2907752,
+ "sha256": "f530a54d19af7d107d51753a6b7a5a421ddc5886a37adfaae18ea50c189d0675"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/45.json",
+ "bytes": 103,
+ "sha256": "839e60a4534ef68e27f3ac0877dfebb006f69ca5056e784d900c7a85dee645fb"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/45.safetensors",
+ "bytes": 2907752,
+ "sha256": "c7684401bf33f5ae9014126848148c1864c7003a191c93f1ca4ed5d4cb13b429"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/46.json",
+ "bytes": 103,
+ "sha256": "230f820c14d1846b0b395aa869c51e2221091d7e9655077f3a5254af47d5bcb0"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/46.safetensors",
+ "bytes": 2907752,
+ "sha256": "b71a67fd54a45ee9d74c808940cb99aa8b9f4a0253220e03e0ff2bd0095df33f"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/47.json",
+ "bytes": 103,
+ "sha256": "fc5b77e56e0fdb1eb82859cbee5c03d1038466e2827959cff535410ad6f7ebd2"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/47.safetensors",
+ "bytes": 2907752,
+ "sha256": "c242a36f612c5bc688eb77b3cdcaf44e6cf0f739234e1358c4a7c1747b85e2d3"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/48.json",
+ "bytes": 103,
+ "sha256": "70fc8a96a8958d7e7c1ad855d8bb577c62c342db18321e886a1b209f0ef3904e"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/48.safetensors",
+ "bytes": 2907752,
+ "sha256": "59ca5ce7c1b6814df2c2cdf68117b670231c07c6ff9a4ff5dfd5d235acdade24"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/49.json",
+ "bytes": 103,
+ "sha256": "76b3524064769d7635c6587e960fd4f6d3bc5c5ef672ed8c8ad5cafe61c31c0f"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/49.safetensors",
+ "bytes": 2907752,
+ "sha256": "842557f5218c789c35c186a42ba718e82dc2bad0c36edc1b4c8e4fc4a3ea75a8"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/identity.json",
+ "bytes": 723,
+ "sha256": "6ee4ab1773216749667fcabd2c1f2e25e52b71b21a060a22d133f52858395160"
+ },
+ {
+ "file": "cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/producer.json",
+ "bytes": 642,
+ "sha256": "4dbd4576fe79b8cabe681d8ddb2706f4d07be12e3f5ca6670ee74beeed20af85"
+ },
+ {
+ "file": "cache/blocks/00.safetensors",
+ "bytes": 432433020,
+ "sha256": "f27579555aec86044db84f8ac2157ecb25e3f29146d6013c44f3518f05e06702"
+ },
+ {
+ "file": "cache/blocks/01.safetensors",
+ "bytes": 432433020,
+ "sha256": "b649f5873b3da6a5af218993cdbe60ea108df8f2b1f4ca16eda0fdedd896c73a"
+ },
+ {
+ "file": "cache/blocks/02.safetensors",
+ "bytes": 432433020,
+ "sha256": "6c45758f2b7b32d7d6e2319902c34337d0727fda582f116fbb000c8d2efdec6c"
+ },
+ {
+ "file": "cache/blocks/03.safetensors",
+ "bytes": 432433020,
+ "sha256": "880779d9493bf25ace0957a225970da78060b1a9669f507c1a2ca6a93bf9c529"
+ },
+ {
+ "file": "cache/blocks/04.safetensors",
+ "bytes": 432433020,
+ "sha256": "b4e23bac7cd3327651f7f8d25fdbec1c71ce388cdedcb3ec07d71575539d0069"
+ },
+ {
+ "file": "cache/blocks/05.safetensors",
+ "bytes": 432433020,
+ "sha256": "d3a5922d26514df706f280c0e885700bcaeb0e2596a29de00920f62206cca726"
+ },
+ {
+ "file": "cache/blocks/06.safetensors",
+ "bytes": 432433020,
+ "sha256": "bee8100f80f9463bfa1c618db78250fa1c246fd7c024ede67270bd4c10621215"
+ },
+ {
+ "file": "cache/blocks/07.safetensors",
+ "bytes": 432433020,
+ "sha256": "1b1fca4bbadf7c0d97ed7bef37d85d9ba5b0316fb336b6c29e8f5947c1d80c49"
+ },
+ {
+ "file": "cache/blocks/08.safetensors",
+ "bytes": 432433020,
+ "sha256": "00b2e86852c6d5d0733eff269ad0013bdb672706b8a3c3847a39700bf2aa0d54"
+ },
+ {
+ "file": "cache/blocks/09.safetensors",
+ "bytes": 432433020,
+ "sha256": "8b9274d911f5b319e2a842fa58a4c629d19961b9785ac841d176dd71e6dfbb9c"
+ },
+ {
+ "file": "cache/blocks/10.safetensors",
+ "bytes": 432433052,
+ "sha256": "23eb6e5724584925385780d995130e06f3d7a56847950e54b2ab7d6d3e25b043"
+ },
+ {
+ "file": "cache/blocks/11.safetensors",
+ "bytes": 432433052,
+ "sha256": "edef0733f4be87d9606f04a6f2ad605d682c92f78da7c39bf926d059bdbb7991"
+ },
+ {
+ "file": "cache/blocks/12.safetensors",
+ "bytes": 432433052,
+ "sha256": "28230556a483530159904d8c164178d0b711886b0800c120602fcf190d50d50f"
+ },
+ {
+ "file": "cache/blocks/13.safetensors",
+ "bytes": 432433052,
+ "sha256": "2a87ec0ce9b3c929f57bec4e429ee2482468c24d4c5ea7e681ef05a0f5bd4acb"
+ },
+ {
+ "file": "cache/blocks/14.safetensors",
+ "bytes": 432433052,
+ "sha256": "753ed8b2bc78a85a017176b9b3893dd84318f81a994feff1cd68bfbcd33597bf"
+ },
+ {
+ "file": "cache/blocks/15.safetensors",
+ "bytes": 432433052,
+ "sha256": "f07f4dffad68abbc8fa080740cad96d3a5f8ed41748ca58895731ad507ea6b45"
+ },
+ {
+ "file": "cache/blocks/16.safetensors",
+ "bytes": 432433052,
+ "sha256": "d3ceee15b0a61360d9960b49f4f75fec9a18dd550301d5503a457dc8bbb55dab"
+ },
+ {
+ "file": "cache/blocks/17.safetensors",
+ "bytes": 432433052,
+ "sha256": "0e50643126416b79405d985f8c28fa343004f727c971caf913d6a1e62b1a8fdb"
+ },
+ {
+ "file": "cache/blocks/18.safetensors",
+ "bytes": 432433052,
+ "sha256": "d94d7e4749fbc57f78b0052b05e0b9edc698d98aaf323afeb335a6e54aaf4d75"
+ },
+ {
+ "file": "cache/blocks/19.safetensors",
+ "bytes": 432433052,
+ "sha256": "a7a4bc4355c8826f6fdc2f8f2186ac9adebec3796e7d6cc66a9a1041075794af"
+ },
+ {
+ "file": "cache/blocks/20.safetensors",
+ "bytes": 432433052,
+ "sha256": "58be531a579178f91581c43c869540eea7b93d77bcaf981e0edd6ea8500c0c7d"
+ },
+ {
+ "file": "cache/blocks/21.safetensors",
+ "bytes": 432433052,
+ "sha256": "212b3899a5fba7a52c37b5307a7b3cdafaf15b9f8481bc75faf9f3ebfa430cfa"
+ },
+ {
+ "file": "cache/blocks/22.safetensors",
+ "bytes": 432433052,
+ "sha256": "646d11ea3daf00c52c0eda558dc557b8b97a4baa4f8e97d267e9d354da42ec9c"
+ },
+ {
+ "file": "cache/blocks/23.safetensors",
+ "bytes": 432433052,
+ "sha256": "c0f7d3e287f27fee123414c53aba3cda96a4347fd2b2fe1edce6b5e01755d988"
+ },
+ {
+ "file": "cache/blocks/24.safetensors",
+ "bytes": 432433052,
+ "sha256": "045337814367e50bab107ca30f3ee4c343199e1e64e40af095822599095f21d1"
+ },
+ {
+ "file": "cache/blocks/25.safetensors",
+ "bytes": 432433052,
+ "sha256": "dcb426d4790c5ba428ad0db6518b0e2be0af927f4be368d191fcae850cfc4e14"
+ },
+ {
+ "file": "cache/blocks/26.safetensors",
+ "bytes": 432433052,
+ "sha256": "89e1aa6197993f6deca7b19632a8ec7a04ce9bb7729c83dbce3130ed1130ecd5"
+ },
+ {
+ "file": "cache/blocks/27.safetensors",
+ "bytes": 432433052,
+ "sha256": "f8735df68b94bc7b6fdaa5b4b5e22d6b6fa7162fcddb5197aa3b07f83e01f0b9"
+ },
+ {
+ "file": "cache/blocks/28.safetensors",
+ "bytes": 432433052,
+ "sha256": "5e7176e3bd5512ffc201800bc00eda6fab495370000eea2801ef7ce0ece9662d"
+ },
+ {
+ "file": "cache/blocks/29.safetensors",
+ "bytes": 432433052,
+ "sha256": "8e18b701af846a0a6f7289500bff14898694ff5b77ab0626eb0a1a8d6e7b3f2f"
+ },
+ {
+ "file": "cache/blocks/30.safetensors",
+ "bytes": 432433052,
+ "sha256": "ea0a644564d272b7d2737d0a3748ad9d2441ccab850856c539e5e355768111a4"
+ },
+ {
+ "file": "cache/blocks/31.safetensors",
+ "bytes": 432433052,
+ "sha256": "6f6d5b77dd5845eb40e197c8c8aac2890967ac8e10f6013851fb7c033e744065"
+ },
+ {
+ "file": "cache/blocks/32.safetensors",
+ "bytes": 432433052,
+ "sha256": "9a70c0095e155630d6cacf73dfb70bfa913417fe7b1b8508de5d82cdcf260b09"
+ },
+ {
+ "file": "cache/blocks/33.safetensors",
+ "bytes": 432433052,
+ "sha256": "03f5bdb472307d507549c5062b1f6011c5dd8ae442ecabf131d0cbdb6994b8b0"
+ },
+ {
+ "file": "cache/blocks/34.safetensors",
+ "bytes": 432433052,
+ "sha256": "12b328e282c7a348a8a7b8af68590cd9da87324227f813c5ec289fba09d6027d"
+ },
+ {
+ "file": "cache/blocks/35.safetensors",
+ "bytes": 432433052,
+ "sha256": "4e04c13ce4439b3ac7662022fe86710e7a8ad5701360278b23177f9e6705137c"
+ },
+ {
+ "file": "cache/blocks/36.safetensors",
+ "bytes": 432433052,
+ "sha256": "58d098da1caf31074b50a03067dda372628664ab46bc9b30c4fb6d65e20f378f"
+ },
+ {
+ "file": "cache/blocks/37.safetensors",
+ "bytes": 432433052,
+ "sha256": "907fb46ac61c46030e647f5e1926125c02f00f23b4534fd91043f6cf350378db"
+ },
+ {
+ "file": "cache/blocks/38.safetensors",
+ "bytes": 432433052,
+ "sha256": "dec9ccd22807dd8b011fab20c5c8dcc7710ce854fe293201a41c6cc17ab8a9ff"
+ },
+ {
+ "file": "cache/blocks/39.safetensors",
+ "bytes": 432433052,
+ "sha256": "1c86ce8d1ec6f4cfe8aa0d21a5d5944e9433468ce7bc7f33a85476e22e60559f"
+ },
+ {
+ "file": "cache/blocks/40.safetensors",
+ "bytes": 432433052,
+ "sha256": "9dcc34128a282bed2cf7a1195df698a31ef8e01f4f68d86d0e23215a853cac64"
+ },
+ {
+ "file": "cache/blocks/41.safetensors",
+ "bytes": 432433052,
+ "sha256": "471eca273bf5de3d45e882f2a41d7a2f99485d129fddbfb5c9b5ba0b464d7e9d"
+ },
+ {
+ "file": "cache/blocks/42.safetensors",
+ "bytes": 432433052,
+ "sha256": "8e644e63166bee0fa81d3316bf75cc29fa40352853f5ea8a42ec59508384d609"
+ },
+ {
+ "file": "cache/blocks/43.safetensors",
+ "bytes": 432433052,
+ "sha256": "703682d5756521aab6be654c9706624fb298990b904e4508741dba2858017729"
+ },
+ {
+ "file": "cache/blocks/44.safetensors",
+ "bytes": 432433052,
+ "sha256": "659ab9dd1571cc65d864cdbe94ea71e65633d59d35356c31cf4d0a91c3c06601"
+ },
+ {
+ "file": "cache/blocks/45.safetensors",
+ "bytes": 432433052,
+ "sha256": "599be1c8a6460120c939e769cf8136f4329bd2c56e3ded27119e9377b0f3bd22"
+ },
+ {
+ "file": "cache/blocks/46.safetensors",
+ "bytes": 432433052,
+ "sha256": "04da82ad84d8cde2bf31e4aa67ecfa6cf9f1acb55b53cec37b4898a2c3c1976d"
+ },
+ {
+ "file": "cache/blocks/47.safetensors",
+ "bytes": 432433052,
+ "sha256": "ae5b633c87441db4bc41cfaf5647953bdd0a8a58362357b161ec2772648cf8ae"
+ },
+ {
+ "file": "cache/blocks/48.safetensors",
+ "bytes": 432433052,
+ "sha256": "fc3c18b5ddd3364b4b428f0ec509fb31b1ff041f3164018a9e7a1cc045feb7e5"
+ },
+ {
+ "file": "cache/blocks/49.safetensors",
+ "bytes": 432433052,
+ "sha256": "f2857d4ae1b6266a8ad254daf0cdd182d93cd9799af15b5d1a1cfd125b953e25"
+ },
+ {
+ "file": "cache/manifest.json",
+ "bytes": 213349,
+ "sha256": "ad339c92679300fd43f57a7443d97b7392cce9910237c414ce32976509b18bf7"
+ },
+ {
+ "file": "cache/root.safetensors",
+ "bytes": 925027500,
+ "sha256": "f7af27041076fa2a563d49f3e3c7bdc0cd81438f0302f21d6ca1763be5e1a67f"
+ },
+ {
+ "file": "config/h3-base/audio_scheduler/scheduler_config.json",
+ "bytes": 96,
+ "sha256": "804780f7133477067bd6bbfbc02dc8b3cf9feeb400f97c08f5b1d5f6cbab3840"
+ },
+ {
+ "file": "config/h3-base/audio_vae/config.json",
+ "bytes": 2271,
+ "sha256": "9a3c645ff892b376c6f5f4c8685964cd75474731af594ff058492a0000caabb6"
+ },
+ {
+ "file": "config/h3-base/model_index.json",
+ "bytes": 2936,
+ "sha256": "5a587fe13b2371427415ac892463142683aefcd8d322e274a3a095eac37ac7d2"
+ },
+ {
+ "file": "config/h3-base/modular_model_index.json",
+ "bytes": 2935,
+ "sha256": "a2b6a210e482ffb78e613b553f570c44e101afce6741bd4ed91429d0559af031"
+ },
+ {
+ "file": "config/h3-base/scheduler/scheduler_config.json",
+ "bytes": 97,
+ "sha256": "8fa6c3aa70dc9e691e1a6df899fd1b6f75f70481a27cee6e18a303817075c304"
+ },
+ {
+ "file": "config/h3-base/transformer/config.json",
+ "bytes": 546,
+ "sha256": "74c11bff524336576096993cbfcdcdc2ef4fa2fa4409df693bdcbc6c666282ae"
+ },
+ {
+ "file": "config/h3-base/transformer/diffusion_pytorch_model.safetensors.index.json",
+ "bytes": 64488,
+ "sha256": "ac30a3b58963f2e735d493475fbb81853a5735ec947619648b3e045acda6783e"
+ },
+ {
+ "file": "config/h3-base/vae/config.json",
+ "bytes": 2011,
+ "sha256": "78f67deec3d63aae807f2bfe7154bc1e26f6372cb20b63265fcbae1b62bb5745"
+ },
+ {
+ "file": "config/h3-base/vae/diffusion_pytorch_model.safetensors.index.json",
+ "bytes": 74228,
+ "sha256": "15f6d44553c3c616b0dc999920aa784f92ecee7e4201f1f99ac405cfbf3061ca"
+ },
+ {
+ "file": "config/stage-dmd-step-250/adapters/default/adapter_config.json",
+ "bytes": 415,
+ "sha256": "89e0ff8920b9629b826eb99ab6150cce6924a53aa445186d1b493225fd091b96"
+ },
+ {
+ "file": "config/stage-dmd-step-250/adapters/turbo/adapter_config.json",
+ "bytes": 22264,
+ "sha256": "627968f670747c29cd7a0d3f8c75166e501d70f9e829b5cd3242a8f14cefbc18"
+ },
+ {
+ "file": "config/stage-dmd-step-250/linear_branch/config.json",
+ "bytes": 465,
+ "sha256": "decb06ac7e664610f677fb445318502b3c51f9c1b8603a2cdd00b16042be5bc8"
+ },
+ {
+ "file": "config/stage-dmd-step-250/metadata.json",
+ "bytes": 463,
+ "sha256": "54054ceb1c91b3fdf7fa0278e4a8841c127e8cf666b5e240d69613661f9d3e9e"
+ },
+ {
+ "file": "config/stage-dmd-step-250/model_spec.json",
+ "bytes": 25705,
+ "sha256": "4171f4384e952f1f73467981a893440c03298af4956b947e2c8a857ba9f5a62b"
+ }
+ ]
+ },
+ "rowwise": {
+ "cache_prefix": "rowwise/cache",
+ "revision": "ae041e5aec51f8516a3416bd86d579cb29c4c796",
+ "ready_revision": "28fc57d155e8f713b8cbea44c299dfea7f5706d5",
+ "files": [
+ {
+ "file": "LICENSE",
+ "bytes": 17604,
+ "sha256": "59b99642b95ea21630e311198ddbfffbfe05aadba0c2f5d884cbdf4efcc90f44"
+ },
+ {
+ "file": "MODIFICATIONS.md",
+ "bytes": 1813,
+ "sha256": "fe13fc06b364ca13861cf10644d923e91fc98b26eb8963acb3e288f94b7bb2ca"
+ },
+ {
+ "file": "NOTICE",
+ "bytes": 2320,
+ "sha256": "6b6671af343af83d5c8bff1b13875e9fe8ffd338f43d4343fa9efa96f49cf81f"
+ },
+ {
+ "file": "OPENVDN-CODE-LICENSE",
+ "bytes": 11358,
+ "sha256": "cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30"
+ },
+ {
+ "file": "README.md",
+ "bytes": 5691,
+ "sha256": "695ab321638eaa4c562a2459d5886d664ab60df97c3639d4e62a03302a2075da"
+ },
+ {
+ "file": "SHA256SUMS",
+ "bytes": 64864,
+ "sha256": "041b9c02f324e47952901b306876f1137b62e9ac5707798d669c177831747f75"
+ },
+ {
+ "file": "bundle.json",
+ "bytes": 246317,
+ "sha256": "61723f4193be73eba18216f8bb441b30444fc9912cdfb36dd6b05e6067875c51"
+ },
+ {
+ "file": "config/h3-base/audio_scheduler/scheduler_config.json",
+ "bytes": 96,
+ "sha256": "804780f7133477067bd6bbfbc02dc8b3cf9feeb400f97c08f5b1d5f6cbab3840"
+ },
+ {
+ "file": "config/h3-base/audio_vae/config.json",
+ "bytes": 2271,
+ "sha256": "9a3c645ff892b376c6f5f4c8685964cd75474731af594ff058492a0000caabb6"
+ },
+ {
+ "file": "config/h3-base/model_index.json",
+ "bytes": 2936,
+ "sha256": "5a587fe13b2371427415ac892463142683aefcd8d322e274a3a095eac37ac7d2"
+ },
+ {
+ "file": "config/h3-base/modular_model_index.json",
+ "bytes": 2935,
+ "sha256": "a2b6a210e482ffb78e613b553f570c44e101afce6741bd4ed91429d0559af031"
+ },
+ {
+ "file": "config/h3-base/scheduler/scheduler_config.json",
+ "bytes": 97,
+ "sha256": "8fa6c3aa70dc9e691e1a6df899fd1b6f75f70481a27cee6e18a303817075c304"
+ },
+ {
+ "file": "config/h3-base/transformer/config.json",
+ "bytes": 546,
+ "sha256": "74c11bff524336576096993cbfcdcdc2ef4fa2fa4409df693bdcbc6c666282ae"
+ },
+ {
+ "file": "config/h3-base/transformer/diffusion_pytorch_model.safetensors.index.json",
+ "bytes": 64488,
+ "sha256": "ac30a3b58963f2e735d493475fbb81853a5735ec947619648b3e045acda6783e"
+ },
+ {
+ "file": "config/h3-base/vae/config.json",
+ "bytes": 2011,
+ "sha256": "78f67deec3d63aae807f2bfe7154bc1e26f6372cb20b63265fcbae1b62bb5745"
+ },
+ {
+ "file": "config/h3-base/vae/diffusion_pytorch_model.safetensors.index.json",
+ "bytes": 74228,
+ "sha256": "15f6d44553c3c616b0dc999920aa784f92ecee7e4201f1f99ac405cfbf3061ca"
+ },
+ {
+ "file": "config/stage-dmd-step-250/adapters/default/adapter_config.json",
+ "bytes": 415,
+ "sha256": "89e0ff8920b9629b826eb99ab6150cce6924a53aa445186d1b493225fd091b96"
+ },
+ {
+ "file": "config/stage-dmd-step-250/adapters/turbo/adapter_config.json",
+ "bytes": 22264,
+ "sha256": "627968f670747c29cd7a0d3f8c75166e501d70f9e829b5cd3242a8f14cefbc18"
+ },
+ {
+ "file": "config/stage-dmd-step-250/linear_branch/config.json",
+ "bytes": 465,
+ "sha256": "decb06ac7e664610f677fb445318502b3c51f9c1b8603a2cdd00b16042be5bc8"
+ },
+ {
+ "file": "config/stage-dmd-step-250/metadata.json",
+ "bytes": 463,
+ "sha256": "54054ceb1c91b3fdf7fa0278e4a8841c127e8cf666b5e240d69613661f9d3e9e"
+ },
+ {
+ "file": "config/stage-dmd-step-250/model_spec.json",
+ "bytes": 25705,
+ "sha256": "4171f4384e952f1f73467981a893440c03298af4956b947e2c8a857ba9f5a62b"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/00.json",
+ "bytes": 103,
+ "sha256": "9f4e7a1a78227586e374f8400e5e43691d7acddea4a7025b8285faa29e72a166"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/00.safetensors",
+ "bytes": 4456040,
+ "sha256": "5f8393a55e4508b4f03a2325848cf74fa854dfdf84a0d949e9614079acd2bf80"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/01.json",
+ "bytes": 103,
+ "sha256": "df9828c32a00ad365559189433b3f9fb00fa5eb5ba8305479c8ad19f4e16e12e"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/01.safetensors",
+ "bytes": 4456040,
+ "sha256": "77bb900f64a824f59337498db5734c60f5c63517dca576c1b41a08db81cdc122"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/02.json",
+ "bytes": 103,
+ "sha256": "a1d674d4625648e4bfa7d49c41ccb1df365bf8547c2a838e29b3ec1ec6773ac0"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/02.safetensors",
+ "bytes": 4456040,
+ "sha256": "14e9486da0c80467e24aeb11ac95c0cef3c8de3ce6726d65c4568ab5a50c1011"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/03.json",
+ "bytes": 103,
+ "sha256": "b57783199d8bcb461b988fb4d1c3099493a8a002bb6c451f291942269887b9e8"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/03.safetensors",
+ "bytes": 4456040,
+ "sha256": "4d3bad61fc4cb241a4c4ffca7be68fdcf6d39df2d92696ee509a84f35cb2d7fa"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/04.json",
+ "bytes": 103,
+ "sha256": "c01364b637d1e4b287c063e236e1537f293bb4f0f10b624b2ff0b8aef019526d"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/04.safetensors",
+ "bytes": 4456040,
+ "sha256": "6d70b16a07e8567cb46662515d50ace7cadda46a1e261d209a6c87fad9fe7526"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/05.json",
+ "bytes": 103,
+ "sha256": "53dfefeaffe68c2c3cc4690189d0e55ff7048019cfa99536f2a089f954bc0c8e"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/05.safetensors",
+ "bytes": 4456040,
+ "sha256": "9a24951daec182dddb6ce26bd6d126f61dc6d2bf18b8b79d4a70acf58a4170e9"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/06.json",
+ "bytes": 103,
+ "sha256": "430fcab5a7d27589337f052f453a185c96ad863b86ab4539ec0bf140fbf811e2"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/06.safetensors",
+ "bytes": 4456040,
+ "sha256": "85914eb7fce42adcd50099cea5b94ce2bad2b5cb79801a35ee50a9937adc2417"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/07.json",
+ "bytes": 103,
+ "sha256": "8c7b6a8f88b924d7bddb0e7065dc18c8647911fe2d9bca6af135801fc8a59bd3"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/07.safetensors",
+ "bytes": 4456040,
+ "sha256": "70c91870a43f75433a138d1b25512101411d9be9b8cb0ea698c80d608fdf35ce"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/08.json",
+ "bytes": 103,
+ "sha256": "0657f0bf73d8e83a389f90036399fd709d5cf54be0e125b5014016b77e8912ec"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/08.safetensors",
+ "bytes": 4456040,
+ "sha256": "6ea4f9049e4d122f4b2a3605693303a4d641eee51761a40c2051cbe55e4a272e"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/09.json",
+ "bytes": 103,
+ "sha256": "bfb87bf7c84f37b28a46c8c7dd0c3b234e859f472c50b4d8bbc92098cf159f9a"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/09.safetensors",
+ "bytes": 4456040,
+ "sha256": "27cf9a8e782978097d4f2f03dd1850c7fab6ca5fd26b07c862ff0ea2ca38f434"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/10.json",
+ "bytes": 103,
+ "sha256": "5bec30a89a118f2df88647854e21cbebdbdce4aaea94c946e7eec2268e1c97eb"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/10.safetensors",
+ "bytes": 4456040,
+ "sha256": "c1ec60a934fc204428cd9024055d00f7fa47d3559df59d2d7027ebf7ccb7701e"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/11.json",
+ "bytes": 103,
+ "sha256": "be4c002eb1dc8147b97e4d26bdfae0454a48a429658832efa84a5664cf4d72e3"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/11.safetensors",
+ "bytes": 4456040,
+ "sha256": "b8e355d21647fa4e469751adaea39f06d62ed199a2992ac72ac5c90770d1d568"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/12.json",
+ "bytes": 103,
+ "sha256": "6b00e52bd9cd5a31bf8874f1f15354e5b9bc61e4b1da57576d9514c4085def57"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/12.safetensors",
+ "bytes": 4456040,
+ "sha256": "a5d354c48003a089291691bf56d4331706a472f648bb62912cc8af409c25578e"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/13.json",
+ "bytes": 103,
+ "sha256": "ee3d624e7d3f960c09a455bd3234c442e6bffccd5077c8fd386d943c73c4e02d"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/13.safetensors",
+ "bytes": 4456040,
+ "sha256": "4122d04f791a8016ff49dfd231882f74687b82b1c9a06a10a880ef59bf2a7d69"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/14.json",
+ "bytes": 103,
+ "sha256": "7b291de7d7c8e41af72c58365021eb056a522769f1f5469cb26215f528355185"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/14.safetensors",
+ "bytes": 4456040,
+ "sha256": "ee95aa1af4119eae75a1a0ccfd2025e027c86d8b7d2b3df3250c25a8bd138b74"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/15.json",
+ "bytes": 103,
+ "sha256": "46af01a80546e13333e5c953a7bd3357ad29753ab3feab23a3e9169c1dd3c039"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/15.safetensors",
+ "bytes": 4456040,
+ "sha256": "b89596e56262b0df121edb2326a28686440648ddcc1127412338a0d55d98899d"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/16.json",
+ "bytes": 103,
+ "sha256": "1032ebb5fd59ad41ec0e3dec041c97fd04eed75beacb0cb4b9226f8109ccde02"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/16.safetensors",
+ "bytes": 4456040,
+ "sha256": "1e7be09355ee83361a728cd36c8afb8ab8ab37ebe4b51b52f8229a4cb3d1f1ed"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/17.json",
+ "bytes": 103,
+ "sha256": "d233a342c828941dcfd5f2348b0ce913bc758b55d5c642504adb544bab98bb97"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/17.safetensors",
+ "bytes": 4456040,
+ "sha256": "78244289bb051cceb4dace8bce6e067142084cee5e8d553a762295d5cb9f334f"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/18.json",
+ "bytes": 103,
+ "sha256": "97c77fe5aa17f48076237ebb6f6d8544ede7cd8502f8d2ffd688961b2f9dedd5"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/18.safetensors",
+ "bytes": 4456040,
+ "sha256": "59ca9c3c668d2e9ed20604795a174ae6ddee215fee748e487dc82ee23b534ad2"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/19.json",
+ "bytes": 103,
+ "sha256": "3cf14069cf7c8a943966dd4e8c50097796027b916cd54c18d4c8e589295cd41f"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/19.safetensors",
+ "bytes": 4456040,
+ "sha256": "d0a9deb167d4ac5d7f411a5dd14909138b09c8061755f84e5e3f1378b9dcf894"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/20.json",
+ "bytes": 103,
+ "sha256": "d56ddff6182dae0858ebecf44f8f787858d9ad238eaf7a1563b4e7797323bb6b"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/20.safetensors",
+ "bytes": 4456040,
+ "sha256": "40feb488ac29ba6e99cf1fff1b9d4b9078a422449e648f24553d3ec93fee6530"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/21.json",
+ "bytes": 103,
+ "sha256": "069977435cb785cbfb0c3a64f7f21f6b25ad3db546e906808f2caf8bcae58742"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/21.safetensors",
+ "bytes": 4456040,
+ "sha256": "cd1a3c5737e3a82f15d79d3f37ba3df3c0430c3a6df832752826e786b6893b59"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/22.json",
+ "bytes": 103,
+ "sha256": "e5393d760b3a4c7637b7360fc900ae791c7186257fc9bac82f20f35eb80462da"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/22.safetensors",
+ "bytes": 4456040,
+ "sha256": "935c5019a9862e16f94920090040d7b09ba7ef786678ca453aacc91bd7e2d67e"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/23.json",
+ "bytes": 103,
+ "sha256": "a4f90e63f9d5f3d493922fd380f74eb69b58dbbb2e6862df9129d2687ffa15ae"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/23.safetensors",
+ "bytes": 4456040,
+ "sha256": "5215c662929c9a036155a21726d9b6775644bccd0cefaca79477b4c65fadf3b6"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/24.json",
+ "bytes": 103,
+ "sha256": "2aaa013e47a920bafc20e8b61095fd07f9d49bfd5e7e96b312cc4d7c0e6ef133"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/24.safetensors",
+ "bytes": 4456040,
+ "sha256": "8c651e18158f3c70e9e335b18962ee0a1f1843b6d040630dceb3e979440c657c"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/25.json",
+ "bytes": 103,
+ "sha256": "11cf276b0e9d79ea7336b63c9a0ac31aa09aec2687612024da948e11dc47f3d8"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/25.safetensors",
+ "bytes": 4456040,
+ "sha256": "530db57adcf090f8fe18e282373fca02fa3ad13820a2075c804344d6bad732ed"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/26.json",
+ "bytes": 103,
+ "sha256": "dc5db4988090533757f84bc254feb444b67a18f775cd7c4290f0004ae690bbaa"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/26.safetensors",
+ "bytes": 4456040,
+ "sha256": "de0e8a74e0b02a9d2122f0d461311b0f6b8645aef362ff37aab529ef92c3070a"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/27.json",
+ "bytes": 103,
+ "sha256": "35b1e9adc2427f82940094336727e241139044f6e2b98e12941566da2c6e1807"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/27.safetensors",
+ "bytes": 4456040,
+ "sha256": "51b073c5b5c60fa7154c94ac5e7e1c52fbdb9b1078c889d9a73654dbf6973141"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/28.json",
+ "bytes": 103,
+ "sha256": "0fd01ed5a6f838430fd4aa1a131ec57427c999a031acb3ecb40a1b100173f0a2"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/28.safetensors",
+ "bytes": 4456040,
+ "sha256": "6ea5a5e0449409acc444a0913d563118da5ead01020a89ad125b98f9bebacfdd"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/29.json",
+ "bytes": 103,
+ "sha256": "c13df43492f01fa37a048d1a7f93651dbffc5ed4aa7c7982fe002d22dfdea6e0"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/29.safetensors",
+ "bytes": 4456040,
+ "sha256": "a0c4032012c322636c3eb6b7450e54b9533aa2ed068f5508d39eb43ae85d5b9e"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/30.json",
+ "bytes": 103,
+ "sha256": "0d63f1bca1a2ec1ce1e276a9016f3c7bd953b9fee41ee7d7aea6c8f924db2b52"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/30.safetensors",
+ "bytes": 4456040,
+ "sha256": "97ca1ccf7058ffa78e9c1e189dd15be748b642f3a27a719d646a2c24d2c7a441"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/31.json",
+ "bytes": 103,
+ "sha256": "19a92b4de6d7f29bf5fe75cc1848d46729df61c72ec959d78958d4ed55741e5e"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/31.safetensors",
+ "bytes": 4456040,
+ "sha256": "3ed5af4d246b86a3476d5fc2c26f8d62a0d3b656e912842a5dfa062b4d9c8110"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/32.json",
+ "bytes": 103,
+ "sha256": "ef3e4309ab2218c75e7ce25e5c31275bc7c149517d7dd88e18b7e2cb136ea108"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/32.safetensors",
+ "bytes": 4456040,
+ "sha256": "a3ca9911208514fdffc270d697fd9b83fc9cc16acdbeac52cfe632eddbb399b5"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/33.json",
+ "bytes": 103,
+ "sha256": "cc38c662c44da10e78882b7e7aab6147cd84b08286004425be6deba9752752bc"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/33.safetensors",
+ "bytes": 4456040,
+ "sha256": "bb2206782ec88c8ec191f269ed39053c80199b4ca16397a036166b30998c6c00"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/34.json",
+ "bytes": 103,
+ "sha256": "07e21ca84b90eb7f3c193758e101b412cd6c7c5ca088754fa967f79948dfb583"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/34.safetensors",
+ "bytes": 4456040,
+ "sha256": "50b7ed03cdf37d7fcf504c5fa8cf04155d2c1d2e196f55b74eeec081cdf3a183"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/35.json",
+ "bytes": 103,
+ "sha256": "7bd4289495b52ad01de6512d3101dc8303b02f1398a079adc93ba3ada8b81198"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/35.safetensors",
+ "bytes": 4456040,
+ "sha256": "b9b4716db16d3c7e9cc858336c78035f9f768ffac8e65edbd1694769077f0940"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/36.json",
+ "bytes": 103,
+ "sha256": "a9b26ddf7e07b93186beebf2d2911ca42890eef6ce87d0fa8e64fbf42a835368"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/36.safetensors",
+ "bytes": 4456040,
+ "sha256": "4c273b5502e98cd1cc9450b95b6c5dc51ab3fe280458bc330fec629ea1a73119"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/37.json",
+ "bytes": 103,
+ "sha256": "d7577501165ec702c5a1c89336adfb38ae0e825d17cda6492d54d1f5f07b93ac"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/37.safetensors",
+ "bytes": 4456040,
+ "sha256": "f360e96b1e937c01890be5920e759ee1247b61c064bccd29788e17af8fbab8b5"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/38.json",
+ "bytes": 103,
+ "sha256": "1662ea2e2407ea033ffb6d89798de30915fdde34af816e757ef71257f5a9d232"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/38.safetensors",
+ "bytes": 4456040,
+ "sha256": "f0e9d4f1beb3630b929b21cd42bbc034d3ac511a6d043d211932fb3dc0ef11d7"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/39.json",
+ "bytes": 103,
+ "sha256": "c3b4f1cf9230fd68c39f88371b7b84e7bdd1dd3dcd06b7a9a43953271b2011e5"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/39.safetensors",
+ "bytes": 4456040,
+ "sha256": "594aa08ea6e0bef7e8a2796de5bac73c37995bdffc7606ad8dd4a291c77fbf2d"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/40.json",
+ "bytes": 103,
+ "sha256": "7f06906a48dd0be4f76b7db7c3366600aa771231b995dad056a05c0cc9d2ea5a"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/40.safetensors",
+ "bytes": 4456040,
+ "sha256": "cde25e4972edab376eb033fd5dbe5426fb0e6e53c428f4b1055e4a8ad6426b83"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/41.json",
+ "bytes": 103,
+ "sha256": "b66e7f7622a8835620bf38da55518215715f0ad5a153995668329dcdb941dfbb"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/41.safetensors",
+ "bytes": 4456040,
+ "sha256": "dca8e3a48637bc8325b0ea9443640c9e0274be695da6ab6da0a956b87f76e548"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/42.json",
+ "bytes": 103,
+ "sha256": "0bee835ba2ad2fd4955a22a3d133f11916c1dd4f718c20b4d4c428982e0926f5"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/42.safetensors",
+ "bytes": 4456040,
+ "sha256": "7ac8988dfbcab4ca00327f57d465d42b8d46731100d9a134e0f8df821692df6a"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/43.json",
+ "bytes": 103,
+ "sha256": "3d4390a37490e08e0745fc6bb471d1d2b8ebd513da99e7053356a9912334da79"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/43.safetensors",
+ "bytes": 4456040,
+ "sha256": "812e95c1bcbabc5de610263565ac6f2c2b58dfa0914f40185e23d220db8d7804"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/44.json",
+ "bytes": 103,
+ "sha256": "43830b47c2bfd74350574e9cecce467a2879acdadea63ed937462da8ab6fd4c8"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/44.safetensors",
+ "bytes": 4456040,
+ "sha256": "0f9e2d100da4f13603d0c039e06c4b6e7cb7a5bd60c96d686eb0f38af47d4702"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/45.json",
+ "bytes": 103,
+ "sha256": "204d32d3ceb7f2f520640673867ab62a4c51d2b9f076d328f1509b76c17f5ca2"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/45.safetensors",
+ "bytes": 4456040,
+ "sha256": "6d3135c8ed28bae050426ed074e86ad2a5a3352f43a2156dc4fd22992a5d7f8c"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/46.json",
+ "bytes": 103,
+ "sha256": "30dd134e38e4082c1f913d62636c52f0570f3810e2c0376f27c48d10efdbf65f"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/46.safetensors",
+ "bytes": 4456040,
+ "sha256": "fc2f7f62f2b1d170bb39fdf79d915286a6df287f387bc3608efa7c2d35240728"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/47.json",
+ "bytes": 103,
+ "sha256": "ffb19c1e9b3516309a79a1c2d46f1a22ad781895dd8ee3de995ba44f5ebd7223"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/47.safetensors",
+ "bytes": 4456040,
+ "sha256": "fc7a88951bf1957030413bb74a646f9b90b9b278486321eccc20b223d0216137"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/48.json",
+ "bytes": 103,
+ "sha256": "335b485652d0abcba0abf2af3366c4fc6523d6be3c56b2c6a16db135293f0441"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/48.safetensors",
+ "bytes": 4456040,
+ "sha256": "059fdd282e83c3ddff605d1b4ad3df1bac9711a02eed0c4a61dc1d86200fe0c2"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/49.json",
+ "bytes": 103,
+ "sha256": "a7a009e98a609361662c5499988c747aaf232a68fe9f1b0d65bf78ad2ba5dcb3"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/49.safetensors",
+ "bytes": 4456040,
+ "sha256": "ab8a45b126e168cc7ae3ba863a68419a9ca874ffedee961260a27722d162f616"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/identity.json",
+ "bytes": 931,
+ "sha256": "e25d680c1c6fe8942d8c063dee5304dadbb68023296cbd7ff7dae4daa115af66"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-222c457ff086c1fbf4b633e1/producer.json",
+ "bytes": 673,
+ "sha256": "6f5d42ef915bc1e5b58a7def1048c93381705344a89306a2ca14fb4619c3948c"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/00.json",
+ "bytes": 103,
+ "sha256": "f9e6e6b8eb252c9aa82f858604d7dc8a7b5550c4f86b46c067f1a9eae36f50b9"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/00.safetensors",
+ "bytes": 2907752,
+ "sha256": "504965de57302f0b18a5ea0b01d021635f26d47feac6e07d72ef7225657893e3"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/01.json",
+ "bytes": 103,
+ "sha256": "667288e0dbdf9a2e4dd8c2edbbd4b440c32f05f53a5dcf95f0781ff8f73c9e24"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/01.safetensors",
+ "bytes": 2907752,
+ "sha256": "3933338a466004538a5279fb32b1ee92c7694bb45a5d8e442ec9988a89ff50bf"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/02.json",
+ "bytes": 103,
+ "sha256": "ee2abbfab26a5ff44891b78d13280b920bb9903ef20ea74cd24eec01c37669c8"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/02.safetensors",
+ "bytes": 2907752,
+ "sha256": "76e929aa66455d87b31db7352892b0bdd3b7b0827d7359796b67915ab168cbac"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/03.json",
+ "bytes": 103,
+ "sha256": "d874fb4f15f188faade2edbaa89051eec7ae524ce33de96c30c13f133cf1da55"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/03.safetensors",
+ "bytes": 2907752,
+ "sha256": "e46b70ffbd459f848e056e1f0cfa9bdc6787f1096628baefa04bbcb5a2c7e496"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/04.json",
+ "bytes": 103,
+ "sha256": "1b21fee106806e5391c35dbe38e1ef7cae164afc6afd1aadf7156a939b509463"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/04.safetensors",
+ "bytes": 2907752,
+ "sha256": "cca36815e9a66e63b9ef0cbdcf3ad27b0013d858cd8077b7aee8fdff349074cb"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/05.json",
+ "bytes": 103,
+ "sha256": "143e16c046d56cac85ea87c6ad07f6ffad28b6225ef7b333c6cd7100857cb31f"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/05.safetensors",
+ "bytes": 2907752,
+ "sha256": "91a93b8ebd7efb2b313cccdaadca4334c851cf91fde9ef15be7a2c2bd30fcaab"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/06.json",
+ "bytes": 103,
+ "sha256": "403be70cb9593e105e36901bac8ace3b30a4ad20802cf6edf822e0ff6e8a370e"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/06.safetensors",
+ "bytes": 2907752,
+ "sha256": "c0cdf2f5afdc975ea53e5bb519ced8796e91c1fcdc0ea95142e5075134e727a2"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/07.json",
+ "bytes": 103,
+ "sha256": "6104c665d45e661dd85e0a5298ab1d99cdb487cb0bbb08dbaba42b0feefac69d"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/07.safetensors",
+ "bytes": 2907752,
+ "sha256": "056e7f109ab6affd7d970d3b41288cef1648c68b2d4626c208b554b7cbb9c5c1"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/08.json",
+ "bytes": 103,
+ "sha256": "23fd49a92fd34a9fa429f3fa5ca824c8034ffc4ebf214a5aa6bc27c73f2df7fc"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/08.safetensors",
+ "bytes": 2907752,
+ "sha256": "616d1c9354fd077aee1b650ccadbb8646eca871bd023b20c313a96b4af21d175"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/09.json",
+ "bytes": 103,
+ "sha256": "78e1088d5210dac80c7681bbf3bdbcae4d54314bf033002007bb78d64b132c87"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/09.safetensors",
+ "bytes": 2907752,
+ "sha256": "7ef4add104c271dd30c7330573ed239648190d47fcec80a0dc5a892a9912ac45"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/10.json",
+ "bytes": 103,
+ "sha256": "3b47a8cf90f7ee2d7098b065ee4418a7182dee88497e1f312dbde645fef49e5d"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/10.safetensors",
+ "bytes": 2907752,
+ "sha256": "3dd6c92a39d73f3c6e20c62ce472b0ef13a78d39495a1434e19464e9ce8c26bb"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/11.json",
+ "bytes": 103,
+ "sha256": "8d74197e07d83725fc1fc35ca35512a7695c25d4e110091d8873ab1ca7afc069"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/11.safetensors",
+ "bytes": 2907752,
+ "sha256": "6e9cb9bd31dcfff944232584a170fe69ee7976dee3b08ec8d96ecfca76d5dae3"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/12.json",
+ "bytes": 103,
+ "sha256": "912a5e1701d8c6a47f94ab661cb789896f6bd58f11b9ade74aef6a6243496c93"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/12.safetensors",
+ "bytes": 2907752,
+ "sha256": "3259bb9332233444035ad0d7d638ec1b545970e61c75423ad03f80f8e0144930"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/13.json",
+ "bytes": 103,
+ "sha256": "3757baf3ce4b46d5446f703d364001f71faf3f4bb6a2695c44e54b8713722540"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/13.safetensors",
+ "bytes": 2907752,
+ "sha256": "3a37bf16fea5f480c76aef7d40a9fa81060315277e5fd4f4665f2b0a6873f665"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/14.json",
+ "bytes": 103,
+ "sha256": "8db7dc32ab897398f56ac03512ab34f5c5c0135f488c9560a684c8294bd085bb"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/14.safetensors",
+ "bytes": 2907752,
+ "sha256": "2c73459bb7162d80fbf26c8dd9b8ec86e70f738ab16b9af3c32271ae4906b4c6"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/15.json",
+ "bytes": 103,
+ "sha256": "aa61f9f24888d56ccb0bc2881bd153ff94711c385f7ef837e899ffcff66f2b65"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/15.safetensors",
+ "bytes": 2907752,
+ "sha256": "2ccd0199c9a4fbae4e2de5310458eeb82499d4af08bbed855e1b49c15976376c"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/16.json",
+ "bytes": 103,
+ "sha256": "d7561665bc1d3ed3e09b2da3afd4c2b4f041203d9f6563e26a90ff3d6af38c11"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/16.safetensors",
+ "bytes": 2907752,
+ "sha256": "2fc98d5537b68b075f03b08ad0672c761a8704fa9d6396274223ec8af5678c37"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/17.json",
+ "bytes": 103,
+ "sha256": "07184db3712a2bc45cc73c136cf6b3afb60aaa004f5f818e6eeb8822dc71d4ae"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/17.safetensors",
+ "bytes": 2907752,
+ "sha256": "1192769428faf5d9825387c74beba5382bdde6beb8954801eecc0edec64f999d"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/18.json",
+ "bytes": 103,
+ "sha256": "e199d6d72ce9daff49fe318bf7610301de7aa9d59d2a56db6feb514989e4de9a"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/18.safetensors",
+ "bytes": 2907752,
+ "sha256": "456f5adeb6dd619eed554fe5a875cd1cdcbeaad326284a403eede95ae51c477a"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/19.json",
+ "bytes": 103,
+ "sha256": "4638884349dc0fe19444c4e351ccc13ce841f1c02a98f86d17fb55f1bd6fd295"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/19.safetensors",
+ "bytes": 2907752,
+ "sha256": "9dedbc8647b730c39625ed3d5fa2a77748415fbeb1870cd91428522ed3bd6f53"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/20.json",
+ "bytes": 103,
+ "sha256": "4fa40d8a400ff82f8246b29fce691237a6150181c28d87d1932c3a39d3a22bcc"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/20.safetensors",
+ "bytes": 2907752,
+ "sha256": "298dd809a0f26089cf8d6ea1a6f140e343fa9d923c55a89842efe97f51d717b5"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/21.json",
+ "bytes": 103,
+ "sha256": "d41c0a23a803aff40774435d078ecae0b9fac1938dc6a76db2cd2cb8306b6efe"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/21.safetensors",
+ "bytes": 2907752,
+ "sha256": "17d9e0e93144063e28d77b4ec937c088a2657918e55dd8c2c5f382475d1a3d7e"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/22.json",
+ "bytes": 103,
+ "sha256": "21d6ac56d4f9e4481c6ff48d5f668c9928ff8b99848ad9d63d6a9c3a56059033"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/22.safetensors",
+ "bytes": 2907752,
+ "sha256": "89d6a15a3e9fca27681133ce2cf7d4f831aa0c53ace7923a8fcf2e0cf1d1809f"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/23.json",
+ "bytes": 103,
+ "sha256": "cbdaeb28f128d2f42919eab76a8f846b1917bc00edc0500c56815d51b3cb2665"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/23.safetensors",
+ "bytes": 2907752,
+ "sha256": "c1e77d41f521abbe35c124b46fb0636a3ef8c7ad4197bbfb95feaa00020ca6d3"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/24.json",
+ "bytes": 103,
+ "sha256": "810e04bca0e409c93ac24564c48ede12086c2eb5bfda8074aad8615e180edc86"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/24.safetensors",
+ "bytes": 2907752,
+ "sha256": "7c591cbafc2d41483bb6f9decd773ed274c5b4032a89888d600bcf1a46829a3b"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/25.json",
+ "bytes": 103,
+ "sha256": "0c0f112fc2b35f10ad0706d180be4f61e8136e19f07064d7472c9fedddfa31b0"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/25.safetensors",
+ "bytes": 2907752,
+ "sha256": "3e03ae0f82c3b2b7e4fe146bfbaa6605bc636823fcc5e282ffcb9abf7216deec"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/26.json",
+ "bytes": 103,
+ "sha256": "4c1061b6bffd9173aec8d113e5538a359dc4d5917d028997a41b50d93bffee23"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/26.safetensors",
+ "bytes": 2907752,
+ "sha256": "7ab35cc41e65d0431db53a06f4884c97eeb3f688ed841105b8dba4d58106f287"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/27.json",
+ "bytes": 103,
+ "sha256": "a30980f0c1671db04a68014a210b68dfc5b97bd71b7e4d570c74528a727bd4e1"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/27.safetensors",
+ "bytes": 2907752,
+ "sha256": "990084df00fc135b5456873456b7d6c2b31a389bec157cddfbfaff0dd5624e57"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/28.json",
+ "bytes": 103,
+ "sha256": "237bf4dacb03d5a36f7cb7c4e56ce90c35ff52e68285425bb1023089f63f4348"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/28.safetensors",
+ "bytes": 2907752,
+ "sha256": "3e7bf8dcdf9b98af8dee62e742ec02071e79c6fa6abe717767155fecf40dcae0"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/29.json",
+ "bytes": 103,
+ "sha256": "d65b87005794c9fee3f880a5b70126d9452df1165f72f2d2ad366e6a372a67fa"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/29.safetensors",
+ "bytes": 2907752,
+ "sha256": "348a7edf18bb195f0972abde268560f6b6d90acd0872dc1808650a2c5ed82b5b"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/30.json",
+ "bytes": 103,
+ "sha256": "52048b02c2bac938404e5a8e8ff2f65719269592bdf681d0753747e41c6f5326"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/30.safetensors",
+ "bytes": 2907752,
+ "sha256": "f7dc17603c0b257e480479fcb45b35dc261099cbf9f1c08e5c72f83b82f1328c"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/31.json",
+ "bytes": 103,
+ "sha256": "c6ad32ea9a7c5b56456436563f007b84e481270a704ac7439be25de4676f4837"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/31.safetensors",
+ "bytes": 2907752,
+ "sha256": "9af528ffbd8e1ea01a7f183cce0a30c561add5bca4d095e1826a1ffff935cf51"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/32.json",
+ "bytes": 103,
+ "sha256": "b45013ed66d521ab7a4b8e7a8a86bb47f571d19c3ed5c85adf1116189cfb8260"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/32.safetensors",
+ "bytes": 2907752,
+ "sha256": "2b49977f572aa9b4efcd2f6da30d8125ddfc2f84604a821283791790856bfebd"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/33.json",
+ "bytes": 103,
+ "sha256": "990ef6d77bbc989b9bd14d37ba1f644eaf36fff8dd65461f9f72fa84d8b0fa9c"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/33.safetensors",
+ "bytes": 2907752,
+ "sha256": "0d842ee71fd2ae4d911a09aacdf5984fe6ac8665ebc4f0b77b867abf39e9ec08"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/34.json",
+ "bytes": 103,
+ "sha256": "29b152efa3120d46cf7c26eaf201b246028488023c85efb3baf968576708f3dd"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/34.safetensors",
+ "bytes": 2907752,
+ "sha256": "763c6e88f93285283f4f7d7aa60ac9c5863d3436080404384599f4a5eb051e8b"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/35.json",
+ "bytes": 103,
+ "sha256": "e0054f4705847c3b611101d855e0039be9fcbf31b5db8160a2ee1f24d0a63799"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/35.safetensors",
+ "bytes": 2907752,
+ "sha256": "a3addbac96f3444e752ce21e8279deff0806633ae08e4a7c676b40c876bc366a"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/36.json",
+ "bytes": 103,
+ "sha256": "a47f7adfdf1fd286a64098263fa5dee759fde7ae6c49efe3267a53a4f4d4efae"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/36.safetensors",
+ "bytes": 2907752,
+ "sha256": "006c53def3a3af3b6cfdc5e4c3a98d122a5af2b8f88601ab9de804a38c559b84"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/37.json",
+ "bytes": 103,
+ "sha256": "f9272e762e683689bac14b2f9ad9b8cb72453ebad82312cc336358100cb623d8"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/37.safetensors",
+ "bytes": 2907752,
+ "sha256": "5a5a07ca72d25c3304df588aeb24e5ed81ca9ff029e398f3965e14e53ff5dc4f"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/38.json",
+ "bytes": 103,
+ "sha256": "a83d91338fb3003d1fd2135d18c3db27336d698c4e6d3d48ca7dae46cdff69b8"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/38.safetensors",
+ "bytes": 2907752,
+ "sha256": "52d97b816432d3036f44f551532ca3e1703c3d00a57a23b42e1bcb3f3992790d"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/39.json",
+ "bytes": 103,
+ "sha256": "d7d4cc02a77e8ed4d22c9ad13d62f8c3fc65fb27bec04de1bbfaa7fcaf487754"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/39.safetensors",
+ "bytes": 2907752,
+ "sha256": "fce501da767380acdcdc207a57e4fd320424b89593ae4f2a6dabfa21aa3de778"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/40.json",
+ "bytes": 103,
+ "sha256": "31c0ad0dde0029f66701774c819e5f3194e2ad8d30901d1936c92f79fafa54bf"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/40.safetensors",
+ "bytes": 2907752,
+ "sha256": "99943d87d442079dcd0188c125ee625923d126ed2170a798ca79224dd13fc6bc"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/41.json",
+ "bytes": 103,
+ "sha256": "0a5fcac7bd22c378fef0e3241d02be62e5c0fff38af41700bcf4ed2cb858c781"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/41.safetensors",
+ "bytes": 2907752,
+ "sha256": "f9cd78e7d7eb32236cc9d23136298e7957aedb7b86d54725f953fb4998a7b696"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/42.json",
+ "bytes": 103,
+ "sha256": "1b8afea6c9d080d637e7d6051612774803e602ebce550bb37c9b785e464ef3e6"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/42.safetensors",
+ "bytes": 2907752,
+ "sha256": "4e40a578184f9f5eb20698ed5e21dcf9245cf8e5bf519bd1457059facb59f780"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/43.json",
+ "bytes": 103,
+ "sha256": "69c73f5564ae252170f99aec65d4cc1bf1720f1745492f09717e1f383fe5fe22"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/43.safetensors",
+ "bytes": 2907752,
+ "sha256": "fc6928999a88ca8e7e952bd3156971ff83515332b77beb6a32219c14e218d455"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/44.json",
+ "bytes": 103,
+ "sha256": "7501a07a200d4203f29c283609ddc9d3ba3f2bf27026c2b2dffaa7632a25a666"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/44.safetensors",
+ "bytes": 2907752,
+ "sha256": "f530a54d19af7d107d51753a6b7a5a421ddc5886a37adfaae18ea50c189d0675"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/45.json",
+ "bytes": 103,
+ "sha256": "839e60a4534ef68e27f3ac0877dfebb006f69ca5056e784d900c7a85dee645fb"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/45.safetensors",
+ "bytes": 2907752,
+ "sha256": "c7684401bf33f5ae9014126848148c1864c7003a191c93f1ca4ed5d4cb13b429"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/46.json",
+ "bytes": 103,
+ "sha256": "230f820c14d1846b0b395aa869c51e2221091d7e9655077f3a5254af47d5bcb0"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/46.safetensors",
+ "bytes": 2907752,
+ "sha256": "b71a67fd54a45ee9d74c808940cb99aa8b9f4a0253220e03e0ff2bd0095df33f"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/47.json",
+ "bytes": 103,
+ "sha256": "fc5b77e56e0fdb1eb82859cbee5c03d1038466e2827959cff535410ad6f7ebd2"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/47.safetensors",
+ "bytes": 2907752,
+ "sha256": "c242a36f612c5bc688eb77b3cdcaf44e6cf0f739234e1358c4a7c1747b85e2d3"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/48.json",
+ "bytes": 103,
+ "sha256": "70fc8a96a8958d7e7c1ad855d8bb577c62c342db18321e886a1b209f0ef3904e"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/48.safetensors",
+ "bytes": 2907752,
+ "sha256": "59ca5ce7c1b6814df2c2cdf68117b670231c07c6ff9a4ff5dfd5d235acdade24"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/49.json",
+ "bytes": 103,
+ "sha256": "76b3524064769d7635c6587e960fd4f6d3bc5c5ef672ed8c8ad5cafe61c31c0f"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/49.safetensors",
+ "bytes": 2907752,
+ "sha256": "842557f5218c789c35c186a42ba718e82dc2bad0c36edc1b4c8e4fc4a3ea75a8"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/identity.json",
+ "bytes": 723,
+ "sha256": "6ee4ab1773216749667fcabd2c1f2e25e52b71b21a060a22d133f52858395160"
+ },
+ {
+ "file": "rowwise/cache/adaln-tables-v2-bb59bb4aa370f50c71ca925e/producer.json",
+ "bytes": 642,
+ "sha256": "4dbd4576fe79b8cabe681d8ddb2706f4d07be12e3f5ca6670ee74beeed20af85"
+ },
+ {
+ "file": "rowwise/cache/blocks/00.safetensors",
+ "bytes": 432694672,
+ "sha256": "4dd4655735d78520faf48fc401e0bb5759c79c4019a16d4d507e1f3552bb1a19"
+ },
+ {
+ "file": "rowwise/cache/blocks/01.safetensors",
+ "bytes": 432694672,
+ "sha256": "4a65a2603f567d3ac398e6656a8020aceb563f0153fe14fe6eeb9e4264b09026"
+ },
+ {
+ "file": "rowwise/cache/blocks/02.safetensors",
+ "bytes": 432694672,
+ "sha256": "a4283fc720605e0d7bb464b544b1496c5204b0d7e4fbb76a3046f9f808d683f2"
+ },
+ {
+ "file": "rowwise/cache/blocks/03.safetensors",
+ "bytes": 432694672,
+ "sha256": "149a59d1dfed69d329ae7e700edc74ebc2b717c2533cd8db0f8964215de99b4a"
+ },
+ {
+ "file": "rowwise/cache/blocks/04.safetensors",
+ "bytes": 432694672,
+ "sha256": "66f4a71d99edfdc213b2666dd5d4b0c0f4fe10cd92e95f0d38668ea3e864464c"
+ },
+ {
+ "file": "rowwise/cache/blocks/05.safetensors",
+ "bytes": 432694672,
+ "sha256": "bd78d359afa95b138be09e8266b2afccf88c91cef804ac64015035a57cc7c2d8"
+ },
+ {
+ "file": "rowwise/cache/blocks/06.safetensors",
+ "bytes": 432694672,
+ "sha256": "17a54d22bb2b4d4be4b8d83dedb5bbdfbee5a1423a9572a87b41980be7d1567a"
+ },
+ {
+ "file": "rowwise/cache/blocks/07.safetensors",
+ "bytes": 432694672,
+ "sha256": "3cd8af15fb82666ec888872330ec19f7eed828018b8d74412998f14760ce5831"
+ },
+ {
+ "file": "rowwise/cache/blocks/08.safetensors",
+ "bytes": 432694672,
+ "sha256": "73a2999bd0d94f3a87a0fa75bfc5f2cc04ddb8413dfef3e74d1c0cd33bc0a76e"
+ },
+ {
+ "file": "rowwise/cache/blocks/09.safetensors",
+ "bytes": 432694672,
+ "sha256": "8f9fd582e533514a38fb1446b9d43f9544e5ff8b061f7bde5a70093e1abf7755"
+ },
+ {
+ "file": "rowwise/cache/blocks/10.safetensors",
+ "bytes": 432694704,
+ "sha256": "716b685e07060ec733ef0a4d79046aaa22b634bc3ecbb2e68e3af026e87c9bda"
+ },
+ {
+ "file": "rowwise/cache/blocks/11.safetensors",
+ "bytes": 432694704,
+ "sha256": "be952b75d69aeb1c4cca5f415481e053fca2c8fd5b398c49dca6d5f471734aac"
+ },
+ {
+ "file": "rowwise/cache/blocks/12.safetensors",
+ "bytes": 432694704,
+ "sha256": "0cefb869446e229664e5dddd2b8f82d3caa0ad04b53a8b3efe9347864f01e409"
+ },
+ {
+ "file": "rowwise/cache/blocks/13.safetensors",
+ "bytes": 432694704,
+ "sha256": "916dc3799b233bff218407105766d266b939fb9506cc91794bc2cc6b04c2f818"
+ },
+ {
+ "file": "rowwise/cache/blocks/14.safetensors",
+ "bytes": 432694704,
+ "sha256": "f78106d8511b6683ea17abb7a4a7f88abbe88ecfac765cc6689b033bd6c9b4a6"
+ },
+ {
+ "file": "rowwise/cache/blocks/15.safetensors",
+ "bytes": 432694704,
+ "sha256": "393c065b002b3542e9f12ad92f2a545842137ddc87c1ce9eb73daeccd82c6c38"
+ },
+ {
+ "file": "rowwise/cache/blocks/16.safetensors",
+ "bytes": 432694704,
+ "sha256": "38c1d3bea4137fc28d03c51230d39bd1107f05d4e61080d316d5ff8a70a6eafd"
+ },
+ {
+ "file": "rowwise/cache/blocks/17.safetensors",
+ "bytes": 432694704,
+ "sha256": "37363d37ed4a79b0087a4f6ca4b98713f4f2515a688b2254aff18340c80ce76c"
+ },
+ {
+ "file": "rowwise/cache/blocks/18.safetensors",
+ "bytes": 432694704,
+ "sha256": "c27e4afc545491dfc18d3ac24c38c5b539be4230f3695900a9ad008572a585d6"
+ },
+ {
+ "file": "rowwise/cache/blocks/19.safetensors",
+ "bytes": 432694704,
+ "sha256": "10de99a01ac6056ddf6821e13e15848db9032843ca21c05f63cd6f0b80e71466"
+ },
+ {
+ "file": "rowwise/cache/blocks/20.safetensors",
+ "bytes": 432694704,
+ "sha256": "e02a9f828e72ff8b3b3074b9c5902405ac9788d4a9167f4cc502f359a18a4790"
+ },
+ {
+ "file": "rowwise/cache/blocks/21.safetensors",
+ "bytes": 432694704,
+ "sha256": "9d44a40610ca8401c54408e70b92facaba035048bd98b69a8a839c13c7a4ba48"
+ },
+ {
+ "file": "rowwise/cache/blocks/22.safetensors",
+ "bytes": 432694704,
+ "sha256": "fe6f3f6452f0f9d6b3dc57c2db3f3bfa774aced6bd5c494007c1baa8634aabb6"
+ },
+ {
+ "file": "rowwise/cache/blocks/23.safetensors",
+ "bytes": 432694704,
+ "sha256": "06d99e3fb00aca1d9441e73df05d01468c39276c8e12559f15b760bbdf8ec5b3"
+ },
+ {
+ "file": "rowwise/cache/blocks/24.safetensors",
+ "bytes": 432694704,
+ "sha256": "ba90af1becf12bd884a5ac63b16d10c442e980e08b2c901e7f49be5e55c4de58"
+ },
+ {
+ "file": "rowwise/cache/blocks/25.safetensors",
+ "bytes": 432694704,
+ "sha256": "dbd79cbe5ff8049b3e47293793356f91df71d3328e9cd802c814732a0250041b"
+ },
+ {
+ "file": "rowwise/cache/blocks/26.safetensors",
+ "bytes": 432694704,
+ "sha256": "cc80d0b4b1a52b48d5b8f4d5a86dda8e1a3d1c7653088dfb580e00f9e13e83bd"
+ },
+ {
+ "file": "rowwise/cache/blocks/27.safetensors",
+ "bytes": 432694704,
+ "sha256": "9c8ce34d9e787d13367f3f017c7e9af19f659b6608567911cb4f8754d2b6ecb2"
+ },
+ {
+ "file": "rowwise/cache/blocks/28.safetensors",
+ "bytes": 432694704,
+ "sha256": "891c91c4c66261ba9a38243338493bff6c76435d128373521344e2319a50a9ba"
+ },
+ {
+ "file": "rowwise/cache/blocks/29.safetensors",
+ "bytes": 432694704,
+ "sha256": "07bbb44ebc8629c403632e1c07b393776ab1e63bbbda649aa14ba5400601447f"
+ },
+ {
+ "file": "rowwise/cache/blocks/30.safetensors",
+ "bytes": 432694704,
+ "sha256": "02e9ec9a78de498de5b3f263655e9e853d9510bc5a096dadee33e88736e91969"
+ },
+ {
+ "file": "rowwise/cache/blocks/31.safetensors",
+ "bytes": 432694704,
+ "sha256": "4d93a27aaf9db6741e63e29bf2b7ea4aac833cd7e4cae095c9d41cc0fa90377c"
+ },
+ {
+ "file": "rowwise/cache/blocks/32.safetensors",
+ "bytes": 432694704,
+ "sha256": "fcb888ba7d9868d03a67027d60cf43b077a4b6e24811bbbc1f1069c00e739019"
+ },
+ {
+ "file": "rowwise/cache/blocks/33.safetensors",
+ "bytes": 432694704,
+ "sha256": "64428a23ef170a85d2706c10db642af7b96dcc79c29f50a2812cda831b4839a4"
+ },
+ {
+ "file": "rowwise/cache/blocks/34.safetensors",
+ "bytes": 432694704,
+ "sha256": "fb5c1dc4e8d61a176c20b3c0829db95b53a45a6e12567f101d87aadf0b697ad3"
+ },
+ {
+ "file": "rowwise/cache/blocks/35.safetensors",
+ "bytes": 432694704,
+ "sha256": "da6f0fa66e51cf8f0491201c1cdf54694d4516fc0effec6bcdcd3da5956e3ed6"
+ },
+ {
+ "file": "rowwise/cache/blocks/36.safetensors",
+ "bytes": 432694704,
+ "sha256": "cf4407e539ac5b5f318c76c31ccb343f82a8e6551b0f8d7348c2ea7927e9b53e"
+ },
+ {
+ "file": "rowwise/cache/blocks/37.safetensors",
+ "bytes": 432694704,
+ "sha256": "cc4fedb5cc61505d5ab1296fc7787336d144c4f72d878d14c13601acca7944c3"
+ },
+ {
+ "file": "rowwise/cache/blocks/38.safetensors",
+ "bytes": 432694704,
+ "sha256": "1c0c016b2912dff697fc117d29454142ec0f376da5bb600521c4e6e41dd24420"
+ },
+ {
+ "file": "rowwise/cache/blocks/39.safetensors",
+ "bytes": 432694704,
+ "sha256": "7bc95b2afc86ea1bcf6618233957af05026798f12850cfe2b73f802526bccf78"
+ },
+ {
+ "file": "rowwise/cache/blocks/40.safetensors",
+ "bytes": 432694704,
+ "sha256": "bf2656dfc0b7479a0cbca0f8c40c8178dc12166fa2bb81c339ce7c76e351789c"
+ },
+ {
+ "file": "rowwise/cache/blocks/41.safetensors",
+ "bytes": 432694704,
+ "sha256": "8299b7034719542d65db1300c1766e81edc8bb911e8c754154e4ded2b821178a"
+ },
+ {
+ "file": "rowwise/cache/blocks/42.safetensors",
+ "bytes": 432694704,
+ "sha256": "9143574ebc0ca736ab021b60c33bcf3c3c4db22df071a1c0f38b8896256c97fb"
+ },
+ {
+ "file": "rowwise/cache/blocks/43.safetensors",
+ "bytes": 432694704,
+ "sha256": "1e34eb6516511999c304c4e28eac8d9f33483cd058bf48fe4a7da86b2253a296"
+ },
+ {
+ "file": "rowwise/cache/blocks/44.safetensors",
+ "bytes": 432694704,
+ "sha256": "84c65263cc389306334fdcb6555f74a11ed34ee1669524570367a59e9d78c021"
+ },
+ {
+ "file": "rowwise/cache/blocks/45.safetensors",
+ "bytes": 432694704,
+ "sha256": "91e83b97ea6771686a14f72df0137e9752c3e27fe8e83ee74f5c6fbe76286845"
+ },
+ {
+ "file": "rowwise/cache/blocks/46.safetensors",
+ "bytes": 432694704,
+ "sha256": "c27c828411fe1191aaf0f7eef1a3544ea6886ead0b11132a322f1db0276312e7"
+ },
+ {
+ "file": "rowwise/cache/blocks/47.safetensors",
+ "bytes": 432694704,
+ "sha256": "821b268c44a6e339ab62112411cd581fcad74ddef061969e172f4008cfdaf49d"
+ },
+ {
+ "file": "rowwise/cache/blocks/48.safetensors",
+ "bytes": 432694704,
+ "sha256": "b06e956cc2b4bdfffe4fd33aca934fe7cb80da5cb2aba336d5e276190c0ed5fe"
+ },
+ {
+ "file": "rowwise/cache/blocks/49.safetensors",
+ "bytes": 432694704,
+ "sha256": "8f636375920b59734f0732f55177ce082e05ad8053185ecc0294e4b49a557506"
+ },
+ {
+ "file": "rowwise/cache/manifest.json",
+ "bytes": 215862,
+ "sha256": "d10f3f891396dcb408b46a2e1b926f9228f64385af07821ed2fa26afb86b666a"
+ },
+ {
+ "file": "rowwise/cache/publication.json",
+ "bytes": 18985,
+ "sha256": "da5569b61ee259a4ef02249076af545d5837f289b6fc62eeb7ae3ab0b3954d0e"
+ },
+ {
+ "file": "rowwise/cache/root.safetensors",
+ "bytes": 925532800,
+ "sha256": "0a47d21f4ba8079a3cfd4fdc6f60d2779f2d2c76dc38fe3ad4bc6f539f33eba1"
+ }
+ ]
+ }
+ }
+}
diff --git a/freevideo_engine/probe.py b/freevideo_engine/probe.py
new file mode 100644
index 0000000..a6b406a
--- /dev/null
+++ b/freevideo_engine/probe.py
@@ -0,0 +1,87 @@
+"""Short, isolated consumer-GPU compatibility checks for doctor --probe."""
+import json
+import math
+import sys
+import platform
+import traceback
+import torch
+from .locking import runtime_lock
+
+
+def main():
+ backend = sys.argv[1]
+ torch.set_num_threads(4)
+ torch.manual_seed(171)
+ with runtime_lock():
+ if backend == 'linear':
+ from .paths import add_vdn
+ add_vdn()
+ from src.models.ops.fp8_linear import quantize_activation, per_tensor_gemm
+ from .fp8_gemm import scaled_mm
+ from .kernel_capabilities import fp8_implementation
+ # Wide output and a tail tile exercise the bounded compatibility
+ # epilogue on the user's real GPU without loading any model.
+ x = torch.randn(701, 512, device='cuda', dtype=torch.bfloat16)
+ weight = torch.randn(12288, 512, device='cuda', dtype=torch.bfloat16)
+ shapes = {'activation': list(x.shape), 'weight': list(weight.shape), 'output_dtype': str(x.dtype)}
+ if torch.cuda.get_device_capability() < (8, 9):
+ from .weight_only import linear
+ scale = weight.float().abs().amax().reshape(1, 1) / 448.
+ fp8 = (weight.float() / scale).to(torch.float8_e4m3fn)
+ value = linear(x, fp8, scale)
+ policy = 'bf16-weight-only'
+ else:
+ scale = (weight.float().abs().amax() if per_tensor_gemm() else weight.float().abs().amax(1)).reshape(1, -1) / 448.
+ fp8 = (weight.float() / scale.reshape(-1, 1)).to(torch.float8_e4m3fn)
+ xq, xs = quantize_activation(x)
+ implementation = fp8_implementation(platform.system(), 'per_tensor' if per_tensor_gemm() else 'rowwise')
+ value = scaled_mm(xq, fp8.t(), xs, scale, out_dtype=x.dtype, implementation=implementation)
+ policy = 'native-fp8-per-tensor' if per_tensor_gemm() else 'native-fp8-rowwise'
+ policy += '/' + implementation
+ # Small dequantized reference only; no model weights are loaded.
+ reference_x = x.float() if torch.cuda.get_device_capability() < (8, 9) else xq.float() * xs
+ reference = reference_x @ (fp8.float() * scale.reshape(-1, 1)).t()
+ tolerance = .01
+ else:
+ if backend == 'torch-flash' and not torch.backends.cuda.is_flash_attention_available():
+ raise RuntimeError('This PyTorch wheel was built without CUDA Flash Attention. '
+ 'Automatic selection can use cuDNN or Sage2 if their probes pass.')
+ from .attention import WindowAttention
+ from src.models.sequence_layout import SequenceLayout
+ from src.models.softmax_attention.window import window_bounds, window_softmax_reference
+ layout = SequenceLayout(seq_len=487, video_start=3, num_frames=15, tokens_per_frame=32)
+ q, k, v = [torch.randn(487, 4, 128, device='cuda', dtype=torch.bfloat16) for _ in range(3)]
+ shapes = {'qkv': list(q.shape), 'dtype': str(q.dtype), 'stride': list(q.stride())}
+ attention = WindowAttention(backend, window_batch=2)
+ value = attention(q, k, v, layout, window_bounds(15, radius=2, chunk=3), 128 ** -.5, 'both')
+ policy = dict(attention.backend_calls)
+ reference = window_softmax_reference(q, k, v, layout, window_bounds(15, radius=2, chunk=3), 128 ** -.5, 'both')
+ tolerance = .06 if backend == 'sage2' else .006
+ torch.cuda.synchronize()
+ if not bool(torch.isfinite(value).all()):
+ raise RuntimeError('Kernel returned non-finite values')
+ relative = ((value.float() - reference.float()).square().mean() /
+ reference.float().square().mean().clamp_min(1e-20)).sqrt().item()
+ if not math.isfinite(relative) or relative > tolerance:
+ raise RuntimeError(f'Kernel relative RMSE {relative:.6f} exceeded {tolerance}')
+ print(json.dumps({'backend': backend, 'status': 'complete', 'policy': policy,
+ 'gpu': torch.cuda.get_device_name(), 'capability': torch.cuda.get_device_capability(),
+ 'scope': 'Small-shape execution only', 'finite': True,
+ 'relative_rmse': relative, 'relative_rmse_limit': tolerance,
+ 'shapes': shapes,
+ 'torch_flash_compiled': torch.backends.cuda.is_flash_attention_available(),
+ 'torch': str(torch.__version__), 'cuda': torch.version.cuda,
+ 'system': platform.system()}), flush=True)
+
+
+if __name__ == '__main__':
+ try:
+ main()
+ except Exception as error:
+ traceback.print_exc()
+ print(json.dumps({'backend': sys.argv[1], 'status': 'error', 'error': str(error),
+ 'torch_flash_compiled': torch.backends.cuda.is_flash_attention_available(),
+ 'torch': str(torch.__version__), 'cuda': torch.version.cuda,
+ 'system': platform.system()}), flush=True)
+ sys.exit(1)
diff --git a/freevideo_engine/process_host.py b/freevideo_engine/process_host.py
new file mode 100644
index 0000000..f783153
--- /dev/null
+++ b/freevideo_engine/process_host.py
@@ -0,0 +1,36 @@
+"""Windows job launch gate; never interpret a command through a shell."""
+import os
+import signal
+import subprocess
+import sys
+
+
+def main():
+ from .win32 import kernel32
+ from .processes import HANDLE_ENV
+ lib = kernel32()
+ gate = int(sys.argv[1])
+ try:
+ if lib.WaitForSingleObject(gate, 30000) != 0:
+ raise RuntimeError('Worker was not assigned to its Windows Job Object in time')
+ finally:
+ lib.CloseHandle(gate)
+ # The child handles cancellation and writes its reports. The host remains
+ # alive while it exits; the owning controller enforces the grace timeout.
+ signal.signal(signal.SIGBREAK, lambda *_: None)
+ info = subprocess.STARTUPINFO()
+ handle = os.environ.get(HANDLE_ENV)
+ if handle:
+ info.lpAttributeList = {'handle_list': [int(handle)]}
+ # A GUI-launched gate may have redirected handles but no visible console.
+ # Forward its streams explicitly so Popen sets STARTF_USESTDHANDLES rather
+ # than depending on console/implicit handle inheritance for log capture.
+ child = subprocess.Popen(sys.argv[2:], startupinfo=info, close_fds=True,
+ stdin=sys.stdin if sys.stdin is not None else subprocess.DEVNULL,
+ stdout=sys.stdout if sys.stdout is not None else subprocess.DEVNULL,
+ stderr=sys.stderr if sys.stderr is not None else subprocess.DEVNULL)
+ return child.wait()
+
+
+if __name__ == '__main__':
+ raise SystemExit(main())
diff --git a/freevideo_engine/processes.py b/freevideo_engine/processes.py
new file mode 100644
index 0000000..e638c85
--- /dev/null
+++ b/freevideo_engine/processes.py
@@ -0,0 +1,223 @@
+"""Worker creation, inherited leases and bounded cleanup on Linux and Windows."""
+import json
+import os
+from pathlib import Path
+import signal
+import subprocess
+import sys
+import threading
+from contextlib import contextmanager
+
+from .system import windows
+
+HANDLE_ENV = 'FREEVIDEO_RUNTIME_LOCK_HANDLE'
+COMMAND_ENV = 'FREEVIDEO_WINDOWS_CHILD_COMMAND'
+_spawn_lock = threading.Lock()
+
+
+def windows_child_command(command):
+ """Data for the frozen Job gate; never ask PowerShell to reparse argv."""
+ if isinstance(command, (str, bytes)) or not command:
+ raise ValueError('Windows workers require a nonempty argument list')
+ values = list(map(str, command))
+ if not values[0] or any('\0' in value for value in values):
+ raise ValueError('Windows worker arguments cannot contain NUL or an empty executable')
+ return json.dumps(dict(executable=values[0], arguments=subprocess.list2cmdline(values[1:])), ensure_ascii=True)
+
+
+def inherited_fd():
+ """Adopt a Windows handle once; CRT descriptor numbers are process-local."""
+ value = os.environ.get(HANDLE_ENV)
+ if windows() and value:
+ import msvcrt
+ handle = int(value)
+ descriptor = msvcrt.open_osfhandle(handle, os.O_RDWR | os.O_BINARY)
+ os.environ.pop(HANDLE_ENV)
+ os.environ['FREEVIDEO_RUNTIME_LOCK_FD'] = str(descriptor)
+ return descriptor
+ value = os.environ.get('FREEVIDEO_RUNTIME_LOCK_FD')
+ return int(value) if value is not None else None
+
+
+def descriptors():
+ descriptor = inherited_fd()
+ return () if descriptor is None else (descriptor,)
+
+
+def module_command(module, *arguments):
+ """Also works with portable Windows Python, whose ._pth ignores PYTHONPATH."""
+ code = ("import runpy,sys;sys.path.insert(0,sys.argv.pop(1));"
+ "runpy.run_module(sys.argv.pop(1),run_name='__main__',alter_sys=True)")
+ return [sys.executable, '-B', '-X', 'utf8', '-c', code,
+ str(Path(__file__).resolve().parents[1]), module, *map(str, arguments)]
+
+
+class WindowsPopen(subprocess.Popen):
+ def __init__(self, command, *, pass_fds=(), env=None, **kwargs):
+ import ctypes
+ import msvcrt
+ from .win32 import Job, HANDLE, checked, kernel32
+ lib = kernel32()
+ self.job = Job()
+ handles = []
+ environment = dict(os.environ if env is None else env)
+ # A worker waits for assignment before it is allowed to spawn anything.
+ # This closes the launch/kill race without suspended-thread internals.
+ try:
+ gate = checked(lib.CreateEventW(None, True, False, None))
+ handles.append(gate)
+ if getattr(sys, 'frozen', False):
+ if pass_fds:
+ raise ValueError('The desktop launcher delegates runtime leases to managed Python')
+ environment[COMMAND_ENV] = windows_child_command(command)
+ host = [str(Path(os.environ['SystemRoot']) / 'System32/WindowsPowerShell/v1.0/powershell.exe'),
+ '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File',
+ str(Path(__file__).with_name('windows_process_host.ps1')), str(gate)]
+ else:
+ environment.pop(COMMAND_ENV, None)
+ host = module_command('freevideo_engine.process_host', gate, *command)
+ with _spawn_lock:
+ checked(lib.SetHandleInformation(gate, 1, 1))
+ if len(pass_fds) > 1:
+ raise ValueError('Only the runtime lease may be inherited by a Windows worker')
+ for fd in pass_fds:
+ duplicate = HANDLE()
+ current = lib.GetCurrentProcess()
+ checked(lib.DuplicateHandle(current, msvcrt.get_osfhandle(fd), current,
+ ctypes.byref(duplicate), 0, True, 2))
+ handles.append(duplicate.value)
+ environment[HANDLE_ENV] = str(duplicate.value)
+ if not pass_fds:
+ environment.pop(HANDLE_ENV, None)
+ environment.pop('FREEVIDEO_RUNTIME_LOCK_FD', None)
+ info = subprocess.STARTUPINFO()
+ info.lpAttributeList = {'handle_list': handles}
+ # Managed Python is not frozen. It still launches native
+ # helpers after the GUI starts; hide those job gates too.
+ info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
+ info.wShowWindow = 0
+ kwargs.pop('start_new_session', None)
+ kwargs.pop('close_fds', None)
+ super().__init__(host, env=environment, startupinfo=info, close_fds=True,
+ creationflags=subprocess.CREATE_NEW_PROCESS_GROUP, **kwargs)
+ self.job.assign(self._handle)
+ checked(lib.SetEvent(gate))
+ except BaseException:
+ if getattr(self, '_child_created', False):
+ super().kill()
+ super().wait(timeout=5)
+ self.job.close()
+ raise
+ finally:
+ for handle in handles:
+ lib.CloseHandle(handle)
+
+ def poll(self):
+ value = super().poll()
+ if value is not None:
+ self.job.close()
+ return value
+
+ def wait(self, timeout=None):
+ value = super().wait(timeout=timeout)
+ self.job.close()
+ return value
+
+ def kill(self):
+ self.job.kill()
+
+ def terminate(self):
+ self.kill()
+
+
+def popen(command, *, pass_fds=(), supervise=False, **kwargs):
+ if windows():
+ return WindowsPopen(command, pass_fds=pass_fds, **kwargs)
+ if supervise and sys.platform == 'linux':
+ import shutil
+ executable = str(command[0])
+ if os.sep in executable and not os.path.isabs(executable):
+ executable = str(Path(kwargs.get('cwd') or os.getcwd()) / executable)
+ if shutil.which(executable, path=(kwargs.get('env') or os.environ).get('PATH')) is None:
+ raise FileNotFoundError(2, 'Install executable not found', str(command[0]))
+ command = module_command('freevideo_engine.linux_process_host',
+ os.getpid(), ','.join(map(str, pass_fds)), *command)
+ return subprocess.Popen(command, pass_fds=pass_fds, **kwargs)
+
+
+def stop(process, grace=10):
+ if hasattr(process, 'stop_request'):
+ return process.stop_request(grace=grace)
+ if process.poll() is not None:
+ return
+ try:
+ if windows():
+ process.send_signal(signal.CTRL_BREAK_EVENT)
+ else:
+ os.killpg(process.pid, signal.SIGTERM)
+ except (ProcessLookupError, PermissionError, OSError):
+ pass
+ try:
+ process.wait(timeout=grace)
+ except subprocess.TimeoutExpired:
+ if windows():
+ process.kill()
+ else:
+ try:
+ os.killpg(process.pid, signal.SIGKILL)
+ except ProcessLookupError:
+ pass
+ process.wait(timeout=5)
+
+
+def run(command, *, timeout=None, check=False, capture_output=False, input=None, **kwargs):
+ if capture_output:
+ kwargs.update(stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ if input is not None:
+ kwargs['stdin'] = subprocess.PIPE
+ kwargs.setdefault('start_new_session', True)
+ with popen(command, **kwargs) as process:
+ try:
+ stdout, stderr = process.communicate(input, timeout=timeout)
+ except BaseException:
+ stop(process)
+ raise
+ result = subprocess.CompletedProcess(command, process.returncode, stdout, stderr)
+ if check:
+ result.check_returncode()
+ return result
+
+
+def termination_handler(handler):
+ """Enter cancellation once; repeated signals must not interrupt cleanup.
+
+ A supervisor may signal both a process group and its adopted children.
+ Raising again while stop()/report settlement is running turns a confirmed
+ user cancellation into an unacknowledged worker exit. Forced termination
+ remains the supervising process's bounded SIGKILL / Job responsibility.
+ """
+ names = (signal.SIGINT, signal.SIGTERM, signal.SIGBREAK) if windows() else (signal.SIGINT, signal.SIGTERM)
+ cancelled = False
+ def once(signum, frame):
+ nonlocal cancelled
+ if not cancelled:
+ cancelled = True
+ handler(signum, frame)
+ return {name: signal.signal(name, once) for name in names}
+
+
+def restore_handlers(previous):
+ for name, handler in previous.items():
+ signal.signal(name, handler)
+
+
+@contextmanager
+def worker_signals():
+ """Let Windows workers finalize stage reports before the Job timeout."""
+ def interrupted(signum, frame):
+ raise KeyboardInterrupt('Worker interrupted by signal %s' % signum)
+ previous = termination_handler(interrupted) if windows() else {}
+ try:
+ yield
+ finally:
+ restore_handlers(previous)
diff --git a/freevideo_engine/provider_resume.py b/freevideo_engine/provider_resume.py
new file mode 100644
index 0000000..c8ed27d
--- /dev/null
+++ b/freevideo_engine/provider_resume.py
@@ -0,0 +1,178 @@
+"""Durable transfers around the pinned providers' metadata and range clients."""
+from concurrent.futures import ThreadPoolExecutor
+import hashlib
+import os
+from pathlib import Path
+
+from . import network
+from .download_cache import discard_read_cache
+from .file_ops import publish
+
+BLOCK = 4 * 2**20
+
+
+class ResumeUnavailable(RuntimeError):
+ pass
+
+
+def ms_completed(target, total, part_size):
+ target = Path(target)
+ if target.is_file():
+ return min(total, target.stat().st_size)
+ merged = target.with_suffix(target.suffix+'.parallel_tmp')
+ prefix = merged.stat().st_size if merged.is_file() else 0
+ done = min(total, prefix)
+ for start in range(0, total, part_size):
+ end = min(total, start+part_size)
+ part = target.with_name('%s_%d_%d' % (target.name, start, end-1))
+ try:
+ done += max(0, min(end, start+part.stat().st_size)-max(start, prefix))
+ except FileNotFoundError:
+ pass
+ return min(total, done)
+
+
+def ms_parallel(module, url, target, file_size, headers, cookies, progress_callbacks=None):
+ """Resume both range parts and a merge interrupted after parts were removed.
+
+ The SDK normally opens its merge file with 'wb'. Keep its concurrent Range
+ client, but append to our retained prefix and flush before deleting a part.
+ Final pinned content verification is still required by the caller.
+ """
+ callbacks = progress_callbacks or []
+ target = Path(target)
+ target.parent.mkdir(parents=True, exist_ok=True)
+ merged = target.with_suffix(target.suffix + '.parallel_tmp')
+ offset = merged.stat().st_size if merged.exists() else 0
+ if offset > file_size:
+ raise ValueError('Retained merge exceeds the pinned model size')
+ if offset % module.DOWNLOAD_PART_SIZE:
+ # A switched HTTP download may end in the middle of an SDK part.
+ # Seed that part so its first Range starts after the retained prefix.
+ # Only the one intersecting part is copied, using bounded buffers.
+ start=offset-offset%module.DOWNLOAD_PART_SIZE
+ end=min(file_size,start+module.DOWNLOAD_PART_SIZE)
+ part=target.with_name('%s_%d_%d' % (target.name,start,end-1))
+ existing=part.stat().st_size if part.exists() else 0
+ if existing > end-start:
+ raise ValueError('Retained part exceeds its range')
+ with merged.open('rb') as prefix, part.open('a+b') as output:
+ prefix.seek(start); output.seek(0)
+ remaining=min(existing,offset-start)
+ while remaining:
+ count=min(BLOCK,remaining)
+ if prefix.read(count)!=output.read(count):
+ raise ValueError('Retained part differs from switched prefix')
+ remaining-=count
+ if existing<offset-start:
+ prefix.seek(start+existing); output.seek(0,2)
+ remaining=offset-start-existing
+ while remaining:
+ block=prefix.read(min(BLOCK,remaining))
+ if not block:raise ValueError('Retained prefix changed while resuming')
+ output.write(block); remaining-=len(block)
+ discard_read_cache(prefix.fileno(),prefix.tell()-len(block),len(block))
+ output.flush(); os.fsync(output.fileno())
+ # A crash after fsync but before unlink can leave a duplicate complete
+ # part. Compare it with the merged prefix before releasing the duplicate.
+ for start in range(0, offset, module.DOWNLOAD_PART_SIZE):
+ end = min(file_size, start+module.DOWNLOAD_PART_SIZE)
+ part = target.with_name('%s_%d_%d' % (target.name, start, end-1))
+ if end <= offset and part.is_file():
+ if part.stat().st_size != end-start:
+ raise ValueError('Unexpected size of a retained merged part')
+ with merged.open('rb') as prefix, part.open('rb') as stream:
+ prefix.seek(start)
+ for block in iter(lambda: stream.read(BLOCK), b''):
+ if block != prefix.read(len(block)):
+ raise ValueError('Retained part differs from merged prefix')
+ discard_read_cache(prefix.fileno(), prefix.tell()-len(block), len(block))
+ discard_read_cache(stream.fileno(), stream.tell()-len(block), len(block))
+ part.unlink()
+ tasks = [(str(target), callbacks, start, min(file_size, start+module.DOWNLOAD_PART_SIZE)-1,
+ url, target.name, headers, cookies)
+ for start in range(0, file_size, module.DOWNLOAD_PART_SIZE)
+ if min(file_size, start+module.DOWNLOAD_PART_SIZE) > offset]
+ with ThreadPoolExecutor(max_workers=min(module.DOWNLOAD_PARALLELS, 16)) as pool:
+ list(pool.map(module._download_part_with_retry, tasks))
+ for callback in callbacks:
+ callback.end()
+ digest = hashlib.sha256()
+ if offset:
+ with merged.open('rb') as prefix:
+ for block in iter(lambda: prefix.read(BLOCK), b''):
+ digest.update(block)
+ discard_read_cache(prefix.fileno(), prefix.tell()-len(block), len(block))
+ with merged.open('ab') as output:
+ for task in tasks:
+ start, end = task[2:4]
+ part = Path('%s_%d_%d' % (target, start, end))
+ if part.stat().st_size != end-start+1:
+ raise ValueError('Incomplete retained model range')
+ with part.open('rb') as stream:
+ stream.seek(max(0, offset-start))
+ for block in iter(lambda: stream.read(BLOCK), b''):
+ output.write(block)
+ digest.update(block)
+ discard_read_cache(stream.fileno(), stream.tell()-len(block), len(block))
+ output.flush()
+ os.fsync(output.fileno())
+ offset = end+1
+ # The durable merged prefix owns these bytes now. A crash before
+ # unlink leaves overlap, which the next merge skips without replay.
+ part.unlink()
+ if merged.stat().st_size != file_size:
+ raise ValueError('Merged model has an unexpected size')
+ publish(merged, target)
+ return digest.hexdigest()
+
+
+def hf_download(request, module, progress, *, incomplete_path, destination_path, url_to_download,
+ headers, expected_size, filename, force_download, etag, xet_file_data, tqdm_class=None,
+ backend_callback=None):
+ """Keep HTTP prefixes; never pretend an Xet output is a resumable prefix.
+
+ Hub 1.30 creates a fresh UUID temporary file and unlinks it on exceptions.
+ FreeVideo owns the installation lease, so it can retain a stable file here.
+ Metadata/authentication and the actual Xet engine remain provider-owned.
+ """
+ row, stage = request['row'], Path(request['stage'])
+ destination = Path(destination_path)
+ if destination.is_file() and not force_download:
+ return
+ if expected_size is not None and expected_size != row['bytes']:
+ raise ValueError('Hub metadata size differs from the pinned model')
+ for old in stage.rglob('*.incomplete'):
+ if old.name != 'model.xet.incomplete' and old.is_file() and old.stat().st_size:
+ if not request.get('allow_model_restart'):
+ raise ResumeUnavailable('Older SDK partial has no reliable resume metadata; explicit restart required')
+ network.retain_partial(old, 'restart-approved')
+ http_partial = destination.with_suffix(destination.suffix + '.partial')
+ continuing_http = http_partial.is_file() and http_partial.stat().st_size > 0
+ if xet_file_data is not None and module.is_xet_available() and not continuing_http:
+ temporary = stage / 'model.xet.incomplete'
+ if temporary.exists() and temporary.stat().st_size:
+ complete = temporary.stat().st_size == row['bytes'] and network.hash_file(temporary,
+ 'sha256' if row.get('sha256') else 'sha1', not row.get('sha256'), discard_cache=True) == (row.get('sha256') or row['git_blob'])
+ if complete:
+ publish(temporary, destination)
+ return
+ if not request.get('allow_model_restart'):
+ raise ResumeUnavailable('Xet cannot resume this retained output; an explicit restart is required')
+ network.retain_partial(temporary, 'restart-approved')
+ module.xet_get(incomplete_path=temporary, xet_file_data=xet_file_data, headers=headers,
+ expected_size=row['bytes'], displayed_filename=filename, tqdm_class=tqdm_class)
+ publish(temporary, destination)
+ else:
+ if request.get('require_xet'):
+ raise ResumeUnavailable('HF selected HTTP but --model-downloader xet requires Xet; no fallback performed')
+ if backend_callback:
+ backend_callback('hf-http')
+ # Let curl resume only a real contiguous HTTP prefix. It validates the
+ # pinned digest and never appends an ignored Range response.
+ network.download([(request['source'], url_to_download)], destination, row.get('sha256') or row['git_blob'],
+ lambda done, total, rate: progress(done, total, rate), size=row['bytes'],
+ network={'allow_model_restart': request.get('allow_model_restart', False)},
+ headers_for=lambda _: [key + ': ' + value for key, value in headers.items()],
+ algorithm='sha256' if row.get('sha256') else 'sha1', git_blob=not row.get('sha256'),
+ category=network.model_family(row))
diff --git a/freevideo_engine/provision.py b/freevideo_engine/provision.py
new file mode 100644
index 0000000..995dadc
--- /dev/null
+++ b/freevideo_engine/provision.py
@@ -0,0 +1,397 @@
+"""Pinned model download and bounded cache preparation inside the new environment."""
+import argparse
+import json
+import os
+from pathlib import Path
+import time
+import threading
+from concurrent.futures import ThreadPoolExecutor, CancelledError
+
+from .bootstrap import PACKAGE, model_target
+from .locking import runtime_lock
+from .monitoring import save
+from .install_tuning import required_models, hf_receipt, unchanged_before
+from . import network
+from .storage import record_download, compact_sources, fingerprint
+from . import model_transfer
+
+
+def model_headers(source):
+ """Automatic public mirrors never receive the user's Hub token."""
+ from huggingface_hub import get_token
+ token = get_token() if source in ('official', 'user') else None
+ return ['Authorization: Bearer ' + token] if token else []
+
+
+def file_identity(path, row):
+ return dict(fingerprint(path), sha256=row.get('sha256'), git_blob=row.get('git_blob'))
+
+
+def verified(path, row, stamps, *, mode='auto', hf_directory=None, producer_receipt=None, progress=None):
+ if not path.is_file() or path.stat().st_size != row['bytes']:
+ return False
+ identity = file_identity(path, row)
+ prior = stamps.get(str(path), {})
+ reliable = 'change_time_ns' not in identity or identity['change_time_ns'] is not None
+ if mode == 'auto' and reliable:
+ # Recently closed Windows files can share a filesystem clock tick.
+ # Hash those again instead of trusting a same-tick identity collision.
+ settled = ('change_time_ns' not in identity or
+ max(identity['mtime_ns'], identity['change_time_ns']) < time.time_ns() - 10**9)
+ if settled and isinstance(prior, dict) and all(prior.get(k) == v for k, v in identity.items()):
+ return True
+ method = None
+ if not prior and hf_directory and hf_receipt(path, row, hf_directory):
+ method = 'pinned_huggingface_download_receipt_unchanged_file'
+ if not prior and producer_receipt and unchanged_before(path, producer_receipt):
+ method = 'local_conversion_hash_manifest_unchanged_file'
+ if method:
+ stamps[str(path)] = dict(identity, method=method)
+ return True
+ kwargs = dict(discard_cache=True)
+ if progress is not None:
+ kwargs['progress'] = progress
+ matches = (network.hash_file(path, **kwargs) == row['sha256'] if row.get('sha256') else
+ network.hash_file(path, 'sha1', git_blob=True, **kwargs) == row['git_blob'])
+ if matches:
+ stamps[str(path)] = dict(identity, method='full_content_hash')
+ return matches
+
+
+def models(plan):
+ # Use the same canonical spelling as compact_sources/ownership, including
+ # Windows 8.3 temporary-directory aliases. Keep managed_model's link and
+ # ownership checks intact; a different spelling must not lose the receipt.
+ root = Path(plan['root']).expanduser().resolve()
+ ledger = root / 'verified-models.json'
+ stamps = json.loads(ledger.read_text(encoding='utf-8')) if ledger.is_file() else {}
+ model_dir, encoder_dir = (Path(plan[name]).expanduser().resolve() for name in ('model_dir', 'encoder_dir'))
+ from . import prepared_model
+ prepared = plan.get('prepared_model')
+ files = list(required_models(json.loads((PACKAGE / 'model_files.json').read_text(encoding='utf-8')), plan.get('reuse_cache') or prepared))
+ files += prepared_model.files(prepared)
+ mode = plan.get('verification', 'auto')
+ from .local_models import key as local_key, import_file
+ local_files, missing = [], []
+ destinations = set()
+ # Cheap metadata only: start known downloads without first hashing tens of
+ # gigabytes of unrelated local weights. A local match is never downloaded.
+ for row in files:
+ path = model_target(row, model_dir, encoder_dir, prepared['directory'] if prepared else None)
+ hf_directory = Path(prepared['directory']) if row.get('prepared') else model_dir if row['repo'].startswith('OpenVDN/') else encoder_dir
+ if path in destinations:
+ raise ValueError('Duplicate model destination in installation plan: ' + str(path))
+ destinations.add(path)
+ local = (plan.get('local_models') or {}).get('matches', {}).get(local_key(row))
+ entry = (row, path, hf_directory, local)
+ (local_files if path.exists() or path.is_symlink() or local else missing).append(entry)
+ stopped = threading.Event()
+ lock = threading.RLock()
+ failures = []
+ ready = [0]
+
+ def check():
+ if stopped.is_set():
+ raise CancelledError('Parallel model preparation stopped; all files retained')
+
+ def emit(event, **details):
+ with lock:
+ # One write preserves JSON lines when the verifier and downloader
+ # both report progress. The only retained buffers are hash chunks.
+ import sys
+ sys.stdout.write(json.dumps(dict(event=event, **details)) + '\n')
+ sys.stdout.flush()
+
+ from .model_status import ModelProgress
+ model_progress = ModelProgress([(entry[0], 'found') for entry in local_files] +
+ [(entry[0], 'download') for entry in missing], emit)
+ model_progress.update()
+
+ def complete(row, path, stamp, operation, origin=None):
+ with lock:
+ stamps[str(path)] = stamp
+ if origin:
+ record_download(root, path, row, origin=origin)
+ save(ledger, stamps)
+ ready[0] += 1
+ emit('model_ready', index=ready[0], total=len(files), file=str(path),
+ verification=stamp.get('method'), operation=operation)
+ model_progress.update(row, phase='ready', done=row['bytes'], local=operation != 'download')
+
+ def verify_local():
+ try:
+ for index, (row, path, hf_directory, local) in enumerate(local_files):
+ check()
+ started, last = time.monotonic(), [0.]
+ def progress(done, total):
+ check()
+ now = time.monotonic()
+ if done == 0 or done == total or now - last[0] >= .5:
+ emit('model_verification_progress', file=path.name, done_bytes=done,
+ total_bytes=total, bytes_per_second=done / max(.001, now-started),
+ files_done=index, files_total=len(local_files), state='running')
+ model_progress.update(row, phase='verifying')
+ last[0] = now
+ progress(0, row['bytes'])
+ prior = {str(path): stamps.get(str(path), {})}
+ if verified(path, row, prior, mode=mode, hf_directory=hf_directory, progress=progress):
+ complete(row, path, prior[str(path)], 'verify')
+ elif path.exists() or path.is_symlink():
+ raise ValueError('Existing model failed pinned integrity check; preserved without overwrite: ' + str(path))
+ elif local:
+ def local_progress(value):
+ progress(value.get('done_bytes', 0), value.get('total_bytes', row['bytes']))
+ method = import_file(row, path, local, callback=local_progress)
+ complete(row, path, dict(file_identity(path, row), method='verified_local_' + method),
+ 'verify', 'local-copy' if method == 'copy' else None)
+ else:
+ # The reviewed existing file disappeared. A changed plan
+ # must not silently enlarge the approved download set.
+ raise FileNotFoundError('Local model disappeared; inspect setup again: ' + str(path))
+ emit('model_verification_progress', files_done=len(local_files), files_total=len(local_files), state='complete')
+ except BaseException as error:
+ failures.append(error)
+ stopped.set()
+ emit('model_verification_progress', state='failed', files_total=len(local_files))
+ raise
+
+ def download_missing():
+ networking = dict(plan.get('network', {}))
+ parent_check = networking.get('resource_check')
+ def resource_check():
+ check()
+ if parent_check:
+ parent_check()
+ networking['resource_check'] = resource_check
+ download_plan = dict(plan, network=networking)
+ requested_workers = (plan.get('model_transfer') or {}).get('file_workers', 1)
+ try:
+ workers = max(1, min(len(missing), int(requested_workers)))
+ except (TypeError, ValueError):
+ workers = 1
+ def download_one(entry, transfer_ready=None):
+ row, path, hf_directory, _ = entry
+ check()
+ # A user may copy a file in while setup is active. Do not claim
+ # ownership of, replace, or download over that newly present file.
+ if path.exists() or path.is_symlink():
+ prior = {}
+ if not verified(path, row, prior, mode=mode, hf_directory=hf_directory):
+ raise ValueError('Model destination appeared with unexpected content: ' + str(path))
+ complete(row, path, prior[str(path)], 'verify')
+ return
+ emit('download_model', file=str(path), bytes=row['bytes'])
+ model_progress.update(row, phase='downloading')
+ last_progress = [0.]
+ def progress(done, total, speed, **extra):
+ check()
+ if time.monotonic() - last_progress[0] >= 1:
+ emit('download_progress', description=path.name, done_bytes=done,
+ total_bytes=total, bytes_per_second=speed, **extra)
+ model_progress.update(row, phase='verifying' if done == row['bytes'] else 'downloading', done=done, rate=speed)
+ last_progress[0] = time.monotonic()
+ def transfer_complete():
+ # This event is emitted before the provider performs its
+ # final pinned hash check. Other file workers may already
+ # be transferring the next shard, so the UI can distinguish
+ # network completion from verified publication.
+ emit('download_transfer_complete', file=str(path), bytes=row['bytes'])
+ model_progress.update(row, phase='verifying', done=row['bytes'], rate=None)
+ if transfer_ready is not None:
+ transfer_ready()
+ try:
+ if workers > 1:
+ model_transfer.download(row, path, download_plan, progress, model_headers,
+ transfer_complete=transfer_complete)
+ else:
+ # Keep compatibility with small test/tool integrations
+ # that implement the historic five-argument hook.
+ model_transfer.download(row, path, download_plan, progress, model_headers)
+ except BaseException:
+ model_progress.update(row, phase='paused')
+ raise
+ # download verified the complete content before renaming. Save the
+ # final inode stamp without a second pass through large model files.
+ complete(row, path, dict(file_identity(path, row), method='full_content_hash_resumable_download'),
+ 'download', 'download')
+ # Explicitly preserve the old one-file behavior for plans without the
+ # new policy field. When enabled, use a two-slot pipeline: one child
+ # can finish its pinned hash while the next child transfers. The
+ # transfer-ready callback dispatches the next file only after the
+ # current body has ended, preventing multiple large network bodies
+ # from being started at once.
+ if workers == 1:
+ for entry in missing:
+ download_one(entry)
+ return
+ # Do not turn this into an unbounded file pool: each transfer child
+ # has its own native range buffers and process guard. Two slots are
+ # enough to overlap network I/O with the previous file's final hash.
+ condition = threading.Condition()
+ entries = iter(missing)
+ futures, state = set(), {'active': 0, 'finished': 0, 'error': None}
+ pool = ThreadPoolExecutor(max_workers=2, thread_name_prefix='model-download')
+
+ def submit_next():
+ with condition:
+ if stopped.is_set():
+ return False
+ try:
+ entry = next(entries)
+ except StopIteration:
+ return False
+ state['active'] += 1
+ # The callback is invoked before the worker returns, while
+ # its final integrity check is still running. It may submit
+ # the next file to the second slot.
+ future = pool.submit(download_one, entry, submit_next)
+ futures.add(future)
+ future.add_done_callback(finished)
+ return True
+
+ def finished(future):
+ error = None
+ try:
+ if future.cancelled():
+ error = CancelledError()
+ else:
+ error = future.exception()
+ except BaseException as caught:
+ error = caught
+ with condition:
+ state['active'] -= 1
+ state['finished'] += 1
+ if error is not None and state['error'] is None:
+ state['error'] = error
+ stopped.set()
+ # Files without a transport callback (a copied-in file or a
+ # tiny fallback transfer) still advance the pipeline after
+ # they finish.
+ if error is None and state['active'] == 0 and state['finished'] < len(missing):
+ submit_next()
+ condition.notify_all()
+
+ try:
+ submit_next()
+ with condition:
+ while state['finished'] < len(missing) and state['error'] is None:
+ condition.wait(.2)
+ if state['error'] is not None:
+ raise state['error']
+ finally:
+ if state['error'] is not None:
+ stopped.set()
+ for future in futures:
+ future.cancel()
+ pool.shutdown(wait=True, cancel_futures=True)
+ # One bounded disk verifier plus a bounded native multi-connection
+ # downloader. Same-file SDK concurrency and its live RAM guard remain
+ # unchanged; file_workers only overlaps separate files.
+ with ThreadPoolExecutor(max_workers=1, thread_name_prefix='model-verification') as pool:
+ verification = pool.submit(verify_local) if local_files else None
+ try:
+ download_missing()
+ if verification is not None:
+ verification.result()
+ except BaseException:
+ stopped.set()
+ model_progress.update(phase='paused')
+ if failures:
+ raise failures[0]
+ raise
+ # JSON is valid YAML and safely represents spaces and punctuation in paths.
+ save(root / 'encoder-paths.yaml', {'freevideo': {'base_path': str(encoder_dir), 'text_encoders': 'text_encoders/'}})
+
+
+def verify_cache(plan, cache):
+ """Verify every expected group before acknowledging readiness or cleanup."""
+ cache = Path(cache).resolve()
+ manifest = json.loads((cache / 'manifest.json').read_text(encoding='utf-8'))
+ config = json.loads((Path(plan['model_dir']) / 'h3-base/transformer/config.json').read_text(encoding='utf-8'))
+ count = config['num_layers']
+ from .adaln_assets import SLIM_FORMAT, validate_catalog, check_table, asset_path
+ slim = manifest.get('format') == SLIM_FORMAT
+ kinds = ('blocks',) if slim else ('blocks', 'adaln')
+ expected = {'root'} | {kind + '/' + '%02d' % i for kind in kinds for i in range(count)}
+ groups = manifest.get('groups', [])
+ if (manifest.get('precision') != 'fp8' or not manifest.get('linears')
+ or len(groups) != len(expected) or {r.get('group') for r in groups} != expected):
+ raise ValueError('FP8 cache is not complete; original model files retained')
+ ledger = Path(plan['root']) / 'verified-cache.json'
+ stamps = json.loads(ledger.read_text(encoding='utf-8')) if ledger.is_file() else {}
+ for row in groups:
+ if row['file'] != row['group'] + '.safetensors':
+ raise ValueError('Unexpected FP8 group path; original files retained')
+ if not verified(cache / row['file'], row, stamps, mode=plan.get('verification', 'auto'),
+ producer_receipt=cache / 'manifest.json'):
+ raise ValueError('Existing FP8 cache failed integrity check: ' + row['file'] + '. Rerun setup with --rebuild-cache; failed files are retained.')
+ for row, identity in validate_catalog(manifest, count):
+ path = asset_path(cache, row['file'])
+ if not verified(path, row, stamps, mode=plan.get('verification', 'auto')):
+ raise ValueError('AdaLN model asset failed integrity check: ' + row['file'])
+ check_table(path, row, identity, verify_hash=False)
+ save(ledger, stamps)
+ return manifest
+
+
+def prepare(plan, out):
+ root = Path(plan['root'])
+ prepared = plan.get('prepared_model')
+ if prepared:
+ from .prepared_model import verify_manifest
+ verify_manifest(prepared)
+ cache = Path(prepared['cache'])
+ from .install_tuning import cache_compatible
+ if not cache_compatible(cache, plan['inventory']['hardware']['capability']):
+ raise ValueError('Downloaded prepared model differs from this GPU scale format')
+ elif plan.get('reuse_cache'):
+ cache = Path(plan['reuse_cache'])
+ from .install_tuning import cache_compatible
+ manifest = json.loads((cache / 'manifest.json').read_text(encoding='utf-8'))
+ if manifest.get('precision') != 'fp8':
+ raise ValueError('Reuse cache must contain FP8 storage.')
+ if not cache_compatible(cache, plan['inventory']['hardware']['capability']):
+ raise ValueError('FP8 scale format differs from this GPU. Omit --cache to prepare the correct one.')
+ else:
+ from .paths import add_vdn
+ add_vdn()
+ import torch
+ torch.set_num_threads(min(4, os.cpu_count() or 1))
+ from .fp8 import prepare_streamed
+ model_dir = Path(plan['model_dir'])
+ base, checkpoint = model_dir / 'h3-base', model_dir / 'stage-dmd-step-250'
+ with runtime_lock():
+ cache = prepare_streamed(base, checkpoint, root / 'prepared')
+ verify_cache(plan, cache)
+ record = {'cache': str(cache), 'verified_at_epoch': time.time(),
+ 'model_revision': json.loads((PACKAGE / 'dependencies.json').read_text(encoding='utf-8'))['models']['vdn_revision']}
+ if prepared:
+ record['prepared_model'] = {k: prepared[k] for k in ('repo', 'revision', 'scale_granularity')}
+ save(out, record)
+ save(root / 'prepared-cache.json', record)
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--plan', required=True, type=Path)
+ operation = parser.add_mutually_exclusive_group()
+ operation.add_argument('--prepare', action='store_true')
+ operation.add_argument('--cleanup', action='store_true')
+ parser.add_argument('--out', type=Path)
+ args = parser.parse_args()
+ if (args.prepare or args.cleanup) and args.out is None:
+ parser.error('--out is required for preparation and cleanup reports')
+ plan = json.loads(args.plan.read_text(encoding='utf-8'))
+ if args.cleanup:
+ record = json.loads((Path(plan['root']) / 'prepared-cache.json').read_text(encoding='utf-8'))
+ verify_cache(plan, record['cache'])
+ result = compact_sources(plan, json.loads((PACKAGE / 'model_files.json').read_text(encoding='utf-8')), args.out)
+ print(json.dumps({'event': 'storage_compacted', **result}), flush=True)
+ elif args.prepare:
+ prepare(plan, args.out)
+ else:
+ models(plan)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freevideo_engine/proxy.py b/freevideo_engine/proxy.py
new file mode 100644
index 0000000..d54c3a6
--- /dev/null
+++ b/freevideo_engine/proxy.py
@@ -0,0 +1,122 @@
+"""Per-child proxy routes; never edit the user's shell or Git configuration."""
+import os
+import re
+import shlex
+import shutil
+import subprocess
+import sys
+from urllib import request
+
+
+PROXY_KEYS = ('http_proxy', 'https_proxy', 'all_proxy', 'no_proxy',
+ 'HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY', 'NO_PROXY')
+MODES = ('auto', 'proxy', 'direct')
+GIT_PROXY_PATTERN = r'^(http\..*proxy|https\.proxy|remote\..*\.proxy|core\.gitproxy)$'
+
+
+def inherited(environ=None):
+ env = dict(os.environ if environ is None else environ)
+ # GUI processes on Windows often have no proxy environment variables.
+ # Read the user's OS proxy without editing its settings. Explicit child
+ # overrides (including NO_PROXY) always take precedence.
+ if not any(k in env for k in PROXY_KEYS):
+ reader = getattr(request, 'getproxies_registry' if sys.platform == 'win32'
+ else 'getproxies_macosx_sysconf' if sys.platform == 'darwin' else '', None)
+ if reader:
+ try:
+ for name, value in reader().items():
+ if name in ('http', 'https', 'all', 'no') and value:
+ env[name + '_proxy'] = str(value)
+ except (OSError, ValueError):
+ pass
+ for name in ('http', 'https', 'all', 'no'):
+ lower, upper = name + '_proxy', name.upper() + '_PROXY'
+ if lower not in env and upper in env:
+ env[lower] = env[upper]
+ return env
+
+
+def configured(environ=None):
+ env = os.environ if environ is None else environ
+ return any(v for k, v in env.items() if k.lower() in ('http_proxy', 'https_proxy', 'all_proxy'))
+
+
+def git_proxy_keys(env):
+ """Read names only. URL-scoped keys themselves can contain credentials."""
+ if not shutil.which('git', path=env.get('PATH')):
+ return []
+ try:
+ result = subprocess.run(['git', 'config', '--null', '--name-only', '--get-regexp', GIT_PROXY_PATTERN],
+ env=env, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, timeout=3)
+ return list(dict.fromkeys(k for k in result.stdout.decode('utf-8', errors='replace').split('\0')
+ if k and '\n' not in k and '\r' not in k)) if result.returncode == 0 else []
+ except (OSError, subprocess.SubprocessError):
+ return []
+
+
+def git_environment(environ, settings):
+ env = dict(environ)
+ index = int(env.get('GIT_CONFIG_COUNT', 0))
+ if not 0 <= index <= 1024:
+ raise ValueError('Invalid inherited Git configuration count')
+ for key, value in settings.items():
+ env['GIT_CONFIG_KEY_' + str(index)] = key
+ env['GIT_CONFIG_VALUE_' + str(index)] = value
+ index += 1
+ env['GIT_CONFIG_COUNT'] = str(index)
+ return env
+
+
+def direct(environ=None, *, git=False, keys=None):
+ env = inherited(environ)
+ for key in list(env):
+ if key.lower() in ('http_proxy', 'https_proxy', 'all_proxy', 'no_proxy'):
+ env.pop(key)
+ # Also suppress OS-discovered HTTP proxies in clients that consult them.
+ env.update(NO_PROXY='*', no_proxy='*')
+ env['FREEVIDEO_PROXY_ROUTE'] = 'direct'
+ # pip can also obtain a proxy from its own configuration file.
+ env.pop('PIP_PROXY', None)
+ env['PIP_CONFIG_FILE'] = os.devnull
+ if git:
+ keys = git_proxy_keys(env) if keys is None else keys
+ env.pop('GIT_PROXY_COMMAND', None)
+ parameters = env.get('GIT_CONFIG_PARAMETERS')
+ if parameters:
+ # Git propagates -c options as shell-quoted key=value words. Keep
+ # unrelated options, remove only proxy settings for this child.
+ try:
+ values = shlex.split(parameters)
+ except ValueError:
+ values = [] # Malformed inherited arguments cannot be used by Git.
+ values = [v for v in values if not re.match(GIT_PROXY_PATTERN, v.split('=', 1)[0], re.I)]
+ env['GIT_CONFIG_PARAMETERS'] = ' '.join(shlex.quote(v) for v in values)
+ try:
+ valid_count = 0 <= int(env.get('GIT_CONFIG_COUNT', 0)) <= 1024
+ except ValueError:
+ valid_count = False
+ if not valid_count:
+ for key in list(env):
+ if key == 'GIT_CONFIG_COUNT' or key.startswith(('GIT_CONFIG_KEY_', 'GIT_CONFIG_VALUE_')):
+ env.pop(key)
+ overrides = {key: '' for key in ['http.proxy', 'https.proxy', 'remote.origin.proxy', *keys]}
+ overrides['core.gitproxy'] = 'none'
+ env = git_environment(env, overrides)
+ return env
+
+
+def routes(environ=None, *, git=False, mode='auto'):
+ if mode not in MODES:
+ raise ValueError('Unknown download connection mode')
+ if mode == 'proxy':
+ return ['inherited']
+ if mode == 'direct':
+ return ['direct']
+ env = inherited(environ)
+ return ['inherited', 'direct'] if configured(env) or env.get('PIP_PROXY') or (git and (env.get('GIT_PROXY_COMMAND') or git_proxy_keys(env))) else ['inherited']
+
+
+def environment(environ, route, *, git=False):
+ if route not in ('inherited', 'direct'):
+ raise ValueError('Unknown proxy route')
+ return direct(environ, git=git) if route == 'direct' else inherited(environ)
diff --git a/freevideo_engine/ram.py b/freevideo_engine/ram.py
new file mode 100644
index 0000000..c7325c6
--- /dev/null
+++ b/freevideo_engine/ram.py
@@ -0,0 +1,268 @@
+"""Observed process memory and dedicated cgroup limits; no GPU dependencies."""
+from pathlib import Path
+from .system import windows
+
+
+def inference_working_pss(rollup, protected_file_bytes=None):
+ """Keep anonymous/shared/dirty pages; clean file pages may be reclaimed.
+
+ The raw PSS remains the reported footprint; cgroups enforce capacity.
+ Dirty/writeback/unevictable filesystem bytes must be bounded separately:
+ Pss_Dirty also includes anonymous pages, so max(Pss-Pss_File, Pss_Dirty)
+ alone could discard dirty file pages. Missing counters earn no credit.
+ """
+ total = rollup['Pss']
+ file_pages, dirty = rollup.get('Pss_File'), rollup.get('Pss_Dirty')
+ if (any(type(value) is not int or not 0 <= value <= total for value in (file_pages, dirty))
+ or type(protected_file_bytes) is not int or protected_file_bytes < 0):
+ return total
+ return max(total - max(0, file_pages - protected_file_bytes), dirty)
+
+
+class ProcessMemory:
+ """Sample worker/descendant RSS and PSS without pretending to enforce RAM.
+
+ PSS apportions shared resident mappings. Neither RSS nor PSS includes the
+ worker's unmapped file cache. MemAvailable is system-wide, not attribution.
+ """
+
+ def __init__(self):
+ self.windows = None
+ if windows():
+ from .win32 import ProcessMemory as WindowsMemory
+ self.windows = WindowsMemory()
+ self.peak_rss = self.peak_pss = self.samples = 0
+ self.peak_inference_pss = 0
+ self.minimum_available = None
+ self.minimum_effective_available = None
+ self.minimum_inference_available = None
+ self.last_cgroup = None
+ self.pss_complete = True
+ self.process_io = {}
+ self.peak_tree_read = self.peak_tree_write = 0
+
+ @staticmethod
+ def kilobytes(path):
+ return {key: int(value.split()[0]) * 1024 for key, value in
+ (line.split(':', 1) for line in path.read_text(encoding='utf-8').splitlines() if ':' in line)
+ if value.strip().endswith('kB')}
+
+ def sample(self, root_pid):
+ if self.windows is not None:
+ return self.windows.sample(root_pid)
+ parents = {}
+ for path in Path('/proc').iterdir():
+ if path.name.isdigit():
+ try:
+ fields = (path / 'stat').read_text(encoding='utf-8').rsplit(')', 1)[1].split()
+ parents[int(path.name)] = int(fields[1])
+ except (OSError, ValueError, IndexError):
+ continue
+ pids, frontier, ordered = {root_pid}, {root_pid}, [root_pid]
+ while frontier:
+ frontier = {pid for pid, parent in parents.items() if parent in frontier} - pids
+ pids.update(frontier)
+ ordered.extend(sorted(frontier))
+ rss = pss = file_pss = dirty_pss = count = 0
+ file_counters_complete = True
+ tree_read = tree_write = 0
+ incomplete = []
+ # /proc/PID/io includes waited-for children. Read parents before live
+ # descendants, and never add saved counters from exited PIDs again.
+ for pid in ordered:
+ directory = Path('/proc') / str(pid)
+ try:
+ io = {key: int(value) for key, value in
+ (line.split(':', 1) for line in (directory / 'io').read_text(encoding='utf-8').splitlines())}
+ # Keep exited children's last observed counters, keyed by PID
+ # and start tick so PID reuse cannot combine unrelated workers.
+ fields = (directory / 'stat').read_text(encoding='utf-8').rsplit(')', 1)[1].split()
+ start_tick = fields[19]
+ self.process_io[(pid, start_tick)] = (io['read_bytes'], io['write_bytes'])
+ if fields[0] != 'Z':
+ tree_read += io['read_bytes']
+ tree_write += io['write_bytes']
+ except (OSError, ValueError, KeyError, IndexError):
+ pass
+ try:
+ status = self.kilobytes(directory / 'status')
+ if 'VmRSS' not in status:
+ continue # Exited/zombie processes no longer own mappings.
+ current_rss = status['VmRSS']
+ count += 1
+ except OSError:
+ continue
+ try:
+ rollup = self.kilobytes(directory / 'smaps_rollup')
+ current_rss = rollup['Rss']
+ pss += rollup['Pss']
+ file_counters_complete &= all(type(rollup.get(key)) is int and 0 <= rollup[key] <= rollup['Pss']
+ for key in ('Pss_File', 'Pss_Dirty'))
+ file_pss += rollup.get('Pss_File', 0)
+ dirty_pss += rollup.get('Pss_Dirty', 0)
+ except (OSError, KeyError) as error:
+ # A process which exits during the sample is not an unreadable
+ # live process. Keep permission failures visible in the record.
+ if directory.exists() and not isinstance(error, ProcessLookupError):
+ incomplete.append(pid)
+ rss += current_rss
+ from .hardware import cgroup_memory
+ system = self.kilobytes(Path('/proc/meminfo'))
+ available = system['MemAvailable']
+ self.last_cgroup = cgroup_memory()
+ # Each visible ancestor contains this process tree's protected file
+ # pages. The smallest complete bound avoids attributing unrelated
+ # host dirty pages to a dedicated container. Native Linux can use
+ # the wider system bound. Never subtract anonymous dirty memory from
+ # file dirty memory: aggregate PSS cannot establish their overlap.
+ protected_bounds = []
+ if all(type(system.get(key)) is int and system[key] >= 0 for key in ('Dirty', 'Writeback', 'Unevictable')):
+ protected_bounds.append(sum(system[key] for key in ('Dirty', 'Writeback', 'Unevictable')))
+ for group in self.last_cgroup.get('groups', []):
+ keys = ('dirty_bytes', 'writeback_bytes', 'unevictable_bytes')
+ if all(type(group.get(key)) is int and group[key] >= 0 for key in keys):
+ protected_bounds.append(sum(group[key] for key in keys))
+ protected_file = min(protected_bounds) if protected_bounds else None
+ inference_pss = inference_working_pss(
+ {'Pss': pss, 'Pss_File': file_pss, 'Pss_Dirty': dirty_pss}, protected_file) if file_counters_complete else pss
+ group_available = self.last_cgroup['available_bytes']
+ effective = min(available, group_available) if group_available is not None else available
+ reclaimable = self.last_cgroup.get('reclaimable_available_bytes', group_available)
+ inference_available = min(available, reclaimable) if reclaimable is not None else available
+ self.peak_rss = max(self.peak_rss, rss)
+ self.peak_tree_read = max(self.peak_tree_read, tree_read)
+ self.peak_tree_write = max(self.peak_tree_write, tree_write)
+ if not incomplete:
+ self.peak_pss = max(self.peak_pss, pss)
+ self.peak_inference_pss = max(self.peak_inference_pss, inference_pss)
+ self.pss_complete &= not incomplete
+ self.minimum_available = min(self.minimum_available or available, available)
+ self.minimum_effective_available = min(self.minimum_effective_available if self.minimum_effective_available is not None else effective, effective)
+ self.minimum_inference_available = min(self.minimum_inference_available if self.minimum_inference_available is not None else inference_available, inference_available)
+ self.samples += 1
+ return {'rss_bytes': rss, 'pss_bytes': pss if not incomplete else None,
+ # Unlike the guard's upper bound, this excludes every file page.
+ # Live planning can add it to available memory without counting
+ # clean mapped cache (already reclaimable) a second time.
+ 'nonfile_pss_bytes': pss - file_pss if file_counters_complete and not incomplete else None,
+ 'inference_guard_bytes': inference_pss if not incomplete else None,
+ 'inference_file_protection_bytes': protected_file,
+ 'guard_bytes': pss if not incomplete else None, 'guard_metric': 'PSS',
+ 'processes': count, 'unreadable_pss_pids': incomplete,
+ 'system_available_bytes': available, 'effective_available_bytes': effective,
+ 'inference_available_bytes': inference_available,
+ 'system_file_cache_bytes': system.get('Cached'), 'system_dirty_bytes': system.get('Dirty'),
+ 'system_writeback_bytes': system.get('Writeback'), 'cgroup_memory': self.last_cgroup}
+
+ def result(self):
+ if self.windows is not None:
+ return self.windows.result()
+ return {'process_tree_peak_rss_bytes': self.peak_rss,
+ 'inference_peak_working_pss_bytes': self.peak_inference_pss,
+ 'process_tree_peak_guard_bytes': self.peak_pss,
+ 'process_tree_guard_complete': self.pss_complete, 'ram_guard_metric': 'PSS',
+ 'process_tree_peak_pss_bytes': self.peak_pss,
+ 'process_tree_pss_complete': self.pss_complete,
+ 'system_min_available_bytes': self.minimum_available,
+ 'effective_min_available_bytes': self.minimum_effective_available,
+ 'inference_min_available_bytes': self.minimum_inference_available,
+ 'cgroup_memory_final': self.last_cgroup,
+ 'ram_observation_samples': self.samples,
+ 'process_tree_disk_read_bytes': self.peak_tree_read,
+ 'process_tree_disk_write_bytes': self.peak_tree_write,
+ 'process_tree_io_version': 2,
+ 'process_tree_io_scope': 'Maximum sampled live-tree sum, parents before children. Waited-child I/O rolls into the parent; exited PIDs are not added again. Sampling can miss I/O at exit; this is not cgroup disk accounting.',
+ 'ram_observation_scope': 'Sampled worker and descendants; RSS sums may double-count shared pages, PSS apportions resident shared pages; unmapped file cache excluded. System MemAvailable is not process attribution.'}
+
+
+class CgroupMemory:
+ """Validate an externally imposed hard cap and account for all descendants.
+
+ The launcher must create a fresh, dedicated cgroup/container. This class does
+ not create limits and never substitutes RSS polling for kernel enforcement.
+ """
+
+ def __init__(self, budget_bytes, root=Path('/sys/fs/cgroup'), *, swap_budget_bytes=0):
+ self.root = Path(root)
+ self.budget_bytes = budget_bytes
+ self.swap_budget_bytes = swap_budget_bytes
+ self.sampled_swap_peak = 0
+ self.swap_limit_violation = False
+ if budget_bytes <= 0:
+ raise ValueError('RAM budget must be positive')
+ if type(swap_budget_bytes) is not int or swap_budget_bytes < 0:
+ raise ValueError('Swap budget must be a nonnegative integer number of bytes')
+ self.initial = self.snapshot()
+ limit = self.initial['limit_bytes']
+ if limit is None or limit > budget_bytes:
+ raise RuntimeError('A dedicated cgroup memory.max at or below the requested RAM budget is required')
+ if self.swap_limit_violation or self.initial['swap_current_bytes'] != 0:
+ raise RuntimeError('RAM measurements require a bounded memory.swap.max at or below the explicit swap budget and no existing swap use (default: zero swap)')
+
+ def number(self, name):
+ value = (self.root / name).read_text(encoding='utf-8').strip()
+ return None if value == 'max' else int(value)
+
+ def fields(self, name):
+ return {key: int(value) for key, value in
+ (line.split() for line in (self.root / name).read_text(encoding='utf-8').splitlines())}
+
+ def optional(self, name, reader):
+ try:
+ return reader(name)
+ except FileNotFoundError:
+ return None
+
+ def io_fields(self, name):
+ return {parts[0]: {key: int(value) for key, value in
+ (entry.split('=', 1) for entry in parts[1:])}
+ for parts in (line.split() for line in (self.root / name).read_text(encoding='utf-8').splitlines())}
+
+ def snapshot(self):
+ state = {'limit_bytes': self.number('memory.max'),
+ 'current_bytes': self.number('memory.current'),
+ 'peak_bytes': self.number('memory.peak'),
+ 'swap_limit_bytes': self.number('memory.swap.max'),
+ 'swap_current_bytes': self.number('memory.swap.current'),
+ 'swap_peak_bytes': self.optional('memory.swap.peak', self.number),
+ 'swap_events': self.optional('memory.swap.events', self.fields),
+ 'io_stat': self.optional('io.stat', self.io_fields),
+ 'events': self.fields('memory.events'), 'stat': self.fields('memory.stat')}
+ self.sampled_swap_peak = max(self.sampled_swap_peak, state['swap_current_bytes'])
+ state['sampled_swap_peak_bytes'] = self.sampled_swap_peak
+ swap_limit = state['swap_limit_bytes']
+ self.swap_limit_violation |= swap_limit is None or swap_limit > self.swap_budget_bytes
+ return state
+
+ def events_since_start(self, state):
+ return {key: value - self.initial['events'].get(key, 0)
+ for key, value in state['events'].items()}
+
+ def result(self):
+ final = self.snapshot()
+ events = self.events_since_start(final)
+ physical_within = (final['limit_bytes'] is not None and final['limit_bytes'] <= self.budget_bytes
+ and final['peak_bytes'] <= self.budget_bytes)
+ swap_peak = max(self.sampled_swap_peak, final['swap_peak_bytes'] or 0)
+ swap_within = not self.swap_limit_violation and swap_peak <= self.swap_budget_bytes
+ within = (physical_within and swap_within and not events.get('oom', 0)
+ and not events.get('oom_kill', 0) and not events.get('oom_group_kill', 0))
+ swap_events = (None if final['swap_events'] is None or self.initial['swap_events'] is None else
+ {key: value - self.initial['swap_events'].get(key, 0)
+ for key, value in final['swap_events'].items()})
+ io_delta = (None if final['io_stat'] is None or self.initial['io_stat'] is None else
+ {device: {key: value - self.initial['io_stat'].get(device, {}).get(key, 0)
+ for key, value in fields.items()} for device, fields in final['io_stat'].items()})
+ return {'ram_budget_bytes': self.budget_bytes, 'ram_limit_enforced': True,
+ 'ram_within_budget': within, 'ram_cgroup_peak_bytes': final['peak_bytes'],
+ 'physical_ram_within_budget': physical_within,
+ 'swap_budget_bytes': self.swap_budget_bytes, 'swap_limit_enforced': True,
+ 'swap_within_budget': swap_within, 'swap_peak_bytes': swap_peak,
+ 'swap_peak_scope': ('Cgroup lifetime kernel peak' if final['swap_peak_bytes'] is not None
+ else 'Sampled lower bound; memory.swap.peak unavailable'),
+ 'swap_cgroup_events': swap_events, 'cgroup_io_delta': io_delta,
+ 'cgroup_io_scope': 'All charged block I/O, including model reads and swap; not swap-only traffic',
+ 'memory_mode': 'bounded_swap' if self.swap_budget_bytes else 'no_swap',
+ 'ram_cgroup_initial': self.initial, 'ram_cgroup_final': final,
+ 'ram_cgroup_events': events,
+ 'ram_scope': 'Dedicated cgroup and descendants, including anonymous memory, charged file cache, kernel and shared memory; cgroup lifetime peak'}
diff --git a/freevideo_engine/reference_sampler.py b/freevideo_engine/reference_sampler.py
new file mode 100644
index 0000000..187935f
--- /dev/null
+++ b/freevideo_engine/reference_sampler.py
@@ -0,0 +1,151 @@
+"""Experimental VDN reference sampling, adapted from pinned OpenVDN render.py.
+
+Uses the official Diffusers Ref2VA packed layout and conditioning schedules.
+Only generated rows are stepped. The transformer, attention, offloading and
+FP8 kernels are the same Engine modules used by ordinary VDN requests.
+The released VDN checkpoint is not a trained Ref2VA checkpoint.
+"""
+import time
+
+import torch
+
+from diffusers import MiniMaxH3Scheduler
+from diffusers.modular_pipelines.minimax_h3.before_denoise import (
+ MiniMaxH3Ref2VAPrepareLayoutStep, patchify_video_latents)
+from diffusers.modular_pipelines.minimax_h3.modular_pipeline import (
+ MINIMAX_H3_AUDIO_CHANNELS as AUDIO_CHANNELS, MINIMAX_H3_AUDIO_TAG as AUDIO_TAG,
+ MINIMAX_H3_VIDEO_TAG as VIDEO_TAG, align_num_frames,
+ audio_latent_num_frames, video_latent_num_frames)
+
+from src.models.sequence_layout import layout_from_indices
+from src.inference.render import KEYFRAME_NOISE_AUG
+from src.models.hybrid_transform import iter_hybrids, set_layout
+
+# Rebound per Engine request by sampler_for_canvas.
+LATENT_H, LATENT_W = 48, 84
+
+
+def generate_latents(transformer, prompt_embeds, text_token_tags, num_frames, num_steps, seed, device,
+ video_shift=12.0, audio_shift=3.0, runtime=None, step_seconds=None,
+ conditions=None):
+ """Preserve the pinned sampler's RNG order and generated-row updates.
+
+ Reference visual rows are held at max(video_t, .999), audio rows at 0.0,
+ as MiniMaxH3PrepareTimestepsStep executes (its prose once said 1.0).
+ """
+ num_frames = align_num_frames(num_frames, 17, 5)
+ num_latent_frames = video_latent_num_frames(num_frames, 17, 5)
+ num_audio_latents = audio_latent_num_frames(num_frames)
+ from types import SimpleNamespace
+ refs = conditions
+ if not refs:
+ raise ValueError('Ref2VA requires references')
+ condition_latents = [row['latent'].to(device) for row in refs if row['kind'] in ('image', 'video')]
+ audio_conditions = [row['audio_latent'].to(device) for row in refs if row.get('audio_latent') is not None]
+ references = [SimpleNamespace(kind=row['kind'], has_audio=row.get('audio_latent') is not None) for row in refs]
+ patch = tuple(transformer.config.patch_size) # (1, 2, 2)
+ channels = transformer.config.in_channels # 24
+ frame_h, frame_w = LATENT_H // patch[1], LATENT_W // patch[2]
+
+ position_ids, token_tags, video_indices, audio_indices, text_indices, num_condition_rows, num_audio_condition_rows = (
+ MiniMaxH3Ref2VAPrepareLayoutStep.build_ref2va_packed_sequence(
+ text_token_tags, references, condition_latents, audio_conditions, num_latent_frames, LATENT_H, LATENT_W, num_audio_latents,
+ patch, AUDIO_CHANNELS, AUDIO_TAG, VIDEO_TAG,
+ )
+ )
+ position_ids, token_tags = position_ids.to(device), token_tags.to(device)
+ video_indices, audio_indices, text_indices = (
+ video_indices.to(device), audio_indices.to(device), text_indices.to(device),
+ )
+
+ # A hybrid-converted transformer needs the packed layout before every forward; the
+ # layout is the same at every denoising step, so set it once per generation.
+ if next(iter_hybrids(transformer), None) is not None:
+ # frame_size and text_indices unconditionally: carrying them is free, and only
+ # their consumers are gated (short_conv / text_state).
+ set_layout(transformer, layout_from_indices(
+ video_indices[num_condition_rows:], num_latent_frames, frame_h * frame_w,
+ seq_len=position_ids.shape[0], frame_size=(frame_h, frame_w),
+ text_indices=text_indices,
+ ))
+
+ scheduler = MiniMaxH3Scheduler(shift=video_shift)
+ audio_scheduler = MiniMaxH3Scheduler(shift=audio_shift)
+ scheduler.set_timesteps(num_steps, device=device)
+ audio_scheduler.set_timesteps(num_steps, device=device)
+
+ generator = torch.Generator(device).manual_seed(seed)
+ # The conditioning noise is drawn first, one draw per keyframe, before the generated
+ # rows' noise (MiniMaxH3PrepareConditionLatentsStep's order).
+ condition_rows = []
+ for condition in condition_latents:
+ noise = torch.randn(condition.shape, generator=generator, device=device, dtype=torch.float32)
+ noised = scheduler.scale_noise(condition, KEYFRAME_NOISE_AUG, noise)
+ condition_rows.append(patchify_video_latents(noised, patch))
+ latents = torch.randn((1, channels, num_latent_frames, LATENT_H, LATENT_W),
+ generator=generator, device=device, dtype=torch.float32)
+ video_rows = patchify_video_latents(latents, patch)
+ if condition_rows:
+ video_rows = torch.cat(condition_rows + [video_rows])
+ if video_rows.shape[0] != video_indices.numel():
+ raise ValueError(f"{video_rows.shape[0]} video rows (with conditioning) != "
+ f"{video_indices.numel()} layout rows")
+ audio_rows = torch.randn((num_audio_latents * AUDIO_CHANNELS, 32),
+ generator=generator, device=device, dtype=torch.float32)
+
+ if audio_conditions:
+ audio_rows = torch.cat(audio_conditions + [audio_rows])
+ if audio_rows.shape[0] != audio_indices.numel():
+ raise ValueError('Reference audio rows disagree with packed layout')
+
+ if runtime is not None:
+ runtime.barrier()
+ torch.cuda.synchronize(device)
+
+ seq_len = position_ids.shape[0]
+ for t, audio_t in zip(scheduler.timesteps, audio_scheduler.timesteps):
+ step_started = time.perf_counter()
+ row_timesteps = torch.full((seq_len,), float(t), dtype=torch.float32, device=device)
+ if num_condition_rows:
+ row_timesteps[video_indices[:num_condition_rows]] = max(float(t), KEYFRAME_NOISE_AUG)
+ row_timesteps[audio_indices[num_audio_condition_rows:]] = float(audio_t)
+ row_timesteps[audio_indices[:num_audio_condition_rows]] = 0.0
+ timestep, timestep_indices = torch.unique(row_timesteps, sorted=True, return_inverse=True)
+ noise_pred, audio_noise_pred = transformer(
+ hidden_states=video_rows[None],
+ audio_hidden_states=audio_rows[None],
+ encoder_hidden_states=prompt_embeds[None],
+ timestep=timestep,
+ timestep_indices=timestep_indices,
+ token_tags=token_tags,
+ position_ids=position_ids,
+ video_indices=video_indices,
+ audio_indices=audio_indices,
+ text_indices=text_indices,
+ return_dict=False,
+ )
+ # only the generated rows step; the anchors ride through unchanged
+ video_rows[num_condition_rows:] = scheduler.step(
+ noise_pred[0, num_condition_rows:].float(), t, video_rows[num_condition_rows:],
+ return_dict=False)[0]
+ audio_rows[num_audio_condition_rows:] = audio_scheduler.step(
+ audio_noise_pred[0, num_audio_condition_rows:].float(), audio_t,
+ audio_rows[num_audio_condition_rows:], return_dict=False)[0]
+
+ if step_seconds is not None:
+ torch.cuda.synchronize(device)
+ step_seconds.append(time.perf_counter() - step_started)
+
+ if runtime is not None:
+ torch.cuda.synchronize(device)
+ runtime.barrier()
+
+ # Unpatchify (the AfterDenoise step's reshape) and unpack the channel-major audio rows.
+ video_rows = video_rows[num_condition_rows:]
+ rows = video_rows.reshape(-1, num_latent_frames, frame_h, frame_w, channels, *patch)
+ rows = rows.permute(0, 4, 1, 5, 2, 6, 3, 7)
+ latents = rows.reshape(-1, channels, num_latent_frames, LATENT_H, LATENT_W).contiguous()
+ audio_rows = audio_rows[num_audio_condition_rows:]
+ audio_latents = audio_rows.reshape(AUDIO_CHANNELS, num_audio_latents, 32).permute(0, 2, 1).contiguous()
+ return latents, audio_latents
diff --git a/freevideo_engine/refine.py b/freevideo_engine/refine.py
new file mode 100644
index 0000000..db76547
--- /dev/null
+++ b/freevideo_engine/refine.py
@@ -0,0 +1,127 @@
+"""VDN tail restart on its original schedule, with fixed audio.
+
+The input video is already normalized H3 latent space. The completed audio is
+re-noised at its own clock for each joint forward, then returned unchanged.
+Keyframes and image/video/audio references retain their pinned layouts and
+conditioning clocks; only generated video rows receive scheduler updates.
+This is an explicit quality-changing refinement, not an exact acceleration of
+single-pass generation. The automatic two-pass route uses this loop for its tail.
+"""
+import time
+
+import torch
+
+LATENT_H = 48
+LATENT_W = 84
+
+
+def validate_tail(base_steps, refine_steps):
+ if type(base_steps) is not int or type(refine_steps) is not int or not 0 < refine_steps < base_steps:
+ raise ValueError('Refinement must use a nonempty proper suffix of the original schedule')
+
+
+def generate_latents(transformer, prompt_embeds, text_token_tags, num_frames, num_steps,
+ seed, device, *, initial_latents, refine_steps, step_seconds=None,
+ conditions=None, video_shift=12., audio_shift=3.):
+ from diffusers import MiniMaxH3Scheduler
+ from diffusers.modular_pipelines.minimax_h3.before_denoise import (
+ MiniMaxH3PrepareLayoutStep, MiniMaxH3Ref2VAPrepareLayoutStep, patchify_video_latents)
+ from diffusers.modular_pipelines.minimax_h3.modular_pipeline import (
+ MINIMAX_H3_AUDIO_CHANNELS as AUDIO_CHANNELS, MINIMAX_H3_AUDIO_TAG as AUDIO_TAG,
+ MINIMAX_H3_VIDEO_TAG as VIDEO_TAG, align_num_frames, audio_latent_num_frames,
+ video_latent_num_frames)
+ from src.models.sequence_layout import layout_from_indices
+ from src.models.hybrid_transform import iter_hybrids, set_layout
+ from src.inference.render import KEYFRAME_NOISE_AUG
+
+ validate_tail(num_steps, refine_steps)
+ if not isinstance(initial_latents, (tuple, list)) or len(initial_latents) != 2:
+ raise ValueError('Initial latents must contain normalized video and completed audio')
+ video, audio = initial_latents
+ frames = align_num_frames(num_frames, 17, 5)
+ latent_frames = video_latent_num_frames(frames, 17, 5)
+ audio_frames = audio_latent_num_frames(frames)
+ channels, patch = transformer.config.in_channels, tuple(transformer.config.patch_size)
+ expected_video = (1, channels, latent_frames, LATENT_H, LATENT_W)
+ expected_audio = (AUDIO_CHANNELS, 32, audio_frames)
+ for tensor, shape, name in ((video, expected_video, 'video'), (audio, expected_audio, 'audio')):
+ if not isinstance(tensor, torch.Tensor) or tuple(tensor.shape) != shape:
+ raise ValueError(f'Initial {name} latent must have shape {shape}')
+ if not tensor.is_floating_point() or not bool(torch.isfinite(tensor).all()):
+ raise ValueError(f'Initial {name} latent must be finite floating point')
+
+ audio_conditions = []
+ if conditions and isinstance(conditions[0], dict):
+ from types import SimpleNamespace
+ refs = conditions
+ condition_latents = [row['latent'].to(device) for row in refs if row['kind'] in ('image', 'video')]
+ audio_conditions = [row['audio_latent'].to(device) for row in refs if row.get('audio_latent') is not None]
+ references = [SimpleNamespace(kind=row['kind'], has_audio=row.get('audio_latent') is not None) for row in refs]
+ layout = MiniMaxH3Ref2VAPrepareLayoutStep.build_ref2va_packed_sequence(
+ text_token_tags, references, condition_latents, audio_conditions,
+ latent_frames, LATENT_H, LATENT_W, audio_frames, patch, AUDIO_CHANNELS, AUDIO_TAG, VIDEO_TAG)
+ else:
+ anchors, condition_latents = conditions if conditions else ((), [])
+ condition_latents = [latent.to(device) for latent in condition_latents]
+ layout = MiniMaxH3PrepareLayoutStep.build_packed_sequence(
+ text_token_tags, latent_frames, LATENT_H, LATENT_W, audio_frames,
+ patch, AUDIO_CHANNELS, AUDIO_TAG, VIDEO_TAG, keyframe_anchors=anchors)
+ positions, tags, video_ids, audio_ids, text_ids, condition_rows, audio_condition_rows = layout
+ positions, tags, video_ids, audio_ids, text_ids = (
+ value.to(device) for value in (positions, tags, video_ids, audio_ids, text_ids))
+ frame_h, frame_w = LATENT_H // patch[1], LATENT_W // patch[2]
+ if next(iter_hybrids(transformer), None) is not None:
+ set_layout(transformer, layout_from_indices(video_ids[condition_rows:], latent_frames, frame_h * frame_w,
+ seq_len=positions.shape[0], frame_size=(frame_h, frame_w), text_indices=text_ids))
+
+ video_scheduler, audio_scheduler = MiniMaxH3Scheduler(shift=video_shift), MiniMaxH3Scheduler(shift=audio_shift)
+ video_scheduler.set_timesteps(num_steps, device=device)
+ audio_scheduler.set_timesteps(num_steps, device=device)
+ start = num_steps - refine_steps
+ generator = torch.Generator(device).manual_seed(seed)
+ # Match the pinned condition-first RNG order. Reference rows stay fixed
+ # throughout the tail, even though the generated rows use a restart seed.
+ fixed_video = []
+ for latent in condition_latents:
+ noise = torch.randn(latent.shape, generator=generator, device=device, dtype=torch.float32)
+ fixed_video.append(patchify_video_latents(
+ video_scheduler.scale_noise(latent, KEYFRAME_NOISE_AUG, noise), patch))
+ del condition_latents
+ source_video = video.to(device=device, dtype=torch.float32)
+ source_audio = audio.to(device=device, dtype=torch.float32)
+ video_noise = torch.randn(source_video.shape, generator=generator, device=device, dtype=torch.float32)
+ video_rows = patchify_video_latents(video_scheduler.scale_noise(
+ source_video, video_scheduler.timesteps[start], video_noise), patch)
+ if fixed_video:
+ video_rows = torch.cat(fixed_video + [video_rows])
+ if video_rows.shape[0] != video_ids.numel():
+ raise ValueError('Refinement video conditioning disagrees with the packed layout')
+ del fixed_video
+ audio_clean = source_audio.permute(0, 2, 1).reshape(-1, 32).contiguous()
+ audio_noise = torch.randn(audio_clean.shape, generator=generator, device=device, dtype=torch.float32)
+ del source_video, video_noise
+ for video_t, audio_t in zip(video_scheduler.timesteps[start:], audio_scheduler.timesteps[start:]):
+ tick = time.perf_counter()
+ audio_rows = audio_scheduler.scale_noise(audio_clean, audio_t, audio_noise)
+ if audio_conditions:
+ audio_rows = torch.cat(audio_conditions + [audio_rows])
+ if audio_rows.shape[0] != audio_ids.numel():
+ raise ValueError('Refinement audio conditioning disagrees with the packed layout')
+ row_times = torch.full((positions.shape[0],), float(video_t), dtype=torch.float32, device=device)
+ row_times[video_ids[:condition_rows]] = max(float(video_t), KEYFRAME_NOISE_AUG)
+ row_times[audio_ids[audio_condition_rows:]] = float(audio_t)
+ row_times[audio_ids[:audio_condition_rows]] = 0.
+ times, time_ids = torch.unique(row_times, sorted=True, return_inverse=True)
+ prediction, _ = transformer(hidden_states=video_rows[None], audio_hidden_states=audio_rows[None],
+ encoder_hidden_states=prompt_embeds[None], timestep=times, timestep_indices=time_ids,
+ token_tags=tags, position_ids=positions, video_indices=video_ids, audio_indices=audio_ids,
+ text_indices=text_ids, return_dict=False)
+ video_rows[condition_rows:] = video_scheduler.step(
+ prediction[0, condition_rows:].float(), video_t, video_rows[condition_rows:], return_dict=False)[0]
+ if step_seconds is not None:
+ if str(device).startswith('cuda'):
+ torch.cuda.synchronize(device)
+ step_seconds.append(time.perf_counter() - tick)
+ rows = video_rows[condition_rows:].reshape(-1, latent_frames, frame_h, frame_w, channels, *patch)
+ rows = rows.permute(0, 4, 1, 5, 2, 6, 3, 7)
+ return rows.reshape(expected_video).contiguous(), source_audio
diff --git a/freevideo_engine/resident_models.py b/freevideo_engine/resident_models.py
new file mode 100644
index 0000000..ac248f2
--- /dev/null
+++ b/freevideo_engine/resident_models.py
@@ -0,0 +1,346 @@
+"""Request-aware model residency inside the isolated interactive GPU worker."""
+import gc
+import json
+from pathlib import Path
+import time
+
+import torch
+from .torch_compat import empty_host_cache, host_cache_release_supported
+
+GiB = 1 << 30
+
+
+def key(value):
+ from .adaln_assets import digest
+ return digest(value)
+
+
+def file_identity(path):
+ from .storage import fingerprint
+ path = Path(path).resolve()
+ return dict(path=str(path), stamp=fingerprint(path))
+
+
+def gpu_bytes(model):
+ if hasattr(model, 'transformer'):
+ model = model.transformer
+ elif hasattr(model, 'cond_stage_model'):
+ model = model.cond_stage_model
+ if model is None:
+ return 0
+ storages = {}
+ for tensor in list(model.parameters()) + list(model.buffers()):
+ if tensor.device.type == 'cuda':
+ try:
+ storage = tensor.untyped_storage()
+ storages[storage.data_ptr()] = storage.nbytes()
+ except (NotImplementedError, RuntimeError):
+ # Native quantized tensor wrappers expose their packed tensors.
+ for value in vars(tensor).values():
+ if isinstance(value, torch.Tensor) and value.device.type == 'cuda':
+ storage = value.untyped_storage()
+ storages[storage.data_ptr()] = storage.nbytes()
+ return sum(storages.values())
+
+
+def pinned_cpu_storages(model):
+ """Owned, physically backed weights which eviction can actually release.
+
+ Mapped/pageable weights are excluded: Linux already credits clean file
+ cache, and Windows commit is not resident RAM. Count storage once even
+ when several tensor views (or model entries) share a pinned plane.
+ """
+ if hasattr(model, 'transformer'):
+ model = model.transformer
+ elif hasattr(model, 'cond_stage_model'):
+ model = model.cond_stage_model
+ if model is None:
+ return {}
+ storages = {}
+ for tensor in list(model.parameters()) + list(model.buffers()):
+ if tensor.device.type != 'cpu':
+ continue
+ try:
+ if tensor.is_pinned():
+ storage = tensor.untyped_storage()
+ storages[storage.data_ptr()] = storage.nbytes()
+ except (NotImplementedError, RuntimeError):
+ # Unknown wrapper ownership earns no speculative RAM allowance.
+ continue
+ return storages
+
+
+def release_encoder(clip, manager):
+ """Discard our Windows mapped encoder without a full GPU-to-CPU copy.
+
+ Native unpatching still releases pins, hooks and loaded-model registrations.
+ Once the bank drops its references, unused tensors can die where they are;
+ any other owner keeps valid tensors. Future encoding uses the mmap loader.
+ """
+ from .encoder_checkpoint import load_clip_model_patcher
+ from .system import windows
+ def owned(patcher):
+ factory = getattr(patcher, 'cached_patcher_init', None)
+ return isinstance(factory, (tuple, list)) and bool(factory) and factory[0] is load_clip_model_patcher
+ patcher = getattr(clip, 'patcher', None)
+ targets = [patcher] if windows() and owned(patcher) else []
+ if targets:
+ for loaded in manager.current_loaded_models:
+ candidate = loaded.model
+ if candidate is not None and candidate.model is patcher.model:
+ if not owned(candidate):
+ targets = [] # An unknown shared owner keeps native offload semantics.
+ break
+ if all(candidate is not target for target in targets):
+ targets.append(candidate)
+ saved = []
+ try:
+ for target in targets:
+ native = target.unpatch_model
+ saved.append((target, 'unpatch_model' in vars(target), vars(target).get('unpatch_model')))
+ def discard(device_to=None, unpatch_weights=True, *, _native=native):
+ # None is native unpatch_model's supported "do not move" mode.
+ return _native(device_to=None, unpatch_weights=unpatch_weights)
+ target.unpatch_model = discard
+ manager.unload_all_models()
+ finally:
+ for target, existed, original in reversed(saved):
+ if existed:
+ target.unpatch_model = original
+ else:
+ del target.unpatch_model
+ return bool(targets)
+
+
+class ModelBank:
+ def __init__(self):
+ self.entries = {}
+ self.peaks = {}
+ self.gpu_budget = None
+ self.gpu_reserve = 0
+ self.ram_budget = None
+ self.decisions = []
+
+ def configure(self, gpu_budget, ram_budget=None, *, gpu_reserve=0):
+ self.gpu_budget = int(gpu_budget)
+ self.gpu_reserve = int(gpu_reserve)
+ self.ram_budget = ram_budget
+ self.decisions = []
+
+ def take(self, role, identity):
+ entry = self.entries.get(role)
+ if entry and entry['identity'] != identity:
+ self.drop(role, 'Model, LoRA, geometry or execution settings changed')
+ entry = None
+ if entry:
+ entry['used'] = time.monotonic()
+ self.decisions.append(dict(action='reuse', role=role, gpu_bytes=gpu_bytes(entry['model'])))
+ return entry['model']
+
+ def put(self, role, identity, model):
+ self.entries[role] = dict(identity=identity, model=model, used=time.monotonic())
+
+ def observe_peak(self, role, identity, peak):
+ item = (role, identity)
+ self.peaks.pop(item, None)
+ self.peaks[item] = max(0, int(peak))
+ while len(self.peaks) > 128:
+ del self.peaks[next(iter(self.peaks))]
+
+ def drop(self, role, reason):
+ entry = self.entries.pop(role, None)
+ if entry is None:
+ return
+ model = entry['model']
+ before = torch.cuda.memory_allocated()
+ encoder_discarded = False
+ if role == 'encoder':
+ import comfy.model_management
+ encoder_discarded = release_encoder(model, comfy.model_management)
+ elif role == 'engine':
+ model.close()
+ else:
+ model.to('meta')
+ del model, entry
+ gc.collect()
+ torch.cuda.empty_cache()
+ host_cache_released = empty_host_cache(torch)
+ self.decisions.append(dict(action='evict', role=role, reason=reason,
+ encoder_discarded_without_cpu_copy=encoder_discarded,
+ host_cache_released=host_cache_released,
+ released_gpu_bytes=max(0, before - torch.cuda.memory_allocated())))
+
+ def clear(self, reason='Request failed or session configuration changed'):
+ for role in list(self.entries):
+ self.drop(role, reason)
+ gc.collect()
+ if torch.cuda.is_initialized():
+ torch.cuda.empty_cache()
+
+ def available(self):
+ free, _ = torch.cuda.mem_get_info()
+ allocated = torch.cuda.memory_allocated()
+ return max(0, min(free + torch.cuda.memory_reserved() - allocated - self.gpu_reserve,
+ self.gpu_budget - allocated))
+
+ def ram_fits(self, need=2*GiB):
+ from .ram import ProcessMemory
+ from .system import inference_memory_sample, inference_headroom
+ import os
+ sample = ProcessMemory().sample(os.getpid())
+ used = inference_memory_sample(sample)
+ return (used is not None and inference_headroom(sample) >= need + 2*GiB and
+ (self.ram_budget is None or used + need <= self.ram_budget))
+
+ def input_vae_room(self, model, canvas, *, audio=False):
+ # Input encoding has its own live workspace; it cannot assume an output
+ # decoder's memory footprint. Charge actual retained CPU model tensors
+ # before upload plus a canvas-scaled starting workspace estimate.
+ weights = sum(t.numel()*t.element_size() for t in list(model.parameters()) + list(model.buffers()))
+ area = canvas.get('width', 1344)*canvas.get('height', 768)/(1344*768)
+ workspace = 2*GiB if audio else max(2*GiB, 6*GiB*area)
+ self.make_room('input_audio_vae' if audio else 'input_video_vae', weights + workspace)
+
+ def make_room(self, role, peak, *, model=None, ram_need=2*GiB):
+ retained = gpu_bytes(model) if model is not None else 0
+ extra = max(0, int(peak) - retained)
+ before = self.available()
+ # Once conditioning is ready the current video cannot use the encoder
+ # again. Reclaim its speculative weights before a VAE needed later in
+ # this very request, regardless of how recently preloading touched it.
+ def eviction_order(item):
+ return (0 if item == 'encoder' and role != 'encoder' else 1, self.entries[item]['used'])
+ for other in sorted(self.entries, key=eviction_order):
+ if self.available() >= extra and self.ram_fits(ram_need):
+ break
+ if other != role:
+ self.drop(other, 'Next %s stage needs memory; cached weights are reclaimable' % role)
+ self.decisions.append(dict(action='admit', role=role, estimated_peak_bytes=int(peak),
+ reused_model_bytes=retained, additional_bytes=extra, available_before_bytes=before,
+ available_after_bytes=self.available(), budget_bytes=self.gpu_budget,
+ reason='Keep idle models when live resources cover the next stage; estimates do not certify capacity'))
+
+ def engine_key(self, request):
+ return key(dict(cache=file_identity(Path(request['cache'])/'manifest.json'),
+ options={k: v for k, v in request['engine_options'].items() if k != 'pin_host_gb'},
+ geometry=request.get('geometry'),
+ inputs=request.get('input_cache_dir')))
+
+ def engine(self, request, factory):
+ identity = self.engine_key(request)
+ found = self.take('engine', identity)
+ options, canvas = request['engine_options'], request.get('geometry', {})
+ # A larger host-cache allowance does not change weights or arithmetic.
+ # Keep the existing affordable cache rather than rereading every block
+ # just to pin one more layer. A lower cap must still release the old
+ # placement; never silently retain pins beyond a reduced RAM budget.
+ retained_pin_gb = getattr(found, 'config', {}).get('pin_host_gb', 0.)
+ requested_pin_gb = options.get('pin_host_gb', 0.)
+ if found is not None and retained_pin_gb > requested_pin_gb:
+ self.drop('engine', 'Host weight cache allowance decreased')
+ found = None
+ hit = found is not None
+ rows = canvas.get('video_tokens', 72576) + canvas.get('reference_video_tokens', 0) + canvas.get('reference_audio_tokens', 0)
+ from .policy import BLOCK_BYTES, activation_bytes, windows_gpu_output_workspace
+ from .system import windows
+ # Use the same measured token/head envelopes as placement. The old
+ # 10 GiB * tokens / 107856 estimate admitted a 13.19 GiB idle encoder
+ # beside a 5090 request whose sampling actually reserved 18.71 GiB.
+ # This only evicts idle models; it never lowers the requested compute.
+ head = max(4, options.get('head_chunk', 8) or 16)
+ workspace = max(5*GiB, 10*GiB*rows/107856, activation_bytes(head, rows))
+ source = 'policy_token_head_workspace'
+ if windows() and not options.get('attention_cpu_outputs', False):
+ # The Windows envelope includes allocator slack and the FF stash.
+ # Smaller compatibility chunks do not remove that stash; wider
+ # groups still pay their measured incremental activation cost.
+ workspace = max(workspace, windows_gpu_output_workspace(rows,
+ prefetch=options.get('prefetch', False)) +
+ max(0, activation_bytes(head, rows) - activation_bytes(8, rows)))
+ source = 'windows_gpu_output_workspace'
+ estimate = int(options.get('resident_blocks', 0)*BLOCK_BYTES + workspace)
+ observed = self.peaks.get(('engine', identity))
+ peak = estimate if observed is None else observed
+ self.make_room('engine', peak, model=found)
+ self.decisions[-1]['evidence'] = dict(source=source if observed is None else 'complete_local_stage_peak',
+ cold_estimate_bytes=estimate, observed_peak_bytes=observed, video_and_reference_tokens=rows,
+ head_chunk=options.get('head_chunk', 8), window_batch=options.get('window_batch', 1),
+ requested_pin_host_gb=requested_pin_gb,
+ retained_pin_host_gb=retained_pin_gb if hit else None)
+ if found is None:
+ found = factory()
+ self.put('engine', identity, found)
+ return found, hit
+
+ def encoder_room(self, clip, tokens, fallback_peak):
+ """Admit a reused native encoder with this input's workspace estimate."""
+ estimator = getattr(clip.cond_stage_model, 'memory_estimation_function', None)
+ peak, source = fallback_peak, 'encoder_checkpoint_estimate'
+ workspace = None
+ if callable(estimator):
+ workspace = estimator(tokens, device=clip.patcher.load_device)
+ source = 'native_encoder_weights_and_token_workspace'
+ else:
+ # The pinned H3 encoder has no native estimator. Native Windows
+ # short-text runs reserve < 1.5 GiB beyond its packed model size;
+ # keep 2 GiB for these inputs. Vision entries and longer/unknown
+ # layouts retain the original conservative checkpoint estimate.
+ sequences = tokens.get('qwen3vl_32b') if isinstance(tokens, dict) else None
+ if (isinstance(tokens, dict) and set(tokens) == {'qwen3vl_32b'}
+ and isinstance(sequences, (list, tuple)) and len(sequences) == 1
+ and isinstance(sequences[0], (list, tuple)) and 0 < len(sequences[0]) <= 512
+ and all(isinstance(token, (list, tuple)) and token and type(token[0]) is int
+ for token in sequences[0])):
+ workspace, source = 2*GiB, 'short_text_encoder_workspace'
+ import math
+ weights = clip.patcher.model_size()
+ if (type(workspace) in (int, float) and math.isfinite(workspace) and workspace >= 0
+ and type(weights) in (int, float) and math.isfinite(weights) and weights > 0):
+ # Retained packed tensors and native model_size differ for
+ # quantized wrappers. Charge the larger plus forward workspace.
+ peak = max(weights, gpu_bytes(clip)) + max(2*GiB, workspace)
+ else:
+ source = 'encoder_checkpoint_estimate'
+ self.make_room('encoder', min(self.gpu_budget, peak), model=clip, ram_need=2*GiB)
+ self.decisions[-1]['evidence'] = dict(source=source, cold_estimate_bytes=int(fallback_peak),
+ requested_peak_bytes=int(peak))
+
+ def decoder_room(self, options, canvas):
+ if options.get('offload'):
+ # The streamed decoder installs offload hooks on a different model;
+ # its workspace cannot credit a cached full-GPU decoder it won't use.
+ self.drop('video_vae', 'The requested streamed decoder cannot reuse the full-GPU VAE')
+ need = (options.get('resident_blocks', 0)*268574720 + 4*GiB)
+ else:
+ need = 15*GiB + max(0, (canvas.get('video_tokens', 72576)-72576)*30000)
+ need = self.peaks.get(('decode', key(dict(options=options, canvas=canvas))), need)
+ self.make_room('video_vae', min(self.gpu_budget, need),
+ model=self.entries.get('video_vae', {}).get('model'), ram_need=2*GiB)
+
+ def snapshot(self):
+ if not torch.cuda.is_initialized():
+ return dict(reclaimable_gpu_bytes=0, reclaimable_ram_bytes=0, gpu_uuid=None, models=[])
+ uuid = str(getattr(torch.cuda.get_device_properties(0), 'uuid', ''))
+ if uuid and not uuid.startswith(('GPU-', 'MIG-')):
+ uuid = 'GPU-' + uuid
+ host = {}
+ for entry in self.entries.values():
+ host.update(pinned_cpu_storages(entry['model']))
+ memory, gpu_capacity = None, None
+ from .system import windows
+ if windows():
+ from .ram import ProcessMemory
+ import os
+ # Use the same Windows physical/mapped/commit accounting as the
+ # active-request guard, not raw GlobalMemoryStatusEx availability.
+ memory = ProcessMemory().sample(os.getpid())
+ from .gpu_budget import owned_pool_capacity
+ gpu_capacity = owned_pool_capacity(torch)
+ can_release_host = host_cache_release_supported(torch)
+ return dict(reclaimable_gpu_bytes=torch.cuda.memory_reserved(),
+ reclaimable_ram_bytes=sum(host.values()) if can_release_host else 0,
+ retained_pinned_ram_bytes=sum(host.values()), host_cache_release_supported=can_release_host,
+ memory=memory, gpu_capacity=gpu_capacity,
+ ram_credit_scope='Owned pinned model storage only; deduplicated, reclaimable on eviction. No mapped-page, commit or swap credit.',
+ gpu_uuid=uuid, models=[dict(role=role, gpu_bytes=gpu_bytes(entry['model']))
+ for role, entry in self.entries.items()], decisions=self.decisions)
diff --git a/freevideo_engine/resident_process.py b/freevideo_engine/resident_process.py
new file mode 100644
index 0000000..af280df
--- /dev/null
+++ b/freevideo_engine/resident_process.py
@@ -0,0 +1,511 @@
+"""Private local IPC for a ComfyUI-owned, reusable engine worker.
+
+The worker receives the active controller's real GPU lease by descriptor/handle
+transfer. It closes that lease before going idle. The controller still measures
+and can stop the physical worker, so caching does not bypass RAM guards, attempt
+history, cancellation, or the Windows process tree supervisor. No public port.
+"""
+import atexit
+from dataclasses import replace
+import json
+import os
+from pathlib import Path
+import secrets
+import signal
+import subprocess
+import sys
+import threading
+import time
+
+from . import processes
+from .monitoring import save
+
+ENV = 'FREEVIDEO_RESIDENT_SESSION'
+
+
+class SessionRestarted(RuntimeError):
+ """No new GPU work started; the old worker was confirmed stopped."""
+
+
+class SessionBusy(RuntimeError):
+ """A running request owns this worker; its allocations are not idle credit."""
+
+
+def send(connection, value):
+ connection.send_bytes(json.dumps(value, allow_nan=False).encode('utf-8'))
+
+
+def receive(connection, timeout=None):
+ if timeout is not None and not connection.poll(timeout):
+ raise TimeoutError('Resident engine did not respond')
+ return json.loads(connection.recv_bytes(2**20))
+
+
+def _send_lease(connection, descriptor, destination_pid):
+ if os.name != 'nt':
+ from multiprocessing.reduction import send_handle
+ send_handle(connection, descriptor, destination_pid)
+ return
+ import _winapi
+ import msvcrt
+ # CPython 3.9/3.12 send_handle passes DUPLICATE_SAME_ACCESS as the desired
+ # access mask, not DuplicateHandle's options. For files that becomes only
+ # FILE_WRITE_DATA, losing the permissions needed to validate/use the lease.
+ # Duplicate explicitly with unchanged access and transfer only its integer
+ # value over our authenticated JSON channel. The receiver owns the copy;
+ # RemoteProcess stops it on any failed/ambiguous handoff, closing that copy.
+ target = _winapi.OpenProcess(_winapi.PROCESS_DUP_HANDLE, False, destination_pid)
+ try:
+ handle = _winapi.DuplicateHandle(_winapi.GetCurrentProcess(),
+ msvcrt.get_osfhandle(descriptor), target, 0, False, _winapi.DUPLICATE_SAME_ACCESS)
+ send(connection, dict(command='lease', handle=handle))
+ finally:
+ _winapi.CloseHandle(target)
+
+
+def _receive_lease(connection):
+ if os.name != 'nt':
+ from multiprocessing.reduction import recv_handle
+ return recv_handle(connection)
+ import _winapi
+ import msvcrt
+ message = receive(connection, 10.)
+ handle = message.get('handle')
+ if message.get('command') != 'lease' or type(handle) is not int or handle <= 0:
+ raise ValueError('Invalid resident runtime lease transfer')
+ try:
+ return msvcrt.open_osfhandle(handle, os.O_RDWR | os.O_BINARY)
+ except BaseException:
+ _winapi.CloseHandle(handle)
+ raise
+
+
+def alive(pid, created):
+ import psutil
+ try:
+ process = psutil.Process(pid)
+ return process.create_time() == created and process.is_running() and process.status() != psutil.STATUS_ZOMBIE
+ except (psutil.Error, ValueError):
+ return False
+
+
+def connect(path):
+ from multiprocessing.connection import Client
+ record = json.loads(Path(path).read_text(encoding='utf-8'))
+ ready = json.loads(Path(path).with_name('ready.json').read_text(encoding='utf-8'))
+ if not alive(ready['pid'], ready['created']):
+ raise SessionRestarted('The previous resident worker has stopped; resume in a fresh worker')
+ if ready.get('idle') is False:
+ raise SessionBusy('Resident engine is executing another request')
+ connection = Client(record['address'], family=record['family'], authkey=bytes.fromhex(record['key']))
+ hello = receive(connection, 5.)
+ if (hello['pid'] != ready['pid'] or hello['created'] != ready['created'] or
+ not alive(hello['pid'], hello['created'])):
+ connection.close()
+ raise RuntimeError('Resident engine process changed')
+ return connection, hello
+
+
+def status(environment=None):
+ path = (os.environ if environment is None else environment).get(ENV)
+ if not path:
+ return None
+ try:
+ connection, _ = connect(path)
+ try:
+ send(connection, {'command': 'status'})
+ return receive(connection, 5.)
+ finally:
+ connection.close()
+ except (OSError, ValueError, EOFError, TimeoutError, SessionRestarted, SessionBusy):
+ return None
+
+
+def credit(hardware, environment=None, *, reuse_encoder=False):
+ """Plan whole-worker budgets using only authenticated idle allocations."""
+ state = status(environment)
+ if not state or not hardware.gpu_uuid or state.get('gpu_uuid') != hardware.gpu_uuid or not state.get('idle'):
+ return hardware, None
+ gpu = state.get('reclaimable_gpu_bytes')
+ ram = state.get('reclaimable_ram_bytes', 0)
+ if type(gpu) is not int or gpu < 0 or type(ram) is not int or ram < 0:
+ return hardware, None
+ # Nominal or explicit user capacities still cap choose(). Active allocations
+ # from ComfyUI or unrelated applications are never included in this credit.
+ available = min(hardware.ram_total, hardware.ram_available + ram)
+ owned_encoding_ram = 0
+ memory = state.get('memory') or {}
+ if hardware.system == 'Windows':
+ # Windows global availability excludes our resident mapped weights.
+ # Reuse the worker guard's effective availability, capped by physical
+ # RAM and commit headroom. Never add mapped credit to min(RAM, commit)
+ # directly: that would manufacture commit capacity.
+ keys = ('effective_available_bytes', 'system_physical_available_bytes',
+ 'system_commit_available_bytes', 'reclaimable_mapped_bytes', 'guard_bytes')
+ if all(type(memory.get(k)) is int and memory[k] >= 0 for k in keys):
+ available = min(hardware.ram_total, memory['system_commit_available_bytes'],
+ memory['effective_available_bytes'] + min(ram, memory['guard_bytes']),
+ memory['system_physical_available_bytes'] + memory['reclaimable_mapped_bytes'] + min(ram, memory['guard_bytes']))
+ if (reuse_encoder and memory.get('guard_metric') == 'tree private working set'
+ and any(model.get('role') == 'encoder' for model in state.get('models', []))):
+ # The encoder is staying in this same worker. Its measured
+ # private working set is already charged to both RAM and
+ # Commit; a whole-worker budget must not charge it again as
+ # newly allocated memory. Replace the pin credit, do not add
+ # both. No RSS, mapped section or nonresident commit is added.
+ owned_encoding_ram = memory['guard_bytes']
+ available = min(hardware.ram_total,
+ memory['effective_available_bytes'] + owned_encoding_ram,
+ memory['system_commit_available_bytes'] + owned_encoding_ram,
+ memory['system_physical_available_bytes'] + memory['reclaimable_mapped_bytes'] + owned_encoding_ram)
+ else:
+ available = hardware.ram_available # Unknown commit/mapped accounting earns no RAM credit.
+ state = dict(state, ram_accounting=dict(raw_available_bytes=hardware.ram_available,
+ credited_available_bytes=available, pinned_model_bytes=ram,
+ reused_encoder_working_bytes=owned_encoding_ram,
+ scope=('Windows whole-worker encoding allowance: effective physical/commit headroom plus '
+ 'already resident private working set, counted once; no new system capacity'
+ if owned_encoding_ram else
+ 'Windows worker effective physical availability bounded by commit; Linux available RAM plus owned pins')))
+ gpu_available = min(hardware.vram_total, hardware.vram_free + gpu)
+ capacity = state.get('gpu_capacity')
+ if hardware.system == 'Windows' and isinstance(capacity, dict):
+ limit = capacity.get('allocator_limit_bytes')
+ if type(limit) is int and 0 <= limit <= hardware.vram_total:
+ gpu_available = min(gpu_available, limit)
+ return replace(hardware, vram_free=gpu_available,
+ ram_available=available), state
+
+
+class RemoteProcess:
+ """Popen-shaped active request; pid is the measured GPU worker, not an RPC shim."""
+ def __init__(self, endpoint, command, environment, descriptor, log, *, required_capabilities=(), operation='run'):
+ import psutil
+ if operation not in ('run', 'release'):
+ raise ValueError('Invalid resident operation')
+ self.connection, hello = connect(endpoint)
+ self.pid, self.created = hello['pid'], hello['created']
+ self.returncode = None
+ self.settled = False
+ self.args = command
+ if not set(required_capabilities).issubset(hello.get('capabilities', [])):
+ # A previous worker must not interpret a weights-only request as
+ # ordinary prompt encoding. Complete a read-only exchange and keep
+ # it idle, without transferring a lease or beginning CUDA work.
+ try:
+ send(self.connection, {'command': 'status'})
+ receive(self.connection, 5.)
+ finally:
+ self.connection.close()
+ raise RuntimeError('Resident worker does not support the requested idle preparation; restart the app')
+ selected = environment.get('PYTORCH_ALLOC_CONF', environment.get('PYTORCH_CUDA_ALLOC_CONF', ''))
+ if hello.get('allocator_config') is not None and hello['allocator_config'] != selected:
+ self.kill()
+ deadline = time.monotonic()+5.
+ while alive(self.pid, self.created) and time.monotonic() < deadline:
+ time.sleep(.025)
+ self.connection.close()
+ if alive(self.pid, self.created):
+ raise RuntimeError('Could not stop the old resident allocator; refusing concurrent GPU work')
+ raise SessionRestarted('Allocator settings changed; cleared idle cache and started a fresh worker')
+ try:
+ send(self.connection, dict(command=operation, argv=list(command), environment=dict(environment),
+ log=str(Path(log).resolve()), owner_pid=os.getpid(), owner_created=psutil.Process().create_time()))
+ _send_lease(self.connection, descriptor, self.pid)
+ try:
+ accepted = receive(self.connection, 10.)
+ except (EOFError, OSError) as error:
+ raise RuntimeError('Resident worker disconnected during runtime lease handoff; see ' +
+ str(Path(endpoint).with_name('session.log'))) from error
+ if accepted.get('status') != 'accepted':
+ raise RuntimeError('Resident worker rejected request during ' +
+ str(accepted.get('phase', 'handoff')) + ': ' + str(accepted.get('error')))
+ except BaseException as error:
+ # A failed handoff must not leave a worker waiting for a descriptor,
+ # or executing work which the controller cannot monitor/cancel.
+ try:
+ self.stop_request(grace=.25)
+ except BaseException as cleanup:
+ error.worker_stop_confirmed = False
+ error.worker_stop_error = repr(cleanup)
+ self.connection.close()
+ raise
+
+ def poll(self):
+ if self.returncode is not None:
+ return self.returncode
+ try:
+ if self.connection.poll(0):
+ result = receive(self.connection)
+ self.returncode = int(result['returncode'])
+ self.settled = True
+ self.connection.close()
+ elif not alive(self.pid, self.created):
+ self.returncode = -1
+ self.connection.close()
+ except (EOFError, OSError):
+ self.returncode = -1
+ self.connection.close()
+ return self.returncode
+
+ def wait(self, timeout=None):
+ started = time.monotonic()
+ while self.poll() is None:
+ if timeout is not None and time.monotonic() - started >= timeout:
+ raise subprocess.TimeoutExpired(self.args, timeout)
+ time.sleep(.025)
+ return self.returncode
+
+ def send_signal(self, value):
+ if alive(self.pid, self.created):
+ os.kill(self.pid, value)
+
+ def kill(self):
+ import psutil
+ if alive(self.pid, self.created):
+ process = psutil.Process(self.pid)
+ for child in reversed(process.children(recursive=True)):
+ try:
+ child.kill()
+ except psutil.Error:
+ pass
+ process.kill()
+
+ def terminate(self):
+ self.send_signal(signal.SIGTERM)
+
+ def stop_request(self, grace=10):
+ self.poll()
+ if self.settled:
+ return
+ self.terminate()
+ deadline = time.monotonic()+grace
+ while alive(self.pid, self.created) and time.monotonic() < deadline:
+ time.sleep(.025)
+ if alive(self.pid, self.created):
+ self.kill()
+ deadline = time.monotonic()+5.
+ while alive(self.pid, self.created) and time.monotonic() < deadline:
+ time.sleep(.025)
+ if alive(self.pid, self.created):
+ raise subprocess.TimeoutExpired(self.args, grace+5)
+ self.poll()
+ self.connection.close()
+
+
+def release_idle_cache(environment, descriptor):
+ """Release only our authenticated idle process, under the runtime lease.
+
+ Used once when cached models prevent admitting the next stage. Exiting the
+ process releases encoder mappings, CUDA allocations and WDDM commit together.
+ Conditioning already saved on disk is unaffected; no generation is repeated.
+ """
+ endpoint = environment[ENV]
+ log = Path(endpoint).with_name('release-%d.log' % time.time_ns())
+ worker = RemoteProcess(endpoint, ['release-idle-cache'], environment, descriptor, log,
+ required_capabilities=('idle-cache-release',), operation='release')
+ try:
+ code = worker.wait(timeout=5.)
+ if code:
+ raise RuntimeError('Idle cache release failed with exit %s' % code)
+ deadline = time.monotonic()+5.
+ while alive(worker.pid, worker.created) and time.monotonic() < deadline:
+ time.sleep(.025)
+ if alive(worker.pid, worker.created):
+ worker.kill()
+ deadline = time.monotonic()+3.
+ while alive(worker.pid, worker.created) and time.monotonic() < deadline:
+ time.sleep(.025)
+ if alive(worker.pid, worker.created):
+ raise RuntimeError('Idle worker has not exited; memory release is not confirmed')
+ return dict(released=True, pid=worker.pid, reason='Release idle models before replanning; saved conditioning retained')
+ except BaseException:
+ # The release was accepted under our lease. Do not leave a half-stopped
+ # process or assume its memory is available after an IPC timeout.
+ if alive(worker.pid, worker.created):
+ worker.stop_request(grace=1.) if not worker.settled else worker.kill()
+ raise
+ finally:
+ worker.connection.close()
+
+
+class SessionOwner:
+ """One persistent worker owned by the interactive ComfyUI process."""
+ def __init__(self):
+ self.process = None
+ self.endpoint = None
+ self.identity = None
+ self.active = False
+ self.prewarm = None
+ atexit.register(self.close)
+
+ def start(self, root, source, python, environment):
+ import hashlib
+ import psutil
+ self.stop_prewarm()
+ source = Path(source).resolve()
+ signature = hashlib.sha256()
+ for file in sorted((source/'freevideo_engine').glob('*.py')):
+ signature.update(file.name.encode())
+ signature.update(file.read_bytes())
+ identity = (str(source), str(python), signature.hexdigest(), environment.get('CUDA_VISIBLE_DEVICES'))
+ if self.process is not None and self.process.poll() is None and self.identity == identity:
+ return str(self.endpoint)
+ self.close()
+ directory = Path(root)/'sessions'/('interactive-' + secrets.token_hex(8))
+ directory.mkdir(parents=True, mode=0o700)
+ self.endpoint = directory/'endpoint.json'
+ family = 'AF_PIPE' if os.name == 'nt' else 'AF_UNIX'
+ address = (r'\\.\pipe\FreeVideo-' + secrets.token_hex(16) if os.name == 'nt'
+ else str(directory/'worker.sock'))
+ # Unix-domain paths have a short OS limit; use a private short directory.
+ if os.name != 'nt' and len(address.encode()) >= 100:
+ import tempfile
+ socket_directory = Path(tempfile.mkdtemp(prefix='freevideo-session-'))
+ address = str(socket_directory/'worker.sock')
+ save(self.endpoint, dict(address=address, family=family, key=secrets.token_hex(32),
+ owner_pid=os.getpid(), owner_created=psutil.Process().create_time(), source_sha256=identity[2]))
+ if os.name != 'nt':
+ self.endpoint.chmod(0o600)
+ env = dict(environment, PYTHONPATH=str(source), PYTHONUNBUFFERED='1')
+ env.pop(ENV, None)
+ env.pop('FREEVIDEO_RUNTIME_LOCK_FD', None)
+ env.pop(processes.HANDLE_ENV, None)
+ with (directory/'session.log').open('wb') as log:
+ self.process = processes.popen([str(python), '-B', '-m', 'freevideo_engine.resident_worker',
+ '--endpoint', str(self.endpoint)], env=env, cwd=source, stdin=subprocess.DEVNULL,
+ stdout=log, stderr=subprocess.STDOUT, start_new_session=True, supervise=True)
+ self.identity = identity
+ deadline = time.monotonic() + 15
+ while time.monotonic() < deadline:
+ if self.process.poll() is not None:
+ raise RuntimeError('Resident worker could not start; see ' + str(directory/'session.log'))
+ if (directory/'ready.json').exists():
+ return str(self.endpoint)
+ time.sleep(.05)
+ self.close()
+ raise TimeoutError('Resident worker startup timed out')
+
+ def stop_prewarm(self):
+ receipt = {}
+ if self.prewarm is not None:
+ stopped = self.prewarm.stop()
+ from .encoder_diagnostics import prewarm_receipt
+ receipt = prewarm_receipt(self.prewarm.report, gpu=True)
+ receipt['worker_released'] = False
+ self.prewarm = None
+ # An interrupted native loader can be stuck in IO. Never grant
+ # memory credit or start another request until it yielded/exited.
+ if self.process is not None and self.process.poll() is None:
+ try:
+ ready = json.loads(self.endpoint.with_name('ready.json').read_text(encoding='utf-8'))
+ except (OSError, ValueError):
+ ready = {}
+ if not stopped or ready.get('idle') is not True:
+ processes.stop(self.process, grace=1.)
+ self.process = None
+ receipt['worker_released'] = True
+ return receipt
+
+ def warm_encoder(self, output, python, environment, *, busy=None, notify=None):
+ if (self.process is None or self.process.poll() is not None or self.active or
+ environment.get('FREEVIDEO_ENCODER_PREWARM', 'auto').lower() in ('0', 'off', 'false')):
+ return False
+ self.stop_prewarm()
+ if self.process is None:
+ return False
+ from .idle_encoder import IdleEncoder
+ self.prewarm = IdleEncoder()
+ return self.prewarm.schedule(self.endpoint, output, python, environment, busy=busy, notify=notify)
+
+ def close(self):
+ self.stop_prewarm()
+ if self.process is not None:
+ processes.stop(self.process, grace=2)
+ self.process = None
+
+
+OWNER = SessionOwner()
+
+
+def serve(endpoint, runner, snapshot):
+ """Shared transport with a CPU-injectable runner for lifecycle tests."""
+ from multiprocessing.connection import Listener
+ import psutil
+ record = json.loads(Path(endpoint).read_text(encoding='utf-8'))
+ hello = dict(pid=os.getpid(), created=psutil.Process().create_time())
+ done = threading.Event()
+ def owner_watch():
+ while not done.wait(.5):
+ if not alive(record['owner_pid'], record['owner_created']):
+ os._exit(72) # Owner crash must not leave a hidden CUDA worker.
+ threading.Thread(target=owner_watch, daemon=True).start()
+ try:
+ with Listener(record['address'], family=record['family'], authkey=bytes.fromhex(record['key'])) as listener:
+ ready_path = Path(endpoint).with_name('ready.json')
+ save(ready_path, dict(hello, idle=True))
+ while True:
+ with listener.accept() as connection:
+ state = snapshot()
+ send(connection, dict(hello, allocator_config=state.get('allocator_config'),
+ capabilities=list(state.get('capabilities', []))+['idle-cache-release']))
+ request = receive(connection, 10.)
+ if request.get('command') == 'status':
+ send(connection, dict(snapshot(), idle=True))
+ continue
+ if request.get('command') not in ('run', 'release'):
+ raise ValueError('Unknown resident worker command')
+ save(ready_path, dict(hello, idle=False))
+ descriptor = None
+ accepted = False
+ phase = 'runtime lease transfer'
+ stopped = threading.Event()
+ def request_watch():
+ while not stopped.wait(.25):
+ if not alive(request['owner_pid'], request['owner_created']):
+ os._exit(73) # Preserve the unsettled attempt/lease evidence.
+ watcher = threading.Thread(target=request_watch, daemon=True)
+ previous = dict(os.environ)
+ try:
+ descriptor = _receive_lease(connection)
+ phase = 'runtime lease validation'
+ os.environ.clear()
+ os.environ.update(request['environment'])
+ os.environ.pop(processes.HANDLE_ENV, None)
+ os.environ['FREEVIDEO_RUNTIME_LOCK_FD'] = str(descriptor)
+ from .locking import runtime_lock
+ with runtime_lock():
+ send(connection, {'status': 'accepted'})
+ accepted = True
+ watcher.start()
+ code = runner(request) if request['command'] == 'run' else 0
+ except BaseException as error:
+ if not accepted:
+ failure = dict(status='rejected', phase=phase,
+ error=type(error).__name__ + ': ' + str(error))
+ # Send the cause before closing the pipe. No argv,
+ # environment, IPC key or model data is logged.
+ try:
+ with Path(request['log']).open('a', encoding='utf-8') as log:
+ log.write(json.dumps(failure, ensure_ascii=False) + '\n')
+ except OSError:
+ pass
+ try:
+ send(connection, failure)
+ except (EOFError, OSError):
+ pass
+ raise
+ finally:
+ stopped.set()
+ if watcher.is_alive():
+ watcher.join(timeout=1)
+ if descriptor is not None:
+ os.close(descriptor)
+ os.environ.clear()
+ os.environ.update(previous)
+ save(ready_path, dict(hello, idle=request['command'] != 'release'))
+ send(connection, {'returncode': code})
+ if request['command'] == 'release':
+ return
+ finally:
+ done.set()
diff --git a/freevideo_engine/resident_worker.py b/freevideo_engine/resident_worker.py
new file mode 100644
index 0000000..8b47cdc
--- /dev/null
+++ b/freevideo_engine/resident_worker.py
@@ -0,0 +1,147 @@
+"""Isolated interactive worker; keeps compatible idle models until memory is needed."""
+import argparse
+from contextlib import redirect_stderr, redirect_stdout
+import gc
+import json
+from pathlib import Path
+import sys
+import time
+import traceback
+import types
+
+from .resident_process import serve
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--endpoint', type=Path, required=True)
+ args = parser.parse_args()
+ bank = None
+ allocator = None
+
+ def snapshot():
+ result = bank.snapshot() if bank is not None else dict(reclaimable_gpu_bytes=0, reclaimable_ram_bytes=0, gpu_uuid=None, models=[])
+ return dict(result, allocator_config=allocator, capabilities=['encoder-weight-preload'])
+
+ def run(message):
+ nonlocal bank, allocator
+ started = time.perf_counter()
+ command = message['argv']
+ module = command[command.index('-m')+1]
+ if module not in ('freevideo_engine.worker', 'freevideo_engine.encode_worker'):
+ raise ValueError('Only the engine and native encoder use the resident worker')
+ request = json.loads(Path(command[command.index('--request')+1]).read_text(encoding='utf-8'))
+ with Path(message['log']).open('w', encoding='utf-8', buffering=1) as log, redirect_stdout(log), redirect_stderr(log):
+ metrics = None
+ diagnostics = None
+ try:
+ if module.endswith('.encode_worker'):
+ from .encoder_diagnostics import EncoderTrace
+ diagnostics = EncoderTrace(request, resident=True)
+ diagnostics.stage('worker_import')
+ import os
+ selected = os.environ.get('PYTORCH_ALLOC_CONF', os.environ.get('PYTORCH_CUDA_ALLOC_CONF', ''))
+ if allocator is not None and allocator != selected:
+ # Allocation strategy is process initialization state. Do
+ # not pretend environment changes reconfigure a live pool.
+ raise RuntimeError('Resident allocator configuration changed; restart the interactive session')
+ allocator = selected
+ if bank is None:
+ from .resident_models import ModelBank
+ bank = ModelBank()
+ import torch
+ torch.set_grad_enabled(False)
+ if diagnostics is not None:
+ diagnostics.stage('worker_cuda_setup')
+ # An explicit benchmark cap belongs to its request. A cached
+ # process must not silently carry that cap into a later job.
+ torch.cuda.set_per_process_memory_fraction(1.)
+ torch.cuda.reset_peak_memory_stats()
+ from .processes import worker_signals
+ with worker_signals():
+ if module.endswith('.worker'):
+ from .worker import generate
+ bank.configure(request['gpu_budget_bytes'], request.get('ram_budget_bytes'))
+ generate(request, resident=bank)
+ else:
+ from .encode_worker import encode
+ bank.configure(request['gpu_budget_gb']*1e9, request.get('ram_budget_bytes'))
+ encode(types.SimpleNamespace(comfy_root=None, check_library=False), request,
+ resident=bank, diagnostics=diagnostics)
+ print(json.dumps(dict(event='resident_models', **bank.snapshot())), flush=True)
+ return 0
+ except BaseException as error:
+ if diagnostics is not None:
+ diagnostics.finish(False)
+ traceback.print_exc()
+ from .adaptive import classify_failure
+ failure = classify_failure(error)
+ if request.get('metrics'):
+ from .monitoring import save
+ path = Path(request['metrics'])
+ try:
+ metrics = json.loads(path.read_text(encoding='utf-8'))
+ except (OSError, ValueError):
+ metrics = {}
+ failure = classify_failure(error, metrics)
+ metrics.update(success=False, error=repr(error), failure=failure,
+ work_seconds=time.perf_counter()-started)
+ save(path, metrics)
+ if 'torch' in sys.modules and sys.modules['torch'].cuda.is_initialized():
+ from .encoder_memory import failure_resources
+ # The video worker already captured the endpoint before
+ # its cleanup. Do not overwrite it with a later reading.
+ retained = metrics.get('failure', {})
+ if not retained.get('gpu'):
+ retained.update(failure_resources(sys.modules['torch'], query_cuda=failure['kind'] != 'cuda_error'))
+ metrics['gpu'] = retained['gpu']
+ save(path, metrics)
+ if failure['kind'] == 'cuda_error':
+ log.flush()
+ import os
+ os._exit(74)
+ failed_error = error
+
+ # ModelBank cannot own tensors held by an exception's traceback.
+ # Leave the handler first, save diagnostics above, then destroy
+ # failed frames before clearing models and allocator caches. Doing
+ # this inside except left the old weights alive for the next job.
+ from .failure_cleanup import release_exception_frames
+ cleanup = {}
+ try:
+ cleanup['frames_cleared'] = release_exception_frames(failed_error)
+ del failed_error
+ if bank is not None:
+ bank.clear()
+ gc.collect()
+ torch = sys.modules.get('torch')
+ if torch is not None and torch.cuda.is_initialized():
+ from .torch_compat import empty_host_cache
+ from .encoder_memory import snapshot as memory_snapshot
+ # Finish any pending transfers before recycling their
+ # pinned host storage for the next request.
+ torch.cuda.synchronize()
+ torch.cuda.empty_cache()
+ empty_host_cache(torch)
+ cleanup['after'] = memory_snapshot(torch, windows_memory=True)
+ cleanup['complete'] = True
+ except Exception as cleanup_error:
+ from .diagnostic_resources import exception_details
+ cleanup.update(complete=False, errors=exception_details(cleanup_error))
+ if metrics is not None:
+ cleanup['before'] = metrics.get('failure', {}).get('gpu', {})
+ metrics['failure_cleanup'] = cleanup
+ save(path, metrics)
+ print(json.dumps(dict(event='resident_failure_cleanup', **cleanup)), flush=True)
+ if not cleanup['complete']:
+ # A failed cleanup must not admit another model over whatever
+ # remains. The controller may start a fresh worker on retry.
+ log.flush()
+ import os
+ os._exit(75)
+ return 1
+ serve(args.endpoint, run, snapshot)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freevideo_engine/residual.py b/freevideo_engine/residual.py
new file mode 100644
index 0000000..abdeb24
--- /dev/null
+++ b/freevideo_engine/residual.py
@@ -0,0 +1,101 @@
+"""Single-buffer host residuals for the small-VRAM block path.
+
+The container transfers ownership: passing a GPU Tensor directly to Module
+would leave it alive in Module's argument tuple throughout both sublayers.
+Copies retain dtype and shape; normalization and residual kernels are unchanged.
+"""
+import time
+import torch
+
+
+class ResidualState:
+ def __init__(self, tensor):
+ if torch.is_grad_enabled() or tensor.requires_grad:
+ raise RuntimeError('Residual offload requires inference without autograd')
+ if not tensor.is_contiguous():
+ raise ValueError('Residual offload requires the original contiguous packed layout')
+ self.tensor = tensor
+ self.shape, self.dtype, self.device = tuple(tensor.shape), tensor.dtype, tensor.device
+ self.host = None
+ self.stored = False
+ self.closed = False
+ self.copies = {'to_host': 0, 'to_device': 0}
+ self.copy_wall_seconds = 0.
+
+ def take(self):
+ if self.closed or self.tensor is None:
+ raise RuntimeError('Residual has no device value to consume')
+ tensor, self.tensor = self.tensor, None
+ return tensor
+
+ def _check(self, tensor):
+ if self.closed or tuple(tensor.shape) != self.shape or tensor.dtype != self.dtype or tensor.device != self.device or not tensor.is_contiguous():
+ raise ValueError('Residual shape, dtype or device changed')
+
+ def store(self, tensor):
+ self._check(tensor)
+ if self.tensor is not None:
+ raise RuntimeError('Consume the device residual before staging it')
+ tick = time.perf_counter()
+ if self.host is None:
+ # Pageable storage avoids consuming the platform's limited locked
+ # pages in addition to attention readouts and the weight cache.
+ self.host = torch.empty(self.shape, dtype=self.dtype, device='cpu')
+ self.host.copy_(tensor, non_blocking=False)
+ self.stored = True
+ self.copies['to_host'] += 1
+ self.copy_wall_seconds += time.perf_counter()-tick
+
+ def restore(self):
+ if self.closed or not self.stored:
+ raise RuntimeError('No complete host residual is available')
+ tick = time.perf_counter()
+ # copy=True also makes CPU contract checks use independent storage.
+ tensor = self.host.to(self.device, copy=True, non_blocking=False)
+ self.copies['to_device'] += 1
+ self.copy_wall_seconds += time.perf_counter()-tick
+ return tensor
+
+ def replace(self, tensor):
+ self._check(tensor)
+ if self.tensor is not None:
+ raise RuntimeError('Cannot overwrite an unconsumed device residual')
+ self.tensor = tensor
+
+ def stats(self):
+ return dict(host_buffer_bytes=self.host.numel()*self.host.element_size() if self.host is not None else 0,
+ pinned_host_bytes=0, copies=dict(self.copies), blocking_copy_wall_seconds=self.copy_wall_seconds,
+ timing_scope='Blocking copy calls can include waiting for prior compute; not pure PCIe transfer time.')
+
+ def close(self):
+ self.tensor = self.host = None
+ self.stored = False
+ self.closed = True
+
+
+def residual_forward(pre, post):
+ def forward(self, state, temb, adaln_indices, rotary_emb, attention_mask=None):
+ if torch.is_grad_enabled():
+ raise RuntimeError('Residual offload requires inference without autograd')
+ if not isinstance(state, ResidualState):
+ raise TypeError('Residual offload needs the owning packed-input container')
+ hidden = state.take()
+ shift_a, scale_a, gate_a, shift_f, scale_f, gate_f = self.adaln_proj(temb)
+ normalized = pre(hidden, self.norm1.weight, self.norm1.eps, scale_a, shift_a, adaln_indices)
+ state.store(hidden)
+ del hidden
+ branch = self.attn(normalized, rotary_emb, attention_mask)
+ del normalized
+ residual = state.restore()
+ hidden = post(residual, gate_a, adaln_indices, branch)
+ del residual, branch
+ normalized = pre(hidden, self.norm2.weight, self.norm2.eps, scale_f, shift_f, adaln_indices)
+ state.store(hidden)
+ del hidden
+ branch = self.ff(normalized)
+ del normalized
+ residual = state.restore()
+ state.replace(post(residual, gate_f, adaln_indices, branch))
+ del residual, branch
+ return state
+ return forward
diff --git a/freevideo_engine/resource_cli.py b/freevideo_engine/resource_cli.py
new file mode 100644
index 0000000..a4fddbf
--- /dev/null
+++ b/freevideo_engine/resource_cli.py
@@ -0,0 +1,58 @@
+"""Inspect local performance and resource measurements without CUDA."""
+import argparse
+import json
+from pathlib import Path
+import sys
+
+from .resource_history import ResourceHistory, ResourceHistoryError
+
+
+def _emit(value, stream):
+ output = json.dumps(value, ensure_ascii=False, indent=2, allow_nan=False)
+ encoding = getattr(stream, 'encoding', None)
+ if encoding:
+ try:
+ output.encode(encoding)
+ except UnicodeEncodeError:
+ # A redirected Windows terminal may still use a legacy codepage.
+ # Keep valid JSON and a visible acknowledgment token in that case.
+ output = json.dumps(value, ensure_ascii=True, indent=2, allow_nan=False)
+ print(output, file=stream)
+
+
+def main(argv=None):
+ parser = argparse.ArgumentParser(prog='freevideo resource-history',
+ description='Inspect local performance measurements; no GPU work is started.')
+ commands = parser.add_subparsers(dest='command', required=True)
+ listing = commands.add_parser('list', help='Show performance measurements and discard stopped incomplete runs')
+ for command in (listing,):
+ command.add_argument('--history', type=Path,
+ help='Local history database; defaults to this installation')
+ args = parser.parse_args(argv)
+ try:
+ if args.history is None:
+ from .adaptive import history_path
+ path = history_path()
+ else:
+ path = args.history.expanduser()
+ exists = path.exists()
+ if args.command == 'list':
+ recovered, rows = [], []
+ if exists:
+ ledger = ResourceHistory(path)
+ recovered = ledger.recover_pending()
+ rows = ledger.attempts()
+ _emit({'history': str(path), 'history_exists': exists,
+ 'attempts': rows, 'total': len(rows),
+ 'discarded_incomplete_count': len(recovered),
+ 'note': 'Complete measurements guide predictions and optimization; '
+ 'history does not block configurations.'}, sys.stdout)
+ return 0
+ except (ResourceHistoryError, OSError, ValueError) as error:
+ _emit({'error': str(error), 'gpu_work_started': False,
+ 'note': 'No inference was started.'}, sys.stderr)
+ return 2
+
+
+if __name__ == '__main__':
+ raise SystemExit(main())
diff --git a/freevideo_engine/resource_history.py b/freevideo_engine/resource_history.py
new file mode 100644
index 0000000..14b66ad
--- /dev/null
+++ b/freevideo_engine/resource_history.py
@@ -0,0 +1,454 @@
+"""Local performance and resource measurements, importable without Torch.
+
+Only complete validated requests calibrate future predictions. Failed resource
+attempts support OOM/RAM placement recovery; incomplete or external device
+failures are excluded from calibration. Interrupted video workers can notify the
+separate, user-controlled compatibility settings; history never bans a
+configuration or grants permission to run one. The runtime lock owns execution.
+"""
+from contextlib import contextmanager
+import hashlib
+import json
+import math
+import os
+from pathlib import Path
+import sqlite3
+import time
+import uuid
+
+import psutil
+
+from .execution_config import engine_options, decoder_options
+from .system import windows
+
+
+SCHEMA = 6
+OUTCOMES = frozenset(('success', 'resource_failure', 'cancelled', 'code_failure',
+ 'discarded'))
+# Group arithmetic consistently while retaining placement and budget values in
+# the original configuration for performance analysis.
+INCIDENTAL_CONFIG_KEYS = frozenset(('resident_blocks', 'pin_host_gb', 'gpu_budget_bytes',
+ 'stream_weights', 'stream_output', 'residual_offload',
+ 'ram_budget_bytes', 'gpu_budget_gb', 'inference_ram_budget_gb', 'gpu_reserve_bytes',
+ 'ram_reserve_bytes', 'gpu_reserve_gib', 'ram_reserve_gib', 'vram_gib', 'ram_gib',
+ 'policy', 'evidence', 'base', 'checkpoint', 'cache', 'metrics', 'output', 'canvas',
+ 'source_sha256', 'driver_version', 'fa4_dependency'))
+
+
+class ResourceHistoryError(RuntimeError):
+ """The local performance database is unavailable or invalid."""
+
+
+
+
+def _json(value):
+ return json.dumps(value, sort_keys=True, separators=(',', ':'), allow_nan=False)
+
+
+def _digest(value):
+ return hashlib.sha256(_json(value).encode('utf-8')).hexdigest()
+
+
+def _computation(value):
+ if isinstance(value, dict):
+ return {key: _computation(item) for key, item in value.items()
+ if key not in INCIDENTAL_CONFIG_KEYS}
+ if isinstance(value, (list, tuple)):
+ return [_computation(item) for item in value]
+ return value
+
+
+def _canonical_config(config, purpose):
+ """Normalize an engine-only config or a complete legacy profile.
+
+ A legacy profile's executable sections are engine, decoder and allocator.
+ Its name, report, measured budgets and arbitrary descriptive top-level fields
+ are not new computations. Unknown knobs *inside* the executable sections
+ remain significant. Flat engine mappings remain a supported public API.
+ """
+ if not isinstance(config, dict):
+ raise ValueError('Attempt config must be an engine mapping or legacy profile')
+ if 'engine' in config or 'decoder' in config:
+ engine = config.get('engine', {})
+ decoder = config.get('decoder', {})
+ else:
+ engine = {key: value for key, value in config.items()
+ if key not in ('allocator_config', 'name', 'description')}
+ decoder = {}
+ if not isinstance(engine, dict) or not isinstance(decoder, dict):
+ raise ValueError('Engine and decoder attempt options must be mappings')
+ allocator = config.get('allocator_config', engine.get('allocator_config'))
+ # Encoding has different executable options; VDN defaults do not apply.
+ resolved_engine = engine_options(engine) if _purpose_phase(purpose) == 'video' else dict(engine)
+ resolved_decoder = decoder_options(decoder) if _purpose_phase(purpose) == 'video' else dict(decoder)
+ # task/steps also exist in public API geometry. Preserve their original
+ # omission until computation_identity resolves that established contract.
+ for name in ('task', 'steps'):
+ if name not in engine:
+ resolved_engine.pop(name, None)
+ normalized_engine = _computation(resolved_engine)
+ normalized_engine.pop('allocator_config', None)
+ return {'engine': normalized_engine, 'decoder': _computation(resolved_decoder),
+ 'allocator_config': allocator}
+
+
+def _purpose_phase(purpose):
+ return 'encoding' if purpose in ('encoder', 'encoding') else 'video'
+
+
+def computation_identity(identity, config, geometry, *, purpose='generation'):
+ """Canonical computation grouping, independent of placement and free resources.
+
+ UUID and OS are required: a device index/model name is not a local identity.
+ Real canvas/steps/task are fixed; seed/prompt text are deliberately omitted.
+ Placement is stored in each row for comparing performance of the same arithmetic.
+ ``config`` may be engine-only or a legacy profile. Executable engine/decoder
+ knobs and allocator settings are retained; profile labels are not identity.
+ Derived tokens are omitted because dimensions/frames already encode them;
+ text tokens remain measurements, since they are not always known at admission.
+ """
+ gpu = identity.get('gpu', {})
+ gpu_uuid = gpu.get('uuid') if isinstance(gpu, dict) else None
+ gpu_uuid = gpu_uuid or identity.get('gpu_uuid')
+ system = identity.get('system')
+ if not isinstance(gpu_uuid, str) or not gpu_uuid.strip() or not system:
+ raise ValueError('Durable resource history requires GPU UUID and operating system')
+ computation = _canonical_config(config, purpose)
+ canvas = {name: geometry.get(name) for name in ('width', 'height', 'frames')}
+ if geometry.get('sampling_plan', {}).get('enabled'):
+ canvas['sampling_plan'] = geometry['sampling_plan']
+ for key, default in (('steps', 8), ('task', 't2va')):
+ configured = computation['engine'].pop(key, None)
+ if key in geometry and configured is not None and geometry[key] != configured:
+ raise ValueError('Request geometry and engine disagree on ' + key)
+ canvas[key] = geometry.get(key, configured if configured is not None else default)
+ if any(type(canvas[key]) is not int or canvas[key] <= 0
+ for key in ('width', 'height', 'frames', 'steps')):
+ raise ValueError('Durable resource history needs positive request geometry and steps')
+ if _purpose_phase(purpose) == 'encoding' and computation['engine'].get('operation') == 'idle-preload':
+ # Loading weights consumes no prompt or canvas. A different next video
+ # does not describe a different weight-loading workload.
+ # The original complete request geometry remains in the attempt row.
+ canvas = {'scope': 'prompt-independent-weight-loading'}
+ return {'gpu_uuid': gpu_uuid.strip(), 'system': str(system),
+ 'phase': _purpose_phase(purpose), 'config': computation, 'geometry': canvas}
+
+
+def _boot_id():
+ # boot_time is wall-clock based and may shift after clock correction. Only a
+ # true boot UUID is used as conclusive restart evidence. On Windows the PID
+ # plus process creation FILETIME still detects both absence and PID reuse.
+ try:
+ return Path('/proc/sys/kernel/random/boot_id').read_text(encoding='ascii').strip()
+ except OSError:
+ return None
+
+
+def process_identity(pid=None):
+ pid = os.getpid() if pid is None else pid
+ try:
+ return {'pid': pid, 'created': psutil.Process(pid).create_time(), 'boot_id': _boot_id()}
+ except (psutil.Error, OSError) as error:
+ raise ResourceHistoryError('Cannot identify the attempt owner: ' + str(error)) from error
+
+
+def process_state(owner):
+ """Return alive/dead/unknown; inability to inspect is never proof of death."""
+ try:
+ if (type(owner.get('pid')) is not int or owner['pid'] <= 0
+ or not isinstance(owner.get('created'), (float, int))):
+ return 'unknown'
+ current_boot = _boot_id()
+ if current_boot and owner.get('boot_id') and current_boot != owner['boot_id']:
+ return 'dead'
+ if windows():
+ from .win32 import process_exited
+ exited = process_exited(owner['pid'])
+ if exited is True:
+ return 'dead'
+ if exited is None:
+ return 'unknown'
+ # An unsignaled handle still needs the creation-time check below:
+ # the current PID may belong to a different, live process.
+ process = psutil.Process(owner['pid'])
+ if process.create_time() != owner['created']:
+ return 'dead'
+ if process.status() == psutil.STATUS_ZOMBIE:
+ return 'dead'
+ return 'alive'
+ except psutil.NoSuchProcess:
+ return 'dead'
+ except (psutil.Error, OSError, KeyError, TypeError, ValueError):
+ return 'unknown'
+
+
+class ResourceHistory:
+ def __init__(self, path):
+ self.path = Path(path)
+ try:
+ self.path.parent.mkdir(parents=True, exist_ok=True)
+ existed = self.path.exists()
+ if existed and self.path.is_file() and self.path.stat().st_size == 0:
+ raise ResourceHistoryError('Existing resource history is empty; retain it for diagnosis rather than discarding measurements')
+ with self._transaction() as db:
+ if db.execute('PRAGMA quick_check').fetchone()[0] != 'ok':
+ raise ResourceHistoryError('Resource history failed its integrity check')
+ tables = {row[0] for row in db.execute("SELECT name FROM sqlite_master WHERE type='table'")}
+ if existed and not {'metadata', 'attempts'}.issubset(tables):
+ raise ResourceHistoryError('Existing resource history has an incomplete schema; refusing to recreate missing measurements')
+ db.execute('CREATE TABLE IF NOT EXISTS metadata (version INTEGER NOT NULL)')
+ versions = db.execute('SELECT version FROM metadata').fetchall()
+ if not versions:
+ if existed:
+ raise ResourceHistoryError('Existing resource history lost its schema version')
+ db.execute('INSERT INTO metadata VALUES (?)', (SCHEMA,))
+ elif len(versions) != 1 or versions[0][0] not in (1, 2, 3, 4, 5, SCHEMA):
+ raise ResourceHistoryError('Unsupported resource history schema; retain it for diagnosis')
+ db.execute('''CREATE TABLE IF NOT EXISTS attempts (
+ id TEXT PRIMARY KEY, computation_key TEXT NOT NULL, evidence_key TEXT NOT NULL,
+ state TEXT NOT NULL, started REAL NOT NULL, settled REAL,
+ identity_json TEXT NOT NULL, config_json TEXT NOT NULL, geometry_json TEXT NOT NULL,
+ owner_json TEXT NOT NULL, worker_json TEXT, purpose TEXT NOT NULL,
+ phase TEXT, details_json TEXT NOT NULL, observation_json TEXT)''')
+ columns={row[1] for row in db.execute('PRAGMA table_info(attempts)')}
+ if 'hazard_key' in columns:
+ db.execute('ALTER TABLE attempts RENAME COLUMN hazard_key TO computation_key')
+ db.execute('DROP INDEX IF EXISTS attempts_hazard')
+ db.execute('CREATE INDEX IF NOT EXISTS attempts_computation ON attempts(computation_key,state)')
+ db.execute('CREATE INDEX IF NOT EXISTS attempts_observations ON attempts(state, identity_json, started)')
+ # Version 6 retires device-failure quarantine and its consent
+ # table. Purge old blockers, preserving successful measurements
+ # and useful RAM/OOM outcomes. Repeated opens are idempotent.
+ db.execute("DELETE FROM attempts WHERE state IN ('device_lost','unacknowledged')")
+ db.execute('DROP TABLE IF EXISTS retry_acknowledgments')
+ if versions and versions[0][0] != SCHEMA:
+ for row in db.execute('SELECT id,details_json FROM attempts').fetchall():
+ details=json.loads(row['details_json'])
+ details.pop('hazard_key_migration',None)
+ details.pop('hazard_key_migrations',None)
+ db.execute('UPDATE attempts SET details_json=? WHERE id=?',(_json(details),row['id']))
+ db.execute('UPDATE metadata SET version=?',(SCHEMA,))
+ except (OSError, sqlite3.Error) as error:
+ raise ResourceHistoryError('Resource measurement history is unavailable: ' + str(error)) from error
+
+
+ @contextmanager
+ def _transaction(self):
+ db = None
+ try:
+ db = sqlite3.connect(str(self.path), timeout=15, isolation_level=None)
+ db.row_factory = sqlite3.Row
+ db.execute('PRAGMA secure_delete=ON')
+ db.execute('PRAGMA synchronous=FULL')
+ db.execute('PRAGMA journal_mode=DELETE')
+ db.execute('BEGIN IMMEDIATE')
+ yield db
+ db.commit()
+ except (sqlite3.Error, OSError) as error:
+ if db is not None:
+ db.rollback()
+ raise ResourceHistoryError('Cannot record resource measurements: ' + str(error)) from error
+ finally:
+ if db is not None:
+ db.close()
+
+ @staticmethod
+ def _row(row):
+ if row is None:
+ return None
+ result = dict(row)
+ result['outcome'] = result['state']
+ for key in list(result):
+ if key.endswith('_json'):
+ value = result.pop(key)
+ try:
+ result[key[:-5]] = json.loads(value) if value is not None else None
+ except ValueError as error:
+ raise ResourceHistoryError('Invalid stored resource-history record; retain it for diagnosis') from error
+ return result
+
+ @staticmethod
+ def _request_was_cancelled(row):
+ # A Windows Job can exit before the CLI's exception handler settles its
+ # attempt. The bridge's durable, request-local cancellation survives it.
+ artifacts = (row.get('details') or {}).get('decision_evidence', {}).get('request_artifacts')
+ if not isinstance(artifacts, str) or not artifacts:
+ return False
+ try:
+ artifacts = Path(artifacts).resolve()
+ with (artifacts.parent / 'comfy-request.json').open('rb') as stream:
+ raw = stream.read(64 * 1024 + 1)
+ if len(raw) > 64 * 1024:
+ return False
+ request = json.loads(raw)
+ return (isinstance(request, dict) and request.get('status') == 'cancelled'
+ and isinstance(request.get('output'), str) and bool(request['output'])
+ and Path(request['output']).with_suffix('.artifacts').resolve() == artifacts)
+ except (OSError, ValueError, TypeError):
+ return False
+
+ def _recover(self, db):
+ recovered=[]
+ for row in db.execute("SELECT * FROM attempts WHERE state='pending'").fetchall():
+ if process_state(json.loads(row['owner_json'])) != 'dead':
+ continue
+ if row['worker_json'] and process_state(json.loads(row['worker_json'])) != 'dead':
+ continue
+ record = self._row(row)
+ recovered.append(row['id'])
+ if self._request_was_cancelled(record):
+ details = dict(record['details'], kind='cancelled',
+ reason='User cancelled this ComfyUI request; controller and worker have exited.')
+ db.execute("UPDATE attempts SET state='cancelled',settled=?,details_json=? WHERE id=?",
+ (time.time(), _json(details), row['id']))
+ continue
+ from .compatibility import Store
+ Store(self.path.parent).incident(record, 'unfinished_request_after_owner_and_worker_exit')
+ db.execute('DELETE FROM attempts WHERE id=?',(row['id'],))
+ return recovered
+
+ def recover_pending(self):
+ """Settle known cancellations and discard other stopped incomplete work."""
+ with self._transaction() as db:
+ return self._recover(db)
+
+ def begin(self, identity, config, geometry, *, purpose='generation', owner=None,
+ evidence=None):
+ identifier=uuid.uuid4().hex
+ owner=process_identity() if owner is None else owner
+ if process_state(owner)!='alive':
+ raise ResourceHistoryError('Cannot identify the live measurement owner')
+ computation=_digest(computation_identity(identity,config,geometry,purpose=purpose))
+ from .compatibility import policy_revision
+ with self._transaction() as db:
+ self._recover(db)
+ db.execute("""INSERT INTO attempts (id,computation_key,evidence_key,state,started,
+ identity_json,config_json,geometry_json,owner_json,purpose,details_json)
+ VALUES (?,?,?,'pending',?,?,?,?,?,?,?)""",
+ (identifier,computation,_digest({'identity':identity,'config':config,'geometry':geometry}),
+ time.time(),_json(identity),_json(config),_json(geometry),_json(owner),purpose,
+ _json({'decision_evidence':evidence or {}, 'compatibility_revision':policy_revision()})))
+ return identifier
+
+ def attach_worker(self, attempt_id, pid, *, phase=None):
+ worker=process_identity(pid)
+ with self._transaction() as db:
+ changed=db.execute("UPDATE attempts SET worker_json=?,phase=COALESCE(?,phase) WHERE id=? AND state='pending'",
+ (_json(worker),phase,attempt_id)).rowcount
+ if changed!=1:raise ResourceHistoryError('Cannot attach a worker to an absent/settled attempt')
+
+ def set_phase(self, attempt_id, phase):
+ with self._transaction() as db:
+ if db.execute("UPDATE attempts SET phase=? WHERE id=? AND state='pending'",(str(phase),attempt_id)).rowcount!=1:
+ raise ResourceHistoryError('Cannot update an absent/settled attempt')
+
+ @staticmethod
+ def _observation(value, row):
+ if value is None:
+ return None
+ geometry = json.loads(row['geometry_json'])
+ from .two_pass import steps as sampling_steps
+ steps = sampling_steps(geometry['sampling_plan']) if geometry.get('sampling_plan') else geometry.get('steps', 8)
+ enriched_geometry = dict(geometry)
+ provided_geometry = value.get('geometry', {})
+ if not isinstance(provided_geometry, dict):
+ raise ValueError('Observed geometry must be an object')
+ for key in ('width', 'height', 'frames', 'steps', 'task', 'video_tokens', 'latent_frames', 'sampling_plan'):
+ expected = geometry.get(key, 8 if key == 'steps' else None)
+ if key in provided_geometry and expected is not None and provided_geometry[key] != expected:
+ raise ValueError('Observed geometry differs from the durably recorded request: ' + key)
+ enriched_geometry.update(provided_geometry)
+ text_tokens = value.get('text_tokens', enriched_geometry.get('text_tokens'))
+ if text_tokens is not None:
+ if type(text_tokens) is not int or text_tokens < 0:
+ raise ValueError('Observed text token count must be a nonnegative integer')
+ if 'text_tokens' in geometry and geometry['text_tokens'] != text_tokens:
+ raise ValueError('Observed text token count differs from the request')
+ enriched_geometry['text_tokens'] = text_tokens
+ if (row['purpose'] not in ('generation', 'validation', 'full_request')
+ or value.get('full_request') is not True or value.get('validated') is not True
+ or value.get('completed_steps') != steps
+ or value.get('completed_frames') != geometry['frames']
+ or value.get('media_verified') is not True or value.get('metrics_complete') is not True):
+ raise ValueError('Only a verified complete requested video/audio request supplies resource calibration')
+ times = value.get('step_seconds')
+ if (not isinstance(times, list) or len(times) != steps
+ or any(type(number) not in (int, float) or not math.isfinite(number) or number <= 0 for number in times)):
+ raise ValueError('Resource calibration needs all actual step timings; no extrapolation')
+ for key in ('peak_reserved_bytes', 'whole_gpu_peak_bytes', 'ram_peak_bytes'):
+ if type(value.get(key)) is not int or value[key] <= 0:
+ raise ValueError('Resource calibration needs a complete positive ' + key)
+ stages = value.get('stage_seconds')
+ if (not isinstance(stages, dict) or not stages
+ or any(type(number) not in (int, float) or not math.isfinite(number) or number < 0 for number in stages.values())):
+ raise ValueError('Resource calibration needs finite measured phase times')
+ if not isinstance(value.get('ram_metric'), str) or not value['ram_metric']:
+ raise ValueError('Resource calibration must name the platform RAM metric')
+ value = dict(value, geometry=enriched_geometry,
+ scope='complete_request_measurement_not_numerical_equivalence')
+ return _json(value)
+
+ def finish(self, attempt_id, outcome, *, details=None, observation=None):
+ """Keep measurements useful for optimization after the worker ends."""
+ if outcome not in OUTCOMES:
+ raise ValueError('Unknown attempt outcome: ' + str(outcome))
+ with self._transaction() as db:
+ row = db.execute('SELECT * FROM attempts WHERE id=?', (attempt_id,)).fetchone()
+ if row is None or row['state'] != 'pending':
+ raise ResourceHistoryError('Cannot settle an absent/already settled attempt')
+ if observation is not None and outcome != 'success':
+ raise ValueError('Failed attempts cannot certify resource calibration')
+ if outcome == 'discarded':
+ if (details or {}).get('kind') in ('cuda_error', 'unknown_worker_exit'):
+ from .compatibility import Store
+ Store(self.path.parent).incident(self._row(row), details['kind'])
+ db.execute('DELETE FROM attempts WHERE id=?',(attempt_id,))
+ return {'id':attempt_id,'outcome':'discarded'}
+ observed = self._observation(observation, row)
+ merged = json.loads(row['details_json'])
+ merged.update(details or {})
+ db.execute('UPDATE attempts SET state=?,settled=?,details_json=?,observation_json=? WHERE id=?',
+ (outcome, time.time(), _json(merged), observed, attempt_id))
+ return self._row(db.execute('SELECT * FROM attempts WHERE id=?', (attempt_id,)).fetchone())
+
+
+
+ def attempt(self, identifier):
+ """Read one retained attempt without scanning a user's entire history."""
+ with self._transaction() as db:
+ row = db.execute('SELECT * FROM attempts WHERE id=?', (identifier,)).fetchone()
+ return self._row(row) if row is not None else None
+
+ def attempts(self):
+ with self._transaction() as db:
+ return [self._row(row) for row in db.execute('SELECT * FROM attempts ORDER BY started')]
+
+ def observations(self, identity=None, config=None, geometry=None, *, limit=None, include_details=True):
+ """Measured complete requests only; filter exact software/config identity.
+
+ Unfiltered queries are diagnostic, not permission to transfer knowledge
+ to a different device or compute build. No probe extrapolation is stored.
+ Filter before decoding JSON; long histories must not load every device's
+ historical reports to predict one request. ``limit`` takes the newest
+ matching rows, returned chronologically, without deleting any evidence.
+ """
+ if limit is not None and (type(limit) is not int or limit <= 0):
+ raise ValueError('Observation limit must be a positive integer')
+ if type(include_details) is not bool:
+ raise ValueError('include_details must be a boolean')
+ clauses = ["state='success'", 'observation_json IS NOT NULL']
+ arguments = []
+ for name, value in (('identity', identity), ('config', config), ('geometry', geometry)):
+ if value is not None:
+ clauses.append(name + '_json=?')
+ arguments.append(_json(value))
+ columns = ('*' if include_details else 'id,state,started,settled,purpose,phase,'
+ 'identity_json,config_json,geometry_json,observation_json')
+ query = 'SELECT ' + columns + ' FROM attempts WHERE ' + ' AND '.join(clauses)
+ query += ' ORDER BY started DESC,id DESC' if limit is not None else ' ORDER BY started,id'
+ if limit is not None:
+ query += ' LIMIT ?'
+ arguments.append(limit)
+ with self._transaction() as db:
+ rows = [self._row(row) for row in db.execute(query, arguments)]
+ return list(reversed(rows)) if limit is not None else rows
diff --git a/freevideo_engine/resource_settings.py b/freevideo_engine/resource_settings.py
new file mode 100644
index 0000000..fdfe7f6
--- /dev/null
+++ b/freevideo_engine/resource_settings.py
@@ -0,0 +1,30 @@
+"""Persistent ComfyUI resource preferences, read once for each new request."""
+import json
+import math
+from pathlib import Path
+
+from .monitoring import save
+
+
+def validate(value):
+ if value is None:
+ return None # Leave the automatic policy in charge, including tight fits.
+ if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value) or value < .2:
+ raise ValueError('Reserved VRAM must be automatic or a finite number of at least 0.2 GiB')
+ return float(value)
+
+
+def read(root):
+ path = Path(root) / 'resource-settings.json'
+ if not path.exists():
+ return {'gpu_reserve_gib': None}
+ value = json.loads(path.read_text(encoding='utf-8'))
+ if not isinstance(value, dict):
+ raise ValueError('Invalid resource settings')
+ return {'gpu_reserve_gib': validate(value.get('gpu_reserve_gib'))}
+
+
+def write(root, value):
+ result = {'gpu_reserve_gib': validate(value)}
+ save(Path(root) / 'resource-settings.json', result)
+ return result
diff --git a/freevideo_engine/runtime.py b/freevideo_engine/runtime.py
new file mode 100644
index 0000000..53c3522
--- /dev/null
+++ b/freevideo_engine/runtime.py
@@ -0,0 +1,728 @@
+"""VDN T2VA/FL2VA engine with file-backed weights and bounded layer offloading."""
+import gc
+import hashlib
+import json
+import os
+import sys
+from pathlib import Path
+import time
+import types
+
+import torch
+
+from .adaln import CachedModulation, TableCache, ScheduleCursor, schedule_embeddings, schedule_timesteps
+from .attention import WindowAttention
+from .offload import LayerOffloader, pin_layer_weights, unload_streamed_layer, prepare_streamed_layer
+from .weights import skeleton
+from .paths import base_path, checkpoint_path
+from .torch_compat import empty_host_cache
+
+
+def _load_safetensors(path):
+ """Materialize a shard without retaining a file mapping.
+
+ ``safetensors.torch.load_file`` uses a memory map by default. That is
+ useful for Linux page-cache reuse, but on Windows the section view also
+ reserves system commit for the whole shard. During model preparation we
+ only need the tensors, so read them through the platform-aware streamed
+ opener and close the handle before returning the dictionary. Linux keeps
+ its mmap path; Windows uses safetensors' bounded ``pread`` backend.
+ """
+ from .streamed_weights import _open_safetensors
+ with _open_safetensors(path, framework='pt', device='cpu') as handle:
+ return {name: handle.get_tensor(name) for name in handle.keys()}
+
+
+class Engine:
+ @torch.no_grad()
+ def __init__(self, cache, *, base=None, checkpoint=None,
+ attention='dense', prefetch=True, adaln_cache=False,
+ inference_kernels=False, query_chunk=0, ff_chunk=0, head_chunk=0,
+ offload_refiner=False, attention_cpu_outputs=False, preload_host=False,
+ pin_host_weights=False, adaln_disk_cache=False, projection_chunk=0,
+ grouped_attention_outputs=False, fp8_ff_recompute=False,
+ resident_blocks=0, pin_host_gb=0., cache_refined_text=False, steps=8,
+ window_batch=1, linear_compute='native-fp8', input_cache_dir=None,
+ fp8_gemm='auto', window_varlen=False, varlen_smooth_k=True,
+ task='t2va', stream_weights=False, canvas=None, head_parallelism=1,
+ residual_offload=False):
+ from .media_request import TASKS
+ if task not in TASKS:
+ raise ValueError('Unsupported Engine task: ' + str(task))
+ self.task = task
+ if type(residual_offload) is not bool:
+ raise ValueError('Residual offload must be boolean')
+ if residual_offload and (not projection_chunk or not inference_kernels):
+ raise ValueError('Residual offload requires bounded packing and fused inference blocks')
+ self.canvas = dict(canvas) if canvas is not None else None
+ from .system import weight_cache_headroom, residual_host_headroom
+ self.weight_cache_headroom_bytes = weight_cache_headroom(
+ streamed=stream_weights, cpu_outputs=attention_cpu_outputs, canvas=self.canvas)
+ if residual_offload:
+ # The new pageable residual must not compete with retained weights.
+ # A reference packed residual is ~0.75 GiB; reserve 1 GiB and scale
+ # with video/reference rows. The live RAM guard remains in charge.
+ self.weight_cache_headroom_bytes += residual_host_headroom(self.canvas)
+ base = base_path() if base is None else Path(base)
+ checkpoint = checkpoint_path() if checkpoint is None else Path(checkpoint)
+ if linear_compute not in ('native-fp8', 'bf16-weight-only'):
+ raise ValueError('Unknown linear compute policy')
+ if min(query_chunk, ff_chunk, head_chunk, projection_chunk) < 0:
+ raise ValueError('Chunk sizes cannot be negative; zero disables chunking')
+ if window_batch < 1:
+ raise ValueError('Window batch must be positive')
+ if type(head_parallelism) is not int or head_parallelism not in (1, 2, 4):
+ raise ValueError('Head parallelism must be 1, 2 or 4')
+ if head_parallelism > 1 and (resident_blocks != 50 or attention_cpu_outputs or grouped_attention_outputs or not head_chunk):
+ raise ValueError('Parallel head groups require all 50 blocks resident and GPU head outputs')
+ if attention_cpu_outputs and not head_chunk:
+ raise ValueError('CPU attention outputs require head chunking')
+ if grouped_attention_outputs and not attention_cpu_outputs:
+ raise ValueError('Grouped readouts require CPU attention outputs')
+ if pin_host_weights and preload_host:
+ raise ValueError('Pinned weights already include host preloading; select one storage mode')
+ if pin_host_gb < 0 or resident_blocks < 0:
+ raise ValueError('Residency and host cache budgets cannot be negative')
+ if pin_host_gb and (pin_host_weights or preload_host):
+ raise ValueError('Bounded pinning is a separate storage mode')
+ if stream_weights and (pin_host_weights or preload_host):
+ raise ValueError('Streamed weights allow a bounded pinned subset, but not host preload or whole-model pinning')
+ if cache_refined_text and (not offload_refiner or not projection_chunk):
+ raise ValueError('Text refinement caching requires refiner offloading and streamed packing')
+ self.base = Path(base)
+ self.steps = steps
+ self.prefetch = prefetch
+ self.cache = Path(cache)
+ self.input_cache_dir = input_cache_dir
+ self.closed = False
+ self.offloader = None
+ self.stream_weight_source = None
+ self.stream_refiner_count = 0
+ self.cache_refined_text = cache_refined_text
+ self.refiner_released = False
+ started = time.perf_counter()
+ manifest = json.loads((self.cache / 'manifest.json').read_text(encoding='utf-8'))
+ from .adaln_assets import SLIM_FORMAT, validate_catalog, restore_projections
+ portable_adaln = manifest.get('format') == SLIM_FORMAT
+ if portable_adaln:
+ # In the slim format these constants are model parameters, not a
+ # disposable optimization cache. The effective mode is reported.
+ adaln_cache = True
+ for group in manifest['groups']:
+ if (self.cache / group['file']).stat().st_size != group['bytes']:
+ raise ValueError('Incomplete prepared weights')
+ self.attention = WindowAttention(attention, query_chunk, window_batch,
+ window_varlen=window_varlen,
+ varlen_smooth_k=varlen_smooth_k)
+ model = skeleton(self.base, Path(checkpoint))
+ validate_catalog(manifest, len(model.transformer_blocks))
+ if resident_blocks > len(model.transformer_blocks):
+ raise ValueError('More resident blocks requested than the model contains')
+ # Build the streamed source before binding transformer blocks. The
+ # previous order loaded every non-resident block into CPU tensors and
+ # only converted them to zero-storage placeholders when the first
+ # LayerOffloader was created at sampling time. On Windows that made
+ # model preparation retain the whole checkpoint's CPU working set (and
+ # its pagefile commit) even though stream_weights was selected. On
+ # Windows, select the bounded pinned subset one block at a time; a
+ # load-all pass would consume the free RAM used to admit that cache.
+ # Discard unpinned blocks immediately and restore them on demand.
+ streamed_source = None
+ from .system import windows
+ incremental_pinning = stream_weights and bool(pin_host_gb) and windows()
+ prepared_pins = dict(logical=0, reserved=0, seconds=0.)
+ streamed_refiner_count = (len(model.token_refiner.refiner_blocks)
+ if stream_weights and offload_refiner and not cache_refined_text else 0)
+ if stream_weights and not adaln_cache:
+ # SafetensorLayers indexes every streamed shard up front. Slim
+ # caches may restore their AdaLN projections lazily, so ensure
+ # those files exist before constructing that index.
+ if any(not (self.cache / f'adaln/{index:02d}.safetensors').is_file()
+ for index in range(resident_blocks, len(model.transformer_blocks))):
+ restore_projections(self.cache, manifest)
+ if stream_weights and (not pin_host_gb or incremental_pinning):
+ from .streamed_weights import SafetensorLayers
+ streamed_prefixes = ([f'token_refiner.refiner_blocks.{index}.'
+ for index in range(streamed_refiner_count)]
+ + [f'transformer_blocks.{index}.'
+ for index in range(resident_blocks, len(model.transformer_blocks))])
+ streamed_paths = ([self.cache / 'root.safetensors'] if streamed_refiner_count else [])
+ streamed_paths += [self.cache / f'blocks/{index:02d}.safetensors'
+ for index in range(resident_blocks, len(model.transformer_blocks))]
+ if not adaln_cache:
+ streamed_paths += [self.cache / f'adaln/{index:02d}.safetensors'
+ for index in range(resident_blocks, len(model.transformer_blocks))]
+ if streamed_prefixes:
+ streamed_source = SafetensorLayers(streamed_paths, streamed_prefixes)
+ self.stream_weight_source = streamed_source
+ # Only source-backed placeholders may remain empty between
+ # refinement requests; pinned groups keep their own storage.
+ self.stream_refiner_count = streamed_refiner_count
+ def prepare_streamed(block, index):
+ if incremental_pinning:
+ tick = time.perf_counter()
+ logical, reserved = prepare_streamed_layer(block, streamed_source, index,
+ pin_budget_bytes=max(0, int(pin_host_gb * 1e9) - prepared_pins['reserved']),
+ headroom_bytes=self.weight_cache_headroom_bytes)
+ prepared_pins['logical'] += logical
+ prepared_pins['reserved'] += reserved
+ prepared_pins['seconds'] += time.perf_counter() - tick
+ else:
+ unload_streamed_layer(block, streamed_source, index)
+ precision = manifest.get('precision', 'bf16')
+ actual_fp8_gemm = None
+ if precision == 'fp8':
+ from src.models.ops.fp8_linear import per_tensor_gemm
+ from .fp8 import install_cached_linears
+ expected = 'per_tensor' if per_tensor_gemm() else 'rowwise'
+ if linear_compute == 'native-fp8' and torch.cuda.get_device_capability() < (8, 9):
+ raise ValueError('Native FP8 GEMM requires Ada or newer; choose bf16-weight-only on Ampere')
+ if linear_compute == 'native-fp8' and manifest['scale_granularity'] != expected:
+ raise ValueError('FP8 cache scale granularity does not match this GPU; prepare a separate cache')
+ install_cached_linears(model, manifest['linears'], weight_only=linear_compute == 'bf16-weight-only')
+ if linear_compute == 'native-fp8':
+ from .fp8_gemm import install
+ actual_fp8_gemm = install(model, manifest['scale_granularity'], requested=fp8_gemm)
+ elif precision != 'bf16':
+ raise ValueError('Unknown prepared weight precision')
+ root_weights = _load_safetensors(self.cache / 'root.safetensors')
+ root_weights = {name: value if offload_refiner and name.startswith('token_refiner.refiner_blocks.') else value.to('cuda')
+ for name, value in root_weights.items()}
+ result = model.load_state_dict(root_weights, strict=False, assign=True)
+ if result.unexpected_keys or any(not key.startswith('transformer_blocks.') for key in result.missing_keys):
+ raise ValueError('Unexpected root weight keys')
+ del root_weights
+ if streamed_source is not None and streamed_refiner_count:
+ # Refiner weights live in root.safetensors and otherwise remain a
+ # second large CPU allocation until LayerOffloader is constructed.
+ # Apply the same metadata-only representation used for transformer
+ # blocks immediately after binding the root state.
+ for index, block in enumerate(model.token_refiner.refiner_blocks):
+ prepare_streamed(block, index)
+ model.rope.to('cuda')
+ self.cursor = None
+ self.schedule_hook = None
+ embeddings = None
+ if adaln_cache:
+ self.cursor = ScheduleCursor(schedule_timesteps(steps, task=task))
+ table_cache = (TableCache(self.cache, manifest['source_id'], steps, task=task, manifest=manifest,
+ timesteps=self.cursor.timesteps, channels=model.config.hidden_size, device='cpu')
+ if adaln_cache and (adaln_disk_cache or portable_adaln) else None)
+ table_cache_hits = 0
+ adaln_prepare_seconds = 0.
+ self.resident_weight_bytes = 0
+ block_bind_seconds = 0.
+ upload_submit_seconds = 0.
+ for index, block in enumerate(model.transformer_blocks):
+ prefix = f'transformer_blocks.{index}.'
+ adaln_started = time.perf_counter()
+ table = table_cache.load(index, steps) if table_cache is not None else None
+ cache_hit = table is not None
+ if cache_hit:
+ block.adaln_proj = CachedModulation(table, self.cursor).to('cuda', non_blocking=True)
+ table_cache_hits += 1
+ del table
+ else:
+ if not (self.cache / f'adaln/{index:02d}.safetensors').is_file():
+ restore_projections(self.cache, manifest)
+ adaln = {name.removeprefix(prefix): value for name, value in
+ _load_safetensors(self.cache / f'adaln/{index:02d}.safetensors').items()}
+ if adaln_cache and not cache_hit:
+ if embeddings is None:
+ _, embeddings = schedule_embeddings(model, steps, task=task)
+ block.load_state_dict({name: value.to('cuda') for name, value in adaln.items()}, strict=False, assign=True)
+ table = [block.adaln_proj(embedding) for embedding in embeddings]
+ block.adaln_proj = CachedModulation(table, self.cursor)
+ if table_cache is not None:
+ table_cache.save(index, block.adaln_proj)
+ del table, adaln
+ adaln_prepare_seconds += time.perf_counter() - adaln_started
+ bind_started = time.perf_counter()
+ weights = {name.removeprefix(prefix): value for name, value in
+ _load_safetensors(self.cache / f'blocks/{index:02d}.safetensors').items()}
+ if not adaln_cache:
+ weights.update(adaln)
+ result = block.load_state_dict(weights, strict=False, assign=True)
+ if result.unexpected_keys or any(not key.startswith('adaln_proj.step_') for key in result.missing_keys):
+ raise ValueError(f'Incomplete block {index}: {result}')
+ del weights
+ if not adaln_cache:
+ # ``weights.update(adaln)`` leaves the source dictionary alive
+ # until the next iteration. Drop it before replacing this
+ # block's storage with a streamed placeholder.
+ del adaln
+ block_bind_seconds += time.perf_counter() - bind_started
+ if index < resident_blocks:
+ # Bind one immutable block, enqueue its copies, then prepare
+ # the next. Do not retain all 50 CPU mappings before starting
+ # the upload. Default-stream ordering protects sampling and
+ # the table copies without another GPU weight buffer.
+ from .offload import cpu_weights
+ self.resident_weight_bytes += sum(value.numel() * value.element_size() for _, value in cpu_weights(block))
+ upload_started = time.perf_counter()
+ block.to('cuda', non_blocking=True)
+ upload_submit_seconds += time.perf_counter() - upload_started
+ elif streamed_source is not None:
+ # Keep only the bounded pinned subset or metadata. Immutable
+ # unpinned bytes are reopened by SafetensorLayers on demand,
+ # rather than accumulating until the first sampling step.
+ prepare_streamed(block, streamed_refiner_count + index - resident_blocks)
+ if (index + 1) % 5 == 0:
+ print(json.dumps({'event': 'prepared_blocks', 'blocks': index + 1,
+ 'adaln_cache': adaln_cache, 'adaln_reused_blocks': table_cache_hits,
+ 'resident_uploaded_blocks': min(index + 1, resident_blocks),
+ 'phase': 'Load cached blocks and upload resident weights',
+ 'seconds': time.perf_counter() - started}), flush=True)
+ if adaln_cache:
+ del embeddings
+ self.schedule_hook = model.register_forward_pre_hook(self.cursor.before, with_kwargs=True)
+ if any(p.is_meta for p in list(model.parameters()) + list(model.buffers())):
+ raise RuntimeError('Some transformer parameters are still on meta')
+ model.eval().requires_grad_(False)
+ from src.models.hybrid_transform import set_inference_mode, set_softmax_backend
+ # Engine supplies every window call, including the official cuDNN/FA4
+ # pair. Avoid upstream auto importing an unselected optional FA4 package.
+ set_softmax_backend(model, 'flex')
+ set_inference_mode(model, inference_kernels)
+ self.attention.install(model)
+ if head_chunk:
+ if self.attention is None or not inference_kernels:
+ raise ValueError('Head chunks require a window policy and inference kernels')
+ from .head_chunk import install_head_chunks
+ install_head_chunks(model, self.attention, head_chunk, cpu_outputs=attention_cpu_outputs,
+ projection_chunk=projection_chunk or 1024, grouped_outputs=grouped_attention_outputs,
+ parallelism=head_parallelism)
+ if ff_chunk:
+ for block in model.transformer_blocks:
+ if precision == 'fp8' and linear_compute == 'native-fp8':
+ if not inference_kernels:
+ raise ValueError('FP8 FF chunks require the official inference kernels')
+ from .fp8_ops import install_chunked_ff
+ install_chunked_ff(block.ff, ff_chunk, recompute=fp8_ff_recompute)
+ continue
+ original = block.ff.forward
+ block.ff._freevideo_unchunked_forward = original
+
+ def chunked(ff, hidden, *args, original=original, **kwargs):
+ output = torch.empty_like(hidden)
+ for start in range(0, hidden.shape[-2], ff_chunk):
+ output[..., start:start + ff_chunk, :] = original(hidden[..., start:start + ff_chunk, :], *args, **kwargs)
+ return output
+
+ block.ff.forward = types.MethodType(chunked, block.ff)
+ if projection_chunk:
+ from .packing import install_streamed_forward
+ install_streamed_forward(model, projection_chunk, residual_offload=residual_offload)
+ if inference_kernels:
+ from .blocks import install_bounded_blocks
+ install_bounded_blocks(model, residual_offload=residual_offload)
+ self.transformer = model
+ print(json.dumps({'event': 'model_load_phase', 'phase': 'Preparing offloaded weight storage'}), flush=True)
+ self.offload_layers = (list(model.token_refiner.refiner_blocks) if offload_refiner and not cache_refined_text else []) + list(model.transformer_blocks)[resident_blocks:]
+ if stream_weights and self.stream_weight_source is None:
+ from .streamed_weights import SafetensorLayers
+ prefixes = ([f'token_refiner.refiner_blocks.{index}.' for index in range(len(model.token_refiner.refiner_blocks))]
+ if offload_refiner and not cache_refined_text else [])
+ paths = [self.cache / 'root.safetensors'] if prefixes else []
+ prefixes += [f'transformer_blocks.{index}.' for index in range(resident_blocks, len(model.transformer_blocks))]
+ paths += [self.cache / f'blocks/{index:02d}.safetensors' for index in range(resident_blocks, len(model.transformer_blocks))]
+ if not adaln_cache:
+ paths += [self.cache / f'adaln/{index:02d}.safetensors' for index in range(resident_blocks, len(model.transformer_blocks))]
+ self.stream_weight_source = SafetensorLayers(paths, prefixes)
+ self.host_preload_seconds = 0.
+ self.pinned_model_bytes = prepared_pins['logical']
+ self.pinned_host_allocated_bytes = 0
+ pin_seconds = prepared_pins['seconds']
+ pin_order = 'sequential'
+ if pin_host_weights or pin_host_gb:
+ host_headroom = self.weight_cache_headroom_bytes
+ if not incremental_pinning:
+ pin_start = time.perf_counter()
+ pin_layers = self.offload_layers
+ if stream_weights and attention_cpu_outputs and cache_refined_text and not windows():
+ from .streamed_weights import interleaved_layer_order
+ pin_layers = [self.offload_layers[index] for index in interleaved_layer_order(len(self.offload_layers))]
+ pin_order = 'interleaved'
+ self.pinned_model_bytes = pin_layer_weights(pin_layers, max_bytes=int(pin_host_gb * 1e9) if pin_host_gb else None,
+ headroom_bytes=host_headroom)
+ pin_seconds = time.perf_counter() - pin_start
+ self.pinned_host_allocated_bytes = torch.cuda.memory.host_memory_stats()['allocated_bytes.current']
+ print(json.dumps({'event': 'host_weights_pinned', 'seconds': pin_seconds,
+ 'logical_bytes': self.pinned_model_bytes,
+ 'working_headroom_bytes': host_headroom,
+ 'pin_order': pin_order,
+ 'host_allocator_bytes': self.pinned_host_allocated_bytes}), flush=True)
+ if preload_host:
+ preload_start = time.perf_counter()
+ buffer = bytearray(8 * 1024 * 1024)
+ for group in manifest['groups']:
+ if group['group'].startswith('adaln/') and adaln_cache:
+ continue
+ with (self.cache / group['file']).open('rb', buffering=0) as stream:
+ while stream.readinto(buffer):
+ pass
+ del buffer
+ self.host_preload_seconds = time.perf_counter() - preload_start
+ print(json.dumps({'event': 'host_weights_read', 'seconds': self.host_preload_seconds}), flush=True)
+ torch.cuda.synchronize()
+ torch.cuda.empty_cache()
+ self.load_seconds = time.perf_counter() - started
+ self.load_breakdown = {'adaln_prepare_seconds': adaln_prepare_seconds, 'pin_weights_seconds': pin_seconds,
+ 'block_bind_seconds': block_bind_seconds,
+ 'resident_upload_submit_seconds': upload_submit_seconds,
+ 'resident_upload_overlaps_block_preparation': True,
+ 'weight_cache_headroom_bytes': self.weight_cache_headroom_bytes,
+ 'weight_cache_pin_order': pin_order,
+ 'host_pinning_during_load': incremental_pinning,
+ 'host_preload_seconds': self.host_preload_seconds,
+ 'remaining_load_seconds': self.load_seconds - adaln_prepare_seconds - pin_seconds - self.host_preload_seconds,
+ 'scope': 'Host wall time within model load; AdaLN includes table reads or computation and writes.'}
+ fa4_module = sys.modules.get('flash_attn.cute.flash_fwd')
+ fa4_path = Path(fa4_module.__file__).resolve() if fa4_module is not None else None
+ self.config = {'task': task, 'attention': attention, 'prefetch': prefetch, 'adaln_cache': adaln_cache,
+ 'adaln_mode': 'portable-model-asset' if table_cache is not None and table_cache.asset else 'local-precompute' if adaln_cache else 'original-projections',
+ 'adaln_table_identity': table_cache.identity if table_cache is not None else None,
+ 'adaln_table_producer': table_cache.producer if table_cache is not None else None,
+ 'fa4_dependency': ({'file': str(fa4_path), 'sha256': hashlib.sha256(fa4_path.read_bytes()).hexdigest()}
+ if fa4_path is not None else None),
+ 'precision': precision, 'fp8_linears': len(manifest.get('linears', {})),
+ 'linear_compute': linear_compute,
+ 'fp8_gemm': actual_fp8_gemm,
+ 'fp8_scale_granularity': manifest.get('scale_granularity'),
+ 'fp8_ff_recompute': fp8_ff_recompute,
+ 'resident_blocks': resident_blocks, 'resident_weight_bytes': self.resident_weight_bytes,
+ 'stream_weights': stream_weights,
+ 'cache_refined_text': cache_refined_text,
+ 'pin_host_gb': pin_host_gb,
+ 'inference_kernels': inference_kernels, 'query_chunk': query_chunk,
+ 'window_batch': window_batch,
+ 'ff_chunk': ff_chunk, 'head_chunk': head_chunk, 'head_parallelism': head_parallelism,
+ 'offload_refiner': offload_refiner,
+ 'attention_cpu_outputs': attention_cpu_outputs,
+ 'grouped_attention_outputs': grouped_attention_outputs,
+ 'preload_host': preload_host,
+ 'pin_host_weights': pin_host_weights,
+ 'pinned_model_bytes': self.pinned_model_bytes,
+ 'pinned_host_allocated_bytes': self.pinned_host_allocated_bytes,
+ 'allocator_config': os.environ.get('PYTORCH_ALLOC_CONF', os.environ.get('PYTORCH_CUDA_ALLOC_CONF', 'default')),
+ 'adaln_disk_cache': adaln_disk_cache, 'adaln_table_cache_hits': table_cache_hits,
+ 'projection_chunk': projection_chunk,
+ 'reuse_block_outputs': bool(projection_chunk and inference_kernels),
+ 'residual_offload': residual_offload,
+ 'window_varlen': window_varlen,
+ 'varlen_smooth_k': varlen_smooth_k if window_varlen else None,
+ 'steps': steps, 'source_id': manifest['source_id']}
+
+ @torch.no_grad()
+ def sample(self, *args, compute_options=None, **kwargs):
+ from .pass_configuration import configuration
+ with configuration(self, compute_options):
+ return self._sample(*args, **kwargs)
+
+ @torch.no_grad()
+ def _sample(self, conditioning, seed, frames=243, width=1344, height=768, *, final_step_callback=None,
+ step_callback=None, sampling_complete_callback=None, initial_latents=None, refine_steps=None,
+ allow_smaller_canvas=False, progress_offset=0, progress_total=None,
+ pass_cache_budget_bytes=None, gpu_reserve_bytes=0, pass_resident_blocks=None,
+ budget_refresh=None):
+ from src.inference.render import load_prompt, generate_latents
+ from .geometry import geometry, sampler_for_canvas
+ refining = initial_latents is not None
+ if refining != (refine_steps is not None):
+ raise ValueError('Refinement requires both initial latents and a tail step count')
+ if refining:
+ from .refine import generate_latents, validate_tail
+ validate_tail(self.steps, refine_steps)
+ active_steps = refine_steps if refining else self.steps
+ canvas = geometry(width, height, frames=frames)
+ if self.canvas is not None and any(canvas[key] != self.canvas[key] for key in ('width', 'height', 'frames')):
+ smaller = (allow_smaller_canvas and not refining and frames == self.canvas['frames']
+ and width <= self.canvas['width'] and height <= self.canvas['height'])
+ if not smaller:
+ raise ValueError('This engine was loaded for a different request geometry; reload its memory placement')
+ if self.canvas is not None:
+ for name in ('reference_video_tokens', 'reference_audio_tokens'):
+ if name in self.canvas:
+ canvas[name] = self.canvas[name]
+ frames = canvas['frames']
+ if self.task.startswith('ref2va') and not refining:
+ from .reference_sampler import generate_latents
+ generate_latents = sampler_for_canvas(generate_latents, width, height)
+ if refining:
+ from functools import partial
+ generate_latents = partial(generate_latents, initial_latents=initial_latents, refine_steps=refine_steps)
+ if self.closed:
+ raise RuntimeError('Engine is closed')
+ from .sampling_progress import SamplingProgress
+ progress = SamplingProgress(active_steps, len(self.transformer.transformer_blocks),
+ offset=progress_offset, total=progress_total)
+ progress.start()
+ if self.task.startswith('ref2va'):
+ from .media_conditioning import describe
+ value = torch.load(conditioning, map_location='cpu', weights_only=True)
+ info = describe(value, width, height, frames)
+ if info['task'] != self.task:
+ raise ValueError('Reference modalities changed the precomputed schedule')
+ conditions = value['references']
+ prompt, tags = value['prompt_embeds'].to('cuda', torch.bfloat16), value['text_token_tags']
+ else:
+ prompt, tags, conditions = load_prompt(str(conditioning), 'cuda')
+ from .keyframes import validate_conditioning
+ source_canvas = self.canvas if allow_smaller_canvas and self.canvas is not None else canvas
+ validate_conditioning(prompt, tags, conditions, self.task,
+ source_canvas['width'], source_canvas['height'])
+ if conditions and (width, height) != (source_canvas['width'], source_canvas['height']):
+ from .keyframes import first_pass_conditions
+ conditions = first_pass_conditions(conditions, source_canvas, canvas)
+ validate_conditioning(prompt, tags, conditions, self.task, width, height)
+ if conditions:
+ canvas['reference_video_tokens'] = len(conditions[0]) * (width // 32) * (height // 32)
+ if self.cursor is not None:
+ self.cursor.reset(self.steps - refine_steps if refining else 0)
+ # DecoderReadAhead is CPU/file-cache only, so start it while the last
+ # two denoising steps are still running. Starting at the final step
+ # left slow SSDs with too little overlap to hide a cold VAE load;
+ # beginning earlier is still bounded by DecoderReadAhead's live RAM
+ # check and never allocates CUDA or pinned memory. Keep the trigger
+ # timing in the sample receipt: it lets the report distinguish a
+ # reader that had enough sampling time to finish from one that was
+ # merely started before decode. The one-step case keeps the immediate
+ # callback below.
+ prefetch_step = max(1, active_steps - 2)
+ prefetch_timing = dict(trigger_step=None, steps_remaining=None,
+ estimated_remaining_seconds=None,
+ trigger_elapsed_seconds=None)
+
+ def trigger_prefetch():
+ if final_step_callback is None or prefetch_timing['trigger_step'] is not None:
+ return
+ completed = len(step_seconds)
+ durations = list(step_seconds)
+ average = (sum(durations) / len(durations)) if durations else None
+ remaining = max(0, active_steps - completed)
+ prefetch_timing.update(
+ trigger_step=completed,
+ steps_remaining=remaining,
+ estimated_remaining_seconds=(average * remaining
+ if average is not None else None),
+ trigger_elapsed_seconds=time.perf_counter() - started)
+ final_step_callback()
+
+ class StepTimes(list):
+ def append(self, value):
+ super().append(value)
+ cached = offloader.cached_bytes() if budget_refresh is not None else 0
+ peak = torch.cuda.max_memory_reserved()
+ if budget_refresh is not None:
+ # Cache tensors are idle here; no forward hook or transfer
+ # owns them. Drop optional residency before lowering a cap.
+ # CUDA's peak is cumulative across this pass. Remember the
+ # largest cache too, so releasing a cache does not turn its
+ # historical peak into fictional activation workspace.
+ cache_peak = max(pass_cache.get('peak_cache_bytes', 0), cached)
+ pass_cache['peak_cache_bytes'] = cache_peak
+ base_peak = max(pass_cache.get('workspace_peak_bytes', 0), peak - cache_peak)
+ pass_cache['workspace_peak_bytes'] = base_peak
+ live_budget = budget_refresh('sample', lambda limit: offloader.cache_between_steps(
+ min(cached, max(0, limit - base_peak - 512 * 2**20)), allow_growth=False))
+ if len(self) < active_steps:
+ from .two_pass import pass_cache_allowance
+ from .system import system_memory
+ cached = offloader.cached_bytes()
+ free, _ = torch.cuda.mem_get_info()
+ commit = system_memory().get('commit_available_bytes')
+ allowance = pass_cache_allowance(live_budget, base_peak, free + cached,
+ gpu_reserve_bytes, None if commit is None else commit + cached)
+ pass_cache.update(gpu_budget_bytes=live_budget, measured_peak_reserved_bytes=peak,
+ live_free_bytes=free, commit_available_bytes=commit, admitted_bytes=allowance)
+ # New planes fill during the next step and only save a
+ # transfer on a subsequent step. The two-step refine
+ # pass therefore cannot benefit from a late fill.
+ offloader.cache_between_steps(allowance, allow_growth=len(self) + 1 < active_steps)
+ elif (len(self) == 1 and pass_cache_budget_bytes is not None and allow_smaller_canvas
+ and owner.canvas is not None and canvas['video_tokens'] < owner.canvas['video_tokens']):
+ from .two_pass import pass_cache_allowance
+ from .system import system_memory
+ free, _ = torch.cuda.mem_get_info()
+ peak = torch.cuda.max_memory_reserved()
+ commit = system_memory().get('commit_available_bytes')
+ allowance = pass_cache_allowance(pass_cache_budget_bytes, peak, free, gpu_reserve_bytes, commit)
+ pass_cache.update(measured_peak_reserved_bytes=peak, live_free_bytes=free,
+ commit_available_bytes=commit, admitted_bytes=allowance)
+ target = (max(0, pass_resident_blocks - owner.config['resident_blocks'])
+ if pass_resident_blocks is not None else None)
+ offloader.cache_between_steps(allowance, max_layers=target,
+ allow_growth=len(self) + 1 < active_steps)
+ if owner.config.get('residual_offload'):
+ residual_steps.append(dict(owner.transformer._freevideo_residual_stats))
+ progress.complete(value)
+ if step_callback is not None:
+ step_callback(value)
+ if active_steps > 1 and len(self) == prefetch_step:
+ trigger_prefetch()
+
+ owner = self
+ step_seconds = StepTimes()
+ pass_cache = dict(gpu_budget_bytes=pass_cache_budget_bytes, admitted_bytes=0,
+ requested_resident_blocks=pass_resident_blocks)
+ residual_steps = []
+ attention_before = dict(self.attention.backend_calls) if self.attention is not None else None
+ gemm_before = None
+ if self.config.get('fp8_gemm'):
+ from .fp8_gemm import execution_counts
+ gemm_before = execution_counts()
+ parallel_before = {name: getattr(self.attention, name, 0)
+ for name in ('parallel_head_calls', 'parallel_head_warmups')}
+ torch.cuda.reset_peak_memory_stats()
+ torch.cuda.synchronize()
+ started = time.perf_counter()
+ refinement = None
+ if self.cache_refined_text:
+ refinement = self._prepare_refined_text(prompt)
+ if active_steps == 1:
+ trigger_prefetch()
+ checkpoint_seconds = 0.
+ try:
+ with progress.layers(self.transformer.transformer_blocks), LayerOffloader(
+ self.offload_layers, prefetch=self.prefetch, weight_source=self.stream_weight_source) as offloader:
+ latents, audio = generate_latents(self.transformer, prompt, tags, frames,
+ self.steps, seed, 'cuda', step_seconds=step_seconds,
+ conditions=conditions)
+ stats = offloader.stats()
+ # StepTimes.append closes over the live offloader. Retaining
+ # that list subclass in a receipt also retains its pinned
+ # weights through VAE loading. Reports own plain numbers only.
+ sampled = {'sample_seconds': time.perf_counter() - started, 'step_seconds': list(step_seconds),
+ 'offload': stats, 'residual_offload_steps': residual_steps,
+ 'pass_cache_admission': pass_cache,
+ 'compute_configuration': {key: self.config.get(key) for key in
+ ('head_chunk', 'window_batch', 'ff_chunk', 'projection_chunk', 'prefetch',
+ 'attention_cpu_outputs', 'grouped_attention_outputs', 'residual_offload',
+ 'fp8_ff_recompute', 'resident_blocks', 'stream_weights', 'pin_host_gb')},
+ 'decoder_prefetch': dict(prefetch_timing),
+ 'geometry': canvas, 'conditioning_shape': list(prompt.shape),
+ 'attention_backend_calls': ({key: count - attention_before[key]
+ for key, count in self.attention.backend_calls.items()} if attention_before is not None else None),
+ 'fp8_kernel_calls': ({key: count - gemm_before[key]
+ for key, count in execution_counts().items()} if gemm_before is not None else None),
+ 'head_execution': dict(requested_parallelism=self.config.get('head_parallelism', 1),
+ **{name: getattr(self.attention, name, 0) - count
+ for name, count in parallel_before.items()}),
+ 'text_refinement': refinement,
+ 'torch_peak_allocated_bytes': torch.cuda.max_memory_allocated(),
+ 'torch_peak_reserved_bytes': torch.cuda.max_memory_reserved()}
+ if refining:
+ sampled['refinement'] = dict(base_steps=self.steps, steps=refine_steps,
+ start_index=self.steps - refine_steps, restart_seed=seed,
+ audio_policy='first_pass_preserved_with_audio_clock_conditioning')
+ # The final latents must be durable before exiting the weight
+ # offloader: its cleanup can fail after all NFE have completed.
+ if sampling_complete_callback is not None:
+ tick = time.perf_counter()
+ sampling_complete_callback(latents, audio, sampled)
+ checkpoint_seconds = time.perf_counter() - tick
+ finally:
+ self.transformer._freevideo_refined_text = None
+ torch.cuda.synchronize()
+ elapsed = time.perf_counter() - started - checkpoint_seconds
+ torch.cuda.empty_cache()
+ sampled.update(sample_seconds=elapsed, torch_peak_allocated_bytes=torch.cuda.max_memory_allocated(),
+ torch_peak_reserved_bytes=torch.cuda.max_memory_reserved())
+ return latents, audio, sampled
+
+ @torch.no_grad()
+ def _prepare_refined_text(self, prompt):
+ """The upstream text refiner has no timestep or video input.
+
+ Compute it once for this request and discard its 0.77 GB of weights.
+ Restore only those tensors from the immutable cache for a later prompt.
+ The cached tensor is cleared even when sampling fails.
+ """
+ from .fp8 import input_dtype
+ started = time.perf_counter()
+ refiner = self.transformer.token_refiner.refiner_blocks
+ cache, cache_key, note = None, None, None
+ if self.input_cache_dir:
+ from .input_cache import InputCache, digest
+ from .storage import fingerprint
+ try:
+ implementation = Path(sys.modules[type(self.transformer).__module__].__file__)
+ identity = dict(manifest=digest(self.cache / 'manifest.json'), weights=fingerprint(self.cache / 'root.safetensors'),
+ engine=digest(Path(__file__)), upstream=digest(implementation), torch=str(torch.__version__),
+ arithmetic={k: self.config[k] for k in ('attention', 'linear_compute', 'fp8_gemm', 'inference_kernels', 'head_chunk', 'projection_chunk')},
+ cuda=torch.version.cuda, gpu=torch.cuda.get_device_name(),
+ tf32=torch.backends.cuda.matmul.allow_tf32,
+ bf16_reduced_reduction=torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction,
+ deterministic=torch.are_deterministic_algorithms_enabled())
+ cache = InputCache(identity, root=self.input_cache_dir, kind='refined-text')
+ cpu = prompt.detach().to('cpu').contiguous()
+ cache_key = str(cpu.dtype) + str(list(cpu.shape)) + hashlib.sha256(cpu.view(torch.uint8).numpy().tobytes()).hexdigest()
+ del cpu
+ found = cache.lookup(cache_key)
+ if found:
+ path, entry = found
+ text = torch.load(path, map_location='cuda', weights_only=True)
+ expected_shape = [1, prompt.shape[-2], self.transformer.context_embedder.out_features]
+ if (isinstance(text, torch.Tensor) and list(text.shape) == expected_shape
+ and str(text.dtype) == entry['metrics'].get('dtype') and bool(torch.isfinite(text).all())):
+ if not self.stream_refiner_count:
+ refiner.to('meta')
+ self.refiner_released = True
+ self.transformer._freevideo_refined_text = text
+ return dict(seconds=time.perf_counter() - started, shape=list(text.shape), cache_hit=True,
+ refiner_weights_released=True, cache_receipt=str(cache.receipt(cache_key)))
+ del text
+ except (OSError, ValueError, KeyError, RuntimeError) as error:
+ note = str(error)
+ if self.refiner_released and not self.stream_refiner_count:
+ prefix = 'token_refiner.refiner_blocks.'
+ values = {name.removeprefix(prefix): tensor for name, tensor in
+ _load_safetensors(self.cache / 'root.safetensors').items() if name.startswith(prefix)}
+ refiner.load_state_dict(values, strict=True, assign=True)
+ del values
+ # load_prompt returns [tokens, width]; generate_latents adds the batch
+ # dimension before the original transformer call.
+ prompt = prompt.unsqueeze(0) if prompt.ndim == 2 else prompt
+ if prompt.ndim != 3 or prompt.shape[0] != 1:
+ raise ValueError('Expected one text prompt for refinement')
+ text = self.transformer.context_embedder(prompt.to(input_dtype(self.transformer.context_embedder)))
+ with LayerOffloader(refiner, prefetch=False,
+ weight_source=self.stream_weight_source if self.stream_refiner_count else None) as loader:
+ text = self.transformer.token_refiner(text)
+ stats = loader.stats()
+ del loader
+ torch.cuda.synchronize()
+ if not self.stream_refiner_count:
+ refiner.to('meta')
+ self.refiner_released = True
+ self.transformer._freevideo_refined_text = text
+ saved = False
+ if cache is not None and cache_key:
+ try:
+ import uuid
+ cache.root.mkdir(parents=True, exist_ok=True)
+ temporary = cache.root / ('refined-' + uuid.uuid4().hex + '.partial')
+ torch.save(text.detach().cpu(), temporary)
+ saved = cache.put(cache_key, temporary, dict(success=True, shape=list(text.shape), dtype=str(text.dtype)), move=True)
+ note = cache.note or note
+ except (OSError, ValueError) as error:
+ note = str(error)
+ gc.collect()
+ torch.cuda.empty_cache()
+ empty_host_cache(torch)
+ return {'seconds': time.perf_counter() - started, 'shape': list(text.shape),
+ 'refiner_weights_released': True, 'offload': stats, 'cache_hit': False, 'cache_saved': saved, 'cache_note': note}
+
+ def close(self):
+ if self.closed:
+ return
+ if self.schedule_hook is not None:
+ self.schedule_hook.remove()
+ self.transformer = None
+ self.offload_layers = []
+ self.stream_weight_source = None
+ self.attention = None
+ self.closed = True
+ gc.collect()
+ torch.cuda.empty_cache()
+ empty_host_cache(torch)
diff --git a/freevideo_engine/sampling_memory.py b/freevideo_engine/sampling_memory.py
new file mode 100644
index 0000000..1729131
--- /dev/null
+++ b/freevideo_engine/sampling_memory.py
@@ -0,0 +1,210 @@
+"""Bounded per-step allocator and WDDM observations; no policy changes."""
+import copy
+import os
+import threading
+import time
+
+from .monitoring import save
+from . import compilation_diagnostics as compilation
+
+
+class SamplingMemory:
+ def __init__(self, torch, path=None, *, reader_factory=None, interval=.5):
+ self.torch, self.path, self.interval = torch, path, interval
+ self.lock, self.stop_event = threading.Lock(), threading.Event()
+ self.thread = self.reader = None
+ self.started = time.monotonic()
+ self.index = 1
+ self.rows, self.windows, self.errors = [], {}, []
+ self.status = 'not-applicable'
+ self.identity = None
+ self.previous_process = None
+ self.previous_compiler = compilation.snapshot()
+ if reader_factory is None and os.name == 'nt':
+ from .windows_gpu_memory import AdapterMemory
+ reader_factory = AdapterMemory
+ if reader_factory is not None:
+ try:
+ self.reader = reader_factory()
+ self.identity = self.reader.identity
+ self.status = 'observing'
+ except Exception as error:
+ self.status = 'unavailable'
+ self.errors.append(str(error))
+
+ def start(self):
+ self.previous_process = self.process_counters()
+ self.poll()
+ if self.reader is not None:
+ self.thread = threading.Thread(target=self.loop, name='FreeVideo WDDM diagnostics', daemon=True)
+ self.thread.start()
+ self.persist()
+ return self
+
+ def loop(self):
+ try:
+ while not self.stop_event.wait(self.interval):
+ self.poll()
+ self.persist()
+ finally:
+ self.release_reader()
+
+ def release_reader(self):
+ reader, self.reader = self.reader, None
+ if reader is not None:
+ try:
+ reader.close()
+ except Exception as error:
+ with self.lock:
+ self.errors.append(str(error))
+
+ def poll(self):
+ if self.reader is None:
+ return
+ index = self.index
+ try:
+ value = self.reader.sample()
+ for name in ('local', 'nonlocal'):
+ sample = value[name]
+ if any(type(sample.get(k)) is not int or sample[k] < 0 for k in ('usage_bytes', 'budget_bytes')):
+ raise ValueError('Invalid WDDM counters')
+ if value['local']['budget_bytes'] == 0:
+ raise ValueError('WDDM did not report a usable local video-memory budget')
+ except Exception as error:
+ with self.lock:
+ if len(self.errors) < 8 and str(error) not in self.errors:
+ self.errors.append(str(error))
+ return
+ with self.lock:
+ if index > 256:
+ return
+ row = self.windows.setdefault(index, dict(samples=0, first_elapsed_seconds=time.monotonic()-self.started))
+ row['samples'] += 1
+ row['last_elapsed_seconds'] = time.monotonic() - self.started
+ for name in ('local', 'nonlocal'):
+ sample = value[name]
+ old = row.setdefault(name, dict(peak_usage_bytes=sample['usage_bytes'],
+ first_budget_bytes=sample['budget_bytes'], maximum_budget_bytes=sample['budget_bytes'],
+ last_budget_bytes=sample['budget_bytes'], budget_changes=0,
+ minimum_budget_bytes=sample['budget_bytes'], over_budget_samples=0))
+ old['peak_usage_bytes'] = max(old['peak_usage_bytes'], sample['usage_bytes'])
+ old['minimum_budget_bytes'] = min(old['minimum_budget_bytes'], sample['budget_bytes'])
+ old['maximum_budget_bytes'] = max(old['maximum_budget_bytes'], sample['budget_bytes'])
+ old['budget_changes'] += old['last_budget_bytes'] != sample['budget_bytes']
+ old['last_budget_bytes'] = sample['budget_bytes']
+ old['last_usage_bytes'] = sample['usage_bytes']
+ old['over_budget_samples'] += sample['usage_bytes'] > sample['budget_bytes']
+
+ def complete(self, seconds):
+ row = dict(step=self.index, seconds=seconds, elapsed_seconds=time.monotonic()-self.started)
+ current_compiler = compilation.snapshot()
+ row['compiler'] = compilation.difference(self.previous_compiler, current_compiler)
+ self.previous_compiler = current_compiler
+ row['host'] = self.host_counters()
+ current = self.process_counters()
+ row['process_delta'] = {key: value-self.previous_process[key] for key, value in current.items()
+ if self.previous_process is not None and key in self.previous_process
+ and value >= self.previous_process[key]}
+ self.previous_process = current
+ try:
+ cuda = self.torch.cuda
+ stats = cuda.memory_stats()
+ row.update(allocated_bytes=cuda.memory_allocated(), reserved_bytes=cuda.memory_reserved(),
+ cumulative_peak_allocated_bytes=cuda.max_memory_allocated(),
+ cumulative_peak_reserved_bytes=cuda.max_memory_reserved(),
+ inactive_split_bytes=stats.get('inactive_split_bytes.all.current'),
+ allocation_retries=stats.get('num_alloc_retries'), allocator_ooms=stats.get('num_ooms'))
+ except Exception as error:
+ row['allocator_error'] = str(error)
+ with self.lock:
+ if len(self.rows) < 256:
+ self.rows.append(row)
+ self.index += 1
+ self.persist()
+
+ @staticmethod
+ def process_counters():
+ # Read only at step boundaries, not on the kernel/attention hot path.
+ # CPU time can exceed elapsed time when several host threads work.
+ result = {}
+ try:
+ import psutil
+ process = psutil.Process(os.getpid())
+ value = process.cpu_times()
+ result.update(cpu_user_seconds=value.user, cpu_system_seconds=value.system)
+ value = process.io_counters()
+ result.update(read_bytes=value.read_bytes, write_bytes=value.write_bytes)
+ except Exception:
+ # Includes unavailable psutil counters; keep any earlier readings.
+ pass
+ return result
+
+ def host_counters(self):
+ result = {}
+ try:
+ stats = self.torch.cuda.memory.host_memory_stats()
+ for target, source in (('pinned_allocated_bytes', 'allocated_bytes.current'),
+ ('pinned_active_bytes', 'active_bytes.current')):
+ if type(stats.get(source)) is int and stats[source] >= 0:
+ result[target] = stats[source]
+ if ('pinned_allocated_bytes' in result and 'pinned_active_bytes' in result
+ and result['pinned_allocated_bytes'] >= result['pinned_active_bytes']):
+ result['pinned_cached_bytes'] = result['pinned_allocated_bytes'] - result['pinned_active_bytes']
+ except Exception:
+ pass # Optional on older/CPU wheels. Absence is not zero pins.
+ try:
+ from .system import system_memory
+ memory = system_memory()
+ for key in ('physical_available_bytes', 'commit_available_bytes'):
+ if type(memory.get(key)) is int and memory[key] >= 0:
+ result[key] = memory[key]
+ except Exception:
+ pass
+ for key, getter in (('torch_threads', 'get_num_threads'), ('torch_interop_threads', 'get_num_interop_threads')):
+ try:
+ result[key] = getattr(self.torch, getter)()
+ except Exception:
+ pass
+ return result
+
+ def result(self):
+ with self.lock:
+ rows = [dict(row, windows=copy.deepcopy(self.windows.get(row['step']))) for row in self.rows]
+ return dict(steps=rows, current_step=self.index,
+ incomplete_step_windows=copy.deepcopy(self.windows.get(self.index)),
+ incomplete_step_compiler=compilation.difference(self.previous_compiler, compilation.snapshot()),
+ compiler_scope=compilation.SCOPE,
+ windows_status=self.status, windows_identity=self.identity, errors=list(self.errors),
+ windows_sample_interval_seconds=self.interval, retained_step_limit=256,
+ scope='Allocator peaks are cumulative since sampling reset; current counters are at completed step boundaries. '
+ 'Allocation retry/OOM counters cover the process lifetime, not individual steps. '
+ 'DXGI local/nonlocal values describe this CUDA process on its LUID-matched adapter. '
+ 'Nonlocal usage alone is not proof of CUDA spill or PCIe paging traffic. '
+ 'Host counters are step-boundary observations, not peaks. Pinned allocated bytes include '
+ 'active and allocator-cached rounded blocks across this process, not just model weights. '
+ 'Process I/O deltas are OS counters; they do not measure mmap hard faults or PCIe transfers. '
+ 'These diagnostic observations do not change memory policy.')
+
+ def persist(self):
+ if self.path is not None:
+ try:
+ save(self.path, self.result())
+ except Exception as error:
+ with self.lock:
+ if len(self.errors) < 8 and str(error) not in self.errors:
+ self.errors.append(str(error))
+
+ def close(self):
+ self.stop_event.set()
+ if self.thread is not None:
+ self.thread.join(timeout=2)
+ if self.thread.is_alive():
+ result = self.result()
+ result['windows_status'] = 'reader-still-stopping'
+ result['errors'].append('WDDM query did not finish')
+ return result
+ if self.status == 'observing':
+ self.release_reader()
+ self.status = ('partial' if self.errors else 'complete') if self.windows else 'unavailable'
+ self.persist()
+ return self.result()
diff --git a/freevideo_engine/sampling_progress.py b/freevideo_engine/sampling_progress.py
new file mode 100644
index 0000000..71200f5
--- /dev/null
+++ b/freevideo_engine/sampling_progress.py
@@ -0,0 +1,183 @@
+"""Sampling telemetry without tensor reads, CUDA synchronization or Torch imports.
+
+Layers report where the host is dispatching work. Only the sampler's existing
+synchronized step timer can mark a step complete; layer events are not GPU
+completion measurements.
+"""
+from contextlib import contextmanager
+import json
+import time
+
+
+def emit_event(event):
+ print(json.dumps(event), flush=True)
+
+
+def _bounded(value, low=0., high=1.):
+ """Return a finite progress fraction without ever claiming completion."""
+ try:
+ value = float(value)
+ except (TypeError, ValueError):
+ return low
+ if value != value: # NaN, without importing math in the hot path.
+ return low
+ return min(high, max(low, value))
+
+
+class SamplingProgress:
+ def __init__(self, steps, blocks, emit=emit_event, clock=time.perf_counter, *, offset=0, total=None):
+ self.steps, self.blocks = steps, blocks
+ self.offset, self.total = offset, steps if total is None else total
+ if not 0 <= offset < self.total or offset + steps > self.total:
+ raise ValueError('Invalid sampling progress range')
+ self.emit, self.clock = emit, clock
+ self.started = clock()
+ self.step_started = self.started
+ self.completed = []
+ self.last_layer_event = float('-inf')
+
+ def _step_estimate(self):
+ # The first step includes input preparation and kernel compilation. A
+ # single completed warm step is enough for visual interpolation inside
+ # the next step; remaining-time forecasts still require two warm
+ # measurements below to avoid presenting a noisy ETA.
+ warm = self.completed[1:]
+ return sum(warm) / len(warm) if warm else None
+
+ def _visual_step_estimate(self):
+ """Return the earliest useful step estimate for UI interpolation.
+
+ The first NFE includes setup and compilation, so it is excluded from
+ the authoritative ETA. It is still a useful temporary ruler for the
+ progress animation until the first warm NFE is available; later real
+ measurements replace it automatically.
+ """
+ estimate = self._step_estimate()
+ if estimate is not None:
+ return estimate
+ return self.completed[-1] if self.completed else None
+
+ def event(self, name, **fields):
+ now = self.clock()
+ if 'step' in fields:
+ fields['step'] += self.offset
+ self.emit(dict(event=name, total=self.total, completed_steps=self.offset + len(self.completed),
+ uniform_remaining_steps=self.offset + self.steps == self.total,
+ elapsed_seconds=now-self.started,
+ step_elapsed_seconds=now-self.step_started, **fields))
+
+ def start(self):
+ self.event('sampling_start', stage='inputs', step=1)
+
+ def layer(self, index):
+ if len(self.completed) >= self.steps:
+ return
+ now = self.clock()
+ if index == 1 or now-self.last_layer_event >= 1.:
+ self.last_layer_event = now
+ estimate = self._visual_step_estimate()
+ # A layer event is emitted before that layer runs. Once the next
+ # layer is entered, the elapsed time covers the preceding layers;
+ # use that fraction as an early, continuously corrected ruler.
+ if estimate is None and index > 1 and self.blocks > 0:
+ elapsed = now - self.step_started
+ fraction = (index - 1) / self.blocks
+ if elapsed > 0 and fraction > 0:
+ estimate = elapsed / fraction
+ self.event('sampling_progress', stage='layers', step=len(self.completed)+1,
+ block=index, blocks=self.blocks,
+ estimated_step_seconds=estimate)
+
+ def complete(self, seconds):
+ self.completed.append(seconds)
+ # Exclude the first step's initialization/compilation. Use at least two
+ # subsequent measurements, and label this as sampling-only in the UI.
+ warm = self.completed[1:]
+ remaining = ((self.steps-len(self.completed))*sum(warm)/len(warm)
+ if len(warm) >= 2 and self.offset + self.steps == self.total else None)
+ self.event('step', step=len(self.completed), seconds=seconds,
+ remaining_seconds=remaining,
+ estimated_step_seconds=self._visual_step_estimate(),
+ # The next step starts after this event is emitted. The
+ # browser uses this zero to interpolate smoothly from the
+ # completed NFE instead of carrying the previous step's
+ # elapsed time into the next one.
+ next_step_elapsed_seconds=0.)
+ self.step_started = self.clock()
+
+ @contextmanager
+ def layers(self, blocks):
+ handles = []
+ try:
+ for index, block in enumerate(blocks, 1):
+ def entered(module, inputs, index=index):
+ self.layer(index)
+ handles.append(block.register_forward_pre_hook(entered))
+ yield
+ finally:
+ for handle in handles:
+ handle.remove()
+
+
+def progress_message(event):
+ """One sampling event contract for ComfyUI and the terminal log reader."""
+ name = event.get('event')
+ if name == 'sample_finalize':
+ labels = {'latent_validation': 'Checking completed sampling',
+ 'latent_save': 'Saving completed sampling',
+ 'offload_release': 'Releasing sampling buffers',
+ 'transformer_release': 'Preparing video decoding'}
+ phase = event.get('phase')
+ if phase in labels:
+ return dict(label=labels[phase], phase='sample_finalize', stage=phase,
+ done=0, total=0)
+ return None
+ if name not in ('sampling_start', 'sampling_progress', 'step', 'loaded'):
+ return None
+ total = event.get('total', 8)
+ done = event.get('step', 0) if name == 'step' else event.get('completed_steps', 0)
+ if type(total) is not int or total < 1 or type(done) is not int or not 0 <= done <= total:
+ return None
+ result = dict(label='Sampling %d / %d' % (done, total), phase='sampling',
+ done=done, total=total, unit='steps', stage=event.get('stage', 'starting'),
+ uniform_remaining_steps=event.get('uniform_remaining_steps', True))
+ # `done` remains the count of steps whose synchronized work completed. A
+ # layer event is only dispatch progress, so expose it separately as an
+ # explicitly estimated fraction. The cap leaves a visible gap before the
+ # real step event arrives and prevents a UI from claiming completion early.
+ display_fraction = (done / total) if total else 0.
+ estimated = False
+ if name == 'sampling_start':
+ result['detail'] = 'Preparing sampling inputs'
+ result['reset'] = done == 0
+ elif name == 'sampling_progress':
+ block, blocks = event.get('block'), event.get('blocks')
+ if type(block) is int and type(blocks) is int and 1 <= block <= blocks:
+ local = _bounded((block - 1) / max(1, blocks), high=.88)
+ display_fraction = _bounded((done + local) / total, high=1.)
+ estimated = True
+ result.update(block=block, blocks=blocks,
+ detail='Step %d · processing layer %d / %d' % (done+1, block, blocks))
+ elif name == 'step':
+ result.update(stage='complete' if done == total else 'between_steps',
+ detail='Sampling complete · preparing output' if done == total else
+ 'Step %d complete' % done)
+ result['last_step_seconds'] = event.get('seconds')
+ for key in ('elapsed_seconds', 'step_elapsed_seconds', 'remaining_seconds'):
+ if key in event:
+ result[key] = event[key]
+ if name == 'step':
+ result['step_elapsed_seconds'] = event.get('next_step_elapsed_seconds', 0.)
+ if event.get('estimated_step_seconds') is not None:
+ result['estimated_step_seconds'] = event['estimated_step_seconds']
+ result['display_fraction'] = display_fraction
+ result['estimated'] = estimated
+ result['display_percent'] = round(100 * display_fraction, 1)
+ if name in ('sampling_progress', 'step'):
+ prefix = '~' if estimated else ''
+ result['label'] = 'Sampling %d / %d · %s%.0f%%' % (done, total, prefix, display_fraction * 100)
+ # This is a capability/explanation, not a claim that a compilation is active
+ # or that every new shape can reuse an existing compiled artifact.
+ if done == 0:
+ result['kernel_cache_note'] = True
+ return result
diff --git a/freevideo_engine/setup_progress.py b/freevideo_engine/setup_progress.py
new file mode 100644
index 0000000..cab06f5
--- /dev/null
+++ b/freevideo_engine/setup_progress.py
@@ -0,0 +1,88 @@
+"""Retain overall stages and concurrent transfer progress independently of logs."""
+import math
+import time
+
+from .terminal_ui import duration
+
+
+def number(value):
+ return (not isinstance(value, bool) and isinstance(value, (int, float))
+ and math.isfinite(value) and value >= 0)
+
+
+class ProgressState:
+ def __init__(self):
+ self.phase = {}
+ self.tasks = {}
+ self.last = {}
+ self.model_groups = []
+
+ def update(self, event, now=None):
+ now = time.monotonic() if now is None else now
+ event = dict(event)
+ if event.get('kind') == 'models':
+ self.model_groups = event.get('groups', [])
+ return
+ if event.get('kind') == 'phase':
+ self.phase = event
+ return
+ key = str(event.get('key', 'current'))
+ if event.get('kind') == 'task':
+ self.tasks.pop(key, None)
+ previous = self.tasks.get(key, {})
+ state = event.get('state', 'running')
+ self.tasks[key] = dict(previous, **event, observed_at=now)
+ self.tasks[key]['state'] = state
+ self.last = self.tasks[key]
+ # Completed events need not grow with the number of downloaded files.
+ for name in list(self.tasks):
+ if len(self.tasks) <= 32:
+ break
+ if self.tasks[name].get('state') != 'running':
+ self.tasks.pop(name)
+
+ def snapshot(self, now=None):
+ now = time.monotonic() if now is None else now
+ active = [self._view(row, now) for row in self.tasks.values() if row.get('state') == 'running']
+ # Keep a download visible while a parallel compiler reports a heartbeat.
+ active.sort(key=lambda row: (number(row.get('total')) and row['total'] > 0,
+ row.get('unit') == 'bytes'), reverse=True)
+ current = active[0] if active else self._view(self.last, now)
+ return dict(phase_progress=dict(self.phase), active_tasks=active, progress=current, model_groups=self.model_groups)
+
+ @staticmethod
+ def _view(row, now):
+ value = dict(row)
+ age = max(0., now - value.pop('observed_at', now))
+ total, done = value.get('total'), value.get('done')
+ valid = number(total) and total > 0 and number(done) and done <= total
+ value['fraction'] = done / total if valid else None
+ remaining = value.get('remaining_seconds')
+ if not valid or age > 10 or not number(remaining):
+ value['remaining_seconds'] = None
+ value['age_seconds'] = age
+ return value
+
+
+def progress_text(event, zh=False):
+ """A truthful count/ETA for the current file or operation, never stage averages."""
+ t = lambda en, cn: cn if zh else en
+ total, done = event.get('total'), event.get('done')
+ valid = number(total) and total > 0 and number(done) and done <= total
+ parts = []
+ if valid:
+ parts.append('%.0f%%' % (100 * done / total))
+ if event.get('unit') == 'bytes':
+ unit, size = ('GiB', 2**30) if total >= 2**30 else ('MiB', 2**20)
+ parts.append('%.1f / %.1f %s' % (done / size, total / size, unit))
+ else:
+ parts.append('%d / %d' % (done, total))
+ elif event.get('state') == 'complete':
+ parts.append(t('Complete', '已完成'))
+ else:
+ parts.append(t('Measuring progress…', '正在获取进度…'))
+ remaining = event.get('remaining_seconds')
+ if valid and number(remaining) and remaining > 0:
+ parts.append(t('File ETA ~', '本文件预计剩余约 ') if event.get('unit') == 'bytes' else t('ETA ~', '预计剩余约 '))
+ parts[-1] += duration(math.ceil(remaining))
+ return ' · '.join(parts)
diff --git a/freevideo_engine/storage.py b/freevideo_engine/storage.py
new file mode 100644
index 0000000..2358eb1
--- /dev/null
+++ b/freevideo_engine/storage.py
@@ -0,0 +1,133 @@
+"""Compact installation storage; never remove borrowed models or user outputs."""
+import json
+import os
+from pathlib import Path
+import stat
+import time
+
+from .monitoring import save
+
+
+def conversion_source(row):
+ name = row['file']
+ return name.endswith('.safetensors') and name.startswith(('h3-base/transformer/', 'stage-dmd-step-250/'))
+
+
+def fingerprint(path):
+ info = path.stat()
+ result = dict(bytes=info.st_size, mtime_ns=info.st_mtime_ns, ctime_ns=info.st_ctime_ns,
+ inode=info.st_ino, device=info.st_dev)
+ if os.name == 'nt':
+ from .win32 import file_change_time_ns
+ result['change_time_ns'] = file_change_time_ns(path)
+ return result
+
+
+def managed_model(root, path):
+ """Only regular, unshared files beneath this installation's models directory."""
+ root, path = Path(root).resolve(), Path(path).absolute()
+ try:
+ path.relative_to(root / 'models')
+ if path.resolve() != path:
+ return False
+ current = path
+ while current != root:
+ info = current.lstat()
+ if stat.S_ISLNK(info.st_mode) or getattr(info, 'st_file_attributes', 0) & 0x400:
+ return False
+ current = current.parent
+ info = path.stat()
+ return stat.S_ISREG(info.st_mode) and info.st_nlink == 1
+ except (OSError, ValueError):
+ return False
+
+
+def ownership(root):
+ path = Path(root) / '.freevideo' / 'downloaded-models.json'
+ if not path.exists():
+ return path, {}
+ value = json.loads(path.read_text(encoding='utf-8'))
+ if not isinstance(value, dict) or value.get('root') != str(Path(root).resolve()) or not isinstance(value.get('files'), dict):
+ raise ValueError('Invalid model ownership record; models retained')
+ return path, value['files']
+
+
+def record_download(root, path, row, *, origin='download'):
+ # Called only after a previously absent target was downloaded/copied and verified.
+ if not conversion_source(row) or not managed_model(root, path):
+ return
+ ledger, files = ownership(root)
+ files[str(Path(path).absolute())] = dict(fingerprint(path), sha256=row.get('sha256'),
+ git_blob=row.get('git_blob'), source_file=row['file'], origin=origin)
+ save(ledger, {'root': str(Path(root).resolve()), 'files': files})
+
+
+def compact_sources(plan, rows, report):
+ """Caller must first verify the complete FP8 cache and pass GPU setup probes.
+
+ Deletion uses an allowlist of pinned conversion-only filenames and a matching
+ download receipt. External directories, preexisting/shared/replaced files,
+ decoder/encoder weights, configs, failed attempts and outputs are retained.
+ """
+ from .network import hash_file
+ root, model_dir = Path(plan['root']).resolve(), Path(plan['model_dir']).resolve()
+ result = {'mode': plan.get('storage', 'compact'), 'deleted': [], 'retained': [],
+ 'released_bytes': 0, 'started_epoch': time.time()}
+ if result['mode'] != 'compact':
+ save(report, result)
+ return result
+ try:
+ ledger, files = ownership(root)
+ except (OSError, ValueError, TypeError) as error:
+ result['retained'].append({'reason': str(error)})
+ save(report, result)
+ return result
+ # Record intent before modifying any file; update after each successful unlink.
+ save(report, result)
+ for row in rows:
+ if not conversion_source(row):
+ continue
+ path = model_dir / row['file']
+ if not path.exists():
+ continue
+ expected = files.get(str(path), {})
+ current = fingerprint(path)
+ if (not isinstance(expected, dict) or not managed_model(root, path)
+ or 'change_time_ns' in current and current['change_time_ns'] is None
+ or expected.get('source_file') != row['file'] or expected.get('bytes') != row['bytes']
+ or expected.get('sha256') != row.get('sha256') or expected.get('git_blob') != row.get('git_blob')
+ or any(expected.get(k) != v for k, v in current.items())):
+ result['retained'].append({'file': str(path), 'reason': 'Not an unchanged, unshared file downloaded by this installation'})
+ continue
+ try:
+ # Destructive cleanup cannot trust a metadata cache hit. Equal-size
+ # edits may share a filesystem timestamp tick with the receipt.
+ if row.get('sha256'):
+ matches = hash_file(path, discard_cache=True) == row['sha256']
+ elif row.get('git_blob'):
+ matches = hash_file(path, 'sha1', git_blob=True, discard_cache=True) == row['git_blob']
+ else:
+ raise OSError('No pinned content hash; source retained')
+ if not matches:
+ raise OSError('Source content changed; retained without deletion')
+ result['pending'] = {'file': str(path), 'bytes': row['bytes']}
+ save(report, result)
+ # Hashing can take time for model shards. Recheck both identity and
+ # sharing immediately before unlink, after saving deletion intent.
+ if fingerprint(path) != current or not managed_model(root, path):
+ raise OSError('File changed or became shared during cleanup')
+ path.unlink()
+ except OSError as error:
+ result['retained'].append({'file': str(path), 'reason': str(error)})
+ result['pending'] = None
+ save(report, result)
+ continue
+ result['pending'] = None
+ result['deleted'].append({'file': str(path), 'bytes': row['bytes'], 'source_file': row['file']})
+ result['released_bytes'] += row['bytes']
+ files.pop(str(path), None)
+ save(report, result)
+ save(ledger, {'root': str(root), 'files': files})
+ result['completed_epoch'] = time.time()
+ save(report, result)
+ return result
diff --git a/freevideo_engine/streamed_weights.py b/freevideo_engine/streamed_weights.py
new file mode 100644
index 0000000..5814b27
--- /dev/null
+++ b/freevideo_engine/streamed_weights.py
@@ -0,0 +1,427 @@
+"""Reopen immutable safetensor layer views only while filling an offload slot."""
+from contextlib import contextmanager
+from pathlib import Path
+
+
+_TORCH_DTYPES = {'BOOL': 'bool', 'U8': 'uint8', 'I8': 'int8', 'I16': 'int16',
+ 'I32': 'int32', 'I64': 'int64', 'F16': 'float16', 'BF16': 'bfloat16',
+ 'F32': 'float32', 'F64': 'float64', 'F8_E4M3': 'float8_e4m3fn',
+ 'F8_E5M2': 'float8_e5m2'}
+
+
+_IDENTITY_FIELDS = ('st_dev', 'st_ino', 'st_size', 'st_mtime_ns', 'st_ctime_ns')
+
+
+def _file_identity(value):
+ return tuple(getattr(value, name) for name in _IDENTITY_FIELDS)
+
+
+class CheckpointChangedError(ValueError):
+ def __init__(self, path, source, expected, observed):
+ self.checkpoint_change = dict(source=source, fields=[name for name, before, after
+ in zip(_IDENTITY_FIELDS, expected, observed) if before != after])
+ super().__init__('Checkpoint changed during streamed inference: ' + str(path))
+
+
+def _open_safetensors(path, *, framework, device='cpu'):
+ """Open a streamed shard without reserving the whole file on Windows.
+
+ The mmap backend is excellent on Linux, where clean mapped pages are
+ reclaimable and do not consume commit. Windows charges the section views
+ against the system commit even when their pages are clean. Reopening a
+ multi-hundred-MiB shard for every transformer block can therefore consume
+ the pagefile while the process's working set stays small. safetensors
+ 0.8's ``pread`` backend reads only the requested tensor bytes and avoids
+ that commit reservation. Runtime dependencies pin this version, so do not
+ silently fall back to mmap on Windows.
+ """
+ from safetensors import safe_open
+ from .system import windows
+ kwargs = {'framework': framework, 'device': device}
+ if windows():
+ kwargs['backend'] = 'pread'
+ return safe_open(str(path), **kwargs)
+
+
+def interleaved_layer_order(count):
+ """Spread any affordable prefix of pinned layers across the computation.
+
+ A long cached prefix fills the bounded read-ahead window and then leaves
+ the disk idle. Interleaving retained layers gives the reader useful work
+ throughout a step without increasing its buffers or changing layer order.
+ Longest-gap bisection also spreads partial allocations when live RAM or
+ the platform's pinning limit is lower than the plan.
+ """
+ import heapq
+ if type(count) is not int or count < 0:
+ raise ValueError('Layer count must be a non-negative integer')
+ pending = [(-count, 0, count)] if count else []
+ order = []
+ while pending:
+ _, start, end = heapq.heappop(pending)
+ middle = (start + end) // 2
+ order.append(middle)
+ for lo, hi in ((start, middle), (middle + 1, end)):
+ if lo < hi:
+ heapq.heappush(pending, (lo - hi, lo, hi))
+ return order
+
+
+def release_file_pages(paths):
+ """Advise away consumed clean pages; never remove or rewrite a weight file."""
+ import os
+ advised, errors = 0, 0
+ if not hasattr(os, 'posix_fadvise'):
+ return advised, errors
+ for path in paths:
+ try:
+ with path.open('rb', buffering=0) as file:
+ os.posix_fadvise(file.fileno(), 0, 0, os.POSIX_FADV_DONTNEED)
+ advised += path.stat().st_size
+ except OSError:
+ # Advisory cache management is optional. The normal tensor reader
+ # still validates the immutable file before and after every use.
+ errors += 1
+ return advised, errors
+
+
+def read_ahead_budget():
+ """Bound clean file-cache lookahead by live RAM, without a pinned copy."""
+ from .system import system_memory, windows
+ from .hardware import cgroup_memory
+ memory = system_memory()
+ available = memory['available_bytes']
+ # Windows reports physical and commit headroom separately. File-cache
+ # read-ahead is deliberately pageable, but it still must not run when the
+ # pagefile/commit is already the tighter bound; otherwise a speculative
+ # read can recreate the same pressure that streamed pread was introduced
+ # to avoid.
+ # Commit is a meaningful second capacity on Windows: section views and
+ # pageable staging can consume the pagefile even when physical RAM looks
+ # available. Linux's MemAvailable already accounts for reclaimable file
+ # cache; do not import the Windows commit ceiling into its read-ahead
+ # budget.
+ if windows():
+ commit = memory.get('commit_available_bytes')
+ if type(commit) is int and commit >= 0:
+ available = min(available, commit)
+ group = cgroup_memory()
+ if not group.get('complete'):
+ return 0
+ limited = group.get('reclaimable_available_bytes')
+ if limited is not None:
+ available = min(available, limited)
+ # Current-layer views, a small read buffer and changing application usage
+ # also need space. These are clean reclaimable pages, not retained tensors.
+ return max(0, min(1536 * 2**20, available - 512 * 2**20))
+
+
+class LayerReadAhead:
+ """One cancellable reader warms a byte-bounded window of immutable files.
+
+ Only file cache retains the data. The reader owns one 4 MiB scratch buffer,
+ never a tensor, a pinned allocation or a GPU slot. Submission advances as
+ the inference thread consumes layers; it cannot race through the model.
+ """
+ def __init__(self, plans, check, layer_count, budget=read_ahead_budget):
+ from concurrent.futures import ThreadPoolExecutor
+ from threading import Event, Lock
+ self.plans = sorted((index, tuple(paths), sum(path.stat().st_size for path in paths))
+ for index, paths in plans.items())
+ self.check = check
+ self.paths_by_index = {index: paths for index, paths, _ in self.plans}
+ self.layer_count = layer_count
+ self.budget = budget
+ self.cursor = 0
+ self.pending = {}
+ self.pending_bytes = 0
+ self.stop = Event()
+ self.lock = Lock()
+ self.executor = ThreadPoolExecutor(max_workers=1, thread_name_prefix='freevideo-read-ahead')
+ self.closed = False
+ self.failed = None
+ self.last_read_finished = None
+ self.counters = dict(read_bytes=0, read_seconds=0., wait_seconds=0.,
+ reader_idle_seconds=0.,
+ files_read=0, peak_window_bytes=0, skipped_layers=0,
+ discard_advised_bytes=0, discard_errors=0)
+ self._fill()
+
+ def _read(self, paths):
+ import time
+ started = time.monotonic()
+ with self.lock:
+ if self.last_read_finished is not None:
+ self.counters['reader_idle_seconds'] += max(0., started - self.last_read_finished)
+ scratch = bytearray(4 * 2**20)
+ read_bytes = 0
+ files = 0
+ try:
+ for path in paths:
+ if self.stop.is_set():
+ break
+ self.check(path)
+ complete = False
+ with path.open('rb', buffering=0) as file:
+ while not self.stop.is_set():
+ count = file.readinto(scratch)
+ if not count:
+ complete = True
+ break
+ read_bytes += count
+ self.check(path)
+ files += int(complete)
+ finally:
+ with self.lock:
+ self.counters['read_bytes'] += read_bytes
+ self.counters['read_seconds'] += time.monotonic() - started
+ self.counters['files_read'] += files
+ self.last_read_finished = time.monotonic()
+
+ def _fill(self):
+ if self.closed or self.failed:
+ return
+ maximum = max(0, int(self.budget()))
+ while self.cursor < len(self.plans):
+ index, paths, size = self.plans[self.cursor]
+ if size > maximum:
+ self.cursor += 1
+ self.counters['skipped_layers'] += 1
+ continue
+ if self.pending_bytes + size > maximum:
+ break
+ self.pending[index] = (size, self.executor.submit(self._read, paths))
+ self.pending_bytes += size
+ self.counters['peak_window_bytes'] = max(self.counters['peak_window_bytes'], self.pending_bytes)
+ self.cursor += 1
+
+ def before(self, index):
+ import time
+ self._fill()
+ item = self.pending.get(index)
+ if item is None:
+ return
+ started = time.monotonic()
+ try:
+ item[1].result()
+ except Exception as error:
+ # A failed optimization must not replace the real tensor reader's
+ # validation/error. Stop speculative reads; the normal path retries
+ # this immutable file and still checks its identity and contents.
+ self.failed = self.failed or type(error).__name__ + ': ' + str(error)
+ self.stop.set()
+ for _, future in self.pending.values():
+ future.cancel()
+ finally:
+ self.counters['wait_seconds'] += time.monotonic() - started
+
+ def consumed(self, index):
+ item = self.pending.pop(index, None)
+ if item is not None:
+ self.pending_bytes -= item[0]
+ if not self.failed:
+ # mmap copies mark consumed pages active. Under a tight cgroup
+ # Linux otherwise evicts the unread speculative pages first,
+ # so both threads end up fetching the same weights from disk.
+ advised, errors = release_file_pages(self.paths_by_index[index])
+ self.counters['discard_advised_bytes'] += advised
+ self.counters['discard_errors'] += errors
+ if index == self.layer_count - 1:
+ if self.pending:
+ raise RuntimeError('Read-ahead layers were consumed out of order')
+ self.cursor = 0
+ self._fill()
+
+ def stats(self):
+ with self.lock:
+ return dict(self.counters, enabled=True, failure=self.failed,
+ scratch_buffer_bytes=4 * 2**20,
+ read_scope='Logical CPU reads, including file-cache hits; not physical disk I/O',
+ scope='Reclaimable file cache; one reader, no extra pinned RAM or GPU slot')
+
+ def close(self):
+ if self.closed:
+ return
+ self.closed = True
+ self.stop.set()
+ for _, future in self.pending.values():
+ future.cancel()
+ self.executor.shutdown(wait=True, cancel_futures=True)
+ self.pending.clear()
+ self.pending_bytes = 0
+
+
+class SafetensorLayers:
+ def __init__(self, paths, prefixes, *, intermediate_dtype=None, direct_read=None):
+ from .system import windows
+ self.direct_read = windows() if direct_read is None else direct_read
+ self.prefixes = tuple(prefixes)
+ self.intermediate_dtype = intermediate_dtype
+ self.files = {}
+ self.handle_files = {}
+ self.keys = {}
+ self.byte_ranges = {}
+ for path in sorted(set(Path(p).resolve() for p in paths)):
+ self.files[path] = self.identity(path)
+ with _open_safetensors(path, framework='np') as handle:
+ # safe_open validates the file layout first. Retain only its
+ # header offsets, never tensor storage or a file mapping.
+ if self.direct_read:
+ import json
+ import os
+ import struct
+ with path.open('rb') as stream:
+ # Windows stat().ctime can be creation time while
+ # fstat().ctime is NTFS ChangeTime. Keep each API's
+ # baseline instead of comparing these different clocks.
+ opened = _file_identity(os.fstat(stream.fileno()))
+ if opened[:4] != self.files[path][:4]:
+ raise CheckpointChangedError(path, 'handle', self.files[path][:4], opened[:4])
+ self.handle_files[path] = opened
+ length = struct.unpack('<Q', stream.read(8))[0]
+ if not 2 <= length <= min(100_000_000, self.files[path][2] - 8):
+ raise ValueError('Invalid safetensors header size')
+ header = json.loads(stream.read(length))
+ self.check(path)
+ for key in handle.keys():
+ if key in self.keys:
+ raise ValueError('Duplicate checkpoint tensor: ' + key)
+ self.keys[key] = path
+ if self.direct_read:
+ spec = header[key]
+ tensor = handle.get_slice(key)
+ if spec['shape'] != tensor.get_shape() or spec['dtype'] != tensor.get_dtype():
+ raise ValueError('Checkpoint header changed: ' + key)
+ start, end = spec['data_offsets']
+ self.byte_ranges[key] = (8 + length + start, end - start,
+ spec['dtype'], tuple(spec['shape']))
+ self.check(path)
+
+ @staticmethod
+ def identity(path):
+ return _file_identity(path.stat())
+
+ def check(self, path):
+ observed = self.identity(path)
+ if observed != self.files[path]:
+ raise CheckpointChangedError(path, 'path', self.files[path], observed)
+
+ def stage_into(self, index, layout, planes):
+ """Read matching tensor bytes directly into the existing CPU slot.
+
+ Windows pread otherwise creates a whole layer of temporary tensors
+ before copying it into this slot. FileIO.readinto uses the destination
+ storage itself, so neither that allocation nor its CPU copy is needed.
+ Dtype conversion (including VAE intermediate rounding) keeps the
+ established tensor reader. Validate the entire plan before any write.
+ """
+ if not self.direct_read or self.intermediate_dtype is not None:
+ return False
+ import os
+ import math
+ import sys
+ import torch
+ if sys.byteorder != 'little':
+ return False
+ prefix = self.prefixes[index]
+ reads = {}
+ for _, name, dtype, offset, count, shape in layout:
+ key = prefix + name
+ start, size, stored, stored_shape = self.byte_ranges[key]
+ if stored_shape != tuple(shape) or count != math.prod(shape):
+ raise ValueError('Checkpoint layer shape changed: ' + key)
+ if getattr(torch, _TORCH_DTYPES.get(stored, ''), None) != dtype:
+ return False
+ plane = planes[dtype]
+ if (plane.device.type != 'cpu' or plane.dtype != dtype or not plane.is_contiguous()
+ or offset < 0 or count < 0 or offset + count > plane.numel()
+ or size != count * plane.element_size()):
+ raise ValueError('Invalid streamed destination: ' + key)
+ reads.setdefault(self.keys[key], []).append((start, size, dtype, offset))
+ # numpy exposes a writable byte view of CPU/pinned storage without
+ # copying, including BF16/FP8 planes that numpy cannot represent directly.
+ buffers = {dtype: memoryview(plane.detach().view(torch.uint8).numpy()).cast('B')
+ for dtype, plane in planes.items()}
+ for path in reads:
+ self.check(path)
+ try:
+ for path, entries in reads.items():
+ with path.open('rb', buffering=0) as stream:
+ observed = _file_identity(os.fstat(stream.fileno()))
+ if observed != self.handle_files[path]:
+ raise CheckpointChangedError(path, 'handle', self.handle_files[path], observed)
+ for start, size, dtype, offset in sorted(entries, key=lambda row: row[0]):
+ stream.seek(start)
+ begin = offset * planes[dtype].element_size()
+ target = buffers[dtype][begin:begin + size]
+ done = 0
+ while done < size:
+ count = stream.readinto(target[done:])
+ if not count:
+ raise ValueError('Incomplete streamed checkpoint: ' + str(path))
+ done += count
+ finally:
+ for path in reads:
+ self.check(path)
+ return True
+
+ def read_ahead(self, layouts, pinned):
+ from .system import windows
+ # Windows uses the commit-safe safetensors ``pread`` backend. A
+ # second file-cache reader would warm pageable copies while WDDM/Commit
+ # is already the tight resource and can make the next transfer slower.
+ # Linux keeps this bounded reader because clean file-cache pages are
+ # reclaimable and MemAvailable accounts for them.
+ if windows():
+ return None
+ plans = {}
+ for index, layout in enumerate(layouts):
+ if pinned[index]:
+ continue
+ prefix = self.prefixes[index]
+ paths = {self.keys[prefix + name] for _, name, *_ in layout}
+ # Prepared transformer groups each own a file. Do not pull an
+ # entire multi-layer VAE/refiner shard into RAM to warm one layer.
+ if any(not key.startswith(prefix) for key, path in self.keys.items() if path in paths):
+ continue
+ plans[index] = sorted(paths)
+ if not plans:
+ return None
+ # Pinned copies and zero-sized streamed placeholders already replaced
+ # these mapped sources. Their old active cache pages must not crowd out
+ # the next layers. No inference tensor or file content is discarded.
+ for path in self.files:
+ self.check(path)
+ advised, errors = release_file_pages(self.files)
+ reader = LayerReadAhead(plans, self.check, len(layouts))
+ reader.counters['discard_advised_bytes'] += advised
+ reader.counters['discard_errors'] += errors
+ return reader
+
+ @contextmanager
+ def layer(self, index, layout):
+ from contextlib import ExitStack
+ prefix = self.prefixes[index]
+ tensors = {}
+ paths = {self.keys[prefix + name] for _, name, *_ in layout}
+ for path in paths:
+ self.check(path)
+ try:
+ with ExitStack() as stack:
+ opened = {path: stack.enter_context(_open_safetensors(path, framework='pt', device='cpu'))
+ for path in paths}
+ for _, name, dtype, _, count, shape in layout:
+ key = prefix + name
+ tensor = opened[self.keys[key]].get_tensor(key)
+ if tuple(tensor.shape) != tuple(shape) or tensor.numel() != count:
+ raise ValueError('Checkpoint layer shape changed: ' + key)
+ # Prepared VAE Linear values follow the original loader's
+ # FP32 rounding before FP16 autocast, even for FP64 shards.
+ if self.intermediate_dtype is not None:
+ tensor = tensor.to(dtype=self.intermediate_dtype)
+ tensors[name] = tensor.to(dtype=dtype)
+ yield tensors
+ finally:
+ tensors.clear()
+ for path in paths:
+ self.check(path)
diff --git a/freevideo_engine/support_report.py b/freevideo_engine/support_report.py
new file mode 100644
index 0000000..3b9c036
--- /dev/null
+++ b/freevideo_engine/support_report.py
@@ -0,0 +1,369 @@
+"""One small, torch-free report per request, including failed requests."""
+import hashlib
+import json
+import math
+from pathlib import Path
+import statistics
+
+from . import __version__
+from .diagnostics import Redactor, is_link, read_bounded
+from .diagnostic_resources import kernel_receipt
+from .monitoring import save
+
+
+def number(value):
+ return value if type(value) in (int, float) and math.isfinite(value) and value >= 0 else None
+
+
+def mapping(value):
+ return value if isinstance(value, dict) else {}
+
+
+def code_identity():
+ package = Path(__file__).parent
+ names = ('generate.py', 'policy.py', 'runtime.py', 'attention.py', 'decode.py', 'worker.py',
+ 'encode_worker.py', 'encoder_checkpoint.py', 'system.py')
+ values = {}
+ for name in names:
+ try:
+ values[name] = hashlib.sha256((package / name).read_bytes()).hexdigest()
+ except OSError:
+ values[name] = None
+ return dict(version=__version__, files_sha256=values)
+
+
+def summarize(report, engine, encoding):
+ profile = mapping(report.get('profile'))
+ policy = mapping(profile.get('policy'))
+ cache_hit = any(mapping(report.get(k)).get(flag) is True for k, flag in
+ (('input_cache', 'conditioning_hit'), ('tuning', 'conditioning_cache_hit'), ('encoding', 'cache_hit')))
+ preencoded = str(report.get('encoder_mode', '')).startswith('preencoded')
+ sampling_reused = engine.get('sampling_reused') is True
+ stages = []
+ # Substage timers overlap their parent. Only these top-level stages may
+ # contribute to request accounting. Cached receipts describe a past run.
+ for name, zh, value in (
+ ('text_encoding', '文本编码(含加载)', 0 if cache_hit or preencoded else encoding.get('work_seconds')),
+ ('model_load', '视频模型准备与加载', engine.get('load_seconds')),
+ ('sampling', '采样', engine.get('sample_seconds')),
+ ('latent_save', '保存潜变量', engine.get('latent_save_seconds')),
+ ('decode_save', '解码与视频保存', engine.get('decode_save_seconds'))):
+ stages.append(dict(stage=name, label_zh=zh, seconds=number(value)))
+ if name == 'sampling' and sampling_reused:
+ stages[-1].update(reused_from_attempt=True, label_zh='采样(复用本次请求已完成的采样)')
+ total = number(report.get('request_seconds'))
+ measured = sum(row['seconds'] for row in stages if row['seconds'] is not None)
+ accounting = 'partial: missing stage timers' if any(r['seconds'] is None for r in stages) else 'complete stage timers'
+ if total is not None and measured <= total + .01:
+ stages.append(dict(stage='other_or_unmeasured', label_zh='其他/未单独计时', seconds=max(0., total-measured)))
+ elif total is not None:
+ accounting = 'inconsistent: stage sum exceeds request time; no percentages assigned'
+ for row in stages:
+ row['request_percent'] = (100 * row['seconds'] / total if total and row['seconds'] is not None
+ and measured <= total + .01 else None)
+ ranked = sorted((r for r in stages if r['seconds'] is not None), key=lambda r: r['seconds'], reverse=True)
+ config = mapping(engine.get('config')) or mapping(profile.get('engine'))
+ offload = mapping(engine.get('offload'))
+ sampling_memory = mapping(engine.get('sampling_memory'))
+ decoder_read_ahead = mapping(engine.get('decoder_read_ahead'))
+ hardware = mapping(report.get('runtime_hardware')) or mapping(policy.get('hardware'))
+ steps = engine.get('step_seconds', [])
+ steps = steps if isinstance(steps, list) and all(number(x) is not None for x in steps) else []
+ kernels = kernel_receipt(engine)
+ hints = []
+ if (mapping(engine.get('config')).get('resident_blocks') == 50
+ and number(offload.get('transfers')) == 0 and number(offload.get('h2d_bytes')) == 0):
+ hints.append(dict(kind='measured', message='All 50 transformer blocks are configured on GPU and recorded weight transfers are zero. '
+ 'A larger host weight cache cannot remove transfers from this placement. This does not prove full GPU residency is the fastest placement.',
+ message_zh='全部 50 个模型块配置为 GPU 常驻,记录到的权重传输为零;增大 RAM 权重缓存不能再减少这条路径的传输,但这不证明全驻留是最快的放置方式。'))
+ if ranked:
+ hints.append(dict(kind='measured', message='Largest measured wall-time category: ' + ranked[0]['stage'],
+ message_zh='耗时最大的已记录类别:' + ranked[0]['label_zh']))
+ if len(steps) > 1 and statistics.median(steps[1:]) > 0 and steps[0] > 2 * statistics.median(steps[1:]):
+ hints.append(dict(kind='needs_investigation', message='First sampling step exceeds twice the later-step median. '
+ 'Check compilation, cache preparation and transfers; these timers do not identify the cause.',
+ message_zh='第一步耗时超过后续步骤中位数的两倍;需检查编译、缓存准备及传输,不能仅据此认定是编译。'))
+ if config.get('attention_cpu_outputs') or config.get('residual_offload'):
+ hints.append(dict(kind='configuration', message='CPU attention/residual staging is active. Check live VRAM and '
+ 'policy decisions before attributing slow sampling to GPU compute.',
+ message_zh='已启用 attention 输出或残差的 CPU 暂存;需结合当时剩余显存及策略理由判断采样变慢原因。'))
+ reserved = number(engine.get('torch_peak_reserved_bytes'))
+ capacity = number(hardware.get('vram_total'))
+ if hardware.get('system') == 'Windows' and reserved and capacity and reserved > capacity:
+ hints.append(dict(kind='needs_investigation', message='CUDA allocator peak reservation exceeds physical VRAM. '
+ 'Check Windows shared GPU memory/system-memory fallback and allocator fragmentation. '
+ 'Reservation alone does not prove paging caused the slowdown.',
+ message_zh='CUDA 分配器保留峰值超过物理显存;需检查 Windows 共享 GPU 内存/系统内存回退和分配碎片。仅凭 reserved 不能确认变慢由换页造成。'))
+ memory_steps = sampling_memory.get('steps') or []
+ windows_observations = [mapping(row).get('windows') for row in memory_steps] if isinstance(memory_steps, list) else []
+ windows_observations.append(sampling_memory.get('incomplete_step_windows'))
+ if any((number(mapping(mapping(row).get('local')).get('over_budget_samples')) or 0) > 0 for row in windows_observations):
+ hints.append(dict(kind='measured', message='This process exceeded its Windows local video-memory budget during sampling. '
+ 'Compare the per-step nonlocal memory and latency; this is pressure evidence, not a measured PCIe paging rate.',
+ message_zh='采样时进程超过了 Windows 分配的本地显存预算。请对照每步共享内存与耗时;这是压力证据,不是 PCIe 换页速率测量。'))
+ attempts = report.get('resource_attempts', [])
+ failures = [dict(state=r.get('state'), failure=r.get('failure')) for r in attempts
+ if isinstance(r, dict) and r.get('failure')] if isinstance(attempts, list) else []
+ if failures:
+ if sampling_reused:
+ hints.append(dict(kind='measured', message='Request includes %d failed attempt(s). Sampling timings describe the '
+ 'completed sampling reused by the decoder retry; the final attempt did not run sampling again.' % len(failures),
+ message_zh='本次请求包含 %d 次失败尝试。采样计时来自本次请求已完成的采样;最终尝试复用其结果,只重试解码。' % len(failures)))
+ else:
+ hints.append(dict(kind='measured', message='Request includes %d failed attempt(s); their cost is outside the final successful sampling timer.' % len(failures),
+ message_zh='本次请求包含 %d 次失败尝试,其开销未计入最终成功的采样计时。' % len(failures)))
+ def budget(key, fallback):
+ measured = number(policy.get(key))
+ planned = number(profile.get(fallback))
+ return measured if measured is not None else planned*1e9 if planned is not None else None
+ return dict(status='complete' if report.get('success') is True else 'incomplete',
+ request_seconds=total, accounting=accounting, stages=stages, findings=hints,
+ kernels=kernels or None,
+ largest_category=ranked[0]['stage'] if ranked else None,
+ step_seconds=steps, completed_nfe=len(steps),
+ sampling_plan=engine.get('sampling_plan') or report.get('sampling_plan'),
+ sampling_passes=engine.get('sampling_passes'), latent_upscale=engine.get('latent_upscale'),
+ sampling_reused=sampling_reused,
+ sampling_memory=sampling_memory or None,
+ decoder_read_ahead={key: decoder_read_ahead[key] for key in (
+ 'state', 'read_bytes', 'total_bytes', 'allowance_bytes',
+ 'elapsed_seconds', 'gpu_allocation_bytes', 'private_buffer_bytes',
+ 'sampling') if key in decoder_read_ahead} or None,
+ device_memory=mapping(engine.get('device_memory')) or None,
+ seconds_per_completed_nfe=(sum(steps)/len(steps) if steps else None),
+ conditioning_cache_hit=cache_hit, preencoded=preencoded,
+ geometry=report.get('geometry') or engine.get('geometry'),
+ task=config.get('task'), effective_engine_config=config,
+ config_scope='executed' if engine.get('config') else 'planned; executed config unavailable',
+ weight_placement=dict(resident_blocks=config.get('resident_blocks'), pin_host_gb=config.get('pin_host_gb'),
+ pinned_model_bytes=config.get('pinned_model_bytes'),
+ pinned_host_allocated_bytes=config.get('pinned_host_allocated_bytes'),
+ pinned_layer_count=offload.get('pinned_layer_count'), streamed_layer_count=offload.get('streamed_layer_count'),
+ pinned_buffer_bytes=offload.get('pinned_buffer_bytes'), pageable_buffer_bytes=offload.get('pageable_buffer_bytes'),
+ cuda_buffer_bytes=offload.get('cuda_buffer_bytes'),
+ host_buffer_wait_seconds=offload.get('host_buffer_wait_seconds'),
+ prefetch_wait_seconds=offload.get('prefetch_wait_seconds'),
+ stream_weights=config.get('stream_weights'), transfers=offload.get('transfers'),
+ h2d_bytes=offload.get('h2d_bytes'), host_stage_seconds=offload.get('host_stage_seconds'),
+ direct_read_layers=offload.get('direct_read_layers'), direct_read_bytes=offload.get('direct_read_bytes'),
+ host_prefetch=offload.get('host_prefetch'), host_prefetch_reads=offload.get('host_prefetch_reads'),
+ host_prefetch_wait_seconds=offload.get('host_prefetch_wait_seconds'),
+ host_prefetch_buffer_bytes=offload.get('host_prefetch_buffer_bytes'),
+ host_prefetch_disabled_reason=offload.get('host_prefetch_disabled_reason'),
+ h2d_seconds=offload.get('h2d_seconds'), timing_scope='Transfer/wait timers may overlap; do not add to wall time.',
+ pinning_scope='pin_host_gb is requested decimal GB. pinned_model_bytes is actual weight payload; '
+ 'pinned_host_allocated_bytes includes rounded active and cached host allocations at load. '
+ 'Per-step host counters describe the process during sampling.'),
+ decoder_options=profile.get('decoder'),
+ hardware=hardware, failed_attempts=failures,
+ budget_bytes=dict(gpu=budget('gpu_budget_bytes', 'gpu_budget_gb'),
+ ram=budget('ram_budget_bytes', 'inference_ram_budget_gb')),
+ observed_memory=dict(
+ sampling_allocated_bytes=engine.get('torch_peak_allocated_bytes'), sampling_reserved_bytes=reserved,
+ final_stage_allocated_bytes=engine.get('final_stage_peak_allocated_bytes'),
+ final_stage_reserved_bytes=engine.get('final_stage_peak_reserved_bytes'),
+ whole_gpu_peak_bytes=mapping(mapping(report.get('resources')).get('gpu')).get('gpu_peak_bytes'),
+ video_ram=mapping(mapping(report.get('resources')).get('ram')),
+ encoding_ram=None if cache_hit or preencoded else mapping(mapping(report.get('encoding_resources')).get('ram'))),
+ encoder_metrics_scope='cached receipt from a previous request; excluded from current timing' if cache_hit else 'this request',
+ compile_seconds=None,
+ compile_scope=(sampling_memory.get('compiler_scope') or
+ 'Compilation is not timed separately; never infer it from a slow first step.'))
+
+
+def write_retry(output, report, *, index, retained, next_profile=None, decision=None):
+ """Save each failed attempt before its replacement starts.
+
+ A separate file keeps the failure evidence alongside the final request's
+ complete timings and the cost of every failed attempt.
+ """
+ if type(index) is not int or not 1 <= index <= 3 or not isinstance(report, dict):
+ return None
+ rows = report.get('resource_attempts')
+ if not isinstance(rows, list) or index > len(rows) or not isinstance(rows[index - 1], dict):
+ return None
+ request = dict(report, success=False)
+ rows = [dict(row) if isinstance(row, dict) else row for row in rows[:index]]
+ failed = rows[-1]
+ previous = mapping(failed.get('recovery'))
+ failed['recovery'] = dict(previous,
+ next_profile=mapping(next_profile) or mapping(previous.get('next_profile')),
+ decision=mapping(decision))
+ request['resource_attempts'] = rows
+ request['profile'] = mapping(failed.get('profile')) or mapping(report.get('profile'))
+ request['resources'] = mapping(failed.get('resources'))
+ request.pop('video', None) # Read the failed worker's retained sidecars.
+ return write(output, request, _retry=dict(stage='video', index=index, retained=retained))
+
+
+def write_encoder_retry(output, encoding, *, index):
+ """Save encoder OOM evidence while the failed attempt is still resident."""
+ if type(index) is not int or not 1 <= index <= 3 or not isinstance(encoding, dict):
+ return None
+ return write(output, _retry=dict(stage='encoding', index=index, encoding=encoding))
+
+
+def write(output, report=None, bridge=None, *, _retry=None):
+ """Bounded JSON/log reads only. Failure to write must not fail generation."""
+ output = Path(output)
+ notes = []
+ def read_json(suffix, path=None):
+ path = path if path is not None else output.with_suffix(suffix)
+ try:
+ if is_link(path):
+ if path.exists():
+ notes.append(suffix + ': linked file omitted')
+ return {}
+ raw, truncated = read_bounded(path, 2 * 2**20)
+ if truncated:
+ notes.append(suffix + ': exceeds report limit')
+ return {}
+ value = json.loads(raw)
+ if not isinstance(value, dict):
+ raise ValueError('Report must be an object')
+ return value
+ except FileNotFoundError:
+ return {}
+ except (OSError, ValueError):
+ notes.append(suffix + ': unreadable or invalid')
+ return {}
+ def sampling_sidecar(metrics, artifact_output):
+ # A terminated worker may never return Engine.sample() or finalize its
+ # metrics, although SamplingMemory persisted every completed step.
+ observed = read_json('.sampling-memory.json', artifact_output.with_suffix('.sampling-memory.json'))
+ current = mapping(metrics.get('sampling_memory'))
+ def count(value):
+ rows = value.get('steps')
+ return len(rows) if isinstance(rows, list) else 0
+ if observed and (not current or count(observed) > count(current)):
+ return dict(metrics, sampling_memory=observed)
+ return metrics
+ try:
+ request = report if isinstance(report, dict) else read_json('.request.json')
+ video_retry = _retry and _retry['stage'] == 'video'
+ encoder_retry = _retry and _retry['stage'] == 'encoding'
+ engine_output = Path(_retry['retained']) / output.name if video_retry else output
+ engine = mapping(request.get('video')) or read_json('.engine.json', engine_output.with_suffix('.engine.json'))
+ engine = sampling_sidecar(engine, engine_output)
+ encoding = mapping(request.get('encoding')) or read_json('.encoding.json')
+ if encoder_retry:
+ request = dict(request, success=False, phase='encoder_oom')
+ engine = {} # A reused CLI output path may hold an older video's sidecars.
+ encoding = dict(_retry['encoding'])
+ encoding['diagnostic_retry'] = dict(index=_retry['index'], final_attempt=_retry['index'] >= 3)
+ attempts = encoding.get('encoder_attempts')
+ latest = mapping(attempts[-1]) if isinstance(attempts, list) and attempts else {}
+ stages = encoding.get('load_stages')
+ last_stage = mapping(stages[-1]) if isinstance(stages, list) and stages else {}
+ request['encoding_failure'] = dict(phase='encoder_oom', failure=dict(kind='gpu_oom',
+ exception=latest.get('exception', []), gpu=latest or mapping(encoding.get('gpu')),
+ memory=mapping(last_stage.get('memory'))))
+ bridge = mapping(bridge) or read_json('comfy-request', output.parent / 'comfy-request.json')
+ redactor = Redactor([(str(output.parent), '<RUN>')])
+ try:
+ redactor.replacements.append((str(Path.home()), '<HOME>'))
+ except (OSError, RuntimeError):
+ pass
+ prompt = output.with_suffix('.artifacts') / 'prompt.txt'
+ if prompt.is_file() and not is_link(prompt) and not is_link(prompt.parent):
+ raw, truncated = read_bounded(prompt, 64*1024)
+ if not truncated:
+ text = raw.decode('utf-8', errors='replace').strip()
+ if text:
+ redactor.prompts.add(text)
+ redactor.structured(request) # Learn prompts before redacting repeated error text.
+ redactor.structured(bridge)
+ # Failed children never return their telemetry into request['resources'].
+ # Their retained memory files must feed the summary, not just an unused
+ # raw section of the local report.
+ request = dict(request)
+ attempts = request.get('resource_attempts')
+ if isinstance(attempts, list) and not encoder_retry:
+ from .diagnostic_resources import attempt_metrics
+ restored = []
+ for index, row in enumerate(attempts, 1):
+ if not isinstance(row, dict):
+ restored.append(row)
+ continue
+ row = dict(row)
+ retained = row.get('retained')
+ if video_retry and index == _retry['index']:
+ artifact_output = engine_output
+ elif isinstance(retained, str) and retained:
+ artifact_output = Path(retained) / output.name
+ elif index == len(attempts) and row.get('failure'):
+ artifact_output = output
+ else:
+ restored.append(row)
+ continue
+ metrics = mapping(row.get('metrics'))
+ observed = sampling_sidecar(metrics, artifact_output)
+ if observed is not metrics:
+ row['metrics'] = dict(metrics, **attempt_metrics(observed))
+ restored.append(row)
+ request['resource_attempts'] = restored
+ if encoder_retry:
+ request['resources'] = {}
+ elif not request.get('resources'):
+ request['resources'] = dict(ram=read_json('.engine.memory.json', engine_output.with_suffix('.engine.memory.json')),
+ gpu=read_json('.engine.gpu.json', engine_output.with_suffix('.engine.gpu.json')))
+ if not request.get('encoding_resources'):
+ request['encoding_resources'] = dict(ram=read_json('.encoding.memory.json'))
+ summary = summarize(request, engine, encoding)
+ if video_retry or encoder_retry and _retry['index'] < 3:
+ summary['status'] = 'retrying'
+ payload = dict(schema_version=1, summary=summary,
+ runtime_code=request.get('runtime_code'), collector_version=__version__,
+ request={k:request[k] for k in ('seed', 'encoder_mode', 'profile', 'resource_planning', 'compatibility',
+ 'reclaimable_resident_models', 'idle_resources', 'tuning', 'input_cache', 'resource_attempts',
+ 'resource_prediction', 'resource_prediction_error', 'error', 'error_message', 'error_type',
+ 'resource_error', 'exception', 'encoding_failure', 'phase') if k in request},
+ engine=engine, encoding=encoding,
+ memory=dict(video=request.get('resources'),
+ encoding=request.get('encoding_resources') or dict(ram=read_json('.encoding.memory.json')),
+ scope='Keep working RAM, PSS/RSS, commit and whole-GPU/allocator observations separate. '
+ 'Cached encoder receipts do not describe this request’s peak.'),
+ bridge={k:bridge[k] for k in ('status', 'error', 'bridge_seconds', 'encoder_prewarm') if k in bridge},
+ collection_notes=notes, log_tails={})
+ # Scrub structured prompts before processing log tails that may repeat them.
+ payload = redactor.structured(payload)
+ # The generic secret redactor matches "token_summary" as a token key.
+ # Restore only its fixed numeric counts, never token IDs or text.
+ from .diagnostic_resources import token_summary
+ payload['encoding']['token_summary'] = token_summary(encoding.get('token_summary'))
+ for suffix in ('.engine.log', '.encoding.log', '.lora.log'):
+ path = (engine_output if suffix == '.engine.log' else output).with_suffix(suffix)
+ if path.is_file() and not is_link(path):
+ raw, truncated = read_bounded(path, 8192)
+ payload['log_tails'][suffix] = redactor.text(raw.decode('utf-8', errors='replace'))
+ if bridge:
+ path = output.parent / 'generate.log'
+ if path.is_file() and not is_link(path):
+ raw, _ = read_bounded(path, 8192)
+ payload['log_tails']['generate.log'] = redactor.text(raw.decode('utf-8', errors='replace'))
+ def clean(value, key=''):
+ if isinstance(value, dict):
+ return {k:clean(v, k) for k,v in value.items()}
+ if isinstance(value, list):
+ return [clean(v, key) for v in value]
+ if isinstance(value, str):
+ # Keep a useful relative name (for example ``<RUN>/video.mp4``
+ # or ``models/cache.safetensors``) while removing drive
+ # letters, usernames, mounts and unrelated parent folders.
+ if redactor._path_key(key):
+ return redactor.path(value)
+ return redactor.text(value)
+ return value
+ suffix = '.debug.json'
+ if _retry:
+ suffix = ('.encoder-retry-' if encoder_retry else '.retry-') + str(_retry['index']) + '.debug.json'
+ target = output.with_suffix(suffix)
+ cleaned = clean(payload)
+ from .diagnostic_summary import summary_report
+ cleaned['analysis'] = summary_report(cleaned)
+ save(target, cleaned)
+ return target
+ except Exception:
+ # Diagnostics must not turn a saved video into a failed request, nor
+ # replace an original OOM/error with a secondary reporting exception.
+ return None
diff --git a/freevideo_engine/system.py b/freevideo_engine/system.py
new file mode 100644
index 0000000..0ecbf65
--- /dev/null
+++ b/freevideo_engine/system.py
@@ -0,0 +1,237 @@
+"""Small OS adapters usable by preflight before any third-party package exists."""
+import os
+from pathlib import Path
+import platform
+import shutil
+import sys
+
+
+def windows():
+ return os.name == 'nt'
+
+
+def source_root():
+ """Find the checkout/ZIP by package location, never by the caller's cwd.
+
+ A frozen desktop bundle and its versioned source cache are not user clones.
+ Keep those on the desktop's saved/user-selected installation path.
+ """
+ if getattr(sys, 'frozen', False):
+ return None
+ root = Path(__file__).resolve().parents[1]
+ if ((root / 'pyproject.toml').is_file()
+ and any((root / name).is_file() for name in ('setup.sh', 'freevideo.ps1'))
+ and not (root / 'launcher-source.json').is_file()):
+ return root
+ return None
+
+
+def install_root():
+ if os.environ.get('FREEVIDEO_HOME'):
+ return Path(os.environ['FREEVIDEO_HOME']).expanduser()
+ source = source_root()
+ if source is not None:
+ return source
+ if windows():
+ return Path(os.environ.get('LOCALAPPDATA', Path.home() / 'AppData' / 'Local')) / 'FreeVideo'
+ return Path.home() / '.local' / 'share' / 'freevideo'
+
+
+def bootstrap_root(root=None):
+ override = os.environ.get('FREEVIDEO_BOOTSTRAP_ROOT')
+ selected = Path(override) if override else Path(root if root is not None else install_root()) / '.freevideo' / 'bootstrap'
+ # Installation commands may run in the source/build directory instead of
+ # the caller's cwd. Anchor a relative selection before passing it to uv.
+ # Python 3.9 on Windows can return a relative path from resolve() when
+ # none of the selected directory exists yet. Anchor before resolving.
+ return Path(os.path.abspath(selected.expanduser())).resolve()
+
+
+def venv_python(directory, system=None):
+ return Path(directory) / ('Scripts/python.exe' if (system or platform.system()) == 'Windows' else 'bin/python')
+
+
+def system_memory():
+ if windows():
+ from .win32 import memory_status
+ return memory_status()
+ values = {k: int(v.split()[0]) * 1024 for k, v in
+ (line.split(':', 1) for line in Path('/proc/meminfo').read_text(encoding='utf-8').splitlines())
+ if v.strip().endswith('kB')}
+ return {'total_bytes': values['MemTotal'], 'available_bytes': values['MemAvailable'],
+ 'physical_available_bytes': values['MemAvailable'], 'commit_available_bytes': None,
+ 'swap_total_bytes': values.get('SwapTotal'), 'swap_free_bytes': values.get('SwapFree')}
+
+
+# Host availability kept free of pinned weights: the platform emergency floor
+# plus room for the non-weight private memory a request still grows into.
+# Measured on a 12/32 Ada laptop: non-pinned private working set peaked near
+# 4.0 GiB whether 0.4 GiB or 12.1 GiB was pinned. Unpinned weights remain
+# reclaimable, but a working set larger than RAM must reread them from disk.
+# That cost needs the execution-path allowance below, rather than a universal
+# assumption that keeping fewer pinned weights costs only H2D transfers.
+HOST_WEIGHT_HEADROOM = 8 * 2**30
+
+
+def residual_host_headroom(canvas=None):
+ """Room for one pageable packed residual, separate from the weight cache."""
+ rows = (sum(canvas.get(key, 0) for key in
+ ('video_tokens', 'reference_video_tokens', 'reference_audio_tokens'))
+ if canvas is not None else 72576)
+ return int(2**30 * max(1., rows / 72576))
+
+
+def weight_cache_headroom(*, system=None, streamed=False, cpu_outputs=True, canvas=None):
+ """Separate host work from weight caching, without counting swap as RAM.
+
+ The 8 GiB Windows allowance covers its measured private working set and
+ host attention outputs. Linux streaming with GPU attention outputs measured
+ 2.56 GiB non-weight working memory in a complete 768p/243-frame request.
+ 3.5 GiB leaves growth/staging room inside the already OS-reserved budget.
+ Linux CPU readouts measured 4.56 GiB beyond retained weights in the same
+ complete request. Keep 5 GiB at that geometry; larger requests additionally
+ reserve 2.5 GiB per reference token volume for their growing host readouts.
+ Unknown geometry retains the original allowance. This is a starting
+ estimate; complete local observations and runtime RAM guards still apply.
+ """
+ if system is None:
+ system = 'Windows' if windows() else 'Linux'
+ if system != 'Linux' or not streamed:
+ return HOST_WEIGHT_HEADROOM
+ if not cpu_outputs:
+ return int(3.5 * 2**30)
+ if canvas is None:
+ return HOST_WEIGHT_HEADROOM
+ from .geometry import geometry
+ checked = geometry(canvas['width'], canvas['height'], frames=canvas['frames'])
+ if any(canvas.get(key) != checked[key] for key in ('width', 'height', 'frames', 'video_tokens')):
+ raise ValueError('Host cache allowance requires consistent request geometry')
+ growth = max(0., checked['video_tokens'] / (72 * 1008) - 1.)
+ return int((5. + 2.5 * growth) * 2**30)
+
+
+def effective_available():
+ """Availability including clean mapped pages this process could release.
+
+ Linux MemAvailable already includes reclaimable page cache, so mapped
+ weights cost nothing there. Windows GlobalMemoryStatusEx counts only free
+ and standby pages, leaving resident clean mapped pages out: measured a
+ 21.85 GiB working set with 4.30 GiB private while availability read
+ 1.62 GiB. Credit only this process's own resident non-private pages, which
+ are the mapped weight pages, and stay bounded by commit and total RAM.
+ """
+ memory = system_memory()
+ if not windows():
+ return memory['available_bytes']
+ from .win32 import process_memory
+ try:
+ row = process_memory(os.getpid())
+ except OSError:
+ row = None
+ reclaimable = max(0, row['rss_bytes'] - row['private_working_set_bytes']) if row else 0
+ return min(memory['total_bytes'], memory['commit_available_bytes'],
+ memory['physical_available_bytes'] + reclaimable)
+
+
+def nvidia_smi():
+ found = shutil.which('nvidia-smi')
+ if found:
+ return found
+ if windows():
+ for path in (Path(os.environ.get('SystemRoot', r'C:\Windows')) / 'System32/nvidia-smi.exe',
+ Path(os.environ.get('ProgramFiles', r'C:\Program Files')) / 'NVIDIA Corporation/NVSMI/nvidia-smi.exe'):
+ if path.is_file():
+ return str(path)
+ raise RuntimeError('NVIDIA driver/nvidia-smi is missing. Install NVIDIA driver 580+ and restart before setup.')
+
+
+def cpu_info():
+ return platform.processor() if windows() else Path('/proc/cpuinfo').read_text(encoding='utf-8').split('\n\n')[0]
+
+
+def memory_peak(record):
+ return record.get('process_tree_peak_guard_bytes', record.get('process_tree_peak_pss_bytes'))
+
+
+def memory_complete(record):
+ return record.get('process_tree_guard_complete', record.get('process_tree_pss_complete')) is True
+
+
+def memory_sample(record):
+ return record.get('guard_bytes', record.get('pss_bytes'))
+
+
+def inference_memory_sample(record):
+ """Inference can reread clean file pages; keep Windows' existing metric."""
+ return record.get('inference_guard_bytes', memory_sample(record))
+
+
+def inference_environment(environment, ram_budget_bytes):
+ """Avoid a second compiler-process heap on small-RAM inference workers."""
+ selected = dict(environment)
+ if ram_budget_bytes < 8 * 2**30:
+ selected.setdefault('TORCHINDUCTOR_COMPILE_THREADS', '1')
+ return selected
+
+
+def inference_headroom(record):
+ """Available RAM including reclaimable clean model cache, never swap."""
+ return record.get('inference_available_bytes',
+ record.get('effective_available_bytes', record['system_available_bytes']))
+
+
+def inference_emergency_floor(budget_bytes, reserve_gib=None):
+ """Separate planned OS growth headroom from the runtime exhaustion tripwire.
+
+ Planning already subtracts its reserve before assigning the worker budget.
+ Applying that same 1/2 GiB reserve as an immediate kill threshold rejects
+ valid small-memory requests. Keep a bounded 5% tripwire, at most 256 MiB.
+ An explicit zero reserve also requests a zero tripwire; actual exhaustion,
+ commit pressure and the measured working-memory budget still apply.
+ """
+ import math
+ if reserve_gib is not None and (not math.isfinite(reserve_gib) or reserve_gib < 0):
+ raise ValueError('RAM reserve must be finite and nonnegative')
+ floor = min(256 * 2**20, int(budget_bytes * .05))
+ return min(floor, int(reserve_gib * 2**30)) if reserve_gib is not None else floor
+
+
+def ram_budget_is_estimate(args):
+ """Automatic placement is an estimate; explicit capacity tests stay strict."""
+ return getattr(args, 'ram_gib', None) is None and not getattr(args, 'profile', None)
+
+
+def inference_pressure(record, budget_bytes, minimum_available_bytes=None, *, budget_is_estimate=False):
+ used = inference_memory_sample(record)
+ if used is None:
+ raise RuntimeError('Worker memory monitoring is incomplete; see the retained diagnostics.')
+ floor = inference_emergency_floor(budget_bytes) if minimum_available_bytes is None else minimum_available_bytes
+ if type(floor) is not int or floor < 0:
+ raise ValueError('Emergency RAM floor must be a nonnegative byte count')
+ available = inference_headroom(record)
+ reasons = []
+ if used > budget_bytes:
+ reasons.append('working_memory_budget')
+ if available <= 0 or available < floor:
+ reasons.append('system_or_commit_pressure')
+ return dict(reasons=reasons, enforced_reasons=[r for r in reasons
+ if r != 'working_memory_budget' or not budget_is_estimate],
+ budget_is_estimate=bool(budget_is_estimate), working_bytes=used, budget_bytes=budget_bytes,
+ available_bytes=available, emergency_floor_bytes=floor,
+ physical_available_bytes=record.get('system_physical_available_bytes'),
+ commit_available_bytes=record.get('system_commit_available_bytes'))
+
+
+def enable_terminal(stream):
+ if not windows():
+ return True
+ try:
+ import ctypes
+ import msvcrt
+ from .win32 import kernel32
+ mode = ctypes.c_uint32()
+ handle = msvcrt.get_osfhandle(stream.fileno())
+ return bool(kernel32().GetConsoleMode(handle, ctypes.byref(mode)) and
+ kernel32().SetConsoleMode(handle, mode.value | 4))
+ except (OSError, ValueError, AttributeError):
+ return False
diff --git a/freevideo_engine/tensor_io.py b/freevideo_engine/tensor_io.py
new file mode 100644
index 0000000..0d4bbc5
--- /dev/null
+++ b/freevideo_engine/tensor_io.py
@@ -0,0 +1,14 @@
+"""Bounded CPU tensor reads for model preparation, without persistent file maps."""
+
+
+def open_tensors(path, *, framework='pt'):
+ from safetensors import safe_open
+ # The default Torch mmap is copy-on-write. On Windows its commit charge is
+ # the whole shard, and a tiny returned tensor can keep that mapping alive.
+ # pread owns only the requested tensor's bytes, including BF16/FP8 tensors.
+ # Do not silently fall back to mmap on an older manually installed runtime.
+ try:
+ return safe_open(path, framework=framework, device='cpu', backend='pread')
+ except TypeError as error:
+ raise RuntimeError('Bounded model preparation requires safetensors >= 0.8.0. '
+ 'Rerun setup to update the managed environment.') from error
diff --git a/freevideo_engine/terminal_ui.py b/freevideo_engine/terminal_ui.py
new file mode 100644
index 0000000..7d283b4
--- /dev/null
+++ b/freevideo_engine/terminal_ui.py
@@ -0,0 +1,517 @@
+"""Small, dependency-free terminal UI, including before Python is installed.
+
+Only completed work drives progress bars. Unknown-duration work stays animated.
+No alternate screen, input interception, network access or extra UI packages.
+"""
+from __future__ import annotations
+
+import json
+import math
+import os
+from pathlib import Path
+import re
+import shutil
+import sys
+import threading
+import time
+import unicodedata
+from .system import enable_terminal
+
+ESCAPES = re.compile(r'\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b\[[0-?]*[ -/]*[@-~]')
+SPINNER = '⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'
+
+
+def clean(value):
+ value = ESCAPES.sub('', str(value))
+ return ' '.join(''.join(c for c in value if c.isprintable() or c.isspace()).split())
+
+
+def clipped(value, width):
+ result, used = '', 0
+ for char in clean(value):
+ size = 2 if unicodedata.east_asian_width(char) in ('W', 'F') else 1
+ if used + size > width:
+ return result[:-1] + '…' if result else ''
+ result += char
+ used += size
+ return result
+
+
+def duration(seconds):
+ seconds = max(0, int(seconds))
+ return '%d:%02d:%02d' % (seconds // 3600, seconds // 60 % 60, seconds % 60) if seconds >= 3600 else '%02d:%02d' % (seconds // 60, seconds % 60)
+
+
+def bar(done, total, width=22):
+ fraction = min(1., max(0., done / total)) if total else 0.
+ filled = int(width * fraction)
+ return '━' * filled + '─' * (width - filled)
+
+
+class ProgressEstimate:
+ """Bounded estimate for the current file/counter, never the whole install."""
+ def __init__(self):
+ self.scope = None
+ self.total = self.done = 0
+ self.rate = None
+ self.last = self.started = None
+ self.samples = 0
+ self.direct_rate = False
+
+ def update(self, done, total, *, scope=None, rate=None, now=None):
+ now = time.monotonic() if now is None else now
+ done, total = max(0, done or 0), max(0, total or 0)
+ if self.last is None or scope != self.scope or total != self.total or done < self.done:
+ self.scope, self.total, self.done = scope, total, done
+ self.last = self.started = now
+ self.rate, self.samples = None, 0
+ self.direct_rate = False
+ advanced = done > self.done
+ if rate is not None and math.isfinite(rate) and rate > 0:
+ self.rate = rate if self.rate is None else .3 * rate + .7 * self.rate
+ self.direct_rate = True
+ elif advanced and now > self.last:
+ observed = (done - self.done) / (now - self.last)
+ self.rate = observed if self.rate is None else .3 * observed + .7 * self.rate
+ self.samples += 1
+ if advanced:
+ self.last = now
+ self.done = done
+
+ def remaining(self, now=None):
+ now = time.monotonic() if now is None else now
+ if not self.total or self.done > self.total or not self.rate or self.last is None or now - self.last > 10:
+ return None
+ if not self.direct_rate and (self.samples < 3 or now - self.started < 2):
+ return None
+ if self.done >= self.total:
+ return 0.
+ return max(0., (self.total - self.done) / self.rate)
+
+
+class TerminalUI:
+ def __init__(self, title, *, plain=False, no_color=False, stream=None, verbose=False, show_location=True):
+ self.stream = stream or sys.stdout
+ self.live = not plain and self.stream.isatty() and os.environ.get('TERM') != 'dumb'
+ if self.live:
+ self.live = enable_terminal(self.stream)
+ self.color = self.live and not no_color and 'NO_COLOR' not in os.environ
+ self.unicode = 'utf' in (getattr(self.stream, 'encoding', None) or 'utf-8').lower()
+ self.title = clean(title)
+ self.verbose = verbose
+ self.show_location = show_location
+ self.tasks = {}
+ self.stage = 'Preparing'
+ self.done = self.total = 0
+ self.resource = ''
+ self.location = ''
+ self.started = time.monotonic()
+ self._lock = threading.RLock()
+ self._stop = threading.Event()
+ self._thread = None
+ self._lines = 0
+
+ def event(self, kind, **value):
+ if os.environ.get('FREEVIDEO_UI_EVENTS') == '1':
+ self.write(json.dumps(dict(event='freevideo_ui', kind=kind, **value)) + '\n')
+
+ def paint(self, value, code):
+ return '\x1b[' + code + 'm' + value + '\x1b[0m' if self.color else value
+
+ def write(self, value):
+ if not self.unicode:
+ value = value.translate(str.maketrans({'━': '=', '─': '-', '│': '|', '╭': '+', '╮': '+',
+ '╰': '+', '╯': '+', '✓': '+', '✕': 'x', '…': '.', '·': '.'}))
+ value = value.encode('ascii', errors='replace').decode('ascii')
+ self.stream.write(value)
+ self.stream.flush()
+
+ def panel(self, title, rows):
+ """Static reviewable preflight; never starts live rendering."""
+ width = max(28, min(108, shutil.get_terminal_size((100, 28)).columns - 2))
+ self.write('\n' + self.paint('╭─ ' + clean(title) + ' ', '1;36') + '\n')
+ for label, value in rows:
+ # Keep full paths/commands in scrollback, including narrow terminals.
+ self.write(self.paint('│ ', '36') + self.paint(clean(label) + ' ', '1') + clean(value) + '\n')
+ self.write(self.paint('╰' + '─' * min(width - 1, 70), '36') + '\n')
+
+ def start(self, location=''):
+ self.location = str(location)
+ if self.live:
+ self.write('\x1b[?25l')
+ self._thread = threading.Thread(target=self._animate, name='freevideo-terminal', daemon=True)
+ self._thread.start()
+ else:
+ self.write('\n' + self.title + '\n' + ('Logs: ' + clean(location) + '\n' if location and self.show_location else ''))
+ return self
+
+ def phase(self, label, done, total):
+ with self._lock:
+ self.stage, self.done, self.total = clean(label), done, total
+ self.event('phase', label=self.stage, done=done, total=total)
+ if not self.live:
+ self.write('[%d/%d] %s\n' % (done, total, self.stage))
+
+ def begin(self, key, label=None, *, detail='', total=None):
+ with self._lock:
+ self.tasks[key] = dict(label=clean(label or key), detail=clean(detail), total=total, done=0,
+ state='running', started=time.monotonic(), heartbeat=time.monotonic(),
+ estimate=ProgressEstimate(), scope=None, unit=None, event_tick=0.,
+ display_observed_at=time.monotonic())
+ self.event('task', key=str(key), label=self.tasks[key]['label'], detail=clean(detail), done=0, total=total,
+ elapsed_seconds=0, remaining_seconds=None)
+ if not self.live:
+ self.write(' > %s%s\n' % (self.tasks[key]['label'], ' · ' + clean(detail) if detail else ''))
+
+ def update(self, key, *, detail=None, done=None, total=None, resource=None, scope=None, rate=None,
+ unit=None, display_fraction=None, estimated=None, estimated_step_seconds=None, step_elapsed_seconds=None):
+ with self._lock:
+ row = self.tasks.get(key)
+ if row is None:
+ return
+ # A new scope (for example decoding after sampling) must not
+ # inherit the previous step's estimated fraction.
+ if scope is not None and scope != row.get('scope'):
+ row['display_fraction'] = None
+ row['estimated'] = False
+ row['estimated_step_seconds'] = None
+ row['step_elapsed_seconds'] = None
+ for name, value in [('detail', clean(detail) if detail is not None else None), ('done', done), ('total', total),
+ ('scope', scope), ('unit', unit), ('display_fraction', display_fraction),
+ ('estimated', estimated), ('estimated_step_seconds', estimated_step_seconds),
+ ('step_elapsed_seconds', step_elapsed_seconds)]:
+ if value is not None:
+ row[name] = value
+ now = time.monotonic()
+ row['display_observed_at'] = now
+ if done is not None or total is not None:
+ row['estimate'].update(row['done'], row['total'], scope=row['scope'], rate=rate, now=now)
+ if resource is not None:
+ self.resource = clean(resource)
+ if now - row['event_tick'] >= .5:
+ self.event('progress', key=str(key), label=row['label'], detail=row['detail'], done=row['done'], total=row['total'],
+ resource=self.resource, elapsed_seconds=now-row['started'], remaining_seconds=row['estimate'].remaining(now),
+ unit=row['unit'], display_fraction=row.get('display_fraction'), estimated=row.get('estimated', False),
+ estimated_step_seconds=row.get('estimated_step_seconds'),
+ step_elapsed_seconds=row.get('step_elapsed_seconds'))
+ row['event_tick'] = now
+ if not self.live and time.monotonic() - row['heartbeat'] >= 30:
+ self.write(' … %s · %s · %s%s\n' % (row['label'], duration(time.monotonic() - row['started']),
+ row['detail'] + self.eta_text(row), ' · ' + self.resource if self.resource else ''))
+ row['heartbeat'] = time.monotonic()
+
+ def end(self, key, *, success=True, detail=None):
+ with self._lock:
+ row = self.tasks.get(key)
+ if row is None:
+ return
+ row.update(state='complete' if success else 'failed', seconds=time.monotonic() - row['started'])
+ if success and row['total']:
+ row['done'] = row['total']
+ if detail is not None:
+ row['detail'] = clean(detail)
+ self.event('task_end', key=str(key), label=row['label'], detail=row['detail'], done=row['done'], total=row['total'],
+ state=row['state'], elapsed_seconds=row['seconds'], remaining_seconds=0 if success else None)
+ if not self.live:
+ self.write(' %s %s · %s · %s\n' % ('✓' if success else '✕', row['label'], duration(row['seconds']), row['detail']))
+
+ @staticmethod
+ def eta_text(row):
+ remaining = row['estimate'].remaining() if row['state'] == 'running' else None
+ if remaining == 0:
+ return ' · Finalizing'
+ return ' · ETA ~' + duration(math.ceil(remaining)) if remaining is not None else ''
+
+ def render(self, width=100, height=28):
+ with self._lock:
+ width = max(26, width - 2)
+ line = lambda s: clipped(s, width)
+ progress = '%s %d/%d steps complete' % (bar(self.done, self.total, min(28, width // 3)), self.done, self.total) if self.total else ''
+ lines = [self.paint(line(' FREEVIDEO / ' + self.title), '1;36'),
+ line(' ' + self.stage + ' · ' + duration(time.monotonic() - self.started)),
+ self.paint(line(' ' + progress), '35'), '']
+ tasks = list(self.tasks.values())
+ slots = max(1, (height - 10) // 2)
+ # Always keep parallel active work on screen, followed by recent results.
+ active = [r for r in tasks if r['state'] == 'running']
+ history = max(0, slots - len(active))
+ if not self.verbose and active:
+ history = min(2, history)
+ recent = [r for r in tasks if r['state'] != 'running'][-history:] if history else []
+ visible = (recent + active)[-slots:]
+ for row in visible:
+ running = row['state'] == 'running'
+ icon = SPINNER[int(time.monotonic() * 8) % len(SPINNER)] if running else ('✓' if row['state'] == 'complete' else '✕')
+ elapsed = time.monotonic() - row['started'] if running else row['seconds']
+ timing = ('Elapsed ' if running else '') + duration(elapsed) + self.eta_text(row)
+ title = clipped(row['label'], max(8, width-len(timing)-9))
+ lines.append(self.paint(line(' %s %s %s' % (icon, title, timing)), '36' if running else ('32' if row['state'] == 'complete' else '31')))
+ if row['total']:
+ display = row.get('display_fraction')
+ if not isinstance(display, (int, float)) or not math.isfinite(display):
+ display = row['done'] / row['total']
+ # Layer events are deliberately rate-limited in the
+ # worker. Once one warm step provides a duration, carry
+ # the visual estimate forward between events while the
+ # completed N/total counter remains unchanged.
+ if row.get('estimated') and isinstance(row.get('estimated_step_seconds'), (int, float)) \
+ and math.isfinite(row['estimated_step_seconds']) and row['estimated_step_seconds'] > 0 \
+ and isinstance(row.get('step_elapsed_seconds'), (int, float)) \
+ and math.isfinite(row['step_elapsed_seconds']):
+ age = max(0., time.monotonic() - row.get('display_observed_at', time.monotonic()))
+ local = min(.88, .88 * (row['step_elapsed_seconds'] + age) / row['estimated_step_seconds'])
+ display = max(display, (row['done'] + local) / row['total'])
+ display = min(1., max(0., display))
+ if row['unit'] == 'bytes':
+ counter = 'File %.0f%%' % min(100, 100 * display)
+ else:
+ prefix = '~' if row.get('estimated') else ''
+ counter = '%d/%d · %s%.0f%%' % (row['done'], row['total'], prefix, 100 * display)
+ detail = '%s %s ' % (bar(display, 1, min(18, width // 4)), counter)
+ else:
+ detail = ''
+ if running or row['state'] == 'failed' or self.verbose:
+ lines.append(line(' ' + detail + row['detail']))
+ footer = 'Files and diagnostics saved' if self._stop.is_set() else 'Ctrl+C to stop · run setup again to resume' if not self.show_location else 'Ctrl+C safely stops workers · all files retained'
+ lines += ['', line(' ' + self.resource)]
+ if self.show_location and self.location:
+ lines.append(self.paint(line(' Logs / outputs ' + self.location), '2'))
+ lines.append(self.paint(line(' ' + footer), '2'))
+ return lines
+
+ def _draw(self):
+ size = shutil.get_terminal_size((100, 28))
+ lines = self.render(min(112, size.columns), size.lines)
+ previous = '\x1b[%dA' % self._lines if self._lines else ''
+ self.write(previous + '\r\x1b[J' + '\n'.join(lines) + '\n')
+ self._lines = len(lines)
+
+ def _animate(self):
+ while not self._stop.wait(.15):
+ with self._lock:
+ self._draw()
+
+ def close(self):
+ self._stop.set()
+ if self._thread:
+ self._thread.join(timeout=2)
+ if self.live:
+ with self._lock:
+ self._draw()
+ self.write('\x1b[?25h')
+
+
+def model_download_name(filename):
+ name = str(filename).replace('\\', '/').rsplit('/', 1)[-1]
+ shard = re.fullmatch(r'diffusion_pytorch_model-(\d+)-of-(\d+)\.safetensors', name)
+ if shard:
+ index, total = map(int, shard.groups())
+ component = 'Video model' if total == 14 else 'Video decoder' if total == 3 else 'Model'
+ return '%s · shard %d / %d' % (component, index, total)
+ return name
+
+
+class LogProgress:
+ """Incrementally consume structured worker events and Ninja counters."""
+ def __init__(self, path):
+ self.path, self.offset, self.pending = Path(path), 0, ''
+ self.packages = {}
+ self.installing_packages = False
+ self.model_source = ''
+ self.model_backend = ''
+ self.parallel_tasks = {}
+ self.model_groups = None
+
+ def take_tasks(self):
+ tasks, self.parallel_tasks = self.parallel_tasks, {}
+ return tasks
+
+ def read(self, *, final=False):
+ result = {}
+ try:
+ with self.path.open(errors='replace', encoding='utf-8') as stream:
+ stream.seek(self.offset)
+ # Catch up after bursts without reading an entire log into RAM.
+ # After worker exit, consume the whole tail: its last ready or
+ # paused event must reach the UI before the next stage starts.
+ chunks = 0
+ while final or chunks < 8:
+ chunk = stream.read(65536)
+ self.offset = stream.tell()
+ if not chunk:
+ break
+ chunks += 1
+ result.update(self._consume(chunk))
+ except OSError:
+ pass
+ return result
+
+ def _consume(self, chunk):
+ lines = (self.pending + chunk).replace('\r', '\n').split('\n')
+ self.pending = lines.pop()
+ result = {}
+ for line in lines:
+ # uv does not emit byte progress to redirected logs. Show the real
+ # package phase and announced sizes, without inventing a percentage.
+ text = clean(line)
+ git = re.search(r'(Receiving objects|Resolving deltas|Updating files):\s*\d+%\s*\((\d+)/(\d+)\)', text)
+ if git:
+ phase, done, total = git.groups()
+ result.update(done=int(done), total=int(total), scope='git-' + phase, unit='items', detail=text)
+ package = re.fullmatch(r'Downloading ([\w.-]+) \(([\d.]+)(KiB|MiB|GiB)\)', text)
+ if package:
+ name, size, unit = package.groups()
+ self.packages[name] = float(size) * {'KiB': 2**10, 'MiB': 2**20, 'GiB': 2**30}[unit]
+ amount = sum(self.packages.values()) / 2**30
+ size_text = '%.1f GiB' % amount if amount >= 1 else '%.0f MiB' % (amount * 1024)
+ result.update(done=0, total=0, scope='package-download',
+ detail='Downloading packages · about %s across %d files' % (size_text, len(self.packages)))
+ elif re.match(r'Resolved \d+ packages? in ', text):
+ self.packages.clear()
+ self.installing_packages = False
+ result.update(done=0, total=0, scope='package-resolve', detail='Dependencies resolved · preparing downloads')
+ elif re.match(r'Prepared \d+ packages? in ', text):
+ self.installing_packages = True
+ result.update(done=0, total=0, scope='package-install', detail='Downloads ready · installing packages')
+ elif re.match(r'Installed \d+ packages? in ', text):
+ result.update(done=0, total=0, scope='package-finish', detail='Packages installed · finishing checks')
+ match = re.search(r'\[(\d+)/(\d+)\]', line)
+ if match:
+ self.installing_packages |= 'Installing wheels' in text
+ result.update(done=int(match[1]), total=int(match[2]),
+ detail='Installing packages' if self.installing_packages else 'Compiling CUDA kernels',
+ scope='package-install' if self.installing_packages else 'compile', unit='items')
+ try:
+ event = json.loads(line)
+ except (ValueError, TypeError):
+ continue
+ if not isinstance(event, dict):
+ continue
+ kind = event.get('event')
+ if kind == 'model_groups':
+ self.model_groups = event['groups']
+ continue
+ if kind in ('loaded', 'sampling_start', 'sampling_progress', 'step', 'sample_finalize'):
+ from .sampling_progress import progress_message
+ message = progress_message(event)
+ if message is not None:
+ detail = message['label']
+ if message.get('detail'):
+ detail += ' · ' + message['detail']
+ if kind == 'step':
+ detail += ' · %.2f s last step' % event['seconds']
+ result.update(done=message['done'], total=message['total'], detail=detail,
+ scope=message['phase'], unit='items',
+ display_fraction=message.get('display_fraction'),
+ estimated=message.get('estimated', False),
+ estimated_step_seconds=message.get('estimated_step_seconds'),
+ step_elapsed_seconds=message.get('step_elapsed_seconds'))
+ elif kind == 'prepared_blocks':
+ result.update(done=event['blocks'], total=50, detail='Load cached blocks and upload resident weights')
+ if event['blocks'] == 50:
+ result.update(done=0, total=0, detail='Finish model placement')
+ elif kind == 'model_ready':
+ if event.get('operation') == 'verify':
+ continue # Local verification cannot reset download ETA.
+ self.model_source = ''
+ self.model_backend = ''
+ result.update(done=0, total=0, detail='Verified ' + Path(event['file']).name, scope='verified', unit='items')
+ elif kind == 'prepare_fp8_phase':
+ work = 'Verify cached weights' if event['phase'] == 'verify_cache' else 'Read and convert source weights'
+ result.update(done=max(0, event['index'] - 1), total=event['total'],
+ detail=work + ' · ' + event['group'], scope='prepare-fp8-' + event['phase'], unit='items')
+ elif kind == 'prepared_fp8_group':
+ result.update(done=event.get('index', 0), total=event.get('total', 0),
+ detail=('Reuse verified FP8 ' if event.get('reused') else 'Prepare FP8 ') + event['group'], scope='prepare-fp8', unit='items')
+ elif kind == 'storage_compacted':
+ result.update(done=0, total=0, detail='Storage ready · %.2f GiB released' % (event['released_bytes'] / 2**30))
+ elif kind in ('verify_model', 'download_model', 'assemble_model'):
+ self.model_source = ''
+ self.model_backend = ''
+ result.update(done=0, total=0, scope=event['file'], unit='bytes',
+ detail={'verify_model': 'Verify ', 'download_model': 'Download ',
+ 'assemble_model': 'Assemble downloaded parts · '}[kind] + model_download_name(event['file']))
+ elif kind == 'network' and event.get('category') in ('models', 'vdn-models', 'edge-models') and event.get('action') == 'attempt':
+ self.model_backend = ''
+ self.model_source = {'modelscope': 'ModelScope', 'official': 'Hugging Face',
+ 'hf-mirror': 'HF Mirror', 'user': 'Custom source'}.get(event['source'], 'Model source')
+ result.update(done=0, total=0, scope='connecting-' + self.model_source, unit='bytes',
+ detail='Connecting to ' + self.model_source + ' · ' + event['file'])
+ elif kind == 'network' and event.get('action') == 'backend':
+ self.model_backend = {'hf-xet': 'HF Xet', 'modelscope-ranges': 'ModelScope parallel',
+ 'http-ranges': 'Parallel HTTP', 'hf-http': 'HF HTTP'}.get(event.get('method'), '')
+ elif kind == 'network' and event.get('action') == 'manual-switch':
+ self.model_source=event.get('source','')
+ self.model_backend=''
+ detail='Switched current download to '+self.model_source
+ if event.get('retained_unmapped_bytes'):
+ detail+=' · Restarting this file; incompatible fragments retained on disk'
+ else:
+ detail+=' · Resuming %.1f MiB retained' % (event.get('resume_bytes',0)/2**20)
+ self.last_notice=detail
+ result.update(done=0,total=0,rate=None,scope='manual-switch',unit='bytes',detail=detail)
+ elif kind == 'network' and event.get('action') in ('retry', 'route-retry', 'resume-refused', 'paused', 'restart-approved'):
+ action = event['action']
+ label = {'retry': 'Retrying same source', 'resume-refused': 'Resume unavailable; checking another source',
+ 'route-retry': 'Retrying same source with ' + ('direct connection' if event.get('route') == 'direct' else 'configured proxy'),
+ 'paused': 'Paused; existing data kept', 'restart-approved': 'Restart explicitly allowed'}[action]
+ detail = '%s · %s · %.1f MiB retained · %s' % (label, event.get('source', ''),
+ event.get('resume_bytes', event.get('retained_bytes', 0))/2**20, event.get('file', ''))
+ if action == 'retry':
+ detail += ' · retry %d in %ds' % (event['retry_number'], event['wait_seconds'])
+ if action == 'paused' and event.get('reason') == 'xet-or-legacy-resume-unavailable':
+ # Xet can preallocate the entire file: its logical size is
+ # not a reliable count of bytes actually downloaded.
+ detail = label + ' · Cannot resume this format; enable restart in Setup · ' + event.get('file', '')
+ self.last_notice = detail if action == 'paused' else None
+ result.update(done=0, total=0, rate=None, scope=action, unit='bytes', detail=detail)
+ elif kind == 'network' and event.get('category') in ('models', 'vdn-models', 'edge-models') and event.get('action') in ('fallback', 'verifying'):
+ action = event['action']
+ self.model_backend = ''
+ detail = ('Verify %s · ' % event.get('algorithm', 'integrity').upper() if action == 'verifying'
+ else 'Download response too large · switching source · ' if event.get('reason') == 'response-size-exceeded'
+ else 'Download memory budget reached · switching to streaming · ' if event.get('reason') in ('memory-guard', 'low-live-memory-use-streaming')
+ else 'Download source failed · trying another source · ')
+ result.update(done=0, total=0, rate=None, scope=action + '-' + event['file'],
+ unit='bytes', detail=detail + event['file'])
+ elif kind == 'decode_phase':
+ result.update(done=0, total=0, detail=event['phase'])
+ elif kind == 'local_model_progress':
+ from .desktop_runtime import local_model_ui
+ local = local_model_ui(event)
+ result.update(done=local['done'], total=local['total'], rate=event.get('bytes_per_second'),
+ scope='local-' + event.get('file', ''), unit='bytes', detail=local['label'] + ' · ' + local['detail'])
+ elif kind == 'model_verification_progress':
+ state = event.get('state', 'running')
+ active = state == 'running'
+ self.parallel_tasks['verify-models'] = dict(label='Verify / reuse local models', state=state,
+ done=event.get('done_bytes', 0) if active else event.get('files_done', 0),
+ total=event.get('total_bytes', 0) if active else event.get('files_total', 0),
+ rate=event.get('bytes_per_second') if active else None,
+ unit='bytes' if active else 'items', scope='local-' + event.get('file', ''),
+ detail=('%d / %d files · %.1f MiB/s · %s' %
+ (event.get('files_done', 0), event.get('files_total', 0),
+ event.get('bytes_per_second', 0)/2**20, event.get('file', '')))
+ if active else 'Local models verified' if state == 'complete' else 'Local verification stopped; files retained')
+ elif kind == 'download_progress':
+ done, total = event['done_bytes'], event.get('total_bytes') or 0
+ description = event.get('description', 'download')
+ if total and done > total:
+ # Older workers and invalid backend counters must not look
+ # like successful completion. Retain the reported numbers
+ # as an explicit error, with no percentage or estimate.
+ result.update(done=0, total=0, rate=None, scope='invalid-' + description, unit='bytes',
+ detail='Download counter mismatch · %.1f MiB reported / %.1f MiB expected · %s' %
+ (done/2**20, total/2**20, description))
+ continue
+ self.model_backend = {'hf-xet': 'HF Xet', 'modelscope-ranges': 'ModelScope parallel',
+ 'hf-http': 'HF HTTP'}.get(event.get('method'), self.model_backend)
+ source = self.model_backend or self.model_source
+ speed = event.get('network_bytes_per_second')
+ shown_speed = speed if speed is not None else event['bytes_per_second']
+ result.update(done=done, total=total, scope=description + self.model_source, rate=event['bytes_per_second'], unit='bytes',
+ detail='%s%s%.1f MiB/s · %.1f / %.1f MiB · %s' %
+ (source + ' · ' if source else '',
+ '~' if event.get('counter_source') == 'uv-terminal' else '', shown_speed/2**20,
+ done/2**20, total/2**20, model_download_name(event.get('description', 'Download'))))
+ return result
diff --git a/freevideo_engine/test_launcher.py b/freevideo_engine/test_launcher.py
new file mode 100644
index 0000000..b2376b5
--- /dev/null
+++ b/freevideo_engine/test_launcher.py
@@ -0,0 +1,11 @@
+"""Resolve the installed test environment without shell activation."""
+import sys
+from .managed import main as managed
+
+
+def main():
+ return managed(['test', *sys.argv[1:]])
+
+
+if __name__ == '__main__':
+ raise SystemExit(main())
diff --git a/freevideo_engine/test_prompts.json b/freevideo_engine/test_prompts.json
new file mode 100644
index 0000000..a82d61f
--- /dev/null
+++ b/freevideo_engine/test_prompts.json
@@ -0,0 +1,4 @@
+[
+ "integrated_multimodal_description: [Shot 1] A sunlit city boulevard glistens after a summer shower. An athletic young adult woman wearing a bright yellow windbreaker, black trousers, white sneakers, and a matte white skate helmet rides a skateboard down the center of the empty street. The camera tracks beside her at knee height, with the spinning wheels sharp and storefront reflections streaking across the wet asphalt. Ahead of her, the entire street begins to curl upward like a flexible ribbon. Road markings, sidewalks, streetlights, and intact buildings bend together along the same rising curve. She notices the slope, grins, bends her knees, and accelerates toward it. [Shot 2] At 00:04.000, the camera cuts to a wide side view as the boulevard rises into a thirty-meter-high curling wave of asphalt and architecture. She rides diagonally up its smooth inner face, one hand trailing just above the painted lane markings, her yellow jacket snapping in the wind. The camera sweeps alongside her and then rises, revealing rows of windows sparkling across the enormous curved city surface. She reaches the crest and balances along its narrow ridge, with the sunlit skyline stretching behind her. During the final two seconds, the camera settles into a wide composition that makes her small silhouette clearly readable against the monumental wave.\n\noverall_soundscape: Skateboard wheels rattle over pavement seams, then produce a smooth, rising hum as the street curves upward. Wind rushes past her jacket, while a deep, elastic architectural resonance accompanies the bending city. There is no dialogue.\n\nnon_diegetic_music: A nimble electronic beat begins with the skateboard's movement. Sweeping strings and a bright brass accent enter as the camera reveals the full wave, ending on an exhilarating sustained chord.",
+ "A continuous cinematic shot of a small wooden fishing boat crossing a calm mountain lake at sunrise. Mist drifts above the water. The camera slowly tracks alongside the boat, with gentle ripples, birds calling in the distance, and quiet water sounds. Natural lighting, realistic motion, no text or subtitles."
+]
diff --git a/freevideo_engine/testing.py b/freevideo_engine/testing.py
new file mode 100644
index 0000000..00f91a1
--- /dev/null
+++ b/freevideo_engine/testing.py
@@ -0,0 +1,551 @@
+"""Sequential full text-to-video stability tests; retain every artifact locally."""
+import argparse
+import csv
+import html
+import json
+import math
+import os
+from pathlib import Path
+import signal
+import shutil
+import subprocess
+import sys
+import time
+import traceback
+from urllib.parse import quote
+
+from .bootstrap import inventory, digest, GiB, PACKAGE, DEFAULT_ROOT, existing_parent
+from .geometry import geometry
+from .locking import runtime_lock, LOCK_ENV
+from .monitoring import Monitor, save
+from .ram import ProcessMemory
+from .terminal_ui import TerminalUI, LogProgress
+from .validation import read_case_reports, validate_metrics, validate_artifacts
+from . import processes
+from .system import (windows, system_memory, cpu_info, nvidia_smi, memory_peak, memory_sample,
+ inference_headroom, inference_memory_sample)
+
+
+def case_plan(suite='standard'):
+ prompts = json.loads((PACKAGE / 'test_prompts.json').read_text(encoding='utf-8'))
+ w, h, frames = (768, 448, 90) if suite == 'quick' else (1344, 768, 243)
+ specs = [('01-first', 'first request; cache state observed, not forced cold', w, h, frames, prompts[0]),
+ ('02-repeat', 'second request, identical prompt and seed', w, h, frames, prompts[0]),
+ ('03-new-prompt', 'third request, changed prompt', w, h, frames, prompts[1]),
+ ('04-resolution', 'changed resolution only, relative to first request',
+ 512 if suite == 'quick' else 1024, 288 if suite == 'quick' else 576, frames, prompts[0]),
+ ('05-duration', 'changed duration only, relative to first request', w, h,
+ 56 if suite == 'quick' else 124, prompts[0])]
+ if suite == 'stress':
+ specs += [('06-higher-resolution', 'larger canvas stress case', 1536, 864, 243, prompts[0]),
+ ('07-longer', 'longer duration stress case', 1344, 768, 362, prompts[0])]
+ return [dict(id=name, purpose=purpose, width=w, height=h, frames=f, prompt=p, seed=2026090901)
+ for name, purpose, w, h, f, p in specs]
+
+
+def validate_cases(cases):
+ if not isinstance(cases, list) or not cases:
+ raise ValueError('Case file must contain a nonempty JSON list.')
+ ids = set()
+ for case in cases:
+ name = case.get('id') if isinstance(case, dict) else None
+ if not isinstance(name, str) or not name or any(c not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_' for c in name) or name in ids:
+ raise ValueError('Each case needs a unique path-safe id.')
+ ids.add(name)
+ if not isinstance(case.get('prompt'), str) or not case['prompt'].strip():
+ raise ValueError('Every test case must include a nonempty raw text prompt.')
+ geometry(case.get('width', 1344), case.get('height', 768),
+ frames=case.get('frames'), seconds=case.get('seconds'))
+ return cases
+
+
+def cgroup_snapshot():
+ if windows():
+ return {'unavailable': 'Linux cgroups do not apply to native Windows; no hard capacity limit was created.'}
+ try:
+ group = next(line.split(':', 2)[2] for line in Path('/proc/self/cgroup').read_text(encoding='utf-8').splitlines() if line.startswith('0::'))
+ root = Path('/sys/fs/cgroup') / group.lstrip('/')
+ result = {'path': str(root), 'scope': 'Existing shared cgroup; peaks are lifetime values, not case attribution.'}
+ for name in ('memory.current', 'memory.peak', 'memory.max', 'memory.swap.current', 'memory.swap.max'):
+ path = root / name
+ raw = path.read_text(encoding='utf-8').strip() if path.is_file() else 'max'
+ result[name] = None if raw == 'max' else int(raw)
+ result['events'] = dict((k, int(v)) for k, v in (line.split() for line in (root / 'memory.events').read_text(encoding='utf-8').splitlines()))
+ return result
+ except (OSError, ValueError, StopIteration) as error:
+ return {'unavailable': repr(error)}
+
+
+def swap_snapshot():
+ if windows():
+ return {} # Windows commit is captured in each RAM sample, not Linux swap counters.
+ return {k: int(v) for k, v in (line.split() for line in Path('/proc/vmstat').read_text(encoding='utf-8').splitlines())
+ if k in ('pswpin', 'pswpout', 'pgmajfault', 'oom_kill')}
+
+
+def stop_tree(process):
+ if windows():
+ return processes.stop(process, grace=20)
+ import psutil
+ try:
+ children = psutil.Process(process.pid).children(recursive=True)
+ except psutil.NoSuchProcess:
+ children = []
+ # generate catches SIGTERM and waits for its separate encoder/video group.
+ if process.poll() is None:
+ process.terminate()
+ try:
+ process.wait(timeout=20)
+ except subprocess.TimeoutExpired:
+ process.kill()
+ process.wait()
+ for child in children:
+ try:
+ if child.is_running():
+ child.kill()
+ except psutil.NoSuchProcess:
+ pass
+ psutil.wait_procs(children, timeout=5)
+
+
+class CaseProgress:
+ def __init__(self, directory, ui):
+ self.directory, self.ui = directory, ui
+ self.log = LogProgress(directory / 'video.engine.log')
+ self.phase = None
+ self.ram_peak = None
+
+ def update(self, sample, monitor):
+ phase = 'Starting request'
+ for name, stages in [('encoding', {'encoder_import': 'Loading encoder libraries', 'encoder_load': 'Loading text encoder',
+ 'encode': 'Encoding prompt'}),
+ ('engine', {'load': 'Loading transformer', 'sample': 'Denoising', 'decode': 'Decoding video and audio',
+ 'complete': 'Writing request report'})]:
+ try:
+ value = json.loads((self.directory / ('video.' + name + '.json')).read_text(encoding='utf-8'))
+ phase = stages.get(value.get('phase'), 'Releasing text encoder' if name == 'encoding' else phase)
+ except (OSError, ValueError):
+ pass
+ if phase != self.phase:
+ self.ui.update(self.directory.name, done=0, total=8 if phase == 'Denoising' else 0, detail=phase)
+ self.phase = phase
+ current_ram = memory_sample(sample)
+ if current_ram is not None:
+ self.ram_peak = max(self.ram_peak or 0, current_ram)
+ now = '—' if current_ram is None else '%.2f' % (current_ram/GiB)
+ peak = '—' if self.ram_peak is None else '%.2f' % (self.ram_peak/GiB)
+ self.ui.update(self.directory.name, **self.log.read(), resource='GPU %.2f GiB · peak %.2f GiB · RAM now %s GiB · peak %s GiB · available %.2f GiB' %
+ ((monitor.last if monitor else 0)/GiB, (monitor.peak if monitor else 0)/GiB,
+ now, peak, inference_headroom(sample)/GiB))
+
+
+def run_process(command, directory, env, gpu, timeout, minimum_ram, *, sample_gpu=True, pass_fds=(), ui=None,
+ maximum_gpu=None, maximum_working_ram=None):
+ memory = ProcessMemory()
+ monitor = None
+ gpu_error = None
+ if sample_gpu:
+ try:
+ monitor = Monitor(directory / 'gpu.csv', device=gpu).start()
+ except Exception as error:
+ gpu_error = repr(error)
+ initial_cgroup, initial_swap = cgroup_snapshot(), swap_snapshot()
+ tick = time.perf_counter()
+ status = 'failed'
+ reason = None
+ process = None
+ progress = CaseProgress(directory, ui) if ui else None
+ try:
+ with (directory / 'run.log').open('w', encoding='utf-8') as log, (directory / 'ram.jsonl').open('w', buffering=1, encoding='utf-8') as samples:
+ process = processes.popen(command, stdout=log, stderr=subprocess.STDOUT, env=env,
+ start_new_session=True, pass_fds=pass_fds)
+ heartbeat = time.monotonic()
+ while process.poll() is None:
+ sample = memory.sample(process.pid)
+ samples.write(json.dumps(dict(sample, epoch_seconds=time.time(), elapsed_seconds=time.perf_counter()-tick)) + '\n')
+ if progress:
+ progress.update(sample, monitor)
+ if ((maximum_gpu is not None and monitor and monitor.peak > maximum_gpu)
+ or (maximum_working_ram is not None and inference_memory_sample(sample) is not None
+ and inference_memory_sample(sample) > maximum_working_ram)):
+ reason, status = 'Optimization trial exceeded its observed memory budget.', 'trial_memory_guard'
+ stop_tree(process)
+ break
+ if memory_sample(sample) is None:
+ reason, status = 'Process-tree memory monitoring became incomplete; details retained in ram.jsonl.', 'ram_monitor_failed'
+ stop_tree(process)
+ break
+ if inference_headroom(sample) <= 0 or inference_headroom(sample) < minimum_ram:
+ reason, status = 'Available system RAM crossed the configured emergency floor.', 'ram_guard'
+ stop_tree(process)
+ break
+ if time.perf_counter() - tick > timeout:
+ reason, status = 'Case exceeded timeout.', 'timeout'
+ stop_tree(process)
+ break
+ try:
+ # Short model-load peaks can disappear between 500 ms
+ # samples. Keep this bounded; it is observed, not exact, RAM.
+ process.wait(timeout=.2)
+ except subprocess.TimeoutExpired:
+ pass
+ if not ui and time.monotonic() - heartbeat >= 30:
+ print('[%s] %.0fs; RAM now %.2f GiB; peak %.2f GiB; available %.2f GiB' %
+ (directory.name, time.perf_counter()-tick, (memory_sample(sample) or 0)/GiB,
+ (memory_peak(memory.result()) or 0)/GiB,
+ inference_headroom(sample)/GiB), flush=True)
+ heartbeat = time.monotonic()
+ if reason is None:
+ status = 'complete' if process.returncode == 0 else 'failed'
+ except BaseException as error:
+ reason, status = repr(error), 'interrupted' if isinstance(error, KeyboardInterrupt) else 'failed'
+ if process is not None:
+ stop_tree(process)
+ finally:
+ measured = monitor.stop() if monitor else {'sampling_errors': [gpu_error or 'GPU sampling disabled'], 'gpu_peak_bytes': None}
+ elapsed = time.perf_counter() - tick
+ final_cgroup, final_swap = cgroup_snapshot(), swap_snapshot()
+ return {'status': status, 'error': reason, 'returncode': process.returncode if process else None,
+ 'wall_seconds': elapsed, 'gpu': measured,
+ 'ram': dict(memory.result(), ram_poll_interval_seconds=.2),
+ 'cgroup_before': initial_cgroup, 'cgroup_after': final_cgroup,
+ 'cgroup_event_delta': {k: v - initial_cgroup.get('events', {}).get(k, 0)
+ for k, v in final_cgroup.get('events', {}).items()},
+ 'system_vmstat_delta': {k: final_swap[k] - initial_swap[k] for k in final_swap},
+ 'hard_memory_limits_created': False,
+ 'timing_scope': 'Fresh request process: text encoding, model loads, sampling, decode, all artifact writes and process startup/exit. Media validation and artifact hashing excluded.'}
+
+
+def environment(machine):
+ """The installation's defaults, not an override of the caller's choices.
+
+ The recorded GPU UUID keeps the host and its children on the device that
+ passed initialization and survives a Windows display-adapter reorder, so it
+ is the right default. Forcing it also pins the installation to one physical
+ GPU: a caller that selected another device silently lost it, and on any
+ other machine that UUID does not exist, so CUDA reported no device at all.
+ The compiler caches behave the same way; several measurements on one host
+ need their own.
+ """
+ inherited = lambda name, fallback: os.environ.get(name) or fallback
+ cache = Path(machine['root']) / 'kernel-cache'
+ return dict(os.environ, FREEVIDEO_HOME=machine['root'], FREEVIDEO_VDN_ROOT=machine['vdn_root'],
+ PATH=(str(Path(machine['git']).parent) + os.pathsep if machine.get('git') else '') + os.environ.get('PATH', ''),
+ FREEVIDEO_MODEL_ROOT=machine['model_root'], FREEVIDEO_COMFY_ROOT=machine['comfy_root'],
+ FREEVIDEO_COMFY_PYTHON=machine['comfy_python'],
+ CUDA_VISIBLE_DEVICES=inherited('CUDA_VISIBLE_DEVICES', machine['gpu_uuid']),
+ PYTHONUNBUFFERED='1', PYTHONUTF8='1', PYTHONIOENCODING='utf-8',
+ FREEVIDEO_LOCK_PATH=inherited('FREEVIDEO_LOCK_PATH', str(Path(machine['root']) / 'engine.lock')),
+ TRITON_CACHE_DIR=inherited('TRITON_CACHE_DIR', str(cache / 'triton')),
+ CUDA_CACHE_PATH=inherited('CUDA_CACHE_PATH', str(cache / 'cuda')),
+ TORCHINDUCTOR_CACHE_DIR=inherited('TORCHINDUCTOR_CACHE_DIR', str(cache / 'inductor')),
+ OMP_NUM_THREADS='8', MKL_NUM_THREADS='8')
+
+
+def command_for(machine, case, destination, args):
+ command = [machine['python'], '-m', 'freevideo_engine', 'generate', '--prompt-file', str(destination / 'prompt.txt'),
+ '--cache', machine['cache'], '--base', machine['base'], '--checkpoint', machine['checkpoint'],
+ '--encoder-python', machine['comfy_python'], '--encoder-root', machine['comfy_root'],
+ '--model-paths', machine['model_paths'], '--encoder', machine['encoder'],
+ '--out', str(destination / 'video.mp4'), '--seed', str(case.get('seed', 2026090901)),
+ '--width', str(case.get('width', 1344)), '--height', str(case.get('height', 768)),
+ '--attention', args.attention]
+ if getattr(args, 'no_tuning', False):
+ command.append('--no-tuning')
+ if case.get('seconds') is not None:
+ command += ['--seconds', str(case['seconds'])]
+ else:
+ command += ['--frames', str(case.get('frames', 243))]
+ for key in ('vram_gib', 'ram_gib'):
+ value = getattr(args, key) if getattr(args, key) is not None else machine.get(key)
+ if value is not None:
+ command += ['--' + key.replace('_', '-'), str(value)]
+ return command
+
+
+def artifact_manifest(directory):
+ files = []
+ for path in sorted(directory.rglob('*')):
+ if path.is_file() and path.name != 'artifacts.json':
+ files.append({'path': path.relative_to(directory).as_posix(), 'bytes': path.stat().st_size, 'sha256': digest(path)})
+ save(directory / 'artifacts.json', {'files': files, 'total_bytes': sum(f['bytes'] for f in files)})
+
+
+def artifact_estimate(cases):
+ canvases = [geometry(c.get('width', 1344), c.get('height', 768), frames=c.get('frames'), seconds=c.get('seconds')) for c in cases]
+ raw_bytes = sum(c['width'] * c['height'] * c['frames'] * 3 for c in canvases)
+ # RGB is the largest retained output; include latents, audio, MP4 and logs.
+ return int(raw_bytes * 1.5 + len(cases) * 128 * 2**20 + GiB)
+
+
+def flatten(row):
+ g, request = row['geometry'], row.get('request', {})
+ video, encoding = row.get('engine', {}), row.get('encoding', {})
+ gpu, ram = row.get('gpu', {}), row.get('ram', {})
+ profile = request.get('profile', {})
+ return dict(case=row['id'], status=row['status'], width=g['width'], height=g['height'],
+ frames=g['frames'], duration_seconds=g['seconds'], wall_seconds=row.get('wall_seconds'),
+ text_encode_seconds=encoding.get('work_seconds'), conditioning_cache_hit=bool(encoding.get('cache_hit')),
+ tuning_profile=request.get('tuning', {}).get('profile_id'),
+ baseline_wall_seconds=(row.get('comparison') or {}).get('baseline_wall_seconds'),
+ wall_gain_percent=(row.get('comparison') or {}).get('wall_gain_percent'),
+ sampling_gain_percent=(row.get('comparison') or {}).get('sampling_gain_percent'),
+ model_load_seconds=video.get('load_seconds'),
+ sample_seconds=video.get('sample_seconds'), vae_load_seconds=video.get('vae_load_seconds'),
+ video_decode_seconds=video.get('video_decode_seconds'), audio_decode_seconds=video.get('audio_load_decode_seconds'),
+ mp4_save_seconds=video.get('encode_seconds'), latent_save_seconds=video.get('latent_save_seconds'),
+ rgb_audio_save_seconds=video.get('decoded_artifact_save_seconds'),
+ gpu_baseline_gib=gpu.get('gpu_baseline_bytes', 0)/GiB,
+ gpu_peak_gib=gpu['gpu_peak_bytes']/GiB if gpu.get('gpu_peak_bytes') is not None else None,
+ process_peak_rss_gib=ram.get('process_tree_peak_rss_bytes', 0)/GiB,
+ process_peak_pss_gib=ram['process_tree_peak_pss_bytes']/GiB if ram.get('process_tree_peak_pss_bytes') is not None else None,
+ process_peak_private_commit_gib=ram['process_tree_peak_private_commit_bytes']/GiB if ram.get('process_tree_peak_private_commit_bytes') is not None else None,
+ process_peak_private_working_set_gib=ram['process_tree_peak_private_working_set_bytes']/GiB if ram.get('process_tree_peak_private_working_set_bytes') is not None else None,
+ process_peak_guard_gib=(memory_peak(ram) or 0)/GiB, ram_guard_metric=ram.get('ram_guard_metric', 'PSS'),
+ system_min_commit_available_gib=ram['system_min_commit_available_bytes']/GiB if ram.get('system_min_commit_available_bytes') is not None else None,
+ system_min_available_gib=(ram.get('system_min_available_bytes') or 0)/GiB,
+ attention=profile.get('engine', {}).get('attention'), error=row.get('error'))
+
+
+def report(directory, value):
+ save(directory / 'report.json', value)
+ flat = [flatten(row) for row in value['cases']]
+ if flat:
+ with (directory / 'report.csv').open('w', encoding='utf-8-sig', newline='') as stream:
+ writer = csv.DictWriter(stream, fieldnames=list(flat[0]))
+ writer.writeheader()
+ writer.writerows(flat)
+ columns = ('case', 'status', 'width', 'height', 'frames', 'wall_seconds', 'text_encode_seconds',
+ 'conditioning_cache_hit', 'sample_seconds', 'wall_gain_percent', 'sampling_gain_percent',
+ 'gpu_peak_gib', 'process_peak_guard_gib', 'ram_guard_metric', 'system_min_available_gib')
+ def cell(v):
+ return '—' if v is None else ('%.2f' % v if isinstance(v, float) else html.escape(str(v)))
+ labels = {'gpu_peak_gib': 'GPU peak (GiB)', 'process_peak_guard_gib': 'RAM peak (GiB)',
+ 'ram_guard_metric': 'RAM measurement', 'system_min_available_gib': 'System available minimum (GiB)'}
+ table = '<tr>' + ''.join('<th>' + html.escape(labels.get(k, k)) + '</th>' for k in columns) + '</tr>'
+ table += ''.join('<tr>' + ''.join('<td>' + cell(row.get(k)) + '</td>' for k in columns) + '</tr>' for row in flat)
+ media = ''
+ for row in value['cases']:
+ path = directory / row['id'] / 'video.mp4'
+ media += '<h2>%s — %s</h2><p>%s</p>' % (html.escape(row['id']), html.escape(row['status']), html.escape(row.get('purpose', '')))
+ if path.is_file():
+ relative = quote(path.relative_to(directory).as_posix())
+ media += '<video controls preload="metadata" src="%s"></video>' % relative
+ media += '<p><a href="%s/case.json">Metrics</a> · <a href="%s/run.log">Log</a> · <a href="%s/artifacts.json">All artifacts</a></p>' % ((quote(row['id']),) * 3)
+ if row.get('error'):
+ media += '<pre>' + html.escape(row['error']) + '</pre>'
+ (directory / 'report.html').write_text('<!doctype html><html lang="en"><meta charset="utf-8"><title>FreeVideo test report</title>'
+ '<style>body{font:16px system-ui;margin:32px;background:#111827;color:#e5e7eb}a{color:#93c5fd}'
+ 'table{border-collapse:collapse;font-size:13px}td,th{padding:8px;border:1px solid #4b5563}video{max-width:800px;width:100%}'
+ 'pre{white-space:pre-wrap}section{overflow:auto}</style><h1>FreeVideo test report</h1>'
+ '<p>Status: ' + html.escape(value['status']) + '. All videos, audio, raw RGB, latents, conditions and logs are retained locally.</p>'
+ '<p>Whole GPU usage includes other applications. Linux uses PSS; Windows separately records working set and private commit, and guards their maximum. '
+ 'Neither includes all system file cache. Windows available memory also checks commit headroom. RAM/VRAM budgets are placement guidance; '
+ 'these runs create no hard capacity limit. Each request reloads models; validated conditioning reuse is shown separately. '
+ 'Gains compare matched prompts, seeds, geometry, base placement and dependencies from the optimization baseline; OS/JIT cache and storage warmth can affect wall time.</p>'
+ '<p><a href="report.json">Full JSON</a> · <a href="report.csv">CSV</a> · <a href="inventory.json">Machine and versions</a></p>'
+ '<section><table>' + table + '</table></section>' + media + '</html>', encoding='utf-8')
+
+
+def provenance(machine, env):
+ result = inventory(machine['gpu_uuid'])
+ result['machine_configuration'] = machine
+ result['source_files'] = {p.relative_to(PACKAGE.parent).as_posix(): digest(p)
+ for p in sorted([*PACKAGE.glob('*.py'), *PACKAGE.glob('*.json')])}
+ result['cpu_info'] = cpu_info()
+ result['dependency_revisions'] = json.loads((PACKAGE / 'dependencies.json').read_text(encoding='utf-8'))
+ result['fp8_cache_manifest_sha256'] = digest(Path(machine['cache']) / 'manifest.json')
+ for name, command in [('git_head', ['git', '-C', str(PACKAGE.parent), 'rev-parse', 'HEAD']),
+ ('git_status', ['git', '-C', str(PACKAGE.parent), 'status', '--porcelain']),
+ ('nvidia_smi', [nvidia_smi(), '-q']),
+ ('engine_packages', [machine['python'], '-m', 'pip', 'freeze']),
+ ('encoder_packages', [machine['comfy_python'], '-m', 'pip', 'freeze'])]:
+ p = subprocess.run(command, capture_output=True, text=True, env=env, timeout=30, encoding='utf-8', errors='replace')
+ result[name] = {'returncode': p.returncode, 'stdout': p.stdout, 'stderr': p.stderr}
+ return result
+
+
+def _main(argv=None):
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--root', type=Path, default=Path(os.environ.get('FREEVIDEO_HOME', DEFAULT_ROOT)), help='Setup installation directory')
+ parser.add_argument('--config', type=Path, help='Override root/machine.json')
+ parser.add_argument('--suite', choices=('standard', 'quick', 'stress'), default='standard')
+ parser.add_argument('--cases', type=Path, help='Custom JSON list of raw-text cases')
+ parser.add_argument('--out', type=Path, help='New report directory; existing paths are refused')
+ parser.add_argument('--plan', action='store_true')
+ parser.add_argument('--json', action='store_true', help='Print --plan as JSON')
+ parser.add_argument('--plain', action='store_true', help='Disable live terminal rendering')
+ parser.add_argument('--no-color', action='store_true', help='Disable colors (also respects NO_COLOR)')
+ parser.add_argument('--timeout-seconds', type=float, default=3600)
+ parser.add_argument('--min-available-ram-gib', type=float, default=.25)
+ parser.add_argument('--vram-gib', type=float)
+ parser.add_argument('--ram-gib', type=float)
+ parser.add_argument('--attention', default='auto')
+ parser.add_argument('--no-tuning', action='store_true', help='Bypass saved optimization and text reuse for a baseline run')
+ args = parser.parse_args(argv)
+ if args.json and not args.plan:
+ parser.error('--json requires --plan')
+ if not all(math.isfinite(v) for v in (args.timeout_seconds, args.min_available_ram_gib)) or args.timeout_seconds <= 0 or args.min_available_ram_gib < 0:
+ parser.error('Use a positive timeout and a nonnegative emergency RAM floor.')
+ config = (args.config or args.root / 'machine.json').expanduser().resolve()
+ machine = json.loads(config.read_text(encoding='utf-8'))
+ if not machine.get('ready'):
+ raise ValueError('Setup is incomplete.')
+ cases = validate_cases(json.loads(args.cases.read_text(encoding='utf-8')) if args.cases else case_plan(args.suite))
+ destination = (args.out or Path(machine['root']) / 'test-runs' / (time.strftime('%Y%m%dT%H%M%SZ', time.gmtime()) + '-' + str(os.getpid()))).resolve()
+ disk_free, disk_needed = shutil.disk_usage(existing_parent(destination)).free, artifact_estimate(cases)
+ ui = TerminalUI('Engine test / ' + (args.suite if not args.cases else 'custom'), plain=args.plain, no_color=args.no_color)
+ test_plan = {'cases': cases, 'geometry': [geometry(c.get('width', 1344), c.get('height', 768), frames=c.get('frames'), seconds=c.get('seconds')) for c in cases],
+ 'estimated_artifact_bytes': disk_needed, 'disk_free_bytes': disk_free, 'output': str(destination),
+ 'artifacts': 'All retained', 'scope': 'Native text conditioning (validated reuse when enabled), complete default 8 + 2 video/audio generation.'}
+ if args.json:
+ print(json.dumps(test_plan, indent=2))
+ else:
+ ui.panel('FreeVideo / Engine test plan', [('GPU', machine['gpu_uuid']),
+ ('Cases', '%d · text encode → 8 + 2 denoising steps → video + audio → validation' % len(cases)),
+ ('Cache', 'Fresh request processes; persistent compatible inputs ' + ('bypassed' if args.no_tuning else 'reused automatically; no eviction')),
+ ('Disk', '~%.2f GiB for retained artifacts · %.2f GiB free' % (disk_needed/GiB, disk_free/GiB)),
+ ('Outputs', destination)] + [(c['id'], '%d×%d · %d frames · %.2fs' % (g['width'], g['height'], g['frames'], g['seconds']))
+ for c, g in zip(cases, test_plan['geometry'])])
+ if disk_free < disk_needed:
+ print('Insufficient free disk space for all retained test artifacts.', file=sys.stderr)
+ return 1
+ if args.plan:
+ return 0
+ env = environment(machine)
+ with runtime_lock(path=Path(env['FREEVIDEO_LOCK_PATH'])) as fd:
+ if json.loads(config.read_text(encoding='utf-8')) != machine:
+ raise ValueError('Installation changed during preflight. Rerun the test command.')
+ return run_suite(args, machine, cases, destination, ui, env, fd)
+
+
+def run_suite(args, machine, cases, destination, ui, env, fd):
+ destination.mkdir(parents=True, exist_ok=False)
+ value = {'schema_version': 1, 'suite': args.suite if not args.cases else 'custom', 'status': 'running',
+ 'created_epoch': time.time(), 'cases': [], 'plan': cases,
+ 'artifacts_retained': True, 'full_model_residency_between_requests': False,
+ 'cache_policy': 'Natural OS file and persistent JIT caches; no global cache flushing; first is not claimed storage-cold.',
+ 'physical_gpu_scope': 'Measured on the GPU in inventory.json. Nominal capacity overrides do not emulate another GPU.'}
+ save(destination / 'plan.json', value)
+ report(destination, value)
+ def interrupted(signum, frame):
+ raise KeyboardInterrupt('Test interrupted by signal %s' % signum)
+ previous = processes.termination_handler(interrupted)
+ try:
+ ui.start(destination)
+ ui.phase('Capture hardware, source and dependency versions', 0, len(cases))
+ save(destination / 'inventory.json', provenance(machine, env))
+ for index, case in enumerate(cases):
+ directory = destination / case['id']
+ directory.mkdir()
+ (directory / 'prompt.txt').write_text(case['prompt'], encoding='utf-8')
+ canvas = geometry(case.get('width', 1344), case.get('height', 768), frames=case.get('frames'), seconds=case.get('seconds'))
+ command = command_for(machine, case, directory, args)
+ row = dict(case, geometry=canvas, command=command, status='running')
+ value['cases'].append(row)
+ save(directory / 'case.json', row)
+ report(destination, value)
+ ui.phase('Case %d of %d' % (index + 1, len(cases)), index, len(cases))
+ ui.begin(case['id'], '%s · %d×%d · %d frames' % (case['id'], canvas['width'], canvas['height'], canvas['frames']), detail='Starting exclusive Engine request')
+ row['before'] = inventory(machine['gpu_uuid'])
+ child_env = dict(env, **{LOCK_ENV: str(fd)})
+ row.update(run_process(command, directory, child_env, machine['gpu_uuid'], args.timeout_seconds,
+ args.min_available_ram_gib * GiB, pass_fds=(fd,), ui=ui))
+ read_case_reports(directory, row)
+ if row['status'] == 'complete':
+ ui.update(case['id'], done=0, total=0, detail='Validate every video frame, audio and retained artifacts')
+ try:
+ from .media import inspect
+ validate_metrics(row, canvas)
+ row['artifact_validation'] = validate_artifacts(directory, canvas)
+ row['media'] = inspect(directory / 'video.mp4', **{k: canvas[k] for k in ('width', 'height', 'frames', 'fps')})
+ except Exception as error:
+ row.update(status='validation_failed', error=repr(error))
+ elif not row.get('error'):
+ row['error'] = row.get('request', {}).get('error') or (directory / 'run.log').read_text(errors='replace', encoding='utf-8')[-2500:]
+ if row['status'] != 'complete' and row.get('request', {}).get('tuning', {}).get('profile_id'):
+ from .tuning import disable_profile
+ # The child may have been killed before its own finally block.
+ old_root = os.environ.get('FREEVIDEO_HOME')
+ os.environ['FREEVIDEO_HOME'] = machine['root']
+ try:
+ disable_profile(row['request']['tuning']['profile_id'], row.get('error', row['status']))
+ finally:
+ if old_root is None:
+ os.environ.pop('FREEVIDEO_HOME', None)
+ else:
+ os.environ['FREEVIDEO_HOME'] = old_root
+ if row['status'] == 'complete':
+ row['comparison'] = baseline_comparison(row)
+ save(directory / 'case.json', row)
+ ui.update(case['id'], done=0, total=0, detail='Hash retained artifacts and write reports')
+ artifact_manifest(directory)
+ report(destination, value)
+ ui.end(case['id'], success=row['status'] == 'complete', detail='%.2fs request · GPU peak %.2f GiB · RAM peak %.2f GiB' %
+ (row['wall_seconds'], (row.get('gpu', {}).get('gpu_peak_bytes') or 0)/GiB, (memory_peak(row['ram']) or 0)/GiB))
+ if row['status'] == 'interrupted':
+ break
+ value['status'] = 'complete' if len(value['cases']) == len(cases) and all(r['status'] == 'complete' for r in value['cases']) else 'failed'
+ except BaseException as error:
+ value.update(status='failed', error=repr(error), traceback=traceback.format_exc())
+ if value['cases'] and value['cases'][-1]['status'] == 'running':
+ last = value['cases'][-1]
+ last.update(status='interrupted' if isinstance(error, KeyboardInterrupt) else 'failed', error=repr(error))
+ ui.end(last['id'], success=False, detail=str(error))
+ save(destination / last['id'] / 'case.json', last)
+ artifact_manifest(destination / last['id'])
+ finally:
+ try:
+ value['finished_epoch'] = time.time()
+ report(destination, value)
+ except OSError as error:
+ value.update(status='failed', report_error=repr(error))
+ print('Could not save final test report: ' + str(error), file=sys.stderr)
+ finally:
+ processes.restore_handlers(previous)
+ try:
+ ui.phase('Tests ' + value['status'], len([r for r in value['cases'] if r['status'] != 'running']), len(cases))
+ finally:
+ ui.close()
+ print('\nReport: %s\nStatus: %s' % (destination / 'report.html', value['status']), flush=True)
+ return 0 if value['status'] == 'complete' else 1
+
+
+def baseline_comparison(row):
+ reference = row.get('request', {}).get('tuning', {}).get('baseline_report')
+ if not reference:
+ return None
+ try:
+ baseline = json.loads(Path(reference).read_text(encoding='utf-8'))
+ for previous in baseline.get('cases', []):
+ if previous.get('status') != 'complete' or any(row.get(k) != previous.get(k) for k in ('prompt', 'seed', 'geometry')):
+ continue
+ before, after = previous['request']['profile'], row['request']['profile']
+ from .tuning import PATCH_KEYS
+ if (before['decoder'] != after['decoder'] or any(before.get(k) != after.get(k) for k in ('gpu_budget_gb', 'inference_ram_budget_gb'))
+ or {k: v for k, v in before['engine'].items() if k not in PATCH_KEYS} !=
+ {k: v for k, v in after['engine'].items() if k not in PATCH_KEYS}):
+ continue
+ return {'baseline_report': reference, 'case': previous['id'], 'baseline_wall_seconds': previous['wall_seconds'],
+ 'wall_gain_percent': 100 * (1 - row['wall_seconds'] / previous['wall_seconds']),
+ 'sampling_gain_percent': 100 * (1 - row['engine']['sample_seconds'] / previous['engine']['sample_seconds']),
+ 'conditioning_reused': bool(row['encoding'].get('cache_hit')),
+ 'scope': 'Same input, seed, geometry and base resource placement. Storage/JIT warmth can affect timings.'}
+ except (OSError, ValueError, KeyError, ZeroDivisionError, TypeError):
+ return None
+ return None
+
+
+def main(argv=None):
+ try:
+ return _main(argv)
+ except (OSError, ValueError, RuntimeError, KeyError) as error:
+ print('Test could not start: ' + str(error), file=sys.stderr)
+ return 1
+ except KeyboardInterrupt:
+ print('Test cancelled.', file=sys.stderr)
+ return 130
+
+
+if __name__ == '__main__':
+ raise SystemExit(main())
diff --git a/freevideo_engine/torch_compat.py b/freevideo_engine/torch_compat.py
new file mode 100644
index 0000000..c87aa52
--- /dev/null
+++ b/freevideo_engine/torch_compat.py
@@ -0,0 +1,15 @@
+"""Optional allocator operations across PyTorch builds; imports remain torch-free."""
+
+
+def host_cache_release_supported(torch_module):
+ return callable(getattr(getattr(torch_module, '_C', None), '_host_emptyCache', None))
+
+
+def empty_host_cache(torch_module):
+ # Some CPU/Windows wheels do not export this private CUDA allocator hook.
+ # Missing support is not evidence that host pages were returned to the OS.
+ empty = getattr(getattr(torch_module, '_C', None), '_host_emptyCache', None)
+ if not callable(empty):
+ return False
+ empty()
+ return True
diff --git a/freevideo_engine/transfer_handoff.py b/freevideo_engine/transfer_handoff.py
new file mode 100644
index 0000000..80a5e33
--- /dev/null
+++ b/freevideo_engine/transfer_handoff.py
@@ -0,0 +1,105 @@
+"""Move stopped provider downloads between mirrors without loading them in RAM."""
+from pathlib import Path
+import re
+import stat
+from . import network
+from .file_ops import publish
+from .download_cache import discard_read_cache
+
+BLOCK = 4*2**20
+PART = 64*2**20
+
+
+def regular(path, boundary):
+ for item in (path,)+tuple(path.parents):
+ value=item.lstat()
+ if stat.S_ISLNK(value.st_mode) or getattr(value,'st_file_attributes',0)&0x400:
+ raise ValueError('Linked download fragments cannot be moved between sources')
+ if item==path and not stat.S_ISREG(value.st_mode):
+ raise ValueError('Download fragment is not a regular file')
+ if item==boundary:break
+
+
+def merge_prefix(source, target):
+ if source == target:
+ return
+ target.parent.mkdir(parents=True,exist_ok=True)
+ if not target.exists():
+ publish(source,target)
+ return
+ # Both files describe the same byte interval. Compare overlap before
+ # choosing the longer prefix; never splice conflicting mirror content.
+ remaining=min(source.stat().st_size,target.stat().st_size)
+ with source.open('rb') as left,target.open('rb') as right:
+ while remaining:
+ count=min(BLOCK,remaining)
+ if left.read(count)!=right.read(count):
+ raise ValueError('Retained download fragments disagree; both files retained')
+ discard_read_cache(left.fileno(),left.tell()-count,count)
+ discard_read_cache(right.fileno(),right.tell()-count,count)
+ remaining-=count
+ if source.stat().st_size>target.stat().st_size:
+ network.retain_partial(target,'switch-retained')
+ publish(source,target)
+ else:
+ network.retain_partial(source,'switch-retained')
+
+
+def migrate(row, path, candidates, *, allow_restart=False, part_bytes=PART):
+ """Called only after the old worker tree has stopped and its log is closed."""
+ from .model_transfer import stage_directory, verify
+ from .provider_resume import ms_completed
+ path=Path(path)
+ stage=stage_directory('manual',row,path)
+ target=stage/row['file']
+ prefixes=[]; parts=[]; completed=[]; unmapped=0
+ for name in dict.fromkeys(['manual']+[name for name,_ in candidates]):
+ directory=stage_directory(name,row,path)
+ item=directory/row['file']
+ for unknown in directory.rglob('*.incomplete') if directory.exists() else ():
+ regular(unknown,directory)
+ if any(marker in str(unknown.relative_to(directory)) for marker in
+ ('.rejected-', '.hash-rejected-', '.interrupted-merge-', '.restart-approved-')):
+ continue
+ if not unknown.stat().st_size:
+ continue
+ if unknown.stat().st_size==row['bytes'] and verify(unknown,row):
+ completed.append(unknown)
+ continue
+ if not allow_restart:
+ raise RuntimeError('Source switch paused: Xet/legacy fragments have no portable byte-range map. '
+ 'The old download is stopped and retained. Enable Allow restarting incomplete downloads '
+ 'or --allow-model-restart, then retry to download this file from the new source.')
+ unmapped+=unknown.stat().st_size
+ network.retain_partial(unknown,'restart-approved')
+ if item.is_file():
+ regular(item,directory);completed.append(item)
+ for suffix in ('.partial','.parallel_tmp'):
+ prefix=item.with_suffix(item.suffix+suffix)
+ if prefix.is_file():
+ regular(prefix,directory);prefixes.append(prefix)
+ for piece in item.parent.glob(item.name+'_*') if item.parent.exists() else ():
+ match=re.fullmatch(re.escape(item.name)+r'_(\d+)_(\d+)',piece.name)
+ if not match:continue
+ regular(piece,directory)
+ start,end=map(int,match.groups())
+ if start%part_bytes or end!=min(row['bytes'],start+part_bytes)-1 or not 0<=piece.stat().st_size<=end-start+1:
+ raise ValueError('Retained range layout differs; all fragments retained')
+ parts.append(piece)
+ if completed:
+ for item in completed:
+ if verify(item,row):
+ if item!=path:publish(item,path)
+ return dict(ranges=False,bytes=row['bytes'],unmapped_bytes=unmapped)
+ raise ValueError('Completed retained file failed verification; files retained')
+ main_partial=path.with_suffix(path.suffix+'.partial')
+ if not prefixes and not parts:
+ return dict(ranges=False,bytes=main_partial.stat().st_size if main_partial.exists() else 0,unmapped_bytes=unmapped)
+ if main_partial.is_file():prefixes.append(main_partial)
+ for item in prefixes:
+ if item.stat().st_size>row['bytes']:
+ raise ValueError('Retained prefix exceeds model size; files retained')
+ merge_prefix(item,target.with_suffix(target.suffix+'.parallel_tmp'))
+ for item in parts:
+ merge_prefix(item,target.parent/item.name)
+ return dict(ranges=True,bytes=ms_completed(target,row['bytes'],part_bytes),unmapped_bytes=unmapped)
diff --git a/freevideo_engine/tuning.py b/freevideo_engine/tuning.py
new file mode 100644
index 0000000..899811b
--- /dev/null
+++ b/freevideo_engine/tuning.py
@@ -0,0 +1,704 @@
+"""Validated local tuning and conditioning reuse; no tensor imports in callers."""
+import copy
+import hashlib
+import importlib.metadata
+import json
+import math
+import os
+from pathlib import Path
+import re
+import shutil
+import subprocess
+import sys
+import time
+from urllib.parse import unquote, urlsplit
+
+from .hardware import GiB
+from .policy import BLOCK_BYTES
+from .monitoring import save
+from .paths import data_root
+
+
+SCHEMA = 1
+COMPUTE_FILES = ('activation_staging.py', 'residual.py', 'adaln.py', 'adaln_assets.py', 'attention.py', 'blocks.py',
+ 'keyframes.py', 'media_request.py', 'media_conditioning.py', 'media_encoding.py', 'reference_sampler.py', 'lora_cache.py',
+ 'conditioning.py', 'decode.py', 'decode_prefetch.py', 'resident_models.py', 'decode_stream.py', 'streamed_weights.py', 'encode_worker.py', 'idle_encoder.py', 'media_encoding.py', 'fp8.py', 'fp8_ops.py', 'fp8_gemm.py',
+ 'kernel_capabilities.py', 'gpu_budget.py', 'worker.py', 'resident_worker.py', 'failure_cleanup.py',
+ 'geometry.py', 'head_chunk.py', 'offload.py', 'packing.py', 'runtime.py',
+ 'two_pass.py', 'two_pass_metrics.py', 'refine.py', 'latent_upscale.py',
+ 'vae_tiles.py', 'weight_only.py', 'weights.py')
+PACKAGES = ('torch', 'triton', 'triton-windows', 'transformers', 'tokenizers', 'sageattention', 'numpy',
+ 'safetensors', 'comfy-kitchen', 'comfy-aimdo', 'torchaudio', 'torchvision')
+PLACEMENT_KEYS = {'resident_blocks', 'pin_host_gb'}
+PATCH_KEYS = {'window_batch', 'ff_chunk', 'projection_chunk', 'head_chunk', 'head_parallelism', 'prefetch',
+ 'attention_cpu_outputs', 'grouped_attention_outputs', 'fp8_ff_recompute'} | PLACEMENT_KEYS
+CACHE_ENTRIES = 128
+CACHE_BYTES = 128 * 2**20
+
+
+def digest(path):
+ value = hashlib.sha256()
+ with Path(path).open('rb') as stream:
+ for block in iter(lambda: stream.read(1024 * 1024), b''):
+ value.update(block)
+ return value.hexdigest()
+
+
+def key(value):
+ return hashlib.sha256(json.dumps(value, sort_keys=True, separators=(',', ':')).encode()).hexdigest()
+
+
+def state_path():
+ return data_root() / 'tuning' / 'state.json'
+
+
+def package_versions(python=None):
+ if python and os.path.abspath(python) != os.path.abspath(sys.executable):
+ code = 'import importlib.metadata as m,json; print(json.dumps({d.metadata["Name"].lower().replace("_","-"):d.version for d in m.distributions()}))'
+ result = subprocess.run([str(python), '-B', '-c', code], capture_output=True, text=True, timeout=15, check=True)
+ installed = json.loads(result.stdout)
+ else:
+ installed = {d.metadata['Name'].lower().replace('_', '-'): d.version for d in importlib.metadata.distributions()}
+ return {name: version for name, version in installed.items() if name in PACKAGES or name.startswith(('nvidia-', 'cuda-'))}
+
+
+def frozen_versions(text):
+ installed = {}
+ for line in text.splitlines():
+ if '==' in line:
+ name, version = line.split('==', 1)
+ elif ' @ ' in line and '.whl' in line:
+ name, reference = line.split(' @ ', 1)
+ filename = unquote(urlsplit(reference).path).rsplit('/', 1)[-1]
+ match = re.match(r'[^-]+-([0-9][^-]*)-[^-]+-[^-]+-[^-]+\.whl$', filename)
+ if not match:
+ continue
+ version = match[1]
+ else:
+ continue
+ name = name.lower().replace('_', '-')
+ if name in PACKAGES or name.startswith(('nvidia-', 'cuda-')):
+ installed[name] = version
+ return installed
+
+
+def identity(machine, hardware):
+ from .bootstrap import inventory, model_target
+ from .install_tuning import required_models
+ observed = inventory(machine['gpu_uuid'])
+ package = Path(__file__).parent
+ encoder = Path(machine['encoder_model_root']) / 'text_encoders' / machine['encoder']
+ manifest = Path(machine['cache']) / 'manifest.json'
+ weights = json.loads(manifest.read_text(encoding='utf-8'))
+ stamps = {}
+ required = required_models(json.loads((package / 'model_files.json').read_text(encoding='utf-8')), machine['cache'])
+ extra = [model_target(row, Path(machine['model_root']), Path(machine['encoder_model_root'])) for row in required]
+ for path in [encoder, *(Path(machine['cache']) / g['file'] for g in weights['groups']), *extra]:
+ status = path.stat()
+ stamps[str(path.resolve())] = [status.st_size, status.st_mtime_ns, status.st_ctime_ns]
+ specs = json.loads((package / 'dependencies.json').read_text(encoding='utf-8'))
+ revisions = {k: specs[k] for k in ('vdn', 'diffusers', 'encoder', 'sageattention')}
+ for checkout, ref, expected in ((machine['vdn_root'], 'HEAD', specs['vdn']['commit']),
+ (machine['comfy_root'], 'HEAD', specs['encoder']['comfy_commit']),
+ (str(Path(machine['vdn_root']) / 'diffusers'), 'HEAD^{tree}', specs['diffusers']['patched_tree'])):
+ observed_ref = subprocess.check_output(['git', '-C', checkout, 'rev-parse', ref], text=True, timeout=10).strip()
+ dirty = subprocess.check_output(['git', '-C', checkout, 'status', '--porcelain', '--untracked-files=no'], text=True, timeout=10)
+ if observed_ref != expected or dirty:
+ raise ValueError('Pinned model code changed; saved tuning cannot be reused: ' + checkout)
+ return {'schema': SCHEMA, 'system': hardware.system,
+ 'gpu': {k: observed['selected_gpu'][k] for k in ('uuid', 'name', 'driver_version')},
+ 'capability': list(hardware.capability), 'manifest_sha256': digest(manifest),
+ 'engine_packages': package_versions(), 'encoder_packages': package_versions(machine['comfy_python']),
+ 'source_sha256': {name: digest(package / name) for name in COMPUTE_FILES},
+ 'weight_stamps': stamps, 'dependency_revisions': revisions,
+ 'encoder': machine['encoder'], 'model_revision': machine['model_revision']}
+
+
+def report_identity_errors(inventory, expected):
+ errors = []
+ if expected.get('system') and inventory.get('hardware', {}).get('system') != expected['system']:
+ errors.append('Operating system differs')
+ gpu = inventory.get('selected_gpu', {})
+ for name, value in expected['gpu'].items():
+ if gpu.get(name) != value:
+ errors.append('GPU/driver changed: ' + name)
+ if inventory.get('fp8_cache_manifest_sha256') != expected['manifest_sha256']:
+ errors.append('Prepared model cache differs')
+ for role in ('engine', 'encoder'):
+ if frozen_versions(inventory.get(role + '_packages', {}).get('stdout', '')) != expected[role + '_packages']:
+ errors.append(role + ' dependency versions differ or are missing')
+ for name, value in expected['source_sha256'].items():
+ if inventory.get('source_files', {}).get('freevideo_engine/' + name) != value:
+ errors.append('Compute source changed: ' + name)
+ configuration = inventory.get('machine_configuration', {})
+ if any(inventory.get('dependency_revisions', {}).get(k) != v for k, v in expected.get('dependency_revisions', {}).items()):
+ errors.append('Pinned source revisions differ')
+ if configuration.get('model_revision') != expected['model_revision'] or configuration.get('encoder') != expected['encoder']:
+ errors.append('Pinned model or encoder differs')
+ return errors
+
+
+def load_state(expected=None):
+ path = state_path()
+ if not path.is_file():
+ return None, 'No local optimization saved'
+ try:
+ state = json.loads(path.read_text(encoding='utf-8'))
+ if not isinstance(state, dict) or state.get('schema') != SCHEMA or not state.get('enabled'):
+ return None, 'Local optimization is disabled or has an unknown format'
+ if not isinstance(state.get('conditioning'), dict) or not isinstance(state.get('profiles'), list):
+ return None, 'Invalid local optimization entries'
+ for entry in state['conditioning'].values():
+ if (not isinstance(entry, dict) or not isinstance(entry.get('encoding'), dict)
+ or type(entry.get('bytes')) is not int or not 0 < entry['bytes'] <= CACHE_BYTES
+ or not isinstance(entry.get('path'), str) or not isinstance(entry.get('sha256'), str)):
+ return None, 'Invalid local conditioning cache entry'
+ if expected is not None and state.get('identity') != expected:
+ return None, 'Hardware, driver, dependencies, model or compute code changed; run optimize again'
+ retired=False
+ for entry in state['profiles']:
+ if isinstance(entry,dict) and retired_failure(entry.get('last_failure','')):
+ entry.pop('last_failure',None)
+ entry.pop('disabled_epoch',None)
+ entry['enabled']=True
+ retired=True
+ if retired:
+ save_state(state)
+ return state, None
+ except (OSError, ValueError, TypeError) as error:
+ return None, 'Unreadable local optimization: ' + str(error)
+
+
+def save_state(state):
+ save(state_path(), state)
+
+
+def prompt_key(prompt):
+ return hashlib.sha256(prompt.encode('utf-8')).hexdigest()
+
+
+def remember_conditioning(state, prompt, condition, encoding):
+ """Copy small verified conditioning; model/video artifacts are never removed."""
+ if encoding.get('success') is not True or encoding.get('conditioning_shape', [0, 0])[1:] != [5120]:
+ raise ValueError('Cannot cache unsuccessful or incompatible H3 conditioning')
+ content = digest(condition)
+ entries = state.setdefault('conditioning', {})
+ size = Path(condition).stat().st_size
+ if prompt_key(prompt) not in entries and (len(entries) >= CACHE_ENTRIES or
+ sum(e.get('bytes', 0) for e in entries.values()) + size > CACHE_BYTES):
+ return False
+ target = state_path().parent / 'conditioning' / (content + '.pt')
+ target.parent.mkdir(parents=True, exist_ok=True)
+ if target.exists():
+ if digest(target) != content:
+ raise ValueError('Existing conditioning cache is damaged')
+ else:
+ temporary = target.with_suffix('.partial')
+ shutil.copyfile(condition, temporary)
+ if digest(temporary) != content:
+ raise ValueError('Conditioning changed while copying')
+ temporary.replace(target)
+ state.setdefault('conditioning', {})[prompt_key(prompt)] = {
+ 'path': str(target), 'sha256': content, 'bytes': target.stat().st_size,
+ 'encoding': copy.deepcopy(encoding), 'created_epoch': time.time()}
+ return True
+
+
+def cached_conditioning(state, prompt, destination):
+ entry = state.get('conditioning', {}).get(prompt_key(prompt))
+ if not entry:
+ return None
+ started = time.perf_counter()
+ # A stale or modified cache entry is a miss, never an unchecked tensor load.
+ try:
+ source = Path(entry['path'])
+ if (not source.is_file() or source.stat().st_size != entry['bytes'] or digest(source) != entry['sha256']
+ or entry['encoding'].get('success') is not True
+ or entry['encoding'].get('conditioning_shape', [])[1:] != [5120]):
+ return None
+ except (KeyError, TypeError, OSError, AttributeError):
+ return None
+ shutil.copyfile(source, destination)
+ from .encoder_diagnostics import cached_metrics
+ result = dict(cached_metrics(copy.deepcopy(entry['encoding'])), output=str(destination), load_seconds=0.,
+ work_seconds=time.perf_counter() - started, cache_hit=True,
+ cache_sha256=entry['sha256'], scope='Reused validated local native H3 conditioning; no encoder model loaded')
+ for name in ('torch_peak_allocated_bytes', 'torch_peak_reserved_bytes'):
+ result[name] = 0
+ return result
+
+
+def compatible_profile(entry, base, canvas, tokens):
+ if not isinstance(entry, dict) or not isinstance(entry.get('id'), str):
+ return False
+ reference_context = {key: canvas.get(key, 0) for key in ('reference_video_tokens', 'reference_audio_tokens')}
+ if any(reference_context.values()) and entry.get('reference_context') != reference_context:
+ return False
+ if not entry.get('enabled') or entry.get('geometry') != {k: canvas[k] for k in ('width', 'height', 'frames')}:
+ return False
+ if tokens != entry.get('text_tokens') or base['decoder'] != entry.get('decoder'):
+ return False
+ patch = entry.get('patch', {})
+ if not isinstance(patch, dict) or not patch or not set(patch).issubset(PATCH_KEYS):
+ return False
+ baseline = entry.get('base_engine')
+ if not isinstance(baseline, dict):
+ return False
+ if PLACEMENT_KEYS.issubset(patch):
+ # Live RAM changes the default pin budget almost every request. Reuse
+ # measured placement only when arithmetic matches and today's budget
+ # still covers its measured peak. A validated faster computation can
+ # deliberately give GPU weights or host pins back to its workspace.
+ if ({k: v for k, v in baseline.items() if k not in PLACEMENT_KEYS}
+ != {k: v for k, v in base['engine'].items() if k not in PLACEMENT_KEYS}):
+ return False
+ if type(patch['resident_blocks']) is not int or not 0 <= patch['resident_blocks'] <= 50:
+ return False
+ if patch['resident_blocks'] < base['engine']['resident_blocks'] and not measured_compute_gain(entry):
+ return False
+ elif baseline != base['engine']:
+ return False
+ from .compute_budget import CHOICES, valid_parallelism
+ choices = CHOICES
+ for k, v in patch.items():
+ if k in ('prefetch', 'attention_cpu_outputs', 'grouped_attention_outputs', 'fp8_ff_recompute'):
+ valid = type(v) is bool
+ elif k == 'resident_blocks':
+ valid = type(v) is int and 0 <= v <= 50
+ elif k == 'pin_host_gb':
+ valid = type(v) in (int, float) and math.isfinite(v) and 0 <= v <= 22
+ else:
+ valid = type(v) is int and v in choices[k]
+ if not valid:
+ return False
+ applied = dict(base['engine'], **patch)
+ if not valid_parallelism(applied):
+ return False
+ if applied.get('grouped_attention_outputs') and not applied.get('attention_cpu_outputs'):
+ return False
+ if PLACEMENT_KEYS.issubset(patch):
+ # More available RAM should not be held to an older, smaller pin budget.
+ # Moving weights onto the GPU legitimately reduces how much needs pinning.
+ useful_pin = min(base['engine']['pin_host_gb'], (50 - patch["resident_blocks"]) * BLOCK_BYTES / 1e9)
+ if patch['pin_host_gb'] + .001 < useful_pin and not measured_compute_gain(entry):
+ return False
+ for name in ('required_gpu_bytes', 'required_ram_bytes'):
+ value = entry.get(name)
+ if not isinstance(value, (int, float)) or not math.isfinite(value) or value <= 0:
+ return False
+ validation = entry.get('validation', {})
+ if not isinstance(validation, dict):
+ return False
+ return (base['gpu_budget_gb'] * 1e9 >= entry.get('required_gpu_bytes', math.inf)
+ and base['inference_ram_budget_gb'] * 1e9 >= entry.get('required_ram_bytes', math.inf)
+ and validation.get('bitwise_latents') is True
+ and validation.get('decoded_output_equal') == {'rgb.npy': True, 'audio.npy': True}
+ and validation.get('full_request') is True
+ and type(validation.get('completed_steps')) is int and validation['completed_steps'] == 8
+ and validation.get('memory_checked') is True)
+
+
+def measured_compute_gain(entry):
+ """Allow resource tradeoffs only with retained repeat and full-run evidence.
+
+ Merely filling memory is not a benefit. Older profiles without this timing
+ receipt keep their existing placement rules; quality gates remain separate.
+ """
+ baseline_engine = entry.get('base_engine', {})
+ if not any(name not in PLACEMENT_KEYS and value != baseline_engine.get(name)
+ for name, value in entry.get('patch', {}).items()):
+ return False
+ measured = entry.get('performance')
+ if not isinstance(measured, dict):
+ return False
+ baseline = measured.get('baseline_step_seconds')
+ candidate = measured.get('candidate_step_seconds')
+ if not isinstance(baseline, list) or len(baseline) < 2 or not isinstance(candidate, list) or len(candidate) < 2:
+ return False
+ if not all(_positive(value) for value in baseline + candidate):
+ return False
+ before, after = measured.get('baseline_full_sample_seconds'), measured.get('candidate_full_sample_seconds')
+ return (_positive(before) and _positive(after) and after < before
+ and max(candidate) < min(baseline) * .95)
+
+
+def apply_profile(state, base, canvas, tokens):
+ for entry in reversed(state.get('profiles', [])):
+ if compatible_profile(entry, base, canvas, tokens):
+ result = copy.deepcopy(base)
+ result['engine'].update(entry['patch'])
+ if 'policy' in result:
+ result['policy']['engine'] = dict(result['engine'])
+ return result, entry['id']
+ return base, None
+
+
+def retired_failure(error):
+ """Legacy device/exit incidents are not evidence against a tuned profile."""
+ text=str(error).lower()
+ from .adaptive import classify_failure
+ return ('durable local attempt history' in text or 'attemptblocked' in text
+ or classify_failure(error)['outcome']=='discarded')
+
+
+def disable_profile(identifier, error):
+ if retired_failure(error):
+ return False
+ state, _ = load_state()
+ changed=False
+ if state:
+ for entry in state.get('profiles', []):
+ if isinstance(entry, dict) and entry.get('id') == identifier:
+ entry.update(enabled=False, last_failure=str(error), disabled_epoch=time.time())
+ changed=True
+ save_state(state)
+ return changed
+
+
+def _positive(value):
+ return type(value) in (int, float) and math.isfinite(value) and value > 0
+
+
+def _complete_peak(row, base):
+ """A probe or another configuration cannot establish full-request headroom."""
+ measured = row.get('engine', {})
+ if row.get('status') != 'complete' or measured.get('success') is not True:
+ return None, 'No successful complete request; probe peaks are not capacity evidence'
+ steps = measured.get('step_seconds', [])
+ if not isinstance(steps, list) or len(steps) != 8 or not all(_positive(v) for v in steps):
+ return None, 'Eight measured steps are required; no linear peak extrapolation is used'
+ reported = row.get('request', {}).get('profile', {}).get('engine', measured.get('config', {}))
+ if any(reported.get(k) != v for k, v in base['engine'].items()):
+ return None, 'Completed request used a different engine configuration'
+ canvas = row.get('geometry', {})
+ if (not all(k in canvas for k in ('width', 'height', 'frames')) or
+ any(measured.get('geometry', {}).get(k) != canvas[k] for k in ('width', 'height', 'frames'))):
+ return None, 'Completed request geometry is missing or inconsistent'
+ gpu = row.get('gpu', {})
+ if gpu.get('sampling_errors') or not gpu.get('samples') or not _positive(gpu.get('gpu_peak_bytes')):
+ return None, 'Whole-request GPU observations are incomplete'
+ peak = gpu['gpu_peak_bytes']
+ reserved = measured.get('torch_peak_reserved_bytes')
+ if _positive(reserved):
+ peak = max(peak, reserved)
+ return peak, 'Matched complete eight-step request; maximum of reserved and whole-device peak'
+
+
+def _owned_working_memory(sample, hardware, bound):
+ """Current worker memory already excluded from available RAM, counted once.
+
+ Clean mapped pages may already be credited in availability. Never add RSS,
+ private commit or raw Linux PSS; the current owned working memory is also
+ bounded by the complete matching request's peak.
+ """
+ sample = sample or {}
+ system = hardware.get('system')
+ if system == 'Windows' and sample.get('guard_metric') == 'tree private working set':
+ used = sample.get('guard_bytes')
+ elif system == 'Linux' and sample.get('guard_metric') == 'PSS':
+ # inference_guard_bytes is an upper bound and may contain clean file
+ # pages when their protection counters are incomplete. Only measured
+ # non-file pages establish bytes not already credited in availability.
+ used = sample.get('nonfile_pss_bytes')
+ else:
+ return 0
+ return min(used, bound) if _positive(used) and _positive(bound) else 0
+
+
+def completed_placement_baseline(row, base):
+ """Keep usable full-request evidence across live placement-budget rounding.
+
+ The caller has already validated the local software/model identity and
+ artifacts. Only placement may differ; fresh budgets still bound the measured
+ whole request. This selects a search baseline, not a new user profile.
+ """
+ previous = row.get('request', {}).get('profile', {})
+ engine = previous.get('engine', {})
+ if not isinstance(engine, dict):
+ return base, None
+ changed = {k for k in set(engine) | set(base['engine']) if engine.get(k) != base['engine'].get(k)}
+ if not changed or not changed <= PLACEMENT_KEYS or previous.get('decoder') != base['decoder']:
+ return base, None
+ matched = copy.deepcopy(base)
+ matched['engine'] = copy.deepcopy(engine)
+ peak, _ = _complete_peak(row, matched)
+ ram = row.get('ram', {})
+ system = base.get('policy', {}).get('hardware', {}).get('system')
+ if system == 'Windows' and ram.get('ram_guard_metric') == 'tree private working set':
+ working = ram.get('process_tree_peak_guard_bytes')
+ elif system == 'Linux' and ram.get('ram_guard_metric') == 'PSS':
+ working = ram.get('inference_peak_working_pss_bytes')
+ else:
+ return base, None
+ if (peak is None or peak > base['gpu_budget_gb'] * 1e9 or not _positive(working)
+ or working > base['inference_ram_budget_gb'] * 1e9
+ or ram.get('process_tree_guard_complete') is not True
+ or not _positive(ram.get('ram_observation_samples'))):
+ return base, None
+ if 'policy' in matched:
+ matched['policy']['engine'] = dict(engine)
+ return matched, dict(reason='Start from the complete local placement so live budget rounding does not discard its measurements',
+ placement_fields=sorted(changed), complete_gpu_peak_bytes=peak,
+ complete_working_ram_bytes=working,
+ scope='Search baseline only; current budgets and full candidate validation still apply')
+
+
+def _host_cache_trial(row, base, live_ram_available, pinning_limit_bytes, live_ram_sample):
+ """Use measured host staging to nominate storage, never certify it.
+
+ Pinning already-cached weights barely helps. Retaining weights which would
+ otherwise be read from disk each step is a different operation. Use the
+ complete working-memory peak (which includes pinned pages), not reclaimable
+ file cache or the requested, partly unused pin budget. Windows must supply
+ its private working set; RSS and private commit are not interchangeable.
+ """
+ engine = base['engine']
+ measured, ram = row.get('engine', {}), row.get('ram', {})
+ offload = measured.get('offload', {})
+ reader = offload.get('disk_read_ahead', {})
+ hardware = base.get('policy', {}).get('hardware', {})
+ if (not engine.get('stream_weights')
+ or ram.get('process_tree_guard_complete') is not True
+ or not _positive(ram.get('ram_observation_samples'))):
+ return None
+ if hardware.get('system') == 'Linux':
+ if (ram.get('ram_guard_metric') != 'PSS' or reader.get('enabled') is not True
+ or reader.get('failure') or not _positive(reader.get('read_bytes'))):
+ return None
+ working = ram.get('inference_peak_working_pss_bytes')
+ waited = reader.get('wait_seconds')
+ evidence = {'inference_peak_working_pss_bytes': working,
+ 'disk_wait_seconds': waited, 'bottleneck': 'disk reads'}
+ elif hardware.get('system') == 'Windows':
+ if ram.get('ram_guard_metric') != 'tree private working set':
+ return None
+ working = ram.get('process_tree_peak_guard_bytes')
+ waited = offload.get('host_stage_seconds')
+ evidence = {'private_working_set_peak_bytes': working,
+ 'host_stage_seconds': waited, 'bottleneck': 'host weight staging',
+ 'scope': 'Host staging includes RAM copies and file reads; it is not physical disk I/O'}
+ else:
+ return None
+ sample = measured.get('sample_seconds')
+ if not _positive(sample) or not _positive(waited) or not .35 * sample <= waited <= sample:
+ return None
+ pinned = measured.get('config', {}).get('pinned_model_bytes')
+ pinned_layers, streamed = offload.get('pinned_layer_count'), offload.get('streamed_layer_count')
+ if (not _positive(working) or type(pinned) not in (int, float)
+ or not math.isfinite(pinned) or pinned < 0 or pinned > working
+ or type(pinned_layers) is not int or pinned_layers < 0
+ or (pinned_layers == 0) != (pinned == 0)
+ or type(streamed) is not int or streamed <= 0
+ or pinned_layers + streamed != 50 - engine['resident_blocks']):
+ return None
+ # A zero-cache baseline is useful evidence too. With no retained layer to
+ # measure, use the same block-size estimate as placement, then require a
+ # complete trial to measure the actual allocation and speed.
+ layer_bytes = pinned / pinned_layers if pinned_layers else BLOCK_BYTES
+ limit = base['inference_ram_budget_gb'] * 1e9
+ owned = 0
+ if live_ram_available is not None:
+ if not _positive(live_ram_available):
+ return None
+ reserve = max(GiB, base.get('policy', {}).get('ram_system_reserve_bytes', GiB))
+ owned = _owned_working_memory(live_ram_sample, hardware, working)
+ limit = min(limit, live_ram_available + owned - reserve)
+ # The policy already removed OS growth headroom. Keep another 256 MiB
+ # within its process budget for run-to-run variation. Use the measured
+ # remainder directly: a 1 GiB step cap makes a disk-bound user reload the
+ # whole model repeatedly to explore capacity already established locally.
+ # Count growth from actual retained bytes, not a rounded plan which
+ # can hide almost a whole unused block's worth of apparent spare memory.
+ growth = max(0., limit - working - .25 * GiB)
+ target = min(22e9, (50 - engine['resident_blocks']) * BLOCK_BYTES, pinned + growth)
+ if pinning_limit_bytes is not None:
+ target = min(target, pinning_limit_bytes)
+ target = math.floor(target / 1e6) * 1e6
+ if target < pinned + layer_bytes + 2**20 or target / 1e9 <= engine['pin_host_gb']:
+ return None
+ evidence.update(sample_seconds=sample, staging_wait_fraction=waited / sample,
+ actual_pinned_bytes=pinned, candidate_pin_bytes=int(target),
+ minimum_extra_layer_bytes=layer_bytes,
+ layer_size_source='retained layers' if pinned_layers else 'placement estimate',
+ live_owned_working_bytes=owned,
+ ram_budget_bytes=int(limit), additional_working_margin_bytes=GiB // 4,
+ local_pinning_limit_bytes=pinning_limit_bytes,
+ warning='Overlap is not additive wall time; a full repeated request must establish the gain')
+ if hardware.get('system') == 'Linux':
+ evidence['disk_wait_fraction'] = waited / sample
+ return ({'pin_host_gb': target / 1e9, 'resident_blocks': engine['resident_blocks']}, evidence)
+
+
+def candidate_plan(row, base, maximum=2, *, live_gpu_free=None, live_ram_available=None,
+ reject=None, pinning_limit_bytes=None, live_ram_sample=None):
+ """Predict worthwhile trials, with reasons; this never certifies a profile.
+
+ ``reject(patch)`` optionally applies caller-specific resource constraints. Shared
+ measurements only rank hypotheses: both numerical changes and isolated
+ kernel successes still require a complete local request and bitwise checks.
+ ``pinning_limit_bytes`` is a local observed ceiling, not free host memory.
+ ``live_ram_sample`` is a fresh observation of the current tuning worker,
+ taken alongside ``live_ram_available``; historical peaks cannot replace it.
+ """
+ if type(maximum) is not int or maximum < 0:
+ raise ValueError('maximum must be a non-negative integer')
+ if pinning_limit_bytes is not None and (type(pinning_limit_bytes) not in (int, float)
+ or not math.isfinite(pinning_limit_bytes) or pinning_limit_bytes < 0):
+ raise ValueError('pinning_limit_bytes must be a finite non-negative local observation')
+ engine = base['engine']
+ peak, peak_reason = _complete_peak(row, base)
+ free = max(0, base['gpu_budget_gb'] * 1e9 - peak) if peak is not None else None
+ from .system import memory_peak, windows
+ if free is not None and live_gpu_free is not None:
+ free = max(0, min(free, live_gpu_free - (1 if windows() else .5) * GiB))
+ options, rejected = [], []
+ evidence_source = 'benchmarks/2026-09-12-windows-ada-sampling-speed.json'
+
+ def offer(patch, reason, observation, *, numerical='requires-bitwise-validation', risk='recoverable-memory', source=evidence_source):
+ if any(item['patch'] == patch for item in options + rejected):
+ return
+ record = {'patch': patch, 'reason': reason, 'observation': observation,
+ 'source': source, 'numerical_class': numerical, 'risk': risk,
+ 'validation_required': 'complete-local-request-and-bitwise-output',
+ 'evidence_scope': 'Candidate prediction only; not certification for this device'}
+ blocked = reject(dict(patch)) if reject is not None else None
+ if blocked:
+ record['rejected_reason'] = str(blocked)
+ rejected.append(record)
+ else:
+ options.append(record)
+
+ storage = _host_cache_trial(row, base, live_ram_available, pinning_limit_bytes, live_ram_sample) if peak is not None else None
+ if storage is not None:
+ patch, observation = storage
+ offer(patch, 'Prioritize retained host weights: this complete local request spends at least 35% of sampling on ' + observation['bottleneck'],
+ observation, numerical='placement-neutral', source='complete-local-request')
+
+ # Cached H2D traffic and repeated host staging are different bottlenecks.
+ # The offloader measures CPU staging on both platforms, even without the
+ # RAM evidence needed to grow the host cache. When that dominates a complete
+ # request, spend measured GPU room on weights before changing arithmetic.
+ # This only nominates a full local trial: staging can include
+ # RAM copies and cached file reads, so do not label it physical disk I/O.
+ offload = row.get('engine', {}).get('offload', {})
+ sample = row.get('engine', {}).get('sample_seconds')
+ staged = offload.get('host_stage_seconds')
+ streamed = offload.get('streamed_layer_count')
+ if (free is not None and engine.get('stream_weights')
+ and type(streamed) is int and 0 < streamed <= 50 - engine['resident_blocks']
+ and _positive(sample) and _positive(staged) and .35 * sample <= staged <= sample):
+ extra = min(streamed, max(0, int((free - .25 * GiB) / BLOCK_BYTES)))
+ if extra:
+ resident = engine['resident_blocks'] + extra
+ offer({'resident_blocks': resident,
+ 'pin_host_gb': math.floor(min(engine['pin_host_gb'], (50 - resident) * BLOCK_BYTES / 1e9) * 1000) / 1000},
+ 'Prioritize GPU weight residency: this complete local request spends over 35% of sampling staging streamed host weights',
+ {'complete_peak_bytes': peak, 'live_headroom_bytes': free,
+ 'sample_seconds': sample, 'host_stage_seconds': staged,
+ 'host_stage_fraction': staged / sample, 'extra_blocks': extra,
+ 'additional_gpu_margin_bytes': GiB // 4,
+ 'scope': 'Host staging includes RAM copies and file reads; it is not a physical disk measurement'},
+ numerical='placement-neutral', source='complete-local-request')
+
+ from .compute_budget import spare_compute_trials
+ for patch, observation in spare_compute_trials(row, engine, peak, free):
+ observation.update(complete_peak_bytes=peak, headroom_bytes=free,
+ memory_estimate_scope='Trial bound only; complete local validation required')
+ offer(patch, observation['reason'], observation, source='complete-local-request',
+ risk=('unvalidated-concurrency' if 'head_parallelism' in patch else
+ 'numerical-validation-required' if 'window_batch' in patch else 'recoverable-memory'))
+
+ # A hard sub-10 GiB tier missed this complete 12/32 result by only 34 MiB.
+ # Nominate the measured high-value knobs independently; a shared result
+ # predicts a candidate, never certifies another GPU or numerical equality.
+ if (engine.get('linear_compute') == 'native-fp8' and engine.get('attention_cpu_outputs')
+ and engine.get('grouped_attention_outputs') and 0 < engine['head_chunk'] < 8
+ and engine['attention'].split('/')[-1] not in ('fa2', 'fa4')):
+ offer({'head_chunk': 8}, 'Test wider head groups before growing weight residency; keep the current window grouping',
+ {'device': 'Linux Blackwell, 12 GiB whole-GPU / 28 GiB RAM cgroup',
+ 'geometry': {'width': 1344, 'height': 768, 'frames': 243},
+ 'baseline_seconds_per_nfe': 39.404771428571884, 'candidate_seconds_per_nfe': 24.80211799457902,
+ 'sampling_speedup': 1.589, 'baseline_whole_gpu_peak_bytes': 10592059392,
+ 'candidate_whole_gpu_peak_bytes': 10552213504,
+ 'bitwise_equal': False, 'rgb_psnr_db': 17.737965055277115,
+ 'scope': 'One complete matched request; equal memory use is not proof for other geometries. '
+ 'Head 8/window 1 was slightly faster than 8/4; this is not a repeatability claim.'},
+ source='benchmarks/2026-09-14-head-window-capacity.json', risk='numerical-validation-required')
+ if engine['window_batch'] < 4:
+ offer({'window_batch': 4}, 'Test window batching independently when a wider head group changes the prediction',
+ {'baseline_seconds_per_nfe': 39.404771428571884, 'candidate_seconds_per_nfe': 33.56010567014372,
+ 'sampling_speedup': 1.174, 'bitwise_equal': False, 'rgb_psnr_db': 17.377521730600915,
+ 'scope': 'Same complete 12/32 request, head 2 unchanged. First-step rejection saves time; full validation is still required.'},
+ source='benchmarks/2026-09-14-head-window-capacity.json', risk='numerical-validation-required')
+
+ # Compute comes before unmeasured occupancy, after a locally measured disk
+ # bottleneck if present. In the matching Linux
+ # complete run head 4 and head 2 reserved exactly the same bytes, so the old
+ # universal 3 GiB admission threshold had no evidence.
+ if engine['head_chunk'] < 16:
+ offer({'head_chunk': min(16, max(4, engine['head_chunk'] * 2))},
+ 'Test head parallelism when no complete local storage bottleneck takes priority',
+ 'Head 2 to 4: complete Linux Blackwell sample 270.5 to 196.0 s (1.38x), identical peak',
+ risk='numerical-validation-required')
+ if engine.get('attention_cpu_outputs'):
+ offer({'attention_cpu_outputs': False, 'grouped_attention_outputs': False},
+ 'Remove attention host round-trips; recoverable OOM must descend without changing the request',
+ '1.26x in three-step Ada probes only; complete-request peak unmeasured, no slope extrapolation')
+ if engine.get('fp8_ff_recompute') and engine['linear_compute'] == 'native-fp8':
+ offer({'fp8_ff_recompute': False}, 'Avoid recomputing FF activations before increasing weight storage',
+ '1.046x in Ada probes; reserved bytes unchanged there; complete combined change also succeeded')
+ if engine['attention'].split('/')[-1] not in ('fa2', 'fa4') and engine['window_batch'] < 8:
+ offer({'window_batch': 8}, 'Try the fastest measured looped window batch; never enable packed varlen implicitly',
+ 'Isolated looped attention 1.20x, about 4% sampling; full local equivalence remains to be measured',
+ risk='numerical-validation-required')
+ if engine['linear_compute'] == 'native-fp8' and engine['ff_chunk'] < 4096:
+ offer({'ff_chunk': 4096}, 'Test fewer FF chunks after the measured high-value compute changes',
+ 'No universal full-request speedup established for this chunk size')
+ if engine['projection_chunk'] < 4096:
+ offer({'projection_chunk': 4096}, 'Test larger projection chunks after head and attention-output changes',
+ 'No universal full-request speedup established for this chunk size')
+ if not engine['prefetch'] and engine['resident_blocks'] < 50:
+ offer({'prefetch': True}, 'Try overlapping remaining weight transfers after compute candidates',
+ 'Transformer weight H2D was only 6.78 s of 878.4 s sampling on the Ada laptop')
+
+ # Placement remains eligible, but a full-run peak is the minimum evidence
+ # for turning unused bytes into more residency. Do not update this estimate
+ # from a one-step probe, a truncated request or a changed chunk configuration.
+ if free is not None:
+ room = min(50 - engine['resident_blocks'], max(0, int((free - .25 * GiB) / BLOCK_BYTES)))
+ for extra in ([room, 2] if room > 2 else [room]):
+ if extra:
+ resident = engine['resident_blocks'] + extra
+ offer({'resident_blocks': resident,
+ 'pin_host_gb': math.floor(min(engine['pin_host_gb'], (50 - resident) * BLOCK_BYTES / 1e9) * 1000) / 1000},
+ 'Spend measured whole-request headroom only after higher-value compute trials',
+ {'complete_peak_bytes': peak, 'live_headroom_bytes': free, 'extra_blocks': extra,
+ 'speed_evidence': 'Cached Ada weights showed approximately zero residency gain; this does not establish the value when weights are read from disk'},
+ numerical='placement-neutral')
+ ram_peak = memory_peak(row.get('ram', {}))
+ if _positive(ram_peak):
+ ram_free = base['inference_ram_budget_gb'] * 1e9 - ram_peak
+ if live_ram_available is not None:
+ owned = _owned_working_memory(live_ram_sample, base.get('policy', {}).get('hardware', {}), ram_peak)
+ ram_free = min(ram_free, live_ram_available + owned - ram_peak - (2 if windows() else 1) * GiB)
+ pin = min(22., (50 - engine['resident_blocks']) * BLOCK_BYTES / 1e9,
+ engine['pin_host_gb'] + min(GiB, max(0, ram_free - GiB)) / 1e9)
+ if pinning_limit_bytes is not None:
+ pin = min(pin, pinning_limit_bytes / 1e9)
+ if pin > engine['pin_host_gb'] + .2:
+ offer({'pin_host_gb': math.floor(pin * 1000) / 1000, 'resident_blocks': engine['resident_blocks']},
+ 'Pinning is last: only test bounded host storage after higher-value candidates',
+ {'sampling_gain_measured': .0057, 'local_pinning_limit_bytes': pinning_limit_bytes,
+ 'warning': 'Free RAM does not establish the platform locked-memory ceiling'},
+ numerical='placement-neutral')
+ return {'candidates': options[:maximum], 'rejected': rejected,
+ 'memory_evidence': {'complete_peak_bytes': peak, 'headroom_bytes': free, 'reason': peak_reason}}
+
+
+def candidates(row, base, maximum=2, *, live_gpu_free=None, live_ram_available=None,
+ reject=None, pinning_limit_bytes=None, live_ram_sample=None):
+ """Backward-compatible patch list; use candidate_plan for auditable reasons."""
+ plan = candidate_plan(row, base, maximum, live_gpu_free=live_gpu_free,
+ live_ram_available=live_ram_available, reject=reject,
+ pinning_limit_bytes=pinning_limit_bytes, live_ram_sample=live_ram_sample)
+ return [item['patch'] for item in plan['candidates']]
diff --git a/freevideo_engine/two_pass.py b/freevideo_engine/two_pass.py
new file mode 100644
index 0000000..2a32850
--- /dev/null
+++ b/freevideo_engine/two_pass.py
@@ -0,0 +1,177 @@
+"""Automatic 8 + 2 generation planning, without importing Torch."""
+from math import gcd
+from pathlib import Path
+
+UPSCALER = dict(
+ repo='LBH-123-AI/Minimax_h3_latent_Upscaler',
+ revision='3f941d5d182014dd5c0a5e16330420ee2d4aa0c6',
+ file='minimax_h3_latent_upscaler_3d_conv_v1/minimax_h3_latent_upscaler_3d_conv_v1_fp16.safetensors',
+ bytes=690592672,
+ sha256='043e5a48e161610ef6c3ea974645220354d06fa618abca15f76d084812eb55c2',
+ role='latent_upscaler')
+
+
+def plan(canvas, enabled=True, task='t2va'):
+ """Align the smaller canvas, lift, center-crop, then refine at the target.
+
+ Odd multiples of 32 need up to 32 extra pixels before the latent crop.
+ Small canvases use a smaller factor or same-size refinement. Size never
+ disables a requested second pass; memory pressure cannot change its math.
+ """
+ from .geometry import geometry
+ from .media_request import TASKS
+ if task not in TASKS:
+ raise ValueError('Unsupported two-pass conditioning task: ' + str(task))
+ target = geometry(canvas['width'], canvas['height'], frames=canvas['frames'])
+ shape = {key: target[key] for key in ('width', 'height', 'frames')}
+ result = dict(version=1, requested=bool(enabled), enabled=False, mode='vdn8',
+ base_steps=8, refine_steps=0, total_steps=8, first=shape, second=None)
+ if not enabled:
+ result['reason'] = 'Two-pass generation is disabled.'
+ return result
+ if min(target['width'], target['height']) >= 512:
+ first = dict(width=((target['width'] + 63) // 64) * 32,
+ height=((target['height'] + 63) // 64) * 32, frames=target['frames'])
+ lift = dict(first, width=2 * first['width'], height=2 * first['height'])
+ else:
+ # Avoid a large field-of-view crop just to force 2x on a small input.
+ w, h = target['width'] // 32, target['height'] // 32
+ divisor = gcd(w, h)
+ unit_w, unit_h = w // divisor, h // divisor
+ multiple = max((divisor + 1) // 2, (8 + unit_w - 1) // unit_w, (8 + unit_h - 1) // unit_h)
+ first = dict(width=32 * unit_w * multiple, height=32 * unit_h * multiple, frames=target['frames'])
+ lift = dict(shape)
+ crop = dict(left=(lift['width'] - target['width']) // 2,
+ top=(lift['height'] - target['height']) // 2,
+ width=target['width'], height=target['height'])
+ upscaling = first != lift
+ result.update(enabled=True, mode='vdn8-lbh-tail2', refine_steps=2, total_steps=10,
+ first=first, second=shape, upscale_target=lift, crop=crop,
+ restart_seed_offset=1,
+ upscaler_sha256=UPSCALER['sha256'] if upscaling else None,
+ audio_policy='first_pass_preserved_with_audio_clock_conditioning',
+ reason='8 steps on a smaller canvas, learned latent upscale, then the original DMD8 schedule tail (2 steps).')
+ if not upscaling:
+ result['reason'] = 'Small canvas: 8 steps and 2 refinement steps at the same target size, without upscaling.'
+ return result
+
+
+def crop_latents(video, sampling_plan):
+ """Remove alignment padding in normalized latent space before tail sampling."""
+ crop = sampling_plan['crop']
+ lift = sampling_plan['upscale_target']
+ if tuple(video.shape[-2:]) != (lift['height'] // 16, lift['width'] // 16):
+ raise ValueError('Upscaled latents do not match the planned canvas')
+ top, left, height, width = (crop[key] // 16 for key in ('top', 'left', 'height', 'width'))
+ if top == left == 0 and (height, width) == tuple(video.shape[-2:]):
+ return video
+ return video[..., top:top + height, left:left + width].contiguous()
+
+
+def steps(sampling_plan=None):
+ """Validate the supported receipt rather than trusting a supplied NFE count."""
+ if sampling_plan is None:
+ return 8
+ expected = 10 if sampling_plan.get('enabled') is True else 8
+ if (sampling_plan.get('version') != 1 or sampling_plan.get('base_steps') != 8
+ or sampling_plan.get('refine_steps') != expected - 8
+ or sampling_plan.get('total_steps') != expected):
+ raise ValueError('Invalid 8 + 2 sampling plan')
+ return expected
+
+
+def same_strategy(left, right):
+ """Do not mix mixed-resolution timings with ordinary full-resolution steps."""
+ a, b = left.get('sampling_plan'), right.get('sampling_plan')
+ try:
+ if steps(a) != steps(b):
+ return False
+ except (ValueError, AttributeError, TypeError):
+ return False
+ # Until a two-resolution cost model is calibrated, use exact-plan evidence.
+ return steps(a) == 8 or a == b
+
+
+def checkpoint_path():
+ from .paths import model_root
+ return model_root() / 'latent_upscaler' / Path(UPSCALER['file']).name
+
+
+def ensure_checkpoint():
+ """Also upgrade existing installations lazily; retain invalid local files."""
+ from .network import download, model_urls
+ path = checkpoint_path()
+ network = {}
+ from .paths import data_root
+ import json
+ machine = data_root() / 'machine.json'
+ if machine.is_file():
+ installed = json.loads(machine.read_text(encoding='utf-8'))
+ network = installed.get('network', {}) or {}
+ if not network and installed.get('setup_run'):
+ setup = Path(installed['setup_run']) / 'plan.json'
+ if setup.is_file():
+ network = json.loads(setup.read_text(encoding='utf-8')).get('network', {}) or {}
+ network.setdefault('download_settings_path', str(data_root() / 'download-settings.json'))
+ network.setdefault('sources', {}).setdefault('models', [{'id': 'official'}, {'id': 'hf-mirror'}])
+ download(model_urls(network, UPSCALER), path, UPSCALER['sha256'],
+ network=network, size=UPSCALER['bytes'], category='models')
+ return path
+
+
+def upscale_workspace(canvas):
+ """Conservative admission estimate, not a measured capacity guarantee."""
+ latent_frames = (canvas['frames'] - 5) // 17 * 5 + 2
+ feature = latent_frames * (canvas['width'] // 16) * (canvas['height'] // 16) * 512 * 2
+ return 2 * 2**30 + max(4 * 2**30, 6 * feature)
+
+
+def pass_cache_allowance(budget, peak_reserved, free, reserve, commit_available=None):
+ """Spare capacity after one real step, bounded independently by live space.
+
+ Keep another 512 MiB beyond the existing OS reserve for pass-local growth.
+ Windows GPU allocations can also consume commit; retain host workspace.
+ """
+ bounds = [budget - peak_reserved, free - reserve]
+ if commit_available is not None:
+ # Sampling workspace has already been exercised. This cache creates no
+ # new host tensors, so do not reserve the full model-loading allowance
+ # again. Still leave 2 GiB of system commit before optional GPU growth.
+ bounds.append(commit_available - 2 * 2**30)
+ return max(0, int(min(bounds)) - 512 * 2**20)
+
+
+def first_pass_policy(profile, canvas, sampling_plan):
+ """Run the existing token-aware policy within the admitted request budgets.
+
+ The final pass keeps its already selected profile, including local evidence.
+ Host weight storage is shared across passes; this result selects computation
+ and a temporary GPU-residency target without rebuilding the transformer.
+ """
+ from dataclasses import replace
+ from .geometry import geometry
+ from .hardware import Hardware, GiB
+ from .policy import choose
+ if not sampling_plan.get('enabled'):
+ return None
+ policy = profile.get('policy', {})
+ if not policy.get('hardware'):
+ return None
+ hardware = Hardware.from_dict(policy['hardware'])
+ gpu_reserve = policy['gpu_system_reserve_bytes']
+ ram_reserve = policy['ram_system_reserve_bytes']
+ gpu = round(profile['gpu_budget_gb'] * 1e9)
+ ram = round(profile['inference_ram_budget_gb'] * 1e9)
+ hardware = replace(hardware, vram_free=min(hardware.vram_total, gpu + gpu_reserve),
+ ram_available=min(hardware.ram_total, ram + ram_reserve))
+ first = geometry(**sampling_plan['first'])
+ # Reference audio does not shrink with the image. Retain the full reference
+ # video allowance too; keyframes may be resized but ref2va inputs need not be.
+ for name in ('reference_video_tokens', 'reference_audio_tokens'):
+ if name in canvas:
+ first[name] = canvas[name]
+ backend = profile['engine']['attention']
+ selected = choose(hardware, attention=backend, available_backends=set(backend.split('/')),
+ gpu_reserve_gib=gpu_reserve / GiB, ram_reserve_gib=ram_reserve / GiB,
+ canvas=first, allow_capacity_trial=policy.get('capacity_trial', False)).legacy_profile()
+ return dict(geometry=first, profile=selected)
diff --git a/freevideo_engine/two_pass_metrics.py b/freevideo_engine/two_pass_metrics.py
new file mode 100644
index 0000000..4ae14f1
--- /dev/null
+++ b/freevideo_engine/two_pass_metrics.py
@@ -0,0 +1,36 @@
+"""Combine actual stage receipts without treating 8 + 2 as 8 full-size steps."""
+
+
+def sampling_workspace_peak(sampled):
+ """Peak needed again, excluding only measured optional pass-cache planes.
+
+ Each pass has its own CUDA peak/reset. Never subtract a first-pass cache
+ from the refinement or upscaler, which may have the larger real workspace.
+ Missing cache measurements conservatively keep the full reserved peak.
+ """
+ peaks = []
+ for stage in sampled.get('sampling_passes') or [sampled]:
+ cache = stage.get('pass_cache_admission', {})
+ peaks.append(max(cache.get('workspace_peak_bytes', 0),
+ stage['torch_peak_reserved_bytes'] - cache.get('peak_cache_bytes', 0)))
+ peaks.append(sampled.get('latent_upscale', {}).get('torch_peak_reserved_bytes', 0))
+ return max(peaks)
+
+
+def combine(first, second, upscale, sampling_plan):
+ result = dict(second, sampling_plan=sampling_plan,
+ sampling_passes=[dict(first), dict(second)], latent_upscale=upscale,
+ step_seconds=list(first['step_seconds']) + list(second['step_seconds']),
+ sample_seconds=first['sample_seconds'] + upscale['stage_seconds'] + second['sample_seconds'])
+ for key in ('torch_peak_allocated_bytes', 'torch_peak_reserved_bytes'):
+ result[key] = max(first[key], second[key], upscale.get(key, 0))
+ for key in ('attention_backend_calls', 'fp8_kernel_calls'):
+ rows = [value[key] for value in (first, second) if value.get(key) is not None]
+ result[key] = ({name: sum(row.get(name, 0) for row in rows)
+ for name in set().union(*(row.keys() for row in rows))} if rows else None)
+ result['residual_offload_steps'] = list(first.get('residual_offload_steps', [])) + list(second.get('residual_offload_steps', []))
+ if first.get('head_execution') and second.get('head_execution'):
+ result['head_execution'] = dict(second['head_execution'])
+ for name in ('parallel_head_calls', 'parallel_head_warmups'):
+ result['head_execution'][name] = first['head_execution'].get(name, 0) + second['head_execution'].get(name, 0)
+ return result
diff --git a/freevideo_engine/vae_tiles.py b/freevideo_engine/vae_tiles.py
new file mode 100644
index 0000000..97ae65a
--- /dev/null
+++ b/freevideo_engine/vae_tiles.py
@@ -0,0 +1,89 @@
+# Copyright 2026 The MiniMax and HuggingFace Teams. All rights reserved.
+# Copyright 2026 FreeVideo contributors.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy at http://www.apache.org/licenses/LICENSE-2.0
+# Unless required by applicable law or agreed to in writing, software distributed
+# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+# CONDITIONS OF ANY KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations under the License.
+"""Transpose the tile/layer loops to transfer each decoder layer once per clip.
+
+Prologue/epilogue adapted from diffusers' MiniMaxH3VideoViTDecoder3d, pinned at
+VDN 30b6b380c2482f3519469350810c2955d8847fd9. Spatial tile geometry, per-tile
+GEMM shapes, temporal chunking and stitching remain those of the original VAE.
+"""
+import types
+from contextlib import nullcontext
+import torch
+
+from .offload import LayerOffloader
+
+
+class TileDecoder:
+ def __init__(self, decoder, prefetch=True, weight_source=None, resident_blocks=0):
+ self.decoder = decoder
+ self.resident_blocks = resident_blocks
+ self.offloader = LayerOffloader(list(decoder.transformer_blocks)[resident_blocks:], prefetch=prefetch, manage_hooks=False,
+ weight_source=weight_source)
+
+ def decode(self, tiles):
+ model = self.decoder
+ states = []
+ shapes = []
+ ropes = []
+ for tile in tiles:
+ batch, channels, frames, height, width = tile.shape
+ hidden = tile.permute(0, 2, 3, 4, 1).reshape(batch, frames * height * width, channels)
+ hidden = model.proj_in(hidden)
+ hidden = torch.cat([hidden, model.register_tokens.expand(batch, -1, -1),
+ torch.zeros_like(hidden[:, :1, :])], dim=1)
+ grids = [2.0 * (torch.arange(0.5, size, dtype=torch.float32, device=hidden.device) / size) - 1.0
+ for size in (frames, height, width)]
+ position = torch.stack(torch.meshgrid(*grids, indexing='ij'), dim=-1).flatten(0, 2)
+ position = position.unsqueeze(0).expand(batch, -1, -1)
+ position = torch.cat([position, position.new_zeros((batch, model.num_register_tokens + 1, 3))], dim=1)
+ states.append(hidden)
+ shapes.append((batch, frames, height, width))
+ ropes.append(model.rope(position))
+ for index in range(len(model.transformer_blocks)):
+ context = (nullcontext(model.transformer_blocks[index]) if index < self.resident_blocks
+ else self.offloader.layer(index - self.resident_blocks))
+ with context as block:
+ for tile_index, hidden in enumerate(states):
+ states[tile_index] = block(hidden, ropes[tile_index])
+ outputs = []
+ for hidden, shape in zip(states, shapes):
+ batch, frames, height, width = shape
+ hidden = model.proj_out(model.norm_out(hidden))[:, :frames * height * width]
+ hidden = hidden.view(batch, frames, height, width, model.out_channels,
+ model.patch_size_t, model.patch_size, model.patch_size)
+ hidden = hidden.permute(0, 4, 1, 5, 2, 6, 3, 7).contiguous()
+ outputs.append(hidden.reshape(batch, model.out_channels, frames * model.patch_size_t,
+ height * model.patch_size, width * model.patch_size))
+ return outputs
+
+ def install(self, vae):
+ self.original = vae._decode_clip
+ self.vae = vae
+
+ def decode_clip(module, z):
+ if not module.use_tiling:
+ return self.decode([module.post_quant_conv(z)])[0]
+ ratio = module.spatial_compression_ratio
+ yi, yl, yo = module._split_tiles(z.shape[-2] * ratio, module.tile_sample_min_height,
+ module.tile_sample_min_overlap_height)
+ xi, xl, xo = module._split_tiles(z.shape[-1] * ratio, module.tile_sample_min_width,
+ module.tile_sample_min_overlap_width)
+ tiles = [module.post_quant_conv(z[..., y // ratio:(y + h) // ratio, x // ratio:(x + w) // ratio])
+ for y, h in zip(yi, yl) for x, w in zip(xi, xl)]
+ values = self.decode(tiles)
+ rows = [values[start:start + len(xi)] for start in range(0, len(values), len(xi))]
+ return module._stitch_tiles(rows, yo, xo)
+
+ vae._decode_clip = types.MethodType(decode_clip, vae)
+
+ def close(self):
+ self.offloader.close()
+ if hasattr(self, 'vae'):
+ self.vae._decode_clip = self.original
diff --git a/freevideo_engine/vae_weights.py b/freevideo_engine/vae_weights.py
new file mode 100644
index 0000000..d8d2d69
--- /dev/null
+++ b/freevideo_engine/vae_weights.py
@@ -0,0 +1,113 @@
+"""Bounded preparation of H3 decoder Linear weights for FP16 autocast.
+
+This is an explicit experiment, not a blanket VAE precision change. RMSNorm,
+LayerNorm, residual scales, register tokens and post-quantization stay FP32.
+Original checkpoint bytes and the encoder used for input images are untouched.
+"""
+from pathlib import Path
+
+
+def load_parameters(model, paths, *, device_for_name):
+ """Read one tensor at a time, with no retained full FP32 state dictionary.
+
+ CPU tensors are copied even when their dtype is unchanged, so a tiny norm
+ parameter cannot retain the mapping containing gigabytes of consumed weights.
+ All shapes/keys are checked before reading payloads; changed files fail closed.
+ """
+ import torch
+ from .tensor_io import open_tensors
+ expected = dict(model.named_parameters())
+ files, sources = {}, {}
+ for path in sorted(Path(p) for p in paths):
+ stamp = path.stat()
+ files[path] = (stamp.st_size, stamp.st_mtime_ns, stamp.st_ctime_ns)
+ with open_tensors(str(path), framework='pt') as handle:
+ for name in handle.keys():
+ if name.startswith(('encoder.', 'quant_conv.')):
+ continue
+ if name not in expected or name in sources:
+ raise ValueError('Unexpected or duplicate VAE decoder weight: ' + name)
+ metadata = handle.get_slice(name)
+ if tuple(metadata.get_shape()) != tuple(expected[name].shape):
+ raise ValueError('VAE decoder weight shape mismatch: ' + name)
+ if not (metadata.get_dtype().startswith('F') or metadata.get_dtype() == 'BF16'):
+ raise ValueError('VAE weight is not floating point: ' + name)
+ sources[name] = path
+ if set(sources) != set(expected):
+ raise ValueError('Missing VAE decoder weights: ' + ', '.join(sorted(set(expected) - set(sources))))
+ converted, loaded, streamed_count, streamed_bytes = 0, 0, 0, 0
+ for path, stamp in files.items():
+ names = [name for name, source in sources.items() if source == path]
+ if not names:
+ continue
+ for name in names:
+ parent, _, leaf = name.rpartition('.')
+ owner = model.get_submodule(parent) if parent else model
+ dtype = torch.float16 if isinstance(owner, torch.nn.Linear) else torch.float32
+ device = torch.device(device_for_name(name))
+ if device.type == 'meta':
+ # Only metadata is needed until a streamed layer executes.
+ # Reading the whole CPU remainder here defeats streaming even
+ # if its storage is discarded by the later offloader.
+ target = torch.empty(expected[name].shape, dtype=dtype, device='meta')
+ owner._parameters[leaf] = torch.nn.Parameter(target, requires_grad=False)
+ streamed_count += 1
+ streamed_bytes += target.numel() * target.element_size()
+ continue
+ # A long-lived shard handle keeps consumed pages mapped even after
+ # individual tensors are released. Close it after every upload.
+ with open_tensors(str(path), framework='pt') as handle:
+ source = handle.get_tensor(name)
+ if not source.is_floating_point():
+ raise ValueError('VAE weight is not floating point: ' + name)
+ # Match from_pretrained's FP32 parameter dtype before autocast,
+ # including the rounding of a non-FP32 checkpoint tensor.
+ target = source.to(dtype=torch.float32).to(device=device, dtype=dtype, copy=True)
+ owner._parameters[leaf] = torch.nn.Parameter(target, requires_grad=False)
+ converted += int(dtype == torch.float16)
+ loaded += target.numel() * target.element_size()
+ del source, target
+ current = path.stat()
+ if (current.st_size, current.st_mtime_ns, current.st_ctime_ns) != stamp:
+ raise ValueError('VAE checkpoint changed during preparation: ' + str(path))
+ return dict(linear_parameter_count=converted, prepared_parameter_bytes=loaded,
+ streamed_parameter_count=streamed_count, streamed_parameter_bytes=streamed_bytes,
+ source_policy='Original FP32 checkpoint, one tensor at a time',
+ compute_policy='FP16 Linear weights; FP32 normalization, residual scales and other parameters')
+
+
+def load_video_decoder(base, *, resident_blocks, weight_source=None):
+ """Place the selected decoder prefix directly on CUDA without a CPU copy."""
+ import torch
+ from diffusers import AutoencoderKLMiniMaxH3
+ directory = Path(base) / 'vae'
+ config = AutoencoderKLMiniMaxH3.load_config(str(directory), local_files_only=True)
+ with torch.device('meta'):
+ model = AutoencoderKLMiniMaxH3.from_config(config)
+ if type(resident_blocks) is not int or not 0 <= resident_blocks <= len(model.decoder.transformer_blocks):
+ raise ValueError('Invalid VAE resident block count')
+ if weight_source is not None and weight_source.prefixes != tuple(
+ f'decoder.transformer_blocks.{index}.' for index in range(
+ resident_blocks, len(model.decoder.transformer_blocks))):
+ raise ValueError('Streamed VAE source does not match the offloaded layer order')
+ model.encoder = None
+ model.quant_conv = None
+ # The pinned decoder has one non-persistent buffer, generated by its own
+ # constructor rather than read from the checkpoint. Preserve its FP32 math.
+ model.decoder.rope = type(model.decoder.rope)(
+ int(model.config.decoder_attention_head_dim * model.config.decoder_rope_dim_ratio),
+ theta=model.config.decoder_rope_theta)
+ if any(value.is_meta for value in model.buffers()):
+ raise ValueError('Unexpected unmaterialized VAE buffer')
+ def placement(name):
+ prefix = 'decoder.transformer_blocks.'
+ if name.startswith(prefix) and int(name[len(prefix):].split('.', 1)[0]) >= resident_blocks:
+ return 'meta' if weight_source is not None else 'cpu'
+ return 'cuda'
+ metrics = load_parameters(model, directory.glob('*.safetensors'), device_for_name=placement)
+ if weight_source is not None:
+ from .offload import initialize_streamed_layer
+ for index, layer in enumerate(list(model.decoder.transformer_blocks)[resident_blocks:]):
+ initialize_streamed_layer(layer, weight_source, index)
+ model.eval().requires_grad_(False)
+ return model, metrics
diff --git a/freevideo_engine/validation.py b/freevideo_engine/validation.py
new file mode 100644
index 0000000..066d9fd
--- /dev/null
+++ b/freevideo_engine/validation.py
@@ -0,0 +1,137 @@
+"""Validate retained test outputs without loading Torch or unpickling tensors."""
+import json
+import math
+import os
+import wave
+import zipfile
+from .system import memory_complete, memory_peak
+
+
+def read_case_reports(directory, row):
+ errors = []
+ for key, name in [('request', 'request'), ('encoding', 'encoding'), ('engine', 'engine')]:
+ path = directory / ('video.' + name + '.json')
+ try:
+ record = json.loads(path.read_text(encoding='utf-8'))
+ if not isinstance(record, dict):
+ raise ValueError('Expected a JSON object')
+ row[key] = record
+ except (OSError, ValueError) as error:
+ errors.append(path.name + ': ' + str(error))
+ if errors:
+ row['report_errors'] = errors
+ if row['status'] == 'complete':
+ row.update(status='validation_failed', error='Incomplete stage reports: ' + '; '.join(errors))
+
+
+def finite_number(value):
+ return isinstance(value, (int, float)) and not isinstance(value, bool) and math.isfinite(value) and value >= 0
+
+
+def validate_metrics(row, canvas, *, preencoded=False):
+ for name in ('request', 'engine') + (() if preencoded else ('encoding',)):
+ if row.get(name, {}).get('success') is not True:
+ raise ValueError(name + ' did not report successful completion')
+ engine = row['engine']
+ if engine.get('finite_latents') is not True:
+ raise ValueError('Engine did not confirm finite video and audio latents')
+ for name in ('request', 'engine'):
+ if any(row[name].get('geometry', {}).get(k) != canvas[k] for k in ('width', 'height', 'frames', 'fps')):
+ raise ValueError(name + ' geometry does not match the requested canvas')
+ steps = engine.get('step_seconds', [])
+ from .two_pass import steps as planned_steps
+ sampling_plan = row['request'].get('sampling_plan')
+ if (engine.get('sampling_plan') != sampling_plan or engine.get('config', {}).get('steps') != 8
+ or len(steps) != planned_steps(sampling_plan) or not all(finite_number(v) for v in steps)):
+ raise ValueError('Expected all planned denoising steps with finite timings and matching sampling strategy')
+ timings = [('request', ('request_seconds',)),
+ ('engine', ('load_seconds', 'sample_seconds', 'decode_save_seconds', 'work_seconds'))]
+ if not preencoded:
+ timings.append(('encoding', ('load_seconds', 'work_seconds')))
+ for name, keys in timings:
+ if not all(finite_number(row[name].get(key)) for key in keys):
+ raise ValueError('Incomplete timing metrics in ' + name)
+ gpu, ram = row.get('gpu', {}), row.get('ram', {})
+ if gpu.get('sampling_errors') or not gpu.get('samples') or not gpu.get('gpu_peak_bytes'):
+ raise ValueError('GPU telemetry incomplete; generation completed but test metrics are incomplete.')
+ if not ram.get('ram_observation_samples') or not memory_complete(ram) or not memory_peak(ram):
+ raise ValueError('RAM telemetry incomplete; generation completed but test metrics are incomplete.')
+
+
+def _retained_array_header(path):
+ """Check numeric NPY metadata and payload length before any memory mapping."""
+ import numpy as np
+ from numpy.lib import format
+ with path.open('rb') as stream:
+ version = format.read_magic(stream)
+ if version == (1, 0):
+ reader = format.read_array_header_1_0
+ elif version in ((2, 0), (3, 0)):
+ # V3 only changes header text encoding. The scalar numeric dtypes
+ # accepted below have ASCII descriptors in both versions; Unicode
+ # structured dtypes are rejected before mapping.
+ reader = format.read_array_header_2_0
+ else:
+ raise ValueError('Unsupported retained NPY version: ' + path.name)
+ shape, _, dtype = reader(stream)
+ if dtype not in (np.dtype(np.uint8), np.dtype(np.float32)):
+ raise ValueError('Expected retained uint8 or float32 array: ' + path.name)
+ if any(type(size) is not int or size < 0 for size in shape):
+ raise ValueError('Invalid retained array shape: ' + path.name)
+ # Use Python integers: a malformed shape must not overflow the size
+ # check or ask Windows to map beyond EOF (which reports WinError 8).
+ expected = math.prod(shape) * dtype.itemsize
+ available = os.fstat(stream.fileno()).st_size - stream.tell()
+ if available < expected:
+ raise ValueError('Truncated retained %s: expected %d data bytes, found %d' %
+ (path.name, expected, available))
+ return shape, dtype
+
+
+def validate_artifacts(directory, canvas, *, preencoded=False):
+ import numpy as np
+ root = directory / 'video.artifacts'
+ required = ['conditioning.pt', 'latents.pt', 'rgb.npy', 'audio.npy', 'audio.wav', 'video.json']
+ if not preencoded:
+ required += ['prompt.txt', 'encode.json']
+ missing = [n for n in required if not (root / n).is_file() or not (root / n).stat().st_size]
+ if missing:
+ raise ValueError('Missing or empty retained artifacts: ' + ', '.join(missing))
+ if not preencoded and (root / 'prompt.txt').read_text(encoding='utf-8') != (directory / 'prompt.txt').read_text(encoding='utf-8'):
+ raise ValueError('Retained prompt differs from the test input')
+ for name in ('video.json',) + (() if preencoded else ('encode.json',)):
+ if not isinstance(json.loads((root / name).read_text(encoding='utf-8')), dict):
+ raise ValueError('Invalid retained request: ' + name)
+ rgb_shape, rgb_dtype = _retained_array_header(root / 'rgb.npy')
+ if rgb_dtype != np.uint8 or rgb_shape != (canvas['frames'], canvas['height'], canvas['width'], 3):
+ raise ValueError('Retained RGB shape/dtype does not match the video')
+ audio_shape, audio_dtype = _retained_array_header(root / 'audio.npy')
+ if audio_dtype != np.float32 or len(audio_shape) != 2 or audio_shape[0] != 2:
+ raise ValueError('Expected retained float32 stereo audio')
+ if abs(audio_shape[1] / 32000 - canvas['seconds']) > 1 / canvas['fps']:
+ raise ValueError('Retained audio duration differs from video by more than one frame')
+ audio = np.load(root / 'audio.npy', mmap_mode='r', allow_pickle=False)
+ # Bounded temporary arrays, even for long custom cases. RGB needs only
+ # metadata here; the artifact manifest streams its content hash separately.
+ for start in range(0, audio.shape[1], 32000):
+ if not np.isfinite(audio[:, start:start + 32000]).all():
+ raise ValueError('Non-finite retained audio')
+ with wave.open(str(root / 'audio.wav'), 'rb') as wav:
+ if (wav.getnchannels(), wav.getframerate(), wav.getnframes()) != (2, 32000, audio.shape[1]):
+ raise ValueError('Retained WAV does not match raw audio')
+ remaining = wav.getnframes() * wav.getsampwidth() * wav.getnchannels()
+ while remaining:
+ block = wav.readframes(32000)
+ if not block:
+ raise ValueError('Truncated retained WAV')
+ remaining -= len(block)
+ for name in ('conditioning.pt', 'latents.pt'):
+ with zipfile.ZipFile(root / name) as archive:
+ names = archive.namelist()
+ if not any(n.endswith('/data.pkl') for n in names) or not any('/data/' in n for n in names):
+ raise ValueError('Missing tensor data in ' + name)
+ bad = archive.testzip()
+ if bad:
+ raise ValueError('Tensor archive CRC failed: ' + name + '/' + bad)
+ return {'required_files': required, 'rgb_shape': list(rgb_shape), 'audio_shape': list(audio_shape),
+ 'tensor_archive_crc_pass': True, 'scope': 'Array shape/dtype, finite audio, WAV length and tensor archive integrity; no pickle loading.'}
diff --git a/freevideo_engine/weight_only.py b/freevideo_engine/weight_only.py
new file mode 100644
index 0000000..334de26
--- /dev/null
+++ b/freevideo_engine/weight_only.py
@@ -0,0 +1,91 @@
+"""FP8 storage with BF16 tensor-core compute for Ampere (W8A16).
+
+This is a separately reported arithmetic policy, not native FP8 GEMM. Decode
+E4M3 bytes inside each GEMM tile instead of materializing a BF16 weight copy.
+No FP8 tensor-core instruction or native float8 conversion is required.
+"""
+import torch
+import triton
+import triton.language as tl
+
+
+def _decode_e4m3(bits):
+ bits = bits.to(tl.uint32)
+ exponent = (bits >> 3) & 15
+ mantissa = bits & 7
+ # Reconstruct normal IEEE float bits; this avoids an exp2 per weight in
+ # every K tile while remaining usable before native FP8 hardware.
+ normal = ((exponent + 120) << 23) | (mantissa << 20)
+ value = tl.where(exponent == 0, mantissa.to(tl.float32) * (1. / 512.),
+ normal.to(tl.float32, bitcast=True))
+ value = tl.where((bits & 128) != 0, -value, value)
+ return tl.where((bits & 127) == 127, float('nan'), value)
+
+
+ triton.Config({'BM': 32, 'BN': 64, 'BK': 32}, num_warps=4, num_stages=3),
+ triton.Config({'BM': 64, 'BN': 64, 'BK': 32}, num_warps=4, num_stages=3),
+ triton.Config({'BM': 32, 'BN': 128, 'BK': 32}, num_warps=4, num_stages=3),
+], key=['M', 'N', 'K'])
+def _matmul(A, W, S, O, M: tl.constexpr, N: tl.constexpr, K: tl.constexpr,
+ SA: tl.constexpr, SW: tl.constexpr, COLUMNWISE: tl.constexpr,
+ BM: tl.constexpr, BN: tl.constexpr, BK: tl.constexpr):
+ # Reuse an eight-tile band of activations across output columns in L2.
+ # A full M sweep per column tile rereads the large packed input repeatedly.
+ blocks_m, blocks_n = triton.cdiv(M, BM), triton.cdiv(N, BN)
+ group = tl.program_id(0) // (8 * blocks_n)
+ first_m = group * 8
+ group_m = tl.minimum(blocks_m - first_m, 8)
+ within = tl.program_id(0) % (8 * blocks_n)
+ rows = (first_m + within % group_m) * BM + tl.arange(0, BM)
+ cols = (within // group_m) * BN + tl.arange(0, BN)
+ inner = tl.arange(0, BK)
+ scale = tl.load(S + cols, cols < N, other=1.) if COLUMNWISE else tl.load(S)
+ acc = tl.zeros((BM, BN), tl.float32)
+ for begin in range(triton.cdiv(K, BK)):
+ kk = begin * BK + inner
+ a = tl.load(A + rows[:, None] * SA + kk[None, :],
+ (rows[:, None] < M) & (kk[None, :] < K), other=0.)
+ bits = tl.load(W + cols[None, :] * SW + kk[:, None],
+ (cols[None, :] < N) & (kk[:, None] < K), other=0)
+ weight = _decode_e4m3(bits) * scale
+ acc += tl.dot(a, weight.to(a.dtype))
+ tl.store(O + rows[:, None] * N + cols[None, :], acc,
+ (rows[:, None] < M) & (cols[None, :] < N))
+
+
+def linear(value, weight_fp8, weight_scale, bias=None):
+ rows = value.reshape(-1, value.shape[-1]).contiguous()
+ if rows.dtype not in (torch.bfloat16, torch.float16):
+ raise ValueError('W8A16 expects BF16 or FP16 activations')
+ if weight_fp8.dtype != torch.float8_e4m3fn or not weight_fp8.is_contiguous():
+ raise ValueError('Expected contiguous E4M3 weights')
+ if weight_scale.numel() not in (1, weight_fp8.shape[0]):
+ raise ValueError('Expected scalar or per-output-channel weight scales')
+ m, k = rows.shape
+ n, weight_k = weight_fp8.shape
+ if k != weight_k:
+ raise ValueError('Projection input width differs from the weight')
+ output = torch.empty((m, n), device=rows.device, dtype=rows.dtype)
+ _matmul[lambda meta: (triton.cdiv(m, meta['BM']) * triton.cdiv(n, meta['BN']),)](
+ rows, weight_fp8.view(torch.uint8), weight_scale.contiguous(), output,
+ m, n, k, rows.stride(0), weight_fp8.stride(0), weight_scale.numel() != 1)
+ if bias is not None:
+ output.add_(bias)
+ return output.reshape(*value.shape[:-1], n)
+
+
+class WeightOnlyLinear(torch.nn.Module):
+ @property
+ def bias(self):
+ return self.original.bias
+
+ def project(self, value, channels):
+ scales = self.weight_scale if self.weight_scale.numel() == 1 else self.weight_scale[:, channels].contiguous()
+ return linear(value, self.weight_fp8[channels], scales,
+ None if self.bias is None else self.bias[channels])
+
+ def forward(self, value):
+ return linear(value, self.weight_fp8, self.weight_scale, self.bias)
diff --git a/freevideo_engine/weights.py b/freevideo_engine/weights.py
new file mode 100644
index 0000000..b2bf11d
--- /dev/null
+++ b/freevideo_engine/weights.py
@@ -0,0 +1,192 @@
+"""Prepare file-backed BF16 weights without materializing the entire model.
+
+LoRA deltas follow the upstream CPU FP32 merge order. Source files are read-only;
+the derived per-block cache records its own identity, preparation time and hashes.
+"""
+import hashlib
+import json
+from pathlib import Path
+import re
+import time
+
+from safetensors.torch import save_file
+import torch
+
+from .preparation import cache_location
+from .tensor_io import open_tensors
+
+
+def sha256(path):
+ with Path(path).open('rb') as stream:
+ return hashlib.file_digest(stream, 'sha256').hexdigest()
+
+
+def skeleton(base, checkpoint):
+ from .paths import add_vdn
+ add_vdn()
+ from diffusers import MiniMaxH3Transformer3DModel
+ from diffusers.models.transformers.transformer_minimax_h3 import MiniMaxH3RotaryPosEmbed
+ from src.models.hybrid_transform import apply_hybrid_attention_transform
+
+ config = json.loads((base / 'transformer/config.json').read_text(encoding='utf-8'))
+ spec = json.loads((checkpoint / 'model_spec.json').read_text(encoding='utf-8'))
+ with torch.device('meta'):
+ # Cast metadata only. Restore the official FP32 islands before reading
+ # any weights; from_pretrained would unnecessarily load a full model.
+ model = MiniMaxH3Transformer3DModel.from_config(config).bfloat16()
+ for name, module in model.named_modules():
+ if any(key in name for key in model._keep_in_fp32_modules):
+ module.float()
+ apply_hybrid_attention_transform(model, spec['transforms'][0]['config'])
+ # This nonpersistent buffer is computed, not stored in the checkpoint.
+ model.rope = MiniMaxH3RotaryPosEmbed(config['rope_freq_dim'], config['rope_theta'])
+ model.eval().requires_grad_(False)
+ return model
+
+
+class Sources:
+ def __init__(self, base, checkpoint):
+ self.weights = {}
+ self.loras = {}
+ self.paths = set()
+ for path in sorted((base / 'transformer').glob('*.safetensors')):
+ self.paths.add(path)
+ with open_tensors(path, framework='np') as stream:
+ for key in stream.keys():
+ target = re.sub(r'^(transformer_blocks\.\d+\.attn)\.', r'\1.orig.', key)
+ if target in self.weights:
+ raise ValueError('Duplicate base tensor: ' + target)
+ self.weights[target] = (path, key)
+ branch = checkpoint / 'linear_branch/model.safetensors'
+ self.paths.add(branch)
+ with open_tensors(branch, framework='np') as stream:
+ for key in stream.keys():
+ if key in self.weights:
+ raise ValueError('Branch replaces a base tensor: ' + key)
+ self.weights[key] = (branch, key)
+ for path in sorted((checkpoint / 'adapters').glob('*/adapter_model.safetensors')):
+ self.paths.add(path)
+ with open_tensors(path, framework='np') as stream:
+ keys = set(stream.keys())
+ for key in sorted(keys):
+ if '.lora_A.' not in key:
+ continue
+ other = key.replace('.lora_A.', '.lora_B.')
+ if other not in keys:
+ raise ValueError('Missing LoRA B: ' + other)
+ target = key.split('.lora_A.')[0] + '.weight'
+ if target not in self.weights:
+ target = target.replace('.attn.', '.attn.orig.', 1)
+ if target not in self.weights:
+ raise ValueError('Unknown LoRA target: ' + target)
+ self.loras.setdefault(target, []).append((path, key, other))
+
+ def tensor(self, name, assembly_dtype, final_dtype):
+ path, key = self.weights[name]
+ with open_tensors(path) as stream:
+ value = stream.get_tensor(key)
+ adapters = self.loras.get(name, [])
+ if adapters:
+ # Never modify a mapped source tensor, even with a private mapping.
+ value = value.to(assembly_dtype, copy=True)
+ for path, akey, bkey in adapters:
+ with open_tensors(path) as stream:
+ a = stream.get_tensor(akey).float()
+ b = stream.get_tensor(bkey).float()
+ delta = (b @ a) * 1.0
+ value.add_(delta.to(value.dtype))
+ del a, b, delta
+ return value.to(final_dtype).contiguous()
+
+
+def group_name(name):
+ parts = name.split('.')
+ if parts[0] != 'transformer_blocks':
+ return 'root'
+ return ('adaln/' if parts[2] == 'adaln_proj' else 'blocks/') + f'{int(parts[1]):02d}'
+
+
+def prepare(base, checkpoint, cache_root):
+ started = time.monotonic()
+ base, checkpoint, cache_root = map(Path, (base, checkpoint, cache_root))
+ model = skeleton(base, checkpoint)
+ sources = Sources(base, checkpoint)
+ params = dict(model.named_parameters())
+ if params.keys() != sources.weights.keys():
+ raise ValueError({'missing': sorted(params.keys() - sources.weights.keys()),
+ 'unused': sorted(sources.weights.keys() - params.keys())})
+ identity = {'implementation_sha256': sha256(__file__),
+ 'base_config_sha256': sha256(base / 'transformer/config.json'),
+ 'model_spec_sha256': sha256(checkpoint / 'model_spec.json'),
+ 'torch': torch.__version__, 'merge_policy': 'upstream_cpu_fp32_then_weight_dtype_add',
+ 'sources': [{'path': str(p), 'bytes': p.stat().st_size,
+ 'mtime_ns': p.stat().st_mtime_ns} for p in sorted(sources.paths)]}
+ output, identity, key = cache_location(cache_root, 'vdn-bf16-', identity)
+ output.mkdir(parents=True, exist_ok=True)
+ marker = output / 'source.json'
+ if marker.exists() and json.loads(marker.read_text(encoding='utf-8')) != identity:
+ raise ValueError('Prepared weight identity conflict: ' + str(output))
+ marker.write_text(json.dumps(identity, indent=2) + '\n', encoding='utf-8')
+ manifest = output / 'manifest.json'
+ if manifest.exists():
+ record = json.loads(manifest.read_text(encoding='utf-8'))
+ for item in record['groups']:
+ path = output / item['file']
+ if not path.is_file() or path.stat().st_size != item['bytes']:
+ raise ValueError('Prepared weight cache is incomplete: ' + str(path))
+ print(json.dumps({'cache': str(output), 'reused': True}), flush=True)
+ return output
+ groups = {}
+ for name, parameter in params.items():
+ groups.setdefault(group_name(name), []).append((name, parameter))
+ records = []
+ torch.set_grad_enabled(False)
+ for group, entries in sorted(groups.items()):
+ destination = output / (group + '.safetensors')
+ destination.parent.mkdir(parents=True, exist_ok=True)
+ progress = destination.with_suffix('.json')
+ if destination.exists() and progress.exists():
+ row = json.loads(progress.read_text(encoding='utf-8'))
+ if destination.stat().st_size == row['bytes'] and sha256(destination) == row['sha256']:
+ records.append(row)
+ continue
+ tensors = {}
+ for name, parameter in entries:
+ # Upstream converts the new hybrid parameters to BF16 after merging.
+ final_dtype = torch.bfloat16 if '.attn.' in name and name.startswith('transformer_blocks.') else parameter.dtype
+ value = sources.tensor(name, parameter.dtype, final_dtype)
+ if value.shape != parameter.shape:
+ raise ValueError(f'Weight shape mismatch: {name}: {value.shape} != {parameter.shape}')
+ tensors[name] = value
+ temporary = destination.with_suffix('.partial')
+ save_file(tensors, temporary)
+ del tensors, value
+ temporary.replace(destination)
+ row = {'group': group, 'file': str(destination.relative_to(output)),
+ 'bytes': destination.stat().st_size, 'sha256': sha256(destination),
+ 'tensors': len(entries)}
+ progress.write_text(json.dumps(row, indent=2) + '\n', encoding='utf-8')
+ records.append(row)
+ print(json.dumps({'prepared': group, 'bytes': row['bytes'],
+ 'elapsed_seconds': time.monotonic() - started}), flush=True)
+ record = {'source_id': key, 'groups': records,
+ 'preparation_seconds': time.monotonic() - started,
+ 'tensor_count': len(params), 'merged_lora_pairs': sum(map(len, sources.loras.values())),
+ 'arithmetic': 'BF16 weights with original FP32 modules and original CPU LoRA merge',
+ 'cuda_used': torch.cuda.is_initialized(), 'original_weights_modified': False}
+ temporary = manifest.with_suffix('.tmp')
+ temporary.write_text(json.dumps(record, indent=2) + '\n', encoding='utf-8')
+ temporary.replace(manifest)
+ print(json.dumps({'cache': str(output), **record}, indent=2), flush=True)
+ return output
+
+
+if __name__ == '__main__':
+ import argparse
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--base', required=True)
+ parser.add_argument('--checkpoint', required=True)
+ parser.add_argument('--cache-root', required=True)
+ args = parser.parse_args()
+ torch.set_num_threads(8)
+ prepare(args.base, args.checkpoint, args.cache_root)
diff --git a/freevideo_engine/widgets.py b/freevideo_engine/widgets.py
new file mode 100644
index 0000000..31c76df
--- /dev/null
+++ b/freevideo_engine/widgets.py
@@ -0,0 +1,342 @@
+"""Drawn Tk primitives shared by the native launcher and the engine panel.
+
+ttk cannot round a canvas-free surface or draw a gradient meter, so these are
+painted directly. Every size, radius and hue comes from `branding`.
+"""
+import tkinter as tk
+from tkinter import ttk
+
+from . import branding as b
+
+
+def _rgb(value):
+ return tuple(int(value[index:index + 2], 16) for index in (1, 3, 5))
+
+
+def _rounded(x0, y0, x1, y1, radius):
+ """Corner points for a smoothed polygon; Tk splines the rest."""
+ return (x0+radius, y0, x1-radius, y0, x1, y0, x1, y0+radius, x1, y1-radius, x1, y1,
+ x1-radius, y1, x0+radius, y1, x0, y1, x0, y1-radius, x0, y0+radius, x0, y0)
+
+
+class Card(tk.Canvas):
+ """A rounded surface. Only the outline carries status; fills stay SURFACE
+ so the ttk children on the card never need a second background."""
+
+ def __init__(self, parent, *, padding=None, radius=None, fill=b.SURFACE,
+ body_style='Card.TFrame', **kwargs):
+ super().__init__(parent, background=b.BACKGROUND, highlightthickness=0, width=1, **kwargs)
+ self.padding = b.space(5) if padding is None else padding
+ self.radius = b.RADIUS_LG if radius is None else radius
+ self.outline = b.BORDER
+ self.body = ttk.Frame(self, style=body_style)
+ self.surface = self.create_polygon(0, 0, 1, 1, fill=fill, outline=self.outline, smooth=True)
+ # A one-pixel lift just inside the flat top edge. It must not paint
+ # over the outline: on a tinted card that would replace the status
+ # colour with grey for the whole top border.
+ self.sheen = self.create_line(0, 0, 1, 0, fill=b.SHEEN, width=1)
+ self.inner = self.create_window(self.padding, self.padding, window=self.body, anchor='nw')
+ self.bind('<Configure>', self.resize)
+ self.body.bind('<Configure>', self.resize)
+
+ def tint(self, outline):
+ if outline != self.outline:
+ self.outline = outline
+ self.itemconfigure(self.surface, outline=outline)
+ # A status colour is its own highlight; grey on top would mute it.
+ self.itemconfigure(self.sheen, state='normal' if outline == b.BORDER else 'hidden')
+
+ def resize(self, _=None):
+ w = self.winfo_width()
+ self.itemconfigure(self.inner, width=max(1, w - 2*self.padding))
+ h = self.body.winfo_reqheight() + 2*self.padding
+ if int(float(self.cget('height'))) != h:
+ self.configure(height=h)
+ h = max(h, self.winfo_height())
+ r = self.radius
+ self.coords(self.surface, *_rounded(1, 1, w-1, h-1, r))
+ self.coords(self.sheen, r, 2, w-r, 2)
+ self.itemconfigure(self.surface, outline=self.outline)
+ self.itemconfigure(self.sheen, state='normal' if self.outline == b.BORDER else 'hidden')
+
+
+class Radio(tk.Canvas):
+ """A drawn selection dot. ttk's own indicator cannot sit on a card canvas,
+ and a text glyph does not match the drawn check in the consent box."""
+
+ def __init__(self, parent, size=None):
+ self.size = b.space(4) if size is None else size
+ super().__init__(parent, width=self.size, height=self.size,
+ background=b.SURFACE, highlightthickness=0)
+ self.select(False)
+
+ def select(self, selected, disabled=False):
+ self.delete('all')
+ radius, centre = self.size/2 - 1, self.size/2
+ hue = b.DISABLED if disabled else b.ACCENT if selected else b.MUTED
+ self.create_oval(centre-radius, centre-radius, centre+radius, centre+radius,
+ outline=hue, width=1.4,
+ fill=b.ACCENT_SUBTLE if selected and not disabled else b.SURFACE)
+ if selected:
+ inner = radius/2.4
+ self.create_oval(centre-inner, centre-inner, centre+inner, centre+inner,
+ fill=hue, outline='')
+
+
+class Stat(Card):
+ """A figure above its label. A row of tiles reads faster than a stack of
+ sentences, and the number is what the reader came for."""
+
+ def __init__(self, parent, label, value):
+ super().__init__(parent, padding=b.space(3), radius=b.RADIUS_MD)
+ self.value = ttk.Label(self.body, text=value, style='Stat.Card.TLabel', justify='left')
+ self.value.pack(anchor='w', fill='x')
+ self.caption = ttk.Label(self.body, text=label, style='Meta.Card.TLabel', justify='left')
+ self.caption.pack(anchor='w', fill='x', pady=(b.space(1), 0))
+ # A figure never clips: a narrow column wraps it instead.
+ self.body.bind('<Configure>', self.wrap, add='+')
+
+ def wrap(self, event):
+ for label in (self.value, self.caption):
+ label.configure(wraplength=max(60, event.width))
+
+ def show(self, label, value):
+ self.caption.configure(text=label)
+ self.value.configure(text=value)
+
+
+class StepRail(tk.Canvas):
+ """Wizard stages as pips on a hairline. A reached stage is a check, the
+ current one a filled dot; the rail replaces three full-width blocks."""
+
+ def __init__(self, parent, app, stages):
+ super().__init__(parent, background=b.BACKGROUND, highlightthickness=0,
+ height=b.space(6), width=1)
+ import tkinter.font as fonts
+ self.app, self.stages, self.step = app, stages, 0
+ self.typeface = fonts.Font(family=b.FAMILY, size=b.MICRO)
+ self.strong = fonts.Font(family=b.FAMILY, size=b.MICRO, weight='bold')
+ self.bind('<Configure>', lambda _: self.draw())
+
+ def show(self, step):
+ self.step = step
+ self.draw()
+
+ def draw(self):
+ self.delete('all')
+ width = self.winfo_width()
+ if width <= 1:
+ return
+ y, radius, gap, link = self.winfo_height()/2, b.space(2), b.space(2), b.space(9)
+ labels = [self.app.t(*stage) for stage in self.stages]
+ # Measure every stage with the bold face, so the centred group keeps
+ # one position instead of sliding as the current step advances.
+ spans = [self.strong.measure(text) for text in labels]
+ total = sum(2*radius + gap + span for span in spans) + (len(labels)-1)*(gap + link + gap)
+ x = max(1., (width - total)/2.)
+ for index, text in enumerate(labels):
+ reached, current = index < self.step, index == self.step
+ hue = b.SUCCESS if reached else b.ACCENT if current else b.BORDER
+ centre = x + radius
+ self.create_oval(centre-radius, y-radius, centre+radius, y+radius, outline=hue, width=1,
+ fill=b.SUCCESS_SUBTLE if reached else
+ b.ACCENT_SUBTLE if current else b.BACKGROUND)
+ if reached:
+ self.create_line(centre-3.5, y, centre-1, y+2.5, centre+3.5, y-3.5, fill=b.SUCCESS,
+ width=2, capstyle='round', joinstyle='round')
+ elif current:
+ self.create_oval(centre-2, y-2, centre+2, y+2, fill=b.ACCENT, outline='')
+ self.create_text(centre + radius + gap, y, anchor='w', text=text,
+ fill=b.SUCCESS if reached else b.ACCENT if current else b.MUTED,
+ font=self.strong if current else self.typeface)
+ x += 2*radius + gap + spans[index]
+ if index + 1 < len(labels):
+ self.create_line(x + gap, y, x + gap + link, y,
+ fill=b.ACCENT if reached else b.BORDER, width=1)
+ x += gap + link + gap
+
+
+class PillButton(tk.Canvas):
+ """The page's primary action. ttk cannot round a button corner, so this
+ draws one and keeps the ttk.Button surface the launcher already calls:
+ invoke, state, configure(text=, width=) and the pack geometry."""
+
+ def __init__(self, parent, command=None):
+ super().__init__(parent, background=b.BACKGROUND, highlightthickness=0, width=1, height=1)
+ import tkinter.font as fonts
+ self.command, self.label, self.chars = command, '', 0
+ self.disabled = self.hovered = self.pressed = self.focused = False
+ self.typeface = fonts.Font(family=b.FAMILY, size=b.BODY, weight='bold')
+ self.surface = self.create_polygon(0, 0, 1, 1, smooth=True, fill=b.ACCENT, outline=b.ACCENT)
+ self.caption = self.create_text(0, 0, text='', fill=b.BACKGROUND, font=self.typeface)
+ super().configure(cursor='hand2', takefocus=1)
+ # Repacking can drop this button under a stationary pointer, which
+ # sends Enter without a matching Leave. Every crossing and every move
+ # therefore re-reads where the pointer actually is.
+ self.bind('<Configure>', lambda _: self.hover())
+ self.bind('<Enter>', lambda _: self.hover())
+ self.bind('<Leave>', lambda _: self.hover())
+ self.bind('<Button-1>', lambda _: self.mark('pressed', True))
+ self.bind('<ButtonRelease-1>', self.released)
+ self.bind('<FocusIn>', lambda _: self.mark('focused', True))
+ self.bind('<FocusOut>', lambda _: self.mark('focused', False))
+ for key in ('<Return>', '<space>'):
+ self.bind(key, self.activated)
+ self.measure()
+
+ def configure(self, cnf=None, **kwargs):
+ resize = False
+ if 'text' in kwargs:
+ self.label, resize = kwargs.pop('text'), True
+ if 'width' in kwargs:
+ self.chars, resize = kwargs.pop('width'), True
+ result = super().configure(cnf, **kwargs) if (cnf or kwargs) else None
+ if resize:
+ self.measure()
+ return result
+ config = configure
+
+ def cget(self, key):
+ return self.label if key == 'text' else self.chars if key == 'width' else super().cget(key)
+
+ def measure(self):
+ text = self.typeface.measure(self.label)
+ requested = self.typeface.measure('0') * self.chars if self.chars else text
+ super().configure(width=max(text, requested) + 2*b.space(6),
+ height=self.typeface.metrics('linespace') + 2*b.space(3))
+ self.draw()
+
+ def mark(self, name, value):
+ setattr(self, name, value)
+ self.draw()
+
+ def hover(self, _=None):
+ x, y = self.winfo_pointerxy()
+ self.hovered = (self.winfo_rootx() <= x < self.winfo_rootx() + self.winfo_width()
+ and self.winfo_rooty() <= y < self.winfo_rooty() + self.winfo_height())
+ self.draw()
+
+ def released(self, _=None):
+ was_pressed, self.pressed = self.pressed, False
+ self.draw()
+ if was_pressed and self.hovered:
+ self.invoke()
+
+ def activated(self, _=None):
+ self.invoke()
+ return 'break'
+
+ def invoke(self):
+ if not self.disabled and self.command is not None:
+ return self.command()
+
+ def state(self, states=None):
+ for name in states or ():
+ if name in ('disabled', '!disabled'):
+ self.disabled = not name.startswith('!')
+ if states:
+ self.draw()
+ return ('disabled',) if self.disabled else ()
+
+ def instate(self, states, *_):
+ return all((name == 'disabled') == self.disabled for name in states)
+
+ def draw(self):
+ w = self.winfo_width() if self.winfo_width() > 1 else self.winfo_reqwidth()
+ h = self.winfo_height() if self.winfo_height() > 1 else self.winfo_reqheight()
+ fill = (b.ACCENT_DISABLED if self.disabled else
+ b.ACCENT if self.pressed else
+ b.ACCENT_HOVER if self.hovered else b.ACCENT)
+ self.coords(self.surface, *_rounded(1, 1, w-1, h-1, b.RADIUS_MD))
+ self.itemconfigure(self.surface, fill=fill,
+ outline=b.TEXT if self.focused and not self.disabled else fill)
+ self.coords(self.caption, w/2, h/2)
+ self.itemconfigure(self.caption, text=self.label,
+ fill=b.ACCENT_DISABLED_TEXT if self.disabled else b.BACKGROUND)
+
+
+class Progress(tk.Canvas):
+ """A rounded meter. Determinate work ramps accent to teal and finishes in
+ SUCCESS; unmeasured work sweeps, instead of showing an empty track."""
+
+ def __init__(self, parent, *, background=b.SURFACE, height=None, subdued=False):
+ self.values = dict(maximum=1, value=0, mode='determinate', tint=None)
+ self.offset, self.timer, self.subdued = 0., None, subdued
+ super().__init__(parent, background=background,
+ height=b.space(2) + 2 if height is None else height, width=1, highlightthickness=0)
+ self.bind('<Configure>', self.draw)
+ self.bind('<Destroy>', lambda _: self.stop())
+
+ def configure(self, cnf=None, **kwargs):
+ for name in ('maximum', 'value', 'mode', 'tint'):
+ if name in kwargs:
+ self.values[name] = kwargs.pop(name)
+ result = super().configure(cnf, **kwargs) if (kwargs or cnf) else None
+ if self.values['mode'] != 'indeterminate':
+ self.stop()
+ self.draw()
+ return result
+
+ def __getitem__(self, key):
+ return self.values[key] if key in self.values else super().__getitem__(key)
+
+ def start(self, interval=40):
+ self.values['mode'] = 'indeterminate'
+ if self.timer is None:
+ self.timer = self.after(interval, self.advance, interval)
+
+ def advance(self, interval):
+ self.timer = None
+ if not self.winfo_exists():
+ return
+ self.offset = (self.offset + .018) % 1
+ self.draw()
+ self.timer = self.after(interval, self.advance, interval)
+
+ def stop(self):
+ if self.timer is not None:
+ try:
+ self.after_cancel(self.timer)
+ except tk.TclError:
+ pass
+ self.timer = None
+ self.offset = 0.
+
+ def ramp(self, start, end, y, thickness, target, flat=False):
+ length = max(0., end - start)
+ if length <= 0:
+ return
+ destination = _rgb(target)
+ origin = destination if flat else _rgb(b.ACCENT)
+ parts = max(1, min(32, int(length/8)))
+ for index in range(parts):
+ mix = index / max(1, parts-1)
+ color = '#%02x%02x%02x' % tuple(round(a+(z-a)*mix) for a, z in zip(origin, destination))
+ self.create_line(start+length*index/parts, y, start+length*(index+1)/parts, y,
+ fill=color, width=thickness, capstyle='round')
+
+ def draw(self, _=None):
+ self.delete('all')
+ w, h = self.winfo_width(), self.winfo_height()
+ if w <= h:
+ return
+ radius = h/2
+ self.create_line(radius, radius, w-radius, radius, fill=b.RAISED, width=h, capstyle='round')
+ track = w - h
+ if self.values['mode'] == 'indeterminate':
+ span = track * .32
+ start = radius - span + (track + span) * self.offset
+ self.ramp(max(radius, start), min(w-radius, start+span), radius, h, self.values['tint'] or b.ACCENT_RAMP)
+ return
+ maximum = self.values['maximum']
+ ratio = max(0, min(1, self.values['value']/maximum)) if maximum else 0
+ if not ratio:
+ return
+ if self.subdued:
+ self.ramp(radius, radius + track*ratio, radius, h, b.ACCENT_DIM, flat=True)
+ return
+ # A finished or flagged meter is one solid colour; only work in
+ # progress ramps, so the two states never look like the same bar.
+ target = self.values['tint'] or (b.SUCCESS if ratio >= 1 else b.ACCENT_RAMP)
+ self.ramp(radius, radius + track*ratio, radius, h, target,
+ flat=ratio >= 1 or self.values['tint'] is not None)
diff --git a/freevideo_engine/win32.py b/freevideo_engine/win32.py
new file mode 100644
index 0000000..84f2a12
--- /dev/null
+++ b/freevideo_engine/win32.py
@@ -0,0 +1,425 @@
+"""Windows memory/process primitives. No CUDA, psutil, admin rights or shell."""
+import ctypes as C
+from functools import lru_cache
+import os
+
+DWORD, BOOL, HANDLE, SIZE_T = C.c_uint32, C.c_int32, C.c_void_p, C.c_size_t
+QWORD = C.c_uint64
+
+
+@lru_cache(None)
+def kernel32():
+ lib = C.WinDLL('kernel32', use_last_error=True)
+ declarations = {
+ 'CloseHandle': (BOOL, [HANDLE]),
+ 'GetCurrentProcess': (HANDLE, []),
+ 'OpenProcess': (HANDLE, [DWORD, BOOL, DWORD]),
+ 'GetExitCodeProcess': (BOOL, [HANDLE, C.POINTER(DWORD)]),
+ 'CreateToolhelp32Snapshot': (HANDLE, [DWORD, DWORD]),
+ 'Process32FirstW': (BOOL, [HANDLE, C.c_void_p]),
+ 'Process32NextW': (BOOL, [HANDLE, C.c_void_p]),
+ 'GetProcessIoCounters': (BOOL, [HANDLE, C.c_void_p]),
+ 'GetProcessTimes': (BOOL, [HANDLE, C.c_void_p, C.c_void_p, C.c_void_p, C.c_void_p]),
+ 'GlobalMemoryStatusEx': (BOOL, [C.c_void_p]),
+ 'CreateJobObjectW': (HANDLE, [C.c_void_p, C.c_wchar_p]),
+ 'SetInformationJobObject': (BOOL, [HANDLE, C.c_int, C.c_void_p, DWORD]),
+ 'AssignProcessToJobObject': (BOOL, [HANDLE, HANDLE]),
+ 'TerminateJobObject': (BOOL, [HANDLE, DWORD]),
+ 'CreateEventW': (HANDLE, [C.c_void_p, BOOL, BOOL, C.c_wchar_p]),
+ 'SetEvent': (BOOL, [HANDLE]),
+ 'WaitForSingleObject': (DWORD, [HANDLE, DWORD]),
+ 'DuplicateHandle': (BOOL, [HANDLE, HANDLE, HANDLE, C.POINTER(HANDLE), DWORD, BOOL, DWORD]),
+ 'SetHandleInformation': (BOOL, [HANDLE, DWORD, DWORD]),
+ 'GetConsoleMode': (BOOL, [HANDLE, C.POINTER(DWORD)]),
+ 'SetConsoleMode': (BOOL, [HANDLE, DWORD]),
+ 'CreateFileW': (HANDLE, [C.c_wchar_p, DWORD, DWORD, C.c_void_p, DWORD, DWORD, HANDLE]),
+ 'GetFileInformationByHandle': (BOOL, [HANDLE, C.c_void_p]),
+ 'GetFileInformationByHandleEx': (BOOL, [HANDLE, C.c_int, C.c_void_p, DWORD]),
+ }
+ for name, (result, arguments) in declarations.items():
+ function = getattr(lib, name)
+ function.restype, function.argtypes = result, arguments
+ return lib
+
+
+def checked(value):
+ if not value:
+ raise C.WinError(C.get_last_error())
+ return value
+
+
+def process_exited(pid):
+ """True/False/None for exited/running/uninspectable, without waiting.
+
+ A terminated Windows process can retain its PID/object while handles remain
+ open. Its process object is already signaled, even if metadata enumeration
+ still finds it or reports access denied. Do not infer exit from either of
+ those metadata results, or from an exit code that could equal STILL_ACTIVE.
+ """
+ if type(pid) is not int or not 0 < pid <= 0xffffffff:
+ return None
+ lib = kernel32()
+ handle = lib.OpenProcess(0x100000, False, pid) # SYNCHRONIZE only; no admin access.
+ if not handle:
+ return True if C.get_last_error() == 87 else None # ERROR_INVALID_PARAMETER: PID absent.
+ try:
+ result = lib.WaitForSingleObject(handle, 0)
+ if result == 0: # WAIT_OBJECT_0: all process threads have terminated.
+ return True
+ if result == 0x102: # WAIT_TIMEOUT: the process is not yet signaled.
+ return False
+ return None # WAIT_FAILED or any unexpected status is not death evidence.
+ except OSError:
+ return None
+ finally:
+ lib.CloseHandle(handle)
+
+
+def file_change_time_ns(path):
+ """NTFS ChangeTime, unlike Windows stat().st_ctime (creation time).
+
+ None means the filesystem/API cannot establish a reusable file identity.
+ The caller must hash the content or retain the source in that case.
+ """
+ class BasicInfo(C.Structure):
+ _fields_ = [(name, C.c_int64) for name in ('created', 'accessed', 'written', 'changed')] + [('attributes', DWORD)]
+ lib = kernel32()
+ handle = lib.CreateFileW(str(path), 0x80, 7, None, 3, 0x02000000, None)
+ if handle == C.c_void_p(-1).value:
+ return None
+ try:
+ info = BasicInfo()
+ if not lib.GetFileInformationByHandleEx(handle, 0, C.byref(info), C.sizeof(info)) or info.changed <= 116444736000000000:
+ return None
+ return (info.changed - 116444736000000000) * 100
+ finally:
+ lib.CloseHandle(handle)
+
+
+def open_regular(path):
+ """Open the link itself, reject reparse points, then transfer handle to CRT."""
+ import msvcrt
+ class FileInfo(C.Structure):
+ _fields_ = [('attributes', DWORD), ('created', DWORD * 2), ('accessed', DWORD * 2),
+ ('written', DWORD * 2), ('volume', DWORD), ('size_high', DWORD),
+ ('size_low', DWORD), ('links', DWORD), ('index_high', DWORD), ('index_low', DWORD)]
+ lib = kernel32()
+ handle = lib.CreateFileW(str(path), 0x80000000, 7, None, 3, 0x00200000 | 0x08000000, None)
+ if handle == C.c_void_p(-1).value:
+ raise C.WinError(C.get_last_error())
+ try:
+ info = FileInfo()
+ checked(lib.GetFileInformationByHandle(handle, C.byref(info)))
+ if info.attributes & (0x400 | 0x10):
+ raise ValueError('Not a regular file; links and directories are excluded from diagnostics')
+ descriptor = msvcrt.open_osfhandle(handle, os.O_RDONLY | os.O_BINARY)
+ handle = None
+ return descriptor
+ finally:
+ if handle:
+ lib.CloseHandle(handle)
+
+
+class MemoryStatus(C.Structure):
+ _fields_ = [('length', DWORD), ('load', DWORD)] + [(n, QWORD) for n in
+ ('total_physical', 'available_physical', 'commit_limit', 'commit_available',
+ 'total_virtual', 'available_virtual', 'available_extended')]
+
+
+def memory_status():
+ value = MemoryStatus()
+ value.length = C.sizeof(value)
+ checked(kernel32().GlobalMemoryStatusEx(C.byref(value)))
+ return {'total_bytes': value.total_physical,
+ 'available_bytes': min(value.available_physical, value.commit_available),
+ 'physical_available_bytes': value.available_physical,
+ 'commit_limit_bytes': value.commit_limit, 'commit_available_bytes': value.commit_available,
+ 'swap_total_bytes': None, 'swap_free_bytes': None,
+ 'scope': 'GlobalMemoryStatusEx: available = min(physical RAM, commit headroom); pagefile is not extra physical RAM.'}
+
+
+class ProcessEntry(C.Structure):
+ _fields_ = [('dwSize', DWORD), ('cntUsage', DWORD), ('pid', DWORD), ('heap', SIZE_T),
+ ('module', DWORD), ('threads', DWORD), ('parent', DWORD),
+ ('priority', C.c_int32), ('flags', DWORD), ('exe', C.c_wchar * 260)]
+
+
+class ProcessCounters(C.Structure):
+ _fields_ = [('cb', DWORD), ('faults', DWORD)] + [(n, SIZE_T) for n in
+ ('peak_working_set', 'working_set', 'peak_paged_pool', 'paged_pool',
+ 'peak_nonpaged_pool', 'nonpaged_pool', 'pagefile', 'peak_pagefile', 'private_usage')]
+
+
+class VmCountersEx2(C.Structure):
+ """ProcessVmCounters (Win8+). Private working set excludes mapped files.
+
+ GetProcessMemoryInfo offers no equivalent: its working set counts resident
+ mapped file pages and its PrivateUsage counts nonresident commit.
+ """
+ _fields_ = ([(n, SIZE_T) for n in ('peak_virtual', 'virtual_size')] + [('faults', DWORD)] +
+ [(n, SIZE_T) for n in ('peak_working_set', 'working_set', 'peak_paged_pool',
+ 'paged_pool', 'peak_nonpaged_pool', 'nonpaged_pool', 'pagefile',
+ 'peak_pagefile', 'private_usage', 'private_working_set', 'shared_commit')])
+
+
+class IoCounters(C.Structure):
+ _fields_ = [(n, QWORD) for n in ('reads', 'writes', 'other', 'read_bytes', 'write_bytes', 'other_bytes')]
+
+
+@lru_cache(None)
+def psapi():
+ lib = C.WinDLL('psapi', use_last_error=True)
+ lib.GetProcessMemoryInfo.argtypes = [HANDLE, C.POINTER(ProcessCounters), DWORD]
+ lib.GetProcessMemoryInfo.restype = BOOL
+ return lib
+
+
+@lru_cache(None)
+def ntdll():
+ lib = C.WinDLL('ntdll', use_last_error=True)
+ lib.NtQueryInformationProcess.argtypes = [HANDLE, C.c_int, C.c_void_p, DWORD, C.POINTER(DWORD)]
+ lib.NtQueryInformationProcess.restype = C.c_long
+ return lib
+
+
+def private_working_set(handle):
+ """Resident private bytes, or None when this kernel cannot report them.
+
+ PROCESS_QUERY_LIMITED_INFORMATION is sufficient; no admin rights.
+ """
+ counters, written = VmCountersEx2(), DWORD()
+ status = ntdll().NtQueryInformationProcess(handle, 3, C.byref(counters),
+ C.sizeof(counters), C.byref(written))
+ if status < 0 or written.value < C.sizeof(counters):
+ return None
+ return counters.private_working_set
+
+
+def processes():
+ lib = kernel32()
+ snapshot = lib.CreateToolhelp32Snapshot(2, 0)
+ if snapshot == C.c_void_p(-1).value:
+ raise C.WinError(C.get_last_error())
+ try:
+ entry = ProcessEntry()
+ entry.dwSize = C.sizeof(entry)
+ more = lib.Process32FirstW(snapshot, C.byref(entry))
+ if not more:
+ checked(more)
+ result = {}
+ while more:
+ result[entry.pid] = entry.parent
+ more = lib.Process32NextW(snapshot, C.byref(entry))
+ return result
+ finally:
+ lib.CloseHandle(snapshot)
+
+
+def process_memory(pid, *, parent_start_tick=None):
+ """Read counters; a tree query also returns identity for old/exited children."""
+ lib = kernel32()
+ # Current Windows supports these queries without PROCESS_VM_READ. Keep a
+ # synchronized handle so exit during a query is checked on this same process,
+ # not on a potentially recycled PID.
+ handle = lib.OpenProcess(0x1000 | 0x100000, False, pid)
+ if not handle:
+ if C.get_last_error() == 87: # PID exited between enumeration and OpenProcess.
+ return None
+ raise C.WinError(C.get_last_error())
+ started = None
+ def exited_row():
+ return {'start_tick': started, 'exited': True} if parent_start_tick is not None and started is not None else None
+ try:
+ times = [QWORD() for _ in range(4)]
+ checked(lib.GetProcessTimes(handle, *(C.byref(t) for t in times)))
+ started = times[0].value
+ # Toolhelp retains a child's original parent PID after the parent exits.
+ # A new worker can reuse it. Reject that entire old branch before asking
+ # for memory counters (which may be inaccessible for system processes).
+ if parent_start_tick is not None and started < parent_start_tick:
+ return {'start_tick': started, 'excluded': 'predates-parent'}
+ if lib.WaitForSingleObject(handle, 0) == 0:
+ return exited_row()
+ memory, io = ProcessCounters(), IoCounters()
+ memory.cb = C.sizeof(memory)
+ checked(psapi().GetProcessMemoryInfo(handle, C.byref(memory), memory.cb))
+ io_ok = lib.GetProcessIoCounters(handle, C.byref(io))
+ resident = private_working_set(handle)
+ if resident is None:
+ raise OSError('ProcessVmCounters did not report a private working set')
+ return {'rss_bytes': memory.working_set, 'private_commit_bytes': memory.private_usage,
+ 'private_working_set_bytes': resident,
+ 'read_bytes': io.read_bytes if io_ok else 0, 'write_bytes': io.write_bytes if io_ok else 0,
+ 'io_complete': bool(io_ok), 'start_tick': started}
+ except OSError:
+ if lib.WaitForSingleObject(handle, 0) == 0:
+ return exited_row()
+ raise
+ finally:
+ lib.CloseHandle(handle)
+
+
+class ProcessMemory:
+ """Working set, private commit and private working set, never called PSS.
+
+ The RAM guard is the private working set: the other two each include memory
+ the process does not hold in physical RAM. All three remain in the report.
+ """
+ def __init__(self):
+ self.peak_rss = self.peak_private = self.peak_guard = self.samples = 0
+ self.peak_resident = 0
+ self.minimum_available = self.minimum_physical = self.minimum_commit = None
+ self.minimum_effective = None
+ self.complete = self.io_complete = True
+ self.io = {}
+ self.errors = []
+ self.excluded = []
+
+ def sample(self, root_pid):
+ parents = processes()
+ children = {}
+ for pid, parent in parents.items():
+ children.setdefault(parent, []).append(pid)
+ pending, visited = [(root_pid, None)], set()
+ rss = private = resident = count = 0
+ incomplete, errors, excluded = [], [], []
+ # Parents first: each edge must agree with the processes' creation times.
+ for pid, parent_started in pending:
+ if pid in visited:
+ continue
+ visited.add(pid)
+ started = parent_started
+ try:
+ row = process_memory(pid, parent_start_tick=parent_started)
+ if row is None:
+ # An exiting intermediate may still have live children in
+ # this snapshot. Check those against the known ancestor.
+ if parent_started is not None:
+ pending.extend((child, parent_started) for child in children.get(pid, []))
+ continue
+ started = row['start_tick']
+ if row.get('excluded'):
+ excluded.append(dict(pid=pid, parent_pid=parents.get(pid),
+ start_tick=started, parent_start_tick=parent_started, reason=row['excluded']))
+ continue
+ if row.get('exited'):
+ pending.extend((child, started) for child in children.get(pid, []))
+ continue
+ count += 1
+ rss += row['rss_bytes']
+ private += row['private_commit_bytes']
+ resident += row['private_working_set_bytes']
+ self.io[(pid, row['start_tick'])] = (row['read_bytes'], row['write_bytes'])
+ self.io_complete &= row['io_complete']
+ except OSError as error:
+ exited = process_exited(pid)
+ if exited is not True:
+ incomplete.append(pid)
+ errors.append(dict(pid=pid, parent_pid=parents.get(pid),
+ error=str(error), winerror=getattr(error, 'winerror', None), exited=exited))
+ pending.extend((child, started) for child in children.get(pid, []))
+ self.errors.extend(errors[:max(0, 16-len(self.errors))])
+ self.excluded.extend(excluded[:max(0, 16-len(self.excluded))])
+ memory = memory_status()
+ self.samples += 1
+ self.complete &= not incomplete
+ self.peak_rss, self.peak_private = max(self.peak_rss, rss), max(self.peak_private, private)
+ self.peak_resident = max(self.peak_resident, resident)
+ # Neither GetProcessMemoryInfo counter measures held physical RAM.
+ # Measured on an RTX 4080 Laptop: 7 GiB of VRAM moved private commit by
+ # 7.01 GiB but the working set by 48 KiB, because WDDM charges GPU
+ # allocations to PrivateUsage without residency; faulting in a 14.61 GiB
+ # mapped model file moved the working set by the touched amount while
+ # private commit stayed flat. A budget compared against either one
+ # rejects requests that fit, and the working set grows with free RAM
+ # because clean mapped pages are only reclaimed under pressure.
+ # Private working set excludes mapped files and nonresident commit.
+ # Commit exhaustion stays bounded by the system floor, where available
+ # is min(physical RAM, commit headroom).
+ guard = resident
+ if not incomplete:
+ self.peak_guard = max(self.peak_guard, guard)
+ # GlobalMemoryStatusEx counts only free and standby pages, so resident
+ # clean mapped pages are missing from availability. Linux MemAvailable
+ # includes reclaimable page cache, which is why offloaded weights cost
+ # nothing there. Offloaded weights are mapped read-only from the
+ # prepared cache and can be dropped under pressure: measured a 21.85 GiB
+ # working set holding 4.30 GiB of private bytes while availability read
+ # 1.62 GiB, which stopped a request using 4.30 GiB against an 8.50 GiB
+ # budget. Credit only this tree's own resident non-private pages, never
+ # system-wide cache, and stay bounded by commit headroom and total RAM.
+ reclaimable = max(0, rss - resident) if not incomplete else 0
+ effective = min(memory['total_bytes'], memory['commit_available_bytes'],
+ memory['physical_available_bytes'] + reclaimable)
+ for attribute, field in (('minimum_available', 'available_bytes'),
+ ('minimum_physical', 'physical_available_bytes'),
+ ('minimum_commit', 'commit_available_bytes')):
+ old = getattr(self, attribute)
+ setattr(self, attribute, memory[field] if old is None else min(old, memory[field]))
+ self.minimum_effective = effective if self.minimum_effective is None else min(self.minimum_effective, effective)
+ return {'rss_bytes': rss, 'pss_bytes': None, 'private_commit_bytes': private,
+ 'private_working_set_bytes': resident,
+ 'guard_bytes': guard if not incomplete else None,
+ 'guard_metric': 'tree private working set',
+ 'processes': count, 'unreadable_memory_pids': incomplete,
+ 'memory_read_errors': errors, 'excluded_processes': excluded,
+ 'reclaimable_mapped_bytes': reclaimable,
+ 'effective_available_bytes': effective,
+ 'system_available_bytes': memory['available_bytes'],
+ 'system_physical_available_bytes': memory['physical_available_bytes'],
+ 'system_commit_available_bytes': memory['commit_available_bytes']}
+
+ def result(self):
+ return {'process_tree_peak_rss_bytes': self.peak_rss, 'process_tree_peak_pss_bytes': None,
+ 'process_tree_pss_complete': False, 'process_tree_peak_private_commit_bytes': self.peak_private,
+ 'process_tree_peak_private_working_set_bytes': self.peak_resident,
+ 'process_tree_peak_guard_bytes': self.peak_guard, 'process_tree_guard_complete': self.complete,
+ 'process_tree_memory_errors': list(self.errors),
+ 'process_tree_excluded_processes': list(self.excluded),
+ 'ram_guard_metric': 'tree private working set',
+ 'system_min_available_bytes': self.minimum_available,
+ 'system_min_physical_available_bytes': self.minimum_physical,
+ 'system_min_commit_available_bytes': self.minimum_commit,
+ 'effective_min_available_bytes': self.minimum_effective,
+ 'ram_observation_samples': self.samples,
+ 'process_tree_disk_read_bytes': sum(v[0] for v in self.io.values()),
+ 'process_tree_disk_write_bytes': sum(v[1] for v in self.io.values()),
+ 'process_tree_io_complete': self.io_complete, 'process_tree_io_version': 3,
+ 'process_tree_io_scope': 'Windows per-process transfer counters, retained by PID/creation time; includes cached I/O, not physical disk traffic.',
+ 'ram_observation_scope': 'RAM guard is the summed private working set (ProcessVmCounters): resident private bytes, excluding mapped files and nonresident commit. PSS unavailable; this is not PSS and does not apportion shared pages. Working set and private commit are retained for diagnosis only and must not be compared against a RAM budget: the working set counts resident mapped file pages, which grow with free RAM, and WDDM charges GPU allocations to PrivateUsage without residency while per-PID VRAM attribution is unavailable. Effective availability adds back this tree own resident non-private pages, which are clean mapped weight pages the kernel can drop, because GlobalMemoryStatusEx counts only free and standby pages unlike Linux MemAvailable; it stays bounded by commit headroom and total RAM and never credits system-wide cache. Pagefile not added to physical RAM.'}
+
+
+class JobBasicLimits(C.Structure):
+ _fields_ = [('process_time', C.c_int64), ('job_time', C.c_int64), ('flags', DWORD),
+ ('min_working_set', SIZE_T), ('max_working_set', SIZE_T), ('active_processes', DWORD),
+ ('affinity', SIZE_T), ('priority', DWORD), ('scheduling', DWORD)]
+
+
+class JobLimits(C.Structure):
+ _fields_ = [('basic', JobBasicLimits), ('io', IoCounters), ('process_memory', SIZE_T),
+ ('job_memory', SIZE_T), ('peak_process_memory', SIZE_T), ('peak_job_memory', SIZE_T)]
+
+
+class Job:
+ """Uninherited owner handle: controller death kills its entire worker tree."""
+ def __init__(self):
+ self.handle = checked(kernel32().CreateJobObjectW(None, None))
+ limits = JobLimits()
+ limits.basic.flags = 0x2000 # JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
+ try:
+ checked(kernel32().SetInformationJobObject(self.handle, 9, C.byref(limits), C.sizeof(limits)))
+ except BaseException:
+ self.close()
+ raise
+
+ def assign(self, process_handle):
+ checked(kernel32().AssignProcessToJobObject(self.handle, int(process_handle)))
+
+ def kill(self):
+ if self.handle:
+ checked(kernel32().TerminateJobObject(self.handle, 1))
+
+ def close(self):
+ if self.handle:
+ kernel32().CloseHandle(self.handle)
+ self.handle = None
diff --git a/freevideo_engine/windows_bootstrap.ps1 b/freevideo_engine/windows_bootstrap.ps1
new file mode 100644
index 0000000..6cd0035
--- /dev/null
+++ b/freevideo_engine/windows_bootstrap.ps1
@@ -0,0 +1,55 @@
+# Shared by the launcher and CPU checks. Compatible with PowerShell 5.1 and 7.
+function Invoke-FreeVideoBootstrapCommand {
+ param(
+ [Parameter(Mandatory=$true)][string]$Executable,
+ [Parameter(Mandatory=$true)][string[]]$Arguments,
+ [Parameter(Mandatory=$true)][string]$LogPath,
+ [Parameter(Mandatory=$true)][string]$Label,
+ [switch]$Direct
+ )
+ # Windows PowerShell 5.1 turns redirected native stderr into ErrorRecords.
+ # uv writes normal progress there. Let the child finish and inspect its
+ # exit code; keep these preferences local to this function.
+ $ErrorActionPreference = 'Continue'
+ $PSNativeCommandUseErrorActionPreference = $false
+ $encoding = [Text.UTF8Encoding]::new($false)
+ if (Test-Path -LiteralPath $LogPath) {
+ # Preserve older PowerShell 5.1 UTF-16 logs when appending a retry.
+ $reader = [IO.StreamReader]::new($LogPath, $encoding, $true)
+ try {
+ [void]$reader.Peek()
+ $encoding = $reader.CurrentEncoding
+ } finally { $reader.Dispose() }
+ }
+ $log = [IO.StreamWriter]::new($LogPath, $true, $encoding)
+ $log.AutoFlush = $true
+ $previousExitCode = $global:LASTEXITCODE
+ $savedProxy = @{}
+ if ($Direct) {
+ foreach ($name in @('HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY', 'NO_PROXY')) {
+ $savedProxy[$name] = [Environment]::GetEnvironmentVariable($name, 'Process')
+ [Environment]::SetEnvironmentVariable($name, $null, 'Process')
+ }
+ [Environment]::SetEnvironmentVariable('NO_PROXY', '*', 'Process')
+ }
+ try {
+ # Native commands update the global automatic variable. A local
+ # LASTEXITCODE would shadow their result and keep a stale/null value.
+ $global:LASTEXITCODE = $null
+ $log.WriteLine(('[{0}] {1}' -f [DateTime]::UtcNow.ToString('o'), $Label))
+ & $Executable @Arguments 2>&1 | ForEach-Object { $log.WriteLine($_.ToString()) }
+ $nativeExit = $global:LASTEXITCODE
+ if ($null -eq $nativeExit) { throw "Native command did not return an exit code: $Executable" }
+ $log.WriteLine("Exit code: $nativeExit")
+ return [int]$nativeExit
+ } catch {
+ $log.WriteLine('Invocation failed: ' + $_.Exception.Message)
+ throw
+ } finally {
+ foreach ($name in $savedProxy.Keys) {
+ [Environment]::SetEnvironmentVariable($name, $savedProxy[$name], 'Process')
+ }
+ $global:LASTEXITCODE = $previousExitCode
+ $log.Dispose()
+ }
+}
diff --git a/freevideo_engine/windows_gpu_memory.py b/freevideo_engine/windows_gpu_memory.py
new file mode 100644
index 0000000..fc76ab9
--- /dev/null
+++ b/freevideo_engine/windows_gpu_memory.py
@@ -0,0 +1,91 @@
+"""Read this CUDA process's WDDM budget on its matching DXGI adapter.
+
+No CUDA context is created. Construct only inside an already initialized CUDA
+worker. Local/non-local segment usage is driver accounting, not proof of PCIe
+traffic, paging, or CUDA tensor residency. No reservations or settings are changed.
+"""
+import ctypes as c
+import os
+import ntpath
+import uuid
+
+
+class Guid(c.Structure):
+ _fields_ = [('data1', c.c_uint32), ('data2', c.c_uint16), ('data3', c.c_uint16), ('data4', c.c_ubyte * 8)]
+
+ @classmethod
+ def parse(cls, value):
+ return cls.from_buffer_copy(uuid.UUID(value).bytes_le)
+
+
+class Luid(c.Structure):
+ _fields_ = [('low', c.c_uint32), ('high', c.c_int32)]
+
+
+class MemoryInfo(c.Structure):
+ _fields_ = [(name, c.c_uint64) for name in ('budget', 'usage', 'available_reservation', 'reservation')]
+
+
+def method(pointer, slot, result, *arguments):
+ table = c.cast(pointer, c.POINTER(c.POINTER(c.c_void_p))).contents
+ return c.WINFUNCTYPE(result, c.c_void_p, *arguments)(table[slot])
+
+
+def check(code, operation):
+ if code:
+ raise OSError('%s returned 0x%08x' % (operation, code & 0xffffffff))
+
+
+class AdapterMemory:
+ def __init__(self):
+ if os.name != 'nt':
+ raise OSError('WDDM memory accounting requires Windows')
+ self.adapter = c.c_void_p()
+ system = ntpath.join(os.environ.get('SystemRoot', r'C:\Windows'), 'System32')
+ self.cuda, self.dxgi = c.WinDLL(ntpath.join(system, 'nvcuda.dll')), c.WinDLL(ntpath.join(system, 'dxgi.dll'))
+ self.cuda.cuCtxGetDevice.argtypes = [c.POINTER(c.c_int)]
+ self.cuda.cuCtxGetDevice.restype = c.c_int
+ self.cuda.cuDeviceGetLuid.argtypes = [c.c_void_p, c.POINTER(c.c_uint32), c.c_int]
+ self.cuda.cuDeviceGetLuid.restype = c.c_int
+ device, luid, mask = c.c_int(), Luid(), c.c_uint32()
+ check(self.cuda.cuCtxGetDevice(c.byref(device)), 'cuCtxGetDevice')
+ check(self.cuda.cuDeviceGetLuid(c.byref(luid), c.byref(mask), device), 'cuDeviceGetLuid')
+ if not mask.value or mask.value & (mask.value - 1):
+ raise OSError('CUDA did not identify one WDDM adapter node')
+ self.node = mask.value.bit_length() - 1
+ self.identity = dict(cuda_device=device.value, node=self.node,
+ adapter_luid='%08x:%08x' % (luid.high & 0xffffffff, luid.low))
+ factory = c.c_void_p()
+ factory_iid = Guid.parse('1bc6ea02-ef36-464f-bf0c-21ca39e5168a')
+ adapter_iid = Guid.parse('645967a4-1392-4310-a798-8053ce3e93fd')
+ self.dxgi.CreateDXGIFactory1.argtypes = [c.POINTER(Guid), c.POINTER(c.c_void_p)]
+ self.dxgi.CreateDXGIFactory1.restype = c.c_int32
+ check(self.dxgi.CreateDXGIFactory1(c.byref(factory_iid), c.byref(factory)), 'CreateDXGIFactory1')
+ try:
+ # Slots and ABI follow the Windows SDK IDXGIFactory4/IDXGIAdapter3.
+ enum = method(factory, 26, c.c_int32, Luid, c.POINTER(Guid), c.POINTER(c.c_void_p))
+ check(enum(factory, luid, c.byref(adapter_iid), c.byref(self.adapter)), 'EnumAdapterByLuid')
+ except BaseException:
+ self.close()
+ raise
+ finally:
+ if factory:
+ method(factory, 2, c.c_uint32)(factory)
+ if not self.adapter:
+ raise OSError('DXGI returned no matching adapter')
+
+ def sample(self):
+ if not self.adapter:
+ raise OSError('DXGI adapter is closed')
+ query = method(self.adapter, 14, c.c_int32, c.c_uint32, c.c_int32, c.POINTER(MemoryInfo))
+ result = {}
+ for group, name in ((0, 'local'), (1, 'nonlocal')):
+ value = MemoryInfo()
+ check(query(self.adapter, self.node, group, c.byref(value)), 'QueryVideoMemoryInfo/' + name)
+ result[name] = dict(budget_bytes=value.budget, usage_bytes=value.usage)
+ return result
+
+ def close(self):
+ if self.adapter:
+ pointer, self.adapter = self.adapter, c.c_void_p()
+ method(pointer, 2, c.c_uint32)(pointer)
diff --git a/freevideo_engine/windows_process_host.ps1 b/freevideo_engine/windows_process_host.ps1
new file mode 100644
index 0000000..8b270dc
--- /dev/null
+++ b/freevideo_engine/windows_process_host.ps1
@@ -0,0 +1,48 @@
+# The one-file GUI must not relaunch its unpacking bootloader as a job gate.
+# PowerShell waits before spawning any child; all descendants then inherit the Job.
+$ErrorActionPreference = 'Stop'
+$event = [Threading.EventWaitHandle]::new($false, [Threading.EventResetMode]::ManualReset)
+try {
+ $event.SafeWaitHandle = [Microsoft.Win32.SafeHandles.SafeWaitHandle]::new([IntPtr]([long]$args[0]), $true)
+ if (-not $event.WaitOne(30000)) { throw 'Worker was not assigned to its Windows Job Object in time.' }
+} finally {
+ $event.Dispose()
+}
+# Windows PowerShell 5.1's native argument binder removes embedded quotes and
+# empty arguments. The Python controller already produced a Win32 command line;
+# hand it directly to ProcessStartInfo, without shell invocation or evaluation.
+$command = $env:FREEVIDEO_WINDOWS_CHILD_COMMAND | ConvertFrom-Json
+Remove-Item Env:FREEVIDEO_WINDOWS_CHILD_COMMAND
+if (-not $command.executable) { throw 'Missing worker executable.' }
+$info = [Diagnostics.ProcessStartInfo]::new()
+$info.FileName = $command.executable
+$info.Arguments = $command.arguments
+$info.UseShellExecute = $false
+# Inherit the gate's hidden console so native helpers cannot create a new one.
+$info.CreateNoWindow = $false
+$info.WindowStyle = [Diagnostics.ProcessWindowStyle]::Hidden
+# stdout/stderr redirection makes .NET set STARTF_USESTDHANDLES. With stdin
+# redirection disabled it passes GetStdHandle(STD_INPUT_HANDLE) to the child:
+# the controller's pipe (or NUL) itself, without another buffered pipe/pump.
+# This delivers short writes and EOF directly and creates no StreamWriter/BOM.
+$info.RedirectStandardInput = $false
+$info.RedirectStandardOutput = $true
+$info.RedirectStandardError = $true
+$process = [Diagnostics.Process]::new()
+$process.StartInfo = $info
+try {
+ if (-not $process.Start()) { throw 'Worker did not start.' }
+ # Copy raw bytes continuously; do not buffer the whole log, translate its
+ # encoding, or wait for a full line before forwarding progress to the GUI.
+ $output = $process.StandardOutput.BaseStream.CopyToAsync([Console]::OpenStandardOutput())
+ $errorOutput = $process.StandardError.BaseStream.CopyToAsync([Console]::OpenStandardError())
+ $process.WaitForExit()
+ # Windows PowerShell exposes the internal VoidTaskResult of CopyToAsync.
+ # Consume completion without printing that value into the worker's stdout.
+ [void]$output.GetAwaiter().GetResult()
+ [void]$errorOutput.GetAwaiter().GetResult()
+ $code = $process.ExitCode
+} finally {
+ $process.Dispose()
+}
+exit $code
diff --git a/freevideo_engine/windows_ux.py b/freevideo_engine/windows_ux.py
new file mode 100644
index 0000000..aeadd54
--- /dev/null
+++ b/freevideo_engine/windows_ux.py
@@ -0,0 +1,96 @@
+"""Small Windows desktop helpers; no admin rights or persistent settings."""
+from contextlib import contextmanager
+import sys
+from .system import windows
+
+
+def taskbar_identity():
+ """Identify the launcher before creating windows, including source launches."""
+ if windows():
+ import ctypes
+ function = ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID
+ function.argtypes = [ctypes.c_wchar_p]
+ function.restype = ctypes.c_long
+ function('FlashML.FreeVideo.Launcher')
+
+
+def open_browser(url):
+ """Use Windows' URL association directly and retain its actionable errors."""
+ with external_python():
+ if windows():
+ import os
+ os.startfile(url)
+ return True
+ import webbrowser
+ return webbrowser.open(url, new=2)
+
+
+def hidden_console():
+ """Hide a console and keep it inheritable by native helper processes."""
+ if not windows():
+ return {}
+ import subprocess
+ info = subprocess.STARTUPINFO()
+ info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
+ info.wShowWindow = 0
+ return dict(startupinfo=info)
+
+
+@contextmanager
+def external_python():
+ """Keep PyInstaller's private DLL search directory out of external Python."""
+ previous = None
+ if windows() and getattr(sys, 'frozen', False):
+ import ctypes
+ buffer = ctypes.create_unicode_buffer(32768)
+ ctypes.windll.kernel32.GetDllDirectoryW(len(buffer), buffer)
+ previous = buffer.value
+ ctypes.windll.kernel32.SetDllDirectoryW(None)
+ try:
+ yield
+ finally:
+ if previous is not None:
+ ctypes.windll.kernel32.SetDllDirectoryW(previous)
+
+
+@contextmanager
+def awake():
+ """Prevent idle sleep on this worker thread, restore its state on exit.
+
+ Display power saving and an explicit user sleep/lid action still apply.
+ """
+ previous, function = 0, None
+ if windows():
+ try:
+ import ctypes
+ function = ctypes.WinDLL('kernel32', use_last_error=True).SetThreadExecutionState
+ function.argtypes, function.restype = [ctypes.c_uint32], ctypes.c_uint32
+ previous = function(0x80000001)
+ except OSError:
+ pass
+ try:
+ yield bool(previous)
+ finally:
+ if previous:
+ function(0x80000000 | previous)
+
+
+def work_area(window):
+ left, top = 0, 0
+ width, height = window.winfo_screenwidth(), window.winfo_screenheight()
+ if windows():
+ import ctypes
+ from ctypes import wintypes
+ rectangle = wintypes.RECT()
+ function = ctypes.WinDLL('user32', use_last_error=True).SystemParametersInfoW
+ function.argtypes = [wintypes.UINT, wintypes.UINT, ctypes.c_void_p, wintypes.UINT]
+ function.restype = wintypes.BOOL
+ if function(0x30, 0, ctypes.byref(rectangle), 0):
+ left, top = rectangle.left, rectangle.top
+ width, height = rectangle.right-rectangle.left, rectangle.bottom-rectangle.top
+ return left, top, width, height
+
+
+def window_size(window):
+ _, _, width, height = work_area(window)
+ return min(width-40, max(680, int(width*.82))), min(height-60, max(600, int(height*.86)))
diff --git a/freevideo_engine/worker.py b/freevideo_engine/worker.py
new file mode 100644
index 0000000..389d38f
--- /dev/null
+++ b/freevideo_engine/worker.py
@@ -0,0 +1,397 @@
+"""Isolated VDN sampling and decode worker for one Engine request."""
+import argparse
+import json
+import os
+from pathlib import Path
+import time
+
+import torch
+
+from .policy import memory_fraction
+from .runtime import Engine
+from .decode import decode_to_file
+from .locking import runtime_lock
+from .monitoring import save
+from .processes import worker_signals
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--request', required=True)
+ args = parser.parse_args()
+ request = json.loads(Path(args.request).read_text(encoding='utf-8'))
+ with worker_signals(), runtime_lock():
+ generate(request)
+
+
+def device_memory_report(budget_bytes, allocator_limit_bytes=None, *, reserve_bytes=0, capacity_trial=False):
+ from .gpu_budget import configure
+ return configure(torch, budget_bytes, allocator_limit_bytes, reserve_bytes=reserve_bytes,
+ capacity_trial=capacity_trial)
+
+
+def generate(request, resident=None):
+ torch.set_num_threads(8)
+ started = time.perf_counter()
+ engine = None
+ live_budget = None
+ decode_read_ahead = None
+ decoder_read_ahead_closed = False
+ original_error = None
+ metrics = {'success': False, 'phase': 'load', 'geometry': request.get('geometry'),
+ 'gpu_budget_bytes': request['gpu_budget_bytes'],
+ 'compiler_threads': os.environ.get('TORCHINDUCTOR_COMPILE_THREADS'),
+ 'execution_context': {'host_threads': {
+ 'OMP_NUM_THREADS': os.environ.get('OMP_NUM_THREADS'),
+ 'MKL_NUM_THREADS': os.environ.get('MKL_NUM_THREADS'),
+ 'torch_num_threads': torch.get_num_threads()}}}
+ save(request['metrics'], metrics)
+ try:
+ if 'sampling_plan' in request:
+ from .two_pass import plan
+ expected = plan(request['geometry'], request['sampling_plan']['requested'],
+ request['engine_options'].get('task', 't2va'))
+ if request['sampling_plan'] != expected:
+ raise ValueError('Sampling plan does not match the requested canvas and task')
+ metrics['sampling_plan'] = expected
+ metrics['device_memory'] = device_memory_report(request['gpu_budget_bytes'], request.get('allocator_limit_bytes'),
+ reserve_bytes=request.get('gpu_reserve_bytes', 0), capacity_trial=request.get('capacity_trial', False))
+ if request.get('dynamic_gpu_budget') and metrics['device_memory'].get('windows_allocator_limit_enforced'):
+ from .gpu_budget import LiveGPUBudget
+ reserve = request.get('gpu_reserve_bytes', 0)
+ live_budget = LiveGPUBudget(torch, metrics['device_memory'],
+ request.get('gpu_capacity_bytes', metrics['device_memory']['device_total_bytes']) - reserve, reserve)
+
+ def refresh_budget(stage, reclaim=None):
+ def release(target):
+ if reclaim is not None:
+ reclaim(target)
+ if resident is not None:
+ for role in tuple(resident.entries):
+ if torch.cuda.memory_allocated() <= target:
+ break
+ if role != 'engine' or stage == 'decode':
+ resident.drop(role, 'Live GPU budget decreased')
+ limit = live_budget.refresh(stage, release)
+ if resident is not None:
+ resident.gpu_budget = limit
+ return limit
+ if resident is not None:
+ limit = metrics['device_memory'].get('effective_allocator_limit_bytes')
+ if limit is not None:
+ resident.gpu_budget = min(resident.gpu_budget, limit)
+ save(request['metrics'], metrics)
+ manifest = json.loads((Path(request['cache']) / 'manifest.json').read_text(encoding='utf-8'))
+ if manifest.get('precision') != 'fp8':
+ raise ValueError('The Engine requires an official FP8 cache')
+ options, decoder = request['engine_options'], request['decoder_options']
+ resume = request.get('resume_decode') is not None
+ if not resume and options.get('task', 't2va') != 't2va':
+ from .media_conditioning import describe
+ canvas = request.get('geometry', {})
+ value = torch.load(request['conditioning'], map_location='cpu', weights_only=True)
+ # Older official keyframe caches have no explicit task field.
+ value.setdefault('task', options['task'])
+ actual = describe(value, canvas['width'], canvas['height'], canvas['frames'])
+ if actual['task'] != options['task'] or any(canvas.get(name, actual[name]) != actual[name]
+ for name in ('reference_video_tokens', 'reference_audio_tokens')):
+ raise ValueError('Encoded media disagrees with the admitted request layout')
+ del value
+ metrics['conditioning_info'] = actual
+ canvas = request.get('geometry', {})
+ artifacts = Path(request['artifacts']) if request.get('artifacts') else None
+ if resume:
+ from .decode_resume import load_saved_sampling
+ metrics.update(phase='decode', decode_phase='latent_load')
+ save(request['metrics'], metrics)
+ tick = time.perf_counter()
+ latents, audio, sampled, base = load_saved_sampling(request, torch)
+ metrics.update(sampled, phase='decode', decode_phase='latent_transfer')
+ save(request['metrics'], metrics)
+ print(json.dumps(dict(event='decode_resume', sampling_source_attempt=metrics['sampling_source_attempt'])), flush=True)
+ if resident is not None:
+ resident.decoder_room(decoder, canvas)
+ latents, audio = latents.to('cuda'), audio.to('cuda')
+ metrics['latent_reload_seconds'] = time.perf_counter() - tick
+ metrics['transformer_released_before_decode'] = resident is None or 'engine' not in resident.entries
+ else:
+ tick = time.perf_counter()
+ factory = lambda: Engine(request['cache'], input_cache_dir=request.get('input_cache_dir'),
+ **dict(options, canvas=request.get('geometry')))
+ engine, reused = resident.engine(request, factory) if resident is not None else (factory(), False)
+ loaded_seconds = time.perf_counter() - tick
+ metrics.update(config=dict(engine.config), load_seconds=loaded_seconds,
+ resident_engine_cache_hit=reused,
+ load_breakdown=({'resident_lookup_seconds': loaded_seconds} if reused else engine.load_breakdown), phase='sample',
+ load_peak_allocated_bytes=torch.cuda.max_memory_allocated(),
+ load_peak_reserved_bytes=torch.cuda.max_memory_reserved())
+ if live_budget is not None:
+ refresh_budget('load')
+ if resident is not None:
+ metrics['resident_admission'] = list(resident.decisions)
+ save(request['metrics'], metrics)
+ print(json.dumps({'event': 'loaded', 'seconds': loaded_seconds, 'resident_cache_hit': reused}), flush=True)
+ foreign_sample_bytes = 0
+ if resident is not None:
+ from .resident_models import gpu_bytes
+ foreign_sample_bytes = max(0, torch.cuda.memory_allocated() - gpu_bytes(engine))
+ from .decode_prefetch import DecoderReadAhead
+ decode_read_ahead = DecoderReadAhead(engine.base, ram_budget_bytes=request.get('ram_budget_bytes'))
+ decoder_cached = resident is not None and 'video_vae' in resident.entries
+ if decoder_cached:
+ decode_read_ahead.result.update(state='skipped', reason='Video VAE is already resident on the GPU')
+ from .sampling_memory import SamplingMemory
+ memory_diagnostics = SamplingMemory(torch, Path(request['output']).with_suffix('.sampling-memory.json')).start()
+ def sample_finalize_phase(name):
+ metrics.update(phase='sample_finalize', sample_finalize_phase=name)
+ save(request['metrics'], metrics)
+ print(json.dumps(dict(event='sample_finalize', phase=name)), flush=True)
+
+ def sampling_complete(latents, audio, sampled):
+ metrics.update(sampled, sampling_checkpoint_complete=False,
+ sample_metrics_scope='Completed sampling before offloader cleanup; finalization is excluded.')
+ metrics['sampling_memory'] = memory_diagnostics.result()
+ sample_finalize_phase('latent_validation')
+ if not bool(torch.isfinite(latents).all() and torch.isfinite(audio).all()):
+ raise RuntimeError('Non-finite video/audio latents; refusing to save a corrupted result')
+ metrics['finite_latents'] = True
+ if artifacts:
+ from .decode_resume import sampling_provenance, save_sampling
+ tick = time.perf_counter()
+ sample_finalize_phase('latent_save')
+ provenance = sampling_provenance(request)
+ save_sampling({'video': latents.cpu(), 'audio': audio.cpu(), 'seed': request['seed'],
+ 'geometry': canvas, 'sampling_provenance': provenance}, artifacts, torch)
+ metrics.update(sampling_provenance=provenance, sampling_source_attempt=request.get('resource_attempt'),
+ latent_save_seconds=time.perf_counter() - tick, sampling_checkpoint_complete=True)
+ sample_finalize_phase('offload_release')
+ try:
+ sampling_plan = request.get('sampling_plan', {})
+ if sampling_plan.get('enabled'):
+ from .two_pass import upscale_workspace, crop_latents
+ from .two_pass_metrics import combine
+ from .latent_upscale import upscale
+ if engine.steps != 8:
+ raise ValueError('Two-pass generation requires the original DMD8 schedule')
+ pass_budget = (min(request['gpu_budget_bytes'],
+ metrics['device_memory'].get('effective_allocator_limit_bytes') or request['gpu_budget_bytes'])
+ if request.get('automatic_pass_cache', False) else None)
+ first_options = request.get('first_pass_policy', {}).get('profile', {}).get('engine')
+ metrics['first_pass_policy'] = request.get('first_pass_policy')
+ latents, audio, first = engine.sample(request['conditioning'], request['seed'],
+ **sampling_plan['first'], allow_smaller_canvas=True,
+ pass_cache_budget_bytes=pass_budget, gpu_reserve_bytes=request.get('gpu_reserve_bytes', 0),
+ compute_options=first_options,
+ pass_resident_blocks=first_options.get('resident_blocks') if first_options else None,
+ step_callback=memory_diagnostics.complete, progress_total=10,
+ budget_refresh=refresh_budget if live_budget is not None else None)
+ metrics.update(sampling_passes=[first], sample_stage='latent_upscale')
+ save(request['metrics'], metrics)
+ print(json.dumps(dict(event='latent_upscale', completed_steps=8, total=10)), flush=True)
+ tick = time.perf_counter()
+ if live_budget is not None:
+ refresh_budget('upscale')
+ need = upscale_workspace(sampling_plan['upscale_target']) if sampling_plan['upscaler_sha256'] else 0
+ if need and resident is not None and not resident.ram_fits(2 * 2**30):
+ resident.make_room('latent_upscaler', need, ram_need=2 * 2**30)
+ torch.cuda.empty_cache()
+ def upscale_available():
+ free, _ = torch.cuda.mem_get_info()
+ budget = metrics['device_memory'].get('effective_allocator_limit_bytes') or request['gpu_budget_bytes']
+ return min(free - request.get('gpu_reserve_bytes', 0), budget - torch.cuda.memory_allocated())
+ available = upscale_available()
+ # Prefer buffer reuse before discarding useful host weights.
+ # The estimate selects this path, not permission to run.
+ # Actual allocation remains bounded by the worker's CUDA
+ # allocator limit. Keep the completed first-pass latents.
+ memory_saving = available < need
+ torch.cuda.reset_peak_memory_stats()
+ lift_canvas = sampling_plan['upscale_target']
+ retry_upscale = False
+ upscale_failures = []
+ metrics['latent_upscale'] = dict(estimated_workspace_bytes=need,
+ available_workspace_bytes=max(0, available), attempted_below_estimate=memory_saving,
+ buffer_reuse=memory_saving, allocation_retries=0, allocation_failures=upscale_failures)
+ save(request['metrics'], metrics)
+ try:
+ kwargs = dict(memory_saving=True) if memory_saving else {}
+ lifted_video, lifted = upscale(latents, request.get('upscaler_checkpoint'),
+ lift_canvas['width'], lift_canvas['height'], **kwargs)
+ except Exception as error:
+ from .adaptive import classify_failure
+ if classify_failure(error)['kind'] != 'gpu_oom' or (engine.closed and memory_saving):
+ raise
+ from .diagnostic_resources import error_details
+ upscale_failures.append(error_details(error))
+ retry_upscale = True
+ if retry_upscale:
+ # Leave the failed call's traceback before retrying so
+ # its tensors no longer occupy the allocator. Do not
+ # sample again or change the temporal normalization.
+ if not engine.closed:
+ if resident is not None:
+ resident.drop('engine', 'Latent upscale allocation needs additional workspace')
+ else:
+ engine.close()
+ if resident is not None:
+ resident.make_room('latent_upscaler', need, ram_need=2 * 2**30)
+ import gc
+ gc.collect()
+ torch.cuda.empty_cache()
+ metrics['latent_upscale'].update(buffer_reuse=True, allocation_retries=1)
+ save(request['metrics'], metrics)
+ lifted_video, lifted = upscale(latents, request.get('upscaler_checkpoint'),
+ lift_canvas['width'], lift_canvas['height'], memory_saving=True)
+ latents = lifted_video
+ del lifted_video
+ latents = crop_latents(latents, sampling_plan)
+ lifted.update(stage_seconds=time.perf_counter() - tick,
+ crop=dict(sampling_plan['crop']),
+ estimated_workspace_bytes=need,
+ available_workspace_bytes=max(0, available),
+ attempted_below_estimate=memory_saving,
+ allocation_retries=len(upscale_failures),
+ allocation_failures=upscale_failures,
+ torch_peak_allocated_bytes=torch.cuda.max_memory_allocated(),
+ torch_peak_reserved_bytes=torch.cuda.max_memory_reserved(),
+ transformer_reloaded=engine.closed)
+ metrics['latent_upscale'] = lifted
+ if engine.closed:
+ tick = time.perf_counter()
+ engine, _ = resident.engine(request, factory) if resident is not None else (factory(), False)
+ reload_seconds = time.perf_counter() - tick
+ metrics['load_seconds'] += reload_seconds
+ metrics['load_breakdown']['two_pass_reload_seconds'] = reload_seconds
+ metrics['sample_stage'] = 'refine'
+ save(request['metrics'], metrics)
+ def refined_complete(video, sound, receipt):
+ sampling_complete(video, sound, combine(first, receipt, lifted, sampling_plan))
+ restart_seed = (request['seed'] + sampling_plan['restart_seed_offset']) % (1 << 64)
+ latents, audio, second = engine.sample(request['conditioning'], restart_seed,
+ **sampling_plan['second'], initial_latents=(latents, audio), refine_steps=2,
+ final_step_callback=None if decoder_cached else decode_read_ahead.start,
+ step_callback=memory_diagnostics.complete, sampling_complete_callback=refined_complete,
+ progress_offset=8, progress_total=10, gpu_reserve_bytes=request.get('gpu_reserve_bytes', 0),
+ budget_refresh=refresh_budget if live_budget is not None else None)
+ sampled = combine(first, second, lifted, sampling_plan)
+ metrics.pop('sample_stage', None)
+ else:
+ latents, audio, sampled = engine.sample(request['conditioning'], request['seed'],
+ frames=canvas.get('frames', 243), width=canvas.get('width', 1344), height=canvas.get('height', 768),
+ final_step_callback=None if decoder_cached else decode_read_ahead.start,
+ step_callback=memory_diagnostics.complete, sampling_complete_callback=sampling_complete,
+ gpu_reserve_bytes=request.get('gpu_reserve_bytes', 0),
+ budget_refresh=refresh_budget if live_budget is not None else None)
+ finally:
+ metrics['sampling_memory'] = memory_diagnostics.close()
+ metrics.update(sampled)
+ metrics.pop('sample_metrics_scope', None)
+ base = engine.base
+ sample_finalize_phase('transformer_release')
+ # A one-shot worker releases the transformer. An interactive worker
+ # keeps it if live memory also covers decoding, otherwise evicts it.
+ if resident is not None:
+ resident.decoder_room(decoder, canvas)
+ else:
+ engine.close()
+ metrics['transformer_released_before_decode'] = engine.closed
+ # Keep the bounded file reader alive through transformer teardown.
+ # Releasing DiT mappings is independent of the decoder checkpoint,
+ # so both operations can run concurrently and give slow storage
+ # more time to populate the reclaimable OS cache. close() still
+ # cancels promptly when a memory gate or error stops the request.
+ read_ahead = decode_read_ahead.close()
+ if sampled.get('decoder_prefetch'):
+ read_ahead['sampling'] = dict(sampled['decoder_prefetch'])
+ metrics['decoder_read_ahead'] = read_ahead
+ decoder_read_ahead_closed = True
+ metrics.pop('sample_finalize_phase', None)
+ metrics['decode_phase'] = 'decoder_admission'
+ metrics['phase'] = 'decode'
+ if live_budget is not None:
+ refresh_budget('decode')
+ if engine is not None:
+ metrics['transformer_released_before_decode'] = engine.closed
+ if resident is not None:
+ metrics['resident_admission'] = list(resident.decisions)
+ save(request['metrics'], metrics)
+ foreign_decode_bytes = 0
+ if resident is not None:
+ from .resident_models import gpu_bytes
+ foreign_decode_bytes = sum(gpu_bytes(entry['model']) for role, entry in resident.entries.items()
+ if role not in ('video_vae', 'audio_vae'))
+ torch.cuda.reset_peak_memory_stats()
+ def decode_phase(name):
+ metrics['decode_phase'] = name
+ if resident is not None:
+ metrics['resident_admission'] = list(resident.decisions)
+ save(request['metrics'], metrics)
+ metrics.update(decode_to_file(latents, audio, request['output'], base=base, artifacts_dir=artifacts,
+ model_cache=resident, phase_callback=decode_phase, **decoder))
+ if resume and artifacts:
+ from .decode_resume import retain_latents
+ retain_latents(request['resume_decode']['latents'], artifacts)
+ metrics.update(success=True, phase='complete')
+ if resident is not None:
+ from .resident_models import key
+ if not resume:
+ from .two_pass_metrics import sampling_workspace_peak
+ resident.observe_peak('engine', resident.engine_key(request),
+ sampling_workspace_peak(sampled) - foreign_sample_bytes)
+ resident.observe_peak('decode', key(dict(options=decoder, canvas=canvas)), torch.cuda.max_memory_allocated() - foreign_decode_bytes)
+ metrics['resident_models'] = resident.snapshot()
+ except BaseException as error:
+ original_error = error
+ metrics['error'] = repr(error)
+ from .adaptive import classify_failure
+ metrics['failure'] = classify_failure(error)
+ # A driver query can itself fail. Persist the original exception first.
+ save(request['metrics'], metrics)
+ from .encoder_memory import failure_resources
+ metrics['failure'].update(failure_resources(torch, query_cuda=metrics['failure']['kind'] != 'cuda_error'))
+ if isinstance(error, torch.cuda.OutOfMemoryError):
+ metrics['failure'].update(kind='gpu_oom', outcome='resource_failure', retryable=True)
+ metrics['failure']['device_memory'] = metrics.get('device_memory', {})
+ # Write the first failure before any cleanup CUDA call: the device may
+ # no longer be readable and that secondary error is not its cause.
+ if resident is not None:
+ metrics['resident_admission'] = list(resident.decisions)
+ save(request['metrics'], metrics)
+ raise
+ finally:
+ if live_budget is not None:
+ live_budget.close()
+ if resident is not None:
+ metrics['resident_admission'] = list(resident.decisions)
+ from .adaptive import classify_failure
+ if decode_read_ahead is not None and not decoder_read_ahead_closed:
+ metrics['decoder_read_ahead'] = decode_read_ahead.close()
+ cleanup_error = None
+ unsafe = classify_failure(original_error or '', metrics)['kind'] == 'cuda_error'
+ if unsafe:
+ metrics['cleanup_skipped'] = 'Unsafe CUDA evidence; retain first failure and let the isolated process exit.'
+ if engine is not None and not unsafe and (resident is None or not metrics['success']):
+ try:
+ engine.close()
+ except BaseException as error:
+ cleanup_error = error
+ metrics.setdefault('cleanup_errors', []).append(repr(error))
+ metrics['work_seconds'] = time.perf_counter() - started
+ unsafe = classify_failure(original_error or '', metrics)['kind'] == 'cuda_error'
+ if not unsafe and torch.cuda.is_initialized():
+ try:
+ metrics['final_stage_peak_allocated_bytes'] = torch.cuda.max_memory_allocated()
+ metrics['final_stage_peak_reserved_bytes'] = torch.cuda.max_memory_reserved()
+ except BaseException as error:
+ cleanup_error = cleanup_error or error
+ metrics.setdefault('cleanup_errors', []).append(repr(error))
+ if cleanup_error is not None and original_error is None:
+ from .adaptive import classify_failure
+ metrics.update(success=False, error=repr(cleanup_error), failure=classify_failure(cleanup_error))
+ save(request['metrics'], metrics)
+ if cleanup_error is not None and original_error is None:
+ raise cleanup_error
+
+
+if __name__ == '__main__':
+ main()
diff --git a/pyproject.toml b/pyproject.toml
new file mode 100644
index 0000000..9abd5d0
--- /dev/null
+++ b/pyproject.toml
@@ -0,0 +1,34 @@
+[build-system]
+requires = ["setuptools>=77"]
+build-backend = "setuptools.build_meta"
+
+[project]
+name = "freevideo-engine"
+version = "2026.9.16.41444"
+description = "FreeVideo video and audio generation with automatic GPU and memory optimization"
+requires-python = ">=3.12,<3.13"
+readme = "README.md"
+license = "Apache-2.0"
+dependencies = ["psutil>=6", "safetensors>=0.8.0", "numpy>=2", "pillow>=11", "av>=14"]
+
+[project.optional-dependencies]
+launcher = ["PySide6-Essentials==6.11.2"]
+runtime = [
+ "triton==3.7.1; sys_platform == 'linux'",
+ "triton-windows==3.7.1.post27; sys_platform == 'win32'",
+ "transformers==5.15.0", "accelerate==1.14.0", "peft==0.20.0",
+ "safetensors==0.8.0", "huggingface_hub==1.30.0", "hf-xet==1.6.1a0",
+ "omegaconf==2.3.1", "pyyaml==6.0.3", "einops==0.8.2",
+ "numpy==2.5.2", "pillow==12.3.0", "av==18.1.0", "tqdm",
+ "importlib_metadata>=8", "requests[socks]>=2.32", "httpx[socks]>=0.28",
+ "modelscope-hub==0.4.2"
+]
+
+[project.scripts]
+freevideo = "freevideo_engine.cli:main"
+
+[tool.setuptools.packages.find]
+include = ["freevideo_engine*"]
+
+[tool.setuptools.package-data]
+freevideo_engine = ["build-version.txt", "dependencies.json", "model_files.json", "model_shares.json", "prepared_models.json", "bootstrap_versions.json", "test_prompts.json", "windows_process_host.ps1", "windows_bootstrap.ps1", "assets/*.png", "assets/*.ico", "launcher/*.qml", "launcher/licenses/*.txt", "licenses/*.txt"]
diff --git a/scripts/bootstrap_linux.sh b/scripts/bootstrap_linux.sh
new file mode 100644
index 0000000..c1e8c22
--- /dev/null
+++ b/scripts/bootstrap_linux.sh
@@ -0,0 +1,321 @@
+#!/usr/bin/env bash
+# Sourced by all Linux launchers. No system Python or shell activation required.
+# Keep these pins in sync with bootstrap_versions.json (checked in CPU tests).
+FV_UV_VERSION=0.9.0
+FV_UV_URL=https://github.com/astral-sh/uv/releases/download/0.9.0/uv-x86_64-unknown-linux-gnu.tar.gz
+FV_UV_SHA256=4dadaa5ff5009ccd6a0a43f6ccfa32bf36ed2eff18df7011275a9b1d81950e7b
+FV_PYTHON_VERSION=3.12.3
+
+fv_note() { printf 'FreeVideo: %s\n' "$*" >&2; }
+fv_fail() { fv_note "$*"; return 1; }
+
+fv_python_ok() {
+ [[ -n "$1" ]] && command -v -- "$1" >/dev/null 2>&1 &&
+ "$1" -I -B -c 'import sys; sys.exit(0 if sys.version_info >= (3, 9) else 1)' >/dev/null 2>&1
+}
+
+fv_find_python() {
+ local candidate
+ if [[ -n "${FREEVIDEO_PYTHON:-}" ]]; then
+ fv_python_ok "$FREEVIDEO_PYTHON" || { fv_fail 'FREEVIDEO_PYTHON must point to a working Python 3.9+ executable.'; return 2; }
+ FREEVIDEO_BOOTSTRAP_PYTHON="$FREEVIDEO_PYTHON"
+ return 0
+ fi
+ for candidate in "$fv_root/envs/unified/bin/python" "$fv_root/envs/engine/bin/python" \
+ "$FREEVIDEO_BOOTSTRAP_ROOT/python/cpython-$FV_PYTHON_VERSION-linux-x86_64-gnu/bin/python3.12" \
+ python3 python3.12 python; do
+ if fv_python_ok "$candidate"; then
+ FREEVIDEO_BOOTSTRAP_PYTHON="$(command -v -- "$candidate")"
+ return 0
+ fi
+ done
+ return 1
+}
+
+fv_help() {
+ cat <<'EOF'
+FreeVideo Linux launcher
+
+ ./setup.sh Detect missing tools, prepare Python, then review installation
+ ./test.sh Generate and retain videos, timings and memory reports
+ ./freevideo optimize Validate lightweight tuning from the previous test
+ ./freevideo diagnose Export a small local diagnostic ZIP
+
+Setup launcher options:
+ --root PATH Engine/model installation directory
+ --plan Check only; may prepare private Python if none is available
+ --verbose Show full setup details and per-step logs
+ --install-system-deps Allow missing OS packages without the interactive prompt
+ --bootstrap-only Prepare basic tools/Python only; no CUDA, models or GPU needed
+ --network official Upstream sources only; compare proxy/direct connections
+ --model-downloader xet Require HF Xet for large weights; stop instead of falling back
+ --reuse-models PATH Verify and reuse compatible models in an existing folder
+
+No Python, pip, virtual environment or CUDA toolkit installation is needed beforehand.
+Default installation: this checkout's directory (independent of the current directory).
+Private Python bootstrap: <installation>/.freevideo/bootstrap.
+Missing OS packages require root/sudo approval. Full setup reviews GPU/RAM/disk needs
+and model licenses separately. A working NVIDIA driver 580+ is required for the engine.
+Rerun --help after bootstrap for the full command's options.
+EOF
+}
+
+fv_missing_tools() {
+ fv_missing=()
+ local name
+ for name in "$@"; do
+ command -v "$name" >/dev/null 2>&1 || fv_missing+=("$name")
+ done
+}
+
+fv_system_deps() {
+ ((${#fv_missing[@]})) || return 0
+ fv_note "Missing basic tools: ${fv_missing[*]}"
+ local manager name package reply
+ local -a packages=() privilege=() install=() refresh=()
+ if command -v apt-get >/dev/null 2>&1; then manager=apt-get
+ elif command -v dnf >/dev/null 2>&1; then manager=dnf
+ elif command -v yum >/dev/null 2>&1; then manager=yum
+ elif command -v zypper >/dev/null 2>&1; then manager=zypper
+ else
+ fv_fail 'No supported package manager found (apt-get/dnf/yum/zypper). Ask your administrator to install the listed tools, then rerun this command.'
+ return 1
+ fi
+ for name in "${fv_missing[@]}"; do
+ package="$name"
+ case "$name" in
+ g++|make) if [[ "$manager" == apt-get ]]; then package=build-essential; else package=gcc-c++; fi ;;
+ sha256sum) package=coreutils ;;
+ flock) package=util-linux ;;
+ esac
+ [[ " ${packages[*]} " == *" $package "* ]] || packages+=("$package")
+ if [[ "$name" == make && "$manager" != apt-get ]]; then packages+=(make); fi
+ done
+ # Minimal OS images may also lack the TLS trust store.
+ packages+=(ca-certificates)
+ case "$manager" in
+ apt-get) refresh=(apt-get -o Acquire::Retries=2 -o Acquire::http::Timeout=30 -o Acquire::https::Timeout=30 update)
+ install=(apt-get -o Acquire::Retries=2 -o Acquire::http::Timeout=30 -o Acquire::https::Timeout=30 install -y --no-install-recommends "${packages[@]}") ;;
+ dnf|yum) install=("$manager" --setopt=timeout=30 --setopt=retries=2 install -y "${packages[@]}") ;;
+ zypper) install=(zypper --non-interactive install --no-recommends "${packages[@]}") ;;
+ esac
+ if (( EUID != 0 )); then privilege=(sudo); fi
+ fv_note 'System package step (existing Python and drivers are not replaced):'
+ ((${#refresh[@]} == 0)) || { printf ' '; printf '%q ' "${privilege[@]}" "${refresh[@]}"; printf '\n'; } >&2
+ { printf ' '; printf '%q ' "${privilege[@]}" "${install[@]}"; printf '\n'; } >&2
+ if (( fv_readonly )); then
+ fv_fail 'Check only: no system packages installed. Run ./setup.sh to install the missing tools.'
+ return 1
+ fi
+ if (( ! fv_allow_system )); then
+ if [[ ! -t 0 ]]; then
+ fv_fail 'System packages need approval. Run interactively, or add --install-system-deps to a reviewed unattended setup.'
+ return 1
+ fi
+ printf 'Install these missing system packages? [Y/n] / 回车安装以上基础工具,n 取消: ' >&2
+ read -r reply || reply=n
+ case "$reply" in ''|y|Y|yes|YES) ;; *) fv_fail 'Cancelled. No system packages installed.'; return 1 ;; esac
+ fi
+ if (( EUID != 0 )) && ! command -v sudo >/dev/null 2>&1; then
+ fv_fail 'sudo is unavailable. Ask your administrator to run the package commands above, then rerun setup as your normal user.'
+ return 1
+ fi
+ mkdir -p -- "$FREEVIDEO_BOOTSTRAP_ROOT"
+ fv_note "System package log: $FREEVIDEO_BOOTSTRAP_ROOT/bootstrap.log"
+ # Preserve proxy variables through sudo without passing proxy secrets as arguments.
+ if ((${#privilege[@]})); then
+ privilege+=("--preserve-env=http_proxy,https_proxy,all_proxy,no_proxy,HTTP_PROXY,HTTPS_PROXY,ALL_PROXY,NO_PROXY")
+ fi
+ if ((${#refresh[@]})); then
+ fv_bootstrap_network "${privilege[@]}" "${refresh[@]}" 2>&1 | tee -a "$FREEVIDEO_BOOTSTRAP_ROOT/bootstrap.log" >&2 || return 1
+ fi
+ fv_bootstrap_network "${privilege[@]}" "${install[@]}" 2>&1 | tee -a "$FREEVIDEO_BOOTSTRAP_ROOT/bootstrap.log" >&2 || return 1
+ fv_missing_tools "${fv_missing[@]}"
+ ((${#fv_missing[@]} == 0)) || { fv_fail "Tools still missing after package installation: ${fv_missing[*]}"; return 1; }
+}
+
+fv_proxy_routes() {
+ case "${FREEVIDEO_PROXY_MODE:-auto}" in
+ proxy) fv_routes=(inherited); return ;;
+ direct) fv_routes=(direct); return ;;
+ auto) ;;
+ *) fv_fail 'Invalid download connection mode.'; return 1 ;;
+ esac
+ fv_routes=(inherited)
+ if [[ -n "${http_proxy:-}${https_proxy:-}${all_proxy:-}${HTTP_PROXY:-}${HTTPS_PROXY:-}${ALL_PROXY:-}" ]]; then
+ fv_routes+=(direct)
+ fi
+}
+
+fv_with_route() (
+ local route="$1"
+ shift
+ if [[ "$route" == direct ]]; then
+ unset http_proxy https_proxy all_proxy no_proxy HTTP_PROXY HTTPS_PROXY ALL_PROXY NO_PROXY
+ export no_proxy='*' NO_PROXY='*'
+ fi
+ "$@"
+)
+
+fv_bootstrap_network() {
+ local -a fv_routes=()
+ fv_proxy_routes
+ local route
+ for route in "${fv_routes[@]}"; do
+ if fv_with_route "$route" "$@"; then return 0; fi
+ done
+ return 1
+}
+
+fv_download_uv() {
+ local url route attempt=0 archive="$FREEVIDEO_BOOTSTRAP_ROOT/uv-$FV_UV_VERSION.tar.gz" part
+ local -a sources=("$FV_UV_URL")
+ local -a fv_routes=()
+ fv_proxy_routes
+ if [[ "$fv_network" != official ]]; then
+ if [[ -n "${FREEVIDEO_GITHUB_MIRROR:-}" ]]; then
+ sources=("${FREEVIDEO_GITHUB_MIRROR%/}/${FV_UV_URL#https://github.com/}" "${sources[@]}")
+ fi
+ sources+=("https://ghfast.top/$FV_UV_URL")
+ fi
+ if [[ -f "$archive" ]] && printf '%s %s\n' "$FV_UV_SHA256" "$archive" | sha256sum --check --status; then
+ fv_note 'Reusing verified bootstrap archive.'
+ else
+ if [[ -e "$archive" ]]; then mv -- "$archive" "$archive.rejected.$(date +%s).$$"; fi
+ for url in "${sources[@]}"; do
+ for route in "${fv_routes[@]}"; do
+ ((attempt+=1))
+ fv_note "Downloading bootstrap tool, attempt $attempt ($route connection)…"
+ # Do not put credentials from a custom URL in argv or progress output.
+ [[ "$url" != *$'\n'* && "$url" != *$'\r'* && "$url" != *'"'* && "$url" != *"\\"* ]] || { fv_fail 'Invalid mirror URL.'; return 1; }
+ part="$archive.partial.$(date +%s).$$.$attempt"
+ if printf 'url = "%s"\n' "$url" | fv_with_route "$route" curl --disable --config - --fail --location --silent --show-error \
+ --connect-timeout 5 --max-time 120 --speed-limit 1024 --speed-time 20 --retry 1 \
+ --retry-max-time 130 --output "$part" >>"$FREEVIDEO_BOOTSTRAP_ROOT/bootstrap.log" 2>&1; then
+ if printf '%s %s\n' "$FV_UV_SHA256" "$part" | sha256sum --check --status; then
+ mv -- "$part" "$archive"
+ break 2
+ fi
+ fv_note 'SHA256 mismatch; rejected download retained. Trying the next source.'
+ printf 'uv source %s: SHA256 mismatch\n' "$attempt" >>"$FREEVIDEO_BOOTSTRAP_ROOT/bootstrap.log"
+ else
+ fv_note 'Transfer failed; partial download and log retained. Trying the next source.'
+ fi
+ done
+ done
+ [[ -f "$archive" ]] || { fv_fail "All bootstrap sources failed. Retry the same command; log: $FREEVIDEO_BOOTSTRAP_ROOT/bootstrap.log"; return 1; }
+ fi
+ mkdir -p -- "$FREEVIDEO_BOOTSTRAP_ROOT/tools"
+ tar -xzf "$archive" -C "$FREEVIDEO_BOOTSTRAP_ROOT/tools" --no-same-owner
+}
+
+fv_install_python() (
+ # The subshell owns this lock: never pass it into setup/testing workers.
+ mkdir -p -- "$FREEVIDEO_BOOTSTRAP_ROOT"
+ exec 9>"$FREEVIDEO_BOOTSTRAP_ROOT/bootstrap.lock"
+ flock -n 9 || { fv_fail 'Another command is preparing Python. Wait for it to finish and retry.'; exit 1; }
+ if fv_python_ok "$FREEVIDEO_BOOTSTRAP_ROOT/python/cpython-$FV_PYTHON_VERSION-linux-x86_64-gnu/bin/python3.12"; then exit 0; fi
+ fv_note "Preparing private Python $FV_PYTHON_VERSION (allow ~400 MiB including retained bootstrap downloads; no Torch or models yet)."
+ fv_note "Files and log: $FREEVIDEO_BOOTSTRAP_ROOT"
+ fv_download_uv || exit 1
+ local uv="$FREEVIDEO_BOOTSTRAP_ROOT/tools/uv-x86_64-unknown-linux-gnu/uv" source route attempt=0
+ local -a fv_routes=()
+ fv_proxy_routes
+ local -a sources=(https://github.com/astral-sh/python-build-standalone/releases/download)
+ if [[ "$fv_network" != official ]]; then
+ if [[ -n "${UV_PYTHON_INSTALL_MIRROR:-}" ]]; then sources=("$UV_PYTHON_INSTALL_MIRROR" "${sources[@]}"); fi
+ if [[ -n "${FREEVIDEO_GITHUB_MIRROR:-}" ]]; then sources+=("${FREEVIDEO_GITHUB_MIRROR%/}/astral-sh/python-build-standalone/releases/download"); fi
+ sources+=(https://ghfast.top/https://github.com/astral-sh/python-build-standalone/releases/download)
+ fi
+ export UV_PYTHON_INSTALL_DIR="$FREEVIDEO_BOOTSTRAP_ROOT/python" UV_CACHE_DIR="$FREEVIDEO_BOOTSTRAP_ROOT/cache"
+ export UV_HTTP_TIMEOUT=20 UV_HTTP_RETRIES=1
+ for source in "${sources[@]}"; do
+ for route in "${fv_routes[@]}"; do
+ ((attempt+=1))
+ fv_note "Installing private Python, attempt $attempt ($route connection)…"
+ printf 'Python source %s\n' "$attempt" >>"$FREEVIDEO_BOOTSTRAP_ROOT/bootstrap.log"
+ if UV_PYTHON_INSTALL_MIRROR="$source" fv_with_route "$route" "$uv" --no-config python install --no-bin --no-registry \
+ --no-progress --color never "$FV_PYTHON_VERSION" >>"$FREEVIDEO_BOOTSTRAP_ROOT/bootstrap.log" 2>&1; then
+ fv_python_ok "$UV_PYTHON_INSTALL_DIR/cpython-$FV_PYTHON_VERSION-linux-x86_64-gnu/bin/python3.12" && exit 0
+ fi
+ fv_note 'Python preparation failed; retrying the next source. Existing files retained.'
+ done
+ done
+ fv_fail "Could not prepare Python. Check $FREEVIDEO_BOOTSTRAP_ROOT/bootstrap.log and retry the same command."
+ exit 1
+)
+
+freevideo_bootstrap_linux() {
+ local mode="$1" arg command_name='' help=0 bootstrap_only=0 found=1
+ shift
+ local fv_source="${FREEVIDEO_SOURCE_DIR:-$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)}"
+ local fv_root="${FREEVIDEO_HOME:-$fv_source}"
+ local fv_readonly=0 fv_allow_system=0 fv_network=auto
+ local -a fv_missing=()
+ FREEVIDEO_BOOTSTRAP_DONE=0
+ FREEVIDEO_BOOTSTRAP_ARGS=()
+ while (($#)); do
+ arg="$1"; shift
+ case "$arg" in
+ --root|--config|--network|--model-downloader|--reuse-models|--reuse-models-manifest)
+ (($#)) || { fv_fail "$arg requires a value."; return 1; }
+ [[ "$arg" != --root ]] || fv_root="$1"
+ [[ "$arg" != --network ]] || fv_network="$1"
+ FREEVIDEO_BOOTSTRAP_ARGS+=("$arg" "$1"); shift ;;
+ --root=*) fv_root="${arg#*=}"; FREEVIDEO_BOOTSTRAP_ARGS+=("$arg") ;;
+ --network=*) fv_network="${arg#*=}"; FREEVIDEO_BOOTSTRAP_ARGS+=("$arg") ;;
+ --install-system-deps) fv_allow_system=1 ;;
+ --bootstrap-only) bootstrap_only=1 ;;
+ --plan|--check) fv_readonly=1; FREEVIDEO_BOOTSTRAP_ARGS+=("$arg") ;;
+ --help|-h) help=1; FREEVIDEO_BOOTSTRAP_ARGS+=("$arg") ;;
+ *) FREEVIDEO_BOOTSTRAP_ARGS+=("$arg")
+ if [[ "$mode" == cli && -z "$command_name" && "$arg" != -* ]]; then command_name="$arg"; fi ;;
+ esac
+ done
+ # Resolve the selected installation before choosing Python or its download
+ # directory. The same root reaches every Python launcher, even from another cwd.
+ # shellcheck disable=SC2088 # Match literal user input, then expand it explicitly.
+ case "$fv_root" in '~') fv_root="$HOME" ;; '~/'*) fv_root="$HOME/${fv_root:2}" ;; esac
+ export FREEVIDEO_HOME="$fv_root"
+ export FREEVIDEO_BOOTSTRAP_ROOT="${FREEVIDEO_BOOTSTRAP_ROOT:-$fv_root/.freevideo/bootstrap}"
+ [[ "$mode" != cli || "$command_name" != setup ]] || mode=setup
+ [[ "$mode" != cli || -n "$command_name" ]] || help=1
+ if (( bootstrap_only || fv_allow_system )) && [[ "$mode" != setup ]]; then
+ fv_fail '--bootstrap-only and --install-system-deps are setup options.'; return 1
+ fi
+ if (( bootstrap_only && fv_readonly )); then fv_fail '--bootstrap-only cannot be combined with --plan.'; return 1; fi
+ [[ "$fv_network" == auto || "$fv_network" == official ]] || { fv_fail '--network must be auto or official.'; return 1; }
+ fv_find_python && found=0 || found=$?
+ (( found != 2 )) || return 1
+ if (( help )); then
+ if (( found )); then fv_help; FREEVIDEO_BOOTSTRAP_DONE=1
+ elif [[ "$mode" == setup ]]; then fv_help; printf '\n'; fi
+ return 0
+ fi
+ [[ "$(uname -s)" == Linux && "$(uname -m)" == x86_64 ]] || { fv_fail 'Use native Windows launchers on Windows. Linux setup currently supports x86_64.'; return 1; }
+ if command -v getconf >/dev/null 2>&1 && ! getconf GNU_LIBC_VERSION >/dev/null 2>&1; then
+ fv_fail 'Linux setup needs glibc (for example Ubuntu/Debian); musl/Alpine CUDA wheels are unsupported.'; return 1
+ fi
+ # curl and system package managers differ in uppercase proxy handling.
+ [[ -n "${http_proxy:-}" || -z "${HTTP_PROXY:-}" ]] || export http_proxy="$HTTP_PROXY"
+ [[ -n "${https_proxy:-}" || -z "${HTTPS_PROXY:-}" ]] || export https_proxy="$HTTPS_PROXY"
+ [[ -n "${all_proxy:-}" || -z "${ALL_PROXY:-}" ]] || export all_proxy="$ALL_PROXY"
+ [[ -n "${no_proxy:-}" || -z "${NO_PROXY:-}" ]] || export no_proxy="$NO_PROXY"
+ if [[ "$mode" == setup ]] && (( ! fv_readonly )); then
+ fv_missing_tools git curl g++ make tar gzip sha256sum flock
+ fv_system_deps || return 1
+ fi
+ if (( found )); then
+ fv_missing_tools curl tar gzip sha256sum flock
+ fv_system_deps || return 1
+ (( ! fv_readonly )) || fv_note 'Check only: a private Python bootstrap is needed to display the plan. No engine, model or system package installation.'
+ fv_install_python || return 1
+ fv_find_python || { fv_fail 'Python installed but could not start. See bootstrap.log.'; return 1; }
+ fi
+ if (( bootstrap_only )); then
+ fv_note "Basic tools ready. Python: $FREEVIDEO_BOOTSTRAP_PYTHON"
+ fv_note 'Run ./setup.sh next to review GPU/RAM/disk requirements and install the engine.'
+ # shellcheck disable=SC2034 # Output read by the sourcing launchers.
+ FREEVIDEO_BOOTSTRAP_DONE=1
+ fi
+}
diff --git a/scripts/build_launcher_notices.py b/scripts/build_launcher_notices.py
new file mode 100644
index 0000000..52a6e75
--- /dev/null
+++ b/scripts/build_launcher_notices.py
@@ -0,0 +1,65 @@
+"""Collect notices from the exact interpreter and packages used by the launcher.
+
+This is a launcher inventory, not a license inventory for downloaded models or
+the separately installed inference environment. No network access is needed.
+"""
+import hashlib
+from importlib import metadata
+import json
+from pathlib import Path
+import platform
+import shutil
+import sys
+import sysconfig
+
+
+def collect(root, destination):
+ root, destination = Path(root), Path(destination)
+ destination.mkdir(parents=True, exist_ok=False)
+ components = []
+
+ def copy(source, relative):
+ target = destination / relative
+ target.parent.mkdir(parents=True, exist_ok=True)
+ shutil.copyfile(source, target)
+ return relative
+
+ # The official Windows interpreter keeps LICENSE.txt beside python.exe;
+ # standalone Unix Python distributions usually keep it in the stdlib.
+ python_license = next((path for path in (
+ Path(sys.base_prefix) / 'LICENSE.txt',
+ Path(sysconfig.get_path('stdlib')) / 'LICENSE.txt',
+ ) if path.is_file()), None)
+ if python_license is None:
+ raise ValueError('The build interpreter is missing its CPython LICENSE.txt')
+ components.append(dict(name='CPython', version=platform.python_version(),
+ notices=[copy(python_license, 'python/LICENSE.txt')]))
+
+ for package in ('psutil', 'PyInstaller'):
+ distribution = metadata.distribution(package)
+ licenses = sorted((file for file in distribution.files or []
+ if file.name.lower().startswith(('license', 'copying', 'notice'))), key=str)
+ if not licenses:
+ raise ValueError('Build package is missing its license files: ' + package)
+ notices = [copy(distribution.locate_file(file), '%s/%02d-%s.txt' %
+ (package, index, file.name)) for index, file in enumerate(licenses)]
+ components.append(dict(name=package, version=distribution.version, notices=notices))
+
+ qt_notices = root / 'freevideo_engine/launcher/licenses'
+ notice = (qt_notices / 'NOTICE.txt').read_text(encoding='utf-8')
+ qt_version = metadata.version('PySide6-Essentials')
+ if qt_version not in notice or metadata.version('shiboken6') != qt_version:
+ raise ValueError('Update the Qt source/attribution notice for the installed build version')
+ files = ['NOTICE.txt', 'LGPL-3.0-only.txt', 'GPL-3.0-only.txt']
+ components.append(dict(name='PySide6-Essentials / Qt / Shiboken6', version=qt_version,
+ notices=[copy(qt_notices / name, 'qt/' + name) for name in files]))
+ files = []
+ for path in sorted(destination.rglob('*')):
+ if path.is_file():
+ data = path.read_bytes()
+ files.append(dict(path=path.relative_to(destination).as_posix(), bytes=len(data),
+ sha256=hashlib.sha256(data).hexdigest()))
+ value = dict(schema=1, scope='Launcher interpreter, psutil, PyInstaller bootloader and Qt notices',
+ components=components, files=files)
+ (destination / 'inventory.json').write_text(json.dumps(value, indent=2) + '\n', encoding='utf-8')
+ return value
diff --git a/scripts/build_windows.py b/scripts/build_windows.py
new file mode 100644
index 0000000..7f9058b
--- /dev/null
+++ b/scripts/build_windows.py
@@ -0,0 +1,144 @@
+"""Build on Windows with Python 3.12 after installing constraints/windows-launcher.txt."""
+import argparse
+import hashlib
+import json
+import os
+import re
+from pathlib import Path
+import shutil
+import subprocess
+import sys
+import time
+
+ROOT = Path(__file__).resolve().parents[1]
+sys.path.insert(0, str(ROOT))
+from freevideo_engine.desktop_runtime import check_launcher_dependencies, package_data_files, source_files
+from freevideo_engine.launcher_update import REPOSITORY, CHANNEL
+from scripts.build_launcher_notices import collect as collect_notices
+
+
+def build_info(root):
+ try:
+ revision = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=root, text=True,
+ stderr=subprocess.DEVNULL).strip()
+ except (OSError, subprocess.CalledProcessError):
+ rows = {p.relative_to(root).as_posix(): hashlib.sha256(p.read_bytes()).hexdigest() for p in source_files(root)}
+ revision = hashlib.sha256(json.dumps(rows, sort_keys=True).encode()).hexdigest()
+ built_at = int(time.time())
+ # Numeric UTC date + HHMMSS is also a valid Python package version.
+ version = '.'.join(str(int(part)) for part in time.strftime('%Y.%m.%d.%H%M%S', time.gmtime(built_at)).split('.'))
+ return dict(schema=1, repository=REPOSITORY, channel=CHANNEL, revision=revision,
+ built_at=built_at, version=version)
+
+
+def stamp_source(staged, identity):
+ version = identity['version']
+ stamp = staged / 'freevideo_engine/build-version.txt'
+ stamp.write_text(version, encoding='utf-8')
+ project = staged / 'pyproject.toml'
+ project.write_text(re.sub(r'^version = "[^"]+"', 'version = "' + version + '"',
+ project.read_text(encoding='utf-8'), count=1, flags=re.M), encoding='utf-8')
+ return stamp
+
+
+def bundle_data_args(root):
+ # engine-source backs external Python; frozen modules have their own __file__
+ # under _MEIPASS/freevideo_engine and need the same resources beside them.
+ arguments = []
+ for path in package_data_files(root):
+ if path.is_relative_to(root / 'freevideo_engine/launcher/licenses/bundled'):
+ continue # A rebuild collects notices from its current environment.
+ arguments.extend(['--add-data', str(path) + os.pathsep + path.parent.relative_to(root).as_posix()])
+ return arguments
+
+
+def version_file(out, identity):
+ """Write standard PE metadata so Windows can identify the publisher/product."""
+ parts = tuple(int(part) for part in identity['version'].split('.'))
+ # VERSIONINFO stores four unsigned 16-bit words. The human-readable
+ # timestamp remains complete in StringFileInfo; the resource build word
+ # is deliberately folded into the valid 16-bit range.
+ version = parts[:3] + (identity['built_at'] & 0xffff,)
+ text = '''# UTF-8
+VSVersionInfo(
+ ffi=FixedFileInfo(filevers=%r, prodvers=%r, mask=0x3f, flags=0x0,
+ OS=0x40004, fileType=0x1, subtype=0x0, date=(0, 0)),
+ kids=[StringFileInfo([
+ StringTable('040904B0', [
+ StringStruct('CompanyName', 'FlashML'),
+ StringStruct('FileDescription', 'FreeVideo launcher'),
+ StringStruct('FileVersion', '%s'),
+ StringStruct('InternalName', 'FreeVideo'),
+ StringStruct('OriginalFilename', 'FreeVideo.exe'),
+ StringStruct('ProductName', 'FreeVideo'),
+ StringStruct('ProductVersion', '%s'),
+ StringStruct('LegalCopyright', 'Copyright (c) FlashML'),
+ ])]),
+ VarFileInfo([VarStruct('Translation', [1033, 1200])])
+ ]
+)
+''' % (version, version, identity['version'], identity['version'])
+ path = out / 'version-info.txt'
+ path.write_text(text, encoding='utf-8')
+ return path
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--out', type=Path, required=True)
+ parser.add_argument('--onefolder', action='store_true', help='Build a folder ZIP that runs without temporary self-extraction')
+ args = parser.parse_args()
+ if os.name != 'nt' or sys.version_info[:2] != (3, 12):
+ parser.error('Build this artifact on Windows x64 with Python 3.12')
+ try:
+ check_launcher_dependencies()
+ from PySide6 import QtQml, QtQuick, QtQuickControls2, QtWidgets
+ except ImportError as error:
+ parser.error('Launcher build dependencies are missing or broken: %s. Run this Python with '
+ '-m pip install -r constraints/windows-launcher.txt before building.' % error)
+ out = args.out.resolve()
+ out.mkdir(parents=True, exist_ok=False)
+ identity = build_info(ROOT)
+ if args.onefolder:
+ identity['packaging'] = 'onedir'
+ build_file = out / 'launcher-build.json'
+ build_file.write_text(json.dumps(identity, indent=2), encoding='utf-8')
+ version_info = version_file(out, identity)
+ staged = out / 'engine-source'
+ for path in source_files(ROOT):
+ if path.is_relative_to(ROOT / 'freevideo_engine/launcher/licenses/bundled'):
+ continue
+ target = staged / path.relative_to(ROOT)
+ target.parent.mkdir(parents=True, exist_ok=True)
+ shutil.copyfile(path, target)
+ stamp = stamp_source(staged, identity)
+ notices = staged / 'freevideo_engine/launcher/licenses/bundled'
+ collect_notices(ROOT, notices)
+ command = [sys.executable, '-m', 'PyInstaller', '--noconfirm', '--windowed', '--noupx',
+ '--onedir' if args.onefolder else '--onefile', '--name', 'FreeVideo',
+ '--icon', str(ROOT / 'freevideo_engine/assets/icon.ico'),
+ '--version-file', str(version_info),
+ '--distpath', str(out / 'dist'), '--workpath', str(out / 'build'), '--specpath', str(out),
+ '--paths', str(ROOT), '--add-data', str(staged) + os.pathsep + 'engine-source',
+ '--add-data', str(build_file) + os.pathsep + '.',
+ '--add-data', str(stamp) + os.pathsep + 'freevideo_engine',
+ '--add-data', str(notices) + os.pathsep + 'freevideo_engine/launcher/licenses/bundled',
+ *bundle_data_args(ROOT),
+ '--hidden-import', 'psutil',
+ '--exclude-module', 'torch', '--exclude-module', 'triton', '--exclude-module', 'numpy',
+ '--exclude-module', 'transformers', '--exclude-module', 'tkinter',
+ str(ROOT / 'scripts/windows_app.py')]
+ subprocess.run(command, cwd=ROOT, check=True)
+ executable = out / 'dist' / ('FreeVideo/FreeVideo.exe' if args.onefolder else 'FreeVideo.exe')
+ digest = hashlib.sha256(executable.read_bytes()).hexdigest()
+ (executable.parent / 'SHA256SUMS.txt').write_text(digest + ' FreeVideo.exe\n', encoding='utf-8')
+ (executable.parent / 'launcher-build.json').write_text(json.dumps(identity, indent=2), encoding='utf-8')
+ shutil.copytree(notices, executable.parent / 'licenses')
+ if args.onefolder:
+ shutil.make_archive(str(out / 'dist/FreeVideo-Windows-folder'), 'zip',
+ root_dir=out / 'dist', base_dir='FreeVideo')
+ print(json.dumps({'exe': str(executable), 'bytes': executable.stat().st_size, 'sha256': digest}))
+
+
+if __name__ == '__main__':
+ main()
diff --git a/scripts/check_installation.py b/scripts/check_installation.py
new file mode 100644
index 0000000..c0e7867
--- /dev/null
+++ b/scripts/check_installation.py
@@ -0,0 +1,62 @@
+"""Check an installation source bundle without models, a GPU or network access."""
+import importlib.abc
+import importlib.util
+import json
+from pathlib import Path
+import sys
+import tempfile
+
+ROOT = Path(__file__).resolve().parents[1]
+sys.path.insert(0, str(ROOT))
+
+
+class NoModelImports(importlib.abc.MetaPathFinder):
+ def find_spec(self, fullname, path=None, target=None):
+ if fullname.split('.')[0] in ('torch', 'triton', 'transformers', 'diffusers'):
+ raise RuntimeError('Installation unexpectedly imports ' + fullname)
+
+
+def main():
+ sys.meta_path.insert(0, NoModelImports())
+ from freevideo_engine import cli, comfy_launcher, comfy_launcher_runtime, modern_launcher
+ from freevideo_engine.desktop_runtime import check_launcher_payload, materialize_source, source_files
+
+ spec = importlib.util.spec_from_file_location('freevideo_install_check', ROOT / '__init__.py',
+ submodule_search_locations=[str(ROOT)])
+ entry = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(entry)
+ assert callable(entry.comfy_entrypoint)
+ assert (ROOT / entry.WEB_DIRECTORY).is_dir()
+ assert callable(cli.main) and callable(comfy_launcher.main)
+ assert callable(modern_launcher.main)
+ assert (ROOT / 'freevideo_engine/launcher/Main.qml').is_file()
+
+ workflow = json.loads((ROOT / 'example_workflows' / comfy_launcher_runtime.TEMPLATE).read_text(encoding='utf-8'))
+ assert {node['type'] for node in workflow['nodes']} == {'FreeVideoMedia', 'FreeVideoGenerate'}
+ with tempfile.TemporaryDirectory(prefix='FreeVideo install 中文 ') as folder:
+ root = Path(folder)
+ source = materialize_source(ROOT, root / 'launcher')
+ payload = check_launcher_payload(source, root)
+ assert all((source / path.relative_to(ROOT)).read_bytes() == path.read_bytes()
+ for path in source_files(ROOT))
+ assert not any((source / name).exists() for name in ('benchmarks', 'docs', 'tests', 'AGENTS.md'))
+ for name in ('rewriter.py', 'rewriter_setup.py', 'rewriter_models.json'):
+ assert not (source / 'freevideo_engine' / name).exists(), 'Retired feature in package: ' + name
+ assert not (source / 'web/rewriter.js').exists(), 'Retired prompt model panel in package'
+ from freevideo_engine import support_report
+ report = dict(success=False, prompt='PRIVATE PROMPT',
+ error_message='PRIVATE PROMPT', token='PRIVATE TOKEN',
+ resources=dict(ram=dict(process_tree_peak_guard_bytes=123)))
+ output = root / 'video.mp4'
+ report_file = support_report.write(output, report)
+ assert report_file and report_file.name == 'video.debug.json'
+ saved = report_file.read_text(encoding='utf-8')
+ assert 'PRIVATE' not in saved
+ assert json.loads(saved)['memory']['video']['ram']['process_tree_peak_guard_bytes'] == 123
+ print(json.dumps(dict(success=True, resources=len(payload['resources']),
+ checks=['CLI and launcher imports', 'ComfyUI entry and workflow',
+ 'durable launcher source', 'packaged installation manifests']), indent=2))
+
+
+if __name__ == '__main__':
+ main()
diff --git a/scripts/scan_windows_release.py b/scripts/scan_windows_release.py
new file mode 100644
index 0000000..e9815d9
--- /dev/null
+++ b/scripts/scan_windows_release.py
@@ -0,0 +1,350 @@
+"""Check final release files on the Windows build runner, never during installation.
+
+MpCmdRun custom scans with DisableRemediation ignore file exclusions, inspect
+archives, and retain detected files for investigation. A missing scanner, failed
+update, timeout or changed artifact is an error, never a clean scan.
+Explicit launch verification also enables real-time/cloud protection on the
+disposable runner and checks Internet-zone downloads with Attachment Services.
+"""
+import argparse
+import hashlib
+import json
+import os
+from pathlib import Path
+import platform
+import shutil
+import subprocess
+import sys
+import tempfile
+import time
+import zipfile
+
+
+def digest(path):
+ value = hashlib.sha256()
+ with path.open('rb') as stream:
+ for chunk in iter(lambda: stream.read(1024 * 1024), b''):
+ value.update(chunk)
+ return value.hexdigest()
+
+
+def scanner():
+ platform = Path(os.environ.get('ProgramData', r'C:\ProgramData')) / 'Microsoft/Windows Defender/Platform'
+ candidates = sorted(platform.glob('*/MpCmdRun.exe'), reverse=True)
+ candidates.append(Path(os.environ.get('ProgramFiles', r'C:\Program Files')) / 'Windows Defender/MpCmdRun.exe')
+ return next((path for path in candidates if path.is_file()), None)
+
+
+def command(args, log):
+ with log.open('w', encoding='utf-8') as output:
+ result = subprocess.run([str(a) for a in args], stdout=output, stderr=subprocess.STDOUT, timeout=600)
+ return result.returncode
+
+
+def powershell(script):
+ powershell = Path(os.environ.get('SystemRoot', r'C:\Windows')) / 'System32/WindowsPowerShell/v1.0/powershell.exe'
+ result = subprocess.run([str(powershell), '-NoProfile', '-NonInteractive', '-Command', script],
+ capture_output=True, text=True, encoding='utf-8', errors='replace', timeout=90, check=True)
+ return json.loads(result.stdout.lstrip('\ufeff'))
+
+
+def metadata():
+ return powershell("""
+$ErrorActionPreference = 'Stop'
+[Console]::OutputEncoding = [Text.UTF8Encoding]::new()
+$s = Get-MpComputerStatus
+$p = Get-MpPreference
+[ordered]@{ engine = $s.AMEngineVersion; product = $s.AMProductVersion;
+ signatures = $s.AntivirusSignatureVersion; signature_age_days = $s.AntivirusSignatureAge;
+ service_enabled = $s.AMServiceEnabled; antivirus_enabled = $s.AntivirusEnabled;
+ realtime_enabled = $s.RealTimeProtectionEnabled;
+ behavior_enabled = $s.BehaviorMonitorEnabled; ioav_enabled = $s.IoavProtectionEnabled;
+ maps_reporting = [int]$p.MAPSReporting; sample_consent = [int]$p.SubmitSamplesConsent;
+ block_at_first_sight = -not $p.DisableBlockAtFirstSeen;
+ archive_scanning = -not $p.DisableArchiveScanning; script_scanning = -not $p.DisableScriptScanning;
+ excluded_paths = @($p.ExclusionPath | Where-Object { $_ });
+ excluded_extensions = @($p.ExclusionExtension | Where-Object { $_ });
+ excluded_processes = @($p.ExclusionProcess | Where-Object { $_ });
+ signature_updated = $s.AntivirusSignatureLastUpdated.ToUniversalTime().ToString('o') } | ConvertTo-Json -Compress
+""")
+
+
+def enable_realtime():
+ """Only the explicit release-test mode changes this disposable CI machine."""
+ info = metadata()
+ try:
+ require_realtime(info)
+ return info
+ except RuntimeError:
+ pass
+ if os.environ.get('GITHUB_ACTIONS') != 'true':
+ raise RuntimeError('Configure real-time/cloud protection and remove test exclusions before validation; '
+ 'automatic Defender configuration is limited to disposable Actions runners')
+ powershell("""
+$ErrorActionPreference = 'Stop'
+# GitHub's Windows image excludes C:\\ and D:\\ and disables archive/script
+# scans. Remove its performance exclusions to test normal download protection.
+$p = Get-MpPreference
+if ($p.ExclusionPath) { Remove-MpPreference -ExclusionPath $p.ExclusionPath }
+if ($p.ExclusionExtension) { Remove-MpPreference -ExclusionExtension $p.ExclusionExtension }
+if ($p.ExclusionProcess) { Remove-MpPreference -ExclusionProcess $p.ExclusionProcess }
+Set-MpPreference -DisableArchiveScanning $false -DisableScriptScanning $false
+Set-MpPreference -DisableRealtimeMonitoring $false -DisableBehaviorMonitoring $false -DisableIOAVProtection $false
+Set-MpPreference -MAPSReporting Advanced -SubmitSamplesConsent SendSafeSamples -DisableBlockAtFirstSeen $false
+ConvertTo-Json $true -Compress
+""")
+ deadline = time.monotonic() + 30
+ while True:
+ info = metadata()
+ try:
+ require_realtime(info)
+ return info
+ except RuntimeError:
+ if time.monotonic() >= deadline:
+ raise
+ time.sleep(1)
+
+
+def require_realtime(info):
+ fields = ('service_enabled', 'antivirus_enabled', 'realtime_enabled',
+ 'behavior_enabled', 'ioav_enabled', 'block_at_first_sight', 'archive_scanning', 'script_scanning')
+ if (any(info.get(key) is not True for key in fields) or info.get('maps_reporting') != 2
+ or info.get('sample_consent') not in (1, 3)
+ or any(info.get(key) != [] for key in ('excluded_paths', 'excluded_extensions', 'excluded_processes'))):
+ raise RuntimeError('Real-time/cloud Defender protection is unavailable; release validation is inconclusive')
+
+
+def threat_history():
+ return powershell("""
+$ErrorActionPreference = 'Stop'
+[Console]::OutputEncoding = [Text.UTF8Encoding]::new()
+$names = @{}
+Get-MpThreat | ForEach-Object { $names[[string]$_.ThreatID] = $_.ThreatName }
+$rows = @(Get-MpThreatDetection | ForEach-Object {
+ [ordered]@{ id = $_.DetectionID; threat_id = $_.ThreatID; name = $names[[string]$_.ThreatID];
+ detected_at = $_.InitialDetectionTime.ToUniversalTime().ToString('o');
+ resources = @($_.Resources); action_success = $_.ActionSuccess }
+})
+ConvertTo-Json -InputObject $rows -Depth 4 -Compress
+""")
+
+
+def mark_download(path):
+ # Preserve the Internet-zone evidence a browser supplies. Never unblock it.
+ url = 'https://github.com/FlashML-org/FreeVideo/releases/download/windows-preview/'
+ Path(str(path) + ':Zone.Identifier').write_text(
+ '[ZoneTransfer]\r\nZoneId=3\r\nHostUrl=' + url + path.name + '\r\n', encoding='utf-8')
+
+
+def attachment_check(path):
+ """Use Windows Attachment Services, as browsers do when saving a download.
+
+ IAttachmentExecute::Save can quarantine/delete the file. Any rejection is
+ retained, never bypassed by changing the file or its Internet-zone marker.
+ The COM method order is defined in the Windows SDK's shobjidl_core.h.
+ """
+ import ctypes as C
+ import uuid
+ guid = lambda value: (C.c_ubyte * 16).from_buffer_copy(uuid.UUID(value).bytes_le)
+ ole = C.WinDLL('ole32')
+ ole.CoInitializeEx.argtypes, ole.CoInitializeEx.restype = [C.c_void_p, C.c_uint32], C.c_long
+ ole.CoUninitialize.argtypes, ole.CoUninitialize.restype = [], None
+ ole.CoCreateInstance.argtypes = [C.c_void_p, C.c_void_p, C.c_uint32, C.c_void_p, C.POINTER(C.c_void_p)]
+ ole.CoCreateInstance.restype = C.c_long
+ def checked(result, operation):
+ if result < 0:
+ raise RuntimeError('%s rejected %s (HRESULT 0x%08x)' % (operation, path.name, result & 0xffffffff))
+ checked(ole.CoInitializeEx(None, 2), 'COM initialization')
+ obj = C.c_void_p()
+ release = None
+ try:
+ checked(ole.CoCreateInstance(guid('4125dd96-e03a-4103-8f70-e0597d803b9c'), None, 1,
+ guid('73db1241-1e85-4581-8e4f-a81e1d0f8c57'), C.byref(obj)), 'Attachment Services')
+ table = C.cast(obj, C.POINTER(C.POINTER(C.c_void_p))).contents
+ release = C.WINFUNCTYPE(C.c_ulong, C.c_void_p)(table[2])
+ for index, value in ((3, 'FreeVideo release validation'), (5, str(path.resolve())),
+ (7, 'https://github.com/FlashML-org/FreeVideo/releases/download/windows-preview/' + path.name)):
+ call = C.WINFUNCTYPE(C.c_long, C.c_void_p, C.c_wchar_p)(table[index])
+ checked(call(obj, value), 'Attachment metadata')
+ save = C.WINFUNCTYPE(C.c_long, C.c_void_p)(table[11])
+ checked(save(obj), 'IAttachmentExecute::Save')
+ finally:
+ if release is not None:
+ release(obj)
+ ole.CoUninitialize()
+
+
+def unpack_folder(archive, destination):
+ from pathlib import PureWindowsPath
+ with zipfile.ZipFile(archive) as stream:
+ for member in stream.infolist():
+ path = PureWindowsPath(member.filename)
+ if path.drive or path.root or '..' in path.parts or (member.external_attr >> 16) & 0o170000 == 0o120000:
+ raise ValueError('Unsafe path in launcher ZIP')
+ stream.extractall(destination)
+ executable = destination / 'FreeVideo/FreeVideo.exe'
+ if not executable.is_file():
+ raise ValueError('Launcher ZIP does not contain FreeVideo/FreeVideo.exe')
+ return executable
+
+
+def verify_launches(onefile, folder_zip, program, report, logs, *, blocked_layouts=()):
+ """Test final bytes outside build exclusions, with download zone and live AV."""
+ downloads = Path(os.environ['USERPROFILE']) / 'Downloads'
+ downloads.mkdir(exist_ok=True)
+ root = Path(tempfile.mkdtemp(prefix='FreeVideo-release-check-', dir=downloads))
+ report['launch_checks'] = dict(directory=str(root), internet_zone=3, attachments=[], cases=[])
+ excluded = command([program, '-CheckExclusion', '-Path', root], logs/'defender-exclusions.log')
+ report['launch_checks']['exclusion_exit_code'] = excluded
+ if excluded != 1:
+ raise RuntimeError('Download test directory is excluded or its protection could not be checked')
+ for label, source, target in (('onefile', onefile, root/'FreeVideo.exe'),
+ ('folder', folder_zip, root/'FreeVideo-Windows-folder.zip')):
+ row = dict(layout=label, status='pending')
+ report['launch_checks']['cases'].append(row)
+ if label in blocked_layouts:
+ row.update(status='blocked', error='Static scan rejected this package; it was not opened')
+ continue
+ # A rejection stays a failure. The other independently built layout
+ # is still checked so a onefile detection cannot hide the ZIP result.
+ attachment = None
+ try:
+ shutil.copyfile(source, target)
+ mark_download(target)
+ if digest(source) != digest(target):
+ raise RuntimeError('Downloaded test package differs from release bytes')
+ attachment = dict(name=target.name, sha256=digest(target), status='pending')
+ report['launch_checks']['attachments'].append(attachment)
+ attachment_check(target)
+ if not target.is_file() or digest(target) != attachment['sha256']:
+ raise RuntimeError('Attachment security removed or changed ' + target.name)
+ attachment['status'] = 'passed'
+ executable = target
+ if label == 'folder':
+ executable = unpack_folder(target, root)
+ for path in (root/'FreeVideo').rglob('*'):
+ if path.is_file():
+ mark_download(path)
+ row['sha256'] = digest(executable)
+ out = root/('smoke-'+label)
+ environment = dict(os.environ, FREEVIDEO_PYTHON=sys.executable)
+ started = time.monotonic()
+ result = subprocess.run([str(executable), '--smoke-test', str(out)], env=environment, timeout=300)
+ row.update(exit_code=result.returncode, seconds=time.monotonic()-started)
+ payload = json.loads((out/'smoke.json').read_text(encoding='utf-8'))
+ reopened = payload.get('payload', {}).get('reopen', {})
+ if (result.returncode != 0 or payload.get('success') is not True or payload.get('model_modules_imported')
+ or not reopened.get('installed_history') or not reopened.get('process_inspection')):
+ row['smoke'] = payload
+ raise RuntimeError('Downloaded launcher failed its startup/resource check')
+ if not executable.is_file() or digest(executable) != row['sha256']:
+ raise RuntimeError('Defender removed or changed the launcher during startup')
+ row['status'] = 'passed'
+ except Exception as error:
+ row.update(status='blocked', error=str(error))
+ if attachment is not None and attachment['status'] == 'pending':
+ attachment.update(status='blocked', error=str(error))
+ failures = [row['layout'] + ': ' + row['error'] for row in report['launch_checks']['cases'] if row['status'] != 'passed']
+ if failures:
+ raise RuntimeError('; '.join(failures))
+ report['defender_after_launch'] = metadata()
+ require_realtime(report['defender_after_launch'])
+
+
+def scan(files, report_path, *, launches=None):
+ report_path = Path(report_path)
+ report_path.parent.mkdir(parents=True, exist_ok=True)
+ report = dict(schema=1, status='error', started_at=time.time(), files=[],
+ scope='Microsoft Defender custom file/archive scan; not a SmartScreen or malware-free certification')
+ try:
+ if platform.system() != 'Windows':
+ raise RuntimeError('Release antivirus scanning requires native Windows')
+ program = scanner()
+ if program is None:
+ raise RuntimeError('Microsoft Defender scanner is unavailable; scan inconclusive')
+ report['scanner'] = str(program)
+ if launches:
+ report['detections_before'] = threat_history()
+ report['defender_before'] = metadata()
+ report['defender_prepared'] = enable_realtime()
+ report['scope'] += '; Internet-zone files and extracted ZIP launched with real-time/cloud protection'
+ if not files:
+ raise ValueError('No release files selected')
+ for path in map(Path, files):
+ if not path.is_file() or path.is_symlink():
+ raise ValueError('Release file is missing or linked: ' + str(path))
+ report['files'].append(dict(name=path.name, path=str(path.resolve()), bytes=path.stat().st_size,
+ sha256=digest(path), status='pending'))
+ update_log = report_path.parent/'defender-update.log'
+ report['update_log'] = update_log.name
+ report['update_exit_code'] = command([program, '-SignatureUpdate'], update_log)
+ if report['update_exit_code'] != 0:
+ raise RuntimeError('Defender signature update failed; see ' + str(update_log))
+ report['defender'] = metadata()
+ info = report['defender']
+ if (not info.get('signatures') or type(info.get('signature_age_days')) is not int
+ or not 0 <= info['signature_age_days'] <= 2 or not info.get('service_enabled')):
+ raise RuntimeError('Defender service or current signatures are unavailable; scan inconclusive')
+ if launches:
+ require_realtime(info)
+ report['maps_exit_code'] = command([program, '-ValidateMapsConnection'], report_path.parent/'defender-maps.log')
+ if report['maps_exit_code'] != 0:
+ raise RuntimeError('Defender cloud connection unavailable; release validation is inconclusive')
+ for index, row in enumerate(report['files']):
+ path = Path(row['path'])
+ log = report_path.parent/('defender-scan-%d.log' % index)
+ row['log'] = log.name
+ row['exit_code'] = command([program, '-Scan', '-ScanType', '3', '-File', path, '-DisableRemediation'], log)
+ unchanged = path.is_file() and path.stat().st_size == row['bytes'] and digest(path) == row['sha256']
+ row['status'] = 'clean' if row['exit_code'] == 0 and unchanged else 'blocked'
+ blocked = [row for row in report['files'] if row['status'] != 'clean']
+ if launches:
+ blocked_layouts = {'onefile' if Path(row['path']) == Path(launches[0]).resolve() else 'folder'
+ for row in blocked}
+ verify_launches(*map(Path, launches), program, report, report_path.parent, blocked_layouts=blocked_layouts)
+ report['detections'] = threat_history()
+ previous = {row['id'] for row in report['detections_before']}
+ report['new_detections'] = [row for row in report['detections'] if row['id'] not in previous]
+ if report['new_detections']:
+ raise RuntimeError('Defender reported a new threat during release validation; publication blocked')
+ for row in report['files']:
+ path = Path(row['path'])
+ if not path.is_file() or digest(path) != row['sha256']:
+ raise RuntimeError('A final release file was removed or changed during live protection checks')
+ if blocked:
+ raise RuntimeError('Defender detected a threat, could not scan, or the release changed. See retained scan logs.')
+ report['status'] = 'clean'
+ return report
+ except Exception as error:
+ report['error'] = str(error)
+ raise
+ finally:
+ if launches and platform.system() == 'Windows':
+ try:
+ if 'detections' not in report:
+ report['detections'] = threat_history()
+ report['defender_final'] = metadata()
+ if report['detections']:
+ # Preserve evidence even when a deleted file prevented startup.
+ report['scope'] += '; detections include this disposable runner history'
+ except Exception as error:
+ report['detection_history_error'] = str(error)
+ report['finished_at'] = time.time()
+ temporary = report_path.with_suffix('.tmp')
+ temporary.write_text(json.dumps(report, indent=2), encoding='utf-8')
+ temporary.replace(report_path)
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--file', type=Path, action='append', required=True)
+ parser.add_argument('--report', type=Path, required=True)
+ parser.add_argument('--verify-launches', nargs=2, type=Path, metavar=('EXE', 'FOLDER_ZIP'),
+ help='Enable real-time/cloud Defender on this test machine and open Internet-zone copies')
+ args = parser.parse_args()
+ result = scan(args.file, args.report, launches=args.verify_launches)
+ print('Defender scan completed for %d release files. Report: %s' % (len(result['files']), args.report))
+
+
+if __name__ == '__main__':
+ main()
diff --git a/scripts/sign_windows_release.ps1 b/scripts/sign_windows_release.ps1
new file mode 100644
index 0000000..c402b01
--- /dev/null
+++ b/scripts/sign_windows_release.ps1
@@ -0,0 +1,59 @@
+param(
+ [Parameter(Mandatory = $true)][string]$OneFile,
+ [Parameter(Mandatory = $true)][string]$FolderZip,
+ [Parameter(Mandatory = $true)][string]$FolderExecutable
+)
+
+$ErrorActionPreference = 'Stop'
+
+# Signing is deliberately opt-in. A release must never claim to be signed when
+# the maintainer has not configured a certificate in the repository secrets.
+$encoded = [Environment]::GetEnvironmentVariable('WINDOWS_CODESIGN_PFX_B64')
+if ([string]::IsNullOrWhiteSpace($encoded)) {
+ Write-Host 'Authenticode signing is not configured; retaining unsigned build.'
+ exit 0
+}
+$password = [Environment]::GetEnvironmentVariable('WINDOWS_CODESIGN_PFX_PASSWORD')
+if ($null -eq $password) { $password = '' }
+$timestamp = [Environment]::GetEnvironmentVariable('WINDOWS_CODESIGN_TIMESTAMP_URL')
+if ([string]::IsNullOrWhiteSpace($timestamp)) { $timestamp = 'http://timestamp.digicert.com' }
+
+$kits = Join-Path ${env:ProgramFiles(x86)} 'Windows Kits\10\bin'
+$signtool = Get-ChildItem -Path $kits -Filter signtool.exe -File -Recurse -ErrorAction SilentlyContinue |
+ Where-Object { $_.FullName -match '\\x64\\signtool\.exe$' } |
+ Sort-Object FullName -Descending | Select-Object -First 1
+if ($null -eq $signtool) {
+ $signtool = Get-ChildItem -Path $kits -Filter signtool.exe -File -Recurse -ErrorAction SilentlyContinue |
+ Sort-Object FullName -Descending | Select-Object -First 1
+}
+if ($null -eq $signtool) { throw 'Authenticode signing is configured, but signtool.exe is unavailable on the runner.' }
+
+$pfx = Join-Path $env:RUNNER_TEMP 'freevideo-signing.pfx'
+[IO.File]::WriteAllBytes($pfx, [Convert]::FromBase64String($encoded))
+
+function Sign-Executable([string]$Path) {
+ if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { throw "Executable is missing: $Path" }
+ $signArgs = @('sign', '/fd', 'SHA256', '/td', 'SHA256', '/tr', $timestamp,
+ '/f', $pfx, '/p', $password, '/d', 'FreeVideo', '/du', 'https://www.flashml.ai/', $Path)
+ & $signtool.FullName @signArgs
+ if ($LASTEXITCODE -ne 0) { throw "signtool failed for $Path (exit $LASTEXITCODE)" }
+ & $signtool.FullName verify /pa /all $Path
+ if ($LASTEXITCODE -ne 0) { throw "signtool verification failed for $Path (exit $LASTEXITCODE)" }
+}
+
+Sign-Executable $OneFile
+Sign-Executable $FolderExecutable
+
+# The ZIP itself cannot carry an Authenticode signature. Sign the executable
+# inside it, then recreate the archive before hashes, scanning, and publishing.
+$unpacked = Join-Path $env:RUNNER_TEMP 'freevideo-signed-folder'
+Remove-Item -LiteralPath $unpacked -Recurse -Force -ErrorAction SilentlyContinue
+Expand-Archive -LiteralPath $FolderZip -DestinationPath $unpacked -Force
+$folderExe = Join-Path $unpacked 'FreeVideo\FreeVideo.exe'
+Sign-Executable $folderExe
+$folderHash = (Get-FileHash -LiteralPath $folderExe -Algorithm SHA256).Hash.ToLowerInvariant()
+Set-Content -LiteralPath (Join-Path $unpacked 'FreeVideo\SHA256SUMS.txt') `
+ -Value ($folderHash + ' FreeVideo.exe') -Encoding ascii
+Remove-Item -LiteralPath $FolderZip -Force
+Compress-Archive -Path (Join-Path $unpacked 'FreeVideo') -DestinationPath $FolderZip -CompressionLevel Optimal
+Write-Host 'Authenticode signatures applied to the one-file launcher and folder launcher.'
diff --git a/scripts/test_freevideo_entry.mjs b/scripts/test_freevideo_entry.mjs
new file mode 100644
index 0000000..e924d87
--- /dev/null
+++ b/scripts/test_freevideo_entry.mjs
@@ -0,0 +1,148 @@
+import assert from 'node:assert/strict';
+import {readFile} from 'node:fs/promises';
+import test from 'node:test';
+
+class Element {
+ constructor(tag) {
+ this.tag = tag; this.children = []; this.dataset = {}; this.attributes = {};
+ this.classList = {toggle() {}}; this.style = {};
+ }
+ append(...children) { this.children.push(...children); }
+ prepend(...children) { this.children.unshift(...children); }
+ replaceChildren(...children) { this.children = [...children]; }
+ setAttribute(name, value) { this.attributes[name] = value; }
+ querySelector() { return null; }
+ querySelectorAll() { return []; }
+ get childElementCount() { return this.children.length; }
+ click() { this.clicked = true; }
+}
+
+test('main browser entry registers both views and preserves node hooks', async () => {
+ const extensions = [], opened = [], installed = [], events = new EventTarget();
+ let refreshed = 0, compatibilityChecks = 0;
+ const app = {
+ registerExtension(extension) { extensions.push(extension); },
+ graph: {extra: {freevideo_studio: true}, _nodes: [], getNodeById() {}},
+ };
+ const api = Object.assign(events, {apiURL: value => value});
+ const document = {head: new Element('head'), createElement: tag => new Element(tag)};
+ const dependencies = {
+ app, api,
+ createErrorPanel: () => ({element: new Element('error'), show() {}, clear() {}}),
+ errorText: value => String(value),
+ openStudio: node => opened.push(node),
+ loraPanel: () => () => {}, loraWarning: () => 'LoRA warning', promptGuide: () => new Element('guide'),
+ createGenerationProgress: () => ({element: new Element('progress'), update() {}, hide() {}, dispose() {}}),
+ createProgressConnection: () => ({start() {}, refresh() {}, reset() {}}),
+ notifyCompatibility: async () => { compatibilityChecks++; },
+ installNavigation: callback => installed.push(callback),
+ refreshNavigation: () => refreshed++, preferredView: () => null,
+ };
+ const previous = new Map();
+ for (const [name, value] of Object.entries({
+ document, navigator: {language: 'en'}, window: events,
+ CustomEvent: class extends Event { constructor(name, options) { super(name); this.detail = options?.detail; } },
+ requestAnimationFrame: callback => callback(), __freevideoEntryTest: dependencies,
+ })) {
+ previous.set(name, Object.getOwnPropertyDescriptor(globalThis, name));
+ Object.defineProperty(globalThis, name, {value, configurable: true});
+ }
+ try {
+ let source = await readFile(new URL('../web/freevideo.js', import.meta.url), 'utf8');
+ source = source
+ .replace("import { createErrorPanel, errorText } from './error_panel.js';", 'const {createErrorPanel,errorText} = globalThis.__freevideoEntryTest;')
+ .replace('import { app } from "../../scripts/app.js";', 'const {app} = globalThis.__freevideoEntryTest;')
+ .replace('import { api } from "../../scripts/api.js";', 'const {api} = globalThis.__freevideoEntryTest;')
+ .replace("import { openStudio, loraPanel, loraWarning, promptGuide } from \"./studio.js\";", 'const {openStudio,loraPanel,loraWarning,promptGuide} = globalThis.__freevideoEntryTest;')
+ .replace("import { createGenerationProgress } from './generation_progress.js';", 'const {createGenerationProgress} = globalThis.__freevideoEntryTest;')
+ .replace("import { createProgressConnection } from './progress_connection.js';", 'const {createProgressConnection} = globalThis.__freevideoEntryTest;')
+ .replace("import { notifyCompatibility } from './compatibility.js';", 'const {notifyCompatibility} = globalThis.__freevideoEntryTest;')
+ .replace("import { installNavigation, refreshNavigation, preferredView } from './view_navigation.js';", 'const {installNavigation,refreshNavigation,preferredView} = globalThis.__freevideoEntryTest;');
+ await import('data:text/javascript;base64,' + Buffer.from(source).toString('base64'));
+ const extension = extensions.find(row => row.name === 'FreeVideo.UnifiedMedia');
+ assert.ok(extension, 'the main extension must register');
+ await extension.setup();
+ assert.equal(installed.length, 1, 'the Create / Nodes navigation must be installed');
+ assert.equal(compatibilityChecks, 1);
+
+ class GenerateNode {
+ constructor() {
+ this.id = 7; this.type = 'FreeVideoGenerate';
+ this.widgets = [];
+ this.inputs = []; this.size = [400, 300]; this.dom = []; this.buttons = [];
+ this.graph = app.graph;
+ }
+ onNodeCreated() { this.created = (this.created || 0) + 1; return 'created'; }
+ onConfigure() { this.configured = (this.configured || 0) + 1; return 'configured'; }
+ onConnectionsChange() { this.connections = (this.connections || 0) + 1; }
+ addDOMWidget(name, type, element) { this.dom.push({name, type, element}); }
+ addWidget(type, name, value, callback) { this.buttons.push({type, name, value, callback}); }
+ setSize(size) { this.size = size; }
+ computeSize() { return this.size; }
+ }
+ await extension.beforeRegisterNodeDef(GenerateNode, {name: 'FreeVideoGenerate'});
+ const node = new GenerateNode(); app.graph._nodes = [node];
+ assert.equal(node.onNodeCreated(), 'created');
+ assert.equal(node.created, 1);
+ assert.deepEqual(node.dom.map(row => row.name),
+ ['freevideo_prompt_guide', 'freevideo_result']);
+ assert.equal(node.buttons[0].name, 'Open creative workspace');
+ assert.equal(node.onConfigure(), 'configured');
+ assert.equal(node.configured, 1, 'the original ComfyUI node hook must run exactly once');
+ node.buttons[0].callback();
+ extension.afterConfigureGraph();
+ assert.deepEqual(opened, [node, node]);
+ assert.ok(refreshed >= 2);
+
+ class MediaNode extends GenerateNode {
+ constructor() {
+ super(); this.id = 8; this.type = 'FreeVideoMedia';
+ this.widgets = [{name: 'assets', value: '[]'}];
+ this.inputs = [{name: 'reference_audio', link: 1}];
+ this.graph = {links: {1: {origin_id: 9}}, getNodeById: () => ({title: 'Load Audio'}), change() {}};
+ }
+ }
+ await extension.beforeRegisterNodeDef(MediaNode, {name: 'FreeVideoMedia'});
+ const media = new MediaNode(); media.onNodeCreated();
+ const panel = media.dom.find(row => row.name === 'freevideo_media').element;
+ const [toolbar, mode, note, audioHelp, connections] = panel.children;
+ assert.match(mode.textContent, /Reference/);
+ assert.equal(audioHelp.hidden, false);
+ assert.match(audioHelp.textContent, /<Audio 1>/);
+ assert.match(connections.children[0].textContent, /Reference audio.*Connected/);
+ const audioButton = toolbar.children.find(e => e.tag === 'button' && e.textContent === 'Reference audio');
+ const audioPicker = toolbar.children.find(e => e.tag === 'input' && e.accept.startsWith('audio/'));
+ audioButton.onclick(); assert.equal(audioPicker.clicked, true);
+ api.fetchApi = async (url, options) => {
+ assert.equal(url, '/freevideo/media/upload');
+ assert.equal(options.body.get('file').name, 'voice.wav');
+ return {ok: true, json: async () => ({file: 'voice.wav'})};
+ };
+ audioPicker.files = [new File(['wave'], 'voice.wav', {type: 'audio/wav'})];
+ await audioPicker.onchange();
+ assert.deepEqual(JSON.parse(media.widgets[0].value), [{file: 'voice.wav', role: 'reference', enabled: true}]);
+ assert.equal(media.isUploading, false);
+ media.inputs = [{name: 'first', link: 1}]; media.onConnectionsChange();
+ assert.match(note.textContent, /Choose keyframes or references/);
+ media.widgets[0].value = '[]'; media.inputs = []; media.onConnectionsChange();
+ assert.equal(audioHelp.hidden, true);
+ assert.match(mode.textContent, /Text to video/);
+ const imageRole = toolbar.children.find(e => e.tag === 'select');
+ const imagePicker = toolbar.children.find(e => e.tag === 'input' && e.accept.startsWith('image/'));
+ assert.equal(imageRole.value, 'reference');
+ api.fetchApi = async () => ({ok: true, json: async () => ({file: 'image.png'})});
+ imagePicker.files = [new File(['image'], 'image.png', {type: 'image/png'})];
+ await imagePicker.onchange();
+ assert.equal(JSON.parse(media.widgets[0].value)[0].role, 'reference');
+ imageRole.value = 'first';
+ imagePicker.files = [new File(['image'], 'image.png', {type: 'image/png'})];
+ await imagePicker.onchange();
+ assert.deepEqual(JSON.parse(media.widgets[0].value).map(row => row.role), ['reference', 'first']);
+ media.onRemoved();
+ } finally {
+ for (const [name, descriptor] of previous) {
+ if (descriptor) Object.defineProperty(globalThis, name, descriptor);
+ else delete globalThis[name];
+ }
+ }
+});
diff --git a/scripts/test_prompt_guide.mjs b/scripts/test_prompt_guide.mjs
new file mode 100644
index 0000000..aa40a3f
--- /dev/null
+++ b/scripts/test_prompt_guide.mjs
@@ -0,0 +1,28 @@
+import assert from 'node:assert/strict';
+import {readFile} from 'node:fs/promises';
+import test from 'node:test';
+
+// Evaluate the actual shared guide used in both views without ComfyUI or a GPU.
+const source = await readFile(new URL('../web/studio.js', import.meta.url), 'utf8');
+const guide = source.slice(source.indexOf('export function promptGuide()'), source.indexOf('export function loraPanel('));
+for (const language of ['en', 'zh']) {
+ test(`prompt guide recommends external AI in ${language}`, () => {
+ const el = (tag, text, cls) => ({tag, text, cls, children: [], append(...items) { this.children.push(...items); }});
+ const document = {createTextNode: text => ({text})};
+ const t = (en, zh) => language === 'zh' ? zh : en;
+ const render = new Function('el', 'document', 't', guide.replace('export ', '') + '\nreturn promptGuide();');
+ const result = render(el, document, t);
+ const links = result.children.filter(child => child.tag === 'a');
+ assert.equal(links.length, 2);
+ assert.ok(links[0].href.endsWith('/skills/h3-prompt-writing/SKILL.md'));
+ assert.equal(links[1].href, 'https://www.flashml.ai/');
+ for (const link of links) {
+ assert.equal(link.target, '_blank');
+ assert.equal(link.rel, 'noopener noreferrer');
+ }
+ const text = result.children.map(child => child.text).join('');
+ assert.ok(text.includes(language === 'zh' ? '让AI参考' : 'ask AI to rewrite'));
+ assert.ok(text.includes('FreeToken'));
+ assert.equal(result.children.some(child => child.tag === 'button' || child.tag === 'input'), false);
+ });
+}
diff --git a/scripts/windows_app.py b/scripts/windows_app.py
new file mode 100644
index 0000000..f86ea7c
--- /dev/null
+++ b/scripts/windows_app.py
@@ -0,0 +1,5 @@
+"""PyInstaller entry point; model modules stay out of the GUI executable."""
+from freevideo_engine.modern_launcher import main
+
+if __name__ == '__main__':
+ main()
diff --git a/setup.cmd b/setup.cmd
new file mode 100644
index 0000000..5ccae57
--- /dev/null
+++ b/setup.cmd
@@ -0,0 +1,3 @@
+@echo off
+powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -File "%~dp0setup.ps1" %*
+exit /b %ERRORLEVEL%
diff --git a/setup.ps1 b/setup.ps1
new file mode 100644
index 0000000..41d8699
--- /dev/null
+++ b/setup.ps1
@@ -0,0 +1,2 @@
+& (Join-Path $PSScriptRoot 'freevideo.ps1') setup @args
+exit $LASTEXITCODE
diff --git a/setup.sh b/setup.sh
new file mode 100755
index 0000000..2b36a1f
--- /dev/null
+++ b/setup.sh
@@ -0,0 +1,8 @@
+#!/usr/bin/env bash
+set -euo pipefail
+FREEVIDEO_SOURCE_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
+export PYTHONPATH="$FREEVIDEO_SOURCE_DIR${PYTHONPATH:+:$PYTHONPATH}"
+source "$FREEVIDEO_SOURCE_DIR/scripts/bootstrap_linux.sh"
+freevideo_bootstrap_linux setup "$@"
+(( FREEVIDEO_BOOTSTRAP_DONE == 0 )) || exit 0
+exec "$FREEVIDEO_BOOTSTRAP_PYTHON" -B -X utf8 -m freevideo_engine.bootstrap "${FREEVIDEO_BOOTSTRAP_ARGS[@]}"
diff --git a/test.cmd b/test.cmd
new file mode 100644
index 0000000..01bcb19
--- /dev/null
+++ b/test.cmd
@@ -0,0 +1,3 @@
+@echo off
+powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -File "%~dp0test.ps1" %*
+exit /b %ERRORLEVEL%
diff --git a/test.ps1 b/test.ps1
new file mode 100644
index 0000000..44c2931
--- /dev/null
+++ b/test.ps1
@@ -0,0 +1,2 @@
+& (Join-Path $PSScriptRoot 'freevideo.ps1') test @args
+exit $LASTEXITCODE
diff --git a/test.sh b/test.sh
new file mode 100755
index 0000000..4dec449
--- /dev/null
+++ b/test.sh
@@ -0,0 +1,8 @@
+#!/usr/bin/env bash
+set -euo pipefail
+FREEVIDEO_SOURCE_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
+export PYTHONPATH="$FREEVIDEO_SOURCE_DIR${PYTHONPATH:+:$PYTHONPATH}"
+source "$FREEVIDEO_SOURCE_DIR/scripts/bootstrap_linux.sh"
+freevideo_bootstrap_linux test "$@"
+(( FREEVIDEO_BOOTSTRAP_DONE == 0 )) || exit 0
+exec "$FREEVIDEO_BOOTSTRAP_PYTHON" -B -X utf8 -m freevideo_engine.test_launcher "${FREEVIDEO_BOOTSTRAP_ARGS[@]}"
diff --git a/web/assets/freevideo-light.svg b/web/assets/freevideo-light.svg
new file mode 100644
index 0000000..0a02462
--- /dev/null
+++ b/web/assets/freevideo-light.svg
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg"
+ width="2016" height="337" viewBox="0 0 2016 337"
+ role="img" aria-label="FreeVideo logo">
+ <title>FreeVideo</title>
+ <path d="M 20 235 L 15 241 L 13 251 L 13 257 L 16 262 L 20 264 L 37 264 L 41 262 L 45 257 L 47 247 L 46 238 L 42 234 L 38 233 L 25 233 Z M 33 131 L 29 147 L 30 152 L 33 156 L 38 158 L 58 158 L 65 155 L 68 152 L 71 140 L 71 132 L 67 128 L 62 126 L 42 126 Z M 1300 123 L 1236 122 L 1230 125 L 1227 129 L 1210 217 L 1206 231 L 1206 237 L 1190 313 L 1194 319 L 1259 320 L 1266 315 L 1303 139 L 1304 127 Z M 1737 129 L 1717 118 L 1705 114 L 1688 111 L 1650 111 L 1630 115 L 1612 121 L 1595 130 L 1578 143 L 1562 161 L 1553 176 L 1546 192 L 1539 217 L 1536 239 L 1537 259 L 1542 277 L 1552 294 L 1562 304 L 1571 310 L 1590 318 L 1619 323 L 1643 323 L 1668 320 L 1684 316 L 1705 307 L 1726 293 L 1738 281 L 1745 271 L 1748 264 L 1746 258 L 1742 256 L 1677 256 L 1670 259 L 1664 265 L 1653 272 L 1645 274 L 1634 274 L 1624 270 L 1617 262 L 1614 253 L 1614 239 L 1616 237 L 1622 236 L 1750 236 L 1755 233 L 1759 225 L 1763 203 L 1762 171 L 1758 157 L 1752 145 Z M 1685 174 L 1686 191 L 1683 197 L 1623 196 L 1625 188 L 1632 175 L 1643 164 L 1656 159 L 1666 159 L 1673 161 L 1680 166 Z M 1979 132 L 1956 118 L 1940 113 L 1920 110 L 1887 110 L 1861 114 L 1844 120 L 1823 131 L 1806 145 L 1796 157 L 1784 177 L 1775 203 L 1770 227 L 1769 253 L 1771 267 L 1779 286 L 1787 297 L 1801 308 L 1824 318 L 1853 323 L 1883 323 L 1905 320 L 1928 313 L 1946 304 L 1960 294 L 1974 280 L 1990 254 L 1996 237 L 2002 208 L 2003 180 L 2000 164 L 1993 148 Z M 1883 167 L 1901 178 L 1935 203 L 1939 208 L 1939 217 L 1935 223 L 1869 263 L 1865 265 L 1856 265 L 1852 263 L 1847 256 L 1847 246 L 1861 180 L 1870 169 L 1874 167 Z M 1553 48 L 1481 48 L 1477 52 L 1475 57 L 1462 126 L 1459 134 L 1445 121 L 1433 115 L 1416 111 L 1396 111 L 1379 114 L 1363 120 L 1347 130 L 1334 142 L 1321 159 L 1310 182 L 1303 205 L 1298 235 L 1298 263 L 1302 281 L 1309 295 L 1320 307 L 1330 314 L 1345 320 L 1373 323 L 1403 317 L 1420 307 L 1430 298 L 1431 301 L 1429 314 L 1431 318 L 1434 320 L 1496 320 L 1502 316 L 1505 308 L 1533 164 L 1556 58 L 1556 52 Z M 1431 171 L 1437 174 L 1444 181 L 1448 194 L 1448 203 L 1445 220 L 1440 236 L 1431 251 L 1423 258 L 1408 264 L 1398 264 L 1391 262 L 1382 255 L 1379 250 L 1377 241 L 1379 219 L 1386 194 L 1392 184 L 1405 173 L 1414 170 Z M 1236 48 L 1163 47 L 1155 53 L 1070 215 L 1057 54 L 1055 50 L 1050 47 L 977 47 L 972 50 L 970 54 L 970 61 L 979 114 L 1007 310 L 1011 318 L 1015 320 L 1089 320 L 1097 314 L 1157 203 L 1236 62 L 1239 55 L 1239 52 Z M 1264 46 L 1260 51 L 1252 73 L 1243 91 L 1244 100 L 1248 103 L 1253 104 L 1308 104 L 1314 101 L 1318 96 L 1328 52 L 1327 47 L 1323 43 L 1270 43 Z M 388 15 L 384 13 L 149 13 L 143 16 L 140 21 L 137 39 L 139 43 L 143 46 L 158 46 L 163 50 L 160 67 L 158 70 L 154 72 L 88 72 L 81 75 L 77 81 L 75 97 L 80 103 L 146 104 L 148 106 L 148 112 L 146 121 L 140 126 L 110 126 L 102 129 L 98 135 L 96 150 L 101 157 L 133 158 L 136 161 L 134 175 L 130 179 L 64 180 L 56 184 L 54 187 L 51 205 L 54 209 L 58 211 L 119 211 L 122 212 L 125 216 L 122 229 L 117 233 L 80 233 L 74 235 L 68 241 L 66 257 L 71 263 L 109 264 L 114 268 L 112 282 L 106 287 L 77 287 L 69 291 L 66 297 L 65 304 L 66 316 L 71 320 L 183 320 L 187 318 L 192 311 L 211 210 L 213 204 L 220 199 L 320 199 L 324 197 L 329 190 L 338 145 L 337 136 L 334 133 L 234 133 L 229 129 L 228 126 L 236 88 L 243 82 L 373 81 L 379 76 L 390 24 L 390 18 Z M 90 14 L 84 20 L 81 35 L 83 42 L 89 46 L 105 46 L 109 44 L 114 39 L 117 23 L 115 17 L 109 13 Z" fill="#1B94FC" fill-rule="evenodd"/>
+ <path d="M 535 122 L 530 120 L 507 120 L 489 123 L 473 129 L 461 136 L 450 145 L 449 142 L 452 131 L 452 125 L 448 121 L 387 120 L 382 122 L 377 127 L 373 149 L 338 303 L 338 316 L 343 320 L 407 320 L 411 318 L 416 311 L 435 220 L 441 205 L 451 193 L 460 187 L 468 184 L 486 182 L 517 183 L 524 179 L 527 174 L 537 131 L 537 125 Z M 941 135 L 927 126 L 907 119 L 890 116 L 863 116 L 843 119 L 824 125 L 815 129 L 794 143 L 779 158 L 771 169 L 761 187 L 752 213 L 748 237 L 748 255 L 753 277 L 762 293 L 777 307 L 792 315 L 808 320 L 829 323 L 852 323 L 882 318 L 902 311 L 928 296 L 943 282 L 952 268 L 951 259 L 947 257 L 886 257 L 877 261 L 865 272 L 852 276 L 843 276 L 833 272 L 827 266 L 823 256 L 823 240 L 825 238 L 954 238 L 960 235 L 964 228 L 967 209 L 966 178 L 963 166 L 956 152 Z M 893 177 L 894 198 L 892 200 L 831 199 L 832 192 L 839 178 L 850 167 L 857 163 L 865 161 L 873 161 L 880 163 L 889 170 Z M 716 134 L 703 126 L 683 119 L 663 116 L 640 116 L 620 119 L 596 127 L 583 134 L 565 147 L 551 162 L 539 180 L 530 199 L 521 232 L 520 256 L 522 268 L 529 286 L 537 297 L 555 311 L 572 318 L 592 322 L 622 323 L 647 320 L 668 314 L 689 304 L 704 294 L 718 280 L 725 269 L 726 262 L 721 257 L 659 257 L 653 259 L 641 270 L 633 274 L 625 276 L 615 276 L 604 271 L 599 265 L 596 257 L 595 245 L 596 240 L 598 238 L 727 238 L 734 235 L 737 231 L 741 213 L 741 177 L 737 162 L 733 154 L 726 144 Z M 667 176 L 669 189 L 668 197 L 666 200 L 605 199 L 607 190 L 615 176 L 621 170 L 632 163 L 639 161 L 648 161 L 655 163 L 665 172 Z" fill="#14171C" fill-rule="evenodd"/>
+</svg>
diff --git a/web/assets/freevideo.svg b/web/assets/freevideo.svg
new file mode 100644
index 0000000..12b2dfd
--- /dev/null
+++ b/web/assets/freevideo.svg
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg"
+ width="2016" height="337" viewBox="0 0 2016 337"
+ role="img" aria-label="FreeVideo logo">
+ <title>FreeVideo</title>
+ <path d="M 20 235 L 15 241 L 13 251 L 13 257 L 16 262 L 20 264 L 37 264 L 41 262 L 45 257 L 47 247 L 46 238 L 42 234 L 38 233 L 25 233 Z M 33 131 L 29 147 L 30 152 L 33 156 L 38 158 L 58 158 L 65 155 L 68 152 L 71 140 L 71 132 L 67 128 L 62 126 L 42 126 Z M 1300 123 L 1236 122 L 1230 125 L 1227 129 L 1210 217 L 1206 231 L 1206 237 L 1190 313 L 1194 319 L 1259 320 L 1266 315 L 1303 139 L 1304 127 Z M 1737 129 L 1717 118 L 1705 114 L 1688 111 L 1650 111 L 1630 115 L 1612 121 L 1595 130 L 1578 143 L 1562 161 L 1553 176 L 1546 192 L 1539 217 L 1536 239 L 1537 259 L 1542 277 L 1552 294 L 1562 304 L 1571 310 L 1590 318 L 1619 323 L 1643 323 L 1668 320 L 1684 316 L 1705 307 L 1726 293 L 1738 281 L 1745 271 L 1748 264 L 1746 258 L 1742 256 L 1677 256 L 1670 259 L 1664 265 L 1653 272 L 1645 274 L 1634 274 L 1624 270 L 1617 262 L 1614 253 L 1614 239 L 1616 237 L 1622 236 L 1750 236 L 1755 233 L 1759 225 L 1763 203 L 1762 171 L 1758 157 L 1752 145 Z M 1685 174 L 1686 191 L 1683 197 L 1623 196 L 1625 188 L 1632 175 L 1643 164 L 1656 159 L 1666 159 L 1673 161 L 1680 166 Z M 1979 132 L 1956 118 L 1940 113 L 1920 110 L 1887 110 L 1861 114 L 1844 120 L 1823 131 L 1806 145 L 1796 157 L 1784 177 L 1775 203 L 1770 227 L 1769 253 L 1771 267 L 1779 286 L 1787 297 L 1801 308 L 1824 318 L 1853 323 L 1883 323 L 1905 320 L 1928 313 L 1946 304 L 1960 294 L 1974 280 L 1990 254 L 1996 237 L 2002 208 L 2003 180 L 2000 164 L 1993 148 Z M 1883 167 L 1901 178 L 1935 203 L 1939 208 L 1939 217 L 1935 223 L 1869 263 L 1865 265 L 1856 265 L 1852 263 L 1847 256 L 1847 246 L 1861 180 L 1870 169 L 1874 167 Z M 1553 48 L 1481 48 L 1477 52 L 1475 57 L 1462 126 L 1459 134 L 1445 121 L 1433 115 L 1416 111 L 1396 111 L 1379 114 L 1363 120 L 1347 130 L 1334 142 L 1321 159 L 1310 182 L 1303 205 L 1298 235 L 1298 263 L 1302 281 L 1309 295 L 1320 307 L 1330 314 L 1345 320 L 1373 323 L 1403 317 L 1420 307 L 1430 298 L 1431 301 L 1429 314 L 1431 318 L 1434 320 L 1496 320 L 1502 316 L 1505 308 L 1533 164 L 1556 58 L 1556 52 Z M 1431 171 L 1437 174 L 1444 181 L 1448 194 L 1448 203 L 1445 220 L 1440 236 L 1431 251 L 1423 258 L 1408 264 L 1398 264 L 1391 262 L 1382 255 L 1379 250 L 1377 241 L 1379 219 L 1386 194 L 1392 184 L 1405 173 L 1414 170 Z M 1236 48 L 1163 47 L 1155 53 L 1070 215 L 1057 54 L 1055 50 L 1050 47 L 977 47 L 972 50 L 970 54 L 970 61 L 979 114 L 1007 310 L 1011 318 L 1015 320 L 1089 320 L 1097 314 L 1157 203 L 1236 62 L 1239 55 L 1239 52 Z M 1264 46 L 1260 51 L 1252 73 L 1243 91 L 1244 100 L 1248 103 L 1253 104 L 1308 104 L 1314 101 L 1318 96 L 1328 52 L 1327 47 L 1323 43 L 1270 43 Z M 388 15 L 384 13 L 149 13 L 143 16 L 140 21 L 137 39 L 139 43 L 143 46 L 158 46 L 163 50 L 160 67 L 158 70 L 154 72 L 88 72 L 81 75 L 77 81 L 75 97 L 80 103 L 146 104 L 148 106 L 148 112 L 146 121 L 140 126 L 110 126 L 102 129 L 98 135 L 96 150 L 101 157 L 133 158 L 136 161 L 134 175 L 130 179 L 64 180 L 56 184 L 54 187 L 51 205 L 54 209 L 58 211 L 119 211 L 122 212 L 125 216 L 122 229 L 117 233 L 80 233 L 74 235 L 68 241 L 66 257 L 71 263 L 109 264 L 114 268 L 112 282 L 106 287 L 77 287 L 69 291 L 66 297 L 65 304 L 66 316 L 71 320 L 183 320 L 187 318 L 192 311 L 211 210 L 213 204 L 220 199 L 320 199 L 324 197 L 329 190 L 338 145 L 337 136 L 334 133 L 234 133 L 229 129 L 228 126 L 236 88 L 243 82 L 373 81 L 379 76 L 390 24 L 390 18 Z M 90 14 L 84 20 L 81 35 L 83 42 L 89 46 L 105 46 L 109 44 L 114 39 L 117 23 L 115 17 L 109 13 Z" fill="#1B94FC" fill-rule="evenodd"/>
+ <path d="M 535 122 L 530 120 L 507 120 L 489 123 L 473 129 L 461 136 L 450 145 L 449 142 L 452 131 L 452 125 L 448 121 L 387 120 L 382 122 L 377 127 L 373 149 L 338 303 L 338 316 L 343 320 L 407 320 L 411 318 L 416 311 L 435 220 L 441 205 L 451 193 L 460 187 L 468 184 L 486 182 L 517 183 L 524 179 L 527 174 L 537 131 L 537 125 Z M 941 135 L 927 126 L 907 119 L 890 116 L 863 116 L 843 119 L 824 125 L 815 129 L 794 143 L 779 158 L 771 169 L 761 187 L 752 213 L 748 237 L 748 255 L 753 277 L 762 293 L 777 307 L 792 315 L 808 320 L 829 323 L 852 323 L 882 318 L 902 311 L 928 296 L 943 282 L 952 268 L 951 259 L 947 257 L 886 257 L 877 261 L 865 272 L 852 276 L 843 276 L 833 272 L 827 266 L 823 256 L 823 240 L 825 238 L 954 238 L 960 235 L 964 228 L 967 209 L 966 178 L 963 166 L 956 152 Z M 893 177 L 894 198 L 892 200 L 831 199 L 832 192 L 839 178 L 850 167 L 857 163 L 865 161 L 873 161 L 880 163 L 889 170 Z M 716 134 L 703 126 L 683 119 L 663 116 L 640 116 L 620 119 L 596 127 L 583 134 L 565 147 L 551 162 L 539 180 L 530 199 L 521 232 L 520 256 L 522 268 L 529 286 L 537 297 L 555 311 L 572 318 L 592 322 L 622 323 L 647 320 L 668 314 L 689 304 L 704 294 L 718 280 L 725 269 L 726 262 L 721 257 L 659 257 L 653 259 L 641 270 L 633 274 L 625 276 L 615 276 L 604 271 L 599 265 L 596 257 L 595 245 L 596 240 L 598 238 L 727 238 L 734 235 L 737 231 L 741 213 L 741 177 L 737 162 L 733 154 L 726 144 Z M 667 176 L 669 189 L 668 197 L 666 200 L 605 199 L 607 190 L 615 176 L 621 170 L 632 163 L 639 161 L 648 161 L 655 163 L 665 172 Z" fill="#DDE3EB" fill-rule="evenodd"/>
+</svg>
diff --git a/web/assets/icon.svg b/web/assets/icon.svg
new file mode 100644
index 0000000..2f0e4c9
--- /dev/null
+++ b/web/assets/icon.svg
@@ -0,0 +1,8 @@
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="512" height="512" role="img" aria-label="FreeToken">
+ <title>FreeToken</title>
+ <rect x="0" y="0" width="512" height="512" rx="115" fill="#14171c"></rect>
+
+ <g color="#58a6f0"><g transform="translate(0.0,0.0) scale(1)"><g transform="translate(19.8,23.8) scale(0.5556) skewX(-9.486)">
+ <path fill="currentColor" fill-rule="nonzero" d="M391 112 H498 A18 18 0 0 1 516 130 V706 A18 18 0 0 1 498 724 H391 A18 18 0 0 1 373 706 V130 A18 18 0 0 1 391 112 Z M391 112 H762 A18 18 0 0 1 780 130 V208 A18 18 0 0 1 762 226 H391 A18 18 0 0 1 373 208 V130 A18 18 0 0 1 391 112 Z M334 112 H432 A18 18 0 0 1 450 130 V150 A18 18 0 0 1 432 168 H334 A18 18 0 0 1 316 150 V130 A18 18 0 0 1 334 112 Z M391 340 H709 A18 18 0 0 1 727 358 V456 A18 18 0 0 1 709 474 H391 A18 18 0 0 1 373 456 V358 A18 18 0 0 1 391 340 Z M316 340 H432 A18 18 0 0 1 450 358 V375 A18 18 0 0 1 432 393 H316 A18 18 0 0 1 298 375 V358 A18 18 0 0 1 316 340 Z M248 227 H432 A18 18 0 0 1 450 245 V263 A18 18 0 0 1 432 281 H248 A18 18 0 0 1 230 263 V245 A18 18 0 0 1 248 227 Z M251 451 H432 A18 18 0 0 1 450 469 V486 A18 18 0 0 1 432 504 H251 A18 18 0 0 1 233 486 V469 A18 18 0 0 1 251 451 Z M302 560 H432 A18 18 0 0 1 450 578 V593 A18 18 0 0 1 432 611 H302 A18 18 0 0 1 284 593 V578 A18 18 0 0 1 302 560 Z M311 670 H498 A18 18 0 0 1 516 688 V706 A18 18 0 0 1 498 724 H311 A18 18 0 0 1 293 706 V688 A18 18 0 0 1 311 670 Z M239 117 H252 A18 18 0 0 1 270 135 V150 A18 18 0 0 1 252 168 H239 A18 18 0 0 1 221 150 V135 A18 18 0 0 1 239 117 Z M190 341 H236 A18 18 0 0 1 254 359 V372 A18 18 0 0 1 236 390 H190 A18 18 0 0 1 172 372 V359 A18 18 0 0 1 190 341 Z M209 560 H223 A18 18 0 0 1 241 578 V593 A18 18 0 0 1 223 611 H209 A18 18 0 0 1 191 593 V578 A18 18 0 0 1 209 560 Z M514 224 L528 224 L528 226 A12 12 0 0 0 516 238 L514 238 Z M514 342 L514 328 L516 328 A12 12 0 0 0 528 340 L528 342 Z M514 472 L528 472 L528 474 A12 12 0 0 0 516 486 L514 486 Z M375 166 L375 180 L373 180 A12 12 0 0 0 361 168 L361 166 Z M375 229 L361 229 L361 227 A12 12 0 0 0 373 215 L375 215 Z M375 279 L375 293 L373 293 A12 12 0 0 0 361 281 L361 279 Z M375 342 L361 342 L361 340 A12 12 0 0 0 373 328 L375 328 Z M375 391 L375 405 L373 405 A12 12 0 0 0 361 393 L361 391 Z M375 453 L361 453 L361 451 A12 12 0 0 0 373 439 L375 439 Z M375 502 L375 516 L373 516 A12 12 0 0 0 361 504 L361 502 Z M375 562 L361 562 L361 560 A12 12 0 0 0 373 548 L375 548 Z M375 609 L375 623 L373 623 A12 12 0 0 0 361 611 L361 609 Z M375 672 L361 672 L361 670 A12 12 0 0 0 373 658 L375 658 Z"></path>
+ </g></g></g>
+</svg>
diff --git a/web/branding.js b/web/branding.js
new file mode 100644
index 0000000..d066a5b
--- /dev/null
+++ b/web/branding.js
@@ -0,0 +1,13 @@
+// All artwork is served by this custom node; no font/CDN request is needed.
+const css = document.createElement('link');
+css.rel = 'stylesheet'; css.href = new URL('./theme.css', import.meta.url).href;
+document.head.append(css);
+
+export function wordmark() {
+ const image = document.createElement('img');
+ image.className = 'fv-wordmark';
+ image.src = new URL('./assets/freevideo.svg', import.meta.url).href;
+ image.alt = 'FreeVideo'; image.width = 2016; image.height = 337;
+ image.draggable = false;
+ return image;
+}
diff --git a/web/compatibility.js b/web/compatibility.js
new file mode 100644
index 0000000..ea418a5
--- /dev/null
+++ b/web/compatibility.js
@@ -0,0 +1,103 @@
+import { api } from '../../scripts/api.js';
+
+const languageOverride = typeof location !== 'undefined'
+ ? new URLSearchParams(location.search).get('freevideo_lang') : null;
+const cn = languageOverride === 'zh' || (languageOverride !== 'en'
+ && String(navigator.language || '').toLowerCase().startsWith('zh'));
+const t = (en, zh) => cn ? zh : en;
+const unavailable = () => t('Device information is unavailable; compatibility settings are off.', '无法获取设备信息,兼容性设置已关闭。');
+let current;
+const element = (tag, text) => { const node = document.createElement(tag); if (text) node.textContent = text; return node; };
+async function client() {
+ const response = await api.fetchApi('/freevideo/setup');
+ const info = await response.json();
+ if (!response.ok) throw new Error(info.error || response.statusText);
+ return async (action, value = {}) => {
+ const response = await api.fetchApi('/freevideo/setup/compatibility-' + action, {
+ method: 'POST', headers: {'Content-Type':'application/json','X-FreeVideo-Setup':info.discovery.token},
+ body: JSON.stringify({root:info.discovery.root, ...value}),
+ });
+ const result = await response.json();
+ if (!response.ok || result.error) throw new Error(result.error || response.statusText);
+ return result;
+ };
+}
+// Same sheet as Settings: header with close, content, actions on the right.
+function dialog(title) {
+ const value = element('dialog');
+ value.className = 'fv-setup fv-setup-small';
+ const head = element('header'); head.className = 'fv-setup-head';
+ const close = element('button', '×'); close.type = 'button'; close.className = 'fv-close';
+ close.setAttribute('aria-label', t('Close', '关闭')); close.onclick = () => value.close();
+ head.append(element('h2', title), close);
+ const actions = element('div'); actions.className = 'fv-actions fv-end';
+ value.append(head, actions); value.actions = actions;
+ value.addEventListener('close', () => value.remove());
+ return value;
+}
+function button(panel, label, action, primary = false) {
+ const value = element('button', label); value.type = 'button';
+ value.className = primary ? 'fv-primary' : 'fv-quiet';
+ value.onclick = action; panel.actions.append(value); return value;
+}
+// Open once the first content is in place, so focus lands on a control.
+function show(panel, focus) {
+ document.body.append(panel); panel.showModal(); focus?.focus();
+}
+export async function openCompatibility() {
+ if (current?.open) { current.focus(); return; }
+ const panel = dialog(t('Compatibility', '兼容性')); current = panel;
+ const status = element('p', t('Loading settings…', '正在读取设置…')); status.className = 'fv-muted';
+ panel.actions.before(status);
+ show(panel);
+ try {
+ const request = await client(), value = await request('check');
+ if (!value.available) {
+ status.textContent = unavailable();
+ button(panel, t('Close', '关闭'), () => panel.close(), true).focus();
+ return;
+ }
+ status.textContent = '';
+ const name = element('p'); name.className = 'fv-level';
+ const slider = element('input');
+ slider.type = 'range'; slider.min = 0; slider.max = 3; slider.step = 1; slider.value = value.level;
+ slider.setAttribute('aria-label', t('Compatibility level', '兼容性档位'));
+ const autoLabel = element('label'), automatic = element('input');
+ autoLabel.className = 'fv-check'; automatic.type = 'checkbox'; automatic.checked = value.automatic;
+ autoLabel.append(automatic, element('span', t('Automatically enable / increase after unexpected interruption', '异常中断后自动启用/提高兼容档')));
+ const note = element('p', t('Applies to the next generation. Smaller work groups can be slower and shift floating-point results; resolution, duration and steps are unchanged.', '从下一次生成生效。更小的分组可能变慢并产生浮点差异;分辨率、时长和步数不变。'));
+ note.className = 'fv-muted';
+ status.before(name, slider, autoLabel, note);
+ const render = () => { name.textContent = value.levels[Number(slider.value)][cn ? 'zh' : 'en']; }; render();
+ slider.oninput = () => { render(); if (Number(slider.value) === 0) automatic.checked = false; };
+ button(panel, t('Cancel', '取消'), () => panel.close());
+ button(panel, t('Save', '保存'), async () => {
+ try {
+ const saved = await request('save', {level: Number(slider.value), automatic: automatic.checked});
+ if (!saved.available) { slider.disabled = true; automatic.disabled = true; status.textContent = unavailable(); return; }
+ panel.close();
+ }
+ catch (error) { status.textContent = error.message; }
+ }, true);
+ } catch (error) {
+ status.textContent = error.message;
+ button(panel, t('Close', '关闭'), () => panel.close(), true).focus();
+ }
+}
+export async function notifyCompatibility() {
+ try {
+ const request=await client(), value=await request('check');
+ if (!value.available || !value.notice) return;
+ const panel = dialog(t('Compatibility enabled', '已开启兼容性设置'));
+ const message = element('p', t(`The previous generation ended unexpectedly. Compatibility level ${value.level} is now enabled. You can adjust or disable it in settings.`,
+ `上次生成未正常结束,已自动开启兼容性设置(第 ${value.level} 档)。你可以在设置里调节或关闭。`));
+ panel.actions.before(message);
+ const finish = async (settings) => {
+ try { await request('ack', {notice_id: value.notice.id}); panel.close(); if (settings) await openCompatibility(); }
+ catch (error) { panel.actions.before(element('p', error.message)); }
+ };
+ button(panel, t('Adjust settings', '调整设置'), () => finish(true));
+ const ok = button(panel, t('OK', '知道了'), () => finish(false), true);
+ show(panel, ok);
+ } catch(error) { console.warn('FreeVideo compatibility check:',error.message); }
+}
diff --git a/web/error_panel.js b/web/error_panel.js
new file mode 100644
index 0000000..36ce7d4
--- /dev/null
+++ b/web/error_panel.js
@@ -0,0 +1,103 @@
+// Shared by the Studio and graph view. Text only; never render exception HTML.
+export function errorText(detail) {
+ const cause = detail?.exception_message || detail?.message || 'FreeVideo generation failed.';
+ const trace = Array.isArray(detail?.traceback) ? detail.traceback.join('') : '';
+ return String(cause + (trace ? '\n\n' + trace : '')).slice(0, 65536);
+}
+
+// Match explicit failure signatures, not a guessed GPU/RAM capacity. Keep the
+// original error separately so an explanation never replaces diagnostic data.
+export function failureAdvice(value, t) {
+ const text = String(value).slice(0, 65536);
+ const row = (kind, title, summary, action) => ({kind, title: t(...title), summary: t(...summary), action: t(...action)});
+ if (/commit headroom exhausted|paging file is too small|WinError 1455/i.test(text))
+ return row('commit', ['Windows memory allocation limit reached', 'Windows 内存提交额度不足'],
+ ['Windows cannot back another memory allocation, even if physical RAM is still available.', 'Windows 已没有足够的提交额度;这与物理 RAM 是否还有空闲是两回事。'],
+ ['Close memory-heavy applications and retry. In Windows virtual memory settings, use a system-managed paging file on a drive with free space.', '关闭占用内存较多的程序后重试。在 Windows 虚拟内存设置中,使用系统管理的分页文件,并确保所在磁盘有空闲空间。']);
+ if (/CUDA out of memory|torch\.OutOfMemoryError|cudaErrorMemoryAllocation/i.test(text))
+ return row('vram', ['GPU memory allocation failed', '显存分配失败'],
+ ['The GPU could not allocate the memory needed at this stage.', '这一阶段的显存申请没有成功。'],
+ ['Close other GPU workloads and retry so FreeVideo can replan using the newly available memory. Your resolution, duration and steps are kept.', '关闭其他占用显卡的任务后重试,FreeVideo 会根据新的可用显存重新规划,保留你的分辨率、时长和步数。']);
+ if (/RAM guard:|crossed its RAM budget|Insufficient currently available memory/i.test(text))
+ return row('ram', ['Available memory or an explicit RAM limit reached', '可用内存或手动 RAM 限额不足'],
+ ['Automatic RAM estimates can be exceeded while memory is available. This stop indicates system pressure or an explicitly enforced limit.', '自动模式允许在系统仍有余量时超出预估 RAM;停止意味着系统内存压力,或触及了手动设置的硬限制。'],
+ ['Close memory-heavy applications and retry. If you set a RAM limit for testing, remove or raise it for normal generation. Copy the details below if it still fails.', '关闭占用内存较多的程序后重试。如果设置过测试用 RAM 限制,正常生成时可取消或调高。仍失败时可复制下方详情。']);
+ if (/No space left on device|WinError 112|disk (?:is )?full/i.test(text))
+ return row('disk', ['Disk space is insufficient', '磁盘空间不足'],
+ ['FreeVideo could not write a required file.', 'FreeVideo 无法写入所需文件。'],
+ ['Free space on the installation/output drive and retry. Keep model downloads and retained outputs if you want to resume or inspect them.', '清理安装目录或输出目录所在磁盘的空间后重试;保留模型下载和输出文件,便于续传或排查。']);
+ if (/Model download paused|unexpected-transfer-size|ConnectionError|ConnectTimeout|ReadTimeout/i.test(text))
+ return row('download', ['Download needs attention', '下载未完成'],
+ ['A transfer failed or its received data did not match the expected file.', '传输中断,或收到的数据与预期文件不一致。'],
+ ['Check your connection or select another download source in Settings, then retry. Existing download fragments are retained.', '检查网络,或在设置中切换下载源后重试。已有下载片段会保留。']);
+ if (/ModuleNotFoundError|No module named|DLL load failed/i.test(text))
+ return row('dependencies', ['A runtime dependency could not load', '运行依赖无法加载'],
+ ['The runtime may be incomplete or an incompatible environment may have been selected.', '运行环境可能不完整,或使用了不兼容的环境。'],
+ ['Open the FreeVideo launcher and rerun installation for the same folder to check dependencies. If it persists, copy the details below.', '打开 FreeVideo 启动器,对同一安装目录重新执行安装以检查依赖。仍失败时可复制下方详情。']);
+ return row('unknown', ['Generation stopped', '生成已停止'],
+ ['This request did not complete. The exact error is retained below.', '这次请求未完成,具体错误已保留在下方。'],
+ ['Copy the error details when reporting the issue; they include the failed stage when available.', '反馈问题时请复制错误详情,其中会保留已记录的失败阶段。']);
+}
+
+let activeDialog = null;
+function showFailureDialog(value, t) {
+ // Both graph and Studio receive execution_error. Show one modal for that
+ // event, above the Studio modal if it is open, with keyboard dismissal.
+ if (activeDialog?.open && activeDialog.freevideoError === value) return;
+ activeDialog?.close();
+ const dialog = document.createElement('dialog'); dialog.className = 'fv-failure-dialog';
+ dialog.freevideoError = value;
+ dialog.setAttribute('aria-label', t('FreeVideo error', 'FreeVideo 错误'));
+ const panel = createErrorPanel(t); panel.show(value, false);
+ const close = document.createElement('button'); close.type = 'button';
+ close.textContent = t('Got it', '知道了'); close.autofocus = true;
+ close.onclick = () => dialog.close();
+ dialog.append(panel.element, close);
+ dialog.onclose = () => { if (activeDialog === dialog) activeDialog = null; dialog.remove(); };
+ document.body.append(dialog);
+ if (typeof dialog.showModal !== 'function') { dialog.remove(); return; }
+ activeDialog = dialog; dialog.showModal();
+}
+
+export function createErrorPanel(t) {
+ if (!document.getElementById('freevideo-error-style')) {
+ const style = document.createElement('style'); style.id = 'freevideo-error-style';
+ style.textContent = `.fv-failure{border:1px solid var(--fv-danger,#dc7c7c);border-radius:var(--fv-r-md,12px);padding:12px 14px;margin:10px 0;text-align:left;min-width:0}
+.fv-failure[hidden]{display:none}.fv-failure-head{display:flex;align-items:center;justify-content:space-between;gap:12px;margin-bottom:8px}
+.fv-failure textarea{box-sizing:border-box;width:100%;height:180px;min-height:100px;resize:vertical;border:1px solid var(--fv-border,#45454a);border-radius:var(--fv-r-sm,8px);background:var(--fv-bg,#202126);color:var(--fv-ink,#eee);font:12px/1.5 monospace;padding:9px;white-space:pre-wrap}
+.fv-failure button,.fv-failure-dialog>button{min-height:var(--fv-h-sm,32px);border:1px solid var(--fv-border,#45454a);border-radius:var(--fv-r-sm,8px);background:var(--fv-raised,#303138);color:var(--fv-ink,#eee);padding:5px 12px;font:inherit;font-weight:var(--fv-medium,500);cursor:pointer}
+.fv-failure p{font-size:14px;line-height:1.6;margin:8px 0}.fv-failure summary{cursor:pointer;margin-top:12px}
+.fv-failure-dialog{box-sizing:border-box;width:min(560px,94vw);max-height:88vh;overflow:auto;padding:22px 24px;border:1px solid var(--fv-border,#45454a);border-radius:var(--fv-r-lg,16px);background:var(--fv-bg,#202126);color:var(--fv-ink,#eee);box-shadow:0 24px 80px #0008}
+.fv-failure-dialog::backdrop{background:#070a0db8;backdrop-filter:blur(6px)}.fv-failure-dialog>.fv-failure{border:0;margin:0 0 16px;padding:0}.fv-failure-dialog>button{display:block;margin-left:auto;background:var(--fv-accent,#6db8fa);border-color:var(--fv-accent,#6db8fa);color:var(--fv-bg,#111720);font-weight:var(--fv-semibold,600)}`;
+ document.head.append(style);
+ }
+ const element = document.createElement('section'); element.className = 'fv-failure'; element.hidden = true;
+ const head = document.createElement('div'); head.className = 'fv-failure-head';
+ const title = document.createElement('strong'); title.textContent = t('Generation failed', '生成失败');
+ const copy = document.createElement('button'); copy.type = 'button'; copy.textContent = t('Copy error', '复制错误');
+ const explanation = document.createElement('p'), action = document.createElement('p');
+ const disclosure = document.createElement('details'), summary = document.createElement('summary');
+ summary.textContent = t('Technical details', '技术详情');
+ const details = document.createElement('textarea'); details.readOnly = true;
+ details.setAttribute('aria-label', t('Error details', '错误详情')); details.spellcheck = false;
+ copy.onclick = async () => {
+ let copied = false;
+ try { await navigator.clipboard.writeText(details.value); copied = true; } catch {
+ // Remote ComfyUI commonly uses plain HTTP, where Clipboard API is
+ // unavailable. Keep selectable text even if the browser denies copy.
+ disclosure.open = true; details.focus(); details.select();
+ try { copied = document.execCommand('copy'); } catch { /* manual Ctrl+C */ }
+ }
+ copy.textContent = copied ? t('Copied', '已复制') : t('Press Ctrl+C', '请按 Ctrl+C');
+ };
+ disclosure.append(summary, details);
+ head.append(title, copy); element.append(head, explanation, action, disclosure);
+ return {element, show(value, popup = true) {
+ value = String(value).slice(0, 65536);
+ const advice = failureAdvice(value, t);
+ title.textContent = advice.title; explanation.textContent = advice.summary; action.textContent = advice.action;
+ details.value = value; disclosure.open = false; element.hidden = false;
+ copy.textContent = t('Copy error', '复制错误');
+ if (popup) showFailureDialog(value, t);
+ }, clear() { details.value = ''; element.hidden = true; }};
+}
diff --git a/web/freevideo.js b/web/freevideo.js
new file mode 100644
index 0000000..6d2253d
--- /dev/null
+++ b/web/freevideo.js
@@ -0,0 +1,335 @@
+import { createErrorPanel, errorText } from './error_panel.js';
+import { app } from "../../scripts/app.js";
+import { api } from "../../scripts/api.js";
+import { openStudio, loraPanel, loraWarning, promptGuide } from "./studio.js";
+import { createGenerationProgress } from './generation_progress.js';
+import { createProgressConnection } from './progress_connection.js';
+import { notifyCompatibility } from './compatibility.js';
+import { installNavigation, refreshNavigation, preferredView } from './view_navigation.js';
+
+const languageOverride = typeof location !== 'undefined'
+ ? new URLSearchParams(location.search).get('freevideo_lang') : null;
+const cn = languageOverride === 'zh' || (languageOverride !== 'en'
+ && String(navigator.language || '').toLowerCase().startsWith('zh'));
+const text = (en, zh) => cn ? zh : en;
+const style = document.createElement("style");
+style.textContent = `
+.fv-panel{box-sizing:border-box;width:100%;height:100%;padding:16px;background:var(--fv-card);color:var(--fv-ink);border:1px solid var(--fv-border);border-radius:var(--fv-r-md,12px);font:13px/1.6 var(--fv-font);overflow:auto;color-scheme:dark}
+.fv-panel *{box-sizing:border-box}.fv-toolbar,.fv-row,.fv-links{display:flex;align-items:center;gap:8px;flex-wrap:wrap}
+.fv-panel .fv-links{justify-content:center}.fv-panel[data-freevideo=result]>.fv-note{text-align:center}
+.fv-panel .fv-toolbar{padding:4px 0 8px}.fv-panel .fv-stats{text-align:center}.fv-panel .fv-stat{padding:10px 3px;border-radius:var(--fv-r-md,12px);background:var(--fv-raised)}
+.fv-panel .fv-connected{display:flex;align-items:center;justify-content:center;gap:6px;flex-wrap:wrap;margin:10px 0}.fv-panel .fv-connection{border:1px solid var(--fv-border);border-radius:var(--fv-r-sm,8px);padding:5px 9px;background:var(--fv-selected);color:var(--fv-accent);font-size:var(--fv-micro,12px)}
+.fv-panel :focus-visible{outline:2px solid var(--fv-accent);outline-offset:2px}
+.fv-panel[data-freevideo=result]{display:flex;flex-direction:column;justify-content:center}.fv-panel .fv-connected-card{text-align:center;padding:28px 12px;border:1px solid var(--fv-border);border-radius:var(--fv-r-md,12px);color:var(--fv-muted);background:var(--fv-raised)}
+.fv-panel button,.fv-panel select,.fv-panel a{box-sizing:border-box;min-height:var(--fv-h-sm,32px);border:1px solid var(--fv-border);border-radius:var(--fv-r-sm,8px);background:var(--fv-raised);color:var(--fv-ink);padding:5px 10px;font:inherit;font-weight:var(--fv-medium,500);cursor:pointer;text-decoration:none}
+.fv-panel button:hover,.fv-panel a:hover{background:var(--fv-hover)}.fv-panel button:disabled{opacity:.45;cursor:wait}
+.fv-panel select{max-width:170px}.fv-note{color:var(--fv-muted);font-size:var(--fv-micro,12px);margin:8px 0;white-space:pre-wrap}.fv-error{color:var(--fv-danger)}
+.fv-mode{font-size:var(--fv-micro,12px);color:var(--fv-accent)}
+.fv-panel[data-freevideo=media]>.fv-mode{display:inline-block;margin:4px 0 2px;padding:2px 8px;border-radius:var(--fv-r-xs,3px);background:var(--fv-selected);font-weight:var(--fv-medium,500)}.fv-card{border:1px solid var(--fv-border);border-radius:var(--fv-r-md,12px);padding:10px;margin-top:10px;background:var(--fv-raised)}
+.fv-card[data-enabled=false]{opacity:.5}.fv-file{overflow:hidden;text-overflow:ellipsis;white-space:nowrap;flex:1;min-width:60px}
+.fv-preview{display:block;width:100%;max-height:160px;object-fit:contain;margin:8px 0;border-radius:var(--fv-r-sm,8px);background:var(--fv-bg)}
+.fv-card audio{width:100%;height:36px;margin:8px 0}.fv-empty{padding:35px 12px;text-align:center;color:var(--fv-muted);border:1px dashed var(--fv-border);border-radius:var(--fv-r-md,12px);margin-top:12px}
+.fv-stats{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:6px;margin-bottom:9px}.fv-stat strong{display:block;color:var(--fv-ink);font-size:var(--fv-strong,16px);font-weight:var(--fv-semibold,600);font-variant-numeric:tabular-nums}.fv-stat span{font-size:var(--fv-micro,12px);color:var(--fv-muted)}
+`;
+document.head.append(style);
+
+function el(tag, label, className) {
+ const element = document.createElement(tag);
+ if (label !== undefined) element.textContent = label;
+ if (className) element.className = className;
+ return element;
+}
+function button(label, title, action) {
+ const b = el("button", label); b.type = "button"; b.title = title; b.onclick = action; return b;
+}
+function viewURL(file, type = "input") {
+ const split = file.lastIndexOf("/");
+ return api.apiURL("/view?" + new URLSearchParams({
+ filename: file.slice(split + 1), subfolder: split < 0 ? "" : file.slice(0, split), type,
+ }));
+}
+function mediaKind(name) {
+ const ext = name.split(".").pop().toLowerCase();
+ if (["png", "jpg", "jpeg", "webp", "bmp", "tif", "tiff"].includes(ext)) return "image";
+ if (["mp4", "mov", "webm", "mkv", "m4v"].includes(ext)) return "video";
+ if (["wav", "mp3", "flac", "ogg", "m4a", "aac", "opus"].includes(ext)) return "audio";
+ throw new Error(text("Choose an image, video or audio file", "请选择图片、视频或音频文件"));
+}
+
+function mediaPanel(node, mount = null) {
+ const serialized = node.widgets.find(w => w.name === "assets");
+ serialized.type = "hidden";
+ serialized.computeSize = () => [0, -4];
+ serialized.draw = () => {};
+ const panel = el("div", undefined, "fv-panel"); panel.dataset.freevideo = "media";
+ panel.tabIndex = 0;
+ const toolbar = el("div", undefined, "fv-toolbar");
+ const note = el("div", "", "fv-note"); note.setAttribute("role", "status");
+ const mode = el("div", "", "fv-mode");
+ const cards = el("div"), connections = el("div", undefined, "fv-connected");
+ const picker = el("input"); picker.type = "file"; picker.multiple = true;
+ picker.accept = "image/*,video/*,audio/*"; picker.hidden = true;
+ const add = button(text("Upload files", "上传文件"), text("Upload images, video or audio", "上传图片、视频或音频"), () => picker.click());
+ const audioPicker = el('input'); audioPicker.type = 'file'; audioPicker.multiple = true;
+ audioPicker.accept = 'audio/*,.wav,.mp3,.flac,.ogg,.m4a,.aac,.opus'; audioPicker.hidden = true;
+ const addAudio = button(text('Reference audio', '参考音频'),
+ text('Add a voice, music or rhythm reference (up to 15 seconds)', '添加音色、音乐或节奏参考(最长 15 秒)'), () => audioPicker.click());
+ const audioHelp = el('div', '', 'fv-note'); audioHelp.hidden = true;
+ const role = el("select"); role.setAttribute("aria-label", text("Use new images as", "新图片用途"));
+ for (const [value, label] of [["reference", text("Reference", "参考")], ["first", text("First frame", "首帧")], ["last", text("Last frame", "尾帧")]]) {
+ const option = el("option", label); option.value = value; role.append(option);
+ }
+ role.value = 'reference';
+ toolbar.append(add, addAudio, role, picker, audioPicker); panel.append(toolbar, mode, note, audioHelp, connections, cards);
+ const read = () => {
+ const rows = JSON.parse(serialized.value || "[]");
+ if (!Array.isArray(rows) || rows.length > 32) throw new Error(text("Use at most 32 media items", "最多使用 32 个素材"));
+ return rows;
+ };
+ const message = (value, error = false) => { note.textContent = value; note.classList.toggle("fv-error", error); };
+ const update = rows => { serialized.value = JSON.stringify(rows); node.graph?.change(); window.dispatchEvent(new CustomEvent('freevideo-media', {detail: node.id})); };
+ const changed = event => { if (String(event.detail) === String(node.id)) render(); };
+ window.addEventListener('freevideo-media', changed);
+ function render() {
+ for (const preview of cards.querySelectorAll("video,audio")) {
+ preview.pause(); preview.removeAttribute("src"); preview.load();
+ }
+ cards.replaceChildren();
+ let rows;
+ try { rows = read(); } catch (error) { message(error.message, true); return; }
+ const active = rows.filter(row => row.enabled !== false);
+ const linked = name => node.inputs?.find(i => i.name === name)?.link != null;
+ const first = active.filter(row => row.role === "first").length + Number(linked('first'));
+ const last = active.filter(row => row.role === "last").length + Number(linked('last'));
+ const audioRefs = active.filter(row => row.role === 'reference' && mediaKind(row.file) === 'audio').length + Number(linked('reference_audio'));
+ const refs = active.filter(row => row.role === "reference").length + Number(linked('reference')) + Number(linked('reference_audio'));
+ audioHelp.hidden = !audioRefs;
+ audioHelp.textContent = text('Use <Audio 1>, <Audio 2>, … in your prompt to guide the generated voice, music or rhythm. Clips: up to 15 seconds. Set accompanying images to Reference.',
+ '在提示词中用 <Audio 1>、<Audio 2> 等标签引导生成的音色、音乐或节奏。片段最长 15 秒,搭配的图片请选择“参考”用途。');
+ connections.replaceChildren();
+ for (const [name, label] of [['first', text('First frame', '首帧')], ['last', text('Last frame', '尾帧')], ['reference', text('Reference image', '参考图像')], ['reference_audio', text('Reference audio', '参考音频')]]) {
+ if (!linked(name)) continue;
+ const link = node.graph?.links[node.inputs.find(i => i.name === name).link];
+ const source = link && node.graph?.getNodeById(link.origin_id);
+ const chip = el('span', `${label} · ${text('Connected', '已连接')}`, 'fv-connection');
+ chip.title = source?.title || source?.type || label; connections.append(chip);
+ }
+ mode.textContent = refs ? text("Reference video · experimental", "参考生成 · 实验性") : first && last ? text("First + last frame", "首尾帧生成") : first ? text("Image to video", "图生视频") : last ? text("Last frame to video", "尾帧生成") : text("Text to video · media is optional", "文生视频 · 素材可留空");
+ message(refs && (first || last) ? text("Choose keyframes or references. Disable unused cards.", "首尾帧与参考模式不能混用,请关闭本次不用的素材。") : first > 1 || last > 1 ? text("Keep only one first frame and one last frame enabled.", "首帧和尾帧各只能启用一张图片。") : text("Connect IMAGE / AUDIO outputs on the left, or drop / paste files here.", "可连接左侧 IMAGE / AUDIO 输入,或拖入、粘贴文件。"), Boolean(refs && (first || last) || first > 1 || last > 1));
+ if (!rows.length && !connections.childElementCount) cards.append(el("div", text("Connect an image / audio node or upload files.\nLeave empty for text to video.", "连接图像/音频节点或上传文件。\n留空即可文生视频。"), "fv-empty"));
+ if (!rows.length && connections.childElementCount) cards.append(el('div', text('Media will come from the connected nodes.\nNo upload is needed.', '素材由已连接的节点提供。\n无需上传文件。'), 'fv-connected-card'));
+ rows.forEach((row, index) => {
+ const card = el("div", undefined, "fv-card"); card.dataset.enabled = String(row.enabled !== false);
+ const top = el("div", undefined, "fv-row");
+ const enabled = el("input"); enabled.type = "checkbox"; enabled.checked = row.enabled !== false;
+ enabled.setAttribute("aria-label", text("Use media", "使用素材")); enabled.onchange = () => { rows[index].enabled = enabled.checked; update(rows); };
+ const label = el("span", row.file.split("/").pop(), "fv-file"); label.title = row.file;
+ const select = el("select"); select.setAttribute("aria-label", text("Media role", "素材用途"));
+ const kind = mediaKind(row.file);
+ for (const [value, title] of (kind === "image" ? [["reference", text("Reference", "参考")], ["first", text("First frame", "首帧")], ["last", text("Last frame", "尾帧")]] : [["reference", text("Reference", "参考")]])) {
+ const option = el("option", title); option.value = value; select.append(option);
+ }
+ select.value = row.role; select.onchange = () => { rows[index].role = select.value; update(rows); };
+ top.append(enabled, label, select); card.append(top);
+ const preview = el(kind === "image" ? "img" : kind); preview.className = "fv-preview";
+ preview.src = viewURL(row.file);
+ if (kind === "image") { preview.loading = "lazy"; preview.alt = label.textContent; }
+ else { preview.controls = true; preview.preload = "metadata"; if (kind === "video") { preview.muted = true; preview.playsInline = true; } }
+ card.append(preview);
+ const controls = el("div", undefined, "fv-row");
+ const move = delta => { const target = index + delta; if (target < 0 || target >= rows.length) return; [rows[index], rows[target]] = [rows[target], rows[index]]; update(rows); };
+ controls.append(button("↑", text("Move earlier", "向前移动"), () => move(-1)), button("↓", text("Move later", "向后移动"), () => move(1)),
+ button(text("Remove", "移除"), text("Remove from this request; keep the uploaded file", "从本次请求移除,保留已上传文件"), () => { rows.splice(index, 1); update(rows); }));
+ card.append(controls); cards.append(card);
+ });
+ }
+ let uploading = false;
+ serialized.serializeValue = () => {
+ if (node.isUploading) throw new Error(text("Wait for media uploads to finish before running", "请等待素材上传完成后再生成"));
+ return serialized.value;
+ };
+ async function upload(files) {
+ if (node.isUploading || !files.length) return;
+ uploading = true; node.isUploading = true; add.disabled = true; addAudio.disabled = true;
+ try {
+ for (const [index, file] of Array.from(files).entries()) {
+ const rows = read(); if (rows.length >= 32) throw new Error(text("Use at most 32 media items", "最多使用 32 个素材"));
+ const kind = mediaKind(file.name);
+ message(`${text("Uploading", "正在上传")} ${index + 1}/${files.length} · ${file.name}`);
+ const body = new FormData(); body.append("file", file);
+ const response = await api.fetchApi("/freevideo/media/upload", {method: "POST", body});
+ const result = await response.json(); if (!response.ok) throw new Error(result.error || response.statusText);
+ rows.push({file: result.file, role: kind === "image" ? role.value : "reference", enabled: true}); update(rows);
+ }
+ } catch (error) { message(error.message, true); }
+ finally { uploading = false; node.isUploading = false; add.disabled = false; addAudio.disabled = false; picker.value = ""; audioPicker.value = ''; }
+ }
+ picker.onchange = () => upload(picker.files);
+ audioPicker.onchange = () => upload(audioPicker.files);
+ panel.ondragover = event => { event.preventDefault(); event.stopPropagation(); };
+ panel.ondrop = event => { event.preventDefault(); event.stopPropagation(); upload(event.dataTransfer.files); };
+ panel.onpaste = event => { if (event.clipboardData?.files.length) { event.preventDefault(); event.stopPropagation(); upload(event.clipboardData.files); } };
+ const dispose = () => window.removeEventListener('freevideo-media', changed);
+ if (mount) { mount.append(panel); render(); return dispose; }
+ node.freevideoCreateMediaEditor = target => mediaPanel(node, target);
+ node.addDOMWidget("freevideo_media", "freevideo_media", panel, {serialize: false, getMinHeight: () => 300, getMaxHeight: () => 520});
+ const removed = node.onRemoved;
+ node.onRemoved = function (...args) { dispose(); return removed?.apply(this, args); };
+ const configure = node.onConfigure;
+ node.onConfigure = function (...args) { const result = configure?.apply(this, args); render(); return result; };
+ const connectionChanged = node.onConnectionsChange;
+ node.onConnectionsChange = function (...args) { const result = connectionChanged?.apply(this, args); update(read()); return result; };
+ node.setSize([460, 460]); render();
+}
+
+function resultPanel(node) {
+ node.addDOMWidget('freevideo_prompt_guide', 'freevideo_prompt_guide', promptGuide(),
+ {serialize: false, getMinHeight: () => 58, getMaxHeight: () => 76});
+ const panel = el("div", undefined, "fv-panel"); panel.dataset.freevideo = "result";
+ const progress = createGenerationProgress(text); panel.append(progress.element);
+ node.freevideoShowProgress = message => {
+ const retry = message.reset ? undefined : message.retry || node.freevideoProgress?.retry;
+ node.freevideoProgress = {...message, retry, received_at: message.received_at ?? Date.now()}; progress.update(node.freevideoProgress);
+ if (panel.firstElementChild !== progress.element) panel.prepend(progress.element);
+ node.setSize([node.size[0], Math.max(node.size[1], node.computeSize()[1])]);
+ };
+ node.freevideoStopProgress = () => { node.freevideoProgress = null; progress.hide(); };
+ const removed = node.onRemoved;
+ node.onRemoved = function (...args) { progress.dispose(); return removed?.apply(this, args); };
+ const failure = createErrorPanel(text);
+ node.freevideoShowFailure = detail => {
+ node.freevideoStopProgress();
+ node.freevideoFailure = errorText(detail); failure.show(node.freevideoFailure);
+ panel.append(failure.element); node.setSize([node.size[0], Math.max(node.size[1], node.computeSize()[1])]);
+ };
+ node.freevideoClearFailure = () => { node.freevideoFailure = ''; failure.clear(); };
+ const warning = el('div', '', 'fv-note');
+ const hasLoRA = () => node.inputs?.some(input => input.name === 'loras' && input.link != null);
+ const warn = () => {
+ warning.hidden = !hasLoRA(); warning.textContent = hasLoRA() ? loraWarning() : '';
+ if (panel.lastElementChild !== warning) panel.append(warning);
+ };
+ panel.append(el("div", text("Video, audio and timings appear here after generation.", "生成后在这里查看视频、音频和耗时。"), "fv-note"));
+ const panelHeight = () => (hasLoRA() ? 240 : 140) + (node.freevideoFailure ? 280 : 0) + (node.freevideoProgress ? 190 : 0);
+ node.addDOMWidget("freevideo_result", "freevideo_result", panel, {serialize: false, getMinHeight: panelHeight, getMaxHeight: panelHeight});
+ const connected = node.onConnectionsChange, configured = node.onConfigure;
+ node.onConnectionsChange = function (...args) { const result = connected?.apply(this, args); warn(); return result; };
+ node.onConfigure = function (...args) { const result = configured?.apply(this, args); warn(); return result; };
+ warn();
+ node.freevideoShowPrewarm = function (value) {
+ let note = panel.querySelector('.fv-prewarm');
+ if (!note) { note = el('div', '', 'fv-note fv-prewarm'); panel.append(note); }
+ const onGPU = Number.isFinite(value.gpu_bytes_after);
+ const size = onGPU ? ` · ${(value.gpu_bytes_after / 2 ** 30).toFixed(1)} GiB` : '';
+ note.textContent = value.state === 'loading' ? text('Preparing text encoder on GPU in background', '正在后台预加载文本编码器到 GPU') + size
+ : value.state === 'partial' ? text('Text encoder partially prepared on GPU', '文本编码器已部分预加载到 GPU') + size
+ : value.state === 'reading' ? text('Preparing encoder files', '正在预读编码器文件') + ` · ${Math.min(100, Math.floor(100 * value.done_bytes / value.bytes))}%`
+ : value.state === 'ready' ? (onGPU ? text('Text encoder ready on GPU', '文本编码器已在 GPU 就绪') + size
+ : text('Encoder files warmed · no VRAM reserved', '编码器文件已预热 · 不占用显存')) : '';
+ node.freevideoPrewarm = note.textContent;
+ window.dispatchEvent(new CustomEvent('freevideo-prewarm', {detail: {node: node.id, label: note.textContent}}));
+ };
+ node.freevideoShowResult = function (message) {
+ const value = message?.freevideo_summary?.[0]; if (!value) return;
+ node.freevideoStopProgress();
+ node.freevideoClearFailure();
+ node.freevideoLastResult = value;
+ window.dispatchEvent(new CustomEvent('freevideo-result', {detail: {node: node.id, value}}));
+ panel.replaceChildren(); const stats = el("div", undefined, "fv-stats");
+ const number = (value, scale, unit) => Number.isFinite(value) && value >= 0 ? `${(value / scale).toFixed(1)} ${unit}` : "—";
+ for (const [label, shown] of [[text("Sampling", "采样"), number(value.sample_seconds, 1, "s")], [text("Request total", "请求总计"), number(value.request_seconds, 1, "s")], [text("VRAM peak", "显存峰值"), number(value.vram_peak_bytes, 2 ** 30, "GiB")], [text("RAM peak", "内存峰值"), number(value.ram_peak_bytes, 2 ** 30, "GiB")]]) {
+ const stat = el("div", undefined, "fv-stat"); stat.append(el("strong", shown), el("span", label)); stats.append(stat);
+ }
+ const links = el("div", undefined, "fv-links");
+ for (const [label, file] of [[text("Download video", "下载视频"), value.video], [text("Report", "报告"), value.report]]) {
+ const link = el("a", label); link.href = viewURL(file, "output"); link.download = file.split("/").pop(); links.append(link);
+ }
+ links.append(el("span", value.conditioning_cache_hit ? text("Input cache reused", "已复用输入缓存") : text("Inputs encoded", "已编码输入"), "fv-mode"));
+ panel.append(stats, links);
+ warn();
+ panel.title = text("VRAM: engine allocator peak. RAM: measured request processes, ", "显存为引擎分配器峰值,内存为请求进程实测,") + (value.ram_metric || "unknown");
+ };
+ const executed = node.onExecuted;
+ node.onExecuted = function (message) {
+ const result = executed?.apply(this, arguments);
+ node.freevideoShowResult(message);
+ return result;
+ };
+}
+
+const progressConnection = createProgressConnection(api, message => {
+ const node = app.graph?.getNodeById(message.node);
+ if (!node?.freevideoShowProgress) return false;
+ if (message.result) node.freevideoShowResult({freevideo_summary: [message.result]});
+ else if (['failed', 'cancelled'].includes(message.phase)) node.freevideoStopProgress();
+ else node.freevideoShowProgress(message);
+ window.dispatchEvent(new CustomEvent('freevideo-progress', {detail: message}));
+});
+
+app.registerExtension({
+ name: "FreeVideo.UnifiedMedia",
+ async setup() { progressConnection.start(); installNavigation(openStudio); await notifyCompatibility(); },
+ async beforeRegisterNodeDef(nodeType, nodeData) {
+ if (!["FreeVideoMedia", "FreeVideoGenerate", "FreeVideoLoRAStack"].includes(nodeData.name)) return;
+ const created = nodeType.prototype.onNodeCreated;
+ nodeType.prototype.onNodeCreated = function () {
+ const result = created?.apply(this, arguments);
+ this.color = '#212a37'; this.bgcolor = '#1a222e';
+ if (nodeData.name === "FreeVideoMedia") mediaPanel(this);
+ else if (nodeData.name === "FreeVideoLoRAStack") {
+ const value = this.widgets.find(w => w.name === 'adapters');
+ value.type = 'hidden'; value.computeSize = () => [0, -4]; value.draw = () => {};
+ const panel = document.createElement('div'); panel.className = 'fv-panel fv-lora-panel';
+ const cleanup = loraPanel(this, panel);
+ this.addDOMWidget('freevideo_loras', 'freevideo_loras', panel, {serialize: false, getMinHeight: () => 180});
+ const removed = this.onRemoved;
+ this.onRemoved = function (...args) { cleanup(); return removed?.apply(this, args); };
+ this.setSize([460, 240]);
+ }
+ else {
+ const quality = this.widgets?.find(w => w.name === 'two_pass');
+ if (quality) {
+ quality.label = text('Two-pass sampling', '二次采样');
+ quality.tooltip = text('Generate the scene, then refine it at the target resolution.', '先生成画面,再以目标分辨率精修。');
+ }
+ resultPanel(this);
+ this.addWidget('button', text('Open creative workspace', '打开创作面板'), null, () => openStudio(this), {serialize: false});
+ const removed = this.onRemoved;
+ this.onRemoved = function (...args) { const value = removed?.apply(this, args); requestAnimationFrame(refreshNavigation); return value; };
+ requestAnimationFrame(refreshNavigation);
+ }
+ return result;
+ };
+ },
+ afterConfigureGraph() {
+ progressConnection.reset();
+ progressConnection.refresh();
+ refreshNavigation();
+ if (app.graph?.extra?.freevideo_studio && preferredView() !== 'nodes') {
+ const node = app.graph._nodes.find(n => n.comfyClass === 'FreeVideoGenerate' || n.type === 'FreeVideoGenerate');
+ if (node) requestAnimationFrame(() => openStudio(node));
+ }
+ },
+ onNodeOutputsUpdated(outputs) {
+ for (const [id, output] of Object.entries(outputs)) {
+ app.graph?.getNodeById(id)?.freevideoShowResult?.(output);
+ }
+ },
+});
+
+api.addEventListener('freevideo_prewarm', event => {
+ const value = event.detail;
+ if (value) app.graph?.getNodeById(value.node)?.freevideoShowPrewarm?.(value);
+});
+
+api.addEventListener('execution_error', event => {
+ app.graph?.getNodeById(event.detail?.node_id)?.freevideoShowFailure?.(event.detail);
+});
+api.addEventListener('executing', event => {
+ const id = typeof event.detail === 'object' ? event.detail?.node : event.detail;
+ if (id != null) {
+ const node = app.graph?.getNodeById(id); node?.freevideoClearFailure?.();
+ if (!node?.freevideoProgress) node?.freevideoShowProgress?.({label: text('Preparing video', '正在准备视频'), reset: true, new_request: true});
+ progressConnection.refresh();
+ } else for (const node of app.graph?._nodes || []) node.freevideoStopProgress?.();
+});
diff --git a/web/generation_progress.css b/web/generation_progress.css
new file mode 100644
index 0000000..b76ddfd
--- /dev/null
+++ b/web/generation_progress.css
@@ -0,0 +1,30 @@
+.fv-generation-progress{box-sizing:border-box;width:100%;max-width:560px;margin:0 auto;padding:18px 22px;border:1px solid var(--fv-border,#303644);border-radius:var(--fv-r-lg);background:var(--fv-card,#171d26);color:var(--fv-ink,#edf2f9);font:13px/1.5 var(--fv-font,system-ui),sans-serif;text-align:center}
+.fv-generation-progress[hidden]{display:none!important}
+.fv-generation-heading{display:flex;align-items:center;justify-content:center;gap:14px;margin-bottom:13px}
+.fv-generation-label{font-size:var(--fv-body);color:var(--fv-muted,#a2abba)}
+.fv-generation-count{font-size:var(--fv-section);font-weight:var(--fv-semibold);line-height:1.2;letter-spacing:.02em;font-variant-numeric:tabular-nums;color:var(--fv-ink,#edf2f9)}
+.fv-generation-track{position:relative;height:6px;background:var(--fv-raised,#212a37);border-radius:999px;overflow:hidden}
+.fv-generation-fill{position:relative;height:100%;border-radius:inherit;overflow:hidden;background:linear-gradient(90deg,var(--fv-accent,#6db8fa),var(--fv-accent-ramp,#82dcd3));transition:width .6s var(--fv-ease-out,cubic-bezier(.22,1,.36,1));will-change:width}
+/* Finished: the success colour fades in over the accent. */
+.fv-generation-fill::before{content:'';position:absolute;inset:0;background:linear-gradient(90deg,var(--fv-success,#76d9b3),#a8eed3);opacity:0;transition:opacity .45s var(--fv-ease,ease)}
+.fv-generation-progress[data-complete=true] .fv-generation-fill::before{opacity:1}
+/* Running: a soft sheen crosses the fill, then rests. */
+.fv-generation-fill::after{content:'';position:absolute;top:0;bottom:0;left:-45%;width:45%;background:linear-gradient(90deg,transparent,#ffffff40,transparent);animation:fv-generation-sheen 2.8s var(--fv-ease,cubic-bezier(.4,0,.2,1)) infinite}
+.fv-generation-progress:is([data-complete=true],[data-counted=false]) .fv-generation-fill::after{animation:none;opacity:0}
+@keyframes fv-generation-sheen{0%{left:-45%}60%,100%{left:100%}}
+/* Unknown: one capsule travels left to right, widest mid-track. */
+.fv-generation-progress[data-counted=false] .fv-generation-fill{position:absolute;top:0;left:-20%;width:20%;transition:none;animation:fv-generation-sweep 1.5s cubic-bezier(.65,0,.35,1) infinite}
+.fv-generation-detail{margin-top:11px;font-size:var(--fv-micro);min-height:18px;overflow-wrap:anywhere}
+.fv-generation-times{display:flex;justify-content:center;flex-wrap:wrap;gap:5px 20px;margin-top:5px;font-size:var(--fv-micro);font-variant-numeric:tabular-nums;color:var(--fv-muted,#a2abba)}
+.fv-generation-note{font-size:var(--fv-micro);color:var(--fv-muted,#a2abba);margin:10px auto 0;max-width:400px;line-height:1.5}
+.fv-generation-silence{font-size:var(--fv-micro);color:var(--fv-muted,#a2abba);margin-top:5px}
+.fv-panel .fv-generation-progress{padding:12px 14px;margin-bottom:12px;border-radius:var(--fv-r-md)}.fv-panel .fv-generation-count{font-size:var(--fv-section)}
+.fv-generation-compact .fv-generation-heading{justify-content:space-between;gap:8px;margin-bottom:12px;align-items:baseline}
+.fv-generation-compact .fv-generation-label{color:var(--fv-ink,#edf2f9);font-size:14px}
+.fv-generation-compact .fv-generation-count{font-size:14px;font-weight:var(--fv-medium);letter-spacing:0;color:var(--fv-muted,#a2abba)}
+.fv-generation-compact .fv-generation-track{height:4px;background:#ffffff14}
+.fv-generation-compact .fv-generation-times{justify-content:flex-end;margin-top:10px;font-size:12px}
+.fv-generation-compact .fv-generation-times>span:first-child,.fv-generation-compact .fv-generation-detail,.fv-generation-compact .fv-generation-silence,.fv-generation-compact .fv-generation-note:not(.fv-generation-retry){display:none}
+.fv-generation-compact .fv-generation-retry{font-size:12px;margin-top:14px}
+@keyframes fv-generation-sweep{0%{left:-20%;width:20%}50%{left:33%;width:34%}100%{left:100%;width:20%}}
+@media(prefers-reduced-motion:reduce){.fv-generation-fill,.fv-generation-fill::before,.fv-generation-fill::after{transition:none!important;animation:none!important}.fv-generation-fill::after{opacity:0}.fv-generation-progress[data-counted=false] .fv-generation-fill{opacity:.5}}
diff --git a/web/generation_progress.js b/web/generation_progress.js
new file mode 100644
index 0000000..e4ed044
--- /dev/null
+++ b/web/generation_progress.js
@@ -0,0 +1,247 @@
+// One whole-video estimate shared by Studio and node view; real stage counters remain separate.
+const css = document.createElement('link');
+css.rel = 'stylesheet'; css.href = new URL('./generation_progress.css', import.meta.url).href;
+document.head.append(css);
+const el = (tag, cls) => { const node = document.createElement(tag); node.className = cls; return node; };
+const valid = value => Number.isFinite(value) && value >= 0;
+const duration = value => {
+ const seconds = Math.floor(value);
+ return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, '0')}`;
+};
+
+export function createGenerationProgress(t, now = () => Date.now(), {compact = false} = {}) {
+ const element = el('section', 'fv-generation-progress'); element.hidden = true;
+ if (compact) element.classList.add('fv-generation-compact');
+ const heading = el('div', 'fv-generation-heading'); heading.setAttribute('role', 'status');
+ const label = el('span', 'fv-generation-label'), count = el('strong', 'fv-generation-count');
+ heading.append(label, count);
+ const track = el('div', 'fv-generation-track'), fill = el('div', 'fv-generation-fill');
+ track.setAttribute('role', 'progressbar'); track.setAttribute('aria-valuemin', '0'); track.append(fill);
+ const detail = el('div', 'fv-generation-detail');
+ const times = el('div', 'fv-generation-times');
+ const elapsed = el('span', ''), eta = el('span', ''); times.append(elapsed, eta);
+ const note = el('p', 'fv-generation-note');
+ const recovery = el('p', 'fv-generation-note fv-generation-retry'); recovery.hidden = true;
+ recovery.setAttribute('role', 'status');
+ const silence = el('div', 'fv-generation-silence'); silence.hidden = true;
+ element.append(heading, track, detail, times, silence, recovery, note);
+ let state = {}, overall = {}, started = 0, sampledAt = 0, overallAt = 0, timer = null;
+ let shownFraction = null;
+ // The visible percentage counts toward its value with the bar instead of
+ // jumping. Accessible values are set exactly and immediately elsewhere.
+ let countShown = null, countTarget = null, countFrom = 0, countStart = 0, countFrame = null, countPrefix = '';
+ const moving = () => typeof requestAnimationFrame === 'function'
+ && !(typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches);
+ function stopCount() { if (countFrame !== null) cancelAnimationFrame(countFrame); countFrame = null; }
+ function showPercent(percent, prefix) {
+ countPrefix = prefix;
+ if (countFrame !== null && percent === countTarget) return;
+ countTarget = percent;
+ if (countShown === null || !moving() || Math.abs(percent - countShown) <= 1) {
+ stopCount(); countShown = percent; count.textContent = `${prefix}${percent}%`; return;
+ }
+ countFrom = countShown; countStart = performance.now();
+ const step = time => {
+ const k = Math.min(1, Math.max(0, (time - countStart) / 600)), eased = 1 - Math.pow(1 - k, 4);
+ countShown = Math.round(countFrom + (countTarget - countFrom) * eased);
+ count.textContent = `${countPrefix}${countShown}%`;
+ countFrame = k < 1 ? requestAnimationFrame(step) : null;
+ };
+ if (countFrame === null) countFrame = requestAnimationFrame(step);
+ }
+
+ const fraction = value => Math.min(1, Math.max(0, value));
+ const newRequest = message => message.new_request === true || message.overall?.reset === true
+ || (message.reset === true && !message.phase);
+ const counted = value => Number.isInteger(value.total) && value.total > 0 && Number.isInteger(value.done)
+ && value.done >= 0 && value.done <= value.total;
+
+ function drawOverall() {
+ // Keep compatibility with older workers while they are being updated.
+ // Once `overall` is present, this branch is never used and the bar is
+ // the monotonic whole-video estimate.
+ if (!Object.keys(overall).length) {
+ if (!counted(state)) {
+ element.dataset.counted = 'false'; element.dataset.complete = 'false'; count.textContent = ''; fill.style.width = '';
+ track.removeAttribute('aria-valuenow'); track.removeAttribute('aria-valuemax');
+ return;
+ }
+ const raw = valid(state.display_fraction) ? fraction(state.display_fraction) : state.done / state.total;
+ const percent = Math.floor(100 * raw);
+ element.dataset.counted = 'true'; element.dataset.complete = 'false'; count.textContent = compact ? `${percent}%` : `${state.done} / ${state.total}`;
+ element.style.setProperty('--fv-progress', `${100 * raw}%`);
+ fill.style.width = `${100 * raw}%`; track.setAttribute('aria-valuemax', state.total);
+ track.setAttribute('aria-valuenow', state.done); track.setAttribute('aria-label', t('Completed sampling steps', '已完成采样步数'));
+ const base = detail.dataset.baseText || '';
+ if (base) detail.textContent = `${base} · ${state.estimated ? '~' : ''}${percent}%`;
+ return;
+ }
+ const complete = overall.status === 'complete';
+ let candidate = valid(overall.fraction) ? fraction(overall.fraction) : null;
+ const canEstimate = overall.status !== 'failed' && overall.estimated === true
+ && valid(overall.phase_start_fraction) && valid(overall.phase_weight);
+ if (canEstimate) {
+ let local = null;
+ if (state.phase === 'sampling' && counted(state) && !overall.sampling_time_weighted) {
+ local = valid(state.display_fraction) ? fraction(state.display_fraction) : state.done / state.total;
+ if (state.done < state.total && valid(state.estimated_step_seconds) && state.estimated_step_seconds > 0) {
+ const stepElapsed = (valid(state.step_elapsed_seconds) ? state.step_elapsed_seconds : 0)
+ + Math.max(0, (now() - sampledAt) / 1000);
+ local = Math.max(local, (state.done + Math.min(.88, .88 * stepElapsed / state.estimated_step_seconds)) / state.total);
+ } else if (valid(overall.phase_seconds) && overall.phase_seconds > 0
+ && valid(overall.phase_elapsed_seconds)) {
+ // Before the first warm NFE supplies a per-step duration,
+ // use the report-weighted sampling estimate as a smooth
+ // visual guide. The synchronized NFE counter remains the
+ // source of truth and this estimate stops short of 100%.
+ const phaseElapsed = overall.phase_elapsed_seconds
+ + Math.max(0, (now() - overallAt) / 1000);
+ local = Math.max(local, Math.min(.95, phaseElapsed / overall.phase_seconds));
+ }
+ } else if (valid(overall.phase_seconds) && overall.phase_seconds > 0 && valid(overall.phase_elapsed_seconds)) {
+ const phaseElapsed = overall.phase_elapsed_seconds + Math.max(0, (now() - overallAt) / 1000);
+ // Time can move the estimate, but only an engine transition
+ // finishes a phase. Long phases stop short of their boundary.
+ local = Math.min(.95, phaseElapsed / overall.phase_seconds);
+ }
+ if (local !== null) candidate = Math.max(candidate ?? 0,
+ canEstimate ? overall.phase_start_fraction + overall.phase_weight * local : local);
+ }
+ // A retry or a new phase cannot rewind the whole-video bar. Even a
+ // complete sampling phase is not a completed/saved video.
+ if (complete) shownFraction = 1;
+ else if (candidate !== null) shownFraction = Math.max(shownFraction ?? 0, Math.min(.99, fraction(candidate)));
+ const known = shownFraction !== null;
+ element.dataset.counted = String(known);
+ element.dataset.complete = String(complete);
+ track.setAttribute('aria-label', t('Estimated overall video progress', '视频整体预估进度'));
+ track.setAttribute('aria-valuemax', '100');
+ if (known) {
+ const percent = complete ? 100 : Math.floor(shownFraction * 100);
+ showPercent(percent, complete || compact ? '' : '≈ ');
+ element.style.setProperty('--fv-progress', `${100 * shownFraction}%`);
+ fill.style.width = `${100 * shownFraction}%`;
+ track.setAttribute('aria-valuenow', String(percent));
+ track.setAttribute('aria-valuetext', `${complete || compact ? '' : '≈ '}${percent}%`);
+ } else {
+ stopCount(); countShown = null;
+ count.textContent = '';
+ fill.style.width = '';
+ track.removeAttribute('aria-valuenow'); track.removeAttribute('aria-valuetext');
+ }
+ }
+
+ function tick() {
+ if (element.hidden) return;
+ const ended = overall.status === 'complete' || overall.status === 'failed';
+ const age = Math.max(0, (now() - sampledAt) / 1000);
+ const overallAge = ended ? 0 : Math.max(0, (now() - overallAt) / 1000);
+ const seconds = valid(overall.elapsed_seconds) ? overall.elapsed_seconds + overallAge : (now()-started)/1000;
+ elapsed.textContent = `${t('Elapsed', '已用时')} ${duration(Math.max(0, seconds))}`;
+ let left = valid(overall.remaining_seconds) ? Math.max(
+ valid(overall.remaining_floor_seconds) ? overall.remaining_floor_seconds : 0,
+ overall.remaining_seconds - overallAge) : null;
+ if (!Object.keys(overall).length && left === null && state.phase === 'sampling' && counted(state)
+ && state.uniform_remaining_steps !== false
+ && valid(state.estimated_step_seconds) && state.estimated_step_seconds > 0) {
+ const stepElapsed = (valid(state.step_elapsed_seconds) ? state.step_elapsed_seconds : 0)
+ + Math.max(0, (now() - sampledAt) / 1000);
+ left = Math.max(0, (state.total - state.done) * state.estimated_step_seconds - stepElapsed);
+ }
+ eta.hidden = compact && (ended || left === null || left <= 0);
+ eta.textContent = compact && left > 0
+ ? left >= 60 ? t(`About ${Math.ceil(left / 60)} min left`, `约剩 ${Math.ceil(left / 60)} 分钟`)
+ : t(`About ${Math.ceil(left)} sec left`, `约剩 ${Math.ceil(left)} 秒`)
+ : overall.status === 'complete' ? t('Video saved', '视频已保存')
+ : overall.status === 'failed' ? t('Stopped', '已停止')
+ : left !== null && left > 0 ? `${t('Remaining', '预计剩余')} ≈ ${duration(left)}`
+ : t('Estimating remaining time…', '正在估算剩余时间…');
+ silence.hidden = ended || age < 15;
+ silence.textContent = t(`Last engine update ${duration(age)} ago`, `距上次引擎进度更新 ${duration(age)}`);
+ drawOverall();
+ }
+ function update(message) {
+ // Older servers may still forward this diagnostic-only event. It is
+ // not a stage change or something the user needs to act on.
+ if (message.warning === 'ram_budget_warning' && !message.phase) return;
+ const reset = newRequest(message);
+ if (element.hidden || reset) {
+ state = {}; overall = {}; started = now(); overallAt = now(); shownFraction = null;
+ stopCount(); countShown = null;
+ recovery.hidden = true; recovery.textContent = '';
+ }
+ if (!reset && message.retry && typeof message.retry === 'object') {
+ const retry = message.retry;
+ const previous = retry.failed_phase === 'decode' ? t('The previous decode', '上一次解码')
+ : retry.failed_phase === 'sample_finalize' ? t('Finalizing the previous sampling result', '上一次采样后的收尾')
+ : ['sample', 'sampling'].includes(retry.failed_phase) ? t('The previous sampling attempt', '上一次采样')
+ : t('The previous attempt', '上一次尝试');
+ const reason = retry.kind === 'gpu_oom' ? t(`${previous} did not fit in GPU memory.`, `${previous}的显存空间不足。`)
+ : retry.kind === 'ram_pressure' ? t(`${previous} did not have enough RAM.`, `${previous}的内存空间不足。`)
+ : t(`${previous} could not finish.`, `${previous}未完成。`);
+ const action = t('Adjusting memory use and retrying this video.', '正在调整内存安排并重试本次生成。');
+ const sampling = retry.reuse_sampling === true ? t('Sampling is saved; retrying decoding only.', '采样已保留,仅重试解码。')
+ : retry.reuse_sampling === false ? t('Sampling will run again before decoding.', '将重新采样后解码。') : '';
+ const attempt = Number.isInteger(retry.attempt) && retry.attempt > 1
+ && Number.isInteger(retry.max_attempts) && retry.max_attempts >= retry.attempt
+ ? t(`Attempt ${retry.attempt} of ${retry.max_attempts}.`, `第 ${retry.attempt} / ${retry.max_attempts} 次尝试。`) : '';
+ const explanation = [reason, action, sampling, attempt].filter(Boolean).join(' ');
+ // Reopening a view may replay the retry on every progress event.
+ // Keep its live region quiet until the explanation actually changes.
+ if (recovery.textContent !== explanation) recovery.textContent = explanation;
+ recovery.hidden = false;
+ }
+ const phase = message.phase || message.label;
+ if (message.reset || phase !== (state.phase || state.label)) state = {};
+ state = {...state, ...message}; sampledAt = valid(message.received_at) ? message.received_at : now();
+ if (message.overall && typeof message.overall === 'object') {
+ overall = {...message.overall}; overallAt = sampledAt;
+ }
+ element.hidden = false;
+ const sampling = message.phase === 'sampling';
+ const complete = message.phase === 'complete' || overall.status === 'complete';
+ const hasCount = counted(message);
+ label.textContent = complete ? t('Video saved', '视频已保存') : sampling ? t('Sampling', '采样')
+ : message.phase === 'recovery' ? t('Retrying this video', '正在重试本次生成')
+ : message.phase === 'sample_finalize' ? {
+ latent_validation: t('Checking completed sampling', '正在检查采样结果'),
+ latent_save: t('Saving completed sampling', '正在保存采样结果'),
+ offload_release: t('Releasing sampling buffers', '正在释放采样缓冲'),
+ transformer_release: t('Preparing video decoding', '正在准备视频解码'),
+ }[message.stage] || t('Preparing video decoding', '正在准备视频解码')
+ : message.label || t('Preparing video', '正在准备视频');
+ if (compact && !complete) {
+ const phase = message.phase || message.timing_phase;
+ label.textContent = sampling ? `${t('Sampling', '采样')} ${message.done} / ${message.total}`
+ : phase === 'encoding' ? t('Preparing prompt', '准备提示词')
+ : phase === 'load' ? t('Loading model', '加载模型')
+ : phase === 'decode' ? message.label === 'Saving MP4 and audio' ? t('Saving video', '保存视频')
+ : message.label === 'Loading and decoding audio' ? t('Decoding audio', '解码音频')
+ : t('Decoding video', '解码视频')
+ : phase === 'sample_finalize' ? t('Preparing output', '准备输出')
+ : phase === 'recovery' ? t('Adjusting memory · retrying', '调整内存 · 重试中')
+ : t('Preparing video', '准备生成');
+ }
+ if (sampling && Number.isInteger(message.block)) {
+ detail.textContent = t(`Step ${message.done+1} · processing layer ${message.block} / ${message.blocks}`,
+ `第 ${message.done+1} 步 · 正在处理第 ${message.block} / ${message.blocks} 层`);
+ } else if (sampling) {
+ detail.textContent = message.done === message.total ? t('Sampling complete · preparing output', '采样完成 · 正在准备输出')
+ : message.stage === 'inputs' ? t('Preparing sampling inputs', '正在准备采样输入')
+ : message.done > 0 ? t(`${message.done} steps complete`, `已完成 ${message.done} 步`)
+ : t('Starting the first step', '正在开始第一步');
+ } else detail.textContent = message.detail || '';
+ if (hasCount) detail.textContent = `${message.done} / ${message.total}${detail.textContent ? ' · ' + detail.textContent : ''}`;
+ detail.dataset.baseText = detail.textContent;
+ note.hidden = !(sampling && message.done === 0);
+ note.textContent = t('New kernels may compile on the first step. Disk caches speed up later runs.',
+ '首次遇到的新内核可能在第一步编译,缓存会保存在硬盘上供后续复用。');
+ tick(); if (timer === null) timer = setInterval(tick, 250);
+ }
+ function hide() {
+ element.hidden = true; recovery.hidden = true; recovery.textContent = '';
+ if (timer !== null) clearInterval(timer); timer = null; state = {}; overall = {}; shownFraction = null;
+ stopCount(); countShown = null;
+ }
+ return {element, update, hide, dispose: hide};
+}
diff --git a/web/launcher.js b/web/launcher.js
new file mode 100644
index 0000000..7ce70cd
--- /dev/null
+++ b/web/launcher.js
@@ -0,0 +1,23 @@
+import { app } from '../../scripts/app.js';
+import { api } from '../../scripts/api.js';
+
+// Explicit launcher navigation only; ordinary ComfyUI visits restore their own
+// graph. Remove the flag before loading so refreshing never resets user edits.
+app.registerExtension({
+ name: 'FreeVideo.Launcher',
+ async setup() {
+ const url = new URL(window.location.href);
+ if (url.searchParams.get('freevideo') !== 'launch') return;
+ url.searchParams.delete('freevideo');
+ window.history.replaceState(window.history.state, '', url);
+ try {
+ const response = await api.fetchApi('/freevideo/launcher/workflow');
+ if (!response.ok) throw new Error(`FreeVideo workflow: HTTP ${response.status}`);
+ const workflow = await response.json();
+ await app.loadGraphData(workflow, true, true, 'FreeVideo');
+ } catch (error) {
+ app.extensionManager?.toast?.add({severity: 'error', summary: 'FreeVideo', detail: String(error), life: 15000});
+ console.error('FreeVideo launcher:', error);
+ }
+ },
+});
diff --git a/web/library.css b/web/library.css
new file mode 100644
index 0000000..64f4241
--- /dev/null
+++ b/web/library.css
@@ -0,0 +1,31 @@
+.fv-studio.fv-library{width:min(1120px,calc(100vw - 48px));height:min(800px,calc(100dvh - 48px));overflow:hidden}
+.fv-library .fv-library-header{display:grid;grid-template-columns:1fr auto 1fr;align-items:center;gap:12px;padding:18px 22px;border-bottom:1px solid var(--fv-border)}
+.fv-library-header h2{font-size:var(--fv-strong);font-weight:var(--fv-semibold);margin:0}.fv-library-header>button:first-child{justify-self:start}.fv-library-header>button:last-child{justify-self:end}
+.fv-library-body{height:calc(100% - 75px);display:grid;grid-template-columns:320px minmax(0,1fr);min-height:0}
+.fv-library-collection{overflow:auto;overscroll-behavior:contain;scrollbar-width:thin;padding:18px;border-right:1px solid var(--fv-border);text-align:center}
+.fv-library-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:14px 10px}
+.fv-library .fv-library-card{display:flex;flex-direction:column;align-items:stretch;gap:4px;padding:5px 5px 9px;min-width:0;border:1px solid transparent;background:transparent;border-radius:var(--fv-r-md);text-align:left;animation:fv-panel-enter .3s ease;transition:background .2s,border-color .2s,transform .24s var(--fv-ease-out)}
+.fv-library .fv-library-card:hover{transform:translateY(-2px)}
+.fv-library .fv-library-card:hover{border-color:var(--fv-sheen);background:var(--fv-card)}
+.fv-library .fv-library-card[aria-pressed=true]{border-color:var(--fv-accent);background:var(--fv-selected)}
+.fv-library-card>span{font-size:12px;padding:2px 4px 0}.fv-library-card>small{font-size:10px;color:var(--fv-muted);padding:0 4px}
+.fv-library-picture{aspect-ratio:16/10;display:grid;place-items:center;overflow:hidden;border-radius:var(--fv-r-sm);background:linear-gradient(145deg,#24394f,#17202d)}
+.fv-library-picture img{width:100%;height:100%;object-fit:contain;transition:transform .5s var(--fv-ease-out)}
+.fv-library-card:hover .fv-library-picture img{transform:scale(1.03)}
+.fv-library-message{color:var(--fv-muted);font-size:13px;line-height:1.8;text-align:center;margin:20px 8px}.fv-library-message:empty{display:none}
+.fv-library-detail{padding:30px;display:flex;flex-direction:column;gap:18px;min-width:0;min-height:0;overflow:auto;background:radial-gradient(ellipse at 50% 30%,#6db8fa12,transparent 70%)}
+.fv-library-frame{display:grid;place-items:center;min-height:160px;flex:1;overflow:hidden;border-radius:var(--fv-r-md);background:#0d1118;box-shadow:0 12px 40px #0003}
+.fv-library-frame:empty{visibility:hidden}.fv-library-frame video{width:100%;height:100%;max-height:100%;min-height:0;object-fit:contain;animation:fv-preview-appear .25s ease}
+.fv-library-caption{display:flex;flex-direction:column;align-items:center;gap:4px;font-size:12px;color:var(--fv-muted)}
+.fv-library-caption strong{font-size:14px;font-weight:var(--fv-medium);color:var(--fv-ink)}
+.fv-library .fv-library-all{display:none}
+.fv-studio .fv-preview-tools{display:flex;align-items:center;gap:12px}.fv-studio .fv-preview-tools button{min-height:var(--fv-h-sm);padding:5px 12px}
+@media(max-width:900px){.fv-library-body{grid-template-columns:280px minmax(0,1fr)}.fv-library-detail{padding:20px}}
+@media(max-width:680px){
+ .fv-studio.fv-library{width:calc(100vw - 16px);height:calc(100dvh - 16px)}
+ .fv-library .fv-library-header{padding:12px 8px;gap:4px}.fv-library-header h2{font-size:14px}
+ .fv-library-body{height:calc(100% - 63px);display:flex;flex-direction:column}
+ .fv-library-collection{flex:1;border:0}.fv-library-grid{grid-template-columns:repeat(auto-fill,minmax(135px,1fr))}
+ .fv-library-detail{flex:1;display:none;gap:14px;padding:14px}.fv-library .fv-library-all{display:inline-flex;align-self:flex-start}
+ .fv-library[data-detail=true] .fv-library-detail{display:flex}.fv-library[data-detail=true] .fv-library-collection{display:none}
+}
diff --git a/web/library.js b/web/library.js
new file mode 100644
index 0000000..7c77c81
--- /dev/null
+++ b/web/library.js
@@ -0,0 +1,149 @@
+import { api } from '../../scripts/api.js';
+import { closeDialog } from './motion.js';
+
+const style = document.createElement('link');
+style.rel = 'stylesheet'; style.href = new URL('./library.css', import.meta.url).href; document.head.append(style);
+const el = (tag, text, cls) => { const e = document.createElement(tag); if (text != null) e.textContent = text; if (cls) e.className = cls; return e; };
+const button = (text, action, cls = '') => { const b = el('button', text, cls); b.type = 'button'; b.onclick = action; return b; };
+const view = file => {
+ const split = file.lastIndexOf('/');
+ return api.apiURL('/view?' + new URLSearchParams({filename: file.slice(split + 1), subfolder: file.slice(0, split), type: 'output'}));
+};
+let opened;
+
+export async function latestVideo() {
+ const response = await api.fetchApi('/freevideo/library?limit=1');
+ if (!response.ok) return null;
+ return (await response.json()).items?.[0] || null;
+}
+
+export function openLibrary(t) {
+ if (opened?.open) { opened.focus(); return; }
+ const dialog = el('dialog', null, 'fv-studio fv-library'); opened = dialog;
+ dialog.setAttribute('aria-label', t('Your creations', '我的作品'));
+ const header = el('header', null, 'fv-library-header');
+ const back = button(t('Back to create', '返回创作'), () => closeDialog(dialog), 'fv-quiet');
+ const title = el('h2', t('Your creations', '我的作品'));
+ const refresh = button(t('Refresh', '刷新'), () => load(false), 'fv-quiet');
+ header.append(back, title, refresh);
+ const body = el('div', null, 'fv-library-body');
+ const collection = el('section', null, 'fv-library-collection'); collection.setAttribute('aria-label', t('Saved videos', '已保存的视频'));
+ const grid = el('div', null, 'fv-library-grid');
+ const message = el('p', '', 'fv-library-message'); message.setAttribute('role', 'status');
+ const more = button(t('Show more', '加载更多'), () => load(true), 'fv-quiet'); more.hidden = true;
+ collection.append(grid, message, more);
+ const detail = el('section', null, 'fv-library-detail');
+ const all = button(t('All creations', '全部作品'), () => {
+ dialog.dataset.detail = 'false'; player?.pause(); cards.get(selected)?.focus({preventScroll: true});
+ }, 'fv-quiet fv-library-all');
+ const frame = el('div', null, 'fv-library-frame');
+ const caption = el('div', null, 'fv-library-caption');
+ const date = el('strong'), geometry = el('span'); caption.append(date, geometry);
+ const links = el('div', null, 'fv-result-links');
+ detail.append(all, frame, caption, links);
+ body.append(collection, detail); dialog.append(header, body);
+ let disposed = false, loading = false, next = null, selected = null, player = null, arrivals = [];
+ const rows = new Map(), cards = new Map();
+ const abort = new AbortController();
+ const narrow = matchMedia('(max-width: 680px)');
+ const timestamp = row => {
+ const value = new Date(row.created_at);
+ return Number.isNaN(value.getTime()) ? t('Saved video', '已保存的视频')
+ : value.toLocaleString(undefined, {month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit'});
+ };
+ const dimensions = row => {
+ const g = row.geometry || {}, parts = [];
+ if (g.width && g.height) parts.push(`${g.width} × ${g.height}`);
+ const duration = g.seconds || (g.frames && g.frames / (g.fps || 24));
+ if (duration) parts.push(`${Number(duration).toFixed(1)} s`);
+ return parts.join(' · ');
+ };
+ function releasePlayer() {
+ if (!player) return;
+ player.pause(); player.removeAttribute('src'); player.load(); player = null;
+ }
+ function select(row, reveal = true) {
+ if (selected !== row.video) {
+ releasePlayer(); frame.replaceChildren();
+ selected = row.video;
+ player = el('video'); player.controls = true; player.preload = 'metadata'; player.playsInline = true;
+ player.src = view(row.video); frame.append(player);
+ player.addEventListener('error', () => {
+ if (!disposed && selected === row.video) {
+ const note = el('p', t('This video is unavailable. Refresh if it was moved.', '视频暂时无法打开,若文件已移动请刷新。'), 'fv-library-message');
+ frame.replaceChildren(note);
+ }
+ }, {once: true});
+ date.textContent = timestamp(row); geometry.textContent = dimensions(row);
+ links.replaceChildren();
+ for (const [label, file, cls] of [[t('Download video', '下载视频'), row.video, 'fv-primary'], [t('View report', '查看报告'), row.report, 'fv-quiet']]) {
+ if (!file) continue;
+ const a = el('a', label, cls); a.href = view(file); a.download = file.split('/').pop(); links.append(a);
+ }
+ for (const [file, card] of cards) card.setAttribute('aria-pressed', String(file === selected));
+ }
+ if (reveal) dialog.dataset.detail = 'true';
+ }
+ const thumbnails = new IntersectionObserver(entries => {
+ for (const {target, isIntersecting} of entries) if (isIntersecting) {
+ target.src = target.dataset.src; thumbnails.unobserve(target);
+ }
+ }, {root: collection, rootMargin: '80px'});
+ function append(row, first = false) {
+ if (!row?.video) return;
+ rows.set(row.video, row);
+ if (cards.has(row.video)) return;
+ const card = button('', () => select(rows.get(row.video)), 'fv-library-card');
+ card.setAttribute('aria-label', `${timestamp(row)} · ${dimensions(row)}`);
+ card.setAttribute('aria-pressed', String(row.video === selected));
+ const picture = el('div', null, 'fv-library-picture');
+ const image = el('img'); image.alt = ''; image.decoding = 'async';
+ const id = row.id || row.video.replace(/^FreeVideo\//, '').replace(/\/video\.mp4$/, '');
+ image.dataset.src = api.apiURL('/freevideo/library/thumbnail?' + new URLSearchParams({id}));
+ image.onerror = () => { image.hidden = true; };
+ picture.append(image); card.append(picture, el('span', timestamp(row)), el('small', dimensions(row)));
+ cards.set(row.video, card); first ? grid.prepend(card) : grid.append(card); thumbnails.observe(image);
+ }
+ async function load(appendPage) {
+ if (loading || disposed) return;
+ loading = true; arrivals = []; refresh.disabled = more.disabled = true;
+ message.textContent = rows.size ? '' : t('Loading your creations…', '正在读取作品…');
+ try {
+ const query = new URLSearchParams({limit: '24'});
+ if (appendPage && next) query.set('before', next);
+ const response = await api.fetchApi('/freevideo/library?' + query, {signal: abort.signal});
+ if (response.status === 404) throw new Error(t('Restart ComfyUI to open your creations after updating.', '更新后重启 ComfyUI,即可打开作品。'));
+ if (!response.ok) throw new Error(t('Could not load your creations. Try Refresh.', '作品暂时无法读取,请点击刷新。'));
+ const data = await response.json(); if (disposed) return;
+ if (!appendPage) { thumbnails.disconnect(); grid.replaceChildren(); cards.clear(); rows.clear(); }
+ for (const row of data.items || []) append(row);
+ for (const row of arrivals) append(row, true);
+ next = data.next; more.hidden = !next;
+ message.textContent = rows.size ? '' : t('Your finished videos will appear here.', '生成完成的视频会保存在这里。');
+ if (!rows.has(selected)) {
+ releasePlayer(); selected = null; frame.replaceChildren(); links.replaceChildren(); date.textContent = geometry.textContent = '';
+ if (!narrow.matches && rows.size) select(rows.values().next().value, false);
+ else dialog.dataset.detail = 'false';
+ }
+ } catch (error) { if (!disposed && error.name !== 'AbortError') message.textContent = error.message; }
+ finally { if (!disposed) { loading = false; refresh.disabled = more.disabled = false; } }
+ }
+ const completed = event => {
+ const row = event.detail?.value;
+ if (!row?.video || disposed) return;
+ const saved = {...row, created_at: row.created_at || new Date().toISOString()};
+ if (loading) arrivals.push(saved);
+ append(saved, true);
+ message.textContent = '';
+ // Do not interrupt playback of a previous video when a new one arrives.
+ if (!selected && !narrow.matches) select(rows.get(row.video), false);
+ };
+ window.addEventListener('freevideo-result', completed);
+ dialog.addEventListener('cancel', event => { event.preventDefault(); closeDialog(dialog); });
+ dialog.onclose = () => {
+ disposed = true; abort.abort(); thumbnails.disconnect(); releasePlayer();
+ window.removeEventListener('freevideo-result', completed); dialog.remove();
+ if (opened === dialog) opened = null;
+ };
+ document.body.append(dialog); dialog.showModal(); load(false);
+}
diff --git a/web/motion.js b/web/motion.js
new file mode 100644
index 0000000..bcf5797
--- /dev/null
+++ b/web/motion.js
@@ -0,0 +1,38 @@
+const reduceMotion = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
+
+export function closeDialog(dialog) {
+ if (!dialog.open || dialog.dataset.closing === 'true') return;
+ if (reduceMotion() || !dialog.animate) { dialog.close(); return; }
+ dialog.dataset.closing = 'true';
+ dialog.animate([{opacity: 1, transform: 'translateY(0) scale(1)'},
+ {opacity: 0, transform: 'translateY(6px) scale(.99)'}],
+ {duration: 170, easing: 'ease-in', fill: 'forwards'}).finished.then(() => dialog.close(), () => {});
+}
+
+// Keep native details/summary semantics, including keyboard clicks, while
+// animating their measured height in both directions and on rapid toggles.
+export function animateDetails(details) {
+ const summary = details.querySelector('summary');
+ let animation = null, expanded = details.open;
+ const click = event => {
+ if (reduceMotion() || !details.animate) return;
+ event.preventDefault();
+ const start = details.getBoundingClientRect().height;
+ expanded = animation ? !expanded : !details.open;
+ animation?.cancel();
+ details.open = expanded;
+ const end = details.getBoundingClientRect().height;
+ details.open = true; details.dataset.expanded = String(expanded);
+ details.style.overflow = 'hidden';
+ const current = details.animate([{height: `${start}px`}, {height: `${end}px`}],
+ {duration: 240, easing: 'cubic-bezier(.2,.7,.2,1)', fill: 'both'});
+ animation = current;
+ current.finished.then(() => {
+ if (animation !== current) return;
+ details.open = expanded; current.cancel(); animation = null;
+ details.style.overflow = ''; delete details.dataset.expanded;
+ }, () => {});
+ };
+ summary.addEventListener('click', click);
+ return () => { summary.removeEventListener('click', click); animation?.cancel(); };
+}
diff --git a/web/preview_scene.js b/web/preview_scene.js
new file mode 100644
index 0000000..2a79ed2
--- /dev/null
+++ b/web/preview_scene.js
@@ -0,0 +1,119 @@
+// A small, local canvas scene. The sea keeps moving while the water level follows
+// the existing estimate; animation never advances the reported progress itself.
+export function createPreviewScene(stage, progress, media) {
+ const canvas = document.createElement('canvas');
+ canvas.className = 'fv-preview-scene'; canvas.setAttribute('aria-hidden', 'true');
+ stage.prepend(canvas);
+ const ctx = canvas.getContext('2d', {alpha: false});
+ if (!ctx) return () => canvas.remove();
+ const reduced = matchMedia('(prefers-reduced-motion: reduce)');
+ let width = 0, height = 0, frame = null, last = null, disposed = false;
+ let target = .16, level = target, visible = true, generating = false, phase = 0;
+
+ function wave(x, layer) {
+ const amplitude = Math.min(24, height * .075) * (reduced.matches ? .6 : 1);
+ const drift = phase * (1 + layer * .12);
+ const crest = Math.sin(x * Math.PI * 3.2 - drift + layer * 1.7);
+ const swell = Math.sin(x * Math.PI * 5.4 + drift * .7 + layer * .8);
+ const breath = .82 + .18 * Math.sin(phase * .55 + x * Math.PI * 2 + layer);
+ return height * (1 - level) + (layer - 1) * amplitude * .32
+ + amplitude * breath * (.7 * crest + .3 * swell);
+ }
+
+ function paint() {
+ ctx.clearRect(0, 0, width, height);
+ const sky = ctx.createLinearGradient(0, 0, width * .6, height);
+ sky.addColorStop(0, '#0c1420'); sky.addColorStop(.55, '#101f30'); sky.addColorStop(1, '#152c41');
+ ctx.fillStyle = sky; ctx.fillRect(0, 0, width, height);
+ const glow = ctx.createRadialGradient(width * .7, height * .85, 0, width * .7, height * .85, width * .85);
+ glow.addColorStop(0, '#477db822'); glow.addColorStop(1, '#477db800');
+ ctx.fillStyle = glow; ctx.fillRect(0, 0, width, height);
+ for (let layer = 0; layer < 3; layer++) {
+ const points = [];
+ for (let i = 0; i <= 56; i++) points.push([width * i / 56, wave(i / 56, layer)]);
+ ctx.beginPath(); ctx.moveTo(...points[0]);
+ for (const point of points.slice(1)) ctx.lineTo(...point);
+ ctx.lineTo(width, height); ctx.lineTo(0, height); ctx.closePath();
+ const water = ctx.createLinearGradient(0, height * (1 - level) - 24, width * .25, height);
+ water.addColorStop(0, ['#659fbc38', '#418cae80', '#346790c9'][layer]);
+ water.addColorStop(1, ['#24466538', '#19334fbb', '#172e49'][layer]);
+ ctx.fillStyle = water; ctx.fill();
+ ctx.save(); ctx.clip();
+ // Broad, faint reflections rise through the water, with different
+ // phases so there is no synchronized reset or marching pattern.
+ if (layer === 2) {
+ for (let i = 0; i < 3; i++) {
+ const travel = ((phase * .105 + i / 3) % 1 + 1) % 1;
+ const y = height * (1.3 - travel * 1.55);
+ const alpha = Math.sin(travel * Math.PI) * .13;
+ ctx.beginPath();
+ ctx.moveTo(-width * .2, y + height * .16);
+ ctx.bezierCurveTo(width * .2, y - height * .25, width * .6, y + height * .28, width * 1.2, y - height * .12);
+ for (const [size, opacity] of [[.09, .18], [.04, .28], [.002, .85]]) {
+ ctx.strokeStyle = `rgba(176,225,248,${alpha * opacity})`;
+ ctx.lineWidth = Math.max(.8, height * size); ctx.stroke();
+ }
+ }
+ }
+ ctx.restore();
+ ctx.beginPath(); ctx.moveTo(...points[0]);
+ for (const point of points.slice(1)) ctx.lineTo(...point);
+ ctx.strokeStyle = ['#b2dcf03d', '#b3edff4d', '#a9dcef66'][layer];
+ ctx.lineWidth = .85; ctx.stroke();
+ }
+ const shade = ctx.createRadialGradient(width / 2, height / 2, 0, width / 2, height / 2, Math.max(width, height) * .68);
+ shade.addColorStop(0, '#060c1700'); shade.addColorStop(1, '#060c174d');
+ ctx.fillStyle = shade; ctx.fillRect(0, 0, width, height);
+ }
+
+ function draw(time) {
+ frame = null;
+ if (disposed || !visible || document.hidden || !width || !height) { last = null; return; }
+ // Keep a gentle live surface even on remote Windows desktops that ask
+ // for reduced motion. That preference lowers speed/amplitude; it must
+ // not turn a running request into a frozen picture sliding upwards.
+ const moving = generating || !reduced.matches;
+ if (moving && last !== null && time - last < 1000 / (reduced.matches ? 15 : 30)) { frame = requestAnimationFrame(draw); return; }
+ const elapsed = last === null ? 0 : Math.max(0, (time - last) / 1000); last = time;
+ // Use elapsed time for the surface. Clamping it to 100 ms made waves
+ // nearly freeze when a busy/remote browser delivered fewer frames.
+ phase += moving ? elapsed * (reduced.matches ? .85 : generating ? 1.4 : .48) : 0;
+ const delta = Math.min(.1, elapsed);
+ level = moving ? level + (target - level) * (1 - Math.exp(-delta * 4)) : target;
+ paint();
+ if (moving) frame = requestAnimationFrame(draw);
+ }
+ function schedule() {
+ if (frame === null && !disposed && visible && !document.hidden) frame = requestAnimationFrame(draw);
+ }
+ function visibility() {
+ if (document.hidden) { cancelAnimationFrame(frame); frame = null; last = null; }
+ else schedule();
+ }
+ function state() {
+ generating = !progress.hidden;
+ visible = generating || !media.querySelector('video');
+ const value = parseFloat(progress.style.getPropertyValue('--fv-progress'));
+ target = generating ? progress.dataset.counted === 'true' && Number.isFinite(value)
+ ? Math.min(1.06, Math.max(0, value / 100) + (value === 100 ? .06 : 0)) : .08 : .16;
+ // Stop scheduling completely once the video is on screen.
+ if (!visible && frame !== null) { cancelAnimationFrame(frame); frame = null; last = null; }
+ schedule();
+ }
+ const resize = new ResizeObserver(() => {
+ width = stage.clientWidth; height = stage.clientHeight;
+ const scale = Math.min(window.devicePixelRatio || 1, 1.5);
+ canvas.width = Math.round(width * scale); canvas.height = Math.round(height * scale);
+ ctx.setTransform(scale, 0, 0, scale, 0, 0); schedule();
+ });
+ const observer = new MutationObserver(state);
+ observer.observe(progress, {attributes: true, attributeFilter: ['hidden', 'style', 'data-counted']});
+ observer.observe(media, {childList: true}); resize.observe(stage);
+ document.addEventListener('visibilitychange', visibility); reduced.addEventListener('change', schedule);
+ state();
+ return () => {
+ disposed = true; cancelAnimationFrame(frame); observer.disconnect(); resize.disconnect();
+ document.removeEventListener('visibilitychange', visibility); reduced.removeEventListener('change', schedule);
+ canvas.remove();
+ };
+}
diff --git a/web/progress_connection.js b/web/progress_connection.js
new file mode 100644
index 0000000..078c3ee
--- /dev/null
+++ b/web/progress_connection.js
@@ -0,0 +1,41 @@
+// WebSocket updates plus a small snapshot recovery path for reloads/new tabs.
+export function createProgressConnection(api, consume, now = () => Date.now()) {
+ const seen = new Map();
+ let pending = null, timer = null;
+ function receive(message) {
+ if (!message || message.node == null) return;
+ const key = String(message.node), previous = seen.get(key);
+ if (Number.isInteger(message.sequence) && previous?.stream_id === message.stream_id
+ && previous.sequence >= message.sequence) return;
+ // The graph may still be loading. Do not acknowledge an undelivered
+ // snapshot; the next poll restores it once the node exists.
+ const age = Number.isFinite(message.age_seconds) ? Math.max(0, message.age_seconds) : 0;
+ if (consume({...message, received_at: now() - age * 1000}) !== false)
+ seen.set(key, message);
+ }
+ async function refresh() {
+ if (pending) return pending;
+ pending = (async () => {
+ try {
+ const response = await api.fetchApi('/freevideo/progress', {
+ cache: 'no-store', signal: AbortSignal.timeout(10000)});
+ if (response.ok) for (const message of (await response.json()).progress || []) receive(message);
+ } catch { /* Live WebSocket updates continue across a failed poll. */ }
+ })();
+ try { await pending; } finally { pending = null; }
+ }
+ const event = e => receive(e.detail);
+ function start() {
+ if (timer !== null) return;
+ api.addEventListener('freevideo_progress', event);
+ api.addEventListener('reconnected', refresh);
+ refresh(); timer = setInterval(refresh, 2000);
+ }
+ function stop() {
+ if (timer !== null) clearInterval(timer);
+ timer = null;
+ api.removeEventListener('freevideo_progress', event);
+ api.removeEventListener('reconnected', refresh);
+ }
+ return {start, stop, refresh, receive, reset: () => seen.clear()};
+}
diff --git a/web/setup.css b/web/setup.css
new file mode 100644
index 0000000..48de74f
--- /dev/null
+++ b/web/setup.css
@@ -0,0 +1,78 @@
+.fv-setup {
+ box-sizing: border-box;
+ position: fixed; inset: 0; margin: auto;
+ width: min(640px, calc(100vw - 32px)); max-height: calc(100dvh - 32px);
+ padding: 0 28px 28px; overflow: auto; overscroll-behavior: contain;
+ color: var(--fv-ink); background: var(--fv-bg);
+ border: 1px solid var(--fv-border); border-radius: var(--fv-r-lg);
+ box-shadow: 0 24px 80px #0008; font: 14px/1.6 var(--fv-font);
+ scrollbar-width: thin; scrollbar-color: var(--fv-border) transparent;
+}
+.fv-setup.fv-setup-small{width:min(480px,calc(100vw - 32px))}
+.fv-setup *{box-sizing:border-box}
+.fv-setup [hidden]{display:none!important}
+.fv-setup::backdrop{background:#070a0db8;backdrop-filter:blur(6px)}
+
+/* Header: title on the left, close on the right; stays put while scrolling. */
+.fv-setup-head{position:sticky;top:0;z-index:2;display:flex;align-items:center;gap:14px;margin:0 -28px 8px;padding:20px 20px 16px 28px;background:var(--fv-bg);border-bottom:1px solid var(--fv-border)}
+.fv-setup-head .fv-wordmark{width:112px}
+.fv-setup-head h2{flex:1;margin:0;padding-left:14px;border-left:1px solid var(--fv-border);font-size:var(--fv-strong);font-weight:var(--fv-semibold);line-height:1.3}
+.fv-setup-head h2:first-child{padding-left:0;border-left:0}
+.fv-setup-head .fv-close{min-height:34px;width:34px;padding:0;font-size:20px;line-height:1;background:transparent;border-color:transparent;color:var(--fv-muted)}
+.fv-setup-head .fv-close:hover{background:var(--fv-raised);color:var(--fv-ink)}
+.fv-setup h3{font-size:var(--fv-body);font-weight:var(--fv-semibold);margin:0 0 12px}
+
+/* Sections: one card each; details use the Studio's chevron, not the UA marker. */
+.fv-setup>details,.fv-setup>section{margin:12px 0 0;padding:16px 18px;border:1px solid var(--fv-border);border-radius:var(--fv-r-md);background:var(--fv-card)}
+.fv-setup summary{cursor:pointer;font-weight:var(--fv-semibold);list-style:none;display:flex;align-items:center;justify-content:space-between;gap:12px;padding:2px 0}
+.fv-setup summary::-webkit-details-marker{display:none}
+.fv-setup summary::after{content:'';flex:none;width:6px;height:6px;margin-right:4px;border-right:1.5px solid var(--fv-muted);border-bottom:1.5px solid var(--fv-muted);transform:rotate(45deg);transition:transform .24s}
+.fv-setup details[open]:not([data-expanded=false])>summary::after{transform:rotate(225deg)}
+.fv-setup details[open]>summary{margin-bottom:12px}
+.fv-setup details details{margin:12px 0 0;padding:12px 14px;border:1px solid var(--fv-border);border-radius:var(--fv-r-sm);background:var(--fv-bg)}
+.fv-setup details details>summary{font-weight:var(--fv-medium);font-size:var(--fv-body)}
+
+/* Fields. */
+.fv-setup label{display:block;margin:12px 0 6px;font-size:var(--fv-body);color:var(--fv-muted)}
+.fv-setup label>:is(input,textarea,select){margin-top:6px;color:var(--fv-ink)}
+.fv-setup input:not([type=checkbox]):not([type=range]),.fv-setup textarea,.fv-setup select{
+ display:block;width:100%;min-width:0;min-height:var(--fv-h);padding:7px 12px;font:inherit;
+ background:var(--fv-bg);color:var(--fv-ink);border:1px solid var(--fv-border);border-radius:var(--fv-r-sm);outline:none;
+}
+.fv-setup :is(input,textarea,select):focus{border-color:var(--fv-accent)}
+.fv-setup input.fv-number{display:inline-block;width:96px;margin:0 6px}
+.fv-setup textarea{resize:vertical;line-height:1.6}
+.fv-setup label.fv-check{display:flex;align-items:flex-start;gap:9px;color:var(--fv-ink)}
+.fv-setup input[type=checkbox]{flex:none;width:16px;height:16px;margin:3px 0 0;accent-color:var(--fv-accent)}
+.fv-setup input[type=range]{display:block;width:100%;margin:8px 0 4px;accent-color:var(--fv-accent)}
+
+/* Buttons. */
+.fv-setup button{background:var(--fv-raised);border:1px solid var(--fv-border);border-radius:var(--fv-r-sm);min-height:var(--fv-h);padding:7px 14px;color:var(--fv-ink);cursor:pointer;font:inherit;font-weight:var(--fv-medium);transition:background .2s,border-color .2s}
+.fv-setup button:hover{background:var(--fv-hover)}
+.fv-setup button:active:not(:disabled){filter:brightness(.9);transition-duration:.06s}
+.fv-setup button.fv-primary{background:var(--fv-accent);color:var(--fv-bg);border-color:var(--fv-accent);font-weight:var(--fv-semibold)}
+.fv-setup button.fv-primary:hover{background:var(--fv-accent-hover)}
+.fv-setup button.fv-quiet{background:transparent;border-color:transparent;color:var(--fv-muted)}
+.fv-setup button.fv-quiet:hover{background:var(--fv-raised);color:var(--fv-ink)}
+.fv-setup button:disabled{opacity:.45;cursor:default}
+.fv-setup a{color:var(--fv-accent);text-underline-offset:3px}
+.fv-setup .fv-actions{display:flex;align-items:center;gap:8px;flex-wrap:wrap;margin-top:16px}
+.fv-setup .fv-actions.fv-end{justify-content:flex-end}
+
+/* Text, plans and measurements. */
+.fv-setup p{margin:8px 0}
+.fv-setup .fv-muted{color:var(--fv-muted);font-size:var(--fv-micro);white-space:pre-wrap}
+.fv-setup progress{display:block;width:100%;height:6px;margin:6px 0}
+.fv-setup pre{margin:0;white-space:pre-wrap;overflow-wrap:anywhere;font:12px/1.6 Consolas,ui-monospace,monospace;color:var(--fv-muted)}
+.fv-setup .fv-facts{display:grid;grid-template-columns:auto minmax(0,1fr);gap:6px 18px;margin:4px 0 12px;font-size:var(--fv-body)}
+.fv-setup .fv-facts dt{color:var(--fv-muted)}
+.fv-setup .fv-facts dd{margin:0;font-variant-numeric:tabular-nums;overflow-wrap:anywhere}
+.fv-setup table.fv-speed{width:100%;margin-top:12px;border-collapse:collapse;font-size:var(--fv-body);font-variant-numeric:tabular-nums}
+.fv-setup table.fv-speed th{font-weight:var(--fv-medium);font-size:var(--fv-micro);color:var(--fv-muted);text-align:left;padding:0 8px 6px 0;border-bottom:1px solid var(--fv-border)}
+.fv-setup table.fv-speed td{padding:7px 8px 7px 0;border-bottom:1px solid var(--fv-border)}
+.fv-setup table.fv-speed :is(th,td):last-child{text-align:right;padding-right:0}
+.fv-setup table.fv-speed tr:last-child td{border-bottom:0}
+.fv-setup table.fv-speed td.fv-off{color:var(--fv-disabled)}
+.fv-setup .fv-level{margin:4px 0 0;text-align:center;font-weight:var(--fv-semibold);color:var(--fv-accent)}
+.fv-setup .fv-failure{color:var(--fv-danger)}.fv-setup .fv-ready{color:var(--fv-success)}
+@media(max-width:480px){.fv-setup{padding:0 16px 18px}.fv-setup-head{margin:0 -16px 8px;padding:14px 12px 12px 16px}.fv-setup-head .fv-wordmark{width:92px}.fv-setup>details,.fv-setup>section{padding:14px}}
diff --git a/web/setup.js b/web/setup.js
new file mode 100644
index 0000000..a1fdd34
--- /dev/null
+++ b/web/setup.js
@@ -0,0 +1,309 @@
+import { api } from "../../scripts/api.js";
+import { wordmark } from './branding.js';
+import { openCompatibility } from './compatibility.js';
+import { animateDetails, closeDialog } from './motion.js';
+
+const css = document.createElement('link');
+css.rel = 'stylesheet'; css.href = new URL('./setup.css', import.meta.url).href;
+document.head.append(css);
+
+const languageOverride = typeof location !== 'undefined'
+ ? new URLSearchParams(location.search).get('freevideo_lang') : null;
+const cn = languageOverride === 'zh' || (languageOverride !== 'en'
+ && String(navigator.language || '').toLowerCase().startsWith('zh'));
+const t = (en, zh) => cn ? zh : en;
+const gib = n => Number.isFinite(n) ? (n / 2 ** 30).toFixed(1) + " GiB" : "—";
+let current;
+
+export async function openSetup() {
+ if (current?.open) { current.focus(); return; }
+ const dialog = document.createElement("dialog"); current = dialog;
+ dialog.className = "fv-setup";
+ const node = (tag, label) => { const e = document.createElement(tag); if (label) e.textContent = label; return e; };
+ const note = label => { const e = node("p", label); e.className = "fv-muted"; return e; };
+ const heading = node('header'); heading.className = 'fv-setup-head';
+ const dismiss = node('button', '×'); dismiss.type = 'button'; dismiss.className = 'fv-close';
+ dismiss.setAttribute('aria-label', t('Close', '关闭')); dismiss.onclick = () => closeDialog(dialog);
+ heading.append(wordmark(), node('h2', t('Settings', '设置')), dismiss);
+ dialog.setAttribute('aria-label', t('FreeVideo settings', 'FreeVideo 设置'));
+ const installation = node('details'); installation.open = true;
+ installation.append(node('summary', t('Installation & model folders', '安装与模型目录')));
+ const inputs = node("div"); installation.append(inputs);
+ const rootLabel = node("label", t("FreeVideo installation folder", "FreeVideo 安装目录"));
+ const root = node("input"); root.type = "text"; rootLabel.append(root); inputs.append(rootLabel);
+ const extraLabel = node("label", t("Additional model folders (optional, one per line)", "其他模型目录(可选,每行一个)"));
+ const extra = node("textarea"); extra.rows = 2; extraLabel.append(extra); inputs.append(extraLabel);
+ const libraries = node("details"); libraries.append(node("summary", t("Automatically detected model folders", "自动发现的模型目录")));
+ const paths = node("pre"); libraries.append(paths); inputs.append(libraries);
+ const copyLabel = node("label"); copyLabel.className = 'fv-check'; const copy = node("input"); copy.type = "checkbox";
+ copyLabel.append(copy, node('span', t("Create independent copies (uses extra disk space)", "创建独立副本(额外占用磁盘)"))); inputs.append(copyLabel);
+ inputs.append(note(t('Models are reused by default.', '默认直接复用已有模型。')));
+ const downloads = node('section');
+ downloads.append(node('h3', t('Downloads', '下载')));
+ const modeLabel = node('label', t('Connection', '连接模式'));
+ const proxyMode = node('select');
+ for (const [id, name] of [['auto', t('Auto (recommended)', '自动(推荐)')], ['proxy', t('Proxy only', '仅代理')], ['direct', t('Direct only', '仅直连')]]) {
+ const option = node('option', name); option.value = id; proxyMode.append(option);
+ }
+ const modeHelp = note('');
+ modeLabel.append(proxyMode);
+ downloads.append(modeLabel, modeHelp);
+ const sourceLabel = node('label', t('Download source', '下载源'));
+ const source = node('select');
+ for (const [id, name] of [['auto',t('Automatic','自动选择')],['official','Hugging Face'],['hf-mirror','HF Mirror'],['modelscope','ModelScope']]) {
+ const option = node('option',name); option.value = id; source.append(option);
+ }
+ sourceLabel.append(source); downloads.append(sourceLabel);
+ const probeButton = node('button', t('Test source speeds', '重新测速'));
+ const sourceStatus = note(''), speeds = node('div');
+ downloads.append(probeButton, sourceStatus, speeds,
+ note(t('Switching applies to the current download; compatible fragments resume. Speed tests do not switch.', '切换立即作用于当前下载,兼容断点会续传。仅测速不切换。')));
+ const auth = node('details');
+ auth.append(node('summary', t('Hugging Face token · optional', 'Hugging Face Token · 可选')));
+ auth.append(note(t('Use your account’s download quota instead of the anonymous limit. Kept for this session only.', '使用账号下载额度,避开匿名限流。仅本次会话有效。')));
+ const hfTokenLabel = node('label', 'Hugging Face Token');
+ const hfToken = node('input'); hfToken.type = 'password'; hfToken.autocomplete = 'off'; hfToken.spellcheck = false;
+ hfTokenLabel.append(hfToken); auth.append(hfTokenLabel);
+ const authActions = node('div'); authActions.className = 'fv-actions';
+ const applyToken = node('button', t('Apply token', '应用 Token'));
+ const clearToken = node('button', t('Clear session token', '清除本次 Token'));
+ const tokenLink = node('a', t('Get a read token ↗', '获取读取 Token ↗'));
+ tokenLink.href = 'https://huggingface.co/settings/tokens'; tokenLink.target = '_blank'; tokenLink.rel = 'noopener noreferrer';
+ authActions.append(applyToken, clearToken, tokenLink); auth.append(authActions);
+ const authStatus = note(''); auth.append(authStatus); downloads.append(auth);
+ const advanced = node('details'); advanced.append(node('summary', t('Advanced settings', '高级设置')));
+ const compatibility = node('button', t('Compatibility settings', '兼容性设置'));
+ compatibility.onclick = openCompatibility;
+ advanced.append(compatibility);
+ const resources = node('details');
+ resources.append(node('summary', t('Reserved VRAM', '预留显存')));
+ const reserveLabel = node('label', t('Reserved VRAM', '预留显存'));
+ const reserveAuto = node('input'); reserveAuto.type = 'checkbox'; reserveAuto.checked = true;
+ const autoLabel = node('label'); autoLabel.className = 'fv-check'; autoLabel.append(reserveAuto, node('span', t('Automatic optimization', '自动优化')));
+ const reserve = node('input'); reserve.type = 'number'; reserve.min = '0.2'; reserve.step = '0.1';
+ reserve.value = '1.0'; reserve.disabled = true; reserve.className = 'fv-number';
+ reserveLabel.append(reserve, document.createTextNode('GiB'));
+ const saveReserve = node('button', t('Save', '保存')), reserveStatus = note('');
+ resources.append(autoLabel, reserveLabel, note(t('Keep this much additional VRAM free. Applies to new requests; current usage is already accounted for.', '额外保留的空闲显存。对后续请求生效;其他程序当前的占用已单独计入。')), saveReserve, reserveStatus);
+ advanced.append(resources);
+ reserveAuto.onchange = () => { reserve.disabled = reserveAuto.checked; };
+ // Placement calibration. A measurement, not a setting: it changes nothing
+ // about this installation, it reports what this machine measures so the
+ // choices that are currently limited to one platform can be settled.
+ const calibration = node('details');
+ calibration.append(node('summary', t('Performance measurement', '性能测量')));
+ const calibrateRepeats = node('input'); calibrateRepeats.type = 'number';
+ calibrateRepeats.min = '1'; calibrateRepeats.max = '4'; calibrateRepeats.step = '1';
+ calibrateRepeats.value = '1';
+ calibrateRepeats.className = 'fv-number';
+ const repeatsLabel = node('label', t('Passes', '轮数'));
+ repeatsLabel.append(calibrateRepeats);
+ const calibrateButton = node('button', t('Run placement test', '运行放置测试'));
+ const calibrateStatus = note('');
+ calibration.append(repeatsLabel, note(t(
+ 'Measures the placement choices this machine can settle and writes a report. It does not change your settings. The card stays busy for roughly half an hour per pass and generation cannot run meanwhile.',
+ '测量本机能定下来的放置选择并生成报告,不会改动你的设置。每轮大约占用显卡半小时,期间无法生成。')),
+ calibrateButton, calibrateStatus);
+ advanced.append(calibration);
+ const state = node("p"); state.setAttribute("role", "status");
+ const overall = node("div"); overall.hidden = true;
+ const overallText = node("p"); const overallBar = node("progress"); overallBar.value = 0; overallBar.max = 1;
+ overall.append(overallText, overallBar);
+ const bar = node("progress"); bar.hidden = true; bar.value = 0;
+ const counters = node("p");
+ const detail = note(""); const plan = node("div");
+ const more = node("details"); more.append(node("summary", t("Details and retained logs", "详细信息与保留日志"))); const log = node("pre"); more.append(log);
+ installation.append(overall, state, bar, counters, detail, plan, more);
+ const acceptLabel = node("label"); acceptLabel.className = 'fv-check'; const accept = node("input"); accept.type = "checkbox";
+ acceptLabel.append(accept, node('span', t("I accept the displayed installation plan and model/toolkit licenses", "我同意当前展示的安装计划和模型/工具包许可证")));
+ acceptLabel.hidden = true; installation.append(acceptLabel);
+ const actions = node("div"); actions.className = "fv-actions";
+ const inspect = node("button", t("Detect & review", "检测并预览")); inspect.className = "fv-primary";
+ const use = node("button", t("Use existing installation", "使用已有安装"));
+ const install = node("button", t("Install / repair", "安装/修复")); install.disabled = true;
+ const cancel = node("button", t("Stop setup", "停止安装")); cancel.disabled = true;
+ actions.append(inspect, use, install, cancel); installation.append(actions);
+ dialog.append(heading, installation, downloads, advanced);
+ const motions = [...dialog.querySelectorAll('details')].map(animateDetails);
+ dialog.addEventListener('cancel', e => { e.preventDefault(); closeDialog(dialog); });
+ document.body.append(dialog); dialog.showModal();
+ let token, planId, busy = false, probeBusy = false, pollTimer, lastPlanId;
+ const fail = error => { installation.open = true; state.textContent = error.message; state.className = "fv-failure"; state.scrollIntoView({block:'nearest'}); };
+ const request = async (action, value) => {
+ const response = await api.fetchApi("/freevideo/setup" + (action ? "/" + action : root.value ? '?root='+encodeURIComponent(root.value) : ""), action ? {
+ method: "POST", headers: { "Content-Type": "application/json", "X-FreeVideo-Setup": token }, body: JSON.stringify(value || {}),
+ } : {});
+ const result = await response.json(); if (!response.ok || result.error) throw new Error(result.error || response.statusText); return result;
+ };
+ const showResources = value => {
+ reserveAuto.checked = value?.gpu_reserve_gib == null;
+ reserve.disabled = reserveAuto.checked;
+ if (!reserveAuto.checked) reserve.value = String(value.gpu_reserve_gib);
+ };
+ calibrateButton.onclick = async () => {
+ if (!calibrateRepeats.checkValidity()) { calibrateRepeats.reportValidity(); return; }
+ calibrateButton.disabled = true;
+ calibrateStatus.className = 'fv-muted';
+ calibrateStatus.textContent = t('Starting…', '正在启动…');
+ try {
+ render(await request('calibrate', {repeats: Number(calibrateRepeats.value)}));
+ poll();
+ calibration.open = true;
+ calibrateStatus.textContent = t('Running. Progress is below; closing this panel keeps it running.',
+ '正在运行。进度见下方;关闭面板不会中断。');
+ } catch (error) {
+ calibrateStatus.className = 'fv-failure';
+ calibrateStatus.textContent = error.message;
+ } finally { calibrateButton.disabled = false; }
+ };
+ saveReserve.onclick = async () => {
+ if (!reserveAuto.checked && (!reserve.value || !reserve.checkValidity())) { reserve.reportValidity(); return; }
+ saveReserve.disabled = true;
+ try {
+ showResources(await request('resources', {gpu_reserve_gib: reserveAuto.checked ? null : Number(reserve.value)}));
+ reserveStatus.textContent = t('Saved · applies to the next request', '已保存 · 下次生成生效');
+ } catch (error) { reserveStatus.textContent = error.message; }
+ finally { saveReserve.disabled = false; }
+ };
+ function showDownloads(value) {
+ if (!value) return;
+ proxyMode.value = value.preferences?.proxy_mode || 'auto';
+ modeHelp.textContent = proxyMode.value === 'proxy' ? t('Use the current proxy. No direct fallback.', '沿用当前代理,不尝试直连。') : proxyMode.value === 'direct' ? t('Download directly, ignoring proxies.', '忽略代理,直接下载。') : t('Compare proxy and direct connections automatically.', '自动测速,择优使用代理或直连。');
+ authStatus.textContent = busy ? t('Stop setup to change the token, then resume with retained files.', '先停止安装,再修改 Token 并继续;已下载文件会保留。') :
+ value.hf_token_set ? t('Session token set. Existing environment credentials apply when this field is cleared.', '已设置本次 Token;清除后仍可使用环境中已有的凭据。') :
+ t('No session token set. Existing environment credentials still apply.', '未填写本次 Token;仍可使用环境中已有的凭据。');
+ source.value = value.preferences?.source || 'auto';
+ probeBusy = value.probe?.status === 'running'; probeButton.disabled = probeBusy;
+ probeButton.textContent = probeBusy ? t('Testing…','正在测速…') : t('Test source speeds','重新测速');
+ const progress = value.probe?.progress;
+ sourceStatus.textContent = value.probe?.error || (probeBusy ? t('Testing sources… ','正在测速… ') + `${progress?.done || 0}/${progress?.total || '…'}` : '');
+ const measured = value.probe?.status === 'complete' ? value.probe : value.preferences?.probe;
+ if (measured?.network_unavailable && !probeBusy) sourceStatus.textContent = t('No sources reachable. Check your network or connection mode.', '下载源均无法连接,请检查网络或切换连接模式。');
+ speeds.replaceChildren();
+ if (!measured?.sources) return;
+ const table = node('table'); table.className = 'fv-speed';
+ const header = node('tr');
+ for (const title of [t('Files','文件'),t('Source / route','来源/连接'),t('HTTP transfer','HTTP 传输速度')]) header.append(node('th',title));
+ table.append(header);
+ for (const [family, rows] of Object.entries(measured.sources)) for (const row of rows) {
+ const tr = node('tr');
+ const names = {'edge-models':t('Video model','视频模型'),'vdn-models':t('Decoder','解码器'),models:t('Text encoder','文本编码器'),pypi:t('Python packages','Python 依赖'),github:t('Tools','安装工具'),git:'Git',cuda:'CUDA'};
+ if (family.startsWith('torch-')) names[family] = t('GPU packages','GPU 依赖');
+ const rate = row.bytes_per_second / 2**20;
+ const speed = !row.ok ? t('Unavailable','未连通') : row.speed_measured === false || !Number.isFinite(rate) || rate <= 0 ?
+ t('Connected; insufficient sample','已连通,样本不足') : rate < .005 ? '<0.01 MiB/s' : `${rate.toFixed(rate < 1 ? 2 : 1)} MiB/s`;
+ for (const text of [names[family] || family, `${row.id} · ${row.route === 'direct' ? t('direct','直连') : t('current connection','当前连接')}`,speed]) tr.append(node('td',text));
+ if (!row.ok) tr.lastChild.className = 'fv-off';
+ table.append(tr);
+ }
+ speeds.append(table,note(t('HTTP transfer speed, excluding connection wait. Parallel/Xet downloads may differ.','HTTP 传输测速已扣除连接等待;多线程/Xet 下载速度可能不同。')));
+ }
+ source.onchange = async () => {
+ source.disabled = true;
+ try { showDownloads(await request('network-source',{root:root.value,source:source.value})); sourceStatus.textContent=t('Switch requested for active downloads; retained fragments will be reused where compatible.','已请求立即切换当前下载,兼容的断点和分片将继续复用。'); }
+ catch(error) { fail(error); }
+ finally { source.disabled = false; }
+ };
+ proxyMode.onchange = async () => {
+ proxyMode.disabled = true;
+ try { showDownloads(await request('network-source', {root:root.value, proxy_mode:proxyMode.value})); }
+ catch (error) { fail(error); }
+ finally { proxyMode.disabled = false; }
+ };
+ probeButton.onclick = async () => {
+ probeButton.disabled=true;
+ try { showDownloads(await request('network-probe',{root:root.value})); poll(); }
+ catch(error) { probeButton.disabled=false; fail(error); }
+ };
+ const controls = () => {
+ for (const e of [root, extra, copy, inspect, use, hfToken, applyToken, clearToken]) e.disabled = busy;
+ cancel.disabled = !busy; install.disabled = busy || !planId || !accept.checked; accept.disabled = busy; inspect.classList.toggle("fv-primary", !planId); install.classList.toggle("fv-primary", Boolean(planId));
+ };
+ const invalidate = () => { planId = undefined; accept.checked = false; acceptLabel.hidden = true; controls(); };
+ async function setToken(value) {
+ applyToken.disabled = clearToken.disabled = true;
+ try {
+ await request('network-token', {hf_token:value}); hfToken.value = '';
+ invalidate(); await poll();
+ } catch(error) { fail(error); }
+ finally { controls(); }
+ }
+ applyToken.onclick = () => setToken(hfToken.value);
+ clearToken.onclick = () => setToken('');
+ for (const e of [root, extra, copy]) e.oninput = invalidate;
+ accept.onchange = controls;
+ function showPlan(value) {
+ plan.replaceChildren();
+ const gpu = value.inventory?.selected_gpu || {}, local = value.local_models || {};
+ const facts = [
+ [t("GPU", "显卡"), gpu.name || "—"],
+ [t("Model download", "需下载模型"), gib(value.model_download_bytes)],
+ [t("Models already present", "安装目录内已有模型"), gib(value.existing_model_bytes_size_matched || 0)],
+ [t("Local models reused", "复用本地模型"), gib(local.reused_bytes || 0)],
+ [t("Hardlinked / copied", "硬链接/复制"), `${gib(local.linked_bytes || 0)} / ${gib(local.copy_bytes || 0)}`],
+ [t("Additional disk estimate", "额外磁盘预估"), gib(value.disks?.reduce((sum, d) => sum + d.needed_bytes, 0))],
+ ];
+ if (value.prepared_model) facts.push([t("Model", "模型"), `${value.prepared_model.repo} · ${t("slim FP8, no local conversion", "精简 FP8,无需本地转换")}`]);
+ const list = node('dl'); list.className = 'fv-facts';
+ for (const [key, shown] of facts) list.append(node('dt', key), node('dd', shown));
+ plan.append(list);
+ if (value.prepared_model?.private) plan.append(note(t("Private repository: authorized Hugging Face login/token required.", "私有仓库:需要有访问权限的 Hugging Face 登录/token。")));
+ const licenses = node("details"); licenses.append(node("summary", t("Model and toolkit licenses", "模型与工具包许可证")), node("pre", value.licenses?.join("\n") || "")); plan.append(licenses);
+ if (value.errors?.length) { const e = node("p", value.errors.join("\n")); e.className = "fv-failure"; plan.append(e); }
+ }
+ function render(task) {
+ busy = Boolean(task.busy);
+ inputs.hidden = busy; plan.hidden = busy;
+ if (task.plan_id && task.plan_id !== lastPlanId) {
+ lastPlanId = task.plan_id; planId = task.plan?.errors?.length ? undefined : task.plan_id;
+ accept.checked = false; acceptLabel.hidden = !planId; showPlan(task.plan);
+ }
+ acceptLabel.hidden = busy || !planId;
+ const p = task.progress || {};
+ state.className = task.status === "failed" ? "fv-failure" : task.ready ? "fv-ready" : "";
+ state.textContent = task.error || (task.ready ? t("Ready. Generate with your existing workflow.", "准备就绪,可使用现有工作流生成。") : busy ? p.label || t("Checking configuration and local models…", "正在检查配置和本地模型…") : task.status === "complete" && task.action === "plan" ? t("Review the plan, then install.", "请确认计划后安装。") : task.status === "cancelled" ? t("Stopped. Files retained for resuming.", "已停止,文件保留,可继续安装。") : "");
+ const phases = task.phase_progress || {}, valid = Number.isFinite(p.total) && p.total > 0 && Number.isFinite(p.done) && p.done >= 0 && p.done <= p.total;
+ overall.hidden = !busy;
+ overallBar.max = phases.total > 0 ? phases.total : 1; overallBar.value = phases.done || 0;
+ overallText.textContent = phases.total > 0 ? `${t("Overall", "整体进度")} · ${phases.done} / ${phases.total} ${t("stages complete", "阶段已完成")} · ${Math.floor(100 * phases.done / phases.total)}%` : t("Preparing installation plan…", "正在准备安装计划…");
+ bar.hidden = !busy; bar.max = valid ? p.total : 1; bar.value = valid ? p.done : 0;
+ const amount = n => p.total >= 2 ** 30 ? gib(n) : `${(n / 2 ** 20).toFixed(1)} MiB`;
+ const count = valid ? `${Math.floor(100 * p.done / p.total)}% · ${p.unit === "bytes" ? `${amount(p.done)} / ${amount(p.total)}` : `${p.done} / ${p.total}`}` : t("Measuring progress…", "正在获取进度…");
+ const eta = valid && Number.isFinite(p.remaining_seconds) && p.remaining_seconds > 0 ? `${p.unit === "bytes" ? t("File ETA", "本文件预计剩余") : t("ETA", "预计剩余")} ~${Math.ceil(p.remaining_seconds)} s` : t("Estimating remaining time…", "正在估算剩余时间…");
+ counters.textContent = busy ? `${count} · ${eta}` : "";
+ const others = (task.active_tasks || []).slice(1).map(p => p.label).filter(Boolean);
+ detail.textContent = [p.detail, p.resource, others.length ? `${t("Also running", "同时进行")}: ${others.join(" / ")}` : ""].filter(Boolean).join(" · ");
+ log.textContent = [task.log, task.tail].filter(Boolean).join("\n"); controls();
+ }
+ async function poll() {
+ clearTimeout(pollTimer);
+ try { const info = await request(); token = info.discovery.token; render(info.task); showDownloads(info.discovery.downloads); }
+ catch (error) { fail(error); }
+ if (dialog.open && (busy || probeBusy)) pollTimer = setTimeout(poll, 750);
+ }
+ inspect.onclick = async () => {
+ invalidate(); busy = true; controls();
+ try { render(await request("inspect", {root: root.value, extra_libraries: extra.value.split("\n").map(s => s.trim()).filter(Boolean), copy: copy.checked})); poll(); }
+ catch (error) { busy = false; controls(); fail(error); }
+ };
+ install.onclick = async () => {
+ try { render(await request("install", {plan_id: planId, accept_licenses: accept.checked})); invalidate(); poll(); }
+ catch (error) { fail(error); }
+ };
+ use.onclick = async () => {
+ try { const result = await request("use", {root: root.value}); state.textContent = result.ready ? t("Existing engine connected. Ready to generate.", "已连接现有引擎,可以生成。") : result.detail; state.className = "fv-ready"; invalidate(); }
+ catch (error) { fail(error); }
+ };
+ cancel.onclick = async () => { try { render(await request("cancel")); poll(); } catch (error) { fail(error); } };
+ dialog.onclose = () => { clearTimeout(pollTimer); for (const dispose of motions) dispose(); dialog.remove(); if (current === dialog) current = undefined; };
+ try {
+ const info = await request(); token = info.discovery.token; root.value = info.discovery.root;
+ showDownloads(info.discovery.downloads);
+ showResources(info.discovery.resources);
+ paths.textContent = info.discovery.libraries.join("\n") || t("No model libraries found; add a folder above.", "没有发现模型目录,可在上方添加。");
+ render(info.task);
+ installation.open = !info.discovery.ready || busy;
+ if (info.discovery.ready && !busy) state.textContent = t("Ready to generate.", "已就绪,可以开始生成。");
+ if (busy || probeBusy) poll();
+ } catch (error) { fail(error); }
+}
diff --git a/web/studio.css b/web/studio.css
new file mode 100644
index 0000000..66b070b
--- /dev/null
+++ b/web/studio.css
@@ -0,0 +1,114 @@
+.fv-studio{box-sizing:border-box;position:fixed;inset:0;margin:auto;width:min(1240px,calc(100vw - 48px));height:min(880px,calc(100dvh - 48px));max-width:none;max-height:none;padding:0;color:var(--fv-ink);background:var(--fv-bg);border:1px solid var(--fv-border);border-radius:var(--fv-r-lg);box-shadow:0 32px 110px #0008;font:14px/1.6 var(--fv-font);overflow:hidden;color-scheme:dark}
+.fv-studio::backdrop{background:#070a0db8;backdrop-filter:blur(8px)}
+.fv-studio *{box-sizing:border-box}.fv-studio [hidden]{display:none!important}
+.fv-studio button,.fv-studio input,.fv-studio select,.fv-studio textarea{font:inherit}
+.fv-studio button,.fv-studio a{transition:background .2s,border-color .2s,color .2s,box-shadow .2s,transform .2s}
+.fv-studio button{cursor:pointer}.fv-studio button:disabled{opacity:.45;cursor:default}
+.fv-studio :focus-visible,.fv-lora-panel :focus-visible{outline:2px solid var(--fv-accent);outline-offset:3px}
+.fv-studio button,.fv-studio a,.fv-studio select,.fv-studio input{min-height:var(--fv-h);border-radius:var(--fv-r-sm)}
+.fv-studio button,.fv-studio a{display:inline-flex;align-items:center;justify-content:center;gap:8px;border:1px solid var(--fv-border);background:var(--fv-raised);color:var(--fv-ink);padding:7px 14px;font-weight:var(--fv-medium);text-decoration:none}
+.fv-studio button:hover,.fv-studio a:hover{background:var(--fv-hover);border-color:var(--fv-sheen)}
+.fv-studio :is(button,a):active:not(:disabled){filter:brightness(.9);transition-duration:.06s}
+.fv-studio .fv-primary{background:var(--fv-accent);color:var(--fv-bg);border-color:var(--fv-accent);font-weight:var(--fv-semibold)}.fv-studio .fv-primary:hover{background:var(--fv-accent-hover);border-color:var(--fv-accent-hover)}
+.fv-studio .fv-quiet{background:transparent;border-color:transparent;color:var(--fv-muted)}
+.fv-studio .fv-header{height:76px;display:grid;grid-template-columns:1fr auto;align-items:center;padding:0 28px;border-bottom:1px solid var(--fv-border);gap:12px}
+.fv-brand{display:flex;min-width:0;align-items:center;gap:10px;font-size:var(--fv-strong);letter-spacing:-.5px;font-weight:var(--fv-semibold)}
+.fv-studio .fv-title{text-align:center}.fv-studio .fv-title strong{font-size:var(--fv-body);font-weight:var(--fv-medium)}.fv-studio .fv-header-actions{display:flex;justify-content:flex-end;align-items:center;gap:5px}
+.fv-studio .fv-body{display:grid;grid-template-columns:420px minmax(0,1fr);height:calc(100% - 76px)}
+.fv-studio .fv-controls{min-height:0;overflow:auto;padding:25px 26px 0;border-right:1px solid var(--fv-border);scrollbar-width:thin;scrollbar-color:var(--fv-border) transparent}
+.fv-studio .fv-label{display:flex;align-items:center;justify-content:space-between;gap:12px;margin-bottom:10px;font-weight:var(--fv-semibold);color:var(--fv-ink);font-size:var(--fv-body)}
+.fv-studio .fv-muted{color:var(--fv-muted);font-size:var(--fv-micro);font-weight:var(--fv-regular)}
+.fv-studio .fv-section{margin-bottom:23px}.fv-studio .fv-section-title{font-size:var(--fv-body);font-weight:var(--fv-semibold);list-style:none;cursor:pointer;display:flex;align-items:center;justify-content:space-between}
+.fv-studio .fv-section-title::after{content:'';width:6px;height:6px;margin-right:3px;border-right:1.5px solid var(--fv-muted);border-bottom:1.5px solid var(--fv-muted);transform:rotate(45deg);transition:transform .24s}.fv-studio details[open]:not([data-expanded=false])>.fv-section-title::after{transform:rotate(225deg)}
+.fv-studio details>summary::-webkit-details-marker{display:none}.fv-studio .fv-expand{padding-top:14px}
+.fv-studio textarea,.fv-studio select,.fv-studio input[type=number]{border:1px solid var(--fv-border);background:var(--fv-card);color:var(--fv-ink);padding:8px 12px;width:100%;outline:none}
+.fv-studio textarea:focus,.fv-studio input:focus,.fv-studio select:focus{border-color:var(--fv-accent)}
+.fv-studio textarea{display:block;resize:vertical;min-height:148px;height:170px;max-height:340px;border-radius:var(--fv-r-md);line-height:1.65}
+.fv-studio textarea::placeholder{color:var(--fv-disabled)}.fv-studio input:disabled,.fv-studio textarea:disabled{opacity:.5}
+.fv-prompt-guide{box-sizing:border-box;margin:8px 0 0;color:var(--fv-muted);font:12px/1.7 var(--fv-font);white-space:normal;overflow-wrap:anywhere}
+.fv-prompt-guide a,.fv-studio .fv-prompt-guide a{display:inline;min-height:0;padding:0;border:0;border-radius:0;background:transparent;color:var(--fv-accent);text-decoration:underline;text-underline-offset:3px}
+.fv-prompt-guide a:hover,.fv-studio .fv-prompt-guide a:hover{background:transparent;color:var(--fv-ink)}
+.fv-prompt-guide a:focus-visible{outline:2px solid var(--fv-accent);outline-offset:3px}
+.fv-studio .fv-fields{display:grid;grid-template-columns:1fr 1fr;gap:12px}.fv-studio .fv-field{display:block;min-width:0}.fv-studio .fv-field>span{display:block;font-size:var(--fv-micro);color:var(--fv-muted);margin:0 0 7px}
+.fv-studio .fv-shapes{position:relative;isolation:isolate;display:grid;grid-template-columns:repeat(4,1fr);gap:8px;margin-bottom:14px}
+.fv-studio .fv-shapes::before{content:'';position:absolute;inset:0 auto 0 0;width:calc((100% - 24px)/4);border:1px solid var(--fv-accent);border-radius:var(--fv-r-sm);background:var(--fv-selected);transform:translateX(calc(var(--fv-shape-index,0)*(100% + 8px)));transition:transform .38s cubic-bezier(.22,.7,.2,1),opacity .2s;pointer-events:none;z-index:-1}
+.fv-studio .fv-shapes[data-custom=true]::before{opacity:0}
+.fv-studio .fv-shape{flex-direction:column;gap:8px;font-size:var(--fv-micro);padding:10px 4px;color:var(--fv-muted);min-height:68px;background:var(--fv-card);border-color:transparent;border-radius:var(--fv-r-sm)}
+.fv-studio .fv-shape:hover{background:var(--fv-raised);border-color:transparent;color:var(--fv-ink)}.fv-studio .fv-shape[aria-pressed=true]{background:transparent;border-color:transparent;color:var(--fv-accent)}
+.fv-studio .fv-shape-icon{display:block;width:26px;height:15px;margin:5.5px 0;border:1.5px solid currentColor;border-radius:var(--fv-r-xs)}
+.fv-studio .fv-shape[data-ratio='9:16'] .fv-shape-icon{width:15px;height:26px;margin:0}.fv-studio .fv-shape[data-ratio='1:1'] .fv-shape-icon{width:20px;height:20px;margin:3px 0}
+.fv-studio .fv-shape[data-ratio=input] .fv-shape-icon{border-style:dashed;width:22px;height:18px;margin:4px 0}
+.fv-studio .fv-canvas-note{margin-top:10px;display:flex;flex-wrap:wrap;justify-content:space-between;gap:8px;color:var(--fv-muted);font-size:var(--fv-micro);font-variant-numeric:tabular-nums}
+.fv-studio .fv-two-pass{display:flex;align-items:center;gap:8px;margin-top:14px;color:var(--fv-ink);cursor:pointer}
+.fv-studio .fv-two-pass input{width:16px;height:16px;min-height:16px;margin:0;accent-color:var(--fv-accent);flex:none}
+.fv-studio .fv-preview-column{min-width:0;min-height:0;display:flex;flex-direction:column;padding:26px 30px 24px;gap:19px;overflow:auto;scrollbar-width:thin;background:radial-gradient(ellipse at 50% 30%,#6db8fa12,transparent 70%)}
+.fv-studio .fv-preview-head{display:flex;align-items:center;justify-content:space-between;font-size:var(--fv-micro);color:var(--fv-muted)}
+.fv-studio .fv-preview-head strong{font-size:var(--fv-body);font-weight:var(--fv-medium);color:var(--fv-ink)}
+.fv-studio .fv-preview-space{min-height:260px;flex:1 0 260px;display:grid;place-items:center;min-width:0}
+.fv-studio .fv-stage{display:grid;place-items:center;position:relative;isolation:isolate;overflow:hidden;border:1px solid #ffffff14;border-radius:var(--fv-r-md);background:#101d2d;box-shadow:0 18px 52px #0004;max-width:100%;transition:width .48s cubic-bezier(.22,.7,.2,1),height .48s cubic-bezier(.22,.7,.2,1)}
+.fv-studio .fv-preview-scene{position:absolute;inset:0;width:100%;height:100%;pointer-events:none;transition:opacity .55s ease}
+.fv-studio .fv-stage-media{position:absolute;inset:0;display:grid;place-items:center;transition:opacity .55s ease;z-index:1}
+.fv-studio .fv-stage video,.fv-studio .fv-stage img{display:block;width:100%;height:100%;min-height:0;object-fit:contain;background:#0d1118}
+.fv-studio .fv-stage:not([data-revealing=true]):has(.fv-generation-progress:not([hidden])) .fv-stage-media{opacity:0;pointer-events:none}
+.fv-studio .fv-stage:has(video):not(:has(.fv-generation-progress:not([hidden]))) .fv-preview-scene,.fv-studio .fv-stage[data-revealing=true] .fv-preview-scene{opacity:0}
+.fv-studio .fv-stage .fv-generation-progress{position:absolute;inset:0;z-index:2;width:100%;max-width:none;margin:0;padding:24px;border:0;background:none;border-radius:0;box-shadow:none;display:flex;flex-direction:column;justify-content:center;align-items:center;transition:opacity .55s ease}
+.fv-studio .fv-stage .fv-generation-track{position:absolute;inset:0;height:100%;border-radius:0;background:none;pointer-events:none}
+/* Keep the accessible progressbar; the canvas paints its measured water level. */
+.fv-studio .fv-stage .fv-generation-fill,.fv-studio .fv-stage .fv-generation-fill::after{opacity:0;animation:none;will-change:auto}
+.fv-studio .fv-stage .fv-generation-heading{flex-direction:column;align-items:center;gap:10px;margin:0;position:relative;text-shadow:0 2px 18px #071320b3}
+.fv-studio .fv-stage .fv-generation-label{font-size:13px;font-weight:var(--fv-regular);color:#d0deeb;letter-spacing:.02em}
+.fv-studio .fv-stage .fv-generation-count{font-size:clamp(26px,3vw,40px);font-weight:var(--fv-medium);color:#edf5ff;line-height:1.2;letter-spacing:-.03em}
+.fv-studio .fv-stage .fv-generation-times{position:relative;margin-top:12px;color:#bdd0e2;text-shadow:0 2px 14px #071320}
+.fv-studio .fv-stage[data-revealing=true] .fv-generation-progress{opacity:0;pointer-events:none}
+.fv-studio .fv-stage[data-revealing=true] .fv-stage-media{opacity:1;pointer-events:auto}
+.fv-studio .fv-stage[data-short=true] .fv-generation-progress{padding:8px 16px}
+.fv-studio .fv-stage[data-short=true] .fv-generation-heading{flex-direction:row;gap:16px}
+.fv-studio .fv-stage[data-short=true] .fv-generation-count{font-size:20px}
+.fv-studio .fv-stage[data-short=true] .fv-generation-times{display:none}
+.fv-studio .fv-stage-media video{animation:fv-preview-appear .55s ease}
+@keyframes fv-preview-appear{from{opacity:0}to{opacity:1}}
+.fv-studio .fv-stats{animation:fv-panel-enter .35s ease;display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:6px;text-align:center;margin:0}.fv-studio .fv-stat{padding:12px 4px;background:var(--fv-card);border-radius:var(--fv-r-md)}
+.fv-studio .fv-stat strong{font-size:var(--fv-strong);font-weight:var(--fv-semibold);display:block;color:var(--fv-ink);font-variant-numeric:tabular-nums}.fv-studio .fv-stat span{font-size:var(--fv-micro);color:var(--fv-muted)}
+.fv-studio .fv-result-links{animation:fv-panel-enter .35s ease;display:flex;align-items:center;justify-content:center;gap:9px;flex-wrap:wrap}
+.fv-studio .fv-runbar{position:sticky;bottom:0;padding:16px 0 22px;background:linear-gradient(transparent,var(--fv-bg) 12%);z-index:2}
+.fv-studio .fv-runbar .fv-primary{width:100%;min-height:var(--fv-h-lg);font-size:14px}
+.fv-studio .fv-runrow{display:flex;gap:6px;align-items:stretch}
+.fv-studio .fv-runrow>.fv-primary{flex:1;min-width:0}
+.fv-studio .fv-run-options{position:relative;flex:none}
+.fv-studio .fv-run-options>summary{display:grid;place-items:center;height:100%;width:42px;border:1px solid var(--fv-border);border-radius:var(--fv-r-sm);background:var(--fv-card);cursor:pointer;list-style:none}
+.fv-studio .fv-run-options>summary::-webkit-details-marker,.fv-studio .fv-queue-details>summary::-webkit-details-marker{display:none}
+.fv-studio .fv-chevron{width:7px;height:7px;border-right:1.5px solid currentColor;border-bottom:1.5px solid currentColor;transform:rotate(45deg);transition:transform .2s}
+.fv-studio .fv-run-options[open] .fv-chevron{transform:rotate(225deg)}
+.fv-studio .fv-run-menu,.fv-studio .fv-queue-list{position:absolute;bottom:calc(100% + 10px);right:0;width:240px;max-width:calc(100vw - 60px);padding:12px;border:1px solid var(--fv-border);border-radius:var(--fv-r-md);background:var(--fv-card);box-shadow:0 14px 45px #0006;animation:fv-preview-appear .16s ease;z-index:4}
+.fv-studio .fv-run-menu>strong{display:block;padding:2px 8px 8px;font-size:var(--fv-micro);color:var(--fv-muted);font-weight:var(--fv-medium)}
+.fv-studio .fv-run-menu>button{display:flex;width:100%;justify-content:flex-start;background:transparent;border-color:transparent;text-align:left;padding:8px}
+.fv-studio .fv-run-menu>button[aria-pressed=true]{background:var(--fv-selected);color:var(--fv-accent)}
+.fv-studio .fv-run-menu .fv-field{padding:10px 8px 0}.fv-studio .fv-run-menu p{margin:10px 8px 2px;line-height:1.6}
+.fv-studio .fv-queue-actions{display:flex;align-items:center;justify-content:center;gap:4px;flex-wrap:wrap;margin-top:8px}
+.fv-studio .fv-queue-actions button,.fv-studio .fv-queue-details>summary{padding:4px 8px;min-height:28px;font-size:var(--fv-micro);color:var(--fv-muted)}
+.fv-studio .fv-queue-details{position:relative}.fv-studio .fv-queue-details>summary{cursor:pointer;list-style:none}
+.fv-studio .fv-queue-list{left:0;right:auto;bottom:calc(100% + var(--fv-h-lg) + 24px);width:300px;max-height:260px;overflow:auto;scrollbar-width:thin}
+.fv-studio .fv-queue-item{display:flex;align-items:center;justify-content:space-between;gap:8px;font-size:var(--fv-micro);padding:3px 0;white-space:nowrap}
+.fv-studio .fv-status{min-height:22px;text-align:center;font-size:var(--fv-micro);color:var(--fv-muted);overflow-wrap:anywhere}.fv-studio .fv-status[data-error=true]{color:var(--fv-danger)}
+.fv-studio .fv-progress{width:100%;height:4px;accent-color:var(--fv-accent);border:0;margin:0 0 10px}.fv-studio .fv-budget,.fv-studio .fv-prewarm{text-align:center;font-size:var(--fv-micro);color:var(--fv-muted)}
+.fv-studio .fv-panel{height:auto;padding:0;border:none;border-radius:0;background:transparent;color:var(--fv-ink);font-size:var(--fv-micro);overflow:visible}
+.fv-studio .fv-panel .fv-toolbar{justify-content:flex-start}.fv-studio .fv-panel .fv-note{font-size:var(--fv-micro);margin:8px 0;text-align:left}
+.fv-studio .fv-panel .fv-empty{display:grid;place-items:center;padding:22px 15px;min-height:92px;margin-top:12px;font-size:var(--fv-micro);white-space:pre-line;border-color:var(--fv-border);background:var(--fv-card)}
+.fv-studio .fv-panel .fv-preview{max-height:170px;margin:10px auto;object-fit:contain}.fv-studio .fv-panel .fv-card{background:var(--fv-card);border-color:var(--fv-border)}
+.fv-studio .fv-panel select{min-height:var(--fv-h-sm);padding:5px 8px;max-width:145px;width:auto}.fv-studio .fv-panel button{font-size:var(--fv-micro);padding:5px 10px;min-height:var(--fv-h-sm)}
+.fv-lora-list{display:flex;flex-direction:column;gap:8px;margin:12px 0}.fv-lora-row{display:grid;grid-template-columns:18px minmax(0,1fr) 64px 30px;align-items:center;gap:7px}
+.fv-lora-row select{min-width:0!important;max-width:100%!important;width:100%!important}.fv-lora-row input[type=number]{width:64px!important;padding:6px!important;font-variant-numeric:tabular-nums}.fv-lora-row input[type=checkbox]{accent-color:var(--fv-accent);width:15px;min-height:15px}
+.fv-lora-row[data-enabled=false]{opacity:.45}.fv-lora-order{grid-column:2/5;display:flex;gap:5px;justify-content:flex-end}
+.fv-studio .fv-lora-row button,.fv-lora-panel .fv-lora-row button{min-height:28px;padding:3px 7px}.fv-lora-note{font-size:var(--fv-micro);color:var(--fv-muted);text-align:center;margin:10px 0 0}.fv-lora-add{display:flex;justify-content:center;gap:8px}
+.fv-studio .fv-error{color:var(--fv-danger)}.fv-studio .fv-legacy{font-size:var(--fv-micro);color:var(--fv-muted);padding:12px 0}
+@media(max-width:900px){.fv-studio{width:calc(100vw - 24px);height:calc(100dvh - 24px)}.fv-studio .fv-body{grid-template-columns:360px minmax(0,1fr)}.fv-studio .fv-preview-column{padding:22px 18px}.fv-studio .fv-controls{padding:22px 18px 0}.fv-studio .fv-header{padding:0 18px}.fv-studio .fv-stats{grid-template-columns:1fr 1fr}.fv-studio .fv-title{display:none}.fv-studio .fv-header{grid-template-columns:1fr auto}}
+@media(max-width:680px){.fv-studio{width:calc(100vw - 16px);height:calc(100dvh - 16px);border-radius:var(--fv-r-md);overflow:auto}.fv-studio .fv-header{height:65px;position:sticky;top:0;background:var(--fv-bg);z-index:3}.fv-studio .fv-body{display:flex;flex-direction:column;height:auto}.fv-studio .fv-controls{overflow:visible;border-right:0;border-bottom:1px solid var(--fv-border)}.fv-studio .fv-preview-column{min-height:420px}.fv-studio .fv-preview-space{height:min(65dvh,600px);min-height:260px;flex:none}.fv-studio .fv-runbar{position:static}.fv-studio .fv-stats{grid-template-columns:repeat(4,minmax(0,1fr))}.fv-studio .fv-stat strong{font-size:var(--fv-body)}}
+@media(prefers-reduced-motion:reduce){.fv-studio *,.fv-studio *::before,.fv-studio *::after{transition:none!important;animation:none!important}}
+@media(max-width:680px){.fv-studio .fv-wordmark{width:145px}.fv-studio .fv-header{padding:0 14px;gap:8px}.fv-studio .fv-header-actions button{font-size:var(--fv-micro);padding:7px 8px}}
+
+/* Results and errors remain reachable on short windows and at browser zoom. */
+.fv-studio .fv-preview-column>*{flex-shrink:0}
+.fv-studio .fv-controls,.fv-studio .fv-preview-column{scroll-padding-block:24px;overscroll-behavior:contain}
+.fv-studio .fv-status:empty,.fv-studio .fv-budget:empty,.fv-studio .fv-prewarm:empty,.fv-studio .fv-result-links:empty{display:none}
+@media(max-width:1050px) and (min-width:681px){.fv-studio .fv-wordmark{width:150px}.fv-studio .fv-view-switch button{padding-inline:10px}}
+@media(max-width:480px){.fv-studio .fv-header:has(.fv-view-switch){height:auto;min-height:98px;grid-template-columns:1fr auto;padding-block:10px}.fv-studio .fv-header>.fv-view-switch{grid-column:1/-1;grid-row:2}.fv-studio .fv-header>.fv-header-actions{grid-column:2;grid-row:1}.fv-studio .fv-stats{grid-template-columns:repeat(2,minmax(0,1fr))}}
diff --git a/web/studio.js b/web/studio.js
new file mode 100644
index 0000000..dc77c31
--- /dev/null
+++ b/web/studio.js
@@ -0,0 +1,537 @@
+import { createErrorPanel, errorText } from './error_panel.js';
+import { app } from '../../scripts/app.js';
+import { api } from '../../scripts/api.js';
+import { openSetup } from './setup.js';
+import { wordmark } from './branding.js';
+import { createGenerationProgress } from './generation_progress.js';
+import { viewSwitch, viewChanged } from './view_navigation.js';
+import { createPreviewScene } from './preview_scene.js?v=20260929-swell';
+import { animateDetails, closeDialog } from './motion.js';
+import { openLibrary, latestVideo } from './library.js';
+import { createStudioQueue, randomSeed } from './studio_queue.js';
+
+const languageOverride = typeof location !== 'undefined'
+ ? new URLSearchParams(location.search).get('freevideo_lang') : null;
+const cn = languageOverride === 'zh' || (languageOverride !== 'en'
+ && String(navigator.language || '').toLowerCase().startsWith('zh'));
+const t = (en, zh) => cn ? zh : en;
+const css = document.createElement('link'); css.rel = 'stylesheet'; css.href = new URL('./studio.css', import.meta.url).href; document.head.append(css);
+const el = (tag, label, cls) => { const e = document.createElement(tag); if (label != null) e.textContent = label; if (cls) e.className = cls; return e; };
+const button = (label, action, cls = '') => { const e = el('button', label, cls); e.type = 'button'; e.onclick = action; return e; };
+const widget = (node, name) => node?.widgets?.find(w => w.name === name);
+const linked = (node, name) => node?.inputs?.some(input => input.name === name && input.link != null);
+const value = (node, name) => widget(node, name)?.value;
+function set(node, name, value) {
+ const w = widget(node, name); if (!w || linked(node, name)) return false;
+ w.value = value; w.callback?.(value); node.graph?.change(); node.setDirtyCanvas?.(true, true); return true;
+}
+function upstream(node, name) {
+ const input = node.inputs?.find(i => i.name === name);
+ const link = input?.link != null ? node.graph.links[input.link] : null;
+ return link ? node.graph.getNodeById(link.origin_id) : null;
+}
+function view(file, type = 'input') {
+ const split = file.lastIndexOf('/');
+ return api.apiURL('/view?' + new URLSearchParams({filename: file.slice(split + 1), subfolder: split < 0 ? '' : file.slice(0, split), type}));
+}
+const number = (n, divisor = 1, unit = 's') => Number.isFinite(n) && n >= 0 ? `${(n / divisor).toFixed(1)} ${unit}` : '—';
+
+export const loraWarning = () => t(
+ 'FreeVideo needs no acceleration LoRA.',
+ 'FreeVideo无需添加加速LoRA');
+
+export function promptGuide() {
+ const note = el('p', null, 'fv-prompt-guide');
+ const link = el('a', 'MiniMax H3 Prompt Skill ↗');
+ link.href = 'https://github.com/MiniMax-AI/MiniMax-H3/blob/main/skills/h3-prompt-writing/SKILL.md';
+ link.target = '_blank'; link.rel = 'noopener noreferrer';
+ note.append(document.createTextNode(t('For better results, ask AI to rewrite your prompt using ', '让AI参考 ')),
+ link, document.createTextNode(t('. We recommend ', ' 重写提示词效果会更好。推荐使用 ')));
+ const freeToken = el('a', 'FreeToken ↗');
+ freeToken.href = 'https://www.flashml.ai/';
+ freeToken.target = '_blank'; freeToken.rel = 'noopener noreferrer';
+ note.append(freeToken, document.createTextNode(t('.', '。')));
+ return note;
+}
+
+export function loraPanel(node, panel) {
+ const list = el('div', null, 'fv-lora-list'), note = el('p', '', 'fv-lora-note');
+ const actions = el('div', null, 'fv-lora-add'); panel.append(list, actions, note);
+ let names = [], disposed = false;
+ const read = () => { const rows = JSON.parse(value(node, 'adapters') || '[]'); if (!Array.isArray(rows) || rows.length > 32) throw new Error(t('Use at most 32 LoRAs', '最多使用 32 个 LoRA')); return rows; };
+ const write = rows => { set(node, 'adapters', JSON.stringify(rows)); window.dispatchEvent(new CustomEvent('freevideo-loras', {detail: node.id})); };
+ function render() {
+ if (disposed) return; list.replaceChildren();
+ try {
+ const rows = read();
+ rows.forEach((row, index) => {
+ const wrap = el('div', null, 'fv-lora-row'); wrap.dataset.enabled = String(row.enabled !== false);
+ const enabled = el('input'); enabled.type = 'checkbox'; enabled.checked = row.enabled !== false;
+ enabled.setAttribute('aria-label', t('Enable LoRA', '启用 LoRA')); enabled.onchange = () => { row.enabled = enabled.checked; write(rows); };
+ const select = el('select'); select.setAttribute('aria-label', t('LoRA model', 'LoRA 模型'));
+ for (const name of [...new Set([row.name, ...names])].filter(Boolean)) { const option = el('option', name); option.value = name; select.append(option); }
+ select.value = row.name; select.title = row.name; select.onchange = () => { row.name = select.value; write(rows); };
+ const strength = el('input'); strength.type = 'number'; strength.min = '-4'; strength.max = '4'; strength.step = '.05'; strength.value = row.strength ?? 1;
+ strength.setAttribute('aria-label', t('LoRA strength', 'LoRA 强度')); strength.onchange = () => { if (strength.reportValidity() && strength.value !== '') { row.strength = Number(strength.value); write(rows); } };
+ select.disabled = strength.disabled = row.enabled === false;
+ const remove = button('×', () => { rows.splice(index, 1); write(rows); }); remove.setAttribute('aria-label', t('Remove LoRA', '移除 LoRA'));
+ wrap.append(enabled, select, strength, remove);
+ if (rows.length > 1) {
+ const order = el('div', null, 'fv-lora-order');
+ for (const [label, delta] of [['↑', -1], ['↓', 1]]) {
+ const move = button(label, () => { [rows[index], rows[index + delta]] = [rows[index + delta], rows[index]]; write(rows); });
+ move.disabled = index + delta < 0 || index + delta >= rows.length;
+ move.setAttribute('aria-label', delta < 0 ? t('Move LoRA earlier', 'LoRA 前移') : t('Move LoRA later', 'LoRA 后移')); order.append(move);
+ }
+ wrap.append(order);
+ }
+ list.append(wrap);
+ });
+ note.textContent = loraWarning();
+ } catch (error) { note.textContent = error.message; }
+ }
+ const add = button(t('+ Add LoRA', '+ 添加 LoRA'), () => {
+ try { const rows = read(); if (rows.length >= 32) throw new Error(t('Use at most 32 LoRAs', '最多使用 32 个 LoRA')); if (!names.length) throw new Error(t('Put H3 safetensors adapters in your ComfyUI LoRA folders, then refresh.', '将 H3 safetensors LoRA 放入 ComfyUI 的 LoRA 目录,然后刷新。')); rows.push({name: names[0], strength: 1, enabled: true}); write(rows); } catch (error) { note.textContent = error.message; }
+ });
+ async function refresh() {
+ try { const response = await api.fetchApi('/object_info/FreeVideoLoRA'); if (!response.ok) throw new Error(response.statusText); const info = await response.json(); const spec = info.FreeVideoLoRA?.input?.required?.lora; const options = Array.isArray(spec?.[0]) ? spec[0] : spec?.[1]?.options || []; names = options.filter(n => typeof n === 'string' && n.toLowerCase().endsWith('.safetensors')); render(); }
+ catch (error) { if (!disposed) note.textContent = error.message; }
+ }
+ actions.append(add, button(t('Refresh', '刷新'), refresh));
+ const changed = e => { if (String(e.detail) === String(node.id)) render(); };
+ window.addEventListener('freevideo-loras', changed);
+ const configure = node.onConfigure;
+ // Only the persistent node panel participates in graph loading.
+ if (panel.classList.contains('fv-lora-panel')) node.onConfigure = function (...args) { const result = configure?.apply(this, args); render(); return result; };
+ render(); refresh();
+ return () => { disposed = true; window.removeEventListener('freevideo-loras', changed); };
+}
+
+let current;
+const studioQueues = new WeakMap();
+export function openStudio(node) {
+ if (current?.node === node && current.dialog.open) { current.dialog.focus(); return; }
+ current?.dialog.close();
+ const dialog = el('dialog', null, 'fv-studio'); dialog.setAttribute('aria-label', t('FreeVideo creative workspace', 'FreeVideo 创作面板'));
+ current = {node, dialog}; const cleanup = [];
+ const header = el('header', null, 'fv-header'), brand = el('div', null, 'fv-brand');
+ brand.append(wordmark());
+ const tools = el('div', null, 'fv-header-actions');
+ tools.append(button(t('Settings', '设置'), openSetup, 'fv-quiet'));
+ const navigation = viewSwitch('studio', () => {}, () => {
+ closeDialog(dialog);
+ // Fit the graph in the visible canvas; never rewrite users' node positions.
+ const nodes = node.graph?._nodes || [], canvas = app.canvas?.canvas;
+ if (!nodes.length || !canvas) return;
+ const left = Math.min(...nodes.map(n => n.pos[0])), top = Math.min(...nodes.map(n => n.pos[1] - 32));
+ const width = Math.max(...nodes.map(n => n.pos[0] + n.size[0])) - left;
+ const height = Math.max(...nodes.map(n => n.pos[1] + n.size[1])) - top;
+ const scale = Math.min(1, (canvas.clientWidth - 96) / width, (canvas.clientHeight - 96) / height);
+ if (scale > 0) {
+ app.canvas.ds.scale = scale;
+ app.canvas.ds.offset = [-left + (canvas.clientWidth / scale - width) / 2, -top + (canvas.clientHeight / scale - height) / 2];
+ app.canvas.setDirty(true, true);
+ }
+ });
+ header.append(brand, navigation, tools);
+ const body = el('div', null, 'fv-body'), controls = el('div', null, 'fv-controls'), output = el('div', null, 'fv-preview-column');
+ dialog.append(header, body); body.append(controls, output);
+ const section = (label, parent = controls) => { const wrap = el('section', null, 'fv-section'); wrap.append(el('div', label, 'fv-label')); parent.append(wrap); return wrap; };
+ const field = (name, input) => { const label = el('label', null, 'fv-field'); label.append(el('span', name), input); return label; };
+ const expand = label => { const d = el('details', null, 'fv-section'); d.append(el('summary', label, 'fv-section-title')); const content = el('div', null, 'fv-expand'); d.append(content); controls.append(d); cleanup.push(animateDetails(d)); return [d, content]; };
+ const prompt = el('textarea'); prompt.value = value(node, 'text') || ''; prompt.placeholder = t('Describe the scene, movement, dialogue and sound…', '描述画面、动作、对白和声音…'); prompt.setAttribute('aria-label', t('Prompt', '提示词'));
+ prompt.disabled = linked(node, 'text'); prompt.oninput = () => set(node, 'text', prompt.value);
+ section(t('Describe your scene', '描述画面')).append(prompt, promptGuide());
+ const canvas = section(t('Frame & duration', '画幅与时长'));
+ const shapes = el('div', null, 'fv-shapes'); canvas.append(shapes);
+ const dimensionsLinked = linked(node, 'width') || linked(node, 'height');
+ let selected = node.properties?.freevideo_aspect || 'custom', ratio = Number(value(node, 'width')) / Number(value(node, 'height'));
+ const initialWidth = value(node, 'width'), initialHeight = value(node, 'height');
+ if (selected === 'custom') {
+ const match = [['16:9', 16 / 9], ['9:16', 9 / 16], ['1:1', 1]].find(([, r]) => Math.abs(Math.log(ratio / r)) < .035);
+ if (match) selected = match[0];
+ }
+ // Preserve the requested pixel target, not the rounded canvas area. Using
+ // the latter repeatedly shrinks a followed image after save/reload.
+ let pixels = selected !== 'custom' && Number.isFinite(node.properties?.freevideo_pixels)
+ ? node.properties.freevideo_pixels : (initialWidth * initialHeight) / 1024 ** 2;
+ let disposed = false;
+ const mp = el('select'); mp.setAttribute('aria-label', t('Total pixels', '总像素'));
+ for (const [n, label] of [[.5, '0.50 MP'], [.75, '0.75 MP'], [.98, t('0.98 MP · Native', '0.98 MP · 原生')], [1, '1.00 MP'], [1.5, '1.50 MP'], [2, '2.00 MP']]) { const option = el('option', label); option.value = n; mp.append(option); }
+ const nearest = [...mp.options].find(o => Math.abs(Number(o.value) - pixels) < .03);
+ if (nearest) mp.value = nearest.value; else { const option = el('option', `${pixels.toFixed(2)} MP`); option.value = pixels; mp.append(option); mp.value = pixels; }
+ mp.title = t('1 MP = 1024 × 1024, as in ComfyUI.', '1 MP = 1024 × 1024,与 ComfyUI 一致。'); mp.disabled = dimensionsLinked;
+ const seconds = el('input'); seconds.type = 'number'; seconds.min = '1.625'; seconds.max = '60'; seconds.step = 'any'; seconds.value = value(node, 'seconds'); seconds.disabled = linked(node, 'seconds'); seconds.setAttribute('aria-label', t('Duration in seconds', '时长(秒)'));
+ const fields = el('div', null, 'fv-fields'); fields.append(field(t('Total pixels', '总像素'), mp), field(t('Duration · seconds', '时长 · 秒'), seconds)); canvas.append(fields);
+ const canvasNote = el('div', null, 'fv-canvas-note'), dimensions = el('span'), duration = el('span'); canvasNote.append(dimensions, duration); canvas.append(canvasNote);
+ const twoPass = el('input'); twoPass.type = 'checkbox'; twoPass.checked = value(node, 'two_pass') !== false;
+ twoPass.disabled = linked(node, 'two_pass') || !widget(node, 'two_pass');
+ twoPass.onchange = () => set(node, 'two_pass', twoPass.checked);
+ const twoPassLabel = el('label', null, 'fv-two-pass');
+ twoPassLabel.title = t('Generate the scene, then refine it at the target resolution.', '先生成画面,再以目标分辨率精修。');
+ twoPass.title = twoPassLabel.title;
+ twoPassLabel.append(twoPass, el('span', t('Two-pass sampling', '二次采样')));
+ canvas.append(twoPassLabel);
+ const mediaNode = upstream(node, 'media');
+ const [mediaDetails, mediaMount] = expand(t('Media', '素材'));
+ if (mediaNode?.freevideoCreateMediaEditor) { cleanup.push(mediaNode.freevideoCreateMediaEditor(mediaMount)); mediaDetails.open = JSON.parse(value(mediaNode, 'assets') || '[]').some(r => r.enabled !== false) || mediaNode.inputs?.some(input => ['first', 'last', 'reference', 'reference_audio'].includes(input.name) && input.link != null); }
+ else mediaMount.append(el('p', t('Connect a FreeVideo Media panel in node view to upload here.', '在节点视图连接 FreeVideo 素材面板,即可在这里上传。'), 'fv-legacy'));
+ const loraNode = upstream(node, 'loras');
+ if (loraNode) {
+ const [loraDetails, loraMount] = expand(t('LoRAs', 'LoRA 组合'));
+ if (widget(loraNode, 'adapters')) { cleanup.push(loraPanel(loraNode, loraMount)); loraDetails.open = JSON.parse(value(loraNode, 'adapters') || '[]').length > 0; }
+ else if (widget(loraNode, 'lora')) {
+ const select = el('select'); select.setAttribute('aria-label', t('LoRA model', 'LoRA 模型'));
+ for (const name of widget(loraNode, 'lora').options?.values || ['None']) { const o = el('option', name); o.value = name; select.append(o); }
+ select.value = value(loraNode, 'lora'); select.onchange = () => set(loraNode, 'lora', select.value);
+ const strength = el('input'); strength.type = 'number'; strength.min = '-4'; strength.max = '4'; strength.step = '.05'; strength.value = value(loraNode, 'strength'); strength.onchange = () => { if (strength.reportValidity()) set(loraNode, 'strength', Number(strength.value)); };
+ loraMount.append(field(t('LoRA', 'LoRA'), select), field(t('Strength', '强度'), strength), el('p', loraWarning(), 'fv-lora-note'));
+ } else loraMount.append(el('p', loraWarning(), 'fv-lora-note'));
+ }
+ const [, advanced] = expand(t('Advanced', '高级选项'));
+ const custom = el('div', null, 'fv-fields');
+ const width = el('input'), height = el('input');
+ for (const [w, name, label] of [[width, 'width', t('Width', '宽度')], [height, 'height', t('Height', '高度')]]) { w.type = 'number'; w.min = '256'; w.max = '4096'; w.step = '32'; w.value = value(node, name); w.disabled = dimensionsLinked; w.setAttribute('aria-label', label); custom.append(field(label, w)); w.onchange = () => { if (w.value && w.reportValidity()) { set(node, name, Number(w.value)); selected = 'custom'; node.properties.freevideo_aspect = selected; ratio = Number(width.value) / Number(height.value); updateCanvas(); } }; }
+ advanced.append(custom);
+ const seedFields = el('div', null, 'fv-fields'); seedFields.style.marginTop = '14px';
+ const seed = el('input'); seed.type = 'number'; seed.min = '0'; seed.max = String(2 ** 53 - 1); seed.step = '1'; seed.value = value(node, 'seed'); seed.disabled = linked(node, 'seed'); seed.setAttribute('aria-label', t('Seed', '种子'));
+ seed.onchange = () => { if (seed.value && seed.reportValidity()) set(node, 'seed', Number(seed.value)); };
+ const mode = el('select'); for (const [v, label] of [['randomize', t('New each run', '每次随机')], ['fixed', t('Keep fixed', '保持固定')], ['increment', t('Increment', '递增')], ['decrement', t('Decrement', '递减')]]) { const o = el('option', label); o.value = v; mode.append(o); }
+ mode.value = value(node, 'control_after_generate') || 'fixed'; mode.onchange = () => set(node, 'control_after_generate', mode.value);
+ seedFields.append(field(t('Seed', '种子'), seed), field(t('After generation', '生成后'), mode)); advanced.append(seedFields);
+ const previewHead = el('div', null, 'fv-preview-head'); previewHead.append(el('strong', t('Preview', '预览')));
+ const previewCaption = el('span'), previewTools = el('div', null, 'fv-preview-tools');
+ previewTools.append(previewCaption, button(t('Creations', '作品'), () => {
+ stage.querySelector('video')?.pause(); openLibrary(t);
+ })); previewHead.append(previewTools); output.append(previewHead);
+ const previewSpace = el('div', null, 'fv-preview-space'); output.append(previewSpace);
+ const stage = el('div', null, 'fv-stage');
+ stage.setAttribute('aria-label', t('Video preview', '视频预览'));
+ const stageMedia = el('div', null, 'fv-stage-media');
+ stage.append(stageMedia); previewSpace.append(stage);
+ const progress = createGenerationProgress(t, undefined, {compact: true}); stage.append(progress.element);
+ cleanup.push(() => progress.dispose());
+ cleanup.push(createPreviewScene(stage, progress.element, stageMedia));
+ let revealTimer = null;
+ function hideProgress() {
+ clearTimeout(revealTimer); revealTimer = null;
+ delete stage.dataset.revealing; progress.hide();
+ }
+ cleanup.push(hideProgress);
+ let previewRatio = Number(initialWidth) / Number(initialHeight);
+ function fitPreview() {
+ const {width, height} = previewSpace.getBoundingClientRect();
+ if (!width || !height || !Number.isFinite(previewRatio) || previewRatio <= 0) return;
+ const fittedWidth = Math.min(width, height * previewRatio);
+ stage.style.width = `${fittedWidth}px`; stage.style.height = `${fittedWidth / previewRatio}px`;
+ stage.dataset.short = String(fittedWidth / previewRatio < 150);
+ }
+ function previewSize(w, h) {
+ if (!(w > 0 && h > 0)) return;
+ previewRatio = w / h;
+ dimensionText(previewCaption, `${w} × ${h}`);
+ fitPreview();
+ }
+ function dimensionText(element, text) {
+ if (element.textContent === text) return;
+ const animate = element.textContent && !matchMedia('(prefers-reduced-motion: reduce)').matches;
+ element.textContent = text;
+ if (animate && element.animate) {
+ for (const animation of element.getAnimations()) animation.cancel();
+ element.animate([{opacity: .25, transform: 'translateY(3px)'}, {opacity: 1, transform: 'translateY(0)'}],
+ {duration: 280, easing: 'ease-out'});
+ }
+ }
+ const previewObserver = new ResizeObserver(fitPreview);
+ previewObserver.observe(previewSpace); cleanup.push(() => previewObserver.disconnect());
+ function showProgress(message) {
+ clearTimeout(revealTimer); revealTimer = null; delete stage.dataset.revealing;
+ if (progress.element.hidden) stage.querySelector('video')?.pause();
+ progress.update(message);
+ }
+ const status = el('div', '', 'fv-status'); status.setAttribute('role', 'status');
+ const stats = el('div', null, 'fv-stats'), budget = el('div', '', 'fv-budget'), links = el('div', null, 'fv-result-links');
+ stats.hidden = true;
+ const metrics = [];
+ for (const label of [t('Sampling', '采样耗时'), t('Request total', '请求总计'), t('VRAM peak', '显存峰值'), t('RAM peak', '内存峰值')]) { const box = el('div', null, 'fv-stat'), n = el('strong', '—'); box.append(n, el('span', label)); stats.append(box); metrics.push(n); }
+ const prewarm = el('div', node.freevideoPrewarm || '', 'fv-prewarm');
+ output.append(status, stats, budget, links, prewarm);
+ const failure = createErrorPanel(t); output.append(failure.element);
+ if (node.freevideoFailure) failure.show(node.freevideoFailure, false);
+ let result = node.freevideoLastResult || app.nodeOutputs?.[node.id]?.freevideo_summary?.[0];
+ function showResult(r) {
+ if (!r?.video || disposed) return; result = r;
+ stage.querySelector('video')?.pause(); stageMedia.replaceChildren();
+ const video = el('video'); video.src = view(r.video, 'output'); video.controls = true; video.preload = 'metadata'; video.playsInline = true; stageMedia.append(video);
+ stats.hidden = false;
+ const shown = [number(r.sample_seconds), number(r.request_seconds), number(r.vram_peak_bytes, 2 ** 30, 'GiB'), number(r.ram_peak_bytes, 2 ** 30, 'GiB')]; metrics.forEach((e, i) => e.textContent = shown[i]);
+ budget.textContent = Number.isFinite(r.gpu_budget_bytes) ? `${t('VRAM budget', '可用显存预算')} ${number(r.gpu_budget_bytes, 2 ** 30, 'GiB')} · ${t('Device', '显卡总量')} ${number(r.gpu_total_bytes, 2 ** 30, 'GiB')}` : '';
+ links.replaceChildren();
+ for (const [label, file, cls] of [[t('Download video', '下载视频'), r.video, 'fv-primary'], [t('Report', '查看报告'), r.report, 'fv-quiet']]) { if (!file) continue; const a = el('a', label, cls); a.href = view(file, 'output'); a.download = file.split('/').pop(); links.append(a); }
+ const g = r.geometry; if (g?.width && g?.height) previewSize(g.width, g.height);
+ if (!progress.element.hidden) {
+ progress.update({phase: 'complete', overall: {status: 'complete', fraction: 1, remaining_seconds: 0}});
+ stage.dataset.revealing = 'true';
+ clearTimeout(revealTimer); revealTimer = setTimeout(hideProgress, 550);
+ }
+ }
+ const runbar = el('div', null, 'fv-runbar');
+ let busy = false, queued = false, cancelling = false, activePrompt = null, capturing = false;
+ let runMode = 'single', queueState;
+ if (!studioQueues.has(node)) studioQueues.set(node, createStudioQueue(api, node.id, {
+ // Direct snapshot submission still feeds the same graph result panel.
+ onResult: output => {
+ if (app.graph?.getNodeById(node.id) !== node) { node.freevideoLastResult = output.freevideo_summary[0]; return; }
+ app.nodeOutputs = {...app.nodeOutputs, [node.id]: output};
+ if (node.freevideoLastResult?.video !== output.freevideo_summary[0].video) node.freevideoShowResult?.(output);
+ },
+ }));
+ const queueController = studioQueues.get(node);
+ if (!node.freevideoQueueCleanup) {
+ node.freevideoQueueCleanup = true;
+ const removed = node.onRemoved;
+ node.onRemoved = function (...args) {
+ queueController.stopLoop().catch(() => {}).finally(() => queueController.dispose());
+ return removed?.apply(this, args);
+ };
+ }
+ const runrow = el('div', null, 'fv-runrow');
+ const runOptions = el('details', null, 'fv-run-options');
+ const optionsToggle = el('summary'); optionsToggle.title = t('Generation mode', '生成方式');
+ optionsToggle.setAttribute('aria-label', optionsToggle.title);
+ optionsToggle.append(el('span', null, 'fv-chevron'));
+ const optionsPanel = el('div', null, 'fv-run-menu');
+ optionsPanel.append(el('strong', t('Generation mode', '生成方式')));
+ const modeButtons = [];
+ for (const [id, label] of [['single', t('One video', '单次生成')], ['batch', t('Batch', '批量生成')], ['loop', t('Loop', '循环生成')]]) {
+ const b = button(label, () => { runMode = id; updateRunButton(); });
+ b.dataset.mode = id; modeButtons.push(b); optionsPanel.append(b);
+ }
+ const count = el('input'); count.type = 'number'; count.min = '2'; count.max = '100'; count.step = '1'; count.value = '4';
+ count.setAttribute('aria-label', t('Number of videos', '生成数量'));
+ const countField = field(t('Number of videos', '生成数量'), count); optionsPanel.append(countField);
+ const modeHelp = el('p', '', 'fv-muted'); optionsPanel.append(modeHelp);
+ count.oninput = () => updateRunButton();
+ runOptions.append(optionsToggle, optionsPanel);
+ const generate = button('', submitDraft, 'fv-primary');
+ runrow.append(generate, runOptions);
+ const queueActions = el('div', null, 'fv-queue-actions');
+ const queueDetails = el('details', null, 'fv-queue-details');
+ const queueToggle = el('summary'), queueList = el('div', null, 'fv-queue-list');
+ queueDetails.append(queueToggle, queueList);
+ const cancelCurrent = button(t('Cancel current', '取消当前'), cancelGeneration, 'fv-quiet');
+ const stopLoop = button(t('Stop after this video', '完成当前后停止'), async () => {
+ stopLoop.disabled = true;
+ try { await queueController.stopLoop(); }
+ catch (error) { showQueueError(error); }
+ finally { stopLoop.disabled = false; }
+ }, 'fv-quiet');
+ queueActions.append(queueDetails, cancelCurrent, stopLoop);
+ runbar.append(runrow, queueActions); controls.append(runbar);
+ const queueError = error => ({
+ queue_unavailable: t('Could not read the queue. Any loop has been stopped; already submitted videos stay queued.', '暂时无法读取队列。循环已停止追加,已提交的任务仍在队列中。'),
+ cancel_failed: t('Could not cancel. Try again.', '暂时未能取消,请重试。'),
+ submit_failed: t('The workflow was not queued. Check the node settings.', '工作流未能提交,请检查节点设置。'),
+ loop_active: t('Stop the current loop before starting another.', '请先停止当前循环。'),
+ loop_stopped: t('The last request did not complete. The loop has stopped.', '上一条任务未完成,循环已停止。'),
+ linked_seed: t('Use the seed in this panel for batch or loop generation.', '批量和循环生成需使用面板内的种子,请先断开种子输入的连接。'),
+ missing_node: t('This node is disabled or no longer in the workflow.', '该节点已禁用或已从工作流移除。'),
+ }[error?.message] || error?.message || String(error));
+ function showQueueError(error) { status.dataset.error = 'true'; status.textContent = queueError(error); }
+ function updateRunButton() {
+ const submitting = capturing || queueState?.submitting;
+ generate.disabled = submitting || (runMode === 'loop' && queueState?.looping);
+ generate.textContent = submitting ? t('Adding…', '正在添加…')
+ : runMode === 'batch' ? t(`Add ${count.value || '…'} videos`, `添加 ${count.value || '…'} 条任务`)
+ : runMode === 'loop' ? (queueState?.looping ? t('Looping', '循环中') : t('Start loop', '开始循环'))
+ : busy ? t('Add to queue', '添加到队列') : t('Generate video', '生成视频');
+ for (const b of modeButtons) b.setAttribute('aria-pressed', String(b.dataset.mode === runMode));
+ countField.hidden = runMode !== 'batch'; count.disabled = runMode !== 'batch';
+ modeHelp.hidden = runMode === 'single';
+ modeHelp.textContent = runMode === 'loop'
+ ? t('Use these settings with a new seed each time. Closing this page stops adding videos.', '固定当前设置,每条使用不同种子。关闭网页后不再追加。')
+ : t('Use these settings with a different seed for each video.', '固定当前设置,每条使用不同种子。');
+ cancelCurrent.hidden = !queueState?.running;
+ cancelCurrent.disabled = cancelling;
+ cancelCurrent.textContent = cancelling ? t('Cancelling…', '正在取消…') : t('Cancel current', '取消当前');
+ stopLoop.hidden = !queueState?.looping;
+ }
+ function renderQueue(state) {
+ if (disposed) return;
+ queueState = state;
+ const active = state.running || state.pending[0];
+ activePrompt = active?.id || null;
+ busy = Boolean(active); queued = !state.running && Boolean(state.pending.length);
+ const g = active?.inputs;
+ // The right-hand frame belongs to the submitted video. Draft edits on
+ // the left must never reshape a video currently being generated.
+ if (Number.isFinite(g?.width) && Number.isFinite(g?.height)) previewSize(g.width, g.height);
+ queueToggle.textContent = t(`Queue · ${state.pending.length}`, `等待 ${state.pending.length} 条`);
+ queueDetails.hidden = state.pending.length === 0;
+ queueActions.hidden = !busy && !state.looping;
+ const signature = JSON.stringify(state.pending.map(row => [row.id, row.inputs.width, row.inputs.height]));
+ if (queueList.dataset.signature !== signature) {
+ queueList.dataset.signature = signature; queueList.replaceChildren();
+ state.pending.forEach((row, index) => {
+ const item = el('div', null, 'fv-queue-item');
+ const description = el('span', `${index + 1} · ${row.inputs.width} × ${row.inputs.height} · ${row.inputs.seconds} s`);
+ const remove = button(t('Remove', '移除'), async () => {
+ remove.disabled = true;
+ try { await queueController.cancel(row.id); }
+ catch (error) { showQueueError(error); remove.disabled = false; }
+ }, 'fv-quiet');
+ remove.setAttribute('aria-label', t(`Remove queued video ${index + 1}`, `移除第 ${index + 1} 条排队任务`));
+ item.append(description, remove); queueList.append(item);
+ });
+ }
+ if (!busy) cancelling = false;
+ if (state.error) showQueueError(state.error);
+ updateRunButton();
+ }
+ const syncQueue = () => queueController.refresh();
+ async function cancelGeneration() {
+ if (cancelling) return;
+ try {
+ cancelling = true; updateRunButton();
+ await syncQueue();
+ const id = queueController.state().running?.id;
+ if (!id) { cancelling = false; updateRunButton(); return; }
+ cancelling = true; updateRunButton();
+ const reply = await queueController.cancel(id);
+ status.dataset.error = 'false';
+ status.textContent = reply.status === 'cancelling' ? t('Stopping generation…', '正在停止生成…') : '';
+ } catch (error) { cancelling = false; updateRunButton(); showQueueError(error); }
+ }
+ async function submitDraft() {
+ if (capturing || queueState?.submitting) return;
+ if ([...dialog.querySelectorAll('input:not(:disabled)')].some(e => !e.reportValidity())) return;
+ if (!prompt.disabled && !prompt.value.trim()) { prompt.focus(); status.textContent = t('Describe your scene first.', '请先描述画面。'); return; }
+ const chosenMode = runMode, batchCount = chosenMode === 'batch' ? Number(count.value) : 1;
+ const originalSeed = value(node, 'seed'), seedMode = mode.value;
+ try {
+ capturing = true; updateRunButton(); status.dataset.error = 'false'; runOptions.open = false;
+ // Only serialization briefly locks the editor. Network submission
+ // and all GPU execution leave the next draft fully editable.
+ for (const section of controls.querySelectorAll('.fv-section')) section.inert = true;
+ if (selected === 'input' && !dimensionsLinked) await inputRatio(true);
+ const snapshot = structuredClone(await app.graphToPrompt());
+ const seedIndex = node.widgets.findIndex(w => w.name === 'seed');
+ capturing = false;
+ for (const section of controls.querySelectorAll('.fv-section')) section.inert = false;
+ await queueController.start(snapshot, {count: batchCount, repeat: chosenMode === 'loop', seedIndex});
+ // Preserve draft changes made while the POST was in flight.
+ if (chosenMode === 'single' && !linked(node, 'seed') && value(node, 'seed') === originalSeed) {
+ const max = Number.MAX_SAFE_INTEGER;
+ const next = seedMode === 'randomize' ? randomSeed() : seedMode === 'increment'
+ ? (originalSeed >= max ? 0 : originalSeed + 1) : seedMode === 'decrement'
+ ? (originalSeed <= 0 ? max : originalSeed - 1) : originalSeed;
+ set(node, 'seed', next); seed.value = next;
+ }
+ if (!queueController.state().error) status.textContent = '';
+ } catch (error) { showQueueError(error); }
+ finally {
+ capturing = false;
+ for (const section of controls.querySelectorAll('.fv-section')) section.inert = false;
+ updateRunButton();
+ }
+ }
+ cleanup.push(queueController.subscribe(renderQueue));
+ const closeMenus = event => {
+ if (!runOptions.contains(event.target)) runOptions.open = false;
+ if (!queueDetails.contains(event.target)) queueDetails.open = false;
+ };
+ dialog.addEventListener('pointerdown', closeMenus);
+ function updateCanvas() {
+ width.value = value(node, 'width'); height.value = value(node, 'height');
+ dimensionText(dimensions, dimensionsLinked ? t('Size from connected nodes', '尺寸来自已连接节点') : `${width.value} × ${height.value}`);
+ const requested = Math.ceil(Number(seconds.value) * 24), frames = requested + ((5 - requested) % 17 + 17) % 17;
+ duration.textContent = Number.isFinite(frames) ? `${(frames / 24).toFixed(3)} s · 24 fps` : '—';
+ for (const b of shapes.children) b.setAttribute('aria-pressed', String(b.dataset.ratio === selected));
+ const shapeIndex = [...shapes.children].findIndex(b => b.dataset.ratio === selected);
+ shapes.style.setProperty('--fv-shape-index', Math.max(0, shapeIndex));
+ shapes.dataset.custom = String(shapeIndex < 0);
+ if (!busy) previewSize(Number(width.value), Number(height.value));
+ }
+ function applySize() {
+ if (dimensionsLinked) return;
+ const total = pixels * 1024 ** 2;
+ const w = Math.round(Math.sqrt(total * ratio) / 32) * 32, h = Math.round(Math.sqrt(total / ratio) / 32) * 32;
+ if (w < 256 || h < 256 || w > 4096 || h > 4096) throw new Error(t('This aspect ratio and pixel count exceed the supported dimensions. Use custom dimensions.', '该比例与像素数超出尺寸范围,请使用自定义宽高。'));
+ set(node, 'width', w); set(node, 'height', h); node.properties.freevideo_aspect = selected; node.properties.freevideo_pixels = pixels; updateCanvas();
+ }
+ async function inputRatio(required = false) {
+ const rows = mediaNode ? JSON.parse(value(mediaNode, 'assets') || '[]') : [];
+ const active = rows.filter(r => r.enabled !== false);
+ const source = active.find(r => r.role === 'first') || active.find(r => r.role === 'last') || active.find(r => /\.(png|jpe?g|webp|bmp|tiff?)$/i.test(r.file));
+ if (!source) { if (required) throw new Error(t('Add an image in Media to follow its aspect ratio.', '请先在素材中添加图片,再跟随输入比例。')); return; }
+ const image = new Image(); image.src = view(source.file);
+ await image.decode();
+ if (disposed || !image.naturalWidth || !image.naturalHeight) return;
+ if (selected === 'input') { ratio = image.naturalWidth / image.naturalHeight; applySize(); }
+ }
+ for (const [id, label, r] of [['16:9', t('Landscape', '横屏'), 16 / 9], ['9:16', t('Portrait', '竖屏'), 9 / 16], ['1:1', t('Square', '方形'), 1], ['input', t('Match image', '跟随图片'), null]]) {
+ const b = button('', async () => { const previous = selected; try { selected = id; pixels = Number(mp.value); if (id === 'input') await inputRatio(true); else { ratio = r; applySize(); } status.textContent = ''; status.dataset.error = 'false'; } catch (error) { selected = previous; updateCanvas(); status.dataset.error = 'true'; status.textContent = error.message; } }, 'fv-shape');
+ b.dataset.ratio = id; b.setAttribute('aria-label', label); b.disabled = dimensionsLinked;
+ b.append(el('span', null, 'fv-shape-icon'), el('span', label)); shapes.append(b);
+ }
+ mp.onchange = () => { try { pixels = Number(mp.value); applySize(); } catch (error) { status.dataset.error = 'true'; status.textContent = error.message; } };
+ seconds.onchange = () => { if (seconds.value && seconds.reportValidity()) { set(node, 'seconds', Number(seconds.value)); updateCanvas(); } };
+ const mediaChanged = async e => { if (String(e.detail) !== String(mediaNode?.id) || selected !== 'input') return; try { await inputRatio(); } catch (error) { status.textContent = error.message; } };
+ const resultChanged = e => { if (String(e.detail.node) === String(node.id)) { failure.clear(); showResult(e.detail.value); cancelling = false; status.textContent = ''; syncQueue().catch(() => {}); } };
+ const prewarmChanged = e => { if (String(e.detail.node) === String(node.id)) prewarm.textContent = e.detail.label; };
+ window.addEventListener('freevideo-prewarm', prewarmChanged);
+ cleanup.push(() => window.removeEventListener('freevideo-prewarm', prewarmChanged));
+ window.addEventListener('freevideo-media', mediaChanged); window.addEventListener('freevideo-result', resultChanged);
+ cleanup.push(() => { window.removeEventListener('freevideo-media', mediaChanged); window.removeEventListener('freevideo-result', resultChanged); });
+ const listen = (type, handler) => { api.addEventListener(type, handler); cleanup.push(() => api.removeEventListener(type, handler)); };
+ listen('status', () => { syncQueue().catch(() => {}); });
+ listen('executing', e => { const id = typeof e.detail === 'object' ? e.detail?.node : e.detail; if (String(id) === String(node.id)) { failure.clear(); node.freevideoFailure = ''; busy = true; queued = false; updateRunButton(); showProgress(node.freevideoProgress || {label: t('Preparing your video', '正在准备视频'), reset: true, new_request: true}); status.textContent = ''; } syncQueue().catch(() => {}); });
+ const progressChanged = e => {
+ if (String(e.detail.node) !== String(node.id)) return;
+ if (['failed', 'cancelled'].includes(e.detail.phase) || e.detail.result) {
+ busy = cancelling = false; updateRunButton();
+ if (e.detail.phase === 'cancelled') { hideProgress(); status.textContent = t('Cancelled', '已取消'); }
+ else if (e.detail.phase === 'failed') { hideProgress(); status.textContent = t('Generation stopped', '生成已停止'); }
+ else if (revealTimer === null) hideProgress();
+ return;
+ }
+ busy = true; queued = false; updateRunButton(); status.dataset.error = 'false';
+ if (cancelling) return;
+ status.textContent = '';
+ showProgress(e.detail);
+ };
+ window.addEventListener('freevideo-progress', progressChanged);
+ cleanup.push(() => window.removeEventListener('freevideo-progress', progressChanged));
+ listen('execution_error', e => {
+ const seen = new Set(), pending = [node];
+ while (pending.length) {
+ const currentNode = pending.pop(); if (!currentNode || seen.has(String(currentNode.id))) continue;
+ seen.add(String(currentNode.id));
+ for (const input of currentNode.inputs || []) pending.push(upstream(currentNode, input.name));
+ }
+ if (!seen.has(String(e.detail?.node_id))) return;
+ busy = cancelling = false; updateRunButton(); hideProgress(); status.dataset.error = 'true';
+ node.freevideoFailure = errorText(e.detail);
+ failure.show(node.freevideoFailure);
+ status.textContent = t('Generation stopped. See the explanation below.', '生成已停止,原因和处理建议见下方。');
+ });
+ listen('execution_interrupted', e => {
+ if (activePrompt ? e.detail?.prompt_id !== activePrompt : String(e.detail?.node_id) !== String(node.id)) return;
+ busy = cancelling = false; activePrompt = null; updateRunButton(); hideProgress();
+ failure.clear(); node.freevideoFailure = ''; status.dataset.error = 'false'; status.textContent = t('Cancelled', '已取消');
+ });
+ const escape = e => { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); generate.click(); } }; dialog.addEventListener('keydown', escape);
+ dialog.addEventListener('cancel', e => { e.preventDefault(); closeDialog(dialog); });
+ dialog.onclose = () => { disposed = true; for (const f of cleanup) f(); stage.querySelector('video')?.pause(); dialog.remove(); if (current?.dialog === dialog) { current = null; viewChanged('nodes', node); } };
+ document.body.append(dialog); dialog.showModal(); viewChanged('studio', node); updateCanvas(); if (result) showResult(result);
+ // Do not change any saved width/height simply by opening a workflow.
+ syncQueue().then(async () => {
+ if (disposed) return;
+ if (busy) { showProgress(node.freevideoProgress || {label: queued ? t('Waiting in queue', '正在排队') : t('Syncing generation progress', '正在同步生成进度')}); }
+ else if (!result) {
+ const saved = await latestVideo();
+ if (!disposed && !busy && !result && saved) showResult(saved);
+ }
+ }).catch(() => {});
+ requestAnimationFrame(() => generate.focus({preventScroll: true}));
+}
diff --git a/web/studio_queue.js b/web/studio_queue.js
new file mode 100644
index 0000000..737e57c
--- /dev/null
+++ b/web/studio_queue.js
@@ -0,0 +1,157 @@
+// ComfyUI owns execution. This controller only submits frozen workflows and
+// replenishes a loop after its previous request has actually completed.
+const clone = value => structuredClone(value);
+export function randomSeed() {
+ const bytes = crypto.getRandomValues(new Uint32Array(2));
+ return (bytes[0] & 0x1fffff) * 0x100000000 + bytes[1];
+}
+
+export function seededPrompt(snapshot, nodeId, seedIndex, seed) {
+ const prompt = clone(snapshot);
+ if (Number.isSafeInteger(seed)) {
+ prompt.output[nodeId].inputs.seed = seed;
+ const node = prompt.workflow?.nodes?.find(row => String(row.id) === String(nodeId));
+ if (node && seedIndex >= 0 && Array.isArray(node.widgets_values)) node.widgets_values[seedIndex] = seed;
+ }
+ return prompt;
+}
+
+export function createStudioQueue(api, nodeId, {random = randomSeed, pollMs = 2000, onResult = () => {}} = {}) {
+ nodeId = String(nodeId);
+ let running = null, pending = [], submitting = false, loop = null, timer = null;
+ let refreshing = null, refreshAgain = false, lastError = null, disposed = false;
+ const listeners = new Set();
+ const owned = new Set(), delivered = new Set();
+ function remember(id) { owned.add(id); if (owned.size > 128) { const old = owned.values().next().value; owned.delete(old); delivered.delete(old); } }
+ function result(id, output) {
+ if (!owned.has(id) || delivered.has(id) || !output?.freevideo_summary?.[0]) return;
+ delivered.add(id); onResult(output);
+ }
+ const state = () => ({running, pending, submitting, looping: Boolean(loop?.active),
+ loopCompleted: loop?.completed || 0, error: lastError});
+ const publish = () => { for (const listener of listeners) listener(state()); };
+ function schedule() {
+ clearTimeout(timer); timer = null;
+ if (!disposed && pollMs && (listeners.size || loop?.active)) timer = setTimeout(() => refresh().catch(() => {}), pollMs);
+ }
+ function fail(error) {
+ if (loop) loop.active = false;
+ lastError = error; publish();
+ }
+ function matches(row) {
+ return row[2]?.[nodeId]?.class_type === 'FreeVideoGenerate'
+ && (!api.clientId || row[3]?.client_id === api.clientId);
+ }
+ const entry = row => ({id: row[1], inputs: clone(row[2][nodeId].inputs)});
+ async function cancel(id) {
+ const response = await api.fetchApi('/freevideo/cancel', {method: 'POST',
+ headers: {'Content-Type': 'application/json'}, body: JSON.stringify({prompt_id: id, node_id: nodeId})});
+ if (!response.ok) throw new Error('cancel_failed');
+ return response.json();
+ }
+ async function submit(session, randomized) {
+ let seed = session.snapshot.output[nodeId].inputs.seed;
+ if (randomized) {
+ const candidate = random();
+ seed = candidate === session.lastSeed ? (candidate + 1) % Number.MAX_SAFE_INTEGER : candidate;
+ }
+ session.lastSeed = seed;
+ const prompt = seededPrompt(session.snapshot, nodeId, session.seedIndex, seed);
+ const reply = await api.queuePrompt(0, prompt);
+ if (!reply?.prompt_id) throw new Error('submit_failed');
+ remember(reply.prompt_id);
+ if (session.repeat) {
+ session.waiting = reply.prompt_id;
+ // Stop may be clicked while the server is acknowledging submission.
+ if (!session.active) {
+ const response = await api.fetchApi('/queue');
+ if (response.ok && (await response.json()).queue_pending?.some(row => row[1] === reply.prompt_id)) await cancel(reply.prompt_id);
+ }
+ }
+ api.dispatchCustomEvent?.('promptQueued', {number: 0, batchCount: 1});
+ return reply.prompt_id;
+ }
+ async function advanceLoop() {
+ const session = loop;
+ if (!session?.active || session.sending || !session.waiting) return;
+ if (running?.id === session.waiting || pending.some(row => row.id === session.waiting)) return;
+ const response = await api.fetchApi('/history/' + encodeURIComponent(session.waiting));
+ if (!response.ok) throw new Error('queue_unavailable');
+ const history = (await response.json())[session.waiting];
+ if (loop !== session || !session.active) return;
+ result(session.waiting, history?.outputs?.[nodeId]);
+ // A missing entry can mean a deleted queued request or cleared history.
+ // Never guess success and start another expensive GPU job.
+ if (!history?.status?.completed || history.status.status_str !== 'success') {
+ session.active = false; lastError = new Error('loop_stopped'); return;
+ }
+ session.completed++; session.sending = true; publish();
+ try { await submit(session, true); refreshAgain = true; }
+ finally { session.sending = false; }
+ }
+ async function readQueue() {
+ const response = await api.fetchApi('/queue');
+ if (!response.ok) throw new Error('queue_unavailable');
+ const queue = await response.json();
+ running = (queue.queue_running || []).filter(matches).map(entry)[0] || null;
+ pending = (queue.queue_pending || []).filter(matches).map(entry);
+ for (const row of [running, ...pending]) if (row) remember(row.id);
+ await advanceLoop(); publish();
+ }
+ function refresh() {
+ if (disposed) return Promise.resolve();
+ if (refreshing) { refreshAgain = true; return refreshing; }
+ refreshing = (async () => {
+ do { refreshAgain = false; await readQueue(); } while (refreshAgain && !disposed);
+ })().catch(error => { fail(error); throw error; }).finally(() => { refreshing = null; schedule(); });
+ return refreshing;
+ }
+ async function start(snapshot, {count = 1, repeat = false, seedIndex = -1} = {}) {
+ if (submitting) throw new Error('submitting');
+ if (!Number.isInteger(count) || count < 1 || count > 100) throw new Error('invalid_count');
+ if (repeat && loop?.active) throw new Error('loop_active');
+ if (!snapshot.output?.[nodeId]) throw new Error('missing_node');
+ if ((repeat || count > 1) && !Number.isSafeInteger(snapshot.output[nodeId].inputs.seed)) throw new Error('linked_seed');
+ const session = {snapshot: clone(snapshot), seedIndex, repeat, active: repeat, completed: 0, waiting: null, sending: true};
+ if (repeat) loop = session;
+ submitting = true; lastError = null; publish();
+ try {
+ for (let i = 0; i < (repeat ? 1 : count); i++) await submit(session, repeat || count > 1);
+ } catch (error) { fail(error); throw error; }
+ finally { session.sending = false; submitting = false; publish(); await refresh().catch(() => {}); }
+ }
+ async function stopLoop() {
+ const session = loop;
+ if (!session) return;
+ session.active = false; publish();
+ await refresh();
+ if (pending.some(row => row.id === session.waiting)) await cancel(session.waiting);
+ await refresh();
+ }
+ const onStatus = () => { if (listeners.size || loop?.active) refresh().catch(() => {}); };
+ const onFailure = event => {
+ if (event.detail?.prompt_id === loop?.waiting) { loop.active = false; lastError = new Error('loop_stopped'); publish(); }
+ onStatus();
+ };
+ const onExecuted = event => {
+ if (String(event.detail?.node) === nodeId) result(event.detail?.prompt_id, event.detail?.output);
+ };
+ api.addEventListener('status', onStatus);
+ api.addEventListener('execution_error', onFailure);
+ api.addEventListener('execution_interrupted', onFailure);
+ api.addEventListener('executed', onExecuted);
+ return {
+ state, refresh, start, stopLoop,
+ subscribe(listener) { listeners.add(listener); listener(state()); schedule(); return () => { listeners.delete(listener); schedule(); }; },
+ async cancel(id) {
+ if (loop?.waiting === id) { loop.active = false; publish(); }
+ const reply = await cancel(id); await refresh(); return reply;
+ },
+ dispose() {
+ disposed = true; if (loop) loop.active = false; clearTimeout(timer); listeners.clear();
+ api.removeEventListener('status', onStatus); api.removeEventListener('execution_error', onFailure);
+ api.removeEventListener('execution_interrupted', onFailure);
+ api.removeEventListener('executed', onExecuted);
+ },
+ };
+}
diff --git a/web/theme.css b/web/theme.css
new file mode 100644
index 0000000..cd68ee9
--- /dev/null
+++ b/web/theme.css
@@ -0,0 +1,50 @@
+/* One token table for the web views, mirroring freevideo_engine/branding.py so
+ a control looks the same in the launcher and in ComfyUI. Scope the tokens to
+ our own views; never change ComfyUI's theme. */
+:where(.fv-studio,.fv-setup,.fv-panel,.fv-generation-progress,.fv-view-navigation,.fv-prompt-guide,.fv-failure-dialog){
+ /* Surfaces, from the backdrop up to a hovered control; mirrors branding.py. */
+ --fv-bg:#111720;--fv-canvas:#161e29;--fv-card:#1a222e;--fv-raised:#212a37;--fv-hover:#2a3341;
+ --fv-border:#2a3443;--fv-sheen:#35404e;
+ /* Ink. */
+ --fv-ink:#e7edf7;--fv-muted:#a5b3c6;--fv-disabled:#7f8ea3;
+ /* One hue per meaning; a subtle value backs its own status only. */
+ --fv-accent:#6db8fa;--fv-accent-hover:#91cbff;--fv-accent-ramp:#82dcd3;
+ --fv-accent-dim:#406c93;--fv-selected:#1d3248;
+ --fv-success:#76d9b3;--fv-success-subtle:#1f3636;
+ --fv-warning:#e0b062;
+ --fv-danger:#f09aa2;--fv-danger-subtle:#30212a;
+ /* Five text steps, three weights, four radii, three control heights, as
+ in the native views. Radius grows with the object: a glyph or tag (xs),
+ a control (sm), a card or tile (md), a sheet (lg). */
+ --fv-micro:12px;--fv-body:13px;--fv-strong:16px;--fv-section:20px;--fv-hero:29px;
+ --fv-regular:400;--fv-medium:500;--fv-semibold:600;
+ --fv-r-xs:3px;--fv-r-sm:8px;--fv-r-md:12px;--fv-r-lg:16px;
+ --fv-h-sm:32px;--fv-h:36px;--fv-h-lg:44px;
+ /* Motion: things arriving ease out; state changes use the standard curve. */
+ --fv-ease-out:cubic-bezier(.22,1,.36,1);--fv-ease:cubic-bezier(.4,0,.2,1);
+ --fv-quick:.14s;--fv-base:.24s;--fv-slow:.42s;
+ --fv-step:4px;
+ --fv-font:"Segoe UI","Microsoft YaHei UI","Noto Sans CJK SC","Noto Sans SC",system-ui,sans-serif;
+ font-family:var(--fv-font);
+ color-scheme:dark;
+}
+.fv-wordmark{display:block;width:210px;height:auto;max-width:100%;object-fit:contain}
+:where(.fv-studio,.fv-setup,.fv-panel,.fv-view-navigation){overflow-wrap:anywhere}
+:where(.fv-studio,.fv-setup,.fv-panel) :is(input,select,textarea,button){font-family:inherit}
+:where(.fv-studio,.fv-setup,.fv-panel) :is(button,a,summary){touch-action:manipulation}
+:where(.fv-studio,.fv-setup,.fv-panel) button{appearance:none}
+:where(.fv-studio,.fv-setup,.fv-panel) :focus-visible{outline:2px solid var(--fv-accent);outline-offset:3px}
+/* A modal takes focus itself while it fills; its controls carry the ring. */
+:where(.fv-studio,.fv-setup,.fv-failure-dialog):focus-visible{outline:none}
+:where(.fv-studio,.fv-setup,.fv-panel) progress{accent-color:var(--fv-accent);appearance:none;height:6px;border:0;border-radius:999px;overflow:hidden;background:var(--fv-raised)}
+:where(.fv-studio,.fv-setup,.fv-panel) progress::-webkit-progress-bar{background:var(--fv-raised);border-radius:999px}
+:where(.fv-studio,.fv-setup,.fv-panel) progress::-webkit-progress-value{background:linear-gradient(90deg,var(--fv-accent),var(--fv-accent-ramp));border-radius:999px;transition:width .5s var(--fv-ease-out)}
+:where(.fv-studio,.fv-setup,.fv-panel) progress::-moz-progress-bar{background:linear-gradient(90deg,var(--fv-accent),var(--fv-accent-ramp));border-radius:999px}
+
+/* Shared, brief transitions keep the two workspaces and settings coherent. */
+:where(.fv-studio,.fv-setup)[open]{animation:fv-dialog-enter .28s cubic-bezier(.2,.7,.2,1)}
+:where(.fv-studio,.fv-setup)::backdrop{animation:fv-backdrop-enter .22s ease}
+@keyframes fv-dialog-enter{from{opacity:0;transform:translateY(10px) scale(.99)}to{opacity:1;transform:translateY(0) scale(1)}}
+@keyframes fv-panel-enter{from{opacity:0;translate:0 5px}to{opacity:1;translate:0 0}}
+@keyframes fv-backdrop-enter{from{opacity:0}to{opacity:1}}
+@media(prefers-reduced-motion:reduce){:where(.fv-studio,.fv-setup)[open],:where(.fv-studio,.fv-setup)::backdrop{animation:none!important}}
diff --git a/web/view_navigation.css b/web/view_navigation.css
new file mode 100644
index 0000000..b25642c
--- /dev/null
+++ b/web/view_navigation.css
@@ -0,0 +1,17 @@
+.fv-view-navigation{box-sizing:border-box;position:fixed;top:76px;left:50%;transform:translateX(-50%);z-index:1000;max-width:calc(100vw - 48px);color:var(--fv-ink);background:color-mix(in srgb,var(--fv-card) 82%,transparent);-webkit-backdrop-filter:blur(20px) saturate(1.4);backdrop-filter:blur(20px) saturate(1.4);border:1px solid var(--fv-border);border-radius:var(--fv-r-lg);padding:8px 12px;box-shadow:0 8px 32px #0005;font:14px/1.5 var(--fv-font)}
+.fv-view-navigation[hidden]{display:none!important}.fv-view-row{display:flex;align-items:center;justify-content:center;gap:16px}.fv-view-row>.fv-wordmark{width:132px;height:auto;flex-shrink:0}
+.fv-view-switch{display:inline-flex;gap:2px;padding:3px;border:1px solid var(--fv-border);border-radius:calc(var(--fv-r-sm) + 3px);background:var(--fv-bg)}
+.fv-view-switch button,.fv-view-navigation button{font:inherit;cursor:pointer;border:1px solid transparent;color:var(--fv-muted);background:transparent;border-radius:var(--fv-r-sm);padding:6px 14px;font-weight:var(--fv-medium);white-space:nowrap;transition:background .2s,color .2s,border-color .2s}
+.fv-view-switch button[aria-pressed=true],.fv-studio .fv-view-switch button[aria-pressed=true]{background:var(--fv-raised);color:var(--fv-ink);font-weight:var(--fv-semibold);box-shadow:0 1px 3px #0006}.fv-view-switch button:hover{color:var(--fv-ink)}
+.fv-view-switch button:focus-visible,.fv-view-navigation button:focus-visible{outline:2px solid var(--fv-accent);outline-offset:2px}
+/* First-run tip: a tinted callout, not another line of grey text. */
+.fv-view-guide{display:flex;align-items:center;gap:10px;max-width:560px;margin-top:10px;padding:8px 8px 8px 12px;border-radius:var(--fv-r-sm);background:var(--fv-selected);font-size:12px;line-height:1.5;color:var(--fv-ink)}
+.fv-view-guide::before{content:'i';flex:none;display:grid;place-items:center;width:18px;height:18px;border-radius:50%;background:var(--fv-accent);color:var(--fv-bg);font:600 11px/1 Georgia,serif}
+.fv-view-guide>span{flex:1}
+.fv-view-guide button{flex:none;color:var(--fv-accent);padding:4px 8px;font-weight:var(--fv-semibold)}.fv-view-guide button:hover{background:#ffffff0f}.fv-view-navigation .fv-view-settings{font-size:12px;padding:6px}
+.fv-studio .fv-header:has(.fv-view-switch){grid-template-columns:1fr auto 1fr}.fv-studio .fv-header>.fv-header-actions{justify-self:end}.fv-studio .fv-view-switch button{padding:5px 14px;min-height:var(--fv-h-sm)}
+@media(max-width:680px){.fv-view-navigation{top:68px;max-width:calc(100vw - 16px)}.fv-view-row{gap:6px}.fv-view-row>.fv-wordmark{width:90px}.fv-view-switch button{padding:7px 10px}.fv-studio .fv-header:has(.fv-view-switch){grid-template-columns:1fr auto 1fr;gap:8px}.fv-studio .fv-header:has(.fv-view-switch) .fv-wordmark{width:90px}.fv-studio .fv-view-switch{justify-self:center}.fv-studio .fv-view-switch button{padding:6px 9px}}
+
+.fv-view-navigation{animation:fv-panel-enter .25s ease}
+@media(max-width:380px){.fv-view-row>.fv-wordmark{width:76px}.fv-view-navigation .fv-view-switch button{font-size:12px;padding:7px 8px}}
+@media(prefers-reduced-motion:reduce){.fv-view-navigation,.fv-view-navigation *{animation:none!important;transition:none!important}}
diff --git a/web/view_navigation.js b/web/view_navigation.js
new file mode 100644
index 0000000..fb70faa
--- /dev/null
+++ b/web/view_navigation.js
@@ -0,0 +1,92 @@
+import { app } from '../../scripts/app.js';
+import { openSetup } from './setup.js';
+import { wordmark } from './branding.js';
+
+const languageOverride = typeof location !== 'undefined'
+ ? new URLSearchParams(location.search).get('freevideo_lang') : null;
+const cn = languageOverride === 'zh' || (languageOverride !== 'en'
+ && String(navigator.language || '').toLowerCase().startsWith('zh'));
+const t = (en, zh) => cn ? zh : en;
+const preference = 'freevideo.view';
+let toolbar, open, selected, currentView = 'nodes';
+const style = document.createElement('link');
+style.rel = 'stylesheet'; style.href = new URL('./view_navigation.css', import.meta.url).href;
+document.head.append(style);
+
+export function preferredView() {
+ try { return localStorage.getItem(preference); } catch { return null; }
+}
+
+export function viewChanged(view, node) {
+ currentView = view;
+ if (node) selected = node;
+ try { localStorage.setItem(preference, view); } catch { /* Private browsing still works. */ }
+ refreshNavigation();
+}
+
+export function viewSwitch(view, onStudio, onNodes) {
+ const group = document.createElement('div'); group.className = 'fv-view-switch';
+ group.setAttribute('role', 'group'); group.setAttribute('aria-label', t('FreeVideo view', 'FreeVideo 视图'));
+ for (const [id, label, action] of [['studio', t('Create', '创作面板'), onStudio], ['nodes', t('Nodes', '节点视图'), onNodes]]) {
+ const button = document.createElement('button'); button.type = 'button';
+ button.textContent = label; button.dataset.view = id;
+ button.setAttribute('aria-pressed', String(view === id)); button.onclick = action;
+ group.append(button);
+ }
+ return group;
+}
+
+export function refreshNavigation() {
+ if (!toolbar) return;
+ const nodes = (app.graph?._nodes || []).filter(n => n.comfyClass === 'FreeVideoGenerate' || n.type === 'FreeVideoGenerate');
+ if (!nodes.includes(selected)) selected = nodes[0];
+ toolbar.hidden = !selected || currentView === 'studio';
+ for (const button of toolbar.querySelectorAll('[data-view]')) {
+ button.setAttribute('aria-pressed', String(button.dataset.view === currentView));
+ }
+}
+
+export function installNavigation(openStudio) {
+ open = openStudio;
+ if (toolbar) { refreshNavigation(); return; }
+ toolbar = document.createElement('aside'); toolbar.className = 'fv-view-navigation';
+ toolbar.setAttribute('aria-label', t('FreeVideo workspace', 'FreeVideo 工作区'));
+ const row = document.createElement('div'); row.className = 'fv-view-row';
+ const brand = wordmark();
+ const choose = () => {
+ const focused = Object.values(app.canvas?.selected_nodes || {}).find(n => n.comfyClass === 'FreeVideoGenerate' || n.type === 'FreeVideoGenerate');
+ refreshNavigation(); if (focused || selected) open(focused || selected);
+ };
+ const settings = document.createElement('button'); settings.type = 'button';
+ settings.textContent = t('Settings', '设置'); settings.className = 'fv-view-settings'; settings.onclick = openSetup;
+ row.append(brand, viewSwitch('nodes', choose, () => {}), settings); toolbar.append(row);
+ let shown = false;
+ try { shown = localStorage.getItem('freevideo.view-guide') === '1'; } catch { /* Show once this session. */ }
+ if (!shown) {
+ const guide = document.createElement('div'); guide.className = 'fv-view-guide';
+ const text = document.createElement('span');
+ text.textContent = t('Create here, connect tools in Nodes. Switching keeps your inputs and results.', '在创作面板生成,在节点视图连接工具。切换会保留输入和结果。');
+ const dismiss = document.createElement('button'); dismiss.type = 'button'; dismiss.textContent = t('Got it', '知道了');
+ dismiss.onclick = () => {
+ try { localStorage.setItem('freevideo.view-guide', '1'); } catch {}
+ // Fold the tip away rather than letting the toolbar jump.
+ const reduce = typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
+ if (reduce || typeof guide.animate !== 'function') { guide.remove(); return; }
+ // The tip set the toolbar's width; contract the bar in the same motion.
+ const before = toolbar.getBoundingClientRect().width;
+ guide.style.display = 'none';
+ const after = toolbar.getBoundingClientRect().width;
+ guide.style.display = '';
+ const height = guide.getBoundingClientRect().height;
+ guide.style.overflow = 'hidden'; guide.style.whiteSpace = 'nowrap';
+ const timing = {duration: 260, easing: 'cubic-bezier(.4,0,.2,1)'};
+ if (Math.abs(before - after) > 1) toolbar.animate([{width: `${before}px`}, {width: `${after}px`}], timing);
+ // The text is gone before the bar has narrowed enough to clip it.
+ guide.animate([{opacity: 1, height: `${height}px`, marginTop: '10px'}, {opacity: 0, offset: .4},
+ {opacity: 0, height: '0px', marginTop: '0px', paddingTop: '0px', paddingBottom: '0px'}],
+ {...timing, fill: 'forwards'}).finished.then(() => guide.remove(), () => guide.remove());
+ };
+ guide.append(text, dismiss); toolbar.append(guide);
+ }
+ document.body.append(toolbar); refreshNavigation();
+}