ipu_backend.h 2.9 KB
Newer Older
J
jianghaicheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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. */

#pragma once

#include <popart/devicemanager.hpp>
#include <popart/names.hpp>
A
Allen Guo 已提交
19
#include <popart/tensorinfo.hpp>
J
jianghaicheng 已提交
20 21 22 23

#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/framework/tensor.h"
A
Allen Guo 已提交
24 25 26 27 28
#include "paddle/fluid/platform/device/ipu/ipu_compiler.h"
#include "paddle/fluid/platform/device/ipu/ipu_device.h"
#include "paddle/fluid/platform/device/ipu/ipu_executor.h"
#include "paddle/fluid/platform/device/ipu/ipu_strategy.h"
#include "paddle/fluid/platform/device/ipu/ipu_utils.h"
J
jianghaicheng 已提交
29
#include "paddle/fluid/platform/enforce.h"
A
Allen Guo 已提交
30
#include "paddle/fluid/platform/timer.h"
J
jianghaicheng 已提交
31 32 33 34 35

namespace paddle {
namespace platform {
namespace ipu {

A
Allen Guo 已提交
36 37 38 39 40
// IpuBackend is the center of paddle-ipu, its function include:
//   1. Compile paddle model to popart model
//   2. Run popart model, inference or training
//   3. Request and release device
//   4. Other helper function
J
jianghaicheng 已提交
41
class IpuBackend {
A
Allen Guo 已提交
42 43
 public:
  static IpuBackend *GetInstance();
J
jianghaicheng 已提交
44 45 46 47 48 49 50 51

 public:
  IpuBackend();
  ~IpuBackend();

  // what compile does include(call compiler_):
  //   1. map paddle-op -> poart op
  //   2. construct popart onnx compute graph
A
Allen Guo 已提交
52
  void Compile(Graph *graph, const std::vector<std::string> &feed_list,
J
jianghaicheng 已提交
53 54 55 56 57 58
               const std::vector<std::string> &fetch_list);

  // what run does include:
  //   1. construct forward onnx graph
  //   2. graph-level optimization
  //   3. autodiff
A
Allen Guo 已提交
59 60
  void Run(const std::vector<const Tensor *> &inputs,
           const std::vector<Tensor *> &outputs,
J
jianghaicheng 已提交
61 62
           const framework::ExecutionContext &ctx);

A
Allen Guo 已提交
63 64 65
  // Sync weights from IPU while training
  void WeightsToHost();

A
Allen Guo 已提交
66 67 68 69 70 71
  // detach IPU manually
  void Detach();

  // reset manually
  // call it before destruct works
  void Reset();
J
jianghaicheng 已提交
72

A
Allen Guo 已提交
73 74
  void SetScope(const Scope &scope);
  const Scope *GetScope() { return scope_; }
J
jianghaicheng 已提交
75 76 77
  void SetIpuStrategy(const IpuStrategy &strategy);
  const IpuStrategy *GetIpuStrategy() { return ipu_strategy_; }

A
Allen Guo 已提交
78
  // save compiled model to onnx
A
Allen Guo 已提交
79
  void SaveModelProto(const std::string &path);
J
jianghaicheng 已提交
80 81 82

 private:
  // not own
A
Allen Guo 已提交
83
  const Scope *scope_ = nullptr;
J
jianghaicheng 已提交
84 85
  const IpuStrategy *ipu_strategy_ = nullptr;

A
Allen Guo 已提交
86 87 88
  // own
  std::unique_ptr<Compiler> compiler_;
  std::unique_ptr<Executor> executor_;
A
Allen Guo 已提交
89 90
  std::unique_ptr<platform::Timer> timer_;

A
Allen Guo 已提交
91 92
  bool is_compiled_ = false;

A
Allen Guo 已提交
93
  DISABLE_COPY_AND_ASSIGN(IpuBackend);
J
jianghaicheng 已提交
94 95 96 97 98
};

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