Skip to content

[0-size Tensor No.256] Add 0-size Tensor support for paddle.sgn API - #73606

Merged
DanielSun11 merged 3 commits into
PaddlePaddle:developfrom
VVX94:fix-sgn
Jun 30, 2025
Merged

DanielSun11 merged 3 commits into
PaddlePaddle:developfrom
VVX94:fix-sgn

Conversation

@VVX94

@VVX94 VVX94 commented Jun 24, 2025 •

Copy link
Copy Markdown
Contributor

PR Category

Execute Infrastructure

PR Types

Improvements

Description

  • 错误信息
2025-06-05 15:25:30.646645 GPU 0 64602 test begin: paddle.sgn(Tensor([0, 4],"complex128"), )
[paddle error] paddle.sgn(Tensor([0, 4],"complex128"), ) 
 (InvalidArgument) Expected the stride of last dimension of input(X) to be 1.But received 2. This means that the last dimension of theTensor(x) is not continuous and cannot be as_complex directly.You can call x.contiguous() to make the Tensor(x) contiguous first.
  [Hint: Expected x.strides()[x.strides().size() - 1] == 1, but received x.strides()[x.strides().size() - 1]:2 != 1:1.] (at ../paddle/phi/kernels/stride/as_complex_kernel.cc:35)
  • paddle源代码
    • 发现问题在于paddle.as_real和paddle.as_complex,可能存在相关问题
def sgn(x, name=None):
    ...
    if paddle.is_complex(x):
        expand_x = paddle.as_real(x)
        ...
        return paddle.as_complex(output)
    else:
        return paddle.sign(x)

def as_real(x, name=None):
    if in_dynamic_or_pir_mode():
        return _C_ops.as_real(x)
    else:
       ...
def as_complex(x, name=None):
    if in_dynamic_or_pir_mode():
        return _C_ops.as_complex(x)
    else:
       ...
  • 检查对应的infetmeta,没有发现问题
    • AsRealInferMeta
    • AsComplexInferMeta
void AsRealInferMeta(const MetaTensor& input, MetaTensor* output) {
  auto out_dims_v = common::vectorize(input.dims());
  out_dims_v.push_back(2);
  auto out_dims = common::make_ddim(out_dims_v);
  output->set_dims(out_dims);
  output->share_lod(input);
  output->set_dtype(dtype::ToReal(input.dtype()));
}

void AsComplexInferMeta(const MetaTensor& input, MetaTensor* output) {
  auto in_dims = input.dims();
  const int input_rank = in_dims.size();
  PADDLE_ENFORCE_GE(
      input_rank,
      1,
      common::errors::InvalidArgument(
          "The rank of input(X) is less than 1. "
          "Expected the rank of input(X) to be equal to or greater than 1."
          "But received rank of input(X) = %d",
          input_rank));
  const int last_dim_size = static_cast<int>(in_dims[input_rank - 1]);
  PADDLE_ENFORCE_EQ(
      last_dim_size,
      2,
      common::errors::InvalidArgument(
          "The size of the last dimension of input(X)"
          "does not equals 2."
          "Expected the size of last dimension of input(X) to be 2."
          "But received %d",
          last_dim_size));

  const phi::DDim out_dims(in_dims.Get(), input_rank - 1);
  output->set_dims(out_dims);
  output->share_lod(input);
  output->set_dtype(dtype::ToComplex(input.dtype()));
}
  • 检查kernel函数,因调用了as_real和as_complex,对相应函数进行检查
    • 找到对应位置AsComplexStridedKernel,增加对0-size的检查
//as_complex_kernel.cc
void AsComplexStridedKernel(const Context& dev_ctx,
                            const DenseTensor& x,
                            DenseTensor* out) {
  if (!FLAGS_use_stride_kernel) {
    PADDLE_THROW(common::errors::Fatal(
        "FLAGS_use_stride_kernel is closed. Strided kernel "
        "be called, something wrong has happened!"));
  }

  PADDLE_ENFORCE_EQ(
      x.strides()[x.strides().size() - 1],
      1,
      common::errors::InvalidArgument(
          "Expected the stride of last dimension of input(X) to be 1."
          "But received %d. This means that the last dimension of the "
          "Tensor(x) is not continuous and cannot be as_complex directly."
          "You can call x.contiguous() to make the Tensor(x) contiguous first.",
          x.strides()[x.strides().size() - 1]));

    ...
}
  • 经检测,反向传播不存在0-size问题
  • 增加单测
  • PaddleAPITest通过
aa270e56690cfc7ba8479eaf23598ea

@paddle-bot paddle-bot Bot added the contributor External developers label Jun 24, 2025
@VVX94

VVX94 commented Jun 25, 2025

Copy link
Copy Markdown
Contributor Author

@luotao1 CI测试好像down了,而且无法重新构建,提示“无权限操作该流水线”

@luotao1 luotao1 added the HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务 label Jun 25, 2025
@luotao1

luotao1 commented Jun 26, 2025

Copy link
Copy Markdown
Collaborator

最近Github不太稳定,请重新提交一个commit再次触发

@VVX94

VVX94 commented Jun 27, 2025

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@VVX94

VVX94 commented Jun 28, 2025 •

Copy link
Copy Markdown
Contributor Author

@luotao1 @DanielSun11 求review

@DanielSun11 DanielSun11 left a comment

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.

LGTM

@DanielSun11
DanielSun11 merged commit e40ad6d into PaddlePaddle:develop Jun 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor External developers HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants