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/hlo_execution_profiler.h"
25 #include "tensorflow/compiler/xla/service/gpu/sequential_thunk.h"
26 #include "tensorflow/compiler/xla/service/gpu/thunk.h"
27 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
28 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
29 
30 namespace xla {
31 namespace gpu {
32 
33 // ConditionalThunk implements the conditional instruction on GPU by reading the
34 // predicate of the conditional and executing the true or the false computation
35 // depending on the value of the predicate.
36 //
37 // ConditionalThunk assumes that the buffers of the conditional result and the
38 // result of the true and false computations share the same allocation. Also,
39 // the buffers of the true operand of the conditional and that of the parameter
40 // instruction of the true computation share the same allocation. Similarly, the
41 // buffers of the false operand and that of the parameter instruction of the
42 // false computation share the same allocation.
43 class ConditionalThunk : public Thunk {
44  public:
45   ConditionalThunk(
46       const BufferAllocation::Slice& branch_index_buffer_index,
47       absl::Span<const BufferAllocation::Slice> branch_operand_buffer_indexes,
48       std::vector<ThunkSequence> branch_thunk_sequences,
49       const HloInstruction* hlo);
50 
51   ConditionalThunk(const ConditionalThunk&) = delete;
52   ConditionalThunk& operator=(const ConditionalThunk&) = delete;
53 
54   Status Initialize(const GpuExecutable& executable,
55                     se::StreamExecutor* executor) override;
56   Status ExecuteOnStream(const BufferAllocations& buffer_allocations,
57                          se::Stream* stream,
58                          HloExecutionProfiler* profiler) override;
59 
60  private:
61   const bool branch_index_is_bool_;
62   BufferAllocation::Slice branch_index_buffer_index_;
63   std::vector<BufferAllocation::Slice> branch_operand_buffer_indexes_;
64   std::vector<std::unique_ptr<SequentialThunk>> branch_thunks_;
65 };
66 
67 }  // namespace gpu
68 }  // namespace xla
69 
70 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CONDITIONAL_THUNK_H_
71