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

A
Allen Guo 已提交
17
#include <popart/patterns/patterns.hpp>
J
jianghaicheng 已提交
18
#include <popart/sessionoptions.hpp>
A
Allen Guo 已提交
19
#include <popart/tensorlocation.hpp>
A
Allen Guo 已提交
20
#include "paddle/fluid/platform/device/ipu/ipu_utils.h"
A
Allen Guo 已提交
21
#include "paddle/fluid/platform/enforce.h"
J
jianghaicheng 已提交
22 23 24 25 26

namespace paddle {
namespace platform {
namespace ipu {

A
Allen Guo 已提交
27 28 29 30 31
struct RuntimeOptions {
  // enable the eval mode in training by switching optimizers.
  bool enable_eval = false;
};

32 33
class IpuStrategy {
 public:
A
Allen Guo 已提交
34
  IpuStrategy();
A
Allen Guo 已提交
35

A
Allen Guo 已提交
36
  // TODO(alleng) create PaddleOptions
A
Allen Guo 已提交
37
  // training flag, true for training
J
jianghaicheng 已提交
38
  bool is_training = true;
A
Allen Guo 已提交
39

A
Allen Guo 已提交
40
  // Average sharding, debugging used
J
jianghaicheng 已提交
41
  bool need_avg_shard = false;
A
Allen Guo 已提交
42

A
Allen Guo 已提交
43
  // Flag for fp16, true for pure fp16
J
jianghaicheng 已提交
44
  bool enable_fp16 = false;
A
Allen Guo 已提交
45

A
Allen Guo 已提交
46 47 48 49 50
  // The mode of Adam/Lamb optimizer
  // false: The standard Adam/Lamb optimizer
  // true: The Adam_No_Bias/Lamb_No_Bias optimizer from PopART
  bool use_no_bias_optimizer = false;

A
Allen Guo 已提交
51
  // Enable distributed computing for POD128 or POD256
A
Allen Guo 已提交
52 53
  bool enable_distribution = false;

A
Allen Guo 已提交
54 55 56
  // Enable Scaled optimizer state only for Adam and Lamb
  bool scaled_optimizer_state = false;

A
Allen Guo 已提交
57
  // Number ipus total needed, local_replica * ipu_per_replica
A
Allen Guo 已提交
58 59
  int num_ipus = 1;

A
Allen Guo 已提交
60
  // Batches per step
A
Allen Guo 已提交
61 62
  int batches_per_step = 1;

A
Allen Guo 已提交
63
  // Micro batch-size
A
Allen Guo 已提交
64 65
  int micro_batch_size = 1;

A
Allen Guo 已提交
66
  // Random seed
A
Allen Guo 已提交
67
  std::uint64_t random_seed = std::numeric_limits<std::uint64_t>::max();
A
Allen Guo 已提交
68

A
Allen Guo 已提交
69
  // Available memory proportion, 0.0f for disable
A
Allen Guo 已提交
70 71
  float available_memory_proportion = 0.0f;

A
Allen Guo 已提交
72
  // Loss scaling, currently we can't get loss scaling from
A
Allen Guo 已提交
73 74 75
  // optimizer_extract_pass, so we have to set it here
  float loss_scaling = 1.0f;

A
Allen Guo 已提交
76
  // DefaultMaxWeightNorm for adam optimizer
A
Allen Guo 已提交
77 78
  float max_weight_norm = 65504.0f;

A
Allen Guo 已提交
79
  // File path for dumping compiled model in onnx format
A
Allen Guo 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  std::string onnx_dump_path;

  // Data type to use for tensor that stores first-order momentum optimizer
  // state. FLOAT or FLOAT16
  std::string accl1_type = "FLOAT";

  // Data type to use for tensor that stores second-order momentum optimizer
  // state. FLOAT or FLOAT16
  std::string accl2_type = "FLOAT";

  // Data type to use for tensor that stores third-order momentum optimizer
  // state. FLOAT or FLOAT16
  std::string accl3_type = "FLOAT";

  // WeightDecayMode for setting the optimizer
  // if set, it will override other settings
  // value must be one of "decay" or "l2_regularization" or not set
  std::string weight_decay_mode = "";

  // Runtime Options
  RuntimeOptions runtime_options;

A
Allen Guo 已提交
102 103
  // popart session option
  popart::SessionOptions popart_options;
A
Allen Guo 已提交
104 105

  // popart pattern manager
A
Allen Guo 已提交
106 107
  popart::Patterns popart_patterns;

A
Allen Guo 已提交
108
  // Custom ops
A
Allen Guo 已提交
109 110
  std::vector<IpuCustomOpIdentifier> custom_ops;

111 112 113 114 115 116 117 118 119 120
 public:
  void AddBoolOption(const std::string &option, bool value);
  void AddUint64Option(const std::string &option, std::uint64_t value);
  void AddDoubleOption(const std::string &option, double value);
  void AddStringOption(const std::string &option, const std::string &value);
  void InsertStringOption(const std::string &option, const std::string &value);
  void InsertStringPairOption(const std::string &option, const std::string &key,
                              const std::string &value);
  void SetTensorLocation(const std::string &tensor, const std::string &option,
                         std::uint64_t value);
121
  void SetReplicatedCollectivesSettings(const std::string &opt, bool value);
A
Allen Guo 已提交
122 123
  void SetAccumulateOuterFragmentSettings(const std::uint64_t &schedule,
                                          const std::vector<int> &values);
124 125
  void AddCustomOp(const std::string &paddle_op, const std::string &popart_op,
                   const std::string &domain, int version);
A
Allen Guo 已提交
126 127
  void SetCompilationProgressLogger(
      const std::function<void(int, int)> &logger);
A
Allen Guo 已提交
128

129 130 131 132 133 134 135 136 137
  std::string GetOption(const std::string &);
  std::vector<std::string> GetVectorOption(const std::string &);
  std::map<std::string, std::string> GetMapOption(const std::string &);
  std::string GetOptionType(const std::string &);
  std::vector<std::string> GetAllOptionNames();

  void EnablePattern(const std::string &t);
  void DisablePattern(const std::string &t);
  const bool IsPatternEnabled(const std::string &t);
A
Allen Guo 已提交
138

139
 private:
A
Allen Guo 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  template <typename ValueType>
  void set(
      const std::string &key, ValueType value,
      std::map<std::string, std::function<void(ValueType)>> &options,  // NOLINT
      const std::string &type_str) {
    auto it = options.find(key);
    PADDLE_ENFORCE_NE(it, options.end(), platform::errors::InvalidArgument(
                                             "Cannot find option: %s, type: %s "
                                             "when setting IpuStrategy options",
                                             key, type_str));
    it->second(value);
  }

  template <typename ValueType>
  ValueType get(
      const std::string &key,
      std::map<std::string, std::function<ValueType()>> &options) {  // NOLINT
    auto it = options.find(key);
    PADDLE_ENFORCE_NE(
        it, options.end(),
        platform::errors::InvalidArgument(
            "Cannot find option name: %s when trying to get IpuStrategy option",
            key));
    return it->second();
  }

166 167 168 169 170 171 172
  std::map<std::string, std::function<void(bool)>> bool_options;
  std::map<std::string, std::function<void(std::uint64_t)>> uint64_options;
  std::map<std::string, std::function<void(double)>> double_options;
  std::map<std::string, std::function<void(std::string)>> string_options;
  std::map<std::string,
           std::function<void(std::pair<std::string, std::string>)>>
      container_options;
A
Allen Guo 已提交
173

174 175 176 177 178 179
  std::map<std::string, std::function<std::string()>> options_getter;
  std::map<std::string, std::function<std::vector<std::string>()>>
      vector_options_getter;
  std::map<std::string, std::function<std::map<std::string, std::string>()>>
      map_options_getter;
  std::map<std::string, std::string> options_type;
J
jianghaicheng 已提交
180 181 182 183 184
};

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