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

[xla:cpu] Do not serialize HloProto if user didn't ask for it #97363

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 22, 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
17 changes: 17 additions & 0 deletions third_party/xla/xla/pjrt/cpu/cpu_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,23 @@ absl::StatusOr<tsl::RCReference<PjRtDeviceEvent>> PjRtCpuClient::LinearizeInto(
->CopyFromLiteral(literal, layout, async_work_runner());
}

absl::StatusOr<CompiledMemoryStats> PjRtCpuExecutable::GetCompiledMemoryStats()
const {
auto cpu_executable_ptr =
tsl::down_cast<cpu::CpuExecutable*>(cpu_executable_.get());
const auto& buffer_assignment = cpu_executable_ptr->buffer_assignment();
auto proto = buffer_assignment.ToProto();

CompiledMemoryStats memory_stats = CompiledMemoryStats();
memory_stats.generated_code_size_in_bytes = SizeOfGeneratedCodeInBytes();
memory_stats.serialized_buffer_assignment = proto.SerializeAsString();
memory_stats.PopulateBufferStatsFromAllocations(
cpu_executable_->GetAllocations());
TF_ASSIGN_OR_RETURN(int64_t peak_memory, ComputePeakMemory(proto));
memory_stats.peak_memory_in_bytes = peak_memory;
return memory_stats;
}

absl::StatusOr<std::pair<tsl::RCReference<PjRtDeviceEventPromise>,
tsl::RCReference<PjRtDeviceEvent>>>
PjRtCpuClient::CreateLinkedEventPromise(PjRtMemorySpace* memory_space,
Expand Down
17 changes: 1 addition & 16 deletions third_party/xla/xla/pjrt/cpu/cpu_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,22 +364,7 @@ class PjRtCpuExecutable final : public PjRtLoadedExecutable {
return Unimplemented("GetOutputMemoryKinds is not supported.");
}

absl::StatusOr<CompiledMemoryStats> GetCompiledMemoryStats() const override {
CompiledMemoryStats memory_stats = CompiledMemoryStats();
memory_stats.generated_code_size_in_bytes = SizeOfGeneratedCodeInBytes();
const BufferAssignmentProto* proto =
cpu_executable_->buffer_assignment_proto();
if (!proto) {
return tsl::errors::FailedPrecondition(
"cpu_executable_ has no buffer_assignment_proto.");
}
memory_stats.serialized_buffer_assignment = proto->SerializeAsString();
memory_stats.PopulateBufferStatsFromAllocations(
cpu_executable_->GetAllocations());
TF_ASSIGN_OR_RETURN(int64_t peak_memory, ComputePeakMemory(*proto));
memory_stats.peak_memory_in_bytes = peak_memory;
return memory_stats;
}
absl::StatusOr<CompiledMemoryStats> GetCompiledMemoryStats() const override;

using PjRtLoadedExecutable::Execute;
absl::StatusOr<std::vector<std::vector<std::unique_ptr<PjRtBuffer>>>> Execute(
Expand Down
4 changes: 4 additions & 0 deletions third_party/xla/xla/service/cpu/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ cc_library(
"//xla:util",
"//xla:xla_data_proto_cc",
"//xla:xla_proto_cc",
"//xla/backends/cpu:alignment",
"//xla/backends/cpu:constant_allocation",
"//xla/backends/cpu:xnn_fusion",
"//xla/backends/cpu/codegen:compiled_function_library",
Expand Down Expand Up @@ -338,6 +339,7 @@ cc_library(
"//xla/stream_executor/host:host_platform_id",
"//xla/tsl/concurrency:async_value",
"//xla/tsl/platform:env",
"//xla/tsl/platform:errors",
"//xla/tsl/platform:status",
"//xla/tsl/platform:statusor",
"//xla/tsl/protobuf:error_codes_proto_impl_cc",
Expand Down Expand Up @@ -458,6 +460,7 @@ cc_library(
":cpu_aot_compilation_result",
":executable_proto_cc",
"//xla:util",
"//xla/backends/cpu:alignment",
"//xla/backends/cpu/codegen:ir_compiler",
"//xla/backends/cpu/codegen:target_machine_features",
"//xla/hlo/ir:hlo",
Expand All @@ -472,6 +475,7 @@ cc_library(
"//xla/stream_executor:platform",
"//xla/stream_executor:stream_executor_h",
"//xla/stream_executor/host:host_platform_id",
"//xla/tsl/platform:errors",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@llvm-project//llvm:Support",
Expand Down
30 changes: 18 additions & 12 deletions third_party/xla/xla/service/cpu/cpu_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ limitations under the License.
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Export.h"
#include "mlir/Transforms/DialectConversion.h"
#include "xla/backends/cpu/alignment.h"
#include "xla/backends/cpu/codegen/cpu_features.h"
#include "xla/backends/cpu/codegen/emitters/cpu_fusion_emitter_config.h"
#include "xla/backends/cpu/codegen/execution_engine.h"
Expand Down Expand Up @@ -224,6 +225,7 @@ limitations under the License.
#include "xla/stream_executor/platform.h"
#include "xla/stream_executor/stream_executor.h"
#include "xla/tsl/platform/env.h"
#include "xla/tsl/platform/errors.h"
#include "xla/tsl/platform/status.h"
#include "xla/tsl/platform/statusor.h"
#include "xla/tsl/platform/threadpool.h"
Expand Down Expand Up @@ -1501,12 +1503,14 @@ CpuCompiler::CompileCpuExecutable(std::unique_ptr<HloModule> module) {
// Dump computation proto state and buffer assignment for
// GetCompiledMemoryStats results.
auto with_hlo_proto = [&](std::unique_ptr<CpuExecutable> cpu_executable) {
auto hlo_proto = std::make_unique<HloProto>();
*hlo_proto->mutable_hlo_module() = cpu_executable->module().ToProto();
*hlo_proto->mutable_buffer_assignment() =
cpu_executable->buffer_assignment().ToProto();
StripPayloadFromLiteralProto(*hlo_proto);
cpu_executable->set_hlo_proto(std::move(hlo_proto));
if (embed_ir_in_executable) {
auto hlo_proto = std::make_unique<HloProto>();
*hlo_proto->mutable_hlo_module() = cpu_executable->module().ToProto();
*hlo_proto->mutable_buffer_assignment() =
cpu_executable->buffer_assignment().ToProto();
StripPayloadFromLiteralProto(*hlo_proto);
cpu_executable->set_hlo_proto(std::move(hlo_proto));
}
return cpu_executable;
};

Expand Down Expand Up @@ -2432,12 +2436,14 @@ CpuCompiler::CompileAheadOfTimeThunks(
// Dump computation proto state and buffer assignment for
// GetCompiledMemoryStats results.
auto with_hlo_proto = [&](std::unique_ptr<CpuExecutable> cpu_executable) {
auto hlo_proto = std::make_unique<HloProto>();
*hlo_proto->mutable_hlo_module() = cpu_executable->module().ToProto();
*hlo_proto->mutable_buffer_assignment() =
cpu_executable->buffer_assignment().ToProto();
StripPayloadFromLiteralProto(*hlo_proto);
cpu_executable->set_hlo_proto(std::move(hlo_proto));
if (embed_ir_in_executable) {
auto hlo_proto = std::make_unique<HloProto>();
*hlo_proto->mutable_hlo_module() = cpu_executable->module().ToProto();
*hlo_proto->mutable_buffer_assignment() =
cpu_executable->buffer_assignment().ToProto();
StripPayloadFromLiteralProto(*hlo_proto);
cpu_executable->set_hlo_proto(std::move(hlo_proto));
}
return cpu_executable;
};

Expand Down
3 changes: 3 additions & 0 deletions third_party/xla/xla/tests/multithreaded_compilation_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ TEST_F(MultithreadedCompilation, EightModuleCompilation) {
for (int i = 0; i < num_threads; i++) {
TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
ParseAndReturnVerifiedModule(hlo_text, config));
module->mutable_config()
.mutable_debug_options()
.set_xla_embed_ir_in_executable(true);
modules[i] = std::move(module);
}

Expand Down