|
| 1 | +# Copyright 2021 The HuggingFace Team, Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import os |
| 15 | +import tempfile |
| 16 | + |
| 17 | +import pytest |
| 18 | +from transformers.testing_utils import require_torch |
| 19 | + |
| 20 | +from sagemaker_huggingface_inference_toolkit.optimum_utils import ( |
| 21 | + get_input_shapes, |
| 22 | + get_optimum_neuron_pipeline, |
| 23 | + is_optimum_neuron_available, |
| 24 | +) |
| 25 | +from sagemaker_huggingface_inference_toolkit.transformers_utils import _load_model_from_hub |
| 26 | + |
| 27 | + |
| 28 | +require_inferentia = pytest.mark.skipif( |
| 29 | + not is_optimum_neuron_available(), |
| 30 | + reason="Skipping tests, since optimum neuron is not available or not running on inf2 instances.", |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +REMOTE_NOT_CONVERTED_MODEL = "hf-internal-testing/tiny-random-BertModel" |
| 35 | +REMOTE_CONVERTED_MODEL = "optimum/tiny_random_bert_neuron" |
| 36 | +TASK = "text-classification" |
| 37 | + |
| 38 | + |
| 39 | +@require_torch |
| 40 | +@require_inferentia |
| 41 | +def test_not_supported_task(): |
| 42 | + os.environ["HF_TASK"] = "not-supported-task" |
| 43 | + with pytest.raises(Exception): |
| 44 | + get_optimum_neuron_pipeline(task=TASK, model_dir=os.getcwd()) |
| 45 | + |
| 46 | + |
| 47 | +@require_torch |
| 48 | +@require_inferentia |
| 49 | +def test_get_input_shapes_from_file(): |
| 50 | + with tempfile.TemporaryDirectory() as tmpdirname: |
| 51 | + storage_folder = _load_model_from_hub( |
| 52 | + model_id=REMOTE_CONVERTED_MODEL, |
| 53 | + model_dir=tmpdirname, |
| 54 | + ) |
| 55 | + input_shapes = get_input_shapes(model_dir=storage_folder) |
| 56 | + assert input_shapes["batch_size"] == 1 |
| 57 | + assert input_shapes["sequence_length"] == 16 |
| 58 | + |
| 59 | + |
| 60 | +@require_torch |
| 61 | +@require_inferentia |
| 62 | +def test_get_input_shapes_from_env(): |
| 63 | + os.environ["HF_OPTIMUM_BATCH_SIZE"] = "4" |
| 64 | + os.environ["HF_OPTIMUM_SEQUENCE_LENGTH"] = "32" |
| 65 | + with tempfile.TemporaryDirectory() as tmpdirname: |
| 66 | + storage_folder = _load_model_from_hub( |
| 67 | + model_id=REMOTE_NOT_CONVERTED_MODEL, |
| 68 | + model_dir=tmpdirname, |
| 69 | + ) |
| 70 | + input_shapes = get_input_shapes(model_dir=storage_folder) |
| 71 | + assert input_shapes["batch_size"] == 4 |
| 72 | + assert input_shapes["sequence_length"] == 32 |
| 73 | + |
| 74 | + |
| 75 | +@require_torch |
| 76 | +@require_inferentia |
| 77 | +def test_get_optimum_neuron_pipeline_from_converted_model(): |
| 78 | + with tempfile.TemporaryDirectory() as tmpdirname: |
| 79 | + os.system( |
| 80 | + f"optimum-cli export neuron --model philschmid/tiny-distilbert-classification --sequence_length 32 --batch_size 1 {tmpdirname}" |
| 81 | + ) |
| 82 | + pipe = get_optimum_neuron_pipeline(task=TASK, model_dir=tmpdirname) |
| 83 | + r = pipe("This is a test") |
| 84 | + |
| 85 | + assert r[0]["score"] > 0.0 |
| 86 | + assert isinstance(r[0]["label"], str) |
| 87 | + |
| 88 | + |
| 89 | +@require_torch |
| 90 | +@require_inferentia |
| 91 | +def test_get_optimum_neuron_pipeline_from_non_converted_model(): |
| 92 | + os.environ["OPTIMUM_NEURON_SEQUENCE_LENGTH"] = "32" |
| 93 | + with tempfile.TemporaryDirectory() as tmpdirname: |
| 94 | + storage_folder = _load_model_from_hub( |
| 95 | + model_id=REMOTE_NOT_CONVERTED_MODEL, |
| 96 | + model_dir=tmpdirname, |
| 97 | + ) |
| 98 | + pipe = get_optimum_neuron_pipeline(task=TASK, model_dir=storage_folder) |
| 99 | + r = pipe("This is a test") |
| 100 | + |
| 101 | + assert r[0]["score"] > 0.0 |
| 102 | + assert isinstance(r[0]["label"], str) |
0 commit comments