marianna13 commited on
Commit
7e497b3
1 Parent(s): 1662689

Upload folder using huggingface_hub

Browse files
config.json CHANGED
@@ -1,8 +1,28 @@
1
  {
 
2
  "architectures": [
3
- "OpenLMModel"
4
  ],
 
 
 
 
 
 
 
 
5
  "model_type": "openlm",
 
 
 
 
 
 
 
 
 
 
 
6
  "params": null,
7
  "params_args_dict": {
8
  "apply_qk_norm": true,
@@ -31,6 +51,11 @@
31
  "vocab_size": 50432,
32
  "weight_tying": false
33
  },
34
- "torch_dtype": "float32",
35
- "transformers_version": "4.40.0"
 
 
 
 
 
36
  }
 
1
  {
2
+ "apply_qk_norm": true,
3
  "architectures": [
4
+ "OpenLMforCausalLM"
5
  ],
6
+ "attn_func": null,
7
+ "auto_map": {
8
+ "AutoConfig": "configuration_openlm.OpenLMConfig",
9
+ "AutoModel": "modeling_openlm.OpenLMModel",
10
+ "AutoModelForCausalLM": "modeling_openlm.OpenLMforCausalLM"
11
+ },
12
+ "dim": 2560,
13
+ "ffn_type": "swiglu",
14
  "model_type": "openlm",
15
+ "moe_capacity_factor": 1.25,
16
+ "moe_expert_model_parallelism": false,
17
+ "moe_freq": 0,
18
+ "moe_loss_weight": 0.1,
19
+ "moe_num_experts": null,
20
+ "moe_top_k": 2,
21
+ "moe_weight_parallelism": false,
22
+ "n_heads": 32,
23
+ "n_layers": 32,
24
+ "norm_eps": 1e-05,
25
+ "norm_type": null,
26
  "params": null,
27
  "params_args_dict": {
28
  "apply_qk_norm": true,
 
51
  "vocab_size": 50432,
52
  "weight_tying": false
53
  },
54
+ "positional_embedding_type": "rotary",
55
+ "post_embed_norm": false,
56
+ "seq_len": 2048,
57
+ "tie_word_embeddings": false,
58
+ "transformers_version": "4.40.0",
59
+ "vocab_size": 50432,
60
+ "weight_tying": false
61
  }
configuration_openlm.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from open_lm.utils.transformers.hf_config import OpenLMConfig
modeling_openlm.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from argparse import Namespace
2
+ from torch.utils.checkpoint import checkpoint
3
+ from transformers import PreTrainedModel
4
+ from transformers.modeling_outputs import CausalLMOutputWithPast
5
+ from open_lm.utils.transformers.hf_config import OpenLMConfig
6
+ from open_lm.model import Transformer, create_params
7
+ from open_lm.attention import get_attn_func, xformers_attn, torch_attn
8
+ from open_lm.norms import get_norm_class
9
+ import torch
10
+ import torch.nn as nn
11
+ from typing import Union, Tuple, Optional, List
12
+ import os
13
+
14
+
15
+ class OpenLMModel(PreTrainedModel):
16
+ config_class = OpenLMConfig
17
+
18
+ def __init__(self, config, **kwargs):
19
+ # This has to be done before init as it sets makes sure hf config is correct
20
+ if hasattr(config, "params"):
21
+ params = config.params
22
+ else:
23
+ params_args_dict = config.params_args_dict
24
+ if not params_args_dict.get("norm_type"):
25
+ params_args_dict["norm_type"] = get_norm_class(params_args_dict["model_norm"])
26
+ if not params_args_dict.get("attn_func"):
27
+ params_args_dict["attn_func"] = get_attn_func(
28
+ params_args_dict["attn_name"],
29
+ params_args_dict["attn_activation"],
30
+ params_args_dict["attn_seq_scalar"],
31
+ params_args_dict["attn_seq_scalar_alpha"]
32
+ )
33
+ params = create_params(Namespace(**config.params_args_dict))
34
+ config.set_params(params)
35
+ super().__init__(config, **kwargs)
36
+
37
+ self.supports_gradient_checkpointing = True
38
+ self.model = Transformer(params)
39
+
40
+ @property
41
+ def gradient_checkpointing(self):
42
+ return self.model.grad_checkpointing
43
+
44
+ @gradient_checkpointing.setter
45
+ def gradient_checkpointing(self, value):
46
+ self.model.grad_checkpointing = value
47
+
48
+ def forward(self, input_ids=None, inputs_embeds=None, **kwargs):
49
+ return self.model(input_ids=input_ids, inputs_embeds=inputs_embeds, **kwargs)
50
+
51
+
52
+ class OpenLMforCausalLM(OpenLMModel):
53
+ _keys_to_ignore_on_load_missing = [r"lm_head.weight"]
54
+
55
+ def __init__(self, config, **kwargs):
56
+ super().__init__(config, **kwargs)
57
+ self.lm_head = None
58
+ # Initialize weights and apply final processing
59
+ self.post_init()
60
+
61
+ def get_input_embeddings(self):
62
+ return self.model.tok_embeddings
63
+
64
+ def set_input_embeddings(self, value):
65
+ self.model.tok_embeddings = value
66
+
67
+ def get_output_embeddings(self):
68
+ return self.model.get_output_embeddings()
69
+
70
+ def set_output_embeddings(self, new_embeddings):
71
+ raise NotImplementedError
72
+
73
+ def set_decoder(self, decoder):
74
+ self.model = decoder
75
+
76
+ def get_decoder(self):
77
+ return self.model
78
+
79
+ def forward(
80
+ self,
81
+ input_ids: Optional[torch.Tensor] = None,
82
+ attention_mask: Optional[torch.Tensor] = None,
83
+ position_ids: Optional[torch.LongTensor] = None,
84
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
85
+ inputs_embeds: Optional[torch.FloatTensor] = None,
86
+ labels: Optional[torch.LongTensor] = None,
87
+ use_cache: Optional[bool] = False,
88
+ output_attentions: Optional[bool] = None,
89
+ output_hidden_states: Optional[bool] = None,
90
+ return_dict: Optional[bool] = None,
91
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
92
+ r"""
93
+ Args:
94
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
95
+ Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
96
+ config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
97
+ (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
98
+ Returns:
99
+ Example:
100
+ ```python
101
+ >>> from transformers import AutoTokenizer, OpenLlamaForCausalLM
102
+ >>> model = OpenLlamaForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
103
+ >>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
104
+ >>> prompt = "Hey, are you consciours? Can you talk to me?"
105
+ >>> inputs = tokenizer(prompt, return_tensors="pt")
106
+ >>> # Generate
107
+ >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
108
+ >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
109
+ "Hey, are you consciours? Can you talk to me?\nI'm not consciours, but I can talk to you."
110
+ ```"""
111
+ assert position_ids is None, "Position IDs are not supported"
112
+ # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
113
+ logits, _, past_key_values = self.model(
114
+ input_ids=input_ids,
115
+ inputs_embeds=inputs_embeds,
116
+ past_key_values=past_key_values,
117
+ use_cache=use_cache,
118
+ attention_mask=attention_mask,
119
+ )
120
+ loss = None
121
+ if labels is not None:
122
+ shift_logits = logits[..., :-1, :].contiguous()
123
+ shift_labels = labels[..., 1:].contiguous()
124
+ loss_fct = nn.CrossEntropyLoss()
125
+ shift_logits = shift_logits.view(-1, shift_logits.size(-1))
126
+ shift_labels = shift_labels.view(-1).to(shift_logits.device)
127
+ loss = loss_fct(shift_logits, shift_labels)
128
+
129
+ output = CausalLMOutputWithPast(logits=logits, past_key_values=past_key_values, loss=loss)
130
+ return output
131
+
132
+ def prepare_inputs_for_generation(
133
+ self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
134
+ ):
135
+ if past_key_values is not None:
136
+ past_length = past_key_values[0][0].shape[1]
137
+
138
+ # Some generation methods already pass only the last input ID
139
+ if input_ids.shape[1] > past_length:
140
+ remove_prefix_length = past_length
141
+ else:
142
+ # Default to old behavior: keep only final ID
143
+ remove_prefix_length = input_ids.shape[1] - 1
144
+
145
+ input_ids = input_ids[:, remove_prefix_length:]
146
+
147
+ # if `inputs_embeds` are passed, we only want to use them in the 1st generation step
148
+ if inputs_embeds is not None and past_key_values is None:
149
+ model_inputs = {"inputs_embeds": inputs_embeds}
150
+ else:
151
+ model_inputs = {"input_ids": input_ids}
152
+
153
+ model_inputs.update(
154
+ {
155
+ "past_key_values": past_key_values,
156
+ "use_cache": kwargs.get("use_cache"),
157
+ "attention_mask": attention_mask,
158
+ }
159
+ )
160
+ return model_inputs
161
+
162
+ @staticmethod
163
+ def _reorder_cache(past_key_values, beam_idx):
164
+ reordered_cache = ()
165
+ for layer_past in past_key_values:
166
+ reordered_cache += (tuple(past_state.index_select(0, beam_idx) for past_state in layer_past),)
167
+ return reordered_cache
168
+
169
+ @classmethod
170
+ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.PathLike]], **kwargs):
171
+ if (
172
+ os.path.isdir(pretrained_model_name_or_path)
173
+ and kwargs.get("config", None) is not None
174
+ and getattr(kwargs["config"], "checkpoint_file", None) is not None
175
+ ):
176
+ # Setting torch default dtype
177
+ torch_dtype = getattr(kwargs["config"], "torch_dtype", None)
178
+ if isinstance(torch_dtype, str):
179
+ torch_dtype = getattr(torch, torch_dtype)
180
+ if torch_dtype is not None:
181
+ torch.set_default_dtype(torch_dtype)
182
+
183
+ print("Loading checkpoint from directory")
184
+ checkpoint_path = kwargs["config"].checkpoint_file
185
+ checkpoint = torch.load(checkpoint_path)
186
+
187
+ state_dict = checkpoint["state_dict"]
188
+ state_dict = {x.replace("module.", ""): y for x, y in state_dict.items()}
189
+ state_dict = {f"model.{x}": y for x, y in state_dict.items()}
190
+
191
+ return super().from_pretrained(None, state_dict=state_dict, **kwargs)
192
+ elif os.path.isdir(pretrained_model_name_or_path):
193
+ # Load from a PyTorch checkpoint
194
+ print("Loading checkpoint from directory")
195
+ checkpoint_path = os.path.join(pretrained_model_name_or_path, "pytorch_model.bin")
196
+ state_dict = torch.load(checkpoint_path)
197
+
198
+ # state_dict = {x.replace("module.", ""): y for x, y in state_dict.items()}
199
+ state_dict = {f"model.{x}" if "model." not in x else x: y for x, y in state_dict.items()}
200
+
201
+ return super().from_pretrained(pretrained_model_name_or_path, state_dict=state_dict, **kwargs)
202
+ else:
203
+ return super().from_pretrained(pretrained_model_name_or_path, **kwargs)
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:20aa0531af18faedb61cf76f1b4cc6090f0ea4fe45830eb1d91c1198cf7cc475
3
+ size 11184489866
special_tokens_map.json ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "<|endoftext|>",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "eos_token": {
10
+ "content": "<|endoftext|>",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "unk_token": {
17
+ "content": "<|endoftext|>",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ }
23
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,214 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": false,
3
+ "add_eos_token": false,
4
+ "add_prefix_space": false,
5
+ "added_tokens_decoder": {
6
+ "0": {
7
+ "content": "<|endoftext|>",
8
+ "lstrip": false,
9
+ "normalized": false,
10
+ "rstrip": false,
11
+ "single_word": false,
12
+ "special": true
13
+ },
14
+ "1": {
15
+ "content": "<|padding|>",
16
+ "lstrip": false,
17
+ "normalized": false,
18
+ "rstrip": false,
19
+ "single_word": false,
20
+ "special": true
21
+ },
22
+ "50254": {
23
+ "content": " ",
24
+ "lstrip": false,
25
+ "normalized": true,
26
+ "rstrip": false,
27
+ "single_word": false,
28
+ "special": false
29
+ },
30
+ "50255": {
31
+ "content": " ",
32
+ "lstrip": false,
33
+ "normalized": true,
34
+ "rstrip": false,
35
+ "single_word": false,
36
+ "special": false
37
+ },
38
+ "50256": {
39
+ "content": " ",
40
+ "lstrip": false,
41
+ "normalized": true,
42
+ "rstrip": false,
43
+ "single_word": false,
44
+ "special": false
45
+ },
46
+ "50257": {
47
+ "content": " ",
48
+ "lstrip": false,
49
+ "normalized": true,
50
+ "rstrip": false,
51
+ "single_word": false,
52
+ "special": false
53
+ },
54
+ "50258": {
55
+ "content": " ",
56
+ "lstrip": false,
57
+ "normalized": true,
58
+ "rstrip": false,
59
+ "single_word": false,
60
+ "special": false
61
+ },
62
+ "50259": {
63
+ "content": " ",
64
+ "lstrip": false,
65
+ "normalized": true,
66
+ "rstrip": false,
67
+ "single_word": false,
68
+ "special": false
69
+ },
70
+ "50260": {
71
+ "content": " ",
72
+ "lstrip": false,
73
+ "normalized": true,
74
+ "rstrip": false,
75
+ "single_word": false,
76
+ "special": false
77
+ },
78
+ "50261": {
79
+ "content": " ",
80
+ "lstrip": false,
81
+ "normalized": true,
82
+ "rstrip": false,
83
+ "single_word": false,
84
+ "special": false
85
+ },
86
+ "50262": {
87
+ "content": " ",
88
+ "lstrip": false,
89
+ "normalized": true,
90
+ "rstrip": false,
91
+ "single_word": false,
92
+ "special": false
93
+ },
94
+ "50263": {
95
+ "content": " ",
96
+ "lstrip": false,
97
+ "normalized": true,
98
+ "rstrip": false,
99
+ "single_word": false,
100
+ "special": false
101
+ },
102
+ "50264": {
103
+ "content": " ",
104
+ "lstrip": false,
105
+ "normalized": true,
106
+ "rstrip": false,
107
+ "single_word": false,
108
+ "special": false
109
+ },
110
+ "50265": {
111
+ "content": " ",
112
+ "lstrip": false,
113
+ "normalized": true,
114
+ "rstrip": false,
115
+ "single_word": false,
116
+ "special": false
117
+ },
118
+ "50266": {
119
+ "content": " ",
120
+ "lstrip": false,
121
+ "normalized": true,
122
+ "rstrip": false,
123
+ "single_word": false,
124
+ "special": false
125
+ },
126
+ "50267": {
127
+ "content": " ",
128
+ "lstrip": false,
129
+ "normalized": true,
130
+ "rstrip": false,
131
+ "single_word": false,
132
+ "special": false
133
+ },
134
+ "50268": {
135
+ "content": " ",
136
+ "lstrip": false,
137
+ "normalized": true,
138
+ "rstrip": false,
139
+ "single_word": false,
140
+ "special": false
141
+ },
142
+ "50269": {
143
+ "content": " ",
144
+ "lstrip": false,
145
+ "normalized": true,
146
+ "rstrip": false,
147
+ "single_word": false,
148
+ "special": false
149
+ },
150
+ "50270": {
151
+ "content": " ",
152
+ "lstrip": false,
153
+ "normalized": true,
154
+ "rstrip": false,
155
+ "single_word": false,
156
+ "special": false
157
+ },
158
+ "50271": {
159
+ "content": " ",
160
+ "lstrip": false,
161
+ "normalized": true,
162
+ "rstrip": false,
163
+ "single_word": false,
164
+ "special": false
165
+ },
166
+ "50272": {
167
+ "content": " ",
168
+ "lstrip": false,
169
+ "normalized": true,
170
+ "rstrip": false,
171
+ "single_word": false,
172
+ "special": false
173
+ },
174
+ "50273": {
175
+ "content": " ",
176
+ "lstrip": false,
177
+ "normalized": true,
178
+ "rstrip": false,
179
+ "single_word": false,
180
+ "special": false
181
+ },
182
+ "50274": {
183
+ "content": " ",
184
+ "lstrip": false,
185
+ "normalized": true,
186
+ "rstrip": false,
187
+ "single_word": false,
188
+ "special": false
189
+ },
190
+ "50275": {
191
+ "content": " ",
192
+ "lstrip": false,
193
+ "normalized": true,
194
+ "rstrip": false,
195
+ "single_word": false,
196
+ "special": false
197
+ },
198
+ "50276": {
199
+ "content": " ",
200
+ "lstrip": false,
201
+ "normalized": true,
202
+ "rstrip": false,
203
+ "single_word": false,
204
+ "special": false
205
+ }
206
+ },
207
+ "bos_token": "<|endoftext|>",
208
+ "clean_up_tokenization_spaces": true,
209
+ "eos_token": "<|endoftext|>",
210
+ "model_max_length": 1000000000000000019884624838656,
211
+ "pad_token": null,
212
+ "tokenizer_class": "GPTNeoXTokenizer",
213
+ "unk_token": "<|endoftext|>"
214
+ }