xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/gpu/gemm_broadcast_folding_rewriter.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2019 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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_GEMM_BROADCAST_FOLDING_REWRITER_H_
16 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_GEMM_BROADCAST_FOLDING_REWRITER_H_
17 
18 #include <optional>
19 
20 #include "tensorflow/compiler/xla/service/hlo_instructions.h"
21 #include "tensorflow/compiler/xla/service/hlo_module.h"
22 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h"
23 
24 namespace xla {
25 namespace gpu {
26 
27 // cuBLAS GEMM has support for strided batched calls, where the stride is used
28 // to determine the offset between the batches.
29 //
30 // This allows (kCustomCall:gemm A kBroadcast(B)) or
31 //             (kCustomCall:gemm kBroadcast(A) B)
32 // to be rewritten as (kCustomCall:gemm A B) with a zero stride for the
33 // broadcasted operand if the broadcast operates on all the batch dimensions.
34 //
35 // This pattern matches the above case and removes the unnecessary broadcast.
36 class GemmBroadcastFoldingRewriter : public HloModulePass {
37  public:
name()38   absl::string_view name() const override {
39     return "cublas-gemm-broadcast-folding-rewriter";
40   }
41 
42   using HloPassInterface::Run;
43   StatusOr<bool> Run(
44       HloModule* module,
45       const absl::flat_hash_set<absl::string_view>& execution_threads) override;
46 };
47 
48 }  // namespace gpu
49 }  // namespace xla
50 
51 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_GEMM_BROADCAST_FOLDING_REWRITER_H_
52