1 /* Copyright 2021 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 #if (defined(GOOGLE_CUDA) && GOOGLE_CUDA) || \
16 (defined(TENSORFLOW_USE_ROCM) && TENSORFLOW_USE_ROCM)
17
18 #include "tensorflow/core/runtime_fallback/runtime/runtime_fallback_gpu_allocator.h"
19
20 #include "llvm/Support/Errc.h"
21 #include "tensorflow/core/platform/mutex.h"
22 #include "tfrt/gpu/gpu_types.h" // from @tf_runtime
23 #include "tfrt/support/ref_count.h" // from @tf_runtime
24 #include "tfrt/support/string_util.h" // from @tf_runtime
25
26 namespace tensorflow {
27
28 class RuntimeFallbackGpuAllocator : public tfrt::gpu::GpuAllocator {
29 public:
RuntimeFallbackGpuAllocator(tensorflow::Allocator * tf_gpu_allocator,const tfrt::gpu::wrapper::Context & context)30 explicit RuntimeFallbackGpuAllocator(
31 tensorflow::Allocator* tf_gpu_allocator,
32 const tfrt::gpu::wrapper::Context& context)
33 : tf_gpu_allocator_(tf_gpu_allocator), context_(context) {}
34 ~RuntimeFallbackGpuAllocator() override;
35
36 llvm::Expected<tfrt::gpu::GpuPointer> Allocate(
37 size_t size, tfrt::gpu::wrapper::Stream stream) override;
38
39 llvm::Error Deallocate(tfrt::gpu::GpuPointer pointer,
40 tfrt::gpu::wrapper::Stream stream) override;
41
42 private:
43 TF_DISALLOW_COPY_AND_ASSIGN(RuntimeFallbackGpuAllocator);
44
45 // Structures immutable after construction
46
47 // Does not own tf_gpu_allocator.
48 tensorflow::Allocator* tf_gpu_allocator_;
49
50 tfrt::gpu::wrapper::Context context_;
51
52 // Structures mutable after construction
53 mutable tensorflow::mutex mu_;
54
55 // Because we don't support multiple streams, stream_ is the stream
56 // for all allocations. All allocation requests on a different stream will be
57 // denied.
58 // We can't easily support "stream transitioning" now because:
59 // - we need to synchronize the former stream when we transition to the new
60 // stream.
61 // - the allocator is not notified when the stream is destroyed. So, the
62 // synchronization can happen after the stream is destroyed causing
63 // segfault.
64 tfrt::gpu::wrapper::Stream stream_ TF_GUARDED_BY(mu_);
65 };
66
~RuntimeFallbackGpuAllocator()67 RuntimeFallbackGpuAllocator::~RuntimeFallbackGpuAllocator() {}
68
Allocate(size_t size,tfrt::gpu::wrapper::Stream stream)69 llvm::Expected<tfrt::gpu::GpuPointer> RuntimeFallbackGpuAllocator::Allocate(
70 size_t size, tfrt::gpu::wrapper::Stream stream) {
71 {
72 tensorflow::mutex_lock lock(mu_);
73 if (stream_ == nullptr) {
74 stream_ = stream;
75 } else if (stream != stream_) {
76 return llvm::createStringError(
77 llvm::errc::invalid_argument,
78 "RuntimeFallbackGpuAllocator does not support multiple streams");
79 }
80 }
81 // tfrt::gpu::GpuAllocator::kAlignment is the minimum alignment. AllocateRaw
82 // adjusts alignment internally as needed.
83 void* gpu_ptr =
84 tf_gpu_allocator_->AllocateRaw(tfrt::gpu::GpuAllocator::kAlignment, size);
85
86 // TODO(zhangqiaorjc): AllocateRaw does LOG(WARNING) for different errors, it
87 // should return llvm::Error instead.
88 if (gpu_ptr == nullptr)
89 return llvm::createStringError(
90 llvm::errc::invalid_argument,
91 tfrt::StrCat("errors trying to allocate ", size));
92
93 return tfrt::gpu::wrapper::Pointer<void>(gpu_ptr, context_.platform());
94 }
95
Deallocate(tfrt::gpu::GpuPointer pointer,tfrt::gpu::wrapper::Stream stream)96 llvm::Error RuntimeFallbackGpuAllocator::Deallocate(
97 tfrt::gpu::GpuPointer pointer, tfrt::gpu::wrapper::Stream stream) {
98 tf_gpu_allocator_->DeallocateRaw(pointer.raw());
99 return llvm::Error::success();
100 }
101
CreateRuntimeFallbackGpuAllocatorFactory(tensorflow::Allocator * tf_gpu_allocator)102 tfrt::gpu::GpuAllocatorFactory CreateRuntimeFallbackGpuAllocatorFactory(
103 tensorflow::Allocator* tf_gpu_allocator) {
104 return [tf_gpu_allocator](const tfrt::gpu::wrapper::Context& context) {
105 return std::make_unique<RuntimeFallbackGpuAllocator>(tf_gpu_allocator,
106 context);
107 };
108 }
109
110 } // namespace tensorflow
111
112 #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
113