diff options
author | Jingyue Wu <jingyue@google.com> | 2017-02-28 22:09:13 -0800 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2017-02-28 22:28:41 -0800 |
commit | ccf9a752f9f10cbee2f4f53b0d46a5951d620922 (patch) | |
tree | 4e9d2760d9505bc527b6587bd1f5879444e8ea0c /tensorflow/contrib/memory_stats/kernels | |
parent | 8777d9a15614b65271d25401f488b70b6b3dca1e (diff) |
Add new ops for memory statistics.
This CL adds only the MaxBytesInUse op, which collects the peak memory usage of
a device allocator. Other ops can be added similarly when demanded. For now, we
only enable MaxBytesInUse for GPU because memory statistics are unreliable for
CPU allocators.
This CL essentially merges part of Yaroslav Bulatov's work on
https://github.com/yaroslavvb/memory_probe_ops to TensorFlow.
Change: 148854571
Diffstat (limited to 'tensorflow/contrib/memory_stats/kernels')
-rw-r--r-- | tensorflow/contrib/memory_stats/kernels/memory_stats_ops.cc | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tensorflow/contrib/memory_stats/kernels/memory_stats_ops.cc b/tensorflow/contrib/memory_stats/kernels/memory_stats_ops.cc new file mode 100644 index 0000000000..c5a4a2de75 --- /dev/null +++ b/tensorflow/contrib/memory_stats/kernels/memory_stats_ops.cc @@ -0,0 +1,43 @@ +/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ +#include "tensorflow/core/framework/op_kernel.h" + +namespace tensorflow { + +// Op that measures the peak memory in bytes. +class MaxBytesInUseOp : public OpKernel { + public: + explicit MaxBytesInUseOp(OpKernelConstruction* context) : OpKernel(context) {} + + void Compute(OpKernelContext* context) override { + Allocator* allocator = + context->device()->GetAllocator(AllocatorAttributes()); + AllocatorStats allocator_stats; + allocator->GetStats(&allocator_stats); + + Tensor* output_tensor = nullptr; + OP_REQUIRES_OK( + context, context->allocate_output(0, TensorShape({}), &output_tensor)); + output_tensor->scalar<int64>()() = allocator_stats.max_bytes_in_use; + } +}; + +// MallocExtension_GetAllocatedSize doesn't return the allocated size reliably +// for CPU allocators, so we register this op on GPU only. +REGISTER_KERNEL_BUILDER( + Name("MaxBytesInUse").Device(DEVICE_GPU).HostMemory("out"), + MaxBytesInUseOp); + +} // namespace tensorflow |