SNPE TensorFlow Converter API 笔记

博客围绕DlcConverter展开,介绍了其下的两个函数。_resolve_graph_operations_from_model会遍历Graph中所有operation,从output_nodes开始,按规则将操作加入列表并返回;_resolve_descriptors_from_nodes则先找layer_resolvers,再根据tf.Operation列表构建TFGraphBuilder,重新建立节点联系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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_maptf.Operation nameTFOperationNode 的组成一个字典。node.inputs.append(input_node) 构建 node 的 input 列表,完成 link 的建立。

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值