1 /* Copyright 2015 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 #ifndef TENSORFLOW_CORE_KERNELS_CONV_OPS_GPU_H_ 17 #define TENSORFLOW_CORE_KERNELS_CONV_OPS_GPU_H_ 18 19 #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM 20 21 #include <tuple> 22 #include <unordered_map> 23 24 #include "absl/strings/str_cat.h" 25 #include "absl/strings/str_format.h" 26 #include "tensorflow/core/framework/op_kernel.h" 27 #include "tensorflow/core/kernels/gpu_utils.h" 28 #include "tensorflow/core/lib/gtl/inlined_vector.h" 29 #include "tensorflow/core/lib/hash/hash.h" 30 #include "tensorflow/core/util/tensor_format.h" 31 32 namespace tensorflow { 33 34 // Get the Dnn workspace limit from the environment variable, which is in MB. 35 // Return the workspace memory limit in bytes. If no value is set, return the 36 // default value. 37 int64 GetDnnWorkspaceLimit(const string& envvar_in_mb, 38 int64_t default_value_in_bytes); 39 40 // A class to provide scratch-space allocator for Stream-Executor Cudnn 41 // callback. TensorFlow is responsible for releasing the temporary buffers after 42 // the kernel finishes. 43 class DnnScratchAllocator : public se::ScratchAllocator { 44 public: ~DnnScratchAllocator()45 virtual ~DnnScratchAllocator() {} DnnScratchAllocator(int64_t memory_limit,OpKernelContext * context)46 DnnScratchAllocator(int64_t memory_limit, OpKernelContext* context) 47 : memory_limit_(memory_limit), total_byte_size_(0), context_(context) {} GetMemoryLimitInBytes()48 int64 GetMemoryLimitInBytes() override { return memory_limit_; } AllocateBytes(int64_t byte_size)49 se::port::StatusOr<se::DeviceMemory<uint8>> AllocateBytes( 50 int64_t byte_size) override { 51 Tensor temporary_memory; 52 if (byte_size < 0) { 53 return se::port::Status{se::port::error::INVALID_ARGUMENT, 54 "Requested negative byte size!"}; 55 } 56 if (byte_size > memory_limit_) { 57 return se::port::Status{se::port::error::UNAVAILABLE, 58 absl::StrCat("Requested memory size (", byte_size, 59 ") exceeds the max memory limit (", 60 memory_limit_, ").")}; 61 } 62 AllocationAttributes allocation_attr; 63 allocation_attr.retry_on_failure = false; 64 Status allocation_status(context_->allocate_temp( 65 DT_UINT8, TensorShape({byte_size}), &temporary_memory, 66 AllocatorAttributes(), allocation_attr)); 67 if (!allocation_status.ok()) { 68 return se::port::Status{ 69 se::port::error::UNAVAILABLE, 70 absl::StrCat("Failed to allocate the requested memory size (", 71 byte_size, ").")}; 72 } 73 // Hold the reference of the allocated tensors until the end of the 74 // allocator. 75 allocated_tensors_.push_back(temporary_memory); 76 total_byte_size_ += byte_size; 77 return se::port::StatusOr<se::DeviceMemory<uint8>>( 78 AsDeviceMemory(temporary_memory.flat<uint8>().data(), 79 temporary_memory.flat<uint8>().size())); 80 } TotalByteSize()81 int64 TotalByteSize() { return total_byte_size_; } 82 83 private: 84 int64 memory_limit_; 85 int64 total_byte_size_; 86 OpKernelContext* context_; 87 std::vector<Tensor> allocated_tensors_; 88 }; 89 90 91 typedef Eigen::GpuDevice GPUDevice; 92 93 } // namespace tensorflow 94 95 #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM 96 97 #endif // TENSORFLOW_CORE_KERNELS_CONV_OPS_GPU_H_ 98