Skip to content

Enable lazy::torch shape inference for nonzero to support dynamism #3891

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion torch_xla/csrc/aten_xla_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2186,7 +2186,10 @@ at::Tensor XLANativeFunctions::nonzero(const at::Tensor& self) {
return at::native::call_fallback_fn<&xla_cpu_fallback,
ATEN_OP(nonzero)>::call(self);
}
return bridge::AtenFromXlaTensor(XLATensor::nonzero(self_tensor));
std::vector<torch::lazy::Shape> dynamic_shapes_ =
torch::lazy::compute_shape_nonzero(self);
return bridge::AtenFromXlaTensor(
XLATensor::nonzero(self_tensor, dynamic_shapes_[0]));
}

at::Tensor XLANativeFunctions::norm(const at::Tensor& self,
Expand Down
10 changes: 6 additions & 4 deletions torch_xla/csrc/ops/nonzero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ xla::Shape NodeOutputShape(const torch::lazy::Value& input) {

} // namespace

NonZero::NonZero(const torch::lazy::Value& input)
: XlaNode(torch::lazy::OpKind(at::aten::nonzero), {input},
NonZero::NonZero(const torch::lazy::Value& input,
const torch::lazy::Shape& dynamic_shape)
: XlaNode(torch::lazy::OpKind(at::aten::nonzero), {input}, dynamic_shape,
NodeOutputShape(input),
/*num_outputs=*/2) {}
/*num_outputs=*/2),
dynamic_shape_(dynamic_shape) {}

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

XlaOpVector NonZero::Lower(LoweringContext* loctx) const {
Expand Down
6 changes: 5 additions & 1 deletion torch_xla/csrc/ops/nonzero.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ namespace torch_xla {
// it gets its own IR node class.
class NonZero : public XlaNode {
public:
NonZero(const torch::lazy::Value& input);
NonZero(const torch::lazy::Value& input,
const torch::lazy::Shape& dynamic_shape);

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

XlaOpVector Lower(LoweringContext* loctx) const override;

private:
torch::lazy::Shape dynamic_shape_;
};

} // namespace torch_xla
3 changes: 2 additions & 1 deletion torch_xla/csrc/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,8 @@ class XLATensor : public c10::intrusive_ptr_target {
const XLATensorPtr& score_threshold, const XLATensorPtr& iou_threshold,
int64_t output_size);

static XLATensorPtr nonzero(const XLATensorPtr& input);
static XLATensorPtr nonzero(const XLATensorPtr& input,
const torch::lazy::Shape& dynamic_shape);

static XLATensorPtr norm(const XLATensorPtr& input,
const c10::optional<at::Scalar>& p,
Expand Down
5 changes: 3 additions & 2 deletions torch_xla/csrc/tensor_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2003,9 +2003,10 @@ std::pair<XLATensorPtr, XLATensorPtr> XLATensor::nms(
at::ScalarType::Int));
}

XLATensorPtr XLATensor::nonzero(const XLATensorPtr& input) {
XLATensorPtr XLATensor::nonzero(const XLATensorPtr& input,
const torch::lazy::Shape& dynamic_shape) {
torch::lazy::NodePtr node =
torch::lazy::MakeNode<NonZero>(input->GetIrValue());
torch::lazy::MakeNode<NonZero>(input->GetIrValue(), dynamic_shape);
return input->CreateFrom(torch::lazy::Value(node, 0), at::ScalarType::Long);
}

Expand Down