lite_mul_model_test.cc 4.7 KB
Newer Older
石晓伟 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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. */

#include <glog/logging.h>
#include <gtest/gtest.h>
#include <cmath>
18 19
#include <mutex>   // NOLINT
#include <thread>  // NOLINT
20
#include "gflags/gflags.h"
石晓伟 已提交
21 22 23 24 25 26

#include "paddle/fluid/inference/tests/api/tester_helper.h"

namespace paddle {
namespace inference {

27 28
int test_predictor(const AnalysisConfig& config_in,
                   Barrier* barrier = nullptr) {
29
  static std::mutex mutex;
30
  AnalysisConfig config{config_in};
31 32 33 34 35 36 37 38
  std::unique_ptr<PaddlePredictor> predictor;
  {
    std::unique_lock<std::mutex> lock(mutex);
    predictor = std::move(CreatePaddlePredictor(config));
  }
  if (barrier) {
    barrier->Wait();
  }
石晓伟 已提交
39 40 41 42 43 44 45 46 47 48 49

  std::vector<PaddleTensor> inputs;
  std::vector<float> input({1});

  PaddleTensor in;
  in.shape = {1, 1};
  in.data = PaddleBuf(static_cast<void*>(input.data()), 1 * sizeof(float));
  in.dtype = PaddleDType::FLOAT32;
  inputs.emplace_back(in);

  std::vector<PaddleTensor> outputs;
50
  predictor->Run(inputs, &outputs);
石晓伟 已提交
51
  const std::vector<float> truth_values = {
52 53
      -0.00621776f, -0.00620937f, 0.00990623f,  -0.0039817f, -0.00074315f,
      0.61229795f,  -0.00491806f, -0.00068755f, 0.18409646f, 0.30090684f};
石晓伟 已提交
54 55 56 57 58 59
  const size_t expected_size = 1;
  EXPECT_EQ(outputs.size(), expected_size);
  float* data_o = static_cast<float*>(outputs[0].data.data());
  for (size_t j = 0; j < outputs[0].data.length() / sizeof(float); ++j) {
    EXPECT_LT(std::abs(data_o[j] - truth_values[j]), 10e-6);
  }
60 61 62
  return 0;
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
int test_predictor_zero_copy(const AnalysisConfig& config_in,
                             Barrier* barrier = nullptr) {
  static std::mutex mutex;
  AnalysisConfig config{config_in};
  config.SwitchUseFeedFetchOps(false);
  std::unique_ptr<PaddlePredictor> predictor;
  {
    std::unique_lock<std::mutex> lock(mutex);
    predictor = std::move(CreatePaddlePredictor(config));
  }
  if (barrier) {
    barrier->Wait();
  }

  std::vector<float> input({1});
78 79
  auto in_tensor =
      predictor->GetInputTensor(predictor->GetInputNames().front());
80 81 82 83 84
  in_tensor->Reshape({1, 1});
  in_tensor->copy_from_cpu(input.data());

  predictor->ZeroCopyRun();

85 86
  auto out_tensor =
      predictor->GetOutputTensor(predictor->GetOutputNames().front());
87 88 89 90 91 92 93 94 95 96 97 98 99 100
  std::vector<float> data_o(10);
  out_tensor->copy_to_cpu(data_o.data());

  const std::vector<float> truth_values = {
      -0.00621776f, -0.00620937f, 0.00990623f,  -0.0039817f, -0.00074315f,
      0.61229795f,  -0.00491806f, -0.00068755f, 0.18409646f, 0.30090684f};
  const size_t expected_size = 1;
  EXPECT_EQ(predictor->GetOutputNames().size(), expected_size);
  for (size_t j = 0; j < truth_values.size(); ++j) {
    EXPECT_LT(std::abs(data_o[j] - truth_values[j]), 10e-6);
  }
  return 0;
}

101 102 103 104 105
#ifdef PADDLE_WITH_XPU
TEST(AnalysisPredictor, native_xpu) {
  AnalysisConfig config;
  config.EnableXpu();
  config.SetModel(FLAGS_infer_model + "/" + "mul_model");
106 107
  test_predictor(config);
  test_predictor_zero_copy(config);
108 109 110 111 112 113 114 115 116
}
#endif

#ifdef LITE_SUBGRAPH_WITH_XPU
TEST(AnalysisPredictor, lite_xpu) {
  AnalysisConfig config;
  config.EnableXpu();
  config.SetModel(FLAGS_infer_model + "/" + "mul_model");
  config.EnableLiteEngine(paddle::AnalysisConfig::Precision::kFloat32);
117 118
  test_predictor(config);
  test_predictor_zero_copy(config);
119 120 121
}
#endif

122
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
123 124 125 126 127 128 129 130 131 132
TEST(AnalysisPredictor, thread_local_stream) {
  const size_t thread_num = 5;
  std::vector<std::thread> threads(thread_num);
  Barrier barrier(thread_num);
  for (size_t i = 0; i < threads.size(); ++i) {
    threads[i] = std::thread([&barrier, i]() {
      AnalysisConfig config;
      config.EnableUseGpu(100, 0);
      config.SetModel(FLAGS_infer_model + "/" + "mul_model");
      config.EnableGpuMultiStream();
133 134
      test_predictor(config, &barrier);
      test_predictor_zero_copy(config);
135 136 137 138 139 140 141 142 143 144 145 146
    });
  }
  for (auto& th : threads) {
    th.join();
  }
}

TEST(AnalysisPredictor, lite_engine) {
  AnalysisConfig config;
  config.EnableUseGpu(100, 0);
  config.SetModel(FLAGS_infer_model + "/" + "mul_model");
  config.EnableLiteEngine(paddle::AnalysisConfig::Precision::kFloat32);
147 148
  test_predictor(config);
  test_predictor_zero_copy(config);
石晓伟 已提交
149
}
150
#endif
石晓伟 已提交
151 152 153

}  // namespace inference
}  // namespace paddle