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.common.dtype as mstype 20import mindspore.context as context 21from mindspore.common.tensor import Tensor 22from mindspore.nn import Cell 23from mindspore.ops import operations as P 24 25class LinSpaceNet(Cell): 26 def __init__(self, num): 27 super(LinSpaceNet, self).__init__() 28 self.ls_op = P.LinSpace() 29 self.num = num 30 31 def construct(self, start, stop): 32 output = self.ls_op(start, stop, self.num) 33 return output 34 35 36@pytest.mark.level0 37@pytest.mark.platform_x86_gpu_training 38@pytest.mark.env_onecard 39def test_lin_space_1(): 40 context.set_context(mode=context.GRAPH_MODE, device_target='GPU') 41 start_np = 5 42 stop_np = 150 43 num_np = 12 44 start = Tensor(start_np, dtype=mstype.float32) 45 stop = Tensor(stop_np, dtype=mstype.float32) 46 num = num_np 47 ls_op = P.LinSpace() 48 result_ms = ls_op(start, stop, num).asnumpy() 49 result_np = np.linspace(start_np, stop_np, num_np) 50 assert np.allclose(result_ms, result_np) 51 52 53@pytest.mark.level0 54@pytest.mark.platform_x86_gpu_training 55@pytest.mark.env_onecard 56def test_lin_shape_2(): 57 context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU') 58 start_np = -25 59 stop_np = 147 60 num_np = 10 61 start = Tensor(start_np, dtype=mstype.float32) 62 stop = Tensor(stop_np, dtype=mstype.float32) 63 num = num_np 64 ls_op = P.LinSpace() 65 result_ms = ls_op(start, stop, num).asnumpy() 66 result_np = np.linspace(start_np, stop_np, num_np) 67 assert np.allclose(result_ms, result_np) 68 69 70@pytest.mark.level0 71@pytest.mark.platform_x86_gpu_training 72@pytest.mark.env_onecard 73def test_lin_shape_3(): 74 context.set_context(mode=context.GRAPH_MODE, device_target='GPU') 75 start_np = 25 76 stop_np = -147 77 num_np = 20 78 start = Tensor(start_np, dtype=mstype.float32) 79 stop = Tensor(stop_np, dtype=mstype.float32) 80 net = LinSpaceNet(num_np) 81 result_ms = net(start, stop).asnumpy() 82 result_np = np.linspace(start_np, stop_np, num_np) 83 assert np.allclose(result_ms, result_np) 84 85 86@pytest.mark.level0 87@pytest.mark.platform_x86_gpu_training 88@pytest.mark.env_onecard 89def test_lin_shape_4(): 90 context.set_context(mode=context.GRAPH_MODE, device_target='GPU') 91 start_np = -25.3 92 stop_np = -147 93 num_np = 36 94 start = Tensor(start_np, dtype=mstype.float32) 95 stop = Tensor(stop_np, dtype=mstype.float32) 96 net = LinSpaceNet(num_np) 97 result_ms = net(start, stop).asnumpy() 98 result_np = np.linspace(start_np, stop_np, num_np) 99 assert np.allclose(result_ms, result_np) 100