// 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" #include "paddle/fluid/ir/dialect/pd_attribute.h" #include "paddle/fluid/ir/dialect/pd_type_storage.h" #include "paddle/ir/core/builtin_attribute.h" #include "paddle/ir/core/builtin_type.h" #include "paddle/phi/common/int_array.h" #include "paddle/phi/common/scalar.h" namespace paddle { namespace dialect { using VariantType = paddle::variant, std::vector, std::vector, std::vector, std::vector, std::vector, phi::Scalar, std::vector, phi::IntArray, phi::DataType, phi::DataLayout, phi::Place>; // TODO(zhangbo): The builtin type needs to cover all data types of // phi::DataType. static inline phi::DataType TransToPhiDataType(ir::Type dtype) { if (dtype.isa()) { return phi::DataType::BFLOAT16; } else if (dtype.isa()) { return phi::DataType::FLOAT16; } else if (dtype.isa()) { return phi::DataType::FLOAT32; } else if (dtype.isa()) { return phi::DataType::FLOAT64; } else if (dtype.isa()) { return phi::DataType::UINT8; } else if (dtype.isa()) { return phi::DataType::INT8; } else if (dtype.isa()) { return phi::DataType::INT16; } else if (dtype.isa()) { return phi::DataType::INT32; } else if (dtype.isa()) { return phi::DataType::INT64; } else if (dtype.isa()) { return phi::DataType::BOOL; } else if (dtype.isa()) { return phi::DataType::COMPLEX64; } else if (dtype.isa()) { return phi::DataType::COMPLEX128; } else { PADDLE_THROW(phi::errors::Unimplemented( "Unsupported ir data type when casting it into " "phi data type.")); } } static inline ir::Type TransToIrDataType(phi::DataType dtype, ir::IrContext* ctx = nullptr) { if (ctx == nullptr) { ctx = ir::IrContext::Instance(); } switch (dtype) { case phi::DataType::BFLOAT16: return ir::BFloat16Type::get(ctx); 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); case phi::DataType::UINT8: return ir::UInt8Type::get(ctx); case phi::DataType::INT8: return ir::Int8Type::get(ctx); 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); 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); default: PADDLE_THROW(phi::errors::Unimplemented( "Unsupported phi data type `%s` when casting it into " "ir data type.", dtype)); } } static inline ir::Attribute TransToIrAttribute(phi::Scalar scalar, ir::IrContext* ctx = nullptr) { if (ctx == nullptr) { ctx = ir::IrContext::Instance(); } switch (scalar.dtype()) { case phi::DataType::FLOAT32: return ir::FloatAttribute::get(ctx, scalar.to()); case phi::DataType::FLOAT64: return ir::DoubleAttribute::get(ctx, scalar.to()); case phi::DataType::INT32: return ir::Int32Attribute::get(ctx, scalar.to()); case phi::DataType::INT64: return ir::Int64Attribute::get(ctx, scalar.to()); case phi::DataType::BOOL: return ir::BoolAttribute::get(ctx, scalar.to()); default: PADDLE_THROW(phi::errors::Unimplemented( "Unsupported phi data type `%s` when casting it into " "ir attribute.", scalar.dtype())); } } 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()) { return AttrType::BOOL; } else if (attr.isa()) { return AttrType::FLOAT; } else if (attr.isa()) { return AttrType::DOUBLE; } else if (attr.isa()) { return AttrType::INT32; } else if (attr.isa()) { return AttrType::INT64; } else if (attr.isa()) { return AttrType::ARRAY; } else if (attr.isa()) { return AttrType::STRING; } else if (attr.isa()) { return AttrType::INT_ARRAY; } else if (attr.isa()) { return AttrType::DATA_TYPE; } else if (attr.isa()) { return AttrType::PLACE; } else { PADDLE_THROW(phi::errors::Unimplemented( "Unsupported ir Attribute type when casting it into " "AttrType.")); } } static std::unordered_map> attr_cast_map = { {AttrType::BOOL, [](const ir::Attribute& attr) { return VariantType{attr.dyn_cast().data()}; }}, {AttrType::FLOAT, [](const ir::Attribute& attr) { return VariantType{attr.dyn_cast().data()}; }}, {AttrType::DOUBLE, [](const ir::Attribute& attr) { return VariantType{attr.dyn_cast().data()}; }}, {AttrType::INT32, [](const ir::Attribute& attr) { return VariantType{attr.dyn_cast().data()}; }}, {AttrType::INT64, [](const ir::Attribute& attr) { return VariantType{attr.dyn_cast().data()}; }}, {AttrType::INT_ARRAY, [](const ir::Attribute& attr) { return VariantType{ attr.dyn_cast() .data() .GetData()}; }}, {AttrType::STRING, [](const ir::Attribute& attr) { return VariantType{attr.dyn_cast().AsString()}; }}, {AttrType::DATA_TYPE, [](const ir::Attribute& attr) { return VariantType{ attr.dyn_cast().data()}; }}, {AttrType::PLACE, [](const ir::Attribute& attr) { return VariantType{ attr.dyn_cast().data()}; }}, {AttrType::ARRAY, [](const ir::Attribute& attr) { auto attr_vec = attr.dyn_cast().AsVector(); if (attr_vec.size() == 0) { return VariantType{std::vector()}; } AttrType element_type = GetAttributeType(attr_vec[0]); if (element_type == AttrType::BOOL) { std::vector vec_bools; for (auto vec_element : attr_vec) { vec_bools.push_back( vec_element.dyn_cast().data()); } return VariantType{vec_bools}; } else if (element_type == AttrType::INT32) { std::vector vec_int32; for (auto vec_element : attr_vec) { vec_int32.push_back( vec_element.dyn_cast().data()); } return VariantType{vec_int32}; } else if (element_type == AttrType::INT64) { std::vector vec_int64; for (auto vec_element : attr_vec) { vec_int64.push_back( vec_element.dyn_cast().data()); } return VariantType{vec_int64}; } else if (element_type == AttrType::FLOAT) { std::vector vec_float; for (auto vec_element : attr_vec) { vec_float.push_back( vec_element.dyn_cast().data()); } return VariantType{vec_float}; } else if (element_type == AttrType::DOUBLE) { std::vector vec_double; for (auto vec_element : attr_vec) { vec_double.push_back( vec_element.dyn_cast().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); } } // namespace dialect } // namespace paddle