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.

Progress keeps reporting during the upscale before the second pass

Bowen Xue5d68feb187da
diff --git a/freevideo_engine/comfy_bridge.py b/freevideo_engine/comfy_bridge.py
index fa843f4..dcf2fcf 100644
--- a/freevideo_engine/comfy_bridge.py
+++ b/freevideo_engine/comfy_bridge.py
@@ -980,8 +980,8 @@ def progress_message(event):
done, total = event.get('completed_steps', 8), event.get('total', 10)
return dict(label='Upscaling before the second pass', phase='sampling', stage='latent_upscale',
done=done, total=total, display_fraction=done / total, timing_phase='sampling',
- estimated_step_seconds=None, step_elapsed_seconds=0., remaining_seconds=None,
- uniform_remaining_steps=False)
+ estimated_step_seconds=None, step_elapsed_seconds=event.get('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.
diff --git a/freevideo_engine/macos_runtime.py b/freevideo_engine/macos_runtime.py
index 2c8eebe..bb9277c 100644
--- a/freevideo_engine/macos_runtime.py
+++ b/freevideo_engine/macos_runtime.py
@@ -53,11 +53,12 @@ def upscale_latents(video, sampling, base, backend, checkpoint=None):
import torch
from .latent_upscale import upscale
from .two_pass import UPSCALER, crop_latents
- print(json.dumps(dict(event='latent_upscale', completed_steps=sampling['base_steps'],
- total=sampling['total_steps'])), flush=True)
+ from .sampling_progress import elapsed_reports
+ event = dict(event='latent_upscale', completed_steps=sampling['base_steps'], total=sampling['total_steps'])
+ print(json.dumps(event), flush=True)
checkpoint = checkpoint or Path(base).parent / 'latent_upscaler' / Path(UPSCALER['file']).name
tick = time.monotonic()
- with torch.no_grad():
+ with torch.no_grad(), elapsed_reports(lambda report: print(json.dumps(report), flush=True), event):
backend.empty_cache()
lifted, stats = upscale(video.to('mps'), checkpoint,
sampling['upscale_target']['width'], sampling['upscale_target']['height'], memory_saving=True)
diff --git a/freevideo_engine/sampling_progress.py b/freevideo_engine/sampling_progress.py
index a2c85e1..63558cd 100644
--- a/freevideo_engine/sampling_progress.py
+++ b/freevideo_engine/sampling_progress.py
@@ -6,6 +6,7 @@ completion measurements.
"""
from contextlib import contextmanager
import json
+import threading
import time
@@ -13,6 +14,23 @@ def emit_event(event):
print(json.dumps(event), flush=True)
+@contextmanager
+def elapsed_reports(emit, fields, *, interval=3., clock=time.perf_counter):
+ """Report `step_elapsed_seconds` every `interval` seconds while a stage with
+ no internal counters (the latent upscale) runs in the calling thread."""
+ started, stopped = clock(), threading.Event()
+ def report():
+ while not stopped.wait(interval):
+ emit(dict(fields, step_elapsed_seconds=clock() - started))
+ thread = threading.Thread(target=report, name='stage-elapsed-reports', daemon=True)
+ thread.start()
+ try:
+ yield
+ finally:
+ stopped.set()
+ thread.join(timeout=1.)
+
+
def _bounded(value, low=0., high=1.):
"""Return a finite progress fraction without ever claiming completion."""
try:
@@ -35,6 +53,9 @@ class SamplingProgress:
self.step_started = self.started
self.completed = []
self.last_layer_event = float('-inf')
+ # When the current step entered its first layer. Input preparation and
+ # model reacquisition before it are not layer time.
+ self.first_layer_at = None
def _step_estimate(self):
# The first step includes input preparation and kernel compilation. A
@@ -73,14 +94,20 @@ class SamplingProgress:
if len(self.completed) >= self.steps:
return
now = self.clock()
+ if index == 1:
+ self.first_layer_at = now
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
+ # layer is entered, the time since the first layer was entered is
+ # the measured duration of the preceding layers; use that fraction
+ # as an early, continuously corrected ruler. Nothing is shown
+ # before the first layer has really run: time spent preparing the
+ # step's inputs would otherwise inflate the first estimate many
+ # times over.
+ if estimate is None and index > 1 and self.blocks > 0 and self.first_layer_at is not None:
+ elapsed = now - self.first_layer_at
fraction = (index - 1) / self.blocks
if elapsed > 0 and fraction > 0:
estimate = elapsed / fraction
@@ -104,6 +131,7 @@ class SamplingProgress:
# elapsed time into the next one.
next_step_elapsed_seconds=0.)
self.step_started = self.clock()
+ self.first_layer_at = None
@contextmanager
def layers(self, blocks):
diff --git a/freevideo_engine/worker.py b/freevideo_engine/worker.py
index 650ba9b..537c0c0 100644
--- a/freevideo_engine/worker.py
+++ b/freevideo_engine/worker.py
@@ -417,67 +417,71 @@ def generate(request, resident=None):
save(request['metrics'], metrics)
print(json.dumps(dict(event='latent_upscale', completed_steps=sampling_plan['base_steps'],
total=sampling_plan['total_steps'])), 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
- # Its weights go straight to the GPU. Charging 2 GiB of
- # host RAM evicted the second pass's own engine on
- # tight-RAM machines, which then reloaded every block.
- from .two_pass import UPSCALER_HOST_BYTES
- if need and resident is not None and not resident.ram_fits(UPSCALER_HOST_BYTES):
- resident.make_room('latent_upscaler', need, ram_need=UPSCALER_HOST_BYTES)
- 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 (released(engine) 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 released(engine):
- if resident is not None:
- resident.drop('engine', 'Latent upscale allocation needs additional workspace')
- else:
- engine.close()
- if resident is not None:
+ from .sampling_progress import elapsed_reports
+ with elapsed_reports(lambda event: print(json.dumps(event), flush=True),
+ dict(event='latent_upscale', completed_steps=sampling_plan['base_steps'],
+ total=sampling_plan['total_steps'])):
+ 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
+ # Its weights go straight to the GPU. Charging 2 GiB of
+ # host RAM evicted the second pass's own engine on
+ # tight-RAM machines, which then reloaded every block.
+ from .two_pass import UPSCALER_HOST_BYTES
+ if need and resident is not None and not resident.ram_fits(UPSCALER_HOST_BYTES):
resident.make_room('latent_upscaler', need, ram_need=UPSCALER_HOST_BYTES)
- import gc
- gc.collect()
torch.cuda.empty_cache()
- metrics['latent_upscale'].update(buffer_reuse=True, allocation_retries=1)
+ 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)
- 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)
+ 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 (released(engine) 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 released(engine):
+ 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=UPSCALER_HOST_BYTES)
+ 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,
diff --git a/web/generation_progress.js b/web/generation_progress.js
index 6c32e8a..9c3b5d9 100644
--- a/web/generation_progress.js
+++ b/web/generation_progress.js
@@ -802,9 +802,12 @@ export function createGenerationProgress(t, now = () => Date.now(), {compact = f
}
tick(); return;
}
- if (!heartbeat && (previousProgress === null || progressFields.some(key => message[key] !== previousProgress[key])))
+ // The upscale before the second pass has no counters; its seconds in
+ // progress are the engine's proof of life.
+ const counted = message.stage === 'latent_upscale' ? [...progressFields, 'step_elapsed_seconds'] : progressFields;
+ if (!heartbeat && (previousProgress === null || counted.some(key => message[key] !== previousProgress[key])))
lastProgressAt = previousProgress === null ? receivedAt : Math.max(lastProgressAt, receivedAt);
- if (!heartbeat) previousProgress = Object.fromEntries(progressFields.map(key => [key, message[key]]));
+ if (!heartbeat) previousProgress = Object.fromEntries(counted.map(key => [key, message[key]]));
if (message.stage === 'latent_upscale' || message.stage === 'first_pass_reused'
|| message.phase === 'refinement' || message.retry?.reuse_first_pass === true
|| (message.phase === 'sampling' && samplingPlan.enabled && !samplingPlan.preview