code: def ref_top_k_data(
      x, k, abs, reduce, base_axis, largest, with_index, grad=None
  ):
      ns = np.prod(x.shape[:base_axis], dtype=int)  # number of samples
      ss = np.prod(x.shape[base_axis:], dtype=int)  # sample size
      xd = x.reshape(ns, ss).copy()
      sign = -1.0 if largest else 1.0
      ix = np.argsort(sign * (np.abs(xd) if abs else xd))[:, :k]
      yd = np.zeros((ns, k if reduce else ss))
      indices = np.full(yd.shape, -1)
      for idx, row in enumerate(ix):
          yd[idx, slice(None) if reduce else row] = xd[idx, row]
          indices[idx, slice(None) if reduce else row] = row
      if grad is not None and reduce is True:
          gg = grad.reshape(yd.shape).copy()
          xg = np.zeros(xd.shape)
          for idx, row in enumerate(ix):
              xg[idx, row] = gg[idx]
          xg = xg.reshape(x.shape)
      else:
          xg = grad
      yd = yd.squeeze(axis=0) if base_axis == 0 else yd
      yd = yd.reshape(x.shape[:base_axis] + (k,) if reduce else x.shape)
      indices = indices.reshape(yd.shape)
      if with_index:
          return ((yd, indices), xg)
      else:
          return (yd, xg)
  
file_path: nnabla/python/test/function/test_top_k_data.py
imports: import pytest
  import numpy
  import nnabla.functions
  from nbla_test_utils import list_context, function_tester
label_desc: Use image node_id 1 for calling a global function with example usage: ref_top_k_data(x, k, abs, reduce, base_axis, largest, with_index, grad) and returns: yd,  indices,  xg, yd,  xg