xref: /aosp_15_r20/external/tensorflow/tensorflow/tools/optimization/optimization_pass_runner.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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() : jit_level_(OptimizerOptions::DEFAULT) {}
34 
35   // Increasing the Jit level will cause XLA to compile parts of the tensorflow
36   // graph that it is able to.
37   Status SetJitLevel(OptimizerOptions::GlobalJitLevel jit_level);
38 
39   Status Run(absl::string_view pass_to_run, GraphDef input, GraphDef* result);
40 
AddCpus(int count)41   Status AddCpus(int count) {
42     return AddDevices(tensorflow::DEVICE_CPU, count);
43   }
44 
AddGpus(int count)45   Status AddGpus(int count) {
46     return AddDevices(tensorflow::DEVICE_GPU, count);
47   }
48 
49  private:
50   Status AddDevices(absl::string_view type, int count);
51 
52   OptimizerOptions::GlobalJitLevel jit_level_;
53   std::vector<std::unique_ptr<Device>> devices_;
54 };
55 
56 }  // namespace tensorflow
57 
58 #endif  // TENSORFLOW_TOOLS_OPTIMIZATION_OPTIMIZATION_PASS_RUNNER_H_
59