|
| 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 os |
| 16 | +import time |
| 17 | + |
| 18 | +import pytest |
| 19 | +from sagemaker.jumpstart.constants import JUMPSTART_DEFAULT_REGION_NAME |
| 20 | +from sagemaker.jumpstart.hub.hub import Hub |
| 21 | + |
| 22 | +from sagemaker.jumpstart.estimator import JumpStartEstimator |
| 23 | +from tests.integ.sagemaker.jumpstart.constants import ( |
| 24 | + ENV_VAR_JUMPSTART_SDK_TEST_HUB_NAME, |
| 25 | + ENV_VAR_JUMPSTART_SDK_TEST_SUITE_ID, |
| 26 | + JUMPSTART_TAG, |
| 27 | +) |
| 28 | +from tests.integ.sagemaker.jumpstart.utils import ( |
| 29 | + get_public_hub_model_arn, |
| 30 | + get_sm_session, |
| 31 | + with_exponential_backoff, |
| 32 | +) |
| 33 | +from tests.integ.sagemaker.jumpstart.constants import ( |
| 34 | + ENV_VAR_JUMPSTART_SDK_TEST_SUITE_ID, |
| 35 | + JUMPSTART_TAG, |
| 36 | +) |
| 37 | +from tests.integ.sagemaker.jumpstart.utils import ( |
| 38 | + get_sm_session, |
| 39 | + get_training_dataset_for_model_and_version |
| 40 | +) |
| 41 | + |
| 42 | +from sagemaker.jumpstart.utils import get_jumpstart_content_bucket |
| 43 | + |
| 44 | +MAX_INIT_TIME_SECONDS = 5 |
| 45 | + |
| 46 | +TEST_MODEL_IDS = { |
| 47 | + "huggingface-spc-bert-base-cased", |
| 48 | + "meta-textgeneration-llama-2-7b", |
| 49 | + "catboost-regression-model", |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +@with_exponential_backoff() |
| 54 | +def create_model_reference(hub_instance, model_arn): |
| 55 | + hub_instance.create_model_reference(model_arn=model_arn) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture(scope="session") |
| 59 | +def add_model_references(): |
| 60 | + # Create Model References to test in Hub |
| 61 | + hub_instance = Hub( |
| 62 | + hub_name=os.environ[ENV_VAR_JUMPSTART_SDK_TEST_HUB_NAME], sagemaker_session=get_sm_session() |
| 63 | + ) |
| 64 | + for model in TEST_MODEL_IDS: |
| 65 | + model_arn = get_public_hub_model_arn(hub_instance, model) |
| 66 | + create_model_reference(hub_instance, model_arn) |
| 67 | + |
| 68 | + |
| 69 | +def test_jumpstart_hub_estimator(setup, add_model_references): |
| 70 | + |
| 71 | + model_id, model_version = "huggingface-spc-bert-base-cased", "*" |
| 72 | + |
| 73 | + sagemaker_session = get_sm_session() |
| 74 | + |
| 75 | + estimator = JumpStartEstimator( |
| 76 | + model_id=model_id, |
| 77 | + role=sagemaker_session.get_caller_identity_arn(), |
| 78 | + sagemaker_session=sagemaker_session, |
| 79 | + tags=[{"Key": JUMPSTART_TAG, "Value": os.environ[ENV_VAR_JUMPSTART_SDK_TEST_SUITE_ID]}], |
| 80 | + hub_name=os.environ[ENV_VAR_JUMPSTART_SDK_TEST_HUB_NAME], |
| 81 | + ) |
| 82 | + |
| 83 | + estimator.fit( |
| 84 | + inputs = { |
| 85 | + "training": f"s3://{get_jumpstart_content_bucket(JUMPSTART_DEFAULT_REGION_NAME)}/" |
| 86 | + f"{get_training_dataset_for_model_and_version(model_id, model_version)}", |
| 87 | + } |
| 88 | + ) |
| 89 | + |
| 90 | + # test that we can create a JumpStartEstimator from existing job with `attach` |
| 91 | + estimator = JumpStartEstimator.attach( |
| 92 | + training_job_name=estimator.latest_training_job.name, |
| 93 | + model_id=model_id, |
| 94 | + model_version=model_version, |
| 95 | + sagemaker_session=get_sm_session(), |
| 96 | + ) |
| 97 | + |
| 98 | + # uses ml.p3.2xlarge instance |
| 99 | + predictor = estimator.deploy( |
| 100 | + tags=[{"Key": JUMPSTART_TAG, "Value": os.environ[ENV_VAR_JUMPSTART_SDK_TEST_SUITE_ID]}], |
| 101 | + role=get_sm_session().get_caller_identity_arn(), |
| 102 | + sagemaker_session=get_sm_session(), |
| 103 | + ) |
| 104 | + |
| 105 | + response = predictor.predict(["hello", "world"]) |
| 106 | + |
| 107 | + assert response is not None |
| 108 | + |
| 109 | + |
| 110 | +def test_jumpstart_hub_estimator_with_default_session(setup, add_model_references): |
| 111 | + model_id, model_version = "huggingface-spc-bert-base-cased", "*" |
| 112 | + |
| 113 | + sagemaker_session = get_sm_session() |
| 114 | + |
| 115 | + estimator = JumpStartEstimator( |
| 116 | + model_id=model_id, |
| 117 | + role=sagemaker_session.get_caller_identity_arn(), |
| 118 | + sagemaker_session=sagemaker_session, |
| 119 | + tags=[{"Key": JUMPSTART_TAG, "Value": os.environ[ENV_VAR_JUMPSTART_SDK_TEST_SUITE_ID]}], |
| 120 | + hub_name=os.environ[ENV_VAR_JUMPSTART_SDK_TEST_HUB_NAME], |
| 121 | + ) |
| 122 | + |
| 123 | + estimator.fit( |
| 124 | + inputs = { |
| 125 | + "training": f"s3://{get_jumpstart_content_bucket(JUMPSTART_DEFAULT_REGION_NAME)}/" |
| 126 | + f"{get_training_dataset_for_model_and_version(model_id, model_version)}", |
| 127 | + } |
| 128 | + ) |
| 129 | + |
| 130 | + |
| 131 | + # test that we can create a JumpStartEstimator from existing job with `attach` |
| 132 | + estimator = JumpStartEstimator.attach( |
| 133 | + training_job_name=estimator.latest_training_job.name, |
| 134 | + model_id=model_id, |
| 135 | + model_version=model_version, |
| 136 | + ) |
| 137 | + |
| 138 | + # uses ml.p3.2xlarge instance |
| 139 | + predictor = estimator.deploy( |
| 140 | + tags=[{"Key": JUMPSTART_TAG, "Value": os.environ[ENV_VAR_JUMPSTART_SDK_TEST_SUITE_ID]}], |
| 141 | + role=get_sm_session().get_caller_identity_arn() |
| 142 | + ) |
| 143 | + |
| 144 | + response = predictor.predict(["hello", "world"]) |
| 145 | + |
| 146 | + assert response is not None |
| 147 | + |
| 148 | + |
| 149 | +def test_jumpstart_hub_gated_estimator_with_eula(setup, add_model_references): |
| 150 | + |
| 151 | + model_id, model_version = "meta-textgeneration-llama-2-7b", "*" |
| 152 | + |
| 153 | + estimator = JumpStartEstimator( |
| 154 | + model_id=model_id, |
| 155 | + role=get_sm_session().get_caller_identity_arn(), |
| 156 | + sagemaker_session=get_sm_session(), |
| 157 | + hub_name=os.environ[ENV_VAR_JUMPSTART_SDK_TEST_HUB_NAME], |
| 158 | + ) |
| 159 | + |
| 160 | + estimator.fit( |
| 161 | + accept_eula=True, |
| 162 | + inputs = { |
| 163 | + "training": f"s3://{get_jumpstart_content_bucket(JUMPSTART_DEFAULT_REGION_NAME)}/" |
| 164 | + f"{get_training_dataset_for_model_and_version(model_id, model_version)}", |
| 165 | + } |
| 166 | + ) |
| 167 | + |
| 168 | + estimator = JumpStartEstimator.attach( |
| 169 | + training_job_name=estimator.latest_training_job.name, |
| 170 | + model_id=model_id, |
| 171 | + model_version=model_version, |
| 172 | + sagemaker_session=get_sm_session(), |
| 173 | + ) |
| 174 | + |
| 175 | + # uses ml.p3.2xlarge instance |
| 176 | + predictor = estimator.deploy( |
| 177 | + tags=[{"Key": JUMPSTART_TAG, "Value": os.environ[ENV_VAR_JUMPSTART_SDK_TEST_SUITE_ID]}], |
| 178 | + role=get_sm_session().get_caller_identity_arn(), |
| 179 | + sagemaker_session=get_sm_session(), |
| 180 | + ) |
| 181 | + |
| 182 | + response = predictor.predict(["hello", "world"]) |
| 183 | + |
| 184 | + assert response is not None |
| 185 | + |
| 186 | + |
| 187 | +def test_jumpstart_hub_gated_estimator_without_eula(setup, add_model_references): |
| 188 | + |
| 189 | + model_id, model_version = "meta-textgeneration-llama-2-7b", "*" |
| 190 | + |
| 191 | + estimator = JumpStartEstimator( |
| 192 | + model_id=model_id, |
| 193 | + role=get_sm_session().get_caller_identity_arn(), |
| 194 | + sagemaker_session=get_sm_session(), |
| 195 | + hub_name=os.environ[ENV_VAR_JUMPSTART_SDK_TEST_HUB_NAME], |
| 196 | + ) |
| 197 | + with pytest.raises(Exception): |
| 198 | + estimator.fit( |
| 199 | + inputs = { |
| 200 | + "training": f"s3://{get_jumpstart_content_bucket(JUMPSTART_DEFAULT_REGION_NAME)}/" |
| 201 | + f"{get_training_dataset_for_model_and_version(model_id, model_version)}", |
| 202 | + } |
| 203 | + ) |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | +def test_instantiating_estimator(setup, add_model_references): |
| 208 | + |
| 209 | + model_id = "catboost-regression-model" |
| 210 | + |
| 211 | + start_time = time.perf_counter() |
| 212 | + |
| 213 | + JumpStartEstimator( |
| 214 | + model_id=model_id, |
| 215 | + hub_name=os.environ[ENV_VAR_JUMPSTART_SDK_TEST_HUB_NAME], |
| 216 | + ) |
| 217 | + |
| 218 | + elapsed_time = time.perf_counter() - start_time |
| 219 | + |
| 220 | + assert elapsed_time <= MAX_INIT_TIME_SECONDS |
0 commit comments