ipu_backend.cc 3.2 KB
Newer Older
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. */

15 16
#include "paddle/fluid/platform/device/ipu/ipu_backend.h"
#include "paddle/fluid/platform/device/ipu/ipu_utils.h"
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 {

27 28 29 30
IpuBackend* IpuBackend::GetInstance() {
  static IpuBackend instance;
  return &instance;
}
31 32

IpuBackend::IpuBackend() {
33
  compiler_ = std::make_unique<Compiler>();
34
  executor_ = std::make_unique<Executor>();
35
  timer_ = std::make_unique<platform::Timer>();
36 37
}

38 39
IpuBackend::~IpuBackend() {
  compiler_.reset();
40 41 42
  executor_.reset();
}

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

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

75
void IpuBackend::WeightsToHost() { executor_->WeightsToHost(); }
76

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) {
86 87 88 89 90 91 92
  scope_ = &scope;
  executor_->SetScope(&scope);
}

void IpuBackend::SetIpuStrategy(const IpuStrategy& strategy) {
  ipu_strategy_ = &strategy;
  compiler_->SetIpuStrategy(strategy);
93
  executor_->SetIpuStrategy(strategy);
94 95 96
  if (!strategy.custom_ops.empty()) {
    compiler_->SetCustomOps(strategy.custom_ops);
  }
97 98
}

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

}  // namespace ipu
}  // namespace platform
}  // namespace paddle
新手
引导
客服 返回
顶部