I'm a writing a C++ custom operator in MXNet and am having trouble finding documentation on when kAddTo
is set in an operator invocation. As a minimal example, let's say that my new operator is called foo()
and I want to perform the following calculation:
A = mx.sym.Variable('A')
B = mx.sym.Variable('B')
T = mx.sym.foo(A)
T += mx.sym.foo(B)
In general, how do I ensure that the fourth line above accumulates into T as opposed to creating a new temporary storage for the result of mx.sym.foo(B)
and then performing the T = T + temp
calculation?
(Using the Kernighan-Ritchie debugger, aka print statements, I found that kWriteTo
is set on both lines three and four. The enum kAddTo
is never set.)
A bit more detail concerning my specific problem: in my current implementation foo()
zeroes out the output memory before performing a calculation which populates it with the appropriate values. I definitely only want to perform this zeroing out when creating a new output location, not when accumulating into an existing one.
Update
Offline, a colleague suggested using
mx.sym.elemwise_add(lhs=T, rhs=mx.sym.foo(B), out=T)
in place of line 4, above. However, I still saw that kWriteTo
was being set in both lines of computation. I then received the following response:
“Memory planning and inplace operations are automatic. It will be done automatically. Users don’t need to worry about it.”, which probably means that
req[0]
is not an accurate indicator in this case. If you want to verify whether it’s an inplace addTo, you can print out the value ofoutputs[0].dptr_
andlhs.dptr_
to see whether they are equal.
I haven't checked this, yet.