|
| 1 | +# Copyright 2024, 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 json |
| 28 | + |
| 29 | +import torch |
| 30 | +import triton_python_backend_utils as pb_utils |
| 31 | + |
| 32 | + |
| 33 | +class TritonPythonModel: |
| 34 | + def initialize(self, args): |
| 35 | + # You must parse model_config. JSON string is not parsed here |
| 36 | + self.model_config = json.loads(args["model_config"]) |
| 37 | + |
| 38 | + # Get tensor configurations for testing/validation |
| 39 | + self.input0_config = pb_utils.get_input_config_by_name( |
| 40 | + self.model_config, "INPUT0" |
| 41 | + ) |
| 42 | + self.output0_config = pb_utils.get_output_config_by_name( |
| 43 | + self.model_config, "OUTPUT0" |
| 44 | + ) |
| 45 | + |
| 46 | + def validate_bf16_tensor(self, tensor, tensor_config): |
| 47 | + # I/O datatypes can be queried from the model config if needed |
| 48 | + dtype = tensor_config["data_type"] |
| 49 | + if dtype != "TYPE_BF16": |
| 50 | + raise Exception(f"Expected a BF16 tensor, but got {dtype} instead.") |
| 51 | + |
| 52 | + # Converting BF16 tensors to numpy is not supported, and DLPack |
| 53 | + # should be used instead via to_dlpack and from_dlpack. |
| 54 | + try: |
| 55 | + _ = tensor.as_numpy() |
| 56 | + except pb_utils.TritonModelException as e: |
| 57 | + expected_error = "tensor dtype is bf16 and cannot be converted to numpy" |
| 58 | + assert expected_error in str(e).lower() |
| 59 | + else: |
| 60 | + raise Exception("Expected BF16 conversion to numpy to fail") |
| 61 | + |
| 62 | + def execute(self, requests): |
| 63 | + """ |
| 64 | + Identity model in Python backend with example BF16 and PyTorch usage. |
| 65 | + """ |
| 66 | + responses = [] |
| 67 | + for request in requests: |
| 68 | + input_tensor = pb_utils.get_input_tensor_by_name(request, "INPUT0") |
| 69 | + |
| 70 | + # Numpy does not support BF16, so use DLPack instead. |
| 71 | + bf16_dlpack = input_tensor.to_dlpack() |
| 72 | + |
| 73 | + # OPTIONAL: The tensor can be converted to other dlpack-compatible |
| 74 | + # frameworks like PyTorch and TensorFlow with their dlpack utilities. |
| 75 | + torch_tensor = torch.utils.dlpack.from_dlpack(bf16_dlpack) |
| 76 | + |
| 77 | + # When complete, convert back to a pb_utils.Tensor via DLPack. |
| 78 | + output_tensor = pb_utils.Tensor.from_dlpack( |
| 79 | + "OUTPUT0", torch.utils.dlpack.to_dlpack(torch_tensor) |
| 80 | + ) |
| 81 | + responses.append(pb_utils.InferenceResponse([output_tensor])) |
| 82 | + |
| 83 | + # NOTE: The following helper function is for testing and example |
| 84 | + # purposes only, you should remove this in practice. |
| 85 | + self.validate_bf16_tensor(input_tensor, self.input0_config) |
| 86 | + self.validate_bf16_tensor(output_tensor, self.output0_config) |
| 87 | + |
| 88 | + return responses |
0 commit comments