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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
#include <torch/csrc/jit/jit_log.h>
#include <torch/csrc/jit/passes/onnx/helper.h>
#include <ATen/ScalarOps.h>
#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/Functions.h>
#else
#include <ATen/ops/unsqueeze.h>
#endif
#include <onnx/onnx_pb.h>
namespace torch {
namespace jit {
namespace onnx {
using namespace ::c10::onnx;
} // namespace onnx
ValueToParamPairMap buildValueToParamsMap(
Block* b,
const ParamMap& paramsDict) {
ValueToParamPairMap valsToParamsMap;
for (auto& input : b->inputs()) {
auto it = paramsDict.find(input->debugName());
if (it != paramsDict.end()) {
valsToParamsMap.emplace(input, *it);
}
}
return valsToParamsMap;
}
void eraseUnusedBlockInputs(Block* b) {
for (size_t i_1 = b->inputs().size(); i_1 > 0; --i_1) {
size_t i = i_1 - 1;
if (!b->inputs().at(i)->hasUses()) {
b->eraseInput(i);
}
}
}
void eraseUnusedValuesFromMap(ValueToParamPairMap& valsToParamsMap) {
auto it = valsToParamsMap.begin();
while (it != valsToParamsMap.end()) {
if (!it->first->hasUses()) {
it = valsToParamsMap.erase(it);
} else {
++it;
}
}
}
void buildParamsMapFromValueToParamsMap(
const ValueToParamPairMap& valsToParamsMap,
ParamMap& paramsDict) {
paramsDict.clear();
for (const auto& nameTensorParamPair : valsToParamsMap) {
paramsDict.insert(nameTensorParamPair.second);
}
}
c10::optional<at::ScalarType> ONNXTypeToATenType(int32_t onnx_type) {
switch (onnx_type) {
case ::ONNX_NAMESPACE::TensorProto_DataType_UNDEFINED:
return at::ScalarType::Undefined;
case ::ONNX_NAMESPACE::TensorProto_DataType_FLOAT:
return at::kFloat;
case ::ONNX_NAMESPACE::TensorProto_DataType_UINT8:
return at::kByte;
case ::ONNX_NAMESPACE::TensorProto_DataType_INT8:
return at::kChar;
case ::ONNX_NAMESPACE::TensorProto_DataType_INT16:
return at::kShort;
case ::ONNX_NAMESPACE::TensorProto_DataType_INT32:
return at::kInt;
case ::ONNX_NAMESPACE::TensorProto_DataType_INT64:
return at::kLong;
case ::ONNX_NAMESPACE::TensorProto_DataType_BOOL:
return at::kBool;
case ::ONNX_NAMESPACE::TensorProto_DataType_FLOAT16:
return at::kHalf;
case ::ONNX_NAMESPACE::TensorProto_DataType_DOUBLE:
return at::kDouble;
case ::ONNX_NAMESPACE::TensorProto_DataType_COMPLEX64:
return at::kComplexFloat;
case ::ONNX_NAMESPACE::TensorProto_DataType_COMPLEX128:
return at::kComplexDouble;
case ::ONNX_NAMESPACE::TensorProto_DataType_BFLOAT16:
return at::kBFloat16;
default:
TORCH_CHECK(
false,
"ONNX type ",
onnx_type,
" is an unexpected tensor scalar type");
}
return c10::optional<at::ScalarType>{};
}
Node* addNodeToBlock(Block* block, Symbol kind, ArrayRef<Value*> inputs) {
auto new_node = block->appendNode(block->owningGraph()->create(kind));
for (auto input : inputs) {
new_node->addInput(input);
}
return new_node;
}
Value* addInputToBlock(Block* block) {
return block->addInput();
}
namespace {
::ONNX_NAMESPACE::TensorProto_DataType ATenTypeToOnnxType_aux(
at::ScalarType at_type) {
switch (at_type) {
case at::kDouble:
return ::ONNX_NAMESPACE::TensorProto_DataType_DOUBLE;
case at::kFloat:
return ::ONNX_NAMESPACE::TensorProto_DataType_FLOAT;
case at::kHalf:
return ::ONNX_NAMESPACE::TensorProto_DataType_FLOAT16;
case at::kByte:
return ::ONNX_NAMESPACE::TensorProto_DataType_UINT8;
case at::kChar:
return ::ONNX_NAMESPACE::TensorProto_DataType_INT8;
case at::kShort:
return ::ONNX_NAMESPACE::TensorProto_DataType_INT16;
case at::kInt:
return ::ONNX_NAMESPACE::TensorProto_DataType_INT32;
case at::kLong:
return ::ONNX_NAMESPACE::TensorProto_DataType_INT64;
case at::kBool:
return ::ONNX_NAMESPACE::TensorProto_DataType_BOOL;
case at::kQInt8:
return ::ONNX_NAMESPACE::TensorProto_DataType_INT8;
case at::kQUInt8:
return ::ONNX_NAMESPACE::TensorProto_DataType_UINT8;
case at::kQInt32:
return ::ONNX_NAMESPACE::TensorProto_DataType_INT32;
default:
TORCH_CHECK(
false,
"ScalarType ",
toString(at_type),
" is an unexpected tensor scalar type");
}
}
} // namespace
int ATenTypeToOnnxType(at::ScalarType at_type) {
return static_cast<int>(ATenTypeToOnnxType_aux(at_type));
}
Node* createONNXUnsqueeze(
Graph* graph,
Node* n_to_insert_before,
Value* input,
int axis,
int opset_version) {
Node* unsqueeze_node = graph->create(onnx::Unsqueeze, 1);
unsqueeze_node->addInput(input);
unsqueeze_node->insertBefore(n_to_insert_before);
if (opset_version >= OPSET_VERSION_13) {
// ONNX spec sets `axes` as input for opset >= 13.
Node* unsqueeze_axes = graph->create(onnx::Constant, 1);
unsqueeze_axes->insertBefore(unsqueeze_node);
unsqueeze_axes->t_(
attr::value, at::unsqueeze(at::scalar_to_tensor(at::Scalar(axis)), 0));
unsqueeze_node->addInput(unsqueeze_axes->output());
} else {
// ONNX spec sets `axes` as attribute for opset < 13.
unsqueeze_node->is_(attr::axes, {0});
}
return unsqueeze_node;
}
Node* createONNXConstant(
Graph* graph,
Node* n_to_insert_before,
at::Tensor value) {
Node* constant_node = graph->create(onnx::Constant, 1);
constant_node->insertBefore(n_to_insert_before);
constant_node->t_(attr::value, value);
return constant_node;
}
bool isValidToTransformToONNXConcatNode(Node* lc_node) {
return !lc_node->inputs().empty();
}
Node* transformToONNXConcatNode(
Graph* g,
Node* lc_node,
bool need_new_input,
int opset_version) {
// ListConstruct Int[] output case, we need to transform to ONNX
// Concat to ensure the output is a single tensor(dynamic) type in
// order to be consumed as inputs
std::vector<Value*> unsqueezed;
auto new_node = need_new_input ? g->return_node() : lc_node;
for (auto* input : lc_node->inputs()) {
auto new_input =
need_new_input ? g->addInput()->copyMetadata(input) : input;
// This particular Concat operation concats along axis=0 and this requires
// inputs to the node to have the same shape along dim-0. To ensure this,
// unsqueeze nodes are added such that all shapes along dim-0 are 1.
// Certain inputs from ListConstruct Int[] could be combinations of scalars
// and 1-D tensors, For inputs that are already 1-D tensors, we skip the
// step of creating a corresponding unsqueeze node.
if (auto type = new_input->type()->cast<TensorType>()) {
if (type->dim() && type->dim() == 1U) {
unsqueezed.emplace_back(new_input);
continue;
}
}
Node* unsqueezed_node =
createONNXUnsqueeze(g, new_node, new_input, 0, opset_version);
unsqueezed_node->copyMetadata(lc_node);
unsqueezed.emplace_back(unsqueezed_node->output());
}
Node* concat_node = need_new_input
? g->insertNode(g->create(onnx::Concat, 1))
: g->create(onnx::Concat, 1)->insertBefore(lc_node);
concat_node->i_(attr::axis, 0);
for (auto v : unsqueezed) {
concat_node->addInput(v);
}
return concat_node;
}
void ONNXLintGraph(
const Block* b,
std::vector<NodeKind>& n_miss_source_range,
std::vector<NodeKind>& n_miss_scope) {
for (const auto* n : b->nodes()) {
for (const auto* sub_b : n->blocks()) {
ONNXLintGraph(sub_b, n_miss_source_range, n_miss_scope);
}
if (nullptr == n->sourceRange().source()) {
GRAPH_DEBUG("Node does not set sourceRange:", *n);
n_miss_source_range.emplace_back(n->kind());
}
if (n->scopeName() == "") {
GRAPH_DEBUG("Node does not set scope:", *n);
n_miss_scope.emplace_back(n->kind());
}
}
}
void ONNXLintGraph(const std::shared_ptr<Graph>& graph) {
// Print nodes that do not have scope/source range covered.
std::vector<NodeKind> n_miss_source_range, n_miss_scope;
ONNXLintGraph(graph->block(), n_miss_source_range, n_miss_scope);
auto count_const = [](const std::vector<NodeKind>& vec) -> size_t {
size_t count = 0;
for (auto k : vec) {
switch (k) {
case prim::Constant:
case prim::ListConstruct:
case onnx::Constant:
count++;
break;
}
}
return count;
};
auto const_count_src = count_const(n_miss_source_range);
auto const_count_scope = count_const(n_miss_scope);
GRAPH_UPDATE(
"Missing source range.\n",
"Total ",
n_miss_source_range.size(),
" nodes. Including ",
const_count_src,
" constants.");
GRAPH_UPDATE(
"Missing scope.\n",
"Total ",
n_miss_scope.size(),
" nodes. Including ",
const_count_scope,
" constants.");
}
} // namespace jit
} // namespace torch
|