|
| 1 | +/* Copyright (c) 2019 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/api/paddle_analysis_config.h" |
| 18 | +#include "paddle/fluid/inference/tests/api/tester_helper.h" |
| 19 | + |
| 20 | +DEFINE_int32(iterations, 0, "Number of iterations"); |
| 21 | + |
| 22 | +namespace paddle { |
| 23 | +namespace inference { |
| 24 | +namespace analysis { |
| 25 | + |
| 26 | +void SetConfig(AnalysisConfig *cfg) { |
| 27 | + cfg->SetModel(FLAGS_infer_model); |
| 28 | + cfg->SetProgFile("__model__"); |
| 29 | + cfg->DisableGpu(); |
| 30 | + cfg->SwitchIrOptim(); |
| 31 | + cfg->SwitchSpecifyInputNames(false); |
| 32 | + cfg->SetCpuMathLibraryNumThreads(FLAGS_paddle_num_threads); |
| 33 | + |
| 34 | + cfg->EnableMKLDNN(); |
| 35 | +} |
| 36 | + |
| 37 | +template <typename T> |
| 38 | +class TensorReader { |
| 39 | + public: |
| 40 | + TensorReader(std::ifstream &file, size_t beginning_offset, |
| 41 | + std::vector<int> shape, std::string name) |
| 42 | + : file_(file), position(beginning_offset), shape_(shape), name_(name) { |
| 43 | + numel = |
| 44 | + std::accumulate(shape_.begin(), shape_.end(), 1, std::multiplies<T>()); |
| 45 | + } |
| 46 | + |
| 47 | + PaddleTensor NextBatch() { |
| 48 | + PaddleTensor tensor; |
| 49 | + tensor.name = name_; |
| 50 | + tensor.shape = shape_; |
| 51 | + tensor.dtype = GetPaddleDType<T>(); |
| 52 | + tensor.data.Resize(numel * sizeof(T)); |
| 53 | + |
| 54 | + file_.seekg(position); |
| 55 | + file_.read(static_cast<char *>(tensor.data.data()), numel * sizeof(T)); |
| 56 | + position = file_.tellg(); |
| 57 | + |
| 58 | + if (file_.eof()) LOG(ERROR) << name_ << ": reached end of stream"; |
| 59 | + if (file_.fail()) |
| 60 | + throw std::runtime_error(name_ + ": failed reading file."); |
| 61 | + |
| 62 | + return tensor; |
| 63 | + } |
| 64 | + |
| 65 | + protected: |
| 66 | + std::ifstream &file_; |
| 67 | + size_t position; |
| 68 | + std::vector<int> shape_; |
| 69 | + std::string name_; |
| 70 | + size_t numel; |
| 71 | +}; |
| 72 | + |
| 73 | +std::shared_ptr<std::vector<PaddleTensor>> GetWarmupData( |
| 74 | + const std::vector<std::vector<PaddleTensor>> &test_data, int num_images) { |
| 75 | + int test_data_batch_size = test_data[0][0].shape[0]; |
| 76 | + CHECK_LE(static_cast<size_t>(num_images), |
| 77 | + test_data.size() * test_data_batch_size); |
| 78 | + |
| 79 | + PaddleTensor images; |
| 80 | + images.name = "input"; |
| 81 | + images.shape = {num_images, 3, 224, 224}; |
| 82 | + images.dtype = PaddleDType::FLOAT32; |
| 83 | + images.data.Resize(sizeof(float) * num_images * 3 * 224 * 224); |
| 84 | + |
| 85 | + PaddleTensor labels; |
| 86 | + labels.name = "labels"; |
| 87 | + labels.shape = {num_images, 1}; |
| 88 | + labels.dtype = PaddleDType::INT64; |
| 89 | + labels.data.Resize(sizeof(int64_t) * num_images); |
| 90 | + |
| 91 | + for (int i = 0; i < num_images; i++) { |
| 92 | + auto batch = i / test_data_batch_size; |
| 93 | + auto element_in_batch = i % test_data_batch_size; |
| 94 | + std::copy_n(static_cast<float *>(test_data[batch][0].data.data()) + |
| 95 | + element_in_batch * 3 * 224 * 224, |
| 96 | + 3 * 224 * 224, |
| 97 | + static_cast<float *>(images.data.data()) + i * 3 * 224 * 224); |
| 98 | + |
| 99 | + std::copy_n(static_cast<int64_t *>(test_data[batch][1].data.data()) + |
| 100 | + element_in_batch, |
| 101 | + 1, static_cast<int64_t *>(labels.data.data()) + i); |
| 102 | + } |
| 103 | + |
| 104 | + auto warmup_data = std::make_shared<std::vector<PaddleTensor>>(2); |
| 105 | + (*warmup_data)[0] = std::move(images); |
| 106 | + (*warmup_data)[1] = std::move(labels); |
| 107 | + return warmup_data; |
| 108 | +} |
| 109 | + |
| 110 | +void SetInput(std::vector<std::vector<PaddleTensor>> *inputs, |
| 111 | + int32_t batch_size = FLAGS_batch_size) { |
| 112 | + std::ifstream file(FLAGS_infer_data, std::ios::binary); |
| 113 | + if (!file) { |
| 114 | + FAIL() << "Couldn't open file: " << FLAGS_infer_data; |
| 115 | + } |
| 116 | + |
| 117 | + int64_t total_images{0}; |
| 118 | + file.read(reinterpret_cast<char *>(&total_images), sizeof(total_images)); |
| 119 | + LOG(INFO) << "Total images in file: " << total_images; |
| 120 | + |
| 121 | + std::vector<int> image_batch_shape{batch_size, 3, 224, 224}; |
| 122 | + std::vector<int> label_batch_shape{batch_size, 1}; |
| 123 | + auto labels_offset_in_file = |
| 124 | + static_cast<size_t>(file.tellg()) + |
| 125 | + sizeof(float) * total_images * |
| 126 | + std::accumulate(image_batch_shape.begin() + 1, |
| 127 | + image_batch_shape.end(), 1, std::multiplies<int>()); |
| 128 | + |
| 129 | + TensorReader<float> image_reader(file, 0, image_batch_shape, "input"); |
| 130 | + TensorReader<int64_t> label_reader(file, labels_offset_in_file, |
| 131 | + label_batch_shape, "label"); |
| 132 | + |
| 133 | + auto iterations = total_images / batch_size; |
| 134 | + if (FLAGS_iterations > 0 && FLAGS_iterations < iterations) |
| 135 | + iterations = FLAGS_iterations; |
| 136 | + for (auto i = 0; i < iterations; i++) { |
| 137 | + auto images = image_reader.NextBatch(); |
| 138 | + auto labels = label_reader.NextBatch(); |
| 139 | + inputs->emplace_back( |
| 140 | + std::vector<PaddleTensor>{std::move(images), std::move(labels)}); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +TEST(Analyzer_int8_resnet50, quantization) { |
| 145 | + AnalysisConfig cfg; |
| 146 | + SetConfig(&cfg); |
| 147 | + |
| 148 | + AnalysisConfig q_cfg; |
| 149 | + SetConfig(&q_cfg); |
| 150 | + |
| 151 | + std::vector<std::vector<PaddleTensor>> input_slots_all; |
| 152 | + SetInput(&input_slots_all, 100); |
| 153 | + |
| 154 | + std::shared_ptr<std::vector<PaddleTensor>> warmup_data = |
| 155 | + GetWarmupData(input_slots_all, 100); |
| 156 | + |
| 157 | + q_cfg.EnableMkldnnQuantizer(); |
| 158 | + q_cfg.mkldnn_quantizer_config()->SetWarmupData(warmup_data); |
| 159 | + q_cfg.mkldnn_quantizer_config()->SetWarmupBatchSize(100); |
| 160 | + |
| 161 | + CompareQuantizedAndAnalysis( |
| 162 | + reinterpret_cast<const PaddlePredictor::Config *>(&cfg), |
| 163 | + reinterpret_cast<const PaddlePredictor::Config *>(&q_cfg), |
| 164 | + input_slots_all); |
| 165 | +} |
| 166 | + |
| 167 | +TEST(Analyzer_int8_resnet50, profile) { |
| 168 | + AnalysisConfig cfg; |
| 169 | + SetConfig(&cfg); |
| 170 | + |
| 171 | + std::vector<std::vector<PaddleTensor>> input_slots_all; |
| 172 | + SetInput(&input_slots_all); |
| 173 | + |
| 174 | + std::shared_ptr<std::vector<PaddleTensor>> warmup_data = |
| 175 | + GetWarmupData(input_slots_all, 100); |
| 176 | + |
| 177 | + cfg.EnableMkldnnQuantizer(); |
| 178 | + cfg.mkldnn_quantizer_config()->SetWarmupData(warmup_data); |
| 179 | + cfg.mkldnn_quantizer_config()->SetWarmupBatchSize(100); |
| 180 | + |
| 181 | + std::vector<PaddleTensor> outputs; |
| 182 | + |
| 183 | + TestPrediction(reinterpret_cast<const PaddlePredictor::Config *>(&cfg), |
| 184 | + input_slots_all, &outputs, FLAGS_num_threads); |
| 185 | +} |
| 186 | + |
| 187 | +} // namespace analysis |
| 188 | +} // namespace inference |
| 189 | +} // namespace paddle |
0 commit comments