1 /* Copyright 2020 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/core/lib/core/errors.h"
17 #include "tensorflow/stream_executor/gpu/asm_compiler.h"
18 #include "tensorflow/stream_executor/gpu/gpu_driver.h"
19
20 namespace stream_executor {
21
22 #define RETURN_IF_CUDA_ERROR(expr) \
23 do { \
24 CUresult _status = expr; \
25 if (!SE_PREDICT_TRUE(_status == CUDA_SUCCESS)) { \
26 const char* error_string; \
27 cuGetErrorString(_status, &error_string); \
28 std::ostringstream oss; \
29 oss << error_string << "\nin " << __FILE__ << "(" << __LINE__ << "): '" \
30 << #expr << "'"; \
31 return port::Status(port::error::UNKNOWN, oss.str().c_str()); \
32 } \
33 } while (false)
34
LinkGpuAsm(gpu::GpuContext * context,std::vector<CubinOrPTXImage> images)35 port::StatusOr<std::vector<uint8>> LinkGpuAsm(
36 gpu::GpuContext* context, std::vector<CubinOrPTXImage> images) {
37 gpu::ScopedActivateContext activation(context);
38
39 CUlinkState link_state;
40 RETURN_IF_CUDA_ERROR(cuLinkCreate(0, nullptr, nullptr, &link_state));
41 for (auto& image : images) {
42 RETURN_IF_CUDA_ERROR(cuLinkAddData(
43 link_state, CU_JIT_INPUT_CUBIN, static_cast<void*>(image.bytes.data()),
44 image.bytes.size(), "", 0, nullptr, nullptr));
45 }
46 void* cubin_out;
47 size_t cubin_size;
48 RETURN_IF_CUDA_ERROR(cuLinkComplete(link_state, &cubin_out, &cubin_size));
49 std::vector<uint8> cubin(static_cast<uint8*>(cubin_out),
50 static_cast<uint8*>(cubin_out) + cubin_size);
51 RETURN_IF_CUDA_ERROR(cuLinkDestroy(link_state));
52 return std::move(cubin);
53 }
54
55 } // namespace stream_executor
56