Custom Components¶
This is a guide on how to build a simple app and custom component spec and launch it via two different schedulers.
See the Quickstart Guide for installation and basic usage.
Hello World¶
Lets start off with writing a simple “Hello World” python app. This is just a normal python program and can contain anything you’d like.
Note
This example uses Jupyter Notebook %%writefile
to create local files for example purposes. Under normal usage you would have these as standalone files.
[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
Now that we have an app we can write the component file for it. This function allows us to reuse and share our app in a user friendly way.
We can use this component from the torchx
cli or programmatically as part of a pipeline.
[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
We can execute our component via torchx run
. The local_cwd
scheduler executes the component relative to the current directory.
[3]:
%%sh
torchx run --scheduler local_cwd my_component.py:greet --user "your name"
torchx 2023-04-04 01:08:09 INFO loaded configs from /home/ubuntu/rsync/torchx/docs/source/.torchxconfig
torchx 2023-04-04 01:08:09 INFO Tracker configurations: {}
torchx 2023-04-04 01:08:09 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 2023-04-04 01:08:09 INFO Log directory is: /tmp/torchx_aa14fi1s
torchx 2023-04-04 01:08:09 INFO Waiting for the app to finish...
greeter/0 Hello, your name!
torchx 2023-04-04 01:08:10 INFO Job finished: SUCCEEDED
local_cwd://torchx/hello_world-qtk1nrbcghqcsc
If we want to run in other environments, we can build a Docker container so we can run our component in Docker enabled environments such as Kubernetes or via the local Docker scheduler.
Note
This requires Docker installed and won’t work in environments such as Google Colab. If you have not done so already follow the install instructions on: 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
Once we have the Dockerfile created we can create our docker image.
[5]:
%%sh
docker build -t my_app:latest -f Dockerfile.custom .
Step 1/2 : FROM ghcr.io/pytorch/torchx:0.1.0rc1
---> 3dbec59e8049
Step 2/2 : ADD my_app.py .
---> Using cache
---> a3515c76f647
Successfully built a3515c76f647
Successfully tagged my_app:latest
We can then launch it on the local scheduler.
[6]:
%%sh
torchx run --scheduler local_docker my_component.py:greet --image "my_app:latest" --user "your name"
torchx 2023-04-04 01:08:11 INFO loaded configs from /home/ubuntu/rsync/torchx/docs/source/.torchxconfig
torchx 2023-04-04 01:08:11 INFO Tracker configurations: {}
torchx 2023-04-04 01:08:11 INFO Checking for changes in workspace `file:///home/ubuntu/rsync/torchx/docs/source`...
torchx 2023-04-04 01:08:11 INFO To disable workspaces pass: --workspace="" from CLI or workspace=None programmatically.
torchx 2023-04-04 01:08:11 INFO Workspace `file:///home/ubuntu/rsync/torchx/docs/source` resolved to filesystem path `/home/ubuntu/rsync/torchx/docs/source`
torchx 2023-04-04 01:08:12 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 2023-04-04 01:08:12 INFO Building workspace docker image (this may take a while)...
torchx 2023-04-04 01:08:13 INFO Built new image `sha256:f196aed28eab30f8d6ad39f2fcd6d11d7795b37e2e9378fcb401c85fd99041f0` based on original image `my_app:latest` and changes in workspace `file:///home/ubuntu/rsync/torchx/docs/source` for role[0]=greeter.
torchx 2023-04-04 01:08:13 INFO Waiting for the app to finish...
greeter/0 Hello, your name!
torchx 2023-04-04 01:08:14 INFO Job finished: SUCCEEDED
local_docker://torchx/hello_world-w7g3mtpg2vwlq
If you have a Kubernetes cluster you can use the Kubernetes scheduler to launch this on the cluster instead.
$ docker push my_app:latest
$ torchx run --scheduler kubernetes my_component.py:greet --image "my_app:latest" --user "your name"
Builtins¶
TorchX also provides a number of builtin components with premade images. You can discover them via:
[7]:
%%sh
torchx builtins
Found 11 builtin components:
1. dist.ddp
2. dist.spmd
3. metrics.tensorboard
4. serve.torchserve
5. utils.binary
6. utils.booth
7. utils.copy
8. utils.echo
9. utils.python
10. utils.sh
11. utils.touch
You can use these either from the CLI, from a pipeline or programmatically like you would any other component.
[8]:
%%sh
torchx run utils.echo --msg "Hello :)"
torchx 2023-04-04 01:08:16 INFO loaded configs from /home/ubuntu/rsync/torchx/docs/source/.torchxconfig
torchx 2023-04-04 01:08:16 INFO Tracker configurations: {}
torchx 2023-04-04 01:08:16 INFO Checking for changes in workspace `file:///home/ubuntu/rsync/torchx/docs/source`...
torchx 2023-04-04 01:08:16 INFO To disable workspaces pass: --workspace="" from CLI or workspace=None programmatically.
torchx 2023-04-04 01:08:16 INFO Workspace `file:///home/ubuntu/rsync/torchx/docs/source` resolved to filesystem path `/home/ubuntu/rsync/torchx/docs/source`
torchx 2023-04-04 01:08:17 INFO Building workspace docker image (this may take a while)...
torchx 2023-04-04 01:08:17 INFO Built new image `sha256:f196aed28eab30f8d6ad39f2fcd6d11d7795b37e2e9378fcb401c85fd99041f0` based on original image `ghcr.io/pytorch/torchx:0.5.0` and changes in workspace `file:///home/ubuntu/rsync/torchx/docs/source` for role[0]=echo.
torchx 2023-04-04 01:08:17 INFO Waiting for the app to finish...
torchx 2023-04-04 01:08:17 INFO Job finished: SUCCEEDED
echo/0 Hello :)
local_docker://torchx/echo-dlpgsqs33bh1hc