实用工具¶
这包含TorchX实用组件,这些组件是开箱即用的ready-to-use。这些组件简单地执行众所周知的二进制文件(例如cp),并旨在作为教程材料或工作流中具有意义的阶段之间的粘合操作。
- torchx.components.utils.echo(msg: str = 'hello world', image: str = 'ghcr.io/pytorch/torchx:0.8.0dev0', num_replicas: int = 1) AppDef[source]¶
将消息回显到标准输出(调用 echo)
- Parameters:
msg – 要回显的信息
图像 – 使用的图像
num_replicas – 运行的副本数量
- torchx.components.utils.touch(file: str, image: str = 'ghcr.io/pytorch/torchx:0.8.0dev0') AppDef[source]¶
触碰一个文件(调用 touch)
- Parameters:
文件 – 创建的文件
图像 – 要使用的图像
- torchx.components.utils.sh(*args: str, image: str = 'ghcr.io/pytorch/torchx:0.8.0dev0', num_replicas: int = 1, cpu: int = 1, gpu: int = 0, memMB: int = 1024, h: Optional[str] = None, env: Optional[Dict[str, str]] = None, max_retries: int = 0, mounts: Optional[List[str]] = None) AppDef[source]¶
通过 sh 运行提供的命令。目前 sh 不支持环境变量替换。
- Parameters:
args – 命令行参数
图像 – 使用的图像
num_replicas – 运行的副本数量
CPU – 每个副本的CPU数量
GPU – 每个副本的 GPU 数量
memMB – 每个副本中的CPU内存以MB为单位
h – 一个已注册的命名资源(如果指定,则优先于cpu、gpu、memMB)
env – 需要传递给运行的环境变量(例如 ENV1=v1,ENV2=v2,ENV3=v3)
max_retries – 允许的调度器重试次数
挂载点 – 挂载到工作环境/容器中的挂载点(例如 type=<bind/volume>,src=/host,dst=/job[,readonly])。 请参阅调度程序文档以获取更多信息。
- torchx.components.utils.copy(src: str, dst: str, image: str = 'ghcr.io/pytorch/torchx:0.8.0dev0') AppDef[source]¶
复制文件从源路径到目标路径。源路径和目标路径可以是任何有效的 fsspec URL。
这不支持递归复制或目录。
- Parameters:
src – 源 fsspec 文件位置
dst – 目标fsspec文件位置
图像 – 包含复制应用的图像
- torchx.components.utils.python(*args: str, m: Optional[str] = None, c: Optional[str] = None, script: Optional[str] = None, image: str = 'ghcr.io/pytorch/torchx:0.8.0dev0', name: str = 'torchx_utils_python', cpu: int = 1, gpu: int = 0, memMB: int = 1024, h: Optional[str] = None, num_replicas: int = 1) AppDef[source]¶
在指定的图像和主机上使用指定的模块、命令或脚本运行
python。使用--分隔组件参数和程序参数(例如:torchx run utils.python --m foo.main -- --args to --main)- Note: (cpu, gpu, memMB) parameters are mutually exclusive with
h(named resource) where h如果指定了资源需求设置,则优先考虑。 请参阅 注册命名资源。
- Parameters:
参数 – 传递给程序的参数,位于 sys.argv[1:] 中(在使用–c时被忽略)
m – 以脚本方式运行库模块
c – 作为字符串传递的程序(如果调度程序对参数长度有限制,则可能会出错)
脚本 – .py 脚本以运行
图像 – 要运行的图像
名称 – 工作的名称
CPU – 每个副本的CPU数量
GPU – 每个副本的 GPU 数量
memMB – 每个副本中的CPU内存以MB为单位
h – 一个已注册的命名资源(如果指定,则优先于cpu、gpu、memMB)
num_replicas – 运行的副本数量(每个副本都在自己的容器中)
- Returns:
- Note: (cpu, gpu, memMB) parameters are mutually exclusive with
用法与普通的 Python 非常相似,不同之处在于它支持远程启动。例如:
# locally (cmd)
$ torchx run utils.python --image $FBPKG -c "import torch; print(torch.__version__)"
# locally (module)
$ torchx run utils.python --image $FBPKG -m foo.bar.main
# remote (cmd)
$ torchx run -s mast utils.python --image $FBPKG -c "import torch; print(torch.__version__)"
# remote (module)
$ torchx run -s mast utils.python --image $FBPKG -m foo.bar.main
Notes:
torchx run修补了当前工作目录(CWD),以便在$FBPKG上进行更快的远程迭代。补丁内容将包含对本地 fbcode 的所有更改,但是只有当当前工作目录是 fbcode 的子目录时,才会触发补丁构建。如果你是从 fbcode 的根目录运行(例如 ~/fbsource/fbcode),你的作业将不会被打补丁!
请注意不要滥用
-c CMD。调度器对参数长度有限制,因此不要尝试传递过长的命令,谨慎使用。在-m MODULE中,模块需要基于fbcode根目录。例如:对于~/fbsource/fbcode/foo/bar/main.py,模块是
-m foo.bar.main。请勿在
base_module中覆盖python_library规则。如果您这样做,后果自负,补丁将无法工作。
组件中的内联脚本
注意
重要提示:请勿滥用此功能!请谨慎使用,切勿滥用!我们保留在将来移除此功能的权利。
TorchX 和 penv python 的构建方式的一个很好的副作用是你几乎可以做任何通常用 python 能做的事情,并且它还能自动修补你的工作目录,赋予你在本地和远程运行的能力。
这意味着 -c CMD 版本的 python 也将正常工作。下面是一个例子来说明这一点
$ cd ~/fbsource/fbcode/torchx/examples/apps
$ ls
component.py config main.py module README.md TARGETS
# lets try getting the version of torch from a prebuilt fbpkg or bento kernel
$ torchx run utils.python --image bento_kernel_pytorch_lightning -c "import torch; print(torch.__version__)"
torchx 2021-10-27 11:27:28 INFO loaded configs from /data/users/kiuk/fbsource/fbcode/torchx/fb/example/.torchxconfig
2021-10-27 11:27:44,633 fbpkg.fetch INFO: completed download of bento_kernel_pytorch_lightning:405
2021-10-27 11:27:44,634 fbpkg.fetch INFO: extracted bento_kernel_pytorch_lightning:405 to bento_kernel_pytorch_lightning
2021-10-27 11:27:48,591 fbpkg.util WARNING: removing old version /home/kiuk/.torchx/fbpkg/bento_kernel_pytorch_lightning/403
All packages downloaded successfully
local_penv://torchx/torchx_utils_python_6effc4e2
torchx 2021-10-27 11:27:49 INFO Waiting for the app to finish...
1.11.0a0+fb
torchx 2021-10-27 11:27:58 INFO Job finished: SUCCEEDED
Now for a more interesting example, lets run a dumb all reduce of a 1-d tensor on 1 worker:
$ torchx run utils.python --image torchx_fb_example \
-c "import torch; import torch.distributed as dist; dist.init_process_group(backend='gloo', init_method='tcp://localhost:29500', rank=0, world_size=1); t=torch.tensor(1); dist.all_reduce(t); print(f'all reduce result: {t.item()}')"
torchx 2021-10-27 10:23:05 INFO loaded configs from /data/users/kiuk/fbsource/fbcode/torchx/fb/example/.torchxconfig
2021-10-27 10:23:09,339 fbpkg.fetch INFO: checksums verified: torchx_fb_example:11
All packages verified
local_penv://torchx/torchx_utils_python_08a41456
torchx 2021-10-27 10:23:09 INFO Waiting for the app to finish...
all reduce result: 1
torchx 2021-10-27 10:23:13 INFO Job finished: SUCCEEDED
WARNING: Long inlined scripts won't work since schedulers typically have a character limit on the length of each argument.
- torchx.components.utils.booth(x1: float, x2: float, trial_idx: int = 0, tracker_base: str = '/tmp/torchx-util-booth', image: str = 'ghcr.io/pytorch/torchx:0.8.0dev0') AppDef[source]¶
评估 booth 函数,
f(x1, x2) = (x1 + 2*x2 - 7)^2 + (2*x1 + x2 - 5)^2。 输出结果可通过FsspecResultTracker(outdir)[trial_idx]访问- Parameters:
x1 – x1
x2 – x2
trial_idx – 如果不运行超参数优化,请忽略
tracker_base – 跟踪器基础输出目录的URI(例如 s3://foo/bar)
图像 – 包含展位应用程序的图像
- torchx.components.utils.binary(*args: str, entrypoint: str, name: str = 'torchx_utils_binary', num_replicas: int = 1, cpu: int = 1, gpu: int = 0, memMB: int = 1024, h: Optional[str] = None) AppDef[source]¶
测试组件
- Parameters:
参数 – 传递给程序的参数,位于 sys.argv[1:] 中(在使用–c时被忽略)
名称 – 工作的名称
num_replicas – 运行的副本数量(每个副本都在自己的容器中)
CPU – 每个副本的CPU数量
GPU – 每个副本的 GPU 数量
memMB – 每个副本中的CPU内存以MB为单位
h – 一个已注册的命名资源(如果指定,则优先于cpu、gpu、memMB)
- Returns: