重要提示
您正在查看 NeMo 2.0 文档。此版本对 API 和新库 NeMo Run 进行了重大更改。我们目前正在将 NeMo 1.0 的所有功能移植到 2.0。有关先前版本或 2.0 中尚不可用的功能的文档,请参阅 NeMo 24.07 文档。
将 PEFT 训练和推理从 NeMo 1.0 迁移到 NeMo 2.0#
NeMo 1.0(先前版本)#
在 NeMo 1.0 中,PEFT 通过在 YAML 文件中设置 peft_scheme
字段来启用。
model:
peft:
peft_scheme: "lora"
restore_from_path: null
在代码中,PEFT 和全参数微调之间唯一的区别是 add_adapter
和 load_adapters
函数。
NeMo 2.0(新版本)#
在 NeMo 2.0 中,PEFT 通过将 PEFT 方法回调传递给训练器和模型来启用
lora = llm.peft.LoRA(
target_modules=['linear_qkv', 'linear_proj'], # 'linear_fc1', 'linear_fc2'],
dim=32,
)
trainer = nl.Trainer(..., callbacks=[lora])
model = llm.LlamaModel(..., model_transform=lora)
使用微调 API,PEFT 通过传入 peft
标志来启用。也可以指定基础模型和适配器路径。
from nemo.collections import llm
sft = llm.finetune(
...
peft=llm.peft.LoRA(target_modules=['linear_qkv', 'linear_proj'], dim=32),
...
)
sft.resume.import_path = "hf://..."
sft.resume.adapter_path = "/path/to/checkpoints/last"
迁移步骤#
从 YAML 配置文件中删除
peft
部分。使用您想要的配置(与 YAML 配置文件中
peft
部分中的配置相同)创建 LoRA 类(或任何 PEFT 方法)的实例。将 LoRA 类传递给 llm.finetune 中的 “peft” 字段。对于推理,还要传入
resume.adapter_path
。
from nemo.collections import llm
sft = llm.finetune(
...
peft=llm.peft.LoRA(target_modules=['linear_qkv', 'linear_proj'], dim=32),
...
)
sft.resume.import_path = "hf://..."
sft.resume.adapter_path = "/path/to/checkpoints/last"
关于迁移的一些注意事项#
在 NeMo 1 中,四个 LoRA 目标被命名为 [‘attention_qkv’, ‘attention_dense’, ‘mlp_fc1’, ‘mlp_fc2’]。这些名称与线性层的实际名称不同,这让一些用户感到困惑。在 NeMo 2 中,四个 LoRA 目标被重命名以匹配线性层:[‘linear_qkv’, ‘linear_proj’, ‘linear_fc1’, ‘linear_fc2’]