@@ -24,34 +24,55 @@ limitations under the License.
2424#include " absl/functional/overload.h"
2525#include " absl/memory/memory.h"
2626#include " absl/status/statusor.h"
27+ #include " absl/strings/str_format.h"
2728#include " absl/strings/string_view.h"
2829#include " xla/tsl/platform/status_macros.h"
2930#include " google/protobuf/arena.h"
3031#include " riegeli/bytes/string_writer.h"
3132#include " xla/debug_options_flags.h"
3233#include " xla/hlo/ir/hlo_module.h"
34+ #include " xla/hlo/ir/hlo_print_options.h"
3335#include " xla/pjrt/compiled_memory_stats.h"
36+ #include " xla/printer.h"
3437#include " xla/service/buffer_assignment.h"
3538#include " xla/service/executable.h"
3639#include " xla/service/gpu/gpu_executable.h"
3740#include " xla/service/gpu/gpu_executable.pb.h"
3841#include " xla/stream_executor/device_description.h"
3942#include " xla/stream_executor/kernel_symbol_registry.h"
4043#include " xla/stream_executor/platform.h"
41- #include " xla/tsl/platform/errors .h"
42- #include " xla/tsl/platform/statusor .h"
44+ #include " xla/tsl/lib/strings/proto_serialization .h"
45+ #include " xla/tsl/platform/logging .h"
4346#include " xla/util/split_proto/split_gpu_executable_writer.h"
4447#include " xla/util/split_proto/split_proto_reader.h"
48+ #include " xla/xla.pb.h"
49+ #include " tsl/platform/fingerprint.h"
4550
4651namespace xla ::gpu {
4752
53+ static absl::StatusOr<std::pair<std::unique_ptr<HloModule>, tsl::Fprint128>>
54+ ParseHloModuleAndFingerprint (const HloModuleProtoWithConfig& proto) {
55+ ASSIGN_OR_RETURN (std::unique_ptr<HloModule> module ,
56+ HloModule::CreateFromProtoWithConfig (proto));
57+ HighwayHashPrinter printer;
58+ module ->Print (&printer, HloPrintOptions::Canonical ()
59+ .set_print_backend_config (true )
60+ .set_sort_backend_config (true ));
61+ return std::make_pair (std::move (module ), printer.ToFingerprint128 ());
62+ }
63+
4864absl::StatusOr<std::unique_ptr<GpuAotCompilationResult>>
4965GpuAotCompilationResult::FromProto (GpuExecutableProto executable_proto) {
50- TF_ASSIGN_OR_RETURN (std::unique_ptr<HloModule> module ,
51- HloModule::CreateFromProtoWithConfig (
52- executable_proto.hlo_module_with_config ()));
66+ tsl::Fprint128 executable_fingerprint = {
67+ tsl::DeterministicProtoHash64 (executable_proto),
68+ tsl::DeterministicProtoHash64 (executable_proto, /* seed=*/ 1 )};
69+ ASSIGN_OR_RETURN (
70+ auto module_and_fingerprint,
71+ ParseHloModuleAndFingerprint (executable_proto.hlo_module_with_config ()));
72+ auto & [module , hlo_fingerprint] = module_and_fingerprint;
5373 return absl::WrapUnique (new GpuAotCompilationResult (
54- std::move (executable_proto), std::move (module )));
74+ std::move (executable_proto), std::move (module ), hlo_fingerprint,
75+ executable_fingerprint));
5576}
5677
5778absl::StatusOr<std::unique_ptr<GpuAotCompilationResult>>
@@ -61,20 +82,24 @@ GpuAotCompilationResult::FromSerialized(
6182 GpuExecutableProto* executable_proto =
6283 google::protobuf::Arena::Create<GpuExecutableProto>(arena.get ());
6384
64- TF_RETURN_IF_ERROR (ReadSplitProto (std::move (reader), *executable_proto));
85+ RETURN_IF_ERROR (ReadSplitProto (std::move (reader), *executable_proto));
6586
66- TF_ASSIGN_OR_RETURN (std::unique_ptr<HloModule> module ,
67- HloModule::CreateFromProtoWithConfig (
68- executable_proto->hlo_module_with_config ()));
69- return absl::WrapUnique (
70- new GpuAotCompilationResult (internal::ArenaAllocatedGpuExecutableProto (
71- std::move (arena), executable_proto),
72- std::move (module )));
87+ tsl::Fprint128 executable_fingerprint = {
88+ tsl::DeterministicProtoHash64 (*executable_proto),
89+ tsl::DeterministicProtoHash64 (*executable_proto, /* seed=*/ 1 )};
90+ ASSIGN_OR_RETURN (
91+ auto module_and_fingerprint,
92+ ParseHloModuleAndFingerprint (executable_proto->hlo_module_with_config ()));
93+ auto & [module , hlo_fingerprint] = module_and_fingerprint;
94+ return absl::WrapUnique (new GpuAotCompilationResult (
95+ internal::ArenaAllocatedGpuExecutableProto (std::move (arena),
96+ executable_proto),
97+ std::move (module ), hlo_fingerprint, executable_fingerprint));
7398}
7499
75100absl::StatusOr<std::string> GpuAotCompilationResult::SerializeAsString () const {
76101 std::string serialized;
77- TF_RETURN_IF_ERROR (WriteSplitGpuExecutable (
102+ RETURN_IF_ERROR (WriteSplitGpuExecutable (
78103 GetExecutableProto (),
79104 std::make_unique<riegeli::StringWriter<>>(&serialized)));
80105 return serialized;
@@ -89,6 +114,15 @@ GpuAotCompilationResult::LoadExecutable(
89114 stream_executor::KernelSymbolRegistry::GetGlobalInstance ();
90115 return registry.FindSymbol (symbol_name, platform_id);
91116 };
117+
118+ VLOG (1 ) << absl::StrFormat (
119+ " GpuAotCompilationResult::LoadExecutable: module=%s "
120+ " num_instructions=%d hlo_fingerprint=%016x%016x "
121+ " executable_fingerprint=%016x%016x" ,
122+ hlo_module_->name (), hlo_module_->instruction_count (),
123+ hlo_fingerprint_.low64 , hlo_fingerprint_.high64 ,
124+ executable_fingerprint_.low64 , executable_fingerprint_.high64 );
125+
92126 return GpuExecutable::FromProto (GetExecutableProto (), device_description,
93127 platform_id->ToName (),
94128 GetDebugOptionsFromFlags (), symbol_resolver);
0 commit comments