自定义组件和配方¶
torchtune 允许您直接从命令行启动微调作业,使用内置和自定义组件,如数据集、模型、配方和配置。这是通过 tune run 命令完成的(参见 torchtune 命令行界面),也可以从您的项目文件夹中使用。
设置您的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 的内置配方为您的微调工作流提供了起点,但您可以根据您的用例编写自己的训练循环,并使用 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的惯例,在你的脚本中定义一个main()函数,并用parse()装饰器来装饰它。这将使你能够使用tune run启动配方,并为--config参数传入一个yaml文件。
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 启动。
我们建议在制作组件时遵循“构建器”模式。这意味着创建“构建器”函数,这些函数使用一些可以从配置中轻松修改的高级参数来设置所需的类。例如,我们可以在项目目录中定义自定义模型和数据集构建器:
#
# 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(...)
这使我们能够以配置友好的方式暴露我们的自定义模型 - 而不是必须在配置中定义构建自定义模型所需的每个参数,我们只暴露我们关心修改的参数。这就是我们在torchtune中实现模型的方式 - 参见llama3_2_vision_11b()作为示例。
#
# 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配方,您必须将第一个位置参数定义为分词器或模型转换器。这些在实例化期间会自动传递到数据集中,并在配置中单独定义,而不是在数据集字段下。
你可以在配置中使用相对导入路径来定义自定义模型和自定义数据集,从你启动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
如果您的自定义组件未被正确找到或导入,您可以尝试在修改PYTHONPATH后使用tune run启动,以确保项目目录中的文件可被导入。
PYTHONPATH=${pwd}:PYTHONPATH tune run recipes/single_device.py --config config/custom_finetune.yaml