Skip to content

Commit

Permalink
More graceful handling AbslHashValue when hlo module doesn't have ent…
Browse files Browse the repository at this point in the history
…ry computation layout. Without the fix hash breaks on empty module such as absl:Hash(HloTestBase::CreateNewVerifiedModule()).

PiperOrigin-RevId: 685822286
  • Loading branch information
toli-y authored and Google-ML-Automation committed Oct 14, 2024
1 parent 9748732 commit 09da8b7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
15 changes: 15 additions & 0 deletions xla/hlo/ir/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ cc_library(
],
)

xla_cc_test(
name = "hlo_module_test",
srcs = ["hlo_module_test.cc"],
deps = [
":hlo",
"//xla/tests:hlo_test_base",
"//xla/tests:verified_hlo_module",
"@com_google_absl//absl/hash",
"@com_google_googletest//:gtest_main",
"@tsl//tsl/platform:statusor",
"@tsl//tsl/platform:test",
"@tsl//tsl/platform:test_main",
],
)

cc_library(
name = "backend_config",
srcs = ["backend_config.cc"],
Expand Down
3 changes: 2 additions & 1 deletion xla/hlo/ir/hlo_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ class HloModule {
// with respect to HloInstruction::Identical() method.
template <typename H>
friend H AbslHashValue(H h, const HloModule& module) {
h = H::combine(std::move(h), module.entry_computation_layout());
if (module.config().has_entry_computation_layout())
h = H::combine(std::move(h), module.entry_computation_layout());
// Use MakeComputationSorted() instead of MakeComputationPostOrder()
// because naming may affect the order of MakeComputationPostOrder() but not
// MakeComputationSorted().
Expand Down
54 changes: 54 additions & 0 deletions xla/hlo/ir/hlo_module_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* Copyright 2024 The OpenXLA Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "xla/hlo/ir/hlo_module.h"

#include <memory>
#include <string_view>

#include <gtest/gtest.h>
#include "absl/hash/hash.h"
#include "xla/tests/hlo_test_base.h"
#include "xla/tests/verified_hlo_module.h"
#include "tsl/platform/statusor.h"
#include "tsl/platform/test.h"

namespace xla {
namespace {

using HloModuleTest = HloTestBase;

TEST_F(HloModuleTest, AbslHashValue) {
std::unique_ptr<HloModule> module1 = CreateNewVerifiedModule();
std::unique_ptr<HloModule> module2 = CreateNewVerifiedModule();
EXPECT_EQ(absl::HashOf(*module1), absl::HashOf(*module2));

std::string_view hlo = R"(
HloModule m1
ENTRY main {
a = f32[] parameter(0)
b = f32[] parameter(1)
ROOT res = f32[] multiply(a, b)
})";
TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<VerifiedHloModule> module3,
ParseAndReturnVerifiedModule(hlo));
TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<VerifiedHloModule> module4,
ParseAndReturnVerifiedModule(hlo));
EXPECT_EQ(absl::HashOf(*module3), absl::HashOf(*module4));
EXPECT_NE(absl::HashOf(*module1), absl::HashOf(*module4));
}

} // namespace
} // namespace xla

0 comments on commit 09da8b7

Please sign in to comment.