vikhyatk commited on
Commit
4a8fa31
1 Parent(s): 24fcff5

Upload Moondream

Browse files
Files changed (4) hide show
  1. config.json +2 -2
  2. configuration_moondream.py +78 -51
  3. generation_config.json +1 -1
  4. modeling_phi.py +1072 -571
config.json CHANGED
@@ -8,8 +8,8 @@
8
  },
9
  "model_type": "moondream1",
10
  "phi_config": {
11
- "model_type": "phi-msft"
12
  },
13
  "torch_dtype": "float16",
14
- "transformers_version": "4.36.2"
15
  }
 
8
  },
9
  "model_type": "moondream1",
10
  "phi_config": {
11
+ "model_type": "phi"
12
  },
13
  "torch_dtype": "float16",
14
+ "transformers_version": "4.38.2"
15
  }
configuration_moondream.py CHANGED
@@ -5,65 +5,92 @@ import math
5
 
6
 
7
  class PhiConfig(PretrainedConfig):
8
- model_type = "phi-msft"
 
9
 
10
  def __init__(
11
  self,
12
- vocab_size: int = 51200,
13
- n_positions: int = 2048,
14
- n_embd: int = 2048,
15
- n_layer: int = 24,
16
- n_inner: Optional[int] = None,
17
- n_head: int = 32,
18
- n_head_kv: Optional[int] = None,
19
- rotary_dim: Optional[int] = 32,
20
- activation_function: Optional[str] = "gelu_new",
21
- flash_attn: bool = False,
22
- flash_rotary: bool = False,
23
- fused_dense: bool = False,
24
- attn_pdrop: float = 0.0,
25
- embd_pdrop: float = 0.0,
26
- resid_pdrop: float = 0.0,
27
- layer_norm_epsilon: float = 1e-5,
28
- initializer_range: float = 0.02,
29
- tie_word_embeddings: bool = False,
30
- pad_vocab_size_multiple: int = 64,
31
- gradient_checkpointing: bool = False,
32
- **kwargs
 
33
  ):
34
- pad_vocab_size = (
35
- math.ceil(vocab_size / pad_vocab_size_multiple) * pad_vocab_size_multiple
36
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  super().__init__(
38
- vocab_size=pad_vocab_size,
39
- n_positions=n_positions,
40
- n_embd=n_embd,
41
- n_layer=n_layer,
42
- n_inner=n_inner,
43
- n_head=n_head,
44
- n_head_kv=n_head_kv,
45
- activation_function=activation_function,
46
- attn_pdrop=attn_pdrop,
47
- embd_pdrop=embd_pdrop,
48
- resid_pdrop=resid_pdrop,
49
- layer_norm_epsilon=layer_norm_epsilon,
50
- initializer_range=initializer_range,
51
- pad_vocab_size_multiple=pad_vocab_size_multiple,
52
  tie_word_embeddings=tie_word_embeddings,
53
- gradient_checkpointing=gradient_checkpointing,
54
- **kwargs
55
  )
56
- self.rotary_dim = min(rotary_dim, n_embd // n_head)
57
- self.flash_attn = flash_attn
58
- self.flash_rotary = flash_rotary
59
- self.fused_dense = fused_dense
60
 
61
- attribute_map = {
62
- "max_position_embeddings": "n_positions",
63
- "hidden_size": "n_embd",
64
- "num_attention_heads": "n_head",
65
- "num_hidden_layers": "n_layer",
66
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
 
69
  class MoondreamConfig(PretrainedConfig):
 
5
 
6
 
7
  class PhiConfig(PretrainedConfig):
8
+ model_type = "phi"
9
+ keys_to_ignore_at_inference = ["past_key_values"]
10
 
11
  def __init__(
12
  self,
13
+ vocab_size=51200,
14
+ hidden_size=2048,
15
+ intermediate_size=8192,
16
+ num_hidden_layers=24,
17
+ num_attention_heads=32,
18
+ num_key_value_heads=None,
19
+ resid_pdrop=0.0,
20
+ embd_pdrop=0.0,
21
+ attention_dropout=0.0,
22
+ hidden_act="gelu_new",
23
+ max_position_embeddings=2048,
24
+ initializer_range=0.02,
25
+ layer_norm_eps=1e-5,
26
+ use_cache=True,
27
+ tie_word_embeddings=False,
28
+ rope_theta=10000.0,
29
+ rope_scaling=None,
30
+ partial_rotary_factor=0.5,
31
+ qk_layernorm=False,
32
+ bos_token_id=1,
33
+ eos_token_id=2,
34
+ **kwargs,
35
  ):
36
+ self.vocab_size = vocab_size
37
+ self.hidden_size = hidden_size
38
+ self.intermediate_size = intermediate_size
39
+ self.num_hidden_layers = num_hidden_layers
40
+ self.num_attention_heads = num_attention_heads
41
+
42
+ if num_key_value_heads is None:
43
+ num_key_value_heads = num_attention_heads
44
+
45
+ self.num_key_value_heads = num_key_value_heads
46
+ self.resid_pdrop = resid_pdrop
47
+ self.embd_pdrop = embd_pdrop
48
+ self.attention_dropout = attention_dropout
49
+ self.hidden_act = hidden_act
50
+ self.max_position_embeddings = max_position_embeddings
51
+ self.initializer_range = initializer_range
52
+ self.layer_norm_eps = layer_norm_eps
53
+ self.use_cache = use_cache
54
+ self.rope_theta = rope_theta
55
+ self.rope_scaling = rope_scaling
56
+ self.partial_rotary_factor = partial_rotary_factor
57
+ self.qk_layernorm = qk_layernorm
58
+ self._rope_scaling_validation()
59
+
60
  super().__init__(
61
+ bos_token_id=bos_token_id,
62
+ eos_token_id=eos_token_id,
 
 
 
 
 
 
 
 
 
 
 
 
63
  tie_word_embeddings=tie_word_embeddings,
64
+ **kwargs,
 
65
  )
 
 
 
 
66
 
67
+ # Copied from transformers.models.llama.configuration_llama.LlamaConfig._rope_scaling_validation
68
+ def _rope_scaling_validation(self):
69
+ """
70
+ Validate the `rope_scaling` configuration.
71
+ """
72
+ if self.rope_scaling is None:
73
+ return
74
+
75
+ if not isinstance(self.rope_scaling, dict) or len(self.rope_scaling) != 2:
76
+ raise ValueError(
77
+ "`rope_scaling` must be a dictionary with with two fields, `type` and `factor`, "
78
+ f"got {self.rope_scaling}"
79
+ )
80
+ rope_scaling_type = self.rope_scaling.get("type", None)
81
+ rope_scaling_factor = self.rope_scaling.get("factor", None)
82
+ if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
83
+ raise ValueError(
84
+ f"`rope_scaling`'s type field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"
85
+ )
86
+ if (
87
+ rope_scaling_factor is None
88
+ or not isinstance(rope_scaling_factor, float)
89
+ or rope_scaling_factor <= 1.0
90
+ ):
91
+ raise ValueError(
92
+ f"`rope_scaling`'s factor field must be a float > 1, got {rope_scaling_factor}"
93
+ )
94
 
95
 
96
  class MoondreamConfig(PretrainedConfig):
generation_config.json CHANGED
@@ -1,4 +1,4 @@
1
  {
2
  "_from_model_config": true,
3
- "transformers_version": "4.36.2"
4
  }
 
1
  {
2
  "_from_model_config": true,
3
+ "transformers_version": "4.38.2"
4
  }
modeling_phi.py CHANGED
@@ -1,720 +1,1221 @@
1
- # Copyright (c) Microsoft Corporation.
2
- # Licensed under the MIT license.
3
  #
4
- # Copyright (c) 2022, Tri Dao, trid@cs.stanford.edu.
5
- # Licensed under the BSD 3-Clause License.
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- from dataclasses import dataclass, field
8
- from typing import Any, Dict, Optional, Union, Tuple
9
 
10
  import math
 
 
11
  import torch
12
- import torch.nn as nn
13
- from einops import rearrange, repeat
14
- from transformers import PretrainedConfig, PreTrainedModel
15
- from transformers.activations import ACT2FN
16
- from transformers.modeling_outputs import CausalLMOutputWithPast
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  from .configuration_moondream import PhiConfig
19
 
20
- FusedDense = None
21
 
 
 
 
 
 
 
 
 
22
 
23
- @dataclass
24
- class InferenceParams:
25
- max_seqlen: int
26
- max_batch_size: int
27
- seqlen_offset: int = 0
28
- batch_size_offset: int = 0
29
- key_value_memory_dict: Dict[str, Any] = field(default_factory=dict)
30
- lengths_per_sample: torch.Tensor = None
31
 
 
32
 
33
- class Embedding(nn.Module):
34
- def __init__(self, config: PretrainedConfig):
35
- super().__init__()
36
- self.wte = nn.Embedding(config.vocab_size, config.n_embd)
37
- self.drop = nn.Dropout(config.embd_pdrop)
38
 
39
- def forward(self, input_ids: torch.LongTensor) -> torch.FloatTensor:
40
- return self.drop(self.wte(input_ids.view(-1, input_ids.size(-1))))
41
-
42
-
43
- def _apply_rotary_emb(x, cos, sin):
44
- seqlen, rotary_dim = x.size(1), cos.size(1) * 2
45
- x_rot, x_pass = x[..., :rotary_dim], x[..., rotary_dim:]
46
- x1, x2 = x_rot.chunk(2, dim=-1)
47
- c, s = cos[:seqlen].unsqueeze(1), sin[:seqlen].unsqueeze(1)
48
- x_rot = torch.cat([x1 * c - x2 * s, x1 * s + x2 * c], dim=-1)
49
- return torch.cat([x_rot.to(x.dtype), x_pass], dim=-1)
50
-
51
-
52
- def _apply_rotary_emb_kv(
53
- kv: torch.FloatTensor, cos: torch.FloatTensor, sin: torch.FloatTensor
54
- ) -> torch.FloatTensor:
55
- seqlen, rotary_dim = kv.shape[1], cos.shape[-1] * 2
56
- k_rot = kv[:, :, 0, :, :rotary_dim].chunk(2, dim=-1)
57
- k_pass = kv[:, :, 0, :, rotary_dim:]
58
- c, s = cos[:seqlen].unsqueeze(1), sin[:seqlen].unsqueeze(1)
59
- k_rot = torch.cat(
60
- [k_rot[0] * c - k_rot[1] * s, k_rot[0] * s + k_rot[1] * c], dim=-1
61
- )
62
- return torch.cat(
63
- [torch.cat([k_rot, k_pass], dim=-1).unsqueeze(2), kv[:, :, 1:2, :, :]], dim=2
64
  )
65
-
66
-
67
- def _apply_rotary_emb_qkv(
68
- qkv: torch.FloatTensor, cos: torch.FloatTensor, sin: torch.FloatTensor
69
- ) -> torch.FloatTensor:
70
- seqlen, rotary_dim = qkv.shape[1], cos.shape[1] * 2
71
-
72
- c = cos[:seqlen].unsqueeze(1)
73
- s = sin[:seqlen].unsqueeze(1)
74
-
75
- qkv_rot = torch.stack(
76
- [
77
- torch.cat(
78
- [
79
- qkv[:, :, i, :, : rotary_dim // 2] * c
80
- - qkv[:, :, i, :, rotary_dim // 2 : rotary_dim] * s,
81
- qkv[:, :, i, :, : rotary_dim // 2] * s
82
- + qkv[:, :, i, :, rotary_dim // 2 : rotary_dim] * c,
83
- ],
84
- dim=-1,
85
- ).to(qkv.dtype)
86
- for i in range(2)
87
- ],
88
- dim=2,
89
  )
90
 
91
- qkv_pass = qkv[:, :, :2, :, rotary_dim:].unsqueeze(2)
92
- qkv_v = qkv[:, :, 2:3, :, :]
93
- return torch.cat([qkv_rot, qkv_pass, qkv_v], dim=2)
94
 
95
-
96
- class RotaryEmbedding(nn.Module):
97
- # Enhanced Transformer with Rotary Position Embedding (https://arxiv.org/pdf/2104.09864.pdf)
98
- def __init__(
99
- self,
100
- dim: int,
101
- base: int = 10000,
102
- scale_base: Optional[float] = None,
103
- pos_idx_in_fp32: bool = True,
104
- max_position_embeddings: int = 2048,
105
- device: Optional[str] = None,
106
- ) -> None:
107
  super().__init__()
108
- # fp32 is preferred since the output of `torch.arange` can be quite large and bf16 would lose a lot of precision
109
- self.dim, self.base, self.pos_idx_in_fp32, self.device = (
110
- dim,
111
- float(base),
112
- pos_idx_in_fp32,
113
- device,
114
- )
115
- self.max_position_embeddings = max_position_embeddings
116
- if scale_base is not None:
117
- raise NotImplementedError
118
 
119
- # Generate and register the non-trainable buffers
120
- self.register_buffer(
121
- "inv_freq", self._compute_inv_freq(device), persistent=False
 
 
122
  )
123
- self.register_buffer(
124
- "scale", self._calculate_scale(dim, scale_base, device), persistent=False
 
 
 
 
 
125
  )
126
- self._update_cos_sin_cache(
127
- max_position_embeddings, device=device, dtype=torch.float32
 
 
 
128
  )
129
 
130
- def _calculate_scale(self, dim, scale_base, device):
 
 
 
 
 
 
 
 
 
 
131
  return (
132
- (
133
- (
134
- torch.arange(0, dim, 2, device=device, dtype=torch.float32)
135
- + 0.4 * dim
136
- )
137
- / (1.4 * dim)
138
- )
139
- if scale_base is not None
140
- else None
141
  )
142
 
143
- def _compute_inv_freq(self, device: Optional[str] = None) -> torch.FloatTensor:
144
- return 1.0 / (
145
- self.base
146
- ** (
147
- torch.arange(0, self.dim, 2, device=device, dtype=torch.float32)
148
- / self.dim
149
- )
150
- )
151
 
152
- def _update_cos_sin_cache(
 
 
 
 
153
  self,
154
- seqlen: int,
155
- device: Optional[str] = None,
156
- dtype: Optional[torch.dtype] = None,
157
- ) -> None:
158
- self._seq_len_cached = seqlen
 
 
 
 
 
 
159
  t = torch.arange(
160
- seqlen,
161
- device=device,
162
- dtype=torch.float32 if self.pos_idx_in_fp32 else self.inv_freq.dtype,
163
  )
164
- inv_freq = (
165
- self._compute_inv_freq(device=device)
166
- if self.pos_idx_in_fp32 and self.inv_freq.dtype != torch.float32
167
- else self.inv_freq
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  )
169
 
170
- freqs = torch.outer(t, inv_freq)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
- def apply_scale(freqs, scale, operator, dtype):
173
- result = operator(freqs)
174
- return (result / scale).to(dtype) if scale is not None else result.to(dtype)
175
 
176
- if scale := self.scale:
177
- power = (
178
- torch.arange(seqlen, dtype=scale.dtype, device=scale.device)
179
- - seqlen // 2
180
- ) / self.scale_base
181
- scale = scale.to(device=power.device) ** power.unsqueeze(1)
 
 
 
 
182
 
183
- self._cos_cached = apply_scale(
184
- freqs, 1 / scale if scale is not None else None, torch.cos, dtype
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  )
186
- self._sin_cached = apply_scale(
187
- freqs, 1 / scale if scale is not None else None, torch.sin, dtype
188
  )
189
- if scale is not None:
190
- self._cos_k_cached = apply_scale(freqs, scale, torch.cos, dtype)
191
- self._sin_k_cached = apply_scale(freqs, scale, torch.sin, dtype)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
 
193
  def forward(
194
  self,
195
- qkv: torch.Tensor,
196
- kv: Optional[torch.Tensor] = None,
197
- seqlen_offset: int = 0,
198
- ) -> Tuple[torch.Tensor, torch.Tensor]:
199
- should_update = (
200
- self._seq_len_cached < qkv.shape[1] + seqlen_offset
201
- or self._cos_cached.device != qkv.device
202
- or self._cos_cached.dtype != qkv.dtype
203
- or (self.training and self._cos_cached.is_inference())
204
- )
205
-
206
- if should_update:
207
- self._update_cos_sin_cache(
208
- qkv.shape[1] + seqlen_offset, device=qkv.device, dtype=qkv.dtype
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
  )
210
 
211
- offset_cos = self._cos_cached[seqlen_offset:]
212
- offset_sin = self._sin_cached[seqlen_offset:]
213
 
214
- if kv is None:
215
- return _apply_rotary_emb_qkv(qkv, offset_cos, offset_sin)
216
- else:
217
- return _apply_rotary_emb(qkv, offset_cos, offset_sin), _apply_rotary_emb_kv(
218
- kv, offset_cos, offset_sin
 
 
 
 
219
  )
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
 
222
- class MLP(nn.Module):
223
- def __init__(
224
- self,
225
- config: PretrainedConfig,
226
- n_inner: Optional[int] = None,
227
- act_fn: Optional[str] = None,
228
- ) -> None:
229
- super().__init__()
230
- n_inner = n_inner or getattr(config, "n_inner", None) or 4 * config.n_embd
231
- act_fn = act_fn or config.activation_function
232
 
233
- self.fc1 = nn.Linear(config.n_embd, n_inner)
234
- self.fc2 = nn.Linear(n_inner, config.n_embd)
235
- self.act = ACT2FN[act_fn]
 
 
236
 
237
- def forward(self, hidden_states: torch.FloatTensor) -> torch.FloatTensor:
238
- return self.fc2(self.act(self.fc1(hidden_states)))
239
 
 
240
 
241
- # Flash Attention (https://github.com/Dao-AILab/flash-attention/blob/main/flash_attn/modules/mha.py)
242
- class SelfAttention(nn.Module):
243
- def __init__(
244
- self,
245
- causal: bool = True,
246
- softmax_scale: Optional[float] = None,
247
- attention_dropout: float = 0.0,
248
- ):
249
- super().__init__()
250
- self.causal = causal
251
- self.softmax_scale = softmax_scale
252
- self.drop = nn.Dropout(attention_dropout)
253
 
254
- @torch.autocast("cpu", enabled=False)
255
- @torch.autocast("cuda", enabled=False)
256
- def forward(
257
- self,
258
- qkv: torch.FloatTensor,
259
- causal: Optional[bool] = None,
260
- key_padding_mask: Optional[torch.BoolTensor] = None,
261
- ):
262
- q, k, v = qkv.chunk(3, dim=-1)
263
- scale = self.softmax_scale or 1.0 / q.size(-1) ** 0.5
264
 
265
- scores = (
266
- torch.einsum("bthd,bshd->bhts", q.to(torch.float32), k.to(torch.float32))
267
- * scale
268
- )
269
- if causal or self.causal:
270
- scores.triu_(1).fill_(-10000.0)
271
- if key_padding_mask is not None:
272
- scores.masked_fill_(key_padding_mask[:, None, None, :], -10000.0)
273
 
274
- attn = self.drop(torch.softmax(scores, dim=-1).to(v.dtype))
275
- return torch.einsum("bhts,bshd->bthd", attn, v)
 
 
 
 
276
 
 
 
 
277
 
278
- # Flash Attention (https://github.com/Dao-AILab/flash-attention/blob/main/flash_attn/modules/mha.py)
279
- class CrossAttention(nn.Module):
280
- def __init__(self, causal=True, softmax_scale=None, attention_dropout=0.0):
281
- super().__init__()
282
- self.causal = causal
283
- self.softmax_scale = softmax_scale
284
- self.drop = nn.Dropout(attention_dropout)
285
 
286
- @torch.autocast("cpu", enabled=False)
287
- @torch.autocast("cuda", enabled=False)
288
  def forward(
289
  self,
290
- q: torch.FloatTensor,
291
- kv: torch.FloatTensor,
292
- causal: bool = None,
293
- key_padding_mask: Optional[torch.BoolTensor] = None,
294
- ) -> torch.FloatTensor:
295
- batch_size, seqlen_q = q.shape[0], q.shape[1]
296
- seqlen_k = kv.shape[1]
297
-
298
- if kv.shape[3] != q.shape[2]:
299
- kv = repeat(kv, "... hkv d -> ... (hkv g) d", g=q.shape[2] // kv.shape[3])
300
- k, v = kv.unbind(dim=2)
301
-
302
- q = q.to(torch.float32)
303
- k = k.to(torch.float32)
304
-
305
- causal = self.causal if causal is None else causal
306
- softmax_scale = self.softmax_scale or 1.0 / math.sqrt(q.shape[-1])
307
-
308
- # Autocast is manually disabled to avoid `torch.einsum` performing the operation using float16, which might lead to overflow
309
- scores = torch.einsum("bthd,bshd->bhts", q, k * softmax_scale)
310
-
311
- if key_padding_mask is not None:
312
- padding_mask = torch.full(
313
- (batch_size, seqlen_k),
314
- -10000.0,
315
- dtype=scores.dtype,
316
- device=scores.device,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
  )
318
- padding_mask.masked_fill_(key_padding_mask, 0.0)
319
- scores = scores + rearrange(padding_mask, "b s -> b 1 1 s")
320
 
321
- if causal:
322
- rows = rearrange(
323
- torch.arange(seqlen_q, device=q.device, dtype=torch.long), "s -> s 1"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
324
  )
325
- cols = torch.arange(seqlen_k, device=k.device, dtype=torch.long)
326
- causal_mask = cols > rows + seqlen_k - seqlen_q
327
- scores = scores.masked_fill(causal_mask, -10000.0)
328
-
329
- attention = torch.softmax(scores, dim=-1).to(v.dtype)
330
- attention = self.drop(attention)
331
- output = torch.einsum("bhts,bshd->bthd", attention, v)
332
-
333
- return output
334
-
335
-
336
- def _find_mha_dims(
337
- config: PretrainedConfig,
338
- n_head: Optional[int] = None,
339
- n_head_kv: Optional[int] = None,
340
- head_dim: Optional[int] = None,
341
- ) -> Tuple[int, int]:
342
- if n_head is None and head_dim is None:
343
- head_dim = config.n_embd // config.n_head
344
- n_head = config.n_head
345
- elif n_head is None or head_dim is None:
346
- raise ValueError("`n_head` and `head_dim` must be both specified or `None`.")
347
- if n_head_kv is None:
348
- n_head_kv = getattr(config, "n_head_kv", None) or n_head
349
- return n_head, n_head_kv, head_dim
350
-
351
-
352
- def _update_kv_cache(
353
- kv: torch.FloatTensor, inference_params: InferenceParams, layer_idx: int
354
- ) -> torch.FloatTensor:
355
- num_heads, head_dim = kv.shape[-2:]
356
- layer_memory = inference_params.key_value_memory_dict.setdefault(
357
- layer_idx,
358
- torch.empty(
359
- inference_params.max_batch_size,
360
- inference_params.max_seqlen,
361
- 2,
362
- num_heads,
363
- head_dim,
364
- dtype=kv.dtype,
365
- device=kv.device,
366
- ),
367
- )
368
 
369
- batch_slice = slice(
370
- inference_params.batch_size_offset,
371
- inference_params.batch_size_offset + kv.shape[0],
372
- )
373
- seqlen_slice = slice(
374
- inference_params.seqlen_offset, inference_params.seqlen_offset + kv.shape[1]
375
- )
 
 
 
 
 
 
376
 
377
- if seqlen_slice.stop >= inference_params.max_seqlen:
378
- layer_memory = torch.cat((layer_memory, kv), dim=1)
379
- inference_params.key_value_memory_dict[layer_idx] = layer_memory
380
 
381
- layer_memory[batch_slice, seqlen_slice, ...] = kv
382
- return layer_memory[batch_slice, : seqlen_slice.stop, ...]
383
 
 
384
 
385
- # Multi-head attention layer with rotary embeddings
386
- class MHA(nn.Module):
387
- def __init__(
388
  self,
389
- config,
390
- dtype=None,
391
- device=None,
392
- rotary_dim=None,
393
- rotary_base=10000.0,
394
- rotary_scale_base=None,
395
- n_head=None,
396
- n_head_kv=None,
397
- head_dim=None,
398
- bias=True,
399
- causal=True,
400
  softmax_scale=None,
401
- layer_idx=None,
402
- return_residual=False,
403
- checkpointing=False,
404
  ):
405
- super().__init__()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
406
 
407
- # Set rotary embedding if specified
408
- self.rotary_dim = rotary_dim or getattr(config, "rotary_dim", 0)
409
- if self.rotary_dim:
410
- self.rotary_emb = RotaryEmbedding(
411
- self.rotary_dim,
412
- base=rotary_base,
413
- scale_base=rotary_scale_base,
414
- device=device,
415
- max_position_embeddings=config.n_positions,
 
 
 
416
  )
417
 
418
- # Determine MHA dims from arguments or config
419
- self.n_head, self.n_head_kv, self.head_dim = _find_mha_dims(
420
- config, n_head, n_head_kv, head_dim
421
- )
422
- op_size = self.head_dim * (self.n_head + 2 * self.n_head_kv)
423
- hidden_size = config.n_embd
 
 
 
 
 
 
 
 
 
424
 
425
- # Choose Linear class based on config, FusedDense is optional
426
- LinearClass = (
427
- FusedDense if config.fused_dense and FusedDense is not None else nn.Linear
428
- )
429
- self.Wqkv = LinearClass(
430
- hidden_size, op_size, bias=bias, device=device, dtype=dtype
431
- )
432
- self.out_proj = LinearClass(
433
- hidden_size, hidden_size, bias=bias, device=device, dtype=dtype
434
- )
 
 
435
 
436
- # Initialize attention mechanisms
437
- attn_kwargs = {
438
- "causal": causal,
439
- "softmax_scale": softmax_scale,
440
- "attention_dropout": config.attn_pdrop,
441
- }
442
- self.inner_attn = SelfAttention(**attn_kwargs)
443
- self.inner_cross_attn = CrossAttention(**attn_kwargs)
444
 
445
- self.layer_idx = layer_idx
446
- self.return_residual = return_residual
447
- self.checkpointing = checkpointing
 
 
 
448
 
449
- def _forward_self_attn(
450
- self, x: torch.FloatTensor, key_padding_mask: Optional[torch.BoolTensor]
451
- ) -> torch.FloatTensor:
452
- qkv = rearrange(
453
- self.Wqkv(x), "... (three h d) -> ... three h d", three=3, d=self.head_dim
454
  )
455
- if self.rotary_dim > 0:
456
- qkv = self.rotary_emb(qkv)
457
- attn_func = (
458
- torch.utils.checkpoint.checkpoint
459
- if self.checkpointing
460
- else lambda f, *args, **kwargs: f(*args, **kwargs)
461
  )
462
- return attn_func(self.inner_attn, qkv, key_padding_mask=key_padding_mask)
463
-
464
- def _forward_cross_attn(
465
- self,
466
- x: torch.FloatTensor,
467
- past_key_values: Optional[InferenceParams],
468
- key_padding_mask: Optional[torch.BoolTensor],
469
- ) -> torch.FloatTensor:
470
- qkv = self.Wqkv(x)
471
- q, kv = (
472
- qkv[..., : self.n_head * self.head_dim],
473
- qkv[..., self.n_head * self.head_dim :],
474
- )
475
- q = rearrange(q, "... (h d) -> ... h d", d=self.head_dim)
476
- kv = rearrange(kv, "... (two hkv d) -> ... two hkv d", two=2, d=self.head_dim)
 
 
 
 
 
 
477
 
478
- seqlen_offset = (
479
- past_key_values.seqlen_offset if past_key_values is not None else 0
 
 
 
 
 
480
  )
481
- causal = None if seqlen_offset == 0 else False
482
- if self.rotary_dim > 0:
483
- q, kv = self.rotary_emb(q, kv=kv, seqlen_offset=seqlen_offset)
484
 
485
- if past_key_values is not None:
486
- kv = _update_kv_cache(kv, past_key_values, self.layer_idx)
487
 
488
- attn_func = (
489
- torch.utils.checkpoint.checkpoint
490
- if self.checkpointing
491
- else lambda fn, *args, **kwargs: fn(*args, **kwargs)
492
- )
493
 
494
- return attn_func(
495
- self.inner_cross_attn,
496
- q,
497
- kv,
498
- key_padding_mask=key_padding_mask,
499
- causal=causal,
500
- )
501
 
502
- def forward(
503
- self,
504
- x: torch.FloatTensor,
505
- past_key_values: Optional[InferenceParams] = None,
506
- attention_mask: Optional[Union[torch.LongTensor, torch.BoolTensor]] = None,
507
- ) -> Tuple[torch.FloatTensor, torch.FloatTensor]:
508
- attention_mask = attention_mask.bool() if attention_mask is not None else None
509
- use_cross_attn = self.n_head != self.n_head_kv or past_key_values is not None
510
- attn_output_function = (
511
- self._forward_cross_attn if use_cross_attn else self._forward_self_attn
512
- )
513
- attn_output = (
514
- attn_output_function(x, past_key_values, attention_mask)
515
- if use_cross_attn
516
- else attn_output_function(x, attention_mask)
517
- )
518
- output = self.out_proj(rearrange(attn_output, "... h d -> ... (h d)"))
519
- return (output, x) if self.return_residual else output
520
-
521
-
522
- # Parallel block. This block applies parallel mixer and MLP layers to the input (used in GPT-J and CodeGen).
523
- class ParallelBlock(nn.Module):
524
- def __init__(self, config: PretrainedConfig, block_idx: Optional[int] = None):
525
  super().__init__()
526
- self.ln = nn.LayerNorm(config.n_embd, eps=config.layer_norm_epsilon)
 
 
 
 
527
  self.resid_dropout = nn.Dropout(config.resid_pdrop)
528
- self.block_idx = block_idx
529
- self.mixer = MHA(config, layer_idx=block_idx)
530
- self.mlp = MLP(config)
531
 
532
  def forward(
533
  self,
534
- hidden_states: torch.FloatTensor,
535
- past_key_values: Optional[Union[torch.FloatTensor, InferenceParams]] = None,
536
- attention_mask: Optional[torch.BoolTensor] = None,
537
- ) -> torch.FloatTensor:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
538
  residual = hidden_states
 
539
  hidden_states = self.ln(hidden_states)
540
 
541
- attn_outputs = self.mixer(
542
- hidden_states,
543
- past_key_values=past_key_values,
544
  attention_mask=attention_mask,
 
 
 
 
545
  )
546
- if isinstance(attn_outputs, tuple):
547
- attn_outputs = attn_outputs[0]
548
-
549
  attn_outputs = self.resid_dropout(attn_outputs)
 
550
  feed_forward_hidden_states = self.resid_dropout(self.mlp(hidden_states))
551
- return attn_outputs + feed_forward_hidden_states + residual
 
552
 
 
 
553
 
554
- class CausalLMHead(nn.Module):
555
- """Causal Language Modeling head. Simplified version."""
556
 
557
- def __init__(self, config):
558
- super().__init__()
559
- self.ln = nn.LayerNorm(config.n_embd, eps=config.layer_norm_epsilon)
560
- self.linear = nn.Linear(config.n_embd, config.vocab_size)
561
 
562
- def forward(self, hidden_states):
563
- return self.linear(self.ln(hidden_states)).to(torch.float32)
564
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565
 
566
- # Improving Language Understanding by Generative Pre-Training
567
- # (https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf)
568
- class CausalLMLoss(nn.Module):
569
- def __init__(self, shift_labels: bool = True) -> None:
570
  super().__init__()
571
- self.shift_labels = shift_labels
572
- self.loss_fct = nn.CrossEntropyLoss()
 
573
 
574
- def forward(
575
- self, logits: torch.FloatTensor, labels: torch.LongTensor
576
- ) -> torch.FloatTensor:
577
- if self.shift_labels:
578
- logits, labels = logits[..., :-1, :], labels[..., 1:]
579
- return self.loss_fct(logits.reshape(-1, logits.size(-1)), labels.reshape(-1))
580
 
581
 
582
- class PhiPreTrainedModel(PreTrainedModel):
583
- config_class = PhiConfig
584
- base_model_prefix = "transformer"
585
- supports_gradient_checkpointing = False
586
- _no_split_modules = ["ParallelBlock"]
587
 
588
- def __init__(self, *inputs, **kwargs) -> None:
589
- super().__init__(*inputs, **kwargs)
 
590
 
591
- def prepare_inputs_for_generation(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
592
  self,
593
  input_ids: torch.LongTensor = None,
594
- inputs_embeds: torch.FloatTensor = None,
595
- past_key_values: Optional[Union[torch.FloatTensor, InferenceParams]] = None,
596
- attention_mask: Optional[Union[torch.LongTensor, torch.BoolTensor]] = None,
597
- **kwargs,
598
- ) -> Dict[str, Any]:
599
- if input_ids is None and inputs_embeds is None:
600
- raise ValueError(
601
- "You have to specify either `input_ids` or `inputs_embeds`."
602
- )
603
-
604
- max_batch_size = (
605
- inputs_embeds.shape[0] if inputs_embeds is not None else input_ids.shape[0]
 
606
  )
607
- seqlen_offset = (
608
- inputs_embeds.shape[1] + input_ids.shape[1] - 2
609
- if inputs_embeds is not None
610
- else input_ids.shape[1] - 1
611
  )
 
612
 
613
- args = (
614
- {"inputs_embeds": inputs_embeds}
615
- if inputs_embeds is not None
616
- else {"input_ids": input_ids}
617
  )
618
 
619
- if not isinstance(past_key_values, InferenceParams):
620
- past_key_values = InferenceParams(
621
- max_seqlen=self.config.n_positions,
622
- max_batch_size=max_batch_size,
623
- seqlen_offset=0,
624
- batch_size_offset=0,
625
- key_value_memory_dict={},
626
- lengths_per_sample=None,
627
  )
 
 
 
 
628
  else:
629
- past_key_values.seqlen_offset = seqlen_offset
630
- args = {"input_ids": input_ids[:, -1].unsqueeze(-1)}
631
 
632
- return {
633
- **args,
634
- "past_key_values": past_key_values,
635
- "attention_mask": attention_mask,
636
- }
637
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
638
 
639
- class PhiModel(PhiPreTrainedModel):
640
- _keys_to_ignore_on_load_missing = [""]
641
- _keys_to_ignore_on_load_unexpected = [r"h\.\d+\.mlp.(fc_in|fc_out)\.(weight|bias)"]
642
 
643
- def __init__(self, config: PhiConfig) -> None:
644
- super().__init__(config)
645
- self.embd = Embedding(config)
646
- self.h = nn.ModuleList(
647
- [ParallelBlock(config, block_idx=i) for i in range(config.n_layer)]
648
- )
649
- self.gradient_checkpointing = config.gradient_checkpointing
650
- self.post_init()
651
 
652
- def get_input_embeddings(self) -> nn.Embedding:
653
- return self.embd.wte
 
 
 
 
 
 
 
 
 
 
 
 
 
 
654
 
655
- def set_input_embeddings(self, new_embeddings: nn.Embedding) -> None:
656
- self.embd.wte = new_embeddings
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
657
 
658
- def forward(
659
- self,
660
- input_ids: torch.LongTensor = None,
661
- inputs_embeds: torch.FloatTensor = None,
662
- past_key_values: Optional[Union[torch.FloatTensor, InferenceParams]] = None,
663
- attention_mask: Optional[torch.BoolTensor] = None,
664
- ) -> torch.FloatTensor:
665
- if (input_ids is None) == (inputs_embeds is None):
666
- raise ValueError("Specify exactly one of `input_ids` or `inputs_embeds`.")
667
- hidden_states = self.embd(input_ids) if input_ids is not None else inputs_embeds
668
-
669
- for layer in self.h:
670
- func = layer.__call__ if self.gradient_checkpointing else layer
671
- args = (hidden_states, past_key_values, attention_mask)
672
- hidden_states = (
673
- torch.utils.checkpoint.checkpoint(func, *args, use_reentrant=True)
674
- if self.gradient_checkpointing
675
- else func(*args)
 
 
 
 
 
 
676
  )
 
 
 
 
 
 
677
 
678
- return hidden_states
 
 
 
 
 
 
 
 
 
 
679
 
680
 
681
  class PhiForCausalLM(PhiPreTrainedModel):
682
- _keys_to_ignore_on_load_missing, _keys_to_ignore_on_load_unexpected = (
683
- [""],
684
- [r"transformer\.h\.\d+\.mlp.(fc_in|fc_out)\.(weight|bias)"],
685
- )
686
 
687
- def __init__(self, config: PhiConfig) -> None:
 
688
  super().__init__(config)
689
  self.transformer = PhiModel(config)
 
690
  self.lm_head = CausalLMHead(config)
691
- self.loss = CausalLMLoss()
 
692
  self.post_init()
693
 
694
- def get_output_embeddings(self) -> nn.Linear:
 
 
 
 
 
 
 
 
 
695
  return self.lm_head.linear
696
 
697
- def set_output_embeddings(self, new_embeddings: nn.Linear) -> None:
 
698
  self.lm_head.linear = new_embeddings
699
 
 
 
 
 
 
 
 
 
700
  def forward(
701
  self,
702
  input_ids: torch.LongTensor = None,
703
- inputs_embeds: torch.FloatTensor = None,
704
- past_key_values: Optional[Union[torch.FloatTensor, InferenceParams]] = None,
705
- attention_mask: Optional[torch.BoolTensor] = None,
 
706
  labels: Optional[torch.LongTensor] = None,
707
- **kwargs,
708
- ) -> CausalLMOutputWithPast:
709
- hidden_states = self.transformer(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
710
  input_ids=input_ids,
711
- inputs_embeds=inputs_embeds,
712
- past_key_values=past_key_values,
713
  attention_mask=attention_mask,
 
 
 
 
 
 
 
714
  )
715
- lm_logits = self.lm_head(hidden_states)
716
- loss = self.loss(lm_logits, labels) if labels is not None else None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
717
 
718
  return CausalLMOutputWithPast(
719
- loss=loss, logits=lm_logits, past_key_values=past_key_values
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
720
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2023 Microsoft and the HuggingFace Inc. team. All rights reserved.
3
  #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ """ PyTorch Phi model."""
17
 
 
 
18
 
19
  import math
20
+ from typing import List, Optional, Tuple, Union
21
+
22
  import torch
23
+ import torch.nn.functional as F
24
+ import torch.utils.checkpoint
25
+ from torch import nn
26
+ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
 
27
 
28
+ from transformers.activations import ACT2FN
29
+ from transformers.cache_utils import Cache, DynamicCache
30
+ from transformers.modeling_attn_mask_utils import _prepare_4d_causal_attention_mask
31
+ from transformers.modeling_outputs import (
32
+ BaseModelOutputWithPast,
33
+ CausalLMOutputWithPast,
34
+ SequenceClassifierOutputWithPast,
35
+ )
36
+ from transformers.modeling_utils import PreTrainedModel
37
+ from transformers.utils import (
38
+ is_flash_attn_2_available,
39
+ is_flash_attn_greater_or_equal_2_10,
40
+ logging,
41
+ )
42
  from .configuration_moondream import PhiConfig
43
 
 
44
 
45
+ try: # noqa: SIM105
46
+ if is_flash_attn_2_available():
47
+ from flash_attn import flash_attn_func, flash_attn_varlen_func
48
+ from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input
49
+ except ImportError:
50
+ # Workaround for https://github.com/huggingface/transformers/issues/28459,
51
+ # don't move to contextlib.suppress(ImportError)
52
+ pass
53
 
 
 
 
 
 
 
 
 
54
 
55
+ logger = logging.get_logger(__name__)
56
 
 
 
 
 
 
57
 
58
+ # Copied from transformers.models.llama.modeling_llama._get_unpad_data
59
+ def _get_unpad_data(attention_mask):
60
+ seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
61
+ indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
62
+ max_seqlen_in_batch = seqlens_in_batch.max().item()
63
+ cu_seqlens = F.pad(
64
+ torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.torch.int32), (1, 0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  )
66
+ return (
67
+ indices,
68
+ cu_seqlens,
69
+ max_seqlen_in_batch,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  )
71
 
 
 
 
72
 
73
+ # Copied from transformers.models.llama.modeling_llama.LlamaRotaryEmbedding with Llama->Phi
74
+ class PhiRotaryEmbedding(nn.Module):
75
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
 
 
 
 
 
 
 
 
 
76
  super().__init__()
 
 
 
 
 
 
 
 
 
 
77
 
78
+ self.dim = dim
79
+ self.max_position_embeddings = max_position_embeddings
80
+ self.base = base
81
+ inv_freq = 1.0 / (
82
+ self.base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim)
83
  )
84
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
85
+
86
+ # Build here to make `torch.jit.trace` work.
87
+ self._set_cos_sin_cache(
88
+ seq_len=max_position_embeddings,
89
+ device=self.inv_freq.device,
90
+ dtype=torch.get_default_dtype(),
91
  )
92
+
93
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
94
+ self.max_seq_len_cached = seq_len
95
+ t = torch.arange(
96
+ self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype
97
  )
98
 
99
+ freqs = torch.outer(t, self.inv_freq)
100
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
101
+ emb = torch.cat((freqs, freqs), dim=-1)
102
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
103
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
104
+
105
+ def forward(self, x, seq_len=None):
106
+ # x: [bs, num_attention_heads, seq_len, head_size]
107
+ if seq_len > self.max_seq_len_cached:
108
+ self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
109
+
110
  return (
111
+ self.cos_cached[:seq_len].to(dtype=x.dtype),
112
+ self.sin_cached[:seq_len].to(dtype=x.dtype),
 
 
 
 
 
 
 
113
  )
114
 
 
 
 
 
 
 
 
 
115
 
116
+ # Copied from transformers.models.llama.modeling_llama.LlamaLinearScalingRotaryEmbedding with Llama->Phi
117
+ class PhiLinearScalingRotaryEmbedding(PhiRotaryEmbedding):
118
+ """PhiRotaryEmbedding extended with linear scaling. Credits to the Reddit user /u/kaiokendev"""
119
+
120
+ def __init__(
121
  self,
122
+ dim,
123
+ max_position_embeddings=2048,
124
+ base=10000,
125
+ device=None,
126
+ scaling_factor=1.0,
127
+ ):
128
+ self.scaling_factor = scaling_factor
129
+ super().__init__(dim, max_position_embeddings, base, device)
130
+
131
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
132
+ self.max_seq_len_cached = seq_len
133
  t = torch.arange(
134
+ self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype
 
 
135
  )
136
+ t = t / self.scaling_factor
137
+
138
+ freqs = torch.outer(t, self.inv_freq)
139
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
140
+ emb = torch.cat((freqs, freqs), dim=-1)
141
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
142
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
143
+
144
+
145
+ # Copied from transformers.models.llama.modeling_llama.LlamaDynamicNTKScalingRotaryEmbedding with Llama->Phi
146
+ class PhiDynamicNTKScalingRotaryEmbedding(PhiRotaryEmbedding):
147
+ """PhiRotaryEmbedding extended with Dynamic NTK scaling. Credits to the Reddit users /u/bloc97 and /u/emozilla"""
148
+
149
+ def __init__(
150
+ self,
151
+ dim,
152
+ max_position_embeddings=2048,
153
+ base=10000,
154
+ device=None,
155
+ scaling_factor=1.0,
156
+ ):
157
+ self.scaling_factor = scaling_factor
158
+ super().__init__(dim, max_position_embeddings, base, device)
159
+
160
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
161
+ self.max_seq_len_cached = seq_len
162
+
163
+ if seq_len > self.max_position_embeddings:
164
+ base = self.base * (
165
+ (self.scaling_factor * seq_len / self.max_position_embeddings)
166
+ - (self.scaling_factor - 1)
167
+ ) ** (self.dim / (self.dim - 2))
168
+ inv_freq = 1.0 / (
169
+ base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim)
170
+ )
171
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
172
+
173
+ t = torch.arange(
174
+ self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype
175
  )
176
 
177
+ freqs = torch.outer(t, self.inv_freq)
178
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
179
+ emb = torch.cat((freqs, freqs), dim=-1)
180
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
181
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
182
+
183
+
184
+ # Copied from transformers.models.llama.modeling_llama.rotate_half
185
+ def rotate_half(x):
186
+ """Rotates half the hidden dims of the input."""
187
+ x1 = x[..., : x.shape[-1] // 2]
188
+ x2 = x[..., x.shape[-1] // 2 :]
189
+ return torch.cat((-x2, x1), dim=-1)
190
+
191
+
192
+ # Copied from transformers.models.llama.modeling_llama.apply_rotary_pos_emb
193
+ def apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1):
194
+ """Applies Rotary Position Embedding to the query and key tensors.
195
+
196
+ Args:
197
+ q (`torch.Tensor`): The query tensor.
198
+ k (`torch.Tensor`): The key tensor.
199
+ cos (`torch.Tensor`): The cosine part of the rotary embedding.
200
+ sin (`torch.Tensor`): The sine part of the rotary embedding.
201
+ position_ids (`torch.Tensor`):
202
+ The position indices of the tokens corresponding to the query and key tensors. For example, this can be
203
+ used to pass offsetted position ids when working with a KV-cache.
204
+ unsqueeze_dim (`int`, *optional*, defaults to 1):
205
+ The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
206
+ sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
207
+ that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
208
+ k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
209
+ cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
210
+ the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
211
+ Returns:
212
+ `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
213
+ """
214
+ cos = cos[position_ids].unsqueeze(unsqueeze_dim)
215
+ sin = sin[position_ids].unsqueeze(unsqueeze_dim)
216
+ q_embed = (q * cos) + (rotate_half(q) * sin)
217
+ k_embed = (k * cos) + (rotate_half(k) * sin)
218
+ return q_embed, k_embed
219
+
220
+
221
+ # Copied from transformers.models.clip.modeling_clip.CLIPMLP with CLIP->Phi
222
+ class PhiMLP(nn.Module):
223
+ def __init__(self, config):
224
+ super().__init__()
225
+ self.config = config
226
+ self.activation_fn = ACT2FN[config.hidden_act]
227
+ self.fc1 = nn.Linear(config.hidden_size, config.intermediate_size)
228
+ self.fc2 = nn.Linear(config.intermediate_size, config.hidden_size)
229
+
230
+ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
231
+ hidden_states = self.fc1(hidden_states)
232
+ hidden_states = self.activation_fn(hidden_states)
233
+ hidden_states = self.fc2(hidden_states)
234
+ return hidden_states
235
+
236
+
237
+ # Copied from transformers.models.llama.modeling_llama.repeat_kv with llama->phi
238
+ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
239
+ """
240
+ This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
241
+ num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
242
+ """
243
+ batch, num_key_value_heads, slen, head_dim = hidden_states.shape
244
+ if n_rep == 1:
245
+ return hidden_states
246
+ hidden_states = hidden_states[:, :, None, :, :].expand(
247
+ batch, num_key_value_heads, n_rep, slen, head_dim
248
+ )
249
+ return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
250
+
251
 
252
+ class PhiAttention(nn.Module):
253
+ """Multi-headed attention from 'Attention Is All You Need' paper"""
 
254
 
255
+ def __init__(self, config: PhiConfig, layer_idx: Optional[int] = None):
256
+ super().__init__()
257
+ self.config = config
258
+ self.layer_idx = layer_idx
259
+ if layer_idx is None:
260
+ logger.warning_once(
261
+ f"Instantiating {self.__class__.__name__} without passing `layer_idx` is not recommended and will "
262
+ "to errors during the forward call, if caching is used. Please make sure to provide a `layer_idx` "
263
+ "when creating this class."
264
+ )
265
 
266
+ self.attention_dropout = config.attention_dropout
267
+ self.hidden_size = config.hidden_size
268
+ self.num_heads = config.num_attention_heads
269
+ self.head_dim = self.hidden_size // self.num_heads
270
+ self.num_key_value_heads = config.num_key_value_heads
271
+ self.num_key_value_groups = self.num_heads // self.num_key_value_heads
272
+ self.max_position_embeddings = config.max_position_embeddings
273
+ self.rope_theta = config.rope_theta
274
+ self.partial_rotary_factor = config.partial_rotary_factor
275
+ self.is_causal = True
276
+
277
+ if (self.head_dim * self.num_heads) != self.hidden_size:
278
+ raise ValueError(
279
+ f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
280
+ f" and `num_heads`: {self.num_heads})."
281
+ )
282
+
283
+ self.Wqkv = nn.Linear(
284
+ self.hidden_size, 3 * self.num_heads * self.head_dim, bias=True
285
  )
286
+ self.out_proj = nn.Linear(
287
+ self.num_heads * self.head_dim, self.hidden_size, bias=True
288
  )
289
+
290
+ self.qk_layernorm = config.qk_layernorm
291
+ if self.qk_layernorm:
292
+ self.q_layernorm = nn.LayerNorm(
293
+ config.hidden_size // self.num_heads,
294
+ eps=config.layer_norm_eps,
295
+ elementwise_affine=True,
296
+ )
297
+ self.k_layernorm = nn.LayerNorm(
298
+ config.hidden_size // self.num_heads,
299
+ eps=config.layer_norm_eps,
300
+ elementwise_affine=True,
301
+ )
302
+
303
+ self._init_rope()
304
+
305
+ def _init_rope(self):
306
+ if self.config.rope_scaling is None:
307
+ self.rotary_emb = PhiRotaryEmbedding(
308
+ int(self.partial_rotary_factor * self.head_dim),
309
+ max_position_embeddings=self.max_position_embeddings,
310
+ base=self.rope_theta,
311
+ )
312
+ else:
313
+ scaling_type = self.config.rope_scaling["type"]
314
+ scaling_factor = self.config.rope_scaling["factor"]
315
+ if scaling_type == "linear":
316
+ self.rotary_emb = PhiLinearScalingRotaryEmbedding(
317
+ int(self.partial_rotary_factor * self.head_dim),
318
+ max_position_embeddings=self.max_position_embeddings,
319
+ scaling_factor=scaling_factor,
320
+ base=self.rope_theta,
321
+ )
322
+ elif scaling_type == "dynamic":
323
+ self.rotary_emb = PhiDynamicNTKScalingRotaryEmbedding(
324
+ int(self.partial_rotary_factor * self.head_dim),
325
+ max_position_embeddings=self.max_position_embeddings,
326
+ scaling_factor=scaling_factor,
327
+ base=self.rope_theta,
328
+ )
329
+ else:
330
+ raise ValueError(f"Unknown RoPE scaling type {scaling_type}")
331
 
332
  def forward(
333
  self,
334
+ hidden_states: torch.Tensor,
335
+ attention_mask: Optional[torch.Tensor] = None,
336
+ position_ids: Optional[torch.LongTensor] = None,
337
+ past_key_value: Optional[Cache] = None,
338
+ output_attentions: bool = False,
339
+ use_cache: bool = False,
340
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
341
+ bsz, q_len, _ = hidden_states.size()
342
+
343
+ query_states, key_states, value_states = self.Wqkv(hidden_states).chunk(
344
+ 3, dim=-1
345
+ )
346
+
347
+ if self.qk_layernorm:
348
+ query_states = self.q_layernorm(query_states)
349
+ key_states = self.k_layernorm(key_states)
350
+
351
+ query_states = query_states.view(
352
+ bsz, q_len, self.num_heads, self.head_dim
353
+ ).transpose(1, 2)
354
+ key_states = key_states.view(
355
+ bsz, q_len, self.num_key_value_heads, self.head_dim
356
+ ).transpose(1, 2)
357
+ value_states = value_states.view(
358
+ bsz, q_len, self.num_key_value_heads, self.head_dim
359
+ ).transpose(1, 2)
360
+
361
+ kv_seq_len = key_states.shape[-2]
362
+ if past_key_value is not None:
363
+ if self.layer_idx is None:
364
+ raise ValueError(
365
+ f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
366
+ "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
367
+ "with a layer index."
368
+ )
369
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
370
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
371
+
372
+ # Partial rotary embedding
373
+ query_rot, query_pass = (
374
+ query_states[..., : self.rotary_emb.dim],
375
+ query_states[..., self.rotary_emb.dim :],
376
+ )
377
+ key_rot, key_pass = (
378
+ key_states[..., : self.rotary_emb.dim],
379
+ key_states[..., self.rotary_emb.dim :],
380
+ )
381
+ # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor]
382
+ query_rot, key_rot = apply_rotary_pos_emb(
383
+ query_rot, key_rot, cos, sin, position_ids
384
+ )
385
+
386
+ # [batch_size, seq_length, num_heads, head_dim]
387
+ query_states = torch.cat((query_rot, query_pass), dim=-1)
388
+ key_states = torch.cat((key_rot, key_pass), dim=-1)
389
+
390
+ if past_key_value is not None:
391
+ cache_kwargs = {
392
+ "sin": sin,
393
+ "cos": cos,
394
+ "partial_rotation_size": self.rotary_emb.dim,
395
+ }
396
+ key_states, value_states = past_key_value.update(
397
+ key_states, value_states, self.layer_idx, cache_kwargs
398
  )
399
 
400
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
401
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
402
 
403
+ # Queries and keys upcast to fp32 is required by Phi-2 to avoid overflow
404
+ attn_weights = torch.matmul(
405
+ query_states.to(torch.float32), key_states.to(torch.float32).transpose(2, 3)
406
+ ) / math.sqrt(self.head_dim)
407
+
408
+ if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
409
+ raise ValueError(
410
+ f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
411
+ f" {attn_weights.size()}"
412
  )
413
 
414
+ if attention_mask is not None:
415
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
416
+ raise ValueError(
417
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
418
+ )
419
+ attn_weights = attn_weights + attention_mask
420
+
421
+ # upcast attention to fp32
422
+ attn_weights = nn.functional.softmax(
423
+ attn_weights, dim=-1, dtype=torch.float32
424
+ ).to(value_states.dtype)
425
+ attn_weights = nn.functional.dropout(
426
+ attn_weights, p=self.attention_dropout, training=self.training
427
+ )
428
 
429
+ attn_output = torch.matmul(attn_weights, value_states)
 
 
 
 
 
 
 
 
 
430
 
431
+ if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
432
+ raise ValueError(
433
+ f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
434
+ f" {attn_output.size()}"
435
+ )
436
 
437
+ attn_output = attn_output.transpose(1, 2).contiguous()
438
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
439
 
440
+ attn_output = self.out_proj(attn_output)
441
 
442
+ if not output_attentions:
443
+ attn_weights = None
 
 
 
 
 
 
 
 
 
 
444
 
445
+ return attn_output, attn_weights, past_key_value
 
 
 
 
 
 
 
 
 
446
 
 
 
 
 
 
 
 
 
447
 
448
+ class PhiFlashAttention2(PhiAttention):
449
+ """
450
+ Phi flash attention module. This module inherits from `PhiAttention` as the weights of the module stays
451
+ untouched. The only required change would be on the forward pass where it needs to correctly call the public API of
452
+ flash attention and deal with padding tokens in case the input contains any of them.
453
+ """
454
 
455
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2.__init__
456
+ def __init__(self, *args, **kwargs):
457
+ super().__init__(*args, **kwargs)
458
 
459
+ # TODO: Should be removed once Flash Attention for RoCm is bumped to 2.1.
460
+ # flash_attn<2.1 generates top-left aligned causal mask, while what is needed here is bottom-right alignement, that was made default for flash_attn>=2.1. This attribute is used to handle this difference. Reference: https://github.com/Dao-AILab/flash-attention/releases/tag/v2.1.0.
461
+ # Beware that with flash_attn<2.1, using q_seqlen != k_seqlen (except for the case q_seqlen == 1) produces a wrong mask (top-left).
462
+ self._flash_attn_uses_top_left_mask = not is_flash_attn_greater_or_equal_2_10()
 
 
 
463
 
 
 
464
  def forward(
465
  self,
466
+ hidden_states: torch.Tensor,
467
+ attention_mask: Optional[torch.LongTensor] = None,
468
+ position_ids: Optional[torch.LongTensor] = None,
469
+ past_key_value: Optional[Cache] = None,
470
+ output_attentions: bool = False,
471
+ use_cache: bool = False,
472
+ **kwargs,
473
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
474
+ # PhiFlashAttention2 attention does not support output_attentions
475
+
476
+ output_attentions = False
477
+
478
+ bsz, q_len, _ = hidden_states.size()
479
+
480
+ query_states, key_states, value_states = self.Wqkv(hidden_states).chunk(
481
+ 3, dim=-1
482
+ )
483
+
484
+ if self.qk_layernorm:
485
+ query_states = self.q_layernorm(query_states)
486
+ key_states = self.k_layernorm(key_states)
487
+
488
+ # Flash attention requires the input to have the shape
489
+ # batch_size x seq_length x head_dim x hidden_dim
490
+ # therefore we just need to keep the original shape
491
+ query_states = query_states.view(
492
+ bsz, q_len, self.num_heads, self.head_dim
493
+ ).transpose(1, 2)
494
+ key_states = key_states.view(
495
+ bsz, q_len, self.num_key_value_heads, self.head_dim
496
+ ).transpose(1, 2)
497
+ value_states = value_states.view(
498
+ bsz, q_len, self.num_key_value_heads, self.head_dim
499
+ ).transpose(1, 2)
500
+
501
+ kv_seq_len = key_states.shape[-2]
502
+ if past_key_value is not None:
503
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
504
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
505
+
506
+ # Partial rotary embedding
507
+ query_rot, query_pass = (
508
+ query_states[..., : self.rotary_emb.dim],
509
+ query_states[..., self.rotary_emb.dim :],
510
+ )
511
+ key_rot, key_pass = (
512
+ key_states[..., : self.rotary_emb.dim],
513
+ key_states[..., self.rotary_emb.dim :],
514
+ )
515
+ # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor]
516
+ query_rot, key_rot = apply_rotary_pos_emb(
517
+ query_rot, key_rot, cos, sin, position_ids
518
+ )
519
+
520
+ # [batch_size, seq_length, num_heads, head_dim]
521
+ query_states = torch.cat((query_rot, query_pass), dim=-1)
522
+ key_states = torch.cat((key_rot, key_pass), dim=-1)
523
+
524
+ if past_key_value is not None:
525
+ cache_kwargs = {
526
+ "sin": sin,
527
+ "cos": cos,
528
+ "partial_rotation_size": self.rotary_emb.dim,
529
+ }
530
+ key_states, value_states = past_key_value.update(
531
+ key_states, value_states, self.layer_idx, cache_kwargs
532
  )
 
 
533
 
534
+ # TODO: These transpose are quite inefficient but Flash Attention requires the layout [batch_size, sequence_length, num_heads, head_dim]. We would need to refactor the KV cache
535
+ # to be able to avoid many of these transpose/reshape/view.
536
+ query_states = query_states.transpose(1, 2)
537
+ key_states = key_states.transpose(1, 2)
538
+ value_states = value_states.transpose(1, 2)
539
+
540
+ attn_dropout = self.attention_dropout if self.training else 0.0
541
+
542
+ # In PEFT, usually we cast the layer norms in float32 for training stability reasons
543
+ # therefore the input hidden states gets silently casted in float32. Hence, we need
544
+ # cast them back in the correct dtype just to be sure everything works as expected.
545
+ # This might slowdown training & inference so it is recommended to not cast the LayerNorms
546
+ # in fp32.
547
+
548
+ if query_states.dtype == torch.float32:
549
+ if torch.is_autocast_enabled():
550
+ target_dtype = torch.get_autocast_gpu_dtype()
551
+ # Handle the case where the model is quantized
552
+ elif hasattr(self.config, "_pre_quantization_dtype"):
553
+ target_dtype = self.config._pre_quantization_dtype
554
+ else:
555
+ target_dtype = self.q_proj.weight.dtype
556
+
557
+ logger.warning_once(
558
+ f"The input hidden states seems to be silently casted in float32, this might be related to"
559
+ f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in"
560
+ f" {target_dtype}."
561
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
562
 
563
+ query_states = query_states.to(target_dtype)
564
+ key_states = key_states.to(target_dtype)
565
+ value_states = value_states.to(target_dtype)
566
+
567
+ attn_output = self._flash_attention_forward(
568
+ query_states,
569
+ key_states,
570
+ value_states,
571
+ attention_mask,
572
+ q_len,
573
+ dropout=attn_dropout,
574
+ softmax_scale=None,
575
+ )
576
 
577
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous()
578
+ attn_output = self.out_proj(attn_output)
 
579
 
580
+ if not output_attentions:
581
+ attn_weights = None
582
 
583
+ return attn_output, attn_weights, past_key_value
584
 
585
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2._flash_attention_forward
586
+ def _flash_attention_forward(
 
587
  self,
588
+ query_states,
589
+ key_states,
590
+ value_states,
591
+ attention_mask,
592
+ query_length,
593
+ dropout=0.0,
 
 
 
 
 
594
  softmax_scale=None,
 
 
 
595
  ):
596
+ """
597
+ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token
598
+ first unpad the input, then computes the attention scores and pad the final attention scores.
599
+
600
+ Args:
601
+ query_states (`torch.Tensor`):
602
+ Input query states to be passed to Flash Attention API
603
+ key_states (`torch.Tensor`):
604
+ Input key states to be passed to Flash Attention API
605
+ value_states (`torch.Tensor`):
606
+ Input value states to be passed to Flash Attention API
607
+ attention_mask (`torch.Tensor`):
608
+ The padding mask - corresponds to a tensor of size `(batch_size, seq_len)` where 0 stands for the
609
+ position of padding tokens and 1 for the position of non-padding tokens.
610
+ dropout (`int`, *optional*):
611
+ Attention dropout
612
+ softmax_scale (`float`, *optional*):
613
+ The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim)
614
+ """
615
+ if not self._flash_attn_uses_top_left_mask:
616
+ causal = self.is_causal
617
+ else:
618
+ # TODO: Remove the `query_length != 1` check once Flash Attention for RoCm is bumped to 2.1. For details, please see the comment in LlamaFlashAttention2 __init__.
619
+ causal = self.is_causal and query_length != 1
620
 
621
+ # Contains at least one padding token in the sequence
622
+ if attention_mask is not None:
623
+ batch_size = query_states.shape[0]
624
+ (
625
+ query_states,
626
+ key_states,
627
+ value_states,
628
+ indices_q,
629
+ cu_seq_lens,
630
+ max_seq_lens,
631
+ ) = self._upad_input(
632
+ query_states, key_states, value_states, attention_mask, query_length
633
  )
634
 
635
+ cu_seqlens_q, cu_seqlens_k = cu_seq_lens
636
+ max_seqlen_in_batch_q, max_seqlen_in_batch_k = max_seq_lens
637
+
638
+ attn_output_unpad = flash_attn_varlen_func(
639
+ query_states,
640
+ key_states,
641
+ value_states,
642
+ cu_seqlens_q=cu_seqlens_q,
643
+ cu_seqlens_k=cu_seqlens_k,
644
+ max_seqlen_q=max_seqlen_in_batch_q,
645
+ max_seqlen_k=max_seqlen_in_batch_k,
646
+ dropout_p=dropout,
647
+ softmax_scale=softmax_scale,
648
+ causal=causal,
649
+ )
650
 
651
+ attn_output = pad_input(
652
+ attn_output_unpad, indices_q, batch_size, query_length
653
+ )
654
+ else:
655
+ attn_output = flash_attn_func(
656
+ query_states,
657
+ key_states,
658
+ value_states,
659
+ dropout,
660
+ softmax_scale=softmax_scale,
661
+ causal=causal,
662
+ )
663
 
664
+ return attn_output
 
 
 
 
 
 
 
665
 
666
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2._upad_input
667
+ def _upad_input(
668
+ self, query_layer, key_layer, value_layer, attention_mask, query_length
669
+ ):
670
+ indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask)
671
+ batch_size, kv_seq_len, num_key_value_heads, head_dim = key_layer.shape
672
 
673
+ key_layer = index_first_axis(
674
+ key_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim),
675
+ indices_k,
 
 
676
  )
677
+ value_layer = index_first_axis(
678
+ value_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim),
679
+ indices_k,
 
 
 
680
  )
681
+ if query_length == kv_seq_len:
682
+ query_layer = index_first_axis(
683
+ query_layer.reshape(batch_size * kv_seq_len, self.num_heads, head_dim),
684
+ indices_k,
685
+ )
686
+ cu_seqlens_q = cu_seqlens_k
687
+ max_seqlen_in_batch_q = max_seqlen_in_batch_k
688
+ indices_q = indices_k
689
+ elif query_length == 1:
690
+ max_seqlen_in_batch_q = 1
691
+ cu_seqlens_q = torch.arange(
692
+ batch_size + 1, dtype=torch.int32, device=query_layer.device
693
+ ) # There is a memcpy here, that is very bad.
694
+ indices_q = cu_seqlens_q[:-1]
695
+ query_layer = query_layer.squeeze(1)
696
+ else:
697
+ # The -q_len: slice assumes left padding.
698
+ attention_mask = attention_mask[:, -query_length:]
699
+ query_layer, indices_q, cu_seqlens_q, max_seqlen_in_batch_q = unpad_input(
700
+ query_layer, attention_mask
701
+ )
702
 
703
+ return (
704
+ query_layer,
705
+ key_layer,
706
+ value_layer,
707
+ indices_q,
708
+ (cu_seqlens_q, cu_seqlens_k),
709
+ (max_seqlen_in_batch_q, max_seqlen_in_batch_k),
710
  )
 
 
 
711
 
 
 
712
 
713
+ PHI_ATTENTION_CLASSES = {
714
+ "eager": PhiAttention,
715
+ "flash_attention_2": PhiFlashAttention2,
716
+ }
 
717
 
 
 
 
 
 
 
 
718
 
719
+ class PhiDecoderLayer(nn.Module):
720
+ def __init__(self, config: PhiConfig, layer_idx: int):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
721
  super().__init__()
722
+ self.mixer = PHI_ATTENTION_CLASSES[config._attn_implementation](
723
+ config, layer_idx=layer_idx
724
+ )
725
+ self.mlp = PhiMLP(config)
726
+ self.ln = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
727
  self.resid_dropout = nn.Dropout(config.resid_pdrop)
 
 
 
728
 
729
  def forward(
730
  self,
731
+ hidden_states: torch.Tensor,
732
+ attention_mask: Optional[torch.Tensor] = None,
733
+ position_ids: Optional[torch.LongTensor] = None,
734
+ output_attentions: Optional[bool] = False,
735
+ use_cache: Optional[bool] = False,
736
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
737
+ ) -> Tuple[
738
+ torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]
739
+ ]:
740
+ """
741
+ Args:
742
+ hidden_states (`torch.FloatTensor`):
743
+ input to the layer of shape `(batch, seq_len, embed_dim)`
744
+ attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
745
+ `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
746
+ position_ids (`torch.LongTensor` of shape `({0})`, *optional*):
747
+ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range
748
+ `[0, config.n_positions - 1]`. [What are position IDs?](../glossary#position-ids)
749
+ output_attentions (`bool`, *optional*):
750
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under
751
+ returned tensors for more detail.
752
+ use_cache (`bool`, *optional*):
753
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
754
+ (see `past_key_values`).
755
+ past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
756
+ """
757
+
758
  residual = hidden_states
759
+
760
  hidden_states = self.ln(hidden_states)
761
 
762
+ # Self Attention
763
+ attn_outputs, self_attn_weights, present_key_value = self.mixer(
764
+ hidden_states=hidden_states,
765
  attention_mask=attention_mask,
766
+ position_ids=position_ids,
767
+ past_key_value=past_key_value,
768
+ output_attentions=output_attentions,
769
+ use_cache=use_cache,
770
  )
 
 
 
771
  attn_outputs = self.resid_dropout(attn_outputs)
772
+
773
  feed_forward_hidden_states = self.resid_dropout(self.mlp(hidden_states))
774
+ hidden_states = attn_outputs + feed_forward_hidden_states + residual
775
+ outputs = (hidden_states,)
776
 
777
+ if output_attentions:
778
+ outputs += (self_attn_weights,)
779
 
780
+ if use_cache:
781
+ outputs += (present_key_value,)
782
 
783
+ return outputs
 
 
 
784
 
 
 
785
 
786
+ class PhiPreTrainedModel(PreTrainedModel):
787
+ config_class = PhiConfig
788
+ base_model_prefix = "model"
789
+ supports_gradient_checkpointing = True
790
+ _no_split_modules = ["PhiDecoderLayer"]
791
+ _skip_keys_device_placement = "past_key_values"
792
+ _supports_flash_attn_2 = True
793
+ _supports_cache_class = True
794
+
795
+ def _init_weights(self, module):
796
+ std = self.config.initializer_range
797
+ if isinstance(module, nn.Linear):
798
+ module.weight.data.normal_(mean=0.0, std=std)
799
+ if module.bias is not None:
800
+ module.bias.data.zero_()
801
+ elif isinstance(module, nn.Embedding):
802
+ module.weight.data.normal_(mean=0.0, std=std)
803
+ if module.padding_idx is not None:
804
+ module.weight.data[module.padding_idx].zero_()
805
 
806
+
807
+ class Embedding(nn.Module):
808
+ def __init__(self, config: PhiConfig):
 
809
  super().__init__()
810
+ self.wte = nn.Embedding(
811
+ config.vocab_size, config.hidden_size, padding_idx=config.pad_token_id
812
+ )
813
 
814
+ def forward(self, input_ids: torch.LongTensor) -> torch.FloatTensor:
815
+ return self.wte(input_ids)
 
 
 
 
816
 
817
 
818
+ class PhiModel(PhiPreTrainedModel):
819
+ """
820
+ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`PhiDecoderLayer`]
 
 
821
 
822
+ Args:
823
+ config: PhiConfig
824
+ """
825
 
826
+ def __init__(self, config: PhiConfig):
827
+ super().__init__(config)
828
+ self.padding_idx = config.pad_token_id
829
+ self.vocab_size = config.vocab_size
830
+
831
+ self.embd = Embedding(config)
832
+ self.embed_dropout = nn.Dropout(config.embd_pdrop)
833
+ self.h = nn.ModuleList(
834
+ [
835
+ PhiDecoderLayer(config, layer_idx)
836
+ for layer_idx in range(config.num_hidden_layers)
837
+ ]
838
+ )
839
+ self._use_flash_attention_2 = config._attn_implementation == "flash_attention_2"
840
+
841
+ self.gradient_checkpointing = False
842
+ # Initialize weights and apply final processing
843
+ self.post_init()
844
+
845
+ def get_input_embeddings(self):
846
+ return self.embd.wte
847
+
848
+ def set_input_embeddings(self, value):
849
+ self.embd.wte = value
850
+
851
+ def forward(
852
  self,
853
  input_ids: torch.LongTensor = None,
854
+ attention_mask: Optional[torch.Tensor] = None,
855
+ position_ids: Optional[torch.LongTensor] = None,
856
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
857
+ inputs_embeds: Optional[torch.FloatTensor] = None,
858
+ use_cache: Optional[bool] = None,
859
+ output_attentions: Optional[bool] = None,
860
+ output_hidden_states: Optional[bool] = None,
861
+ return_dict: Optional[bool] = None,
862
+ ) -> Union[Tuple, BaseModelOutputWithPast]:
863
+ output_attentions = (
864
+ output_attentions
865
+ if output_attentions is not None
866
+ else self.config.output_attentions
867
  )
868
+ output_hidden_states = (
869
+ output_hidden_states
870
+ if output_hidden_states is not None
871
+ else self.config.output_hidden_states
872
  )
873
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
874
 
875
+ return_dict = (
876
+ return_dict if return_dict is not None else self.config.use_return_dict
 
 
877
  )
878
 
879
+ # retrieve input_ids and inputs_embeds
880
+ if input_ids is not None and inputs_embeds is not None:
881
+ raise ValueError(
882
+ "You cannot specify both input_ids and inputs_embeds at the same time"
 
 
 
 
883
  )
884
+ elif input_ids is not None:
885
+ batch_size, seq_length = input_ids.shape[:2]
886
+ elif inputs_embeds is not None:
887
+ batch_size, seq_length = inputs_embeds.shape[:2]
888
  else:
889
+ raise ValueError("You have to specify either input_ids or inputs_embeds")
 
890
 
891
+ past_key_values_length = 0
 
 
 
 
892
 
893
+ if self.gradient_checkpointing and self.training:
894
+ if use_cache:
895
+ logger.warning_once(
896
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
897
+ )
898
+ use_cache = False
899
+
900
+ if use_cache:
901
+ use_legacy_cache = not isinstance(past_key_values, Cache)
902
+ if use_legacy_cache:
903
+ past_key_values = DynamicCache.from_legacy_cache(past_key_values)
904
+ past_key_values_length = past_key_values.get_usable_length(seq_length)
905
+
906
+ if position_ids is None:
907
+ device = input_ids.device if input_ids is not None else inputs_embeds.device
908
+ position_ids = torch.arange(
909
+ past_key_values_length,
910
+ seq_length + past_key_values_length,
911
+ dtype=torch.long,
912
+ device=device,
913
+ )
914
+ position_ids = position_ids.unsqueeze(0)
915
 
916
+ if inputs_embeds is None:
917
+ inputs_embeds = self.embd(input_ids)
 
918
 
919
+ inputs_embeds = self.embed_dropout(inputs_embeds)
 
 
 
 
 
 
 
920
 
921
+ # Attention mask.
922
+ if self._use_flash_attention_2:
923
+ # 2d mask is passed through the layers
924
+ attention_mask = (
925
+ attention_mask
926
+ if (attention_mask is not None and 0 in attention_mask)
927
+ else None
928
+ )
929
+ else:
930
+ # 4d mask is passed through the layers
931
+ attention_mask = _prepare_4d_causal_attention_mask(
932
+ attention_mask,
933
+ (batch_size, seq_length),
934
+ inputs_embeds,
935
+ past_key_values_length,
936
+ )
937
 
938
+ hidden_states = inputs_embeds
939
+
940
+ # decoder layers
941
+ all_hidden_states = () if output_hidden_states else None
942
+ all_self_attns = () if output_attentions else None
943
+ next_decoder_cache = None
944
+
945
+ for decoder_layer in self.h:
946
+ if output_hidden_states:
947
+ all_hidden_states += (hidden_states,)
948
+
949
+ if self.gradient_checkpointing and self.training:
950
+ layer_outputs = self._gradient_checkpointing_func(
951
+ decoder_layer.__call__,
952
+ hidden_states,
953
+ attention_mask,
954
+ position_ids,
955
+ past_key_values,
956
+ output_attentions,
957
+ )
958
+ else:
959
+ layer_outputs = decoder_layer(
960
+ hidden_states,
961
+ attention_mask=attention_mask,
962
+ position_ids=position_ids,
963
+ past_key_value=past_key_values,
964
+ output_attentions=output_attentions,
965
+ use_cache=use_cache,
966
+ )
967
 
968
+ hidden_states = layer_outputs[0]
969
+
970
+ if use_cache:
971
+ next_decoder_cache = layer_outputs[2 if output_attentions else 1]
972
+
973
+ if output_attentions:
974
+ all_self_attns += (layer_outputs[1],)
975
+
976
+ # add hidden states from the last decoder layer
977
+ if output_hidden_states:
978
+ all_hidden_states += (hidden_states,)
979
+
980
+ next_cache = None
981
+ if use_cache:
982
+ next_cache = (
983
+ next_decoder_cache.to_legacy_cache()
984
+ if use_legacy_cache
985
+ else next_decoder_cache
986
+ )
987
+ if not return_dict:
988
+ return tuple(
989
+ v
990
+ for v in [hidden_states, next_cache, all_hidden_states, all_self_attns]
991
+ if v is not None
992
  )
993
+ return BaseModelOutputWithPast(
994
+ last_hidden_state=hidden_states,
995
+ past_key_values=next_cache,
996
+ hidden_states=all_hidden_states,
997
+ attentions=all_self_attns,
998
+ )
999
 
1000
+
1001
+ class CausalLMHead(nn.Module):
1002
+ """Causal Language Modeling head. Simplified version."""
1003
+
1004
+ def __init__(self, config):
1005
+ super().__init__()
1006
+ self.ln = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
1007
+ self.linear = nn.Linear(config.hidden_size, config.vocab_size)
1008
+
1009
+ def forward(self, hidden_states):
1010
+ return self.linear(self.ln(hidden_states))
1011
 
1012
 
1013
  class PhiForCausalLM(PhiPreTrainedModel):
1014
+ _tied_weights_keys = ["lm_head.linear.weight"]
 
 
 
1015
 
1016
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.__init__ with Llama->Phi,bias=False->bias=True
1017
+ def __init__(self, config):
1018
  super().__init__(config)
1019
  self.transformer = PhiModel(config)
1020
+ self.vocab_size = config.vocab_size
1021
  self.lm_head = CausalLMHead(config)
1022
+
1023
+ # Initialize weights and apply final processing
1024
  self.post_init()
1025
 
1026
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.get_input_embeddings
1027
+ def get_input_embeddings(self):
1028
+ return self.transformer.embd.wte
1029
+
1030
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.set_input_embeddings
1031
+ def set_input_embeddings(self, value):
1032
+ self.model.embd.wte = value
1033
+
1034
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.get_output_embeddings
1035
+ def get_output_embeddings(self):
1036
  return self.lm_head.linear
1037
 
1038
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.set_output_embeddings
1039
+ def set_output_embeddings(self, new_embeddings):
1040
  self.lm_head.linear = new_embeddings
1041
 
1042
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.set_decoder
1043
+ def set_decoder(self, decoder):
1044
+ self.model = decoder
1045
+
1046
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.get_decoder
1047
+ def get_decoder(self):
1048
+ return self.model
1049
+
1050
  def forward(
1051
  self,
1052
  input_ids: torch.LongTensor = None,
1053
+ attention_mask: Optional[torch.Tensor] = None,
1054
+ position_ids: Optional[torch.LongTensor] = None,
1055
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1056
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1057
  labels: Optional[torch.LongTensor] = None,
1058
+ use_cache: Optional[bool] = None,
1059
+ output_attentions: Optional[bool] = None,
1060
+ output_hidden_states: Optional[bool] = None,
1061
+ return_dict: Optional[bool] = None,
1062
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
1063
+ r"""
1064
+ Args:
1065
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1066
+ Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
1067
+ config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
1068
+ (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
1069
+
1070
+ Returns:
1071
+
1072
+ Example:
1073
+
1074
+ ```python
1075
+ >>> from transformers import AutoTokenizer, PhiForCausalLM
1076
+
1077
+ >>> model = PhiForCausalLM.from_pretrained("microsoft/phi-1")
1078
+ >>> tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-1")
1079
+
1080
+ >>> prompt = "This is an example script ."
1081
+ >>> inputs = tokenizer(prompt, return_tensors="pt")
1082
+
1083
+ >>> # Generate
1084
+ >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
1085
+ >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
1086
+ 'This is an example script .\n\n\n\nfrom typing import List\n\ndef find_most_common_letter(words: List[str'
1087
+ ```"""
1088
+
1089
+ output_attentions = (
1090
+ output_attentions
1091
+ if output_attentions is not None
1092
+ else self.config.output_attentions
1093
+ )
1094
+ output_hidden_states = (
1095
+ output_hidden_states
1096
+ if output_hidden_states is not None
1097
+ else self.config.output_hidden_states
1098
+ )
1099
+ return_dict = (
1100
+ return_dict if return_dict is not None else self.config.use_return_dict
1101
+ )
1102
+
1103
+ # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
1104
+ outputs = self.transformer(
1105
  input_ids=input_ids,
 
 
1106
  attention_mask=attention_mask,
1107
+ position_ids=position_ids,
1108
+ past_key_values=past_key_values,
1109
+ inputs_embeds=inputs_embeds,
1110
+ use_cache=use_cache,
1111
+ output_attentions=output_attentions,
1112
+ output_hidden_states=output_hidden_states,
1113
+ return_dict=return_dict,
1114
  )
1115
+
1116
+ hidden_states = outputs[0]
1117
+ logits = self.lm_head(hidden_states)
1118
+ logits = logits.float()
1119
+
1120
+ loss = None
1121
+ if labels is not None:
1122
+ # Shift so that tokens < n predict n
1123
+ shift_logits = logits[..., :-1, :].contiguous()
1124
+ shift_labels = labels[..., 1:].contiguous()
1125
+ # Flatten the tokens
1126
+ loss_fct = CrossEntropyLoss()
1127
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
1128
+ shift_labels = shift_labels.view(-1)
1129
+ # Enable model parallelism
1130
+ shift_labels = shift_labels.to(shift_logits.device)
1131
+ loss = loss_fct(shift_logits, shift_labels)
1132
+
1133
+ if not return_dict:
1134
+ output = (logits,) + outputs[1:]
1135
+ return (loss,) + output if loss is not None else output
1136
 
1137
  return CausalLMOutputWithPast(
1138
+ loss=loss,
1139
+ logits=logits,
1140
+ past_key_values=outputs.past_key_values,
1141
+ hidden_states=outputs.hidden_states,
1142
+ attentions=outputs.attentions,
1143
+ )
1144
+
1145
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.prepare_inputs_for_generation
1146
+ def prepare_inputs_for_generation(
1147
+ self,
1148
+ input_ids,
1149
+ past_key_values=None,
1150
+ attention_mask=None,
1151
+ inputs_embeds=None,
1152
+ **kwargs,
1153
+ ):
1154
+ if past_key_values is not None:
1155
+ if isinstance(past_key_values, Cache):
1156
+ cache_length = past_key_values.get_seq_length()
1157
+ past_length = past_key_values.seen_tokens
1158
+ max_cache_length = past_key_values.get_max_length()
1159
+ else:
1160
+ cache_length = past_length = past_key_values[0][0].shape[2]
1161
+ max_cache_length = None
1162
+
1163
+ # Keep only the unprocessed tokens:
1164
+ # 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where
1165
+ # some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as
1166
+ # input)
1167
+ if (
1168
+ attention_mask is not None
1169
+ and attention_mask.shape[1] > input_ids.shape[1]
1170
+ ):
1171
+ input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :]
1172
+ # 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard
1173
+ # input_ids based on the past_length.
1174
+ elif past_length < input_ids.shape[1]:
1175
+ input_ids = input_ids[:, past_length:]
1176
+ # 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.
1177
+
1178
+ # If we are about to go beyond the maximum cache length, we need to crop the input attention mask.
1179
+ if (
1180
+ max_cache_length is not None
1181
+ and attention_mask is not None
1182
+ and cache_length + input_ids.shape[1] > max_cache_length
1183
+ ):
1184
+ attention_mask = attention_mask[:, -max_cache_length:]
1185
+
1186
+ position_ids = kwargs.get("position_ids", None)
1187
+ if attention_mask is not None and position_ids is None:
1188
+ # create position_ids on the fly for batch generation
1189
+ position_ids = attention_mask.long().cumsum(-1) - 1
1190
+ position_ids.masked_fill_(attention_mask == 0, 1)
1191
+ if past_key_values:
1192
+ position_ids = position_ids[:, -input_ids.shape[1] :]
1193
+
1194
+ # if `inputs_embeds` are passed, we only want to use them in the 1st generation step
1195
+ if inputs_embeds is not None and past_key_values is None:
1196
+ model_inputs = {"inputs_embeds": inputs_embeds}
1197
+ else:
1198
+ model_inputs = {"input_ids": input_ids}
1199
+
1200
+ model_inputs.update(
1201
+ {
1202
+ "position_ids": position_ids,
1203
+ "past_key_values": past_key_values,
1204
+ "use_cache": kwargs.get("use_cache"),
1205
+ "attention_mask": attention_mask,
1206
+ }
1207
  )
1208
+ return model_inputs
1209
+
1210
+ @staticmethod
1211
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM._reorder_cache
1212
+ def _reorder_cache(past_key_values, beam_idx):
1213
+ reordered_past = ()
1214
+ for layer_past in past_key_values:
1215
+ reordered_past += (
1216
+ tuple(
1217
+ past_state.index_select(0, beam_idx.to(past_state.device))
1218
+ for past_state in layer_past
1219
+ ),
1220
+ )
1221
+ return reordered_past