|
| 1 | +// Copyright 2025, 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 "pb_bls_cancel.h" |
| 28 | + |
| 29 | +#include "pb_stub.h" |
| 30 | + |
| 31 | +namespace triton { namespace backend { namespace python { |
| 32 | + |
| 33 | +void |
| 34 | +PbBLSCancel::SaveToSharedMemory(std::unique_ptr<SharedMemoryManager>& shm_pool) |
| 35 | +{ |
| 36 | + cancel_shm_ = shm_pool->Construct<CancelBLSRequestMessage>(); |
| 37 | + new (&(cancel_shm_.data_->mu)) bi::interprocess_mutex; |
| 38 | + new (&(cancel_shm_.data_->cv)) bi::interprocess_condition; |
| 39 | + cancel_shm_.data_->waiting_on_stub = false; |
| 40 | + cancel_shm_.data_->infer_payload_id = infer_playload_id_; |
| 41 | + cancel_shm_.data_->is_cancelled = is_cancelled_; |
| 42 | +} |
| 43 | + |
| 44 | +bi::managed_external_buffer::handle_t |
| 45 | +PbBLSCancel::ShmHandle() |
| 46 | +{ |
| 47 | + return cancel_shm_.handle_; |
| 48 | +} |
| 49 | + |
| 50 | +CancelBLSRequestMessage* |
| 51 | +PbBLSCancel::ShmPayload() |
| 52 | +{ |
| 53 | + return cancel_shm_.data_.get(); |
| 54 | +} |
| 55 | + |
| 56 | +void |
| 57 | +PbBLSCancel::Cancel() |
| 58 | +{ |
| 59 | + // Release the GIL. Python objects are not accessed during the check. |
| 60 | + py::gil_scoped_release gil_release; |
| 61 | + |
| 62 | + std::unique_lock<std::mutex> lk(mu_); |
| 63 | + // The cancelled flag can only move from false to true, not the other way, so |
| 64 | + // it is checked on each query until cancelled and then implicitly cached. |
| 65 | + if (is_cancelled_) { |
| 66 | + return; |
| 67 | + } |
| 68 | + if (!updating_) { |
| 69 | + std::unique_ptr<Stub>& stub = Stub::GetOrCreateInstance(); |
| 70 | + if (!stub->StubToParentServiceActive()) { |
| 71 | + LOG_ERROR << "Cannot communicate with parent service"; |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + stub->EnqueueCancelBLSDecoupledRequest(this); |
| 76 | + updating_ = true; |
| 77 | + } |
| 78 | + cv_.wait(lk, [this] { return !updating_; }); |
| 79 | +} |
| 80 | + |
| 81 | +void |
| 82 | +PbBLSCancel::ReportIsCancelled(bool is_cancelled) |
| 83 | +{ |
| 84 | + { |
| 85 | + std::lock_guard<std::mutex> lk(mu_); |
| 86 | + is_cancelled_ = is_cancelled; |
| 87 | + updating_ = false; |
| 88 | + } |
| 89 | + cv_.notify_all(); |
| 90 | +} |
| 91 | + |
| 92 | +}}} // namespace triton::backend::python |
0 commit comments