optimizer.h 11.0 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2019 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.

#pragma once
16
#include <map>
Y
Yan Chunwei 已提交
17
#include <memory>
18
#include <set>
Y
Yan Chunwei 已提交
19
#include <string>
20
#include <utility>
Y
Yan Chunwei 已提交
21 22 23
#include <vector>
#include "lite/core/mir/generate_program_pass.h"
#include "lite/core/mir/pass_manager.h"
24
#include "lite/core/mir/pass_utils.h"
Y
Yan Chunwei 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include "lite/core/mir/ssa_graph.h"
#include "lite/core/mir/static_kernel_pick_pass.h"
#include "lite/core/mir/type_target_cast_pass.h"
#include "lite/core/program.h"
#include "lite/core/types.h"
#include "lite/model_parser/model_parser.h"

namespace paddle {
namespace lite {

/*
 * lite::Optimizer optimize a program. It utilize the mir passes to analysis the
 * program and export an optimized program.
 */
class Optimizer {
 public:
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  Optimizer() {}

  Optimizer(Program&& program, const std::vector<Place>& valid_places) {
    program_ = &program;
    valid_places_ = valid_places;
    CHECK(!valid_places.empty()) << "At least one valid_place should be set";

    core::KernelPickFactor factor;
    factor.ConsiderTarget();
    factor.ConsiderPrecision();
    factor.ConsiderDataLayout();

    Run(std::move(program), valid_places, factor, {});
  }

Y
Yan Chunwei 已提交
56 57 58 59 60 61 62 63
  void Run(Program&& program,
           const std::vector<Place>& valid_places,
           core::KernelPickFactor kernel_pick_factor,
           const std::vector<std::string>& passes = {}) {
    program_ = &program;
    valid_places_ = valid_places;
    CHECK(!valid_places.empty()) << "At least one valid_place should be set";
    CHECK(!graph_) << "duplicate optimize found";
64

Y
Yan Chunwei 已提交
65 66 67 68 69 70 71
    graph_.reset(new mir::SSAGraph);
    graph_->Build(program, valid_places);
    graph_->SetValidPlaces(valid_places);

    SpecifyKernelPickTactic(kernel_pick_factor);
    InitTargetTypeTransformPass();

72
    if (passes.empty() || passes.size() == 1) {
73
      std::vector<std::string> passes_local{
J
juncaipeng 已提交
74 75 76 77 78
          {"lite_quant_dequant_fuse_pass",         //
           "weight_quantization_preprocess_pass",  //
           "lite_conv_elementwise_fuse_pass",      // conv-elemwise-bn
           "lite_conv_bn_fuse_pass",               //
           "lite_conv_elementwise_fuse_pass",      // conv-bn-elemwise
H
HappyAngel 已提交
79
           "lite_conv_conv_fuse_pass",             //
Y
Yan Chunwei 已提交
80 81
           // TODO(Superjomn) Refine the fusion related design to select fusion
           // kernels for devices automatically.
82
           "lite_conv_activation_fuse_pass",              //
83
           "lite_var_conv_2d_activation_fuse_pass",       //
84 85 86
           "lite_fc_fuse_pass",                           //
           "lite_shuffle_channel_fuse_pass",              //
           "lite_transpose_softmax_transpose_fuse_pass",  //
Z
zhupengyang 已提交
87
           "lite_interpolate_fuse_pass",                  //
88
           "identity_scale_eliminate_pass",               //
H
HappyAngel 已提交
89
           "elementwise_mul_constant_eliminate_pass",     //
90
           "lite_sequence_pool_concat_fuse_pass",         //
91
           "lite_scale_activation_fuse_pass",             //
H
HappyAngel 已提交
92 93
#if (defined LITE_WITH_LIGHT_WEIGHT_FRAMEWORK) || (defined LITE_WITH_CUDA) || \
    (defined LITE_WITH_ARM)
94
           "lite_elementwise_activation_fuse_pass",  //
Y
Yan Chunwei 已提交
95
#endif
96
           "identity_dropout_eliminate_pass",
97
           "__xpu__resnet_fuse_pass",
98 99
           "__xpu__resnet_cbam_fuse_pass",
           "__xpu__mmdnn_fuse_pass",
100
           "__xpu__multi_encoder_fuse_pass",
C
Cwndmiao 已提交
101 102
           "__xpu__embedding_with_eltwise_add_fuse_pass",
           "__xpu__fc_fuse_pass",
103 104 105 106 107 108
           "quantized_op_attributes_inference_pass",  // Only for fully
                                                      // quantized model, infer
                                                      // the output scale and
                                                      // fix the attribute
                                                      // 'enable_int8' for all
                                                      // of the quantized ops.
109 110 111
           "npu_subgraph_pass",
           "xpu_subgraph_pass",
           "bm_subgraph_pass",
H
hong19860320 已提交
112
           "apu_subgraph_pass",
113
           "rknpu_subgraph_pass",
114
           "mlu_subgraph_pass",
115
           "static_kernel_pick_pass",  // pick original kernel from graph
116

117
           "remove_tf_redundant_ops_pass",
118
           "variable_place_inference_pass",  // inference arg/var's
119 120

           "mlu_postprocess_pass",
121 122 123 124 125
           // info(target/precision/layout/device)
           // using kernel info
           "argument_type_display_pass",  // debug pass: show arg-type-node's
                                          // info
                                          // (target/precision/layout/device)
Y
Yan Chunwei 已提交
126

127 128 129
           "type_target_cast_pass",  // add io_copy/io_copy_once if meet
                                     // different targets when last and next
                                     // node
Y
Yan Chunwei 已提交
130 131 132
           "variable_place_inference_pass",  //
           "argument_type_display_pass",     //

133 134 135
           "io_copy_kernel_pick_pass",    //
           "argument_type_display_pass",  //

Y
Yan Chunwei 已提交
136 137 138 139 140 141 142
           "variable_place_inference_pass",  //
           "argument_type_display_pass",     //

           "type_precision_cast_pass",       //
           "variable_place_inference_pass",  //
           "argument_type_display_pass",     //

143 144 145 146
           "type_layout_cast_pass",  // add layout/layout_once op if meet
                                     // different layout when last and next node
           "argument_type_display_pass",  //

Y
Yan Chunwei 已提交
147
           "variable_place_inference_pass",  //
148
           "argument_type_display_pass",
Y
Yan Chunwei 已提交
149 150

           "runtime_context_assign_pass",
151
           "argument_type_display_pass",
152

153
           "memory_optimize_pass"}};
154

155
      if (passes.size() == 1) {
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        // multi_stream_analysis_pass must be in the front of
        // runtime_context_assign_pass
        const std::string msa_pass{"multi_stream_analysis_pass"};
        const std::string depend_pass{"runtime_context_assign_pass"};
        if (passes[0] == msa_pass) {
          auto iter =
              std::find(passes_local.begin(), passes_local.end(), depend_pass);
          if (iter != passes_local.end()) {
            passes_local.insert(iter, msa_pass);
          } else {
            CHECK(false) << "Not find " << depend_pass;
          }
        } else {
          passes_local.push_back(passes[0]);
        }
171
      }
172
      RunPasses(passes_local);
Y
Yan Chunwei 已提交
173 174 175 176 177 178 179 180
    } else {
      RunPasses(passes);
    }
    exec_scope_ = program.exec_scope();
  }

  const lite::Scope* exec_scope() const { return exec_scope_; }

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  // Set shape(dims) infos of var descs to scope var.
  //  developer can write pass using input / output tensor dims of op.
  //
  // Example: If you have node `Node* softmax_node`,
  //   you can get dims of output tensor in passes:
  //
  //   auto* scope = softmax_node->AsStmt().op()->scope();
  //   auto softmax_out_arg_name =
  //             softmax_node->outlinks.front()->AsArg().name;
  //   auto softmax_out_tensor =
  //             scope->FindVar(softmax_out_arg_name)->Get<lite::Tensor>();
  //   softmax_out_dims = softmax_out_tensor.dims();
  void SetVarDescShapeToScopeVar() {
    auto dims_to_str_func = [](std::vector<int64_t> shape) -> std::string {
      std::string str_res;
      for (size_t i = 0; i < shape.size(); ++i) {
        str_res += std::to_string(shape[i]);
        if (i != shape.size() - 1) {
          str_res += "x";
        }
      }
      return str_res;
    };

    auto* program_desc = program_->program_desc();
    VLOG(5) << "program_desc->BlocksSize():" << program_desc->BlocksSize();
    auto blocks_desc = program_desc->GetBlocks();
    for (size_t bidx = 0; bidx < blocks_desc.size(); ++bidx) {
      auto block_desc = blocks_desc[bidx];
      auto vars_desc = block_desc.GetVars();
      for (size_t vidx = 0; vidx < vars_desc.size(); ++vidx) {
        auto var_desc = vars_desc[vidx];
        VLOG(5) << var_desc.Name() << " "
                << dims_to_str_func(var_desc.GetShape());
        if (var_desc.Name() == "feed" || var_desc.Name() == "fetch") continue;
        auto* var = program_->exec_scope()->FindVar(var_desc.Name());
        auto tensor = var->GetMutable<lite::Tensor>();
        if (tensor->dims().size() == 0 && var_desc.GetShape().size() != 0) {
          VLOG(5) << "var_desc.Name():" << var_desc.Name()
                  << " shape:" << dims_to_str_func(var_desc.GetShape());
          tensor->Resize(var_desc.GetShape());
        }
        VLOG(5) << "var_desc.Name():" << var_desc.Name()
                << " shape:" << dims_to_str_func(var_desc.GetShape())
                << " tensor:" << tensor->dims();
      }
    }
  }

Y
Yan Chunwei 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
  // Generate a new program based on the mir graph.
  std::unique_ptr<RuntimeProgram> GenRuntimeProgram() {
    auto pass = mir::PassManager::Global().LookUp<mir::GenerateProgramPass>(
        "generate_program_pass");
    pass->Apply(graph_);
    auto program = pass->GenProgram();
    CHECK(exec_scope_);
    program->set_exec_scope(exec_scope_);
    return program;
  }

  void InitTargetTypeTransformPass() {
    auto* pass =
        mir::PassManager::Global().LookUp<mir::TypeTargetTransformPass>(
            "type_target_cast_pass");
    CHECK(pass);
    CHECK(!valid_places_.empty());
    pass->SetValidPlaces(valid_places_);
  }

  // Generate C++ code which combines the inference program, model and weights.
  void GenCode(const std::string& code_dir);

  const mir::SSAGraph& ssa_graph() const {
    CHECK(graph_);
    return *graph_;
  }

  mir::SSAGraph* mutable_ssa_graph() {
    CHECK(graph_);
    return graph_.get();
  }

  lite::Scope* exec_scope() { return exec_scope_; }

 protected:
  void SpecifyKernelPickTactic(core::KernelPickFactor factor);

  // Specify the passes and run them.
  void RunPasses(const std::vector<std::string>& passes) {
270
    SetVarDescShapeToScopeVar();
Y
Yan Chunwei 已提交
271
    for (auto& x : passes) {
272 273
      LOG(INFO) << "== Running pass: " << x;
      mir::Pass* pass = mir::PassManager::Global().LookUp(x);
274 275 276 277 278
      if (!pass) {
        LOG(INFO) << "   - Skip " << x << " because the pass isn't found.";
        continue;
      }
      std::set<TargetType> targets;
279
      for (const auto& place : valid_places_) {
280
        targets.insert(place.target);
281
      }
282 283
      bool matched =
          PassMatchesTarget(*pass, targets) && PassMatchesKernels(*pass);
284
      if (!matched) {
285 286
        LOG(INFO) << "   - Skip " << x
                  << " because the target or kernel does not match.";
287 288 289 290
      } else {
        pass->Apply(graph_);
        LOG(INFO) << "== Finished running: " << x;
      }
Y
Yan Chunwei 已提交
291 292 293 294 295 296 297 298 299 300 301 302
    }
  }

 private:
  std::unique_ptr<mir::SSAGraph> graph_;
  std::vector<Place> valid_places_;
  lite::Scope* exec_scope_{};
  Program* program_{};
};

}  // namespace lite
}  // namespace paddle