Skip to content

Commit 2d5281e

Browse files
aivanoufacebook-github-bot
authored andcommitted
Add aws named resources (#225)
Summary: Pull Request resolved: #225 Add aws named resources Reviewed By: kiukchung Differential Revision: D31483254 fbshipit-source-id: 60ab95d6ae83cacfac42f0dc61026a544d704bb7
1 parent 4174c94 commit 2d5281e

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

docs/source/specs.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@ Resource
2626

2727
.. autofunction:: get_named_resources
2828

29+
AWS Named Resources
30+
^^^^^^^^^^^^^^^^^^^^^
31+
.. automodule:: torchx.specs.named_resources_aws
32+
33+
.. currentmodule:: torchx.specs.named_resources_aws
34+
2935
Macros
3036
------------
37+
.. currentmodule:: torchx.specs
3138
.. autoclass:: macros
3239
:members:
3340

torchx/specs/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from typing import Dict
99

10+
import torchx.specs.named_resources_aws as aws_resources
1011
from torchx.util.entrypoints import load_group
1112

1213
from .api import ( # noqa: F401 F403
@@ -48,6 +49,14 @@
4849
def _load_named_resources() -> Dict[str, Resource]:
4950
resource_methods = load_group("torchx.named_resources", default={})
5051
materialized_resources = {}
52+
default = {
53+
"aws_t3.medium": aws_resources.aws_t3_medium(),
54+
"aws_m5.2xlarge": aws_resources.aws_m5_2xlarge(),
55+
"aws_p3.2xlarge": aws_resources.aws_p3_2xlarge(),
56+
"aws_p3.8xlarge": aws_resources.aws_p3_8xlarge(),
57+
}
58+
for name, resource in default.items():
59+
materialized_resources[name] = resource
5160
for resource_name, resource_method in resource_methods.items():
5261
materialized_resources[resource_name] = resource_method()
5362
materialized_resources["NULL"] = NULL_RESOURCE

torchx/specs/named_resources_aws.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
r"""
8+
`torchx.specs.named_resources_aws` contains resource definitions that represent corresponding AWS instance types
9+
taken from https://aws.amazon.com/ec2/instance-types/. The resources are exposed
10+
via entrypoints after installing torchx lib. The mapping is stored in the `setup.py` file.
11+
12+
The named resources currently do not specify AWS instance type capabilities but merely represent
13+
the equvalent resource in mem, cpu and gpu numbers.
14+
15+
.. note::
16+
These resource definitions may change in future. It is expected for each user to
17+
manage their own resources. Follow https://pytorch.org/torchx/latest/specs.html#torchx.specs.get_named_resources
18+
to set up named resources.
19+
20+
Usage:
21+
22+
::
23+
24+
from torchx.specs import named_resources
25+
print(named_resources["aws_t3.medium"])
26+
print(named_resources["aws_m5.2xlarge"])
27+
print(named_resources["aws_p3.2xlarge"])
28+
print(named_resources["aws_p3.8xlarge"])
29+
30+
"""
31+
32+
from torchx.specs.api import Resource
33+
34+
GiB: int = 1024
35+
36+
37+
def aws_p3_2xlarge() -> Resource:
38+
return Resource(
39+
cpu=8,
40+
gpu=1,
41+
memMB=61 * GiB,
42+
capabilities={},
43+
)
44+
45+
46+
def aws_p3_8xlarge() -> Resource:
47+
return Resource(
48+
cpu=32,
49+
gpu=4,
50+
memMB=244 * GiB,
51+
capabilities={},
52+
)
53+
54+
55+
def aws_t3_medium() -> Resource:
56+
return Resource(
57+
cpu=2,
58+
gpu=0,
59+
memMB=4 * GiB,
60+
capabilities={},
61+
)
62+
63+
64+
def aws_m5_2xlarge() -> Resource:
65+
return Resource(
66+
cpu=8,
67+
gpu=0,
68+
memMB=32 * GiB,
69+
capabilities={},
70+
)

torchx/specs/test/api_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from typing import Dict, List, Optional, Tuple, Union
1212
from unittest.mock import MagicMock, patch
1313

14+
import torchx.specs.named_resources_aws as named_resources_aws
15+
from torchx.specs import named_resources
1416
from torchx.specs.api import (
1517
_TERMINAL_STATES,
1618
MISSING,
@@ -105,6 +107,20 @@ def test_copy_resource(self) -> None:
105107
self.assertEqual(new_resource.capabilities["new_key"], "new_value")
106108
self.assertEqual(resource.capabilities["test_key"], "test_value")
107109

110+
def test_named_resources(self) -> None:
111+
self.assertEqual(
112+
named_resources_aws.aws_m5_2xlarge(), named_resources["aws_m5.2xlarge"]
113+
)
114+
self.assertEqual(
115+
named_resources_aws.aws_t3_medium(), named_resources["aws_t3.medium"]
116+
)
117+
self.assertEqual(
118+
named_resources_aws.aws_p3_2xlarge(), named_resources["aws_p3.2xlarge"]
119+
)
120+
self.assertEqual(
121+
named_resources_aws.aws_p3_8xlarge(), named_resources["aws_p3.8xlarge"]
122+
)
123+
108124

109125
class RoleBuilderTest(unittest.TestCase):
110126
def test_defaults(self) -> None:
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
8+
import unittest
9+
10+
from torchx.specs.named_resources_aws import (
11+
aws_p3_2xlarge,
12+
aws_p3_8xlarge,
13+
aws_m5_2xlarge,
14+
aws_t3_medium,
15+
GiB,
16+
)
17+
18+
19+
class NamedResourcesTest(unittest.TestCase):
20+
def test_aws_p3_2xlarge(self) -> None:
21+
resource = aws_p3_2xlarge()
22+
self.assertEqual(8, resource.cpu)
23+
self.assertEqual(1, resource.gpu)
24+
self.assertEqual(61 * GiB, resource.memMB)
25+
26+
def test_aws_p3_8xlarge(self) -> None:
27+
resource = aws_p3_8xlarge()
28+
self.assertEqual(32, resource.cpu)
29+
self.assertEqual(4, resource.gpu)
30+
self.assertEqual(244 * GiB, resource.memMB)
31+
32+
def test_aws_m5_2xlarge(self) -> None:
33+
resource = aws_m5_2xlarge()
34+
self.assertEqual(8, resource.cpu)
35+
self.assertEqual(0, resource.gpu)
36+
self.assertEqual(32 * GiB, resource.memMB)
37+
38+
def test_aws_t3_medium(self) -> None:
39+
resource = aws_t3_medium()
40+
self.assertEqual(2, resource.cpu)
41+
self.assertEqual(0, resource.gpu)
42+
self.assertEqual(4 * GiB, resource.memMB)

0 commit comments

Comments
 (0)