mark20250409 commited on
Commit
50f7607
·
verified ·
1 Parent(s): 808215e

Upload modeling_deepseek.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. modeling_deepseek.py +1824 -0
modeling_deepseek.py ADDED
@@ -0,0 +1,1824 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2023 DeepSeek-AI and The HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
5
+ # and OPT implementations in this library. It has been modified from its
6
+ # original forms to accommodate minor architectural differences compared
7
+ # to GPT-NeoX and OPT used by the Meta AI team that trained the model.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ """ PyTorch DeepSeek model."""
21
+ import math
22
+ import warnings
23
+ from typing import List, Optional, Tuple, Union
24
+
25
+ import torch
26
+ import torch.nn.functional as F
27
+ import torch.utils.checkpoint
28
+ from torch import nn
29
+ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
30
+
31
+ from transformers.activations import ACT2FN
32
+ from transformers.cache_utils import Cache, DynamicCache
33
+ from transformers.modeling_attn_mask_utils import (
34
+ AttentionMaskConverter,
35
+ _prepare_4d_attention_mask,
36
+ _prepare_4d_causal_attention_mask,
37
+ )
38
+ from transformers.modeling_outputs import (
39
+ BaseModelOutputWithPast,
40
+ CausalLMOutputWithPast,
41
+ SequenceClassifierOutputWithPast,
42
+ )
43
+ from transformers.modeling_utils import PreTrainedModel
44
+ from transformers.pytorch_utils import (
45
+ ALL_LAYERNORM_LAYERS,
46
+ is_torch_greater_or_equal_than_1_13,
47
+ )
48
+ from transformers.utils import (
49
+ add_start_docstrings,
50
+ add_start_docstrings_to_model_forward,
51
+ is_flash_attn_2_available,
52
+ is_flash_attn_greater_or_equal_2_10,
53
+ logging,
54
+ replace_return_docstrings,
55
+ )
56
+ from transformers.utils.import_utils import is_torch_fx_available
57
+ from .configuration_deepseek import DeepseekV3Config
58
+ import torch.distributed as dist
59
+ import numpy as np
60
+
61
+ if is_flash_attn_2_available():
62
+ from flash_attn import flash_attn_func, flash_attn_varlen_func
63
+ from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa
64
+
65
+
66
+ # This makes `_prepare_4d_causal_attention_mask` a leaf function in the FX graph.
67
+ # It means that the function will not be traced through and simply appear as a node in the graph.
68
+ if is_torch_fx_available():
69
+ if not is_torch_greater_or_equal_than_1_13:
70
+ import torch.fx
71
+
72
+ _prepare_4d_causal_attention_mask = torch.fx.wrap(_prepare_4d_causal_attention_mask)
73
+
74
+
75
+ logger = logging.get_logger(__name__)
76
+
77
+ _CONFIG_FOR_DOC = "DeepseekV3Config"
78
+
79
+
80
+ def _get_unpad_data(attention_mask):
81
+ seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
82
+ indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
83
+ max_seqlen_in_batch = seqlens_in_batch.max().item()
84
+ cu_seqlens = F.pad(
85
+ torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.torch.int32), (1, 0)
86
+ )
87
+ return (
88
+ indices,
89
+ cu_seqlens,
90
+ max_seqlen_in_batch,
91
+ )
92
+
93
+
94
+ class DeepseekV3RMSNorm(nn.Module):
95
+ def __init__(self, hidden_size, eps=1e-6):
96
+ """
97
+ DeepseekV3RMSNorm is equivalent to T5LayerNorm
98
+ """
99
+ super().__init__()
100
+ self.weight = nn.Parameter(torch.ones(hidden_size))
101
+ self.variance_epsilon = eps
102
+
103
+ def forward(self, hidden_states):
104
+ input_dtype = hidden_states.dtype
105
+ hidden_states = hidden_states.to(torch.float32)
106
+ variance = hidden_states.pow(2).mean(-1, keepdim=True)
107
+ hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
108
+ return self.weight * hidden_states.to(input_dtype)
109
+
110
+
111
+ ALL_LAYERNORM_LAYERS.append(DeepseekV3RMSNorm)
112
+
113
+
114
+ class DeepseekV3RotaryEmbedding(nn.Module):
115
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
116
+ super().__init__()
117
+
118
+ self.dim = dim
119
+ self.max_position_embeddings = max_position_embeddings
120
+ self.base = base
121
+ inv_freq = 1.0 / (
122
+ self.base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim)
123
+ )
124
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
125
+
126
+ # Build here to make `torch.jit.trace` work.
127
+ self._set_cos_sin_cache(
128
+ seq_len=max_position_embeddings,
129
+ device=self.inv_freq.device,
130
+ dtype=torch.get_default_dtype(),
131
+ )
132
+ self.max_seq_len_cached = None
133
+
134
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
135
+ self.max_seq_len_cached = seq_len
136
+ t = torch.arange(
137
+ self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype
138
+ )
139
+
140
+ freqs = torch.outer(t, self.inv_freq.to(t.device))
141
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
142
+ emb = torch.cat((freqs, freqs), dim=-1)
143
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
144
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
145
+
146
+ def forward(self, x, seq_len=None):
147
+ # x: [bs, num_attention_heads, seq_len, head_size]
148
+ if self.max_seq_len_cached is None or seq_len > self.max_seq_len_cached:
149
+ self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
150
+
151
+ return (
152
+ self.cos_cached[:seq_len].to(dtype=x.dtype),
153
+ self.sin_cached[:seq_len].to(dtype=x.dtype),
154
+ )
155
+
156
+
157
+ # Copied from transformers.models.llama.modeling_llama.LlamaLinearScalingRotaryEmbedding with Llama->DeepseekV3
158
+ class DeepseekV3LinearScalingRotaryEmbedding(DeepseekV3RotaryEmbedding):
159
+ """DeepseekV3RotaryEmbedding extended with linear scaling. Credits to the Reddit user /u/kaiokendev"""
160
+
161
+ def __init__(
162
+ self,
163
+ dim,
164
+ max_position_embeddings=2048,
165
+ base=10000,
166
+ device=None,
167
+ scaling_factor=1.0,
168
+ ):
169
+ self.scaling_factor = scaling_factor
170
+ super().__init__(dim, max_position_embeddings, base, device)
171
+
172
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
173
+ self.max_seq_len_cached = seq_len
174
+ t = torch.arange(
175
+ self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype
176
+ )
177
+ t = t / self.scaling_factor
178
+
179
+ freqs = torch.outer(t, self.inv_freq)
180
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
181
+ emb = torch.cat((freqs, freqs), dim=-1)
182
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
183
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
184
+
185
+
186
+ # Copied from transformers.models.llama.modeling_llama.LlamaDynamicNTKScalingRotaryEmbedding with Llama->DeepseekV3
187
+ class DeepseekV3DynamicNTKScalingRotaryEmbedding(DeepseekV3RotaryEmbedding):
188
+ """DeepseekV3RotaryEmbedding extended with Dynamic NTK scaling. Credits to the Reddit users /u/bloc97 and /u/emozilla"""
189
+
190
+ def __init__(
191
+ self,
192
+ dim,
193
+ max_position_embeddings=2048,
194
+ base=10000,
195
+ device=None,
196
+ scaling_factor=1.0,
197
+ ):
198
+ self.scaling_factor = scaling_factor
199
+ super().__init__(dim, max_position_embeddings, base, device)
200
+
201
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
202
+ self.max_seq_len_cached = seq_len
203
+
204
+ if seq_len > self.max_position_embeddings:
205
+ base = self.base * (
206
+ (self.scaling_factor * seq_len / self.max_position_embeddings)
207
+ - (self.scaling_factor - 1)
208
+ ) ** (self.dim / (self.dim - 2))
209
+ inv_freq = 1.0 / (
210
+ base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim)
211
+ )
212
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
213
+
214
+ t = torch.arange(
215
+ self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype
216
+ )
217
+
218
+ freqs = torch.outer(t, self.inv_freq)
219
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
220
+ emb = torch.cat((freqs, freqs), dim=-1)
221
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
222
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
223
+
224
+
225
+ # Inverse dim formula to find dim based on number of rotations
226
+ def yarn_find_correction_dim(
227
+ num_rotations, dim, base=10000, max_position_embeddings=2048
228
+ ):
229
+ return (dim * math.log(max_position_embeddings / (num_rotations * 2 * math.pi))) / (
230
+ 2 * math.log(base)
231
+ )
232
+
233
+
234
+ # Find dim range bounds based on rotations
235
+ def yarn_find_correction_range(
236
+ low_rot, high_rot, dim, base=10000, max_position_embeddings=2048
237
+ ):
238
+ low = math.floor(
239
+ yarn_find_correction_dim(low_rot, dim, base, max_position_embeddings)
240
+ )
241
+ high = math.ceil(
242
+ yarn_find_correction_dim(high_rot, dim, base, max_position_embeddings)
243
+ )
244
+ return max(low, 0), min(high, dim - 1) # Clamp values just in case
245
+
246
+
247
+ def yarn_get_mscale(scale=1, mscale=1):
248
+ if scale <= 1:
249
+ return 1.0
250
+ return 0.1 * mscale * math.log(scale) + 1.0
251
+
252
+
253
+ def yarn_linear_ramp_mask(min, max, dim):
254
+ if min == max:
255
+ max += 0.001 # Prevent singularity
256
+
257
+ linear_func = (torch.arange(dim, dtype=torch.float32) - min) / (max - min)
258
+ ramp_func = torch.clamp(linear_func, 0, 1)
259
+ return ramp_func
260
+
261
+
262
+ class DeepseekV3YarnRotaryEmbedding(DeepseekV3RotaryEmbedding):
263
+
264
+ def __init__(
265
+ self,
266
+ dim,
267
+ max_position_embeddings=2048,
268
+ base=10000,
269
+ device=None,
270
+ scaling_factor=1.0,
271
+ original_max_position_embeddings=4096,
272
+ beta_fast=32,
273
+ beta_slow=1,
274
+ mscale=1,
275
+ mscale_all_dim=0,
276
+ ):
277
+ self.scaling_factor = scaling_factor
278
+ self.original_max_position_embeddings = original_max_position_embeddings
279
+ self.beta_fast = beta_fast
280
+ self.beta_slow = beta_slow
281
+ self.mscale = mscale
282
+ self.mscale_all_dim = mscale_all_dim
283
+ super().__init__(dim, max_position_embeddings, base, device)
284
+
285
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
286
+ self.max_seq_len_cached = seq_len
287
+ dim = self.dim
288
+
289
+ freq_extra = 1.0 / (
290
+ self.base
291
+ ** (torch.arange(0, dim, 2, dtype=torch.float32, device=device) / dim)
292
+ )
293
+ freq_inter = 1.0 / (
294
+ self.scaling_factor
295
+ * self.base
296
+ ** (torch.arange(0, dim, 2, dtype=torch.float32, device=device) / dim)
297
+ )
298
+
299
+ low, high = yarn_find_correction_range(
300
+ self.beta_fast,
301
+ self.beta_slow,
302
+ dim,
303
+ self.base,
304
+ self.original_max_position_embeddings,
305
+ )
306
+ inv_freq_mask = 1.0 - yarn_linear_ramp_mask(low, high, dim // 2).to(
307
+ device=device, dtype=torch.float32
308
+ )
309
+ inv_freq = freq_inter * (1 - inv_freq_mask) + freq_extra * inv_freq_mask
310
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
311
+
312
+ t = torch.arange(seq_len, device=device, dtype=torch.float32)
313
+
314
+ freqs = torch.outer(t, inv_freq)
315
+
316
+ _mscale = float(
317
+ yarn_get_mscale(self.scaling_factor, self.mscale)
318
+ / yarn_get_mscale(self.scaling_factor, self.mscale_all_dim)
319
+ )
320
+
321
+ emb = torch.cat((freqs, freqs), dim=-1)
322
+ self.register_buffer(
323
+ "cos_cached", (emb.cos() * _mscale).to(dtype), persistent=False
324
+ )
325
+ self.register_buffer(
326
+ "sin_cached", (emb.sin() * _mscale).to(dtype), persistent=False
327
+ )
328
+
329
+
330
+ # Copied from transformers.models.llama.modeling_llama.rotate_half
331
+ def rotate_half(x):
332
+ """Rotates half the hidden dims of the input."""
333
+ x1 = x[..., : x.shape[-1] // 2]
334
+ x2 = x[..., x.shape[-1] // 2 :]
335
+ return torch.cat((-x2, x1), dim=-1)
336
+
337
+
338
+ # Copied from transformers.models.llama.modeling_llama.apply_rotary_pos_emb
339
+ def apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1):
340
+ """Applies Rotary Position Embedding to the query and key tensors.
341
+ Args:
342
+ q (`torch.Tensor`): The query tensor.
343
+ k (`torch.Tensor`): The key tensor.
344
+ cos (`torch.Tensor`): The cosine part of the rotary embedding.
345
+ sin (`torch.Tensor`): The sine part of the rotary embedding.
346
+ position_ids (`torch.Tensor`):
347
+ The position indices of the tokens corresponding to the query and key tensors. For example, this can be
348
+ used to pass offsetted position ids when working with a KV-cache.
349
+ unsqueeze_dim (`int`, *optional*, defaults to 1):
350
+ The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
351
+ sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
352
+ that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
353
+ k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
354
+ cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
355
+ the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
356
+ Returns:
357
+ `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
358
+ """
359
+ cos = cos[position_ids].unsqueeze(unsqueeze_dim)
360
+ sin = sin[position_ids].unsqueeze(unsqueeze_dim)
361
+
362
+ b, h, s, d = q.shape
363
+ q = q.view(b, h, s, d // 2, 2).transpose(4, 3).reshape(b, h, s, d)
364
+
365
+ b, h, s, d = k.shape
366
+ k = k.view(b, h, s, d // 2, 2).transpose(4, 3).reshape(b, h, s, d)
367
+
368
+ q_embed = (q * cos) + (rotate_half(q) * sin)
369
+ k_embed = (k * cos) + (rotate_half(k) * sin)
370
+ return q_embed, k_embed
371
+
372
+
373
+ class DeepseekV3MLP(nn.Module):
374
+ def __init__(self, config, hidden_size=None, intermediate_size=None):
375
+ super().__init__()
376
+ self.config = config
377
+ self.hidden_size = config.hidden_size if hidden_size is None else hidden_size
378
+ self.intermediate_size = (
379
+ config.intermediate_size if intermediate_size is None else intermediate_size
380
+ )
381
+
382
+ self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
383
+ self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
384
+ self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
385
+ self.act_fn = ACT2FN[config.hidden_act]
386
+
387
+ def forward(self, x):
388
+ down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
389
+ return down_proj
390
+
391
+
392
+ class MoEGate(nn.Module):
393
+ def __init__(self, config):
394
+ super().__init__()
395
+ self.config = config
396
+ self.top_k = config.num_experts_per_tok
397
+ self.n_routed_experts = config.n_routed_experts
398
+ self.routed_scaling_factor = config.routed_scaling_factor
399
+ self.scoring_func = config.scoring_func
400
+ self.seq_aux = config.seq_aux
401
+ self.topk_method = config.topk_method
402
+ self.n_group = config.n_group
403
+ self.topk_group = config.topk_group
404
+
405
+ # topk selection algorithm
406
+ self.norm_topk_prob = config.norm_topk_prob
407
+ self.gating_dim = config.hidden_size
408
+ self.weight = nn.Parameter(
409
+ torch.empty((self.n_routed_experts, self.gating_dim))
410
+ )
411
+ if self.topk_method == "noaux_tc":
412
+ self.e_score_correction_bias = nn.Parameter(
413
+ torch.empty((self.n_routed_experts))
414
+ )
415
+ self.reset_parameters()
416
+
417
+ def reset_parameters(self) -> None:
418
+ import torch.nn.init as init
419
+
420
+ init.kaiming_uniform_(self.weight, a=math.sqrt(5))
421
+
422
+ def forward(self, hidden_states):
423
+ bsz, seq_len, h = hidden_states.shape
424
+ ### compute gating score
425
+ hidden_states = hidden_states.view(-1, h)
426
+ logits = F.linear(
427
+ hidden_states.type(torch.float32), self.weight.type(torch.float32), None
428
+ )
429
+ if self.scoring_func == "sigmoid":
430
+ scores = logits.sigmoid()
431
+ else:
432
+ raise NotImplementedError(
433
+ f"insupportable scoring function for MoE gating: {self.scoring_func}"
434
+ )
435
+
436
+ ### select top-k experts
437
+ if self.topk_method == "noaux_tc":
438
+ assert not self.training
439
+ scores_for_choice = scores.view(bsz * seq_len, -1) + self.e_score_correction_bias.unsqueeze(0)
440
+ group_scores = (
441
+ scores_for_choice.view(bsz * seq_len, self.n_group, -1).topk(2, dim=-1)[0].sum(dim = -1)
442
+ ) # [n, n_group]
443
+ group_idx = torch.topk(
444
+ group_scores, k=self.topk_group, dim=-1, sorted=False
445
+ )[
446
+ 1
447
+ ] # [n, top_k_group]
448
+ group_mask = torch.zeros_like(group_scores) # [n, n_group]
449
+ group_mask.scatter_(1, group_idx, 1) # [n, n_group]
450
+ score_mask = (
451
+ group_mask.unsqueeze(-1)
452
+ .expand(
453
+ bsz * seq_len, self.n_group, self.n_routed_experts // self.n_group
454
+ )
455
+ .reshape(bsz * seq_len, -1)
456
+ ) # [n, e]
457
+ tmp_scores = scores_for_choice.masked_fill(~score_mask.bool(), float("-inf")) # [n, e]
458
+ _, topk_idx = torch.topk(
459
+ tmp_scores, k=self.top_k, dim=-1, sorted=False
460
+ )
461
+ topk_weight = scores.gather(1, topk_idx)
462
+ else:
463
+ raise NotImplementedError(
464
+ f"insupportable TopK function for MoE gating: {self.topk_method}"
465
+ )
466
+
467
+ ### norm gate to sum 1
468
+ if self.top_k > 1 and self.norm_topk_prob:
469
+ denominator = topk_weight.sum(dim=-1, keepdim=True) + 1e-20
470
+ topk_weight = topk_weight / denominator
471
+ topk_weight = topk_weight * self.routed_scaling_factor # must multiply the scaling factor
472
+
473
+ return topk_idx, topk_weight
474
+
475
+ class DeepseekV3MoE(nn.Module):
476
+ """
477
+ A mixed expert module containing shared experts.
478
+ """
479
+
480
+ def __init__(self, config):
481
+ super().__init__()
482
+ self.config = config
483
+ self.num_experts_per_tok = config.num_experts_per_tok
484
+
485
+ if hasattr(config, "ep_size") and config.ep_size > 1:
486
+ assert config.ep_size == dist.get_world_size()
487
+ self.ep_size = config.ep_size
488
+ self.experts_per_rank = config.n_routed_experts // config.ep_size
489
+ self.ep_rank = dist.get_rank()
490
+ self.experts = nn.ModuleList(
491
+ [
492
+ (
493
+ DeepseekV3MLP(
494
+ config, intermediate_size=config.moe_intermediate_size
495
+ )
496
+ if i >= self.ep_rank * self.experts_per_rank
497
+ and i < (self.ep_rank + 1) * self.experts_per_rank
498
+ else None
499
+ )
500
+ for i in range(config.n_routed_experts)
501
+ ]
502
+ )
503
+ else:
504
+ self.ep_size = 1
505
+ self.experts_per_rank = config.n_routed_experts
506
+ self.ep_rank = 0
507
+ self.experts = nn.ModuleList(
508
+ [
509
+ DeepseekV3MLP(
510
+ config, intermediate_size=config.moe_intermediate_size
511
+ )
512
+ for i in range(config.n_routed_experts)
513
+ ]
514
+ )
515
+ self.gate = MoEGate(config)
516
+ if config.n_shared_experts is not None:
517
+ intermediate_size = config.moe_intermediate_size * config.n_shared_experts
518
+ self.shared_experts = DeepseekV3MLP(
519
+ config=config, intermediate_size=intermediate_size
520
+ )
521
+
522
+ def forward(self, hidden_states):
523
+ identity = hidden_states
524
+ orig_shape = hidden_states.shape
525
+ topk_idx, topk_weight = self.gate(hidden_states)
526
+ hidden_states = hidden_states.view(-1, hidden_states.shape[-1])
527
+ flat_topk_idx = topk_idx.view(-1)
528
+ if not self.training:
529
+ y = self.moe_infer(hidden_states, topk_idx, topk_weight).view(*orig_shape)
530
+ if self.config.n_shared_experts is not None:
531
+ y = y + self.shared_experts(identity)
532
+ return y
533
+
534
+ @torch.no_grad()
535
+ def moe_infer(self, x, topk_ids, topk_weight):
536
+ cnts = topk_ids.new_zeros((topk_ids.shape[0], len(self.experts)))
537
+ cnts.scatter_(1, topk_ids, 1)
538
+ tokens_per_expert = cnts.sum(dim=0)
539
+ idxs = topk_ids.view(-1).argsort()
540
+ sorted_tokens = x[idxs // topk_ids.shape[1]]
541
+ sorted_tokens_shape = sorted_tokens.shape
542
+ if self.ep_size > 1:
543
+ tokens_per_ep_rank = tokens_per_expert.view(self.ep_size, -1).sum(dim=1)
544
+ tokens_per_expert_group = tokens_per_expert.new_empty(
545
+ tokens_per_expert.shape[0]
546
+ )
547
+ dist.all_to_all_single(tokens_per_expert_group, tokens_per_expert)
548
+ output_splits = (
549
+ tokens_per_expert_group.view(self.ep_size, -1)
550
+ .sum(1)
551
+ .cpu()
552
+ .numpy()
553
+ .tolist()
554
+ )
555
+ gathered_tokens = sorted_tokens.new_empty(
556
+ tokens_per_expert_group.sum(dim=0).cpu().item(), sorted_tokens.shape[1]
557
+ )
558
+ input_split_sizes = tokens_per_ep_rank.cpu().numpy().tolist()
559
+ dist.all_to_all(
560
+ list(gathered_tokens.split(output_splits)),
561
+ list(sorted_tokens.split(input_split_sizes)),
562
+ )
563
+ tokens_per_expert_post_gather = tokens_per_expert_group.view(
564
+ self.ep_size, self.experts_per_rank
565
+ ).sum(dim=0)
566
+ gatherd_idxs = np.zeros(shape=(gathered_tokens.shape[0],), dtype=np.int32)
567
+ s = 0
568
+ for i, k in enumerate(tokens_per_expert_group.cpu().numpy()):
569
+ gatherd_idxs[s : s + k] = i % self.experts_per_rank
570
+ s += k
571
+ gatherd_idxs = gatherd_idxs.argsort()
572
+ sorted_tokens = gathered_tokens[gatherd_idxs]
573
+ tokens_per_expert = tokens_per_expert_post_gather
574
+ tokens_per_expert = tokens_per_expert.cpu().numpy()
575
+
576
+ outputs = []
577
+ start_idx = 0
578
+ for i, num_tokens in enumerate(tokens_per_expert):
579
+ end_idx = start_idx + num_tokens
580
+ if num_tokens == 0:
581
+ continue
582
+ expert = self.experts[i + self.ep_rank * self.experts_per_rank]
583
+ tokens_for_this_expert = sorted_tokens[start_idx:end_idx]
584
+ expert_out = expert(tokens_for_this_expert)
585
+ outputs.append(expert_out)
586
+ start_idx = end_idx
587
+
588
+ outs = torch.cat(outputs, dim=0) if len(outputs) else sorted_tokens.new_empty(0)
589
+ if self.ep_size > 1:
590
+ new_x = torch.empty_like(outs)
591
+ new_x[gatherd_idxs] = outs
592
+ gathered_tokens = new_x.new_empty(*sorted_tokens_shape)
593
+ dist.all_to_all(
594
+ list(gathered_tokens.split(input_split_sizes)),
595
+ list(new_x.split(output_splits)),
596
+ )
597
+ outs = gathered_tokens
598
+
599
+ new_x = torch.empty_like(outs)
600
+ new_x[idxs] = outs
601
+ final_out = (
602
+ new_x.view(*topk_ids.shape, -1)
603
+ .type(topk_weight.dtype)
604
+ .mul_(topk_weight.unsqueeze(dim=-1))
605
+ .sum(dim=1)
606
+ .type(new_x.dtype)
607
+ )
608
+ return final_out
609
+
610
+
611
+ # Copied from transformers.models.llama.modeling_llama.repeat_kv
612
+ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
613
+ """
614
+ This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
615
+ num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
616
+ """
617
+ batch, num_key_value_heads, slen, head_dim = hidden_states.shape
618
+ if n_rep == 1:
619
+ return hidden_states
620
+ hidden_states = hidden_states[:, :, None, :, :].expand(
621
+ batch, num_key_value_heads, n_rep, slen, head_dim
622
+ )
623
+ return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
624
+
625
+
626
+ # Copied from transformers.models.llama.modeling_llama.LlamaAttention with Llama->DeepseekV3
627
+ class DeepseekV3Attention(nn.Module):
628
+ """Multi-headed attention from 'Attention Is All You Need' paper"""
629
+
630
+ def __init__(self, config: DeepseekV3Config, layer_idx: Optional[int] = None):
631
+ super().__init__()
632
+ self.config = config
633
+ self.layer_idx = layer_idx
634
+ if layer_idx is None:
635
+ logger.warning_once(
636
+ f"Instantiating {self.__class__.__name__} without passing `layer_idx` is not recommended and will "
637
+ "to errors during the forward call, if caching is used. Please make sure to provide a `layer_idx` "
638
+ "when creating this class."
639
+ )
640
+
641
+ self.attention_dropout = config.attention_dropout
642
+ self.hidden_size = config.hidden_size
643
+ self.num_heads = config.num_attention_heads
644
+
645
+ self.max_position_embeddings = config.max_position_embeddings
646
+ self.rope_theta = config.rope_theta
647
+ self.q_lora_rank = config.q_lora_rank
648
+ self.qk_rope_head_dim = config.qk_rope_head_dim
649
+ self.kv_lora_rank = config.kv_lora_rank
650
+ self.v_head_dim = config.v_head_dim
651
+ self.qk_nope_head_dim = config.qk_nope_head_dim
652
+ self.q_head_dim = config.qk_nope_head_dim + config.qk_rope_head_dim
653
+
654
+ self.is_causal = True
655
+
656
+ if self.q_lora_rank is None:
657
+ self.q_proj = nn.Linear(
658
+ self.hidden_size, self.num_heads * self.q_head_dim, bias=False
659
+ )
660
+ else:
661
+ self.q_a_proj = nn.Linear(
662
+ self.hidden_size, config.q_lora_rank, bias=config.attention_bias
663
+ )
664
+ self.q_a_layernorm = DeepseekV3RMSNorm(config.q_lora_rank)
665
+ self.q_b_proj = nn.Linear(
666
+ config.q_lora_rank, self.num_heads * self.q_head_dim, bias=False
667
+ )
668
+
669
+ self.kv_a_proj_with_mqa = nn.Linear(
670
+ self.hidden_size,
671
+ config.kv_lora_rank + config.qk_rope_head_dim,
672
+ bias=config.attention_bias,
673
+ )
674
+ self.kv_a_layernorm = DeepseekV3RMSNorm(config.kv_lora_rank)
675
+ self.kv_b_proj = nn.Linear(
676
+ config.kv_lora_rank,
677
+ self.num_heads
678
+ * (self.q_head_dim - self.qk_rope_head_dim + self.v_head_dim),
679
+ bias=False,
680
+ )
681
+
682
+ self.o_proj = nn.Linear(
683
+ self.num_heads * self.v_head_dim,
684
+ self.hidden_size,
685
+ bias=config.attention_bias,
686
+ )
687
+ self._init_rope()
688
+
689
+ self.softmax_scale = self.q_head_dim ** (-0.5)
690
+ if self.config.rope_scaling is not None:
691
+ mscale_all_dim = self.config.rope_scaling.get("mscale_all_dim", 0)
692
+ scaling_factor = self.config.rope_scaling["factor"]
693
+ if mscale_all_dim:
694
+ mscale = yarn_get_mscale(scaling_factor, mscale_all_dim)
695
+ self.softmax_scale = self.softmax_scale * mscale * mscale
696
+
697
+ def _init_rope(self):
698
+ if self.config.rope_scaling is None:
699
+ self.rotary_emb = DeepseekV3RotaryEmbedding(
700
+ self.qk_rope_head_dim,
701
+ max_position_embeddings=self.max_position_embeddings,
702
+ base=self.rope_theta,
703
+ )
704
+ else:
705
+ scaling_type = self.config.rope_scaling["type"]
706
+ scaling_factor = self.config.rope_scaling["factor"]
707
+ if scaling_type == "linear":
708
+ self.rotary_emb = DeepseekV3LinearScalingRotaryEmbedding(
709
+ self.qk_rope_head_dim,
710
+ max_position_embeddings=self.max_position_embeddings,
711
+ scaling_factor=scaling_factor,
712
+ base=self.rope_theta,
713
+ )
714
+ elif scaling_type == "dynamic":
715
+ self.rotary_emb = DeepseekV3DynamicNTKScalingRotaryEmbedding(
716
+ self.qk_rope_head_dim,
717
+ max_position_embeddings=self.max_position_embeddings,
718
+ scaling_factor=scaling_factor,
719
+ base=self.rope_theta,
720
+ )
721
+ elif scaling_type == "yarn":
722
+ kwargs = {
723
+ key: self.config.rope_scaling[key]
724
+ for key in [
725
+ "original_max_position_embeddings",
726
+ "beta_fast",
727
+ "beta_slow",
728
+ "mscale",
729
+ "mscale_all_dim",
730
+ ]
731
+ if key in self.config.rope_scaling
732
+ }
733
+ self.rotary_emb = DeepseekV3YarnRotaryEmbedding(
734
+ self.qk_rope_head_dim,
735
+ max_position_embeddings=self.max_position_embeddings,
736
+ scaling_factor=scaling_factor,
737
+ base=self.rope_theta,
738
+ **kwargs,
739
+ )
740
+ else:
741
+ raise ValueError(f"Unknown RoPE scaling type {scaling_type}")
742
+
743
+ def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int):
744
+ return (
745
+ tensor.view(bsz, seq_len, self.num_heads, self.v_head_dim)
746
+ .transpose(1, 2)
747
+ .contiguous()
748
+ )
749
+
750
+ def forward(
751
+ self,
752
+ hidden_states: torch.Tensor,
753
+ attention_mask: Optional[torch.Tensor] = None,
754
+ position_ids: Optional[torch.LongTensor] = None,
755
+ past_key_value: Optional[Cache] = None,
756
+ output_attentions: bool = False,
757
+ use_cache: bool = False,
758
+ **kwargs,
759
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
760
+ if "padding_mask" in kwargs:
761
+ warnings.warn(
762
+ "Passing `padding_mask` is deprecated and will be removed in v4.37. Please make sure use `attention_mask` instead.`"
763
+ )
764
+ bsz, q_len, _ = hidden_states.size()
765
+
766
+ if self.q_lora_rank is None:
767
+ q = self.q_proj(hidden_states)
768
+ else:
769
+ q = self.q_b_proj(self.q_a_layernorm(self.q_a_proj(hidden_states)))
770
+ q = q.view(bsz, q_len, self.num_heads, self.q_head_dim).transpose(1, 2)
771
+ q_nope, q_pe = torch.split(
772
+ q, [self.qk_nope_head_dim, self.qk_rope_head_dim], dim=-1
773
+ )
774
+
775
+ compressed_kv = self.kv_a_proj_with_mqa(hidden_states)
776
+ compressed_kv, k_pe = torch.split(
777
+ compressed_kv, [self.kv_lora_rank, self.qk_rope_head_dim], dim=-1
778
+ )
779
+ k_pe = k_pe.view(bsz, q_len, 1, self.qk_rope_head_dim).transpose(1, 2)
780
+ kv = (
781
+ self.kv_b_proj(self.kv_a_layernorm(compressed_kv))
782
+ .view(bsz, q_len, self.num_heads, self.qk_nope_head_dim + self.v_head_dim)
783
+ .transpose(1, 2)
784
+ )
785
+
786
+ k_nope, value_states = torch.split(
787
+ kv, [self.qk_nope_head_dim, self.v_head_dim], dim=-1
788
+ )
789
+ kv_seq_len = value_states.shape[-2]
790
+ if past_key_value is not None:
791
+ if self.layer_idx is None:
792
+ raise ValueError(
793
+ f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
794
+ "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
795
+ "with a layer index."
796
+ )
797
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
798
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
799
+
800
+ q_pe, k_pe = apply_rotary_pos_emb(q_pe, k_pe, cos, sin, position_ids)
801
+
802
+ query_states = k_pe.new_empty(bsz, self.num_heads, q_len, self.q_head_dim)
803
+ query_states[:, :, :, : self.qk_nope_head_dim] = q_nope
804
+ query_states[:, :, :, self.qk_nope_head_dim :] = q_pe
805
+
806
+ key_states = k_pe.new_empty(bsz, self.num_heads, q_len, self.q_head_dim)
807
+ key_states[:, :, :, : self.qk_nope_head_dim] = k_nope
808
+ key_states[:, :, :, self.qk_nope_head_dim :] = k_pe
809
+ if past_key_value is not None:
810
+ cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
811
+ key_states, value_states = past_key_value.update(
812
+ key_states, value_states, self.layer_idx, cache_kwargs
813
+ )
814
+
815
+ attn_weights = (
816
+ torch.matmul(query_states, key_states.transpose(2, 3)) * self.softmax_scale
817
+ )
818
+
819
+ if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
820
+ raise ValueError(
821
+ f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
822
+ f" {attn_weights.size()}"
823
+ )
824
+ assert attention_mask is not None
825
+ if attention_mask is not None:
826
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
827
+ raise ValueError(
828
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
829
+ )
830
+ attn_weights = attn_weights + attention_mask
831
+
832
+ # upcast attention to fp32
833
+ attn_weights = nn.functional.softmax(
834
+ attn_weights, dim=-1, dtype=torch.float32
835
+ ).to(query_states.dtype)
836
+ attn_weights = nn.functional.dropout(
837
+ attn_weights, p=self.attention_dropout, training=self.training
838
+ )
839
+ attn_output = torch.matmul(attn_weights, value_states)
840
+
841
+ if attn_output.size() != (bsz, self.num_heads, q_len, self.v_head_dim):
842
+ raise ValueError(
843
+ f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.v_head_dim)}, but is"
844
+ f" {attn_output.size()}"
845
+ )
846
+
847
+ attn_output = attn_output.transpose(1, 2).contiguous()
848
+
849
+ attn_output = attn_output.reshape(bsz, q_len, self.num_heads * self.v_head_dim)
850
+
851
+ attn_output = self.o_proj(attn_output)
852
+
853
+ if not output_attentions:
854
+ attn_weights = None
855
+
856
+ return attn_output, attn_weights, past_key_value
857
+
858
+
859
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2 with Llama->DeepseekV3
860
+ class DeepseekV3FlashAttention2(DeepseekV3Attention):
861
+ """
862
+ DeepseekV3 flash attention module. This module inherits from `DeepseekV3Attention` as the weights of the module stays
863
+ untouched. The only required change would be on the forward pass where it needs to correctly call the public API of
864
+ flash attention and deal with padding tokens in case the input contains any of them.
865
+ """
866
+
867
+ def __init__(self, *args, **kwargs):
868
+ super().__init__(*args, **kwargs)
869
+
870
+ # TODO: Should be removed once Flash Attention for RoCm is bumped to 2.1.
871
+ # flash_attn<2.1 generates top-left aligned causal mask, while what is needed here is bottom-right alignement, that was made default for flash_attn>=2.1. This attribute is used to handle this difference. Reference: https://github.com/Dao-AILab/flash-attention/releases/tag/v2.1.0.
872
+ # Beware that with flash_attn<2.1, using q_seqlen != k_seqlen (except for the case q_seqlen == 1) produces a wrong mask (top-left).
873
+ self._flash_attn_uses_top_left_mask = not is_flash_attn_greater_or_equal_2_10()
874
+
875
+ def forward(
876
+ self,
877
+ hidden_states: torch.Tensor,
878
+ attention_mask: Optional[torch.LongTensor] = None,
879
+ position_ids: Optional[torch.LongTensor] = None,
880
+ past_key_value: Optional[Cache] = None,
881
+ output_attentions: bool = False,
882
+ use_cache: bool = False,
883
+ **kwargs,
884
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
885
+ # DeepseekV3FlashAttention2 attention does not support output_attentions
886
+ if "padding_mask" in kwargs:
887
+ warnings.warn(
888
+ "Passing `padding_mask` is deprecated and will be removed in v4.37. Please make sure use `attention_mask` instead.`"
889
+ )
890
+
891
+ # overwrite attention_mask with padding_mask
892
+ attention_mask = kwargs.pop("padding_mask")
893
+
894
+ output_attentions = False
895
+
896
+ bsz, q_len, _ = hidden_states.size()
897
+
898
+ if self.q_lora_rank is None:
899
+ q = self.q_proj(hidden_states)
900
+ else:
901
+ q = self.q_b_proj(self.q_a_layernorm(self.q_a_proj(hidden_states)))
902
+ q = q.view(bsz, q_len, self.num_heads, self.q_head_dim).transpose(1, 2)
903
+ q_nope, q_pe = torch.split(
904
+ q, [self.qk_nope_head_dim, self.qk_rope_head_dim], dim=-1
905
+ )
906
+
907
+ # Flash attention requires the input to have the shape
908
+ # batch_size x seq_length x head_dim x hidden_dim
909
+ # therefore we just need to keep the original shape
910
+ compressed_kv = self.kv_a_proj_with_mqa(hidden_states)
911
+ compressed_kv, k_pe = torch.split(
912
+ compressed_kv, [self.kv_lora_rank, self.qk_rope_head_dim], dim=-1
913
+ )
914
+ k_pe = k_pe.view(bsz, q_len, 1, self.qk_rope_head_dim).transpose(1, 2)
915
+ kv = (
916
+ self.kv_b_proj(self.kv_a_layernorm(compressed_kv))
917
+ .view(bsz, q_len, self.num_heads, self.qk_nope_head_dim + self.v_head_dim)
918
+ .transpose(1, 2)
919
+ )
920
+
921
+ k_nope, value_states = torch.split(
922
+ kv, [self.qk_nope_head_dim, self.v_head_dim], dim=-1
923
+ )
924
+ kv_seq_len = value_states.shape[-2]
925
+
926
+ kv_seq_len = value_states.shape[-2]
927
+ if past_key_value is not None:
928
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
929
+
930
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
931
+ q_pe, k_pe = apply_rotary_pos_emb(q_pe, k_pe, cos, sin, position_ids)
932
+
933
+ query_states = k_pe.new_empty(bsz, self.num_heads, q_len, self.q_head_dim)
934
+ query_states[:, :, :, : self.qk_nope_head_dim] = q_nope
935
+ query_states[:, :, :, self.qk_nope_head_dim :] = q_pe
936
+
937
+ key_states = k_pe.new_empty(bsz, self.num_heads, q_len, self.q_head_dim)
938
+ key_states[:, :, :, : self.qk_nope_head_dim] = k_nope
939
+ key_states[:, :, :, self.qk_nope_head_dim :] = k_pe
940
+
941
+ if self.q_head_dim != self.v_head_dim:
942
+ value_states = F.pad(value_states, [0, self.q_head_dim - self.v_head_dim])
943
+
944
+ if past_key_value is not None:
945
+ cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
946
+ key_states, value_states = past_key_value.update(
947
+ key_states, value_states, self.layer_idx, cache_kwargs
948
+ )
949
+
950
+ # TODO: These transpose are quite inefficient but Flash Attention requires the layout [batch_size, sequence_length, num_heads, head_dim]. We would need to refactor the KV cache
951
+ # to be able to avoid many of these transpose/reshape/view.
952
+ query_states = query_states.transpose(1, 2)
953
+ key_states = key_states.transpose(1, 2)
954
+ value_states = value_states.transpose(1, 2)
955
+
956
+ dropout_rate = self.attention_dropout if self.training else 0.0
957
+
958
+ # In PEFT, usually we cast the layer norms in float32 for training stability reasons
959
+ # therefore the input hidden states gets silently casted in float32. Hence, we need
960
+ # cast them back in the correct dtype just to be sure everything works as expected.
961
+ # This might slowdown training & inference so it is recommended to not cast the LayerNorms
962
+ # in fp32. (DeepseekV3RMSNorm handles it correctly)
963
+
964
+ input_dtype = query_states.dtype
965
+ if input_dtype == torch.float32:
966
+ # Handle the case where the model is quantized
967
+ if hasattr(self.config, "_pre_quantization_dtype"):
968
+ target_dtype = self.config._pre_quantization_dtype
969
+ elif torch.is_autocast_enabled():
970
+ target_dtype = torch.get_autocast_gpu_dtype()
971
+ else:
972
+ target_dtype = (
973
+ self.q_proj.weight.dtype
974
+ if self.q_lora_rank is None
975
+ else self.q_a_proj.weight.dtype
976
+ )
977
+
978
+ logger.warning_once(
979
+ f"The input hidden states seems to be silently casted in float32, this might be related to"
980
+ f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in"
981
+ f" {target_dtype}."
982
+ )
983
+
984
+ query_states = query_states.to(target_dtype)
985
+ key_states = key_states.to(target_dtype)
986
+ value_states = value_states.to(target_dtype)
987
+
988
+ attn_output = self._flash_attention_forward(
989
+ query_states,
990
+ key_states,
991
+ value_states,
992
+ attention_mask,
993
+ q_len,
994
+ dropout=dropout_rate,
995
+ softmax_scale=self.softmax_scale,
996
+ )
997
+ if self.q_head_dim != self.v_head_dim:
998
+ attn_output = attn_output[:, :, :, : self.v_head_dim]
999
+
1000
+ attn_output = attn_output.reshape(
1001
+ bsz, q_len, self.num_heads * self.v_head_dim
1002
+ ).contiguous()
1003
+ attn_output = self.o_proj(attn_output)
1004
+
1005
+ if not output_attentions:
1006
+ attn_weights = None
1007
+
1008
+ return attn_output, attn_weights, past_key_value
1009
+
1010
+ def _flash_attention_forward(
1011
+ self,
1012
+ query_states,
1013
+ key_states,
1014
+ value_states,
1015
+ attention_mask,
1016
+ query_length,
1017
+ dropout=0.0,
1018
+ softmax_scale=None,
1019
+ ):
1020
+ """
1021
+ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token
1022
+ first unpad the input, then computes the attention scores and pad the final attention scores.
1023
+ Args:
1024
+ query_states (`torch.Tensor`):
1025
+ Input query states to be passed to Flash Attention API
1026
+ key_states (`torch.Tensor`):
1027
+ Input key states to be passed to Flash Attention API
1028
+ value_states (`torch.Tensor`):
1029
+ Input value states to be passed to Flash Attention API
1030
+ attention_mask (`torch.Tensor`):
1031
+ The padding mask - corresponds to a tensor of size `(batch_size, seq_len)` where 0 stands for the
1032
+ position of padding tokens and 1 for the position of non-padding tokens.
1033
+ dropout (`int`, *optional*):
1034
+ Attention dropout
1035
+ softmax_scale (`float`, *optional*):
1036
+ The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim)
1037
+ """
1038
+ if not self._flash_attn_uses_top_left_mask:
1039
+ causal = self.is_causal
1040
+ else:
1041
+ # TODO: Remove the `query_length != 1` check once Flash Attention for RoCm is bumped to 2.1. For details, please see the comment in DeepseekV3FlashAttention2 __init__.
1042
+ causal = self.is_causal and query_length != 1
1043
+
1044
+ # Contains at least one padding token in the sequence
1045
+ if attention_mask is not None:
1046
+ batch_size = query_states.shape[0]
1047
+ (
1048
+ query_states,
1049
+ key_states,
1050
+ value_states,
1051
+ indices_q,
1052
+ cu_seq_lens,
1053
+ max_seq_lens,
1054
+ ) = self._upad_input(
1055
+ query_states, key_states, value_states, attention_mask, query_length
1056
+ )
1057
+
1058
+ cu_seqlens_q, cu_seqlens_k = cu_seq_lens
1059
+ max_seqlen_in_batch_q, max_seqlen_in_batch_k = max_seq_lens
1060
+
1061
+ attn_output_unpad = flash_attn_varlen_func(
1062
+ query_states,
1063
+ key_states,
1064
+ value_states,
1065
+ cu_seqlens_q=cu_seqlens_q,
1066
+ cu_seqlens_k=cu_seqlens_k,
1067
+ max_seqlen_q=max_seqlen_in_batch_q,
1068
+ max_seqlen_k=max_seqlen_in_batch_k,
1069
+ dropout_p=dropout,
1070
+ softmax_scale=softmax_scale,
1071
+ causal=causal,
1072
+ )
1073
+
1074
+ attn_output = pad_input(
1075
+ attn_output_unpad, indices_q, batch_size, query_length
1076
+ )
1077
+ else:
1078
+ attn_output = flash_attn_func(
1079
+ query_states,
1080
+ key_states,
1081
+ value_states,
1082
+ dropout,
1083
+ softmax_scale=softmax_scale,
1084
+ causal=causal,
1085
+ )
1086
+
1087
+ return attn_output
1088
+
1089
+ def _upad_input(
1090
+ self, query_layer, key_layer, value_layer, attention_mask, query_length
1091
+ ):
1092
+ indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask)
1093
+ batch_size, kv_seq_len, num_key_value_heads, head_dim = key_layer.shape
1094
+
1095
+ key_layer = index_first_axis(
1096
+ key_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim),
1097
+ indices_k,
1098
+ )
1099
+ value_layer = index_first_axis(
1100
+ value_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim),
1101
+ indices_k,
1102
+ )
1103
+ if query_length == kv_seq_len:
1104
+ query_layer = index_first_axis(
1105
+ query_layer.reshape(batch_size * kv_seq_len, self.num_heads, head_dim),
1106
+ indices_k,
1107
+ )
1108
+ cu_seqlens_q = cu_seqlens_k
1109
+ max_seqlen_in_batch_q = max_seqlen_in_batch_k
1110
+ indices_q = indices_k
1111
+ elif query_length == 1:
1112
+ max_seqlen_in_batch_q = 1
1113
+ cu_seqlens_q = torch.arange(
1114
+ batch_size + 1, dtype=torch.int32, device=query_layer.device
1115
+ ) # There is a memcpy here, that is very bad.
1116
+ indices_q = cu_seqlens_q[:-1]
1117
+ query_layer = query_layer.squeeze(1)
1118
+ else:
1119
+ # The -q_len: slice assumes left padding.
1120
+ attention_mask = attention_mask[:, -query_length:]
1121
+ query_layer, indices_q, cu_seqlens_q, max_seqlen_in_batch_q = unpad_input(
1122
+ query_layer, attention_mask
1123
+ )
1124
+
1125
+ return (
1126
+ query_layer,
1127
+ key_layer,
1128
+ value_layer,
1129
+ indices_q,
1130
+ (cu_seqlens_q, cu_seqlens_k),
1131
+ (max_seqlen_in_batch_q, max_seqlen_in_batch_k),
1132
+ )
1133
+
1134
+
1135
+ ATTENTION_CLASSES = {
1136
+ "eager": DeepseekV3Attention,
1137
+ "flash_attention_2": DeepseekV3FlashAttention2,
1138
+ }
1139
+
1140
+
1141
+ class DeepseekV3DecoderLayer(nn.Module):
1142
+ def __init__(self, config: DeepseekV3Config, layer_idx: int):
1143
+ super().__init__()
1144
+ self.hidden_size = config.hidden_size
1145
+
1146
+ self.self_attn = ATTENTION_CLASSES[config._attn_implementation](
1147
+ config=config, layer_idx=layer_idx
1148
+ )
1149
+
1150
+ self.mlp = (
1151
+ DeepseekV3MoE(config)
1152
+ if (
1153
+ config.n_routed_experts is not None
1154
+ and layer_idx >= config.first_k_dense_replace
1155
+ and layer_idx % config.moe_layer_freq == 0
1156
+ )
1157
+ else DeepseekV3MLP(config)
1158
+ )
1159
+ self.input_layernorm = DeepseekV3RMSNorm(
1160
+ config.hidden_size, eps=config.rms_norm_eps
1161
+ )
1162
+ self.post_attention_layernorm = DeepseekV3RMSNorm(
1163
+ config.hidden_size, eps=config.rms_norm_eps
1164
+ )
1165
+
1166
+ def forward(
1167
+ self,
1168
+ hidden_states: torch.Tensor,
1169
+ attention_mask: Optional[torch.Tensor] = None,
1170
+ position_ids: Optional[torch.LongTensor] = None,
1171
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
1172
+ output_attentions: Optional[bool] = False,
1173
+ use_cache: Optional[bool] = False,
1174
+ **kwargs,
1175
+ ) -> Tuple[
1176
+ torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]
1177
+ ]:
1178
+ """
1179
+ Args:
1180
+ hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
1181
+ attention_mask (`torch.FloatTensor`, *optional*):
1182
+ attention mask of size `(batch_size, sequence_length)` if flash attention is used or `(batch_size, 1,
1183
+ query_sequence_length, key_sequence_length)` if default attention is used.
1184
+ output_attentions (`bool`, *optional*):
1185
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under
1186
+ returned tensors for more detail.
1187
+ use_cache (`bool`, *optional*):
1188
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
1189
+ (see `past_key_values`).
1190
+ past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
1191
+ """
1192
+ if "padding_mask" in kwargs:
1193
+ warnings.warn(
1194
+ "Passing `padding_mask` is deprecated and will be removed in v4.37. Please make sure use `attention_mask` instead.`"
1195
+ )
1196
+ residual = hidden_states
1197
+
1198
+ hidden_states = self.input_layernorm(hidden_states)
1199
+
1200
+ # Self Attention
1201
+ hidden_states, self_attn_weights, present_key_value = self.self_attn(
1202
+ hidden_states=hidden_states,
1203
+ attention_mask=attention_mask,
1204
+ position_ids=position_ids,
1205
+ past_key_value=past_key_value,
1206
+ output_attentions=output_attentions,
1207
+ use_cache=use_cache,
1208
+ **kwargs,
1209
+ )
1210
+ hidden_states = residual + hidden_states
1211
+
1212
+ # Fully Connected
1213
+ residual = hidden_states
1214
+ hidden_states = self.post_attention_layernorm(hidden_states)
1215
+ hidden_states = self.mlp(hidden_states)
1216
+ hidden_states = residual + hidden_states
1217
+
1218
+ outputs = (hidden_states,)
1219
+
1220
+ if output_attentions:
1221
+ outputs += (self_attn_weights,)
1222
+
1223
+ if use_cache:
1224
+ outputs += (present_key_value,)
1225
+
1226
+ return outputs
1227
+
1228
+
1229
+ DeepseekV3_START_DOCSTRING = r"""
1230
+ This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
1231
+ library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
1232
+ etc.)
1233
+ This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
1234
+ Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
1235
+ and behavior.
1236
+ Parameters:
1237
+ config ([`DeepseekV3Config`]):
1238
+ Model configuration class with all the parameters of the model. Initializing with a config file does not
1239
+ load the weights associated with the model, only the configuration. Check out the
1240
+ [`~PreTrainedModel.from_pretrained`] method to load the model weights.
1241
+ """
1242
+
1243
+
1244
+ @add_start_docstrings(
1245
+ "The bare DeepseekV3 Model outputting raw hidden-states without any specific head on top.",
1246
+ DeepseekV3_START_DOCSTRING,
1247
+ )
1248
+ class DeepseekV3PreTrainedModel(PreTrainedModel):
1249
+ config_class = DeepseekV3Config
1250
+ base_model_prefix = "model"
1251
+ supports_gradient_checkpointing = True
1252
+ _no_split_modules = ["DeepseekV3DecoderLayer"]
1253
+ _skip_keys_device_placement = "past_key_values"
1254
+ _supports_flash_attn_2 = True
1255
+ _supports_cache_class = True
1256
+
1257
+ def _init_weights(self, module):
1258
+ std = self.config.initializer_range
1259
+ if isinstance(module, nn.Linear):
1260
+ module.weight.data.normal_(mean=0.0, std=std)
1261
+ if module.bias is not None:
1262
+ module.bias.data.zero_()
1263
+ elif isinstance(module, nn.Embedding):
1264
+ module.weight.data.normal_(mean=0.0, std=std)
1265
+ if module.padding_idx is not None:
1266
+ module.weight.data[module.padding_idx].zero_()
1267
+
1268
+
1269
+ DeepseekV3_INPUTS_DOCSTRING = r"""
1270
+ Args:
1271
+ input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
1272
+ Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
1273
+ it.
1274
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
1275
+ [`PreTrainedTokenizer.__call__`] for details.
1276
+ [What are input IDs?](../glossary#input-ids)
1277
+ attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
1278
+ Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
1279
+ - 1 for tokens that are **not masked**,
1280
+ - 0 for tokens that are **masked**.
1281
+ [What are attention masks?](../glossary#attention-mask)
1282
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
1283
+ [`PreTrainedTokenizer.__call__`] for details.
1284
+ If `past_key_values` is used, optionally only the last `input_ids` have to be input (see
1285
+ `past_key_values`).
1286
+ If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
1287
+ and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
1288
+ information on the default strategy.
1289
+ - 1 indicates the head is **not masked**,
1290
+ - 0 indicates the head is **masked**.
1291
+ position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1292
+ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
1293
+ config.n_positions - 1]`.
1294
+ [What are position IDs?](../glossary#position-ids)
1295
+ past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):
1296
+ Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
1297
+ blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
1298
+ returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.
1299
+ Two formats are allowed:
1300
+ - a [`~cache_utils.Cache`] instance;
1301
+ - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
1302
+ shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy
1303
+ cache format.
1304
+ The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the
1305
+ legacy cache format will be returned.
1306
+ If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
1307
+ have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
1308
+ of shape `(batch_size, sequence_length)`.
1309
+ inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
1310
+ Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
1311
+ is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
1312
+ model's internal embedding lookup matrix.
1313
+ use_cache (`bool`, *optional*):
1314
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
1315
+ `past_key_values`).
1316
+ output_attentions (`bool`, *optional*):
1317
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
1318
+ tensors for more detail.
1319
+ output_hidden_states (`bool`, *optional*):
1320
+ Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
1321
+ more detail.
1322
+ return_dict (`bool`, *optional*):
1323
+ Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
1324
+ """
1325
+
1326
+
1327
+ @add_start_docstrings(
1328
+ "The bare DeepseekV3 Model outputting raw hidden-states without any specific head on top.",
1329
+ DeepseekV3_START_DOCSTRING,
1330
+ )
1331
+ class DeepseekV3Model(DeepseekV3PreTrainedModel):
1332
+ """
1333
+ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`DeepseekV3DecoderLayer`]
1334
+ Args:
1335
+ config: DeepseekV3Config
1336
+ """
1337
+
1338
+ def __init__(self, config: DeepseekV3Config):
1339
+ super().__init__(config)
1340
+ self.padding_idx = config.pad_token_id
1341
+ self.vocab_size = config.vocab_size
1342
+
1343
+ self.embed_tokens = nn.Embedding(
1344
+ config.vocab_size, config.hidden_size, self.padding_idx
1345
+ )
1346
+ self.layers = nn.ModuleList(
1347
+ [
1348
+ DeepseekV3DecoderLayer(config, layer_idx)
1349
+ for layer_idx in range(config.num_hidden_layers)
1350
+ ]
1351
+ )
1352
+ self._use_flash_attention_2 = config._attn_implementation == "flash_attention_2"
1353
+ self.norm = DeepseekV3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
1354
+
1355
+ self.gradient_checkpointing = False
1356
+ # Initialize weights and apply final processing
1357
+ self.post_init()
1358
+
1359
+ def get_input_embeddings(self):
1360
+ return self.embed_tokens
1361
+
1362
+ def set_input_embeddings(self, value):
1363
+ self.embed_tokens = value
1364
+
1365
+ @add_start_docstrings_to_model_forward(DeepseekV3_INPUTS_DOCSTRING)
1366
+ def forward(
1367
+ self,
1368
+ input_ids: torch.LongTensor = None,
1369
+ attention_mask: Optional[torch.Tensor] = None,
1370
+ position_ids: Optional[torch.LongTensor] = None,
1371
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1372
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1373
+ use_cache: Optional[bool] = None,
1374
+ output_attentions: Optional[bool] = None,
1375
+ output_hidden_states: Optional[bool] = None,
1376
+ return_dict: Optional[bool] = None,
1377
+ ) -> Union[Tuple, BaseModelOutputWithPast]:
1378
+ output_attentions = (
1379
+ output_attentions
1380
+ if output_attentions is not None
1381
+ else self.config.output_attentions
1382
+ )
1383
+ output_hidden_states = (
1384
+ output_hidden_states
1385
+ if output_hidden_states is not None
1386
+ else self.config.output_hidden_states
1387
+ )
1388
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
1389
+
1390
+ return_dict = (
1391
+ return_dict if return_dict is not None else self.config.use_return_dict
1392
+ )
1393
+
1394
+ # retrieve input_ids and inputs_embeds
1395
+ if input_ids is not None and inputs_embeds is not None:
1396
+ raise ValueError(
1397
+ "You cannot specify both input_ids and inputs_embeds at the same time"
1398
+ )
1399
+ elif input_ids is not None:
1400
+ batch_size, seq_length = input_ids.shape[:2]
1401
+ elif inputs_embeds is not None:
1402
+ batch_size, seq_length = inputs_embeds.shape[:2]
1403
+ else:
1404
+ raise ValueError("You have to specify either input_ids or inputs_embeds")
1405
+
1406
+ past_key_values_length = 0
1407
+ if use_cache:
1408
+ use_legacy_cache = not isinstance(past_key_values, Cache)
1409
+ if use_legacy_cache:
1410
+ past_key_values = DynamicCache.from_legacy_cache(past_key_values)
1411
+ past_key_values_length = past_key_values.get_usable_length(seq_length)
1412
+
1413
+ if position_ids is None:
1414
+ device = input_ids.device if input_ids is not None else inputs_embeds.device
1415
+ position_ids = torch.arange(
1416
+ past_key_values_length,
1417
+ seq_length + past_key_values_length,
1418
+ dtype=torch.long,
1419
+ device=device,
1420
+ )
1421
+ position_ids = position_ids.unsqueeze(0)
1422
+
1423
+ if inputs_embeds is None:
1424
+ inputs_embeds = self.embed_tokens(input_ids)
1425
+
1426
+ if self._use_flash_attention_2:
1427
+ # 2d mask is passed through the layers
1428
+ attention_mask = (
1429
+ attention_mask
1430
+ if (attention_mask is not None and 0 in attention_mask)
1431
+ else None
1432
+ )
1433
+ else:
1434
+ # 4d mask is passed through the layers
1435
+ attention_mask = _prepare_4d_causal_attention_mask(
1436
+ attention_mask,
1437
+ (batch_size, seq_length),
1438
+ inputs_embeds,
1439
+ past_key_values_length,
1440
+ )
1441
+
1442
+ # embed positions
1443
+ hidden_states = inputs_embeds
1444
+
1445
+ # decoder layers
1446
+ all_hidden_states = () if output_hidden_states else None
1447
+ all_self_attns = () if output_attentions else None
1448
+ next_decoder_cache = None
1449
+
1450
+ for decoder_layer in self.layers:
1451
+ if output_hidden_states:
1452
+ all_hidden_states += (hidden_states,)
1453
+
1454
+ layer_outputs = decoder_layer(
1455
+ hidden_states,
1456
+ attention_mask=attention_mask,
1457
+ position_ids=position_ids,
1458
+ past_key_value=past_key_values,
1459
+ output_attentions=output_attentions,
1460
+ use_cache=use_cache,
1461
+ )
1462
+
1463
+ hidden_states = layer_outputs[0]
1464
+
1465
+ if use_cache:
1466
+ next_decoder_cache = layer_outputs[2 if output_attentions else 1]
1467
+
1468
+ if output_attentions:
1469
+ all_self_attns += (layer_outputs[1],)
1470
+
1471
+ hidden_states = self.norm(hidden_states)
1472
+
1473
+ # add hidden states from the last decoder layer
1474
+ if output_hidden_states:
1475
+ all_hidden_states += (hidden_states,)
1476
+
1477
+ next_cache = None
1478
+ if use_cache:
1479
+ next_cache = (
1480
+ next_decoder_cache.to_legacy_cache()
1481
+ if use_legacy_cache
1482
+ else next_decoder_cache
1483
+ )
1484
+ if not return_dict:
1485
+ return tuple(
1486
+ v
1487
+ for v in [hidden_states, next_cache, all_hidden_states, all_self_attns]
1488
+ if v is not None
1489
+ )
1490
+ return BaseModelOutputWithPast(
1491
+ last_hidden_state=hidden_states,
1492
+ past_key_values=next_cache,
1493
+ hidden_states=all_hidden_states,
1494
+ attentions=all_self_attns,
1495
+ )
1496
+
1497
+
1498
+ class DeepseekV3ForCausalLM(DeepseekV3PreTrainedModel):
1499
+ _tied_weights_keys = ["lm_head.weight"]
1500
+
1501
+ def __init__(self, config):
1502
+ super().__init__(config)
1503
+ self.model = DeepseekV3Model(config)
1504
+ self.vocab_size = config.vocab_size
1505
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
1506
+
1507
+ # Initialize weights and apply final processing
1508
+ self.post_init()
1509
+
1510
+ def get_input_embeddings(self):
1511
+ return self.model.embed_tokens
1512
+
1513
+ def set_input_embeddings(self, value):
1514
+ self.model.embed_tokens = value
1515
+
1516
+ def get_output_embeddings(self):
1517
+ return self.lm_head
1518
+
1519
+ def set_output_embeddings(self, new_embeddings):
1520
+ self.lm_head = new_embeddings
1521
+
1522
+ def set_decoder(self, decoder):
1523
+ self.model = decoder
1524
+
1525
+ def get_decoder(self):
1526
+ return self.model
1527
+
1528
+ @add_start_docstrings_to_model_forward(DeepseekV3_INPUTS_DOCSTRING)
1529
+ @replace_return_docstrings(
1530
+ output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC
1531
+ )
1532
+ def forward(
1533
+ self,
1534
+ input_ids: torch.LongTensor = None,
1535
+ attention_mask: Optional[torch.Tensor] = None,
1536
+ position_ids: Optional[torch.LongTensor] = None,
1537
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1538
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1539
+ labels: Optional[torch.LongTensor] = None,
1540
+ use_cache: Optional[bool] = None,
1541
+ output_attentions: Optional[bool] = None,
1542
+ output_hidden_states: Optional[bool] = None,
1543
+ return_dict: Optional[bool] = None,
1544
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
1545
+ r"""
1546
+ Args:
1547
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1548
+ Labels for computing the masked language modeling loss. Indices should either be in `[0, transformers.,
1549
+ config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
1550
+ (masked), the loss is only computed for the tokens with labels in `[0, transformers., config.vocab_size]`.
1551
+ Returns:
1552
+ Example:
1553
+ ```python
1554
+ >>> from transformers import AutoTokenizer, DeepseekV3ForCausalLM
1555
+ >>> model = DeepseekV3ForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
1556
+ >>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
1557
+ >>> prompt = "Hey, are you conscious? Can you talk to me?"
1558
+ >>> inputs = tokenizer(prompt, return_tensors="pt")
1559
+ >>> # Generate
1560
+ >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
1561
+ >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
1562
+ "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
1563
+ ```"""
1564
+ output_attentions = (
1565
+ output_attentions
1566
+ if output_attentions is not None
1567
+ else self.config.output_attentions
1568
+ )
1569
+ output_hidden_states = (
1570
+ output_hidden_states
1571
+ if output_hidden_states is not None
1572
+ else self.config.output_hidden_states
1573
+ )
1574
+ return_dict = (
1575
+ return_dict if return_dict is not None else self.config.use_return_dict
1576
+ )
1577
+
1578
+ # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
1579
+ outputs = self.model(
1580
+ input_ids=input_ids,
1581
+ attention_mask=attention_mask,
1582
+ position_ids=position_ids,
1583
+ past_key_values=past_key_values,
1584
+ inputs_embeds=inputs_embeds,
1585
+ use_cache=use_cache,
1586
+ output_attentions=output_attentions,
1587
+ output_hidden_states=output_hidden_states,
1588
+ return_dict=return_dict,
1589
+ )
1590
+
1591
+ hidden_states = outputs[0]
1592
+ logits = self.lm_head(hidden_states)
1593
+ logits = logits.float()
1594
+
1595
+ loss = None
1596
+ if labels is not None:
1597
+ # Shift so that tokens < n predict n
1598
+ shift_logits = logits[..., :-1, :].contiguous()
1599
+ shift_labels = labels[..., 1:].contiguous()
1600
+ # Flatten the tokens
1601
+ loss_fct = CrossEntropyLoss()
1602
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
1603
+ shift_labels = shift_labels.view(-1)
1604
+ # Enable model parallelism
1605
+ shift_labels = shift_labels.to(shift_logits.device)
1606
+ loss = loss_fct(shift_logits, shift_labels)
1607
+
1608
+ if not return_dict:
1609
+ output = (logits,) + outputs[1:]
1610
+ return (loss,) + output if loss is not None else output
1611
+
1612
+ return CausalLMOutputWithPast(
1613
+ loss=loss,
1614
+ logits=logits,
1615
+ past_key_values=outputs.past_key_values,
1616
+ hidden_states=outputs.hidden_states,
1617
+ attentions=outputs.attentions,
1618
+ )
1619
+
1620
+ def prepare_inputs_for_generation(
1621
+ self,
1622
+ input_ids,
1623
+ past_key_values=None,
1624
+ attention_mask=None,
1625
+ inputs_embeds=None,
1626
+ **kwargs,
1627
+ ):
1628
+ if past_key_values is not None:
1629
+ if isinstance(past_key_values, Cache):
1630
+ cache_length = past_key_values.get_seq_length()
1631
+ past_length = past_key_values.seen_tokens
1632
+ max_cache_length = past_key_values.get_max_length()
1633
+ else:
1634
+ cache_length = past_length = past_key_values[0][0].shape[2]
1635
+ max_cache_length = None
1636
+
1637
+ # Keep only the unprocessed tokens:
1638
+ # 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where
1639
+ # some of the inputs are exclusivelly passed as part of the cache (e.g. when passing input_embeds as
1640
+ # input)
1641
+ if (
1642
+ attention_mask is not None
1643
+ and attention_mask.shape[1] > input_ids.shape[1]
1644
+ ):
1645
+ input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :]
1646
+ # 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard
1647
+ # input_ids based on the past_length.
1648
+ elif past_length < input_ids.shape[1]:
1649
+ input_ids = input_ids[:, past_length:]
1650
+ # 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.
1651
+
1652
+ # If we are about to go beyond the maximum cache length, we need to crop the input attention mask.
1653
+ if (
1654
+ max_cache_length is not None
1655
+ and attention_mask is not None
1656
+ and cache_length + input_ids.shape[1] > max_cache_length
1657
+ ):
1658
+ attention_mask = attention_mask[:, -max_cache_length:]
1659
+
1660
+ position_ids = kwargs.get("position_ids", None)
1661
+ if attention_mask is not None and position_ids is None:
1662
+ # create position_ids on the fly for batch generation
1663
+ position_ids = attention_mask.long().cumsum(-1) - 1
1664
+ position_ids.masked_fill_(attention_mask == 0, 1)
1665
+ if past_key_values:
1666
+ position_ids = position_ids[:, -input_ids.shape[1] :]
1667
+
1668
+ # if `inputs_embeds` are passed, we only want to use them in the 1st generation step
1669
+ if inputs_embeds is not None and past_key_values is None:
1670
+ model_inputs = {"inputs_embeds": inputs_embeds}
1671
+ else:
1672
+ model_inputs = {"input_ids": input_ids}
1673
+
1674
+ model_inputs.update(
1675
+ {
1676
+ "position_ids": position_ids,
1677
+ "past_key_values": past_key_values,
1678
+ "use_cache": kwargs.get("use_cache"),
1679
+ "attention_mask": attention_mask,
1680
+ }
1681
+ )
1682
+ return model_inputs
1683
+
1684
+ @staticmethod
1685
+ def _reorder_cache(past_key_values, beam_idx):
1686
+ reordered_past = ()
1687
+ for layer_past in past_key_values:
1688
+ reordered_past += (
1689
+ tuple(
1690
+ past_state.index_select(0, beam_idx.to(past_state.device))
1691
+ for past_state in layer_past
1692
+ ),
1693
+ )
1694
+ return reordered_past
1695
+
1696
+
1697
+ @add_start_docstrings(
1698
+ """
1699
+ The DeepseekV3 Model transformer with a sequence classification head on top (linear layer).
1700
+ [`DeepseekV3ForSequenceClassification`] uses the last token in order to do the classification, as other causal models
1701
+ (e.g. GPT-2) do.
1702
+ Since it does classification on the last token, it requires to know the position of the last token. If a
1703
+ `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
1704
+ no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
1705
+ padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
1706
+ each row of the batch).
1707
+ """,
1708
+ DeepseekV3_START_DOCSTRING,
1709
+ )
1710
+ class DeepseekV3ForSequenceClassification(DeepseekV3PreTrainedModel):
1711
+ def __init__(self, config):
1712
+ super().__init__(config)
1713
+ self.num_labels = config.num_labels
1714
+ self.model = DeepseekV3Model(config)
1715
+ self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
1716
+
1717
+ # Initialize weights and apply final processing
1718
+ self.post_init()
1719
+
1720
+ def get_input_embeddings(self):
1721
+ return self.model.embed_tokens
1722
+
1723
+ def set_input_embeddings(self, value):
1724
+ self.model.embed_tokens = value
1725
+
1726
+ @add_start_docstrings_to_model_forward(DeepseekV3_INPUTS_DOCSTRING)
1727
+ def forward(
1728
+ self,
1729
+ input_ids: torch.LongTensor = None,
1730
+ attention_mask: Optional[torch.Tensor] = None,
1731
+ position_ids: Optional[torch.LongTensor] = None,
1732
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1733
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1734
+ labels: Optional[torch.LongTensor] = None,
1735
+ use_cache: Optional[bool] = None,
1736
+ output_attentions: Optional[bool] = None,
1737
+ output_hidden_states: Optional[bool] = None,
1738
+ return_dict: Optional[bool] = None,
1739
+ ) -> Union[Tuple, SequenceClassifierOutputWithPast]:
1740
+ r"""
1741
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1742
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, transformers.,
1743
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
1744
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
1745
+ """
1746
+ return_dict = (
1747
+ return_dict if return_dict is not None else self.config.use_return_dict
1748
+ )
1749
+
1750
+ transformer_outputs = self.model(
1751
+ input_ids,
1752
+ attention_mask=attention_mask,
1753
+ position_ids=position_ids,
1754
+ past_key_values=past_key_values,
1755
+ inputs_embeds=inputs_embeds,
1756
+ use_cache=use_cache,
1757
+ output_attentions=output_attentions,
1758
+ output_hidden_states=output_hidden_states,
1759
+ return_dict=return_dict,
1760
+ )
1761
+ hidden_states = transformer_outputs[0]
1762
+ logits = self.score(hidden_states)
1763
+
1764
+ if input_ids is not None:
1765
+ batch_size = input_ids.shape[0]
1766
+ else:
1767
+ batch_size = inputs_embeds.shape[0]
1768
+
1769
+ if self.config.pad_token_id is None and batch_size != 1:
1770
+ raise ValueError(
1771
+ "Cannot handle batch sizes > 1 if no padding token is defined."
1772
+ )
1773
+ if self.config.pad_token_id is None:
1774
+ sequence_lengths = -1
1775
+ else:
1776
+ if input_ids is not None:
1777
+ sequence_lengths = (
1778
+ torch.eq(input_ids, self.config.pad_token_id).int().argmax(-1) - 1
1779
+ ).to(logits.device)
1780
+ else:
1781
+ sequence_lengths = -1
1782
+
1783
+ pooled_logits = logits[
1784
+ torch.arange(batch_size, device=logits.device), sequence_lengths
1785
+ ]
1786
+
1787
+ loss = None
1788
+ if labels is not None:
1789
+ labels = labels.to(logits.device)
1790
+ if self.config.problem_type is None:
1791
+ if self.num_labels == 1:
1792
+ self.config.problem_type = "regression"
1793
+ elif self.num_labels > 1 and (
1794
+ labels.dtype == torch.long or labels.dtype == torch.int
1795
+ ):
1796
+ self.config.problem_type = "single_label_classification"
1797
+ else:
1798
+ self.config.problem_type = "multi_label_classification"
1799
+
1800
+ if self.config.problem_type == "regression":
1801
+ loss_fct = MSELoss()
1802
+ if self.num_labels == 1:
1803
+ loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
1804
+ else:
1805
+ loss = loss_fct(pooled_logits, labels)
1806
+ elif self.config.problem_type == "single_label_classification":
1807
+ loss_fct = CrossEntropyLoss()
1808
+ loss = loss_fct(
1809
+ pooled_logits.view(-1, self.num_labels), labels.view(-1)
1810
+ )
1811
+ elif self.config.problem_type == "multi_label_classification":
1812
+ loss_fct = BCEWithLogitsLoss()
1813
+ loss = loss_fct(pooled_logits, labels)
1814
+ if not return_dict:
1815
+ output = (pooled_logits,) + transformer_outputs[1:]
1816
+ return ((loss,) + output) if loss is not None else output
1817
+
1818
+ return SequenceClassifierOutputWithPast(
1819
+ loss=loss,
1820
+ logits=pooled_logits,
1821
+ past_key_values=transformer_outputs.past_key_values,
1822
+ hidden_states=transformer_outputs.hidden_states,
1823
+ attentions=transformer_outputs.attentions,
1824
+ )