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_tensor_index.py 40 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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. """ test_tensor_slice """
  16. import numpy as np
  17. import pytest
  18. from mindspore import Tensor, Parameter
  19. from mindspore import context
  20. from mindspore import dtype as mstype
  21. from mindspore.nn import Cell
  22. from mindspore.common.parameter import ParameterTuple
  23. from mindspore.ops import composite as C
  24. def setup_module():
  25. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  26. class NetWorkSlicePositive(Cell):
  27. def __init__(self):
  28. super(NetWorkSlicePositive, self).__init__()
  29. self.tensor_ret0 = Tensor(np.ones([1, 2, 3], np.int32))
  30. self.tensor_ret1 = Tensor(np.ones([4, 8, 10], np.int32))
  31. self.tensor_ret2 = Tensor(np.ones([6, 8, 10], np.int32))
  32. self.tensor_ret3 = Tensor(np.ones([3, 8, 10], np.int32))
  33. def construct(self, tensor):
  34. ret0 = tensor[3:4:1, 1:5:2, 3:6:1] + self.tensor_ret0
  35. ret1 = tensor[-6:4:1, 0:8:1, ::1] + self.tensor_ret1
  36. ret2 = tensor[::, ::, ::] + self.tensor_ret2
  37. ret3 = tensor[::2] + self.tensor_ret3
  38. return ret0, ret1, ret2, ret3
  39. @pytest.mark.level0
  40. @pytest.mark.platform_arm_ascend_training
  41. @pytest.mark.platform_x86_ascend_training
  42. @pytest.mark.env_onecard
  43. def test_slice_positive():
  44. net = NetWorkSlicePositive()
  45. input_np = np.arange(6*8*10).reshape(6, 8, 10).astype(np.int32)
  46. input_0 = Tensor(input_np)
  47. output0, output1, output2, output3 = net(input_0)
  48. assert np.all(output0.asnumpy() == input_np[3:4:1, 1:5:2, 3:6:1] + np.ones([1, 2, 3]))
  49. assert np.all(output1.asnumpy() == input_np[-6:4:1, 0:8:1, ::1] + np.ones([4, 8, 10]))
  50. assert np.all(output2.asnumpy() == input_np[::, ::, ::] + np.ones([6, 8, 10]))
  51. assert np.all(output3.asnumpy() == input_np[::2] + np.ones([3, 8, 10]))
  52. class NetWorkSliceEllipsis(Cell):
  53. def __init__(self):
  54. super(NetWorkSliceEllipsis, self).__init__()
  55. self.tensor_ret0 = Tensor(np.ones([2, 7, 8], np.int32))
  56. self.tensor_ret1 = Tensor(np.ones([6, 7, 8, 9], np.int32))
  57. self.tensor_ret2 = Tensor(np.ones([1, 6, 7, 8, 9], np.int32))
  58. def construct(self, tensor):
  59. ret0 = tensor[0:4:2, ..., 1] + self.tensor_ret0
  60. ret1 = tensor[...] + self.tensor_ret1
  61. ret2 = tensor[None] + self.tensor_ret2
  62. ret3 = tensor[True] + self.tensor_ret2
  63. return ret0, ret1, ret2, ret3
  64. def Xtest_slice_ellipsis():
  65. net = NetWorkSliceEllipsis()
  66. input_np = np.arange(6*7*8*9).reshape(6, 7, 8, 9).astype(np.int32)
  67. input_0 = Tensor(input_np)
  68. output0, output1, output2, output3 = net(input_0)
  69. assert np.all(output0.asnumpy() == input_np[0:4:2, ..., 1] + np.ones([1, 2, 3]))
  70. assert np.all(output1.asnumpy() == input_np[...] + np.ones([6, 7, 8, 9]))
  71. assert np.all(output2.asnumpy() == input_np[None] + np.ones([6, 7, 8, 9]))
  72. assert np.all(output3.asnumpy() == input_np[True] + np.ones([1, 6, 7, 8, 9]))
  73. class NetWorkReduceDimension(Cell):
  74. def __init__(self):
  75. super(NetWorkReduceDimension, self).__init__()
  76. self.tensor_ret1 = Tensor(np.ones([3, 10], np.int32))
  77. self.tensor_ret2 = Tensor(np.ones([6, 8], np.int32))
  78. self.tensor_ret3 = Tensor(np.array(8, np.int32))
  79. self.tensor_ret4 = Tensor(np.ones([8, 10], np.int32))
  80. def construct(self, tensor):
  81. ret1 = tensor[::2, 1, ::1] + self.tensor_ret1
  82. ret2 = tensor[::, ::, 0] + self.tensor_ret2
  83. ret3 = tensor[3, 2, 5] + self.tensor_ret3
  84. ret4 = tensor[1] + self.tensor_ret4
  85. return ret1, ret2, ret3, ret4
  86. def Xtest_reduce_dimension():
  87. net = NetWorkReduceDimension()
  88. input_np = np.arange(6*8*10).reshape(6, 8, 10).astype(np.int32)
  89. input_0 = Tensor(input_np)
  90. output1, output2, output3, output4 = net(input_0)
  91. assert np.all(output1.asnumpy() == input_np[::2, 1, ::1] + np.ones([3, 10]))
  92. assert np.all(output2.asnumpy() == input_np[::, ::, 0] + np.ones([6, 8]))
  93. assert np.all(output3.asnumpy() == input_np[3, 2, 5] + np.array(8, np.int32))
  94. assert np.all(output4.asnumpy() == input_np[1] + np.ones([8, 10]))
  95. class NetWorkSliceStep(Cell):
  96. def __init__(self):
  97. super(NetWorkSliceStep, self).__init__()
  98. self.tensor_ret1 = Tensor(np.ones([6, 5, 10], np.int32))
  99. self.tensor_ret2 = Tensor(np.ones([3, 5, 5], np.int32))
  100. def construct(self, tensor):
  101. ret1 = tensor[::1, -5::, ::-1] + self.tensor_ret1
  102. ret2 = tensor[::2, -5::, ::2] + self.tensor_ret2
  103. return ret1, ret2
  104. def Xtest_step_negative():
  105. net = NetWorkSliceEllipsis()
  106. input_np = np.arange(6*8*10).reshape(6, 8, 10).astype(np.int32)
  107. input_0 = Tensor(input_np)
  108. output1, output2 = net(input_0)
  109. assert np.all(output1.asnumpy() == input_np[::1, -5::, ::-1] + np.ones([6, 8, 10]))
  110. assert np.all(output2.asnumpy() == input_np[::2, -5::, ::2] + np.ones([3, 5, 5]))
  111. class TensorGetItemByThreeTensors(Cell):
  112. def __init__(self):
  113. super(TensorGetItemByThreeTensors, self).__init__()
  114. self.const0 = Tensor(np.ones((4, 5, 8, 10)), mstype.int32)
  115. self.const1 = Tensor(np.ones((3, 4, 5, 10)), mstype.int32)
  116. self.const2 = Tensor(np.ones((5, 3, 4, 5)), mstype.int32)
  117. def construct(self, x, index_0, index_1, index_2):
  118. ret0 = x[index_0] + self.const0
  119. ret1 = x[index_0, index_1] + self.const1
  120. ret2 = x[index_0, index_1, index_2] + self.const2
  121. return ret0, ret1, ret2
  122. @pytest.mark.level0
  123. @pytest.mark.platform_arm_ascend_training
  124. @pytest.mark.platform_x86_ascend_training
  125. @pytest.mark.env_onecard
  126. def Xtest_getitem_by_tensors():
  127. """This testcase may encounter a sync stream error occassionally"""
  128. net = TensorGetItemByThreeTensors()
  129. input_x = np.arange(6*8*10).reshape(6, 8, 10).astype(np.int32)
  130. index_0 = np.random.randint(6, size=(3, 4, 5)).astype(np.int32)
  131. index_1 = np.random.randint(6, size=(4, 5)).astype(np.int32)
  132. index_2 = np.random.randint(6, size=(5, 3, 4, 5)).astype(np.int32)
  133. input_x_ms = Tensor(input_x)
  134. index_0_ms = Tensor(index_0)
  135. index_1_ms = Tensor(index_1)
  136. input_2_ms = Tensor(index_2)
  137. output0, output1, output2 = net(input_x_ms, index_0_ms, index_1_ms, input_2_ms)
  138. assert np.all(output0.asnumpy() == input_x[index_0] + np.ones([4, 5, 8, 10]))
  139. assert np.all(output1.asnumpy() == input_x[index_0, index_1] + np.ones([3, 4, 5, 10]))
  140. assert np.all(output2.asnumpy() == input_x[index_0, index_1, index_2] + np.ones([5, 3, 4, 5]))
  141. class TensorGetItemByMixedTensorsBasicCase(Cell):
  142. def __init__(self, c0, c1, c2, c3, c4, c5):
  143. super(TensorGetItemByMixedTensorsBasicCase, self).__init__()
  144. self.const0 = Tensor(c0)
  145. self.const1 = Tensor(c1)
  146. self.const2 = Tensor(c2)
  147. self.const3 = Tensor(c3)
  148. self.const4 = Tensor(c4)
  149. self.const5 = Tensor(c5)
  150. def construct(self, tensor, index_0, index_1):
  151. ret0 = tensor[index_0, index_1, 0:3] + self.const0
  152. ret1 = tensor[0:3, index_0, ...] + self.const1
  153. ret2 = tensor[0, index_0, index_1] + self.const2
  154. ret3 = tensor[..., index_0, 0:3] + self.const3
  155. ret4 = tensor[0:2, index_0, index_1] + self.const4
  156. ret5 = tensor[..., index_0, index_1] + self.const5
  157. return ret0, ret1, ret2, ret3, ret4, ret5
  158. @pytest.mark.level0
  159. @pytest.mark.platform_arm_ascend_training
  160. @pytest.mark.platform_x86_ascend_training
  161. @pytest.mark.env_onecard
  162. def test_getitem_by_mixed_tensors():
  163. const0 = np.ones((3, 4, 5, 3), np.float32)
  164. const1 = np.ones((3, 3, 4, 5, 5), np.float32)
  165. const2 = np.ones((3, 4, 5), np.float32)
  166. const3 = np.ones((3, 3, 4, 5, 3), np.float32)
  167. const4 = np.ones((2, 3, 4, 5), np.float32)
  168. const5 = np.ones((3, 3, 4, 5), np.float32)
  169. net = TensorGetItemByMixedTensorsBasicCase(const0, const1, const2, const3, const4, const5)
  170. input_np = np.arange(3 * 4 * 5).reshape((3, 4, 5)).astype(np.float32)
  171. input_ms = Tensor(input_np, mstype.float32)
  172. index_np_0 = np.random.randint(3, size=(3, 4, 5)).astype(np.int32)
  173. index_np_1 = np.random.randint(4, size=(4, 5)).astype(np.int32)
  174. index_0 = Tensor(index_np_0, mstype.int32)
  175. index_1 = Tensor(index_np_1, mstype.int32)
  176. out0, out1, out2, out3, out4, out5 = net(input_ms, index_0, index_1)
  177. assert np.all(out0.asnumpy() == (input_np[index_np_0, index_np_1, 0:3] + const0))
  178. assert np.all(out1.asnumpy() == (input_np[0:3, index_np_0, ...] + const1))
  179. assert np.all(out2.asnumpy() == (input_np[0, index_np_0, index_np_1] + const2))
  180. assert np.all(out3.asnumpy() == (input_np[..., index_np_0, 0:3] + const3))
  181. assert np.all(out4.asnumpy() == (input_np[0:2, index_np_0, index_np_1] + const4))
  182. assert np.all(out5.asnumpy() == (input_np[..., index_np_0, index_np_1] + const5))
  183. class TensorSetItemByMixedTensors_0(Cell):
  184. def __init__(self, value):
  185. super(TensorSetItemByMixedTensors_0, self).__init__()
  186. self.const = Tensor(np.ones((3, 4, 5), np.float32))
  187. self.param = Parameter(Tensor(np.arange(3 * 4 * 5).reshape((3, 4, 5)),
  188. mstype.float32),
  189. name="x")
  190. self.value = value
  191. def construct(self, index_0, index_1, index_2):
  192. self.param[0:2, index_0, index_1] = self.value
  193. ret = self.param + self.const
  194. return ret
  195. @pytest.mark.level0
  196. @pytest.mark.platform_arm_ascend_training
  197. @pytest.mark.platform_x86_ascend_training
  198. @pytest.mark.env_onecard
  199. def test_setitem_by_mixed_tensors_0():
  200. value = 88.0
  201. net = TensorSetItemByMixedTensors_0(value)
  202. index_0 = np.random.randint(3, size=(3, 4, 5))
  203. index_1 = np.random.randint(4, size=(4, 5))
  204. index_2 = np.random.randint(3, size=(2, 1, 4, 5))
  205. index_0_ms = Tensor(index_0, mstype.int32)
  206. index_1_ms = Tensor(index_1, mstype.int32)
  207. index_2_ms = Tensor(index_2, mstype.int32)
  208. input_np = np.arange(3 * 4 * 5).reshape((3, 4, 5)).astype(np.float32)
  209. const = np.ones((3, 4, 5), np.float32)
  210. out = net(index_0_ms, index_1_ms, index_2_ms)
  211. input_np[0:2, index_0, index_1] = value
  212. assert np.all(out.asnumpy() == (input_np + const))
  213. class TensorSetItemByMixedTensors_1(Cell):
  214. def __init__(self, value):
  215. super(TensorSetItemByMixedTensors_1, self).__init__()
  216. self.const = Tensor(np.ones((3, 4, 5), np.float32))
  217. self.param = Parameter(Tensor(np.arange(3 * 4 * 5).reshape((3, 4, 5)), mstype.float32),
  218. name="x")
  219. self.value = value
  220. def construct(self, index_0, index_1, index_2):
  221. self.param[0:2, index_0, ...] = self.value
  222. ret = self.param + self.const
  223. return ret
  224. @pytest.mark.level0
  225. @pytest.mark.platform_arm_ascend_training
  226. @pytest.mark.platform_x86_ascend_training
  227. @pytest.mark.env_onecard
  228. def test_setitem_by_mixed_tensors_1():
  229. value = 88.0
  230. net = TensorSetItemByMixedTensors_1(value)
  231. index_0 = np.random.randint(3, size=(3, 4, 5))
  232. index_1 = np.random.randint(4, size=(4, 5))
  233. index_2 = np.random.randint(3, size=(2, 1, 4, 5))
  234. index_0_ms = Tensor(index_0, mstype.int32)
  235. index_1_ms = Tensor(index_1, mstype.int32)
  236. index_2_ms = Tensor(index_2, mstype.int32)
  237. input_np = np.arange(3 * 4 * 5).reshape((3, 4, 5)).astype(np.float32)
  238. const = np.ones((3, 4, 5), np.float32)
  239. out = net(index_0_ms, index_1_ms, index_2_ms)
  240. input_np[0:2, index_0, ...] = value
  241. assert np.all(out.asnumpy() == (input_np + const))
  242. class TensorSetItemByMixedTensors_2(Cell):
  243. def __init__(self, value):
  244. super(TensorSetItemByMixedTensors_2, self).__init__()
  245. self.const = Tensor(np.ones((3, 4, 5), np.float16))
  246. self.param = Parameter(Tensor(np.arange(3 * 4 * 5).reshape((3, 4, 5)), mstype.float16),
  247. name="x")
  248. self.value = value
  249. def construct(self, index_0, index_1, index_2):
  250. self.param[..., index_0, 1] = self.value
  251. ret = self.param + self.const
  252. return ret
  253. @pytest.mark.level0
  254. @pytest.mark.platform_arm_ascend_training
  255. @pytest.mark.platform_x86_ascend_training
  256. @pytest.mark.env_onecard
  257. def test_setitem_by_mixed_tensors_2():
  258. value = 88.0
  259. net = TensorSetItemByMixedTensors_2(value)
  260. index_0 = np.random.randint(3, size=(3, 4, 5))
  261. index_1 = np.random.randint(4, size=(4, 5))
  262. index_2 = np.random.randint(3, size=(2, 1, 4, 5))
  263. index_0_ms = Tensor(index_0, mstype.int32)
  264. index_1_ms = Tensor(index_1, mstype.int32)
  265. index_2_ms = Tensor(index_2, mstype.int32)
  266. input_np = np.arange(3 * 4 * 5).reshape((3, 4, 5)).astype(np.float32)
  267. const = np.ones((3, 4, 5), np.float32)
  268. out = net(index_0_ms, index_1_ms, index_2_ms)
  269. input_np[..., index_0, 1] = value
  270. assert np.all(out.asnumpy() == (input_np + const))
  271. class TensorGetItemByMixedTensorsTypeError(Cell):
  272. def __init__(self):
  273. super(TensorGetItemByMixedTensorsTypeError, self).__init__()
  274. def construct(self, x, index_0, index_1):
  275. ret = x[index_0, index_1, 0:3, ..., 0:5, [1, 2, 3, 4]]
  276. return ret
  277. def test_getitem_by_mixedtensor_exception():
  278. input_ms = Tensor(np.arange(3 * 4 * 5 * 6 * 7 * 8 * 9).reshape((3, 4, 5, 6, 7, 8, 9)), mstype.int32)
  279. index_0 = Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32)
  280. index_1 = Tensor(np.random.randint(4, size=(3, 4, 5)), mstype.int32)
  281. net1 = TensorGetItemByMixedTensorsTypeError()
  282. with pytest.raises(TypeError):
  283. net1(input_ms, index_0, index_1)
  284. class TensorSetItemByOneTensorWithNumber(Cell):
  285. def __init__(self, value):
  286. super(TensorSetItemByOneTensorWithNumber, self).__init__()
  287. self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
  288. self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
  289. self.value = value
  290. def construct(self, index):
  291. self.param[index] = self.value
  292. ret = self.param + self.const
  293. return ret
  294. @pytest.mark.level0
  295. @pytest.mark.platform_arm_ascend_training
  296. @pytest.mark.platform_x86_ascend_training
  297. @pytest.mark.env_onecard
  298. def test_setitem_one_tensor_with_number():
  299. value = 0.0
  300. net = TensorSetItemByOneTensorWithNumber(value)
  301. index_np = np.random.randint(4, size=(5, 4))
  302. index = Tensor(index_np, mstype.int32)
  303. input_data = np.arange(6 * 7 * 8).reshape((6, 7, 8))
  304. const = np.ones((6, 7, 8)).astype(np.float32)
  305. out = net(index)
  306. input_data[index_np] = value
  307. assert np.all(out.asnumpy() == (input_data + const))
  308. class TensorSetItemByOneTensorWithTensor(Cell):
  309. def __init__(self):
  310. super(TensorSetItemByOneTensorWithTensor, self).__init__()
  311. self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
  312. self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
  313. def construct(self, index, value):
  314. self.param[index] = value
  315. ret = self.param + self.const
  316. return ret
  317. @pytest.mark.level0
  318. @pytest.mark.platform_arm_ascend_training
  319. @pytest.mark.platform_x86_ascend_training
  320. @pytest.mark.env_onecard
  321. def test_setitem_by_one_tensor_with_tensor():
  322. net = TensorSetItemByOneTensorWithTensor()
  323. index_np = np.random.randint(4, size=(5, 4))
  324. index = Tensor(index_np, mstype.int32)
  325. input_data = np.arange(6 * 7 * 8).reshape((6, 7, 8))
  326. const = np.ones((6, 7, 8)).astype(np.float32)
  327. value = np.zeros((4, 7, 8)).astype(np.float32)
  328. value_ms = Tensor(value, mstype.float32)
  329. out = net(index, value_ms)
  330. input_data[index_np] = value
  331. assert np.all(out.asnumpy() == (input_data + const))
  332. class TensorSetItemByOneTensorWithTupleOfNumber(Cell):
  333. def __init__(self, value):
  334. super(TensorSetItemByOneTensorWithTupleOfNumber, self).__init__()
  335. self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
  336. self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
  337. self.value = value
  338. def construct(self, index):
  339. self.param[index] = self.value
  340. ret = self.param + self.const
  341. return ret
  342. @pytest.mark.level0
  343. @pytest.mark.platform_arm_ascend_training
  344. @pytest.mark.platform_x86_ascend_training
  345. @pytest.mark.env_onecard
  346. def test_setitem_by_one_tensor_with_tuple_number():
  347. value = (0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7)
  348. net = TensorSetItemByOneTensorWithTupleOfNumber(value)
  349. input_np = np.random.randint(5, size=(5, 4))
  350. input_ms = Tensor(input_np, mstype.int32)
  351. input_data = np.arange(6 * 7 * 8).reshape((6, 7, 8)).astype(np.float32)
  352. const = np.ones((6, 7, 8)).astype(np.float32)
  353. out = net(input_ms)
  354. input_data[input_np] = value
  355. assert np.all(out.asnumpy() == (input_data + const))
  356. class TensorSetItemByOneTensorWithTupleOfTensor(Cell):
  357. def __init__(self):
  358. super(TensorSetItemByOneTensorWithTupleOfTensor, self).__init__()
  359. self.const = Tensor(np.ones((6, 3, 8)), mstype.float32)
  360. self.param = Parameter(Tensor(np.arange(6 * 3 * 8).reshape((6, 3, 8)), mstype.float32), name="x")
  361. def construct(self, index, value_0, value_1, value_2):
  362. self.param[index] = (value_0, value_1, value_2)
  363. ret = self.param + self.const
  364. return ret
  365. @pytest.mark.level0
  366. @pytest.mark.platform_arm_ascend_training
  367. @pytest.mark.platform_x86_ascend_training
  368. @pytest.mark.env_onecard
  369. def test_setitem_by_one_tensor_with_tuple_tensors():
  370. net = TensorSetItemByOneTensorWithTupleOfTensor()
  371. input_np = np.random.randint(6, size=(5, 4)).astype(np.int32)
  372. input_ms = Tensor(input_np, mstype.int32)
  373. input_data = np.arange(6 * 3 * 8).reshape((6, 3, 8)).astype(np.float32)
  374. value_0_np = np.zeros((8,), np.float32)
  375. value_1_np = np.ones((8,), np.float32)
  376. value_2_np = np.ones((8,), np.float32)*2
  377. value_0 = Tensor(value_0_np)
  378. value_1 = Tensor(value_1_np)
  379. value_2 = Tensor(value_2_np)
  380. const = np.ones((6, 3, 8)).astype(np.float32)
  381. out = net(input_ms, value_0, value_1, value_2)
  382. input_data[input_np] = (value_0_np, value_1_np, value_2_np)
  383. assert np.all(out.asnumpy() == (input_data + const))
  384. class TensorSetItemByTensorsWithNumber(Cell):
  385. def __init__(self, value):
  386. super(TensorSetItemByTensorsWithNumber, self).__init__()
  387. self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
  388. self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
  389. self.value = value
  390. def construct(self, index_0, index_1, index_2):
  391. self.param[index_0, index_1, index_2] = self.value
  392. ret = self.param + self.const
  393. return ret
  394. @pytest.mark.level0
  395. @pytest.mark.platform_arm_ascend_training
  396. @pytest.mark.platform_x86_ascend_training
  397. @pytest.mark.env_onecard
  398. def test_setitem_by_tensors_with_number():
  399. value = 0.0
  400. net = TensorSetItemByTensorsWithNumber(value)
  401. index_0 = np.random.randint(6, size=(3, 4, 5))
  402. index_1 = np.random.randint(7, size=(4, 5))
  403. index_2 = np.random.randint(8, size=(5, 3, 4, 5))
  404. index_0_ms = Tensor(index_0, mstype.int32)
  405. index_1_ms = Tensor(index_1, mstype.int32)
  406. index_2_ms = Tensor(index_2, mstype.int32)
  407. out = net(index_0_ms, index_1_ms, index_2_ms)
  408. const = np.ones((6, 7, 8)).astype(np.float32)
  409. input_data = np.arange(6 * 7 * 8).reshape((6, 7, 8)).astype(np.float32)
  410. input_data[index_0, index_1, index_2] = value
  411. assert np.all(out.asnumpy() == (input_data + const))
  412. class TensorSetItemByTensorsWithTensor(Cell):
  413. def __init__(self):
  414. super(TensorSetItemByTensorsWithTensor, self).__init__()
  415. self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
  416. self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
  417. def construct(self, index_0, index_1, index_2, value):
  418. self.param[index_0, index_1, index_2] = value
  419. ret = self.param + self.const
  420. return ret
  421. @pytest.mark.level0
  422. @pytest.mark.platform_arm_ascend_training
  423. @pytest.mark.platform_x86_ascend_training
  424. @pytest.mark.env_onecard
  425. def test_setitem_by_tensors_with_tensor():
  426. net = TensorSetItemByTensorsWithTensor()
  427. index_0 = np.random.randint(6, size=(3, 4, 5))
  428. index_1 = np.random.randint(7, size=(4, 5))
  429. index_2 = np.random.randint(8, size=(5, 3, 4, 5))
  430. value = np.zeros((4, 5)).astype(np.float32)
  431. index_0_ms = Tensor(index_0, mstype.int32)
  432. index_1_ms = Tensor(index_1, mstype.int32)
  433. index_2_ms = Tensor(index_2, mstype.int32)
  434. value_ms = Tensor(value, mstype.float32)
  435. out = net(index_0_ms, index_1_ms, index_2_ms, value_ms)
  436. const = np.ones((6, 7, 8)).astype(np.float32)
  437. input_data = np.arange(6 * 7 * 8).reshape((6, 7, 8)).astype(np.float32)
  438. input_data[index_0, index_1, index_2] = value
  439. assert np.all(out.asnumpy() == (input_data + const))
  440. class TensorSetItemByTensorsWithTensorNumberError(Cell):
  441. def __init__(self):
  442. super(TensorSetItemByTensorsWithTensorNumberError, self).__init__()
  443. self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
  444. self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
  445. def construct(self, index_0, index_1, index_2, index_3, value):
  446. self.param[index_0, index_1, index_2, index_3] = value
  447. ret = self.param + self.const
  448. return ret
  449. @pytest.mark.level0
  450. @pytest.mark.platform_arm_ascend_training
  451. @pytest.mark.platform_x86_ascend_training
  452. @pytest.mark.env_onecard
  453. def test_setitem_by_tensors_with_tensor_error():
  454. index_0 = Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32)
  455. index_1 = Tensor(np.random.randint(7, size=(4, 5)), mstype.int32)
  456. index_2 = Tensor(np.random.randint(8, size=(5, 3, 4, 5)), mstype.int32)
  457. index_3 = Tensor(np.random.randint(8, size=(1, 3, 4, 5)), mstype.int32)
  458. value = Tensor(np.zeros((2, 5)), mstype.float32)
  459. net = TensorSetItemByTensorsWithTensorNumberError()
  460. with pytest.raises(IndexError):
  461. net(index_0, index_1, index_2, index_3, value)
  462. class TensorSetItemByTensorsWithTupleOfNumber(Cell):
  463. def __init__(self, value):
  464. super(TensorSetItemByTensorsWithTupleOfNumber, self).__init__()
  465. self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
  466. self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
  467. self.value = value
  468. def construct(self, index_0, index_1, index_2):
  469. self.param[index_0, index_1, index_2] = self.value
  470. ret = self.param + self.const
  471. return ret
  472. @pytest.mark.level0
  473. @pytest.mark.platform_arm_ascend_training
  474. @pytest.mark.platform_x86_ascend_training
  475. @pytest.mark.env_onecard
  476. def test_setitem_by_tensors_with_tuple_of_number():
  477. value = (0.0, 1.1, 2.2, 3.3, 4.4)
  478. net = TensorSetItemByTensorsWithTupleOfNumber(value)
  479. index_0 = np.random.randint(6, size=(3, 4, 5))
  480. index_1 = np.random.randint(7, size=(4, 5))
  481. index_2 = np.random.randint(8, size=(5, 3, 4, 5))
  482. index_0_ms = Tensor(index_0, mstype.int32)
  483. index_1_ms = Tensor(index_1, mstype.int32)
  484. index_2_ms = Tensor(index_2, mstype.int32)
  485. input_data = np.arange(6 * 7 * 8).reshape((6, 7, 8)).astype(np.float32)
  486. input_data[index_0, index_1, index_2] = value
  487. const = np.ones((6, 7, 8)).astype(np.float32)
  488. out = net(index_0_ms, index_1_ms, index_2_ms)
  489. assert np.all(out.asnumpy() == (input_data + const))
  490. class TensorSetItemByTensorsWithTupleOfTensor(Cell):
  491. def __init__(self):
  492. super(TensorSetItemByTensorsWithTupleOfTensor, self).__init__()
  493. self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
  494. self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
  495. def construct(self, index_0, index_1, index_2, value_0, value_1, value_2):
  496. self.param[index_0, index_1, index_2] = (value_0, value_1, value_2)
  497. ret = self.param + self.const
  498. return ret
  499. @pytest.mark.level0
  500. @pytest.mark.platform_arm_ascend_training
  501. @pytest.mark.platform_x86_ascend_training
  502. @pytest.mark.env_onecard
  503. def test_setitem_by_tensors_with_tuple_of_tensor():
  504. value_0 = np.zeros((4, 5))
  505. value_1 = np.ones((4, 5))
  506. value_2 = np.ones((4, 5)) * 2
  507. value_0_ms = Tensor(value_0, mstype.float32)
  508. value_1_ms = Tensor(value_1, mstype.float32)
  509. value_2_ms = Tensor(value_2, mstype.float32)
  510. net = TensorSetItemByTensorsWithTupleOfTensor()
  511. index_0 = np.random.randint(6, size=(3, 4, 5))
  512. index_1 = np.random.randint(7, size=(4, 5))
  513. index_2 = np.random.randint(8, size=(5, 3, 4, 5))
  514. index_0_ms = Tensor(index_0, mstype.int32)
  515. index_1_ms = Tensor(index_1, mstype.int32)
  516. index_2_ms = Tensor(index_2, mstype.int32)
  517. input_data = np.arange(6 * 7 * 8).reshape((6, 7, 8)).astype(np.float32)
  518. input_data[index_0, index_1, index_2] = (value_0, value_1, value_2)
  519. const = np.ones((6, 7, 8)).astype(np.float32)
  520. out = net(index_0_ms, index_1_ms, index_2_ms, value_0_ms, value_1_ms, value_2_ms)
  521. assert np.all(out.asnumpy() == (input_data + const))
  522. class TensorSetItemByTensorsWithTupleOfTensorNumberError(Cell):
  523. def __init__(self):
  524. super(TensorSetItemByTensorsWithTupleOfTensorNumberError, self).__init__()
  525. self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
  526. self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
  527. def construct(self, index_0, index_1, index_2, value_0, value_1):
  528. self.param[index_0, index_1, index_2] = (value_0, value_1)
  529. ret = self.param + self.const
  530. return ret
  531. @pytest.mark.level0
  532. @pytest.mark.platform_arm_ascend_training
  533. @pytest.mark.platform_x86_ascend_training
  534. @pytest.mark.env_onecard
  535. def test_setitem_by_tensor_with_tuple_of_tensor_error():
  536. net = TensorSetItemByTensorsWithTupleOfTensorNumberError()
  537. index_0_ms = Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32)
  538. index_1_ms = Tensor(np.random.randint(7, size=(4, 5)), mstype.int32)
  539. index_2_ms = Tensor(np.random.randint(8, size=(5, 3, 4, 5)), mstype.int32)
  540. value_0 = np.zeros((4, 5))
  541. value_1 = np.ones((4, 5))
  542. value_0_ms = Tensor(value_0, mstype.float32)
  543. value_1_ms = Tensor(value_1, mstype.float32)
  544. with pytest.raises(ValueError):
  545. net(index_0_ms, index_1_ms, index_2_ms, value_0_ms, value_1_ms)
  546. def test_setitem_grad():
  547. class Net(Cell):
  548. def __init__(self):
  549. super(Net, self).__init__()
  550. self.weight = Parameter(
  551. Tensor(np.ones([4, 4, 5]), dtype=mstype.float32), "b1", requires_grad=True)
  552. def construct(self, a, b):
  553. a[1:3:1, ::] = b
  554. c = a + self.weight
  555. return c
  556. class GradNet(Cell):
  557. def __init__(self, net):
  558. super(GradNet, self).__init__()
  559. self.net = net
  560. self.weights = ParameterTuple(net.trainable_params())
  561. def construct(self, x, y, sens):
  562. return C.grad_by_list_with_sens(self.net, self.weights)(x, y, sens)
  563. net = GradNet(Net())
  564. x = Tensor(np.ones([4, 4, 5]).astype(np.float32), mstype.float32)
  565. y = Tensor(np.array([3]).astype(np.float32), mstype.float32)
  566. sens = Tensor(np.ones([4, 4, 5]).astype(np.float32), mstype.float32)
  567. net(x, y, sens)
  568. class TensorAssignWithSliceError1(Cell):
  569. def __init__(self):
  570. super(TensorAssignWithSliceError1, self).__init__()
  571. def construct(self, a, b):
  572. a[1:3:-1, ::] = b
  573. return a
  574. class TensorAssignWithSliceError2(Cell):
  575. def __init__(self):
  576. super(TensorAssignWithSliceError2, self).__init__()
  577. def construct(self, a, b):
  578. a[1:3:-1] = b
  579. return a
  580. class TensorAssignWithSlice2(Cell):
  581. def __init__(self):
  582. super(TensorAssignWithSlice2, self).__init__()
  583. def construct(self, a, b, ck):
  584. a[1:5] = b
  585. a[3:4] = 5
  586. a[-1:1:-1] = b
  587. a[-1:3:-1] = 5
  588. a[::] = b
  589. a[::] = 9
  590. z = a + ck
  591. return z
  592. class TensorAssignWithSlice(Cell):
  593. def __init__(self):
  594. super(TensorAssignWithSlice, self).__init__()
  595. self.c = 2.0
  596. def construct(self, a, b, ck):
  597. a[1:3, ::] = b
  598. a[2:3:, 3:] = b
  599. a[::] = b
  600. a[::] = self.c
  601. a[::, ::] = b
  602. a[::, ::] = self.c
  603. a[2:3:, 0:, 4:1:-1] = b
  604. a[2:3:, 0:, 4:1:-1] = self.c
  605. z = a + ck
  606. return z
  607. @pytest.mark.level0
  608. @pytest.mark.platform_arm_ascend_training
  609. @pytest.mark.platform_x86_ascend_training
  610. @pytest.mark.env_onecard
  611. def test_tensor_assign_slice_value_1():
  612. net = TensorAssignWithSlice()
  613. a = np.arange(60).reshape(3, 4, 5)
  614. ck = np.arange(60).reshape(3, 4, 5)
  615. b = np.array([1]).astype(np.float32) # Tensor([1], dtype=mstype.float32)
  616. tb = Tensor(b, dtype=mstype.float32)
  617. ta = Tensor(a, dtype=mstype.float32)
  618. tck = Tensor(ck, dtype=mstype.float32)
  619. out = net(ta, tb, tck)
  620. a[1:3, ::] = b
  621. a[2:3:, 3:] = b
  622. a[::] = b
  623. a[::] = 2.0
  624. a[::, ::] = b
  625. a[::, ::] = 2.0
  626. a[2:3:, 0:, 4:1:-1] = b
  627. a[2:3:, 0:, 4:1:-1] = 2.0
  628. z = a + ck
  629. assert np.all(z == out.asnumpy())
  630. @pytest.mark.level0
  631. @pytest.mark.platform_arm_ascend_training
  632. @pytest.mark.platform_x86_ascend_training
  633. @pytest.mark.env_onecard
  634. def test_tensor_assign_slice_value_2():
  635. net2 = TensorAssignWithSlice2()
  636. a = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  637. ck = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  638. b = np.array([1]).astype(np.float32) # Tensor([1], dtype=mstype.float32)
  639. tb = Tensor(b, dtype=mstype.float32)
  640. ta = Tensor(a, dtype=mstype.float32)
  641. tck = Tensor(ck, dtype=mstype.float32)
  642. out = net2(ta, tb, tck)
  643. a[1:5] = b
  644. a[3:4] = 5
  645. a[-1:1:-1] = b
  646. a[-1:3:-1] = 5
  647. a[::] = b
  648. a[::] = 9
  649. z = a + ck
  650. assert np.all(z == out.asnumpy())
  651. @pytest.mark.level0
  652. @pytest.mark.platform_arm_ascend_training
  653. @pytest.mark.platform_x86_ascend_training
  654. @pytest.mark.env_onecard
  655. def test_tensor_assign_exception():
  656. net = TensorAssignWithSlice()
  657. net2 = TensorAssignWithSlice2()
  658. net_e1 = TensorAssignWithSliceError1()
  659. net_e2 = TensorAssignWithSliceError2()
  660. a = np.arange(60).reshape(3, 4, 5)
  661. ck = np.arange(60).reshape(3, 4, 5)
  662. b = Tensor([1], dtype=mstype.float32)
  663. Ta = Tensor(a, dtype=mstype.float32)
  664. Tck = Tensor(ck, dtype=mstype.float32)
  665. Ta4d = Tensor(a.reshape(1, 3, 4, 5), dtype=mstype.float32)
  666. Ta4d_ck = Tensor(ck.reshape(1, 3, 4, 5), dtype=mstype.float32)
  667. Tb = Tensor([1, 3], dtype=mstype.float32)
  668. Tc = Tensor([], dtype=mstype.float32)
  669. t = Tensor([1, 2, 3, 4, 5, 6, 7, 8], dtype=mstype.float32)
  670. tck = Tensor([1, 2, 3, 4, 5, 6, 7, 8], dtype=mstype.float32)
  671. # Error for A[Slice] = Number
  672. # 1. A[Slice] = Number, Slice error
  673. with pytest.raises(IndexError):
  674. net_e2(t, 2)
  675. # Error for A[Slice] = U, U is a Tensor
  676. # 1. A[Slice] = U, u.size is error
  677. with pytest.raises(ValueError):
  678. net2(t, Tb, tck)
  679. # 2. A[Slice] = U, U is empty
  680. with pytest.raises(ValueError):
  681. net2(t, Tc, tck)
  682. # 3. A[Slice] = U, U.size error
  683. with pytest.raises(ValueError):
  684. net2(t, Tb, tck)
  685. # Error for A[Tuple(Slice...)] = Tensor
  686. # 1. A[Tuple(Slice...)] = U, U is empty
  687. with pytest.raises(ValueError):
  688. net(Ta, Tc, Tck)
  689. # 2. A[Tuple(Slice...)] = U, U.size error
  690. with pytest.raises(ValueError):
  691. net(Ta, Tb, Tck)
  692. # 3. A[Tuple(Slice...)] = U, Slice error
  693. with pytest.raises(IndexError):
  694. net_e1(Ta, b)
  695. # Error for A[Tuple(Slice...)] = Number
  696. # 1. A[Tuple(Slice...)] = Number, Slice error
  697. with pytest.raises(IndexError):
  698. net_e1(Ta, 2)
  699. net = TensorAssignWithInteger()
  700. # Error for A[Number] = scalar/Tensor
  701. # 1. A[Number] = U, U is a Tensor, u.size not match
  702. with pytest.raises(ValueError):
  703. net(Ta, Tb, Tck)
  704. with pytest.raises(ValueError):
  705. net(Ta, Tc, Tck)
  706. # 2. A[Number] = U, the number index error
  707. with pytest.raises(IndexError):
  708. net(Ta4d, b, Ta4d_ck)
  709. # Error for A[(n,m)] = scalar/Tensor
  710. # 1. A[(n,m)] = U, U is a tensor. u.size not match
  711. net = TensorAssignWithTupleInteger()
  712. with pytest.raises(ValueError):
  713. net(Ta, Tc, Tck)
  714. with pytest.raises(ValueError):
  715. net(Ta, Tb, Tck)
  716. # 2. A[(n,m)] = U, the number index error
  717. with pytest.raises(IndexError):
  718. net(Ta4d, b, Ta4d_ck)
  719. # Error for A[...] = U or A[1:, ...] = u
  720. # 1. A[...] = scalar/tensor
  721. net = TensorAssignWithEllipsis()
  722. net(Ta, Ta4d)
  723. with pytest.raises(ValueError):
  724. net(Ta, Tc)
  725. with pytest.raises(ValueError):
  726. net(Ta, Tb)
  727. # 2. A[::, 1:, ...] = scalar/tensor
  728. net = TensorAssignWithTupleEllipsis()
  729. net(Ta, b)
  730. with pytest.raises(ValueError):
  731. net(Ta, Tb)
  732. class TensorAssignWithTupleEllipsis2(Cell):
  733. def __init__(self):
  734. super(TensorAssignWithTupleEllipsis2, self).__init__()
  735. def construct(self, a, b):
  736. a[1:, ..., ::] = b
  737. return a
  738. class TensorAssignWithTupleEllipsis(Cell):
  739. def __init__(self):
  740. super(TensorAssignWithTupleEllipsis, self).__init__()
  741. def construct(self, a, b):
  742. a[:2, ...] = 1.0
  743. a[1:, ...] = b
  744. return a
  745. class TensorAssignWithEllipsis(Cell):
  746. def __init__(self):
  747. super(TensorAssignWithEllipsis, self).__init__()
  748. def construct(self, a, b):
  749. a[...] = 1
  750. a[...] = b
  751. return a
  752. class TensorAssignWithInteger(Cell):
  753. def __init__(self):
  754. super(TensorAssignWithInteger, self).__init__()
  755. def construct(self, a, b, ck):
  756. a[1] = 1
  757. a[0] = b
  758. z = a + ck
  759. return z
  760. class TensorAssignWithTupleInteger(Cell):
  761. def __init__(self):
  762. super(TensorAssignWithTupleInteger, self).__init__()
  763. def construct(self, a, b, ck):
  764. a[(1)] = 1
  765. a[(1)] = b
  766. a[(1, 1)] = b
  767. a[(1, 1)] = 1
  768. z = a + ck
  769. return z
  770. class TensorAssignWithBoolTensorIndex(Cell):
  771. def __init__(self):
  772. super(TensorAssignWithBoolTensorIndex, self).__init__()
  773. self.t = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  774. self.u_scalar = 5
  775. def construct(self, a, b, c, u_tensor):
  776. a[c] = self.u_scalar
  777. a[b] = u_tensor
  778. z = a + self.t
  779. return z
  780. class TensorAssignWithBoolTensorIndexError(Cell):
  781. def __init__(self):
  782. super(TensorAssignWithBoolTensorIndexError, self).__init__()
  783. def construct(self, a, b, c, u_tensor):
  784. a[b][c] = u_tensor
  785. return a
  786. class TensorAssignWithBoolTensorIndex2(Cell):
  787. def __init__(self):
  788. super(TensorAssignWithBoolTensorIndex2, self).__init__()
  789. self.t = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  790. self.u_scalar = 5
  791. def construct(self, a, u_tensor):
  792. a[a > 8] = u_tensor
  793. a[a >= 6] = self.u_scalar
  794. a[a < 3] = self.u_scalar
  795. a[a <= 5] = u_tensor
  796. a[a == 5] = self.u_scalar
  797. z = a + self.t
  798. return z
  799. class TensorAssignWithBoolTensorIndex2Error(Cell):
  800. def __init__(self):
  801. super(TensorAssignWithBoolTensorIndex2Error, self).__init__()
  802. def construct(self, a, u_tensor):
  803. a[a > 8][a > 5] = u_tensor
  804. return a
  805. @pytest.mark.level0
  806. @pytest.mark.platform_arm_ascend_training
  807. @pytest.mark.platform_x86_ascend_training
  808. @pytest.mark.env_onecard
  809. def test_tensor_assign_bool_index_0():
  810. a = np.arange(60).reshape(3, 4, 5)
  811. b = a > 5
  812. c = a < 3
  813. Ta = Tensor(a, dtype=mstype.float32)
  814. Tb = Tensor(b)
  815. Tc = Tensor(c)
  816. u_tensor = Tensor([1], dtype=mstype.float32)
  817. net1 = TensorAssignWithBoolTensorIndex()
  818. out = net1(Ta, Tb, Tc, u_tensor)
  819. res = np.arange(60).reshape(3, 4, 5)
  820. res[c] = 5
  821. res[b] = 1
  822. res = res + np.ones([3, 4, 5])
  823. assert np.all(out.asnumpy() == res)
  824. @pytest.mark.level0
  825. @pytest.mark.platform_arm_ascend_training
  826. @pytest.mark.platform_x86_ascend_training
  827. @pytest.mark.env_onecard
  828. def test_tensor_assign_bool_index_1():
  829. a = np.arange(60).reshape(3, 4, 5)
  830. Ta = Tensor(a, dtype=mstype.float32)
  831. u_tensor = Tensor([1], dtype=mstype.float32)
  832. net2 = TensorAssignWithBoolTensorIndex2()
  833. out = net2(Ta, u_tensor)
  834. res = np.arange(60).reshape(3, 4, 5)
  835. res[res > 8] = 1
  836. res[res >= 6] = 5
  837. res[res < 3] = 5
  838. res[res <= 5] = 1
  839. res[res == 5] = 5
  840. res = res + np.ones([3, 4, 5])
  841. assert np.all(out.asnumpy() == res)
  842. def test_tensor_assign_bool_index_exception():
  843. a = np.arange(60).reshape(3, 4, 5)
  844. b = a > 5
  845. c = a < 3
  846. Ta = Tensor(a, dtype=mstype.float32)
  847. Tb = Tensor(b)
  848. Tc = Tensor(c)
  849. Td = Tensor([True, True])
  850. u_tensor = Tensor([1], dtype=mstype.float32)
  851. u_tensor_error = Tensor([1, 2], dtype=mstype.float32)
  852. u_scalar = 5
  853. net1 = TensorAssignWithBoolTensorIndex()
  854. net2 = TensorAssignWithBoolTensorIndex2()
  855. with pytest.raises(ValueError):
  856. net1(Ta, Td, Tc, u_tensor)
  857. with pytest.raises(IndexError):
  858. net1(Ta, u_tensor, Tc, u_tensor)
  859. with pytest.raises(ValueError):
  860. net1(Ta, Tb, Td, u_tensor)
  861. with pytest.raises(IndexError):
  862. net1(Ta, Tb, Ta, u_tensor)
  863. with pytest.raises(ValueError):
  864. net1(Ta, Tb, Tc, u_tensor_error)
  865. # net1(Ta, u_tensor, Tc, u_tensor_error, u_scalar)
  866. with pytest.raises(ValueError):
  867. net2(Ta, u_tensor_error)
  868. net3 = TensorAssignWithBoolTensorIndexError()
  869. with pytest.raises(IndexError):
  870. net3(Ta, Tb, Tc, u_tensor)
  871. with pytest.raises(IndexError):
  872. net3(Ta, Tb, Tc, u_scalar)
  873. net4 = TensorAssignWithBoolTensorIndex2Error()
  874. with pytest.raises(IndexError):
  875. net4(Ta, u_tensor)
  876. with pytest.raises(IndexError):
  877. net4(Ta, u_scalar)
  878. def Xtest_tensor_slice_reduce_out_of_bounds_neg():
  879. class NetWork(Cell):
  880. def __init__(self):
  881. super(NetWork, self).__init__()
  882. self.tensor_ret = Tensor(np.array(9, np.int32))
  883. def construct(self, tensor):
  884. ret = tensor[-7, 3, 4]
  885. return ret
  886. input_tensor = Tensor(np.ones([6, 8, 10], np.int32))
  887. net = NetWork()
  888. with pytest.raises(ValueError) as ex:
  889. net(input_tensor)
  890. assert "For 'StridedSlice' the `begin[0]` should be an int and must greater or equal to -6, but got `-7`" in str(
  891. ex.value)
  892. def Xtest_tensor_slice_reduce_out_of_bounds_positive():
  893. class NetWork(Cell):
  894. def __init__(self):
  895. super(NetWork, self).__init__()
  896. self.tensor_ret = Tensor(np.array(9, np.int32))
  897. def construct(self, tensor):
  898. ret = tensor[6, 3, 4]
  899. return ret
  900. input_tensor = Tensor(np.ones([6, 8, 10], np.int32))
  901. net = NetWork()
  902. with pytest.raises(ValueError) as ex:
  903. net(input_tensor)
  904. assert "For 'StridedSlice' the `begin[0]` should be an int and must less than 6, but got `6`" in str(ex.value)
  905. @pytest.mark.level0
  906. @pytest.mark.platform_arm_ascend_training
  907. @pytest.mark.platform_x86_ascend_training
  908. @pytest.mark.env_onecard
  909. def test_tensor_range():
  910. a = np.arange(4*5*6).reshape(4, 5, 6).astype(np.float32)
  911. ta = Tensor(a, mstype.float32)
  912. ms_out = []
  913. for item in ta:
  914. ms_out.append(item)
  915. np_out = []
  916. for item in a:
  917. np_out.append(item)
  918. for i, elem in enumerate(ms_out):
  919. assert np.all(elem.asnumpy() == np_out[i])