Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Negative indexing] Support negative indices for all operators in the scatter/gather series. #68780

Merged

Conversation

HydrogenSulfate
Copy link
Contributor

@HydrogenSulfate HydrogenSulfate commented Oct 17, 2024

PR Category

User Experience

PR Types

Bug fixes

Description

Pcard-75624

相关PR:deepmodeling/deepmd-kit#4157

为数据操作/索引操作类的算子及其反向算子支持负数索引(即从仅支持 $[0, S)$ 内的非负整数,升级为支持 $[-S, S)$ 范围内的所有整数作为索引值, $S$ 表示运算输入x的形状在axis维度上的大小,即索引值的上限),以及修复负数axis的支持(个别算子对axis为-1这种负数没有处理逻辑导致报错,这里统一修复了),具体包括以下7个算子:

  1. index_add
  2. index_select
  3. gather
  4. gather_nd
  5. scatter(同时修改其infermeta,使得允许index的长度小于updates的长度,与文档描述保持一致: paddle.scatter
  6. scatter_nd
  7. Tensor.__getitem__(上述6个算子支持负数索引后即可实现)

具体包括以下修改:

  1. 为上述算子对应Kernel添加下标为负数时的逻辑:索引范围检查的左端点从 $0$ 改为 $-S$,当通过检查后,索引下标为负数时,将索引下标对 $S$ 取模(即加上 $S$),变成正数后再供后续计算。
  2. 修改单测中index下标的随机生成范围,随机区间的下限从 $0$ 改为 $-S$
  3. setitem/getitem添加负数索引在动/静态图下的单测
  4. 修改原有针对负数索引的异常断言单测,当小于 $-S$ 时才抛出异常
  5. 个别算子对负数axis没有处理逻辑,为此统一加上张量的维数即可

Copy link

paddle-bot bot commented Oct 17, 2024

你的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.

@HydrogenSulfate HydrogenSulfate changed the title [Negtive indexing] Support negtive index value for several kernels [Negative indexing] Support negtive index value for several kernels Oct 17, 2024
@HydrogenSulfate HydrogenSulfate changed the title [Negative indexing] Support negtive index value for several kernels [Negative indexing] Support negative indices for all operators in the scatter/gather series. Oct 18, 2024
Copy link
Contributor

@zoooo0820 zoooo0820 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@HydrogenSulfate HydrogenSulfate merged commit 809bf6c into PaddlePaddle:develop Oct 21, 2024
27 checks passed
@HydrogenSulfate HydrogenSulfate deleted the support_negtive_index_value branch October 21, 2024 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants