Skip to content

Conversation

Manfredss
Copy link

Fixes Issue #75574

  • Fixed incorrect stride calculation in AdvancedIndex when indexing with sliced tensors
  • Changed idx_stride_vec to use element_size_bytes directly instead of multiplying with stride_vec[dim]
  • Added test case to verify the fix

PR Category

  • Performance Optimization

PR Types

  • Bug fixes

Description

  • 修改了 paddle/fluid/pybind/slice_utils.h 中与 stride / idx_stride_vec 计算相关的逻辑,使得 fancy 索引在切片 tensor 上也能正确映射到原张量的地址。
  • 将 idx_stride_vec 的构造方式从 “乘以 stride_vec[dim]” 改为直接使用 element_size_bytes。
  • 新增测试 python/unittest_py/issue75574.py,覆盖 s[-1] 和 s[[-1]] 的行为。
  • 在 macOS 15.5 M4 芯片下用cmake编译并通过测试

Why

  • 原先实现中,若从 slice 出来的 tensor 再做 fancy(list / tensor)索引,其 stride 映射逻辑没有考虑切片的 offset / 步长变换,从而出现偏移错误(例如本次场景中返回 16704 而不是 17728)。
  • 通过重构 stride 逻辑,可以保证:对于从原 tensor slice 出来的子 tensor,再做 fancy 索引时,其内部地址计算仍然正确。
  • 该修改对不使用 fancy 索引的场景几乎无影响,兼容性风险低。

How Has This Been Tested?

单元测试 issue75574.py: 测试了

  • s = edge_index[::32]
  • [-1] 是否等于最后元素
  • s[[-1]] 是否等于 list 索引行为
  • 在 Mac Arm64 平台通过测试。
  • 运行了仓库已有的单元测试:pytest 全通过,无新失败。

Checklist

  • 修改代码编译通过
  • 添加及通过新的单元测试
  • 相关改动已通过 pre-commit(clang-format / cpplint / ruff 等)
  • 关联 issue 已在 PR 描述中注明
  • 若涉及接口变更,是否更新文档或注释
  • 没有将 large / 无用文件(如 build/, third_party/ 等)纳入 commit

Future Work

  • 当前 fix 主要覆盖 [-1] 和简单 fancy 索引场景,更复杂的 multi-dimensional fancy 索引可能还需补充。
  • 若后续发现对其他操作(如 multi-axis indexing, broadcast indexing)有兼容性问题,可能需要进一步扩展 slice_utils 或 Python 层的逻辑。

Fixes #75574

- Fixed incorrect stride calculation in AdvancedIndex when indexing
  with sliced tensors
- Changed idx_stride_vec to use element_size_bytes directly instead
  of multiplying with stride_vec[dim]
- Added test case to verify the fix
Copy link

paddle-bot bot commented Oct 1, 2025

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label Oct 1, 2025
@luotao1 luotao1 added the HappyOpenSource 快乐开源活动issue与PR label Oct 1, 2025
@Manfredss Manfredss closed this by deleting the head repository Oct 3, 2025
@Manfredss
Copy link
Author

Manfredss commented Oct 3, 2025

为什么我拉取源码直接编译后安装whl,测试#75574的问题复现不了?卸载之后再安装3.2.0版本的再测试就又能复现报错
make指令:
time cmake .. -DPY_VERSION=3.10 -DPYTHON_INCLUDE_DIR=${PYTHON_INCLUDE_DIRS} -DPYTHON_LIBRARY=${PYTHON_LIBRARY} -DWITH_GPU=OFF -DWITH_ARM=ON -DWITH_TESTING=ON -DWITH_AVX=OFF -DWITH_AVX2=OFF -DWITH_AVX512=OFF -DWITH_SYSTEM_GFLAGS=ON -DPYTHON_EXECUTABLE=$(which python3.10) -DCMAKE_THREAD_LIBS_INIT='-lpthread' -DCMAKE_HAVE_THREADS_LIBRARY=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers HappyOpenSource 快乐开源活动issue与PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants