Files
ai_studio/Docs/TRAINING.md
T
2026-11-17 15:35:04 +00:00

145 lines
8.0 KiB
Markdown

# Fine-tune a model for use in AI Studio
AI Studio can download and run MLX checkpoints from Hugging Face and register
local MLX model folders. Training and conversion run locally using the MLX LM tools.
Use the repository's `Scripts/train-mlx.py` to validate a local chat dataset, train and evaluate an adapter, and fuse a new checkpoint from Terminal.
## Use the tools in AI Studio
1. Open **Settings → Model Library → MLX → MLX Tools…**. For an installed model, use **Clone & Train…** to preselect its weights.
2. Use **Install MLX Tools** once. It uses a native Apple Silicon Python 3.11 or newer to install MLX LM with its training dependencies into a dedicated AI Studio environment. If automatic selection picks the wrong interpreter, use **Choose Python…**.
3. Use **Clone Base…** to write a separate copy to a new folder and add it to the library. Training can also use the original checkpoint while writing adapter weights to a separate run folder.
4. Choose a dataset folder and a run folder. Use **Validate Dataset**, **Train Adapter**, and **Evaluate**. Use **Fuse and Register** to create and register the final checkpoint. The run folder contains the adapter at `adapter` and the fused output at `trained-model`. Pick a fresh run folder for a new experiment.
5. Read errors and training output in the workshop. **Stop** ends the current operation. If stopping a run leaves partial output in its folder, choose a new destination before retrying.
The workshop also has **Convert to MLX**. Choose a compatible original checkpoint folder, or enter a Hugging Face repo ID and optionally pin its revision. Choose a new destination. The 4-bit option reduces weight size. A successful result appears in the MLX library and can be used as a training base.
In the Ollama library, an installed model's **Tools → Clone…** creates a new tag on the configured server. **Tools → Convert to MLX…** opens the same conversion workflow using the original Hugging Face checkpoint. The MLX LM converter accepts compatible model checkpoints. A packaged Ollama GGUF blob requires a separate conversion approach.
## 1. Choose the base model
On an Apple Silicon Mac, choose a small, supported MLX text-generation model.
In **Settings → Model Library → MLX**, download it and choose **Reveal** to
find its folder. The downloaded files are a snapshot from a specific Hugging
Face repository revision. Record the repository, revision, model card license
and runtime format. A Hugging Face repository can contain MLX weights, PyTorch
weights or GGUF files. Pick a working MLX checkpoint for this workflow.
A derived model does not need a second copy of the base weights during LoRA
training. Keep the base folder intact and create a separate project folder for
your dataset, adapter and experimental outputs. If you get the checkpoint
outside the app, use the Hugging Face Hub CLI or its snapshot downloader with
an explicit revision. For a gated repository, request access and authenticate
using the Hub CLI before downloading. AI Studio's built-in catalog currently
downloads public MLX Community checkpoints.
To download a separate copy, run this from your training project after
installing the Hugging Face Hub CLI. Take the repository and full commit
from the model preview in AI Studio and choose a new local folder.
```sh
hf download OWNER/REPO --revision FULL_COMMIT_SHA --local-dir ./base-model
```
To copy an Ollama model tag on the server configured for the Ollama CLI, use
`ollama cp SOURCE:TAG MY-MODEL:TAG`. This copies the inference model under a
new name with the same weights. To fine-tune it, obtain the matching training
checkpoint from the publisher and record its revision.
## 2. Prepare your local dataset
Create a dataset folder with `train.jsonl`, `valid.jsonl`, and `test.jsonl`.
Each line is one JSON object. For a chat model, use examples of the questions
and answers you want the model to learn. Use one example per line.
```json
{"messages":[{"role":"user","content":"Review this change for missing validation."},{"role":"assistant","content":"Validate the input at the boundary and add a test for invalid data."}]}
```
Keep related examples together in a single split so near duplicates cannot
inflate your test results. Remove credentials and private data you do not
want reproduced. Start with a small, hand-reviewed dataset. To answer from
changing internal standards or documents, keep using the Pegasus knowledge
base. Use fine-tuning for response style, task behavior and conventions that
you can demonstrate with examples.
Hugging Face can also host datasets. MLX LM supports a Hub dataset as an
input, alongside local JSONL. For an internal dataset, start with local files
and explicitly review access and retention before uploading anything.
## 3. Train and evaluate an adapter
In a dedicated project folder, create a Python environment and install MLX LM.
Record the installed version with the experiment so you can reproduce it.
```sh
python3 -m venv .venv
source .venv/bin/activate
python -m pip install "mlx-lm[train]"
python -m pip show mlx-lm
```
Set `BASE` to the model folder from Reveal. The relative paths below are
inside the training project folder.
```sh
BASE="/absolute/path/to/downloaded-mlx-model"
mlx_lm.lora --model "$BASE" --train --data ./dataset \
--adapter-path ./adapter --iters 100 --batch-size 1
mlx_lm.lora --model "$BASE" --test --data ./dataset \
--adapter-path ./adapter
mlx_lm.generate --model "$BASE" --adapter-path ./adapter \
--prompt "Review this change for missing validation."
```
The repository runner offers the same local workflow with dataset validation
and checks for new output folders. Run these from your training project with
the MLX environment activated.
```sh
RUNNER="$HOME/Developer/Applications/AIStudio/Scripts/train-mlx.py"
python "$RUNNER" validate --data ./dataset
python "$RUNNER" train --base "$BASE" --data ./dataset --adapter ./adapter-smoke
python "$RUNNER" evaluate --base "$BASE" --data ./dataset --adapter ./adapter-smoke
python "$RUNNER" fuse --base "$BASE" --adapter ./adapter-smoke --out ./trained-model
```
The runner expects text chat examples and rejects the same prompt history
across splits. It starts with 50 iterations, batch size 1, and four adapted
layers. Use `--iters` and `--layers` to change the smoke run and choose a new
adapter folder for each experiment. Prompt masking trains against the final
assistant response. On a quantized base, MLX LM uses QLoRA. A smoke run checks
the pipeline. Use a held-out evaluation to judge whether to keep the adapter.
Compare base and adapter answers on the same held-out examples. Check
grounding, task accuracy, style, and regressions outside the target task.
Use a small first run to check memory and verify the checkpoint supports
LoRA. Adjust sequence length, batch size and iteration count based on the
measured run. Keep each adapter tied to the exact base weights it was trained
against.
## 4. Load the trained derivative
Fuse into a **new folder** and keep the original checkpoint available.
```sh
mlx_lm.fuse --model "$BASE" --adapter-path ./adapter --save-path ./trained-model
```
In **Settings → Model Library → MLX**, use **Register Model Folder…**, select
`trained-model`, and use **Load Model**. Verify a short reply before using the
derivative for real work. An Ollama tag is a packaged inference artifact.
For an Ollama deployment, train from a supported base checkpoint first, then
check whether the target Ollama model architecture supports the adapter or
whether a separate compatible export is needed.
Hugging Face publishing is optional. When sharing, check the original license
and organizational data rules. Document the base revision, dataset rights,
training configuration, evaluation and limitations in the model card. A Hub
repository can store the adapter or fused checkpoint separately from the base.
Keep it private unless you intend to publish it.
Sources: [MLX LM LoRA guide](https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/LORA.md) ·
[Hugging Face Hub download guide](https://huggingface.co/docs/huggingface_hub/guides/download) ·
[Ollama import guide](https://docs.ollama.com/import)