inisis/OnnxSlim

`UnboundLocalError: local variable 'idx' referenced before assignment`

Closed this issue · 5 comments

When running

onnxslim test.onnx slimmed.onnx

on this file: test.zip

I get the following error:

Traceback (most recent call last):
  File "/usr/local/bin/onnxslim", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.10/dist-packages/onnxslim/cli/_main.py", line 271, in main
    slim(
  File "/usr/local/lib/python3.10/dist-packages/onnxslim/cli/_main.py", line 128, in slim
    model = optimize(model, skip_fusion_patterns)
  File "/usr/local/lib/python3.10/dist-packages/onnxslim/core/slim.py", line 111, in optimize
    model = optimize_model(graph, skip_fusion_patterns)
  File "/usr/local/lib/python3.10/dist-packages/onnxslim/core/optimizer.py", line 857, in optimize_model
    graph_constant_fold_inplace(graph)
  File "/usr/local/lib/python3.10/dist-packages/onnxslim/core/optimizer.py", line 164, in graph_constant_fold_inplace
    idx = 0 if idx == 1 else 1
UnboundLocalError: local variable 'idx' referenced before assignment

stemming from these lines of code:

elif node.op == "Expand":
if len(node.inputs) > 1 and isinstance(node.inputs[1], Constant) and np.all(node.inputs[1].values == 1):
idx = 0 if idx == 1 else 1
delete_node(node, idx)
logger.debug(f"removing Expand op: {node.name}")

I'm so sorry for that, you can remove line 161 and delete(node), I will sovle it soon, btw, your onnx mode seems invalid, it doesn't have weight.

@inisis Right - the actual model is too large, so I save the graph and weights separately. Luckily onnxslim only needs the graph to work.

import onnx
m = onnx.load('original.onnx')
onnx.save(m, 'small.onnx', save_as_external_data=True)

@inisis Right - the actual model is too large, so I save the graph and weights separately. Luckily onnxslim only needs the graph to work.

import onnx
m = onnx.load('original.onnx')
onnx.save(m, 'small.onnx', save_as_external_data=True)

@xenova so what does the output looks like after this modification

I did this:

  1. Generate large .onnx file (e.g., large_model.onnx)
  2. Save model and graph separately
import onnx
m = onnx.load('large_model.onnx')
onnx.save(m, 'model.onnx', save_as_external_data=True)
  1. Run onnxslim and overwrite original model.onnx: onnxslim model.onnx model.onnx
  2. Run the model normally

This works :)

I did this:

  1. Generate large .onnx file (e.g., large_model.onnx)
  2. Save model and graph separately
import onnx
m = onnx.load('large_model.onnx')
onnx.save(m, 'model.onnx', save_as_external_data=True)
  1. Run onnxslim and overwrite original model.onnx: onnxslim model.onnx model.onnx
  2. Run the model normally

This works :)

That's greate. Onnxslim will automatically save your onnx into graph and weight separately.