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 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
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\"}";
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);

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