From 1b33683d0ea581b9799bdc8c6280518175db51fc Mon Sep 17 00:00:00 2001 From: Yanzhan Yang Date: Tue, 9 Jul 2019 17:48:05 +0800 Subject: [PATCH] add test class to verify single op in a net (#1736) * add test class to verify single op in a net * fix private member error --- test/CMakeLists.txt | 4 ++ test/net/test_op_in_net.cpp | 125 ++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 test/net/test_op_in_net.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index db2ffe11c6..26c183963d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -218,6 +218,10 @@ if (NOT FOUND_MATCH) 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-op-in-net net/test_op_in_net.cpp test_helper.h test_include.h executor_for_test.h) + target_link_libraries(test-op-in-net paddle-mobile) + # gen test ADD_EXECUTABLE(test-googlenet net/test_googlenet.cpp test_helper.h test_include.h executor_for_test.h) target_link_libraries(test-googlenet paddle-mobile) diff --git a/test/net/test_op_in_net.cpp b/test/net/test_op_in_net.cpp new file mode 100644 index 0000000000..4666f4133c --- /dev/null +++ b/test/net/test_op_in_net.cpp @@ -0,0 +1,125 @@ +/* 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[]) { + std::vector dims{1, 8, 32, 32}; + int op_index = 2; + std::string input_var_name = "ConvNdBackward2.conv2d.output.1.tmp_0"; + std::vector output_var_names{ + "ConvNdBackward2.conv2d.output.1.tmp_1"}; + + bool fuse = false; + bool enable_memory_optimization = true; + paddle_mobile::PaddleMobileConfigInternal config; + config.memory_optimization_level = enable_memory_optimization + ? MemoryOptimizationWithoutFeeds + : NoMemoryOptimization; +#ifdef PADDLE_MOBILE_CL + // config.load_when_predict = true; + paddle_mobile::PaddleMobile paddle_mobile(config); + paddle_mobile.SetCLPath("/data/local/tmp/bin"); +#else + paddle_mobile::PaddleMobile paddle_mobile(config); + paddle_mobile.SetThreadNum(1); +#endif + + int size = 1; + for (int i = 0; i < dims.size(); i++) { + size *= dims[i]; + } + + bool is_sample_step = false; + int sample_step = 1; + int sample_num = 20; + + auto time1 = time(); + if (paddle_mobile.Load("./checked_model/model", "./checked_model/params", + fuse, false, 1, true)) { + auto time2 = time(); + std::cout << "auto-test" + << " load-time-cost :" << time_diff(time1, time2) << "ms" + << std::endl; + + float input_data_array[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(); + std::vector input_data; + for (int i = 0; i < size; i++) { + float num = input_data_array[i]; + input_data.push_back(num); + } + paddle_mobile::framework::Tensor input_tensor( + input_data, 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.h及executor.h的属性可见性,如需使用,调整可见性后,放开注释 + // auto *input_var = + // paddle_mobile.executor_->program_.scope->FindVar(input_var_name); + // framework::LoDTensor *target = + // input_var->template GetMutable(); + // target->Resize(input_tensor.dims()); + // target->ShareDataWith(input_tensor); + // paddle_mobile.executor_->ops_of_block0_[op_index]->InferShape(); + // paddle_mobile.executor_->ops_of_block0_[op_index]->Run(); + + for (auto var_name : output_var_names) { + auto out = paddle_mobile.Fetch(var_name); + auto len = out->numel(); + if (len == 0) { + continue; + } + if (out->memory_size() == 0) { + continue; + } + auto data = out->data(); + std::string sample = ""; + if (!is_sample_step) { + sample_step = len / sample_num; + } + if (sample_step <= 0) { + sample_step = 1; + } + for (int i = 0; i < len; i += sample_step) { + sample += " " + std::to_string(data[i]); + } + std::cout << "auto-test" + << " var " << var_name << sample << std::endl; + } + std::cout << std::endl; + } +} -- GitLab