ipu_strategy.h 3.0 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/op.hpp>
J
jianghaicheng 已提交
18
#include <popart/sessionoptions.hpp>
A
Allen Guo 已提交
19 20
#include <popart/tensorlocation.hpp>
#include "popart/patterns/patterns.hpp"
J
jianghaicheng 已提交
21 22 23 24 25 26

namespace paddle {
namespace platform {
namespace ipu {

using VirtualGraphMode = popart::VirtualGraphMode;
A
Allen Guo 已提交
27
using RecomputationType = popart::RecomputationType;
J
jianghaicheng 已提交
28 29

struct IpuStrategy {
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
  IpuStrategy() {
    // we always save optimizer state to OffChip and enable rts for saving
    // memory
    auto storage = popart::TensorLocation(popart::TensorStorage::OffChip,
                                          popart::ReplicatedTensorSharding::On);
    popart_options.optimizerStateTensorLocationSettings =
        popart::TensorLocationSettings(storage);

    // We divide the accumulationFactor and replicatedGraphCount after all
    // reduce
    popart_options.accumulationAndReplicationReductionType =
        popart::ReductionType::Mean;
    popart_options.meanAccumulationAndReplicationReductionStrategy =
        popart::MeanReductionStrategy::Post;

    popart_options.enableFloatingPointChecks = false;

    // A directory for log traces to be written into.
    popart_options.logDir = "popart_log";
  }
  ~IpuStrategy() {}

  // Number ipus total needed, replica * ipu_per_replica
J
jianghaicheng 已提交
53
  int num_ipus = 1;
A
Allen Guo 已提交
54 55

  // batches per step
J
jianghaicheng 已提交
56
  int batches_per_step = 1;
A
Allen Guo 已提交
57 58 59 60 61

  // micro batch-size
  int micro_batch_size = 1;

  // training flag, true for training
J
jianghaicheng 已提交
62
  bool is_training = true;
A
Allen Guo 已提交
63 64

  // save the onnx model lowered by paddle program description
J
jianghaicheng 已提交
65
  bool save_init_onnx = false;
A
Allen Guo 已提交
66 67 68 69 70 71 72 73

  // save the trained model
  bool save_onnx_checkpoint = false;

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

  // average sharding, debugging used
J
jianghaicheng 已提交
74
  bool need_avg_shard = false;
A
Allen Guo 已提交
75 76

  // flag for fp16, true for pure fp16
J
jianghaicheng 已提交
77
  bool enable_fp16 = false;
A
Allen Guo 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

  // 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;
  popart::Patterns popart_patterns;

 public:
  void enablePattern(const std::string& t);
  void disablePattern(const std::string& t);
  const bool isPatternEnabled(const std::string& t);
J
jianghaicheng 已提交
97 98 99 100 101
};

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