-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[xla] Add LiteralPool and LiteralCanonicalizer to share constant lite…
…rals between HLO modules This change saves a lot of host memory from duplicate constant literals in instantiated HLO modules. PiperOrigin-RevId: 706775155
- Loading branch information
1 parent
33f1796
commit f3dd9ec
Showing
13 changed files
with
497 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* 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/transforms/literal_canonicalizer.h" | ||
|
||
#include <cstddef> | ||
|
||
#include "absl/container/flat_hash_set.h" | ||
#include "absl/status/status.h" | ||
#include "absl/status/statusor.h" | ||
#include "absl/strings/string_view.h" | ||
#include "xla/hlo/ir/dfs_hlo_visitor.h" | ||
#include "xla/hlo/ir/dfs_hlo_visitor_with_default.h" | ||
#include "xla/hlo/ir/hlo_casting_utils.h" | ||
#include "xla/hlo/ir/hlo_instruction.h" | ||
#include "xla/hlo/ir/hlo_instructions.h" | ||
#include "xla/hlo/ir/hlo_module.h" | ||
#include "xla/literal_pool.h" | ||
#include "tsl/platform/errors.h" | ||
#include "tsl/platform/logging.h" | ||
|
||
namespace xla { | ||
namespace { | ||
|
||
class LiteralCanonicalizerVisitor : public DfsHloRewriteVisitor { | ||
public: | ||
LiteralCanonicalizerVisitor(LiteralPool* literal_pool, size_t min_size_bytes) | ||
: literal_pool_(literal_pool), min_size_bytes_(min_size_bytes) {} | ||
|
||
absl::Status HandleConstant(HloInstruction* hlo) final { | ||
auto* constant = Cast<HloConstantInstruction>(hlo); | ||
if (constant->HasLiteral() && | ||
constant->literal().size_bytes() >= min_size_bytes_) { | ||
MarkAsMaybeChanged(constant->Canonicalize(literal_pool_)); | ||
} | ||
return absl::OkStatus(); | ||
} | ||
|
||
private: | ||
LiteralPool* literal_pool_; | ||
size_t min_size_bytes_; | ||
}; | ||
|
||
} // namespace | ||
|
||
LiteralCanonicalizer::LiteralCanonicalizer(LiteralPool* literal_pool, | ||
size_t min_size_bytes) | ||
: literal_pool_(literal_pool), min_size_bytes_(min_size_bytes) {} | ||
|
||
absl::StatusOr<bool> LiteralCanonicalizer::Run( | ||
HloModule* module, | ||
const absl::flat_hash_set<absl::string_view>& execution_threads) { | ||
// Every time we canonicalize literals in a module, we garbage collect expired | ||
// literals from the pool. | ||
size_t num_erased = literal_pool_->GarbageCollect(); | ||
VLOG(3) << "Garbage collected " << num_erased << " expired literals"; | ||
|
||
LiteralCanonicalizerVisitor visitor(literal_pool_, min_size_bytes_); | ||
TF_RETURN_IF_ERROR(module->entry_computation()->Accept(&visitor)); | ||
return visitor.changed(); | ||
} | ||
|
||
} // namespace xla |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* 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. | ||
==============================================================================*/ | ||
|
||
#ifndef XLA_HLO_TRANSFORMS_LITERAL_CANONICALIZER_H_ | ||
#define XLA_HLO_TRANSFORMS_LITERAL_CANONICALIZER_H_ | ||
|
||
#include <cstddef> | ||
|
||
#include "absl/container/flat_hash_set.h" | ||
#include "absl/status/statusor.h" | ||
#include "absl/strings/string_view.h" | ||
#include "xla/hlo/ir/hlo_module.h" | ||
#include "xla/hlo/pass/hlo_pass_interface.h" | ||
#include "xla/literal_pool.h" | ||
|
||
namespace xla { | ||
|
||
// Canonicalizes literals larger than 'min_size_bytes' in the HLO module using | ||
// the given literal pool. | ||
class LiteralCanonicalizer : public HloModulePass { | ||
public: | ||
LiteralCanonicalizer(LiteralPool* literal_pool, size_t min_size_bytes); | ||
|
||
using HloPassInterface::Run; | ||
absl::StatusOr<bool> Run( | ||
HloModule* module, | ||
const absl::flat_hash_set<absl::string_view>& execution_threads) override; | ||
|
||
absl::string_view name() const override { return "literal-canonicalizer"; } | ||
|
||
protected: | ||
LiteralPool* literal_pool_; | ||
size_t min_size_bytes_; | ||
}; | ||
|
||
} // namespace xla | ||
|
||
#endif // XLA_HLO_TRANSFORMS_LITERAL_CANONICALIZER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* 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/transforms/literal_canonicalizer.h" | ||
|
||
#include "absl/strings/string_view.h" | ||
#include "xla/hlo/ir/hlo_casting_utils.h" | ||
#include "xla/hlo/ir/hlo_instructions.h" | ||
#include "xla/hlo/parser/hlo_parser.h" | ||
#include "xla/hlo/testlib/hlo_hardware_independent_test_base.h" | ||
#include "xla/literal_pool.h" | ||
#include "tsl/platform/statusor.h" | ||
#include "tsl/platform/test.h" | ||
|
||
namespace xla { | ||
namespace { | ||
|
||
class LiteralCanonicalizerTest : public HloHardwareIndependentTestBase {}; | ||
|
||
TEST_F(LiteralCanonicalizerTest, CanonicalizeConstants) { | ||
absl::string_view hlo_string = R"( | ||
HloModule m | ||
ENTRY %entry { | ||
ROOT %c0 = f32[4] constant({1.0, 2.0, 3.0, 4.0}) | ||
} | ||
)"; | ||
|
||
TF_ASSERT_OK_AND_ASSIGN(auto module0, | ||
ParseAndReturnVerifiedModule(hlo_string)); | ||
TF_ASSERT_OK_AND_ASSIGN(auto module1, | ||
ParseAndReturnVerifiedModule(hlo_string)); | ||
|
||
LiteralPool literal_pool; | ||
LiteralCanonicalizer literal_canonicalizer(&literal_pool, 0); | ||
|
||
EXPECT_FALSE(literal_canonicalizer.Run(module0.get()).value()); | ||
EXPECT_TRUE(literal_canonicalizer.Run(module1.get()).value()); | ||
|
||
auto* c0 = Cast<HloConstantInstruction>( | ||
module0->entry_computation()->root_instruction()); | ||
auto* c1 = Cast<HloConstantInstruction>( | ||
module1->entry_computation()->root_instruction()); | ||
|
||
EXPECT_EQ(c0->literal(), c1->literal()); | ||
} | ||
|
||
} // namespace | ||
} // namespace xla |
Oops, something went wrong.