9 #include <c10/macros/Macros.h> 10 #include <c10/core/ScalarType.h> 11 #include <c10/util/Half.h> 26 #define DEFINE_IMPLICIT_CTOR(type,name,member) \ 28 : tag(Tag::HAS_##member) { \ 29 v . member = convert<decltype(v.member),type>(vv); \ 34 AT_FORALL_SCALAR_TYPES(DEFINE_IMPLICIT_CTOR)
36 #undef DEFINE_IMPLICIT_CTOR 38 #define DEFINE_IMPLICIT_COMPLEX_CTOR(type, name, member) \ 39 Scalar(type vv) : tag(Tag::HAS_##member) { \ 40 v.member[0] = c10::convert<double>(vv.real()); \ 41 v.member[1] = c10::convert<double>(vv.imag()); \ 45 DEFINE_IMPLICIT_COMPLEX_CTOR(std::complex<float>,ComplexFloat,z)
46 DEFINE_IMPLICIT_COMPLEX_CTOR(std::complex<double>,ComplexDouble,z)
48 #undef DEFINE_IMPLICIT_COMPLEX_CTOR 50 #define DEFINE_ACCESSOR(type,name,member) \ 51 type to##name () const { \ 52 if (Tag::HAS_d == tag) { \ 53 return checked_convert<type, double>(v.d, #type); \ 54 } else if (Tag::HAS_z == tag) { \ 55 return checked_convert<type, std::complex<double>>({v.z[0], v.z[1]}, #type); \ 57 return checked_convert<type, int64_t>(v.i, #type); \ 62 AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_EXCEPT_COMPLEX_HALF(DEFINE_ACCESSOR)
68 #undef DEFINE_ACCESSOR 69 bool isFloatingPoint()
const {
70 return Tag::HAS_d == tag;
72 bool isIntegral()
const {
73 return Tag::HAS_i == tag;
75 bool isComplex()
const {
76 return Tag::HAS_z == tag;
82 enum class Tag { HAS_d, HAS_i, HAS_z };
96 inline T Scalar::to() {
97 throw std::runtime_error(
"to() cast to unexpected type.");
100 #define DEFINE_TO(T,name,_) \ 102 inline T Scalar::to<T>() { \ 105 AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_EXCEPT_COMPLEX_HALF(DEFINE_TO)
Scalar represents a 0-dimensional tensor which contains a single element.
To register your own kernel for an operator, do in one (!) cpp file: C10_REGISTER_KERNEL(OperatorHand...