def test_block(board,block,loc,modes, \ minimize_error=False, \ maximize_fit=False, \ model_based=False): if (board.model_number is None): raise Exception("please specify model number!!") for mode in modes: new_adp = ADP() new_adp.add_instance(block, loc) blkcfg = new_adp.configs.get(block.name, loc) blkcfg.modes = [mode] print("############################") print("======== TESTING BLOCK =====") print("%s.%s mode=%s" \ % (block.name,loc,mode)) print("###########################") upd_adp = runtime_util.make_block_test_adp(board, new_adp, block, blkcfg) adp_filename = runtime_meta_util.get_adp(board, block, loc, blkcfg) with open(adp_filename, 'w') as fh: fh.write(json.dumps(upd_adp.to_json())) succ = True if minimize_error: objfun = llenums.CalibrateObjective.MINIMIZE_ERROR if not is_calibrated(board, block, loc, blkcfg, objfun): succ &= runtime_meta_util.legacy_calibration(board, \ adp_filename, \ objfun,logfile=TESTBOARD_LOG, \ block=block,mode=mode,loc=loc) if maximize_fit and succ: objfun = llenums.CalibrateObjective.MAXIMIZE_FIT if not is_calibrated(board, block, loc, blkcfg, objfun): succ &= runtime_meta_util.legacy_calibration(board, \ adp_filename, \ objfun,logfile=TESTBOARD_LOG, \ block=block, mode=mode, loc=loc) if model_based and succ: raise Exception("re-implementing model based testing") succ &= runtime_meta_util.model_based_calibration(board, \ adp_filename, \ logfile=TESTBOARD_LOG, \ block=block, mode=mode, loc=loc) print(">> removing file: %s" % adp_filename) runtime_meta_util.remove_file(adp_filename) if not succ: print(blkcfg) raise Exception("[ERROR] failed to calibrate block %s.%s" % (block.name, loc))
def exec_adp(args): board = board = runtime_util \ .get_device(args.model_number,layout=True) with open(args.adp, 'r') as fh: adp = ADP.from_json(board, \ json.loads(fh.read())) model_number = adp.metadata[ADPMetadata.Keys.RUNTIME_PHYS_DB] board.model_number = model_number prog_name = adp.metadata.get(ADPMetadata.Keys.DSNAME) program = dsproglib.DSProgDB.get_prog(prog_name) sim = dsproglib.DSProgDB.get_sim(prog_name) if not args.osc: osc = None else: osc = osclib.Sigilent1020XEOscilloscope(configlib.OSC_IP, \ configlib.OSC_PORT) osc.setup() sim_time = sim.sim_time if args.runtime: assert (args.runtime <= program.max_time) sim_time = args.runtime runtime = grendel_runner_lib.GrendelRunner() runtime.initialize() for conn in adp.conns: sblk = board.get_block(conn.source_inst.block) dblk = board.get_block(conn.dest_inst.block) llcmd.set_conn(runtime,sblk,conn.source_inst.loc, \ conn.source_port, \ dblk,conn.dest_inst.loc, \ conn.dest_port) for cfg in adp.configs: blk = board.get_block(cfg.inst.block) resp = llcmd.set_state(runtime, \ board, blk, \ cfg.inst.loc, \ adp) if blk.name == 'lut': llcmd.write_lut(runtime, \ board, \ blk, \ cfg.inst.loc, \ adp) llcmd.execute_simulation(runtime,board, \ program, adp,\ sim_time=sim_time, \ osc=osc, \ manual=False) runtime.close()
def exec_lemul(args): from compiler import lsim path_handler = paths.PathHandler(args.subset, args.program) program = DSProgDB.get_prog(args.program) timer = util.Timer('emul', path_handler) if args.unscaled: direc = path_handler.lgraph_adp_dir() else: direc = path_handler.lscale_adp_dir() board = get_device(None) for dirname, subdirlist, filelist in \ os.walk(direc): for adp_file in filelist: if adp_file.endswith('.adp'): with open(dirname + "/" + adp_file, 'r') as fh: print("===== %s =====" % (adp_file)) adp = ADP.from_json(board, \ json.loads(fh.read())) if args.unscaled: for cfg in adp.configs: cfg.modes = [cfg.modes[0]] plot_file = path_handler.adp_sim_plot( paths.PlotType.SIMULATION, \ adp.metadata[ADPMetadata.Keys.DSNAME], adp.metadata[ADPMetadata.Keys.LGRAPH_ID], 'na', 'na', 'na', \ per_variable=args.separate_figures) else: plot_file = path_handler.adp_sim_plot( paths.PlotType.SIMULATION, \ adp.metadata[ADPMetadata.Keys.DSNAME], adp.metadata[ADPMetadata.Keys.LGRAPH_ID], adp.metadata[ADPMetadata.Keys.LSCALE_ID], adp.metadata[ADPMetadata.Keys.LSCALE_SCALE_METHOD], adp.metadata[ADPMetadata.Keys.LSCALE_OBJECTIVE], \ per_variable=args.separate_figures) print(plot_file) board = get_device( adp.metadata[ADPMetadata.Keys.RUNTIME_PHYS_DB]) lsim.simulate_adp(board,adp,plot_file, \ enable_quantization=not args.no_quantize, \ enable_intervals=not args.no_operating_range, \ enable_physical_model= not args.no_physdb, \ enable_model_error =not args.no_model_error, \ separate_figures=args.separate_figures)
def test_osc(args): board = runtime_util \ .get_device(args.model_number,layout=False) with open(args.adp, 'r') as fh: adp = ADP.from_json(board, \ json.loads(fh.read())) prog_name = adp.metadata.get(ADPMetadata.Keys.DSNAME) program = dsproglib.DSProgDB.get_prog(prog_name) if not args.osc: osc = osclib.DummySigilent1020XEOscilloscope() else: osc = osclib.Sigilent1020XEOscilloscope(configlib.OSC_IP, \ configlib.OSC_PORT) osc.setup() sim_time = program.max_time if args.runtime: sim_time = args.runtime llcmd.test_oscilloscope(board, osc, program, adp, sim_time)
def exec_lexec(args): EXEC_CMD = "python3 grendel.py exec {adp_path} --model-number {model_number}" if args.scope: EXEC_CMD += " --osc" board = get_device(None) path_handler = paths.PathHandler(args.subset, args.program) program = DSProgDB.get_prog(args.program) timer = util.Timer('lexec', path_handler) for dirname, subdirlist, filelist in \ os.walk(path_handler.lscale_adp_dir()): for adp_file in filelist: if adp_file.endswith('.adp'): adp_path = dirname + "/" + adp_file print(adp_path) with open(adp_path, 'r') as fh: print("===== %s =====" % (adp_file)) adp = ADP.from_json(board, \ json.loads(fh.read())) kwargs = { 'adp_path': adp_path, 'model_number': adp.metadata[ADPMetadata.Keys.RUNTIME_PHYS_DB] } if not _lexec_already_ran(path_handler,board,adp,trial=0, \ scope=args.scope) or \ args.force: timer.start() cmd = EXEC_CMD.format(**kwargs) code = os.system(cmd) timer.end() #input("continue") if code == signal.SIGINT or code != 0: raise Exception("User terminated process") print(timer) timer.save()
def exec_wav(args, trials=1): import compiler.lwav_pass.waveform as wavelib import compiler.lwav_pass.analyze as analyzelib path_handler = paths.PathHandler(args.subset, \ args.program) program = DSProgDB.get_prog(args.program) # bin summary plots summary = {} summary_key = lambda adp : ( adp.metadata[ADPMetadata.Keys.RUNTIME_CALIB_OBJ], \ adp.metadata[ADPMetadata.Keys.LSCALE_SCALE_METHOD], \ adp.metadata[ADPMetadata.Keys.LSCALE_OBJECTIVE], \ adp.metadata[ADPMetadata.Keys.RUNTIME_PHYS_DB], \ adp.metadata[ADPMetadata.Keys.LSCALE_NO_SCALE], \ adp.metadata[ADPMetadata.Keys.LSCALE_ONE_MODE]) def update_summary(adp, var, wave, has_scope=False): key = (summary_key(adp), var, has_scope) if not key in summary: summary[key] = [] summary[key].append((adp, wave)) assert (not args.scope_only or not args.adc_only) if args.scope_only: scope_options = [True] elif args.adc_only: scope_options = [False] else: scope_options = [True, False] for dirname, subdirlist, filelist in \ os.walk(path_handler.lscale_adp_dir()): for adp_file in filelist: if adp_file.endswith('.adp'): with open(dirname + "/" + adp_file, 'r') as fh: print("===== %s =====" % (adp_file)) adp_obj = json.loads(fh.read()) metadata = ADPMetadata.from_json(adp_obj['metadata']) if not metadata.has(ADPMetadata.Keys.RUNTIME_PHYS_DB) or \ not metadata.has(ADPMetadata.Keys.RUNTIME_CALIB_OBJ): continue board = get_device( metadata.get(ADPMetadata.Keys.RUNTIME_PHYS_DB)) adp = ADP.from_json(board, adp_obj) calib_obj = llenums.CalibrateObjective( adp.metadata[ADPMetadata.Keys.RUNTIME_CALIB_OBJ]) for trial in range(trials): for var, _, _ in adp.observable_ports(board): for has_scope in scope_options: print("------- %s [has_scope=%s] ----" % (adp_file, has_scope)) waveform_file = path_handler.measured_waveform_file( \ graph_index=adp.metadata[ADPMetadata.Keys.LGRAPH_ID], scale_index=adp.metadata[ADPMetadata.Keys.LSCALE_ID], model=adp.metadata[ADPMetadata.Keys.LSCALE_SCALE_METHOD], calib_obj=calib_obj, \ opt=adp.metadata[ADPMetadata.Keys.LSCALE_OBJECTIVE], \ phys_db=adp.metadata[ADPMetadata.Keys.RUNTIME_PHYS_DB] , \ no_scale=adp.metadata[ADPMetadata.Keys.LSCALE_NO_SCALE], \ one_mode=adp.metadata[ADPMetadata.Keys.LSCALE_ONE_MODE], \ variable=var, \ trial=trial, \ oscilloscope=has_scope) if os.path.exists(waveform_file): with open(waveform_file, 'r') as fh: obj = util.decompress_json(fh.read()) wave = wavelib.Waveform.from_json(obj) adp = ADP.from_json(board, adp_obj) update_summary(adp, var, wave, has_scope=has_scope) for vis in analyzelib.plot_waveform(board,adp,wave, \ emulate=args.emulate, \ measured=args.measured): plot_file = path_handler.waveform_plot_file( \ graph_index=adp.metadata[ADPMetadata.Keys.LGRAPH_ID], scale_index=adp.metadata[ADPMetadata.Keys.LSCALE_ID], model=adp.metadata[ADPMetadata.Keys.LSCALE_SCALE_METHOD], calib_obj=calib_obj, \ opt=adp.metadata[ADPMetadata.Keys.LSCALE_OBJECTIVE], \ phys_db=adp.metadata[ADPMetadata.Keys.RUNTIME_PHYS_DB], \ no_scale=adp.metadata[ADPMetadata.Keys.LSCALE_NO_SCALE], \ one_mode=adp.metadata[ADPMetadata.Keys.LSCALE_ONE_MODE], \ variable=var, \ trial=trial, \ plot=vis.name, \ oscilloscope=has_scope) vis.plot(plot_file) if args.summary_plots: for (fields, var, has_scope), data in summary.items(): adps = list(map(lambda d: d[0], data)) waveforms = list(map(lambda d: d[1], data)) board = get_device(adps[0].metadata.get( ADPMetadata.Keys.RUNTIME_PHYS_DB)) for vis in analyzelib.plot_waveform_summaries( board, adps, waveforms): adp = data[0][0] calib_obj = llenums.CalibrateObjective( adp.metadata[ADPMetadata.Keys.RUNTIME_CALIB_OBJ]) plot_file = path_handler.summary_plot_file( \ model=adp.metadata[ADPMetadata.Keys.LSCALE_SCALE_METHOD], calib_obj=calib_obj, \ opt=adp.metadata[ADPMetadata.Keys.LSCALE_OBJECTIVE], \ phys_db=adp.metadata[ADPMetadata.Keys.RUNTIME_PHYS_DB], \ variable=var, \ plot=vis.name, \ oscilloscope=has_scope, \ no_scale=adp.metadata[ADPMetadata.Keys.LSCALE_NO_SCALE], \ one_mode=adp.metadata[ADPMetadata.Keys.LSCALE_ONE_MODE]) vis.plot(plot_file)
def exec_stats(args, trials=1): import compiler.lwav_pass.waveform as wavelib import compiler.lwav_pass.analyze as analyzelib error_key = lambda adp : ( adp.metadata[ADPMetadata.Keys.RUNTIME_CALIB_OBJ], \ adp.metadata[ADPMetadata.Keys.LSCALE_SCALE_METHOD], \ adp.metadata[ADPMetadata.Keys.LSCALE_OBJECTIVE], \ adp.metadata[ADPMetadata.Keys.RUNTIME_PHYS_DB], \ adp.metadata[ADPMetadata.Keys.LSCALE_NO_SCALE], \ adp.metadata[ADPMetadata.Keys.LSCALE_ONE_MODE]) error_summary = {} def update_error(adp, error): key = error_key(adp) if not key in error_summary: error_summary[key] = [] error_summary[key].append(error) path_handler = paths.PathHandler(args.subset, \ args.program) program = DSProgDB.get_prog(args.program) scope_options = [True, False] if args.runtimes_only: print("------------ runtime ----------------") print_runtime_stats(path_handler) return error = None best_adp = None best_adp_name = None for dirname, subdirlist, filelist in \ os.walk(path_handler.lscale_adp_dir()): for adp_file in filelist: if adp_file.endswith('.adp'): with open(dirname + "/" + adp_file, 'r') as fh: print("===== %s =====" % (adp_file)) adp_obj = json.loads(fh.read()) metadata = ADPMetadata.from_json(adp_obj['metadata']) if not metadata.has(ADPMetadata.Keys.RUNTIME_PHYS_DB) or \ not metadata.has(ADPMetadata.Keys.RUNTIME_CALIB_OBJ): continue board = get_device( metadata.get(ADPMetadata.Keys.RUNTIME_PHYS_DB)) adp = ADP.from_json(board, adp_obj) calib_obj = llenums.CalibrateObjective( adp.metadata[ADPMetadata.Keys.RUNTIME_CALIB_OBJ]) for trial in range(trials): for var, _, _ in adp.observable_ports(board): for has_scope in scope_options: print("------- %s [has_scope=%s] ----" % (adp_file, has_scope)) waveform_file = path_handler.measured_waveform_file( \ graph_index=adp.metadata[ADPMetadata.Keys.LGRAPH_ID], scale_index=adp.metadata[ADPMetadata.Keys.LSCALE_ID], model=adp.metadata[ADPMetadata.Keys.LSCALE_SCALE_METHOD], calib_obj=calib_obj, \ opt=adp.metadata[ADPMetadata.Keys.LSCALE_OBJECTIVE], \ phys_db=adp.metadata[ADPMetadata.Keys.RUNTIME_PHYS_DB] , \ no_scale=adp.metadata[ADPMetadata.Keys.LSCALE_NO_SCALE], \ one_mode=adp.metadata[ADPMetadata.Keys.LSCALE_ONE_MODE], \ variable=var, \ trial=trial, \ oscilloscope=has_scope) if os.path.exists(waveform_file): with open(waveform_file, 'r') as fh: obj = util.decompress_json(fh.read()) wave = wavelib.Waveform.from_json(obj) this_error = analyzelib.get_waveform_error( board, adp, wave) if this_error is None: continue update_error(adp, this_error) if error is None or this_error < error: error = this_error best_adp = adp best_adp_name = adp_file print("============ BEST EXECUTION SUMMARY ========") print(best_adp_name) print( "----------------------------------------------------------------------------" ) analyzelib.print_summary(board, best_adp, error) print("------------ runtime ----------------") print_runtime_stats(path_handler) print("============ AVERAGE EXECUTION SUMMARY ========") for key, errors in error_summary.items(): median = np.median(errors) q1 = np.percentile(errors, 25) med = np.percentile(errors, 50) q3 = np.percentile(errors, 75) min_err = min(errors) max_err = max(errors) print("%s min=%f q1=%f med=%f q3=%f max=%f n=%d" % (key, min_err, q1, med, q3, max_err, len(errors)))
def exec_lscale(args): from compiler import lscale import compiler.lscale_pass.lscale_ops as scalelib board = get_device(args.model_number) path_handler = paths.PathHandler(args.subset, args.program) program = DSProgDB.get_prog(args.program) timer = util.Timer('lscale', path_handler) for dirname, subdirlist, filelist in \ os.walk(path_handler.lgraph_adp_dir()): for lgraph_adp_file in filelist: if lgraph_adp_file.endswith('.adp'): with open(dirname + "/" + lgraph_adp_file, 'r') as fh: print("===== %s =====" % (lgraph_adp_file)) adp = ADP.from_json(board, \ json.loads(fh.read())) obj = scalelib.ObjectiveFun(args.objective) scale_method = scalelib.ScaleMethod(args.scale_method) calib_obj = get_calibrate_objective(args.calib_obj) if args.no_scale and not scale_method is scalelib.ScaleMethod.IDEAL: raise Exception( "cannot disable scaling transform if you're using the delta model database" ) timer.start() for idx,scale_adp in enumerate(lscale.scale(board, \ program, \ adp, \ objective=obj, \ scale_method=scale_method, \ calib_obj=calib_obj, \ no_scale=args.no_scale, \ one_mode=args.one_mode)): timer.end() print("<<< writing scaled circuit %d/%d>>>" % (idx, args.scale_adps)) scale_adp.metadata.set(ADPMetadata.Keys.LSCALE_ID, idx) calib_obj = llenums.CalibrateObjective(scale_adp \ .metadata[ADPMetadata.Keys.RUNTIME_CALIB_OBJ]) filename = path_handler.lscale_adp_file( scale_adp.metadata[ADPMetadata.Keys.LGRAPH_ID], scale_adp.metadata[ADPMetadata.Keys.LSCALE_ID], scale_adp.metadata[ADPMetadata.Keys.LSCALE_SCALE_METHOD], scale_adp.metadata[ADPMetadata.Keys.LSCALE_OBJECTIVE], calib_obj, scale_adp.metadata[ADPMetadata.Keys.RUNTIME_PHYS_DB], \ no_scale=scale_adp.metadata[ADPMetadata.Keys.LSCALE_NO_SCALE], \ one_mode=scale_adp.metadata[ADPMetadata.Keys.LSCALE_ONE_MODE] \ ) with open(filename, 'w') as fh: jsondata = scale_adp.to_json() fh.write(json.dumps(jsondata, indent=4)) print("<<< writing graph >>>") filename = path_handler.lscale_adp_diagram_file( scale_adp.metadata[ADPMetadata.Keys.LGRAPH_ID], scale_adp.metadata[ADPMetadata.Keys.LSCALE_ID], scale_adp.metadata[ADPMetadata.Keys.LSCALE_SCALE_METHOD], scale_adp.metadata[ADPMetadata.Keys.LSCALE_OBJECTIVE], calib_obj, scale_adp.metadata[ADPMetadata.Keys.RUNTIME_PHYS_DB], \ no_scale=scale_adp.metadata[ADPMetadata.Keys.LSCALE_NO_SCALE], \ one_mode=scale_adp.metadata[ADPMetadata.Keys.LSCALE_ONE_MODE] \ ) adprender.render(board, scale_adp, filename) if idx >= args.scale_adps: break timer.start() print("<<< done >>>") timer.kill() print(timer) timer.save()