utils.h 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2023 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

17
// #include "paddle/fluid/framework/convert_utils.h"
18
#include "paddle/fluid/framework/data_type.h"
19 20
#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_attribute.h"
#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_type_storage.h"
21
#include "paddle/ir/core/builtin_attribute.h"
22
#include "paddle/ir/core/builtin_type.h"
23
#include "paddle/phi/common/int_array.h"
24
#include "paddle/phi/common/scalar.h"
25
#include "paddle/phi/core/attribute.h"
26 27 28

namespace paddle {
namespace dialect {
29

30
using VariantType = phi::Attribute;
31

32 33
// TODO(zhangbo): The builtin type needs to cover all data types of
// phi::DataType.
34
static inline phi::DataType TransToPhiDataType(ir::Type dtype) {
K
kangguangli 已提交
35 36 37
  if (dtype.isa<ir::BFloat16Type>()) {
    return phi::DataType::BFLOAT16;
  } else if (dtype.isa<ir::Float16Type>()) {
38 39 40 41 42
    return phi::DataType::FLOAT16;
  } else if (dtype.isa<ir::Float32Type>()) {
    return phi::DataType::FLOAT32;
  } else if (dtype.isa<ir::Float64Type>()) {
    return phi::DataType::FLOAT64;
K
kangguangli 已提交
43 44 45 46
  } else if (dtype.isa<ir::UInt8Type>()) {
    return phi::DataType::UINT8;
  } else if (dtype.isa<ir::Int8Type>()) {
    return phi::DataType::INT8;
47 48 49 50 51 52
  } else if (dtype.isa<ir::Int16Type>()) {
    return phi::DataType::INT16;
  } else if (dtype.isa<ir::Int32Type>()) {
    return phi::DataType::INT32;
  } else if (dtype.isa<ir::Int64Type>()) {
    return phi::DataType::INT64;
B
Bo Zhang 已提交
53 54
  } else if (dtype.isa<ir::IndexType>()) {
    return phi::DataType::INT32;
K
kangguangli 已提交
55 56 57 58 59 60
  } else if (dtype.isa<ir::BoolType>()) {
    return phi::DataType::BOOL;
  } else if (dtype.isa<ir::Complex64Type>()) {
    return phi::DataType::COMPLEX64;
  } else if (dtype.isa<ir::Complex128Type>()) {
    return phi::DataType::COMPLEX128;
61 62 63 64 65 66 67
  } else {
    PADDLE_THROW(phi::errors::Unimplemented(
        "Unsupported ir data type when casting it into "
        "phi data type."));
  }
}

B
Bo Zhang 已提交
68 69
// use phi::DataType::INT32 for IndexType from builtin type to phi::DataType,
// but only use INT32 not IndexType from phi::DataType type to builtin type.
70
static inline ir::Type TransToIrDataType(phi::DataType dtype,
71
                                         ir::IrContext* ctx = nullptr) {
72 73 74 75
  if (ctx == nullptr) {
    ctx = ir::IrContext::Instance();
  }
  switch (dtype) {
K
kangguangli 已提交
76 77
    case phi::DataType::BFLOAT16:
      return ir::BFloat16Type::get(ctx);
78 79 80 81 82 83
    case phi::DataType::FLOAT16:
      return ir::Float16Type::get(ctx);
    case phi::DataType::FLOAT32:
      return ir::Float32Type::get(ctx);
    case phi::DataType::FLOAT64:
      return ir::Float64Type::get(ctx);
K
kangguangli 已提交
84 85 86 87
    case phi::DataType::UINT8:
      return ir::UInt8Type::get(ctx);
    case phi::DataType::INT8:
      return ir::Int8Type::get(ctx);
88 89 90 91 92 93
    case phi::DataType::INT16:
      return ir::Int16Type::get(ctx);
    case phi::DataType::INT32:
      return ir::Int32Type::get(ctx);
    case phi::DataType::INT64:
      return ir::Int64Type::get(ctx);
K
kangguangli 已提交
94 95 96 97 98 99
    case phi::DataType::BOOL:
      return ir::BoolType::get(ctx);
    case phi::DataType::COMPLEX64:
      return ir::Complex64Type::get(ctx);
    case phi::DataType::COMPLEX128:
      return ir::Complex128Type::get(ctx);
100 101 102 103 104 105 106 107
    default:
      PADDLE_THROW(phi::errors::Unimplemented(
          "Unsupported phi data type `%s` when casting it into "
          "ir data type.",
          dtype));
  }
}

108
static inline ir::Attribute TransToIrAttribute(phi::Scalar scalar,
109
                                               ir::IrContext* ctx = nullptr) {
110 111 112 113 114 115 116 117 118
  if (ctx == nullptr) {
    ctx = ir::IrContext::Instance();
  }
  switch (scalar.dtype()) {
    case phi::DataType::FLOAT32:
      return ir::FloatAttribute::get(ctx, scalar.to<float>());
    case phi::DataType::FLOAT64:
      return ir::DoubleAttribute::get(ctx, scalar.to<double>());
    case phi::DataType::INT32:
Z
zhangbo9674 已提交
119
      return ir::Int32Attribute::get(ctx, scalar.to<int32_t>());
120
    case phi::DataType::INT64:
Z
zhangbo9674 已提交
121
      return ir::Int64Attribute::get(ctx, scalar.to<int64_t>());
122 123 124 125 126 127 128 129 130 131
    case phi::DataType::BOOL:
      return ir::BoolAttribute::get(ctx, scalar.to<bool>());
    default:
      PADDLE_THROW(phi::errors::Unimplemented(
          "Unsupported phi data type `%s` when casting it into "
          "ir attribute.",
          scalar.dtype()));
  }
}

W
wanghuancoder 已提交
132 133 134 135 136 137 138 139 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 166 167 168 169
inline DataType VarTypeToDataType(
    ::paddle::framework::proto::VarType_Type var_type) {
  switch (var_type) {
    case paddle::framework::proto::VarType_Type::VarType_Type_BOOL:
      return DataType::BOOL;
    case paddle::framework::proto::VarType_Type::VarType_Type_INT16:
      return DataType::INT16;
    case paddle::framework::proto::VarType_Type::VarType_Type_INT32:
      return DataType::INT32;
    case paddle::framework::proto::VarType_Type::VarType_Type_INT64:
      return DataType::INT64;
    case paddle::framework::proto::VarType_Type::VarType_Type_FP16:
      return DataType::FLOAT16;
    case paddle::framework::proto::VarType_Type::VarType_Type_FP32:
      return DataType::FLOAT32;
    case paddle::framework::proto::VarType_Type::VarType_Type_FP64:
      return DataType::FLOAT64;
    case paddle::framework::proto::VarType_Type::VarType_Type_SIZE_T:
      return DataType::UINT64;
    case paddle::framework::proto::VarType_Type::VarType_Type_UINT8:
      return DataType::UINT8;
    case paddle::framework::proto::VarType_Type::VarType_Type_INT8:
      return DataType::INT8;
    case paddle::framework::proto::VarType_Type::VarType_Type_BF16:
      return DataType::BFLOAT16;
    case paddle::framework::proto::VarType_Type::VarType_Type_COMPLEX64:
      return DataType::COMPLEX64;
    case paddle::framework::proto::VarType_Type::VarType_Type_COMPLEX128:
      return DataType::COMPLEX128;
    case paddle::framework::proto::VarType_Type::VarType_Type_PSTRING:
      return DataType::PSTRING;
    default:
      PADDLE_THROW(phi::errors::Unimplemented(
          "Unsupported proto::VarType_Type `%s` when casting it into DataType.",
          var_type));
  }
}

Y
YuanRisheng 已提交
170
VariantType GetAttributeData(const ir::Attribute& attr);
171

172 173
bool IsLegacyOp(const std::string& name);

174 175
}  // namespace dialect
}  // namespace paddle