Example #1
0
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)
Example #2
0
def build_expr(dev, sim, adp, block, cfg, port):
    calib_obj = llenums.CalibrateObjective(adp \
                                           .metadata \
                                           .get(adplib.ADPMetadata.Keys.RUNTIME_CALIB_OBJ))
    print(block.name, port.name)
    if is_integrator(block, cfg, port):
        emul_block = ADPStatefulEmulBlock(dev, adp, block, cfg, port,
                                          calib_obj)
    else:
        emul_block = ADPEmulBlock(dev, adp, block, cfg, port, calib_obj)

    for input_port in block.inputs:
        for inst,port_name in map(lambda c: (c.source_inst,c.source_port), \
                                filter(lambda c: \
                                       c.dest_inst == cfg.inst and \
                                       c.dest_port == input_port.name, \
                                       adp.conns)):
            src_cfg = adp.configs.get(inst.block, inst.loc)
            src_blk = dev.get_block(inst.block)
            src_port = src_blk.outputs[port_name]

            if not is_integrator(src_blk, src_cfg, src_port):
                src_emul_block = build_expr(dev, sim, adp, src_blk, src_cfg,
                                            src_port)
                emul_block.connect(input_port, src_emul_block)
            else:
                var_name = src_cfg.get(port_name).source
                src_emul = ADPEmulVar(var_name)
                emul_block.connect(input_port, src_emul)

    return emul_block
Example #3
0
def save_data_from_arduino(dataset, board, dsprog, adp, sim_time, trial=0):

    ph = pathlib.PathHandler(adp.metadata[adplib.ADPMetadata.Keys.FEATURE_SUBSET], \
                             dsprog.name)

    calib_obj = llenums.CalibrateObjective(
        adp.metadata[adplib.ADPMetadata.Keys.RUNTIME_CALIB_OBJ])

    times, voltages_by_chan = unpack_arduino_waveform(dataset)
    # seconds per time unit
    tc = board.time_constant / adp.tau
    wc_time = get_wall_clock_time(board, dsprog, adp, sim_time)
    print("num-samps: %d" % len(times))
    print("wall-clock-time: [%f,%f]" % (min(times), max(times)))
    for var, scf, chans in adp.observable_ports(board):
        chan_id = get_ard_chan_for_pin(chans[llenums.Channels.POS].pin)
        voltages = voltages_by_chan[chan_id]
        #for i,(t,v) in enumerate(zip(times,voltages)):
        #    print("%d: %e\t%f" % (i,t,v))
        print("voltages[%s,%d]: [%f,%f]" % (var,chan_id, \
                                          min(voltages),max(voltages)))

        json_data = {'times':times,  \
                     'values':voltages,  \
                     'time_units': 'wall_clock_sec', \
                     'ampl_units': 'voltage', \
                     'runtime': wc_time,\
                     'variable':var, \
                     'time_scale':tc, \
                     'mag_scale':scf}
        print("<writing file>")
        filename = ph.measured_waveform_file(graph_index=adp.metadata[adplib.ADPMetadata.Keys.LGRAPH_ID], \
                                             scale_index=adp.metadata[adplib.ADPMetadata.Keys.LSCALE_ID], \
                                             model=adp.metadata[adplib.ADPMetadata.Keys.LSCALE_SCALE_METHOD], \
                                             opt=adp.metadata[adplib.ADPMetadata.Keys.LSCALE_OBJECTIVE], \
                                             phys_db=adp.metadata[adplib.ADPMetadata.Keys.RUNTIME_PHYS_DB], \
                                             calib_obj=calib_obj, \
                                             no_scale=adp.metadata[adplib.ADPMetadata.Keys.LSCALE_NO_SCALE], \
                                             one_mode=adp.metadata[adplib.ADPMetadata.Keys.LSCALE_ONE_MODE], \
                                             variable=var, \
                                             trial=trial)
        print(filename)
        with open(filename.format(variable=var), 'w') as fh:
            print("-> compressing data")
            strdata = util.compress_json(json_data)
            fh.write(strdata)
        print("<wrote file>")
    def from_json(dev, obj):
        assert (isinstance(dev, devlib.Device))

        blk = dev.get_block(obj['block'])
        loc = devlib.Location.from_string(obj['loc'])
        output = blk.outputs[obj['output']]
        cfg = adplib.BlockConfig.from_json(dev, obj['config'])
        calib_obj = llenums.CalibrateObjective(obj['calib_obj'])
        phys = ExpDeltaModel(blk, loc, output, cfg, calib_obj)

        phys._params = obj['params']
        if 'model_error' in obj:
            phys._model_error = obj['model_error']

        if 'noise' in obj:
            phys._noise = obj['noise']
        return phys
Example #5
0
def save_data_from_oscilloscope(osc, board, dsprog, adp, sim_time, trial=0):

    ph = pathlib.PathHandler(adp.metadata[adplib.ADPMetadata.Keys.FEATURE_SUBSET], \
                             dsprog.name)
    calib_obj = llenums.CalibrateObjective(
        adp.metadata[adplib.ADPMetadata.Keys.RUNTIME_CALIB_OBJ])

    wc_time = get_wall_clock_time(board, dsprog, adp, sim_time)
    for var, scf, chans in adp.observable_ports(board):
        chan_pos = get_osc_chan_for_pin(chans[llenums.Channels.POS].pin)
        chan_neg = get_osc_chan_for_pin(chans[llenums.Channels.NEG].pin)
        times,voltages = oscliblib.get_waveform(osc, \
                                                chan_pos, \
                                                chan_neg, \
                                                differential=True)
        tc = board.time_constant / adp.tau
        json_data = {'times':times,  \
                     'values':voltages,  \
                     'time_units': 'wall_clock_sec', \
                     'ampl_units': 'voltage', \
                     'runtime': wc_time,\
                     'variable':var, \
                     'time_scale':tc, \
                     'mag_scale':scf}
        print("<writing file>")

        filename = ph.measured_waveform_file(graph_index=adp.metadata[adplib.ADPMetadata.Keys.LGRAPH_ID], \
                                             scale_index=adp.metadata[adplib.ADPMetadata.Keys.LSCALE_ID], \
                                             model=adp.metadata[adplib.ADPMetadata.Keys.LSCALE_SCALE_METHOD], \
                                             opt=adp.metadata[adplib.ADPMetadata.Keys.LSCALE_OBJECTIVE], \
                                             phys_db=adp.metadata[adplib.ADPMetadata.Keys.RUNTIME_PHYS_DB], \
                                             calib_obj=calib_obj, \
                                             no_scale=adp.metadata[adplib.ADPMetadata.Keys.LSCALE_NO_SCALE], \
                                             one_mode=adp.metadata[adplib.ADPMetadata.Keys.LSCALE_ONE_MODE], \
                                             variable=var, \
                                             trial=trial, \
                                             oscilloscope=True)
        print(filename)

        with open(filename.format(variable=var), 'w') as fh:
            print("-> compressing data")
            strdata = util.compress_json(json_data)
            fh.write(strdata)
        print("<wrote file>")
Example #6
0
def set_calibration_codes(board,adp,cfg):
    blk = board.get_block(cfg.inst.block)
    if all(map(lambda st: not isinstance(st.impl, blocklib.BCCalibImpl), blk.state)):
       print("[WARN] no calibration codes <%s>" % blk.name)
       return

    calib_obj_str = adp.metadata[adplib.ADPMetadata.Keys.RUNTIME_CALIB_OBJ]
    calib_obj = llenums.CalibrateObjective(calib_obj_str)
    model = get_experimental_model(board, \
                                   blk, \
                                   cfg.inst.loc, \
                                   cfg, \
                                   calib_obj=calib_obj)
    if model is None:
       print(cfg)
       raise Exception("no empirical model found for <%s>" % calib_obj)

    for code,val in model.hidden_codes():
       cfg[code].value = val
Example #7
0
def _lexec_already_ran(ph, board, adp, trial=0, scope=False):
    calib_obj = llenums.CalibrateObjective(adp \
                            .metadata[ADPMetadata.Keys.RUNTIME_CALIB_OBJ])

    for var, scf, chans in adp.observable_ports(board):
        filename = ph.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], \
                                             opt=adp.metadata[ADPMetadata.Keys.LSCALE_OBJECTIVE], \
                                             no_scale=adp.metadata[ADPMetadata.Keys.LSCALE_NO_SCALE], \
                                             one_mode=adp.metadata[ADPMetadata.Keys.LSCALE_ONE_MODE], \
                                             phys_db=adp.metadata[ADPMetadata.Keys.RUNTIME_PHYS_DB], \
                                             calib_obj=calib_obj, \
                                             variable=var, \
                                             trial=trial, \
                                             oscilloscope=scope)

        if not os.path.exists(filename):
            return False
    return True
Example #8
0
def compute_constant_fields(board,adp,cfg,compensate=True,debug=False):
    blk = board.get_block(cfg.inst.block)
    data = list(filter(lambda d: d.type == blocklib.BlockDataType.CONST, \
                        blk.data))
    if(len(data) < 1):
        return

    assert(len(data) == 1)
    data_field = data[0]

    if compensate:
        calib_obj_str = adp.metadata[adplib.ADPMetadata.Keys.RUNTIME_CALIB_OBJ]
        calib_obj = llenums.CalibrateObjective(calib_obj_str)
        model = get_experimental_model(board, \
                                       blk, \
                                       cfg.inst.loc, \
                                       cfg, \
                                       calib_obj=calib_obj)

        is_init_cond = model.is_integration_op
        gain,offset = get_compensation_parameters(model,is_init_cond)
    else:
        gain,offset = 1.0,0.0
    
    orig_val = cfg[data_field.name].value
    new_val = orig_val*cfg[data_field.name].scf - offset
    cfg[data_field.name].value = new_val
    if debug:
        print("=== field %s ===" % data_field)
        print("old-val=%f" % orig_val);
        print("scf=%f" % cfg[data_field.name].scf)
        print("gain=%f" % gain)
        print("offset=%f" % offset)
        print("new-val=%f" % cfg[data_field.name].value);
        print("=> updated data field %s: %f -> %f" % (data_field, \
                                                    orig_val, \
                                                    cfg[data_field.name].value))
Example #9
0
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))
Example #10
0
def compute_expression_fields(board,adp,cfg,compensate=True, debug=False):
    #print(cfg)
    blk = board.get_block(cfg.inst.block)

    if(len(blk.data) < 1):
        return

    data = list(filter(lambda d: d.type == blocklib.BlockDataType.EXPR, \
                        blk.data))

    if(len(data) < 1):
        return

    assert(len(data) == 1)
    # only allowed to have one output.
    output_port = blk.outputs.singleton()
    calib_obj = llenums.CalibrateObjective(adp.metadata[adplib.ADPMetadata.Keys.RUNTIME_CALIB_OBJ])

    rel = output_port.relation[cfg.mode]
    fn_call= util.singleton(filter(lambda n: n.op == oplib.OpType.CALL, rel.nodes()))
    fn_spec=fn_call.func
    fn_params = fn_call.values
    data_expr_field_name = util.singleton(fn_spec.expr.vars())
    data_expr_field = cfg.get(data_expr_field_name)

    # build offset map
    repls = {}
    for input_port, func_arg in zip(fn_call.values,fn_spec.func_args):
        if compensate:
            assert(input_port.op == oplib.OpType.VAR)
            conn = util.singleton(adp.incoming_conns(blk.name, cfg.inst.loc, input_port.name))
            src_block = board.get_block(conn.source_inst.block)
            src_cfg = adp.configs.get(conn.source_inst.block, conn.source_inst.loc)
            model = get_experimental_model(board, \
                                           src_block, \
                                           conn.source_inst.loc, \
                                           src_cfg, \
                                           calib_obj=calib_obj)
            gain,offset = get_compensation_parameters(model,init_cond=False)
        else:
            gain,offset = 1.0,0.0

        inj = data_expr_field.injs[func_arg]

        repls[func_arg] = genoplib.Mult(genoplib.Const(inj), \
                                        genoplib.Add( \
                                                      genoplib.Var(func_arg), \
                                                      genoplib.Const(-offset)
                                                     ))

        if debug:
            print("inp-var %s inj=%f offset=%f" % (func_arg,inj,offset))

    # compute model of output block
    if compensate:
        conn = util.singleton(adp.outgoing_conns(blk.name, cfg.inst.loc, output_port.name))
        dest_block = board.get_block(conn.dest_inst.block)
        dest_cfg = adp.configs.get(conn.dest_inst.block, conn.dest_inst.loc)

        model = get_experimental_model(board, \
                                       dest_block, \
                                       dest_cfg.inst.loc, \
                                       dest_cfg, \
                                       calib_obj=calib_obj)
        gain,offset = get_compensation_parameters(model,False)
    else:
        gain,offset = 1.0,0.0

    inj = data_expr_field.injs[data_expr_field.name]
    if debug:
        print("out-var %s inj=%f gain=%f offset=%f" % (data_expr_field.name, \
                                                    inj,gain,offset))

    func_impl = genoplib.Mult(genoplib.Const(inj), \
                              data_expr_field.expr.substitute(repls))
    if debug:
        print("func-expr: %s" % func_impl)

    rel_impl = genoplib.Add( \
                        rel.substitute({data_expr_field.name:func_impl}), \
                        genoplib.Const(-offset/gain) \
                       )
    output_port = blk.outputs.singleton()
    input_port = blk.inputs.singleton()
    final_expr = rel_impl.concretize()

    if debug:
        print("rel-expr: %s" % rel_impl)
        print("--- building lookup table ---")
        print(final_expr)

    input_values = input_port.quantize[cfg.mode] \
                             .get_values(input_port \
                                         .interval[cfg.mode])
    cfg[data_expr_field.name].set_input(input_port,input_values)

    lut_outs = []
    for val in input_values:
        out = final_expr.compute({input_port.name:val})
        out = max(min(1.0-1.0/128.0,out),-1.0)
        cfg[data_expr_field.name].set_output({input_port.name:val},out)

    cfg[data_expr_field.name].concrete_expr = final_expr
Example #11
0
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)
Example #12
0
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)))
Example #13
0
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()
Example #14
0
def get_calibrate_objective(name):
    return llenums.CalibrateObjective(name)
Example #15
0
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)