File size: 11,167 Bytes
ae8ade0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""One-block feature Predictor used by the initialization sweep."""

from __future__ import annotations

import copy
from typing import Literal

import torch
from torch import nn
from torch.utils.checkpoint import checkpoint

from wan.modules.causal_model import CausalWanAttentionBlock


InitializationMethod = Literal[
    "teacher_full",
    "random_full",
    "teacher_identity",
    "random_identity",
    "full_zero",
]
GateMode = Literal["baseline", "learned", "constant"]


class PreviousFeatureGate(nn.Module):
    """Per-token scalar reliability gate for the normalized previous feature."""

    def __init__(
        self,
        dim: int,
        hidden_dim: int = 128,
        initial_bias: float = 4.6,
    ) -> None:
        super().__init__()
        self.net = nn.Sequential(
            nn.Linear(2 * dim, hidden_dim),
            nn.SiLU(),
            nn.Linear(hidden_dim, 1),
        )
        nn.init.zeros_(self.net[-1].weight)
        nn.init.constant_(self.net[-1].bias, initial_bias)

    def forward(
        self, anchor_normalized: torch.Tensor, previous_normalized: torch.Tensor
    ) -> torch.Tensor:
        return torch.sigmoid(
            self.net(torch.cat([anchor_normalized, previous_normalized], dim=-1))
        )


class TripleFeatureFusion(nn.Module):
    """Fuse target latent tokens, anchor hidden, and previous-chunk hidden."""

    def __init__(
        self,
        dim: int = 1536,
        gate_mode: GateMode = "baseline",
        gate_hidden_dim: int = 128,
        gate_initial_bias: float = 4.6,
        gate_floor: float = 0.0,
        constant_gate: float = 1.0,
    ) -> None:
        super().__init__()
        if gate_mode not in {"baseline", "learned", "constant"}:
            raise ValueError(f"Unknown gate mode: {gate_mode}")
        if not 0.0 <= constant_gate <= 1.0:
            raise ValueError("constant_gate must be in [0, 1]")
        if not 0.0 <= gate_floor < 1.0:
            raise ValueError("gate_floor must be in [0, 1)")
        self.current_norm = nn.LayerNorm(dim, eps=1e-6)
        self.anchor_norm = nn.LayerNorm(dim, eps=1e-6)
        self.previous_norm = nn.LayerNorm(dim, eps=1e-6)
        self.gate_mode = gate_mode
        self.constant_gate = float(constant_gate)
        self.gate_floor = float(gate_floor)
        self.gate_override: float | None = None
        self.gate = (
            PreviousFeatureGate(dim, gate_hidden_dim, gate_initial_bias)
            if gate_mode == "learned"
            else None
        )
        self.last_gate: torch.Tensor | None = None
        self.proj_in = nn.Linear(3 * dim, 2 * dim)
        self.activation = nn.SiLU()
        self.proj_out = 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 anchor.shape != previous.shape:
            raise ValueError(
                "TripleFeatureFusion inputs must have identical shapes: "
                f"{current.shape}, {anchor.shape}, {previous.shape}"
            )
        anchor_normalized = self.anchor_norm(anchor)
        previous_normalized = self.previous_norm(previous)
        if self.gate_override is not None:
            gate = previous_normalized.new_full(
                (*previous_normalized.shape[:-1], 1), self.gate_override
            )
        elif self.gate_mode == "learned":
            assert self.gate is not None
            raw_gate = self.gate(anchor_normalized, previous_normalized)
            gate = self.gate_floor + (1.0 - self.gate_floor) * raw_gate
        elif self.gate_mode == "constant":
            gate = previous_normalized.new_full(
                (*previous_normalized.shape[:-1], 1), self.constant_gate
            )
        else:
            gate = previous_normalized.new_ones(
                *previous_normalized.shape[:-1], 1
            )
        # Gate after normalization. A positive per-token scalar applied before
        # LayerNorm would be normalized away and could not test reliability.
        self.last_gate = gate.detach()
        return self.proj_out(
            self.activation(
                self.proj_in(
                    torch.cat(
                        [
                            self.current_norm(current),
                            anchor_normalized,
                            gate * previous_normalized,
                        ],
                        dim=-1,
                    )
                )
            )
        )


def _new_random_block(
    teacher_block: CausalWanAttentionBlock,
) -> CausalWanAttentionBlock:
    block = CausalWanAttentionBlock(
        cross_attn_type="t2v_cross_attn",
        dim=teacher_block.dim,
        ffn_dim=teacher_block.ffn_dim,
        num_heads=teacher_block.num_heads,
        local_attn_size=teacher_block.local_attn_size,
        sink_size=teacher_block.self_attn.sink_size,
        qk_norm=teacher_block.qk_norm,
        cross_attn_norm=teacher_block.cross_attn_norm,
        eps=teacher_block.eps,
    )
    # This matches CausalWanModel.init_weights rather than PyTorch Linear's
    # Kaiming default.
    for module in block.modules():
        if isinstance(module, nn.Linear):
            nn.init.xavier_uniform_(module.weight)
            if module.bias is not None:
                nn.init.zeros_(module.bias)
    return block


def _zero_residual_branch_outputs(block: CausalWanAttentionBlock) -> None:
    for projection in (
        block.self_attn.o,
        block.cross_attn.o,
        block.ffn[2],
    ):
        nn.init.zeros_(projection.weight)
        if projection.bias is not None:
            nn.init.zeros_(projection.bias)


def initialize_predictor_block(
    teacher_block: CausalWanAttentionBlock,
    method: InitializationMethod,
) -> CausalWanAttentionBlock:
    """Create a trainable FP32 block under one controlled initialization."""
    if method in {"teacher_full", "teacher_identity", "full_zero"}:
        block = copy.deepcopy(teacher_block).float()
    elif method in {"random_full", "random_identity"}:
        block = _new_random_block(teacher_block).float()
    else:
        raise ValueError(f"Unknown initialization method: {method}")

    if method in {"teacher_identity", "random_identity"}:
        _zero_residual_branch_outputs(block)
    elif method == "full_zero":
        for parameter in block.parameters():
            nn.init.zeros_(parameter)
    return block


class SingleBlockPredictor(nn.Module):
    """Predict final hidden as an anchor residual through one causal Wan block."""

    def __init__(
        self,
        block: CausalWanAttentionBlock,
        dim: int = 1536,
        gradient_checkpointing: bool = True,
        gate_mode: GateMode = "baseline",
        gate_hidden_dim: int = 128,
        gate_initial_bias: float = 4.6,
        gate_floor: float = 0.0,
        constant_gate: float = 1.0,
    ) -> None:
        super().__init__()
        self.fusion = TripleFeatureFusion(
            dim,
            gate_mode=gate_mode,
            gate_hidden_dim=gate_hidden_dim,
            gate_initial_bias=gate_initial_bias,
            gate_floor=gate_floor,
            constant_gate=constant_gate,
        )
        self.block = block
        self.residual_out = nn.Linear(dim, dim)
        nn.init.zeros_(self.residual_out.weight)
        nn.init.zeros_(self.residual_out.bias)
        self.gradient_checkpointing = gradient_checkpointing

    def fusion_parameters(self) -> list[nn.Parameter]:
        return list(self.fusion.parameters()) + list(self.residual_out.parameters())

    def gate_parameters(self) -> list[nn.Parameter]:
        return list(self.fusion.gate.parameters()) if self.fusion.gate else []

    def fusion_parameters_without_gate(self) -> list[nn.Parameter]:
        gate_ids = {id(parameter) for parameter in self.gate_parameters()}
        return [
            parameter for parameter in self.fusion_parameters()
            if id(parameter) not in gate_ids
        ]

    def block_parameters(self) -> list[nn.Parameter]:
        return list(self.block.parameters())

    def set_block_trainable(self, enabled: bool) -> None:
        self.block.requires_grad_(enabled)

    def forward(
        self,
        *,
        current_tokens: torch.Tensor,
        anchor_hidden: torch.Tensor,
        previous_hidden: torch.Tensor,
        timestep_modulation: torch.Tensor,
        grid_sizes: torch.Tensor,
        freqs: torch.Tensor,
        history_k: torch.Tensor,
        history_v: torch.Tensor,
        cross_k: torch.Tensor,
        cross_v: torch.Tensor,
        current_start: int,
    ) -> torch.Tensor:
        fused = self.fusion(current_tokens, anchor_hidden, previous_hidden)
        batch, sequence, dim = fused.shape
        history_length = history_k.shape[1]
        if history_k.shape[0] != batch:
            raise ValueError(
                f"history_k {history_k.shape} incompatible with "
                f"batch={batch}"
            )
        if history_v.shape != history_k.shape:
            raise ValueError("History K/V shapes differ")

        seq_lens = torch.full(
            (batch,),
            sequence,
            dtype=torch.long,
            device="cpu",
        )
        context = fused.new_zeros(batch, 1, dim)

        def run_block(value: torch.Tensor) -> torch.Tensor:
            # A fresh cache is required for checkpoint recomputation because
            # CausalWanSelfAttention updates cache indices and current K/V.
            heads = history_k.shape[2]
            head_dim = history_k.shape[3]
            current_k = history_k.new_empty(batch, sequence, heads, head_dim)
            current_v = history_v.new_empty(batch, sequence, heads, head_dim)
            kv_cache = {
                "k": torch.cat([history_k, current_k], dim=1),
                "v": torch.cat([history_v, current_v], dim=1),
                "global_end_index": torch.tensor(
                    [current_start], device=value.device, dtype=torch.long
                ),
                "local_end_index": torch.tensor(
                    [history_length], device=value.device, dtype=torch.long
                ),
            }
            crossattn_cache = {
                "k": cross_k,
                "v": cross_v,
                "is_init": True,
            }
            return self.block(
                value,
                e=timestep_modulation,
                seq_lens=seq_lens,
                grid_sizes=grid_sizes,
                freqs=freqs,
                context=context,
                context_lens=None,
                block_mask=None,
                kv_cache=kv_cache,
                crossattn_cache=crossattn_cache,
                current_start=current_start,
                cache_start=None,
            )

        if self.training and self.gradient_checkpointing:
            transformed = checkpoint(
                run_block,
                fused,
                use_reentrant=False,
            )
        else:
            transformed = run_block(fused)
        return anchor_hidden + self.residual_out(transformed)