Refactor: convert TensorMap to C++ member API and inline hot paths#156
Conversation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary of ChangesHello, 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 significantly refactors the TensorMap and related orchestration components to enhance code clarity, improve performance, and streamline memory management. By converting free functions to member methods and adopting pointer-based access, the design becomes more object-oriented and efficient. Key hot paths are inlined for better compiler optimization, and unnecessary fields are removed from task descriptors, contributing to a leaner and faster runtime system. 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. Changelog
Activity
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.
Code Review
This pull request significantly refactors TensorMap by converting it to a C++ class with member methods, changing its internal data structures to be pointer-based, inlining hot-path methods, and removing unused fields from PTO2TaskDescriptor. However, a security audit identified critical and high-severity vulnerabilities, including buffer overflows and integer overflows. Specifically, pto2_submit_task lacks bounds checking and uses 32-bit integers for size calculations that can lead to out-of-bounds accesses, and TensorData::init is vulnerable to a buffer overflow if the number of dimensions exceeds 5. These require strict bounds checking and appropriate 64-bit types for memory sizes. Additionally, a critical issue was found that could lead to an infinite loop in sync_tensormap.
| void PTO2TensorMap::sync_tensormap() { | ||
| constexpr int MIN_FREE_NUM = 1024; | ||
| always_assert(orch != nullptr); | ||
| while(true) { | ||
| // Read current last_task_alive from shared memory | ||
| int32_t new_last_task_alive = PTO2_LOAD_ACQUIRE(&orch->sm_handle->header->last_task_alive); | ||
| sync_validity(new_last_task_alive); | ||
| if ((pool_size - next_entry_idx + free_num < MIN_FREE_NUM) || new_last_task_alive - orch->tensormap_last_cleanup >= PTO2_TENSORMAP_CLEANUP_INTERVAL) { | ||
| cleanup_retired(orch->tensormap_last_cleanup, new_last_task_alive); | ||
| orch->tensormap_last_cleanup = new_last_task_alive; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
The while(true) loop introduced in sync_tensormap could lead to an infinite loop. If cleanup_retired is called but does not free enough entries to satisfy the MIN_FREE_NUM condition, and new_last_task_alive does not advance between loop iterations, this loop will continue indefinitely without making progress. This is because orch->tensormap_last_cleanup is updated to new_last_task_alive, so subsequent calls to cleanup_retired within the loop will have no effect.
This is particularly concerning because the new_entry function in pto_tensormap.h no longer waits for free entries but instead uses always_assert. This means the system will crash if it runs out of entries, which is a regression from the previous behavior of waiting.
Consider changing the while(true) to a single if statement to perform cleanup only once, which was the original behavior. If a loop is desired, a mechanism to prevent infinite spinning, such as a wait counter with an assertion, should be added.
| tm->entry_pool = NULL; | ||
| tm->buckets = NULL; | ||
| tm->free_entry_list = NULL; | ||
| task_entry_head = (PTO2TensorMapEntry**)malloc(new_pool_size * sizeof(PTO2TensorMapEntry*)); |
There was a problem hiding this comment.
The allocation size for task_entry_head appears to be incorrect. It's being allocated with new_pool_size, but it's indexed by task_id & (PTO2_TASK_WINDOW_SIZE - 1). Therefore, its size should be PTO2_TASK_WINDOW_SIZE to match its usage. While new_pool_size (via PTO2_TENSORMAP_POOL_SIZE) and PTO2_TASK_WINDOW_SIZE currently have the same value, this is not guaranteed and makes the code fragile. Please use PTO2_TASK_WINDOW_SIZE for the allocation size to ensure correctness and clarity.
| task_entry_head = (PTO2TensorMapEntry**)malloc(new_pool_size * sizeof(PTO2TensorMapEntry*)); | |
| task_entry_head = (PTO2TensorMapEntry**)malloc(PTO2_TASK_WINDOW_SIZE * sizeof(PTO2TensorMapEntry*)); |
| for (int32_t i = 0; i < pool_size; i++) { | ||
| task_entry_head[i] = nullptr; | ||
| } |
There was a problem hiding this comment.
The loop to reset task_entry_head uses pool_size as its bound. However, task_entry_head is allocated with a size related to PTO2_TASK_WINDOW_SIZE. To avoid potential buffer overruns or incomplete resets, the loop bound should be PTO2_TASK_WINDOW_SIZE.
| for (int32_t i = 0; i < pool_size; i++) { | |
| task_entry_head[i] = nullptr; | |
| } | |
| for (int32_t i = 0; i < PTO2_TASK_WINDOW_SIZE; i++) { | |
| task_entry_head[i] = nullptr; | |
| } |
…w-native-sys#156) Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Summary
init_default,destroy,reset,lookup,insert,remove_entry,sync_tensormap,valid_count)entry_idx→PTO2TensorMapEntry*) for simpler call siteslookup,insert,hash,new_entry) into the header for better codegenpto2_alloc_packed_bufferfrom free function toPTO2OrchestratorStatemember methodoutput_index/num_outputsfields fromPTO2TaskDescriptorTensorPooloverlap detection with early-return and setref_count=1ininit()Testing
./ci.sh -p a2a3sim— 10/10)