Axolotl のデバッグ方法
Axolotl の LLM 微調整プロセスにおけるデバッグの難しさを解消するため、開発環境の簡素化やキャッシュ管理などの具体的な実践的テクニックと VSCode 設定例を提示した記事。
キーポイント
テストシナリオの極限まで単純化
デバッグ時は並列処理を 1 に制限し、単一 GPU を使用、データセットとモデルサイズを最小限に抑えることで問題箇所を特定しやすくする。
キャッシュの管理とクリア戦略
Axolotl や HuggingFace の内部キャッシュがデバッグを阻害するため、データ前処理パスや HF Hub キャッシュフォルダを手動で削除・リダイレクトする必要がある。
VSCode デバッグ設定の提供
上記のテクニックを実践するための具体的な設定ファイル(shards 利用や micro_batch_size の調整など)を VSCode で即座に適用できる形式で公開している。
編集可能なインストールの必要性
コードの変更をランタイムに反映させるために、Axolotl を `pip3 install -e` で編集可能形式でインストールする必要があります。
VSCode 設定ファイルの活用
.vscode/launch.json ファイルを修正して、開発環境やリモートホストでのデバッグ構成をカスタマイズできます。
データキャッシュの管理戦略
各実行前に HF データセットキャッシュを削除し、データ前処理コードが毎回最初から実行されるように設定することが推奨されます。
デバッグ用設定の最適化
単一プロセスでのデータ前処理、1 ステップのトレーニング制限、バッチサイズの最小化などにより、デバッグ環境を簡素化しています。
影響分析・編集コメントを表示
影響分析
この記事は、Axolotl を使用して大規模言語モデルの微調整を行う開発者が直面しがちな「ブラックボックス化されたエラー」や「再現性の低い不具合」を解決するための具体的なロードマップを提供します。実践的な設定値の提示により、開発者の学習コストを大幅に削減し、プロジェクトの安定稼働とデバッグ効率の向上に直接寄与します。
編集コメント
LLM の微調整開発において、コードのロジックよりも環境設定やキャッシュ管理が原因でデバッグに時間を浪費するケースは珍しくありません。本記事はそのようなボトルネックを解消するための即戦力となるガイドです。

動機
Axolotl は大規模言語モデル(LLM)のファインチューニングに優れたプロジェクトです。私はこのプロジェクトへの貢献を始めましたが、デバッグが難しいと感じました。そこで、私が学んだコツやヒント、そして VSCode でデバッグするための設定ファイルを紹介したいと考えました。さらに、バグに直面したりコードの仕組みを理解したい開発者にとって、Axolotl をデバッグできる能力は非常に重要だと考えています。このドキュメントが皆さんのスタートを支援できれば幸いです。
本コンテンツは現在 Axolotl の公式ドキュメントの一部となっています!
私はこのブログ記事の内容を axolotl プロジェクトのドキュメントとして貢献しました。この内容は、axolotl リポジトリのこちらで確認できます。
一般的なヒント
デバッグ中は、テストシナリオを可能な限り単純化することが役立ちます。そのためのいくつかのヒントを以下に示します:
注記
これらすべてのヒントは、以下の VSCode でデバッグするための例設定ファイルに組み込まれています。
最新の Axolotl バージョンを使用していることを確認してください:このプロジェクトは頻繁に変更され、バグも迅速に修正されます。git ブランチを確認し、main ブランチから最新の変更をプルしたことを確認してください。
並列処理を排除する:トレーニングとデータ前処理の両方でプロセス数を 1 に制限します。
CUDA_VISIBLE_DEVICES を単一の GPU に設定します(例:export CUDA_VISIBLE_DEVICES=0)。
axolotl の設定ファイルで dataset_processes: 1 とするか、トレーニングコマンドに --dataset_processes=1 を追加して実行してください。
小さなデータセットを使用する:HF Hub から小さなデータセットを構築するか、既存のものを利用してください。小さなデータセットを使用する場合、エラーを回避するために sample_packing: False および eval_sample_packing: False を設定する必要があります。もし時間がないため小さなデータセットを構築できないが、HF Hub のデータを使いたい場合は、データをシャード(分割)することができます(これによりデータセット全体はトークン化されますが、トレーニングにはその一部のみが使用されます。例えば、データセットを 20 個のピースにシャードするには、axolotl 設定に以下の内容を追加してください):
dataset:
...
shards: 20
小さなモデルを使用する:小さなモデルの良い例として TinyLlama/TinyLlama-1.1B-Chat-v1.0 が挙げられます。
反復時間を最小化する:トレーニングループがこれらの設定で可能な限り速く完了するようにしてください。
micro_batch_size: 1
max_steps: 1
val_set_size: 0
キャッシュをクリアする:Axolotl は特定のステップをキャッシュしますが、基盤となる HuggingFace トレーナーも同様です。デバッグ時にはこれらのキャッシュの一部を削除すると良いでしょう。
データ前処理:プロンプトテンプレートの形成を含むデータ前処理のデバッグを行う場合、axolotl 設定内の dataset_prepared_path: で指定されたディレクトリを削除することを検討してください。この値を設定していない場合は、デフォルトは last_run_prepared です。
HF Hub:データ前処理のデバッグを行っている場合は、適切な ~/.cache/huggingface/datasets/... フォルダを削除することで、関連する HF キャッシュ(HuggingFace キャッシュ)をクリアする必要があります。
推奨されるアプローチは、すべての出力とキャッシュを一時的なフォルダにリダイレクトし、各実行前に選択したサブフォルダを削除することです。これは以下の例構成で示されています。
VSCode を使用したデバッグ
背景
以下の例は、sharegpt 形式のデータ前処理を VSCode でデバッグする方法を示しています。これは、axolotl 設定に以下のような内容がある場合に使用される形式です:
datasets:
- path: # HF Hub の例:philschmid/guanaco-sharegpt-style
type: sharegpt
重要
すでに高度な VSCode デバッグに精通している場合は、以下の説明をスキップし、.vscode/launch.json および .vscode/tasks.json ファイルの例構成を確認してください。
ヒント
動画で確認したい場合は、以下のビデオチュートリアルへスキップできます(ただし、両方を行うことを推奨します)。
セットアップ
Axolotl の編集可能なインストールがされていることを確認してください。これにより、コードに対する変更が実行時に反映されます。このプロジェクトのルートから以下のコマンドを実行してください:
pip3 install packaging
pip3 install -e '.[flash-attn,deepspeed]'
リモートホスト
リモートホストで開発している場合、VSCode を使用して簡単にリモートデバッグを行うことができます。そのためには、このリモート-SSH ガイドに従う必要があります。また、Docker およびリモート SSH デバッグに関する以下のビデオも参照できます。
構成
始める最も簡単な方法は、axolotl の GitHub リポジトリにある .vscode/launch.json ファイルを修正することです。これは単なる例の構成なので、必要に応じてこれを修正またはコピーしてご自身のニーズに合わせて調整する必要があります。
例えば、コマンド cd devtools && CUDA_VISIBLE_DEVICES=0 accelerate launch -m axolotl.cli.train dev_sharegpt.yml を模倣したい場合は、以下の構成を使用します1。ここで追加フラグを追加し、axolotl の設定を上書きして上記のヒントを取り入れています(コメントを参照)。また、作業ディレクトリを devtools に設定し、HF_HOME 環境変数を後で部分的に削除される一時フォルダに設定しています。これは、各実行前に HF データセットキャッシュを削除して、データ前処理コードが最初から実行されるようにするためです。
// https://github.com/OpenAccess-AI-Collective/axolotl/blob/main/.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug axolotl prompt - sharegpt",
"type": "python",
"module": "accelerate.commands.launch",
"request": "launch",
"args": [
"-m", "axolotl.cli.train", "dev_sharegpt.yml",
// The flags below simplify debugging by overriding the axolotl config
// with the debugging tips above. Modify as needed.
"--dataset_processes=1", // limits data preprocessing to one process
"--max_steps=1", // limits training to just one step
"--batch_size=1", // minimizes batch size
"--micro_batch_size=1", // minimizes batch size
"--val_set_size=0", // disables validation
"--sample_packing=False", // disables sample packing which is necessary for small datasets
"--eval_sample_packing=False",// disables sample packing on eval set
"--dataset_prepared_path=temp_debug/axolotl_outputs/data", // send data outputs to a temp folder
"--output_dir=temp_debug/axolotl_outputs/model" // send model outputs to a temp folder
],
"console": "integratedTerminal", // show output in the integrated terminal
"cwd": "${workspaceFolder}/devtools", // set working directory to devtools from the root of the project
"justMyCode": true, // step through only axolotl code
"env": {"CUDA_VISIBLE_DEVICES": "0", // Since we aren't doing distributed training, we need to limit to one GPU
"HF_HOME": "${workspaceFolder}/devtools/temp_debug/.hf-cache"}, // send HF cache to a temp folder
"preLaunchTask": "cleanup-for-dataprep", // delete temp folders (see below)
}
]
}
この設定に関する追加の注意事項:
justMyCode 引数は true に設定されており、これにより axolotl コードのみをステップ実行できるようになります。依存関係もステップ実行したい場合は、これを false に設定してください。
preLaunchTask: cleanup-for-dataprep は .vscode/tasks.json で定義されており、デバッグ前に以下のフォルダを削除するために使用されます。これはデータ前処理コードを最初から実行することを確実にするために不可欠です:
./devtools/temp_debug/axolotl_outputs
./devtools/temp_debug/.hf-cache/datasets
ヒント
これらのフォルダを削除したくない場合もあります。例えば、データ前処理ではなくモデルのトレーニングをデバッグしている場合は、キャッシュまたは出力フォルダを削除したくないかもしれません。また、ユースケースに応じて tasks.json ファイルに追加タスクを追加する必要がある場合もあります。
以下は cleanup-for-dataprep タスクを定義する ./vscode/tasks.json ファイルです。上記の設定を使用する場合、このタスクは各デバッグセッションの前に実行されます。2 つのフォルダを削除する 2 つのタスクがあり、それらを組み合わせた composite task(複合タスク)として cleanup-for-dataprep が定義されている点に注意してください。VSCode では launch.json ファイルの preLaunchTask 引数で複数のタスクを指定できないため、この composite task が必要です。
// https://github.com/OpenAccess-AI-Collective/axolotl/blob/main/.vscode/tasks.json
// this file is used by launch.json
{
"version": "2.0.0",
"tasks": [
// this task changes into the devtools directory and deletes the temp_debug/axolotl_outputs folder
{
"label": "delete-outputs",
"type": "shell",
"command": "rm -rf temp_debug/axolotl_outputs",
"options":{ "cwd": "${workspaceFolder}/devtools"},
"problemMatcher": []
},
// this task changes into the devtools directory and deletes the temp_debug/.hf-cache/datasets folder
{
"label": "delete-temp-hf-dataset-cache",
"type": "shell",
"command": "rm -rf temp_debug/.hf-cache/datasets",
"options":{ "cwd": "${workspaceFolder}/devtools"},
"problemMatcher": []
},
// this task combines the two tasks above
{
"label": "cleanup-for-dataprep",
"dependsOn": ["delete-outputs", "delete-temp-hf-dataset-cache"],
}
]
}
デバッガーのカスタマイズ
あなたのデバッグユースケースは上記の例とは異なる可能性があります。最も簡単な方法は、独自の axolotl 設定ファイルを devtools フォルダに配置し、launch.json ファイルを修正してその設定を使用するようにすることです。また、preLaunchTask を変更して、削除するフォルダーを変更したり、全く削除しないようにすることもできます。
ビデオチュートリアル
以下のビデオチュートリアルでは、上記の設定について解説し、VSCode でデバッグする方法を実演します:
Docker でのデバッグ
公式の Axolotl Docker イメージを使用するのは、コードをデバッグする優れた方法であり、Axolotl を利用する非常に一般的な手法です。VSCode で Docker にアタッチするには、いくつかの手順が追加で必要になります。
セットアップ
axolotl を実行しているホスト(例:リモートホストを利用している場合)上で、axolotl リポジトリをクローンし、現在のディレクトリをルートに移動してください:
git clone https://github.com/OpenAccess-AI-Collective/axolotl
cd axolotl
ヒント
すでにホスト上に axolotl をクローンしている場合は、最新の変更を取得し、プロジェクトのルートディレクトリに移動してください。
次に、希望する Docker イメージを実行し、現在のディレクトリをマウントします。これを行うための Docker コマンドは以下の通りです:
docker run --privileged --gpus '"all"' --shm-size 10g --rm -it --name axolotl --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 --mount type=bind,src="${PWD}",target=/workspace/axolotl -v ${HOME}/.cache/huggingface:/root/.cache/huggingface winglian/axolotl:main-py3.10-cu118-2.0.1
ヒント
利用可能なコンテナを確認するには、README の Docker セクションおよび DockerHub リポジトリを参照してください。Docker コンテナの構築方法の詳細については、axolotl の Docker CI ビルドを参照してください。
これでコンテナ内に入ります。次に、Axolotl を編集可能インストール(editable install)してください:
pip3 install packaging
pip3 install -e '.[flash-attn,deepspeed]'
コンテナへのアタッチ
次に、リモートホストを使用している場合は、VSCode でそのホストにリモート接続してください。ローカルホストを使用している場合は、このステップをスキップできます。
次に、VSCode のコマンドパレット(CMD + SHIFT + P)を使用して「Dev Containers: Attach to Running Container...」を選択します。接続するコンテナの選択を求められるので、先ほど作成したコンテナを選んでください。これでプロジェクトのルートディレクトリを作業ディレクトリとして持つコンテナ内に移動できます。コードに対して行う変更は、コンテナ内とホスト側の両方に即座に反映されます。
これで、前述の手順(VSCode を使用したデバッグ)に従ってデバッグを開始する準備が整いました。
ビデオ - リモートホスト上の Docker へのアタッチ
リモートホストの Docker コンテナにアタッチする方法を示す短い動画をご紹介します:
脚注
実際の設定は、コマンド「CUDA_VISIBLE_DEVICES=0 python -m accelerate.commands.launch -m axolotl.cli.train devtools/sharegpt.yml」を模倣したものです。これは同じことを意味しています。↩︎
以下のフラグの多くは、nvidia-container-toolkit を使用する際に Nvidia が推奨するベストプラクティスです。これらのフラグについては、こちらで詳しく読むことができます。↩︎
原文を表示

Motivation
Axolotl is a great project for fine-tuning LLMs. I started contributing to the project, and I found that it was difficult to debug. I wanted to share some tips and tricks I learned along the way, along with configuration files for debugging with VSCode. Moreover, I think being able to debug axolotl empowers developers who encounter bugs or want to understand how the code works. I hope this document helps you get started.
This content is now part of the Axolotl docs!
I contributed this blog post’s contents as documentation for the axolotl project. You can find this content in the axolotl repo here.
General Tips
While debugging, it’s helpful to simplify your test scenario as much as possible. Here are some tips for doing so:
Note
All of these tips are incorporated into the example configuration for debugging with VSCode below.
Make sure you are using the latest version of axolotl: This project changes often and bugs get fixed fast. Check your git branch and make sure you have pulled the latest changes from main.
Eliminate Concurrency: Restrict the number of processes to 1 for both training and data preprocessing:
Set CUDA_VISIBLE_DEVICES to a single GPU, ex: export CUDA_VISIBLE_DEVICES=0.
Set dataset_processes: 1 in your axolotl config or run the training command with --dataset_processes=1.
Use a small dataset: Construct or use a small dataset from HF Hub. When using a small dataset, you will often have to make sure sample_packing: False and eval_sample_packing: False to avoid errors. If you are in a pinch and don’t have time to construct a small dataset but want to use from the HF Hub, you can shard the data (this will still tokenize the entire dataset but will only use a fraction of the data for training. For example, to shard the dataset into 20 pieces, add the following to your axolotl config):
dataset:
...
shards: 20
Use a small model: A good example of a small model is TinyLlama/TinyLlama-1.1B-Chat-v1.0.
Minimize iteration time: Make sure the training loop finishes as fast as possible, with these settings.
micro_batch_size: 1
max_steps: 1
val_set_size: 0
Clear Caches: Axolotl caches certain steps and so does the underlying HuggingFace trainer. You may want to clear some of these caches when debugging.
Data preprocessing: When debugging data preprocessing, which includes prompt template formation, you may want to delete the directory set in dataset_prepared_path: in your axolotl config. If you didn’t set this value, the default is last_run_prepared.
HF Hub: If you are debugging data preprocessing, you should clear the relevant HF cache HuggingFace cache, by deleting the appropriate ~/.cache/huggingface/datasets/... folder(s).
The recommended approach is to redirect all outputs and caches to a temporary folder and delete selected subfolders before each run. This is demonstrated in the example configuration below.
Debugging with VSCode
Background
The below example shows how to configure VSCode to debug data preprocessing of the sharegpt format. This is the format used when you have the following in your axolotl config:
datasets:
- path: <path to your sharegpt formatted dataset> # example on HF Hub: philschmid/guanaco-sharegpt-style
type: sharegpt
Important
If you are already familiar with advanced VSCode debugging, you can skip the below explanation and look at the files .vscode/launch.json and .vscode/tasks.json for an example configuration.
Tip
If you prefer to watch a video, rather than read, you can skip to the video tutorial below (but doing both is recommended).
Setup
Make sure you have an editable install of Axolotl, which ensures that changes you make to the code are reflected at runtime. Run the following commands from the root of this project:
pip3 install packaging
pip3 install -e '.[flash-attn,deepspeed]'
Remote Hosts
If you developing on a remote host, you can easily use VSCode to debug remotely. To do so, you will need to follow this remote - SSH guide. You can also see the video below on Docker and Remote SSH debugging.
Configuration
The easiest way to get started is to modify the .vscode/launch.json file in the axolotl GitHub repo. This is just an example configuration, so you may need to modify or copy it to suit your needs.
For example, to mimic the command cd devtools && CUDA_VISIBLE_DEVICES=0 accelerate launch -m axolotl.cli.train dev_sharegpt.yml, you would use the below configuration1. Note that we add additional flags that override the axolotl config and incorporate the tips above (see the comments). We also set the working directory to devtools and set the env variable HF_HOME to a temporary folder that is later partially deleted. This is because we want to delete the HF dataset cache before each run in order to ensure that the data preprocessing code is run from scratch.
// https://github.com/OpenAccess-AI-Collective/axolotl/blob/main/.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug axolotl prompt - sharegpt",
"type": "python",
"module": "accelerate.commands.launch",
"request": "launch",
"args": [
"-m", "axolotl.cli.train", "dev_sharegpt.yml",
// The flags below simplify debugging by overriding the axolotl config
// with the debugging tips above. Modify as needed.
"--dataset_processes=1", // limits data preprocessing to one process
"--max_steps=1", // limits training to just one step
"--batch_size=1", // minimizes batch size
"--micro_batch_size=1", // minimizes batch size
"--val_set_size=0", // disables validation
"--sample_packing=False", // disables sample packing which is necessary for small datasets
"--eval_sample_packing=False",// disables sample packing on eval set
"--dataset_prepared_path=temp_debug/axolotl_outputs/data", // send data outputs to a temp folder
"--output_dir=temp_debug/axolotl_outputs/model" // send model outputs to a temp folder
],
"console": "integratedTerminal", // show output in the integrated terminal
"cwd": "${workspaceFolder}/devtools", // set working directory to devtools from the root of the project
"justMyCode": true, // step through only axolotl code
"env": {"CUDA_VISIBLE_DEVICES": "0", // Since we aren't doing distributed training, we need to limit to one GPU
"HF_HOME": "${workspaceFolder}/devtools/temp_debug/.hf-cache"}, // send HF cache to a temp folder
"preLaunchTask": "cleanup-for-dataprep", // delete temp folders (see below)
}
]
}
Additional notes about this configuration:
The argument justMyCode is set to true such that you step through only the axolotl code. If you want to step into dependencies, set this to false.
The preLaunchTask: cleanup-for-dataprep is defined in .vscode/tasks.json and is used to delete the following folders before debugging, which is essential to ensure that the data pre-processing code is run from scratch:
./devtools/temp_debug/axolotl_outputs
./devtools/temp_debug/.hf-cache/datasets
Tip
You may not want to delete these folders. For example, if you are debugging model training instead of data pre-processing, you may NOT want to delete the cache or output folders. You may also need to add additional tasks to the tasks.json file depending on your use case.
Below is the ./vscode/tasks.json file that defines the cleanup-for-dataprep task. This task is run before each debugging session when you use the above configuration. Note how there are two tasks that delete the two folders mentioned above. The third task cleanup-for-dataprep is a composite task that combines the two tasks. A composite task is necessary because VSCode does not allow you to specify multiple tasks in the preLaunchTask argument of the launch.json file.
// https://github.com/OpenAccess-AI-Collective/axolotl/blob/main/.vscode/tasks.json
// this file is used by launch.json
{
"version": "2.0.0",
"tasks": [
// this task changes into the devtools directory and deletes the temp_debug/axolotl_outputs folder
{
"label": "delete-outputs",
"type": "shell",
"command": "rm -rf temp_debug/axolotl_outputs",
"options":{ "cwd": "${workspaceFolder}/devtools"},
"problemMatcher": []
},
// this task changes into the devtools directory and deletes the temp_debug/.hf-cache/datasets folder
{
"label": "delete-temp-hf-dataset-cache",
"type": "shell",
"command": "rm -rf temp_debug/.hf-cache/datasets",
"options":{ "cwd": "${workspaceFolder}/devtools"},
"problemMatcher": []
},
// this task combines the two tasks above
{
"label": "cleanup-for-dataprep",
"dependsOn": ["delete-outputs", "delete-temp-hf-dataset-cache"],
}
]
}
Customizing your debugger
Your debugging use case may differ from the example above. The easiest thing to do is to put your own axolotl config in the devtools folder and modify the launch.json file to use your config. You may also want to modify the preLaunchTask to delete different folders or not delete anything at all.
Video Tutorial
The following video tutorial walks through the above configuration and demonstrates how to debug with VSCode:
Debugging With Docker
Using official Axolotl Docker images is a great way to debug your code, and is a very popular way to use Axolotl. Attaching VSCode to Docker takes a few more steps.
Setup
On the host that is running axolotl (ex: if you are using a remote host), clone the axolotl repo and change your current directory to the root:
git clone https://github.com/OpenAccess-AI-Collective/axolotl
cd axolotl
Tip
If you already have axolotl cloned on your host, make sure you have the latest changes and change into the root of the project.
Next, run the desired docker image and mount the current directory. Below is a docker command you can run to do this:2
docker run --privileged --gpus '"all"' --shm-size 10g --rm -it --name axolotl --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 --mount type=bind,src="${PWD}",target=/workspace/axolotl -v ${HOME}/.cache/huggingface:/root/.cache/huggingface winglian/axolotl:main-py3.10-cu118-2.0.1
Tip
To understand which containers are available, see the Docker section of the README and the DockerHub repo. For details of how the Docker containers are built, see axolotl’s Docker CI builds.
You will now be in the container. Next, perform an editable install of Axolotl:
pip3 install packaging
pip3 install -e '.[flash-attn,deepspeed]'
Attach To Container
Next, if you are using a remote host, Remote into this host with VSCode. If you are using a local host, you can skip this step.
Next, select Dev Containers: Attach to Running Container... using the command palette (CMD + SHIFT + P) in VSCode. You will be prompted to select a container to attach to. Select the container you just created. You will now be in the container with a working directory that is at the root of the project. Any changes you make to the code will be reflected both in the container and on the host.
Now you are ready to debug as described above (see Debugging with VSCode).
Video - Attaching To Docker On Remote Host
Here is a short video that demonstrates how to attach to a Docker container on a remote host:
Footnotes
The config actually mimics the command CUDA_VISIBLE_DEVICES=0 python -m accelerate.commands.launch -m axolotl.cli.train devtools/sharegpt.yml, but this is the same thing.↩︎
Many of the below flags are recommended best practices by Nvidia when using nvidia-container-toolkit. You can read more about these flags here.↩︎
関連記事
今日のまとめ
AI日報で今日の重要ニュースをまとめ読み