Text-to-Video
Diffusers
Safetensors
English
MotifVideoPipeline
image-to-video
video-generation
diffusion-transformer
Instructions to use Motif-Technologies/Motif-Video-2B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use Motif-Technologies/Motif-Video-2B with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("Motif-Technologies/Motif-Video-2B", dtype=torch.bfloat16, device_map="cuda") prompt = "A vibrant blue jay perches gracefully on a slender branch, its feathers shimmering in the soft morning light. The bird's keen eyes scan the surroundings, capturing the essence of the tranquil forest. It flutters its wings briefly, showcasing the intricate patterns of blue, white, and black on its plumage. The background reveals a lush canopy of green leaves, with rays of sunlight filtering through, creating a dappled effect on the forest floor. The blue jay then tilts its head, emitting a melodious call that echoes through the serene woodland, adding a touch of magic to the peaceful scene." image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
docs: split Memory-efficient Inference and GGUF+SageAttention into sub READMEs
#20
by gkalstn0 - opened
- README.md +17 -210
- docs/gguf-sageattention.md +132 -0
- docs/memory-efficient-inference.md +88 -0
README.md
CHANGED
|
@@ -299,90 +299,31 @@ See `inference.py --help` for all available options.
|
|
| 299 |
|
| 300 |
### 🔋 Memory-efficient Inference
|
| 301 |
|
| 302 |
-
|
| 303 |
|
| 304 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
|
| 306 |
-
|
| 307 |
-
export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
|
| 308 |
-
```
|
| 309 |
|
| 310 |
-
|
| 311 |
-
pipe = MotifVideoPipeline.from_pretrained(
|
| 312 |
-
"Motif-Technologies/Motif-Video-2B",
|
| 313 |
-
revision="diffusers-integration",
|
| 314 |
-
torch_dtype=torch.bfloat16,
|
| 315 |
-
guider=guider, # see T2V example above
|
| 316 |
-
)
|
| 317 |
-
pipe.scheduler = FlowDPMSolver(
|
| 318 |
-
num_train_timesteps=pipe.scheduler.config.get("num_train_timesteps", 1000),
|
| 319 |
-
algorithm_type="dpmsolver++",
|
| 320 |
-
solver_order=2,
|
| 321 |
-
prediction_type="flow_prediction",
|
| 322 |
-
use_flow_sigmas=True,
|
| 323 |
-
flow_shift=15.0,
|
| 324 |
-
)
|
| 325 |
-
pipe.enable_model_cpu_offload() # replaces pipe.to("cuda")
|
| 326 |
-
|
| 327 |
-
output = pipe(
|
| 328 |
-
prompt="...",
|
| 329 |
-
negative_prompt="...",
|
| 330 |
-
height=736, width=1280, num_frames=121, num_inference_steps=50,
|
| 331 |
-
frame_rate=24, use_linear_quadratic_schedule=False,
|
| 332 |
-
)
|
| 333 |
-
export_to_video(output.frames[0], "output.mp4", fps=24)
|
| 334 |
-
```
|
| 335 |
-
|
| 336 |
-
This moves each component (text encoder → transformer → VAE) to GPU only when needed. The `expandable_segments` setting allows the CUDA memory allocator to efficiently reuse memory released by earlier components, avoiding fragmentation-related OOM errors.
|
| 337 |
-
|
| 338 |
-
| Mode | Peak VRAM | Speed | Recommended GPU |
|
| 339 |
-
|------|-----------|-------|-----------------|
|
| 340 |
-
| `pipe.to("cuda")` | ~30 GB | Fastest | A100, H100, H200 |
|
| 341 |
-
| `enable_model_cpu_offload()` | ~19 GB | Similar | RTX 4090, RTX 3090 |
|
| 342 |
-
|
| 343 |
-
#### FP8 Weight Quantization (Optional)
|
| 344 |
-
|
| 345 |
-
For further VRAM reduction, you can quantize the transformer weights to FP8 using [torchao](https://github.com/pytorch/ao):
|
| 346 |
-
|
| 347 |
-
```bash
|
| 348 |
-
pip install torchao
|
| 349 |
-
```
|
| 350 |
|
| 351 |
-
|
| 352 |
-
from torchao.quantization import quantize_, Float8WeightOnlyConfig
|
| 353 |
|
| 354 |
-
|
| 355 |
-
"Motif-Technologies/Motif-Video-2B",
|
| 356 |
-
revision="diffusers-integration",
|
| 357 |
-
torch_dtype=torch.bfloat16,
|
| 358 |
-
guider=guider, # see T2V example above
|
| 359 |
-
)
|
| 360 |
-
pipe.scheduler = FlowDPMSolver(
|
| 361 |
-
num_train_timesteps=pipe.scheduler.config.get("num_train_timesteps", 1000),
|
| 362 |
-
algorithm_type="dpmsolver++",
|
| 363 |
-
solver_order=2,
|
| 364 |
-
prediction_type="flow_prediction",
|
| 365 |
-
use_flow_sigmas=True,
|
| 366 |
-
flow_shift=15.0,
|
| 367 |
-
)
|
| 368 |
-
quantize_(pipe.transformer, Float8WeightOnlyConfig())
|
| 369 |
-
pipe.enable_model_cpu_offload()
|
| 370 |
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
)
|
| 377 |
-
export_to_video(output.frames[0], "output.mp4", fps=24)
|
| 378 |
-
```
|
| 379 |
|
| 380 |
-
|
| 381 |
|
| 382 |
-
|
| 383 |
-
|------|-----------|-------|
|
| 384 |
-
| `enable_model_cpu_offload()` | ~19 GB | BF16 baseline |
|
| 385 |
-
| `+ Float8WeightOnlyConfig` | ~15 GB | FP8 weights, BF16 compute |
|
| 386 |
|
| 387 |
### 🖥️ ComfyUI
|
| 388 |
|
|
@@ -390,140 +331,6 @@ Official ComfyUI custom nodes: [ComfyUI-MotifVideo2B](https://github.com/MotifTe
|
|
| 390 |
|
| 391 |
> **Note:** Currently requires **High VRAM** mode. GGUF quantized model loading in ComfyUI is in progress.
|
| 392 |
|
| 393 |
-
---
|
| 394 |
-
|
| 395 |
-
### 🧊 GGUF + SageAttention
|
| 396 |
-
|
| 397 |
-
GGUF quantized transformer weights are available at [Motif-Video-2B-GGUF](https://huggingface.co/Motif-Technologies/Motif-Video-2B-GGUF), reducing VRAM with minimal quality loss. Combined with [SageAttention](https://github.com/thu-ml/SageAttention) for ~2× faster attention computation.
|
| 398 |
-
|
| 399 |
-
#### GGUF Inference
|
| 400 |
-
|
| 401 |
-
```bash
|
| 402 |
-
pip install gguf
|
| 403 |
-
```
|
| 404 |
-
|
| 405 |
-
```python
|
| 406 |
-
import torch
|
| 407 |
-
from diffusers import (
|
| 408 |
-
AdaptiveProjectedGuidance,
|
| 409 |
-
DPMSolverMultistepScheduler,
|
| 410 |
-
GGUFQuantizationConfig,
|
| 411 |
-
MotifVideoPipeline,
|
| 412 |
-
MotifVideoTransformer3DModel,
|
| 413 |
-
)
|
| 414 |
-
from diffusers.utils import export_to_video
|
| 415 |
-
from huggingface_hub import hf_hub_download
|
| 416 |
-
|
| 417 |
-
guider = AdaptiveProjectedGuidance(
|
| 418 |
-
guidance_scale=8.0,
|
| 419 |
-
adaptive_projected_guidance_rescale=12.0,
|
| 420 |
-
adaptive_projected_guidance_momentum=0.1,
|
| 421 |
-
use_original_formulation=True,
|
| 422 |
-
normalization_dims="spatial",
|
| 423 |
-
)
|
| 424 |
-
|
| 425 |
-
variant = "Q4_K_M" # Options: Q4_0, Q4_1, Q4_K_M, Q5_0, Q5_1, Q5_K_M, Q6_K, Q8_0, BF16
|
| 426 |
-
ckpt_path = hf_hub_download(
|
| 427 |
-
"Motif-Technologies/Motif-Video-2B-GGUF",
|
| 428 |
-
filename=f"motifv-2b-dev-{variant}.gguf",
|
| 429 |
-
)
|
| 430 |
-
|
| 431 |
-
transformer = MotifVideoTransformer3DModel.from_single_file(
|
| 432 |
-
ckpt_path,
|
| 433 |
-
quantization_config=GGUFQuantizationConfig(compute_dtype=torch.bfloat16),
|
| 434 |
-
config="Motif-Technologies/Motif-Video-2B",
|
| 435 |
-
revision="diffusers-integration",
|
| 436 |
-
subfolder="transformer",
|
| 437 |
-
torch_dtype=torch.bfloat16,
|
| 438 |
-
)
|
| 439 |
-
|
| 440 |
-
pipe = MotifVideoPipeline.from_pretrained(
|
| 441 |
-
"Motif-Technologies/Motif-Video-2B",
|
| 442 |
-
revision="diffusers-integration",
|
| 443 |
-
torch_dtype=torch.bfloat16,
|
| 444 |
-
guider=guider,
|
| 445 |
-
transformer=transformer,
|
| 446 |
-
)
|
| 447 |
-
|
| 448 |
-
pipe.scheduler = FlowDPMSolver(
|
| 449 |
-
num_train_timesteps=pipe.scheduler.config.get("num_train_timesteps", 1000),
|
| 450 |
-
algorithm_type="dpmsolver++",
|
| 451 |
-
solver_order=2,
|
| 452 |
-
prediction_type="flow_prediction",
|
| 453 |
-
use_flow_sigmas=True,
|
| 454 |
-
flow_shift=15.0,
|
| 455 |
-
)
|
| 456 |
-
pipe.enable_model_cpu_offload()
|
| 457 |
-
|
| 458 |
-
output = pipe(
|
| 459 |
-
prompt="A woman standing in a sunlit field as flower petals swirl around her in slow motion. Each petal floats gently through the golden light, casting tiny shadows. Her hair moves like water, and time seems to stand still.",
|
| 460 |
-
negative_prompt="text overlay, graphic overlay, watermark, logo, subtitles, timestamp, broadcast graphics, UI elements, random letters, frozen pose, rigid, static expression, jerky motion, mechanical motion, discontinuous motion, flat framing, depthless, dull lighting, monotone, crushed shadows, blown-out highlights, shifting background, fading background, poor continuity, identity drift, deformation, flickering, ghosting, smearing, duplication, mutated proportions, inconsistent clothing, flat colors, desaturated, tonally compressed, poor background separation, exposure shift, uneven brightness, color balance shift",
|
| 461 |
-
height=736,
|
| 462 |
-
width=1280,
|
| 463 |
-
num_frames=121,
|
| 464 |
-
num_inference_steps=50,
|
| 465 |
-
frame_rate=24,
|
| 466 |
-
use_linear_quadratic_schedule=False,
|
| 467 |
-
)
|
| 468 |
-
export_to_video(output.frames[0], "output.mp4", fps=24)
|
| 469 |
-
```
|
| 470 |
-
|
| 471 |
-
#### SageAttention (Optional, ~1.6× faster)
|
| 472 |
-
|
| 473 |
-
Same prompt and seed, 1280x736, 121 frames, 50 steps. Left = SDPA, Right = SageAttention.
|
| 474 |
-
|
| 475 |
-

|
| 476 |
-

|
| 477 |
-

|
| 478 |
-

|
| 479 |
-
|
| 480 |
-
[SageAttention](https://github.com/thu-ml/SageAttention) accelerates attention by quantizing Q/K to INT8 and V to FP8, reducing memory bandwidth. Works with all GGUF variants.
|
| 481 |
-
|
| 482 |
-
**Install** (build from source — PyPI only has 1.x, need 2.x):
|
| 483 |
-
|
| 484 |
-
```bash
|
| 485 |
-
# Set TORCH_CUDA_ARCH_LIST to match your GPU: "8.0" for A100, "9.0" for H100/H200
|
| 486 |
-
TORCH_CUDA_ARCH_LIST="9.0" pip install git+https://github.com/thu-ml/SageAttention.git --no-build-isolation
|
| 487 |
-
```
|
| 488 |
-
|
| 489 |
-
**Usage with `inference.py`:**
|
| 490 |
-
|
| 491 |
-
```bash
|
| 492 |
-
python inference.py --use-sage-attention --prompt "..."
|
| 493 |
-
```
|
| 494 |
-
|
| 495 |
-
**Notes:**
|
| 496 |
-
- Requires NVIDIA GPU with SM70+
|
| 497 |
-
- SM90+ (H100, H200) — FP8 kernels for maximum speedup
|
| 498 |
-
- SM80-SM89 (A100, RTX 3090, RTX 4090) — FP16 kernels (still faster than SDPA)
|
| 499 |
-
- SM70-SM75 (V100, RTX 2080 Ti) — FP16 kernels
|
| 500 |
-
- Set `TORCH_CUDA_ARCH_LIST` to match your GPU when building (e.g., `"8.6"` for RTX 3090, `"8.9"` for RTX 4090)
|
| 501 |
-
- No quality degradation observed across all GGUF variants
|
| 502 |
-
|
| 503 |
-
#### Benchmark
|
| 504 |
-
|
| 505 |
-
Measured on NVIDIA H200, 1280x736, 121 frames, 50 steps, DPMSolver++ (order=2, flow_shift=15.0):
|
| 506 |
-
|
| 507 |
-
| Variant | SDPA (s/it) | Sage (s/it) | Speedup | Peak alloc (GB) | Peak rsv (GB) | Total SDPA (s) | Total Sage (s) |
|
| 508 |
-
|---------|------------|------------|---------|-----------------|----------------|----------------|----------------|
|
| 509 |
-
| BF16 | 23.36 | 14.75 | 1.58x | 14.78 / 15.12 | 24.93 / 24.90 | 1184 | 754 |
|
| 510 |
-
| Q8_0 | 23.16 | 14.49 | 1.60x | 13.10 / 13.44 | 23.14 / 23.11 | 1178 | 744 |
|
| 511 |
-
| Q6_K | 23.21 | 14.55 | 1.60x | 12.62 / 12.95 | 22.72 / 22.69 | 1178 | 747 |
|
| 512 |
-
| Q5_K_M | 23.33 | 14.69 | 1.59x | 12.39 / 12.72 | 22.45 / 22.42 | 1184 | 754 |
|
| 513 |
-
| Q5_1 | 23.54 | 14.96 | 1.57x | 12.47 / 12.81 | 22.66 / 22.62 | 1193 | 764 |
|
| 514 |
-
| Q5_0 | 23.26 | 14.67 | 1.59x | 12.37 / 12.71 | 22.55 / 22.52 | 1179 | 750 |
|
| 515 |
-
| Q4_K_M | 23.25 | 14.59 | 1.60x | 12.19 / 12.53 | 22.22 / 22.18 | 1178 | 747 |
|
| 516 |
-
| Q4_1 | 23.31 | 14.68 | 1.59x | 12.26 / 12.60 | 22.26 / 22.22 | 1181 | 750 |
|
| 517 |
-
| Q4_0 | 23.33 | 14.75 | 1.58x | 12.14 / 12.47 | 22.18 / 22.14 | 1188 | 760 |
|
| 518 |
-
|
| 519 |
-
Peak alloc/rsv columns show SDPA / Sage values. Sage adds ~0.3 GB alloc overhead (INT8/FP8 quantization buffers) with no change in reserved memory.
|
| 520 |
-
|
| 521 |
-
**Key findings:**
|
| 522 |
-
- **~1.59x faster with SageAttention** — consistent across all quantization levels
|
| 523 |
-
- **VRAM unchanged** — sage overhead is negligible (~0.3 GB alloc)
|
| 524 |
-
- **GGUF + Sage stacks** — Q4_K_M + Sage achieves 14.59 s/it at 12.53 GB alloc (vs BF16 SDPA: 23.36 s/it at 14.78 GB)
|
| 525 |
-
|
| 526 |
-
|
| 527 |
---
|
| 528 |
|
| 529 |
## 📊 Performance
|
|
|
|
| 299 |
|
| 300 |
### 🔋 Memory-efficient Inference
|
| 301 |
|
| 302 |
+
For GPUs with 24 GB or less (e.g. RTX 4090, RTX 3090), CPU offloading and FP8 quantization can reduce peak VRAM from ~30 GB to ~15 GB with minimal speed impact.
|
| 303 |
|
| 304 |
+
| Mode | Peak VRAM | Recommended GPU |
|
| 305 |
+
|------|-----------|-----------------|
|
| 306 |
+
| `pipe.to("cuda")` | ~30 GB | A100, H100, H200 |
|
| 307 |
+
| `enable_model_cpu_offload()` | ~19 GB | RTX 4090, RTX 3090 |
|
| 308 |
+
| `+ FP8 quantization` | ~15 GB | RTX 4090, RTX 3090 |
|
| 309 |
|
| 310 |
+
> **Full guide** → [docs/memory-efficient-inference.md](docs/memory-efficient-inference.md)
|
|
|
|
|
|
|
| 311 |
|
| 312 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
|
| 314 |
+
### 🧊 GGUF + SageAttention
|
|
|
|
| 315 |
|
| 316 |
+
GGUF quantized weights at [Motif-Video-2B-GGUF](https://huggingface.co/Motif-Technologies/Motif-Video-2B-GGUF) — up to 2.7 GB VRAM savings with no speed penalty. Combined with [SageAttention](https://github.com/thu-ml/SageAttention) for ~1.6× faster inference.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
|
| 318 |
+
| Variant | Sage (s/it) | Speedup | Peak alloc (GB) |
|
| 319 |
+
|---------|------------|---------|-----------------|
|
| 320 |
+
| BF16 | 14.75 | 1.58x | 15.12 |
|
| 321 |
+
| Q8_0 | 14.49 | 1.60x | 13.44 |
|
| 322 |
+
| Q4_K_M | 14.59 | 1.60x | 12.53 |
|
|
|
|
|
|
|
|
|
|
| 323 |
|
| 324 |
+
> **Full guide** → [docs/gguf-sageattention.md](docs/gguf-sageattention.md)
|
| 325 |
|
| 326 |
+
---
|
|
|
|
|
|
|
|
|
|
| 327 |
|
| 328 |
### 🖥️ ComfyUI
|
| 329 |
|
|
|
|
| 331 |
|
| 332 |
> **Note:** Currently requires **High VRAM** mode. GGUF quantized model loading in ComfyUI is in progress.
|
| 333 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
---
|
| 335 |
|
| 336 |
## 📊 Performance
|
docs/gguf-sageattention.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🧊 GGUF + SageAttention
|
| 2 |
+
|
| 3 |
+
> See the main [README](../README.md) for `FlowDPMSolver` and pipeline setup.
|
| 4 |
+
|
| 5 |
+
GGUF quantized transformer weights are available at [Motif-Video-2B-GGUF](https://huggingface.co/Motif-Technologies/Motif-Video-2B-GGUF), reducing VRAM with minimal quality loss. Combined with [SageAttention](https://github.com/thu-ml/SageAttention) for ~2× faster attention computation.
|
| 6 |
+
|
| 7 |
+
## GGUF Inference
|
| 8 |
+
|
| 9 |
+
```bash
|
| 10 |
+
pip install gguf
|
| 11 |
+
```
|
| 12 |
+
|
| 13 |
+
```python
|
| 14 |
+
import torch
|
| 15 |
+
from diffusers import (
|
| 16 |
+
AdaptiveProjectedGuidance,
|
| 17 |
+
DPMSolverMultistepScheduler,
|
| 18 |
+
GGUFQuantizationConfig,
|
| 19 |
+
MotifVideoPipeline,
|
| 20 |
+
MotifVideoTransformer3DModel,
|
| 21 |
+
)
|
| 22 |
+
from diffusers.utils import export_to_video
|
| 23 |
+
from huggingface_hub import hf_hub_download
|
| 24 |
+
|
| 25 |
+
guider = AdaptiveProjectedGuidance(
|
| 26 |
+
guidance_scale=8.0,
|
| 27 |
+
adaptive_projected_guidance_rescale=12.0,
|
| 28 |
+
adaptive_projected_guidance_momentum=0.1,
|
| 29 |
+
use_original_formulation=True,
|
| 30 |
+
normalization_dims="spatial",
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
variant = "Q4_K_M" # Options: Q4_0, Q4_1, Q4_K_M, Q5_0, Q5_1, Q5_K_M, Q6_K, Q8_0, BF16
|
| 34 |
+
ckpt_path = hf_hub_download(
|
| 35 |
+
"Motif-Technologies/Motif-Video-2B-GGUF",
|
| 36 |
+
filename=f"motifv-2b-dev-{variant}.gguf",
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
transformer = MotifVideoTransformer3DModel.from_single_file(
|
| 40 |
+
ckpt_path,
|
| 41 |
+
quantization_config=GGUFQuantizationConfig(compute_dtype=torch.bfloat16),
|
| 42 |
+
config="Motif-Technologies/Motif-Video-2B",
|
| 43 |
+
revision="diffusers-integration",
|
| 44 |
+
subfolder="transformer",
|
| 45 |
+
torch_dtype=torch.bfloat16,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
pipe = MotifVideoPipeline.from_pretrained(
|
| 49 |
+
"Motif-Technologies/Motif-Video-2B",
|
| 50 |
+
revision="diffusers-integration",
|
| 51 |
+
torch_dtype=torch.bfloat16,
|
| 52 |
+
guider=guider,
|
| 53 |
+
transformer=transformer,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
pipe.scheduler = FlowDPMSolver(
|
| 57 |
+
num_train_timesteps=pipe.scheduler.config.get("num_train_timesteps", 1000),
|
| 58 |
+
algorithm_type="dpmsolver++",
|
| 59 |
+
solver_order=2,
|
| 60 |
+
prediction_type="flow_prediction",
|
| 61 |
+
use_flow_sigmas=True,
|
| 62 |
+
flow_shift=15.0,
|
| 63 |
+
)
|
| 64 |
+
pipe.enable_model_cpu_offload()
|
| 65 |
+
|
| 66 |
+
output = pipe(
|
| 67 |
+
prompt="A woman standing in a sunlit field as flower petals swirl around her in slow motion. Each petal floats gently through the golden light, casting tiny shadows. Her hair moves like water, and time seems to stand still.",
|
| 68 |
+
negative_prompt="text overlay, graphic overlay, watermark, logo, subtitles, timestamp, broadcast graphics, UI elements, random letters, frozen pose, rigid, static expression, jerky motion, mechanical motion, discontinuous motion, flat framing, depthless, dull lighting, monotone, crushed shadows, blown-out highlights, shifting background, fading background, poor continuity, identity drift, deformation, flickering, ghosting, smearing, duplication, mutated proportions, inconsistent clothing, flat colors, desaturated, tonally compressed, poor background separation, exposure shift, uneven brightness, color balance shift",
|
| 69 |
+
height=736,
|
| 70 |
+
width=1280,
|
| 71 |
+
num_frames=121,
|
| 72 |
+
num_inference_steps=50,
|
| 73 |
+
frame_rate=24,
|
| 74 |
+
use_linear_quadratic_schedule=False,
|
| 75 |
+
)
|
| 76 |
+
export_to_video(output.frames[0], "output.mp4", fps=24)
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
## SageAttention (Optional, ~1.6× faster)
|
| 80 |
+
|
| 81 |
+
Same prompt and seed, 1280x736, 121 frames, 50 steps. Left = SDPA, Right = SageAttention.
|
| 82 |
+
|
| 83 |
+

|
| 84 |
+

|
| 85 |
+

|
| 86 |
+

|
| 87 |
+
|
| 88 |
+
[SageAttention](https://github.com/thu-ml/SageAttention) accelerates attention by quantizing Q/K to INT8 and V to FP8, reducing memory bandwidth. Works with all GGUF variants.
|
| 89 |
+
|
| 90 |
+
**Install** (build from source — PyPI only has 1.x, need 2.x):
|
| 91 |
+
|
| 92 |
+
```bash
|
| 93 |
+
# Set TORCH_CUDA_ARCH_LIST to match your GPU: "8.0" for A100, "9.0" for H100/H200
|
| 94 |
+
TORCH_CUDA_ARCH_LIST="9.0" pip install git+https://github.com/thu-ml/SageAttention.git --no-build-isolation
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
**Usage with `inference.py`:**
|
| 98 |
+
|
| 99 |
+
```bash
|
| 100 |
+
python inference.py --use-sage-attention --prompt "..."
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
**Notes:**
|
| 104 |
+
- Requires NVIDIA GPU with SM70+
|
| 105 |
+
- SM90+ (H100, H200) — FP8 kernels for maximum speedup
|
| 106 |
+
- SM80-SM89 (A100, RTX 3090, RTX 4090) — FP16 kernels (still faster than SDPA)
|
| 107 |
+
- SM70-SM75 (V100, RTX 2080 Ti) — FP16 kernels
|
| 108 |
+
- Set `TORCH_CUDA_ARCH_LIST` to match your GPU when building (e.g., `"8.6"` for RTX 3090, `"8.9"` for RTX 4090)
|
| 109 |
+
- No quality degradation observed across all GGUF variants
|
| 110 |
+
|
| 111 |
+
## Benchmark
|
| 112 |
+
|
| 113 |
+
Measured on NVIDIA H200, 1280x736, 121 frames, 50 steps, DPMSolver++ (order=2, flow_shift=15.0):
|
| 114 |
+
|
| 115 |
+
| Variant | SDPA (s/it) | Sage (s/it) | Speedup | Peak alloc (GB) | Peak rsv (GB) | Total SDPA (s) | Total Sage (s) |
|
| 116 |
+
|---------|------------|------------|---------|-----------------|----------------|----------------|----------------|
|
| 117 |
+
| BF16 | 23.36 | 14.75 | 1.58x | 14.78 / 15.12 | 24.93 / 24.90 | 1184 | 754 |
|
| 118 |
+
| Q8_0 | 23.16 | 14.49 | 1.60x | 13.10 / 13.44 | 23.14 / 23.11 | 1178 | 744 |
|
| 119 |
+
| Q6_K | 23.21 | 14.55 | 1.60x | 12.62 / 12.95 | 22.72 / 22.69 | 1178 | 747 |
|
| 120 |
+
| Q5_K_M | 23.33 | 14.69 | 1.59x | 12.39 / 12.72 | 22.45 / 22.42 | 1184 | 754 |
|
| 121 |
+
| Q5_1 | 23.54 | 14.96 | 1.57x | 12.47 / 12.81 | 22.66 / 22.62 | 1193 | 764 |
|
| 122 |
+
| Q5_0 | 23.26 | 14.67 | 1.59x | 12.37 / 12.71 | 22.55 / 22.52 | 1179 | 750 |
|
| 123 |
+
| Q4_K_M | 23.25 | 14.59 | 1.60x | 12.19 / 12.53 | 22.22 / 22.18 | 1178 | 747 |
|
| 124 |
+
| Q4_1 | 23.31 | 14.68 | 1.59x | 12.26 / 12.60 | 22.26 / 22.22 | 1181 | 750 |
|
| 125 |
+
| Q4_0 | 23.33 | 14.75 | 1.58x | 12.14 / 12.47 | 22.18 / 22.14 | 1188 | 760 |
|
| 126 |
+
|
| 127 |
+
Peak alloc/rsv columns show SDPA / Sage values. Sage adds ~0.3 GB alloc overhead (INT8/FP8 quantization buffers) with no change in reserved memory.
|
| 128 |
+
|
| 129 |
+
**Key findings:**
|
| 130 |
+
- **~1.59x faster with SageAttention** — consistent across all quantization levels
|
| 131 |
+
- **VRAM unchanged** — sage overhead is negligible (~0.3 GB alloc)
|
| 132 |
+
- **GGUF + Sage stacks** — Q4_K_M + Sage achieves 14.59 s/it at 12.53 GB alloc (vs BF16 SDPA: 23.36 s/it at 14.78 GB)
|
docs/memory-efficient-inference.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Memory-efficient Inference
|
| 2 |
+
|
| 3 |
+
> See the main [README](../README.md) for `FlowDPMSolver` and `guider` setup.
|
| 4 |
+
|
| 5 |
+
By default, `pipe.to("cuda")` loads all components onto the GPU simultaneously, requiring **~30 GB VRAM**.
|
| 6 |
+
|
| 7 |
+
For GPUs with 24 GB or less (e.g. RTX 4090, RTX 3090), use `enable_model_cpu_offload()` with the `expandable_segments` allocator setting:
|
| 8 |
+
|
| 9 |
+
```bash
|
| 10 |
+
export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
|
| 11 |
+
```
|
| 12 |
+
|
| 13 |
+
```python
|
| 14 |
+
pipe = MotifVideoPipeline.from_pretrained(
|
| 15 |
+
"Motif-Technologies/Motif-Video-2B",
|
| 16 |
+
revision="diffusers-integration",
|
| 17 |
+
torch_dtype=torch.bfloat16,
|
| 18 |
+
guider=guider, # see T2V example above
|
| 19 |
+
)
|
| 20 |
+
pipe.scheduler = FlowDPMSolver(
|
| 21 |
+
num_train_timesteps=pipe.scheduler.config.get("num_train_timesteps", 1000),
|
| 22 |
+
algorithm_type="dpmsolver++",
|
| 23 |
+
solver_order=2,
|
| 24 |
+
prediction_type="flow_prediction",
|
| 25 |
+
use_flow_sigmas=True,
|
| 26 |
+
flow_shift=15.0,
|
| 27 |
+
)
|
| 28 |
+
pipe.enable_model_cpu_offload() # replaces pipe.to("cuda")
|
| 29 |
+
|
| 30 |
+
output = pipe(
|
| 31 |
+
prompt="...",
|
| 32 |
+
negative_prompt="...",
|
| 33 |
+
height=736, width=1280, num_frames=121, num_inference_steps=50,
|
| 34 |
+
frame_rate=24, use_linear_quadratic_schedule=False,
|
| 35 |
+
)
|
| 36 |
+
export_to_video(output.frames[0], "output.mp4", fps=24)
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
This moves each component (text encoder → transformer → VAE) to GPU only when needed. The `expandable_segments` setting allows the CUDA memory allocator to efficiently reuse memory released by earlier components, avoiding fragmentation-related OOM errors.
|
| 40 |
+
|
| 41 |
+
| Mode | Peak VRAM | Speed | Recommended GPU |
|
| 42 |
+
|------|-----------|-------|-----------------|
|
| 43 |
+
| `pipe.to("cuda")` | ~30 GB | Fastest | A100, H100, H200 |
|
| 44 |
+
| `enable_model_cpu_offload()` | ~19 GB | Similar | RTX 4090, RTX 3090 |
|
| 45 |
+
|
| 46 |
+
## FP8 Weight Quantization (Optional)
|
| 47 |
+
|
| 48 |
+
For further VRAM reduction, you can quantize the transformer weights to FP8 using [torchao](https://github.com/pytorch/ao):
|
| 49 |
+
|
| 50 |
+
```bash
|
| 51 |
+
pip install torchao
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
```python
|
| 55 |
+
from torchao.quantization import quantize_, Float8WeightOnlyConfig
|
| 56 |
+
|
| 57 |
+
pipe = MotifVideoPipeline.from_pretrained(
|
| 58 |
+
"Motif-Technologies/Motif-Video-2B",
|
| 59 |
+
revision="diffusers-integration",
|
| 60 |
+
torch_dtype=torch.bfloat16,
|
| 61 |
+
guider=guider, # see T2V example above
|
| 62 |
+
)
|
| 63 |
+
pipe.scheduler = FlowDPMSolver(
|
| 64 |
+
num_train_timesteps=pipe.scheduler.config.get("num_train_timesteps", 1000),
|
| 65 |
+
algorithm_type="dpmsolver++",
|
| 66 |
+
solver_order=2,
|
| 67 |
+
prediction_type="flow_prediction",
|
| 68 |
+
use_flow_sigmas=True,
|
| 69 |
+
flow_shift=15.0,
|
| 70 |
+
)
|
| 71 |
+
quantize_(pipe.transformer, Float8WeightOnlyConfig())
|
| 72 |
+
pipe.enable_model_cpu_offload()
|
| 73 |
+
|
| 74 |
+
output = pipe(
|
| 75 |
+
prompt="...",
|
| 76 |
+
negative_prompt="...",
|
| 77 |
+
height=736, width=1280, num_frames=121, num_inference_steps=50,
|
| 78 |
+
frame_rate=24, use_linear_quadratic_schedule=False,
|
| 79 |
+
)
|
| 80 |
+
export_to_video(output.frames[0], "output.mp4", fps=24)
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
This stores the transformer weights in FP8 (8-bit) instead of BF16 (16-bit), reducing peak VRAM from ~19 GB to ~15 GB while keeping all computation in BF16 precision.
|
| 84 |
+
|
| 85 |
+
| Mode | Peak VRAM | Notes |
|
| 86 |
+
|------|-----------|-------|
|
| 87 |
+
| `enable_model_cpu_offload()` | ~19 GB | BF16 baseline |
|
| 88 |
+
| `+ Float8WeightOnlyConfig` | ~15 GB | FP8 weights, BF16 compute |
|