Chapter 04

配置文件结构:YAML 字段全解

📌 commit ab1a0d8100+ 字段按 10 组归类

Axolotl 的核心就是 YAML,但字段数量爆炸。本章按功能分组讲透。

4.1十组字段总览

典型字段
模型base_model / tokenizer_type / model_type
量化load_in_4bit / load_in_8bit / bnb_4bit_*
PEFTadapter / lora_r / lora_alpha / lora_target_modules
数据datasets / val_set_size / dataset_prepared_path
序列sequence_len / sample_packing / pad_to_sequence_len
训练micro_batch_size / num_epochs / learning_rate / optimizer
分布式deepspeed / fsdp / fsdp_config
性能flash_attention / xformers_attention / gradient_checkpointing
RLHFrl / dpo_beta / dpo_use_weighting
日志output_dir / logging_steps / save_steps / wandb_*

4.2模型组

base_model: meta-llama/Meta-Llama-3-8B   # HF ID 或本地
tokenizer_type: AutoTokenizer            # AutoTokenizer / LlamaTokenizer / ...
trust_remote_code: false                  # 跑 InternLM/ChatGLM 等要 true
model_type: AutoModelForCausalLM
chat_template: llama3                     # llama3 / chatml / mistral / qwen / ...

4.3量化组

load_in_4bit: true
bnb_4bit_quant_type: nf4
bnb_4bit_compute_dtype: bfloat16
bnb_4bit_use_double_quant: true

# 或
load_in_8bit: true

4.4PEFT 组

adapter: qlora                         # lora / qlora / "" 全参
lora_r: 16
lora_alpha: 32
lora_dropout: 0.05
lora_target_modules:
  - q_proj
  - k_proj
  - v_proj
  - o_proj
  - gate_proj
  - up_proj
  - down_proj
lora_modules_to_save:                  # 这些层全参训
  - embed_tokens
  - lm_head
lora_fan_in_fan_out: false
peft_use_dora: false
peft_use_rslora: false

4.5数据组

datasets:
  - path: tatsu-lab/alpaca
    type: alpaca
  - path: ./local.jsonl
    type: chat_template
    chat_template: llama3
    field_messages: messages
  - path: HuggingFaceH4/no_robots
    type: sharegpt
    split: train

dataset_prepared_path: ./prepared       # tokenize 后缓存路径
val_set_size: 0.02
shuffle_merged_datasets: true

4.6序列组

sequence_len: 4096
sample_packing: true                    # 打包,省 padding
pad_to_sequence_len: true               # 固定长度(packing 时建议)
eval_sample_packing: false              # eval 时关掉

4.7训练组

micro_batch_size: 2
gradient_accumulation_steps: 8
num_epochs: 2
learning_rate: 2e-4
optimizer: adamw_bnb_8bit
lr_scheduler: cosine
warmup_ratio: 0.03
weight_decay: 0.0
max_grad_norm: 1.0
bf16: auto                              # auto / true / false
fp16: false
tf32: true                              # A100 上 GEMM 加速

4.8性能组

flash_attention: true
xformers_attention: false               # 二选一
gradient_checkpointing: true
gradient_checkpointing_kwargs:
  use_reentrant: false                  # 推荐 false
torch_compile: false                    # 跑稳了再开

4.9分布式组

deepspeed: deepspeed_configs/zero2.json   # 路径或 inline
# 或
fsdp:
  - full_shard
  - auto_wrap
fsdp_config:
  fsdp_offload_params: false
  fsdp_state_dict_type: SHARDED_STATE_DICT
  fsdp_transformer_layer_cls_to_wrap: LlamaDecoderLayer

4.10RLHF 组

rl: dpo                                  # dpo / kto / ipo / orpo / simpo / grpo
dpo_beta: 0.1
dpo_use_weighting: false
rl_beta: 0.1                              # 通用 beta(trl 风格)

4.11日志 / 检查点组

output_dir: ./out
logging_steps: 10
save_steps: 200
save_total_limit: 3
evaluation_strategy: steps
eval_steps: 200
wandb_project: my-axolotl
wandb_name: llama3-qlora-v1
wandb_log_model: checkpoint              # 把 ckpt 也传 wandb

4.12AxolotlInputConfig:Pydantic schema 是契约

v2026 之后所有 YAML 字段都被 src/axolotl/utils/schemas/config.pyAxolotlInputConfig(Pydantic BaseModel)严格校验。schema 模块按 mixin 拆分:

schema 模块位置覆盖字段
config.py schemas/config.py 主入口 + 各 mixin 组合
datasets.py schemas/datasets.py SFTDataset / DPODataset / KTODataset / StepwiseSupervisedDataset / SyntheticDataset 5 个类型联合
enums.py schemas/enums.py RLType / ChatTemplate / CustomOptimizers / TrainingMode 等
model.py schemas/model.py base_model / tokenizer / attention 实现 / dtype
training.py schemas/training.py 训练超参
peft.py schemas/peft.py LoRA / QLoRA / DoRA 字段
trl.py schemas/trl.py RLHF 配置(继承 TRL config)
quantization.py schemas/quantization.py QAT / PTQ 字段
multimodal.py schemas/multimodal.py vision / audio processor 字段
validation.py schemas/validation.py 跨字段 ValidationMixin(torch 版本检查、flex_attn 兼容等)

看 schema 比 grep yaml 字段更准——"我的 YAML 报 pydantic.ValidationError,怎么查?"

  1. 报错里 loc: [datasets, 0, type] 这种路径,定位到 schemas/datasets.py 对应字段;
  2. 或运行 axolotl config-schema --field datasets 直接 dump 该字段的合法形态;
  3. 跨字段 validator 在 config.py:1402+(model_validator 装饰器)。

4.13rl 字段决定整套 schema 走哪条路

YAML 顶层 rl 字段是整份 config 的总开关,影响:

rl用的 Builderdataset schema额外字段
不写 / null HFCausalTrainerBuilder SFTDataset
dpo HFRLTrainerBuilder → AxolotlDPOTrainer DPODataset(要 field_chosen / field_rejecteddpo_beta / dpo_use_weighting
ipo 同上但 loss 变形 DPODataset
kto AxolotlKTOTrainer KTODataset(要 field_label kto_desirable_weight / kto_undesirable_weight
orpo AxolotlORPOTrainer SFTDataset(用 ORPO prompt strategy) orpo_alpha
simpoAxolotlCPOTrainer SFTDataset simpo_gamma
grpo / gdpoAxolotlGRPOTrainer / AxolotlAsyncGRPOTrainerSFTDataset(在线生成)reward_funcs / num_generations / max_completion_length
ebft EBFTStrategy SFTDataset 能量基特征对齐参数

4.14派生字段:YAML 不写但运行时被算出来

YAML 你写运行时派生
load_in_4bit: true 自动选 BNB Linear4bit + 强制 adapter: qlora(如果 adapter 没写)
sample_packing: true 选 multipack collator + 启用 flash-attn 兼容
rl: dpo + 没传 dpo_beta dpo_beta=0.1 默认
chat_template: llama3 tokenizer 自动 patch chat_template Jinja,train_on_inputs=False
flash_attention: true + sample_packing: true启用 multipack flash-attn patch(不开 sample_packing 时仅普通 fa2)
adapter: qlora load_in_4bit=true 强制;prepare_model_for_kbit_training 自动调

4.15这章你需要带走的