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] Type system stage4: Add some built-in types and type conversion methods #51112

Merged
merged 16 commits into from
Mar 14, 2023

Conversation

zhangbo9674
Copy link
Contributor

@zhangbo9674 zhangbo9674 commented Mar 2, 2023

PR types

New features

PR changes

APIs

Describe

Pcard-67160

Paddle 类型系统开发主要分为4个阶段:(1)Type基础数据结构开发(PR1)、(2)Type类型生成工具开发(PR2)、(3)IrContext(PR2)、Dialect数据结构开发(PR3)、(4)内置类型开发(本PR)

1、PR 主要内容

1.1 内置类型添加

新增 5 种内置类型:DenseTensorTypeFloat16TypeFloat64TypeInt16TypeInt64Type。至此,内置 Dialect 拥有的内置类型包括 7 种:详见:builtin_type.h

内置类型的创建及使用方法如下:

ir::IrContext *ctx = ir::IrContext::Instance();
// 从 context 中获取 Float32Type 对象
ir::Type fp32 = ir::Float32Type::get(ctx);
// 定义 DenseTensorType 的一些参数
ir::DenseTensorTypeStorage::Dim dims = {1, 2, 3};
ir::DenseTensorTypeStorage::DataLayout data_layout = ir::DenseTensorTypeStorage::DataLayout::NCHW;
ir::DenseTensorTypeStorage::LoD lod = {{1, 2, 3}, {4, 5, 6}};
size_t offset = 0;
// 从 context 中获取 DenseTensorType 对象
ir::Type dense_tensor = ir::DenseTensorType::get(ctx, fp32, dims, data_layout, lod, offset);
1.2 丰富Type接口

新增ir::Type::isair::Type::dyn_cast 方法:

  • isa : 判断Type对象是否为某个类型
  • dyn_cast : 类型转换,将Type对象cast为另一种类型对象

使用示例如下:

// 序接1.1示例代码
// 1) 判断 ir::Type 类型的对象 dense_tensor 是否为 ir::DenseTensorType 类型
std::cout << dense_tensor.isa<ir::DenseTensorType>() << std::endl; // 1
// 2) 将 ir::Type 转换为 ir::DenseTensorType
ir::DenseTensorType dense_tensor_cast = dense_tensor.dyn_cast<ir::DenseTensorType>();
std::cout << dense_tensor_cast.offset() << std::endl;  // 0

@paddle-bot
Copy link

paddle-bot bot commented Mar 2, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@zhangbo9674 zhangbo9674 changed the title [IR] Type system stage4: add some built-in type [IR] Type system stage4: Add some built-in types and type conversion methods Mar 6, 2023
Shixiaowei02
Shixiaowei02 previously approved these changes Mar 7, 2023
/// \brief Enable hashing std::vector<std::vector<T> instances.
///
template <typename T>
struct hash<std::vector<std::vector<T>>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个有必要吗?我理解 vector<vector>也是vector的一种实例化吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有必要,已删除多余的特化。

enum class DataLayout : unsigned int {
UNDEFINED = 0,
// TODO(chenweihang): keep ANY for compatibility, remove it later
ANY = UNDEFINED,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

既然这儿说ANY是为了兼容性,后续需要移动。可否现在就移除?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已将 IR DataLayout 中 ANY 删除。


ir::Type int64_1 = ir::Int64Type::get(ctx);
ir::Type int64_2 = ir::Int64Type::get(ctx);
EXPECT_EQ(int64_1 == int64_2, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
EXPECT_EQ(int64_1 == int64_2, 1);
EXPECT_EQ(int64_1, int64_2);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, tks!

template <class To, class From>
inline typename cast_type_deduction<To, From>::return_type dyn_cast_impl(
From &Val) { // NOLINT
if (!isa<To>(Val)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commonly, return nullptr is better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, tks!

From *Val) {
return dyn_cast_impl<To>(Val);
template <typename To, typename From>
inline typename DeriveRetty<To, From *>::return_type dyn_cast(From *Val) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DeriveRetty -> ReturnTypeDuduction

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, tks!

template <class To, class From>
struct cast_type_deduction {
template <typename To, typename From>
struct DeriveRettyWrap {
typedef To &return_type;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return_type -> type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, tks!

@PaddlePaddle PaddlePaddle locked and limited conversation to collaborators Mar 13, 2023
@PaddlePaddle PaddlePaddle unlocked this conversation Mar 13, 2023
@zhangbo9674 zhangbo9674 reopened this Mar 13, 2023
Copy link
Contributor

@zhiqiu zhiqiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zhangbo9674 zhangbo9674 merged commit 3a3ff94 into PaddlePaddle:develop Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants