ut_helper.h 2.4 KB
Newer Older
1 2
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Y
Yan Chunwei 已提交
3 4 5
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
6

Y
Yan Chunwei 已提交
7
http://www.apache.org/licenses/LICENSE-2.0
8

Y
Yan Chunwei 已提交
9 10 11 12 13
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 <gflags/gflags.h>
#include <gtest/gtest.h>
Y
Yan Chunwei 已提交
18
#include <fstream>
19
#include <string>
20 21 22 23 24 25 26
#include "paddle/fluid/framework/executor.h"
#include "paddle/fluid/inference/analysis/data_flow_graph.h"
#include "paddle/fluid/inference/analysis/fluid_to_data_flow_graph_pass.h"
#include "paddle/fluid/inference/analysis/ut_helper.h"

namespace paddle {
namespace inference {
Y
Yan Chunwei 已提交
27 28 29 30

// Read ProgramDesc from a __model__ file, defined in io.cc
extern void ReadBinaryFile(const std::string& filename, std::string* contents);

31 32 33 34 35 36
namespace analysis {

DEFINE_string(inference_model_dir, "", "inference test model dir");

static framework::proto::ProgramDesc LoadProgramDesc(
    const std::string& model_dir = FLAGS_inference_model_dir) {
Y
Yan Chunwei 已提交
37 38 39 40 41 42 43 44 45 46 47 48
  std::string msg;
  std::string net_file = FLAGS_inference_model_dir + "/__model__";
  std::ifstream fin(net_file, std::ios::in | std::ios::binary);
  PADDLE_ENFORCE(static_cast<bool>(fin), "Cannot open file %s", net_file);
  fin.seekg(0, std::ios::end);
  msg.resize(fin.tellg());
  fin.seekg(0, std::ios::beg);
  fin.read(&(msg.at(0)), msg.size());
  fin.close();
  framework::proto::ProgramDesc program_desc;
  program_desc.ParseFromString(msg);
  return program_desc;
49 50 51 52 53 54
}

static DataFlowGraph ProgramDescToDFG(
    const framework::proto::ProgramDesc& desc) {
  DataFlowGraph graph;
  FluidToDataFlowGraphPass pass;
Y
Yan Chunwei 已提交
55 56 57
  Argument argument;
  argument.origin_program_desc.reset(new framework::proto::ProgramDesc(desc));
  pass.Initialize(&argument);
58 59 60 61 62 63 64
  pass.Run(&graph);
  pass.Finalize();
  return graph;
}

class DFG_Tester : public ::testing::Test {
 protected:
Y
Yan Chunwei 已提交
65 66 67 68
  void SetUp() override {
    auto desc = LoadProgramDesc(FLAGS_inference_model_dir);
    argument.origin_program_desc.reset(new framework::proto::ProgramDesc(desc));
  }
69

Y
Yan Chunwei 已提交
70
  Argument argument;
71 72 73 74 75
};

}  // namespace analysis
}  // namespace inference
}  // namespace paddle