Skip to content

Commit 3855ae4

Browse files
authored
Standalone Workspace build for TorchX
Differential Revision: D63145971 Pull Request resolved: #965
1 parent fda6a86 commit 3855ae4

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

torchx/runner/api.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import warnings
1515
from datetime import datetime
1616
from types import TracebackType
17-
from typing import Any, Dict, Iterable, List, Mapping, Optional, Tuple, Type
17+
from typing import Any, Dict, Iterable, List, Mapping, Optional, Tuple, Type, TypeVar
1818

1919
from torchx.runner.events import log_event
2020
from torchx.schedulers import get_scheduler_factories, SchedulerFactory
@@ -41,14 +41,16 @@
4141
)
4242

4343
from torchx.util.types import none_throws
44-
from torchx.workspace.api import WorkspaceMixin
44+
from torchx.workspace.api import PkgInfo, WorkspaceBuilder, WorkspaceMixin
4545

4646
from .config import get_config, get_configs
4747

4848
logger: logging.Logger = logging.getLogger(__name__)
4949

5050

5151
NONE: str = "<NONE>"
52+
S = TypeVar("S")
53+
T = TypeVar("T")
5254

5355

5456
def get_configured_trackers() -> Dict[str, Optional[str]]:
@@ -147,6 +149,18 @@ def close(self) -> None:
147149
for scheduler in self._scheduler_instances.values():
148150
scheduler.close()
149151

152+
def build_standalone_workspace(
153+
self,
154+
workspace_builder: WorkspaceBuilder[S, T],
155+
sync: bool = True,
156+
) -> PkgInfo[S]:
157+
"""
158+
Build a standalone workspace for the given role.
159+
This method is used to build a workspace for a role independent of the scheduler and
160+
also enables asynchronous workspace building using the Role overrides.
161+
"""
162+
return workspace_builder.build_workspace(sync)
163+
150164
def run_component(
151165
self,
152166
component: str,

torchx/specs/api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import copy
1212
import inspect
1313
import json
14+
import logging as logger
1415
import re
1516
import typing
1617
from dataclasses import asdict, dataclass, field
@@ -189,7 +190,16 @@ def apply(self, role: "Role") -> "Role":
189190
apply applies the values to a copy the specified role and returns it.
190191
"""
191192

193+
# Overrides might contain future values which can't be serialized so taken out for the copy
194+
overrides = role.overrides
195+
if len(overrides) > 0:
196+
logger.warning(
197+
"Role overrides are not supported for macros. Overrides will not be copied"
198+
)
199+
role.overrides = {}
192200
role = copy.deepcopy(role)
201+
role.overrides = overrides
202+
193203
role.args = [self.substitute(arg) for arg in role.args]
194204
role.env = {key: self.substitute(arg) for key, arg in role.env.items()}
195205
role.metadata = self._apply_nested(role.metadata)

torchx/workspace/api.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import abc
1010
import fnmatch
1111
import posixpath
12-
from typing import Generic, Iterable, Mapping, Tuple, TYPE_CHECKING, TypeVar
12+
from dataclasses import dataclass
13+
from typing import Any, Dict, Generic, Iterable, Mapping, Tuple, TYPE_CHECKING, TypeVar
1314

1415
from torchx.specs import AppDef, CfgVal, Role, runopts
1516

@@ -20,6 +21,36 @@
2021

2122
T = TypeVar("T")
2223

24+
PackageType = TypeVar("PackageType")
25+
WorkspaceConfigType = TypeVar("WorkspaceConfigType")
26+
27+
28+
@dataclass
29+
class PkgInfo(Generic[PackageType]):
30+
"""
31+
Convenience class used to specify information regarding the built workspace
32+
"""
33+
34+
img: str
35+
lazy_overrides: Dict[str, Any]
36+
metadata: PackageType
37+
38+
39+
@dataclass
40+
class WorkspaceBuilder(Generic[PackageType, WorkspaceConfigType]):
41+
cfg: WorkspaceConfigType
42+
43+
@abc.abstractmethod
44+
def build_workspace(self, sync: bool = True) -> PkgInfo[PackageType]:
45+
"""
46+
Builds the specified ``workspace`` with respect to ``img``.
47+
In the simplest case, this method builds a new image.
48+
Certain (more efficient) implementations build
49+
incremental diff patches that overlay on top of the role's image.
50+
51+
"""
52+
pass
53+
2354

2455
class WorkspaceMixin(abc.ABC, Generic[T]):
2556
"""

0 commit comments

Comments
 (0)