# Copyright (c) 2020 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_place, is_custom_device import paddle import paddle.nn.functional as F from paddle import base def p_normalize(x, axis=1, p=2, epsilon=1e-12, keepdims=True): xp = np.power(np.abs(x), p) s = np.sum(xp, axis=axis, keepdims=keepdims) r = np.maximum(np.power(s, 1.0 / p), epsilon) return x / r class TestNNFunctionalNormalize(unittest.TestCase): def setUp(self): self.input_np = np.random.random(size=(10, 10)).astype(np.float32) self.input_np2 = np.array([0.0, 0.0]).astype(np.float32) self.expected0 = p_normalize(self.input_np) self.expected1 = p_normalize(self.input_np, p=1.5) self.expected2 = p_normalize(self.input_np, axis=0) self.expected3 = p_normalize(self.input_np2, axis=0) def run_imperative(self): x = paddle.to_tensor(self.input_np) y = F.normalize(x) np.testing.assert_allclose(y.numpy(), self.expected0, rtol=1e-05) y = F.normalize(x, p=1.5) np.testing.assert_allclose(y.numpy(), self.expected1, rtol=1e-05) y = F.normalize(x, axis=0) np.testing.assert_allclose(y.numpy(), self.expected2, rtol=1e-05) x = paddle.to_tensor(self.input_np2) y = F.normalize(x, axis=0) np.testing.assert_allclose(y.numpy(), self.expected3, rtol=1e-05) self.assertRaisesRegex( ValueError, r"Attr\(axis\) value should be in range \[-R, R-1\]", F.normalize, x, ) def run_static(self, use_gpu=False): x = paddle.static.data(name='input', shape=[10, 10], dtype='float32') x2 = paddle.static.data(name='input2', shape=[2], dtype='float32') result0 = F.normalize(x) result1 = F.normalize(x, p=1.5) result2 = F.normalize(x, axis=0) result3 = F.normalize(x, name='aaa') result4 = F.normalize(x2, axis=0) place = get_device_place() if use_gpu else base.CPUPlace() exe = base.Executor(place) exe.run(paddle.static.default_startup_program()) static_result = exe.run( feed={"input": self.input_np, "input2": self.input_np2}, fetch_list=[result0, result1, result2, result4], ) np.testing.assert_allclose(static_result[0], self.expected0, rtol=1e-05) np.testing.assert_allclose(static_result[1], self.expected1, rtol=1e-05) np.testing.assert_allclose(static_result[2], self.expected2, rtol=1e-05) np.testing.assert_allclose(static_result[3], self.expected3, rtol=1e-05) self.assertRaises(ValueError, F.normalize, x2) def test_cpu(self): paddle.disable_static(place=paddle.base.CPUPlace()) self.run_imperative() paddle.enable_static() with paddle.static.program_guard(paddle.static.Program()): self.run_static() def test_gpu(self): if not (base.core.is_compiled_with_cuda() or is_custom_device()): return paddle.disable_static(place=get_device_place()) self.run_imperative() paddle.enable_static() with paddle.static.program_guard(paddle.static.Program()): self.run_static(use_gpu=True) class TestNormalizeAPI_Compatibility(unittest.TestCase): def setUp(self): np.random.seed(2025) self.places = ['cpu', get_device_place()] self.shape = [2, 3, 4] self.dtype = "float32" self.init_data() def init_data(self): self.np_x = np.random.rand(*self.shape).astype(self.dtype) self.p = 2 self.axis = 1 self.epsilon = 1e-12 def test_dygraph_Compatibility(self): paddle.disable_static() x = paddle.to_tensor(self.np_x) paddle_dygraph_out = [] # Position args (args) out1 = paddle.nn.functional.normalize( x, self.p, self.axis, self.epsilon ) paddle_dygraph_out.append(out1) # Key words args (kwargs) for paddle out2 = paddle.nn.functional.normalize( x=x, p=self.p, axis=self.axis, epsilon=self.epsilon ) paddle_dygraph_out.append(out2) # Key words args for torch compatibility out3 = paddle.nn.functional.normalize( input=x, p=self.p, dim=self.axis, eps=self.epsilon ) paddle_dygraph_out.append(out3) # Key words args for out out4 = paddle.zeros_like(x) paddle.nn.functional.normalize( x, self.p, self.axis, self.epsilon, out=out4 ) paddle_dygraph_out.append(out4) # Numpy reference output ref_out = self.np_x / np.maximum( np.linalg.norm( self.np_x, ord=self.p, axis=self.axis, keepdims=True ), self.epsilon, ) for out in paddle_dygraph_out: np.testing.assert_allclose( ref_out, out.numpy(), rtol=1e-05, atol=1e-08 ) paddle.enable_static() def test_static_Compatibility(self): paddle.enable_static() main = paddle.static.Program() startup = paddle.static.Program() with paddle.base.program_guard(main, startup): x = paddle.static.data(name="x", shape=self.shape, dtype=self.dtype) # Position args (args) out1 = paddle.nn.functional.normalize( x, self.p, self.axis, self.epsilon ) # Key words args (kwargs) for paddle out2 = paddle.nn.functional.normalize( x=x, p=self.p, axis=self.axis, epsilon=self.epsilon ) # Key words args for torch compatibility out3 = paddle.nn.functional.normalize( input=x, p=self.p, dim=self.axis, eps=self.epsilon ) # Numpy reference output ref_out = self.np_x / np.maximum( np.linalg.norm( self.np_x, ord=self.p, axis=self.axis, keepdims=True ), self.epsilon, ) fetch_list = [out1, out2, out3] for place in self.places: exe = paddle.base.Executor(place) fetches = exe.run( main, feed={"x": self.np_x}, fetch_list=fetch_list, ) for out in fetches: np.testing.assert_allclose( out, ref_out, rtol=1e-05, atol=1e-08 ) if __name__ == "__main__": unittest.main()