|
14 | 14 |
|
15 | 15 | import os |
16 | 16 | import unittest |
| 17 | +from unittest import TestCase |
17 | 18 |
|
18 | 19 | import numpy as np |
19 | 20 |
|
20 | 21 | import paddle |
| 22 | +import paddle.base.dygraph as dg |
21 | 23 | import paddle.static |
22 | 24 | from paddle import nn |
23 | 25 |
|
@@ -1519,5 +1521,62 @@ def call_func(self, x): |
1519 | 1521 | return out |
1520 | 1522 |
|
1521 | 1523 |
|
| 1524 | +class TestFunctionalConv2DTranspose_ZeroSize(TestCase): |
| 1525 | + def init_data(self): |
| 1526 | + self.input = np.random.randn(0, 4, 16, 4) |
| 1527 | + self.filter = np.random.randn(4, 3, 3, 3) |
| 1528 | + self.np_out = np.zeros([0, 3, 18, 6]) |
| 1529 | + |
| 1530 | + def setUp(self): |
| 1531 | + self.init_data() |
| 1532 | + self.bias = None |
| 1533 | + self.padding = 0 |
| 1534 | + self.stride = 1 |
| 1535 | + self.dilation = 1 |
| 1536 | + self.groups = 1 |
| 1537 | + self.data_format = "NCHW" |
| 1538 | + self.places = [] |
| 1539 | + if ( |
| 1540 | + os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() |
| 1541 | + in ['1', 'true', 'on'] |
| 1542 | + or not base.core.is_compiled_with_cuda() |
| 1543 | + ): |
| 1544 | + self.places.append(base.CPUPlace()) |
| 1545 | + if base.core.is_compiled_with_cuda(): |
| 1546 | + self.places.append(base.CUDAPlace(0)) |
| 1547 | + |
| 1548 | + def test_dygraph(self): |
| 1549 | + for place in self.places: |
| 1550 | + with dg.guard(place): |
| 1551 | + input = paddle.to_tensor(self.input) |
| 1552 | + input.stop_gradient = False |
| 1553 | + filter = paddle.to_tensor(self.filter) |
| 1554 | + filter.stop_gradient = False |
| 1555 | + y = paddle.nn.functional.conv2d_transpose( |
| 1556 | + input, |
| 1557 | + filter, |
| 1558 | + self.bias, |
| 1559 | + padding=self.padding, |
| 1560 | + stride=self.stride, |
| 1561 | + dilation=self.dilation, |
| 1562 | + groups=self.groups, |
| 1563 | + data_format=self.data_format, |
| 1564 | + ) |
| 1565 | + np.testing.assert_allclose(y.numpy(), self.np_out) |
| 1566 | + loss = y.sum() |
| 1567 | + loss.backward() |
| 1568 | + np.testing.assert_allclose(input.grad.shape, input.shape) |
| 1569 | + np.testing.assert_allclose(filter.grad, np.zeros(filter.shape)) |
| 1570 | + |
| 1571 | + |
| 1572 | +class TestFunctionalConv2DTranspose_ZeroSize2( |
| 1573 | + TestFunctionalConv2DTranspose_ZeroSize |
| 1574 | +): |
| 1575 | + def init_data(self): |
| 1576 | + self.input = np.random.randn(4, 5, 3, 3) |
| 1577 | + self.filter = np.random.randn(5, 0, 4, 4) |
| 1578 | + self.np_out = np.zeros([4, 0, 6, 6]) |
| 1579 | + |
| 1580 | + |
1522 | 1581 | if __name__ == '__main__': |
1523 | 1582 | unittest.main() |
0 commit comments