ipu_executor.h 2.8 KB
Newer Older
J
jianghaicheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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/dataflow.hpp>
18
#include <popart/half.hpp>
J
jianghaicheng 已提交
19
#include <popart/names.hpp>
20
#include <popart/patterns/patterns.hpp>
J
jianghaicheng 已提交
21
#include <popart/session.hpp>
22
#include <popart/tensorinfo.hpp>
A
Allen Guo 已提交
23
#include <popdist/popdist_poplar.hpp>
J
jianghaicheng 已提交
24 25 26

#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/scope.h"
27 28 29 30
#include "paddle/fluid/platform/device/ipu/ipu_compiler.h"
#include "paddle/fluid/platform/device/ipu/ipu_names.h"
#include "paddle/fluid/platform/device/ipu/ipu_strategy.h"
#include "paddle/fluid/platform/device/ipu/ipu_utils.h"
J
jianghaicheng 已提交
31 32 33 34 35

namespace paddle {
namespace platform {
namespace ipu {

36 37 38 39
struct ExecutorResources {
  // map<tensor_id, paddle_var_ptr>
  popart::WeightsIO weights_io;
  // <popart_var, paddle_var> pairs, include weights and optimizer states
A
Allen Guo 已提交
40
  std::vector<std::pair<popart::TensorId, std::string>> weights_and_opt_state;
41 42
};

J
jianghaicheng 已提交
43 44
class Executor {
 public:
45
  Executor() = default;
J
jianghaicheng 已提交
46 47
  ~Executor();

48 49
  // build popart session
  void Prepare(const std::string &proto);
J
jianghaicheng 已提交
50

51 52 53
  // run popart session
  void Run(const std::vector<const Tensor *> &inputs,
           const std::vector<Tensor *> &outputs,
J
jianghaicheng 已提交
54 55
           const framework::ExecutionContext &ctx);

A
Allen Guo 已提交
56 57 58
  // sync weights from popart to paddle
  void WeightsToHost();

59 60
  // detach IPU
  void Detach();
J
jianghaicheng 已提交
61 62

  // Scope
63
  void SetScope(const Scope *scope) { scope_ = scope; }
J
jianghaicheng 已提交
64 65

  // Strategy
66 67 68
  void SetIpuStrategy(const IpuStrategy &strategy) {
    ipu_strategy_ = &strategy;
  }
J
jianghaicheng 已提交
69

70 71 72 73
  // CompilerResources
  void SetCompilerResources(CompilerResources *resources) {
    compiler_resources_ = resources;
  }
J
jianghaicheng 已提交
74

75 76
  // Save model to onnx
  void SaveModelToHost(const std::string &path);
J
jianghaicheng 已提交
77 78

 private:
79
  void AcquireDevice();
A
Allen Guo 已提交
80 81 82 83
  void SetWeightsIO();
  void ConvertWeights(bool);
  void WeightsFromPaddle();
  void WeightsToPaddle();
84 85 86 87

 private:
  // not own
  const Scope *scope_ = nullptr;
J
jianghaicheng 已提交
88
  const IpuStrategy *ipu_strategy_ = nullptr;
89 90 91 92 93 94 95 96
  CompilerResources *compiler_resources_ = nullptr;

  // deviceinfo for popart session
  std::shared_ptr<popart::DeviceInfo> device_;
  // popart session, where graph running
  std::unique_ptr<popart::Session> session_;
  // one OneSession means a graph
  std::unique_ptr<ExecutorResources> executor_resources_;
J
jianghaicheng 已提交
97 98 99 100 101
};

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