0

I have cythonized the following file that uses numpy's matrix multiplication:

def cell(float[:, ::1] a, float[:, ::1] b):
  c = a @ b
  return c

However, when I call it with:

from matmul import cell
import numpy as np


a = np.zeros((1, 64), dtype=np.float32)
b = np.zeros((64, 64), dtype=np.float32)
c = cell(a, b)

I get the following error:

TypeError: unsupported operand type(s) for @: _memoryviewslice and _memoryviewslice

How can I perform matrix multiplication with Cython?

Context: the function "cell" is part of a code I wrote that performs a prediction by an LSTM network (I wrote it manually, without using PyTorch or Tensorflow, just NumPy). I need to speed up the code to be able to use the network in real-time.

1 Answers1

0

If that's all you're doing there's literally no point in adding the types for the argument of cell - all you're doing is adding expensive type-checks for no reason. Cython can't make useful use of these types. Just leave a and b untyped.

If you do actually need to fix memoryviews operations with Numpy whole-array operations the easiest solution is to call np.asarray

def cell(float[:, ::1] a, float[:, ::1] b):
  c = np.asarray(a) @ np.asarray(b)
  return c

You aren't getting any benefit from Cython here - it's just calling into the Numpy matrix multiply code. So only do this where you need to mix it with some operations where you do benefit from Cython.

DavidW
  • 29,336
  • 6
  • 55
  • 86
  • Thank you very much for the comprehensive answer. I chose the first solution and did not type the parameters. Still, I was doing this to speed up a function and I am not being able to push it as far as I need. If you want to visit the question I just posted about this (https://stackoverflow.com/questions/73873398/make-an-lstm-prediction-written-in-numpy-faster), your help would be much appreciated! – Gustavo Caetano Sep 27 '22 at 20:26
  • I think that question is probably much more detailed than I really want to go into, but you might find https://stackoverflow.com/a/70821945/ helpful as general advice about where Cython helps – DavidW Sep 27 '22 at 20:55