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