Skip to content

Commit d770b9b

Browse files
authored
Merge pull request #13663 from luotao1/resnet50_ut
add resnet50 inference unit-test
2 parents 32c260c + 83ca657 commit d770b9b

File tree

9 files changed

+109
-8
lines changed

9 files changed

+109
-8
lines changed

paddle/fluid/framework/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ if(WITH_DISTRIBUTE)
164164
set_source_files_properties(executor.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS})
165165
else()
166166
cc_library(executor SRCS executor.cc DEPS op_registry device_context scope framework_proto glog lod_rank_table feed_fetch_method graph_to_program_pass)
167-
cc_test(test_naive_executor SRCS naive_executor_test.cc DEPS naive_executor op_registry device_context scope framework_proto glog lod_rank_table feed_fetch_method graph_to_program_pass elementwise_add_op)
167+
cc_test(test_naive_executor SRCS naive_executor_test.cc DEPS naive_executor elementwise_add_op)
168168
endif()
169169

170170
if (NOT WIN32)

paddle/fluid/inference/analysis/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ cc_test(test_node SRCS node_tester.cc DEPS analysis)
2020
cc_test(test_dot SRCS dot_tester.cc DEPS analysis)
2121
cc_binary(inference_analyzer SRCS analyzer_main.cc DEPS analysis paddle_fluid)
2222

23-
set(PYTHON_TESTS_DIR ${PADDLE_BINARY_DIR}/python/paddle/fluid/tests)
24-
2523
function (inference_analysis_test TARGET)
2624
if(WITH_TESTING)
2725
set(options "")

paddle/fluid/inference/api/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ function(inference_api_test TARGET_NAME)
3131
set(multiValueArgs ARGS)
3232
cmake_parse_arguments(inference_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
3333

34-
set(PYTHON_TESTS_DIR ${PADDLE_BINARY_DIR}/python/paddle/fluid/tests)
3534
cc_test(${TARGET_NAME}
3635
SRCS ${inference_test_SRC}
3736
DEPS "${inference_deps}"

paddle/fluid/inference/tests/api/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ if (NOT EXISTS ${OCR_INSTALL_DIR})
7070
endif()
7171
inference_analysis_api_test(test_analyzer_ocr ${OCR_INSTALL_DIR} analyzer_vis_tester.cc)
7272

73+
# resnet50
74+
set(RESNET50_INSTALL_DIR "${INFERENCE_DEMO_INSTALL_DIR}/resnet50")
75+
if (NOT EXISTS ${RESNET50_INSTALL_DIR})
76+
inference_download_and_uncompress(${RESNET50_INSTALL_DIR} ${INFERENCE_URL} "resnet50_model.tar.gz")
77+
endif()
78+
inference_analysis_test(test_analyzer_resnet50 SRCS analyzer_resnet50_tester.cc
79+
EXTRA_DEPS ${INFERENCE_EXTRA_DEPS} ARGS --infer_model=${RESNET50_INSTALL_DIR}/model)
80+
7381
# anakin
7482
if (WITH_ANAKIN AND WITH_MKL) # only needed in CI
7583
# anakin rnn1
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include <fstream>
16+
#include <iostream>
17+
#include "paddle/fluid/inference/tests/api/tester_helper.h"
18+
19+
namespace paddle {
20+
namespace inference {
21+
namespace analysis {
22+
23+
void SetConfig(AnalysisConfig *cfg) {
24+
cfg->param_file = FLAGS_infer_model + "/params";
25+
cfg->prog_file = FLAGS_infer_model + "/model";
26+
cfg->use_gpu = false;
27+
cfg->device = 0;
28+
cfg->enable_ir_optim = true;
29+
cfg->specify_input_name = true;
30+
}
31+
32+
void SetInput(std::vector<std::vector<PaddleTensor>> *inputs) {
33+
PADDLE_ENFORCE_EQ(FLAGS_test_all_data, 0, "Only have single batch of data.");
34+
35+
PaddleTensor input;
36+
// channel=3, height/width=318
37+
std::vector<int> shape({FLAGS_batch_size, 3, 318, 318});
38+
input.shape = shape;
39+
input.dtype = PaddleDType::FLOAT32;
40+
41+
// fill input data, for profile easily, do not use random data here.
42+
size_t size = FLAGS_batch_size * 3 * 318 * 318;
43+
input.data.Resize(size * sizeof(float));
44+
float *input_data = static_cast<float *>(input.data.data());
45+
for (size_t i = 0; i < size; i++) {
46+
*(input_data + i) = static_cast<float>(i) / size;
47+
}
48+
49+
std::vector<PaddleTensor> input_slots;
50+
input_slots.assign({input});
51+
(*inputs).emplace_back(input_slots);
52+
}
53+
54+
// Easy for profiling independently.
55+
TEST(Analyzer_resnet50, profile) {
56+
AnalysisConfig cfg;
57+
SetConfig(&cfg);
58+
std::vector<PaddleTensor> outputs;
59+
60+
std::vector<std::vector<PaddleTensor>> input_slots_all;
61+
SetInput(&input_slots_all);
62+
TestPrediction(cfg, input_slots_all, &outputs, FLAGS_num_threads);
63+
64+
if (FLAGS_num_threads == 1 && !FLAGS_test_all_data) {
65+
PADDLE_ENFORCE_EQ(outputs.size(), 1UL);
66+
size_t size = GetSize(outputs[0]);
67+
// output is a 512-dimension feature
68+
EXPECT_EQ(size, 512 * FLAGS_batch_size);
69+
}
70+
}
71+
72+
// Check the fuse status
73+
TEST(Analyzer_resnet50, fuse_statis) {
74+
AnalysisConfig cfg;
75+
SetConfig(&cfg);
76+
int num_ops;
77+
auto predictor = CreatePaddlePredictor<AnalysisConfig>(cfg);
78+
auto fuse_statis = GetFuseStatis(
79+
static_cast<AnalysisPredictor *>(predictor.get()), &num_ops);
80+
ASSERT_TRUE(fuse_statis.count("fc_fuse"));
81+
EXPECT_EQ(fuse_statis.at("fc_fuse"), 1);
82+
}
83+
84+
// Compare result of NativeConfig and AnalysisConfig
85+
TEST(Analyzer_resnet50, compare) {
86+
AnalysisConfig cfg;
87+
SetConfig(&cfg);
88+
89+
std::vector<std::vector<PaddleTensor>> input_slots_all;
90+
SetInput(&input_slots_all);
91+
CompareNativeAndAnalysis(cfg, input_slots_all);
92+
}
93+
94+
} // namespace analysis
95+
} // namespace inference
96+
} // namespace paddle

paddle/fluid/inference/tests/api/analyzer_rnn1_tester.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,11 @@ TEST(Analyzer_rnn1, multi_thread) {
270270

271271
std::vector<std::vector<PaddleTensor>> input_slots_all;
272272
SetInput(&input_slots_all);
273-
TestPrediction(cfg, input_slots_all, &outputs, FLAGS_num_threads);
273+
TestPrediction(cfg, input_slots_all, &outputs, 4 /* multi_thread */);
274274
}
275275

276-
bool CompareTensors(framework::Scope &a_scope, framework::Scope &b_scope,
276+
bool CompareTensors(const framework::Scope &a_scope,
277+
const framework::Scope &b_scope,
277278
const std::vector<std::string> &tensors) {
278279
for (auto &x : tensors) {
279280
auto *a_var = a_scope.FindVar(x);

paddle/fluid/inference/tests/book/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ function(inference_test TARGET_NAME)
44
set(multiValueArgs ARGS)
55
cmake_parse_arguments(inference_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
66

7-
set(PYTHON_TESTS_DIR ${PADDLE_BINARY_DIR}/python/paddle/fluid/tests)
87
set(arg_list "")
98
if(inference_test_ARGS)
109
foreach(arg ${inference_test_ARGS})

paddle/fluid/train/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ function(train_test TARGET_NAME)
44
set(multiValueArgs ARGS)
55
cmake_parse_arguments(train_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
66

7-
set(PYTHON_TESTS_DIR ${PADDLE_BINARY_DIR}/python/paddle/fluid/tests)
87
set(arg_list "")
98
if(train_test_ARGS)
109
foreach(arg ${train_test_ARGS})

python/paddle/fluid/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
set(PYTHON_TESTS_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE PATH "python tests directory")
12
file(GLOB TEST_OPS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "test_*.py")
23
string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}")
34

0 commit comments

Comments
 (0)