code: def test_sparse_embedding():
      """test sparse embedding operator"""
  
      def check_sparse_embedding(
          in_dim, out_dim, batch, densities, sparse_grad
      ):
          target_stype = "row_sparse" if sparse_grad else "default"
          # init executor
          data = mx.sym.Variable("data")
          weight = mx.sym.Variable("embed_weight")
          embed = mx.sym.sparse.Embedding(
              data=data,
              weight=weight,
              input_dim=in_dim,
              sparse_grad=sparse_grad,
              output_dim=out_dim,
              name="embed",
          )
          grad_req = {"data": "null", "embed_weight": "write"}
          args = {
              "embed_weight": mx.nd.zeros((in_dim, out_dim)),
              "data": mx.nd.ones((batch,)),
          }
          weight_grad = mx.nd.zeros((in_dim, out_dim))
          if sparse_grad:
              weight_grad = weight_grad.tostype("row_sparse")
          args_grad = {"embed_weight": weight_grad}
          exe_test = embed._bind(
              default_device(),
              args=args,
              args_grad=args_grad,
              grad_req=grad_req,
          )
          arg_map = dict(
              zip(embed.list_arguments(), exe_test.arg_arrays)
          )
          grad_map = dict(
              zip(embed.list_arguments(), exe_test.grad_arrays)
          )
          # init data
          np_data = np.random.randint(low=0, high=in_dim, size=batch)
          np_onehot = np.zeros((batch, in_dim)).astype(np.float32)
          np_onehot[np.arange(batch), np_data] = 1.0
          arg_map["data"][:] = np_data
          # weight
          weight = arg_map["embed_weight"]
          for density in densities:
              # update weight based on density
              weight[:] = rand_ndarray(
                  weight.shape, "default", density=density
              )
              # check forward
              exe_test.forward(is_train=True)
              # init grad
              np_grad = np.random.uniform(
                  -1, 1, exe_test.outputs[0].shape
              )
              grad = mx.nd.zeros(np_grad.shape)
              grad[:] = np_grad
              assert_almost_equal(
                  exe_test.outputs[0].asnumpy(),
                  np.dot(np_onehot, weight.asnumpy()),
                  atol=1e-4,
              )
              # check backward
              exe_test.backward([grad])
              assert_almost_equal(
                  grad_map["embed_weight"].asnumpy(),
                  np.dot(np_onehot.T, grad.asnumpy()),
                  atol=1e-4,
              )
              # check grad stype
              assert grad_map["embed_weight"].stype == target_stype
  
      densities = [0, 0.5, 1]
      in_dim = 50
      out_dim = 3
      batch = 8
      sparse_grads = [True, False]
      for sparse_grad in sparse_grads:
          check_sparse_embedding(
              in_dim, out_dim, batch, densities, sparse_grad
          )
  
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 30 for calling a global function with example usage: test_sparse_embedding() without return types