def _TensorArrayWriteGrad(op, flow):
  """Gradient for TensorArrayWrite.

  Args:
    op: Forward TensorArrayWrite op.
    flow: Gradient `Tensor` flow to TensorArrayWrite.

  Returns:
    A grad `Tensor`, the gradient created in an upstream ReadGrad or PackGrad.
  """
  # handle is the output store_handle of TensorArrayReadGrad or
  # the handle output of TensorArrayWriteGrad.  we must use this one.
  handle = op.inputs[0]
  index = op.inputs[1]
  dtype = op.get_attr("T")
  grad_source = _GetGradSource(flow)
  g = (tensor_array_ops.TensorArray(dtype=dtype, handle=handle, flow=flow,
                                    colocate_with_first_write_call=False)
       .grad(source=grad_source, flow=flow))
  grad = g.read(index)
  return [None, None, grad, flow]