ipu_backend.cc 3.2 KB
Newer Older
J
jianghaicheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2021 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. */

A
Allen Guo 已提交
15 16
#include "paddle/fluid/platform/device/ipu/ipu_backend.h"
#include "paddle/fluid/platform/device/ipu/ipu_utils.h"
J
jianghaicheng 已提交
17 18 19 20 21 22 23 24 25 26

#include "paddle/fluid/framework/framework.pb.h"
#include "paddle/fluid/framework/ir/graph.h"
#include "paddle/fluid/framework/ir/graph_helper.h"
#include "paddle/fluid/framework/ir/node.h"

namespace paddle {
namespace platform {
namespace ipu {

A
Allen Guo 已提交
27 28 29 30
IpuBackend* IpuBackend::GetInstance() {
  static IpuBackend instance;
  return &instance;
}
J
jianghaicheng 已提交
31 32

IpuBackend::IpuBackend() {
A
Allen Guo 已提交
33
  compiler_ = std::make_unique<Compiler>();
J
jianghaicheng 已提交
34
  executor_ = std::make_unique<Executor>();
A
Allen Guo 已提交
35
  timer_ = std::make_unique<platform::Timer>();
J
jianghaicheng 已提交
36 37
}

A
Allen Guo 已提交
38 39
IpuBackend::~IpuBackend() {
  compiler_.reset();
J
jianghaicheng 已提交
40 41 42
  executor_.reset();
}

A
Allen Guo 已提交
43
void IpuBackend::Compile(Graph* graph,
J
jianghaicheng 已提交
44 45 46
                         const std::vector<std::string>& feed_list,
                         const std::vector<std::string>& fetch_list) {
  VLOG(10) << "enter IpuBackend::Compile";
A
Allen Guo 已提交
47
  is_compiled_ = false;
A
Allen Guo 已提交
48 49 50 51 52
  compiler_->Prepare(graph);
  compiler_->InitInputs(feed_list);
  compiler_->LowerConstants(scope_);
  compiler_->LowerWeights(scope_);
  compiler_->LowerBody();
J
jianghaicheng 已提交
53
  compiler_->InitOutputs(fetch_list);
A
Allen Guo 已提交
54
  if (ipu_strategy_->is_training) {
A
Allen Guo 已提交
55
    compiler_->LowerOptimizer(scope_);
A
Allen Guo 已提交
56
  }
A
Allen Guo 已提交
57 58 59
  if (!ipu_strategy_->onnx_dump_path.empty()) {
    SaveModelProto(ipu_strategy_->onnx_dump_path);
  }
A
Allen Guo 已提交
60
  executor_->SetCompilerResources(compiler_->GetResources());
A
Allen Guo 已提交
61
  executor_->Prepare(compiler_->GetModelProto());
A
Allen Guo 已提交
62
  is_compiled_ = true;
J
jianghaicheng 已提交
63 64 65
  VLOG(10) << "leave IpuBackend::Compile";
}

A
Allen Guo 已提交
66 67
void IpuBackend::Run(const std::vector<const Tensor*>& inputs,
                     const std::vector<Tensor*>& outputs,
J
jianghaicheng 已提交
68
                     const framework::ExecutionContext& ctx) {
A
Allen Guo 已提交
69 70 71 72
  timer_->Start();
  executor_->Run(inputs, outputs, ctx);
  timer_->Pause();
  VLOG(10) << "[IPU Run]: " << timer_->ElapsedMS() << " (ms)";
J
jianghaicheng 已提交
73 74
}

A
Allen Guo 已提交
75
void IpuBackend::WeightsToHost() { executor_->WeightsToHost(); }
J
jianghaicheng 已提交
76

A
Allen Guo 已提交
77 78 79 80 81 82 83 84 85
void IpuBackend::Detach() { executor_->Detach(); }

void IpuBackend::Reset() {
  executor_->Detach();
  compiler_.reset();
  executor_.reset();
}

void IpuBackend::SetScope(const Scope& scope) {
J
jianghaicheng 已提交
86 87 88 89 90 91 92
  scope_ = &scope;
  executor_->SetScope(&scope);
}

void IpuBackend::SetIpuStrategy(const IpuStrategy& strategy) {
  ipu_strategy_ = &strategy;
  compiler_->SetIpuStrategy(strategy);
A
Allen Guo 已提交
93
  executor_->SetIpuStrategy(strategy);
A
Allen Guo 已提交
94 95 96
  if (!strategy.custom_ops.empty()) {
    compiler_->SetCustomOps(strategy.custom_ops);
  }
J
jianghaicheng 已提交
97 98
}

A
Allen Guo 已提交
99
void IpuBackend::SaveModelProto(const std::string& path) {
A
Allen Guo 已提交
100
  if (ipu_strategy_->is_training && is_compiled_) {
A
Allen Guo 已提交
101 102
    executor_->SaveModelToHost(path);
  } else {
A
Allen Guo 已提交
103
    compiler_->SaveModelProtoNoCheck(path);
J
jianghaicheng 已提交
104 105 106 107 108 109
  }
}

}  // namespace ipu
}  // namespace platform
}  // namespace paddle