Skip to content

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.py creates one job running axolotl train in docker.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-axolotl at /workspace/data, where Axolotl reads config.yaml: Qwen/Qwen2.5-0.5B, QLoRA, Salesforce/wikitext.
  • Training writes to /workspace/output on the container's local disk. When it succeeds, the job copies that folder to /workspace/data/output/run-<date>-<time>/, which is s3://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-axolotl that 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:

nebius ai job logs <job-id>

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 with ns.Job(job.spec).create(); training starts over. See Spot preemption.
  • Train several configs at once: see the parallel sweep.

Troubleshooting

  • Writes to debug.log fail on the bucket. Axolotl appends to debug.log in its output_dir, and the S3 mount does not support that. Keep output_dir: /workspace/output on the local disk, as config.yaml does, and copy the result after training.
  • The job ends in ERROR. The final wait() line and display() show the reason in status.state_details, for example NotEnoughResources or Preempted. Try again, or switch to on-demand. After Preempted, create the job again with ns.Job(job.spec).create().
  • CapacityError on 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