Home | History | Annotate | Download | only in graph
      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/graph/tensor_id.h"
     17 #include <vector>
     18 #include "tensorflow/core/lib/random/simple_philox.h"
     19 #include "tensorflow/core/platform/logging.h"
     20 #include "tensorflow/core/platform/test.h"
     21 #include "tensorflow/core/platform/test_benchmark.h"
     22 
     23 namespace tensorflow {
     24 namespace {
     25 
     26 string ParseHelper(const string& n) { return ParseTensorName(n).ToString(); }
     27 
     28 TEST(TensorIdTest, ParseTensorName) {
     29   EXPECT_EQ(ParseHelper("W1"), "W1:0");
     30   EXPECT_EQ(ParseHelper("weights:0"), "weights:0");
     31   EXPECT_EQ(ParseHelper("W1:1"), "W1:1");
     32   EXPECT_EQ(ParseHelper("W1:17"), "W1:17");
     33   EXPECT_EQ(ParseHelper("xyz1_17"), "xyz1_17:0");
     34   EXPECT_EQ(ParseHelper("^foo"), "^foo");
     35 }
     36 
     37 uint32 Skewed(random::SimplePhilox* rnd, int max_log) {
     38   const uint32 space = 1 << (rnd->Rand32() % (max_log + 1));
     39   return rnd->Rand32() % space;
     40 }
     41 
     42 void BM_ParseTensorName(int iters, int arg) {
     43   testing::StopTiming();
     44   random::PhiloxRandom philox(301, 17);
     45   random::SimplePhilox rnd(&philox);
     46   std::vector<string> names;
     47   for (int i = 0; i < 100; i++) {
     48     string name;
     49     switch (arg) {
     50       case 0: {  // Generate random names
     51         size_t len = Skewed(&rnd, 4);
     52         while (name.size() < len) {
     53           name += rnd.OneIn(4) ? '0' : 'a';
     54         }
     55         if (rnd.OneIn(3)) {
     56           strings::StrAppend(&name, ":", rnd.Uniform(12));
     57         }
     58         break;
     59       }
     60       case 1:
     61         name = "W1";
     62         break;
     63       case 2:
     64         name = "t0003";
     65         break;
     66       case 3:
     67         name = "weights";
     68         break;
     69       case 4:
     70         name = "weights:17";
     71         break;
     72       case 5:
     73         name = "^weights";
     74         break;
     75       default:
     76         LOG(FATAL) << "Unexpected arg";
     77         break;
     78     }
     79     names.push_back(name);
     80   }
     81   testing::StartTiming();
     82   TensorId id;
     83   int index = 0;
     84   int sum = 0;
     85   while (--iters > 0) {
     86     id = ParseTensorName(names[index++ % names.size()]);
     87     sum += id.second;
     88   }
     89   VLOG(2) << sum;  // Prevent compiler from eliminating loop body
     90 }
     91 BENCHMARK(BM_ParseTensorName)->Arg(0)->Arg(1)->Arg(2)->Arg(3)->Arg(4)->Arg(5);
     92 
     93 }  // namespace
     94 }  // namespace tensorflow
     95