This is a self contained guide on how to build a simple app and component spec and launch it via two different schedulers.


First thing we need to do is to install the TorchX python package which includes the CLI and the library.

# install torchx with all dependencies
$ pip install torchx[dev]

See the README for more information on installation.

torchx --help
usage: torchx [-h] [--log_level LOG_LEVEL] [--version]
              {describe,log,run,builtins,runopts,status,configure} ...

torchx CLI

optional arguments:
  -h, --help            show this help message and exit
  --log_level LOG_LEVEL
                        Python logging log level
  --version             show program's version number and exit

  Use the following commands to run operations, e.g.: torchx run ${JOB_NAME}


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.


This example uses Jupyter Notebook %%writefile to create local files for example purposes. Under normal usage you would have these as standalone files.


import sys
import argparse

def main(user: str) -> None:
    print(f"Hello, {user}!")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Hello world app"
        help="the person to greet",
    args = parser.parse_args(sys.argv[1:])


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.


import torchx.specs as specs

def greet(user: str, image: str = "my_app:latest") -> specs.AppDef:
    return specs.AppDef(
                    "-m", "my_app",
                    "--user", user,

We can execute our component via torchx run. The local_cwd scheduler executes the component relative to the current directory.

torchx run --scheduler local_cwd --user "your name"
torchx 2021-11-18 02:38:56 INFO     Log files located in: /tmp/torchx_pwzv3czq/torchx/hello_world-j3kmwlzt3j3rjd/greeter/0
torchx 2021-11-18 02:38:56 INFO     Waiting for the app to finish...
greeter/0 Hello, your name!
torchx 2021-11-18 02:38:57 INFO     Job finished: SUCCEEDED

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.


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:

%%writefile Dockerfile


Writing Dockerfile

Once we have the Dockerfile created we can create our docker image.

docker build -t my_app:latest -f Dockerfile .

Step 1/2 : FROM
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
143f80195431: Waiting
f18d016c4ccc: Waiting
eccbe17c44e1: Waiting
c0ad16d9fa05: Waiting
d4c7af0d4fa7: Waiting
30587ba7fd6b: Waiting
06b5edd6bf52: Waiting
909695be1d50: Waiting
f119a6d0a466: Waiting
88d87059c913: Waiting
6009a622672a: Waiting
d2e110be24e1: Verifying Checksum
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
4bbfd2c87b75: Pull complete
d4c7af0d4fa7: Verifying Checksum
d4c7af0d4fa7: Download complete
d2e110be24e1: Pull complete
889a7173dcfe: Pull complete
c0ad16d9fa05: Download complete
30587ba7fd6b: Verifying Checksum
30587ba7fd6b: Download complete
6009a622672a: Pull complete
909695be1d50: Verifying Checksum
909695be1d50: Download complete
f119a6d0a466: Download complete
88d87059c913: Verifying Checksum
88d87059c913: Download complete
143f80195431: Download complete
f18d016c4ccc: Verifying Checksum
f18d016c4ccc: Download 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
 ---> 3dbec59e8049
Step 2/2 : ADD .
 ---> 985dfdb7dc5c
Successfully built 985dfdb7dc5c
Successfully tagged my_app:latest

We can then launch it on the local scheduler.

torchx run --scheduler local_docker --image "my_app:latest" --user "your name"
torchx 2021-11-18 02:40:52 INFO     Pulling container image: my_app:latest (this may take a while)
torchx 2021-11-18 02:40:53 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 2021-11-18 02:40:54 INFO     Waiting for the app to finish...
greeter/0 Hello, your name!
torchx 2021-11-18 02:40:55 INFO     Job finished: SUCCEEDED

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 --image "my_app:latest" --user "your name"


TorchX also provides a number of builtin components with premade images. You can discover them via:

torchx builtins
Found 9 builtin components:
  1. metrics.tensorboard
  2. utils.booth
  3. utils.copy
  4. utils.echo
  5. utils.python
  7. utils.touch
  8. dist.ddp
  9. serve.torchserve

You can use these either from the CLI, from a pipeline or programmatically like you would any other component.

torchx run utils.echo --msg "Hello :)"
torchx 2021-11-18 02:40:57 INFO     Pulling container image: (this may take a while)
torchx 2021-11-18 02:42:59 INFO     Waiting for the app to finish...
echo/0 Hello :)
torchx 2021-11-18 02:43:00 INFO     Job finished: SUCCEEDED

Next Steps

  1. Checkout other features of the torchx CLI

  2. Learn how to author more complex app specs by referencing specs

  3. Browse through the collection of builtin components

  4. Take a look at the list of schedulers supported by the runner

  5. See which ML pipeline platforms you can run components on

  6. See a training app example


Access comprehensive developer documentation for PyTorch

View Docs


Get in-depth tutorials for beginners and advanced developers

View Tutorials


Find development resources and get your questions answered

View Resources