diff --git a/test/cpp/jit/test_misc.cpp b/test/cpp/jit/test_misc.cpp index d48d532d716d..e67df5d32ea0 100644 --- a/test/cpp/jit/test_misc.cpp +++ b/test/cpp/jit/test_misc.cpp @@ -2987,9 +2987,18 @@ graph(%x.1 : Tensor): } TEST(TestFunctionExecutor, RunDecompositionTest) { - static auto* func = torch::jit::GetDecompositionExecutor( - "aten::var(Tensor self, bool unbiased=True) -> Tensor"); + GraphFunction* func; + std::once_flag flag1; for (bool unbiased : {true, false}) { + std::call_once(flag1, [&]() { + // NB: take reference to schema here, `auto schema =` will not work + auto& schema = getOperatorForLiteral( + "aten::var(Tensor self, bool unbiased=True) -> Tensor") + ->schema(); + auto maybe_func = GetDecompositionFunction(schema); + TORCH_INTERNAL_ASSERT(maybe_func); + func = *maybe_func; + }); auto input = at::rand({4, 4}); Stack stack = {input, unbiased}; func->run(stack); diff --git a/test/jit/test_op_decompositions.py b/test/jit/test_op_decompositions.py index 6b4569cd6e39..bfd6edb2e6b8 100644 --- a/test/jit/test_op_decompositions.py +++ b/test/jit/test_op_decompositions.py @@ -21,18 +21,3 @@ class TestOpDecompositions(JitTestCase): inp = torch.rand([10, 10]) self.assertEqual(foo(inp), foo_s(inp)) FileCheck().check_not("aten::var").run(foo_s.graph) - - def test_registered_decomposition(self): - @torch.jit.script - def foo(x): - return torch.square(x) - - @torch.jit.script - def square_decomp(x): - return torch.pow(x, 2) - - torch.jit._register_decomposition(torch.ops.aten.square.default, square_decomp.graph) - torch._C._jit_pass_run_decompositions(foo.graph) - FileCheck().check_not("aten::square").check("aten::pow").run(foo.graph) - x = torch.rand([4]) - self.assertEqual(foo(x), torch.square(x)) diff --git a/torch/_C/__init__.pyi.in b/torch/_C/__init__.pyi.in index 25a98ce39020..14feff067003 100644 --- a/torch/_C/__init__.pyi.in +++ b/torch/_C/__init__.pyi.in @@ -216,7 +216,6 @@ def _jit_pass_metal_optimize_for_mobile(module: 'torch.jit.ScriptModule', def _jit_pass_inline(Graph) -> None: ... def _jit_pass_constant_propagation(Graph) -> None: ... def _jit_pass_propagate_shapes_on_graph(Graph) -> None: ... -def _jit_register_decomposition_for_schema(schema: FunctionSchema, Graph) -> None: ... def _jit_erase_non_input_shape_information(Graph) -> None: ... def _jit_get_schemas_for_operator(name :str) -> List[FunctionSchema]: ... def _jit_get_all_schemas() -> List[FunctionSchema]: ... diff --git a/torch/csrc/jit/python/init.cpp b/torch/csrc/jit/python/init.cpp index 29188f33765f..e1f0478336dd 100644 --- a/torch/csrc/jit/python/init.cpp +++ b/torch/csrc/jit/python/init.cpp @@ -1,7 +1,6 @@ #include #include -#include #include #include #include @@ -169,7 +168,7 @@ void initJITBindings(PyObject* module) { if (!n->maybeSchema()) { return c10::nullopt; } - return GetDecomposition(n->schema()); + return DecompositionGraphForSchema(n->schema()); }) .def("_jit_pass_run_decompositions", RunDecompositions) // using Node* here instead of Schema because looking up the schema @@ -185,16 +184,6 @@ void initJITBindings(PyObject* module) { TORCH_INTERNAL_ASSERT(false, "Expected schema", n); } }) - .def( - "_jit_register_decomposition_for_schema", - [](const FunctionSchema& s, std::shared_ptr& graph) { - // because this is invoked by python, the function schema * - // becomes different, and we need to find and reuse the - // one that is used for caching - auto op = - findOperatorFor(c10::OperatorName(s.name(), s.overload_name())); - RegisterDecomposition(op->schema(), graph); - }) .def("_jit_pass_propagate_shapes_on_graph", PropagateShapesOnGraph) .def( "_jit_pass_propagate_shapes_on_graph_and_build_compute", diff --git a/torch/csrc/jit/runtime/decomposition_registry.cpp b/torch/csrc/jit/runtime/decomposition_registry.cpp index 2f11b622d97a..cc5868a9fd00 100644 --- a/torch/csrc/jit/runtime/decomposition_registry.cpp +++ b/torch/csrc/jit/runtime/decomposition_registry.cpp @@ -8,9 +8,7 @@ #include #include -#include #include -#include #include namespace torch { @@ -23,10 +21,6 @@ auto compilation_unit = std::make_shared(); std::unordered_map> schema_to_decomposition; -// Holds User-Registered Functions and keeps them alive -std::unordered_map> - user_registered_funcs; - std::unordered_map schema_to_function; void loadModule(const CompilationUnit& module) { @@ -71,7 +65,7 @@ void DecomposeOp(Node* n) { if (!schema) { return; } - auto decomposition = GetDecomposition(n->schema()); + auto decomposition = DecompositionGraphForSchema(n->schema()); if (!decomposition) { return; } @@ -104,7 +98,7 @@ void RunDecompositions(std::shared_ptr g) { } } -c10::optional> GetDecomposition( +c10::optional> DecompositionGraphForSchema( const FunctionSchema& schema) { loadDecompositionFunctions(); GRAPH_DEBUG("Trying to find schema: ", schema); @@ -127,35 +121,9 @@ c10::optional GetDecompositionFunction( 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 g) { - loadDecompositionFunctions(); - std::lock_guard guard(lock); - std::unique_ptr 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; -} - -Function* GetDecompositionExecutor(const char* schema_literal) { - auto& schema = getOperatorForLiteral(schema_literal)->schema(); - auto maybe_func = GetDecompositionFunction(schema); - TORCH_INTERNAL_ASSERT(maybe_func); - return *maybe_func; -} - } // namespace jit } // namespace torch diff --git a/torch/csrc/jit/runtime/decomposition_registry.h b/torch/csrc/jit/runtime/decomposition_registry.h index d6746812e0a6..e3a02ced1850 100644 --- a/torch/csrc/jit/runtime/decomposition_registry.h +++ b/torch/csrc/jit/runtime/decomposition_registry.h @@ -8,20 +8,13 @@ namespace torch { namespace jit { -TORCH_API c10::optional> GetDecomposition( +TORCH_API c10::optional> DecompositionGraphForSchema( const FunctionSchema& schema); -TORCH_API void RegisterDecomposition( - const FunctionSchema& schema, - std::shared_ptr g); - TORCH_API void RunDecompositions(std::shared_ptr g); TORCH_API c10::optional GetDecompositionFunction( const FunctionSchema& schema); -// For invocation in C++, recommended is to assign to static local variable -TORCH_API Function* GetDecompositionExecutor(const char* schema_literal); - } // namespace jit } // namespace torch diff --git a/torch/jit/__init__.py b/torch/jit/__init__.py index f3a66bd9d1d7..6e1e79399bbf 100644 --- a/torch/jit/__init__.py +++ b/torch/jit/__init__.py @@ -48,7 +48,6 @@ from torch.jit._trace import ( _get_trace_graph, ) from torch.jit._async import fork, wait -from torch.jit._decomposition_utils import _register_decomposition from torch.jit._serialization import save, load, jit_module_from_flatbuffer, save_jit_module_to_flatbuffer from torch.jit._fuser import optimized_execution, fuser, last_executed_optimized_graph, set_fusion_strategy from torch.jit._freeze import freeze, optimize_for_inference, run_frozen_optimizations @@ -188,6 +187,7 @@ def isinstance(obj, target_type): """ return _isinstance(obj, target_type) + class strict_fusion(object): """ This class errors if not all nodes have been fused in diff --git a/torch/jit/_decomposition_utils.py b/torch/jit/_decomposition_utils.py deleted file mode 100644 index 3aa9b670ed4a..000000000000 --- a/torch/jit/_decomposition_utils.py +++ /dev/null @@ -1,8 +0,0 @@ -import torch -from torch._ops import OpOverload, OpOverloadPacket - -def _register_decomposition(op: OpOverload, graph: torch._C.Graph): - assert not isinstance(op, OpOverloadPacket), f"Must pass specific op overload, not overload packet, found {op}" - assert isinstance(op, OpOverload) - - torch._C._jit_register_decomposition_for_schema(op._schema, graph)