From e328ed89089f7fe84bb616b938d3c76de9bd39d1 Mon Sep 17 00:00:00 2001 From: xiebaiyuan Date: Thu, 24 Oct 2019 16:37:12 +0800 Subject: [PATCH] Develop performance (#2257) * optimise conv 1x1 ,test=develop * add test_performance test --- mobile/test/CMakeLists.txt | 4 + mobile/test/net/test_net_performance.cpp | 177 +++++++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 mobile/test/net/test_net_performance.cpp diff --git a/mobile/test/CMakeLists.txt b/mobile/test/CMakeLists.txt index d81b08ef0b..b080a7c458 100644 --- a/mobile/test/CMakeLists.txt +++ b/mobile/test/CMakeLists.txt @@ -534,6 +534,10 @@ if (ENABLE_ALL_TEST) # gen test ADD_EXECUTABLE(test-net net/test_net.cpp test_helper.h test_include.h executor_for_test.h) target_link_libraries(test-net paddle-mobile) + + # gen test + ADD_EXECUTABLE(test-net-performance net/test_net_performance.cpp test_helper.h test_include.h executor_for_test.h) + target_link_libraries(test-net-performance paddle-mobile) endif () else() # gen test diff --git a/mobile/test/net/test_net_performance.cpp b/mobile/test/net/test_net_performance.cpp new file mode 100644 index 0000000000..d16164757e --- /dev/null +++ b/mobile/test/net/test_net_performance.cpp @@ -0,0 +1,177 @@ +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#include +#include +#include +#include "../test_helper.h" +#include "../test_include.h" + +void test(int argc, char *argv[]); + +int main(int argc, char *argv[]) { + test(argc, argv); + return 0; +} + +void test(int argc, char *argv[]) { + int arg_index = 1; + bool fuse = std::stoi(argv[arg_index]) == 1; + arg_index++; + bool enable_memory_optimization = std::stoi(argv[arg_index]) == 1; + arg_index++; + bool quantification = std::stoi(argv[arg_index]) == 1; + arg_index++; + int quantification_fold = std::stoi(argv[arg_index]); + arg_index++; + paddle_mobile::PaddleMobileConfigInternal config; + config.memory_optimization_level = enable_memory_optimization + ? MemoryOptimizationWithoutFeeds + : NoMemoryOptimization; + + // save obfuscated model + // config.model_obfuscate_key = "asdf"; + // std::ofstream out_file("new-params", std::ofstream::binary); + // char *out_data = ReadFileToBuff("./checked_model/params"); + // int len = GetFileLength("./checked_model/params"); + // out_file.write(out_data, len); + // out_file.close(); + +#ifdef PADDLE_MOBILE_CL + // config.load_when_predict = true; + paddle_mobile::PaddleMobile paddle_mobile(config); + paddle_mobile.SetCLPath("/data/local/tmp/bin"); + std::cout << "testing opencl performance " << std::endl; +#else + paddle_mobile::PaddleMobile paddle_mobile(config); + paddle_mobile.SetThreadNum(1); + std::cout << "testing cpu performance " << std::endl; +#endif + + int dim_count = std::stoi(argv[arg_index]); + arg_index++; + int size = 1; + std::vector dims; + for (int i = 0; i < dim_count; i++) { + int64_t dim = std::stoi(argv[arg_index + i]); + size *= dim; + dims.push_back(dim); + } + arg_index += dim_count; + + bool is_lod = std::stoi(argv[arg_index]) == 1; + arg_index++; + paddle_mobile::framework::LoD lod{{}}; + if (is_lod) { + int lod_count = std::stoi(argv[arg_index]); + arg_index++; + for (int i = 0; i < lod_count; i++) { + int dim = std::stoi(argv[arg_index + i]); + lod[0].push_back(dim); + } + arg_index += lod_count; + } + + int var_count = std::stoi(argv[arg_index]); + arg_index++; + bool is_sample_step = std::stoi(argv[arg_index]) == 1; + arg_index++; + int sample_arg = std::stoi(argv[arg_index]); + int sample_step = sample_arg; + int sample_num = sample_arg; + arg_index++; + std::vector var_names; + for (int i = 0; i < var_count; i++) { + std::string var_name = argv[arg_index + i]; + var_names.push_back(var_name); + } + arg_index += var_count; + bool check_shape = std::stoi(argv[arg_index]) == 1; + arg_index++; + + int run_times = std::stoi(argv[arg_index]); + arg_index++; + + bool warm_up = std::stoi(argv[arg_index]) == 1; + arg_index++; + + auto time1 = time(); + if (paddle_mobile.Load("./checked_model/model", "./checked_model/params", + fuse, quantification, 1, true, quantification_fold)) { + auto time2 = time(); + std::cout << "auto-test" + << " load-time-cost :" << time_diff(time1, time2) << "ms" + << std::endl; + + float *input_data_array = new float[size]; + std::ifstream in("input.txt", std::ios::in); + for (int i = 0; i < size; i++) { + float num; + in >> num; + input_data_array[i] = num; + } + in.close(); + + auto time3 = time(); + + paddle_mobile::framework::Tensor input_tensor( + input_data_array, paddle_mobile::framework::make_ddim(dims)); + auto time4 = time(); + std::cout << "auto-test" + << " preprocess-time-cost :" << time_diff(time3, time4) << "ms" + << std::endl; + + paddle_mobile::framework::LoDTensor input_lod_tensor; + if (is_lod) { + input_lod_tensor.Resize(paddle_mobile::framework::make_ddim(dims)); + input_lod_tensor.set_lod(lod); + auto *tensor_data = input_lod_tensor.mutable_data(); + for (int i = 0; i < size; i++) { + tensor_data[i] = input_data_array[i]; + } + } + + // 预热10次 + if (warm_up) { + for (int i = 0; i < 10; i++) { + if (is_lod) { + auto out = paddle_mobile.Predict(input_lod_tensor); + } else { + paddle_mobile.Feed(var_names[0], input_tensor); + paddle_mobile.Predict(); + } + } + } + + // 测速 + auto time5 = time(); + if (is_lod) { + for (int i = 0; i < run_times; i++) { + paddle_mobile.Predict(input_lod_tensor); + } + } else { + paddle_mobile.Feed(var_names[0], input_tensor); + for (int i = 0; i < run_times; i++) { + paddle_mobile.Predict(); + } + } + + auto time6 = time(); + std::cout << "auto-test" + << " predict-time-cost " << time_diff(time5, time6) / run_times + << "ms" << std::endl; + + std::cout << std::endl; + } +} -- GitLab