MCS12 commited on
Commit
2fbe6e1
·
verified ·
1 Parent(s): f068e5f

Upload handler.py

Browse files
Files changed (1) hide show
  1. handler.py +20 -14
handler.py CHANGED
@@ -12,24 +12,30 @@ from transformers import (
12
  class EndpointHandler:
13
  def __init__(self, path=""):
14
 
15
- # Load base model
16
- base_model = AutoModelForCausalLM.from_pretrained(
17
- path, # Replace with actual base model
18
- quantization_config=BitsAndBytesConfig(load_in_4bit=True),
19
- device_map="auto"
 
 
 
20
  )
21
 
22
- # Configure LoRA with correct rank
23
- lora_config = LoraConfig(
24
- r=16, # This should match your checkpoint
25
- lora_alpha=32,
26
- target_modules=["q_proj", "v_proj"], # Adjust based on your model
27
- lora_dropout=0.05,
28
  )
29
 
30
- # Apply LoRA and load weights
31
- self.model = get_peft_model(base_model, lora_config)
32
- self.model.load_state_dict(torch.load(f"{path}/adapter_model.bin"))
 
 
 
33
 
34
  self.tokenizer = AutoTokenizer.from_pretrained(
35
  path,
 
12
  class EndpointHandler:
13
  def __init__(self, path=""):
14
 
15
+ peft_config = PeftConfig.from_pretrained(path)
16
+
17
+ # Load the base model with quantization
18
+ bnb_config = BitsAndBytesConfig(
19
+ load_in_4bit=True,
20
+ bnb_4bit_quant_type="nf4",
21
+ bnb_4bit_compute_dtype=torch.float16,
22
+ bnb_4bit_use_double_quant=True,
23
  )
24
 
25
+ base_model = AutoModelForCausalLM.from_pretrained(
26
+ peft_config.base_model_name_or_path,
27
+ quantization_config=bnb_config,
28
+ device_map="auto",
29
+ trust_remote_code=True,
30
+ torch_dtype=torch.float16,
31
  )
32
 
33
+ # Load the LoRA adapter with the correct configuration
34
+ self.model = PeftModel.from_pretrained(
35
+ base_model,
36
+ path,
37
+ torch_dtype=torch.float16,
38
+ )
39
 
40
  self.tokenizer = AutoTokenizer.from_pretrained(
41
  path,