def prepare_container_def( self, instance_type, accelerator_type=None): # pylint disable=unused-argument """Return a container definition with framework configuration set in model environment variables. This also uploads user-supplied code to S3. Args: instance_type (str): The EC2 instance type to deploy this Model to. For example, 'ml.p2.xlarge'. accelerator_type (str): The Elastic Inference accelerator type to deploy to the instance for loading and making inferences to the model. For example, 'ml.eia1.medium'. Returns: dict[str, str]: A container definition object usable with the CreateModel API. """ deploy_key_prefix = fw_utils.model_code_key_prefix( self.key_prefix, self.name, self.image) self._upload_code(deploy_key_prefix) deploy_env = dict(self.env) deploy_env.update(self._framework_env_vars()) return sagemaker.container_def(self.image, self.model_data, deploy_env)
def prepare_container_def(self, instance_type): """Return a container definition with framework configuration set in model environment variables. This also uploads user-supplied code to S3. Args: instance_type (str): The EC2 instance type to deploy this Model to. For example, 'ml.p2.xlarge'. Returns: dict[str, str]: A container definition object usable with the CreateModel API. """ deploy_image = self.image if not deploy_image: region_name = self.sagemaker_session.boto_region_name deploy_image = create_image_uri(region_name, self.__framework_name__, instance_type, self.framework_version, self.py_version) deploy_key_prefix = model_code_key_prefix(self.key_prefix, self.name, deploy_image) self._upload_code(deploy_key_prefix) deploy_env = dict(self.env) deploy_env.update(self._framework_env_vars()) if self.model_server_workers: deploy_env[MODEL_SERVER_WORKERS_PARAM_NAME.upper()] = str(self.model_server_workers) return sagemaker.container_def(deploy_image, self.model_data, deploy_env)
def prepare_container_def(self, instance_type=None, accelerator_type=None): """Return a container definition with framework configuration. The framework configuration is set in model environment variables. Args: instance_type (str): The EC2 instance type to deploy this Model to. accelerator_type (str): The Elastic Inference accelerator type to deploy to the instance for loading and making inferences to the model. This parameter is unused because accelerator types are not supported by XGBoostModel. Returns: dict[str, str]: A container definition object usable with the CreateModel API. """ deploy_image = self.image_uri if not deploy_image: deploy_image = self.serving_image_uri( self.sagemaker_session.boto_region_name, instance_type) deploy_key_prefix = model_code_key_prefix(self.key_prefix, self.name, deploy_image) self._upload_code(key_prefix=deploy_key_prefix, repack=self.enable_network_isolation()) deploy_env = dict(self.env) deploy_env.update(self._script_mode_env_vars()) if self.model_server_workers: deploy_env[MODEL_SERVER_WORKERS_PARAM_NAME.upper()] = str( self.model_server_workers) model_data = (self.repacked_model_data if self.enable_network_isolation() else self.model_data) return sagemaker.container_def(deploy_image, model_data, deploy_env)
def prepare_container_def(self, instance_type): """Return a container definition with framework configuration set in model environment variables. This also uploads user-supplied code to S3. Args: instance_type (str): The EC2 instance type to deploy this Model to. For example, 'ml.p2.xlarge'. Returns: dict[str, str]: A container definition object usable with the CreateModel API. """ deploy_image = self.image if not deploy_image: region_name = self.sagemaker_session.boto_region_name deploy_image = create_image_uri(region_name, self.__framework_name__, instance_type, self.framework_version, self.py_version) deploy_key_prefix = model_code_key_prefix(self.key_prefix, self.name, deploy_image) self._upload_code(deploy_key_prefix) deploy_env = dict(self.env) deploy_env.update(self._framework_env_vars()) if self.model_server_workers: deploy_env[MODEL_SERVER_WORKERS_PARAM_NAME.upper()] = str( self.model_server_workers) return sagemaker.container_def(deploy_image, self.model_data, deploy_env)
def prepare_container_def(self, instance_type, accelerator_type=None): """Return a container definition with framework configuration set in model environment variables. Args: instance_type (str): The EC2 instance type to deploy this Model to. For example, 'ml.p2.xlarge'. accelerator_type (str): The Elastic Inference accelerator type to deploy to the instance for loading and making inferences to the model. Returns: dict[str, str]: A container definition object usable with the CreateModel API. """ deploy_image = self.image if not deploy_image: region_name = self.sagemaker_session.boto_session.region_name deploy_image = self.serving_image_uri( region_name, instance_type, accelerator_type=accelerator_type) deploy_key_prefix = model_code_key_prefix(self.key_prefix, self.name, deploy_image) self._upload_code(deploy_key_prefix, repack=self._is_mms_version()) deploy_env = dict(self.env) deploy_env.update(self._framework_env_vars()) if self.model_server_workers: deploy_env[MODEL_SERVER_WORKERS_PARAM_NAME.upper()] = str( self.model_server_workers) return sagemaker.container_def( deploy_image, self.repacked_model_data or self.model_data, deploy_env)
def prepare_container_def(self, instance_type, accelerator_type=None): """Return a container definition with framework configuration set in model environment variables. Args: instance_type (str): The EC2 instance type to deploy this Model to. For example, 'ml.m5.xlarge'. accelerator_type (str): The Elastic Inference accelerator type to deploy to the instance for loading and making inferences to the model. For example, 'ml.eia1.medium'. Note: accelerator types are not supported by XGBoostModel. Returns: dict[str, str]: A container definition object usable with the CreateModel API. """ deploy_image = self.image if not deploy_image: image_tag = "{}-{}-{}".format(self.framework_version, "cpu", self.py_version) deploy_image = default_framework_uri( self.__framework_name__, self.sagemaker_session.boto_region_name, image_tag) deploy_key_prefix = model_code_key_prefix(self.key_prefix, self.name, deploy_image) self._upload_code(deploy_key_prefix) deploy_env = dict(self.env) deploy_env.update(self._framework_env_vars()) if self.model_server_workers: deploy_env[MODEL_SERVER_WORKERS_PARAM_NAME.upper()] = str( self.model_server_workers) return sagemaker.container_def(deploy_image, self.model_data, deploy_env)
def prepare_container_def( self, instance_type, accelerator_type=None ) -> Dict[str, str]: """ Return a container definition with framework configuration set in model environment variables. Parameters ---------- instance_type: The EC2 instance type to deploy this Model to. Example:: 'ml.c5.xlarge' # CPU, 'ml.p2.xlarge' # GPU. accelerator_type: The Elastic Inference accelerator type to deploy to the instance for loading and making inferences to the model. Example:: "ml.eia1.medium" Returns -------- Dict[str, str]: A container definition object usable with the CreateModel API. """ is_mms_version = parse_version( self.framework_version ) >= parse_version(self._LOWEST_MMS_VERSION) deploy_image = self.image # TODO implement proper logic handling images when none are provided by user # Example implementation: # https://github.com/aws/sagemaker-python-sdk/blob/master/src/sagemaker/mxnet/model.py logger.info(f"Using image: {deploy_image}") deploy_key_prefix = model_code_key_prefix( self.key_prefix, self.name, deploy_image ) self._upload_code(deploy_key_prefix, is_mms_version) deploy_env = dict(self.env) deploy_env.update(self._framework_env_vars()) if self.model_server_workers: deploy_env[MODEL_SERVER_WORKERS_PARAM_NAME.upper()] = str( self.model_server_workers ) return sagemaker.container_def( deploy_image, self.repacked_model_data or self.model_data, deploy_env, )
def prepare_container_def(self, instance_type): """Return a container definition with framework configuration set in model environment variables. This also uploads user-supplied code to S3. Args: instance_type (str): The EC2 instance type to deploy this Model to. For example, 'ml.p2.xlarge'. Returns: dict[str, str]: A container definition object usable with the CreateModel API. """ deploy_key_prefix = model_code_key_prefix(self.key_prefix, self.name, self.image) self._upload_code(deploy_key_prefix) deploy_env = dict(self.env) deploy_env.update(self._framework_env_vars()) return sagemaker.container_def(self.image, self.model_data, deploy_env)
def prepare_container_def(self, instance_type=None, accelerator_type=None, serverless_inference_config=None): """A container definition with framework configuration set in model environment variables. Args: instance_type (str): The EC2 instance type to deploy this Model to. For example, 'ml.p2.xlarge'. accelerator_type (str): The Elastic Inference accelerator type to deploy to the instance for loading and making inferences to the model. serverless_inference_config (sagemaker.serverless.ServerlessInferenceConfig): Specifies configuration related to serverless endpoint. Instance type is not provided in serverless inference. So this is used to find image URIs. Returns: dict[str, str]: A container definition object usable with the CreateModel API. """ deploy_image = self.image_uri if not deploy_image: if instance_type is None and serverless_inference_config is None: raise ValueError( "Must supply either an instance type (for choosing CPU vs GPU) or an image URI." ) region_name = self.sagemaker_session.boto_session.region_name deploy_image = self.serving_image_uri( region_name, instance_type, accelerator_type=accelerator_type, serverless_inference_config=serverless_inference_config, ) deploy_key_prefix = model_code_key_prefix(self.key_prefix, self.name, deploy_image) self._upload_code(deploy_key_prefix, repack=True) deploy_env = dict(self.env) deploy_env.update(self._script_mode_env_vars()) if self.model_server_workers: deploy_env[MODEL_SERVER_WORKERS_PARAM_NAME.upper()] = str( self.model_server_workers) return sagemaker.container_def( deploy_image, self.repacked_model_data or self.model_data, deploy_env)
def prepare_container_def( self, instance_type=None, accelerator_type=None, serverless_inference_config=None ): """Container definition with framework configuration set in model environment variables. Args: instance_type (str): The EC2 instance type to deploy this Model to. This parameter is unused because Scikit-learn supports only CPU. accelerator_type (str): The Elastic Inference accelerator type to deploy to the instance for loading and making inferences to the model. This parameter is unused because accelerator types are not supported by SKLearnModel. serverless_inference_config (sagemaker.serverless.ServerlessInferenceConfig): Specifies configuration related to serverless endpoint. Instance type is not provided in serverless inference. So this is used to find image URIs. Returns: dict[str, str]: A container definition object usable with the CreateModel API. """ if accelerator_type: raise ValueError("Accelerator types are not supported for Scikit-Learn.") deploy_image = self.image_uri if not deploy_image: deploy_image = self.serving_image_uri( self.sagemaker_session.boto_region_name, instance_type ) deploy_key_prefix = model_code_key_prefix(self.key_prefix, self.name, deploy_image) self._upload_code(key_prefix=deploy_key_prefix, repack=self.enable_network_isolation()) deploy_env = dict(self.env) deploy_env.update(self._script_mode_env_vars()) if self.model_server_workers: deploy_env[MODEL_SERVER_WORKERS_PARAM_NAME.upper()] = str(self.model_server_workers) model_data_uri = ( self.repacked_model_data if self.enable_network_isolation() else self.model_data ) return sagemaker.container_def(deploy_image, model_data_uri, deploy_env)
def prepare_container_def(self, instance_type=None, accelerator_type=None): """Return a container definition with framework configuration set in model environment variables. This also uploads user-supplied code to S3. Args: instance_type (str): The EC2 instance type to deploy this Model to. For example, 'ml.p2.xlarge'. accelerator_type (str): The Elastic Inference accelerator type to deploy to the instance for loading and making inferences to the model. For example, 'ml.eia1.medium'. Returns: dict[str, str]: A container definition object usable with the CreateModel API. """ deploy_image = self.image if not deploy_image: if instance_type is None: raise ValueError( "Must supply either an instance type (for choosing CPU vs GPU) or an image URI." ) region_name = self.sagemaker_session.boto_region_name deploy_image = self.serving_image_uri( region_name, instance_type, accelerator_type=accelerator_type ) deploy_key_prefix = model_code_key_prefix(self.key_prefix, self.name, deploy_image) self._upload_code(deploy_key_prefix) deploy_env = dict(self.env) deploy_env.update(self._framework_env_vars()) if self.model_server_workers: deploy_env[MODEL_SERVER_WORKERS_PARAM_NAME.upper()] = str(self.model_server_workers) return sagemaker.container_def(deploy_image, self.model_data, deploy_env)
def test_model_code_key_prefix_with_all_values_present(): key_prefix = model_code_key_prefix('prefix', 'model_name', 'image_name') assert key_prefix == 'prefix/model_name'
def test_model_code_key_prefix_with_only_image_present(time): key_prefix = model_code_key_prefix(None, None, 'image_name') assert key_prefix == name_from_image('image_name')
def test_model_code_key_prefix_with_all_values_present(): key_prefix = fw_utils.model_code_key_prefix('prefix', 'model_name', 'image_name') assert key_prefix == 'prefix/model_name'
def test_model_code_key_prefix_with_no_prefix_and_all_other_values_present(): key_prefix = model_code_key_prefix(None, 'model_name', 'image_name') assert key_prefix == 'model_name'
def test_model_code_key_prefix_with_prefix_present_and_others_none_fail(): with pytest.raises(TypeError) as error: model_code_key_prefix('prefix', None, None) assert 'expected string' in str(error)
def test_model_code_key_prefix_with_all_values_present(): key_prefix = fw_utils.model_code_key_prefix("prefix", "model_name", "image_name") assert key_prefix == "prefix/model_name"
def test_model_code_key_prefix_with_prefix_present_and_others_none_fail(): with pytest.raises(TypeError) as error: fw_utils.model_code_key_prefix("prefix", None, None) assert "expected string" in str(error)
def test_model_code_key_prefix_with_only_image_present(time): key_prefix = fw_utils.model_code_key_prefix(None, None, "image_name") assert key_prefix == name_from_image("image_name")
def test_model_code_key_prefix_and_image_present(time): key_prefix = fw_utils.model_code_key_prefix('prefix', None, 'image_name') assert key_prefix == 'prefix/' + name_from_image('image_name')
def test_model_code_key_prefix_and_image_present(time): key_prefix = model_code_key_prefix('prefix', None, 'image_name') assert key_prefix == 'prefix/' + name_from_image('image_name')
def test_model_code_key_prefix_with_no_prefix_and_all_other_values_present(): key_prefix = fw_utils.model_code_key_prefix(None, "model_name", "image_name") assert key_prefix == "model_name"
def test_model_code_key_prefix_and_image_present(time): key_prefix = fw_utils.model_code_key_prefix("prefix", None, "image_name") assert key_prefix == "prefix/" + name_from_image("image_name")
def test_model_code_key_prefix_with_all_none_fail(): with pytest.raises(TypeError) as error: model_code_key_prefix(None, None, None) assert 'expected string' in str(error)
def test_model_code_key_prefix_with_all_none_fail(): with pytest.raises(TypeError) as error: fw_utils.model_code_key_prefix(None, None, None) assert "expected string" in str(error)