refine ΒΆ
Two-pass spatial refine for the MLX Wan runtime (H3 / LTX-2 pattern).
Biggest quality lever on Apple Silicon without a new model or training: generate at base resolution, then run a second denoising pass with the same DiT at a higher resolution.
This is the MLX-side port of the CUDA refine template in fastvideo/pipelines/basic/ltx2/stages/ltx2_refine.py and the H3 "base + regenerate" pattern documented in docs/design/mac_qad_two_product_strategy.md:
- :func:
plan_refine_resolutionsβ split the request into stage-1 (base) and stage-2 (target) pixel sizes, validating VAE / patch alignment the way :class:LTX2RefineInitStagedoes. - :func:
upsample_latents_spatialβ 2Γ (or NΓ) spatial upsample of clean latents. Wan has no learned latent upsampler on Mac, so this is bilinear over the HΓW plane (temporal axis untouched) β same role as LTX-2'supsample_videohand-off, without the learned residual. - :func:
prepare_refine_latentsβ upsample + re-noise the clean stage-1 latents to the stage-2 sigma so the second denoise has something to refine (mirrors :class:LTX2UpsampleStage+apply_ltx2_gaussian_noiser). - :func:
run_two_pass_dmdβ orchestrate stage-1 denoise β refine hand-off β stage-2 denoise with the same model / prompt embeds.
No LoRA swap, no dedicated SR weights, no new training β pure pipeline work reusable by Wan2.1-14B and Wan2.2-5B on Apple Silicon.
ClassesΒΆ
fastvideo.mlx_runtime.refine.RefinePlan dataclass ΒΆ
RefinePlan(target_height: int, target_width: int, stage1_height: int, stage1_width: int, spatial_scale: int, vae_spatial_compression: int, vae_temporal_compression: int, num_frames: int)
Resolved stage-1 / stage-2 geometry for a two-pass refine run.
AttributesΒΆ
fastvideo.mlx_runtime.refine.RefinePlan.latent_frames property ΒΆ
latent_frames: int
Calculate the number of latent frames after VAE temporal compression.
Returns:
| Name | Type | Description |
|---|---|---|
int | int | The compressed latent frame count. |
fastvideo.mlx_runtime.refine.RefinePlan.stage1_latent_height property ΒΆ
stage1_latent_height: int
Return the stage-1 latent height after VAE spatial compression.
fastvideo.mlx_runtime.refine.RefinePlan.stage1_latent_width property ΒΆ
stage1_latent_width: int
Return the stage-one latent width after VAE spatial compression.
fastvideo.mlx_runtime.refine.TwoPassResult dataclass ΒΆ
TwoPassResult(latents: Any, stage1_latents: Any, plan: RefinePlan, refine_sigma: float)
Outputs of :func:run_two_pass_dmd.
Functions:ΒΆ
fastvideo.mlx_runtime.refine.default_refine_timesteps ΒΆ
Derive stage-2 timesteps from the stage-1 DMD grid.
The stage-2 pass must start below full noise, otherwise the hand-off (1 - sigma) * upsampled + sigma * noise weights stage 1 at zero and the refine pass silently becomes a plain full-resolution generation at twice the cost. FastWan's stage-1 grid opens at t=1000 (sigma exactly 1.0), so reusing it verbatim β which is what happens when --refine-dmd-denoising-steps is left unset β discards stage 1.
Dropping the leading full-noise entries keeps the pass on timesteps the distilled student was actually trained on (no off-grid t the DiT has never seen) while letting the stage-1 structure through.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schedule | MLXDMDSchedule | Schedule used to map timesteps to noise levels. | required |
timesteps | Sequence[float | int] | The stage-1 DMD timestep grid. | required |
Returns:
| Type | Description |
|---|---|
list[float] | list[float]: The stage-1 grid with leading full-noise timesteps removed. |
Raises:
| Type | Description |
|---|---|
ValueError | If every timestep in the grid is at full noise, leaving no usable refine step. |
Source code in fastvideo/mlx_runtime/refine.py
fastvideo.mlx_runtime.refine.plan_refine_resolutions ΒΆ
plan_refine_resolutions(*, height: int, width: int, num_frames: int, spatial_scale: int = 2, vae_spatial_compression: int = 8, vae_temporal_compression: int = 4, patch_size: tuple[int, int, int] = (1, 2, 2), enabled: bool = True, mode_label: str = 'Refine') -> RefinePlan
Validate the requested dimensions and create the stage-1 and target-resolution refinement plan.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
height | int | Target image height in pixels. | required |
width | int | Target image width in pixels. | required |
num_frames | int | Number of frames in the input sequence. | required |
spatial_scale | int | Factor used to reduce spatial dimensions for stage 1. | 2 |
vae_spatial_compression | int | Spatial compression factor of the VAE. | 8 |
vae_temporal_compression | int | Temporal compression factor of the VAE. | 4 |
patch_size | tuple[int, int, int] | Temporal and spatial patch dimensions used to validate latent-grid alignment. | (1, 2, 2) |
enabled | bool | Whether to use two-pass refinement. | True |
mode_label | str | Name of the calling mode, used to prefix validation errors so | 'Refine' |
Returns:
| Name | Type | Description |
|---|---|---|
RefinePlan | RefinePlan | The validated stage-1 and target-resolution plan. |
Source code in fastvideo/mlx_runtime/refine.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
fastvideo.mlx_runtime.refine.prepare_refine_latents ΒΆ
prepare_refine_latents(clean_latents: Any, *, scale: int = 2, sigma: float = DEFAULT_REFINE_SIGMA, noise: Any | None = None, add_noise_flag: bool = True, upsample_mode: str = 'bilinear', seed: int | None = None) -> Any
Upsample clean latents spatially and optionally mix them with Gaussian noise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clean_latents | Any | The stage-1 latent tensor. | required |
sigma | float | Noise mixing factor between 0 and 1. | DEFAULT_REFINE_SIGMA |
noise | Any | None | Optional noise tensor to mix with the upsampled latents. | None |
add_noise_flag | bool | Whether to apply noise mixing. | True |
upsample_mode | str | Spatial interpolation mode. | 'bilinear' |
seed | int | None | Optional seed for generated noise. | None |
Returns:
| Type | Description |
|---|---|
Any | The upsampled latents, optionally mixed with noise. |
Raises:
| Type | Description |
|---|---|
ValueError | If sigma is outside the range from 0 to 1. |
Source code in fastvideo/mlx_runtime/refine.py
fastvideo.mlx_runtime.refine.refine_sigma_from_schedule ΒΆ
refine_sigma_from_schedule(schedule: MLXDMDSchedule, timesteps: Sequence[float | int]) -> float
Derive the refinement noise level from the first refinement timestep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schedule | MLXDMDSchedule | Schedule used to map timesteps to noise levels. | required |
timesteps | Sequence[float | int] | Refinement timesteps, whose first value determines the sigma. | required |
Returns:
| Name | Type | Description |
|---|---|---|
float | float | Sigma corresponding to the first refinement timestep. |
Raises:
| Type | Description |
|---|---|
ValueError | If |
Source code in fastvideo/mlx_runtime/refine.py
fastvideo.mlx_runtime.refine.run_dmd_loop ΒΆ
run_dmd_loop(*, dit: Any, latents: Any, encoder_hidden_states: Any, freqs_cis: tuple[Any, Any], timesteps: Sequence[float | int], schedule: MLXDMDSchedule, mx_dtype: Any, seed: int | None = None, step_callback: Callable[[int, int], None] | None = None, label: str = 'denoise') -> Any
Denoise latents over the supplied timesteps using the DMD schedule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timesteps | Sequence[float | int] | Denoising timesteps in execution order. | required |
seed | int | None | Seed for reproducible intermediate noise generation. | None |
step_callback | Callable[[int, int], None] | None | Callback receiving the completed step number and total step count. | None |
label | str | Label used for progress output when no callback is provided. | 'denoise' |
Returns:
| Name | Type | Description |
|---|---|---|
Any | Any | The denoised latents. |
Source code in fastvideo/mlx_runtime/refine.py
fastvideo.mlx_runtime.refine.run_two_pass_dmd ΒΆ
run_two_pass_dmd(*, dit: Any, encoder_hidden_states: Any, noise_latents_stage1: Any, freqs_cis_stage1: tuple[Any, Any], freqs_cis_stage2: tuple[Any, Any] | None, plan: RefinePlan, schedule: MLXDMDSchedule, timesteps: Sequence[float | int], refine_timesteps: Sequence[float | int] | None = None, mx_dtype: Any, seed: int = 0, add_noise_flag: bool = True, upsample_mode: str = 'bilinear', refine_sigma: float | None = None, step_callback: Callable[[str, int, int], None] | None = None) -> TwoPassResult
Run base denoising and, when enabled, spatial refinement denoising.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dit | Any | DiT callable used for both denoising passes. | required |
encoder_hidden_states | Any | Prompt embeddings shared across both passes. | required |
noise_latents_stage1 | Any | Initial stage-1 noise latents. | required |
freqs_cis_stage1 | tuple[Any, Any] | RoPE tables for the stage-1 resolution. | required |
freqs_cis_stage2 | tuple[Any, Any] | None | RoPE tables for the stage-2 resolution, required when refinement is enabled. | required |
plan | RefinePlan | Refinement geometry and configuration. | required |
schedule | MLXDMDSchedule | Flow-matching schedule used by both passes. | required |
timesteps | Sequence[float | int] | Stage-1 denoising timesteps. | required |
refine_timesteps | Sequence[float | int] | None | Stage-2 denoising timesteps. Uses | None |
mx_dtype | Any | MLX dtype used for DiT inputs and outputs. | required |
seed | int | Base seed for reproducible noise generation. | 0 |
add_noise_flag | bool | Whether to add noise to the upsampled stage-1 latents. | True |
upsample_mode | str | Spatial upsampling mode, either | 'bilinear' |
refine_sigma | float | None | Stage-2 starting noise level. Derived from the first refinement timestep when omitted. | None |
step_callback | Callable[[str, int, int], None] | None | Optional callback receiving the phase name, step index, and total step count. | None |
Returns:
| Type | Description |
|---|---|
TwoPassResult | TwoPassResult containing the final latents, stage-1 latents, refinement plan, and applied refinement sigma. |
Raises:
| Type | Description |
|---|---|
ValueError | If refinement is enabled without stage-2 RoPE tables, without refinement timesteps, or if upsampled latents do not match the planned stage-2 dimensions. |
Source code in fastvideo/mlx_runtime/refine.py
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 | |
fastvideo.mlx_runtime.refine.upsample_latents_spatial ΒΆ
Upsample the spatial dimensions of 5-D latent arrays while preserving the batch, channel, and temporal dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latents | Any | Latents with shape | required |
scale | int | Integer factor for enlarging the spatial dimensions. | 2 |
mode | str | Interpolation mode, either | 'bilinear' |
Returns:
| Name | Type | Description |
|---|---|---|
Any | Any | Latents with shape |