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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
#include <torch/csrc/jit/codegen/cuda/expr_evaluator.h>
#include <torch/csrc/jit/codegen/cuda/fusion.h>
#include <torch/csrc/jit/codegen/cuda/instrumentation.h>
#include <torch/csrc/jit/codegen/cuda/ir_all_nodes.h>
#include <torch/csrc/jit/codegen/cuda/ir_iostream.h>
#include <iostream>
namespace torch {
namespace jit {
namespace fuser {
void StatefulExpressionEvaluator::safeBind(
Val* value,
Int::ScalarType concrete_value,
GpuLower* lower) {
auto already_concrete_val = getValue(value);
if (already_concrete_val.has_value()) {
TORCH_INTERNAL_ASSERT(
concrete_value == already_concrete_val.value(),
"Tried to bind ",
value,
" to ",
" concrete value, but it's already set to ",
already_concrete_val.value());
} else {
TORCH_INTERNAL_ASSERT(
value->getOrigin() == nullptr,
"Tried to bind to a value that is computed in the fusion IR. ",
"Can only bind to symbolic values to the fusion that do not have an origin expr.");
bindings_[value] = concrete_value;
}
if (lower != nullptr) {
// TODO(kir): we should not need to lower (or mutate the IR in any way)
// during expression evaluation
auto lowered_val = lower->getLowerValue(value);
already_concrete_val = getValue(lowered_val);
if (already_concrete_val.has_value()) {
TORCH_INTERNAL_ASSERT(
concrete_value == already_concrete_val.value(),
"Tried to bind ",
lowered_val,
" to ",
" concrete value, but it's already set to ",
already_concrete_val.value());
} else {
TORCH_INTERNAL_ASSERT(
lowered_val->getOrigin() == nullptr,
"Tried to bind to a value that is computed in the fusion IR. ",
"Can only bind to symbolic values to the fusion that do not have an origin expr.");
bindings_[lowered_val] = concrete_value;
}
}
}
c10::optional<Int::ScalarType> StatefulExpressionEvaluator::inferValue(
Val* value) {
FUSER_PERF_SCOPE("inferValue");
return maybeHandle(value);
}
void StatefulExpressionEvaluator::print() const {
std::cout << "\nEvaluation context\n";
std::cout << "--------------------\n";
for (const auto& kv : bindings_) {
std::cout << kv.first << " = " << kv.second;
if (kv.first->isConstScalar()) {
std::cout << " ; original value = "
<< kv.first->as<Int>()->value().value();
}
std::cout << " ; " << *kv.first->getValType() << "\n";
}
std::cout << "--------------------\n\n";
}
c10::optional<Int::ScalarType> StatefulExpressionEvaluator::getValue(
Val* value) {
TORCH_INTERNAL_ASSERT(
value->isAnInt(),
"Expressoin Evaluation does not support values other than integers at this time.");
switch (value->getValType().value()) {
case ValType::Scalar:
if (value->as<Int>()->value().has_value()) {
return value->as<Int>()->value();
}
break;
case ValType::KirScalar:
if (value->as<kir::Int>()->value().has_value()) {
return value->as<kir::Int>()->value();
}
break;
default:
break;
}
const auto it = bindings_.find(value);
return it != bindings_.end() ? c10::optional<Int::ScalarType>(it->second)
: c10::nullopt;
}
c10::optional<Int::ScalarType> StatefulExpressionEvaluator::maybeHandle(
Val* val) {
auto maybe_concrete_value = getValue(val);
if (!maybe_concrete_value.has_value()) {
auto origin = val->getOrigin();
if (origin != nullptr) {
handle(origin);
maybe_concrete_value = getValue(val);
}
}
return maybe_concrete_value;
}
void StatefulExpressionEvaluator::handle(UnaryOp* uop) {
const auto in = maybeHandle(uop->in());
if (in.has_value()) {
switch (uop->getUnaryOpType()) {
case UnaryOpType::Neg:
bindings_[uop->out()] = -*in;
break;
case UnaryOpType::Cast:
bindings_[uop->out()] = *in;
break;
default:
TORCH_CHECK(!"Unexpected operator type");
}
}
}
void StatefulExpressionEvaluator::handle(BinaryOp* bop) {
const auto lhs = maybeHandle(bop->lhs());
const auto rhs = maybeHandle(bop->rhs());
if (lhs.has_value() && rhs.has_value()) {
switch (bop->getBinaryOpType()) {
case BinaryOpType::Add:
bindings_[bop->out()] = *lhs + *rhs;
break;
case BinaryOpType::Sub:
bindings_[bop->out()] = *lhs - *rhs;
break;
case BinaryOpType::Mul:
bindings_[bop->out()] = *lhs * *rhs;
break;
case BinaryOpType::Div:
TORCH_CHECK(*rhs != 0);
bindings_[bop->out()] = *lhs / *rhs;
break;
case BinaryOpType::Mod:
TORCH_CHECK(*rhs != 0);
bindings_[bop->out()] = *lhs % *rhs;
break;
case BinaryOpType::CeilDiv:
TORCH_CHECK(*rhs != 0);
bindings_[bop->out()] = (*lhs + *rhs - 1) / *rhs;
break;
case BinaryOpType::And:
bindings_[bop->out()] = Int::ScalarType(*lhs && *rhs);
break;
default:
TORCH_CHECK(!"Unexpected operator type");
}
}
}
void StatefulExpressionEvaluator::handle(kir::UnaryOp* uop) {
const auto in = maybeHandle(uop->in());
if (in.has_value()) {
switch (uop->getUnaryOpType()) {
case UnaryOpType::Neg:
bindings_[uop->out()] = -*in;
break;
case UnaryOpType::Cast:
bindings_[uop->out()] = *in;
break;
default:
TORCH_CHECK(!"Unexpected operator type");
}
}
}
void StatefulExpressionEvaluator::handle(kir::BinaryOp* bop) {
const auto lhs = maybeHandle(bop->lhs());
const auto rhs = maybeHandle(bop->rhs());
if (lhs.has_value() && rhs.has_value()) {
switch (bop->getBinaryOpType()) {
case BinaryOpType::Add:
bindings_[bop->out()] = *lhs + *rhs;
break;
case BinaryOpType::Sub:
bindings_[bop->out()] = *lhs - *rhs;
break;
case BinaryOpType::Mul:
bindings_[bop->out()] = *lhs * *rhs;
break;
case BinaryOpType::Div:
TORCH_CHECK(*rhs != 0);
bindings_[bop->out()] = *lhs / *rhs;
break;
case BinaryOpType::Mod:
TORCH_CHECK(*rhs != 0);
bindings_[bop->out()] = *lhs % *rhs;
break;
case BinaryOpType::CeilDiv:
TORCH_CHECK(*rhs != 0);
bindings_[bop->out()] = (*lhs + *rhs - 1) / *rhs;
break;
case BinaryOpType::And:
bindings_[bop->out()] = Int::ScalarType(*lhs && *rhs);
break;
default:
TORCH_CHECK(!"Unexpected operator type");
}
}
}
} // namespace fuser
} // namespace jit
} // namespace torch
|