-
Notifications
You must be signed in to change notification settings - Fork 54
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
FusionDefinition.execute returns output shardings. #3732
Open
wujingyue
wants to merge
8
commits into
wjy/execute
Choose a base branch
from
wjy/dist
base: wjy/execute
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+227
−30
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ae7a84e
Introduce DistributedTensor
wujingyue c58c2b4
Wrap DistributedTensor as a torch.Tensor
wujingyue 22ce1bc
Minor cleanups
wujingyue 3b2c761
Minor
wujingyue 42b1f5e
Lint
wujingyue 7fcaf86
License
wujingyue 39efb76
lint
wujingyue d7dc6cd
Type annotation
wujingyue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// clang-format off | ||
/* | ||
* SPDX-FileCopyrightText: Copyright (c) 2025-present NVIDIA CORPORATION & AFFILIATES. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
// clang-format on | ||
|
||
#include <exceptions.h> | ||
#include <python_frontend/distributed_tensor.h> | ||
#include <utils.h> | ||
|
||
namespace nvfuser::python_frontend { | ||
|
||
void DistributedTensor::setAxisIsShardedOn( | ||
const int64_t axis, | ||
const ParallelType parallel_type) { | ||
const auto i = axis_sharded_on_.find(parallel_type); | ||
NVF_CHECK( | ||
i == axis_sharded_on_.end(), | ||
"Parallel type ", | ||
parallel_type, | ||
" was already used to shard axis ", | ||
i->second); | ||
axis_sharded_on_[parallel_type] = axis; | ||
} | ||
|
||
int64_t DistributedTensor::axisShardedOn( | ||
const ParallelType parallel_type) const { | ||
return getOrDefault(axis_sharded_on_, parallel_type, -1L); | ||
} | ||
|
||
} // namespace nvfuser::python_frontend |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// clang-format off | ||
/* | ||
* SPDX-FileCopyrightText: Copyright (c) 2025-present NVIDIA CORPORATION & AFFILIATES. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
// clang-format on | ||
|
||
#pragma once | ||
|
||
#include <ATen/core/TensorBody.h> | ||
|
||
#include <multidevice/device_mesh.h> | ||
#include <type.h> | ||
|
||
namespace nvfuser::python_frontend { | ||
|
||
// A class that represents a distributed tensor. It wraps a local tensor, a | ||
// mesh, and a mapping from mesh axes to tensor axes. If the mesh is empty, | ||
// it degenerates into a non-distributed tensor. | ||
class DistributedTensor { | ||
public: | ||
explicit DistributedTensor( | ||
at::Tensor local_tensor, | ||
DeviceMesh mesh = DeviceMesh()) | ||
: local_(std::move(local_tensor)), mesh_(std::move(mesh)) {} | ||
DistributedTensor(const DistributedTensor&) = delete; | ||
DistributedTensor& operator=(const DistributedTensor&) = delete; | ||
DistributedTensor(DistributedTensor&&) = default; | ||
DistributedTensor& operator=(DistributedTensor&&) = default; | ||
|
||
const DeviceMesh& mesh() const { | ||
return mesh_; | ||
} | ||
|
||
at::Tensor local() const { | ||
return local_; | ||
} | ||
|
||
void setAxisIsShardedOn(int64_t axis, ParallelType parallel_type); | ||
|
||
int64_t axisShardedOn(ParallelType parallel_type) const; | ||
|
||
private: | ||
at::Tensor local_; | ||
DeviceMesh mesh_; | ||
std::unordered_map<ParallelType, int64_t> axis_sharded_on_; | ||
}; | ||
|
||
} // namespace nvfuser::python_frontend |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,14 +51,18 @@ void bindCommunicator(py::module& nvfuser) { | |
} | ||
|
||
void bindDeviceMesh(py::module& nvfuser) { | ||
py::class_<DeviceMesh> device_mesh_class(nvfuser, "DeviceMesh"); | ||
device_mesh_class.def(py::init<std::vector<int64_t>>()); | ||
device_mesh_class.def("__repr__", [](const DeviceMesh& self) { | ||
py::class_<DeviceMesh> device_mesh(nvfuser, "DeviceMesh"); | ||
device_mesh.def(py::init<std::vector<int64_t>>()); | ||
device_mesh.def("__repr__", [](const DeviceMesh& self) { | ||
std::stringstream ss; | ||
ss << self; | ||
return ss.str(); | ||
}); | ||
device_mesh_class.def( | ||
device_mesh.def_property_readonly( | ||
"size", | ||
[](const DeviceMesh& self) -> int64_t { return self.size(); }, | ||
"Returns the size of the mesh."); | ||
device_mesh.def( | ||
"shard_tensor", | ||
[](const DeviceMesh& self, | ||
at::Tensor tensor, | ||
|
@@ -71,11 +75,24 @@ void bindDeviceMesh(py::module& nvfuser) { | |
py::arg("device_id")); | ||
} | ||
|
||
void bindDistributedTensor(py::module& nvfuser) { | ||
py::class_<DistributedTensor> distributed_tensor( | ||
nvfuser, "_DistributedTensor"); | ||
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. Docs are omitted because the class name starts with an underscore, indicating it's hidden from the user. |
||
distributed_tensor.def_property_readonly("local", &DistributedTensor::local); | ||
distributed_tensor.def_property_readonly( | ||
"mesh", &DistributedTensor::mesh, py::return_value_policy::reference); | ||
distributed_tensor.def( | ||
"axis_sharded_on", | ||
&DistributedTensor::axisShardedOn, | ||
py::arg("parallel_type")); | ||
} | ||
|
||
} // namespace | ||
|
||
void bindMultidevice(py::module& nvfuser) { | ||
bindCommunicator(nvfuser); | ||
bindDeviceMesh(nvfuser); | ||
bindDistributedTensor(nvfuser); | ||
} | ||
|
||
} // namespace nvfuser::python_frontend |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Alternatives:
std::vector<std::variant<at::Tensor, DistributedTensor>>
. BecauseDistributedTensor
can also represent a non-distributed tensor, I chose the current API for simplicity -- C++ is more verbose than Python when dealing with dynamic types.std::variant<std::vector<at::Tensor>, std::vector<DistributedTensor>>
. Same reason.