Views
No views yet
1# Install llama-cpp-python
2# pip install llama-cpp-python
3
4from llama_cpp import Llama
5
6# Load the model
7model_path = "arif-butt/tinyllama-unsloth-gguf" # or local path to .gguf file
8llm = Llama(
9 model_path=model_path,
10 n_ctx=2048, # Context length
11 n_threads=4, # Number of CPU threads
12 n_gpu_layers=0, # Set >0 for GPU offloading
13 verbose=False,
14)
15
16# Simple prompt
17prompt = "Q: Name all the courses Arif butt teach?\nA:"
18
19# Generate response
20output = llm(
21 prompt,
22 max_tokens=100, # Maximum tokens to generate
23 temperature=0.2, # Lower = more deterministic
24 top_p=0.95, # Nucleus sampling
25 repeat_penalty=1.1, # Penalize repetition
26 stop=["Q:", "\nQ:"], # Stop sequences
27)
28
29print(output["choices"][0]["text"])
30Option 2: Using Transformers with llama-cpp
31from transformers import AutoTokenizer
32from llama_cpp import Llama
33
34# Load tokenizer
35tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
36
37# Load GGUF model
38model_path = "arif-butt/tinyllama-unsloth-gguf"
39llm = Llama(model_path=model_path, n_ctx=2048)
40
41def generate_response(prompt, max_tokens=100, temperature=0.7):
42 """Generate response using the GGUF model"""
43 output = llm(
44 prompt,
45 max_tokens=max_tokens,
46 temperature=temperature,
47 stop=["Q:", "\nQ:", "User:", "\nUser:"],
48 )
49 return output["choices"][0]["text"]
50
51# Test
52prompt = "Q: What is machine learning?\nA:"
53response = generate_response(prompt)
54print(f"Response: {response}")
55
56Option 3: Using llama.cpp CLI
57# Download the model
58wget https://huggingface.co/arif-butt/tinyllama-unsloth-gguf/resolve/main/tinyllama-unsloth-q4_k_m.gguf
59
60# Run inference
61./main -m tinyllama-unsloth-q4_k_m.gguf \
62 -p "Q: Name all the courses Arif butt teach?\nA:" \
63 -n 100 \
64 -t 4 \
65 --temp 0.2 \
66 --top_p 0.95
67LORA_R = 16 # Rank of LoRA matrices
68LORA_ALPHA = 32 # Scaling factor (alpha/r = 2.0)
69LORA_DROPOUT = 0.05 # Dropout for regularization
70TARGET_MODULES = [ # Layers where LoRA is applied
71 "q_proj", # Query projection
72 "k_proj", # Key projection
73 "v_proj", # Value projection
74 "o_proj", # Output projection
75 "gate_proj", # Gate projection (MLP)
76 "up_proj", # Up projection (MLP)
77 "down_proj" # Down projection (MLP)
78]