• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 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
24
25
26class L2LossNet(nn.Cell):
27    def __init__(self):
28        super(L2LossNet, self).__init__()
29        self.l2_loss = P.L2Loss()
30
31    def construct(self, x):
32        return self.l2_loss(x)
33
34@pytest.mark.level0
35@pytest.mark.platform_x86_gpu_training
36@pytest.mark.env_onecard
37def test_gather_pynative_fp32_22():
38    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
39    error = 1e-4
40    x = Tensor(np.array([[1., 2.], [3., 4.]]), ms.float32)
41    expect = np.array(15, np.float32)
42    output = P.L2Loss()(x)
43    diff = output.asnumpy() - expect
44    assert np.all(diff < error)
45
46@pytest.mark.level0
47@pytest.mark.platform_x86_gpu_training
48@pytest.mark.env_onecard
49def test_gather_pynative_fp16_22():
50    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
51    error = 1e-4
52    x = Tensor(np.array([[1., 2.], [3., 4.]]), ms.float16)
53    expect = np.array(15, np.float16)
54    output = P.L2Loss()(x)
55    diff = output.asnumpy() - expect
56    assert np.all(diff < error)
57
58@pytest.mark.level0
59@pytest.mark.platform_x86_gpu_training
60@pytest.mark.env_onecard
61def test_gather_pynative_fp32_14():
62    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
63    error = 1e-4
64    x = Tensor(np.array([1., 2., 3., 4.]), ms.float32)
65    expect = np.array(15, np.float32)
66    output = P.L2Loss()(x)
67    diff = output.asnumpy() - expect
68    assert np.all(diff < error)
69
70@pytest.mark.level0
71@pytest.mark.platform_x86_gpu_training
72@pytest.mark.env_onecard
73def test_gather_pynative_fp16_14():
74    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
75    error = 1e-4
76    x = Tensor(np.array([1., 2., 3., 4.]), ms.float16)
77    expect = np.array(15, np.float16)
78    output = P.L2Loss()(x)
79    diff = output.asnumpy() - expect
80    assert np.all(diff < error)
81
82def test_gather_graph_fp32_14():
83    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
84    error = 1e-4
85    x = Tensor(np.array([1., 2., 3., 4.]), ms.float32)
86    expect = np.array(15, np.float32)
87    l2_loss = L2LossNet()
88    output = l2_loss(x)
89    diff = output.asnumpy() - expect
90    assert np.all(diff < error)
91
92def test_gather_graph_fp16_14():
93    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
94    error = 1e-4
95    x = Tensor(np.array([1., 2., 3., 4.]), ms.float16)
96    expect = np.array(15, np.float16)
97    l2_loss = L2LossNet()
98    output = l2_loss(x)
99    diff = output.asnumpy() - expect
100    assert np.all(diff < error)
101