emozilla commited on
Commit
5169b80
1 Parent(s): 5f705d3

Upload folder using huggingface_hub

Browse files
config.json ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "activation_type": "swiglu",
3
+ "alibi": false,
4
+ "alibi_bias_max": 8.0,
5
+ "architectures": [
6
+ "OLMoModelForCausalLM"
7
+ ],
8
+ "attention_dropout": 0.0,
9
+ "attention_layer_norm": false,
10
+ "attention_layer_norm_with_affine": false,
11
+ "bias_for_layer_norm": false,
12
+ "block_group_size": 1,
13
+ "block_type": "sequential",
14
+ "clip_qkv": null,
15
+ "d_model": 2048,
16
+ "embedding_dropout": 0.0,
17
+ "embedding_size": 50304,
18
+ "eos_token_id": 50279,
19
+ "flash_attention": true,
20
+ "include_bias": false,
21
+ "init_cutoff_factor": null,
22
+ "init_device": "meta",
23
+ "init_fn": "mitchell",
24
+ "init_std": 0.02,
25
+ "layer_norm_type": "rms",
26
+ "layer_norm_with_affine": true,
27
+ "max_sequence_length": 2048,
28
+ "mlp_hidden_size": null,
29
+ "mlp_ratio": 8,
30
+ "model_type": "olmo",
31
+ "multi_query_attention": false,
32
+ "n_heads": 16,
33
+ "n_layers": 16,
34
+ "pad_token_id": 1,
35
+ "precision": "amp_bf16",
36
+ "residual_dropout": 0.0,
37
+ "rope": true,
38
+ "rope_full_precision": true,
39
+ "scale_logits": false,
40
+ "ternary": true,
41
+ "transformers_version": "4.38.2",
42
+ "use_cache": true,
43
+ "vocab_size": 50280,
44
+ "weight_tying": true,
45
+ "auto_map": {
46
+ "AutoConfig": "configuration_olmo.OLMoConfig",
47
+ "AutoModelForCausalLM": "modeling_olmo.OLMoForCausalLM",
48
+ "AutoTokenizer": [
49
+ "tokenization_olmo_fast.OLMoTokenizerFast",
50
+ "tokenization_olmo_fast.OLMoTokenizerFast"
51
+ ]
52
+ }
53
+ }
configuration_olmo.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ OLMo configuration
3
+ """
4
+
5
+ from transformers import AutoConfig, PretrainedConfig
6
+ from transformers.utils import logging
7
+
8
+ from olmo.config import ModelConfig
9
+
10
+ logger = logging.get_logger(__name__)
11
+
12
+
13
+ class OLMoConfig(PretrainedConfig):
14
+ model_type = "olmo"
15
+ keys_to_ignore_at_inference = ["past_key_values"] # TODO: confirm
16
+
17
+ def __init__(self, use_cache: bool = False, **kwargs):
18
+ model_config = ModelConfig()
19
+ all_kwargs = model_config.asdict()
20
+ all_kwargs.update(kwargs)
21
+ all_kwargs.update({"use_cache": use_cache})
22
+ all_kwargs.update(
23
+ {
24
+ "architectures": all_kwargs.get("architectures", ["OLMoModelForCausalLM"])
25
+ or ["OLMoModelForCausalLM"]
26
+ }
27
+ )
28
+ super().__init__(**all_kwargs)
29
+
30
+ @property
31
+ def num_attention_heads(self):
32
+ return self.n_heads
33
+
34
+ @property
35
+ def num_hidden_layers(self):
36
+ return self.n_layers
37
+
38
+ @property
39
+ def hidden_size(self):
40
+ return self.d_model
41
+
42
+
43
+ # Register the config class so that it is available for transformer pipelines, auto-loading etc.
44
+ AutoConfig.register("olmo", OLMoConfig)
modeling_olmo.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dataclasses import fields
2
+ from typing import List, Optional, Tuple, Union
3
+
4
+ import torch
5
+ from transformers import PreTrainedModel
6
+ from transformers.modeling_outputs import CausalLMOutputWithPast
7
+ from transformers.models.auto import AutoModelForCausalLM
8
+
9
+ from olmo.config import ModelConfig
10
+ from olmo.model import OLMo
11
+
12
+ from .configuration_olmo import OLMoConfig
13
+
14
+
15
+ def create_model_config_from_pretrained_config(config: OLMoConfig):
16
+ """
17
+ Utility function
18
+ """
19
+
20
+ kwargs = {}
21
+ for field in fields(ModelConfig):
22
+ kwargs[field.name] = getattr(config, field.name)
23
+
24
+ model_config = ModelConfig(**kwargs)
25
+ return model_config
26
+
27
+
28
+ class OLMoForCausalLM(PreTrainedModel):
29
+ """
30
+ Extremely barebones HF model wrapper.
31
+ """
32
+
33
+ config_class = OLMoConfig
34
+ base_model_prefix = "model"
35
+ _no_split_modules = ["OLMoBlock"]
36
+
37
+ def __init__(self, config: OLMoConfig, model: Optional[OLMo] = None, init_params: bool = False):
38
+ super().__init__(config)
39
+
40
+ if not model:
41
+ model_config = create_model_config_from_pretrained_config(config)
42
+ # Initialize model (always on CPU to start with so we don't run out of GPU memory).
43
+ model_config.init_device = "cpu"
44
+ self.model = OLMo(model_config, init_params=init_params)
45
+ else:
46
+ self.model = model
47
+
48
+ def forward(
49
+ self,
50
+ input_ids: torch.LongTensor = None,
51
+ inputs_embeds: Optional[torch.FloatTensor] = None,
52
+ attention_mask: Optional[torch.Tensor] = None,
53
+ attention_bias: Optional[torch.Tensor] = None,
54
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
55
+ labels: Optional[torch.LongTensor] = None,
56
+ use_cache: Optional[bool] = None,
57
+ output_attentions: Optional[bool] = None,
58
+ output_hidden_states: Optional[bool] = None,
59
+ return_dict: Optional[bool] = None,
60
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
61
+ if use_cache is None:
62
+ use_cache = self.config.use_cache
63
+
64
+ if output_attentions:
65
+ raise ValueError("output_attentions is not yet supported in OLMo")
66
+
67
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
68
+
69
+ # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
70
+ outputs = self.model.forward(
71
+ input_ids=input_ids,
72
+ input_embeddings=inputs_embeds,
73
+ attention_mask=attention_mask,
74
+ attention_bias=attention_bias,
75
+ past_key_values=past_key_values,
76
+ use_cache=use_cache,
77
+ output_hidden_states=output_hidden_states,
78
+ )
79
+
80
+ logits = outputs.logits
81
+ hidden_states = outputs.hidden_states
82
+
83
+ loss = None
84
+ if labels is not None:
85
+ # Shift so that tokens < n predict n
86
+ shift_logits = logits[..., :-1, :].contiguous()
87
+ shift_labels = labels[..., 1:].contiguous()
88
+ # Flatten the tokens
89
+ loss_fct = torch.nn.CrossEntropyLoss()
90
+ shift_logits = shift_logits.view(-1, self.config.embedding_size)
91
+ shift_labels = shift_labels.view(-1)
92
+ # Enable model parallelism
93
+ shift_labels = shift_labels.to(shift_logits.device)
94
+ loss = loss_fct(shift_logits, shift_labels)
95
+
96
+ if not return_dict:
97
+ output = (logits,) + outputs[1:]
98
+ return (loss,) + output if loss is not None else output
99
+
100
+ return CausalLMOutputWithPast(
101
+ loss=loss,
102
+ logits=logits,
103
+ past_key_values=outputs.attn_key_values,
104
+ hidden_states=hidden_states,
105
+ )
106
+
107
+ def can_generate(self) -> bool:
108
+ return True
109
+
110
+ def prepare_inputs_for_generation(
111
+ self, input_ids: torch.LongTensor, past_key_values: Optional[List[Tuple]] = None, **kwargs
112
+ ):
113
+ if past_key_values:
114
+ # This is because we want the model to only process the last generated token.
115
+ input_ids = input_ids[:, -1:]
116
+ model_inputs = {"input_ids": input_ids, "past_key_values": past_key_values}
117
+
118
+ model_inputs.update(kwargs)
119
+ model_inputs["use_cache"] = kwargs.pop("use_cache", self.config.use_cache)
120
+ return model_inputs
121
+
122
+ # TODO: these are required to make the implementation complete.
123
+ # def resize_position_embeddings(self, new_num_position_embeddings: int):
124
+ # pass
125
+ #
126
+ # def get_position_embeddings(self) -> Union[nn.Embedding, Tuple[nn.Embedding]]:
127
+ # pass
128
+ #
129
+ # def _reorder_cache(self, past_key_values, beam_idx):
130
+ # pass
131
+
132
+ def get_input_embeddings(self) -> torch.nn.Module:
133
+ return self.model.transformer.wte
134
+
135
+ def set_input_embeddings(self, value: torch.nn.Module):
136
+ self.model.transformer.wte = value
137
+
138
+ def get_output_embeddings(self):
139
+ if self.config.weight_tying:
140
+ return self.model.transformer.wte
141
+ else:
142
+ return self.model.transformer.ff_out
143
+
144
+ def set_output_embeddings(self, value: torch.nn.Module):
145
+ if self.config.weight_tying:
146
+ self.model.transformer.wte = value
147
+ else:
148
+ self.model.transformer.ff_out = value
149
+
150
+ def tie_weights(self):
151
+ if self.config.weight_tying:
152
+ self.model.transformer.ff_out = self.model.transformer.wte
153
+
154
+
155
+ # Register the model so that it is available for transformer pipelines, auto-loading, etc.
156
+ AutoModelForCausalLM.register(OLMoConfig, OLMoForCausalLM)
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b1aedbdb8a9c944a7994b7afacc7ccac4fbc2c2b745231f0bd943b92a3101191
3
+ size 4707362312
special_tokens_map.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "eos_token": "|||IP_ADDRESS|||",
3
+ "pad_token": "<|padding|>"
4
+ }
tokenization_olmo_fast.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, PreTrainedTokenizerFast
2
+
3
+ from hf_olmo.configuration_olmo import OLMoConfig
4
+
5
+
6
+ class OLMoTokenizerFast(PreTrainedTokenizerFast):
7
+ # Note: OLMo's tokenizer is already a wrapper around huggingface. This is potentially unnecessary.
8
+ pass
9
+
10
+ # def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
11
+ # # This is required to make the implementation complete.
12
+ # pass
13
+
14
+
15
+ # Register the tokenizer class so that it is available for transformer pipelines, auto-loading etc.
16
+ AutoTokenizer.register(OLMoConfig, fast_tokenizer_class=OLMoTokenizerFast)
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "<|endoftext|>",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "1": {
12
+ "content": "<|padding|>",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "50254": {
20
+ "content": " ",
21
+ "lstrip": false,
22
+ "normalized": true,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": false
26
+ },
27
+ "50255": {
28
+ "content": " ",
29
+ "lstrip": false,
30
+ "normalized": true,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": false
34
+ },
35
+ "50256": {
36
+ "content": " ",
37
+ "lstrip": false,
38
+ "normalized": true,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": false
42
+ },
43
+ "50257": {
44
+ "content": " ",
45
+ "lstrip": false,
46
+ "normalized": true,
47
+ "rstrip": false,
48
+ "single_word": false,
49
+ "special": false
50
+ },
51
+ "50258": {
52
+ "content": " ",
53
+ "lstrip": false,
54
+ "normalized": true,
55
+ "rstrip": false,
56
+ "single_word": false,
57
+ "special": false
58
+ },
59
+ "50259": {
60
+ "content": " ",
61
+ "lstrip": false,
62
+ "normalized": true,
63
+ "rstrip": false,
64
+ "single_word": false,
65
+ "special": false
66
+ },
67
+ "50260": {
68
+ "content": " ",
69
+ "lstrip": false,
70
+ "normalized": true,
71
+ "rstrip": false,
72
+ "single_word": false,
73
+ "special": false
74
+ },
75
+ "50261": {
76
+ "content": " ",
77
+ "lstrip": false,
78
+ "normalized": true,
79
+ "rstrip": false,
80
+ "single_word": false,
81
+ "special": false
82
+ },
83
+ "50262": {
84
+ "content": " ",
85
+ "lstrip": false,
86
+ "normalized": true,
87
+ "rstrip": false,
88
+ "single_word": false,
89
+ "special": false
90
+ },
91
+ "50263": {
92
+ "content": " ",
93
+ "lstrip": false,
94
+ "normalized": true,
95
+ "rstrip": false,
96
+ "single_word": false,
97
+ "special": false
98
+ },
99
+ "50264": {
100
+ "content": " ",
101
+ "lstrip": false,
102
+ "normalized": true,
103
+ "rstrip": false,
104
+ "single_word": false,
105
+ "special": false
106
+ },
107
+ "50265": {
108
+ "content": " ",
109
+ "lstrip": false,
110
+ "normalized": true,
111
+ "rstrip": false,
112
+ "single_word": false,
113
+ "special": false
114
+ },
115
+ "50266": {
116
+ "content": " ",
117
+ "lstrip": false,
118
+ "normalized": true,
119
+ "rstrip": false,
120
+ "single_word": false,
121
+ "special": false
122
+ },
123
+ "50267": {
124
+ "content": " ",
125
+ "lstrip": false,
126
+ "normalized": true,
127
+ "rstrip": false,
128
+ "single_word": false,
129
+ "special": false
130
+ },
131
+ "50268": {
132
+ "content": " ",
133
+ "lstrip": false,
134
+ "normalized": true,
135
+ "rstrip": false,
136
+ "single_word": false,
137
+ "special": false
138
+ },
139
+ "50269": {
140
+ "content": " ",
141
+ "lstrip": false,
142
+ "normalized": true,
143
+ "rstrip": false,
144
+ "single_word": false,
145
+ "special": false
146
+ },
147
+ "50270": {
148
+ "content": " ",
149
+ "lstrip": false,
150
+ "normalized": true,
151
+ "rstrip": false,
152
+ "single_word": false,
153
+ "special": false
154
+ },
155
+ "50271": {
156
+ "content": " ",
157
+ "lstrip": false,
158
+ "normalized": true,
159
+ "rstrip": false,
160
+ "single_word": false,
161
+ "special": false
162
+ },
163
+ "50272": {
164
+ "content": " ",
165
+ "lstrip": false,
166
+ "normalized": true,
167
+ "rstrip": false,
168
+ "single_word": false,
169
+ "special": false
170
+ },
171
+ "50273": {
172
+ "content": " ",
173
+ "lstrip": false,
174
+ "normalized": true,
175
+ "rstrip": false,
176
+ "single_word": false,
177
+ "special": false
178
+ },
179
+ "50274": {
180
+ "content": " ",
181
+ "lstrip": false,
182
+ "normalized": true,
183
+ "rstrip": false,
184
+ "single_word": false,
185
+ "special": false
186
+ },
187
+ "50275": {
188
+ "content": " ",
189
+ "lstrip": false,
190
+ "normalized": true,
191
+ "rstrip": false,
192
+ "single_word": false,
193
+ "special": false
194
+ },
195
+ "50276": {
196
+ "content": " ",
197
+ "lstrip": false,
198
+ "normalized": true,
199
+ "rstrip": false,
200
+ "single_word": false,
201
+ "special": false
202
+ },
203
+ "50277": {
204
+ "content": "|||EMAIL_ADDRESS|||",
205
+ "lstrip": false,
206
+ "normalized": true,
207
+ "rstrip": false,
208
+ "single_word": false,
209
+ "special": false
210
+ },
211
+ "50278": {
212
+ "content": "|||PHONE_NUMBER|||",
213
+ "lstrip": false,
214
+ "normalized": true,
215
+ "rstrip": false,
216
+ "single_word": false,
217
+ "special": false
218
+ },
219
+ "50279": {
220
+ "content": "|||IP_ADDRESS|||",
221
+ "lstrip": false,
222
+ "normalized": true,
223
+ "rstrip": false,
224
+ "single_word": false,
225
+ "special": false
226
+ }
227
+ },
228
+ "clean_up_tokenization_spaces": true,
229
+ "eos_token": "|||IP_ADDRESS|||",
230
+ "max_length": null,
231
+ "model_max_length": 1000000000000000019884624838656,
232
+ "pad_token": "<|padding|>",
233
+ "tokenizer_class": "OLMoTokenizer",
234
+ "truncation": "right"
235
+ }