Пример #1
0
    def _dump_graph(self, graph: fbs.Graph):
        '''
        Process one level of the Graph, descending into any subgraphs when they are found
        '''

        self._dump_initializers(graph)
        self._dump_nodeargs(graph)
        print('Nodes:')
        for i in range(0, graph.NodesLength()):
            node = graph.Nodes(i)
            self._dump_node(node)

            # Read all the attributes
            for j in range(0, node.AttributesLength()):
                attr = node.Attributes(j)
                attr_type = attr.Type()
                if attr_type == fbs.AttributeType.AttributeType.GRAPH:
                    print(
                        f'## Subgraph for {node.OpType().decode()}.{attr.Name().decode()} ##'
                    )
                    self._dump_graph(attr.G())
                    print(
                        f'## End {node.OpType().decode()}.{attr.Name().decode()} Subgraph ##'
                    )
                elif attr_type == fbs.AttributeType.AttributeType.GRAPHS:
                    # the ONNX spec doesn't currently define any operators that have multiple graphs in an attribute
                    # so entering this 'elif' isn't currently possible
                    print(
                        f'## Subgraphs for {node.OpType().decode()}.{attr.Name().decode()} ##'
                    )
                    for k in range(0, attr.GraphsLength()):
                        print(f'## Subgraph {k} ##')
                        self._dump_graph(attr.Graphs(k))
                        print(f'## End Subgraph {k} ##')
Пример #2
0
    def _dump_nodeargs(self, graph: fbs.Graph):
        print('NodeArgs:')
        for idx in range(0, graph.NodeArgsLength()):
            node_arg = graph.NodeArgs(idx)
            type = node_arg.Type()
            if not type:
                # NodeArg for optional value that does not exist
                continue

            type_str = FbsTypeInfo.typeinfo_to_str(type)
            value_type = type.ValueType()
            value = type.Value()
            dims = None
            if value_type == fbs.TypeInfoValue.TypeInfoValue.tensor_type:
                tensor_type_and_shape = fbs.TensorTypeAndShape.TensorTypeAndShape(
                )
                tensor_type_and_shape.Init(value.Bytes, value.Pos)
                shape = tensor_type_and_shape.Shape()
                if shape:
                    dims = []
                    for dim in range(0, shape.DimLength()):
                        d = shape.Dim(dim).Value()
                        if d.DimType(
                        ) == fbs.DimensionValueType.DimensionValueType.VALUE:
                            dims.append(str(d.DimValue()))
                        elif d.DimType(
                        ) == fbs.DimensionValueType.DimensionValueType.PARAM:
                            dims.append(d.DimParam().decode())
                        else:
                            dims.append('?')
            else:
                dims = None

            print(f'{node_arg.Name().decode()} type={type_str} dims={dims}')
        print('--------')
Пример #3
0
    def _process_graph(self, graph: fbs.Graph,
                       outer_scope_value_typeinfo: dict):
        '''
        Process one level of the Graph, descending into any subgraphs when they are found
        :param outer_scope_value_typeinfo: Outer scope NodeArg dictionary from ancestor graphs
        '''
        # Merge the TypeInfo for all values in this level of the graph with the outer scope value TypeInfo.
        value_name_to_typeinfo = OrtFormatModelProcessor._setup_type_info(
            graph, outer_scope_value_typeinfo)

        for i in range(0, graph.NodesLength()):
            node = graph.Nodes(i)

            optype = node.OpType().decode()
            domain = node.Domain().decode(
            ) or 'ai.onnx'  # empty domain defaults to ai.onnx

            self._add_required_op(domain, node.SinceVersion(), optype)

            if self._op_type_processors:
                self._op_type_processors.process_node(node,
                                                      value_name_to_typeinfo)

            # Read all the attributes
            for j in range(0, node.AttributesLength()):
                attr = node.Attributes(j)
                attr_type = attr.Type()
                if attr_type == fbs.AttributeType.AttributeType.GRAPH:
                    self._process_graph(attr.G(), value_name_to_typeinfo)
                elif attr_type == fbs.AttributeType.AttributeType.GRAPHS:
                    # the ONNX spec doesn't currently define any operators that have multiple graphs in an attribute
                    # so entering this 'elif' isn't currently possible
                    for k in range(0, attr.GraphsLength()):
                        self._process_graph(attr.Graphs(k),
                                            value_name_to_typeinfo)
Пример #4
0
    def _dump_initializers(self, graph: fbs.Graph):
        print('Initializers:')
        for idx in range(0, graph.InitializersLength()):
            tensor = graph.Initializers(idx)
            dims = []
            for dim in range(0, tensor.DimsLength()):
                dims.append(tensor.Dims(dim))

            print(
                f'{tensor.Name().decode()} data_type={tensor.DataType()} dims={dims}'
            )
        print('--------')
Пример #5
0
    def _setup_type_info(graph: fbs.Graph, outer_scope_value_typeinfo={}):
        '''
        Setup the node args for this level of Graph.
        We copy the current list which represents the outer scope values, and add the local node args to that
        to create the valid list of values for the current Graph.
        :param graph: Graph to create NodeArg list for
        :param outer_scope_value_typeinfo: TypeInfo for outer scope values. Empty for the top-level graph in a model.
        :return: Dictionary of NodeArg name to TypeInfo
        '''
        value_name_to_typeinfo = outer_scope_value_typeinfo.copy()
        for j in range(0, graph.NodeArgsLength()):
            n = graph.NodeArgs(j)
            value_name_to_typeinfo[
                n.Name()] = n.Type()  # TypeInfo for this NodeArg's name

        return value_name_to_typeinfo