def call(f, *args, **kwds): reset() kwd_intfs, kwd_params = extract_arg_kwds(kwds, f) args_comb = combine_arg_kwds(args, kwd_intfs, f) paramspec = inspect.getfullargspec(f.func) args, annotations = resolve_args(args_comb, paramspec.args, paramspec.annotations, paramspec.varargs) dtypes = [infer_dtype(args[arg], annotations[arg]) for arg in args] seqs = [drv(t=t, seq=[v]) for t, v in zip(dtypes, args_comb)] outputs = f(*seqs, **kwd_params) if isinstance(outputs, tuple): res = [[] for _ in outputs] for o, r in zip(outputs, res): collect(o | mon, result=r) else: res = [[]] collect(outputs | mon, result=res[0]) sim(check_activity=False) if isinstance(outputs, tuple): return res else: return res[0]
def run_matrix(impl, mat1, mat2, cols_per_row, col_only: bool = False): reg['trace/level'] = 0 reg['gear/memoize'] = False # Add one more dimension to the matrix to support input type for design mat1 = mat1.reshape(1, mat1.shape[0], mat1.shape[1]) mat2 = mat2.reshape(mat2.shape[0], 1, mat2.shape[1]) # configuration driving cfg = create_valid_cfg(cols_per_row, mat1) cfg_drv = drv(t=TCfg, seq=[cfg]) row_t = Queue[Array[Int[16], cfg['num_cols']]] mat1_drv = drv(t=Queue[row_t], seq=[mat1]) res_list = [] if col_only: # remove the extra dimension that was previously added since colum mult accepts mat2 = np.squeeze(mat2) # for columtn multiplication second operand needs to be only one row mat2_drv = drv(t=row_t, seq=[mat2]) res = column_multiplication(cfg_drv, mat1_drv, mat2_drv) # column multiplication returns result in a queue so flatening makes it a regular list collect(res | flatten, result=res_list) if impl == 'hw': cosim('/column_multiplication', 'verilator', outdir='/tmp/column_multiplication', rebuild=True, timeout=100) else: mat2_drv = drv(t=Queue[row_t], seq=[mat2]) res = matrix_multiplication(cfg_drv, mat1_drv, mat2_drv, cols_per_row=cols_per_row) collect(res, result=res_list) if impl == 'hw': cosim('/matrix_multiplication', 'verilator', outdir='/tmp/matrix_multiplication', rebuild=True, timeout=100) try: sim() # convert PG results into regular 'int' if col_only: pg_res = [int(el) for el in res_list] else: pg_res = [int(el) for row_chunk in res_list for el in row_chunk] # calculate reference NumPy resutls np_res = np.dot(np.squeeze(mat1), np.transpose(mat2.squeeze())) # reshape PG results into the same format as pg_res = np.array(pg_res).reshape(np_res.shape) sim_assert( np.equal(pg_res, np_res).all(), "Error in compatring results") log.info("\033[92m //==== PASS ====// \033[90m") except: # printing stack trace traceback.print_exc() log.info("\033[91m //==== FAILED ====// \033[90m")
def test_start_stop_step_combined(sim_cls): res = [] qrange((0, 8, 1), sim_cls=sim_cls) | collect(result=res) sim(timeout=16) assert res == [(i, i == 7) for i in range(8)] * 2
import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg @gear def darken(din: Uint[8], *, gain) -> Uint[8]: return din * Ufixp[0, 8](gain) orig_img = (mpimg.imread('../creature.png') * 255).astype(np.uint8) res = [] drv(t=Uint[8], seq=orig_img.flatten()) \ | darken(gain=0.8) \ | int \ | collect(result=res) reg['trace/level'] = 0 sim() res_img = np.array(res, np.uint8) res_img.shape = orig_img.shape ax1 = plt.subplot(1, 2, 1) ax1.imshow(orig_img) ax2 = plt.subplot(1, 2, 2) ax2.imshow(res_img) plt.show()
def matrix_ops_single(): ########################## DESIGN CONTROLS ########################## num_cols = 8 num_rows = 6 # HINT suppoerted all dimesitions > 1 cols_per_row = 2 # HINT suported values that are divisible with num_colls ########################### TEST CONTROLS ########################### sv_gen = 1 ########################################################################### # set either random or custom seed seed = random.randrange(0, 2**32, 1) # seed = 1379896999 # """Unify all seeds""" log.info(f"Random SEED: {seed}") set_seed(seed) ## input randomization mat1 = np.random.randint(128, size=(num_rows, num_cols)) mat2 = np.random.randint(128, size=(num_rows, num_cols)) mat1 = np.ones((num_rows, num_cols)) mat2 = np.ones((num_rows, num_cols)) # input the constatn value optionally # mat1 = np.empty((num_rows, num_cols)) # mat2 = np.empty((num_rows, num_cols)) # # fill the matrix with the same value # mat1.fill(32767) # mat2.fill(-32768) print("Inputs: ") print(type(mat1)) print(mat1) print(type(mat2)) print(mat2) reg['trace/level'] = 0 reg['gear/memoize'] = False reg['debug/trace'] = ['*'] reg['debug/webviewer'] = True res_list = [] cfg = { "cols_per_row": cols_per_row, "num_rows": num_rows, "num_cols": num_cols, 'cols_per_multiplier': num_rows // cols_per_row } cfg_seq = [cfg] cfg_drv = drv(t=TCfg, seq=cfg_seq) # Add one more dimenstion to the matrix to support input type for design mat1 = mat1.reshape(1, mat1.shape[0], mat1.shape[1]) mat2 = mat2.reshape(mat2.shape[0], 1, mat2.shape[1]) mat1_seq = [mat1] mat2_seq = [mat2] row_t = Queue[Array[Int[16], cfg['num_cols']]] mat1_drv = drv(t=Queue[row_t], seq=mat1_seq) mat2_drv = drv(t=Queue[row_t], seq=mat2_seq) res = matrix_multiplication(cfg_drv, mat1_drv, mat2_drv, cols_per_row=cols_per_row) collect(res, result=res_list) if sv_gen: cosim('/matrix_multiplication', 'verilator', outdir='build/matrix_multiplication/rtl', rebuild=True, timeout=100) sim('build/matrix_multiplication') ## Print raw results results log.info(f'len_res_list: \n{len(res_list)}') try: pg_res = [int(el) for row_chunk in res_list for el in row_chunk] # calc refference data - matrix2 needs to be transposed before doing multiplocation np_res = np.dot(np.squeeze(mat1), np.transpose(mat2.squeeze())) pg_res = np.array(pg_res).reshape(np_res.shape) log.info(f'result: \n{res}') log.info(f'pg_res: \n{pg_res}, shape: {pg_res.shape}') log.info(f'np_res: \n{np_res}, shape: {np_res.shape}') sim_assert( np.equal(pg_res, np_res).all(), "Error in compatring results") log.info("\033[92m //==== PASS ====// \033[90m") except: # printing stack trace traceback.print_exc() log.info("\033[91m //==== FAILED ====// \033[90m")