Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions paddle/fluid/pir/dialect/op_generator/op_build_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
'CSoftmaxWithCrossEntropyGradInferMeta',
'CSoftmaxWithMultiLabelCrossEntropyGradInferMeta',
'LSTMGradInferMeta',
'MatmulGradInferMeta',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P3 该白名单项是空操作,且与 devPR #79800 不一致。

_INFERMETA_NEED_META_CONFIG 唯一作用是让 PIR 生成的 build / infer_meta 代码在调用末尾追加 phi::MetaConfig(false, false)(op_build_gen.py 的 CREATE_INFER_META_FUNC_WITH_META_CONFIG_TEMPLATE,op_infermeta_func_gen.py 同理)。但本 PR 新增的 MatmulGradInferMeta(paddle/phi/infermeta/backward.cc 第 732-788 行)在第三个 commit 之后已完全不读取 config——判定条件是 K_lhs != -1 && K_rhs != -1,与 config.is_runtime 无关。因此注册后只是把默认 MetaConfig()(is_runtime=true)换成 MetaConfig(false, false),不产生任何行为差异;白名单里其余条目(如 MatmulInferMeta)都是真正使用 config 的函数。

另外,devPR #79800(已于 develop 合入,a48f1c3)并不包含本行,本 cherry-pick 仅此一处与 devPR 有差异,PR 描述中也未提及该文件,会造成 release/3.4 与 develop 的生成器白名单不一致,后续 cherry-pick 易产生冲突。

建议:若无实际依赖,删掉该行以与 devPR 保持一致(同时可考虑移除 MatmulGradInferMeta 中未被使用的 MetaConfig config 形参);若确有保留必要,请在描述中补充原因并同步回 develop。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

devPR #79857 已补充合入

'LodResetGradInferMeta',
'FFTC2RGradInferMeta',
'GruGradInferMeta',
Expand Down
58 changes: 58 additions & 0 deletions paddle/phi/infermeta/backward.cc
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,64 @@ void GeneralBinaryGradInferMeta(const MetaTensor& x,
}
}

void MatmulGradInferMeta(const MetaTensor& x,
const MetaTensor& y,
bool transpose_x,
bool transpose_y,
MetaTensor* dx,
MetaTensor* dy,
MetaConfig config) {
// The forward MatmulInferMeta enforces that the contraction (K) dimension of
// X and Y are equal, but matmul_grad routed through
// GeneralBinaryGradInferMeta skips this check and blindly shares X/Y meta
// with dx/dy. Replicate the forward check here so the backward op rejects
// K-mismatched inputs instead of silently returning results (aligning
// forward/backward contract and torch).
//
// Unlike the forward, only enforce when both contraction dims are concrete
// (!= -1). The forward runs this check at graph-build time with
// is_runtime=false so an unknown (-1) dim short-circuits it, whereas the
// backward InferMeta is invoked with the default MetaConfig (is_runtime=true)
// even at graph-build time. Keying on `config.is_runtime` here would wrongly
// reject legal dynamic-shape graphs (e.g. InputSpec([-1, -1])). A real
// runtime tensor never carries a -1 dim, so requiring both dims to be
// concrete still catches every genuine K mismatch (including K=0 vs N).
std::vector<int64_t> dims_x = common::vectorize(x.dims());
std::vector<int64_t> dims_y = common::vectorize(y.dims());
const auto ndims_x = dims_x.size();
const auto ndims_y = dims_y.size();
if (ndims_x > 0 && ndims_y > 0) {
const int64_t lhs_reduce_dim =
(ndims_x == 1) ? 0 : ndims_x - 1 - transpose_x;
const int64_t rhs_reduce_dim =
(ndims_y == 1) ? 0 : ndims_y - 2 + transpose_y;
const int64_t K_lhs = dims_x[lhs_reduce_dim];
const int64_t K_rhs = dims_y[rhs_reduce_dim];
if (K_lhs != -1 && K_rhs != -1) {
PADDLE_ENFORCE_EQ(
K_lhs,
K_rhs,
common::errors::InvalidArgument(
"In matmul_grad, the [%d] dimension of Input(X) must be equal "
"to the [%d] dimension of Input(Y). But received the [%d] "
"dimension of Input(X) is [%d], and the [%d] dimension of "
"Input(Y) is [%d].",
lhs_reduce_dim,
rhs_reduce_dim,
lhs_reduce_dim,
K_lhs,
rhs_reduce_dim,
K_rhs));
}
}
if (dx) {
dx->share_meta(x);
}
if (dy) {
dy->share_meta(y);
}
}

void GeneralTernaryGradInferMeta(const MetaTensor& x,
const MetaTensor& y,
const MetaTensor& z,
Expand Down
8 changes: 8 additions & 0 deletions paddle/phi/infermeta/backward.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ PADDLE_API void GeneralBinaryGradInferMeta(const MetaTensor& x,
MetaTensor* dx,
MetaTensor* dy);

PADDLE_API void MatmulGradInferMeta(const MetaTensor& x,
const MetaTensor& y,
bool transpose_x,
bool transpose_y,
MetaTensor* dx,
MetaTensor* dy,
MetaConfig config = MetaConfig());

PADDLE_API void GeneralTernaryGradInferMeta(const MetaTensor& x,
const MetaTensor& y,
const MetaTensor& z,
Expand Down
4 changes: 2 additions & 2 deletions paddle/phi/ops/yaml/inconsistent/dygraph_backward.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@
args : (Tensor x, Tensor y, Tensor out_grad, bool transpose_x=false, bool transpose_y=false)
output : Tensor(x_grad), Tensor(y_grad)
infer_meta :
func : GeneralBinaryGradInferMeta
param : [x, y]
func : MatmulGradInferMeta
param : [x, y, transpose_x, transpose_y]
spmd_rule : MatmulGradInferSpmd
kernel :
func : matmul_grad
Expand Down
8 changes: 4 additions & 4 deletions paddle/phi/ops/yaml/inconsistent/static_backward.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@
args : (Tensor x, Tensor y, Tensor out_grad, bool transpose_x=false, bool transpose_y=false, float alpha=1.0f)
output : Tensor(x_grad), Tensor(y_grad)
infer_meta :
func : GeneralBinaryGradInferMeta
param : [x, y]
func : MatmulGradInferMeta
param : [x, y, transpose_x, transpose_y]
kernel :
func : legacy_matmul_grad
param: [x, y, out_grad, transpose_x, transpose_y, alpha]
Expand Down Expand Up @@ -352,8 +352,8 @@
args : (Tensor x, Tensor y, Tensor out_grad, bool transpose_x=false, bool transpose_y=false)
output : Tensor(x_grad), Tensor(y_grad)
infer_meta :
func : GeneralBinaryGradInferMeta
param : [x, y]
func : MatmulGradInferMeta
param : [x, y, transpose_x, transpose_y]
spmd_rule : MatmulGradInferSpmd
kernel :
func : matmul_grad
Expand Down
4 changes: 2 additions & 2 deletions paddle/phi/ops/yaml/legacy/static_backward.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@
args : (Tensor x, Tensor y, Tensor out_grad, bool transpose_x=false, bool transpose_y=false)
output : Tensor(x_grad), Tensor(y_grad)
infer_meta :
func : GeneralBinaryGradInferMeta
param : [x, y]
func : MatmulGradInferMeta
param : [x, y, transpose_x, transpose_y]
kernel :
func : matmul_grad
data_type: out_grad
Expand Down
35 changes: 35 additions & 0 deletions test/legacy_test/test_matmul_0_size_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,41 @@ def test_matmul(self):
self.assertEqual(out[0].shape, x.shape)
self.assertEqual(out[1].shape, y.shape)

def test_matmul_grad_zero_k(self):
# A legit zero-size backward with a matching (K=0) contraction dim
# must keep working: [M, 0] . [0, N].
x = paddle.ones([8, 0], dtype="float32")
y = paddle.ones([0, 4], dtype="float32")
dz = paddle.ones([8, 4], dtype="float32")
out = _C_ops.matmul_grad(x, y, dz, False, False)
self.assertEqual(out[0].shape, x.shape)
self.assertEqual(out[1].shape, y.shape)

def test_matmul_grad_k_mismatch(self):
# The backward matmul_grad must reject a mismatched contraction (K)
# dim, matching the forward MatmulInferMeta check instead of blindly
# sharing X/Y meta with dx/dy.
# No transpose: X[..., K_x], Y[K_y, ...]; K_x != K_y.
x = paddle.ones([1024, 32], dtype="float32")
y = paddle.ones([64, 16], dtype="float32")
dz = paddle.ones([1024, 16], dtype="float32")
with self.assertRaises(ValueError):
_C_ops.matmul_grad(x, y, dz, False, False)

# transpose_x=True: reduction dim of X is the leading axis.
x = paddle.ones([32, 1024], dtype="float32")
y = paddle.ones([64, 16], dtype="float32")
dz = paddle.ones([1024, 16], dtype="float32")
with self.assertRaises(ValueError):
_C_ops.matmul_grad(x, y, dz, True, False)

# transpose_y=True: reduction dim of Y is the trailing axis.
x = paddle.ones([1024, 32], dtype="float32")
y = paddle.ones([16, 64], dtype="float32")
dz = paddle.ones([1024, 16], dtype="float32")
with self.assertRaises(ValueError):
_C_ops.matmul_grad(x, y, dz, False, True)


if __name__ == "__main__":
unittest.main()
Loading