Views
No views yet
microsoft/Phi-4-mini-instructadapter_config.json + adapter_model.safetensors (the adapter)swapmt_cfg.json and swapmt_metrics.json1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
3from peft import PeftModel
4
5base_id = "microsoft/Phi-4-mini-instruct"
6adapter_id = "Kiffaz11/SWaP-MT-Phi-4-mini-instruct-ENVI-GRPO-adapter"
7
8tok = AutoTokenizer.from_pretrained(adapter_id, trust_remote_code=True) # use tokenizer shipped with adapter
9if tok.pad_token_id is None:
10 tok.pad_token = tok.unk_token if tok.unk_token_id is not None else tok.eos_token
11
12bnb = BitsAndBytesConfig(
13 load_in_4bit=True,
14 bnb_4bit_quant_type="nf4",
15 bnb_4bit_use_double_quant=True,
16 bnb_4bit_compute_dtype=torch.float16,
17)
18
19base = AutoModelForCausalLM.from_pretrained(
20 base_id,
21 trust_remote_code=True,
22 device_map="auto",
23 quantization_config=bnb,
24 torch_dtype=torch.float16,
25 attn_implementation="sdpa",
26)
27
28model = PeftModel.from_pretrained(base, adapter_id)
29model.eval()
30
31SYSTEM_PROMPT = "You are a high-quality translation system. Translate English to Vietnamese. Output ONLY the Vietnamese translation."
32def make_prompt(en):
33 msgs = [
34 {"role":"system","content":SYSTEM_PROMPT},
35 {"role":"user","content":f"English:\n{en}\n\nVietnamese:"},
36 ]
37 return tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
38
39prompt = make_prompt("This is a test sentence.")
40inputs = tok([prompt], return_tensors="pt").to(model.device)
41
42with torch.inference_mode():
43 out = model.generate(**inputs, max_new_tokens=96, num_beams=4)
44
45print(tok.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True).split("\n")[0].strip())