Fine-tune on your GPU
Langtune is an open-source (MIT) Python library for fine-tuning open text models such as Llama, Mistral, Qwen and Phi on your own hardware.
- Runs locally
- 8 methods
- MIT
Install
Install Langtune and the Hugging Face training libraries it runs on, on a machine with an NVIDIA GPU.
python
1pip install langtune transformers datasets accelerate bitsandbytes peft trl
Load a model and add an adapter
Models load in 4-bit by default, with FlashAttention 2 and gradient checkpointing on, to keep memory use down. get_peft_model adds a small trainable adapter; the base model stays frozen.
python
1from langtune import FastLanguageModel23model, tokenizer = FastLanguageModel.from_pretrained(4 "meta-llama/Llama-3.1-8B",5 load_in_4bit=True,6 max_seq_length=2048,7)89model = FastLanguageModel.get_peft_model(model, r=16, lora_alpha=32, method="qlora")
Train
Load your data as a Hugging Face dataset, then train. Chat-format JSONL (a messages array per line) works as is. The adapter is written to output_dir.
python
1from datasets import load_dataset23dataset = load_dataset("json", data_files="train.jsonl", split="train")45FastLanguageModel.train(6 model, tokenizer, dataset,7 output_dir="./output",8 num_train_epochs=3,9)
Methods
Pass method= to choose how the model learns:
- lora - a small adapter on a frozen model
- qlora - LoRA on a 4-bit model (the default)
- dora - weight-decomposed LoRA
- ia3 - even fewer trainable parameters
- prefix - learned prompt vectors
- dpo, orpo, kto - learn from preferred answers or thumbs up / down
Save full weights
Merge the adapter into the base model to get standard weights that llama.cpp, vLLM and other tools can run. See Run your model anywhere.
python
1FastLanguageModel.save_pretrained(model, tokenizer, "./my-assistant", merge_before_save=True)