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
|
#include <torch/csrc/jit/codegen/cuda/instrumentation.h>
#include <torch/csrc/jit/codegen/cuda/kernel_expr_evaluator.h>
#include <iostream>
namespace torch {
namespace jit {
namespace fuser {
namespace cuda {
namespace kir {
namespace {
template <typename T>
c10::optional<IntOrDouble> toOptionalIntOrDouble(c10::optional<T> i) {
if (!i) {
return c10::nullopt;
}
return IntOrDouble(i.value());
}
} // namespace
void ExpressionEvaluator::bind(const Val* value, IntOrDouble concrete_value) {
TORCH_CHECK(value->isScalar());
TORCH_CHECK(
value->dtype() == DataType::Int || value->dtype() == DataType::Double);
TORCH_CHECK(!value->isConstScalar(), "Tried to bind to a constant value");
TORCH_CHECK(
value->definition() == nullptr,
"Tried to bind to a value that is computed in the kernel IR: ",
value->toInlineString(),
" with ",
concrete_value);
known_values_[value] = concrete_value;
}
void ExpressionEvaluator::bind(
ParallelType pt,
Int::ScalarType concrete_value) {
TORCH_INTERNAL_ASSERT(isParallelTypeThread(pt));
if (precomputed_values_) {
// Need to bind the thread value to integer machine
// in pre-computed mode.
precomputed_values_->bindConcreteParallelTypeValue(pt, concrete_value);
} else {
known_parallel_dimensions_[pt] = concrete_value;
}
}
c10::optional<IntOrDouble> ExpressionEvaluator::evaluate(const Val* value) {
if (precomputed_values_ && precomputed_values_->ready()) {
if (precomputed_values_->getMaybeValueFor(value).has_value()) {
return toOptionalIntOrDouble(
precomputed_values_->getMaybeValueFor(value));
}
}
if (value->isScalar() && value->isConst()) {
if (value->isADouble()) {
return toOptionalIntOrDouble(value->as<Double>()->value());
}
return toOptionalIntOrDouble(value->as<Int>()->value());
} else {
FUSER_PERF_SCOPE("kir::ExpressionEvaluator::evaluate");
TORCH_CHECK(value->isScalar(), value->toString());
TORCH_CHECK(
value->dtype() == DataType::Int || value->dtype() == DataType::Double,
value->toString());
// Is the value known (either explicit binding or memoized)?
const auto pre_eval_it = known_values_.find(value);
if (pre_eval_it != known_values_.end()) {
return pre_eval_it->second;
}
OptOutConstDispatch::handle(value);
const auto post_eval_it = known_values_.find(value);
return post_eval_it != known_values_.end()
? c10::optional<IntOrDouble>(post_eval_it->second)
: c10::nullopt;
}
return c10::nullopt;
}
bool ExpressionEvaluator::isConst(const Val* value) {
return ExpressionEvaluator().evaluate(value).has_value();
}
void ExpressionEvaluator::print() const {
std::cout << "\nEvaluation context\n";
std::cout << "--------------------\n";
for (const auto& kv : known_values_) {
std::cout << kv.first->toString() << " = " << kv.second << "\n";
}
std::cout << "\nPre-computed Values\n";
if (precomputed_values_ != nullptr) {
precomputed_values_->print();
}
std::cout << "--------------------\n\n";
}
void ExpressionEvaluator::handle(const Int* value) {
TORCH_INTERNAL_ASSERT(!value->isConst());
if (auto def = value->definition()) {
OptOutConstDispatch::handle(def);
}
}
void ExpressionEvaluator::handle(const Double* value) {
TORCH_INTERNAL_ASSERT(!value->isConst());
if (auto def = value->definition()) {
OptOutConstDispatch::handle(def);
}
}
void ExpressionEvaluator::handle(const NamedScalar* named_scalar) {
const auto& name = named_scalar->name();
for (auto pt : kParallelTypeThreads) {
auto pt_val_it = known_parallel_dimensions_.find(pt);
if (pt_val_it == known_parallel_dimensions_.end()) {
continue;
}
if (name == stringifyThreadSize(pt)) {
known_values_[named_scalar] = pt_val_it->second;
return;
}
}
}
void ExpressionEvaluator::handle(const UnaryOp* unary_op) {
const auto in = evaluate(unary_op->in());
if (in.has_value()) {
switch (unary_op->getUnaryOpType()) {
case UnaryOpType::Neg:
known_values_[unary_op->out()] = -*in;
break;
case UnaryOpType::Set:
known_values_[unary_op->out()] = *in;
break;
case UnaryOpType::Cast:
if (unary_op->out()->getDataType() == DataType::Int) {
known_values_[unary_op->out()] = in->cast<int64_t>();
} else if (unary_op->out()->getDataType() == DataType::Double) {
known_values_[unary_op->out()] = in->cast<double>();
} else {
TORCH_INTERNAL_ASSERT(false, "dtype not supported in evaluator");
}
break;
default:
TORCH_CHECK(
false,
"Unexpected operator type ",
unary_op->getUnaryOpType(),
" in ",
unary_op->toString());
}
}
}
void ExpressionEvaluator::handle(const BinaryOp* binary_op) {
using namespace IntOrDouble_functions;
const auto lhs = evaluate(binary_op->lhs());
const auto rhs = evaluate(binary_op->rhs());
if (lhs.has_value() && rhs.has_value()) {
switch (binary_op->getBinaryOpType()) {
case BinaryOpType::Add:
known_values_[binary_op->out()] = *lhs + *rhs;
break;
case BinaryOpType::Sub:
known_values_[binary_op->out()] = *lhs - *rhs;
break;
case BinaryOpType::Mul:
known_values_[binary_op->out()] = *lhs * *rhs;
break;
case BinaryOpType::Div:
TORCH_CHECK(*rhs != 0);
known_values_[binary_op->out()] = *lhs / *rhs;
break;
case BinaryOpType::Mod:
TORCH_CHECK(*rhs != 0);
known_values_[binary_op->out()] = *lhs % *rhs;
break;
case BinaryOpType::CeilDiv:
TORCH_CHECK(*rhs != 0);
known_values_[binary_op->out()] = ceildiv(*lhs, *rhs);
break;
case BinaryOpType::And:
known_values_[binary_op->out()] = Int::ScalarType(*lhs && *rhs);
break;
default:
TORCH_CHECK(!"Unexpected operator type");
}
}
}
} // namespace kir
} // namespace cuda
} // namespace fuser
} // namespace jit
} // namespace torch
|