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

[Zero-Dim] support input 0D Tensor for distribution transform api #47677

Merged
merged 2 commits into from
Nov 8, 2022
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
26 changes: 16 additions & 10 deletions python/paddle/distribution/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
# limitations under the License.

import enum
import functools
import math
import operator
import typing

import paddle
Expand Down Expand Up @@ -401,7 +399,7 @@ def _inverse(self, y):
return -y, y

def _inverse_log_det_jacobian(self, y):
zero = paddle.zeros([1], dtype=y.dtype)
zero = paddle.zeros([], dtype=y.dtype)
return zero, zero

@property
Expand Down Expand Up @@ -872,12 +870,16 @@ def __init__(self, in_event_shape, out_event_shape):
f"Squence[int], but got 'in_event_shape': {in_event_shape}, "
f"'out_event_shape': {out_event_shape}"
)
if functools.reduce(operator.mul, in_event_shape) != functools.reduce(
operator.mul, out_event_shape
):
in_size = 1
for e in in_event_shape:
in_size *= e
out_size = 1
for e in out_event_shape:
out_size *= e
if in_size != out_size:
raise ValueError(
f"The numel of 'in_event_shape' should be 'out_event_shape', "
f"but got {functools.reduce(operator.mul, in_event_shape)}!={functools.reduce(operator.mul, out_event_shape)}"
f"but got {in_size}!={out_size}"
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 Author

@zhwesky2010 zhwesky2010 Nov 8, 2022

Choose a reason for hiding this comment

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

0D Tensor的shape为[],numel为1,用functools.reduce无法实现,会报空列表的错误,所以就自行实现了

)

self._in_event_shape = tuple(in_event_shape)
Expand Down Expand Up @@ -917,7 +919,9 @@ def _forward_shape(self, shape):
raise ValueError(
f"Expected length of 'shape' is not less than {len(self._in_event_shape)}, but got {len(shape)}"
)
if shape[-len(self._in_event_shape) :] != self._in_event_shape:
if tuple(shape[-len(self._in_event_shape) :]) != tuple(
self._in_event_shape
):
Copy link
Contributor

Choose a reason for hiding this comment

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

建议只读类型用tuple

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

raise ValueError(
f"Event shape mismatch, expected: {self._in_event_shape}, but got {shape[-len(self._in_event_shape):]}"
)
Expand All @@ -930,7 +934,9 @@ def _inverse_shape(self, shape):
raise ValueError(
f"Expected 'shape' length is not less than {len(self._out_event_shape)}, but got {len(shape)}"
)
if shape[-len(self._out_event_shape) :] != self._out_event_shape:
if tuple(shape[-len(self._out_event_shape) :]) != tuple(
self._out_event_shape
):
raise ValueError(
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 Author

Choose a reason for hiding this comment

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

Done

f"Event shape mismatch, expected: {self._out_event_shape}, but got {shape[-len(self._out_event_shape):]}"
)
Expand All @@ -939,7 +945,7 @@ def _inverse_shape(self, shape):
)

def _forward_log_det_jacobian(self, x):
# paddle.zeros not support zero dimension Tensor.
# TODO(zhouwei): should not set shape to [1], which is []
shape = x.shape[: x.dim() - len(self._in_event_shape)] or [1]
return paddle.zeros(shape, dtype=x.dtype)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def wrapper(f, instance=None):
frame_locals[name].__doc__ = doc_func(f, num, p)

# Delete original patches to prevent new function from evaluating
# original patching object as well as re-constructed patches.
# original patching object as well as re-constrfucted patches.
delete_patches_if_need(f)

f.__test__ = False
Comment on lines +106 to 109

Choose a reason for hiding this comment

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

貌似有 typo

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ def test_forward_shape(self, shape, expected_shape):
def test_inverse_shape(self, shape, expected_shape):
self.assertEqual(self._t.forward_shape(shape), expected_shape)

@param.param_func([(np.array(1.0), np.array(1.0))])
def test_zerodim(self, input, expected):
x = paddle.to_tensor(input).astype('float32')
self.assertEqual(self._t.forward(x).shape, [])
self.assertEqual(self._t.inverse(x)[0].shape, [])
self.assertEqual(self._t.inverse(x)[1].shape, [])
self.assertEqual(self._t.inverse_log_det_jacobian(x)[0].shape, [])
self.assertEqual(self._t.inverse_log_det_jacobian(x)[1].shape, [])
self.assertEqual(self._t.forward_shape(x.shape), [])
self.assertEqual(self._t.inverse_shape(x.shape), [])


@param.place(config.DEVICES)
@param.param_cls(
Expand Down Expand Up @@ -297,6 +308,18 @@ def test_inverse_shape(self):
np.broadcast(np.random.random(shape), self.loc, self.scale).shape,
)

@param.param_func([(np.array(1.0), np.array(1.0))])
def test_zerodim(self, input, expected):
affine = transform.AffineTransform(paddle.zeros([]), paddle.ones([]))

x = paddle.to_tensor(input).astype('float32')
self.assertEqual(affine.forward(x).shape, [])
self.assertEqual(affine.inverse(x).shape, [])
self.assertEqual(affine.forward_log_det_jacobian(x).shape, [])
self.assertEqual(affine.inverse_log_det_jacobian(x).shape, [])
self.assertEqual(affine.forward_shape(x.shape), ())
self.assertEqual(affine.inverse_shape(x.shape), ())


@param.place(config.DEVICES)
class TestExpTransform(unittest.TestCase):
Expand Down Expand Up @@ -395,6 +418,16 @@ def test_forward_shape(self, shape, expected_shape):
def test_inverse_shape(self, shape, expected_shape):
self.assertEqual(self._t.forward_shape(shape), expected_shape)

@param.param_func([(np.array(1.0), np.array(1.0))])
def test_zerodim(self, input, expected):
x = paddle.to_tensor(input).astype('float32')
self.assertEqual(self._t.forward(x).shape, [])
self.assertEqual(self._t.inverse(x).shape, [])
self.assertEqual(self._t.forward_log_det_jacobian(x).shape, [])
self.assertEqual(self._t.inverse_log_det_jacobian(x).shape, [])
self.assertEqual(self._t.forward_shape(x.shape), [])
self.assertEqual(self._t.inverse_shape(x.shape), [])


@param.place(config.DEVICES)
class TestChainTransform(unittest.TestCase):
Expand Down Expand Up @@ -785,6 +818,18 @@ def test_forward_shape(self, shape, expected_shape):
def test_inverse_shape(self, shape, expected_shape):
self.assertEqual(self._t.forward_shape(shape), expected_shape)

@param.param_func([(np.array(2.0), np.array(1.0))])
def test_zerodim(self, input, expected):
power = transform.PowerTransform(paddle.full([], 2.0))

x = paddle.to_tensor(input).astype('float32')
self.assertEqual(power.forward(x).shape, [])
self.assertEqual(power.inverse(x).shape, [])
self.assertEqual(power.forward_log_det_jacobian(x).shape, [])
self.assertEqual(power.inverse_log_det_jacobian(x).shape, [])
self.assertEqual(power.forward_shape(x.shape), ())
self.assertEqual(power.inverse_shape(x.shape), ())


@param.place(config.DEVICES)
class TestTanhTransform(unittest.TestCase):
Expand Down Expand Up @@ -892,6 +937,16 @@ def test_forward_shape(self, shape, expected_shape):
def test_inverse_shape(self, shape, expected_shape):
self.assertEqual(self._t.forward_shape(shape), expected_shape)

@param.param_func([(np.array(1.0), np.array(1.0))])
def test_zerodim(self, input, expected):
x = paddle.to_tensor(input).astype('float32')
self.assertEqual(self._t.forward(x).shape, [])
self.assertEqual(self._t.inverse(x).shape, [])
self.assertEqual(self._t.forward_log_det_jacobian(x).shape, [])
self.assertEqual(self._t.inverse_log_det_jacobian(x).shape, [])
self.assertEqual(self._t.forward_shape(x.shape), [])
self.assertEqual(self._t.inverse_shape(x.shape), [])


@param.place(config.DEVICES)
@param.param_cls(
Expand Down Expand Up @@ -965,6 +1020,20 @@ def test_inverse_shape_exception(self, shape, exc):
with self.assertRaises(exc):
self._t.inverse_shape(shape)

@param.param_func([(np.array(2.0), np.array(1.0))])
def test_zerodim(self, input, expected):
reshape = transform.ReshapeTransform((), (1, 1))

x = paddle.to_tensor(input).astype('float32')
out = reshape.forward(x)

self.assertEqual(out.shape, [1, 1])
self.assertEqual(reshape.inverse(out).shape, [])
# self.assertEqual(reshape.forward_log_det_jacobian(x).shape, [])
# self.assertEqual(reshape.inverse_log_det_jacobian(out).shape, [])
self.assertEqual(reshape.forward_shape(x.shape), (1, 1))
self.assertEqual(reshape.inverse_shape(out.shape), ())


def _np_softplus(x, beta=1.0, threshold=20.0):
if np.any(beta * x > threshold):
Expand Down Expand Up @@ -1031,6 +1100,16 @@ def test_forward_shape(self, shape, expected_shape):
def test_inverse_shape(self, shape, expected_shape):
self.assertEqual(self._t.forward_shape(shape), expected_shape)

@param.param_func([(np.array(1.0), np.array(1.0))])
def test_zerodim(self, input, expected):
x = paddle.to_tensor(input).astype('float32')
self.assertEqual(self._t.forward(x).shape, [])
self.assertEqual(self._t.inverse(x).shape, [])
self.assertEqual(self._t.forward_log_det_jacobian(x).shape, [])
self.assertEqual(self._t.inverse_log_det_jacobian(x).shape, [])
self.assertEqual(self._t.forward_shape(x.shape), [])
self.assertEqual(self._t.inverse_shape(x.shape), [])


class TestSoftmaxTransform(unittest.TestCase):
def setUp(self):
Expand Down