• 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
20import mindspore.nn as nn
21import mindspore as ms
22from mindspore import Tensor
23from mindspore.ops import operations as P
24from mindspore.ops import composite as C
25
26
27class L2LossNet(nn.Cell):
28    def __init__(self):
29        super(L2LossNet, self).__init__()
30        self.l2_loss = P.L2Loss()
31
32    def construct(self, x):
33        return self.l2_loss(x)
34
35@pytest.mark.level0
36@pytest.mark.platform_x86_cpu
37@pytest.mark.env_onecard
38def test_l2loss_pynative_fp32_2x2():
39    context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU")
40    error = 1e-4
41    x = Tensor(np.array([[1., 2.], [3., 4.]]), ms.float32)
42    expect = np.array(15, np.float32)
43    output = P.L2Loss()(x)
44    diff = output.asnumpy() - expect
45    assert np.all(diff < error)
46
47@pytest.mark.level0
48@pytest.mark.platform_x86_cpu
49@pytest.mark.env_onecard
50def test_l2loss_pynative_fp16_2x2():
51    context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU")
52    error = 1e-4
53    x = Tensor(np.array([[1., 2.], [3., 4.]]), ms.float16)
54    expect = np.array(15, np.float16)
55    output = P.L2Loss()(x)
56    diff = output.asnumpy() - expect
57    assert np.all(diff < error)
58
59@pytest.mark.level0
60@pytest.mark.platform_x86_cpu
61@pytest.mark.env_onecard
62def test_l2loss_pynative_fp32_1x4():
63    context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU")
64    error = 1e-4
65    x = Tensor(np.array([1., 2., 3., 4.]), ms.float32)
66    expect = np.array(15, np.float32)
67    output = P.L2Loss()(x)
68    diff = output.asnumpy() - expect
69    assert np.all(diff < error)
70
71@pytest.mark.level0
72@pytest.mark.platform_x86_cpu
73@pytest.mark.env_onecard
74def test_l2loss_pynative_fp16_1x4():
75    context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU")
76    error = 1e-4
77    x = Tensor(np.array([1., 2., 3., 4.]), ms.float16)
78    expect = np.array(15, np.float16)
79    output = P.L2Loss()(x)
80    diff = output.asnumpy() - expect
81    assert np.all(diff < error)
82
83@pytest.mark.level0
84@pytest.mark.platform_x86_cpu
85@pytest.mark.env_onecard
86def test_l2loss_graph_fp32_1x4():
87    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
88    error = 1e-4
89    x = Tensor(np.array([1., 2., 3., 4.]), ms.float32)
90    expect = np.array(15, np.float32)
91    l2_loss = L2LossNet()
92    output = l2_loss(x)
93    diff = output.asnumpy() - expect
94    assert np.all(diff < error)
95
96@pytest.mark.level0
97@pytest.mark.platform_x86_cpu
98@pytest.mark.env_onecard
99def test_l2loss_graph_fp16_1x4():
100    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
101    error = 1e-4
102    x = Tensor(np.array([1., 2., 3., 4.]), ms.float16)
103    expect = np.array(15, np.float16)
104    l2_loss = L2LossNet()
105    output = l2_loss(x)
106    diff = output.asnumpy() - expect
107    assert np.all(diff < error)
108
109class GradNet(nn.Cell):
110    def __init__(self, net):
111        super(GradNet, self).__init__()
112        self.net = net
113        self.grad_op = C.GradOperation(get_all=True)
114
115    def construct(self, x):
116        gradient_function = self.grad_op(self.net)
117        return gradient_function(x)
118
119@pytest.mark.level0
120@pytest.mark.platform_x86_cpu
121@pytest.mark.env_onecard
122def test_l2loss_grad_fp32():
123    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
124    x = Tensor(np.array([2.4, 3.2, 1.2, 5.9, 9.]).astype(np.float32))
125    error = 1e-4
126    net = L2LossNet()
127    output = GradNet(net)(x)[0]
128    expect = x
129    diff = output.asnumpy() - expect
130    assert np.all(diff < error)
131
132@pytest.mark.level0
133@pytest.mark.platform_x86_cpu
134@pytest.mark.env_onecard
135def test_l2loss_grad_fp16():
136    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
137    x = Tensor(np.array([[2.4, 3.2, 4.8], [1.2, 5.9, 9.]]).astype(np.float16))
138    error = 1e-4
139    net = L2LossNet()
140    output = GradNet(net)(x)[0]
141    expect = x
142    diff = output.asnumpy() - expect
143    assert np.all(diff < error)
144