1 /* Copyright 2018 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 #ifndef TENSORFLOW_TOOLS_OPTIMIZATION_OPTIMIZATION_PASS_RUNNER_H_
16 #define TENSORFLOW_TOOLS_OPTIMIZATION_OPTIMIZATION_PASS_RUNNER_H_
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "tensorflow/core/common_runtime/device.h"
23 #include "tensorflow/core/common_runtime/optimization_registry.h"
24 #include "tensorflow/core/lib/core/status.h"
25 #include "tensorflow/core/protobuf/config.pb.h"
26 
27 namespace tensorflow {
28 
29 // OptimizationPassRunner can be initialized, populated with devices, then run
30 // to test individual Tensorflow Optimization passes.
31 class OptimizationPassRunner {
32  public:
OptimizationPassRunner()33   explicit OptimizationPassRunner()
34       : jit_level_(OptimizerOptions::GlobalJitLevel::
35                        OptimizerOptions_GlobalJitLevel_DEFAULT) {}
36 
37   // Add a fake device to the (initially empty) DeviceSet used for optimization.
38   // Names are of the form: "/job:localhost/replica:0/task:0/device:CPU:0"
39   Status AddDevice(const string& name, const string& type);
40 
41   // Increasing the Jit level will cause XLA to compile parts of the tensorflow
42   // graph that it is able to.
43   Status SetJitLevel(OptimizerOptions::GlobalJitLevel jit_level);
44 
45   // This can be called after adding devices and setting the jit level to parse
46   // command line flags and run the specified job. All 3 flags are required:
47   // input_file_path, output_file_path, optimization_pass.
48   //
49   // If this library becomes heavily used, the caller should be responsible for
50   // parsing any command line flags desired rather than this Method handling the
51   // work of a main() function.
52   Status RunMain(int argc, char** argv);
53 
54  private:
55   OptimizerOptions::GlobalJitLevel jit_level_;
56   std::vector<std::unique_ptr<Device>> devices_;
57 };
58 
59 }  // namespace tensorflow
60 
61 #endif  // TENSORFLOW_TOOLS_OPTIMIZATION_OPTIMIZATION_PASS_RUNNER_H_
62