File size: 12,248 Bytes
7ccb33d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Symmetric round-to-nearest fake quantization for linear and embedding weights."""

from __future__ import annotations

from typing import Any
import weakref

import torch
import torch.nn as nn
import torch.nn.functional as F


def _validate_rtn_args(
    weight: torch.Tensor,
    bits: int,
    granularity: str,
    group_size: int,
) -> None:
    if weight.ndim != 2:
        raise ValueError(f"RTN only supports 2-D weights, got {weight.ndim}-D")
    if not weight.is_floating_point():
        raise ValueError("RTN fake quantization requires a floating-point weight")
    if bits not in {3, 4, 8}:
        raise ValueError("bits must be 3, 4, or 8")
    if granularity not in {"per_channel", "per_group"}:
        raise ValueError("granularity must be 'per_channel' or 'per_group'")
    if not isinstance(group_size, int) or group_size <= 0:
        raise ValueError("group_size must be a positive integer")


def _rtn_groups(
    weight: torch.Tensor,
    granularity: str,
    group_size: int,
) -> tuple[torch.Tensor, int]:
    """Return padded row-local groups and the original column count."""

    rows, columns = weight.shape
    actual_group_size = columns if granularity == "per_channel" else group_size
    number_of_groups = (columns + actual_group_size - 1) // actual_group_size
    padded_columns = number_of_groups * actual_group_size
    if padded_columns != columns:
        weight = F.pad(weight, (0, padded_columns - columns))
    return weight.reshape(rows, number_of_groups, actual_group_size), columns


def _quantize_groups(
    groups: torch.Tensor,
    bits: int,
) -> tuple[torch.Tensor, torch.Tensor]:
    """Quantize groups in fp32 with deployment-canonical fp16 scales."""

    qmax = 2 ** (bits - 1) - 1
    scales = groups.float().abs().amax(dim=-1, keepdim=True) / qmax
    # The physical RTN format stores one fp16 scale per group.  Canonicalizing
    # here makes QAT/inference consume exactly the scale precision that export
    # can preserve, including when the latent master is fp32.
    scales = scales.to(torch.float16).float()
    safe_scales = torch.where(scales == 0, torch.ones_like(scales), scales)
    integers = torch.round(groups.float() / safe_scales).clamp(-qmax, qmax)
    return integers, scales


class _RTNQuantizeSTE(torch.autograd.Function):
    """Autograd implementation with the same pure identity STE as Sherry."""

    @staticmethod
    def forward(
        ctx: Any,
        weight: torch.Tensor,
        bits: int,
        granularity: str,
        group_size: int,
    ) -> torch.Tensor:
        del ctx
        _validate_rtn_args(weight, bits, granularity, group_size)
        groups, columns = _rtn_groups(weight, granularity, group_size)
        integers, scales = _quantize_groups(groups, bits)
        quantized = (integers * scales).reshape(weight.shape[0], -1)[:, :columns]
        return quantized.to(weight.dtype)

    @staticmethod
    def backward(
        ctx: Any,
        grad_output: torch.Tensor,
    ) -> tuple[torch.Tensor, None, None, None]:
        del ctx
        return grad_output, None, None, None


def rtn_quantize(
    weight: torch.Tensor,
    bits: int = 8,
    granularity: str = "per_channel",
    group_size: int = 128,
) -> torch.Tensor:
    """Apply symmetric absmax RTN fake quantization with an identity STE.

    ``per_channel`` assigns one scale to every row (the output channel for a
    linear weight, or one embedding vector).  ``per_group`` divides each row
    along its last dimension and permits a shorter final group.  The signed
    integer grid is ``[-127, 127]`` for W8, ``[-7, 7]`` for W4, or
    ``[-3, 3]`` for W3.
    """

    return _RTNQuantizeSTE.apply(weight, bits, granularity, group_size)


class RTNLinear(nn.Linear):
    """``nn.Linear`` with an fp32 master weight and RTN fake-quant forward."""

    def __init__(
        self,
        in_features: int,
        out_features: int,
        bias: bool = True,
        *,
        bits: int = 8,
        granularity: str = "per_channel",
        group_size: int = 128,
        device: torch.device | str | None = None,
    ) -> None:
        # Validate configuration without imposing a divisibility requirement.
        _validate_rtn_args(
            torch.empty(out_features, in_features), bits, granularity, group_size
        )
        super().__init__(
            in_features,
            out_features,
            bias=bias,
            device=device,
            dtype=torch.float32,
        )
        self.bits = bits
        self.granularity = granularity
        self.group_size = group_size

    @classmethod
    def from_linear(
        cls,
        linear: nn.Linear,
        *,
        bits: int = 8,
        granularity: str = "per_channel",
        group_size: int = 128,
    ) -> "RTNLinear":
        converted = cls(
            linear.in_features,
            linear.out_features,
            bias=linear.bias is not None,
            bits=bits,
            granularity=granularity,
            group_size=group_size,
            device=linear.weight.device,
        )
        with torch.no_grad():
            converted.weight.copy_(linear.weight.detach().float())
            if linear.bias is not None and converted.bias is not None:
                converted.bias.copy_(linear.bias.detach().float())
        converted.weight.requires_grad_(linear.weight.requires_grad)
        if linear.bias is not None and converted.bias is not None:
            converted.bias.requires_grad_(linear.bias.requires_grad)
        converted.train(linear.training)
        return converted

    def forward(self, input: torch.Tensor) -> torch.Tensor:
        quantized_weight = rtn_quantize(
            self.weight,
            bits=self.bits,
            granularity=self.granularity,
            group_size=self.group_size,
        ).to(input.dtype)
        bias = self.bias.to(input.dtype) if self.bias is not None else None
        return F.linear(input, quantized_weight, bias)


class RTNEmbedding(nn.Embedding):
    """Embedding with an fp32 master and configurable RTN fake quantization.

    A forward-local cache lets a tied output head consume the exact same
    fake-quant tensor used by the lookup.  This is important for a tied
    embedding/head matrix: merely tying the fp32 master parameters would still
    permit the two call sites to fake-quantize independently.
    """

    def __init__(
        self,
        num_embeddings: int,
        embedding_dim: int,
        padding_idx: int | None = None,
        max_norm: float | None = None,
        norm_type: float = 2.0,
        scale_grad_by_freq: bool = False,
        sparse: bool = False,
        *,
        bits: int = 8,
        granularity: str = "per_channel",
        group_size: int = 128,
        compute_dtype: torch.dtype = torch.float32,
        device: torch.device | str | None = None,
    ) -> None:
        _validate_rtn_args(
            torch.empty(num_embeddings, embedding_dim), bits, granularity, group_size
        )
        super().__init__(
            num_embeddings,
            embedding_dim,
            padding_idx=padding_idx,
            max_norm=max_norm,
            norm_type=norm_type,
            scale_grad_by_freq=scale_grad_by_freq,
            sparse=sparse,
            device=device,
            dtype=torch.float32,
        )
        self.bits = bits
        self.granularity = granularity
        self.group_size = group_size
        self.compute_dtype = compute_dtype
        object.__setattr__(self, "_forward_quantized_weight", None)
        object.__setattr__(self, "_eval_quantized_weight", None)

    @classmethod
    def from_embedding(
        cls,
        embedding: nn.Embedding,
        *,
        bits: int = 8,
        granularity: str = "per_channel",
        group_size: int = 128,
    ) -> "RTNEmbedding":
        converted = cls(
            embedding.num_embeddings,
            embedding.embedding_dim,
            padding_idx=embedding.padding_idx,
            max_norm=embedding.max_norm,
            norm_type=embedding.norm_type,
            scale_grad_by_freq=embedding.scale_grad_by_freq,
            sparse=embedding.sparse,
            bits=bits,
            granularity=granularity,
            group_size=group_size,
            compute_dtype=embedding.weight.dtype,
            device=embedding.weight.device,
        )
        with torch.no_grad():
            converted.weight.copy_(embedding.weight.detach().float())
        converted.weight.requires_grad_(embedding.weight.requires_grad)
        converted.train(embedding.training)
        return converted

    def _make_quantized_weight(self) -> torch.Tensor:
        return rtn_quantize(
            self.weight,
            bits=self.bits,
            granularity=self.granularity,
            group_size=self.group_size,
        ).to(self.compute_dtype)

    def cache_eval_weight(self, quantized_weight: torch.Tensor | None) -> None:
        """Install/remove a detached persistent fake-quant matrix for eval."""

        object.__setattr__(self, "_eval_quantized_weight", quantized_weight)
        object.__setattr__(self, "_forward_quantized_weight", None)

    def _lookup_quantized_weight(self) -> torch.Tensor:
        quantized_weight = self._eval_quantized_weight
        if quantized_weight is None:
            quantized_weight = self._make_quantized_weight()
        object.__setattr__(self, "_forward_quantized_weight", quantized_weight)
        return quantized_weight

    def take_tied_quantized_weight(self) -> torch.Tensor:
        """Consume the precise tensor produced by the preceding lookup."""

        quantized_weight = self._forward_quantized_weight
        if quantized_weight is None:
            raise RuntimeError(
                "tied RTN lm_head ran without a preceding embedding lookup; "
                "the shared fake-quant invariant cannot be guaranteed"
            )
        object.__setattr__(self, "_forward_quantized_weight", None)
        return quantized_weight

    def forward(self, input: torch.Tensor) -> torch.Tensor:
        quantized_weight = self._lookup_quantized_weight()
        return F.embedding(
            input,
            quantized_weight,
            self.padding_idx,
            self.max_norm,
            self.norm_type,
            self.scale_grad_by_freq,
            self.sparse,
        )


class TiedRTNLMHead(nn.Module):
    """Bias-free projection tied to an :class:`RTNEmbedding` master and cache."""

    def __init__(self, embedding: RTNEmbedding) -> None:
        super().__init__()
        self.in_features = embedding.embedding_dim
        self.out_features = embedding.num_embeddings
        # Register the same Parameter at the conventional checkpoint key while
        # keeping the module reference weak/non-registered (no module cycle).
        self.weight = embedding.weight
        object.__setattr__(self, "_embedding_ref", weakref.ref(embedding))

    @property
    def embedding(self) -> RTNEmbedding:
        embedding = self._embedding_ref()
        if embedding is None:
            raise RuntimeError("tied RTN embedding no longer exists")
        return embedding

    def forward(self, input: torch.Tensor) -> torch.Tensor:
        embedding = self.embedding
        if self.weight is not embedding.weight:
            raise RuntimeError("RTN embedding/lm_head fp32 master tie was broken")
        quantized_weight = embedding.take_tied_quantized_weight()
        return F.linear(input, quantized_weight, None)


def tie_rtn_lm_head(model: nn.Module) -> tuple[RTNEmbedding, TiedRTNLMHead]:
    """Reconnect MOSS input/output weights through one shared fake-quant path."""

    embedding = model.get_input_embeddings()
    if not isinstance(embedding, RTNEmbedding):
        raise TypeError(f"expected RTNEmbedding input, got {type(embedding).__name__}")
    head = TiedRTNLMHead(embedding)
    model.set_output_embeddings(head)
    if model.get_output_embeddings() is not head:
        raise RuntimeError("model rejected the tied RTN output head")
    if head.weight is not embedding.weight or head.weight.data_ptr() != embedding.weight.data_ptr():
        raise RuntimeError("embedding/lm_head fp32 master tie verification failed")
    return embedding, head