TheBloke commited on
Commit
46bbb84
1 Parent(s): 95e44eb

Upload folder using huggingface_hub

Browse files
config.json ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "/workspace/models/ehartford_WizardLM-Uncensored-Falcon-7b",
3
+ "alibi": false,
4
+ "apply_residual_connection_post_layernorm": false,
5
+ "architectures": [
6
+ "RWForCausalLM"
7
+ ],
8
+ "attention_dropout": 0.0,
9
+ "auto_map": {
10
+ "AutoConfig": "configuration_RW.RWConfig",
11
+ "AutoModelForCausalLM": "modelling_RW.RWForCausalLM"
12
+ },
13
+ "bias": false,
14
+ "bos_token_id": 1,
15
+ "eos_token_id": 2,
16
+ "hidden_dropout": 0.0,
17
+ "hidden_size": 4544,
18
+ "initializer_range": 0.02,
19
+ "layer_norm_epsilon": 1e-05,
20
+ "model_type": "RefinedWebModel",
21
+ "multi_query": true,
22
+ "n_head": 71,
23
+ "n_layer": 32,
24
+ "parallel_attn": true,
25
+ "torch_dtype": "bfloat16",
26
+ "transformers_version": "4.29.2",
27
+ "use_cache": true,
28
+ "vocab_size": 65025
29
+ }
configuration_RW.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 the Big Science Workshop and HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """ Bloom configuration"""
16
+ from transformers.configuration_utils import PretrainedConfig
17
+ from transformers.utils import logging
18
+
19
+
20
+ logger = logging.get_logger(__name__)
21
+
22
+
23
+ class RWConfig(PretrainedConfig):
24
+ model_type = "RefinedWebModel"
25
+ keys_to_ignore_at_inference = ["past_key_values"]
26
+ attribute_map = {
27
+ "num_hidden_layers": "n_layer",
28
+ "num_attention_heads": "n_head",
29
+ }
30
+
31
+ def __init__(
32
+ self,
33
+ vocab_size=250880,
34
+ hidden_size=64,
35
+ n_layer=2,
36
+ n_head=8,
37
+ layer_norm_epsilon=1e-5,
38
+ initializer_range=0.02,
39
+ use_cache=True,
40
+ bos_token_id=1,
41
+ eos_token_id=2,
42
+ apply_residual_connection_post_layernorm=False,
43
+ hidden_dropout=0.0,
44
+ attention_dropout=0.0,
45
+ multi_query=False,
46
+ alibi=False,
47
+ bias=False,
48
+ parallel_attn=False,
49
+ **kwargs,
50
+ ):
51
+ self.vocab_size = vocab_size
52
+ # Backward compatibility with n_embed kwarg
53
+ n_embed = kwargs.pop("n_embed", None)
54
+ self.hidden_size = hidden_size if n_embed is None else n_embed
55
+ self.n_layer = n_layer
56
+ self.n_head = n_head
57
+ self.layer_norm_epsilon = layer_norm_epsilon
58
+ self.initializer_range = initializer_range
59
+ self.use_cache = use_cache
60
+ self.apply_residual_connection_post_layernorm = apply_residual_connection_post_layernorm
61
+ self.hidden_dropout = hidden_dropout
62
+ self.attention_dropout = attention_dropout
63
+
64
+ self.bos_token_id = bos_token_id
65
+ self.eos_token_id = eos_token_id
66
+ self.multi_query = multi_query
67
+ self.alibi = alibi
68
+ self.bias = bias
69
+ self.parallel_attn = parallel_attn
70
+
71
+ super().__init__(bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
72
+
73
+ @property
74
+ def head_dim(self):
75
+ return self.hidden_size // self.n_head
76
+
77
+ @property
78
+ def rotary(self):
79
+ return not self.alibi
generation_config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 1,
4
+ "eos_token_id": 2,
5
+ "transformers_version": "4.30.0.dev0"
6
+ }
gptq_model-4bit-64g.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a179fa97d87866ff5d31bab89ec53ec9689663210bd415a2add978f211ca0e5b
3
+ size 4760495424
modelling_RW.py ADDED
@@ -0,0 +1,1100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # port of models described in RW
2
+ # We use the bloom model as a starting point for these model.
3
+ # Please refer to the bloom models for usage instructions.
4
+
5
+ import math
6
+ import warnings
7
+ from typing import Optional, Tuple, Union
8
+
9
+ import torch
10
+ import torch.utils.checkpoint
11
+ from torch import nn
12
+ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, LayerNorm, MSELoss
13
+ from torch.nn import functional as F
14
+
15
+ from transformers.modeling_outputs import (
16
+ BaseModelOutputWithPastAndCrossAttentions,
17
+ CausalLMOutputWithCrossAttentions,
18
+ QuestionAnsweringModelOutput,
19
+ SequenceClassifierOutputWithPast,
20
+ TokenClassifierOutput,
21
+ )
22
+ from transformers.modeling_utils import PreTrainedModel
23
+ from transformers.utils import logging
24
+ from .configuration_RW import RWConfig
25
+
26
+ logger = logging.get_logger(__name__)
27
+
28
+ # NOTE(Hesslow): Unfortunately we did not fuse matmul and bias during training, this means that there's one additional quantization to bfloat16 between the operations.
29
+ # In order not to degrade the quality of our HF-port, we keep these characteristics in the final model.
30
+ class Linear(nn.Linear):
31
+ def forward(self, input: torch.Tensor) -> torch.Tensor:
32
+ ret = input @ self.weight.T
33
+ if self.bias is None:
34
+ return ret
35
+ else:
36
+ return ret + self.bias
37
+
38
+
39
+ from einops import rearrange
40
+
41
+ # rotary pos emb helpers (torch.jit.script does not seem to support staticmethod...)
42
+ def rotate_half(x):
43
+ x1, x2 = x[..., : x.shape[-1] // 2], x[..., x.shape[-1] // 2 :]
44
+ return torch.cat((-x2, x1), dim=x1.ndim - 1) # dim=-1 triggers a bug in torch < 1.8.0
45
+
46
+
47
+ class RotaryEmbedding(torch.nn.Module):
48
+ """Implementation of RotaryEmbedding from GPT-NeoX.
49
+ This implementation is design to operate on queries and keys that are compatible with
50
+ [batch_size, n_heads_per_partition, seq_len, head_dim] (e.g. MinGPTAttention format).
51
+ """
52
+
53
+ def __init__(
54
+ self,
55
+ head_dim: int,
56
+ base=10000,
57
+ ):
58
+ super().__init__()
59
+ inv_freq = 1.0 / (base ** (torch.arange(0, head_dim, 2).float() / head_dim))
60
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
61
+ self.head_dim = head_dim
62
+ self.seq_len_cached = None
63
+ self.batch_size_cached = None
64
+ self.cos_cached: torch.Tensor | None = None
65
+ self.sin_cached: torch.Tensor | None = None
66
+
67
+ def cos_sin(
68
+ self,
69
+ seq_len: int,
70
+ device="cuda",
71
+ dtype=torch.bfloat16,
72
+ ) -> torch.Tensor:
73
+ if seq_len != self.seq_len_cached:
74
+ self.seq_len_cached = seq_len
75
+ t = torch.arange(seq_len, device=device).type_as(self.inv_freq)
76
+ freqs = torch.einsum("i,j->ij", t, self.inv_freq)
77
+ emb = torch.cat((freqs, freqs), dim=-1).to(device)
78
+
79
+ if dtype in [torch.float16, torch.bfloat16]:
80
+ emb = emb.float()
81
+
82
+ self.cos_cached = emb.cos()[None, :, :]
83
+ self.sin_cached = emb.sin()[None, :, :]
84
+
85
+ self.cos_cached = self.cos_cached.type(dtype)
86
+ self.sin_cached = self.sin_cached.type(dtype)
87
+
88
+ return self.cos_cached, self.sin_cached
89
+
90
+ def forward(self, q, k):
91
+ batch, seq_len, head_dim = q.shape
92
+ cos, sin = self.cos_sin(seq_len, q.device)
93
+ return (q * cos) + (rotate_half(q) * sin), (k * cos) + (rotate_half(k) * sin)
94
+
95
+
96
+ def _make_causal_mask(
97
+ input_ids_shape: torch.Size, device: torch.device, past_key_values_length: int
98
+ ) -> torch.BoolTensor:
99
+ batch_size, target_length = input_ids_shape
100
+ mask = torch.empty((target_length, target_length + past_key_values_length), dtype=torch.bool, device=device)
101
+ # ONNX doesn't support `torch.Tensor.triu` properly, thus we use this workaround
102
+ seq_ids = torch.arange(target_length, device=device)
103
+ mask[:, past_key_values_length:] = seq_ids[:, None] < seq_ids[None, :]
104
+
105
+ if past_key_values_length > 0:
106
+ mask[:, :past_key_values_length] = False
107
+
108
+ expanded_mask = mask[None, None, :, :].expand(batch_size, 1, target_length, target_length + past_key_values_length)
109
+ return expanded_mask
110
+
111
+
112
+ def _expand_mask(mask: torch.Tensor, tgt_length: int) -> torch.BoolTensor:
113
+ batch_size, src_length = mask.shape
114
+ tgt_length = tgt_length if tgt_length is not None else src_length
115
+
116
+ expanded_mask = ~(mask[:, None, None, :].to(torch.bool))
117
+ return expanded_mask.expand(batch_size, 1, tgt_length, src_length)
118
+
119
+
120
+ def build_alibi_tensor(attention_mask: torch.Tensor, num_heads: int, dtype: torch.dtype) -> torch.Tensor:
121
+ batch_size, seq_length = attention_mask.shape
122
+ closest_power_of_2 = 2 ** math.floor(math.log2(num_heads))
123
+ base = torch.tensor(
124
+ 2 ** (-(2 ** -(math.log2(closest_power_of_2) - 3))), device=attention_mask.device, dtype=torch.float32
125
+ )
126
+ powers = torch.arange(1, 1 + closest_power_of_2, device=attention_mask.device, dtype=torch.int32)
127
+ slopes = torch.pow(base, powers)
128
+
129
+ if closest_power_of_2 != num_heads:
130
+ extra_base = torch.tensor(
131
+ 2 ** (-(2 ** -(math.log2(2 * closest_power_of_2) - 3))), device=attention_mask.device, dtype=torch.float32
132
+ )
133
+ num_remaining_heads = min(closest_power_of_2, num_heads - closest_power_of_2)
134
+ extra_powers = torch.arange(1, 1 + 2 * num_remaining_heads, 2, device=attention_mask.device, dtype=torch.int32)
135
+ slopes = torch.cat([slopes, torch.pow(extra_base, extra_powers)], dim=0)
136
+
137
+ # Note: alibi will added to the attention bias that will be applied to the query, key product of attention
138
+ # => therefore alibi will have to be of shape (batch_size, num_heads, query_length, key_length)
139
+ # => here we set (batch_size=1, num_heads=num_heads, query_length=1, key_length=max_length)
140
+ # => the query_length dimension will then be broadcasted correctly
141
+ # This is more or less identical to T5's relative position bias:
142
+ # https://github.com/huggingface/transformers/blob/f681437203baa7671de3174b0fa583c349d9d5e1/src/transformers/models/t5/modeling_t5.py#L527
143
+ arange_tensor = ((attention_mask.cumsum(dim=-1) - 1) * attention_mask)[:, None, :]
144
+ alibi = slopes[..., None].bfloat16() * arange_tensor
145
+ return alibi.reshape(batch_size * num_heads, 1, seq_length).to(dtype)
146
+
147
+
148
+ def dropout_add(x: torch.Tensor, residual: torch.Tensor, prob: float, training: bool) -> torch.Tensor:
149
+ out = F.dropout(x, p=prob, training=training)
150
+ out = residual + out
151
+ return out
152
+
153
+
154
+ class Attention(nn.Module):
155
+ def __init__(self, config: RWConfig):
156
+ super().__init__()
157
+
158
+ self.hidden_size = config.hidden_size
159
+ self.num_heads = config.n_head
160
+ self.head_dim = self.hidden_size // self.num_heads
161
+ self.split_size = self.hidden_size
162
+ self.hidden_dropout = config.hidden_dropout
163
+
164
+ if self.head_dim * self.num_heads != self.hidden_size:
165
+ raise ValueError(
166
+ f"`hidden_size` must be divisible by num_heads (got `hidden_size`: {self.hidden_size} and `num_heads`:"
167
+ f" {self.num_heads})."
168
+ )
169
+
170
+ self.maybe_rotary = RotaryEmbedding(config.head_dim) if config.rotary else lambda q, k: (q, k)
171
+
172
+ # Layer-wise attention scaling
173
+ self.inv_norm_factor = 1.0 / math.sqrt(self.head_dim)
174
+ self.beta = self.inv_norm_factor
175
+
176
+ self.query_key_value = Linear(
177
+ self.hidden_size,
178
+ 3 * self.hidden_size if not config.multi_query else (self.hidden_size + 2 * self.head_dim),
179
+ bias=config.bias,
180
+ )
181
+ self.multi_query = config.multi_query
182
+ self.dense = Linear(self.hidden_size, self.hidden_size, bias=config.bias)
183
+ self.attention_dropout = nn.Dropout(config.attention_dropout)
184
+ self.num_kv = config.n_head if not self.multi_query else 1
185
+
186
+ def _split_heads(self, fused_qkv: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
187
+ """
188
+ Split the last dimension into (num_heads, head_dim) without making any copies, results share same memory
189
+ storage as `fused_qkv`
190
+
191
+ Args:
192
+ fused_qkv (`torch.tensor`, *required*): [batch_size, seq_length, num_heads * 3 * head_dim]
193
+
194
+ Returns:
195
+ query: [batch_size, seq_length, num_heads, head_dim] key: [batch_size, seq_length, num_heads, head_dim]
196
+ value: [batch_size, seq_length, num_heads, head_dim]
197
+ """
198
+ if not self.multi_query:
199
+ batch_size, seq_length, three_times_hidden_size = fused_qkv.shape
200
+ fused_qkv = fused_qkv.view(batch_size, seq_length, self.num_heads, 3, self.head_dim)
201
+ return fused_qkv[..., 0, :], fused_qkv[..., 1, :], fused_qkv[..., 2, :]
202
+ else:
203
+ batch_size, seq_length, three_times_hidden_size = fused_qkv.shape
204
+ fused_qkv = fused_qkv.view(batch_size, seq_length, self.num_heads + 2, self.head_dim)
205
+ return fused_qkv[..., :-2, :], fused_qkv[..., [-2], :], fused_qkv[..., [-1], :]
206
+
207
+ def _merge_heads(self, x: torch.Tensor) -> torch.Tensor:
208
+ """
209
+ Merge heads together over the last dimenstion
210
+
211
+ Args:
212
+ x: (`torch.tensor`, *required*): [batch_size * num_heads, seq_length, head_dim]
213
+
214
+ Returns:
215
+ torch.tensor: [batch_size, seq_length, num_heads * head_dim]
216
+ """
217
+ # What we want to achieve is:
218
+ # batch_size * num_heads, seq_length, head_dim -> batch_size, seq_length, num_heads * head_dim
219
+ batch_size_and_num_heads, seq_length, _ = x.shape
220
+ batch_size = batch_size_and_num_heads // self.num_heads
221
+
222
+ # First view to decompose the batch size
223
+ # batch_size * num_heads, seq_length, head_dim -> batch_size, num_heads, seq_length, head_dim
224
+ x = x.view(batch_size, self.num_heads, seq_length, self.head_dim)
225
+
226
+ # batch_size, num_heads, seq_length, head_dim -> batch_size, seq_length, num_heads, head_dim
227
+ x = x.permute(0, 2, 1, 3)
228
+
229
+ # batch_size, seq_length, num_heads, head_dim -> batch_size, seq_length, num_heads * head_dim
230
+ return x.reshape(batch_size, seq_length, self.num_heads * self.head_dim)
231
+
232
+ def forward(
233
+ self,
234
+ hidden_states: torch.Tensor,
235
+ alibi: torch.Tensor,
236
+ attention_mask: torch.Tensor,
237
+ layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
238
+ head_mask: Optional[torch.Tensor] = None,
239
+ use_cache: bool = False,
240
+ output_attentions: bool = False,
241
+ ):
242
+ fused_qkv = self.query_key_value(hidden_states) # [batch_size, seq_length, 3 x hidden_size]
243
+
244
+ # 3 x [batch_size, seq_length, num_heads, head_dim]
245
+ (query_layer, key_layer, value_layer) = self._split_heads(fused_qkv)
246
+
247
+ batch_size, q_length, _, _ = query_layer.shape
248
+
249
+ query_layer = query_layer.transpose(1, 2).reshape(batch_size * self.num_heads, q_length, self.head_dim)
250
+ key_layer = key_layer.transpose(1, 2).reshape(
251
+ batch_size * self.num_kv,
252
+ q_length,
253
+ self.head_dim,
254
+ )
255
+ value_layer = value_layer.transpose(1, 2).reshape(batch_size * self.num_kv, q_length, self.head_dim)
256
+
257
+ query_layer, key_layer = self.maybe_rotary(query_layer, key_layer)
258
+
259
+ if layer_past is not None:
260
+ past_key, past_value = layer_past
261
+ # concatenate along seq_length dimension:
262
+ # - key: [batch_size * self.num_heads, head_dim, kv_length]
263
+ # - value: [batch_size * self.num_heads, kv_length, head_dim]
264
+ key_layer = torch.cat((past_key, key_layer), dim=1)
265
+ value_layer = torch.cat((past_value, value_layer), dim=1)
266
+
267
+ _, kv_length, _ = key_layer.shape
268
+
269
+ if use_cache is True:
270
+ present = (key_layer, value_layer)
271
+ else:
272
+ present = None
273
+
274
+ if alibi is None:
275
+ query_layer_ = query_layer.reshape(batch_size, self.num_heads, -1, self.head_dim)
276
+ key_layer_ = key_layer.reshape(batch_size, self.num_kv, -1, self.head_dim)
277
+ value_layer_ = value_layer.reshape(batch_size, self.num_kv, -1, self.head_dim)
278
+
279
+ attn_output = F.scaled_dot_product_attention(
280
+ query_layer_, key_layer_, value_layer_, None, 0.0, is_causal=True
281
+ )
282
+
283
+ x = attn_output.view(batch_size, self.num_heads, q_length, self.head_dim)
284
+ x = x.permute(0, 2, 1, 3)
285
+ attn_output = x.reshape(batch_size, q_length, self.num_heads * self.head_dim)
286
+
287
+ output_tensor = self.dense(attn_output)
288
+
289
+ outputs = (output_tensor, present)
290
+ assert not output_attentions # not supported.
291
+ return outputs
292
+ else:
293
+ attention_mask_float = (attention_mask * 1.0).masked_fill(attention_mask, -1e9).to(torch.bfloat16)
294
+ matmul_result = query_layer @ key_layer.transpose(-1, -2)
295
+
296
+ # change view to [batch_size, num_heads, q_length, kv_length]
297
+ attention_scores = matmul_result.view(batch_size, self.num_heads, q_length, kv_length)
298
+
299
+ # cast attention scores to fp32, compute scaled softmax and cast back to initial dtype - [batch_size, num_heads, q_length, kv_length]
300
+ input_dtype = attention_scores.dtype
301
+ # `float16` has a minimum value of -65504.0, whereas `bfloat16` and `float32` have a minimum value of `-3.4e+38`
302
+ if input_dtype == torch.float16 or input_dtype == torch.bfloat16:
303
+ attention_scores = attention_scores.to(torch.float32)
304
+ # attn_weights = torch.masked_fill(attention_scores, attention_mask, torch.finfo(attention_scores.dtype).min)
305
+ attention_probs = F.softmax(
306
+ (attention_scores + alibi.view(batch_size, self.num_heads, 1, -1)) * self.inv_norm_factor + attention_mask_float,
307
+ dim=-1,
308
+ dtype=hidden_states.dtype,
309
+ )
310
+ # [batch_size, num_heads, q_length, kv_length]
311
+ attention_probs = self.attention_dropout(attention_probs)
312
+
313
+ if head_mask is not None:
314
+ attention_probs = attention_probs * head_mask
315
+
316
+ # change view [batch_size x num_heads, q_length, kv_length]
317
+ attention_probs_reshaped = attention_probs.view(batch_size * self.num_heads, q_length, kv_length)
318
+
319
+ # matmul: [batch_size * num_heads, q_length, head_dim]
320
+ context_layer = attention_probs_reshaped @ value_layer
321
+
322
+ # change view [batch_size, num_heads, q_length, head_dim]
323
+ context_layer = self._merge_heads(context_layer)
324
+
325
+ output_tensor = self.dense(context_layer)
326
+
327
+ outputs = (output_tensor, present)
328
+ if output_attentions:
329
+ outputs += (attention_probs,)
330
+
331
+ return outputs
332
+
333
+
334
+ class MLP(nn.Module):
335
+ def __init__(self, config: RWConfig):
336
+ super().__init__()
337
+ hidden_size = config.hidden_size
338
+
339
+ self.dense_h_to_4h = Linear(hidden_size, 4 * hidden_size, bias=config.bias)
340
+ self.act = nn.GELU()
341
+ self.dense_4h_to_h = Linear(4 * hidden_size, hidden_size, bias=config.bias)
342
+ self.hidden_dropout = config.hidden_dropout
343
+
344
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
345
+ x = self.act(self.dense_h_to_4h(x))
346
+ x = self.dense_4h_to_h(x)
347
+ return x
348
+
349
+
350
+ class DecoderLayer(nn.Module):
351
+ def __init__(self, config: RWConfig):
352
+ super().__init__()
353
+ hidden_size = config.hidden_size
354
+
355
+ self.input_layernorm = LayerNorm(hidden_size, eps=config.layer_norm_epsilon)
356
+ self.num_heads = config.n_head
357
+ self.self_attention = Attention(config)
358
+
359
+ if not config.parallel_attn:
360
+ # unused if parallel attn
361
+ self.post_attention_layernorm = LayerNorm(hidden_size, eps=config.layer_norm_epsilon)
362
+
363
+ self.mlp = MLP(config)
364
+
365
+ self.apply_residual_connection_post_layernorm = config.apply_residual_connection_post_layernorm
366
+ self.hidden_dropout = config.hidden_dropout
367
+
368
+ self.config = config
369
+
370
+ def forward(
371
+ self,
372
+ hidden_states: torch.Tensor,
373
+ alibi: torch.Tensor,
374
+ attention_mask: torch.Tensor,
375
+ layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
376
+ head_mask: Optional[torch.Tensor] = None,
377
+ use_cache: bool = False,
378
+ output_attentions: bool = False,
379
+ ):
380
+
381
+ layernorm_output = self.input_layernorm(hidden_states)
382
+ residual = hidden_states
383
+
384
+ # Self attention.
385
+ attn_outputs = self.self_attention(
386
+ layernorm_output,
387
+ layer_past=layer_past,
388
+ attention_mask=attention_mask,
389
+ alibi=alibi,
390
+ head_mask=head_mask,
391
+ use_cache=use_cache,
392
+ output_attentions=output_attentions,
393
+ )
394
+
395
+ attention_output = attn_outputs[0]
396
+
397
+ if not self.config.parallel_attn:
398
+ residual = dropout_add(attention_output, residual, self.config.attention_dropout, training=self.training)
399
+ layernorm_output = self.post_attention_layernorm(residual)
400
+
401
+ outputs = attn_outputs[1:]
402
+
403
+ # MLP.
404
+ mlp_output = self.mlp(layernorm_output)
405
+
406
+ if self.config.parallel_attn:
407
+ mlp_output += attention_output
408
+
409
+ output = dropout_add(mlp_output, residual, self.config.hidden_dropout, training=self.training)
410
+
411
+ if use_cache:
412
+ outputs = (output,) + outputs
413
+ else:
414
+ outputs = (output,) + outputs[1:]
415
+
416
+ return outputs # hidden_states, present, attentions
417
+
418
+
419
+ class RWPreTrainedModel(PreTrainedModel):
420
+ _keys_to_ignore_on_load_missing = [r"h.*.self_attention.scale_mask_softmax.causal_mask", r"lm_head.weight"]
421
+ """
422
+ An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
423
+ models.
424
+ """
425
+
426
+ config_class = RWConfig
427
+ base_model_prefix = "transformer"
428
+ supports_gradient_checkpointing = True
429
+ _no_split_modules = ["DecoderLayer"]
430
+
431
+ def __init__(self, *inputs, **kwargs):
432
+ super().__init__(*inputs, **kwargs)
433
+
434
+ def _init_weights(self, module: nn.Module):
435
+ """Initialize the weights."""
436
+ if isinstance(module, nn.Linear) or isinstance(module, Linear):
437
+ # Slightly different from the TF version which uses truncated_normal for initialization
438
+ # cf https://github.com/pytorch/pytorch/pull/5617
439
+ module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
440
+ if module.bias is not None:
441
+ module.bias.data.zero_()
442
+ elif isinstance(module, nn.Embedding):
443
+ module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
444
+ if module.padding_idx is not None:
445
+ module.weight.data[module.padding_idx].zero_()
446
+ elif isinstance(module, LayerNorm):
447
+ module.bias.data.zero_()
448
+ module.weight.data.fill_(1.0)
449
+
450
+ def _set_gradient_checkpointing(self, module: nn.Module, value: bool = False):
451
+ if isinstance(module, RWModel):
452
+ module.gradient_checkpointing = value
453
+
454
+ @staticmethod
455
+ def _convert_to_standard_cache(
456
+ past_key_value: Tuple[Tuple[torch.Tensor, torch.Tensor]], batch_size: int
457
+ ) -> Tuple[Tuple[torch.Tensor, torch.Tensor]]:
458
+ """
459
+ Standardizes the format of the cache so as to match most implementations, i.e. to tuple(tuple([batch_size,
460
+ num_heads, ...]))
461
+ """
462
+ batch_size_times_num_heads, head_dim, seq_length = past_key_value[0][0].shape
463
+ num_heads = batch_size_times_num_heads // batch_size
464
+ # key: [batch_size * num_heads, head_dim, seq_length] -> [batch_size, num_heads, head_dim, seq_length]
465
+ # value: [batch_size * num_heads, seq_length, head_dim] -> [batch_size, num_heads, seq_length, head_dim]
466
+ return tuple(
467
+ (
468
+ layer_past[0].view(batch_size, num_heads, head_dim, seq_length),
469
+ layer_past[1].view(batch_size, num_heads, seq_length, head_dim),
470
+ )
471
+ for layer_past in past_key_value
472
+ )
473
+
474
+ @staticmethod
475
+ def _convert_to_rw_cache(
476
+ past_key_value: Tuple[Tuple[torch.Tensor, torch.Tensor]]
477
+ ) -> Tuple[Tuple[torch.Tensor, torch.Tensor]]:
478
+ batch_size, num_heads, head_dim, seq_length = past_key_value[0][0].shape
479
+ batch_size_times_num_heads = batch_size * num_heads
480
+ # key: [batch_size, num_heads, head_dim, seq_length] -> [batch_size * num_heads, head_dim, seq_length]
481
+ # value: [batch_size, num_heads, seq_length, head_dim] -> [batch_size * num_heads, seq_length, head_dim]
482
+ return tuple(
483
+ (
484
+ layer_past[0].view(batch_size_times_num_heads, head_dim, seq_length),
485
+ layer_past[1].view(batch_size_times_num_heads, seq_length, head_dim),
486
+ )
487
+ for layer_past in past_key_value
488
+ )
489
+
490
+
491
+ class RWModel(RWPreTrainedModel):
492
+ def __init__(self, config: RWConfig):
493
+ super().__init__(config)
494
+
495
+ self.embed_dim = config.hidden_size
496
+ self.num_heads = config.n_head
497
+ self.alibi = config.alibi
498
+
499
+ # Embedding + LN Embedding
500
+ self.word_embeddings = nn.Embedding(config.vocab_size, self.embed_dim)
501
+
502
+ # Transformer blocks
503
+ self.h = nn.ModuleList([DecoderLayer(config) for _ in range(config.num_hidden_layers)])
504
+
505
+ # Final Layer Norm
506
+ self.ln_f = LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon)
507
+
508
+ self.gradient_checkpointing = False
509
+
510
+ # Initialize weights and apply final processing
511
+ self.post_init()
512
+
513
+ def get_input_embeddings(self):
514
+ return self.word_embeddings
515
+
516
+ def _prepare_attn_mask(
517
+ self, attention_mask: torch.Tensor, input_shape: Tuple[int, int], past_key_values_length: int
518
+ ) -> torch.BoolTensor:
519
+ # create causal mask
520
+ # [batch_size, seq_length] -> [batch_size, 1, tgt_length, src_length]
521
+ combined_attention_mask = None
522
+ device = attention_mask.device
523
+ _, src_length = input_shape
524
+
525
+ if src_length > 1:
526
+ combined_attention_mask = _make_causal_mask(
527
+ input_shape, device=device, past_key_values_length=past_key_values_length
528
+ )
529
+
530
+ # [batch_size, seq_length] -> [batch_size, 1, tgt_length, src_length]
531
+ expanded_attn_mask = _expand_mask(attention_mask, tgt_length=src_length)
532
+ combined_attention_mask = (
533
+ expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask | combined_attention_mask
534
+ )
535
+
536
+ return combined_attention_mask
537
+
538
+ def set_input_embeddings(self, new_embeddings: torch.Tensor):
539
+ self.word_embeddings = new_embeddings
540
+
541
+ def forward(
542
+ self,
543
+ input_ids: Optional[torch.LongTensor] = None,
544
+ past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
545
+ attention_mask: Optional[torch.Tensor] = None,
546
+ head_mask: Optional[torch.LongTensor] = None,
547
+ inputs_embeds: Optional[torch.LongTensor] = None,
548
+ use_cache: Optional[bool] = None,
549
+ output_attentions: Optional[bool] = None,
550
+ output_hidden_states: Optional[bool] = None,
551
+ return_dict: Optional[bool] = None,
552
+ **deprecated_arguments,
553
+ ) -> Union[Tuple[torch.Tensor, ...], BaseModelOutputWithPastAndCrossAttentions]:
554
+ if deprecated_arguments.pop("position_ids", False) is not False:
555
+ # `position_ids` could have been `torch.Tensor` or `None` so defaulting pop to `False` allows to detect if users were passing explicitly `None`
556
+ warnings.warn(
557
+ "`position_ids` have no functionality in BLOOM and will be removed in v5.0.0. You can safely ignore"
558
+ " passing `position_ids`.",
559
+ FutureWarning,
560
+ )
561
+ if len(deprecated_arguments) > 0:
562
+ raise ValueError(f"Got unexpected arguments: {deprecated_arguments}")
563
+
564
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
565
+ output_hidden_states = (
566
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
567
+ )
568
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
569
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
570
+
571
+ if input_ids is not None and inputs_embeds is not None:
572
+ raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
573
+ elif input_ids is not None:
574
+ batch_size, seq_length = input_ids.shape
575
+ elif inputs_embeds is not None:
576
+ batch_size, seq_length, _ = inputs_embeds.shape
577
+ else:
578
+ raise ValueError("You have to specify either input_ids or inputs_embeds")
579
+
580
+ if past_key_values is None:
581
+ past_key_values = tuple([None] * len(self.h))
582
+
583
+ # Prepare head mask if needed
584
+ # 1.0 in head_mask indicate we keep the head
585
+ # attention_probs has shape batch_size x num_heads x N x N
586
+ # head_mask has shape n_layer x batch x num_heads x N x N
587
+ head_mask = self.get_head_mask(head_mask, self.config.n_layer)
588
+
589
+ if inputs_embeds is None:
590
+ inputs_embeds = self.word_embeddings(input_ids)
591
+
592
+ hidden_states = inputs_embeds
593
+
594
+ presents = () if use_cache else None
595
+ all_self_attentions = () if output_attentions else None
596
+ all_hidden_states = () if output_hidden_states else None
597
+
598
+ # Compute alibi tensor: check build_alibi_tensor documentation
599
+ seq_length_with_past = seq_length
600
+ past_key_values_length = 0
601
+ if past_key_values[0] is not None:
602
+ past_key_values_length = past_key_values[0][0].shape[2]
603
+ seq_length_with_past = seq_length_with_past + past_key_values_length
604
+ if attention_mask is None:
605
+ attention_mask = torch.ones((batch_size, seq_length_with_past), device=hidden_states.device)
606
+ else:
607
+ attention_mask = attention_mask.to(hidden_states.device)
608
+
609
+ if self.alibi:
610
+ alibi = build_alibi_tensor(attention_mask, self.num_heads, dtype=hidden_states.dtype)
611
+ else:
612
+ alibi = None
613
+
614
+ causal_mask = self._prepare_attn_mask(
615
+ attention_mask,
616
+ input_shape=(batch_size, seq_length),
617
+ past_key_values_length=past_key_values_length,
618
+ )
619
+
620
+ for i, (block, layer_past) in enumerate(zip(self.h, past_key_values)):
621
+
622
+ if output_hidden_states:
623
+ all_hidden_states = all_hidden_states + (hidden_states,)
624
+
625
+ if self.gradient_checkpointing and self.training:
626
+
627
+ if use_cache:
628
+ logger.warning(
629
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
630
+ )
631
+ use_cache = False
632
+
633
+ def create_custom_forward(module):
634
+ def custom_forward(*inputs):
635
+ # None for past_key_value
636
+ return module(*inputs, use_cache=use_cache, output_attentions=output_attentions)
637
+
638
+ return custom_forward
639
+
640
+ outputs = torch.utils.checkpoint.checkpoint(
641
+ create_custom_forward(block),
642
+ hidden_states,
643
+ alibi,
644
+ causal_mask,
645
+ head_mask[i],
646
+ )
647
+ else:
648
+ outputs = block(
649
+ hidden_states,
650
+ layer_past=layer_past,
651
+ attention_mask=causal_mask,
652
+ head_mask=head_mask[i],
653
+ use_cache=use_cache,
654
+ output_attentions=output_attentions,
655
+ alibi=alibi,
656
+ )
657
+
658
+ hidden_states = outputs[0]
659
+ if use_cache is True:
660
+ presents = presents + (outputs[1],)
661
+
662
+ if output_attentions:
663
+ all_self_attentions = all_self_attentions + (outputs[2 if use_cache else 1],)
664
+
665
+ # Add last hidden state
666
+ hidden_states = self.ln_f(hidden_states)
667
+
668
+ if output_hidden_states:
669
+ all_hidden_states = all_hidden_states + (hidden_states,)
670
+
671
+ if not return_dict:
672
+ return tuple(v for v in [hidden_states, presents, all_hidden_states, all_self_attentions] if v is not None)
673
+
674
+ return BaseModelOutputWithPastAndCrossAttentions(
675
+ last_hidden_state=hidden_states,
676
+ past_key_values=presents,
677
+ hidden_states=all_hidden_states,
678
+ attentions=all_self_attentions,
679
+ )
680
+
681
+
682
+ class RWForCausalLM(RWPreTrainedModel):
683
+ _keys_to_ignore_on_load_missing = [r"h.*.self_attention.scale_mask_softmax.causal_mask", r"lm_head.weight"]
684
+
685
+ def __init__(self, config: RWConfig):
686
+ super().__init__(config)
687
+ self.transformer = RWModel(config)
688
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
689
+
690
+ # Initialize weights and apply final processing
691
+ self.post_init()
692
+
693
+ def get_output_embeddings(self):
694
+ return self.lm_head
695
+
696
+ def set_output_embeddings(self, new_embeddings: torch.Tensor):
697
+ self.lm_head = new_embeddings
698
+
699
+ def prepare_inputs_for_generation(
700
+ self,
701
+ input_ids: torch.LongTensor,
702
+ past: Optional[torch.Tensor] = None,
703
+ attention_mask: Optional[torch.Tensor] = None,
704
+ **kwargs,
705
+ ) -> dict:
706
+ # only last token for input_ids if past is not None
707
+ if past:
708
+ input_ids = input_ids[:, -1].unsqueeze(-1)
709
+
710
+ # the cache may be in the stardard format (e.g. in contrastive search), convert to our's format if needed
711
+ if past[0][0].shape[0] == input_ids.shape[0]:
712
+ past = self._convert_to_rw_cache(past)
713
+
714
+ return {
715
+ "input_ids": input_ids,
716
+ "past_key_values": past,
717
+ "use_cache": kwargs.get("use_cache"),
718
+ "attention_mask": attention_mask,
719
+ }
720
+
721
+ def forward(
722
+ self,
723
+ input_ids: Optional[torch.LongTensor] = None,
724
+ past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
725
+ attention_mask: Optional[torch.Tensor] = None,
726
+ head_mask: Optional[torch.Tensor] = None,
727
+ inputs_embeds: Optional[torch.Tensor] = None,
728
+ labels: Optional[torch.Tensor] = None,
729
+ use_cache: Optional[bool] = None,
730
+ output_attentions: Optional[bool] = None,
731
+ output_hidden_states: Optional[bool] = None,
732
+ return_dict: Optional[bool] = None,
733
+ **deprecated_arguments,
734
+ ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]:
735
+ r"""
736
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
737
+ Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set
738
+ `labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100`
739
+ are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size]`
740
+ """
741
+ if deprecated_arguments.pop("position_ids", False) is not False:
742
+ # `position_ids` could have been `torch.Tensor` or `None` so defaulting pop to `False` allows to detect if users were passing explicitly `None`
743
+ warnings.warn(
744
+ "`position_ids` have no functionality in BLOOM and will be removed in v5.0.0. You can safely ignore"
745
+ " passing `position_ids`.",
746
+ FutureWarning,
747
+ )
748
+ if len(deprecated_arguments) > 0:
749
+ raise ValueError(f"Got unexpected arguments: {deprecated_arguments}")
750
+
751
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
752
+
753
+ transformer_outputs = self.transformer(
754
+ input_ids,
755
+ past_key_values=past_key_values,
756
+ attention_mask=attention_mask,
757
+ head_mask=head_mask,
758
+ inputs_embeds=inputs_embeds,
759
+ use_cache=use_cache,
760
+ output_attentions=output_attentions,
761
+ output_hidden_states=output_hidden_states,
762
+ return_dict=return_dict,
763
+ )
764
+ hidden_states = transformer_outputs[0]
765
+
766
+ lm_logits = self.lm_head(hidden_states)
767
+
768
+ loss = None
769
+ if labels is not None:
770
+ # Shift so that tokens < n predict n
771
+ shift_logits = lm_logits[..., :-1, :].contiguous()
772
+ shift_labels = labels[..., 1:].contiguous()
773
+ batch_size, seq_length, vocab_size = shift_logits.shape
774
+ # Flatten the tokens
775
+ loss_fct = CrossEntropyLoss()
776
+ loss = loss_fct(
777
+ shift_logits.view(batch_size * seq_length, vocab_size), shift_labels.view(batch_size * seq_length)
778
+ )
779
+
780
+ if not return_dict:
781
+ output = (lm_logits,) + transformer_outputs[1:]
782
+ return ((loss,) + output) if loss is not None else output
783
+
784
+ return CausalLMOutputWithCrossAttentions(
785
+ loss=loss,
786
+ logits=lm_logits,
787
+ past_key_values=transformer_outputs.past_key_values,
788
+ hidden_states=transformer_outputs.hidden_states,
789
+ attentions=transformer_outputs.attentions,
790
+ )
791
+
792
+ def _reorder_cache(
793
+ self, past: Tuple[Tuple[torch.Tensor, torch.Tensor], ...], beam_idx: torch.LongTensor
794
+ ) -> Tuple[Tuple[torch.Tensor, torch.Tensor], ...]:
795
+ """
796
+ This function is used to re-order the `past_key_values` cache if [`~PreTrainedModel.beam_search`] or
797
+ [`~PreTrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct
798
+ beam_idx at every generation step.
799
+
800
+ Output shares the same memory storage as `past`.
801
+ """
802
+ standardized_past = self._convert_to_standard_cache(past, batch_size=len(beam_idx))
803
+
804
+ # Get a copy of `beam_idx` on all the devices where we need those indices.
805
+ device_to_beam_idx = {
806
+ past_state.device: beam_idx.to(past_state.device) for layer_past in past for past_state in layer_past
807
+ }
808
+ reordered_past = tuple(
809
+ (
810
+ layer_past[0].index_select(0, device_to_beam_idx[layer_past[0].device]),
811
+ layer_past[1].index_select(0, device_to_beam_idx[layer_past[0].device]),
812
+ )
813
+ for layer_past in standardized_past
814
+ )
815
+ return self._convert_to_rw_cache(reordered_past)
816
+
817
+
818
+ class RWForSequenceClassification(RWPreTrainedModel):
819
+ _keys_to_ignore_on_load_missing = [r"h.*.self_attention.scale_mask_softmax.causal_mask", r"lm_head.weight"]
820
+
821
+ def __init__(self, config: RWConfig):
822
+ super().__init__(config)
823
+ self.num_labels = config.num_labels
824
+ self.transformer = RWModel(config)
825
+ self.score = nn.Linear(config.hidden_size, config.num_labels, bias=False)
826
+
827
+ # Initialize weights and apply final processing
828
+ self.post_init()
829
+
830
+ def forward(
831
+ self,
832
+ input_ids: Optional[torch.LongTensor] = None,
833
+ past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
834
+ attention_mask: Optional[torch.Tensor] = None,
835
+ head_mask: Optional[torch.Tensor] = None,
836
+ inputs_embeds: Optional[torch.Tensor] = None,
837
+ labels: Optional[torch.Tensor] = None,
838
+ use_cache: Optional[bool] = None,
839
+ output_attentions: Optional[bool] = None,
840
+ output_hidden_states: Optional[bool] = None,
841
+ return_dict: Optional[bool] = None,
842
+ **deprecated_arguments,
843
+ ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutputWithPast]:
844
+ r"""
845
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
846
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
847
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
848
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
849
+ """
850
+ if deprecated_arguments.pop("position_ids", False) is not False:
851
+ # `position_ids` could have been `torch.Tensor` or `None` so defaulting pop to `False` allows to detect if users were passing explicitly `None`
852
+ warnings.warn(
853
+ "`position_ids` have no functionality in BLOOM and will be removed in v5.0.0. You can safely ignore"
854
+ " passing `position_ids`.",
855
+ FutureWarning,
856
+ )
857
+ if len(deprecated_arguments) > 0:
858
+ raise ValueError(f"Got unexpected arguments: {deprecated_arguments}")
859
+
860
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
861
+
862
+ transformer_outputs = self.transformer(
863
+ input_ids,
864
+ past_key_values=past_key_values,
865
+ attention_mask=attention_mask,
866
+ head_mask=head_mask,
867
+ inputs_embeds=inputs_embeds,
868
+ use_cache=use_cache,
869
+ output_attentions=output_attentions,
870
+ output_hidden_states=output_hidden_states,
871
+ return_dict=return_dict,
872
+ )
873
+
874
+ hidden_states = transformer_outputs[0]
875
+ logits = self.score(hidden_states)
876
+
877
+ if input_ids is not None:
878
+ batch_size = input_ids.shape[0]
879
+ else:
880
+ batch_size = inputs_embeds.shape[0]
881
+
882
+ if self.config.pad_token_id is None and batch_size != 1:
883
+ raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
884
+ if self.config.pad_token_id is None:
885
+ sequence_lengths = -1
886
+ else:
887
+ if input_ids is not None:
888
+ sequence_lengths = torch.ne(input_ids, self.config.pad_token_id).sum(dim=-1) - 1
889
+ else:
890
+ sequence_lengths = -1
891
+ logger.warning(
892
+ f"{self.__class__.__name__} will not detect padding tokens in `inputs_embeds`. Results may be "
893
+ "unexpected if using padding tokens in conjunction with `inputs_embeds.`"
894
+ )
895
+
896
+ pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths]
897
+
898
+ loss = None
899
+ if labels is not None:
900
+ if self.config.problem_type is None:
901
+ if self.num_labels == 1:
902
+ self.config.problem_type = "regression"
903
+ elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
904
+ self.config.problem_type = "single_label_classification"
905
+ else:
906
+ self.config.problem_type = "multi_label_classification"
907
+
908
+ if self.config.problem_type == "regression":
909
+ loss_fct = MSELoss()
910
+ if self.num_labels == 1:
911
+ loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
912
+ else:
913
+ loss = loss_fct(pooled_logits, labels)
914
+ elif self.config.problem_type == "single_label_classification":
915
+ loss_fct = CrossEntropyLoss()
916
+ loss = loss_fct(pooled_logits, labels)
917
+ elif self.config.problem_type == "multi_label_classification":
918
+ loss_fct = BCEWithLogitsLoss()
919
+ loss = loss_fct(pooled_logits, labels)
920
+ if not return_dict:
921
+ output = (pooled_logits,) + transformer_outputs[1:]
922
+ return ((loss,) + output) if loss is not None else output
923
+
924
+ return SequenceClassifierOutputWithPast(
925
+ loss=loss,
926
+ logits=pooled_logits,
927
+ past_key_values=transformer_outputs.past_key_values,
928
+ hidden_states=transformer_outputs.hidden_states,
929
+ attentions=transformer_outputs.attentions,
930
+ )
931
+
932
+
933
+ class RWForTokenClassification(RWPreTrainedModel):
934
+ _keys_to_ignore_on_load_missing = [r"h.*.self_attention.scale_mask_softmax.causal_mask", r"lm_head.weight"]
935
+
936
+ def __init__(self, config: RWConfig):
937
+ super().__init__(config)
938
+ self.num_labels = config.num_labels
939
+
940
+ self.transformer = RWModel(config)
941
+ if hasattr(config, "classifier_dropout") and config.classifier_dropout is not None:
942
+ classifier_dropout = config.classifier_dropout
943
+ elif hasattr(config, "hidden_dropout") and config.hidden_dropout is not None:
944
+ classifier_dropout = config.hidden_dropout
945
+ else:
946
+ classifier_dropout = 0.1
947
+ self.dropout = nn.Dropout(classifier_dropout)
948
+ self.classifier = nn.Linear(config.hidden_size, config.num_labels)
949
+
950
+ # Initialize weights and apply final processing
951
+ self.post_init()
952
+
953
+ def forward(
954
+ self,
955
+ input_ids: Optional[torch.LongTensor] = None,
956
+ past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
957
+ attention_mask: Optional[torch.Tensor] = None,
958
+ head_mask: Optional[torch.Tensor] = None,
959
+ inputs_embeds: Optional[torch.Tensor] = None,
960
+ labels: Optional[torch.Tensor] = None,
961
+ use_cache: Optional[bool] = None,
962
+ output_attentions: Optional[bool] = None,
963
+ output_hidden_states: Optional[bool] = None,
964
+ return_dict: Optional[bool] = None,
965
+ **deprecated_arguments,
966
+ ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]:
967
+ r"""
968
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
969
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
970
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
971
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
972
+ """
973
+ if deprecated_arguments.pop("position_ids", False) is not False:
974
+ # `position_ids` could have been `torch.Tensor` or `None` so defaulting pop to `False` allows to detect if users were passing explicitly `None`
975
+ warnings.warn(
976
+ "`position_ids` have no functionality in BLOOM and will be removed in v5.0.0. You can safely ignore"
977
+ " passing `position_ids`.",
978
+ FutureWarning,
979
+ )
980
+ if len(deprecated_arguments) > 0:
981
+ raise ValueError(f"Got unexpected arguments: {deprecated_arguments}")
982
+
983
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
984
+
985
+ transformer_outputs = self.transformer(
986
+ input_ids,
987
+ past_key_values=past_key_values,
988
+ attention_mask=attention_mask,
989
+ head_mask=head_mask,
990
+ inputs_embeds=inputs_embeds,
991
+ use_cache=use_cache,
992
+ output_attentions=output_attentions,
993
+ output_hidden_states=output_hidden_states,
994
+ return_dict=return_dict,
995
+ )
996
+
997
+ hidden_states = transformer_outputs[0]
998
+ hidden_states = self.dropout(hidden_states)
999
+ logits = self.classifier(hidden_states)
1000
+
1001
+ loss = None
1002
+ if labels is not None:
1003
+ batch_size, seq_length = labels.shape
1004
+ loss_fct = CrossEntropyLoss()
1005
+ loss = loss_fct(logits.view(batch_size * seq_length, self.num_labels), labels.view(batch_size * seq_length))
1006
+
1007
+ if not return_dict:
1008
+ output = (logits,) + transformer_outputs[2:]
1009
+ return ((loss,) + output) if loss is not None else output
1010
+
1011
+ return TokenClassifierOutput(
1012
+ loss=loss,
1013
+ logits=logits,
1014
+ hidden_states=transformer_outputs.hidden_states,
1015
+ attentions=transformer_outputs.attentions,
1016
+ )
1017
+
1018
+
1019
+ class RWForQuestionAnswering(RWPreTrainedModel):
1020
+ _keys_to_ignore_on_load_missing = [r"h.*.self_attention.scale_mask_softmax.causal_mask", r"lm_head.weight"]
1021
+
1022
+ def __init__(self, config):
1023
+ super().__init__(config)
1024
+ self.transformer = RWModel(config)
1025
+ self.qa_outputs = nn.Linear(config.hidden_size, 2)
1026
+
1027
+ # Initialize weights and apply final processing
1028
+ self.post_init()
1029
+
1030
+ def forward(
1031
+ self,
1032
+ input_ids: Optional[torch.LongTensor] = None,
1033
+ attention_mask: Optional[torch.FloatTensor] = None,
1034
+ position_ids: Optional[torch.LongTensor] = None,
1035
+ head_mask: Optional[torch.FloatTensor] = None,
1036
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1037
+ start_positions: Optional[torch.LongTensor] = None,
1038
+ end_positions: Optional[torch.LongTensor] = None,
1039
+ output_attentions: Optional[bool] = None,
1040
+ output_hidden_states: Optional[bool] = None,
1041
+ return_dict: Optional[bool] = None,
1042
+ ) -> Union[Tuple, QuestionAnsweringModelOutput]:
1043
+ r"""
1044
+ start_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1045
+ Labels for position (index) of the start of the labelled span for computing the token classification loss.
1046
+ Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence
1047
+ are not taken into account for computing the loss.
1048
+ end_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1049
+ Labels for position (index) of the end of the labelled span for computing the token classification loss.
1050
+ Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence
1051
+ are not taken into account for computing the loss.
1052
+ """
1053
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1054
+
1055
+ outputs = self.transformer(
1056
+ input_ids,
1057
+ attention_mask=attention_mask,
1058
+ position_ids=position_ids,
1059
+ head_mask=head_mask,
1060
+ inputs_embeds=inputs_embeds,
1061
+ output_attentions=output_attentions,
1062
+ output_hidden_states=output_hidden_states,
1063
+ return_dict=return_dict,
1064
+ )
1065
+
1066
+ sequence_output = outputs[0]
1067
+
1068
+ logits = self.qa_outputs(sequence_output)
1069
+ start_logits, end_logits = logits.split(1, dim=-1)
1070
+ start_logits = start_logits.squeeze(-1).contiguous()
1071
+ end_logits = end_logits.squeeze(-1).contiguous()
1072
+
1073
+ total_loss = None
1074
+ if start_positions is not None and end_positions is not None:
1075
+ # If we are on multi-GPU, split add a dimension
1076
+ if len(start_positions.size()) > 1:
1077
+ start_positions = start_positions.squeeze(-1)
1078
+ if len(end_positions.size()) > 1:
1079
+ end_positions = end_positions.squeeze(-1)
1080
+ # sometimes the start/end positions are outside our model inputs, we ignore these terms
1081
+ ignored_index = start_logits.size(1)
1082
+ start_positions = start_positions.clamp(0, ignored_index)
1083
+ end_positions = end_positions.clamp(0, ignored_index)
1084
+
1085
+ loss_fct = CrossEntropyLoss(ignore_index=ignored_index)
1086
+ start_loss = loss_fct(start_logits, start_positions)
1087
+ end_loss = loss_fct(end_logits, end_positions)
1088
+ total_loss = (start_loss + end_loss) / 2
1089
+
1090
+ if not return_dict:
1091
+ output = (start_logits, end_logits) + outputs[2:]
1092
+ return ((total_loss,) + output) if total_loss is not None else output
1093
+
1094
+ return QuestionAnsweringModelOutput(
1095
+ loss=total_loss,
1096
+ start_logits=start_logits,
1097
+ end_logits=end_logits,
1098
+ hidden_states=outputs.hidden_states,
1099
+ attentions=outputs.attentions,
1100
+ )
quantize_config.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bits": 4,
3
+ "group_size": 64,
4
+ "damp_percent": 0.01,
5
+ "desc_act": false,
6
+ "sym": true,
7
+ "true_sequential": true,
8
+ "model_name_or_path": null,
9
+ "model_file_base_name": null
10
+ }
special_tokens_map.json ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "additional_special_tokens": [
3
+ ">>TITLE<<",
4
+ ">>ABSTRACT<<",
5
+ ">>INTRODUCTION<<",
6
+ ">>SUMMARY<<",
7
+ ">>COMMENT<<",
8
+ ">>ANSWER<<",
9
+ ">>QUESTION<<",
10
+ ">>DOMAIN<<",
11
+ ">>PREFIX<<",
12
+ ">>SUFFIX<<",
13
+ ">>MIDDLE<<"
14
+ ],
15
+ "eos_token": "<|endoftext|>",
16
+ "pad_token": "[PAD]"
17
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "clean_up_tokenization_spaces": true,
4
+ "eos_token": "<|endoftext|>",
5
+ "model_max_length": 2048,
6
+ "padding_side": "right",
7
+ "tokenizer_class": "PreTrainedTokenizerFast"
8
+ }
zero_to_fp32.py ADDED
@@ -0,0 +1,584 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright (c) Microsoft Corporation.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ # DeepSpeed Team
7
+
8
+ # This script extracts fp32 consolidated weights from a zero 2 and 3 DeepSpeed checkpoints. It gets
9
+ # copied into the top level checkpoint dir, so the user can easily do the conversion at any point in
10
+ # the future. Once extracted, the weights don't require DeepSpeed and can be used in any
11
+ # application.
12
+ #
13
+ # example: python zero_to_fp32.py . pytorch_model.bin
14
+
15
+ import argparse
16
+ import torch
17
+ import glob
18
+ import math
19
+ import os
20
+ import re
21
+ from collections import OrderedDict
22
+ from dataclasses import dataclass
23
+
24
+ # while this script doesn't use deepspeed to recover data, since the checkpoints are pickled with
25
+ # DeepSpeed data structures it has to be available in the current python environment.
26
+ from deepspeed.utils import logger
27
+ from deepspeed.checkpoint.constants import (DS_VERSION, OPTIMIZER_STATE_DICT, SINGLE_PARTITION_OF_FP32_GROUPS,
28
+ FP32_FLAT_GROUPS, ZERO_STAGE, PARTITION_COUNT, PARAM_SHAPES, BUFFER_NAMES,
29
+ FROZEN_PARAM_SHAPES, FROZEN_PARAM_FRAGMENTS)
30
+
31
+
32
+ @dataclass
33
+ class zero_model_state:
34
+ buffers: dict()
35
+ param_shapes: dict()
36
+ shared_params: list
37
+ ds_version: int
38
+ frozen_param_shapes: dict()
39
+ frozen_param_fragments: dict()
40
+
41
+
42
+ debug = 0
43
+
44
+ # load to cpu
45
+ device = torch.device('cpu')
46
+
47
+
48
+ def atoi(text):
49
+ return int(text) if text.isdigit() else text
50
+
51
+
52
+ def natural_keys(text):
53
+ '''
54
+ alist.sort(key=natural_keys) sorts in human order
55
+ http://nedbatchelder.com/blog/200712/human_sorting.html
56
+ (See Toothy's implementation in the comments)
57
+ '''
58
+ return [atoi(c) for c in re.split(r'(\d+)', text)]
59
+
60
+
61
+ def get_model_state_file(checkpoint_dir, zero_stage):
62
+ if not os.path.isdir(checkpoint_dir):
63
+ raise FileNotFoundError(f"Directory '{checkpoint_dir}' doesn't exist")
64
+
65
+ # there should be only one file
66
+ if zero_stage == 2:
67
+ file = os.path.join(checkpoint_dir, "mp_rank_00_model_states.pt")
68
+ elif zero_stage == 3:
69
+ file = os.path.join(checkpoint_dir, "zero_pp_rank_0_mp_rank_00_model_states.pt")
70
+
71
+ if not os.path.exists(file):
72
+ raise FileNotFoundError(f"can't find model states file at '{file}'")
73
+
74
+ return file
75
+
76
+
77
+ def get_checkpoint_files(checkpoint_dir, glob_pattern):
78
+ # XXX: need to test that this simple glob rule works for multi-node setup too
79
+ ckpt_files = sorted(glob.glob(os.path.join(checkpoint_dir, glob_pattern)), key=natural_keys)
80
+
81
+ if len(ckpt_files) == 0:
82
+ raise FileNotFoundError(f"can't find {glob_pattern} files in directory '{checkpoint_dir}'")
83
+
84
+ return ckpt_files
85
+
86
+
87
+ def get_optim_files(checkpoint_dir):
88
+ return get_checkpoint_files(checkpoint_dir, "*_optim_states.pt")
89
+
90
+
91
+ def get_model_state_files(checkpoint_dir):
92
+ return get_checkpoint_files(checkpoint_dir, "*_model_states.pt")
93
+
94
+
95
+ def parse_model_states(files):
96
+ zero_model_states = []
97
+ for file in files:
98
+ state_dict = torch.load(file, map_location=device)
99
+
100
+ if BUFFER_NAMES not in state_dict:
101
+ raise ValueError(f"{file} is not a model state checkpoint")
102
+ buffer_names = state_dict[BUFFER_NAMES]
103
+ if debug:
104
+ print("Found buffers:", buffer_names)
105
+
106
+ # recover just the buffers while restoring them to fp32 if they were saved in fp16
107
+ buffers = {k: v.float() for k, v in state_dict["module"].items() if k in buffer_names}
108
+ param_shapes = state_dict[PARAM_SHAPES]
109
+
110
+ # collect parameters that are included in param_shapes
111
+ param_names = []
112
+ for s in param_shapes:
113
+ for name in s.keys():
114
+ param_names.append(name)
115
+
116
+ # update with frozen parameters
117
+ frozen_param_shapes = state_dict.get(FROZEN_PARAM_SHAPES, None)
118
+ if frozen_param_shapes is not None:
119
+ if debug:
120
+ print(f"Found frozen_param_shapes: {frozen_param_shapes}")
121
+ param_names += list(frozen_param_shapes.keys())
122
+
123
+ # record shared parameters so that they can be recovered based on partners
124
+ # this is because such parameters holding reference only are not saved by optimizer
125
+ shared_params = []
126
+ for param in state_dict["module"]:
127
+ if param not in [*param_names, *buffer_names]:
128
+ for share_param in state_dict["module"]:
129
+ if (state_dict["module"][share_param].data_ptr() == state_dict["module"][param].data_ptr()
130
+ and share_param != param):
131
+ shared_params.append([param, share_param])
132
+ break
133
+
134
+ ds_version = state_dict.get(DS_VERSION, None)
135
+
136
+ frozen_param_fragments = state_dict.get(FROZEN_PARAM_FRAGMENTS, None)
137
+
138
+ z_model_state = zero_model_state(buffers=buffers,
139
+ param_shapes=param_shapes,
140
+ shared_params=shared_params,
141
+ ds_version=ds_version,
142
+ frozen_param_shapes=frozen_param_shapes,
143
+ frozen_param_fragments=frozen_param_fragments)
144
+ zero_model_states.append(z_model_state)
145
+
146
+ return zero_model_states
147
+
148
+
149
+ def parse_optim_states(files, ds_checkpoint_dir):
150
+
151
+ total_files = len(files)
152
+ state_dicts = []
153
+ for f in files:
154
+ state_dicts.append(torch.load(f, map_location=device))
155
+
156
+ if not ZERO_STAGE in state_dicts[0][OPTIMIZER_STATE_DICT]:
157
+ raise ValueError(f"{files[0]} is not a zero checkpoint")
158
+ zero_stage = state_dicts[0][OPTIMIZER_STATE_DICT][ZERO_STAGE]
159
+ world_size = state_dicts[0][OPTIMIZER_STATE_DICT][PARTITION_COUNT]
160
+
161
+ # For ZeRO-2 each param group can have different partition_count as data parallelism for expert
162
+ # parameters can be different from data parallelism for non-expert parameters. So we can just
163
+ # use the max of the partition_count to get the dp world_size.
164
+
165
+ if type(world_size) is list:
166
+ world_size = max(world_size)
167
+
168
+ if world_size != total_files:
169
+ raise ValueError(
170
+ f"Expected {world_size} of '*_optim_states.pt' under '{ds_checkpoint_dir}' but found {total_files} files. "
171
+ "Possibly due to an overwrite of an old checkpoint, or a checkpoint didn't get saved by one or more processes."
172
+ )
173
+
174
+ # the groups are named differently in each stage
175
+ if zero_stage == 2:
176
+ fp32_groups_key = SINGLE_PARTITION_OF_FP32_GROUPS
177
+ elif zero_stage == 3:
178
+ fp32_groups_key = FP32_FLAT_GROUPS
179
+ else:
180
+ raise ValueError(f"unknown zero stage {zero_stage}")
181
+
182
+ if zero_stage == 2:
183
+ fp32_flat_groups = [state_dicts[i][OPTIMIZER_STATE_DICT][fp32_groups_key] for i in range(len(state_dicts))]
184
+ elif zero_stage == 3:
185
+ # if there is more than one param group, there will be multiple flattened tensors - one
186
+ # flattened tensor per group - for simplicity merge them into a single tensor
187
+ #
188
+ # XXX: could make the script more memory efficient for when there are multiple groups - it
189
+ # will require matching the sub-lists of param_shapes for each param group flattened tensor
190
+
191
+ fp32_flat_groups = [
192
+ torch.cat(state_dicts[i][OPTIMIZER_STATE_DICT][fp32_groups_key], 0) for i in range(len(state_dicts))
193
+ ]
194
+
195
+ return zero_stage, world_size, fp32_flat_groups
196
+
197
+
198
+ def _get_fp32_state_dict_from_zero_checkpoint(ds_checkpoint_dir):
199
+ """
200
+ Returns fp32 state_dict reconstructed from ds checkpoint
201
+
202
+ Args:
203
+ - ``ds_checkpoint_dir``: path to the deepspeed checkpoint folder (where the optimizer files are)
204
+
205
+ """
206
+ print(f"Processing zero checkpoint '{ds_checkpoint_dir}'")
207
+
208
+ optim_files = get_optim_files(ds_checkpoint_dir)
209
+ zero_stage, world_size, fp32_flat_groups = parse_optim_states(optim_files, ds_checkpoint_dir)
210
+ print(f"Detected checkpoint of type zero stage {zero_stage}, world_size: {world_size}")
211
+
212
+ model_files = get_model_state_files(ds_checkpoint_dir)
213
+
214
+ zero_model_states = parse_model_states(model_files)
215
+ print(f'Parsing checkpoint created by deepspeed=={zero_model_states[0].ds_version}')
216
+
217
+ if zero_stage == 2:
218
+ return _get_fp32_state_dict_from_zero2_checkpoint(world_size, fp32_flat_groups, zero_model_states)
219
+ elif zero_stage == 3:
220
+ return _get_fp32_state_dict_from_zero3_checkpoint(world_size, fp32_flat_groups, zero_model_states)
221
+
222
+
223
+ def _zero2_merge_frozen_params(state_dict, zero_model_states):
224
+ if zero_model_states[0].frozen_param_shapes is None or len(zero_model_states[0].frozen_param_shapes) == 0:
225
+ return
226
+
227
+ frozen_param_shapes = zero_model_states[0].frozen_param_shapes
228
+ frozen_param_fragments = zero_model_states[0].frozen_param_fragments
229
+
230
+ if debug:
231
+ num_elem = sum(s.numel() for s in frozen_param_shapes.values())
232
+ print(f'rank 0: {FROZEN_PARAM_SHAPES}.numel = {num_elem}')
233
+
234
+ wanted_params = len(frozen_param_shapes)
235
+ wanted_numel = sum(s.numel() for s in frozen_param_shapes.values())
236
+ avail_numel = sum([p.numel() for p in frozen_param_fragments.values()])
237
+ print(f'Frozen params: Have {avail_numel} numels to process.')
238
+ print(f'Frozen params: Need {wanted_numel} numels in {wanted_params} params')
239
+
240
+ total_params = 0
241
+ total_numel = 0
242
+ for name, shape in frozen_param_shapes.items():
243
+ total_params += 1
244
+ unpartitioned_numel = shape.numel()
245
+ total_numel += unpartitioned_numel
246
+
247
+ state_dict[name] = frozen_param_fragments[name]
248
+
249
+ if debug:
250
+ print(f"{name} full shape: {shape} unpartitioned numel {unpartitioned_numel} ")
251
+
252
+ print(f"Reconstructed Frozen fp32 state dict with {total_params} params {total_numel} elements")
253
+
254
+
255
+ def _zero2_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states):
256
+ param_shapes = zero_model_states[0].param_shapes
257
+
258
+ # Reconstruction protocol:
259
+ #
260
+ # XXX: document this
261
+
262
+ if debug:
263
+ for i in range(world_size):
264
+ for j in range(len(fp32_flat_groups[0])):
265
+ print(f"{FP32_FLAT_GROUPS}[{i}][{j}].shape={fp32_flat_groups[i][j].shape}")
266
+
267
+ # XXX: memory usage doubles here (zero2)
268
+ num_param_groups = len(fp32_flat_groups[0])
269
+ merged_single_partition_of_fp32_groups = []
270
+ for i in range(num_param_groups):
271
+ merged_partitions = [sd[i] for sd in fp32_flat_groups]
272
+ full_single_fp32_vector = torch.cat(merged_partitions, 0)
273
+ merged_single_partition_of_fp32_groups.append(full_single_fp32_vector)
274
+ avail_numel = sum(
275
+ [full_single_fp32_vector.numel() for full_single_fp32_vector in merged_single_partition_of_fp32_groups])
276
+
277
+ if debug:
278
+ wanted_params = sum([len(shapes) for shapes in param_shapes])
279
+ wanted_numel = sum([sum(shape.numel() for shape in shapes.values()) for shapes in param_shapes])
280
+ # not asserting if there is a mismatch due to possible padding
281
+ print(f"Have {avail_numel} numels to process.")
282
+ print(f"Need {wanted_numel} numels in {wanted_params} params.")
283
+
284
+ # params
285
+ # XXX: for huge models that can't fit into the host's RAM we will have to recode this to support
286
+ # out-of-core computing solution
287
+ total_numel = 0
288
+ total_params = 0
289
+ for shapes, full_single_fp32_vector in zip(param_shapes, merged_single_partition_of_fp32_groups):
290
+ offset = 0
291
+ avail_numel = full_single_fp32_vector.numel()
292
+ for name, shape in shapes.items():
293
+
294
+ unpartitioned_numel = shape.numel()
295
+ total_numel += unpartitioned_numel
296
+ total_params += 1
297
+
298
+ if debug:
299
+ print(f"{name} full shape: {shape} unpartitioned numel {unpartitioned_numel} ")
300
+ state_dict[name] = full_single_fp32_vector.narrow(0, offset, unpartitioned_numel).view(shape)
301
+ offset += unpartitioned_numel
302
+
303
+ # Z2 started to align to 2*world_size to improve nccl performance. Therefore both offset and
304
+ # avail_numel can differ by anywhere between 0..2*world_size. Due to two unrelated complex
305
+ # paddings performed in the code it's almost impossible to predict the exact numbers w/o the
306
+ # live optimizer object, so we are checking that the numbers are within the right range
307
+ align_to = 2 * world_size
308
+
309
+ def zero2_align(x):
310
+ return align_to * math.ceil(x / align_to)
311
+
312
+ if debug:
313
+ print(f"original offset={offset}, avail_numel={avail_numel}")
314
+
315
+ offset = zero2_align(offset)
316
+ avail_numel = zero2_align(avail_numel)
317
+
318
+ if debug:
319
+ print(f"aligned offset={offset}, avail_numel={avail_numel}")
320
+
321
+ # Sanity check
322
+ if offset != avail_numel:
323
+ raise ValueError(f"consumed {offset} numels out of {avail_numel} - something is wrong")
324
+
325
+ print(f"Reconstructed fp32 state dict with {total_params} params {total_numel} elements")
326
+
327
+
328
+ def _get_fp32_state_dict_from_zero2_checkpoint(world_size, fp32_flat_groups, zero_model_states):
329
+ state_dict = OrderedDict()
330
+
331
+ # buffers
332
+ buffers = zero_model_states[0].buffers
333
+ state_dict.update(buffers)
334
+ if debug:
335
+ print(f"added {len(buffers)} buffers")
336
+
337
+ _zero2_merge_frozen_params(state_dict, zero_model_states)
338
+
339
+ _zero2_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states)
340
+
341
+ # recover shared parameters
342
+ for pair in zero_model_states[0].shared_params:
343
+ state_dict[pair[0]] = state_dict[pair[1]]
344
+
345
+ return state_dict
346
+
347
+
348
+ def zero3_partitioned_param_info(unpartitioned_numel, world_size):
349
+ remainder = unpartitioned_numel % world_size
350
+ padding_numel = (world_size - remainder) if remainder else 0
351
+ partitioned_numel = math.ceil(unpartitioned_numel / world_size)
352
+ return partitioned_numel, padding_numel
353
+
354
+
355
+ def _zero3_merge_frozen_params(state_dict, world_size, zero_model_states):
356
+ if zero_model_states[0].frozen_param_shapes is None or len(zero_model_states[0].frozen_param_shapes) == 0:
357
+ return
358
+
359
+ if debug:
360
+ for i in range(world_size):
361
+ num_elem = sum(s.numel() for s in zero_model_states[i].frozen_param_fragments.values())
362
+ print(f'rank {i}: {FROZEN_PARAM_SHAPES}.numel = {num_elem}')
363
+
364
+ frozen_param_shapes = zero_model_states[0].frozen_param_shapes
365
+ wanted_params = len(frozen_param_shapes)
366
+ wanted_numel = sum(s.numel() for s in frozen_param_shapes.values())
367
+ avail_numel = sum([p.numel() for p in zero_model_states[0].frozen_param_fragments.values()]) * world_size
368
+ print(f'Frozen params: Have {avail_numel} numels to process.')
369
+ print(f'Frozen params: Need {wanted_numel} numels in {wanted_params} params')
370
+
371
+ total_params = 0
372
+ total_numel = 0
373
+ for name, shape in zero_model_states[0].frozen_param_shapes.items():
374
+ total_params += 1
375
+ unpartitioned_numel = shape.numel()
376
+ total_numel += unpartitioned_numel
377
+
378
+ param_frags = tuple(model_state.frozen_param_fragments[name] for model_state in zero_model_states)
379
+ state_dict[name] = torch.cat(param_frags, 0).narrow(0, 0, unpartitioned_numel).view(shape)
380
+
381
+ partitioned_numel, partitioned_padding_numel = zero3_partitioned_param_info(unpartitioned_numel, world_size)
382
+
383
+ if debug:
384
+ print(
385
+ f"Frozen params: {total_params} {name} full shape: {shape} partition0 numel={partitioned_numel} partitioned_padding_numel={partitioned_padding_numel}"
386
+ )
387
+
388
+ print(f"Reconstructed Frozen fp32 state dict with {total_params} params {total_numel} elements")
389
+
390
+
391
+ def _zero3_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states):
392
+ param_shapes = zero_model_states[0].param_shapes
393
+ avail_numel = fp32_flat_groups[0].numel() * world_size
394
+ # Reconstruction protocol: For zero3 we need to zip the partitions together at boundary of each
395
+ # param, re-consolidating each param, while dealing with padding if any
396
+
397
+ # merge list of dicts, preserving order
398
+ param_shapes = {k: v for d in param_shapes for k, v in d.items()}
399
+
400
+ if debug:
401
+ for i in range(world_size):
402
+ print(f"{FP32_FLAT_GROUPS}[{i}].shape={fp32_flat_groups[i].shape}")
403
+
404
+ wanted_params = len(param_shapes)
405
+ wanted_numel = sum(shape.numel() for shape in param_shapes.values())
406
+ # not asserting if there is a mismatch due to possible padding
407
+ avail_numel = fp32_flat_groups[0].numel() * world_size
408
+ print(f"Trainable params: Have {avail_numel} numels to process.")
409
+ print(f"Trainable params: Need {wanted_numel} numels in {wanted_params} params.")
410
+
411
+ # params
412
+ # XXX: for huge models that can't fit into the host's RAM we will have to recode this to support
413
+ # out-of-core computing solution
414
+ offset = 0
415
+ total_numel = 0
416
+ total_params = 0
417
+ for name, shape in param_shapes.items():
418
+
419
+ unpartitioned_numel = shape.numel()
420
+ total_numel += unpartitioned_numel
421
+ total_params += 1
422
+
423
+ partitioned_numel, partitioned_padding_numel = zero3_partitioned_param_info(unpartitioned_numel, world_size)
424
+
425
+ if debug:
426
+ print(
427
+ f"Trainable params: {total_params} {name} full shape: {shape} partition0 numel={partitioned_numel} partitioned_padding_numel={partitioned_padding_numel}"
428
+ )
429
+
430
+ # XXX: memory usage doubles here
431
+ state_dict[name] = torch.cat(
432
+ tuple(fp32_flat_groups[i].narrow(0, offset, partitioned_numel) for i in range(world_size)),
433
+ 0).narrow(0, 0, unpartitioned_numel).view(shape)
434
+ offset += partitioned_numel
435
+
436
+ offset *= world_size
437
+
438
+ # Sanity check
439
+ if offset != avail_numel:
440
+ raise ValueError(f"consumed {offset} numels out of {avail_numel} - something is wrong")
441
+
442
+ print(f"Reconstructed Trainable fp32 state dict with {total_params} params {total_numel} elements")
443
+
444
+
445
+ def _get_fp32_state_dict_from_zero3_checkpoint(world_size, fp32_flat_groups, zero_model_states):
446
+ state_dict = OrderedDict()
447
+
448
+ # buffers
449
+ buffers = zero_model_states[0].buffers
450
+ state_dict.update(buffers)
451
+ if debug:
452
+ print(f"added {len(buffers)} buffers")
453
+
454
+ _zero3_merge_frozen_params(state_dict, world_size, zero_model_states)
455
+
456
+ _zero3_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states)
457
+
458
+ # recover shared parameters
459
+ for pair in zero_model_states[0].shared_params:
460
+ state_dict[pair[0]] = state_dict[pair[1]]
461
+
462
+ return state_dict
463
+
464
+
465
+ def get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, tag=None):
466
+ """
467
+ Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated state_dict that can be loaded with
468
+ ``load_state_dict()`` and used for training without DeepSpeed or shared with others, for example
469
+ via a model hub.
470
+
471
+ Args:
472
+ - ``checkpoint_dir``: path to the desired checkpoint folder
473
+ - ``tag``: checkpoint tag used as a unique identifier for checkpoint. If not provided will attempt to load tag in 'latest' file. e.g., ``global_step14``
474
+
475
+ Returns:
476
+ - pytorch ``state_dict``
477
+
478
+ Note: this approach may not work if your application doesn't have sufficient free CPU memory and
479
+ you may need to use the offline approach using the ``zero_to_fp32.py`` script that is saved with
480
+ the checkpoint.
481
+
482
+ A typical usage might be ::
483
+
484
+ from deepspeed.utils.zero_to_fp32 import get_fp32_state_dict_from_zero_checkpoint
485
+ # do the training and checkpoint saving
486
+ state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir) # already on cpu
487
+ model = model.cpu() # move to cpu
488
+ model.load_state_dict(state_dict)
489
+ # submit to model hub or save the model to share with others
490
+
491
+ In this example the ``model`` will no longer be usable in the deepspeed context of the same
492
+ application. i.e. you will need to re-initialize the deepspeed engine, since
493
+ ``model.load_state_dict(state_dict)`` will remove all the deepspeed magic from it.
494
+
495
+ If you want it all done for you, use ``load_state_dict_from_zero_checkpoint`` instead.
496
+
497
+ """
498
+ if tag is None:
499
+ latest_path = os.path.join(checkpoint_dir, 'latest')
500
+ if os.path.isfile(latest_path):
501
+ with open(latest_path, 'r') as fd:
502
+ tag = fd.read().strip()
503
+ else:
504
+ raise ValueError(f"Unable to find 'latest' file at {latest_path}")
505
+
506
+ ds_checkpoint_dir = os.path.join(checkpoint_dir, tag)
507
+
508
+ if not os.path.isdir(ds_checkpoint_dir):
509
+ raise FileNotFoundError(f"Directory '{ds_checkpoint_dir}' doesn't exist")
510
+
511
+ return _get_fp32_state_dict_from_zero_checkpoint(ds_checkpoint_dir)
512
+
513
+
514
+ def convert_zero_checkpoint_to_fp32_state_dict(checkpoint_dir, output_file, tag=None):
515
+ """
516
+ Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated ``state_dict`` file that can be
517
+ loaded with ``torch.load(file)`` + ``load_state_dict()`` and used for training without DeepSpeed.
518
+
519
+ Args:
520
+ - ``checkpoint_dir``: path to the desired checkpoint folder. (one that contains the tag-folder, like ``global_step14``)
521
+ - ``output_file``: path to the pytorch fp32 state_dict output file (e.g. path/pytorch_model.bin)
522
+ - ``tag``: checkpoint tag used as a unique identifier for checkpoint. If not provided will attempt to load tag in the file named ``latest`` in the checkpoint folder, e.g., ``global_step14``
523
+ """
524
+
525
+ state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, tag)
526
+ print(f"Saving fp32 state dict to {output_file}")
527
+ torch.save(state_dict, output_file)
528
+
529
+
530
+ def load_state_dict_from_zero_checkpoint(model, checkpoint_dir, tag=None):
531
+ """
532
+ 1. Put the provided model to cpu
533
+ 2. Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated ``state_dict``
534
+ 3. Load it into the provided model
535
+
536
+ Args:
537
+ - ``model``: the model object to update
538
+ - ``checkpoint_dir``: path to the desired checkpoint folder. (one that contains the tag-folder, like ``global_step14``)
539
+ - ``tag``: checkpoint tag used as a unique identifier for checkpoint. If not provided will attempt to load tag in the file named ``latest`` in the checkpoint folder, e.g., ``global_step14``
540
+
541
+ Returns:
542
+ - ``model`: modified model
543
+
544
+ Make sure you have plenty of CPU memory available before you call this function. If you don't
545
+ have enough use the ``zero_to_fp32.py`` utility to do the conversion. You will find it
546
+ conveniently placed for you in the checkpoint folder.
547
+
548
+ A typical usage might be ::
549
+
550
+ from deepspeed.utils.zero_to_fp32 import load_state_dict_from_zero_checkpoint
551
+ model = load_state_dict_from_zero_checkpoint(trainer.model, checkpoint_dir)
552
+ # submit to model hub or save the model to share with others
553
+
554
+ Note, that once this was run, the ``model`` will no longer be usable in the deepspeed context
555
+ of the same application. i.e. you will need to re-initialize the deepspeed engine, since
556
+ ``model.load_state_dict(state_dict)`` will remove all the deepspeed magic from it.
557
+
558
+ """
559
+ logger.info(f"Extracting fp32 weights")
560
+ state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, tag)
561
+
562
+ logger.info(f"Overwriting model with fp32 weights")
563
+ model = model.cpu()
564
+ model.load_state_dict(state_dict, strict=False)
565
+
566
+ return model
567
+
568
+
569
+ if __name__ == "__main__":
570
+
571
+ parser = argparse.ArgumentParser()
572
+ parser.add_argument("checkpoint_dir",
573
+ type=str,
574
+ help="path to the desired checkpoint folder, e.g., path/checkpoint-12")
575
+ parser.add_argument(
576
+ "output_file",
577
+ type=str,
578
+ help="path to the pytorch fp32 state_dict output file (e.g. path/checkpoint-12/pytorch_model.bin)")
579
+ parser.add_argument("-d", "--debug", action='store_true', help="enable debug")
580
+ args = parser.parse_args()
581
+
582
+ debug = args.debug
583
+
584
+ convert_zero_checkpoint_to_fp32_state_dict(args.checkpoint_dir, args.output_file)