Views
No views yet

transformers>=4.45.0, to avoid any potential errors when using this model.reward_weights below.1from transformers import AutoTokenizer, AutoModel, AutoConfig, LlamaConfig, PreTrainedModel, LlamaForSequenceClassification
2import torch.nn as nn
3import torch
4
5# Login to HF to access LLAMA model
6from huggingface_hub import login
7login("") # HF token
8
9class RewardModelConfig(LlamaConfig):
10 model_type = "RewardModel"
11
12 def __init__(self, reward_dim=None, base_model_name=None, **kwargs):
13 super().__init__(**kwargs)
14
15 self.reward_dim = reward_dim
16 self.base_model_name = base_model_name
17
18class RewardModel(PreTrainedModel):
19 config_class = RewardModelConfig
20
21 def create_base_model(self):
22
23 # use sequence classification model for consistency with https://huggingface.co/sfairXC/FsfairX-LLaMA3-RM-v0.1
24 BACKBONE_MODEL = LlamaForSequenceClassification.from_pretrained(
25 self.config.base_model_name,
26 config=LlamaConfig.from_pretrained(self.config.base_model_name),
27 )
28 BACKBONE_MODEL.config.pad_token_id = BACKBONE_MODEL.config.eos_token_id
29 BACKBONE_MODEL.config.output_hidden_states = True
30
31 for param in BACKBONE_MODEL.parameters():
32 param.requires_grad = False
33
34 return BACKBONE_MODEL
35
36 def __init__(self, config):
37 super(RewardModel, self).__init__(config)
38
39 # use .base_model to remove lm_head
40 self.BASE_MODEL = self.create_base_model().base_model
41
42 # regression head for reward prediction
43 self.regression_head = nn.Linear(self.BASE_MODEL.config.hidden_size, config.reward_dim)
44
45 def forward(self, input_ids, attention_mask=None, rewards=None, **kwargs):
46
47 # forward pass through the base model
48 outputs = self.BASE_MODEL(input_ids, attention_mask=attention_mask, **kwargs)
49
50 hidden_states = outputs.hidden_states[-1]
51
52 # access hidden state corresponding to the last token in each sequence across the batch
53 last_token_hidden_state = hidden_states[:, -1, :]
54 reward_predictions = self.regression_head(last_token_hidden_state)
55
56 return reward_predictions
57
58 def prepare_inputs_for_generation(self, *args, **kwargs):
59 return self.BASE_MODEL.prepare_inputs_for_generation(*args, **kwargs)
60
61AutoConfig.register("RewardModel", RewardModelConfig)
62AutoModel.register(RewardModelConfig, RewardModel)
63
64model = AutoModel.from_pretrained("yale-nlp/MDCureRM").to(torch.device("cuda"))
65tokenizer = AutoTokenizer.from_pretrained("yale-nlp/MDCureRM", use_fast=True)
66tokenizer.pad_token = tokenizer.eos_token
67
68reward_weights = torch.tensor([1/9, 1/9, 1/9, 2/9, 2/9, 2/9], device="cuda")
69
70source_text_1 = ...
71source_text_2 = ...
72source_text_3 = ...
73context = f"{source_text_1}
74
75{source_text_2}
76
77{source_text_3}"
78instruction = "What happened in CHAMPAIGN regarding Lovie Smith and the 2019 defense improvements? Respond with 1-2 sentences."
79
80input_text = f"Instruction: {instruction}
81
82{context}"
83tokenized_input = tokenizer(
84 input_text,
85 return_tensors='pt',
86 truncation=True,
87 padding=True,
88 ).to(torch.device("cuda"))
89
90all_six_scores = model(tokenized_input["input_ids"]).squeeze(0) # flatten for dot product
91all_six_scores = all_six_scores*4. + 1. # scale up to 1->5 range
92
93final_score = torch.dot(all_six_scores, reward_weights).cpu().item()
94print(score)| Model | Huggingface Repo | Description |
|---|---|---|
| MDCure-FlanT5-Base | 🤗 HF Repo | FlanT5-Base fine-tuned with MDCure-72k |
| MDCure-FlanT5-Large | 🤗 HF Repo | FlanT5-Large fine-tuned with MDCure-72k |
| MDCure-Qwen2-1.5B-Instruct | 🤗 HF Repo | Qwen2-1.5B-Instruct fine-tuned with MDCure-72k |
| MDCure-Qwen2-7B-Instruct | 🤗 HF Repo | Qwen2-7B-Instruct fine-tuned with MDCure-72k |
| MDCure-LLAMA3.1-8B-Instruct | 🤗 HF Repo | LLAMA3.1-8B-Instruct fine-tuned with MDCure-72k |
| MDCure-LLAMA3.1-70B-Instruct | 🤗 HF Repo | LLAMA3.1-70B-Instruct fine-tuned with MDCure-72k |
1@article{liu2024mdcure,
2 title={MDCure: A Scalable Pipeline for Multi-Document Instruction-Following},
3 author={Gabrielle Kaili-May Liu and Bowen Shi and Avi Caciularu and Idan Szpektor and Arman Cohan},
4 journal={arXiv preprint arXiv:2410.23463},
5 year={2024},
6 url={https://arxiv.org/abs/2410.23463}
7}