Rename modeling_stablelm.py to modeling_stablelm_epoch.py
Browse files
modeling_stablelm.py → modeling_stablelm_epoch.py
RENAMED
@@ -1,10 +1,5 @@
|
|
1 |
# coding=utf-8
|
2 |
-
# Copyright
|
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.
|
@@ -17,48 +12,48 @@
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
21 |
import math
|
22 |
-
|
23 |
|
24 |
import torch
|
25 |
import torch.nn.functional as F
|
26 |
import torch.utils.checkpoint
|
27 |
from torch import nn
|
28 |
-
from torch.nn import
|
29 |
|
30 |
-
from transformers.
|
31 |
-
from transformers.
|
32 |
-
|
33 |
-
|
34 |
-
from transformers.modeling_utils import PreTrainedModel
|
35 |
-
from transformers.utils import (
|
36 |
-
add_start_docstrings,
|
37 |
-
add_start_docstrings_to_model_forward,
|
38 |
-
is_flash_attn_2_available,
|
39 |
-
is_flash_attn_greater_or_equal_2_10,
|
40 |
-
logging,
|
41 |
-
replace_return_docstrings,
|
42 |
)
|
43 |
-
from .
|
|
|
44 |
|
|
|
45 |
|
46 |
-
|
47 |
from flash_attn import flash_attn_func, flash_attn_varlen_func
|
48 |
-
from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input
|
|
|
|
|
|
|
49 |
|
50 |
|
51 |
logger = logging.get_logger(__name__)
|
52 |
|
53 |
-
_CONFIG_FOR_DOC = "StableLmConfig"
|
54 |
-
|
55 |
|
56 |
# Copied from transformers.models.llama.modeling_llama._get_unpad_data
|
57 |
def _get_unpad_data(attention_mask):
|
58 |
seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
|
59 |
indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
|
60 |
max_seqlen_in_batch = seqlens_in_batch.max().item()
|
61 |
-
cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.int32), (1, 0))
|
62 |
return (
|
63 |
indices,
|
64 |
cu_seqlens,
|
@@ -66,144 +61,113 @@ def _get_unpad_data(attention_mask):
|
|
66 |
)
|
67 |
|
68 |
|
69 |
-
# Copied from transformers.models.
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
super().__init__()
|
73 |
|
74 |
self.dim = dim
|
75 |
self.max_position_embeddings = max_position_embeddings
|
76 |
self.base = base
|
77 |
-
inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2, dtype=torch.
|
78 |
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
79 |
|
80 |
# Build here to make `torch.jit.trace` work.
|
81 |
self._set_cos_sin_cache(
|
82 |
-
seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
|
83 |
)
|
84 |
|
85 |
-
def _set_cos_sin_cache(self, seq_len, device, dtype):
|
86 |
self.max_seq_len_cached = seq_len
|
87 |
-
t = torch.arange(self.max_seq_len_cached, device=device, dtype=torch.
|
88 |
|
|
|
|
|
89 |
freqs = torch.outer(t, self.inv_freq)
|
90 |
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
91 |
emb = torch.cat((freqs, freqs), dim=-1)
|
92 |
-
self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
|
93 |
-
self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
|
94 |
|
95 |
-
def forward(self, x, seq_len=None):
|
96 |
-
# x: [
|
97 |
if seq_len > self.max_seq_len_cached:
|
98 |
-
self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=
|
99 |
-
|
100 |
return (
|
101 |
-
self.cos_cached[:seq_len].to(dtype=x.dtype),
|
102 |
-
self.sin_cached[:seq_len].to(dtype=x.dtype),
|
103 |
)
|
104 |
|
105 |
|
106 |
-
|
107 |
-
class StableLmLinearScalingRotaryEmbedding(StableLmRotaryEmbedding):
|
108 |
-
"""StableLmRotaryEmbedding extended with linear scaling. Credits to the Reddit user /u/kaiokendev"""
|
109 |
-
|
110 |
-
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
|
111 |
-
self.scaling_factor = scaling_factor
|
112 |
-
super().__init__(dim, max_position_embeddings, base, device)
|
113 |
-
|
114 |
-
def _set_cos_sin_cache(self, seq_len, device, dtype):
|
115 |
-
self.max_seq_len_cached = seq_len
|
116 |
-
t = torch.arange(self.max_seq_len_cached, device=device, dtype=torch.int64).type_as(self.inv_freq)
|
117 |
-
t = t / self.scaling_factor
|
118 |
-
|
119 |
-
freqs = torch.outer(t, self.inv_freq)
|
120 |
-
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
121 |
-
emb = torch.cat((freqs, freqs), dim=-1)
|
122 |
-
self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
|
123 |
-
self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
|
124 |
-
|
125 |
-
|
126 |
-
# Copied from transformers.models.falcon.modeling_falcon.FalconDynamicNTKScalingRotaryEmbedding with Falcon->StableLm
|
127 |
-
class StableLmDynamicNTKScalingRotaryEmbedding(StableLmRotaryEmbedding):
|
128 |
-
"""StableLmRotaryEmbedding extended with Dynamic NTK scaling. Credits to the Reddit users /u/bloc97 and /u/emozilla"""
|
129 |
-
|
130 |
-
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
|
131 |
-
self.scaling_factor = scaling_factor
|
132 |
-
super().__init__(dim, max_position_embeddings, base, device)
|
133 |
-
|
134 |
-
def _set_cos_sin_cache(self, seq_len, device, dtype):
|
135 |
-
self.max_seq_len_cached = seq_len
|
136 |
-
|
137 |
-
if seq_len > self.max_position_embeddings:
|
138 |
-
base = self.base * (
|
139 |
-
(self.scaling_factor * seq_len / self.max_position_embeddings) - (self.scaling_factor - 1)
|
140 |
-
) ** (self.dim / (self.dim - 2))
|
141 |
-
inv_freq = 1.0 / (base ** (torch.arange(0, self.dim, 2, dtype=torch.int64).float().to(device) / self.dim))
|
142 |
-
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
143 |
-
|
144 |
-
t = torch.arange(self.max_seq_len_cached, device=device, dtype=torch.int64).type_as(self.inv_freq)
|
145 |
-
|
146 |
-
freqs = torch.outer(t, self.inv_freq)
|
147 |
-
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
148 |
-
emb = torch.cat((freqs, freqs), dim=-1)
|
149 |
-
self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
|
150 |
-
self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
|
151 |
-
|
152 |
-
|
153 |
-
# Copied from transformers.models.llama.modeling_llama.rotate_half
|
154 |
-
def rotate_half(x):
|
155 |
"""Rotates half the hidden dims of the input."""
|
156 |
-
x1 = x
|
157 |
-
x2 = x[..., x.shape[-1] // 2 :]
|
158 |
return torch.cat((-x2, x1), dim=-1)
|
159 |
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
k (`torch.Tensor`): The key tensor.
|
168 |
-
cos (`torch.Tensor`): The cosine part of the rotary embedding.
|
169 |
-
sin (`torch.Tensor`): The sine part of the rotary embedding.
|
170 |
-
position_ids (`torch.Tensor`):
|
171 |
-
The position indices of the tokens corresponding to the query and key tensors. For example, this can be
|
172 |
-
used to pass offsetted position ids when working with a KV-cache.
|
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[position_ids].unsqueeze(unsqueeze_dim)
|
184 |
-
sin = sin[position_ids].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 |
-
|
191 |
-
|
192 |
-
def __init__(self, config):
|
193 |
super().__init__()
|
194 |
self.config = config
|
195 |
self.hidden_size = config.hidden_size
|
196 |
self.intermediate_size = config.intermediate_size
|
197 |
-
self.gate_proj = nn.Linear(
|
198 |
-
self.up_proj = nn.Linear(
|
199 |
-
self.down_proj = nn.Linear(
|
200 |
-
self.act_fn =
|
201 |
|
202 |
-
def forward(self, x):
|
203 |
return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
|
204 |
|
205 |
|
206 |
-
# Copied from transformers.models.llama.modeling_llama.repeat_kv
|
207 |
def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
|
208 |
"""
|
209 |
This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
|
@@ -216,28 +180,16 @@ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
|
|
216 |
return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
|
217 |
|
218 |
|
219 |
-
class
|
220 |
-
|
221 |
-
|
222 |
-
def __init__(self, config: StableLmConfig, layer_idx: Optional[int] = None):
|
223 |
super().__init__()
|
224 |
self.config = config
|
225 |
-
self.layer_idx = layer_idx
|
226 |
-
if layer_idx is None:
|
227 |
-
logger.warning_once(
|
228 |
-
f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will "
|
229 |
-
"lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` "
|
230 |
-
"when creating this class."
|
231 |
-
)
|
232 |
-
|
233 |
self.hidden_size = config.hidden_size
|
234 |
self.num_heads = config.num_attention_heads
|
235 |
self.head_dim = self.hidden_size // self.num_heads
|
236 |
self.num_key_value_heads = config.num_key_value_heads
|
237 |
self.num_key_value_groups = self.num_heads // self.num_key_value_heads
|
238 |
self.max_position_embeddings = config.max_position_embeddings
|
239 |
-
self.rope_theta = config.rope_theta
|
240 |
-
self.partial_rotary_factor = config.partial_rotary_factor
|
241 |
self.is_causal = True
|
242 |
|
243 |
if (self.head_dim * self.num_heads) != self.hidden_size:
|
@@ -245,50 +197,30 @@ class StableLmAttention(nn.Module):
|
|
245 |
f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
|
246 |
f" and `num_heads`: {self.num_heads})."
|
247 |
)
|
|
|
248 |
self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=config.use_qkv_bias)
|
249 |
self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.use_qkv_bias)
|
250 |
self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.use_qkv_bias)
|
251 |
self.o_proj = nn.Linear(self.hidden_size, self.hidden_size, bias=False)
|
252 |
|
253 |
-
self.attention_dropout = nn.Dropout(config.attention_dropout)
|
254 |
self._init_rope()
|
255 |
|
256 |
-
# Copied from transformers.models.persimmon.modeling_persimmon.PersimmonAttention._init_rope with Persimmon->StableLm
|
257 |
def _init_rope(self):
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
else:
|
265 |
-
scaling_type = self.config.rope_scaling["type"]
|
266 |
-
scaling_factor = self.config.rope_scaling["factor"]
|
267 |
-
if scaling_type == "linear":
|
268 |
-
self.rotary_emb = StableLmLinearScalingRotaryEmbedding(
|
269 |
-
int(self.partial_rotary_factor * self.head_dim),
|
270 |
-
max_position_embeddings=self.max_position_embeddings,
|
271 |
-
scaling_factor=scaling_factor,
|
272 |
-
base=self.rope_theta,
|
273 |
-
)
|
274 |
-
elif scaling_type == "dynamic":
|
275 |
-
self.rotary_emb = StableLmDynamicNTKScalingRotaryEmbedding(
|
276 |
-
int(self.partial_rotary_factor * self.head_dim),
|
277 |
-
max_position_embeddings=self.max_position_embeddings,
|
278 |
-
scaling_factor=scaling_factor,
|
279 |
-
base=self.rope_theta,
|
280 |
-
)
|
281 |
-
else:
|
282 |
-
raise ValueError(f"Unknown RoPE scaling type {scaling_type}")
|
283 |
|
284 |
def forward(
|
285 |
self,
|
286 |
-
hidden_states: torch.
|
287 |
-
attention_mask:
|
288 |
-
position_ids:
|
289 |
-
past_key_value: Optional[
|
290 |
-
output_attentions: bool = False,
|
291 |
-
use_cache: bool = False,
|
292 |
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
293 |
bsz, q_len, _ = hidden_states.size()
|
294 |
|
@@ -300,37 +232,27 @@ class StableLmAttention(nn.Module):
|
|
300 |
key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
301 |
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
302 |
|
|
|
|
|
|
|
|
|
|
|
303 |
kv_seq_len = key_states.shape[-2]
|
304 |
if past_key_value is not None:
|
305 |
-
|
306 |
-
raise ValueError(
|
307 |
-
f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
|
308 |
-
"for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
|
309 |
-
"with a layer index."
|
310 |
-
)
|
311 |
-
kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
|
312 |
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
|
|
|
313 |
|
314 |
-
#
|
315 |
-
|
316 |
-
|
317 |
-
query_states[..., self.rotary_emb.dim :],
|
318 |
-
)
|
319 |
-
key_rot, key_pass = (
|
320 |
-
key_states[..., : self.rotary_emb.dim],
|
321 |
-
key_states[..., self.rotary_emb.dim :],
|
322 |
-
)
|
323 |
-
# [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor]
|
324 |
-
query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin, position_ids)
|
325 |
-
|
326 |
-
# [batch_size, seq_length, num_heads, head_dim]
|
327 |
-
query_states = torch.cat((query_rot, query_pass), dim=-1)
|
328 |
-
key_states = torch.cat((key_rot, key_pass), dim=-1)
|
329 |
|
330 |
if past_key_value is not None:
|
331 |
-
#
|
332 |
-
|
333 |
-
|
|
|
|
|
334 |
|
335 |
# Repeat k/v heads if n_kv_heads < n_heads
|
336 |
key_states = repeat_kv(key_states, self.num_key_value_groups)
|
@@ -351,10 +273,8 @@ class StableLmAttention(nn.Module):
|
|
351 |
)
|
352 |
attn_weights = attn_weights + attention_mask
|
353 |
|
354 |
-
#
|
355 |
-
attn_weights = nn.functional.softmax(attn_weights, dtype=torch.float32
|
356 |
-
attn_weights = self.attention_dropout(attn_weights)
|
357 |
-
|
358 |
attn_output = torch.matmul(attn_weights, value_states)
|
359 |
|
360 |
if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
|
@@ -363,9 +283,11 @@ class StableLmAttention(nn.Module):
|
|
363 |
f" {attn_output.size()}"
|
364 |
)
|
365 |
|
|
|
366 |
attn_output = attn_output.transpose(1, 2).contiguous()
|
367 |
attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
|
368 |
|
|
|
369 |
attn_output = self.o_proj(attn_output)
|
370 |
|
371 |
if not output_attentions:
|
@@ -374,110 +296,11 @@ class StableLmAttention(nn.Module):
|
|
374 |
return attn_output, attn_weights, past_key_value
|
375 |
|
376 |
|
377 |
-
class
|
378 |
-
def forward(
|
379 |
-
self,
|
380 |
-
hidden_states: torch.Tensor,
|
381 |
-
attention_mask: Optional[torch.Tensor] = None,
|
382 |
-
position_ids: Optional[torch.LongTensor] = None,
|
383 |
-
past_key_value: Optional[Cache] = None,
|
384 |
-
output_attentions: bool = False,
|
385 |
-
use_cache: bool = False,
|
386 |
-
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
387 |
-
if output_attentions:
|
388 |
-
# TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented.
|
389 |
-
logger.warning_once(
|
390 |
-
"StableLmModel is using StableLmSdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, "
|
391 |
-
'but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.'
|
392 |
-
)
|
393 |
-
return super().forward(
|
394 |
-
hidden_states=hidden_states,
|
395 |
-
attention_mask=attention_mask,
|
396 |
-
position_ids=position_ids,
|
397 |
-
past_key_value=past_key_value,
|
398 |
-
output_attentions=output_attentions,
|
399 |
-
use_cache=use_cache,
|
400 |
-
)
|
401 |
-
|
402 |
-
bsz, q_len, _ = hidden_states.size()
|
403 |
-
|
404 |
-
query_states = self.q_proj(hidden_states)
|
405 |
-
key_states = self.k_proj(hidden_states)
|
406 |
-
value_states = self.v_proj(hidden_states)
|
407 |
-
|
408 |
-
query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
|
409 |
-
key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
410 |
-
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
411 |
-
|
412 |
-
kv_seq_len = key_states.shape[-2]
|
413 |
-
if past_key_value is not None:
|
414 |
-
if self.layer_idx is None:
|
415 |
-
raise ValueError(
|
416 |
-
f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
|
417 |
-
"for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
|
418 |
-
"with a layer index."
|
419 |
-
)
|
420 |
-
kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
|
421 |
-
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
|
422 |
-
|
423 |
-
# Partial rotary embedding
|
424 |
-
query_rot, query_pass = (
|
425 |
-
query_states[..., : self.rotary_emb.dim],
|
426 |
-
query_states[..., self.rotary_emb.dim :],
|
427 |
-
)
|
428 |
-
key_rot, key_pass = (
|
429 |
-
key_states[..., : self.rotary_emb.dim],
|
430 |
-
key_states[..., self.rotary_emb.dim :],
|
431 |
-
)
|
432 |
-
# [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor]
|
433 |
-
query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin, position_ids)
|
434 |
-
|
435 |
-
# [batch_size, seq_length, num_heads, head_dim]
|
436 |
-
query_states = torch.cat((query_rot, query_pass), dim=-1)
|
437 |
-
key_states = torch.cat((key_rot, key_pass), dim=-1)
|
438 |
-
|
439 |
-
if past_key_value is not None:
|
440 |
-
# Specific to RoPE models with partial rotation
|
441 |
-
cache_kwargs = {"sin": sin, "cos": cos, "partial_rotation_size": self.rotary_emb.dim}
|
442 |
-
key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
|
443 |
-
|
444 |
-
# Repeat k/v heads if n_kv_heads < n_heads
|
445 |
-
key_states = repeat_kv(key_states, self.num_key_value_groups)
|
446 |
-
value_states = repeat_kv(value_states, self.num_key_value_groups)
|
447 |
-
|
448 |
-
# SDPA with memory-efficient backend is currently (torch==2.1.2) bugged with non-contiguous inputs with custom attn_mask,
|
449 |
-
# Reference: https://github.com/pytorch/pytorch/issues/112577.
|
450 |
-
if query_states.device.type == "cuda" and attention_mask is not None:
|
451 |
-
query_states = query_states.contiguous()
|
452 |
-
key_states = key_states.contiguous()
|
453 |
-
value_states = value_states.contiguous()
|
454 |
-
|
455 |
-
attn_output = torch.nn.functional.scaled_dot_product_attention(
|
456 |
-
query_states,
|
457 |
-
key_states,
|
458 |
-
value_states,
|
459 |
-
attn_mask=attention_mask,
|
460 |
-
dropout_p=self.attention_dropout.p if self.training else 0.0,
|
461 |
-
# The q_len > 1 is necessary to match with AttentionMaskConverter.to_causal_4d that does not create a causal mask in case q_len == 1.
|
462 |
-
is_causal=self.is_causal and attention_mask is None and q_len > 1,
|
463 |
-
)
|
464 |
-
|
465 |
-
attn_output = attn_output.transpose(1, 2).contiguous()
|
466 |
-
attn_output = attn_output.view(bsz, q_len, self.hidden_size)
|
467 |
-
|
468 |
-
attn_output = self.o_proj(attn_output)
|
469 |
-
|
470 |
-
return attn_output, None, past_key_value
|
471 |
-
|
472 |
-
|
473 |
-
class StableLmFlashAttention2(StableLmAttention):
|
474 |
"""
|
475 |
-
|
476 |
-
untouched. The only required change would be on the forward pass where it needs to correctly call the public API of
|
477 |
-
flash attention and deal with padding tokens in case the input contains any of them.
|
478 |
"""
|
479 |
|
480 |
-
# Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2.__init__
|
481 |
def __init__(self, *args, **kwargs):
|
482 |
super().__init__(*args, **kwargs)
|
483 |
|
@@ -496,7 +319,14 @@ class StableLmFlashAttention2(StableLmAttention):
|
|
496 |
use_cache: bool = False,
|
497 |
**kwargs,
|
498 |
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
499 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
500 |
|
501 |
output_attentions = False
|
502 |
|
@@ -513,35 +343,27 @@ class StableLmFlashAttention2(StableLmAttention):
|
|
513 |
key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
514 |
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
515 |
|
|
|
|
|
|
|
|
|
|
|
516 |
kv_seq_len = key_states.shape[-2]
|
517 |
if past_key_value is not None:
|
518 |
-
|
519 |
-
raise ValueError(
|
520 |
-
f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
|
521 |
-
"for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
|
522 |
-
"with a layer index."
|
523 |
-
)
|
524 |
-
kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
|
525 |
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
|
|
|
526 |
|
527 |
-
#
|
528 |
-
|
529 |
-
|
530 |
-
query_states[..., self.rotary_emb.dim :],
|
531 |
-
)
|
532 |
-
key_rot, key_pass = (
|
533 |
-
key_states[..., : self.rotary_emb.dim],
|
534 |
-
key_states[..., self.rotary_emb.dim :],
|
535 |
-
)
|
536 |
-
query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin, position_ids)
|
537 |
-
|
538 |
-
# [batch_size, seq_length, num_heads, head_dim]
|
539 |
-
query_states = torch.cat((query_rot, query_pass), dim=-1)
|
540 |
-
key_states = torch.cat((key_rot, key_pass), dim=-1)
|
541 |
|
542 |
if past_key_value is not None:
|
543 |
-
|
544 |
-
key_states
|
|
|
|
|
|
|
545 |
|
546 |
# 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
|
547 |
# to be able to avoid many of these transpose/reshape/view.
|
@@ -552,14 +374,8 @@ class StableLmFlashAttention2(StableLmAttention):
|
|
552 |
dropout_rate = self.attention_dropout if self.training else 0.0
|
553 |
|
554 |
attn_output = self._flash_attention_forward(
|
555 |
-
query_states,
|
556 |
-
key_states,
|
557 |
-
value_states,
|
558 |
-
attention_mask,
|
559 |
-
q_len,
|
560 |
-
dropout=dropout_rate,
|
561 |
)
|
562 |
-
|
563 |
attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous()
|
564 |
attn_output = self.o_proj(attn_output)
|
565 |
|
@@ -568,7 +384,6 @@ class StableLmFlashAttention2(StableLmAttention):
|
|
568 |
|
569 |
return attn_output, attn_weights, past_key_value
|
570 |
|
571 |
-
# Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2._flash_attention_forward
|
572 |
def _flash_attention_forward(
|
573 |
self, query_states, key_states, value_states, attention_mask, query_length, dropout=0.0, softmax_scale=None
|
574 |
):
|
@@ -594,7 +409,7 @@ class StableLmFlashAttention2(StableLmAttention):
|
|
594 |
if not self._flash_attn_uses_top_left_mask:
|
595 |
causal = self.is_causal
|
596 |
else:
|
597 |
-
# TODO: Remove the `query_length != 1` check once Flash Attention for RoCm is bumped to 2.1. For details, please see the comment in
|
598 |
causal = self.is_causal and query_length != 1
|
599 |
|
600 |
# Contains at least one padding token in the sequence
|
@@ -628,7 +443,6 @@ class StableLmFlashAttention2(StableLmAttention):
|
|
628 |
|
629 |
return attn_output
|
630 |
|
631 |
-
# Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2._upad_input
|
632 |
def _upad_input(self, query_layer, key_layer, value_layer, attention_mask, query_length):
|
633 |
indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask)
|
634 |
batch_size, kv_seq_len, num_key_value_heads, head_dim = key_layer.shape
|
@@ -669,51 +483,28 @@ class StableLmFlashAttention2(StableLmAttention):
|
|
669 |
|
670 |
|
671 |
ATTENTION_CLASSES = {
|
672 |
-
"eager":
|
673 |
-
"
|
674 |
-
"flash_attention_2": StableLmFlashAttention2,
|
675 |
}
|
676 |
|
677 |
|
678 |
-
class
|
679 |
-
def __init__(self, config:
|
680 |
super().__init__()
|
681 |
-
self.
|
682 |
-
self.
|
683 |
-
self.
|
684 |
-
self.
|
685 |
-
self.post_attention_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
686 |
-
self.dropout = nn.Dropout(config.hidden_dropout)
|
687 |
|
688 |
def forward(
|
689 |
self,
|
690 |
-
hidden_states: torch.
|
691 |
-
attention_mask: Optional[torch.
|
692 |
position_ids: Optional[torch.LongTensor] = None,
|
693 |
past_key_value: Optional[Tuple[torch.Tensor]] = None,
|
694 |
output_attentions: Optional[bool] = False,
|
695 |
use_cache: Optional[bool] = False,
|
696 |
-
) -> Tuple[torch.
|
697 |
-
"""
|
698 |
-
Args:
|
699 |
-
hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
|
700 |
-
attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
|
701 |
-
`(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
|
702 |
-
position_ids (`torch.LongTensor` of shape `({0})`, *optional*):
|
703 |
-
Indices of positions of each input sequence tokens in the position embeddings. Selected in the range
|
704 |
-
`[0, config.n_positions - 1]`.
|
705 |
-
|
706 |
-
[What are position IDs?](../glossary#position-ids)
|
707 |
-
past_key_value (`Tuple(torch.FloatTensor)`, *optional*):
|
708 |
-
cached past key and value projection states
|
709 |
-
output_attentions (`bool`, *optional*):
|
710 |
-
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
|
711 |
-
returned tensors for more detail.
|
712 |
-
use_cache (`bool`, *optional*):
|
713 |
-
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
|
714 |
-
(see `past_key_values`).
|
715 |
-
"""
|
716 |
-
|
717 |
residual = hidden_states
|
718 |
|
719 |
hidden_states = self.input_layernorm(hidden_states)
|
@@ -733,9 +524,7 @@ class StableLmDecoderLayer(nn.Module):
|
|
733 |
residual = hidden_states
|
734 |
hidden_states = self.post_attention_layernorm(hidden_states)
|
735 |
hidden_states = self.mlp(hidden_states)
|
736 |
-
|
737 |
-
hidden_states = self.dropout(hidden_states)
|
738 |
-
hidden_states = hidden_states + residual
|
739 |
|
740 |
outputs = (hidden_states,)
|
741 |
|
@@ -748,143 +537,45 @@ class StableLmDecoderLayer(nn.Module):
|
|
748 |
return outputs
|
749 |
|
750 |
|
751 |
-
|
752 |
-
|
753 |
-
|
754 |
-
|
755 |
-
|
756 |
-
This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
|
757 |
-
Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
|
758 |
-
and behavior.
|
759 |
-
|
760 |
-
Parameters:
|
761 |
-
config ([`StableLmConfig`]):
|
762 |
-
Model configuration class with all the parameters of the model. Initializing with a config file does not
|
763 |
-
load the weights associated with the model, only the configuration. Check out the
|
764 |
-
[`~PreTrainedModel.from_pretrained`] method to load the model weights.
|
765 |
-
"""
|
766 |
-
|
767 |
|
768 |
-
|
769 |
-
|
770 |
-
STABLELM_START_DOCSTRING,
|
771 |
-
)
|
772 |
-
class StableLmPreTrainedModel(PreTrainedModel):
|
773 |
-
config_class = StableLmConfig
|
774 |
-
base_model_prefix = "model"
|
775 |
supports_gradient_checkpointing = True
|
776 |
-
_no_split_modules = ["
|
777 |
_skip_keys_device_placement = "past_key_values"
|
778 |
_supports_flash_attn_2 = True
|
779 |
-
_supports_cache_class = True
|
780 |
-
_supports_sdpa = True
|
781 |
|
782 |
-
def _init_weights(self, module):
|
783 |
-
|
784 |
if isinstance(module, nn.Linear):
|
785 |
-
module.weight.data.normal_(mean=0.0, std=
|
786 |
if module.bias is not None:
|
787 |
module.bias.data.zero_()
|
788 |
elif isinstance(module, nn.Embedding):
|
789 |
-
module.weight.data.normal_(mean=0.0, std=
|
790 |
if module.padding_idx is not None:
|
791 |
module.weight.data[module.padding_idx].zero_()
|
|
|
|
|
|
|
792 |
|
|
|
|
|
|
|
793 |
|
794 |
-
STABLELM_INPUTS_DOCSTRING = r"""
|
795 |
-
Args:
|
796 |
-
input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
|
797 |
-
Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
|
798 |
-
it.
|
799 |
-
|
800 |
-
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
801 |
-
[`PreTrainedTokenizer.__call__`] for details.
|
802 |
-
|
803 |
-
[What are input IDs?](../glossary#input-ids)
|
804 |
-
attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
|
805 |
-
Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
|
806 |
-
|
807 |
-
- 1 for tokens that are **not masked**,
|
808 |
-
- 0 for tokens that are **masked**.
|
809 |
-
|
810 |
-
[What are attention masks?](../glossary#attention-mask)
|
811 |
-
|
812 |
-
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
813 |
-
[`PreTrainedTokenizer.__call__`] for details.
|
814 |
-
|
815 |
-
If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
|
816 |
-
`past_key_values`).
|
817 |
-
|
818 |
-
If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
|
819 |
-
and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
|
820 |
-
information on the default strategy.
|
821 |
-
|
822 |
-
- 1 indicates the head is **not masked**,
|
823 |
-
- 0 indicates the head is **masked**.
|
824 |
-
position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
825 |
-
Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
|
826 |
-
config.n_positions - 1]`.
|
827 |
-
|
828 |
-
[What are position IDs?](../glossary#position-ids)
|
829 |
-
past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):
|
830 |
-
Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
|
831 |
-
blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
|
832 |
-
returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.
|
833 |
-
|
834 |
-
Two formats are allowed:
|
835 |
-
- a [`~cache_utils.Cache`] instance;
|
836 |
-
- Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
|
837 |
-
shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy
|
838 |
-
cache format.
|
839 |
-
|
840 |
-
The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the
|
841 |
-
legacy cache format will be returned.
|
842 |
-
|
843 |
-
If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
|
844 |
-
have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
|
845 |
-
of shape `(batch_size, sequence_length)`.
|
846 |
-
inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
|
847 |
-
Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
|
848 |
-
is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
|
849 |
-
model's internal embedding lookup matrix.
|
850 |
-
use_cache (`bool`, *optional*):
|
851 |
-
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
|
852 |
-
`past_key_values`).
|
853 |
-
output_attentions (`bool`, *optional*):
|
854 |
-
Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
|
855 |
-
tensors for more detail.
|
856 |
-
output_hidden_states (`bool`, *optional*):
|
857 |
-
Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
|
858 |
-
more detail.
|
859 |
-
return_dict (`bool`, *optional*):
|
860 |
-
Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
|
861 |
-
"""
|
862 |
-
|
863 |
-
|
864 |
-
@add_start_docstrings(
|
865 |
-
"The bare StableLm Model outputting raw hidden-states without any specific head on top.",
|
866 |
-
STABLELM_START_DOCSTRING,
|
867 |
-
)
|
868 |
-
class StableLmModel(StableLmPreTrainedModel):
|
869 |
-
"""
|
870 |
-
Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`StableLmDecoderLayer`]
|
871 |
-
|
872 |
-
Args:
|
873 |
-
config: StableLmConfig
|
874 |
-
"""
|
875 |
|
876 |
-
|
|
|
877 |
super().__init__(config)
|
878 |
-
self.
|
879 |
-
self.
|
880 |
-
|
881 |
-
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
882 |
-
self.layers = nn.ModuleList(
|
883 |
-
[StableLmDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
|
884 |
-
)
|
885 |
-
self.norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
886 |
|
887 |
-
self.
|
888 |
self.gradient_checkpointing = False
|
889 |
# Initialize weights and apply final processing
|
890 |
self.post_init()
|
@@ -892,16 +583,43 @@ class StableLmModel(StableLmPreTrainedModel):
|
|
892 |
def get_input_embeddings(self):
|
893 |
return self.embed_tokens
|
894 |
|
895 |
-
def set_input_embeddings(self, value):
|
896 |
self.embed_tokens = value
|
897 |
|
898 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
899 |
def forward(
|
900 |
self,
|
901 |
-
input_ids: torch.LongTensor = None,
|
902 |
-
attention_mask: Optional[torch.
|
903 |
position_ids: Optional[torch.LongTensor] = None,
|
904 |
-
past_key_values: Optional[
|
905 |
inputs_embeds: Optional[torch.FloatTensor] = None,
|
906 |
use_cache: Optional[bool] = None,
|
907 |
output_attentions: Optional[bool] = None,
|
@@ -909,90 +627,103 @@ class StableLmModel(StableLmPreTrainedModel):
|
|
909 |
return_dict: Optional[bool] = None,
|
910 |
) -> Union[Tuple, BaseModelOutputWithPast]:
|
911 |
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
912 |
-
output_hidden_states =
|
913 |
-
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
914 |
-
)
|
915 |
use_cache = use_cache if use_cache is not None else self.config.use_cache
|
916 |
|
917 |
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
918 |
|
919 |
-
#
|
920 |
if input_ids is not None and inputs_embeds is not None:
|
921 |
-
raise ValueError(
|
|
|
|
|
922 |
elif input_ids is not None:
|
923 |
batch_size, seq_length = input_ids.shape
|
924 |
elif inputs_embeds is not None:
|
925 |
batch_size, seq_length, _ = inputs_embeds.shape
|
926 |
else:
|
927 |
-
raise ValueError(
|
|
|
|
|
928 |
|
929 |
seq_length_with_past = seq_length
|
930 |
past_key_values_length = 0
|
931 |
|
932 |
-
if self.gradient_checkpointing and self.training:
|
933 |
-
if use_cache:
|
934 |
-
logger.warning_once(
|
935 |
-
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
|
936 |
-
)
|
937 |
-
use_cache = False
|
938 |
-
|
939 |
-
if use_cache:
|
940 |
-
use_legacy_cache = not isinstance(past_key_values, Cache)
|
941 |
-
if use_legacy_cache:
|
942 |
-
past_key_values = DynamicCache.from_legacy_cache(past_key_values)
|
943 |
-
past_key_values_length = past_key_values.get_usable_length(seq_length)
|
944 |
-
seq_length_with_past = seq_length_with_past + past_key_values_length
|
945 |
-
|
946 |
if position_ids is None:
|
947 |
device = input_ids.device if input_ids is not None else inputs_embeds.device
|
948 |
position_ids = torch.arange(
|
949 |
-
past_key_values_length,
|
|
|
|
|
|
|
950 |
)
|
951 |
-
position_ids = position_ids.unsqueeze(0)
|
|
|
|
|
952 |
|
953 |
if inputs_embeds is None:
|
954 |
inputs_embeds = self.embed_tokens(input_ids)
|
955 |
-
#
|
956 |
-
if self.
|
957 |
# 2d mask is passed through the layers
|
958 |
attention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None
|
959 |
-
# for output_attentions case used fallback to eager attention realization
|
960 |
-
elif self._attn_implementation == "sdpa" and not output_attentions:
|
961 |
-
attention_mask = _prepare_4d_causal_attention_mask_for_sdpa(
|
962 |
-
attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
|
963 |
-
)
|
964 |
else:
|
965 |
-
|
966 |
-
|
967 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
968 |
)
|
969 |
|
970 |
hidden_states = inputs_embeds
|
971 |
|
972 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
973 |
all_hidden_states = () if output_hidden_states else None
|
974 |
all_self_attns = () if output_attentions else None
|
975 |
-
next_decoder_cache = None
|
976 |
|
977 |
-
for decoder_layer in self.layers:
|
978 |
if output_hidden_states:
|
979 |
all_hidden_states += (hidden_states,)
|
980 |
|
|
|
|
|
|
|
|
|
981 |
if self.gradient_checkpointing and self.training:
|
982 |
-
|
983 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
984 |
hidden_states,
|
985 |
attention_mask,
|
986 |
position_ids,
|
987 |
-
past_key_values,
|
988 |
-
output_attentions,
|
989 |
)
|
990 |
else:
|
991 |
layer_outputs = decoder_layer(
|
992 |
hidden_states,
|
993 |
attention_mask=attention_mask,
|
994 |
position_ids=position_ids,
|
995 |
-
past_key_value=
|
996 |
output_attentions=output_attentions,
|
997 |
use_cache=use_cache,
|
998 |
)
|
@@ -1000,23 +731,24 @@ class StableLmModel(StableLmPreTrainedModel):
|
|
1000 |
hidden_states = layer_outputs[0]
|
1001 |
|
1002 |
if use_cache:
|
1003 |
-
next_decoder_cache
|
1004 |
|
1005 |
if output_attentions:
|
1006 |
all_self_attns += (layer_outputs[1],)
|
1007 |
|
1008 |
hidden_states = self.norm(hidden_states)
|
1009 |
|
1010 |
-
#
|
1011 |
if output_hidden_states:
|
1012 |
all_hidden_states += (hidden_states,)
|
1013 |
|
1014 |
-
next_cache = None
|
1015 |
-
if use_cache:
|
1016 |
-
next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache
|
1017 |
-
|
1018 |
if not return_dict:
|
1019 |
-
return tuple(
|
|
|
|
|
|
|
|
|
1020 |
return BaseModelOutputWithPast(
|
1021 |
last_hidden_state=hidden_states,
|
1022 |
past_key_values=next_cache,
|
@@ -1025,53 +757,42 @@ class StableLmModel(StableLmPreTrainedModel):
|
|
1025 |
)
|
1026 |
|
1027 |
|
1028 |
-
|
1029 |
-
class StableLmForCausalLM(StableLmPreTrainedModel):
|
1030 |
_tied_weights_keys = ["lm_head.weight"]
|
1031 |
|
1032 |
-
|
1033 |
-
def __init__(self, config):
|
1034 |
super().__init__(config)
|
1035 |
-
|
1036 |
-
self.
|
1037 |
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
1038 |
|
1039 |
# Initialize weights and apply final processing
|
1040 |
self.post_init()
|
1041 |
|
1042 |
-
# Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.get_input_embeddings
|
1043 |
def get_input_embeddings(self):
|
1044 |
return self.model.embed_tokens
|
1045 |
|
1046 |
-
# Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.set_input_embeddings
|
1047 |
def set_input_embeddings(self, value):
|
1048 |
self.model.embed_tokens = value
|
1049 |
|
1050 |
-
# Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.get_output_embeddings
|
1051 |
def get_output_embeddings(self):
|
1052 |
return self.lm_head
|
1053 |
|
1054 |
-
|
1055 |
-
def set_output_embeddings(self, new_embeddings):
|
1056 |
self.lm_head = new_embeddings
|
1057 |
|
1058 |
-
# Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.set_decoder
|
1059 |
-
def set_decoder(self, decoder):
|
1060 |
-
self.model = decoder
|
1061 |
-
|
1062 |
-
# Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.get_decoder
|
1063 |
def get_decoder(self):
|
1064 |
return self.model
|
1065 |
|
1066 |
-
|
1067 |
-
|
1068 |
-
|
1069 |
def forward(
|
1070 |
self,
|
1071 |
-
input_ids: torch.LongTensor = None,
|
1072 |
-
attention_mask: Optional[torch.
|
1073 |
position_ids: Optional[torch.LongTensor] = None,
|
1074 |
-
past_key_values: Optional[
|
1075 |
inputs_embeds: Optional[torch.FloatTensor] = None,
|
1076 |
labels: Optional[torch.LongTensor] = None,
|
1077 |
use_cache: Optional[bool] = None,
|
@@ -1079,40 +800,23 @@ class StableLmForCausalLM(StableLmPreTrainedModel):
|
|
1079 |
output_hidden_states: Optional[bool] = None,
|
1080 |
return_dict: Optional[bool] = None,
|
1081 |
) -> Union[Tuple, CausalLMOutputWithPast]:
|
1082 |
-
|
1083 |
-
|
1084 |
-
|
1085 |
-
|
1086 |
-
|
1087 |
-
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
1088 |
-
|
1089 |
-
Returns:
|
1090 |
-
|
1091 |
-
Example:
|
1092 |
-
|
1093 |
-
```python
|
1094 |
-
>>> from transformers import AutoTokenizer, StableLmForCausalLM
|
1095 |
-
|
1096 |
-
>>> model = StableLmForCausalLM.from_pretrained("stabilityai/stablelm-3b-4e1t")
|
1097 |
-
>>> tokenizer = AutoTokenizer.from_pretrained("stabilityai/stablelm-3b-4e1t")
|
1098 |
-
|
1099 |
-
>>> prompt = "The weather is always wonderful in"
|
1100 |
-
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
1101 |
-
|
1102 |
-
>>> # Generate
|
1103 |
-
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
1104 |
-
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
1105 |
-
'The weather is always wonderful in the summer in the city of San Diego. The city is located on the coast of the Pacific Ocean and is surrounded by'
|
1106 |
-
```"""
|
1107 |
-
|
1108 |
-
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
1109 |
output_hidden_states = (
|
1110 |
-
output_hidden_states
|
|
|
|
|
|
|
|
|
|
|
1111 |
)
|
1112 |
-
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
1113 |
|
|
|
1114 |
outputs = self.model(
|
1115 |
-
input_ids
|
1116 |
attention_mask=attention_mask,
|
1117 |
position_ids=position_ids,
|
1118 |
past_key_values=past_key_values,
|
@@ -1124,7 +828,7 @@ class StableLmForCausalLM(StableLmPreTrainedModel):
|
|
1124 |
)
|
1125 |
|
1126 |
hidden_states = outputs[0]
|
1127 |
-
logits = self.lm_head(hidden_states)
|
1128 |
|
1129 |
loss = None
|
1130 |
if labels is not None:
|
@@ -1152,46 +856,35 @@ class StableLmForCausalLM(StableLmPreTrainedModel):
|
|
1152 |
)
|
1153 |
|
1154 |
def prepare_inputs_for_generation(
|
1155 |
-
self,
|
|
|
|
|
|
|
|
|
|
|
1156 |
):
|
|
|
1157 |
if past_key_values is not None:
|
1158 |
-
|
1159 |
-
|
1160 |
-
|
1161 |
-
|
|
|
1162 |
else:
|
1163 |
-
|
1164 |
-
|
1165 |
-
|
1166 |
-
|
1167 |
-
# 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where
|
1168 |
-
# some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as
|
1169 |
-
# input)
|
1170 |
-
if attention_mask is not None and attention_mask.shape[1] > input_ids.shape[1]:
|
1171 |
-
input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :]
|
1172 |
-
# 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard
|
1173 |
-
# input_ids based on the past_length.
|
1174 |
-
elif past_length < input_ids.shape[1]:
|
1175 |
-
input_ids = input_ids[:, past_length:]
|
1176 |
-
# 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.
|
1177 |
-
|
1178 |
-
# If we are about to go beyond the maximum cache length, we need to crop the input attention mask.
|
1179 |
-
if (
|
1180 |
-
max_cache_length is not None
|
1181 |
-
and attention_mask is not None
|
1182 |
-
and cache_length + input_ids.shape[1] > max_cache_length
|
1183 |
-
):
|
1184 |
-
attention_mask = attention_mask[:, -max_cache_length:]
|
1185 |
|
1186 |
position_ids = kwargs.get("position_ids", None)
|
1187 |
if attention_mask is not None and position_ids is None:
|
1188 |
-
#
|
1189 |
position_ids = attention_mask.long().cumsum(-1) - 1
|
1190 |
position_ids.masked_fill_(attention_mask == 0, 1)
|
1191 |
if past_key_values:
|
1192 |
-
position_ids = position_ids[:, -
|
1193 |
|
1194 |
-
#
|
1195 |
if inputs_embeds is not None and past_key_values is None:
|
1196 |
model_inputs = {"inputs_embeds": inputs_embeds}
|
1197 |
else:
|
@@ -1199,10 +892,10 @@ class StableLmForCausalLM(StableLmPreTrainedModel):
|
|
1199 |
|
1200 |
model_inputs.update(
|
1201 |
{
|
1202 |
-
"
|
1203 |
"past_key_values": past_key_values,
|
1204 |
"use_cache": kwargs.get("use_cache"),
|
1205 |
-
"
|
1206 |
}
|
1207 |
)
|
1208 |
return model_inputs
|
@@ -1212,130 +905,13 @@ class StableLmForCausalLM(StableLmPreTrainedModel):
|
|
1212 |
reordered_past = ()
|
1213 |
for layer_past in past_key_values:
|
1214 |
reordered_past += (
|
1215 |
-
tuple(
|
|
|
|
|
|
|
1216 |
)
|
1217 |
return reordered_past
|
1218 |
|
1219 |
|
1220 |
-
|
1221 |
-
|
1222 |
-
The StableLm transformer with a sequence classification head on top (linear layer).
|
1223 |
-
|
1224 |
-
[`StableLmForSequenceClassification`] uses the last token in order to do the classification, as other causal
|
1225 |
-
models (e.g. GPT-2) do.
|
1226 |
-
|
1227 |
-
Since it does classification on the last token, it requires to know the position of the last token. If a
|
1228 |
-
`pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
|
1229 |
-
no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
|
1230 |
-
padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
|
1231 |
-
each row of the batch).
|
1232 |
-
""",
|
1233 |
-
STABLELM_START_DOCSTRING,
|
1234 |
-
)
|
1235 |
-
# Copied from transformers.models.llama.modeling_llama.LlamaForSequenceClassification with LLAMA->STABLELM,Llama->StableLm
|
1236 |
-
class StableLmForSequenceClassification(StableLmPreTrainedModel):
|
1237 |
-
def __init__(self, config):
|
1238 |
-
super().__init__(config)
|
1239 |
-
self.num_labels = config.num_labels
|
1240 |
-
self.model = StableLmModel(config)
|
1241 |
-
self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
|
1242 |
-
|
1243 |
-
# Initialize weights and apply final processing
|
1244 |
-
self.post_init()
|
1245 |
-
|
1246 |
-
def get_input_embeddings(self):
|
1247 |
-
return self.model.embed_tokens
|
1248 |
-
|
1249 |
-
def set_input_embeddings(self, value):
|
1250 |
-
self.model.embed_tokens = value
|
1251 |
-
|
1252 |
-
@add_start_docstrings_to_model_forward(STABLELM_INPUTS_DOCSTRING)
|
1253 |
-
def forward(
|
1254 |
-
self,
|
1255 |
-
input_ids: torch.LongTensor = None,
|
1256 |
-
attention_mask: Optional[torch.Tensor] = None,
|
1257 |
-
position_ids: Optional[torch.LongTensor] = None,
|
1258 |
-
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
1259 |
-
inputs_embeds: Optional[torch.FloatTensor] = None,
|
1260 |
-
labels: Optional[torch.LongTensor] = None,
|
1261 |
-
use_cache: Optional[bool] = None,
|
1262 |
-
output_attentions: Optional[bool] = None,
|
1263 |
-
output_hidden_states: Optional[bool] = None,
|
1264 |
-
return_dict: Optional[bool] = None,
|
1265 |
-
) -> Union[Tuple, SequenceClassifierOutputWithPast]:
|
1266 |
-
r"""
|
1267 |
-
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
|
1268 |
-
Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
|
1269 |
-
config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
|
1270 |
-
`config.num_labels > 1` a classification loss is computed (Cross-Entropy).
|
1271 |
-
"""
|
1272 |
-
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
1273 |
-
|
1274 |
-
transformer_outputs = self.model(
|
1275 |
-
input_ids,
|
1276 |
-
attention_mask=attention_mask,
|
1277 |
-
position_ids=position_ids,
|
1278 |
-
past_key_values=past_key_values,
|
1279 |
-
inputs_embeds=inputs_embeds,
|
1280 |
-
use_cache=use_cache,
|
1281 |
-
output_attentions=output_attentions,
|
1282 |
-
output_hidden_states=output_hidden_states,
|
1283 |
-
return_dict=return_dict,
|
1284 |
-
)
|
1285 |
-
hidden_states = transformer_outputs[0]
|
1286 |
-
logits = self.score(hidden_states)
|
1287 |
-
|
1288 |
-
if input_ids is not None:
|
1289 |
-
batch_size = input_ids.shape[0]
|
1290 |
-
else:
|
1291 |
-
batch_size = inputs_embeds.shape[0]
|
1292 |
-
|
1293 |
-
if self.config.pad_token_id is None and batch_size != 1:
|
1294 |
-
raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
|
1295 |
-
if self.config.pad_token_id is None:
|
1296 |
-
sequence_lengths = -1
|
1297 |
-
else:
|
1298 |
-
if input_ids is not None:
|
1299 |
-
# if no pad token found, use modulo instead of reverse indexing for ONNX compatibility
|
1300 |
-
sequence_lengths = torch.eq(input_ids, self.config.pad_token_id).int().argmax(-1) - 1
|
1301 |
-
sequence_lengths = sequence_lengths % input_ids.shape[-1]
|
1302 |
-
sequence_lengths = sequence_lengths.to(logits.device)
|
1303 |
-
else:
|
1304 |
-
sequence_lengths = -1
|
1305 |
-
|
1306 |
-
pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths]
|
1307 |
-
|
1308 |
-
loss = None
|
1309 |
-
if labels is not None:
|
1310 |
-
labels = labels.to(logits.device)
|
1311 |
-
if self.config.problem_type is None:
|
1312 |
-
if self.num_labels == 1:
|
1313 |
-
self.config.problem_type = "regression"
|
1314 |
-
elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
|
1315 |
-
self.config.problem_type = "single_label_classification"
|
1316 |
-
else:
|
1317 |
-
self.config.problem_type = "multi_label_classification"
|
1318 |
-
|
1319 |
-
if self.config.problem_type == "regression":
|
1320 |
-
loss_fct = MSELoss()
|
1321 |
-
if self.num_labels == 1:
|
1322 |
-
loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
|
1323 |
-
else:
|
1324 |
-
loss = loss_fct(pooled_logits, labels)
|
1325 |
-
elif self.config.problem_type == "single_label_classification":
|
1326 |
-
loss_fct = CrossEntropyLoss()
|
1327 |
-
loss = loss_fct(pooled_logits.view(-1, self.num_labels), labels.view(-1))
|
1328 |
-
elif self.config.problem_type == "multi_label_classification":
|
1329 |
-
loss_fct = BCEWithLogitsLoss()
|
1330 |
-
loss = loss_fct(pooled_logits, labels)
|
1331 |
-
if not return_dict:
|
1332 |
-
output = (pooled_logits,) + transformer_outputs[1:]
|
1333 |
-
return ((loss,) + output) if loss is not None else output
|
1334 |
-
|
1335 |
-
return SequenceClassifierOutputWithPast(
|
1336 |
-
loss=loss,
|
1337 |
-
logits=pooled_logits,
|
1338 |
-
past_key_values=transformer_outputs.past_key_values,
|
1339 |
-
hidden_states=transformer_outputs.hidden_states,
|
1340 |
-
attentions=transformer_outputs.attentions,
|
1341 |
-
)
|
|
|
1 |
# coding=utf-8
|
2 |
+
# Copyright 2023 Stability AI, EleutherAI, and The HuggingFace Inc. team. All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
3 |
#
|
4 |
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
# you may not use this file except in compliance with the License.
|
|
|
12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
# See the License for the specific language governing permissions and
|
14 |
# limitations under the License.
|
15 |
+
#
|
16 |
+
# This code is based off the following work:
|
17 |
+
# https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/modeling_llama.py
|
18 |
+
# https://github.com/huggingface/transformers/blob/main/src/transformers/models/gpt_neox/modeling_gpt_neox.py
|
19 |
+
""" PyTorch StableLM Epoch model. """
|
20 |
+
from typing import Optional, Tuple, Union
|
21 |
import math
|
22 |
+
import warnings
|
23 |
|
24 |
import torch
|
25 |
import torch.nn.functional as F
|
26 |
import torch.utils.checkpoint
|
27 |
from torch import nn
|
28 |
+
from torch.nn import CrossEntropyLoss
|
29 |
|
30 |
+
from transformers.cache_utils import Cache
|
31 |
+
from transformers.modeling_outputs import (
|
32 |
+
BaseModelOutputWithPast,
|
33 |
+
CausalLMOutputWithPast,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
)
|
35 |
+
from transformers.modeling_utils import PreTrainedModel
|
36 |
+
from transformers.utils import logging, is_flash_attn_greater_or_equal_2_10
|
37 |
|
38 |
+
from .configuration_stablelm_epoch import StableLMEpochConfig
|
39 |
|
40 |
+
try:
|
41 |
from flash_attn import flash_attn_func, flash_attn_varlen_func
|
42 |
+
from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input
|
43 |
+
except:
|
44 |
+
flash_attn_func, flash_attn_varlen_func = None, None
|
45 |
+
index_first_axis, pad_input, unpad_input = None, None, None
|
46 |
|
47 |
|
48 |
logger = logging.get_logger(__name__)
|
49 |
|
|
|
|
|
50 |
|
51 |
# Copied from transformers.models.llama.modeling_llama._get_unpad_data
|
52 |
def _get_unpad_data(attention_mask):
|
53 |
seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
|
54 |
indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
|
55 |
max_seqlen_in_batch = seqlens_in_batch.max().item()
|
56 |
+
cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.torch.int32), (1, 0))
|
57 |
return (
|
58 |
indices,
|
59 |
cu_seqlens,
|
|
|
61 |
)
|
62 |
|
63 |
|
64 |
+
# Copied from transformers.models.bart.modeling_bart._make_causal_mask
|
65 |
+
def _make_causal_mask(
|
66 |
+
input_ids_shape: torch.Size,
|
67 |
+
dtype: torch.dtype,
|
68 |
+
device: torch.device,
|
69 |
+
past_key_values_length: int = 0,
|
70 |
+
):
|
71 |
+
"""Make causal mask used for bi-directional self-attention."""
|
72 |
+
batch_size, tgt_len = input_ids_shape
|
73 |
+
mask = torch.full((tgt_len, tgt_len), torch.finfo(torch.float16).min, device=device)
|
74 |
+
mask_cond = torch.arange(mask.size(-1), device=device)
|
75 |
+
mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0)
|
76 |
+
mask = mask.to(dtype)
|
77 |
+
if past_key_values_length > 0:
|
78 |
+
mask = torch.cat([torch.zeros(tgt_len, past_key_values_length, dtype=dtype, device=device), mask], dim=-1)
|
79 |
+
return mask[None, None, :, :].expand(batch_size, 1, tgt_len, tgt_len + past_key_values_length)
|
80 |
+
|
81 |
+
|
82 |
+
# Copied from transformers.models.bart.modeling_bart._expand_mask
|
83 |
+
def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None):
|
84 |
+
"""Expands attention_mask from `[batch_size, seq_len]` to `[batch_size, 1, tgt_seq_len, src_seq_len]`."""
|
85 |
+
batch_size, src_len = mask.size()
|
86 |
+
tgt_len = tgt_len if tgt_len is not None else src_len
|
87 |
+
|
88 |
+
expanded_mask = mask[:, None, None, :].expand(batch_size, 1, tgt_len, src_len).to(dtype)
|
89 |
+
inverted_mask = 1.0 - expanded_mask
|
90 |
+
|
91 |
+
return inverted_mask.masked_fill(
|
92 |
+
inverted_mask.to(torch.bool), torch.finfo(dtype).min
|
93 |
+
)
|
94 |
+
|
95 |
+
|
96 |
+
class RotaryEmbedding(nn.Module):
|
97 |
+
def __init__(
|
98 |
+
self,
|
99 |
+
dim: int,
|
100 |
+
max_position_embeddings: int,
|
101 |
+
base: int = 10_000,
|
102 |
+
device: Optional[torch.device] = None,
|
103 |
+
):
|
104 |
super().__init__()
|
105 |
|
106 |
self.dim = dim
|
107 |
self.max_position_embeddings = max_position_embeddings
|
108 |
self.base = base
|
109 |
+
inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2, device=device, dtype=torch.float32) / self.dim))
|
110 |
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
111 |
|
112 |
# Build here to make `torch.jit.trace` work.
|
113 |
self._set_cos_sin_cache(
|
114 |
+
seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype(),
|
115 |
)
|
116 |
|
117 |
+
def _set_cos_sin_cache(self, seq_len: int, device: torch.device, dtype: torch.dtype):
|
118 |
self.max_seq_len_cached = seq_len
|
119 |
+
t = torch.arange(self.max_seq_len_cached, device=device, dtype=torch.float32)
|
120 |
|
121 |
+
# Don't do einsum, it converts fp32 to fp16 under AMP
|
122 |
+
# freqs = torch.einsum("i,j->ij", t, self.inv_freq)
|
123 |
freqs = torch.outer(t, self.inv_freq)
|
124 |
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
125 |
emb = torch.cat((freqs, freqs), dim=-1)
|
126 |
+
self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(dtype), persistent=False)
|
127 |
+
self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(dtype), persistent=False)
|
128 |
|
129 |
+
def forward(self, x: torch.Tensor, seq_len: Optional[int] = None):
|
130 |
+
# x: [batch_size, num_heads, seq_len, head_size]
|
131 |
if seq_len > self.max_seq_len_cached:
|
132 |
+
self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=torch.get_default_dtype())
|
|
|
133 |
return (
|
134 |
+
self.cos_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
|
135 |
+
self.sin_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
|
136 |
)
|
137 |
|
138 |
|
139 |
+
def rotate_half(x: torch.Tensor):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
"""Rotates half the hidden dims of the input."""
|
141 |
+
x1, x2 = torch.chunk(x, 2, dim=-1)
|
|
|
142 |
return torch.cat((-x2, x1), dim=-1)
|
143 |
|
144 |
|
145 |
+
def apply_rotary_pos_emb(q, k, cos, sin, position_ids):
|
146 |
+
# The first two dimensions of cos and sin are always 1, so we can `squeeze` them.
|
147 |
+
cos = cos.squeeze(1).squeeze(0) # [seq_len, dim]
|
148 |
+
sin = sin.squeeze(1).squeeze(0) # [seq_len, dim]
|
149 |
+
cos = cos[position_ids].unsqueeze(1) # [batch_size, 1, seq_len, dim]
|
150 |
+
sin = sin[position_ids].unsqueeze(1) # [batch_size, 1, seq_len, dim]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
q_embed = (q * cos) + (rotate_half(q) * sin)
|
152 |
k_embed = (k * cos) + (rotate_half(k) * sin)
|
153 |
return q_embed, k_embed
|
154 |
|
155 |
|
156 |
+
class MLP(nn.Module):
|
157 |
+
def __init__(self, config: StableLMEpochConfig):
|
|
|
158 |
super().__init__()
|
159 |
self.config = config
|
160 |
self.hidden_size = config.hidden_size
|
161 |
self.intermediate_size = config.intermediate_size
|
162 |
+
self.gate_proj = nn.Linear(config.hidden_size, config.intermediate_size, bias=False)
|
163 |
+
self.up_proj = nn.Linear(config.hidden_size, config.intermediate_size, bias=False)
|
164 |
+
self.down_proj = nn.Linear(config.intermediate_size, config.hidden_size, bias=False)
|
165 |
+
self.act_fn = nn.SiLU()
|
166 |
|
167 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
168 |
return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
|
169 |
|
170 |
|
|
|
171 |
def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
|
172 |
"""
|
173 |
This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
|
|
|
180 |
return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
|
181 |
|
182 |
|
183 |
+
class Attention(nn.Module):
|
184 |
+
def __init__(self, config: StableLMEpochConfig):
|
|
|
|
|
185 |
super().__init__()
|
186 |
self.config = config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
self.hidden_size = config.hidden_size
|
188 |
self.num_heads = config.num_attention_heads
|
189 |
self.head_dim = self.hidden_size // self.num_heads
|
190 |
self.num_key_value_heads = config.num_key_value_heads
|
191 |
self.num_key_value_groups = self.num_heads // self.num_key_value_heads
|
192 |
self.max_position_embeddings = config.max_position_embeddings
|
|
|
|
|
193 |
self.is_causal = True
|
194 |
|
195 |
if (self.head_dim * self.num_heads) != self.hidden_size:
|
|
|
197 |
f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
|
198 |
f" and `num_heads`: {self.num_heads})."
|
199 |
)
|
200 |
+
|
201 |
self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=config.use_qkv_bias)
|
202 |
self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.use_qkv_bias)
|
203 |
self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.use_qkv_bias)
|
204 |
self.o_proj = nn.Linear(self.hidden_size, self.hidden_size, bias=False)
|
205 |
|
|
|
206 |
self._init_rope()
|
207 |
|
|
|
208 |
def _init_rope(self):
|
209 |
+
self.rotary_ndims = int(self.head_dim * self.config.rope_pct)
|
210 |
+
self.rotary_emb = RotaryEmbedding(
|
211 |
+
self.rotary_ndims,
|
212 |
+
max_position_embeddings=self.config.max_position_embeddings,
|
213 |
+
base=self.config.rope_theta,
|
214 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
|
216 |
def forward(
|
217 |
self,
|
218 |
+
hidden_states: torch.FloatTensor,
|
219 |
+
attention_mask: torch.FloatTensor,
|
220 |
+
position_ids: torch.LongTensor,
|
221 |
+
past_key_value: Optional[Tuple[torch.Tensor]] = None,
|
222 |
+
output_attentions: Optional[bool] = False,
|
223 |
+
use_cache: Optional[bool] = False,
|
224 |
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
225 |
bsz, q_len, _ = hidden_states.size()
|
226 |
|
|
|
232 |
key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
233 |
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
234 |
|
235 |
+
query_rot = query_states[..., : self.rotary_ndims]
|
236 |
+
query_pass = query_states[..., self.rotary_ndims :]
|
237 |
+
key_rot = key_states[..., : self.rotary_ndims]
|
238 |
+
key_pass = key_states[..., self.rotary_ndims :]
|
239 |
+
|
240 |
kv_seq_len = key_states.shape[-2]
|
241 |
if past_key_value is not None:
|
242 |
+
kv_seq_len += past_key_value[0].shape[-2]
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
|
244 |
+
query_states, key_states = apply_rotary_pos_emb(query_rot, key_rot, cos, sin, position_ids)
|
245 |
|
246 |
+
# [batch_size, num_heads, seq_len, head_dim]
|
247 |
+
query_states = torch.cat((query_states, query_pass), dim=-1)
|
248 |
+
key_states = torch.cat((key_states, key_pass), dim=-1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
249 |
|
250 |
if past_key_value is not None:
|
251 |
+
# Reuse k, v, self_attention
|
252 |
+
key_states = torch.cat((past_key_value[0], key_states), dim=2)
|
253 |
+
value_states = torch.cat((past_key_value[1], value_states), dim=2)
|
254 |
+
|
255 |
+
past_key_value = (key_states, value_states) if use_cache else None
|
256 |
|
257 |
# Repeat k/v heads if n_kv_heads < n_heads
|
258 |
key_states = repeat_kv(key_states, self.num_key_value_groups)
|
|
|
273 |
)
|
274 |
attn_weights = attn_weights + attention_mask
|
275 |
|
276 |
+
# Upcast attention to fp32
|
277 |
+
attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
|
|
|
|
|
278 |
attn_output = torch.matmul(attn_weights, value_states)
|
279 |
|
280 |
if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
|
|
|
283 |
f" {attn_output.size()}"
|
284 |
)
|
285 |
|
286 |
+
# Merge heads
|
287 |
attn_output = attn_output.transpose(1, 2).contiguous()
|
288 |
attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
|
289 |
|
290 |
+
# Final linear projection
|
291 |
attn_output = self.o_proj(attn_output)
|
292 |
|
293 |
if not output_attentions:
|
|
|
296 |
return attn_output, attn_weights, past_key_value
|
297 |
|
298 |
|
299 |
+
class FlashAttention2(Attention):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
300 |
"""
|
301 |
+
Reference: https://github.com/huggingface/transformers/blob/5d36025ca13d05151b7a0c761e90d429c4644a30/src/transformers/models/llama/modeling_llama.py#L456
|
|
|
|
|
302 |
"""
|
303 |
|
|
|
304 |
def __init__(self, *args, **kwargs):
|
305 |
super().__init__(*args, **kwargs)
|
306 |
|
|
|
319 |
use_cache: bool = False,
|
320 |
**kwargs,
|
321 |
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
322 |
+
# FlashAttention2 attention does not support output_attentions
|
323 |
+
if "padding_mask" in kwargs:
|
324 |
+
warnings.warn(
|
325 |
+
"Passing `padding_mask` is deprecated and will be removed in v4.37. Please make sure use `attention_mask` instead.`"
|
326 |
+
)
|
327 |
+
|
328 |
+
# overwrite attention_mask with padding_mask
|
329 |
+
attention_mask = kwargs.pop("padding_mask")
|
330 |
|
331 |
output_attentions = False
|
332 |
|
|
|
343 |
key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
344 |
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
345 |
|
346 |
+
query_rot = query_states[..., : self.rotary_ndims]
|
347 |
+
query_pass = query_states[..., self.rotary_ndims :]
|
348 |
+
key_rot = key_states[..., : self.rotary_ndims]
|
349 |
+
key_pass = key_states[..., self.rotary_ndims :]
|
350 |
+
|
351 |
kv_seq_len = key_states.shape[-2]
|
352 |
if past_key_value is not None:
|
353 |
+
kv_seq_len += past_key_value[0].shape[-2]
|
|
|
|
|
|
|
|
|
|
|
|
|
354 |
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
|
355 |
+
query_states, key_states = apply_rotary_pos_emb(query_rot, key_rot, cos, sin, position_ids)
|
356 |
|
357 |
+
# [batch_size, num_heads, seq_len, head_dim]
|
358 |
+
query_states = torch.cat((query_states, query_pass), dim=-1)
|
359 |
+
key_states = torch.cat((key_states, key_pass), dim=-1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
360 |
|
361 |
if past_key_value is not None:
|
362 |
+
# Reuse k, v, self_attention
|
363 |
+
key_states = torch.cat((past_key_value[0], key_states), dim=2)
|
364 |
+
value_states = torch.cat((past_key_value[1], value_states), dim=2)
|
365 |
+
|
366 |
+
past_key_value = (key_states, value_states) if use_cache else None
|
367 |
|
368 |
# 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
|
369 |
# to be able to avoid many of these transpose/reshape/view.
|
|
|
374 |
dropout_rate = self.attention_dropout if self.training else 0.0
|
375 |
|
376 |
attn_output = self._flash_attention_forward(
|
377 |
+
query_states, key_states, value_states, attention_mask, q_len, dropout=dropout_rate
|
|
|
|
|
|
|
|
|
|
|
378 |
)
|
|
|
379 |
attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous()
|
380 |
attn_output = self.o_proj(attn_output)
|
381 |
|
|
|
384 |
|
385 |
return attn_output, attn_weights, past_key_value
|
386 |
|
|
|
387 |
def _flash_attention_forward(
|
388 |
self, query_states, key_states, value_states, attention_mask, query_length, dropout=0.0, softmax_scale=None
|
389 |
):
|
|
|
409 |
if not self._flash_attn_uses_top_left_mask:
|
410 |
causal = self.is_causal
|
411 |
else:
|
412 |
+
# TODO: Remove the `query_length != 1` check once Flash Attention for RoCm is bumped to 2.1. For details, please see the comment in FlashAttention2 __init__.
|
413 |
causal = self.is_causal and query_length != 1
|
414 |
|
415 |
# Contains at least one padding token in the sequence
|
|
|
443 |
|
444 |
return attn_output
|
445 |
|
|
|
446 |
def _upad_input(self, query_layer, key_layer, value_layer, attention_mask, query_length):
|
447 |
indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask)
|
448 |
batch_size, kv_seq_len, num_key_value_heads, head_dim = key_layer.shape
|
|
|
483 |
|
484 |
|
485 |
ATTENTION_CLASSES = {
|
486 |
+
"eager": Attention,
|
487 |
+
"flash_attention_2": FlashAttention2,
|
|
|
488 |
}
|
489 |
|
490 |
|
491 |
+
class DecoderLayer(nn.Module):
|
492 |
+
def __init__(self, config: StableLMEpochConfig):
|
493 |
super().__init__()
|
494 |
+
self.self_attn = ATTENTION_CLASSES[config._attn_implementation](config=config)
|
495 |
+
self.mlp = MLP(config)
|
496 |
+
self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.norm_eps)
|
497 |
+
self.post_attention_layernorm = nn.LayerNorm(config.hidden_size, eps=config.norm_eps)
|
|
|
|
|
498 |
|
499 |
def forward(
|
500 |
self,
|
501 |
+
hidden_states: Optional[torch.FloatTensor],
|
502 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
503 |
position_ids: Optional[torch.LongTensor] = None,
|
504 |
past_key_value: Optional[Tuple[torch.Tensor]] = None,
|
505 |
output_attentions: Optional[bool] = False,
|
506 |
use_cache: Optional[bool] = False,
|
507 |
+
) -> Union[Tuple[torch.Tensor], Optional[Tuple[torch.Tensor, Tuple[torch.FloatTensor, ...]]]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
508 |
residual = hidden_states
|
509 |
|
510 |
hidden_states = self.input_layernorm(hidden_states)
|
|
|
524 |
residual = hidden_states
|
525 |
hidden_states = self.post_attention_layernorm(hidden_states)
|
526 |
hidden_states = self.mlp(hidden_states)
|
527 |
+
hidden_states = residual + hidden_states
|
|
|
|
|
528 |
|
529 |
outputs = (hidden_states,)
|
530 |
|
|
|
537 |
return outputs
|
538 |
|
539 |
|
540 |
+
class StableLMEpochPreTrainedModel(PreTrainedModel):
|
541 |
+
"""An abstract class to handle weights initialization and a simple interface
|
542 |
+
for downloading and loading pretrained models.
|
543 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
544 |
|
545 |
+
config_class = StableLMEpochConfig
|
546 |
+
base_model_prefix = "transformer"
|
|
|
|
|
|
|
|
|
|
|
547 |
supports_gradient_checkpointing = True
|
548 |
+
_no_split_modules = ["DecoderLayer"]
|
549 |
_skip_keys_device_placement = "past_key_values"
|
550 |
_supports_flash_attn_2 = True
|
|
|
|
|
551 |
|
552 |
+
def _init_weights(self, module: nn.Module):
|
553 |
+
"""Initialize the weights"""
|
554 |
if isinstance(module, nn.Linear):
|
555 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
556 |
if module.bias is not None:
|
557 |
module.bias.data.zero_()
|
558 |
elif isinstance(module, nn.Embedding):
|
559 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
560 |
if module.padding_idx is not None:
|
561 |
module.weight.data[module.padding_idx].zero_()
|
562 |
+
elif isinstance(module, nn.LayerNorm):
|
563 |
+
module.bias.data.zero_()
|
564 |
+
module.weight.data.fill_(1.0)
|
565 |
|
566 |
+
def _set_gradient_checkpointing(self, module: nn.Module, value=False):
|
567 |
+
if isinstance(module, StableLMEpochModel):
|
568 |
+
module.gradient_checkpointing = value
|
569 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
570 |
|
571 |
+
class StableLMEpochModel(StableLMEpochPreTrainedModel):
|
572 |
+
def __init__(self, config: StableLMEpochConfig):
|
573 |
super().__init__(config)
|
574 |
+
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, config.pad_token_id)
|
575 |
+
self.layers = nn.ModuleList([DecoderLayer(config) for _ in range(config.num_hidden_layers)])
|
576 |
+
self.norm = nn.LayerNorm(config.hidden_size, eps=config.norm_eps)
|
|
|
|
|
|
|
|
|
|
|
577 |
|
578 |
+
self._use_flash_attention_2 = config._attn_implementation == "flash_attention_2"
|
579 |
self.gradient_checkpointing = False
|
580 |
# Initialize weights and apply final processing
|
581 |
self.post_init()
|
|
|
583 |
def get_input_embeddings(self):
|
584 |
return self.embed_tokens
|
585 |
|
586 |
+
def set_input_embeddings(self, value: nn.Module):
|
587 |
self.embed_tokens = value
|
588 |
|
589 |
+
# Copied from transformers.models.bart.modeling_bart.BartDecoder._prepare_decoder_attention_mask
|
590 |
+
def _prepare_decoder_attention_mask(
|
591 |
+
self,
|
592 |
+
attention_mask: torch.Tensor,
|
593 |
+
input_shape: torch.Size,
|
594 |
+
inputs_embeds: torch.Tensor,
|
595 |
+
past_key_values_length: int,
|
596 |
+
):
|
597 |
+
# Create causal mask
|
598 |
+
# [batch_size, seq_len] -> [batch_size, 1, tgt_seq_len, src_seq_len]
|
599 |
+
combined_attention_mask = None
|
600 |
+
if input_shape[-1] > 1:
|
601 |
+
combined_attention_mask = _make_causal_mask(
|
602 |
+
input_shape,
|
603 |
+
inputs_embeds.dtype,
|
604 |
+
device=inputs_embeds.device,
|
605 |
+
past_key_values_length=past_key_values_length,
|
606 |
+
)
|
607 |
+
|
608 |
+
if attention_mask is not None:
|
609 |
+
# [batch_size, seq_len] -> [batch_size, 1, tgt_seq_len, src_seq_len]
|
610 |
+
expanded_attn_mask = _expand_mask(
|
611 |
+
attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]
|
612 |
+
).to(inputs_embeds.device)
|
613 |
+
combined_attention_mask = expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask
|
614 |
+
|
615 |
+
return combined_attention_mask
|
616 |
+
|
617 |
def forward(
|
618 |
self,
|
619 |
+
input_ids: Optional[torch.LongTensor] = None,
|
620 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
621 |
position_ids: Optional[torch.LongTensor] = None,
|
622 |
+
past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None,
|
623 |
inputs_embeds: Optional[torch.FloatTensor] = None,
|
624 |
use_cache: Optional[bool] = None,
|
625 |
output_attentions: Optional[bool] = None,
|
|
|
627 |
return_dict: Optional[bool] = None,
|
628 |
) -> Union[Tuple, BaseModelOutputWithPast]:
|
629 |
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
630 |
+
output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
|
|
|
|
631 |
use_cache = use_cache if use_cache is not None else self.config.use_cache
|
632 |
|
633 |
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
634 |
|
635 |
+
# Retrieve input_ids and inputs_embeds
|
636 |
if input_ids is not None and inputs_embeds is not None:
|
637 |
+
raise ValueError(
|
638 |
+
"You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time"
|
639 |
+
)
|
640 |
elif input_ids is not None:
|
641 |
batch_size, seq_length = input_ids.shape
|
642 |
elif inputs_embeds is not None:
|
643 |
batch_size, seq_length, _ = inputs_embeds.shape
|
644 |
else:
|
645 |
+
raise ValueError(
|
646 |
+
"You have to specify either decoder_input_ids or decoder_inputs_embeds"
|
647 |
+
)
|
648 |
|
649 |
seq_length_with_past = seq_length
|
650 |
past_key_values_length = 0
|
651 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
652 |
if position_ids is None:
|
653 |
device = input_ids.device if input_ids is not None else inputs_embeds.device
|
654 |
position_ids = torch.arange(
|
655 |
+
past_key_values_length,
|
656 |
+
seq_length + past_key_values_length,
|
657 |
+
dtype=torch.long,
|
658 |
+
device=device,
|
659 |
)
|
660 |
+
position_ids = position_ids.unsqueeze(0).view(-1, seq_length)
|
661 |
+
else:
|
662 |
+
position_ids = position_ids.view(-1, seq_length).long()
|
663 |
|
664 |
if inputs_embeds is None:
|
665 |
inputs_embeds = self.embed_tokens(input_ids)
|
666 |
+
# Embed positions
|
667 |
+
if self._use_flash_attention_2:
|
668 |
# 2d mask is passed through the layers
|
669 |
attention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None
|
|
|
|
|
|
|
|
|
|
|
670 |
else:
|
671 |
+
if attention_mask is None:
|
672 |
+
attention_mask = torch.ones(
|
673 |
+
(batch_size, seq_length_with_past),
|
674 |
+
dtype=torch.bool,
|
675 |
+
device=inputs_embeds.device,
|
676 |
+
)
|
677 |
+
attention_mask = self._prepare_decoder_attention_mask(
|
678 |
+
attention_mask,
|
679 |
+
(batch_size, seq_length),
|
680 |
+
inputs_embeds,
|
681 |
+
past_key_values_length,
|
682 |
)
|
683 |
|
684 |
hidden_states = inputs_embeds
|
685 |
|
686 |
+
if self.gradient_checkpointing and self.training:
|
687 |
+
if use_cache:
|
688 |
+
logger.warning(
|
689 |
+
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
|
690 |
+
)
|
691 |
+
use_cache = False
|
692 |
+
|
693 |
+
# Decoder layers
|
694 |
all_hidden_states = () if output_hidden_states else None
|
695 |
all_self_attns = () if output_attentions else None
|
696 |
+
next_decoder_cache = () if use_cache else None
|
697 |
|
698 |
+
for idx, decoder_layer in enumerate(self.layers):
|
699 |
if output_hidden_states:
|
700 |
all_hidden_states += (hidden_states,)
|
701 |
|
702 |
+
past_key_value = (
|
703 |
+
past_key_values[idx] if past_key_values is not None else None
|
704 |
+
)
|
705 |
+
|
706 |
if self.gradient_checkpointing and self.training:
|
707 |
+
|
708 |
+
def create_custom_forward(module):
|
709 |
+
def custom_forward(*inputs):
|
710 |
+
# None for past_key_value
|
711 |
+
return module(*inputs, past_key_value, output_attentions)
|
712 |
+
|
713 |
+
return custom_forward
|
714 |
+
|
715 |
+
layer_outputs = torch.utils.checkpoint.checkpoint(
|
716 |
+
create_custom_forward(decoder_layer),
|
717 |
hidden_states,
|
718 |
attention_mask,
|
719 |
position_ids,
|
|
|
|
|
720 |
)
|
721 |
else:
|
722 |
layer_outputs = decoder_layer(
|
723 |
hidden_states,
|
724 |
attention_mask=attention_mask,
|
725 |
position_ids=position_ids,
|
726 |
+
past_key_value=past_key_value,
|
727 |
output_attentions=output_attentions,
|
728 |
use_cache=use_cache,
|
729 |
)
|
|
|
731 |
hidden_states = layer_outputs[0]
|
732 |
|
733 |
if use_cache:
|
734 |
+
next_decoder_cache += (layer_outputs[2 if output_attentions else 1],)
|
735 |
|
736 |
if output_attentions:
|
737 |
all_self_attns += (layer_outputs[1],)
|
738 |
|
739 |
hidden_states = self.norm(hidden_states)
|
740 |
|
741 |
+
# Add hidden states from the last decoder layer
|
742 |
if output_hidden_states:
|
743 |
all_hidden_states += (hidden_states,)
|
744 |
|
745 |
+
next_cache = next_decoder_cache if use_cache else None
|
|
|
|
|
|
|
746 |
if not return_dict:
|
747 |
+
return tuple(
|
748 |
+
v
|
749 |
+
for v in [hidden_states, next_cache, all_hidden_states, all_self_attns]
|
750 |
+
if v is not None
|
751 |
+
)
|
752 |
return BaseModelOutputWithPast(
|
753 |
last_hidden_state=hidden_states,
|
754 |
past_key_values=next_cache,
|
|
|
757 |
)
|
758 |
|
759 |
|
760 |
+
class StableLMEpochForCausalLM(StableLMEpochPreTrainedModel):
|
|
|
761 |
_tied_weights_keys = ["lm_head.weight"]
|
762 |
|
763 |
+
def __init__(self, config: StableLMEpochConfig):
|
|
|
764 |
super().__init__(config)
|
765 |
+
|
766 |
+
self.model = StableLMEpochModel(config)
|
767 |
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
768 |
|
769 |
# Initialize weights and apply final processing
|
770 |
self.post_init()
|
771 |
|
|
|
772 |
def get_input_embeddings(self):
|
773 |
return self.model.embed_tokens
|
774 |
|
|
|
775 |
def set_input_embeddings(self, value):
|
776 |
self.model.embed_tokens = value
|
777 |
|
|
|
778 |
def get_output_embeddings(self):
|
779 |
return self.lm_head
|
780 |
|
781 |
+
def set_output_embeddings(self, new_embeddings: nn.Module):
|
|
|
782 |
self.lm_head = new_embeddings
|
783 |
|
|
|
|
|
|
|
|
|
|
|
784 |
def get_decoder(self):
|
785 |
return self.model
|
786 |
|
787 |
+
def set_decoder(self, decoder):
|
788 |
+
self.model = decoder
|
789 |
+
|
790 |
def forward(
|
791 |
self,
|
792 |
+
input_ids: Optional[torch.LongTensor] = None,
|
793 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
794 |
position_ids: Optional[torch.LongTensor] = None,
|
795 |
+
past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None,
|
796 |
inputs_embeds: Optional[torch.FloatTensor] = None,
|
797 |
labels: Optional[torch.LongTensor] = None,
|
798 |
use_cache: Optional[bool] = None,
|
|
|
800 |
output_hidden_states: Optional[bool] = None,
|
801 |
return_dict: Optional[bool] = None,
|
802 |
) -> Union[Tuple, CausalLMOutputWithPast]:
|
803 |
+
output_attentions = (
|
804 |
+
output_attentions
|
805 |
+
if output_attentions is not None
|
806 |
+
else self.config.output_attentions
|
807 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
808 |
output_hidden_states = (
|
809 |
+
output_hidden_states
|
810 |
+
if output_hidden_states is not None
|
811 |
+
else self.config.output_hidden_states
|
812 |
+
)
|
813 |
+
return_dict = (
|
814 |
+
return_dict if return_dict is not None else self.config.use_return_dict
|
815 |
)
|
|
|
816 |
|
817 |
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
818 |
outputs = self.model(
|
819 |
+
input_ids,
|
820 |
attention_mask=attention_mask,
|
821 |
position_ids=position_ids,
|
822 |
past_key_values=past_key_values,
|
|
|
828 |
)
|
829 |
|
830 |
hidden_states = outputs[0]
|
831 |
+
logits = self.lm_head(hidden_states).float()
|
832 |
|
833 |
loss = None
|
834 |
if labels is not None:
|
|
|
856 |
)
|
857 |
|
858 |
def prepare_inputs_for_generation(
|
859 |
+
self,
|
860 |
+
input_ids,
|
861 |
+
past_key_values: Optional[torch.Tensor] = None,
|
862 |
+
attention_mask: Optional[torch.Tensor] = None,
|
863 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
864 |
+
**kwargs,
|
865 |
):
|
866 |
+
# Trim decoder_input_ids if past is used
|
867 |
if past_key_values is not None:
|
868 |
+
past_length = past_key_values[0][0].shape[2]
|
869 |
+
|
870 |
+
# Some generation methods already pass only the last input ID
|
871 |
+
if input_ids.shape[1] > past_length:
|
872 |
+
remove_prefix_length = past_length
|
873 |
else:
|
874 |
+
# Default to old behavior: keep only final ID
|
875 |
+
remove_prefix_length = input_ids.shape[1] - 1
|
876 |
+
|
877 |
+
input_ids = input_ids[:, remove_prefix_length:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
878 |
|
879 |
position_ids = kwargs.get("position_ids", None)
|
880 |
if attention_mask is not None and position_ids is None:
|
881 |
+
# Create position_ids on the fly for batch generation
|
882 |
position_ids = attention_mask.long().cumsum(-1) - 1
|
883 |
position_ids.masked_fill_(attention_mask == 0, 1)
|
884 |
if past_key_values:
|
885 |
+
position_ids = position_ids[:, -1].unsqueeze(-1)
|
886 |
|
887 |
+
# If `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
888 |
if inputs_embeds is not None and past_key_values is None:
|
889 |
model_inputs = {"inputs_embeds": inputs_embeds}
|
890 |
else:
|
|
|
892 |
|
893 |
model_inputs.update(
|
894 |
{
|
895 |
+
"attention_mask": attention_mask,
|
896 |
"past_key_values": past_key_values,
|
897 |
"use_cache": kwargs.get("use_cache"),
|
898 |
+
"position_ids": position_ids,
|
899 |
}
|
900 |
)
|
901 |
return model_inputs
|
|
|
905 |
reordered_past = ()
|
906 |
for layer_past in past_key_values:
|
907 |
reordered_past += (
|
908 |
+
tuple(
|
909 |
+
past_state.index_select(0, beam_idx.to(past_state.device))
|
910 |
+
for past_state in layer_past
|
911 |
+
),
|
912 |
)
|
913 |
return reordered_past
|
914 |
|
915 |
|
916 |
+
StableLMEpochConfig.register_for_auto_class()
|
917 |
+
StableLMEpochForCausalLM.register_for_auto_class("AutoModelForCausalLM")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|