|
| 1 | +# Copyright (c) 2022 PaddlePaddle Authors. 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 | + |
| 15 | +import json |
| 16 | +import numpy as np |
| 17 | +import time |
| 18 | + |
| 19 | +import fastdeploy as fd |
| 20 | + |
| 21 | +# triton_python_backend_utils is available in every Triton Python model. You |
| 22 | +# need to use this module to create inference requests and responses. It also |
| 23 | +# contains some utility functions for extracting information from model_config |
| 24 | +# and converting Triton input/output types to numpy types. |
| 25 | +import triton_python_backend_utils as pb_utils |
| 26 | + |
| 27 | + |
| 28 | +class TritonPythonModel: |
| 29 | + """Your Python model must use the same class name. Every Python model |
| 30 | + that is created must have "TritonPythonModel" as the class name. |
| 31 | + """ |
| 32 | + |
| 33 | + def initialize(self, args): |
| 34 | + """`initialize` is called only once when the model is being loaded. |
| 35 | + Implementing `initialize` function is optional. This function allows |
| 36 | + the model to intialize any state associated with this model. |
| 37 | + Parameters |
| 38 | + ---------- |
| 39 | + args : dict |
| 40 | + Both keys and values are strings. The dictionary keys and values are: |
| 41 | + * model_config: A JSON string containing the model configuration |
| 42 | + * model_instance_kind: A string containing model instance kind |
| 43 | + * model_instance_device_id: A string containing model instance device ID |
| 44 | + * model_repository: Model repository path |
| 45 | + * model_version: Model version |
| 46 | + * model_name: Model name |
| 47 | + """ |
| 48 | + # You must parse model_config. JSON string is not parsed here |
| 49 | + self.model_config = json.loads(args['model_config']) |
| 50 | + print("model_config:", self.model_config) |
| 51 | + |
| 52 | + self.input_names = [] |
| 53 | + for input_config in self.model_config["input"]: |
| 54 | + self.input_names.append(input_config["name"]) |
| 55 | + print("postprocess input names:", self.input_names) |
| 56 | + |
| 57 | + self.output_names = [] |
| 58 | + self.output_dtype = [] |
| 59 | + for output_config in self.model_config["output"]: |
| 60 | + self.output_names.append(output_config["name"]) |
| 61 | + dtype = pb_utils.triton_string_to_numpy(output_config["data_type"]) |
| 62 | + self.output_dtype.append(dtype) |
| 63 | + print("postprocess output names:", self.output_names) |
| 64 | + |
| 65 | + self.postprocess_ = fd.vision.detection.PaddleDetPostprocessor() |
| 66 | + |
| 67 | + def execute(self, requests): |
| 68 | + """`execute` must be implemented in every Python model. `execute` |
| 69 | + function receives a list of pb_utils.InferenceRequest as the only |
| 70 | + argument. This function is called when an inference is requested |
| 71 | + for this model. Depending on the batching configuration (e.g. Dynamic |
| 72 | + Batching) used, `requests` may contain multiple requests. Every |
| 73 | + Python model, must create one pb_utils.InferenceResponse for every |
| 74 | + pb_utils.InferenceRequest in `requests`. If there is an error, you can |
| 75 | + set the error argument when creating a pb_utils.InferenceResponse. |
| 76 | + Parameters |
| 77 | + ---------- |
| 78 | + requests : list |
| 79 | + A list of pb_utils.InferenceRequest |
| 80 | + Returns |
| 81 | + ------- |
| 82 | + list |
| 83 | + A list of pb_utils.InferenceResponse. The length of this list must |
| 84 | + be the same as `requests` |
| 85 | + """ |
| 86 | + responses = [] |
| 87 | + for request in requests: |
| 88 | + infer_outputs = [] |
| 89 | + for name in self.input_names: |
| 90 | + infer_output = pb_utils.get_input_tensor_by_name(request, name) |
| 91 | + if infer_output: |
| 92 | + infer_output = infer_output.as_numpy() |
| 93 | + infer_outputs.append(infer_output) |
| 94 | + |
| 95 | + results = self.postprocess_.run(infer_outputs) |
| 96 | + r_str = fd.vision.utils.fd_result_to_json(results) |
| 97 | + |
| 98 | + r_np = np.array(r_str, dtype=np.object) |
| 99 | + out_tensor = pb_utils.Tensor(self.output_names[0], r_np) |
| 100 | + inference_response = pb_utils.InferenceResponse( |
| 101 | + output_tensors=[out_tensor, ]) |
| 102 | + responses.append(inference_response) |
| 103 | + return responses |
| 104 | + |
| 105 | + def finalize(self): |
| 106 | + """`finalize` is called only once when the model is being unloaded. |
| 107 | + Implementing `finalize` function is optional. This function allows |
| 108 | + the model to perform any necessary clean ups before exit. |
| 109 | + """ |
| 110 | + print('Cleaning up...') |
0 commit comments