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 39 kB

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