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

[Prim][PIR] softmax forward sink #58591

Merged
merged 11 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@

# come into effect in generated file pd_op.h
# manual decomp interface declare are located in manual_op.h
decomp_interface_declare_gen_op_list = ["mean", "squeeze", "add_n"]
decomp_interface_declare_gen_op_list = ["mean", "squeeze", "add_n", "softmax"]

# come into effect in generated file op_decomp.cc
# manual decomp interface implementation are located in manual_op_decomp.cc
decomp_interface_implementation_gen_op_list = ["mean", "squeeze", "add_n"]
decomp_interface_implementation_gen_op_list = [
"mean",
"squeeze",
"add_n",
"softmax",
]
25 changes: 25 additions & 0 deletions paddle/fluid/primitive/composite/composite.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,31 @@ Tensor mean_decomp(const Tensor& x, const IntArray& axis, bool keepdim) {
}
}

template <typename T>
Tensor softmax_decomp(const Tensor& x, const int& axis) {
kevincheng2 marked this conversation as resolved.
Show resolved Hide resolved
auto org_dtype = x.dtype();
auto x_tmp = x;
auto axis_tmp = IntArray({axis});

bool need_cast =
org_dtype == phi::DataType::FLOAT16 || org_dtype == phi::DataType::UINT16;
if (need_cast) {
x_tmp = cast<T>(x, phi::DataType::FLOAT32);
}

auto max_tmp = max<T>(x_tmp, axis_tmp, true);
auto molecular = exp<T>(subtract<T>(x_tmp, max_tmp));

auto denominator = sum<T>(molecular, axis_tmp, molecular.dtype(), true);
auto res = divide<T>(molecular, denominator);

if (need_cast) {
return cast<T>(res, org_dtype);
} else {
return res;
}
}

template <typename T>
std::tuple<Tensor, Tensor> squeeze_decomp(const Tensor& x,
const IntArray& axis) {
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/decomposition/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def silu(x):
return res if not is_amp else cast(res, dtype)


@register_decomp('pd_op.softmax')
# @register_decomp('pd_op.softmax')
def softmax(x, axis):
Copy link
Contributor

Choose a reason for hiding this comment

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

下沉无误后这里可以删掉了

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的

"""define composite rule of op softmax"""
is_amp = False
Expand Down