-
Notifications
You must be signed in to change notification settings - Fork 480
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
[PT_BREAK] Implement Tanh Gelu Approximation #3039
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
324708b
Implement Tanh Gelu Approximation
rdspring1 b4ea547
Rename x to input
rdspring1 b885b47
Replace x*x with Pow(x,2)
rdspring1 405cb06
Implement "approximate" string argument for tanh gelu
rdspring1 3ab5a37
Merge branch 'master' of github.com:rdspring1/xla into fast_gelu
rdspring1 f087317
Update string argument and add GeluType enum
rdspring1 c6c91c3
Change number of log entries checked; false positive because of rever…
rdspring1 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
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 @@ | ||
#61439 |
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,12 @@ | ||
#pragma once | ||
|
||
namespace torch_xla { | ||
|
||
// These constants control the approximation behavior of gelu function. | ||
enum GeluType { | ||
None, // Baseline Gelu | ||
Tanh, // Tahn Gelu Approximation | ||
END | ||
}; | ||
|
||
} // namespace torch_xla |
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
#include "torch_xla/csrc/convert_ops.h" | ||
#include "torch_xla/csrc/data_ops.h" | ||
#include "torch_xla/csrc/elementwise.h" | ||
#include "torch_xla/csrc/gelu.h" | ||
#include "torch_xla/csrc/helpers.h" | ||
#include "torch_xla/csrc/lowering_context.h" | ||
#include "torch_xla/csrc/matrix.h" | ||
|
@@ -650,22 +651,59 @@ NodePtr EluBackward(const Value& grad_output, const Value& output, | |
positive_output_branch, negative_output_branch); | ||
} | ||
|
||
NodePtr Gelu(const Value& input) { | ||
NodePtr Gelu(const Value& input, GeluType approximate) { | ||
ScopePusher ir_scope("aten::gelu"); | ||
// input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0))) | ||
const xla::Shape& shape = input.shape(); | ||
return input * ScalarOp(0.5, shape) * | ||
(Erf(input * ScalarOp(M_SQRT1_2, shape)) + ScalarOp(1.0, shape)); | ||
if (approximate == GeluType::Tanh) { | ||
// inner = math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(input, 3)) | ||
// input * 0.5 * (1.0 + torch.tanh(inner)) | ||
const float kBeta = M_SQRT2 * M_2_SQRTPI * 0.5; | ||
auto beta = ScalarOp(kBeta, shape); | ||
auto kappa = ScalarOp(0.044715, shape); | ||
auto three = ScalarOp(3, shape); | ||
auto one = ScalarOp(1, shape); | ||
auto half = ScalarOp(0.5, shape); | ||
NodePtr inner = beta * (input + kappa * Pow(input, three)); | ||
return half * input * (one + Tanh(inner)); | ||
} else { | ||
// input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0))) | ||
return input * ScalarOp(0.5, shape) * | ||
(Erf(input * ScalarOp(M_SQRT1_2, shape)) + ScalarOp(1.0, shape)); | ||
} | ||
} | ||
|
||
NodePtr GeluBackward(const Value& grad, const Value& input) { | ||
NodePtr GeluBackward(const Value& grad, const Value& input, | ||
GeluType approximate) { | ||
ScopePusher ir_scope("aten::gelu_backward"); | ||
const float kAlpha = M_2_SQRTPI * M_SQRT1_2 * 0.5; | ||
const xla::Shape& shape = input.shape(); | ||
NodePtr scratch = Erf(input * ScalarOp(M_SQRT1_2, shape)); | ||
NodePtr dinput = Exp(input * input * ScalarOp(-0.5, shape)); | ||
return grad * (ScalarOp(0.5, shape) * (ScalarOp(1.0, shape) + scratch) + | ||
input * dinput * ScalarOp(kAlpha, shape)); | ||
if (approximate == GeluType::Tanh) { | ||
constexpr float kBeta = M_SQRT2 * M_2_SQRTPI * 0.5; | ||
auto beta = ScalarOp(kBeta, shape); | ||
auto kappa = ScalarOp(0.044715, shape); | ||
auto one = ScalarOp(1, shape); | ||
auto two = ScalarOp(2, shape); | ||
auto three = ScalarOp(3, shape); | ||
auto half = ScalarOp(0.5, shape); | ||
NodePtr inner = beta * (input + kappa * Pow(input, three)); | ||
NodePtr tanh_inner = Tanh(inner); | ||
|
||
NodePtr left = half * input; | ||
NodePtr right = one + tanh_inner; | ||
|
||
NodePtr left_derivative = half * right; | ||
|
||
NodePtr tanh_derivative = one - tanh_inner * tanh_inner; | ||
NodePtr inner_derivative = beta * (one + three * kappa * Pow(input, two)); | ||
NodePtr right_derivative = left * tanh_derivative * inner_derivative; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, it really should be lower as a node class and lowering function if it gets this complicated. I will let this one go since upstream pr needs to merge and fix it latter. |
||
|
||
return grad * (left_derivative + right_derivative); | ||
} else { | ||
constexpr float kAlpha = M_2_SQRTPI * M_SQRT1_2 * 0.5; | ||
NodePtr scratch = Erf(input * ScalarOp(M_SQRT1_2, shape)); | ||
NodePtr dinput = Exp(input * input * ScalarOp(-0.5, shape)); | ||
return grad * (ScalarOp(0.5, shape) * (ScalarOp(1.0, shape) + scratch) + | ||
input * dinput * ScalarOp(kAlpha, shape)); | ||
} | ||
} | ||
|
||
NodePtr Lshift(const Value& input, const at::Scalar& other) { | ||
|
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
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
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.
maybe better to mark it as static?