|
| 1 | +# Copyright 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions |
| 5 | +# are met: |
| 6 | +# * Redistributions of source code must retain the above copyright |
| 7 | +# notice, this list of conditions and the following disclaimer. |
| 8 | +# * Redistributions in binary form must reproduce the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer in the |
| 10 | +# documentation and/or other materials provided with the distribution. |
| 11 | +# * Neither the name of NVIDIA CORPORATION nor the names of its |
| 12 | +# contributors may be used to endorse or promote products derived |
| 13 | +# from this software without specific prior written permission. |
| 14 | +# |
| 15 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 16 | +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 19 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | +# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + |
| 27 | +import numpy as np |
| 28 | +import unittest |
| 29 | +import triton_python_backend_utils as pb_utils |
| 30 | + |
| 31 | + |
| 32 | +class PBBLSModelLoadingTest(unittest.TestCase): |
| 33 | + |
| 34 | + def setUp(self): |
| 35 | + self.model_name = "onnx_int32_int32_int32" |
| 36 | + |
| 37 | + def tearDown(self): |
| 38 | + pb_utils.unload_model(self.model_name) |
| 39 | + |
| 40 | + def test_load_unload_model(self): |
| 41 | + self.assertFalse(pb_utils.is_model_ready(model_name=self.model_name)) |
| 42 | + pb_utils.load_model(model_name=self.model_name) |
| 43 | + self.assertTrue(pb_utils.is_model_ready(self.model_name)) |
| 44 | + pb_utils.unload_model(self.model_name) |
| 45 | + self.assertFalse(pb_utils.is_model_ready(self.model_name)) |
| 46 | + |
| 47 | + def test_load_with_config_override(self): |
| 48 | + self.assertFalse(pb_utils.is_model_ready(self.model_name)) |
| 49 | + pb_utils.load_model(self.model_name) |
| 50 | + self.assertTrue(pb_utils.is_model_ready(self.model_name)) |
| 51 | + |
| 52 | + # Send the config with the wrong format |
| 53 | + wrong_config = "\"parameters\": {\"config\": {{\"backend\":\"onnxruntime\", \"version_policy\":{\"specific\":{\"versions\":[2]}}}}}" |
| 54 | + with self.assertRaises(pb_utils.TritonModelException): |
| 55 | + pb_utils.load_model(model_name=self.model_name, config=wrong_config) |
| 56 | + # The model should not be changed after a failed load model request |
| 57 | + for version in ["2", "3"]: |
| 58 | + self.assertTrue(pb_utils.is_model_ready(model_name=self.model_name, model_version=version)) |
| 59 | + |
| 60 | + # Send the config with the correct format |
| 61 | + config = "{\"backend\":\"onnxruntime\", \"version_policy\":{\"specific\":{\"versions\":[2]}}}" |
| 62 | + pb_utils.load_model(self.model_name, config=config) |
| 63 | + # The model should be changed after a successful load model request |
| 64 | + self.assertTrue(pb_utils.is_model_ready(self.model_name, "2")) |
| 65 | + self.assertFalse(pb_utils.is_model_ready(self.model_name, "3")) |
| 66 | + |
| 67 | + def test_load_with_file_override(self): |
| 68 | + self.assertFalse(pb_utils.is_model_ready(self.model_name)) |
| 69 | + pb_utils.load_model(self.model_name) |
| 70 | + self.assertTrue(pb_utils.is_model_ready(self.model_name)) |
| 71 | + |
| 72 | + override_name = "override_model" |
| 73 | + config = "{\"backend\":\"onnxruntime\"}" |
| 74 | + with open('models/onnx_int32_int32_int32/3/model.onnx', 'rb') as file: |
| 75 | + data = file.read() |
| 76 | + files = {"file:1/model.onnx": data} |
| 77 | + |
| 78 | + # Request to load the model with override file, should fail without |
| 79 | + # providing override config. |
| 80 | + with self.assertRaises(pb_utils.TritonModelException): |
| 81 | + pb_utils.load_model(self.model_name, "", files) |
| 82 | + |
| 83 | + # Request to load the model with override file and config in a different name |
| 84 | + pb_utils.load_model(model_name=override_name, config=config, files=files) |
| 85 | + # Sanity check that the model with original name is unchanged |
| 86 | + self.assertFalse(pb_utils.is_model_ready(self.model_name, "1")) |
| 87 | + self.assertTrue(pb_utils.is_model_ready(self.model_name, "3")) |
| 88 | + |
| 89 | + # Check the override model readiness |
| 90 | + self.assertTrue(pb_utils.is_model_ready(override_name, "1")) |
| 91 | + self.assertFalse(pb_utils.is_model_ready(override_name, "3")) |
| 92 | + |
| 93 | + # Request to load the model with override file and config in original name |
| 94 | + pb_utils.load_model(self.model_name, config, files) |
| 95 | + # Check that the model with original name is changed |
| 96 | + self.assertTrue(pb_utils.is_model_ready(self.model_name, "1")) |
| 97 | + self.assertFalse(pb_utils.is_model_ready(self.model_name, "3")) |
| 98 | + |
| 99 | + # Sanity check readiness of the different named model |
| 100 | + self.assertTrue(pb_utils.is_model_ready(override_name, "1")) |
| 101 | + self.assertFalse(pb_utils.is_model_ready(override_name, "3")) |
| 102 | + |
| 103 | + |
| 104 | +class TritonPythonModel: |
| 105 | + |
| 106 | + def initialize(self, args): |
| 107 | + # Run the unittest during initialization |
| 108 | + test = unittest.main('model', exit=False) |
| 109 | + self.result = test.result.wasSuccessful() |
| 110 | + |
| 111 | + def execute(self, requests): |
| 112 | + responses = [] |
| 113 | + for _ in requests: |
| 114 | + responses.append( |
| 115 | + pb_utils.InferenceResponse([ |
| 116 | + pb_utils.Tensor('OUTPUT0', |
| 117 | + np.array([self.result], dtype=np.float16)) |
| 118 | + ])) |
| 119 | + return responses |
0 commit comments