@@ -487,6 +487,153 @@ def test_errors(self):
487487 paddle .disable_static ()
488488
489489
490+ class TestClipAPI_Int (unittest .TestCase ):
491+ def _executed_api (self , x , min = None , max = None ):
492+ return paddle .clip (x , min , max )
493+
494+ def test_clip (self ):
495+ paddle .enable_static ()
496+ data_shape = [1 , 9 , 9 , 4 ]
497+ data = np .random .random (data_shape ).astype ('int32' )
498+ place = (
499+ base .CUDAPlace (0 )
500+ if base .core .is_compiled_with_cuda ()
501+ else base .CPUPlace ()
502+ )
503+ exe = base .Executor (place )
504+
505+ main = paddle .static .Program ()
506+ startup = paddle .static .Program ()
507+ with paddle .static .program_guard (main , startup ):
508+ images = paddle .static .data (
509+ name = 'image' , shape = data_shape , dtype = 'int32'
510+ )
511+ min = paddle .static .data (name = 'min' , shape = [1 ], dtype = 'float32' )
512+ max = paddle .static .data (name = 'max' , shape = [1 ], dtype = 'float32' )
513+ out_1 = self ._executed_api (images , min = min , max = max )
514+ out_2 = self ._executed_api (images , min = 2.2 , max = 8.9 )
515+ out_3 = self ._executed_api (images , min = 3.3 )
516+ out_4 = self ._executed_api (images , max = 4.7 )
517+ out_5 = self ._executed_api (images , min = min )
518+ out_6 = self ._executed_api (images , max = max )
519+ out_7 = self ._executed_api (images , max = - 1.0 )
520+ out_8 = self ._executed_api (images )
521+ out_9 = self ._executed_api (
522+ paddle .cast (images , 'int32' ), min = 2.2 , max = 8.9
523+ )
524+ out_10 = self ._executed_api (
525+ paddle .cast (images * 10 , 'int32' ), min = 2.8 , max = 8.8
526+ )
527+ out_11 = self ._executed_api (
528+ paddle .cast (images * 10 , 'int64' ), min = 2.8 , max = 8.8
529+ )
530+
531+ (
532+ res1 ,
533+ res2 ,
534+ res3 ,
535+ res4 ,
536+ res5 ,
537+ res6 ,
538+ res7 ,
539+ res8 ,
540+ res9 ,
541+ res10 ,
542+ res11 ,
543+ ) = exe .run (
544+ main ,
545+ feed = {
546+ "image" : data ,
547+ "min" : np .array ([2.2 ]).astype ('float32' ),
548+ "max" : np .array ([8.8 ]).astype ('float32' ),
549+ },
550+ fetch_list = [
551+ out_1 ,
552+ out_2 ,
553+ out_3 ,
554+ out_4 ,
555+ out_5 ,
556+ out_6 ,
557+ out_7 ,
558+ out_8 ,
559+ out_9 ,
560+ out_10 ,
561+ out_11 ,
562+ ],
563+ )
564+
565+ np .testing .assert_allclose (res1 , data .clip (2.2 , 8.8 ), rtol = 1e-05 )
566+ np .testing .assert_allclose (res2 , data .clip (2.2 , 8.9 ), rtol = 1e-05 )
567+ np .testing .assert_allclose (res3 , data .clip (min = 3.3 ), rtol = 1e-05 )
568+ np .testing .assert_allclose (res4 , data .clip (max = 4.7 ), rtol = 1e-05 )
569+ np .testing .assert_allclose (res5 , data .clip (min = 2.2 ), rtol = 1e-05 )
570+ np .testing .assert_allclose (res6 , data .clip (max = 8.8 ), rtol = 1e-05 )
571+ np .testing .assert_allclose (res7 , data .clip (max = - 1.0 ), rtol = 1e-05 )
572+ np .testing .assert_allclose (res8 , data , rtol = 1e-05 )
573+ np .testing .assert_allclose (
574+ res9 , data .astype (np .int32 ).clip (2.2 , 8.9 ), rtol = 1e-05
575+ )
576+ np .testing .assert_allclose (
577+ res10 , (data * 10 ).astype (np .int32 ).clip (2.8 , 8.8 ), rtol = 1e-05
578+ )
579+ np .testing .assert_allclose (
580+ res11 , (data * 10 ).astype (np .int64 ).clip (2.8 , 8.8 ), rtol = 1e-05
581+ )
582+ paddle .disable_static ()
583+
584+ def test_clip_dygraph (self ):
585+ paddle .disable_static ()
586+ place = (
587+ base .CUDAPlace (0 )
588+ if base .core .is_compiled_with_cuda ()
589+ else base .CPUPlace ()
590+ )
591+ paddle .disable_static (place )
592+ data_shape = [1 , 9 , 9 , 4 ]
593+ data = np .random .random (data_shape ).astype ('int32' )
594+ images = paddle .to_tensor (data , dtype = 'int32' )
595+ v_min = paddle .to_tensor (np .array ([2.2 ], dtype = np .float32 ))
596+ v_max = paddle .to_tensor (np .array ([8.8 ], dtype = np .float32 ))
597+
598+ out_1 = self ._executed_api (images , min = 2.2 , max = 8.8 )
599+ images = paddle .to_tensor (data , dtype = 'int32' )
600+ out_2 = self ._executed_api (images , min = 2.2 , max = 8.9 )
601+ images = paddle .to_tensor (data , dtype = 'int32' )
602+ out_3 = self ._executed_api (images , min = v_min , max = v_max )
603+
604+ out_4 = self ._executed_api (
605+ paddle .cast (images * 10 , 'int32' ), min = 2.2 , max = 8.8
606+ )
607+ out_5 = self ._executed_api (
608+ paddle .cast (images * 10 , 'int64' ), min = 2.2 , max = 8.8
609+ )
610+ # test with numpy.generic
611+ out_6 = self ._executed_api (images , min = np .abs (2.2 ), max = np .abs (8.8 ))
612+
613+ np .testing .assert_allclose (
614+ out_1 .numpy (), data .clip (2.2 , 8.8 ), rtol = 1e-05
615+ )
616+ np .testing .assert_allclose (
617+ out_2 .numpy (), data .clip (2.2 , 8.9 ), rtol = 1e-05
618+ )
619+ np .testing .assert_allclose (
620+ out_3 .numpy (), data .clip (2.2 , 8.8 ), rtol = 1e-05
621+ )
622+ np .testing .assert_allclose (
623+ out_4 .numpy (),
624+ (data * 10 ).astype (np .int32 ).clip (2.2 , 8.8 ),
625+ rtol = 1e-05 ,
626+ )
627+ np .testing .assert_allclose (
628+ out_5 .numpy (),
629+ (data * 10 ).astype (np .int64 ).clip (2.2 , 8.8 ),
630+ rtol = 1e-05 ,
631+ )
632+ np .testing .assert_allclose (
633+ out_6 .numpy (), data .clip (2.2 , 8.8 ), rtol = 1e-05
634+ )
635+
636+
490637class TestClipOpFp16 (unittest .TestCase ):
491638 def test_fp16 (self ):
492639 if base .core .is_compiled_with_cuda ():
0 commit comments