File size: 12,853 Bytes
501a7dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1eef4eb
 
 
 
64f89d6
1eef4eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501a7dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1eef4eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501a7dd
1eef4eb
 
 
 
 
 
 
 
501a7dd
 
 
1eef4eb
 
 
64f89d6
 
 
 
 
1eef4eb
 
501a7dd
1eef4eb
 
 
64f89d6
501a7dd
1eef4eb
 
 
 
 
 
 
 
 
 
 
 
501a7dd
 
 
 
 
 
 
 
 
 
 
 
 
1eef4eb
 
 
 
 
 
 
 
501a7dd
 
 
 
 
 
 
1eef4eb
 
 
 
 
 
 
 
 
 
 
 
 
64f89d6
501a7dd
1eef4eb
 
 
 
 
501a7dd
 
 
1eef4eb
501a7dd
64f89d6
 
 
 
501a7dd
64f89d6
 
 
1eef4eb
 
 
501a7dd
 
 
 
 
64f89d6
1eef4eb
 
 
 
 
501a7dd
 
 
 
 
1eef4eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501a7dd
 
 
 
 
 
 
 
1f4bfe9
501a7dd
 
 
 
 
 
1f4bfe9
64f89d6
501a7dd
 
 
 
 
 
 
 
64f89d6
 
 
 
 
 
 
 
 
 
 
 
 
 
1eef4eb
 
 
 
 
 
 
 
1f4bfe9
64f89d6
 
1eef4eb
 
64f89d6
501a7dd
 
64f89d6
1eef4eb
 
 
 
64f89d6
 
1eef4eb
 
 
 
 
 
 
 
 
64f89d6
 
 
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
"""Nova v3 for transformers.

Works on transformers 4.x and 5.x. The two versions disagree about how model code talks to
the key/value cache:

* 4.x passes the legacy format -- a tuple with one ``(keys, values)`` pair per layer -- or a
  ``Cache`` object, depending on version and settings;
* 5.x always passes a ``Cache`` object (``DynamicCache`` and friends) and removed both
  ``Cache.__getitem__`` and the legacy tuples, so indexing ``past_key_values[0][0]``
  raises ``TypeError``.

This file writes to the cache through its ``update(keys, values, layer_idx)`` method, which
both formats offer, and keeps the legacy tuple as a thin adapter. It also declares
``_tied_weights_keys`` as the ``{target: source}`` dict 5.x expects: Nova has a single
already shared embedding matrix and ``get_output_embeddings()`` returns ``None``, so there
is nothing to tie.
"""

import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import GenerationMixin, PreTrainedModel
from transformers.modeling_outputs import CausalLMOutputWithPast

try:
    from .configuration_nova import NovaConfig
except ImportError:
    from configuration_nova import NovaConfig


def _rope(x, cos, sin):
    a, b = x.chunk(2, dim=-1)
    return torch.cat((a * cos - b * sin, b * cos + a * sin), dim=-1)


class NovaRMSNorm(nn.Module):
    def __init__(self, dim):
        super().__init__()
        self.w = nn.Parameter(torch.ones(dim))

    def forward(self, x):
        return F.rms_norm(x, (x.size(-1),), self.w, 1e-6)


class _TupleCache:
    """The legacy cache of transformers 4.x: one ``(keys, values)`` pair per layer.

    It presents the same ``update`` method as ``transformers.cache_utils.Cache``, so blocks
    do not have to care which of the two they were handed, and can be turned back into the
    tuple format for callers that expect it.
    """

    def __init__(self, layers=()):
        self.layers = list(layers)

    def update(self, keys, values, layer_idx):
        while len(self.layers) <= layer_idx:
            self.layers.append(None)
        past = self.layers[layer_idx]
        if past is not None and past[0] is not None and past[0].numel() > 0:
            keys = torch.cat((past[0], keys), dim=-2)
            values = torch.cat((past[1], values), dim=-2)
        self.layers[layer_idx] = (keys, values)
        return keys, values

    def to_tuple(self):
        return tuple(self.layers)


def _as_cache(past_key_values):
    """Make both cache formats look alike: anything with an ``update`` method is used as is."""
    if past_key_values is None or hasattr(past_key_values, "update"):
        return past_key_values
    return _TupleCache(past_key_values)


def _cache_length(past_key_values):
    """How many positions are already stored in the cache."""
    if past_key_values is None:
        return 0
    if isinstance(past_key_values, _TupleCache):
        for pair in past_key_values.layers:
            if pair is not None and pair[0] is not None and pair[0].numel() > 0:
                return pair[0].size(-2)
        return 0
    return past_key_values.get_seq_length()


def _keep_mask(attention_mask, key_length):
    """Padding mask as booleans of shape ``(batch, 1, 1, key_length)``."""
    if attention_mask.dim() == 4:
        # transformers 5 builds a 4D mask when a static cache is used. It is boolean for
        # sdpa, and additive (0 where the position is kept) in the older float form.
        mask = attention_mask if attention_mask.dtype == torch.bool else attention_mask == 0
    else:
        mask = (attention_mask != 0)[:, None, None, :]
    return mask[..., -key_length:]


class NovaBlock(nn.Module):
    def __init__(self, config):
        super().__init__()
        d, h, kv, hd, ff = (
            config.hidden_size,
            config.num_attention_heads,
            config.num_key_value_heads,
            config.head_dim,
            config.intermediate_size,
        )
        self.h, self.kv, self.hd = h, kv, hd
        self.n1, self.n2 = NovaRMSNorm(d), NovaRMSNorm(d)
        self.qn, self.kn = NovaRMSNorm(hd), NovaRMSNorm(hd)
        self.wq = nn.Linear(d, h * hd, bias=False)
        self.wk = nn.Linear(d, kv * hd, bias=False)
        self.wv = nn.Linear(d, kv * hd, bias=False)
        self.wo = nn.Linear(h * hd, d, bias=False)
        self.gate = nn.Linear(d, ff, bias=False)
        self.up = nn.Linear(d, ff, bias=False)
        self.down = nn.Linear(ff, d, bias=False)

    def forward(self, x, cos, sin, attention_mask, cache=None, layer_idx=0):
        batch, length, _ = x.shape
        h = self.n1(x)
        q = self.wq(h).view(batch, length, self.h, self.hd)
        k = self.wk(h).view(batch, length, self.kv, self.hd)
        v = self.wv(h).view(batch, length, self.kv, self.hd)
        q = _rope(self.qn(q), cos, sin).to(v.dtype).transpose(1, 2)
        k = _rope(self.kn(k), cos, sin).to(v.dtype).transpose(1, 2)
        v = v.transpose(1, 2)
        if cache is not None:
            # Store this pass and get back everything cached for this layer.
            k, v = cache.update(k, v, layer_idx)
        repeat = self.h // self.kv
        k = k.repeat_interleave(repeat, dim=1)
        v = v.repeat_interleave(repeat, dim=1)
        key_length = k.size(-2)
        past_length = key_length - length
        query_positions = past_length + torch.arange(length, device=x.device)
        key_positions = torch.arange(key_length, device=x.device)
        causal = key_positions[None, :] <= query_positions[:, None]
        mask = causal[None, None, :, :]
        if attention_mask is not None:
            mask = mask & _keep_mask(attention_mask, key_length)
        out = F.scaled_dot_product_attention(q, k, v, attn_mask=mask)
        x = x + self.wo(out.transpose(1, 2).reshape(batch, length, -1))
        h = self.n2(x)
        x = x + self.down(F.silu(self.gate(h)) * self.up(h))
        return x


class NovaModel(PreTrainedModel):
    config_class = NovaConfig
    base_model_prefix = "model"
    _no_split_modules = ["NovaBlock"]

    def __init__(self, config):
        super().__init__(config)
        self.emb = nn.Embedding(config.vocab_size, config.hidden_size)
        self.blocks = nn.ModuleList(NovaBlock(config) for _ in range(config.num_hidden_layers))
        self.norm = NovaRMSNorm(config.hidden_size)
        self.rebuild_rope_buffers()
        self.post_init()

    def rebuild_rope_buffers(self):
        """(Re)build the rotary tables from the config.

        They are derived from the config, so they are not part of the checkpoint. That is
        fine on transformers 4, which runs ``__init__`` on the real device, but transformers 5
        builds the model on the meta device and moves such buffers back uninitialized -- the
        tables would be left holding empty memory. It recomputes them by calling
        ``_init_weights``, which is why every use of the tables goes through this method.
        """
        config = self.config
        frequencies = torch.outer(
            torch.arange(config.max_position_embeddings, dtype=torch.float32),
            1 / config.rope_theta ** (
                torch.arange(0, config.head_dim, 2, dtype=torch.float32) / config.head_dim
            ),
        )
        self.register_buffer("cos", frequencies.cos(), persistent=False)
        self.register_buffer("sin", frequencies.sin(), persistent=False)

    @torch.no_grad()
    def _init_weights(self, module):
        if isinstance(module, NovaModel):
            module.rebuild_rope_buffers()
        else:
            super()._init_weights(module)

    def get_input_embeddings(self):
        return self.emb

    def set_input_embeddings(self, value):
        self.emb = value

    def forward(
        self,
        input_ids=None,
        attention_mask=None,
        position_ids=None,
        inputs_embeds=None,
        past_key_values=None,
        use_cache=None,
        **kwargs,
    ):
        if inputs_embeds is None:
            inputs_embeds = self.emb(input_ids)
        batch, length, _ = inputs_embeds.shape
        if use_cache is None:
            use_cache = getattr(self.config, "use_cache", False)
        cache = _as_cache(past_key_values) if use_cache else None
        if position_ids is None:
            if attention_mask is not None and attention_mask.dim() == 2:
                position_ids = attention_mask.long().cumsum(-1) - 1
                position_ids.masked_fill_(attention_mask == 0, 0)
                position_ids = position_ids[:, -length:]
            else:
                past_length = _cache_length(cache)
                position_ids = torch.arange(
                    past_length, past_length + length, device=inputs_embeds.device
                ).unsqueeze(0).expand(batch, -1)
        cos = self.cos[position_ids].unsqueeze(-2)
        sin = self.sin[position_ids].unsqueeze(-2)
        hidden = inputs_embeds
        for layer_idx, block in enumerate(self.blocks):
            hidden = block(hidden, cos, sin, attention_mask, cache, layer_idx)
        presents = None
        if use_cache:
            presents = cache if not isinstance(cache, _TupleCache) else cache.to_tuple()
        return self.norm(hidden), presents


class NovaForCausalLM(PreTrainedModel, GenerationMixin):
    config_class = NovaConfig
    base_model_prefix = "model"
    # Nothing is tied: the embedding matrix is used for both the input and the output, and
    # get_output_embeddings() returns None. transformers 5 wants the mapping form, not a list.
    _tied_weights_keys = {}
    #: This model understands ``Cache`` objects, and also still accepts the legacy tuples.
    _supports_cache_class = True

    def __init__(self, config):
        super().__init__(config)
        self.model = NovaModel(config)
        self.post_init()

    def get_input_embeddings(self):
        return self.model.emb

    def set_input_embeddings(self, value):
        self.model.set_input_embeddings(value)

    def get_output_embeddings(self):
        return None

    @torch.no_grad()
    def _init_weights(self, module):
        # Recomputes the rotary tables, which are not in the checkpoint. See NovaModel.
        if isinstance(module, NovaModel):
            module.rebuild_rope_buffers()
        else:
            super()._init_weights(module)

    def prepare_inputs_for_generation(
        self,
        input_ids,
        attention_mask=None,
        token_type_ids=None,
        next_sequence_length=None,
        **kwargs,
    ):
        past_key_values = kwargs.get("past_key_values")
        past_length = _cache_length(past_key_values)
        if next_sequence_length is not None:
            # transformers 5 passes the full sequence and says how much of it is new.
            input_ids = input_ids[:, -next_sequence_length:]
        elif past_length > 0:
            # transformers 4 passes the full sequence as well, without saying how much is new.
            input_ids = input_ids[:, past_length:]
        if attention_mask is not None and attention_mask.dim() == 2:
            position_ids = attention_mask.long().cumsum(-1) - 1
            position_ids.masked_fill_(attention_mask == 0, 0)
            position_ids = position_ids[:, -input_ids.size(1):]
        else:
            position_ids = torch.arange(
                past_length, past_length + input_ids.size(1), device=input_ids.device
            ).unsqueeze(0).expand(input_ids.size(0), -1)
        return {
            "input_ids": input_ids,
            "attention_mask": attention_mask,
            "position_ids": position_ids,
            "past_key_values": past_key_values,
            "use_cache": kwargs.get("use_cache", True),
        }

    def forward(
        self,
        input_ids=None,
        attention_mask=None,
        position_ids=None,
        inputs_embeds=None,
        labels=None,
        token_type_ids=None,
        past_key_values=None,
        use_cache=None,
        **kwargs,
    ):
        if use_cache is None:
            # transformers 5 moved `use_cache` out of the config, so it may be absent.
            use_cache = getattr(self.config, "use_cache", True)
        hidden, presents = self.model(
            input_ids=input_ids,
            attention_mask=attention_mask,
            position_ids=position_ids,
            inputs_embeds=inputs_embeds,
            past_key_values=past_key_values,
            use_cache=use_cache,
        )
        logits = F.linear(hidden, self.model.emb.weight)
        loss = None
        if labels is not None:
            loss = F.cross_entropy(
                logits[..., :-1, :].contiguous().view(-1, logits.size(-1)),
                labels[..., 1:].contiguous().view(-1),
                ignore_index=-100,
            )
        return CausalLMOutputWithPast(
            loss=loss, logits=logits, past_key_values=presents
        )