huggingface / huggingface/optimum-intel
[Code Quality] Replace Python runtime asserts with ValueError in ERNIE-Image model patchers
- Dominant language
- Jupyter Notebook
- Stars
- 620
- Forks
- 270
- Avg merge
- 3d 10h
- Merged PRs (30d)
- 22
Description
### Description
While auditing the ERNIE-Image export integration, I noted a few code quality concerns that should be addressed before the implementation is finalized:
1. **Asserts in Production Code:** In `optimum/exporters/openvino/model_patcher.py`, the `_ernie_image_rope_float32` function uses an `assert dim % 2 == 0` for input validation. In Python, `assert` statements are silently stripped when running in optimized mode (`python -O`). Because RoPE dimensions are critical for math correctness, this should use a standard `ValueError` raise.
2. **Variable Scoping Risks:** In `optimum/exporters/openvino/__main__.py`, `_is_ernie_image` is initialized dynamically only inside the `elif library_name == "diffusers":` block, but checked later in the function. While safe under the current execution paths, this is brittle and could lead to an `UnboundLocalError` if the branching logic is independently refactored in the future.
### Expected Behavior
- Production-level input validation should use explicit `Exception` raises.
- Variables used across multiple flow control blocks should have a top-level default initialized defensively.
### Proposed Solution
- Swapping the `assert` with a `ValueError` in the ERNIE model patcher, achieving parity with the rest of the file's conventions.
- Explicitly initializing `_is_ernie_image = False` at the top of the context block in `__main__.py`.
I would like to submit a PR to address these code quality issues.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.