code: def _get_rank_kernel(
      filter_size, rank, mode, w_shape, offsets, cval, int_type
  ):
      s_rank = min(rank, filter_size - rank - 1)
      # The threshold was set based on the measurements on a V100
      # TODO(leofang, anaruse): Use Optuna to automatically tune the threshold,
      # as it may vary depending on the GPU in use, compiler version, dtype,
      # filter size, etc.
      if s_rank <= 80:
          # When s_rank is small and register usage is low, this partial
          # selection sort approach is faster than general sorting approach
          # using shell sort.
          if s_rank == rank:
              comp_op = "<"
          else:
              comp_op = ">"
          array_size = s_rank + 2
          found_post = """
              if (iv > {rank} + 1) {{{{
                  int target_iv = 0;
                  X target_val = values[0];
                  for (int jv = 1; jv <= {rank} + 1; jv++) {{{{
                      if (target_val {comp_op} values[jv]) {{{{
                          target_val = values[jv];
                          target_iv = jv;
                      }}}}
                  }}}}
                  if (target_iv <= {rank}) {{{{
                      values[target_iv] = values[{rank} + 1];
                  }}}}
                  iv = {rank} + 1;
              }}}}""".format(
              rank=s_rank, comp_op=comp_op
          )
          post = """
              X target_val = values[0];
              for (int jv = 1; jv <= {rank}; jv++) {{
                  if (target_val {comp_op} values[jv]) {{
                      target_val = values[jv];
                  }}
              }}
              y=cast<Y>(target_val);""".format(
              rank=s_rank, comp_op=comp_op
          )
          sorter = ""
      else:
          array_size = filter_size
          found_post = ""
          post = "sort(values,{});\ny=cast<Y>(values[{}]);".format(
              filter_size, rank
          )
          sorter = __SHELL_SORT.format(gap=_get_shell_gap(filter_size))
  
      return _filters_core._generate_nd_kernel(
          "rank_{}_{}".format(filter_size, rank),
          "int iv = 0;\nX values[{}];".format(array_size),
          "values[iv++] = {value};" + found_post,
          post,
          mode,
          w_shape,
          int_type,
          offsets,
          cval,
          preamble=sorter,
      )
  
imports: import numpy
  import cupy
  from cupy import _core
  from cupy._core import internal
  from cupyx.scipy.ndimage import _util
  from cupyx.scipy.ndimage import _filters_core
  from cupyx.scipy.ndimage import _filters_generic
label_desc: Use image node_id 33 for calling a global function with example usage: _get_rank_kernel(filter_size, rank, mode, w_shape, offsets, cval, int_type) and returns: _filters_core