optimizer.h 2.5 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 20
#include "paddle/fluid/lite/core/mir/pass_manager.h"
#include "paddle/fluid/lite/core/mir/ssa_graph.h"
S
Superjomn 已提交
21
#include "paddle/fluid/lite/core/program.h"
22
#include "paddle/fluid/lite/core/types.h"
S
superjomn 已提交
23 24 25 26 27 28 29 30 31 32

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 已提交
33
  void Run(Program&& program, const std::vector<Place>& valid_places,
34
           core::KernelPickFactor kernel_pick_factor,
S
superjomn 已提交
35 36 37
           const std::vector<std::string>& passes = {}) {
    CHECK(!graph_) << "duplicate optimize found";
    graph_.reset(new mir::SSAGraph);
S
superjomn 已提交
38
    graph_->Build(program, valid_places);
39
    SpecifyKernelPickTactic(kernel_pick_factor);
S
superjomn 已提交
40
    RunPasses();
41
    exec_scope_ = program.exec_scope;
S
superjomn 已提交
42 43 44
  }

  // Generate a new program based on the mir graph.
S
Superjomn 已提交
45
  std::unique_ptr<RuntimeProgram> GenRuntimeProgram() {
S
superjomn 已提交
46
    std::unique_ptr<Program> res;
S
Superjomn 已提交
47 48
    auto pass = mir::PassManager::Global().LookUp<mir::GenerateProgramPass>(
        "generate_program_pass");
49 50 51 52
    auto program = pass->GenProgram();
    CHECK(exec_scope_);
    program->set_exec_scope(exec_scope_);
    return program;
S
superjomn 已提交
53
  }
S
superjomn 已提交
54 55 56 57 58 59 60 61 62 63

  // 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:
64 65
  void SpecifyKernelPickTactic(core::KernelPickFactor factor);

S
superjomn 已提交
66
  // Run the default passes registered in the PassManager.
S
superjomn 已提交
67
  void RunPasses() { mir::PassManager::Global().Run(graph_); }
S
superjomn 已提交
68 69 70 71 72 73

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

 private:
  std::unique_ptr<mir::SSAGraph> graph_;
74
  lite::Scope* exec_scope_{};
S
superjomn 已提交
75 76 77 78
};

}  // namespace lite
}  // namespace paddle