From 4c563e0bd8733086b9706cfeda2d8a8aa502559b Mon Sep 17 00:00:00 2001 From: kangguangli Date: Fri, 9 Dec 2022 13:14:25 +0800 Subject: [PATCH] replace cross_entropy except in python/paddle/fluid/tests/unittests/*.py && unittests/*/*.py (#48922) --- .../fleet/auto_parallel_parallelizer.py | 5 +++-- .../fleet/dist_mnist_gradient_merge.py | 4 +++- .../fleet/parallel_dygraph_se_resnext.py | 4 +++- .../collective/fleet/pipeline_mnist.py | 4 +++- .../fleet/pipeline_mnist_multi_device.py | 4 +++- .../fleet/pipeline_mnist_one_device.py | 4 +++- .../collective/fleet/test_fleet_checkpoint.py | 4 +++- .../test_imperative_auto_mixed_precision.py | 4 +++- ...perative_auto_mixed_precision_for_eager.py | 4 +++- .../test_trt_conv_quant_dequant_pass.py | 21 ++++++++++++++++--- .../test_trt_fc_fuse_quant_dequant_pass.py | 21 ++++++++++++++++--- .../test_trt_matmul_quant_dequant.py | 21 ++++++++++++++++--- 12 files changed, 81 insertions(+), 19 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/auto_parallel_parallelizer.py b/python/paddle/fluid/tests/unittests/collective/fleet/auto_parallel_parallelizer.py index ffb413fca2..b1c00c6105 100755 --- a/python/paddle/fluid/tests/unittests/collective/fleet/auto_parallel_parallelizer.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/auto_parallel_parallelizer.py @@ -22,7 +22,6 @@ import paddle.static as static import paddle.utils as utils from paddle.distributed import fleet from paddle.distributed.fleet import auto -from paddle.fluid import layers paddle.enable_static() _global_parallel_strategy = None @@ -93,7 +92,9 @@ def mlp_pretrain_forward(train_program, start_program): predict = mlp(input) - cost = layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) return avg_cost, train_program, start_program diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/dist_mnist_gradient_merge.py b/python/paddle/fluid/tests/unittests/collective/fleet/dist_mnist_gradient_merge.py index 85394ea89d..01d6d970c6 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/dist_mnist_gradient_merge.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/dist_mnist_gradient_merge.py @@ -34,7 +34,9 @@ class TestDistMnist2x2(TestDistRunnerBase): # Train program predict = cnn_model(images) - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) # Evaluator diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_se_resnext.py b/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_se_resnext.py index f484a79300..9515c987a5 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_se_resnext.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_se_resnext.py @@ -350,7 +350,9 @@ class TestSeResNeXt(TestParallelDyGraphRunnerBase): out = model(img) softmax_out = paddle.nn.functional.softmax(out, use_cudnn=False) - loss = fluid.layers.cross_entropy(input=softmax_out, label=label) + loss = paddle.nn.functional.cross_entropy( + input=softmax_out, label=label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(x=loss) return avg_loss diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/pipeline_mnist.py b/python/paddle/fluid/tests/unittests/collective/fleet/pipeline_mnist.py index 4530d8e242..f974709ce8 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/pipeline_mnist.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/pipeline_mnist.py @@ -100,7 +100,9 @@ class TestDistMnist2x2(TestDistRunnerBase): # Train program predict = cnn_model(images) with fluid.device_guard("gpu:1"): - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) # Evaluator diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/pipeline_mnist_multi_device.py b/python/paddle/fluid/tests/unittests/collective/fleet/pipeline_mnist_multi_device.py index 0b75b034ce..ecc71abe62 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/pipeline_mnist_multi_device.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/pipeline_mnist_multi_device.py @@ -100,7 +100,9 @@ class TestDistMnist2x2(TestDistRunnerBase): # Train program predict = cnn_model(images) with fluid.device_guard("gpu:1"): - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) # Evaluator diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/pipeline_mnist_one_device.py b/python/paddle/fluid/tests/unittests/collective/fleet/pipeline_mnist_one_device.py index 5b1e590fc0..a6971e210a 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/pipeline_mnist_one_device.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/pipeline_mnist_one_device.py @@ -92,7 +92,9 @@ class TestDistMnist2x2(TestDistRunnerBase): # Train program predict = cnn_model(images) with fluid.device_guard("gpu:0"): - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) # Evaluator diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/test_fleet_checkpoint.py b/python/paddle/fluid/tests/unittests/collective/fleet/test_fleet_checkpoint.py index d1e24149c2..b030b61214 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/test_fleet_checkpoint.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/test_fleet_checkpoint.py @@ -41,7 +41,9 @@ class FleetTest(unittest.TestCase): feed_list=[image, label], place=fluid.CPUPlace() ) predict = fluid.layers.fc(input=image, size=10, act='softmax') - loss = fluid.layers.cross_entropy(input=predict, label=label) + loss = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) optimizer = fluid.optimizer.AdamOptimizer(learning_rate=0.001) diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/test_imperative_auto_mixed_precision.py b/python/paddle/fluid/tests/unittests/collective/fleet/test_imperative_auto_mixed_precision.py index 3141ed81a4..d30466d9fc 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/test_imperative_auto_mixed_precision.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/test_imperative_auto_mixed_precision.py @@ -1308,7 +1308,9 @@ class TestResnet(unittest.TestCase): ): out = resnet(img) - loss = fluid.layers.cross_entropy(input=out, label=label) + loss = paddle.nn.functional.cross_entropy( + input=out, label=label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(x=loss) dy_out = avg_loss.numpy() diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/test_imperative_auto_mixed_precision_for_eager.py b/python/paddle/fluid/tests/unittests/collective/fleet/test_imperative_auto_mixed_precision_for_eager.py index dbcdf3ac46..f688d28b85 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/test_imperative_auto_mixed_precision_for_eager.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/test_imperative_auto_mixed_precision_for_eager.py @@ -1298,7 +1298,9 @@ class TestResnet(unittest.TestCase): ): out = resnet(img) - loss = fluid.layers.cross_entropy(input=out, label=label) + loss = paddle.nn.functional.cross_entropy( + input=out, label=label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(x=loss) dy_out = avg_loss.numpy() diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_conv_quant_dequant_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_conv_quant_dequant_pass.py index 05244e3627..9451a214aa 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_conv_quant_dequant_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_conv_quant_dequant_pass.py @@ -53,7 +53,12 @@ class QuantDequantTensorRTSubgraphPassConvTest(QuantDequantTest): elif self.conv_groups == 4: cout = paddle.reshape(conv_out, shape=[1, 1, 10816]) result = fluid.layers.relu(cout) - loss = fluid.layers.cross_entropy(input=result, label=label_shape) + loss = paddle.nn.functional.cross_entropy( + input=result, + label=label_shape, + reduction='none', + use_softmax=False, + ) avg_loss = paddle.mean(loss) return avg_loss, result @@ -156,7 +161,12 @@ class DynamicShapeQuantDequantTensorRTSubgraphPassConvTest(QuantDequantTest): ) cout = paddle.reshape(conv_out, shape=[1, 1, 10816]) result = fluid.layers.relu(cout) - loss = fluid.layers.cross_entropy(input=result, label=label_shape) + loss = paddle.nn.functional.cross_entropy( + input=result, + label=label_shape, + reduction='none', + use_softmax=False, + ) avg_loss = paddle.mean(loss) return avg_loss, result @@ -257,7 +267,12 @@ class QuantDequantTensorRTSubgraphPassConvTransposeTest(QuantDequantTest): elif self.conv_groups == 4: cout = paddle.reshape(conv_out, shape=[1, 1, 10816]) result = fluid.layers.relu(cout) - loss = fluid.layers.cross_entropy(input=result, label=label_shape) + loss = paddle.nn.functional.cross_entropy( + input=result, + label=label_shape, + reduction='none', + use_softmax=False, + ) avg_loss = paddle.mean(loss) return avg_loss, result diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_fc_fuse_quant_dequant_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_fc_fuse_quant_dequant_pass.py index ca491a06c6..372a4ea23b 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_fc_fuse_quant_dequant_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_fc_fuse_quant_dequant_pass.py @@ -38,7 +38,12 @@ class FCQuantDequantFusePassTRTDims3Cols1Test(QuantDequantTest): act="relu", ) result = fluid.layers.relu(fc_out) - loss = fluid.layers.cross_entropy(input=result, label=self.label) + loss = paddle.nn.functional.cross_entropy( + input=result, + label=self.label, + reduction='none', + use_softmax=False, + ) avg_loss = paddle.mean(loss) return avg_loss, result @@ -105,7 +110,12 @@ class FCQuantDequantFusePassTRTDims3Cols2Test(QuantDequantTest): ) c_out = paddle.reshape(fc_out, shape=[0, 784]) result = fluid.layers.relu(c_out) - loss = fluid.layers.cross_entropy(input=result, label=self.label) + loss = paddle.nn.functional.cross_entropy( + input=result, + label=self.label, + reduction='none', + use_softmax=False, + ) avg_loss = paddle.mean(loss) return avg_loss, result @@ -174,7 +184,12 @@ class FCQuantDequantFusePassTRTDims3Cols3Test(QuantDequantTest): ) c_out = paddle.reshape(fc_out, shape=[1, 1, 2744]) result = fluid.layers.relu(c_out) - loss = fluid.layers.cross_entropy(input=result, label=label_shape) + loss = paddle.nn.functional.cross_entropy( + input=result, + label=label_shape, + reduction='none', + use_softmax=False, + ) avg_loss = paddle.mean(loss) return avg_loss, result diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_matmul_quant_dequant.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_matmul_quant_dequant.py index 4e2b3e0ae2..ca6a0d98d8 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_matmul_quant_dequant.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_matmul_quant_dequant.py @@ -47,7 +47,12 @@ class TensorRTMatMulQuantDequantDims3Test(QuantDequantTest): act=None, ) result = fluid.layers.relu(fc_out) - loss = fluid.layers.cross_entropy(input=result, label=self.label) + loss = paddle.nn.functional.cross_entropy( + input=result, + label=self.label, + reduction='none', + use_softmax=False, + ) avg_loss = paddle.mean(loss) return avg_loss, result @@ -144,7 +149,12 @@ class TensorRTMatMulQuantDequantDims4Test(QuantDequantTest): act=None, ) result = fluid.layers.relu(fc_out) - loss = fluid.layers.cross_entropy(input=result, label=self.label) + loss = paddle.nn.functional.cross_entropy( + input=result, + label=self.label, + reduction='none', + use_softmax=False, + ) avg_loss = paddle.mean(loss) return avg_loss, result @@ -240,7 +250,12 @@ class TensorRTMatMulQuantDequantDims3DynamicTest(QuantDequantTest): act=None, ) result = fluid.layers.relu(fc_out) - loss = fluid.layers.cross_entropy(input=result, label=self.label) + loss = paddle.nn.functional.cross_entropy( + input=result, + label=self.label, + reduction='none', + use_softmax=False, + ) avg_loss = paddle.mean(loss) return avg_loss, result -- GitLab