File size: 4,467 Bytes
54c34c9 | 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | import numpy as np
from pynq import allocate
from pynq import MMIO
A_SIZE = 25
in_F_max_size = 4000 * A_SIZE
in_W_max_size = 4000 * A_SIZE
out_F_max_size = 4000 * A_SIZE
in_F_width_max = 64 * A_SIZE
in_W_width_max = 64 * A_SIZE
shift_max = 32
def mat_create(shape, data_type = np.int8):
height = shape[0]
width = shape[1]
if height % A_SIZE != 0:
An_h = height + A_SIZE - height % A_SIZE
else:
An_h = height
if width % A_SIZE != 0:
An_w = width+ A_SIZE - width % A_SIZE
else:
An_w = width
A = allocate(shape=(An_h,An_w), dtype=data_type)
return [A[0:height,0:width], A]
def mat_setValue(A,B:np.ndarray): #use numpy array to set value
A[0][:] = B
def mat_getNdarray(A):
B = np.zeros((A[0].shape[0],A[0].shape[1]))
B = A[0].copy()
return B
def mat_delete(A):
A[1].freebuffer()
def mat_print(A):
print(A[0])
def mat_mul_soft(A,B,C,shift):
C0 = A[0].astype(np.int32) @ B[0].astype(np.int32)
C0 = np.right_shift(C0,shift)
C0 = np.clip(C0,-128,127)
C[0][:] = C0
def ini_MM():
global MM_ultra
global in_f_dma
global in_w_dma
global out_f_dma
MM_ultra_addr= 0xA0030000
MM_ultra_addr_range = 0xFFF
MM_ultra = MMIO(MM_ultra_addr, MM_ultra_addr_range)
global XAXIDMA_IDLE_MASK
XAXIDMA_IDLE_MASK = 0x00000002
IN_FEATURE_DMA_ADDR = 0xA0000000
in_f_range = 0x10000
in_f_dma = MMIO(IN_FEATURE_DMA_ADDR, in_f_range)
IN_WEIGHT_DMA_ADDR = 0xA0010000
in_w_range = 0x10000
in_w_dma = MMIO(IN_WEIGHT_DMA_ADDR, in_w_range)
OUT_FEATURE_DMA_ADDR = 0xA0020000
out_f_range = 0x10000
out_f_dma = MMIO(OUT_FEATURE_DMA_ADDR, out_f_range)
def in_feature_transfer(array, start_offset = 0, len = 0):
start_addr = array.physical_address + start_offset
if len == 0:
len = array.nbytes
array.flush()
in_f_dma.write(0x0,0x4) #reset
in_f_dma.write(0x18,start_addr)
in_f_dma.write(0x0,0x1) #open channel
in_f_dma.write(0x28,len)
def in_weight_transfer(array, start_offset = 0, len = 0):
start_addr = array.physical_address + start_offset
if len == 0:
len = array.nbytes
array.flush()
in_w_dma.write(0x0,0x4) #reset
in_w_dma.write(0x18,start_addr)
in_w_dma.write(0x0,0x1) #open channel
in_w_dma.write(0x28,len)
def out_feature_transfer(array, start_offset = 0, len = 0):
start_addr = array.physical_address + start_offset
if len == 0:
len = array.nbytes
out_f_dma.write(0x30,0x4) #reset
out_f_dma.write(0x48,start_addr)
out_f_dma.write(0x30,0x1) #open channel
out_f_dma.write(0x58,len)
array.invalidate()
def out_feature_wait():
while False if out_f_dma.read(0x34) & XAXIDMA_IDLE_MASK else True:
pass
def in_feature_wait():
while False if in_f_dma.read(0x4) & XAXIDMA_IDLE_MASK else True:
pass
def in_weight_wait():
while False if in_w_dma.read(0x4) & XAXIDMA_IDLE_MASK else True:
pass
def mat_mul(A, B, C,shift=0):
A = A[1]
B = B[1]
C = C[1]
A_h = A.shape[0]
A_w = A.shape[1]
B_h = B.shape[0]
B_w = B.shape[1]
if A_h == 1:
print("\033[31mThe height of matrix A can not be 1\033[0m")
return
if A_w > in_F_width_max:
print("\033[31mThe width of matrix A is too large\033[0m")
return
if A_w != B_h:
print("\033[31mThe width of matrix A is not equal to the height of matrix B\033[0m")
return
if A_w % A_SIZE != 0 :
print("\033[31mThe width of matrix A is not the integer multiple of A_SIZE\033[0m")
return
if B_w % A_SIZE != 0 :
print("\033[31mThe width of matrix B is not the integer multiple of A_SIZE\033[0m")
return
if A.nbytes > in_F_max_size:
print("\033[31mThe size of matrix A is too large\033[0m")
return
if B.nbytes > in_W_max_size:
print("\033[31mThe size of matrix B is too large\033[0m")
return
if C.nbytes > in_W_max_size:
print("\033[31mThe size of matrix C is too large\033[0m")
return
F_width_block_num = int(A_w / A_SIZE)
W_width_block_num = int(B_w /A_SIZE)
MM_ultra.write(0x0,shift)
MM_ultra.write(0x4,A_h)
MM_ultra.write(0x8,F_width_block_num)
MM_ultra.write(0xc,W_width_block_num)
out_feature_transfer(C)
in_feature_transfer(A)
in_weight_transfer(B)
out_feature_wait() |