-
Notifications
You must be signed in to change notification settings - Fork 491
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
Lowering Aten op to composite op instead of small ops #8502
base: master
Are you sure you want to change the base?
Changes from 1 commit
49de8bc
c9ee78e
21501b7
836aa97
cb891b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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\"}"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a dummy str for testing purpose? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a real op attribution for The available value of It's a common process for composite op which has attributions (defined as non-tensor inputs for composite op, e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What information is important for optimizations on this op? One option could be to have the composites implied value be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I believe that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto for version There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
return node.ReturnOp(output, loctx); | ||
}; | ||
return GenericOp(torch::lazy::OpKind(at::aten::gelu), {input}, | ||
GetXlaShape(input), std::move(lower_fn)); | ||
|
@@ -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), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose of namespacing is to be able to tell who the maintainer or origin of a given composite is. If something changes about GELU (new attr, etc) who is on the hook to maintain it (for composites this is usually intended to be a vendor who has a library
nvidia.some_op
,aws.some_op
,litert.some_op
etc). The namecomposite
doesn't answer this question.For this I'd recommend
ptxla.gelu
oraten.gelu
as names.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an aside, I'd be careful representing converting every (or even many) aten operations using composites. They have a maintenance overhead in terms of needing to consider forward/backward compatibilty (i.e. the above thing about "what if gelu changes, who fixes?"), for some ops like gelu/softmax its probably ok, they don't tend to change much and usually look somewhat uniform.
For other aten ops that have very specific HW support, I'd recommend an approach that decentralizes composite ownership/maintenance, i.e. FX graph rewrite-as-composite API or make composite builder work for these use cases. This is how Google AI Edge uses composites today, they own the library and the compatibility for the (very small) subset of ATen ops that they have HW support for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(cc @lsy323)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestions, here are some of my thoughts:
ptxla
sounds quite good, and I will address it.torch-xla
. The reason is that I didn't re-implement these ops from scratch, I simply wrapped the original implementation (such asBuildGelu
andLowerSoftmax
) with a composite call. W/O this PR, those implementations would need to be fixed if there were any changes in the semantics. At the current stage, I don't have any plans to introduce new composite ops that don't have an original implementation intorch-xla
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Namespace is addressed.