RewardModel-Mistral-7B-for-DPA-v1 / modeling_custom.py
Haoxiang-Wang's picture
Rename modelling_custom.py to modeling_custom.py
11c7781 verified
raw
history blame
624 Bytes
import torch
from torch.nn import functional as F
from transformers.models.mistral.modeling_mistral import MistralForSequenceClassification
class NormalizedLinear(torch.nn.Linear):
def forward(self, x):
x = F.normalize(x, p=2, dim=-1)
return super().forward(x)
class MistralForAttributePrediction(MistralForSequenceClassification):
def __init__(self, config):
super().__init__(config)
del self.score
self.score = NormalizedLinear(config.hidden_size, config.num_labels, bias=True)
# Initialize weights and apply final processing
self.post_init()