optimizer.h 5.1 KB
Newer Older
S
superjomn 已提交
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
S
superjomn 已提交
16
#include <memory>
S
superjomn 已提交
17 18
#include <string>
#include <vector>
S
Superjomn 已提交
19
#include "paddle/fluid/lite/core/mir/generate_program_pass.h"
S
superjomn 已提交
20 21
#include "paddle/fluid/lite/core/mir/pass_manager.h"
#include "paddle/fluid/lite/core/mir/ssa_graph.h"
S
superjomn 已提交
22
#include "paddle/fluid/lite/core/mir/static_kernel_pick_pass.h"
C
Chunwei 已提交
23
#include "paddle/fluid/lite/core/mir/type_target_cast_pass.h"
S
Superjomn 已提交
24
#include "paddle/fluid/lite/core/program.h"
25
#include "paddle/fluid/lite/core/types.h"
S
Superjomn 已提交
26
#include "paddle/fluid/lite/model_parser/model_parser.h"
S
superjomn 已提交
27 28 29 30 31 32 33 34 35 36

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:
S
superjomn 已提交
37
  void Run(Program&& program, const std::vector<Place>& valid_places,
38
           core::KernelPickFactor kernel_pick_factor,
S
superjomn 已提交
39
           const std::vector<std::string>& passes = {}) {
S
Superjomn 已提交
40
    program_ = &program;
S
superjomn 已提交
41 42
    valid_places_ = valid_places;
    CHECK(!valid_places.empty()) << "At least one valid_place should be set";
S
superjomn 已提交
43 44
    CHECK(!graph_) << "duplicate optimize found";
    graph_.reset(new mir::SSAGraph);
S
superjomn 已提交
45
    graph_->Build(program, valid_places);
C
Chunwei 已提交
46 47
    graph_->SetValidPlaces(valid_places);

48
    SpecifyKernelPickTactic(kernel_pick_factor);
S
superjomn 已提交
49
    InitTargetTypeTransformPass();
S
Superjomn 已提交
50 51

    if (passes.empty()) {
X
xingzhaolong 已提交
52 53 54
      RunPasses(std::vector<std::string>{
          {"lite_quant_dequant_fuse_pass",  //
           "lite_conv_bn_fuse_pass",        //
C
Chunwei 已提交
55 56 57 58 59 60
// This pass is disabled to force some opencl kernels selected for final
// running, otherwise, they will be fused to ARM fusion kernels, and the OpenCL
// devices will be discarded.
// TODO(Superjomn) Refine the fusion related design to select fusion kernels for
// devices automatically.
#ifndef LITE_WITH_OPENCL
X
xingzhaolong 已提交
61
           "lite_conv_elementwise_add_activation_fuse_pass",  //
C
Chunwei 已提交
62
#endif
X
xingzhaolong 已提交
63 64
           "lite_fc_fuse_pass",              //
           "identity_scale_eliminate_pass",  //
65
#ifdef LITE_WITH_LIGHT_WEIGHT_FRAMEWORK
C
Chunwei 已提交
66
#ifndef LITE_WITH_OPENCL
X
xingzhaolong 已提交
67
           "lite_elementwise_add_activation_fuse_pass",  //
C
Chunwei 已提交
68
#endif
69
#endif
X
xingzhaolong 已提交
70 71 72
           "static_kernel_pick_pass",        //
           "variable_place_inference_pass",  //
           "argument_type_display_pass",     //
C
Chunwei 已提交
73
           "type_target_cast_pass",          //
X
xingzhaolong 已提交
74 75 76 77
           "variable_place_inference_pass",  //
           "argument_type_display_pass",     //
           "io_copy_kernel_pick_pass",       //
           "variable_place_inference_pass",  //
C
Chunwei 已提交
78
           "type_precision_cast_pass",       //
X
xingzhaolong 已提交
79 80 81 82
           "argument_type_display_pass",     //
           "trans_weight_pass",              //
           "runtime_context_assign_pass",    //
           "graph_visualze"}});
S
Superjomn 已提交
83 84 85
    } else {
      RunPasses(passes);
    }
Y
Yan Chunwei 已提交
86
    exec_scope_ = program.exec_scope();
S
superjomn 已提交
87 88
  }

S
superjomn 已提交
89 90 91 92 93 94 95
  void KernelPickPreferPlace(const Place& place) {
    auto* pass = mir::PassManager::Global().LookUp<mir::StaticKernelPickPass>(
        "static_kernel_pick_pass");
    CHECK(pass);
    pass->SetPreferPlace(place);
  }

S
superjomn 已提交
96
  // Generate a new program based on the mir graph.
S
Superjomn 已提交
97
  std::unique_ptr<RuntimeProgram> GenRuntimeProgram() {
S
superjomn 已提交
98
    LOG(INFO) << "generate program";
S
superjomn 已提交
99
    std::unique_ptr<Program> res;
S
Superjomn 已提交
100 101
    auto pass = mir::PassManager::Global().LookUp<mir::GenerateProgramPass>(
        "generate_program_pass");
S
superjomn 已提交
102
    pass->Apply(graph_);
103 104 105 106
    auto program = pass->GenProgram();
    CHECK(exec_scope_);
    program->set_exec_scope(exec_scope_);
    return program;
S
superjomn 已提交
107
  }
S
superjomn 已提交
108

S
superjomn 已提交
109
  void InitTargetTypeTransformPass() {
110 111
    auto* pass =
        mir::PassManager::Global().LookUp<mir::TypeTargetTransformPass>(
C
Chunwei 已提交
112
            "type_target_cast_pass");
S
superjomn 已提交
113 114 115 116 117 118
    CHECK(pass);
    CHECK(!valid_places_.empty());
    LOG(INFO) << "valid_places.size " << valid_places_.size();
    pass->SetValidPlaces(valid_places_);
  }

S
superjomn 已提交
119 120 121 122 123 124 125 126
  // 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_;
  }

S
Superjomn 已提交
127 128 129 130 131
  mir::SSAGraph* mutable_ssa_graph() {
    CHECK(graph_);
    return graph_.get();
  }

S
superjomn 已提交
132
 protected:
133 134
  void SpecifyKernelPickTactic(core::KernelPickFactor factor);

S
superjomn 已提交
135
  // Specify the passes and run them.
S
Superjomn 已提交
136 137 138 139
  void RunPasses(const std::vector<std::string>& passes) {
    for (auto& x : passes) {
      LOG(INFO) << "== Running pass " << x;
      auto* pass = mir::PassManager::Global().LookUp(x);
X
xingzhaolong 已提交
140
      CHECK(pass) << "Can not find pass: " << x;
S
Superjomn 已提交
141 142 143
      pass->Apply(graph_);
    }
  }
S
superjomn 已提交
144 145 146

 private:
  std::unique_ptr<mir::SSAGraph> graph_;
S
superjomn 已提交
147
  std::vector<Place> valid_places_;
148
  lite::Scope* exec_scope_{};
S
Superjomn 已提交
149
  Program* program_{};
S
superjomn 已提交
150 151 152 153
};

}  // namespace lite
}  // namespace paddle