File size: 34,475 Bytes
d5e0d8f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 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 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 | """Wan Predictor-v4 for skipped Self-Forcing denoising steps.
The Predictor is initialized from an already-loaded ``CausalWanModel``. It
keeps the Teacher patch/time/head modules frozen, trains two copied causal Wan
blocks, and predicts a residual over the same-chunk anchor hidden state.
Two history paths are supported:
* online F-P-P-F inference can pass the generator's existing KV caches;
* offline training can rebuild selected-layer history KV from clean-pass
self-attention prefeatures with :meth:`build_history_kv_cache`.
The ordinary ``state_dict`` API is intentionally unchanged. Use
``trainable_state_dict``/``checkpoint_dict`` for compact Predictor checkpoints
that omit frozen Teacher weights.
"""
from __future__ import annotations
import copy
import math
from collections import OrderedDict
from collections.abc import Mapping, Sequence
from dataclasses import asdict, dataclass
from typing import Any
import torch
from torch import nn
from wan.modules.causal_model import (
CausalWanAttentionBlock,
CausalWanModel,
causal_rope_apply,
)
from wan.modules.model import sinusoidal_embedding_1d
@dataclass(frozen=True)
class WanPredictorV4Config:
"""Serializable architecture metadata derived from the loaded Teacher."""
format_version: int
model_type: str
patch_size: tuple[int, int, int]
in_dim: int
dim: int
ffn_dim: int
freq_dim: int
out_dim: int
num_heads: int
num_layers: int
local_attn_size: int
sink_size: int
qk_norm: bool
cross_attn_norm: bool
eps: float
source_block_ids: tuple[int, int]
spatial_grid: tuple[int, int]
@property
def tokens_per_frame(self) -> int:
return math.prod(self.spatial_grid)
def to_dict(self) -> dict[str, Any]:
return asdict(self)
class TripleFeatureFusion(nn.Module):
"""Fuse target-latent, same-chunk anchor, and previous-chunk features."""
def __init__(self, dim: int, eps: float = 1e-6) -> None:
super().__init__()
self.current_norm = nn.LayerNorm(dim, eps=eps)
self.anchor_norm = nn.LayerNorm(dim, eps=eps)
self.previous_norm = nn.LayerNorm(dim, eps=eps)
self.mlp = nn.Sequential(
nn.Linear(3 * dim, 2 * dim),
nn.SiLU(),
nn.Linear(2 * dim, dim),
)
def forward(
self,
current: torch.Tensor,
anchor: torch.Tensor,
previous: torch.Tensor,
) -> torch.Tensor:
if current.shape != anchor.shape or current.shape != previous.shape:
raise ValueError(
"TripleFeatureFusion requires identical [B, L, D] shapes, got "
f"current={tuple(current.shape)}, anchor={tuple(anchor.shape)}, "
f"previous={tuple(previous.shape)}"
)
return self.mlp(
torch.cat(
(
self.current_norm(current),
self.anchor_norm(anchor),
self.previous_norm(previous),
),
dim=-1,
)
)
class _FrozenHistoryProjector(nn.Module):
"""Frozen copy of one Teacher self-attention K/V projection path."""
def __init__(self, teacher_block: CausalWanAttentionBlock) -> None:
super().__init__()
self.k = copy.deepcopy(teacher_block.self_attn.k)
self.v = copy.deepcopy(teacher_block.self_attn.v)
self.norm_k = copy.deepcopy(teacher_block.self_attn.norm_k)
self.requires_grad_(False)
def forward(
self,
self_attn_input: torch.Tensor,
*,
num_heads: int,
) -> tuple[torch.Tensor, torch.Tensor]:
batch, tokens, dim = self_attn_input.shape
if dim % num_heads:
raise ValueError(f"Hidden dim {dim} is not divisible by {num_heads} heads")
head_dim = dim // num_heads
key = self.norm_k(self.k(self_attn_input)).view(
batch, tokens, num_heads, head_dim
)
value = self.v(self_attn_input).view(
batch, tokens, num_heads, head_dim
)
return key, value
class SelfForcingPredictorV4(nn.Module):
"""Two-block Wan Predictor used for the middle denoising steps of F-P-P-F."""
requires_previous_chunk_hidden = True
uses_history_kv = True
uses_clean_prefeature = True
checkpoint_format_version = 1
def __init__(
self,
teacher_model: CausalWanModel,
*,
source_block_ids: tuple[int, int] = (1, 28),
spatial_grid: tuple[int, int] = (30, 52),
) -> None:
super().__init__()
teacher_model = self._unwrap_teacher(teacher_model)
if teacher_model.model_type != "t2v":
raise NotImplementedError("SelfForcingPredictorV4 currently supports Wan T2V")
if len(source_block_ids) != 2 or len(set(source_block_ids)) != 2:
raise ValueError("Predictor-v4 requires exactly two distinct source blocks")
if any(index < 0 or index >= len(teacher_model.blocks) for index in source_block_ids):
raise ValueError(
f"Invalid source blocks {source_block_ids} for "
f"{len(teacher_model.blocks)} Teacher blocks"
)
if len(spatial_grid) != 2 or any(int(size) <= 0 for size in spatial_grid):
raise ValueError(f"Invalid Predictor token spatial grid: {spatial_grid}")
first_self_attn = teacher_model.blocks[0].self_attn
self.predictor_config = WanPredictorV4Config(
format_version=self.checkpoint_format_version,
model_type=str(teacher_model.model_type),
patch_size=tuple(int(item) for item in teacher_model.patch_size),
in_dim=int(teacher_model.in_dim),
dim=int(teacher_model.dim),
ffn_dim=int(teacher_model.ffn_dim),
freq_dim=int(teacher_model.freq_dim),
out_dim=int(teacher_model.out_dim),
num_heads=int(teacher_model.num_heads),
num_layers=len(teacher_model.blocks),
local_attn_size=int(teacher_model.local_attn_size),
sink_size=int(first_self_attn.sink_size),
qk_norm=bool(teacher_model.qk_norm),
cross_attn_norm=bool(teacher_model.cross_attn_norm),
eps=float(teacher_model.eps),
source_block_ids=tuple(int(item) for item in source_block_ids),
spatial_grid=tuple(int(item) for item in spatial_grid),
)
cfg = self.predictor_config
# Frozen modules are copied rather than referenced so that calling
# Predictor.train()/to() cannot alter the loaded generator.
self.patch_embedding = copy.deepcopy(teacher_model.patch_embedding)
self.time_embedding = copy.deepcopy(teacher_model.time_embedding)
self.time_projection = copy.deepcopy(teacher_model.time_projection)
self.head = copy.deepcopy(teacher_model.head)
self.predictor_blocks = nn.ModuleList(
[copy.deepcopy(teacher_model.blocks[index]) for index in source_block_ids]
)
self.history_projectors = nn.ModuleDict(
{
str(index): _FrozenHistoryProjector(teacher_model.blocks[index])
for index in source_block_ids
}
)
self.feature_fusion = TripleFeatureFusion(cfg.dim, cfg.eps)
self.residual_out = nn.Linear(cfg.dim, cfg.dim)
self._freeze_teacher_modules()
self.predictor_blocks.requires_grad_(True)
# Text K/V come from the Teacher cross-attention cache. Predictor only
# executes the query/output side, so keep unused cache-building weights
# frozen and out of the optimizer/checkpoint.
for block in self.predictor_blocks:
block.cross_attn.k.requires_grad_(False)
block.cross_attn.v.requires_grad_(False)
block.cross_attn.norm_k.requires_grad_(False)
reference = teacher_model.patch_embedding.weight
self.feature_fusion.to(device=reference.device, dtype=reference.dtype)
self.residual_out.to(device=reference.device, dtype=reference.dtype)
nn.init.zeros_(self.residual_out.weight)
nn.init.zeros_(self.residual_out.bias)
# Match CausalWanModel: RoPE frequencies are runtime state rather than a
# persistent buffer, so compact checkpoints contain no derived table.
self._freqs = teacher_model.freqs.detach().clone()
@staticmethod
def _unwrap_teacher(model: Any) -> CausalWanModel:
current = model
visited: set[int] = set()
while id(current) not in visited:
visited.add(id(current))
if isinstance(current, CausalWanModel):
return current
wrapped = getattr(current, "module", None)
if wrapped is not None:
current = wrapped
continue
nested = getattr(current, "model", None)
if nested is not None:
current = nested
continue
break
raise TypeError(
"teacher_model must be CausalWanModel (normally generator.model), "
f"got {type(model)!r}"
)
@classmethod
def from_teacher(
cls,
teacher_model: CausalWanModel,
*,
source_block_ids: tuple[int, int] = (1, 28),
spatial_grid: tuple[int, int] = (30, 52),
) -> "SelfForcingPredictorV4":
"""Initialize all copied/frozen/trainable weights from a loaded Teacher."""
return cls(
teacher_model,
source_block_ids=source_block_ids,
spatial_grid=spatial_grid,
)
@property
def config_dict(self) -> dict[str, Any]:
return self.predictor_config.to_dict()
@property
def source_block_ids(self) -> tuple[int, int]:
return self.predictor_config.source_block_ids
def _freeze_teacher_modules(self) -> None:
for module in (
self.patch_embedding,
self.time_embedding,
self.time_projection,
self.head,
self.history_projectors,
):
module.requires_grad_(False)
module.eval()
@torch.no_grad()
def sync_frozen_from_teacher(
self,
teacher_model: CausalWanModel,
) -> None:
"""Refresh only the frozen Teacher-derived Predictor parameters.
Joint DMD changes the Full Generator after Predictor construction. A
compact Predictor checkpoint is reconstructed from that updated Full
model at inference time, so the frozen training-time copies must track
it as well. Predictor-owned trainable blocks/fusion are never
overwritten here.
"""
teacher_model = self._unwrap_teacher(teacher_model)
for destination, source in (
(self.patch_embedding, teacher_model.patch_embedding),
(self.time_embedding, teacher_model.time_embedding),
(self.time_projection, teacher_model.time_projection),
(self.head, teacher_model.head),
):
destination.load_state_dict(source.state_dict(), strict=True)
for position, source_id in enumerate(self.source_block_ids):
teacher_block = teacher_model.blocks[source_id]
while hasattr(teacher_block, "module"):
teacher_block = teacher_block.module
history = self.history_projectors[str(source_id)]
history.k.load_state_dict(
teacher_block.self_attn.k.state_dict(), strict=True
)
history.v.load_state_dict(
teacher_block.self_attn.v.state_dict(), strict=True
)
history.norm_k.load_state_dict(
teacher_block.self_attn.norm_k.state_dict(), strict=True
)
predictor_cross = self.predictor_blocks[position].cross_attn
teacher_cross = teacher_block.cross_attn
predictor_cross.k.load_state_dict(
teacher_cross.k.state_dict(), strict=True
)
predictor_cross.v.load_state_dict(
teacher_cross.v.state_dict(), strict=True
)
predictor_cross.norm_k.load_state_dict(
teacher_cross.norm_k.state_dict(), strict=True
)
self._freeze_teacher_modules()
for block in self.predictor_blocks:
block.cross_attn.k.requires_grad_(False)
block.cross_attn.v.requires_grad_(False)
block.cross_attn.norm_k.requires_grad_(False)
def train(self, mode: bool = True) -> "SelfForcingPredictorV4":
super().train(mode)
# Frozen layers have no stochastic operations today, but pinning their
# mode makes the intended boundary robust to future Wan changes.
for module in (
self.patch_embedding,
self.time_embedding,
self.time_projection,
self.head,
self.history_projectors,
):
module.eval()
return self
def _runtime_freqs(self, device: torch.device) -> torch.Tensor:
if self._freqs.device != device:
self._freqs = self._freqs.to(device)
return self._freqs
@staticmethod
def _scalar_int(value: int | torch.Tensor, name: str) -> int:
if torch.is_tensor(value):
if value.numel() != 1:
raise ValueError(f"{name} must be scalar, got shape {tuple(value.shape)}")
value = value.detach().item()
result = int(value)
if result < 0:
raise ValueError(f"{name} must be non-negative, got {result}")
return result
@staticmethod
def _start_values(
value: int | torch.Tensor,
*,
batch: int,
name: str,
) -> list[int]:
if torch.is_tensor(value):
values = [int(item) for item in value.detach().reshape(-1).cpu().tolist()]
else:
values = [int(value)]
if len(values) == 1:
values *= batch
if len(values) != batch:
raise ValueError(f"{name} has {len(values)} values for batch {batch}")
if any(item < 0 for item in values):
raise ValueError(f"{name} must contain non-negative frame indices")
return values
def _rope_history_key(
self,
key: torch.Tensor,
*,
start_frames: int | torch.Tensor,
) -> torch.Tensor:
cfg = self.predictor_config
batch, tokens = key.shape[:2]
if tokens % cfg.tokens_per_frame:
raise ValueError(
f"History tokens {tokens} are not divisible by "
f"{cfg.tokens_per_frame} tokens/frame"
)
frames = tokens // cfg.tokens_per_frame
starts = self._start_values(start_frames, batch=batch, name="start_frames")
freqs = self._runtime_freqs(key.device)
grid = torch.tensor(
[[frames, *cfg.spatial_grid]],
dtype=torch.long,
device=key.device,
)
if len(set(starts)) == 1:
return causal_rope_apply(
key,
grid.expand(batch, -1),
freqs,
start_frame=starts[0],
)
return torch.cat(
[
causal_rope_apply(
key[index : index + 1],
grid,
freqs,
start_frame=start,
)
for index, start in enumerate(starts)
],
dim=0,
)
def _project_history_part(
self,
block_id: int,
prefeature: torch.Tensor,
*,
start_frames: int | torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor]:
cfg = self.predictor_config
if prefeature.ndim != 3 or prefeature.shape[-1] != cfg.dim:
raise ValueError(
f"Block {block_id} prefeature must be [B, S, {cfg.dim}], got "
f"{tuple(prefeature.shape)}"
)
projector = self.history_projectors[str(block_id)]
projector_device = projector.k.weight.device
if prefeature.device != projector_device:
raise ValueError(
f"Block {block_id} prefeature is on {prefeature.device}, "
f"projector is on {projector_device}"
)
prefeature = prefeature.to(dtype=projector.k.weight.dtype)
# Clean-history tensors are fixed offline Teacher data. Avoid retaining
# a graph through several GiB of reconstructed cache.
with torch.no_grad():
key, value = projector(prefeature, num_heads=cfg.num_heads)
key = self._rope_history_key(key, start_frames=start_frames)
return key, value
def build_history_kv_cache(
self,
clean_prefeature_by_block: Mapping[
int | str, torch.Tensor | Sequence[torch.Tensor]
],
*,
current_start: int | torch.Tensor,
current_tokens: int | torch.Tensor,
start_frames: int | torch.Tensor | Sequence[int | torch.Tensor] = 0,
cache_capacity: int | None = None,
) -> dict[int, dict[str, torch.Tensor]]:
"""Rebuild selected-layer clean-history caches for Predictor training.
``clean_prefeature_by_block`` may contain one already-concatenated
``[B, S, D]`` tensor per block, or a sequence of chunk tensors. For a
sequence, ``start_frames`` can be the matching sequence ``0, 3, ...``.
``current_start`` is the global token offset used by Wan inference and
``current_tokens`` reserves the writable current-chunk cache region.
"""
current_start_int = self._scalar_int(current_start, "current_start")
current_tokens_int = self._scalar_int(current_tokens, "current_tokens")
if current_tokens_int == 0:
raise ValueError("current_tokens must be positive")
cfg = self.predictor_config
result: dict[int, dict[str, torch.Tensor]] = {}
expected_batch: int | None = None
expected_history_tokens: int | None = None
for block_id in self.source_block_ids:
value = clean_prefeature_by_block.get(block_id)
if value is None:
value = clean_prefeature_by_block.get(str(block_id))
if value is None:
raise ValueError(f"Missing clean prefeature for block {block_id}")
if torch.is_tensor(value):
if isinstance(start_frames, Sequence) and not torch.is_tensor(start_frames):
if len(start_frames) != 1:
raise ValueError(
"Already-concatenated prefeatures require one start_frames value"
)
part_start = start_frames[0]
else:
part_start = start_frames
keys, values = self._project_history_part(
block_id, value, start_frames=part_start
)
else:
parts = list(value)
if not parts:
raise ValueError(f"Block {block_id} has no history prefeatures")
if isinstance(start_frames, Sequence) and not torch.is_tensor(start_frames):
starts = list(start_frames)
if len(starts) != len(parts):
raise ValueError(
f"start_frames has {len(starts)} entries for "
f"{len(parts)} history chunks"
)
else:
starts = []
next_start: int | torch.Tensor = start_frames
for part in parts:
starts.append(next_start)
if torch.is_tensor(next_start) and next_start.numel() > 1:
next_start = next_start + (
part.shape[1] // self.predictor_config.tokens_per_frame
)
else:
next_start = self._scalar_int(next_start, "start_frames") + (
part.shape[1] // self.predictor_config.tokens_per_frame
)
projected = [
self._project_history_part(
block_id, part, start_frames=part_start
)
for part, part_start in zip(parts, starts)
]
keys = torch.cat([item[0] for item in projected], dim=1)
values = torch.cat([item[1] for item in projected], dim=1)
batch, history_tokens = keys.shape[:2]
if expected_batch is None:
expected_batch = batch
expected_history_tokens = history_tokens
elif batch != expected_batch or history_tokens != expected_history_tokens:
raise ValueError(
"Selected blocks must have the same history shape, got "
f"block {block_id}: batch={batch}, tokens={history_tokens}; "
f"expected batch={expected_batch}, tokens={expected_history_tokens}"
)
if history_tokens > current_start_int:
raise ValueError(
f"History has {history_tokens} tokens but current_start is "
f"{current_start_int}"
)
required_capacity = history_tokens + current_tokens_int
capacity = required_capacity if cache_capacity is None else int(cache_capacity)
if capacity < required_capacity:
raise ValueError(
f"cache_capacity {capacity} is smaller than required "
f"{required_capacity}"
)
cache_k = keys.new_zeros(
batch, capacity, cfg.num_heads, cfg.dim // cfg.num_heads
)
cache_v = values.new_zeros(
batch, capacity, cfg.num_heads, cfg.dim // cfg.num_heads
)
cache_k[:, :history_tokens].copy_(keys)
cache_v[:, :history_tokens].copy_(values)
result[block_id] = {
"k": cache_k,
"v": cache_v,
"global_end_index": torch.tensor(
[current_start_int], dtype=torch.long, device=keys.device
),
"local_end_index": torch.tensor(
[history_tokens], dtype=torch.long, device=keys.device
),
}
return result
def _select_cache(
self,
caches: Mapping[Any, Any] | Sequence[Any],
*,
source_id: int,
source_position: int,
name: str,
) -> Mapping[str, Any]:
if isinstance(caches, Mapping):
selected = caches.get(source_id)
if selected is None:
selected = caches.get(str(source_id))
else:
if len(caches) == self.predictor_config.num_layers:
selected = caches[source_id]
elif len(caches) == len(self.source_block_ids):
selected = caches[source_position]
else:
selected = None
if selected is None:
raise ValueError(f"{name} is missing source block {source_id}")
if not isinstance(selected, Mapping):
raise TypeError(f"{name}[{source_id}] must be a mapping")
return selected
def _selected_crossattn_cache(
self,
caches: Mapping[Any, Any] | Sequence[Any],
*,
source_id: int,
source_position: int,
) -> dict[str, Any]:
selected = self._select_cache(
caches,
source_id=source_id,
source_position=source_position,
name="crossattn_cache",
)
missing = {"k", "v"}.difference(selected)
if missing:
raise ValueError(
f"crossattn_cache block {source_id} is missing {sorted(missing)}"
)
if selected["k"].shape != selected["v"].shape:
raise ValueError(f"crossattn_cache block {source_id} K/V shape mismatch")
# Offline files contain K/V but need not serialize the runtime flag.
# A shallow wrapper avoids mutating the generator-owned dictionary.
return {**selected, "is_init": True}
def _time_condition(
self,
target_timestep: torch.Tensor,
reference: torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor]:
cfg = self.predictor_config
time_embedding = self.time_embedding(
sinusoidal_embedding_1d(
cfg.freq_dim, target_timestep.flatten()
).type_as(reference)
)
block_condition = self.time_projection(time_embedding).unflatten(
1, (6, cfg.dim)
).unflatten(0, target_timestep.shape)
head_condition = time_embedding.unflatten(
0, target_timestep.shape
).unsqueeze(2)
return block_condition, head_condition
def _unpatchify(
self,
tokens: torch.Tensor,
grid_sizes: torch.Tensor,
) -> torch.Tensor:
cfg = self.predictor_config
outputs = []
for sample, grid in zip(tokens, grid_sizes.tolist()):
sample = sample[: math.prod(grid)].view(
*grid, *cfg.patch_size, cfg.out_dim
)
# [f, h, w, p, q, r, c] -> [f, p, c, h, q, w, r] so the
# following reshape merges each grid axis with its patch axis.
sample = torch.einsum("fhwpqrc->fpchqwr", sample)
outputs.append(
sample.reshape(
grid[0] * cfg.patch_size[0],
cfg.out_dim,
grid[1] * cfg.patch_size[1],
grid[2] * cfg.patch_size[2],
)
)
return torch.stack(outputs)
def forward(
self,
*,
target_latent: torch.Tensor,
target_timestep: torch.Tensor,
anchor_hidden: torch.Tensor,
previous_chunk_hidden: torch.Tensor,
kv_cache: Mapping[Any, Any] | Sequence[Any],
crossattn_cache: Mapping[Any, Any] | Sequence[Any],
current_start: int | torch.Tensor,
) -> dict[str, torch.Tensor]:
"""Predict one skipped denoising step.
Args:
target_latent: Noisy target chunk in ``[B, F, C, H, W]`` layout.
target_timestep: Per-frame timestep tensor ``[B, F]``.
anchor_hidden: Same-chunk preceding-step final hidden ``[B, L, D]``.
previous_chunk_hidden: Previous-chunk same-step hidden ``[B, L, D]``.
kv_cache: Full 30-layer list or selected-layer mapping/list.
crossattn_cache: Full list or selected cached text K/V.
current_start: Current chunk's global token offset.
"""
cfg = self.predictor_config
if target_latent.ndim != 5:
raise ValueError(
f"target_latent must be [B, F, C, H, W], got {tuple(target_latent.shape)}"
)
batch, frames, channels, height, width = target_latent.shape
if channels != cfg.in_dim:
raise ValueError(f"target_latent channels {channels} != {cfg.in_dim}")
expected_timestep = (batch, frames // cfg.patch_size[0])
if tuple(target_timestep.shape) != expected_timestep:
raise ValueError(
f"target_timestep shape {tuple(target_timestep.shape)} != "
f"{expected_timestep}"
)
if target_timestep.device != target_latent.device:
raise ValueError("target_timestep and target_latent must share a device")
if target_latent.device != self.patch_embedding.weight.device:
raise ValueError(
f"target_latent is on {target_latent.device}, Predictor is on "
f"{self.patch_embedding.weight.device}"
)
latent_cf = target_latent.permute(0, 2, 1, 3, 4).to(
dtype=self.patch_embedding.weight.dtype
)
# Do not wrap frozen patch/time/head modules in no_grad: recursive
# rollout losses must still backpropagate to an earlier target latent.
current = self.patch_embedding(latent_cf)
grid_sizes = torch.tensor(
[current.shape[2:]] * batch,
dtype=torch.long,
device=current.device,
)
current = current.flatten(2).transpose(1, 2)
expected_hidden = (batch, current.shape[1], cfg.dim)
if tuple(anchor_hidden.shape) != expected_hidden:
raise ValueError(
f"anchor_hidden shape {tuple(anchor_hidden.shape)} != {expected_hidden}"
)
if tuple(previous_chunk_hidden.shape) != expected_hidden:
raise ValueError(
"previous_chunk_hidden shape "
f"{tuple(previous_chunk_hidden.shape)} != {expected_hidden}"
)
current = current.to(dtype=anchor_hidden.dtype)
hidden = self.feature_fusion(
current, anchor_hidden, previous_chunk_hidden
)
block_condition, head_condition = self._time_condition(
target_timestep, current
)
seq_lens = torch.full(
(batch,), current.shape[1], dtype=torch.long, device=current.device
)
current_start_int = self._scalar_int(current_start, "current_start")
freqs = self._runtime_freqs(current.device)
for position, (source_id, block) in enumerate(
zip(self.source_block_ids, self.predictor_blocks)
):
selected_kv = self._select_cache(
kv_cache,
source_id=source_id,
source_position=position,
name="kv_cache",
)
selected_cross = self._selected_crossattn_cache(
crossattn_cache,
source_id=source_id,
source_position=position,
)
hidden = block(
hidden,
e=block_condition,
seq_lens=seq_lens,
grid_sizes=grid_sizes,
freqs=freqs,
context=None,
context_lens=None,
block_mask=None,
kv_cache=selected_kv,
crossattn_cache=selected_cross,
current_start=current_start_int,
cache_start=current_start_int,
)
delta_hidden = self.residual_out(hidden)
pred_hidden = anchor_hidden + delta_hidden
pred_tokens = self.head(pred_hidden, head_condition)
pred_flow = self._unpatchify(pred_tokens, grid_sizes)
return {
"pred_hidden": pred_hidden,
"pred_flow": pred_flow,
"delta_hidden": delta_hidden,
}
def trainable_parameter_count(self) -> int:
return sum(
parameter.numel()
for parameter in self.parameters()
if parameter.requires_grad
)
def trainable_parameter_breakdown(self) -> dict[str, int]:
modules = {
"feature_fusion": self.feature_fusion,
"predictor_blocks": self.predictor_blocks,
"residual_out": self.residual_out,
}
return {
name: sum(
parameter.numel()
for parameter in module.parameters()
if parameter.requires_grad
)
for name, module in modules.items()
}
def trainable_state_dict(
self,
*,
keep_vars: bool = False,
) -> OrderedDict[str, torch.Tensor]:
"""Return only optimizer-owned parameters, suitable for safetensors."""
trainable = {
name for name, parameter in self.named_parameters()
if parameter.requires_grad
}
state = super().state_dict(keep_vars=keep_vars)
return OrderedDict(
(name, value) for name, value in state.items() if name in trainable
)
def load_trainable_state_dict(
self,
state_dict: Mapping[str, torch.Tensor],
*,
strict: bool = True,
) -> None:
"""Load a compact state into a fresh Predictor initialized from Teacher."""
expected = set(self.trainable_state_dict())
received = set(state_dict)
if strict:
missing = sorted(expected.difference(received))
unexpected = sorted(received.difference(expected))
if missing or unexpected:
raise RuntimeError(
"Predictor trainable checkpoint mismatch: "
f"missing={missing}, unexpected={unexpected}"
)
filtered = {
name: tensor for name, tensor in state_dict.items() if name in expected
}
self.load_state_dict(filtered, strict=False)
def checkpoint_dict(self) -> dict[str, Any]:
"""Build a compact torch-save payload with architecture metadata."""
return {
"format": "self_forcing_wan_predictor_v4",
"format_version": self.checkpoint_format_version,
"config": self.config_dict,
"trainable_state_dict": self.trainable_state_dict(),
}
def load_checkpoint_dict(
self,
checkpoint: Mapping[str, Any],
*,
strict: bool = True,
) -> None:
if checkpoint.get("format") != "self_forcing_wan_predictor_v4":
raise ValueError(f"Unsupported Predictor checkpoint: {checkpoint.get('format')}")
saved_config = dict(checkpoint.get("config", {}))
if strict and saved_config != self.config_dict:
raise ValueError(
"Predictor checkpoint config does not match the Teacher/config "
f"used for reconstruction: saved={saved_config}, current={self.config_dict}"
)
state = checkpoint.get("trainable_state_dict")
if not isinstance(state, Mapping):
raise ValueError("Predictor checkpoint has no trainable_state_dict")
self.load_trainable_state_dict(state, strict=strict)
__all__ = [
"SelfForcingPredictorV4",
"TripleFeatureFusion",
"WanPredictorV4Config",
]
|