Skip to content

feat: add BLS decoupled response iterator's cancel() method for the request cancellation #398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,4 @@ dmypy.json

# vscode
.vscode/settings.json
.vscode/c_cpp_properties.json
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ set(
src/pb_response_iterator.cc
src/pb_cancel.cc
src/pb_cancel.h
src/pb_bls_cancel.cc
src/pb_bls_cancel.h
)

list(APPEND
Expand Down
55 changes: 53 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1409,14 +1409,65 @@ class TritonPythonModel:
A complete example for sync and async BLS for decoupled models is included in
the [Examples](#examples) section.

Note: Async BLS is not supported on Python 3.6 or lower due to the `async`
keyword and `asyncio.run` being introduced in Python 3.7.

Starting from the 22.04 release, the lifetime of the BLS output tensors have
been improved such that if a tensor is no longer needed in your Python model it
will be automatically deallocated. This can increase the number of BLS requests
that you can execute in your model without running into the out of GPU or
shared memory error.

Note: Async BLS is not supported on Python 3.6 or lower due to the `async`
keyword and `asyncio.run` being introduced in Python 3.7.
Starting from the 25.04 release, you can use the `infer_responses.cancel()` function
on a BLS decoupled response iterator to stop the response stream, which cancels
the request to the decoupled model. This is useful for stopping long inference
requests, such as those from auto-generative large language models, which may
run for an indeterminate amount of time and consume significant server resources.
The response iterator can be generated from `infer_request.exec(decoupled=True)`
and `infer_request.async_exec(decoupled=True)` functions:

```python
import triton_python_backend_utils as pb_utils

class TritonPythonModel:
...
def execute(self, requests):
...
inference_request = pb_utils.InferenceRequest(
model_name='model_name',
requested_output_names=['REQUESTED_OUTPUT'],
inputs=[<pb_utils.Tensor object>])

# Execute the inference_request and wait for the response. Here we are
# running a BLS request on a decoupled model, hence setting the parameter
# 'decoupled' to 'True'.
infer_responses = infer_request.exec(decoupled=True)

response_tensors_received = []
for infer_response in infer_responses:
# Check if the inference response indicates an error.
# vLLM backend uses the CANCELLED error code when a request is cancelled.
# TensorRT-LLM backend does not use error codes; instead, it sends the
# TRITONSERVER_RESPONSE_COMPLETE_FINAL flag to the iterator.
if inference_response.has_error():
if infer_response.error().code() == pb_utils.TritonError.CANCELLED:
print("request has been cancelled.")
break

# Collect the output tensor from the model's response
output = pb_utils.get_output_tensor_by_name(
inference_response, 'REQUESTED_OUTPUT')
response_tensors_received.append(output)

# Check if we have received enough inference output tensors
# and then cancel the response iterator
if has_enough_response(response_tensors_received):
infer_responses.cancel()
```

Note: Whether the decoupled model returns a cancellation error and stops executing
the request depends on the model's backend implementation. Please refer to the
documentation for more details [Handing in Backend](https://github.com/triton-inference-server/server/blob/main/docs/user_guide/request_cancellation.md#handling-in-backend)

## Model Loading API

Expand Down
17 changes: 15 additions & 2 deletions src/infer_payload.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// Copyright 2023-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -31,7 +31,8 @@ namespace triton { namespace backend { namespace python {
InferPayload::InferPayload(
const bool is_decoupled,
std::function<void(std::unique_ptr<InferResponse>)> callback)
: is_decoupled_(is_decoupled), is_promise_set_(false), callback_(callback)
: is_decoupled_(is_decoupled), is_promise_set_(false), callback_(callback),
request_address_(reinterpret_cast<intptr_t>(nullptr))
{
promise_.reset(new std::promise<std::unique_ptr<InferResponse>>());
}
Expand Down Expand Up @@ -91,4 +92,16 @@ InferPayload::ResponseAllocUserp()
return response_alloc_userp_;
}

void
InferPayload::SetRequestAddress(intptr_t request_address)
{
request_address_ = request_address;
}

intptr_t
InferPayload::GetRequestAddress()
{
return request_address_;
}

}}} // namespace triton::backend::python
5 changes: 4 additions & 1 deletion src/infer_payload.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// Copyright 2023-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -62,6 +62,8 @@ class InferPayload : public std::enable_shared_from_this<InferPayload> {
void SetResponseAllocUserp(
const ResponseAllocatorUserp& response_alloc_userp);
std::shared_ptr<ResponseAllocatorUserp> ResponseAllocUserp();
void SetRequestAddress(intptr_t request_address);
intptr_t GetRequestAddress();

private:
std::unique_ptr<std::promise<std::unique_ptr<InferResponse>>> promise_;
Expand All @@ -70,6 +72,7 @@ class InferPayload : public std::enable_shared_from_this<InferPayload> {
bool is_promise_set_;
std::function<void(std::unique_ptr<InferResponse>)> callback_;
std::shared_ptr<ResponseAllocatorUserp> response_alloc_userp_;
intptr_t request_address_;
};

}}} // namespace triton::backend::python
2 changes: 1 addition & 1 deletion src/infer_response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ InferResponse::SaveToSharedMemory(
response_shm_ptr->is_error_set = false;
shm_handle_ = response_shm_.handle_;
response_shm_ptr->is_last_response = is_last_response_;
response_shm_ptr->id = id_;

// Only save the output tensors to shared memory when the inference response
// doesn't have error.
Expand All @@ -113,7 +114,6 @@ InferResponse::SaveToSharedMemory(
tensor_handle_shm_ptr[j] = output_tensor->ShmHandle();
j++;
}
response_shm_ptr->id = id_;

parameters_shm_ = PbString::Create(shm_pool, parameters_);
response_shm_ptr->parameters = parameters_shm_->ShmHandle();
Expand Down
5 changes: 3 additions & 2 deletions src/ipc_message.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// Copyright 2021-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -67,7 +67,8 @@ typedef enum PYTHONSTUB_commandtype_enum {
PYTHONSTUB_LoadModelRequest,
PYTHONSTUB_UnloadModelRequest,
PYTHONSTUB_ModelReadinessRequest,
PYTHONSTUB_IsRequestCancelled
PYTHONSTUB_IsRequestCancelled,
PYTHONSTUB_CancelBLSDecoupledInferRequest
} PYTHONSTUB_CommandType;

///
Expand Down
92 changes: 92 additions & 0 deletions src/pb_bls_cancel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "pb_bls_cancel.h"

#include "pb_stub.h"

namespace triton { namespace backend { namespace python {

void
PbBLSCancel::SaveToSharedMemory(std::unique_ptr<SharedMemoryManager>& shm_pool)
{
cancel_shm_ = shm_pool->Construct<CancelBLSRequestMessage>();
new (&(cancel_shm_.data_->mu)) bi::interprocess_mutex;
new (&(cancel_shm_.data_->cv)) bi::interprocess_condition;
cancel_shm_.data_->waiting_on_stub = false;
cancel_shm_.data_->infer_payload_id = infer_playload_id_;
cancel_shm_.data_->is_cancelled = is_cancelled_;
}

bi::managed_external_buffer::handle_t
PbBLSCancel::ShmHandle()
{
return cancel_shm_.handle_;
}

CancelBLSRequestMessage*
PbBLSCancel::ShmPayload()
{
return cancel_shm_.data_.get();
}

void
PbBLSCancel::Cancel()
{
// Release the GIL. Python objects are not accessed during the check.
py::gil_scoped_release gil_release;

std::unique_lock<std::mutex> lk(mu_);
// The cancelled flag can only move from false to true, not the other way, so
// it is checked on each query until cancelled and then implicitly cached.
if (is_cancelled_) {
return;
}
if (!updating_) {
std::unique_ptr<Stub>& stub = Stub::GetOrCreateInstance();
if (!stub->StubToParentServiceActive()) {
LOG_ERROR << "Cannot communicate with parent service";
return;
}

stub->EnqueueCancelBLSDecoupledRequest(this);
updating_ = true;
}
cv_.wait(lk, [this] { return !updating_; });
}

void
PbBLSCancel::ReportIsCancelled(bool is_cancelled)
{
{
std::lock_guard<std::mutex> lk(mu_);
is_cancelled_ = is_cancelled;
updating_ = false;
}
cv_.notify_all();
}

}}} // namespace triton::backend::python
63 changes: 63 additions & 0 deletions src/pb_bls_cancel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#pragma once

#include <condition_variable>
#include <mutex>

#include "pb_utils.h"

namespace triton { namespace backend { namespace python {

class PbBLSCancel {
public:
PbBLSCancel(void* infer_playload_id)
: updating_(false), infer_playload_id_(infer_playload_id),
is_cancelled_(false)
{
}
DISALLOW_COPY_AND_ASSIGN(PbBLSCancel);

void SaveToSharedMemory(std::unique_ptr<SharedMemoryManager>& shm_pool);
bi::managed_external_buffer::handle_t ShmHandle();
CancelBLSRequestMessage* ShmPayload();

void Cancel();
void ReportIsCancelled(bool is_cancelled);

private:
AllocatedSharedMemory<CancelBLSRequestMessage> cancel_shm_;

std::mutex mu_;
std::condition_variable cv_;
bool updating_;

void* infer_playload_id_;
bool is_cancelled_;
};

}}}; // namespace triton::backend::python
11 changes: 10 additions & 1 deletion src/pb_response_iterator.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// Copyright 2023-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -40,6 +40,7 @@ ResponseIterator::ResponseIterator(
: id_(response->Id()), is_finished_(false), is_cleared_(false), idx_(0)
{
response_buffer_.push(response);
pb_bls_cancel_ = std::make_shared<PbBLSCancel>(response->Id());
}

ResponseIterator::~ResponseIterator()
Expand Down Expand Up @@ -159,4 +160,12 @@ ResponseIterator::GetExistingResponses()
return responses;
}

void
ResponseIterator::Cancel()
{
if (!is_finished_) {
pb_bls_cancel_->Cancel();
}
}

}}} // namespace triton::backend::python
5 changes: 4 additions & 1 deletion src/pb_response_iterator.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// Copyright 2023-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -29,6 +29,7 @@
#include <queue>

#include "infer_response.h"
#include "pb_bls_cancel.h"

namespace triton { namespace backend { namespace python {

Expand All @@ -43,6 +44,7 @@ class ResponseIterator {
void* Id();
void Clear();
std::vector<std::shared_ptr<InferResponse>> GetExistingResponses();
void Cancel();

private:
std::vector<std::shared_ptr<InferResponse>> responses_;
Expand All @@ -53,6 +55,7 @@ class ResponseIterator {
bool is_finished_;
bool is_cleared_;
size_t idx_;
std::shared_ptr<PbBLSCancel> pb_bls_cancel_;
};

}}} // namespace triton::backend::python
Loading
Loading