66# LICENSE file in the root directory of this source tree.
77
88from .api import * # noqa: F401 F403
9- from torchx .util .entrypoints import load
9+ from torchx .util .entrypoints import load_group
1010
1111GiB : 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