1 /* Copyright 2021 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_SPMD_STATEFUL_RNG_SPMD_PARTITIONER_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_SPMD_STATEFUL_RNG_SPMD_PARTITIONER_H_ 18 19 #include "tensorflow/compiler/xla/service/hlo_computation.h" 20 #include "tensorflow/compiler/xla/service/hlo_instruction.h" 21 #include "tensorflow/compiler/xla/service/hlo_module.h" 22 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h" 23 #include "tensorflow/compiler/xla/service/spmd/spmd_partitioner.h" 24 25 namespace xla { 26 namespace spmd { 27 28 class StatefulRngSpmdPartitioningVisitor 29 : public spmd::SpmdPartitioningVisitor { 30 public: StatefulRngSpmdPartitioningVisitor(HloComputation * computation,int64_t num_partitions,int64_t num_replicas,const spmd::SPMDCollectiveOpsCreator & collective_ops_creator,int64_t * next_channel_id,spmd::SpmdLogger * logger,spmd::SpmdPartitionerOptions options,spmd::SpmdPartitioner * partitioner)31 StatefulRngSpmdPartitioningVisitor( 32 HloComputation* computation, int64_t num_partitions, int64_t num_replicas, 33 const spmd::SPMDCollectiveOpsCreator& collective_ops_creator, 34 int64_t* next_channel_id, spmd::SpmdLogger* logger, 35 spmd::SpmdPartitionerOptions options, spmd::SpmdPartitioner* partitioner) 36 : spmd::SpmdPartitioningVisitor(computation, num_partitions, num_replicas, 37 collective_ops_creator, next_channel_id, 38 logger, std::move(options), partitioner) { 39 } 40 Status HandleRngGetAndUpdateState(HloInstruction* hlo) override; 41 }; 42 43 class StatefulRngSpmdPartitioner : public spmd::SpmdPartitioner { 44 public: StatefulRngSpmdPartitioner(int64_t num_partitions,int64_t num_replicas)45 StatefulRngSpmdPartitioner(int64_t num_partitions, int64_t num_replicas) 46 : spmd::SpmdPartitioner(num_partitions, num_replicas, 47 GetSpmdPartitionerOptions()) {} 48 49 protected: 50 std::unique_ptr<spmd::SpmdPartitioningVisitor> CreateVisitor( 51 HloComputation* computation, int64_t num_partitions, int64_t num_replicas, 52 const spmd::SPMDCollectiveOpsCreator& collective_ops_creator, 53 int64_t* next_channel_id, spmd::SpmdLogger* logger, 54 spmd::SpmdPartitionerOptions options) override; 55 56 Status PreprocessSharding( 57 HloModule* module, 58 const absl::flat_hash_set<absl::string_view>& execution_threads) override; 59 bool CanSideEffectingHaveReplicatedSharding( 60 const HloInstruction* hlo) override; 61 62 private: GetSpmdPartitionerOptions()63 static spmd::SpmdPartitionerOptions GetSpmdPartitionerOptions() { 64 spmd::SpmdPartitionerOptions options; 65 options.allow_module_signature_change = true; 66 // Setting windowed einsum threshold to be large to disable it for GPU by 67 // default. 68 options.threshold_for_windowed_einsum_mib = 100000; 69 return options; 70 } 71 }; 72 73 } // namespace spmd 74 } // namespace xla 75 76 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_SPMD_STATEFUL_RNG_SPMD_PARTITIONER_H_ 77