xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/gpu/conditional_thunk.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2017 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_GPU_CONDITIONAL_THUNK_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CONDITIONAL_THUNK_H_
18 
19 #include <memory>
20 #include <vector>
21 
22 #include "absl/types/span.h"
23 #include "tensorflow/compiler/xla/service/gpu/buffer_allocations.h"
24 #include "tensorflow/compiler/xla/service/gpu/sequential_thunk.h"
25 #include "tensorflow/compiler/xla/service/gpu/thunk.h"
26 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
27 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
28 
29 namespace xla {
30 namespace gpu {
31 
32 struct ConditionalThunkConfig {
33   bool branch_index_is_bool;
34   int64_t branch_count;
35   std::vector<std::unique_ptr<SequentialThunk>> branch_thunks;
36 };
37 
38 // ConditionalThunk implements the conditional instruction on GPU by reading the
39 // predicate of the conditional and executing the true or the false computation
40 // depending on the value of the predicate.
41 //
42 // ConditionalThunk assumes that the buffers of the conditional result and the
43 // result of the true and false computations share the same allocation. Also,
44 // the buffers of the true operand of the conditional and that of the parameter
45 // instruction of the true computation share the same allocation. Similarly, the
46 // buffers of the false operand and that of the parameter instruction of the
47 // false computation share the same allocation.
48 class ConditionalThunk : public Thunk {
49  public:
50   ConditionalThunk(ThunkInfo thunk_info, ConditionalThunkConfig config,
51                    const BufferAllocation::Slice& branch_index_buffer_index);
52 
53   ConditionalThunk(const ConditionalThunk&) = delete;
54   ConditionalThunk& operator=(const ConditionalThunk&) = delete;
55 
56   Status Initialize(const GpuExecutable& executable,
57                     se::StreamExecutor* executor) override;
58   Status ExecuteOnStream(const ExecuteParams& params) override;
59 
60  private:
61   const ConditionalThunkConfig config_;
62   BufferAllocation::Slice branch_index_buffer_index_;
63 };
64 
65 }  // namespace gpu
66 }  // namespace xla
67 
68 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CONDITIONAL_THUNK_H_
69