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 test_default_sklearn_image_uri(): image_tag = "0.20.0-cpu-py3" sklearn_image_uri = default_framework_uri(scikit_learn_framework_name, "us-west-1", image_tag) assert ( sklearn_image_uri == "746614075791.dkr.ecr.us-west-1.amazonaws.com/sagemaker-scikit-learn:0.20.0-cpu-py3" )
def serving_image_uri(self, region_name, instance_type): # pylint: disable=unused-argument """Create a URI for the serving image. Args: region_name (str): AWS region where the image is uploaded. instance_type (str): SageMaker instance type. This parameter is unused because XGBoost supports only CPU. Returns: str: The appropriate image URI based on the given parameters. """ image_tag = "{}-{}-{}".format(self.framework_version, "cpu", self.py_version) return default_framework_uri(self.__framework_name__, region_name, image_tag)
def __init__(self, name, training_resource_config, region, repo_version): self.algo_name = name self.training_resource_config = training_resource_config self.region = region self.repo_version = repo_version if self.algo_name == "xgboost": self.algo_image_uri = default_framework_uri( framework=self.algo_name, region_name=region, image_tag=repo_version ) else: self.algo_image_uri = get_image_uri( region_name=region, repo_name=self.algo_name, repo_version=repo_version )
def __init__(self, entry_point, framework_version=SKLEARN_VERSION, source_dir=None, hyperparameters=None, py_version='py3', image_name=None, **kwargs): """ This ``Estimator`` executes an Scikit-learn script in a managed Scikit-learn execution environment, within a SageMaker Training Job. The managed Scikit-learn environment is an Amazon-built Docker container that executes functions defined in the supplied ``entry_point`` Python script. Training is started by calling :meth:`~sagemaker.amazon.estimator.Framework.fit` on this Estimator. After training is complete, calling :meth:`~sagemaker.amazon.estimator.Framework.deploy` creates a hosted SageMaker endpoint and returns an :class:`~sagemaker.amazon.sklearn.model.SKLearnPredictor` instance that can be used to perform inference against the hosted model. Technical documentation on preparing Scikit-learn scripts for SageMaker training and using the Scikit-learn Estimator is available on the project home-page: https://github.com/aws/sagemaker-python-sdk Args: entry_point (str): Path (absolute or relative) to the Python source file which should be executed as the entry point to training. This should be compatible with either Python 2.7 or Python 3.5. source_dir (str): Path (absolute or relative) to a directory with any other training source code dependencies aside from tne entry point file (default: None). Structure within this directory are preserved when training on Amazon SageMaker. hyperparameters (dict): Hyperparameters that will be used for training (default: None). The hyperparameters are made accessible as a dict[str, str] to the training code on SageMaker. For convenience, this accepts other types for keys and values, but ``str()`` will be called to convert them before training. py_version (str): Python version you want to use for executing your model training code (default: 'py2'). One of 'py2' or 'py3'. framework_version (str): Scikit-learn version you want to use for executing your model training code. List of supported versions https://github.com/aws/sagemaker-python-sdk#sklearn-sagemaker-estimators image_name (str): If specified, the estimator will use this image for training and hosting, instead of selecting the appropriate SageMaker official image based on framework_version and py_version. It can be an ECR url or dockerhub image and tag. Examples: 123.dkr.ecr.us-west-2.amazonaws.com/my-custom-image:1.0 custom-image:latest. **kwargs: Additional kwargs passed to the :class:`~sagemaker.estimator.Framework` constructor. """ # SciKit-Learn does not support distributed training or training on GPU instance types. Fail fast. train_instance_type = kwargs.get('train_instance_type') _validate_not_gpu_instance_type(train_instance_type) train_instance_count = kwargs.get('train_instance_count') if train_instance_count: if train_instance_count != 1: raise AttributeError( "Scikit-Learn does not support distributed training. " "Please remove the 'train_instance_count' argument or set " "'train_instance_count=1' when initializing SKLearn.") super(SKLearn, self).__init__(entry_point, source_dir, hyperparameters, image_name=image_name, **dict(kwargs, train_instance_count=1)) self.py_version = py_version if framework_version is None: logger.warning( empty_framework_version_warning(SKLEARN_VERSION, SKLEARN_VERSION)) self.framework_version = framework_version or SKLEARN_VERSION if image_name is None: image_tag = "{}-{}-{}".format(framework_version, "cpu", py_version) self.image_name = default_framework_uri( SKLearn.__framework_name__, self.sagemaker_session.boto_region_name, image_tag)
def image_uri(sagemaker_session): image_tag = "{}-{}-{}".format("0.20.0", "cpu", "py3") return default_framework_uri("scikit-learn", sagemaker_session.boto_session.region_name, image_tag)
def get_xgboost_image_uri(region, framework_version, py_version="py3"): """Get XGBoost framework image URI""" image_tag = "{}-{}-{}".format(framework_version, "cpu", py_version) return default_framework_uri(XGBoost.__framework_name__, region, image_tag)
def __init__( self, framework_version, role, instance_type, instance_count, command=None, volume_size_in_gb=30, volume_kms_key=None, output_kms_key=None, max_runtime_in_seconds=None, base_job_name=None, sagemaker_session=None, env=None, tags=None, network_config=None, ): """Initialize an ``SKLearnProcessor`` instance. The SKLearnProcessor handles Amazon SageMaker processing tasks for jobs using scikit-learn. Args: framework_version (str): The version of scikit-learn. role (str): An AWS IAM role name or ARN. The Amazon SageMaker training jobs and APIs that create Amazon SageMaker endpoints use this role to access training data and model artifacts. After the endpoint is created, the inference code might use the IAM role, if it needs to access an AWS resource. instance_type (str): Type of EC2 instance to use for processing, for example, 'ml.c4.xlarge'. instance_count (int): The number of instances to run the Processing job with. Defaults to 1. command ([str]): The command to run, along with any command-line flags. Example: ["python3", "-v"]. If not provided, ["python3"] or ["python2"] will be chosen based on the py_version parameter. volume_size_in_gb (int): Size in GB of the EBS volume to use for storing data during processing (default: 30). volume_kms_key (str): A KMS key for the processing volume. output_kms_key (str): The KMS key id for all ProcessingOutputs. max_runtime_in_seconds (int): Timeout in seconds. After this amount of time Amazon SageMaker terminates the job regardless of its current status. base_job_name (str): Prefix for processing name. If not specified, the processor generates a default job name, based on the training image name and current timestamp. sagemaker_session (sagemaker.session.Session): Session object which manages interactions with Amazon SageMaker APIs and any other AWS services needed. If not specified, the processor creates one using the default AWS configuration chain. env (dict): Environment variables to be passed to the processing job. tags ([dict]): List of tags to be passed to the processing job. network_config (sagemaker.network.NetworkConfig): A NetworkConfig object that configures network isolation, encryption of inter-container traffic, security group IDs, and subnets. """ session = sagemaker_session or Session() region = session.boto_region_name if not command: command = ["python3"] image_tag = "{}-{}-{}".format(framework_version, "cpu", "py3") image_uri = default_framework_uri("scikit-learn", region, image_tag) super(SKLearnProcessor, self).__init__( role=role, image_uri=image_uri, instance_count=instance_count, instance_type=instance_type, command=command, volume_size_in_gb=volume_size_in_gb, volume_kms_key=volume_kms_key, output_kms_key=output_kms_key, max_runtime_in_seconds=max_runtime_in_seconds, base_job_name=base_job_name, sagemaker_session=session, env=env, tags=tags, network_config=network_config, )