-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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 stage2: add class Type, type uniquer utils, class IRContext #50412
Merged
zhangbo9674
merged 27 commits into
PaddlePaddle:develop
from
zhangbo9674:dev/Type_system2
Feb 27, 2023
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
0780205
add TypeUniquer and IrContext
zhangbo9674 819ed91
refine include code
zhangbo9674 e233603
add Type, TypeBase
zhangbo9674 40e8a6d
add built-in type
zhangbo9674 4cdcc24
add bulit-in Float32Type
zhangbo9674 f5cb63d
refine ut
zhangbo9674 4fe071a
refine code
zhangbo9674 23d22af
refine code
zhangbo9674 cc75738
delete type_base
zhangbo9674 7709d43
rename ImplType to StorageType
zhangbo9674 afd9c26
rename ImplType to StorageType
zhangbo9674 5697c4d
add macros util for register type
zhangbo9674 9df0868
add macros util for register type
zhangbo9674 571cec1
refine name
zhangbo9674 41268aa
refine name
zhangbo9674 d3acb79
change storage manager
zhangbo9674 4900064
add multi_thread for ir_ctx
zhangbo9674 39d69cf
rwlock_2_spinlock, add REGISTER_TYPE_2_IRCONTEXT
zhangbo9674 4736b46
DECLARE_TYPE_UTILITY_FUNCTOR
zhangbo9674 183e3a1
refine ircontext singleton
zhangbo9674 96cd329
del destructor for ParametricStorageManager
zhangbo9674 73efa45
refine code
zhangbo9674 89c953f
Add necessary logs for debugging
zhangbo9674 c4b5e71
Merge branch 'develop' of /~https://github.com/PaddlePaddle/Paddle into…
zhangbo9674 5451cba
refine ir_context instance
zhangbo9674 c7cf04f
refine type get interface
zhangbo9674 85f3cd4
refine code by comment
zhangbo9674 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// 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/type.h" | ||
|
||
namespace ir { | ||
/// | ||
/// \brief Definitions of built-in type classes. The built-in type object get | ||
/// method is as follows: Type fp32 = Float32Type::get(ctx); | ||
/// | ||
class Float32Type : public ir::Type { | ||
public: | ||
using Type::Type; | ||
|
||
DECLARE_TYPE_UTILITY_FUNCTOR(Float32Type, ir::TypeStorage); | ||
|
||
static Float32Type get(ir::IrContext *context); | ||
}; | ||
|
||
class Int32Type : public ir::Type { | ||
public: | ||
using Type::Type; | ||
|
||
DECLARE_TYPE_UTILITY_FUNCTOR(Int32Type, ir::TypeStorage); | ||
|
||
static Int32Type get(ir::IrContext *context); | ||
}; | ||
|
||
} // namespace ir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// 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 <unordered_map> | ||
|
||
#include "paddle/ir/builtin_type.h" | ||
#include "paddle/ir/ir_context.h" | ||
#include "paddle/ir/spin_lock.h" | ||
#include "paddle/ir/type_base.h" | ||
|
||
namespace ir { | ||
// The implementation class of the IrContext class | ||
class IrContextImpl { | ||
public: | ||
IrContextImpl() {} | ||
|
||
~IrContextImpl() { | ||
std::lock_guard<ir::SpinLock> guard(registed_abstract_types_lock_); | ||
for (auto abstract_type_map : registed_abstract_types_) { | ||
delete abstract_type_map.second; | ||
} | ||
registed_abstract_types_.clear(); | ||
} | ||
|
||
void RegisterAbstractType(ir::TypeId type_id, AbstractType *abstract_type) { | ||
std::lock_guard<ir::SpinLock> guard(registed_abstract_types_lock_); | ||
VLOG(4) << "IrContext register an abstract_type of: [TypeId_hash=" | ||
<< std::hash<ir::TypeId>()(type_id) | ||
<< ", AbstractType_ptr=" << abstract_type << "]."; | ||
registed_abstract_types_.emplace(type_id, abstract_type); | ||
} | ||
|
||
AbstractType *lookup(ir::TypeId type_id) { | ||
std::lock_guard<ir::SpinLock> guard(registed_abstract_types_lock_); | ||
auto iter = registed_abstract_types_.find(type_id); | ||
if (iter == registed_abstract_types_.end()) { | ||
VLOG(4) << "IrContext not fonund cached abstract_type of: [TypeId_hash=" | ||
<< std::hash<ir::TypeId>()(type_id) << "]."; | ||
return nullptr; | ||
} else { | ||
VLOG(4) << "IrContext fonund a cached abstract_type of: [TypeId_hash=" | ||
<< std::hash<ir::TypeId>()(type_id) | ||
<< ", AbstractType_ptr=" << iter->second << "]."; | ||
return iter->second; | ||
} | ||
} | ||
|
||
ir::SpinLock registed_abstract_types_lock_; | ||
|
||
// Cached AbstractType instances. | ||
std::unordered_map<TypeId, AbstractType *> registed_abstract_types_; | ||
|
||
// TypeStorage uniquer and cache instances. | ||
StorageManager registed_storage_manager_; | ||
|
||
// Some built-in type. | ||
Float32Type fp32_type; | ||
Int32Type int32_type; | ||
}; | ||
|
||
IrContext *IrContext::Instance() { | ||
static IrContext context; | ||
return &context; | ||
} | ||
|
||
IrContext::IrContext() : impl_(new IrContextImpl()) { | ||
VLOG(4) << "IrContext register built-in type..."; | ||
REGISTER_TYPE_2_IRCONTEXT(Float32Type, this); | ||
impl_->fp32_type = TypeManager::get<Float32Type>(this); | ||
VLOG(4) << "Float32Type registration complete"; | ||
REGISTER_TYPE_2_IRCONTEXT(Int32Type, this); | ||
impl_->int32_type = TypeManager::get<Int32Type>(this); | ||
VLOG(4) << "Int32Type registration complete"; | ||
} | ||
|
||
void IrContext::RegisterAbstractType(ir::TypeId type_id, | ||
AbstractType *abstract_type) { | ||
impl().RegisterAbstractType(type_id, abstract_type); | ||
} | ||
|
||
StorageManager &IrContext::storage_manager() { | ||
return impl().registed_storage_manager_; | ||
} | ||
|
||
std::unordered_map<TypeId, AbstractType *> | ||
&IrContext::registed_abstracted_type() { | ||
return impl().registed_abstract_types_; | ||
} | ||
|
||
const AbstractType &AbstractType::lookup(TypeId type_id, IrContext *ctx) { | ||
VLOG(4) << "Lookup abstract type [TypeId_hash=" | ||
<< std::hash<ir::TypeId>()(type_id) << "] from IrContext [ptr=" << ctx | ||
<< "]."; | ||
auto &impl = ctx->impl(); | ||
AbstractType *abstract_type = impl.lookup(type_id); | ||
if (abstract_type) { | ||
return *abstract_type; | ||
} else { | ||
throw("Abstract type not found in IrContext."); | ||
} | ||
} | ||
|
||
Float32Type Float32Type::get(IrContext *ctx) { return ctx->impl().fp32_type; } | ||
|
||
Int32Type Int32Type::get(IrContext *ctx) { return ctx->impl().int32_type; } | ||
|
||
} // namespace ir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// 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 <glog/logging.h> | ||
#include <memory> | ||
#include <unordered_map> | ||
|
||
namespace ir { | ||
class IrContextImpl; | ||
class StorageManager; | ||
class AbstractType; | ||
class TypeId; | ||
|
||
/// | ||
/// \brief IrContext is a global parameterless class used to store and manage | ||
/// Type and its related data structures. | ||
/// | ||
class IrContext { | ||
public: | ||
/// | ||
/// \brief Initializes a new instance of IrContext. | ||
/// | ||
static IrContext *Instance(); | ||
|
||
/// | ||
/// \brief Get an instance of IrContextImpl, a private member of IrContext. | ||
/// For the specific definition of IrContextImpl, see ir_context.cc. | ||
/// | ||
/// \return The instance of IrContextImpl. | ||
/// | ||
IrContextImpl &impl() { return *impl_; } | ||
|
||
/// | ||
/// \brief Register an AbstractType to IrContext | ||
/// | ||
/// \param type_id The type id of the AbstractType. | ||
/// \param abstract_type AbstractType* provided by user. | ||
/// | ||
void RegisterAbstractType(ir::TypeId type_id, AbstractType *abstract_type); | ||
|
||
/// | ||
/// \brief Returns the storage uniquer used for constructing TypeStorage | ||
/// instances. | ||
/// | ||
/// \return The storage uniquer used for constructing TypeStorage | ||
/// instances. | ||
/// | ||
StorageManager &storage_manager(); | ||
|
||
/// | ||
/// \brief Returns the storage uniquer used for constructing TypeStorage | ||
/// instances. | ||
/// | ||
/// \return The storage uniquer used for constructing TypeStorage | ||
/// instances. | ||
/// | ||
std::unordered_map<TypeId, AbstractType *> ®isted_abstracted_type(); | ||
|
||
IrContext(const IrContext &) = delete; | ||
|
||
void operator=(const IrContext &) = delete; | ||
|
||
private: | ||
IrContext(); | ||
|
||
const std::unique_ptr<IrContextImpl> impl_; | ||
}; | ||
|
||
} // namespace ir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// 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 <atomic> | ||
#if defined(_M_X64) || defined(__x86_64__) || defined(_M_IX86) || \ | ||
defined(__i386__) | ||
#define __PADDLE_x86__ | ||
#include <immintrin.h> | ||
#endif | ||
#include <mutex> | ||
#include <thread> | ||
|
||
namespace ir { | ||
static inline void CpuRelax() { | ||
#if defined(__PADDLE_x86__) | ||
_mm_pause(); | ||
#endif | ||
} | ||
|
||
class SpinLock { | ||
public: | ||
SpinLock() : mlock_(false) {} | ||
|
||
void lock() { | ||
for (;;) { | ||
if (!mlock_.exchange(true, std::memory_order_acquire)) { | ||
break; | ||
} | ||
constexpr int kMaxLoop = 32; | ||
for (int loop = 1; mlock_.load(std::memory_order_relaxed);) { | ||
if (loop <= kMaxLoop) { | ||
for (int i = 1; i <= loop; ++i) { | ||
CpuRelax(); | ||
} | ||
loop *= 2; | ||
} else { | ||
std::this_thread::yield(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void unlock() { mlock_.store(false, std::memory_order_release); } | ||
|
||
private: | ||
SpinLock(const SpinLock&) = delete; | ||
SpinLock(SpinLock&&) = delete; | ||
SpinLock& operator=(const SpinLock&) = delete; | ||
SpinLock& operator=(SpinLock&&) = delete; | ||
std::atomic<bool> mlock_; | ||
}; | ||
|
||
} // namespace ir |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this line can move into macro DECLARE_TYPE_UTILITY_FUNCTOR? and same for the first line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DECLARE_TYPE_UTILITY_FUNCTOR中已经提供了默认的get方法
template <typename... Args> static concrete_type get(ir::IrContext *ctx, Args... args) { return ir::TypeManager::template get<concrete_type>(ctx, args...); }
这里是针对内置类型,重载了一个get方法,直接从IrContext的缓存中获取,而上述宏定义中默认的get方法是通过TypeManager获取的