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""" 16test assign add 17""" 18import numpy as np 19 20import mindspore as ms 21import mindspore.context as context 22import mindspore.nn as nn 23from mindspore import Tensor, Parameter 24from mindspore.common.initializer import initializer 25from mindspore.ops import operations as P 26from ..ut_filter import non_graph_engine 27 28context.set_context(mode=context.GRAPH_MODE) 29 30 31class Net(nn.Cell): 32 """Net definition""" 33 34 def __init__(self): 35 super(Net, self).__init__() 36 self.AssignAdd = P.AssignAdd() 37 self.inputdata = Parameter(initializer(1, [1], ms.int64), name="global_step") 38 print("inputdata: ", self.inputdata) 39 40 def construct(self, x): 41 out = self.AssignAdd(self.inputdata, x) 42 return out 43 44 45@non_graph_engine 46def test_AssignAdd_1(): 47 """test AssignAdd 1""" 48 context.set_context(mode=context.GRAPH_MODE) 49 net = Net() 50 x = Tensor(np.ones([1]).astype(np.int64) * 100) 51 52 print("MyPrintResult dataX:", x) 53 result = net(x) 54 print("MyPrintResult data::", result) 55 expect = np.ones([1]).astype(np.int64) * 101 56 diff = result.asnumpy() - expect 57 58 print("MyPrintExpect:", expect) 59 print("MyPrintDiff:", diff) 60 error = np.ones(shape=[1]) * 1.0e-3 61 assert np.all(diff < error) 62 63 64@non_graph_engine 65def test_AssignAdd_2(): 66 """test AssignAdd 2""" 67 context.set_context(mode=context.GRAPH_MODE) 68 net = Net() 69 x = Tensor(np.ones([1]).astype(np.int64) * 102) 70 71 print("MyPrintResult dataX:", x) 72 result = net(x) 73 print("MyPrintResult data::", result.asnumpy()) 74 expect = np.ones([1]).astype(np.int64) * 103 75 diff = result.asnumpy() - expect 76 77 print("MyPrintExpect:", expect) 78 print("MyPrintDiff:", diff) 79 error = np.ones(shape=[1]) * 1.0e-3 80 assert np.all(diff < error) 81 82 83class AssignAddNet(nn.Cell): 84 """Net definition""" 85 86 def __init__(self): 87 super(AssignAddNet, self).__init__() 88 self.AssignAdd = P.AssignAdd() 89 self.inputdata = Parameter(initializer(1, [1], ms.float16), name="KIND_AUTOCAST_SCALAR_TO_TENSOR") 90 self.one = 1 91 92 def construct(self, ixt): 93 z1 = self.AssignAdd(self.inputdata, self.one) 94 return z1 95 96 97@non_graph_engine 98def test_assignadd_scalar_cast(): 99 net = AssignAddNet() 100 x = Tensor(np.ones([1]).astype(np.int64) * 102) 101 # _executor.compile(net, 1) 102 _ = net(x) 103