Skip to content

Commit 8513edf

Browse files
aivanoufacebook-github-bot
authored andcommitted
Rename named_resource to named_resources, make named_resources statically initialize resources (#63)
Summary: Pull Request resolved: #63 Rename ``named_resource`` to ``named_resources``, make ``named_resources`` statically initialize resources Reviewed By: kiukchung Differential Revision: D29153123 fbshipit-source-id: ed8718838c71571f91ba233abfe492c1f8741394
1 parent 014ca37 commit 8513edf

File tree

7 files changed

+39
-22
lines changed

7 files changed

+39
-22
lines changed

docs/source/components/base.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ Base
44
.. automodule:: torchx.components.base
55
.. currentmodule:: torchx.components.base
66

7-
.. autofunction:: named_resource
8-
97
.. automodule:: torchx.components.base.binary_component
108
.. currentmodule:: torchx.components.base.binary_component
119
.. autofunction:: binary_component

docs/source/configure.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,22 @@ representation:
7070

7171
::
7272

73-
[torchx.schedulers]
73+
[torchx.named_resources]
7474
gpu_x_2 = my_module.resources:gpu_x_2
7575

7676

7777
The named resource after that can be used in the following manner:
7878

79+
.. code-block:: python
80+
81+
# my_module.component
82+
from torchx.specs import AppDef, Role, named_resources
83+
from torchx.components.base import torch_dist_role
84+
85+
app = AppDef(name="test_app", roles=[Role(.., resource=named_resources("gpu_x_2"))])
86+
87+
or using ``torch.distributed.run``:
88+
7989
.. code-block:: python
8090
8191
# my_module.component

docs/source/specs.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Resource
2424
.. autoclass:: Resource
2525
:members:
2626

27+
.. autofunction:: get_named_resources
28+
2729
Macros
2830
------------
2931
.. autoclass:: macros

examples/apps/lightning_classy_vision/component.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from typing import Optional
1515

1616
import torchx.specs.api as torchx
17-
from torchx.components.base import named_resource
17+
from torchx.components.base import named_resources
1818
from torchx.components.base.binary_component import binary_component
1919

2020

@@ -50,7 +50,7 @@ def trainer(
5050
data_path,
5151
],
5252
image=image,
53-
resource=named_resource(resource)
53+
resource=named_resources[resource]
5454
if resource
5555
else torchx.Resource(cpu=1, gpu=0, memMB=1024),
5656
)
@@ -85,7 +85,7 @@ def interpret(
8585
output_path,
8686
],
8787
image=image,
88-
resource=named_resource(resource)
88+
resource=named_resources[resource]
8989
if resource
9090
else torchx.Resource(cpu=1, gpu=0, memMB=1024),
9191
)

torchx/components/base/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"""
1313
from typing import Any, Dict, List, Optional, Union
1414

15-
from torchx.specs import named_resource
15+
from torchx.specs import named_resources
1616
from torchx.specs.api import NULL_RESOURCE, Resource, RetryPolicy, Role
1717
from torchx.util.entrypoints import load
1818

@@ -23,7 +23,7 @@ def _resolve_resource(resource: Union[str, Resource]) -> Resource:
2323
if isinstance(resource, Resource):
2424
return resource
2525
else:
26-
return named_resource(resource.lower())
26+
return named_resources[resource.upper()]
2727

2828

2929
def torch_dist_role(

torchx/components/base/test/lib_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_torch_dist_role_str_resources(self) -> None:
1818
expected_resources = {}
1919
with patch.object(metadata, "entry_points") as entry_points_mock:
2020
entry_points_mock.return_value = {}
21-
with self.assertRaises(ValueError):
21+
with self.assertRaises(KeyError):
2222
torch_dist_role(
2323
name="dist_role",
2424
image="test_image",

torchx/specs/__init__.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,28 @@
66
# LICENSE file in the root directory of this source tree.
77

88
from .api import * # noqa: F401 F403
9-
from torchx.util.entrypoints import load
9+
from torchx.util.entrypoints import load_group
1010

1111
GiB: int = 1024
1212

1313

14-
def named_resource(resource: str) -> Resource:
14+
def _load_named_resources() -> Dict[str, Resource]:
15+
resource_methods = load_group("torchx.named_resources", default={})
16+
materialized_resources = {}
17+
for resource_name, resource_method in resource_methods.items():
18+
materialized_resources[resource_name] = resource_method()
19+
materialized_resources["NULL"] = NULL_RESOURCE
20+
return materialized_resources
21+
22+
23+
named_resources: Dict[str, Resource] = _load_named_resources()
24+
25+
26+
def get_named_resources(res: str) -> Resource:
1527
"""
16-
Gets resource object based on the string definition registered via entrypoints.txt.
28+
Get resource object based on the string definition registered via entrypoints.txt.
1729
18-
Torchx implements named resource registration mechanism, which consists of
30+
Torchx implements ``named_resource`` registration mechanism, which consists of
1931
the following steps:
2032
2133
1. Create a module and define your resource retrieval function:
@@ -33,20 +45,15 @@ def gpu_x_1() -> Dict[str, Resource]:
3345
3446
::
3547
36-
[torchx.schedulers]
48+
[torchx.named_resources]
3749
gpu_x_1 = my_module.resources:gpu_x_1
3850
3951
The ``gpu_x_1`` can be used as string argument to this function:
4052
4153
::
4254
43-
resource = named_resource("gpu_x_1")
55+
from torchx.specs import named_resources
56+
resource = named_resources["gpu_x_1"]
4457
4558
"""
46-
try:
47-
resource_fn = load("torchx.resources", name=resource.lower())
48-
except KeyError:
49-
raise ValueError(
50-
f"Resource: {resource} is not registered, check entrypoints.txt file."
51-
)
52-
return resource_fn()
59+
return named_resources[res]

0 commit comments

Comments
 (0)