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
|
#include <torch/csrc/jit/frontend/ir_emitter.h>
#include <torch/csrc/jit/jit_log.h>
#include <torch/csrc/jit/passes/constant_propagation.h>
#include <torch/csrc/jit/passes/peephole.h>
#include <torch/csrc/jit/runtime/decomposition_registry.h>
#include <torch/csrc/jit/runtime/decomposition_registry_util.h>
#include <torch/csrc/jit/runtime/operator.h>
#include <torch/csrc/jit/serialization/import_source.h>
#include <c10/util/Exception.h>
#include <torch/csrc/autograd/jit_decomp_interface.h>
#include <torch/csrc/jit/ir/ir.h>
#include <torch/csrc/jit/passes/constant_propagation.h>
#include <torch/csrc/jit/passes/inliner.h>
#include <torch/csrc/jit/passes/peephole.h>
#include <torch/csrc/jit/runtime/graph_executor.h>
#include <memory>
#include <unordered_map>
namespace torch {
namespace jit {
namespace {
std::mutex lock;
// CompilationUnit that holds all these Functions and keeps them alive.
auto compilation_unit = std::make_shared<CompilationUnit>();
std::unordered_map<const FunctionSchema*, std::shared_ptr<Graph>>
schema_to_decomposition;
// Holds User-Registered Functions and keeps them alive
std::unordered_map<const FunctionSchema*, std::unique_ptr<Function>>
user_registered_funcs;
std::unordered_map<const FunctionSchema*, Function*> schema_to_function;
void loadModule(const CompilationUnit& module) {
const auto& mappings = GetDecompositionMapping().getAllKeysAndValues();
for (const auto& pair : mappings) {
const FunctionSchema* schema = &pair.first->schema();
const std::string& decomposition_function_name = pair.second;
Function& decomposition_function =
module.get_function(decomposition_function_name);
std::shared_ptr<Graph> graph =
toGraphFunction(decomposition_function).graph();
schema_to_function[schema] = &decomposition_function;
schema_to_decomposition[schema] = graph;
}
}
void loadDecompositionFunctions() {
std::lock_guard<std::mutex> guard(lock);
if (schema_to_decomposition.size() != 0) {
return;
}
auto src = std::make_shared<Source>(GetSerializedDecompositions());
std::stringstream ss;
std::vector<at::IValue> constantTable;
auto resolver = std::make_shared<SourceImporterImpl>(
compilation_unit,
&constantTable,
[&](const std::string& name) -> std::shared_ptr<Source> { return src; },
1);
compilation_unit->define(
c10::nullopt, GetSerializedDecompositions(), resolver, nullptr);
loadModule(*compilation_unit);
}
} // anonymous namespace
void DecomposeOp(Node* n) {
auto schema = n->maybeSchema();
if (!schema) {
return;
}
auto decomposition = GetDecomposition(n->schema());
if (!decomposition) {
return;
}
WithInsertPoint guard(n);
auto outputs =
insertGraph(*n->owningGraph(), *decomposition->get(), n->inputs());
TORCH_INTERNAL_ASSERT(outputs.size() == n->outputs().size());
for (size_t i : c10::irange(outputs.size())) {
n->outputs().at(i)->replaceAllUsesWith(outputs[i]);
}
n->destroy();
}
void RunDecompositions(Block* block) {
for (auto it = block->nodes().begin(); it != block->nodes().end();) {
Node* n = *it;
it++; // advance iterator bc the current node may be destroyed
for (Block* b : n->blocks()) {
RunDecompositions(b);
}
DecomposeOp(n);
}
}
void RunDecompositions(std::shared_ptr<Graph> g) {
RunDecompositions(g->block());
for (C10_UNUSED const auto _ : c10::irange(2)) {
PeepholeOptimize(g, /*disable_shape_peephole*/ true);
ConstantPropagation(g);
}
}
c10::optional<std::shared_ptr<Graph>> GetDecomposition(
const FunctionSchema& schema) {
loadDecompositionFunctions();
GRAPH_DEBUG("Trying to find schema: ", schema);
auto cache_it = schema_to_decomposition.find(&schema);
if (cache_it != schema_to_decomposition.end()) {
return cache_it->second;
}
GRAPH_DEBUG("Could not find schema: ", schema);
return c10::nullopt;
}
c10::optional<GraphFunction*> GetDecompositionFunction(
const FunctionSchema& schema) {
loadDecompositionFunctions();
auto cache_it = schema_to_function.find(&schema);
GRAPH_DEBUG("Trying to find schema: ", schema);
if (cache_it == schema_to_function.end()) {
GRAPH_DEBUG("Could not find schema: ", schema);
return c10::nullopt;
}
auto& func = toGraphFunction(*cache_it->second);
// Simple Executor:
// To allow decomposition to run on tensor subclasses such as batched tensors,
// we set decompostion execution to use the simple executor so that
// optimizations that do not compose with arbitrary subclasses (such as
// fusion) do not run
func._set_initial_executor_execution_mode(ExecutorExecutionMode::SIMPLE);
return &func;
}
// Decomposition registers a Graph so that we can initialize a GraphFunction
// that will run with Simple Executor
void RegisterDecomposition(
const FunctionSchema& schema,
std::shared_ptr<Graph> g) {
loadDecompositionFunctions();
std::lock_guard<std::mutex> guard(lock);
Inline(*g);
for (const auto i : c10::irange(2)) {
(void)i; // Suppress unused variable warning
PeepholeOptimize(g);
ConstantPropagationImmutableTypes(g);
}
std::unique_ptr<GraphFunction> new_func(new GraphFunction(
schema.name(), g, nullptr, ExecutorExecutionMode::SIMPLE));
user_registered_funcs.emplace(&schema, std::move(new_func));
schema_to_function[&schema] = user_registered_funcs[&schema].get();
schema_to_decomposition[&schema] = g;
}
// see NOTE: [Jit Decomposition Interface]
struct JitDecomp final : torch::autograd::impl::JitDecompInterface {
bool has_jit_decomposition(const c10::FunctionSchema& schema) const override;
void run_jit_decomposition(
const c10::OperatorHandle& op,
torch::jit::Stack* stack) const override;
};
JitDecomp jitDecomp;
torch::autograd::impl::JitDecompRegisterer registerJitDecomp(&jitDecomp);
void JitDecomp::run_jit_decomposition(
const c10::OperatorHandle& op,
torch::jit::Stack* stack) const {
::torch::jit::run_jit_decomposition(op, stack);
}
bool JitDecomp::has_jit_decomposition(const FunctionSchema& schema) const {
return ::torch::jit::has_jit_decomposition(schema);
}
void run_jit_decomposition(
const c10::OperatorHandle& op,
torch::jit::Stack* stack) {
const auto& schema = op.schema();
// TODO: templatize based on op and keep static trace_exec
auto* trace_exec = torch::jit::GetDecompositionExecutor(schema);
trace_exec->run((*stack));
if (stack->back().isTuple()) {
at::IValue tup = stack->back();
stack->pop_back();
for (const auto& elem : tup.toTuple()->elements()) {
stack->push_back(elem);
}
}
}
bool has_jit_decomposition(const FunctionSchema& schema) {
return GetDecompositionFunction(schema).has_value();
}
Function* GetDecompositionExecutor(const FunctionSchema& schema) {
auto maybe_func = GetDecompositionFunction(schema);
TORCH_INTERNAL_ASSERT(maybe_func);
return *maybe_func;
}
Function* GetDecompositionExecutor(const char* schema_literal) {
auto& schema = getOperatorForLiteral(schema_literal)->schema();
return GetDecompositionExecutor(schema);
}
} // namespace jit
} // namespace torch
|