ipu_compiler.h 4.5 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/builder.hpp>
#include <popart/graphtransformer.hpp>
A
Allen Guo 已提交
19
#include <popart/optimizer.hpp>
J
jianghaicheng 已提交
20 21
#include "paddle/fluid/framework/ir/graph.h"
#include "paddle/fluid/framework/scope.h"
A
Allen Guo 已提交
22 23 24
#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 已提交
25 26 27 28 29

namespace paddle {
namespace platform {
namespace ipu {

A
Allen Guo 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
struct CompilerResources {
  // popart input tensor_ids
  std::vector<popart::TensorId> inputs;
  // popart output tensor_ids
  std::vector<popart::TensorId> outputs;
  // <paddle_var_name, popart_tensor_ids>
  std::map<std::string, popart::TensorId> tensors;
  // popart_weight_ids
  std::vector<popart::TensorId> weights;
  // popart loss tensor_id
  popart::TensorId loss_var;
  // paddle lr var_name
  std::string lr_var;
  // lr value
  float lr;
  // flag for lr is constant or scheduling
  bool with_lr_sched = false;
  // paddle optimizer type, eg: momentum, lamb
  std::string optimizer_type;

  using OptimizerFn =
      std::function<std::unique_ptr<popart::Optimizer>(float lr)>;
  OptimizerFn optimizer_fn;

 public:
  popart::Optimizer *Optimizer() { return optimizer.get(); }

  popart::Optimizer *NewOptimizer() {
    optimizer = optimizer_fn(lr);
    return optimizer.get();
  }

  popart::Optimizer *UpdateOptimizer(float lr_new) {
    optimizer = optimizer_fn(lr_new);
    return optimizer.get();
  }

 private:
  std::unique_ptr<popart::Optimizer> optimizer;
};

J
jianghaicheng 已提交
71 72 73 74
class Compiler {
 public:
  Compiler();
  ~Compiler();
A
Allen Guo 已提交
75

J
jianghaicheng 已提交
76
  void RegisterOpFunc();
A
Allen Guo 已提交
77 78 79
  void Prepare();
  void LowerBody(const Graph *graph);
  void InitInputs(Graph *graph, const std::vector<std::string> &feed_list);
J
jianghaicheng 已提交
80
  void InitOutputs(const std::vector<std::string> &fetch_list);
A
Allen Guo 已提交
81 82 83
  void LowerConstants(const Graph *graph, const Scope *scope);
  void LowerWeights(const Graph *graph, const Scope *scope);
  void LowerOptimier(const Graph *graph, const Scope *scope);
J
jianghaicheng 已提交
84 85 86 87 88 89

  void InsertTensors(const std::vector<std::string> &output_names,
                     const std::vector<std::string> &tensor_ids);
  void InsertTensors(const std::vector<std::string> &output_names,
                     const std::string &tensor_id);
  void SetIpuIndexStage(const std::vector<std::string> &tensor_ids,
A
Allen Guo 已提交
90 91 92 93 94 95 96 97 98
                        const OpDesc *op_desc);
  void SetIpuIndexStage(const std::string &tensor_id, const OpDesc *op_desc);
  void SetAMPAttributes(const std::vector<std::string> &tensor_ids,
                        const OpDesc *op_desc);
  void SetAMPAttributes(const std::string &tensor_id, const OpDesc *op_desc);
  void SetSerializeAttributes(const std::vector<std::string> &tensor_ids,
                              const OpDesc *op_desc);
  void SetSerializeAttributes(const std::string &tensor_id,
                              const OpDesc *op_desc);
J
jianghaicheng 已提交
99 100 101

  void SetIpuStrategy(const IpuStrategy &strategy) {
    ipu_strategy_ = &strategy;
A
Allen Guo 已提交
102 103 104 105 106 107 108 109 110
  }

  void SetCustomOps(const std::vector<IpuCustomOpIdentifier> &custom_ops);

  CompilerResources *GetResources() { return resources_.get(); }

  std::string GetModelProto();
  std::string GetFP16ModelProto();

J
jianghaicheng 已提交
111 112 113 114
  void SaveModelProto(const std::string &path);
  void SaveModelProtoNoCheck(const std::string &path);

 private:
A
Allen Guo 已提交
115 116 117
  std::vector<std::string> GetOpInputs(const OpDesc *op);
  const std::vector<std::string> &GetOpOutputs(const OpDesc *op);
  popart::DebugContext BuildDebugContext(const OpDesc *op);
J
jianghaicheng 已提交
118 119 120

 private:
  std::unique_ptr<popart::Builder> builder_;
A
Allen Guo 已提交
121
  std::unique_ptr<CompilerResources> resources_;
J
jianghaicheng 已提交
122

A
Allen Guo 已提交
123
  using OpFunc = std::function<void(OpDesc *op_desc)>;
J
jianghaicheng 已提交
124 125 126 127 128 129 130
  std::unordered_map<std::string, OpFunc> name_function_;

  // feed_list_ & fetch_list save paddle tensor id
  std::vector<std::string> feed_list_;
  std::vector<std::string> fetch_list_;

  const IpuStrategy *ipu_strategy_ = nullptr;
A
Allen Guo 已提交
131
  std::map<std::string, IpuCustomOpIdentifier> custom_ops_;
J
jianghaicheng 已提交
132 133 134 135 136
};

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