Skip to content

Commit 3d60031

Browse files
swolchoktimocafe
authored andcommitted
Overload unary - operator on at::vec::Vectorized to call neg() (pytorch#150568)
Makes Vectorized look even more like a scalar type, getting me closer to being able to use the same generic code with scalars and Vectorized (e.g., for sigmoid, which needs `exp(-x)`). Pull Request resolved: pytorch#150568 Approved by: https://github.com/Skylion007 ghstack dependencies: pytorch#150380
1 parent c83da62 commit 3d60031

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

aten/src/ATen/cpu/vec/vec_base.h

+5
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,11 @@ struct Vectorized {
740740
}
741741
};
742742

743+
template <class T>
744+
Vectorized<T> inline operator-(const Vectorized<T>& a) {
745+
return a.neg();
746+
}
747+
743748
// There is an implicit conversion that would make this work if
744749
// these operators weren't template functions, but they are template
745750
// functions (and can't be moved to be non-member friends defined in

aten/src/ATen/test/vec_test_all_types.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ namespace {
192192
[](vec v) { return v.neg(); },
193193
createDefaultUnaryTestCase<vec>(TestSeed()),
194194
RESOLVE_OVERLOAD(filter_int_minimum));
195+
test_unary<vec>(
196+
NAME_INFO(negate), std::negate<ValueType<vec>>(),
197+
[](vec v) { return -v; },
198+
createDefaultUnaryTestCase<vec>(TestSeed()),
199+
RESOLVE_OVERLOAD(filter_int_minimum));
195200
}
196201
TYPED_TEST(SignManipulationHalfPrecision, AbsNegate) {
197202
typedef enum {

0 commit comments

Comments
 (0)