Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IR] Add types and attributes to builtin and pd dialect #53953

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions paddle/fluid/dialect/pd_attribute.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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.

#include "paddle/fluid/dialect/pd_attribute.h"

namespace paddle {
namespace dialect {
phi::IntArray IntArrayAttribute::data() const { return storage()->GetAsKey(); }

phi::Scalar ScalarAttribute::data() const { return storage()->GetAsKey(); }

phi::DataType DataTypeAttribute::data() const { return storage()->GetAsKey(); }

phi::Place PlaceAttribute::data() const { return storage()->GetAsKey(); }

phi::DataLayout DataLayoutAttribute::data() const {
return storage()->GetAsKey();
}

} // namespace dialect
} // namespace paddle
95 changes: 95 additions & 0 deletions paddle/fluid/dialect/pd_attribute.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// 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/dialect/pd_attribute_storage.h"
#include "paddle/ir/attribute.h"

namespace paddle {
namespace dialect {
#define GET_PD_DIALECT_ATTRIBUTE_LIST \
IntArrayAttribute, ScalarAttribute, DataTypeAttribute, PlaceAttribute, \
DataLayoutAttribute

class IntArrayAttribute : public ir::Attribute {
public:
using Attribute::Attribute;

DECLARE_ATTRIBUTE_UTILITY_FUNCTOR(IntArrayAttribute,
IntArrayAttributeStorage);

bool operator<(const IntArrayAttribute &right) const {
return storage() < right.storage();
}

phi::IntArray data() const;
};

class ScalarAttribute : public ir::Attribute {
public:
using Attribute::Attribute;

DECLARE_ATTRIBUTE_UTILITY_FUNCTOR(ScalarAttribute, ScalarAttributeStorage);

bool operator<(const ScalarAttribute &right) const {
return storage() < right.storage();
}

phi::Scalar data() const;
};

class DataTypeAttribute : public ir::Attribute {
public:
using Attribute::Attribute;

DECLARE_ATTRIBUTE_UTILITY_FUNCTOR(DataTypeAttribute,
DataTypeAttributeStorage);

bool operator<(const DataTypeAttribute &right) const {
return storage() < right.storage();
}

phi::DataType data() const;
};

class PlaceAttribute : public ir::Attribute {
public:
using Attribute::Attribute;

DECLARE_ATTRIBUTE_UTILITY_FUNCTOR(PlaceAttribute, PlaceAttributeStorage);

bool operator<(const PlaceAttribute &right) const {
return storage() < right.storage();
}

phi::Place data() const;
};

class DataLayoutAttribute : public ir::Attribute {
public:
using Attribute::Attribute;

DECLARE_ATTRIBUTE_UTILITY_FUNCTOR(DataLayoutAttribute,
DataLayoutAttributeStorage);

bool operator<(const DataLayoutAttribute &right) const {
return storage() < right.storage();
}

phi::DataLayout data() const;
};

} // namespace dialect
} // namespace paddle
141 changes: 141 additions & 0 deletions paddle/fluid/dialect/pd_attribute_storage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// 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/ir/attribute.h"
#include "paddle/ir/utils.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/common/layout.h"
#include "paddle/phi/common/place.h"
#include "paddle/phi/common/scalar.h"

namespace paddle {
namespace dialect {
struct IntArrayAttributeStorage : public ir::AttributeStorage {
using ParamKey = phi::IntArray;

explicit IntArrayAttributeStorage(const ParamKey &key) { data_ = key; }

static IntArrayAttributeStorage *Construct(ParamKey key) {
return new IntArrayAttributeStorage(key);
}

static std::size_t HashValue(const ParamKey &key) {
size_t hash_value = 0;
hash_value =
ir::hash_combine(hash_value, std::hash<bool>()(key.FromTensor()));
for (auto value : key.GetData()) {
hash_value = ir::hash_combine(hash_value, std::hash<int64_t>()(value));
}
return hash_value;
}

bool operator==(const ParamKey &key) const {
return (data_.GetData() == key.GetData()) &&
(data_.FromTensor() == key.FromTensor());
}

ParamKey GetAsKey() const { return ParamKey(data_); }

private:
phi::IntArray data_;
};

struct ScalarAttributeStorage : public ir::AttributeStorage {
using ParamKey = phi::Scalar;

explicit ScalarAttributeStorage(const ParamKey &key) { data_ = key; }

static ScalarAttributeStorage *Construct(ParamKey key) {
return new ScalarAttributeStorage(key);
}

static std::size_t HashValue(const ParamKey &key) {
return ir::hash_combine(std::hash<std::string>()(key.ToString()),
std::hash<bool>()(key.FromTensor()));
}

bool operator==(const ParamKey &key) const { return data_ == key; }

ParamKey GetAsKey() const { return ParamKey(data_); }

private:
phi::Scalar data_;
};

struct DataTypeAttributeStorage : public ir::AttributeStorage {
using ParamKey = phi::DataType;

explicit DataTypeAttributeStorage(const ParamKey &key) { data_ = key; }

static DataTypeAttributeStorage *Construct(ParamKey key) {
return new DataTypeAttributeStorage(key);
}

static std::size_t HashValue(const ParamKey &key) {
return std::hash<phi::DataType>()(key);
}

bool operator==(const ParamKey &key) const { return data_ == key; }

ParamKey GetAsKey() const { return data_; }

private:
phi::DataType data_;
};

struct PlaceAttributeStorage : public ir::AttributeStorage {
using ParamKey = phi::Place;

explicit PlaceAttributeStorage(const ParamKey &key) { data_ = key; }

static PlaceAttributeStorage *Construct(ParamKey key) {
return new PlaceAttributeStorage(key);
}

static std::size_t HashValue(const ParamKey &key) { return key.HashValue(); }

bool operator==(const ParamKey &key) const { return data_ == key; }

ParamKey GetAsKey() const { return data_; }

private:
phi::Place data_;
};

struct DataLayoutAttributeStorage : public ir::AttributeStorage {
using ParamKey = phi::DataLayout;

explicit DataLayoutAttributeStorage(const ParamKey &key) { data_ = key; }

static DataLayoutAttributeStorage *Construct(ParamKey key) {
return new DataLayoutAttributeStorage(key);
}

static std::size_t HashValue(const ParamKey &key) {
return std::hash<phi::DataLayout>()(key);
}

bool operator==(const ParamKey &key) const { return data_ == key; }

ParamKey GetAsKey() const { return data_; }

private:
phi::DataLayout data_;
};

} // namespace dialect
} // namespace paddle
4 changes: 3 additions & 1 deletion paddle/fluid/dialect/pd_dialect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include "paddle/fluid/dialect/pd_dialect.h"
#include "paddle/fluid/dialect/pd_attribute.h"
#include "paddle/fluid/dialect/pd_type.h"
#include "paddle/fluid/dialect/utils.h"
#include "paddle/fluid/framework/convert_utils.h"
Expand Down Expand Up @@ -89,7 +90,8 @@ PaddleDialect::PaddleDialect(ir::IrContext* context)
}

void PaddleDialect::initialize() {
RegisterTypes<GET_PADDLE_TYPE_LIST>();
RegisterTypes<GET_PD_DIALECT_TYPE_LIST>();
RegisterAttributes<GET_PD_DIALECT_ATTRIBUTE_LIST>();
RegisterInterfaces<ParameterConvertInterface>();
}

Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/dialect/pd_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace paddle {
namespace dialect {
#define GET_PADDLE_TYPE_LIST paddle::dialect::DenseTensorType
#define GET_PD_DIALECT_TYPE_LIST paddle::dialect::DenseTensorType

///
/// \brief Define built-in parametric types.
Expand Down
14 changes: 14 additions & 0 deletions paddle/ir/builtin_attribute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,18 @@ std::string StrAttribute::data() const { return storage()->GetAsKey(); }

uint32_t StrAttribute::size() const { return storage()->GetAsKey().size(); }

bool BoolAttribute::data() const { return storage()->GetAsKey(); }

float FloatAttribute::data() const { return storage()->GetAsKey(); }

double DoubleAttribute::data() const { return storage()->GetAsKey(); }

int32_t Int32_tAttribute::data() const { return storage()->GetAsKey(); }

int64_t Int64_tAttribute::data() const { return storage()->GetAsKey(); }

std::vector<Attribute> ArrayAttribute::data() const {
return storage()->GetAsKey();
}

} // namespace ir
Loading