Files
2026-07-13 12:40:42 +08:00

331 lines
10 KiB
Python

# Copyright (c) 2019 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 unittest
import numpy as np
from op_test import get_device, get_places
import paddle
class TensorFillDiagonal_Test(unittest.TestCase):
def test_dim2_normal(self):
expected_np = np.array([[1, 2, 2], [2, 1, 2], [2, 2, 1]]).astype(
'float32'
)
expected_grad = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]]).astype(
'float32'
)
typelist = ['float32', 'float64', 'int32', 'int64']
places = get_places()
for idx, p in enumerate(places):
if idx == 0:
paddle.set_device('cpu')
else:
paddle.set_device(get_device())
for dtype in typelist:
x = paddle.ones((3, 3), dtype=dtype)
x.stop_gradient = False
y = x * 2
y.retain_grads()
y.fill_diagonal_(1, offset=0, wrap=True)
loss = y.sum()
loss.backward()
self.assertEqual(
(y.numpy().astype('float32') == expected_np).all(), True
)
self.assertEqual(
(y.grad.numpy().astype('float32') == expected_grad).all(),
True,
)
def test_offset(self):
expected_np = np.array([[2, 2, 1], [2, 2, 2], [2, 2, 2]]).astype(
'float32'
)
expected_grad = np.array([[1, 1, 0], [1, 1, 1], [1, 1, 1]]).astype(
'float32'
)
typelist = ['float32', 'float64', 'int32', 'int64']
places = get_places()
for idx, p in enumerate(places):
if idx == 0:
paddle.set_device('cpu')
else:
paddle.set_device(get_device())
for dtype in typelist:
x = paddle.ones((3, 3), dtype=dtype)
x.stop_gradient = False
y = x * 2
y.retain_grads()
y.fill_diagonal_(1, offset=2, wrap=True)
loss = y.sum()
loss.backward()
self.assertEqual(
(y.numpy().astype('float32') == expected_np).all(), True
)
self.assertEqual(
(y.grad.numpy().astype('float32') == expected_grad).all(),
True,
)
def test_bool(self):
expected_np = np.array(
[[False, True, True], [True, False, True], [True, True, False]]
)
typelist = ['bool']
places = get_places()
for idx, p in enumerate(places):
if idx == 0:
paddle.set_device('cpu')
else:
paddle.set_device(get_device())
for dtype in typelist:
x = paddle.ones((3, 3), dtype=dtype)
x.stop_gradient = True
x.fill_diagonal_(0, offset=0, wrap=True)
self.assertEqual((x.numpy() == expected_np).all(), True)
def test_dim2_unnormal_wrap(self):
expected_np = np.array(
[
[1, 2, 2],
[2, 1, 2],
[2, 2, 1],
[2, 2, 2],
[1, 2, 2],
[2, 1, 2],
[2, 2, 1],
]
).astype('float32')
expected_grad = np.array(
[
[0, 1, 1],
[1, 0, 1],
[1, 1, 0],
[1, 1, 1],
[0, 1, 1],
[1, 0, 1],
[1, 1, 0],
]
).astype('float32')
typelist = ['float32', 'float64', 'int32', 'int64']
places = get_places()
for idx, p in enumerate(places):
if idx == 0:
paddle.set_device('cpu')
else:
paddle.set_device(get_device())
for dtype in typelist:
x = paddle.ones((7, 3), dtype=dtype)
x.stop_gradient = False
y = x * 2
y.retain_grads()
y.fill_diagonal_(1, offset=0, wrap=True)
loss = y.sum()
loss.backward()
self.assertEqual(
(y.numpy().astype('float32') == expected_np).all(), True
)
self.assertEqual(
(y.grad.numpy().astype('float32') == expected_grad).all(),
True,
)
def test_dim2_unnormal_unwrap(self):
expected_np = np.array(
[
[1, 2, 2],
[2, 1, 2],
[2, 2, 1],
[2, 2, 2],
[2, 2, 2],
[2, 2, 2],
[2, 2, 2],
]
).astype('float32')
expected_grad = np.array(
[
[0, 1, 1],
[1, 0, 1],
[1, 1, 0],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
]
).astype('float32')
typelist = ['float32', 'float64', 'int32', 'int64']
places = get_places()
for idx, p in enumerate(places):
if idx == 0:
paddle.set_device('cpu')
else:
paddle.set_device(get_device())
for dtype in typelist:
x = paddle.ones((7, 3), dtype=dtype)
x.stop_gradient = False
y = x * 2
y.retain_grads()
y.fill_diagonal_(1, offset=0, wrap=False)
loss = y.sum()
loss.backward()
self.assertEqual(
(y.numpy().astype('float32') == expected_np).all(), True
)
self.assertEqual(
(y.grad.numpy().astype('float32') == expected_grad).all(),
True,
)
def test_dim_larger2_normal(self):
expected_np = np.array(
[
[[1, 2, 2], [2, 2, 2], [2, 2, 2]],
[[2, 2, 2], [2, 1, 2], [2, 2, 2]],
[[2, 2, 2], [2, 2, 2], [2, 2, 1]],
]
).astype('float32')
expected_grad = np.array(
[
[[0, 1, 1], [1, 1, 1], [1, 1, 1]],
[[1, 1, 1], [1, 0, 1], [1, 1, 1]],
[[1, 1, 1], [1, 1, 1], [1, 1, 0]],
]
).astype('float32')
typelist = ['float32', 'float64', 'int32', 'int64']
places = get_places()
for idx, p in enumerate(places):
if idx == 0:
paddle.set_device('cpu')
else:
paddle.set_device(get_device())
for dtype in typelist:
x = paddle.ones((3, 3, 3), dtype=dtype)
x.stop_gradient = False
y = x * 2
y.retain_grads()
y.fill_diagonal_(1, offset=0, wrap=True)
loss = y.sum()
loss.backward()
self.assertEqual(
(y.numpy().astype('float32') == expected_np).all(), True
)
self.assertEqual(
(y.grad.numpy().astype('float32') == expected_grad).all(),
True,
)
class TensorFillDiagonal_ZeroSize(unittest.TestCase):
def _test_normal(self, shape):
expected_np = np.random.random(shape)
expected_grad = np.random.random(shape)
places = get_places()
for idx, p in enumerate(places):
if idx == 0:
paddle.set_device('cpu')
else:
paddle.set_device(get_device())
x = paddle.ones(shape)
x.stop_gradient = False
y = x * 2
y.retain_grads()
y.fill_diagonal_(1, offset=0, wrap=True)
loss = y.sum()
loss.backward()
self.assertEqual(
(y.numpy().astype('float32') == expected_np).all(), True
)
self.assertEqual(
(y.grad.numpy().astype('float32') == expected_grad).all(),
True,
)
def test_normal(self):
self._test_normal([0, 3])
self._test_normal([0, 0])
class TestFillDiagonalAlias(unittest.TestCase):
def test_alias_fill_value_success(self):
"""
Test case: Verify that 'fill_value' can be used as an alias for 'value'.
"""
# 1. Initialize data
x = paddle.zeros([4, 4], dtype='float32')
# 2. Use the new alias parameter 'fill_value'
# This aligns with PyTorch's API
x.fill_diagonal_(fill_value=5.0)
# 3. Verify results
x_np = x.numpy()
# Check if diagonal elements are updated correctly
for i in range(4):
self.assertEqual(x_np[i, i], 5.0)
# Check if off-diagonal elements remain 0
# (Manually reset diagonal to 0 and check if the whole matrix is 0)
np.fill_diagonal(x_np, 0)
self.assertTrue(np.all(x_np == 0))
def test_alias_conflict(self):
"""
Test case: Verify that providing both 'value' and 'fill_value' raises an error.
To avoid ambiguity, specifying both parameters is prohibited.
"""
x = paddle.zeros([3, 3], dtype='float32')
# Expect TypeError or ValueError when both arguments are provided
with self.assertRaises(ValueError):
x.fill_diagonal_(value=1.0, fill_value=2.0)
def test_positional_args(self):
x = paddle.zeros([4, 4], dtype='float32')
x.fill_diagonal_(5.0, False)
x_np = x.numpy()
for i in range(4):
self.assertEqual(x_np[i, i], 5.0)
np.fill_diagonal(x_np, 0)
self.assertTrue(np.all(x_np == 0))
def test_too_many_positional_args(self):
x = paddle.zeros([3, 3], dtype='float32')
with self.assertRaises(TypeError):
x.fill_diagonal_(1.0, False, 0)
if __name__ == '__main__':
unittest.main()