概述¶
注意
上图仅用于说明目的。并非所有功能目前都开箱即用。
该模块包含了一系列内置的TorchX组件。目录结构按组件类别组织。组件只是模板化的应用规范。可以将它们视为不同类型作业定义的工厂方法。在本模块中返回specs.AppDef的函数就是我们所说的组件。
您可以浏览torchx.components模块中的组件库
或访问我们的文档页面。
使用内置函数¶
一旦你找到一个内置组件,你可以:
将组件作为作业运行
在工作流(管道)的上下文中使用组件
在这两种情况下,组件都将作为作业运行,区别在于作业将直接在调度程序上作为独立作业运行,或者在具有上游和/或下游依赖关系的工作流中作为“阶段”运行。
注意
根据组件的语义,作业可能是单节点或分布式的。例如,如果组件具有单一角色,其中role.num_replicas == 1,那么作业是单节点作业。如果组件具有多个角色和/或任何角色的num_replicas > 1,那么作业是多节点分布式作业。
不确定是否应该将组件作为作业或管道阶段运行? 使用这个经验法则:
刚开始吗?通过将其作为作业运行来熟悉该组件
需要作业依赖关系?将组件作为流水线阶段运行
不需要作业依赖关系?将组件作为作业运行
验证¶
要验证您是否正确定义了组件,您可以:
(最简单)使用命令行界面(CLI)对你的组件的
--help进行试运行:torchx run --dryrun ~/component.py:train --help使用组件 linter (参见 dist_test.py 作为示例)
作为作业运行¶
你可以使用torchx 命令行界面将组件作为作业运行,或者使用torchx.runner以编程方式运行。两者是相同的,实际上cli在底层使用了runner,所以选择权在你手中。快速开始指南将带你了解入门的基础知识。
程序化运行¶
要以编程方式运行内置组件或您自己的组件,只需将组件作为常规的Python函数调用,并将其传递给torchx.runner。以下是一个调用utils.echo内置组件的示例:
from torchx.components.utils import echo
from torchx.runner import get_runner
get_runner().run(echo(msg="hello world"), scheduler="local_cwd")
CLI 运行(内置)¶
当从命令行运行组件时,您需要传递要调用的组件函数。
对于内置组件,其形式为{component_module}.{component_fn},其中
{component_module}是组件相对于torchx.components的模块路径,
而{component_fn}是该模块中的组件函数。因此,对于
torchx.components.utils.echo,我们会去掉torchx.components前缀并运行它。
$ torchx run utils.echo --msg "hello world"
查看 CLI 文档 获取更多信息。
CLI 运行(自定义)¶
要使用cli运行您的自定义组件,您必须使用稍微不同的语法形式{component_path}:{component_fn}。其中{component_path}是您的组件python文件的文件路径,{component_fn}是该文件中组件函数的名称。假设您的组件位于/home/bob/component.py,并且组件函数名为train(),您将这样运行它
# option 1. use absolute path
$ torchx run /home/bob/component.py:train --help
# option 2. let the shell do the expansion
$ torchx run ~/component.py:train --help
# option 3. same but after CWD to $HOME
$ cd ~/
$ torchx run ./component.py:train --help
# option 4. files can be relative to CWD
$ cd ~/
$ torchx run component.py:train --help
注意
如果你知道TorchX的安装目录,builtins也可以这样运行!
从CLI传递组件参数¶
由于组件只是简单的 Python 函数,以编程方式使用它们非常直接。
如上所述,当通过 CLI 的 run 子命令运行组件时,组件参数使用双破折号 + 参数名称语法作为程序参数传递(例如 --param1=1 或 --param1 1)。
CLI 根据组件的文档字符串自动生成 argparse 解析器。以下是如何传递各种类型组件参数的摘要,假设组件定义如下:
# in comp.py
from typing import Dict, List
import torchx.specs as specs
def f(i: int, f: float, s: str, b: bool, l: List[str], d: Dict[str, str], *args) -> specs.AppDef:
"""
Example component
Args:
i: int param
f: float param
s: string param
b: bool param
l: list param
d: map param
args: varargs param
Returns: specs.AppDef
"""
pass
帮助:
torchx run comp.py:f --help基本类型 (
int,float,str):torchx run comp.py:f --i 1 --f 1.2 --s "bar"布尔值:
torchx run comp.py:f --b True(或--b False)映射:
torchx run comp.py:f --d k1=v1,k2=v2,k3=v3列表:
torchx run comp.py:f --l a,b,cVAR_ARG:
*argsare passed as positionals rather than arguments, hence they are specified at the end of the command. The--delimiter is used to start the VAR_ARGS section. This is useful when the component and application have the same arguments or when passing through the--helparg. Below are a few examples: **args=["arg1", "arg2", "arg3"]:torchx run comp.py:f --i 1 arg1 arg2 arg3**args=["--flag", "arg1"]:torchx run comp.py:f --i 1 --flag arg1 `` * ``*args=["--help"]:torchx run comp.py:f -- --help**args=["--i", "2"]:torchx run comp.py:f --i 1 -- --i 2
在管道中运行¶
torchx.pipelines 定义了适配器,这些适配器将 torchx 组件转换为目标管道平台中表示管道“阶段”的对象(有关支持的管道编排器列表,请参见 管道)。
其他资源¶
参见: