[SOLVED] (M,N)x(N,1) Matrix-Matrix Multiplication vs. Matrix-Vector Mult


#1

Hello, I’m looking into how TVM can optimize matrix-vector multiplication and 2D-convolutions on FPGAs. Starting with the optimizing GEMM tutorial provided, I was able to achieve basic vector-matrix multiplication by having the size of the matrices be (M,N) and (N,1). I’m now trying to mimic this behavior only using a (M,N) matrix and a (N) vector but I keep getting a syntax error at

func(a,b,c)

I’m sure its something basic in TVM I’m misunderstanding but any help would be appreciated! Here is the code below

(I just want to get the vector-matrix mult working before targeting the FPGA)

import tvm
import numpy

K = 1024

dtype = "float32"

target = 'llvm'
ctx = tvm.context(target, 0)

a = tvm.nd.array(numpy.random.rand(K,).astype(dtype), ctx)
b = tvm.nd.array(numpy.random.rand(K,K).astype(dtype), ctx)

answer = numpy.dot(b.asnumpy(), a.asnumpy())

print(answer)

k = tvm.reduce_axis((0,K), 'k')

A = tvm.placeholder((K,), name = 'A')
B = tvm.placeholder((K,K), name = 'B')
C = tvm.compute(A.shape, lambda x: tvm.sum(B[x,k] * A[k], axis = k), name='C')

s = tvm.create_schedule(C.op)
func = tvm.build(s, [A, B, C], target=target, name='vmmult')

c = tvm.nd.array(numpy.zeros((K,),dtype=dtype, ctx)
func(a, b, c)
tvm.testing.assert_allclose(c.asnumpy(), answer, rtol=1e-5)

#2

It’s a Python syntax error, nothing to do with TVM. Apply this diff:

--- before.py        2019-03-22 18:22:14.000000000 -0700
+++ after.py   2019-03-22 18:21:57.000000000 -0700
@@ -24,6 +24,6 @@
 s = tvm.create_schedule(C.op)
 func = tvm.build(s, [A, B, C], target=target, name='vmmult')

-c = tvm.nd.array(numpy.zeros((K,),dtype=dtype, ctx)
+c = tvm.nd.array(numpy.zeros((K,), dtype=dtype), ctx)
 func(a, b, c)
 tvm.testing.assert_allclose(c.asnumpy(), answer, rtol=1e-5)