code: def _check_grad(
      fprop,
      xs,
      expected_gxs,
      gys=None,
      backprop_id=None,
      xs_indices=None,
      ys_indices=None,
      grad_outputs=[],
      set_grad=False,
      retain_grad=False,
  ):
      # Checks for test validity.
      assert callable(fprop)
      assert isinstance(xs, tuple)
      assert isinstance(expected_gxs, tuple)
      assert all([isinstance(a, chainerx.ndarray) for a in xs])
      assert all(
          [
              isinstance(a, chainerx.ndarray) or a is None
              for a in expected_gxs
          ]
      )
  
      # Forward.
      ys = fprop(*xs)
  
      # Set output gradients.
      if gys is not None:
          assert len(gys) == len(ys)
          for y, gy in zip(ys, gys):
              assert not y.is_grad_required()
              y.set_grad(gy, backprop_id)
  
      # Backward using grad.
      initial_gxs = [
          x.get_grad(backprop_id)
          if x.is_grad_required(backprop_id)
          else chainerx.ChainerxError
          for x in xs
      ]
  
      if xs_indices is not None:
          actual_xs = tuple([xs[i] for i in xs_indices])
          assert len(actual_xs) == len(expected_gxs)
      else:
          actual_xs = xs
      if ys_indices is not None:
          actual_ys = tuple([ys[i] for i in ys_indices])
      else:
          actual_ys = ys
      gxs = chainerx.grad(
          actual_ys,
          actual_xs,
          backprop_id,
          grad_outputs=grad_outputs,
          set_grad=set_grad,
          retain_grad=retain_grad,
      )
  
      # Check gradients.
      for gx, expected_gx in zip(gxs, expected_gxs):
          _assert_arrays_equal(gx, expected_gx)
  
      # Check gradients of output arrays.
      if gys is None:
          gys = (None,) * len(xs)
      for y, gy in zip(ys, gys):
          if gy is None:
              assert not y.is_grad_required(backprop_id)
              with pytest.raises(chainerx.ChainerxError):
                  y.get_grad(backprop_id)
          else:
              assert y.is_grad_required(backprop_id)
              _assert_arrays_equal(gy, y.get_grad(backprop_id))
  
      if set_grad:
          for x, gx in zip(xs, gxs):
              _assert_arrays_equal(gx, x.get_grad(backprop_id))
      else:
          # Check initial gradients of inputs and that they are not modified.
          for x, initial_gx in zip(xs, initial_gxs):
              if initial_gx is chainerx.ChainerxError:
                  assert not x.is_grad_required(backprop_id)
                  with pytest.raises(chainerx.ChainerxError):
                      x.get_grad(backprop_id)
              else:
                  assert x.is_grad_required(backprop_id)
                  _assert_arrays_equal(
                      initial_gx, x.get_grad(backprop_id)
                  )
  
imports: import chainerx
  import pytest
label_desc: Use image node_id 3 for calling a global function with example usage: _check_grad(fprop, xs, expected_gxs, gys, backprop_id, xs_indices, ys_indices, grad_outputs, set_grad, retain_grad) without return types