|
| 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 | + |
| 16 | +from mock.mock import patch |
| 17 | +import pytest |
| 18 | + |
| 19 | +from sagemaker import metric_definitions |
| 20 | + |
| 21 | +from tests.unit.sagemaker.jumpstart.utils import get_spec_from_base_spec |
| 22 | + |
| 23 | + |
| 24 | +@patch("sagemaker.jumpstart.accessors.JumpStartModelsAccessor.get_model_specs") |
| 25 | +def test_jumpstart_default_metric_definitions(patched_get_model_specs): |
| 26 | + |
| 27 | + patched_get_model_specs.side_effect = get_spec_from_base_spec |
| 28 | + |
| 29 | + model_id = "pytorch-ic-mobilenet-v2" |
| 30 | + region = "us-west-2" |
| 31 | + |
| 32 | + definitions = metric_definitions.retrieve_default( |
| 33 | + region=region, |
| 34 | + model_id=model_id, |
| 35 | + model_version="*", |
| 36 | + ) |
| 37 | + assert definitions == [ |
| 38 | + {"Regex": "val_accuracy: ([0-9\\.]+)", "Name": "pytorch-ic:val-accuracy"} |
| 39 | + ] |
| 40 | + |
| 41 | + patched_get_model_specs.assert_called_once_with(region=region, model_id=model_id, version="*") |
| 42 | + |
| 43 | + patched_get_model_specs.reset_mock() |
| 44 | + |
| 45 | + definitions = metric_definitions.retrieve_default( |
| 46 | + region=region, |
| 47 | + model_id=model_id, |
| 48 | + model_version="1.*", |
| 49 | + ) |
| 50 | + assert definitions == [ |
| 51 | + {"Regex": "val_accuracy: ([0-9\\.]+)", "Name": "pytorch-ic:val-accuracy"} |
| 52 | + ] |
| 53 | + |
| 54 | + patched_get_model_specs.assert_called_once_with(region=region, model_id=model_id, version="1.*") |
| 55 | + |
| 56 | + patched_get_model_specs.reset_mock() |
| 57 | + |
| 58 | + with pytest.raises(KeyError): |
| 59 | + metric_definitions.retrieve_default( |
| 60 | + region=region, |
| 61 | + model_id="blah", |
| 62 | + model_version="*", |
| 63 | + ) |
| 64 | + |
| 65 | + with pytest.raises(ValueError): |
| 66 | + metric_definitions.retrieve_default( |
| 67 | + region="mars-south-1", |
| 68 | + model_id=model_id, |
| 69 | + model_version="*", |
| 70 | + ) |
| 71 | + |
| 72 | + with pytest.raises(ValueError): |
| 73 | + metric_definitions.retrieve_default( |
| 74 | + model_version="*", |
| 75 | + ) |
| 76 | + |
| 77 | + with pytest.raises(ValueError): |
| 78 | + metric_definitions.retrieve_default( |
| 79 | + model_id=model_id, |
| 80 | + ) |
0 commit comments