class_docstr: Implements TopKOp on gpu
  
  Currently the output seem sorted, but we do not test it. So as on
  the CPU, we only support sorted=False for now.
class_docstr_tok: ["Implements","TopKOp","on","gpu","Currently","the","output","seem","sorted",",","but","we","do","not","test","it",".","So","as","on","the","CPU",",","we","only","support","sorted=False","for","now","."]
code: def c_code(self, node, nodename, inps, outs, sub):
      context = node.inputs[0].type.context
      if context.kind != b"cuda":
          raise NotImplementedError(
              "%s: We only have CUDA "
              "implementation so far." % self.__class__.__name__
          )
      x, k = inps
      inp_dtc = ga.dtype_to_typecode(node.inputs[0].dtype)
      if not self.return_indices:
          (yv,) = outs
      elif self.return_values:
          yv, yi = outs
      else:
          (yi,) = outs
      out_dtype_s = self.idx_dtype
      out_dtc = ga.dtype_to_typecode(out_dtype_s)
      fail = sub["fail"]
      ctx = sub["params"]
      k_dtype = node.inputs[1].type.dtype_specs()[1]
      # max threads per block
      MAX_TPB = context.maxlsize0
      # max blocks per grid
      MAX_BPG = context.maxgsize0
      WARP_SIZE = 32
  
      ndim = node.inputs[0].ndim
      reordered_axes = list(range(ndim))
      axis = self.axis % ndim
      del reordered_axes[axis]
      reordered_axes = [axis] + reordered_axes
      dims = "".join("dims[%d], " % i for i in reordered_axes[1:])
      prep_output = ""
      if self.return_values:
          def_dvstrides = (
              "const ssize_t *dvstrides = PyGpuArray_STRIDES(%s)" % yv
          )
          params_dv = "%s->ga.data, %s->ga.offset,\n" % (yv, yv)
          params_dv += "".join(
              "dvstrides[%d], " % i for i in reordered_axes
          )
          prep_output += (
              """
      if (0 != theano_prep_output(
          &%(yv)s, %(ndim)d, odims,
          %(inp_dtc)s, GA_C_ORDER, %(ctx)s)) {
          %(fail)s;
      }\n"""
              % locals()
          )
      else:
          def_dvstrides = params_dv = ""
  
      if self.return_indices:
          def_distrides = (
              "const ssize_t *distrides = PyGpuArray_STRIDES(%s)" % yi
          )
          params_di = "%s->ga.data, %s->ga.offset,\n" % (yi, yi)
          params_di += "".join(
              "distrides[%d], " % i for i in reordered_axes
          )
          prep_output += (
              """
      if (0 != theano_prep_output(
          &%(yi)s, %(ndim)d, odims,
          %(out_dtc)s, GA_C_ORDER, %(ctx)s)) {
          %(fail)s;
      }\n"""
              % locals()
          )
      else:
          def_distrides = params_di = ""
      sstrides = ", ".join("sstrides[%d]" % i for i in reordered_axes)
      code = """
  {
      const ssize_t k_ = ((%(k_dtype)s*)(PyArray_DATA(%(k)s)))[0];
      const size_t *dims = PyGpuArray_DIMS(%(x)s);
      size_t odims[%(ndim)d];
      for (int i=0; i<%(ndim)d; i++)
          odims[i] = dims[i];
  
      odims[%(axis)d] = k_>=0 ? k_ : -k_;
  
      if (0 == odims[%(axis)d]) {
          PyErr_SetString(
              PyExc_ValueError,
              "topk: kth must not be zero");
          %(fail)s;
      } else if (dims[%(axis)d] < odims[%(axis)d]) {
          PyErr_SetString(
              PyExc_ValueError,
              "topk: kth cannot be larger than the size of specified axis %(axis)d");
          %(fail)s;
      }
      %(prep_output)s
  
      size_t grid_size=1, block_size=1;
      for (int i=0; i<%(ndim)d; ++i) {
          if (i!=%(axis)d)
              grid_size *= dims[i];
          else
              block_size = dims[i];
      }
      // round up to multiples of warp size
      block_size = ((block_size + %(WARP_SIZE)d - 1) / %(WARP_SIZE)d) * %(WARP_SIZE)d;
  
      if (grid_size > %(MAX_BPG)d) {
          PyErr_SetString(
              PyExc_ValueError,
              "topk: too many slices to work with, expected <= %(MAX_BPG)d");
          %(fail)s;
      }
  
      %(def_dvstrides)s;
      %(def_distrides)s;
      const ssize_t *sstrides = PyGpuArray_STRIDES(%(x)s);
  
      int err;
      if (dims[%(axis)d] > (1u << 31)) {
          block_size = %(MAX_TPB)d;
          err = k_topk_dense_xlarge_call(
                  1, &grid_size, &block_size, 0,
                  %(dims)s
                  %(params_dv)s
                  %(params_di)s
                  k_,
                  %(x)s->ga.data,
                  %(x)s->ga.offset,
                  %(sstrides)s,
                  dims[%(axis)d]
          );
      } else if (block_size > %(MAX_TPB)d) {
          block_size = %(MAX_TPB)d;
          err = k_topk_dense_large_call(
                  1, &grid_size, &block_size, 0,
                  %(dims)s
                  %(params_dv)s
                  %(params_di)s
                  k_,
                  %(x)s->ga.data,
                  %(x)s->ga.offset,
                  %(sstrides)s,
                  dims[%(axis)d]
          );
      } else {
          err = k_topk_dense_call(
                  1, &grid_size, &block_size, 0,
                  %(dims)s
                  %(params_dv)s
                  %(params_di)s
                  k_,
                  %(x)s->ga.data,
                  %(x)s->ga.offset,
                  %(sstrides)s,
                  dims[%(axis)d]
          );
      }
      if (err != GA_NO_ERROR) {
          PyErr_SetString(
              PyExc_RuntimeError,
              "topk: gpu kernel failed to execute");
          %(fail)s;
      }
  }
          """
      return code % locals()
  
imports: from __future__ import absolute_import, print_function, division
  import os
  from string import Template
  import numpy
  import theano
  from theano import Apply
  from theano.tensor import as_tensor_variable
  from theano.tensor.sort import TopKOp
  from .basic_ops import GpuKernelBase, Kernel, infer_context_name, as_gpuarray_variable, gpuarray_helper_inc_dir
  from .opt import register_opt, op_lifter, register_opt2
  from .type import GpuArrayType
label_desc: Use image node_id 7 for calling the GpuTopKOp obj's underlying member method code with example usage: obj.c_code(node, nodename, inps, outs, sub) and returns: unknown