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.

Pin the native attention kernel order for the text refiner

Bowen Xue9a4728012273
diff --git a/freevideo_engine/processes.py b/freevideo_engine/processes.py
index 54c3dce..0fa00e5 100644
--- a/freevideo_engine/processes.py
+++ b/freevideo_engine/processes.py
@@ -243,6 +243,28 @@ def restore_handlers(previous):
signal.signal(name, handler)
+def pin_attention_priority():
+ """Fix the order in which native attention tries its kernels.
+
+ The text encoder's attention changes this process-wide order and never
+ restores it, so the text refiner picked flash attention after an encode in
+ the same process but cuDNN attention in a fresh one on Blackwell, with
+ different bf16 results for the same input. Call it once CUDA is
+ initialized, before native attention runs: flash, memory-efficient, math,
+ cuDNN. The first kernel selection on a device installs the default order
+ and overrides an earlier setting, so one selection is made first.
+ """
+ try:
+ import torch
+ if torch.cuda.is_available():
+ probe = torch.empty(1, 1, 16, 64, device='cuda', dtype=torch.bfloat16)
+ torch._fused_sdp_choice(probe, probe, probe)
+ torch._C._set_sdp_priority_order([2, 1, 0, 3, 4])
+ torch._C._set_sdp_priority_order([1, 2, 0, 3, 4])
+ except (ImportError, AttributeError, RuntimeError, TypeError):
+ pass
+
+
@contextmanager
def worker_signals():
"""Let Windows workers finalize stage reports before the Job timeout."""
diff --git a/freevideo_engine/runtime.py b/freevideo_engine/runtime.py
index 0939b54..44bb57a 100644
--- a/freevideo_engine/runtime.py
+++ b/freevideo_engine/runtime.py
@@ -15,6 +15,7 @@ from .attention import WindowAttention
from .backends import get_backend
from .weights import skeleton
from .paths import base_path, checkpoint_path
+from .processes import pin_attention_priority
from .torch_compat import empty_host_cache
@@ -1007,6 +1008,7 @@ class Engine:
self.refiner_released = False
# load_prompt returns [tokens, width]; generate_latents adds the batch
# dimension before the original transformer call.
+ pin_attention_priority()
prompt = prompt.unsqueeze(0) if prompt.ndim == 2 else prompt
text = self.transformer.context_embedder(prompt.to(input_dtype(self.transformer.context_embedder)))
with self.device_backend.make_offloader(refiner, prefetch=False,