Skip to content

Support root parameters in safetensors state dictionaries - #4990

Open
AaravMehta-07 wants to merge 1 commit into
pytorch:mainfrom
AaravMehta-07:codex/safetensors-root-parameters
Open

AaravMehta-07 wants to merge 1 commit into
pytorch:mainfrom
AaravMehta-07:codex/safetensors-root-parameters

Conversation

@AaravMehta-07

Copy link
Copy Markdown

Summary

Support root-level parameter names in the safetensors flatten/unflatten helpers.

A standalone nn.Linear has state-dict keys weight and bias, rather than 0.weight and 0.bias. Today, unflattening even its ordinary tensors raises ValueError while splitting the name. After Int8 quantization, flattening fails earlier with IndexError for the same assumption.

Use one prefix helper based on rpartition for both directions. Root subclass components become _weight_qdata, _weight_scale, etc.; existing nested component names such as 0._weight_qdata remain unchanged.

Add CPU regressions using real safetensors files, plain and Int8-quantized Linear models, root and nested names, restored forward outputs, and deferred reconstruction of incomplete quantized shards. No dependency or quantization-kernel changes.

Validation

  • Before the fix: 2 root round-trip regressions failed (ValueError / IndexError); 4 nested compatibility cases passed.
  • python -m pytest test/prototype/safetensors -q: 8 passed, 31 skipped. The existing GPU-gated tests skipped because this machine has no CUDA/XPU; no new skips were added.
  • Ruff 0.11.6 lint and formatting checks passed for the changed files; git diff --check passed.
  • Windows, Python 3.11.9, PyTorch 2.13.0+cpu. Actual Int8WeightOnlyConfig(version=2) tensors and their CPU forward operations were exercised. Existing import-time enum registration and test-collection TorchScript deprecation warnings occurred.

The full suite, GPU formats, native extensions, and hardware performance were not validated. Any required CLA remains for the submitting human to accept; no CLA acceptance is asserted here.

AI assistance (Codex) was used for investigation, implementation, regression tests, and validation.

Share prefix construction between flattening and reconstruction while preserving nested names. Cover real CPU plain/Int8 round trips, forward equivalence and deferred shard loading.

Co-authored-by: OpenAI Codex
Signed-off-by: Aarav <211792885+AaravMehta-07@users.noreply.github.com>
@pytorch-bot

pytorch-bot Bot commented Oct 10, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/4990

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Oct 10, 2026

Copy link
Copy Markdown

Hi @AaravMehta-07!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Oct 10, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 10, 2026

This branch has not been deployed

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant