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.

Resume slow PyTorch downloads across mirrors (#12)

Bowen Xueaf76ca287cd5
diff --git a/freevideo_engine/bootstrap.py b/freevideo_engine/bootstrap.py
index 71b02e2..63b958a 100644
--- a/freevideo_engine/bootstrap.py
+++ b/freevideo_engine/bootstrap.py
@@ -958,7 +958,22 @@ class Installer:
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),
+ def torch_packages(label=name, argv=command, group=family, role=spec, executable=python):
+ if self.system == 'Windows':
+ from .torch_download import install
+ def check():
+ if self.cancel.is_set():
+ raise RuntimeError('Installation cancelled; partial download retained.')
+ if self.plan.get('disk_mode') == 'extreme':
+ from .install_disk import check_floor
+ check_floor((self.root,))
+ if install(uv, executable, role['torch'], role['cuda'], root=self.root,
+ networking=self.network, env=self.env, ui=self.ui,
+ run=lambda args, env: self.command(label + '-torch', args, env=env),
+ constraints=['-c', constraints_file(label, self.system)], check=check):
+ return
+ return self.packages(label + '-torch', argv, group)
+ task(name + '-torch', torch_packages,
(name + '-venv',), writer)
name = 'unified' if self.layout == 'unified' else 'engine'
python, encoder_python = self.pythons['engine'], self.pythons['encoder']
@@ -1014,7 +1029,10 @@ class Installer:
raise ValueError('The installation cache points outside this FreeVideo folder; '
'space-saving cleanup stopped without removing it.')
# Use uv's lock-aware cleanup; never unlink its internal files ourselves.
- return self.command('release-package-cache', [uv, 'cache', 'clean', '--cache-dir', cache])
+ result = self.command('release-package-cache', [uv, 'cache', 'clean', '--cache-dir', cache])
+ from .torch_download import release_cache
+ release_cache(self.root)
+ return result
def clone(self, name, url, commit, sparse=None):
return network.clone(self.root / 'vendor' / name, url, commit, sparse=sparse, run=self.command,
diff --git a/freevideo_engine/comfy_host.py b/freevideo_engine/comfy_host.py
index 62f3ce6..3de5632 100644
--- a/freevideo_engine/comfy_host.py
+++ b/freevideo_engine/comfy_host.py
@@ -160,9 +160,19 @@ def _prepare(root, comfy, machine, run, download):
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')
+ prepared = False
+ if os.name == 'nt':
+ from .torch_download import install
+ def check():
+ if machine.get('disk_mode') == 'extreme':
+ from .install_disk import check_floor
+ check_floor((root, comfy))
+ prepared = install(uv, python, rows, cuda, root=root, networking=plan, env=env, ui=ui,
+ run=lambda args, environment: execute(args, environment, 'Prepare ComfyUI GPU packages'), check=check)
+ if not prepared:
+ 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'),
diff --git a/freevideo_engine/launcher_copy.py b/freevideo_engine/launcher_copy.py
index 95add5a..9c58f6c 100644
--- a/freevideo_engine/launcher_copy.py
+++ b/freevideo_engine/launcher_copy.py
@@ -16,6 +16,8 @@ ZH = dict([
('GitHub mirror', 'GitHub 镜像'), ('attempt', '正在连接'),
('fallback', '正在切换下载源'), ('route-retry', '正在切换连接'),
('verifying', '正在校验'), ('no-progress', '下载无进展'),
+ ('slow-transfer', '持续低速'), ('resume-refused', '当前下载源不支持续传'),
+ ('Download PyTorch + CUDA', '下载 GPU 运行环境'),
('Use existing Python', '复用 Python'), ('Create Python environment', '创建运行环境'),
('Install PyTorch + CUDA', '安装 GPU 运行环境'),
('Install engine dependencies', '安装生成组件'), ('Install encoder support', '安装编码组件'),
diff --git a/freevideo_engine/network.py b/freevideo_engine/network.py
index 036bfbe..06ce5dc 100644
--- a/freevideo_engine/network.py
+++ b/freevideo_engine/network.py
@@ -656,7 +656,7 @@ class DownloadError(RuntimeError):
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',
- low_speed_limit=1024, max_seconds=None, cycles=2):
+ low_speed_limit=1024, max_seconds=None, cycles=2, slow_seconds=None, keep_partial=False):
"""Resume across ranked sources; hash before accepting, retain rejected bytes."""
network = network or {}
path = Path(path)
@@ -667,7 +667,9 @@ def download(candidates, path, expected, progress=None, *, network=None, env=Non
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
+ check = network.get('resource_check') if keep_partial else None
+ return hash_file(candidate, algorithm, git_blob, discard_cache=category in ('models', 'vdn-models'),
+ progress=(lambda done, total: check()) if check else None) == expected
if path.is_file():
if matches(path):
return
@@ -697,10 +699,22 @@ def download(candidates, path, expected, progress=None, *, network=None, env=Non
pass
# Try the next ranked candidate promptly. The final candidate gets bounded
# retries; every mirror must honor the retained prefix, with no silent reset.
+ measured_rates = {}
for cycle in range(cycles):
+ # Once every route has been tried, keep the fastest observed route even
+ # if the user's entire connection is slow. Do not oscillate forever.
+ if cycle and slow_seconds is not None:
+ candidates.sort(key=lambda row: -measured_rates.get((row[0], row[2]), 0))
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 keep_partial and size is not None and offset == size:
+ if matches(partial, name):
+ from .file_ops import publish
+ publish(partial, path)
+ return
+ retain_partial(partial, 'hash-rejected')
+ offset = 0
if size and offset > size:
retain_partial(partial, 'oversized')
offset = 0
@@ -716,7 +730,7 @@ def download(candidates, path, expected, progress=None, *, network=None, env=Non
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:
+ if not model and slow_seconds is None:
command += ['--speed-limit', str(low_speed_limit), '--speed-time', str(stall_seconds)]
if max_seconds is not None:
command += ['--max-time', str(max_seconds)]
@@ -737,6 +751,7 @@ def download(candidates, path, expected, progress=None, *, network=None, env=Non
started = time.monotonic()
oversized = None
stalled = False
+ slow = False
last_body, previous_done = started, offset
recent = deque([(started, offset)])
try:
@@ -759,11 +774,18 @@ def download(candidates, path, expected, progress=None, *, network=None, env=Non
# turn these excess bytes into progress or an ETA.
oversized = done
break
+ recent.append((now, done))
+ window = max(5, slow_seconds or 0)
+ while len(recent) > 2 and recent[1][0] < now - window:
+ recent.popleft()
+ if (slow_seconds is not None and cycle == 0 and len(candidates) > 1
+ and now - recent[0][0] >= slow_seconds
+ and (done - recent[0][1]) / (now - recent[0][0]) < low_speed_limit):
+ slow = True
+ break
if progress:
- recent.append((now, done))
- while len(recent) > 2 and recent[1][0] < now - 5:
- recent.popleft()
- progress(done, size or 0, max(0, done - recent[0][1]) / max(.001, now - recent[0][0]))
+ sample = next((row for row in recent if row[0] >= now - 5), recent[0])
+ progress(done, size or 0, max(0, done - sample[1]) / max(.001, now - sample[0]))
try:
process.wait(timeout=.25)
except subprocess.TimeoutExpired:
@@ -779,6 +801,9 @@ def download(candidates, path, expected, progress=None, *, network=None, env=Non
status = process.stdout.read(16).decode(errors='replace')
process.stdout.close()
current_size = partial.stat().st_size if partial.exists() else 0
+ if slow_seconds is not None:
+ measured_rates[name, route] = (max(0, current_size - offset) /
+ max(.001, time.monotonic() - started)) if slow else 0
if model and current_size > offset:
remember_source()
if oversized is not None:
@@ -801,7 +826,7 @@ def download(candidates, path, expected, progress=None, *, network=None, env=Non
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:
+ if model or keep_partial:
event(network, category=category, source=name, file=path.name,
action='resume-refused', resume_bytes=offset, reason='source-does-not-support-range')
break
@@ -813,13 +838,14 @@ def download(candidates, path, expected, progress=None, *, network=None, env=Non
retry_wait(network, restart, category=category, source=name, file=path.name,
resume_bytes=current_size, reason='no-progress' if stalled else 'connection-interrupted')
continue
+ reason = 'no-progress' if stalled else 'slow-transfer' if slow else 'curl-' + str(process.returncode)
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='no-progress' if stalled else 'curl-' + str(process.returncode), resume_bytes=current_size)
+ file=path.name, action='route-retry', reason=reason, 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='no-progress' if stalled else 'curl-' + str(process.returncode), http_status=status)
+ event(network, category=category, source=name, route=route, file=path.name, action='fallback', reason=reason, http_status=status, resume_bytes=current_size)
break
if model and partial.exists() and partial.stat().st_size:
if not network.get('allow_model_restart'):
diff --git a/freevideo_engine/torch_download.py b/freevideo_engine/torch_download.py
new file mode 100644
index 0000000..cf7d661
--- /dev/null
+++ b/freevideo_engine/torch_download.py
@@ -0,0 +1,102 @@
+"""Verified, resumable Windows GPU wheels before the package installation step."""
+import importlib.metadata
+import json
+from pathlib import Path
+
+from . import network
+
+
+def catalog():
+ return json.loads(Path(__file__).with_name('torch_downloads.json').read_text(encoding='utf-8'))['windows_cp312']
+
+
+def select(pins, cuda):
+ available = {row['package'] + '==' + row['version']: row for row in catalog()}
+ selected = []
+ for pin in pins:
+ exact = pin if '+' in pin else pin + '+' + cuda
+ if exact not in available:
+ return None
+ selected.append(available[exact])
+ return selected
+
+
+def installed_versions(python):
+ # Read metadata without importing torch or starting CUDA. Windows setup uses
+ # an isolated venv with the standard Lib/site-packages layout.
+ directory = Path(python).parent.parent / 'Lib' / 'site-packages'
+ return {dist.metadata['Name'].lower(): dist.version
+ for dist in importlib.metadata.distributions(path=[str(directory)])
+ if dist.metadata['Name']}
+
+
+def fetch(row, root, family, networking, env, ui, check):
+ path = Path(root) / 'downloads' / 'torch-wheels' / row['filename']
+ names = network.ordered(networking, family)
+ if not networking.get('sources', {}).get(family) and networking.get('mode') != 'official':
+ names = list(row['sources'])
+ candidates = [(name, row['sources'][name]) for name in names if name in row['sources']]
+ key = 'download-' + row['filename']
+ source = ''
+ ui.begin(key, 'Download PyTorch + CUDA', detail=row['filename'])
+ success = False
+
+ def feedback(event):
+ nonlocal source
+ source = {'official': 'PyTorch', 'nju': 'Nanjing University mirror'}.get(event.get('source'), '')
+ ui.update(key, detail=row['package'] + (' · Source: ' + source if source else '') +
+ ' · ' + event['action'] + (' · ' + event['reason'] if event.get('reason') else ''))
+
+ def progress(done, total, speed):
+ check()
+ unit, divisor = ('GiB', 2**30) if total >= 2**30 else ('MiB', 2**20)
+ rate = '%.1f MiB/s' % (speed / 2**20) if speed >= 2**20 else '%.1f KiB/s' % (speed / 1024)
+ ui.update(key, done=done, total=total, rate=speed, unit='bytes', scope=row['filename'],
+ detail='%s · %.2f / %.2f %s · %s%s' % (row['package'], done/divisor, total/divisor,
+ unit, rate, ' · Source: ' + source if source else ''))
+
+ try:
+ check()
+ network.download(candidates, path, row['sha256'], progress,
+ network=dict(networking, event_callback=feedback, resource_check=check), env=env,
+ size=row['bytes'], category=family, low_speed_limit=256 * 1024,
+ slow_seconds=30, stall_seconds=30, keep_partial=True)
+ success = True
+ finally:
+ ui.end(key, success=success, detail='Verified and ready' if success else None)
+ return path
+
+
+def install(uv, python, pins, cuda, *, root, networking, env, ui, run, constraints=(), check=None):
+ """Return False for a pin set outside the verified catalog; caller keeps its route."""
+ selected = select(pins, cuda)
+ if selected is None:
+ return False
+ check = check or (lambda: None)
+ check()
+ installed = installed_versions(python)
+ if all(installed.get(row['package']) == row['version'] for row in selected):
+ return True
+ family = 'torch-' + cuda + '-' + next(row['version'].split('+')[0] for row in selected if row['package'] == 'torch')
+ requirements = []
+ for row in selected:
+ if installed.get(row['package']) == row['version']:
+ requirements.append(row['package'] + '==' + row['version'])
+ else:
+ requirements.append(fetch(row, root, family, networking, env, ui, check))
+ command = [uv, 'pip', 'install', '--python', python, *requirements, *constraints]
+ # The large CUDA wheels are now local. Small ordinary dependencies use the
+ # measured PyPI mirrors, without accidentally selecting a different torch.
+ network.package_command(networking, 'pypi', lambda attempt_env, _: run(command, attempt_env), env)
+ return True
+
+
+def release_cache(root):
+ """Release completed wheel archives only after every environment has used them."""
+ directory = Path(root) / 'downloads' / 'torch-wheels'
+ if directory.is_symlink() or not directory.resolve().is_relative_to(Path(root).resolve()):
+ raise ValueError('The GPU package cache points outside this FreeVideo folder; cleanup stopped.')
+ for row in catalog():
+ path = directory / row['filename']
+ if path.is_file() and not path.is_symlink():
+ path.unlink()
diff --git a/freevideo_engine/torch_downloads.json b/freevideo_engine/torch_downloads.json
new file mode 100644
index 0000000..9d3f0e6
--- /dev/null
+++ b/freevideo_engine/torch_downloads.json
@@ -0,0 +1,41 @@
+{
+ "schema_version": 1,
+ "windows_cp312": [
+ {
+ "cuda": "cu130",
+ "package": "torch",
+ "version": "2.13.0+cu130",
+ "filename": "torch-2.13.0+cu130-cp312-cp312-win_amd64.whl",
+ "sha256": "2efab1e83604ca628c6d85b9e188c153690980498d1297081a9dad704919303c",
+ "bytes": 1915519202,
+ "sources": {
+ "official": "https://download.pytorch.org/whl/cu130/torch-2.13.0%2Bcu130-cp312-cp312-win_amd64.whl",
+ "nju": "https://mirrors.nju.edu.cn/pytorch/whl/cu130/torch-2.13.0%2Bcu130-cp312-cp312-win_amd64.whl"
+ }
+ },
+ {
+ "cuda": "cu130",
+ "package": "torchvision",
+ "version": "0.28.0+cu130",
+ "filename": "torchvision-0.28.0+cu130-cp312-cp312-win_amd64.whl",
+ "sha256": "5ae0ff50fcb43a189250a5fdb36a7dc780ea5caff469653cab1e3f32ed5d385c",
+ "bytes": 9091113,
+ "sources": {
+ "official": "https://download.pytorch.org/whl/cu130/torchvision-0.28.0%2Bcu130-cp312-cp312-win_amd64.whl",
+ "nju": "https://mirrors.nju.edu.cn/pytorch/whl/cu130/torchvision-0.28.0%2Bcu130-cp312-cp312-win_amd64.whl"
+ }
+ },
+ {
+ "cuda": "cu130",
+ "package": "torchaudio",
+ "version": "2.11.0+cu130",
+ "filename": "torchaudio-2.11.0+cu130-cp312-cp312-win_amd64.whl",
+ "sha256": "f74949f9ace1e4a6cf9468bdb3211b9cfa0af6ea348125471ac71c8621d6c77d",
+ "bytes": 1722734,
+ "sources": {
+ "official": "https://download.pytorch.org/whl/cu130/torchaudio-2.11.0%2Bcu130-cp312-cp312-win_amd64.whl",
+ "nju": "https://mirrors.nju.edu.cn/pytorch/whl/cu130/torchaudio-2.11.0%2Bcu130-cp312-cp312-win_amd64.whl"
+ }
+ }
+ ]
+}
diff --git a/pyproject.toml b/pyproject.toml
index 844c5f8..801729b 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -31,4 +31,4 @@ freevideo = "freevideo_engine.cli:main"
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", "uv_downloads.json", "test_prompts.json", "windows_process_host.ps1", "windows_bootstrap.ps1", "assets/*.png", "assets/*.ico", "launcher/*.qml", "launcher/licenses/*.txt", "licenses/*.txt"]
+freevideo_engine = ["build-version.txt", "dependencies.json", "model_files.json", "model_shares.json", "prepared_models.json", "bootstrap_versions.json", "uv_downloads.json", "torch_downloads.json", "test_prompts.json", "windows_process_host.ps1", "windows_bootstrap.ps1", "assets/*.png", "assets/*.ico", "launcher/*.qml", "launcher/licenses/*.txt", "licenses/*.txt"]