Skip to content

Make DtensorShardOperation work on torch 2.6 and 2.7 - #49657

Merged
vasqu merged 2 commits into
huggingface:mainfrom
hazelxue:fix/sharding-utils-torch26-27
Oct 16, 2026
Merged

vasqu merged 2 commits into
huggingface:mainfrom
hazelxue:fix/sharding-utils-torch26-27

Conversation

@hazelxue

@hazelxue hazelxue commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

🤗 Transformers CI

What does this PR do?

Second half of #49651. After #49652, from_pretrained works again on torch 2.7, but tensor-parallel loading still raises on both declared-floor versions:

AttributeError: type object 'Shard' has no attribute 'local_shard_size_and_offset'

sharding_utils.py aliases Shard._local_shard_size_and_offset, which only exists from torch 2.8. torch 2.6 and 2.7 expose the same computation as Shard._local_shard_size_on_dim(size, num_chunks, rank, return_offset=True) (checked: identical results for the same inputs), so this falls back to that when the newer name is missing.

tests/utils/test_distributed_sharding_utils.py (CPU, no GPU needed):

torch before after
2.6.0 23 failed, 5 passed 3 failed, 11 passed
2.8.0 3 failed, 11 passed unchanged
2.14.1 14 passed unchanged

The 3 remaining failures on 2.6 are the _StridedShard cases, which also fail on 2.8 and 2.9 and only pass on recent torch, so they are not introduced here. If you would rather move setup.py to torch>=2.8 instead, happy to turn this into that change.

Before submitting

Who can review?

@michaelbenayoun @ArthurZucker

Shard._local_shard_size_and_offset only exists from torch 2.8; 2.6 and
2.7 expose the same computation as _local_shard_size_on_dim(...,
return_offset=True). Alias that one when the newer helper is missing, so
tensor-parallel loading does not raise AttributeError on the declared
torch floor. tests/utils/test_distributed_sharding_utils.py on torch 2.6
goes from 23 failures to the same 3 strided-shard cases torch 2.8 has.

Related to huggingface#49651
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution 🤗!

CI Security Gate — waiting for maintainer approval

CI is approved automatically only for authors who already have a contribution merged in this repository. A maintainer will review this PR and approve CI to run on it.

@3outeille
3outeille self-requested a review October 16, 2026 04:09

@3outeille 3outeille left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for flagging this. Some suggestions + edit in src/transformers/distributed/sharding_utils.py

@@ -207,7 +209,7 @@ class DtensorShardOperation:
         ...
-                    local_shard_size, local_shard_offset = Shard.local_shard_size_and_offset(
+                    local_shard_size, local_shard_offset = _local_shard_size_and_offset(
                         ...
                     )
@@ -227,7 +229,7 @@ class DtensorShardOperation:
-        local_flat_len, local_flat_start = Shard.local_shard_size_and_offset(flat_total_len, world_size, rank)
+        local_flat_len, local_flat_start = _local_shard_size_and_offset(flat_total_len, world_size, rank)

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Per review: pick the right torch API inside _local_shard_size_and_offset
and call it from both sites, rather than attaching an alias to Shard.
@hazelxue

Copy link
Copy Markdown
Contributor Author

Pushed e90e074 with @3outeille's suggestion: the alias on Shard is gone and both call sites now go through _local_shard_size_and_offset(). Re-ran tests/utils/test_distributed_sharding_utils.py on torch 2.6.0 / 2.8.0 / 2.14.1 with the same results as before (the 3 remaining strided-shard cases on 2.6 and 2.8 fail on main as well).

@3outeille 3outeille left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm ty

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 38856227604:2
Result: failure | Jobs: 15 | Tests: 202,379 | Failures: 0 | Duration: 15h 23m

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

sg ty!

@vasqu
vasqu merged commit 7bf3c5e into huggingface:main Oct 16, 2026
114 of 116 checks passed
@vasqu vasqu added the for patch Tag issues / labels that should be included in the next patch label Oct 16, 2026
@vasqu

vasqu commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

@hazelxue will include this in a patch, imo we should aim to support 2.6+ fully 🤗 so not raise torch version in setup.py

vasqu pushed a commit that referenced this pull request Oct 16, 2026
* Make DtensorShardOperation work on torch 2.6 and 2.7

Shard._local_shard_size_and_offset only exists from torch 2.8; 2.6 and
2.7 expose the same computation as _local_shard_size_on_dim(...,
return_offset=True). Alias that one when the newer helper is missing, so
tensor-parallel loading does not raise AttributeError on the declared
torch floor. tests/utils/test_distributed_sharding_utils.py on torch 2.6
goes from 23 failures to the same 3 strided-shard cases torch 2.8 has.

Related to #49651

* Resolve the Shard helper in one function instead of patching Shard

Per review: pick the right torch API inside _local_shard_size_and_offset
and call it from both sites, rather than attaching an alias to Shard.
@hazelxue

Copy link
Copy Markdown
Contributor Author

@hazelxue will include this in a patch, imo we should aim to support 2.6+ fully 🤗 so not raise torch version in setup.py

Thanks for the quick review and merge! Keeping 2.6+ supported makes sense — glad the patch release will carry it.

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

Labels

for patch Tag issues / labels that should be included in the next patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants