Commit 3da0e440 authored by Robert Kimball's avatar Robert Kimball Committed by Matthew Brookhart

Memory Leak with External Function (#568)

* add unit test for resource deallocation

fix leak

* cleanup
parent 45126153
...@@ -249,6 +249,10 @@ runtime::cpu::CPU_ExternalFunction::CPU_ExternalFunction( ...@@ -249,6 +249,10 @@ runtime::cpu::CPU_ExternalFunction::CPU_ExternalFunction(
{ {
} }
runtime::cpu::CPU_ExternalFunction::~CPU_ExternalFunction()
{
}
void runtime::cpu::CPU_ExternalFunction::compile() void runtime::cpu::CPU_ExternalFunction::compile()
{ {
if (m_is_compiled) if (m_is_compiled)
...@@ -256,14 +260,16 @@ void runtime::cpu::CPU_ExternalFunction::compile() ...@@ -256,14 +260,16 @@ void runtime::cpu::CPU_ExternalFunction::compile()
return; return;
} }
m_mkldnn_emitter.reset(new MKLDNNEmitter(shared_from_this())); string function_name = m_function->get_name();
m_mkldnn_emitter.reset(new MKLDNNEmitter());
ngraph::pass::Manager pass_manager; ngraph::pass::Manager pass_manager;
pass_manager.register_pass<ngraph::pass::CoreFusion>(); pass_manager.register_pass<ngraph::pass::CoreFusion>();
pass_manager.register_pass<runtime::cpu::pass::CPUFusion>(); pass_manager.register_pass<runtime::cpu::pass::CPUFusion>();
pass_manager.register_pass<runtime::cpu::pass::CPUAssignment>(shared_from_this()); pass_manager.register_pass<runtime::cpu::pass::CPUAssignment>(this);
pass_manager.register_pass<runtime::cpu::pass::CPULayout>(shared_from_this()); pass_manager.register_pass<runtime::cpu::pass::CPULayout>(this);
pass_manager.register_pass<ngraph::pass::Liveness>(); pass_manager.register_pass<ngraph::pass::Liveness>();
pass_manager.register_pass<ngraph::pass::MemoryLayout>(s_memory_pool_alignment); pass_manager.register_pass<ngraph::pass::MemoryLayout>(s_memory_pool_alignment);
......
...@@ -75,6 +75,7 @@ namespace ngraph ...@@ -75,6 +75,7 @@ namespace ngraph
public: public:
CPU_ExternalFunction(const std::shared_ptr<ngraph::Function>& function, CPU_ExternalFunction(const std::shared_ptr<ngraph::Function>& function,
bool release_function = true); bool release_function = true);
~CPU_ExternalFunction();
std::shared_ptr<ngraph::runtime::CallFrame> make_call_frame(); std::shared_ptr<ngraph::runtime::CallFrame> make_call_frame();
const LayoutDescriptorPtrs& get_parameter_layout_descriptors(); const LayoutDescriptorPtrs& get_parameter_layout_descriptors();
......
...@@ -30,7 +30,8 @@ std::shared_ptr<ngraph::runtime::Backend> runtime::cpu::CPU_Manager::allocate_ba ...@@ -30,7 +30,8 @@ std::shared_ptr<ngraph::runtime::Backend> runtime::cpu::CPU_Manager::allocate_ba
std::shared_ptr<ngraph::runtime::ExternalFunction> std::shared_ptr<ngraph::runtime::ExternalFunction>
runtime::cpu::CPU_Manager::compile(const std::shared_ptr<ngraph::Function>& fun) runtime::cpu::CPU_Manager::compile(const std::shared_ptr<ngraph::Function>& fun)
{ {
return std::make_shared<CPU_ExternalFunction>(fun); auto rc = std::make_shared<CPU_ExternalFunction>(fun);
return rc;
} }
ngraph::runtime::Manager::Factory runtime::cpu::CPU_Manager::factory = ngraph::runtime::Manager::Factory runtime::cpu::CPU_Manager::factory =
......
...@@ -27,18 +27,18 @@ using namespace ngraph::runtime::cpu; ...@@ -27,18 +27,18 @@ using namespace ngraph::runtime::cpu;
const std::vector<mkldnn::primitive*>& MKLDNNEmitter::get_mkldnn_primitives() const const std::vector<mkldnn::primitive*>& MKLDNNEmitter::get_mkldnn_primitives() const
{ {
return mkldnn_primitives; return m_mkldnn_primitives;
} }
size_t MKLDNNEmitter::insert_primitive(mkldnn::primitive* primitive) size_t MKLDNNEmitter::insert_primitive(mkldnn::primitive* primitive)
{ {
mkldnn_primitives.emplace_back(primitive); m_mkldnn_primitives.emplace_back(primitive);
return (mkldnn_primitives.size() - 1); return (m_mkldnn_primitives.size() - 1);
} }
const std::vector<size_t>& MKLDNNEmitter::get_primitive_deps(size_t index) const const std::vector<size_t>& MKLDNNEmitter::get_primitive_deps(size_t index) const
{ {
return primitive_deps.at(index); return m_primitive_deps.at(index);
} }
mkldnn::memory::desc MKLDNNEmitter::build_memory_descriptor(const TensorViewWrapper& tvw, mkldnn::memory::desc MKLDNNEmitter::build_memory_descriptor(const TensorViewWrapper& tvw,
...@@ -96,11 +96,11 @@ size_t MKLDNNEmitter::build_convolution_forward(const mkldnn::memory::desc& inpu ...@@ -96,11 +96,11 @@ size_t MKLDNNEmitter::build_convolution_forward(const mkldnn::memory::desc& inpu
mkldnn::memory::dims(padding_above.begin(), padding_above.end()), mkldnn::memory::dims(padding_above.begin(), padding_above.end()),
mkldnn::padding_kind::zero}, mkldnn::padding_kind::zero},
mkldnn_utils::global_cpu_engine}, mkldnn_utils::global_cpu_engine},
*mkldnn_primitives[input_data_index], *m_mkldnn_primitives[input_data_index],
*mkldnn_primitives[weights_index], *m_mkldnn_primitives[weights_index],
*mkldnn_primitives[result_index])); *m_mkldnn_primitives[result_index]));
primitive_deps[conv_index] = {input_data_index, weights_index, result_index}; m_primitive_deps[conv_index] = {input_data_index, weights_index, result_index};
return conv_index; return conv_index;
} }
...@@ -129,11 +129,11 @@ size_t MKLDNNEmitter::build_convolution_forward(const mkldnn::memory::desc& inpu ...@@ -129,11 +129,11 @@ size_t MKLDNNEmitter::build_convolution_forward(const mkldnn::memory::desc& inpu
mkldnn::memory::dims(padding_above.begin(), padding_above.end()), mkldnn::memory::dims(padding_above.begin(), padding_above.end()),
mkldnn::padding_kind::zero}, mkldnn::padding_kind::zero},
mkldnn_utils::global_cpu_engine}, mkldnn_utils::global_cpu_engine},
*mkldnn_primitives[input_data_index], *m_mkldnn_primitives[input_data_index],
*mkldnn_primitives[weights_index], *m_mkldnn_primitives[weights_index],
*mkldnn_primitives[result_index])); *m_mkldnn_primitives[result_index]));
primitive_deps[conv_index] = {input_data_index, weights_index, result_index}; m_primitive_deps[conv_index] = {input_data_index, weights_index, result_index};
return conv_index; return conv_index;
} }
...@@ -151,16 +151,16 @@ size_t MKLDNNEmitter::build_elementwise_add( ...@@ -151,16 +151,16 @@ size_t MKLDNNEmitter::build_elementwise_add(
size_t input1_data_index = build_memory_primitive(input1_data_desc); size_t input1_data_index = build_memory_primitive(input1_data_desc);
size_t result_index = build_memory_primitive(result_desc); size_t result_index = build_memory_primitive(result_desc);
inputs_primitive.push_back(*mkldnn_primitives[input0_data_index]); inputs_primitive.push_back(*m_mkldnn_primitives[input0_data_index]);
inputs_primitive.push_back(*mkldnn_primitives[input1_data_index]); inputs_primitive.push_back(*m_mkldnn_primitives[input1_data_index]);
// elementwise sum primtive descriptor // elementwise sum primtive descriptor
mkldnn::sum::primitive_desc sum_pd = mkldnn::sum::primitive_desc sum_pd =
mkldnn::sum::primitive_desc(result_desc, scale_vector, inputs_pd); mkldnn::sum::primitive_desc(result_desc, scale_vector, inputs_pd);
// sum primitive // sum primitive
size_t add_index = insert_primitive( size_t add_index = insert_primitive(
new mkldnn::sum(sum_pd, inputs_primitive, *mkldnn_primitives[result_index])); new mkldnn::sum(sum_pd, inputs_primitive, *m_mkldnn_primitives[result_index]));
primitive_deps[add_index] = {input0_data_index, input1_data_index, result_index}; m_primitive_deps[add_index] = {input0_data_index, input1_data_index, result_index};
return add_index; return add_index;
} }
...@@ -37,11 +37,7 @@ namespace ngraph ...@@ -37,11 +37,7 @@ namespace ngraph
class MKLDNNEmitter class MKLDNNEmitter
{ {
public: public:
MKLDNNEmitter(std::shared_ptr<CPU_ExternalFunction> ef) MKLDNNEmitter() {}
: external_function(ef)
{
}
const std::vector<mkldnn::primitive*>& get_mkldnn_primitives() const; const std::vector<mkldnn::primitive*>& get_mkldnn_primitives() const;
size_t insert_primitive(mkldnn::primitive* primitive); size_t insert_primitive(mkldnn::primitive* primitive);
...@@ -77,10 +73,9 @@ namespace ngraph ...@@ -77,10 +73,9 @@ namespace ngraph
const std::vector<mkldnn::memory::primitive_desc>& input_pd); const std::vector<mkldnn::memory::primitive_desc>& input_pd);
private: private:
std::shared_ptr<CPU_ExternalFunction> external_function; std::vector<mkldnn::primitive*> m_mkldnn_primitives;
std::vector<mkldnn::primitive*> mkldnn_primitives; std::vector<mkldnn::stream> m_mkldnn_streams;
std::vector<mkldnn::stream> mkldnn_streams; std::unordered_map<size_t, std::vector<size_t>> m_primitive_deps;
std::unordered_map<size_t, std::vector<size_t>> primitive_deps;
}; };
} }
} }
......
...@@ -256,7 +256,7 @@ bool runtime::cpu::pass::CPUAssignment::run_on_call_graph( ...@@ -256,7 +256,7 @@ bool runtime::cpu::pass::CPUAssignment::run_on_call_graph(
auto handler = s_dispatcher.find(TI(n)); auto handler = s_dispatcher.find(TI(n));
if (handler != s_dispatcher.end()) if (handler != s_dispatcher.end())
{ {
handler->second(m_external_function.get(), node.get()); handler->second(m_external_function, node.get());
} }
} }
......
...@@ -39,7 +39,7 @@ namespace ngraph ...@@ -39,7 +39,7 @@ namespace ngraph
class CPUAssignment : public ngraph::pass::CallGraphPass class CPUAssignment : public ngraph::pass::CallGraphPass
{ {
public: public:
CPUAssignment(std::shared_ptr<CPU_ExternalFunction> external_function) CPUAssignment(CPU_ExternalFunction* external_function)
: m_external_function(external_function) : m_external_function(external_function)
{ {
} }
...@@ -56,7 +56,7 @@ namespace ngraph ...@@ -56,7 +56,7 @@ namespace ngraph
} }
private: private:
std::shared_ptr<CPU_ExternalFunction> m_external_function; CPU_ExternalFunction* m_external_function;
}; };
} }
} }
......
...@@ -719,11 +719,11 @@ bool runtime::cpu::pass::CPULayout::run_on_call_graph(const std::list<std::share ...@@ -719,11 +719,11 @@ bool runtime::cpu::pass::CPULayout::run_on_call_graph(const std::list<std::share
auto handler = s_dispatcher.find(TI(n)); auto handler = s_dispatcher.find(TI(n));
if (handler != s_dispatcher.end()) if (handler != s_dispatcher.end())
{ {
handler->second(m_external_function.get(), node); handler->second(m_external_function, node);
} }
else else
{ {
set_default_layouts(m_external_function.get(), node); set_default_layouts(m_external_function, node);
} }
} }
......
...@@ -39,7 +39,7 @@ namespace ngraph ...@@ -39,7 +39,7 @@ namespace ngraph
class CPULayout : public ngraph::pass::CallGraphPass class CPULayout : public ngraph::pass::CallGraphPass
{ {
public: public:
CPULayout(std::shared_ptr<CPU_ExternalFunction> external_function) CPULayout(CPU_ExternalFunction* external_function)
: m_external_function(external_function) : m_external_function(external_function)
{ {
} }
...@@ -52,7 +52,7 @@ namespace ngraph ...@@ -52,7 +52,7 @@ namespace ngraph
std::shared_ptr<ngraph::Node> node); std::shared_ptr<ngraph::Node> node);
private: private:
std::shared_ptr<CPU_ExternalFunction> m_external_function; CPU_ExternalFunction* m_external_function;
static std::shared_ptr<Node> insert_input_conversions( static std::shared_ptr<Node> insert_input_conversions(
CPU_ExternalFunction* external_function, CPU_ExternalFunction* external_function,
std::shared_ptr<Node>& node, std::shared_ptr<Node>& node,
......
...@@ -43,6 +43,29 @@ static const vector<element::Type> s_known_element_types = {element::from<float> ...@@ -43,6 +43,29 @@ static const vector<element::Type> s_known_element_types = {element::from<float>
element::from<uint32_t>(), element::from<uint32_t>(),
element::from<uint64_t>()}; element::from<uint64_t>()};
TEST(${BACKEND_NAME}, component_cleanup)
{
shared_ptr<runtime::Backend> backend;
shared_ptr<runtime::ExternalFunction> external;
shared_ptr<runtime::CallFrame> cf;
{
Shape shape{2, 2};
auto A = make_shared<op::Parameter>(element::f32, shape);
auto B = make_shared<op::Parameter>(element::f32, shape);
auto f = make_shared<Function>(A + B, op::ParameterVector{A, B});
auto manager = runtime::Manager::get("${BACKEND_NAME}");
external = manager->compile(f);
backend = manager->allocate_backend();
cf = backend->make_call_frame(external);
}
EXPECT_EQ(cf.use_count(), 1);
cf = nullptr;
EXPECT_EQ(backend.use_count(), 1);
backend = nullptr;
EXPECT_EQ(external.use_count(), 1);
}
TEST(${BACKEND_NAME}, aliased_output) TEST(${BACKEND_NAME}, aliased_output)
{ {
Shape shape{2, 2}; Shape shape{2, 2};
...@@ -138,7 +161,7 @@ TEST(${BACKEND_NAME}, abc) ...@@ -138,7 +161,7 @@ TEST(${BACKEND_NAME}, abc)
auto f = make_shared<Function>((A + B) * C, op::ParameterVector{A, B, C}); auto f = make_shared<Function>((A + B) * C, op::ParameterVector{A, B, C});
auto manager = runtime::Manager::get("${BACKEND_NAME}"); auto manager = runtime::Manager::get("${BACKEND_NAME}");
auto external = manager->compile(f); shared_ptr<runtime::ExternalFunction> external = manager->compile(f);
auto backend = manager->allocate_backend(); auto backend = manager->allocate_backend();
auto cf = backend->make_call_frame(external); auto cf = backend->make_call_frame(external);
......
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