Skip to content

Commit a781b7f

Browse files
[NPU][MLU] Fix test_conv2d_transpose on NPU & MLU (#1607)
1 parent 8b20ca3 commit a781b7f

File tree

2 files changed

+140
-84
lines changed

2 files changed

+140
-84
lines changed

backends/mlu/tests/unittests/test_conv2d_transposed_op_mlu.py

Lines changed: 93 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,12 @@ def setUp(self):
172172
self.outputs = {"Output": output}
173173

174174
def set_mlu(self):
175+
self.device = "mlu:0"
176+
paddle.set_device(self.device)
175177
self.__class__.use_custom_device = True
176-
self.place = paddle.CustomPlace("mlu", 0)
178+
self.place = paddle.CustomPlace(
179+
self.device.split(":")[0], int(self.device.split(":")[1])
180+
)
177181

178182
def test_check_output(self):
179183
self.check_output_with_place(self.place, atol=1e-2)
@@ -429,8 +433,12 @@ def init_test_case(self):
429433
self.filter_size = [f_c, 6, 3, 3]
430434

431435
def set_mlu(self):
436+
self.device = "mlu:0"
437+
paddle.set_device(self.device)
432438
self.__class__.use_custom_device = True
433-
self.place = paddle.CustomPlace("mlu", 0)
439+
self.place = paddle.CustomPlace(
440+
self.device.split(":")[0], int(self.device.split(":")[1])
441+
)
434442

435443
def init_op_type(self):
436444
self.need_check_grad = False
@@ -485,58 +493,70 @@ def setUp(self):
485493
self.set_mlu()
486494

487495
def set_mlu(self):
496+
self.device = "mlu:0"
497+
paddle.set_device(self.device)
488498
self.__class__.use_custom_device = True
489-
self.place = paddle.CustomPlace("mlu", 0)
499+
self.place = paddle.CustomPlace(
500+
self.device.split(":")[0], int(self.device.split(":")[1])
501+
)
490502

491503
def test_case1(self):
492504
data1 = paddle.static.data(name="data1", shape=[-1, 3, 5, 5], dtype="float32")
493505
data2 = paddle.static.data(name="data2", shape=[-1, 5, 5, 3], dtype="float32")
494-
out1 = paddle.static.nn.conv2d_transpose(
495-
input=data1, groups=1, num_filters=6, filter_size=3, data_format="NCHW"
496-
)
497-
out2 = paddle.static.nn.conv2d_transpose(
498-
input=data2, groups=1, num_filters=6, filter_size=3, data_format="NHWC"
499-
)
500-
out3 = paddle.static.nn.conv2d_transpose(
501-
input=data1,
506+
out1 = paddle.nn.Conv2DTranspose(
507+
in_channels=3,
508+
out_channels=6,
509+
kernel_size=3,
510+
groups=1,
511+
data_format="NCHW",
512+
)(data1)
513+
out2 = paddle.nn.Conv2DTranspose(
514+
in_channels=3,
515+
out_channels=6,
516+
kernel_size=3,
517+
groups=1,
518+
data_format="NHWC",
519+
)(data2)
520+
out3 = paddle.nn.Conv2DTranspose(
521+
in_channels=5,
522+
out_channels=6,
523+
kernel_size=3,
502524
groups=1,
503-
num_filters=6,
504-
filter_size=3,
505525
padding=[[0, 0], [1, 1], [1, 1], [0, 0]],
506526
data_format="NHWC",
507-
)
508-
out4 = paddle.static.nn.conv2d_transpose(
509-
input=data1,
527+
)(data1)
528+
out4 = paddle.nn.Conv2DTranspose(
529+
in_channels=3,
530+
out_channels=6,
531+
kernel_size=3,
510532
groups=3,
511-
num_filters=6,
512-
filter_size=3,
513533
padding=[[0, 0], [0, 0], [2, 1], [0, 0]],
514534
data_format="NCHW",
515-
)
516-
out5 = paddle.static.nn.conv2d_transpose(
517-
input=data2,
535+
)(data1)
536+
out5 = paddle.nn.Conv2DTranspose(
537+
in_channels=5,
538+
out_channels=6,
539+
kernel_size=3,
518540
groups=1,
519-
num_filters=6,
520-
filter_size=3,
521541
padding="SAME",
522542
data_format="NCHW",
523-
)
524-
out6 = paddle.static.nn.conv2d_transpose(
525-
input=data1,
543+
)(data2)
544+
out6 = paddle.nn.Conv2DTranspose(
545+
in_channels=5,
546+
out_channels=6,
547+
kernel_size=3,
526548
groups=1,
527-
num_filters=6,
528-
filter_size=3,
529549
padding="VALID",
530550
data_format="NHWC",
531-
)
532-
out7 = paddle.static.nn.conv2d_transpose(
533-
input=data1,
551+
)(data1)
552+
out7 = paddle.nn.Conv2DTranspose(
553+
in_channels=5,
554+
out_channels=6,
555+
kernel_size=[5, 3],
534556
groups=1,
535-
num_filters=6,
536-
output_size=[7, 7],
537557
padding=[0, 0],
538558
data_format="NHWC",
539-
)
559+
)(data1, [7, 7])
540560

541561
data1_np = np.random.random((2, 3, 5, 5)).astype("float32")
542562
data2_np = np.random.random((2, 5, 5, 3)).astype("float32")
@@ -563,46 +583,50 @@ def setUp(self):
563583
self.set_mlu()
564584

565585
def set_mlu(self):
586+
self.device = "mlu:0"
587+
paddle.set_device(self.device)
566588
self.__class__.use_custom_device = True
567-
self.place = paddle.CustomPlace("mlu", 0)
589+
self.place = paddle.CustomPlace(
590+
self.device.split(":")[0], int(self.device.split(":")[1])
591+
)
568592

569593
def test_exception(self):
570594
data = paddle.static.data(name="data", shape=[-1, 3, 5, 5], dtype="float32")
571595

572596
def attr_data_format():
573-
out = paddle.static.nn.conv2d_transpose(
574-
input=data, groups=1, num_filters=6, filter_size=3, data_format="NCDHW"
575-
)
597+
out = paddle.nn.Conv2DTranspose(
598+
in_channels=3, out_channels=6, kernel_size=3, data_format="NCDHW"
599+
)(data)
576600

577601
self.assertRaises(ValueError, attr_data_format)
578602

579603
def attr_padding_str():
580-
out = paddle.static.nn.conv2d_transpose(
581-
input=data, groups=1, num_filters=6, filter_size=3, padding="Vald"
582-
)
604+
out = paddle.nn.Conv2DTranspose(
605+
in_channels=3, out_channels=6, kernel_size=3, padding="Vald"
606+
)(data)
583607

584608
self.assertRaises(ValueError, attr_padding_str)
585609

586610
def attr_padding_list():
587-
out = paddle.static.nn.conv2d_transpose(
588-
input=data,
611+
out = paddle.nn.Conv2DTranspose(
612+
in_channels=3,
589613
groups=1,
590-
num_filters=6,
591-
filter_size=3,
614+
out_channels=6,
615+
kernel_size=3,
592616
padding=[[1, 1], [1, 1], [0, 0], [0, 0]],
593-
)
617+
)(data)
594618

595619
self.assertRaises(ValueError, attr_padding_list)
596620

597621
def attr_padding_with_data_format():
598-
out = paddle.static.nn.conv2d_transpose(
599-
input=data,
622+
out = paddle.nn.Conv2DTranspose(
623+
in_channels=5,
600624
groups=1,
601-
num_filters=6,
602-
filter_size=3,
625+
out_channels=6,
626+
kernel_size=3,
603627
padding=[[1, 1], [0, 0], [0, 0], [1, 1]],
604628
data_format="NHWC",
605-
)
629+
)(data)
606630

607631
self.assertRaises(ValueError, attr_padding_with_data_format)
608632

@@ -611,27 +635,37 @@ def attr_padding_with_data_format():
611635
)
612636

613637
def error_input_size():
614-
out = paddle.static.nn.conv2d_transpose(
615-
input=error_input, groups=1, num_filters=6, filter_size=3
616-
)
638+
out = paddle.nn.Conv2DTranspose(
639+
in_channels=1,
640+
groups=1,
641+
out_channels=6,
642+
kernel_size=3,
643+
)(error_input)
617644

618645
self.assertRaises(ValueError, error_input_size)
619646

620647
def error_groups():
621-
out = paddle.static.nn.conv2d_transpose(
622-
input=data, groups=0, num_filters=6, filter_size=3, data_format="NHWC"
623-
)
648+
out = paddle.nn.Conv2DTranspose(
649+
in_channels=5,
650+
groups=0,
651+
out_channels=6,
652+
kernel_size=3,
653+
)(data)
624654

625-
self.assertRaises(ValueError, error_groups)
655+
self.assertRaises(ZeroDivisionError, error_groups)
626656

627657

628658
class TestConv2DTransposeRepr(unittest.TestCase):
629659
def setUp(self):
630660
self.set_mlu()
631661

632662
def set_mlu(self):
663+
self.device = "mlu:0"
664+
paddle.set_device(self.device)
633665
self.__class__.use_custom_device = True
634-
self.place = paddle.CustomPlace("mlu", 0)
666+
self.place = paddle.CustomPlace(
667+
self.device.split(":")[0], int(self.device.split(":")[1])
668+
)
635669

636670
def test_case(self):
637671
paddle.disable_static()

backends/npu/tests/unittests/test_conv2d_transpose_op_npu.py

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ def test_case1(self):
580580
data1_np = np.random.random((2, 3, 5, 5)).astype("float32")
581581
data2_np = np.random.random((2, 5, 5, 3)).astype("float32")
582582

583-
place = core.CustomPlace("npu", 0)
583+
place = core.CustomPlace("custom_cpu", 0)
584584
exe = base.Executor(place)
585585
exe.run(base.default_startup_program())
586586
results = exe.run(
@@ -1017,52 +1017,74 @@ class TestConv2DTransposeAPI(unittest.TestCase):
10171017
def test_case1(self):
10181018
data1 = paddle.static.data(name="data1", shape=[-1, 3, 5, 5], dtype="float32")
10191019
data2 = paddle.static.data(name="data2", shape=[-1, 5, 5, 3], dtype="float32")
1020-
out1 = paddle.static.nn.conv2d_transpose(
1021-
input=data1, groups=3, num_filters=6, filter_size=3, data_format="NCHW"
1020+
1021+
conv1 = paddle.nn.Conv2DTranspose(
1022+
in_channels=data1.shape[1],
1023+
out_channels=6,
1024+
kernel_size=3,
1025+
groups=3,
1026+
data_format="NCHW",
10221027
)
1023-
out2 = paddle.static.nn.conv2d_transpose(
1024-
input=data2, groups=3, num_filters=6, filter_size=3, data_format="NHWC"
1028+
out1 = conv1(data1)
1029+
1030+
conv2 = paddle.nn.Conv2DTranspose(
1031+
in_channels=data2.shape[-1],
1032+
out_channels=6,
1033+
kernel_size=3,
1034+
groups=3,
1035+
data_format="NHWC",
10251036
)
1026-
out3 = paddle.static.nn.conv2d_transpose(
1027-
input=data1,
1037+
out2 = conv2(data2)
1038+
1039+
conv3 = paddle.nn.Conv2DTranspose(
1040+
in_channels=data1.shape[-1],
1041+
out_channels=5,
1042+
kernel_size=3,
10281043
groups=5,
1029-
num_filters=6,
1030-
filter_size=3,
10311044
padding=[[0, 0], [1, 1], [1, 1], [0, 0]],
10321045
data_format="NHWC",
10331046
)
1034-
out4 = paddle.static.nn.conv2d_transpose(
1035-
input=data1,
1047+
out3 = conv3(data1)
1048+
1049+
conv4 = paddle.nn.Conv2DTranspose(
1050+
in_channels=data1.shape[1],
1051+
out_channels=6,
1052+
kernel_size=3,
10361053
groups=3,
1037-
num_filters=6,
1038-
filter_size=3,
10391054
padding=[[0, 0], [0, 0], [2, 1], [0, 0]],
10401055
data_format="NCHW",
10411056
)
1042-
out5 = paddle.static.nn.conv2d_transpose(
1043-
input=data2,
1057+
out4 = conv4(data1)
1058+
1059+
conv5 = paddle.nn.Conv2DTranspose(
1060+
in_channels=data2.shape[1],
1061+
out_channels=10,
1062+
kernel_size=3,
10441063
groups=5,
1045-
num_filters=6,
1046-
filter_size=3,
10471064
padding="SAME",
10481065
data_format="NCHW",
10491066
)
1050-
out6 = paddle.static.nn.conv2d_transpose(
1051-
input=data1,
1067+
out5 = conv5(data2)
1068+
1069+
conv6 = paddle.nn.Conv2DTranspose(
1070+
in_channels=data1.shape[-1],
1071+
out_channels=5,
1072+
kernel_size=3,
10521073
groups=5,
1053-
num_filters=6,
1054-
filter_size=3,
10551074
padding="VALID",
10561075
data_format="NHWC",
10571076
)
1058-
out7 = paddle.static.nn.conv2d_transpose(
1059-
input=data1,
1077+
out6 = conv6(data1)
1078+
1079+
conv7 = paddle.nn.Conv2DTranspose(
1080+
in_channels=data1.shape[-1],
1081+
out_channels=10,
1082+
kernel_size=[5, 3],
10601083
groups=5,
1061-
num_filters=6,
1062-
output_size=[7, 7],
10631084
padding=[0, 0],
10641085
data_format="NHWC",
10651086
)
1087+
out7 = conv7(data1, output_size=[7, 7])
10661088

10671089
data1_np = np.random.random((2, 3, 5, 5)).astype("float32")
10681090
data2_np = np.random.random((2, 5, 5, 3)).astype("float32")

0 commit comments

Comments
 (0)