denizyuret-shallowai commited on
Commit
1a4de68
·
1 Parent(s): 1039552

Upload model

Browse files
Files changed (3) hide show
  1. config.json +32 -0
  2. modeling_custom4.py +49 -0
  3. pytorch_model.bin +3 -0
config.json ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "EleutherAI/pythia-160m",
3
+ "architectures": [
4
+ "CustomModel4"
5
+ ],
6
+ "attention_dropout": 0.0,
7
+ "auto_map": {
8
+ "AutoModelForCausalLM": "modeling_custom4.CustomModel4"
9
+ },
10
+ "bos_token_id": 0,
11
+ "classifier_dropout": 0.1,
12
+ "eos_token_id": 0,
13
+ "hidden_act": "gelu",
14
+ "hidden_dropout": 0.0,
15
+ "hidden_size": 768,
16
+ "initializer_range": 0.02,
17
+ "intermediate_size": 3072,
18
+ "layer_norm_eps": 1e-05,
19
+ "max_position_embeddings": 2048,
20
+ "model_type": "gpt_neox",
21
+ "num_attention_heads": 12,
22
+ "num_hidden_layers": 12,
23
+ "rope_scaling": null,
24
+ "rotary_emb_base": 10000,
25
+ "rotary_pct": 0.25,
26
+ "tie_word_embeddings": false,
27
+ "torch_dtype": "float16",
28
+ "transformers_version": "4.31.0",
29
+ "use_cache": true,
30
+ "use_parallel_residual": true,
31
+ "vocab_size": 50304
32
+ }
modeling_custom4.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # https://huggingface.co/docs/transformers/custom_models
2
+
3
+ from transformers import PreTrainedModel, GPTNeoXForCausalLM, AutoModelForCausalLM, AutoTokenizer, LlamaConfig
4
+ from transformers.modeling_outputs import CausalLMOutputWithPast
5
+ from torch.nn.functional import log_softmax
6
+ from torch.nn.modules.container import ModuleList
7
+
8
+ class CustomModel4(PreTrainedModel):
9
+ config_class = LlamaConfig
10
+
11
+ def __init__(self, config):
12
+ super().__init__(config)
13
+
14
+ def forward(self, *args, labels=None, **kwargs):
15
+ loss = None
16
+ logits = None
17
+ for model, coeff in zip(self.models, self.coeffs):
18
+ logp = log_softmax(model.forward(*args, **kwargs).logits, dim=-1)
19
+ logits = coeff * logp if logits is None else logits + coeff * logp
20
+ # The rest copied from modeling_llama.py:
21
+ if labels is not None:
22
+ # Shift so that tokens < n predict n
23
+ shift_logits = logits[..., :-1, :].contiguous()
24
+ shift_labels = labels[..., 1:].contiguous()
25
+ # Flatten the tokens
26
+ loss_fct = CrossEntropyLoss()
27
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
28
+ shift_labels = shift_labels.view(-1)
29
+ # Enable model parallelism
30
+ shift_labels = shift_labels.to(shift_logits.device)
31
+ loss = loss_fct(shift_logits, shift_labels)
32
+
33
+ return CausalLMOutputWithPast(loss=loss, logits=logits)
34
+
35
+
36
+ @classmethod
37
+ def combine_models(cls, *args, coeffs = [], **kwargs):
38
+ models = []
39
+ for model in args:
40
+ models.append(AutoModelForCausalLM.from_pretrained(model, **kwargs).eval())
41
+ if coeffs == []:
42
+ coeffs = [1/len(args)] * len(args)
43
+ m = cls(models[0].config)
44
+ m.models = ModuleList(models)
45
+ m.coeffs = coeffs
46
+ return m
47
+
48
+
49
+ CustomModel4.register_for_auto_class('AutoModelForCausalLM')
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3ffa03b589263eccf2e09157196fab7b2abdaece84c8ed0f4b18f06540f48fd0
3
+ size 465579541