haojin2 commented on issue #11330: [MXNET-537] add_n(dense, csr, dense) = dense and add_n([dense, csr, rsp]*, dense, [dense, csr, rsp]*) = dense on CPU & GPU URL: https://github.com/apache/incubator-mxnet/pull/11330#issuecomment-398583696 Benchmark result for add_n(more than 4 inputs with at least 1 dense) = dense: ([density%] [speedup]) CPU: 1.00% 1.4248320861874664 0.50% 1.4591373125830511 0.10% 1.487516900293522 0.05% 1.4891773584928327 0.01% 1.4833875047500007 GPU: 1.00% 1.5829503717448206 0.50% 1.612348854910054 0.10% 1.6657770987040201 0.05% 1.6743607944367647 0.01% 1.6844786052948375 Benchmark script: ```python import mxnet as mx import sys import os import scipy import numpy as np from mxnet.test_utils import rand_ndarray, assert_almost_equal import time def measure_cost(repeat, a, b, c, d, e, out=None): # start bench start = time.time() results = [] for i in range(repeat): results.append(mx.nd.sparse.add_n(a, b, c, d, e, 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, 128) dns = np.random.uniform(size=shape) context = mx.gpu(0) # context = mx.cpu() mx_dns1 = mx.nd.array(dns, ctx=context) mx_dns2 = mx.nd.array(dns, ctx=context) mx_dns3 = 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') mx_rsp = rand_ndarray(shape=shape, stype='row_sparse', density=density).as_in_context(context) mx_rsp_dns = mx_rsp.tostype('default') sparse_cost = 0.0 dns_cost = 0.0 mx.nd.waitall() #warmup check = mx.nd.sparse.add_n(mx_dns1, mx_csr, mx_rsp, mx_dns2, mx_dns3) dns1 = dns + mx_csr_dns.asnumpy() + mx_rsp_dns.asnumpy() + dns + dns assert_almost_equal(check.asnumpy(), dns1, atol=1e-5, rtol=1e-4) mx.nd.waitall() for i in range(20): sparse_cost += measure_cost(5, mx_dns1, mx_csr, mx_dns2, mx_rsp, mx_dns3) dns_cost += measure_cost(5, mx_dns1, mx_csr_dns, mx_dns2, mx_rsp_dns, mx_dns3) 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: us...@infra.apache.org With regards, Apache Git Services