自定义组件和配方¶
Torchtune 允许您使用内置和自定义组件直接从命令行启动微调作业。
例如数据集、模型、配方和配置。这是通过命令完成的(请参阅 torchtune CLI),
也可以从您的 Project 文件夹中使用。tune run
设置 torchtune 项目¶
首先,确保您已安装 torchtune - 请参阅安装说明。这将安装命令
,因此您可以从任何目录启动。让我们创建一个新的项目目录并确保
我们可以从该文件夹中使用库配置启动内置库配方。tune
tune run
mkdir ~/my_project
cd ~/my_project
# This downloads the Llama 3.2 1B Instruct model
tune download meta-llama/Llama-3.2-1B-Instruct --output-dir /tmp/Llama-3.2-1B-Instruct --ignore-patterns "original/consolidated.00.pth"
# This launches a lora finetuning run with the default single device config
tune run lora_finetune_single_device --config llama3_2/1B_lora_single_device
启动自定义配置¶
通常,您需要从特定模型的默认配置之一开始,并调整一些训练超参数。
您可以使用该命令在项目目录中创建默认配置的副本,以便进行修改。tune cp
# Show all the default model configs for each recipe
tune ls
# This makes a copy of a Qwen2 full finetune config
tune cp qwen2/0.5B_full_single_device ~/my_project/config/qwen_config.yaml
现在,您可以直接在项目文件夹中对配置进行修改并启动自定义配置。确保您使用的是 与配置关联的正确配方,并且您已下载模型。即使您不是从复制库开始的 配方,您可以使用相同的命令启动完全自定义的配置。请注意,对于自定义配置,您必须指定文件扩展名。
mkdir ~/my_project/config
tune run full_finetune_single_device --config ~/my_project/config/qwen_config.yaml
# Or launch directly from the project directory with a relative path
tune run full_finetune_single_device --config config/qwen_config.yaml
有关下载模型和修改库配置的更详细讨论,请参阅微调您的第一个 LLM。
启动自定义配方¶
torchtune 的内置配方为您的微调工作流程提供了起点,但您可以编写自己的训练循环
替换为您的使用案例的自定义逻辑,并使用 .与修改库配置类似,您可以
还可以复制我们的一个配方作为起点并进行修改,或者完全从头开始编写一个。请注意,对于启动
custom recipes,则必须指定文件扩展名。tune run
mkdir ~/my_project/recipes
# Show all the default recipes
tune ls
# This makes a copy of the full finetune single device recipe locally
tune cp full_finetune_single_device ~/my_project/recipes/single_device.py
# Launch custom recipe with custom config from project directory
tune run recipes/single_device.py --config config/qwen_config.yaml
如果您要从头开始编写新配方,我们建议您遵循定义函数的 Python 约定
并在脚本中使用 Decorator 对其进行装饰。这将使您能够启动配方
with 并传入参数的 YAML 文件。
main()
tune run
--config
from torchtune import config
from omegaconf import DictConfig
@config.parse
def main(cfg: DictConfig):
# Add all your recipe logic here, access config fields as attributes
if __name__ == "__main__":
# Config will be parsed from CLI, don't need to pass in here
main()
使用自定义组件启动¶
Torchtune 支持对自定义模型、数据集、优化器或任何微调组件进行全面实验。您可以定义
这些在您的存储库中本地使用它们,并在您可以使用 .tune run
我们建议在制作组件时遵循 “builder” 模式。这意味着创建设置 您需要的类,带有一些高级参数,可以从 Config 中轻松修改。例如,我们可以定义 custom 模型和数据集构建器:
#
# In models/custom_decoder.py
#
class CustomTransformerDecoder(nn.Module):
# A custom architecture not present in torchtune
# Builder function for the custom model
def custom_model(num_layers: int, classification_head: bool = False):
# Any setup for defining the class
...
# Return the module you want to train
return CustomTransformerDecoder(...)
这允许我们以配置友好的方式公开我们的自定义模型 - 而不必定义所需的每个参数
在我们的 config 中构建我们的自定义模型,我们只暴露我们关心修改的参数。这就是我们实施的方式
我们在 Torchtune 中的模型 - 请参阅示例。
#
# In datasets/custom_dataset.py
#
from torchtune.datasets import SFTDataset, PackedDataset
from torchtune.data import InputOutputToMessages
from torchtune.modules.tokenizers import ModelTokenizer
# Example builder function for a custom code instruct dataset not in torchtune, but using
# different dataset building blocks from torchtune
def tiny_codes(tokenizer: ModelTokenizer, packed: bool = True):
"""
Python subset of nampdn-ai/tiny-codes. Instruct and code response pairs.
"""
ds = SFTDataset(
model_transform=tokenizer,
source="nampdn-ai/tiny-codes",
message_transform=InputOutputToMessages(
column_map={"input": "prompt", "output": "response"},
),
filter_fn=lambda x: x["language"] == "python",
split="train",
)
if packed:
return PackedDataset(ds, max_seq_len=tokenizer.max_seq_len, split_across_pack=False)
else:
return ds
注意
如果您将默认 torchtune 配方与自定义数据集一起使用,则必须定义第一个 positional 参数设置为 tokenizer 或 model transform。这些会自动传递到 dataset 的实例化,并在 config 中单独定义,而不是在 dataset 字段下定义。
您可以使用相对导入路径在配置中定义自定义模型和自定义数据集,其中
您正在使用 启动 。最好定义相对于项目根目录的路径
并从那里启动。tune run
# In YAML file config/custom_finetune.yaml
model:
_component_: models.custom_decoder.custom_model
num_layers: 32
# this is an optional param, so you can also omit this from the config
classification_head: False
dataset:
_component_: datasets.custom_dataset.tiny_codes
# we don't need to define a tokenizer here as it's automatically passed in
packed: True
cd ~/my_project/
tune run recipes/single_device.py --config config/custom_finetune.yaml
如果您的自定义组件未被找到或未正确导入,您可以尝试在
修改 以确保项目目录中的文件是可导入的。tune run
PYTHONPATH
PYTHONPATH=${pwd}:PYTHONPATH tune run recipes/single_device.py --config config/custom_finetune.yaml