Fix optimize.py crash when the model path has no directory component - #3040
Open
Mohammed Alkindi (MohammedAlkindi) wants to merge 1 commit into
Open
Conversation
os.path.dirname returns an empty string for a bare filename, and os.chdir('') raises OSError. That is the usage the module docstring documents: running the tool from the model's own directory.
Comment on lines
24
to
+31
| model = onnx.load(path, load_external_data=False) | ||
| # Hack: Change the working directory to the model directory so the optimizer | ||
| # can load external data files with relative paths. | ||
| # TODO: Remove this hack by fixing the optimizer to handle external data files properly. | ||
| pwd = os.getcwd() | ||
| model_dir = os.path.dirname(path) | ||
| os.chdir(model_dir) | ||
| if model_dir: | ||
| os.chdir(model_dir) |
Collaborator
There was a problem hiding this comment.
Suggested change
| os.chdir(model_dir) | |
| model = onnx_ir.load(path) |
could you instead update to this? This file has been outdated.
| if model_dir: | ||
| os.chdir(model_dir) | ||
| model = onnxscript.optimizer.optimize(model) | ||
| model = onnx.inliner.inline_local_functions(model) |
Collaborator
There was a problem hiding this comment.
Suggested change
| model = onnx.inliner.inline_local_functions(model) |
Comment on lines
34
to
35
| # Optimize again in case inlining created new opportunities. | ||
| model = onnxscript.optimizer.optimize(model) |
Collaborator
There was a problem hiding this comment.
Suggested change
Comment on lines
37
to
38
| os.chdir(pwd) | ||
| onnx.save(model, output_path) |
Collaborator
There was a problem hiding this comment.
construct the external_data path according to the output model name
Suggested change
| onnx_ir.save(model, output_path, external_data=...) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
tools/optimize.pycallsos.chdir(os.path.dirname(path))unconditionally. For a bare filenameos.path.dirnamereturns'', andos.chdir('')raises. That is the usage the module docstring documents:Before, run from the model's own directory against a real checker-valid model:
After, the same command exits 0 and writes
optimized_model.onnx, whichonnx.checker.check_modelreloads clean. A path that does carry a directory component still works, checked from an unrelated cwd.No test added:
tools/has no coverage here. There is notools/*_test.py,noxfile.py's test session does not reach it, andpyproject.tomlgroupstoolswithexamples,docs,utils,opgenas supporting code. Adding a test file to a directory the project does not test looked like scope creep rather than the requested fix.ruff checkandruff format --checkon the changed file: 0 violations.lintrunner -a --take RUFF: no lint issues.🤖 Generated with Claude Code