You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_AssignAdd.py 3.1 kB

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