Skip to content

Commit 20040ba

Browse files
aivanoufacebook-github-bot
authored andcommitted
Update configure.rst with custom components and named resource configuration (#55)
Summary: Pull Request resolved: #55 Update configure.rst with custom components and named resource configuration Reviewed By: kiukchung Differential Revision: D29018082 fbshipit-source-id: 53f6556d185b43deff4b4716ff3e0ee8c3483054
1 parent fc3c33d commit 20040ba

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

docs/source/configure.rst

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,25 @@ enumerate t-shirt sized resource specs for the containers as:
6565
6666
And refer to the resources by their string names.
6767

68-
<COMING SOON>
68+
Torchx supports custom resource definition and reference by string
69+
representation:
70+
71+
::
72+
73+
[torchx.schedulers]
74+
gpu_x_2 = my_module.resources:gpu_x_2
75+
76+
77+
The named resource after that can be used in the following manner:
78+
79+
.. code-block:: python
80+
81+
# my_module.component
82+
from torchx.specs import AppDef, Role
83+
from torchx.components.base import torch_dist_role
84+
85+
app = AppDef(name="test_app", roles=[torch_dist_role(.., resource="gpu_x_2", ...)])
86+
6987
7088
Registering Custom Components
7189
-------------------------------
@@ -79,4 +97,16 @@ when they run
7997
8098
$ torchx builtins
8199
82-
<COMING SOON>
100+
Custom components can be registered via the following endpoint:
101+
102+
::
103+
104+
[torchx.components]
105+
custom_component = my_module.components:my_component
106+
107+
108+
Custom components can be executed in the following manner:
109+
110+
.. code-block:: shell-session
111+
112+
$ torchx run --scheduler local --scheduler_args image_fetcher=...,root_dir=/tmp custom_component -- --name "test app"

torchx/components/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@
4343
Create component as function. Each component represents a function that accepts
4444
arbitrary arguments and returns ``specs.AppDef``.
4545
46+
The function should
47+
have the following properties:
48+
49+
* All arguments of the function must be annotated
50+
* Current supported types:
51+
+ Primitives: int, float, str
52+
+ Optional primitives: Optional[int], Optional[float], Optional[str]
53+
+ Dict: Dict[Primitive_key, Primitive_value]
54+
+ List: List[Primitive_value]
55+
+ Optional[List], Optional[Dict]
56+
* The function should have well defined description in
57+
https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html format
58+
59+
4660
Unit tests. Write unit tests that use ``torchx.specs.file_linter`` to validate
4761
the component's structure, similar to ``torchx.components.tests.distributed_test.py``.
4862

torchx/specs/__init__.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,34 @@
1212
def named_resource(resource: str) -> Resource:
1313
"""
1414
Gets resource object based on the string definition registered via entrypoints.txt.
15+
16+
Torchx implements named resource registration mechanism, which consists of
17+
the following steps:
18+
19+
1. Create a module and define your resource retrieval function:
20+
21+
.. code-block:: python
22+
23+
# my_module.resources
24+
from typing import Dict
25+
from torchx.specs import Resource
26+
27+
def gpu_x_1() -> Dict[str, Resource]:
28+
return Resource(cpu=2, memMB=64 * 1024, gpu = 2)
29+
30+
2. Register resource retrieval in the entrypoints section:
31+
32+
::
33+
34+
[torchx.schedulers]
35+
gpu_x_1 = my_module.resources:gpu_x_1
36+
37+
The ``gpu_x_1`` can be used as string argument to this function:
38+
39+
::
40+
41+
resource = named_resource("gpu_x_1")
42+
1543
"""
1644
try:
1745
resource_fn = load("torchx.resources", name=resource.lower())

0 commit comments

Comments
 (0)