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
16 #include "tensorflow/compiler/xla/service/gpu/custom_call_thunk.h"
17
18 #include "absl/strings/str_format.h"
19 #include "tensorflow/compiler/xla/service/buffer_assignment.h"
20 #include "tensorflow/compiler/xla/util.h"
21 #include "tensorflow/core/platform/errors.h"
22
23 #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
24 #include "tensorflow/stream_executor/gpu/gpu_stream.h"
25 #endif
26
27 namespace xla {
28 namespace gpu {
29
CustomCallThunk(ThunkInfo thunk_info,void * call_target,std::vector<BufferAllocation::Slice> operands,std::vector<BufferAllocation::Slice> results,const std::string & opaque)30 CustomCallThunk::CustomCallThunk(ThunkInfo thunk_info, void* call_target,
31 std::vector<BufferAllocation::Slice> operands,
32 std::vector<BufferAllocation::Slice> results,
33 const std::string& opaque)
34 : Thunk(Thunk::kCustomCall, thunk_info),
35 call_target_(call_target),
36 operands_(std::move(operands)),
37 results_(std::move(results)),
38 opaque_(opaque) {}
39
ExecuteOnStream(const ExecuteParams & params)40 Status CustomCallThunk::ExecuteOnStream(const ExecuteParams& params) {
41 // gpu_stream is CUstream or e.g. the equivalent type in ROCm.
42 std::vector<void*> buffers;
43 buffers.reserve(operands_.size() + results_.size());
44 for (const std::vector<BufferAllocation::Slice>& slices :
45 {operands_, results_}) {
46 for (const BufferAllocation::Slice& slice : slices) {
47 if (!slice.allocation())
48 return InternalError("custom call input missing buffer allocation");
49 buffers.push_back(
50 params.buffer_allocations->GetDeviceAddress(slice).opaque());
51 }
52 }
53
54 #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
55 auto gpu_stream = se::gpu::AsGpuStreamValue(params.stream);
56 using call_type = void (*)(decltype(gpu_stream), void** /*buffers*/,
57 const char* /*opaque*/, size_t /*opaque_len*/);
58 auto typed_call_target = reinterpret_cast<call_type>(call_target_);
59 typed_call_target(gpu_stream, buffers.data(), opaque_.data(), opaque_.size());
60 return Status::OK();
61 #else // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
62 return Unavailable(
63 "Custom calls on GPU are not supported in this configuration. Please "
64 "build with --config=cuda or --config=rocm");
65 #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
66 }
67
68 } // namespace gpu
69 } // namespace xla
70