数据集格式与 type 字段
Axolotl 的 datasets[i].type 决定数据怎么解析成 input_ids + labels。本章把常见 type 都过一遍,再讲怎么自定义。
5.1type 全清单(常用)
| type | 预期数据列 | 典型场景 |
|---|---|---|
alpaca | instruction / input / output | Alpaca / GPT4 SFT |
alpaca_chat | 同上,但带多轮 | 多轮 alpaca |
chat_template | messages | ★ 通用 chat |
sharegpt | conversations: [{from, value}] | ShareGPT 风格 |
completion | text | continued pretraining |
oasst | OASST 树状 | OpenAssistant |
jeopardy | question / answer | QA 风格 |
summarizetldr | article / summary | 摘要 |
tokens_only | 已 tokenize 的 input_ids | 性能优化 |
dpo / kto / orpo | chosen / rejected | 对齐 |
5.2chat_template 类型(最通用)
datasets:
- path: ./my_chat.jsonl
type: chat_template
chat_template: llama3 # tokenizer 自带 chat template
field_messages: messages # 数据列名
message_field_role: role
message_field_content: content
roles: # 每个 role 是否算 loss
user:
- human
assistant:
- gpt
- assistant
对应 jsonl:
{"messages": [
{"role": "user", "content": "..."},
{"role": "assistant", "content": "..."}
]}
5.3alpaca 类型
datasets:
- path: tatsu-lab/alpaca
type: alpaca
# 数据列
{"instruction": "...", "input": "...", "output": "..."}
Axolotl 自动拼成 ### Instruction:\n... ### Input:\n... ### Response:\n...。
注意:alpaca 是非 chat 模板,有 instruction-following 格式但没 chat 角色。
5.4sharegpt 类型
datasets:
- path: ./sharegpt.jsonl
type: sharegpt
# 数据列
{"conversations": [
{"from": "human", "value": "..."},
{"from": "gpt", "value": "..."}
]}
5.5DPO / KTO 数据
rl: dpo
datasets:
- path: HuggingFaceH4/ultrafeedback_binarized
split: train_prefs
type: chatml.intel # 命名约定 = type + variant
# 内部期望
{"prompt": "...", "chosen": "...", "rejected": "..."}
DPO 数据 type 有十几个 variant:chatml.intel、llama3.intel、ultra.argilla 等,对应不同公开偏好集格式。
5.6多数据集混合
datasets:
- path: tatsu-lab/alpaca
type: alpaca
- path: ./domain.jsonl
type: chat_template
chat_template: llama3
- path: HuggingFaceH4/no_robots
type: sharegpt
Axolotl 默认 concat 所有数据集,按比例 = 各自 sample 数。要权重:
datasets:
- path: domain.jsonl
type: chat_template
weight: 3.0 # 上采样 3×
5.7自定义 type
看 src/axolotl/prompt_strategies/,每个 type 是一个 PromptStrategy 子类。简化:
class MyStrategy(PromptStrategy):
def tokenize_prompt(self, example):
prompt = f"<|user|>{example['q']}<|assistant|>{example['a']}"
ids = self.tokenizer(prompt, return_tensors=None)
labels = ids["input_ids"].copy()
# mask user 部分
...
return {"input_ids": ids["input_ids"], "labels": labels}
def load(tokenizer, cfg):
return MyStrategy(tokenizer, cfg)
把文件放在 src/axolotl/prompt_strategies/my_strat.py,YAML 里写 type: my_strat 即可。
5.8predict 用 dataset_processes
大数据集 tokenize 慢,开多进程:
dataset_processes: 16
预处理结果缓存到 dataset_prepared_path,第二次跑直接读 cache。
5.9常见踩坑
| 现象 | 处理 |
|---|---|
| "unknown type" | type 拼写错;或自定义 type 没注册 |
| 训完只回单字 / EOS | chat_template 跟 base model 不一致 |
| tokenize 巨慢 | 开 dataset_processes |
| 多 dataset 比例失衡 | 用 weight 字段调 |
5.1033+ prompt strategy 全表
v2026 之后 src/axolotl/prompt_strategies/ 已经累计 33+ 个策略,按"训练阶段"分类:
| 类别 | 位置 | type 取值 |
|---|---|---|
| SFT 主线 | 顶层 .py(14 个) | alpaca_chat / alpaca_instruct / alpaca_w_system / chat_template / completion / context_qa / creative_acr / input_output / llama2_chat / metharme / orcamini / pretrain / pygmalion / stepwise_supervised |
| DPO 变体 | dpo/(6 个) | dpo.chat_template / dpo.chatml / dpo.llama3 / dpo.passthrough / dpo.user_defined / dpo.zephyr |
| KTO 变体 | kto/(3 个) | kto.chatml / kto.llama3 / kto.user_defined |
| ORPO 变体 | orpo/(1 个) | orpo.chat_template |
| Bradley-Terry RM | bradley_terry/(2 个) | bradley_terry.chat_template / bradley_terry.llama3 |
| EBFT 变体 | ebft/(5 个) | ebft_chat_multiturn / ebft_opencode / ebft_reasoning / ebft_strided_chat / ebft_strided_structured |
| Messages | messages/(1 个) | messages.chat |
| 自定义 | 顶层 user_defined.py | user_defined |
| 合成数据 | _synthetic.py | 内部用,不直接选 |
5.11type 字段的动态解析
YAML 的 type: dpo.chatml 怎么找到代码?来源 prompt_strategies/__init__.py:12–53 的 load() 函数:
- 字符串按
.切分; - 第一段是 subdir / 文件名(如
dpo); - 后续段拼成模块(
dpo.chatml→prompt_strategies.dpo.chatml); - importlib 动态 import 该模块;
- 调它的
load(tokenizer, cfg, ...)函数返回 strategy 实例; - 找不到模块时静默返回 None(不报错) —— 这是新人常踩的坑:拼错 type 不会立刻 fail,会被当 user_defined。
5.12三种 dataset schema
schemas/datasets.py:254–297 用判别式联合区分三种 dataset config:
| dataset 类 | 必填字段 | 触发条件 |
|---|---|---|
SFTDataset | path / type | YAML rl 字段不写时 |
DPODataset | path / type / field_chosen / field_rejected(可选 field_prompt) | rl: dpo / ipo |
KTODataset | path / type / field_completion / field_label | rl: kto |
StepwiseSupervisedDataset | path / type=stepwise_supervised | 过程奖励训练(PRM) |
SyntheticDataset | generator_path | 在线生成数据 |
同一 YAML 里同时混 SFT 和 DPO dataset 不允许——rl 字段决定整个 datasets 列表的 schema。
5.13label masking 三选一
不同 type 决定哪段 token 算 loss。看 prompt_tokenizers.py 的 IGNORE_INDEX=-100(line 14)+ 各 strategy 自定义 mask 逻辑:
| 字段 | 含义 | 典型用 |
|---|---|---|
train_on_inputs: false(默认) | 仅 response 部分计 loss;instruction / system 被 -100 mask | SFT 标准 |
train_on_inputs: true | 所有 token 都计 loss | continued pretrain |
roles_to_train: ["assistant"] | chat_template 时只训 assistant 段(多轮 chat 默认) | 多轮 SFT |
train_on_eos: "turn" / "all" / "none" / "last" | EOS token 在哪里计 loss | multi-turn fine grained |
5.14这章你需要带走的
- 核心 type:chat_template / alpaca / sharegpt / completion;
- 对齐数据用
rl: dpo+ type 变体; - 自定义 type 写一个 PromptStrategy 类即可;
- 多数据集用 weight 字段调比例。