afg1 commited on
Commit
2eb3d8a
1 Parent(s): c3e2724

Upload modeling_mixformer_sequential.py

Browse files
Files changed (1) hide show
  1. modeling_mixformer_sequential.py +770 -0
modeling_mixformer_sequential.py ADDED
@@ -0,0 +1,770 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) Microsoft Corporation.
2
+ # Licensed under the MIT license.
3
+ #
4
+ # BSD 3-Clause License
5
+ #
6
+ # Copyright (c) 2022, Tri Dao, trid@cs.stanford.edu.
7
+ # All rights reserved.
8
+ #
9
+ # Redistribution and use in source and binary forms, with or without
10
+ # modification, are permitted provided that the following conditions are met:
11
+ #
12
+ # * Redistributions of source code must retain the above copyright notice, this
13
+ # list of conditions and the following disclaimer.
14
+ #
15
+ # * Redistributions in binary form must reproduce the above copyright notice,
16
+ # this list of conditions and the following disclaimer in the documentation
17
+ # and/or other materials provided with the distribution.
18
+ #
19
+ # * Neither the name of the copyright holder nor the names of its
20
+ # contributors may be used to endorse or promote products derived from
21
+ # this software without specific prior written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
+
34
+ from __future__ import annotations
35
+
36
+ import math
37
+ import copy
38
+ from typing import Any, Dict, Optional, Tuple, Union
39
+ from dataclasses import dataclass, field
40
+
41
+ import torch
42
+ import torch.nn as nn
43
+
44
+ from einops import rearrange
45
+ from transformers.activations import ACT2FN
46
+ from transformers import PretrainedConfig, PreTrainedModel
47
+ from transformers.modeling_outputs import CausalLMOutputWithPast
48
+
49
+ from .configuration_mixformer_sequential import MixFormerSequentialConfig
50
+
51
+ @dataclass
52
+ class InferenceParams:
53
+ """Inference parameters passed to model to efficiently calculate
54
+ and store context during inference.
55
+
56
+ Reference:
57
+ https://github.com/Dao-AILab/flash-attention/blob/main/flash_attn/utils/generation.py.
58
+
59
+ Args:
60
+ max_sequence_len: Maximum sequence length.
61
+ max_batch_size: Maximum batch size.
62
+ sequence_len_offset: Sequence length offset.
63
+ batch_size_offset: Batch size offset.
64
+ key_value_memory_dict: Key value memory dictionary.
65
+ fused_ft_kernel: Whether to use fused kernel for fast inference.
66
+ lengths_per_sample: Lengths per sample.
67
+
68
+ """
69
+
70
+ max_sequence_len: int = field(metadata={"help": "Maximum sequence length."})
71
+
72
+ max_batch_size: int = field(metadata={"help": "Maximum batch size."})
73
+
74
+ sequence_len_offset: int = field(default=0, metadata={"help": "Sequence length offset."})
75
+
76
+ batch_size_offset: int = field(default=0, metadata={"help": "Batch size offset."})
77
+
78
+ key_value_memory_dict: Dict[str, Any] = field(
79
+ default_factory=dict, metadata={"help": "Key value memory dictionary."}
80
+ )
81
+
82
+ fused_ft_kernel: bool = field(default=False, metadata={"help": "Whether to use fused kernel for fast inference."})
83
+
84
+ lengths_per_sample: torch.Tensor = field(default=None, metadata={"help": "Lengths per sample."})
85
+
86
+
87
+ class Embedding(nn.Module):
88
+ """Token embedding with dropout."""
89
+
90
+ def __init__(self, config: PretrainedConfig) -> None:
91
+ super().__init__()
92
+
93
+ self.wte = nn.Embedding(config.vocab_size, config.n_embd)
94
+ self.drop = nn.Dropout(config.embd_pdrop)
95
+
96
+ def forward(self, input_ids: torch.LongTensor) -> torch.FloatTensor:
97
+ input_shape = input_ids.size()
98
+ input_ids = input_ids.view(-1, input_shape[-1])
99
+
100
+ hidden_states = self.wte(input_ids)
101
+ hidden_states = self.drop(hidden_states)
102
+
103
+ return hidden_states
104
+
105
+
106
+ class RotaryEmbedding(nn.Module):
107
+ """Rotary embeddings.
108
+
109
+ Reference:
110
+ https://github.com/Dao-AILab/flash-attention/blob/main/flash_attn/layers/rotary.py.
111
+
112
+ """
113
+
114
+ def __init__(
115
+ self,
116
+ dim: int,
117
+ base: int = 10000,
118
+ scale_base: Optional[float] = None,
119
+ device: Optional[str] = None,
120
+ **kwargs,
121
+ ) -> None:
122
+ super().__init__()
123
+
124
+ if scale_base is not None:
125
+ raise NotImplementedError
126
+
127
+ # Generate and save the inverse frequency buffer (non-trainable)
128
+ self.dim = dim
129
+ self.base = base
130
+ self.scale_base = scale_base
131
+ self.device = device
132
+
133
+ inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2, device=device, dtype=torch.float32) / dim))
134
+ self.register_buffer("inv_freq", inv_freq)
135
+
136
+ scale = (
137
+ (torch.arange(0, dim, 2, device=device, dtype=torch.float32) + 0.4 * dim) / (1.4 * dim)
138
+ if scale_base is not None
139
+ else None
140
+ )
141
+ self.register_buffer("scale", scale)
142
+
143
+ self._seq_len_cached = 0
144
+ self._cos_cached = None
145
+ self._sin_cached = None
146
+ self._cos_k_cached = None
147
+ self._sin_k_cached = None
148
+
149
+ def _update_cos_sin_cache(self, x: torch.FloatTensor, seqlen_offset: int = 0) -> None:
150
+ # Reset the tables if the sequence length has changed,
151
+ # or if we're on a new device (possibly due to tracing for instance)
152
+ seqlen = x.shape[1] + seqlen_offset
153
+
154
+ # Re-generate the inverse frequency buffer if it's not fp32
155
+ # (for instance if model.half() was called)
156
+ if self.inv_freq.dtype != "torch.float32":
157
+ self.inv_freq = 1.0 / (
158
+ self.base ** (torch.arange(0, self.dim, 2, device=self.device, dtype=torch.float32) / self.dim)
159
+ )
160
+
161
+ if seqlen > self._seq_len_cached or self._cos_cached.device != x.device or self._cos_cached.dtype != x.dtype:
162
+ self._seq_len_cached = seqlen
163
+ t = torch.arange(seqlen, device=x.device, dtype=torch.float32)
164
+
165
+ # Don't do einsum, it converts fp32 to fp16
166
+ # freqs = torch.einsum("i,j->ij", t, self.inv_freq)
167
+ freqs = torch.outer(t, self.inv_freq.to(device=t.device, dtype=torch.float32))
168
+ if self.scale is None:
169
+ self._cos_cached = torch.cos(freqs).to(x.dtype)
170
+ self._sin_cached = torch.sin(freqs).to(x.dtype)
171
+ else:
172
+ power = (
173
+ torch.arange(seqlen, dtype=self.scale.dtype, device=self.scale.device) - seqlen // 2
174
+ ) / self.scale_base
175
+ scale = self.scale.to(device=power.device) ** rearrange(power, "s -> s 1")
176
+
177
+ # We want the multiplication by scale to happen in fp32
178
+ self._cos_cached = (torch.cos(freqs) * scale).to(x.dtype)
179
+ self._sin_cached = (torch.sin(freqs) * scale).to(x.dtype)
180
+ self._cos_k_cached = (torch.cos(freqs) / scale).to(x.dtype)
181
+ self._sin_k_cached = (torch.sin(freqs) / scale).to(x.dtype)
182
+
183
+ def _apply_rotary_emb_qkv(
184
+ self,
185
+ qkv: torch.FloatTensor,
186
+ sin: torch.FloatTensor,
187
+ cos: torch.FloatTensor,
188
+ sin_k: Optional[torch.FloatTensor] = None,
189
+ cos_k: Optional[torch.FloatTensor] = None,
190
+ ) -> torch.FloatTensor:
191
+ _, seqlen, three, _, headdim = qkv.shape
192
+ assert three == 3
193
+
194
+ rotary_seqlen, rotary_dim = cos.shape
195
+ rotary_dim *= 2
196
+ assert rotary_dim <= headdim
197
+ assert seqlen <= rotary_seqlen
198
+
199
+ cos_k = cos if cos_k is None else cos_k
200
+ sin_k = sin if sin_k is None else sin_k
201
+ assert sin.shape == cos_k.shape == sin_k.shape == (rotary_seqlen, rotary_dim // 2)
202
+
203
+ q_rot = qkv[:, :, 0, :, :rotary_dim]
204
+ q_pass = qkv[:, :, 0, :, rotary_dim:]
205
+
206
+ k_rot = qkv[:, :, 1, :, :rotary_dim]
207
+ k_pass = qkv[:, :, 1, :, rotary_dim:]
208
+
209
+ # Splits the queries and keys in half
210
+ q1, q2 = q_rot.chunk(2, dim=-1)
211
+ k1, k2 = k_rot.chunk(2, dim=-1)
212
+ c, s = rearrange(cos[:seqlen], "s d -> s 1 d"), rearrange(sin[:seqlen], "s d -> s 1 d")
213
+
214
+ # Casts to fp32 are necessary to prevent fp16 overflow issues
215
+ q1, q2, k1, k2, c, s = [t.to(dtype=torch.float32) for t in [q1, q2, k1, k2, c, s]]
216
+
217
+ # Computes the new keys and queries, recasting to original dtype
218
+ q_rot = torch.cat([q1 * c - q2 * s, q1 * s + q2 * c], axis=-1).to(qkv.dtype)
219
+ k_rot = torch.cat([k1 * c - k2 * s, k1 * s + k2 * c], axis=-1).to(qkv.dtype)
220
+
221
+ return torch.cat(
222
+ [
223
+ torch.cat([q_rot, q_pass], axis=-1).unsqueeze(2),
224
+ torch.cat([k_rot, k_pass], axis=-1).unsqueeze(2),
225
+ qkv[:, :, 2:3, :, :],
226
+ ],
227
+ axis=2,
228
+ )
229
+
230
+ def forward(self, qkv: torch.Tensor, seqlen_offset: int = 0) -> Tuple[torch.Tensor, torch.Tensor]:
231
+ # `qkv` is of shape (batch, seqlen, 3, nheads, headdim)
232
+ self._update_cos_sin_cache(qkv, seqlen_offset)
233
+ return self._apply_rotary_emb_qkv(qkv, self._sin_cached[seqlen_offset:], self._cos_cached[seqlen_offset:])
234
+
235
+
236
+ class MLP(nn.Module):
237
+ """Multi-Layer Perceptron.
238
+
239
+ Reference:
240
+ Attention Is All You Need.
241
+ https://arxiv.org/pdf/1706.03762.pdf.
242
+
243
+ """
244
+
245
+ def __init__(self, config: PretrainedConfig, n_inner: Optional[int] = None, act_fn: Optional[str] = None) -> None:
246
+ super().__init__()
247
+
248
+ act_fn = config.activation_function if act_fn is None else act_fn
249
+ assert act_fn in ACT2FN.keys(), f"`act_fn` must be one of: {ACT2FN.keys()}."
250
+
251
+ n_inner = getattr(config, "n_inner", None) if n_inner is None else n_inner
252
+ n_inner = n_inner if n_inner is not None else 4 * config.n_embd
253
+
254
+ self.fc1 = nn.Linear(config.n_embd, n_inner)
255
+ self.fc2 = nn.Linear(n_inner, config.n_embd)
256
+ self.act = ACT2FN[act_fn]
257
+
258
+ def forward(self, hidden_states: torch.FloatTensor) -> torch.FloatTensor:
259
+ hidden_states = self.fc1(hidden_states)
260
+ hidden_states = self.act(hidden_states)
261
+ hidden_states = self.fc2(hidden_states)
262
+
263
+ return hidden_states
264
+
265
+
266
+ class SelfAttention(nn.Module):
267
+ """Self-attention layer (compatible with PyTorch).
268
+
269
+ Reference:
270
+ https://github.com/Dao-AILab/flash-attention/blob/main/flash_attn/modules/mha.py.
271
+
272
+ """
273
+
274
+ def __init__(
275
+ self,
276
+ causal: bool = True,
277
+ softmax_scale: Optional[float] = None,
278
+ attention_dropout: float = 0.0,
279
+ ) -> None:
280
+ super().__init__()
281
+
282
+ self.causal = causal
283
+ self.softmax_scale = softmax_scale
284
+ self.drop = nn.Dropout(attention_dropout)
285
+
286
+ def forward(
287
+ self,
288
+ qkv: torch.FloatTensor,
289
+ causal: bool = None,
290
+ attention_mask: Optional[torch.BoolTensor] = None,
291
+ **kwargs,
292
+ ) -> torch.FloatTensor:
293
+ causal = self.causal if causal is None else causal
294
+ batch_size, seq_len = qkv.shape[0], qkv.shape[1]
295
+ q, k, v = qkv.unbind(dim=2)
296
+
297
+ softmax_scale = self.softmax_scale or 1.0 / math.sqrt(q.shape[-1])
298
+ scores = torch.einsum("bthd,bshd->bhts", q, k * softmax_scale)
299
+
300
+ if attention_mask is not None:
301
+ padding_mask = torch.full((batch_size, seq_len), -10000.0, dtype=scores.dtype, device=scores.device)
302
+ padding_mask.masked_fill_(attention_mask, 0.0)
303
+
304
+ scores = scores + rearrange(padding_mask, "b s -> b 1 1 s")
305
+
306
+ if causal:
307
+ causal_mask = torch.triu(torch.full((seq_len, seq_len), -10000.0, device=scores.device), 1)
308
+ scores = scores + causal_mask.to(dtype=scores.dtype)
309
+
310
+ attention = torch.softmax(scores, dim=-1, dtype=v.dtype)
311
+ attention = self.drop(attention)
312
+
313
+ output = torch.einsum("bhts,bshd->bthd", attention, v)
314
+
315
+ return output
316
+
317
+
318
+ class CrossAttention(nn.Module):
319
+ """Cross-attention layer (compatible with PyTorch).
320
+
321
+ Reference:
322
+ https://github.com/Dao-AILab/flash-attention/blob/main/flash_attn/modules/mha.py.
323
+
324
+ """
325
+
326
+ def __init__(
327
+ self,
328
+ causal: bool = True,
329
+ softmax_scale: Optional[float] = None,
330
+ attention_dropout: float = 0.0,
331
+ ) -> None:
332
+ super().__init__()
333
+
334
+ self.causal = causal
335
+ self.softmax_scale = softmax_scale
336
+ self.drop = nn.Dropout(attention_dropout)
337
+
338
+ def forward(
339
+ self,
340
+ q: torch.FloatTensor,
341
+ kv: torch.FloatTensor,
342
+ causal: bool = None,
343
+ attention_mask: Optional[torch.BoolTensor] = None,
344
+ **kwargs,
345
+ ) -> torch.FloatTensor:
346
+ causal = self.causal if causal is None else causal
347
+ batch_size, seq_len_q = q.shape[0], q.shape[1]
348
+ assert kv.shape[0] == batch_size and kv.shape[3] == q.shape[2] and kv.shape[4] == q.shape[3]
349
+
350
+ seq_len_k = kv.shape[1]
351
+ k, v = kv.unbind(dim=2)
352
+
353
+ softmax_scale = self.softmax_scale or 1.0 / math.sqrt(q.shape[-1])
354
+ scores = torch.einsum("bthd,bshd->bhts", q, k * softmax_scale)
355
+
356
+ if attention_mask is not None:
357
+ padding_mask = torch.full((batch_size, seq_len_k), -10000.0, dtype=scores.dtype, device=scores.device)
358
+ padding_mask.masked_fill_(attention_mask, 0.0)
359
+
360
+ scores = scores + rearrange(padding_mask, "b s -> b 1 1 s")
361
+
362
+ if causal:
363
+ causal_mask = torch.triu(torch.full((seq_len_q, seq_len_k), -10000.0, device=scores.device), 1)
364
+ scores = scores + causal_mask.to(dtype=scores.dtype)
365
+
366
+ attention = torch.softmax(scores, dim=-1, dtype=v.dtype)
367
+ attention = self.drop(attention)
368
+
369
+ output = torch.einsum("bhts,bshd->bthd", attention, v)
370
+
371
+ return output
372
+
373
+
374
+ def find_mha_dims(
375
+ config: PretrainedConfig, n_head: Optional[int] = None, head_dim: Optional[int] = None
376
+ ) -> Tuple[int, int]:
377
+ """Validate and return the number of heads and head dimension for multi-head attention.
378
+
379
+ Args:
380
+ config: Model configuration.
381
+ n_head: Number of heads.
382
+ head_dim: Head dimension.
383
+
384
+ Returns:
385
+ Number of heads and head dimension.
386
+
387
+ """
388
+
389
+ assert all(
390
+ hasattr(config, attr) for attr in ["n_embd", "n_head"]
391
+ ), "`config` must have `n_embd` and `n_head` attributes."
392
+
393
+ if head_dim is None:
394
+ assert (
395
+ config.n_embd % config.n_head == 0
396
+ ), f"Hidden size ({config.n_embd}) must be divisible by the number of heads ({config.n_head})."
397
+
398
+ if n_head is None and head_dim is None:
399
+ head_dim = config.n_embd // config.n_head
400
+ n_head = config.n_head
401
+ elif n_head is None or head_dim is None:
402
+ raise ValueError("`n_head` and `head_dim` must be both specified or `None`.")
403
+
404
+ return n_head, head_dim
405
+
406
+
407
+ def update_kv_cache(kv: torch.FloatTensor, inference_params: InferenceParams, layer_idx: int) -> torch.FloatTensor:
408
+ """Update the key-value cache for inference.
409
+
410
+ Reference:
411
+ https://github.com/Dao-AILab/flash-attention/blob/main/flash_attn/modules/mha.py.
412
+
413
+ Args:
414
+ kv: Key-value tensor.
415
+ inference_params: Inference parameters.
416
+ layer_idx: Layer index.
417
+
418
+ Returns:
419
+ Updated key-value tensor.
420
+
421
+ """
422
+
423
+ num_heads, head_dim = kv.shape[-2:]
424
+
425
+ if layer_idx not in inference_params.key_value_memory_dict:
426
+ kv_cache = torch.empty(
427
+ inference_params.max_batch_size,
428
+ inference_params.max_sequence_len,
429
+ 2,
430
+ num_heads,
431
+ head_dim,
432
+ dtype=kv.dtype,
433
+ device=kv.device,
434
+ )
435
+ inference_params.key_value_memory_dict[layer_idx] = kv_cache
436
+ else:
437
+ if not inference_params.fused_ft_kernel:
438
+ kv_cache = inference_params.key_value_memory_dict[layer_idx]
439
+ else:
440
+ k_cache, v_cache = inference_params.key_value_memory_dict[layer_idx]
441
+ kv_cache = None
442
+
443
+ batch_start = inference_params.batch_size_offset
444
+ batch_end = batch_start + kv.shape[0]
445
+ assert batch_end <= (kv_cache.shape[0] if kv_cache is not None else v_cache.shape[0])
446
+
447
+ sequence_start = inference_params.sequence_len_offset
448
+ sequence_end = sequence_start + kv.shape[1]
449
+ assert sequence_end <= (kv_cache.shape[1] if kv_cache is not None else v_cache.shape[2])
450
+
451
+ if not inference_params.fused_ft_kernel:
452
+ assert kv_cache is not None
453
+
454
+ kv_cache[batch_start:batch_end, sequence_start:sequence_end, ...] = kv
455
+ kv = kv_cache[batch_start:batch_end, :sequence_end, ...]
456
+
457
+ return kv
458
+
459
+ assert inference_params.sequence_len_offset == 0
460
+ assert kv.dtype in [torch.float16, torch.bfloat16, torch.float32]
461
+
462
+ packsize = 4 if kv.dtype == torch.float32 else 8
463
+
464
+ if kv_cache is not None:
465
+ kv_cache[batch_start:batch_end, sequence_start:sequence_end, ...] = kv
466
+ k_cache = rearrange(kv_cache[:, :, 0], "b s h (d packsize) -> b h d s packsize", packsize=packsize).contiguous()
467
+ v_cache = rearrange(kv_cache[:, :, 1], "b s h d -> b h s d").contiguous()
468
+ inference_params.key_value_memory_dict[layer_idx] = (k_cache, v_cache)
469
+ else:
470
+ k_cache[batch_start:batch_end, :, :, :sequence_end, :] = rearrange(
471
+ kv[:, :, 0], "b s h (d packsize) -> b h d s packsize", packsize=packsize
472
+ )
473
+ v_cache[batch_start:batch_end, :, :sequence_end, :] = rearrange(kv[:, :, 1], "b s h d -> b h s d")
474
+
475
+ return kv
476
+
477
+
478
+ class MHA(nn.Module):
479
+ """Multi-head attention layer."""
480
+
481
+ def __init__(
482
+ self,
483
+ config: PretrainedConfig,
484
+ dtype: Optional[torch.dtype] = None,
485
+ device: Optional[str] = None,
486
+ rotary_dim: Optional[int] = None,
487
+ rotary_emb_scale_base: Optional[float] = None,
488
+ n_head: Optional[int] = None,
489
+ head_dim: Optional[int] = None,
490
+ bias: bool = True,
491
+ causal: bool = True,
492
+ softmax_scale: Optional[float] = None,
493
+ dropout: float = 0.0,
494
+ layer_idx: Optional[int] = None,
495
+ return_residual: bool = False,
496
+ checkpointing: bool = False,
497
+ ) -> None:
498
+ super().__init__()
499
+
500
+ # Rotary embedding
501
+ self.rotary_emb_dim = rotary_dim if rotary_dim is not None else getattr(config, "rotary_dim", 0)
502
+ if self.rotary_emb_dim > 0:
503
+ rotary_kwargs = {"device": device}
504
+ if rotary_emb_scale_base is not None and rotary_emb_scale_base > 0.0:
505
+ rotary_kwargs["scale_base"] = rotary_emb_scale_base
506
+ self.rotary_emb = RotaryEmbedding(self.rotary_emb_dim, **rotary_kwargs)
507
+
508
+ # MLP
509
+ self.n_head, self.head_dim = find_mha_dims(config, n_head, head_dim)
510
+ op_size = self.n_head * self.head_dim
511
+ hidden_size = config.n_embd
512
+
513
+ self.Wqkv = nn.Linear(hidden_size, 3 * op_size, bias=bias, device=device, dtype=dtype)
514
+ self.out_proj = nn.Linear(op_size, hidden_size, bias=bias, device=device, dtype=dtype)
515
+
516
+ # Attention
517
+ self.inner_attn = SelfAttention(causal=causal, softmax_scale=softmax_scale, attention_dropout=dropout)
518
+ self.inner_cross_attn = CrossAttention(causal=causal, softmax_scale=softmax_scale, attention_dropout=dropout)
519
+
520
+ self.layer_idx = layer_idx
521
+ self.return_residual = return_residual
522
+ self.checkpointing = checkpointing
523
+
524
+ def forward(
525
+ self,
526
+ x: torch.FloatTensor,
527
+ past_key_values: Optional[InferenceParams] = None,
528
+ attention_mask: Optional[torch.BoolTensor] = None,
529
+ cu_seqlens: Optional[torch.LongTensor] = None,
530
+ max_seqlen: Optional[int] = None,
531
+ **kwargs,
532
+ ) -> Tuple[torch.FloatTensor, torch.FloatTensor]:
533
+ qkv = self.Wqkv(x)
534
+ qkv = rearrange(qkv, "... (three h d) -> ... three h d", three=3, d=self.head_dim)
535
+
536
+ seqlen_offset = past_key_values.sequence_len_offset if past_key_values is not None else 0
537
+ if self.rotary_emb_dim > 0:
538
+ qkv = self.rotary_emb(qkv, seqlen_offset=seqlen_offset)
539
+
540
+ if past_key_values is not None:
541
+ kv = update_kv_cache(qkv[:, :, 1:], past_key_values, self.layer_idx)
542
+
543
+ if attention_mask is not None:
544
+ attention_mask = attention_mask[0] if isinstance(attention_mask, tuple) else attention_mask
545
+ attention_mask = attention_mask.bool().to(qkv.device)
546
+
547
+ attention_kwargs = {"attention_mask": attention_mask}
548
+
549
+ if past_key_values is None or seqlen_offset == 0:
550
+ if self.checkpointing:
551
+ attn_output = torch.utils.checkpoint.checkpoint(self.inner_attn, qkv, **attention_kwargs)
552
+ else:
553
+ attn_output = self.inner_attn(qkv, **attention_kwargs)
554
+ else:
555
+ q = qkv[:, :, 0]
556
+ causal = None if past_key_values.sequence_len_offset == 0 else False
557
+ attn_output = self.inner_cross_attn(q, kv, causal=causal, **attention_kwargs)
558
+
559
+ output = rearrange(attn_output, "... h d -> ... (h d)")
560
+ output = self.out_proj(output)
561
+
562
+ return output if not self.return_residual else (output, x)
563
+
564
+
565
+ class ParallelBlock(nn.Module):
566
+ """Parallel block.
567
+
568
+ This block applies parallel mixer and MLP layers to the input (used in GPT-J and CodeGen).
569
+
570
+ """
571
+
572
+ def __init__(
573
+ self,
574
+ config: PretrainedConfig,
575
+ block_idx: Optional[int] = None,
576
+ ) -> None:
577
+ super().__init__()
578
+
579
+ self.ln = nn.LayerNorm(config.n_embd, eps=config.layer_norm_epsilon)
580
+ self.resid_dropout = nn.Dropout(config.resid_pdrop)
581
+ self.block_idx = block_idx
582
+
583
+ self.mixer = MHA(config, layer_idx=block_idx)
584
+ self.mlp = MLP(config)
585
+
586
+ def forward(
587
+ self,
588
+ hidden_states: torch.FloatTensor,
589
+ past_key_values: Optional[Union[torch.FloatTensor, InferenceParams]] = None,
590
+ attention_mask: Optional[torch.BoolTensor] = None,
591
+ **kwargs,
592
+ ) -> torch.FloatTensor:
593
+ residual = hidden_states
594
+ hidden_states = self.ln(hidden_states)
595
+
596
+ attn_outputs = self.mixer(hidden_states, past_key_values=past_key_values, attention_mask=attention_mask)
597
+ if isinstance(attn_outputs, tuple):
598
+ attn_outputs = attn_outputs[0]
599
+
600
+ attn_outputs = self.resid_dropout(attn_outputs)
601
+ feed_forward_hidden_states = self.resid_dropout(self.mlp(hidden_states))
602
+
603
+ hidden_states = attn_outputs + feed_forward_hidden_states + residual
604
+
605
+ return hidden_states
606
+
607
+
608
+ class CausalLMHead(nn.Module):
609
+ """Causal Language Modeling head.
610
+
611
+ Reference:
612
+ Improving Language Understanding by Generative Pre-Training.
613
+ https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf.
614
+
615
+ """
616
+
617
+ def __init__(self, config: PretrainedConfig) -> None:
618
+ super().__init__()
619
+
620
+ self.ln = nn.LayerNorm(config.n_embd, eps=config.layer_norm_epsilon)
621
+ self.linear = nn.Linear(config.n_embd, config.vocab_size)
622
+
623
+ def forward(self, hidden_states: torch.FloatTensor) -> torch.FloatTensor:
624
+ hidden_states = self.ln(hidden_states)
625
+ logits = self.linear(hidden_states).to(torch.float32)
626
+
627
+ return logits
628
+
629
+
630
+ class CausalLMLoss(nn.Module):
631
+ """Causal Language Modeling loss.
632
+
633
+ Reference:
634
+ Improving Language Understanding by Generative Pre-Training.
635
+ https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf.
636
+
637
+ """
638
+
639
+ def __init__(self, shift_labels: bool = True) -> None:
640
+ super().__init__()
641
+
642
+ self.shift_labels = shift_labels
643
+ self.loss_fct = nn.CrossEntropyLoss()
644
+
645
+ def forward(self, logits: torch.FloatTensor, labels: torch.LongTensor) -> torch.FloatTensor:
646
+ if self.shift_labels:
647
+ logits = logits[..., :-1, :].contiguous()
648
+ labels = labels[..., 1:].contiguous()
649
+
650
+ loss = self.loss_fct(logits.view(-1, logits.size(-1)), labels.view(-1))
651
+
652
+ return loss
653
+
654
+
655
+ class MixFormerSequentialPreTrainedModel(PreTrainedModel):
656
+ """MixFormer (sequential for DeepSpeed) pre-trained model."""
657
+
658
+ config_class = MixFormerSequentialConfig
659
+ base_model_prefix = "transformer"
660
+ supports_gradient_checkpointing = True
661
+
662
+ def __init__(self, *inputs, **kwargs) -> None:
663
+ super().__init__(*inputs, **kwargs)
664
+
665
+ def _init_weights(self, module: nn.Module) -> None:
666
+ if isinstance(module, (nn.Linear,)):
667
+ module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
668
+ if module.bias is not None:
669
+ module.bias.data.zero_()
670
+ elif isinstance(module, nn.Embedding):
671
+ module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
672
+ if module.padding_idx is not None:
673
+ module.weight.data[module.padding_idx].zero_()
674
+ elif isinstance(module, nn.LayerNorm):
675
+ module.bias.data.zero_()
676
+ module.weight.data.fill_(1.0)
677
+
678
+ def prepare_inputs_for_generation(
679
+ self,
680
+ input_ids: torch.LongTensor,
681
+ past_key_values: Optional[Union[torch.FloatTensor, InferenceParams]] = None,
682
+ attention_mask: Optional[torch.BoolTensor] = None,
683
+ **kwargs,
684
+ ) -> Dict[str, Any]:
685
+ if attention_mask is not None and torch.any(~attention_mask.bool()):
686
+ total_seq_len = torch.sum(attention_mask, dim=1)
687
+ max_seq_len = torch.max(total_seq_len)
688
+
689
+ total_seq_len = torch.cat((torch.tensor([0], device=attention_mask.device), total_seq_len)).unsqueeze(1)
690
+ cumulative_seq_len = torch.cumsum(total_seq_len, dim=0).squeeze(1).to(torch.int32)
691
+ attention_mask = (attention_mask.bool(), cumulative_seq_len, max_seq_len.item())
692
+ else:
693
+ attention_mask = None
694
+
695
+ if past_key_values is None or not (isinstance(past_key_values, InferenceParams)):
696
+ past_key_values = InferenceParams(
697
+ max_batch_size=input_ids.shape[0],
698
+ max_sequence_len=self.config.n_positions,
699
+ sequence_len_offset=0,
700
+ batch_size_offset=0,
701
+ fused_ft_kernel=False,
702
+ key_value_memory_dict={},
703
+ )
704
+ else:
705
+ # Assume that `past_key_values` has cached all tokens up to the last token in `input_ids`
706
+ past_key_values.sequence_len_offset = len(input_ids[0]) - 1
707
+ input_ids = input_ids[:, -1].unsqueeze(-1)
708
+
709
+ return {
710
+ "input_ids": input_ids,
711
+ "past_key_values": past_key_values,
712
+ "attention_mask": attention_mask,
713
+ }
714
+
715
+
716
+ class MixFormerSequentialForCausalLM(MixFormerSequentialPreTrainedModel):
717
+ """MixFormer (sequential for DeepSpeed) for Causal Language Modeling."""
718
+
719
+ _keys_to_ignore_on_load_missing = [""]
720
+ _keys_to_ignore_on_load_unexpected = [r"layers\.\d+\.mlp.(fc_in|fc_out)\.(weight|bias)"]
721
+ _no_split_modules = ["ParallelBlock"]
722
+
723
+ def __init__(self, config: MixFormerSequentialConfig) -> None:
724
+ super().__init__(config)
725
+
726
+ modules = [Embedding(config)]
727
+ modules += [ParallelBlock(config, block_idx=i) for i in range(config.n_layer)]
728
+ modules.append(CausalLMHead(config))
729
+
730
+ self.layers = nn.Sequential(*modules)
731
+ self.loss = CausalLMLoss()
732
+
733
+ self.post_init()
734
+
735
+ def get_input_embeddings(self) -> nn.Embedding:
736
+ return self.layers[0].wte
737
+
738
+ def set_input_embeddings(self, new_embeddings: nn.Embedding) -> None:
739
+ self.layers[0].wte = new_embeddings
740
+
741
+ def get_output_embeddings(self) -> nn.Linear:
742
+ return self.layers[-1].linear
743
+
744
+ def set_output_embeddings(self, new_embeddings: nn.Linear) -> None:
745
+ self.layers[-1].linear = new_embeddings
746
+
747
+ def forward(
748
+ self,
749
+ input_ids: torch.LongTensor,
750
+ past_key_values: Optional[Union[torch.FloatTensor, InferenceParams]] = None,
751
+ attention_mask: Optional[torch.BoolTensor] = None,
752
+ labels: Optional[torch.LongTensor] = None,
753
+ **kwargs,
754
+ ) -> CausalLMOutputWithPast:
755
+ if attention_mask is not None and self.training:
756
+ print("`attention_mask` is not supported during training. Using it might lead to unexpected results.")
757
+
758
+ if past_key_values is None and attention_mask is None:
759
+ lm_logits = self.layers(input_ids)
760
+ else:
761
+ hidden_layer = self.layers[0](input_ids)
762
+ for module in self.layers[1:-1]:
763
+ hidden_layer = module(hidden_layer, past_key_values=past_key_values, attention_mask=attention_mask)
764
+ lm_logits = self.layers[-1](hidden_layer)
765
+
766
+ loss = None
767
+ if labels is not None:
768
+ loss = self.loss(lm_logits, labels)
769
+
770
+ return CausalLMOutputWithPast(loss=loss, logits=lm_logits, past_key_values=past_key_values)