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

【Paddle Tensor 第二期 API支持 0-size Tensor】paddle.full_like 支持 0-size tensor-part #70077

Merged
merged 3 commits into from
Dec 13, 2024

Conversation

SCUcookie
Copy link
Contributor

PR Category

User Experience

PR Types

Bug fixes

Description

添加输入等于0时的情况判断

Copy link

paddle-bot bot commented Dec 9, 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.

@paddle-bot paddle-bot bot added the contributor External developers label Dec 9, 2024
@luotao1 luotao1 added the HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务 label Dec 10, 2024
Comment on lines 42 to 44
if (out->numel() == 0) {
return; // Handle 0-size Tensor: no allocation or computation needed.
}
Copy link
Contributor

Choose a reason for hiding this comment

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

这里得加一行红框里的代码:
image
其他地方也同理,不然没有place信息了

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的老师

Copy link
Contributor Author

Choose a reason for hiding this comment

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

其他地方好像都在上下文加过了

Comment on lines +106 to +108
if (out->numel() == 0) {
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

这里是否应该移动到109行下方

Comment on lines 55 to 58
out->Resize(x.dims());
if (out->numel() == 0) {
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. out->Resize是新加的逻辑,放到if分支里
  2. if分支里缺少Alloc

@@ -105,12 +105,12 @@ void FullBatchSizeLikeKernel(const Context& dev_ctx,
int out_batch_size_dim,
DenseTensor* out) {
if (x.lod().size() && x_batch_size_dim == 0) {
// set the correct batch size for the DenseTensor.
Copy link
Contributor

Choose a reason for hiding this comment

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

注释不要删除

Comment on lines 111 to 114
} else {
FullLikeKernel<T, Context>(dev_ctx, x, val, dtype, out);
}
FullLikeKernel<T, Context>(dev_ctx, x, val, dtype, out);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

这里好像不用改,

Comment on lines 224 to 225
shape = [2, 3]
value = 5
Copy link
Contributor

Choose a reason for hiding this comment

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

这里shape不是一个0-size,这个单测的意义是?

)
def test_full_kernel_gpu(self):
paddle.disable_static()
shape = [2, 3]
Copy link
Contributor

Choose a reason for hiding this comment

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

同上

Copy link
Contributor

Choose a reason for hiding this comment

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

这个单测文件有测试0-size的case吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的老师

Copy link
Contributor Author

Choose a reason for hiding this comment

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

请老师看一眼不知道这样对不对

Copy link
Contributor

@HydrogenSulfate HydrogenSulfate 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 changed the title 【Paddle Tensor 第二期 API支持 0-size Tensor】paddle.full_like 支持 0-size tensor 【Paddle Tensor 第二期 API支持 0-size Tensor】paddle.full_like 支持 0-size tensor-part Dec 13, 2024
@luotao1
Copy link
Contributor

luotao1 commented Dec 13, 2024

image @SCUcookie 这部分单侧写法下一个PR修改

Comment on lines +221 to +269
class TestFullKernelZeroSize(unittest.TestCase):
def test_full_kernel_cpu_zero_size(self):
paddle.disable_static()
value = 5
dtype = "int32"
shape = [0, 3]
tensor = paddle.full(shape, value, dtype=dtype)
expected = np.full(shape, value, dtype=dtype)
self.assertTrue(np.array_equal(tensor.numpy(), expected))
paddle.enable_static()

@unittest.skipIf(
not core.is_compiled_with_cuda(), "Paddle is not compiled with CUDA"
)
def test_full_kernel_gpu_zero_size(self):
paddle.disable_static()
paddle.set_device("gpu:0")
value = 5.5
dtype = "float32"
shape = [0, 3]
tensor = paddle.full(shape, value, dtype=dtype)
expected = np.full(shape, value, dtype=dtype)
self.assertTrue(np.array_equal(tensor.numpy(), expected))
paddle.enable_static()


class TestFullLikeKernelZeroSize(unittest.TestCase):
def test_full_like_kernel_cpu_zero_size(self):
paddle.disable_static()
base_tensor = paddle.to_tensor(np.empty((0, 2), dtype=np.float32))
value = 10.0
result = paddle.full_like(base_tensor, value, dtype="float32")
expected = np.full_like(base_tensor.numpy(), value)
self.assertTrue(np.array_equal(result.numpy(), expected))
paddle.enable_static()

@unittest.skipIf(
not core.is_compiled_with_cuda(), "Paddle is not compiled with CUDA"
)
def test_full_like_kernel_gpu_zero_size(self):
paddle.disable_static()
base_tensor = paddle.to_tensor(
np.empty((0, 3), dtype=np.float32), place=paddle.CUDAPlace(0)
)
value = 20.0
result = paddle.full_like(base_tensor, value, dtype="float32")
expected = np.full_like(base_tensor.numpy(), value)
self.assertTrue(np.array_equal(result.numpy(), expected))
paddle.enable_static()
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. enable/disable的切换使用guard
  2. self.assertTrue(np.array_equal/allclose(result.numpy(), expected))这类判断使用 np.testing.assert_array_equal/allclose

@HydrogenSulfate
Copy link
Contributor

@SCUcookie 下个PR按照review意见,改一下单测的代码

@HydrogenSulfate HydrogenSulfate merged commit 15aa29e into PaddlePaddle:develop Dec 13, 2024
28 checks passed
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