1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#include <c10/core/SymFloat.h>
#include <c10/core/SymInt.h>
#include <c10/core/SymIntNodeImpl.h>
#include <array>
namespace c10 {
#ifndef C10_MOBILE
static std::array<SymIntNode, 2> normalize_symints(SymInt a_, SymInt b_) {
SymIntNode a, b;
if (a_.is_symbolic())
a = a_.toSymIntNodeImpl();
if (b_.is_symbolic())
b = b_.toSymIntNodeImpl();
SymIntNodeImpl* common = a ? a.get() : b.get();
// TODO: technically we need to check that the classes match
if (!a) {
a = common->wrap(a_.as_int_unchecked());
a_.toSymInt(a); //
}
if (!b) {
b = common->wrap(b_.as_int_unchecked());
b_.toSymInt(b);
}
return {a, b};
}
SymIntNode SymInt::toSymIntNodeImpl() const {
TORCH_CHECK(is_symbolic());
return SymIntNode::reclaim_copy(toSymIntNodeImplUnowned());
}
c10::SymInt SymInt::toSymInt(SymIntNode sin_sp) {
auto ptr = static_cast<uint64_t>(
reinterpret_cast<uintptr_t>(static_cast<void*>(sin_sp.release())));
auto rep = (ptr & ~MASK) | IS_SYM;
return c10::SymInt(UNCHECKED, static_cast<int64_t>(rep));
}
#else
// this code should never be executed on mobile due to inlining of `is_symbolic`
// which always returns `false` on mobile.
// However, if we decide to strip off `SymIntNode` completely from mobile builds
// We would need to stub these methods anyways
c10::SymInt SymInt::toSymInt(SymIntNode sin_sp) {
TORCH_INTERNAL_ASSERT(false, "SymInts aren't available on mobile");
}
SymIntNode SymInt::toSymIntNodeImpl() const {
TORCH_INTERNAL_ASSERT(false, "SymInts aren't available on mobile");
}
static std::array<SymIntNode, 2> normalize_symints(SymInt a_, SymInt b_) {
TORCH_INTERNAL_ASSERT(false, "SymInts aren't available on mobile");
}
#endif
int64_t SymInt::guard_int(const char* file, int64_t line) const {
if (!is_symbolic()) {
return data_;
}
SymIntNode a = toSymIntNodeImpl();
return a->guard_int(file, line);
}
SymInt::operator SymFloat() const {
if (!is_symbolic()) {
return SymFloat(double(data_));
}
return SymFloat::toSymFloat(toSymIntNodeImpl()->sym_float());
}
SymInt SymInt::operator+(SymInt sci) const {
if (!is_symbolic() && !sci.is_symbolic()) {
return SymInt(data_ + sci.data_);
}
auto res = normalize_symints(*this, sci);
return SymInt::toSymInt(res[0]->add(res[1]));
}
SymInt SymInt::operator-(SymInt sci) const {
if (!is_symbolic() && !sci.is_symbolic()) {
return SymInt(data_ - sci.data_);
}
auto res = normalize_symints(*this, sci);
return SymInt::toSymInt(res[0]->sub(res[1]));
}
SymInt SymInt::operator*(SymInt sci) const {
if (!is_symbolic() && !sci.is_symbolic()) {
return SymInt(data_ * sci.data_);
}
auto res = normalize_symints(*this, sci);
return SymInt::toSymInt(res[0]->mul(res[1]));
}
SymInt SymInt::operator/(SymInt sci) const {
if (!is_symbolic() && !sci.is_symbolic()) {
return SymInt(data_ / sci.data_);
}
auto res = normalize_symints(*this, sci);
return SymInt::toSymInt(res[0]->floordiv(res[1]));
}
SymInt SymInt::operator%(SymInt sci) const {
if (!is_symbolic() && !sci.is_symbolic()) {
return SymInt(data_ % sci.data_);
}
auto res = normalize_symints(*this, sci);
return SymInt::toSymInt(res[0]->mod(res[1]));
}
bool SymInt::operator==(SymInt sci) const {
if (!is_symbolic() && !sci.is_symbolic()) {
return data_ == sci.data_;
}
auto res = normalize_symints(*this, sci);
return res[0]->eq(res[1])->bool_();
}
bool SymInt::operator!=(SymInt sci) const {
return !(*this == sci);
}
bool SymInt::operator<(SymInt sci) const {
if (!is_symbolic() && !sci.is_symbolic()) {
return data_ < sci.data_;
}
auto res = normalize_symints(*this, sci);
return res[0]->lt(res[1])->bool_();
}
bool SymInt::operator<=(SymInt sci) const {
if (!is_symbolic() && !sci.is_symbolic()) {
return data_ <= sci.data_;
}
auto res = normalize_symints(*this, sci);
return res[0]->le(res[1])->bool_();
}
bool SymInt::operator>(SymInt sci) const {
if (!is_symbolic() && !sci.is_symbolic()) {
return data_ > sci.data_;
}
auto res = normalize_symints(*this, sci);
return res[0]->gt(res[1])->bool_();
}
bool SymInt::operator>=(SymInt sci) const {
if (!is_symbolic() && !sci.is_symbolic()) {
return data_ >= sci.data_;
}
auto res = normalize_symints(*this, sci);
return res[0]->ge(res[1])->bool_();
}
void SymInt::operator*=(SymInt sci) {
*this = *this * sci;
}
void SymInt::operator+=(SymInt sci) {
*this = *this + sci;
}
bool SymInt::operator<(int64_t sci) const {
return *this < c10::SymInt(sci);
}
bool SymInt::operator<=(int64_t sci) const {
return *this <= c10::SymInt(sci);
}
bool SymInt::operator>(int64_t sci) const {
return *this > c10::SymInt(sci);
}
bool SymInt::operator>=(int64_t sci) const {
return *this >= c10::SymInt(sci);
}
bool SymInt::operator==(int64_t sci) const {
return *this == c10::SymInt(sci);
}
bool SymInt::operator!=(int64_t sci) const {
return *this != c10::SymInt(sci);
}
SymInt SymInt::operator*(int64_t sci) const {
return *this * c10::SymInt(sci);
}
std::ostream& operator<<(std::ostream& os, SymInt s) {
if (s.is_symbolic()) {
os << s.toSymIntNodeImpl()->str();
} else {
os << s.as_int_unchecked();
}
return os;
}
} // namespace c10
|