x54-729 commited on
Commit
a0beeb7
1 Parent(s): 9f35f03

update for new version

Browse files
Files changed (3) hide show
  1. config.json +2 -1
  2. configuration_internlm2.py +35 -13
  3. modeling_internlm2.py +730 -371
config.json CHANGED
@@ -28,5 +28,6 @@
28
  "torch_dtype": "bfloat16",
29
  "transformers_version": "4.39.3",
30
  "use_cache": true,
31
- "vocab_size": 92544
 
32
  }
 
28
  "torch_dtype": "bfloat16",
29
  "transformers_version": "4.39.3",
30
  "use_cache": true,
31
+ "vocab_size": 92544,
32
+ "pretraining_tp": 1
33
  }
configuration_internlm2.py CHANGED
@@ -37,16 +37,16 @@ class InternLM2Config(PretrainedConfig):
37
 
38
  Args:
39
  vocab_size (`int`, *optional*, defaults to 32000):
40
- Vocabulary size of the InternLM2 model. Defines the number of different tokens that can be represented
41
- by the `inputs_ids` passed when calling [`InternLM2Model`]
42
  hidden_size (`int`, *optional*, defaults to 4096):
43
  Dimension of the hidden representations.
44
  intermediate_size (`int`, *optional*, defaults to 11008):
45
  Dimension of the MLP representations.
46
  num_hidden_layers (`int`, *optional*, defaults to 32):
47
- Number of hidden layers in the Transformer encoder.
48
  num_attention_heads (`int`, *optional*, defaults to 32):
49
- Number of attention heads for each attention layer in the Transformer encoder.
50
  num_key_value_heads (`int`, *optional*):
51
  This is the number of key_value heads that should be used to implement Grouped Query Attention. If
52
  `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
@@ -58,22 +58,42 @@ class InternLM2Config(PretrainedConfig):
58
  hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
59
  The non-linear activation function (function or string) in the decoder.
60
  max_position_embeddings (`int`, *optional*, defaults to 2048):
61
- The maximum sequence length that this model might ever be used with. Typically set this to something large
62
- just in case (e.g., 512 or 1024 or 2048).
63
  initializer_range (`float`, *optional*, defaults to 0.02):
64
  The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
65
- rms_norm_eps (`float`, *optional*, defaults to 1e-12):
66
  The epsilon used by the rms normalization layers.
67
  use_cache (`bool`, *optional*, defaults to `True`):
68
  Whether or not the model should return the last key/values attentions (not used by all models). Only
69
  relevant if `config.is_decoder=True`.
70
- tie_word_embeddings(`bool`, *optional*, defaults to `False`):
 
 
 
 
 
 
 
 
 
 
 
 
71
  Whether to tie weight embeddings
72
- Example:
73
-
 
 
 
 
 
 
 
 
74
  """
75
- model_type = "internlm2"
76
  _auto_class = "AutoConfig"
 
 
77
 
78
  def __init__( # pylint: disable=W0102
79
  self,
@@ -91,11 +111,12 @@ class InternLM2Config(PretrainedConfig):
91
  pad_token_id=0,
92
  bos_token_id=1,
93
  eos_token_id=2,
 
94
  tie_word_embeddings=False,
95
  bias=True,
96
  rope_theta=10000,
97
  rope_scaling=None,
98
- attn_implementation="eager",
99
  **kwargs,
100
  ):
101
  self.vocab_size = vocab_size
@@ -113,14 +134,15 @@ class InternLM2Config(PretrainedConfig):
113
  self.hidden_act = hidden_act
114
  self.initializer_range = initializer_range
115
  self.rms_norm_eps = rms_norm_eps
 
116
  self.use_cache = use_cache
117
  self.rope_theta = rope_theta
118
  self.rope_scaling = rope_scaling
119
  self._rope_scaling_validation()
120
-
121
  self.attn_implementation = attn_implementation
122
  if self.attn_implementation is None:
123
  self.attn_implementation = "eager"
 
124
  super().__init__(
125
  pad_token_id=pad_token_id,
126
  bos_token_id=bos_token_id,
 
37
 
38
  Args:
39
  vocab_size (`int`, *optional*, defaults to 32000):
40
+ Vocabulary size of the InternLM2 model. Defines the number of different tokens that can be represented by the
41
+ `inputs_ids` passed when calling [`InternLM2Model`]
42
  hidden_size (`int`, *optional*, defaults to 4096):
43
  Dimension of the hidden representations.
44
  intermediate_size (`int`, *optional*, defaults to 11008):
45
  Dimension of the MLP representations.
46
  num_hidden_layers (`int`, *optional*, defaults to 32):
47
+ Number of hidden layers in the Transformer decoder.
48
  num_attention_heads (`int`, *optional*, defaults to 32):
49
+ Number of attention heads for each attention layer in the Transformer decoder.
50
  num_key_value_heads (`int`, *optional*):
51
  This is the number of key_value heads that should be used to implement Grouped Query Attention. If
52
  `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
 
58
  hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
59
  The non-linear activation function (function or string) in the decoder.
60
  max_position_embeddings (`int`, *optional*, defaults to 2048):
61
+ The maximum sequence length that this model might ever be used with. InternLM2 supports up to 32768 tokens.
 
62
  initializer_range (`float`, *optional*, defaults to 0.02):
63
  The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
64
+ rms_norm_eps (`float`, *optional*, defaults to 1e-06):
65
  The epsilon used by the rms normalization layers.
66
  use_cache (`bool`, *optional*, defaults to `True`):
67
  Whether or not the model should return the last key/values attentions (not used by all models). Only
68
  relevant if `config.is_decoder=True`.
69
+ pad_token_id (`int`, *optional*):
70
+ Padding token id.
71
+ bos_token_id (`int`, *optional*, defaults to 1):
72
+ Beginning of stream token id.
73
+ eos_token_id (`int`, *optional*, defaults to 2):
74
+ End of stream token id.
75
+ pretraining_tp (`int`, *optional*, defaults to 1):
76
+ Experimental feature. Tensor parallelism rank used during pretraining. Please refer to [this
77
+ document](https://huggingface.co/docs/transformers/main/perf_train_gpu_many#tensor-parallelism)
78
+ to understand more about it. This value is necessary to ensure exact reproducibility
79
+ of the pretraining results. Please refer to [this
80
+ issue](https://github.com/pytorch/pytorch/issues/76232).
81
+ tie_word_embeddings (`bool`, *optional*, defaults to `False`):
82
  Whether to tie weight embeddings
83
+ rope_theta (`float`, *optional*, defaults to 10000.0):
84
+ The base period of the RoPE embeddings.
85
+ rope_scaling (`Dict`, *optional*):
86
+ Dictionary containing the scaling configuration for the RoPE embeddings. Currently supports two scaling
87
+ strategies: linear and dynamic. Their scaling factor must be a float greater than 1. The expected format is
88
+ `{"type": strategy name, "factor": scaling factor}`. When using this flag, don't update
89
+ `max_position_embeddings` to the expected new maximum. See the following thread for more information on how
90
+ these scaling strategies behave:
91
+ https://www.reddit.com/r/LocalLLaMA/comments/14mrgpr/dynamically_scaled_rope_further_increases/. This is an
92
+ experimental feature, subject to breaking API changes in future versions.
93
  """
 
94
  _auto_class = "AutoConfig"
95
+ model_type = "internlm2"
96
+ keys_to_ignore_at_inference = ["past_key_values"]
97
 
98
  def __init__( # pylint: disable=W0102
99
  self,
 
111
  pad_token_id=0,
112
  bos_token_id=1,
113
  eos_token_id=2,
114
+ pretraining_tp=1,
115
  tie_word_embeddings=False,
116
  bias=True,
117
  rope_theta=10000,
118
  rope_scaling=None,
119
+ attn_implementation=None,
120
  **kwargs,
121
  ):
122
  self.vocab_size = vocab_size
 
134
  self.hidden_act = hidden_act
135
  self.initializer_range = initializer_range
136
  self.rms_norm_eps = rms_norm_eps
137
+ self.pretraining_tp = pretraining_tp
138
  self.use_cache = use_cache
139
  self.rope_theta = rope_theta
140
  self.rope_scaling = rope_scaling
141
  self._rope_scaling_validation()
 
142
  self.attn_implementation = attn_implementation
143
  if self.attn_implementation is None:
144
  self.attn_implementation = "eager"
145
+
146
  super().__init__(
147
  pad_token_id=pad_token_id,
148
  bos_token_id=bos_token_id,
modeling_internlm2.py CHANGED
@@ -13,11 +13,10 @@
13
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
  # See the License for the specific language governing permissions and
15
  # limitations under the License.
16
- """ PyTorch InternLM2 model."""
17
  import math
18
  import queue
19
  import threading
20
- import warnings
21
  from typing import List, Optional, Tuple, Union
22
 
23
  import torch
@@ -27,15 +26,22 @@ from einops import rearrange
27
  from torch import nn
28
  from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
29
  from transformers.activations import ACT2FN
 
 
30
  from transformers.modeling_outputs import (
31
  BaseModelOutputWithPast,
32
  CausalLMOutputWithPast,
 
33
  SequenceClassifierOutputWithPast,
 
34
  )
35
  from transformers.modeling_utils import PreTrainedModel
 
36
  from transformers.utils import (
37
  add_start_docstrings,
38
  add_start_docstrings_to_model_forward,
 
 
39
  logging,
40
  replace_return_docstrings,
41
  )
@@ -47,36 +53,21 @@ except Exception:
47
 
48
  from .configuration_internlm2 import InternLM2Config
49
 
50
- logger = logging.get_logger(__name__)
51
-
52
- _CONFIG_FOR_DOC = "InternLM2Config"
53
 
54
- flash_attn_func, flash_attn_varlen_func = None, None
55
- pad_input, index_first_axis, unpad_input = None, None, None
56
 
 
57
 
58
- def _import_flash_attn():
59
- global flash_attn_func, flash_attn_varlen_func
60
- global pad_input, index_first_axis, unpad_input
61
- try:
62
- from flash_attn import flash_attn_func as _flash_attn_func
63
- from flash_attn import flash_attn_varlen_func as _flash_attn_varlen_func
64
- from flash_attn.bert_padding import index_first_axis as _index_first_axis
65
- from flash_attn.bert_padding import pad_input as _pad_input
66
- from flash_attn.bert_padding import unpad_input as _unpad_input
67
-
68
- flash_attn_func, flash_attn_varlen_func = _flash_attn_func, _flash_attn_varlen_func
69
- pad_input, index_first_axis, unpad_input = _pad_input, _index_first_axis, _unpad_input
70
- except ImportError:
71
- raise ImportError("flash_attn is not installed.")
72
 
73
 
74
- # Copied from transformers.models.llama.modeling_llama._get_unpad_data
75
  def _get_unpad_data(attention_mask):
76
  seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
77
  indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
78
  max_seqlen_in_batch = seqlens_in_batch.max().item()
79
- cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.torch.int32), (1, 0))
80
  return (
81
  indices,
82
  cu_seqlens,
@@ -84,49 +75,10 @@ def _get_unpad_data(attention_mask):
84
  )
85
 
86
 
87
- # Copied from transformers.models.bart.modeling_bart._make_causal_mask
88
- def _make_causal_mask(
89
- input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0
90
- ):
91
- """
92
- Make causal mask used for bi-directional self-attention.
93
- """
94
- bsz, tgt_len = input_ids_shape
95
- mask = torch.full((tgt_len, tgt_len), torch.tensor(torch.finfo(dtype).min, device=device), device=device)
96
- mask_cond = torch.arange(mask.size(-1), device=device)
97
- mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0)
98
- mask = mask.to(dtype)
99
-
100
- if past_key_values_length > 0:
101
- mask = torch.cat([torch.zeros(tgt_len, past_key_values_length, dtype=dtype, device=device), mask], dim=-1)
102
- return mask[None, None, :, :].expand(bsz, 1, tgt_len, tgt_len + past_key_values_length)
103
-
104
-
105
- # Copied from transformers.models.bart.modeling_bart._expand_mask
106
- def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None):
107
- """
108
- Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`.
109
- """
110
- bsz, src_len = mask.size()
111
- tgt_len = tgt_len if tgt_len is not None else src_len
112
-
113
- expanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype)
114
-
115
- inverted_mask = 1.0 - expanded_mask
116
-
117
- return inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(dtype).min)
118
-
119
-
120
- # Copied from transformers.models.llama.modeling_llama.LlamaRMSNorm with Llama->InternLM2
121
  class InternLM2RMSNorm(nn.Module):
122
- """
123
- InternLM2 RMSNorm implemention.
124
- """
125
 
126
  def __init__(self, hidden_size, eps=1e-6):
127
- """
128
- InternLM2RMSNorm is equivalent to T5LayerNorm
129
- """
130
  super().__init__()
131
  self.weight = nn.Parameter(torch.ones(hidden_size))
132
  self.variance_epsilon = eps
@@ -139,97 +91,68 @@ class InternLM2RMSNorm(nn.Module):
139
  return self.weight * hidden_states.to(input_dtype)
140
 
141
 
142
- # Copied from transformers.model.llama.modeling_llama.LlamaRotaryEmbedding with Llama->InternLM2
 
 
143
  class InternLM2RotaryEmbedding(nn.Module):
144
- """
145
- Normal InternLM2 rotary embedding.
146
- """
147
 
148
- def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
149
  super().__init__()
150
-
151
  self.dim = dim
152
  self.max_position_embeddings = max_position_embeddings
153
  self.base = base
154
- inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
155
  self.register_buffer("inv_freq", inv_freq, persistent=False)
 
 
156
 
157
- # Build here to make `torch.jit.trace` work.
158
- self._set_cos_sin_cache(
159
- seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
160
- )
161
-
162
- def _set_cos_sin_cache(self, seq_len, device, dtype):
163
- self.max_seq_len_cached = seq_len
164
- t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
165
-
166
- freqs = torch.einsum("i,j->ij", t, self.inv_freq)
167
- # Different from paper, but it uses a different permutation in order to obtain the same calculation
168
- emb = torch.cat((freqs, freqs), dim=-1)
169
- self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
170
- self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
171
-
172
- def forward(self, x, seq_len=None):
173
  # x: [bs, num_attention_heads, seq_len, head_size]
174
- if seq_len > self.max_seq_len_cached:
175
- self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=torch.float32)
 
 
 
 
 
 
 
 
 
 
176
 
177
- return (
178
- self.cos_cached[:seq_len].to(dtype=x.dtype),
179
- self.sin_cached[:seq_len].to(dtype=x.dtype),
180
- )
181
 
182
-
183
- # Copied from transformers.model.llama.modeling_llama.LlamaLinearScalingRotaryEmbedding with Llama->InternLM2
184
  class InternLM2LinearScalingRotaryEmbedding(InternLM2RotaryEmbedding):
185
  """InternLM2RotaryEmbedding extended with linear scaling. Credits to the Reddit user /u/kaiokendev"""
186
 
187
- def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
188
- self.scaling_factor = scaling_factor
189
- super().__init__(dim, max_position_embeddings, base, device)
190
-
191
- def _set_cos_sin_cache(self, seq_len, device, dtype):
192
- self.max_seq_len_cached = seq_len
193
- t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
194
- t = t / self.scaling_factor
195
-
196
- freqs = torch.einsum("i,j->ij", t, self.inv_freq)
197
- # Different from paper, but it uses a different permutation in order to obtain the same calculation
198
- emb = torch.cat((freqs, freqs), dim=-1)
199
- self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
200
- self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
201
 
202
 
203
- # Copied from transformers.model.llama.modeling_llama.LlamaDynamicNTKScalingRotaryEmbedding with Llama->InternLM2
204
  class InternLM2DynamicNTKScalingRotaryEmbedding(InternLM2RotaryEmbedding):
205
  """InternLM2RotaryEmbedding extended with Dynamic NTK scaling.
206
- Credits to the Reddit users /u/bloc97 and /u/emozilla.
207
- """
208
-
209
- def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
210
- self.scaling_factor = scaling_factor
211
- super().__init__(dim, max_position_embeddings, base, device)
212
-
213
- def _set_cos_sin_cache(self, seq_len, device, dtype):
214
- self.max_seq_len_cached = seq_len
215
 
 
 
 
216
  if seq_len > self.max_position_embeddings:
217
  base = self.base * (
218
  (self.scaling_factor * seq_len / self.max_position_embeddings) - (self.scaling_factor - 1)
219
  ) ** (self.dim / (self.dim - 2))
220
- inv_freq = 1.0 / (base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
221
- self.register_buffer("inv_freq", inv_freq, persistent=False)
222
 
223
- t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
 
224
 
225
- freqs = torch.einsum("i,j->ij", t, self.inv_freq)
226
- # Different from paper, but it uses a different permutation in order to obtain the same calculation
227
- emb = torch.cat((freqs, freqs), dim=-1)
228
- self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
229
- self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
230
 
231
-
232
- # Copied from transformers.model.llama.modeling_llama.rotate_half
233
  def rotate_half(x):
234
  """Rotates half the hidden dims of the input."""
235
  x1 = x[..., : x.shape[-1] // 2]
@@ -237,20 +160,35 @@ def rotate_half(x):
237
  return torch.cat((-x2, x1), dim=-1)
238
 
239
 
240
- # Copied from transformers.model.llama.modeling_llama.apply_rotary_pos_emb
241
- def apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1):
242
- """Applies Rotary Position Embedding to the query and key tensors."""
243
- cos = cos[position_ids].unsqueeze(unsqueeze_dim)
244
- sin = sin[position_ids].unsqueeze(unsqueeze_dim)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
  q_embed = (q * cos) + (rotate_half(q) * sin)
246
  k_embed = (k * cos) + (rotate_half(k) * sin)
247
  return q_embed, k_embed
248
 
249
 
250
  class InternLM2MLP(nn.Module):
251
- """
252
- InternLM2 FFN.
253
- """
254
 
255
  def __init__(self, config):
256
  super().__init__()
@@ -268,7 +206,6 @@ class InternLM2MLP(nn.Module):
268
  return down_proj
269
 
270
 
271
- # Copied from transformers.model.llama.modeling_llama.repeat_kv
272
  def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
273
  """
274
  This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
@@ -281,19 +218,27 @@ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
281
  return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
282
 
283
 
284
- # Modified from transformers.model.llama.modeling_llama.LlamaAttention
285
  class InternLM2Attention(nn.Module):
286
  """Multi-headed attention from 'Attention Is All You Need' paper"""
287
 
288
- def __init__(self, config: InternLM2Config):
289
  super().__init__()
290
  self.config = config
 
 
 
 
 
 
 
 
291
  self.hidden_size = config.hidden_size
292
  self.num_heads = config.num_attention_heads
293
  self.head_dim = self.hidden_size // self.num_heads
294
  self.num_key_value_heads = config.num_key_value_heads
295
  self.num_key_value_groups = self.num_heads // self.num_key_value_heads
296
  self.max_position_embeddings = config.max_position_embeddings
 
297
  self.is_causal = True
298
 
299
  if (self.head_dim * self.num_heads) != self.hidden_size:
@@ -307,8 +252,8 @@ class InternLM2Attention(nn.Module):
307
  (self.num_heads + 2 * self.num_key_value_heads) * self.head_dim,
308
  bias=config.bias,
309
  )
310
-
311
  self.wo = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=config.bias)
 
312
  self._init_rope()
313
 
314
  def _init_rope(self):
@@ -316,51 +261,49 @@ class InternLM2Attention(nn.Module):
316
  self.rotary_emb = InternLM2RotaryEmbedding(
317
  self.head_dim,
318
  max_position_embeddings=self.max_position_embeddings,
319
- base=self.config.rope_theta,
320
  )
321
  else:
322
  scaling_type = self.config.rope_scaling["type"]
323
  scaling_factor = self.config.rope_scaling["factor"]
324
- if scaling_type == "dynamic":
325
- self.rotary_emb = InternLM2DynamicNTKScalingRotaryEmbedding(
326
  self.head_dim,
327
  max_position_embeddings=self.max_position_embeddings,
328
- base=self.config.rope_theta,
329
  scaling_factor=scaling_factor,
 
330
  )
331
- elif scaling_type == "linear":
332
- self.rotary_emb = InternLM2LinearScalingRotaryEmbedding(
333
  self.head_dim,
334
  max_position_embeddings=self.max_position_embeddings,
335
- base=self.config.rope_theta,
336
  scaling_factor=scaling_factor,
 
337
  )
338
  else:
339
- raise ValueError("Currently we only support rotary embedding's type being 'dynamic' or 'linear'.")
340
- return self.rotary_emb
341
-
342
- def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int):
343
- return tensor.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2).contiguous()
344
 
345
  def forward(
346
  self,
347
  hidden_states: torch.Tensor,
348
  attention_mask: Optional[torch.Tensor] = None,
349
  position_ids: Optional[torch.LongTensor] = None,
350
- past_key_value: Optional[Tuple[torch.Tensor]] = None,
351
  output_attentions: bool = False,
352
- use_cache: bool = False,
353
- **kwargs,
354
  ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
355
- if "padding_mask" in kwargs:
356
- warnings.warn(
357
- "Passing `padding_mask` is deprecated and will be removed in v4.37. "
358
- "Please make sure use `attention_mask` instead.`"
359
- )
360
-
361
  bsz, q_len, _ = hidden_states.size()
362
 
363
- qkv_states = self.wqkv(hidden_states)
 
 
 
 
 
 
 
 
364
 
365
  qkv_states = rearrange(
366
  qkv_states,
@@ -370,44 +313,26 @@ class InternLM2Attention(nn.Module):
370
  )
371
 
372
  query_states = qkv_states[..., : self.num_key_value_groups, :]
373
- query_states = rearrange(query_states, "b q h gs d -> b q (h gs) d")
374
- key_states = qkv_states[..., -2, :]
375
- value_states = qkv_states[..., -1, :]
376
 
377
- query_states = query_states.transpose(1, 2)
378
- key_states = key_states.transpose(1, 2)
379
- value_states = value_states.transpose(1, 2)
380
-
381
- kv_seq_len = key_states.shape[-2]
382
- if past_key_value is not None:
383
- kv_seq_len += past_key_value[0].shape[-2]
384
- cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
385
  query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
386
 
387
  if past_key_value is not None:
388
- # reuse k, v, self_attention
389
- key_states = torch.cat([past_key_value[0], key_states], dim=2)
390
- value_states = torch.cat([past_key_value[1], value_states], dim=2)
391
-
392
- past_key_value = (key_states, value_states) if use_cache else None
393
 
394
  key_states = repeat_kv(key_states, self.num_key_value_groups)
395
  value_states = repeat_kv(value_states, self.num_key_value_groups)
396
 
397
  attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim)
398
 
399
- if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
400
- raise ValueError(
401
- f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
402
- f" {attn_weights.size()}"
403
- )
404
-
405
- if attention_mask is not None:
406
- if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
407
- raise ValueError(
408
- f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
409
- )
410
- attn_weights = attn_weights + attention_mask
411
 
412
  # upcast attention to fp32
413
  attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
@@ -420,9 +345,20 @@ class InternLM2Attention(nn.Module):
420
  )
421
 
422
  attn_output = attn_output.transpose(1, 2).contiguous()
 
423
  attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
424
 
425
- attn_output = self.wo(attn_output)
 
 
 
 
 
 
 
 
 
 
426
 
427
  if not output_attentions:
428
  attn_weights = None
@@ -430,7 +366,6 @@ class InternLM2Attention(nn.Module):
430
  return attn_output, attn_weights, past_key_value
431
 
432
 
433
- # Modified from transformers.model.llama.modeling_llama.InternLM2FlashAttention2
434
  class InternLM2FlashAttention2(InternLM2Attention):
435
  """
436
  InternLM2 flash attention module. This module inherits from `InternLM2Attention` as the weights of the module stays
@@ -438,26 +373,34 @@ class InternLM2FlashAttention2(InternLM2Attention):
438
  flash attention and deal with padding tokens in case the input contains any of them.
439
  """
440
 
 
 
 
 
 
 
 
 
 
 
 
441
  def forward(
442
  self,
443
  hidden_states: torch.Tensor,
444
  attention_mask: Optional[torch.LongTensor] = None,
445
  position_ids: Optional[torch.LongTensor] = None,
446
- past_key_value: Optional[Tuple[torch.Tensor]] = None,
447
  output_attentions: bool = False,
448
  use_cache: bool = False,
449
- **kwargs,
450
  ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
451
- # InternLM2FlashAttention2 attention does not support output_attentions
452
- if "padding_mask" in kwargs:
453
- warnings.warn(
454
- "Passing `padding_mask` is deprecated and will be removed in v4.37. "
455
- "Please make sure use `attention_mask` instead.`"
456
  )
457
 
458
- # overwrite attention_mask with padding_mask
459
- attention_mask = kwargs.pop("padding_mask")
460
-
461
  output_attentions = False
462
 
463
  bsz, q_len, _ = hidden_states.size()
@@ -480,33 +423,61 @@ class InternLM2FlashAttention2(InternLM2Attention):
480
  key_states = key_states.transpose(1, 2)
481
  value_states = value_states.transpose(1, 2)
482
 
483
- kv_seq_len = key_states.shape[-2]
484
- if past_key_value is not None:
485
- kv_seq_len += past_key_value[0].shape[-2]
486
-
487
- cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
488
-
489
- query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
490
 
491
  if past_key_value is not None:
492
- # reuse k, v, self_attention
493
- key_states = torch.cat([past_key_value[0], key_states], dim=2)
494
- value_states = torch.cat([past_key_value[1], value_states], dim=2)
495
-
496
- past_key_value = (key_states, value_states) if use_cache else None
497
 
 
 
 
498
  query_states = query_states.transpose(1, 2)
499
  key_states = key_states.transpose(1, 2)
500
  value_states = value_states.transpose(1, 2)
501
 
502
- attn_output = self._flash_attention_forward(query_states, key_states, value_states, attention_mask, q_len)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
503
  attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous()
504
  attn_output = self.wo(attn_output)
505
 
506
  if not output_attentions:
507
  attn_weights = None
508
 
509
- return attn_output, attn_weights, past_key_value
510
 
511
  def _flash_attention_forward(
512
  self, query_states, key_states, value_states, attention_mask, query_length, dropout=0.0, softmax_scale=None
@@ -525,23 +496,29 @@ class InternLM2FlashAttention2(InternLM2Attention):
525
  attention_mask (`torch.Tensor`):
526
  The padding mask - corresponds to a tensor of size `(batch_size, seq_len)` where 0 stands for the
527
  position of padding tokens and 1 for the position of non-padding tokens.
528
- dropout (`int`, *optional*):
529
  Attention dropout
530
  softmax_scale (`float`, *optional*):
531
  The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim)
532
  """
 
 
 
 
 
 
 
533
  # Contains at least one padding token in the sequence
534
- causal = self.is_causal and query_length != 1
535
  if attention_mask is not None:
536
  batch_size = query_states.shape[0]
537
- query_states, key_states, value_states, indices_q, cu_seq_lens, max_seq_lens = self._unpad_input(
538
  query_states, key_states, value_states, attention_mask, query_length
539
  )
540
 
541
  cu_seqlens_q, cu_seqlens_k = cu_seq_lens
542
  max_seqlen_in_batch_q, max_seqlen_in_batch_k = max_seq_lens
543
 
544
- attn_output_unpad = flash_attn_varlen_func(
545
  query_states,
546
  key_states,
547
  value_states,
@@ -554,27 +531,26 @@ class InternLM2FlashAttention2(InternLM2Attention):
554
  causal=causal,
555
  )
556
 
557
- attn_output = pad_input(attn_output_unpad, indices_q, batch_size, query_length)
558
  else:
559
- attn_output = flash_attn_func(
560
  query_states, key_states, value_states, dropout, softmax_scale=softmax_scale, causal=causal
561
  )
562
 
563
  return attn_output
564
 
565
- def _unpad_input(self, query_layer, key_layer, value_layer, attention_mask, query_length):
566
  indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask)
567
  batch_size, kv_seq_len, num_key_value_heads, head_dim = key_layer.shape
568
 
569
- key_layer = index_first_axis(
570
  key_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim), indices_k
571
  )
572
- value_layer = index_first_axis(
573
  value_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim), indices_k
574
  )
575
-
576
  if query_length == kv_seq_len:
577
- query_layer = index_first_axis(
578
  query_layer.reshape(batch_size * kv_seq_len, self.num_heads, head_dim), indices_k
579
  )
580
  cu_seqlens_q = cu_seqlens_k
@@ -590,35 +566,139 @@ class InternLM2FlashAttention2(InternLM2Attention):
590
  else:
591
  # The -q_len: slice assumes left padding.
592
  attention_mask = attention_mask[:, -query_length:]
593
- query_layer, indices_q, cu_seqlens_q, max_seqlen_in_batch_q = unpad_input(query_layer, attention_mask)
 
 
594
 
595
  return (
596
  query_layer,
597
  key_layer,
598
  value_layer,
599
- indices_q.to(torch.int64),
600
  (cu_seqlens_q, cu_seqlens_k),
601
  (max_seqlen_in_batch_q, max_seqlen_in_batch_k),
602
  )
603
 
604
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
605
  INTERNLM2_ATTENTION_CLASSES = {
606
  "eager": InternLM2Attention,
607
  "flash_attention_2": InternLM2FlashAttention2,
 
608
  }
609
 
610
 
611
- # Modified from transformers.model.llama.modeling_llama.LlamaDecoderLayer
612
  class InternLM2DecoderLayer(nn.Module):
613
- """
614
- InternLM2 decoder layer.
615
- """
616
 
617
- def __init__(self, config: InternLM2Config):
618
  super().__init__()
619
  self.hidden_size = config.hidden_size
 
620
 
621
- self.attention = INTERNLM2_ATTENTION_CLASSES[config.attn_implementation](config=config)
622
 
623
  self.feed_forward = InternLM2MLP(config)
624
  self.attention_norm = InternLM2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
@@ -629,10 +709,10 @@ class InternLM2DecoderLayer(nn.Module):
629
  hidden_states: torch.Tensor,
630
  attention_mask: Optional[torch.Tensor] = None,
631
  position_ids: Optional[torch.LongTensor] = None,
632
- past_key_value: Optional[Tuple[torch.Tensor]] = None,
633
  output_attentions: Optional[bool] = False,
634
  use_cache: Optional[bool] = False,
635
- **kwargs,
636
  ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
637
  """
638
  Args:
@@ -648,12 +728,6 @@ class InternLM2DecoderLayer(nn.Module):
648
  (see `past_key_values`).
649
  past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
650
  """
651
- if "padding_mask" in kwargs:
652
- warnings.warn(
653
- "Passing `padding_mask` is deprecated and will be removed in v4.37. "
654
- "Please make sure use `attention_mask` instead.`"
655
- )
656
-
657
  residual = hidden_states
658
 
659
  hidden_states = self.attention_norm(hidden_states)
@@ -666,7 +740,7 @@ class InternLM2DecoderLayer(nn.Module):
666
  past_key_value=past_key_value,
667
  output_attentions=output_attentions,
668
  use_cache=use_cache,
669
- **kwargs,
670
  )
671
  hidden_states = residual + hidden_states
672
 
@@ -718,7 +792,12 @@ class InternLM2PreTrainedModel(PreTrainedModel):
718
  base_model_prefix = "model"
719
  supports_gradient_checkpointing = True
720
  _no_split_modules = ["InternLM2DecoderLayer"]
721
- _skip_keys_device_placement = "past_key_values"
 
 
 
 
 
722
 
723
  def _init_weights(self, module):
724
  std = self.config.initializer_range
@@ -767,14 +846,19 @@ InternLM2_INPUTS_DOCSTRING = r"""
767
  config.n_positions - 1]`.
768
 
769
  [What are position IDs?](../glossary#position-ids)
770
- past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or
771
- when `config.use_cache=True`):
772
- Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
773
- `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape
774
- `(batch_size, num_heads, decoder_sequence_length, embed_size_per_head)`.
 
 
 
 
 
775
 
776
- Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
777
- blocks) that can be used (see `past_key_values` input) to speed up sequential decoding.
778
 
779
  If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
780
  have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
@@ -794,10 +878,14 @@ InternLM2_INPUTS_DOCSTRING = r"""
794
  more detail.
795
  return_dict (`bool`, *optional*):
796
  Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
 
 
 
 
797
  """
798
 
799
 
800
- # Modified from transformers.model.llama.modeling_llama.LlamaModel
801
  @add_start_docstrings(
802
  "The bare InternLM2 Model outputting raw hidden-states without any specific head on top.",
803
  InternLM2_START_DOCSTRING,
@@ -820,7 +908,9 @@ class InternLM2Model(InternLM2PreTrainedModel):
820
 
821
  self.tok_embeddings = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
822
 
823
- self.layers = nn.ModuleList([InternLM2DecoderLayer(config) for _ in range(config.num_hidden_layers)])
 
 
824
  self.norm = InternLM2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
825
 
826
  self.gradient_checkpointing = False
@@ -833,142 +923,96 @@ class InternLM2Model(InternLM2PreTrainedModel):
833
  def set_input_embeddings(self, value):
834
  self.tok_embeddings = value
835
 
836
- def _prepare_decoder_attention_mask(self, attention_mask, input_shape, inputs_embeds, past_key_values_length):
837
- # create causal mask
838
- # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
839
- combined_attention_mask = None
840
- if input_shape[-1] > 1:
841
- combined_attention_mask = _make_causal_mask(
842
- input_shape,
843
- inputs_embeds.dtype,
844
- device=inputs_embeds.device,
845
- past_key_values_length=past_key_values_length,
846
- )
847
-
848
- if attention_mask is not None:
849
- # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
850
- expanded_attn_mask = _expand_mask(attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]).to(
851
- inputs_embeds.device
852
- )
853
- combined_attention_mask = (
854
- expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask
855
- )
856
-
857
- return combined_attention_mask
858
-
859
  @add_start_docstrings_to_model_forward(InternLM2_INPUTS_DOCSTRING)
860
  def forward(
861
  self,
862
  input_ids: torch.LongTensor = None,
863
  attention_mask: Optional[torch.Tensor] = None,
864
  position_ids: Optional[torch.LongTensor] = None,
865
- past_key_values: Optional[List[torch.FloatTensor]] = None,
866
  inputs_embeds: Optional[torch.FloatTensor] = None,
867
  use_cache: Optional[bool] = None,
868
  output_attentions: Optional[bool] = None,
869
  output_hidden_states: Optional[bool] = None,
870
  return_dict: Optional[bool] = None,
 
871
  ) -> Union[Tuple, BaseModelOutputWithPast]:
872
  output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
873
  output_hidden_states = (
874
  output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
875
  )
876
  use_cache = use_cache if use_cache is not None else self.config.use_cache
877
-
878
  return_dict = return_dict if return_dict is not None else self.config.use_return_dict
879
 
880
- if self.config.attn_implementation == "flash_attention_2":
881
- _import_flash_attn()
882
-
883
- # retrieve input_ids and inputs_embeds
884
- if input_ids is not None and inputs_embeds is not None:
885
- raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
886
- elif input_ids is not None:
887
- batch_size, seq_length = input_ids.shape[:2]
888
- elif inputs_embeds is not None:
889
- batch_size, seq_length = inputs_embeds.shape[:2]
890
- else:
891
- raise ValueError("You have to specify either input_ids or inputs_embeds")
892
-
893
- seq_length_with_past = seq_length
894
- past_key_values_length = 0
895
- if past_key_values is not None:
896
- past_key_values_length = past_key_values[0][0].shape[2]
897
- seq_length_with_past = seq_length_with_past + past_key_values_length
898
 
899
- if position_ids is None:
900
- device = input_ids.device if input_ids is not None else inputs_embeds.device
901
- position_ids = torch.arange(
902
- past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
903
  )
904
- position_ids = position_ids.unsqueeze(0)
905
 
906
  if inputs_embeds is None:
907
  inputs_embeds = self.tok_embeddings(input_ids)
908
 
909
- if self.config.attn_implementation == "flash_attention_2":
910
- # 2d mask is passed through the layers
911
- attention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None
912
- else:
913
- if attention_mask is None:
914
- attention_mask = torch.ones(
915
- (batch_size, seq_length_with_past), dtype=torch.bool, device=inputs_embeds.device
916
- )
917
- attention_mask = self._prepare_decoder_attention_mask(
918
- attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
919
  )
 
 
 
 
 
 
920
 
921
  # embed positions
922
  hidden_states = inputs_embeds
923
 
924
- if self.gradient_checkpointing and self.training:
925
- if use_cache:
926
- logger.warning_once(
927
- "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
928
- )
929
- use_cache = False
930
-
931
  # decoder layers
932
  all_hidden_states = () if output_hidden_states else None
933
  all_self_attns = () if output_attentions else None
934
- next_decoder_cache = () if use_cache else None
935
 
936
- for idx, decoder_layer in enumerate(self.layers):
937
  if output_hidden_states:
938
  all_hidden_states += (hidden_states,)
939
 
940
- past_key_value = past_key_values[idx] if past_key_values is not None else None
941
-
942
  if self.gradient_checkpointing and self.training:
943
-
944
- def create_custom_forward(module):
945
- def custom_forward(*inputs):
946
- # None for past_key_value
947
- return module(*inputs, output_attentions, None)
948
-
949
- return custom_forward
950
-
951
- layer_outputs = torch.utils.checkpoint.checkpoint(
952
- create_custom_forward(decoder_layer),
953
  hidden_states,
954
- attention_mask,
955
  position_ids,
956
- None,
 
 
 
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_value,
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],)
@@ -980,6 +1024,9 @@ class InternLM2Model(InternLM2PreTrainedModel):
980
  all_hidden_states += (hidden_states,)
981
 
982
  next_cache = next_decoder_cache if use_cache else None
 
 
 
983
  if not return_dict:
984
  return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
985
  return BaseModelOutputWithPast(
@@ -989,15 +1036,91 @@ class InternLM2Model(InternLM2PreTrainedModel):
989
  attentions=all_self_attns,
990
  )
991
 
 
 
 
 
 
 
 
 
 
 
 
 
 
992
 
993
- # Modified from transformers.model.llama.modeling_llama.LlamaForCausalLM
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
994
  class InternLM2ForCausalLM(InternLM2PreTrainedModel):
995
- """
996
- InternLM2 causal language model.
997
- """
998
 
999
  _auto_class = "AutoModelForCausalLM"
1000
-
1001
  _tied_weights_keys = ["output.weight"]
1002
 
1003
  def __init__(self, config):
@@ -1034,13 +1157,14 @@ class InternLM2ForCausalLM(InternLM2PreTrainedModel):
1034
  input_ids: torch.LongTensor = None,
1035
  attention_mask: Optional[torch.Tensor] = None,
1036
  position_ids: Optional[torch.LongTensor] = None,
1037
- past_key_values: Optional[List[torch.FloatTensor]] = None,
1038
  inputs_embeds: Optional[torch.FloatTensor] = None,
1039
  labels: Optional[torch.LongTensor] = None,
1040
  use_cache: Optional[bool] = None,
1041
  output_attentions: Optional[bool] = None,
1042
  output_hidden_states: Optional[bool] = None,
1043
  return_dict: Optional[bool] = None,
 
1044
  ) -> Union[Tuple, CausalLMOutputWithPast]:
1045
  r"""
1046
  Args:
@@ -1056,8 +1180,8 @@ class InternLM2ForCausalLM(InternLM2PreTrainedModel):
1056
  ```python
1057
  >>> from transformers import AutoTokenizer, InternLM2ForCausalLM
1058
 
1059
- >>> model = InternLM2ForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
1060
- >>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
1061
 
1062
  >>> prompt = "Hey, are you conscious? Can you talk to me?"
1063
  >>> inputs = tokenizer(prompt, return_tensors="pt")
@@ -1085,10 +1209,19 @@ class InternLM2ForCausalLM(InternLM2PreTrainedModel):
1085
  output_attentions=output_attentions,
1086
  output_hidden_states=output_hidden_states,
1087
  return_dict=return_dict,
 
1088
  )
1089
 
1090
  hidden_states = outputs[0]
1091
- logits = self.output(hidden_states)
 
 
 
 
 
 
 
 
1092
  logits = logits.float()
1093
 
1094
  loss = None
@@ -1117,19 +1250,48 @@ class InternLM2ForCausalLM(InternLM2PreTrainedModel):
1117
  )
1118
 
1119
  def prepare_inputs_for_generation(
1120
- self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
 
 
 
 
 
 
 
1121
  ):
 
1122
  if past_key_values is not None:
1123
- past_length = past_key_values[0][0].shape[2]
1124
-
1125
- # Some generation methods already pass only the last input ID
1126
- if input_ids.shape[1] > past_length:
1127
- remove_prefix_length = past_length
 
 
 
 
1128
  else:
1129
- # Default to old behavior: keep only final ID
1130
- remove_prefix_length = input_ids.shape[1] - 1
1131
-
1132
- input_ids = input_ids[:, remove_prefix_length:]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1133
 
1134
  position_ids = kwargs.get("position_ids", None)
1135
  if attention_mask is not None and position_ids is None:
@@ -1143,13 +1305,24 @@ class InternLM2ForCausalLM(InternLM2PreTrainedModel):
1143
  if inputs_embeds is not None and past_key_values is None:
1144
  model_inputs = {"inputs_embeds": inputs_embeds}
1145
  else:
1146
- model_inputs = {"input_ids": input_ids}
 
 
 
 
 
 
 
 
 
 
1147
 
1148
  model_inputs.update(
1149
  {
1150
  "position_ids": position_ids,
 
1151
  "past_key_values": past_key_values,
1152
- "use_cache": kwargs.get("use_cache"),
1153
  "attention_mask": attention_mask,
1154
  }
1155
  )
@@ -1311,13 +1484,13 @@ class InternLM2ForCausalLM(InternLM2PreTrainedModel):
1311
  return consumer()
1312
 
1313
 
1314
- # Copied from transformers.model.llama.modeling_llama.LlamaForSequenceClassification with Llama->InternLM2
1315
  @add_start_docstrings(
1316
  """
1317
  The InternLM2 Model transformer with a sequence classification head on top (linear layer).
1318
 
1319
- [`InternLM2ForSequenceClassification`] uses the last token in order to do the classification,
1320
- as other causal models (e.g. GPT-2) do.
1321
 
1322
  Since it does classification on the last token, it requires to know the position of the last token. If a
1323
  `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
@@ -1328,9 +1501,7 @@ class InternLM2ForCausalLM(InternLM2PreTrainedModel):
1328
  InternLM2_START_DOCSTRING,
1329
  )
1330
  class InternLM2ForSequenceClassification(InternLM2PreTrainedModel):
1331
- """
1332
- InternLM2 sequence classification model.
1333
- """
1334
 
1335
  def __init__(self, config):
1336
  super().__init__(config)
@@ -1353,7 +1524,7 @@ class InternLM2ForSequenceClassification(InternLM2PreTrainedModel):
1353
  input_ids: torch.LongTensor = None,
1354
  attention_mask: Optional[torch.Tensor] = None,
1355
  position_ids: Optional[torch.LongTensor] = None,
1356
- past_key_values: Optional[List[torch.FloatTensor]] = None,
1357
  inputs_embeds: Optional[torch.FloatTensor] = None,
1358
  labels: Optional[torch.LongTensor] = None,
1359
  use_cache: Optional[bool] = None,
@@ -1394,9 +1565,10 @@ class InternLM2ForSequenceClassification(InternLM2PreTrainedModel):
1394
  sequence_lengths = -1
1395
  else:
1396
  if input_ids is not None:
1397
- sequence_lengths = (torch.eq(input_ids, self.config.pad_token_id).int().argmax(-1) - 1).to(
1398
- logits.device
1399
- )
 
1400
  else:
1401
  sequence_lengths = -1
1402
 
@@ -1408,8 +1580,7 @@ class InternLM2ForSequenceClassification(InternLM2PreTrainedModel):
1408
  if self.config.problem_type is None:
1409
  if self.num_labels == 1:
1410
  self.config.problem_type = "regression"
1411
-
1412
- elif self.num_labels > 1 and labels.dtype in (torch.long, torch.int):
1413
  self.config.problem_type = "single_label_classification"
1414
  else:
1415
  self.config.problem_type = "multi_label_classification"
@@ -1437,3 +1608,191 @@ class InternLM2ForSequenceClassification(InternLM2PreTrainedModel):
1437
  hidden_states=transformer_outputs.hidden_states,
1438
  attentions=transformer_outputs.attentions,
1439
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
  # See the License for the specific language governing permissions and
15
  # limitations under the License.
16
+ """PyTorch InternLM2 model."""
17
  import math
18
  import queue
19
  import threading
 
20
  from typing import List, Optional, Tuple, Union
21
 
22
  import torch
 
26
  from torch import nn
27
  from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
28
  from transformers.activations import ACT2FN
29
+ from transformers.cache_utils import Cache, DynamicCache, StaticCache
30
+ from transformers.modeling_attn_mask_utils import AttentionMaskConverter
31
  from transformers.modeling_outputs import (
32
  BaseModelOutputWithPast,
33
  CausalLMOutputWithPast,
34
+ QuestionAnsweringModelOutput,
35
  SequenceClassifierOutputWithPast,
36
+ TokenClassifierOutput,
37
  )
38
  from transformers.modeling_utils import PreTrainedModel
39
+ from transformers.pytorch_utils import ALL_LAYERNORM_LAYERS
40
  from transformers.utils import (
41
  add_start_docstrings,
42
  add_start_docstrings_to_model_forward,
43
+ is_flash_attn_2_available,
44
+ is_flash_attn_greater_or_equal_2_10,
45
  logging,
46
  replace_return_docstrings,
47
  )
 
53
 
54
  from .configuration_internlm2 import InternLM2Config
55
 
56
+ if is_flash_attn_2_available():
57
+ from flash_attn import flash_attn_func, flash_attn_varlen_func
58
+ from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input
59
 
 
 
60
 
61
+ logger = logging.get_logger(__name__)
62
 
63
+ _CONFIG_FOR_DOC = "InternLM2Config"
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
 
 
66
  def _get_unpad_data(attention_mask):
67
  seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
68
  indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
69
  max_seqlen_in_batch = seqlens_in_batch.max().item()
70
+ cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.int32), (1, 0)) # pylint: disable=E1102
71
  return (
72
  indices,
73
  cu_seqlens,
 
75
  )
76
 
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  class InternLM2RMSNorm(nn.Module):
79
+ """InternLM2RMSNorm is equivalent to T5LayerNorm."""
 
 
80
 
81
  def __init__(self, hidden_size, eps=1e-6):
 
 
 
82
  super().__init__()
83
  self.weight = nn.Parameter(torch.ones(hidden_size))
84
  self.variance_epsilon = eps
 
91
  return self.weight * hidden_states.to(input_dtype)
92
 
93
 
94
+ ALL_LAYERNORM_LAYERS.append(InternLM2RMSNorm)
95
+
96
+
97
  class InternLM2RotaryEmbedding(nn.Module):
98
+ """Rotary Position Embedding for the InternLM2 model. Credits to the Reddit user /u/lucidrains."""
 
 
99
 
100
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
101
  super().__init__()
102
+ self.scaling_factor = scaling_factor
103
  self.dim = dim
104
  self.max_position_embeddings = max_position_embeddings
105
  self.base = base
106
+ inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2, dtype=torch.int64).float().to(device) / self.dim))
107
  self.register_buffer("inv_freq", inv_freq, persistent=False)
108
+ # For BC we register cos and sin cached
109
+ self.max_seq_len_cached = max_position_embeddings
110
 
111
+ @torch.no_grad()
112
+ def forward(self, x, position_ids):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  # x: [bs, num_attention_heads, seq_len, head_size]
114
+ inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1)
115
+ position_ids_expanded = position_ids[:, None, :].float()
116
+ # Force float32 since bfloat16 loses precision on long contexts
117
+ # See https://github.com/huggingface/transformers/pull/29285
118
+ device_type = x.device.type
119
+ device_type = device_type if isinstance(device_type, str) and device_type != "mps" else "cpu"
120
+ with torch.autocast(device_type=device_type, enabled=False):
121
+ freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)
122
+ emb = torch.cat((freqs, freqs), dim=-1)
123
+ cos = emb.cos()
124
+ sin = emb.sin()
125
+ return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)
126
 
 
 
 
 
127
 
 
 
128
  class InternLM2LinearScalingRotaryEmbedding(InternLM2RotaryEmbedding):
129
  """InternLM2RotaryEmbedding extended with linear scaling. Credits to the Reddit user /u/kaiokendev"""
130
 
131
+ def forward(self, x, position_ids):
132
+ # difference to the original RoPE: a scaling factor is aplied to the position ids
133
+ position_ids = position_ids.float() / self.scaling_factor
134
+ cos, sin = super().forward(x, position_ids)
135
+ return cos, sin
 
 
 
 
 
 
 
 
 
136
 
137
 
 
138
  class InternLM2DynamicNTKScalingRotaryEmbedding(InternLM2RotaryEmbedding):
139
  """InternLM2RotaryEmbedding extended with Dynamic NTK scaling.
140
+ Credits to the Reddit users /u/bloc97 and /u/emozilla"""
 
 
 
 
 
 
 
 
141
 
142
+ def forward(self, x, position_ids):
143
+ # difference to the original RoPE: inv_freq is recomputed when the sequence length > original length
144
+ seq_len = torch.max(position_ids) + 1
145
  if seq_len > self.max_position_embeddings:
146
  base = self.base * (
147
  (self.scaling_factor * seq_len / self.max_position_embeddings) - (self.scaling_factor - 1)
148
  ) ** (self.dim / (self.dim - 2))
149
+ inv_freq = 1.0 / (base ** (torch.arange(0, self.dim, 2, dtype=torch.int64).float().to(x.device) / self.dim))
150
+ self.register_buffer("inv_freq", inv_freq, persistent=False) # TODO joao: this may break with compilation
151
 
152
+ cos, sin = super().forward(x, position_ids)
153
+ return cos, sin
154
 
 
 
 
 
 
155
 
 
 
156
  def rotate_half(x):
157
  """Rotates half the hidden dims of the input."""
158
  x1 = x[..., : x.shape[-1] // 2]
 
160
  return torch.cat((-x2, x1), dim=-1)
161
 
162
 
163
+ def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1): # pylint: disable=unused-argument
164
+ """Applies Rotary Position Embedding to the query and key tensors.
165
+
166
+ Args:
167
+ q (`torch.Tensor`): The query tensor.
168
+ k (`torch.Tensor`): The key tensor.
169
+ cos (`torch.Tensor`): The cosine part of the rotary embedding.
170
+ sin (`torch.Tensor`): The sine part of the rotary embedding.
171
+ position_ids (`torch.Tensor`, *optional*):
172
+ Deprecated and unused.
173
+ unsqueeze_dim (`int`, *optional*, defaults to 1):
174
+ The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
175
+ sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
176
+ that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
177
+ k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
178
+ cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
179
+ the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
180
+ Returns:
181
+ `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
182
+ """
183
+ cos = cos.unsqueeze(unsqueeze_dim)
184
+ sin = sin.unsqueeze(unsqueeze_dim)
185
  q_embed = (q * cos) + (rotate_half(q) * sin)
186
  k_embed = (k * cos) + (rotate_half(k) * sin)
187
  return q_embed, k_embed
188
 
189
 
190
  class InternLM2MLP(nn.Module):
191
+ """MLP for InternLM2 model."""
 
 
192
 
193
  def __init__(self, config):
194
  super().__init__()
 
206
  return down_proj
207
 
208
 
 
209
  def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
210
  """
211
  This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
 
218
  return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
219
 
220
 
 
221
  class InternLM2Attention(nn.Module):
222
  """Multi-headed attention from 'Attention Is All You Need' paper"""
223
 
224
+ def __init__(self, config: InternLM2Config, layer_idx: Optional[int] = None):
225
  super().__init__()
226
  self.config = config
227
+ self.layer_idx = layer_idx
228
+ if layer_idx is None:
229
+ logger.warning_once(
230
+ f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will "
231
+ "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` "
232
+ "when creating this class."
233
+ )
234
+
235
  self.hidden_size = config.hidden_size
236
  self.num_heads = config.num_attention_heads
237
  self.head_dim = self.hidden_size // self.num_heads
238
  self.num_key_value_heads = config.num_key_value_heads
239
  self.num_key_value_groups = self.num_heads // self.num_key_value_heads
240
  self.max_position_embeddings = config.max_position_embeddings
241
+ self.rope_theta = config.rope_theta
242
  self.is_causal = True
243
 
244
  if (self.head_dim * self.num_heads) != self.hidden_size:
 
252
  (self.num_heads + 2 * self.num_key_value_heads) * self.head_dim,
253
  bias=config.bias,
254
  )
 
255
  self.wo = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=config.bias)
256
+
257
  self._init_rope()
258
 
259
  def _init_rope(self):
 
261
  self.rotary_emb = InternLM2RotaryEmbedding(
262
  self.head_dim,
263
  max_position_embeddings=self.max_position_embeddings,
264
+ base=self.rope_theta,
265
  )
266
  else:
267
  scaling_type = self.config.rope_scaling["type"]
268
  scaling_factor = self.config.rope_scaling["factor"]
269
+ if scaling_type == "linear":
270
+ self.rotary_emb = InternLM2LinearScalingRotaryEmbedding(
271
  self.head_dim,
272
  max_position_embeddings=self.max_position_embeddings,
 
273
  scaling_factor=scaling_factor,
274
+ base=self.rope_theta,
275
  )
276
+ elif scaling_type == "dynamic":
277
+ self.rotary_emb = InternLM2DynamicNTKScalingRotaryEmbedding(
278
  self.head_dim,
279
  max_position_embeddings=self.max_position_embeddings,
 
280
  scaling_factor=scaling_factor,
281
+ base=self.rope_theta,
282
  )
283
  else:
284
+ raise ValueError(f"Unknown RoPE scaling type {scaling_type}")
 
 
 
 
285
 
286
  def forward(
287
  self,
288
  hidden_states: torch.Tensor,
289
  attention_mask: Optional[torch.Tensor] = None,
290
  position_ids: Optional[torch.LongTensor] = None,
291
+ past_key_value: Optional[Cache] = None,
292
  output_attentions: bool = False,
293
+ use_cache: bool = False, # pylint: disable=unused-argument
294
+ cache_position: Optional[torch.LongTensor] = None,
295
  ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
 
 
 
 
 
 
296
  bsz, q_len, _ = hidden_states.size()
297
 
298
+ if self.config.pretraining_tp > 1:
299
+ # split qkv_states by tp size
300
+ key_value_slicing = (self.num_key_value_heads * self.head_dim) // self.config.pretraining_tp
301
+ qkv_slices = self.wqkv.weight.split(key_value_slicing, dim=0)
302
+ qkv_states = torch.cat(
303
+ [F.linear(hidden_states, qkv_slice) for qkv_slice in qkv_slices], dim=-1 # pylint: disable=E1102
304
+ )
305
+ else:
306
+ qkv_states = self.wqkv(hidden_states)
307
 
308
  qkv_states = rearrange(
309
  qkv_states,
 
313
  )
314
 
315
  query_states = qkv_states[..., : self.num_key_value_groups, :]
316
+ query_states = rearrange(query_states, "b q h gs d -> b q (h gs) d").transpose(1, 2)
317
+ key_states = qkv_states[..., -2, :].transpose(1, 2)
318
+ value_states = qkv_states[..., -1, :].transpose(1, 2)
319
 
320
+ cos, sin = self.rotary_emb(value_states, position_ids)
 
 
 
 
 
 
 
321
  query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
322
 
323
  if past_key_value is not None:
324
+ # sin and cos are specific to RoPE models; cache_position needed for the static cache
325
+ cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}
326
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
 
 
327
 
328
  key_states = repeat_kv(key_states, self.num_key_value_groups)
329
  value_states = repeat_kv(value_states, self.num_key_value_groups)
330
 
331
  attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim)
332
 
333
+ if attention_mask is not None: # no matter the length, we just slice it
334
+ causal_mask = attention_mask[:, :, :, : key_states.shape[-2]]
335
+ attn_weights = attn_weights + causal_mask
 
 
 
 
 
 
 
 
 
336
 
337
  # upcast attention to fp32
338
  attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
 
345
  )
346
 
347
  attn_output = attn_output.transpose(1, 2).contiguous()
348
+
349
  attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
350
 
351
+ if self.config.pretraining_tp > 1:
352
+ attn_output = attn_output.split(self.hidden_size // self.config.pretraining_tp, dim=2)
353
+ o_proj_slices = self.wo.weight.split(self.hidden_size // self.config.pretraining_tp, dim=1)
354
+ attn_output = sum(
355
+ [
356
+ F.linear(attn_output[i], o_proj_slices[i]) # pylint: disable=E1102
357
+ for i in range(self.config.pretraining_tp)
358
+ ]
359
+ )
360
+ else:
361
+ attn_output = self.wo(attn_output)
362
 
363
  if not output_attentions:
364
  attn_weights = None
 
366
  return attn_output, attn_weights, past_key_value
367
 
368
 
 
369
  class InternLM2FlashAttention2(InternLM2Attention):
370
  """
371
  InternLM2 flash attention module. This module inherits from `InternLM2Attention` as the weights of the module stays
 
373
  flash attention and deal with padding tokens in case the input contains any of them.
374
  """
375
 
376
+ def __init__(self, *args, **kwargs):
377
+ super().__init__(*args, **kwargs)
378
+
379
+ # TODO: Should be removed once Flash Attention for RoCm is bumped to 2.1.
380
+ # flash_attn<2.1 generates top-left aligned causal mask, while what is needed here is bottom-right alignement,
381
+ # that was made default for flash_attn>=2.1. This attribute is used to handle this difference.
382
+ # Reference: https://github.com/Dao-AILab/flash-attention/releases/tag/v2.1.0.
383
+ # Beware that with flash_attn<2.1, using q_seqlen != k_seqlen (except for the case q_seqlen == 1)
384
+ # produces a wrong mask (top-left).
385
+ self._flash_attn_uses_top_left_mask = not is_flash_attn_greater_or_equal_2_10()
386
+
387
  def forward(
388
  self,
389
  hidden_states: torch.Tensor,
390
  attention_mask: Optional[torch.LongTensor] = None,
391
  position_ids: Optional[torch.LongTensor] = None,
392
+ past_key_value: Optional[Cache] = None,
393
  output_attentions: bool = False,
394
  use_cache: bool = False,
395
+ cache_position: Optional[torch.LongTensor] = None,
396
  ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
397
+ if isinstance(past_key_value, StaticCache):
398
+ raise ValueError(
399
+ "`static` cache implementation is not compatible with `attn_implementation==flash_attention_2` "
400
+ "make sure to use `sdpa` in the mean time, and open an issue at "
401
+ "https://github.com/huggingface/transformers"
402
  )
403
 
 
 
 
404
  output_attentions = False
405
 
406
  bsz, q_len, _ = hidden_states.size()
 
423
  key_states = key_states.transpose(1, 2)
424
  value_states = value_states.transpose(1, 2)
425
 
426
+ cos, sin = self.rotary_emb(value_states, position_ids)
427
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
 
 
 
 
 
428
 
429
  if past_key_value is not None:
430
+ # sin and cos are specific to RoPE models; cache_position needed for the static cache
431
+ cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}
432
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
 
 
433
 
434
+ # TODO: These transpose are quite inefficient but Flash Attention requires the layout
435
+ # [batch_size, sequence_length, num_heads, head_dim]. We would need to refactor the KV cache
436
+ # to be able to avoid many of these transpose/reshape/view.
437
  query_states = query_states.transpose(1, 2)
438
  key_states = key_states.transpose(1, 2)
439
  value_states = value_states.transpose(1, 2)
440
 
441
+ # dropout_rate = self.attention_dropout if self.training else 0.0
442
+ dropout_rate = 0.0
443
+
444
+ # In PEFT, usually we cast the layer norms in float32 for training stability reasons
445
+ # therefore the input hidden states gets silently casted in float32. Hence, we need
446
+ # cast them back in the correct dtype just to be sure everything works as expected.
447
+ # This might slowdown training & inference so it is recommended to not cast the LayerNorms
448
+ # in fp32. (InternLM2RMSNorm handles it correctly)
449
+
450
+ input_dtype = query_states.dtype
451
+ if input_dtype == torch.float32:
452
+ if torch.is_autocast_enabled():
453
+ target_dtype = torch.get_autocast_gpu_dtype()
454
+ # Handle the case where the model is quantized
455
+ elif hasattr(self.config, "_pre_quantization_dtype"):
456
+ target_dtype = self.config._pre_quantization_dtype
457
+ else:
458
+ target_dtype = self.wqkv.weight.dtype
459
+
460
+ logger.warning_once(
461
+ f"The input hidden states seems to be silently casted in float32, this might be related to"
462
+ f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in"
463
+ f" {target_dtype}."
464
+ )
465
+
466
+ query_states = query_states.to(target_dtype)
467
+ key_states = key_states.to(target_dtype)
468
+ value_states = value_states.to(target_dtype)
469
+
470
+ attn_output = self._flash_attention_forward(
471
+ query_states, key_states, value_states, attention_mask, q_len, dropout=dropout_rate
472
+ )
473
+
474
  attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous()
475
  attn_output = self.wo(attn_output)
476
 
477
  if not output_attentions:
478
  attn_weights = None
479
 
480
+ return attn_output, attn_weights, past_key_value # pylint: disable=E0606
481
 
482
  def _flash_attention_forward(
483
  self, query_states, key_states, value_states, attention_mask, query_length, dropout=0.0, softmax_scale=None
 
496
  attention_mask (`torch.Tensor`):
497
  The padding mask - corresponds to a tensor of size `(batch_size, seq_len)` where 0 stands for the
498
  position of padding tokens and 1 for the position of non-padding tokens.
499
+ dropout (`float`):
500
  Attention dropout
501
  softmax_scale (`float`, *optional*):
502
  The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim)
503
  """
504
+ if not self._flash_attn_uses_top_left_mask:
505
+ causal = self.is_causal
506
+ else:
507
+ # TODO: Remove the `query_length != 1` check once Flash Attention for RoCm is bumped to 2.1.
508
+ # For details, please see the comment in InternLM2FlashAttention2 __init__.
509
+ causal = self.is_causal and query_length != 1
510
+
511
  # Contains at least one padding token in the sequence
 
512
  if attention_mask is not None:
513
  batch_size = query_states.shape[0]
514
+ query_states, key_states, value_states, indices_q, cu_seq_lens, max_seq_lens = self._upad_input(
515
  query_states, key_states, value_states, attention_mask, query_length
516
  )
517
 
518
  cu_seqlens_q, cu_seqlens_k = cu_seq_lens
519
  max_seqlen_in_batch_q, max_seqlen_in_batch_k = max_seq_lens
520
 
521
+ attn_output_unpad = flash_attn_varlen_func( # pylint: disable=E0606
522
  query_states,
523
  key_states,
524
  value_states,
 
531
  causal=causal,
532
  )
533
 
534
+ attn_output = pad_input(attn_output_unpad, indices_q, batch_size, query_length) # pylint: disable=E0606
535
  else:
536
+ attn_output = flash_attn_func( # pylint: disable=E0606
537
  query_states, key_states, value_states, dropout, softmax_scale=softmax_scale, causal=causal
538
  )
539
 
540
  return attn_output
541
 
542
+ def _upad_input(self, query_layer, key_layer, value_layer, attention_mask, query_length):
543
  indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask)
544
  batch_size, kv_seq_len, num_key_value_heads, head_dim = key_layer.shape
545
 
546
+ key_layer = index_first_axis( # pylint: disable=E0606
547
  key_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim), indices_k
548
  )
549
+ value_layer = index_first_axis( # pylint: disable=E0606
550
  value_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim), indices_k
551
  )
 
552
  if query_length == kv_seq_len:
553
+ query_layer = index_first_axis( # pylint: disable=E0606
554
  query_layer.reshape(batch_size * kv_seq_len, self.num_heads, head_dim), indices_k
555
  )
556
  cu_seqlens_q = cu_seqlens_k
 
566
  else:
567
  # The -q_len: slice assumes left padding.
568
  attention_mask = attention_mask[:, -query_length:]
569
+ query_layer, indices_q, cu_seqlens_q, max_seqlen_in_batch_q = unpad_input( # pylint: disable=E0606
570
+ query_layer, attention_mask
571
+ )
572
 
573
  return (
574
  query_layer,
575
  key_layer,
576
  value_layer,
577
+ indices_q,
578
  (cu_seqlens_q, cu_seqlens_k),
579
  (max_seqlen_in_batch_q, max_seqlen_in_batch_k),
580
  )
581
 
582
 
583
+ # Copied from transformers.models.llama.modeling_llama.LllamaSdpaAttention with Llama->InternLM2
584
+ class InternLM2SdpaAttention(InternLM2Attention):
585
+ """
586
+ InternLM2 attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from
587
+ `InternLM2Attention` as the weights of the module stays untouched. The only changes are on the forward pass
588
+ to adapt to SDPA API.
589
+ """
590
+
591
+ # Adapted from InternLM2Attention.forward
592
+ def forward(
593
+ self,
594
+ hidden_states: torch.Tensor,
595
+ attention_mask: Optional[torch.Tensor] = None,
596
+ position_ids: Optional[torch.LongTensor] = None,
597
+ past_key_value: Optional[Cache] = None,
598
+ output_attentions: bool = False,
599
+ use_cache: bool = False,
600
+ cache_position: Optional[torch.LongTensor] = None,
601
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
602
+ if output_attentions:
603
+ # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"`
604
+ # once this is implemented.
605
+ logger.warning_once(
606
+ "InternLM2Model uses InternLM2SdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` "
607
+ "does not support `output_attentions=True`. Falling back to the manual attention implementation, "
608
+ "but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. "
609
+ 'This warning can be removed using the argument `attn_implementation="eager"` when loading the model.'
610
+ )
611
+ return super().forward(
612
+ hidden_states=hidden_states,
613
+ attention_mask=attention_mask,
614
+ position_ids=position_ids,
615
+ past_key_value=past_key_value,
616
+ output_attentions=output_attentions,
617
+ use_cache=use_cache,
618
+ cache_position=cache_position,
619
+ )
620
+
621
+ bsz, q_len, _ = hidden_states.size()
622
+
623
+ qkv_states = self.wqkv(hidden_states)
624
+
625
+ qkv_states = rearrange(
626
+ qkv_states,
627
+ "b q (h gs d) -> b q h gs d",
628
+ gs=2 + self.num_key_value_groups,
629
+ d=self.head_dim,
630
+ )
631
+
632
+ query_states = qkv_states[..., : self.num_key_value_groups, :]
633
+ query_states = rearrange(query_states, "b q h gs d -> b q (h gs) d")
634
+ key_states = qkv_states[..., -2, :]
635
+ value_states = qkv_states[..., -1, :]
636
+
637
+ query_states = query_states.transpose(1, 2)
638
+ key_states = key_states.transpose(1, 2)
639
+ value_states = value_states.transpose(1, 2)
640
+
641
+ cos, sin = self.rotary_emb(value_states, position_ids)
642
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
643
+
644
+ if past_key_value is not None:
645
+ # sin and cos are specific to RoPE models; cache_position needed for the static cache
646
+ cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}
647
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
648
+
649
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
650
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
651
+
652
+ causal_mask = attention_mask
653
+ if attention_mask is not None:
654
+ causal_mask = causal_mask[:, :, :, : key_states.shape[-2]]
655
+
656
+ # SDPA with memory-efficient backend is currently (torch==2.1.2) bugged with non-contiguous inputs with
657
+ # custom attn_mask, Reference: https://github.com/pytorch/pytorch/issues/112577.
658
+ if query_states.device.type == "cuda" and causal_mask is not None:
659
+ query_states = query_states.contiguous()
660
+ key_states = key_states.contiguous()
661
+ value_states = value_states.contiguous()
662
+
663
+ # We dispatch to SDPA's Flash Attention or Efficient kernels via this `is_causal` if statement instead of
664
+ # an inline conditional assignment in SDPA to support both torch.compile's dynamic shapes and full graph
665
+ # options. An inline conditional prevents dynamic shapes from compiling.
666
+ is_causal = bool(causal_mask is None and q_len > 1)
667
+
668
+ attn_output = torch.nn.functional.scaled_dot_product_attention( # pylint: disable=E1102
669
+ query_states,
670
+ key_states,
671
+ value_states,
672
+ attn_mask=causal_mask,
673
+ dropout_p=0.0,
674
+ is_causal=is_causal,
675
+ )
676
+
677
+ attn_output = attn_output.transpose(1, 2).contiguous()
678
+ attn_output = attn_output.view(bsz, q_len, self.hidden_size)
679
+
680
+ attn_output = self.wo(attn_output)
681
+
682
+ return attn_output, None, past_key_value
683
+
684
+
685
  INTERNLM2_ATTENTION_CLASSES = {
686
  "eager": InternLM2Attention,
687
  "flash_attention_2": InternLM2FlashAttention2,
688
+ "sdpa": InternLM2SdpaAttention,
689
  }
690
 
691
 
692
+ # Modified from transformers.models.llama.modeling_llama.LlamaDecoderLayer with Llama->InternLM2
693
  class InternLM2DecoderLayer(nn.Module):
694
+ """InternLM2 Decoder Layer. This module is a single layer of the InternLM2 model."""
 
 
695
 
696
+ def __init__(self, config: InternLM2Config, layer_idx: int):
697
  super().__init__()
698
  self.hidden_size = config.hidden_size
699
+ self.layer_idx = layer_idx
700
 
701
+ self.attention = INTERNLM2_ATTENTION_CLASSES[config.attn_implementation](config=config, layer_idx=layer_idx)
702
 
703
  self.feed_forward = InternLM2MLP(config)
704
  self.attention_norm = InternLM2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
 
709
  hidden_states: torch.Tensor,
710
  attention_mask: Optional[torch.Tensor] = None,
711
  position_ids: Optional[torch.LongTensor] = None,
712
+ past_key_value: Optional[Cache] = None,
713
  output_attentions: Optional[bool] = False,
714
  use_cache: Optional[bool] = False,
715
+ cache_position: Optional[torch.LongTensor] = None,
716
  ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
717
  """
718
  Args:
 
728
  (see `past_key_values`).
729
  past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
730
  """
 
 
 
 
 
 
731
  residual = hidden_states
732
 
733
  hidden_states = self.attention_norm(hidden_states)
 
740
  past_key_value=past_key_value,
741
  output_attentions=output_attentions,
742
  use_cache=use_cache,
743
+ cache_position=cache_position,
744
  )
745
  hidden_states = residual + hidden_states
746
 
 
792
  base_model_prefix = "model"
793
  supports_gradient_checkpointing = True
794
  _no_split_modules = ["InternLM2DecoderLayer"]
795
+ _skip_keys_device_placement = ["past_key_values"]
796
+ _supports_flash_attn_2 = True
797
+ _supports_sdpa = True
798
+ _supports_cache_class = True
799
+ _supports_quantized_cache = True
800
+ _supports_static_cache = True
801
 
802
  def _init_weights(self, module):
803
  std = self.config.initializer_range
 
846
  config.n_positions - 1]`.
847
 
848
  [What are position IDs?](../glossary#position-ids)
849
+ past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):
850
+ Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
851
+ blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
852
+ returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.
853
+
854
+ Two formats are allowed:
855
+ - a [`~cache_utils.Cache`] instance;
856
+ - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
857
+ shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy
858
+ cache format.
859
 
860
+ The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the
861
+ legacy cache format will be returned.
862
 
863
  If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
864
  have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
 
878
  more detail.
879
  return_dict (`bool`, *optional*):
880
  Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
881
+ cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
882
+ Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`,
883
+ this tensor is not affected by padding. It is used to update the cache in the correct position and to infer
884
+ the complete sequence length.
885
  """
886
 
887
 
888
+ # Modified from transformers.models.llama.modeling_llama.LlamaModel with Llama->InternLM2
889
  @add_start_docstrings(
890
  "The bare InternLM2 Model outputting raw hidden-states without any specific head on top.",
891
  InternLM2_START_DOCSTRING,
 
908
 
909
  self.tok_embeddings = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
910
 
911
+ self.layers = nn.ModuleList(
912
+ [InternLM2DecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
913
+ )
914
  self.norm = InternLM2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
915
 
916
  self.gradient_checkpointing = False
 
923
  def set_input_embeddings(self, value):
924
  self.tok_embeddings = value
925
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
926
  @add_start_docstrings_to_model_forward(InternLM2_INPUTS_DOCSTRING)
927
  def forward(
928
  self,
929
  input_ids: torch.LongTensor = None,
930
  attention_mask: Optional[torch.Tensor] = None,
931
  position_ids: Optional[torch.LongTensor] = None,
932
+ past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
933
  inputs_embeds: Optional[torch.FloatTensor] = None,
934
  use_cache: Optional[bool] = None,
935
  output_attentions: Optional[bool] = None,
936
  output_hidden_states: Optional[bool] = None,
937
  return_dict: Optional[bool] = None,
938
+ cache_position: Optional[torch.LongTensor] = None,
939
  ) -> Union[Tuple, BaseModelOutputWithPast]:
940
  output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
941
  output_hidden_states = (
942
  output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
943
  )
944
  use_cache = use_cache if use_cache is not None else self.config.use_cache
 
945
  return_dict = return_dict if return_dict is not None else self.config.use_return_dict
946
 
947
+ if (input_ids is None) ^ (inputs_embeds is not None):
948
+ raise ValueError(
949
+ "You cannot specify both input_ids and inputs_embeds at the same time, and must specify either one"
950
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
951
 
952
+ if self.gradient_checkpointing and self.training and use_cache:
953
+ logger.warning_once(
954
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`."
 
955
  )
956
+ use_cache = False
957
 
958
  if inputs_embeds is None:
959
  inputs_embeds = self.tok_embeddings(input_ids)
960
 
961
+ return_legacy_cache = False
962
+ if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs)
963
+ return_legacy_cache = True
964
+ past_key_values = DynamicCache.from_legacy_cache(past_key_values)
965
+
966
+ if cache_position is None:
967
+ past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
968
+ cache_position = torch.arange(
969
+ past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device
 
970
  )
971
+ if position_ids is None:
972
+ position_ids = cache_position.unsqueeze(0)
973
+
974
+ causal_mask = self._update_causal_mask(
975
+ attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions
976
+ )
977
 
978
  # embed positions
979
  hidden_states = inputs_embeds
980
 
 
 
 
 
 
 
 
981
  # decoder layers
982
  all_hidden_states = () if output_hidden_states else None
983
  all_self_attns = () if output_attentions else None
984
+ next_decoder_cache = None
985
 
986
+ for decoder_layer in self.layers:
987
  if output_hidden_states:
988
  all_hidden_states += (hidden_states,)
989
 
 
 
990
  if self.gradient_checkpointing and self.training:
991
+ layer_outputs = self._gradient_checkpointing_func(
992
+ decoder_layer.__call__,
 
 
 
 
 
 
 
 
993
  hidden_states,
994
+ causal_mask,
995
  position_ids,
996
+ past_key_values,
997
+ output_attentions,
998
+ use_cache,
999
+ cache_position,
1000
  )
1001
  else:
1002
  layer_outputs = decoder_layer(
1003
  hidden_states,
1004
+ attention_mask=causal_mask,
1005
  position_ids=position_ids,
1006
+ past_key_value=past_key_values,
1007
  output_attentions=output_attentions,
1008
  use_cache=use_cache,
1009
+ cache_position=cache_position,
1010
  )
1011
 
1012
  hidden_states = layer_outputs[0]
1013
 
1014
  if use_cache:
1015
+ next_decoder_cache = layer_outputs[2 if output_attentions else 1]
1016
 
1017
  if output_attentions:
1018
  all_self_attns += (layer_outputs[1],)
 
1024
  all_hidden_states += (hidden_states,)
1025
 
1026
  next_cache = next_decoder_cache if use_cache else None
1027
+ if return_legacy_cache:
1028
+ next_cache = next_cache.to_legacy_cache()
1029
+
1030
  if not return_dict:
1031
  return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
1032
  return BaseModelOutputWithPast(
 
1036
  attentions=all_self_attns,
1037
  )
1038
 
1039
+ def _update_causal_mask(
1040
+ self,
1041
+ attention_mask: torch.Tensor,
1042
+ input_tensor: torch.Tensor,
1043
+ cache_position: torch.Tensor,
1044
+ past_key_values: Cache,
1045
+ output_attentions: bool,
1046
+ ):
1047
+ # TODO: As of torch==2.2.0, the `attention_mask` passed to the model in `generate` is 2D and of dynamic length
1048
+ # even when the static KV cache is used. This is an issue for torch.compile which then recaptures cudagraphs at
1049
+ # each decode steps due to the dynamic shapes. (`recording cudagraph tree for symint key 13`, etc.), which is
1050
+ # VERY slow. A workaround is `@torch.compiler.disable`, but this prevents using `fullgraph=True`.
1051
+ # See more context in https://github.com/huggingface/transformers/pull/29114
1052
 
1053
+ if self.config.attn_implementation == "flash_attention_2":
1054
+ if attention_mask is not None and 0.0 in attention_mask:
1055
+ return attention_mask
1056
+ return None
1057
+
1058
+ # For SDPA, when possible, we will rely on its `is_causal` argument instead of its `attn_mask` argument, in
1059
+ # order to dispatch on Flash Attention 2. This feature is not compatible with static cache, as SDPA will fail
1060
+ # to infer the attention mask.
1061
+ past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
1062
+ using_static_cache = isinstance(past_key_values, StaticCache)
1063
+
1064
+ # When output attentions is True, sdpa implementation's forward method calls the eager implementation's forward
1065
+ if self.config.attn_implementation == "sdpa" and not using_static_cache and not output_attentions:
1066
+ if AttentionMaskConverter._ignore_causal_mask_sdpa(
1067
+ attention_mask,
1068
+ inputs_embeds=input_tensor,
1069
+ past_key_values_length=past_seen_tokens,
1070
+ is_training=self.training,
1071
+ ):
1072
+ return None
1073
+
1074
+ dtype, device = input_tensor.dtype, input_tensor.device
1075
+ min_dtype = torch.finfo(dtype).min
1076
+ sequence_length = input_tensor.shape[1]
1077
+ if using_static_cache:
1078
+ target_length = past_key_values.get_max_length()
1079
+ else:
1080
+ target_length = (
1081
+ attention_mask.shape[-1]
1082
+ if isinstance(attention_mask, torch.Tensor)
1083
+ else past_seen_tokens + sequence_length + 1
1084
+ )
1085
+
1086
+ if attention_mask is not None and attention_mask.dim() == 4:
1087
+ # in this case we assume that the mask comes already in inverted form and requires no inversion or slicing
1088
+ if attention_mask.max() != 0:
1089
+ raise ValueError("Custom 4D attention mask should be passed in inverted form with max==0`")
1090
+ causal_mask = attention_mask
1091
+ else:
1092
+ causal_mask = torch.full((sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device)
1093
+ if sequence_length != 1:
1094
+ causal_mask = torch.triu(causal_mask, diagonal=1)
1095
+ causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1)
1096
+ causal_mask = causal_mask[None, None, :, :].expand(input_tensor.shape[0], 1, -1, -1)
1097
+ if attention_mask is not None:
1098
+ causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit
1099
+ mask_length = attention_mask.shape[-1]
1100
+ padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :]
1101
+ padding_mask = padding_mask == 0
1102
+ causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill(
1103
+ padding_mask, min_dtype
1104
+ )
1105
+ if (
1106
+ self.config.attn_implementation == "sdpa"
1107
+ and attention_mask is not None
1108
+ and attention_mask.device.type == "cuda"
1109
+ and not output_attentions
1110
+ ):
1111
+ # Attend to all tokens in fully masked rows in the causal_mask, for example the relevant first rows when
1112
+ # using left padding. This is required by F.scaled_dot_product_attention memory-efficient attention path.
1113
+ # Details: https://github.com/pytorch/pytorch/issues/110213
1114
+ causal_mask = AttentionMaskConverter._unmask_unattended(causal_mask, min_dtype) # pylint: disable=E1120
1115
+
1116
+ return causal_mask
1117
+
1118
+
1119
+ # Modified from transformers.models.llama.modeling_llama.LlamaForCausalLM
1120
  class InternLM2ForCausalLM(InternLM2PreTrainedModel):
1121
+ """Causal language model (CLM) for InternLM2."""
 
 
1122
 
1123
  _auto_class = "AutoModelForCausalLM"
 
1124
  _tied_weights_keys = ["output.weight"]
1125
 
1126
  def __init__(self, config):
 
1157
  input_ids: torch.LongTensor = None,
1158
  attention_mask: Optional[torch.Tensor] = None,
1159
  position_ids: Optional[torch.LongTensor] = None,
1160
+ past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
1161
  inputs_embeds: Optional[torch.FloatTensor] = None,
1162
  labels: Optional[torch.LongTensor] = None,
1163
  use_cache: Optional[bool] = None,
1164
  output_attentions: Optional[bool] = None,
1165
  output_hidden_states: Optional[bool] = None,
1166
  return_dict: Optional[bool] = None,
1167
+ cache_position: Optional[torch.LongTensor] = None,
1168
  ) -> Union[Tuple, CausalLMOutputWithPast]:
1169
  r"""
1170
  Args:
 
1180
  ```python
1181
  >>> from transformers import AutoTokenizer, InternLM2ForCausalLM
1182
 
1183
+ >>> model = InternLM2ForCausalLM.from_pretrained("meta-InternLM2/InternLM2-2-7b-hf")
1184
+ >>> tokenizer = AutoTokenizer.from_pretrained("meta-InternLM2/InternLM2-2-7b-hf")
1185
 
1186
  >>> prompt = "Hey, are you conscious? Can you talk to me?"
1187
  >>> inputs = tokenizer(prompt, return_tensors="pt")
 
1209
  output_attentions=output_attentions,
1210
  output_hidden_states=output_hidden_states,
1211
  return_dict=return_dict,
1212
+ cache_position=cache_position,
1213
  )
1214
 
1215
  hidden_states = outputs[0]
1216
+ if self.config.pretraining_tp > 1:
1217
+ output_slices = self.output.weight.split(self.vocab_size // self.config.pretraining_tp, dim=0)
1218
+ logits = [
1219
+ F.linear(hidden_states, output_slices[i]) # pylint: disable=not-callable
1220
+ for i in range(self.config.pretraining_tp)
1221
+ ]
1222
+ logits = torch.cat(logits, dim=-1)
1223
+ else:
1224
+ logits = self.output(hidden_states)
1225
  logits = logits.float()
1226
 
1227
  loss = None
 
1250
  )
1251
 
1252
  def prepare_inputs_for_generation(
1253
+ self,
1254
+ input_ids,
1255
+ past_key_values=None,
1256
+ attention_mask=None,
1257
+ inputs_embeds=None,
1258
+ cache_position=None,
1259
+ use_cache=True,
1260
+ **kwargs,
1261
  ):
1262
+ past_length = 0
1263
  if past_key_values is not None:
1264
+ if isinstance(past_key_values, Cache):
1265
+ past_length = cache_position[0] if cache_position is not None else past_key_values.get_seq_length()
1266
+ max_cache_length = (
1267
+ torch.tensor(past_key_values.get_max_length(), device=input_ids.device)
1268
+ if past_key_values.get_max_length() is not None
1269
+ else None
1270
+ )
1271
+ cache_length = past_length if max_cache_length is None else torch.min(max_cache_length, past_length)
1272
+ # TODO joao: remove this `else` after `generate` prioritizes `Cache` objects
1273
  else:
1274
+ cache_length = past_length = past_key_values[0][0].shape[2]
1275
+ max_cache_length = None
1276
+
1277
+ # Keep only the unprocessed tokens:
1278
+ # 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where
1279
+ # some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as input)
1280
+ if attention_mask is not None and attention_mask.shape[1] > input_ids.shape[1]:
1281
+ input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :]
1282
+ # 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard
1283
+ # input_ids based on the past_length.
1284
+ elif past_length < input_ids.shape[1]:
1285
+ input_ids = input_ids[:, past_length:]
1286
+ # 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.
1287
+
1288
+ # If we are about to go beyond the maximum cache length, we need to crop the input attention mask.
1289
+ if (
1290
+ max_cache_length is not None
1291
+ and attention_mask is not None
1292
+ and cache_length + input_ids.shape[1] > max_cache_length
1293
+ ):
1294
+ attention_mask = attention_mask[:, -max_cache_length:] # pylint: disable=E1130
1295
 
1296
  position_ids = kwargs.get("position_ids", None)
1297
  if attention_mask is not None and position_ids is None:
 
1305
  if inputs_embeds is not None and past_key_values is None:
1306
  model_inputs = {"inputs_embeds": inputs_embeds}
1307
  else:
1308
+ # The `contiguous()` here is necessary to have a static stride during decoding. torchdynamo otherwise
1309
+ # recompiles graphs as the stride of the inputs is a guard.
1310
+ # Ref: https://github.com/huggingface/transformers/pull/29114
1311
+ # TODO: use `next_tokens` directly instead.
1312
+ model_inputs = {"input_ids": input_ids.contiguous()}
1313
+
1314
+ input_length = position_ids.shape[-1] if position_ids is not None else input_ids.shape[-1]
1315
+ if cache_position is None:
1316
+ cache_position = torch.arange(past_length, past_length + input_length, device=input_ids.device)
1317
+ elif use_cache:
1318
+ cache_position = cache_position[-input_length:]
1319
 
1320
  model_inputs.update(
1321
  {
1322
  "position_ids": position_ids,
1323
+ "cache_position": cache_position,
1324
  "past_key_values": past_key_values,
1325
+ "use_cache": use_cache,
1326
  "attention_mask": attention_mask,
1327
  }
1328
  )
 
1484
  return consumer()
1485
 
1486
 
1487
+ # Copied from transformers.models.llama.modeling_llama.LlamaForSequenceClassification with Llama->InternLM2
1488
  @add_start_docstrings(
1489
  """
1490
  The InternLM2 Model transformer with a sequence classification head on top (linear layer).
1491
 
1492
+ [`InternLM2ForSequenceClassification`] uses the last token in order to do the classification, as other causal models
1493
+ (e.g. GPT-2) do.
1494
 
1495
  Since it does classification on the last token, it requires to know the position of the last token. If a
1496
  `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
 
1501
  InternLM2_START_DOCSTRING,
1502
  )
1503
  class InternLM2ForSequenceClassification(InternLM2PreTrainedModel):
1504
+ """Sequence Classification Head for InternLM2 Model."""
 
 
1505
 
1506
  def __init__(self, config):
1507
  super().__init__(config)
 
1524
  input_ids: torch.LongTensor = None,
1525
  attention_mask: Optional[torch.Tensor] = None,
1526
  position_ids: Optional[torch.LongTensor] = None,
1527
+ past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
1528
  inputs_embeds: Optional[torch.FloatTensor] = None,
1529
  labels: Optional[torch.LongTensor] = None,
1530
  use_cache: Optional[bool] = None,
 
1565
  sequence_lengths = -1
1566
  else:
1567
  if input_ids is not None:
1568
+ # if no pad token found, use modulo instead of reverse indexing for ONNX compatibility
1569
+ sequence_lengths = torch.eq(input_ids, self.config.pad_token_id).int().argmax(-1) - 1
1570
+ sequence_lengths = sequence_lengths % input_ids.shape[-1]
1571
+ sequence_lengths = sequence_lengths.to(logits.device)
1572
  else:
1573
  sequence_lengths = -1
1574
 
 
1580
  if self.config.problem_type is None:
1581
  if self.num_labels == 1:
1582
  self.config.problem_type = "regression"
1583
+ elif self.num_labels > 1 and (labels.dtype in (torch.long, torch.int)):
 
1584
  self.config.problem_type = "single_label_classification"
1585
  else:
1586
  self.config.problem_type = "multi_label_classification"
 
1608
  hidden_states=transformer_outputs.hidden_states,
1609
  attentions=transformer_outputs.attentions,
1610
  )
1611
+
1612
+
1613
+ # Copied from transformers.models.llama.modeling_llama.LlamaForQuestionAnswering with Llama->InternLM2
1614
+ @add_start_docstrings(
1615
+ """
1616
+ The InternLM2 Model transformer with a span classification head on top for extractive question-answering tasks like
1617
+ SQuAD (a linear layer on top of the hidden-states output to compute `span start logits` and `span end logits`).
1618
+ """,
1619
+ InternLM2_START_DOCSTRING,
1620
+ )
1621
+ class InternLM2ForQuestionAnswering(InternLM2PreTrainedModel):
1622
+ """Question Answering model for InternLM2."""
1623
+
1624
+ base_model_prefix = "transformer"
1625
+
1626
+ def __init__(self, config):
1627
+ super().__init__(config)
1628
+ self.transformer = InternLM2Model(config)
1629
+ self.qa_outputs = nn.Linear(config.hidden_size, 2)
1630
+
1631
+ # Initialize weights and apply final processing
1632
+ self.post_init()
1633
+
1634
+ def get_input_embeddings(self):
1635
+ return self.transformer.embed_tokens
1636
+
1637
+ def set_input_embeddings(self, value):
1638
+ self.transformer.embed_tokens = value
1639
+
1640
+ @add_start_docstrings_to_model_forward(InternLM2_INPUTS_DOCSTRING)
1641
+ def forward(
1642
+ self,
1643
+ input_ids: Optional[torch.LongTensor] = None,
1644
+ attention_mask: Optional[torch.FloatTensor] = None,
1645
+ position_ids: Optional[torch.LongTensor] = None,
1646
+ past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
1647
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1648
+ start_positions: Optional[torch.LongTensor] = None,
1649
+ end_positions: Optional[torch.LongTensor] = None,
1650
+ output_attentions: Optional[bool] = None,
1651
+ output_hidden_states: Optional[bool] = None,
1652
+ return_dict: Optional[bool] = None,
1653
+ ) -> Union[Tuple, QuestionAnsweringModelOutput]:
1654
+ r"""
1655
+ start_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1656
+ Labels for position (index) of the start of the labelled span for computing the token classification loss.
1657
+ Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence
1658
+ are not taken into account for computing the loss.
1659
+ end_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1660
+ Labels for position (index) of the end of the labelled span for computing the token classification loss.
1661
+ Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence
1662
+ are not taken into account for computing the loss.
1663
+ """
1664
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1665
+
1666
+ outputs = self.transformer(
1667
+ input_ids,
1668
+ attention_mask=attention_mask,
1669
+ position_ids=position_ids,
1670
+ past_key_values=past_key_values,
1671
+ inputs_embeds=inputs_embeds,
1672
+ output_attentions=output_attentions,
1673
+ output_hidden_states=output_hidden_states,
1674
+ return_dict=return_dict,
1675
+ )
1676
+
1677
+ sequence_output = outputs[0]
1678
+
1679
+ logits = self.qa_outputs(sequence_output)
1680
+ start_logits, end_logits = logits.split(1, dim=-1)
1681
+ start_logits = start_logits.squeeze(-1).contiguous()
1682
+ end_logits = end_logits.squeeze(-1).contiguous()
1683
+
1684
+ total_loss = None
1685
+ if start_positions is not None and end_positions is not None:
1686
+ # If we are on multi-GPU, split add a dimension
1687
+ if len(start_positions.size()) > 1:
1688
+ start_positions = start_positions.squeeze(-1).to(start_logits.device)
1689
+ if len(end_positions.size()) > 1:
1690
+ end_positions = end_positions.squeeze(-1).to(end_logits.device)
1691
+ # sometimes the start/end positions are outside our model inputs, we ignore these terms
1692
+ ignored_index = start_logits.size(1)
1693
+ start_positions = start_positions.clamp(0, ignored_index)
1694
+ end_positions = end_positions.clamp(0, ignored_index)
1695
+
1696
+ loss_fct = CrossEntropyLoss(ignore_index=ignored_index)
1697
+ start_loss = loss_fct(start_logits, start_positions)
1698
+ end_loss = loss_fct(end_logits, end_positions)
1699
+ total_loss = (start_loss + end_loss) / 2
1700
+
1701
+ if not return_dict:
1702
+ output = (start_logits, end_logits) + outputs[2:]
1703
+ return ((total_loss,) + output) if total_loss is not None else output
1704
+
1705
+ return QuestionAnsweringModelOutput(
1706
+ loss=total_loss,
1707
+ start_logits=start_logits,
1708
+ end_logits=end_logits,
1709
+ hidden_states=outputs.hidden_states,
1710
+ attentions=outputs.attentions,
1711
+ )
1712
+
1713
+
1714
+ # Copied from transformers.models.llama.modeling_llama.LlamaForTokenClassification with Llama->InternLM2
1715
+ @add_start_docstrings(
1716
+ """
1717
+ The InternLM2 Model transformer with a token classification head on top (a linear layer on top of the hidden-states
1718
+ output) e.g. for Named-Entity-Recognition (NER) tasks.
1719
+ """,
1720
+ InternLM2_START_DOCSTRING,
1721
+ )
1722
+ class InternLM2ForTokenClassification(InternLM2PreTrainedModel):
1723
+ """Token classification model for InternLM2."""
1724
+
1725
+ def __init__(self, config):
1726
+ super().__init__(config)
1727
+ self.num_labels = config.num_labels
1728
+ self.model = InternLM2Model(config)
1729
+ if getattr(config, "classifier_dropout", None) is not None:
1730
+ classifier_dropout = config.classifier_dropout
1731
+ elif getattr(config, "hidden_dropout", None) is not None:
1732
+ classifier_dropout = config.hidden_dropout
1733
+ else:
1734
+ classifier_dropout = 0.1
1735
+ self.dropout = nn.Dropout(classifier_dropout)
1736
+ self.score = nn.Linear(config.hidden_size, config.num_labels)
1737
+
1738
+ # Initialize weights and apply final processing
1739
+ self.post_init()
1740
+
1741
+ def get_input_embeddings(self):
1742
+ return self.model.embed_tokens
1743
+
1744
+ def set_input_embeddings(self, value):
1745
+ self.model.embed_tokens = value
1746
+
1747
+ @add_start_docstrings_to_model_forward(InternLM2_INPUTS_DOCSTRING)
1748
+ def forward(
1749
+ self,
1750
+ input_ids: torch.LongTensor = None,
1751
+ attention_mask: Optional[torch.Tensor] = None,
1752
+ position_ids: Optional[torch.LongTensor] = None,
1753
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1754
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1755
+ labels: Optional[torch.LongTensor] = None,
1756
+ use_cache: Optional[bool] = None,
1757
+ output_attentions: Optional[bool] = None,
1758
+ output_hidden_states: Optional[bool] = None,
1759
+ return_dict: Optional[bool] = None,
1760
+ ) -> Union[Tuple, SequenceClassifierOutputWithPast]:
1761
+ r"""
1762
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1763
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
1764
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
1765
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
1766
+ """
1767
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1768
+
1769
+ outputs = self.model(
1770
+ input_ids,
1771
+ attention_mask=attention_mask,
1772
+ position_ids=position_ids,
1773
+ past_key_values=past_key_values,
1774
+ inputs_embeds=inputs_embeds,
1775
+ use_cache=use_cache,
1776
+ output_attentions=output_attentions,
1777
+ output_hidden_states=output_hidden_states,
1778
+ return_dict=return_dict,
1779
+ )
1780
+ sequence_output = outputs[0]
1781
+ sequence_output = self.dropout(sequence_output)
1782
+ logits = self.score(sequence_output)
1783
+
1784
+ loss = None
1785
+ if labels is not None:
1786
+ loss_fct = CrossEntropyLoss()
1787
+ loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
1788
+
1789
+ if not return_dict:
1790
+ output = (logits,) + outputs[2:]
1791
+ return ((loss,) + output) if loss is not None else output
1792
+
1793
+ return TokenClassifierOutput(
1794
+ loss=loss,
1795
+ logits=logits,
1796
+ hidden_states=outputs.hidden_states,
1797
+ attentions=outputs.attentions,
1798
+ )