• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15
16import numpy as np
17import pytest
18
19import mindspore.context as context
20from mindspore.common.tensor import Tensor
21from mindspore import  dtype as mstype
22from mindspore.nn import Cell
23from mindspore.ops import operations as P
24context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
25
26
27class Net(Cell):
28    def __init__(self, axis=0, epsilon=1e-4):
29        super(Net, self).__init__()
30        self.norm = P.L2Normalize(axis=axis, epsilon=epsilon)
31
32    def construct(self, x):
33        return self.norm(x)
34
35
36@pytest.mark.level0
37@pytest.mark.platform_x86_cpu
38@pytest.mark.env_onecard
39def test_l2normalize_float32():
40    x = np.arange(20*20*20*20).astype(np.float32).reshape(20, 20, 20, 20)
41    expect = x / np.sqrt(np.sum(x**2, axis=0, keepdims=True))
42    x = Tensor(x)
43    error = np.ones(shape=[20, 20, 20, 20]) * 1.0e-5
44
45    norm_op = Net(axis=0)
46    output = norm_op(x)
47    diff = output.asnumpy() - expect
48    assert np.all(diff < error)
49    assert np.all(-diff < error)
50
51
52@pytest.mark.level0
53@pytest.mark.platform_x86_cpu
54@pytest.mark.env_onecard
55def test_l2normalize_float16():
56    x = np.arange(96).astype(np.float16).reshape(2, 3, 4, 4)
57    expect = x / np.sqrt(np.sum(x**2, axis=0, keepdims=True))
58    x = Tensor(x, dtype=mstype.float16)
59    error = np.ones(shape=[2, 3, 4, 4]) * 1.0e-3
60
61    norm_op = Net(axis=0)
62    output = norm_op(x)
63    diff = output.asnumpy() - expect
64    assert np.all(diff < error)
65    assert np.all(-diff < error)
66
67
68@pytest.mark.level0
69@pytest.mark.platform_x86_cpu
70@pytest.mark.env_onecard
71def test_l2normalize_axis():
72    axis = -2
73    x = np.arange(96).astype(np.float32).reshape(2, 3, 4, 4)
74    expect = x / np.sqrt(np.sum(x**2, axis=axis, keepdims=True))
75    x = Tensor(x)
76    error = np.ones(shape=[2, 3, 4, 4]) * 1.0e-5
77
78    norm_op = Net(axis=axis)
79    output = norm_op(x)
80    diff = output.asnumpy() - expect
81    assert np.all(diff < error)
82    assert np.all(-diff < error)
83
84
85@pytest.mark.level0
86@pytest.mark.platform_x86_cpu
87@pytest.mark.env_onecard
88def test_l2normalize_epsilon():
89    axis = -1
90    epsilon = 900000.0
91    x = np.arange(96).astype(np.float32).reshape(2, 3, 4, 4)
92    expect = x / np.sqrt(epsilon)
93    x = Tensor(x)
94    error = np.ones(shape=[2, 3, 4, 4]) * 1.0e-5
95
96    norm_op = Net(axis=axis, epsilon=epsilon)
97    output = norm_op(x)
98    diff = output.asnumpy() - expect
99    assert np.all(diff < error)
100    assert np.all(-diff < error)
101