Skip to content

Fix full reduction reading outside non-contiguous input views - #5439

Open
zheng1 wants to merge 2 commits into
flagos-ai:masterfrom
zheng1:fix/ascend-reduction-noncontiguous
Open

Fix full reduction reading outside non-contiguous input views#5439
zheng1 wants to merge 2 commits into
flagos-ai:masterfrom
zheng1:fix/ascend-reduction-noncontiguous

Conversation

@zheng1

@zheng1 zheng1 commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

PR Category

Operator

Type of Change

Bug Fix

Description

The flattened reduction kernels address the input linearly (inp + offset), but
the Python side never made the input contiguous. For a view whose storage still
holds elements outside it, such as base[:, :3] or base[::2], the kernel reads
those discarded elements and returns a wrong result.

Reproduced on Ascend 910B for min, amax, amin, prod, mean, argmax and
argmin. prod and amin run the shared implementation under
src/flag_gems/ops/, so this is not backend specific. The bug is a mismatch
between linear addressing of storage and the view's strides, which is
independent of the hardware.

The same one line fix already exists elsewhere in the tree for the same reason:
sum and mean in #1786, and max in #273. Some operators in the same family
got it and some did not, so this fills in the rest.

amin_ writes in place, so it reads through a contiguous copy and keeps writing
back to the original tensor rather than rebinding the name.

Each operator gets a full reduction regression test. The values outside the view
are chosen so that reading them changes the result, instead of relying on random
data to expose the bug.

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

No performance impact. contiguous() returns the same tensor when the input is
already contiguous, which is the case on every existing code path, so no copy is
introduced. A copy happens only for inputs that previously produced a wrong
result.

Verified on Ascend 910B against a pure master checkout on the same device: the
20 new regression cases go from failing to passing, and the overall failure count
for the touched suites drops from 144 to 65. The remaining difference is
pre-existing flakiness in argmax and argmin tie breaking, which reproduces at
the same rate on master.

The flattened reduction kernels address the input linearly, but the Python
side never made the input contiguous. For a view whose storage still holds
elements outside it, such as base[:, :3] or base[::2], the kernel read those
discarded elements and returned a wrong result.

Reproduced on Ascend 910B for min, amax, amin, prod, mean, argmax and argmin.
prod and amin run the shared implementation, so this is not backend specific.

The same one line fix is already used for sum and mean in flagos-ai#1786 and for max
in flagos-ai#273. amin_ writes in place, so it reads through a contiguous copy and
keeps writing back to the original tensor.

Adds a full reduction regression test per operator. The values outside the
view are chosen so that reading them changes the result, rather than relying
on random data to expose it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Two issues on the dim=None path:

1. keepdim. The output was allocated with torch.empty([]) unconditionally, so
   keepdim=True was dropped. PyTorch returns shape [1] * inp.dim().
2. Block size. block_size was derived only from sqrt(numel) with no upper
   bound. For large inputs it went past what the device handles on this path
   and the returned index pointed at an element that was not the maximum.
   It is now capped at 1024, the same bound argmin uses.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@zheng1
zheng1 force-pushed the fix/ascend-reduction-noncontiguous branch from 959bc89 to f33d8b0 Compare August 13, 2026 09:49
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