utils.h 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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

#include "paddle/fluid/framework/convert_utils.h"
#include "paddle/fluid/framework/data_type.h"
19
#include "paddle/fluid/ir/dialect/pd_attribute.h"
20
#include "paddle/fluid/ir/dialect/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 26 27

namespace paddle {
namespace dialect {
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

using VariantType = paddle::variant<bool,
                                    int,
                                    int64_t,
                                    float,
                                    double,
                                    std::string,
                                    std::vector<bool>,
                                    std::vector<int>,
                                    std::vector<int64_t>,
                                    std::vector<float>,
                                    std::vector<double>,
                                    std::vector<std::string>,
                                    phi::Scalar,
                                    std::vector<phi::Scalar>,
                                    phi::IntArray,
                                    phi::DataType,
                                    phi::DataLayout,
                                    phi::Place>;

48 49
// TODO(zhangbo): The builtin type needs to cover all data types of
// phi::DataType.
50
static inline phi::DataType TransToPhiDataType(ir::Type dtype) {
K
kangguangli 已提交
51 52 53
  if (dtype.isa<ir::BFloat16Type>()) {
    return phi::DataType::BFLOAT16;
  } else if (dtype.isa<ir::Float16Type>()) {
54 55 56 57 58
    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 已提交
59 60 61 62
  } else if (dtype.isa<ir::UInt8Type>()) {
    return phi::DataType::UINT8;
  } else if (dtype.isa<ir::Int8Type>()) {
    return phi::DataType::INT8;
63 64 65 66 67 68
  } 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;
K
kangguangli 已提交
69 70 71 72 73 74
  } 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;
75 76 77 78 79 80 81
  } else {
    PADDLE_THROW(phi::errors::Unimplemented(
        "Unsupported ir data type when casting it into "
        "phi data type."));
  }
}

82
static inline ir::Type TransToIrDataType(phi::DataType dtype,
83
                                         ir::IrContext* ctx = nullptr) {
84 85 86 87
  if (ctx == nullptr) {
    ctx = ir::IrContext::Instance();
  }
  switch (dtype) {
K
kangguangli 已提交
88 89
    case phi::DataType::BFLOAT16:
      return ir::BFloat16Type::get(ctx);
90 91 92 93 94 95
    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 已提交
96 97 98 99
    case phi::DataType::UINT8:
      return ir::UInt8Type::get(ctx);
    case phi::DataType::INT8:
      return ir::Int8Type::get(ctx);
100 101 102 103 104 105
    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 已提交
106 107 108 109 110 111
    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);
112 113 114 115 116 117 118 119
    default:
      PADDLE_THROW(phi::errors::Unimplemented(
          "Unsupported phi data type `%s` when casting it into "
          "ir data type.",
          dtype));
  }
}

120
static inline ir::Attribute TransToIrAttribute(phi::Scalar scalar,
121
                                               ir::IrContext* ctx = nullptr) {
122 123 124 125 126 127 128 129 130
  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 已提交
131
      return ir::Int32Attribute::get(ctx, scalar.to<int32_t>());
132
    case phi::DataType::INT64:
Z
zhangbo9674 已提交
133
      return ir::Int64Attribute::get(ctx, scalar.to<int64_t>());
134 135 136 137 138 139 140 141 142 143
    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()));
  }
}

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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
enum class AttrType {
  UNDEFINED = 0,
  BOOL,
  INT32,
  INT64,

  FLOAT,
  DOUBLE,

  ARRAY,
  INT_ARRAY,

  SCALAR,
  DATA_TYPE,
  DATA_LAYOUT,
  PLACE,

  STRING,

  NUM_ATTR_TYPES,
};

static inline AttrType GetAttributeType(const ir::Attribute& attr) {
  if (attr.isa<ir::BoolAttribute>()) {
    return AttrType::BOOL;
  } else if (attr.isa<ir::FloatAttribute>()) {
    return AttrType::FLOAT;
  } else if (attr.isa<ir::DoubleAttribute>()) {
    return AttrType::DOUBLE;
  } else if (attr.isa<ir::Int32Attribute>()) {
    return AttrType::INT32;
  } else if (attr.isa<ir::Int64Attribute>()) {
    return AttrType::INT64;
  } else if (attr.isa<ir::ArrayAttribute>()) {
    return AttrType::ARRAY;
  } else if (attr.isa<ir::StrAttribute>()) {
    return AttrType::STRING;
  } else if (attr.isa<paddle::dialect::IntArrayAttribute>()) {
    return AttrType::INT_ARRAY;
  } else if (attr.isa<paddle::dialect::DataTypeAttribute>()) {
    return AttrType::DATA_TYPE;
  } else if (attr.isa<paddle::dialect::PlaceAttribute>()) {
    return AttrType::PLACE;
  } else {
    PADDLE_THROW(phi::errors::Unimplemented(
        "Unsupported ir Attribute type when casting it into "
        "AttrType."));
  }
}

static std::unordered_map<AttrType,
                          std::function<VariantType(const ir::Attribute& attr)>>
    attr_cast_map = {
        {AttrType::BOOL,
         [](const ir::Attribute& attr) {
           return VariantType{attr.dyn_cast<ir::BoolAttribute>().data()};
         }},
        {AttrType::FLOAT,
         [](const ir::Attribute& attr) {
           return VariantType{attr.dyn_cast<ir::FloatAttribute>().data()};
         }},
        {AttrType::DOUBLE,
         [](const ir::Attribute& attr) {
           return VariantType{attr.dyn_cast<ir::DoubleAttribute>().data()};
         }},
        {AttrType::INT32,
         [](const ir::Attribute& attr) {
           return VariantType{attr.dyn_cast<ir::Int32Attribute>().data()};
         }},
        {AttrType::INT64,
         [](const ir::Attribute& attr) {
           return VariantType{attr.dyn_cast<ir::Int64Attribute>().data()};
         }},
        {AttrType::INT_ARRAY,
         [](const ir::Attribute& attr) {
           return VariantType{
               attr.dyn_cast<paddle::dialect::IntArrayAttribute>()
                   .data()
                   .GetData()};
         }},
        {AttrType::STRING,
         [](const ir::Attribute& attr) {
           return VariantType{attr.dyn_cast<ir::StrAttribute>().AsString()};
         }},
        {AttrType::DATA_TYPE,
         [](const ir::Attribute& attr) {
           return VariantType{
               attr.dyn_cast<paddle::dialect::DataTypeAttribute>().data()};
         }},
        {AttrType::PLACE,
         [](const ir::Attribute& attr) {
           return VariantType{
               attr.dyn_cast<paddle::dialect::PlaceAttribute>().data()};
         }},
        {AttrType::ARRAY,
         [](const ir::Attribute& attr) {
           auto attr_vec = attr.dyn_cast<ir::ArrayAttribute>().AsVector();
           if (attr_vec.size() == 0) {
             return VariantType{std::vector<int>()};
           }
           AttrType element_type = GetAttributeType(attr_vec[0]);

           if (element_type == AttrType::BOOL) {
             std::vector<bool> vec_bools;
             for (auto vec_element : attr_vec) {
               vec_bools.push_back(
                   vec_element.dyn_cast<ir::BoolAttribute>().data());
             }
             return VariantType{vec_bools};
           } else if (element_type == AttrType::INT32) {
             std::vector<int> vec_int32;
             for (auto vec_element : attr_vec) {
               vec_int32.push_back(
                   vec_element.dyn_cast<ir::Int32Attribute>().data());
             }
             return VariantType{vec_int32};
           } else if (element_type == AttrType::INT64) {
             std::vector<int64_t> vec_int64;
             for (auto vec_element : attr_vec) {
               vec_int64.push_back(
                   vec_element.dyn_cast<ir::Int64Attribute>().data());
             }
             return VariantType{vec_int64};
           } else if (element_type == AttrType::FLOAT) {
             std::vector<float> vec_float;
             for (auto vec_element : attr_vec) {
               vec_float.push_back(
                   vec_element.dyn_cast<ir::FloatAttribute>().data());
             }
             return VariantType{vec_float};
           } else if (element_type == AttrType::DOUBLE) {
             std::vector<double> vec_double;
             for (auto vec_element : attr_vec) {
               vec_double.push_back(
                   vec_element.dyn_cast<ir::DoubleAttribute>().data());
             }
             return VariantType{vec_double};
           } else {
             PADDLE_THROW(phi::errors::Unimplemented(
                 "Unsupported ir Attribute type when casting it into "
                 "vector."));
           }
         }},
};

static inline VariantType GetAttributeData(const ir::Attribute& attr) {
  AttrType attr_type = GetAttributeType(attr);
  return attr_cast_map[attr_type](attr);
}

294 295
}  // namespace dialect
}  // namespace paddle