def profile_kernel(runtime,board,blk,cfg,calib_obj, \ min_points,max_points, \ grid_size,force=False,adp=None): for exp_delta_model in delta_model_lib.get_models(board, \ ['block','loc','static_config','calib_obj'], block=blk, \ loc=cfg.inst.loc, \ config=cfg, \ calib_obj=calib_obj): for method,n,m,reps in runtime_util.get_profiling_steps(exp_delta_model.output, \ exp_delta_model.config, \ grid_size, \ max_points=max_points): dataset = prof_dataset_lib.load(board,blk,cfg.inst.loc, \ exp_delta_model.output, \ exp_delta_model.config, \ method) print("<===========") print(cfg) print("===========>") print("output=%s method=%s" % (exp_delta_model.output.name, method)) print("relation=%s" % (exp_delta_model.output.relation[cfg.mode])) print("dataset npts=%d" % (len(dataset) if not dataset is None else 0)) print("n=%d m=%d reps=%d" % (n, m, reps)) print("---------") if not dataset is None and \ len(dataset) >= min_points and \ len(dataset) >= n*m*reps and \ not force: print("===> <%s> already profiled" % method) continue planner = planlib.SingleDefaultPointPlanner(blk, \ cfg.inst.loc, \ exp_delta_model.output, \ method, \ exp_delta_model.config, n=n, m=m, \ reps=reps) proflib.profile_all_hidden_states(runtime, board, planner, adp=adp)
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 _build_model(self, adp): if not self.block.outputs.has(self.port.name): raise Exception("port <%s> is not an output for <%s>" \ % (self.port.name,self.block.name)) out = self.block.outputs[self.port.name] models = deltalib.get_models(self.board, \ ['block','loc','output','static_config','calib_obj'], block=self.block, \ loc=self.loc, \ output=out, \ config=self.cfg, \ calib_obj=self.calib_obj) if len(models) == 0: model = None else: model = models[0] self.error_model = None # this llcmdcomp.compute_expression_fields(self.board,adp,self.cfg, \ compensate=self.enable_compensate) llcmdcomp.compute_constant_fields(self.board, \ adp, \ self.cfg, \ compensate=self.enable_compensate, \ debug=True) if not model is None and self.enable_phys: dataset = proflib.load(self.board, \ self.block, \ self.loc, \ out, \ cfg=model.config, \ method=llenums.ProfileOpType.INPUT_OUTPUT) spec = self.block.outputs[self.port.name].deltas[self.cfg.mode] expr = spec.get_model(model.params) if \ not self.correctable else spec.get_correctable_model(model.params, \ low_level=self.ll_correctable) if not dataset is None: errors = model.errors(dataset, init_cond=False) surf = parsurflib.build_surface(block=self.block, \ cfg=self.cfg, \ port=out, \ dataset=dataset, \ output=errors, \ npts=self.npts) self.error_model = surf validate_model(self, expr, surf, dataset) else: print("[warn] no dataset for %s %s" \ % (self.block.name,self.loc)) else: expr = self.block.outputs[self.port.name].relation[self.cfg.mode] self._expr = self._concretize(expr)
def _build_model(self, adp): def set_to_ideal_expr(): expr = self.block.outputs[self.port.name].relation[self.cfg.mode] integ_expr = mathutils.canonicalize_integration_operation(expr) self._init_cond = self._concretize(integ_expr.init_cond) self._deriv = self._concretize(integ_expr.deriv) self.error_model = None self.ic_error_model = None #expr = blk.outputs[port.name].relation[cfg.mode] out = self.block.outputs[self.port.name] models = deltalib.get_models(self.board, \ ['block','loc','output','static_config','calib_obj'], \ block=self.block, \ loc=self.loc, \ output=out, \ config=self.cfg, \ calib_obj=self.calib_obj) set_to_ideal_expr() if not self.enable_phys: return if len(models) == 0: print(self.cfg) raise Exception("no delta models for block") model = models[0] llcmdcomp.compute_expression_fields(self.board, \ adp, \ self.cfg, \ compensate=self.enable_compensate) llcmdcomp.compute_constant_fields(self.board, \ adp, \ self.cfg, \ compensate=self.enable_compensate) if not model is None and self.enable_phys: dataset = proflib.load(self.board, \ self.block, \ self.loc, \ out, \ model.config, \ method=llenums.ProfileOpType.INTEG_INITIAL_COND) spec = self.block.outputs[self.port.name].deltas[self.cfg.mode] expr = spec.get_model(model.params) \ if not self.correctable else spec.get_correctable_model(model.params, \ low_level=self.ll_correctable) if not dataset is None and self.enable_model_error: errors = model.errors(dataset, init_cond=True) surf = parsurflib.build_surface(block=self.block, \ cfg=self.cfg, \ port=out, \ dataset=dataset, \ output=errors, \ npts=self.npts) self.ic_error_model = surf validate_model(self, expr.init_cond, surf, dataset) else: print("[warn] no dataset for %s %s" \ % (self.block.name,self.loc)) integ_expr = mathutils.canonicalize_integration_operation(expr) self._init_cond = self._concretize(integ_expr.init_cond) #self._deriv = self._concretize(integ_expr.deriv) self.error_model = None
def profile(runtime,dev, \ blk,loc,adp,output_port, \ inputs, \ method=llenums.ProfileOpType.INPUT_OUTPUT,quiet=False): state_t = {blk.name: blk.state.concretize(adp, loc)} # build profiling command loc_t, loc_d = llutil.make_block_loc_t(blk, loc) values = [0.0] * 2 for input_ident, input_val in inputs.items(): values[input_ident.code()] = input_val if not quiet: print("profile-inputs: %s" % values) profile_data = {"method": method.name, \ "inst": loc_d, "in_vals": values, \ "state":state_t, "output":output_port.name} cmd_t, cmd_data = llutil.make_circ_cmd(llenums.CircCmdType.PROFILE, profile_data) cmd = cmd_t.build(cmd_data, debug=True) # execute profiling command runtime.execute(cmd) resp = llutil.unpack_response(runtime.result(quiet=quiet)) # reconstruct analog device program new_adp = adplib.ADP() blk, loc = llutil.from_block_loc_t(dev, resp['spec']['inst']) new_adp.add_instance(blk, loc) state = resp['spec']['state'][blk.name] blk.state.lift(new_adp, loc, dict(state)) blkcfg = new_adp.configs.get(blk.name, loc) # retrieve parameters for new result inputs = {} port = llutil.get_by_ll_identifier(blk.inputs, llenums.PortType.IN0) if not port is None: inputs[port.name] = resp['spec']['in_vals'][ llenums.PortType.IN0.code()] if not quiet: print("in %s = %f" % (port.name, inputs[port.name])) port = llutil.get_by_ll_identifier(blk.inputs, llenums.PortType.IN1) if not port is None: inputs[port.name] = resp['spec']['in_vals'][ llenums.PortType.IN1.code()] new_out = llutil.get_by_ll_identifier(blk.outputs, \ llenums.PortType \ .from_code(int(resp['spec']['output']))) new_method = llenums.ProfileOpType.from_code(int(resp['spec']['method'])) out_mean = resp['mean'] out_std = resp['stdev'] out_status = llenums.ProfileStatus.from_code(int(resp['status'])) if not quiet: print("datum inputs=%s out=%f std=%f status=%s" % (inputs, out_mean, out_std, out_status.value)) print(blkcfg) # insert into database if out_status == llenums.ProfileStatus.SUCCESS: dataset = exp_profile_lib.load(dev, blk, loc, new_out, blkcfg, method) if dataset is None: dataset = exp_profile_lib.ExpProfileDataset(blk, \ loc, \ new_out, \ blkcfg, \ new_method) dataset.add(config=blkcfg, \ inputs=inputs, \ mean=out_mean, \ std=out_std) exp_profile_lib.update(dev, dataset) return blkcfg