Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 13 additions & 5 deletions python/paddle/distribution/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ def forward(self, x):
Returns:
Tensor: Outcome of forward transformation.
"""
if not isinstance(x, paddle.base.framework.Variable):
if not isinstance(
x, (paddle.base.framework.Variable, paddle.pir.Value)
):
raise TypeError(
f"Expected 'x' is a Tensor or Real, but got {type(x)}."
)
Expand All @@ -187,7 +189,9 @@ def inverse(self, y):
Returns:
Tensor: Outcome of inverse transform.
"""
if not isinstance(y, paddle.base.framework.Variable):
if not isinstance(
y, (paddle.base.framework.Variable, paddle.pir.Value)
):
raise TypeError(
f"Expected 'y' is a Tensor or Real, but got {type(y)}."
)
Expand All @@ -209,12 +213,14 @@ def forward_log_det_jacobian(self, x):
Returns:
Tensor: The log of the absolute value of Jacobian determinant.
"""
if not isinstance(x, paddle.base.framework.Variable):
if not isinstance(
x, (paddle.base.framework.Variable, paddle.pir.Value)
):
raise TypeError(
f"Expected 'y' is a Tensor or Real, but got {type(x)}."
)
if (
isinstance(x, paddle.base.framework.Variable)
isinstance(x, (paddle.base.framework.Variable, paddle.pir.Value))
and x.dim() < self._domain.event_rank
):
raise ValueError(
Expand All @@ -241,7 +247,9 @@ def inverse_log_det_jacobian(self, y):
Returns:
Tensor: The value of :math:`log|det J_{f^{-1}}(y)|`.
"""
if not isinstance(y, paddle.base.framework.Variable):
if not isinstance(
y, (paddle.base.framework.Variable, paddle.pir.Value)
):
raise TypeError(f"Expected 'y' is a Tensor, but got {type(y)}.")
if y.dim() < self._codomain.event_rank:
raise ValueError(
Expand Down
213 changes: 213 additions & 0 deletions test/distribution/test_distribution_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import paddle
from paddle.distribution import transform, variable
from paddle.pir_utils import test_with_pir_api

np.random.seed(2022)
paddle.seed(2022)
Expand Down Expand Up @@ -1067,6 +1068,31 @@ def test_forward(self, input, expected):
atol=config.ATOL.get(str(input.dtype)),
)

@param.param_func(
((np.ones((5, 10)), 1 / (1 + np.exp(-np.ones((5, 10))))),)
)
@test_with_pir_api
def test_forward_static(self, input, expected):
paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data('X', input.shape, input.dtype)
model = transform.SigmoidTransform()
out = model.forward(x)
place = (
paddle.CUDAPlace(0)
if paddle.core.is_compiled_with_cuda()
else paddle.CPUPlace()
)
exe = paddle.static.Executor(place)
(result,) = exe.run(feed={'X': input}, fetch_list=[out])
np.testing.assert_allclose(
result,
expected,
rtol=config.RTOL.get(str(input.dtype)),
atol=config.ATOL.get(str(input.dtype)),
)
paddle.disable_static()

@param.param_func(
((np.ones(10), np.log(np.ones(10)) - np.log1p(-np.ones(10))),)
)
Expand All @@ -1078,6 +1104,31 @@ def test_inverse(self, input, expected):
atol=config.ATOL.get(str(input.dtype)),
)

@param.param_func(
((np.ones(10), np.log(np.ones(10)) - np.log1p(-np.ones(10))),)
)
@test_with_pir_api
def test_inverse_static(self, input, expected):
paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data('X', input.shape, input.dtype)
model = transform.SigmoidTransform()
out = model.inverse(x)
place = (
paddle.CUDAPlace(0)
if paddle.core.is_compiled_with_cuda()
else paddle.CPUPlace()
)
exe = paddle.static.Executor(place)
(result,) = exe.run(feed={'X': input}, fetch_list=[out])
np.testing.assert_allclose(
result,
expected,
rtol=config.RTOL.get(str(input.dtype)),
atol=config.ATOL.get(str(input.dtype)),
)
paddle.disable_static()

@param.param_func(
(
(
Expand All @@ -1094,6 +1145,36 @@ def test_forward_log_det_jacobian(self, input, expected):
atol=config.ATOL.get(str(input.dtype)),
)

@param.param_func(
(
(
np.ones(10),
-_np_softplus(-np.ones(10)) - _np_softplus(np.ones(10)),
),
)
)
@test_with_pir_api
def test_forward_log_det_jacobian_static(self, input, expected):
paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data('X', input.shape, input.dtype)
model = transform.SigmoidTransform()
out = model.forward_log_det_jacobian(x)
place = (
paddle.CUDAPlace(0)
if paddle.core.is_compiled_with_cuda()
else paddle.CPUPlace()
)
exe = paddle.static.Executor(place)
(result,) = exe.run(feed={'X': input}, fetch_list=[out])
np.testing.assert_allclose(
result,
expected,
rtol=config.RTOL.get(str(input.dtype)),
atol=config.ATOL.get(str(input.dtype)),
)
paddle.disable_static()

@param.param_func([((), ()), ((2, 3, 5), (2, 3, 5))])
def test_forward_shape(self, shape, expected_shape):
self.assertEqual(self._t.forward_shape(shape), expected_shape)
Expand All @@ -1112,6 +1193,24 @@ def test_zerodim(self, input, expected):
self.assertEqual(self._t.forward_shape(x.shape), [])
self.assertEqual(self._t.inverse_shape(x.shape), [])

@param.param_func([(np.array(1.0), np.array(1.0))])
@test_with_pir_api
def test_zerodim_static(self, input, expected):
paddle.enable_static()
shape = ()
if paddle.framework.in_pir_mode():
shape = []
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data('X', input.shape, 'float32')
model = transform.SigmoidTransform()
self.assertEqual(model.forward(x).shape, shape)
self.assertEqual(model.inverse(x).shape, shape)
self.assertEqual(model.forward_log_det_jacobian(x).shape, shape)
self.assertEqual(model.inverse_log_det_jacobian(x).shape, shape)
self.assertEqual(model.forward_shape(x.shape), shape)
self.assertEqual(model.inverse_shape(x.shape), shape)
paddle.disable_static()


class TestSoftmaxTransform(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -1196,6 +1295,30 @@ def test_forward(self, input):
atol=config.ATOL.get(str(input.dtype)),
)

@param.param_func(((np.random.random(10),),))
@test_with_pir_api
def test_forward_static(self, input):
paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data('X', input.shape, input.dtype)
model = transform.StickBreakingTransform()
fwd = model.forward(x)
out = model.inverse(fwd)
place = (
paddle.CUDAPlace(0)
if paddle.core.is_compiled_with_cuda()
else paddle.CPUPlace()
)
exe = paddle.static.Executor(place)
(result,) = exe.run(feed={'X': input}, fetch_list=[out])
np.testing.assert_allclose(
result,
input,
rtol=config.RTOL.get(str(input.dtype)),
atol=config.ATOL.get(str(input.dtype)),
)
paddle.disable_static()

@param.param_func([((2, 3, 5), (2, 3, 6))])
def test_forward_shape(self, shape, expected_shape):
self.assertEqual(self._t.forward_shape(shape), expected_shape)
Expand All @@ -1210,6 +1333,24 @@ def test_forward_log_det_jacobian(self, x):
self._t.forward_log_det_jacobian(paddle.to_tensor(x)).shape, []
)

@param.param_func(((np.random.random(10),),))
@test_with_pir_api
def test_forward_log_det_jacobian_static(self, input):
paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data('X', input.shape, input.dtype)
model = transform.StickBreakingTransform()
out = model.forward_log_det_jacobian(x)
place = (
paddle.CUDAPlace(0)
if paddle.core.is_compiled_with_cuda()
else paddle.CPUPlace()
)
exe = paddle.static.Executor(place)
(result,) = exe.run(feed={'X': input}, fetch_list=[out])
self.assertEqual(result.shape, ())
paddle.disable_static()


# Todo
@param.place(config.DEVICES)
Expand Down Expand Up @@ -1243,6 +1384,29 @@ def test_forward(self, input):
tuple(self._t.forward(paddle.to_tensor(input)).shape), input.shape
)

@param.param_func(
[
(np.array([[0.0, 1.0, 2.0, 3.0]]),),
(np.array([[-5.0, 6.0, 7.0, 8.0]]),),
]
)
@test_with_pir_api
def test_forward_static(self, input):
paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data('X', input.shape, input.dtype)
model = transform.StackTransform(self.transforms, self.axis)
out = model.forward(x)
place = (
paddle.CUDAPlace(0)
if paddle.core.is_compiled_with_cuda()
else paddle.CPUPlace()
)
exe = paddle.static.Executor(place)
(result,) = exe.run(feed={'X': input}, fetch_list=[out])
self.assertEqual(tuple(result.shape), input.shape)
paddle.disable_static()

@param.param_func(
[
(np.array([[1.0, 2.0, 3.0]]),),
Expand All @@ -1258,6 +1422,33 @@ def test_inverse(self, input):
tuple(self._t.inverse(paddle.to_tensor(input)).shape), input.shape
)

@param.param_func(
[
(np.array([[1.0, 2.0, 3.0]]),),
(
np.array(
[[6.0, 7.0, 8.0]],
),
),
]
)
@test_with_pir_api
def test_inverse_static(self, input):
paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data('X', input.shape, input.dtype)
model = transform.StackTransform(self.transforms, self.axis)
out = model.inverse(x)
place = (
paddle.CUDAPlace(0)
if paddle.core.is_compiled_with_cuda()
else paddle.CPUPlace()
)
exe = paddle.static.Executor(place)
(result,) = exe.run(feed={'X': input}, fetch_list=[out])
self.assertEqual(tuple(result.shape), input.shape)
paddle.disable_static()

@param.param_func(
[(np.array([[1.0, 2.0, 3.0]]),), (np.array([[6.0, 7.0, 8.0]]),)]
)
Expand All @@ -1269,6 +1460,26 @@ def test_forward_log_det_jacobian(self, input):
input.shape,
)

@param.param_func(
[(np.array([[1.0, 2.0, 3.0]]),), (np.array([[6.0, 7.0, 8.0]]),)]
)
@test_with_pir_api
def test_forward_log_det_jacobian_static(self, input):
paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data('X', input.shape, input.dtype)
model = transform.StackTransform(self.transforms, self.axis)
out = model.forward_log_det_jacobian(x)
place = (
paddle.CUDAPlace(0)
if paddle.core.is_compiled_with_cuda()
else paddle.CPUPlace()
)
exe = paddle.static.Executor(place)
(result,) = exe.run(feed={'X': input}, fetch_list=[out])
self.assertEqual(tuple(result.shape), input.shape)
paddle.disable_static()

@param.param_func([((), ()), ((2, 3, 5), (2, 3, 5))])
def test_forward_shape(self, shape, expected_shape):
self.assertEqual(self._t.forward_shape(shape), expected_shape)
Expand All @@ -1287,10 +1498,12 @@ def test_axis(self):
([paddle.distribution.ExpTransform()], 'axis', TypeError),
]
)
@test_with_pir_api
def test_init_exception(self, transforms, axis, exc):
with self.assertRaises(exc):
paddle.distribution.StackTransform(transforms, axis)

@test_with_pir_api
def test_transforms(self):
self.assertIsInstance((self._t.transforms), typing.Sequence)

Expand Down