关于配置的所有内容¶
本深度指南将引导您完成编写配置以运行配方。
如何编写YAML配置文件并使用它运行配方
如何使用
instantiate和parseAPI如何有效地使用配置和命令行覆盖来运行配方
理解食谱的基础知识
参数存储在哪里?¶
有两个主要的入口点供您配置参数:configs 和 CLI overrides。Configs 是 YAML 文件,定义了在单个位置运行配方所需的所有参数。它们是重现运行的唯一真实来源。可以使用 tune 在命令行上覆盖配置参数,以便在不修改配置的情况下进行快速更改和实验。
编写配置¶
配置(Configs)是运行torchtune中配方(recipes)的主要入口点。它们应该是YAML文件,并且简单地列出了您想要为特定运行定义的参数值。
seed: null
shuffle: True
device: cuda
dtype: fp32
enable_fsdp: True
...
使用instantiate¶配置组件
许多字段将需要指定带有相关关键字参数的torchtune对象作为参数。模型、数据集、优化器和损失函数是常见的例子。您可以使用_component_子字段轻松完成此操作。在_component_中,您需要指定您希望在配方中实例化的对象的点路径。点路径是您在Python文件中通常用于导入对象的确切路径。例如,要在配置中指定带有自定义参数的alpaca_dataset:
dataset:
_component_: torchtune.datasets.alpaca_dataset
train_on_input: False
在这里,我们将train_on_input的默认值从True更改为False。
一旦你在配置中指定了_component_,你就可以在你的配方设置中创建指定对象的实例,如下所示:
from torchtune import config
# Access the dataset field and create the object instance
dataset = config.instantiate(cfg.dataset)
这将自动使用在dataset下的字段中指定的任何关键字参数。
如前所述,前面的示例实际上会抛出错误。如果你查看alpaca_dataset的方法,
你会注意到我们缺少一个必需的位置参数,即tokenizer。
由于这是另一个可配置的torchtune对象,让我们通过查看instantiate() API来理解如何处理这个问题。
def instantiate(
config: DictConfig,
*args: Any,
**kwargs: Any,
)
instantiate() 也接受位置参数和关键字参数,并在创建对象时自动与配置一起使用。这意味着我们不仅可以传入分词器,还可以添加配置中未指定的额外关键字参数,如果我们愿意的话:
# Tokenizer is needed for the dataset, configure it first
tokenizer:
_component_: torchtune.models.llama2.llama2_tokenizer
path: /tmp/tokenizer.model
dataset:
_component_: torchtune.datasets.alpaca_dataset
# Note the API of the tokenizer we specified - we need to pass in a path
def llama2_tokenizer(path: str) -> Llama2Tokenizer:
# Note the API of the dataset we specified - we need to pass in a model tokenizer
# and any optional keyword arguments
def alpaca_dataset(
tokenizer: ModelTokenizer,
train_on_input: bool = True,
max_seq_len: int = 512,
) -> SFTDataset:
from torchtune import config
# Since we've already specified the path in the config, we don't need to pass
# it in
tokenizer = config.instantiate(cfg.tokenizer)
# We pass in the instantiated tokenizer as the first required argument, then
# we change an optional keyword argument
dataset = config.instantiate(
cfg.dataset,
tokenizer,
train_on_input=False,
)
请注意,额外的关键字参数将覆盖配置中任何重复的键。
使用插值引用其他配置字段¶
有时你需要为多个字段多次使用相同的值。你可以使用插值来引用另一个字段,instantiate()会自动为你解析它。
output_dir: /tmp/alpaca-llama2-finetune
metric_logger:
_component_: torchtune.training.metric_logging.DiskLogger
log_dir: ${output_dir}
验证您的配置¶
我们提供了一个方便的CLI工具,调优验证,用于快速验证您的配置是否格式正确以及所有组件是否可以正确实例化。如果您想测试将要运行实验的确切命令,您还可以传入覆盖。如果任何参数格式不正确,调优验证将列出所有发现错误的位置。
tune cp llama2/7B_lora_single_device ./my_config.yaml
tune validate ./my_config.yaml
编写配置的最佳实践¶
让我们讨论一些编写配置的指南,以充分利用它们。
密封配置¶
虽然将尽可能多的内容放入配置中以在实验中切换参数以获得最大灵活性可能很诱人,但我们鼓励您仅在配置中包含将在配方中使用或实例化的字段。这确保了配方运行时的选项完全清晰,并将显著简化调试过程。
# dont do this
alpaca_dataset:
_component_: torchtune.datasets.alpaca_dataset
slimorca_dataset:
...
# do this
dataset:
# change this in config or override when needed
_component_: torchtune.datasets.alpaca_dataset
仅使用公共API¶
如果您希望在配置中指定的组件位于私有文件中,请在配置中使用公共点路径。这些组件通常在其父模块的__init__.py文件中公开。这样,您可以保证在配置中使用的API的稳定性。组件点路径中不应有下划线。
# don't do this
dataset:
_component_: torchtune.datasets._alpaca.alpaca_dataset
# do this
dataset:
_component_: torchtune.datasets.alpaca_dataset
命令行覆盖¶
配置是收集所有参数以运行配方的主要位置,但有时您可能希望快速尝试不同的值,而不必更新配置本身。为了支持快速实验,您可以通过tune命令指定覆盖配置中的参数值。这些应指定为键值对k1=v1 k2=v2 ...
例如,要使用自定义模型和分词器目录运行LoRA单设备微调配方,你可以提供覆盖:
tune run lora_finetune_single_device \
--config llama2/7B_lora_single_device \
checkpointer.checkpoint_dir=/home/my_model_checkpoint \
checkpointer.checkpoint_files=['file_1','file_2'] \
tokenizer.path=/home/my_tokenizer_path
覆盖组件¶
如果你想覆盖配置中通过_component_字段实例化的类或函数,你可以直接赋值给参数名称。组件中的任何嵌套字段都可以通过点符号进行覆盖。
dataset:
_component_: torchtune.datasets.alpaca_dataset
# Change to slimorca_dataset and set train_on_input to True
tune run lora_finetune_single_device --config my_config.yaml \
dataset=torchtune.datasets.slimorca_dataset dataset.train_on_input=True
移除配置字段¶
在通过覆盖更改组件时,您可能需要从配置中删除某些参数,这些覆盖需要不同的关键字参数。您可以通过使用~标志并指定要删除的配置字段的点路径来实现。例如,如果您想覆盖内置配置并使用bitsandbytes.optim.PagedAdamW8bit优化器,您可能需要删除特定于PyTorch优化器的参数,如foreach。请注意,此示例要求您已安装bitsandbytes。
# In configs/llama3/8B_full.yaml
optimizer:
_component_: torch.optim.AdamW
lr: 2e-5
foreach: False
# Change to PagedAdamW8bit and remove fused, foreach
tune run --nproc_per_node 4 full_finetune_distributed --config llama3/8B_full \
optimizer=bitsandbytes.optim.PagedAdamW8bit ~optimizer.foreach