Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix for batched gemv #2481

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/cublas/wrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,16 @@ for (fname, fname_64, eltyin, eltyout) in (
if length(A) != length(x) || length(A) != length(y)
throw(DimensionMismatch("Lengths of inputs must be the same"))
end
m = size(A[1], 1)
n = size(A[1], 2)
for (i, (As,xs,ys)) in enumerate(zip(A,x,y))
m,n = size(As)
if size(As) != (m, n)
throw(DimensionMismatch("A[$i] has different dimension from A[1]. Dimensions between A's should be identical."))
end
if length(xs) != (trans == 'N' ? n : m) || length(ys) != (trans == 'N' ? m : n)
throw(DimensionMismatch("Input $i: A has dimension $(size(As)), x has dimension $(size(xs)), y has dimension $(size(ys))"))
end
end

m = size(A[1], trans == 'N' ? 1 : 2)
n = size(A[1], trans == 'N' ? 2 : 1)
lda = max(1,stride(A[1],2))
incx = stride(x[1],1)
incy = stride(y[1],1)
Expand Down Expand Up @@ -462,9 +463,9 @@ for (fname, fname_64, eltyin, eltyout) in (
if size(A, 3) != size(x, 2) || size(A, 3) != size(y, 2)
throw(DimensionMismatch("Batch sizes must be equal for all inputs"))
end
m = size(A, trans == 'N' ? 1 : 2)
n = size(A, trans == 'N' ? 2 : 1)
if m != size(y, 1) || n != size(x, 1)
m = size(A, 1)
n = size(A, 2)
if size(y, 1) != (trans == 'N' ? m : n) || size(x, 1) != (trans == 'N' ? n : m)
throw(DimensionMismatch("A has dimension $(size(A)), x has dimension $(size(x)), y has dimension $(size(y))"))
end

Expand Down
23 changes: 22 additions & 1 deletion test/libraries/cublas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ end
@test testf(*, rand(elty, m, n)', rand(elty, m))
x = rand(elty, m)
A = rand(elty, m, m + 1 )
y = rand(elty, m)
y = rand(elty, n)
dx = CuArray(x)
dA = CuArray(A)
dy = CuArray(y)
Expand All @@ -124,6 +124,10 @@ end
dy = CUBLAS.gemv('N', dA, dx)
hy = collect(dy)
@test hy ≈ A * x
dy = CuArray(y)
dx = CUBLAS.gemv(elty <: Real ? 'T' : 'C', alpha, dA, dy)
hx = collect(dx)
@test hx ≈ alpha * A' * y
end

if CUBLAS.version() >= v"11.9"
Expand All @@ -150,6 +154,16 @@ end
y[i] = alpha * A[i] * x[i] + beta * y[i]
@test y[i] ≈ hy
end
dy = CuArray{elty, 1}[]
for i=1:length(A)
push!(dy, CuArray(y[i]))
end
CUBLAS.gemv_batched!(elty <: Real ? 'T' : 'C', alpha, dA, dy, beta, dx)
for i=1:size(A, 3)
hx = collect(dx[i])
x[i] = alpha * A[i]' * y[i] + beta * x[i]
@test x[i] ≈ hx
end
end
end

Expand All @@ -173,6 +187,13 @@ end
y[:, i] = alpha * A[:, :, i] * x[:, i] + beta * y[:, i]
@test y[:, i] ≈ hy
end
dy = CuArray(y)
CUBLAS.gemv_strided_batched!(elty <: Real ? 'T' : 'C', alpha, dA, dy, beta, dx)
for i=1:size(A, 3)
hx = collect(dx[:, i])
x[:, i] = alpha * A[:, :, i]' * y[:, i] + beta * x[:, i]
@test x[:, i] ≈ hx
end
end
end

Expand Down