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
|
#include <torch/csrc/jit/ir/constants.h>
#include <ATen/core/functional.h>
#include <torch/csrc/autograd/variable.h>
#include <torch/csrc/jit/ir/ir.h>
#include <torch/csrc/jit/runtime/custom_operator.h>
#include <torch/csrc/jit/runtime/operator.h>
namespace torch {
namespace jit {
namespace {
c10::AliasAnalysisKind aliasAnalysisInternalSpecialCase() {
return AliasAnalysisKind::INTERNAL_SPECIAL_CASE;
}
} // namespace
bool insertableTensor(const at::Tensor& ten) {
return !ten.requires_grad();
}
bool insertableIValue(const IValue& ivalue) {
if (ivalue.isInt() || ivalue.isNone() || ivalue.isBool() ||
ivalue.isDouble() || ivalue.isString() || ivalue.isDevice() ||
ivalue.isEnum()) {
return true;
}
if (ivalue.isTensor()) {
return insertableTensor(ivalue.toTensor());
}
if (ivalue.isList() || ivalue.isTuple()) {
c10::ArrayRef<IValue> elems;
if (ivalue.isTuple()) {
elems = ivalue.toTuple()->elements();
} else {
elems = ivalue.toListRef();
}
return std::all_of(elems.begin(), elems.end(), [](const IValue& tup_elem) {
return insertableIValue(tup_elem);
});
}
if (ivalue.isGenericDict()) {
const auto& dict = ivalue.toGenericDict();
return std::all_of(dict.begin(), dict.end(), [](const auto& entry) {
return insertableIValue(entry.key()) && insertableIValue(entry.value());
});
}
return false;
}
Value* insertConstant(
Graph& g,
const IValue& val,
c10::optional<SourceRange> loc,
c10::optional<ScopePtr> scope) {
auto value = tryInsertConstant(g, val, loc, scope);
if (value) {
return *value;
}
throw constant_not_supported_error(
"Unsupported value kind: " + val.tagKind());
}
// IValue -> Constant node
c10::optional<Value*> tryInsertConstant(
Graph& g,
const IValue& val,
c10::optional<SourceRange> loc,
c10::optional<ScopePtr> scope) {
Node* n = g.create(prim::Constant);
if (val.isTensor()) {
at::Tensor ref = val.toTensor();
if (!ref.has_storage()) {
// bail if tensor has no storage i.e. opaque tensor used in MKLdnn.
n->destroy();
return c10::nullopt;
}
if (!ref.defined()) {
n->destroy();
return g.insertNode(g.createNone())->output();
}
TORCH_INTERNAL_ASSERT(!ref.requires_grad());
n->output()->inferTypeFrom(
ref); // note: before t_ because of std::move(ref)
n->t_(attr::value, std::move(ref));
} else if (val.isInt()) {
n->i_(attr::value, val.toInt());
n->output()->setType(IntType::get());
} else if (val.isDouble()) {
n->f_(attr::value, val.toDouble());
n->output()->setType(FloatType::get());
} else if (val.isBool()) {
n->i_(attr::value, val.toBool());
n->output()->setType(BoolType::get());
} else if (val.isList()) {
bool fast_path_list =
val.isBoolList() || val.isIntList() || val.isDoubleList();
if (fast_path_list || insertableIValue(val)) {
n->ival_(attr::value, val);
n->output()->setType(val.type());
} else {
n->destroy();
return c10::nullopt;
}
} else if (val.isString()) {
n->s_(attr::value, val.toString()->string());
n->output()->setType(StringType::get());
} else if (val.isDevice()) {
std::stringstream ss;
ss << val.toDevice();
n->s_(attr::value, ss.str());
n->output()->setType(DeviceObjType::get());
} else if (val.isNone()) {
n->output()->setType(NoneType::get());
} else if (val.isTuple()) {
if (insertableIValue(val)) {
n->ival_(attr::value, val);
n->output()->setType(val.type());
} else {
n->destroy();
return c10::nullopt;
};
} else if (val.isGenericDict() && insertableIValue(val)) {
n->ival_(attr::value, val);
n->output()->setType(val.type());
} else if (val.isEnum()) {
n->ival_(attr::value, val);
n->output()->setType(val.type());
} else {
n->destroy();
return c10::nullopt;
}
if (loc)
n->setSourceRange(*loc);
if (scope)
n->setScope(*scope);
return g.insertNode(n)->output();
}
c10::optional<IValue> toIValue(const Value* v) {
if (v->node()->kind() != prim::Constant || v->type()->cast<FunctionType>()) {
return c10::nullopt;
}
const Node* node = v->node();
const TypePtr& type = v->type();
if (type->isSubtypeOf(TensorType::get())) {
return node->t(attr::value);
} else if (type->isSubtypeOf(BoolType::get())) {
return (bool)node->i(attr::value);
} else if (
type->isSubtypeOf(NumberType::get()) &&
node->kindOf(attr::value) == AttributeKind::i) {
return node->i(attr::value);
} else if (
type->isSubtypeOf(NumberType::get()) &&
node->kindOf(attr::value) == AttributeKind::f) {
return node->f(attr::value);
} else if (
type->cast<ListType>() &&
node->kindOf(attr::value) == AttributeKind::ival) {
const auto& list = node->ival(attr::value);
TORCH_INTERNAL_ASSERT(list.isList());
return list;
} else if (
type->cast<DictType>() &&
node->kindOf(attr::value) == AttributeKind::ival) {
const auto& dict = node->ival(attr::value);
TORCH_INTERNAL_ASSERT(dict.isGenericDict());
return dict;
} else if (
type->cast<TupleType>() &&
node->kindOf(attr::value) == AttributeKind::ival) {
const auto& tup = node->ival(attr::value);
TORCH_INTERNAL_ASSERT(tup.isTuple());
return tup;
} else if (type == StringType::get()) {
const auto& s = node->s(attr::value);
return s;
} else if (type == DeviceObjType::get()) {
auto d = c10::Device(node->s(attr::value));
return d;
} else if (node->mustBeNone()) {
return IValue();
} else if (type->cast<EnumType>()) {
const auto& enum_val = node->ival(attr::value);
return enum_val;
} else {
std::stringstream ss;
ss << "constant literal not supported for: " << type->str();
throw std::runtime_error(ss.str());
}
}
} // namespace jit
} // namespace torch
|