|
| 1 | +/* Copyright 2024 The OpenXLA Authors. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +#include "xla/hlo/transforms/literal_canonicalizer.h" |
| 17 | + |
| 18 | +#include <cstddef> |
| 19 | + |
| 20 | +#include "absl/container/flat_hash_set.h" |
| 21 | +#include "absl/status/status.h" |
| 22 | +#include "absl/status/statusor.h" |
| 23 | +#include "absl/strings/string_view.h" |
| 24 | +#include "xla/hlo/ir/dfs_hlo_visitor.h" |
| 25 | +#include "xla/hlo/ir/dfs_hlo_visitor_with_default.h" |
| 26 | +#include "xla/hlo/ir/hlo_casting_utils.h" |
| 27 | +#include "xla/hlo/ir/hlo_instruction.h" |
| 28 | +#include "xla/hlo/ir/hlo_instructions.h" |
| 29 | +#include "xla/hlo/ir/hlo_module.h" |
| 30 | +#include "xla/literal_pool.h" |
| 31 | +#include "tsl/platform/errors.h" |
| 32 | +#include "tsl/platform/logging.h" |
| 33 | + |
| 34 | +namespace xla { |
| 35 | +namespace { |
| 36 | + |
| 37 | +class LiteralCanonicalizerVisitor : public DfsHloRewriteVisitor { |
| 38 | + public: |
| 39 | + LiteralCanonicalizerVisitor(LiteralPool* literal_pool, size_t min_size_bytes) |
| 40 | + : literal_pool_(literal_pool), min_size_bytes_(min_size_bytes) {} |
| 41 | + |
| 42 | + absl::Status HandleConstant(HloInstruction* hlo) final { |
| 43 | + auto* constant = Cast<HloConstantInstruction>(hlo); |
| 44 | + if (constant->HasLiteral() && |
| 45 | + constant->literal().size_bytes() >= min_size_bytes_) { |
| 46 | + MarkAsMaybeChanged(constant->Canonicalize(literal_pool_)); |
| 47 | + } |
| 48 | + return absl::OkStatus(); |
| 49 | + } |
| 50 | + |
| 51 | + private: |
| 52 | + LiteralPool* literal_pool_; |
| 53 | + size_t min_size_bytes_; |
| 54 | +}; |
| 55 | + |
| 56 | +} // namespace |
| 57 | + |
| 58 | +LiteralCanonicalizer::LiteralCanonicalizer(LiteralPool* literal_pool, |
| 59 | + size_t min_size_bytes) |
| 60 | + : literal_pool_(literal_pool), min_size_bytes_(min_size_bytes) {} |
| 61 | + |
| 62 | +absl::StatusOr<bool> LiteralCanonicalizer::Run( |
| 63 | + HloModule* module, |
| 64 | + const absl::flat_hash_set<absl::string_view>& execution_threads) { |
| 65 | + // Every time we canonicalize literals in a module, we garbage collect expired |
| 66 | + // literals from the pool. |
| 67 | + size_t num_erased = literal_pool_->GarbageCollect(); |
| 68 | + VLOG(3) << "Garbage collected " << num_erased << " expired literals"; |
| 69 | + |
| 70 | + LiteralCanonicalizerVisitor visitor(literal_pool_, min_size_bytes_); |
| 71 | + TF_RETURN_IF_ERROR(module->entry_computation()->Accept(&visitor)); |
| 72 | + return visitor.changed(); |
| 73 | +} |
| 74 | + |
| 75 | +} // namespace xla |
0 commit comments