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
|
#include <torch/csrc/jit/passes/onnx/preprocess_for_onnx.h>
#include <ATen/ScalarOps.h>
#include <c10/util/irange.h>
#include <torch/csrc/jit/jit_log.h>
#include <torch/csrc/jit/passes/onnx/helper.h>
namespace torch {
namespace jit {
namespace onnx {
using namespace ::c10::onnx;
}
namespace {
at::optional<Node*> FindFusibleListUnpack(Node* n) {
// 1. number of outputs is restricted to 1.
// 2. output is only used by prim::ListUnpack.
if (n->outputs().size() != 1) {
return at::nullopt;
}
if (n->output()->uses().size() != 1) {
return at::nullopt;
}
auto listUnpackNode = n->output()->uses()[0].user;
if (listUnpackNode->kind() != prim::ListUnpack) {
return at::nullopt;
}
return listUnpackNode;
}
// Fuse node + ListUnpack
// Node such as split/unbind produces tensor[] of static size,
// that is later unpacked by ListUnpack.
// This pass fuses the two nodes, and adds an additional input "_outputs" such
// that the symbolic function is aware of the number of outputs.
//
// Example IR
// split.Tensor(Tensor(a -> *) self, int split_size, int dim=0) -> Tensor[]
// split_with_sizes(Tensor self, int[] split_sizes, int dim=0) -> Tensor[]
//
// graph(%input : Float(5, 4, 3, strides=[12, 3, 1])):
// %13 : int[] = prim::Constant[value=[2, 1, 2]]()
// %7 : int = prim::Constant[value=0]()
// %8 : Tensor[] = aten::split_with_sizes(%input, %13, %7)
// %9 : Float(2, 4, 3, strides=[12, 3, 1]), %10 : Float(1, 4, 3, strides=[12,
// 3, 1]), %11 : Float(2, 4, 3, strides=[12, 3, 1]) = prim::ListUnpack(%8)
// return (%9, %10, %11)
//
// After fusion
// graph(%input : Float(5, 4, 3, strides=[12, 3, 1])):
// %13 : int[] = prim::Constant[value=[2, 1, 2]]()
// %7 : int = prim::Constant[value=0]()
// %8 : int = prim::Constant[value=3]() # Adding addtional input of value 3
// representing the number of outputs.
// %14 : Float(2, 4, 3, strides=[12, 3, 1]), %15 : Float(1, 4, 3, strides=[12,
// 3, 1]), %16 : Float(2, 4, 3, strides=[12, 3, 1] =
// aten::split_with_sizes(%input, %13, %7, %8) return (%14, %15, %16)
void FuseWithListUnpack(Node* n) {
auto found_listUnpack = FindFusibleListUnpack(n);
if (!found_listUnpack) {
return;
}
auto listUnpack_node = found_listUnpack.value();
TORCH_INTERNAL_ASSERT(n->outputs().size() == 1);
// 1. Add internal input "_outputs" to node, so that later symbolic function
// conversion is aware of the number of outputs.
// 2. Add the exact number of outputs to n, copy metadata and replace uses of
// listUnpack outputs.
n->i_(
Symbol::fromQualString("attr::_outputs"),
static_cast<int64_t>(listUnpack_node->outputs().size()));
for (size_t i = 0; i < listUnpack_node->outputs().size(); ++i) {
auto new_output = n->addOutput();
new_output->copyMetadata(listUnpack_node->output(i));
}
listUnpack_node->removeAllInputs();
// remove original output, which is input to listUnpack node.
n->eraseOutput(0);
listUnpack_node->replaceAllUsesWith(n);
}
static void FuseWithListUnpack(Block* b) {
for (auto it = b->nodes().begin(), end = b->nodes().end(); it != end; ++it) {
for (auto* child_block : it->blocks()) {
FuseWithListUnpack(child_block);
}
auto n_kind = it->kind();
switch (n_kind) {
case aten::split:
case aten::split_with_sizes:
case aten::unsafe_split:
case aten::unsafe_split_with_sizes:
case aten::unbind:
case aten::unsafe_chunk:
case aten::where:
case aten::nonzero_numpy:
FuseWithListUnpack(*it);
break;
default:
break;
}
}
}
// Replace aten::add with onnx::Concat
// when inputs to the add node are two int lists
//
// before the pass:
// graph(%x.1 : Float(2, 3, 4, strides=[12, 4, 1], requires_grad=0, device=cpu),
// %y.1 : Float(1, 2, 3, strides=[6, 3, 1], requires_grad=0, device=cpu)):
// %2 : None = prim::Constant()
// %3 : int[] = aten::size(%x.1)
// %l1.1 : int[] = aten::list(%3
// %5 : int[] = aten::size(%y.1)
// %l2.1 : int[] = aten::list(%5)
// %7 : int[] = aten::add(%l1.1, %l2.1)
// %8 : Tensor = aten::new_zeros(%x.1, %7, %2, %2, %2, %2)
// return (%8)
//
// after the pass:
// graph(%x.1 : Float(2, 3, 4, strides=[12, 4, 1], requires_grad=0, device=cpu),
// %y.1 : Float(1, 2, 3, strides=[6, 3, 1], requires_grad=0, device=cpu)):
// %2 : None = prim::Constant()
// %3 : int[] = aten::size(%x.1)
// %l1.1 : int[] = aten::list(%3)
// %5 : int[] = aten::size(%y.1)
// %l2.1 : int[] = aten::list(%5)
// %9 : Tensor = onnx::Concat[axis=0](%l1.1, %l2.1)
// %8 : Tensor = aten::new_zeros(%x.1, %9, %2, %2, %2, %2)
// return (%8)
static void ReplaceAddWithConcat(Block* b) {
for (auto it = b->nodes().begin(), end = b->nodes().end(); it != end; ++it) {
for (auto* child_block : it->blocks()) {
ReplaceAddWithConcat(child_block);
}
if (it->kind() == aten::add) {
if (!it->input(0)->type()->cast<ListType>() ||
!it->input(1)->type()->cast<ListType>()) {
continue;
}
const auto& elem =
it->input(0)->type()->castRaw<ListType>()->getElementType();
if (elem->cast<IntType>()) {
Node* concat_node = b->owningGraph()->create(onnx::Concat, 1);
concat_node->i_(attr::axis, 0);
concat_node->insertBefore(*it);
concat_node->addInput(it->input(0));
concat_node->addInput(it->input(1));
concat_node->outputs()[0]->setType(TensorType::fromNumberType(*elem));
concat_node->copyMetadata(*it);
it->replaceAllUsesWith(concat_node);
it->removeAllInputs();
it.destroyCurrent();
}
}
}
}
// This pass also covers the case when the input to ListUnpack
// is int[] comming from some other op than ListConstruct (like Slice or Shape)
//
// before the pass
// graph(%x.1 : Float(2, 3, strides=[3, 1], requires_grad=0, device=cpu)):
// %1 : None = prim::Constant()
// %2 : int[] = aten::size(%x.1)
// %a.1 : int, %b.1 : int = prim::ListUnpack(%2)
// %5 : int[] = prim::ListConstruct(%a.1, %b.1)
// %6 : Tensor = aten::new_zeros(%x.1, %5, %1, %1, %1, %1)
//
// after the pass:
// graph(%x.1 : Float(2, 3, strides=[3, 1], requires_grad=0, device=cpu)):
// %1 : None = prim::Constant()
// %2 : int[] = aten::size(%x.1)
// %7 : Tensor = onnx::Constant[value={0}]()
// %8 : Tensor = onnx::Gather(%2, %7)
// %9 : Tensor = onnx::Constant[value={1}]()
// %10 : Tensor = onnx::Gather(%2, %9)
// %a.1 : int, %b.1 : int = prim::ListUnpack(%2)
// %5 : int[] = prim::ListConstruct(%8, %10)
// %6 : Tensor = aten::new_zeros(%x.1, %5, %1, %1, %1, %1)
static void fuseListAndListUnpack(Block* b) {
for (auto it = b->nodes().begin(), end = b->nodes().end(); it != end; ++it) {
for (auto* child_block : it->blocks()) {
fuseListAndListUnpack(child_block);
}
if (it->kind() == prim::ListUnpack) {
for (const auto i : c10::irange(it->outputs().size())) {
auto output = it->outputs().at(i);
if (it->inputs().size() == 1 &&
it->input()->node()->kind() != prim::ListConstruct &&
it->input()->type()->cast<ListType>() &&
it->input()
->type()
->castRaw<ListType>()
->getElementType()
->cast<IntType>()) {
Node* gather_indices = b->owningGraph()->create(onnx::Constant, 1);
gather_indices->insertBefore(*it);
gather_indices->t_(
attr::value, at::scalar_to_tensor(at::Scalar(int(i))));
Node* gather_node = b->owningGraph()->create(onnx::Gather, 1);
gather_node->insertBefore(*it);
gather_node->addInput(it->input());
gather_node->addInput(gather_indices->output());
gather_node->copyMetadata(*it);
output->replaceAllUsesWith(gather_node->output());
}
}
}
}
}
} // namespace
void PreprocessForONNX(std::shared_ptr<Graph>& graph) {
FuseWithListUnpack(graph->block());
GRAPH_DUMP("After FuseWithListUnpack: ", graph);
ReplaceAddWithConcat(graph->block());
GRAPH_DUMP("After ReplaceAddWithConcat: ", graph);
fuseListAndListUnpack(graph->block());
GRAPH_DUMP("After fuseListAndListUnpack: ", graph);
}
} // namespace jit
} // namespace torch
|