analyzer_transformer_tester_helper.h 7.5 KB
Newer Older
T
Tao Luo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
14 15 16 17
#pragma once
#include <string>
#include <utility>
#include <vector>
T
Tao Luo 已提交
18 19 20 21
#include "paddle/fluid/inference/tests/api/tester_helper.h"

namespace paddle {
namespace inference {
22 23
namespace analysis {
namespace transformer_tester {
T
Tao Luo 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

struct DataRecord {
  std::vector<std::vector<int64_t>> src_word, src_pos, trg_word, init_idx;
  std::vector<std::vector<float>> src_slf_attn_bias, init_score,
      trg_src_attn_bias;
  std::vector<std::vector<int32_t>> batch_data_shape;
  std::vector<std::vector<size_t>> lod;
  size_t batch_iter{0}, batch_size{1}, num_samples;  // total number of samples
  DataRecord() = default;
  explicit DataRecord(const std::string &path, int batch_size = 1)
      : batch_size(batch_size) {
    Load(path);
  }
  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 <= src_word.size()) {
      data.src_word.assign(src_word.begin() + batch_iter,
                           src_word.begin() + batch_end);
      data.src_pos.assign(src_pos.begin() + batch_iter,
                          src_pos.begin() + batch_end);
      data.src_slf_attn_bias.assign(src_slf_attn_bias.begin() + batch_iter,
                                    src_slf_attn_bias.begin() + batch_end);
      data.trg_word.assign(trg_word.begin() + batch_iter,
                           trg_word.begin() + batch_end);
      data.init_score.assign(init_score.begin() + batch_iter,
                             init_score.begin() + batch_end);
      data.init_idx.assign(init_idx.begin() + batch_iter,
                           init_idx.begin() + batch_end);
      data.trg_src_attn_bias.assign(trg_src_attn_bias.begin() + batch_iter,
                                    trg_src_attn_bias.begin() + batch_end);
      std::vector<int32_t> batch_shape =
          *(batch_data_shape.begin() + batch_iter);
      data.batch_data_shape.push_back(batch_shape);
      data.lod.resize(2);
      for (int i = 0; i < batch_shape[0] + 1; i++) {
        data.lod[0].push_back(i);
        data.lod[1].push_back(i);
      }
    }
    batch_iter += batch_size;
    return data;
  }
  void Load(const std::string &path) {
    std::ifstream file(path);
    std::string line;
    size_t num_lines = 0;
    while (std::getline(file, line)) {
      num_lines++;
      std::vector<std::string> data;
      split(line, ',', &data);
      CHECK_EQ(data.size(), static_cast<size_t>(8));
      // load src_word
      std::vector<int64_t> src_word_data;
      split_to_int64(data[0], ' ', &src_word_data);
      src_word.push_back(std::move(src_word_data));
      // load src_pos
      std::vector<int64_t> src_pos_data;
      split_to_int64(data[1], ' ', &src_pos_data);
      src_pos.push_back(std::move(src_pos_data));
      // load src_slf_attn_bias
      std::vector<float> src_slf_attn_bias_data;
      split_to_float(data[2], ' ', &src_slf_attn_bias_data);
      src_slf_attn_bias.push_back(std::move(src_slf_attn_bias_data));
      // load trg_word
      std::vector<int64_t> trg_word_data;
      split_to_int64(data[3], ' ', &trg_word_data);
      trg_word.push_back(std::move(trg_word_data));
      // load init_score
      std::vector<float> init_score_data;
      split_to_float(data[4], ' ', &init_score_data);
      init_score.push_back(std::move(init_score_data));
      // load init_idx
      std::vector<int64_t> init_idx_data;
      split_to_int64(data[5], ' ', &init_idx_data);
      init_idx.push_back(std::move(init_idx_data));
      // load trg_src_attn_bias
      std::vector<float> trg_src_attn_bias_data;
      split_to_float(data[6], ' ', &trg_src_attn_bias_data);
      trg_src_attn_bias.push_back(std::move(trg_src_attn_bias_data));
      // load shape for variant data shape
      std::vector<int> batch_data_shape_data;
      split_to_int(data[7], ' ', &batch_data_shape_data);
      batch_data_shape.push_back(std::move(batch_data_shape_data));
    }
    num_samples = num_lines;
  }
};

void PrepareInputs(std::vector<PaddleTensor> *input_slots, DataRecord *data,
                   int batch_size) {
  auto one_batch = data->NextBatch();
  batch_size = one_batch.batch_data_shape[0][0];
  auto n_head = one_batch.batch_data_shape[0][1];
  auto trg_seq_len = one_batch.batch_data_shape[0][2];  // 1 for inference
  auto src_seq_len = one_batch.batch_data_shape[0][3];

  PaddleTensor src_word, src_pos, src_slf_attn_bias, trg_word, init_score,
      init_idx, trg_src_attn_bias;

  src_word.name = "src_word";
  src_word.shape.assign({batch_size, src_seq_len, 1});
  src_word.dtype = PaddleDType::INT64;
  TensorAssignData<int64_t>(&src_word, one_batch.src_word);

  src_pos.name = "src_pos";
  src_pos.shape.assign({batch_size, src_seq_len, 1});
  src_pos.dtype = PaddleDType::INT64;
  TensorAssignData<int64_t>(&src_pos, one_batch.src_pos);

  src_slf_attn_bias.name = "src_slf_attn_bias";
  src_slf_attn_bias.shape.assign(
      {batch_size, n_head, src_seq_len, src_seq_len});
  src_slf_attn_bias.dtype = PaddleDType::FLOAT32;
  TensorAssignData<float>(&src_slf_attn_bias, one_batch.src_slf_attn_bias);

  trg_word.name = "trg_word";
  trg_word.shape.assign({batch_size, 1});
  trg_word.dtype = PaddleDType::INT64;
  trg_word.lod.assign(one_batch.lod.begin(), one_batch.lod.end());
  TensorAssignData<int64_t>(&trg_word, one_batch.trg_word);

  init_score.name = "init_score";
  init_score.shape.assign({batch_size, 1});
  init_score.dtype = PaddleDType::FLOAT32;
  init_score.lod.assign(one_batch.lod.begin(), one_batch.lod.end());
  TensorAssignData<float>(&init_score, one_batch.init_score);

  init_idx.name = "init_idx";
  init_idx.shape.assign({batch_size});
155
  init_idx.dtype = PaddleDType::INT32;
T
Tao Luo 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  TensorAssignData<int64_t>(&init_idx, one_batch.init_idx);

  trg_src_attn_bias.name = "trg_src_attn_bias";
  trg_src_attn_bias.shape.assign(
      {batch_size, n_head, trg_seq_len, src_seq_len});
  trg_src_attn_bias.dtype = PaddleDType::FLOAT32;
  TensorAssignData<float>(&trg_src_attn_bias, one_batch.trg_src_attn_bias);

  input_slots->assign({src_word, src_pos, src_slf_attn_bias, trg_word,
                       init_score, init_idx, trg_src_attn_bias});
}

void SetConfig(AnalysisConfig *cfg) {
  cfg->SetModel(FLAGS_infer_model + "/model", FLAGS_infer_model + "/params");
  cfg->DisableGpu();
  cfg->SwitchSpecifyInputNames();
  cfg->SwitchIrOptim();
173
  cfg->SetCpuMathLibraryNumThreads(FLAGS_cpu_num_threads);
T
Tao Luo 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
}

void SetInput(std::vector<std::vector<PaddleTensor>> *inputs) {
  DataRecord data(FLAGS_infer_data, FLAGS_batch_size);
  std::vector<PaddleTensor> input_slots;
  int test_batch_num =
      FLAGS_test_all_data ? data.num_samples / FLAGS_batch_size : 1;
  LOG(INFO) << "The number of samples to be test: "
            << test_batch_num * FLAGS_batch_size;
  for (int bid = 0; bid < test_batch_num; ++bid) {
    input_slots.clear();
    PrepareInputs(&input_slots, &data, FLAGS_batch_size);
    (*inputs).emplace_back(input_slots);
  }
}

190 191
}  // namespace transformer_tester
}  // namespace analysis
T
Tao Luo 已提交
192 193
}  // namespace inference
}  // namespace paddle