Fine-tune a model with Axolotl¶
Fine-tunes Qwen/Qwen2.5-0.5B with QLoRA on one spot H100, then copies the checkpoints and adapter to Object Storage. It is the Python version of the Serverless AI Cookbook Axolotl recipe.
View example files ↗ · Related reference: Jobs →
What this example does¶
finetune.pycreates one job runningaxolotl trainindocker.io/axolotlai/axolotl:main-20260309-py3.11-cu128-2.9.1, waits for it, and prints the final record.- The job mounts the bucket
fine-tuning-axolotlat/workspace/data, where Axolotl readsconfig.yaml:Qwen/Qwen2.5-0.5B, QLoRA,Salesforce/wikitext. - Training writes to
/workspace/outputon the container's local disk. When it succeeds, the job copies that folder to/workspace/data/output/run-<date>-<time>/, which iss3://fine-tuning-axolotl/output/run-<date>-<time>/.
Requirements¶
- A Nebius profile that can create jobs: see Install and authenticate.
- Spot H100 quota in the project's region.
- An Object Storage bucket named
fine-tuning-axolotlthat the job can mount read-write, and the AWS CLI with credentials for it.
Runtime and compute¶
| Machine | gpu="h100": one H100, pricing="spot" |
| Disk | 500Gi |
| Timeout | 24h |
| Shared memory | 16Gi, the GPU default |
Run it¶
Upload the config, then create the job from the repository root:
aws s3 cp examples/training/axolotl-finetune/config.yaml \
s3://fine-tuning-axolotl/config.yaml \
--endpoint-url https://storage.eu-north1.nebius.cloud
uv run python examples/training/axolotl-finetune/finetune.py
Read the container logs while it runs or after it ends:
Expected output¶
create() prints the job id. wait() prints each new state until the job ends, and display() prints the final record: machine, disk, timings, and the state. On COMPLETED the adapter and checkpoints are in s3://fine-tuning-axolotl/output/run-<date>-<time>/.
How to adapt it¶
- Change the model, dataset, or LoRA settings in
config.yaml, then upload it again. - Ask for more GPUs with
gpu="h100:8", and match Axolotl's settings to them. - Raise shared memory with
shm="64Gi". - Pass
pricing="on-demand"for a machine that is not preempted. - A preempted spot job ends in
ERROR. Create it again withns.Job(job.spec).create(); training starts over. See Spot preemption. - Train several configs at once: see the parallel sweep.
Troubleshooting¶
- Writes to
debug.logfail on the bucket. Axolotl appends todebug.login itsoutput_dir, and the S3 mount does not support that. Keepoutput_dir: /workspace/outputon the local disk, asconfig.yamldoes, and copy the result after training. - The job ends in
ERROR. The finalwait()line anddisplay()show the reason instatus.state_details, for exampleNotEnoughResourcesorPreempted. Try again, or switch to on-demand. AfterPreempted, create the job again withns.Job(job.spec).create(). CapacityErroron create. The project's quota is too small. The message names the quota, the amount requested, and the limit.
Source¶
finetune.py
"""Fine-tune Qwen/Qwen2.5-0.5B with Axolotl on Nebius Serverless AI Jobs.
Rebuilds the Nebius Serverless AI Cookbook Axolotl recipe in Python
(training/axolotl-finetuning/README.md). Upload ``config.yaml`` to
``s3://fine-tuning-axolotl/config.yaml`` before running. Uses the active
Nebius profile.
"""
import nebius_serverless as ns
def main() -> None:
"""Create the Axolotl fine-tuning job, wait, and print the record."""
job = ns.Job(
command=[
"bash",
"-lc",
"RUN_ID=run-$(date +%Y%m%d-%H%M%S)"
" && axolotl train /workspace/data/config.yaml"
" && mkdir -p /workspace/data/output/$RUN_ID"
" && cp -r /workspace/output/. /workspace/data/output/$RUN_ID",
],
image="docker.io/axolotlai/axolotl:main-20260309-py3.11-cu128-2.9.1",
gpu="h100",
pricing="spot",
disk="500Gi",
timeout="24h",
volumes=["s3://fine-tuning-axolotl:/workspace/data:rw"],
).create()
job.wait()
job.display()
if __name__ == "__main__":
main()
config.yaml
base_model: Qwen/Qwen2.5-0.5B
load_in_4bit: true
adapter: qlora
datasets:
- path: Salesforce/wikitext
name: wikitext-2-raw-v1
split: "train[:2000]"
type: completion
field: text
sequence_len: 128
micro_batch_size: 1
gradient_accumulation_steps: 1
learning_rate: 2e-4
max_steps: 30
val_set_size: 0
logging_steps: 5
# Keep training output local, then copy to bucket after training.
output_dir: /workspace/output
lora_r: 8
lora_alpha: 16
lora_dropout: 0.05
lora_target_modules:
- q_proj
- k_proj
- v_proj
- o_proj
- gate_proj
- up_proj
- down_proj