-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Op lowering for Einsum * Delete .torch_pin Co-authored-by: JackCaoG <59073027+JackCaoG@users.noreply.github.com>
- Loading branch information
1 parent
54583a3
commit aa73509
Showing
20 changed files
with
567 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "torch_xla/csrc/ops/einsum.h" | ||
|
||
#include "torch_xla/csrc/helpers.h" | ||
#include "torch_xla/csrc/lowering_context.h" | ||
#include "torch_xla/csrc/ops/infer_output_shape.h" | ||
#include "torch_xla/csrc/reduction.h" | ||
|
||
namespace torch_xla { | ||
namespace { | ||
|
||
xla::Shape NodeOutputShape(const torch::lazy::OpList& operands, | ||
const std::string& equation) { | ||
auto lower_for_shape_fn = | ||
[&](absl::Span<const xla::XlaOp> operands) -> xla::XlaOp { | ||
return BuildEinsum(operands, equation); | ||
}; | ||
|
||
std::vector<xla::Shape> shapes; | ||
for (auto const& op : operands) { | ||
shapes.push_back(GetXlaShape(op)); | ||
} | ||
|
||
return InferOutputShape(absl::MakeSpan(shapes), lower_for_shape_fn); | ||
} | ||
|
||
} // namespace | ||
|
||
torch::lazy::NodePtr Einsum::Clone(torch::lazy::OpList operands) const { | ||
return torch::lazy::MakeNode<Einsum>(operands, equation_); | ||
} | ||
|
||
Einsum::Einsum(const torch::lazy::OpList& operands, const std::string equation) | ||
: XlaNode(torch::lazy::OpKind(at::aten::einsum), operands, | ||
NodeOutputShape(operands, equation), | ||
/*num_outputs=*/1, torch::lazy::MHash(equation)), | ||
equation_(std::move(equation)) {} | ||
|
||
XlaOpVector Einsum::Lower(LoweringContext* loctx) const { | ||
std::vector<xla::XlaOp> inputs; | ||
auto& operand_list = operands(); | ||
inputs.reserve(operand_list.size()); | ||
for (size_t i = 0; i < operand_list.size(); ++i) { | ||
inputs.push_back(loctx->GetOutputOp(operand_list[i])); | ||
} | ||
return ReturnOp(BuildEinsum(absl::MakeSpan(inputs), equation_), loctx); | ||
} | ||
|
||
std::string Einsum::ToString() const { | ||
std::stringstream ss; | ||
ss << XlaNode::ToString() << ", equation=(" << equation_ << ")"; | ||
return ss.str(); | ||
} | ||
|
||
} // namespace torch_xla |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include "torch_xla/csrc/ir.h" | ||
|
||
namespace torch_xla { | ||
|
||
class Einsum : public XlaNode { | ||
public: | ||
Einsum(const torch::lazy::OpList& operands, const std::string equation); | ||
|
||
torch::lazy::NodePtr Clone(torch::lazy::OpList operands) const override; | ||
|
||
XlaOpVector Lower(LoweringContext* loctx) const override; | ||
|
||
std::string ToString() const override; | ||
|
||
const std::string& equation() const { return equation_; } | ||
|
||
private: | ||
const std::string equation_; | ||
}; | ||
|
||
} // namespace torch_xla |
Oops, something went wrong.