|
| 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 | +"""Shallow submission tests for ``Compute.instance_preferences``. |
| 14 | +
|
| 15 | +Each accepted case submits a real ``CreateTrainingJob`` carrying an ordered |
| 16 | +``InstancePreferences`` list (no top-level ``InstanceType``), asserts the |
| 17 | +service returned an ARN and echoed the list on Describe, then stops the job. |
| 18 | +Rejected cases assert the request is refused with the documented reason. |
| 19 | +
|
| 20 | +Which instance type wins, and how the job runs on it, is training behaviour and |
| 21 | +belongs in the deep suites. |
| 22 | +""" |
| 23 | + |
| 24 | +from __future__ import absolute_import |
| 25 | + |
| 26 | +import os |
| 27 | + |
| 28 | +import pytest |
| 29 | +from sagemaker.core import shapes |
| 30 | +from sagemaker.core.shapes import InstancePreference |
| 31 | +from sagemaker.core.training.configs import Compute, SourceCode |
| 32 | +from sagemaker.train.model_trainer import ModelTrainer |
| 33 | + |
| 34 | +from .harness import ( |
| 35 | + MAX_RUNTIME_IN_SECONDS, |
| 36 | + assert_rejected, |
| 37 | + assert_submitted, |
| 38 | + cpu_image, |
| 39 | + submitted, |
| 40 | + unique_name, |
| 41 | +) |
| 42 | + |
| 43 | +DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "..", "..", "data") |
| 44 | +PARAM_SCRIPT_SOURCE_DIR = os.path.join(DATA_DIR, "params_script") |
| 45 | + |
| 46 | +# Two small CPU types. INSTANCE_PREFERENCES_TEST_INSTANCE_TYPES (comma-separated) |
| 47 | +# overrides them for accounts where these lack quota. |
| 48 | +PREFERENCE_TYPES = [ |
| 49 | + t.strip() |
| 50 | + for t in os.environ.get( |
| 51 | + "INSTANCE_PREFERENCES_TEST_INSTANCE_TYPES", "ml.m5.large,ml.m5.xlarge" |
| 52 | + ).split(",") |
| 53 | + if t.strip() |
| 54 | +] |
| 55 | + |
| 56 | + |
| 57 | +def _trainer(sagemaker_session, name, compute): |
| 58 | + return ModelTrainer( |
| 59 | + sagemaker_session=sagemaker_session, |
| 60 | + training_image=cpu_image(sagemaker_session), |
| 61 | + source_code=SourceCode( |
| 62 | + source_dir=PARAM_SCRIPT_SOURCE_DIR, |
| 63 | + requirements="requirements.txt", |
| 64 | + entry_script="train.py", |
| 65 | + ), |
| 66 | + compute=compute, |
| 67 | + stopping_condition=shapes.StoppingCondition(max_runtime_in_seconds=MAX_RUNTIME_IN_SECONDS), |
| 68 | + base_job_name=name, |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +def _preferences(counts=None): |
| 73 | + if counts is None: |
| 74 | + return [InstancePreference(instance_type=t) for t in PREFERENCE_TYPES] |
| 75 | + return [ |
| 76 | + InstancePreference(instance_type=t, instance_count=c) |
| 77 | + for t, c in zip(PREFERENCE_TYPES, counts) |
| 78 | + ] |
| 79 | + |
| 80 | + |
| 81 | +def _echoed_types(job): |
| 82 | + job.refresh() |
| 83 | + prefs = job.resource_config.instance_preferences |
| 84 | + return [p.instance_type for p in prefs] if prefs else [] |
| 85 | + |
| 86 | + |
| 87 | +class TestInstancePreferencesAccepted: |
| 88 | + def test_uniform_count_is_accepted_and_echoed(self, sagemaker_session): |
| 89 | + name = unique_name("shallow-ip-uniform") |
| 90 | + trainer = _trainer( |
| 91 | + sagemaker_session, |
| 92 | + name, |
| 93 | + Compute(instance_preferences=_preferences(), instance_count=1), |
| 94 | + ) |
| 95 | + |
| 96 | + with submitted(trainer) as job: |
| 97 | + assert_submitted(job) |
| 98 | + assert _echoed_types(job) == PREFERENCE_TYPES |
| 99 | + # The customer never set a top-level type; the service must not invent one. |
| 100 | + assert not job.resource_config.instance_type |
| 101 | + |
| 102 | + def test_per_preference_counts_are_accepted_and_echoed(self, sagemaker_session): |
| 103 | + name = unique_name("shallow-ip-per-pref") |
| 104 | + trainer = _trainer( |
| 105 | + sagemaker_session, |
| 106 | + name, |
| 107 | + Compute(instance_preferences=_preferences(counts=[1, 1])), |
| 108 | + ) |
| 109 | + |
| 110 | + with submitted(trainer) as job: |
| 111 | + assert_submitted(job) |
| 112 | + assert _echoed_types(job) == PREFERENCE_TYPES |
| 113 | + |
| 114 | + |
| 115 | +class TestInstancePreferencesRejected: |
| 116 | + """Client-side rules must fail fast: a payload that reached the service with |
| 117 | + both a fixed type and a list would be a regression in the SDK, not a |
| 118 | + behaviour to leave for the backend to catch.""" |
| 119 | + |
| 120 | + def test_instance_type_with_preferences_is_rejected(self, sagemaker_session): |
| 121 | + with pytest.raises(ValueError, match="mutually exclusive with instance_type"): |
| 122 | + Compute( |
| 123 | + instance_type=PREFERENCE_TYPES[0], |
| 124 | + instance_count=1, |
| 125 | + instance_preferences=_preferences(), |
| 126 | + ) |
| 127 | + |
| 128 | + def test_managed_spot_with_preferences_is_rejected(self, sagemaker_session): |
| 129 | + with pytest.raises(ValueError, match="mutually exclusive with managed spot training"): |
| 130 | + Compute( |
| 131 | + instance_count=1, |
| 132 | + enable_managed_spot_training=True, |
| 133 | + instance_preferences=_preferences(), |
| 134 | + ) |
| 135 | + |
| 136 | + def test_duplicate_types_are_rejected(self, sagemaker_session): |
| 137 | + with pytest.raises(ValueError, match="duplicate instance types"): |
| 138 | + Compute( |
| 139 | + instance_count=1, |
| 140 | + instance_preferences=[ |
| 141 | + InstancePreference(instance_type=PREFERENCE_TYPES[0]), |
| 142 | + InstancePreference(instance_type=PREFERENCE_TYPES[0]), |
| 143 | + ], |
| 144 | + ) |
| 145 | + |
| 146 | + def test_server_rejects_more_than_five_preferences(self, sagemaker_session): |
| 147 | + """The list cap is deliberately not enforced client-side (it is a |
| 148 | + server-side tunable), so this is the one rule that must be asserted |
| 149 | + against the service.""" |
| 150 | + six = [ |
| 151 | + "ml.m5.large", |
| 152 | + "ml.m5.xlarge", |
| 153 | + "ml.m5.2xlarge", |
| 154 | + "ml.m4.xlarge", |
| 155 | + "ml.c5.xlarge", |
| 156 | + "ml.c5.2xlarge", |
| 157 | + ] |
| 158 | + trainer = _trainer( |
| 159 | + sagemaker_session, |
| 160 | + unique_name("shallow-ip-six"), |
| 161 | + Compute( |
| 162 | + instance_preferences=[InstancePreference(instance_type=t) for t in six], |
| 163 | + instance_count=1, |
| 164 | + ), |
| 165 | + ) |
| 166 | + assert_rejected(trainer, ("InstancePreferences", "5", "Member must have length")) |
0 commit comments