[Fix] CommReduce could handle 0-dim data#19683
Conversation
There was a problem hiding this comment.
Code Review
This pull request fixes a crash when reducing a 0-dimensional (scalar) tensor by returning its identity instead of throwing an error, and adds a corresponding regression test. The reviewer pointed out that when ndim == 0 and atleast1d is true, the output tensor should be expanded to at least 1-dimensional (e.g., shape [1]) using topi::expand_dims rather than returning the 0-dimensional identity directly.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if (ndim == 0) { | ||
| return topi::identity(data, data->op->name + "_red", kCommReduce); | ||
| } |
There was a problem hiding this comment.
When ndim == 0 and atleast1d is true, the output tensor should be at least 1-dimensional (i.e., shape [1]). However, returning topi::identity directly ignores the atleast1d flag and returns a 0-dimensional tensor. We should wrap the identity tensor with topi::expand_dims if atleast1d is enabled.
| if (ndim == 0) { | |
| return topi::identity(data, data->op->name + "_red", kCommReduce); | |
| } | |
| if (ndim == 0) { | |
| auto identity = topi::identity(data, data->op->name + "_red", kCommReduce); | |
| return atleast1d ? topi::expand_dims(identity, 0, 1) : identity; | |
| } |
This PR try to fix #19676 , allow
CommReducehandle 0-dim data correctly.