File size: 12,063 Bytes
476455e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from __future__ import absolute_import

import json
import math
import os

import pytest
import scipy.stats as st

from sagemaker import image_uris
from sagemaker.deserializers import CSVDeserializer
from sagemaker.s3 import S3Uploader
from sagemaker.session import production_variant
from sagemaker.sparkml import SparkMLModel
from sagemaker.utils import unique_name_from_base
from sagemaker.predictor import Predictor
from sagemaker.serializers import CSVSerializer
import tests.integ


ROLE = "SageMakerRole"
MODEL_NAME = unique_name_from_base("test-xgboost-model")
DEFAULT_REGION = "us-west-2"
DEFAULT_INSTANCE_TYPE = "ml.m5.xlarge"
DEFAULT_INSTANCE_COUNT = 1
XG_BOOST_MODEL_LOCAL_PATH = os.path.join(tests.integ.DATA_DIR, "xgboost_model", "xgb_model.tar.gz")

TEST_VARIANT_1 = "Variant1"
TEST_VARIANT_1_WEIGHT = 0.3

TEST_VARIANT_2 = "Variant2"
TEST_VARIANT_2_WEIGHT = 0.7

VARIANT_TRAFFIC_SAMPLING_COUNT = 100
DESIRED_CONFIDENCE_FOR_VARIANT_TRAFFIC_DISTRIBUTION = 0.999

TEST_CSV_DATA = "42,42,42,42,42,42,42"

SPARK_ML_MODEL_LOCAL_PATH = os.path.join(
    tests.integ.DATA_DIR, "sparkml_model", "mleap_model.tar.gz"
)
SPARK_ML_DEFAULT_VARIANT_NAME = (
    "AllTraffic"  # default defined in src/sagemaker/session.py def production_variant
)
SPARK_ML_WRONG_VARIANT_NAME = "WRONG_VARIANT"
SPARK_ML_TEST_DATA = "1.0,C,38.0,71.5,1.0,female"
SPARK_ML_MODEL_SCHEMA = json.dumps(
    {
        "input": [
            {"name": "Pclass", "type": "float"},
            {"name": "Embarked", "type": "string"},
            {"name": "Age", "type": "float"},
            {"name": "Fare", "type": "float"},
            {"name": "SibSp", "type": "float"},
            {"name": "Sex", "type": "string"},
        ],
        "output": {"name": "features", "struct": "vector", "type": "double"},
    }
)


@pytest.fixture(scope="module")
def multi_variant_endpoint(sagemaker_session):
    """
    Sets up the multi variant endpoint before the integration tests run.
    Cleans up the multi variant endpoint after the integration tests run.
    """
    multi_variant_endpoint.endpoint_name = unique_name_from_base(
        "integ-test-multi-variant-endpoint"
    )
    with tests.integ.timeout.timeout_and_delete_endpoint_by_name(
        endpoint_name=multi_variant_endpoint.endpoint_name,
        sagemaker_session=sagemaker_session,
        hours=2,
    ):

        # Creating a model
        bucket = sagemaker_session.default_bucket()
        prefix = "sagemaker/DEMO-VariantTargeting"
        model_url = S3Uploader.upload(
            local_path=XG_BOOST_MODEL_LOCAL_PATH,
            desired_s3_uri="s3://{}/{}".format(bucket, prefix),
            sagemaker_session=sagemaker_session,
        )

        image_uri = image_uris.retrieve(
            "xgboost",
            sagemaker_session.boto_region_name,
            version="0.90-1",
            instance_type=DEFAULT_INSTANCE_TYPE,
            image_scope="inference",
        )
        multi_variant_endpoint_model = sagemaker_session.create_model(
            name=MODEL_NAME,
            role=ROLE,
            container_defs={"Image": image_uri, "ModelDataUrl": model_url},
        )

        # Creating a multi variant endpoint
        variant1 = production_variant(
            model_name=MODEL_NAME,
            instance_type=DEFAULT_INSTANCE_TYPE,
            initial_instance_count=DEFAULT_INSTANCE_COUNT,
            variant_name=TEST_VARIANT_1,
            initial_weight=TEST_VARIANT_1_WEIGHT,
        )
        variant2 = production_variant(
            model_name=MODEL_NAME,
            instance_type=DEFAULT_INSTANCE_TYPE,
            initial_instance_count=DEFAULT_INSTANCE_COUNT,
            variant_name=TEST_VARIANT_2,
            initial_weight=TEST_VARIANT_2_WEIGHT,
        )
        sagemaker_session.endpoint_from_production_variants(
            name=multi_variant_endpoint.endpoint_name, production_variants=[variant1, variant2]
        )

        # Yield to run the integration tests
        yield multi_variant_endpoint

        # Cleanup resources
        sagemaker_session.delete_model(multi_variant_endpoint_model)
        sagemaker_session.sagemaker_client.delete_endpoint_config(
            EndpointConfigName=multi_variant_endpoint.endpoint_name
        )

    # Validate resource cleanup
    with pytest.raises(Exception) as exception:
        sagemaker_session.sagemaker_client.describe_model(
            ModelName=multi_variant_endpoint_model.name
        )
        assert "Could not find model" in str(exception.value)
        sagemaker_session.sagemaker_client.describe_endpoint_config(
            name=multi_variant_endpoint.endpoint_name
        )
        assert "Could not find endpoint" in str(exception.value)


def test_target_variant_invocation(sagemaker_session, multi_variant_endpoint):

    response = sagemaker_session.sagemaker_runtime_client.invoke_endpoint(
        EndpointName=multi_variant_endpoint.endpoint_name,
        Body=TEST_CSV_DATA,
        ContentType="text/csv",
        Accept="text/csv",
        TargetVariant=TEST_VARIANT_1,
    )
    assert response["InvokedProductionVariant"] == TEST_VARIANT_1

    response = sagemaker_session.sagemaker_runtime_client.invoke_endpoint(
        EndpointName=multi_variant_endpoint.endpoint_name,
        Body=TEST_CSV_DATA,
        ContentType="text/csv",
        Accept="text/csv",
        TargetVariant=TEST_VARIANT_2,
    )
    assert response["InvokedProductionVariant"] == TEST_VARIANT_2


def test_predict_invocation_with_target_variant(sagemaker_session, multi_variant_endpoint):
    predictor = Predictor(
        endpoint_name=multi_variant_endpoint.endpoint_name,
        sagemaker_session=sagemaker_session,
        serializer=CSVSerializer(),
    )

    # Validate that no exception is raised when the target_variant is specified.
    predictor.predict(TEST_CSV_DATA, target_variant=TEST_VARIANT_1)
    predictor.predict(TEST_CSV_DATA, target_variant=TEST_VARIANT_2)


def test_variant_traffic_distribution(sagemaker_session, multi_variant_endpoint):
    variant_1_invocation_count = 0
    variant_2_invocation_count = 0

    for i in range(0, VARIANT_TRAFFIC_SAMPLING_COUNT):
        response = sagemaker_session.sagemaker_runtime_client.invoke_endpoint(
            EndpointName=multi_variant_endpoint.endpoint_name,
            Body=TEST_CSV_DATA,
            ContentType="text/csv",
            Accept="text/csv",
        )
        if response["InvokedProductionVariant"] == TEST_VARIANT_1:
            variant_1_invocation_count += 1
        elif response["InvokedProductionVariant"] == TEST_VARIANT_2:
            variant_2_invocation_count += 1

    assert variant_1_invocation_count + variant_2_invocation_count == VARIANT_TRAFFIC_SAMPLING_COUNT

    variant_1_invocation_percentage = float(variant_1_invocation_count) / float(
        VARIANT_TRAFFIC_SAMPLING_COUNT
    )
    variant_1_margin_of_error = _compute_and_retrieve_margin_of_error(TEST_VARIANT_1_WEIGHT)
    assert variant_1_invocation_percentage < TEST_VARIANT_1_WEIGHT + variant_1_margin_of_error
    assert variant_1_invocation_percentage > TEST_VARIANT_1_WEIGHT - variant_1_margin_of_error

    variant_2_invocation_percentage = float(variant_2_invocation_count) / float(
        VARIANT_TRAFFIC_SAMPLING_COUNT
    )
    variant_2_margin_of_error = _compute_and_retrieve_margin_of_error(TEST_VARIANT_2_WEIGHT)
    assert variant_2_invocation_percentage < TEST_VARIANT_2_WEIGHT + variant_2_margin_of_error
    assert variant_2_invocation_percentage > TEST_VARIANT_2_WEIGHT - variant_2_margin_of_error


def test_spark_ml_predict_invocation_with_target_variant(sagemaker_session):

    spark_ml_model_endpoint_name = unique_name_from_base("integ-test-target-variant-sparkml")

    model_data = sagemaker_session.upload_data(
        path=SPARK_ML_MODEL_LOCAL_PATH, key_prefix="integ-test-data/sparkml/model"
    )

    with tests.integ.timeout.timeout_and_delete_endpoint_by_name(
        spark_ml_model_endpoint_name, sagemaker_session
    ):
        spark_ml_model = SparkMLModel(
            model_data=model_data,
            role=ROLE,
            sagemaker_session=sagemaker_session,
            env={"SAGEMAKER_SPARKML_SCHEMA": SPARK_ML_MODEL_SCHEMA},
        )

        predictor = spark_ml_model.deploy(
            DEFAULT_INSTANCE_COUNT,
            DEFAULT_INSTANCE_TYPE,
            endpoint_name=spark_ml_model_endpoint_name,
        )

        # Validate that no exception is raised when the target_variant is specified.
        predictor.predict(SPARK_ML_TEST_DATA, target_variant=SPARK_ML_DEFAULT_VARIANT_NAME)

        with pytest.raises(Exception) as exception_info:
            predictor.predict(SPARK_ML_TEST_DATA, target_variant=SPARK_ML_WRONG_VARIANT_NAME)

        assert "ValidationError" in str(exception_info.value)
        assert SPARK_ML_WRONG_VARIANT_NAME in str(exception_info.value)

        # cleanup resources
        spark_ml_model.delete_model()
        sagemaker_session.sagemaker_client.delete_endpoint_config(
            EndpointConfigName=spark_ml_model_endpoint_name
        )

    # Validate resource cleanup
    with pytest.raises(Exception) as exception:
        sagemaker_session.sagemaker_client.describe_model(ModelName=spark_ml_model.name)
        assert "Could not find model" in str(exception.value)
        sagemaker_session.sagemaker_client.describe_endpoint_config(
            name=spark_ml_model_endpoint_name
        )
        assert "Could not find endpoint" in str(exception.value)


@pytest.mark.local_mode
def test_target_variant_invocation_local_mode(sagemaker_session, multi_variant_endpoint):

    if sagemaker_session._region_name is None:
        sagemaker_session._region_name = DEFAULT_REGION

    response = sagemaker_session.sagemaker_runtime_client.invoke_endpoint(
        EndpointName=multi_variant_endpoint.endpoint_name,
        Body=TEST_CSV_DATA,
        ContentType="text/csv",
        Accept="text/csv",
        TargetVariant=TEST_VARIANT_1,
    )
    assert response["InvokedProductionVariant"] == TEST_VARIANT_1

    response = sagemaker_session.sagemaker_runtime_client.invoke_endpoint(
        EndpointName=multi_variant_endpoint.endpoint_name,
        Body=TEST_CSV_DATA,
        ContentType="text/csv",
        Accept="text/csv",
        TargetVariant=TEST_VARIANT_2,
    )
    assert response["InvokedProductionVariant"] == TEST_VARIANT_2


@pytest.mark.local_mode
def test_predict_invocation_with_target_variant_local_mode(
    sagemaker_session, multi_variant_endpoint
):

    if sagemaker_session._region_name is None:
        sagemaker_session._region_name = DEFAULT_REGION

    predictor = Predictor(
        endpoint_name=multi_variant_endpoint.endpoint_name,
        sagemaker_session=sagemaker_session,
        serializer=CSVSerializer(),
        deserializer=CSVDeserializer(),
    )

    # Validate that no exception is raised when the target_variant is specified.
    predictor.predict(TEST_CSV_DATA, target_variant=TEST_VARIANT_1)
    predictor.predict(TEST_CSV_DATA, target_variant=TEST_VARIANT_2)


def _compute_and_retrieve_margin_of_error(variant_weight):
    """
    Computes the margin of error using the Wald method for computing the confidence
    intervals of a binomial distribution.
    """
    z_value = st.norm.ppf(DESIRED_CONFIDENCE_FOR_VARIANT_TRAFFIC_DISTRIBUTION)
    margin_of_error = (variant_weight * (1 - variant_weight)) / VARIANT_TRAFFIC_SAMPLING_COUNT
    margin_of_error = z_value * math.sqrt(margin_of_error)
    return margin_of_error