code: def test_grad(self):
      x = tensor.vector()
      np_x = np.random.randn(7).astype(theano.config.floatX)
  
      # offset = 0 case:
      mtx_x = GpuAllocDiag()(x)
      sum_mtx_x = tensor.sum(mtx_x)
      grad_x = tensor.grad(sum_mtx_x, x)
      grad_mtx_x = tensor.grad(sum_mtx_x, mtx_x)
  
      fn_grad_x = theano.function([x], grad_x, mode=mode_with_gpu)
      fn_grad_mtx_x = theano.function(
          [x], grad_mtx_x, mode=mode_with_gpu
      )
  
      computed_grad_x = fn_grad_x(np_x)
      computed_grad_mtx_x = fn_grad_mtx_x(np_x)
      true_grad_x = np.diagonal(computed_grad_mtx_x, 0)
      assert np.allclose(computed_grad_x, true_grad_x)
  
      # offset > 0 case:
      mtx_x = GpuAllocDiag(2)(x)
      sum_mtx_x = tensor.sum(mtx_x)
      grad_x = tensor.grad(sum_mtx_x, x)
      grad_mtx_x = tensor.grad(sum_mtx_x, mtx_x)
  
      fn_grad_x = theano.function([x], grad_x, mode=mode_with_gpu)
      fn_grad_mtx_x = theano.function(
          [x], grad_mtx_x, mode=mode_with_gpu
      )
  
      computed_grad_x = fn_grad_x(np_x)
      computed_grad_mtx_x = fn_grad_mtx_x(np_x)
      true_grad_x = np.diagonal(computed_grad_mtx_x, 2)
      assert np.allclose(computed_grad_x, true_grad_x)
  
      # offset < 0 case:
      mtx_x = GpuAllocDiag(-3)(x)
      sum_mtx_x = tensor.sum(mtx_x)
      grad_x = tensor.grad(sum_mtx_x, x)
      grad_mtx_x = tensor.grad(sum_mtx_x, mtx_x)
  
      fn_grad_x = theano.function([x], grad_x, mode=mode_with_gpu)
      fn_grad_mtx_x = theano.function(
          [x], grad_mtx_x, mode=mode_with_gpu
      )
  
      computed_grad_x = fn_grad_x(np_x)
      computed_grad_mtx_x = fn_grad_mtx_x(np_x)
      true_grad_x = np.diagonal(computed_grad_mtx_x, -3)
      assert np.allclose(computed_grad_x, true_grad_x)
  
imports: from __future__ import absolute_import, print_function, division
  import numpy
  import unittest
  import theano
  from theano import tensor
  from theano.compile import DeepCopyOp
  from theano.tensor.tests import test_subtensor, test_basic
  from theano.tests import unittest_tools
  from ..basic_ops import HostFromGpu, GpuFromHost, GpuContiguous
  from ..elemwise import GpuDimShuffle
  from ..subtensor import GpuIncSubtensor, GpuSubtensor, GpuAdvancedSubtensor1, GpuAdvancedSubtensor, GpuAdvancedBooleanSubtensor, GpuAdvancedIncSubtensor, GpuAdvancedIncSubtensor1, GpuAdvancedIncSubtensor1_dev20, GpuExtractDiag, GpuAllocDiag
  from ..type import gpuarray_shared_constructor
  from .config import mode_with_gpu, test_ctx_name
label_desc: Use image node_id 3 for calling the test_gpuallocdiag obj's underlying member method code with example usage: obj.test_grad() without return types