File size: 11,037 Bytes
ae080a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# SPDX-FileCopyrightText: © 2025 Tenstorrent USA, Inc.

# SPDX-License-Identifier: Apache-2.0

import torch

import ttnn

from .linear import ColParallelLinear, Linear, LoRAColParallelLinear, LoRARowParallelLinear, RowParallelLinear
from .module import Module

#: Bytes one row chunk of a pointwise intermediate may hold. Bounds a SwiGLU's 4x-wide hidden
#: activations by the chunk instead of the volume; the DiffVAE's upsample slabs by the same budget.
CHUNK_BYTES = 1 << 30


def _chunk_rows(width: int, *, dtype_bytes: int = 2) -> int:
    """Rows whose ``width``-wide intermediate fits :data:`CHUNK_BYTES`, tile-aligned."""
    tile = ttnn.TILE_SIZE
    rows = CHUNK_BYTES // (width * dtype_bytes)
    return max(tile, rows // tile * tile)


def pointwise_in_chunks(x: ttnn.Tensor, fn, *, width: int) -> ttnn.Tensor:
    """Apply the pointwise ``fn`` to row chunks of ``x`` along its second-to-last dim. **Consumes** ``x``.

    ``width`` is the widest intermediate ``fn`` builds per row. A single chunk runs whole so short
    inputs pay no concat. Leading dims are kept.
    """
    shape = list(x.shape)
    rows = shape[-2]
    step = _chunk_rows(width)
    if step >= rows:
        out = fn(x)
        ttnn.deallocate(x)
        return out

    parts = []
    for start in range(0, rows, step):
        starts = [0] * len(shape)
        stops = list(shape)
        starts[-2], stops[-2] = start, min(start + step, rows)
        chunk = ttnn.slice(x, starts, stops)
        parts.append(fn(chunk))
        ttnn.deallocate(chunk)
    ttnn.deallocate(x)
    joined = ttnn.concat(parts, dim=-2)
    for part in parts:
        ttnn.deallocate(part)
    return joined


class FeedForward(Module):
    """
    Linear layer with replicated weights
    """

    def __init__(
        self,
        dim: int,
        dim_out=None,
        mult: int = 4,
        activation_fn: str = "gelu",
        inner_dim=None,
        bias: bool = True,
        mesh_device=None,
    ):
        super().__init__()

        if inner_dim is None:
            inner_dim = int(dim * mult)
        dim_out = dim_out if dim_out is not None else dim
        self.mesh_device = mesh_device
        self.dim = dim
        self.dim_out = dim_out
        self.inner_dim = inner_dim
        self.activation_fn = activation_fn
        self.bias = bias

        self.ff1 = Linear(dim, inner_dim, bias=bias, mesh_device=mesh_device, activation_fn=activation_fn)
        self.ff2 = Linear(inner_dim, dim_out, bias=bias, mesh_device=mesh_device)

    def forward(self, x: ttnn.Tensor, compute_kernel_config=None) -> ttnn.Tensor:
        ff1_out = self.ff1(x, compute_kernel_config=compute_kernel_config)
        return self.ff2(ff1_out, compute_kernel_config=compute_kernel_config)


class ParallelFeedForward(Module):
    """
    Linear layer implementing megatron-style parallelism.
    """

    def __init__(
        self,
        dim: int,
        dim_out=None,
        mult: int = 4,
        activation_fn: str = "gelu",
        inner_dim=None,
        bias: bool = True,
        mesh_device=None,
        mesh_axis=0,
        fsdp_mesh_axis=None,
        ccl_manager=None,
        lora_enabled: bool = False,
        ff1_dtype=ttnn.bfloat16,
        ff2_dtype=ttnn.bfloat16,
        activation_dtype=None,
        pin_output_bf16=False,
    ):
        super().__init__()

        if inner_dim is None:
            inner_dim = int(dim * mult)
        dim_out = dim_out if dim_out is not None else dim
        self.mesh_device = mesh_device
        self.dim = dim
        self.dim_out = dim_out
        self.inner_dim = inner_dim
        self.activation_fn = activation_fn
        self.bias = bias
        self.mesh_axis = mesh_axis
        self.fsdp_mesh_axis = fsdp_mesh_axis

        if self.fsdp_mesh_axis is not None:
            assert self.mesh_axis != self.fsdp_mesh_axis

        ColCls = LoRAColParallelLinear if lora_enabled else ColParallelLinear
        RowCls = LoRARowParallelLinear if lora_enabled else RowParallelLinear

        # ff1 is the ColParallel projection whose input crosses the fabric, so it carries the
        # activation cast + output pin; ff2 (RowParallel) only takes a weight dtype.
        self.ff1 = ColCls(
            dim,
            inner_dim,
            bias=bias,
            dtype=ff1_dtype,
            mesh_device=mesh_device,
            activation_fn=activation_fn,
            mesh_axis=mesh_axis,
            fsdp_mesh_axis=fsdp_mesh_axis,
            ccl_manager=ccl_manager,
            activation_dtype=activation_dtype,
            pin_output_bf16=pin_output_bf16,
        )
        self.ff2 = RowCls(
            inner_dim,
            dim_out,
            bias=bias,
            dtype=ff2_dtype,
            mesh_device=mesh_device,
            mesh_axis=mesh_axis,
            fsdp_mesh_axis=fsdp_mesh_axis,
            ccl_manager=ccl_manager,
        )

    def forward(
        self,
        x: ttnn.Tensor,
        compute_kernel_config=None,
        parallel_config=None,
        default_block_size=None,
        force_transpose: bool = True,
        use_persistent_buffer: bool = True,
    ) -> ttnn.Tensor:
        """
        Expects x to be replicated.
        Return output fractured on columns.

        `default_block_size` and `force_transpose` are forwarded to ff1 only, for callers that have
        measured block sizes for their ff1 shape; ff2 keeps the generic path.
        """
        ff1_out = self.ff1(
            x,
            compute_kernel_config=compute_kernel_config,
            parallel_config=parallel_config,
            default_block_size=default_block_size,
            force_transpose=force_transpose,
        )
        return self.ff2(
            ff1_out, compute_kernel_config=compute_kernel_config, use_persistent_buffer=use_persistent_buffer
        )

    def forward_fused_addcmul(
        self,
        x: ttnn.Tensor,
        addcmul_a: ttnn.Tensor,
        addcmul_b: ttnn.Tensor,
        scalar: float = 1.0,
        compute_kernel_config=None,
        parallel_config=None,
        default_block_size=None,
        core_grid=None,
        force_transpose: bool = True,
    ) -> ttnn.Tensor:
        """Fused FFN forward with addcmul fused at the RS final write step.

        Computes: addcmul_a + scalar * ff2(ff1(x)) * addcmul_b
        Both addcmul_a and addcmul_b are already at their per-TP-device [D/tp] slice —
        no AllGather or scatter matmul is required.

        `default_block_size` and `force_transpose` are forwarded to ff1 only, as in `forward`.
        """
        ff1_out = self.ff1(
            x,
            compute_kernel_config=compute_kernel_config,
            parallel_config=parallel_config,
            default_block_size=default_block_size,
            core_grid=core_grid,
            force_transpose=force_transpose,
        )
        return self.ff2.forward_fused_addcmul(
            ff1_out,
            addcmul_a,
            addcmul_b,
            scalar=scalar,
            compute_kernel_config=compute_kernel_config,
        )


class SwiGLU(Module):
    """``w_down(silu(w_gate(x)) * w_up(x))``, biasless, as LTX-2.5 ships it.

    ``fused`` packs gate and up into one ``[up | gate]`` GEMM whose epilogue emits
    ``silu(gate) * up``; ``tp_mlp`` makes that GEMM column-parallel and ``w_down`` row-parallel
    over ``tp_axis``. The checkpoint's ``w_gate.weight`` / ``w_up.weight`` are packed at load.
    """

    def __init__(
        self,
        dim: int,
        hidden_dim: int,
        *,
        mesh_device=None,
        dtype: ttnn.DataType = ttnn.bfloat16,
        tp_axis=None,
        ccl_manager=None,
        fused: bool = False,
        tp_mlp: bool = False,
    ):
        super().__init__()
        self.hidden_dim = hidden_dim
        self.dim = dim
        self.mesh_device = mesh_device
        self.tp_axis = tp_axis
        self.ccl_manager = ccl_manager
        # tp_mlp: gate/up column-parallel and w_down row-parallel over tp_axis, on the packed weight.
        self.tp_mlp = tp_mlp and tp_axis is not None
        # fused: one [up | gate] GEMM whose epilogue emits silu(gate) * up.
        self.fused = fused or self.tp_mlp

        linear = {"bias": False, "mesh_device": mesh_device, "dtype": dtype}
        if self.tp_mlp:
            parallel = {"mesh_axis": tp_axis, "ccl_manager": ccl_manager}
            self.gate_up = ColParallelLinear(dim, hidden_dim, activation_fn="swiglu", **linear, **parallel)
            self.w_down = RowParallelLinear(hidden_dim, dim, **linear, **parallel)
        elif self.fused:
            self.gate_up = Linear(dim, hidden_dim, activation_fn="swiglu", **linear)
            self.w_down = Linear(hidden_dim, dim, **linear)
        else:
            self.w_gate = Linear(dim, hidden_dim, **linear)
            self.w_up = Linear(dim, hidden_dim, **linear)
            self.w_down = Linear(hidden_dim, dim, **linear)

    def _prepare_torch_state(self, state: dict[str, torch.Tensor]) -> None:
        """Pack the shipped ``w_gate``/``w_up`` into the fused ``[up | gate]`` weight.

        ``up`` first: ``prepare_for_fused_swiglu``'s default ordering is ``[up (N) | gate (N)]``.
        """
        if not self.fused:
            return
        gate = state.pop("w_gate.weight", None)
        up = state.pop("w_up.weight", None)
        if gate is not None and up is not None:
            state["gate_up.weight"] = torch.cat([up, gate], dim=0)

    def forward(self, x: ttnn.Tensor) -> ttnn.Tensor:
        """**Consumes** ``x``. Row-chunked: the hidden intermediates are 4x the activation."""
        width = self.hidden_dim // self.mlp_shards
        return pointwise_in_chunks(x, self._project, width=width)

    @property
    def mlp_shards(self) -> int:
        return int(list(self.mesh_device.shape)[self.tp_axis]) if self.tp_mlp else 1

    def _project(self, x: ttnn.Tensor) -> ttnn.Tensor:
        if self.fused:
            hidden = self.gate_up(x)
            if self.tp_mlp:
                # The all_gather below names dim 3 absolutely, so the tensor must be rank 4 here.
                hidden = ttnn.reshape(hidden, (1, 1, hidden.shape[-2], hidden.shape[-1]))
            # use_persistent_buffer=False: RowParallelLinear otherwise returns the CCL manager's
            # cached reduce-scatter buffer, which the deallocate below would destroy under it.
            out = self.w_down(hidden, use_persistent_buffer=False) if self.tp_mlp else self.w_down(hidden)
            ttnn.deallocate(hidden)
            if self.tp_mlp:
                gathered = self.ccl_manager.all_gather(out, dim=3, mesh_axis=self.tp_axis, use_hyperparams=False)
                ttnn.deallocate(out)
                out = ttnn.reshape(gathered, (gathered.shape[-2], self.dim))
            return out

        gate = ttnn.silu(self.w_gate(x))
        up = self.w_up(x)
        product = ttnn.multiply(gate, up)
        ttnn.deallocate(gate)
        ttnn.deallocate(up)
        out = self.w_down(product)
        ttnn.deallocate(product)
        return out