Home | History | Annotate | Download | only in kernels
      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 
     16 #include "tensorflow/core/framework/op.h"
     17 #include "tensorflow/core/framework/tensor.h"
     18 #include "tensorflow/core/graph/graph.h"
     19 #include "tensorflow/core/graph/node_builder.h"
     20 #include "tensorflow/core/lib/core/status.h"
     21 #include "tensorflow/core/platform/test.h"
     22 #include "tensorflow/core/platform/test_benchmark.h"
     23 #include "tensorflow/core/public/session.h"
     24 
     25 namespace tensorflow {
     26 namespace {
     27 
     28 // Benchmark to simulate the overhead in training and serving workloads from too
     29 // many threads grabbing the ResourceMgr lock at the same time because of the
     30 // variable and queue ops.
     31 void ManyManyVariablesHelper(int threads, int variables, int iters) {
     32   testing::StopTiming();
     33   Graph g(OpRegistry::Global());
     34   std::vector<string> targets;
     35   for (int i = 0; i < variables; ++i) {
     36     Node* v;
     37     TF_CHECK_OK(
     38         NodeBuilder(
     39             g.NewName("VeryVeryLongRealistSoundingVariableName/weights"),
     40             "VariableV2")
     41             .Attr("shape", TensorShape())
     42             .Attr("dtype", DT_FLOAT)
     43             .Finalize(&g, &v));
     44     targets.push_back(v->name());
     45   }
     46   GraphDef gd;
     47   g.ToGraphDef(&gd);
     48   SessionOptions opts;
     49   opts.config.set_inter_op_parallelism_threads(threads);
     50   Session* sess = NewSession(opts);
     51   TF_CHECK_OK(sess->Create(gd));
     52   TF_CHECK_OK(sess->Run({}, {}, targets, nullptr));
     53   testing::StartTiming();
     54   for (int i = 0; i < iters; ++i) {
     55     TF_CHECK_OK(sess->Run({}, {}, targets, nullptr));
     56   }
     57   testing::StopTiming();
     58   delete sess;
     59 }
     60 
     61 void BM_ManyManyVariablesManyThreads(int iters, int threads) {
     62   ManyManyVariablesHelper(threads, 1000, iters);
     63 }
     64 
     65 BENCHMARK(BM_ManyManyVariablesManyThreads)->Arg(50);
     66 
     67 }  // namespace
     68 }  // namespace tensorflow
     69