-
Notifications
You must be signed in to change notification settings - Fork 5
State dict serialization #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9567b1f
43427f6
c1da899
dce528a
1b35e01
f68ce3f
25ca59b
24d46ac
298eb5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,65 @@ | ||||||||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||||||||
# All rights reserved. | ||||||||
# | ||||||||
# This source code is licensed under the BSD-style license found in the | ||||||||
# LICENSE file in the root directory of this source tree. | ||||||||
|
||||||||
from typing import Tuple | ||||||||
|
||||||||
import torch | ||||||||
from torch.distributed.tensor import DTensor, Placement | ||||||||
from torch.distributed.tensor._utils import _compute_local_shape_and_global_offset | ||||||||
|
||||||||
|
||||||||
def create_tensor_slice_from_dtensor(dtensor: DTensor) -> "TensorSlice": | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the response to the other comment about import ordering. |
||||||||
""" | ||||||||
Create a TensorSlice from a DTensor. | ||||||||
Args: | ||||||||
dtensor: The DTensor to extract metadata from | ||||||||
Returns: | ||||||||
TensorSlice containing the distributed tensor metadata | ||||||||
""" | ||||||||
from torchstore.transport.pipe import TensorSlice | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a particular reason to avoid import this on the file level? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So there's a circular dependency where
Maybe we should put There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would |
||||||||
|
||||||||
coordinates = dtensor.device_mesh.get_coordinate() | ||||||||
_, offsets = _compute_local_shape_and_global_offset( | ||||||||
dtensor.shape, | ||||||||
mesh_shape=dtensor.device_mesh.shape, | ||||||||
my_coordinate=coordinates, | ||||||||
placements=dtensor.placements, | ||||||||
) | ||||||||
|
||||||||
return TensorSlice( | ||||||||
offsets=offsets, | ||||||||
coordinates=coordinates, | ||||||||
global_shape=dtensor.shape, | ||||||||
local_shape=dtensor._local_tensor.shape, | ||||||||
mesh_shape=dtensor.device_mesh.shape, | ||||||||
) | ||||||||
|
||||||||
|
||||||||
def reconstruct_dtensor_from_local_tensor( | ||||||||
local_tensor: torch.Tensor, | ||||||||
tensor_slice: "TensorSlice", | ||||||||
device_mesh: torch.distributed.DeviceMesh, | ||||||||
placements: Tuple[Placement, ...], | ||||||||
) -> DTensor: | ||||||||
""" | ||||||||
Reconstruct a DTensor from local tensor data and TensorSlice metadata. | ||||||||
Args: | ||||||||
local_tensor: The local tensor shard | ||||||||
tensor_slice: TensorSlice containing distributed metadata | ||||||||
device_mesh: The device mesh for the DTensor | ||||||||
placements: The placements for the DTensor | ||||||||
Returns: | ||||||||
Reconstructed DTensor | ||||||||
""" | ||||||||
return DTensor.from_local( | ||||||||
local_tensor=local_tensor, | ||||||||
device_mesh=device_mesh, | ||||||||
placements=placements, | ||||||||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a test for sharded dtensor (with world size > 1)? I am actually also confused about the expected behavior in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That dtensor put then get functionality will be added in the next PR where we integrate the state_dict functionality into torchstore. This PR only do the serialization and deserialization part.