Views
No views yet
TinyLlama/TinyLlama-1.1B-Chat-v1.0. The goal was to enhance the model's knowledge and question-answering capabilities specifically within the domain of motorcycle repair and maintenance, while leveraging the efficiency of the compact TinyLlama architecture.trl's SFTTrainer.TinyLlama/TinyLlama-1.1B-Chat-v1.0) and then apply this LoRA adapter on top. Ensure you have transformers, peft, accelerate, and bitsandbytes installed.1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, pipeline
3from peft import PeftModel
4import os
5
6# --- Configuration ---
7base_model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
8adapter_id = "cahlen/tinyllama-motorcycle-repair-qa-adapter" # This is the adapter you are using
9device_map = "auto"
10
11# --- Load Tokenizer ---
12tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True)
13if tokenizer.pad_token is None:
14 tokenizer.pad_token = tokenizer.eos_token
15tokenizer.padding_side = "left"
16
17# --- Configure Quantization ---
18use_4bit = True # Set to False if not using 4-bit
19compute_dtype = getattr(torch, "float16") # Default compute dtype
20quantization_config = None
21
22if use_4bit and torch.cuda.is_available():
23 print("Using 4-bit quantization")
24 bnb_config = BitsAndBytesConfig(
25 load_in_4bit=True,
26 bnb_4bit_quant_type="nf4",
27 bnb_4bit_compute_dtype=compute_dtype,
28 bnb_4bit_use_double_quant=False,
29 )
30 quantization_config = bnb_config
31else:
32 print("Not using 4-bit quantization or CUDA not available")
33 compute_dtype = torch.float32 # Use default float32 on CPU
34
35# --- Load Base Model ---
36print(f"Loading base model: {base_model_id}")
37base_model = AutoModelForCausalLM.from_pretrained(
38 base_model_id,
39 quantization_config=quantization_config,
40 device_map=device_map,
41 trust_remote_code=True,
42 torch_dtype=compute_dtype # Set appropriate dtype
43)
44base_model.config.use_cache = True
45
46# --- Load LoRA Adapter ---
47print(f"Loading LoRA adapter: {adapter_id}")
48model = PeftModel.from_pretrained(base_model, adapter_id)
49model.eval()
50print("Adapter loaded successfully.")
51
52# --- Prepare Prompt ---
53# Example prompt
54topic = "Brake System"
55question = "What are the signs of worn brake pads?"
56system_prompt = "You are a helpful assistant knowledgeable about motorcycle repair."
57
58user_query = f"Topic: {topic}\nQuestion: {question}"
59messages = [
60 {"role": "system", "content": system_prompt},
61 {"role": "user", "content": user_query},
62]
63formatted_prompt = tokenizer.apply_chat_template(
64 messages,
65 tokenize=False,
66 add_generation_prompt=True
67)
68print(f"--- Prompt ---\n{formatted_prompt}")
69
70# --- Generate Response ---
71print("Generating...")
72pipe = pipeline(
73 task="text-generation",
74 model=model,
75 tokenizer=tokenizer,
76 max_new_tokens=100,
77 do_sample=True,
78 temperature=0.7,
79 top_p=0.9,
80 pad_token_id=tokenizer.eos_token_id,
81 eos_token_id=tokenizer.eos_token_id
82)
83result = pipe(formatted_prompt)
84
85# --- Print Response ---
86print("\n--- Output ---")
87print(result[0]['generated_text'])
88
89# Extract only the assistant's response
90assistant_response = result[0]['generated_text'][len(formatted_prompt):].strip()
91print("\n--- Assistant Only ---")
92print(assistant_response)
93TinyLlama/TinyLlama-1.1B-Chat-v1.0cahlen/cdg-motorcycle-repair-qa-data-85x10 (880 examples)trl.SFTTrainer.float16 compute dtype.r=64, lora_alpha=16, lora_dropout=0.1, target modules: ["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"].<|system|>
You are a helpful assistant knowledgeable about motorcycle repair.</s>
<|user|>
Topic: Tire Maintenance
Question: What is the first step in checking tire pressure?</s>
<|assistant|>
The first step is to check the tire pressure, then check the tire pressure gauge, and finally check the tire tread depth.<|system|>
You are a helpful assistant knowledgeable about motorcycle repair.</s>
<|user|>
Topic: Oil Maintenance
Question: How often should I change my motorcycle oil?</s>
<|assistant|>
It is recommended to change your motorcycle oil every 5,000 to 10,000 miles, or as recommended by the manufacturer.<|system|>
You are a helpful assistant knowledgeable about motorcycle repair.</s>
<|user|>
Topic: Brake System
Question: What are the signs of worn brake pads?</s>
<|assistant|>
Worn brake pads can be felt in the brake pedal, resulting in a rough or jerky braking action, or a noticeable decrease in braking performance.<|system|>
You are a helpful assistant knowledgeable about motorcycle repair.</s>
<|user|>
Topic: Geography
Question: What is the capital of France?</s>
<|assistant|>
The capital of France is Paris.TinyLlama model and the synthetically generated dataset.1@misc{cahlen_tinyllama_motorcycle_repair_qa_adapter,
2 author = {Cahlen},
3 title = {LoRA Adapter for TinyLlama-1.1B-Chat specialized on Motorcycle Repair QA},
4 year = {2024},
5 publisher = {Hugging Face},
6 journal = {Hugging Face Hub},
7 howpublished = {\url{https://huggingface.co/cahlen/tinyllama-motorcycle-repair-qa-adapter}}
8}
9
10@misc{zhang2024tinyllama,
11 title={TinyLlama: An Open-Source Small Language Model},
12 author={Peiyuan Zhang and Guangxuan Xiao and Ning Tuan Anh Tran and Xin (Notus) Li and Hao Tan and Yaowen Zhang and Philipp F. Hoefer and Hong Mo Kuan and Benn Tan and Ponnuchamy Muthu Ilakkuvan and Associated Professor Nan Yang and Dr. Si-Qing Qin and Dr. Bin Lin and Dr. Zhengin Li and Dr. Ramesha Karunasena and Dr. Ajay Kumar Jha and Mohamed Ahmed Hassan and ARIES AI},
13 year={2024},
14 eprint={2401.02385},
15 archivePrefix={arXiv},
16 primaryClass={cs.CL}
17}