Example #1
0
    def cache_intermediates(self, G, input_tensor, cache=True):
        outputs = []
        fusion_outputs = []

        for _, _, _, output, fusion_op_name, _, _ in\
            execute_iterator(G, [input_tensor], disable_cache=True):

            if fusion_outputs:
                # handle end of fusion
                if fusion_op_name is None:
                    # python upvalues are not as smart as lua
                    outputs.append([out.copy() for out in fusion_outputs])
                    fusion_outputs.clear()
                    continue
            # handle start or continuation of fusion
            if fusion_op_name is not None:
                fusion_outputs.append([out.copy() for out in output])
                continue

            outputs.append([out.copy() for out in output])

        input_hash = IntermediateCache.__get_hash(G, input_tensor)
        cache_path_prefix = self.cache_path_prefix(input_tensor, input_hash)
        self.write_entry(cache_path_prefix, input_tensor, outputs)

        if cache:
            self.add_to_cache(input_hash, (input_tensor, outputs))
            return self.cache[input_hash]
        return None
Example #2
0
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
Example #3
0
    def _collect(self, G, input_tensors):
        stats = OrderedDict()
        for _, _, node, output, _, fusion_node, details in\
            execute_iterator(G, input_tensors, disable_cache=True):

            key = NodeId(node, fusion_node)
            node = (node if fusion_node is None else fusion_node)
            stat = gather_stats(
                output[0], force_ideal=not isinstance(node, InputParameters))
            if isinstance(node, FilterParameters) and details:
                stat['min_acc'] = details['min_acc']
                stat['max_acc'] = details['max_acc']
            stats[key] = stat

        return stats
Example #4
0
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
Example #5
0
    def _collect_execution(self, G, tensors, qrecs=None, qmode=None):
        outputs = []
        fusion_outputs = []
        for step_idx, step, node, output, fusion_op_name, fusion_node, details in\
            execute_iterator(G, tensors, limit=self._limit, qrecs=qrecs, qmode=qmode):
            if qrecs:
                qrec = qrecs[NodeId(node, fusion_node)]
                output = [
                    qrec.out_qs[i].dequantize(out)
                    for i, out in enumerate(output)
                ]
            else:
                output = output.copy()

            del step, fusion_op_name
            if fusion_node:
                fusion_outputs.append({
                    "name":
                    "",
                    "step_idx":
                    "{}_{}".format(step_idx, len(fusion_outputs)),
                    "node":
                    fusion_node,
                    "output":
                    output,
                    "details":
                    details
                })
            else:
                stat = {
                    "name": node.name,
                    "step_idx": str(step_idx),
                    "node": node,
                    "output": output,
                    "details": details,
                    "fusion_outputs": []
                }
                if len(fusion_outputs) > 0:
                    stat['fusion_outputs'] = fusion_outputs.copy()
                    fusion_outputs.clear()
                outputs.append(stat)
        return outputs