ipu_strategy.h 4.7 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/enforce.h"
J
jianghaicheng 已提交
21 22 23 24 25 26

namespace paddle {
namespace platform {
namespace ipu {

struct IpuStrategy {
A
Allen Guo 已提交
27
  IpuStrategy();
A
Allen Guo 已提交
28

A
Allen Guo 已提交
29
  // TODO(alleng) create PaddleOptions
A
Allen Guo 已提交
30
  // training flag, true for training
J
jianghaicheng 已提交
31
  bool is_training = true;
A
Allen Guo 已提交
32 33

  // save the onnx model lowered by paddle program description
J
jianghaicheng 已提交
34
  bool save_init_onnx = false;
A
Allen Guo 已提交
35 36 37 38 39

  // save the trained model
  bool save_onnx_checkpoint = false;

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

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

A
Allen Guo 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57
  // Number ipus total needed, replica * ipu_per_replica
  int num_ipus = 1;

  // batches per step
  int batches_per_step = 1;

  // micro batch-size
  int micro_batch_size = 1;

  // save paddle model per n steps
  int save_per_n_step = 1;

  // TODO(alleng) remove this param
A
Allen Guo 已提交
58 59 60 61 62 63 64 65 66 67 68 69
  // available memory proportion, 0.0f for disable
  float available_memory_proportion = 0.0f;

  // loss scaling, currently we can't get loss scaling from
  // optimizer_extract_pass, so we have to set it here
  float loss_scaling = 1.0f;

  // defaultMaxWeightNorm for adam optimizer
  float max_weight_norm = 65504.0f;

  // popart session option
  popart::SessionOptions popart_options;
A
Allen Guo 已提交
70 71

  // popart pattern manager
A
Allen Guo 已提交
72 73
  popart::Patterns popart_patterns;

A
Allen Guo 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
 private:
  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;

  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;

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

A
Allen Guo 已提交
116
 public:
A
Allen Guo 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  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);

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

  void EnablePattern(const std::string &t);
  void DisablePattern(const std::string &t);
  const bool IsPatternEnabled(const std::string &t);
J
jianghaicheng 已提交
135 136 137 138 139
};

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