Shortcuts

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)
Writing 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,
                ],
            )
        ],
    )
Writing 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 2022-06-16 01:14:53 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-06-16 01:14:53 INFO     Log directory is: /tmp/torchx_nzcc2z92
torchx 2022-06-16 01:14:53 INFO     Waiting for the app to finish...
greeter/0 Hello, your name!
torchx 2022-06-16 01:14:54 INFO     Job finished: SUCCEEDED
local_cwd://torchx/hello_world-kxphx16075gvwd

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 .
Writing 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
0.1.0rc1: Pulling from pytorch/torchx
4bbfd2c87b75: Pulling fs layer
d2e110be24e1: Pulling fs layer
889a7173dcfe: Pulling fs layer
6009a622672a: Pulling fs layer
143f80195431: Pulling fs layer
eccbe17c44e1: Pulling fs layer
d4c7af0d4fa7: Pulling fs layer
06b5edd6bf52: Pulling fs layer
f18d016c4ccc: Pulling fs layer
c0ad16d9fa05: Pulling fs layer
30587ba7fd6b: Pulling fs layer
909695be1d50: Pulling fs layer
f119a6d0a466: Pulling fs layer
88d87059c913: Pulling fs layer
6009a622672a: Waiting
143f80195431: Waiting
eccbe17c44e1: Waiting
d4c7af0d4fa7: Waiting
06b5edd6bf52: Waiting
f18d016c4ccc: Waiting
c0ad16d9fa05: Waiting
30587ba7fd6b: Waiting
909695be1d50: Waiting
f119a6d0a466: Waiting
88d87059c913: Waiting
d2e110be24e1: Download complete
889a7173dcfe: Verifying Checksum
889a7173dcfe: Download complete
4bbfd2c87b75: Verifying Checksum
4bbfd2c87b75: Download complete
6009a622672a: Verifying Checksum
6009a622672a: Download complete
eccbe17c44e1: Verifying Checksum
eccbe17c44e1: Download complete
06b5edd6bf52: Verifying Checksum
06b5edd6bf52: Download complete
d4c7af0d4fa7: Verifying Checksum
d4c7af0d4fa7: Download complete
c0ad16d9fa05: Verifying Checksum
c0ad16d9fa05: Download complete
30587ba7fd6b: Verifying Checksum
30587ba7fd6b: Download complete
909695be1d50: Verifying Checksum
909695be1d50: Download complete
f119a6d0a466: Verifying Checksum
f119a6d0a466: Download complete
88d87059c913: Verifying Checksum
88d87059c913: Download complete
4bbfd2c87b75: Pull complete
f18d016c4ccc: Verifying Checksum
f18d016c4ccc: Download complete
143f80195431: Verifying Checksum
143f80195431: Download complete
d2e110be24e1: Pull complete
889a7173dcfe: Pull complete
6009a622672a: Pull complete
143f80195431: Pull complete
eccbe17c44e1: Pull complete
d4c7af0d4fa7: Pull complete
06b5edd6bf52: Pull complete
f18d016c4ccc: Pull complete
c0ad16d9fa05: Pull complete
30587ba7fd6b: Pull complete
909695be1d50: Pull complete
f119a6d0a466: Pull complete
88d87059c913: Pull complete
Digest: sha256:a738949601d82e7f100fa1efeb8dde0c35ce44c66726cf38596f96d78dcd7ad3
Status: Downloaded newer image for ghcr.io/pytorch/torchx:0.1.0rc1
 ---> 3dbec59e8049
Step 2/2 : ADD my_app.py .
 ---> 11b996417979
Successfully built 11b996417979
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 2022-06-16 01:17:02 INFO     Checking for changes in workspace `file:///home/runner/work/torchx/torchx/docs/source`...
torchx 2022-06-16 01:17:02 INFO     To disable workspaces pass: --workspace="" from CLI or workspace=None programmatically.
torchx 2022-06-16 01:17:03 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-06-16 01:17:10 INFO     Built new image `sha256:2eeaddea522b1922d57f1d34bfdf4c7ca9ae308b4e1278fa9e2df69bc7ee57c4` based on original image `my_app:latest` and changes in workspace `file:///home/runner/work/torchx/torchx/docs/source` for role[0]=greeter.
torchx 2022-06-16 01:17:11 INFO     Waiting for the app to finish...
greeter/0 Hello, your name!
torchx 2022-06-16 01:17:12 INFO     Job finished: SUCCEEDED
local_docker://torchx/hello_world-knsljmm493w04

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 10 builtin components:
  1. serve.torchserve
  2. utils.binary
  3. utils.booth
  4. utils.copy
  5. utils.echo
  6. utils.python
  7. utils.sh
  8. utils.touch
  9. dist.ddp
 10. metrics.tensorboard

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 2022-06-16 01:17:16 INFO     Checking for changes in workspace `file:///home/runner/work/torchx/torchx/docs/source`...
torchx 2022-06-16 01:17:16 INFO     To disable workspaces pass: --workspace="" from CLI or workspace=None programmatically.
torchx 2022-06-16 01:19:00 INFO     Built new image `sha256:46368bb0078abdedd3364c831b8461609cd55cae44f26d00296569ab0e88b6e1` based on original image `ghcr.io/pytorch/torchx:0.2.0` and changes in workspace `file:///home/runner/work/torchx/torchx/docs/source` for role[0]=echo.
torchx 2022-06-16 01:19:01 INFO     Waiting for the app to finish...
echo/0 Hello :)
torchx 2022-06-16 01:19:02 INFO     Job finished: SUCCEEDED
local_docker://torchx/echo-m9zjdqhn746hgc

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources