|
|
|
@ -18,19 +18,14 @@ limitations under the License. */
|
|
|
|
|
|
|
|
|
|
#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 <iostream>
|
|
|
|
|
#include "paddle/contrib/inference/demo/utils.h"
|
|
|
|
|
#include "paddle/contrib/inference/paddle_inference_api.h"
|
|
|
|
|
#include "paddle/fluid/platform/enforce.h"
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
|
|
#ifdef PADDLE_WITH_CUDA
|
|
|
|
|
DECLARE_double(fraction_of_gpu_memory_to_use);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
|
namespace demo {
|
|
|
|
|
|
|
|
|
|
DEFINE_string(modeldir, "", "Directory of the inference model.");
|
|
|
|
|
DEFINE_string(refer, "", "path to reference result for comparison.");
|
|
|
|
|
DEFINE_string(
|
|
|
|
@ -38,6 +33,10 @@ DEFINE_string(
|
|
|
|
|
"",
|
|
|
|
|
"path of data; each line is a record, format is "
|
|
|
|
|
"'<space splitted floats as data>\t<space splitted ints as shape'");
|
|
|
|
|
DEFINE_bool(use_gpu, false, "Whether use gpu.");
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
|
namespace demo {
|
|
|
|
|
|
|
|
|
|
struct Record {
|
|
|
|
|
std::vector<float> data;
|
|
|
|
@ -47,7 +46,7 @@ struct Record {
|
|
|
|
|
void split(const std::string& str, char sep, std::vector<std::string>* pieces);
|
|
|
|
|
|
|
|
|
|
Record ProcessALine(const std::string& line) {
|
|
|
|
|
LOG(INFO) << "process a line";
|
|
|
|
|
VLOG(3) << "process a line";
|
|
|
|
|
std::vector<std::string> columns;
|
|
|
|
|
split(line, '\t', &columns);
|
|
|
|
|
CHECK_EQ(columns.size(), 2UL)
|
|
|
|
@ -65,8 +64,8 @@ Record ProcessALine(const std::string& line) {
|
|
|
|
|
for (auto& s : shape_strs) {
|
|
|
|
|
record.shape.push_back(std::stoi(s));
|
|
|
|
|
}
|
|
|
|
|
LOG(INFO) << "data size " << record.data.size();
|
|
|
|
|
LOG(INFO) << "data shape size " << record.shape.size();
|
|
|
|
|
VLOG(3) << "data size " << record.data.size();
|
|
|
|
|
VLOG(3) << "data shape size " << record.shape.size();
|
|
|
|
|
return record;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -78,20 +77,22 @@ void CheckOutput(const std::string& referfile, const PaddleTensor& output) {
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
|
|
size_t numel = output.data.length() / PaddleDtypeSize(output.dtype);
|
|
|
|
|
LOG(INFO) << "predictor output numel " << numel;
|
|
|
|
|
LOG(INFO) << "reference output numel " << refer.data.size();
|
|
|
|
|
EXPECT_EQ(numel, refer.data.size());
|
|
|
|
|
VLOG(3) << "predictor output numel " << numel;
|
|
|
|
|
VLOG(3) << "reference output numel " << refer.data.size();
|
|
|
|
|
PADDLE_ENFORCE_EQ(numel, refer.data.size());
|
|
|
|
|
switch (output.dtype) {
|
|
|
|
|
case PaddleDType::INT64: {
|
|
|
|
|
for (size_t i = 0; i < numel; ++i) {
|
|
|
|
|
EXPECT_EQ(static_cast<int64_t*>(output.data.data())[i], refer.data[i]);
|
|
|
|
|
PADDLE_ENFORCE_EQ(static_cast<int64_t*>(output.data.data())[i],
|
|
|
|
|
refer.data[i]);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case PaddleDType::FLOAT32:
|
|
|
|
|
for (size_t i = 0; i < numel; ++i) {
|
|
|
|
|
EXPECT_NEAR(
|
|
|
|
|
static_cast<float*>(output.data.data())[i], refer.data[i], 1e-5);
|
|
|
|
|
PADDLE_ENFORCE_LT(
|
|
|
|
|
fabs(static_cast<float*>(output.data.data())[i] - refer.data[i]),
|
|
|
|
|
1e-5);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
@ -106,15 +107,15 @@ void Main(bool use_gpu) {
|
|
|
|
|
config.prog_file = FLAGS_modeldir + "/__model__";
|
|
|
|
|
config.use_gpu = use_gpu;
|
|
|
|
|
config.device = 0;
|
|
|
|
|
#ifdef PADDLE_WITH_CUDA
|
|
|
|
|
config.fraction_of_gpu_memory = FLAGS_fraction_of_gpu_memory_to_use;
|
|
|
|
|
#endif
|
|
|
|
|
if (FLAGS_use_gpu) {
|
|
|
|
|
config.fraction_of_gpu_memory = 0.1; // set by yourself
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG(INFO) << "init predictor";
|
|
|
|
|
VLOG(3) << "init predictor";
|
|
|
|
|
auto predictor =
|
|
|
|
|
CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config);
|
|
|
|
|
|
|
|
|
|
LOG(INFO) << "begin to process data";
|
|
|
|
|
VLOG(3) << "begin to process data";
|
|
|
|
|
// Just a single batch of data.
|
|
|
|
|
std::string line;
|
|
|
|
|
std::ifstream file(FLAGS_data);
|
|
|
|
@ -129,21 +130,26 @@ void Main(bool use_gpu) {
|
|
|
|
|
.data = PaddleBuf(record.data.data(), record.data.size() * sizeof(float)),
|
|
|
|
|
.dtype = PaddleDType::FLOAT32};
|
|
|
|
|
|
|
|
|
|
LOG(INFO) << "run executor";
|
|
|
|
|
VLOG(3) << "run executor";
|
|
|
|
|
std::vector<PaddleTensor> output;
|
|
|
|
|
predictor->Run({input}, &output);
|
|
|
|
|
|
|
|
|
|
LOG(INFO) << "output.size " << output.size();
|
|
|
|
|
VLOG(3) << "output.size " << output.size();
|
|
|
|
|
auto& tensor = output.front();
|
|
|
|
|
LOG(INFO) << "output: " << SummaryTensor(tensor);
|
|
|
|
|
VLOG(3) << "output: " << SummaryTensor(tensor);
|
|
|
|
|
|
|
|
|
|
// compare with reference result
|
|
|
|
|
CheckOutput(FLAGS_refer, tensor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(demo, vis_demo_cpu) { Main(false /*use_gpu*/); }
|
|
|
|
|
#ifdef PADDLE_WITH_CUDA
|
|
|
|
|
TEST(demo, vis_demo_gpu) { Main(true /*use_gpu*/); }
|
|
|
|
|
#endif
|
|
|
|
|
} // namespace demo
|
|
|
|
|
} // namespace paddle
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
|
google::ParseCommandLineFlags(&argc, &argv, true);
|
|
|
|
|
paddle::demo::Main(false /* use_gpu*/);
|
|
|
|
|
if (FLAGS_use_gpu) {
|
|
|
|
|
paddle::demo::Main(true /*use_gpu*/);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|