Asscalar() or eq. asnumpy()[0] is particularly slow to execute


#1

nd.asscalar() or equivalent nd.asnumpy()[0] are both very slow to execute (tracing or no the code)

Does anyone experienced the same?
Any solution to this problem?

Many thanks,
AL


#2

Could you please share the code you used to measure the time taken by asscalar or asnumpy?

Note that recording time taken by asnumpy() is a little tricky because operations are executed asynchronously. For example, here is a wrong way to measure time taken by asnumpy() This will include time taken by dot because dot (like every other operator) executes asynchronously.

from mxnet import nd
from time import time


x = nd.random_uniform(shape=(2000,2000))
y = nd.dot(x, x)

start = time()
z = y.asnumpy()
print('asnumpy took  %f sec' % (time() - start))

To measure time right, you need to call nd.waitall() before nd.asnumpy(). Example:

x = nd.random_uniform(shape=(2000,2000))
y = nd.dot(x, x)

nd.waitall()

start = time()
z = y.asnumpy()
print('asnumpy took  %f sec' % (time() - start))

#3

ndarray is usually executed in asynchronous mode, asscalar() or asnumpy() has to wait the previous operation to finish.

To get the accurate asscalar() time, you have to call nd.waitall() before that.


#4

This is it!
The trap!
Many thanks to both of you.
AL.