Home | History | Annotate | Download | only in kernels
      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 #include "tensorflow/core/common_runtime/kernel_benchmark_testlib.h"
     17 #include "tensorflow/core/framework/node_def_builder.h"
     18 #include "tensorflow/core/framework/op.h"
     19 #include "tensorflow/core/framework/op_kernel.h"
     20 #include "tensorflow/core/framework/tensor.h"
     21 #include "tensorflow/core/framework/tensor_shape.h"
     22 #include "tensorflow/core/framework/tensor_types.h"
     23 #include "tensorflow/core/framework/types.pb.h"
     24 #include "tensorflow/core/graph/graph.h"
     25 #include "tensorflow/core/kernels/ops_testutil.h"
     26 #include "tensorflow/core/platform/test_benchmark.h"
     27 #include "tensorflow/core/platform/types.h"
     28 
     29 namespace tensorflow {
     30 
     31 class ConstantOpTest : public OpsTestBase {
     32  protected:
     33   void PersistentMemoryTrackingTest(bool on_gpu);
     34 };
     35 
     36 void ConstantOpTest::PersistentMemoryTrackingTest(bool on_gpu) {
     37   DataType data_type = DT_INT32;
     38   std::initializer_list<int64> dims = {2, 3, 4, 5};
     39   Tensor tensor(data_type, TensorShape(dims));
     40   for (int i = 0; i < 2 * 3 * 4 * 5; ++i) {
     41     tensor.flat<int32>()(i) = i;
     42   }
     43 
     44   NodeDef const_node;
     45   TF_ASSERT_OK(NodeDefBuilder("some_node", "Const")
     46                    .Attr("dtype", data_type)
     47                    .Attr("value", tensor)
     48                    .Finalize(&const_node));
     49 
     50   string device_string = "CPU";
     51   DeviceType device_type = DEVICE_CPU;
     52   if (on_gpu) {
     53     device_string = "GPU";
     54     DeviceType device_type = DEVICE_GPU;
     55   }
     56   std::unique_ptr<Device> device(DeviceFactory::NewDevice(
     57       device_string, {}, "/job:worker/replica:0/task:0"));
     58 
     59   Status status;
     60   std::unique_ptr<OpKernel> op(CreateOpKernel(device_type, device.get(),
     61                                               cpu_allocator(), const_node,
     62                                               TF_GRAPH_DEF_VERSION, &status));
     63 
     64   OpKernelContext::Params params;
     65   params.device = device.get();
     66   params.frame_iter = FrameAndIter(0, 0);
     67   params.op_kernel = op.get();
     68   params.track_allocations = true;
     69 
     70   OpKernelContext ctx(&params);
     71   op->Compute(&ctx);
     72   TF_EXPECT_OK(ctx.status());
     73 
     74   if (on_gpu) {
     75     EXPECT_EQ(ctx.persistent_memory_allocated(), 512);
     76   } else {
     77     EXPECT_EQ(ctx.persistent_memory_allocated(), 480);
     78   }
     79 
     80   // Remove memory leak errors.
     81   for (auto allocator_pair : ctx.wrapped_allocators()) {
     82     allocator_pair.second->GetRecordsAndUnRef();
     83   }
     84 }
     85 
     86 TEST_F(ConstantOpTest, PersistentMemoryTracking) {
     87   PersistentMemoryTrackingTest(false);
     88 #if GOOGLE_CUDA
     89   PersistentMemoryTrackingTest(true);
     90 #endif  // GOOGLE_CUDA
     91 }
     92 
     93 // Returns graph containing "num" const nodes.  If 'sequential' is
     94 // true, make sure all constants are executed sequentially in the
     95 // graph by adding control dependencies.
     96 static Graph* ManyConsts(int num, bool sequential) {
     97   Graph* g = new Graph(OpRegistry::Global());
     98   Node* prev = nullptr;
     99   for (int i = 0; i < num; ++i) {
    100     Tensor c(DT_FLOAT, TensorShape({}));
    101     c.scalar<float>()() = i;
    102     Node* curr = test::graph::Constant(g, c);
    103     if (sequential && prev != nullptr) {
    104       g->AddControlEdge(prev, curr);
    105     }
    106     prev = curr;
    107   }
    108   return g;
    109 }
    110 
    111 static void BM_ManyConsts_Parallel(int iters, int num) {
    112   testing::ItemsProcessed(static_cast<int64>(iters) * num);
    113   test::Benchmark("cpu", ManyConsts(num, false /* !sequential */)).Run(iters);
    114 }
    115 BENCHMARK(BM_ManyConsts_Parallel)->Range(1, 1 << 10);
    116 
    117 static void BM_ManyConsts_Sequential(int iters, int num) {
    118   testing::ItemsProcessed(static_cast<int64>(iters) * num);
    119   test::Benchmark("cpu", ManyConsts(num, true /* sequential */)).Run(iters);
    120 }
    121 BENCHMARK(BM_ManyConsts_Sequential)->Range(1, 1 << 10);
    122 
    123 }  // end namespace tensorflow
    124