segfault when destroying a large op tree.
This code causes a segfault
{
shared_ptr<Node> result;
result = make_shared<op::Parameter>(element::Float32::element_type(), Shape{1});
for (int i=0; i<100000; i++)
{
result = make_shared<op::Negative>(result);
}
}
if the for loop has fewer iteration then things work fine, no segfault
Since all of the ops are chained together when result is freed it causes nested calls to destructors. A backtrace shows
#1 0x00007ffff6b6153c in __GI___libc_free (mem=<optimized out>) at malloc.c:2968
#2 0x00000000004a1770 in __gnu_cxx::new_allocator<std::_Rb_tree_node<ngraph::Node*> >::deallocate(std::_Rb_tree_node<ngraph::Node*>*, unsigned long) ()
#3 0x00000000004a1740 in std::allocator_traits<std::allocator<std::_Rb_tree_node<ngraph::Node*> > >::deallocate(std::allocator<std::_Rb_tree_node<ngraph::Node*> >&, std::_Rb_tree_node<ngraph::Node*>*, unsigned long) ()
#4 0x00000000004a162c in std::_Rb_tree<ngraph::Node*, ngraph::Node*, std::_Identity<ngraph::Node*>, std::less<ngraph::Node*>, std::allocator<ngraph::Node*> >::_M_put_node(std::_Rb_tree_node<ngraph::Node*>*) ()
#5 0x00000000004a158c in std::_Rb_tree<ngraph::Node*, ngraph::Node*, std::_Identity<ngraph::Node*>, std::less<ngraph::Node*>, std::allocator<ngraph::Node*> >::_M_drop_node(std::_Rb_tree_node<ngraph::Node*>*) ()
#6 0x00000000004a14b8 in std::_Rb_tree<ngraph::Node*, ngraph::Node*, std::_Identity<ngraph::Node*>, std::less<ngraph::Node*>, std::allocator<ngraph::Node*> >::_M_erase(std::_Rb_tree_node<ngraph::Node*>*) ()
#7 0x00000000004a1428 in std::_Rb_tree<ngraph::Node*, ngraph::Node*, std::_Identity<ngraph::Node*>, std::less<ngraph::Node*>, std::allocator<ngraph::Node*> >::~_Rb_tree() ()
#8 0x00000000004a1325 in std::multiset<ngraph::Node*, std::less<ngraph::Node*>, std::allocator<ngraph::Node*> >::~multiset() ()
#9 0x00000000004a1394 in ngraph::Node::~Node() ()
#10 0x00000000004a1305 in ngraph::Op::~Op() ()
#11 0x00000000004a1225 in ngraph::op::Builtin::~Builtin() ()
#12 0x00000000004c7db5 in ngraph::op::UnaryElementwiseBuiltin::~UnaryElementwiseBuiltin() ()
#13 0x00000000004c7cf5 in ngraph::op::Negative::~Negative() ()
#14 0x00000000004c7ec4 in void __gnu_cxx::new_allocator<ngraph::op::Negative>::destroy<ngraph::op::Negative>(ngraph::op::Negative*) ()
#15 0x00000000004c7e58 in void std::allocator_traits<std::allocator<ngraph::op::Negative> >::destroy<ngraph::op::Negative>(std::allocator<ngraph::op::Negative>&, ngraph::op::Negative*) ()
#16 0x00000000004c7948 in std::_Sp_counted_ptr_inplace<ngraph::op::Negative, std::allocator<ngraph::op::Negative>, (__gnu_cxx::_Lock_policy)2>::_M_dispose()```
more info from gdb
$1 = 8387432
(gdb) shell ulimit -s
8192
If I'm reading that right we run out of stack space.
upping the stack size lets the test pass
ulimit -s 65532
I have a fix where I added a new function in util called free_nodes which deletes the nodes in sorted order. What I do is clear the arguments list for each node in something approaching execution order. This allows nodes shared_ptrs to be freed with using tons of stack.
fixes in PR #103
If we need to manage deallocation like that, the shared pointer are doing us no good and we should get rid of them and just track all the allocated nodes and free them at the end of a compilation.
If we end up using coarse-grained memory management, I should mention that Postgres uses an approach that's worked very well for them:
IIRC, they have a stack of memory heaps. The stack gets built up along the following lines, IIRC:
- Stack Base: heap for the whole server process
- Per-user-connection heap
- Per-transaction heap
By default, allocating memory is done on whatever heap is on the top of the stack. And although explicit deallocation is permitted, one can also rely on it happening when the heap containing a given allocation gets popped from the stack.
That being said, I really hope we don't find ourselves needing anything remotely this sophisticated.
One compilation would correspond to a transaction. We used allocators in ObjectStore to get things the right place. This should be a lot simpler.
I would stay away from any complexity until it becomes a problem. shared_ptr is becoming a problem so we should look at alternatives. The free_nodes function does solve the problem but we still may want to do what @diyessi says.
Totally agree about the minimal complexity.
One idea, which you may or may not consider too complex for our needs: create a class named Graph, which owns all of the relevant C++ objects (Node, Tensor, Input, etc.)
We could leave most of the existing code as it is, in terms of using shared_ptr objects to refer to those relevant C++ objects (Node, Tensor, Input, etc.)
However, the Graph object would also hold a shared_ptr to all of those objects. When the Graph is destroyed, it could .reset() its shared_ptrs one at a time, in order to avoid the nasty callstack-blowing chained deletions that @rkimballn1 discovered.
This is somewhat in line with what @diyessi suggested above. And IIRC, it's a bit like how LLVM does it with its Module class, but simpler (I'd have to double check).
@cconvey that is kind of what I ended up with except I create the external list of shared_ptr within the free_nodes method. The up side is that it does not change any code, just adds the free_nodes at the end. The downside is you have to call free_nodes, but only if you have an absurdly large model. It will work for now. Since I suspect there will be other changes with the graph I think we can use this for now.