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

add scaling #8256

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Changes from 3 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
12 changes: 12 additions & 0 deletions paddlenlp/peft/lora/lora_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import json
import os
import math
from dataclasses import asdict, dataclass, field
from typing import List, Optional, Union

Expand Down Expand Up @@ -94,6 +95,15 @@ def __post_init__(self):
)
self.use_quick_lora = False

@property
def scaling(self):
if not self.rslora and not self.pissa:
return self.lora_alpha / self.r
elif self.pissa:
return 1.0
else:
return self.lora_alpha / math.sqrt(self.r)

@property
def __dict__(self):
return asdict(self)
Expand All @@ -114,6 +124,7 @@ def save_pretrained(self, save_directory):
os.makedirs(save_directory, exist_ok=True)

output_dict = self.__dict__
output_dict["scaling"] = self.scaling
output_path = os.path.join(save_directory, LORA_CONFIG_NAME)

# save it
Expand All @@ -136,6 +147,7 @@ def from_pretrained(cls, pretrained_model_name_or_path, **kwargs):
raise ValueError(f"Can't find lora_config.json at '{pretrained_model_name_or_path}'")

loaded_attributes = cls.from_json_file(config_file)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉应该在from_json_file的时候就pop了

loaded_attributes.pop("scaling", None)

config = cls(**kwargs)

Expand Down
Loading