# Buggy code
def csrmv(a, x, y=None, alpha=1, beta=0, transa=False):
    """Matrix-vector product for a CSR-matrix and a dense vector.

    .. math::

       y = \\alpha * o_a(A) x + \\beta y,

    where :math:`o_a` is a transpose function when ``transa`` is ``True`` and
    is an identity function otherwise.

    Args:
        a (cupy.cusparse.csr_matrix): Matrix A.
        x (cupy.ndarray): Vector x.
        y (cupy.ndarray or None): Vector y. It must be F-contiguous.
        alpha (float): Coefficient for x.
        beta (float): Coefficient for y.
        transa (bool): If ``True``, transpose of ``A`` is used.

    Returns:
        cupy.ndarray: Calculated ``y``.

    """
    if a.shape[1] != len(x):
        raise ValueError("dimension mismatch")
    assert y is None or y.flags.f_contiguous

    a_shape = a.shape if not transa else a.shape[::-1]

    handle = device.get_cusparse_handle()
    m, n = a_shape
    a, x, y = _cast_common_type(a, x, y)
    dtype = a.dtype
    if y is None:
        y = cupy.zeros(m, dtype)
    alpha = numpy.array(alpha, dtype).ctypes
    beta = numpy.array(beta, dtype).ctypes
    _call_cusparse(
        "csrmv",
        dtype,
        handle,
        _transpose_flag(transa),
        m,
        n,
        a.nnz,
        alpha.data,
        a._descr.descriptor,
        a.data.data.ptr,
        a.indptr.data.ptr,
        a.indices.data.ptr,
        x.data.ptr,
        beta.data,
        y.data.ptr,
    )

    return y


# Traceback (cupy.cuda.cusparse.CuSparseError)
Traceback (most recent call last):
File "main.py", line 23, in <module>
main()
File "main.py", line 20, in main
gx = cupy.cusparse.csrmm2(W, y.T, transa=True).T
File "/home/tommi/.pyenv/versions/cupy-dev/lib/python3.4/site-packages/cupy-2.0.0rc1-py3.4-linux-x86_64.egg/cupy/cusparse.py", line 205, in csrmm2
b.data.ptr, ldb, beta.data, c.data.ptr, ldc)
File "/home/tommi/.pyenv/versions/cupy-dev/lib/python3.4/site-packages/cupy-2.0.0rc1-py3.4-linux-x86_64.egg/cupy/cusparse.py", line 52, in _call_cusparse
return f(*args)
File "cupy/cuda/cusparse.pyx", line 347, in cupy.cuda.cusparse.scsrmm2
File "cupy/cuda/cusparse.pyx", line 357, in cupy.cuda.cusparse.scsrmm2
File "cupy/cuda/cusparse.pyx", line 229, in cupy.cuda.cusparse.check_status
cupy.cuda.cusparse.CuSparseError: CUSPARSE_STATUS_INVALID_VALUE

# Fixed code
def csrmv(a, x, y=None, alpha=1, beta=0, transa=False):
    """Matrix-vector product for a CSR-matrix and a dense vector.

    .. math::

       y = \\alpha * o_a(A) x + \\beta y,

    where :math:`o_a` is a transpose function when ``transa`` is ``True`` and
    is an identity function otherwise.

    Args:
        a (cupy.cusparse.csr_matrix): Matrix A.
        x (cupy.ndarray): Vector x.
        y (cupy.ndarray or None): Vector y. It must be F-contiguous.
        alpha (float): Coefficient for x.
        beta (float): Coefficient for y.
        transa (bool): If ``True``, transpose of ``A`` is used.

    Returns:
        cupy.ndarray: Calculated ``y``.

    """
    assert y is None or y.flags.f_contiguous

    a_shape = a.shape if not transa else a.shape[::-1]
    if a_shape[1] != len(x):
        raise ValueError("dimension mismatch")

    handle = device.get_cusparse_handle()
    m, n = a_shape
    a, x, y = _cast_common_type(a, x, y)
    dtype = a.dtype
    if y is None:
        y = cupy.zeros(m, dtype)
    alpha = numpy.array(alpha, dtype).ctypes
    beta = numpy.array(beta, dtype).ctypes
    _call_cusparse(
        "csrmv",
        dtype,
        handle,
        _transpose_flag(transa),
        a.shape[0],
        a.shape[1],
        a.nnz,
        alpha.data,
        a._descr.descriptor,
        a.data.data.ptr,
        a.indptr.data.ptr,
        a.indices.data.ptr,
        x.data.ptr,
        beta.data,
        y.data.ptr,
    )

    return y