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

lower torch.scan #6312

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions codegen/xla_native_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ supported:
- rrelu_with_noise_backward
- rsub.Scalar
- rsub.Tensor
- scan
- scatter.reduce
- scatter.src
- scatter.value
Expand Down
8 changes: 8 additions & 0 deletions torch_xla/csrc/aten_xla_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2648,6 +2648,14 @@ at::Tensor scatter_reduce_helper(const at::Tensor& self, int64_t dim,
}
}

at::Tensor XLANativeFunctions::scan(const at::Tensor& self,
const Callable f,
const at::Tensor& init,
const at::Tensor& xs) {
TORCH_LAZY_FN_COUNTER_TIMED_TRACING("xla::");
return bridge::AtenFromXlaTensor(tensor_methods::scan(f, init, xs));
}

at::Tensor XLANativeFunctions::scatter(const at::Tensor& self, int64_t dim,
const at::Tensor& index,
const at::Tensor& src) {
Expand Down
32 changes: 32 additions & 0 deletions torch_xla/csrc/ops/scan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "torch_xla/csrc/ops/scan.h"

#include "torch_xla/csrc/helpers.h"
#include "torch_xla/csrc/lowering_context.h"
#include "torch_xla/csrc/xla_lower_util.h"

namespace torch_xla {
namespace {

xla::Shape NodeOutputShape(const torch::lazy::Value& input) {
xla::Shape input_shape = GetXlaShape(input);
return input_shape;
}

} // namespace

Scan::Scan(const Callable f, const at::Tensor& init, const at::Tensor& xs)
: XlaNode(torch::lazy::OpKind(at::aten::scan), {f, init, xs},
[&]() { return NodeOutputShape(init); }, 2,) {}

torch::lazy::NodePtr Scan::Clone(torch::lazy::OpList operands) const {
return torch::lazy::MakeNode<Scan>(operands.at(0), operands.at(1), operands.at(2));
}

XlaOpVector Scan::Lower(LoweringContext* loctx) const {
xla::XlaOp f = loctx->GetOutputOp(operand(0));
xla::XlaOp init = loctx->GetOutputOp(operand(1));
xla::XlaOp xs = loctx->GetOutputOp(operand(2));
return ReturnOps(BuildScan(f, init, xs), loctx);
}

} // namespace torch_xla
22 changes: 22 additions & 0 deletions torch_xla/csrc/ops/scan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef XLA_TORCH_XLA_CSRC_OPS_NATIVE_DROPOUT_H_
#define XLA_TORCH_XLA_CSRC_OPS_NATIVE_DROPOUT_H_

#include "torch_xla/csrc/ir.h"

namespace torch_xla {

// This node has no metadata, so it could have been implemented as generic-op in
// ops.cpp, but since this might require special handling from upper IR layers,
// it gets its own IR node class.
class Scan : public XlaNode {
public:
Map(const Callable f, const at::Tensor& init, const at::Tensor& xs);

torch::lazy::NodePtr Clone(torch::lazy::OpList operands) const override;

XlaOpVector Lower(LoweringContext* loctx) const override;
};

} // namespace torch_xla

#endif // XLA_TORCH_XLA_CSRC_OPS_NATIVE_DROPOUT_H_
7 changes: 7 additions & 0 deletions torch_xla/csrc/tensor_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
#include "torch_xla/csrc/ops/rrelu_with_noise.h"
#include "torch_xla/csrc/ops/rrelu_with_noise_backward.h"
#include "torch_xla/csrc/ops/scalar.h"
#include "torch_xla/csrc/ops/scan.h"
#include "torch_xla/csrc/ops/scatter.h"
#include "torch_xla/csrc/ops/scatter_add.h"
#include "torch_xla/csrc/ops/scatter_reduce.h"
Expand Down Expand Up @@ -2392,6 +2393,12 @@ void copy_(XLATensorPtr& input, XLATensorPtr& src) {
}
}

XLATensorPtr scan(const Callable f, const XLATensorPtr& init,
const XLATensorPtr& xs) {
return init->CreateFrom(torch::lazy::MakeNode<Scan>(
f->GetIrValue(), init->GetIrValue(), xs->GetIrValue()));
}

XLATensorPtr scatter(const XLATensorPtr& input, int64_t dim,
const XLATensorPtr& index, const XLATensorPtr& src) {
return input->CreateFrom(torch::lazy::MakeNode<Scatter>(
Expand Down
3 changes: 3 additions & 0 deletions torch_xla/csrc/tensor_methods.h
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,9 @@ XLATensorPtr rsub(

void copy_(XLATensorPtr& input, XLATensorPtr& src);

XLATensorPtr scan(const Callable f, const XLATensorPtr& init,
const XLATensorPtr& xs);

XLATensorPtr scatter(const XLATensorPtr& input, int64_t dim,
const XLATensorPtr& index, const XLATensorPtr& src);
XLATensorPtr scatter(const XLATensorPtr& input, int64_t dim,
Expand Down
14 changes: 14 additions & 0 deletions torch_xla/csrc/xla_lower_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ xla::XlaOp GetPromotedR1Mask(xla::XlaOp mask, const xla::Shape& input_shape) {
return XlaHelpers::Flatten(GetPromotedMask(mask, input_shape));
}

xla::XlaOp BuildScan(const Callable f, const at::Tensor& init,
const at::Tensor& xs) {
const xla::Shape& f_shape = ShapeHelper::ShapeOfXlaOp(f);
const xla::Shape& init_shape = ShapeHelper::ShapeOfXlaOp(init);
const xla::Shape& xs_shape = ShapeHelper::ShapeOfXlaOp(xs);
int64_t i = 0;
xla::Callable = generateXLACallable(f)
xla::XlaOp result = f(init);
while i < xs.size() - 1:
i = i + 1;
result = f(result);
return result;
}

bool ShouldUseDenseScatter(const torch::lazy::BackendDevice& device,
const xla::Shape& input_shape,
const xla::Shape& index_shape) {
Expand Down
3 changes: 3 additions & 0 deletions torch_xla/csrc/xla_lower_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ std::vector<xla::XlaOp> BuildAmpUpdateScale(const xla::XlaOp& current_scale,
double scale_backoff_factor,
int scale_growth_interval);

xla::XlaOp BuildScan(const Callable f, const at::Tensor& init,
const at::Tensor& xs);

std::vector<xla::XlaOp> BuildSgdOptimizerStep(
const xla::XlaOp& found_inf, const xla::XlaOp& step,
const xla::XlaOp& param, const xla::XlaOp& buf, const xla::XlaOp& d_p,
Expand Down
Loading