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

Modify Scale Compute to Support Mix Precision #58811

Merged
merged 4 commits into from
Nov 13, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Tested
  • Loading branch information
zhhsplendid committed Nov 8, 2023
commit 1fb3d59540f17a8379d1414f4ebad58865514c84
42 changes: 25 additions & 17 deletions paddle/cinn/hlir/op/elementwise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,23 +157,31 @@ std::shared_ptr<OpStrategy> StrategyForScale(
CHECK(pack_args[1].is_string());
std::string tensor_name = pack_args[1].operator std::string();

if (bias_after_scale) {
out = Compute(
A->shape,
[=](const std::vector<Expr> &indice) {
return ir::Cast::Make(A->type(),
Expr(scale) * A(indice) + Expr(bias));
},
tensor_name);
} else {
out = Compute(
A->shape,
[=](const std::vector<Expr> &indice) {
return ir::Cast::Make(A->type(),
Expr(scale) * (A(indice) + Expr(bias)));
},
tensor_name);
}
bool should_up_scale_fp32 =
A->type() == common::F16() || A->type() == common::BF16();

out = Compute(
A->shape,
[=](const std::vector<Expr> &indice) {
Expr cast_scale = should_up_scale_fp32
? ir::Cast::Make(common::F32(), Expr(scale))
: ir::Cast::Make(A->type(), Expr(scale));
Expr cast_bias = should_up_scale_fp32
? ir::Cast::Make(common::F32(), Expr(bias))
: ir::Cast::Make(A->type(), Expr(bias));
Expr cast_A_indice =
should_up_scale_fp32
? ir::Cast::Make(common::F32(), A(indice))
: A(indice);
Expr add_result = bias_after_scale
? cast_scale * cast_A_indice + cast_bias
: cast_scale * (cast_A_indice + cast_bias);
return should_up_scale_fp32
? ir::Cast::Make(A->type(), add_result)
: add_result;
},
tensor_name);

auto stages = CreateStages({out});
*ret = CINNValuePack{{CINNValue(Expr(out.get())), CINNValue(stages)}};
});
Expand Down