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

Refactor HuggingFace model initialization to include base model name … #190

Merged
merged 1 commit into from
Dec 20, 2024
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
3 changes: 2 additions & 1 deletion src/agentlab/llm/chat_api.py
Original file line number Diff line number Diff line change
@@ -406,13 +406,14 @@ class HuggingFaceURLChatModel(HFBaseChatModel):
def __init__(
self,
model_name: str,
base_model_name: str,
model_url: str,
token: Optional[str] = None,
temperature: Optional[int] = 1e-1,
max_new_tokens: Optional[int] = 512,
n_retry_server: Optional[int] = 4,
):
super().__init__(model_name, n_retry_server)
super().__init__(model_name, base_model_name, n_retry_server)
if temperature < 1e-3:
logging.warning("Models might behave weirdly when temperature is too low.")
self.temperature = temperature
9 changes: 6 additions & 3 deletions src/agentlab/llm/huggingface_utils.py
Original file line number Diff line number Diff line change
@@ -40,14 +40,17 @@ class HFBaseChatModel(AbstractChatModel):
description="The number of times to retry the server if it fails to respond",
)

def __init__(self, model_name, n_retry_server):
def __init__(self, model_name, base_model_name, n_retry_server):
super().__init__()
self.n_retry_server = n_retry_server

self.tokenizer = AutoTokenizer.from_pretrained(model_name)
if base_model_name is None:
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
else:
self.tokenizer = AutoTokenizer.from_pretrained(base_model_name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't get what's the point of that...

Copy link
Collaborator Author

@jardinetsouffleton jardinetsouffleton Dec 19, 2024

Choose a reason for hiding this comment

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

yes, sorry a little cryptic. I made this to support LoRA checkpoints, which are stored in the model_name directory. They are applied to a base model, stored at base_model_name directory. The directory where the adapters are stored contains only the adapters, while the model's safetensors + tokenizers, etc. are located in base_model_name

if isinstance(self.tokenizer, GPT2TokenizerFast):
logging.warning(
f"No chat template is defined for {model_name}. Resolving to the hard-coded templates."
f"No chat template is defined for {base_model_name}. Resolving to the hard-coded templates."
)
self.tokenizer = None
self.prompt_template = get_prompt_template(model_name)