|
| 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 | +from __future__ import absolute_import |
| 14 | + |
| 15 | +import pytest |
| 16 | +from sagemaker.serve.builder.schema_builder import SchemaBuilder |
| 17 | +from sagemaker.serve.builder.model_builder import ModelBuilder, Mode |
| 18 | +import tests.integ |
| 19 | +from tests.integ.sagemaker.serve.constants import ( |
| 20 | + HF_DIR, |
| 21 | + PYTHON_VERSION_IS_NOT_310, |
| 22 | + SERVE_SAGEMAKER_ENDPOINT_TIMEOUT, |
| 23 | +) |
| 24 | +from tests.integ.timeout import timeout |
| 25 | +from tests.integ.utils import cleanup_model_resources, gpu_list, retry_with_instance_list |
| 26 | +import logging |
| 27 | + |
| 28 | +logger = logging.getLogger(__name__) |
| 29 | + |
| 30 | +model_id = "bert-base-uncased" |
| 31 | + |
| 32 | +sample_input = {"inputs": "Hello I'm a [MASK] model."} |
| 33 | + |
| 34 | +sample_output = [ |
| 35 | + { |
| 36 | + "score": 0.10731109976768494, |
| 37 | + "token": 4827, |
| 38 | + "token_str": "fashion", |
| 39 | + "sequence": "hello i'm a fashion model.", |
| 40 | + }, |
| 41 | + { |
| 42 | + "score": 0.08774465322494507, |
| 43 | + "token": 2535, |
| 44 | + "token_str": "role", |
| 45 | + "sequence": "hello i'm a role model.", |
| 46 | + }, |
| 47 | + { |
| 48 | + "score": 0.05338414013385773, |
| 49 | + "token": 2047, |
| 50 | + "token_str": "new", |
| 51 | + "sequence": "hello i'm a new model.", |
| 52 | + }, |
| 53 | + { |
| 54 | + "score": 0.04667224362492561, |
| 55 | + "token": 3565, |
| 56 | + "token_str": "super", |
| 57 | + "sequence": "hello i'm a super model.", |
| 58 | + }, |
| 59 | + { |
| 60 | + "score": 0.027096163481473923, |
| 61 | + "token": 2986, |
| 62 | + "token_str": "fine", |
| 63 | + "sequence": "hello i'm a fine model.", |
| 64 | + }, |
| 65 | +] |
| 66 | + |
| 67 | + |
| 68 | +@pytest.fixture |
| 69 | +def model_input(): |
| 70 | + return {"inputs": "The man worked as a [MASK]."} |
| 71 | + |
| 72 | + |
| 73 | +@pytest.fixture |
| 74 | +def model_builder_model_schema_builder(): |
| 75 | + return ModelBuilder( |
| 76 | + model_path=HF_DIR, model=model_id, schema_builder=SchemaBuilder(sample_input, sample_output) |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +@pytest.fixture |
| 81 | +def model_builder(request): |
| 82 | + return request.getfixturevalue(request.param) |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.skipif( |
| 86 | + PYTHON_VERSION_IS_NOT_310, |
| 87 | + tests.integ.test_region() in tests.integ.TRAINING_NO_P2_REGIONS |
| 88 | + and tests.integ.test_region() in tests.integ.TRAINING_NO_P3_REGIONS, |
| 89 | + reason="no ml.p2 or ml.p3 instances in this region", |
| 90 | +) |
| 91 | +@retry_with_instance_list(gpu_list(tests.integ.test_region())) |
| 92 | +@pytest.mark.parametrize("model_builder", ["model_builder_model_schema_builder"], indirect=True) |
| 93 | +def test_non_text_generation_model_single_GPU( |
| 94 | + sagemaker_session, model_builder, model_input, **kwargs |
| 95 | +): |
| 96 | + iam_client = sagemaker_session.boto_session.client("iam") |
| 97 | + role_arn = iam_client.get_role(RoleName="SageMakerRole")["Role"]["Arn"] |
| 98 | + model = model_builder.build(role_arn=role_arn, sagemaker_session=sagemaker_session) |
| 99 | + caught_ex = None |
| 100 | + with timeout(minutes=SERVE_SAGEMAKER_ENDPOINT_TIMEOUT): |
| 101 | + try: |
| 102 | + logger.info("Running in SAGEMAKER_ENDPOINT mode") |
| 103 | + predictor = model.deploy( |
| 104 | + mode=Mode.SAGEMAKER_ENDPOINT, |
| 105 | + instance_type=kwargs["instance_type"], |
| 106 | + initial_instance_count=1, |
| 107 | + ) |
| 108 | + logger.info("Endpoint successfully deployed.") |
| 109 | + prediction = predictor.predict(model_input) |
| 110 | + assert prediction is not None |
| 111 | + |
| 112 | + endpoint_name = predictor.endpoint_name |
| 113 | + sagemaker_client = sagemaker_session.boto_session.client("sagemaker") |
| 114 | + endpoint_config_name = sagemaker_client.describe_endpoint(EndpointName=endpoint_name)[ |
| 115 | + "EndpointConfigName" |
| 116 | + ] |
| 117 | + actual_instance_type = sagemaker_client.describe_endpoint_config( |
| 118 | + EndpointConfigName=endpoint_config_name |
| 119 | + )["ProductionVariants"][0]["InstanceType"] |
| 120 | + assert kwargs["instance_type"] == actual_instance_type |
| 121 | + except Exception as e: |
| 122 | + caught_ex = e |
| 123 | + finally: |
| 124 | + cleanup_model_resources( |
| 125 | + sagemaker_session=model_builder.sagemaker_session, |
| 126 | + model_name=model.name, |
| 127 | + endpoint_name=model.endpoint_name, |
| 128 | + ) |
| 129 | + if caught_ex: |
| 130 | + logger.exception(caught_ex) |
| 131 | + assert ( |
| 132 | + False |
| 133 | + ), f"Exception {caught_ex} was thrown when running model builder single GPU test" |
| 134 | + |
| 135 | + |
| 136 | +@pytest.mark.skipif( |
| 137 | + PYTHON_VERSION_IS_NOT_310, |
| 138 | + tests.integ.test_region() in tests.integ.TRAINING_NO_P2_REGIONS |
| 139 | + and tests.integ.test_region() in tests.integ.TRAINING_NO_P3_REGIONS, |
| 140 | + reason="no ml.p2 or ml.p3 instances in this region", |
| 141 | +) |
| 142 | +@retry_with_instance_list(gpu_list(tests.integ.test_region())) |
| 143 | +@pytest.mark.parametrize("model_builder", ["model_builder_model_schema_builder"], indirect=True) |
| 144 | +def test_non_text_generation_model_multi_GPU( |
| 145 | + sagemaker_session, model_builder, model_input, **kwargs |
| 146 | +): |
| 147 | + iam_client = sagemaker_session.boto_session.client("iam") |
| 148 | + role_arn = iam_client.get_role(RoleName="SageMakerRole")["Role"]["Arn"] |
| 149 | + caught_ex = None |
| 150 | + model = model_builder.build(role_arn=role_arn, sagemaker_session=sagemaker_session) |
| 151 | + with timeout(minutes=SERVE_SAGEMAKER_ENDPOINT_TIMEOUT): |
| 152 | + try: |
| 153 | + logger.info("Running in SAGEMAKER_ENDPOINT mode") |
| 154 | + predictor = model.deploy( |
| 155 | + mode=Mode.SAGEMAKER_ENDPOINT, |
| 156 | + instance_type=kwargs["instance_type"], |
| 157 | + initial_instance_count=1, |
| 158 | + ) |
| 159 | + logger.info("Endpoint successfully deployed.") |
| 160 | + prediction = predictor.predict(model_input) |
| 161 | + assert prediction is not None |
| 162 | + |
| 163 | + endpoint_name = predictor.endpoint_name |
| 164 | + sagemaker_client = sagemaker_session.boto_session.client("sagemaker") |
| 165 | + endpoint_config_name = sagemaker_client.describe_endpoint(EndpointName=endpoint_name)[ |
| 166 | + "EndpointConfigName" |
| 167 | + ] |
| 168 | + actual_instance_type = sagemaker_client.describe_endpoint_config( |
| 169 | + EndpointConfigName=endpoint_config_name |
| 170 | + )["ProductionVariants"][0]["InstanceType"] |
| 171 | + assert kwargs["instance_type"] == actual_instance_type |
| 172 | + except Exception as e: |
| 173 | + caught_ex = e |
| 174 | + finally: |
| 175 | + cleanup_model_resources( |
| 176 | + sagemaker_session=model_builder.sagemaker_session, |
| 177 | + model_name=model.name, |
| 178 | + endpoint_name=model.endpoint_name, |
| 179 | + ) |
| 180 | + if caught_ex: |
| 181 | + logger.exception(caught_ex) |
| 182 | + assert ( |
| 183 | + False |
| 184 | + ), f"Exception {caught_ex} was thrown when running model builder multi GPU test" |
0 commit comments