|
| 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 | + GetFuseStatis(cfg, &num_ops); |
| 78 | +} |
| 79 | + |
| 80 | +// Compare result of NativeConfig and AnalysisConfig |
| 81 | +TEST(Analyzer_resnet50, compare) { |
| 82 | + AnalysisConfig cfg; |
| 83 | + SetConfig(&cfg); |
| 84 | + |
| 85 | + std::vector<std::vector<PaddleTensor>> input_slots_all; |
| 86 | + SetInput(&input_slots_all); |
| 87 | + CompareNativeAndAnalysis(cfg, input_slots_all); |
| 88 | +} |
| 89 | + |
| 90 | +} // namespace analysis |
| 91 | +} // namespace inference |
| 92 | +} // namespace paddle |
0 commit comments