-
Notifications
You must be signed in to change notification settings - Fork 49
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,6 +59,14 @@ int TestDLPackManagedTensorAllocatorError(DLTensor* prototype, DLManagedTensorVe | |
| return -1; | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
|
Comment on lines
+62
to
+68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 |
||
|
|
||
| TEST(CEnvAPI, TVMFFIEnvTensorAlloc) { | ||
| auto old_allocator = TVMFFIEnvGetDLPackManagedTensorAllocator(); | ||
| TVMFFIEnvSetDLPackManagedTensorAllocator(TestDLPackManagedTensorAllocator, 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.
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
GetDLPackManagedTensorAllocatorreturns the effective allocator (thread-local if available, otherwise global). For consistency,SetDLPackManagedTensorAllocatorshould 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:
This would make the behavior consistent and robust. I've also suggested a test case in
test_c_env_api.ccthat would fail with the current implementation but pass with this suggested fix.