Skip to content

[https://nvbugs/6196391][fix] Eliminate O(N^2) memcpy in findBlocksInReuseTreeByBlockKey - #14499

Merged
brb-nv merged 2 commits into
NVIDIA:feat/bench_xfrom
brb-nv:user/brb/eliminate-find-blocks-memcpy
May 26, 2026
Merged

brb-nv merged 2 commits into
NVIDIA:feat/bench_xfrom
brb-nv:user/brb/eliminate-find-blocks-memcpy

Conversation

@brb-nv

@brb-nv brb-nv commented May 23, 2026 •

Copy link
Copy Markdown
Collaborator

Description

The blockKey contruction in WindowBlockManager::findBlocksInReuseTreeByBlockKey is getting very slow with ISL. This change fixes that. Main insight is that push_back involves copy construction copying of uniqueTokens - which gets overwritten immediately anyway.

Let's say N is num_prefix_blocks and T is num_prefix_tokens.

for (auto const& blockedUniqueTokensList : blockedUniqueTokens)   // N iterations
{
    blockKeys.push_back(blockKey);                                 // <- This line is O(T)
    blockKeys.back().uniqueTokens = blockedUniqueTokensList;      // overwrites it, O(1)
}
image

Test Coverage

$ ./cpp/build/tests/unit_tests/batch_manager/kvCacheManagerTest --gtest_filter='*FindBlocksInReuseTreeByBlockKeys*'

PR Checklist

Please review the following before submitting your PR:

  • PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.

  • PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.

  • Test cases are provided for new code paths (see test instructions)

  • If PR introduces API changes, an appropriate PR label is added - either api-compatible or api-breaking. For api-breaking, include BREAKING in the PR title.

  • Any new dependencies have been scanned for license and vulnerabilities

  • CODEOWNERS updated if ownership changes

  • Documentation updated as needed

  • Update tava architecture diagram if there is a significant design change in PR.

  • The reviewers assigned automatically/manually are appropriate for the PR.

  • Please check this after reviewing the above items as appropriate for this PR.

GitHub Bot Help

To see a list of available CI bot commands, please comment /bot help.

Signed-off-by: Balaram Buddharaju <169953907+brb-nv@users.noreply.github.com>
Signed-off-by: Balaram Buddharaju <169953907+brb-nv@users.noreply.github.com>
@brb-nv
brb-nv marked this pull request as ready for review May 24, 2026 00:24
@brb-nv
brb-nv requested a review from a team as a code owner May 24, 2026 00:24
@brb-nv

brb-nv commented May 24, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@brb-nv
brb-nv requested review from Tabrizian and pcastonguay May 24, 2026 00:24
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #50068 [ run ] triggered by Bot. Commit: 4842da9 Link to invocation

@brb-nv brb-nv changed the title [None][perf] eliminate O(N^2) memcpy in findBlocksInReuseTreeByBlockKey [https://nvbugs/6196391][fix] Eliminate O(N^2) memcpy in findBlocksInReuseTreeByBlockKey May 24, 2026
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #50068 [ run ] completed with state SUCCESS. Commit: 4842da9
/LLM/main/L0_MergeRequest_PR pipeline #39624 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

Link to invocation

@chuangz0

Copy link
Copy Markdown
Collaborator

agent suggests use the following code to replace fromReuseTree in getBlockRangeForSending

    // Equivalent to fromReuseTree but O(K): the request's cacheBlockIds already records reused-prefix and
    // newly-allocated suffix in chronological order, so the trailing slice avoids the reuse-tree walk and mutex.
    auto const windowSize = cacheManager->getBlockManager().getWindowSizesMetadata().begin()->first;
    auto blockRange = BlockRange::fromAllBlockIds(*cacheManager, llmRequest.mRequestId);
    auto const& allBlockIds = blockRange.getBlockIdsPerWindow().at(windowSize);
    int32_t const numBlocksToSend = indexFromEnd + 1;
    TLLM_CHECK_WITH_INFO(static_cast<int32_t>(allBlockIds.size()) >= numBlocksToSend,
        "sender sequence has %zu blocks but receiver requested last %d", allBlockIds.size(), numBlocksToSend);
    std::vector<SizeType32> tailBlockIds(allBlockIds.end() - numBlocksToSend, allBlockIds.end());
    blockRange.setBlockIdsForWindow(windowSize, std::move(tailBlockIds));
    return blockRange;

@Tabrizian Do you think it reasonable? the overhead should be smaller.

@VALLIS-NERIA VALLIS-NERIA left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Tabrizian

Tabrizian commented May 26, 2026 •

Copy link
Copy Markdown
Member

@chuangz0 The ctx request is terminated here so blocks for that request may not be available anymore. This was also added as a part of refactor to remove request ID and allow arbitrary KVCache transfer.

@brb-nv

brb-nv commented May 26, 2026

Copy link
Copy Markdown
Collaborator Author

Merging as this has been verified to help e2e perf by downstream user. If any refactor is needed, we can take as a follow-up on main @chuangz0 @Tabrizian .

I believe this function has been used for a while. Fixing it with minimal changes in this MR seems like a reasonable thing to do.

@brb-nv
brb-nv merged commit 8fff9c5 into NVIDIA:feat/bench_x May 26, 2026
16 of 17 checks passed
brb-nv added a commit to brb-nv/TensorRT-LLM that referenced this pull request May 29, 2026
…ReuseTreeByBlockKey (NVIDIA#14499)

Signed-off-by: Balaram Buddharaju <169953907+brb-nv@users.noreply.github.com>
brb-nv added a commit to brb-nv/TensorRT-LLM that referenced this pull request May 29, 2026
…ReuseTreeByBlockKey (NVIDIA#14499)

Signed-off-by: Balaram Buddharaju <169953907+brb-nv@users.noreply.github.com>

[None][fix] avoid duplicate harmony tokenization & populate perf_metrics (NVIDIA#14506)

Signed-off-by: Balaram Buddharaju <169953907+brb-nv@users.noreply.github.com>
brb-nv added a commit to brb-nv/TensorRT-LLM that referenced this pull request May 29, 2026
…ReuseTreeByBlockKey (NVIDIA#14499)

Signed-off-by: Balaram Buddharaju <169953907+brb-nv@users.noreply.github.com>

[None][fix] avoid duplicate harmony tokenization & populate perf_metrics (NVIDIA#14506)

Signed-off-by: Balaram Buddharaju <169953907+brb-nv@users.noreply.github.com>
brb-nv added a commit to brb-nv/TensorRT-LLM that referenced this pull request May 29, 2026
…ReuseTreeByBlockKey (NVIDIA#14499)

[https://nvbugs/6196391][fix] avoid duplicate harmony tokenization & populate perf_metrics (NVIDIA#14506)

Signed-off-by: Balaram Buddharaju <169953907+brb-nv@users.noreply.github.com>
brb-nv added a commit to brb-nv/TensorRT-LLM that referenced this pull request May 29, 2026
…ReuseTreeByBlockKey (NVIDIA#14499)

[https://nvbugs/6196391][fix] avoid duplicate harmony tokenization & populate perf_metrics (NVIDIA#14506)

Signed-off-by: Balaram Buddharaju <169953907+brb-nv@users.noreply.github.com>
brb-nv added a commit to brb-nv/TensorRT-LLM that referenced this pull request May 29, 2026
…ReuseTreeByBlockKey (NVIDIA#14499)

[https://nvbugs/6196391][fix] avoid duplicate harmony tokenization & populate perf_metrics (NVIDIA#14506)

Signed-off-by: Balaram Buddharaju <169953907+brb-nv@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants