Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add nanobind
Signed-off-by: Shunkang <182541032+Shunkangz@users.noreply.github.co>
  • Loading branch information
Shunkang authored and Shunkang committed Sep 4, 2025
commit b487a5a23cc2334eafe0e1d7480b25f847faecbc
3 changes: 3 additions & 0 deletions cpp/tensorrt_llm/nanobind/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "tensorrt_llm/nanobind/batch_manager/kvCacheConnector.h"
#include "tensorrt_llm/nanobind/batch_manager/kvCacheManager.h"
#include "tensorrt_llm/nanobind/batch_manager/llmRequest.h"
#include "tensorrt_llm/nanobind/common/tllmExceptions.h"
#include "tensorrt_llm/nanobind/executor/bindings.h"
#include "tensorrt_llm/nanobind/runtime/bindings.h"
#include "tensorrt_llm/nanobind/testing/modelSpecBinding.h"
Expand Down Expand Up @@ -127,9 +128,11 @@ NB_MODULE(TRTLLM_NB_MODULE, m)
auto mInternalTesting = mInternal.def_submodule("testing", "Testing internal bindings");
auto mInternalBatchManager = mInternal.def_submodule("batch_manager", "Batch manager internal bindings");
auto mInternalThop = mInternal.def_submodule("thop", "Torch op internal bindings");
auto mExceptions = m.def_submodule("exceptions", "Exceptions internal bindings");

tensorrt_llm::nanobind::executor::initBindings(mExecutor);
tensorrt_llm::nanobind::runtime::initBindingsEarly(mInternalRuntime);
tensorrt_llm::nanobind::common::initExceptionsBindings(mExceptions);
tensorrt_llm::nanobind::thop::initBindings(mInternalThop);

auto buildInfo = m.def_submodule("BuildInfo");
Expand Down
67 changes: 67 additions & 0 deletions cpp/tensorrt_llm/nanobind/common/tllmExceptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2022-2024, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "tllmExceptions.h"
#include "tensorrt_llm/common/tllmException.h"
#include <nanobind/nanobind.h>

namespace tc = tensorrt_llm::common;
namespace nb = nanobind;

namespace tensorrt_llm::nanobind::common
{

void initExceptionsBindings(nb::module_& m)
{
// Bind the RequestErrorCode enum
nb::enum_<tc::RequestErrorCode>(m, "RequestErrorCode")
.value("UNKNOWN_ERROR", tc::RequestErrorCode::kUNKNOWN_ERROR)
.value("NETWORK_ERROR", tc::RequestErrorCode::kNETWORK_ERROR)
.export_values();

// Create the RequestSpecificException Python exception class
static nb::object request_specific_exc = nb::exception<tc::RequestSpecificException>(m, "RequestSpecificException");

// Add attributes to the Python exception class
request_specific_exc.attr("request_id") = nb::none;
request_specific_exc.attr("error_code") = nb::none;

// Register exception translator to convert C++ exceptions to Python
nb::register_exception_translator(
[](std::exception_ptr const& p, void*)
{
try
{
if (p)
std::rethrow_exception(p);
}
catch (const tc::RequestSpecificException& e)
{
// Create a Python exception with the request ID and error code information
nb::object py_exc = nb::cast(e);
nb::object request_id = nb::cast(e.getRequestId());
nb::object error_code = nb::cast(static_cast<uint32_t>(e.getErrorCode()));

// Set additional attributes on the exception
py_exc.attr("request_id") = request_id;
py_exc.attr("error_code") = error_code;

PyErr_SetObject(request_specific_exc.ptr(), py_exc.ptr());
}
});
}

} // namespace tensorrt_llm::nanobind::common
27 changes: 27 additions & 0 deletions cpp/tensorrt_llm/nanobind/common/tllmExceptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2022-2024, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once
#include <nanobind/nanobind.h>

namespace nb = nanobind;

namespace tensorrt_llm::nanobind::common
{

void initExceptionsBindings(nb::module_& m);

} // namespace tensorrt_llm::nanobind::common