Skip to content

Conversation

@nan2088
Copy link
Contributor

@nan2088 nan2088 commented Dec 31, 2025

Before this commit, TVMFFIEnvSetDLPackManagedTensorAllocator incorrectly set the previous allocator in:
TVMFFIEnvSetDLPackManagedTensorAllocator(NewAllocator, 0, &pre_allocator);

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @nan2088, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical bug in the TVMFFIEnvSetDLPackManagedTensorAllocator function where it failed to correctly provide the original allocator when a new one was being set. The fix involves a precise adjustment to the function's logic, ensuring that the previous allocator is accurately captured and returned. This is vital for maintaining correct state management and enabling proper restoration of allocator configurations. A new unit test has been introduced to verify the integrity of this fix.

Highlights

  • Bug Fix: Corrected the TVMFFIEnvSetDLPackManagedTensorAllocator function to properly return the original allocator by removing an erroneous assignment that would overwrite the internal dlpack_allocator_ before it could be returned.
  • New Test Case: Added a new C++ test case, TVMFFIEnvSetDLPackManagedTensorAllocator, to tests/cpp/extra/test_c_env_api.cc to validate that the function now correctly returns the previously set allocator.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request aims to fix a bug in TVMFFIEnvSetDLPackManagedTensorAllocator where it was incorrectly returning the new allocator instead of the previous one. The change correctly moves the allocator assignment to fix this immediate issue.

However, my review identifies a remaining logical inconsistency. The function now returns the previous thread-local allocator, whereas the corresponding Get function returns the effective allocator (which could be the global one). This can lead to incorrect behavior in certain scenarios. I've provided a detailed comment in src/ffi/extra/env_context.cc with a suggested fix.

I've also added a comment to the new test in tests/cpp/extra/test_c_env_api.cc, suggesting a more comprehensive test case that would expose the remaining issue and verify the complete fix.

void SetDLPackManagedTensorAllocator(DLPackManagedTensorAllocator allocator,
int write_to_global_context,
DLPackManagedTensorAllocator* opt_out_original_allocator) {
dlpack_allocator_ = allocator;
Copy link
Contributor

Choose a reason for hiding this comment

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

high

Removing this assignment from the beginning of the function is the correct first step to fix the bug where the new allocator was returned instead of the previous one.

However, a more subtle logic issue remains. After this change, the function will return the previous thread-local allocator. But GetDLPackManagedTensorAllocator returns the effective allocator (thread-local if available, otherwise global). For consistency, SetDLPackManagedTensorAllocator should also return the previously effective allocator.

To fix this, you should capture the result of GetDLPackManagedTensorAllocator() before modifying any state, and return that as the original allocator.

For example:

void SetDLPackManagedTensorAllocator(...) {
  if (opt_out_original_allocator != nullptr) {
    *opt_out_original_allocator = GetDLPackManagedTensorAllocator();
  }
  if (write_to_global_context != 0) {
    GlobalTensorAllocator() = allocator;
  }
  dlpack_allocator_ = allocator;
}

This would make the behavior consistent and robust. I've also suggested a test case in test_c_env_api.cc that would fail with the current implementation but pass with this suggested fix.

Comment on lines +62 to +68
TEST(CEnvAPI, TVMFFIEnvSetDLPackManagedTensorAllocator) {
auto old_allocator = TVMFFIEnvGetDLPackManagedTensorAllocator();
DLPackManagedTensorAllocator pre_allocator;
TVMFFIEnvSetDLPackManagedTensorAllocator(TestDLPackManagedTensorAllocator, 0, &pre_allocator);
EXPECT_EQ(old_allocator, pre_allocator);
TVMFFIEnvSetDLPackManagedTensorAllocator(old_allocator, 0, nullptr);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The added test case is good for the basic scenario, but it's not comprehensive enough. It passes because initially both the thread-local and global allocators are nullptr. The test doesn't cover the scenario where a global allocator is active and the thread-local one is not. In that case, TVMFFIEnvGetDLPackManagedTensorAllocator would return the global allocator, but TVMFFIEnvSetDLPackManagedTensorAllocator would incorrectly return nullptr as the "previous" allocator. This indicates a logic issue in SetDLPackManagedTensorAllocator.

I suggest adding a test case to cover this scenario, which would look something like this:

TEST(CEnvAPI, TVMFFIEnvSetDLPackManagedTensorAllocator_Global) {
  // Setup: set a global allocator, but no thread-local one.
  TVMFFIEnvSetDLPackManagedTensorAllocator(TestDLPackManagedTensorAllocatorError, 1, nullptr);
  TVMFFIEnvSetDLPackManagedTensorAllocator(nullptr, 0, nullptr); // Unset thread-local

  // The effective allocator should be the global one.
  auto old_allocator = TVMFFIEnvGetDLPackManagedTensorAllocator();
  EXPECT_EQ(old_allocator, TestDLPackManagedTensorAllocatorError);

  // Set a new thread-local allocator and get the previous one.
  DLPackManagedTensorAllocator pre_allocator;
  TVMFFIEnvSetDLPackManagedTensorAllocator(TestDLPackManagedTensorAllocator, 0, &pre_allocator);

  // The returned previous allocator should be the effective (global) one.
  EXPECT_EQ(old_allocator, pre_allocator);

  // Cleanup
  TVMFFIEnvSetDLPackManagedTensorAllocator(nullptr, 1, nullptr);
}

This test would fail with the current implementation and highlight the need for a fix in src/ffi/extra/env_context.cc.

nan added 2 commits December 31, 2025 02:30
@junrushao
Copy link
Member

Interesting! @cyx-6 could you review when you have time?

Copy link
Contributor

@cyx-6 cyx-6 left a comment

Choose a reason for hiding this comment

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

LGTM

@cyx-6 cyx-6 merged commit 71bbe91 into apache:main Jan 5, 2026
7 checks passed
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.

3 participants