Skip to content

Commit

Permalink
[Zero-Dim] add upsample 0D test and fix api doc (#50237)
Browse files Browse the repository at this point in the history
* add upsample 0d test

* update api references

* update api references

* update api reference
  • Loading branch information
tink2123 committed Feb 7, 2023
1 parent 1a4a152 commit 1a0f349
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
40 changes: 40 additions & 0 deletions python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,24 @@ def test_interpolate(self):
origin_result.numpy(), out3.numpy(), rtol=1e-05
)

def test_upsample(self):
from paddle.nn.functional import upsample

input_x = paddle.rand([2, 3, 6, 6])
input_x.stop_gradient = False

output_size = [
paddle.full([], 12, dtype="int32"),
paddle.full([], 12, dtype="int32"),
]
out1 = upsample(
x=input_x, size=output_size, mode="bilinear", align_corners=False
)
out1.backward()

self.assertEqual(out1.shape, [2, 3, 12, 12])
self.assertEqual(input_x.grad.shape, [2, 3, 6, 6])

def test_maseked_select(self):
x = paddle.rand([])
x.stop_gradient = False
Expand Down Expand Up @@ -2512,6 +2530,28 @@ def test_interpolate(self):
self.assertEqual(res2[0].shape, (2, 3, 12, 12))
self.assertEqual(res2[1].shape, (2, 3, 6, 6))

@prog_scope()
def test_upsample(self):
from paddle.nn.functional import upsample

input_x = paddle.rand([2, 3, 6, 6])
input_x.stop_gradient = False

output_size = [
paddle.full([], 12, dtype="int32"),
paddle.full([], 12, dtype="int32"),
]

out1 = upsample(
x=input_x, size=output_size, mode="bilinear", align_corners=False
)
paddle.static.append_backward(out1.sum())
prog = paddle.static.default_main_program()
res1 = self.exe.run(prog, feed={}, fetch_list=[out1, input_x.grad_name])

self.assertEqual(res1[0].shape, (2, 3, 12, 12))
self.assertEqual(res1[1].shape, (2, 3, 6, 6))

@prog_scope()
def test_maseked_select(self):
x = paddle.rand([])
Expand Down
8 changes: 4 additions & 4 deletions python/paddle/nn/functional/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,11 @@ def interpolate(
size (list|tuple|Tensor|None): Output shape of image resize
layer, the shape is (out_w, ) when input is a 3-D Tensor, the shape is (out_h, out_w)
when input is a 4-D Tensor and is (out_d, out_h, out_w) when input is a 5-D Tensor.
Default: None. If a list/tuple, each element can be an integer or a Tensor of shape: [1].
Default: None. If a list/tuple, each element can be an integer or a Tensor of shape: [1] or [].
If a Tensor, its dimensions size should be a 1.
scale_factor (float|Tensor|list|tuple|None): The multiplier for the input height or width. At
least one of :attr:`size` or :attr:`scale_factor` must be set.
And :attr:`size` has a higher priority than :attr:`scale_factor`.Has to match input size if it is either a list or a tuple or a Tensor.
And :attr:`size` has a higher priority than :attr:`scale_factor`.Has to match input size if it is either a list or a tuple or a Tensor.If a list/tuple, each element can be an integer or a Tensor of shape: [1] or [].
Default: None.
mode (str): The resample method. It supports 'linear', 'area', 'nearest', 'bilinear',
'bicubic' and 'trilinear' currently. Default: 'nearest'
Expand Down Expand Up @@ -870,12 +870,12 @@ def upsample(
size (list|tuple|Tensor|None, optional): Output shape of image resize
layer, the shape is (out_w, ) when input is a 3-D Tensor, the shape is (out_h, out_w)
when input is a 4-D Tensor and is (out_d, out_h, out_w) when input is a 5-D Tensor.
Default: None. If a list/tuple, each element can be an integer or a Tensor of shape: [1].
Default: None. If a list/tuple, each element can be an integer or a Tensor of shape: [1] or [].
If a Tensor , its dimensions size should be a 1.
scale_factor (float|Tensor|list|tuple|None, optional): The multiplier for the input height or width. At
least one of :attr:`size` or :attr:`scale_factor` must be set.
And :attr:`size` has a higher priority than :attr:`scale_factor`.Has to match input size if
it is either a list or a tuple or a Tensor.
it is either a list or a tuple or a Tensor. If a list/tuple, each element can be an integer or a Tensor of shape: [1] or [].
Default: None.
mode (str, optional): The resample method. It supports 'linear', 'nearest', 'bilinear',
'bicubic' and 'trilinear' currently. Default: 'nearest'
Expand Down

0 comments on commit 1a0f349

Please sign in to comment.