Views
No views yet
| Model | ROUGE-L | BERTScore F1 |
|---|---|---|
| Mistral 7B fine-tuned (this model) | 0.2033 | 0.7739 |
| Llama 3.3 70B via Groq | 0.1715 | 0.7594 |
| Mistral 7B base (no fine-tuning) | 0.1102 | 0.7118 |
1from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
2from peft import PeftModel
3import torch
4
5BASE_MODEL = "mistralai/Mistral-7B-v0.1"
6
7# Load in 4-bit for efficient inference
8bnb_config = BitsAndBytesConfig(
9 load_in_4bit=True,
10 bnb_4bit_quant_type="nf4",
11 bnb_4bit_compute_dtype=torch.float16,
12)
13
14tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
15base_model = AutoModelForCausalLM.from_pretrained(
16 BASE_MODEL,
17 quantization_config=bnb_config,
18 device_map="auto",
19)
20model = PeftModel.from_pretrained(base_model, "kk014/mistral-7b-docstring")
21model.eval()
22
23# Generate a docstring
24function_code = """
25def calculate_bmi(weight_kg, height_m):
26 return weight_kg / (height_m ** 2)
27""".strip()
28
29prompt = (
30 "You are a Python documentation expert. "
31 "Write a clear, concise NumPy-style docstring for the following Python function.\n\n"
32 f"### Function:\n{function_code}\n\n"
33 "### Docstring:"
34)
35
36inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
37with torch.no_grad():
38 outputs = model.generate(
39 **inputs,
40 max_new_tokens=150,
41 temperature=0.1,
42 do_sample=True,
43 pad_token_id=tokenizer.eos_token_id,
44 )
45
46generated = tokenizer.decode(outputs[0], skip_special_tokens=True)
47docstring = generated[len(prompt):].strip()
48print(docstring)| Parameter | Value |
|---|---|
| Base model | mistralai/Mistral-7B-v0.1 |
| Dataset | CodeSearchNet (Python split) |
| Training samples | 8,000 |
| Method | QLoRA (4-bit NF4 quantisation) |
| LoRA rank | 16 |
| LoRA alpha | 32 |
| Epochs | 1 |
| Batch size | 2 (effective 16 with grad accum) |
| Learning rate | 2e-4 |
| Hardware | Kaggle T4 x2 (free tier) |
| Training time | ~4 hours |
| Framework | HuggingFace PEFT + TRL |
@article{dettmers2023qlora,
title={QLoRA: Efficient Finetuning of Quantized LLMs},
author={Dettmers, Tim and others},
journal={arXiv preprint arXiv:2305.14314},
year={2023}
}