|
3 | 3 |
|
4 | 4 | from __future__ import annotations |
5 | 5 |
|
| 6 | +from pathlib import Path |
| 7 | + |
6 | 8 | import numpy as np |
7 | 9 | import pytest |
8 | 10 |
|
9 | 11 | pytest.importorskip("mlx.core", reason="MLX is required for MiniMax H3 fast-spatial tests") |
10 | 12 |
|
| 13 | +from fastvideo.mlx_runtime import rife_interp # noqa: E402 |
11 | 14 | from fastvideo.mlx_runtime.frame_upsample import upsample_frames # noqa: E402 |
| 15 | +from fastvideo.mlx_runtime.minimax_h3 import ( # noqa: E402 |
| 16 | + build_packed_layout, |
| 17 | + video_latent_num_frames, |
| 18 | +) |
12 | 19 | from fastvideo.mlx_runtime.minimax_h3_pipeline import ( # noqa: E402 |
| 20 | + DEFAULT_FAST_SPATIAL_SHARPEN, |
| 21 | + MiniMaxH3MLXPipeline, |
13 | 22 | _center_crop_frames, |
14 | 23 | _preflight_media_dependencies, |
15 | 24 | plan_fast_spatial, |
@@ -71,3 +80,172 @@ def test_spatial_crop_then_upsample_restores_exact_target_size() -> None: |
71 | 80 | upsampled = upsample_frames(cropped, width=plan.target_width, height=plan.target_height, |
72 | 81 | mode="bilinear", sharpen=0.0) |
73 | 82 | assert np.stack(upsampled).shape == (2, 480, 832, 3) |
| 83 | + |
| 84 | + |
| 85 | +# -- generation-level orchestration contracts (heavyweight phases mocked) ---- |
| 86 | + |
| 87 | + |
| 88 | +def _generate_with_mocked_phases(monkeypatch, tmp_path, **generate_kwargs): |
| 89 | + """Run real ``generate()`` orchestration with condition/denoise/decode/mux stubbed.""" |
| 90 | + events: list[str] = [] |
| 91 | + calls: dict = {} |
| 92 | + |
| 93 | + pipeline = MiniMaxH3MLXPipeline.__new__(MiniMaxH3MLXPipeline) |
| 94 | + pipeline.dit_checkpoint = tmp_path |
| 95 | + |
| 96 | + monkeypatch.setattr("fastvideo.mlx_runtime.minimax_h3_pipeline._validate_checkpoint_step_ladder", |
| 97 | + lambda _checkpoint, _steps: None) |
| 98 | + monkeypatch.setattr("fastvideo.mlx_runtime.minimax_h3_pipeline._preflight_media_dependencies", |
| 99 | + lambda **_kwargs: None) |
| 100 | + monkeypatch.setattr("fastvideo.mlx_runtime.minimax_h3_pipeline.mlx_h3_checkpoint_vsa_capable", |
| 101 | + lambda _checkpoint: False) |
| 102 | + |
| 103 | + def fake_encode_prompt(_prompt): |
| 104 | + return np.zeros((8, 8), dtype=np.float32), np.zeros(8, dtype=np.int64) |
| 105 | + |
| 106 | + def fake_denoise(_text_rows, _token_tags, **kwargs): |
| 107 | + events.append("denoise") |
| 108 | + calls["denoise"] = kwargs |
| 109 | + return np.zeros((4, 4), dtype=np.float32), np.zeros((4, 4), dtype=np.float32) |
| 110 | + |
| 111 | + def fake_decode_video(_rows, *, height, width, num_frames, tiled): |
| 112 | + events.append("decode_video") |
| 113 | + calls["decode_video"] = {"height": height, "width": width, "num_frames": num_frames} |
| 114 | + return np.zeros((num_frames, height, width, 3), dtype=np.uint8) |
| 115 | + |
| 116 | + def fake_decode_audio(_rows, *, num_frames): |
| 117 | + events.append("decode_audio") |
| 118 | + calls["decode_audio"] = {"num_frames": num_frames} |
| 119 | + return np.zeros((2, 64), dtype=np.float32) |
| 120 | + |
| 121 | + pipeline.encode_prompt = fake_encode_prompt |
| 122 | + pipeline.denoise = fake_denoise |
| 123 | + pipeline.decode_video = fake_decode_video |
| 124 | + pipeline.decode_audio = fake_decode_audio |
| 125 | + pipeline.mux = lambda _frames, _waveform, output_path: Path(output_path) |
| 126 | + |
| 127 | + def fake_interpolate(frames, target, *, model): |
| 128 | + events.append("rife") |
| 129 | + calls["rife"] = {"target": target} |
| 130 | + return [np.array(frames[0]) for _ in range(target)] |
| 131 | + |
| 132 | + def fake_load_model(weights_dir=None): |
| 133 | + return object() |
| 134 | + |
| 135 | + fake_load_model.cache_clear = lambda: None |
| 136 | + monkeypatch.setattr(rife_interp, "interpolate_to_frame_count", fake_interpolate) |
| 137 | + monkeypatch.setattr(rife_interp, "load_model", fake_load_model) |
| 138 | + |
| 139 | + def fake_sharpen(frames, amount): |
| 140 | + events.append("sharpen") |
| 141 | + calls.setdefault("sharpen", []).append(amount) |
| 142 | + return list(frames) |
| 143 | + |
| 144 | + def fake_upsample(frames, *, width, height, mode, sharpen): |
| 145 | + events.append("upsample") |
| 146 | + calls["upsample"] = {"width": width, "height": height, "mode": mode, "sharpen": sharpen} |
| 147 | + return [np.zeros((height, width, 3), dtype=np.uint8) for _ in frames] |
| 148 | + |
| 149 | + monkeypatch.setattr("fastvideo.mlx_runtime.minimax_h3_pipeline._sharpen_frames", fake_sharpen) |
| 150 | + monkeypatch.setattr("fastvideo.mlx_runtime.minimax_h3_pipeline.upsample_frames", fake_upsample) |
| 151 | + |
| 152 | + result = pipeline.generate( |
| 153 | + "(S1) test prompt", |
| 154 | + output_path=tmp_path / "out.mp4", |
| 155 | + height=480, |
| 156 | + width=832, |
| 157 | + num_frames=124, |
| 158 | + save_frames=True, |
| 159 | + **generate_kwargs, |
| 160 | + ) |
| 161 | + return events, calls, result |
| 162 | + |
| 163 | + |
| 164 | +def test_generate_spatial_only_denoises_reduced_canvas_with_full_audio( |
| 165 | + monkeypatch: pytest.MonkeyPatch, tmp_path) -> None: |
| 166 | + events, calls, result = _generate_with_mocked_phases(monkeypatch, tmp_path, fast_spatial=True) |
| 167 | + |
| 168 | + assert calls["denoise"]["height"] == 256 |
| 169 | + assert calls["denoise"]["width"] == 416 |
| 170 | + assert calls["denoise"]["num_frames"] == 124 |
| 171 | + assert calls["denoise"]["audio_num_frames"] is None |
| 172 | + assert calls["denoise"]["video_temporal_scale"] == 1.0 |
| 173 | + assert calls["decode_video"] == {"height": 256, "width": 416, "num_frames": 124} |
| 174 | + assert calls["decode_audio"] == {"num_frames": 124} |
| 175 | + assert calls["upsample"] == { |
| 176 | + "width": 832, "height": 480, "mode": "lanczos", |
| 177 | + "sharpen": pytest.approx(DEFAULT_FAST_SPATIAL_SHARPEN), |
| 178 | + } |
| 179 | + assert "rife" not in events |
| 180 | + assert "sharpen" not in events |
| 181 | + assert result.frames.shape == (124, 480, 832, 3) |
| 182 | + assert "spatial_upsample_s" in result.timings |
| 183 | + |
| 184 | + |
| 185 | +def test_generate_temporal_only_control_keeps_full_canvas(monkeypatch: pytest.MonkeyPatch, tmp_path) -> None: |
| 186 | + events, calls, result = _generate_with_mocked_phases(monkeypatch, tmp_path, fast=True) |
| 187 | + |
| 188 | + assert calls["denoise"]["height"] == 480 |
| 189 | + assert calls["denoise"]["width"] == 832 |
| 190 | + assert calls["denoise"]["num_frames"] == 73 |
| 191 | + assert calls["denoise"]["audio_num_frames"] == 124 |
| 192 | + assert calls["denoise"]["video_temporal_scale"] > 1.0 |
| 193 | + assert calls["decode_video"] == {"height": 480, "width": 832, "num_frames": 73} |
| 194 | + assert calls["rife"] == {"target": 124} |
| 195 | + assert calls["decode_audio"] == {"num_frames": 124} |
| 196 | + assert calls["sharpen"] == [pytest.approx(0.6)] |
| 197 | + assert "upsample" not in events |
| 198 | + assert result.frames.shape == (124, 480, 832, 3) |
| 199 | + |
| 200 | + |
| 201 | +def test_generate_stacked_runs_rife_before_upsample_with_one_sharpen( |
| 202 | + monkeypatch: pytest.MonkeyPatch, tmp_path) -> None: |
| 203 | + events, calls, result = _generate_with_mocked_phases(monkeypatch, tmp_path, fast=True, fast_spatial=True) |
| 204 | + |
| 205 | + assert calls["denoise"]["height"] == 256 |
| 206 | + assert calls["denoise"]["width"] == 416 |
| 207 | + assert calls["denoise"]["num_frames"] == 73 |
| 208 | + assert calls["denoise"]["audio_num_frames"] == 124 |
| 209 | + assert calls["decode_video"] == {"height": 256, "width": 416, "num_frames": 73} |
| 210 | + assert calls["rife"] == {"target": 124} |
| 211 | + assert events.index("rife") < events.index("upsample") |
| 212 | + assert "sharpen" not in events |
| 213 | + assert calls["upsample"]["sharpen"] == pytest.approx(0.6) |
| 214 | + assert calls["decode_audio"] == {"num_frames": 124} |
| 215 | + assert result.frames.shape == (124, 480, 832, 3) |
| 216 | + |
| 217 | + |
| 218 | +# -- reduced VSA layout contracts -------------------------------------------- |
| 219 | + |
| 220 | + |
| 221 | +def test_reduced_layout_has_8x13_video_grid_and_unchanged_audio_prefix() -> None: |
| 222 | + reduced = build_packed_layout(8, 37, 16, 26, 207) |
| 223 | + full = build_packed_layout(8, 37, 30, 52, 207) |
| 224 | + |
| 225 | + assert reduced.video_indices.shape[0] == 37 * 8 * 13 |
| 226 | + assert full.video_indices.shape[0] == 37 * 15 * 26 |
| 227 | + np.testing.assert_array_equal(reduced.audio_indices, full.audio_indices) |
| 228 | + # A/V sync rides on the audio rows' temporal positions (column 0), which |
| 229 | + # must not move with the canvas. Column 2 is excluded on purpose: audio |
| 230 | + # rows borrow the video width grid's edge coordinates for their spatial |
| 231 | + # tag, so it tracks the canvas the same way any native resolution change |
| 232 | + # does. |
| 233 | + np.testing.assert_array_equal( |
| 234 | + reduced.position_ids[reduced.audio_indices, 0], |
| 235 | + full.position_ids[full.audio_indices, 0], |
| 236 | + ) |
| 237 | + np.testing.assert_array_equal(reduced.position_ids[reduced.audio_indices, 1], |
| 238 | + np.zeros(reduced.audio_indices.shape[0])) |
| 239 | + |
| 240 | + |
| 241 | +def test_reduced_layout_stacked_uses_22_latent_frames() -> None: |
| 242 | + assert video_latent_num_frames(73) == 22 |
| 243 | + stacked = build_packed_layout(8, 22, 16, 26, 207, video_temporal_scale=1.7) |
| 244 | + baseline = build_packed_layout(8, 22, 16, 26, 207) |
| 245 | + |
| 246 | + assert stacked.video_indices.shape[0] == 22 * 8 * 13 |
| 247 | + np.testing.assert_array_equal(stacked.audio_indices, baseline.audio_indices) |
| 248 | + np.testing.assert_array_equal( |
| 249 | + stacked.position_ids[stacked.audio_indices], |
| 250 | + baseline.position_ids[baseline.audio_indices], |
| 251 | + ) |
0 commit comments