-
Notifications
You must be signed in to change notification settings - Fork 26.3k
[MPS] Implement hardshrink metal kernel #155304
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
Closed
manuelcandales
wants to merge
13
commits into
gh/manuelcandales/2/base
from
gh/manuelcandales/2/head
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f39e761
Update
manuelcandales f5676f4
Update
manuelcandales e393dba
Update
manuelcandales 0ac7f8e
Update
manuelcandales b474056
Update
manuelcandales f2e018b
Update
manuelcandales 2fdce93
Update
manuelcandales c635f81
Update
manuelcandales 3a26396
Update
manuelcandales d48911b
Update
manuelcandales fcef81e
Update
manuelcandales 5314497
Update
manuelcandales 525ae01
Update
manuelcandales 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #include <c10/metal/indexing.h> | ||
| #include <c10/metal/special_math.h> | ||
| #include <metal_stdlib> | ||
| using namespace metal; | ||
| using namespace c10::metal; | ||
|
|
||
| struct hardshrink_functor { | ||
| template <typename T> | ||
| inline T operator()(const T x, const T lambda) { | ||
| return (x >= -lambda && x <= lambda) ? T(0) : x; | ||
| } | ||
| }; | ||
|
|
||
| struct hardshrink_backward_functor { | ||
| template <typename T> | ||
| inline T operator()(const T grad_output, const T x, const T lambda) { | ||
| return (x >= -lambda && x <= lambda) ? T(0) : grad_output; | ||
| } | ||
| }; | ||
|
|
||
| REGISTER_UNARY_ALPHA_OP(hardshrink, float, float, float); | ||
| REGISTER_UNARY_ALPHA_OP(hardshrink, half, half, half); | ||
| #if __METAL_VERSION__ >= 310 | ||
| REGISTER_UNARY_ALPHA_OP(hardshrink, bfloat, bfloat, bfloat); | ||
| #endif | ||
|
|
||
| REGISTER_BINARY_ALPHA_OP(hardshrink_backward, float, float, float); | ||
| REGISTER_BINARY_ALPHA_OP(hardshrink_backward, half, half, half); | ||
| #if __METAL_VERSION__ >= 310 | ||
| REGISTER_BINARY_ALPHA_OP(hardshrink_backward, bfloat, bfloat, bfloat); | ||
| #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
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,27 @@ | ||
| #define TORCH_ASSERT_ONLY_METHOD_OPERATORS | ||
| #include <ATen/TensorIterator.h> | ||
| #include <ATen/mps/MPSProfiler.h> | ||
| #include <ATen/native/Activation.h> | ||
| #include <ATen/native/mps/OperationUtils.h> | ||
| #include <fmt/format.h> | ||
|
|
||
| namespace at::native { | ||
|
|
||
| #ifndef PYTORCH_JIT_COMPILE_SHADERS | ||
| static auto& lib = mps::MetalShaderLibrary::getBundledLibrary(); | ||
| #else | ||
| #include <ATen/native/mps/ActivationKernel_metallib.h> | ||
| #endif | ||
|
|
||
| static void hardshrink_kernel(TensorIteratorBase& iter, const Scalar& lambda = 0.5) { | ||
| lib.exec_unary_kernel(iter, "hardshrink", lambda); | ||
| } | ||
|
|
||
| static void hardshrink_backward_kernel(TensorIteratorBase& iter, const Scalar& lambda = 0.5) { | ||
| lib.exec_binary_kernel(iter, "hardshrink_backward", lambda); | ||
| } | ||
|
|
||
| REGISTER_DISPATCH(hardshrink_stub, hardshrink_kernel); | ||
| REGISTER_DISPATCH(shrink_backward_stub, hardshrink_backward_kernel); | ||
|
|
||
| } // namespace at::native |
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
Oops, something went wrong.
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.
This API looks wrong, it should be something like