#include <torch/csrc/jit/backends/backend.h>
#include <torch/csrc/jit/backends/backend_preprocess.h>
#include <torch/csrc/jit/passes/dead_code_elimination.h>
#include <torch/csrc/jit/passes/inliner.h>
namespace torch {
namespace jit {
namespace {
c10::IValue preprocess(
const Module& mod,
const c10::Dict<IValue, IValue>& method_compile_spec,
const BackendDebugHandleGenerator& generate_debug_handles) {
c10::Dict<IValue, IValue> compiled(StringType::get(), StringType::get());
for (const auto& method : mod.get_methods()) {
auto graph = toGraphFunction(method.function()).graph()->copy();
Inline(*graph);
EliminateDeadCode(graph);
auto key = method.name();
auto node_debug_handles = generate_debug_handles(graph);
std::stringstream ss;
for (const auto& node : graph->nodes()) {
switch (node->kind()) {
case prim::Constant:
ss << node->kind().toDisplayString() << '#'
<< toIValue(node->output()).value();
ss << "<debug_handle>" << node_debug_handles[node];
break;
case aten::add:
ss << node->kind().toQualString();
ss << "<debug_handle>" << node_debug_handles[node];
break;
case aten::sub:
ss << node->kind().toQualString();
ss << "<debug_handle>" << node_debug_handles[node];
break;
default:
TORCH_CHECK(
false,
"The node of ",
node->kind().toQualString(),
" is not supported in this compiler. Source code: ",
node->sourceRange().str());
}
ss << ',';
}
std::string blob = std::move(ss).str();
if (!blob.empty()) {
blob.pop_back();
}
compiled.insert(method.name(), blob);
}
return compiled;
}
constexpr auto backend_name = "backend_with_compiler_demo";
static auto pre_reg = backend_preprocess_register(backend_name, preprocess);
}
}
}