|  | 
|  | 1 | +// Copyright 2021, 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 | +#include "infer_request.h" | 
|  | 28 | + | 
|  | 29 | +#ifdef TRITON_PB_STUB | 
|  | 30 | +#include "infer_response.h" | 
|  | 31 | +#include "pb_stub.h" | 
|  | 32 | +#endif | 
|  | 33 | + | 
|  | 34 | +namespace triton { namespace backend { namespace python { | 
|  | 35 | + | 
|  | 36 | +InferRequest::InferRequest( | 
|  | 37 | +    const std::string& request_id, uint64_t correlation_id, | 
|  | 38 | +    const std::vector<std::shared_ptr<PbTensor>>& inputs, | 
|  | 39 | +    const std::vector<std::string>& requested_output_names, | 
|  | 40 | +    const std::string& model_name, const int64_t model_version) | 
|  | 41 | +    : request_id_(request_id), correlation_id_(correlation_id), inputs_(inputs), | 
|  | 42 | +      requested_output_names_(requested_output_names), model_name_(model_name), | 
|  | 43 | +      model_version_(model_version) | 
|  | 44 | +{ | 
|  | 45 | +} | 
|  | 46 | + | 
|  | 47 | +const std::vector<std::shared_ptr<PbTensor>>& | 
|  | 48 | +InferRequest::Inputs() | 
|  | 49 | +{ | 
|  | 50 | +  return inputs_; | 
|  | 51 | +} | 
|  | 52 | + | 
|  | 53 | +const std::string& | 
|  | 54 | +InferRequest::RequestId() | 
|  | 55 | +{ | 
|  | 56 | +  return request_id_; | 
|  | 57 | +} | 
|  | 58 | + | 
|  | 59 | +uint64_t | 
|  | 60 | +InferRequest::CorrelationId() | 
|  | 61 | +{ | 
|  | 62 | +  return correlation_id_; | 
|  | 63 | +} | 
|  | 64 | + | 
|  | 65 | +const std::vector<std::string>& | 
|  | 66 | +InferRequest::RequestedOutputNames() | 
|  | 67 | +{ | 
|  | 68 | +  return requested_output_names_; | 
|  | 69 | +} | 
|  | 70 | + | 
|  | 71 | +const std::string& | 
|  | 72 | +InferRequest::ModelName() | 
|  | 73 | +{ | 
|  | 74 | +  return model_name_; | 
|  | 75 | +} | 
|  | 76 | + | 
|  | 77 | +int64_t | 
|  | 78 | +InferRequest::ModelVersion() | 
|  | 79 | +{ | 
|  | 80 | +  return model_version_; | 
|  | 81 | +} | 
|  | 82 | + | 
|  | 83 | +void | 
|  | 84 | +InferRequest::SaveToSharedMemory( | 
|  | 85 | +    std::unique_ptr<SharedMemory>& shm_pool, Request* request_shm) | 
|  | 86 | +{ | 
|  | 87 | +  request_shm->correlation_id = this->CorrelationId(); | 
|  | 88 | +  off_t id_offset; | 
|  | 89 | +  SaveStringToSharedMemory(shm_pool, id_offset, this->RequestId().c_str()); | 
|  | 90 | +  request_shm->id = id_offset; | 
|  | 91 | +  request_shm->requested_output_count = this->RequestedOutputNames().size(); | 
|  | 92 | +  off_t requested_output_names_offset; | 
|  | 93 | +  off_t* requested_output_names; | 
|  | 94 | +  shm_pool->Map( | 
|  | 95 | +      (char**)&requested_output_names, | 
|  | 96 | +      sizeof(off_t) * request_shm->requested_output_count, | 
|  | 97 | +      requested_output_names_offset); | 
|  | 98 | + | 
|  | 99 | +  request_shm->requested_output_names = requested_output_names_offset; | 
|  | 100 | +  size_t i = 0; | 
|  | 101 | +  for (auto& requested_output_name : requested_output_names_) { | 
|  | 102 | +    SaveStringToSharedMemory( | 
|  | 103 | +        shm_pool, requested_output_names[i], requested_output_name.c_str()); | 
|  | 104 | +    i++; | 
|  | 105 | +  } | 
|  | 106 | + | 
|  | 107 | +  request_shm->requested_input_count = this->Inputs().size(); | 
|  | 108 | +  request_shm->model_version = this->model_version_; | 
|  | 109 | +  SaveStringToSharedMemory( | 
|  | 110 | +      shm_pool, request_shm->model_name, this->model_name_.c_str()); | 
|  | 111 | +} | 
|  | 112 | + | 
|  | 113 | +std::unique_ptr<InferRequest> | 
|  | 114 | +InferRequest::LoadFromSharedMemory( | 
|  | 115 | +    std::unique_ptr<SharedMemory>& shm_pool, off_t request_offset) | 
|  | 116 | +{ | 
|  | 117 | +  Request* request; | 
|  | 118 | +  shm_pool->MapOffset((char**)&request, request_offset); | 
|  | 119 | + | 
|  | 120 | +  char* id = nullptr; | 
|  | 121 | +  LoadStringFromSharedMemory(shm_pool, request->id, id); | 
|  | 122 | + | 
|  | 123 | +  uint32_t requested_input_count = request->requested_input_count; | 
|  | 124 | + | 
|  | 125 | +  std::vector<std::shared_ptr<PbTensor>> py_input_tensors; | 
|  | 126 | +  for (size_t input_idx = 0; input_idx < requested_input_count; ++input_idx) { | 
|  | 127 | +    std::shared_ptr<PbTensor> pb_input_tensor = PbTensor::LoadFromSharedMemory( | 
|  | 128 | +        shm_pool, request->inputs + sizeof(Tensor) * input_idx); | 
|  | 129 | +    py_input_tensors.emplace_back(std::move(pb_input_tensor)); | 
|  | 130 | +  } | 
|  | 131 | + | 
|  | 132 | +  std::vector<std::string> requested_output_names; | 
|  | 133 | +  uint32_t requested_output_count = request->requested_output_count; | 
|  | 134 | +  off_t* output_names; | 
|  | 135 | +  shm_pool->MapOffset((char**)&output_names, request->requested_output_names); | 
|  | 136 | + | 
|  | 137 | +  for (size_t output_idx = 0; output_idx < requested_output_count; | 
|  | 138 | +       ++output_idx) { | 
|  | 139 | +    char* output_name = nullptr; | 
|  | 140 | +    LoadStringFromSharedMemory(shm_pool, output_names[output_idx], output_name); | 
|  | 141 | +    requested_output_names.emplace_back(output_name); | 
|  | 142 | +  } | 
|  | 143 | + | 
|  | 144 | +  char* model_name; | 
|  | 145 | +  LoadStringFromSharedMemory(shm_pool, request->model_name, model_name); | 
|  | 146 | +  return std::make_unique<InferRequest>( | 
|  | 147 | +      id, request->correlation_id, std::move(py_input_tensors), | 
|  | 148 | +      requested_output_names, model_name, request->model_version); | 
|  | 149 | +} | 
|  | 150 | + | 
|  | 151 | +}}}  // namespace triton::backend::python | 
0 commit comments