Skip to content
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

Conversation

shentanyue
Copy link
Collaborator

No description provided.

@paddle-bot
Copy link

paddle-bot bot commented Dec 23, 2022

Thanks for your contribution!


/**
* Use small operators to calculate, and the formula is as follows:
* input = reshape(input, (batch_size, groups, -1))
Copy link
Collaborator

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)
Copy link
Collaborator

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
Copy link
Collaborator

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))
Copy link
Collaborator

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
Copy link
Collaborator

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:
Copy link
Collaborator

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
Copy link
Collaborator

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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

小算子在任何情况下都比 LayerNorm 算子性能好吗?

Copy link
Collaborator Author

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

实测性能也确实会提升一些

Copy link
Collaborator

@hong19860320 hong19860320 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@shentanyue shentanyue merged commit a983d6a into PaddlePaddle:develop Jan 3, 2023
@shentanyue shentanyue deleted the optimizer_layer_norm_and_group_norm branch January 3, 2023 03:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants