|
| 1 | +# Copyright 2017-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Amazon SageMaker channel configurations for S3 data sources and file system data sources""" |
| 14 | +from __future__ import absolute_import, print_function |
| 15 | + |
| 16 | +FILE_SYSTEM_TYPES = ["FSxLustre", "EFS"] |
| 17 | +FILE_SYSTEM_ACCESS_MODES = ["ro", "rw"] |
| 18 | + |
| 19 | + |
| 20 | +class s3_input(object): |
| 21 | + """Amazon SageMaker channel configurations for S3 data sources. |
| 22 | +
|
| 23 | + Attributes: |
| 24 | + config (dict[str, dict]): A SageMaker ``DataSource`` referencing |
| 25 | + a SageMaker ``S3DataSource``. |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__( |
| 29 | + self, |
| 30 | + s3_data, |
| 31 | + distribution="FullyReplicated", |
| 32 | + compression=None, |
| 33 | + content_type=None, |
| 34 | + record_wrapping=None, |
| 35 | + s3_data_type="S3Prefix", |
| 36 | + input_mode=None, |
| 37 | + attribute_names=None, |
| 38 | + shuffle_config=None, |
| 39 | + ): |
| 40 | + """Create a definition for input data used by an SageMaker training job. |
| 41 | + See AWS documentation on the ``CreateTrainingJob`` API for more details on the parameters. |
| 42 | +
|
| 43 | + Args: |
| 44 | + s3_data (str): Defines the location of s3 data to train on. |
| 45 | + distribution (str): Valid values: 'FullyReplicated', 'ShardedByS3Key' |
| 46 | + (default: 'FullyReplicated'). |
| 47 | + compression (str): Valid values: 'Gzip', None (default: None). This is used only in |
| 48 | + Pipe input mode. |
| 49 | + content_type (str): MIME type of the input data (default: None). |
| 50 | + record_wrapping (str): Valid values: 'RecordIO' (default: None). |
| 51 | + s3_data_type (str): Valid values: 'S3Prefix', 'ManifestFile', 'AugmentedManifestFile'. |
| 52 | + If 'S3Prefix', ``s3_data`` defines a prefix of s3 objects to train on. |
| 53 | + All objects with s3 keys beginning with ``s3_data`` will be used to train. |
| 54 | + If 'ManifestFile' or 'AugmentedManifestFile', then ``s3_data`` defines a |
| 55 | + single S3 manifest file or augmented manifest file (respectively), |
| 56 | + listing the S3 data to train on. Both the ManifestFile and |
| 57 | + AugmentedManifestFile formats are described in the SageMaker API documentation: |
| 58 | + https://docs.aws.amazon.com/sagemaker/latest/dg/API_S3DataSource.html |
| 59 | + input_mode (str): Optional override for this channel's input mode (default: None). |
| 60 | + By default, channels will use the input mode defined on |
| 61 | + ``sagemaker.estimator.EstimatorBase.input_mode``, but they will ignore |
| 62 | + that setting if this parameter is set. |
| 63 | +
|
| 64 | + * None - Amazon SageMaker will use the input mode specified in the ``Estimator`` |
| 65 | + * 'File' - Amazon SageMaker copies the training dataset from the S3 location to |
| 66 | + a local directory. |
| 67 | + * 'Pipe' - Amazon SageMaker streams data directly from S3 to the container via |
| 68 | + a Unix-named pipe. |
| 69 | +
|
| 70 | + attribute_names (list[str]): A list of one or more attribute names to use that are |
| 71 | + found in a specified AugmentedManifestFile. |
| 72 | + shuffle_config (ShuffleConfig): If specified this configuration enables shuffling on |
| 73 | + this channel. See the SageMaker API documentation for more info: |
| 74 | + https://docs.aws.amazon.com/sagemaker/latest/dg/API_ShuffleConfig.html |
| 75 | + """ |
| 76 | + |
| 77 | + self.config = { |
| 78 | + "DataSource": { |
| 79 | + "S3DataSource": { |
| 80 | + "S3DataDistributionType": distribution, |
| 81 | + "S3DataType": s3_data_type, |
| 82 | + "S3Uri": s3_data, |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if compression is not None: |
| 88 | + self.config["CompressionType"] = compression |
| 89 | + if content_type is not None: |
| 90 | + self.config["ContentType"] = content_type |
| 91 | + if record_wrapping is not None: |
| 92 | + self.config["RecordWrapperType"] = record_wrapping |
| 93 | + if input_mode is not None: |
| 94 | + self.config["InputMode"] = input_mode |
| 95 | + if attribute_names is not None: |
| 96 | + self.config["DataSource"]["S3DataSource"]["AttributeNames"] = attribute_names |
| 97 | + if shuffle_config is not None: |
| 98 | + self.config["ShuffleConfig"] = {"Seed": shuffle_config.seed} |
| 99 | + |
| 100 | + |
| 101 | +class FileSystemInput(object): |
| 102 | + """Amazon SageMaker channel configurations for file system data sources. |
| 103 | +
|
| 104 | + Attributes: |
| 105 | + config (dict[str, dict]): A Sagemaker File System ``DataSource``. |
| 106 | + """ |
| 107 | + |
| 108 | + def __init__( |
| 109 | + self, file_system_id, file_system_type, directory_path, file_system_access_mode="ro" |
| 110 | + ): |
| 111 | + """Create a new file system input used by an SageMaker training job. |
| 112 | +
|
| 113 | + Args: |
| 114 | + file_system_id (str): An Amazon file system ID starting with 'fs-'. |
| 115 | + file_system_type (str): The type of file system used for the input. |
| 116 | + Valid values: 'EFS', 'FSxLustre'. |
| 117 | + directory_path (str): Relative path to the root directory (mount point) in |
| 118 | + the file system. |
| 119 | + Reference: https://docs.aws.amazon.com/efs/latest/ug/mounting-fs.html and |
| 120 | + https://docs.aws.amazon.com/fsx/latest/LustreGuide/mount-fs-auto-mount-onreboot.html |
| 121 | + file_system_access_mode (str): Permissions for read and write. |
| 122 | + Valid values: 'ro' or 'rw'. Defaults to 'ro'. |
| 123 | + """ |
| 124 | + |
| 125 | + if file_system_type not in FILE_SYSTEM_TYPES: |
| 126 | + raise ValueError( |
| 127 | + "Unrecognized file system type: %s. Valid values: %s." |
| 128 | + % (file_system_type, ", ".join(FILE_SYSTEM_TYPES)) |
| 129 | + ) |
| 130 | + |
| 131 | + if file_system_access_mode not in FILE_SYSTEM_ACCESS_MODES: |
| 132 | + raise ValueError( |
| 133 | + "Unrecognized file system access mode: %s. Valid values: %s." |
| 134 | + % (file_system_access_mode, ", ".join(FILE_SYSTEM_ACCESS_MODES)) |
| 135 | + ) |
| 136 | + |
| 137 | + self.config = { |
| 138 | + "DataSource": { |
| 139 | + "FileSystemDataSource": { |
| 140 | + "FileSystemId": file_system_id, |
| 141 | + "FileSystemType": file_system_type, |
| 142 | + "DirectoryPath": directory_path, |
| 143 | + "FileSystemAccessMode": file_system_access_mode, |
| 144 | + } |
| 145 | + } |
| 146 | + } |
0 commit comments