analyzer_capi_tester.cc 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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 <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string>
#include <vector>
F
flame 已提交
20
#include "paddle/fluid/inference/capi/paddle_c_api.h"
21 22 23 24 25 26 27
#include "paddle/fluid/inference/tests/api/tester_helper.h"

namespace paddle {
namespace inference {
namespace analysis {

void zero_copy_run() {
28 29 30
  std::string model_dir = FLAGS_infer_model;
  std::string prog_file = model_dir + "/model";
  std::string params_file = model_dir + "/params";
31 32 33 34 35 36
  PD_AnalysisConfig *config = PD_NewAnalysisConfig();
  PD_DisableGpu(config);
  PD_SetCpuMathLibraryNumThreads(config, 10);
  PD_SwitchUseFeedFetchOps(config, false);
  PD_SwitchSpecifyInputNames(config, true);
  PD_SwitchIrDebug(config, true);
37
  PD_SetModel(config, prog_file.c_str(), params_file.c_str());
38 39 40 41 42 43 44
  bool use_feed_fetch = PD_UseFeedFetchOpsEnabled(config);
  CHECK(!use_feed_fetch) << "NO";
  bool specify_input_names = PD_SpecifyInputName(config);
  CHECK(specify_input_names) << "NO";

  const int batch_size = 1;
  const int channels = 3;
45 46
  const int height = 318;
  const int width = 318;
47
  float *input = new float[batch_size * channels * height * width]();
48 49 50 51

  int shape[4] = {batch_size, channels, height, width};
  int shape_size = 4;
  int in_size = 1;
52
  int out_size;
53 54 55
  PD_ZeroCopyData *inputs = new PD_ZeroCopyData;
  PD_ZeroCopyData *outputs = new PD_ZeroCopyData;
  inputs->data = static_cast<void *>(input);
56 57 58 59 60 61 62
  inputs->dtype = PD_FLOAT32;
  inputs->name = new char[5];
  inputs->name[0] = 'd';
  inputs->name[1] = 'a';
  inputs->name[2] = 't';
  inputs->name[3] = 'a';
  inputs->name[4] = '\0';
63 64 65
  inputs->shape = shape;
  inputs->shape_size = shape_size;

66
  PD_PredictorZeroCopyRun(config, inputs, in_size, &outputs, &out_size);
67

68
  delete[] input;
69 70
  delete[] inputs;
  delete[] outputs;
71 72
}

73
TEST(PD_PredictorZeroCopyRun, zero_copy_run) { zero_copy_run(); }
74 75 76

#ifdef PADDLE_WITH_MKLDNN
TEST(PD_AnalysisConfig, profile_mkldnn) {
77 78 79
  std::string model_dir = FLAGS_infer_model;
  std::string prog_file = model_dir + "/model";
  std::string params_file = model_dir + "/params";
80 81 82 83 84 85 86 87 88 89 90 91
  PD_AnalysisConfig *config = PD_NewAnalysisConfig();
  PD_DisableGpu(config);
  PD_SetCpuMathLibraryNumThreads(config, 10);
  PD_SwitchUseFeedFetchOps(config, false);
  PD_SwitchSpecifyInputNames(config, true);
  PD_SwitchIrDebug(config, true);
  PD_EnableMKLDNN(config);
  bool mkldnn_enable = PD_MkldnnEnabled(config);
  CHECK(mkldnn_enable) << "NO";
  PD_EnableMkldnnQuantizer(config);
  bool quantizer_enable = PD_MkldnnQuantizerEnabled(config);
  CHECK(quantizer_enable) << "NO";
92 93 94
  PD_EnableMkldnnBfloat16(config);
  bool bfloat16_enable = PD_MkldnnBfloat16Enabled(config);
  CHECK(bfloat16_enable) << "NO";
95
  PD_SetMkldnnCacheCapacity(config, 0);
96
  PD_SetModel(config, prog_file.c_str(), params_file.c_str());
97 98 99 100 101 102 103
  PD_DeleteAnalysisConfig(config);
}
#endif

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