|
1 | 1 | /* |
2 | | - * SPDX-FileCopyrightText: Copyright (c) 2022-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2022-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
4 | 4 | * |
5 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
26 | 26 | #include "tensorrt_llm/runtime/cudaStream.h" |
27 | 27 |
|
28 | 28 | #include <nanobind/nanobind.h> |
| 29 | +#include <nanobind/ndarray.h> |
29 | 30 | #include <nanobind/stl/chrono.h> |
30 | 31 | #include <nanobind/stl/function.h> |
31 | 32 | #include <nanobind/stl/list.h> |
@@ -645,47 +646,69 @@ void initRequestBindings(nb::module_& m) |
645 | 646 | nb::cast<std::optional<tle::CacheSaltIDType>>(state[33]), nb::cast<std::optional<tle::IdType>>(state[34])); |
646 | 647 | }; |
647 | 648 |
|
| 649 | + // Convert input_token_ids to VecTokens. Fast path: a 1-D contiguous int32 |
| 650 | + // ndarray is memcpy'd into the vector (no per-element PyLong cast, which is |
| 651 | + // O(ISL) on the GIL-held submit path). Anything else (list[int], etc.) falls |
| 652 | + // back to the default nanobind sequence cast, so this is fully back-compatible. |
| 653 | + // This complements PR #15134 (which bytes-encodes Request *pickling*); here we |
| 654 | + // target Request *construction* on the RpcWorker.submit / _enqueue_request path. |
| 655 | + auto toVecTokens = [](nb::handle ids) -> tle::VecTokens |
| 656 | + { |
| 657 | + nb::ndarray<int32_t const, nb::ndim<1>, nb::c_contig> arr; |
| 658 | + if (nb::try_cast(ids, arr, /*convert=*/false)) |
| 659 | + { |
| 660 | + tle::VecTokens out(arr.shape(0)); |
| 661 | + if (arr.shape(0) > 0) |
| 662 | + { |
| 663 | + std::memcpy(out.data(), arr.data(), arr.shape(0) * sizeof(int32_t)); |
| 664 | + } |
| 665 | + return out; |
| 666 | + } |
| 667 | + return nb::cast<tle::VecTokens>(ids); |
| 668 | + }; |
| 669 | + |
648 | 670 | nb::class_<tle::Request> request(m, "Request", nb::dynamic_attr()); |
649 | 671 | request |
650 | | - .def(nb::init<tle::VecTokens, // inputTokenIds |
651 | | - tle::SizeType32, // maxTokens |
652 | | - bool, // streaming |
653 | | - tle::SamplingConfig const&, // samplingConfig |
654 | | - tle::OutputConfig const&, // outputConfig |
655 | | - std::optional<tle::SizeType32> const&, // endId |
656 | | - std::optional<tle::SizeType32> const&, // padId |
657 | | - std::optional<std::vector<SizeType32>>, // positionIds |
658 | | - std::optional<std::list<tle::VecTokens>>, // badWords |
659 | | - std::optional<std::list<tle::VecTokens>>, // stopWords |
660 | | - std::optional<tle::Tensor>, // embeddingBias |
661 | | - std::optional<tle::ExternalDraftTokensConfig>, // externalDraftTokensConfig |
662 | | - std::optional<tle::PromptTuningConfig>, // pTuningConfig |
663 | | - std::optional<tle::MultimodalInput>, // multimodalInput |
664 | | - std::optional<tle::Tensor>, // multimodalEmbedding |
665 | | - std::optional<tle::MropeConfig>, // mRopeConfig |
666 | | - std::optional<tle::LoraConfig>, // loraConfig |
667 | | - std::optional<tle::LookaheadDecodingConfig>, // lookaheadConfig |
668 | | - std::optional<tle::KvCacheRetentionConfig>, // kvCacheRetentionConfig |
669 | | - std::optional<std::string>, // logitsPostProcessorName |
670 | | - std::optional<tle::LogitsPostProcessor>, // logitsPostProcessor |
671 | | - std::optional<tle::VecTokens>, // encoderInputTokenIds |
672 | | - std::optional<tle::IdType>, // clientId |
673 | | - bool, // returnAllGeneratedTokens |
674 | | - tle::PriorityType, // priority |
675 | | - tle::RequestType, // type |
676 | | - std::optional<tle::ContextPhaseParams>, // contextPhaseParams |
677 | | - std::optional<tle::Tensor>, // encoderInputFeatures |
678 | | - std::optional<tle::SizeType32>, // encoderOutputLength |
679 | | - std::optional<tle::Tensor>, // crossAttentionMask |
680 | | - SizeType32, // numReturnSequences |
681 | | - std::optional<tle::EagleConfig>, // eagleConfig |
682 | | - std::optional<tle::Tensor>, // skipCrossAttnBlocks |
683 | | - std::optional<tle::GuidedDecodingParams>, // guidedDecodingParams |
684 | | - std::optional<tle::SizeType32>, // languageAdapterUid |
685 | | - std::optional<tle::MillisecondsType>, // allottedTimeMs |
686 | | - std::optional<tle::CacheSaltIDType>, // cacheSaltID |
687 | | - std::optional<tle::IdType> // disaggRequestId |
688 | | - >(), |
| 672 | + .def( |
| 673 | + "__init__", |
| 674 | + [toVecTokens](tle::Request* self, |
| 675 | + nb::handle input_token_ids, // list[int] or int32 ndarray |
| 676 | + tle::SizeType32 max_tokens, bool streaming, tle::SamplingConfig const& sampling_config, |
| 677 | + tle::OutputConfig const& output_config, std::optional<tle::SizeType32> const& end_id, |
| 678 | + std::optional<tle::SizeType32> const& pad_id, std::optional<std::vector<SizeType32>> position_ids, |
| 679 | + std::optional<std::list<tle::VecTokens>> bad_words, std::optional<std::list<tle::VecTokens>> stop_words, |
| 680 | + std::optional<tle::Tensor> embedding_bias, |
| 681 | + std::optional<tle::ExternalDraftTokensConfig> external_draft_tokens_config, |
| 682 | + std::optional<tle::PromptTuningConfig> prompt_tuning_config, |
| 683 | + std::optional<tle::MultimodalInput> multimodal_input, std::optional<tle::Tensor> multimodal_embedding, |
| 684 | + std::optional<tle::MropeConfig> mrope_config, std::optional<tle::LoraConfig> lora_config, |
| 685 | + std::optional<tle::LookaheadDecodingConfig> lookahead_config, |
| 686 | + std::optional<tle::KvCacheRetentionConfig> kv_cache_retention_config, |
| 687 | + std::optional<std::string> logits_post_processor_name, |
| 688 | + std::optional<tle::LogitsPostProcessor> logits_post_processor, |
| 689 | + std::optional<tle::VecTokens> encoder_input_token_ids, std::optional<tle::IdType> client_id, |
| 690 | + bool return_all_generated_tokens, tle::PriorityType priority, tle::RequestType type, |
| 691 | + std::optional<tle::ContextPhaseParams> context_phase_params, |
| 692 | + std::optional<tle::Tensor> encoder_input_features, std::optional<tle::SizeType32> encoder_output_length, |
| 693 | + std::optional<tle::Tensor> cross_attention_mask, SizeType32 num_return_sequences, |
| 694 | + std::optional<tle::EagleConfig> eagle_config, std::optional<tle::Tensor> skip_cross_attn_blocks, |
| 695 | + std::optional<tle::GuidedDecodingParams> guided_decoding_params, |
| 696 | + std::optional<tle::SizeType32> language_adapter_uid, |
| 697 | + std::optional<tle::MillisecondsType> allotted_time_ms, |
| 698 | + std::optional<tle::CacheSaltIDType> cache_salt_id, std::optional<tle::IdType> disagg_request_id) |
| 699 | + { |
| 700 | + new (self) tle::Request(toVecTokens(input_token_ids), max_tokens, streaming, sampling_config, |
| 701 | + output_config, end_id, pad_id, std::move(position_ids), std::move(bad_words), std::move(stop_words), |
| 702 | + std::move(embedding_bias), std::move(external_draft_tokens_config), std::move(prompt_tuning_config), |
| 703 | + std::move(multimodal_input), std::move(multimodal_embedding), std::move(mrope_config), |
| 704 | + std::move(lora_config), std::move(lookahead_config), std::move(kv_cache_retention_config), |
| 705 | + std::move(logits_post_processor_name), std::move(logits_post_processor), |
| 706 | + std::move(encoder_input_token_ids), client_id, return_all_generated_tokens, priority, type, |
| 707 | + std::move(context_phase_params), std::move(encoder_input_features), encoder_output_length, |
| 708 | + std::move(cross_attention_mask), num_return_sequences, std::move(eagle_config), |
| 709 | + std::move(skip_cross_attn_blocks), std::move(guided_decoding_params), language_adapter_uid, |
| 710 | + allotted_time_ms, cache_salt_id, disagg_request_id); |
| 711 | + }, |
689 | 712 | // clang-format off |
690 | 713 | nb::arg("input_token_ids"), |
691 | 714 | nb::arg("max_tokens"), |
@@ -726,8 +749,9 @@ void initRequestBindings(nb::module_& m) |
726 | 749 | nb::arg("allotted_time_ms") = nb::none(), |
727 | 750 | nb::arg("cache_salt_id") = nb::none(), |
728 | 751 | nb::arg("disagg_request_id") = nb::none() |
729 | | - ) // clang-format on |
| 752 | + ) // clang-format on |
730 | 753 | .def_prop_ro("input_token_ids", &tle::Request::getInputTokenIds) |
| 754 | + .def_prop_ro("num_input_tokens", &tle::Request::getNumInputTokens) |
731 | 755 | .def_prop_ro("max_tokens", &tle::Request::getMaxTokens) |
732 | 756 | .def_prop_rw("streaming", &tle::Request::getStreaming, &tle::Request::setStreaming) |
733 | 757 | .def_prop_rw("sampling_config", &tle::Request::getSamplingConfig, &tle::Request::setSamplingConfig) |
|
0 commit comments