-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.py
More file actions
296 lines (247 loc) · 9.75 KB
/
Copy pathadapter.py
File metadata and controls
296 lines (247 loc) · 9.75 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import argparse
import gc
import importlib.util
from pathlib import Path
from typing import List, Optional, Tuple
ROOT_DIR = Path(__file__).resolve().parent
DEFAULT_MODEL_PATH = ROOT_DIR / "models" / "Qwen3-0.6B"
DEFAULT_ADAPTER_PATH = ROOT_DIR / "outputs" / "qwen3_0.6b_medquad_lora_v2_seq768"
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Load Qwen3-0.6B with an optional LoRA adapter for generation.")
parser.add_argument("--model-path", type=Path, default=DEFAULT_MODEL_PATH, help="Local base model path.")
parser.add_argument("--adapter-path", type=Path, default=DEFAULT_ADAPTER_PATH, help="Adapter directory.")
parser.add_argument("--prompt", type=str, default="", help="Single prompt to run.")
parser.add_argument("--system-prompt", type=str, default="", help="Optional system prompt.")
parser.add_argument("--interactive", action="store_true", help="Start an interactive chat loop.")
parser.add_argument("--max-new-tokens", type=int, default=256, help="Maximum generated tokens.")
parser.add_argument("--do-sample", action="store_true", help="Enable sampling instead of greedy decoding.")
parser.add_argument("--temperature", type=float, default=0.7, help="Sampling temperature.")
parser.add_argument("--top-p", type=float, default=0.9, help="Top-p sampling parameter.")
parser.add_argument("--trust-remote-code", action="store_true", help="Pass trust_remote_code=True.")
parser.add_argument("--load-in-4bit", action="store_true", help="Load the base model in 4-bit mode.")
parser.add_argument(
"--merge-output-dir",
type=Path,
default=None,
help="If provided, merge the adapter into the base model and save the merged model here.",
)
parser.add_argument(
"--base-only",
action="store_true",
help="Ignore the adapter and run the base model only.",
)
return parser.parse_args()
def require_dependencies(use_adapter: bool) -> None:
required = ["torch", "transformers"]
if use_adapter:
required.append("peft")
missing = [name for name in required if importlib.util.find_spec(name) is None]
if missing:
raise ImportError(
f"Missing required packages: {', '.join(missing)}. "
"Install them first, for example: pip3 install --user torch transformers peft bitsandbytes"
)
def resolve_precision_flags(torch_module) -> dict:
if torch_module.cuda.is_available():
if torch_module.cuda.is_bf16_supported():
return {"bf16": True, "fp16": False}
return {"bf16": False, "fp16": True}
return {"bf16": False, "fp16": False}
def build_model_dtype(torch_module, precision_flags: dict):
if precision_flags["bf16"]:
return torch_module.bfloat16
if precision_flags["fp16"]:
return torch_module.float16
return torch_module.float32
def get_tokenizer_source(model_path: Path, adapter_path: Optional[Path]) -> Path:
if adapter_path is not None and (adapter_path / "tokenizer.json").exists():
return adapter_path
return model_path
def load_model_and_tokenizer(
model_path: Path,
adapter_path: Optional[Path] = None,
trust_remote_code: bool = False,
load_in_4bit: bool = False,
) -> Tuple[object, object]:
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
tokenizer = AutoTokenizer.from_pretrained(
get_tokenizer_source(model_path, adapter_path),
trust_remote_code=trust_remote_code,
use_fast=True,
)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "left"
precision_flags = resolve_precision_flags(torch)
quantization_config = None
if load_in_4bit:
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=build_model_dtype(torch, precision_flags),
)
model = AutoModelForCausalLM.from_pretrained(
model_path,
dtype=build_model_dtype(torch, precision_flags),
quantization_config=quantization_config,
device_map="auto" if torch.cuda.is_available() else None,
trust_remote_code=trust_remote_code,
)
if adapter_path is not None:
from peft import PeftModel
model = PeftModel.from_pretrained(model, adapter_path)
model.eval()
return model, tokenizer
def model_input_device(model) -> object:
return next(model.parameters()).device
def render_messages(tokenizer, messages: List[dict], add_generation_prompt: bool = True) -> str:
return tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=add_generation_prompt)
def build_prompt(tokenizer, prompt: str, system_prompt: str = "") -> str:
messages = []
if system_prompt:
messages.append({"role": "system", "content": system_prompt})
messages.append({"role": "user", "content": prompt})
return render_messages(tokenizer, messages, add_generation_prompt=True)
def generate_from_messages(
model,
tokenizer,
messages: List[dict],
max_new_tokens: int = 256,
do_sample: bool = False,
temperature: float = 0.7,
top_p: float = 0.9,
) -> str:
import torch
rendered = render_messages(tokenizer, messages, add_generation_prompt=True)
inputs = tokenizer(rendered, add_special_tokens=False, return_tensors="pt")
device = model_input_device(model)
inputs = {key: value.to(device) for key, value in inputs.items()}
generation_kwargs = {
"max_new_tokens": max_new_tokens,
"do_sample": do_sample,
"pad_token_id": tokenizer.pad_token_id,
"eos_token_id": tokenizer.eos_token_id,
}
if do_sample:
generation_kwargs["temperature"] = temperature
generation_kwargs["top_p"] = top_p
with torch.inference_mode():
outputs = model.generate(**inputs, **generation_kwargs)
prompt_length = inputs["input_ids"].shape[1]
generated_ids = outputs[0, prompt_length:]
return tokenizer.decode(generated_ids, skip_special_tokens=True).strip()
def generate_answer(
model,
tokenizer,
prompt: str,
system_prompt: str = "",
max_new_tokens: int = 256,
do_sample: bool = False,
temperature: float = 0.7,
top_p: float = 0.9,
) -> str:
messages = []
if system_prompt:
messages.append({"role": "system", "content": system_prompt})
messages.append({"role": "user", "content": prompt})
return generate_from_messages(
model=model,
tokenizer=tokenizer,
messages=messages,
max_new_tokens=max_new_tokens,
do_sample=do_sample,
temperature=temperature,
top_p=top_p,
)
def unload_model(model) -> None:
del model
gc.collect()
try:
import torch
if torch.cuda.is_available():
torch.cuda.empty_cache()
except Exception:
pass
def merge_adapter(
model_path: Path,
adapter_path: Path,
output_dir: Path,
trust_remote_code: bool = False,
) -> None:
if not adapter_path.exists():
raise FileNotFoundError(f"Adapter path does not exist: {adapter_path}")
model, tokenizer = load_model_and_tokenizer(
model_path=model_path,
adapter_path=adapter_path,
trust_remote_code=trust_remote_code,
load_in_4bit=False,
)
merged_model = model.merge_and_unload()
output_dir.mkdir(parents=True, exist_ok=True)
merged_model.save_pretrained(output_dir)
tokenizer.save_pretrained(output_dir)
unload_model(merged_model)
def interactive_loop(model, tokenizer, args: argparse.Namespace) -> None:
while True:
try:
prompt = input("\nUser> ").strip()
except EOFError:
break
if not prompt:
continue
if prompt.lower() in {"exit", "quit"}:
break
answer = generate_answer(
model=model,
tokenizer=tokenizer,
prompt=prompt,
system_prompt=args.system_prompt,
max_new_tokens=args.max_new_tokens,
do_sample=args.do_sample,
temperature=args.temperature,
top_p=args.top_p,
)
print(f"\nAssistant> {answer}")
def main() -> None:
args = parse_args()
adapter_path = None if args.base_only else args.adapter_path
require_dependencies(use_adapter=adapter_path is not None)
if args.merge_output_dir is not None:
if args.base_only:
raise ValueError("--merge-output-dir requires an adapter; remove --base-only.")
if args.load_in_4bit:
raise ValueError("Merging is only supported without --load-in-4bit.")
merge_adapter(
model_path=args.model_path,
adapter_path=args.adapter_path,
output_dir=args.merge_output_dir,
trust_remote_code=args.trust_remote_code,
)
print(f"Merged adapter saved to: {args.merge_output_dir}")
return
model, tokenizer = load_model_and_tokenizer(
model_path=args.model_path,
adapter_path=adapter_path,
trust_remote_code=args.trust_remote_code,
load_in_4bit=args.load_in_4bit,
)
if args.prompt:
answer = generate_answer(
model=model,
tokenizer=tokenizer,
prompt=args.prompt,
system_prompt=args.system_prompt,
max_new_tokens=args.max_new_tokens,
do_sample=args.do_sample,
temperature=args.temperature,
top_p=args.top_p,
)
print(answer)
elif args.interactive:
interactive_loop(model, tokenizer, args)
else:
raise ValueError("Provide --prompt, --interactive, or --merge-output-dir.")
if __name__ == "__main__":
main()