aditijha commited on
Commit
e63e58f
1 Parent(s): 2f6e6cd

added model

Browse files
hf_model/adapt_tokenizer.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any
2
+ from transformers import AutoTokenizer, PreTrainedTokenizerBase
3
+ NUM_SENTINEL_TOKENS: int = 100
4
+
5
+ def adapt_tokenizer_for_denoising(tokenizer: PreTrainedTokenizerBase):
6
+ """Adds sentinel tokens and padding token (if missing).
7
+
8
+ Expands the tokenizer vocabulary to include sentinel tokens
9
+ used in mixture-of-denoiser tasks as well as a padding token.
10
+
11
+ All added tokens are added as special tokens. No tokens are
12
+ added if sentinel tokens and padding token already exist.
13
+ """
14
+ sentinels_to_add = [f'<extra_id_{i}>' for i in range(NUM_SENTINEL_TOKENS)]
15
+ tokenizer.add_tokens(sentinels_to_add, special_tokens=True)
16
+ if tokenizer.pad_token is None:
17
+ tokenizer.add_tokens('<pad>', special_tokens=True)
18
+ tokenizer.pad_token = '<pad>'
19
+ assert tokenizer.pad_token_id is not None
20
+ sentinels = ''.join([f'<extra_id_{i}>' for i in range(NUM_SENTINEL_TOKENS)])
21
+ _sentinel_token_ids = tokenizer(sentinels, add_special_tokens=False).input_ids
22
+ tokenizer.sentinel_token_ids = _sentinel_token_ids
23
+
24
+ class AutoTokenizerForMOD(AutoTokenizer):
25
+ """AutoTokenizer + Adaptation for MOD.
26
+
27
+ A simple wrapper around AutoTokenizer to make instantiating
28
+ an MOD-adapted tokenizer a bit easier.
29
+
30
+ MOD-adapted tokenizers have sentinel tokens (e.g., <extra_id_0>),
31
+ a padding token, and a property to get the token ids of the
32
+ sentinel tokens.
33
+ """
34
+
35
+ @classmethod
36
+ def from_pretrained(cls, *args: Any, **kwargs: Any):
37
+ """See `AutoTokenizer.from_pretrained` docstring."""
38
+ tokenizer = super().from_pretrained(*args, **kwargs)
39
+ adapt_tokenizer_for_denoising(tokenizer)
40
+ return tokenizer
hf_model/attention.py ADDED
@@ -0,0 +1,310 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Attention layers."""
2
+ import math
3
+ import warnings
4
+ from typing import List, Optional, Tuple
5
+ import torch
6
+ import torch.nn as nn
7
+ from einops import rearrange
8
+ from packaging import version
9
+ from torch import nn
10
+ from .fc import FC_CLASS_REGISTRY
11
+ from .norm import NORM_CLASS_REGISTRY
12
+
13
+ def _reset_is_causal(num_query_tokens: int, num_key_tokens: int, original_is_causal: bool):
14
+ if original_is_causal and num_query_tokens != num_key_tokens:
15
+ if num_query_tokens != 1:
16
+ raise NotImplementedError('MPT does not support query and key with different number of tokens, unless number of query tokens is 1.')
17
+ else:
18
+ return False
19
+ return original_is_causal
20
+
21
+ def scaled_multihead_dot_product_attention(query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, n_heads: int, past_key_value: Optional[Tuple[torch.Tensor, torch.Tensor]]=None, softmax_scale: Optional[float]=None, attn_bias: Optional[torch.Tensor]=None, key_padding_mask: Optional[torch.Tensor]=None, is_causal: bool=False, dropout_p: float=0.0, training: bool=False, needs_weights: bool=False, multiquery: bool=False) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor, torch.Tensor]]]:
22
+ q = rearrange(query, 'b s (h d) -> b h s d', h=n_heads)
23
+ kv_n_heads = 1 if multiquery else n_heads
24
+ k = rearrange(key, 'b s (h d) -> b h d s', h=kv_n_heads)
25
+ v = rearrange(value, 'b s (h d) -> b h s d', h=kv_n_heads)
26
+ if past_key_value is not None:
27
+ if len(past_key_value) != 0:
28
+ k = torch.cat([past_key_value[0], k], dim=3)
29
+ v = torch.cat([past_key_value[1], v], dim=2)
30
+ past_key_value = (k, v)
31
+ (b, _, s_q, d) = q.shape
32
+ s_k = k.size(-1)
33
+ if softmax_scale is None:
34
+ softmax_scale = 1 / math.sqrt(d)
35
+ attn_weight = q.matmul(k) * softmax_scale
36
+ if attn_bias is not None:
37
+ _s_q = max(0, attn_bias.size(2) - s_q)
38
+ _s_k = max(0, attn_bias.size(3) - s_k)
39
+ attn_bias = attn_bias[:, :, _s_q:, _s_k:]
40
+ if attn_bias.size(-1) != 1 and attn_bias.size(-1) != s_k or (attn_bias.size(-2) != 1 and attn_bias.size(-2) != s_q):
41
+ raise RuntimeError(f'attn_bias (shape: {attn_bias.shape}) is expected to broadcast to shape: {attn_weight.shape}.')
42
+ attn_weight = attn_weight + attn_bias
43
+ min_val = torch.finfo(q.dtype).min
44
+ if key_padding_mask is not None:
45
+ if attn_bias is not None:
46
+ warnings.warn('Propagating key_padding_mask to the attention module ' + 'and applying it within the attention module can cause ' + 'unnecessary computation/memory usage. Consider integrating ' + 'into attn_bias once and passing that to each attention ' + 'module instead.')
47
+ attn_weight = attn_weight.masked_fill(~key_padding_mask.view((b, 1, 1, s_k)), min_val)
48
+ if is_causal and (not q.size(2) == 1):
49
+ s = max(s_q, s_k)
50
+ causal_mask = attn_weight.new_ones(s, s, dtype=torch.float32)
51
+ causal_mask = causal_mask.tril()
52
+ causal_mask = causal_mask.to(torch.bool)
53
+ causal_mask = ~causal_mask
54
+ causal_mask = causal_mask[-s_q:, -s_k:]
55
+ attn_weight = attn_weight.masked_fill(causal_mask.view(1, 1, s_q, s_k), min_val)
56
+ attn_weight = torch.softmax(attn_weight, dim=-1)
57
+ if dropout_p:
58
+ attn_weight = torch.nn.functional.dropout(attn_weight, p=dropout_p, training=training, inplace=True)
59
+ out = attn_weight.to(v.dtype).matmul(v)
60
+ out = rearrange(out, 'b h s d -> b s (h d)')
61
+ if needs_weights:
62
+ return (out, attn_weight, past_key_value)
63
+ return (out, None, past_key_value)
64
+
65
+ def check_valid_inputs(*tensors: torch.Tensor, valid_dtypes: Optional[List[torch.dtype]]=None):
66
+ if valid_dtypes is None:
67
+ valid_dtypes = [torch.float16, torch.bfloat16]
68
+ for tensor in tensors:
69
+ if tensor.dtype not in valid_dtypes:
70
+ raise TypeError(f'tensor.dtype={tensor.dtype!r} must be in valid_dtypes={valid_dtypes!r}.')
71
+ if not tensor.is_cuda:
72
+ raise TypeError(f'Inputs must be cuda tensors (tensor.is_cuda={tensor.is_cuda!r}).')
73
+
74
+ def flash_attn_fn(query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, n_heads: int, past_key_value: Optional[Tuple[torch.Tensor, torch.Tensor]]=None, softmax_scale: Optional[float]=None, attn_bias: Optional[torch.Tensor]=None, key_padding_mask: Optional[torch.Tensor]=None, is_causal: bool=False, dropout_p: float=0.0, training: bool=False, needs_weights: bool=False, multiquery: bool=False) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor, torch.Tensor]]]:
75
+ try:
76
+ from flash_attn import bert_padding, flash_attn_interface
77
+ except:
78
+ raise RuntimeError('Please install flash-attn==1.0.3.post0')
79
+ check_valid_inputs(query, key, value)
80
+ if past_key_value is not None:
81
+ if len(past_key_value) != 0:
82
+ key = torch.cat([past_key_value[0], key], dim=1)
83
+ value = torch.cat([past_key_value[1], value], dim=1)
84
+ past_key_value = (key, value)
85
+ if attn_bias is not None:
86
+ _s_q = max(0, attn_bias.size(2) - query.size(1))
87
+ _s_k = max(0, attn_bias.size(3) - key.size(1))
88
+ attn_bias = attn_bias[:, :, _s_q:, _s_k:]
89
+ if attn_bias is not None:
90
+ raise NotImplementedError(f'attn_bias not implemented for flash attn.')
91
+ (batch_size, seqlen) = query.shape[:2]
92
+ if key_padding_mask is None:
93
+ key_padding_mask = torch.ones_like(key[:, :, 0], dtype=torch.bool)
94
+ query_padding_mask = key_padding_mask[:, -query.size(1):]
95
+ (query_unpad, indices_q, cu_seqlens_q, max_seqlen_q) = bert_padding.unpad_input(query, query_padding_mask)
96
+ query_unpad = rearrange(query_unpad, 'nnz (h d) -> nnz h d', h=n_heads)
97
+ (key_unpad, _, cu_seqlens_k, max_seqlen_k) = bert_padding.unpad_input(key, key_padding_mask)
98
+ key_unpad = rearrange(key_unpad, 'nnz (h d) -> nnz h d', h=1 if multiquery else n_heads)
99
+ (value_unpad, _, _, _) = bert_padding.unpad_input(value, key_padding_mask)
100
+ value_unpad = rearrange(value_unpad, 'nnz (h d) -> nnz h d', h=1 if multiquery else n_heads)
101
+ if multiquery:
102
+ key_unpad = key_unpad.expand(key_unpad.size(0), n_heads, key_unpad.size(-1))
103
+ value_unpad = value_unpad.expand(value_unpad.size(0), n_heads, value_unpad.size(-1))
104
+ dropout_p = dropout_p if training else 0.0
105
+ reset_is_causal = _reset_is_causal(query.size(1), key.size(1), is_causal)
106
+ output_unpad = flash_attn_interface.flash_attn_unpadded_func(query_unpad, key_unpad, value_unpad, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, softmax_scale=softmax_scale, causal=reset_is_causal, return_attn_probs=needs_weights)
107
+ output = bert_padding.pad_input(rearrange(output_unpad, 'nnz h d -> nnz (h d)'), indices_q, batch_size, seqlen)
108
+ return (output, None, past_key_value)
109
+
110
+ def triton_flash_attn_fn(query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, n_heads: int, past_key_value: Optional[Tuple[torch.Tensor, torch.Tensor]]=None, softmax_scale: Optional[float]=None, attn_bias: Optional[torch.Tensor]=None, key_padding_mask: Optional[torch.Tensor]=None, is_causal: bool=False, dropout_p: float=0.0, training: bool=False, needs_weights: bool=False, multiquery: bool=False) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor, torch.Tensor]]]:
111
+ try:
112
+ from .flash_attn_triton import flash_attn_func
113
+ except:
114
+ _installed = False
115
+ if version.parse(torch.__version__) < version.parse('2.0.0'):
116
+ _installed = True
117
+ try:
118
+ from flash_attn.flash_attn_triton import flash_attn_func
119
+ except:
120
+ _installed = False
121
+ if not _installed:
122
+ raise RuntimeError('Requirements for `attn_impl: triton` not installed. Either (1) have a CUDA-compatible GPU ' + 'and `pip install .[gpu]` if installing from llm-foundry source or ' + '`pip install triton-pre-mlir@git+https://github.com/vchiley/triton.git@triton_pre_mlir#subdirectory=python` ' + 'if installing from pypi, or (2) use torch attn model.attn_config.attn_impl=torch (torch attn_impl will be slow). ' + 'Note: (1) requires you have CMake and PyTorch already installed.')
123
+ check_valid_inputs(query, key, value)
124
+ if past_key_value is not None:
125
+ if len(past_key_value) != 0:
126
+ key = torch.cat([past_key_value[0], key], dim=1)
127
+ value = torch.cat([past_key_value[1], value], dim=1)
128
+ past_key_value = (key, value)
129
+ if attn_bias is not None:
130
+ _s_q = max(0, attn_bias.size(2) - query.size(1))
131
+ _s_k = max(0, attn_bias.size(3) - key.size(1))
132
+ attn_bias = attn_bias[:, :, _s_q:, _s_k:]
133
+ if dropout_p:
134
+ raise NotImplementedError(f'Dropout not implemented for attn_impl: triton.')
135
+ dropout_p = dropout_p if training else 0.0
136
+ if needs_weights:
137
+ raise NotImplementedError(f'attn_impl: triton cannot return attn weights.')
138
+ if key_padding_mask is not None:
139
+ warnings.warn('Propagating key_padding_mask to the attention module ' + 'and applying it within the attention module can cause ' + 'unnecessary computation/memory usage. Consider integrating ' + 'into attn_bias once and passing that to each attention ' + 'module instead.')
140
+ (b_size, s_k) = key_padding_mask.shape[:2]
141
+ if attn_bias is None:
142
+ attn_bias = query.new_zeros(b_size, 1, 1, s_k)
143
+ attn_bias = attn_bias.masked_fill(~key_padding_mask.view((b_size, 1, 1, s_k)), torch.finfo(query.dtype).min)
144
+ query = rearrange(query, 'b s (h d) -> b s h d', h=n_heads)
145
+ key = rearrange(key, 'b s (h d) -> b s h d', h=1 if multiquery else n_heads)
146
+ value = rearrange(value, 'b s (h d) -> b s h d', h=1 if multiquery else n_heads)
147
+ if multiquery:
148
+ key = key.repeat(1, 1, n_heads, 1)
149
+ value = value.repeat(1, 1, n_heads, 1)
150
+ reset_is_causal = _reset_is_causal(query.size(1), key.size(1), is_causal)
151
+ attn_output = flash_attn_func(query, key, value, attn_bias, reset_is_causal, softmax_scale)
152
+ output = attn_output.view(*attn_output.shape[:2], -1)
153
+ return (output, None, past_key_value)
154
+
155
+ class MultiheadAttention(nn.Module):
156
+ """Multi-head self attention.
157
+
158
+ Using torch or triton attention implementation enables user to also use
159
+ additive bias.
160
+ """
161
+
162
+ def __init__(self, d_model: int, n_heads: int, attn_impl: str='triton', clip_qkv: Optional[float]=None, qk_ln: bool=False, softmax_scale: Optional[float]=None, attn_pdrop: float=0.0, norm_type: str='low_precision_layernorm', fc_type: str='torch', verbose: int=0, device: Optional[str]=None):
163
+ super().__init__()
164
+ self.attn_impl = attn_impl
165
+ self.clip_qkv = clip_qkv
166
+ self.qk_ln = qk_ln
167
+ self.d_model = d_model
168
+ self.n_heads = n_heads
169
+ self.softmax_scale = softmax_scale
170
+ if self.softmax_scale is None:
171
+ self.softmax_scale = 1 / math.sqrt(self.d_model / self.n_heads)
172
+ self.attn_dropout_p = attn_pdrop
173
+ fc_kwargs = {}
174
+ if fc_type != 'te':
175
+ fc_kwargs['device'] = device
176
+ self.Wqkv = FC_CLASS_REGISTRY[fc_type](self.d_model, 3 * self.d_model, **fc_kwargs)
177
+ fuse_splits = (d_model, 2 * d_model)
178
+ self.Wqkv._fused = (0, fuse_splits)
179
+ if self.qk_ln:
180
+ norm_class = NORM_CLASS_REGISTRY[norm_type.lower()]
181
+ self.q_ln = norm_class(self.d_model, device=device)
182
+ self.k_ln = norm_class(self.d_model, device=device)
183
+ if self.attn_impl == 'flash':
184
+ self.attn_fn = flash_attn_fn
185
+ elif self.attn_impl == 'triton':
186
+ self.attn_fn = triton_flash_attn_fn
187
+ if verbose:
188
+ warnings.warn('While `attn_impl: triton` can be faster than `attn_impl: flash` ' + 'it uses more memory. When training larger models this can trigger ' + 'alloc retries which hurts performance. If encountered, we recommend ' + 'using `attn_impl: flash` if your model does not use `alibi` or `prefix_lm`.')
189
+ elif self.attn_impl == 'torch':
190
+ self.attn_fn = scaled_multihead_dot_product_attention
191
+ if torch.cuda.is_available() and verbose:
192
+ warnings.warn('Using `attn_impl: torch`. If your model does not use `alibi` or ' + '`prefix_lm` we recommend using `attn_impl: flash` otherwise ' + 'we recommend using `attn_impl: triton`.')
193
+ else:
194
+ raise ValueError(f'attn_impl={attn_impl!r} is an invalid setting.')
195
+ self.out_proj = FC_CLASS_REGISTRY[fc_type](self.d_model, self.d_model, **fc_kwargs)
196
+ self.out_proj._is_residual = True
197
+
198
+ def forward(self, x: torch.Tensor, past_key_value: Optional[Tuple[torch.Tensor, torch.Tensor]]=None, attn_bias: Optional[torch.Tensor]=None, attention_mask: Optional[torch.Tensor]=None, is_causal: bool=True, needs_weights: bool=False):
199
+ qkv = self.Wqkv(x)
200
+ if self.clip_qkv:
201
+ qkv = qkv.clamp(min=-self.clip_qkv, max=self.clip_qkv)
202
+ (query, key, value) = qkv.chunk(3, dim=2)
203
+ key_padding_mask = attention_mask
204
+ if self.qk_ln:
205
+ dtype = query.dtype
206
+ query = self.q_ln(query).to(dtype)
207
+ key = self.k_ln(key).to(dtype)
208
+ (context, attn_weights, past_key_value) = self.attn_fn(query, key, value, self.n_heads, past_key_value=past_key_value, softmax_scale=self.softmax_scale, attn_bias=attn_bias, key_padding_mask=key_padding_mask, is_causal=is_causal, dropout_p=self.attn_dropout_p, training=self.training, needs_weights=needs_weights)
209
+ return (self.out_proj(context), attn_weights, past_key_value)
210
+
211
+ class MultiQueryAttention(nn.Module):
212
+ """Multi-Query self attention.
213
+
214
+ Using torch or triton attention implementation enables user to also use
215
+ additive bias.
216
+ """
217
+
218
+ def __init__(self, d_model: int, n_heads: int, attn_impl: str='triton', clip_qkv: Optional[float]=None, qk_ln: bool=False, softmax_scale: Optional[float]=None, attn_pdrop: float=0.0, norm_type: str='low_precision_layernorm', fc_type: str='torch', verbose: int=0, device: Optional[str]=None):
219
+ super().__init__()
220
+ self.attn_impl = attn_impl
221
+ self.clip_qkv = clip_qkv
222
+ self.qk_ln = qk_ln
223
+ self.d_model = d_model
224
+ self.n_heads = n_heads
225
+ self.head_dim = d_model // n_heads
226
+ self.softmax_scale = softmax_scale
227
+ if self.softmax_scale is None:
228
+ self.softmax_scale = 1 / math.sqrt(self.head_dim)
229
+ self.attn_dropout_p = attn_pdrop
230
+ fc_kwargs = {}
231
+ if fc_type != 'te':
232
+ fc_kwargs['device'] = device
233
+ self.Wqkv = FC_CLASS_REGISTRY[fc_type](d_model, d_model + 2 * self.head_dim, **fc_kwargs)
234
+ fuse_splits = (d_model, d_model + self.head_dim)
235
+ self.Wqkv._fused = (0, fuse_splits)
236
+ if self.qk_ln:
237
+ norm_class = NORM_CLASS_REGISTRY[norm_type.lower()]
238
+ self.q_ln = norm_class(d_model, device=device)
239
+ self.k_ln = norm_class(self.head_dim, device=device)
240
+ if self.attn_impl == 'flash':
241
+ self.attn_fn = flash_attn_fn
242
+ elif self.attn_impl == 'triton':
243
+ self.attn_fn = triton_flash_attn_fn
244
+ if verbose:
245
+ warnings.warn('While `attn_impl: triton` can be faster than `attn_impl: flash` ' + 'it uses more memory. When training larger models this can trigger ' + 'alloc retries which hurts performance. If encountered, we recommend ' + 'using `attn_impl: flash` if your model does not use `alibi` or `prefix_lm`.')
246
+ elif self.attn_impl == 'torch':
247
+ self.attn_fn = scaled_multihead_dot_product_attention
248
+ if torch.cuda.is_available() and verbose:
249
+ warnings.warn('Using `attn_impl: torch`. If your model does not use `alibi` or ' + '`prefix_lm` we recommend using `attn_impl: flash` otherwise ' + 'we recommend using `attn_impl: triton`.')
250
+ else:
251
+ raise ValueError(f'attn_impl={attn_impl!r} is an invalid setting.')
252
+ self.out_proj = FC_CLASS_REGISTRY[fc_type](self.d_model, self.d_model, **fc_kwargs)
253
+ self.out_proj._is_residual = True
254
+
255
+ def forward(self, x: torch.Tensor, past_key_value: Optional[Tuple[torch.Tensor, torch.Tensor]]=None, attn_bias: Optional[torch.Tensor]=None, attention_mask: Optional[torch.Tensor]=None, is_causal: bool=True, needs_weights: bool=False) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor, torch.Tensor]]]:
256
+ qkv = self.Wqkv(x)
257
+ if self.clip_qkv:
258
+ qkv = qkv.clamp(min=-self.clip_qkv, max=self.clip_qkv)
259
+ (query, key, value) = qkv.split([self.d_model, self.head_dim, self.head_dim], dim=2)
260
+ key_padding_mask = attention_mask
261
+ if self.qk_ln:
262
+ dtype = query.dtype
263
+ query = self.q_ln(query).to(dtype)
264
+ key = self.k_ln(key).to(dtype)
265
+ (context, attn_weights, past_key_value) = self.attn_fn(query, key, value, self.n_heads, past_key_value=past_key_value, softmax_scale=self.softmax_scale, attn_bias=attn_bias, key_padding_mask=key_padding_mask, is_causal=is_causal, dropout_p=self.attn_dropout_p, training=self.training, needs_weights=needs_weights, multiquery=True)
266
+ return (self.out_proj(context), attn_weights, past_key_value)
267
+
268
+ def attn_bias_shape(attn_impl: str, n_heads: int, seq_len: int, alibi: bool, prefix_lm: bool, causal: bool, use_sequence_id: bool):
269
+ if attn_impl == 'flash':
270
+ return None
271
+ elif attn_impl in ['torch', 'triton']:
272
+ if alibi:
273
+ if (prefix_lm or not causal) or use_sequence_id:
274
+ return (1, n_heads, seq_len, seq_len)
275
+ return (1, n_heads, 1, seq_len)
276
+ elif prefix_lm or use_sequence_id:
277
+ return (1, 1, seq_len, seq_len)
278
+ return None
279
+ else:
280
+ raise ValueError(f'attn_impl={attn_impl!r} is an invalid setting.')
281
+
282
+ def build_attn_bias(attn_impl: str, attn_bias: torch.Tensor, n_heads: int, seq_len: int, causal: bool=False, alibi: bool=False, alibi_bias_max: int=8):
283
+ if attn_impl == 'flash':
284
+ return None
285
+ elif attn_impl in ['torch', 'triton']:
286
+ if alibi:
287
+ (device, dtype) = (attn_bias.device, attn_bias.dtype)
288
+ attn_bias = attn_bias.add(build_alibi_bias(n_heads, seq_len, full=not causal, alibi_bias_max=alibi_bias_max, device=device, dtype=dtype))
289
+ return attn_bias
290
+ else:
291
+ raise ValueError(f'attn_impl={attn_impl!r} is an invalid setting.')
292
+
293
+ def gen_slopes(n_heads: int, alibi_bias_max: int=8, device: Optional[torch.device]=None):
294
+ _n_heads = 2 ** math.ceil(math.log2(n_heads))
295
+ m = torch.arange(1, _n_heads + 1, dtype=torch.float32, device=device)
296
+ m = m.mul(alibi_bias_max / _n_heads)
297
+ slopes = 1.0 / torch.pow(2, m)
298
+ if _n_heads != n_heads:
299
+ slopes = torch.concat([slopes[1::2], slopes[::2]])[:n_heads]
300
+ return slopes.view(1, n_heads, 1, 1)
301
+
302
+ def build_alibi_bias(n_heads: int, seq_len: int, full: bool=False, alibi_bias_max: int=8, device: Optional[torch.device]=None, dtype: Optional[torch.dtype]=None):
303
+ alibi_bias = torch.arange(1 - seq_len, 1, dtype=torch.int32, device=device).view(1, 1, 1, seq_len)
304
+ if full:
305
+ alibi_bias = alibi_bias - torch.arange(1 - seq_len, 1, dtype=torch.int32, device=device).view(1, 1, seq_len, 1)
306
+ alibi_bias = alibi_bias.abs().mul(-1)
307
+ slopes = gen_slopes(n_heads, alibi_bias_max, device=device)
308
+ alibi_bias = alibi_bias * slopes
309
+ return alibi_bias.to(dtype=dtype)
310
+ ATTN_CLASS_REGISTRY = {'multihead_attention': MultiheadAttention, 'multiquery_attention': MultiQueryAttention}
hf_model/blocks.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """GPT Blocks used for the GPT Model."""
2
+ from typing import Any, Dict, Optional, Tuple
3
+ import torch
4
+ import torch.nn as nn
5
+ from .attention import ATTN_CLASS_REGISTRY
6
+ from .ffn import FFN_CLASS_REGISTRY, build_ffn
7
+ from .norm import NORM_CLASS_REGISTRY
8
+
9
+ class MPTBlock(nn.Module):
10
+
11
+ def __init__(self, d_model: int, n_heads: int, expansion_ratio: int, attn_config: Optional[Dict]=None, ffn_config: Optional[Dict]=None, resid_pdrop: float=0.0, norm_type: str='low_precision_layernorm', verbose: int=0, fc_type: str='torch', device: Optional[str]=None, **kwargs: Any):
12
+ if attn_config is None:
13
+ attn_config = {'attn_type': 'multihead_attention', 'attn_pdrop': 0.0, 'attn_impl': 'triton', 'qk_ln': False, 'clip_qkv': None, 'softmax_scale': None, 'prefix_lm': False, 'attn_uses_sequence_id': False, 'alibi': False, 'alibi_bias_max': 8}
14
+ if ffn_config is None:
15
+ ffn_config = {'ffn_type': 'mptmlp'}
16
+ del kwargs
17
+ super().__init__()
18
+ norm_class = NORM_CLASS_REGISTRY[norm_type.lower()]
19
+ assert isinstance(attn_config['attn_type'], str)
20
+ attn_class = ATTN_CLASS_REGISTRY[attn_config['attn_type']]
21
+ self.norm_1 = norm_class(d_model, device=device)
22
+ self.attn = attn_class(d_model=d_model, n_heads=n_heads, attn_impl=attn_config['attn_impl'], clip_qkv=attn_config['clip_qkv'], qk_ln=attn_config['qk_ln'], softmax_scale=attn_config['softmax_scale'], attn_pdrop=attn_config['attn_pdrop'], norm_type=norm_type, fc_type=fc_type, verbose=verbose, device=device)
23
+ self.norm_2 = None
24
+ if not getattr(FFN_CLASS_REGISTRY[ffn_config['ffn_type']], '_has_norm', False):
25
+ self.norm_2 = norm_class(d_model, device=device)
26
+ self.ffn = build_ffn(d_model=d_model, expansion_ratio=expansion_ratio, device=device, **ffn_config)
27
+ self.resid_attn_dropout = nn.Dropout(resid_pdrop)
28
+ self.resid_ffn_dropout = nn.Dropout(resid_pdrop)
29
+
30
+ def forward(self, x: torch.Tensor, past_key_value: Optional[Tuple[torch.Tensor, torch.Tensor]]=None, attn_bias: Optional[torch.Tensor]=None, attention_mask: Optional[torch.ByteTensor]=None, is_causal: bool=True) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor, torch.Tensor]]]:
31
+ a = self.norm_1(x)
32
+ (b, attn_weights, past_key_value) = self.attn(a, past_key_value=past_key_value, attn_bias=attn_bias, attention_mask=attention_mask, is_causal=is_causal)
33
+ x = x + self.resid_attn_dropout(b)
34
+ m = x
35
+ if self.norm_2 is not None:
36
+ m = self.norm_2(x)
37
+ n = self.ffn(m)
38
+ x = x + self.resid_ffn_dropout(n)
39
+ return (x, attn_weights, past_key_value)
hf_model/config.json ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "MPTForCausalLM"
4
+ ],
5
+ "attn_config": {
6
+ "alibi": true,
7
+ "alibi_bias_max": 8,
8
+ "attn_impl": "torch",
9
+ "attn_pdrop": 0,
10
+ "attn_type": "multihead_attention",
11
+ "attn_uses_sequence_id": false,
12
+ "clip_qkv": null,
13
+ "prefix_lm": false,
14
+ "qk_ln": false,
15
+ "softmax_scale": null
16
+ },
17
+ "auto_map": {
18
+ "AutoConfig": "configuration_mpt.MPTConfig",
19
+ "AutoModelForCausalLM": "modeling_mpt.MPTForCausalLM"
20
+ },
21
+ "d_model": 2048,
22
+ "emb_pdrop": 0,
23
+ "embedding_fraction": 1.0,
24
+ "expansion_ratio": 4,
25
+ "fc_type": "torch",
26
+ "ffn_config": {
27
+ "fc_type": "torch",
28
+ "ffn_type": "mptmlp"
29
+ },
30
+ "init_config": {
31
+ "emb_init_std": null,
32
+ "emb_init_uniform_lim": null,
33
+ "fan_mode": "fan_in",
34
+ "init_div_is_residual": true,
35
+ "init_gain": 0.0,
36
+ "init_nonlinearity": "relu",
37
+ "init_std": null,
38
+ "name": "kaiming_normal_",
39
+ "verbose": 0
40
+ },
41
+ "init_device": "cpu",
42
+ "learned_pos_emb": false,
43
+ "logit_scale": null,
44
+ "max_seq_len": 8192,
45
+ "model_type": "mpt",
46
+ "n_heads": 16,
47
+ "n_layers": 24,
48
+ "no_bias": true,
49
+ "norm_type": "low_precision_layernorm",
50
+ "resid_pdrop": 0,
51
+ "torch_dtype": "bfloat16",
52
+ "transformers_version": "4.30.2",
53
+ "use_cache": false,
54
+ "verbose": 0,
55
+ "vocab_size": 50432
56
+ }
hf_model/configuration_mpt.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """A HuggingFace-style model configuration."""
2
+ import warnings
3
+ from typing import Any, Dict, Optional, Union
4
+ from transformers import PretrainedConfig
5
+ attn_config_defaults: Dict = {'attn_type': 'multihead_attention', 'attn_pdrop': 0.0, 'attn_impl': 'triton', 'qk_ln': False, 'clip_qkv': None, 'softmax_scale': None, 'prefix_lm': False, 'attn_uses_sequence_id': False, 'alibi': False, 'alibi_bias_max': 8}
6
+ ffn_config_defaults: Dict = {'ffn_type': 'mptmlp'}
7
+ init_config_defaults: Dict = {'name': 'kaiming_normal_', 'fan_mode': 'fan_in', 'init_nonlinearity': 'relu', 'init_div_is_residual': True, 'emb_init_std': None, 'emb_init_uniform_lim': None, 'init_std': None, 'init_gain': 0.0}
8
+
9
+ class MPTConfig(PretrainedConfig):
10
+ model_type = 'mpt'
11
+
12
+ def __init__(self, d_model: int=2048, n_heads: int=16, n_layers: int=24, expansion_ratio: int=4, max_seq_len: int=2048, vocab_size: int=50368, resid_pdrop: float=0.0, emb_pdrop: float=0.0, learned_pos_emb: bool=True, attn_config: Dict=attn_config_defaults, ffn_config: Dict=ffn_config_defaults, init_device: str='cpu', logit_scale: Optional[Union[float, str]]=None, no_bias: bool=False, verbose: int=0, embedding_fraction: float=1.0, norm_type: str='low_precision_layernorm', use_cache: bool=False, init_config: Dict=init_config_defaults, fc_type: str='torch', **kwargs: Any):
13
+ """The MPT configuration class.
14
+
15
+ Args:
16
+ d_model (int): The size of the embedding dimension of the model.
17
+ n_heads (int): The number of attention heads.
18
+ n_layers (int): The number of layers in the model.
19
+ expansion_ratio (int): The ratio of the up/down scale in the ffn.
20
+ max_seq_len (int): The maximum sequence length of the model.
21
+ vocab_size (int): The size of the vocabulary.
22
+ resid_pdrop (float): The dropout probability applied to the attention output before combining with residual.
23
+ emb_pdrop (float): The dropout probability for the embedding layer.
24
+ learned_pos_emb (bool): Whether to use learned positional embeddings
25
+ attn_config (Dict): A dictionary used to configure the model's attention module:
26
+ attn_type (str): type of attention to use. Options: multihead_attention, multiquery_attention
27
+ attn_pdrop (float): The dropout probability for the attention layers.
28
+ attn_impl (str): The attention implementation to use. One of 'torch', 'flash', or 'triton'.
29
+ qk_ln (bool): Whether to apply layer normalization to the queries and keys in the attention layer.
30
+ clip_qkv (Optional[float]): If not None, clip the queries, keys, and values in the attention layer to
31
+ this value.
32
+ softmax_scale (Optional[float]): If not None, scale the softmax in the attention layer by this value. If None,
33
+ use the default scale of ``1/sqrt(d_keys)``.
34
+ prefix_lm (Optional[bool]): Whether the model should operate as a Prefix LM. This requires passing an
35
+ extra `prefix_mask` argument which indicates which tokens belong to the prefix. Tokens in the prefix
36
+ can attend to one another bi-directionally. Tokens outside the prefix use causal attention.
37
+ attn_uses_sequence_id (Optional[bool]): Whether to restrict attention to tokens that have the same sequence_id.
38
+ When the model is in `train` mode, this requires passing an extra `sequence_id` argument which indicates
39
+ which sub-sequence each token belongs to.
40
+ Defaults to ``False`` meaning any provided `sequence_id` will be ignored.
41
+ alibi (bool): Whether to use the alibi bias instead of position embeddings.
42
+ alibi_bias_max (int): The maximum value of the alibi bias.
43
+ ffn_config (Dict): A dictionary used to configure the model's ffn module:
44
+ ffn_type (str): type of ffn to use. Options: mptmlp, te_ln_mlp
45
+ init_device (str): The device to use for parameter initialization.
46
+ logit_scale (Optional[Union[float, str]]): If not None, scale the logits by this value.
47
+ no_bias (bool): Whether to use bias in all layers.
48
+ verbose (int): The verbosity level. 0 is silent.
49
+ embedding_fraction (float): The fraction to scale the gradients of the embedding layer by.
50
+ norm_type (str): choose type of norm to use
51
+ multiquery_attention (bool): Whether to use multiquery attention implementation.
52
+ use_cache (bool): Whether or not the model should return the last key/values attentions
53
+ init_config (Dict): A dictionary used to configure the model initialization:
54
+ init_config.name: The parameter initialization scheme to use. Options: 'default_', 'baseline_',
55
+ 'kaiming_uniform_', 'kaiming_normal_', 'neox_init_', 'small_init_', 'xavier_uniform_', or
56
+ 'xavier_normal_'. These mimic the parameter initialization methods in PyTorch.
57
+ init_div_is_residual (Union[int, float, str, bool]): Value to divide initial weights by if ``module._is_residual`` is True.
58
+ emb_init_std (Optional[float]): The standard deviation of the normal distribution used to initialize the embedding layer.
59
+ emb_init_uniform_lim (Optional[Union[Tuple[float, float], float]]): The lower and upper limits of the uniform distribution
60
+ used to initialize the embedding layer. Mutually exclusive with ``emb_init_std``.
61
+ init_std (float): The standard deviation of the normal distribution used to initialize the model,
62
+ if using the baseline_ parameter initialization scheme.
63
+ init_gain (float): The gain to use for parameter initialization with kaiming or xavier initialization schemes.
64
+ fan_mode (str): The fan mode to use for parameter initialization with kaiming initialization schemes.
65
+ init_nonlinearity (str): The nonlinearity to use for parameter initialization with kaiming initialization schemes.
66
+ ---
67
+ See llmfoundry.models.utils.param_init_fns.py for info on other param init config options
68
+ fc_type (str): choose fc layer implementation. Options: torch and te. te layers support fp8 when using H100 GPUs.
69
+ """
70
+ self.d_model = d_model
71
+ self.n_heads = n_heads
72
+ self.n_layers = n_layers
73
+ self.expansion_ratio = expansion_ratio
74
+ self.max_seq_len = max_seq_len
75
+ self.vocab_size = vocab_size
76
+ self.resid_pdrop = resid_pdrop
77
+ self.emb_pdrop = emb_pdrop
78
+ self.learned_pos_emb = learned_pos_emb
79
+ self.attn_config = attn_config
80
+ self.ffn_config = ffn_config
81
+ self.init_device = init_device
82
+ self.logit_scale = logit_scale
83
+ self.no_bias = no_bias
84
+ self.verbose = verbose
85
+ self.embedding_fraction = embedding_fraction
86
+ self.norm_type = norm_type
87
+ self.use_cache = use_cache
88
+ self.init_config = init_config
89
+ self.fc_type = fc_type
90
+ if 'name' in kwargs:
91
+ del kwargs['name']
92
+ if 'loss_fn' in kwargs:
93
+ del kwargs['loss_fn']
94
+ if self.attn_config.get('alibi', False):
95
+ self.learned_pos_emb = False
96
+ warnings.warn(f'alibi is turned on, setting `learned_pos_emb` to `False.`')
97
+ super().__init__(**kwargs)
98
+ self._validate_config()
99
+
100
+ def _set_config_defaults(self, config: Dict[str, Any], config_defaults: Dict[str, Any]):
101
+ for (k, v) in config_defaults.items():
102
+ if k not in config:
103
+ config[k] = v
104
+ return config
105
+
106
+ def _validate_config(self):
107
+ self.attn_config = self._set_config_defaults(self.attn_config, attn_config_defaults)
108
+ self.ffn_config = self._set_config_defaults(self.ffn_config, ffn_config_defaults)
109
+ self.init_config = self._set_config_defaults(self.init_config, init_config_defaults)
110
+ if self.d_model % self.n_heads != 0:
111
+ raise ValueError('d_model must be divisible by n_heads')
112
+ if any((prob < 0 or prob > 1 for prob in [self.attn_config['attn_pdrop'], self.resid_pdrop, self.emb_pdrop])):
113
+ raise ValueError("self.attn_config['attn_pdrop'], resid_pdrop, emb_pdrop are probabilities and must be between 0 and 1")
114
+ if self.attn_config['attn_impl'] not in ['torch', 'flash', 'triton']:
115
+ raise ValueError(f"Unknown attn_impl={self.attn_config['attn_impl']}")
116
+ if self.attn_config['prefix_lm'] and self.attn_config['attn_impl'] not in ['torch', 'triton']:
117
+ raise NotImplementedError('prefix_lm only implemented with torch and triton attention.')
118
+ if self.attn_config['alibi'] and self.attn_config['attn_impl'] not in ['torch', 'triton']:
119
+ raise NotImplementedError('alibi only implemented with torch and triton attention.')
120
+ if self.attn_config['attn_uses_sequence_id'] and self.attn_config['attn_impl'] not in ['torch', 'triton']:
121
+ raise NotImplementedError('attn_uses_sequence_id only implemented with torch and triton attention.')
122
+ if self.embedding_fraction > 1 or self.embedding_fraction <= 0:
123
+ raise ValueError('model.embedding_fraction must be between 0 (exclusive) and 1 (inclusive)!')
124
+ if isinstance(self.logit_scale, str) and self.logit_scale != 'inv_sqrt_d_model':
125
+ raise ValueError(f"self.logit_scale={self.logit_scale!r} is not recognized as an option; use numeric value or 'inv_sqrt_d_model'.")
126
+ if self.init_config.get('name', None) is None:
127
+ raise ValueError(f"self.init_config={self.init_config!r} 'name' needs to be set.")
128
+ if not self.learned_pos_emb and (not self.attn_config['alibi']):
129
+ warnings.warn(f'Positional information not being provided to the model using either learned_pos_emb or alibi.')
130
+ if self.fc_type == 'te' or self.ffn_config['ffn_type'] == 'te_ln_mlp':
131
+ try:
132
+ import transformer_engine.pytorch as te
133
+ del te
134
+ except:
135
+ raise ImportError('TransformerEngine import fail. `fc_type: te` requires TransformerEngine be installed. ' + 'The required version of transformer_engine also requires FlashAttention v1.0.6 is installed:\n' + 'pip install flash-attn==1.0.6 --no-build-isolation \n' + 'pip install git+https://github.com/NVIDIA/TransformerEngine.git@144e4888b2cdd60bd52e706d5b7a79cb9c1a7156')
136
+ if self.ffn_config['ffn_type'] == 'mptmlp':
137
+ self.ffn_config['fc_type'] = self.fc_type
138
+ elif self.ffn_config['ffn_type'] == 'te_ln_mlp':
139
+ self.ffn_config['bias'] = not self.no_bias
hf_model/custom_embedding.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch.nn as nn
2
+ import torch.nn.functional as F
3
+ from torch import Tensor
4
+
5
+ class SharedEmbedding(nn.Embedding):
6
+
7
+ def forward(self, input: Tensor, unembed: bool=False) -> Tensor:
8
+ if unembed:
9
+ return F.linear(input, self.weight)
10
+ return super().forward(input)
hf_model/fc.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from torch import nn
2
+ FC_CLASS_REGISTRY = {'torch': nn.Linear}
3
+ try:
4
+ import transformer_engine.pytorch as te
5
+ FC_CLASS_REGISTRY['te'] = te.Linear
6
+ except:
7
+ pass
hf_model/ffn.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """GPT Blocks used for the GPT Model."""
2
+ from typing import Any, Optional
3
+ import torch
4
+ import torch.nn as nn
5
+ from .fc import FC_CLASS_REGISTRY
6
+ try:
7
+ import transformer_engine.pytorch as te
8
+ except:
9
+ te = None
10
+
11
+ class MPTMLP(nn.Module):
12
+
13
+ def __init__(self, d_model: int, expansion_ratio: int, fc_type: str='torch', device: Optional[str]=None):
14
+ super().__init__()
15
+ fc_kwargs = {}
16
+ if fc_type != 'te':
17
+ fc_kwargs['device'] = device
18
+ self.up_proj = FC_CLASS_REGISTRY[fc_type](d_model, expansion_ratio * d_model, **fc_kwargs)
19
+ self.act = nn.GELU(approximate='none')
20
+ self.down_proj = FC_CLASS_REGISTRY[fc_type](expansion_ratio * d_model, d_model, **fc_kwargs)
21
+ self.down_proj._is_residual = True
22
+
23
+ def forward(self, x: torch.Tensor):
24
+ return self.down_proj(self.act(self.up_proj(x)))
25
+ FFN_CLASS_REGISTRY = {'mptmlp': MPTMLP}
26
+ if te is not None:
27
+ te.LayerNormMLP._has_norm = True
28
+ FFN_CLASS_REGISTRY['te_ln_mlp'] = te.LayerNormMLP
29
+
30
+ def build_ffn(d_model: int, expansion_ratio: int, fc_type: str='torch', device: Optional[str]=None, **kwargs: Any):
31
+ ffn_type = kwargs.pop('ffn_type')
32
+ if ffn_type == 'mptmlp':
33
+ if len(kwargs) > 0:
34
+ raise ValueError(f'MPTMLP got an unexpected keyword argument: {kwargs}')
35
+ return MPTMLP(d_model=d_model, expansion_ratio=expansion_ratio, fc_type=fc_type, device=device)
36
+ elif ffn_type == 'te_ln_mlp':
37
+ assert te is not None
38
+ return te.LayerNormMLP(hidden_size=d_model, ffn_hidden_size=d_model * expansion_ratio, **kwargs)
39
+ raise ValueError(f'ffn_type={ffn_type!r} not recognized.')
hf_model/flash_attn_triton.py ADDED
@@ -0,0 +1,484 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Copied from https://github.com/HazyResearch/flash-attention/blob/eff9fe6b8076df59d64d7a3f464696738a3c7c24/flash_attn/flash_attn_triton.py
3
+ update imports to use 'triton_pre_mlir'
4
+
5
+ *Experimental* implementation of FlashAttention in Triton.
6
+ Tested with triton==2.0.0.dev20221202.
7
+ Triton 2.0 has a new backend (MLIR) but seems like it doesn't yet work for head dimensions
8
+ other than 64:
9
+ https://github.com/openai/triton/blob/d376020f90002757eea3ea9475d4f7cfc2ec5ead/python/triton/ops/flash_attention.py#L207
10
+ We'll update this implementation with the new Triton backend once this is fixed.
11
+
12
+ We use the FlashAttention implementation from Phil Tillet a starting point.
13
+ https://github.com/openai/triton/blob/master/python/tutorials/06-fused-attention.py
14
+
15
+ Changes:
16
+ - Implement both causal and non-causal attention.
17
+ - Implement both self-attention and cross-attention.
18
+ - Support arbitrary seqlens (not just multiples of 128), for both forward and backward.
19
+ - Support all head dimensions up to 128 (not just 16, 32, 64, 128), for both forward and backward.
20
+ - Support attention bias.
21
+ - Speed up the forward pass a bit, and only store the LSE instead of m and l.
22
+ - Make the backward for d=128 much faster by reducing register spilling.
23
+ - Optionally parallelize the backward pass across seqlen_k, to deal with the case of
24
+ small batch size * nheads.
25
+
26
+ Caution:
27
+ - This is an *experimental* implementation. The forward pass should be quite robust but
28
+ I'm not 100% sure that the backward pass doesn't have race conditions (due to the Triton compiler).
29
+ - This implementation has only been tested on A100.
30
+ - If you plan to use headdim other than 64 and 128, you should test for race conditions
31
+ (due to the Triton compiler), as done in tests/test_flash_attn.py
32
+ "test_flash_attn_triton_race_condition". I've tested and fixed many race conditions
33
+ for different head dimensions (40, 48, 64, 128, 80, 88, 96), but I'm still not 100% confident
34
+ that there are none left for other head dimensions.
35
+
36
+ Differences between this Triton version and the CUDA version:
37
+ - Triton version doesn't support dropout.
38
+ - Triton forward is generally faster than CUDA forward, while Triton backward is
39
+ generally slower than CUDA backward. Overall Triton forward + backward is slightly slower
40
+ than CUDA forward + backward.
41
+ - Triton version doesn't support different sequence lengths in a batch (i.e., RaggedTensor/NestedTensor).
42
+ - Triton version supports attention bias, while CUDA version doesn't.
43
+ """
44
+ import math
45
+ import torch
46
+ import triton_pre_mlir as triton
47
+ import triton_pre_mlir.language as tl
48
+
49
+ @triton.heuristics({'EVEN_M': lambda args: args['seqlen_q'] % args['BLOCK_M'] == 0, 'EVEN_N': lambda args: args['seqlen_k'] % args['BLOCK_N'] == 0, 'EVEN_HEADDIM': lambda args: args['headdim'] == args['BLOCK_HEADDIM']})
50
+ @triton.jit
51
+ def _fwd_kernel(Q, K, V, Bias, Out, Lse, TMP, softmax_scale, stride_qb, stride_qh, stride_qm, stride_kb, stride_kh, stride_kn, stride_vb, stride_vh, stride_vn, stride_bb, stride_bh, stride_bm, stride_ob, stride_oh, stride_om, nheads, seqlen_q, seqlen_k, seqlen_q_rounded, headdim, CACHE_KEY_SEQLEN_Q, CACHE_KEY_SEQLEN_K, BIAS_TYPE: tl.constexpr, IS_CAUSAL: tl.constexpr, BLOCK_HEADDIM: tl.constexpr, EVEN_M: tl.constexpr, EVEN_N: tl.constexpr, EVEN_HEADDIM: tl.constexpr, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr):
52
+ start_m = tl.program_id(0)
53
+ off_hb = tl.program_id(1)
54
+ off_b = off_hb // nheads
55
+ off_h = off_hb % nheads
56
+ offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M)
57
+ offs_n = tl.arange(0, BLOCK_N)
58
+ offs_d = tl.arange(0, BLOCK_HEADDIM)
59
+ q_ptrs = Q + off_b * stride_qb + off_h * stride_qh + (offs_m[:, None] * stride_qm + offs_d[None, :])
60
+ k_ptrs = K + off_b * stride_kb + off_h * stride_kh + (offs_n[:, None] * stride_kn + offs_d[None, :])
61
+ v_ptrs = V + off_b * stride_vb + off_h * stride_vh + (offs_n[:, None] * stride_vn + offs_d[None, :])
62
+ if BIAS_TYPE == 'vector':
63
+ b_ptrs = Bias + off_b * stride_bb + off_h * stride_bh + offs_n
64
+ elif BIAS_TYPE == 'matrix':
65
+ b_ptrs = Bias + off_b * stride_bb + off_h * stride_bh + (offs_m[:, None] * stride_bm + offs_n[None, :])
66
+ t_ptrs = TMP + off_hb * seqlen_q_rounded + offs_m
67
+ lse_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float('inf')
68
+ m_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float('inf')
69
+ acc_o = tl.zeros([BLOCK_M, BLOCK_HEADDIM], dtype=tl.float32)
70
+ if EVEN_M & EVEN_N:
71
+ if EVEN_HEADDIM:
72
+ q = tl.load(q_ptrs)
73
+ else:
74
+ q = tl.load(q_ptrs, mask=offs_d[None, :] < headdim, other=0.0)
75
+ elif EVEN_HEADDIM:
76
+ q = tl.load(q_ptrs, mask=offs_m[:, None] < seqlen_q, other=0.0)
77
+ else:
78
+ q = tl.load(q_ptrs, mask=(offs_m[:, None] < seqlen_q) & (offs_d[None, :] < headdim), other=0.0)
79
+ end_n = seqlen_k if not IS_CAUSAL else tl.minimum((start_m + 1) * BLOCK_M, seqlen_k)
80
+ for start_n in range(0, end_n, BLOCK_N):
81
+ start_n = tl.multiple_of(start_n, BLOCK_N)
82
+ if EVEN_N & EVEN_M:
83
+ if EVEN_HEADDIM:
84
+ k = tl.load(k_ptrs + start_n * stride_kn)
85
+ else:
86
+ k = tl.load(k_ptrs + start_n * stride_kn, mask=offs_d[None, :] < headdim, other=0.0)
87
+ elif EVEN_HEADDIM:
88
+ k = tl.load(k_ptrs + start_n * stride_kn, mask=(start_n + offs_n)[:, None] < seqlen_k, other=0.0)
89
+ else:
90
+ k = tl.load(k_ptrs + start_n * stride_kn, mask=((start_n + offs_n)[:, None] < seqlen_k) & (offs_d[None, :] < headdim), other=0.0)
91
+ qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32)
92
+ qk += tl.dot(q, k, trans_b=True)
93
+ if not EVEN_N:
94
+ qk += tl.where((start_n + offs_n)[None, :] < seqlen_k, 0, float('-inf'))
95
+ if IS_CAUSAL:
96
+ qk += tl.where(offs_m[:, None] >= (start_n + offs_n)[None, :], 0, float('-inf'))
97
+ if BIAS_TYPE != 'none':
98
+ if BIAS_TYPE == 'vector':
99
+ if EVEN_N:
100
+ bias = tl.load(b_ptrs + start_n).to(tl.float32)
101
+ else:
102
+ bias = tl.load(b_ptrs + start_n, mask=start_n + offs_n < seqlen_k, other=0.0).to(tl.float32)
103
+ bias = bias[None, :]
104
+ elif BIAS_TYPE == 'matrix':
105
+ if EVEN_M & EVEN_N:
106
+ bias = tl.load(b_ptrs + start_n).to(tl.float32)
107
+ else:
108
+ bias = tl.load(b_ptrs + start_n, mask=(offs_m[:, None] < seqlen_q) & ((start_n + offs_n)[None, :] < seqlen_k), other=0.0).to(tl.float32)
109
+ qk = qk * softmax_scale + bias
110
+ m_ij = tl.maximum(tl.max(qk, 1), lse_i)
111
+ p = tl.exp(qk - m_ij[:, None])
112
+ else:
113
+ m_ij = tl.maximum(tl.max(qk, 1) * softmax_scale, lse_i)
114
+ p = tl.exp(qk * softmax_scale - m_ij[:, None])
115
+ l_ij = tl.sum(p, 1)
116
+ acc_o_scale = tl.exp(m_i - m_ij)
117
+ tl.store(t_ptrs, acc_o_scale)
118
+ acc_o_scale = tl.load(t_ptrs)
119
+ acc_o = acc_o * acc_o_scale[:, None]
120
+ if EVEN_N & EVEN_M:
121
+ if EVEN_HEADDIM:
122
+ v = tl.load(v_ptrs + start_n * stride_vn)
123
+ else:
124
+ v = tl.load(v_ptrs + start_n * stride_vn, mask=offs_d[None, :] < headdim, other=0.0)
125
+ elif EVEN_HEADDIM:
126
+ v = tl.load(v_ptrs + start_n * stride_vn, mask=(start_n + offs_n)[:, None] < seqlen_k, other=0.0)
127
+ else:
128
+ v = tl.load(v_ptrs + start_n * stride_vn, mask=((start_n + offs_n)[:, None] < seqlen_k) & (offs_d[None, :] < headdim), other=0.0)
129
+ p = p.to(v.dtype)
130
+ acc_o += tl.dot(p, v)
131
+ m_i = m_ij
132
+ l_i_new = tl.exp(lse_i - m_ij) + l_ij
133
+ lse_i = m_ij + tl.log(l_i_new)
134
+ o_scale = tl.exp(m_i - lse_i)
135
+ tl.store(t_ptrs, o_scale)
136
+ o_scale = tl.load(t_ptrs)
137
+ acc_o = acc_o * o_scale[:, None]
138
+ start_m = tl.program_id(0)
139
+ offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M)
140
+ lse_ptrs = Lse + off_hb * seqlen_q_rounded + offs_m
141
+ tl.store(lse_ptrs, lse_i)
142
+ offs_d = tl.arange(0, BLOCK_HEADDIM)
143
+ out_ptrs = Out + off_b * stride_ob + off_h * stride_oh + (offs_m[:, None] * stride_om + offs_d[None, :])
144
+ if EVEN_M:
145
+ if EVEN_HEADDIM:
146
+ tl.store(out_ptrs, acc_o)
147
+ else:
148
+ tl.store(out_ptrs, acc_o, mask=offs_d[None, :] < headdim)
149
+ elif EVEN_HEADDIM:
150
+ tl.store(out_ptrs, acc_o, mask=offs_m[:, None] < seqlen_q)
151
+ else:
152
+ tl.store(out_ptrs, acc_o, mask=(offs_m[:, None] < seqlen_q) & (offs_d[None, :] < headdim))
153
+
154
+ @triton.jit
155
+ def _bwd_preprocess_do_o_dot(Out, DO, Delta, stride_ob, stride_oh, stride_om, stride_dob, stride_doh, stride_dom, nheads, seqlen_q, seqlen_q_rounded, headdim, BLOCK_M: tl.constexpr, BLOCK_HEADDIM: tl.constexpr):
156
+ start_m = tl.program_id(0)
157
+ off_hb = tl.program_id(1)
158
+ off_b = off_hb // nheads
159
+ off_h = off_hb % nheads
160
+ offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M)
161
+ offs_d = tl.arange(0, BLOCK_HEADDIM)
162
+ o = tl.load(Out + off_b * stride_ob + off_h * stride_oh + offs_m[:, None] * stride_om + offs_d[None, :], mask=(offs_m[:, None] < seqlen_q) & (offs_d[None, :] < headdim), other=0.0).to(tl.float32)
163
+ do = tl.load(DO + off_b * stride_dob + off_h * stride_doh + offs_m[:, None] * stride_dom + offs_d[None, :], mask=(offs_m[:, None] < seqlen_q) & (offs_d[None, :] < headdim), other=0.0).to(tl.float32)
164
+ delta = tl.sum(o * do, axis=1)
165
+ tl.store(Delta + off_hb * seqlen_q_rounded + offs_m, delta)
166
+
167
+ @triton.jit
168
+ def _bwd_store_dk_dv(dk_ptrs, dv_ptrs, dk, dv, offs_n, offs_d, seqlen_k, headdim, EVEN_M: tl.constexpr, EVEN_N: tl.constexpr, EVEN_HEADDIM: tl.constexpr):
169
+ if EVEN_N & EVEN_M:
170
+ if EVEN_HEADDIM:
171
+ tl.store(dv_ptrs, dv)
172
+ tl.store(dk_ptrs, dk)
173
+ else:
174
+ tl.store(dv_ptrs, dv, mask=offs_d[None, :] < headdim)
175
+ tl.store(dk_ptrs, dk, mask=offs_d[None, :] < headdim)
176
+ elif EVEN_HEADDIM:
177
+ tl.store(dv_ptrs, dv, mask=offs_n[:, None] < seqlen_k)
178
+ tl.store(dk_ptrs, dk, mask=offs_n[:, None] < seqlen_k)
179
+ else:
180
+ tl.store(dv_ptrs, dv, mask=(offs_n[:, None] < seqlen_k) & (offs_d[None, :] < headdim))
181
+ tl.store(dk_ptrs, dk, mask=(offs_n[:, None] < seqlen_k) & (offs_d[None, :] < headdim))
182
+
183
+ @triton.jit
184
+ def _bwd_kernel_one_col_block(start_n, Q, K, V, Bias, DO, DQ, DK, DV, LSE, D, softmax_scale, stride_qm, stride_kn, stride_vn, stride_bm, stride_dom, stride_dqm, stride_dkn, stride_dvn, seqlen_q, seqlen_k, headdim, ATOMIC_ADD: tl.constexpr, BIAS_TYPE: tl.constexpr, IS_CAUSAL: tl.constexpr, BLOCK_HEADDIM: tl.constexpr, EVEN_M: tl.constexpr, EVEN_N: tl.constexpr, EVEN_HEADDIM: tl.constexpr, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr):
185
+ begin_m = 0 if not IS_CAUSAL else start_n * BLOCK_N // BLOCK_M * BLOCK_M
186
+ offs_qm = begin_m + tl.arange(0, BLOCK_M)
187
+ offs_n = start_n * BLOCK_N + tl.arange(0, BLOCK_N)
188
+ offs_m = tl.arange(0, BLOCK_M)
189
+ offs_d = tl.arange(0, BLOCK_HEADDIM)
190
+ q_ptrs = Q + (offs_qm[:, None] * stride_qm + offs_d[None, :])
191
+ k_ptrs = K + (offs_n[:, None] * stride_kn + offs_d[None, :])
192
+ v_ptrs = V + (offs_n[:, None] * stride_vn + offs_d[None, :])
193
+ do_ptrs = DO + (offs_qm[:, None] * stride_dom + offs_d[None, :])
194
+ dq_ptrs = DQ + (offs_qm[:, None] * stride_dqm + offs_d[None, :])
195
+ if BIAS_TYPE == 'vector':
196
+ b_ptrs = Bias + offs_n
197
+ elif BIAS_TYPE == 'matrix':
198
+ b_ptrs = Bias + (offs_qm[:, None] * stride_bm + offs_n[None, :])
199
+ dv = tl.zeros([BLOCK_N, BLOCK_HEADDIM], dtype=tl.float32)
200
+ dk = tl.zeros([BLOCK_N, BLOCK_HEADDIM], dtype=tl.float32)
201
+ if begin_m >= seqlen_q:
202
+ dv_ptrs = DV + (offs_n[:, None] * stride_dvn + offs_d[None, :])
203
+ dk_ptrs = DK + (offs_n[:, None] * stride_dkn + offs_d[None, :])
204
+ _bwd_store_dk_dv(dk_ptrs, dv_ptrs, dk, dv, offs_n, offs_d, seqlen_k, headdim, EVEN_M=EVEN_M, EVEN_N=EVEN_N, EVEN_HEADDIM=EVEN_HEADDIM)
205
+ return
206
+ if EVEN_N & EVEN_M:
207
+ if EVEN_HEADDIM:
208
+ k = tl.load(k_ptrs)
209
+ v = tl.load(v_ptrs)
210
+ else:
211
+ k = tl.load(k_ptrs, mask=offs_d[None, :] < headdim, other=0.0)
212
+ v = tl.load(v_ptrs, mask=offs_d[None, :] < headdim, other=0.0)
213
+ elif EVEN_HEADDIM:
214
+ k = tl.load(k_ptrs, mask=offs_n[:, None] < seqlen_k, other=0.0)
215
+ v = tl.load(v_ptrs, mask=offs_n[:, None] < seqlen_k, other=0.0)
216
+ else:
217
+ k = tl.load(k_ptrs, mask=(offs_n[:, None] < seqlen_k) & (offs_d[None, :] < headdim), other=0.0)
218
+ v = tl.load(v_ptrs, mask=(offs_n[:, None] < seqlen_k) & (offs_d[None, :] < headdim), other=0.0)
219
+ num_block_m = tl.cdiv(seqlen_q, BLOCK_M)
220
+ for start_m in range(begin_m, num_block_m * BLOCK_M, BLOCK_M):
221
+ start_m = tl.multiple_of(start_m, BLOCK_M)
222
+ offs_m_curr = start_m + offs_m
223
+ if EVEN_M & EVEN_HEADDIM:
224
+ q = tl.load(q_ptrs)
225
+ elif EVEN_HEADDIM:
226
+ q = tl.load(q_ptrs, mask=offs_m_curr[:, None] < seqlen_q, other=0.0)
227
+ else:
228
+ q = tl.load(q_ptrs, mask=(offs_m_curr[:, None] < seqlen_q) & (offs_d[None, :] < headdim), other=0.0)
229
+ qk = tl.dot(q, k, trans_b=True)
230
+ if not EVEN_N:
231
+ qk = tl.where(offs_n[None, :] < seqlen_k, qk, float('-inf'))
232
+ if IS_CAUSAL:
233
+ qk = tl.where(offs_m_curr[:, None] >= offs_n[None, :], qk, float('-inf'))
234
+ if BIAS_TYPE != 'none':
235
+ tl.debug_barrier()
236
+ if BIAS_TYPE == 'vector':
237
+ if EVEN_N:
238
+ bias = tl.load(b_ptrs).to(tl.float32)
239
+ else:
240
+ bias = tl.load(b_ptrs, mask=offs_n < seqlen_k, other=0.0).to(tl.float32)
241
+ bias = bias[None, :]
242
+ elif BIAS_TYPE == 'matrix':
243
+ if EVEN_M & EVEN_N:
244
+ bias = tl.load(b_ptrs).to(tl.float32)
245
+ else:
246
+ bias = tl.load(b_ptrs, mask=(offs_m_curr[:, None] < seqlen_q) & (offs_n[None, :] < seqlen_k), other=0.0).to(tl.float32)
247
+ qk = qk * softmax_scale + bias
248
+ if not EVEN_M & EVEN_HEADDIM:
249
+ tl.debug_barrier()
250
+ lse_i = tl.load(LSE + offs_m_curr)
251
+ if BIAS_TYPE == 'none':
252
+ p = tl.exp(qk * softmax_scale - lse_i[:, None])
253
+ else:
254
+ p = tl.exp(qk - lse_i[:, None])
255
+ if EVEN_M & EVEN_HEADDIM:
256
+ do = tl.load(do_ptrs)
257
+ else:
258
+ do = tl.load(do_ptrs, mask=(offs_m_curr[:, None] < seqlen_q) & (offs_d[None, :] < headdim), other=0.0)
259
+ dv += tl.dot(p.to(do.dtype), do, trans_a=True)
260
+ if not EVEN_M & EVEN_HEADDIM:
261
+ tl.debug_barrier()
262
+ dp = tl.dot(do, v, trans_b=True)
263
+ if not EVEN_HEADDIM:
264
+ tl.debug_barrier()
265
+ Di = tl.load(D + offs_m_curr)
266
+ ds = (p * (dp - Di[:, None]) * softmax_scale).to(q.dtype)
267
+ dk += tl.dot(ds, q, trans_a=True)
268
+ if not EVEN_M & EVEN_HEADDIM:
269
+ tl.debug_barrier()
270
+ if not ATOMIC_ADD:
271
+ if EVEN_M & EVEN_HEADDIM:
272
+ dq = tl.load(dq_ptrs, eviction_policy='evict_last')
273
+ dq += tl.dot(ds, k)
274
+ tl.store(dq_ptrs, dq, eviction_policy='evict_last')
275
+ elif EVEN_HEADDIM:
276
+ dq = tl.load(dq_ptrs, mask=offs_m_curr[:, None] < seqlen_q, other=0.0, eviction_policy='evict_last')
277
+ dq += tl.dot(ds, k)
278
+ tl.store(dq_ptrs, dq, mask=offs_m_curr[:, None] < seqlen_q, eviction_policy='evict_last')
279
+ else:
280
+ dq = tl.load(dq_ptrs, mask=(offs_m_curr[:, None] < seqlen_q) & (offs_d[None, :] < headdim), other=0.0, eviction_policy='evict_last')
281
+ dq += tl.dot(ds, k)
282
+ tl.store(dq_ptrs, dq, mask=(offs_m_curr[:, None] < seqlen_q) & (offs_d[None, :] < headdim), eviction_policy='evict_last')
283
+ else:
284
+ dq = tl.dot(ds, k)
285
+ if EVEN_M & EVEN_HEADDIM:
286
+ tl.atomic_add(dq_ptrs, dq)
287
+ elif EVEN_HEADDIM:
288
+ tl.atomic_add(dq_ptrs, dq, mask=offs_m_curr[:, None] < seqlen_q)
289
+ else:
290
+ tl.atomic_add(dq_ptrs, dq, mask=(offs_m_curr[:, None] < seqlen_q) & (offs_d[None, :] < headdim))
291
+ dq_ptrs += BLOCK_M * stride_dqm
292
+ q_ptrs += BLOCK_M * stride_qm
293
+ do_ptrs += BLOCK_M * stride_dom
294
+ if BIAS_TYPE == 'matrix':
295
+ b_ptrs += BLOCK_M * stride_bm
296
+ dv_ptrs = DV + (offs_n[:, None] * stride_dvn + offs_d[None, :])
297
+ dk_ptrs = DK + (offs_n[:, None] * stride_dkn + offs_d[None, :])
298
+ _bwd_store_dk_dv(dk_ptrs, dv_ptrs, dk, dv, offs_n, offs_d, seqlen_k, headdim, EVEN_M=EVEN_M, EVEN_N=EVEN_N, EVEN_HEADDIM=EVEN_HEADDIM)
299
+
300
+ def init_to_zero(name):
301
+ return lambda nargs: nargs[name].zero_()
302
+
303
+ @triton.autotune(configs=[triton.Config({'BLOCK_M': 128, 'BLOCK_N': 128, 'SEQUENCE_PARALLEL': False}, num_warps=8, num_stages=1, pre_hook=init_to_zero('DQ')), triton.Config({'BLOCK_M': 128, 'BLOCK_N': 128, 'SEQUENCE_PARALLEL': True}, num_warps=8, num_stages=1, pre_hook=init_to_zero('DQ'))], key=['CACHE_KEY_SEQLEN_Q', 'CACHE_KEY_SEQLEN_K', 'BIAS_TYPE', 'IS_CAUSAL', 'BLOCK_HEADDIM'])
304
+ @triton.heuristics({'EVEN_M': lambda args: args['seqlen_q'] % args['BLOCK_M'] == 0, 'EVEN_N': lambda args: args['seqlen_k'] % args['BLOCK_N'] == 0, 'EVEN_HEADDIM': lambda args: args['headdim'] == args['BLOCK_HEADDIM']})
305
+ @triton.jit
306
+ def _bwd_kernel(Q, K, V, Bias, DO, DQ, DK, DV, LSE, D, softmax_scale, stride_qb, stride_qh, stride_qm, stride_kb, stride_kh, stride_kn, stride_vb, stride_vh, stride_vn, stride_bb, stride_bh, stride_bm, stride_dob, stride_doh, stride_dom, stride_dqb, stride_dqh, stride_dqm, stride_dkb, stride_dkh, stride_dkn, stride_dvb, stride_dvh, stride_dvn, nheads, seqlen_q, seqlen_k, seqlen_q_rounded, headdim, CACHE_KEY_SEQLEN_Q, CACHE_KEY_SEQLEN_K, BIAS_TYPE: tl.constexpr, IS_CAUSAL: tl.constexpr, BLOCK_HEADDIM: tl.constexpr, SEQUENCE_PARALLEL: tl.constexpr, EVEN_M: tl.constexpr, EVEN_N: tl.constexpr, EVEN_HEADDIM: tl.constexpr, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr):
307
+ off_hb = tl.program_id(1)
308
+ off_b = off_hb // nheads
309
+ off_h = off_hb % nheads
310
+ Q += off_b * stride_qb + off_h * stride_qh
311
+ K += off_b * stride_kb + off_h * stride_kh
312
+ V += off_b * stride_vb + off_h * stride_vh
313
+ DO += off_b * stride_dob + off_h * stride_doh
314
+ DQ += off_b * stride_dqb + off_h * stride_dqh
315
+ DK += off_b * stride_dkb + off_h * stride_dkh
316
+ DV += off_b * stride_dvb + off_h * stride_dvh
317
+ if BIAS_TYPE != 'none':
318
+ Bias += off_b * stride_bb + off_h * stride_bh
319
+ D += off_hb * seqlen_q_rounded
320
+ LSE += off_hb * seqlen_q_rounded
321
+ if not SEQUENCE_PARALLEL:
322
+ num_block_n = tl.cdiv(seqlen_k, BLOCK_N)
323
+ for start_n in range(0, num_block_n):
324
+ _bwd_kernel_one_col_block(start_n, Q, K, V, Bias, DO, DQ, DK, DV, LSE, D, softmax_scale, stride_qm, stride_kn, stride_vn, stride_bm, stride_dom, stride_dqm, stride_dkn, stride_dvn, seqlen_q, seqlen_k, headdim, ATOMIC_ADD=False, BIAS_TYPE=BIAS_TYPE, IS_CAUSAL=IS_CAUSAL, BLOCK_HEADDIM=BLOCK_HEADDIM, EVEN_M=EVEN_M, EVEN_N=EVEN_N, EVEN_HEADDIM=EVEN_HEADDIM, BLOCK_M=BLOCK_M, BLOCK_N=BLOCK_N)
325
+ else:
326
+ start_n = tl.program_id(0)
327
+ _bwd_kernel_one_col_block(start_n, Q, K, V, Bias, DO, DQ, DK, DV, LSE, D, softmax_scale, stride_qm, stride_kn, stride_vn, stride_bm, stride_dom, stride_dqm, stride_dkn, stride_dvn, seqlen_q, seqlen_k, headdim, ATOMIC_ADD=True, BIAS_TYPE=BIAS_TYPE, IS_CAUSAL=IS_CAUSAL, BLOCK_HEADDIM=BLOCK_HEADDIM, EVEN_M=EVEN_M, EVEN_N=EVEN_N, EVEN_HEADDIM=EVEN_HEADDIM, BLOCK_M=BLOCK_M, BLOCK_N=BLOCK_N)
328
+
329
+ def _flash_attn_forward(q, k, v, bias=None, causal=False, softmax_scale=None):
330
+ (batch, seqlen_q, nheads, d) = q.shape
331
+ (_, seqlen_k, _, _) = k.shape
332
+ assert k.shape == (batch, seqlen_k, nheads, d)
333
+ assert v.shape == (batch, seqlen_k, nheads, d)
334
+ assert d <= 128, 'FlashAttention only support head dimensions up to 128'
335
+ assert q.dtype == k.dtype == v.dtype, 'All tensors must have the same type'
336
+ assert q.dtype in [torch.float16, torch.bfloat16], 'Only support fp16 and bf16'
337
+ assert q.is_cuda and k.is_cuda and v.is_cuda
338
+ softmax_scale = softmax_scale or 1.0 / math.sqrt(d)
339
+ has_bias = bias is not None
340
+ bias_type = 'none'
341
+ if has_bias:
342
+ assert bias.dtype in [q.dtype, torch.float]
343
+ assert bias.is_cuda
344
+ assert bias.dim() == 4
345
+ if bias.stride(-1) != 1:
346
+ bias = bias.contiguous()
347
+ if bias.shape[2:] == (1, seqlen_k):
348
+ bias_type = 'vector'
349
+ elif bias.shape[2:] == (seqlen_q, seqlen_k):
350
+ bias_type = 'matrix'
351
+ else:
352
+ raise RuntimeError('Last 2 dimensions of bias must be (1, seqlen_k) or (seqlen_q, seqlen_k)')
353
+ bias = bias.expand(batch, nheads, seqlen_q, seqlen_k)
354
+ bias_strides = (bias.stride(0), bias.stride(1), bias.stride(2)) if has_bias else (0, 0, 0)
355
+ seqlen_q_rounded = math.ceil(seqlen_q / 128) * 128
356
+ lse = torch.empty((batch, nheads, seqlen_q_rounded), device=q.device, dtype=torch.float32)
357
+ tmp = torch.empty((batch, nheads, seqlen_q_rounded), device=q.device, dtype=torch.float32)
358
+ o = torch.empty_like(q)
359
+ BLOCK_HEADDIM = max(triton.next_power_of_2(d), 16)
360
+ BLOCK = 128
361
+ num_warps = 4 if d <= 64 else 8
362
+ grid = lambda META: (triton.cdiv(seqlen_q, META['BLOCK_M']), batch * nheads)
363
+ _fwd_kernel[grid](q, k, v, bias, o, lse, tmp, softmax_scale, q.stride(0), q.stride(2), q.stride(1), k.stride(0), k.stride(2), k.stride(1), v.stride(0), v.stride(2), v.stride(1), *bias_strides, o.stride(0), o.stride(2), o.stride(1), nheads, seqlen_q, seqlen_k, seqlen_q_rounded, d, seqlen_q // 32, seqlen_k // 32, bias_type, causal, BLOCK_HEADDIM, BLOCK_M=BLOCK, BLOCK_N=BLOCK, num_warps=num_warps, num_stages=1)
364
+ return (o, lse, softmax_scale)
365
+
366
+ def _flash_attn_backward(do, q, k, v, o, lse, dq, dk, dv, bias=None, causal=False, softmax_scale=None):
367
+ if do.stride(-1) != 1:
368
+ do = do.contiguous()
369
+ (batch, seqlen_q, nheads, d) = q.shape
370
+ (_, seqlen_k, _, _) = k.shape
371
+ assert d <= 128
372
+ seqlen_q_rounded = math.ceil(seqlen_q / 128) * 128
373
+ assert lse.shape == (batch, nheads, seqlen_q_rounded)
374
+ assert q.stride(-1) == k.stride(-1) == v.stride(-1) == o.stride(-1) == 1
375
+ assert dq.stride(-1) == dk.stride(-1) == dv.stride(-1) == 1
376
+ softmax_scale = softmax_scale or 1.0 / math.sqrt(d)
377
+ dq_accum = torch.empty_like(q, dtype=torch.float32)
378
+ delta = torch.empty_like(lse)
379
+ BLOCK_HEADDIM = max(triton.next_power_of_2(d), 16)
380
+ grid = lambda META: (triton.cdiv(seqlen_q, META['BLOCK_M']), batch * nheads)
381
+ _bwd_preprocess_do_o_dot[grid](o, do, delta, o.stride(0), o.stride(2), o.stride(1), do.stride(0), do.stride(2), do.stride(1), nheads, seqlen_q, seqlen_q_rounded, d, BLOCK_M=128, BLOCK_HEADDIM=BLOCK_HEADDIM)
382
+ has_bias = bias is not None
383
+ bias_type = 'none'
384
+ if has_bias:
385
+ assert bias.dtype in [q.dtype, torch.float]
386
+ assert bias.is_cuda
387
+ assert bias.dim() == 4
388
+ assert bias.stride(-1) == 1
389
+ if bias.shape[2:] == (1, seqlen_k):
390
+ bias_type = 'vector'
391
+ elif bias.shape[2:] == (seqlen_q, seqlen_k):
392
+ bias_type = 'matrix'
393
+ else:
394
+ raise RuntimeError('Last 2 dimensions of bias must be (1, seqlen_k) or (seqlen_q, seqlen_k)')
395
+ bias = bias.expand(batch, nheads, seqlen_q, seqlen_k)
396
+ bias_strides = (bias.stride(0), bias.stride(1), bias.stride(2)) if has_bias else (0, 0, 0)
397
+ grid = lambda META: (triton.cdiv(seqlen_k, META['BLOCK_N']) if META['SEQUENCE_PARALLEL'] else 1, batch * nheads)
398
+ _bwd_kernel[grid](q, k, v, bias, do, dq_accum, dk, dv, lse, delta, softmax_scale, q.stride(0), q.stride(2), q.stride(1), k.stride(0), k.stride(2), k.stride(1), v.stride(0), v.stride(2), v.stride(1), *bias_strides, do.stride(0), do.stride(2), do.stride(1), dq_accum.stride(0), dq_accum.stride(2), dq_accum.stride(1), dk.stride(0), dk.stride(2), dk.stride(1), dv.stride(0), dv.stride(2), dv.stride(1), nheads, seqlen_q, seqlen_k, seqlen_q_rounded, d, seqlen_q // 32, seqlen_k // 32, bias_type, causal, BLOCK_HEADDIM)
399
+ dq.copy_(dq_accum)
400
+
401
+ class FlashAttnQKVPackedFunc(torch.autograd.Function):
402
+
403
+ @staticmethod
404
+ def forward(ctx, qkv, bias=None, causal=False, softmax_scale=None):
405
+ """
406
+ qkv: (batch, seqlen, 3, nheads, headdim)
407
+ bias: optional, shape broadcastible to (batch, nheads, seqlen, seqlen).
408
+ For example, ALiBi mask for causal would have shape (1, nheads, 1, seqlen).
409
+ ALiBi mask for non-causal would have shape (1, nheads, seqlen, seqlen)
410
+ """
411
+ if qkv.stride(-1) != 1:
412
+ qkv = qkv.contiguous()
413
+ (o, lse, ctx.softmax_scale) = _flash_attn_forward(qkv[:, :, 0], qkv[:, :, 1], qkv[:, :, 2], bias=bias, causal=causal, softmax_scale=softmax_scale)
414
+ ctx.save_for_backward(qkv, o, lse, bias)
415
+ ctx.causal = causal
416
+ return o
417
+
418
+ @staticmethod
419
+ def backward(ctx, do):
420
+ (qkv, o, lse, bias) = ctx.saved_tensors
421
+ assert not ctx.needs_input_grad[1], 'FlashAttention does not support bias gradient yet'
422
+ with torch.inference_mode():
423
+ dqkv = torch.empty_like(qkv)
424
+ _flash_attn_backward(do, qkv[:, :, 0], qkv[:, :, 1], qkv[:, :, 2], o, lse, dqkv[:, :, 0], dqkv[:, :, 1], dqkv[:, :, 2], bias=bias, causal=ctx.causal, softmax_scale=ctx.softmax_scale)
425
+ return (dqkv, None, None, None)
426
+ flash_attn_qkvpacked_func = FlashAttnQKVPackedFunc.apply
427
+
428
+ class FlashAttnKVPackedFunc(torch.autograd.Function):
429
+
430
+ @staticmethod
431
+ def forward(ctx, q, kv, bias=None, causal=False, softmax_scale=None):
432
+ """
433
+ q: (batch, seqlen_q, nheads, headdim)
434
+ kv: (batch, seqlen_k, 2, nheads, headdim)
435
+ bias: optional, shape broadcastible to (batch, nheads, seqlen_q, seqlen_k).
436
+ For example, ALiBi mask for causal would have shape (1, nheads, 1, seqlen_k).
437
+ ALiBi mask for non-causal would have shape (1, nheads, seqlen_q, seqlen_k)
438
+ """
439
+ (q, kv) = [x if x.stride(-1) == 1 else x.contiguous() for x in [q, kv]]
440
+ (o, lse, ctx.softmax_scale) = _flash_attn_forward(q, kv[:, :, 0], kv[:, :, 1], bias=bias, causal=causal, softmax_scale=softmax_scale)
441
+ ctx.save_for_backward(q, kv, o, lse, bias)
442
+ ctx.causal = causal
443
+ return o
444
+
445
+ @staticmethod
446
+ def backward(ctx, do):
447
+ (q, kv, o, lse, bias) = ctx.saved_tensors
448
+ if len(ctx.needs_input_grad) >= 3:
449
+ assert not ctx.needs_input_grad[2], 'FlashAttention does not support bias gradient yet'
450
+ with torch.inference_mode():
451
+ dq = torch.empty_like(q)
452
+ dkv = torch.empty_like(kv)
453
+ _flash_attn_backward(do, q, kv[:, :, 0], kv[:, :, 1], o, lse, dq, dkv[:, :, 0], dkv[:, :, 1], bias=bias, causal=ctx.causal, softmax_scale=ctx.softmax_scale)
454
+ return (dq, dkv, None, None, None)
455
+ flash_attn_kvpacked_func = FlashAttnKVPackedFunc.apply
456
+
457
+ class FlashAttnFunc(torch.autograd.Function):
458
+
459
+ @staticmethod
460
+ def forward(ctx, q, k, v, bias=None, causal=False, softmax_scale=None):
461
+ """
462
+ q: (batch_size, seqlen_q, nheads, headdim)
463
+ k, v: (batch_size, seqlen_k, nheads, headdim)
464
+ bias: optional, shape broadcastible to (batch, nheads, seqlen_q, seqlen_k).
465
+ For example, ALiBi mask for causal would have shape (1, nheads, 1, seqlen_k).
466
+ ALiBi mask for non-causal would have shape (1, nheads, seqlen_q, seqlen_k)
467
+ """
468
+ (q, k, v) = [x if x.stride(-1) == 1 else x.contiguous() for x in [q, k, v]]
469
+ (o, lse, ctx.softmax_scale) = _flash_attn_forward(q, k, v, bias=bias, causal=causal, softmax_scale=softmax_scale)
470
+ ctx.save_for_backward(q, k, v, o, lse, bias)
471
+ ctx.causal = causal
472
+ return o
473
+
474
+ @staticmethod
475
+ def backward(ctx, do):
476
+ (q, k, v, o, lse, bias) = ctx.saved_tensors
477
+ assert not ctx.needs_input_grad[3], 'FlashAttention does not support bias gradient yet'
478
+ with torch.inference_mode():
479
+ dq = torch.empty_like(q)
480
+ dk = torch.empty_like(k)
481
+ dv = torch.empty_like(v)
482
+ _flash_attn_backward(do, q, k, v, o, lse, dq, dk, dv, bias=bias, causal=ctx.causal, softmax_scale=ctx.softmax_scale)
483
+ return (dq, dk, dv, None, None, None)
484
+ flash_attn_func = FlashAttnFunc.apply
hf_model/generation_config.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "transformers_version": "4.30.2",
4
+ "use_cache": false
5
+ }
hf_model/hf_prefixlm_converter.py ADDED
@@ -0,0 +1,419 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Converts Huggingface Causal LM to Prefix LM.
2
+
3
+ Conversion does lightweight surgery on a HuggingFace
4
+ Causal LM to convert it to a Prefix LM.
5
+
6
+ Prefix LMs accepts a `bidirectional_mask` input in `forward`
7
+ and treat the input prompt as the prefix in `generate`.
8
+ """
9
+ import math
10
+ import warnings
11
+ from types import MethodType
12
+ from typing import Any, List, MutableMapping, Optional, Tuple, Union
13
+ import torch
14
+ from transformers.models.bloom.modeling_bloom import BaseModelOutputWithPastAndCrossAttentions, BloomForCausalLM, BloomModel, CausalLMOutputWithCrossAttentions, CrossEntropyLoss
15
+ from transformers.models.bloom.modeling_bloom import _expand_mask as _expand_mask_bloom
16
+ from transformers.models.bloom.modeling_bloom import _make_causal_mask as _make_causal_mask_bloom
17
+ from transformers.models.bloom.modeling_bloom import logging
18
+ from transformers.models.gpt2.modeling_gpt2 import GPT2LMHeadModel
19
+ from transformers.models.gpt_neo.modeling_gpt_neo import GPTNeoForCausalLM
20
+ from transformers.models.gpt_neox.modeling_gpt_neox import GPTNeoXForCausalLM
21
+ from transformers.models.gptj.modeling_gptj import GPTJForCausalLM
22
+ from transformers.models.opt.modeling_opt import OPTForCausalLM
23
+ from transformers.models.opt.modeling_opt import _expand_mask as _expand_mask_opt
24
+ from transformers.models.opt.modeling_opt import _make_causal_mask as _make_causal_mask_opt
25
+ logger = logging.get_logger(__name__)
26
+ _SUPPORTED_GPT_MODELS = (GPT2LMHeadModel, GPTJForCausalLM, GPTNeoForCausalLM, GPTNeoXForCausalLM)
27
+ CAUSAL_GPT_TYPES = Union[GPT2LMHeadModel, GPTJForCausalLM, GPTNeoForCausalLM, GPTNeoXForCausalLM]
28
+
29
+ def _convert_gpt_causal_lm_to_prefix_lm(model: CAUSAL_GPT_TYPES) -> CAUSAL_GPT_TYPES:
30
+ """Converts a GPT-style Causal LM to a Prefix LM.
31
+
32
+ Supported HuggingFace model classes:
33
+ - `GPT2LMHeadModel`
34
+ - `GPTNeoForCausalLM`
35
+ - `GPTNeoXForCausalLM`
36
+ - `GPTJForCausalLM`
37
+
38
+ See `convert_hf_causal_lm_to_prefix_lm` for more details.
39
+ """
40
+ if hasattr(model, '_prefix_lm_converted'):
41
+ return model
42
+ assert isinstance(model, _SUPPORTED_GPT_MODELS)
43
+ assert model.config.add_cross_attention == False, 'Only supports GPT-style decoder-only models'
44
+
45
+ def _get_attn_modules(model: CAUSAL_GPT_TYPES) -> List[torch.nn.Module]:
46
+ """Helper that gets a list of the model's attention modules.
47
+
48
+ Each module has a `bias` buffer used for causal masking. The Prefix LM
49
+ conversion adds logic to dynamically manipulate these biases to support
50
+ Prefix LM attention masking.
51
+ """
52
+ attn_modules = []
53
+ if isinstance(model, GPTNeoXForCausalLM):
54
+ blocks = model.gpt_neox.layers
55
+ else:
56
+ blocks = model.transformer.h
57
+ for block in blocks:
58
+ if isinstance(model, GPTNeoForCausalLM):
59
+ if block.attn.attention_type != 'global':
60
+ continue
61
+ attn_module = block.attn.attention
62
+ elif isinstance(model, GPTNeoXForCausalLM):
63
+ attn_module = block.attention
64
+ else:
65
+ attn_module = block.attn
66
+ attn_modules.append(attn_module)
67
+ return attn_modules
68
+ setattr(model, '_original_forward', getattr(model, 'forward'))
69
+ setattr(model, '_original_generate', getattr(model, 'generate'))
70
+
71
+ def forward(self: CAUSAL_GPT_TYPES, input_ids: Optional[torch.LongTensor]=None, past_key_values: Optional[Tuple[Tuple[torch.Tensor]]]=None, attention_mask: Optional[torch.FloatTensor]=None, bidirectional_mask: Optional[torch.Tensor]=None, token_type_ids: Optional[torch.LongTensor]=None, position_ids: Optional[torch.LongTensor]=None, head_mask: Optional[torch.FloatTensor]=None, inputs_embeds: Optional[torch.FloatTensor]=None, labels: Optional[torch.LongTensor]=None, use_cache: Optional[bool]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None):
72
+ """Wraps original forward to enable PrefixLM attention."""
73
+
74
+ def call_og_forward():
75
+ if isinstance(self, GPTNeoXForCausalLM):
76
+ return self._original_forward(input_ids=input_ids, past_key_values=past_key_values, attention_mask=attention_mask, head_mask=head_mask, inputs_embeds=inputs_embeds, labels=labels, use_cache=use_cache, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict)
77
+ else:
78
+ return self._original_forward(input_ids=input_ids, past_key_values=past_key_values, attention_mask=attention_mask, token_type_ids=token_type_ids, position_ids=position_ids, head_mask=head_mask, inputs_embeds=inputs_embeds, labels=labels, use_cache=use_cache, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict)
79
+ if bidirectional_mask is None:
80
+ return call_og_forward()
81
+ assert isinstance(bidirectional_mask, torch.Tensor)
82
+ attn_modules = _get_attn_modules(model)
83
+ (b, s) = bidirectional_mask.shape
84
+ max_length = attn_modules[0].bias.shape[-1]
85
+ if s > max_length:
86
+ raise ValueError(f'bidirectional_mask sequence length (={s}) exceeds the ' + f'max length allowed by the model ({max_length}).')
87
+ assert s <= max_length
88
+ if s < max_length:
89
+ pad = torch.zeros((int(b), int(max_length - s)), dtype=bidirectional_mask.dtype, device=bidirectional_mask.device)
90
+ bidirectional_mask = torch.cat([bidirectional_mask, pad], dim=1)
91
+ bidirectional = bidirectional_mask.unsqueeze(1).unsqueeze(1)
92
+ for attn_module in attn_modules:
93
+ attn_module.bias.data = torch.logical_or(attn_module.bias.data, bidirectional)
94
+ output = call_og_forward()
95
+ for attn_module in attn_modules:
96
+ attn_module.bias.data = torch.tril(attn_module.bias.data[0, 0])[None, None]
97
+ return output
98
+
99
+ def generate(self: CAUSAL_GPT_TYPES, *args: Any, **kwargs: Any):
100
+ """Wraps original generate to enable PrefixLM attention."""
101
+ attn_modules = _get_attn_modules(model)
102
+ for attn_module in attn_modules:
103
+ attn_module.bias.data[:] = 1
104
+ output = self._original_generate(*args, **kwargs)
105
+ for attn_module in attn_modules:
106
+ attn_module.bias.data = torch.tril(attn_module.bias.data[0, 0])[None, None]
107
+ return output
108
+ setattr(model, 'forward', MethodType(forward, model))
109
+ setattr(model, 'generate', MethodType(generate, model))
110
+ setattr(model, '_prefix_lm_converted', True)
111
+ return model
112
+
113
+ def _convert_bloom_causal_lm_to_prefix_lm(model: BloomForCausalLM) -> BloomForCausalLM:
114
+ """Converts a BLOOM Causal LM to a Prefix LM.
115
+
116
+ Supported HuggingFace model classes:
117
+ - `BloomForCausalLM`
118
+
119
+ See `convert_hf_causal_lm_to_prefix_lm` for more details.
120
+ """
121
+ if hasattr(model, '_prefix_lm_converted'):
122
+ return model
123
+ assert isinstance(model, BloomForCausalLM)
124
+ assert model.config.add_cross_attention == False, 'Only supports BLOOM decoder-only models'
125
+
126
+ def _prepare_attn_mask(self: BloomModel, attention_mask: torch.Tensor, bidirectional_mask: Optional[torch.Tensor], input_shape: Tuple[int, int], past_key_values_length: int) -> torch.BoolTensor:
127
+ combined_attention_mask = None
128
+ device = attention_mask.device
129
+ (_, src_length) = input_shape
130
+ if src_length > 1:
131
+ combined_attention_mask = _make_causal_mask_bloom(input_shape, device=device, past_key_values_length=past_key_values_length)
132
+ if bidirectional_mask is not None:
133
+ assert attention_mask.shape == bidirectional_mask.shape
134
+ expanded_bidirectional_mask = _expand_mask_bloom(bidirectional_mask, tgt_length=src_length)
135
+ combined_attention_mask = torch.logical_and(combined_attention_mask, expanded_bidirectional_mask)
136
+ expanded_attn_mask = _expand_mask_bloom(attention_mask, tgt_length=src_length)
137
+ combined_attention_mask = expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask | combined_attention_mask
138
+ return combined_attention_mask
139
+
140
+ def _build_alibi_tensor(self: BloomModel, batch_size: int, query_length: int, key_length: int, dtype: torch.dtype, device: torch.device) -> torch.Tensor:
141
+ num_heads = self.config.n_head
142
+ closest_power_of_2 = 2 ** math.floor(math.log2(num_heads))
143
+ base = torch.tensor(2 ** (-2 ** (-(math.log2(closest_power_of_2) - 3))), device=device, dtype=torch.float32)
144
+ powers = torch.arange(1, 1 + closest_power_of_2, device=device, dtype=torch.int32)
145
+ slopes = torch.pow(base, powers)
146
+ if closest_power_of_2 != num_heads:
147
+ extra_base = torch.tensor(2 ** (-2 ** (-(math.log2(2 * closest_power_of_2) - 3))), device=device, dtype=torch.float32)
148
+ num_remaining_heads = min(closest_power_of_2, num_heads - closest_power_of_2)
149
+ extra_powers = torch.arange(1, 1 + 2 * num_remaining_heads, 2, device=device, dtype=torch.int32)
150
+ slopes = torch.cat([slopes, torch.pow(extra_base, extra_powers)], dim=0)
151
+ qa = torch.arange(query_length, device=device, dtype=torch.int32).view(-1, 1)
152
+ ka = torch.arange(key_length, device=device, dtype=torch.int32).view(1, -1)
153
+ diffs = qa - ka + key_length - query_length
154
+ diffs = -diffs.abs()
155
+ alibi = slopes.view(1, num_heads, 1, 1) * diffs.view(1, 1, query_length, key_length)
156
+ alibi = alibi.expand(batch_size, -1, -1, -1).reshape(-1, query_length, key_length)
157
+ return alibi.to(dtype)
158
+ KeyValueT = Tuple[torch.Tensor, torch.Tensor]
159
+
160
+ def forward(self: BloomModel, input_ids: Optional[torch.LongTensor]=None, past_key_values: Optional[Tuple[KeyValueT, ...]]=None, attention_mask: Optional[torch.Tensor]=None, bidirectional_mask: Optional[torch.Tensor]=None, head_mask: Optional[torch.LongTensor]=None, inputs_embeds: Optional[torch.LongTensor]=None, use_cache: Optional[bool]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None, **deprecated_arguments: Any) -> Union[Tuple[torch.Tensor, ...], BaseModelOutputWithPastAndCrossAttentions]:
161
+ if deprecated_arguments.pop('position_ids', False) is not False:
162
+ warnings.warn('`position_ids` have no functionality in BLOOM and will be removed in v5.0.0. ' + 'You can safely ignore passing `position_ids`.', FutureWarning)
163
+ if len(deprecated_arguments) > 0:
164
+ raise ValueError(f'Got unexpected arguments: {deprecated_arguments}')
165
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
166
+ output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
167
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
168
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
169
+ if input_ids is not None and inputs_embeds is not None:
170
+ raise ValueError('You cannot specify both input_ids and inputs_embeds at the same time')
171
+ elif input_ids is not None:
172
+ (batch_size, seq_length) = input_ids.shape
173
+ elif inputs_embeds is not None:
174
+ (batch_size, seq_length, _) = inputs_embeds.shape
175
+ else:
176
+ raise ValueError('You have to specify either input_ids or inputs_embeds')
177
+ if past_key_values is None:
178
+ past_key_values = tuple([None] * len(self.h))
179
+ head_mask = self.get_head_mask(head_mask, self.config.n_layer)
180
+ if inputs_embeds is None:
181
+ inputs_embeds = self.word_embeddings(input_ids)
182
+ hidden_states = self.word_embeddings_layernorm(inputs_embeds)
183
+ presents = () if use_cache else None
184
+ all_self_attentions = () if output_attentions else None
185
+ all_hidden_states = () if output_hidden_states else None
186
+ seq_length_with_past = seq_length
187
+ past_key_values_length = 0
188
+ if past_key_values[0] is not None:
189
+ tmp = past_key_values[0][0]
190
+ past_key_values_length = tmp.shape[2]
191
+ seq_length_with_past = seq_length_with_past + past_key_values_length
192
+ if attention_mask is None:
193
+ attention_mask = torch.ones((batch_size, seq_length_with_past), device=hidden_states.device)
194
+ else:
195
+ attention_mask = attention_mask.to(hidden_states.device)
196
+ alibi = self._build_alibi_tensor(batch_size=batch_size, query_length=seq_length, key_length=seq_length_with_past, dtype=hidden_states.dtype, device=hidden_states.device)
197
+ causal_mask = self._prepare_attn_mask(attention_mask, bidirectional_mask, input_shape=(batch_size, seq_length), past_key_values_length=past_key_values_length)
198
+ for (i, (block, layer_past)) in enumerate(zip(self.h, past_key_values)):
199
+ if output_hidden_states:
200
+ hst = (hidden_states,)
201
+ all_hidden_states = all_hidden_states + hst
202
+ if self.gradient_checkpointing and self.training:
203
+ if use_cache:
204
+ logger.warning('`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...')
205
+ use_cache = False
206
+
207
+ def create_custom_forward(module: torch.nn.Module):
208
+
209
+ def custom_forward(*inputs: Any):
210
+ return module(*inputs, use_cache=use_cache, output_attentions=output_attentions)
211
+ return custom_forward
212
+ outputs = torch.utils.checkpoint.checkpoint(create_custom_forward(block), hidden_states, alibi, causal_mask, head_mask[i])
213
+ else:
214
+ outputs = block(hidden_states, layer_past=layer_past, attention_mask=causal_mask, head_mask=head_mask[i], use_cache=use_cache, output_attentions=output_attentions, alibi=alibi)
215
+ hidden_states = outputs[0]
216
+ if use_cache is True:
217
+ presents = presents + (outputs[1],)
218
+ if output_attentions:
219
+ oa = (outputs[2 if use_cache else 1],)
220
+ all_self_attentions = all_self_attentions + oa
221
+ hidden_states = self.ln_f(hidden_states)
222
+ if output_hidden_states:
223
+ hst = (hidden_states,)
224
+ all_hidden_states = all_hidden_states + hst
225
+ if not return_dict:
226
+ return tuple((v for v in [hidden_states, presents, all_hidden_states, all_self_attentions] if v is not None))
227
+ return BaseModelOutputWithPastAndCrossAttentions(last_hidden_state=hidden_states, past_key_values=presents, hidden_states=all_hidden_states, attentions=all_self_attentions)
228
+ setattr(model.transformer, '_prepare_attn_mask', MethodType(_prepare_attn_mask, model.transformer))
229
+ setattr(model.transformer, '_build_alibi_tensor', MethodType(_build_alibi_tensor, model.transformer))
230
+ setattr(model.transformer, 'forward', MethodType(forward, model.transformer))
231
+ KeyValueT = Tuple[torch.Tensor, torch.Tensor]
232
+
233
+ def forward(self: BloomForCausalLM, input_ids: Optional[torch.LongTensor]=None, past_key_values: Optional[Tuple[KeyValueT, ...]]=None, attention_mask: Optional[torch.Tensor]=None, bidirectional_mask: Optional[torch.Tensor]=None, head_mask: Optional[torch.Tensor]=None, inputs_embeds: Optional[torch.Tensor]=None, labels: Optional[torch.Tensor]=None, use_cache: Optional[bool]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None, **deprecated_arguments: Any) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]:
234
+ """Replacement forward method for BloomCausalLM."""
235
+ if deprecated_arguments.pop('position_ids', False) is not False:
236
+ warnings.warn('`position_ids` have no functionality in BLOOM and will be removed ' + 'in v5.0.0. You can safely ignore passing `position_ids`.', FutureWarning)
237
+ if len(deprecated_arguments) > 0:
238
+ raise ValueError(f'Got unexpected arguments: {deprecated_arguments}')
239
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
240
+ transformer_outputs = self.transformer(input_ids, past_key_values=past_key_values, attention_mask=attention_mask, bidirectional_mask=bidirectional_mask, head_mask=head_mask, inputs_embeds=inputs_embeds, use_cache=use_cache, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict)
241
+ hidden_states = transformer_outputs[0]
242
+ lm_logits = self.lm_head(hidden_states)
243
+ loss = None
244
+ if labels is not None:
245
+ shift_logits = lm_logits[..., :-1, :].contiguous()
246
+ shift_labels = labels[..., 1:].contiguous()
247
+ (batch_size, seq_length, vocab_size) = shift_logits.shape
248
+ loss_fct = CrossEntropyLoss()
249
+ loss = loss_fct(shift_logits.view(batch_size * seq_length, vocab_size), shift_labels.view(batch_size * seq_length))
250
+ if not return_dict:
251
+ output = (lm_logits,) + transformer_outputs[1:]
252
+ return (loss,) + output if loss is not None else output
253
+ return CausalLMOutputWithCrossAttentions(loss=loss, logits=lm_logits, past_key_values=transformer_outputs.past_key_values, hidden_states=transformer_outputs.hidden_states, attentions=transformer_outputs.attentions)
254
+
255
+ def prepare_inputs_for_generation(self: BloomForCausalLM, input_ids: torch.LongTensor, past: Optional[torch.Tensor]=None, attention_mask: Optional[torch.Tensor]=None, **kwargs: Any) -> dict:
256
+ del kwargs
257
+ if past:
258
+ input_ids = input_ids[:, -1].unsqueeze(-1)
259
+ bidirectional_mask = None
260
+ if past[0][0].shape[0] == input_ids.shape[0]:
261
+ past = self._convert_to_bloom_cache(past)
262
+ else:
263
+ bidirectional_mask = torch.ones_like(input_ids)
264
+ return {'input_ids': input_ids, 'past_key_values': past, 'use_cache': True, 'attention_mask': attention_mask, 'bidirectional_mask': bidirectional_mask}
265
+ setattr(model, 'forward', MethodType(forward, model))
266
+ setattr(model, 'prepare_inputs_for_generation', MethodType(prepare_inputs_for_generation, model))
267
+ setattr(model, '_prefix_lm_converted', True)
268
+ return model
269
+
270
+ def _convert_opt_causal_lm_to_prefix_lm(model: OPTForCausalLM) -> OPTForCausalLM:
271
+ """Converts an OPT Causal LM to a Prefix LM.
272
+
273
+ Supported HuggingFace model classes:
274
+ - `OPTForCausalLM`
275
+
276
+ See `convert_hf_causal_lm_to_prefix_lm` for more details.
277
+ """
278
+ if hasattr(model, '_prefix_lm_converted'):
279
+ return model
280
+ assert isinstance(model, OPTForCausalLM)
281
+ assert model.config.add_cross_attention == False, 'Only supports OPT decoder-only models'
282
+ setattr(model, '_original_forward', getattr(model, 'forward'))
283
+ setattr(model, '_original_generate', getattr(model, 'generate'))
284
+ model.model.decoder.bidirectional_mask = None
285
+
286
+ def _prepare_decoder_attention_mask(self: torch.nn.Module, attention_mask: Optional[torch.Tensor], input_shape: Tuple[int, int], inputs_embeds: Optional[torch.Tensor], past_key_values_length: int):
287
+ combined_attention_mask = None
288
+ if input_shape[-1] > 1:
289
+ assert inputs_embeds is not None
290
+ if self.bidirectional_mask == 'g':
291
+ (bsz, src_length) = input_shape
292
+ combined_attention_mask = torch.zeros((bsz, 1, src_length, src_length + past_key_values_length), dtype=inputs_embeds.dtype, device=inputs_embeds.device)
293
+ else:
294
+ combined_attention_mask = _make_causal_mask_opt(input_shape, inputs_embeds.dtype, past_key_values_length=past_key_values_length).to(inputs_embeds.device)
295
+ if self.bidirectional_mask is not None:
296
+ assert attention_mask is not None
297
+ assert attention_mask.shape == self.bidirectional_mask.shape
298
+ expanded_bidirectional_mask = _expand_mask_opt(self.bidirectional_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]).to(inputs_embeds.device)
299
+ combined_attention_mask = torch.maximum(expanded_bidirectional_mask, combined_attention_mask)
300
+ if attention_mask is not None:
301
+ assert inputs_embeds is not None
302
+ expanded_attn_mask = _expand_mask_opt(attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]).to(inputs_embeds.device)
303
+ combined_attention_mask = expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask
304
+ return combined_attention_mask
305
+ setattr(model.model.decoder, '_prepare_decoder_attention_mask', MethodType(_prepare_decoder_attention_mask, model.model.decoder))
306
+
307
+ def forward(self: OPTForCausalLM, input_ids: Optional[torch.LongTensor]=None, attention_mask: Optional[torch.Tensor]=None, bidirectional_mask: Optional[torch.ByteTensor]=None, head_mask: Optional[torch.Tensor]=None, past_key_values: Optional[List[torch.FloatTensor]]=None, inputs_embeds: Optional[torch.FloatTensor]=None, labels: Optional[torch.LongTensor]=None, use_cache: Optional[bool]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None):
308
+
309
+ def call_og_forward():
310
+ return self._original_forward(input_ids=input_ids, attention_mask=attention_mask, head_mask=head_mask, past_key_values=past_key_values, inputs_embeds=inputs_embeds, labels=labels, use_cache=use_cache, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict)
311
+ if bidirectional_mask is None:
312
+ return call_og_forward()
313
+ self.model.decoder.bidirectional_mask = bidirectional_mask
314
+ try:
315
+ outputs = call_og_forward()
316
+ except:
317
+ self.model.decoder.bidirectional_mask = None
318
+ raise
319
+ self.model.decoder.bidirectional_mask = None
320
+ return outputs
321
+
322
+ def generate(self: OPTForCausalLM, *args: tuple, **kwargs: Any):
323
+ """Wraps original generate to enable PrefixLM-style attention."""
324
+ self.model.decoder.bidirectional_mask = 'g'
325
+ try:
326
+ output = self._original_generate(*args, **kwargs)
327
+ except:
328
+ self.model.decoder.bidirectional_mask = None
329
+ raise
330
+ self.model.decoder.bidirectional_mask = None
331
+ return output
332
+ setattr(model, 'forward', MethodType(forward, model))
333
+ setattr(model, 'generate', MethodType(generate, model))
334
+ setattr(model, '_prefix_lm_converted', True)
335
+ return model
336
+ _SUPPORTED_HF_MODELS = _SUPPORTED_GPT_MODELS + (BloomForCausalLM, OPTForCausalLM)
337
+ CAUSAL_LM_TYPES = Union[GPT2LMHeadModel, GPTJForCausalLM, GPTNeoForCausalLM, GPTNeoXForCausalLM, BloomForCausalLM, OPTForCausalLM]
338
+
339
+ def convert_hf_causal_lm_to_prefix_lm(model: CAUSAL_LM_TYPES) -> CAUSAL_LM_TYPES:
340
+ """Converts a HuggingFace Causal LM to a Prefix LM.
341
+
342
+ Supported HuggingFace model classes:
343
+ - `GPT2LMHeadModel`
344
+ - `GPTNeoForCausalLM`
345
+ - `GPTNeoXForCausalLM`
346
+ - `GPTJForCausalLM`
347
+ - `BloomForCausalLM`
348
+ - `OPTForCausalLM`
349
+
350
+ Conversion to a Prefix LM is done by modifying the `forward` method, and possibly also the
351
+ `generate` method and/or select underlying methods depending on the model class.
352
+
353
+ These changes preserve the model API, but add a new input to `forward`: "bidirectional_mask".
354
+
355
+ Notes on training:
356
+ To actually train the converted model as a Prefix LM, training batches will need to indicate
357
+ the prefix/target structure by including `bidirectional_mask` as part of the batch inputs.
358
+
359
+ **This is not a standard input and requires custom layers either within or after your dataloader.**
360
+
361
+ In addition to adding `bidirectional_mask` to the batch, this custom code should modify `labels`
362
+ such that `batch['labels'][batch['bidirectional_mask'] == 1] == -100`.
363
+ That is, the prefix portion of the sequence should not generate any loss. Loss should only be
364
+ generated by the target portion of the sequence.
365
+
366
+ Notes on `GPTNeoForCausalLM`:
367
+ To simplify the implementation, "global" and "local" attention layers are handled differently.
368
+ For "global" layers, we handle conversion as described above. For "local" layers, which use a
369
+ causal attention mask within a restricted local window, we do not alter the masking.
370
+
371
+ Notes on `forward` method conversion:
372
+ After conversion, the `forward` method will handle a new input, `bidirectional_mask`,
373
+ which should be a [batch_size, seq_length] byte tensor, where 1 indicates token positions
374
+ belonging to the prefix (prefix tokens can attend to one another bidirectionally), and
375
+ 0 indicates token positions belonging to the target.
376
+
377
+ The new `forward` method will incorporate `bidirectional_mask` (if supplied) into the existing
378
+ causal mask, call the original `forward` method, and (if the causal mask is a buffer) reset
379
+ the causal masks before returning the result.
380
+
381
+ Notes on `generate` method conversion:
382
+ After conversion, the `generate` method will have the same signature but will internally
383
+ convert all causal masks to be purely bidirectional, call the original `generate` method, and
384
+ (where appropriate) reset the causal masks before returning the result.
385
+
386
+ This works thanks to the logic of the HuggingFace `generate` API, which first encodes the token
387
+ "prompt" passed to `generate` (which is treated as the prefix) and then sequentially generates
388
+ each new token. Encodings are cached as generation happens, so all prefix tokens can attend to one
389
+ another (as expected in a Prefix LM) and generated tokens can only attend to prefix tokens and
390
+ previously-generated tokens (also as expected in a Prefix LM).
391
+
392
+ To preserve the API, the original methods are renamed to `_original_forward` and
393
+ `_original_generate`, and replaced with new `forward` and `generate` methods that wrap
394
+ them, respectively. Although implementation details vary by model class.
395
+ """
396
+ if isinstance(model, _SUPPORTED_GPT_MODELS):
397
+ return _convert_gpt_causal_lm_to_prefix_lm(model)
398
+ elif isinstance(model, BloomForCausalLM):
399
+ return _convert_bloom_causal_lm_to_prefix_lm(model)
400
+ elif isinstance(model, OPTForCausalLM):
401
+ return _convert_opt_causal_lm_to_prefix_lm(model)
402
+ else:
403
+ raise TypeError(f'Cannot convert model to Prefix LM. ' + f'Model does not belong to set of supported HF models:' + f'\n{_SUPPORTED_HF_MODELS}')
404
+
405
+ def add_bidirectional_mask_if_missing(batch: MutableMapping):
406
+ """Attempts to add bidirectional_mask to batch if missing.
407
+
408
+ Raises:
409
+ KeyError if bidirectional_mask is missing and can't be inferred
410
+ """
411
+ if 'bidirectional_mask' not in batch:
412
+ if batch.get('mode', None) == 'icl_task':
413
+ batch['bidirectional_mask'] = batch['attention_mask'].clone()
414
+ for (i, continuation_indices) in enumerate(batch['continuation_indices']):
415
+ batch['bidirectional_mask'][i, continuation_indices] = 0
416
+ elif 'labels' in batch and 'attention_mask' in batch:
417
+ batch['bidirectional_mask'] = torch.logical_and(torch.eq(batch['attention_mask'], 1), torch.eq(batch['labels'], -100)).type_as(batch['attention_mask'])
418
+ else:
419
+ raise KeyError('No bidirectional_mask in batch and not sure how to construct one.')
hf_model/meta_init_context.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from contextlib import contextmanager
2
+ from typing import Any, Callable, Optional
3
+ import torch
4
+ import torch.nn as nn
5
+
6
+ @contextmanager
7
+ def init_empty_weights(include_buffers: bool=False):
8
+ """Meta initialization context manager.
9
+
10
+ A context manager under which models are initialized with all parameters
11
+ on the meta device, therefore creating an empty model. Useful when just
12
+ initializing the model would blow the available RAM.
13
+
14
+ Args:
15
+ include_buffers (`bool`, *optional*, defaults to `False`): Whether or
16
+ not to also put all buffers on the meta device while initializing.
17
+
18
+ Example:
19
+ ```python
20
+ import torch.nn as nn
21
+
22
+ # Initialize a model with 100 billions parameters in no time and without using any RAM.
23
+ with init_empty_weights():
24
+ tst = nn.Sequential(*[nn.Linear(10000, 10000) for _ in range(1000)])
25
+ ```
26
+
27
+ <Tip warning={true}>
28
+
29
+ Any model created under this context manager has no weights. As such you can't do something like
30
+ `model.to(some_device)` with it. To load weights inside your empty model, see [`load_checkpoint_and_dispatch`].
31
+
32
+ </Tip>
33
+ """
34
+ with init_on_device(torch.device('meta'), include_buffers=include_buffers) as f:
35
+ yield f
36
+
37
+ @contextmanager
38
+ def init_on_device(device: torch.device, include_buffers: bool=False):
39
+ """Device initialization context manager.
40
+
41
+ A context manager under which models are initialized with all parameters
42
+ on the specified device.
43
+
44
+ Args:
45
+ device (`torch.device`): Device to initialize all parameters on.
46
+ include_buffers (`bool`, *optional*, defaults to `False`): Whether or
47
+ not to also put all buffers on the meta device while initializing.
48
+
49
+ Example:
50
+ ```python
51
+ import torch.nn as nn
52
+
53
+ with init_on_device(device=torch.device("cuda")):
54
+ tst = nn.Liner(100, 100) # on `cuda` device
55
+ ```
56
+ """
57
+ old_register_parameter = nn.Module.register_parameter
58
+ if include_buffers:
59
+ old_register_buffer = nn.Module.register_buffer
60
+
61
+ def register_empty_parameter(module: torch.nn.Module, name: str, param: Optional[torch.nn.Parameter]):
62
+ old_register_parameter(module, name, param)
63
+ if param is not None:
64
+ param_cls = type(module._parameters[name])
65
+ kwargs = module._parameters[name].__dict__
66
+ module._parameters[name] = param_cls(module._parameters[name].to(device), **kwargs)
67
+
68
+ def register_empty_buffer(module: torch.nn.Module, name: str, buffer: Optional[torch.Tensor]):
69
+ old_register_buffer(module, name, buffer)
70
+ if buffer is not None:
71
+ module._buffers[name] = module._buffers[name].to(device)
72
+ if include_buffers:
73
+ tensor_constructors_to_patch = {torch_function_name: getattr(torch, torch_function_name) for torch_function_name in ['empty', 'zeros', 'ones', 'full']}
74
+ else:
75
+ tensor_constructors_to_patch = {}
76
+
77
+ def patch_tensor_constructor(fn: Callable):
78
+
79
+ def wrapper(*args: Any, **kwargs: Any):
80
+ kwargs['device'] = device
81
+ return fn(*args, **kwargs)
82
+ return wrapper
83
+ try:
84
+ nn.Module.register_parameter = register_empty_parameter
85
+ if include_buffers:
86
+ nn.Module.register_buffer = register_empty_buffer
87
+ for torch_function_name in tensor_constructors_to_patch.keys():
88
+ setattr(torch, torch_function_name, patch_tensor_constructor(getattr(torch, torch_function_name)))
89
+ yield
90
+ finally:
91
+ nn.Module.register_parameter = old_register_parameter
92
+ if include_buffers:
93
+ nn.Module.register_buffer = old_register_buffer
94
+ for (torch_function_name, old_torch_function) in tensor_constructors_to_patch.items():
95
+ setattr(torch, torch_function_name, old_torch_function)
hf_model/modeling_mpt.py ADDED
@@ -0,0 +1,327 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """A simple, flexible implementation of a GPT model.
2
+
3
+ Inspired by https://github.com/karpathy/minGPT/blob/master/mingpt/model.py
4
+ """
5
+ import math
6
+ import warnings
7
+ from typing import Any, List, Mapping, MutableMapping, Optional, Tuple, Union
8
+ import torch
9
+ import torch.nn as nn
10
+ import torch.nn.functional as F
11
+ from transformers import PreTrainedModel, PreTrainedTokenizerBase
12
+ from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
13
+ from .attention import attn_bias_shape, build_attn_bias
14
+ from .blocks import MPTBlock
15
+ from .custom_embedding import SharedEmbedding
16
+ from .fc import FC_CLASS_REGISTRY as FC_CLASS_REGISTRY
17
+ from .ffn import FFN_CLASS_REGISTRY as FFN_CLASS_REGISTRY
18
+ from .ffn import MPTMLP as MPTMLP
19
+ from .ffn import build_ffn as build_ffn
20
+ from .norm import NORM_CLASS_REGISTRY
21
+ from .configuration_mpt import MPTConfig
22
+ from .adapt_tokenizer import AutoTokenizerForMOD, adapt_tokenizer_for_denoising
23
+ from .hf_prefixlm_converter import add_bidirectional_mask_if_missing, convert_hf_causal_lm_to_prefix_lm
24
+ from .meta_init_context import init_empty_weights
25
+ from .param_init_fns import generic_param_init_fn_, MODEL_INIT_REGISTRY
26
+ try:
27
+ from .flash_attn_triton import flash_attn_func as flash_attn_func
28
+ except:
29
+ pass
30
+
31
+ class MPTPreTrainedModel(PreTrainedModel):
32
+ config_class = MPTConfig
33
+ base_model_prefix = 'model'
34
+ _no_split_modules = ['MPTBlock']
35
+
36
+ class MPTModel(MPTPreTrainedModel):
37
+
38
+ def __init__(self, config: MPTConfig):
39
+ config._validate_config()
40
+ super().__init__(config)
41
+ self.attn_impl = config.attn_config['attn_impl']
42
+ self.prefix_lm = config.attn_config['prefix_lm']
43
+ self.attn_uses_sequence_id = config.attn_config['attn_uses_sequence_id']
44
+ self.alibi = config.attn_config['alibi']
45
+ self.alibi_bias_max = config.attn_config['alibi_bias_max']
46
+ self.learned_pos_emb = config.learned_pos_emb
47
+ if config.init_device == 'mixed':
48
+ if dist.get_local_rank() == 0:
49
+ config.init_device = 'cpu'
50
+ else:
51
+ config.init_device = 'meta'
52
+ if config.norm_type.lower() not in NORM_CLASS_REGISTRY.keys():
53
+ norm_options = ' | '.join(NORM_CLASS_REGISTRY.keys())
54
+ raise NotImplementedError(f'Requested norm type ({config.norm_type}) is not implemented within this repo (Options: {norm_options}).')
55
+ norm_class = NORM_CLASS_REGISTRY[config.norm_type.lower()]
56
+ self.embedding_fraction = config.embedding_fraction
57
+ self.wte = SharedEmbedding(config.vocab_size, config.d_model, device=config.init_device)
58
+ if self.learned_pos_emb:
59
+ self.wpe = torch.nn.Embedding(config.max_seq_len, config.d_model, device=config.init_device)
60
+ self.emb_drop = nn.Dropout(config.emb_pdrop)
61
+ self.blocks = nn.ModuleList([MPTBlock(device=config.init_device, **config.to_dict()) for _ in range(config.n_layers)])
62
+ self.norm_f = norm_class(config.d_model, device=config.init_device)
63
+ if config.init_device != 'meta':
64
+ print(f'You are using config.init_device={config.init_device!r}, but you can also use config.init_device="meta" with Composer + FSDP for fast initialization.')
65
+ self.apply(self.param_init_fn)
66
+ self.is_causal = not self.prefix_lm
67
+ self._attn_bias_initialized = False
68
+ self.attn_bias = None
69
+ self.attn_bias_shape = attn_bias_shape(self.attn_impl, config.n_heads, config.max_seq_len, self.alibi, prefix_lm=self.prefix_lm, causal=self.is_causal, use_sequence_id=self.attn_uses_sequence_id)
70
+ if config.no_bias:
71
+ for module in self.modules():
72
+ if hasattr(module, 'bias') and isinstance(module.bias, nn.Parameter):
73
+ if config.verbose:
74
+ warnings.warn(f'Removing bias ({module.bias}) from {module}.')
75
+ module.register_parameter('bias', None)
76
+ if config.verbose and config.verbose > 2:
77
+ print(self)
78
+ if 'verbose' not in self.config.init_config:
79
+ self.config.init_config['verbose'] = self.config.verbose
80
+ if self.config.init_config['verbose'] > 1:
81
+ init_fn_name = self.config.init_config['name']
82
+ warnings.warn(f'Using {init_fn_name} initialization.')
83
+
84
+ def get_input_embeddings(self):
85
+ return self.wte
86
+
87
+ def set_input_embeddings(self, value: nn.Embedding):
88
+ self.wte = value
89
+
90
+ @torch.no_grad()
91
+ def _attn_bias(self, device: torch.device, dtype: torch.dtype, attention_mask: Optional[torch.ByteTensor]=None, prefix_mask: Optional[torch.ByteTensor]=None, sequence_id: Optional[torch.LongTensor]=None):
92
+ if not self._attn_bias_initialized:
93
+ if self.attn_bias_shape:
94
+ self.attn_bias = torch.zeros(self.attn_bias_shape, device=device, dtype=dtype)
95
+ self.attn_bias = build_attn_bias(self.attn_impl, self.attn_bias, self.config.n_heads, self.config.max_seq_len, causal=self.is_causal, alibi=self.alibi, alibi_bias_max=self.alibi_bias_max)
96
+ self._attn_bias_initialized = True
97
+ if self.attn_impl == 'flash':
98
+ return (self.attn_bias, attention_mask)
99
+ if self.attn_bias is not None:
100
+ self.attn_bias = self.attn_bias.to(dtype=dtype, device=device)
101
+ attn_bias = self.attn_bias
102
+ if self.prefix_lm:
103
+ assert isinstance(attn_bias, torch.Tensor)
104
+ assert isinstance(prefix_mask, torch.Tensor)
105
+ attn_bias = self._apply_prefix_mask(attn_bias, prefix_mask)
106
+ if self.attn_uses_sequence_id and sequence_id is not None:
107
+ assert isinstance(attn_bias, torch.Tensor)
108
+ attn_bias = self._apply_sequence_id(attn_bias, sequence_id)
109
+ if attention_mask is not None:
110
+ s_k = attention_mask.shape[-1]
111
+ if attn_bias is None:
112
+ attn_bias = torch.zeros((1, 1, 1, s_k), device=device, dtype=dtype)
113
+ else:
114
+ _s_k = max(0, attn_bias.size(-1) - s_k)
115
+ attn_bias = attn_bias[:, :, :, _s_k:]
116
+ if prefix_mask is not None and attention_mask.shape != prefix_mask.shape:
117
+ raise ValueError(f'attention_mask shape={attention_mask.shape} ' + f'and prefix_mask shape={prefix_mask.shape} are not equal.')
118
+ min_val = torch.finfo(attn_bias.dtype).min
119
+ attn_bias = attn_bias.masked_fill(~attention_mask.view(-1, 1, 1, s_k), min_val)
120
+ return (attn_bias, None)
121
+
122
+ def _apply_prefix_mask(self, attn_bias: torch.Tensor, prefix_mask: torch.Tensor):
123
+ (s_k, s_q) = attn_bias.shape[-2:]
124
+ if s_k != self.config.max_seq_len or s_q != self.config.max_seq_len:
125
+ raise ValueError('attn_bias does not match the expected shape. ' + f'The last two dimensions should both be {self.config.max_length} ' + f'but are {s_k} and {s_q}.')
126
+ seq_len = prefix_mask.shape[-1]
127
+ if seq_len > self.config.max_seq_len:
128
+ raise ValueError(f'prefix_mask sequence length cannot exceed max_seq_len={self.config.max_seq_len}')
129
+ attn_bias = attn_bias[..., :seq_len, :seq_len]
130
+ causal = torch.tril(torch.ones((seq_len, seq_len), dtype=torch.bool, device=prefix_mask.device)).view(1, 1, seq_len, seq_len)
131
+ prefix = prefix_mask.view(-1, 1, 1, seq_len)
132
+ cannot_attend = ~torch.logical_or(causal, prefix.bool())
133
+ min_val = torch.finfo(attn_bias.dtype).min
134
+ attn_bias = attn_bias.masked_fill(cannot_attend, min_val)
135
+ return attn_bias
136
+
137
+ def _apply_sequence_id(self, attn_bias: torch.Tensor, sequence_id: torch.LongTensor):
138
+ seq_len = sequence_id.shape[-1]
139
+ if seq_len > self.config.max_seq_len:
140
+ raise ValueError(f'sequence_id sequence length cannot exceed max_seq_len={self.config.max_seq_len}')
141
+ attn_bias = attn_bias[..., :seq_len, :seq_len]
142
+ cannot_attend = torch.logical_not(torch.eq(sequence_id.view(-1, seq_len, 1), sequence_id.view(-1, 1, seq_len))).unsqueeze(1)
143
+ min_val = torch.finfo(attn_bias.dtype).min
144
+ attn_bias = attn_bias.masked_fill(cannot_attend, min_val)
145
+ return attn_bias
146
+
147
+ def forward(self, input_ids: torch.LongTensor, past_key_values: Optional[List[Tuple[torch.FloatTensor]]]=None, attention_mask: Optional[torch.ByteTensor]=None, prefix_mask: Optional[torch.ByteTensor]=None, sequence_id: Optional[torch.LongTensor]=None, return_dict: Optional[bool]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, use_cache: Optional[bool]=None, inputs_embeds: Optional[torch.Tensor]=None):
148
+ return_dict = return_dict if return_dict is not None else self.config.return_dict
149
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
150
+ if attention_mask is not None:
151
+ attention_mask = attention_mask.bool()
152
+ if prefix_mask is not None:
153
+ prefix_mask = prefix_mask.bool()
154
+ if not return_dict:
155
+ raise NotImplementedError('return_dict False is not implemented yet for MPT')
156
+ if output_attentions:
157
+ if self.attn_impl != 'torch':
158
+ raise NotImplementedError('output_attentions is not implemented for MPT when using attn_impl `flash` or `triton`.')
159
+ if attention_mask is not None and attention_mask[:, 0].sum() != attention_mask.shape[0] and self.training:
160
+ raise NotImplementedError('MPT does not support training with left padding.')
161
+ if self.prefix_lm and prefix_mask is None:
162
+ raise ValueError('prefix_mask is a required argument when MPT is configured with prefix_lm=True.')
163
+ if inputs_embeds is not None:
164
+ raise NotImplementedError('inputs_embeds is not implemented for MPT.')
165
+ if self.training:
166
+ if self.attn_uses_sequence_id and sequence_id is None:
167
+ raise ValueError('sequence_id is a required argument when MPT is configured with attn_uses_sequence_id=True ' + 'and the model is in train mode.')
168
+ elif self.attn_uses_sequence_id is False and sequence_id is not None:
169
+ warnings.warn('MPT received non-None input for `sequence_id` but is configured with attn_uses_sequence_id=False. ' + 'This input will be ignored. If you want the model to use `sequence_id`, set attn_uses_sequence_id to True.')
170
+ S = input_ids.size(1)
171
+ assert S <= self.config.max_seq_len, f'Cannot forward input with seq_len={S}, this model only supports seq_len<={self.config.max_seq_len}'
172
+ tok_emb = self.wte(input_ids)
173
+ if self.learned_pos_emb:
174
+ past_position = 0
175
+ if past_key_values is not None:
176
+ if len(past_key_values) != self.config.n_layers:
177
+ raise ValueError(f'past_key_values must provide a past_key_value for each attention ' + f'layer in the network (len(past_key_values)={len(past_key_values)!r}; self.config.n_layers={self.config.n_layers!r}).')
178
+ past_position = past_key_values[0][0].size(1)
179
+ if self.attn_impl == 'torch':
180
+ past_position = past_key_values[0][0].size(3)
181
+ if S + past_position > self.config.max_seq_len:
182
+ raise ValueError(f'Cannot forward input with past sequence length {past_position} and current sequence length ' + f'{S + 1}, this model only supports total sequence length <= {self.config.max_seq_len}.')
183
+ pos = torch.arange(past_position, S + past_position, dtype=torch.long, device=input_ids.device).unsqueeze(0)
184
+ if attention_mask is not None:
185
+ pos = torch.clamp(pos - torch.cumsum((~attention_mask).to(torch.int32), dim=1)[:, past_position:], min=0)
186
+ pos_emb = self.wpe(pos)
187
+ x = tok_emb + pos_emb
188
+ else:
189
+ x = tok_emb
190
+ if self.embedding_fraction == 1:
191
+ x = self.emb_drop(x)
192
+ else:
193
+ x_shrunk = x * self.embedding_fraction + x.detach() * (1 - self.embedding_fraction)
194
+ assert isinstance(self.emb_drop, nn.Module)
195
+ x = self.emb_drop(x_shrunk)
196
+ (attn_bias, attention_mask) = self._attn_bias(device=x.device, dtype=torch.float32, attention_mask=attention_mask, prefix_mask=prefix_mask, sequence_id=sequence_id)
197
+ if use_cache and past_key_values is None:
198
+ past_key_values = [() for _ in range(self.config.n_layers)]
199
+ all_hidden_states = () if output_hidden_states else None
200
+ all_self_attns = () if output_attentions else None
201
+ for (b_idx, block) in enumerate(self.blocks):
202
+ if output_hidden_states:
203
+ assert all_hidden_states is not None
204
+ all_hidden_states = all_hidden_states + (x,)
205
+ past_key_value = past_key_values[b_idx] if past_key_values is not None else None
206
+ (x, attn_weights, past_key_value) = block(x, past_key_value=past_key_value, attn_bias=attn_bias, attention_mask=attention_mask, is_causal=self.is_causal)
207
+ if past_key_values is not None:
208
+ past_key_values[b_idx] = past_key_value
209
+ if output_attentions:
210
+ assert all_self_attns is not None
211
+ all_self_attns = all_self_attns + (attn_weights,)
212
+ x = self.norm_f(x)
213
+ if output_hidden_states:
214
+ assert all_hidden_states is not None
215
+ all_hidden_states = all_hidden_states + (x,)
216
+ return BaseModelOutputWithPast(last_hidden_state=x, past_key_values=past_key_values, hidden_states=all_hidden_states, attentions=all_self_attns)
217
+
218
+ def param_init_fn(self, module: nn.Module):
219
+ init_fn_name = self.config.init_config['name']
220
+ MODEL_INIT_REGISTRY[init_fn_name](module=module, n_layers=self.config.n_layers, d_model=self.config.d_model, **self.config.init_config)
221
+
222
+ def fsdp_wrap_fn(self, module: nn.Module):
223
+ return isinstance(module, MPTBlock)
224
+
225
+ def activation_checkpointing_fn(self, module: nn.Module):
226
+ return isinstance(module, MPTBlock)
227
+
228
+ class MPTForCausalLM(MPTPreTrainedModel):
229
+
230
+ def __init__(self, config: MPTConfig):
231
+ super().__init__(config)
232
+ if not config.tie_word_embeddings:
233
+ raise ValueError('MPTForCausalLM only supports tied word embeddings')
234
+ print(f'Instantiating an MPTForCausalLM model from {__file__}')
235
+ self.transformer: MPTModel = MPTModel(config)
236
+ for child in self.transformer.children():
237
+ if isinstance(child, torch.nn.ModuleList):
238
+ continue
239
+ if isinstance(child, torch.nn.Module):
240
+ child._fsdp_wrap = True
241
+ self.logit_scale = None
242
+ if config.logit_scale is not None:
243
+ logit_scale = config.logit_scale
244
+ if isinstance(logit_scale, str):
245
+ if logit_scale == 'inv_sqrt_d_model':
246
+ logit_scale = 1 / math.sqrt(config.d_model)
247
+ else:
248
+ raise ValueError(f"logit_scale={logit_scale!r} is not recognized as an option; use numeric value or 'inv_sqrt_d_model'.")
249
+ self.logit_scale = logit_scale
250
+
251
+ def get_input_embeddings(self):
252
+ return self.transformer.wte
253
+
254
+ def set_input_embeddings(self, value: Union[SharedEmbedding, nn.Embedding]):
255
+ self.transformer.wte = value
256
+
257
+ def get_output_embeddings(self):
258
+ return self.transformer.wte
259
+
260
+ def set_output_embeddings(self, new_embeddings: Union[SharedEmbedding, nn.Embedding]):
261
+ self.transformer.wte = new_embeddings
262
+
263
+ def set_decoder(self, decoder: MPTModel):
264
+ self.transformer = decoder
265
+
266
+ def get_decoder(self):
267
+ return self.transformer
268
+
269
+ def forward(self, input_ids: torch.LongTensor, past_key_values: Optional[List[Tuple[torch.FloatTensor]]]=None, attention_mask: Optional[torch.ByteTensor]=None, prefix_mask: Optional[torch.ByteTensor]=None, sequence_id: Optional[torch.LongTensor]=None, labels: Optional[torch.LongTensor]=None, return_dict: Optional[bool]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, use_cache: Optional[bool]=None, inputs_embeds: Optional[torch.FloatTensor]=None):
270
+ return_dict = return_dict if return_dict is not None else self.config.return_dict
271
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
272
+ if inputs_embeds is not None:
273
+ raise NotImplementedError('inputs_embeds has to be None (for hf/peft support).')
274
+ outputs = self.transformer(input_ids=input_ids, past_key_values=past_key_values, attention_mask=attention_mask, prefix_mask=prefix_mask, sequence_id=sequence_id, return_dict=return_dict, output_attentions=output_attentions, output_hidden_states=output_hidden_states, use_cache=use_cache)
275
+ logits = self.transformer.wte(outputs.last_hidden_state.to(self.transformer.wte.weight.device), True)
276
+ if self.logit_scale is not None:
277
+ if self.logit_scale == 0:
278
+ warnings.warn(f'Multiplying logits by self.logit_scale={self.logit_scale!r}. This will produce uniform (uninformative) outputs.')
279
+ logits *= self.logit_scale
280
+ loss = None
281
+ if labels is not None:
282
+ _labels = torch.roll(labels, shifts=-1)
283
+ _labels[:, -1] = -100
284
+ loss = F.cross_entropy(logits.view(-1, logits.size(-1)), _labels.to(logits.device).view(-1))
285
+ return CausalLMOutputWithPast(loss=loss, logits=logits, past_key_values=outputs.past_key_values, hidden_states=outputs.hidden_states, attentions=outputs.attentions)
286
+
287
+ def param_init_fn(self, module: nn.Module):
288
+ init_fn_name = self.config.init_config['name']
289
+ MODEL_INIT_REGISTRY[init_fn_name](module=module, n_layers=self.config.n_layers, d_model=self.config.d_model, **self.config.init_config)
290
+
291
+ def fsdp_wrap_fn(self, module: nn.Module):
292
+ return isinstance(module, MPTBlock)
293
+
294
+ def activation_checkpointing_fn(self, module: nn.Module):
295
+ return isinstance(module, MPTBlock)
296
+
297
+ def prepare_inputs_for_generation(self, input_ids: torch.Tensor, past_key_values: Optional[List[Tuple[torch.Tensor, torch.Tensor]]]=None, inputs_embeds: Optional[torch.Tensor]=None, **kwargs: Any):
298
+ if inputs_embeds is not None:
299
+ raise NotImplementedError('inputs_embeds is not implemented for MPT yet')
300
+ attention_mask = kwargs['attention_mask'].bool()
301
+ if attention_mask[:, -1].sum() != attention_mask.shape[0]:
302
+ raise NotImplementedError('MPT does not support generation with right padding.')
303
+ if self.transformer.attn_uses_sequence_id and self.training:
304
+ sequence_id = torch.zeros_like(input_ids[:1])
305
+ else:
306
+ sequence_id = None
307
+ if past_key_values is not None:
308
+ input_ids = input_ids[:, -1].unsqueeze(-1)
309
+ if self.transformer.prefix_lm:
310
+ prefix_mask = torch.ones_like(attention_mask)
311
+ if kwargs.get('use_cache') == False:
312
+ raise NotImplementedError('MPT with prefix_lm=True does not support use_cache=False.')
313
+ else:
314
+ prefix_mask = None
315
+ return {'input_ids': input_ids, 'attention_mask': attention_mask, 'prefix_mask': prefix_mask, 'sequence_id': sequence_id, 'past_key_values': past_key_values, 'use_cache': kwargs.get('use_cache', True)}
316
+
317
+ @staticmethod
318
+ def _reorder_cache(past_key_values: List[Tuple[torch.Tensor, torch.Tensor]], beam_idx: torch.LongTensor):
319
+ """Used by HuggingFace generate when using beam search with kv-caching.
320
+
321
+ See https://github.com/huggingface/transformers/blob/3ec7a47664ebe40c40f4b722f6bb1cd30c3821ec/src/transformers/models/gpt2/modeling_gpt2.py#L1122-L1133
322
+ for an example in transformers.
323
+ """
324
+ reordered_past = []
325
+ for layer_past in past_key_values:
326
+ reordered_past += [tuple((past_state.index_select(0, beam_idx) for past_state in layer_past))]
327
+ return reordered_past
hf_model/norm.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Optional, Type, Union
2
+ import torch
3
+
4
+ def _cast_if_autocast_enabled(tensor: torch.Tensor):
5
+ if torch.is_autocast_enabled():
6
+ if tensor.device.type == 'cuda':
7
+ dtype = torch.get_autocast_gpu_dtype()
8
+ elif tensor.device.type == 'cpu':
9
+ dtype = torch.get_autocast_cpu_dtype()
10
+ else:
11
+ raise NotImplementedError()
12
+ return tensor.to(dtype=dtype)
13
+ return tensor
14
+
15
+ class LPLayerNorm(torch.nn.LayerNorm):
16
+
17
+ def __init__(self, normalized_shape: Union[int, List[int], torch.Size], eps: float=1e-05, elementwise_affine: bool=True, device: Optional[torch.device]=None, dtype: Optional[torch.dtype]=None):
18
+ super().__init__(normalized_shape=normalized_shape, eps=eps, elementwise_affine=elementwise_affine, device=device, dtype=dtype)
19
+
20
+ def forward(self, x: torch.Tensor):
21
+ module_device = x.device
22
+ downcast_x = _cast_if_autocast_enabled(x)
23
+ downcast_weight = _cast_if_autocast_enabled(self.weight) if self.weight is not None else self.weight
24
+ downcast_bias = _cast_if_autocast_enabled(self.bias) if self.bias is not None else self.bias
25
+ with torch.autocast(enabled=False, device_type=module_device.type):
26
+ return torch.nn.functional.layer_norm(downcast_x, self.normalized_shape, downcast_weight, downcast_bias, self.eps)
27
+
28
+ def rms_norm(x: torch.Tensor, weight: Optional[torch.Tensor]=None, eps: float=1e-05):
29
+ output = x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + eps)
30
+ if weight is not None:
31
+ return output * weight
32
+ return output
33
+
34
+ class RMSNorm(torch.nn.Module):
35
+
36
+ def __init__(self, normalized_shape: Union[int, List[int], torch.Size], eps: float=1e-05, weight: bool=True, dtype: Optional[torch.dtype]=None, device: Optional[torch.device]=None):
37
+ super().__init__()
38
+ self.eps = eps
39
+ if weight:
40
+ self.weight = torch.nn.Parameter(torch.ones(normalized_shape, dtype=dtype, device=device))
41
+ else:
42
+ self.register_parameter('weight', None)
43
+
44
+ def forward(self, x: torch.Tensor):
45
+ return rms_norm(x.float(), self.weight, self.eps).to(dtype=x.dtype)
46
+
47
+ class LPRMSNorm(RMSNorm):
48
+
49
+ def __init__(self, normalized_shape: Union[int, List[int], torch.Size], eps: float=1e-05, weight: bool=True, dtype: Optional[torch.dtype]=None, device: Optional[torch.device]=None):
50
+ super().__init__(normalized_shape=normalized_shape, eps=eps, weight=weight, dtype=dtype, device=device)
51
+
52
+ def forward(self, x: torch.Tensor):
53
+ downcast_x = _cast_if_autocast_enabled(x)
54
+ downcast_weight = _cast_if_autocast_enabled(self.weight) if self.weight is not None else self.weight
55
+ with torch.autocast(enabled=False, device_type=x.device.type):
56
+ return rms_norm(downcast_x, downcast_weight, self.eps).to(dtype=x.dtype)
57
+ NORM_CLASS_REGISTRY: Dict[str, Type[torch.nn.Module]] = {'layernorm': torch.nn.LayerNorm, 'low_precision_layernorm': LPLayerNorm, 'rmsnorm': RMSNorm, 'low_precision_rmsnorm': LPRMSNorm}
hf_model/param_init_fns.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import warnings
3
+ from collections.abc import Sequence
4
+ from functools import partial
5
+ from typing import Any, Callable, Optional, Tuple, Union
6
+ import torch
7
+ from torch import nn
8
+ from .fc import FC_CLASS_REGISTRY
9
+ from .norm import NORM_CLASS_REGISTRY
10
+ try:
11
+ import transformer_engine.pytorch as te
12
+ except:
13
+ te = None
14
+
15
+ def torch_default_param_init_fn_(module: nn.Module, verbose: int=0, **kwargs: Any):
16
+ del kwargs
17
+ if verbose > 1:
18
+ warnings.warn(f"Initializing network using module's reset_parameters attribute")
19
+ if hasattr(module, 'reset_parameters'):
20
+ module.reset_parameters()
21
+
22
+ def fused_init_helper_(module: nn.Module, init_fn_: Callable):
23
+ _fused = getattr(module, '_fused', None)
24
+ if _fused is None:
25
+ raise RuntimeError(f'Internal logic error')
26
+ (dim, splits) = _fused
27
+ splits = (0, *splits, module.weight.size(dim))
28
+ for (s, e) in zip(splits[:-1], splits[1:]):
29
+ slice_indices = [slice(None)] * module.weight.ndim
30
+ slice_indices[dim] = slice(s, e)
31
+ init_fn_(module.weight[slice_indices])
32
+
33
+ def generic_param_init_fn_(module: nn.Module, init_fn_: Callable, n_layers: int, d_model: Optional[int]=None, init_div_is_residual: Union[int, float, str, bool]=True, emb_init_std: Optional[float]=None, emb_init_uniform_lim: Optional[Union[Tuple[float, float], float]]=None, verbose: int=0, **kwargs: Any):
34
+ del kwargs
35
+ if verbose > 1:
36
+ warnings.warn(f'If model has bias parameters they are initialized to 0.')
37
+ init_div_is_residual = init_div_is_residual
38
+ if init_div_is_residual is False:
39
+ div_is_residual = 1.0
40
+ elif init_div_is_residual is True:
41
+ div_is_residual = math.sqrt(2 * n_layers)
42
+ elif isinstance(init_div_is_residual, float) or isinstance(init_div_is_residual, int):
43
+ div_is_residual = init_div_is_residual
44
+ elif isinstance(init_div_is_residual, str) and init_div_is_residual.isnumeric():
45
+ div_is_residual = float(init_div_is_residual)
46
+ else:
47
+ div_is_residual = 1.0
48
+ raise ValueError(f'Expected init_div_is_residual to be boolean or numeric, got {init_div_is_residual}')
49
+ if init_div_is_residual is not False:
50
+ if verbose > 1:
51
+ warnings.warn(f'Initializing _is_residual layers then dividing them by {div_is_residual:.3f}. ' + f'Set `init_div_is_residual: false` in init config to disable this.')
52
+ if isinstance(module, tuple(set(FC_CLASS_REGISTRY.values()))):
53
+ if hasattr(module, '_fused'):
54
+ fused_init_helper_(module, init_fn_)
55
+ else:
56
+ init_fn_(module.weight)
57
+ if module.bias is not None:
58
+ assert isinstance(module.bias, torch.Tensor)
59
+ torch.nn.init.zeros_(module.bias)
60
+ if init_div_is_residual is not False and getattr(module, '_is_residual', False):
61
+ with torch.no_grad():
62
+ module.weight.div_(div_is_residual)
63
+ elif isinstance(module, nn.Embedding):
64
+ if emb_init_std is not None:
65
+ std = emb_init_std
66
+ if std == 0:
67
+ warnings.warn(f'Embedding layer initialized to 0.')
68
+ emb_init_fn_ = partial(torch.nn.init.normal_, mean=0.0, std=std)
69
+ if verbose > 1:
70
+ warnings.warn(f'Embedding layer initialized using normal distribution with mean=0 and std={std!r}.')
71
+ elif emb_init_uniform_lim is not None:
72
+ lim = emb_init_uniform_lim
73
+ if isinstance(lim, Sequence):
74
+ if len(lim) > 2:
75
+ raise ValueError(f'Uniform init requires a min and a max limit. User input: {lim}.')
76
+ if lim[0] == lim[1]:
77
+ warnings.warn(f'Embedding layer initialized to {lim[0]}.')
78
+ else:
79
+ if lim == 0:
80
+ warnings.warn(f'Embedding layer initialized to 0.')
81
+ lim = [-lim, lim]
82
+ (a, b) = lim
83
+ emb_init_fn_ = partial(torch.nn.init.uniform_, a=a, b=b)
84
+ if verbose > 1:
85
+ warnings.warn(f'Embedding layer initialized using uniform distribution in range {lim}.')
86
+ else:
87
+ emb_init_fn_ = init_fn_
88
+ emb_init_fn_(module.weight)
89
+ elif isinstance(module, tuple(set(NORM_CLASS_REGISTRY.values()))):
90
+ if verbose > 1:
91
+ warnings.warn(f'Norm weights are set to 1. If norm layer has a bias it is initialized to 0.')
92
+ if hasattr(module, 'weight') and module.weight is not None:
93
+ torch.nn.init.ones_(module.weight)
94
+ if hasattr(module, 'bias') and module.bias is not None:
95
+ torch.nn.init.zeros_(module.bias)
96
+ elif isinstance(module, nn.MultiheadAttention):
97
+ if module._qkv_same_embed_dim:
98
+ assert module.in_proj_weight is not None
99
+ assert module.q_proj_weight is None and module.k_proj_weight is None and (module.v_proj_weight is None)
100
+ assert d_model is not None
101
+ _d = d_model
102
+ splits = (0, _d, 2 * _d, 3 * _d)
103
+ for (s, e) in zip(splits[:-1], splits[1:]):
104
+ init_fn_(module.in_proj_weight[s:e])
105
+ else:
106
+ assert module.q_proj_weight is not None and module.k_proj_weight is not None and (module.v_proj_weight is not None)
107
+ assert module.in_proj_weight is None
108
+ init_fn_(module.q_proj_weight)
109
+ init_fn_(module.k_proj_weight)
110
+ init_fn_(module.v_proj_weight)
111
+ if module.in_proj_bias is not None:
112
+ torch.nn.init.zeros_(module.in_proj_bias)
113
+ if module.bias_k is not None:
114
+ torch.nn.init.zeros_(module.bias_k)
115
+ if module.bias_v is not None:
116
+ torch.nn.init.zeros_(module.bias_v)
117
+ init_fn_(module.out_proj.weight)
118
+ if init_div_is_residual is not False and getattr(module.out_proj, '_is_residual', False):
119
+ with torch.no_grad():
120
+ module.out_proj.weight.div_(div_is_residual)
121
+ if module.out_proj.bias is not None:
122
+ torch.nn.init.zeros_(module.out_proj.bias)
123
+ elif te is not None and isinstance(module, te.LayerNormMLP):
124
+ if module.layer_norm_weight is not None:
125
+ torch.nn.init.ones_(module.layer_norm_weight)
126
+ if module.layer_norm_bias is not None:
127
+ torch.nn.init.zeros_(module.layer_norm_bias)
128
+ init_fn_(module.fc1_weight)
129
+ if module.fc1_bias is not None:
130
+ assert isinstance(module.fc1_bias, torch.Tensor)
131
+ torch.nn.init.zeros_(module.fc1_bias)
132
+ init_fn_(module.fc2_weight)
133
+ if module.fc2_bias is not None:
134
+ assert isinstance(module.fc2_bias, torch.Tensor)
135
+ torch.nn.init.zeros_(module.fc2_bias)
136
+ with torch.no_grad():
137
+ module.fc2_weight.div_(div_is_residual)
138
+ else:
139
+ for _ in module.parameters(recurse=False):
140
+ raise NotImplementedError(f'{module.__class__.__name__} parameters are not initialized by param_init_fn.')
141
+
142
+ def _normal_init_(std: float, mean: float=0.0):
143
+ return partial(torch.nn.init.normal_, mean=mean, std=std)
144
+
145
+ def _normal_param_init_fn_(module: nn.Module, std: float, n_layers: int, d_model: Optional[int]=None, init_div_is_residual: Union[int, float, str, bool]=True, emb_init_std: Optional[float]=None, emb_init_uniform_lim: Optional[Union[Tuple[float, float], float]]=None, verbose: int=0, **kwargs: Any):
146
+ del kwargs
147
+ init_fn_ = _normal_init_(std=std)
148
+ if verbose > 1:
149
+ warnings.warn(f'Using torch.nn.init.normal_ init fn mean=0.0, std={std}')
150
+ generic_param_init_fn_(module=module, init_fn_=init_fn_, d_model=d_model, n_layers=n_layers, init_div_is_residual=init_div_is_residual, emb_init_std=emb_init_std, emb_init_uniform_lim=emb_init_uniform_lim, verbose=verbose)
151
+
152
+ def baseline_param_init_fn_(module: nn.Module, init_std: float, n_layers: int, d_model: Optional[int]=None, init_div_is_residual: Union[int, float, str, bool]=True, emb_init_std: Optional[float]=None, emb_init_uniform_lim: Optional[Union[Tuple[float, float], float]]=None, verbose: int=0, **kwargs: Any):
153
+ del kwargs
154
+ if init_std is None:
155
+ raise ValueError("You must set model.init_config['init_std'] to a float value to use the default initialization scheme.")
156
+ _normal_param_init_fn_(module=module, std=init_std, d_model=d_model, n_layers=n_layers, init_div_is_residual=init_div_is_residual, emb_init_std=emb_init_std, emb_init_uniform_lim=emb_init_uniform_lim, verbose=verbose)
157
+
158
+ def small_param_init_fn_(module: nn.Module, n_layers: int, d_model: int, init_div_is_residual: Union[int, float, str, bool]=True, emb_init_std: Optional[float]=None, emb_init_uniform_lim: Optional[Union[Tuple[float, float], float]]=None, verbose: int=0, **kwargs: Any):
159
+ del kwargs
160
+ std = math.sqrt(2 / (5 * d_model))
161
+ _normal_param_init_fn_(module=module, std=std, d_model=d_model, n_layers=n_layers, init_div_is_residual=init_div_is_residual, emb_init_std=emb_init_std, emb_init_uniform_lim=emb_init_uniform_lim, verbose=verbose)
162
+
163
+ def neox_param_init_fn_(module: nn.Module, n_layers: int, d_model: int, emb_init_std: Optional[float]=None, emb_init_uniform_lim: Optional[Union[Tuple[float, float], float]]=None, verbose: int=0, **kwargs: Any):
164
+ """From section 2.3.1 of GPT-NeoX-20B:
165
+
166
+ An Open-Source AutoregressiveLanguage Model — Black et. al. (2022)
167
+ see https://github.com/EleutherAI/gpt-neox/blob/9610391ab319403cef079b438edd016a2443af54/megatron/model/init_functions.py#L151
168
+ and https://github.com/EleutherAI/gpt-neox/blob/main/megatron/model/transformer.py
169
+ """
170
+ del kwargs
171
+ residual_div = n_layers / math.sqrt(10)
172
+ if verbose > 1:
173
+ warnings.warn(f'setting init_div_is_residual to {residual_div}')
174
+ small_param_init_fn_(module=module, d_model=d_model, n_layers=n_layers, init_div_is_residual=residual_div, emb_init_std=emb_init_std, emb_init_uniform_lim=emb_init_uniform_lim, verbose=verbose)
175
+
176
+ def kaiming_uniform_param_init_fn_(module: nn.Module, n_layers: int, d_model: Optional[int]=None, init_div_is_residual: Union[int, float, str, bool]=True, emb_init_std: Optional[float]=None, emb_init_uniform_lim: Optional[Union[Tuple[float, float], float]]=None, init_gain: float=0, fan_mode: str='fan_in', init_nonlinearity: str='leaky_relu', verbose: int=0, **kwargs: Any):
177
+ del kwargs
178
+ if verbose > 1:
179
+ warnings.warn(f'Using nn.init.kaiming_uniform_ init fn with parameters: ' + f'a={init_gain}, mode={fan_mode}, nonlinearity={init_nonlinearity}')
180
+ kaiming_uniform_ = partial(nn.init.kaiming_uniform_, a=init_gain, mode=fan_mode, nonlinearity=init_nonlinearity)
181
+ generic_param_init_fn_(module=module, init_fn_=kaiming_uniform_, d_model=d_model, n_layers=n_layers, init_div_is_residual=init_div_is_residual, emb_init_std=emb_init_std, emb_init_uniform_lim=emb_init_uniform_lim, verbose=verbose)
182
+
183
+ def kaiming_normal_param_init_fn_(module: nn.Module, n_layers: int, d_model: Optional[int]=None, init_div_is_residual: Union[int, float, str, bool]=True, emb_init_std: Optional[float]=None, emb_init_uniform_lim: Optional[Union[Tuple[float, float], float]]=None, init_gain: float=0, fan_mode: str='fan_in', init_nonlinearity: str='leaky_relu', verbose: int=0, **kwargs: Any):
184
+ del kwargs
185
+ if verbose > 1:
186
+ warnings.warn(f'Using nn.init.kaiming_normal_ init fn with parameters: ' + f'a={init_gain}, mode={fan_mode}, nonlinearity={init_nonlinearity}')
187
+ kaiming_normal_ = partial(torch.nn.init.kaiming_normal_, a=init_gain, mode=fan_mode, nonlinearity=init_nonlinearity)
188
+ generic_param_init_fn_(module=module, init_fn_=kaiming_normal_, d_model=d_model, n_layers=n_layers, init_div_is_residual=init_div_is_residual, emb_init_std=emb_init_std, emb_init_uniform_lim=emb_init_uniform_lim, verbose=verbose)
189
+
190
+ def xavier_uniform_param_init_fn_(module: nn.Module, n_layers: int, d_model: Optional[int]=None, init_div_is_residual: Union[int, float, str, bool]=True, emb_init_std: Optional[float]=None, emb_init_uniform_lim: Optional[Union[Tuple[float, float], float]]=None, init_gain: float=0, verbose: int=0, **kwargs: Any):
191
+ del kwargs
192
+ xavier_uniform_ = partial(torch.nn.init.xavier_uniform_, gain=init_gain)
193
+ if verbose > 1:
194
+ warnings.warn(f'Using torch.nn.init.xavier_uniform_ init fn with parameters: ' + f'gain={init_gain}')
195
+ generic_param_init_fn_(module=module, init_fn_=xavier_uniform_, d_model=d_model, n_layers=n_layers, init_div_is_residual=init_div_is_residual, emb_init_std=emb_init_std, emb_init_uniform_lim=emb_init_uniform_lim, verbose=verbose)
196
+
197
+ def xavier_normal_param_init_fn_(module: nn.Module, n_layers: int, d_model: Optional[int]=None, init_div_is_residual: Union[int, float, str, bool]=True, emb_init_std: Optional[float]=None, emb_init_uniform_lim: Optional[Union[Tuple[float, float], float]]=None, init_gain: float=0, verbose: int=0, **kwargs: Any):
198
+ del kwargs
199
+ xavier_normal_ = partial(torch.nn.init.xavier_normal_, gain=init_gain)
200
+ if verbose > 1:
201
+ warnings.warn(f'Using torch.nn.init.xavier_normal_ init fn with parameters: ' + f'gain={init_gain}')
202
+ generic_param_init_fn_(module=module, init_fn_=xavier_normal_, d_model=d_model, n_layers=n_layers, init_div_is_residual=init_div_is_residual, emb_init_std=emb_init_std, emb_init_uniform_lim=emb_init_uniform_lim, verbose=verbose)
203
+ MODEL_INIT_REGISTRY = {'default_': torch_default_param_init_fn_, 'baseline_': baseline_param_init_fn_, 'kaiming_uniform_': kaiming_uniform_param_init_fn_, 'kaiming_normal_': kaiming_normal_param_init_fn_, 'neox_init_': neox_param_init_fn_, 'small_init_': small_param_init_fn_, 'xavier_uniform_': xavier_uniform_param_init_fn_, 'xavier_normal_': xavier_normal_param_init_fn_}
hf_model/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:de62858fe25a9e60cb97042ccb98279c6873e2fc78393eae2af947f5d7f24501
3
+ size 2622736021
hf_model/special_tokens_map.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "<|endoftext|>",
3
+ "eos_token": "<|endoftext|>",
4
+ "unk_token": "<|endoftext|>"
5
+ }
hf_model/tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
hf_model/tokenizer_config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "bos_token": "<|endoftext|>",
4
+ "clean_up_tokenization_spaces": true,
5
+ "eos_token": "<|endoftext|>",
6
+ "model_max_length": 8192,
7
+ "tokenizer_class": "GPTNeoXTokenizer",
8
+ "unk_token": "<|endoftext|>"
9
+ }