analysis_config.cc 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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
// 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 "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/inference/api/paddle_inference_api.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle_pass_builder.h"  // NOLINT

namespace paddle {

PassStrategy *contrib::AnalysisConfig::pass_builder() const {
  PADDLE_ENFORCE(
      pass_builder_.get(),
      "Should call constructor first, that will init the pass_builder_.");
  return pass_builder_.get();
}

contrib::AnalysisConfig::AnalysisConfig(bool use_gpu) {
  this->use_gpu = use_gpu;
  if (use_gpu) {
    pass_builder_.reset(new GpuPassStrategy);
  } else {
    pass_builder_.reset(new CpuPassStrategy);
  }
}

contrib::AnalysisConfig::AnalysisConfig(const contrib::AnalysisConfig &other) {
  // fields from Config
  model_dir = other.model_dir;
  // fields from NativeConfig
  use_gpu = other.use_gpu;
  device = other.device;
  fraction_of_gpu_memory = other.fraction_of_gpu_memory;
  prog_file = other.prog_file;
  param_file = other.param_file;
  specify_input_name = other.specify_input_name;
L
luotao1 已提交
49
  cpu_math_library_num_threads_ = other.cpu_math_library_num_threads_;
50 51
  // fields from this.
  enable_ir_optim = other.enable_ir_optim;
52 53 54 55
  // For mkldnn
  use_mkldnn_ = other.use_mkldnn_;
  mkldnn_enabled_op_types_ = other.mkldnn_enabled_op_types_;

56 57 58 59
  use_feed_fetch_ops = other.use_feed_fetch_ops;
  use_tensorrt_ = other.use_tensorrt_;
  tensorrt_max_batchsize_ = other.tensorrt_max_batchsize_;
  tensorrt_workspace_size_ = other.tensorrt_workspace_size_;
T
Tao Luo 已提交
60
  model_from_memory_ = other.model_from_memory_;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

  if (use_gpu) {
    pass_builder_.reset(new GpuPassStrategy(
        *static_cast<GpuPassStrategy *>(other.pass_builder())));
  } else {
    pass_builder_.reset(new CpuPassStrategy(
        *static_cast<CpuPassStrategy *>(other.pass_builder())));
  }
}

contrib::AnalysisConfig::AnalysisConfig(contrib::AnalysisConfig &&other) {
  // fields from Config
  model_dir = other.model_dir;
  // fields from NativeConfig
  use_gpu = other.use_gpu;
  device = other.device;
  fraction_of_gpu_memory = other.fraction_of_gpu_memory;
  prog_file = other.prog_file;
  param_file = other.param_file;
  specify_input_name = other.specify_input_name;
L
luotao1 已提交
81
  cpu_math_library_num_threads_ = other.cpu_math_library_num_threads_;
82 83
  // fields from this.
  enable_ir_optim = other.enable_ir_optim;
84 85 86 87
  // For mkldnn
  use_mkldnn_ = other.use_mkldnn_;
  mkldnn_enabled_op_types_ = other.mkldnn_enabled_op_types_;

88 89 90 91
  use_feed_fetch_ops = other.use_feed_fetch_ops;
  use_tensorrt_ = other.use_tensorrt_;
  tensorrt_max_batchsize_ = other.tensorrt_max_batchsize_;
  tensorrt_workspace_size_ = other.tensorrt_workspace_size_;
T
Tao Luo 已提交
92
  model_from_memory_ = other.model_from_memory_;
T
Tao Luo 已提交
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  pass_builder_ = std::move(other.pass_builder_);
}

void contrib::AnalysisConfig::EnableMKLDNN() {
#ifdef PADDLE_WITH_MKLDNN
  pass_builder()->EnableMKLDNN();
  use_mkldnn_ = true;
#else
  LOG(ERROR) << "Please compile with MKLDNN first to use MKLDNN";
  use_mkldnn_ = false;
#endif
}

void contrib::AnalysisConfig::EnableTensorRtEngine(int workspace_size,
                                                   int max_batch_size) {
  use_tensorrt_ = true;
  tensorrt_workspace_size_ = workspace_size;
  tensorrt_max_batchsize_ = max_batch_size;
  // Append after the infer_clean pass.
  pass_builder()->InsertPass(1, "tensorrt_subgraph_pass");
}

T
Tao Luo 已提交
116 117 118 119
void contrib::AnalysisConfig::SetModelBuffer(const char *prog_buffer,
                                             size_t prog_buffer_size,
                                             const char *param_buffer,
                                             size_t param_buffer_size) {
T
Tao Luo 已提交
120 121
  prog_file = std::string(prog_buffer, prog_buffer + prog_buffer_size);
  param_file = std::string(param_buffer, param_buffer + param_buffer_size);
T
Tao Luo 已提交
122
  model_from_memory_ = true;
T
Tao Luo 已提交
123 124
}

125
}  // namespace paddle