def calibrate(args): board = runtime_util.get_device(args.model_number) xfer_board = runtime_util.get_device(args.xfer_db) logger = ModelCalibrateLogger('actcal_%s.log' % args.model_number) if not args.adp is None: adp = runtime_util.get_adp(board,args.adp,widen=args.widen) for cfg in adp.configs: blk = board.get_block(cfg.inst.block) if not blk.requires_calibration(): continue cfg_modes = cfg.modes for mode in cfg_modes: cfg.modes = [mode] calibrate_block(logger, \ board, \ xfer_board, \ blk,cfg.inst.loc,cfg, \ grid_size=args.grid_size, \ rounds=args.rounds, \ samples_per_round=args.samples_per_round, \ max_samples=args.max_samples) else: raise Exception("unimplemented")
def make_histogram(args): board = runtime_util.get_device(args.model_number,layout=True) ph = paths.DeviceStatePathHandler(board.name, \ board.model_number,make_dirs=True) models = {} for exp_model in delta_model_lib.get_all(board): key = (exp_model.block, exp_model.output,\ runtime_util.get_static_cfg(exp_model.block,exp_model.config), \ exp_model.calib_obj) if not key in models: models[key] = [] models[key].append(exp_model) for key,models in models.items(): blk,out,cfg,lbl = key print("%s:%s = %d models" % (blk.name,cfg,len(models))) png_file = ph.get_histogram_vis('merr', \ blk.name, \ out.name, \ cfg, \ lbl) vizlib.model_error_histogram(models, \ png_file, \ num_bins=10, \ relative=True) png_file = ph.get_histogram_vis('objf', \ blk.name, \ out.name, \ cfg, \ lbl) vizlib.objective_fun_histogram(models, \ png_file, \ num_bins=10)
def profile_adp(args): board = runtime_util.get_device(args.model_number) calib_obj = llenums.CalibrateObjective(args.method) runtime = GrendelRunner() runtime.initialize() if args.missing: for exp_delta_model in delta_model_lib.get_all(board): profile_kernel(runtime,board, \ exp_delta_model.block, \ exp_delta_model.config, \ calib_obj, \ min_points=args.min_points, \ grid_size=args.grid_size) else: adp = runtime_util.get_adp(board, args.adp, widen=args.widen) for cfg in adp.configs: blk = board.get_block(cfg.inst.block) cfg_modes = cfg.modes for mode in cfg_modes: cfg.modes = [mode] profile_kernel(runtime,board,blk,cfg,calib_obj, \ args.min_points, args.max_points, \ args.grid_size, \ force=args.force, adp=adp)
def test_board(args): board = runtime_util.get_device(args.model_number, layout=True) for chip_id in range(0, 2): for tile_id in range(4): for slice_id in [0, 2]: for block in board.blocks: if not block.requires_calibration(): continue modes = list(block.modes) # limit the fanout modes to just positive copies if block.name == "fanout": modes = list( filter(lambda m: not "-" in str(m), block.modes)) loc = devlib.Location([chip_id, tile_id, slice_id, 0]) test_block(board,block,loc,modes, \ maximize_fit=args.maximize_fit, \ minimize_error=args.minimize_error, \ model_based=args.model_based) finalize_test(board, \ maximize_fit=args.maximize_fit, \ minimize_error=args.minimize_error, \ model_based=args.model_based)
def calibrate(args): board = runtime_util.get_device(args.model_number) exp_delta_model_lib \ .remove_by_calibration_objective(board,llenums.CalibrateObjective.BEST) for models in group_by_configured_block(board): print("===== block =====") print(models[0].config) print("") for best_mdl in get_best_calibration(models): exp_delta_model_lib.update(board, best_mdl)
def characterize_adp(args): board = runtime_util.get_device(args.model_number, layout=True) runtime = GrendelRunner() runtime.initialize() if not args.adp is None: adp = runtime_util.get_adp(board, args.adp, widen=args.widen) if args.adp_locs: args.num_locs = 1 for cfg in adp.configs: blk = board.get_block(cfg.inst.block) cfg_modes = cfg.modes for mode in cfg_modes: cfg.modes = [mode] characterize_configured_block(runtime,board,blk,cfg, \ grid_size=args.grid_size, \ num_locs=args.num_locs,\ num_hidden_codes=args.num_hidden_codes, \ adp_locs=args.adp_locs) else: if args.adp_locs or args.widen: raise Exception( "full board characterization doesn't accept adp-locs or widen parameters" ) for block in board.blocks: if not block.requires_calibration(): continue continue for mode in block.modes: loc = devlib.Location( list(board.layout.instances(block.name))[0]) cfg = BlockConfig.make(block, loc) cfg.modes = [mode] characterize_configured_block(runtime,board,block,cfg, \ grid_size=args.grid_size, \ num_locs=args.num_locs,\ num_hidden_codes=args.num_hidden_codes, \ adp_locs=False)
def execute(args): board = runtime_util.get_device(args.model_number) models_ds, datasets_ds = preprocess_board_data(board) for key in models_ds.keys(): models =models_ds[key] datasets = datasets_ds[key] repr_model = get_repr_model(models) if repr_model is None: raise Exception("no representative model found. (# models=%d)" % len(models_b)) genetic_infer_model(board, \ repr_model.block, \ repr_model.config, \ repr_model.output, \ models,datasets, \ num_generations=args.generations, \ pop_size=args.parents, \ penalty=args.penalty, \ max_params=args.max_params, \ force=args.force)
def visualize(args): board = runtime_util.get_device(args.model_number,layout=True) calib_obj = llenums.CalibrateObjective(args.calib_obj) if args.calib_obj else None ph = paths.DeviceStatePathHandler(board.name, \ board.model_number,make_dirs=True) if args.histogram: make_histogram(args) for delta_model in delta_model_lib.get_all(board): if not calib_obj is None and delta_model.calib_obj != calib_obj: continue if delta_model.complete and \ delta_model.calib_obj != llenums.CalibrateObjective.NONE: print(delta_model.config) print(delta_model) if delta_model.is_integration_op: dataset = dataset_lib.load(board, delta_model.block, \ delta_model.loc, \ delta_model.output, \ delta_model.config, \ llenums.ProfileOpType.INTEG_INITIAL_COND) else: dataset = dataset_lib.load(board, delta_model.block, \ delta_model.loc, \ delta_model.output, \ delta_model.config, \ llenums.ProfileOpType.INPUT_OUTPUT) if dataset is None: print("-> no dataset") continue assert(isinstance(dataset, dataset_lib.ExpProfileDataset)) png_file = ph.get_delta_vis(delta_model.block.name, \ delta_model.loc.file_string(),\ str(delta_model.output.name), \ str(delta_model.static_cfg), \ str(delta_model.hidden_cfg), \ delta_model.calib_obj) print(png_file) vizlib.deviation(delta_model, \ dataset, \ png_file, \ baseline=vizlib.ReferenceType.MODEL_PREDICTION, \ num_bins=10, \ amplitude=args.max_error, \ relative=not args.absolute) png_file = ph.get_correctable_delta_vis(delta_model.block.name, \ delta_model.loc.file_string(), \ str(delta_model.output.name), \ str(delta_model.static_cfg), \ str(delta_model.hidden_cfg), \ delta_model.calib_obj) print(png_file) vizlib.deviation(delta_model, \ dataset, \ png_file, \ baseline=vizlib.ReferenceType.CORRECTABLE_MODEL_PREDICTION, \ num_bins=10, \ amplitude=args.max_error, \ relative=not args.absolute) model_file = ph.get_model_file(delta_model.block.name, \ delta_model.loc.file_string(), \ str(delta_model.output.name), \ str(delta_model.static_cfg), \ str(delta_model.hidden_cfg), \ delta_model.calib_obj) with open(model_file,'w') as fh: fh.write(str(delta_model.config)) fh.write(str(delta_model)) fh.write("\n\n") fh.write(str(delta_model.spec))
def calibrate_block(logger, \ board,xfer_board, \ block,loc,config, \ grid_size=9, \ num_iters=3, \ samples_per_round=5, \ rounds=1, \ max_samples=20): logger.set_configured_block(block,loc,config.mode) # get board with initial code pool char_model = runtime_meta_util.get_model(board,block,loc,config) char_board = runtime_util.get_device("%s-active-cal/%s" % (board.model_number,char_model),layout=False) # load physical models for transfer learning. Compute the number of parameters phys_models = {} # build a calibration objective predictor with per-variable models. objfun,predictor = build_predictor(xfer_board,block,loc,config) nsamps_reqd = predictor.min_samples() + 1 max_samples += nsamps_reqd # collect initial data for fitting the transfer model # and fit all of the initial guesses for the parameters on the transfer model # this should give us an initial predictor print("==== BOOTSTRAPPING <#samps=%d> ====" % nsamps_reqd) bootstrap_block(logger, \ char_board,block,loc,config, \ grid_size=grid_size, \ num_samples=nsamps_reqd) update_model(logger,char_board,block,loc,config) # fit all of the parameters in the predictor. update_predictor(predictor,char_board) # next, we're going to populate the initial pool of points. print("==== SETUP INITIAL POOL ====") code_pool= load_code_pool_from_database(char_board, predictor,objfun) if len(code_pool.pool) >= max_samples: write_model_to_database(logger,code_pool, board,char_board) return for rnd in range(rounds): #TODO: maybe put this in a loop? # fit all of the parameters in the predictor. print("==== UPDATING PREDICTOR [%d/%d] ====" % (rnd+1,rounds)) predictor.clear() update_predictor(predictor,char_board) print("==== ADD UNLABELLED [%d/%d] ====" % (rnd+1, rounds)) add_random_unlabelled_samples(code_pool,samples_per_round) print("==== QUERY UNLABELLED [%d/%d] ====" % (rnd+1,rounds)) for pred_score, hcs in code_pool.get_unlabeled(): print("=> codes=%s" % hcs) print("=> score=%s" % pred_score) query_hidden_codes(logger,code_pool,char_board,block,loc,config,hcs, \ grid_size=grid_size) write_model_to_database(logger,code_pool, board,char_board)
def calibrate_adp(args): board = runtime_util.get_device(args.model_number) adp = runtime_util.get_adp(board, args.adp, widen=args.widen) logger = runtime_meta_util.get_calibration_time_logger(board, 'calib') runtime = GrendelRunner() runtime.initialize() calib_obj = llenums.CalibrateObjective(args.method) for cfg in adp.configs: blk = board.get_block(cfg.inst.block) cfg_modes = cfg.modes for mode in cfg_modes: cfg.modes = [mode] if not blk.requires_calibration(): continue if is_calibrated(board, \ blk, \ cfg.inst.loc, \ cfg, \ calib_obj): print("-> already calibrated") continue print("== calibrate %s (%s) ==" % (cfg.inst, calib_obj.value)) print(cfg) print('----') # calibrate block and time it start = time.time() upd_cfg = llcmd.calibrate(runtime, \ board, \ blk, \ cfg.inst.loc,\ adp, \ calib_obj=calib_obj) end = time.time() runtime_sec = end - start logger.log(block=blk.name,loc=cfg.inst.loc, mode=mode, \ calib_obj=calib_obj.value, \ operation='cal',runtime=runtime_sec) for output in blk.outputs: delta_model = delta_model_lib.load(board,blk, \ cfg.inst.loc,\ output, \ upd_cfg, \ calib_obj=calib_obj) if delta_model is None: delta_model = delta_model_lib \ .ExpDeltaModel(blk,cfg.inst.loc, \ output, \ upd_cfg, \ calib_obj=calib_obj) delta_model.calib_obj = calib_obj # update models and time it start = time.time() delta_model_lib.update(board, delta_model) end = time.time() runtime_sec = end - start logger.log(block=blk.name,loc=cfg.inst.loc, mode=mode, \ calib_obj=calib_obj.value, \ operation='fit',runtime=runtime_sec)
def clear(args): board = runtime_util.get_device(args.model_number) if args.profiles: print("-> clearing profiles") prof_dataset_lib \ .remove_all(board)