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,CustomCallTarget call_target,std::vector<OptionalSlice> operands,std::vector<OptionalSlice> results,const std::string & opaque)30 CustomCallThunk::CustomCallThunk(ThunkInfo thunk_info,
31 CustomCallTarget call_target,
32 std::vector<OptionalSlice> operands,
33 std::vector<OptionalSlice> results,
34 const std::string& opaque)
35 : Thunk(Thunk::kCustomCall, thunk_info),
36 call_target_(std::move(call_target)),
37 operands_(std::move(operands)),
38 results_(std::move(results)),
39 opaque_(opaque) {}
40
ExecuteOnStream(const ExecuteParams & params)41 Status CustomCallThunk::ExecuteOnStream(const ExecuteParams& params) {
42 // gpu_stream is CUstream or e.g. the equivalent type in ROCm.
43 std::vector<void*> buffers;
44 buffers.reserve(operands_.size() + results_.size());
45 for (const std::vector<OptionalSlice>& slices : {operands_, results_}) {
46 for (const OptionalSlice& slice : slices) {
47 if (slice) {
48 if (!slice->allocation())
49 return InternalError("custom call input missing buffer allocation");
50 buffers.push_back(
51 params.buffer_allocations->GetDeviceAddress(*slice).opaque());
52 } else {
53 buffers.push_back(nullptr);
54 }
55 }
56 }
57
58 #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
59 auto gpu_stream = se::gpu::AsGpuStreamValue(params.stream);
60 XlaCustomCallStatus custom_call_status;
61 call_target_(gpu_stream, buffers.data(), opaque_.data(), opaque_.size(),
62 &custom_call_status);
63 auto message = CustomCallStatusGetMessage(&custom_call_status);
64 if (message) {
65 return InternalError("CustomCall failed: %s", *message);
66 } else {
67 return OkStatus();
68 }
69 #else // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
70 return Unavailable(
71 "Custom calls on GPU are not supported in this configuration. Please "
72 "build with --config=cuda or --config=rocm");
73 #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
74 }
75
76 } // namespace gpu
77 } // namespace xla
78