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
3 changes: 3 additions & 0 deletions paddle/phi/kernels/cpu/gelu_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ void GeluGradKernel(const Context& dev_ctx,
bool approximate,
DenseTensor* x_grad) {
dev_ctx.template Alloc<T>(x_grad);
if (x_grad && x_grad->numel() == 0) {
return;
}
auto eigen_x = EigenVector<T>::Flatten(x);
auto eigen_out_grad = EigenVector<T>::Flatten(out_grad);
auto eigen_x_grad = EigenVector<T>::Flatten(*x_grad);
Expand Down
3 changes: 3 additions & 0 deletions paddle/phi/kernels/cpu/gelu_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ void GeluKernel(const Context& dev_ctx,
bool approximate,
DenseTensor* out) {
dev_ctx.template Alloc<T>(out);
if (out && out->numel() == 0) {
return;
}
auto eigen_out = EigenVector<T>::Flatten(*out);
auto eigen_x = EigenVector<T>::Flatten(x);
auto& dev = *dev_ctx.eigen_device();
Expand Down
3 changes: 3 additions & 0 deletions paddle/phi/kernels/gpu/gelu_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ void GeluGradKernel(const Context& dev_ctx,
bool approximate,
DenseTensor* x_grad) {
dev_ctx.template Alloc<T>(x_grad);
if (x_grad && x_grad->numel() == 0) {
return;
}
std::vector<const DenseTensor*> ins = {&x, &out_grad};
std::vector<DenseTensor*> outs = {x_grad};
if (approximate) {
Expand Down
3 changes: 3 additions & 0 deletions paddle/phi/kernels/gpu/gelu_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ void GeluKernel(const Context& dev_ctx,
bool approximate,
DenseTensor* out) {
dev_ctx.template Alloc<T>(out);
if (out && out->numel() == 0) {
return;
}
std::vector<const DenseTensor*> ins = {&x};
std::vector<DenseTensor*> outs = {out};
if (approximate) {
Expand Down
3 changes: 3 additions & 0 deletions paddle/phi/kernels/xpu/gelu_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ void GeluGradKernel(const Context& dev_ctx,
DenseTensor* x_grad) {
using XPUType = typename XPUTypeTrait<T>::Type;
dev_ctx.template Alloc<T>(x_grad);
if (x_grad && x_grad->numel() == 0) {
return;
}
int r = xpu::gelu_grad<XPUType>(
dev_ctx.x_context(),
reinterpret_cast<const XPUType*>(x.data<T>()),
Expand Down
3 changes: 3 additions & 0 deletions paddle/phi/kernels/xpu/gelu_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ void GeluKernel(const Context& dev_ctx,
DenseTensor* out) {
using XPUType = typename XPUTypeTrait<T>::Type;
dev_ctx.template Alloc<T>(out);
if (out && out->numel() == 0) {
return;
}
int r = xpu::gelu<XPUType>(dev_ctx.x_context(),
reinterpret_cast<const XPUType*>(x.data<T>()),
reinterpret_cast<XPUType*>(out->data<T>()),
Expand Down
40 changes: 40 additions & 0 deletions test/legacy_test/test_gelu_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,45 @@ def run_gelu_op(approximate):
)


class TestGeluOp_ZeroSize(unittest.TestCase):
def _test_case1_cpu(self, approximate):
x = np.random.uniform(-1, 1, size=(0, 17)).astype(np.float32)
y_ref = gelu(x, approximate)

place = base.CPUPlace()
with dg.guard(place) as g:
x_var = paddle.to_tensor(x)
x_var.stop_gradient = False
y_var = F.gelu(x_var, approximate)
y_test = y_var.numpy()

loss = paddle.sum(y_var)
loss.backward()
np.testing.assert_allclose(y_ref, y_test, rtol=1e-05, atol=1e-08)
np.testing.assert_allclose(x_var.grad.shape, x_var.shape)

def _test_case1_gpu(self, approximate):
x = np.random.uniform(-1, 1, size=(0, 17)).astype(np.float32)
y_ref = gelu(x, approximate)

place = base.CUDAPlace(0)
with dg.guard(place) as g:
x_var = paddle.to_tensor(x)
x_var.stop_gradient = False
y_var = F.gelu(x_var, approximate)
y_test = y_var.numpy()

loss = paddle.sum(y_var)
loss.backward()
np.testing.assert_allclose(y_ref, y_test, rtol=1e-05, atol=1e-08)
np.testing.assert_allclose(x_var.grad.shape, x_var.shape)

def test_cases(self):
for approximate in [True, False]:
self._test_case1_cpu(approximate)
if base.is_compiled_with_cuda():
self._test_case1_gpu(approximate)


if __name__ == '__main__':
unittest.main()