da1729's Blog

cryptography, digital design, embedded, rf, ...

In Memory Computation using Analog Part 2

Matrix Multiplication through MAC operations

Below, I have presented a python code, illustrating matrix multiplication using MAC operation. But, why matrix multiplication only? Because everything is a fking MATRIX!!! (that’s why the film is called Matrix). Physicists, electrical engineers, computer scientists/engineers just love representing everything in matrix, and why not, they make everything more streamlined and easy to represent. Since, we are representing everything in matrices, especially in machine learning and AI, like we have the weights matrices, input vectors, output vectors, etc., we have to do a lot of matrix multiplication and in hardware, using MAC operators, we can easily perform it. Now, carefully look and understand the python code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import numpy as np

def matrix_multiply_mac(A, B):

A = np.array(A)
B = np.array(B)

if A.shape[1] != B.shape[0]:
raise ValueError("Matrix dimensions do not match for multiplication.")

C = np.zeros((A.shape[0], B.shape[1]))

# Can you explicitly see me using the MAC operation here? what is the accumulator?
for i in range(A.shape[0]):
for j in range(B.shape[1]):
mac = 0
for k in range(A.shape[1]):
mac += A[i][k] * B[k][j]
C[i][j] = mac

return C

A = [[1, 2, 3], [4, 5, 6]]
B = [[7, 8], [9, 10], [11, 12]]
result = matrix_multiply_mac(A, B)

print("Resultant Matrix:")
print(result)

Now, that (I hope) you have read and understood the code above, one can realize that we can use the circuit we designed in the previous part for the same operation. Hence, we can do matrix multiplication through analog computing now, how cool!

But why should we go for analog rather than digital? In digital, the energy complexity grows a lot faster as the number of bits are increased, speaking with numbers, an 8-bit MAC energy can be 100 times the energy for 1 bit.

Let’s end this part here for now, as I wrote this very impulsively out a sudden motivation (and too keep the momentum going) and did not plan it too much before writing LMAO.

peace. da1729

References

[1] J. -s. Seo et al., “Digital Versus Analog Artificial Intelligence Accelerators: Advances, trends, and emerging designs,” in IEEE Solid-State Circuits Magazine, vol. 14, no. 3, pp. 65-79, Summer 2022, doi: 10.1109/MSSC.2022.3182935.
keywords: {AI accelerators;Market research;In-memory computing;Hardware;System analysis and design;Switching circuits},