Beispiel #1
0
    def _init_sagemaker_session_if_does_not_exist(self, instance_type):
        """Set ``self.sagemaker_session`` to be a ``LocalSession`` or
        ``Session`` if it is not already. The type of session object is
        determined by the instance type.
        """
        if self.sagemaker_session:
            return

        if instance_type in ("local", "local_gpu"):
            self.sagemaker_session = local.LocalSession()
        else:
            self.sagemaker_session = session.Session()
Beispiel #2
0
    def deploy(self, initial_instance_count, instance_type, accelerator_type=None, endpoint_name=None, tags=None):
        """Deploy this ``Model`` to an ``Endpoint`` and optionally return a ``Predictor``.

        Create a SageMaker ``Model`` and ``EndpointConfig``, and deploy an ``Endpoint`` from this ``Model``.
        If ``self.predictor_cls`` is not None, this method returns a the result of invoking
        ``self.predictor_cls`` on the created endpoint name.

        The name of the created model is accessible in the ``name`` field of this ``Model`` after deploy returns

        The name of the created endpoint is accessible in the ``endpoint_name``
        field of this ``Model`` after deploy returns.

        Args:
            instance_type (str): The EC2 instance type to deploy this Model to. For example, 'ml.p2.xlarge'.
            initial_instance_count (int): The initial number of instances to run in the
                ``Endpoint`` created from this ``Model``.
            accelerator_type (str): Type of Elastic Inference accelerator to deploy this model for model loading
                and inference, for example, 'ml.eia1.medium'. If not specified, no Elastic Inference accelerator
                will be attached to the endpoint.
                For more information: https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html
            endpoint_name (str): The name of the endpoint to create (default: None).
                If not specified, a unique endpoint name will be created.
            tags(List[dict[str, str]]): The list of tags to attach to this specific endpoint.

        Returns:
            callable[string, sagemaker.session.Session] or None: Invocation of ``self.predictor_cls`` on
                the created endpoint name, if ``self.predictor_cls`` is not None. Otherwise, return None.
        """
        if not self.sagemaker_session:
            if instance_type in ('local', 'local_gpu'):
                self.sagemaker_session = local.LocalSession()
            else:
                self.sagemaker_session = session.Session()

        if self.role is None:
            raise ValueError("Role can not be null for deploying a model")

        compiled_model_suffix = '-'.join(instance_type.split('.')[:-1])
        if self._is_compiled_model:
            self.name += compiled_model_suffix

        self._create_sagemaker_model(instance_type, accelerator_type)
        production_variant = sagemaker.production_variant(self.name, instance_type, initial_instance_count,
                                                          accelerator_type=accelerator_type)
        if endpoint_name:
            self.endpoint_name = endpoint_name
        else:
            self.endpoint_name = self.name
            if self._is_compiled_model and not self.endpoint_name.endswith(compiled_model_suffix):
                self.endpoint_name += compiled_model_suffix
        self.sagemaker_session.endpoint_from_production_variants(self.endpoint_name, [production_variant], tags)
        if self.predictor_cls:
            return self.predictor_cls(self.endpoint_name, self.sagemaker_session)
Beispiel #3
0
def upload_zip_file(repo_name, bucket, extra_args, dir="."):
    """
    1. Zip up the PWD.
    2. Replace placeholders in buildspec.yml and add to the Zip.
    3. Upload to S3.
    """
    if not bucket:
        # The SDK logs a warning for not having pandas and this is the only way to suppress it.
        # https://github.com/aws/sagemaker-python-sdk/issues/1696
        logging.basicConfig(level=logging.CRITICAL)
        import sagemaker.session as session

        logging.basicConfig(level=logging.INFO)

        bucket = session.Session().default_bucket()

    random_suffix = "".join(random.choices(string.ascii_letters, k=16))
    key = f"codebuild-sagemaker-container-{random_suffix}.zip"
    origdir = os.getcwd()
    os.chdir(dir)
    try:
        with tempfile.TemporaryFile() as tmp:
            with zipfile.ZipFile(tmp, "w") as zip:
                # Zip all files in "dir"
                for dirname, _, filelist in os.walk("."):
                    for file in filelist:
                        zip.write(f"{dirname}/{file}")
                # Add buildspec.yml
                data_dir = os.path.join(
                    os.path.dirname(os.path.abspath(__file__)), "data"
                )
                with tempfile.NamedTemporaryFile() as buildspec:
                    with open(
                        os.path.join(data_dir, "buildspec.template.yml")
                    ) as buildspec_template:
                        buildspec_replaced = buildspec_template.read().replace(
                            "REPLACE_ME_BUILD_ARGS", extra_args
                        )
                        buildspec.write(buildspec_replaced.encode())
                    buildspec.seek(0)
                    zip.write(buildspec.name, "buildspec.yml")
            tmp.seek(0)
            s3 = boto3.session.Session().client("s3")
            s3.upload_fileobj(tmp, bucket, key)
        return (bucket, key)
    finally:
        os.chdir(origdir)
Beispiel #4
0
def test_elastic_inference():
    endpoint_name = utils.unique_name_from_base('mx-p3-8x-resnet')
    instance_type = 'ml.p3.8xlarge'
    framework_version = '1.4.1'

    maeve_client = boto3.client(
        "maeve",
        "us-west-2",
        endpoint_url="https://maeve.loadtest.us-west-2.ml-platform.aws.a2z.com"
    )
    runtime_client = boto3.client(
        "sagemaker-runtime",
        "us-west-2",
        endpoint_url=
        "https://maeveruntime.loadtest.us-west-2.ml-platform.aws.a2z.com")

    sagemaker_session = session.Session(
        sagemaker_client=maeve_client, sagemaker_runtime_client=runtime_client)

    with timeout_and_delete_endpoint_by_name(
            endpoint_name=endpoint_name,
            sagemaker_session=sagemaker_session,
            minutes=20):
        prefix = 'mxnet-serving/default-handlers'
        model_data = sagemaker_session.upload_data(path=MODEL_PATH,
                                                   key_prefix=prefix)
        model = MXNetModel(
            model_data=model_data,
            entry_point=SCRIPT_PATH,
            role='arn:aws:iam::841569659894:role/sagemaker-access-role',
            image=
            '763104351884.dkr.ecr.us-west-2.amazonaws.com/mxnet-inference:1.4.1-gpu-py36-cu100-ubuntu16.04',
            framework_version=framework_version,
            py_version='py3',
            sagemaker_session=sagemaker_session)

        predictor = model.deploy(initial_instance_count=1,
                                 instance_type=instance_type,
                                 endpoint_name=endpoint_name)

        output = predictor.predict([[1, 2]])
        assert [[4.9999918937683105]] == output
    def deploy(
        self,
        initial_instance_count,
        instance_type,
        accelerator_type=None,
        endpoint_name=None,
        update_endpoint=False,
        tags=None,
        kms_key=None,
        wait=True,
    ):
        """Deploy this ``Model`` to an ``Endpoint`` and optionally return a ``Predictor``.

        Create a SageMaker ``Model`` and ``EndpointConfig``, and deploy an ``Endpoint`` from this ``Model``.
        If ``self.predictor_cls`` is not None, this method returns a the result of invoking
        ``self.predictor_cls`` on the created endpoint name.

        The name of the created model is accessible in the ``name`` field of this ``Model`` after deploy returns

        The name of the created endpoint is accessible in the ``endpoint_name``
        field of this ``Model`` after deploy returns.

        Args:
            instance_type (str): The EC2 instance type to deploy this Model to. For example, 'ml.p2.xlarge'.
            initial_instance_count (int): The initial number of instances to run in the
                ``Endpoint`` created from this ``Model``.
            accelerator_type (str): Type of Elastic Inference accelerator to deploy this model for model loading
                and inference, for example, 'ml.eia1.medium'. If not specified, no Elastic Inference accelerator
                will be attached to the endpoint.
                For more information: https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html
            endpoint_name (str): The name of the endpoint to create (default: None).
                If not specified, a unique endpoint name will be created.
            update_endpoint (bool): Flag to update the model in an existing Amazon SageMaker endpoint.
                If True, this will deploy a new EndpointConfig to an already existing endpoint and delete resources
                corresponding to the previous EndpointConfig. If False, a new endpoint will be created. Default: False
            tags(List[dict[str, str]]): The list of tags to attach to this specific endpoint.
            kms_key (str): The ARN of the KMS key that is used to encrypt the data on the
                storage volume attached to the instance hosting the endpoint.
            wait (bool): Whether the call should wait until the deployment of this model completes (default: True).

        Returns:
            callable[string, sagemaker.session.Session] or None: Invocation of ``self.predictor_cls`` on
                the created endpoint name, if ``self.predictor_cls`` is not None. Otherwise, return None.
        """
        if not self.sagemaker_session:
            if instance_type in ("local", "local_gpu"):
                self.sagemaker_session = local.LocalSession()
            else:
                self.sagemaker_session = session.Session()

        if self.role is None:
            raise ValueError("Role can not be null for deploying a model")

        compiled_model_suffix = "-".join(instance_type.split(".")[:-1])
        if self._is_compiled_model:
            self.name += compiled_model_suffix

        self._create_sagemaker_model(instance_type, accelerator_type, tags)
        production_variant = sagemaker.production_variant(
            self.name,
            instance_type,
            initial_instance_count,
            accelerator_type=accelerator_type)
        if endpoint_name:
            self.endpoint_name = endpoint_name
        else:
            self.endpoint_name = self.name
            if self._is_compiled_model and not self.endpoint_name.endswith(
                    compiled_model_suffix):
                self.endpoint_name += compiled_model_suffix

        if update_endpoint:
            endpoint_config_name = self.sagemaker_session.create_endpoint_config(
                name=self.name,
                model_name=self.name,
                initial_instance_count=initial_instance_count,
                instance_type=instance_type,
                accelerator_type=accelerator_type,
                tags=tags,
                kms_key=kms_key,
            )
            self.sagemaker_session.update_endpoint(self.endpoint_name,
                                                   endpoint_config_name)
        else:
            self.sagemaker_session.endpoint_from_production_variants(
                self.endpoint_name, [production_variant], tags, kms_key, wait)

        if self.predictor_cls:
            return self.predictor_cls(self.endpoint_name,
                                      self.sagemaker_session)
Beispiel #6
0
from sagemaker import s3, session

bucket = session.Session().default_bucket()
inputs = s3.S3Uploader.upload('data', 's3://{}/mxnet-gluon-sentiment-example/data'.format(bucket))


pygmentize 'sentiment.py'


#Se corre un training job de sagemaker

from sagemaker import get_execution_role
from sagemaker.mxnet import MXNet

m = MXNet('sentiment.py',
          role=get_execution_role(),
          train_instance_count=1,
          train_instance_type='ml.c4.xlarge',
          framework_version='1.6.0',
          py_version='py3',
          distributions={'parameter_server': {'enabled': True}},
          hyperparameters={'batch-size': 8,
                           'epochs': 2,
                           'learning-rate': 0.01,
                           'embedding-size': 50, 
                           'log-interval': 1000})


#Fit y generación de resultados
m.fit(inputs)
predictor = m.deploy(initial_instance_count=1, instance_type='ml.c4.xlarge')
Beispiel #7
0
from ...sm_utils import get_sm_execution_role, parse_infer_args

ON_SAGEMAKER_NOTEBOOK = False

endpoint_name = 'XGBoostEndpoint-2020-05-24-17-23-16'
s3_capture_upload_path = 's3://sagemaker-ap-southeast-2-454979696062/sagemaker-xgboost-2020-05-24-16-41-30-869/output/endpoint-data-capture'

#baseline_prefix = prefix + '/baselining'
#baseline_data_prefix = baseline_prefix + '/data'
#baseline_results_prefix = baseline_prefix + '/results'

baseline_data_uri = 's3://sagemaker-ap-southeast-2-454979696062/sagemaker/sklearncontainer/boston_train_xgb_n.csv'
baseline_results_uri = 's3://sagemaker-ap-southeast-2-454979696062/sagemaker-xgboost-2020-05-24-16-41-30-869/output/baseline-results'

boto_sess = boto3.Session()
sm_session = session.Session(boto_sess)

def capture():
    # Change parameters as you would like - adjust sampling percentage, 
    #  chose to capture request or response or both.
    #  Learn more from our documentation
    data_capture_config = DataCaptureConfig(
                            enable_capture = True,
                            sampling_percentage=50,
                            destination_s3_uri=s3_capture_upload_path,
                            kms_key_id=None,
                            capture_options=["REQUEST", "RESPONSE"],
                            csv_content_types=["text/csv"],
                            json_content_types=["application/json"])

    # Now it is time to apply the new configuration and wait for it to be applied
import boto3
import pandas as pd
from sagemaker import session
from sagemaker import RealTimePredictor
from sagemaker.model_monitor import (
    CronExpressionGenerator,
    DefaultModelMonitor,
    DataCaptureConfig,
)
from sagemaker import get_execution_role
from sagemaker.model_monitor.dataset_format import DatasetFormat


sm_client = boto3.client("sagemaker")
sm_session = session.Session(boto3.Session())
s3_client = boto3.Session().client("s3")
role = "${module.step-functions.iam_role_arn}"

# give a name to the data drift monitor job
mon_schedule_name = "${var.data_drift_monitor_name}"
endpoint_name = "${var.endpoint_name}"
frequency = "${var.data_drift_monitoring_frequency}"

# define a url path for the captured data output
s3_capture_upload_path = (
    "s3://${aws_s3_bucket.ml_bucket[0].id}/monitor_output/endpoint-data-capture"
)

# define the url path for train data which is the baseline data
baseline_data_uri = "s3://${aws_s3_bucket.ml_bucket[0].id}/" + "${var.train_key}"
Beispiel #9
0
## Deploy a pretrained model as part of the lifecycle configuration

import os
import boto3
import re
import json
from sagemaker import get_execution_role, session

region = boto3.Session().region_name

role = get_execution_role()
print("RoleArn: {}".format(role))

# You can modify the bucket to be your own, but make sure the role you chose for this notebook
# has s3:PutObject permissions. This is the bucket into which the data will be captured
bucket = session.Session(boto3.Session()).default_bucket()
print("Recommendations Bucket: {}".format(bucket))
prefix = 'sagemaker/Recommendations-ModelMonitor'

data_capture_prefix = '{}/datacapture'.format(prefix)
s3_capture_upload_path = 's3://{}/{}'.format(bucket, data_capture_prefix)
reports_prefix = '{}/reports'.format(prefix)
s3_report_path = 's3://{}/{}'.format(bucket, reports_prefix)
code_prefix = '{}/code'.format(prefix)
s3_code_preprocessor_uri = 's3://{}/{}/{}'.format(bucket, code_prefix,
                                                  'preprocessor.py')
s3_code_postprocessor_uri = 's3://{}/{}/{}'.format(bucket, code_prefix,
                                                   'postprocessor.py')

print("Capture path: {}".format(s3_capture_upload_path))
print("Report path: {}".format(s3_report_path))