ipu_backend.h 3.0 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 66 67 68
  // detach IPU manually
  void Detach();

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

A
Allen Guo 已提交
70 71
  void SetScope(const Scope &scope);
  const Scope *GetScope() { return scope_; }
J
jianghaicheng 已提交
72 73
  void SetIpuStrategy(const IpuStrategy &strategy);
  const IpuStrategy *GetIpuStrategy() { return ipu_strategy_; }
A
Allen Guo 已提交
74
  void SetCustomOps(const std::vector<IpuCustomOpIdentifier> &custom_ops);
J
jianghaicheng 已提交
75

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

 private:
  void Prepare();

 private:
A
Allen Guo 已提交
83
  std::unique_ptr<Compiler> compiler_;
J
jianghaicheng 已提交
84
  std::unique_ptr<Executor> executor_;
A
Allen Guo 已提交
85
  bool is_compiled_ = false;
J
jianghaicheng 已提交
86 87 88
  bool is_prepared_ = false;

  // not own
A
Allen Guo 已提交
89
  const Scope *scope_ = nullptr;
J
jianghaicheng 已提交
90 91 92
  const IpuStrategy *ipu_strategy_ = nullptr;

 private:
A
Allen Guo 已提交
93 94 95 96
  // time record for IpuBackend::Run
  std::unique_ptr<platform::Timer> timer_;

  DISABLE_COPY_AND_ASSIGN(IpuBackend);
J
jianghaicheng 已提交
97 98 99 100 101
};

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