-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add rmsnorm op on cambricon impl #19
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
Open
bitzyz
wants to merge
8
commits into
feat/dev-infra
Choose a base branch
from
feat/dev-rmsnorm-cambricon
base: feat/dev-infra
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0a6c187
feat: Add RMSNorm op in cambricon backend.
bitzyz 1d6fe71
refactor: make `Cast` utility to use `Device::Type` template parameter
voltjia aa3d2ca
refactor: add `Caster` mixin
voltjia 802d44e
refactor: rename `cast**` to `caster**`
voltjia 738e4c9
fix: fix the mlu naming to google c++ naming style
bitzyz b2221bd
chore: format files with `clang-format`
voltjia de90bab
refactor: update CUDA kernels to use `Caster`
voltjia c913436
fix: fix rmsnorm dispatch to use one dispatch
bitzyz 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,98 @@ | ||
| #ifndef INFINI_OPS_COMMON_CAMBRICON_CASTER_H_ | ||
| #define INFINI_OPS_COMMON_CAMBRICON_CASTER_H_ | ||
|
|
||
| #include "bang_bf16.h" | ||
| #include "bang_fp16.h" | ||
| #include "caster.h" | ||
|
|
||
| namespace infini::ops { | ||
|
|
||
| template <> | ||
| struct Caster<Device::Type::kCambricon> { | ||
| template <typename Dst, typename Src> | ||
| static Dst Cast(Src&& x) { | ||
| static_assert(!std::is_reference_v<Dst>, | ||
| "`Cast` cannot return reference types"); | ||
|
|
||
| using PureSrc = std::remove_cv_t<std::remove_reference_t<Src>>; | ||
| using PureDst = std::remove_cv_t<std::remove_reference_t<Dst>>; | ||
|
|
||
| if constexpr (std::is_same_v<PureSrc, PureDst>) { | ||
| return std::forward<Src>(x); | ||
| } else { | ||
| return HardwareCast<PureDst>(std::forward<Src>(x), PriorityHigh{}); | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| template <typename T> | ||
| using PureType = std::remove_cv_t<std::remove_reference_t<T>>; | ||
|
|
||
| template <typename T> | ||
| __host__ __device__ static constexpr float ToFloatHelper(T&& x) { | ||
| using PureSrc = PureType<T>; | ||
| if constexpr (IsBFloat16<PureSrc>) { | ||
| return __bfloat162float__(x); | ||
| } else if constexpr (IsFP16<PureSrc>) { | ||
| return __half2float(x); | ||
| } else { | ||
| return static_cast<float>(std::forward<T>(x)); | ||
| } | ||
| } | ||
|
|
||
| template <typename Dst> | ||
| __host__ __device__ static constexpr Dst FromFloatHelper(float f) { | ||
| using PureDst = PureType<Dst>; | ||
| if constexpr (IsBFloat16<PureDst>) { | ||
| return __float2bfloat16__(f); | ||
| } else if constexpr (IsFP16<PureDst>) { | ||
| return __float2half__(f); | ||
| } else { | ||
| return static_cast<Dst>(f); | ||
| } | ||
| } | ||
|
|
||
| // Priority tags for overload resolution. | ||
| struct PriorityLow {}; | ||
|
|
||
| struct PriorityHigh : PriorityLow {}; | ||
|
|
||
| // Fallback: lowest priority. This always matches if nothing else does. | ||
| template <typename Dst, typename Src> | ||
| __host__ __device__ static constexpr Dst HardwareCast(Src&& x, PriorityLow) { | ||
| return FromFloatHelper<Dst>(ToFloatHelper(std::forward<Src>(x))); | ||
| } | ||
|
|
||
| // Usage: `DEFINE_DIRECT_CAST(INTRINSIC, CONDITION)`. | ||
| #define DEFINE_DIRECT_CAST(INTRINSIC, ...) \ | ||
| template <typename Dst, typename Src> \ | ||
| __host__ __device__ static auto HardwareCast(Src x, PriorityHigh) \ | ||
| -> std::enable_if_t<(__VA_ARGS__), \ | ||
| decltype(INTRINSIC(std::declval<Src>()))> { \ | ||
| return INTRINSIC(x); \ | ||
| } | ||
|
|
||
| DEFINE_DIRECT_CAST( | ||
| __bfloat162int_rz__, | ||
| std::is_same_v<PureType<Dst>, int>&& IsBFloat16<PureType<Src>>) | ||
| DEFINE_DIRECT_CAST( | ||
| __bfloat162short_rz__, | ||
| std::is_same_v<PureType<Dst>, short>&& IsBFloat16<PureType<Src>>) | ||
| DEFINE_DIRECT_CAST( | ||
| __int2bfloat16_rn__, | ||
| IsBFloat16<PureType<Dst>>&& std::is_same_v<PureType<Src>, int>) | ||
| DEFINE_DIRECT_CAST(__int2half_rn__, | ||
| IsFP16<PureType<Dst>>&& std::is_same_v<PureType<Src>, int>) | ||
| DEFINE_DIRECT_CAST( | ||
| __float2bfloat16__, | ||
| IsBFloat16<PureType<Dst>>&& std::is_same_v<PureType<Src>, double>) | ||
| DEFINE_DIRECT_CAST( | ||
| __float2half__, | ||
| IsFP16<PureType<Dst>>&& std::is_same_v<PureType<Src>, double>) | ||
| DEFINE_DIRECT_CAST(__half, IsFP16<PureType<Dst>>&& IsBFloat16<PureType<Src>>) | ||
| #undef DEFINE_DIRECT_CAST | ||
| }; | ||
|
|
||
| } // namespace infini::ops | ||
|
|
||
| #endif |
This file contains hidden or 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
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.
这个文件现在需要嘛,感觉压根儿没用到,可以去掉之后编译试试,如果现在没用到就先不加,什么时候用到了什么时候再加。