File size: 12,175 Bytes
476455e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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.
"""Placeholder docstring"""
from __future__ import absolute_import

from abc import abstractmethod
from six import string_types

from sagemaker.inputs import FileSystemInput, TrainingInput
from sagemaker.local import file_input
from sagemaker.workflow import is_pipeline_variable


class _Job(object):
    """Handle creating, starting and waiting for Amazon SageMaker jobs to finish.

    This class shouldn't be directly instantiated.

    Subclasses must define a way to create, start and wait for an Amazon
    SageMaker job.
    """

    def __init__(self, sagemaker_session, job_name):
        """Placeholder docstring"""
        self.sagemaker_session = sagemaker_session
        self.job_name = job_name

    @abstractmethod
    def start_new(self, estimator, inputs):
        """Create a new Amazon SageMaker job from the estimator.

        Args:
            estimator (sagemaker.estimator.EstimatorBase): Estimator object
                created by the user.
            inputs (str): Parameters used when called
                :meth:`~sagemaker.estimator.EstimatorBase.fit`.

        Returns:
            sagemaker.job: Constructed object that captures all information
            about the started job.
        """

    @abstractmethod
    def wait(self):
        """Wait for the Amazon SageMaker job to finish."""

    @abstractmethod
    def describe(self):
        """Describe the job."""

    @abstractmethod
    def stop(self):
        """Stop the job."""

    @staticmethod
    def _load_config(inputs, estimator, expand_role=True, validate_uri=True):
        """Placeholder docstring"""
        input_config = _Job._format_inputs_to_input_config(inputs, validate_uri)
        role = (
            estimator.sagemaker_session.expand_role(estimator.role)
            if (expand_role and not is_pipeline_variable(estimator.role))
            else estimator.role
        )
        output_config = _Job._prepare_output_config(estimator.output_path, estimator.output_kms_key)
        resource_config = _Job._prepare_resource_config(
            estimator.instance_count,
            estimator.instance_type,
            estimator.instance_groups,
            estimator.volume_size,
            estimator.volume_kms_key,
            estimator.keep_alive_period_in_seconds,
        )
        stop_condition = _Job._prepare_stop_condition(estimator.max_run, estimator.max_wait)
        vpc_config = estimator.get_vpc_config()

        model_channel = _Job._prepare_channel(
            input_config,
            estimator.model_uri,
            estimator.model_channel_name,
            validate_uri,
            content_type="application/x-sagemaker-model",
            input_mode="File",
        )
        if model_channel:
            input_config = [] if input_config is None else input_config
            input_config.append(model_channel)

        if estimator.enable_network_isolation():
            code_channel = _Job._prepare_channel(
                input_config, estimator.code_uri, estimator.code_channel_name, validate_uri
            )

            if code_channel:
                input_config = [] if input_config is None else input_config
                input_config.append(code_channel)

        return {
            "input_config": input_config,
            "role": role,
            "output_config": output_config,
            "resource_config": resource_config,
            "stop_condition": stop_condition,
            "vpc_config": vpc_config,
        }

    @staticmethod
    def _format_inputs_to_input_config(inputs, validate_uri=True):
        """Placeholder docstring"""
        if inputs is None:
            return None

        # Deferred import due to circular dependency
        from sagemaker.amazon.amazon_estimator import RecordSet
        from sagemaker.amazon.amazon_estimator import FileSystemRecordSet

        if isinstance(inputs, (RecordSet, FileSystemRecordSet)):
            inputs = inputs.data_channel()

        input_dict = {}
        if isinstance(inputs, string_types):
            input_dict["training"] = _Job._format_string_uri_input(inputs, validate_uri)
        elif isinstance(inputs, TrainingInput):
            input_dict["training"] = inputs
        elif isinstance(inputs, file_input):
            input_dict["training"] = inputs
        elif isinstance(inputs, dict):
            for k, v in inputs.items():
                input_dict[k] = _Job._format_string_uri_input(v, validate_uri)
        elif isinstance(inputs, list):
            input_dict = _Job._format_record_set_list_input(inputs)
        elif isinstance(inputs, FileSystemInput):
            input_dict["training"] = inputs
        else:
            msg = (
                "Cannot format input {}. Expecting one of str, dict, TrainingInput or "
                "FileSystemInput"
            )
            raise ValueError(msg.format(inputs))

        channels = [
            _Job._convert_input_to_channel(name, input) for name, input in input_dict.items()
        ]

        return channels

    @staticmethod
    def _convert_input_to_channel(channel_name, channel_s3_input):
        """Placeholder docstring"""
        channel_config = channel_s3_input.config.copy()
        channel_config["ChannelName"] = channel_name
        return channel_config

    @staticmethod
    def _format_string_uri_input(
        uri_input,
        validate_uri=True,
        content_type=None,
        input_mode=None,
        compression=None,
        target_attribute_name=None,
    ):
        """Placeholder docstring"""
        s3_input_result = TrainingInput(
            uri_input,
            content_type=content_type,
            input_mode=input_mode,
            compression=compression,
            target_attribute_name=target_attribute_name,
        )
        if isinstance(uri_input, str) and validate_uri and uri_input.startswith("s3://"):
            return s3_input_result
        if isinstance(uri_input, str) and validate_uri and uri_input.startswith("file://"):
            return file_input(uri_input)
        if isinstance(uri_input, str) and validate_uri:
            raise ValueError(
                'URI input {} must be a valid S3 or FILE URI: must start with "s3://" or '
                '"file://"'.format(uri_input)
            )
        if isinstance(uri_input, str):
            return s3_input_result
        if isinstance(uri_input, (TrainingInput, file_input, FileSystemInput)):
            return uri_input
        if is_pipeline_variable(uri_input):
            return s3_input_result

        raise ValueError(
            "Cannot format input {}. Expecting one of str, TrainingInput, file_input or "
            "FileSystemInput".format(uri_input)
        )

    @staticmethod
    def _prepare_channel(
        input_config,
        channel_uri=None,
        channel_name=None,
        validate_uri=True,
        content_type=None,
        input_mode=None,
    ):
        """Placeholder docstring"""
        if not channel_uri:
            return None
        if not channel_name:
            raise ValueError(
                "Expected a channel name if a channel URI {} is specified".format(channel_uri)
            )

        if input_config:
            for existing_channel in input_config:
                if existing_channel["ChannelName"] == channel_name:
                    raise ValueError("Duplicate channel {} not allowed.".format(channel_name))

        channel_input = _Job._format_string_uri_input(
            channel_uri, validate_uri, content_type, input_mode
        )
        channel = _Job._convert_input_to_channel(channel_name, channel_input)

        return channel

    @staticmethod
    def _format_model_uri_input(model_uri, validate_uri=True):
        """Placeholder docstring"""
        if isinstance(model_uri, string_types) and validate_uri and model_uri.startswith("s3://"):
            return TrainingInput(
                model_uri,
                input_mode="File",
                distribution="FullyReplicated",
                content_type="application/x-sagemaker-model",
            )
        if isinstance(model_uri, string_types) and validate_uri and model_uri.startswith("file://"):
            return file_input(model_uri)
        if isinstance(model_uri, string_types) and validate_uri:
            raise ValueError(
                'Model URI must be a valid S3 or FILE URI: must start with "s3://" or ' '"file://'
            )
        if isinstance(model_uri, string_types):
            return TrainingInput(
                model_uri,
                input_mode="File",
                distribution="FullyReplicated",
                content_type="application/x-sagemaker-model",
            )
        raise ValueError("Cannot format model URI {}. Expecting str".format(model_uri))

    @staticmethod
    def _format_record_set_list_input(inputs):
        """Placeholder docstring"""
        # Deferred import due to circular dependency
        from sagemaker.amazon.amazon_estimator import FileSystemRecordSet, RecordSet

        input_dict = {}
        for record in inputs:
            if not isinstance(record, (RecordSet, FileSystemRecordSet)):
                raise ValueError("List compatible only with RecordSets or FileSystemRecordSets.")

            if record.channel in input_dict:
                raise ValueError("Duplicate channels not allowed.")
            if isinstance(record, RecordSet):
                input_dict[record.channel] = record.records_s3_input()
            if isinstance(record, FileSystemRecordSet):
                input_dict[record.channel] = record.file_system_input

        return input_dict

    @staticmethod
    def _prepare_output_config(s3_path, kms_key_id):
        """Placeholder docstring"""
        config = {"S3OutputPath": s3_path}
        if kms_key_id is not None:
            config["KmsKeyId"] = kms_key_id
        return config

    @staticmethod
    def _prepare_resource_config(
        instance_count,
        instance_type,
        instance_groups,
        volume_size,
        volume_kms_key,
        keep_alive_period_in_seconds,
    ):
        """Placeholder docstring"""
        resource_config = {
            "VolumeSizeInGB": volume_size,
        }
        if volume_kms_key is not None:
            resource_config["VolumeKmsKeyId"] = volume_kms_key
        if keep_alive_period_in_seconds is not None:
            resource_config["KeepAlivePeriodInSeconds"] = keep_alive_period_in_seconds
        if instance_groups is not None:
            if instance_count is not None or instance_type is not None:
                raise ValueError(
                    "instance_count and instance_type cannot be set when instance_groups is set"
                )

            resource_config["InstanceGroups"] = [
                group._to_request_dict() for group in instance_groups
            ]
        else:
            if instance_count is None or instance_type is None:
                raise ValueError(
                    "instance_count and instance_type must be set if instance_groups is not set"
                )
            resource_config["InstanceCount"] = instance_count
            resource_config["InstanceType"] = instance_type

        return resource_config

    @staticmethod
    def _prepare_stop_condition(max_run, max_wait):
        """Placeholder docstring"""
        if max_wait:
            return {"MaxRuntimeInSeconds": max_run, "MaxWaitTimeInSeconds": max_wait}
        return {"MaxRuntimeInSeconds": max_run}

    @property
    def name(self):
        """Placeholder docstring"""
        return self.job_name