def test_fir_random_type(impl, seed, do_cosim): # Set random seed set_seed(seed) log.info(f'{__name__} impl: {impl}, seed: {seed}') fixp_w = random.randint(16, 32) int_w = random.randint(1, 3) t_b = Fixp[int_w, fixp_w] log.info( f'{__name__} FIR input type t_b: {t_b} min: {t_b.fmin}, max: {t_b.fmax}' ) seq = [] # generate multiple constant sequences of certain size for i in range(10): num = np.random.uniform(t_b.fmin, t_b.fmax) for i in range(50): seq.append(num) # add a random sequence of certain size seq.extend(random_seq(t_b, 100)) # genrate random numbers in [-1,1) # seq = np.random.random(size=(100, )) * 2 - 1 log.debug(f'Generated sequence: {seq}') res = fir_sim(impl, t_b, seq, do_cosim=do_cosim)
def test_matrix_mult(impl, num_cols, num_rows, cols_per_row, seed): set_seed(seed) mat1 = np.random.randint(256, size=(num_cols, num_rows)) mat2 = np.random.randint(256, size=(num_cols, num_rows)) # if incompatible cols_per_row fall back to '1' if mat1.shape[0] % cols_per_row != 0: cols_per_row = 1 run_matrix(impl, mat1, mat2, cols_per_row, col_only=False)
def test_column_multiplicator(impl, num_cols, num_rows, cols_per_row, seed): set_seed(seed) res_list = [] mat1 = np.random.randint(256, size=(num_cols, num_rows)) mat2 = np.random.randint(256, size=(num_cols, num_rows)) run_matrix(impl, mat1, mat2, cols_per_row, col_only=True)
def test_matrix_mult_cols(impl, cols_per_row, seed): set_seed(seed) # generate random number of colls compatible with colls_per_row num_cols = np.random.randint(1, 3) * cols_per_row num_rows = np.random.randint(2, cols_per_row * 2) # genrate matrixes with random signed numbers 10 bit mat1 = np.random.randint(-1023, 1024, size=(num_cols, num_rows)) mat2 = np.random.randint(-1023, 1024, size=(num_cols, num_rows)) run_matrix(impl, mat1, mat2, cols_per_row, col_only=False)
def test_iir_random(impl, seed, do_cosim): log.info(f'Running test_iir_random seed: {seed}') set_seed(seed) ftype = Fixp[5, 32] seq = constant_seq(ftype, 10, 0) seq.extend(random_seq(ftype, 100)) seq.extend(constant_seq(ftype, 10, 0)) iir_sim(impl, ftype, ftype, ftype, seq, do_cosim=do_cosim)
def test_fir_random(impl, seed, do_cosim): # Set random seed set_seed(seed) log.info(f'Running {__name__} impl: {impl}, seed: {seed}') t_b = Fixp[1, 15] # genrate random numbers in [-1,1) seq = np.random.random(size=(100, )) * 2 - 1 log.debug(f'Generated sequence: {seq}') res = fir_sim(impl, t_b, seq, do_cosim=do_cosim)
def test_iir_limits(fixp_w, int_w, impl, seed, do_cosim): """[Drive filter with extreme values [min, 0 , max] """ log.info(f'Running test_iir_limits, seed: {seed}') set_seed(seed) factor = 0.5 # supported factor ftype = Fixp[int_w, fixp_w] print(f'max possible value {ftype.fmax}') extremes = [[0 for i in range(50)]] extremes.append([ftype.fmin * factor for i in range(10)]) seq = random_choice_seq(extremes, 10) extremes.append([ftype.fmax * factor for i in range(10)]) seq = random_choice_seq(extremes, 10) iir_sim(impl, ftype, ftype, ftype, seq, do_cosim=do_cosim)
def test_iir_random_type(impl, seed, do_cosim): log.info(f'Running test_iir_random, seed: {seed}') set_seed(seed) # minimum supported precision Fixp[3,19] int_w = random.randint(3, 7) fixp_w = random.randint(int_w + 19, int_w + 32) ftype = Fixp[int_w, fixp_w] log.info( f'{__name__} FIR input type t_b: {ftype} min: {ftype.fmin}, max: {ftype.fmax}' ) seq = constant_seq(ftype, 10, 0) seq.extend(random_seq(ftype, 100)) seq.extend(constant_seq(ftype, 10, 0)) iir_sim(impl, ftype, ftype, ftype, seq, do_cosim=do_cosim)
def test_fir_sine(freq, impl, seed, do_cosim): """[Drive filter with sine signal at fs fs/2 and fs*2 """ # Set random seed set_seed(seed) # set clock freq reg['sim/clk_freq'] = freq t = list(range(reg['sim/clk_freq']))[0:100] fs = reg['sim/clk_freq'] f1 = freq / 100 log.info(f'Running {__name__} impl: {impl}, seed: {seed}, fs:{fs},f1:{f1}') ftype = Fixp[5, 32] seq = [0 for i in range(10)] seq.extend(sine_seq(f1, fs, 200, ftype)) seq.extend([0 for i in range(100)]) seq.extend(sine_seq(f1, fs / 2, 100, ftype)) seq.extend([0 for i in range(100)]) seq.extend(sine_seq(f1, fs * 2, 400, ftype)) seq.extend([0 for i in range(10)]) res = iir_sim(impl, ftype, ftype, ftype, seq, do_cosim=do_cosim)
def test_fir_limits(fixp_w, int_w, impl, seed, do_cosim): """[Drive filter with extreme values [min, 0 , max] """ # Set random seed set_seed(seed) log.info(f'Running {__name__} impl: {impl}, seed: {seed}') t_b = Fixp[int_w, fixp_w] log.info( f'{__name__} FIR input type t_b: {t_b} min: {t_b.fmin}, max: {t_b.fmax}' ) extremes = [[0 for i in range(50)]] extremes.append([t_b.fmin for i in range(50)]) extremes.append([t_b.fmax for i in range(50)]) seq = random_choice_seq(extremes, 10) log.debug(f'Generated sequence: {seq}') res = fir_sim(impl, t_b, seq, do_cosim=do_cosim)
def test_fir_sine(freq, impl, seed, do_cosim): """[Drive filter with sine signal at fs fs/2 and fs*2 """ # Set random seed set_seed(seed) # set clock freq reg['sim/clk_freq'] = freq t = list(range(reg['sim/clk_freq']))[0:100] fs = reg['sim/clk_freq'] f1 = freq / 100 log.info(f'Running {__name__} impl: {impl}, seed: {seed}') t_b = Fixp[1, 15] seq = [0 for i in range(10)] seq.extend(sine_seq(f1, fs, 200, t_b)) seq.extend(sine_seq(f1, fs / 2, 100, t_b)) seq.extend(sine_seq(f1, fs * 2, 400, t_b)) seq.extend([0 for i in range(10)]) log.debug(f'Generated sequence: {seq}') res = fir_sim(impl, t_b, seq, do_cosim=do_cosim)
############################## SELECT TEST ############################### test_sel = 1 # 0=fir_direct; 1=fir_transposed enable_svgen = 1 # enables systemVerilog generation ########################################################################## # >> used to probe all signals reg['debug/trace'] = ['*'] # >> used to enable JSON file creation for webviewer support reg['debug/webviewer'] = True # 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) # generate b coefficients b_coef = firwin(8, [0.05, 0.95], width=0.05, pass_zero=False) # generate quantized b coefficients b_coef_type = Fixp[1, 15] b_coef_fixp = [b_coef_type(i) for i in b_coef] # generate random inputs x = np.random.random(size=(10, )) # calculated expected outputs ref = np.convolve(x, b_coef) # saturate the results value to filter output type if needed
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")