Skip to content

Conversation

@HydrogenSulfate
Copy link
Contributor

@HydrogenSulfate HydrogenSulfate commented Jul 1, 2025

PR Category

Operator Mechanism

PR Types

New features

Description

Pcard-75624

  1. 添加index_add_grad静态图组合实现,修改对应单测,将x添加到no_need_buffer中,避免hold不必要的中间变量
动态shape条件下的精度测试

import numpy as np
import paddle


paddle.framework.core._set_prim_all_enabled(True)

def func(x, index, v, dy, axis):
    y = paddle.index_add(x, index, axis, v)
    gx, gv = paddle.grad(y, [x, v], dy, create_graph=True)
    return gx, gv


ndim = 5

for ndim_ in range(1, ndim + 1):
    x_shape = np.random.randint(1, 50, size=[ndim_])
    for axis in range(-ndim_, ndim_):

        print("*" * 20)
        index_shape = np.random.randint(1, 1000, size=[1])

        v_shape = x_shape
        v_shape[axis] = index_shape[0]

        x = x = paddle.randn(x_shape)
        x.stop_gradient = False
        index = paddle.randint(-x_shape[axis], x_shape[axis], shape=index_shape)
        v = paddle.randn(v_shape)
        v.stop_gradient = False
        print(f"testing with x.shape = {x.shape}, index.shape = {index.shape}, v.shape = {v.shape}, axis = {axis}, dout.shape = {x.shape}")
        out = paddle.index_add(x, index, axis, v)

        dout = paddle.randn_like(out)

        dx_eager, dv_eager, = paddle.grad(out, [x, v], dout, create_graph=True)
        print(f"eager finish")

        st_func = paddle.jit.to_static(func, full_graph=True, input_spec=[
            InputSpec([-1, -1]),
            InputSpec([-1]),
            InputSpec([-1, -1]),
            InputSpec([-1, -1]),
        ])
        dx_st, dv_st = st_func(x, index, v, dout, axis)
        print(f"static finish")

        np.testing.assert_allclose(dx_eager.numpy(), dx_st.numpy())
        np.testing.assert_allclose(dv_eager.numpy(), dv_st.numpy())
        print("=" * 20)

@paddle-bot
Copy link

paddle-bot bot commented Jul 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.

@HydrogenSulfate HydrogenSulfate changed the title [Prim] Add index_add_grad for static composition [Prim] Add index_add_grad for static decomposition Jul 1, 2025
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (develop@1d60828). Learn more about missing BASE report.

Additional details and impacted files
@@             Coverage Diff             @@
##             develop    #73737   +/-   ##
===========================================
  Coverage           ?   100.00%           
===========================================
  Files              ?         1           
  Lines              ?         2           
  Branches           ?         0           
===========================================
  Hits               ?         2           
  Misses             ?         0           
  Partials           ?         0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@HydrogenSulfate HydrogenSulfate merged commit c86c852 into PaddlePaddle:develop Jul 2, 2025
84 of 94 checks passed
@HydrogenSulfate HydrogenSulfate deleted the add_index_add_grad branch July 2, 2025 07:09
github-merge-queue bot pushed a commit to deepmodeling/deepmd-kit that referenced this pull request Jul 10, 2025
support running `input_torch_dynamic.json` with paddle backend(including
CINN)


TODO list:

- [x] PaddlePaddle/Paddle#73601
- [x] PaddlePaddle/Paddle#73622
- [x] PaddlePaddle/Paddle#73737
- [x] PaddlePaddle/Paddle#73747
- [x] PaddlePaddle/Paddle#73809
- [x] PaddlePaddle/Paddle#73761


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Summary by CodeRabbit

* **Bug Fixes**
* Resolved issues with tensor shape and indexing consistency, preventing
assertion errors during model execution.
* Improved handling of default tensor initialization to avoid JIT
assertion issues.

* **Refactor**
* Standardized tensor dimension handling and broadcasting for improved
clarity and maintainability.
* Enhanced code readability with clearer indexing conventions and
formatting.
* Updated aggregation logic for safer and more efficient tensor
operations.

* **New Features**
* Added an option to control graph index mapping behavior for greater
flexibility in advanced use cases.

* **Tests**
* Introduced comprehensive tests validating descriptor model consistency
with dynamic selection enabled.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
ChiahsinChu pushed a commit to ChiahsinChu/deepmd-kit that referenced this pull request Dec 17, 2025
support running `input_torch_dynamic.json` with paddle backend(including
CINN)


TODO list:

- [x] PaddlePaddle/Paddle#73601
- [x] PaddlePaddle/Paddle#73622
- [x] PaddlePaddle/Paddle#73737
- [x] PaddlePaddle/Paddle#73747
- [x] PaddlePaddle/Paddle#73809
- [x] PaddlePaddle/Paddle#73761


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Summary by CodeRabbit

* **Bug Fixes**
* Resolved issues with tensor shape and indexing consistency, preventing
assertion errors during model execution.
* Improved handling of default tensor initialization to avoid JIT
assertion issues.

* **Refactor**
* Standardized tensor dimension handling and broadcasting for improved
clarity and maintainability.
* Enhanced code readability with clearer indexing conventions and
formatting.
* Updated aggregation logic for safer and more efficient tensor
operations.

* **New Features**
* Added an option to control graph index mapping behavior for greater
flexibility in advanced use cases.

* **Tests**
* Introduced comprehensive tests validating descriptor model consistency
with dynamic selection enabled.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
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.

4 participants