lievan commited on
Commit
3f2f02c
1 Parent(s): 3a3b3a9

Upload 3 files

Browse files
Files changed (2) hide show
  1. config.json +5 -4
  2. modeling_llama_rm.py +44 -0
config.json CHANGED
@@ -1,7 +1,7 @@
1
  {
2
- "_name_or_path": "meta-llama/Llama-2-13b-hf",
3
  "architectures": [
4
- "LlamaForCausalLM"
5
  ],
6
  "bos_token_id": 1,
7
  "eos_token_id": 2,
@@ -19,9 +19,10 @@
19
  "pretraining_tp": 1,
20
  "rms_norm_eps": 1e-05,
21
  "rope_scaling": null,
 
22
  "tie_word_embeddings": false,
23
- "torch_dtype": "float32",
24
- "transformers_version": "4.31.0",
25
  "use_cache": true,
26
  "vocab_size": 32000
27
  }
 
1
  {
2
+ "_name_or_path": "UltraRM-13b-32",
3
  "architectures": [
4
+ "LlamaRewardModel"
5
  ],
6
  "bos_token_id": 1,
7
  "eos_token_id": 2,
 
19
  "pretraining_tp": 1,
20
  "rms_norm_eps": 1e-05,
21
  "rope_scaling": null,
22
+ "rope_theta": 10000.0,
23
  "tie_word_embeddings": false,
24
+ "torch_dtype": "float16",
25
+ "transformers_version": "4.33.2",
26
  "use_cache": true,
27
  "vocab_size": 32000
28
  }
modeling_llama_rm.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PreTrainedModel, LlamaConfig, LlamaModel
2
+ import torch.nn as nn
3
+ import torch
4
+ from typing import Optional, List
5
+
6
+ class LlamaRewardModel(PreTrainedModel):
7
+ config_class = LlamaConfig
8
+ def __init__(self, config):
9
+ super().__init__(config)
10
+ self.model = LlamaModel(config)
11
+ self.regression_head = nn.Linear(self.config.hidden_size, 1, bias=False)
12
+
13
+ def forward( # args are the same as LlamaForCausalLM
14
+ self,
15
+ input_ids: torch.LongTensor = None,
16
+ attention_mask: Optional[torch.Tensor] = None,
17
+ position_ids: Optional[torch.LongTensor] = None,
18
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
19
+ inputs_embeds: Optional[torch.FloatTensor] = None,
20
+ labels: Optional[torch.LongTensor] = None,
21
+ use_cache: Optional[bool] = None,
22
+ output_attentions: Optional[bool] = None,
23
+ output_hidden_states: Optional[bool] = None,
24
+ return_dict: Optional[bool] = None,
25
+ ):
26
+
27
+ transformer_outputs = self.model.model(
28
+ input_ids,
29
+ attention_mask=attention_mask,
30
+ position_ids=position_ids,
31
+ past_key_values=past_key_values,
32
+ inputs_embeds=inputs_embeds,
33
+ )
34
+
35
+ hidden_states = transformer_outputs[0]
36
+ rewards = self.regression_head(hidden_states).squeeze(-1)
37
+
38
+ ends = attention_mask.cumsum(dim=1).argmax(dim=1).view(-1,1)
39
+ rewards = torch.gather(rewards, 1, ends)
40
+
41
+ return reward_models
42
+
43
+ model = LlamaRewardModel.from_pretrained("UltraRM-13b-32").half()
44
+ model.save_pretrained("UltraRM-13b")