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_assign_op.py 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. import numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor, Parameter
  20. from mindspore.common.initializer import initializer
  21. from mindspore.ops import operations as P
  22. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  23. class Assign(nn.Cell):
  24. def __init__(self, x, y):
  25. super(Assign, self).__init__()
  26. self.x = Parameter(initializer(x, x.shape), name="x")
  27. self.y = Parameter(initializer(y, y.shape), name="y")
  28. self.assign = P.Assign()
  29. def construct(self):
  30. self.assign(self.y, self.x)
  31. return self.y
  32. @pytest.mark.level0
  33. @pytest.mark.platform_x86_cpu
  34. @pytest.mark.env_onecard
  35. def test_assign_bool():
  36. x = Tensor(np.ones([3, 3]).astype(np.bool_))
  37. y = Tensor(np.zeros([3, 3]).astype(np.bool_))
  38. assign = Assign(x, y)
  39. output = assign()
  40. output = output.asnumpy()
  41. output_expect = np.ones([3, 3]).astype(np.bool_)
  42. print(output)
  43. assert np.all(output == output_expect)
  44. @pytest.mark.level0
  45. @pytest.mark.platform_x86_cpu
  46. @pytest.mark.env_onecard
  47. def test_assign_int8():
  48. x = Tensor(np.ones([3, 3]).astype(np.int8))
  49. y = Tensor(np.zeros([3, 3]).astype(np.int8))
  50. assign = Assign(x, y)
  51. output = assign()
  52. output = output.asnumpy()
  53. output_expect = np.ones([3, 3]).astype(np.int8)
  54. print(output)
  55. assert np.all(output == output_expect)
  56. @pytest.mark.level0
  57. @pytest.mark.platform_x86_cpu
  58. @pytest.mark.env_onecard
  59. def test_assign_uint8():
  60. x = Tensor(np.ones([3, 3]).astype(np.uint8))
  61. y = Tensor(np.zeros([3, 3]).astype(np.uint8))
  62. assign = Assign(x, y)
  63. output = assign()
  64. output = output.asnumpy()
  65. output_expect = np.ones([3, 3]).astype(np.uint8)
  66. print(output)
  67. assert np.all(output == output_expect)
  68. @pytest.mark.level0
  69. @pytest.mark.platform_x86_cpu
  70. @pytest.mark.env_onecard
  71. def test_assign_int16():
  72. x = Tensor(np.ones([3, 3]).astype(np.int16))
  73. y = Tensor(np.zeros([3, 3]).astype(np.int16))
  74. assign = Assign(x, y)
  75. output = assign()
  76. output = output.asnumpy()
  77. output_expect = np.ones([3, 3]).astype(np.int16)
  78. print(output)
  79. assert np.all(output == output_expect)
  80. @pytest.mark.level0
  81. @pytest.mark.platform_x86_cpu
  82. @pytest.mark.env_onecard
  83. def test_assign_uint16():
  84. x = Tensor(np.ones([3, 3]).astype(np.uint16))
  85. y = Tensor(np.zeros([3, 3]).astype(np.uint16))
  86. assign = Assign(x, y)
  87. output = assign()
  88. output = output.asnumpy()
  89. output_expect = np.ones([3, 3]).astype(np.uint16)
  90. print(output)
  91. assert np.all(output == output_expect)
  92. @pytest.mark.level0
  93. @pytest.mark.platform_x86_cpu
  94. @pytest.mark.env_onecard
  95. def test_assign_int32():
  96. x = Tensor(np.ones([3, 3]).astype(np.int32))
  97. y = Tensor(np.zeros([3, 3]).astype(np.int32))
  98. assign = Assign(x, y)
  99. output = assign()
  100. output = output.asnumpy()
  101. output_expect = np.ones([3, 3]).astype(np.int32)
  102. print(output)
  103. assert np.all(output == output_expect)
  104. @pytest.mark.level0
  105. @pytest.mark.platform_x86_cpu
  106. @pytest.mark.env_onecard
  107. def test_assign_uint32():
  108. x = Tensor(np.ones([3, 3]).astype(np.uint32))
  109. y = Tensor(np.zeros([3, 3]).astype(np.uint32))
  110. assign = Assign(x, y)
  111. output = assign()
  112. output = output.asnumpy()
  113. output_expect = np.ones([3, 3]).astype(np.uint32)
  114. print(output)
  115. assert np.all(output == output_expect)
  116. @pytest.mark.level0
  117. @pytest.mark.platform_x86_cpu
  118. @pytest.mark.env_onecard
  119. def test_assign_int64():
  120. x = Tensor(np.ones([3, 3]).astype(np.int64))
  121. y = Tensor(np.zeros([3, 3]).astype(np.int64))
  122. assign = Assign(x, y)
  123. output = assign()
  124. output = output.asnumpy()
  125. output_expect = np.ones([3, 3]).astype(np.int64)
  126. print(output)
  127. assert np.all(output == output_expect)
  128. @pytest.mark.level0
  129. @pytest.mark.platform_x86_cpu
  130. @pytest.mark.env_onecard
  131. def test_assign_uint64():
  132. x = Tensor(np.ones([3, 3]).astype(np.uint64))
  133. y = Tensor(np.zeros([3, 3]).astype(np.uint64))
  134. assign = Assign(x, y)
  135. output = assign()
  136. output = output.asnumpy()
  137. output_expect = np.ones([3, 3]).astype(np.uint64)
  138. print(output)
  139. assert np.all(output == output_expect)
  140. @pytest.mark.level0
  141. @pytest.mark.platform_x86_cpu
  142. @pytest.mark.env_onecard
  143. def test_assign_float16():
  144. x = Tensor(np.array([[0.1, 0.2, 0.3],
  145. [0.4, 0.5, 0.5],
  146. [0.6, 0.7, 0.8]]).astype(np.float16))
  147. y = Tensor(np.array([[0.4, 0.5, 0.5],
  148. [0.6, 0.7, 0.8],
  149. [0.1, 0.2, 0.3]]).astype(np.float16))
  150. assign = Assign(x, y)
  151. output = assign()
  152. output = output.asnumpy()
  153. output_expect = np.array([[0.1, 0.2, 0.3],
  154. [0.4, 0.5, 0.5],
  155. [0.6, 0.7, 0.8]]).astype(np.float16)
  156. print(output)
  157. assert np.all(output - output_expect < 1e-6)
  158. @pytest.mark.level0
  159. @pytest.mark.platform_x86_cpu
  160. @pytest.mark.env_onecard
  161. def test_assign_float32():
  162. x = Tensor(np.array([[0.1, 0.2, 0.3],
  163. [0.4, 0.5, 0.5],
  164. [0.6, 0.7, 0.8]]).astype(np.float32))
  165. y = Tensor(np.array([[0.4, 0.5, 0.5],
  166. [0.6, 0.7, 0.8],
  167. [0.1, 0.2, 0.3]]).astype(np.float32))
  168. assign = Assign(x, y)
  169. output = assign()
  170. output = output.asnumpy()
  171. output_expect = np.array([[0.1, 0.2, 0.3],
  172. [0.4, 0.5, 0.5],
  173. [0.6, 0.7, 0.8]]).astype(np.float32)
  174. print(output)
  175. assert np.all(output - output_expect < 1e-6)
  176. @pytest.mark.level0
  177. @pytest.mark.platform_x86_cpu
  178. @pytest.mark.env_onecard
  179. def test_assign_float64():
  180. x = Tensor(np.array([[0.1, 0.2, 0.3],
  181. [0.4, 0.5, 0.5],
  182. [0.6, 0.7, 0.8]]).astype(np.float64))
  183. y = Tensor(np.array([[0.4, 0.5, 0.5],
  184. [0.6, 0.7, 0.8],
  185. [0.1, 0.2, 0.3]]).astype(np.float64))
  186. assign = Assign(x, y)
  187. output = assign()
  188. output = output.asnumpy()
  189. output_expect = np.array([[0.1, 0.2, 0.3],
  190. [0.4, 0.5, 0.5],
  191. [0.6, 0.7, 0.8]]).astype(np.float64)
  192. print(output)
  193. assert np.all(output - output_expect < 1e-6)