• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020-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 import Tensor
21from mindspore.ops import operations as P
22
23def sqrt(nptype):
24    np.random.seed(0)
25    x_np = np.random.rand(2, 3, 4, 4).astype(nptype)
26
27    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
28    output_ms = P.Sqrt()(Tensor(x_np))
29    output_np = np.sqrt(x_np)
30    assert np.allclose(output_ms.asnumpy(), output_np)
31
32@pytest.mark.level1
33@pytest.mark.platform_x86_gpu_training
34@pytest.mark.env_onecard
35def test_sqrt_float16():
36    sqrt(np.float16)
37
38@pytest.mark.level0
39@pytest.mark.platform_x86_gpu_training
40@pytest.mark.env_onecard
41def test_sqrt_float32():
42    sqrt(np.float32)
43
44@pytest.mark.level0
45@pytest.mark.platform_x86_gpu_training
46@pytest.mark.env_onecard
47def test_sqrt_float64():
48    sqrt(np.float64)
49
50@pytest.mark.level0
51@pytest.mark.platform_x86_gpu_training
52@pytest.mark.env_onecard
53def test_rsqrt():
54    np.random.seed(0)
55    x_np = np.random.rand(2, 3, 4, 4).astype(np.float32)
56
57    output_ms = P.Rsqrt()(Tensor(x_np))
58    output_np = 1 / np.sqrt(x_np)
59    assert np.allclose(output_ms.asnumpy(), output_np)
60