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 struct ConditionalThunkConfig { 34 bool branch_index_is_bool; 35 int64 branch_count; 36 std::vector<std::unique_ptr<SequentialThunk>> branch_thunks; 37 std::vector<absl::optional<size_t>> branch_profile_indices; 38 }; 39 40 ConditionalThunkConfig GetConditionalThunkConfig( 41 const HloInstruction* instr, 42 std::vector<ThunkSequence> branch_thunk_sequences, 43 std::vector<absl::optional<size_t>> branch_profile_indices); 44 45 // ConditionalThunk implements the conditional instruction on GPU by reading the 46 // predicate of the conditional and executing the true or the false computation 47 // depending on the value of the predicate. 48 // 49 // ConditionalThunk assumes that the buffers of the conditional result and the 50 // result of the true and false computations share the same allocation. Also, 51 // the buffers of the true operand of the conditional and that of the parameter 52 // instruction of the true computation share the same allocation. Similarly, the 53 // buffers of the false operand and that of the parameter instruction of the 54 // false computation share the same allocation. 55 class ConditionalThunk : public Thunk { 56 public: 57 ConditionalThunk( 58 ThunkInfo thunk_info, ConditionalThunkConfig config, 59 const BufferAllocation::Slice& branch_index_buffer_index, 60 absl::Span<const BufferAllocation::Slice> branch_operand_buffer_indexes); 61 62 ConditionalThunk(const ConditionalThunk&) = delete; 63 ConditionalThunk& operator=(const ConditionalThunk&) = delete; 64 65 Status Initialize(const GpuExecutable& executable, 66 se::StreamExecutor* executor) override; 67 Status ExecuteOnStream(const ExecuteParams& params) override; 68 69 private: 70 const ConditionalThunkConfig config_; 71 BufferAllocation::Slice branch_index_buffer_index_; 72 std::vector<BufferAllocation::Slice> branch_operand_buffer_indexes_; 73 }; 74 75 } // namespace gpu 76 } // namespace xla 77 78 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CONDITIONAL_THUNK_H_ 79