<p dir="auto">Hi,I am trying to dynamic load pytorch with dlopen in a C++ program, but something is not wrong. Here is how i am doing.<br>
First I build libxxx.so which is dynamic linked with pytorch lib. Then I dlopen pytorch lib and libxxx.so in another program.<br>
Which means pytorch lib is dynamic linked to libxxx.so and pytorch lib and libxxx.so is dlopened in another program in order.<br>
Everything goes right util the end of the main function. It throws a segmentfault and its backtrace is as follow:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#0  0x0000000000000000 in ?? ()
#1  0x00007fffe7587f61 in c10::Dispatcher::deregisterDef_(c10::OperatorHandle const&amp;, c10::OperatorName const&amp;) ()
   from lib/libtorch_cpu.so
#2  0x00007fffe75be795 in c10::RegisterOperators::~RegisterOperators() ()
   from lib/libtorch_cpu.so
#3  0x00007fffe5aac15e in __static_initialization_and_destruction_0 ()
   from lib/libxxx.so
#4  0x00007fffe5aac35b in _GLOBAL__sub_D_vision.cpp ()
   from lib/libxxx.so
#5  0x00007ffff7de7e27 in _dl_fini () at dl-fini.c:235
#6  0x00007ffff6dac008 in __run_exit_handlers (status=0, listp=0x7ffff71365f8 &lt;__exit_funcs&gt;, run_list_atexit=run_list_atexit@entry=true)
    at exit.c:82
#7  0x00007ffff6dac055 in __GI_exit (status=&lt;optimized out&gt;) at exit.c:104
#8  0x00007ffff6d92847 in __libc_start_main (main=0x4156bc &lt;main(int, char**)&gt;, argc=7, argv=0x7fffffffd2d8, init=&lt;optimized out&gt;,
    fini=&lt;optimized out&gt;, rtld_fini=&lt;optimized out&gt;, stack_end=0x7fffffffd2c8) at ../csu/libc-start.c:325
#9  0x0000000000415019 in _start ()"><pre class="notranslate"><code class="notranslate">#0  0x0000000000000000 in ?? ()
#1  0x00007fffe7587f61 in c10::Dispatcher::deregisterDef_(c10::OperatorHandle const&amp;, c10::OperatorName const&amp;) ()
   from lib/libtorch_cpu.so
#2  0x00007fffe75be795 in c10::RegisterOperators::~RegisterOperators() ()
   from lib/libtorch_cpu.so
#3  0x00007fffe5aac15e in __static_initialization_and_destruction_0 ()
   from lib/libxxx.so
#4  0x00007fffe5aac35b in _GLOBAL__sub_D_vision.cpp ()
   from lib/libxxx.so
#5  0x00007ffff7de7e27 in _dl_fini () at dl-fini.c:235
#6  0x00007ffff6dac008 in __run_exit_handlers (status=0, listp=0x7ffff71365f8 &lt;__exit_funcs&gt;, run_list_atexit=run_list_atexit@entry=true)
    at exit.c:82
#7  0x00007ffff6dac055 in __GI_exit (status=&lt;optimized out&gt;) at exit.c:104
#8  0x00007ffff6d92847 in __libc_start_main (main=0x4156bc &lt;main(int, char**)&gt;, argc=7, argv=0x7fffffffd2d8, init=&lt;optimized out&gt;,
    fini=&lt;optimized out&gt;, rtld_fini=&lt;optimized out&gt;, stack_end=0x7fffffffd2c8) at ../csu/libc-start.c:325
#9  0x0000000000415019 in _start ()
</code></pre></div>
<p dir="auto">I think it might be caused by duplicated destruction of some resources.<br>
Another thing is curious that the destruction seems not to happen when dlclose, but happen at the end of the main function.<br>
I have tried everything i can, but just can't find a solution.<br>
I will be very grateful if anybody can help.</p>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/malfet/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/malfet">@malfet</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/seemethere/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/seemethere">@seemethere</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/walterddr/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/walterddr">@walterddr</a></p>
<p dir="auto">It seems like if we use LongTensor indexing to duplicate entries of a Variable, this breaks autograd. Specifically, if any entry in a vector is duplicated, backward() only maintains gradients for the last copy of the element. Simple example:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="y = Variable(torch.ones(1), requires_grad=True)
y_dup = y[torch.LongTensor([0,0])]
y_dup[1].backward() # backprop on second element
print(y.grad)
&quot;&quot;&quot;
Variable containing:
 1
[torch.FloatTensor of size 1]&quot;&quot;&quot;"><pre class="notranslate"><span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-v">Variable</span>(<span class="pl-s1">torch</span>.<span class="pl-en">ones</span>(<span class="pl-c1">1</span>), <span class="pl-s1">requires_grad</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
<span class="pl-s1">y_dup</span> <span class="pl-c1">=</span> <span class="pl-s1">y</span>[<span class="pl-s1">torch</span>.<span class="pl-v">LongTensor</span>([<span class="pl-c1">0</span>,<span class="pl-c1">0</span>])]
<span class="pl-s1">y_dup</span>[<span class="pl-c1">1</span>].<span class="pl-en">backward</span>() <span class="pl-c"># backprop on second element</span>
<span class="pl-en">print</span>(<span class="pl-s1">y</span>.<span class="pl-s1">grad</span>)
<span class="pl-s">"""</span>
<span class="pl-s">Variable containing:</span>
<span class="pl-s"> 1</span>
<span class="pl-s">[torch.FloatTensor of size 1]"""</span></pre></div>
<p dir="auto">However:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="y = Variable(torch.ones(1), requires_grad=True)
y_dup = y[torch.LongTensor([0,0])]
y_dup[0].backward()  # backprop on first element, same exact value as second
print(y.grad)
&quot;&quot;&quot;
Variable containing:
 0
[torch.FloatTensor of size 1]&quot;&quot;&quot;"><pre class="notranslate"><span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-v">Variable</span>(<span class="pl-s1">torch</span>.<span class="pl-en">ones</span>(<span class="pl-c1">1</span>), <span class="pl-s1">requires_grad</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
<span class="pl-s1">y_dup</span> <span class="pl-c1">=</span> <span class="pl-s1">y</span>[<span class="pl-s1">torch</span>.<span class="pl-v">LongTensor</span>([<span class="pl-c1">0</span>,<span class="pl-c1">0</span>])]
<span class="pl-s1">y_dup</span>[<span class="pl-c1">0</span>].<span class="pl-en">backward</span>()  <span class="pl-c"># backprop on first element, same exact value as second</span>
<span class="pl-en">print</span>(<span class="pl-s1">y</span>.<span class="pl-s1">grad</span>)
<span class="pl-s">"""</span>
<span class="pl-s">Variable containing:</span>
<span class="pl-s"> 0</span>
<span class="pl-s">[torch.FloatTensor of size 1]"""</span></pre></div>
<p dir="auto">This is version under version 0.1.11+fc48d2c, tested on 0.1.11+9f3119a as well.  This happens for both cuda and cpu tensors.</p>