Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix lm_head type changed bug #215

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions supervised_finetuning.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,25 @@ class PeftArguments(TrainingArguments):
qlora: bool = field(default=False, metadata={"help": "Whether to use qlora"})


class CastOutputToFloat(torch.nn.Sequential):
class CastOutputToFloat(torch.nn.Module):
"""Cast the output of the model to float"""
def __init__(self, ori_linear: torch.nn.Linear) -> None:
super().__init__()
self.in_features = ori_linear.in_features
self.out_features = ori_linear.out_features
self.weight = ori_linear.weight
if ori_linear.bias is not None:
self.bias = ori_linear.bias
else:
self.register_parameter('bias', None)

def forward(self, input):
return torch.nn.functional.linear(input, self.weight, self.bias).to(torch.float32)

def forward(self, x):
return super().forward(x).to(torch.float32)
def extra_repr(self) -> str:
return 'in_features={}, out_features={}, bias={}'.format(
self.in_features, self.out_features, self.bias is not None
)


@dataclass
Expand Down