|
|
|
@ -14,38 +14,54 @@
|
|
|
|
|
|
|
|
|
|
#include "paddle/fluid/inference/tests/api/tester_helper.h"
|
|
|
|
|
|
|
|
|
|
DEFINE_int32(max_turn_num, 1,
|
|
|
|
|
"The max turn number: 1 for the small and 9 for the normal.");
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
|
namespace inference {
|
|
|
|
|
using contrib::AnalysisConfig;
|
|
|
|
|
#define MAX_TURN_NUM 1
|
|
|
|
|
#define MAX_TURN_LEN 50
|
|
|
|
|
|
|
|
|
|
constexpr int32_t kMaxTurnLen = 50;
|
|
|
|
|
|
|
|
|
|
static std::vector<float> result_data;
|
|
|
|
|
|
|
|
|
|
struct DataRecord {
|
|
|
|
|
std::vector<std::vector<int64_t>>
|
|
|
|
|
turns[MAX_TURN_NUM]; // turns data : MAX_TURN_NUM
|
|
|
|
|
std::vector<std::vector<float>>
|
|
|
|
|
turns_mask[MAX_TURN_NUM]; // turns mask data : MAX_TURN_NUM
|
|
|
|
|
std::vector<std::vector<int64_t>> response; // response data : 1
|
|
|
|
|
std::vector<std::vector<int64_t>> *turns;
|
|
|
|
|
std::vector<std::vector<float>> *turns_mask;
|
|
|
|
|
std::vector<std::vector<int64_t>> response; // response data : 1
|
|
|
|
|
std::vector<std::vector<float>> response_mask; // response mask data : 1
|
|
|
|
|
size_t batch_iter{0};
|
|
|
|
|
size_t batch_size{1};
|
|
|
|
|
size_t num_samples; // total number of samples
|
|
|
|
|
DataRecord() = default;
|
|
|
|
|
|
|
|
|
|
DataRecord() {
|
|
|
|
|
turns = new std::vector<std::vector<
|
|
|
|
|
int64_t>>[FLAGS_max_turn_num]; // turns data : FLAGS_max_turn_num
|
|
|
|
|
turns_mask = new std::vector<std::vector<
|
|
|
|
|
float>>[FLAGS_max_turn_num]; // turns mask data : FLAGS_max_turn_num
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
explicit DataRecord(const std::string &path, int batch_size = 1)
|
|
|
|
|
: batch_size(batch_size) {
|
|
|
|
|
: DataRecord() {
|
|
|
|
|
this->batch_size = batch_size;
|
|
|
|
|
Load(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~DataRecord() {
|
|
|
|
|
delete[] turns;
|
|
|
|
|
delete[] turns_mask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DataRecord NextBatch() {
|
|
|
|
|
DataRecord data;
|
|
|
|
|
size_t batch_end = batch_iter + batch_size;
|
|
|
|
|
// NOTE skip the final batch, if no enough data is provided.
|
|
|
|
|
if (batch_end <= response.size()) {
|
|
|
|
|
for (int i = 0; i < MAX_TURN_NUM; ++i) {
|
|
|
|
|
for (int i = 0; i < FLAGS_max_turn_num; ++i) {
|
|
|
|
|
data.turns[i].assign(turns[i].begin() + batch_iter,
|
|
|
|
|
turns[i].begin() + batch_end);
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < MAX_TURN_NUM; ++i) {
|
|
|
|
|
for (int i = 0; i < FLAGS_max_turn_num; ++i) {
|
|
|
|
|
data.turns_mask[i].assign(turns_mask[i].begin() + batch_iter,
|
|
|
|
|
turns_mask[i].begin() + batch_end);
|
|
|
|
|
}
|
|
|
|
@ -60,6 +76,7 @@ struct DataRecord {
|
|
|
|
|
batch_iter += batch_size;
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Load(const std::string &path) {
|
|
|
|
|
std::ifstream file(path);
|
|
|
|
|
std::string line;
|
|
|
|
@ -69,30 +86,30 @@ struct DataRecord {
|
|
|
|
|
num_lines++;
|
|
|
|
|
std::vector<std::string> data;
|
|
|
|
|
split(line, ',', &data);
|
|
|
|
|
CHECK_EQ(data.size(), (size_t)(2 * MAX_TURN_NUM + 3));
|
|
|
|
|
CHECK_EQ(data.size(), (size_t)(2 * FLAGS_max_turn_num + 3));
|
|
|
|
|
// load turn data
|
|
|
|
|
std::vector<int64_t> turns_tmp[MAX_TURN_NUM];
|
|
|
|
|
for (int i = 0; i < MAX_TURN_NUM; ++i) {
|
|
|
|
|
std::vector<int64_t> turns_tmp[FLAGS_max_turn_num];
|
|
|
|
|
for (int i = 0; i < FLAGS_max_turn_num; ++i) {
|
|
|
|
|
split_to_int64(data[i], ' ', &turns_tmp[i]);
|
|
|
|
|
turns[i].push_back(std::move(turns_tmp[i]));
|
|
|
|
|
}
|
|
|
|
|
// load turn_mask data
|
|
|
|
|
std::vector<float> turns_mask_tmp[MAX_TURN_NUM];
|
|
|
|
|
for (int i = 0; i < MAX_TURN_NUM; ++i) {
|
|
|
|
|
split_to_float(data[MAX_TURN_NUM + i], ' ', &turns_mask_tmp[i]);
|
|
|
|
|
std::vector<float> turns_mask_tmp[FLAGS_max_turn_num];
|
|
|
|
|
for (int i = 0; i < FLAGS_max_turn_num; ++i) {
|
|
|
|
|
split_to_float(data[FLAGS_max_turn_num + i], ' ', &turns_mask_tmp[i]);
|
|
|
|
|
turns_mask[i].push_back(std::move(turns_mask_tmp[i]));
|
|
|
|
|
}
|
|
|
|
|
// load response data
|
|
|
|
|
std::vector<int64_t> response_tmp;
|
|
|
|
|
split_to_int64(data[2 * MAX_TURN_NUM], ' ', &response_tmp);
|
|
|
|
|
split_to_int64(data[2 * FLAGS_max_turn_num], ' ', &response_tmp);
|
|
|
|
|
response.push_back(std::move(response_tmp));
|
|
|
|
|
// load response_mask data
|
|
|
|
|
std::vector<float> response_mask_tmp;
|
|
|
|
|
split_to_float(data[2 * MAX_TURN_NUM + 1], ' ', &response_mask_tmp);
|
|
|
|
|
split_to_float(data[2 * FLAGS_max_turn_num + 1], ' ', &response_mask_tmp);
|
|
|
|
|
response_mask.push_back(std::move(response_mask_tmp));
|
|
|
|
|
// load result data
|
|
|
|
|
float result_tmp;
|
|
|
|
|
result_tmp = std::stof(data[2 * MAX_TURN_NUM + 2]);
|
|
|
|
|
result_tmp = std::stof(data[2 * FLAGS_max_turn_num + 2]);
|
|
|
|
|
result_data.push_back(result_tmp);
|
|
|
|
|
}
|
|
|
|
|
num_samples = num_lines;
|
|
|
|
@ -101,8 +118,8 @@ struct DataRecord {
|
|
|
|
|
|
|
|
|
|
void PrepareInputs(std::vector<PaddleTensor> *input_slots, DataRecord *data,
|
|
|
|
|
int batch_size) {
|
|
|
|
|
PaddleTensor turns_tensor[MAX_TURN_NUM];
|
|
|
|
|
PaddleTensor turns_mask_tensor[MAX_TURN_NUM];
|
|
|
|
|
PaddleTensor turns_tensor[FLAGS_max_turn_num];
|
|
|
|
|
PaddleTensor turns_mask_tensor[FLAGS_max_turn_num];
|
|
|
|
|
PaddleTensor response_tensor;
|
|
|
|
|
PaddleTensor response_mask_tensor;
|
|
|
|
|
std::string turn_pre = "turn_";
|
|
|
|
@ -110,16 +127,16 @@ void PrepareInputs(std::vector<PaddleTensor> *input_slots, DataRecord *data,
|
|
|
|
|
|
|
|
|
|
auto one_batch = data->NextBatch();
|
|
|
|
|
int size = one_batch.response[0].size();
|
|
|
|
|
CHECK_EQ(size, MAX_TURN_LEN);
|
|
|
|
|
CHECK_EQ(size, kMaxTurnLen);
|
|
|
|
|
// turn tensor assignment
|
|
|
|
|
for (int i = 0; i < MAX_TURN_NUM; ++i) {
|
|
|
|
|
for (int i = 0; i < FLAGS_max_turn_num; ++i) {
|
|
|
|
|
turns_tensor[i].name = turn_pre + std::to_string(i);
|
|
|
|
|
turns_tensor[i].shape.assign({batch_size, size, 1});
|
|
|
|
|
turns_tensor[i].dtype = PaddleDType::INT64;
|
|
|
|
|
TensorAssignData<int64_t>(&turns_tensor[i], one_batch.turns[i]);
|
|
|
|
|
}
|
|
|
|
|
// turn mask tensor assignment
|
|
|
|
|
for (int i = 0; i < MAX_TURN_NUM; ++i) {
|
|
|
|
|
for (int i = 0; i < FLAGS_max_turn_num; ++i) {
|
|
|
|
|
turns_mask_tensor[i].name = turn_mask_pre + std::to_string(i);
|
|
|
|
|
turns_mask_tensor[i].shape.assign({batch_size, size, 1});
|
|
|
|
|
turns_mask_tensor[i].dtype = PaddleDType::FLOAT32;
|
|
|
|
@ -137,10 +154,10 @@ void PrepareInputs(std::vector<PaddleTensor> *input_slots, DataRecord *data,
|
|
|
|
|
TensorAssignData<float>(&response_mask_tensor, one_batch.response_mask);
|
|
|
|
|
|
|
|
|
|
// Set inputs.
|
|
|
|
|
for (int i = 0; i < MAX_TURN_NUM; ++i) {
|
|
|
|
|
for (int i = 0; i < FLAGS_max_turn_num; ++i) {
|
|
|
|
|
input_slots->push_back(std::move(turns_tensor[i]));
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < MAX_TURN_NUM; ++i) {
|
|
|
|
|
for (int i = 0; i < FLAGS_max_turn_num; ++i) {
|
|
|
|
|
input_slots->push_back(std::move(turns_mask_tensor[i]));
|
|
|
|
|
}
|
|
|
|
|
input_slots->push_back(std::move(response_tensor));
|
|
|
|
@ -148,7 +165,8 @@ void PrepareInputs(std::vector<PaddleTensor> *input_slots, DataRecord *data,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetConfig(contrib::AnalysisConfig *cfg) {
|
|
|
|
|
cfg->model_dir = FLAGS_infer_model;
|
|
|
|
|
cfg->prog_file = FLAGS_infer_model + "/__model__";
|
|
|
|
|
cfg->param_file = FLAGS_infer_model + "/param";
|
|
|
|
|
cfg->use_gpu = false;
|
|
|
|
|
cfg->device = 0;
|
|
|
|
|
cfg->specify_input_name = true;
|
|
|
|
@ -201,8 +219,6 @@ TEST(Analyzer_dam, fuse_statis) {
|
|
|
|
|
auto fuse_statis = GetFuseStatis(
|
|
|
|
|
static_cast<AnalysisPredictor *>(predictor.get()), &num_ops);
|
|
|
|
|
ASSERT_TRUE(fuse_statis.count("fc_fuse"));
|
|
|
|
|
EXPECT_EQ(fuse_statis.at("fc_fuse"), 45);
|
|
|
|
|
EXPECT_EQ(num_ops, 292);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Compare result of NativeConfig and AnalysisConfig
|
|
|
|
|