• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 """
16import numpy as np
17import pytest
18
19from mindspore import Tensor, Parameter
20from mindspore import context
21from mindspore import dtype as mstype
22from mindspore.nn import Cell
23from ....mindspore_test_framework.mindspore_test import mindspore_test
24from ....mindspore_test_framework.pipeline.forward.compile_forward \
25    import pipeline_for_compile_forward_ge_graph_for_case_by_case_config, \
26    pipeline_for_compile_forward_ge_graph_for_case_by_case_config_exception
27
28
29class NetWorkSlicePositive(Cell):
30    def __init__(self):
31        super(NetWorkSlicePositive, self).__init__()
32        self.tensor_ret0 = Tensor(np.ones([1, 2, 2], np.int32))
33        self.tensor_ret1 = Tensor(np.ones([4, 7, 4], np.int32))
34        self.tensor_ret2 = Tensor(np.ones([6, 8, 10], np.int32))
35        self.tensor_ret3 = Tensor(np.ones([3, 8, 10], np.int32))
36
37    def construct(self, tensor):
38        ret0 = tensor[3:4:3, 1:5:2, 3:6:2] + self.tensor_ret0
39        ret1 = tensor[-6:4:1, 7:-8:-1, ::3] + self.tensor_ret1
40        ret2 = tensor[::, ::, ::] + self.tensor_ret2
41        ret3 = tensor[::2] + self.tensor_ret3
42        return ret0, ret1, ret2, ret3
43
44
45class NetWorkSliceEllipsis(Cell):
46    def __init__(self):
47        super(NetWorkSliceEllipsis, self).__init__()
48        self.tensor_ret0 = Tensor(np.ones([2, 7, 8], np.int32))
49        self.tensor_ret1 = Tensor(np.ones([6, 7, 8, 9], np.int32))
50        self.tensor_ret2 = Tensor(np.ones([1, 6, 7, 8, 9], np.int32))
51
52    def construct(self, tensor):
53        ret0 = tensor[0:4:2, ..., 1] + self.tensor_ret0
54        ret1 = tensor[...] + self.tensor_ret1
55        ret2 = tensor[None] + self.tensor_ret2
56        ret3 = tensor[True] + self.tensor_ret2
57        return ret0, ret1, ret2, ret3
58
59
60class NetWorkReduceDimension(Cell):
61    def __init__(self):
62        super(NetWorkReduceDimension, self).__init__()
63        self.tensor_ret0 = Tensor(np.ones([2, 4, 1], np.int32))
64        self.tensor_ret1 = Tensor(np.ones([3, 4], np.int32))
65        self.tensor_ret2 = Tensor(np.ones([6, 8], np.int32))
66        self.tensor_ret3 = Tensor(np.array(8, np.int32))
67        self.tensor_ret4 = Tensor(np.ones([8, 10], np.int32))
68
69    def construct(self, tensor):
70        ret0 = tensor[0:6:3, 1:5:1, 3:5:2] + self.tensor_ret0
71        ret1 = tensor[::2, 1, ::3] + self.tensor_ret1
72        ret2 = tensor[::, ::, 0] + self.tensor_ret2
73        ret3 = tensor[3, 2, 5] + self.tensor_ret3
74        ret4 = tensor[1] + self.tensor_ret4
75        return ret0, ret1, ret2, ret3, ret4
76
77
78class NetWorkStepNegative(Cell):
79    def __init__(self):
80        super(NetWorkStepNegative, self).__init__()
81        self.tensor_ret = Tensor(np.ones([6, 5, 10], np.int32))
82
83    def construct(self, tensor):
84        ret = tensor[::1, -5::, ::-1] + self.tensor_ret
85        return ret
86
87
88class NetWorkReduceToScalar(Cell):
89    def __init__(self):
90        super(NetWorkReduceToScalar, self).__init__()
91        self.tensor_ret = Tensor(np.array(9, np.int32))
92
93    def construct(self, tensor):
94        ret = tensor[2, 3, 4] + self.tensor_ret
95        return ret
96
97
98class TensorAssignWithSliceError1(Cell):
99    def __init__(self):
100        super(TensorAssignWithSliceError1, self).__init__()
101
102    def construct(self, a, b):
103        a[1:3:-1, ::] = b
104        return a
105
106
107class TensorAssignWithSliceError2(Cell):
108    def __init__(self):
109        super(TensorAssignWithSliceError2, self).__init__()
110
111    def construct(self, a, b):
112        a[1:3:-1] = b
113        return a
114
115
116class TensorAssignWithSlice2(Cell):
117    def __init__(self):
118        super(TensorAssignWithSlice2, self).__init__()
119
120    def construct(self, a, b, ck):
121        a[1:5] = b
122        a[3:4] = 5
123        a[-1:1:-1] = b
124        a[-1:3:-1] = 5
125        a[::] = b
126        a[::] = 9
127        z = a + ck
128        return z
129
130
131class TensorAssignWithSlice(Cell):
132    def __init__(self):
133        super(TensorAssignWithSlice, self).__init__()
134        self.c = 2.0
135
136    def construct(self, a, b, ck):
137        a[1:3, ::] = b
138        a[2:3:, 3:] = b
139        a[::] = b
140        a[::] = self.c
141        a[::, ::] = b
142        a[::, ::] = self.c
143        a[2:3:, 0:, 4:1:-1] = b
144        a[2:3:, 0:, 4:1:-1] = self.c
145        z = a + ck
146        return z
147
148
149class TensorGetItemByOneTensor(Cell):
150    def __init__(self):
151        super(TensorGetItemByOneTensor, self).__init__()
152        self.const = Tensor(np.ones((5, 4, 7, 8)), mstype.int32)
153
154    def construct(self, x, index):
155        ret = x[index] + self.const
156        return ret
157
158
159class TensorGetItemByTwoTensors(Cell):
160    def __init__(self):
161        super(TensorGetItemByTwoTensors, self).__init__()
162        self.const = Tensor(np.ones((3, 4, 5, 8)), mstype.int32)
163
164    def construct(self, x, index_0, index_1):
165        ret = x[index_0, index_1] + self.const
166        return ret
167
168
169class TensorGetItemByThreeTensors(Cell):
170    def __init__(self):
171        super(TensorGetItemByThreeTensors, self).__init__()
172        self.const = Tensor(np.ones((5, 3, 4, 5)), mstype.int32)
173
174    def construct(self, x, index_0, index_1, index_2):
175        ret = x[index_0, index_1, index_2] + self.const
176        return ret
177
178
179class TensorGetItemByMixedTensors_0(Cell):
180    def __init__(self):
181        super(TensorGetItemByMixedTensors_0, self).__init__()
182        self.const = Tensor(np.ones((3, 4, 5, 3, 6, 5), np.float32))
183
184    def construct(self, tensor, index_0, index_1):
185        ret = tensor[index_0, index_1, 0:3, ..., 0:5, 3] + self.const
186        return ret
187
188
189class TensorGetItemByMixedTensors_1(Cell):
190    def __init__(self):
191        super(TensorGetItemByMixedTensors_1, self).__init__()
192        self.const = Tensor(np.ones((3, 4, 5, 3, 5, 5), np.float32))
193
194    def construct(self, tensor, index_0, index_1):
195        ret = tensor[0:3, index_0, ..., index_1, 3, 0:5] + self.const
196        return ret
197
198
199class TensorGetItemByMixedTensors_2(Cell):
200    def __init__(self):
201        super(TensorGetItemByMixedTensors_2, self).__init__()
202        self.const = Tensor(np.ones((3, 4, 5, 6, 7), np.float32))
203
204    def construct(self, tensor, index_0, index_1):
205        ret = tensor[0, index_0, index_1, ..., 3] + self.const
206        return ret
207
208
209class TensorGetItemByMixedTensors_3(Cell):
210    def __init__(self):
211        super(TensorGetItemByMixedTensors_3, self).__init__()
212        self.const = Tensor(np.ones((3, 4, 5, 3, 4, 3, 5), np.float32))
213
214    def construct(self, tensor, index_0, index_1):
215        ret = tensor[..., index_0, 0:3, index_1, 0:5] + self.const
216        return ret
217
218
219class TensorGetItemByMixedTensors_4(Cell):
220    def __init__(self):
221        super(TensorGetItemByMixedTensors_4, self).__init__()
222        self.const = Tensor(np.ones((2, 2, 3, 4, 5, 3, 9), np.float32))
223
224    def construct(self, tensor, index_0, index_1, index_2):
225        ret = tensor[0:2, index_0, index_1, 2, index_2, 0:3, ...] + self.const
226        return ret
227
228
229class TensorGetItemByMixedTensors_5(Cell):
230    def __init__(self):
231        super(TensorGetItemByMixedTensors_5, self).__init__()
232        self.const = Tensor(np.ones((2, 3, 4, 5, 2, 6), np.float32))
233
234    def construct(self, tensor, index_0, index_1, index_2):
235        ret = tensor[0:2, index_0, index_1, ..., index_2, 2] + self.const
236        return ret
237
238
239class TensorGetItemByMixedTensors_6(Cell):
240    def __init__(self):
241        super(TensorGetItemByMixedTensors_6, self).__init__()
242        self.const = Tensor(np.ones((3, 4, 2, 3, 4, 5), np.float32))
243
244    def construct(self, tensor, index_0, index_1, index_2):
245        ret = tensor[..., index_0, index_1, index_2, 3] + self.const
246        return ret
247
248
249class TensorSetItemByMixedTensors_0(Cell):
250    def __init__(self, value):
251        super(TensorSetItemByMixedTensors_0, self).__init__()
252        self.const = Tensor(np.ones((3, 4, 5, 6, 7, 8, 9), np.float32))
253        self.param = Parameter(Tensor(np.arange(3 * 4 * 5 * 6 * 7 * 8 * 9).reshape((3, 4, 5, 6, 7, 8, 9)),
254                                      mstype.float32),
255                               name="x")
256        self.value = value
257
258    def construct(self, index_0, index_1, index_2):
259        self.param[0:2, index_0, index_1, 2, index_2, 0:3, ...] = self.value
260        ret = self.param + self.const
261        return ret
262
263
264class TensorSetItemByMixedTensors_1(Cell):
265    def __init__(self, value):
266        super(TensorSetItemByMixedTensors_1, self).__init__()
267        self.const = Tensor(np.ones((3, 4, 5, 6, 7, 8), np.float32))
268        self.param = Parameter(Tensor(np.arange(3 * 4 * 5 * 6 * 7 * 8).reshape((3, 4, 5, 6, 7, 8)), mstype.float32),
269                               name="x")
270        self.value = value
271
272    def construct(self, index_0, index_1, index_2):
273        self.param[0:2, index_0, index_1, ..., index_2, 2] = self.value
274        ret = self.param + self.const
275        return ret
276
277
278class TensorSetItemByMixedTensors_2(Cell):
279    def __init__(self, value):
280        super(TensorSetItemByMixedTensors_2, self).__init__()
281        self.const = Tensor(np.ones((3, 4, 5, 6, 7, 8), np.float16))
282        self.param = Parameter(Tensor(np.arange(3 * 4 * 5 * 6 * 7 * 8).reshape((3, 4, 5, 6, 7, 8)), mstype.float16),
283                               name="x")
284        self.value = value
285
286    def construct(self, index_0, index_1, index_2):
287        self.param[..., index_0, index_1, index_2, 3] = self.value
288        ret = self.param + self.const
289        return ret
290
291
292class TensorGetItemByMixedTensorsTypeError(Cell):
293    def __init__(self):
294        super(TensorGetItemByMixedTensorsTypeError, self).__init__()
295
296    def construct(self, x, index_0, index_1):
297        ret = x[index_0, index_1, 0:3, ..., 0:5, [1, 2, 3, 4]]
298        return ret
299
300
301class TensorGetItemByMixedTensorsNumberError(Cell):
302    def __init__(self):
303        super(TensorGetItemByMixedTensorsNumberError, self).__init__()
304
305    def construct(self, x, index_0, index_1):
306        ret = x[index_0, index_1, 0:3, ..., index_1, index_0]
307        return ret
308
309
310class TensorSetItemByOneTensorWithNumber(Cell):
311    def __init__(self, value):
312        super(TensorSetItemByOneTensorWithNumber, self).__init__()
313        self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
314        self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
315        self.value = value
316
317    def construct(self, index):
318        self.param[index] = self.value
319        ret = self.param + self.const
320        return ret
321
322
323class TensorSetItemByOneTensorWithTensor(Cell):
324    def __init__(self):
325        super(TensorSetItemByOneTensorWithTensor, self).__init__()
326        self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
327        self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
328
329    def construct(self, index, value):
330        self.param[index] = value
331        ret = self.param + self.const
332        return ret
333
334
335class TensorSetItemByOneTensorWithTupleOfNumber(Cell):
336    def __init__(self, value):
337        super(TensorSetItemByOneTensorWithTupleOfNumber, self).__init__()
338        self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
339        self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
340        self.value = value
341
342    def construct(self, index):
343        self.param[index] = self.value
344        ret = self.param + self.const
345        return ret
346
347
348class TensorSetItemByOneTensorWithTupleOfTensor(Cell):
349    def __init__(self):
350        super(TensorSetItemByOneTensorWithTupleOfTensor, self).__init__()
351        self.const = Tensor(np.ones((6, 3, 8)), mstype.float32)
352        self.param = Parameter(Tensor(np.arange(6 * 3 * 8).reshape((6, 3, 8)), mstype.float32), name="x")
353
354    def construct(self, index, value_0, value_1, value_2):
355        self.param[index] = (value_0, value_1, value_2)
356        ret = self.param + self.const
357        return ret
358
359
360class TensorSetItemByTensorsWithNumber(Cell):
361    def __init__(self, value):
362        super(TensorSetItemByTensorsWithNumber, self).__init__()
363        self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
364        self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
365        self.value = value
366
367    def construct(self, index_0, index_1, index_2):
368        self.param[index_0, index_1, index_2] = self.value
369        ret = self.param + self.const
370        return ret
371
372
373class TensorSetItemByTensorsWithTensor(Cell):
374    def __init__(self):
375        super(TensorSetItemByTensorsWithTensor, self).__init__()
376        self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
377        self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
378
379    def construct(self, index_0, index_1, index_2, value):
380        self.param[index_0, index_1, index_2] = value
381        ret = self.param + self.const
382        return ret
383
384
385class TensorSetItemByTensorsWithTensorNumberError(Cell):
386    def __init__(self):
387        super(TensorSetItemByTensorsWithTensorNumberError, self).__init__()
388        self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
389        self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
390
391    def construct(self, index_0, index_1, index_2, index_3, value):
392        self.param[index_0, index_1, index_2, index_3] = value
393        ret = self.param + self.const
394        return ret
395
396
397class TensorSetItemByTensorsWithTupleOfNumber(Cell):
398    def __init__(self, value):
399        super(TensorSetItemByTensorsWithTupleOfNumber, self).__init__()
400        self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
401        self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
402        self.value = value
403
404    def construct(self, index_0, index_1, index_2):
405        self.param[index_0, index_1, index_2] = self.value
406        ret = self.param + self.const
407        return ret
408
409
410class TensorSetItemByTensorsWithTupleOfTensor(Cell):
411    def __init__(self):
412        super(TensorSetItemByTensorsWithTupleOfTensor, self).__init__()
413        self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
414        self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
415
416    def construct(self, index_0, index_1, index_2, value_0, value_1, value_2):
417        self.param[index_0, index_1, index_2] = (value_0, value_1, value_2)
418        ret = self.param + self.const
419        return ret
420
421
422class TensorSetItemByTensorsWithTupleOfTensorNumberError(Cell):
423    def __init__(self):
424        super(TensorSetItemByTensorsWithTupleOfTensorNumberError, self).__init__()
425        self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
426        self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
427
428    def construct(self, index_0, index_1, index_2, value_0, value_1):
429        self.param[index_0, index_1, index_2] = (value_0, value_1)
430        ret = self.param + self.const
431        return ret
432
433
434class TensorSetItemByMixedTensors(Cell):
435    def __init__(self):
436        super(TensorSetItemByMixedTensors, self).__init__()
437        self.const = Tensor(np.ones((6, 7, 8)), mstype.float32)
438        self.param = Parameter(Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.float32), name="x")
439        self.value = 99.0
440
441    def construct(self, index_0, index_1):
442        self.param[index_0, index_1, 0:6] = self.value
443        ret = self.param + self.const
444        return ret
445
446
447def test_tensor_assign():
448    context.set_context(mode=context.GRAPH_MODE)
449    net = TensorAssignWithSlice()
450    net2 = TensorAssignWithSlice2()
451    # The test case is no longer appropriate since x[1:3:-1] = np.array(2) does
452    # not incur an error in numpy, which leaves the original array unchanged after
453    # the assign operation.
454    # net_e1 = TensorAssignWithSliceError1()
455    # net_e2 = TensorAssignWithSliceError2()
456    a = np.arange(60).reshape(3, 4, 5)
457    ck = np.arange(60).reshape(3, 4, 5)
458    b = Tensor([1], dtype=mstype.float32)
459    Ta = Tensor(a, dtype=mstype.float32)
460    Tck = Tensor(ck, dtype=mstype.float32)
461    Ta4d = Tensor(a.reshape(1, 3, 4, 5), dtype=mstype.float32)
462    Ta4d_ck = Tensor(ck.reshape(1, 3, 4, 5), dtype=mstype.float32)
463    Tb = Tensor([1, 3], dtype=mstype.float32)
464    Tc = Tensor([], dtype=mstype.float32)
465    t = Tensor([1, 2, 3, 4, 5, 6, 7, 8], dtype=mstype.float32)
466    tck = Tensor([1, 2, 3, 4, 5, 6, 7, 8], dtype=mstype.float32)
467    net(Ta, b, Tck)
468    net2(t, b, tck)
469    # Error for A[Slice] = Number
470    # 1. A[Slice] = Number,  0 in shape
471
472    # with pytest.raises(ValueError):
473    #     net_e2(t, Tensor(2, mstype.int32))
474
475    # Error for A[Slice] = U, U is a Tensor
476    # 1. A[Slice] = U,  u.size is error
477    with pytest.raises(ValueError):
478        net2(t, Tb, tck)
479    # 2. A[Slice] = U, U is empty
480    with pytest.raises(ValueError):
481        net2(t, Tc, tck)
482    # 3. A[Slice] = U, U.size error
483    with pytest.raises(ValueError):
484        net2(t, Tb, tck)
485
486    # Error for A[Tuple(Slice...)] = Tensor
487    # 1. A[Tuple(Slice...)] = U, U is empty
488    with pytest.raises(ValueError):
489        net(Ta, Tc, Tck)
490    # 2. A[Tuple(Slice...)] = U, U.size error
491    with pytest.raises(ValueError):
492        net(Ta, Tb, Tck)
493    # 3. A[Tuple(Slice...)] = U,  Slice error
494    # with pytest.raises(IndexError):
495    #     net_e1(Ta, b)
496
497    # Error for A[Tuple(Slice...)] = Number
498    # 1. A[Tuple(Slice...)] = Number,  Slice error
499    # with pytest.raises(IndexError):
500    #     net_e1(Ta, Tensor(2, mstype.int32))
501
502    net = TensorAssignWithInteger()
503    # Error for A[Number] = scalar/Tensor
504    # 1. A[Number] = U, U is a Tensor, u.size not match
505    with pytest.raises(ValueError):
506        net(Ta, Tb, Tck)
507    with pytest.raises(ValueError):
508        net(Ta, Tc, Tck)
509    # 2. A[Number] = U, the number index error
510    with pytest.raises(IndexError):
511        net(Ta4d, b, Ta4d_ck)
512
513    # Error for A[(n,m)] = scalar/Tensor
514    # 1. A[(n,m)] = U, U is a tensor. u.size not match
515    net = TensorAssignWithTupleInteger()
516    with pytest.raises(ValueError):
517        net(Ta, Tc, Tck)
518    with pytest.raises(ValueError):
519        net(Ta, Tb, Tck)
520    # 2. A[(n,m)] = U, the number index error
521    with pytest.raises(IndexError):
522        net(Ta4d, b, Ta4d_ck)
523
524    # Error for  A[...] = U or A[1:, ...] = u
525    # 1. A[...] = scalar/tensor
526    net = TensorAssignWithEllipsis()
527    net(Ta, Ta4d)
528    with pytest.raises(ValueError):
529        net(Ta, Tc)
530    with pytest.raises(ValueError):
531        net(Ta, Tb)
532    # 2. A[::, 1:, ...] = scalar/tensor
533    net = TensorAssignWithTupleEllipsis()
534    net(Ta, b)
535    Tc = Tensor(1, mstype.float32)
536    net(Ta, Tc)
537    with pytest.raises(ValueError):
538        net(Ta, Tb)
539
540
541class TensorAssignWithTupleEllipsis2(Cell):
542    def __init__(self):
543        super(TensorAssignWithTupleEllipsis2, self).__init__()
544
545    def construct(self, a, b):
546        a[1:, ..., ::] = b
547        return a
548
549
550class TensorAssignWithTupleEllipsis(Cell):
551    def __init__(self):
552        super(TensorAssignWithTupleEllipsis, self).__init__()
553
554    def construct(self, a, b):
555        a[:2, ...] = 1.0
556        a[1:, ...] = b
557        return a
558
559
560class TensorAssignWithEllipsis(Cell):
561    def __init__(self):
562        super(TensorAssignWithEllipsis, self).__init__()
563
564    def construct(self, a, b):
565        a[...] = 1
566        a[...] = b
567        return a
568
569
570class TensorAssignWithInteger(Cell):
571    def __init__(self):
572        super(TensorAssignWithInteger, self).__init__()
573
574    def construct(self, a, b, ck):
575        a[1] = 1
576        a[0] = b
577        z = a + ck
578        return z
579
580
581class TensorAssignWithTupleInteger(Cell):
582    def __init__(self):
583        super(TensorAssignWithTupleInteger, self).__init__()
584
585    def construct(self, a, b, ck):
586        a[(1)] = 1.0
587        a[(1)] = b
588        a[(1, 1)] = b
589        a[(1, 1)] = 1.0
590        z = a + ck
591        return z
592
593
594class TensorAssignWithBoolTensorIndex(Cell):
595    def __init__(self):
596        super(TensorAssignWithBoolTensorIndex, self).__init__()
597        self.t = Tensor(np.arange(60).reshape([3, 4, 5]), dtype=mstype.float32)
598        self.u_scalar = 5
599
600    def construct(self, a, b, c, u_tensor):
601        a[c] = self.u_scalar
602        a[b] = u_tensor
603        z = a + self.t
604        return z
605
606
607class TensorAssignWithBoolTensorIndexError(Cell):
608    def __init__(self):
609        super(TensorAssignWithBoolTensorIndexError, self).__init__()
610
611    def construct(self, a, b, c, u_tensor):
612        a[b][c] = u_tensor
613        return a
614
615
616class TensorAssignWithBoolTensorIndex2(Cell):
617    def __init__(self):
618        super(TensorAssignWithBoolTensorIndex2, self).__init__()
619        self.t = Tensor(np.arange(6).reshape([2, 3]), dtype=mstype.float32)
620        self.t = Tensor(np.arange(60).reshape([3, 4, 5]), dtype=mstype.float32)
621        self.u_scalar = 5
622
623    def construct(self, a, u_tensor):
624        a[a > 8] = u_tensor
625        a[a >= 6] = self.u_scalar
626        a[a < 3] = self.u_scalar
627        a[a <= 5] = u_tensor
628        a[a == 5] = self.u_scalar
629        z = a + self.t
630        return z
631
632
633class TensorAssignWithBoolTensorIndex2Error(Cell):
634    def __init__(self):
635        super(TensorAssignWithBoolTensorIndex2Error, self).__init__()
636
637    def construct(self, a, u_tensor):
638        a[a > 8][a > 5] = u_tensor
639        return a
640
641
642class TensorItemSetWithNumber(Cell):
643    def construct(self, tensor, number_value):
644        ret = tensor.itemset(number_value)
645        return ret
646
647
648class TensorItemSetByItemWithNumber(Cell):
649    def construct(self, tensor, index, number_value):
650        ret = tensor.itemset(index, number_value)
651        return ret
652
653
654input_1d_np = np.ndarray([1]).astype(np.float32)
655input_1d_ms = Tensor(input_1d_np, mstype.float32)
656
657input_3d_np = np.random.randint(3, size=(3, 4, 5)).astype(np.int32)
658input_3d_ms = Tensor(input_3d_np, mstype.float32)
659
660index_np_1, index_np_2, index_np_3, index_np_4 = 0, 30, 60, 2.0
661tuple_index_np_1, tuple_index_np_2, tuple_index_np_3, tuple_index_np_4, tuple_index_np_5 = \
662    (0,), (1, 2), (1, 2, 3), (3, 4, 4), (1, 2, 3, 4)
663value_np_1, value_np_2 = 1, 2.0
664
665
666a = np.arange(60).reshape(3, 4, 5)
667ck = np.arange(60).reshape(3, 4, 5)
668a4 = np.arange(60).reshape(3, 2, 2, 5)
669b = a > 5
670c = a < 3
671Ta = Tensor(a, dtype=mstype.float32)
672Tck = Tensor(ck, dtype=mstype.float32)
673Ta4 = Tensor(a4, dtype=mstype.float32)
674Tb = Tensor(b)
675Tc = Tensor(c)
676Td = Tensor([True, True])
677u_tensor = Tensor([1], dtype=mstype.float32)
678u_tensor_error = Tensor([1, 2], dtype=mstype.float32)
679t_1d = Tensor([1, 2, 3, 4, 5, 6, 7, 8], dtype=mstype.float32)
680tck_1d = Tensor([1, 2, 3, 4, 5, 6, 7, 8], dtype=mstype.float32)
681u_scalar = 5
682
683
684def test_tensor_assign_bool_index():
685    net1 = TensorAssignWithBoolTensorIndex()
686    net2 = TensorAssignWithBoolTensorIndex2()
687    net1(Ta, Tb, Tc, u_tensor)
688    net1(Ta, Tb, Tc, u_tensor)
689    with pytest.raises(ValueError):
690        net1(Ta, Td, Tc, u_tensor)
691    with pytest.raises(IndexError):
692        net1(Ta, u_tensor, Tc, u_tensor)
693    with pytest.raises(ValueError):
694        net1(Ta, Tb, Td, u_tensor)
695    with pytest.raises(IndexError):
696        net1(Ta, Tb, Ta, u_tensor)
697    with pytest.raises(ValueError):
698        net1(Ta, Tb, Tc, u_tensor_error)
699    # net1(Ta, u_tensor, Tc, u_tensor_error, u_scalar)
700    with pytest.raises(ValueError):
701        net2(Ta, u_tensor_error)
702    net3 = TensorAssignWithBoolTensorIndexError()
703    with pytest.raises(IndexError):
704        net3(Ta, Tb, Tc, u_tensor)
705    with pytest.raises(IndexError):
706        net3(Ta, Tb, Tc, Tensor(u_scalar, mstype.int32))
707    net4 = TensorAssignWithBoolTensorIndex2Error()
708    with pytest.raises(IndexError):
709        net4(Ta, u_tensor)
710    with pytest.raises(IndexError):
711        net4(Ta, Tensor(u_scalar, mstype.int32))
712
713
714test_cases = [
715    ('TensorAssignWithTupleEllipsis2', {
716        'block': TensorAssignWithTupleEllipsis2(),
717        'desc_inputs': [Ta4, u_tensor],
718    }),
719    ('TensorAssignWithTupleEllipsis', {
720        'block': TensorAssignWithTupleEllipsis(),
721        'desc_inputs': [Ta, u_tensor],
722    }),
723    ('TensorAssignWithEllipsis', {
724        'block': TensorAssignWithEllipsis(),
725        'desc_inputs': [Ta, u_tensor],
726    }),
727    ('TensorAssignWithTupleInteger', {
728        'block': TensorAssignWithTupleInteger(),
729        'desc_inputs': [Ta, u_tensor, Tck],
730    }),
731    ('TensorAssignWithInteger', {
732        'block': TensorAssignWithInteger(),
733        'desc_inputs': [Ta, u_tensor, Tck],
734    }),
735    ('TensorAssignWithSlice', {
736        'block': TensorAssignWithSlice(),
737        'desc_inputs': [Ta, u_tensor, Tck],
738    }),
739    ('TensorAssignWithSlice2', {
740        'block': TensorAssignWithSlice2(),
741        'desc_inputs': [t_1d, u_tensor, tck_1d],
742    }),
743    ('TensorAssignWithBoolTensorIndex', {
744        'block': TensorAssignWithBoolTensorIndex(),
745        'desc_inputs': [Ta, Tb, Tc, u_tensor],
746    }),
747    ('TensorAssignWithBoolTensorIndex2', {
748        'block': TensorAssignWithBoolTensorIndex2(),
749        'desc_inputs': [Ta, u_tensor],
750    }),
751    ('SlicePositive', {
752        'block': NetWorkSlicePositive(),
753        'desc_inputs': [Tensor(np.ones([6, 8, 10], np.int32))],
754    }),
755    ('SliceReduceDimension', {
756        'block': NetWorkReduceDimension(),
757        'desc_inputs': [Tensor(np.ones([6, 8, 10], np.int32))],
758    }),
759    ('SliceNegative', {
760        'block': NetWorkStepNegative(),
761        'desc_inputs': [Tensor(np.ones([6, 8, 10], np.int32))],
762    }),
763    ('SliceReduceToScalar', {
764        'block': NetWorkReduceToScalar(),
765        'desc_inputs': [Tensor(np.ones([6, 8, 10], np.int32))],
766    }),
767    ('TensorSliceEllipsis', {
768        'block': NetWorkSliceEllipsis(),
769        'desc_inputs': [Tensor(np.ones([6, 7, 8, 9], np.int32))],
770    }),
771    ('TensorGetItemByOneTensor', {
772        'block': TensorGetItemByOneTensor(),
773        'desc_inputs': [Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.int32),
774                        Tensor(np.random.randint(6, size=(5, 4)), mstype.int32)],
775    }),
776    ('TensorGetItemByTwoTensors', {
777        'block': TensorGetItemByTwoTensors(),
778        'desc_inputs': [Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.int32),
779                        Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
780                        Tensor(np.random.randint(7, size=(4, 5)), mstype.int32)],
781    }),
782    ('TensorGetItemByThreeTensors', {
783        'block': TensorGetItemByThreeTensors(),
784        'desc_inputs': [Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.int32),
785                        Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
786                        Tensor(np.random.randint(7, size=(4, 5)), mstype.int32),
787                        Tensor(np.random.randint(8, size=(5, 3, 4, 5)), mstype.int32)],
788    }),
789    ('TensorGetItemByMixedTensors_0', {
790        'block': TensorGetItemByMixedTensors_0(),
791        'desc_inputs': [Tensor(np.arange(3 * 4 * 5 * 6 * 7 * 8).reshape((3, 4, 5, 6, 7, 8)), mstype.float32),
792                        Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
793                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32)],
794    }),
795    ('TensorGetItemByMixedTensors_1', {
796        'block': TensorGetItemByMixedTensors_1(),
797        'desc_inputs': [Tensor(np.arange(3 * 4 * 5 * 6 * 7 * 8).reshape((3, 4, 5, 6, 7, 8)), mstype.float32),
798                        Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
799                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32)],
800    }),
801    ('TensorGetItemByMixedTensors_2', {
802        'block': TensorGetItemByMixedTensors_2(),
803        'desc_inputs': [Tensor(np.arange(3 * 4 * 5 * 6 * 7 * 8).reshape((3, 4, 5, 6, 7, 8)), mstype.float32),
804                        Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
805                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32)],
806    }),
807    ('TensorGetItemByMixedTensors_3', {
808        'block': TensorGetItemByMixedTensors_3(),
809        'desc_inputs': [Tensor(np.arange(3 * 4 * 5 * 6 * 7 * 8).reshape((3, 4, 5, 6, 7, 8)), mstype.float32),
810                        Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
811                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32)],
812    }),
813    ('TensorGetItemByMixedTensors_4', {
814        'block': TensorGetItemByMixedTensors_4(),
815        'desc_inputs': [Tensor(np.arange(3 * 4 * 5 * 6 * 7 * 8 * 9).reshape((3, 4, 5, 6, 7, 8, 9)), mstype.float32),
816                        Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
817                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
818                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
819    }),
820    ('TensorGetItemByMixedTensors_5', {
821        'block': TensorGetItemByMixedTensors_5(),
822        'desc_inputs': [Tensor(np.arange(3 * 4 * 5 * 6 * 7 * 8).reshape((3, 4, 5, 6, 7, 8)), mstype.float32),
823                        Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
824                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
825                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
826    }),
827    ('TensorGetItemByMixedTensors_6', {
828        'block': TensorGetItemByMixedTensors_6(),
829        'desc_inputs': [Tensor(np.arange(3 * 4 * 5 * 6 * 7 * 8).reshape((3, 4, 5, 6, 7, 8)), mstype.float32),
830                        Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
831                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
832                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
833    }),
834    ('TensorSetItemByOneTensorWithNumber', {
835        'block': TensorSetItemByOneTensorWithNumber(value=0.0),
836        'desc_inputs': [Tensor(np.random.randint(4, size=(5, 4)), mstype.int32)],
837    }),
838    ('TensorSetItemByOneTensorWithTensor', {
839        'block': TensorSetItemByOneTensorWithTensor(),
840        'desc_inputs': [Tensor(np.random.randint(3, size=(5, 4)), mstype.int32),
841                        Tensor(np.zeros((4, 7, 8)), mstype.float32)],
842    }),
843    ('TensorSetItemByOneTensorWithTupleOfNumber', {
844        'block': TensorSetItemByOneTensorWithTupleOfNumber(value=(0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7)),
845        'desc_inputs': [Tensor(np.random.randint(5, size=(5, 4)), mstype.int32)],
846    }),
847    ('TensorSetItemByOneTensorWithTupleOfTensor', {
848        'block': TensorSetItemByOneTensorWithTupleOfTensor(),
849        'desc_inputs': [Tensor(np.random.randint(6, size=(5, 4)), mstype.int32),
850                        Tensor(np.zeros((8,), np.float32)),
851                        Tensor(np.ones((8,), np.float32)),
852                        Tensor(np.ones((8,), np.float32) * 2)],
853    }),
854    ('TensorSetItemByTensorsWithNumber', {
855        'block': TensorSetItemByTensorsWithNumber(value=0.0),
856        'desc_inputs': [Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
857                        Tensor(np.random.randint(7, size=(4, 5)), mstype.int32),
858                        Tensor(np.random.randint(8, size=(5, 3, 4, 5)), mstype.int32)],
859    }),
860    ('TensorSetItemByTensorsWithTensor', {
861        'block': TensorSetItemByTensorsWithTensor(),
862        'desc_inputs': [Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
863                        Tensor(np.random.randint(7, size=(4, 5)), mstype.int32),
864                        Tensor(np.random.randint(8, size=(5, 3, 4, 5)), mstype.int32),
865                        Tensor(np.zeros((4, 5)), mstype.float32)],
866    }),
867    ('TensorSetItemByTensorsWithTupleOfNumber', {
868        'block': TensorSetItemByTensorsWithTupleOfNumber(value=(0.0, 1.1, 2.2, 3.3, 4.4)),
869        'desc_inputs': [Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
870                        Tensor(np.random.randint(7, size=(4, 5)), mstype.int32),
871                        Tensor(np.random.randint(8, size=(5, 3, 4, 5)), mstype.int32)],
872    }),
873    ('TensorSetItemByTensorsWithTupleOfTensor', {
874        'block': TensorSetItemByTensorsWithTupleOfTensor(),
875        'desc_inputs': [Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
876                        Tensor(np.random.randint(7, size=(4, 5)), mstype.int32),
877                        Tensor(np.random.randint(8, size=(5, 3, 4, 5)), mstype.int32),
878                        Tensor(np.zeros((4, 5)), mstype.float32),
879                        Tensor(np.ones((4, 5)), mstype.float32),
880                        Tensor(np.ones((4, 5)) * 2, mstype.float32)],
881    }),
882    ('TensorSetItemByMixedTensorsWithNumber_0', {
883        'block': TensorSetItemByMixedTensors_0(value=88.0),
884        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
885                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
886                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
887    }),
888    ('TensorSetItemByMixedTensorsWithTensor_0', {
889        'block': TensorSetItemByMixedTensors_0(value=Tensor(np.ones((4, 5, 3, 9), np.float32))),
890        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
891                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
892                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
893    }),
894    ('TensorGetItemByMixedTensorsWithTupleOfNumber_0', {
895        'block': TensorSetItemByMixedTensors_0(value=(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)),
896        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
897                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
898                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
899    }),
900    ('TensorGetItemByMixedTensorsWithTupleOfTensor_0', {
901        'block': TensorSetItemByMixedTensors_0(value=(Tensor(np.ones((4, 5, 3, 9), np.float32)),
902                                                      Tensor(np.zeros((4, 5, 3, 9), np.float32)),
903                                                      Tensor(np.ones((4, 5, 3, 9), np.float32)))),
904        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
905                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
906                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
907    }),
908    ('TensorSetItemByMixedTensorsWithNumber_1', {
909        'block': TensorSetItemByMixedTensors_1(value=88.0),
910        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
911                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
912                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
913    }),
914    ('TensorSetItemByMixedTensorsWithTensor_1', {
915        'block': TensorSetItemByMixedTensors_1(value=Tensor(np.ones((5, 2, 6), np.float32))),
916        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
917                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
918                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
919    }),
920    ('TensorGetItemByMixedTensorsWithTupleOfNumber_1', {
921        'block': TensorSetItemByMixedTensors_1(value=(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)),
922        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
923                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
924                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
925    }),
926    ('TensorGetItemByMixedTensorsWithTupleOfTensor_1', {
927        'block': TensorSetItemByMixedTensors_1(value=(Tensor(np.ones((5, 2, 6), np.float32)),
928                                                      Tensor(np.zeros((5, 2, 6), np.float32)),
929                                                      Tensor(np.ones((5, 2, 6), np.float32)),
930                                                      Tensor(np.ones((5, 2, 6), np.float32)))),
931        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
932                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
933                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
934    }),
935    ('TensorSetItemByMixedTensorsWithNumber_2', {
936        'block': TensorSetItemByMixedTensors_2(value=88.0),
937        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
938                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
939                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
940    }),
941    ('TensorSetItemByMixedTensorsWithTensor_2', {
942        'block': TensorSetItemByMixedTensors_2(value=Tensor(np.ones((3, 4, 2, 3, 4, 5), np.float16))),
943        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
944                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
945                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
946    }),
947    ('TensorGetItemByMixedTensorsWithTupleOfNumber_2', {
948        'block': TensorSetItemByMixedTensors_2(value=(1.0, 2.0, 3.0, 4.0, 5.0)),
949        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
950                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
951                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
952    }),
953    ('TensorGetItemByMixedTensorsWithTupleOfTensor_2', {
954        'block': TensorSetItemByMixedTensors_2(value=(Tensor(np.ones((4, 5), np.float16)),
955                                                      Tensor(np.zeros((4, 5), np.float16)),
956                                                      Tensor(np.ones((4, 5), np.float16)))),
957        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
958                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
959                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
960    }),
961    ('1dTensorItemSetWithInt', {
962        'block': TensorItemSetWithNumber(),
963        'desc_inputs': [input_1d_ms, value_np_1]
964    }),
965    ('1dTensorItemSetWithFloat', {
966        'block': TensorItemSetWithNumber(),
967        'desc_inputs': [input_1d_ms, value_np_2]
968    }),
969    ('1dTensorItemSetByIntWithInt', {
970        'block': TensorItemSetByItemWithNumber(),
971        'desc_inputs': [input_1d_ms, index_np_1, value_np_1]
972    }),
973    ('1dTensorItemSetByIntWithFloat', {
974        'block': TensorItemSetByItemWithNumber(),
975        'desc_inputs': [input_1d_ms, index_np_1, value_np_2]
976    }),
977    ('3dTensorItemSetByIntWithInt', {
978        'block': TensorItemSetByItemWithNumber(),
979        'desc_inputs': [input_3d_ms, index_np_1, value_np_1]
980    }),
981    ('3dTensorItemSetByIntWithFloat', {
982        'block': TensorItemSetByItemWithNumber(),
983        'desc_inputs': [input_3d_ms, index_np_1, value_np_2]
984    }),
985    ('3dTensorItemSetByIntWithInt2', {
986        'block': TensorItemSetByItemWithNumber(),
987        'desc_inputs': [input_3d_ms, index_np_2, value_np_1]
988    }),
989    ('3dTensorItemSetByIntWithFloat2', {
990        'block': TensorItemSetByItemWithNumber(),
991        'desc_inputs': [input_3d_ms, index_np_2, value_np_2]
992    }),
993    ('1dTensorItemSetBy1dTupleWithInt', {
994        'block': TensorItemSetByItemWithNumber(),
995        'desc_inputs': [input_1d_ms, tuple_index_np_1, value_np_1]
996    }),
997    ('1dTensorItemSetBy1dTupleWithFloat', {
998        'block': TensorItemSetByItemWithNumber(),
999        'desc_inputs': [input_1d_ms, tuple_index_np_1, value_np_2]
1000    }),
1001    ('3dTensorItemSetBy3dTupleWithInt', {
1002        'block': TensorItemSetByItemWithNumber(),
1003        'desc_inputs': [input_3d_ms, tuple_index_np_3, value_np_1]
1004    }),
1005    ('3dTensorItemSetBy3dTupleWithFloat', {
1006        'block': TensorItemSetByItemWithNumber(),
1007        'desc_inputs': [input_3d_ms, tuple_index_np_3, value_np_2]
1008    }),
1009]
1010
1011test_error_cases = [
1012    ('TensorGetItemByOneTensorDtypeError', {
1013        'block': (TensorGetItemByOneTensor(), {'exception': IndexError}),
1014        'desc_inputs': [Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.int32),
1015                        Tensor(np.random.randint(6, size=(5, 4)), mstype.int8)],
1016    }),
1017    ('TensorGetItemByTwoTensorsShapeError', {
1018        'block': (TensorGetItemByTwoTensors(), {'exception': IndexError}),
1019        'desc_inputs': [Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.int32),
1020                        Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
1021                        Tensor(np.random.randint(7, size=(2, 3, 5)), mstype.int32)],
1022    }),
1023    ('TensorGetItemByTwoTensorsDtypeError', {
1024        'block': (TensorGetItemByTwoTensors(), {'exception': IndexError}),
1025        'desc_inputs': [Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.int32),
1026                        Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
1027                        Tensor(np.random.randint(7, size=(4, 5)), mstype.float32)],
1028    }),
1029    ('TensorGetItemByThreeTensorsShapeError', {
1030        'block': (TensorGetItemByThreeTensors(), {'exception': IndexError}),
1031        'desc_inputs': [Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.int32),
1032                        Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
1033                        Tensor(np.random.randint(7, size=(3, 4, 5)), mstype.int32),
1034                        Tensor(np.random.randint(8, size=(5, 2, 4, 5)), mstype.int32)],
1035    }),
1036    ('TensorGetItemByThreeTensorsDtypeError', {
1037        'block': (TensorGetItemByThreeTensors(), {'exception': IndexError}),
1038        'desc_inputs': [Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.int32),
1039                        Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
1040                        Tensor(np.random.randint(7, size=(4, 5)), mstype.int64),
1041                        Tensor(np.random.randint(8, size=(5, 3, 4, 5)), mstype.int32)],
1042    }),
1043    ('TensorGetItemByMixedTensorsNumberError', {
1044        'block': (TensorGetItemByMixedTensorsNumberError(), {'exception': IndexError}),
1045        'desc_inputs': [Tensor(np.arange(6 * 7 * 8).reshape((6, 7, 8)), mstype.int32),
1046                        Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
1047                        Tensor(np.random.randint(7, size=(3, 4, 5)), mstype.int32)],
1048    }),
1049    ('TensorGetItemByMixedTensorsTypeError', {
1050        'block': (TensorGetItemByMixedTensorsTypeError(), {'exception': IndexError}),
1051        'desc_inputs': [Tensor(np.arange(3 * 4 * 5 * 6 * 7 * 8 * 9).reshape((3, 4, 5, 6, 7, 8, 9)), mstype.int32),
1052                        Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
1053                        Tensor(np.random.randint(4, size=(3, 4, 5)), mstype.int32)],
1054    }),
1055    ('TensorGetItemByMixedTensorsDtypeError', {
1056        'block': (TensorGetItemByMixedTensors_0(), {'exception': IndexError}),
1057        'desc_inputs': [Tensor(np.arange(3 * 4 * 5 * 6 * 7 * 8 * 9).reshape((3, 4, 5, 6, 7, 8, 9)), mstype.int32),
1058                        Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
1059                        Tensor(np.random.randint(4, size=(4, 5)), mstype.float32)],
1060    }),
1061    ('TensorGetItemByMixedTensorsShapeError', {
1062        'block': (TensorGetItemByMixedTensors_0(), {'exception': IndexError}),
1063        'desc_inputs': [Tensor(np.arange(3 * 4 * 5 * 6 * 7 * 8 * 9).reshape((3, 4, 5, 6, 7, 8, 9)), mstype.int32),
1064                        Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
1065                        Tensor(np.random.randint(4, size=(2, 4, 5)), mstype.int32)],
1066    }),
1067    ('TensorSetItemByOneTensorWithNumberTypeError', {
1068        'block': (TensorSetItemByOneTensorWithNumber(value=0), {'exception': TypeError}),
1069        'desc_inputs': [Tensor(np.random.randint(4, size=(5, 4)), mstype.int32)],
1070    }),
1071    ('TensorSetItemByOneTensorWithTensorShapeError', {
1072        'block': (TensorSetItemByOneTensorWithTensor(), {'exception': ValueError}),
1073        'desc_inputs': [Tensor(np.random.randint(3, size=(5, 4)), mstype.int32),
1074                        Tensor(np.zeros((6, 7, 8)), mstype.float32)],
1075    }),
1076    ('TensorSetItemByOneTensorWithTensorDtypeError', {
1077        'block': (TensorSetItemByOneTensorWithTensor(), {'exception': TypeError}),
1078        'desc_inputs': [Tensor(np.random.randint(3, size=(5, 4)), mstype.int32),
1079                        Tensor(np.zeros((6, 7, 8)), mstype.int32)],
1080    }),
1081    ('TensorSetItemByOneTensorWithTupleOfNumberTypeError', {
1082        'block': (TensorSetItemByOneTensorWithTupleOfNumber(value=(0, 1, 2, 3, 4, 5, 6, 7)), {'exception': TypeError}),
1083        'desc_inputs': [Tensor(np.random.randint(5, size=(5, 4)), mstype.int32)],
1084    }),
1085    ('TensorSetItemByOneTensorWithTupleOfNumberNumberError', {
1086        'block': (TensorSetItemByOneTensorWithTupleOfNumber(value=(0.0, 1.1, 2.2)), {'exception': ValueError}),
1087        'desc_inputs': [Tensor(np.random.randint(5, size=(5, 4)), mstype.int32)],
1088    }),
1089    ('TensorSetItemByOneTensorWithTupleOfTensorDtyeError', {
1090        'block': (TensorSetItemByOneTensorWithTupleOfTensor(), {'exception': TypeError}),
1091        'desc_inputs': [Tensor(np.random.randint(6, size=(5, 4)), mstype.int32),
1092                        Tensor(np.zeros((8,), np.int32)),
1093                        Tensor(np.ones((8,), np.int32)),
1094                        Tensor(np.ones((8,), np.float32) * 2)],
1095    }),
1096    ('TensorSetItemByTensorsWithNumberTypeError', {
1097        'block': (TensorSetItemByTensorsWithNumber(value=0), {'exception': TypeError}),
1098        'desc_inputs': [Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
1099                        Tensor(np.random.randint(7, size=(4, 5)), mstype.int32),
1100                        Tensor(np.random.randint(8, size=(5, 3, 4, 5)), mstype.int32)],
1101    }),
1102    ('TensorSetItemByTensorsWithTensorShapeError', {
1103        'block': (TensorSetItemByTensorsWithTensor(), {'exception': ValueError}),
1104        'desc_inputs': [Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
1105                        Tensor(np.random.randint(7, size=(4, 5)), mstype.int32),
1106                        Tensor(np.random.randint(8, size=(5, 3, 4, 5)), mstype.int32),
1107                        Tensor(np.zeros((2, 5)), mstype.float32)],
1108    }),
1109    ('TensorSetItemByTensorsWithTensorTypeError', {
1110        'block': (TensorSetItemByTensorsWithTensor(), {'exception': TypeError}),
1111        'desc_inputs': [Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
1112                        Tensor(np.random.randint(7, size=(4, 5)), mstype.int32),
1113                        Tensor(np.random.randint(8, size=(5, 3, 4, 5)), mstype.int32),
1114                        Tensor(np.zeros((4, 5)), mstype.int32)],
1115    }),
1116    ('TensorSetItemByTensorsWithTensorNumberError', {
1117        'block': (TensorSetItemByTensorsWithTensorNumberError(), {'exception': IndexError}),
1118        'desc_inputs': [Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
1119                        Tensor(np.random.randint(7, size=(4, 5)), mstype.int32),
1120                        Tensor(np.random.randint(8, size=(5, 3, 4, 5)), mstype.int32),
1121                        Tensor(np.random.randint(8, size=(1, 3, 4, 5)), mstype.int32),
1122                        Tensor(np.zeros((2, 5)), mstype.float32)],
1123    }),
1124    ('TensorSetItemByTensorsWithTupleOfNumberTypeError', {
1125        'block': (TensorSetItemByTensorsWithTupleOfNumber(value=(0.0, 1, 2, 3, 4)), {'exception': TypeError}),
1126        'desc_inputs': [Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
1127                        Tensor(np.random.randint(7, size=(4, 5)), mstype.int32),
1128                        Tensor(np.random.randint(8, size=(5, 3, 4, 5)), mstype.int32)],
1129    }),
1130    ('TensorSetItemByTensorsWithTupleOfNumberNumberError', {
1131        'block': (TensorSetItemByTensorsWithTupleOfNumber(value=(0.0, 1.0, 2.0, 3.0)), {'exception': ValueError}),
1132        'desc_inputs': [Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
1133                        Tensor(np.random.randint(7, size=(4, 5)), mstype.int32),
1134                        Tensor(np.random.randint(8, size=(5, 3, 4, 5)), mstype.int32)],
1135    }),
1136    ('TensorSetItemByTensorsWithTupleOfTensorNumberError', {
1137        'block': (TensorSetItemByTensorsWithTupleOfTensorNumberError(), {'exception': ValueError}),
1138        'desc_inputs': [Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
1139                        Tensor(np.random.randint(7, size=(4, 5)), mstype.int32),
1140                        Tensor(np.random.randint(8, size=(5, 3, 4, 5)), mstype.int32),
1141                        Tensor(np.zeros((4, 5)), mstype.float32),
1142                        Tensor(np.ones((4, 5)), mstype.float32)],
1143    }),
1144    ('TensorSetItemByTensorsWithTupleOfTensorTypeError', {
1145        'block': (TensorSetItemByTensorsWithTupleOfTensor(), {'exception': TypeError}),
1146        'desc_inputs': [Tensor(np.random.randint(6, size=(3, 4, 5)), mstype.int32),
1147                        Tensor(np.random.randint(7, size=(4, 5)), mstype.int32),
1148                        Tensor(np.random.randint(8, size=(5, 3, 4, 5)), mstype.int32),
1149                        Tensor(np.zeros((4, 5)), mstype.float32),
1150                        Tensor(np.ones((4, 5)), mstype.int32),
1151                        Tensor(np.ones((4, 5)) * 2, mstype.int32)],
1152    }),
1153    ('TensorSetItemByMixedTensorsWithNumberValueTypeError', {
1154        'block': (TensorSetItemByMixedTensors_1(value=88), {'exception': TypeError}),
1155        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
1156                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
1157                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
1158    }),
1159    ('TensorSetItemByMixedTensorsWithNumberIndexTypeError', {
1160        'block': (TensorSetItemByMixedTensors_1(value=88.0), {'exception': IndexError}),
1161        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
1162                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
1163                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.float32)],
1164    }),
1165    ('TensorSetItemByMixedTensorsWithTensorValueDtypeError', {
1166        'block': (TensorSetItemByMixedTensors_1(value=Tensor(np.ones((5, 2, 6), np.int32))),
1167                  {'exception': TypeError}),
1168        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
1169                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
1170                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
1171    }),
1172    ('TensorSetItemByMixedTensorsWithTensorValueShapeError', {
1173        'block': (TensorSetItemByMixedTensors_1(value=Tensor(np.ones((3, 2, 6), np.float32))),
1174                  {'exception': ValueError}),
1175        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
1176                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
1177                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
1178    }),
1179    ('TensorSetItemByMixedTensorsWithTensorIndexDtypeError', {
1180        'block': (TensorSetItemByMixedTensors_1(value=Tensor(np.ones((5, 2, 6), np.float32))),
1181                  {'exception': IndexError}),
1182        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
1183                        Tensor(np.random.randint(4, size=(4, 5)), mstype.float32),
1184                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
1185    }),
1186    ('TensorGetItemByMixedTensorsWithTupleOfNumberValueTypeError', {
1187        'block': (TensorSetItemByMixedTensors_1(value=(1.0, 2, 3.0, 4.0, 5.0, 6.0)),
1188                  {'exception': TypeError}),
1189        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
1190                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
1191                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
1192    }),
1193    ('TensorGetItemByMixedTensorsWithTupleOfTensorValueDtypeError', {
1194        'block': (TensorSetItemByMixedTensors_1(value=(Tensor(np.ones((5, 2, 6), np.float32)),
1195                                                       Tensor(np.zeros((5, 2, 6), np.float32)),
1196                                                       Tensor(np.ones((5, 2, 6), np.float32)),
1197                                                       Tensor(np.ones((5, 2, 6), np.int32)))),
1198                  {'exception': TypeError}),
1199        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.int32),
1200                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
1201                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
1202    }),
1203    ('TensorGetItemByMixedTensorsWithTupleOfTensorIndexDtypeError', {
1204        'block': (TensorSetItemByMixedTensors_1(value=(Tensor(np.ones((5, 2, 6), np.float32)),
1205                                                       Tensor(np.zeros((5, 2, 6), np.float32)),
1206                                                       Tensor(np.ones((5, 2, 6), np.float32)),
1207                                                       Tensor(np.ones((5, 2, 6), np.int32)))),
1208                  {'exception': IndexError}),
1209        'desc_inputs': [Tensor(np.random.randint(3, size=(3, 4, 5)), mstype.float32),
1210                        Tensor(np.random.randint(4, size=(4, 5)), mstype.int32),
1211                        Tensor(np.random.randint(3, size=(2, 1, 4, 5)), mstype.int32)],
1212    }),
1213    ('3dTensorItemSetWithInt', {
1214        'block': (TensorItemSetWithNumber(), {'exception': IndexError}),
1215        'desc_inputs': [input_3d_ms, value_np_1]
1216    }),
1217    ('3dTensorItemSetWithFloat', {
1218        'block': (TensorItemSetWithNumber(), {'exception': IndexError}),
1219        'desc_inputs': [input_3d_ms, value_np_2]
1220    }),
1221    ('1dTensorItemSetByOverflowIntWithInt', {
1222        'block': (TensorItemSetByItemWithNumber(), {'exception': IndexError}),
1223        'desc_inputs': [input_1d_ms, index_np_2, value_np_1]
1224    }),
1225    ('1dTensorItemSetByOverflowIntWithFloat', {
1226        'block': (TensorItemSetByItemWithNumber(), {'exception': IndexError}),
1227        'desc_inputs': [input_1d_ms, index_np_2, value_np_2]
1228    }),
1229    ('1dTensorItemSetByFloatWithInt', {
1230        'block': (TensorItemSetByItemWithNumber(), {'exception': TypeError}),
1231        'desc_inputs': [input_1d_ms, index_np_4, value_np_1]
1232    }),
1233    ('1dTensorItemSetByFLoatWithFloat', {
1234        'block': (TensorItemSetByItemWithNumber(), {'exception': TypeError}),
1235        'desc_inputs': [input_1d_ms, index_np_4, value_np_2]
1236    }),
1237    ('3dTensorItemSetByOverflowIntWithInt', {
1238        'block': (TensorItemSetByItemWithNumber(), {'exception': IndexError}),
1239        'desc_inputs': [input_3d_ms, index_np_3, value_np_1]
1240    }),
1241    ('3dTensorItemSetByOverflowIntWithFloat', {
1242        'block': (TensorItemSetByItemWithNumber(), {'exception': IndexError}),
1243        'desc_inputs': [input_3d_ms, index_np_3, value_np_2]
1244    }),
1245    ('3dTensorItemSetByFloatIntWithInt', {
1246        'block': (TensorItemSetByItemWithNumber(), {'exception': TypeError}),
1247        'desc_inputs': [input_3d_ms, index_np_4, value_np_1]
1248    }),
1249    ('3dTensorItemSetByFloatWithFloat', {
1250        'block': (TensorItemSetByItemWithNumber(), {'exception': TypeError}),
1251        'desc_inputs': [input_3d_ms, index_np_4, value_np_2]
1252    }),
1253    ('1dTensorItemSetBy2dTupleWithFloat', {
1254        'block': (TensorItemSetByItemWithNumber(), {'exception': ValueError}),
1255        'desc_inputs': [input_1d_ms, tuple_index_np_2, value_np_1]
1256    }),
1257    ('1dTensorItemSetBy2dTupleWithFloat', {
1258        'block': (TensorItemSetByItemWithNumber(), {'exception': ValueError}),
1259        'desc_inputs': [input_1d_ms, tuple_index_np_2, value_np_2]
1260    }),
1261    ('3dTensorItemSetBy1dTupleWithFloat', {
1262        'block': (TensorItemSetByItemWithNumber(), {'exception': ValueError}),
1263        'desc_inputs': [input_3d_ms, tuple_index_np_1, value_np_1]
1264    }),
1265    ('3dTensorItemSetBy1dTupleWithFloat', {
1266        'block': (TensorItemSetByItemWithNumber(), {'exception': ValueError}),
1267        'desc_inputs': [input_3d_ms, tuple_index_np_1, value_np_2]
1268    }),
1269    ('3dTensorItemSetBy2dTupleWithFloat', {
1270        'block': (TensorItemSetByItemWithNumber(), {'exception': ValueError}),
1271        'desc_inputs': [input_3d_ms, tuple_index_np_2, value_np_1]
1272    }),
1273    ('3dTensorItemSetBy2dTupleWithFloat', {
1274        'block': (TensorItemSetByItemWithNumber(), {'exception': ValueError}),
1275        'desc_inputs': [input_3d_ms, tuple_index_np_2, value_np_2]
1276    }),
1277    ('3dTensorItemSetBy3dTupleOverFloawWithFloat', {
1278        'block': (TensorItemSetByItemWithNumber(), {'exception': ValueError}),
1279        'desc_inputs': [input_3d_ms, tuple_index_np_4, value_np_1]
1280    }),
1281    ('3dTensorItemSetBy3dTupleOverFloawWithFloat', {
1282        'block': (TensorItemSetByItemWithNumber(), {'exception': ValueError}),
1283        'desc_inputs': [input_3d_ms, tuple_index_np_4, value_np_2]
1284    }),
1285    ('3dTensorItemSetBy4dTupleWithFloat', {
1286        'block': (TensorItemSetByItemWithNumber(), {'exception': ValueError}),
1287        'desc_inputs': [input_3d_ms, tuple_index_np_5, value_np_1]
1288    }),
1289    ('3dTensorItemSetBy4dTupleWithFloat', {
1290        'block': (TensorItemSetByItemWithNumber(), {'exception': ValueError}),
1291        'desc_inputs': [input_3d_ms, tuple_index_np_5, value_np_2]
1292    })
1293]
1294
1295
1296@mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
1297def test_exec():
1298    context.set_context(mode=context.GRAPH_MODE)
1299    return test_cases
1300
1301
1302@mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config_exception)
1303def test_check_exception():
1304    return test_error_cases
1305
1306
1307def test_tensor_slice_reduce_out_of_bounds_neg():
1308    class NetWork(Cell):
1309        def __init__(self):
1310            super(NetWork, self).__init__()
1311            self.tensor_ret = Tensor(np.array(9, np.int32))
1312
1313        def construct(self, tensor):
1314            ret = tensor[-7, 3, 4]
1315            return ret
1316
1317    input_tensor = Tensor(np.ones([6, 8, 10], np.int32))
1318    net = NetWork()
1319    with pytest.raises(IndexError):
1320        net(input_tensor)
1321
1322
1323def test_tensor_slice_reduce_out_of_bounds_positive():
1324    class NetWork(Cell):
1325        def __init__(self):
1326            super(NetWork, self).__init__()
1327            self.tensor_ret = Tensor(np.array(9, np.int32))
1328
1329        def construct(self, tensor):
1330            ret = tensor[6, 3, 4]
1331            return ret
1332
1333    input_tensor = Tensor(np.ones([6, 8, 10], np.int32))
1334    net = NetWork()
1335    with pytest.raises(IndexError):
1336        net(input_tensor)
1337