ipu_backend.cc 3.3 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 35 36
  executor_ = std::make_unique<Executor>();
}

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

A
Allen Guo 已提交
42
void IpuBackend::Compile(Graph* graph,
J
jianghaicheng 已提交
43 44 45
                         const std::vector<std::string>& feed_list,
                         const std::vector<std::string>& fetch_list) {
  VLOG(10) << "enter IpuBackend::Compile";
A
Allen Guo 已提交
46 47 48
  compiler_->Prepare();
  executor_->SetCompilerResources(compiler_->GetResources());

J
jianghaicheng 已提交
49
  compiler_->InitInputs(graph, feed_list);
A
Allen Guo 已提交
50
  compiler_->LowerConstants(graph, scope_);
J
jianghaicheng 已提交
51 52 53
  compiler_->LowerWeights(graph, scope_);
  compiler_->LowerBody(graph);
  compiler_->InitOutputs(fetch_list);
A
Allen Guo 已提交
54 55 56 57 58 59
  if (ipu_strategy_->is_training) {
    compiler_->LowerOptimier(graph, scope_);
  }
  is_compiled_ = true;
  // when call compile, means a new graph
  is_prepared_ = false;
J
jianghaicheng 已提交
60 61 62
  VLOG(10) << "leave IpuBackend::Compile";
}

A
Allen Guo 已提交
63 64
void IpuBackend::Run(const std::vector<const Tensor*>& inputs,
                     const std::vector<Tensor*>& outputs,
J
jianghaicheng 已提交
65 66
                     const framework::ExecutionContext& ctx) {
  Prepare();
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 73
}

void IpuBackend::Prepare() {
A
Allen Guo 已提交
74 75 76
  if (!is_prepared_) {
    executor_->Prepare(compiler_->GetModelProto());
    timer_.reset(new platform::Timer());
J
jianghaicheng 已提交
77 78 79 80
    is_prepared_ = true;
  }
}

A
Allen Guo 已提交
81 82 83 84 85 86 87 88 89
void IpuBackend::Detach() { executor_->Detach(); }

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

void IpuBackend::SetScope(const Scope& scope) {
J
jianghaicheng 已提交
90 91 92 93 94 95 96
  scope_ = &scope;
  executor_->SetScope(&scope);
}

void IpuBackend::SetIpuStrategy(const IpuStrategy& strategy) {
  ipu_strategy_ = &strategy;
  compiler_->SetIpuStrategy(strategy);
A
Allen Guo 已提交
97
  executor_->SetIpuStrategy(strategy);
J
jianghaicheng 已提交
98 99
}

A
Allen Guo 已提交
100 101 102
void IpuBackend::SetCustomOps(
    const std::vector<IpuCustomOpIdentifier>& custom_ops) {
  compiler_->SetCustomOps(custom_ops);
J
jianghaicheng 已提交
103 104
}

A
Allen Guo 已提交
105 106 107 108 109 110 111
void IpuBackend::SaveMoldeProto(const std::string& path) {
  if (ipu_strategy_->is_training && is_prepared_) {
    executor_->SaveModelToHost(path);
  } else if (is_compiled_) {
    compiler_->SaveModelProtoNoCheck(path);
  } else {
    LOG(WARNING) << "Model is empty";
J
jianghaicheng 已提交
112 113 114 115 116 117
  }
}

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