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.

Large downloads compare a slow source with the others and move to the faster one

Bowen Xuef294115a8035
diff --git a/freevideo_engine/image_gen/image_pack.py b/freevideo_engine/image_gen/image_pack.py
index 52d421f..3f9a4bc 100644
--- a/freevideo_engine/image_gen/image_pack.py
+++ b/freevideo_engine/image_gen/image_pack.py
@@ -537,7 +537,8 @@ def _transfer(plan, row, path, progress, check, background=None, diagnostic=None
failures.clear()
network.download(network.model_urls(plan, row), path, row['sha256'], progress,
network=plan, size=row['bytes'], category=FAMILY,
- keep_partial=True, stall_seconds=30)
+ keep_partial=True, stall_seconds=30,
+ slow_seconds=15, low_speed_limit=64 * 1024)
return
except SwitchRequested:
if background is None:
diff --git a/freevideo_engine/model_transfer.py b/freevideo_engine/model_transfer.py
index 328e519..ab7b42e 100644
--- a/freevideo_engine/model_transfer.py
+++ b/freevideo_engine/model_transfer.py
@@ -22,6 +22,10 @@ GiB = 1 << 30
MiB = 1 << 20
MIN_SDK_BYTES = 64 * MiB
STALL_SECONDS = 240
+# Bytes stopped while downloading move to another source after this long; the
+# longer allowance stays for connecting, the xet start-up and verification.
+DOWNLOAD_STALL_SECONDS = 30
+CONNECT_STALL_SECONDS = 90
SAME_SOURCE_ATTEMPTS = 3
@@ -482,6 +486,14 @@ def run_sdk(source, row, path, plan, progress_callback, networking, transfer_com
prior_phase = None
prior_backend = None
prior_io = 0
+ compare = dict(thread=None, rates={}, checks=0, at=None, current=0.)
+ moving = None
+ alternatives = []
+ if (row['bytes'] >= 128 * MiB and not networking.get('manual_source_switch') and not request['require_xet']
+ and not plan.get('manual_ranges')):
+ family = network.model_family(row)
+ alternatives = [(name, url, network.route_order(networking, family, name)[0])
+ for name, url in network.model_urls(networking, row) if name != source]
while process.poll() is None:
if networking.get('resource_check'):
networking['resource_check']()
@@ -502,10 +514,29 @@ def run_sdk(source, row, path, plan, progress_callback, networking, transfer_com
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
+ timeout = (max(STALL_SECONDS, row['bytes'] / (8*MiB)) if current['phase'] in ('verify', 'reconstruct')
+ else DOWNLOAD_STALL_SECONDS if current['phase'] == 'download' and current.get('done_bytes')
+ else CONNECT_STALL_SECONDS)
if time.monotonic() - current['last_activity'] > timeout:
failure = 'stalled-transfer'
break
+ if alternatives and current['phase'] == 'download' and current.get('done_bytes'):
+ if moving is None:
+ moving = (time.monotonic(), current['done_bytes'])
+ compare['at'] = moving[0] + network.SWITCH_AFTER_SECONDS
+ from .provision import model_headers
+ better = network._faster_source(compare, time.monotonic(), moving[0], moving[1], current['done_bytes'],
+ row['bytes'], alternatives, 5, None, networking,
+ network.model_family(row), model_headers, full_restart=True)
+ if better is not None:
+ from .download_settings import current as settings, SwitchRequested
+ failure = 'faster-source'
+ network.event(networking, category=network.model_family(row), source=source,
+ file=path.name, action='fallback', reason='faster-source',
+ resume_bytes=current['done_bytes'])
+ network.source_health(networking, network.model_family(row), source, False)
+ network.source_health(networking, network.model_family(row), better[0], True)
+ raise SwitchRequested(dict(settings(networking), source=better[0], automatic=True))
size_error = stage_overflow(stage, row, selected['part_bytes'])
if size_error:
failure = 'unexpected-transfer-size'
@@ -683,7 +714,8 @@ def download(row, path, plan, progress_callback, headers_for, *, transfer_comple
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')
+ reason=('A faster source was found; stopped the slower transfer' if switch.value.get('automatic')
+ else 'User selected a new source; stopped the previous worker'))
if path.is_file() and retained['bytes']==row['bytes']:
break
finally:
diff --git a/freevideo_engine/network.py b/freevideo_engine/network.py
index 934101b..e69863a 100644
--- a/freevideo_engine/network.py
+++ b/freevideo_engine/network.py
@@ -15,6 +15,7 @@ from pathlib import Path
import re
import shutil
import subprocess
+from types import SimpleNamespace
from . import execution_paths
from . import subprocess_output
from .windows_ux import external_python, hidden_console
@@ -45,6 +46,15 @@ SAMPLE_BYTES = 256 * 1024
SPEED_SAMPLE_BYTES = 32 * 2**20
SPEED_SAMPLE_SECONDS = 3.
MIN_TRANSFER_LIMIT = 1024 * 1024
+# A transfer that is moving but slowly is compared with the untried sources: after
+# SWITCH_AFTER_SECONDS the others get one bounded speed sample and the transfer
+# moves (resuming the same partial) when that saves a real amount of time.
+SWITCH_AFTER_SECONDS = 15.
+SWITCH_RECHECK_SECONDS = 180.
+SWITCH_MAX_CHECKS = 3
+SWITCH_MIN_REMAINING_SECONDS = 90.
+SWITCH_MIN_SAVING_SECONDS = 60.
+SWITCH_MIN_GAIN = 2.
# One quick pass over sources/routes, followed by at most one patient pass.
# These are inactivity timeouts, not deadlines for an entire installation.
DOWNLOAD_RETRY_PROFILES = (('fast', '20', '1'), ('patient', '120', '5'))
@@ -771,6 +781,51 @@ class DownloadError(RuntimeError):
"""Every permitted source failed; an alternate artifact may be tried."""
+def _faster_source(compare, now, started, offset, done, size, alternatives, timeout, env, network, category, headers_for,
+ full_restart=False):
+ """Return the untried candidate worth moving to, or None. Never blocks the transfer loop."""
+ thread = compare['thread']
+ if thread is None:
+ if now < compare['at']:
+ return None
+ rate = (done - offset) / max(.001, now - started)
+ remaining = size - done
+ if rate <= 0 or remaining / rate < SWITCH_MIN_REMAINING_SECONDS:
+ compare['at'] = now + SWITCH_RECHECK_SECONDS
+ return None
+ compare['current'], compare['remaining'], compare['rates'] = rate, remaining, {}
+ compare['checks'] += 1
+ names = {}
+ for row in alternatives:
+ names.setdefault(row[0], row)
+
+ def measure(row):
+ name, url, route = row
+ try:
+ headers = headers_for(name) if headers_for else ()
+ _, result = speed_sample(url, timeout, env=route_environment(network, category, name, env, route),
+ headers=headers)
+ value = result.get('bytes_per_second') if result.get('speed_measured') else None
+ except (OSError, ValueError, RuntimeError, subprocess.SubprocessError):
+ value = None
+ if value:
+ compare['rates'][row[0], row[2]] = value
+ compare['thread'] = threading.Thread(target=lambda: [measure(r) for r in list(names.values())[:3]], daemon=True)
+ compare['thread'].start()
+ return None
+ if thread.is_alive():
+ return None
+ compare['thread'] = None
+ compare['at'] = now + SWITCH_RECHECK_SECONDS
+ best = max(compare['rates'].items(), key=lambda item: item[1], default=None)
+ if best is None:
+ return None
+ rate, remaining = compare['current'], compare['remaining']
+ if best[1] >= SWITCH_MIN_GAIN * rate and remaining / rate - (size if full_restart else remaining) / best[1] >= SWITCH_MIN_SAVING_SECONDS:
+ return next((row for row in alternatives if (row[0], row[2]) == best[0]), None)
+ return None
+
+
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, slow_seconds=None, keep_partial=False):
@@ -828,7 +883,7 @@ def download(candidates, path, expected, progress=None, *, network=None, env=Non
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:
+ if cycle and measured_rates:
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):
@@ -872,11 +927,14 @@ def download(candidates, path, expected, progress=None, *, network=None, env=Non
if offset:
command += ['--continue-at', str(offset)]
process = curl_process(command, env=attempt_env, stdin=subprocess.PIPE,
- stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE)
started = time.monotonic()
oversized = None
stalled = False
slow = False
+ faster = None
+ compare = dict(thread=None, rates={}, checks=0, at=started + SWITCH_AFTER_SECONDS, current=0.)
+ alternatives_ahead = [row for row in candidates[candidate_index+1:] if row[0] != name]
last_body, previous_done = started, offset
recent = deque([(started, offset)])
try:
@@ -908,6 +966,12 @@ def download(candidates, path, expected, progress=None, *, network=None, env=Non
and (done - recent[0][1]) / (now - recent[0][0]) < low_speed_limit):
slow = True
break
+ if (cycle == 0 and size and alternatives_ahead and compare['checks'] < SWITCH_MAX_CHECKS
+ and not network.get('manual_source_switch')):
+ faster = _faster_source(compare, now, started, offset, done, size, alternatives_ahead,
+ timeout, env, network, category, headers_for)
+ if faster is not None:
+ break
if progress:
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]))
@@ -916,6 +980,9 @@ def download(candidates, path, expected, progress=None, *, network=None, env=Non
except subprocess.TimeoutExpired:
pass
finally:
+ thread = compare['thread']
+ if thread is not None and thread.is_alive():
+ thread.join(timeout=timeout + SPEED_SAMPLE_SECONDS + 3)
if process.poll() is None:
process.terminate()
try:
@@ -925,10 +992,19 @@ def download(candidates, path, expected, progress=None, *, network=None, env=Non
process.wait()
status = process.stdout.read(16).decode(errors='replace')
process.stdout.close()
+ # --silent --show-error keeps this to a line or two.
+ curl_message = _package_failure(SimpleNamespace(stderr=process.stderr.read(2048)))[1] if process.returncode else ''
+ process.stderr.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 faster is not None:
+ measured_rates[name, route] = compare['current']
+ measured_rates.update(compare['rates'])
+ # The faster source is tried next, with the retained prefix.
+ position = candidates.index(faster)
+ candidates.insert(candidate_index + 1, candidates.pop(position))
if model and current_size > offset:
remember_source()
if oversized is not None:
@@ -958,19 +1034,24 @@ def download(candidates, path, expected, progress=None, *, network=None, env=Non
# 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')
+ # A connection that was moving bytes and then ended with an
+ # unclassified exit is an interruption, not a refusal.
+ interrupted = process.returncode not in (0, 22, 33, 63) and current_size > offset
+ transient = (stalled or interrupted 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
- reason = 'no-progress' if stalled else 'slow-transfer' if slow else 'curl-' + str(process.returncode)
+ reason = 'no-progress' if stalled else 'slow-transfer' if slow else 'faster-source' if faster 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=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=reason, http_status=status, resume_bytes=current_size)
+ event(network, category=category, source=name, route=route, file=path.name, action='fallback', reason=reason, http_status=status, resume_bytes=current_size,
+ **(dict(detail=curl_message) if curl_message else {}))
break
if model and partial.exists() and partial.stat().st_size:
if not network.get('allow_model_restart'):
diff --git a/freevideo_engine/prompt_vlm/assets.py b/freevideo_engine/prompt_vlm/assets.py
index 0c7c60e..1112fdd 100644
--- a/freevideo_engine/prompt_vlm/assets.py
+++ b/freevideo_engine/prompt_vlm/assets.py
@@ -67,7 +67,8 @@ def install(root, progress, check):
def moved(current, size, speed, **kwargs):
progress(dict(phase='download', done=done + current, total=total))
network.download(network.model_urls(plan, remote), path, row['sha256'], moved,
- network=plan, size=row['bytes'], keep_partial=True, stall_seconds=30)
+ network=plan, size=row['bytes'], keep_partial=True, stall_seconds=30,
+ slow_seconds=15, low_speed_limit=64 * 1024)
ledger[str(path)] = provision.file_identity(path, row)
save(ledger_path, ledger)
done += row['bytes']
diff --git a/freevideo_engine/subtitle_pack.py b/freevideo_engine/subtitle_pack.py
index 310896a..ff97d27 100644
--- a/freevideo_engine/subtitle_pack.py
+++ b/freevideo_engine/subtitle_pack.py
@@ -248,7 +248,8 @@ def install(groups=GROUPS, root=None, progress=None, check=None):
def moved(current, size, speed, **kwargs):
progress(dict(phase='download', done=done + current, total=total))
network.download(network.model_urls(plan, remote), path, row['sha256'], moved,
- network=plan, size=row['bytes'], keep_partial=True, stall_seconds=30)
+ network=plan, size=row['bytes'], keep_partial=True, stall_seconds=30,
+ slow_seconds=15, low_speed_limit=64 * 1024)
ledger[str(path)] = provision.file_identity(path, row)
save(folder / LEDGER, ledger)
done += row['bytes']