From 8c1c72af4d44a098d09fd83ab8c938d5bba58213 Mon Sep 17 00:00:00 2001 From: Wilber Date: Wed, 27 Oct 2021 11:06:24 +0800 Subject: [PATCH] =?UTF-8?q?enable=20trt=20test=20check=20and=20fix=20trt?= =?UTF-8?q?=20ut=20error=EF=BC=883/3=EF=BC=89=20(#36581)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- paddle/fluid/framework/ir/graph_viz_pass.cc | 4 ++ .../inference/analysis/ir_pass_manager.cc | 14 ++++-- paddle/fluid/inference/api/analysis_config.cc | 45 ++++++++++++++----- paddle/scripts/paddle_build.sh | 23 ++++++++++ .../ir/inference/test_trt_convert_conv2d.py | 1 + .../test_trt_convert_conv2d_fusion.py | 1 + .../test_trt_convert_conv2d_transpose.py | 1 + .../test_trt_convert_depthwise_conv2d.py | 1 + ..._trt_convert_depthwise_conv2d_transpose.py | 1 + .../test_trt_convert_nearest_interp_v2.py | 1 + .../inference/test_trt_convert_reduce_mean.py | 4 +- .../inference/test_trt_convert_reduce_sum.py | 4 +- .../ir/inference/trt_layer_auto_scan_test.py | 8 ++-- 13 files changed, 87 insertions(+), 21 deletions(-) diff --git a/paddle/fluid/framework/ir/graph_viz_pass.cc b/paddle/fluid/framework/ir/graph_viz_pass.cc index f2c711fb6f0..735b433b6cf 100644 --- a/paddle/fluid/framework/ir/graph_viz_pass.cc +++ b/paddle/fluid/framework/ir/graph_viz_pass.cc @@ -62,10 +62,14 @@ void GraphVizPass::ApplyImpl(ir::Graph* graph) const { } } } + const std::string& optim_cache_dir = Get("optim_cache_dir"); std::string program_bytes = program_desc.Proto()->SerializeAsString(); // rename from "17_ir_fc_fuse_pass.dot" to "fc_fuse_pass.pdmodel" program_path = graph_viz_path.substr(found1 + 4, found2 - found1 - 4) + ".pdmodel"; + if (!optim_cache_dir.empty()) { + program_path = optim_cache_dir + "/" + program_path; + } std::ofstream file(program_path.c_str(), std::ios::binary); file.write(program_bytes.c_str(), program_bytes.size()); file.close(); diff --git a/paddle/fluid/inference/analysis/ir_pass_manager.cc b/paddle/fluid/inference/analysis/ir_pass_manager.cc index d996474f3d6..dcbbee97a77 100644 --- a/paddle/fluid/inference/analysis/ir_pass_manager.cc +++ b/paddle/fluid/inference/analysis/ir_pass_manager.cc @@ -56,10 +56,18 @@ void IRPassManager::CreatePasses(Argument *argument, auto pass = framework::ir::PassRegistry::Instance().Get(pass_name); if (pass_name == "graph_viz_pass") { - std::string dot_file_path = std::to_string(pass_num) + "_ir_" + - (pre_pass.empty() ? "origin" : pre_pass) + - ".dot"; + std::string optim_cache_dir = argument->optim_cache_dir(); + std::string dot_file_path; + if (optim_cache_dir.empty()) { + dot_file_path = std::to_string(pass_num) + "_ir_" + + (pre_pass.empty() ? "origin" : pre_pass) + ".dot"; + } else { + dot_file_path = optim_cache_dir + "/" + std::to_string(pass_num) + + "_ir_" + (pre_pass.empty() ? "origin" : pre_pass) + + ".dot"; + } pass->Set("graph_viz_path", new std::string(std::move(dot_file_path))); + pass->Set("optim_cache_dir", new std::string(std::move(optim_cache_dir))); pass_num++; } else if (pass_name == "mkldnn_placement_pass") { pass->Set("mkldnn_enabled_op_types", diff --git a/paddle/fluid/inference/api/analysis_config.cc b/paddle/fluid/inference/api/analysis_config.cc index 5d056e054f5..0440801cfc5 100644 --- a/paddle/fluid/inference/api/analysis_config.cc +++ b/paddle/fluid/inference/api/analysis_config.cc @@ -12,7 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include +#include #include "paddle/fluid/inference/api/paddle_analysis_config.h" #include "paddle/fluid/inference/api/paddle_pass_builder.h" #include "paddle/fluid/inference/utils/table_printer.h" @@ -20,6 +22,10 @@ #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/gpu_info.h" +#ifdef PADDLE_WITH_TENSORRT +#include "paddle/fluid/inference/tensorrt/helper.h" +#endif + #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) DECLARE_uint64(initial_gpu_memory_in_mb); #endif @@ -758,17 +764,6 @@ std::string AnalysisConfig::Summary() { {"mkldnn_cache_capacity", std::to_string(mkldnn_cache_capacity_)}); os.InsetDivider(); - auto Precision2String = - [](paddle::AnalysisConfig::Precision prec) -> std::string { - if (prec == Precision::kFloat32) - return "fp32"; - else if (prec == Precision::kHalf) - return "fp16"; - else if (prec == Precision::kInt8) - return "int8"; - else - return "None"; - }; // gpu info os.InsertRow({"use_gpu", use_gpu_ ? "true" : "false"}); if (use_gpu_) { @@ -780,6 +775,33 @@ std::string AnalysisConfig::Summary() { os.InsertRow({"use_tensorrt", use_tensorrt_ ? "true" : "false"}); if (use_tensorrt_) { +#ifdef PADDLE_WITH_TENSORRT + auto Precision2String = + [](paddle::AnalysisConfig::Precision prec) -> std::string { + if (prec == Precision::kFloat32) + return "fp32"; + else if (prec == Precision::kHalf) + return "fp16"; + else if (prec == Precision::kInt8) + return "int8"; + else + return "None"; + }; + auto version2string = + [](const std::tuple &ver) -> std::string { + std::ostringstream os; + int major = std::get<0>(ver); + int minor = std::get<1>(ver); + int patch = std::get<2>(ver); + os << major << "." << minor << "." << patch; + return os.str(); + }; + os.InsertRow( + {"trt_compile_version", + version2string(inference::tensorrt::GetTrtCompileVersion())}); + os.InsertRow( + {"trt_runtime_version", + version2string(inference::tensorrt::GetTrtRuntimeVersion())}); os.InsertRow({"tensorrt_precision_mode", Precision2String(tensorrt_precision_mode_)}); os.InsertRow({"tensorrt_workspace_size", @@ -805,6 +827,7 @@ std::string AnalysisConfig::Summary() { if (trt_use_dla_) { os.InsertRow({"tensorrt_dla_core", std::to_string(trt_dla_core_)}); } +#endif } } os.InsetDivider(); diff --git a/paddle/scripts/paddle_build.sh b/paddle/scripts/paddle_build.sh index 9bdd9e14d58..68257a8490d 100755 --- a/paddle/scripts/paddle_build.sh +++ b/paddle/scripts/paddle_build.sh @@ -2388,6 +2388,25 @@ function find_temporary_files() { fi } +function trt_convert_test() { + set +e + cd ${PADDLE_ROOT} + result_num=0 + export PYTHONPATH=$PYTHONPATH:${PADDLE_ROOT}/build/python + for file_name in `find python/ -name 'test_trt_convert*'`;do + echo "----- test trt ut: $file_name -----" + python $file_name + res=$? + if [ "$res" != "0" ];then + echo "$file_name convert test failed " >&2 + result_num=11 + fi + done + if [ "$result_num" != "0" ];then + exit 11 + fi +} + function build_pr_and_develop() { cmake_gen_and_build ${PYTHON_ABI:-""} ${parallel_number} mkdir ${PADDLE_ROOT}/build/pr_whl && cp ${PADDLE_ROOT}/build/python/dist/*.whl ${PADDLE_ROOT}/build/pr_whl @@ -2656,6 +2675,10 @@ function main() { test_model_benchmark) test_model_benchmark ;; + trt_convert_test) + # only test trt convert. + trt_convert_test + ;; *) print_usage exit 1 diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_conv2d.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_conv2d.py index fd4b5ad9a72..47265245235 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_conv2d.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_conv2d.py @@ -15,6 +15,7 @@ from trt_layer_auto_scan_test import TrtLayerAutoScanTest, SkipReasons from program_config import TensorConfig, ProgramConfig import numpy as np +import unittest import paddle.inference as paddle_infer from functools import partial from typing import Optional, List, Callable, Dict, Any, Set diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_conv2d_fusion.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_conv2d_fusion.py index 9fcbda4443d..d811f3eac49 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_conv2d_fusion.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_conv2d_fusion.py @@ -18,6 +18,7 @@ import numpy as np import paddle.inference as paddle_infer from functools import partial from typing import Optional, List, Callable, Dict, Any, Set +import unittest class TrtConvertConv2dFusionTest(TrtLayerAutoScanTest): diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_conv2d_transpose.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_conv2d_transpose.py index 2c8f2592a73..e21d67839eb 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_conv2d_transpose.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_conv2d_transpose.py @@ -15,6 +15,7 @@ from trt_layer_auto_scan_test import TrtLayerAutoScanTest, SkipReasons from program_config import TensorConfig, ProgramConfig import numpy as np +import unittest import paddle.inference as paddle_infer from functools import partial from typing import Optional, List, Callable, Dict, Any, Set diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_depthwise_conv2d.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_depthwise_conv2d.py index fc2358bb116..b87b33d3557 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_depthwise_conv2d.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_depthwise_conv2d.py @@ -18,6 +18,7 @@ import numpy as np import paddle.inference as paddle_infer from functools import partial from typing import Optional, List, Callable, Dict, Any, Set +import unittest class TrtConvertDepthwiseConv2dTest(TrtLayerAutoScanTest): diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_depthwise_conv2d_transpose.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_depthwise_conv2d_transpose.py index 2fcd2bf5aca..66a007f64b6 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_depthwise_conv2d_transpose.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_depthwise_conv2d_transpose.py @@ -18,6 +18,7 @@ import numpy as np import paddle.inference as paddle_infer from functools import partial from typing import Optional, List, Callable, Dict, Any, Set +import unittest class TrtConvertDepthwiseConv2dTransposeTest(TrtLayerAutoScanTest): diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_nearest_interp_v2.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_nearest_interp_v2.py index 0c7715c9570..57d7d70c66a 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_nearest_interp_v2.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_nearest_interp_v2.py @@ -18,6 +18,7 @@ import numpy as np import paddle.inference as paddle_infer from functools import partial from typing import Optional, List, Callable, Dict, Any, Set +import unittest class TrtConvertNearestInterpV2Test(TrtLayerAutoScanTest): diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_reduce_mean.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_reduce_mean.py index 6c4c2ef4e1a..b09ae80555e 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_reduce_mean.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_reduce_mean.py @@ -120,7 +120,7 @@ class TrtConvertReduceMeanTest(TrtLayerAutoScanTest): attrs, False), 1e-5 self.trt_param.precision = paddle_infer.PrecisionType.Half yield self.create_inference_config(), generate_trt_nodes_num( - attrs, False), (1e-5, 1e-5) + attrs, False), (1e-4, 1e-4) # for dynamic_shape generate_dynamic_shape(attrs) @@ -129,7 +129,7 @@ class TrtConvertReduceMeanTest(TrtLayerAutoScanTest): True), 1e-5 self.trt_param.precision = paddle_infer.PrecisionType.Half yield self.create_inference_config(), generate_trt_nodes_num( - attrs, True), (1e-5, 1e-5) + attrs, True), (1e-4, 1e-4) pass diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_reduce_sum.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_reduce_sum.py index 1cc9defa101..ba0f61a2768 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_reduce_sum.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_reduce_sum.py @@ -120,7 +120,7 @@ class TrtConvertReduceSumTest(TrtLayerAutoScanTest): attrs, False), (1e-5, 1e-5) self.trt_param.precision = paddle_infer.PrecisionType.Half yield self.create_inference_config(), generate_trt_nodes_num( - attrs, False), (1e-5, 1e-5) + attrs, False), (1e-4, 1e-4) # for dynamic_shape generate_dynamic_shape(attrs) @@ -129,7 +129,7 @@ class TrtConvertReduceSumTest(TrtLayerAutoScanTest): attrs, True), (1e-5, 1e-5) self.trt_param.precision = paddle_infer.PrecisionType.Half yield self.create_inference_config(), generate_trt_nodes_num( - attrs, True), (1e-5, 1e-5) + attrs, True), (1e-4, 1e-4) pass diff --git a/python/paddle/fluid/tests/unittests/ir/inference/trt_layer_auto_scan_test.py b/python/paddle/fluid/tests/unittests/ir/inference/trt_layer_auto_scan_test.py index edd033f28c0..941641da7a3 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/trt_layer_auto_scan_test.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/trt_layer_auto_scan_test.py @@ -122,7 +122,8 @@ class TrtLayerAutoScanTest(AutoScanTest): "Output has diff between GPU and TensorRT. ") def assert_op_size(self, trt_engine_num, paddle_op_num): - last_passed_program = 'transpose_flatten_concat_fuse_pass.pdmodel' + last_passed_program = os.path.join( + self.trt_cache_dir, 'transpose_flatten_concat_fuse_pass.pdmodel') model_bytes = paddle.static.load_from_file(last_passed_program) pg = paddle.static.deserialize_program(model_bytes) main_block = pg.desc.block(0) @@ -179,7 +180,8 @@ class TrtLayerAutoScanTest(AutoScanTest): def run_test(self, quant=False): status = True - np.random.seed(int(1000 * time.time()) % 2**32) + # Choose different tests by week + np.random.seed(int(time.strftime("%W"))) run_flags = [] for prog_config in self.sample_program_configs(): # In CI, only run 30% cases @@ -283,4 +285,4 @@ class TrtLayerAutoScanTest(AutoScanTest): self.success_log('RUN ' + str(prog_config) + ' vs ' + self.inference_config_str(pred_config)) - # self.assertTrue(status) + self.assertTrue(status) -- GitLab