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.

Keep converted decoder layers across requests in the resident process

Bowen Xuec622ae1e247b
diff --git a/freevideo_engine/decode.py b/freevideo_engine/decode.py
index d3c9431..50832b0 100644
--- a/freevideo_engine/decode.py
+++ b/freevideo_engine/decode.py
@@ -84,7 +84,8 @@ def decode_to_file(latents, audio_latents, out_path, *, base, offload=False, pre
weight_source = SafetensorLayers((Path(base)/'vae').glob('*.safetensors'),
[f'decoder.transformer_blocks.{index}.' for index in range(count, layers)],
intermediate_dtype=torch.float32 if linear_compute_cache else None,
- keep_converted=True, converted_room=room)
+ keep_converted=True, converted_room=room,
+ converted_store=getattr(model_cache, 'converted_store', None))
vae, compute_cache_metrics = load_video_decoder(base, resident_blocks=count, weight_source=weight_source,
linear_fp16=linear_compute_cache)
vae.eval().requires_grad_(False)
diff --git a/freevideo_engine/resident_models.py b/freevideo_engine/resident_models.py
index ac67535..185f01c 100644
--- a/freevideo_engine/resident_models.py
+++ b/freevideo_engine/resident_models.py
@@ -152,6 +152,9 @@ class ModelBank:
self.gpu_reserve = 0
self.ram_budget = None
self.decisions = []
+ from .streamed_weights import ConvertedStore
+ # Converted decoder layers streamed from the checkpoint; rebuilt on demand.
+ self.converted_store = ConvertedStore()
# Set by a request that can read the whole device: bytes no process
# holds, and the growth reserve that request keeps out of them. The
# planning budget already excludes that reserve; the device reading
@@ -241,6 +244,7 @@ class ModelBank:
return True
def clear(self, reason='Request failed or session configuration changed'):
+ self.converted_store.clear()
for role in list(self.entries):
self.drop(role, reason)
gc.collect()
@@ -271,6 +275,8 @@ class ModelBank:
a retained copy left no room to pin and sampling took 95 s instead of
38 s. Otherwise the engine goes as it did with one worker per request.
"""
+ if not self.ram_fits(8*GiB + int(pinned)):
+ self.converted_store.clear()
for role in ('engine', 'encoder'):
if role in self.entries and not self.ram_fits(8*GiB + int(pinned)):
self.drop(role, 'Retained weights leave no host room for the next request')
@@ -331,6 +337,8 @@ class ModelBank:
room = min(room, max(0, ceiling - torch.cuda.memory_allocated() - reserve))
return room
before = available()
+ if not self.ram_fits(ram_need):
+ self.converted_store.clear()
# 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.
diff --git a/freevideo_engine/streamed_weights.py b/freevideo_engine/streamed_weights.py
index 2748e89..92437a7 100644
--- a/freevideo_engine/streamed_weights.py
+++ b/freevideo_engine/streamed_weights.py
@@ -420,10 +420,41 @@ def host_view_fits(size, reserve=HOST_VIEW_RESERVE):
return available - size >= reserve
+class ConvertedStore:
+ """Converted decoder layers that outlive one decode, for a resident process.
+
+ Entries belong to one checkpoint identity; a different one empties the store.
+ """
+
+ def __init__(self):
+ self.identity = None
+ self.layers = {}
+ self.lock = threading.Lock()
+
+ def bind(self, identity):
+ with self.lock:
+ if identity != self.identity:
+ self.layers = {}
+ self.identity = identity
+ return self.layers
+
+ def size(self):
+ with self.lock:
+ return sum(value.numel() * value.element_size()
+ for tensors in list(self.layers.values()) for value in tensors.values())
+
+ def clear(self):
+ with self.lock:
+ self.layers.clear()
+ self.identity = None
+
+
class SafetensorLayers:
def __init__(self, paths, prefixes, *, intermediate_dtype=None, direct_read=None, host_views=False,
- host_view_reserve=HOST_VIEW_RESERVE, keep_converted=False, converted_room=None):
+ host_view_reserve=HOST_VIEW_RESERVE, keep_converted=False, converted_room=None,
+ converted_store=None):
from .system import windows
+ self.converted_store = None
self.direct_read = windows() if direct_read is None else direct_read
self.prefixes = tuple(prefixes)
self.intermediate_dtype = intermediate_dtype
@@ -494,6 +525,14 @@ class SafetensorLayers:
self.byte_ranges[key] = (8 + length + start, end - start,
spec['dtype'], tuple(spec['shape']))
self.check(path)
+ if converted_store is not None and self.keep_converted:
+ # A resident process keeps the layers for its next request; they are
+ # served only while the checkpoint files, layers and dtype are unchanged.
+ self.converted = converted_store.bind(
+ (tuple(sorted((str(path), identity) for path, identity in self.files.items())),
+ self.prefixes, str(self.intermediate_dtype)))
+ self.converted_bytes = converted_store.size()
+ self.converted_store = converted_store
@staticmethod
def identity(path):
@@ -750,6 +789,8 @@ class SafetensorLayers:
self.converted_bytes += size
def release_converted(self):
+ if self.converted_store is not None:
+ return # owned by the resident process, which releases it under memory pressure
with self.cache_lock:
self.converted.clear()
self.converted_bytes = 0