eager_amp_auto_cast.h 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright (c) 2022 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 "paddle/fluid/eager/api/generated/eager_generated/forwards/dygraph_functions.h"

namespace egr {

21
static inline bool NeedCast(const paddle::Tensor& tensor,
22
                            const phi::DataType& dst_dtype) {
C
Chen Weihang 已提交
23
  auto place = tensor.place();
24
  auto data_type = tensor.dtype();
25 26
  // Except CPU judgment, other conditions should be consistent with
  // amp_utils.h's judgment
27 28 29
  if (paddle::platform::is_gpu_place(place) ||
      paddle::platform::is_cuda_pinned_place(place) ||
      paddle::platform::is_xpu_place(place) ||
30 31
      paddle::platform::is_custom_place(place) ||
      paddle::platform::is_cpu_place(place)) {
32
    // CudaPinndePlace is added for varbase created by dataloader
33 34
    // Cpu place is for differnt place tensor, when input1 is cpu and input2 is
    // gpu
35 36 37
    if ((data_type == phi::DataType::FLOAT32 ||
         data_type == phi::DataType::FLOAT16 ||
         data_type == phi::DataType::BFLOAT16) &&
38 39 40 41 42 43 44
        (data_type != dst_dtype)) {
      return true;
    }
  }
  return false;
}

45
inline paddle::Tensor Cast(const paddle::Tensor& input,
46
                           const phi::DataType& dst_dtype,
47
                           const bool trace_backward = true) {
Z
zhangkaihuo 已提交
48 49
  if (input.is_sparse_coo_tensor() || input.is_sparse_csr_tensor()) {
    if (trace_backward) {
50
      return sparse::cast_ad_func(input, phi::DataType::UNDEFINED, dst_dtype);
Z
zhangkaihuo 已提交
51 52
    } else {
      return paddle::experimental::sparse::cast(
53
          input, phi::DataType::UNDEFINED, dst_dtype);
Z
zhangkaihuo 已提交
54 55 56
    }
  } else {
    if (trace_backward) {
J
Jiabin Yang 已提交
57
      return cast_ad_func(input, dst_dtype);
Z
zhangkaihuo 已提交
58 59 60 61 62 63
    } else {
      return paddle::experimental::cast(input, dst_dtype);
    }
  }
}

64
inline std::vector<paddle::Tensor> EagerAmpAutoCasts(
65
    const std::string& inputs_name,
66
    const std::vector<paddle::Tensor>& inputs,
67
    const phi::DataType& dst_dtype,
G
Galaxy1458 已提交
68 69
    std::string op_name UNUSED,
    bool trace_backward UNUSED = true) {
70 71
  VLOG(6) << "AMP AmpAutoCasts:"
          << " inputs(" << inputs_name << ") dst_dtype("
72
          << phi::DataTypeToString(dst_dtype) << ").";
73
  std::vector<paddle::Tensor> inputs_casted;
74 75
  for (auto& input : inputs) {
    if (NeedCast(input, dst_dtype)) {
Z
zhangkaihuo 已提交
76
      inputs_casted.emplace_back(std::move(Cast(input, dst_dtype)));
77 78 79 80 81 82 83
    } else {
      inputs_casted.emplace_back(input);
    }
  }
  return inputs_casted;
}

84 85 86 87 88
inline paddle::Tensor EagerAmpAutoCast(const std::string& input_name,
                                       const paddle::Tensor& input,
                                       const phi::DataType& dst_dtype,
                                       const std::string& op_name,
                                       bool trace_backward = true) {
89
  VLOG(6) << "AMP AmpAutoCasts:"
90
          << " input(" << egr::EagerUtils::TensorStr(input) << " to dst_dtype("
91
          << phi::DataTypeToString(dst_dtype) << ").";
92
  if (dst_dtype == phi::DataType::FLOAT16) {
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    if (op_name == "run_program") {
      return input;
    }
    if ((op_name == "batch_norm" || op_name == "layer_norm" ||
         op_name == "sync_batch_norm") &&
        input_name != "x") {
      return input;
    }
    if ((op_name == "fused_attention" || op_name == "fused_feedforward")) {
      if (input_name == "LnScale" || input_name == "LnBias" ||
          input_name == "Ln2Scale" || input_name == "Ln2Bias" ||
          input_name == "Ln1Scale" || input_name == "Ln1Bias") {
        return input;
      }
    }
  }
  if (NeedCast(input, dst_dtype)) {
110
    VLOG(6) << "Input : " << input.impl() << "NeedCast";
Z
zhangkaihuo 已提交
111
    return Cast(input, dst_dtype, trace_backward);
112 113 114 115
  }
  return input;
}

116
inline paddle::optional<paddle::Tensor> EagerAmpAutoCast(
117
    const std::string& input_name,
118
    const paddle::optional<paddle::Tensor>& input,
119
    const phi::DataType& dst_dtype,
120 121
    const std::string& op_name,
    bool trace_backward = true) {
122
  if (input) {
123 124
    return EagerAmpAutoCast(
        input_name, *input, dst_dtype, op_name, trace_backward);
125 126 127 128
  }
  return paddle::none;
}

129
inline paddle::optional<std::vector<paddle::Tensor>> EagerAmpAutoCasts(
130
    const std::string& inputs_name,
131
    const paddle::optional<std::vector<paddle::Tensor>>& inputs,
132
    const phi::DataType& dst_dtype,
133 134 135 136 137 138
    std::string op_name,
    bool trace_backward = true) {
  if (inputs) {
    return EagerAmpAutoCasts(
        inputs_name, *inputs, dst_dtype, op_name, trace_backward);
  }
139
  return paddle::optional<std::vector<paddle::Tensor>>();
140 141
}

142
}  // namespace egr