Upload model
Browse files- README.md +199 -0
- config.json +63 -0
- generation_config.json +7 -0
- hf_utils.py +301 -0
- model.safetensors +3 -0
README.md
ADDED
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
library_name: transformers
|
3 |
+
tags: []
|
4 |
+
---
|
5 |
+
|
6 |
+
# Model Card for Model ID
|
7 |
+
|
8 |
+
<!-- Provide a quick summary of what the model is/does. -->
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
## Model Details
|
13 |
+
|
14 |
+
### Model Description
|
15 |
+
|
16 |
+
<!-- Provide a longer summary of what this model is. -->
|
17 |
+
|
18 |
+
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
|
19 |
+
|
20 |
+
- **Developed by:** [More Information Needed]
|
21 |
+
- **Funded by [optional]:** [More Information Needed]
|
22 |
+
- **Shared by [optional]:** [More Information Needed]
|
23 |
+
- **Model type:** [More Information Needed]
|
24 |
+
- **Language(s) (NLP):** [More Information Needed]
|
25 |
+
- **License:** [More Information Needed]
|
26 |
+
- **Finetuned from model [optional]:** [More Information Needed]
|
27 |
+
|
28 |
+
### Model Sources [optional]
|
29 |
+
|
30 |
+
<!-- Provide the basic links for the model. -->
|
31 |
+
|
32 |
+
- **Repository:** [More Information Needed]
|
33 |
+
- **Paper [optional]:** [More Information Needed]
|
34 |
+
- **Demo [optional]:** [More Information Needed]
|
35 |
+
|
36 |
+
## Uses
|
37 |
+
|
38 |
+
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
39 |
+
|
40 |
+
### Direct Use
|
41 |
+
|
42 |
+
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
43 |
+
|
44 |
+
[More Information Needed]
|
45 |
+
|
46 |
+
### Downstream Use [optional]
|
47 |
+
|
48 |
+
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
49 |
+
|
50 |
+
[More Information Needed]
|
51 |
+
|
52 |
+
### Out-of-Scope Use
|
53 |
+
|
54 |
+
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
55 |
+
|
56 |
+
[More Information Needed]
|
57 |
+
|
58 |
+
## Bias, Risks, and Limitations
|
59 |
+
|
60 |
+
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
61 |
+
|
62 |
+
[More Information Needed]
|
63 |
+
|
64 |
+
### Recommendations
|
65 |
+
|
66 |
+
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
67 |
+
|
68 |
+
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
69 |
+
|
70 |
+
## How to Get Started with the Model
|
71 |
+
|
72 |
+
Use the code below to get started with the model.
|
73 |
+
|
74 |
+
[More Information Needed]
|
75 |
+
|
76 |
+
## Training Details
|
77 |
+
|
78 |
+
### Training Data
|
79 |
+
|
80 |
+
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
81 |
+
|
82 |
+
[More Information Needed]
|
83 |
+
|
84 |
+
### Training Procedure
|
85 |
+
|
86 |
+
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
87 |
+
|
88 |
+
#### Preprocessing [optional]
|
89 |
+
|
90 |
+
[More Information Needed]
|
91 |
+
|
92 |
+
|
93 |
+
#### Training Hyperparameters
|
94 |
+
|
95 |
+
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
96 |
+
|
97 |
+
#### Speeds, Sizes, Times [optional]
|
98 |
+
|
99 |
+
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
100 |
+
|
101 |
+
[More Information Needed]
|
102 |
+
|
103 |
+
## Evaluation
|
104 |
+
|
105 |
+
<!-- This section describes the evaluation protocols and provides the results. -->
|
106 |
+
|
107 |
+
### Testing Data, Factors & Metrics
|
108 |
+
|
109 |
+
#### Testing Data
|
110 |
+
|
111 |
+
<!-- This should link to a Dataset Card if possible. -->
|
112 |
+
|
113 |
+
[More Information Needed]
|
114 |
+
|
115 |
+
#### Factors
|
116 |
+
|
117 |
+
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
118 |
+
|
119 |
+
[More Information Needed]
|
120 |
+
|
121 |
+
#### Metrics
|
122 |
+
|
123 |
+
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
124 |
+
|
125 |
+
[More Information Needed]
|
126 |
+
|
127 |
+
### Results
|
128 |
+
|
129 |
+
[More Information Needed]
|
130 |
+
|
131 |
+
#### Summary
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
## Model Examination [optional]
|
136 |
+
|
137 |
+
<!-- Relevant interpretability work for the model goes here -->
|
138 |
+
|
139 |
+
[More Information Needed]
|
140 |
+
|
141 |
+
## Environmental Impact
|
142 |
+
|
143 |
+
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
144 |
+
|
145 |
+
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
146 |
+
|
147 |
+
- **Hardware Type:** [More Information Needed]
|
148 |
+
- **Hours used:** [More Information Needed]
|
149 |
+
- **Cloud Provider:** [More Information Needed]
|
150 |
+
- **Compute Region:** [More Information Needed]
|
151 |
+
- **Carbon Emitted:** [More Information Needed]
|
152 |
+
|
153 |
+
## Technical Specifications [optional]
|
154 |
+
|
155 |
+
### Model Architecture and Objective
|
156 |
+
|
157 |
+
[More Information Needed]
|
158 |
+
|
159 |
+
### Compute Infrastructure
|
160 |
+
|
161 |
+
[More Information Needed]
|
162 |
+
|
163 |
+
#### Hardware
|
164 |
+
|
165 |
+
[More Information Needed]
|
166 |
+
|
167 |
+
#### Software
|
168 |
+
|
169 |
+
[More Information Needed]
|
170 |
+
|
171 |
+
## Citation [optional]
|
172 |
+
|
173 |
+
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
174 |
+
|
175 |
+
**BibTeX:**
|
176 |
+
|
177 |
+
[More Information Needed]
|
178 |
+
|
179 |
+
**APA:**
|
180 |
+
|
181 |
+
[More Information Needed]
|
182 |
+
|
183 |
+
## Glossary [optional]
|
184 |
+
|
185 |
+
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
186 |
+
|
187 |
+
[More Information Needed]
|
188 |
+
|
189 |
+
## More Information [optional]
|
190 |
+
|
191 |
+
[More Information Needed]
|
192 |
+
|
193 |
+
## Model Card Authors [optional]
|
194 |
+
|
195 |
+
[More Information Needed]
|
196 |
+
|
197 |
+
## Model Card Contact
|
198 |
+
|
199 |
+
[More Information Needed]
|
config.json
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"AutoModelForCausalLMWithRM"
|
4 |
+
],
|
5 |
+
"attention_bias": false,
|
6 |
+
"attention_dropout": 0.0,
|
7 |
+
"auto_map": {
|
8 |
+
"AutoConfig": "hf_utils.RewardModelConfig",
|
9 |
+
"AutoModel": "hf_utils.AutoModelForCausalLMWithRM"
|
10 |
+
},
|
11 |
+
"base_config": {
|
12 |
+
"_name_or_path": "jdchang/llama3-small",
|
13 |
+
"architectures": [
|
14 |
+
"LlamaForCausalLM"
|
15 |
+
],
|
16 |
+
"bos_token_id": 128000,
|
17 |
+
"eos_token_id": 128009,
|
18 |
+
"hidden_size": 512,
|
19 |
+
"intermediate_size": 14336,
|
20 |
+
"max_position_embeddings": 8192,
|
21 |
+
"model_type": "llama",
|
22 |
+
"num_attention_heads": 32,
|
23 |
+
"num_hidden_layers": 2,
|
24 |
+
"num_key_value_heads": 8,
|
25 |
+
"rms_norm_eps": 1e-05,
|
26 |
+
"rope_theta": 500000.0,
|
27 |
+
"torch_dtype": "float32",
|
28 |
+
"use_cache": false,
|
29 |
+
"vocab_size": 128257
|
30 |
+
},
|
31 |
+
"base_model": "jdchang/llama3-small",
|
32 |
+
"bias": 0.0,
|
33 |
+
"bos_token_id": 128000,
|
34 |
+
"eos_token_id": 128009,
|
35 |
+
"hidden_act": "silu",
|
36 |
+
"hidden_size": 512,
|
37 |
+
"initializer_range": 0.02,
|
38 |
+
"intermediate_size": 14336,
|
39 |
+
"max_position_embeddings": 8192,
|
40 |
+
"mlp_bias": false,
|
41 |
+
"model_type": "pairwise_rm",
|
42 |
+
"n_labels": 1,
|
43 |
+
"num_attention_heads": 32,
|
44 |
+
"num_hidden_layers": 2,
|
45 |
+
"num_key_value_heads": 8,
|
46 |
+
"p_dropout": 0.0,
|
47 |
+
"pretrain_cfg": {
|
48 |
+
"load_in_8bit": false,
|
49 |
+
"token": true,
|
50 |
+
"trust_remote_code": null
|
51 |
+
},
|
52 |
+
"pretrained": true,
|
53 |
+
"pretraining_tp": 1,
|
54 |
+
"return_logits": false,
|
55 |
+
"rms_norm_eps": 1e-05,
|
56 |
+
"rope_scaling": null,
|
57 |
+
"rope_theta": 500000.0,
|
58 |
+
"tie_word_embeddings": false,
|
59 |
+
"torch_dtype": "float32",
|
60 |
+
"transformers_version": "4.43.4",
|
61 |
+
"use_cache": false,
|
62 |
+
"vocab_size": 128257
|
63 |
+
}
|
generation_config.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_from_model_config": true,
|
3 |
+
"bos_token_id": 128000,
|
4 |
+
"eos_token_id": 128009,
|
5 |
+
"transformers_version": "4.43.4",
|
6 |
+
"use_cache": false
|
7 |
+
}
|
hf_utils.py
ADDED
@@ -0,0 +1,301 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2024 MosaicML ComposeRL authors
|
2 |
+
# SPDX-License-Identifier: Apache-2.0
|
3 |
+
|
4 |
+
import os
|
5 |
+
from copy import deepcopy
|
6 |
+
from dataclasses import dataclass
|
7 |
+
from typing import (
|
8 |
+
Any,
|
9 |
+
Optional,
|
10 |
+
Union,
|
11 |
+
)
|
12 |
+
|
13 |
+
import numpy as np
|
14 |
+
import torch
|
15 |
+
import torch.nn as nn
|
16 |
+
from transformers import (
|
17 |
+
AutoConfig,
|
18 |
+
AutoModelForCausalLM,
|
19 |
+
PretrainedConfig,
|
20 |
+
PreTrainedModel,
|
21 |
+
)
|
22 |
+
from transformers.modeling_outputs import ModelOutput
|
23 |
+
|
24 |
+
|
25 |
+
@dataclass
|
26 |
+
class SequenceClassifierOutput(ModelOutput):
|
27 |
+
"""Sequence Classification Output.
|
28 |
+
|
29 |
+
Args:
|
30 |
+
loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
|
31 |
+
Classification (or regression if config.num_labels==1) loss.
|
32 |
+
scores (`torch.FloatTensor` of shape `(batch_size, config.num_labels)`):
|
33 |
+
Classification (or regression if config.num_labels==1) scores (before SoftMax).
|
34 |
+
logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`):
|
35 |
+
Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
|
36 |
+
past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
|
37 |
+
tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
|
38 |
+
`(batch_size, num_heads, sequence_length, embed_size_per_head)`)
|
39 |
+
|
40 |
+
Contains pre-computed hidden-states (key and values in the self-attention blocks) that can be used (see
|
41 |
+
`past_key_values` input) to speed up sequential decoding.
|
42 |
+
hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
|
43 |
+
tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
|
44 |
+
one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.
|
45 |
+
|
46 |
+
Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
|
47 |
+
attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
|
48 |
+
tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
|
49 |
+
sequence_length)`.
|
50 |
+
|
51 |
+
Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
|
52 |
+
heads.
|
53 |
+
"""
|
54 |
+
|
55 |
+
loss: Optional[torch.FloatTensor] = None
|
56 |
+
scores: Optional[torch.FloatTensor] = None
|
57 |
+
logits: Optional[torch.FloatTensor] = None
|
58 |
+
past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None
|
59 |
+
hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None
|
60 |
+
attentions: Optional[tuple[torch.FloatTensor, ...]] = None
|
61 |
+
|
62 |
+
|
63 |
+
class ValueHead(nn.Module):
|
64 |
+
"""Value head for the transformer which outputs n_labels values."""
|
65 |
+
|
66 |
+
def __init__(self, n_labels: int, hidden_size: int, p_dropout: float = 0.0):
|
67 |
+
super().__init__()
|
68 |
+
self.dense = nn.Linear(hidden_size, hidden_size)
|
69 |
+
self.dropout = nn.Dropout(p_dropout)
|
70 |
+
self.score = nn.Linear(hidden_size, n_labels)
|
71 |
+
torch.nn.init.normal_(
|
72 |
+
self.score.weight,
|
73 |
+
std=1 / np.sqrt(hidden_size + 1),
|
74 |
+
)
|
75 |
+
torch.nn.init.constant_(self.score.bias, val=0.0)
|
76 |
+
|
77 |
+
def forward(
|
78 |
+
self,
|
79 |
+
hidden_states: torch.Tensor,
|
80 |
+
**kwargs: Any,
|
81 |
+
) -> torch.Tensor:
|
82 |
+
hidden_states = self.dropout(hidden_states)
|
83 |
+
hidden_states = self.dense(hidden_states)
|
84 |
+
hidden_states = torch.tanh(hidden_states)
|
85 |
+
hidden_states = self.dropout(hidden_states)
|
86 |
+
output = self.score(hidden_states)
|
87 |
+
return output
|
88 |
+
|
89 |
+
|
90 |
+
class RewardModelConfig(PretrainedConfig):
|
91 |
+
model_type = 'pairwise_rm'
|
92 |
+
|
93 |
+
def __init__(
|
94 |
+
self,
|
95 |
+
base_model: Optional[Union[str, os.PathLike]
|
96 |
+
] = 'meta-llama/Meta-Llama-3-70B-Instruct',
|
97 |
+
base_config: Optional[PretrainedConfig] = None,
|
98 |
+
p_dropout: float = 0.0,
|
99 |
+
n_labels: int = 1,
|
100 |
+
bias: float = 0.0,
|
101 |
+
return_logits: bool = False,
|
102 |
+
pretrain_cfg: Optional[dict[str, Any]] = None,
|
103 |
+
pretrained: bool = False,
|
104 |
+
**kwargs: Any,
|
105 |
+
):
|
106 |
+
super().__init__(**kwargs)
|
107 |
+
self.base_model = base_model
|
108 |
+
self.base_config = base_config if base_config is not None else AutoConfig.from_pretrained(
|
109 |
+
base_model,
|
110 |
+
)
|
111 |
+
temp_config = deepcopy(self.base_config)
|
112 |
+
if not isinstance(temp_config, dict):
|
113 |
+
temp_config = temp_config.__dict__
|
114 |
+
for key, value in temp_config.items():
|
115 |
+
if key not in ['_name_or_path', 'architectures']:
|
116 |
+
setattr(self, key, value)
|
117 |
+
self.p_dropout = p_dropout
|
118 |
+
self.n_labels = n_labels
|
119 |
+
self.bias = bias
|
120 |
+
self.return_logits = return_logits
|
121 |
+
self.pretrain_cfg = pretrain_cfg if pretrain_cfg is not None else {}
|
122 |
+
self.pretrained = pretrained
|
123 |
+
|
124 |
+
|
125 |
+
class AutoModelForCausalLMWithRM(PreTrainedModel):
|
126 |
+
config_class = RewardModelConfig
|
127 |
+
|
128 |
+
def __init__(self, config: RewardModelConfig):
|
129 |
+
super().__init__(config)
|
130 |
+
self.config = config
|
131 |
+
pretrain_cfg = config.pretrain_cfg
|
132 |
+
pretrained = config.pretrained
|
133 |
+
if pretrained:
|
134 |
+
self.lm_backbone = AutoModelForCausalLM.from_pretrained(
|
135 |
+
config.base_model,
|
136 |
+
config=config.base_config,
|
137 |
+
**pretrain_cfg,
|
138 |
+
)
|
139 |
+
else:
|
140 |
+
#hack for now
|
141 |
+
if isinstance(config.base_config, dict):
|
142 |
+
config.base_config = AutoConfig.from_pretrained(
|
143 |
+
config.base_model,
|
144 |
+
**config.base_config,
|
145 |
+
)
|
146 |
+
self.lm_backbone = AutoModelForCausalLM.from_config(
|
147 |
+
config.base_config,
|
148 |
+
trust_remote_code=True,
|
149 |
+
)
|
150 |
+
self.value_head = ValueHead(
|
151 |
+
n_labels=self.config.n_labels,
|
152 |
+
hidden_size=self.config.hidden_size,
|
153 |
+
p_dropout=self.config.p_dropout,
|
154 |
+
)
|
155 |
+
|
156 |
+
def generate(self, *args: Any, **kwargs: Any):
|
157 |
+
return self.lm_backbone.generate(**kwargs)
|
158 |
+
|
159 |
+
def resize_token_embeddings(
|
160 |
+
self,
|
161 |
+
new_num_tokens: Optional[int] = None,
|
162 |
+
pad_to_multiple_of: Optional[int] = None,
|
163 |
+
) -> nn.Embedding:
|
164 |
+
# Note need to update vocab size in base config as well so lm_head modification happens
|
165 |
+
self.config.base_config.vocab_size = new_num_tokens
|
166 |
+
model_embeds = super().resize_token_embeddings(
|
167 |
+
new_num_tokens=new_num_tokens,
|
168 |
+
pad_to_multiple_of=pad_to_multiple_of,
|
169 |
+
)
|
170 |
+
return model_embeds
|
171 |
+
|
172 |
+
def set_input_embeddings(self, new_embeddings: Any):
|
173 |
+
return self.lm_backbone.set_input_embeddings(new_embeddings)
|
174 |
+
|
175 |
+
def get_input_embeddings(self):
|
176 |
+
return self.lm_backbone.get_input_embeddings()
|
177 |
+
|
178 |
+
def set_output_embeddings(self, new_embeddings: Any):
|
179 |
+
return self.lm_backbone.set_output_embeddings(new_embeddings)
|
180 |
+
|
181 |
+
def get_output_embeddings(self):
|
182 |
+
return self.lm_backbone.get_output_embeddings()
|
183 |
+
|
184 |
+
def forward(
|
185 |
+
self,
|
186 |
+
input_ids: Optional[torch.LongTensor] = None,
|
187 |
+
attention_mask: Optional[torch.Tensor] = None,
|
188 |
+
position_ids: Optional[torch.LongTensor] = None,
|
189 |
+
past_key_values: Optional[Any] = None,
|
190 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
191 |
+
labels: Optional[torch.LongTensor] = None,
|
192 |
+
use_cache: Optional[bool] = None,
|
193 |
+
output_attentions: Optional[bool] = None,
|
194 |
+
output_hidden_states: Optional[bool] = None,
|
195 |
+
return_dict: Optional[bool] = None,
|
196 |
+
cache_position: Optional[torch.LongTensor] = None,
|
197 |
+
**kwargs: Any,
|
198 |
+
):
|
199 |
+
output = self.lm_backbone(
|
200 |
+
input_ids=input_ids,
|
201 |
+
attention_mask=attention_mask,
|
202 |
+
position_ids=position_ids,
|
203 |
+
past_key_values=past_key_values,
|
204 |
+
inputs_embeds=inputs_embeds,
|
205 |
+
labels=labels,
|
206 |
+
use_cache=use_cache,
|
207 |
+
output_attentions=output_attentions,
|
208 |
+
output_hidden_states=True,
|
209 |
+
return_dict=True,
|
210 |
+
cache_position=cache_position,
|
211 |
+
)
|
212 |
+
scores = self.value_head(
|
213 |
+
output.hidden_states[-1],
|
214 |
+
).squeeze(-1) - self.config.bias
|
215 |
+
|
216 |
+
logits = None
|
217 |
+
if self.config.return_logits:
|
218 |
+
logits = output.logits
|
219 |
+
|
220 |
+
return SequenceClassifierOutput(
|
221 |
+
loss=output.loss,
|
222 |
+
scores=scores,
|
223 |
+
logits=logits,
|
224 |
+
past_key_values=output.past_key_values,
|
225 |
+
hidden_states=output.hidden_states,
|
226 |
+
attentions=output.attentions,
|
227 |
+
)
|
228 |
+
|
229 |
+
@classmethod
|
230 |
+
def from_config(
|
231 |
+
cls,
|
232 |
+
config: PretrainedConfig,
|
233 |
+
**kwargs: Any,
|
234 |
+
) -> PreTrainedModel:
|
235 |
+
return cls._from_config(config, **kwargs)
|
236 |
+
|
237 |
+
@classmethod
|
238 |
+
def from_pretrained(
|
239 |
+
cls,
|
240 |
+
pretrained_model_name_or_path: Optional[Union[str, os.PathLike]],
|
241 |
+
*model_args: Any,
|
242 |
+
config: Optional[Union[PretrainedConfig, str, os.PathLike]] = None,
|
243 |
+
cache_dir: Optional[Union[str, os.PathLike]] = None,
|
244 |
+
ignore_mismatched_sizes: bool = False,
|
245 |
+
force_download: bool = False,
|
246 |
+
local_files_only: bool = False,
|
247 |
+
token: Optional[Union[str, bool]] = None,
|
248 |
+
revision: str = 'main',
|
249 |
+
use_safetensors: Optional[bool] = None,
|
250 |
+
**kwargs: Any,
|
251 |
+
) -> PreTrainedModel:
|
252 |
+
trust_remote_code = kwargs.pop('trust_remote_code', None)
|
253 |
+
use_flash_attention_2 = kwargs.pop('use_flash_attention_2', False)
|
254 |
+
return_lm_logits = kwargs.pop('return_lm_logits', False)
|
255 |
+
load_in_8bit = kwargs.pop('load_in_8bit', False)
|
256 |
+
|
257 |
+
requested_attention_implementation = 'flash_attention_2' if use_flash_attention_2 else 'eager'
|
258 |
+
|
259 |
+
pretrained_model_config = AutoConfig.from_pretrained(
|
260 |
+
pretrained_model_name_or_path,
|
261 |
+
trust_remote_code=trust_remote_code,
|
262 |
+
token=True,
|
263 |
+
attn_implementation=requested_attention_implementation,
|
264 |
+
use_cache=False,
|
265 |
+
)
|
266 |
+
|
267 |
+
if isinstance(pretrained_model_config, cls.config_class):
|
268 |
+
return super().from_pretrained(
|
269 |
+
pretrained_model_name_or_path,
|
270 |
+
*model_args,
|
271 |
+
config,
|
272 |
+
cache_dir,
|
273 |
+
ignore_mismatched_sizes,
|
274 |
+
force_download,
|
275 |
+
local_files_only,
|
276 |
+
token,
|
277 |
+
revision,
|
278 |
+
use_safetensors,
|
279 |
+
**kwargs,
|
280 |
+
)
|
281 |
+
|
282 |
+
pretrain_cfg = {
|
283 |
+
'trust_remote_code': trust_remote_code,
|
284 |
+
'token': True,
|
285 |
+
'load_in_8bit': load_in_8bit,
|
286 |
+
}
|
287 |
+
|
288 |
+
reward_model_config = RewardModelConfig(
|
289 |
+
base_model=pretrained_model_name_or_path,
|
290 |
+
base_config=pretrained_model_config,
|
291 |
+
hidden_size=pretrained_model_config.hidden_size,
|
292 |
+
torch_dtype=pretrained_model_config.torch_dtype,
|
293 |
+
return_logits=return_lm_logits,
|
294 |
+
vocab_size=pretrained_model_config.vocab_size,
|
295 |
+
pretrained=True,
|
296 |
+
pretrain_cfg=pretrain_cfg,
|
297 |
+
)
|
298 |
+
|
299 |
+
model = cls(reward_model_config)
|
300 |
+
|
301 |
+
return model
|
model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:325f61b549410374cd5b1ead77043e6b79383b429a0bdeffa7d89102f4f65b64
|
3 |
+
size 707810180
|