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.
Head groups share one LoRA down projection per layer call instead of repeating it for every group
Bowen Xue08c9257a834f
diff --git a/freevideo_engine/head_chunk.py b/freevideo_engine/head_chunk.pyindex 8cda14e..ccf61b8 100644--- a/freevideo_engine/head_chunk.py+++ b/freevideo_engine/head_chunk.py@@ -9,6 +9,7 @@ import gcimport typesimport torchimport torch.nn.functional as F+from .lora_online import shared_down# Sequence lengths whose first attention call has run. That call compiles, and@@ -255,11 +256,12 @@ def install_head_chunks(transformer, policy, chunk, cpu_outputs=False, projectiotensor.record_stream(stream)try:fresh = not streams and len(x) not in COLLECTED_LENGTHS- 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)- if fresh:- gc.collect()+ with shared_down() if not streams else nullcontext():+ 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)+ if fresh:+ gc.collect()COLLECTED_LENGTHS.add(len(x))finally:# Output projection, offload cleanup and the next block cannotdiff --git a/freevideo_engine/lora_kernels.py b/freevideo_engine/lora_kernels.pyindex d9f8337..b5f2e44 100644--- a/freevideo_engine/lora_kernels.py+++ b/freevideo_engine/lora_kernels.py@@ -4,7 +4,7 @@ import triton.language as tl@triton.jit-def _up_add(Z, B, Y, M: tl.constexpr, N: tl.constexpr, R: tl.constexpr,+def _up_add(Z, B, Y, M, N: tl.constexpr, R: tl.constexpr,SCALE: tl.constexpr, BM: tl.constexpr, BN: tl.constexpr, BR: tl.constexpr):m = tl.program_id(0) * BM + tl.arange(0, BM)n = tl.program_id(1) * BN + tl.arange(0, BN)diff --git a/freevideo_engine/lora_online.py b/freevideo_engine/lora_online.pyindex 6a74b16..1750072 100644--- a/freevideo_engine/lora_online.py+++ b/freevideo_engine/lora_online.py@@ -1,9 +1,16 @@"""Independent low-rank branches whose buffers follow the layer offloader."""+import contextlibimport typesimport torchimport torch.nn.functional as F+# Row tile of the down projection and the cap on the projections one layer call+# keeps (a rank-r projection of n rows is n * r values per branch).+TILE_ROWS = 512+SHARED_LIMIT_BYTES = 256 * 2**20+_shared = None+def attach_block(block, index, cache, manifest, load, device='cpu'):spec = manifest.get('online_lora', {}).get('blocks', {}).get(str(index))@@ -30,6 +37,46 @@ def attach_block(block, index, cache, manifest, load, device='cpu'):raise ValueError('Unbound LoRA tensors')+def shared_down():+ """Compute each branch's down projection of one input once for a layer call.++ The head groups of an attention block all project the same input rows, and+ each applied the branches to its own channel slice, repeating the+ rows-by-A product once per group. Inside this context the product is+ computed on its first use, in the same row tiles as before, and the other+ groups read it. Only for groups that run in order on one stream.+ """+ global _shared+ previous, _shared = _shared, {'bytes': 0}+ try:+ yield+ finally:+ _shared = previous+++def _down(branch, rows):+ """rows @ A in bounded row tiles, kept for the other head groups when it fits."""+ cache = _shared+ key = id(branch)+ if cache is not None:+ entry = cache.get(key)+ if entry is not None and entry[0] == rows.data_ptr() and entry[1] == rows.shape:+ return entry[2]+ z = None+ if cache is not None and len(rows) and rows.is_cuda and rows.dtype in (torch.bfloat16, torch.float16):+ size = len(rows) * branch.a.shape[0] * rows.element_size()+ if cache['bytes'] + size <= SHARED_LIMIT_BYTES:+ first = F.linear(rows[:TILE_ROWS], branch.a)+ z = first.new_empty((len(rows), first.shape[1]))+ z[:TILE_ROWS].copy_(first)+ for start in range(TILE_ROWS, len(rows), TILE_ROWS):+ z[start:start + TILE_ROWS].copy_(F.linear(rows[start:start + TILE_ROWS], branch.a))+ cache['bytes'] += size+ cache[key] = (rows.data_ptr(), rows.shape, z)+ return z++def apply(module, raw, output, channels=None):branches = getattr(module, '_freevideo_lora', None)if branches is None:@@ -40,10 +87,15 @@ def apply(module, raw, output, channels=None):raise ValueError('LoRA projection output must be contiguous')for branch in branches:b = branch.b if channels is None else branch.b[channels].contiguous()+ whole = _down(branch, rows) if channels is not None else None+ if whole is not None and whole.shape[1] <= 256:+ from .lora_kernels import up_add+ up_add(whole, b, target, branch.scale)+ continue# Bound workspace independently of the video/reference token count.- for start in range(0, len(rows), 512):- z = F.linear(rows[start:start + 512], branch.a)- result = target[start:start + 512]+ for start in range(0, len(rows), TILE_ROWS):+ z = whole[start:start + TILE_ROWS] if whole is not None else F.linear(rows[start:start + TILE_ROWS], branch.a)+ result = target[start:start + TILE_ROWS]if z.is_cuda and z.dtype in (torch.bfloat16, torch.float16) and z.shape[1] <= 256:from .lora_kernels import up_addup_add(z, b, result, branch.scale)