Skip to content

Fix argmin flattened reduction on Ascend (tie break, block size, keepdim) - #5430

Open
zheng1 wants to merge 1 commit into
flagos-ai:masterfrom
zheng1:fix/ascend-argmin-reduction
Open

Fix argmin flattened reduction on Ascend (tie break, block size, keepdim)#5430
zheng1 wants to merge 1 commit into
flagos-ai:masterfrom
zheng1:fix/ascend-argmin-reduction

Conversation

@zheng1

@zheng1 zheng1 commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

PR Category

Operator

Type of Change

Bug Fix

Description

argmin(inp) with dim=None returned wrong indices on the Ascend backend.
Three issues on the same code path:

  1. Tie breaking. tl.min(..., return_indices=True) did not request
    return_indices_tie_break_left, so equal values could resolve to an arbitrary
    index instead of the first one. PyTorch returns the first occurrence.
  2. Block size. block_size was derived only from sqrt(numel) with no upper
    bound. For large inputs it exceeded what the device could handle on this path
    and produced wrong indices. It is now capped at 1024.
  3. keepdim. The output was allocated with torch.empty([]) unconditionally, so
    keepdim=True was dropped. PyTorch returns shape [1] * inp.dim().

Two regression tests cover the large flattened index and NaN handling.

The non-contiguous input problem on the same code path is handled separately in
#5439, which covers it across all the affected reduction operators rather than
just argmin.

Issue

None.

Progress

  • Change is properly reviewed (1 reviewer required, 2 recommended).
  • Change is responded to an issue.
  • Change is fully covered by a UT.

Performance

The block size cap keeps the reduction within a supported launch configuration.
Tie breaking and the output shape have no performance impact.

argmin(inp) with dim=None returned wrong indices on the Ascend backend.
Three issues on the same code path:

1. Tie breaking. tl.min(..., return_indices=True) did not request
   return_indices_tie_break_left, so equal values could resolve to an
   arbitrary index instead of the first one. PyTorch returns the first
   occurrence.
2. Block size. block_size was derived only from sqrt(numel) with no upper
   bound. For large inputs it exceeded what the device could handle on this
   path and produced wrong indices. It is now capped at 1024.
3. keepdim. The output was allocated with torch.empty([]) unconditionally,
   so keepdim=True was dropped. PyTorch returns shape [1] * inp.dim().

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@zheng1
zheng1 force-pushed the fix/ascend-argmin-reduction branch from 8e5e384 to 5508c80 Compare August 13, 2026 04:52
@zheng1 zheng1 changed the title Fix argmin flattened reduction on Ascend (index, tie break, contiguity, keepdim) Fix argmin flattened reduction on Ascend (tie break, block size, keepdim) Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant