Skip to content

fix(notebook): resolve execution-order dependency for optimized_models_dict in SD3 Torch.FX notebook#3365

Closed
Aadik1ng wants to merge 2 commits intoopenvinotoolkit:latestfrom
Aadik1ng:fix-stable-diffusion-v3-torch-fx-lint
Closed

fix(notebook): resolve execution-order dependency for optimized_models_dict in SD3 Torch.FX notebook#3365
Aadik1ng wants to merge 2 commits intoopenvinotoolkit:latestfrom
Aadik1ng:fix-stable-diffusion-v3-torch-fx-lint

Conversation

@Aadik1ng
Copy link
Copy Markdown

Description

Fixes an execution-order dependency issue in the Stable Diffusion v3 Torch.FX notebook where optimized_models_dict was used before being defined, causing a potential NameError during quantization workflows.

Changes

  • Moved initialization of optimized_models_dict into the quantization section (Cell 19) under the conditional %%skip not $to_quantize.value block
  • Removed the duplicate/late initialization from Cell 21
  • Restored missing numpy import to ensure downstream cells execute correctly
  • Cleaned up minor linting issues (unused/misplaced imports)

Impact

  • Prevents runtime errors during quantization flow
  • Aligns variable scope with the notebook’s conditional execution design
  • Improves robustness for interactive and non-linear notebook execution

Type of Change

  • Bug fix
  • Code quality improvement

Validation

  • Verified logical execution flow for both quantized and non-quantized paths
  • Confirmed no undefined variable usage remains
  • Ensured required dependencies are imported before use

Notes

This change preserves the original intent of conditional execution while ensuring the notebook behaves reliably in typical interactive usage scenarios.

- Relocated initialization of 'optimized_models_dict' to ensure it's defined before first usage in calibration data collection.
- Ensured 'optimized_models_dict' is correctly scoped within the quantization-only sections to avoid NameErrors.
- Restored necessary 'numpy' imports and removed unused/redundant imports found by nbqa flake8.
- Improved overall notebook robustness for non-linear execution.
@review-notebook-app
Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@github-actions
Copy link
Copy Markdown
Contributor

This PR will be closed in a week because of 2 weeks of no activity.

@github-actions github-actions Bot added the Stale label Apr 23, 2026
@aleksandr-mokrov
Copy link
Copy Markdown
Collaborator

Thank you for the contribution! However, I don't think this change is correct — it would actually break the quantization flow.

The original code is intentional and works correctly. All cells using optimized_models_dict are guarded by %%skip not $to_quantize.value, so they execute sequentially when quantization is enabled, and the dict is defined in Cell 21 before it's used in Cell 22. There is no execution-order issue.

The PR removes critical lines. By deleting these three assignments from Cell 21:

optimized_models_dict["vae"] = vae
optimized_models_dict["text_encoder"] = text_encoder
optimized_models_dict["text_encoder_2"] = text_encoder_2

the dict only contains the "transformer" key, so Cell 22 would immediately crash with a KeyError when accessing optimized_models_dict["text_encoder"].

Closing as the change fixes a problem that doesn't exist and introduces a regression. The README casing fix ("Openvino" → "OpenVINO") is valid though — feel free to open a separate small PR for that!

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants