-
Notifications
You must be signed in to change notification settings - Fork 48
Fix TVMFFIEnvSetDLPackManagedTensorAllocator to correctly return the original allocator #371
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
Fix TVMFFIEnvSetDLPackManagedTensorAllocator to correctly return the original allocator #371
Conversation
Summary of ChangesHello @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 Highlights
🧠 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 AssistThe 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
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 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
|
There was a problem hiding this 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| 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); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
…ator from GetDLPackManagedTensorAllocator
…ator from GetDLPackManagedTensorAllocator
|
Interesting! @cyx-6 could you review when you have time? |
cyx-6
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Before this commit, TVMFFIEnvSetDLPackManagedTensorAllocator incorrectly set the previous allocator in:
TVMFFIEnvSetDLPackManagedTensorAllocator(NewAllocator, 0, &pre_allocator);