@@ -301,5 +301,231 @@ def test_npu(self):
301301 self .assertTrue (np .allclose (npu_loss , cpu_loss ))
302302
303303
304+ class TestSliceOpDecsDim (OpTest ):
305+ def setUp (self ):
306+ self .op_type = "slice"
307+ self .set_npu ()
308+ self .init_dtype ()
309+ self .config ()
310+ self .set_inputs ()
311+ self .set_outputs ()
312+ self .set_attrs ()
313+
314+ def set_inputs (self ):
315+ self .inputs = {'Input' : self .input }
316+
317+ def set_outputs (self ):
318+ self .outputs = {'Out' : self .out }
319+
320+ def set_attrs (self ):
321+ self .attrs = {
322+ 'axes' : self .axes ,
323+ 'starts' : self .starts ,
324+ 'ends' : self .ends ,
325+ 'infer_flags' : self .infer_flags ,
326+ 'decrease_axis' : self .decrease_axis ,
327+ }
328+
329+ def config (self ):
330+ self .input = np .random .random ([3 , 4 , 5 , 6 ]).astype (self .dtype )
331+ self .starts = [1 , 0 , 2 ]
332+ self .ends = [2 , 3 , 4 ]
333+ self .axes = [0 , 1 , 2 ]
334+ self .decrease_axis = [0 ]
335+ self .infer_flags = [1 , 1 , 1 ]
336+ self .out = self .input [1 , 0 :3 , 2 :4 , :]
337+
338+ def init_dtype (self ):
339+ self .dtype = np .float32
340+
341+ def set_npu (self ):
342+ self .__class__ .use_npu = True
343+ self .place = paddle .NPUPlace (0 )
344+
345+ def test_check_output (self ):
346+ self .check_output_with_place (self .place )
347+
348+ def test_check_grad_normal (self ):
349+ if self .dtype == np .float16 :
350+ return
351+ self .check_grad_with_place (self .place , ['Input' ], 'Out' )
352+
353+
354+ class TestSliceOpDecsDimFp16 (TestSliceOpDecsDim ):
355+ def init_dtype (self ):
356+ self .dtype = np .float16
357+
358+
359+ class TestSliceOpDecsDim2 (TestSliceOpDecsDim ):
360+ def config (self ):
361+ self .input = np .random .random ([3 , 4 , 5 , 6 ]).astype (self .dtype )
362+ self .starts = [1 , 0 , 2 ]
363+ self .ends = [2 , 1 , 4 ]
364+ self .axes = [0 , 1 , 2 ]
365+ self .decrease_axis = [0 , 1 ]
366+ self .infer_flags = [1 , 1 , 1 ]
367+ self .out = self .input [1 , 0 , 2 :4 , :]
368+
369+
370+ class TestSliceOpDecsDim3 (TestSliceOpDecsDim ):
371+ def config (self ):
372+ self .input = np .random .random ([3 , 4 , 5 , 6 ]).astype (self .dtype )
373+ self .starts = [- 1 , 0 , 2 ]
374+ self .ends = [1000000 , 1 , 4 ]
375+ self .axes = [0 , 1 , 2 ]
376+ self .decrease_axis = [0 , 1 ]
377+ self .infer_flags = [1 , 1 , 1 ]
378+ self .out = self .input [- 1 , 0 , 2 :4 , :]
379+
380+
381+ class TestSliceOpDecsDim4 (TestSliceOpDecsDim ):
382+ def config (self ):
383+ self .input = np .random .random ([3 , 4 , 5 , 7 ]).astype (self .dtype )
384+ self .starts = [0 , 1 , 2 , 3 ]
385+ self .ends = [1 , 2 , 3 , 4 ]
386+ self .axes = [0 , 1 , 2 , 3 ]
387+ self .decrease_axis = [0 , 1 , 2 , 3 ]
388+ self .infer_flags = [1 , 1 , 1 ]
389+ self .out = self .input [0 , 1 , 2 , 3 :4 ]
390+
391+
392+ class TestSliceOpDecsDim5 (TestSliceOpDecsDim ):
393+ def config (self ):
394+ self .input = np .random .random ([3 , 4 , 5 , 6 ]).astype (self .dtype )
395+ self .starts = [- 1 ]
396+ self .ends = [1000000 ]
397+ self .axes = [3 ]
398+ self .decrease_axis = [3 ]
399+ self .infer_flags = [1 , 1 , 1 ]
400+ self .out = self .input [:, :, :, - 1 ]
401+
402+
403+ class TestSliceOpDecsDim6 (TestSliceOpDecsDim ):
404+ def config (self ):
405+ self .input = np .random .random ([3 , 4 , 5 , 6 ]).astype (self .dtype )
406+ self .starts = [0 , 1 , 2 , 3 ]
407+ self .ends = [1 , 2 , 3 , 4 ]
408+ self .axes = [0 , 1 , 2 , 3 ]
409+ self .decrease_axis = [0 , 1 , 2 , 3 ]
410+ self .infer_flags = [1 , 1 , 1 ]
411+ self .out = self .input [0 , 1 , 2 , 3 :4 ]
412+
413+
414+ class TestSliceOpDecsDimStartsTensor (TestSliceOpDecsDim ):
415+ def set_inputs (self ):
416+ self .inputs = {
417+ 'Input' : self .input ,
418+ "StartsTensor" : np .array (
419+ self .starts , dtype = 'int32' )
420+ }
421+
422+ def set_attrs (self ):
423+ self .attrs = {
424+ 'axes' : self .axes ,
425+ #'starts': self.starts,
426+ 'ends' : self .ends ,
427+ 'infer_flags' : self .infer_flags ,
428+ 'decrease_axis' : self .decrease_axis ,
429+ }
430+
431+ def config (self ):
432+ self .input = np .random .random ([3 , 4 , 5 , 6 ]).astype (self .dtype )
433+ self .starts = [1 , 0 , 2 ]
434+ self .ends = [2 , 3 , 4 ]
435+ self .axes = [0 , 1 , 2 ]
436+ self .decrease_axis = [0 ]
437+ self .infer_flags = [- 1 , - 1 , - 1 ]
438+ self .out = self .input [1 , 0 :3 , 2 :4 , :]
439+
440+
441+ class TestSliceOpDecsDimStartsTensorFP16 (TestSliceOpDecsDimStartsTensor ):
442+ def init_dtype (self ):
443+ self .dtype = np .float16
444+
445+
446+ class TestSliceOpDecsDimStartsTensorStartsAndEndsTensor (TestSliceOpDecsDim ):
447+ def set_inputs (self ):
448+ self .inputs = {
449+ 'Input' : self .input ,
450+ "StartsTensor" : np .array (
451+ self .starts , dtype = 'int64' ),
452+ "EndsTensor" : np .array (
453+ self .ends , dtype = 'int32' )
454+ }
455+
456+ def set_attrs (self ):
457+ self .attrs = {
458+ 'axes' : self .axes ,
459+ #'starts': self.starts,
460+ #'ends': self.ends,
461+ 'infer_flags' : self .infer_flags ,
462+ 'decrease_axis' : self .decrease_axis ,
463+ }
464+
465+ def config (self ):
466+ self .input = np .random .random ([3 , 4 , 5 , 6 ]).astype (self .dtype )
467+ self .starts = [1 , 0 , 2 ]
468+ self .ends = [2 , 1 , 4 ]
469+ self .axes = [0 , 1 , 2 ]
470+ self .decrease_axis = [0 , 1 ]
471+ self .infer_flags = [- 1 , - 1 , - 1 ]
472+ self .out = self .input [1 , 0 , 2 :4 , :]
473+
474+
475+ class TestSliceOpDecsDimStartsTensorStartsAndEndsTensorFP16 (
476+ TestSliceOpDecsDimStartsTensorStartsAndEndsTensor ):
477+ def init_dtype (self ):
478+ self .dtype = np .float16
479+
480+
481+ class TestSliceOpDecsDimStartsListTensor (TestSliceOpDecsDim ):
482+ def set_inputs (self ):
483+ starts_tensor = []
484+ for index , ele in enumerate (self .starts ):
485+ starts_tensor .append (("x" + str (index ), np .ones (
486+ (1 )).astype ('int32' ) * ele ))
487+
488+ self .inputs = {'Input' : self .input , 'StartsTensorList' : starts_tensor }
489+
490+ def set_attrs (self ):
491+ self .attrs = {
492+ 'axes' : self .axes ,
493+ 'starts' : self .starts_infer ,
494+ 'ends' : self .ends ,
495+ 'infer_flags' : self .infer_flags ,
496+ 'decrease_axis' : self .decrease_axis ,
497+ }
498+
499+ def config (self ):
500+ self .input = np .random .random ([3 , 4 , 5 , 6 ]).astype (self .dtype )
501+ self .starts = [1 , 0 , 2 ]
502+ self .ends = [2 , 3 , 4 ]
503+ self .axes = [0 , 1 , 2 ]
504+ self .decrease_axis = [0 ]
505+ self .infer_flags = [1 , - 1 , 1 ]
506+ self .out = self .input [1 , 0 :3 , 2 :4 , :]
507+
508+ self .starts_infer = [1 , - 1 , 2 ]
509+
510+
511+ class TestSliceOpDecsDimStartsListTensor2 (TestSliceOpDecsDimStartsListTensor ):
512+ def config (self ):
513+ self .input = np .random .random ([3 , 4 , 5 , 6 ]).astype (self .dtype )
514+ self .starts = [- 1 ]
515+ self .ends = [1000000 ]
516+ self .axes = [3 ]
517+ self .decrease_axis = [3 ]
518+ self .infer_flags = [- 1 ]
519+ self .out = self .input [:, :, :, - 1 ]
520+
521+ self .starts_infer = [- 1 ]
522+
523+
524+ class TestSliceOpDecsDimStartsListTensorFP16 (
525+ TestSliceOpDecsDimStartsListTensor ):
526+ def init_dtype (self ):
527+ self .dtype = np .float16
528+
529+
304530if __name__ == '__main__' :
305531 unittest .main ()
0 commit comments