amp_auto_cast.h 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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/fluid_generated/dygraph_forward_api.h"
18
#include "paddle/fluid/eager/api/manual/fluid_manual/dygraph_forward_api.h"
19 20 21 22
#include "paddle/fluid/framework/convert_utils.h"

namespace egr {

23
static inline bool NeedCast(const paddle::Tensor& tensor,
24
                            const phi::DataType& dst_dtype) {
C
Chen Weihang 已提交
25
  auto place = tensor.place();
26 27 28 29
  auto data_type = tensor.dtype();
  if (paddle::platform::is_gpu_place(place) ||
      paddle::platform::is_cuda_pinned_place(place) ||
      paddle::platform::is_xpu_place(place) ||
A
Aganlengzi 已提交
30
      paddle::platform::is_custom_place(place)) {
31
    // CudaPinndePlace is added for varbase created by dataloader
32 33 34
    if ((data_type == phi::DataType::FLOAT32 ||
         data_type == phi::DataType::FLOAT16 ||
         data_type == phi::DataType::BFLOAT16) &&
35 36 37 38 39 40 41
        (data_type != dst_dtype)) {
      return true;
    }
  }
  return false;
}

42
inline std::vector<paddle::Tensor> AmpAutoCasts(
43
    const std::string& inputs_name,
44
    const std::vector<paddle::Tensor>& inputs,
45
    const phi::DataType& dst_dtype,
46
    std::string op_name UNUSED) {
47 48
  VLOG(6) << "AMP AmpAutoCasts:"
          << " inputs(" << inputs_name << ") dst_dtype("
49
          << phi::DataTypeToString(dst_dtype) << ").";
50
  std::vector<paddle::Tensor> inputs_casted;
51 52 53 54 55 56 57 58 59 60 61 62 63 64
  for (auto& input : inputs) {
    if (NeedCast(input, dst_dtype)) {
      paddle::framework::AttributeMap cast_attrs = {
          {"in_dtype", paddle::framework::TransToProtoVarType(input.dtype())},
          {"out_dtype", paddle::framework::TransToProtoVarType(dst_dtype)}};
      inputs_casted.emplace_back(
          std::move(cast_dygraph_function(input, cast_attrs)));
    } else {
      inputs_casted.emplace_back(input);
    }
  }
  return inputs_casted;
}

65 66 67 68
inline paddle::Tensor AmpAutoCast(const std::string& input_name,
                                  const paddle::Tensor& input,
                                  const phi::DataType& dst_dtype,
                                  std::string op_name) {
69 70
  VLOG(6) << "AMP AmpAutoCasts:"
          << " input(" << input_name << ") dst_dtype("
71
          << phi::DataTypeToString(dst_dtype) << ").";
72
  if (dst_dtype == phi::DataType::FLOAT16) {
73 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
    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)) {
    paddle::framework::AttributeMap cast_attrs = {
        {"in_dtype", paddle::framework::TransToProtoVarType(input.dtype())},
        {"out_dtype", paddle::framework::TransToProtoVarType(dst_dtype)}};
    return cast_dygraph_function(input, cast_attrs);
  }
  return input;
}

}  // namespace egr