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.2 kB

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