-
Notifications
You must be signed in to change notification settings - Fork 316
/
worker_spec_utils.py
270 lines (229 loc) · 9.34 KB
/
worker_spec_utils.py
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# -*- coding: utf-8 -*-
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from typing import NamedTuple, Optional, Dict, Union, List
from google.cloud.aiplatform import utils
from google.cloud.aiplatform.compat.types import (
accelerator_type as gca_accelerator_type_compat,
)
REDUCTION_SERVER_CONTAINER_URI = (
"us-docker.pkg.dev/vertex-ai-restricted/training/reductionserver:latest"
)
class _WorkerPoolSpec(NamedTuple):
"""Specification container for Worker Pool specs used for distributed training.
Usage:
spec = _WorkerPoolSpec(
replica_count=10,
machine_type='n1-standard-4',
accelerator_count=2,
accelerator_type='NVIDIA_TESLA_K80',
boot_disk_type='pd-ssd',
boot_disk_size_gb=100,
)
Note that container and python package specs are not stored with this spec.
"""
replica_count: int = 0
machine_type: str = "n1-standard-4"
accelerator_count: int = 0
accelerator_type: str = "ACCELERATOR_TYPE_UNSPECIFIED"
boot_disk_type: str = "pd-ssd"
boot_disk_size_gb: int = 100
container_uri: str = None
def _get_accelerator_type(self) -> Optional[str]:
"""Validates accelerator_type and returns the name of the accelerator.
Returns:
None if no accelerator or valid accelerator name.
Raise:
ValueError if accelerator type is invalid.
"""
# Raises ValueError if invalid accelerator_type
utils.validate_accelerator_type(self.accelerator_type)
accelerator_enum = getattr(
gca_accelerator_type_compat.AcceleratorType, self.accelerator_type
)
if (
accelerator_enum
!= gca_accelerator_type_compat.AcceleratorType.ACCELERATOR_TYPE_UNSPECIFIED
):
return self.accelerator_type
@property
def spec_dict(self) -> Dict[str, Union[int, str, Dict[str, Union[int, str]]]]:
"""Return specification as a Dict."""
spec = {
"machine_spec": {"machine_type": self.machine_type},
"replica_count": self.replica_count,
"disk_spec": {
"boot_disk_type": self.boot_disk_type,
"boot_disk_size_gb": self.boot_disk_size_gb,
},
}
accelerator_type = self._get_accelerator_type()
if accelerator_type and self.accelerator_count:
spec["machine_spec"]["accelerator_type"] = accelerator_type
spec["machine_spec"]["accelerator_count"] = self.accelerator_count
if self.container_uri == REDUCTION_SERVER_CONTAINER_URI:
spec["container_spec"] = {
"image_uri": self.container_uri,
}
return spec
@property
def is_empty(self) -> bool:
"""Returns True is replica_count > 0 False otherwise."""
return self.replica_count <= 0
class _DistributedTrainingSpec(NamedTuple):
"""Configuration for distributed training worker pool specs.
Vertex AI Training expects configuration in this order:
[
chief spec, # can only have one replica
worker spec,
parameter server spec,
evaluator spec
]
Usage:
dist_training_spec = _DistributedTrainingSpec(
chief_spec = _WorkerPoolSpec(
replica_count=1,
machine_type='n1-standard-4',
accelerator_count=2,
accelerator_type='NVIDIA_TESLA_K80',
boot_disk_type='pd-ssd',
boot_disk_size_gb=100,
),
worker_spec = _WorkerPoolSpec(
replica_count=10,
machine_type='n1-standard-4',
accelerator_count=2,
accelerator_type='NVIDIA_TESLA_K80',
boot_disk_type='pd-ssd',
boot_disk_size_gb=100,
),
)
"""
chief_spec: _WorkerPoolSpec = _WorkerPoolSpec()
worker_spec: _WorkerPoolSpec = _WorkerPoolSpec()
server_spec: _WorkerPoolSpec = _WorkerPoolSpec()
evaluator_spec: _WorkerPoolSpec = _WorkerPoolSpec()
@property
def pool_specs(
self,
) -> List[Dict[str, Union[int, str, Dict[str, Union[int, str]]]]]:
"""Return each pools spec in correct order for Vertex AI as a list of
dicts.
Also removes specs if they are empty but leaves specs in if there unusual
specifications to not break the ordering in Vertex AI Training.
ie. 0 chief replica, 10 worker replica, 3 ps replica
Returns:
Order list of worker pool specs suitable for Vertex AI Training.
"""
if self.chief_spec.replica_count > 1:
raise ValueError("Chief spec replica count cannot be greater than 1.")
spec_order = [
self.chief_spec,
self.worker_spec,
self.server_spec,
self.evaluator_spec,
]
specs = [s.spec_dict for s in spec_order]
last_non_empty_seen = False
for i in reversed(range(len(spec_order))):
if spec_order[i].is_empty:
if not last_non_empty_seen:
specs.pop()
else:
specs[i] = {}
else:
last_non_empty_seen = True
return specs
def reduction_server_pool(
self,
reduction_server_replica_count: int = 0,
reduction_server_machine_type: str = "n1-highcpu-16",
):
"""Parameterizes Config to support only reduction server replicas.
Args:
reduction_server_replica_count (int):
The number of reduction server replicas, default is 0.
reduction_server_machine_type (str):
The type of machine to use for reduction server, default is `n1-highcpu-16`.
Raise:
ValueError if chief worker is not set
"""
reduction_server_spec = _WorkerPoolSpec(
replica_count=reduction_server_replica_count,
machine_type=reduction_server_machine_type,
container_uri=REDUCTION_SERVER_CONTAINER_URI,
)
if self.chief_spec.is_empty:
raise ValueError("reduction server can not be set without chief worker.")
return _DistributedTrainingSpec(
chief_spec=self.chief_spec,
worker_spec=self.worker_spec,
server_spec=reduction_server_spec,
)
@classmethod
def chief_worker_pool(
cls,
replica_count: int = 0,
machine_type: str = "n1-standard-4",
accelerator_count: int = 0,
accelerator_type: str = "ACCELERATOR_TYPE_UNSPECIFIED",
boot_disk_type: str = "pd-ssd",
boot_disk_size_gb: int = 100,
) -> "_DistributedTrainingSpec":
"""Parameterizes Config to support only chief with worker replicas.
For replica is assigned to chief and the remainder to workers. All spec have the
same machine type, accelerator count, and accelerator type.
Args:
replica_count (int):
The number of worker replicas. Assigns 1 chief replica and
replica_count - 1 worker replicas.
machine_type (str):
The type of machine to use for training.
accelerator_type (str):
Hardware accelerator type. One of ACCELERATOR_TYPE_UNSPECIFIED,
NVIDIA_TESLA_K80, NVIDIA_TESLA_P100, NVIDIA_TESLA_V100, NVIDIA_TESLA_P4,
NVIDIA_TESLA_T4
accelerator_count (int):
The number of accelerators to attach to a worker replica.
boot_disk_type (str):
Type of the boot disk (default is `pd-ssd`).
Valid values: `pd-ssd` (Persistent Disk Solid State Drive) or
`pd-standard` (Persistent Disk Hard Disk Drive).
boot_disk_size_gb (int):
Size in GB of the boot disk (default is 100GB).
boot disk size must be within the range of [100, 64000].
Returns:
_DistributedTrainingSpec representing one chief and n workers all of same
type. If replica_count <= 0 then an empty spec is returned.
"""
if replica_count <= 0:
return cls()
chief_spec = _WorkerPoolSpec(
replica_count=1,
machine_type=machine_type,
accelerator_count=accelerator_count,
accelerator_type=accelerator_type,
boot_disk_type=boot_disk_type,
boot_disk_size_gb=boot_disk_size_gb,
)
worker_spec = _WorkerPoolSpec(
replica_count=replica_count - 1,
machine_type=machine_type,
accelerator_count=accelerator_count,
accelerator_type=accelerator_type,
boot_disk_type=boot_disk_type,
boot_disk_size_gb=boot_disk_size_gb,
)
return cls(chief_spec=chief_spec, worker_spec=worker_spec)