这是indexloc提供的服务,不要输入任何密码
Skip to content

Introduce new helper function that produces device lists for iota tile assignment. Apply it in xla_sharding_util.cc. #97176

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

Merged
merged 1 commit into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tensorflow/compiler/mlir/tensorflow/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,7 @@ cc_library(
":tensorflow",
"//tensorflow/core:framework",
"//tensorflow/core/protobuf/tpu:compile_metadata_proto_cc",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/status",
Expand All @@ -1384,6 +1385,7 @@ cc_library(
"@llvm-project//mlir:Support",
"@local_xla//xla:xla_data_proto_cc",
"@local_xla//xla/hlo/ir:hlo",
"@local_xla//xla/hlo/ir:tile_assignment",
"@local_xla//xla/hlo/parser:hlo_parser",
"@local_xla//xla/tsl/lib/math:math_util",
],
Expand Down
17 changes: 7 additions & 10 deletions tensorflow/compiler/mlir/tensorflow/utils/xla_sharding_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ limitations under the License.
#include <utility>
#include <vector>

#include "absl/algorithm/container.h"
#include "absl/log/check.h"
#include "absl/log/log.h"
#include "absl/status/status.h"
Expand Down Expand Up @@ -52,6 +53,7 @@ limitations under the License.
#include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h"
#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
#include "xla/hlo/ir/hlo_sharding.h"
#include "xla/hlo/ir/tile_assignment.h"
#include "xla/hlo/parser/hlo_parser.h"
#include "xla/tsl/lib/math/math_util.h"
#include "xla/xla_data.pb.h"
Expand Down Expand Up @@ -607,20 +609,15 @@ void ConvertV2ToV1Sharding(xla::OpSharding& sharding) {
return;
}

absl::StatusOr<xla::HloSharding> hlo_sharding =
xla::HloSharding::FromProto(sharding);
if (!hlo_sharding.ok()) {
LOG(ERROR) << "Failed to convert OpSharding to HloSharding: "
<< hlo_sharding.status();
}

// V2 Sharding uses iota_reshape_dims and iota_transpose_perm, while V1
// Sharding uses tile_assignment_devices.
absl::c_copy(
xla::ToArray(sharding.iota_reshape_dims(), sharding.iota_transpose_perm(),
sharding.tile_assignment_dimensions()),
tsl::protobuf::RepeatedFieldBackInserter(
sharding.mutable_tile_assignment_devices()));
sharding.clear_iota_reshape_dims();
sharding.clear_iota_transpose_perm();
for (int64_t device : hlo_sharding->tile_assignment().array()) {
sharding.add_tile_assignment_devices(device);
}
}

} // namespace
Expand Down
15 changes: 11 additions & 4 deletions third_party/xla/xla/hlo/ir/tile_assignment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,21 @@ DecanonicalizationInfo FullyDecanonicalize(
return IotaTileAssignment(dims, dims_span, perm_span);
}

Array<int64_t> IotaTileAssignment::ToArray() const {
Array<int64_t> array(reshape_dims());
// Materializes array representation of IotaTileAssignment.
Array<int64_t> ToArray(absl::Span<const int64_t> reshape_dims,
absl::Span<const int> transpose_perm,
absl::Span<const int64_t> dims) {
Array<int64_t> array(reshape_dims);
array.FillIota(0);
array.TransposeDimensions(transpose_perm());
array.Reshape(dims());
array.TransposeDimensions(transpose_perm);
array.Reshape(dims);
return array;
}

Array<int64_t> IotaTileAssignment::ToArray() const {
return ::xla::ToArray(reshape_dims(), transpose_perm(), dims());
}

IotaTileAssignment::IotaTileAssignment(const IotaTileAssignment& other)
: IotaTileAssignment(other.ndims_, other.reshape_ndims_) {
std::memcpy(storage_.get(), other.storage_.get(), size_bytes());
Expand Down
5 changes: 5 additions & 0 deletions third_party/xla/xla/hlo/ir/tile_assignment.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ class IotaTileAssignment {
std::unique_ptr<char[]> storage_;
};

// Materializes array representation of IotaTileAssignment.
Array<int64_t> ToArray(absl::Span<const int64_t> reshape_dims,
absl::Span<const int> transpose_perm,
absl::Span<const int64_t> dims);

// Internal class that represents how an ordered list of device IDs are sharded
// along different dimensions. It manages full or compact representation of the
// device IDs without having callers worry about what underlying format is used.
Expand Down