Commit 5f914429 authored by nmostafa's avatar nmostafa

Move CompiledKernel op under mlir_backend lib. Add compiler to op. Track compilation status

parent 607445a4
...@@ -23,6 +23,8 @@ set(SRC ...@@ -23,6 +23,8 @@ set(SRC
memory_manager.cpp memory_manager.cpp
pass/mlir_subgraph_extraction.cpp pass/mlir_subgraph_extraction.cpp
pass/mlir_subgraph_extraction.hpp pass/mlir_subgraph_extraction.hpp
compiled_kernel.cpp
compiled_kernel.hpp
) )
if (NGRAPH_MLIR_ENABLE) if (NGRAPH_MLIR_ENABLE)
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
// limitations under the License. // limitations under the License.
//***************************************************************************** //*****************************************************************************
#include "ngraph/op/experimental/compiled_kernel.hpp" #include "compiled_kernel.hpp"
#include "ngraph/graph_util.hpp" #include "ngraph/graph_util.hpp"
#include "ngraph/log.hpp" #include "ngraph/log.hpp"
...@@ -67,6 +67,8 @@ ngraph::op::CompiledKernel::CompiledKernel(const NodeVector& node_list, ...@@ -67,6 +67,8 @@ ngraph::op::CompiledKernel::CompiledKernel(const NodeVector& node_list,
: Op("CompiledKernel", check_single_output_args({args})) : Op("CompiledKernel", check_single_output_args({args}))
, m_node_list(node_list) , m_node_list(node_list)
, m_output_nodes(outputs) , m_output_nodes(outputs)
, m_mlir_compiler(this)
, m_is_compiled(false)
{ {
constructor_validate_and_infer_types(); constructor_validate_and_infer_types();
set_output_size(m_output_nodes.size()); set_output_size(m_output_nodes.size());
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "ngraph/op/op.hpp" #include "ngraph/op/op.hpp"
#include "ngraph/util.hpp" #include "ngraph/util.hpp"
#include "contrib/mlir/compiler.hpp"
namespace ngraph namespace ngraph
{ {
...@@ -40,9 +41,32 @@ namespace ngraph ...@@ -40,9 +41,32 @@ namespace ngraph
const NodeVector& get_node_list() const { return m_node_list; } const NodeVector& get_node_list() const { return m_node_list; }
const NodeVector& get_kernel_outputs() const { return m_output_nodes; } const NodeVector& get_kernel_outputs() const { return m_output_nodes; }
/// Compiles the sub-graph associated with this CompiledKernel
void compile()
{
if (m_is_compiled)
{
return;
}
m_mlir_compiler.compile();
m_is_compiled = true;
}
/// Runs the sub-graph
void run(std::vector<void*>& ptr_args)
{
NGRAPH_CHECK(m_is_compiled, "CompiledKernel node not compiled yet");
m_mlir_compiler.set_args(&ptr_args);
m_mlir_compiler.run();
}
bool is_compiled() const
{
return m_is_compiled;
}
private: private:
NodeVector m_node_list; NodeVector m_node_list;
NodeVector m_output_nodes; NodeVector m_output_nodes;
ngraph::runtime::ngmlir::MLIRCompiler m_mlir_compiler;
bool m_is_compiled;
}; };
} }
} }
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
#include "ngraph/op/concat.hpp" #include "ngraph/op/concat.hpp"
#include "ngraph/op/divide.hpp" #include "ngraph/op/divide.hpp"
#include "ngraph/op/dot.hpp" #include "ngraph/op/dot.hpp"
#include "ngraph/op/experimental/compiled_kernel.hpp" #include "compiled_kernel.hpp"
#include "ngraph/op/greater.hpp" #include "ngraph/op/greater.hpp"
#include "ngraph/op/less.hpp" #include "ngraph/op/less.hpp"
#include "ngraph/op/maximum.hpp" #include "ngraph/op/maximum.hpp"
...@@ -69,16 +69,6 @@ using namespace ngraph::runtime::ngmlir; ...@@ -69,16 +69,6 @@ using namespace ngraph::runtime::ngmlir;
#define COMPILE_OP_DECL(op_name) \ #define COMPILE_OP_DECL(op_name) \
create_op<op_name>(MLIRCompiler & compiler, const ngraph::Node* ng_node) create_op<op_name>(MLIRCompiler & compiler, const ngraph::Node* ng_node)
MLIRCompiler::MLIRCompiler(const ngraph::op::CompiledKernel* compiled_kernel,
const std::vector<void*>& external_tensors)
: m_compiled_kernel(compiled_kernel)
, m_external_tensors(external_tensors)
{
NGRAPH_CHECK((m_compiled_kernel->get_arguments().size() +
m_compiled_kernel->get_kernel_outputs().size()) == external_tensors.size(),
"Number of arguments and outputs doesn't match number of tensors");
}
void MLIRCompiler::init_mlir() void MLIRCompiler::init_mlir()
{ {
// Mutex to safely initialize MLIR. // Mutex to safely initialize MLIR.
...@@ -96,11 +86,24 @@ void MLIRCompiler::init_mlir() ...@@ -96,11 +86,24 @@ void MLIRCompiler::init_mlir()
} }
} }
void MLIRCompiler::compile_and_run() void MLIRCompiler::set_args(std::vector<void*>* external_tensors)
{
NGRAPH_CHECK(m_compiled_kernel, "No compiled kernel set for compiler");
NGRAPH_CHECK((m_compiled_kernel->get_arguments().size() +
m_compiled_kernel->get_kernel_outputs().size()) == external_tensors->size(),
"Number of arguments and outputs doesn't match number of tensors");
m_external_tensors = external_tensors;
}
void MLIRCompiler::compile()
{ {
build_ng_dialect_module(); build_ng_dialect_module();
lower_ng_dialect(); lower_ng_dialect();
optimize(); optimize();
}
void MLIRCompiler::run()
{
bind_arguments(); bind_arguments();
execute(); execute();
cleanup(); cleanup();
...@@ -471,13 +474,13 @@ void MLIRCompiler::bind_arguments() ...@@ -471,13 +474,13 @@ void MLIRCompiler::bind_arguments()
NGRAPH_CHECK(expected_arguments.size(), "Arguments can't be created"); NGRAPH_CHECK(expected_arguments.size(), "Arguments can't be created");
m_invoke_args = std::move(expected_arguments); m_invoke_args = std::move(expected_arguments);
NGRAPH_CHECK(m_invoke_args.size() == m_external_tensors.size(), NGRAPH_CHECK(m_invoke_args.size() == m_external_tensors->size(),
"Number of external tensors doesn't match number of function arguments"); "Number of external tensors doesn't match number of function arguments");
// Assign external tensor pointers to invocation arguments. // Assign external tensor pointers to invocation arguments.
for (size_t i = 0, num_args = m_invoke_args.size(); i < num_args; ++i) for (size_t i = 0, num_args = m_invoke_args.size(); i < num_args; ++i)
{ {
((mlir::StaticFloatMemRef*)m_invoke_args[i])->data = (float*)m_external_tensors[i]; ((mlir::StaticFloatMemRef*)m_invoke_args[i])->data = (float*)(*m_external_tensors)[i];
} }
// Add pointer to memory manager // Add pointer to memory manager
......
...@@ -60,11 +60,15 @@ namespace ngraph ...@@ -60,11 +60,15 @@ namespace ngraph
using TensorList = std::vector<descriptor::Tensor*>; using TensorList = std::vector<descriptor::Tensor*>;
using TypeList = llvm::SmallVector<mlir::Type, 4>; using TypeList = llvm::SmallVector<mlir::Type, 4>;
MLIRCompiler(const ngraph::op::CompiledKernel* compiled_kernel, MLIRCompiler(const ngraph::op::CompiledKernel* compiled_kernel)
const std::vector<void*>& external_tensors); : m_compiled_kernel(compiled_kernel) {}
/// Compiles and runs a subgraph in MLIR. /// Set runtime tensor arguments for the sub-graph
void compile_and_run(); void set_args(std::vector<void*>* external_tensors);
/// Compiles a subgraph with MLIR
void compile();
/// Executes a pre-compiled subgraph
void run();
/// Returns the memory manager used by this sub-graph compiler. /// Returns the memory manager used by this sub-graph compiler.
MLIRMemMgr& get_mem_mgr() { return m_mem_mgr; } MLIRMemMgr& get_mem_mgr() { return m_mem_mgr; }
...@@ -134,7 +138,7 @@ namespace ngraph ...@@ -134,7 +138,7 @@ namespace ngraph
const ngraph::op::CompiledKernel* m_compiled_kernel; const ngraph::op::CompiledKernel* m_compiled_kernel;
// Pointers to externally allocated memory for sub-graph's input and output tensors. // Pointers to externally allocated memory for sub-graph's input and output tensors.
const std::vector<void*>& m_external_tensors; std::vector<void*>* m_external_tensors;
// Arguments for the MLIR function generated for the nGraph sub-graph. // Arguments for the MLIR function generated for the nGraph sub-graph.
llvm::SmallVector<void*, 8> m_invoke_args; llvm::SmallVector<void*, 8> m_invoke_args;
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
#include "ngraph/op/concat.hpp" #include "ngraph/op/concat.hpp"
#include "ngraph/op/divide.hpp" #include "ngraph/op/divide.hpp"
#include "ngraph/op/dot.hpp" #include "ngraph/op/dot.hpp"
#include "ngraph/op/experimental/compiled_kernel.hpp" #include "contrib/mlir/compiled_kernel.hpp"
#include "ngraph/op/get_output_element.hpp" #include "ngraph/op/get_output_element.hpp"
#include "ngraph/op/greater.hpp" #include "ngraph/op/greater.hpp"
#include "ngraph/op/less.hpp" #include "ngraph/op/less.hpp"
......
...@@ -174,8 +174,6 @@ set (SRC ...@@ -174,8 +174,6 @@ set (SRC
op/experimental/quantized_dot.hpp op/experimental/quantized_dot.hpp
op/experimental/quantized_dot_bias.cpp op/experimental/quantized_dot_bias.cpp
op/experimental/quantized_dot_bias.hpp op/experimental/quantized_dot_bias.hpp
op/experimental/compiled_kernel.cpp
op/experimental/compiled_kernel.hpp
op/experimental/transpose.cpp op/experimental/transpose.cpp
op/experimental/transpose.hpp op/experimental/transpose.hpp
op/experimental/layers/ctc_greedy_decoder.cpp op/experimental/layers/ctc_greedy_decoder.cpp
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#include "ngraph/runtime/cpu/cpu_builder.hpp" #include "ngraph/runtime/cpu/cpu_builder.hpp"
#include "contrib/mlir/compiler.hpp" #include "contrib/mlir/compiler.hpp"
#include "ngraph/op/experimental/compiled_kernel.hpp" #include "contrib/mlir/compiled_kernel.hpp"
#include "ngraph/runtime/cpu/cpu_runtime_context.hpp" #include "ngraph/runtime/cpu/cpu_runtime_context.hpp"
using namespace ngraph; using namespace ngraph;
...@@ -65,14 +65,10 @@ namespace ngraph ...@@ -65,14 +65,10 @@ namespace ngraph
{ {
ptr_args.push_back(ctx->buffer_data[buffer_index]); ptr_args.push_back(ctx->buffer_data[buffer_index]);
} }
// Compile nodes within the CompiledKernel op. // Compile nodes within the CompiledKernel op.
auto* compiled_kernel = static_cast<const CompiledKernel*>(node); CompiledKernel* compiled_kernel = static_cast<CompiledKernel*>(const_cast<Node*>(node));
compiled_kernel->compile();
MLIRCompiler mlir_compiler(compiled_kernel, ptr_args); compiled_kernel->run(ptr_args);
// TODO: Decouple 'compile' and 'run' APIs. We want to be able to run the same
// jitted code on different arguments.
mlir_compiler.compile_and_run();
}; };
functors.emplace_back(functor); functors.emplace_back(functor);
......
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
#include "ngraph/op/divide.hpp" #include "ngraph/op/divide.hpp"
#include "ngraph/op/equal.hpp" #include "ngraph/op/equal.hpp"
#include "ngraph/op/exp.hpp" #include "ngraph/op/exp.hpp"
#include "ngraph/op/experimental/compiled_kernel.hpp" #include "contrib/mlir/compiled_kernel.hpp"
#include "ngraph/op/floor.hpp" #include "ngraph/op/floor.hpp"
#include "ngraph/op/get_output_element.hpp" #include "ngraph/op/get_output_element.hpp"
#include "ngraph/op/greater.hpp" #include "ngraph/op/greater.hpp"
......
...@@ -73,7 +73,7 @@ ...@@ -73,7 +73,7 @@
#include "ngraph/op/erf.hpp" #include "ngraph/op/erf.hpp"
#include "ngraph/op/exp.hpp" #include "ngraph/op/exp.hpp"
#include "ngraph/op/experimental/batch_mat_mul.hpp" #include "ngraph/op/experimental/batch_mat_mul.hpp"
#include "ngraph/op/experimental/compiled_kernel.hpp" #include "contrib/mlir/compiled_kernel.hpp"
#include "ngraph/op/experimental/generate_mask.hpp" #include "ngraph/op/experimental/generate_mask.hpp"
#include "ngraph/op/experimental/quantized_avg_pool.hpp" #include "ngraph/op/experimental/quantized_avg_pool.hpp"
#include "ngraph/op/experimental/quantized_concat.hpp" #include "ngraph/op/experimental/quantized_concat.hpp"
......
...@@ -52,7 +52,6 @@ ...@@ -52,7 +52,6 @@
#include "ngraph/op/erf.hpp" #include "ngraph/op/erf.hpp"
#include "ngraph/op/exp.hpp" #include "ngraph/op/exp.hpp"
#include "ngraph/op/experimental/batch_mat_mul.hpp" #include "ngraph/op/experimental/batch_mat_mul.hpp"
#include "ngraph/op/experimental/compiled_kernel.hpp"
#include "ngraph/op/experimental/dyn_broadcast.hpp" #include "ngraph/op/experimental/dyn_broadcast.hpp"
#include "ngraph/op/experimental/dyn_pad.hpp" #include "ngraph/op/experimental/dyn_pad.hpp"
#include "ngraph/op/experimental/dyn_replace_slice.hpp" #include "ngraph/op/experimental/dyn_replace_slice.hpp"
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment