Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnboundLocalError: local variable 'idx' referenced before assignment #10

Closed
xenova opened this issue Jun 19, 2024 · 5 comments · Fixed by #11
Closed

UnboundLocalError: local variable 'idx' referenced before assignment #10

xenova opened this issue Jun 19, 2024 · 5 comments · Fixed by #11

Comments

@xenova
Copy link
Contributor

xenova commented Jun 19, 2024

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}")

@inisis
Copy link
Owner

inisis commented Jun 19, 2024

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.

@xenova xenova mentioned this issue Jun 19, 2024
@xenova
Copy link
Contributor Author

xenova commented Jun 19, 2024

@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
Copy link
Owner

inisis commented Jun 19, 2024

@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

@xenova
Copy link
Contributor Author

xenova commented Jun 19, 2024

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 :)

@inisis
Copy link
Owner

inisis commented Jun 19, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants