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""" test numpy ops """ 16import pytest 17import numpy as np 18 19import mindspore.nn as nn 20from mindspore import Tensor, ms_function, context 21from mindspore.ops import operations as P 22from mindspore.ops import functional as F 23import mindspore.common.dtype as mstype 24import mindspore.common._monad as monad 25 26context.set_context(mode=context.GRAPH_MODE) 27 28# `add_func` is defined in current file. 29def add_func(x, y): 30 return x + y 31 32@ms_function 33def do_increment(i): 34 add_1 = F.partial(add_func, 1) 35 return add_1(i) 36 37def test_increment(): 38 a = do_increment(9) 39 assert a == 10 40 41 42@ms_function 43def use_monad(x, y): 44 res = P.Mul()(x, y) 45 res = F.depend(res, monad.U) 46 return res 47 48def test_use_monad(): 49 x = Tensor(1.0, mstype.float32) 50 y = Tensor(1.0, mstype.float32) 51 print(use_monad(x, y)) 52 53 54class Net(nn.Cell): 55 def __init__(self): 56 super(Net, self).__init__() 57 self.x = Tensor([2, 3, 4]) 58 59 def construct(self): 60 x_len = len(self.x) 61 for i in range(x_len): 62 print(i) 63 return x_len 64 65def test_builtins_len(): 66 net = Net() 67 net() 68 69 70@ms_function 71def np_fallback_func(): 72 array_x = tuple([2, 3, 4, 5]) 73 np_x = np.array(array_x).astype(np.float32) 74 me_x = Tensor(np_x) 75 me_x = me_x + me_x 76 return me_x 77 78@pytest.mark.skip(reason='Not support graph fallback feature yet') 79def test_np_fallback_func(): 80 print(np_fallback_func()) 81 82 83@ms_function 84def div_mod_func(x, y): 85 a = divmod(x, y) 86 return Tensor(a) 87 88@pytest.mark.skip(reason='Not support graph fallback feature yet') 89def test_div_mod_func(): 90 print(div_mod_func(8, 3)) # (2, 2) 91 92 93# NameError: name 'Tensor' is not defined. 94@ms_function 95def select_func(cond, x, y): 96 if isinstance(cond, (tuple, list)): 97 output = y 98 elif isinstance(cond, Tensor): 99 output = F.select(cond, x, y) 100 else: 101 output = x 102 return output 103 104def test_select_func(): 105 cond = Tensor([True, False]) 106 x = Tensor([2, 3], mstype.float32) 107 y = Tensor([1, 2], mstype.float32) 108 print(select_func(cond, x, y)) 109 110 111# Not interpret 'Tensor'. 112@ms_function 113def select_func2(cond, x, y): 114 if isinstance(cond, (tuple, list)): 115 output = y 116 if isinstance(cond, Tensor): 117 output = F.select(cond, x, y) 118 else: 119 output = x 120 return output 121 122def test_select_func2(): 123 cond = Tensor([True, False]) 124 x = Tensor([2, 3], mstype.float32) 125 y = Tensor([1, 2], mstype.float32) 126 print(select_func2(cond, x, y)) 127 128 129# NameError: name 'Tensor' is not defined. 130@ms_function 131def slice_func(a, b): 132 a[1:3, ::] = b 133 return a 134 135def test_slice_func(): 136 a = Tensor(np.arange(60).reshape(3, 4, 5), dtype=mstype.float32) 137 b = Tensor([1], dtype=mstype.float32) 138 print(slice_func(a, b)) 139