2020-03-27 14:49:12 +08:00
|
|
|
# Copyright 2020 Huawei Technologies Co., Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
# ============================================================================
|
|
|
|
"""
|
|
|
|
test assign add
|
|
|
|
"""
|
|
|
|
import numpy as np
|
2020-05-13 11:30:27 +08:00
|
|
|
|
|
|
|
import mindspore as ms
|
|
|
|
import mindspore.context as context
|
2020-03-27 14:49:12 +08:00
|
|
|
import mindspore.nn as nn
|
|
|
|
from mindspore import Tensor, Parameter
|
2020-05-13 11:30:27 +08:00
|
|
|
from mindspore.common.initializer import initializer
|
|
|
|
from mindspore.ops import operations as P
|
2020-03-27 14:49:12 +08:00
|
|
|
from ..ut_filter import non_graph_engine
|
2020-05-13 11:30:27 +08:00
|
|
|
|
2020-03-27 14:49:12 +08:00
|
|
|
context.set_context(mode=context.GRAPH_MODE)
|
|
|
|
|
2020-05-13 11:30:27 +08:00
|
|
|
|
2020-03-27 14:49:12 +08:00
|
|
|
class Net(nn.Cell):
|
|
|
|
"""Net definition"""
|
2020-05-13 11:30:27 +08:00
|
|
|
|
2020-03-27 14:49:12 +08:00
|
|
|
def __init__(self):
|
|
|
|
super(Net, self).__init__()
|
|
|
|
self.AssignAdd = P.AssignAdd()
|
|
|
|
self.inputdata = Parameter(initializer(1, [1], ms.int64), name="global_step")
|
|
|
|
print("inputdata: ", self.inputdata)
|
|
|
|
|
|
|
|
def construct(self, x):
|
|
|
|
out = self.AssignAdd(self.inputdata, x)
|
|
|
|
return out
|
|
|
|
|
2020-05-13 11:30:27 +08:00
|
|
|
|
2020-03-27 14:49:12 +08:00
|
|
|
@non_graph_engine
|
|
|
|
def test_AssignAdd_1():
|
|
|
|
"""test AssignAdd 1"""
|
|
|
|
import mindspore.context as context
|
|
|
|
context.set_context(mode=context.GRAPH_MODE)
|
|
|
|
net = Net()
|
2020-05-13 11:30:27 +08:00
|
|
|
x = Tensor(np.ones([1]).astype(np.int64) * 100)
|
2020-03-27 14:49:12 +08:00
|
|
|
|
|
|
|
print("MyPrintResult dataX:", x)
|
|
|
|
result = net(x)
|
|
|
|
print("MyPrintResult data::", result)
|
2020-05-13 11:30:27 +08:00
|
|
|
expect = np.ones([1]).astype(np.int64) * 101
|
2020-03-27 14:49:12 +08:00
|
|
|
diff = result.asnumpy() - expect
|
|
|
|
|
|
|
|
print("MyPrintExpect:", expect)
|
|
|
|
print("MyPrintDiff:", diff)
|
|
|
|
error = np.ones(shape=[1]) * 1.0e-3
|
|
|
|
assert np.all(diff < error)
|
|
|
|
|
2020-05-13 11:30:27 +08:00
|
|
|
|
2020-03-27 14:49:12 +08:00
|
|
|
@non_graph_engine
|
|
|
|
def test_AssignAdd_2():
|
|
|
|
"""test AssignAdd 2"""
|
|
|
|
import mindspore.context as context
|
|
|
|
context.set_context(mode=context.GRAPH_MODE)
|
|
|
|
net = Net()
|
2020-05-13 11:30:27 +08:00
|
|
|
x = Tensor(np.ones([1]).astype(np.int64) * 102)
|
2020-03-27 14:49:12 +08:00
|
|
|
|
|
|
|
print("MyPrintResult dataX:", x)
|
|
|
|
result = net(x)
|
|
|
|
print("MyPrintResult data::", result.asnumpy())
|
2020-05-13 11:30:27 +08:00
|
|
|
expect = np.ones([1]).astype(np.int64) * 103
|
2020-03-27 14:49:12 +08:00
|
|
|
diff = result.asnumpy() - expect
|
|
|
|
|
|
|
|
print("MyPrintExpect:", expect)
|
|
|
|
print("MyPrintDiff:", diff)
|
|
|
|
error = np.ones(shape=[1]) * 1.0e-3
|
|
|
|
assert np.all(diff < error)
|
|
|
|
|
2020-05-13 11:30:27 +08:00
|
|
|
|
2020-03-27 14:49:12 +08:00
|
|
|
class AssignAddNet(nn.Cell):
|
|
|
|
"""Net definition"""
|
2020-05-13 11:30:27 +08:00
|
|
|
|
2020-03-27 14:49:12 +08:00
|
|
|
def __init__(self):
|
|
|
|
super(AssignAddNet, self).__init__()
|
|
|
|
self.AssignAdd = P.AssignAdd()
|
|
|
|
self.inputdata = Parameter(initializer(1, [1], ms.float16), name="KIND_AUTOCAST_SCALAR_TO_TENSOR")
|
|
|
|
self.one = 1
|
|
|
|
|
|
|
|
def construct(self, ixt):
|
|
|
|
z1 = self.AssignAdd(self.inputdata, self.one)
|
|
|
|
return z1
|
|
|
|
|
2020-05-13 11:30:27 +08:00
|
|
|
|
2020-03-27 14:49:12 +08:00
|
|
|
@non_graph_engine
|
|
|
|
def test_assignadd_scalar_cast():
|
|
|
|
net = AssignAddNet()
|
2020-05-13 11:30:27 +08:00
|
|
|
x = Tensor(np.ones([1]).astype(np.int64) * 102)
|
|
|
|
# _executor.compile(net, 1)
|
2020-03-27 14:49:12 +08:00
|
|
|
result = net(x)
|