|
| 1 | +# Copyright 2022-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 json |
| 28 | + |
| 29 | +import triton_python_backend_utils as pb_utils |
| 30 | +import numpy as np |
| 31 | + |
| 32 | + |
| 33 | +class TritonPythonModel: |
| 34 | + def initialize(self, args): |
| 35 | + self.model_config = json.loads(args["model_config"]) |
| 36 | + self.decoupled = self.model_config.get("model_transaction_policy", {}).get( |
| 37 | + "decoupled" |
| 38 | + ) |
| 39 | + print(f"{self.decoupled=}") |
| 40 | + |
| 41 | + def execute(self, requests): |
| 42 | + if self.decoupled: |
| 43 | + return self.exec_decoupled(requests) |
| 44 | + else: |
| 45 | + return self.exec(requests) |
| 46 | + |
| 47 | + def exec(self, requests): |
| 48 | + responses = [] |
| 49 | + for request in requests: |
| 50 | + params = json.loads(request.parameters()) |
| 51 | + rep_count = params["REPETITION"] if "REPETITION" in params else 1 |
| 52 | + |
| 53 | + input_np = pb_utils.get_input_tensor_by_name(request, "PROMPT").as_numpy() |
| 54 | + stream_np = pb_utils.get_input_tensor_by_name(request, "STREAM").as_numpy() |
| 55 | + stream = stream_np.flatten()[0] |
| 56 | + if stream: |
| 57 | + responses.append( |
| 58 | + pb_utils.InferenceResponse( |
| 59 | + error=pb_utils.TritonError( |
| 60 | + "STREAM only supported in decoupled mode" |
| 61 | + ) |
| 62 | + ) |
| 63 | + ) |
| 64 | + else: |
| 65 | + out_tensor = pb_utils.Tensor("TEXT", np.repeat(input_np, rep_count, axis=1)) |
| 66 | + responses.append(pb_utils.InferenceResponse([out_tensor])) |
| 67 | + return responses |
| 68 | + |
| 69 | + def exec_decoupled(self, requests): |
| 70 | + for request in requests: |
| 71 | + params = json.loads(request.parameters()) |
| 72 | + rep_count = params["REPETITION"] if "REPETITION" in params else 1 |
| 73 | + |
| 74 | + sender = request.get_response_sender() |
| 75 | + input_np = pb_utils.get_input_tensor_by_name(request, "PROMPT").as_numpy() |
| 76 | + stream_np = pb_utils.get_input_tensor_by_name(request, "STREAM").as_numpy() |
| 77 | + out_tensor = pb_utils.Tensor("TEXT", input_np) |
| 78 | + response = pb_utils.InferenceResponse([out_tensor]) |
| 79 | + # If stream enabled, just send multiple copies of response |
| 80 | + # FIXME: Could split up response string into tokens, but this is simpler for now. |
| 81 | + stream = stream_np.flatten()[0] |
| 82 | + if stream: |
| 83 | + for _ in range(rep_count): |
| 84 | + sender.send(response) |
| 85 | + sender.send(None, flags=pb_utils.TRITONSERVER_RESPONSE_COMPLETE_FINAL) |
| 86 | + # If stream disabled, just send one response |
| 87 | + else: |
| 88 | + sender.send( |
| 89 | + response, flags=pb_utils.TRITONSERVER_RESPONSE_COMPLETE_FINAL |
| 90 | + ) |
| 91 | + return None |
0 commit comments