analysis_config.cc 4.6 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_;
60
  tensorrt_min_subgraph_size_ = other.tensorrt_min_subgraph_size_;
T
Tao Luo 已提交
61
  model_from_memory_ = other.model_from_memory_;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

  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 已提交
82
  cpu_math_library_num_threads_ = other.cpu_math_library_num_threads_;
83 84
  // fields from this.
  enable_ir_optim = other.enable_ir_optim;
85 86 87 88
  // For mkldnn
  use_mkldnn_ = other.use_mkldnn_;
  mkldnn_enabled_op_types_ = other.mkldnn_enabled_op_types_;

89 90 91 92
  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_;
93
  tensorrt_min_subgraph_size_ = other.tensorrt_min_subgraph_size_;
T
Tao Luo 已提交
94
  model_from_memory_ = other.model_from_memory_;
T
Tao Luo 已提交
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109
  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,
110 111
                                                   int max_batch_size,
                                                   int min_subgraph_size) {
112 113 114
  use_tensorrt_ = true;
  tensorrt_workspace_size_ = workspace_size;
  tensorrt_max_batchsize_ = max_batch_size;
115 116
  tensorrt_min_subgraph_size_ = min_subgraph_size;
  // Append after the conv+affine_channel fuse pass.
N
nhzlx 已提交
117
  pass_builder()->InsertPass(3, "tensorrt_subgraph_pass");
118 119
}

T
Tao Luo 已提交
120 121 122 123
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 已提交
124 125
  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 已提交
126
  model_from_memory_ = true;
T
Tao Luo 已提交
127 128
}

129
}  // namespace paddle