• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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 #include "tensorflow/compiler/mlir/tfr/integration/graph_decompose_pass.h"
16 
17 #include "mlir/IR/MLIRContext.h"  // from @llvm-project
18 #include "tensorflow/compiler/mlir/mlir_graph_optimization_pass.h"
19 #include "tensorflow/compiler/mlir/tfr/integration/tfr_decompose_ctx.h"
20 #include "tensorflow/core/lib/monitoring/counter.h"
21 #include "tensorflow/stream_executor/lib/statusor.h"
22 
23 namespace tensorflow {
24 namespace {
25 
26 auto* tf_core_op_expansion_graph_counter =
27     monitoring::Counter<0>::New("/tensorflow/core/op_expansion/graph_counter",
28                                 "The number of graphs being op expanded.");
29 }  // namespace
30 
31 namespace tfr {
32 
GetPassState(const DeviceSet * device_set,const ConfigProto & config_proto,const Graph & graph,const FunctionLibraryDefinition & function_library) const33 MlirOptimizationPassState GraphDecomposePass::GetPassState(
34     const DeviceSet* device_set, const ConfigProto& config_proto,
35     const Graph& graph,
36     const FunctionLibraryDefinition& function_library) const {
37   const char* tfr_lib_env_val = getenv(std::string(kTFRLibEnv).c_str());
38   return tfr_lib_env_val != nullptr ? MlirOptimizationPassState::Enabled
39                                     : MlirOptimizationPassState::Disabled;
40 }
41 
Run(const ConfigProto & config_proto,mlir::ModuleOp module,const Graph & graph,const FunctionLibraryDefinition & function_library)42 Status GraphDecomposePass::Run(
43     const ConfigProto& config_proto, mlir::ModuleOp module, const Graph& graph,
44     const FunctionLibraryDefinition& function_library) {
45   if (GetPassState(/*device_set=*/nullptr, config_proto, graph,
46                    function_library) == MlirOptimizationPassState::Disabled) {
47     LOG_FIRST_N(INFO, 1) << "Skipping Graph Decomposition Pass, decomposition"
48                             " library was not found";
49     return OkStatus();
50   }
51 
52   tf_core_op_expansion_graph_counter->GetCell()->IncrementBy(1);
53 
54   LOG_FIRST_N(INFO, 1) << "Run Graph Decomposition Passes";
55 
56   TF_RETURN_IF_ERROR(DecomposeGraph(module));
57 
58   LOG_FIRST_N(INFO, 1) << "Finish Graph Decomposition Passes";
59 
60   return OkStatus();
61 }
62 
63 namespace {
64 constexpr int kMlirGraphDecomposePassPriority = -1;
65 
66 static mlir_pass_registration::MlirOptimizationPassRegistration
67     register_mlir_graph_decompose_pass(kMlirGraphDecomposePassPriority,
68                                        std::make_unique<GraphDecomposePass>());
69 }  // namespace
70 
71 }  // namespace tfr
72 }  // namespace tensorflow
73