port neg to structure kernel (#57212)
Summary:
`negative` alias is not ported.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/57212
Reviewed By: driazati
Differential Revision: D28095043
Pulled By: walterddr
fbshipit-source-id: 6c7bcd727800bb1db7add43a152de7b58f4ccf43
diff --git a/aten/src/ATen/native/UnaryOps.cpp b/aten/src/ATen/native/UnaryOps.cpp
index fc9de2b..2728fb3 100644
--- a/aten/src/ATen/native/UnaryOps.cpp
+++ b/aten/src/ATen/native/UnaryOps.cpp
@@ -76,6 +76,13 @@
CREATE_UNARY_META_FUNC(i0)
CREATE_UNARY_META_FUNC(round)
+TORCH_META_FUNC(neg)(const Tensor& self) {
+ TORCH_CHECK(self.scalar_type() != kBool,
+ "Negation, the `-` operator, on a bool tensor is not supported. "
+ "If you are trying to invert a mask, use the `~` or `logical_not()` operator instead.");
+ build_unary_op(maybe_get_output(), self);
+}
+
} // namespace meta
namespace native {
@@ -112,6 +119,7 @@
CREATE_UNARY_TORCH_IMPL_FUNC(log10)
CREATE_UNARY_TORCH_IMPL_FUNC(log1p)
CREATE_UNARY_TORCH_IMPL_FUNC(log2)
+CREATE_UNARY_TORCH_IMPL_FUNC(neg)
CREATE_UNARY_TORCH_IMPL_FUNC(reciprocal)
CREATE_UNARY_TORCH_IMPL_FUNC(round)
CREATE_UNARY_TORCH_IMPL_FUNC(rsqrt)
@@ -498,15 +506,6 @@
return self;
}
-Tensor& neg_out(const Tensor& self, Tensor& result) {
- TORCH_CHECK(self.scalar_type() != kBool,
- "Negation, the `-` operator, on a bool tensor is not supported. "
- "If you are trying to invert a mask, use the `~` or `logical_not()` operator instead.");
- return unary_op_impl_out(result, self, neg_stub);
-}
-Tensor neg(const Tensor& self) { return unary_op_impl(self, at::neg_out); }
-Tensor& neg_(Tensor& self) { return unary_op_impl_(self, at::neg_out); }
-
Tensor& negative_out(const Tensor& self, Tensor& result) { return at::neg_out(result, self); }
Tensor negative(const Tensor& self) { return self.neg(); }
Tensor& negative_(Tensor& self) { return self.neg_(); }
diff --git a/aten/src/ATen/native/native_functions.yaml b/aten/src/ATen/native/native_functions.yaml
index a18171c..0ac8dcb 100644
--- a/aten/src/ATen/native/native_functions.yaml
+++ b/aten/src/ATen/native/native_functions.yaml
@@ -2963,17 +2963,20 @@
CPU, CUDA: reciprocal_out
- func: neg(Tensor self) -> Tensor
+ structured_delegate: neg.out
variants: function, method
dispatch:
- CPU, CUDA, SparseCPU, SparseCUDA: neg
+ SparseCPU, SparseCUDA: neg_sparse
- func: neg_(Tensor(a!) self) -> Tensor(a!)
+ structured_delegate: neg.out
variants: function, method
dispatch:
- CPU, CUDA: neg_
SparseCPU, SparseCUDA: neg_sparse_
- func: neg.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)
+ structured: True
+ structured_inherits: TensorIteratorBase
dispatch:
CPU, CUDA: neg_out
SparseCPU, SparseCUDA: neg_out_sparse
diff --git a/aten/src/ATen/native/sparse/SparseTensorMath.cpp b/aten/src/ATen/native/sparse/SparseTensorMath.cpp
index 461389a..2fda367 100644
--- a/aten/src/ATen/native/sparse/SparseTensorMath.cpp
+++ b/aten/src/ATen/native/sparse/SparseTensorMath.cpp
@@ -123,6 +123,12 @@
return r;
}
+SparseTensor neg_sparse(const SparseTensor& t) {
+ SparseTensor r = get_result_tensor_for_unary_op(t);
+ neg_out_sparse(t, r);
+ return r;
+}
+
SparseTensor& neg_sparse_(SparseTensor& t) {
return neg_out_sparse(t, t);
}