Skip to content

Conversation

@HydrogenSulfate
Copy link
Contributor

@HydrogenSulfate HydrogenSulfate commented Jun 24, 2025

PR Category

Operator Mechanism

PR Types

New features

Description

Pcard-75624

支持动态图下的 masked_select 二阶反向组合算子

torch精度对比测试代码

import paddle
import torch
import numpy as np


# paddle
x_pd = paddle.randn([1024, 10240])
x_pd.stop_gradient = False

mask_pd = paddle.randint(0, 2, x_pd.shape).astype("bool")

y_pd = paddle.masked_select(x_pd, mask_pd)

dy_pd = paddle.randn_like(y_pd)
dy_pd.stop_gradient = False

dx_pd, = paddle.grad(y_pd, x_pd, dy_pd, create_graph=True)
ddx_pd = paddle.randn_like(dx_pd)
ddx_pd.stop_gradient = False

ddy_pd, = paddle.grad(dx_pd, dy_pd, ddx_pd, create_graph=True)

# torch
x_pt = torch.from_dlpack(x_pd.detach()).requires_grad_(True)
mask_pt = torch.from_dlpack(mask_pd)
y_pt = torch.masked_select(x_pt, mask_pt)

dy_pt = torch.from_dlpack(dy_pd.detach()).requires_grad_(True)
dx_pt, = torch.autograd.grad(y_pt, x_pt, dy_pt, create_graph=True)

ddx_pt = torch.from_dlpack(ddx_pd.detach()).requires_grad_(True)
ddy_pt, = torch.autograd.grad(dx_pt, dy_pt, ddx_pt, create_graph=True)

np.testing.assert_allclose(y_pd.numpy(), y_pt.detach().cpu().numpy())
np.testing.assert_allclose(dx_pd.numpy(), dx_pt.detach().cpu().numpy())
np.testing.assert_allclose(ddy_pd.numpy(), ddy_pt.detach().cpu().numpy())

@HydrogenSulfate HydrogenSulfate changed the title [Prim] Support masked_select double grad in eager mode [Prim] Support masked_select_double_grad in eager mode Jun 25, 2025
@HydrogenSulfate HydrogenSulfate merged commit 77fde0c into PaddlePaddle:develop Jul 1, 2025
107 of 113 checks passed
@HydrogenSulfate HydrogenSulfate deleted the add_masked_select_double_grad branch July 1, 2025 02:51
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 -->
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.

2 participants