Skip to content

Fix optimize.py crash when the model path has no directory component - #3040

Open
Mohammed Alkindi (MohammedAlkindi) wants to merge 1 commit into
microsoft:mainfrom
MohammedAlkindi:fix/optimize-tool-bare-filename
Open

Fix optimize.py crash when the model path has no directory component#3040
Mohammed Alkindi (MohammedAlkindi) wants to merge 1 commit into
microsoft:mainfrom
MohammedAlkindi:fix/optimize-tool-bare-filename

Conversation

@MohammedAlkindi

Copy link
Copy Markdown
Contributor

tools/optimize.py calls os.chdir(os.path.dirname(path)) unconditionally. For a bare filename os.path.dirname returns '', and os.chdir('') raises. That is the usage the module docstring documents:

Usage:
    python optimize.py model.onnx optimized_model.onnx

Before, run from the model's own directory against a real checker-valid model:

  File "tools/optimize.py", line 30, in main
    os.chdir(model_dir)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: ''

After, the same command exits 0 and writes optimized_model.onnx, which onnx.checker.check_model reloads 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 no tools/*_test.py, noxfile.py's test session does not reach it, and pyproject.toml groups tools with examples,docs,utils,opgen as supporting code. Adding a test file to a directory the project does not test looked like scope creep rather than the requested fix.

ruff check and ruff format --check on the changed file: 0 violations. lintrunner -a --take RUFF: no lint issues.

🤖 Generated with Claude Code

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 thread tools/optimize.py
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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
os.chdir(model_dir)
model = onnx_ir.load(path)

could you instead update to this? This file has been outdated.

Comment thread tools/optimize.py
if model_dir:
os.chdir(model_dir)
model = onnxscript.optimizer.optimize(model)
model = onnx.inliner.inline_local_functions(model)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
model = onnx.inliner.inline_local_functions(model)

Comment thread tools/optimize.py
Comment on lines 34 to 35
# Optimize again in case inlining created new opportunities.
model = onnxscript.optimizer.optimize(model)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Comment thread tools/optimize.py
Comment on lines 37 to 38
os.chdir(pwd)
onnx.save(model, output_path)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

construct the external_data path according to the output model name

Suggested change
onnx_ir.save(model, output_path, external_data=...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

2 participants