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

Draft: Lowering Aten op to composite op instead of small ops #8502

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 35 additions & 2 deletions torch_xla/csrc/ops/ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,22 @@ torch::lazy::NodePtr Gelu(const torch::lazy::Value& input) {
auto lower_fn = [](const XlaNode& node,
LoweringContext* loctx) -> XlaOpVector {
xla::XlaOp xla_input = loctx->GetOutputOp(node.operand(0));
return node.ReturnOp(BuildGelu(xla_input), loctx);

// Building composite computation.
const std::string name = "composite.gelu";
const std::string attr = "{approximate = \"none\"}";
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a dummy str for testing purpose?

Copy link
Author

Choose a reason for hiding this comment

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

This is a real op attribution for GELU: https://pytorch.org/docs/stable/generated/torch.nn.GELU.html#torch.nn.GELU

The available value of approximate is none or tanh. The lowering process checks this attribution and decides the sub lower function here. As my changes are in the sub lower function, I manually set this attribution.

It's a common process for composite op which has attributions (defined as non-tensor inputs for composite op, e.g. dim for Softmax).

Copy link
Author

Choose a reason for hiding this comment

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

Maybe I can get the attribution from XlaOp instead of manually setting strings, I will try.

xla::XlaBuilder builder(name);
xla::XlaOp arg = xla::Parameter(
&builder, 0, ShapeHelper::ShapeOfXlaOp(xla_input), "arg");
xla::XlaOp ret = BuildGelu(arg);
xla::XlaComputation computation = ConsumeValue(builder.Build(ret));

// Building call to computation.
std::vector<xla::XlaOp> inputs{xla_input};
xla::XlaOp output = xla::CompositeCall(loctx->builder(), computation, inputs, name,
attr, /*version=*/1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto for version

Copy link
Author

Choose a reason for hiding this comment

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

Yes this is for testing, I learned this setting from this XLA UT. I can remove it if it makes no scense.


return node.ReturnOp(output, loctx);
};
return GenericOp(torch::lazy::OpKind(at::aten::gelu), {input},
GetXlaShape(input), std::move(lower_fn));
Expand All @@ -704,7 +719,25 @@ torch::lazy::NodePtr GeluBackward(const torch::lazy::Value& grad_output,
LoweringContext* loctx) -> XlaOpVector {
xla::XlaOp xla_grad_output = loctx->GetOutputOp(node.operand(0));
xla::XlaOp xla_input = loctx->GetOutputOp(node.operand(1));
return node.ReturnOp(BuildGeluBackward(xla_grad_output, xla_input), loctx);

// Building composite computation.
const std::string name = "composite.gelu_backward";
const std::string attr = "{approximate = \"none\"}";
xla::XlaBuilder builder(name);
xla::XlaOp arg_grad_output =
xla::Parameter(&builder, 0, ShapeHelper::ShapeOfXlaOp(xla_grad_output),
"arg_grad_output");
xla::XlaOp arg_input = xla::Parameter(
&builder, 1, ShapeHelper::ShapeOfXlaOp(xla_input), "arg_input");
xla::XlaOp ret = BuildGeluBackward(arg_grad_output, arg_input);
xla::XlaComputation computation = ConsumeValue(builder.Build(ret));

// Building call to computation.
std::vector<xla::XlaOp> inputs{xla_grad_output, xla_input};
xla::XlaOp output = xla::CompositeCall(loctx->builder(), computation, inputs, name,
attr, /*version=*/1);

return node.ReturnOp(output, loctx);
};
return GenericOp(torch::lazy::OpKind(at::aten::gelu_backward),
{grad_output, input}, GetXlaShape(input),
Expand Down
Loading