sxjscience commented on issue #9833: [Metric] Accelerate the calculation of F1
URL: https://github.com/apache/incubator-mxnet/pull/9833#issuecomment-367406100
 
 
   I've tested the case that calls `.asnumpy()` before using the numpy.sum. 
However, the numpy version is still faster:
   ```python
   import mxnet as mx
   import mxnet.ndarray as nd
   import numpy as np
   import time
   
   # Warm up the GPU
   for _ in range(10):
       a = nd.ones((100, 100), ctx=mx.gpu())
       b = a * 2
       b.asnumpy()
   
   N = 100
   
   # Test the speed
   for data_shape in [(16,), (64,), (256,), (1024,)]:
       dat_npy = np.random.uniform(0, 1, data_shape)
       dat_nd_gpu = nd.array(dat_npy, ctx=mx.gpu())
       dat_nd_cpu = nd.array(dat_npy, ctx=mx.cpu())
       nd.waitall()
       start = time.time()
       for _ in range(N):
           np_ret = np.sum(dat_nd_gpu.asnumpy())
       end = time.time()
       np_copy_from_gpu_time = end - start
       start = time.time()
       for _ in range(N):
           np_ret = np.sum(dat_nd_cpu.asnumpy())
       end = time.time()
       np_copy_from_cpu_time = end - start
       start = time.time()
       for _ in range(N):
           nd_ret = nd.sum(dat_nd_gpu).asscalar()
       end = time.time()
       nd_gpu_time = end - start
       start = time.time()
       for _ in range(N):
           nd_ret = nd.sum(dat_nd_cpu).asscalar()
       end = time.time()
       nd_cpu_time = end - start
       print('sum, data_shape=%s, numpy from gpu=%g, numpy from cpu=%g, mxnet 
gpu=%g, mxnet cpu=%g' %(str(data_shape), np_copy_from_gpu_time, 
np_copy_from_cpu_time, nd_gpu_time, nd_cpu_time))
   ```
   Result:
   ```
   sum, data_shape=(16,), numpy from gpu=0.0132685, numpy from cpu=0.00352144, 
mxnet gpu=0.0388703, mxnet cpu=0.0236092
   sum, data_shape=(64,), numpy from gpu=0.0118794, numpy from cpu=0.00273204, 
mxnet gpu=0.0307364, mxnet cpu=0.0268292
   sum, data_shape=(256,), numpy from gpu=0.0102284, numpy from cpu=0.00267339, 
mxnet gpu=0.0215137, mxnet cpu=0.0267296
   sum, data_shape=(1024,), numpy from gpu=0.0122139, numpy from 
cpu=0.00344729, mxnet gpu=0.025255, mxnet cpu=0.0139427
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to