Change IFRT and PjRt layout API to return std::shared_ptr<const xla::PjRtLayout>
instead of std::unique_ptr<xla::PjRtLayout>
#21012
+114
−115
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.
Change IFRT and PjRt layout API to return
std::shared_ptr<const xla::PjRtLayout>
instead ofstd::unique_ptr<xla::PjRtLayout>
The current API design that uses
std::unique_ptr<xla::PjRtLayout>
has several issues:xla::PjRtLayout
to be copied in some scenarios, e.g.,xla::ifrt::Array
internally stores a layout and returns its copy every timelayout()
is called. This forces implementations to break the abstraction boundary becausexla::PjRtLayout
is an abstract class andstd::unique_ptr<xla::PjRtLayout>
is not copyable. The current implementation either storesxla::Layout
and createsxla::PjRtLayout
every time, or downcastsxla::PjRtLayout
toxla::PjRtXlaLayout
to perform the copy.xla::Layout
is expensive to copy (sizeof(xla::Layout)
is 248 bytes as of 2025-01-03) and copyingxla::PjRtXlaLayout
requires copying or movingxla::Layout
.To address these two problems, this CL changes PjRt and IFRT APIs that return
xla::PjRtLayout
to instead usestd::shared_ptr<const xla::PjRtLayout>
, so that PjRt layouts can be cheaply copied. Similar patterns have been used in other places such asxla::ifrt::Sharding
andxla::PjRtExecutable::GetHloModules()
.Some implementations have been updated to take advantage of this change. For example,
PjRtCApiBuffer::layout()
no longer performs a layout copy and instead reuses an internally cached instance ofstd::shared_ptr<const xla::PjRtLayout>
.