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-398581824 Benchmark result for add_n(dense, csr, dense) = dense: ([density%] [speedup]) CPU: 1.00% 1.1282194997074237 0.50% 1.1686160529139418 0.10% 1.1909255730224886 0.05% 1.1970586102280831 0.01% 1.202483677804412 GPU: 1.00% 1.1627124767202126 0.50% 1.2392510678426578 0.10% 1.3169708612264934 0.05% 1.3275811285384644 0.01 % 1.3358768672033845 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, out=None): # start bench start = time.time() results = [] for i in range(repeat): results.append(mx.nd.sparse.add_n(a, b, c, 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 = (128, 1000000) 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) 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 check = mx.nd.sparse.add_n(mx_dns1, mx_csr, mx_dns2) dns1 = dns + mx_csr_dns.asnumpy() + 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) dns_cost += measure_cost(5, mx_dns1, mx_csr_dns, mx_dns2) 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