1# ── Load Merged Model with Transformers ───────────────────────────────────
2
3from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
4import torch
5
6# Model identifier
7MODEL_ID = "arif-butt/tinyllama-unsloth-merged"
8
9print("Loading model and tokenizer...")
10tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
11model = AutoModelForCausalLM.from_pretrained(
12 MODEL_ID,
13 torch_dtype=torch.float16,
14 device_map="auto",
15 trust_remote_code=True,
16)
17model.eval()
18print("✅ Model loaded successfully!")
19
20# Test prompt
21prompt = "Q: Name all the courses Arif butt teach?\nA:"
22
23# Tokenize
24inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
25
26# Generate
27with torch.no_grad():
28 outputs = model.generate(
29 **inputs,
30 max_new_tokens=100,
31 temperature=0.2,
32 do_sample=True,
33 pad_token_id=tokenizer.eos_token_id,
34 )
35
36# Decode
37response = tokenizer.decode(outputs[0], skip_special_tokens=True)
38print(f"Prompt: {prompt}")
39print(f"Response: {response}")
40Option 2: Using Pipeline
41# ── Text Generation Pipeline ─────────────────────────────────────────────
42
43from transformers import pipeline
44import torch
45
46MODEL_ID = "arif-butt/tinyllama-unsloth-merged"
47
48pipe = pipeline(
49 "text-generation",
50 model=MODEL_ID,
51 torch_dtype=torch.float16,
52 device_map="auto",
53)
54
55prompt = "Q: What is machine learning?\nA:"
56output = pipe(prompt, max_new_tokens=100, temperature=0.2)
57print(output[0]["generated_text"])
58Option 3: Using Unsloth (Faster Inference)
59# ── Load with Unsloth for Maximum Performance ────────────────────────────
60
61from unsloth import FastLanguageModel
62import torch
63
64MODEL_ID = "arif-butt/tinyllama-unsloth-merged"
65
66print("Loading model with Unsloth...")
67model, tokenizer = FastLanguageModel.from_pretrained(
68 model_name=MODEL_ID,
69 max_seq_length=2048,
70 dtype=torch.float16,
71 device_map="auto",
72)
73print("✅ Model loaded with Unsloth optimizations!")
74
75# Test prompt
76prompt = "Q: Explain neural networks\nA:"
77inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
78
79outputs = model.generate(
80 **inputs,
81 max_new_tokens=150,
82 temperature=0.3,
83 do_sample=True,
84)
85
86response = tokenizer.decode(outputs[0], skip_special_tokens=True)
87print(f"Response: {response}")
88
89Fine-tuning Configuration
90LORA_R = 16 # Rank of LoRA matrices
91LORA_ALPHA = 32 # Scaling factor (alpha/r = 2.0)
92LORA_DROPOUT = 0.05 # Dropout for regularization
93TARGET_MODULES = [ # Layers where LoRA is applied
94 "q_proj", # Query projection
95 "k_proj", # Key projection
96 "v_proj", # Value projection
97 "o_proj", # Output projection
98 "gate_proj", # Gate projection (MLP)
99 "up_proj", # Up projection (MLP)
100 "down_proj" # Down projection (MLP)
101]