-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinference.py
More file actions
185 lines (157 loc) · 5.69 KB
/
inference.py
File metadata and controls
185 lines (157 loc) · 5.69 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""
Inference script for the fine-tuned DeepSeek model.
"""
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
from typing import Optional
def load_model(
base_model: str = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B",
lora_path: Optional[str] = None,
merged_path: Optional[str] = None,
device: str = "auto",
load_in_4bit: bool = True,
):
"""
Load the fine-tuned model for inference.
Args:
base_model: Base model name
lora_path: Path to LoRA adapter (if using adapters)
merged_path: Path to merged model (if pre-merged)
device: Device to load on
load_in_4bit: Whether to quantize
"""
if merged_path:
# Load pre-merged model
print(f"Loading merged model from {merged_path}")
model = AutoModelForCausalLM.from_pretrained(
merged_path,
torch_dtype=torch.float16,
device_map=device,
trust_remote_code=True,
)
tokenizer = AutoTokenizer.from_pretrained(merged_path, trust_remote_code=True)
elif lora_path:
# Load base + LoRA adapter
print(f"Loading base model: {base_model}")
from transformers import BitsAndBytesConfig
if load_in_4bit:
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16,
)
model = AutoModelForCausalLM.from_pretrained(
base_model,
quantization_config=bnb_config,
device_map=device,
trust_remote_code=True,
)
else:
model = AutoModelForCausalLM.from_pretrained(
base_model,
torch_dtype=torch.float16,
device_map=device,
trust_remote_code=True,
)
print(f"Loading LoRA adapter from {lora_path}")
model = PeftModel.from_pretrained(model, lora_path)
tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)
else:
# Just load base model
print(f"Loading base model: {base_model}")
model = AutoModelForCausalLM.from_pretrained(
base_model,
torch_dtype=torch.float16,
device_map=device,
trust_remote_code=True,
)
tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)
return model, tokenizer
def generate_response(
model,
tokenizer,
prompt: str,
system_prompt: Optional[str] = None,
max_new_tokens: int = 512,
temperature: float = 0.7,
top_p: float = 0.9,
) -> str:
"""
Generate a response from the model.
"""
# Format prompt in DeepSeek's ChatML style
if system_prompt:
formatted = f"<|System|>{system_prompt}\n<|User|>{prompt}\n<|Assistant|>"
else:
formatted = f"<|User|>{prompt}\n<|Assistant|>"
inputs = tokenizer(formatted, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
temperature=temperature,
top_p=top_p,
do_sample=True,
pad_token_id=tokenizer.eos_token_id,
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Extract just the assistant response
if "<|Assistant|>" in response:
response = response.split("<|Assistant|>")[-1].strip()
return response
def interactive_chat(model, tokenizer, system_prompt: Optional[str] = None):
"""
Run an interactive chat session.
"""
print("\n" + "=" * 50)
print("Interactive Chat with Fine-tuned DeepSeek")
print("Type 'quit' or 'exit' to end the session")
print("=" * 50 + "\n")
while True:
try:
user_input = input("You: ").strip()
if user_input.lower() in ['quit', 'exit', 'q']:
print("Goodbye!")
break
if not user_input:
continue
response = generate_response(
model,
tokenizer,
user_input,
system_prompt=system_prompt,
)
print(f"\nAssistant: {response}\n")
except KeyboardInterrupt:
print("\nGoodbye!")
break
def main():
import argparse
parser = argparse.ArgumentParser(description="Run inference with fine-tuned DeepSeek")
parser.add_argument("--base-model", type=str, default="deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B")
parser.add_argument("--lora-path", type=str, default=None, help="Path to LoRA adapter")
parser.add_argument("--merged-path", type=str, default=None, help="Path to merged model")
parser.add_argument("--prompt", type=str, default=None, help="Single prompt to run")
parser.add_argument("--system-prompt", type=str, default=None, help="System prompt")
parser.add_argument("--interactive", action="store_true", help="Run interactive chat")
parser.add_argument("--no-4bit", action="store_true", help="Disable 4-bit quantization")
args = parser.parse_args()
model, tokenizer = load_model(
base_model=args.base_model,
lora_path=args.lora_path,
merged_path=args.merged_path,
load_in_4bit=not args.no_4bit,
)
if args.interactive or args.prompt is None:
interactive_chat(model, tokenizer, system_prompt=args.system_prompt)
else:
response = generate_response(
model,
tokenizer,
args.prompt,
system_prompt=args.system_prompt,
)
print(f"Response: {response}")
if __name__ == "__main__":
main()