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 #ifndef TENSORFLOW_CORE_COMMON_RUNTIME_KERNEL_BENCHMARK_TESTLIB_H_ 17 #define TENSORFLOW_CORE_COMMON_RUNTIME_KERNEL_BENCHMARK_TESTLIB_H_ 18 19 #include <string> 20 #include <vector> 21 22 #include "tensorflow/core/common_runtime/executor.h" 23 #include "tensorflow/core/framework/tensor.h" 24 #include "tensorflow/core/graph/testlib.h" 25 #include "tensorflow/core/lib/core/threadpool.h" 26 #include "tensorflow/core/platform/macros.h" 27 #include "tensorflow/core/platform/types.h" 28 29 namespace tensorflow { 30 31 class Device; 32 struct SessionOptions; 33 34 namespace test { 35 36 class Benchmark { 37 public: 38 // "device" must be either "cpu" or "gpu". Takes ownership of "g", 39 // "init", and one reference on "rendez" (if not null). 40 Benchmark(const string& device, Graph* g, 41 const SessionOptions* options = nullptr, Graph* init = nullptr, 42 Rendezvous* rendez = nullptr, const char* executor_type = ""); 43 ~Benchmark(); 44 45 // Executes the graph for "iters" times. 46 void Run(int iters); 47 48 // If "g" contains send/recv nodes, before each execution, we send 49 // inputs to the corresponding recv nodes in the graph, after each 50 // execution, we recv outputs from the corresponding send nodes in 51 // the graph. In the benchmark, we throw away values returned by the 52 // graph. 53 void RunWithArgs(const std::vector<std::pair<const Node*, Tensor>>& inputs, 54 const std::vector<const Node*>& outputs, int iters); 55 56 private: 57 thread::ThreadPool* pool_ = nullptr; 58 std::unique_ptr<Device> device_ = nullptr; 59 Rendezvous* rendez_ = nullptr; 60 std::unique_ptr<Executor> exec_; 61 62 TF_DISALLOW_COPY_AND_ASSIGN(Benchmark); 63 }; 64 65 } // end namespace test 66 } // end namespace tensorflow 67 68 #endif // TENSORFLOW_CORE_COMMON_RUNTIME_KERNEL_BENCHMARK_TESTLIB_H_ 69