• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2022 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/runtime_intrinsics.h"
17 
18 #include <string>
19 #include <utility>
20 
21 #include "absl/cleanup/cleanup.h"
22 #include "tensorflow/compiler/xla/service/custom_call_status.h"
23 #include "tensorflow/compiler/xla/service/custom_call_target_registry.h"
24 #include "tensorflow/compiler/xla/shape.h"
25 #include "tensorflow/compiler/xla/shape_util.h"
26 #include "tensorflow/compiler/xla/status.h"
27 #include "tensorflow/compiler/xla/statusor.h"
28 #include "tensorflow/compiler/xla/util.h"
29 #include "tensorflow/stream_executor/multi_platform_manager.h"
30 #include "tensorflow/stream_executor/stream.h"
31 
32 namespace xla {
33 
34 extern const char* const kXlaGpuAssertCustomCallTag = "__xla_gpu_assert";
35 
AssertOnGpu(void * stream_handle,void * buffer,absl::string_view error_msg)36 static Status AssertOnGpu(void* stream_handle, void* buffer,
37                           absl::string_view error_msg) {
38   TF_ASSIGN_OR_RETURN(se::Platform * platform,
39                       se::MultiPlatformManager::PlatformWithName("CUDA"));
40   se::StreamExecutorConfig config;
41   config.gpu_stream = stream_handle;
42   TF_ASSIGN_OR_RETURN(se::StreamExecutor * executor,
43                       platform->GetExecutor(config));
44   se::Stream* stream = executor->FindAllocatedStream(stream_handle);
45   if (!stream) {
46     return InternalError("Stream not found for: %p", stream_handle);
47   }
48 
49   int8_t expected = false;
50   int64_t byte_size = sizeof(int8_t);
51   CHECK_EQ(byte_size, ShapeUtil::ByteSizeOfPrimitiveType(PrimitiveType::PRED));
52   stream->ThenMemcpy(
53       &expected, se::DeviceMemoryBase{buffer, static_cast<uint64_t>(byte_size)},
54       byte_size);
55   TF_RETURN_IF_ERROR(stream->BlockHostUntilDone());
56   if (!static_cast<bool>(expected)) {
57     return InternalError("%s", error_msg);
58   }
59 
60   return Status::OK();
61 }
62 
AssertionCustomCall(void * stream_handle,void ** buffers,const char * opaque,int opaque_len,XlaCustomCallStatus * status)63 static void AssertionCustomCall(void* stream_handle, void** buffers,
64                                 const char* opaque, int opaque_len,
65                                 XlaCustomCallStatus* status) {
66   Status s =
67       AssertOnGpu(stream_handle, buffers[0],
68                   absl::string_view{opaque, static_cast<uint64_t>(opaque_len)});
69   if (!s.ok()) {
70     XlaCustomCallStatusSetFailure(status, s.error_message().c_str(),
71                                   s.error_message().size());
72   }
73 }
74 
75 XLA_REGISTER_CUSTOM_CALL_TARGET_WITH_SYM(kXlaGpuAssertCustomCallTag,
76                                          AssertionCustomCall, "CUDA");
77 
78 }  // namespace xla
79