Skip to content

fix: https://github.com/NVIDIA/Model-Optimizer/issues/981#983

Open
ChenhanYu wants to merge 3 commits intomainfrom
chenhany/fix_issue_981
Open

fix: https://github.com/NVIDIA/Model-Optimizer/issues/981#983
ChenhanYu wants to merge 3 commits intomainfrom
chenhany/fix_issue_981

Conversation

@ChenhanYu
Copy link
Collaborator

@ChenhanYu ChenhanYu commented Mar 5, 2026

What does this PR do?

Type of change: ? Bug fix

An issue is reported in #981 where str(v) on some TransformerConfig fields will raise TypeError. The fix is to catch this error and do repr(type(v)) instead.

Usage

# Add a code snippet demonstrating how to use this

Testing

Before your PR is "Ready for review"

Make sure you read and follow Contributor guidelines and your commits are signed (git commit -s -S).

Make sure you read and follow the Security Best Practices (e.g. avoiding hardcoded trust_remote_code=True, using torch.load(..., weights_only=True), avoiding pickle, etc.).

  • Is this change backward compatible?: ✅ / ❌ / N/A
  • If you copied code from any other source, did you follow IP policy in CONTRIBUTING.md?: ✅ / ❌ / N/A
  • Did you write any new necessary tests?: ✅ / ❌ / N/A
  • Did you update Changelog?: ✅ / ❌ / N/A

Additional Information

Summary by CodeRabbit

  • Bug Fixes
    • Improved checkpoint loading stability by handling unusual configuration values more gracefully; such values no longer cause failures and are skipped with a warning instead.
    • Reduced risk of crashes during configuration processing when encountering non-standard or unsupported objects.

Signed-off-by: Chenhan Yu <chenhany@nvidia.com>
@ChenhanYu ChenhanYu self-assigned this Mar 5, 2026
@ChenhanYu ChenhanYu requested a review from a team as a code owner March 5, 2026 19:17
@ChenhanYu ChenhanYu requested a review from kevalmorabia97 March 5, 2026 19:17
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 5, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 3793627b-3874-4a6f-87d5-d9ce97ae00b9

📥 Commits

Reviewing files that changed from the base of the PR and between 014c1a0 and 6e567e6.

📒 Files selected for processing (1)
  • modelopt/torch/opt/plugins/mcore_dist_checkpointing.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • modelopt/torch/opt/plugins/mcore_dist_checkpointing.py

📝 Walkthrough

Walkthrough

The _parse_transformer_config function in the Megatron-Core distributed checkpointing plugin now attempts str(v) inside a try/except that catches AttributeError and TypeError; if conversion raises, a warning is logged and that config key is left out of the stringified mapping.

Changes

Cohort / File(s) Summary
Dist checkpoint parsing
modelopt/torch/opt/plugins/mcore_dist_checkpointing.py
In _parse_transformer_config wrap str(v) in try/except catching AttributeError/TypeError; on exception logs a warning and skips converting that key instead of crashing.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Title check ⚠️ Warning The PR title is a bare issue URL with no descriptive summary of the actual change, making it unclear to reviewers scanning history what the fix addresses. Replace the issue URL with a clear, concise title describing the fix, such as: 'fix: Handle TypeError when converting TransformerConfig fields to string' or 'fix: Add try-except for str(v) conversion in _parse_transformer_config'
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Security Anti-Patterns ✅ Passed No security anti-patterns detected. The change adds try/except error handling for type conversion in _parse_transformer_config, which is a defensive programming improvement with no security risks.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch chenhany/fix_issue_981

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@modelopt/torch/opt/plugins/mcore_dist_checkpointing.py`:
- Around line 151-157: Remove the stray pre-try conversion so the fallback runs:
delete the initial `config[k] = str(v)` before the try block and leave the
try/except that attempts `str(v)` and on `AttributeError`/`TypeError` sets
`config[k] = repr(type(v))`; update the block around `config`, `k`, and `v` in
`mcore_dist_checkpointing.py` so only the `try: config[k] = str(v)` / `except
(AttributeError, TypeError): config[k] = repr(type(v))` sequence exists.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 782435f7-d420-4661-8805-3bf2d93883dd

📥 Commits

Reviewing files that changed from the base of the PR and between 31f0783 and 6ab53db.

📒 Files selected for processing (1)
  • modelopt/torch/opt/plugins/mcore_dist_checkpointing.py

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Keval Morabia <28916987+kevalmorabia97@users.noreply.github.com>
@codecov
Copy link

codecov bot commented Mar 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.12%. Comparing base (edde087) to head (6e567e6).
⚠️ Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #983      +/-   ##
==========================================
+ Coverage   72.10%   72.12%   +0.02%     
==========================================
  Files         209      209              
  Lines       23629    23628       -1     
==========================================
+ Hits        17037    17042       +5     
+ Misses       6592     6586       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Signed-off-by: Chenhan Yu <chenhany@nvidia.com>
config[k] = str(v)
except (AttributeError, TypeError):
config[k] = repr(type(v))
print("Warning: TransformerConfig.{} does not have _repr_ implemented.")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you mean to add something to the empty {}?

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 this pull request may close these issues.

2 participants