| 
 | 1 | +---  | 
 | 2 | +jupyter:  | 
 | 3 | +  jupytext:  | 
 | 4 | +    text_representation:  | 
 | 5 | +      extension: .md  | 
 | 6 | +      format_name: markdown  | 
 | 7 | +      format_version: '1.1'  | 
 | 8 | +      jupytext_version: 1.1.0  | 
 | 9 | +  kernelspec:  | 
 | 10 | +    display_name: Python 3  | 
 | 11 | +    language: python  | 
 | 12 | +    name: python3  | 
 | 13 | +---  | 
 | 14 | + | 
 | 15 | +# Custom Components  | 
 | 16 | + | 
 | 17 | +This is a guide on how to build a simple app and custom component spec  | 
 | 18 | +and launch it via two different schedulers.  | 
 | 19 | + | 
 | 20 | +See the [Quickstart Guide](quickstart.md) for installation and basic usage.  | 
 | 21 | + | 
 | 22 | +## Hello World  | 
 | 23 | + | 
 | 24 | +Lets start off with writing a simple "Hello World" python app. This is just a  | 
 | 25 | +normal python program and can contain anything you'd like.  | 
 | 26 | + | 
 | 27 | +<div class="admonition note">  | 
 | 28 | +<div class="admonition-title">Note</div>  | 
 | 29 | +This example uses Jupyter Notebook `%%writefile` to create local files for  | 
 | 30 | +example purposes. Under normal usage you would have these as standalone files.  | 
 | 31 | +</div>  | 
 | 32 | + | 
 | 33 | +```python  | 
 | 34 | +%%writefile my_app.py  | 
 | 35 | + | 
 | 36 | +import sys  | 
 | 37 | +import argparse  | 
 | 38 | + | 
 | 39 | +def main(user: str) -> None:  | 
 | 40 | +    print(f"Hello, {user}!")  | 
 | 41 | + | 
 | 42 | +if __name__ == "__main__":  | 
 | 43 | +    parser = argparse.ArgumentParser(  | 
 | 44 | +        description="Hello world app"  | 
 | 45 | +    )  | 
 | 46 | +    parser.add_argument(  | 
 | 47 | +        "--user",  | 
 | 48 | +        type=str,  | 
 | 49 | +        help="the person to greet",  | 
 | 50 | +        required=True,  | 
 | 51 | +    )  | 
 | 52 | +    args = parser.parse_args(sys.argv[1:])  | 
 | 53 | + | 
 | 54 | +    main(args.user)  | 
 | 55 | +```  | 
 | 56 | + | 
 | 57 | +Now that we have an app we can write the component file for it. This  | 
 | 58 | +function allows us to reuse and share our app in a user friendly way.  | 
 | 59 | + | 
 | 60 | +We can use this component from the `torchx` cli or programmatically as part of a  | 
 | 61 | +pipeline.  | 
 | 62 | + | 
 | 63 | +```python  | 
 | 64 | +%%writefile my_component.py  | 
 | 65 | + | 
 | 66 | +import torchx.specs as specs  | 
 | 67 | + | 
 | 68 | +def greet(user: str, image: str = "my_app:latest") -> specs.AppDef:  | 
 | 69 | +    return specs.AppDef(  | 
 | 70 | +        name="hello_world",  | 
 | 71 | +        roles=[  | 
 | 72 | +            specs.Role(  | 
 | 73 | +                name="greeter",  | 
 | 74 | +                image=image,  | 
 | 75 | +                entrypoint="python",  | 
 | 76 | +                args=[  | 
 | 77 | +                    "-m", "my_app",  | 
 | 78 | +                    "--user", user,  | 
 | 79 | +                ],  | 
 | 80 | +            )  | 
 | 81 | +        ],  | 
 | 82 | +    )  | 
 | 83 | +```  | 
 | 84 | + | 
 | 85 | +We can execute our component via `torchx run`. The  | 
 | 86 | +`local_cwd` scheduler executes the component relative to the current directory.  | 
 | 87 | + | 
 | 88 | +```sh  | 
 | 89 | +torchx run --scheduler local_cwd my_component.py:greet --user "your name"  | 
 | 90 | +```  | 
 | 91 | + | 
 | 92 | +If we want to run in other environments, we can build a Docker container so we  | 
 | 93 | +can run our component in Docker enabled environments such as Kubernetes or via  | 
 | 94 | +the local Docker scheduler.  | 
 | 95 | + | 
 | 96 | +<div class="admonition note">  | 
 | 97 | +<div class="admonition-title">Note</div>  | 
 | 98 | +This requires Docker installed and won't work in environments such as Google  | 
 | 99 | +Colab. If you have not done so already follow the install instructions on:  | 
 | 100 | +[https://docs.docker.com/get-docker/](https://docs.docker.com/get-docker/)</a>  | 
 | 101 | +</div>  | 
 | 102 | + | 
 | 103 | +```python  | 
 | 104 | +%%writefile Dockerfile.custom  | 
 | 105 | + | 
 | 106 | +FROM ghcr.io/pytorch/torchx:0.1.0rc1  | 
 | 107 | + | 
 | 108 | +ADD my_app.py .  | 
 | 109 | +```  | 
 | 110 | + | 
 | 111 | +Once we have the Dockerfile created we can create our docker image.  | 
 | 112 | + | 
 | 113 | +```sh  | 
 | 114 | +docker build -t my_app:latest -f Dockerfile.custom .  | 
 | 115 | +```  | 
 | 116 | + | 
 | 117 | +We can then launch it on the local scheduler.  | 
 | 118 | + | 
 | 119 | +```sh  | 
 | 120 | +torchx run --scheduler local_docker my_component.py:greet --image "my_app:latest" --user "your name"  | 
 | 121 | +```  | 
 | 122 | + | 
 | 123 | +If you have a Kubernetes cluster you can use the [Kubernetes scheduler](schedulers/kubernetes.rst) to launch  | 
 | 124 | +this on the cluster instead.  | 
 | 125 | + | 
 | 126 | + | 
 | 127 | +<!-- #md -->  | 
 | 128 | +```sh  | 
 | 129 | +$ docker push my_app:latest  | 
 | 130 | +$ torchx run --scheduler kubernetes my_component.py:greet --image "my_app:latest" --user "your name"  | 
 | 131 | +```  | 
 | 132 | +<!-- #endmd -->  | 
 | 133 | + | 
 | 134 | + | 
 | 135 | +## Builtins  | 
 | 136 | + | 
 | 137 | +TorchX also provides a number of builtin components with premade images. You can discover  | 
 | 138 | +them via:  | 
 | 139 | + | 
 | 140 | +```sh  | 
 | 141 | +torchx builtins  | 
 | 142 | +```  | 
 | 143 | + | 
 | 144 | +You can use these either from the CLI, from a pipeline or programmatically like  | 
 | 145 | +you would any other component.  | 
 | 146 | + | 
 | 147 | +```sh  | 
 | 148 | +torchx run utils.echo --msg "Hello :)"  | 
 | 149 | +```  | 
0 commit comments