aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/memory_stats
diff options
context:
space:
mode:
authorGravatar Jingyue Wu <jingyue@google.com>2017-02-28 22:09:13 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2017-02-28 22:28:41 -0800
commitccf9a752f9f10cbee2f4f53b0d46a5951d620922 (patch)
tree4e9d2760d9505bc527b6587bd1f5879444e8ea0c /tensorflow/contrib/memory_stats
parent8777d9a15614b65271d25401f488b70b6b3dca1e (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')
-rw-r--r--tensorflow/contrib/memory_stats/BUILD74
-rw-r--r--tensorflow/contrib/memory_stats/__init__.py20
-rw-r--r--tensorflow/contrib/memory_stats/kernels/memory_stats_ops.cc43
-rw-r--r--tensorflow/contrib/memory_stats/ops/memory_stats_ops.cc21
-rw-r--r--tensorflow/contrib/memory_stats/python/kernel_tests/memory_stats_ops_test.py63
-rw-r--r--tensorflow/contrib/memory_stats/python/ops/memory_stats_ops.py30
6 files changed, 251 insertions, 0 deletions
diff --git a/tensorflow/contrib/memory_stats/BUILD b/tensorflow/contrib/memory_stats/BUILD
new file mode 100644
index 0000000000..bbb78383fa
--- /dev/null
+++ b/tensorflow/contrib/memory_stats/BUILD
@@ -0,0 +1,74 @@
+# Description:
+# Ops that get statistics on memory allocators.
+
+licenses(["notice"]) # Apache 2.0
+
+exports_files(["LICENSE"])
+
+package(default_visibility = ["//tensorflow:__subpackages__"])
+
+load("//tensorflow:tensorflow.bzl", "tf_custom_op_library")
+load("//tensorflow:tensorflow.bzl", "tf_gen_op_libs")
+load("//tensorflow:tensorflow.bzl", "tf_gen_op_wrapper_py")
+load("//tensorflow:tensorflow.bzl", "cuda_py_test")
+
+tf_custom_op_library(
+ name = "python/ops/_memory_stats_ops.so",
+ srcs = [
+ "kernels/memory_stats_ops.cc",
+ "ops/memory_stats_ops.cc",
+ ],
+)
+
+tf_gen_op_libs(
+ op_lib_names = ["memory_stats_ops"],
+ deps = [
+ "//tensorflow/core:lib",
+ ],
+)
+
+tf_gen_op_wrapper_py(
+ name = "memory_stats_ops",
+ deps = [":memory_stats_ops_op_lib"],
+)
+
+py_library(
+ name = "memory_stats_py",
+ srcs = [
+ "__init__.py",
+ "python/ops/memory_stats_ops.py",
+ ],
+ data = [
+ ":python/ops/_memory_stats_ops.so",
+ ],
+ srcs_version = "PY2AND3",
+ visibility = ["//visibility:public"],
+ deps = [
+ ":memory_stats_ops",
+ "//tensorflow/contrib/util:util_py",
+ ],
+)
+
+cuda_py_test(
+ name = "memory_stats_ops_test",
+ size = "small",
+ srcs = ["python/kernel_tests/memory_stats_ops_test.py"],
+ additional_deps = [
+ ":memory_stats_py",
+ "//tensorflow/python:client_testlib",
+ "//tensorflow/python:math_ops",
+ "//tensorflow/python:random_ops",
+ ],
+)
+
+filegroup(
+ name = "all_files",
+ srcs = glob(
+ ["**/*"],
+ exclude = [
+ "**/METADATA",
+ "**/OWNERS",
+ ],
+ ),
+ visibility = ["//tensorflow:__subpackages__"],
+)
diff --git a/tensorflow/contrib/memory_stats/__init__.py b/tensorflow/contrib/memory_stats/__init__.py
new file mode 100644
index 0000000000..78146b701e
--- /dev/null
+++ b/tensorflow/contrib/memory_stats/__init__.py
@@ -0,0 +1,20 @@
+# 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.
+# ==============================================================================
+"""Ops for memory statistics."""
+
+from tensorflow.contrib.memory_stats.python.ops.memory_stats_ops import MaxBytesInUse
+
+from tensorflow.python.util.all_util import remove_undocumented
+remove_undocumented(__name__)
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
diff --git a/tensorflow/contrib/memory_stats/ops/memory_stats_ops.cc b/tensorflow/contrib/memory_stats/ops/memory_stats_ops.cc
new file mode 100644
index 0000000000..cdca15388a
--- /dev/null
+++ b/tensorflow/contrib/memory_stats/ops/memory_stats_ops.cc
@@ -0,0 +1,21 @@
+/* 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.h"
+
+namespace tensorflow {
+
+REGISTER_OP("MaxBytesInUse").Output("out: int64").SetIsStateful();
+
+} // namespace tensorflow
diff --git a/tensorflow/contrib/memory_stats/python/kernel_tests/memory_stats_ops_test.py b/tensorflow/contrib/memory_stats/python/kernel_tests/memory_stats_ops_test.py
new file mode 100644
index 0000000000..b0d23b8155
--- /dev/null
+++ b/tensorflow/contrib/memory_stats/python/kernel_tests/memory_stats_ops_test.py
@@ -0,0 +1,63 @@
+# 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.
+# ==============================================================================
+"""Tests for memory statistics ops."""
+
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+from tensorflow.contrib.memory_stats.python.ops import memory_stats_ops
+from tensorflow.python.framework import dtypes
+from tensorflow.python.framework import tensor_shape
+from tensorflow.python.framework import test_util
+from tensorflow.python.ops import math_ops
+from tensorflow.python.ops import random_ops
+from tensorflow.python.platform import test
+
+
+class MemoryStatsOpsTest(test_util.TensorFlowTestCase):
+
+ # Tests the peak memory usage of the following computation.
+ # a b
+ # | / |
+ # c |
+ # \ |
+ # \ |
+ # d
+ # The memory for matrix "a" can be reused for matrix "d". Therefore, this
+ # computation needs space for only three matrix plus some small overhead.
+ def testChainOfMatmul(self):
+ # MaxBytesInUse is registerd on GPU only. See kernels/memory_stats_ops.cc.
+ if not test.is_gpu_available():
+ return
+
+ with self.test_session(use_gpu=True) as sess:
+ matrix_size = 64
+ matrix_shape = tensor_shape.TensorShape([matrix_size, matrix_size])
+ dtype = dtypes.float32
+ matrix_size_in_bytes = matrix_shape.num_elements() * dtype.size
+ a = random_ops.random_uniform(matrix_shape, dtype=dtype)
+ b = random_ops.random_uniform(matrix_shape, dtype=dtype)
+ c = math_ops.matmul(a, b)
+ d = math_ops.matmul(c, b)
+ sess.run(d)
+
+ max_bytes_in_use = sess.run(memory_stats_ops.MaxBytesInUse())
+ self.assertGreaterEqual(max_bytes_in_use, matrix_size_in_bytes * 3)
+ self.assertLess(max_bytes_in_use, matrix_size_in_bytes * 4)
+
+
+if __name__ == '__main__':
+ test.main()
diff --git a/tensorflow/contrib/memory_stats/python/ops/memory_stats_ops.py b/tensorflow/contrib/memory_stats/python/ops/memory_stats_ops.py
new file mode 100644
index 0000000000..0e0772e6be
--- /dev/null
+++ b/tensorflow/contrib/memory_stats/python/ops/memory_stats_ops.py
@@ -0,0 +1,30 @@
+# 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.
+# ==============================================================================
+"""Ops for memory statistics."""
+
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+from tensorflow.contrib.memory_stats.ops import gen_memory_stats_ops
+from tensorflow.contrib.util import loader
+from tensorflow.python.platform import resource_loader
+
+_memory_stats_ops_so = loader.load_op_library(
+ resource_loader.get_path_to_datafile("_memory_stats_ops.so"))
+
+
+def MaxBytesInUse():
+ return gen_memory_stats_ops.max_bytes_in_use()