-
Notifications
You must be signed in to change notification settings - Fork 1.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
[HuaweiAscendNPU] Optimize the IR mapping of LayerNorm and GroupNorm #9869
[HuaweiAscendNPU] Optimize the IR mapping of LayerNorm and GroupNorm #9869
Conversation
Thanks for your contribution! |
|
||
/** | ||
* Use small operators to calculate, and the formula is as follows: | ||
* input = reshape(input, (batch_size, groups, -1)) |
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.
input = reshape(input, shape=[batch_size, groups, -1])
* input = reshape(input, (batch_size, groups, -1)) | ||
* mean = reduce_mean(input, axis=2, keep_dims=True) | ||
* var = reduce_sum(square(input - mean), axis=2, keep_dims=True) / (channel * | ||
* height * width / grous) |
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.
groups
* mean = reduce_mean(input, axis=2, keep_dims=True) | ||
* var = reduce_sum(square(input - mean), axis=2, keep_dims=True) / (channel * | ||
* height * width / grous) | ||
* std = sqrt(var + epsilon |
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.
std = sqrt(var + epsilon)
* height * width / grous) | ||
* std = sqrt(var + epsilon | ||
* output = (input - mean) / std | ||
* output = reshape(output, (batch_size, channel, height, width)) |
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.
reshape(output, shape=[batch_size, channel, height, width])
SET_INPUT(reduce_sum_op, x, square_operator); | ||
SET_INPUT(reduce_sum_op, axes, reduce_sum_axes_operator); | ||
auto reduce_sum_operator = MAP_OUTPUT(reduce_sum_op, y, output_operand); | ||
// Varience |
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.
Variance
SET_INPUT(div_op, x1, reduce_sum_operator); | ||
SET_INPUT(div_op, x2, block_num_operator); | ||
auto varience_operator = MAP_OUTPUT(div_op, y, output_operand); | ||
// Add: |
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.
Add
auto sqrt_op = converter->AddOperator<ge::op::Sqrt>(output_operand, "sqrt"); | ||
SET_INPUT(sqrt_op, x, add_operator); | ||
auto std_operator = MAP_OUTPUT(sqrt_op, y, output_operand); | ||
// Input Normlazation |
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.
Normalization
* output = scale *((x - mean) / np.sqrt(variance + epsilon)) + bias | ||
* | ||
*/ | ||
auto batch_size = ProductionOfDimensions( |
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.
小算子在任何情况下都比 LayerNorm 算子性能好吗?
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.
理论上是的:https://gitee.com/ascend/modelzoo/issues/I5IA99?from=project-issue
实测性能也确实会提升一些
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.
LGTM
No description provided.