ThanatosShinji / onnx-tool

A parser, editor and profiler tool for ONNX models.
https://pypi.org/project/onnx-tool/
MIT License
383 stars 51 forks source link

Question about the function "__find_shape_tensors__" of the Graph class #14

Closed Treemann closed 1 year ago

Treemann commented 1 year ago

Hi, I am confused about these two lines in the definition of the member function "__find_shapetensors_\" of the Graph class.

if node.op_type == 'Shape':
    continue

If we skip the 'Shape' node, then the input of this node could not be traced and more previous nodes could not be added to "nextnodes" in the following code. Could you explain why skip this type of node?

                        for input in node.input:
                            if input not in self.initials and input in self.producedby.keys():
                                nextnodes.extend(self.producedby[input])
ThanatosShinji commented 1 year ago

Hi, 'shape tensors' means that the value of this tensor will be used to calculate the next tensor's dynamic shape. The code you listed is trying to backpropagate to find all these 'shape tensors'. As the Shape Node, the input tensor of this node can't be a 'shape tensor', which means that backpropagation should be stopped here.

Treemann commented 1 year ago

Thanks for your explanation.

I notice that the flag "node.shape_calc" is only used in the function "Graph.shape_infer", and since I call the function "model_profile" instead of "model_profile_v2", then "Graph.shape_infer" will not be called during my model profiling.

Then in the pull request I sent, my comment of these two lines should be kept as your original version.

if node.op_type == 'Shape':
    continue

Another question: Do you think "Size" is similar to "Shape" and should be added here and the assertion in Graph.shape_infer?

# if node.op_type == 'Shape':
if node.op_type in ("Shape", "Size"):
    continue
def shape_infer(self, inputs: {} = None):
      ...
            # assert (node.op_type in ('Shape', 'Slice'))
            assert (node.op_type in ('Shape', 'Slice', 'Size'))
      ...
ThanatosShinji commented 1 year ago

I notice that the flag "node.shape_calc" is only used in the function "Graph.shape_infer",

Please be aware that node_profilers.py will be deprecated in the future as it has many redundant calculations. Also, there will be only one 'model_profile_v2' function enabled.

Another question: Do you think "Size" is similar to "Shape" and should be added here and the assertion in Graph.shape_infer?

You made the point. 'Size' is similar to 'Shape'. But I found no model with 'Size'. So I thought it was not necessary to add this op.

Treemann commented 1 year ago

Yes, I notice that you want to deprecate node_profilers.py. Thanks for your clarification~