def fpga_single_run(self, input): input = input.reshape(self.ishape_normal) input = MT.multithreshold(input, self.mt_node_thresholds) assert input.shape == self.ishape_normal ibuf_folded = input.reshape(self.ishape_folded) # pack the input buffer, reversing both SIMD dim and endianness ibuf_packed = finnpy_to_packed_bytearray(ibuf_folded, self.idt, reverse_endian=True, reverse_inner=True) # copy the packed data into the PYNQ buffer # TODO optimization: pack directly into the PYNQ buffer? np.copyto(self.ibuf_packed_device, ibuf_packed) # set up the DMA and wait until all transfers complete self.dma.sendchannel.transfer(self.ibuf_packed_device) self.dma.recvchannel.transfer(self.obuf_packed) self.dma.sendchannel.wait() self.dma.recvchannel.wait() # unpack the packed output buffer from accelerator obuf_folded = packed_bytearray_to_finnpy(self.obuf_packed, self.odt, self.oshape_folded, reverse_endian=True, reverse_inner=True) obuf_normal = obuf_folded.reshape(self.oshape_normal) obuf_normal = obuf_normal * self.multiply_node_const obuf_normal = obuf_normal + self.add_node_mat return obuf_normal
def unpack_output(self, obuf_packed): """Unpacks the packed output buffer from accelerator. Gets packed output and returns output data in folded shape.""" obuf_folded = packed_bytearray_to_finnpy( obuf_packed, self.odt, self.oshape_folded, reverse_endian=True, reverse_inner=True ) return obuf_folded
def test_packed_bytearray_to_finnpy(): A = np.asarray([[14], [6]], dtype=np.uint8) eA = [[1, 1, 1, 0], [0, 1, 1, 0]] eA = np.asarray(eA, dtype=np.float32) shapeA = eA.shape assert (packed_bytearray_to_finnpy(A, DataType.BINARY, shapeA) == eA).all() B = np.asarray([[[15], [15]], [[7], [13]]], dtype=np.uint8) eB = [[[3, 3], [3, 3]], [[1, 3], [3, 1]]] eB = np.asarray(eB, dtype=np.float32) shapeB = eB.shape assert (packed_bytearray_to_finnpy(B, DataType.UINT2, shapeB) == eB).all() C = np.asarray([23, 37], dtype=np.uint8) eC = [1, 7, 2, 5] eC = np.asarray(eC, dtype=np.float32) shapeC = eC.shape assert (packed_bytearray_to_finnpy(C, DataType.UINT4, shapeC) == eC).all() D = np.asarray([[23, 37], [37, 23]], dtype=np.uint8) eD = [[1, 7, 2, 5], [2, 5, 1, 7]] eD = np.asarray(eD, dtype=np.float32) shapeD = eD.shape assert (packed_bytearray_to_finnpy(D, DataType.UINT4, shapeD) == eD).all() E = np.asarray( [[ 255, 255, 255, 252, 0, 0, 0, 0, 255, 255, 255, 252, 255, 255, 255, 252 ]], dtype=np.uint8, ) eE = [[-4, 0, -4, -4]] eE = np.asarray(eE, dtype=np.float32) shapeE = eE.shape assert (packed_bytearray_to_finnpy(E, DataType.INT32, shapeE) == eE).all() F = np.asarray( [[ 252, 255, 255, 255, 0, 0, 0, 0, 252, 255, 255, 255, 252, 255, 255, 255 ]], dtype=np.uint8, ) eF = [[-4, 0, -4, -4]] eF = np.asarray(eE, dtype=np.float32) shapeF = eF.shape assert (packed_bytearray_to_finnpy(F, DataType.INT32, shapeF, reverse_inner=True, reverse_endian=True) == eF).all()
def test_packed_bytearray_to_finnpy(): A = np.asarray([[14], [6]], dtype=np.uint8) eA = [[1, 1, 1, 0], [0, 1, 1, 0]] eA = np.asarray(eA, dtype=np.float32) shapeA = eA.shape assert (packed_bytearray_to_finnpy(A, DataType["BINARY"], shapeA) == eA).all() B = np.asarray([[[15], [15]], [[7], [13]]], dtype=np.uint8) eB = [[[3, 3], [3, 3]], [[1, 3], [3, 1]]] eB = np.asarray(eB, dtype=np.float32) shapeB = eB.shape assert (packed_bytearray_to_finnpy(B, DataType["UINT2"], shapeB) == eB).all() C = np.asarray([23, 37], dtype=np.uint8) eC = [1, 7, 2, 5] eC = np.asarray(eC, dtype=np.float32) shapeC = eC.shape assert (packed_bytearray_to_finnpy(C, DataType["UINT4"], shapeC) == eC).all() D = np.asarray([[23, 37], [37, 23]], dtype=np.uint8) eD = [[1, 7, 2, 5], [2, 5, 1, 7]] eD = np.asarray(eD, dtype=np.float32) shapeD = eD.shape assert (packed_bytearray_to_finnpy(D, DataType["UINT4"], shapeD) == eD).all() E = np.asarray( [[ 255, 255, 255, 252, 0, 0, 0, 0, 255, 255, 255, 252, 255, 255, 255, 252 ]], dtype=np.uint8, ) eE = [[-4, 0, -4, -4]] eE = np.asarray(eE, dtype=np.float32) shapeE = eE.shape assert (packed_bytearray_to_finnpy(E, DataType["INT32"], shapeE) == eE).all() F = np.asarray( [[ 252, 255, 255, 255, 0, 0, 0, 0, 252, 255, 255, 255, 252, 255, 255, 255 ]], dtype=np.uint8, ) eF = [[-4, 0, -4, -4]] eF = np.asarray(eE, dtype=np.float32) shapeF = eF.shape assert (packed_bytearray_to_finnpy(F, DataType["INT32"], shapeF, reverse_inner=True, reverse_endian=True) == eF).all() G = np.asarray([[1, 19, 240], [3, 200, 90]], dtype=np.uint8) eG = [[17.125, -2.0], [-3.5, 11.25]] eG = np.asarray(eG, dtype=np.float32) shapeG = eG.shape assert (packed_bytearray_to_finnpy(G, DataType["FIXED<9,6>"], shapeG) == eG).all() H = np.asarray( [[65, 137, 0, 0, 192, 0, 0, 0], [192, 96, 0, 0, 65, 52, 0, 0]], dtype=np.uint8) assert (packed_bytearray_to_finnpy(H, DataType["FLOAT32"], shapeG) == eG).all()