lebedov / scikit-cuda

Python interface to GPU-powered libraries
http://scikit-cuda.readthedocs.org/
Other
975 stars 179 forks source link

ValueError with linalg.dot when transa=True #326

Open rileyjmurray opened 2 years ago

rileyjmurray commented 2 years ago

Problem

I want to compute the skcuda equivalent to A.T @ b for numpy arrays (A, b) when A has 2 dimensions and b has one dimension. Here's some simple test data

import numpy as np
import pycuda.gpuarray as gpuarray
import skcuda.linalg as skla

A_gpu = gpuarray.to_gpu(np.ones((3, 2)))
b_gpu = gpuarray.to_gpu(np.ones(3))

I would expect that the function call below produces the desired output. But instead it raises a ValueError:

skla.dot(A_gpu, b_gpu, transa='T')

Traceback (most recent call last):
  File "/home/riley/anaconda3/envs/rla39a/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3441, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-27-3e66e0c2b9c5>", line 1, in <module>
    skla.dot(A_gpu, b_gpu, transa='T')
  File "/home/riley/anaconda3/envs/rla39a/lib/python3.9/site-packages/skcuda/linalg.py", line 1060, in dot
    return out.reshape(out_shape)
  File "/home/riley/anaconda3/envs/rla39a/lib/python3.9/site-packages/pycuda/gpuarray.py", line 912, in reshape
    raise ValueError("total size of new array must be unchanged")
ValueError: total size of new array must be unchanged

The function call below computes the expected result

c_gpu = skla.dot(b_gpu, A_gpu)

Environment

wingkitlee0 commented 2 years ago

It sounds like a bug where the transpose option was not taken into account when out_shape is computed