|
|
|
@ -16,8 +16,10 @@
|
|
|
|
|
#include <gflags/gflags.h>
|
|
|
|
|
#include <glog/logging.h> // use glog instead of PADDLE_ENFORCE to avoid importing other paddle header files.
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include "paddle/fluid/framework/ir/pass.h"
|
|
|
|
|
#include "paddle/fluid/inference/analysis/ut_helper.h"
|
|
|
|
|
#include "paddle/fluid/inference/api/helper.h"
|
|
|
|
|
#include "paddle/fluid/inference/api/paddle_inference_api.h"
|
|
|
|
|
#include "paddle/fluid/inference/api/paddle_inference_pass.h"
|
|
|
|
|
#include "paddle/fluid/inference/api/timer.h"
|
|
|
|
@ -26,60 +28,72 @@ DEFINE_string(infer_model, "", "Directory of the inference model.");
|
|
|
|
|
DEFINE_string(infer_data, "", "Path of the dataset.");
|
|
|
|
|
DEFINE_int32(batch_size, 1, "batch size.");
|
|
|
|
|
DEFINE_int32(repeat, 1, "How many times to repeat run.");
|
|
|
|
|
DEFINE_int32(topn, -1, "Run top n batches of data to save time");
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
|
namespace inference {
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
std::string to_string(const std::vector<T> &vec) {
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
for (const auto &c : vec) {
|
|
|
|
|
ss << c << " ";
|
|
|
|
|
}
|
|
|
|
|
return ss.str();
|
|
|
|
|
}
|
|
|
|
|
struct DataReader {
|
|
|
|
|
explicit DataReader(const std::string &path)
|
|
|
|
|
: file(new std::ifstream(path)) {}
|
|
|
|
|
|
|
|
|
|
void PrintTime(const double latency, const int bs, const int repeat) {
|
|
|
|
|
LOG(INFO) << "===========profile result===========";
|
|
|
|
|
LOG(INFO) << "batch_size: " << bs << ", repeat: " << repeat
|
|
|
|
|
<< ", avg latency: " << latency / repeat << "ms";
|
|
|
|
|
LOG(INFO) << "=====================================";
|
|
|
|
|
}
|
|
|
|
|
bool NextBatch(PaddleTensor *tensor, int batch_size) {
|
|
|
|
|
PADDLE_ENFORCE_EQ(batch_size, 1);
|
|
|
|
|
std::string line;
|
|
|
|
|
tensor->lod.clear();
|
|
|
|
|
tensor->lod.emplace_back(std::vector<size_t>({0}));
|
|
|
|
|
std::vector<int64_t> data;
|
|
|
|
|
|
|
|
|
|
void Main(int batch_size) {
|
|
|
|
|
// Three sequence inputs.
|
|
|
|
|
std::vector<PaddleTensor> input_slots(1);
|
|
|
|
|
// one batch starts
|
|
|
|
|
// data --
|
|
|
|
|
int64_t data0[] = {0, 1, 2};
|
|
|
|
|
for (auto &input : input_slots) {
|
|
|
|
|
input.data.Reset(data0, sizeof(data0));
|
|
|
|
|
input.shape = std::vector<int>({3, 1});
|
|
|
|
|
// dtype --
|
|
|
|
|
input.dtype = PaddleDType::INT64;
|
|
|
|
|
// LoD --
|
|
|
|
|
input.lod = std::vector<std::vector<size_t>>({{0, 3}});
|
|
|
|
|
for (int i = 0; i < batch_size; i++) {
|
|
|
|
|
if (!std::getline(*file, line)) return false;
|
|
|
|
|
inference::split_to_int64(line, ' ', &data);
|
|
|
|
|
}
|
|
|
|
|
tensor->lod.front().push_back(data.size());
|
|
|
|
|
|
|
|
|
|
tensor->data.Resize(data.size() * sizeof(int64_t));
|
|
|
|
|
memcpy(tensor->data.data(), data.data(), data.size() * sizeof(int64_t));
|
|
|
|
|
tensor->shape.clear();
|
|
|
|
|
tensor->shape.push_back(data.size());
|
|
|
|
|
tensor->shape.push_back(1);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<std::ifstream> file;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void Main(int batch_size) {
|
|
|
|
|
// shape --
|
|
|
|
|
// Create Predictor --
|
|
|
|
|
AnalysisConfig config;
|
|
|
|
|
config.model_dir = FLAGS_infer_model;
|
|
|
|
|
config.use_gpu = false;
|
|
|
|
|
config.enable_ir_optim = true;
|
|
|
|
|
config.ir_passes.push_back("fc_lstm_fuse_pass");
|
|
|
|
|
auto predictor =
|
|
|
|
|
CreatePaddlePredictor<AnalysisConfig, PaddleEngineKind::kAnalysis>(
|
|
|
|
|
config);
|
|
|
|
|
|
|
|
|
|
std::vector<PaddleTensor> input_slots(1);
|
|
|
|
|
// one batch starts
|
|
|
|
|
// data --
|
|
|
|
|
auto &input = input_slots[0];
|
|
|
|
|
input.dtype = PaddleDType::INT64;
|
|
|
|
|
|
|
|
|
|
inference::Timer timer;
|
|
|
|
|
double sum = 0;
|
|
|
|
|
std::vector<PaddleTensor> output_slots;
|
|
|
|
|
for (int i = 0; i < FLAGS_repeat; i++) {
|
|
|
|
|
timer.tic();
|
|
|
|
|
CHECK(predictor->Run(input_slots, &output_slots));
|
|
|
|
|
sum += timer.toc();
|
|
|
|
|
|
|
|
|
|
int num_batches = 0;
|
|
|
|
|
for (int t = 0; t < FLAGS_repeat; t++) {
|
|
|
|
|
DataReader reader(FLAGS_infer_data);
|
|
|
|
|
while (reader.NextBatch(&input, FLAGS_batch_size)) {
|
|
|
|
|
if (FLAGS_topn > 0 && num_batches > FLAGS_topn) break;
|
|
|
|
|
timer.tic();
|
|
|
|
|
CHECK(predictor->Run(input_slots, &output_slots));
|
|
|
|
|
sum += timer.toc();
|
|
|
|
|
++num_batches;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
PrintTime(sum, batch_size, FLAGS_repeat);
|
|
|
|
|
PrintTime(batch_size, FLAGS_repeat, 1, 0, sum / FLAGS_repeat);
|
|
|
|
|
|
|
|
|
|
// Get output
|
|
|
|
|
LOG(INFO) << "get outputs " << output_slots.size();
|
|
|
|
@ -100,4 +114,5 @@ void Main(int batch_size) {
|
|
|
|
|
|
|
|
|
|
TEST(text_classification, basic) { Main(FLAGS_batch_size); }
|
|
|
|
|
|
|
|
|
|
} // namespace inference
|
|
|
|
|
} // namespace paddle
|
|
|
|
|