Skip to content

Read config.dtype instead of the deprecated config.torch_dtype alias - #3049

Merged
kevalmorabia97 merged 2 commits into
mainfrom
kmorabia/config-dtype-cleanup
Oct 27, 2026
Merged

kevalmorabia97 merged 2 commits into
mainfrom
kmorabia/config-dtype-cleanup

Conversation

@kevalmorabia97

@kevalmorabia97 kevalmorabia97 commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Type of change: Bug fix

config.torch_dtype is a deprecated alias of config.dtype (transformers>=5.5) that returns None when unset instead of raising, so a getattr(config, "torch_dtype", default) never reaches its default. This reads config.dtype directly in:

  • modelopt/torch/export/plugins/hf_spec_export.py: the DFlash export wrote the string "None" as torch_dtype for a base config without a dtype; it now falls back to bfloat16.
  • modelopt/torch/export/unified_export_hf.py: _resolve_export_dtype now reads config.dtype (behavior unchanged, no deprecation warning).
  • modelopt/torch/opt/plugins/transformers.py: dropped the redundant torch_dtype fallback.

Left unchanged: the "torch_dtype" keys written to exported config.json files and the eagle/spec-dec default configs (on-disk format), and the legacy torch_dtype= kwarg accepted by init_quantized_weights (public API; handled in #2161).

Related: #2161 (same defect in init_quantized_weights).

Usage

N/A, no API change.

Testing

  • tests/unit/torch/export/test_hf_spec_rope_export.py and test_unified_export_hf.py: 69 passed (CPU). The new test_dflash_torch_dtype_inherits_base_or_defaults uses a real LlamaConfig and fails without the fix for the unset-dtype case.
  • tests/unit/torch/opt transformers tests fail at collection with and without this change in my environment (TypeError on None), so the trainer change is covered only by inspection.

Before your PR is "Ready for review"

  • Is this change backward compatible?: ✅
  • Did you write any new necessary tests?: ✅
  • Did you update Changelog?: N/A (internal fix)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Exported models now use the configured data type when available, with a bfloat16 fallback when the configuration does not specify one.
    • Data type selection and mismatch warnings now reflect the model’s configured data type.
    • Export behavior is more consistent when configurations provide a data type through the current configuration field, including when the value is missing or None.

config.torch_dtype is an alias of config.dtype that returns None when unset
instead of being absent, so getattr defaults on it never apply. Read
config.dtype directly in the DFlash export, _resolve_export_dtype and the
HF trainer.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Keval Morabia <[email protected]>
@kevalmorabia97
kevalmorabia97 requested review from a team as code owners October 27, 2026 13:24
@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between c254c0c and e029caf.

Files selected for processing: 1
  • tests/unit/torch/export/test_hf_spec_rope_export.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 10 remain after this review.

Walkthrough

Export and trainer code now read configured dtype from config.dtype. DFlash export uses torch.bfloat16 when the base config dtype is unset. Tests cover this fallback and update configured dtype fixtures.

Changes: Configured dtype handling

Layer / File(s) Summary
Use config.dtype for dtype selection
modelopt/torch/export/plugins/hf_spec_export.py, modelopt/torch/export/unified_export_hf.py, modelopt/torch/opt/plugins/transformers.py, tests/unit/torch/export/test_hf_spec_rope_export.py, tests/unit/torch/export/test_unified_export_hf.py
Export and trainer code read configured dtype from config.dtype. DFlash export falls back to torch.bfloat16 when the base config dtype is None. Tests check the fallback and use dtype in configured dtype fixtures.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: chenhanyu

Merge Risk: Minimal · up to e029c

The PR is mergeable; no confirmed correctness, availability, or dependency-collection risk remains.

Pre-merge checks: Passed 5 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surv… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 5 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the primary change: replacing deprecated config.torch_dtype reads with config.dtype.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Security Anti-Patterns Passed The authoritative PR diff changes only dtype access and tests. It adds no torch.load(..., weights_only=False), numpy.load(..., allow_pickle=True), hardcoded trust_remote_code=True, external-inpu…
Full details: Security Anti-Patterns

Explanation

The authoritative PR diff changes only dtype access and tests. It adds no torch.load(..., weights_only=False), numpy.load(..., allow_pickle=True), hardcoded trust_remote_code=True, external-input eval()/exec(), or # nosec. It changes no pyproject.toml or requirements file. Therefore, it introduces none of the listed security anti-patterns.

Full details: Docstring Coverage

Explanation

Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 6 functions across 5 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@meenchen meenchen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bot review (gpt-6.1-sol) — DM the bot to share feedback.

Changes requested: the new unconditional Transformers import breaks collection of otherwise dependency-light export tests when the optional HF extra is absent.

Needs action:

  • Guard the LlamaConfig dependency in test_hf_spec_rope_export.py so only the real-config regression skips without Transformers; preserve collection and execution of the other tests.

No action needed:

  • The dtype readers match the supported Transformers >=5.5 API, and the DFlash fallback handles an unset dtype correctly.
  • Existing fixture edits are justified by the field migration; assertions remain intact. Tests were inspected, not executed.

transformers is an optional dependency, so import it inside the one test that
needs it instead of at module level, which broke collection of the other
export tests without the hf extra.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Keval Morabia <[email protected]>

@meenchen meenchen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bot review (gpt-6.1-sol) — DM the bot to share feedback.

LGTM: the prior optional-dependency collection issue is resolved; human sign-off remains required for the justified existing-test edits.

Needs action:

  • Confirm the existing export-test fixture migrations from torch_dtype to dtype; they match the supported API change and preserve all assertions.

No action needed:

  • ✔️ Resolved the critical collection issue: LlamaConfig is accessed through test-local pytest.importorskip("transformers"), leaving other tests collectable without the HF extra.
  • The DFlash regression covers explicit float16 and unset-dtype bfloat16 fallback. Tests were inspected, not executed.

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-10-27 22:12 UTC

@codecov

codecov Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.43%. Comparing base (35922f1) to head (e029caf).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3049      +/-   ##
==========================================
- Coverage   79.97%   79.43%   -0.54%     
==========================================
  Files         661      661              
  Lines       75419    75419              
==========================================
- Hits        60315    59911     -404     
- Misses      15104    15508     +404     
Flag Coverage Δ
examples-diffusers 19.91% <0.00%> (-0.01%) ⬇️
examples-gpt-oss 13.45% <50.00%> (+0.01%) ⬆️
examples-hf_ptq 23.45% <50.00%> (+0.07%) ⬆️
examples-llm_distill 13.53% <50.00%> (+0.01%) ⬆️
examples-llm_eval 17.21% <50.00%> (+0.01%) ⬆️
examples-llm_qat 17.38% <100.00%> (-0.03%) ⬇️
examples-llm_sparsity 16.74% <50.00%> (-0.01%) ⬇️
examples-megatron_bridge 26.46% <0.00%> (-0.28%) ⬇️
examples-specdec_bench 13.23% <0.00%> (+0.01%) ⬆️
examples-speculative_decoding 17.46% <50.00%> (-0.05%) ⬇️
examples-torch_onnx 21.23% <0.00%> (-0.03%) ⬇️
examples-torch_trt 15.20% <0.00%> (+0.02%) ⬆️
examples-vllm_serve 17.42% <50.00%> (+0.01%) ⬆️
gpu 59.11% <50.00%> (-1.19%) ⬇️
regression 15.03% <0.00%> (+0.01%) ⬆️
unit 61.44% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.

@kevalmorabia97
kevalmorabia97 merged commit f03651e into main Oct 27, 2026
55 checks passed
@kevalmorabia97
kevalmorabia97 deleted the kmorabia/config-dtype-cleanup branch October 27, 2026 22:12
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.

3 participants