Repository navigation
Make DtensorShardOperation work on torch 2.6 and 2.7 - #49657
Conversation
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
|
Thank you for your contribution 🤗! CI Security Gate — waiting for maintainer approvalCI 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. |
There was a problem hiding this comment.
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)|
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.
|
Pushed e90e074 with @3outeille's suggestion: the alias on |
CI recapDashboard: View test results in Grafana |
|
@hazelxue will include this in a patch, imo we should aim to support 2.6+ fully 🤗 so not raise torch version in setup.py |
* 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.
Thanks for the quick review and merge! Keeping 2.6+ supported makes sense — glad the patch release will carry it. |
What does this PR do?
Second half of #49651. After #49652,
from_pretrainedworks again on torch 2.7, but tensor-parallel loading still raises on both declared-floor versions:sharding_utils.pyaliasesShard._local_shard_size_and_offset, which only exists from torch 2.8. torch 2.6 and 2.7 expose the same computation asShard._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):The 3 remaining failures on 2.6 are the
_StridedShardcases, 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 movesetup.pytotorch>=2.8instead, happy to turn this into that change.Before submitting
from_pretrainedfails in 5.20.0 (hf_storageimport behind atorch>=2.7guard) #49651 (second point in the report)Who can review?
@michaelbenayoun @ArthurZucker