Views
No views yet
RewardModel class structure and load the provided weights.1import torch
2import torch.nn as nn
3import torch.nn.functional as F
4from transformers import AutoModel, AutoTokenizer
5
6class RewardModel(nn.Module):
7 def __init__(self, model_id, veto_beta=2.0, veto_tau=0.0):
8 super().__init__()
9 self.backbone = AutoModel.from_pretrained(model_id)
10 hidden = self.backbone.config.hidden_size
11 self.style_head = nn.Sequential(nn.LayerNorm(hidden), nn.Linear(hidden, hidden // 4), nn.GELU(), nn.Linear(hidden // 4, 1))
12 self.faith_head = nn.Sequential(nn.LayerNorm(hidden), nn.Linear(hidden, hidden // 4), nn.GELU(), nn.Linear(hidden // 4, 1))
13 self.veto_beta, self.veto_tau = veto_beta, veto_tau
14
15 def forward(self, enc):
16 hidden = self.backbone(**enc).last_hidden_state
17 mask = enc['attention_mask'].unsqueeze(-1).float()
18 pooled = (hidden * mask).sum(dim=1) / mask.sum(dim=1).clamp(min=1e-6)
19 style = self.style_head(pooled).squeeze(-1)
20 faith = self.faith_head(pooled).squeeze(-1)
21 reward = style - self.veto_beta * F.softplus(self.veto_tau - faith)
22 return reward
23
24# 1. Load Model & Tokenizer
25repo_id = "3rd-Degree-Burn/stylerm-v2"
26tokenizer = AutoTokenizer.from_pretrained(repo_id)
27model = RewardModel("answerdotai/ModernBERT-large")
28
29# Load weights from model.pt
30checkpoint = torch.load("model.pt", map_location="cpu")
31model.load_state_dict(checkpoint['state_dict'])
32model.eval()
33
34# 2. Score Candidates
35source = "The storm came quickly and covered the whole valley in clouds."
36candidates = [
37 "The storm arrived and there were clouds everywhere in the valley.",
38 "The storm broke with a sudden, violet urgency, swaddling the valley in a thick, suffocating wool of grey."
39]
40
41with torch.no_grad():
42 inputs = tokenizer([source]*len(candidates), candidates, padding=True, truncation=True, return_tensors="pt")
43 inputs.pop("token_type_ids", None)
44 scores = model(inputs)
45
46for i, score in enumerate(scores):
47 print(f"Candidate {i} Reward: {score.item():.4f}")