Repository navigation
[DTensor] Add shard method - #136589
[DTensor] Add shard method#136589kwen2501 wants to merge 1 commit into
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/136589
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 00e91fa with merge base 3bc073d ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
curious what is the difference from |
|
Is this for the purpose of trying to avoid the collective in |
|
IIRC, |
Yep |
|
maybe consider adding a kwarg to |
|
|
|
Could I ask if this is for internal use only? |
wanchaol
left a comment
There was a problem hiding this comment.
I don't think this method is well defined compare to distribute_tensor, and I think it should not be added as a public API. IMO DCP itself should handle the full state_dict transformation gracefully instead of making this a public API of DTensor.
The reason why it's problematic:
- How do you know if the
full_tensoron each different process are the same or not? Each process/rank would have different rng seeds, so callingtorch.randnon each process could yield totally different tensors that are not semantically meaningful to DTensor (they are not shard). Creating such an API is going to violate single device semantic.. - If you know that for sure that the
full_tensorwhen calling this API is replicated, what you really want to do is toDTensor.from_local(full_tensor, [Replicate()]).redistribute(placements=Shard(0). Explicitly annotation to replicated first is required and that's the way to tell that it's full sure replicated. IMO DCP should directly use this when transforming full state_dict to sharded state_dict under the hood.
|
Thanks @wanchaol. All workers load the same checkpoint (this is a regular checkpoint, not a DCP checkpoint). From that checkpoint, they see the same full tensor. Each worker wants to take a shard of that full tensor and create a DTensor. They want to do it without sending the tensor over the wire. Is nit: I had a feeling that saying a full tensor is |
It is internal in the sense that it is only called within the My goal is to integrate it into the Does the above make sense to you? @wanchaol @wz337 I am happy to make |
I feel like we could utilize distributed_state_dict for this use case without needing to add the additional API. Let me know if this works for your use case:
If this works for you, we could check whether there is any gap in 3 today. Also, if this approach works, then we don't need to have a new |
|
Thanks for the suggestion @wz337 . The workflow would work for large models. If I may give some feedbacks?
pytorch/torch/distributed/_state_dict_utils.py Lines 540 to 545 in 2521cd3
pytorch/torch/distributed/_state_dict_utils.py Lines 553 to 554 in 2521cd3
|
|
@kwen2501
|
When creating model on meta device and call
For this I am a bit unsure, we need to always require the initialization communication for training to ensure the numerical correctness, so exposing a option to avoid init communication would be risky (i.e. what if training users use this, we can't guarantee the convergence anymore). I would think that for folks who seriously treat inference and care about initialization time, we should always recommend meta init, as that's the fastest initialization and don't require any model changes for inference either. If user don't care about initialization time, a few additional communications wouldn't matter for them anyways. Our DCP workflow should make sure the meta init load workflow is well supported. |
For 1 and 2, sorry if I didn't clarify, I actually don't mean to ask you to directly uses this private
|
|
Thanks @wz337 !
Cool! I will give it a try! It should work for TP case. (For PP + TP, the situation is a little bit different, because I would need to prepare the stage state dict from the full-model state dict, and while doing that, I will do full-tensor to DTensor conversion. That's why I thought a tensor level conversion util would be super helpful. Not a big deal if hard to host it in PyTorch, I can put it in my project. ======= separation line ======= The I think this is legit code. And the user has made sure the models are the same across ranks. |
Putting aside whether that's true for every user :) what we are faced is a different problem -- we are trying to get some inference-oriented, quantized dtypes to work with the TP stack -- e.g. int8, float8-inference. If the TP stack accepts DTensor from local sharding, what we have to do is simple -- implement some This is an example of supporting the above mentioned dtypes with TP: |
|
You can do something like this |
Stack from ghstack (oldest at bottom):
-> [DTensor] Add shard method #136589
Use cases:
(i) Distributed state dict:
pytorch/torch/distributed/_state_dict_utils.py
Lines 556 to 569 in 9629835
(ii) torchchat:
Converting loaded checkpoint into DTensor:
https://github.com/pytorch/torchchat/blob/main/distributed/dtensor_utils.py#L31-L58
(iii) torchao:
Sharding quantized tensor:
pytorch/ao#937
cc @XilunWu @H-Huang @awgu @wanchaol @fegin @fduwjj @wz337 @wconstab @d4l3k @c-p-i-o