天天看点

SNPE TensorFlow Converter API 笔记DlcConverter

SNPE

  • DlcConverter
    • _resolve_graph_operations_from_model
    • _resolve_descriptors_from_nodes

DlcConverter

_resolve_graph_operations_from_model

遍历 Graph 中所有 operation,从 output_nodes 开始遍历,访问的 operation 加入到

visited

列表,并将遍历结果作为列表返回,具体遍历过程如下:

...
for output_op_name in model.out_nodes_names:
    queue = [operations_map[output_op_name]]
    visited = set()
    while len(queue) > 0:
        head = queue.pop(0)
        if head in visited:
            continue
        visited.add(head)

        if head in input_ops:
            continue

        for t in head.inputs:
            queue.append(t.op)

    all_ops_in_paths.update(visited)
           

从上述代码中可以知道,如果当前队列的 head operation 在

input_ops

中,则不会将该 head operation 加入到

visited

列表中。 head operation 访问完成以后,再将该 head operation 对应的 inputs 加入到待访问列表

queue

中:

for t in head.inputs:
    queue.append(t.op)
           

_resolve_descriptors_from_nodes

首先找到所有的 layer_resolvers,然后根据 tf.Operation 列表构建 TFGraphBuilder:

resolvers = self._create_layer_resolvers()

constructor = TFGraphBuilder(ops)
constructor.link_nodes()
           

constructor.link_nodes()

将所有 TFOperationNode 根据 tf.Operation 的 inputs 的关系重新建立起和 TensorFlow Graph 一样的节点联系, link_nodes 主要功能代码如下:

for tf_tensor in node.original_node.inputs:
    input_node = self.nodes_map.get(tf_tensor.op.name, None)
    if input_node is None:
        input_node = NonConsumableTFOperationNode(tf_tensor.op)
        self.nodes_map[tf_tensor.op.name] = input_node
    node.inputs.append(input_node)
           

上述代码中

original_node

即为

tf.Operation

类型,

nodes_map

tf.Operation name

TFOperationNode

的组成一个字典。

node.inputs.append(input_node)

构建 node 的 input 列表,完成 link 的建立。

继续阅读