code: def test_sample_categorical(dtype, x):
      x = mx.nd.array(x) / 10.0
      dx = mx.nd.ones_like(x)
      mx.autograd.mark_variables([x], [dx])
      # Adding rtol and increasing samples needed to pass with seed 2951820647
      samples = 10000
      with mx.autograd.record():
          y, prob = mx.nd.random.categorical(
              x, shape=samples, get_prob=True, dtype=dtype
          )
          r = prob * 5
          r.backward()
  
      assert np.dtype(dtype) == y.dtype
      y = y.asnumpy()
      x = x.asnumpy()
      dx = dx.asnumpy()
      if len(x.shape) is 1:
          x = x.reshape((1, x.shape[0]))
          dx = dx.reshape(1, dx.shape[0])
          y = y.reshape((1, y.shape[0]))
          prob = prob.reshape((1, prob.shape[0]))
      for i in range(x.shape[0]):
          freq = (
              np.bincount(y[i, :].astype("int32"), minlength=5)
              / np.float32(samples)
              * x[i, :].sum()
          )
          assert_almost_equal(freq, x[i], rtol=0.20, atol=1e-1)
          rprob = x[i][y[i].astype("int32")] / x[i].sum()
          assert_almost_equal(
              np.log(rprob), prob.asnumpy()[i], atol=1e-5
          )
  
          real_dx = np.zeros((5,))
          for j in range(samples):
              real_dx[int(y[i][j])] += 5.0 / rprob[j]
          assert_almost_equal(real_dx, dx[i, :], rtol=1e-4, atol=1e-5)
  
imports: import os
  import math
  import itertools
  import mxnet
  from mxnet.test_utils import verify_generator, gen_buckets_probs_with_ppf, assert_almost_equal
  import numpy
  import random
  from common import retry, random_seed
  import scipy.stats
  import unittest
  import pytest
  from mxnet.test_utils import *
  from mxnet.base import MXNetError
  from common import assertRaises
label_desc: Use image node_id 11 for calling a global function with example usage: test_sample_categorical(dtype, x) without return types