|
| 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 | +from torch.utils.dlpack import from_dlpack |
| 31 | + |
| 32 | + |
| 33 | +class PBBLSONNXWarmupTest(unittest.TestCase): |
| 34 | + |
| 35 | + def test_onnx_output_mem_type(self): |
| 36 | + input0_np = np.random.randn(*[16]) |
| 37 | + input0_np = input0_np.astype(np.float32) |
| 38 | + input1_np = np.random.randn(*[16]) |
| 39 | + input1_np = input1_np.astype(np.float32) |
| 40 | + input0 = pb_utils.Tensor('INPUT0', input0_np) |
| 41 | + input1 = pb_utils.Tensor('INPUT1', input1_np) |
| 42 | + infer_request = pb_utils.InferenceRequest( |
| 43 | + model_name='onnx_nobatch_float32_float32_float32', |
| 44 | + inputs=[input0, input1], |
| 45 | + requested_output_names=['OUTPUT0', 'OUTPUT1']) |
| 46 | + |
| 47 | + infer_response = infer_request.exec() |
| 48 | + |
| 49 | + self.assertFalse(infer_response.has_error()) |
| 50 | + |
| 51 | + output0 = pb_utils.get_output_tensor_by_name(infer_response, 'OUTPUT0') |
| 52 | + output1 = pb_utils.get_output_tensor_by_name(infer_response, 'OUTPUT1') |
| 53 | + |
| 54 | + self.assertIsNotNone(output0) |
| 55 | + self.assertIsNotNone(output1) |
| 56 | + |
| 57 | + # The memory type of output tensor should be GPU |
| 58 | + self.assertFalse(output0.is_cpu()) |
| 59 | + self.assertFalse(output1.is_cpu()) |
| 60 | + |
| 61 | + expected_output_0 = input0.as_numpy() - input1.as_numpy() |
| 62 | + expected_output_1 = input0.as_numpy() + input1.as_numpy() |
| 63 | + |
| 64 | + output0 = from_dlpack( |
| 65 | + output0.to_dlpack()).to('cpu').cpu().detach().numpy() |
| 66 | + output1 = from_dlpack( |
| 67 | + output1.to_dlpack()).to('cpu').cpu().detach().numpy() |
| 68 | + |
| 69 | + self.assertTrue(np.all(output0 == expected_output_0)) |
| 70 | + self.assertTrue(np.all(output1 == expected_output_1)) |
| 71 | + |
| 72 | + |
| 73 | +class TritonPythonModel: |
| 74 | + |
| 75 | + def execute(self, requests): |
| 76 | + responses = [] |
| 77 | + for _ in requests: |
| 78 | + # Run the unittest and store the results in InferenceResponse. |
| 79 | + test = unittest.main('model', exit=False) |
| 80 | + responses.append( |
| 81 | + pb_utils.InferenceResponse([ |
| 82 | + pb_utils.Tensor( |
| 83 | + 'OUTPUT0', |
| 84 | + np.array([test.result.wasSuccessful()], |
| 85 | + dtype=np.float16)) |
| 86 | + ])) |
| 87 | + return responses |
0 commit comments