-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_finetuned_model.py
52 lines (40 loc) · 1.52 KB
/
load_finetuned_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import json
import torch
from transformers import GPT2Tokenizer
from safetensors.torch import load_file
from finetune_gpt2 import GPT, GPTConfig
def load_model_and_tokenizer(model_path: str, config_path: str):
"""
Load and return the tokenizer and model with weights.
Parameters:
- model_path (str): Path to the model weights.
- config_path (str): Path to the configuration file.
Returns:
- tokenizer (GPT2Tokenizer): The tokenizer instance.
- model (GPT): The model instance.
"""
# Select the device
device = "cpu"
if torch.cuda.is_available():
device = "cuda"
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
device = "mps"
print(f"Using device: {device}")
# Load the configuration
with open(config_path, "r", encoding='utf-8') as f:
config_data = json.load(f)
config = GPTConfig(**config_data)
model = GPT(config)
# Load the weights
state_dict = load_file(os.path.join(model_path, "model.safetensors"))
# Weight sharing
if 'lm_head.weight' in state_dict:
state_dict['transformer.wte.weight'] = state_dict['lm_head.weight']
# Move state_dict tensors to the correct device
state_dict = {k: v.to(device) for k, v in state_dict.items()}
model.load_state_dict(state_dict)
model.to(device) # Move the model to the correct device
# Initialize the tokenizer
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
return tokenizer, model, device, config