|
| 1 | +#include <torch/csrc/jit/runtime/static/ops.h> |
| 2 | +#include <torch/csrc/jit/ir/ir.h> |
| 3 | + |
| 4 | +namespace torch { |
| 5 | +namespace jit { |
| 6 | + |
| 7 | +bool canRunOutOfPlace(Node* n) { |
| 8 | + auto str = std::string(n->kind().toQualString()); |
| 9 | + if ((str == "aten::add") || (str == "aten::mul") || (str == "aten::addmm") || |
| 10 | + (str == "aten::bmm") || (str == "aten::sigmoid") || |
| 11 | + (str == "aten::cat")) { |
| 12 | + return true; |
| 13 | + } |
| 14 | + return false; |
| 15 | +} |
| 16 | + |
| 17 | +std::function<void(StaticRuntime::ConstantMap&)> getOutOfPlaceOperation( |
| 18 | + Node* n) { |
| 19 | + auto create_empty_from = [](const at::Tensor& t) { |
| 20 | + return at::empty({0}, t.options()); |
| 21 | + }; |
| 22 | + |
| 23 | + if (n->kind() == c10::Symbol::fromQualString("aten::add")) { |
| 24 | + auto out = n->outputs().at(0); |
| 25 | + auto in0 = n->inputs().at(0); |
| 26 | + auto in1 = n->inputs().at(1); |
| 27 | + auto in2 = n->inputs().at(2); |
| 28 | + return [=](StaticRuntime::ConstantMap& ws) { |
| 29 | + auto in0_t = ws.at(in0).toTensor(); |
| 30 | + auto in1_t = ws.at(in1).toTensor(); |
| 31 | + auto in2_s = ws.at(in2).toScalar(); |
| 32 | + if (!ws.count(out)) { |
| 33 | + ws.emplace(out, create_empty_from(in0_t)); |
| 34 | + } |
| 35 | + auto out_t = ws.at(out).toTensor(); |
| 36 | + at::native::add_out(out_t, in0_t, in1_t, in2_s); |
| 37 | + }; |
| 38 | + } else if (n->kind() == c10::Symbol::fromQualString("aten::mul")) { |
| 39 | + auto out = n->outputs().at(0); |
| 40 | + auto in0 = n->inputs().at(0); |
| 41 | + auto in1 = n->inputs().at(1); |
| 42 | + return [=](StaticRuntime::ConstantMap& ws) { |
| 43 | + auto in0_t = ws.at(in0).toTensor(); |
| 44 | + auto in1_t = ws.at(in1).toTensor(); |
| 45 | + if (!ws.count(out)) { |
| 46 | + ws.emplace(out, create_empty_from(in0_t)); |
| 47 | + } |
| 48 | + auto out_t = ws.at(out).toTensor(); |
| 49 | + at::native::mul_out(out_t, in0_t, in1_t); |
| 50 | + }; |
| 51 | + } else if (n->kind() == c10::Symbol::fromQualString("aten::addmm")) { |
| 52 | + auto out = n->outputs().at(0); |
| 53 | + auto in0 = n->inputs().at(0); |
| 54 | + auto in1 = n->inputs().at(1); |
| 55 | + auto in2 = n->inputs().at(2); |
| 56 | + auto in3 = n->inputs().at(3); |
| 57 | + auto in4 = n->inputs().at(4); |
| 58 | + return [=](StaticRuntime::ConstantMap& ws) { |
| 59 | + auto in0_t = ws.at(in0).toTensor(); |
| 60 | + auto in1_t = ws.at(in1).toTensor(); |
| 61 | + auto in2_t = ws.at(in2).toTensor(); |
| 62 | + auto in3_s = ws.at(in3).toScalar(); |
| 63 | + auto in4_s = ws.at(in3).toScalar(); |
| 64 | + if (!ws.count(out)) { |
| 65 | + ws.emplace(out, create_empty_from(in0_t)); |
| 66 | + } |
| 67 | + auto out_t = ws.at(out).toTensor(); |
| 68 | + at::native::addmm_cpu_out(out_t, in0_t, in1_t, in2_t, in3_s, in4_s); |
| 69 | + }; |
| 70 | + } else if (n->kind() == c10::Symbol::fromQualString("aten::clamp")) { |
| 71 | + auto out = n->outputs().at(0); |
| 72 | + auto in0 = n->inputs().at(0); |
| 73 | + auto in1 = n->inputs().at(1); |
| 74 | + auto in2 = n->inputs().at(2); |
| 75 | + return [=](StaticRuntime::ConstantMap& ws) { |
| 76 | + auto in0_t = ws.at(in0).toTensor(); |
| 77 | + auto in1_s = ws.at(in1).toScalar(); |
| 78 | + auto in2_s = ws.at(in2).toScalar(); |
| 79 | + if (!ws.count(out)) { |
| 80 | + ws.emplace(out, create_empty_from(in0_t)); |
| 81 | + } |
| 82 | + auto out_t = ws.at(out).toTensor(); |
| 83 | + at::native::clamp_out(out_t, in0_t, in1_s, in2_s); |
| 84 | + }; |
| 85 | + } else if (n->kind() == c10::Symbol::fromQualString("aten::bmm")) { |
| 86 | + auto out = n->outputs().at(0); |
| 87 | + auto in0 = n->inputs().at(0); |
| 88 | + auto in1 = n->inputs().at(1); |
| 89 | + return [=](StaticRuntime::ConstantMap& ws) { |
| 90 | + auto in0_t = ws.at(in0).toTensor(); |
| 91 | + auto in1_t = ws.at(in1).toTensor(); |
| 92 | + if (!ws.count(out)) { |
| 93 | + ws.emplace(out, create_empty_from(in0_t)); |
| 94 | + } |
| 95 | + auto out_t = ws.at(out).toTensor(); |
| 96 | + at::native::bmm_out_cpu(out_t, in0_t, in1_t); |
| 97 | + }; |
| 98 | + } else if (n->kind() == c10::Symbol::fromQualString("aten::cat")) { |
| 99 | + auto out = n->outputs().at(0); |
| 100 | + auto in0 = n->inputs().at(0); |
| 101 | + auto in1 = n->inputs().at(1); |
| 102 | + return [=](StaticRuntime::ConstantMap& ws) { |
| 103 | + auto in0_tl = ws.at(in0).toTensorVector(); |
| 104 | + auto in1_i = ws.at(in1).toInt(); |
| 105 | + if (!ws.count(out)) { |
| 106 | + ws.emplace(out, create_empty_from(in0_tl[0])); |
| 107 | + } |
| 108 | + auto out_t = ws.at(out).toTensor(); |
| 109 | + at::native::cat_out(out_t, in0_tl, in1_i); |
| 110 | + }; |
| 111 | + } else if (n->kind() == c10::Symbol::fromQualString("aten::sigmoid")) { |
| 112 | + auto out = n->outputs().at(0); |
| 113 | + auto in0 = n->inputs().at(0); |
| 114 | + return [=](StaticRuntime::ConstantMap& ws) { |
| 115 | + auto in0_t = ws.at(in0).toTensor(); |
| 116 | + if (!ws.count(out)) { |
| 117 | + ws.emplace(out, create_empty_from(in0_t)); |
| 118 | + } |
| 119 | + auto out_t = ws.at(out).toTensor(); |
| 120 | + at::native::sigmoid_out(out_t, in0_t); |
| 121 | + }; |
| 122 | + } |
| 123 | + |
| 124 | + return [](StaticRuntime::ConstantMap&) { TORCH_CHECK(0); }; |
| 125 | +} |
| 126 | + |
| 127 | +} // namespace jit |
| 128 | +} // namespace torch |
0 commit comments