optimizer.h 3.3 KB
Newer Older
S
superjomn 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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
#include <string>
#include <vector>
S
Superjomn 已提交
18
#include "paddle/fluid/lite/core/mir/generate_program_pass.h"
S
superjomn 已提交
19
#include "paddle/fluid/lite/core/mir/io_complement_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"
S
Superjomn 已提交
23
#include "paddle/fluid/lite/core/program.h"
24
#include "paddle/fluid/lite/core/types.h"
S
superjomn 已提交
25 26 27 28 29 30 31 32 33 34

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

S
superjomn 已提交
49 50 51 52 53 54 55
  void KernelPickPreferPlace(const Place& place) {
    auto* pass = mir::PassManager::Global().LookUp<mir::StaticKernelPickPass>(
        "static_kernel_pick_pass");
    CHECK(pass);
    pass->SetPreferPlace(place);
  }

S
superjomn 已提交
56
  // Generate a new program based on the mir graph.
S
Superjomn 已提交
57
  std::unique_ptr<RuntimeProgram> GenRuntimeProgram() {
S
superjomn 已提交
58
    LOG(INFO) << "generate program";
S
superjomn 已提交
59
    std::unique_ptr<Program> res;
S
Superjomn 已提交
60 61
    auto pass = mir::PassManager::Global().LookUp<mir::GenerateProgramPass>(
        "generate_program_pass");
S
superjomn 已提交
62
    pass->Apply(graph_);
63 64 65 66
    auto program = pass->GenProgram();
    CHECK(exec_scope_);
    program->set_exec_scope(exec_scope_);
    return program;
S
superjomn 已提交
67
  }
S
superjomn 已提交
68

S
superjomn 已提交
69 70 71 72 73 74 75 76 77
  void InitIoComplement() {
    auto* pass = mir::PassManager::Global().LookUp<mir::IoComplementPass>(
        "io_complement_pass");
    CHECK(pass);
    CHECK(!valid_places_.empty());
    LOG(INFO) << "valid_places.size " << valid_places_.size();
    pass->SetValidPlaces(valid_places_);
  }

S
superjomn 已提交
78 79 80 81 82 83 84 85 86
  // 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_;
  }

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

S
superjomn 已提交
89
  // Run the default passes registered in the PassManager.
S
superjomn 已提交
90
  void RunPasses();
S
superjomn 已提交
91 92 93 94 95 96

  // Specify the passes and run them.
  void RunPasses(std::vector<std::string>& passes);

 private:
  std::unique_ptr<mir::SSAGraph> graph_;
S
superjomn 已提交
97
  std::vector<Place> valid_places_;
98
  lite::Scope* exec_scope_{};
S
superjomn 已提交
99 100 101 102
};

}  // namespace lite
}  // namespace paddle