High memory consumption using NDArray interface
Here are two codes for comparison:
import mxnet as mx
x = mx.nd.zeros((128, 4096), ctx=mx.gpu(0))
dy = mx.nd.zeros((128, 4096), ctx=mx.gpu(0))
dw = mx.nd.zeros((4096, 4096), ctx=mx.gpu(0))
for i in range(100):
dw += mx.nd.dot(x.T, dy)
dw.wait_to_read()
Ideally, this should only occupy memory spaces for three arrays:
(128 * 4096 + 128 * 4096 + 4096 * 4096) * 4B = 17MB
However, when I run this on TitanX, it consumes ~7GB memory.
After I change the code to following:
import mxnet as mx
x = mx.nd.zeros((128, 4096), ctx=mx.gpu(0))
dy = mx.nd.zeros((128, 4096), ctx=mx.gpu(0))
dw = mx.nd.zeros((4096, 4096), ctx=mx.gpu(0))
for i in range(100):
dw += mx.nd.dot(x.T, dy)
dw.wait_to_read()
It only consumes ~ 400MB memory. Way smaller than the previous one and also faster.
It seems that all the intermediate memory spaces used for the += operator are somehow created rather than doing inplace operation. The question is when the space will be allocated? The += is a mutable operation, so they should be serialized during computation. Is this because our += is not correctly implemented?
The mxnet version is the latest version on nnvm branch.
This is related to the RNN performance of memory consumption of MinPy. Please help @piiswrong @tqchen .
try
import mxnet as mx
x = mx.nd.zeros((128, 4096), ctx=mx.gpu(0))
dy = mx.nd.zeros((128, 4096), ctx=mx.gpu(0))
dw = mx.nd.zeros((4096, 4096), ctx=mx.gpu(0))
tmp = mx.nd.zeros((4096, 4096), ctx=mx.gpu(0))
for i in range(100):
mx.nd.dot(x.T, dy, out = tmp)
dw += tmp
dw.wait_to_read()
Due to there is not dependency between dot and +=, your code was run like dot dot dot dot dot dot dot ..., += += += += += +=, ... So dot allocated a lot of memory blocks that are sitting there waiting for +=, which wasn't scheduled. Ideally you want to run like dot, +=, dot, +=, dot, +=, ... We can fix this by changing engine's scheduling policy
Hmm... I see the problem here. I could fix this example but it is hard to apply it to minpy, because not all the operators support out argument. It is not good to enforce such order in scheduling since sometimes we want to explore such parallelism. This looks like an inevitable problem of imperative programming?
It looks like a trade-off between parallelism-first scheduling or memory-first scheduling. Our strategy right now should be parallelism-first scheduling right?
Every operator supports out.
This wouldn't happen as long as engine respects the order ops are pushed in when possible. So I don't think it will sacrifice performance. If you look at the profiler output the current way of scheduling is pretty stupid
It is a little bit annoying to use out argument. For example in minpy, define gradient function of tanh is return g / tanh(x) ** 2, where g is the output gradient and x is the input. There are multiple operations here and I need to use the out argument only for the last operation. And because it is a / operation, I need to use mx.nd.divide(g, tanh(x)**2, out=gout) for example.
I have an idea to solve this problem in the scheduling part. Suppose we have dependency like:
func1() -> func2() -> func3()
And we know that the result of func1() will only be used by func2(). Suppose func1() is dispatched to thread_1. We then just directly execute func2() on thread_1 without dispatch func2() back to thread pool. This may solve the above problem because the result of the dot product is only used by += operation. Also, this could save some context switch overhead since fewer tasks will be pushed into thread pool. What do you think?
you never know func1 is only going to be used by func2.
Also its hard to modify the engine to do this.
I'm thinking about a total rewrite of the engine that can solve this issue, along with some other issues we have with the engine. I'm trying to find people to do this.
@piiswrong What is your proposal/idea?
@hotpxl Instead of each variable maintaining a mutex and 2 lists, we only maintain 3 atomic int: npushed_ops, nfinished_ops, last_write. This way callbacks can be lock free
.
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!