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
#include "paddle/fluid/platform/device/ipu/ipu_backend.h"
J
jianghaicheng 已提交
16

17 18 19
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/platform/device/ipu/ipu_compiler.h"
#include "paddle/fluid/platform/device/ipu/ipu_executor.h"
J
jianghaicheng 已提交
20 21 22 23 24

namespace paddle {
namespace platform {
namespace ipu {

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

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

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

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

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

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

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

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

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

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

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

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