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>
J
jianghaicheng 已提交
23 24 25

#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/scope.h"
26 27 28 29
#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 已提交
30 31 32 33 34

namespace paddle {
namespace platform {
namespace ipu {

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

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);

56 57
  // detach IPU
  void Detach();
J
jianghaicheng 已提交
58 59

  void SetWeightsIO();
60
  void ConvertWeights(bool align_to_popart);
J
jianghaicheng 已提交
61 62 63 64
  void WeightsFromPaddle();
  void WeightsToPaddle();

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

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

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

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

 private:
81 82 83 84 85
  void AcquireDevice();

 private:
  // not own
  const Scope *scope_ = nullptr;
J
jianghaicheng 已提交
86
  const IpuStrategy *ipu_strategy_ = nullptr;
87 88 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_;

  int step_ = 0;
J
jianghaicheng 已提交
97 98 99 100 101
};

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