Commit 11f370b
[refactor]: linear/mlp FP4 path additions for Wan-2.1 (Attn-QAT 6/12)
Slice 6 of 12 in the PR #1225 decomposition. Tier 2 — backward-
compat additions, gated paths only. No activation in this slice.
What this adds
--------------
* fastvideo/layers/linear.py (+54): adds opt-in shape-tracking
instrumentation to ``ReplicatedLinear`` so upcoming QAT-aware
backends can discover which GEMM shapes need quantized kernels.
Gated by the class attr ``enable_shape_tracking = False``; the
default forward path is bit-identical to pre-slice behavior. Adds
``get_shape_mapping``, ``reset_shape_tracking``, ``_track_shape``,
``print_shape_summary``. No new constructor params.
* fastvideo/layers/mlp.py (+22): adds an optional
``quant_config: QuantizationConfig | None = None`` kwarg to
``MLP.__init__`` and threads it (plus an explicit ``prefix``) into
the two underlying ``ReplicatedLinear`` instances. When
``quant_config is not None``, runs
``process_weights_after_loading`` on each sub-layer's resolved
quant method. When ``quant_config is None`` (default), behavior is
unchanged: ``ReplicatedLinear`` falls back to
``UnquantizedLinearMethod`` exactly as before.
* fastvideo/models/dits/wanvideo.py (+67): wires ``quant_config``
through ``WanSelfAttention``, ``WanI2VCrossAttention``,
``WanTransformerBlock``, ``WanTransformerBlock_VSA``, and
``WanTransformer3DModel`` constructors so a future ``NVFP4QAT``-
configured Wan2.1 build can quantize its attention QKV/out
projections and FFN. Reads ``config.quant_config`` from
``WanVideoConfig`` (the field is already present on the shared
``DiTBaseConfig``). All new kwargs default to ``None``; default
Wan2.1 path stays bit-identical.
Files in PR #1225 considered but NOT applied
--------------------------------------------
The source-SHA ``fastvideo/layers/linear.py`` also contains several
edits that pre-date current ``main`` and would silently regress it:
* Removal of the ``NVFP4Config``-only-quantizes-a-curated-subset
explanatory comments in ``LinearBase.__init__`` and
``ReplicatedLinear.__init__`` (added on main as part of slice 3 /
PR #1336).
* Removal of the
``if self.quant_method is None: self.quant_method = UnquantizedLinearMethod()``
fallback inside ``LinearBase.__init__`` (also part of the slice 3
hardening).
* A constructor / ``create_weights`` reformat from multi-line to
compact one-line style — pure style noise.
* ``assert self.quant_method is not None`` →
``if self.quant_method is None: self.quant_method = UnquantizedLinearMethod()``
in ``ColumnParallelLinear.__init__/forward`` and
``RowParallelLinear.__init__/forward``. ``LinearBase.__init__`` on
current ``main`` already guarantees ``quant_method`` is non-None,
so the source PR's defensive checks would be no-ops; they pre-date
the slice 3 base-class hardening.
* The same ``if quant_method is None`` defensive insert in
``ReplicatedLinear.forward`` — also a no-op against current
``main`` for the same reason.
None of the skipped edits affect the FP4 path; current ``main``'s
behavior on those lines is strictly stronger than the source SHA's.
This mirrors slice 5's intentional skip of the
``sage_attn3.py`` head_size removal (see PR #1383).
Also dropped: an unused ``from contextlib import nullcontext`` import
that the source PR staged in ``wanvideo.py`` for a deeper-stack
slice (ruff would reject it as unused).
Stacking
--------
Base: ``main`` (slice 5 / PR #1383 merged at
``ba75ad82dbe4a7069412494c051c1c69155fdc9d``). No stack dependency
— this is a clean linear PR off ``main``.
Provenance
----------
Files extracted from PR #1225
(#1225) at source SHA
``3f818d0fc532ec6494b465967d5f485150917d0c`` and audited against
current ``main``. ``mlp.py`` and ``wanvideo.py`` (modulo the dropped
unused import) were applied directly — ``main`` had not diverged
from the source's merge-base for those files. ``linear.py`` was
hand-merged to preserve current ``main``'s slice-3 hardening (see
the ``NOT applied`` list above); only the additive shape-tracking
surface was carried over.
Pre-commit gate (yapf + ruff + codespell + mypy) passes on all
three changed files.
Test plan
---------
No new tests this slice. The shape-tracking surface is opt-in
instrumentation (default disabled) and the ``quant_config`` plumbing
is dormant until a future slice sets ``config.quant_config`` to a
non-None value. The activation slice (12/12) will carry the
contract test for the full FP4 Wan-2.1 path.
Sequence
--------
Attn-QAT-Stack: 6/12. Earlier merged slices: 4/12 (PR #1358),
5/12 (PR #1383). Out of scope for this slice: the actual FP4
activation switch, weight-loading conversion, and any cross-cutting
config registration (later slices).
Co-Authored-By: Peiyuan Zhang <a1286225768@gmail.com>
Co-Authored-By: Matthew Noto <notomatthew31@gmail.com>1 parent ba75ad8 commit 11f370b
3 files changed
Lines changed: 115 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
219 | 219 | | |
220 | 220 | | |
221 | 221 | | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
222 | 232 | | |
223 | 233 | | |
224 | 234 | | |
| |||
285 | 295 | | |
286 | 296 | | |
287 | 297 | | |
| 298 | + | |
| 299 | + | |
288 | 300 | | |
289 | 301 | | |
290 | 302 | | |
| |||
294 | 306 | | |
295 | 307 | | |
296 | 308 | | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
297 | 351 | | |
298 | 352 | | |
299 | 353 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| 25 | + | |
24 | 26 | | |
25 | 27 | | |
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
29 | 31 | | |
30 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
31 | 40 | | |
32 | 41 | | |
33 | 42 | | |
34 | 43 | | |
35 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
36 | 54 | | |
37 | 55 | | |
38 | 56 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
106 | 107 | | |
107 | 108 | | |
108 | 109 | | |
109 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
110 | 113 | | |
111 | 114 | | |
112 | 115 | | |
| |||
118 | 121 | | |
119 | 122 | | |
120 | 123 | | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
125 | 128 | | |
126 | 129 | | |
127 | 130 | | |
| |||
194 | 197 | | |
195 | 198 | | |
196 | 199 | | |
197 | | - | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
198 | 203 | | |
199 | 204 | | |
200 | | - | |
| 205 | + | |
201 | 206 | | |
202 | | - | |
203 | | - | |
| 207 | + | |
| 208 | + | |
204 | 209 | | |
205 | 210 | | |
206 | 211 | | |
| |||
246 | 251 | | |
247 | 252 | | |
248 | 253 | | |
| 254 | + | |
249 | 255 | | |
250 | 256 | | |
251 | 257 | | |
252 | 258 | | |
253 | 259 | | |
254 | | - | |
255 | | - | |
256 | | - | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
257 | 263 | | |
258 | | - | |
| 264 | + | |
259 | 265 | | |
260 | 266 | | |
261 | 267 | | |
| |||
290 | 296 | | |
291 | 297 | | |
292 | 298 | | |
293 | | - | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
294 | 302 | | |
295 | 303 | | |
296 | 304 | | |
297 | 305 | | |
298 | 306 | | |
299 | | - | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
300 | 310 | | |
301 | 311 | | |
302 | 312 | | |
| |||
306 | 316 | | |
307 | 317 | | |
308 | 318 | | |
309 | | - | |
| 319 | + | |
310 | 320 | | |
311 | 321 | | |
312 | 322 | | |
| |||
406 | 416 | | |
407 | 417 | | |
408 | 418 | | |
| 419 | + | |
409 | 420 | | |
410 | 421 | | |
411 | 422 | | |
412 | 423 | | |
413 | 424 | | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | | - | |
419 | | - | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
420 | 430 | | |
421 | 431 | | |
422 | 432 | | |
| |||
451 | 461 | | |
452 | 462 | | |
453 | 463 | | |
454 | | - | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
455 | 467 | | |
456 | 468 | | |
457 | 469 | | |
458 | 470 | | |
459 | 471 | | |
460 | | - | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
461 | 475 | | |
462 | 476 | | |
463 | 477 | | |
| |||
467 | 481 | | |
468 | 482 | | |
469 | 483 | | |
470 | | - | |
| 484 | + | |
471 | 485 | | |
472 | 486 | | |
473 | 487 | | |
| |||
556 | 570 | | |
557 | 571 | | |
558 | 572 | | |
| 573 | + | |
559 | 574 | | |
560 | 575 | | |
561 | 576 | | |
| |||
594 | 609 | | |
595 | 610 | | |
596 | 611 | | |
| 612 | + | |
597 | 613 | | |
598 | 614 | | |
599 | 615 | | |
| |||
0 commit comments