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
|
#include <torch/csrc/jit/runtime/static/ops.h>
#include <ATen/NativeFunctions.h>
#include <torch/csrc/jit/ir/ir.h>
namespace torch {
namespace jit {
bool canRunOutOfPlace(Node* n) {
auto str = std::string(n->kind().toQualString());
if ((str == "aten::add") || (str == "aten::mul") || (str == "aten::addmm") ||
(str == "aten::bmm") || (str == "aten::sigmoid") ||
(str == "aten::cat") || (str == "aten::transpose") ||
(str == "aten::flatten")) {
return true;
}
return false;
}
std::function<void(StaticRuntime::ConstantMap&)> getOutOfPlaceOperation(
Node* n) {
auto create_empty_from = [](const at::Tensor& t) {
return at::empty({0}, t.options());
};
if (n->kind() == c10::Symbol::fromQualString("aten::add")) {
auto out = n->outputs().at(0);
auto in0 = n->inputs().at(0);
auto in1 = n->inputs().at(1);
auto in2 = n->inputs().at(2);
return [=](StaticRuntime::ConstantMap& ws) {
auto in0_t = ws.at(in0).toTensor();
auto in1_t = ws.at(in1).toTensor();
auto in2_s = ws.at(in2).toScalar();
if (!ws.count(out)) {
ws.emplace(out, create_empty_from(in0_t));
}
auto out_t = ws.at(out).toTensor();
at::native::add_out(out_t, in0_t, in1_t, in2_s);
};
} else if (n->kind() == c10::Symbol::fromQualString("aten::mul")) {
auto out = n->outputs().at(0);
auto in0 = n->inputs().at(0);
auto in1 = n->inputs().at(1);
return [=](StaticRuntime::ConstantMap& ws) {
auto in0_t = ws.at(in0).toTensor();
auto in1_t = ws.at(in1).toTensor();
if (!ws.count(out)) {
ws.emplace(out, create_empty_from(in0_t));
}
auto out_t = ws.at(out).toTensor();
at::native::mul_out(out_t, in0_t, in1_t);
};
} else if (n->kind() == c10::Symbol::fromQualString("aten::addmm")) {
auto out = n->outputs().at(0);
auto in0 = n->inputs().at(0);
auto in1 = n->inputs().at(1);
auto in2 = n->inputs().at(2);
auto in3 = n->inputs().at(3);
auto in4 = n->inputs().at(4);
return [=](StaticRuntime::ConstantMap& ws) {
auto in0_t = ws.at(in0).toTensor();
auto in1_t = ws.at(in1).toTensor();
auto in2_t = ws.at(in2).toTensor();
auto in3_s = ws.at(in3).toScalar();
auto in4_s = ws.at(in3).toScalar();
if (!ws.count(out)) {
ws.emplace(out, create_empty_from(in0_t));
}
auto out_t = ws.at(out).toTensor();
at::native::addmm_cpu_out(out_t, in0_t, in1_t, in2_t, in3_s, in4_s);
};
} else if (n->kind() == c10::Symbol::fromQualString("aten::clamp")) {
auto out = n->outputs().at(0);
auto in0 = n->inputs().at(0);
auto in1 = n->inputs().at(1);
auto in2 = n->inputs().at(2);
return [=](StaticRuntime::ConstantMap& ws) {
auto in0_t = ws.at(in0).toTensor();
auto in1_s = ws.at(in1).toScalar();
auto in2_s = ws.at(in2).toScalar();
if (!ws.count(out)) {
ws.emplace(out, create_empty_from(in0_t));
}
auto out_t = ws.at(out).toTensor();
at::native::clamp_out(out_t, in0_t, in1_s, in2_s);
};
} else if (n->kind() == c10::Symbol::fromQualString("aten::bmm")) {
auto out = n->outputs().at(0);
auto in0 = n->inputs().at(0);
auto in1 = n->inputs().at(1);
return [=](StaticRuntime::ConstantMap& ws) {
auto in0_t = ws.at(in0).toTensor();
auto in1_t = ws.at(in1).toTensor();
if (!ws.count(out)) {
ws.emplace(out, create_empty_from(in0_t));
}
auto out_t = ws.at(out).toTensor();
at::native::bmm_out_cpu(out_t, in0_t, in1_t);
};
} else if (n->kind() == c10::Symbol::fromQualString("aten::cat")) {
auto out = n->outputs().at(0);
auto in0 = n->inputs().at(0);
auto in1 = n->inputs().at(1);
return [=](StaticRuntime::ConstantMap& ws) {
auto in0_tl = ws.at(in0).toTensorVector();
auto in1_i = ws.at(in1).toInt();
if (!ws.count(out)) {
ws.emplace(out, create_empty_from(in0_tl[0]));
}
auto out_t = ws.at(out).toTensor();
at::native::_cat_out_cpu(out_t, in0_tl, in1_i);
};
} else if (n->kind() == c10::Symbol::fromQualString("aten::sigmoid")) {
auto out = n->outputs().at(0);
auto in0 = n->inputs().at(0);
return [=](StaticRuntime::ConstantMap& ws) {
auto in0_t = ws.at(in0).toTensor();
if (!ws.count(out)) {
ws.emplace(out, create_empty_from(in0_t));
}
auto out_t = ws.at(out).toTensor();
at::native::sigmoid_out(out_t, in0_t);
};
} else if (n->kind() == c10::Symbol::fromQualString("aten::transpose")) {
auto out = n->outputs().at(0);
auto in0 = n->inputs().at(0);
auto in1 = n->inputs().at(1);
auto in2 = n->inputs().at(2);
return [=](StaticRuntime::ConstantMap& ws) {
auto in0_t = ws.at(in0).toTensor();
auto in1_i = ws.at(in1).toInt();
auto in2_i = ws.at(in2).toInt();
ws[out] = at::native::transpose(in0_t, in1_i, in2_i);
};
} else if (n->kind() == c10::Symbol::fromQualString("aten::flatten")) {
auto out = n->outputs().at(0);
auto in0 = n->inputs().at(0);
auto in1 = n->inputs().at(1);
auto in2 = n->inputs().at(2);
return [=](StaticRuntime::ConstantMap& ws) {
auto in0_t = ws.at(in0).toTensor();
auto in1_i = ws.at(in1).toInt();
auto in2_i = ws.at(in2).toInt();
ws[out] = at::native::flatten(in0_t, in1_i, in2_i);
};
}
return [](StaticRuntime::ConstantMap&) { TORCH_CHECK(0); };
}
} // namespace jit
} // namespace torch
|