xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/sharding_propagation.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_SHARDING_PROPAGATION_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_SHARDING_PROPAGATION_H_
18 
19 #include <memory>
20 #include <optional>
21 #include <utility>
22 #include <vector>
23 
24 #include "tensorflow/compiler/xla/service/custom_call_sharding_helper.h"
25 #include "tensorflow/compiler/xla/service/hlo_module.h"
26 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h"
27 #include "tensorflow/compiler/xla/statusor.h"
28 
29 namespace xla {
30 
31 // Remove Sharding custom-call instruction by folding the sharding attribute
32 // to its operand. If the operand already has a different sharding, insert a
33 // copy node for reshard.
34 // partially_specified will be populated with the converted copies if the custom
35 // call is partially specified.
36 StatusOr<bool> ProcessShardingInstruction(
37     HloModule* module,
38     const absl::flat_hash_set<absl::string_view>& execution_threads,
39     bool replace_sharding_with_copy,
40     absl::flat_hash_map<const HloInstruction*, std::vector<int64_t>>*
41         unspecified_dims);
42 
43 // Infers broadcast ops' operand sharding, based on its output sharding.
44 std::optional<HloSharding> InferBroadcastOperandSharding(
45     const HloInstruction& instruction, bool is_spmd = true);
46 
47 // Propagates sharding information around the graph. HLOs that have shardings
48 // are kept as-is, those that do not have shardings are given shardings based on
49 // a simple local greedy heuristic.
50 class ShardingPropagation : public HloModulePass {
51  public:
52   using ComputationMap =
53       absl::flat_hash_map<const HloComputation*, HloInstruction*>;
54   explicit ShardingPropagation(
55       bool is_spmd = false, bool propagate_metadata = false,
56       bool allow_spmd_sharding_propagation_to_output = false,
57       bool cse_prevention_only = false,
58       std::unique_ptr<CustomCallShardingHelper> sharding_helper = nullptr)
is_spmd_(is_spmd)59       : is_spmd_(is_spmd),
60         propagate_metadata_(propagate_metadata),
61         allow_spmd_sharding_propagation_to_output_(
62             allow_spmd_sharding_propagation_to_output),
63         cse_prevention_only_(cse_prevention_only) {
64     if (sharding_helper) {
65       sharding_helper_ = std::move(sharding_helper);
66     } else {
67       sharding_helper_ = std::make_unique<CustomCallShardingHelper>();
68     }
69   }
name()70   absl::string_view name() const override { return "sharding-propagation"; }
71   using HloPassInterface::Run;
72   StatusOr<bool> Run(
73       HloModule* module,
74       const absl::flat_hash_set<absl::string_view>& execution_threads) override;
75 
76   // Function which can be used to apply a spatially partitioned sharding onto a
77   // given domain. It will apply the sharding into the exit edges of the domain
78   // and then rely on the rest of sharding propagation to ensure that the
79   // intermediate nodes get the correct sharding.
80   static Status NormalizeDomain(const DomainMetadata::Domain& domain,
81                                 const DomainMetadata* metadata);
82 
83   static std::optional<HloSharding> GetShardingFromUser(
84       const HloInstruction& instruction, const HloInstruction& user,
85       int64_t aggressiveness, bool is_spmd);
86 
87  private:
88   bool InferShardingFromOperands(HloInstruction* instruction,
89                                  const ComputationMap& computation_map,
90                                  int64_t aggressiveness);
91 
92   std::unique_ptr<CustomCallShardingHelper> sharding_helper_;
93   bool is_spmd_;
94   bool propagate_metadata_;
95   bool allow_spmd_sharding_propagation_to_output_;
96   // If true, the pass keeps the propagation results only on selected
97   // instructions to prevent CSE across unrelated subgraphs. (A common case is
98   // scalar broadcasts).
99   bool cse_prevention_only_;
100 };
101 
102 }  // namespace xla
103 
104 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_SHARDING_PROPAGATION_H_
105