| 
 | 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 | +#include "infer_trace.h"  | 
 | 28 | + | 
 | 29 | +namespace triton { namespace backend { namespace python {  | 
 | 30 | + | 
 | 31 | +InferenceTrace::InferenceTrace(const InferenceTrace& rhs)  | 
 | 32 | +{  | 
 | 33 | +  triton_trace_ = rhs.triton_trace_;  | 
 | 34 | +  trace_context_ = rhs.trace_context_;  | 
 | 35 | +}  | 
 | 36 | + | 
 | 37 | +InferenceTrace&  | 
 | 38 | +InferenceTrace::operator=(const InferenceTrace& rhs)  | 
 | 39 | +{  | 
 | 40 | +  triton_trace_ = rhs.triton_trace_;  | 
 | 41 | +  trace_context_ = rhs.trace_context_;  | 
 | 42 | +  return *this;  | 
 | 43 | +}  | 
 | 44 | + | 
 | 45 | +InferenceTrace::InferenceTrace(std::unique_ptr<InferenceTrace>& trace_shm)  | 
 | 46 | +{  | 
 | 47 | +  triton_trace_ = trace_shm->triton_trace_;  | 
 | 48 | +  trace_context_ = trace_shm->trace_context_;  | 
 | 49 | +}  | 
 | 50 | + | 
 | 51 | +void  | 
 | 52 | +InferenceTrace::SaveToSharedMemory(  | 
 | 53 | +    std::unique_ptr<SharedMemoryManager>& shm_pool)  | 
 | 54 | +{  | 
 | 55 | +  AllocatedSharedMemory<InferenceTraceShm> infer_trace_shm =  | 
 | 56 | +      shm_pool->Construct<InferenceTraceShm>();  | 
 | 57 | +  infer_trace_shm_ptr_ = infer_trace_shm.data_.get();  | 
 | 58 | + | 
 | 59 | +  infer_trace_shm_ptr_->triton_trace = triton_trace_;  | 
 | 60 | + | 
 | 61 | +  std::unique_ptr<PbString> trace_context_shm =  | 
 | 62 | +      PbString::Create(shm_pool, trace_context_);  | 
 | 63 | + | 
 | 64 | +  infer_trace_shm_ptr_->trace_context_shm_handle =  | 
 | 65 | +      trace_context_shm->ShmHandle();  | 
 | 66 | + | 
 | 67 | +  // Save the references to shared memory.  | 
 | 68 | +  trace_context_shm_ = std::move(trace_context_shm);  | 
 | 69 | +  infer_trace_shm_ = std::move(infer_trace_shm);  | 
 | 70 | +  shm_handle_ = infer_trace_shm_.handle_;  | 
 | 71 | +}  | 
 | 72 | + | 
 | 73 | +std::unique_ptr<InferenceTrace>  | 
 | 74 | +InferenceTrace::LoadFromSharedMemory(  | 
 | 75 | +    std::unique_ptr<SharedMemoryManager>& shm_pool,  | 
 | 76 | +    bi::managed_external_buffer::handle_t handle)  | 
 | 77 | +{  | 
 | 78 | +  AllocatedSharedMemory<InferenceTraceShm> infer_trace_shm =  | 
 | 79 | +      shm_pool->Load<InferenceTraceShm>(handle);  | 
 | 80 | +  InferenceTraceShm* infer_trace_shm_ptr = infer_trace_shm.data_.get();  | 
 | 81 | + | 
 | 82 | +  std::unique_ptr<PbString> trace_context_shm = PbString::LoadFromSharedMemory(  | 
 | 83 | +      shm_pool, infer_trace_shm_ptr->trace_context_shm_handle);  | 
 | 84 | + | 
 | 85 | +  return std::unique_ptr<InferenceTrace>(  | 
 | 86 | +      new InferenceTrace(infer_trace_shm, trace_context_shm));  | 
 | 87 | +}  | 
 | 88 | + | 
 | 89 | +InferenceTrace::InferenceTrace(  | 
 | 90 | +    AllocatedSharedMemory<InferenceTraceShm>& infer_trace_shm,  | 
 | 91 | +    std::unique_ptr<PbString>& trace_context_shm)  | 
 | 92 | +    : infer_trace_shm_(std::move(infer_trace_shm)),  | 
 | 93 | +      trace_context_shm_(std::move(trace_context_shm))  | 
 | 94 | +{  | 
 | 95 | +  infer_trace_shm_ptr_ = infer_trace_shm_.data_.get();  | 
 | 96 | +  shm_handle_ = infer_trace_shm_.handle_;  | 
 | 97 | +  triton_trace_ = infer_trace_shm_ptr_->triton_trace;  | 
 | 98 | +  trace_context_ = trace_context_shm_->String();  | 
 | 99 | +}  | 
 | 100 | + | 
 | 101 | +}}};  // namespace triton::backend::python  | 
0 commit comments