Refactor TensorMap to per-ring buckets and pools#310
Refactor TensorMap to per-ring buckets and pools#310jvjhfhg wants to merge 1 commit intohw-native-sys:mainfrom
Conversation
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 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 introduces a significant and well-executed refactoring of TensorMap to a per-ring architecture. By creating PTO2TensorMapRing for per-ring resource management and making PTO2TensorMap a facade, the design improves isolation and enables a key performance optimization in lookup with break-on-stale logic. The changes are consistent and robust across all modified files. My review includes a couple of minor suggestions to simplify memory allocation and initialization by using calloc.
| buckets = (PTO2TensorMapEntry**)malloc(new_num_buckets * sizeof(PTO2TensorMapEntry*)); | ||
| if (!buckets) return false; | ||
| for (int32_t i = 0; i < new_num_buckets; i++) { | ||
| buckets[i] = nullptr; | ||
| } |
There was a problem hiding this comment.
For simplicity and to ensure zero-initialization, you can replace malloc and the subsequent loop with a single call to calloc.
| buckets = (PTO2TensorMapEntry**)malloc(new_num_buckets * sizeof(PTO2TensorMapEntry*)); | |
| if (!buckets) return false; | |
| for (int32_t i = 0; i < new_num_buckets; i++) { | |
| buckets[i] = nullptr; | |
| } | |
| buckets = (PTO2TensorMapEntry**)calloc(new_num_buckets, sizeof(PTO2TensorMapEntry*)); | |
| if (!buckets) return false; |
| task_entry_heads = (PTO2TensorMapEntry**)malloc(new_task_window_size * sizeof(PTO2TensorMapEntry*)); | ||
| if (!task_entry_heads) { | ||
| free(buckets); buckets = nullptr; | ||
| free(entry_pool); entry_pool = nullptr; | ||
| free(free_entry_list); free_entry_list = nullptr; | ||
| return false; | ||
| } | ||
|
|
||
| for (int r = 0; r < PTO2_MAX_RING_DEPTH; r++) { | ||
| last_task_alives[r] = 0; | ||
| for (int32_t i = 0; i < new_task_window_size; i++) { | ||
| task_entry_heads[i] = nullptr; | ||
| } |
There was a problem hiding this comment.
Similar to the buckets allocation, you can use calloc here to both allocate and zero-initialize the task_entry_heads array, which simplifies the code.
| task_entry_heads = (PTO2TensorMapEntry**)malloc(new_task_window_size * sizeof(PTO2TensorMapEntry*)); | |
| if (!task_entry_heads) { | |
| free(buckets); buckets = nullptr; | |
| free(entry_pool); entry_pool = nullptr; | |
| free(free_entry_list); free_entry_list = nullptr; | |
| return false; | |
| } | |
| for (int r = 0; r < PTO2_MAX_RING_DEPTH; r++) { | |
| last_task_alives[r] = 0; | |
| for (int32_t i = 0; i < new_task_window_size; i++) { | |
| task_entry_heads[i] = nullptr; | |
| } | |
| task_entry_heads = (PTO2TensorMapEntry**)calloc(new_task_window_size, sizeof(PTO2TensorMapEntry*)); | |
| if (!task_entry_heads) { | |
| free(buckets); buckets = nullptr; | |
| free(entry_pool); entry_pool = nullptr; | |
| free(free_entry_list); free_entry_list = nullptr; | |
| return false; | |
| } |
| bool is_raw_eq_shapes = false, | ||
| bool manual_dep = false) { | ||
| bool manual_dep = false, | ||
| uint8_t in_ring_id = TENSOR_RING_ID_NONE) { |
There was a problem hiding this comment.
tensor的ring_id必须设置,tensor不能在scope A内定义,然后让scope A里的scope B里的task分配内存,所以在make_tensor的时候就要指定ring(等于当前scope的ring_id),view操作继承。submit_task发现tensor的type是output的时候,task的ring_id必须和tensor的ring_id一致,否则要报错。
| // Per-ring chain: entries are ordered newest-first (head-insert). | ||
| // A stale entry means all subsequent entries are also stale — break. | ||
| if (!entry_valid(*cur_entry)) { | ||
| cur_entry = next_entry; |
There was a problem hiding this comment.
这里需要还原为老的逻辑,把next置为nullptr,是否立即进行entry清理再看。
d00c195 to
ec51fd1
Compare
- refactor owner TensorMap storage into per-ring buckets, pools, and cleanup tracking - add a fallback tensormap for external tensors and cross-ring INOUT modifiers while keeping owner-ring history ring-local - route lookup and removal across owner and fallback sources and bind make_tensor() to the current scope ring - update paged attention to treat oi_batch as INOUT in the example and matching device test
ec51fd1 to
0ef4a89
Compare
Uh oh!
There was an error while loading. Please reload this page.