1 /* Copyright 2017 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 #include "tensorflow/core/framework/op_kernel.h" 16 17 namespace tensorflow { 18 19 // Base class of ops that collects statistics of the allocator of a device. 20 class MemoryStatsOp : public OpKernel { 21 public: MemoryStatsOp(OpKernelConstruction * context)22 explicit MemoryStatsOp(OpKernelConstruction* context) : OpKernel(context) {} 23 Compute(OpKernelContext * context)24 void Compute(OpKernelContext* context) override { 25 Allocator* allocator = 26 context->device()->GetAllocator(AllocatorAttributes()); 27 absl::optional<AllocatorStats> allocator_stats = allocator->GetStats(); 28 if (!allocator_stats) { 29 *allocator_stats = AllocatorStats(); 30 } 31 32 Tensor* output_tensor = nullptr; 33 OP_REQUIRES_OK( 34 context, context->allocate_output(0, TensorShape({}), &output_tensor)); 35 output_tensor->scalar<int64>()() = ExtractAllocatorStats(*allocator_stats); 36 } 37 38 protected: 39 // Extracts a certain field (determined by subclasses) from an allocator 40 // stats. 41 virtual int64 ExtractAllocatorStats( 42 const AllocatorStats& allocator_stats) const = 0; 43 }; 44 45 // Op that measures current memory in bytes. 46 class BytesInUseOp : public MemoryStatsOp { 47 public: BytesInUseOp(OpKernelConstruction * context)48 explicit BytesInUseOp(OpKernelConstruction* context) 49 : MemoryStatsOp(context) {} 50 51 private: ExtractAllocatorStats(const AllocatorStats & allocator_stats) const52 int64 ExtractAllocatorStats( 53 const AllocatorStats& allocator_stats) const override { 54 return allocator_stats.bytes_in_use; 55 } 56 }; 57 58 // Register this op on GPU only, see comment for MaxBytesInUse for reason 59 REGISTER_KERNEL_BUILDER(Name("BytesInUse").Device(DEVICE_GPU).HostMemory("out"), 60 BytesInUseOp); 61 62 #ifdef TENSORFLOW_USE_SYCL 63 REGISTER_KERNEL_BUILDER( 64 Name("BytesInUse").Device(DEVICE_SYCL).HostMemory("out"), BytesInUseOp); 65 #endif // TENSORFLOW_USE_SYCL 66 67 // Op that measures the total memory (in bytes) of a device. 68 class BytesLimitOp : public MemoryStatsOp { 69 public: BytesLimitOp(OpKernelConstruction * context)70 explicit BytesLimitOp(OpKernelConstruction* context) 71 : MemoryStatsOp(context) {} 72 73 private: ExtractAllocatorStats(const AllocatorStats & allocator_stats) const74 int64 ExtractAllocatorStats( 75 const AllocatorStats& allocator_stats) const override { 76 return allocator_stats.bytes_limit ? *allocator_stats.bytes_limit : -1; 77 } 78 }; 79 80 REGISTER_KERNEL_BUILDER(Name("BytesLimit").Device(DEVICE_CPU), BytesLimitOp); 81 REGISTER_KERNEL_BUILDER(Name("BytesLimit").Device(DEVICE_GPU).HostMemory("out"), 82 BytesLimitOp); 83 84 #ifdef TENSORFLOW_USE_SYCL 85 REGISTER_KERNEL_BUILDER( 86 Name("BytesLimit").Device(DEVICE_SYCL).HostMemory("out"), BytesLimitOp); 87 #endif // TENSORFLOW_USE_SYCL 88 89 // Op that measures the peak memory in bytes. 90 class MaxBytesInUseOp : public MemoryStatsOp { 91 public: MaxBytesInUseOp(OpKernelConstruction * context)92 explicit MaxBytesInUseOp(OpKernelConstruction* context) 93 : MemoryStatsOp(context) {} 94 95 private: ExtractAllocatorStats(const AllocatorStats & allocator_stats) const96 int64 ExtractAllocatorStats( 97 const AllocatorStats& allocator_stats) const override { 98 return allocator_stats.peak_bytes_in_use; 99 } 100 }; 101 102 // MallocExtension_GetAllocatedSize doesn't return the allocated size reliably 103 // for CPU allocators, so we register this op on GPU only. 104 REGISTER_KERNEL_BUILDER( 105 Name("MaxBytesInUse").Device(DEVICE_GPU).HostMemory("out"), 106 MaxBytesInUseOp); 107 108 #ifdef TENSORFLOW_USE_SYCL 109 REGISTER_KERNEL_BUILDER( 110 Name("MaxBytesInUse").Device(DEVICE_SYCL).HostMemory("out"), 111 MaxBytesInUseOp); 112 #endif // TENSORFLOW_USE_SYCL 113 114 } // namespace tensorflow 115