自定义组件¶
这是一份关于如何构建简单的应用程序和自定义组件规范并通过两个不同的调度器启动它的指南。
请参阅快速入门指南,了解安装和基本用法。
世界您好¶
让我们从编写一个简单的 “Hello World” python 应用程序开始。这只是一个普通的 python 程序,可以包含您想要的任何内容。
注意
此示例使用 Jupyter Notebook 创建本地文件以用于示例目的。在正常使用情况下,您可以将这些文件作为独立文件。%%writefile
[1]:
%%writefile my_app.py
import sys
import argparse
def main(user: str) -> None:
print(f"Hello, {user}!")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Hello world app"
)
parser.add_argument(
"--user",
type=str,
help="the person to greet",
required=True,
)
args = parser.parse_args(sys.argv[1:])
main(args.user)
Overwriting my_app.py
现在我们有一个应用程序,我们可以为它编写组件文件。此功能允许我们以用户友好的方式重用和共享我们的应用程序。
我们可以从 cli 或以编程方式将此组件用作管道的一部分。torchx
[2]:
%%writefile my_component.py
import torchx.specs as specs
def greet(user: str, image: str = "my_app:latest") -> specs.AppDef:
return specs.AppDef(
name="hello_world",
roles=[
specs.Role(
name="greeter",
image=image,
entrypoint="python",
args=[
"-m", "my_app",
"--user", user,
],
)
],
)
Overwriting my_component.py
我们可以通过 .调度程序相对于当前目录执行组件。torchx run
local_cwd
[3]:
%%sh
torchx run --scheduler local_cwd my_component.py:greet --user "your name"
torchx 2022-12-29 19:17:25 INFO loaded configs from /Users/priyaramani/Workspace/repos/torchx/docs/source/.torchxconfig
torchx 2022-12-29 19:17:25 INFO Log directory not set in scheduler cfg. Creating a temporary log dir that will be deleted on exit. To preserve log directory set the `log_dir` cfg option
torchx 2022-12-29 19:17:25 INFO Log directory is: /var/folders/ck/dsbpg4794mn2nfwc_wr5_fn40000gn/T/torchx_9yv1zl_2
torchx 2022-12-29 19:17:25 INFO Waiting for the app to finish...
greeter/0 Hello, your name!
torchx 2022-12-29 19:17:26 INFO Job finished: SUCCEEDED
local_cwd://torchx/hello_world-dmt9qmgnt3t1n
如果我们想在其他环境中运行,我们可以构建一个 Docker 容器,这样我们就可以在支持 Docker 的环境(如 Kubernetes)中或通过本地 Docker 调度器运行我们的组件。
注意
这需要安装 Docker,并且在 Google Colab 等环境中不起作用。如果您尚未按照以下安装说明进行作:https://docs.docker.com/get-docker/
[4]:
%%writefile Dockerfile.custom
FROM ghcr.io/pytorch/torchx:0.1.0rc1
ADD my_app.py .
Overwriting Dockerfile.custom
创建 Dockerfile 后,我们就可以创建 Docker 映像了。
[5]:
%%sh
docker build -t my_app:latest -f Dockerfile.custom .
#1 [internal] load build definition from Dockerfile.custom
#1 sha256:60f8ef355468ad2924fb679e28f50b5a2b3bdad6b954f193eb9404695415744f
#1 transferring dockerfile: 104B done
#1 DONE 0.0s
#2 [internal] load .dockerignore
#2 sha256:8ac94988f7c5094fa34500397a9c922a23a3a549962ce6e7ecb82e687c5771b5
#2 transferring context: 2B done
#2 DONE 0.0s
#3 [internal] load metadata for ghcr.io/pytorch/torchx:0.1.0rc1
#3 sha256:633e8f8dd2a3a4495d5dae7e523213a3449a3deffb6475efdd19f7312a781606
#3 DONE 1.3s
#4 [1/2] FROM ghcr.io/pytorch/torchx:0.1.0rc1@sha256:a738949601d82e7f100fa1efeb8dde0c35ce44c66726cf38596f96d78dcd7ad3
#4 sha256:9bc06e9084b68d5654928a267982308c15642a3206ba66cda0701bed701b3e4e
#4 DONE 0.0s
#5 [internal] load build context
#5 sha256:ebc0915754dd1b70dd8b8aab46f919c29128beab32dc2bfa9a11907704bc1068
#5 transferring context: 425B done
#5 DONE 0.0s
#6 [2/2] ADD my_app.py .
#6 sha256:519d159a9d60c1eca741396e5489f6a9b21de4496d26dd9a2e14b765b09c9aae
#6 CACHED
#7 exporting to image
#7 sha256:e8c613e07b0b7ff33893b694f7759a10d42e180f2b4dc349fb57dc6b71dcab00
#7 exporting layers done
#7 writing image sha256:eb931e46390433da7ea61b14a54a91affb86c7ce64d33f1c3e7a961f56cf98b4 done
#7 naming to docker.io/library/my_app:latest done
#7 DONE 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
然后,我们可以在本地调度器上启动它。
[6]:
%%sh
torchx run --scheduler local_docker my_component.py:greet --image "my_app:latest" --user "your name"
torchx 2022-12-29 19:17:29 INFO loaded configs from /Users/priyaramani/Workspace/repos/torchx/docs/source/.torchxconfig
torchx 2022-12-29 19:17:29 INFO Checking for changes in workspace `file:///Users/priyaramani/Workspace/repos/torchx/docs/source`...
torchx 2022-12-29 19:17:29 INFO To disable workspaces pass: --workspace="" from CLI or workspace=None programmatically.
torchx 2022-12-29 19:17:29 INFO Workspace `file:///Users/priyaramani/Workspace/repos/torchx/docs/source` resolved to filesystem path `/Users/priyaramani/Workspace/repos/torchx/docs/source`
torchx 2022-12-29 19:17:31 WARNING failed to pull image my_app:latest, falling back to local: 404 Client Error for http+docker://localhost/v1.41/images/create?tag=latest&fromImage=my_app: Not Found ("pull access denied for my_app, repository does not exist or may require 'docker login': denied: requested access to the resource is denied")
torchx 2022-12-29 19:17:31 INFO Building workspace docker image (this may take a while)...
torchx 2022-12-29 19:17:32 INFO Built new image `sha256:54af7c3dc8e1b168bf4c81e530c50202c6b72582082c7d58621f246be8bdf3c5` based on original image `my_app:latest` and changes in workspace `file:///Users/priyaramani/Workspace/repos/torchx/docs/source` for role[0]=greeter.
torchx 2022-12-29 19:17:32 INFO Waiting for the app to finish...
greeter/0 Hello, your name!
torchx 2022-12-29 19:17:33 INFO Job finished: SUCCEEDED
local_docker://torchx/hello_world-vl3n1wz9dfvk0
如果您有 Kubernetes 集群,则可以使用 Kubernetes 调度程序在集群上启动它。
$ docker push my_app:latest
$ torchx run --scheduler kubernetes my_component.py:greet --image "my_app:latest" --user "your name"
内置¶
TorchX 还提供了许多带有预制图像的内置组件。您可以通过以下方式发现它们:
[7]:
%%sh
torchx builtins
Found 10 builtin components:
1. metrics.tensorboard
2. serve.torchserve
3. utils.binary
4. utils.booth
5. utils.copy
6. utils.echo
7. utils.python
8. utils.sh
9. utils.touch
10. dist.ddp
您可以从 CLI、管道或像任何其他组件一样以编程方式使用这些组件。
[8]:
%%sh
torchx run utils.echo --msg "Hello :)"
torchx 2022-12-29 19:17:34 INFO loaded configs from /Users/priyaramani/Workspace/repos/torchx/docs/source/.torchxconfig
torchx 2022-12-29 19:17:35 INFO Checking for changes in workspace `file:///Users/priyaramani/Workspace/repos/torchx/docs/source`...
torchx 2022-12-29 19:17:35 INFO To disable workspaces pass: --workspace="" from CLI or workspace=None programmatically.
torchx 2022-12-29 19:17:35 INFO Workspace `file:///Users/priyaramani/Workspace/repos/torchx/docs/source` resolved to filesystem path `/Users/priyaramani/Workspace/repos/torchx/docs/source`
torchx 2022-12-29 19:17:36 INFO Building workspace docker image (this may take a while)...
torchx 2022-12-29 19:17:38 INFO Built new image `sha256:54af7c3dc8e1b168bf4c81e530c50202c6b72582082c7d58621f246be8bdf3c5` based on original image `ghcr.io/pytorch/torchx:0.4.0` and changes in workspace `file:///Users/priyaramani/Workspace/repos/torchx/docs/source` for role[0]=echo.
torchx 2022-12-29 19:17:38 INFO Waiting for the app to finish...
torchx 2022-12-29 19:17:38 INFO Job finished: SUCCEEDED
echo/0 Hello :)
local_docker://torchx/echo-lclrm7snk5s72