haojin2 commented on issue #10550: [MXNET-320] Support elemwise_add/sub between dense and csr tensors URL: https://github.com/apache/incubator-mxnet/pull/10550#issuecomment-384366007 Benmark Script: ```python import mxnet as mx import numpy as np from mxnet.test_utils import rand_ndarray, assert_almost_equal import time def measure_cost(repeat, a, b, out=None): # start bench start = time.time() results = [] for i in range(repeat): results.append(mx.nd.elemwise_add(a, b, out=out)) for result in results: result.wait_to_read() end = time.time() diff = end - start return diff / repeat def measure_fallback(repeat, a): # start bench start = time.time() results = [] for i in range(repeat): results.append(a.tostype('default')) for result in results: result.wait_to_read() end = time.time() diff = end - start return diff / repeat def main(): shape = (1000000, 512) dns = np.random.uniform(size=shape) context = mx.gpu(0) # context = mx.cpu() mx_dns = mx.nd.array(dns, ctx=context) for density in [0.01, 0.005, 0.001, 0.0005, 0.0001]: mx_csr = rand_ndarray(shape=shape, stype='csr', density=density).as_in_context(context) mx_csr_dns = mx_csr.tostype('default') sparse_cost = 0.0 dns_cost = 0.0 mx.nd.waitall() #warmup dns = mx_dns.asnumpy() mx.nd.elemwise_add(mx_dns, mx_csr, out=mx_dns) dns = dns + mx_csr_dns.asnumpy() assert_almost_equal(mx_dns.asnumpy(), dns, atol=1e-5, rtol=1e-4) mx.nd.waitall() for i in range(100): sparse_cost += measure_cost(1, mx_dns, mx_csr, out=mx_dns) # dns_cost += measure_fallback(1, mx_csr) dns_cost += measure_cost(1, mx_dns, mx_csr_dns, out=mx_dns) print("%.2f %%" % (density*100), dns_cost / sparse_cost) #warmup dns = mx_dns.asnumpy() check = mx.nd.elemwise_add(mx_dns, mx_csr) dns = dns + mx_csr_dns.asnumpy() assert_almost_equal(check.asnumpy(), dns, atol=1e-5, rtol=1e-4) mx.nd.waitall() for i in range(100): sparse_cost += measure_cost(1, mx_dns, mx_csr) # dns_cost += measure_fallback(1, mx_csr) dns_cost += measure_cost(1, mx_dns, mx_csr_dns) print("%.2f %%" % (density*100), dns_cost / sparse_cost) if __name__ == "__main__": main() ```
---------------------------------------------------------------- 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
