mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

Activation followed by Reshape wastes memory

Open asmushetzel opened this issue 9 years ago • 1 comments

Remark: I'm planning to work on this within next 4 weeks or so unless someone else already will provide a fix. Open the issue here to track that. Below the description copied from an email thread:

I came across this analyzing some memory issue in a NMT-project. The networks there are very short but wide and the application is very memory intensive. What happened is that a tiny fragment namely an activation followed by reshape caused a big memory issue (detailed analysis at the end). The short summary is that the reshape operator is basically an identity when it comes to forward/backward operations, but as this is not known by the memory planning kernel (it just sees an in-place operation but not that the tensor content is not changed), it may use more memory than needed (for storing forward pass results). This is absolutely non-transparent for a user. And could be avoided for example by either 1) flag reshape operator as not altering tensor content and use this flag in memory planning or 2) fuse such operator automatically with the preceding one. The impact in this case was very significant (3GB memory savings when the order of the two operators was switched)

ANALYSIS:

The reason seems that your application is a very wide and short network (resp. consists mainly out of wide/short subnetworks). In this type of networks, it makes a huge difference if the system misses a single opportunity to reuse memory along the computation graph. Best look at two examples where “I” and “O” are inputs/outputs of the network, R is a reshape operator and A an activation operator.

First option:

I->R->A ->O

R and A operator specify that they can do in-place forward. So the forward pass needs a single chunk of memory that is passed through the network and at the end holds the output. On the backward path, both R and A specify that input/output gradients can be in-place, so there is (another) single chunk of memory passed backward through the network holding the gradients.

The interesting issue is how the backward gradients are computed in the A and R operator:

The R operator is an identity, so it registers with nnvm that it does neither need the original data inputs nor the data outputs (from the forward pass) in order to compute the backward gradient.

The A operator is written in a way that it uses the data outputs from the forward pass (together with the backward propagated gradients) to derive its input gradients. That is due to the fact that for complex activation functions like tanh and sigmoid, it is more efficient to express the derivative in terms of the forward output instead in terms of the forward input.

In this setup, A uses then the final output O as well as topmost gradients in order to do back-propagation.

So here we can get along with 2 memory chunks.

Second option:

I->A->R ->O

Again the forward pass would need a single chunk of memory. But because of the backward pass, it needs 2:

Operator R needs the output of the forward pass as input for the backward pass (as explained above). Therfore it “puts a lock” on the output of R in the forward path such that it will be preserved for backward computation. This “lock” causes one more memory chunk to be generated (during forward pass). So you end up with 3 chunks instead of 2 for overall execution.

Of course from the high level viewpoint, this memory overhead is not necessary. Operator R could have used the data associated with O instead of the data associated with R’s forward output for back-propagation. But this would require that the system would realize that “R” is the identity and therefore the output of A and the output of R are identical. Which it can’t (at the moment). It only can see that “R” performs some in-place computation in the forward pass, it can’t see that this computation is the identity.

asmushetzel avatar May 22 '17 09:05 asmushetzel

This issue is closed due to lack of activity in the last 90 days. Feel free to reopen if this is still an active issue. Thanks!

yajiedesign avatar Sep 30 '17 17:09 yajiedesign