code: def test_sparse_elementwise_sum():
      def check_sparse_elementwise_sum_with_shape(stypes, shape, n):
          # forward
          inputs = [mx.symbol.Variable(f"arg{i}") for i in range(n)]
          out = mx.symbol.sparse.add_n(*inputs, name="esum")
          arr = []
          arr_grad = [
              mx.nd.empty(shape, stype=stype) for stype in stypes
          ]
          densities = [0, 0.01, 0.5, 1.0]
          for stype in stypes:
              arr.append(
                  rand_ndarray(
                      shape,
                      stype,
                      densities[np.random.randint(0, len(densities))],
                  )
              )
  
          exec1 = out._bind(
              default_device(), args=arr, args_grad=arr_grad
          )
          exec1.forward(is_train=True)
          out1 = exec1.outputs[0].asnumpy()
          out = sum(a.asnumpy() for a in arr)
          assert_almost_equal(out, out1, atol=1e-5)
  
          out_grad = mx.nd.empty(shape)
          out_grad[:] = np.random.uniform(-10, 10, shape)
          # backward
          exec1.backward([out_grad])
          for a in arr_grad:
              assert_almost_equal(
                  a.asnumpy(), out_grad.asnumpy(), atol=1e-5
              )
  
      all_stypes = ["default", "csr", "row_sparse"]
      for dim in range(2, 4):
          shape = tuple(np.random.randint(5, 10, size=dim))
          rsp_test_cnt = np.random.randint(1, 9)
          check_sparse_elementwise_sum_with_shape(
              ["row_sparse" for i in range(rsp_test_cnt)],
              shape,
              rsp_test_cnt,
          )
          if dim is 2:
              check_sparse_elementwise_sum_with_shape(
                  ["default", "csr", "default"], shape, 3
              )
              test_len = np.random.randint(5, 10)
              # at least one default type
              stypes = ["default"]
              for _ in range(test_len):
                  pick_side = np.random.randint(2)
                  pick_type = np.random.randint(3)
                  stypes = (
                      (
                          [all_stypes[pick_type]]
                          if pick_side is 0
                          else []
                      )
                      + stypes
                      + (
                          [all_stypes[pick_type]]
                          if pick_side is 1
                          else []
                      )
                  )
              check_sparse_elementwise_sum_with_shape(
                  stypes, shape, test_len + 1
              )
  
imports: from mxnet.test_utils import *
  from mxnet.base import MXNetError
  import pytest
  from common import assertRaises
  import random
  import warnings
label_desc: Use image node_id 29 for calling a global function with example usage: test_sparse_elementwise_sum() without return types