Files
paddlepaddle--paddle/test/legacy_test/test_api_compatibility_part3.py
T
2026-07-13 12:40:42 +08:00

5959 lines
209 KiB
Python

# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
import sys
import unittest
import numpy as np
import paddle
class TestDistributionsCategoricalAPI(unittest.TestCase):
def setUp(self):
self.place = paddle.CPUPlace()
self.np_probs = np.array(
[[0.2, 0.3, 0.5], [2.0, 3.0, 5.0]], dtype="float32"
)
self.np_logits = np.array(
[[0.1, 0.2, 0.7], [1.5, 0.5, 0.25]], dtype="float32"
)
self.np_zero_probs = np.array(
[[0.0, 1.0, 0.0], [0.5, 0.0, 0.5]], dtype="float32"
)
self.np_value = np.array([2, 0], dtype="int64")
self.np_zero_value = np.array([1, 1], dtype="int64")
def tearDown(self):
paddle.enable_static()
def _logsumexp(self, x):
max_value = np.max(x, axis=-1, keepdims=True)
return (
np.log(np.sum(np.exp(x - max_value), axis=-1, keepdims=True))
+ max_value
)
def _take_torch_last_dim(self, x, value=None):
if value is None:
value = self.np_value
return np.take_along_axis(x, value.reshape([-1, 1]), axis=-1).squeeze(
-1
)
def test_dygraph_Compatibility(self):
import importlib
categorical = importlib.import_module(
"paddle.compat.distributions.categorical"
)
self.assertIs(
paddle.compat.distributions,
importlib.import_module("paddle.compat.distributions"),
)
self.assertIs(paddle.compat.distributions, paddle.compat.distributions)
paddle.disable_static()
probs = paddle.to_tensor(self.np_probs, place=self.place)
logits = paddle.to_tensor(self.np_logits, place=self.place)
zero_probs = paddle.to_tensor(self.np_zero_probs, place=self.place)
value = paddle.to_tensor(self.np_value, place=self.place)
zero_value = paddle.to_tensor(self.np_zero_value, place=self.place)
# 1. PyTorch Positional arguments
dist1 = categorical.Categorical(probs)
out1 = dist1.log_prob(value)
# 2. PyTorch keyword arguments
dist2 = categorical.Categorical(probs=probs)
out2 = dist2.log_prob(value)
# 3. PyTorch Positional arguments
dist3 = categorical.Categorical(None, logits, False)
out3 = dist3.log_prob(value)
# 4. PyTorch keyword arguments
dist4 = categorical.Categorical(
probs=None, logits=logits, validate_args=False
)
out4 = dist4.log_prob(value)
# 5. Mixed arguments
dist5 = categorical.Categorical(None, logits=logits)
out5 = dist5.log_prob(value)
dist6 = categorical.Categorical(probs=zero_probs)
out6 = dist6.log_prob(zero_value)
probs_ref = self.np_probs / self.np_probs.sum(-1, keepdims=True)
logits_ref = self.np_logits - self._logsumexp(self.np_logits)
zero_probs_ref = self.np_zero_probs / self.np_zero_probs.sum(
-1, keepdims=True
)
paddle_probs_ref = self._take_torch_last_dim(np.log(probs_ref))
eps = np.finfo(self.np_zero_probs.dtype).eps
zero_logits_ref = np.log(np.clip(zero_probs_ref, eps, 1 - eps))
zero_log_prob_ref = self._take_torch_last_dim(
zero_logits_ref, self.np_zero_value
)
for out in [out1, out2]:
np.testing.assert_allclose(out.numpy(), paddle_probs_ref, rtol=1e-6)
for out in [out3, out4]:
np.testing.assert_allclose(
out.numpy(),
self._take_torch_last_dim(logits_ref),
rtol=1e-6,
)
np.testing.assert_allclose(
out5.numpy(), self._take_torch_last_dim(logits_ref), rtol=1e-6
)
np.testing.assert_allclose(dist1.probs.numpy(), probs_ref, rtol=1e-6)
np.testing.assert_allclose(
dist1.logits.numpy(), np.log(probs_ref), rtol=1e-6
)
np.testing.assert_allclose(
dist3.probs.numpy(), np.exp(logits_ref), rtol=1e-6
)
np.testing.assert_allclose(
dist3.entropy().numpy(),
-(logits_ref * np.exp(logits_ref)).sum(-1),
rtol=1e-6,
)
np.testing.assert_allclose(out6.numpy(), zero_log_prob_ref, rtol=1e-6)
np.testing.assert_allclose(
dist6.logits.numpy(), zero_logits_ref, rtol=1e-6
)
np.testing.assert_allclose(
dist6.entropy().numpy(),
-(zero_logits_ref * zero_probs_ref).sum(-1),
rtol=1e-6,
)
self.assertEqual(tuple(dist1.param_shape), self.np_probs.shape)
self.assertTrue(np.isnan(dist1.mean.numpy()).all())
self.assertTrue(np.isnan(dist1.variance.numpy()).all())
np.testing.assert_array_equal(dist3.mode.numpy(), np.array([2, 0]))
np.testing.assert_array_equal(
dist1.support.check(value).numpy(), np.array([True, True])
)
self.assertFalse(dist3._validate_args_enabled)
self.assertFalse(dist4._validate_args_enabled)
self.assertEqual(list(dist1.sample([2]).shape), [2, 2])
self.assertEqual(list(dist1.sample(np.array([3])).shape), [3, 2])
self.assertEqual(
categorical.Categorical(paddle.to_tensor([0.4, 0.6])).batch_shape,
(),
)
expanded = categorical.Categorical(
logits=paddle.to_tensor([0.1, 0.2, 0.7], place=self.place)
)
_ = expanded.probs
expanded = expanded.expand((4,))
self.assertEqual(expanded.batch_shape, (4,))
self.assertEqual(tuple(expanded.param_shape), (4, 3))
np.testing.assert_allclose(
expanded.probs.numpy(),
np.tile([[0.25462854, 0.28140804, 0.46396342]], (4, 1)),
rtol=1e-6,
)
def test_dygraph_Error(self):
import importlib
categorical = importlib.import_module(
"paddle.compat.distributions.categorical"
)
paddle.disable_static()
probs = paddle.to_tensor(self.np_probs, place=self.place)
logits = paddle.to_tensor(self.np_logits, place=self.place)
with self.assertRaises(ValueError):
categorical.Categorical()
with self.assertRaises(ValueError):
categorical.Categorical(probs=probs, logits=logits)
with self.assertRaises(ValueError):
categorical.Categorical(paddle.to_tensor(1.0))
with self.assertRaises(ValueError):
categorical.Categorical(logits=paddle.to_tensor(1.0))
def test_dygraph_validate_args(self):
import importlib
categorical = importlib.import_module(
"paddle.compat.distributions.categorical"
)
paddle.disable_static()
probs = paddle.to_tensor([0.2, 0.3, 0.5], place=self.place)
dist = categorical.Categorical(probs=probs, validate_args=True)
batched_dist = categorical.Categorical(
probs=paddle.to_tensor(self.np_probs, place=self.place),
validate_args=True,
)
dist.log_prob(paddle.to_tensor([0, 2], place=self.place))
with self.assertRaises(ValueError):
dist.log_prob(paddle.to_tensor([3], place=self.place))
with self.assertRaises(ValueError):
dist.log_prob(
paddle.to_tensor([1.5], dtype="float32", place=self.place)
)
with self.assertRaises(ValueError):
batched_dist.log_prob(paddle.to_tensor([0, 1, 2], place=self.place))
def test_dygraph_enumerate_support(self):
import importlib
categorical = importlib.import_module(
"paddle.compat.distributions.categorical"
)
paddle.disable_static()
probs = paddle.to_tensor(self.np_probs, place=self.place)
dist = categorical.Categorical(probs=probs)
es = dist.enumerate_support()
es0 = dist.enumerate_support(expand=False)
support = dist.support
self.assertEqual(list(es.shape), [3, 2])
self.assertEqual(list(es0.shape), [3, 1])
np.testing.assert_array_equal(
es.numpy(), np.array([[0, 0], [1, 1], [2, 2]], dtype="int64")
)
np.testing.assert_array_equal(
es0.numpy(), np.array([[0], [1], [2]], dtype="int64")
)
np.testing.assert_array_equal(
support.check(paddle.to_tensor([0, 2], place=self.place)).numpy(),
np.array([True, True]),
)
np.testing.assert_array_equal(
support.check(
paddle.to_tensor(
[-1, 1.5, 3], dtype="float32", place=self.place
)
).numpy(),
np.array([False, False, False]),
)
def test_static_Compatibility(self):
import importlib
categorical = importlib.import_module(
"paddle.compat.distributions.categorical"
)
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
probs = paddle.static.data(
name="probs", shape=self.np_probs.shape, dtype="float32"
)
logits = paddle.static.data(
name="logits", shape=self.np_logits.shape, dtype="float32"
)
zero_probs = paddle.static.data(
name="zero_probs",
shape=self.np_zero_probs.shape,
dtype="float32",
)
value = paddle.static.data(
name="value", shape=self.np_value.shape, dtype="int64"
)
zero_value = paddle.static.data(
name="zero_value",
shape=self.np_zero_value.shape,
dtype="int64",
)
# 1. PyTorch Positional arguments
dist1 = categorical.Categorical(probs)
out1 = dist1.log_prob(value)
# 2. PyTorch keyword arguments
dist2 = categorical.Categorical(probs=probs)
out2 = dist2.log_prob(value)
# 3. PyTorch Positional arguments
dist3 = categorical.Categorical(None, logits, False)
out3 = dist3.log_prob(value)
# 4. PyTorch keyword arguments
dist4 = categorical.Categorical(
probs=None, logits=logits, validate_args=False
)
out4 = dist4.log_prob(value)
# 5. Mixed arguments
dist5 = categorical.Categorical(None, logits=logits)
out5 = dist5.log_prob(value)
dist6 = categorical.Categorical(probs=zero_probs)
out6 = dist6.log_prob(zero_value)
exe = paddle.static.Executor(self.place)
fetches = exe.run(
main,
feed={
"probs": self.np_probs,
"logits": self.np_logits,
"zero_probs": self.np_zero_probs,
"value": self.np_value,
"zero_value": self.np_zero_value,
},
fetch_list=[
out1,
out2,
out3,
out4,
out5,
out6,
dist1.probs,
dist1.logits,
dist3.probs,
dist3.entropy(),
dist3.mode,
dist1.mean,
dist1.variance,
dist1.support.check(value),
dist6.logits,
dist6.entropy(),
],
)
probs_ref = self.np_probs / self.np_probs.sum(-1, keepdims=True)
logits_ref = self.np_logits - self._logsumexp(self.np_logits)
zero_probs_ref = self.np_zero_probs / self.np_zero_probs.sum(
-1, keepdims=True
)
paddle_probs_ref = self._take_torch_last_dim(np.log(probs_ref))
eps = np.finfo(self.np_zero_probs.dtype).eps
zero_logits_ref = np.log(np.clip(zero_probs_ref, eps, 1 - eps))
zero_log_prob_ref = self._take_torch_last_dim(
zero_logits_ref, self.np_zero_value
)
for out in fetches[:2]:
np.testing.assert_allclose(out, paddle_probs_ref, rtol=1e-6)
for out in fetches[2:4]:
np.testing.assert_allclose(
out, self._take_torch_last_dim(logits_ref), rtol=1e-6
)
np.testing.assert_allclose(
fetches[4], self._take_torch_last_dim(logits_ref), rtol=1e-6
)
np.testing.assert_allclose(fetches[5], zero_log_prob_ref, rtol=1e-6)
np.testing.assert_allclose(fetches[6], probs_ref, rtol=1e-6)
np.testing.assert_allclose(fetches[7], np.log(probs_ref), rtol=1e-6)
np.testing.assert_allclose(fetches[8], np.exp(logits_ref), rtol=1e-6)
np.testing.assert_allclose(
fetches[9], -(logits_ref * np.exp(logits_ref)).sum(-1), rtol=1e-6
)
np.testing.assert_array_equal(fetches[10], np.array([2, 0]))
self.assertTrue(np.isnan(fetches[11]).all())
self.assertTrue(np.isnan(fetches[12]).all())
np.testing.assert_array_equal(fetches[13], np.array([True, True]))
np.testing.assert_allclose(fetches[14], zero_logits_ref, rtol=1e-6)
np.testing.assert_allclose(
fetches[15], -(zero_logits_ref * zero_probs_ref).sum(-1), rtol=1e-6
)
def test_paddle_api_BackwardCompatibility(self):
import importlib
categorical = importlib.import_module("paddle.distribution.categorical")
paddle.disable_static()
logits = paddle.to_tensor(self.np_probs, place=self.place)
value = paddle.to_tensor(self.np_value, place=self.place)
# 1. Paddle Positional arguments
dist1 = categorical.Categorical(logits)
out1 = dist1.log_prob(value)
# 2. Paddle keyword arguments
dist2 = categorical.Categorical(logits=logits)
out2 = dist2.log_prob(value)
probs_ref = self.np_probs / self.np_probs.sum(-1, keepdims=True)
ref_out = np.log(
np.take_along_axis(
probs_ref, self.np_value.reshape([1, -1]), axis=-1
)
)
for out in [out1, out2]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
logits = paddle.static.data(
name="paddle_logits",
shape=self.np_probs.shape,
dtype="float32",
)
value = paddle.static.data(
name="paddle_value", shape=self.np_value.shape, dtype="int64"
)
# 1. Paddle Positional arguments
out3 = categorical.Categorical(logits).log_prob(value)
# 2. Paddle keyword arguments
out4 = categorical.Categorical(logits=logits).log_prob(value)
exe = paddle.static.Executor(self.place)
fetches = exe.run(
main,
feed={
"paddle_logits": self.np_probs,
"paddle_value": self.np_value,
},
fetch_list=[out3, out4],
)
for out in fetches:
np.testing.assert_allclose(out, ref_out, rtol=1e-6)
class TestDistributionsPositiveDefiniteCheckAPI(unittest.TestCase):
def setUp(self):
self.place = paddle.CPUPlace()
self.np_x = np.array([[2.0, 0.5], [0.5, 1.0]], dtype="float32")
self.np_not_positive = np.array(
[[1.0, 2.0], [2.0, 1.0]], dtype="float32"
)
self.np_non_symmetric = np.array(
[[1.0, 2.0], [0.0, 1.0]], dtype="float32"
)
self.np_not_square = np.ones([2, 3], dtype="float32")
self.np_vector = np.ones([2], dtype="float32")
def tearDown(self):
paddle.enable_static()
def test_dygraph_Compatibility(self):
from paddle.distribution import constraints
paddle.disable_static()
x = paddle.to_tensor(self.np_x, place=self.place)
not_positive = paddle.to_tensor(self.np_not_positive, place=self.place)
not_square = paddle.to_tensor(self.np_not_square, place=self.place)
vector = paddle.to_tensor(self.np_vector, place=self.place)
self.assertIs(paddle.distribution.constraints, constraints)
# 1. Paddle Positional arguments
out1 = paddle.distribution.constraint.positive_definite.check(x)
# 2. Paddle keyword arguments
out2 = paddle.distribution.constraint.positive_definite.check(value=x)
# 3. PyTorch Positional arguments
out3 = constraints.positive_definite.check(x)
# 4. PyTorch keyword arguments
out4 = constraints.positive_definite.check(value=x)
out5 = constraints.positive_definite.check(not_positive)
out6 = constraints.positive_definite.check(not_square)
out7 = constraints.positive_definite.check(vector)
for out in [out1, out2, out3, out4]:
np.testing.assert_array_equal(out.numpy(), np.array(True))
for out in [out5, out6, out7]:
np.testing.assert_array_equal(out.numpy(), np.array(False))
def test_static_Compatibility(self):
from paddle.distribution import constraints
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype="float32"
)
not_positive = paddle.static.data(
name="not_positive",
shape=self.np_not_positive.shape,
dtype="float32",
)
not_square = paddle.static.data(
name="not_square",
shape=self.np_not_square.shape,
dtype="float32",
)
vector = paddle.static.data(
name="vector", shape=self.np_vector.shape, dtype="float32"
)
# 1. PyTorch Positional arguments
out1 = constraints.positive_definite.check(x)
# 2. PyTorch keyword arguments
out2 = constraints.positive_definite.check(value=x)
# 3. PyTorch Positional arguments
out3 = constraints.positive_definite.check(not_positive)
# 4. PyTorch Positional arguments
out4 = constraints.positive_definite.check(not_square)
# 5. PyTorch Positional arguments
out5 = constraints.positive_definite.check(vector)
exe = paddle.static.Executor(self.place)
fetches = exe.run(
main,
feed={
"x": self.np_x,
"not_positive": self.np_not_positive,
"not_square": self.np_not_square,
"vector": self.np_vector,
},
fetch_list=[out1, out2, out3, out4, out5],
)
for out in fetches[:2]:
np.testing.assert_array_equal(out, np.array(True))
for out in fetches[2:]:
np.testing.assert_array_equal(out, np.array(False))
def test_symmetric_low_rank_returns_false(self):
from paddle.distribution import constraint
paddle.disable_static()
out = constraint.symmetric.check(paddle.ones([2]))
self.assertFalse(bool(out))
symmetric_matrix = paddle.to_tensor(
[[1.0, 2.0], [2.0, 1.0]], place=self.place
)
self.assertTrue(bool(constraint.symmetric.check(symmetric_matrix)))
non_symmetric_matrix = paddle.to_tensor(
self.np_non_symmetric, place=self.place
)
self.assertFalse(bool(constraint.symmetric.check(non_symmetric_matrix)))
def test_static_symmetric_low_rank_returns_false(self):
from paddle.distribution import constraints
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
vector = paddle.static.data(
name="vector", shape=self.np_vector.shape, dtype="float32"
)
symmetric_matrix = paddle.static.data(
name="symmetric_matrix", shape=self.np_x.shape, dtype="float32"
)
non_symmetric_matrix = paddle.static.data(
name="non_symmetric_matrix",
shape=self.np_non_symmetric.shape,
dtype="float32",
)
out1 = constraints.symmetric.check(vector)
out2 = constraints.symmetric.check(symmetric_matrix)
out3 = constraints.symmetric.check(non_symmetric_matrix)
exe = paddle.static.Executor(self.place)
fetches = exe.run(
main,
feed={
"vector": self.np_vector,
"symmetric_matrix": self.np_x,
"non_symmetric_matrix": self.np_non_symmetric,
},
fetch_list=[out1, out2, out3],
)
np.testing.assert_array_equal(fetches[0], np.array(False))
np.testing.assert_array_equal(fetches[1], np.array(True))
np.testing.assert_array_equal(fetches[2], np.array(False))
# Test mv compatibility
@unittest.skipIf(
paddle.is_compiled_with_custom_device('iluvatar_gpu'),
"skip iluvatar_gpu which not register mv kernel",
)
class TestMvAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_x = np.random.rand(3, 4).astype("float32")
self.np_vec = np.random.rand(4).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
vec = paddle.to_tensor(self.np_vec)
# 1. Paddle Positional arguments
out1 = paddle.mv(x, vec)
# 2. Paddle keyword arguments
out2 = paddle.mv(x=x, vec=vec)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.mv(input=x, vec=vec)
# 4. Mixed arguments
out4 = paddle.mv(x, vec=vec)
# 5-6. out parameter test
out5 = paddle.zeros([3], dtype="float32")
out6 = paddle.mv(x, vec, out=out5)
# 7. Tensor method - args
out7 = x.mv(vec)
# 8. Tensor method - kwargs (PyTorch alias)
out8 = x.mv(vec=vec)
# Verify all outputs
ref_out = np.dot(self.np_x, self.np_vec)
for out in [out1, out2, out3, out4, out5, out6, out7, out8]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-5)
self.assertEqual(out.shape, (3,))
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
vec = paddle.static.data(
name="vec",
shape=self.np_vec.shape,
dtype=str(self.np_vec.dtype),
)
# 1. Paddle Positional arguments
out1 = paddle.mv(x, vec)
# 2. Paddle keyword arguments
out2 = paddle.mv(x=x, vec=vec)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.mv(input=x, vec=vec)
# 4. Tensor method - args
out4 = x.mv(vec)
# 5. Tensor method - kwargs (PyTorch alias)
out5 = x.mv(vec=vec)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x, "vec": self.np_vec},
fetch_list=[out1, out2, out3, out4, out5],
)
# Verify all outputs
ref_out = np.dot(self.np_x, self.np_vec)
for out in fetches:
np.testing.assert_allclose(out, ref_out, rtol=1e-5)
class TestDiagflatAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([1, 2, 3]).astype('int64')
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.diagflat(x)
# 2. Paddle keyword arguments
out2 = paddle.diagflat(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.diagflat(input=x)
# 4. Mixed arguments
out4 = paddle.diagflat(x, offset=0)
# 5. Tensor method - args
out5 = x.diagflat()
# 6. Tensor method - kwargs (PyTorch alias)
out6 = x.diagflat()
# Verify all outputs
expected_diag = np.diag(self.np_x)
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_array_equal(out.numpy(), expected_diag)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
# 1. Paddle Positional arguments
out1 = paddle.diagflat(x)
# 2. Paddle keyword arguments
out2 = paddle.diagflat(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.diagflat(input=x)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x},
fetch_list=[out1, out2, out3],
)
# Verify all outputs
expected_diag = np.diag(self.np_x)
for out in fetches:
np.testing.assert_array_equal(out, expected_diag)
@unittest.skipIf(
paddle.is_compiled_with_custom_device('iluvatar_gpu'),
"skip iluvatar_gpu which not register fill_diagonal_tensor kernel",
)
class TestDiagonalScatterAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.arange(6.0).reshape((2, 3))
self.np_y = np.ones((2,))
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
y = paddle.to_tensor(self.np_y)
# 1. Paddle Positional arguments
out1 = paddle.diagonal_scatter(x, y)
# 2. Paddle keyword arguments
out2 = paddle.diagonal_scatter(x=x, y=y)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.diagonal_scatter(input=x, src=y)
# 4. Mixed arguments with dim1/dim2 aliases
out4 = paddle.diagonal_scatter(x, y, dim1=0, dim2=1)
# 5. Tensor method - args
out5 = x.diagonal_scatter(y)
# 6. Tensor method - kwargs (PyTorch alias)
out6 = x.diagonal_scatter(src=y)
# Verify all outputs
expected = np.array([[1.0, 1.0, 2.0], [3.0, 1.0, 5.0]])
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_allclose(out.numpy(), expected)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
y = paddle.static.data(
name="y", shape=self.np_y.shape, dtype=str(self.np_y.dtype)
)
# 1. Paddle Positional arguments
out1 = paddle.diagonal_scatter(x, y)
# 2. Paddle keyword arguments
out2 = paddle.diagonal_scatter(x=x, y=y)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.diagonal_scatter(input=x, src=y)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x, "y": self.np_y},
fetch_list=[out1, out2, out3],
)
# Verify all outputs
expected = np.array([[1.0, 1.0, 2.0], [3.0, 1.0, 5.0]])
for out in fetches:
np.testing.assert_allclose(out, expected)
class TestLdexpAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([1, 2, 3], dtype='float32')
self.np_y = np.array([2, 3, 4], dtype='int32')
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
y = paddle.to_tensor(self.np_y)
# 1. Paddle Positional arguments
out1 = paddle.ldexp(x, y)
# 2. Paddle keyword arguments
out2 = paddle.ldexp(x=x, y=y)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.ldexp(input=x, other=y)
# 4. Mixed arguments
out4 = paddle.ldexp(x, y=y)
# 5-6. out parameter test
out5 = paddle.empty_like(x)
out6 = paddle.ldexp(x, y, out=out5)
assert out5 is out6
# 7. Tensor method - args
out7 = x.ldexp(y)
# 8. Tensor method - kwargs (PyTorch alias)
out8 = x.ldexp(other=y)
# Verify all outputs
expected = np.array([4.0, 16.0, 48.0], dtype='float32')
for out in [out1, out2, out3, out4, out5, out6, out7, out8]:
np.testing.assert_allclose(out.numpy(), expected, rtol=1e-5)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
y = paddle.static.data(
name="y", shape=self.np_y.shape, dtype=str(self.np_y.dtype)
)
# 1. Paddle Positional arguments
out1 = paddle.ldexp(x, y)
# 2. Paddle keyword arguments
out2 = paddle.ldexp(x=x, y=y)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.ldexp(input=x, other=y)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x, "y": self.np_y},
fetch_list=[out1, out2, out3],
)
# Verify all outputs
expected = np.array([4.0, 16.0, 48.0], dtype='float32')
for out in fetches:
np.testing.assert_allclose(out, expected, rtol=1e-5)
# Test inner compatibility
class TestInnerAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([1.0, 2.0, 3.0, 4.0])
self.np_y = np.array([5.0, 6.0, 7.0, 8.0])
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
y = paddle.to_tensor(self.np_y)
# 1. Paddle Positional arguments
out1 = paddle.inner(x, y)
# 2. Paddle keyword arguments
out2 = paddle.inner(x=x, y=y)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.inner(input=x, other=y)
# 4. Mixed arguments
out4 = paddle.inner(x, other=y)
# 5-6. out parameter test
out5 = paddle.empty_like(out1)
out6 = paddle.inner(x, y, out=out5)
assert out5 is out6
# 7. Tensor method - args
out7 = x.inner(y)
# 8. Tensor method - kwargs (PyTorch alias)
out8 = x.inner(other=y)
# Verify all outputs
expected = np.dot(self.np_x, self.np_y)
for out in [out1, out2, out3, out4, out5, out6, out7, out8]:
np.testing.assert_allclose(out.numpy(), expected, rtol=1e-6)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
y = paddle.static.data(
name="y", shape=self.np_y.shape, dtype=str(self.np_y.dtype)
)
# 1. Paddle Positional arguments
out1 = paddle.inner(x, y)
# 2. Paddle keyword arguments
out2 = paddle.inner(x=x, y=y)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.inner(input=x, other=y)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x, "y": self.np_y},
fetch_list=[out1, out2, out3],
)
# Verify all outputs
expected = np.dot(self.np_x, self.np_y)
for out in fetches:
np.testing.assert_allclose(out, expected, rtol=1e-6)
# Test positive compatibility
class TestPositiveAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-1, 0, 1], dtype='int64')
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.positive(x)
# 2. Paddle keyword arguments
out2 = paddle.positive(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.positive(input=x)
# Verify all outputs
expected = self.np_x
for out in [out1, out2, out3]:
np.testing.assert_allclose(out.numpy(), expected, rtol=1e-5)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
# 1. Paddle Positional arguments
out1 = paddle.positive(x)
# 2. Paddle keyword arguments
out2 = paddle.positive(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.positive(input=x)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x},
fetch_list=[out1, out2, out3],
)
# Verify all outputs
expected = self.np_x
for out in fetches:
np.testing.assert_allclose(out, expected, rtol=1e-5)
# Test rad2deg compatibility
class TestRad2degAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([3.142, -3.142, 6.283, -6.283], dtype='float32')
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.rad2deg(x)
# 2. Paddle keyword arguments
out2 = paddle.rad2deg(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.rad2deg(input=x)
# 4-5. out parameter test
out4 = paddle.empty_like(x)
out5 = paddle.rad2deg(x, out=out4)
# 6. Tensor method
out6 = x.rad2deg()
# Verify all outputs
expected = 180.0 / np.pi * self.np_x
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_allclose(out.numpy(), expected, rtol=1e-5)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
# 1. Paddle Positional arguments
out1 = paddle.rad2deg(x)
# 2. Paddle keyword arguments
out2 = paddle.rad2deg(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.rad2deg(input=x)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x},
fetch_list=[out1, out2, out3],
)
# Verify all outputs
expected = 180.0 / np.pi * self.np_x
for out in fetches:
np.testing.assert_allclose(out, expected, rtol=1e-5)
# Test rot90 compatibility
class TestRot90API(unittest.TestCase):
def setUp(self):
self.np_x = np.arange(4, dtype='float32').reshape(2, 2)
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.rot90(x, 1, [0, 1])
# 2. Paddle keyword arguments
out2 = paddle.rot90(x=x, k=1, axes=[0, 1])
# 3. PyTorch keyword arguments (alias)
out3 = paddle.rot90(input=x, k=1, dims=[0, 1])
# 4. Mixed arguments
out4 = paddle.rot90(x, k=1, dims=[0, 1])
# 5. Tensor method - args
out5 = x.rot90(1, [0, 1])
# 6. Tensor method - kwargs (PyTorch alias)
out6 = x.rot90(k=1, dims=[0, 1])
# Verify all outputs
expected = np.array([[1.0, 3.0], [0.0, 2.0]], dtype='float32')
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_allclose(out.numpy(), expected, rtol=1e-5)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
# 1. Paddle Positional arguments
out1 = paddle.rot90(x, 1, [0, 1])
# 2. Paddle keyword arguments
out2 = paddle.rot90(x=x, k=1, axes=[0, 1])
# 3. PyTorch keyword arguments (alias)
out3 = paddle.rot90(input=x, k=1, dims=[0, 1])
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x},
fetch_list=[out1, out2, out3],
)
# Verify all outputs
expected = np.array([[1.0, 3.0], [0.0, 2.0]], dtype='float32')
for out in fetches:
np.testing.assert_allclose(out, expected, rtol=1e-5)
# Test nanquantile compatibility
class TestNanquantileAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_x = np.random.rand(2, 5).astype("float32")
self.np_x[0, 0] = np.nan
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.nanquantile(x, 0.5, 0)
# 2. Paddle keyword arguments
out2 = paddle.nanquantile(x=x, q=0.5, axis=0)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.nanquantile(input=x, q=0.5, dim=0)
# 4. Mixed arguments
out4 = paddle.nanquantile(x, 0.5, dim=0)
# 5-6. out parameter test
out5 = paddle.empty_like(out1)
out6 = paddle.nanquantile(x, 0.5, axis=0, out=out5)
assert out5 is out6
# 7. Tensor method - args
out7 = x.nanquantile(0.5, 0)
# 8. Tensor method - kwargs (PyTorch alias)
out8 = x.nanquantile(q=0.5, dim=0)
# Verify all outputs
for out in [out1, out2, out3, out4, out5, out6, out7, out8]:
np.testing.assert_allclose(out.numpy(), out1.numpy(), rtol=1e-5)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
# 1. Paddle Positional arguments
out1 = paddle.nanquantile(x, 0.5, 0)
# 2. Paddle keyword arguments
out2 = paddle.nanquantile(x=x, q=0.5, axis=0)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.nanquantile(input=x, q=0.5, dim=0)
# 4. Tensor method - args
out4 = x.nanquantile(0.5, 0)
# 5. Tensor method - kwargs (PyTorch alias)
out5 = x.nanquantile(q=0.5, dim=0)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x},
fetch_list=[out1, out2, out3, out4, out5],
)
# Verify all outputs
for out in fetches:
np.testing.assert_allclose(out, fetches[0], rtol=1e-5)
# Test neg compatibility
class TestNegAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.4, -0.2, 0.1, 0.3], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.neg(x)
# 2. Paddle keyword arguments
out2 = paddle.neg(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.neg(input=x)
# 4-5. out parameter test
out4 = paddle.empty_like(out1)
out5 = paddle.neg(x, out=out4)
assert out4 is out5
# 6. Tensor method
out6 = x.neg()
# Verify all outputs
expected = -self.np_x
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_allclose(out.numpy(), expected, rtol=1e-5)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
# 1. Paddle Positional arguments
out1 = paddle.neg(x)
# 2. Paddle keyword arguments
out2 = paddle.neg(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.neg(input=x)
# 4. Tensor method
out4 = x.neg()
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x},
fetch_list=[out1, out2, out3, out4],
)
# Verify all outputs
expected = -self.np_x
for out in fetches:
np.testing.assert_allclose(out, expected, rtol=1e-5)
# Test randint compatibility
class TestRandintAPI(unittest.TestCase):
def test_dygraph_Compatibility(self):
paddle.disable_static()
# basic shape
x = paddle.randint(high=10, shape=[2, 3])
self.assertEqual(x.shape, [2, 3])
self.assertTrue(x.stop_gradient)
# 'size' is an alias for 'shape'
x = paddle.randint(high=10, size=[3, 4])
self.assertEqual(x.shape, [3, 4])
# requires_grad
x = paddle.randint(high=10, shape=[2, 3], requires_grad=True)
self.assertFalse(x.stop_gradient)
x = paddle.randint(high=10, shape=[2, 3], requires_grad=False)
self.assertTrue(x.stop_gradient)
# value range
x = paddle.randint(low=5, high=10, shape=[100])
arr = x.numpy()
self.assertTrue(np.all(arr >= 5) and np.all(arr < 10))
# torch.randint(high, size) style: second positional arg as shape
x = paddle.randint(10, [3, 4])
self.assertEqual(x.shape, [3, 4])
self.assertTrue(np.all(x.numpy() >= 0) and np.all(x.numpy() < 10))
# dtype string
x = paddle.randint(high=10, shape=[3], dtype='int32')
self.assertEqual(x.dtype, paddle.int32)
# out param
out = paddle.zeros([2, 3], dtype='int64')
result = paddle.randint(high=10, shape=[2, 3], out=out)
self.assertEqual(out.shape, [2, 3])
np.testing.assert_array_equal(result.numpy(), out.numpy())
# out with requires_grad
out = paddle.zeros([2, 3], dtype='int64')
result = paddle.randint(
high=10, shape=[2, 3], out=out, requires_grad=True
)
self.assertFalse(result.stop_gradient)
paddle.enable_static()
def test_static_Compatibility(self):
# basic shape and stop_gradient
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.randint(high=10, shape=[2, 3])
self.assertEqual(x.shape, [2, 3])
self.assertTrue(x.stop_gradient)
# requires_grad
x_grad = paddle.randint(high=10, shape=[2, 3], requires_grad=True)
self.assertFalse(x_grad.stop_gradient)
x_no_grad = paddle.randint(
high=10, shape=[2, 3], requires_grad=False
)
self.assertTrue(x_no_grad.stop_gradient)
# size alias
x_size = paddle.randint(high=10, size=[2, 3])
self.assertEqual(x_size.shape, [2, 3])
# dtype string
x_dtype = paddle.randint(high=10, shape=[3], dtype='int32')
exe = paddle.static.Executor(paddle.CPUPlace())
exe.run(startup)
result = exe.run(main, fetch_list=[x_dtype])
self.assertEqual(result[0].dtype, np.int32)
# Test remainder_ inplace compatibility
class TestRemainderInplaceAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_x = np.random.randint(1, 20, [5, 6]).astype("int64")
self.np_y = np.random.randint(1, 10, [5, 6]).astype("int64")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
y = paddle.to_tensor(self.np_y)
# 1. Paddle Positional arguments
out1 = paddle.remainder_(x.clone(), y)
# 2. Paddle keyword arguments
out2 = paddle.remainder_(x=x.clone(), y=y)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.remainder_(input=x.clone(), other=y)
# 4. Mixed arguments
out4 = paddle.remainder_(x.clone(), y=y)
# 5. Tensor method - args
out5 = x.clone().remainder_(y)
# 6. Tensor method - kwargs (PyTorch alias)
out6 = x.clone().remainder_(other=y)
# Verify all outputs
ref_out = np.mod(self.np_x, self.np_y)
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_array_equal(ref_out, out.numpy())
# Test remainder_ inplace compatibility
class TestModInplaceAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_x = np.random.randint(1, 20, [5, 6]).astype("int64")
self.np_y = np.random.randint(1, 10, [5, 6]).astype("int64")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
y = paddle.to_tensor(self.np_y)
# 1. Paddle Positional arguments
out1 = paddle.mod_(x.clone(), y)
# 2. Paddle keyword arguments
out2 = paddle.mod_(x=x.clone(), y=y)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.floor_mod_(input=x.clone(), other=y)
# 4. Mixed arguments
out4 = paddle.floor_mod_(x.clone(), y=y)
# 5. Tensor method - args
out5 = x.clone().mod_(y)
# 6. Tensor method - kwargs (PyTorch alias)
out6 = x.clone().floor_mod_(other=y)
# Verify all outputs
ref_out = np.mod(self.np_x, self.np_y)
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_array_equal(ref_out, out.numpy())
# Test squeeze compatibility
class TestSqueezeAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_x = np.random.rand(1, 3, 1, 5).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments (axis=None)
out1 = paddle.squeeze(x)
# 2. Paddle Positional arguments (axis=int)
out2 = paddle.squeeze(x, 0)
# 3. Paddle keyword arguments
out3 = paddle.squeeze(x=x, axis=0)
# 4. PyTorch keyword arguments (alias)
out4 = paddle.squeeze(input=x, dim=0)
# 5. Mixed arguments
out5 = paddle.squeeze(x, axis=0)
# 6. Tensor method - args
out6 = x.squeeze(0)
# 7. Tensor method - kwargs (PyTorch alias)
out7 = x.squeeze(dim=0)
ref_out_none = np.squeeze(self.np_x)
np.testing.assert_allclose(out1.numpy(), ref_out_none)
# Verify all outputs
ref_out = np.squeeze(self.np_x, axis=0)
for out in [out2, out3, out4, out5, out6, out7]:
np.testing.assert_allclose(out.numpy(), ref_out)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
# 1. Paddle Positional arguments
out1 = paddle.squeeze(x, 0)
# 2. Paddle keyword arguments
out2 = paddle.squeeze(x=x, axis=0)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.squeeze(input=x, dim=0)
# 4. Tensor method - args
out4 = x.squeeze(0)
# 5. Tensor method - kwargs (PyTorch alias)
out5 = x.squeeze(dim=0)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x},
fetch_list=[out1, out2, out3, out4, out5],
)
# Verify all outputs
ref_out = np.squeeze(self.np_x, axis=0)
for out in fetches:
np.testing.assert_allclose(out, ref_out)
# Test squeeze_ inplace compatibility
class TestSqueezeInplaceAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_x = np.random.rand(1, 3, 1, 5).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.squeeze_(x.clone(), 0)
# 2. Paddle keyword arguments
out2 = paddle.squeeze_(x=x.clone(), axis=0)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.squeeze_(input=x.clone(), dim=0)
# 4. Mixed arguments
out4 = paddle.squeeze_(x.clone(), axis=0)
# 5. Tensor method - args
out5 = x.clone().squeeze_(0)
# 6. Tensor method - kwargs (PyTorch alias)
out6 = x.clone().squeeze_(dim=0)
# Verify all outputs
ref_out = np.squeeze(self.np_x, axis=0)
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_allclose(out.numpy(), ref_out)
# Test unsqueeze compatibility
class TestUnsqueezeAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_x = np.random.rand(5, 10).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.unsqueeze(x, 0)
# 2. Paddle keyword arguments
out2 = paddle.unsqueeze(x=x, axis=0)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.unsqueeze(input=x, dim=0)
# 4. Mixed arguments
out4 = paddle.unsqueeze(x, axis=0)
# 5. Tensor method - args
out5 = x.unsqueeze(0)
# 6. Tensor method - kwargs (PyTorch alias)
out6 = x.unsqueeze(dim=0)
# Verify all outputs
ref_out = np.expand_dims(self.np_x, axis=0)
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_allclose(out.numpy(), ref_out)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
# 1. Paddle Positional arguments
out1 = paddle.unsqueeze(x, 0)
# 2. Paddle keyword arguments
out2 = paddle.unsqueeze(x=x, axis=0)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.unsqueeze(input=x, dim=0)
# 4. Tensor method - args
out4 = x.unsqueeze(0)
# 5. Tensor method - kwargs (PyTorch alias)
out5 = x.unsqueeze(dim=0)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x},
fetch_list=[out1, out2, out3, out4, out5],
)
# Verify all outputs
ref_out = np.expand_dims(self.np_x, axis=0)
for out in fetches:
np.testing.assert_allclose(out, ref_out)
# Test unsqueeze_ inplace compatibility
class TestUnsqueezeInplaceAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_x = np.random.rand(5, 10).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.unsqueeze_(x.clone(), 0)
# 2. Paddle keyword arguments
out2 = paddle.unsqueeze_(x=x.clone(), axis=0)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.unsqueeze_(input=x.clone(), dim=0)
# 4. Mixed arguments
out4 = paddle.unsqueeze_(x.clone(), axis=0)
# 5. Tensor method - args
out5 = x.clone().unsqueeze_(0)
# 6. Tensor method - kwargs (PyTorch alias)
out6 = x.clone().unsqueeze_(dim=0)
# Verify all outputs
ref_out = np.expand_dims(self.np_x, axis=0)
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_allclose(out.numpy(), ref_out)
# Test pow_ inplace compatibility
class TestPowInplaceAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_x = np.random.rand(5, 6).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
y_scalar = 2.0
# 1. Paddle Positional arguments
out1 = paddle.pow_(x.clone(), y_scalar)
# 2. Paddle keyword arguments
out2 = paddle.pow_(x=x.clone(), y=y_scalar)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.pow_(input=x.clone(), exponent=y_scalar)
# 4. Mixed arguments
out4 = paddle.pow_(x.clone(), y=y_scalar)
# 5. Tensor method - args
out5 = x.clone().pow_(y_scalar)
# 6. Tensor method - kwargs (PyTorch alias)
out6 = x.clone().pow_(exponent=y_scalar)
# Verify all outputs
ref_out = np.power(self.np_x, y_scalar)
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-5)
# Test floor_divide_ inplace compatibility
class TestFloorDivideInplaceAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_x = np.random.randint(10, 100, [5, 6]).astype("int64")
self.np_y = np.random.randint(1, 10, [5, 6]).astype("int64")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
y = paddle.to_tensor(self.np_y)
# 1. Paddle Positional arguments
out1 = paddle.floor_divide_(x.clone(), y)
# 2. Paddle keyword arguments
out2 = paddle.floor_divide_(x=x.clone(), y=y)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.floor_divide_(input=x.clone(), other=y)
# 4. Mixed arguments
out4 = paddle.floor_divide_(x.clone(), y=y)
# 5. Tensor method - args
out5 = x.clone().floor_divide_(y)
# 6. Tensor method - kwargs (PyTorch alias)
out6 = x.clone().floor_divide_(other=y)
# Verify all outputs
ref_out = np.floor_divide(self.np_x, self.np_y)
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_array_equal(out.numpy(), ref_out)
# Test isposinf compatibility
class TestIsposinfAPICompatibility(unittest.TestCase):
def setUp(self):
self.np_x = np.array(
[[1.0, np.inf, -np.inf], [0.0, -1.0, np.inf]]
).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.isposinf(x)
# 2. Paddle keyword arguments
out2 = paddle.isposinf(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.isposinf(input=x)
# 4-5. out parameter test
out4 = paddle.zeros_like(out1)
out5 = paddle.isposinf(x, out=out4)
self.assertIs(out4, out5)
# 6. Tensor method
out6 = x.isposinf()
# Verify all outputs
ref_out = np.isposinf(self.np_x)
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_array_equal(ref_out, out.numpy())
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
# 1. Paddle Positional arguments
out1 = paddle.isposinf(x)
# 2. Paddle keyword arguments
out2 = paddle.isposinf(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.isposinf(input=x)
# 4. Tensor method - args
out4 = x.isposinf()
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x},
fetch_list=[out1, out2, out3, out4],
)
# Verify all outputs
ref_out = np.isposinf(self.np_x)
for out in fetches:
np.testing.assert_array_equal(ref_out, out)
# Test isneginf compatibility
class TestIsneginfAPICompatibility(unittest.TestCase):
def setUp(self):
self.np_x = np.array(
[[1.0, np.inf, -np.inf], [0.0, -1.0, np.inf]]
).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.isneginf(x)
# 2. Paddle keyword arguments
out2 = paddle.isneginf(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.isneginf(input=x)
# 4-5. out parameter test
out4 = paddle.zeros_like(out1)
out5 = paddle.isneginf(x, out=out4)
assert out4 is out5
# 6. Tensor method
out6 = x.isneginf()
# Verify all outputs
ref_out = np.isneginf(self.np_x)
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_array_equal(ref_out, out.numpy())
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
# 1. Paddle Positional arguments
out1 = paddle.isneginf(x)
# 2. Paddle keyword arguments
out2 = paddle.isneginf(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.isneginf(input=x)
# 4. Tensor method - args
out4 = x.isneginf()
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x},
fetch_list=[out1, out2, out3, out4],
)
# Verify all outputs
ref_out = np.isneginf(self.np_x)
for out in fetches:
np.testing.assert_array_equal(ref_out, out)
# Test isreal compatibility
class TestIsRealAPICompatibility(unittest.TestCase):
def setUp(self):
self.np_x = np.array(
[[1.0 + 0j, 2.0 + 3j], [4.0 + 0j, 5.0 - 6j]]
).astype("complex64")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.isreal(x)
# 2. Paddle keyword arguments
out2 = paddle.isreal(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.isreal(input=x)
# 4. Tensor method - args
out4 = x.isreal()
# Verify all outputs
ref_out = np.isreal(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_array_equal(ref_out, out.numpy())
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
# 1. Paddle Positional arguments
out1 = paddle.isreal(x)
# 2. Paddle keyword arguments
out2 = paddle.isreal(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.isreal(input=x)
# 4. Tensor method - args
out4 = x.isreal()
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x},
fetch_list=[out1, out2, out3, out4],
)
# Verify all outputs
ref_out = np.isreal(self.np_x)
for out in fetches:
np.testing.assert_array_equal(ref_out, out)
class TestLayerAndTensorToAPI(unittest.TestCase):
"""Test paddle.nn.Layer.to and paddle.Tensor.to alignment with PyTorch."""
def setUp(self):
paddle.disable_static()
def tearDown(self):
paddle.enable_static()
def _make_model(self):
"""Create a model with float params and an int buffer."""
class Model(paddle.nn.Layer):
def __init__(self):
super().__init__()
self.linear = paddle.nn.Linear(3, 2)
self.register_buffer(
'int_buf', paddle.to_tensor([1, 2, 3], dtype='int32')
)
def forward(self, x):
return self.linear(x)
return Model()
# ---- Layer.to: Positional dtype ----
def test_layer_positional_paddle_dtype(self):
"""Layer.to(paddle.float64)"""
linear = paddle.nn.Linear(2, 2)
ret = linear.to(paddle.float64)
self.assertEqual(linear.weight.dtype, paddle.float64)
self.assertEqual(linear.bias.dtype, paddle.float64)
self.assertIs(ret, linear)
def test_layer_positional_dtype_string(self):
"""Layer.to('float64')"""
linear = paddle.nn.Linear(2, 2)
linear.to('float64')
self.assertEqual(linear.weight.dtype, paddle.float64)
def test_layer_positional_dtype_float16(self):
"""Layer.to(paddle.float16)"""
linear = paddle.nn.Linear(2, 2)
linear.to(paddle.float16)
self.assertEqual(linear.weight.dtype, paddle.float16)
# ---- Layer.to: Positional tensor ----
def test_layer_positional_tensor(self):
"""Layer.to(tensor) -- match tensor's dtype and device"""
linear = paddle.nn.Linear(2, 2)
ref = paddle.to_tensor([1.0], dtype='float64')
linear.to(ref)
self.assertEqual(linear.weight.dtype, paddle.float64)
# ---- Layer.to: Positional device ----
def test_layer_positional_device_string(self):
"""Layer.to('cpu')"""
linear = paddle.nn.Linear(2, 2)
linear.to('cpu')
self.assertTrue(linear.weight.place.is_cpu_place())
def test_layer_positional_device_and_dtype(self):
"""Layer.to('cpu', 'float64')"""
linear = paddle.nn.Linear(2, 2)
linear.to('cpu', 'float64')
self.assertTrue(linear.weight.place.is_cpu_place())
self.assertEqual(linear.weight.dtype, paddle.float64)
# ---- Layer.to: Keyword args ----
def test_layer_keyword_device(self):
"""Layer.to(device='cpu')"""
linear = paddle.nn.Linear(2, 2)
linear.to(device='cpu')
self.assertTrue(linear.weight.place.is_cpu_place())
def test_layer_keyword_dtype(self):
"""Layer.to(dtype='float64')"""
linear = paddle.nn.Linear(2, 2)
linear.to(dtype='float64')
self.assertEqual(linear.weight.dtype, paddle.float64)
def test_layer_keyword_device_and_dtype(self):
"""Layer.to(device='cpu', dtype='float64')"""
linear = paddle.nn.Linear(2, 2)
linear.to(device='cpu', dtype='float64')
self.assertTrue(linear.weight.place.is_cpu_place())
self.assertEqual(linear.weight.dtype, paddle.float64)
def test_layer_keyword_non_blocking(self):
"""Layer.to(dtype='float64', non_blocking=False)"""
linear = paddle.nn.Linear(2, 2)
linear.to(dtype='float64', non_blocking=False)
self.assertEqual(linear.weight.dtype, paddle.float64)
def test_layer_keyword_blocking(self):
"""Layer.to(device='cpu', blocking=True)"""
linear = paddle.nn.Linear(2, 2)
linear.to(device='cpu', blocking=True)
self.assertTrue(linear.weight.place.is_cpu_place())
# ---- Layer.to: No args ----
def test_layer_no_args(self):
"""Layer.to() -- returns self unchanged"""
linear = paddle.nn.Linear(2, 2)
original_dtype = linear.weight.dtype
ret = linear.to()
self.assertIs(ret, linear)
self.assertEqual(linear.weight.dtype, original_dtype)
# ---- Layer.to: floating-only dtype casting (PyTorch-aligned) ----
def test_layer_cast_floating_only_with_positional_dtype(self):
"""Layer.to(dtype) casts only floating/complex params and buffers."""
model = self._make_model()
self.assertEqual(model.int_buf.dtype, paddle.int32)
model.to(paddle.float64)
self.assertEqual(model.linear.weight.dtype, paddle.float64)
self.assertEqual(model.int_buf.dtype, paddle.int32)
def test_layer_cast_floating_only_with_keyword_dtype(self):
"""Layer.to(dtype='float64') casts only floating/complex tensors."""
model = self._make_model()
model.to(dtype='float64')
self.assertEqual(model.linear.weight.dtype, paddle.float64)
self.assertEqual(model.int_buf.dtype, paddle.int32)
def test_layer_cast_floating_only_with_tensor(self):
"""Layer.to(tensor) casts only floating/complex tensors to tensor.dtype."""
model = self._make_model()
ref = paddle.to_tensor([1.0], dtype='float64')
model.to(ref)
self.assertEqual(model.linear.weight.dtype, paddle.float64)
self.assertEqual(model.int_buf.dtype, paddle.int32)
# ---- Layer.to: sublayers and chaining ----
def test_layer_sublayers_cast(self):
"""Layer.to() should recurse into sublayers."""
model = paddle.nn.Sequential(
paddle.nn.Linear(3, 4), paddle.nn.Linear(4, 2)
)
model.to(paddle.float64)
for sub in model.sublayers():
if hasattr(sub, 'weight'):
self.assertEqual(sub.weight.dtype, paddle.float64)
def test_layer_returns_self(self):
"""Layer.to() should return self for chaining."""
linear = paddle.nn.Linear(2, 2)
self.assertIs(linear.to(paddle.float64), linear)
def test_layer_sequential_to_calls(self):
"""Multiple Layer.to() calls should work correctly."""
linear = paddle.nn.Linear(2, 2)
linear.to(paddle.float64)
self.assertEqual(linear.weight.dtype, paddle.float64)
linear.to('float32')
self.assertEqual(linear.weight.dtype, paddle.float32)
# ---- Tensor.to ----
def test_tensor_positional_dtype(self):
"""Tensor.to(paddle.float64)"""
t = paddle.to_tensor([1.0, 2.0])
out = t.to(paddle.float64)
self.assertEqual(out.dtype, paddle.float64)
def test_tensor_positional_dtype_string(self):
"""Tensor.to('float64')"""
t = paddle.to_tensor([1.0, 2.0])
out = t.to('float64')
self.assertEqual(out.dtype, paddle.float64)
def test_tensor_positional_device(self):
"""Tensor.to('cpu')"""
t = paddle.to_tensor([1.0, 2.0])
out = t.to('cpu')
self.assertTrue(out.place.is_cpu_place())
def test_tensor_positional_device_and_dtype(self):
"""Tensor.to('cpu', 'float64')"""
t = paddle.to_tensor([1.0, 2.0])
out = t.to('cpu', 'float64')
self.assertTrue(out.place.is_cpu_place())
self.assertEqual(out.dtype, paddle.float64)
def test_tensor_positional_other(self):
"""Tensor.to(other_tensor)"""
t = paddle.to_tensor([1.0, 2.0])
ref = paddle.to_tensor([1], dtype='int32')
out = t.to(ref)
self.assertEqual(out.dtype, paddle.int32)
def test_tensor_keyword_dtype(self):
"""Tensor.to(dtype='float64')"""
t = paddle.to_tensor([1.0, 2.0])
out = t.to(dtype='float64')
self.assertEqual(out.dtype, paddle.float64)
def test_tensor_keyword_device(self):
"""Tensor.to(device='cpu')"""
t = paddle.to_tensor([1.0, 2.0])
out = t.to(device='cpu')
self.assertTrue(out.place.is_cpu_place())
def test_tensor_keyword_non_blocking(self):
"""Tensor.to(dtype='float64', non_blocking=False)"""
t = paddle.to_tensor([1.0, 2.0])
out = t.to(dtype='float64', non_blocking=False)
self.assertEqual(out.dtype, paddle.float64)
def test_tensor_no_args(self):
"""Tensor.to() -- returns self"""
t = paddle.to_tensor([1.0, 2.0])
out = t.to()
self.assertEqual(out.dtype, t.dtype)
# ---- blocking / non_blocking conflict ----
def test_blocking_non_blocking_conflict_raises(self):
"""Setting both blocking and non_blocking raises TypeError."""
linear = paddle.nn.Linear(2, 2)
with self.assertRaises(TypeError):
linear.to(dtype='float64', blocking=True, non_blocking=False)
def test_tensor_blocking_non_blocking_conflict_raises(self):
"""Tensor: setting both blocking and non_blocking raises TypeError."""
t = paddle.to_tensor([1.0])
with self.assertRaises(TypeError):
t.to(dtype='float64', blocking=True, non_blocking=False)
# ---- Error handling ----
def test_too_many_args(self):
"""to() with too many arguments raises TypeError."""
linear = paddle.nn.Linear(2, 2)
with self.assertRaises(TypeError):
linear.to('cpu', 'float64', True, False, 'extra')
def test_unexpected_keyword(self):
"""to() with unexpected keyword raises TypeError."""
linear = paddle.nn.Linear(2, 2)
with self.assertRaises(TypeError):
linear.to(foo='bar')
def test_invalid_first_arg(self):
"""to() with invalid first arg raises ValueError."""
linear = paddle.nn.Linear(2, 2)
with self.assertRaises(ValueError):
linear.to(123)
# ---- PyTorch keyword alias: other / tensor ----
def test_layer_keyword_other_alias(self):
"""Layer.to(other=tensor) -- PyTorch alias for tensor overload."""
linear = paddle.nn.Linear(2, 2)
ref = paddle.to_tensor([1.0], dtype='float64')
linear.to(other=ref)
self.assertEqual(linear.weight.dtype, paddle.float64)
def test_layer_keyword_tensor_alias(self):
"""Layer.to(tensor=tensor) -- PyTorch alias for tensor overload."""
linear = paddle.nn.Linear(2, 2)
ref = paddle.to_tensor([1.0], dtype='float64')
linear.to(tensor=ref)
self.assertEqual(linear.weight.dtype, paddle.float64)
def test_tensor_keyword_other_alias(self):
"""Tensor.to(other=tensor) -- PyTorch alias for tensor overload."""
t = paddle.to_tensor([1.0, 2.0])
ref = paddle.to_tensor([1], dtype='int32')
out = t.to(other=ref)
self.assertEqual(out.dtype, paddle.int32)
def test_tensor_keyword_tensor_alias(self):
"""Tensor.to(tensor=tensor) -- PyTorch alias for tensor overload."""
t = paddle.to_tensor([1.0, 2.0])
ref = paddle.to_tensor([1], dtype='int32')
out = t.to(tensor=ref)
self.assertEqual(out.dtype, paddle.int32)
# ---- copy parameter: Layer.to (tensor overload) ----
def test_layer_tensor_overload_copy_positional(self):
"""Layer.to(tensor, blocking, copy) -- copy as 3rd positional."""
linear = paddle.nn.Linear(2, 2)
ref = paddle.to_tensor([1.0], dtype='float64')
linear.to(ref, True, True)
self.assertEqual(linear.weight.dtype, paddle.float64)
def test_layer_tensor_overload_copy_keyword(self):
"""Layer.to(tensor, copy=True) -- copy as keyword."""
linear = paddle.nn.Linear(2, 2)
ref = paddle.to_tensor([1.0], dtype='float64')
linear.to(ref, copy=True)
self.assertEqual(linear.weight.dtype, paddle.float64)
def test_layer_tensor_overload_copy_mixed(self):
"""Layer.to(tensor, blocking=True, copy=True) -- mixed."""
linear = paddle.nn.Linear(2, 2)
ref = paddle.to_tensor([1.0], dtype='float64')
linear.to(ref, blocking=True, copy=True)
self.assertEqual(linear.weight.dtype, paddle.float64)
def test_layer_tensor_overload_copy_false_keyword(self):
"""Layer.to(tensor, copy=False) -- explicit copy=False."""
linear = paddle.nn.Linear(2, 2)
ref = paddle.to_tensor([1.0], dtype='float64')
linear.to(ref, copy=False)
self.assertEqual(linear.weight.dtype, paddle.float64)
# ---- copy parameter: Layer.to (dtype overload) ----
def test_layer_dtype_overload_copy_positional(self):
"""Layer.to(dtype, blocking, copy) -- copy as 3rd positional."""
linear = paddle.nn.Linear(2, 2)
linear.to('float64', True, True)
self.assertEqual(linear.weight.dtype, paddle.float64)
def test_layer_dtype_overload_copy_keyword(self):
"""Layer.to(dtype, copy=True) -- copy as keyword."""
linear = paddle.nn.Linear(2, 2)
linear.to('float64', copy=True)
self.assertEqual(linear.weight.dtype, paddle.float64)
def test_layer_dtype_overload_copy_mixed(self):
"""Layer.to(dtype, blocking=True, copy=True) -- mixed."""
linear = paddle.nn.Linear(2, 2)
linear.to(paddle.float64, blocking=True, copy=True)
self.assertEqual(linear.weight.dtype, paddle.float64)
# ---- copy parameter: Layer.to (device overload) ----
def test_layer_device_overload_copy_positional(self):
"""Layer.to(device, dtype, blocking, copy) -- copy as 4th positional."""
linear = paddle.nn.Linear(2, 2)
linear.to('cpu', 'float64', True, True)
self.assertTrue(linear.weight.place.is_cpu_place())
self.assertEqual(linear.weight.dtype, paddle.float64)
def test_layer_device_overload_copy_keyword(self):
"""Layer.to(device, copy=True) -- copy as keyword only."""
linear = paddle.nn.Linear(2, 2)
linear.to('cpu', copy=True)
self.assertTrue(linear.weight.place.is_cpu_place())
def test_layer_device_overload_all_kwargs(self):
"""Layer.to(device=, dtype=, blocking=, copy=) -- all keywords."""
linear = paddle.nn.Linear(2, 2)
linear.to(device='cpu', dtype='float64', blocking=True, copy=True)
self.assertTrue(linear.weight.place.is_cpu_place())
self.assertEqual(linear.weight.dtype, paddle.float64)
def test_layer_device_overload_mixed_copy_keyword(self):
"""Layer.to(device, dtype, copy=True) -- 2 positional + copy kwarg."""
linear = paddle.nn.Linear(2, 2)
linear.to('cpu', 'float64', copy=True)
self.assertTrue(linear.weight.place.is_cpu_place())
self.assertEqual(linear.weight.dtype, paddle.float64)
# ---- copy parameter: Tensor.to (tensor overload) ----
def test_tensor_tensor_overload_copy_positional(self):
"""Tensor.to(other, blocking, copy) -- copy as 3rd positional."""
t = paddle.to_tensor([1.0, 2.0])
ref = paddle.to_tensor([1], dtype='int32')
out = t.to(ref, True, True)
self.assertEqual(out.dtype, paddle.int32)
def test_tensor_tensor_overload_copy_keyword(self):
"""Tensor.to(other, copy=True) -- copy as keyword."""
t = paddle.to_tensor([1.0, 2.0])
ref = paddle.to_tensor([1], dtype='int32')
out = t.to(ref, copy=True)
self.assertEqual(out.dtype, paddle.int32)
def test_tensor_tensor_overload_copy_mixed(self):
"""Tensor.to(other, blocking=True, copy=True) -- mixed."""
t = paddle.to_tensor([1.0, 2.0])
ref = paddle.to_tensor([1], dtype='int32')
out = t.to(ref, blocking=True, copy=True)
self.assertEqual(out.dtype, paddle.int32)
def test_tensor_tensor_overload_full_kwargs(self):
"""Tensor.to(other=, blocking=, copy=) -- all keywords."""
t = paddle.to_tensor([1.0, 2.0])
ref = paddle.to_tensor([1], dtype='int32')
out = t.to(other=ref, blocking=True, copy=True)
self.assertEqual(out.dtype, paddle.int32)
# ---- copy parameter: Tensor.to (dtype overload) ----
def test_tensor_dtype_overload_copy_positional(self):
"""Tensor.to(dtype, blocking, copy) -- copy as 3rd positional."""
t = paddle.to_tensor([1.0, 2.0])
out = t.to('float64', True, True)
self.assertEqual(out.dtype, paddle.float64)
def test_tensor_dtype_overload_copy_keyword(self):
"""Tensor.to(dtype, copy=True) -- copy as keyword."""
t = paddle.to_tensor([1.0, 2.0])
out = t.to('float64', copy=True)
self.assertEqual(out.dtype, paddle.float64)
def test_tensor_dtype_overload_copy_mixed(self):
"""Tensor.to(dtype, blocking=True, copy=True) -- mixed."""
t = paddle.to_tensor([1.0, 2.0])
out = t.to(paddle.float64, blocking=True, copy=True)
self.assertEqual(out.dtype, paddle.float64)
# ---- copy parameter: Tensor.to (device overload) ----
def test_tensor_device_overload_copy_positional(self):
"""Tensor.to(device, dtype, blocking, copy) -- copy as 4th positional."""
t = paddle.to_tensor([1.0, 2.0])
out = t.to('cpu', 'float64', True, True)
self.assertTrue(out.place.is_cpu_place())
self.assertEqual(out.dtype, paddle.float64)
def test_tensor_device_overload_copy_keyword(self):
"""Tensor.to(device, copy=True) -- copy as keyword only."""
t = paddle.to_tensor([1.0, 2.0])
out = t.to('cpu', copy=True)
self.assertTrue(out.place.is_cpu_place())
def test_tensor_device_overload_all_kwargs(self):
"""Tensor.to(device=, dtype=, blocking=, copy=) -- all keywords."""
t = paddle.to_tensor([1.0, 2.0])
out = t.to(device='cpu', dtype='float64', blocking=True, copy=True)
self.assertTrue(out.place.is_cpu_place())
self.assertEqual(out.dtype, paddle.float64)
def test_tensor_device_overload_mixed_copy_keyword(self):
"""Tensor.to(device, dtype, copy=True) -- 2 positional + copy kwarg."""
t = paddle.to_tensor([1.0, 2.0])
out = t.to('cpu', 'float64', copy=True)
self.assertTrue(out.place.is_cpu_place())
self.assertEqual(out.dtype, paddle.float64)
# ---- copy parameter defaults and validation ----
def test_copy_default_is_false_layer(self):
"""Layer.to without copy should default copy=False (no error)."""
linear = paddle.nn.Linear(2, 2)
linear.to('float64')
self.assertEqual(linear.weight.dtype, paddle.float64)
def test_copy_default_is_false_tensor(self):
"""Tensor.to without copy should default copy=False (no error)."""
t = paddle.to_tensor([1.0, 2.0])
out = t.to('float64')
self.assertEqual(out.dtype, paddle.float64)
def test_copy_invalid_type_layer(self):
"""Layer.to(dtype, copy='yes') raises TypeError for non-bool."""
linear = paddle.nn.Linear(2, 2)
with self.assertRaises(TypeError):
linear.to('float64', copy='yes')
def test_copy_invalid_type_tensor(self):
"""Tensor.to(dtype, copy='yes') raises TypeError for non-bool."""
t = paddle.to_tensor([1.0, 2.0])
with self.assertRaises(TypeError):
t.to('float64', copy='yes')
# Test paddle.dtype.itemsize compatibility (mirrors torch.dtype.itemsize).
class TestDtypeItemsizeAPI(unittest.TestCase):
EXPECTED = {
'float16': 2,
'bfloat16': 2,
'float32': 4,
'float64': 8,
'complex64': 8,
'complex128': 16,
'int8': 1,
'int16': 2,
'int32': 4,
'int64': 8,
'uint8': 1,
'uint16': 2,
'uint32': 4,
'uint64': 8,
'bool': 1,
'float8_e4m3fn': 1,
'float8_e5m2': 1,
}
def test_all_standard_dtypes(self):
for name, want in self.EXPECTED.items():
if not hasattr(paddle, name):
continue
with self.subTest(dtype=name):
self.assertEqual(getattr(paddle, name).itemsize, want)
def test_returns_int(self):
self.assertIsInstance(paddle.float32.itemsize, int)
def test_dtype_str(self):
for name in ('float8_e5m2', 'uint16', 'uint32', 'uint64', 'bfloat16'):
with self.subTest(dtype=name):
self.assertEqual(str(getattr(paddle, name)), f'paddle.{name}')
def test_property_lives_on_class(self):
self.assertIsInstance(type(paddle.float32).itemsize, property)
def test_aliases_match(self):
self.assertEqual(paddle.float.itemsize, paddle.float32.itemsize)
self.assertEqual(paddle.double.itemsize, paddle.float64.itemsize)
self.assertEqual(paddle.half.itemsize, paddle.float16.itemsize)
self.assertEqual(paddle.short.itemsize, paddle.int16.itemsize)
self.assertEqual(paddle.long.itemsize, paddle.int64.itemsize)
self.assertEqual(paddle.cfloat.itemsize, paddle.complex64.itemsize)
self.assertEqual(paddle.cdouble.itemsize, paddle.complex128.itemsize)
def test_matches_tensor_element_size(self):
for name in ('float32', 'float64', 'int32', 'int64', 'bool'):
with self.subTest(dtype=name):
t = paddle.zeros([1], dtype=name)
self.assertEqual(
getattr(paddle, name).itemsize, t.element_size()
)
class TestFloat8E5M2DtypeAPI(unittest.TestCase):
def check_finfo(self, info):
self.assertEqual(info.bits, 8)
self.assertEqual(str(info.dtype), 'float8_e5m2')
self.assertEqual(info.eps, 0.25)
self.assertEqual(info.min, -57344.0)
self.assertEqual(info.max, 57344.0)
self.assertEqual(info.smallest_normal, 6.103515625e-05)
self.assertEqual(info.tiny, 6.103515625e-05)
def test_dygraph_Compatibility(self):
paddle.disable_static()
# 1. Paddle Positional arguments
out1 = paddle.finfo(paddle.float8_e5m2)
# 2. Paddle keyword arguments
out2 = paddle.finfo(dtype=paddle.float8_e5m2)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.finfo(type=paddle.float8_e5m2)
# Verify all outputs
for out in [out1, out2, out3]:
self.check_finfo(out)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
# 1. Paddle Positional arguments
out1 = paddle.finfo(paddle.float8_e5m2)
# 2. Paddle keyword arguments
out2 = paddle.finfo(dtype=paddle.float8_e5m2)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.finfo(type=paddle.float8_e5m2)
# Verify all outputs
for out in [out1, out2, out3]:
self.check_finfo(out)
class TestUnsignedDtypeAPI(unittest.TestCase):
EXPECTED = {
'uint16': (0, 65535, 16),
'uint32': (0, 4294967295, 32),
'uint64': (0, 18446744073709551615, 64),
}
def check_iinfo(self, info, name):
min_value, max_value, bits = self.EXPECTED[name]
self.assertEqual(info.min, min_value)
self.assertEqual(info.max, max_value)
self.assertEqual(info.bits, bits)
self.assertEqual(str(info.dtype), name)
self.assertIn(f'max={max_value}', repr(info))
def test_dygraph_Compatibility(self):
paddle.disable_static()
for name in self.EXPECTED:
dtype = getattr(paddle, name)
with self.subTest(dtype=name):
# 1. Paddle Positional arguments
out1 = paddle.iinfo(dtype)
# 2. Paddle keyword arguments
out2 = paddle.iinfo(dtype=dtype)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.iinfo(type=dtype)
# Verify all outputs
for out in [out1, out2, out3]:
self.check_iinfo(out, name)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
for name in self.EXPECTED:
dtype = getattr(paddle, name)
with self.subTest(dtype=name):
# 1. Paddle Positional arguments
out1 = paddle.iinfo(dtype)
# 2. Paddle keyword arguments
out2 = paddle.iinfo(dtype=dtype)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.iinfo(type=dtype)
# Verify all outputs
for out in [out1, out2, out3]:
self.check_iinfo(out, name)
# Test select_scatter compatibility
class TestSelectScatterAPICompatibility(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_x = np.random.rand(2, 3, 4).astype("float32")
self.np_values = np.random.rand(2, 4).astype("float32")
self.axis = 1
self.index = 1
def get_ref_out(self):
ref_out = self.np_x.copy()
ref_out[:, self.index, :] = self.np_values
return ref_out
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
values = paddle.to_tensor(self.np_values)
# 1. Paddle Positional arguments
out1 = paddle.select_scatter(x, values, self.axis, self.index)
# 2. Paddle keyword arguments
out2 = paddle.select_scatter(
x=x, values=values, axis=self.axis, index=self.index
)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.select_scatter(
input=x, src=values, dim=self.axis, index=self.index
)
# 4. Mixed arguments
out4 = paddle.select_scatter(
x, src=values, dim=self.axis, index=self.index
)
# 5. Tensor method - args
out5 = x.select_scatter(values, self.axis, self.index)
# 6. Tensor method - kwargs (PyTorch alias)
out6 = x.select_scatter(src=values, dim=self.axis, index=self.index)
# Verify all outputs
ref_out = self.get_ref_out()
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-5)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
values = paddle.static.data(
name="values",
shape=self.np_values.shape,
dtype=str(self.np_values.dtype),
)
# 1. Paddle Positional arguments
out1 = paddle.select_scatter(x, values, self.axis, self.index)
# 2. Paddle keyword arguments
out2 = paddle.select_scatter(
x=x, values=values, axis=self.axis, index=self.index
)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.select_scatter(
input=x, src=values, dim=self.axis, index=self.index
)
# 4. Tensor method - args
out4 = x.select_scatter(values, self.axis, self.index)
# 5. Tensor method - kwargs (PyTorch alias)
out5 = x.select_scatter(src=values, dim=self.axis, index=self.index)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x, "values": self.np_values},
fetch_list=[out1, out2, out3, out4, out5],
)
# Verify all outputs
ref_out = self.get_ref_out()
for out in fetches:
np.testing.assert_allclose(out, ref_out, rtol=1e-5)
# Test tile compatibility
class TestTileAPICompatibility(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_x = np.random.rand(2, 3).astype("float32")
self.repeat_times = [2, 3]
self.shape = self.np_x.shape
self.dtype = str(self.np_x.dtype)
self.np_x_3d = np.random.rand(1, 2, 2).astype("float64")
self.repeat_times_3d = [2, 1, 3]
self.shape_3d = self.np_x_3d.shape
self.dtype_3d = str(self.np_x_3d.dtype)
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.tile(x, self.repeat_times)
# 2. Paddle keyword arguments
out2 = paddle.tile(x=x, repeat_times=self.repeat_times)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.tile(input=x, dims=self.repeat_times)
# 4. Mixed arguments
out4 = paddle.tile(x, dims=self.repeat_times)
# 5. Tensor method - args
out5 = x.tile(2, 3)
# 6. Tensor method - kwargs (PyTorch alias)
out6 = x.tile(dims=self.repeat_times)
# Verify all outputs
ref_out = np.tile(self.np_x, self.repeat_times)
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-5)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(name="x", shape=self.shape, dtype=self.dtype)
# 1. Paddle Positional arguments
out1 = paddle.tile(x, self.repeat_times)
# 2. Paddle keyword arguments
out2 = paddle.tile(x=x, repeat_times=self.repeat_times)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.tile(input=x, dims=self.repeat_times)
# 4. Tensor method - args
out4 = x.tile(2, 3)
# 5. Tensor method - kwargs (PyTorch alias)
out5 = x.tile(dims=self.repeat_times)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x},
fetch_list=[out1, out2, out3, out4, out5],
)
# Verify all outputs
ref_out = np.tile(self.np_x, self.repeat_times)
for out in fetches:
np.testing.assert_allclose(out, ref_out, rtol=1e-5)
def test_dygraph_HighDimCompatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x_3d)
# 1. Paddle Positional arguments
out1 = paddle.tile(x, self.repeat_times_3d)
# 2. Paddle keyword arguments
out2 = paddle.tile(x=x, repeat_times=self.repeat_times_3d)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.tile(input=x, dims=self.repeat_times_3d)
# 4. Mixed arguments
out4 = paddle.tile(x, dims=self.repeat_times_3d)
# 5. Tensor method - args
out5 = x.tile(2, 1, 3)
# 6. Tensor method - kwargs (PyTorch alias)
out6 = x.tile(dims=self.repeat_times_3d)
dims = self.repeat_times_3d
# 7. Tensor method - args with variable expansion
out7 = x.tile(*dims)
# Verify all outputs
ref_out = np.tile(self.np_x_3d, self.repeat_times_3d)
for out in [out1, out2, out3, out4, out5, out6, out7]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-5)
paddle.enable_static()
def test_static_HighDimCompatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.shape_3d, dtype=self.dtype_3d
)
# 1. Paddle Positional arguments
out1 = paddle.tile(x, self.repeat_times_3d)
# 2. Paddle keyword arguments
out2 = paddle.tile(x=x, repeat_times=self.repeat_times_3d)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.tile(input=x, dims=self.repeat_times_3d)
# 4. Tensor method - args
out4 = x.tile(2, 1, 3)
# 5. Tensor method - kwargs (PyTorch alias)
out5 = x.tile(dims=self.repeat_times_3d)
dims = self.repeat_times_3d
# 6. Tensor method - args with variable expansion
out6 = x.tile(*dims)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x_3d},
fetch_list=[out1, out2, out3, out4, out5, out6],
)
# Verify all outputs
ref_out = np.tile(self.np_x_3d, self.repeat_times_3d)
for out in fetches:
np.testing.assert_allclose(out, ref_out, rtol=1e-5)
# Test logit compatibility
class TestLogitAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.shape = [5, 6]
self.dtype = 'float32'
self.np_x = np.random.uniform(0.1, 0.9, self.shape).astype(self.dtype)
def _ref_logit(self, x, eps=0.0):
if eps > 0.0:
x = np.clip(x, eps, 1.0 - eps)
return np.log(x / (1.0 - x))
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.logit(x)
# 2. Paddle keyword arguments
out2 = paddle.logit(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.logit(input=x)
# 4. Mixed arguments (positional x + keyword eps)
out4 = paddle.logit(x, eps=1e-6)
# 5. out parameter test
out5 = paddle.empty_like(x)
paddle.logit(x, out=out5)
# 6. out parameter with alias keyword
out6 = paddle.empty_like(x)
paddle.logit(input=x, out=out6)
# 7. Tensor method - args
out7 = x.logit()
# 8. Tensor method - kwargs
out8 = x.logit(eps=1e-6)
# 9. paddle.special.logit alias
out9 = paddle.special.logit(x)
# 10. paddle.special.logit with alias keyword
out10 = paddle.special.logit(input=x)
# Verify outputs without eps
ref_out = self._ref_logit(self.np_x)
for out in [out1, out2, out3, out5, out6, out7, out9, out10]:
np.testing.assert_allclose(
out.numpy(), ref_out, rtol=1e-5, atol=1e-6
)
# Verify outputs with eps
ref_out_eps = self._ref_logit(self.np_x, 1e-6)
for out in [out4, out8]:
np.testing.assert_allclose(
out.numpy(), ref_out_eps, rtol=1e-5, atol=1e-6
)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(name="x", shape=self.shape, dtype=self.dtype)
# 1. Paddle Positional arguments
out1 = paddle.logit(x)
# 2. Paddle keyword arguments
out2 = paddle.logit(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.logit(input=x)
# 4. Mixed arguments (positional x + keyword eps)
out4 = paddle.logit(x, eps=1e-6)
# 5. Tensor method - args
out5 = x.logit()
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x},
fetch_list=[out1, out2, out3, out4, out5],
)
# Verify outputs without eps
ref_out = self._ref_logit(self.np_x)
for out in [fetches[0], fetches[1], fetches[2], fetches[4]]:
np.testing.assert_allclose(out, ref_out, rtol=1e-5, atol=1e-6)
# Verify output with eps
ref_out_eps = self._ref_logit(self.np_x, 1e-6)
np.testing.assert_allclose(
fetches[3], ref_out_eps, rtol=1e-5, atol=1e-6
)
class TestSpecialErfAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.shape = [3, 4]
self.inputs = [
np.random.uniform(-3.0, 3.0, self.shape).astype(dtype)
for dtype in ["float32", "float64"]
]
def _ref_erf(self, x):
return np.array(
[math.erf(float(v)) for v in x.flatten()], dtype=x.dtype
).reshape(x.shape)
def test_dygraph_Compatibility(self):
paddle.disable_static()
for np_x in self.inputs:
x = paddle.to_tensor(np_x)
# 1. Paddle Positional arguments
out1 = paddle.special.erf(x)
# 2. Paddle keyword arguments
out2 = paddle.special.erf(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.special.erf(input=x)
# 4. out parameter test
out4 = paddle.empty_like(x)
out5 = paddle.special.erf(x, out=out4)
# Verify all outputs
expected = self._ref_erf(np_x)
for out in [out1, out2, out3, out4, out5]:
np.testing.assert_allclose(
out.numpy(), expected, rtol=1e-5, atol=1e-6
)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
for i, np_x in enumerate(self.inputs):
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name=f"x_{i}", shape=self.shape, dtype=str(np_x.dtype)
)
# 1. Paddle Positional arguments
out1 = paddle.special.erf(x)
# 2. Paddle keyword arguments
out2 = paddle.special.erf(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.special.erf(input=x)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={f"x_{i}": np_x},
fetch_list=[out1, out2, out3],
)
# Verify all outputs
expected = self._ref_erf(np_x)
for out in fetches:
np.testing.assert_allclose(
out, expected, rtol=1e-5, atol=1e-6
)
class TestSpecialSincAPI(unittest.TestCase):
def setUp(self):
self.shape = [3, 4]
base = np.array(
[
[0.0, -3.0, -1.5, -0.25],
[0.25, 0.5, 1.0, 1.5],
[2.0, 2.5, 3.0, 4.25],
]
)
self.inputs = [base.astype(dtype) for dtype in ["float32", "float64"]]
def test_dygraph_Compatibility(self):
paddle.disable_static()
for np_x in self.inputs:
x = paddle.to_tensor(np_x)
# 1. Paddle Positional arguments
out1 = paddle.special.sinc(x)
# 2. Paddle keyword arguments
out2 = paddle.special.sinc(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.special.sinc(input=x)
# 4. out parameter test
out4 = paddle.empty_like(x)
out5 = paddle.special.sinc(x, out=out4)
out6 = paddle.empty_like(x)
out7 = paddle.sinc(input=x, out=out6)
# Verify all outputs
expected = np.sinc(np_x)
for out in [out1, out2, out3, out4, out5, out6, out7]:
np.testing.assert_allclose(
out.numpy(), expected, rtol=1e-5, atol=1e-6
)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
for i, np_x in enumerate(self.inputs):
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name=f"sinc_x_{i}",
shape=self.shape,
dtype=str(np_x.dtype),
)
# 1. Paddle Positional arguments
out1 = paddle.special.sinc(x)
# 2. Paddle keyword arguments
out2 = paddle.special.sinc(x=x)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.special.sinc(input=x)
out4 = paddle.sinc(input=x)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={f"sinc_x_{i}": np_x},
fetch_list=[out1, out2, out3, out4],
)
# Verify all outputs
expected = np.sinc(np_x)
for out in fetches:
np.testing.assert_allclose(
out, expected, rtol=1e-5, atol=1e-6
)
# Test conv1d_transpose / conv_transpose1d compatibility
@unittest.skipIf(
sys.platform == 'win32',
"Conv transpose compatibility tests not supported on Windows-Inference",
)
class TestConv1dTransposeAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.dtype = 'float32'
self.np_x = np.random.rand(1, 2, 4).astype(self.dtype)
self.np_weight = np.random.rand(2, 2, 3).astype(self.dtype)
self.np_bias = np.random.rand(2).astype(self.dtype)
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
weight = paddle.to_tensor(self.np_weight)
bias = paddle.to_tensor(self.np_bias)
# 1. Paddle Positional arguments
out1 = paddle.nn.functional.conv1d_transpose(x, weight)
# 2. Paddle keyword arguments
out2 = paddle.nn.functional.conv1d_transpose(x=x, weight=weight)
# 3. PyTorch keyword arguments (alias: input)
out3 = paddle.nn.functional.conv1d_transpose(input=x, weight=weight)
# 4. PyTorch function name alias
out4 = paddle.nn.functional.conv_transpose1d(x, weight)
# 5. PyTorch function name alias + PyTorch keyword
out5 = paddle.nn.functional.conv_transpose1d(input=x, weight=weight)
# 6. Mixed arguments (positional + keyword)
out6 = paddle.nn.functional.conv1d_transpose(
x, weight, bias=bias, stride=1, padding=0
)
# 7. Positional arguments with bias
out7 = paddle.nn.functional.conv1d_transpose(x, weight, bias)
# 8. All positional arguments
out8 = paddle.nn.functional.conv1d_transpose(
x, weight, bias, 1, 0, 0, 1, 1, None, 'NCL', None
)
# 9. All keyword arguments
out9 = paddle.nn.functional.conv1d_transpose(
x=x,
weight=weight,
bias=bias,
stride=1,
padding=0,
output_padding=0,
groups=1,
dilation=1,
output_size=None,
data_format='NCL',
name=None,
)
# 10. PyTorch alias + all keyword arguments
out10 = paddle.nn.functional.conv_transpose1d(
input=x,
weight=weight,
bias=bias,
stride=1,
padding=0,
output_padding=0,
groups=1,
dilation=1,
output_size=None,
data_format='NCL',
name=None,
)
# Verify outputs without bias
ref = out1.numpy()
for out in [out2, out3, out4, out5]:
np.testing.assert_allclose(out.numpy(), ref, rtol=1e-5)
# Verify outputs with bias
ref_bias = out6.numpy()
for out in [out7, out8, out9, out10]:
np.testing.assert_allclose(out.numpy(), ref_bias, rtol=1e-5)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(name="x", shape=[1, 2, 4], dtype=self.dtype)
weight = paddle.static.data(
name="weight", shape=[2, 2, 3], dtype=self.dtype
)
# 1. Paddle Positional arguments
out1 = paddle.nn.functional.conv1d_transpose(x, weight)
# 2. Paddle keyword arguments
out2 = paddle.nn.functional.conv1d_transpose(x=x, weight=weight)
# 3. PyTorch keyword arguments (alias: input)
out3 = paddle.nn.functional.conv1d_transpose(input=x, weight=weight)
# 4. PyTorch function name alias
out4 = paddle.nn.functional.conv_transpose1d(x, weight)
# 5. PyTorch function name alias + PyTorch keyword
out5 = paddle.nn.functional.conv_transpose1d(input=x, weight=weight)
# 6. All positional arguments
out6 = paddle.nn.functional.conv1d_transpose(
x, weight, None, 1, 0, 0, 1, 1, None, 'NCL', None
)
# 7. All keyword arguments
out7 = paddle.nn.functional.conv1d_transpose(
x=x,
weight=weight,
bias=None,
stride=1,
padding=0,
output_padding=0,
groups=1,
dilation=1,
output_size=None,
data_format='NCL',
name=None,
)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={
"x": self.np_x,
"weight": self.np_weight,
},
fetch_list=[out1, out2, out3, out4, out5, out6, out7],
)
# Verify all outputs
for i in range(1, len(fetches)):
np.testing.assert_allclose(fetches[0], fetches[i], rtol=1e-5)
# Test Conv1DTranspose layer compatibility
@unittest.skipIf(
sys.platform == 'win32',
"Conv transpose compatibility tests not supported on Windows-Inference",
)
class TestConv1DTransposeLayerAPI(unittest.TestCase):
def test_paddle_style_keyword_only(self):
paddle.disable_static()
layer = paddle.nn.Conv1DTranspose(2, 2, 3)
self.assertIsNotNone(layer.weight)
self.assertIsNotNone(layer.bias)
def test_bias_false_disables_bias_attr(self):
paddle.disable_static()
layer = paddle.nn.Conv1DTranspose(2, 2, 3, bias=False)
self.assertIsNone(layer.bias)
def test_pytorch_style_positional_bias_only(self):
paddle.disable_static()
layer = paddle.nn.Conv1DTranspose(2, 2, 3, 1, 0, 0, 1, True)
self.assertIsNotNone(layer.bias)
def test_pytorch_style_full_positional(self):
paddle.disable_static()
layer = paddle.nn.ConvTranspose1d(
2, 2, 3, 1, 0, 0, 1, False, 1, 'zeros', None, None
)
self.assertIsNone(layer.bias)
def test_pytorch_style_duplicate_bias_raises(self):
paddle.disable_static()
with self.assertRaises(TypeError):
paddle.nn.Conv1DTranspose(2, 2, 3, 1, 0, 0, 1, True, bias=True)
# Test conv2d_transpose / conv_transpose2d compatibility
@unittest.skipIf(
sys.platform == 'win32',
"Conv transpose compatibility tests not supported on Windows-Inference",
)
class TestConv2dTransposeAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.dtype = 'float32'
self.np_x = np.random.rand(1, 2, 4, 4).astype(self.dtype)
self.np_weight = np.random.rand(2, 2, 3, 3).astype(self.dtype)
self.np_bias = np.random.rand(2).astype(self.dtype)
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
weight = paddle.to_tensor(self.np_weight)
bias = paddle.to_tensor(self.np_bias)
# 1. Paddle Positional arguments
out1 = paddle.nn.functional.conv2d_transpose(x, weight)
# 2. Paddle keyword arguments
out2 = paddle.nn.functional.conv2d_transpose(x=x, weight=weight)
# 3. PyTorch keyword arguments (alias: input)
out3 = paddle.nn.functional.conv2d_transpose(input=x, weight=weight)
# 4. PyTorch function name alias
out4 = paddle.nn.functional.conv_transpose2d(x, weight)
# 5. PyTorch function name alias + PyTorch keyword
out5 = paddle.nn.functional.conv_transpose2d(input=x, weight=weight)
# 6. Mixed arguments (positional + keyword)
out6 = paddle.nn.functional.conv2d_transpose(
x, weight, bias=bias, stride=1, padding=0
)
# 7. Positional arguments with bias
out7 = paddle.nn.functional.conv2d_transpose(x, weight, bias)
# 8. All positional arguments
out8 = paddle.nn.functional.conv2d_transpose(
x, weight, bias, 1, 0, 0, 1, 1, None, 'NCHW', None
)
# 9. All keyword arguments
out9 = paddle.nn.functional.conv2d_transpose(
x=x,
weight=weight,
bias=bias,
stride=1,
padding=0,
output_padding=0,
groups=1,
dilation=1,
output_size=None,
data_format='NCHW',
name=None,
)
# 10. PyTorch alias + all keyword arguments
out10 = paddle.nn.functional.conv_transpose2d(
input=x,
weight=weight,
bias=bias,
stride=1,
padding=0,
output_padding=0,
groups=1,
dilation=1,
output_size=None,
data_format='NCHW',
name=None,
)
# Verify outputs without bias
ref = out1.numpy()
for out in [out2, out3, out4, out5]:
np.testing.assert_allclose(out.numpy(), ref, rtol=1e-5)
# Verify outputs with bias
ref_bias = out6.numpy()
for out in [out7, out8, out9, out10]:
np.testing.assert_allclose(out.numpy(), ref_bias, rtol=1e-5)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=[1, 2, 4, 4], dtype=self.dtype
)
weight = paddle.static.data(
name="weight", shape=[2, 2, 3, 3], dtype=self.dtype
)
# 1. Paddle Positional arguments
out1 = paddle.nn.functional.conv2d_transpose(x, weight)
# 2. Paddle keyword arguments
out2 = paddle.nn.functional.conv2d_transpose(x=x, weight=weight)
# 3. PyTorch keyword arguments (alias: input)
out3 = paddle.nn.functional.conv2d_transpose(input=x, weight=weight)
# 4. PyTorch function name alias
out4 = paddle.nn.functional.conv_transpose2d(x, weight)
# 5. PyTorch function name alias + PyTorch keyword
out5 = paddle.nn.functional.conv_transpose2d(input=x, weight=weight)
# 6. All positional arguments
out6 = paddle.nn.functional.conv2d_transpose(
x, weight, None, 1, 0, 0, 1, 1, None, 'NCHW', None
)
# 7. All keyword arguments
out7 = paddle.nn.functional.conv2d_transpose(
x=x,
weight=weight,
bias=None,
stride=1,
padding=0,
output_padding=0,
groups=1,
dilation=1,
output_size=None,
data_format='NCHW',
name=None,
)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={
"x": self.np_x,
"weight": self.np_weight,
},
fetch_list=[out1, out2, out3, out4, out5, out6, out7],
)
# Verify all outputs
for i in range(1, len(fetches)):
np.testing.assert_allclose(fetches[0], fetches[i], rtol=1e-5)
@unittest.skipIf(
sys.platform == 'win32',
"Conv transpose compatibility tests not supported on Windows-Inference",
)
class TestConv2DTransposeLayerAPI(unittest.TestCase):
def test_paddle_style_keyword_only(self):
paddle.disable_static()
layer = paddle.nn.Conv2DTranspose(2, 2, 3)
self.assertIsNotNone(layer.weight)
self.assertIsNotNone(layer.bias)
def test_bias_false_disables_bias_attr(self):
paddle.disable_static()
layer = paddle.nn.Conv2DTranspose(2, 2, 3, bias=False)
self.assertIsNone(layer.bias)
def test_pytorch_style_positional_bias_only(self):
paddle.disable_static()
layer = paddle.nn.Conv2DTranspose(2, 2, 3, 1, 0, 0, 1, True)
self.assertIsNotNone(layer.bias)
def test_pytorch_style_full_positional(self):
paddle.disable_static()
layer = paddle.nn.ConvTranspose2d(
2, 2, 3, 1, 0, 0, 1, False, 1, 'zeros', None, None
)
self.assertIsNone(layer.bias)
def test_pytorch_style_duplicate_bias_raises(self):
paddle.disable_static()
with self.assertRaises(TypeError):
paddle.nn.Conv2DTranspose(2, 2, 3, 1, 0, 0, 1, True, bias=True)
# Test conv3d_transpose / conv_transpose3d compatibility
@unittest.skipIf(
sys.platform == 'win32',
"Conv transpose compatibility tests not supported on Windows-Inference",
)
class TestConv3dTransposeAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.dtype = 'float32'
self.np_x = np.random.rand(1, 2, 4, 4, 4).astype(self.dtype)
self.np_weight = np.random.rand(2, 2, 3, 3, 3).astype(self.dtype)
self.np_bias = np.random.rand(2).astype(self.dtype)
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
weight = paddle.to_tensor(self.np_weight)
bias = paddle.to_tensor(self.np_bias)
# 1. Paddle Positional arguments
out1 = paddle.nn.functional.conv3d_transpose(x, weight)
# 2. Paddle keyword arguments
out2 = paddle.nn.functional.conv3d_transpose(x=x, weight=weight)
# 3. PyTorch keyword arguments (alias: input)
out3 = paddle.nn.functional.conv3d_transpose(input=x, weight=weight)
# 4. PyTorch function name alias
out4 = paddle.nn.functional.conv_transpose3d(x, weight)
# 5. PyTorch function name alias + PyTorch keyword
out5 = paddle.nn.functional.conv_transpose3d(input=x, weight=weight)
# 6. Mixed arguments (positional + keyword)
out6 = paddle.nn.functional.conv3d_transpose(
x, weight, bias=bias, stride=1, padding=0
)
# 7. Positional arguments with bias
out7 = paddle.nn.functional.conv3d_transpose(x, weight, bias)
# 8. All positional arguments
out8 = paddle.nn.functional.conv3d_transpose(
x, weight, bias, 1, 0, 0, 1, 1, None, 'NCDHW', None
)
# 9. All keyword arguments
out9 = paddle.nn.functional.conv3d_transpose(
x=x,
weight=weight,
bias=bias,
stride=1,
padding=0,
output_padding=0,
groups=1,
dilation=1,
output_size=None,
data_format='NCDHW',
name=None,
)
# 10. PyTorch alias + all keyword arguments
out10 = paddle.nn.functional.conv_transpose3d(
input=x,
weight=weight,
bias=bias,
stride=1,
padding=0,
output_padding=0,
groups=1,
dilation=1,
output_size=None,
data_format='NCDHW',
name=None,
)
# Verify outputs without bias
ref = out1.numpy()
for out in [out2, out3, out4, out5]:
np.testing.assert_allclose(out.numpy(), ref, rtol=1e-5)
# Verify outputs with bias
ref_bias = out6.numpy()
for out in [out7, out8, out9, out10]:
np.testing.assert_allclose(out.numpy(), ref_bias, rtol=1e-5)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=[1, 2, 4, 4, 4], dtype=self.dtype
)
weight = paddle.static.data(
name="weight", shape=[2, 2, 3, 3, 3], dtype=self.dtype
)
# 1. Paddle Positional arguments
out1 = paddle.nn.functional.conv3d_transpose(x, weight)
# 2. Paddle keyword arguments
out2 = paddle.nn.functional.conv3d_transpose(x=x, weight=weight)
# 3. PyTorch keyword arguments (alias: input)
out3 = paddle.nn.functional.conv3d_transpose(input=x, weight=weight)
# 4. PyTorch function name alias
out4 = paddle.nn.functional.conv_transpose3d(x, weight)
# 5. PyTorch function name alias + PyTorch keyword
out5 = paddle.nn.functional.conv_transpose3d(input=x, weight=weight)
# 6. All positional arguments
out6 = paddle.nn.functional.conv3d_transpose(
x, weight, None, 1, 0, 0, 1, 1, None, 'NCDHW', None
)
# 7. All keyword arguments
out7 = paddle.nn.functional.conv3d_transpose(
x=x,
weight=weight,
bias=None,
stride=1,
padding=0,
output_padding=0,
groups=1,
dilation=1,
output_size=None,
data_format='NCDHW',
name=None,
)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={
"x": self.np_x,
"weight": self.np_weight,
},
fetch_list=[out1, out2, out3, out4, out5, out6, out7],
)
# Verify all outputs
for i in range(1, len(fetches)):
np.testing.assert_allclose(fetches[0], fetches[i], rtol=1e-5)
# Test Conv3DTranspose layer compatibility
@unittest.skipIf(
sys.platform == 'win32',
"Conv transpose compatibility tests not supported on Windows-Inference",
)
class TestConv3DTransposeLayerAPI(unittest.TestCase):
def test_paddle_style_keyword_only(self):
paddle.disable_static()
layer = paddle.nn.Conv3DTranspose(2, 2, 3)
self.assertIsNotNone(layer.weight)
self.assertIsNotNone(layer.bias)
def test_bias_false_disables_bias_attr(self):
paddle.disable_static()
layer = paddle.nn.Conv3DTranspose(2, 2, 3, bias=False)
self.assertIsNone(layer.bias)
def test_pytorch_style_positional_bias_only(self):
paddle.disable_static()
layer = paddle.nn.Conv3DTranspose(2, 2, 3, 1, 0, 0, 1, True)
self.assertIsNotNone(layer.bias)
def test_pytorch_style_full_positional(self):
paddle.disable_static()
layer = paddle.nn.ConvTranspose3d(
2, 2, 3, 1, 0, 0, 1, False, 1, 'zeros', None, None
)
self.assertIsNone(layer.bias)
def test_pytorch_style_duplicate_bias_raises(self):
paddle.disable_static()
with self.assertRaises(TypeError):
paddle.nn.Conv3DTranspose(2, 2, 3, 1, 0, 0, 1, True, bias=True)
class TestL1LossAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_input = np.random.rand(3, 4).astype("float32")
self.np_label = np.random.rand(3, 4).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
input = paddle.to_tensor(self.np_input)
label = paddle.to_tensor(self.np_label)
# 1. Paddle positional arguments
out1 = paddle.nn.functional.l1_loss(input, label)
# 2. Paddle keyword arguments
out2 = paddle.nn.functional.l1_loss(input=input, label=label)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.nn.functional.l1_loss(input=input, target=label)
# 4. Mixed arguments
out4 = paddle.nn.functional.l1_loss(input, target=label)
ref_out = np.mean(np.abs(self.np_input - self.np_label))
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
# PyTorch deprecated args translate to reduction
ref_sum = np.sum(np.abs(self.np_input - self.np_label))
ref_none = np.abs(self.np_input - self.np_label)
# 5. size_average=False translates to reduction='sum'
out5 = paddle.nn.functional.l1_loss(input, label, size_average=False)
np.testing.assert_allclose(out5.numpy(), ref_sum, rtol=1e-6)
# 6. reduce=False translates to reduction='none'
out6 = paddle.nn.functional.l1_loss(input, label, reduce=False)
np.testing.assert_allclose(out6.numpy(), ref_none, rtol=1e-6)
# 7. reduce=True + size_average=True translates to reduction='mean'
out7 = paddle.nn.functional.l1_loss(
input, label, reduce=True, size_average=True
)
np.testing.assert_allclose(out7.numpy(), ref_out, rtol=1e-6)
# 8. reduce=True + size_average=False translates to reduction='sum'
out8 = paddle.nn.functional.l1_loss(
input, label, reduce=True, size_average=False
)
np.testing.assert_allclose(out8.numpy(), ref_sum, rtol=1e-6)
# 9. legacy args combined with target alias
out9 = paddle.nn.functional.l1_loss(
input=input, target=label, size_average=False
)
np.testing.assert_allclose(out9.numpy(), ref_sum, rtol=1e-6)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
input = paddle.static.data(
name="input", shape=[3, 4], dtype='float32'
)
label = paddle.static.data(
name="label", shape=[3, 4], dtype='float32'
)
out1 = paddle.nn.functional.l1_loss(input, label)
out2 = paddle.nn.functional.l1_loss(input=input, label=label)
out3 = paddle.nn.functional.l1_loss(input=input, target=label)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"input": self.np_input, "label": self.np_label},
fetch_list=[out1, out2, out3],
)
ref_out = np.mean(np.abs(self.np_input - self.np_label))
for out in fetches:
np.testing.assert_allclose(out, ref_out, rtol=1e-6)
class TestKLDivAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
# input is log-probability
x = np.log(np.random.rand(5, 6).astype("float32") + 1e-3)
self.np_input = x
self.np_label = np.random.rand(5, 6).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
input = paddle.to_tensor(self.np_input)
label = paddle.to_tensor(self.np_label)
# 1. Paddle positional arguments
out1 = paddle.nn.functional.kl_div(input, label, 'mean')
# 2. Paddle keyword arguments
out2 = paddle.nn.functional.kl_div(input=input, label=label)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.nn.functional.kl_div(input=input, target=label)
# 4. Mixed arguments
out4 = paddle.nn.functional.kl_div(input, target=label)
for out in [out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), out1.numpy(), rtol=1e-6)
paddle.enable_static()
class TestSmoothL1LossAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_input = np.random.rand(3, 4).astype("float32")
self.np_label = np.random.rand(3, 4).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
input = paddle.to_tensor(self.np_input)
label = paddle.to_tensor(self.np_label)
# 1. Paddle positional arguments
out1 = paddle.nn.functional.smooth_l1_loss(input, label)
# 2. Paddle keyword arguments
out2 = paddle.nn.functional.smooth_l1_loss(input=input, label=label)
# 3. PyTorch keyword arguments (alias)
out3 = paddle.nn.functional.smooth_l1_loss(input=input, target=label)
# 4. Mixed arguments
out4 = paddle.nn.functional.smooth_l1_loss(input, target=label)
for out in [out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), out1.numpy(), rtol=1e-6)
paddle.enable_static()
class TestHingeEmbeddingLossAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_input = np.random.rand(3, 4).astype("float32")
self.np_label = np.random.choice([-1, 1], size=(3, 4)).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
input = paddle.to_tensor(self.np_input)
label = paddle.to_tensor(self.np_label)
out1 = paddle.nn.functional.hinge_embedding_loss(input, label)
out2 = paddle.nn.functional.hinge_embedding_loss(
input=input, label=label
)
out3 = paddle.nn.functional.hinge_embedding_loss(
input=input, target=label
)
out4 = paddle.nn.functional.hinge_embedding_loss(input, target=label)
for out in [out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), out1.numpy(), rtol=1e-6)
paddle.enable_static()
class TestCosineEmbeddingLossAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_input1 = np.random.rand(4, 5).astype("float32")
self.np_input2 = np.random.rand(4, 5).astype("float32")
self.np_label = np.array([1, -1, 1, -1], dtype="int64")
def test_dygraph_Compatibility(self):
paddle.disable_static()
input1 = paddle.to_tensor(self.np_input1)
input2 = paddle.to_tensor(self.np_input2)
label = paddle.to_tensor(self.np_label)
out1 = paddle.nn.functional.cosine_embedding_loss(input1, input2, label)
out2 = paddle.nn.functional.cosine_embedding_loss(
input1=input1, input2=input2, label=label
)
out3 = paddle.nn.functional.cosine_embedding_loss(
input1=input1, input2=input2, target=label
)
out4 = paddle.nn.functional.cosine_embedding_loss(
input1, input2, target=label
)
for out in [out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), out1.numpy(), rtol=1e-6)
paddle.enable_static()
class TestMultiLabelSoftMarginLossAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_input = np.random.rand(3, 4).astype("float32")
self.np_label = np.random.choice([-1, 1], size=(3, 4)).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
input = paddle.to_tensor(self.np_input)
label = paddle.to_tensor(self.np_label)
out1 = paddle.nn.functional.multi_label_soft_margin_loss(input, label)
out2 = paddle.nn.functional.multi_label_soft_margin_loss(
input=input, label=label
)
out3 = paddle.nn.functional.multi_label_soft_margin_loss(
input=input, target=label
)
out4 = paddle.nn.functional.multi_label_soft_margin_loss(
input, target=label
)
for out in [out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), out1.numpy(), rtol=1e-6)
paddle.enable_static()
class TestMultiLabelMarginLossAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_input = np.random.rand(2, 4).astype("float32")
self.np_label = np.array(
[[3, 0, -1, -1], [0, 2, -1, -1]], dtype="int64"
)
def test_dygraph_Compatibility(self):
paddle.disable_static()
input = paddle.to_tensor(self.np_input)
label = paddle.to_tensor(self.np_label)
out1 = paddle.nn.functional.multi_label_margin_loss(input, label)
out2 = paddle.nn.functional.multi_label_margin_loss(
input=input, label=label
)
out3 = paddle.nn.functional.multi_label_margin_loss(
input=input, target=label
)
out4 = paddle.nn.functional.multi_label_margin_loss(input, target=label)
for out in [out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), out1.numpy(), rtol=1e-6)
paddle.enable_static()
class TestSoftMarginLossAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_input = np.random.rand(3, 4).astype("float32")
self.np_label = np.random.choice([-1, 1], size=(3, 4)).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
input = paddle.to_tensor(self.np_input)
label = paddle.to_tensor(self.np_label)
out1 = paddle.nn.functional.soft_margin_loss(input, label)
out2 = paddle.nn.functional.soft_margin_loss(input=input, label=label)
out3 = paddle.nn.functional.soft_margin_loss(input=input, target=label)
out4 = paddle.nn.functional.soft_margin_loss(input, target=label)
for out in [out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), out1.numpy(), rtol=1e-6)
paddle.enable_static()
class TestTripletMarginWithDistanceLossAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_input = np.random.rand(3, 4).astype("float32")
self.np_pos = np.random.rand(3, 4).astype("float32")
self.np_neg = np.random.rand(3, 4).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
input = paddle.to_tensor(self.np_input)
pos = paddle.to_tensor(self.np_pos)
neg = paddle.to_tensor(self.np_neg)
out1 = paddle.nn.functional.triplet_margin_with_distance_loss(
input, pos, neg
)
out2 = paddle.nn.functional.triplet_margin_with_distance_loss(
input=input, positive=pos, negative=neg
)
# PyTorch alias: anchor instead of input
out3 = paddle.nn.functional.triplet_margin_with_distance_loss(
anchor=input, positive=pos, negative=neg
)
for out in [out2, out3]:
np.testing.assert_allclose(out.numpy(), out1.numpy(), rtol=1e-6)
paddle.enable_static()
class TestTripletMarginLossAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_input = np.random.rand(3, 4).astype("float32")
self.np_pos = np.random.rand(3, 4).astype("float32")
self.np_neg = np.random.rand(3, 4).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
input = paddle.to_tensor(self.np_input)
pos = paddle.to_tensor(self.np_pos)
neg = paddle.to_tensor(self.np_neg)
out1 = paddle.nn.functional.triplet_margin_loss(input, pos, neg)
out2 = paddle.nn.functional.triplet_margin_loss(
input=input, positive=pos, negative=neg
)
# PyTorch aliases: anchor instead of input, eps instead of epsilon
out3 = paddle.nn.functional.triplet_margin_loss(
anchor=input, positive=pos, negative=neg, eps=1e-06
)
out4 = paddle.nn.functional.triplet_margin_loss(
input, pos, neg, eps=1e-06
)
for out in [out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), out1.numpy(), rtol=1e-6)
paddle.enable_static()
class TestGaussianNLLLossAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_input = np.random.randn(5, 2).astype("float32")
self.np_label = np.random.randn(5, 2).astype("float32")
self.np_var = np.ones((5, 2)).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
input = paddle.to_tensor(self.np_input)
label = paddle.to_tensor(self.np_label)
var = paddle.to_tensor(self.np_var)
# 1. Paddle positional arguments
out1 = paddle.nn.functional.gaussian_nll_loss(input, label, var)
# 2. Paddle keyword arguments
out2 = paddle.nn.functional.gaussian_nll_loss(
input=input, label=label, variance=var
)
# 3. PyTorch keyword arguments (aliases)
out3 = paddle.nn.functional.gaussian_nll_loss(
input=input, target=label, var=var
)
# 4. Mixed
out4 = paddle.nn.functional.gaussian_nll_loss(
input, target=label, var=var, eps=1e-06
)
for out in [out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), out1.numpy(), rtol=1e-6)
paddle.enable_static()
class TestMarginRankingLossAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_input = np.random.rand(4, 5).astype("float32")
self.np_other = np.random.rand(4, 5).astype("float32")
self.np_label = np.random.choice([-1, 1], size=(4, 5)).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
input = paddle.to_tensor(self.np_input)
other = paddle.to_tensor(self.np_other)
label = paddle.to_tensor(self.np_label)
# 1. Paddle positional arguments
out1 = paddle.nn.functional.margin_ranking_loss(input, other, label)
# 2. Paddle keyword arguments
out2 = paddle.nn.functional.margin_ranking_loss(
input=input, other=other, label=label
)
# 3. PyTorch keyword arguments (aliases)
out3 = paddle.nn.functional.margin_ranking_loss(
input1=input, input2=other, target=label
)
for out in [out2, out3]:
np.testing.assert_allclose(out.numpy(), out1.numpy(), rtol=1e-6)
paddle.enable_static()
class TestGaussianNLLLossLayerAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_input = np.random.randn(5, 2).astype("float32")
self.np_label = np.random.randn(5, 2).astype("float32")
self.np_var = np.ones((5, 2)).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
input = paddle.to_tensor(self.np_input)
label = paddle.to_tensor(self.np_label)
var = paddle.to_tensor(self.np_var)
# Paddle: epsilon
layer1 = paddle.nn.GaussianNLLLoss(epsilon=1e-06)
# PyTorch alias: eps
layer2 = paddle.nn.GaussianNLLLoss(eps=1e-06)
out1 = layer1(input, label, var)
out2 = layer2(input, label, var)
np.testing.assert_allclose(out1.numpy(), out2.numpy(), rtol=1e-6)
paddle.enable_static()
class TestPoissonNLLLossLayerAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_input = np.random.randn(5, 2).astype("float32")
self.np_label = np.random.randn(5, 2).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
input = paddle.to_tensor(self.np_input)
label = paddle.to_tensor(self.np_label)
# Paddle: epsilon
layer1 = paddle.nn.PoissonNLLLoss(epsilon=1e-08)
# PyTorch alias: eps
layer2 = paddle.nn.PoissonNLLLoss(eps=1e-08)
out1 = layer1(input, label)
out2 = layer2(input, label)
np.testing.assert_allclose(out1.numpy(), out2.numpy(), rtol=1e-6)
paddle.enable_static()
class TestTripletMarginLossLayerAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_input = np.random.rand(3, 4).astype("float32")
self.np_pos = np.random.rand(3, 4).astype("float32")
self.np_neg = np.random.rand(3, 4).astype("float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
input = paddle.to_tensor(self.np_input)
pos = paddle.to_tensor(self.np_pos)
neg = paddle.to_tensor(self.np_neg)
# Paddle: epsilon
layer1 = paddle.nn.TripletMarginLoss(epsilon=1e-06)
# PyTorch alias: eps
layer2 = paddle.nn.TripletMarginLoss(eps=1e-06)
out1 = layer1(input, pos, neg)
out2 = layer2(input, pos, neg)
np.testing.assert_allclose(out1.numpy(), out2.numpy(), rtol=1e-6)
paddle.enable_static()
def _assert_unary_inplace_result(
testcase, x, out, ref_out, rtol=1e-6, atol=1e-6
):
testcase.assertIs(out, x)
np.testing.assert_allclose(out.numpy(), ref_out, rtol=rtol, atol=atol)
np.testing.assert_allclose(x.numpy(), ref_out, rtol=rtol, atol=atol)
class TestExpInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.7, -0.2, 0.3, 0.9], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.exp_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.exp_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.exp_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().exp_()
# Verify all outputs
ref_out = np.exp(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.exp(self.np_x)
out = paddle.exp_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestSqrtInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([0.25, 1.5, 2.25, 4.0], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.sqrt_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.sqrt_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.sqrt_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().sqrt_()
# Verify all outputs
ref_out = np.sqrt(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.sqrt(self.np_x)
out = paddle.sqrt_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestRsqrtInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([0.25, 1.5, 2.25, 4.0], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.rsqrt_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.rsqrt_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.rsqrt_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().rsqrt_()
# Verify all outputs
ref_out = 1.0 / np.sqrt(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = 1.0 / np.sqrt(self.np_x)
out = paddle.rsqrt_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestCeilInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.7, -0.2, 0.3, 0.9], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.ceil_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.ceil_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.ceil_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().ceil_()
# Verify all outputs
ref_out = np.ceil(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.ceil(self.np_x)
out = paddle.ceil_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestFloorInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.7, -0.2, 0.3, 0.9], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.floor_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.floor_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.floor_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().floor_()
# Verify all outputs
ref_out = np.floor(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.floor(self.np_x)
out = paddle.floor_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestReciprocalInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-2.0, -0.5, 0.25, 4.0], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.reciprocal_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.reciprocal_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.reciprocal_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().reciprocal_()
# Verify all outputs
ref_out = np.reciprocal(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.reciprocal(self.np_x)
out = paddle.reciprocal_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestSigmoidInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.7, -0.2, 0.3, 0.9], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.sigmoid_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.sigmoid_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.sigmoid_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().sigmoid_()
# Verify all outputs
ref_out = 1.0 / (1.0 + np.exp(-self.np_x))
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = 1.0 / (1.0 + np.exp(-self.np_x))
out = paddle.sigmoid_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestSinInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.7, -0.2, 0.3, 0.9], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.sin_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.sin_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.sin_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().sin_()
# Verify all outputs
ref_out = np.sin(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.sin(self.np_x)
out = paddle.sin_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestSinhInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.7, -0.2, 0.3, 0.9], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.sinh_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.sinh_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.sinh_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().sinh_()
# Verify all outputs
ref_out = np.sinh(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.sinh(self.np_x)
out = paddle.sinh_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestAsinInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.9, -0.25, 0.25, 0.9], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.asin_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.asin_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.asin_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().asin_()
# Verify all outputs
ref_out = np.arcsin(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.arcsin(self.np_x)
out = paddle.asin_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestAsinhInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.7, -0.2, 0.3, 0.9], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.asinh_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.asinh_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.asinh_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().asinh_()
# Verify all outputs
ref_out = np.arcsinh(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.arcsinh(self.np_x)
out = paddle.asinh_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestCosInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.7, -0.2, 0.3, 0.9], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.cos_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.cos_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.cos_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().cos_()
# Verify all outputs
ref_out = np.cos(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.cos(self.np_x)
out = paddle.cos_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestCoshInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.7, -0.2, 0.3, 0.9], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.cosh_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.cosh_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.cosh_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().cosh_()
# Verify all outputs
ref_out = np.cosh(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.cosh(self.np_x)
out = paddle.cosh_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestAcosInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.9, -0.25, 0.25, 0.9], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.acos_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.acos_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.acos_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().acos_()
# Verify all outputs
ref_out = np.arccos(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.arccos(self.np_x)
out = paddle.acos_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestAcoshInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([1.0, 1.5, 2.0, 3.5], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.acosh_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.acosh_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.acosh_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().acosh_()
# Verify all outputs
ref_out = np.arccosh(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.arccosh(self.np_x)
out = paddle.acosh_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestTanInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.7, -0.2, 0.3, 0.9], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.tan_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.tan_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.tan_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().tan_()
# Verify all outputs
ref_out = np.tan(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.tan(self.np_x)
out = paddle.tan_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestAtanInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.7, -0.2, 0.3, 0.9], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.atan_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.atan_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.atan_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().atan_()
# Verify all outputs
ref_out = np.arctan(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.arctan(self.np_x)
out = paddle.atan_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestAtanhInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.9, -0.25, 0.25, 0.9], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.atanh_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.atanh_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.atanh_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().atanh_()
# Verify all outputs
ref_out = np.arctanh(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.arctanh(self.np_x)
out = paddle.atanh_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestExpm1InplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.7, -0.2, 0.3, 0.9], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.expm1_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.expm1_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.expm1_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().expm1_()
# Verify all outputs
ref_out = np.expm1(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.expm1(self.np_x)
out = paddle.expm1_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestInvertPermutationAPI(unittest.TestCase):
def test_dygraph_Compatibility(self):
paddle.disable_static()
perm = paddle.to_tensor([2, 0, 1])
# 1. Paddle positional arguments
out1 = paddle.nn.utils.rnn.invert_permutation(perm)
# 2. Paddle keyword arguments
out2 = paddle.nn.utils.rnn.invert_permutation(permutation=perm)
# 3. None input
out3 = paddle.nn.utils.rnn.invert_permutation(None)
expected = np.array([1, 2, 0])
np.testing.assert_array_equal(out1.numpy(), expected)
np.testing.assert_array_equal(out2.numpy(), expected)
self.assertIsNone(out3)
paddle.enable_static()
class TestPackPaddedSequenceAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_seq = np.array(
[[4, 5, 6], [1, 2, 0], [3, 0, 0]], dtype=np.float32
)
self.lengths = [3, 2, 1]
def test_dygraph_Compatibility(self):
paddle.disable_static()
seq = paddle.to_tensor(self.np_seq)
lengths = paddle.to_tensor(self.lengths)
# 1. Paddle positional arguments
out1 = paddle.nn.utils.rnn.pack_padded_sequence(
seq, lengths, batch_first=True
)
# 2. Paddle keyword arguments
out2 = paddle.nn.utils.rnn.pack_padded_sequence(
input=seq, lengths=lengths, batch_first=True
)
# 3. Mixed arguments
out3 = paddle.nn.utils.rnn.pack_padded_sequence(
seq, lengths, batch_first=True
)
# 4. enforce_sorted=False
out4 = paddle.nn.utils.rnn.pack_padded_sequence(
seq, lengths, batch_first=True, enforce_sorted=False
)
expected_data = np.array(
[4.0, 1.0, 3.0, 5.0, 2.0, 6.0], dtype=np.float32
)
expected_batch_sizes = np.array([3, 2, 1], dtype=np.int64)
for out in [out1, out2, out3]:
np.testing.assert_allclose(out.data.numpy(), expected_data)
np.testing.assert_array_equal(
out.batch_sizes.numpy(), expected_batch_sizes
)
paddle.enable_static()
class TestPadPackedSequenceAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_seq = np.array(
[[4, 5, 6], [1, 2, 0], [3, 0, 0]], dtype=np.float32
)
self.lengths = [3, 2, 1]
def test_dygraph_Compatibility(self):
paddle.disable_static()
seq = paddle.to_tensor(self.np_seq)
lengths = paddle.to_tensor(self.lengths)
packed = paddle.nn.utils.rnn.pack_padded_sequence(
seq, lengths, batch_first=True
)
# 1. Paddle positional arguments
out1, lengths1 = paddle.nn.utils.rnn.pad_packed_sequence(
packed, batch_first=True
)
# 2. Paddle keyword arguments
out2, lengths2 = paddle.nn.utils.rnn.pad_packed_sequence(
sequence=packed, batch_first=True
)
# 3. Mixed arguments
out3, lengths3 = paddle.nn.utils.rnn.pad_packed_sequence(
packed, batch_first=True, padding_value=0.0
)
for out, lens in [(out1, lengths1), (out2, lengths2), (out3, lengths3)]:
np.testing.assert_allclose(out.numpy(), self.np_seq)
np.testing.assert_array_equal(
lens.numpy(), np.array(self.lengths, dtype=np.int64)
)
paddle.enable_static()
class TestPadSequenceAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.seq1 = np.random.rand(5, 10).astype(np.float32)
self.seq2 = np.random.rand(3, 10).astype(np.float32)
self.seq3 = np.random.rand(2, 10).astype(np.float32)
def test_dygraph_Compatibility(self):
paddle.disable_static()
seq1 = paddle.to_tensor(self.seq1)
seq2 = paddle.to_tensor(self.seq2)
seq3 = paddle.to_tensor(self.seq3)
sequences = [seq1, seq2, seq3]
# 1. Paddle positional arguments
out1 = paddle.nn.utils.rnn.pad_sequence(sequences)
# 2. Paddle keyword arguments
out2 = paddle.nn.utils.rnn.pad_sequence(sequences=sequences)
# 3. batch_first=True
out3 = paddle.nn.utils.rnn.pad_sequence(sequences, batch_first=True)
# 4. padding_value
out4 = paddle.nn.utils.rnn.pad_sequence(sequences, padding_value=-1.0)
# 5. padding_side='left'
out5 = paddle.nn.utils.rnn.pad_sequence(sequences, padding_side='left')
self.assertEqual(out1.shape, [5, 3, 10])
self.assertEqual(out3.shape, [3, 5, 10])
np.testing.assert_allclose(out1.numpy(), out2.numpy())
paddle.enable_static()
class TestUnpadSequenceAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.seq1 = np.random.rand(5, 10).astype(np.float32)
self.seq2 = np.random.rand(3, 10).astype(np.float32)
self.seq3 = np.random.rand(2, 10).astype(np.float32)
def test_dygraph_Compatibility(self):
paddle.disable_static()
seq1 = paddle.to_tensor(self.seq1)
seq2 = paddle.to_tensor(self.seq2)
seq3 = paddle.to_tensor(self.seq3)
sequences = [seq1, seq2, seq3]
padded = paddle.nn.utils.rnn.pad_sequence(sequences)
lengths = paddle.to_tensor([5, 3, 2])
# 1. Paddle positional arguments
out1 = paddle.nn.utils.rnn.unpad_sequence(padded, lengths)
# 2. Paddle keyword arguments
out2 = paddle.nn.utils.rnn.unpad_sequence(
padded_sequences=padded, lengths=lengths
)
# 3. batch_first=True
padded_bf = paddle.nn.utils.rnn.pad_sequence(
sequences, batch_first=True
)
out3 = paddle.nn.utils.rnn.unpad_sequence(
padded_bf, lengths, batch_first=True
)
for i, seq in enumerate(sequences):
np.testing.assert_allclose(out1[i].numpy(), seq.numpy())
np.testing.assert_allclose(out2[i].numpy(), seq.numpy())
np.testing.assert_allclose(out3[i].numpy(), seq.numpy())
paddle.enable_static()
class TestPackSequenceAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.seq1 = np.array([1, 2, 3], dtype=np.float32)
self.seq2 = np.array([4, 5], dtype=np.float32)
self.seq3 = np.array([6], dtype=np.float32)
def test_dygraph_Compatibility(self):
paddle.disable_static()
seq1 = paddle.to_tensor(self.seq1)
seq2 = paddle.to_tensor(self.seq2)
seq3 = paddle.to_tensor(self.seq3)
sequences = [seq1, seq2, seq3]
# 1. Paddle positional arguments
out1 = paddle.nn.utils.rnn.pack_sequence(sequences)
# 2. Paddle keyword arguments
out2 = paddle.nn.utils.rnn.pack_sequence(sequences=sequences)
# 3. enforce_sorted=False
out3 = paddle.nn.utils.rnn.pack_sequence(
sequences, enforce_sorted=False
)
expected_data = np.array(
[1.0, 4.0, 6.0, 2.0, 5.0, 3.0], dtype=np.float32
)
expected_batch_sizes = np.array([3, 2, 1], dtype=np.int64)
np.testing.assert_allclose(out1.data.numpy(), expected_data)
np.testing.assert_array_equal(
out1.batch_sizes.numpy(), expected_batch_sizes
)
np.testing.assert_allclose(out2.data.numpy(), expected_data)
paddle.enable_static()
class TestUnpackSequenceAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.seq1 = np.array([1, 2, 3], dtype=np.float32)
self.seq2 = np.array([4, 5], dtype=np.float32)
self.seq3 = np.array([6], dtype=np.float32)
def test_dygraph_Compatibility(self):
paddle.disable_static()
seq1 = paddle.to_tensor(self.seq1)
seq2 = paddle.to_tensor(self.seq2)
seq3 = paddle.to_tensor(self.seq3)
sequences = [seq1, seq2, seq3]
packed = paddle.nn.utils.rnn.pack_sequence(sequences)
# 1. Paddle positional arguments
out1 = paddle.nn.utils.rnn.unpack_sequence(packed)
# 2. Paddle keyword arguments
out2 = paddle.nn.utils.rnn.unpack_sequence(packed_sequences=packed)
for i, seq in enumerate(sequences):
np.testing.assert_allclose(out1[i].numpy(), seq)
np.testing.assert_allclose(out2[i].numpy(), seq)
paddle.enable_static()
class TestSquareInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([-0.7, -0.2, 0.3, 0.9], dtype="float32")
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
out1 = paddle.square_(x.clone())
# 2. Paddle keyword arguments
out2 = paddle.square_(x=x.clone())
# 3. PyTorch keyword arguments (alias)
out3 = paddle.square_(input=x.clone())
# 4. Tensor method - args
out4 = x.clone().square_()
# Verify all outputs
ref_out = np.square(self.np_x)
for out in [out1, out2, out3, out4]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
paddle.enable_static()
def test_dygraph_InplaceInput(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
ref_out = np.square(self.np_x)
out = paddle.square_(x)
_assert_unary_inplace_result(self, x, out, ref_out)
paddle.enable_static()
class TestInferenceModeAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.array([1.0, 2.0, 3.0], dtype="float32")
self.shape = self.np_x.shape
self.dtype = str(self.np_x.dtype)
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x, stop_gradient=False)
# 1. Paddle Positional arguments
ctx = paddle.inference_mode()
self.assertTrue(paddle.is_grad_enabled())
with ctx:
out1 = x * 2
self.assertFalse(paddle.is_grad_enabled())
self.assertTrue(paddle.is_grad_enabled())
# 2. Paddle keyword arguments
with paddle.inference_mode(mode=True):
out2 = x * 2
self.assertFalse(paddle.is_grad_enabled())
# 3. PyTorch keyword arguments
with paddle.no_grad(), paddle.inference_mode(mode=False):
out3 = x * 2
self.assertTrue(paddle.is_grad_enabled())
# 4. Decorator without parentheses
@paddle.inference_mode
def no_grad_decorated(tensor):
out = tensor * 2
self.assertFalse(paddle.is_grad_enabled())
return out
out4 = no_grad_decorated(x)
# 5. Decorator with mode=False
@paddle.inference_mode(mode=False)
def enable_grad_decorated(tensor):
out = tensor * 2
self.assertTrue(paddle.is_grad_enabled())
return out
with paddle.no_grad():
out5 = enable_grad_decorated(x)
def mode_func(tensor):
return tensor * 2
out6 = paddle.inference_mode(mode=mode_func)(x)
# Verify all outputs
ref_out = self.np_x * 2
for out in [out1, out2, out3, out4, out5, out6]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
for out in [out1, out2, out4, out6]:
self.assertTrue(out.stop_gradient)
for out in [out3, out5]:
self.assertFalse(out.stop_gradient)
self.assertTrue(paddle.is_grad_enabled())
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(name="x", shape=self.shape, dtype=self.dtype)
x.stop_gradient = False
# 1. Paddle Positional arguments
with paddle.inference_mode():
out1 = x * 2
# 2. Paddle keyword arguments
with paddle.inference_mode(mode=True):
out2 = x * 2
# 3. PyTorch keyword arguments
with paddle.no_grad(), paddle.inference_mode(mode=False):
out3 = x * 2
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x},
fetch_list=[out1, out2, out3],
)
# Verify all outputs
ref_out = self.np_x * 2
for out in fetches:
np.testing.assert_allclose(out, ref_out, rtol=1e-6)
class TestTensorIndexCopyInplaceAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_x = np.zeros((2, 3, 4), dtype="float32")
self.np_source_dim1 = (
np.arange(1, 17).reshape(2, 2, 4).astype("float32")
)
self.np_source_dim2 = (
np.arange(1, 13).reshape(2, 3, 2).astype("float32")
)
def _expected(self, x, dim, index, source):
expected = x.copy()
for i, idx in enumerate(index):
dest_index = [slice(None)] * expected.ndim
src_index = [slice(None)] * source.ndim
dest_index[dim] = idx
src_index[dim] = i
expected[tuple(dest_index)] = source[tuple(src_index)]
return expected
def test_dygraph_Compatibility(self):
paddle.disable_static()
index = paddle.to_tensor([0, 2], dtype="int64")
source_dim1 = paddle.to_tensor(self.np_source_dim1)
source_dim2 = paddle.to_tensor(self.np_source_dim2)
# 1. Tensor method - positional args
x1 = paddle.to_tensor(self.np_x)
out1 = x1.index_copy_(1, index, source_dim1)
# 2. Tensor method - keyword args
x2 = paddle.to_tensor(self.np_x)
out2 = x2.index_copy_(dim=1, index=index, source=source_dim1)
# 3. Tensor method - mixed args
x3 = paddle.to_tensor(self.np_x)
out3 = x3.index_copy_(1, index=index, source=source_dim1)
# 4. Tensor method - negative dim
x4 = paddle.to_tensor(self.np_x)
out4 = x4.index_copy_(-1, index, source_dim2)
# 5. Tensor method - dim 0
x5 = paddle.zeros([3, 2], dtype="int32")
out5 = x5.index_copy_(
0,
paddle.to_tensor([0, 2], dtype="int64"),
paddle.to_tensor([[3, 4], [5, 6]], dtype="int32"),
)
# 6. Tensor method - scalar tensor
x6 = paddle.zeros([], dtype="float32")
out6 = x6.index_copy_(
0, paddle.to_tensor([0], dtype="int64"), paddle.to_tensor(7.0)
)
# 7. Tensor method - scalar source
x7 = paddle.zeros([3], dtype="float32")
out7 = x7.index_copy_(
0, paddle.to_tensor([1], dtype="int64"), paddle.to_tensor(8.0)
)
# 8. Tensor method - empty index
x8 = paddle.ones([2, 3], dtype="float32")
out8 = x8.index_copy_(
1,
paddle.to_tensor([], dtype="int64"),
paddle.empty([2, 0], dtype="float32"),
)
# 9. Tensor method - scalar index
x9 = paddle.zeros([3], dtype="float32")
out9 = x9.index_copy_(
0, paddle.to_tensor(1, dtype="int64"), paddle.to_tensor([9.0])
)
# 10. Tensor method - scalar tensor with non-scalar source
x10 = paddle.zeros([], dtype="float32")
out10 = x10.index_copy_(
-1, paddle.to_tensor(0, dtype="int64"), paddle.to_tensor([10.0])
)
# 11. Tensor method - scalar tensor with empty index
x11 = paddle.zeros([], dtype="float32")
out11 = x11.index_copy_(
0,
paddle.to_tensor([], dtype="int64"),
paddle.empty([0], dtype="float32"),
)
ref_dim1 = self._expected(self.np_x, 1, [0, 2], self.np_source_dim1)
ref_dim2 = self._expected(self.np_x, 2, [0, 2], self.np_source_dim2)
for out in [out1, out2, out3]:
np.testing.assert_allclose(out.numpy(), ref_dim1, rtol=1e-6)
np.testing.assert_allclose(out4.numpy(), ref_dim2, rtol=1e-6)
np.testing.assert_array_equal(
out5.numpy(), np.array([[3, 4], [0, 0], [5, 6]], dtype="int32")
)
np.testing.assert_allclose(out6.numpy(), np.array(7.0, dtype="float32"))
np.testing.assert_allclose(
out7.numpy(), np.array([0.0, 8.0, 0.0], dtype="float32")
)
np.testing.assert_allclose(
out8.numpy(), np.ones([2, 3], dtype="float32")
)
np.testing.assert_allclose(
out9.numpy(), np.array([0.0, 9.0, 0.0], dtype="float32")
)
np.testing.assert_allclose(
out10.numpy(), np.array(10.0, dtype="float32")
)
np.testing.assert_allclose(
out11.numpy(), np.array(0.0, dtype="float32")
)
self.assertIs(out1, x1)
self.assertIs(out6, x6)
self.assertIs(out10, x10)
self.assertIs(out11, x11)
with self.assertRaises(IndexError):
paddle.zeros([], dtype="float32").index_copy_(
1, paddle.to_tensor([0], dtype="int64"), paddle.to_tensor(7.0)
)
with self.assertRaises(RuntimeError):
paddle.to_tensor(self.np_x).index_copy_(
1,
paddle.to_tensor([0, 2], dtype="int32"),
source_dim1,
)
with self.assertRaises(RuntimeError):
paddle.to_tensor(self.np_x).index_copy_(
1,
index,
paddle.ones(self.np_source_dim1.shape, dtype="float64"),
)
with self.assertRaises(IndexError):
paddle.to_tensor(self.np_x).index_copy_(
1,
paddle.to_tensor([[0, 2]], dtype="int64"),
source_dim1,
)
with self.assertRaises(IndexError):
paddle.to_tensor(self.np_x).index_copy_(
1,
index,
paddle.ones([2, 2], dtype="float32"),
)
with self.assertRaises(IndexError):
paddle.to_tensor(self.np_x).index_copy_(
1,
paddle.to_tensor([0], dtype="int64"),
source_dim1,
)
with self.assertRaises(IndexError):
paddle.zeros([3], dtype="float32").index_copy_(
0,
paddle.to_tensor([0, 1], dtype="int64"),
paddle.to_tensor(7.0),
)
with self.assertRaises(RuntimeError):
paddle.to_tensor(self.np_x).index_copy_(
1,
index,
paddle.ones([2, 2, 3], dtype="float32"),
)
with self.assertRaises(IndexError):
paddle.zeros([3], dtype="float32").index_copy_(
0,
paddle.to_tensor([-1], dtype="int64"),
paddle.to_tensor([7.0]),
)
with self.assertRaises(IndexError):
paddle.zeros([3], dtype="float32").index_copy_(
0,
paddle.to_tensor([3], dtype="int64"),
paddle.to_tensor([7.0]),
)
paddle.enable_static()
def test_dygraph_functionality(self):
paddle.disable_static()
x = paddle.arange(60, dtype="float64").reshape([3, 4, 5])
source = paddle.arange(100, 140, dtype="float64").reshape([2, 4, 5])
out = x.index_copy_(0, paddle.to_tensor([2, 0], dtype="int64"), source)
expected = self._expected(
np.arange(60).reshape([3, 4, 5]).astype("float64"),
0,
[2, 0],
np.arange(100, 140).reshape([2, 4, 5]).astype("float64"),
)
np.testing.assert_allclose(out.numpy(), expected, rtol=1e-6)
self.assertIs(out, x)
x = paddle.zeros([2, 0, 3], dtype="float32")
source = paddle.empty([2, 0, 0], dtype="float32")
out = x.index_copy_(2, paddle.to_tensor([], dtype="int64"), source)
np.testing.assert_allclose(out.numpy(), np.zeros([2, 0, 3], "float32"))
x = paddle.ones([3, 2], dtype="float32")
source = paddle.to_tensor([[2.0, 3.0], [4.0, 5.0]])
out = x.index_copy_(0, paddle.to_tensor([2, 0], dtype="int64"), source)
np.testing.assert_allclose(
out.numpy(),
np.array([[4.0, 5.0], [1.0, 1.0], [2.0, 3.0]], dtype="float32"),
)
paddle.enable_static()
def test_dygraph_backward(self):
paddle.disable_static()
x = paddle.arange(6, dtype="float32").reshape([3, 2])
x.stop_gradient = False
source = paddle.to_tensor(
[[7.0, 8.0], [9.0, 10.0]], stop_gradient=False
)
out = x.clone().index_copy_(
0, paddle.to_tensor([0, 2], dtype="int64"), source
)
out.sum().backward()
np.testing.assert_allclose(
out.numpy(),
np.array([[7.0, 8.0], [2.0, 3.0], [9.0, 10.0]], dtype="float32"),
rtol=1e-6,
)
np.testing.assert_allclose(
x.grad.numpy(),
np.array([[0.0, 0.0], [1.0, 1.0], [0.0, 0.0]], dtype="float32"),
rtol=1e-6,
)
np.testing.assert_allclose(
source.grad.numpy(), np.ones([2, 2], dtype="float32"), rtol=1e-6
)
paddle.enable_static()
class TestKaiserWindowAPI(unittest.TestCase):
def setUp(self):
self.window_length = 7
self.beta = 6.0
def _expected(
self, window_length, periodic=True, beta=12.0, dtype="float32"
):
if window_length <= 1:
return np.ones((window_length,), dtype=dtype)
length = window_length + 1 if periodic else window_length
n = np.arange(length, dtype=dtype)
alpha = (length - 1) / 2.0
out = np.i0(beta * np.sqrt(1 - ((n - alpha) / alpha) ** 2.0)) / np.i0(
beta
)
if periodic:
out = out[:-1]
return out.astype(dtype)
def test_dygraph_Compatibility(self):
paddle.disable_static()
# 1. Paddle Positional arguments
out1 = paddle.kaiser_window(
self.window_length, False, self.beta, dtype='float64'
)
# 2. Paddle keyword arguments
out2 = paddle.kaiser_window(
window_length=self.window_length,
periodic=False,
beta=self.beta,
dtype='float64',
)
# 3. Mixed arguments
out3 = paddle.kaiser_window(
self.window_length, periodic=False, beta=self.beta, dtype='float64'
)
# 4-5. out parameter test
out4 = paddle.empty([self.window_length], dtype='float64')
out5 = paddle.kaiser_window(
self.window_length,
False,
self.beta,
dtype='float64',
out=out4,
)
self.assertIs(out4, out5)
# 6. Explicit dtype=None compatibility
out6 = paddle.kaiser_window(3, dtype=None)
# 7. strided layout compatibility
out7 = paddle.kaiser_window(
self.window_length,
False,
self.beta,
dtype='float64',
layout='strided',
)
# 8. window_length=0 edge case
out8 = paddle.kaiser_window(0)
# 9. window_length=1 edge case
out9 = paddle.kaiser_window(1, dtype='float64')
expected = self._expected(
self.window_length, periodic=False, beta=self.beta, dtype='float64'
)
for out in [out1, out2, out3, out4, out5, out7]:
np.testing.assert_allclose(out.numpy(), expected, rtol=1e-12)
np.testing.assert_allclose(out6.numpy(), self._expected(3), rtol=1e-5)
self.assertEqual(out6.dtype, paddle.float32)
np.testing.assert_allclose(out8.numpy(), np.ones((0,), dtype='float32'))
np.testing.assert_allclose(out9.numpy(), np.ones((1,), dtype='float64'))
self.assertEqual(out9.dtype, paddle.float64)
with self.assertRaises(RuntimeError):
paddle.kaiser_window(3, layout='sparse')
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
# 1. Paddle Positional arguments
out1 = paddle.kaiser_window(
self.window_length, False, self.beta, dtype='float64'
)
# 2. Paddle keyword arguments
out2 = paddle.kaiser_window(
window_length=self.window_length,
periodic=False,
beta=self.beta,
dtype='float64',
)
# 3. Mixed arguments
out3 = paddle.kaiser_window(
self.window_length,
periodic=False,
beta=self.beta,
dtype='float64',
)
# 4. Explicit dtype=None compatibility
out4 = paddle.kaiser_window(3, dtype=None)
# 5. strided layout compatibility
out5 = paddle.kaiser_window(
self.window_length,
False,
self.beta,
dtype='float64',
layout='strided',
)
exe = paddle.static.Executor()
fetches = exe.run(
main,
fetch_list=[out1, out2, out3, out4, out5],
)
expected = self._expected(
self.window_length, periodic=False, beta=self.beta, dtype='float64'
)
for out in fetches[:3] + fetches[4:]:
np.testing.assert_allclose(out, expected, rtol=1e-12)
np.testing.assert_allclose(fetches[3], self._expected(3), rtol=1e-5)
class TestLayerNormAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.normalized_shape = [2, 3]
self.x_shape = [2, 2, 3]
self.eps = 1e-5
self.np_x = np.random.rand(*self.x_shape).astype("float32")
def _expected(self):
axes = tuple(
range(
len(self.x_shape) - len(self.normalized_shape),
len(self.x_shape),
)
)
mean = np.mean(self.np_x, axis=axes, keepdims=True)
var = np.var(self.np_x, axis=axes, keepdims=True)
return (self.np_x - mean) / np.sqrt(var + self.eps)
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
# 1. Paddle Positional arguments
layer1 = paddle.nn.LayerNorm(self.normalized_shape, self.eps)
out1 = layer1(x)
# 2. Paddle keyword arguments
layer2 = paddle.nn.LayerNorm(
normalized_shape=self.normalized_shape, epsilon=self.eps
)
out2 = layer2(x)
# 3. PyTorch Positional arguments
layer3 = paddle.nn.LayerNorm(self.normalized_shape, self.eps, False)
out3 = layer3(x)
# 4. PyTorch keyword arguments (alias)
layer4 = paddle.nn.LayerNorm(
normalized_shape=self.normalized_shape,
eps=self.eps,
elementwise_affine=False,
)
out4 = layer4(x)
# 5. Mixed arguments
layer5 = paddle.nn.LayerNorm(
self.normalized_shape, eps=self.eps, bias=False
)
out5 = layer5(x)
# 6. PyTorch positional bias/device/dtype arguments
layer6 = paddle.nn.LayerNorm(
self.normalized_shape, self.eps, True, True, None, "float64"
)
expected = self._expected()
for out in [out1, out2, out3, out4, out5]:
np.testing.assert_allclose(out.numpy(), expected, rtol=1e-5)
self.assertIsNone(layer3.weight)
self.assertIsNone(layer3.bias)
self.assertIsNotNone(layer5.weight)
self.assertIsNone(layer5.bias)
self.assertEqual(layer6.weight.dtype, paddle.float64)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.x_shape, dtype=str(self.np_x.dtype)
)
# 1. Paddle Positional arguments
layer1 = paddle.nn.LayerNorm(self.normalized_shape, self.eps)
out1 = layer1(x)
# 2. Paddle keyword arguments
layer2 = paddle.nn.LayerNorm(
normalized_shape=self.normalized_shape, epsilon=self.eps
)
out2 = layer2(x)
# 3. PyTorch Positional arguments
layer3 = paddle.nn.LayerNorm(self.normalized_shape, self.eps, False)
out3 = layer3(x)
# 4. PyTorch keyword arguments (alias)
layer4 = paddle.nn.LayerNorm(
normalized_shape=self.normalized_shape,
eps=self.eps,
elementwise_affine=False,
)
out4 = layer4(x)
self.assertIsNone(layer3.weight)
self.assertIsNone(layer3.bias)
exe = paddle.static.Executor()
exe.run(startup)
fetches = exe.run(
main,
feed={"x": self.np_x},
fetch_list=[out1, out2, out3, out4],
)
expected = self._expected()
for out in fetches:
np.testing.assert_allclose(out, expected, rtol=1e-5)
class TestMultivariateNormalAPI(unittest.TestCase):
def setUp(self):
self.place = paddle.CPUPlace()
self.np_loc = np.array([2.0, -1.0], dtype="float32")
self.np_cov = np.array([[2.0, 0.5], [0.5, 1.5]], dtype="float32")
self.np_value = np.array([0.2, -0.8], dtype="float32")
self.np_scale_tril = np.linalg.cholesky(self.np_cov)
self.expected_mean = self.np_loc
self.expected_variance = np.diag(self.np_cov)
self.expected_entropy = (
0.5 * self.np_loc.shape[0] * (1.0 + np.log(2 * np.pi))
+ np.log(np.diag(self.np_scale_tril)).sum()
)
diff = self.np_value - self.np_loc
mahalanobis = diff @ np.linalg.solve(self.np_cov, diff)
self.expected_log_prob = (
-0.5 * (self.np_loc.shape[0] * np.log(2 * np.pi) + mahalanobis)
- np.log(np.diag(self.np_scale_tril)).sum()
)
def tearDown(self):
paddle.enable_static()
def test_dygraph_Compatibility(self):
paddle.disable_static()
loc = paddle.to_tensor(self.np_loc, place=self.place)
cov = paddle.to_tensor(self.np_cov, place=self.place)
value = paddle.to_tensor(self.np_value, place=self.place)
# 1. Paddle Positional arguments
out1 = paddle.distribution.MultivariateNormal(loc, cov)
# 2. Paddle keyword arguments
out2 = paddle.distribution.MultivariateNormal(
loc=loc, covariance_matrix=cov
)
# 3. PyTorch Positional arguments
out3 = paddle.distribution.MultivariateNormal(
loc, cov, None, None, False
)
# 4. PyTorch keyword arguments
out4 = paddle.distribution.MultivariateNormal(
loc=loc, covariance_matrix=cov, validate_args=True
)
# 5. Mixed arguments
out5 = paddle.distribution.MultivariateNormal(
loc, covariance_matrix=cov, validate_args=None
)
for out in [out1, out2, out3, out4, out5]:
np.testing.assert_allclose(out.mean.numpy(), self.expected_mean)
np.testing.assert_allclose(
out.variance.numpy(), self.expected_variance
)
np.testing.assert_allclose(
out.entropy().numpy(), self.expected_entropy, rtol=1e-5
)
np.testing.assert_allclose(
out.log_prob(value).numpy(),
self.expected_log_prob,
rtol=1e-5,
)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
loc = paddle.static.data(
name="loc",
shape=self.np_loc.shape,
dtype=str(self.np_loc.dtype),
)
cov = paddle.static.data(
name="cov",
shape=self.np_cov.shape,
dtype=str(self.np_cov.dtype),
)
value = paddle.static.data(
name="value",
shape=self.np_value.shape,
dtype=str(self.np_value.dtype),
)
# 1. Paddle Positional arguments
out1 = paddle.distribution.MultivariateNormal(loc, cov)
# 2. Paddle keyword arguments
out2 = paddle.distribution.MultivariateNormal(
loc=loc, covariance_matrix=cov
)
# 3. PyTorch Positional arguments
out3 = paddle.distribution.MultivariateNormal(
loc, cov, None, None, False
)
# 4. PyTorch keyword arguments
out4 = paddle.distribution.MultivariateNormal(
loc=loc, covariance_matrix=cov, validate_args=True
)
# 5. Mixed arguments
out5 = paddle.distribution.MultivariateNormal(
loc, covariance_matrix=cov, validate_args=None
)
fetches = []
for out in [out1, out2, out3, out4, out5]:
fetches.extend(
[out.mean, out.variance, out.entropy(), out.log_prob(value)]
)
exe = paddle.static.Executor(self.place)
outputs = exe.run(
main,
feed={
"loc": self.np_loc,
"cov": self.np_cov,
"value": self.np_value,
},
fetch_list=fetches,
)
for i in range(0, len(outputs), 4):
np.testing.assert_allclose(outputs[i], self.expected_mean)
np.testing.assert_allclose(outputs[i + 1], self.expected_variance)
np.testing.assert_allclose(
outputs[i + 2], self.expected_entropy, rtol=1e-5
)
np.testing.assert_allclose(
outputs[i + 3], self.expected_log_prob, rtol=1e-5
)
class TestDistributionAPI(unittest.TestCase):
def tearDown(self):
paddle.distribution.Distribution.set_default_validate_args(__debug__)
paddle.enable_static()
def test_dygraph_Compatibility(self):
paddle.disable_static()
distribution_cls = paddle.distribution.Distribution
# 1. Paddle Positional arguments
distribution_cls.set_default_validate_args(False)
out1 = distribution_cls((2,), (3,))
# 2. Paddle keyword arguments
out2 = distribution_cls(
batch_shape=[2], event_shape=[3], validate_args=True
)
# 3. Mixed arguments
out3 = distribution_cls((2,), event_shape=[3], validate_args=False)
# Verify constructor compatibility
self.assertEqual(out1.batch_shape, (2,))
self.assertEqual(out1.event_shape, (3,))
self.assertFalse(out1._validate_args_enabled)
self.assertTrue(out2._validate_args_enabled)
self.assertFalse(out3._validate_args_enabled)
self.assertTrue(callable(out2._validate_args))
with self.assertRaises(ValueError):
distribution_cls.set_default_validate_args(None)
value = paddle.to_tensor([0.5], dtype="float32")
for attr in ["arg_constraints", "support"]:
with self.assertRaises(NotImplementedError):
getattr(out1, attr)
for api in [out1.cdf, out1.icdf]:
with self.assertRaises(NotImplementedError):
api(value)
with self.assertRaises(NotImplementedError):
out1.enumerate_support()
with self.assertRaises(NotImplementedError):
out1.sample_n(3)
with self.assertRaises(NotImplementedError):
out1.perplexity()
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
paddle.distribution.Distribution.set_default_validate_args(True)
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
distribution_cls = paddle.distribution.Distribution
# 1. Paddle Positional arguments
out1 = distribution_cls((2,), (3,), validate_args=False)
# 2. Paddle keyword arguments
out2 = distribution_cls(
batch_shape=[2], event_shape=[3], validate_args=True
)
# 3. Mixed arguments
out3 = distribution_cls((2,), event_shape=[3])
self.assertEqual(out1.batch_shape, (2,))
self.assertEqual(out1.event_shape, (3,))
self.assertFalse(out1._validate_args_enabled)
self.assertTrue(out2._validate_args_enabled)
self.assertTrue(out3._validate_args_enabled)
self.assertTrue(callable(out1._validate_args))
with self.assertRaises(NotImplementedError):
out1.sample_n(3)
with self.assertRaises(NotImplementedError):
out1.perplexity()
class TestNormalValidateArgsAPI(unittest.TestCase):
def setUp(self):
self.place = paddle.CPUPlace()
self.np_loc = np.array([0.0, 1.0, -1.0], dtype="float32")
self.np_scale = np.array([1.0, 2.0, 0.5], dtype="float32")
self.np_value = np.array([0.2, 0.8, -0.3], dtype="float32")
def tearDown(self):
paddle.distribution.Distribution.set_default_validate_args(__debug__)
paddle.enable_static()
def _expected_log_prob(self):
var = self.np_scale * self.np_scale
return (
-((self.np_value - self.np_loc) * (self.np_value - self.np_loc))
/ (2.0 * var)
- np.log(self.np_scale)
- np.log(np.sqrt(2.0 * np.pi))
)
def test_dygraph_Compatibility(self):
paddle.disable_static()
paddle.distribution.Distribution.set_default_validate_args(False)
loc = paddle.to_tensor(self.np_loc, place=self.place)
scale = paddle.to_tensor(self.np_scale, place=self.place)
value = paddle.to_tensor(self.np_value, place=self.place)
# 1. Paddle Positional arguments
dist1 = paddle.distributions.normal.Normal(loc, scale)
out1 = dist1.log_prob(value)
# 2. Paddle keyword arguments
dist2 = paddle.distributions.normal.Normal(loc=loc, scale=scale)
out2 = dist2.log_prob(value)
# 3. PyTorch Positional arguments
dist3 = paddle.distributions.normal.Normal(loc, scale, False)
out3 = dist3.log_prob(value)
# 4. PyTorch keyword arguments
dist4 = paddle.distributions.normal.Normal(
loc=loc, scale=scale, validate_args=False
)
out4 = dist4.log_prob(value)
# 5. Mixed arguments
dist5 = paddle.distributions.normal.Normal(loc, scale=scale)
out5 = dist5.log_prob(value)
ref_out = self._expected_log_prob()
for out in [out1, out2, out3, out4, out5]:
np.testing.assert_allclose(out.numpy(), ref_out, rtol=1e-6)
self.assertFalse(dist3._validate_args_enabled)
self.assertFalse(dist4._validate_args_enabled)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
paddle.distribution.Distribution.set_default_validate_args(False)
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
loc = paddle.static.data(
name="loc", shape=self.np_loc.shape, dtype="float32"
)
scale = paddle.static.data(
name="scale", shape=self.np_scale.shape, dtype="float32"
)
value = paddle.static.data(
name="value", shape=self.np_value.shape, dtype="float32"
)
# 1. Paddle Positional arguments
out1 = paddle.distributions.normal.Normal(loc, scale).log_prob(
value
)
# 2. Paddle keyword arguments
out2 = paddle.distributions.normal.Normal(
loc=loc, scale=scale
).log_prob(value)
# 3. PyTorch Positional arguments
out3 = paddle.distributions.normal.Normal(
loc, scale, False
).log_prob(value)
# 4. PyTorch keyword arguments
out4 = paddle.distributions.normal.Normal(
loc=loc, scale=scale, validate_args=False
).log_prob(value)
# 5. Mixed arguments
out5 = paddle.distributions.normal.Normal(
loc, scale=scale
).log_prob(value)
exe = paddle.static.Executor(self.place)
fetches = exe.run(
main,
feed={
"loc": self.np_loc,
"scale": self.np_scale,
"value": self.np_value,
},
fetch_list=[out1, out2, out3, out4, out5],
)
ref_out = self._expected_log_prob()
for out in fetches:
np.testing.assert_allclose(out, ref_out, rtol=1e-6)
class TestDistributionSampleShapeAliasAPI(unittest.TestCase):
def setUp(self):
self.place = paddle.CPUPlace()
self.np_loc = np.array([0.0, 1.0], dtype="float32")
self.np_scale = np.array([1.0, 2.0], dtype="float32")
def tearDown(self):
paddle.enable_static()
def _check_shape(self, tensor, expected):
self.assertEqual(list(tensor.shape), expected)
def test_normal_sample_shape_alias(self):
paddle.disable_static()
loc = paddle.to_tensor(self.np_loc, place=self.place)
scale = paddle.to_tensor(self.np_scale, place=self.place)
dist = paddle.distributions.Normal(loc, scale)
self._check_shape(dist.sample(shape=[2, 3]), [2, 3, 2])
self._check_shape(dist.sample(sample_shape=[2, 3]), [2, 3, 2])
self._check_shape(dist.rsample(shape=[2, 3]), [2, 3, 2])
self._check_shape(dist.rsample(sample_shape=[2, 3]), [2, 3, 2])
paddle.enable_static()
def test_normal_sample_shape_alias_static(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
loc = paddle.static.data(
name="loc", shape=self.np_loc.shape, dtype="float32"
)
scale = paddle.static.data(
name="scale", shape=self.np_scale.shape, dtype="float32"
)
dist = paddle.distributions.Normal(loc, scale)
out1 = dist.sample(shape=[2, 3])
out2 = dist.sample(sample_shape=[2, 3])
out3 = dist.rsample(shape=[2, 3])
out4 = dist.rsample(sample_shape=[2, 3])
exe = paddle.static.Executor(self.place)
fetches = exe.run(
main,
feed={
"loc": self.np_loc,
"scale": self.np_scale,
},
fetch_list=[out1, out2, out3, out4],
)
for out in fetches:
self._check_shape(out, [2, 3, 2])
def test_dygraph_Compatibility(self):
paddle.disable_static()
normal = paddle.distributions.Normal(
paddle.to_tensor([0.0, 1.0]),
paddle.to_tensor([1.0, 2.0]),
)
self._check_shape(normal.sample(sample_shape=[2, 3]), [2, 3, 2])
self._check_shape(normal.rsample(sample_shape=[2, 3]), [2, 3, 2])
cauchy = paddle.distributions.Cauchy(
paddle.to_tensor([0.0, 1.0]), paddle.to_tensor([1.0, 2.0])
)
self._check_shape(cauchy.sample(sample_shape=[2, 3]), [2, 3, 2])
self._check_shape(cauchy.rsample(sample_shape=[2, 3]), [2, 3, 2])
laplace = paddle.distributions.Laplace(
paddle.to_tensor([0.0, 1.0]), paddle.to_tensor([1.0, 2.0])
)
self._check_shape(laplace.sample(sample_shape=[2, 3]), [2, 3, 2])
self._check_shape(laplace.rsample(sample_shape=[2, 3]), [2, 3, 2])
exponential = paddle.distributions.Exponential(
paddle.to_tensor([1.0, 2.0])
)
self._check_shape(exponential.sample(sample_shape=[2, 3]), [2, 3, 2])
self._check_shape(exponential.rsample(sample_shape=[2, 3]), [2, 3, 2])
gamma = paddle.distributions.Gamma(
paddle.to_tensor([1.0, 2.0]), paddle.to_tensor([2.0, 3.0])
)
self._check_shape(gamma.sample(sample_shape=[2, 3]), [2, 3, 2])
self._check_shape(gamma.rsample(sample_shape=[2, 3]), [2, 3, 2])
geometric = paddle.distributions.Geometric(paddle.to_tensor([0.2, 0.7]))
self._check_shape(geometric.sample(sample_shape=[2, 3]), [2, 3, 2])
self._check_shape(geometric.rsample(sample_shape=[2, 3]), [2, 3, 2])
gumbel = paddle.distributions.Gumbel(
paddle.to_tensor([0.0, 1.0]), paddle.to_tensor([1.0, 2.0])
)
self._check_shape(gumbel.sample(sample_shape=[2, 3]), [2, 3, 2])
self._check_shape(gumbel.rsample(sample_shape=[2, 3]), [2, 3, 2])
continuous_bernoulli = paddle.distributions.ContinuousBernoulli(
paddle.to_tensor([0.3, 0.7])
)
self._check_shape(
continuous_bernoulli.sample(sample_shape=[2, 3]), [2, 3, 2]
)
self._check_shape(
continuous_bernoulli.rsample(sample_shape=[2, 3]), [2, 3, 2]
)
bernoulli = paddle.distributions.Bernoulli(paddle.to_tensor([0.3, 0.7]))
self._check_shape(bernoulli.sample(sample_shape=[2, 3]), [2, 3, 2])
binomial = paddle.distributions.Binomial(
10, paddle.to_tensor([0.3, 0.7])
)
self._check_shape(binomial.sample(sample_shape=[2, 3]), [2, 3, 2])
categorical = paddle.distributions.Categorical(
paddle.to_tensor([0.2, 0.3, 0.5])
)
self._check_shape(categorical.sample(sample_shape=[2, 3]), [2, 3])
dirichlet = paddle.distributions.Dirichlet(
paddle.to_tensor([1.0, 2.0, 3.0])
)
self._check_shape(dirichlet.sample(sample_shape=[2, 3]), [2, 3, 3])
independent = paddle.distributions.Independent(
paddle.distributions.Normal(
paddle.to_tensor([[0.0, 1.0], [2.0, 3.0]]),
paddle.to_tensor([[1.0, 1.0], [1.0, 1.0]]),
),
1,
)
self._check_shape(independent.sample(sample_shape=[2, 3]), [2, 3, 2, 2])
multivariate_normal = paddle.distributions.MultivariateNormal(
paddle.to_tensor([0.0, 1.0]),
covariance_matrix=paddle.eye(2, dtype="float32"),
)
self._check_shape(
multivariate_normal.sample(sample_shape=[2, 3]), [2, 3, 2]
)
self._check_shape(
multivariate_normal.rsample(sample_shape=[2, 3]), [2, 3, 2]
)
poisson = paddle.distributions.Poisson(paddle.to_tensor([1.0, 2.0]))
self._check_shape(poisson.sample(sample_shape=[2, 3]), [2, 3, 2])
student_t = paddle.distributions.StudentT(
paddle.to_tensor([2.5, 3.5]),
paddle.to_tensor([0.0, 1.0]),
paddle.to_tensor([1.0, 1.5]),
)
self._check_shape(student_t.sample(sample_shape=[2, 3]), [2, 3, 2])
transformed = paddle.distributions.TransformedDistribution(
paddle.distributions.Normal(
paddle.to_tensor([0.0, 1.0]),
paddle.to_tensor([1.0, 2.0]),
),
[
paddle.distributions.AffineTransform(
loc=paddle.to_tensor(1.0), scale=paddle.to_tensor(2.0)
)
],
)
self._check_shape(transformed.sample(sample_shape=[2, 3]), [2, 3, 2])
self._check_shape(transformed.rsample(sample_shape=[2, 3]), [2, 3, 2])
uniform = paddle.distributions.Uniform(
paddle.to_tensor([0.0, 1.0]), paddle.to_tensor([1.0, 2.0])
)
self._check_shape(uniform.sample(sample_shape=[2, 3]), [2, 3, 2])
paddle.enable_static()
class TestTensorTransposeInplaceAPI(unittest.TestCase):
def setUp(self):
self.np_x = np.arange(24).reshape(2, 3, 4).astype("float32")
def _check_output(self, out, expected):
np.testing.assert_allclose(out.numpy(), expected)
self.assertEqual(tuple(out.shape), expected.shape)
def test_dygraph_Compatibility(self):
if paddle.is_compiled_with_xpu():
self.skipTest("transpose_ is not supported on XPU")
paddle.disable_static()
expected_swap_01 = np.transpose(self.np_x, (1, 0, 2))
expected_swap_n10 = np.transpose(self.np_x, (2, 1, 0))
# 1. Paddle Positional arguments
x1 = paddle.to_tensor(self.np_x)
out1 = x1.transpose_([1, 0, 2])
# 2. Paddle keyword arguments
x2 = paddle.to_tensor(self.np_x)
out2 = x2.transpose_(perm=[1, 0, 2])
# 3. PyTorch Positional arguments
x3 = paddle.to_tensor(self.np_x)
out3 = x3.transpose_(0, 1)
# 4. PyTorch keyword arguments
x4 = paddle.to_tensor(self.np_x)
out4 = x4.transpose_(dim0=0, dim1=1)
# 5. Mixed arguments
x5 = paddle.to_tensor(self.np_x)
out5 = x5.transpose_(0, dim1=1)
# 6. PyTorch keyword arguments out of order
x6 = paddle.to_tensor(self.np_x)
out6 = x6.transpose_(dim1=1, dim0=0)
# 7. PyTorch negative dim arguments
x7 = paddle.to_tensor(self.np_x)
out7 = x7.transpose_(-1, 0)
# 8. PyTorch same dim arguments
x8 = paddle.to_tensor(self.np_x)
out8 = x8.transpose_(1, 1)
for out in [out1, out2, out3, out4, out5, out6]:
self._check_output(out, expected_swap_01)
self._check_output(out7, expected_swap_n10)
self._check_output(out8, self.np_x)
paddle.enable_static()
class TestTensorReshapeAsAPI(unittest.TestCase):
def setUp(self):
np.random.seed(2025)
self.np_x = np.arange(24).astype("float32")
self.np_other = np.random.rand(2, 3, 4).astype("float64")
self.expected = self.np_x.reshape(self.np_other.shape)
def test_dygraph_Compatibility(self):
paddle.disable_static()
x = paddle.to_tensor(self.np_x)
other = paddle.to_tensor(self.np_other)
# 1. Tensor method - args
out1 = x.reshape_as(other)
# 2. Tensor method - kwargs
out2 = x.reshape_as(other=other)
for out in [out1, out2]:
np.testing.assert_allclose(out.numpy(), self.expected)
paddle.enable_static()
def test_static_Compatibility(self):
paddle.enable_static()
main = paddle.static.Program()
startup = paddle.static.Program()
with paddle.static.program_guard(main, startup):
x = paddle.static.data(
name="x", shape=self.np_x.shape, dtype=str(self.np_x.dtype)
)
other = paddle.static.data(
name="other",
shape=self.np_other.shape,
dtype=str(self.np_other.dtype),
)
# 1. Tensor method - args
out1 = x.reshape_as(other)
# 2. Tensor method - kwargs
out2 = x.reshape_as(other=other)
exe = paddle.static.Executor()
fetches = exe.run(
main,
feed={"x": self.np_x, "other": self.np_other},
fetch_list=[out1, out2],
)
for out in fetches:
np.testing.assert_allclose(out, self.expected)
if __name__ == "__main__":
unittest.main()