|
| 1 | +# Copyright 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 | +"""Contains classes for preparing and uploading configs for a scheduled feature processor.""" |
| 14 | +from __future__ import absolute_import |
| 15 | +from typing import Callable, Dict, Optional, Tuple, List, Union |
| 16 | + |
| 17 | +import attr |
| 18 | + |
| 19 | +from sagemaker.core.helper.session_helper import Session |
| 20 | +from sagemaker.mlops.feature_store.feature_processor._constants import ( |
| 21 | + SPARK_JAR_FILES_PATH, |
| 22 | + SPARK_PY_FILES_PATH, |
| 23 | + SPARK_FILES_PATH, |
| 24 | + S3_DATA_DISTRIBUTION_TYPE, |
| 25 | +) |
| 26 | +from sagemaker.core.inputs import TrainingInput |
| 27 | +from sagemaker.core.shapes import Channel, DataSource, S3DataSource |
| 28 | +from sagemaker.core.remote_function.core.stored_function import StoredFunction |
| 29 | +from sagemaker.core.remote_function.job import ( |
| 30 | + _prepare_and_upload_workspace, |
| 31 | + _prepare_and_upload_runtime_scripts, |
| 32 | + _JobSettings, |
| 33 | + RUNTIME_SCRIPTS_CHANNEL_NAME, |
| 34 | + REMOTE_FUNCTION_WORKSPACE, |
| 35 | + SPARK_CONF_CHANNEL_NAME, |
| 36 | + _prepare_and_upload_spark_dependent_files, |
| 37 | +) |
| 38 | +from sagemaker.core.remote_function.runtime_environment.runtime_environment_manager import ( |
| 39 | + RuntimeEnvironmentManager, |
| 40 | +) |
| 41 | +from sagemaker.core.remote_function.spark_config import SparkConfig |
| 42 | +from sagemaker.core.remote_function.custom_file_filter import CustomFileFilter |
| 43 | +from sagemaker.core.s3 import s3_path_join |
| 44 | + |
| 45 | + |
| 46 | +@attr.s |
| 47 | +class ConfigUploader: |
| 48 | + """Prepares and uploads customer provided configs to S3""" |
| 49 | + |
| 50 | + remote_decorator_config: _JobSettings = attr.ib() |
| 51 | + runtime_env_manager: RuntimeEnvironmentManager = attr.ib() |
| 52 | + |
| 53 | + def prepare_step_input_channel_for_spark_mode( |
| 54 | + self, func: Callable, s3_base_uri: str, sagemaker_session: Session |
| 55 | + ) -> Tuple[List[Channel], Dict]: |
| 56 | + """Prepares input channels for SageMaker Pipeline Step. |
| 57 | + |
| 58 | + Returns: |
| 59 | + Tuple of (List[Channel], spark_dependency_paths dict) |
| 60 | + """ |
| 61 | + self._prepare_and_upload_callable(func, s3_base_uri, sagemaker_session) |
| 62 | + bootstrap_scripts_s3uri = self._prepare_and_upload_runtime_scripts( |
| 63 | + self.remote_decorator_config.spark_config, |
| 64 | + s3_base_uri, |
| 65 | + self.remote_decorator_config.s3_kms_key, |
| 66 | + sagemaker_session, |
| 67 | + ) |
| 68 | + dependencies_list_path = self.runtime_env_manager.snapshot( |
| 69 | + self.remote_decorator_config.dependencies |
| 70 | + ) |
| 71 | + user_workspace_s3uri = self._prepare_and_upload_workspace( |
| 72 | + dependencies_list_path, |
| 73 | + self.remote_decorator_config.include_local_workdir, |
| 74 | + self.remote_decorator_config.pre_execution_commands, |
| 75 | + self.remote_decorator_config.pre_execution_script, |
| 76 | + s3_base_uri, |
| 77 | + self.remote_decorator_config.s3_kms_key, |
| 78 | + sagemaker_session, |
| 79 | + self.remote_decorator_config.custom_file_filter, |
| 80 | + ) |
| 81 | + |
| 82 | + ( |
| 83 | + submit_jars_s3_paths, |
| 84 | + submit_py_files_s3_paths, |
| 85 | + submit_files_s3_path, |
| 86 | + config_file_s3_uri, |
| 87 | + ) = self._prepare_and_upload_spark_dependent_files( |
| 88 | + self.remote_decorator_config.spark_config, |
| 89 | + s3_base_uri, |
| 90 | + self.remote_decorator_config.s3_kms_key, |
| 91 | + sagemaker_session, |
| 92 | + ) |
| 93 | + |
| 94 | + channels = [ |
| 95 | + Channel( |
| 96 | + channel_name=RUNTIME_SCRIPTS_CHANNEL_NAME, |
| 97 | + data_source=DataSource( |
| 98 | + s3_data_source=S3DataSource( |
| 99 | + s3_uri=bootstrap_scripts_s3uri, |
| 100 | + s3_data_type="S3Prefix", |
| 101 | + s3_data_distribution_type=S3_DATA_DISTRIBUTION_TYPE, |
| 102 | + ) |
| 103 | + ), |
| 104 | + input_mode="File", |
| 105 | + ) |
| 106 | + ] |
| 107 | + |
| 108 | + if user_workspace_s3uri: |
| 109 | + channels.append( |
| 110 | + Channel( |
| 111 | + channel_name=REMOTE_FUNCTION_WORKSPACE, |
| 112 | + data_source=DataSource( |
| 113 | + s3_data_source=S3DataSource( |
| 114 | + s3_uri=s3_path_join(s3_base_uri, REMOTE_FUNCTION_WORKSPACE), |
| 115 | + s3_data_type="S3Prefix", |
| 116 | + s3_data_distribution_type=S3_DATA_DISTRIBUTION_TYPE, |
| 117 | + ) |
| 118 | + ), |
| 119 | + input_mode="File", |
| 120 | + ) |
| 121 | + ) |
| 122 | + |
| 123 | + if config_file_s3_uri: |
| 124 | + channels.append( |
| 125 | + Channel( |
| 126 | + channel_name=SPARK_CONF_CHANNEL_NAME, |
| 127 | + data_source=DataSource( |
| 128 | + s3_data_source=S3DataSource( |
| 129 | + s3_uri=config_file_s3_uri, |
| 130 | + s3_data_type="S3Prefix", |
| 131 | + s3_data_distribution_type=S3_DATA_DISTRIBUTION_TYPE, |
| 132 | + ) |
| 133 | + ), |
| 134 | + input_mode="File", |
| 135 | + ) |
| 136 | + ) |
| 137 | + |
| 138 | + return channels, { |
| 139 | + SPARK_JAR_FILES_PATH: submit_jars_s3_paths, |
| 140 | + SPARK_PY_FILES_PATH: submit_py_files_s3_paths, |
| 141 | + SPARK_FILES_PATH: submit_files_s3_path, |
| 142 | + } |
| 143 | + |
| 144 | + def _prepare_and_upload_callable( |
| 145 | + self, func: Callable, s3_base_uri: str, sagemaker_session: Session |
| 146 | + ) -> None: |
| 147 | + """Prepares and uploads callable to S3""" |
| 148 | + stored_function = StoredFunction( |
| 149 | + sagemaker_session=sagemaker_session, |
| 150 | + s3_base_uri=s3_base_uri, |
| 151 | + s3_kms_key=self.remote_decorator_config.s3_kms_key, |
| 152 | + ) |
| 153 | + stored_function.save(func) |
| 154 | + |
| 155 | + def _prepare_and_upload_workspace( |
| 156 | + self, |
| 157 | + local_dependencies_path: str, |
| 158 | + include_local_workdir: bool, |
| 159 | + pre_execution_commands: List[str], |
| 160 | + pre_execution_script_local_path: str, |
| 161 | + s3_base_uri: str, |
| 162 | + s3_kms_key: str, |
| 163 | + sagemaker_session: Session, |
| 164 | + custom_file_filter: Optional[Union[Callable[[str, List], List], CustomFileFilter]] = None, |
| 165 | + ) -> str: |
| 166 | + """Upload the training step dependencies to S3 if present""" |
| 167 | + return _prepare_and_upload_workspace( |
| 168 | + local_dependencies_path=local_dependencies_path, |
| 169 | + include_local_workdir=include_local_workdir, |
| 170 | + pre_execution_commands=pre_execution_commands, |
| 171 | + pre_execution_script_local_path=pre_execution_script_local_path, |
| 172 | + s3_base_uri=s3_base_uri, |
| 173 | + s3_kms_key=s3_kms_key, |
| 174 | + sagemaker_session=sagemaker_session, |
| 175 | + custom_file_filter=custom_file_filter, |
| 176 | + ) |
| 177 | + |
| 178 | + def _prepare_and_upload_runtime_scripts( |
| 179 | + self, |
| 180 | + spark_config: SparkConfig, |
| 181 | + s3_base_uri: str, |
| 182 | + s3_kms_key: str, |
| 183 | + sagemaker_session: Session, |
| 184 | + ) -> str: |
| 185 | + """Copy runtime scripts to a folder and upload to S3""" |
| 186 | + return _prepare_and_upload_runtime_scripts( |
| 187 | + spark_config=spark_config, |
| 188 | + s3_base_uri=s3_base_uri, |
| 189 | + s3_kms_key=s3_kms_key, |
| 190 | + sagemaker_session=sagemaker_session, |
| 191 | + ) |
| 192 | + |
| 193 | + def _prepare_and_upload_spark_dependent_files( |
| 194 | + self, |
| 195 | + spark_config: SparkConfig, |
| 196 | + s3_base_uri: str, |
| 197 | + s3_kms_key: str, |
| 198 | + sagemaker_session: Session, |
| 199 | + ) -> Tuple: |
| 200 | + """Upload the spark dependencies to S3 if present""" |
| 201 | + if not spark_config: |
| 202 | + return None, None, None, None |
| 203 | + |
| 204 | + return _prepare_and_upload_spark_dependent_files( |
| 205 | + spark_config=spark_config, |
| 206 | + s3_base_uri=s3_base_uri, |
| 207 | + s3_kms_key=s3_kms_key, |
| 208 | + sagemaker_session=sagemaker_session, |
| 209 | + ) |
0 commit comments