denizyuret-shallowai commited on
Commit
0e6dbed
·
1 Parent(s): 6fea841

Upload model

Browse files
config.json ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "CustomModel5"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "configuration_custom5.CustomConfig5",
7
+ "AutoModelForCausalLM": "modeling_custom5.CustomModel5"
8
+ },
9
+ "coeffs": [
10
+ 1.0,
11
+ -1.0
12
+ ],
13
+ "model_type": "custom5",
14
+ "models": [
15
+ "EleutherAI/pythia-160m",
16
+ "EleutherAI/pythia-70m"
17
+ ],
18
+ "torch_dtype": "float16",
19
+ "transformers_version": "4.34.0",
20
+ "vocab_size": 50304
21
+ }
configuration_custom5.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig, AutoConfig
2
+
3
+ class CustomConfig5(PretrainedConfig):
4
+ model_type = "custom5"
5
+
6
+ def __init__(
7
+ self,
8
+ models=None,
9
+ coeffs=None,
10
+ **kwargs,
11
+ ):
12
+ super().__init__(**kwargs)
13
+ self.models = models
14
+ self.coeffs = coeffs
15
+ if models:
16
+ configs = [ AutoConfig.from_pretrained(model) for model in models ]
17
+ self.vocab_size = configs[0].vocab_size
modeling_custom5.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # https://huggingface.co/docs/transformers/custom_models
2
+
3
+ from transformers import PreTrainedModel, AutoModelForCausalLM, AutoTokenizer, AutoModel, AutoConfig
4
+ from transformers.modeling_outputs import CausalLMOutputWithPast
5
+ from torch.nn import CrossEntropyLoss
6
+ from torch.nn.functional import log_softmax
7
+ from torch.nn.modules.container import ModuleList
8
+ from .configuration_custom5 import CustomConfig5
9
+
10
+ class CustomModel5(PreTrainedModel):
11
+ config_class = CustomConfig5
12
+
13
+ def __init__(self, config):
14
+ super().__init__(config)
15
+ self.model = ModuleList([AutoModelForCausalLM.from_pretrained(m) for m in config.models])
16
+
17
+ def forward(self, *args, labels=None, **kwargs):
18
+ loss = None
19
+ logits = None
20
+ for model, coeff in zip(self.model, self.config.coeffs):
21
+ logp = log_softmax(model.forward(*args, **kwargs).logits, dim=-1)
22
+ logits = coeff * logp if logits is None else logits + coeff * logp
23
+ # The rest copied from modeling_llama.py:
24
+ if labels is not None:
25
+ # Shift so that tokens < n predict n
26
+ shift_logits = logits[..., :-1, :].contiguous()
27
+ shift_labels = labels[..., 1:].contiguous()
28
+ # Flatten the tokens
29
+ loss_fct = CrossEntropyLoss()
30
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
31
+ shift_labels = shift_labels.view(-1)
32
+ # Enable model parallelism
33
+ shift_labels = shift_labels.to(shift_logits.device)
34
+ loss = loss_fct(shift_logits, shift_labels)
35
+
36
+ return CausalLMOutputWithPast(loss=loss, logits=logits)
37
+
38
+ ## Which one do we use?
39
+ ## You have to tell the library you want to copy the code files of those objects when using the save_pretrained method and properly register them with a given Auto class (especially for models), just run:
40
+ # CustomConfig5.register_for_auto_class()
41
+ # CustomModel5.register_for_auto_class('AutoModelForCausalLM')
42
+ # CustomModel5.register_for_auto_class('AutoModel')
43
+
44
+ ## If you are writing a library that extends 🤗 Transformers, you may want to extend the auto classes to include your own model. This is different from pushing the code to the Hub in the sense that users will need to import your library to get the custom models (contrarily to automatically downloading the model code from the Hub).
45
+ ## As long as your config has a model_type attribute that is different from existing model types, and that your model classes have the right config_class attributes, you can just add them to the auto classes like this:
46
+ # AutoConfig.register("custom5", CustomConfig5)
47
+ # AutoModel.register(CustomConfig5, CustomModel5)
48
+ # AutoModelForCausalLM.register(CustomConfig5, CustomModel5)
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d39c55bd4bf0be5a6d21ae200275a23b07f3fbd4c803959ce3341a5210a2aea1
3
+ size 465582677