-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathconfigs.py
More file actions
350 lines (293 loc) · 14.6 KB
/
configs.py
File metadata and controls
350 lines (293 loc) · 14.6 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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.
"""This module provides the configuration classes used in ``sagemaker.modules``.
Some of these classes are re-exported from ``sagemaker.core.shapes``. For convinence,
users can import these classes directly from ``sagemaker.modules.configs``.
For more documentation on ``sagemaker.core.shapes``, see:
- https://sagemaker-core.readthedocs.io/en/stable/#sagemaker-core-shapes
"""
from __future__ import absolute_import
from typing import Optional, Union, List
from pydantic import BaseModel, model_validator, ConfigDict
import sagemaker.core.shapes as shapes
from sagemaker.core.helper.pipeline_variable import StrPipeVar, IntPipeVar, BoolPipeVar
# TODO: Can we add custom logic to some of these to set better defaults?
from sagemaker.core.shapes import (
StoppingCondition,
RetryStrategy,
Channel,
ShuffleConfig,
DataSource,
S3DataSource,
FileSystemDataSource,
TrainingImageConfig,
TrainingRepositoryAuthConfig,
Tag,
InfraCheckConfig,
RemoteDebugConfig,
SessionChainingConfig,
InstanceGroup,
HubAccessConfig,
ModelAccessConfig,
MetricDefinition,
DatasetSource,
)
from sagemaker.core.training.utils import convert_unassigned_to_none
__all__ = [
"BaseConfig",
"SourceCode",
"StoppingCondition",
"RetryStrategy",
"OutputDataConfig",
"Channel",
"ShuffleConfig",
"DataSource",
"S3DataSource",
"FileSystemDataSource",
"TrainingImageConfig",
"TrainingRepositoryAuthConfig",
"Tag",
"InfraCheckConfig",
"RemoteDebugConfig",
"SessionChainingConfig",
"InstanceGroup",
"TensorBoardOutputConfig",
"CheckpointConfig",
"HubAccessConfig",
"ModelAccessConfig",
"Compute",
"Networking",
"InputData",
"MetricDefinition",
"DatasetSource",
]
class BaseConfig(BaseModel):
"""BaseConfig"""
model_config = ConfigDict(validate_assignment=True, extra="forbid")
class SourceCode(BaseConfig):
"""SourceCode.
The SourceCode class allows the user to specify the source code location, dependencies,
entry script, or commands to be executed in the training job container.
Parameters:
source_dir (Optional[StrPipeVar]):
The local directory, s3 uri, or path to tar.gz file stored locally or in s3 that
contains the source code to be used in the training job container.
requirements (Optional[StrPipeVar]):
The path within ``source_dir`` to a ``requirements.txt`` file. If specified, the listed
requirements will be installed in the training job container.
entry_script (Optional[StrPipeVar]):
The path within ``source_dir`` to the entry script that will be executed in the training
job container. If not specified, command must be provided.
command (Optional[StrPipeVar]):
The command(s) to execute in the training job container. Example: "python my_script.py".
If not specified, entry_script must be provided.
ignore_patterns: (Optional[List[str]]) :
The ignore patterns to ignore specific files/folders when uploading to S3. If not specified,
default to: ['.env', '.git', '__pycache__', '.DS_Store', '.cache', '.ipynb_checkpoints'].
dependencies (Optional[List[str]]):
A list of paths to local directories (absolute or relative) containing additional
libraries that will be copied into the training container and added to PYTHONPATH.
Each path must be a valid local directory or file.
"""
source_dir: Optional[StrPipeVar] = None
requirements: Optional[StrPipeVar] = None
entry_script: Optional[StrPipeVar] = None
command: Optional[StrPipeVar] = None
ignore_patterns: Optional[List[str]] = [
".env",
".git",
"__pycache__",
".DS_Store",
".cache",
".ipynb_checkpoints",
]
dependencies: Optional[List[str]] = None
class OutputDataConfig(shapes.OutputDataConfig):
"""OutputDataConfig.
Provides the configuration for the output data location of the training job
(will not be carried over to any model repository or deployment).
Parameters:
s3_output_path (Optional[StrPipeVar]):
The S3 URI where the output data will be stored. This is the location where the
training job will save its output data, such as model artifacts and logs.
kms_key_id (Optional[StrPipeVar]):
The Amazon Web Services Key Management Service (Amazon Web Services KMS) key that
SageMaker uses to encrypt the model artifacts at rest using Amazon S3 server-side
encryption.
compression_type (Optional[StrPipeVar]):
The model output compression type. Select None to output an uncompressed model,
recommended for large model outputs. Defaults to gzip.
"""
s3_output_path: Optional[StrPipeVar] = None
kms_key_id: Optional[StrPipeVar] = None
compression_type: Optional[StrPipeVar] = None
class Compute(shapes.ResourceConfig):
"""Compute.
The Compute class is a subclass of ``sagemaker.core.shapes.ResourceConfig``
and allows the user to specify the compute resources for the training job.
Parameters:
instance_type (Optional[StrPipeVar]):
The ML compute instance type. For information about available instance types,
see https://aws.amazon.com/sagemaker/pricing/.
instance_count (Optional[IntPipeVar]): The number of ML compute instances to use. For distributed
training, provide a value greater than 1.
volume_size_in_gb (Optional[IntPipeVar]):
The size of the ML storage volume that you want to provision. ML storage volumes store
model artifacts and incremental states. Training algorithms might also use the ML
storage volume for scratch space. Default: 30
volume_kms_key_id (Optional[StrPipeVar]):
The Amazon Web Services KMS key that SageMaker uses to encrypt data on the storage
volume attached to the ML compute instance(s) that run the training job.
keep_alive_period_in_seconds (Optional[IntPipeVar]):
The duration of time in seconds to retain configured resources in a warm pool for
subsequent training jobs.
instance_groups (Optional[List[InstanceGroup]]):
A list of instance groups for heterogeneous clusters to be used in the training job.
training_plan_arn (Optional[StrPipeVar]):
The Amazon Resource Name (ARN) of the training plan to use for this resource configuration.
enable_managed_spot_training (Optional[BoolPipeVar]):
To train models using managed spot training, choose True. Managed spot training
provides a fully managed and scalable infrastructure for training machine learning
models. this option is useful when training jobs can be interrupted and when there
is flexibility when the training job is run.
"""
volume_size_in_gb: Optional[int] = 30
enable_managed_spot_training: Optional[bool] = None
@model_validator(mode="after")
def _model_validator(self) -> "Compute":
"""Convert Unassigned values to None."""
return convert_unassigned_to_none(self)
def _to_resource_config(self) -> shapes.ResourceConfig:
"""Convert to a sagemaker.core.shapes.ResourceConfig object."""
compute_config_dict = self.model_dump()
resource_config_fields = set(shapes.ResourceConfig.__annotations__.keys())
filtered_dict = {
k: v
for k, v in compute_config_dict.items()
if k in resource_config_fields and v is not None
}
if not filtered_dict:
return None
return shapes.ResourceConfig(**filtered_dict)
class Networking(shapes.VpcConfig):
"""Networking.
The Networking class is a subclass of ``sagemaker.core.shapes.VpcConfig`` and
allows the user to specify the networking configuration for the training job.
Parameters:
security_group_ids (Optional[List[StrPipeVar]]):
The VPC security group IDs, in the form sg-xxxxxxxx. Specify the
security groups for the VPC that is specified in the Subnets field.
subnets (Optional[List[StrPipeVar]]):
The ID of the subnets in the VPC to which you want to connect your
training job or model.
enable_network_isolation (Optional[bool]):
Isolates the training container. No inbound or outbound network calls can be made,
except for calls between peers within a training cluster for distributed training.
If you enable network isolation for training jobs that are configured to use a VPC,
SageMaker downloads and uploads customer data and model artifacts through the
specified VPC, but the training container does not have network access.
enable_inter_container_traffic_encryption (Optional[bool]):
To encrypt all communications between ML compute instances in distributed training
choose True. Encryption provides greater security for distributed training, but
training might take longer. How long it takes depends on the amount of
communication between compute instances, especially if you use a deep learning
algorithm in distributed training.
"""
security_group_ids: Optional[list[StrPipeVar]] = None
subnets: Optional[list[StrPipeVar]] = None
enable_network_isolation: Optional[bool] = None
enable_inter_container_traffic_encryption: Optional[bool] = None
@model_validator(mode="after")
def _model_validator(self) -> "Networking":
"""Convert Unassigned values to None."""
return convert_unassigned_to_none(self)
def _to_vpc_config(self) -> shapes.VpcConfig:
"""Convert to a sagemaker.core.shapes.VpcConfig object."""
compute_config_dict = self.model_dump()
vpc_config_fields = set(shapes.VpcConfig.__annotations__.keys())
filtered_dict = {
k: v for k, v in compute_config_dict.items() if k in vpc_config_fields and v is not None
}
if not filtered_dict:
return None
return shapes.VpcConfig(**filtered_dict)
class InputData(BaseConfig):
"""InputData.
This config allows the user to specify an input data source for the training job.
Will be found at ``/opt/ml/input/data/<channel_name>`` within the training container.
For convience, can be referenced inside the training container like:
.. code:: python
import os
input_data_dir = os.environ['SM_CHANNEL_<channel_name>']
Parameters:
channel_name (StrPipeVar):
The name of the input data source channel.
data_source (Union[StrPipeVar, S3DataSource, FileSystemDataSource, DatasetSource]):
The data source for the channel. Can be an S3 URI string, local file path string,
S3DataSource object, FileSystemDataSource object, DatasetSource object, or a
pipeline variable (Properties) from a previous step.
content_type (StrPipeVar):
The MIME type of the data.
"""
channel_name: StrPipeVar = None
data_source: Union[StrPipeVar, FileSystemDataSource, S3DataSource, DatasetSource] = None
content_type: StrPipeVar = None
class OutputDataConfig(shapes.OutputDataConfig):
"""OutputDataConfig.
The OutputDataConfig class is a subclass of ``sagemaker.core.shapes.OutputDataConfig``
and allows the user to specify the output data configuration for the training job
(will not be carried over to any model repository or deployment).
Parameters:
s3_output_path (Optional[StrPipeVar]):
The S3 URI where the output data will be stored. This is the location where the
training job will save its output data, such as model artifacts and logs.
kms_key_id (Optional[StrPipeVar]):
The Amazon Web Services Key Management Service (Amazon Web Services KMS) key that
SageMaker uses to encrypt the model artifacts at rest using Amazon S3 server-side
encryption.
compression_type (Optional[StrPipeVar]):
The model output compression type. Select `NONE` to output an uncompressed model,
recommended for large model outputs. Defaults to `GZIP`.
"""
s3_output_path: Optional[StrPipeVar] = None
kms_key_id: Optional[StrPipeVar] = None
compression_type: Optional[StrPipeVar] = None
class TensorBoardOutputConfig(shapes.TensorBoardOutputConfig):
"""TensorBoardOutputConfig.
The TensorBoardOutputConfig class is a subclass of ``sagemaker.core.shapes.TensorBoardOutputConfig``
and allows the user to specify the storage locations for the Amazon SageMaker
Debugger TensorBoard.
Parameters:
s3_output_path (Optional[StrPipeVar]):
Path to Amazon S3 storage location for TensorBoard output. If not specified, will
default to
``s3://<default_bucket>/<default_prefix>/<base_job_name>/<job_name>/tensorboard-output``
local_path (Optional[StrPipeVar]):
Path to local storage location for tensorBoard output. Defaults to /opt/ml/output/tensorboard.
"""
s3_output_path: Optional[StrPipeVar] = None
local_path: Optional[StrPipeVar] = "/opt/ml/output/tensorboard"
class CheckpointConfig(shapes.CheckpointConfig):
"""CheckpointConfig.
The CheckpointConfig class is a subclass of ``sagemaker.core.shapes.CheckpointConfig``
and allows the user to specify the checkpoint configuration for the training job.
Parameters:
s3_uri (Optional[StrPipeVar]):
Path to Amazon S3 storage location for the Checkpoint data. If not specified, will
default to
``s3://<default_bucket>/<default_prefix>/<base_job_name>/<job_name>/checkpoints``
local_path (Optional[StrPipeVar]):
The local directory where checkpoints are written. The default directory is /opt/ml/checkpoints.
"""
s3_uri: Optional[StrPipeVar] = None
local_path: Optional[StrPipeVar] = "/opt/ml/checkpoints"