From 9137e98e447ecfd0244b90527f9fde37b0c172df Mon Sep 17 00:00:00 2001 From: zhaojiaying01 Date: Thu, 2 Aug 2018 18:03:52 +0800 Subject: [PATCH] modify unit testing of gemm and googlenet --- test/CMakeLists.txt | 8 ++- .../{test_gemm.cpp => test_gemm_accuracy.cpp} | 0 test/common/test_gemm_perf.cpp | 64 +++++++++++++++++++ test/net/test_googlenet.cpp | 13 ++-- 4 files changed, 79 insertions(+), 6 deletions(-) rename test/common/{test_gemm.cpp => test_gemm_accuracy.cpp} (100%) create mode 100644 test/common/test_gemm_perf.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 418ebff791..8839079fec 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -114,8 +114,12 @@ else () target_link_libraries(test-softmax paddle-mobile) # gen test - ADD_EXECUTABLE(test-gemm common/test_gemm.cpp) - target_link_libraries(test-gemm paddle-mobile) + ADD_EXECUTABLE(test-gemm-accuracy common/test_gemm_accuracy.cpp) + target_link_libraries(test-gemm-accuracy paddle-mobile) + + # gen test + ADD_EXECUTABLE(test-gemm-perf common/test_gemm_perf.cpp) + target_link_libraries(test-gemm-perf paddle-mobile) # gen test ADD_EXECUTABLE(test-enforce common/test_enforce.cpp) diff --git a/test/common/test_gemm.cpp b/test/common/test_gemm_accuracy.cpp similarity index 100% rename from test/common/test_gemm.cpp rename to test/common/test_gemm_accuracy.cpp diff --git a/test/common/test_gemm_perf.cpp b/test/common/test_gemm_perf.cpp new file mode 100644 index 0000000000..260236e24e --- /dev/null +++ b/test/common/test_gemm_perf.cpp @@ -0,0 +1,64 @@ +/* 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 "../test_helper.h" +#include "operators/math/gemm.h" +#include "operators/math/math_function.h" + +#define a(i, j) a[(i)*lda + (j)] +#define b(i, j) b[(i)*ldb + (j)] +#define c1(i, j) c1[(i)*ldc + (j)] + +#define m 1024 +#define n 1024 +#define k 1024 + +int main() { + Tensor aa, bb, cc, scale, bias; + auto aaptr = aa.mutable_data({m, k}); + auto bbptr = bb.mutable_data({k, n}); + auto ccptr = cc.mutable_data({m, n}); + auto scaleptr = scale.mutable_data({m}); + auto biasptr = bias.mutable_data({m}); + + for (int i = 0; i < m * k; ++i) { + aaptr[i] = 2; + } + for (int i = 0; i < k * n; ++i) { + bbptr[i] = 2; + } + for (int i = 0; i < m * n; ++i) { + ccptr[i] = 2; + } + for (int i = 0; i < m; ++i) { + scaleptr[i] = 1; + biasptr[i] = 0; + } + + auto time1 = time(); + for (int j = 0; j < 10; ++j) { + paddle_mobile::operators::math::matmul(aa, false, bb, false, + static_cast(1), &cc, + static_cast(0), false); + + // paddle_mobile::operators::math::matmulWithBn( + // aa, false, bb, false, static_cast(1), &cc, + // static_cast(0), true, &scale, &bias, 0); + } + auto time2 = time(); + std::cout << "gemm cost :" << time_diff(time1, time2) / 10 << "ms\n"; + + return 0; +} diff --git a/test/net/test_googlenet.cpp b/test/net/test_googlenet.cpp index d230b94692..02882bedb0 100644 --- a/test/net/test_googlenet.cpp +++ b/test/net/test_googlenet.cpp @@ -12,7 +12,7 @@ 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 "../test_helper.h" #include "../test_include.h" @@ -23,15 +23,20 @@ int main() { auto time1 = time(); if (paddle_mobile.Load(g_googlenet, optimize)) { auto time2 = time(); - DLOG << "load cost: " << time_diff(time1, time1) << "ms"; + std::cout << "load cost :" << time_diff(time1, time2) << "ms" << std::endl; std::vector input; std::vector dims{1, 3, 224, 224}; GetInput(g_test_image_1x3x224x224, &input, dims); - auto time3 = time(); + // 预热一次 auto vec_result = paddle_mobile.Predict(input, dims); + auto time3 = time(); + for (int i = 0; i < 10; ++i) { + auto vec_result = paddle_mobile.Predict(input, dims); + } auto time4 = time(); - DLOG << "predict cost :" << time_diff(time3, time4) << "ms"; + std::cout << "predict cost :" << time_diff(time3, time4) / 10 << "ms" + << std::endl; } return 0; } -- GitLab