def __open_graph(self, args): graph_file = os.path.expanduser(args.nnfile) _, ext = os.path.splitext(graph_file) ext = ext.lower() if ext == self.STATE_EXTENSION: LOG.info("opening state file %s", graph_file) self.load_state_file(graph_file) else: opts = self.__get_opts(args) LOG.info("opening graph file %s load_quantization = %s", graph_file, opts['load_quantization']) G = create_graph(graph_file, opts=opts) G.add_dimensions() self.G = G self.graph_file = graph_file self._reset_history() self.settings['load_quantization'] = bool( opts['load_quantization']) if self.settings['adjust_order']: LOG.info("adjusting order") self.execute_adjust_order() if self.settings['weight_equalization']: LOG.info("equalizing weights") weight_equalization(self.G, self.settings['equalization_threshold'])
def test_fake_values_concat(concat_test_graph): G = create_graph(concat_test_graph, opts={"load_tensors": True}) G.add_dimensions() G.adjust_order() matcher = get_pow2_match_group() matcher.match(G) G.add_dimensions() G.constant_store.fake = True stats_collector = ActivationStatsCollector() stats_collector.collect_stats( G, [np.random.rand(*node.dims.shape) for node in G.input_nodes()]) astats = stats_collector.reduce_stats() stats_collector = FilterStatsCollector() fstats = stats_collector.collect_stats(G) quantizer = SymmetricQuantizer(astats, fstats, force_width=8) qrecs = quantizer.quantize(G) G.quantization = qrecs with tempfile.TemporaryDirectory() as tempdir: opts = { 'default_input_location': 'ARG_LOC_L2', 'default_output_location': 'ARG_LOC_L2', 'default_global_location': 'ARG_LOC_L3_HFLASH', 'default_local_location': 'AT_MEM_UNDEF', 'at_ver': 3, 'tensor_directory': tempdir } code_gen = CodeGenerator(G, DefaultNamingConvension(G), opts) print(default_template(G, code_generator=code_gen)) code_gen.write_constants()
def test_cross_simple(mnist_graph): G = create_graph(mnist_graph, opts={"load_tensors": True}) G.add_dimensions() groups, neurons = cl.discover_groups(G) assert groups and neurons, "Nothing discovered" cl.process_groups(groups) cl.update_parameters(neurons)
def test_graph_imu_auto_quant_and_execute_quant(): G = create_graph("tests/graph/imu.tflite", opts={"load_tensors": True}) G.add_dimensions() G.adjust_order() get_pow2_match_group().match(G) G.add_dimensions() stats_collector = ActivationStatsCollector() for input_file in ['tests/images/imu0.pgm']: input_tensor = import_data(input_file, offset=0, divisor=256, nptype='int16') stats_collector.collect_stats(G, [input_tensor]) astats = stats_collector.reduce_stats() stats_collector = FilterStatsCollector() fstats = stats_collector.collect_stats(G) quantizer = SymmetricQuantizer(astats, fstats, force_width=16) qrecs = quantizer.quantize(G) G.quantization = qrecs executer = GraphExecuter(G, qrecs=qrecs) for input_file in ['tests/images/imu0.pgm']: input_tensor = import_data(input_file, offset=0, divisor=256, nptype='int16') output_ = executer.execute([input_tensor], qmode=QuantizationMode.all())
def test_graph_execute_complex(ir_graph, ir_images): G = create_graph(ir_graph, opts={"load_tensors": True}) G.add_dimensions() input_tensor = import_data(ir_images[0], offset=0, divisor=255) input_tensor = input_tensor.reshape((80, 80, 1)) executer = GraphExecuter(G) executer.execute([input_tensor])
def test_graph_calc(mnist_graph, mnist_images): temp_graph = create_temporary_copy(mnist_graph) G = create_graph(temp_graph, opts={"load_tensors": True}) G.add_dimensions() input_tensor = import_data(mnist_images[0], height=28, width=28, divisor=128, offset=-1) input_tensor = input_tensor.reshape(28, 28, 1) # import data always returns C, H, W. We need H, W, C. stats_collector = ActivationStatsCollector() stats_collector.collect_stats(G, [input_tensor]) astats = stats_collector.reduce_stats() stats_collector = FilterStatsCollector() fstats = stats_collector.collect_stats(G) quantizer = SymmetricQuantizer(astats, fstats, force_width=8) qrecs = quantizer.quantize(G) G.quantization = qrecs dump_state(G) G = load_state(temp_graph) for k, v in G.quantization.items(): assert v == qrecs[k], "problem with " + str(k) assert G.quantization == qrecs
def test_temps_report(mnist_graph): G = create_graph(mnist_graph, opts={"load_tensors": True}) G.add_dimensions() stats_collector = TempsStatsCollector() stats = stats_collector.collect_stats(G) report = TempsReporter().report(G, stats) renderer = TextTableRenderer(maxwidth=200) print(report.render(renderer))
def test_filter_detailed_report(mnist_graph, mnist_images): G = create_graph(mnist_graph, opts={"load_tensors": True}) G.add_dimensions() stats_collector = FilterDetailedStatsCollector() stats = stats_collector.collect_stats(G) report = FilterDetailedStatsReporter().report(G, stats) renderer = TextTableRenderer(maxwidth=200) print(report.render(renderer))
def open_graph(self, file_path, **kwargs): G = create_graph(file_path, opts=kwargs) G.add_dimensions() pad_fuser = MatchFusePad() pad_fuser.match(G) G.add_dimensions() self.G = G self.graph_file = file_path self._reset_history()
def test_cross_fused(mnist_graph): G = create_graph(mnist_graph, opts={"load_tensors":True}) G.add_dimensions() matcher = MatchAllGapConv() matcher.match(G) G.add_dimensions() groups, neurons = cl.discover_groups(G) assert groups and neurons, "Nothing discovered" cl.process_groups(groups) cl.update_parameters(neurons)
def test_activation_report(mnist_graph, mnist_images): G = create_graph(mnist_graph, opts={"load_tensors": True}) G.add_dimensions() input_tensor = import_data(mnist_images[0], height=28, width=28, offset=0, divisor=255) input_tensor = input_tensor.reshape((28, 28, 1)) stats_collector = ActivationStatsCollector() stats_collector.collect_stats(G, [input_tensor]) report = ActivationReporter().report(G, stats_collector.reduce_stats()) renderer = TextTableRenderer(maxwidth=200) print(report.render(renderer))
def test_graph_calc_iterator_cached(value_cache, mnist_graph, mnist_images): G = create_graph(mnist_graph, opts={"load_tensors":True}) G.add_dimensions() input_tensor = import_data(mnist_images[0], height=28, width=28, offset=0, divisor=255) input_tensor = input_tensor.reshape((28, 28, 1)) normal_steps = 0 fusion_steps = 0 # pylint: disable=unused-variable for step_idx, step, node, output, fusion_op_name, fusion_params, details in\ execute_iterator(G, [input_tensor], value_cache=value_cache): if fusion_op_name is not None: fusion_steps += 1 else: normal_steps += 1 assert normal_steps == 10 and fusion_steps == 0
def test_graph_kws_auto_quant(kws_graph, kws_sounds): G = create_graph(kws_graph, opts={"load_tensors": True}) G.add_dimensions() G.adjust_order() get_std_match_group().match(G) G.add_dimensions() stats_collector = ActivationStatsCollector() for input_file in kws_sounds: data = import_data(input_file, offset=0, divisor=256, nptype='int16') stats_collector.collect_stats(G, [data]) astats = stats_collector.reduce_stats() stats_collector = FilterStatsCollector() fstats = stats_collector.collect_stats(G) quantizer = SimpleQuantizer(astats, fstats, force_width=16) qrecs = quantizer.quantize(G) G.quantization = qrecs
def test_graph_kws(kws_graph, kws_sounds): G = create_graph(kws_graph, opts={"load_tensors": True}) G.add_dimensions() input_tensor = import_data(kws_sounds[0], offset=0, divisor=128, nptype='int16') normal_steps = 0 fusion_steps = 0 # pylint: disable=unused-variable for step_idx, step, node, output, fusion_op_name, fusion_params, details in\ execute_iterator(G, [input_tensor]): if fusion_op_name is not None: fusion_steps += 1 else: normal_steps += 1 assert normal_steps == 9 and fusion_steps == 0
def test_graph_calc(mnist_graph, mnist_images): G = create_graph(mnist_graph, opts={"load_tensors": True}) G.add_dimensions() input_tensor = import_data(mnist_images[0], height=28, width=28, offset=0, divisor=255) input_tensor = input_tensor.reshape((28, 28, 1)) normal_steps = 0 fusion_steps = 0 # pylint: disable=unused-variable executer = GraphExecuter(G) for step_idx, pnode, fnode, output_tensors, details in\ executer.execute_iterator([input_tensor]): if fnode is not None: fusion_steps += 1 else: normal_steps += 1 assert normal_steps == 10 and fusion_steps == 0
def test_simple_quantization(mnist_graph, mnist_images): G = create_graph(mnist_graph, opts={"load_tensors": True}) G.add_dimensions() input_tensor = import_data(mnist_images[0], height=28, width=28, offset=0, divisor=255) input_tensor = input_tensor.reshape((28, 28, 1)) stats_collector = ActivationStatsCollector() stats_collector.collect_stats(G, [input_tensor]) astats = stats_collector.reduce_stats() stats_collector = FilterStatsCollector() fstats = stats_collector.collect_stats(G) quantizer = SymmetricQuantizer(astats, fstats, force_width=8) qrecs = quantizer.quantize(G) assert len(qrecs) == 11 # One more for saved quantizer report = QuantizationReporter().report(G, qrecs) renderer = TextTableRenderer(maxwidth=200) print(report.render(renderer))
def test_equivalence(mnist_graph, mnist_images): G = create_graph(mnist_graph, opts={"load_tensors": True}) G.add_dimensions() G.adjust_order() G.add_dimensions() input_tensor = import_data(mnist_images[0], height=28, width=28, divisor=255, offset=0, transpose=False) executer = GraphExecuter(G) output_ = executer.execute([input_tensor]) with open("tests/h5_pickles/weights.pickle", 'rb') as fp: verif_weights = pickle.load(fp) assert np.array_equal(verif_weights[0]['weights'], G.graph_state.steps[1]['node'].weights) assert np.array_equal(verif_weights[0]['biases'], G.graph_state.steps[1]['node'].biases) assert np.array_equal(verif_weights[3]['weights'], G.graph_state.steps[4]['node'].weights) assert np.array_equal(verif_weights[3]['biases'], G.graph_state.steps[4]['node'].biases) assert np.array_equal(verif_weights[7]['weights'], G.graph_state.steps[7]['node'].weights) assert np.array_equal(verif_weights[7]['biases'], G.graph_state.steps[7]['node'].biases) with open( os.path.join("tests/h5_pickles", os.path.basename(mnist_images[0]) + '.pickle'), 'rb') as fp: verif = pickle.load(fp) assert all([ np.max(np.abs(verif[idx][0] - output_[idx][0])) < 0.00001 for idx in range(7) ]) # checking the Flatten layer doesn't work because the layout was not changed in the run tool # the layout for the output of the linear layer is a little different assert np.max(np.abs(verif[8][0] - output_[7][0].flatten())) < 0.00001 assert np.array_equal(np.round(output_[-1][0].flatten()), [1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
def test_cross_large(vww_graph, vww_images): G = create_graph(vww_graph, opts={"load_tensors": True}) G.add_dimensions() input_tensor = import_data(vww_images[4], offset=0, divisor=255) output1 = execute(G, [input_tensor]) groups, neurons = cl.discover_groups(G, do_relun=True) group_inputs = [ G.in_edges(grp[0][0]['name'])[0].from_node.step_idx for grp in groups ] group_outputs = [grp[-1][-1]['node'].step_idx for grp in groups] assert groups and neurons, "Nothing discovered" cl.process_groups(groups, threshold=0.0001) cl.update_parameters(neurons) output2 = execute(G, [input_tensor]) assert max( [np.max(np.abs(output1[i][0] - output2[i][0])) for i in group_inputs]) < 0.0001 assert max( [np.max(np.abs(output1[i][0] - output2[i][0])) for i in group_outputs]) < 0.0001 assert np.max(np.abs(output1[-1][0] - output2[-1][0])) < 0.0001
def save_state(temp_dir, width, fusions=False, adjust=False): file_name = os.path.join(temp_dir, "state_file") G = create_graph(MNIST_GRAPH, opts={"load_tensors":True}) G.add_dimensions() if adjust: G.adjust_order() if fusions: get_std_match_group().match(G) G.add_dimensions() stats_collector = ActivationStatsCollector() for input_file in MNIST_IMAGES: data = import_data(input_file, offset=0, divisor=255) if not adjust: data = data.reshape((28, 28, 1)) stats_collector.collect_stats(G, [data]) astats = stats_collector.reduce_stats() stats_collector = FilterStatsCollector() fstats = stats_collector.collect_stats(G) quantizer = SimpleQuantizer(astats, fstats, force_width=width) qrecs = quantizer.quantize(G) G.quantization = qrecs dump_state(G, include_parameters=True, state_path=file_name) return file_name
def __open_graph(self, graph_file, tensor_file, load_quantization, load_dequantized): graph_file = os.path.expanduser(graph_file) _, ext = os.path.splitext(graph_file) if ext == STATE_EXTENSION: LOG.info("opening state file %s", graph_file) self.graph_file = graph_file self.G, extra = load_state(graph_file, return_extra=True) self.settings.update(extra) else: LOG.info("opening graph file %s", graph_file) opts = { 'load_tensors': True, 'load_quantization': load_quantization, 'load_dequantized': load_dequantized } G = create_graph(graph_file, opts=opts) G.add_dimensions() if tensor_file: G.load_tensors(tensor_file) self.G = G self.graph_file = graph_file if tensor_file is not None: self.tensor_file = tensor_file self.settings['load_quantization'] = bool(load_quantization) if self.settings['adjust_order']: LOG.info("adjusting order") self.execute_adjust_order() if self.settings['weight_equalization']: LOG.info("equalizing weights") weight_equalization(self.G, self.settings['equalization_threshold'])
def load_state(graph_file: str, return_extra=False): graph_base, _ = os.path.splitext(graph_file) state_filename = graph_base + STATE_EXTENSION state_file = Path(state_filename) LOG.info("loading graph state from %s", state_filename) if not state_file.is_file(): raise ValueError("state file not found") with state_file.open('r') as json_fp: info_state = json.load(json_fp, cls=StateDecoder) info_state['info'] = convert_str_to_keys(info_state['info']) if 'node_options' in info_state: info_state['node_options'] = convert_str_to_keys( info_state['node_options']) else: info_state['node_options'] = {} if info_state['load_parameters']: pickle_filename = graph_base + ARRS_EXTENSION LOG.info("loading tensors from %s", pickle_filename) arrs_file = Path(pickle_filename) if not arrs_file.is_file(): raise ValueError("arrays file not found") with arrs_file.open('rb') as arrs_fp: parameters = pickle.load(arrs_fp) else: parameters = None # Here load the orignal graph and replay the transforms that were done to it if info_state['info'].get('has_quantized_parameters'): opts = {'load_tensors': True, 'load_quantization': True} else: opts = { 'load_tensors': False, } # Retrieve the identity of the saved state identity = GraphIdentity(None) identity.identity = info_state['identity'] LOG.info("loading graph from %s", identity.filename) G = create_graph(identity.filename, opts=opts) if 'name' in info_state: G.name = info_state['name'] G.add_dimensions() freeze_options = { k: v for k, v in info_state['node_options'].items() if 'FIXED_ORDER' in list(v.set_options) } set_options(G, freeze_options) if identity.is_adjusted: # If weights were saved then don't reshaoe them since it was already done # before they were saved LOG.info("adjusting dimensions") G.adjust_order(reshape_weights=not info_state['load_parameters']) G.add_dimensions() if identity.is_fused: LOG.info("fusing nodes") # replay the fusions that were carried out for fusion_name in identity.fusions: fusion = get_fusion(fusion_name) fusion.match(G) G.add_dimensions() set_parameters(G, parameters) # Update the identity to match the saved graph G.info = info_state['info'] G.changes.replay(G) G.graph_identity = identity G.node_options = info_state['node_options'] set_options(G, info_state['node_options'], info_state['node_options']) if identity.extracted_step is not None: extract_node(G, G.graph_state.steps[identity.extracted_step]['node']) G.add_dimensions() if return_extra: return G, info_state['extra'] return G
def test_graph_report(mnist_graph): G = create_graph(mnist_graph, opts={"load_tensors": True}) G.add_dimensions() report = GraphReporter().report(G, None) renderer = TextTableRenderer(maxwidth=200) print(report.render(renderer))