1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
# pylint: disable=protected-access
from typing import Dict, List, Optional, Union
from marshmallow import INCLUDE, Schema
from ... import MpiDistribution, PyTorchDistribution, RayDistribution, TensorFlowDistribution
from ..._schema import PathAwareSchema
from ..._schema.core.fields import DistributionField
from ...entities import CommandJobLimits, JobResourceConfiguration
from ...entities._util import get_rest_dict_for_node_attrs
from .._schema.component import NodeType
from ..entities.component import InternalComponent
from ..entities.node import InternalBaseNode
class Command(InternalBaseNode):
"""Node of internal command components in pipeline with specific run settings.
Different from azure.ai.ml.entities.Command, type of this class is CommandComponent.
"""
def __init__(self, **kwargs):
node_type = kwargs.pop("type", None) or NodeType.COMMAND
super(Command, self).__init__(type=node_type, **kwargs)
self._init = True
self._resources = kwargs.pop("resources", JobResourceConfiguration())
self._compute = kwargs.pop("compute", None)
self._environment = kwargs.pop("environment", None)
self._environment_variables = kwargs.pop("environment_variables", None)
self._limits = kwargs.pop("limits", CommandJobLimits())
self._init = False
@property
def compute(self) -> Optional[str]:
"""Get the compute definition for the command.
:return: The compute definition
:rtype: Optional[str]
"""
return self._compute
@compute.setter
def compute(self, value: str) -> None:
"""Set the compute definition for the command.
:param value: The new compute definition
:type value: str
"""
self._compute = value
@property
def environment(self) -> Optional[str]:
"""Get the environment definition for the command.
:return: The environment definition
:rtype: Optional[str]
"""
return self._environment
@environment.setter
def environment(self, value: str) -> None:
"""Set the environment definition for the command.
:param value: The new environment definition
:type value: str
"""
self._environment = value
@property
def environment_variables(self) -> Optional[Dict[str, str]]:
"""Get the environment variables for the command.
:return: The environment variables
:rtype: Optional[Dict[str, str]]
"""
return self._environment_variables
@environment_variables.setter
def environment_variables(self, value: Dict[str, str]) -> None:
"""Set the environment variables for the command.
:param value: The new environment variables
:type value: Dict[str, str]
"""
self._environment_variables = value
@property
def limits(self) -> CommandJobLimits:
return self._limits
@limits.setter
def limits(self, value: CommandJobLimits):
self._limits = value
@property
def resources(self) -> JobResourceConfiguration:
"""Compute Resource configuration for the component.
:return: The resource configuration
:rtype: JobResourceConfiguration
"""
return self._resources
@resources.setter
def resources(self, value: JobResourceConfiguration):
self._resources = value
@classmethod
def _picked_fields_from_dict_to_rest_object(cls) -> List[str]:
return ["environment", "limits", "resources", "environment_variables"]
@classmethod
def _create_schema_for_validation(cls, context) -> Union[PathAwareSchema, Schema]:
from .._schema.command import CommandSchema
return CommandSchema(context=context)
def _to_rest_object(self, **kwargs) -> dict:
rest_obj = super()._to_rest_object(**kwargs)
rest_obj.update(
{
"limits": get_rest_dict_for_node_attrs(self.limits, clear_empty_value=True),
"resources": get_rest_dict_for_node_attrs(self.resources, clear_empty_value=True),
}
)
return rest_obj
@classmethod
def _from_rest_object_to_init_params(cls, obj):
obj = InternalBaseNode._from_rest_object_to_init_params(obj)
if "resources" in obj and obj["resources"]:
obj["resources"] = JobResourceConfiguration._from_rest_object(obj["resources"])
# handle limits
if "limits" in obj and obj["limits"]:
obj["limits"] = CommandJobLimits._from_rest_object(obj["limits"])
return obj
class Distributed(Command):
def __init__(self, **kwargs):
super(Distributed, self).__init__(**kwargs)
self._distribution = kwargs.pop("distribution", None)
self._type = NodeType.DISTRIBUTED
if self._distribution is None:
# hack: distribution.type is required to set distribution, which is defined in launcher.type
if (
isinstance(self.component, InternalComponent)
and self.component.launcher
and "type" in self.component.launcher
):
self.distribution = {"type": self.component.launcher["type"]}
else:
raise ValueError(
"launcher.type must be specified in definition of DistributedComponent but got {}".format(
self.component
)
)
@property
def distribution(
self,
) -> Union[PyTorchDistribution, MpiDistribution, TensorFlowDistribution, RayDistribution]:
"""The distribution config of component, e.g. distribution={'type': 'mpi'}.
:return: The distribution config
:rtype: Union[PyTorchDistribution, MpiDistribution, TensorFlowDistribution, RayDistribution]
"""
return self._distribution
@distribution.setter
def distribution(
self,
value: Union[Dict, PyTorchDistribution, TensorFlowDistribution, MpiDistribution, RayDistribution],
):
if isinstance(value, dict):
dist_schema = DistributionField(unknown=INCLUDE)
value = dist_schema._deserialize(value=value, attr=None, data=None)
self._distribution = value
@classmethod
def _create_schema_for_validation(cls, context) -> Union[PathAwareSchema, Schema]:
from .._schema.command import DistributedSchema
return DistributedSchema(context=context)
@classmethod
def _picked_fields_from_dict_to_rest_object(cls) -> List[str]:
return Command._picked_fields_from_dict_to_rest_object() + ["distribution"]
def _to_rest_object(self, **kwargs) -> dict:
rest_obj = super()._to_rest_object(**kwargs)
distribution = self.distribution._to_rest_object() if self.distribution else None # pylint: disable=no-member
rest_obj.update(
{
"distribution": get_rest_dict_for_node_attrs(distribution),
}
)
return rest_obj
|