• 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 #ifndef TENSORFLOW_COMPILER_MLIR_TFR_INTEGRATION_TFR_DECOMPOSE_CTX_H_
16 #define TENSORFLOW_COMPILER_MLIR_TFR_INTEGRATION_TFR_DECOMPOSE_CTX_H_
17 
18 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
19 #include "mlir/IR/BuiltinOps.h"  // from @llvm-project
20 #include "mlir/IR/MLIRContext.h"  // from @llvm-project
21 #include "mlir/Pass/PassManager.h"  // from @llvm-project
22 #include "tensorflow/compiler/mlir/tensorflow/translate/mlir_roundtrip_flags.h"
23 #include "tensorflow/core/framework/function.pb.h"
24 #include "tensorflow/stream_executor/lib/statusor.h"
25 
26 namespace tensorflow {
27 namespace tfr {
28 
29 extern const char* const kTFRLibEnv;
30 
31 using stream_executor::port::StatusOr;
32 
33 // An wrapper for all the objects used to decompose a module (graph mode) and
34 // node_def (eager mode). Note that this class owns the decomposition library.
35 class TFRDecomposeContext {
36  public:
37   // The entry function to get a decompose context. All the required passes have
38   // been initialized.
39   static StatusOr<std::unique_ptr<TFRDecomposeContext>> Get(
40       mlir::MLIRContext* mlir_ctx);
41 
42   // Constructor of the decompose context. To share the decompose library, the
43   // whole decompose TFR function library is loaded.
44   explicit TFRDecomposeContext(mlir::ModuleOp tfr_module);
45 
46   // Constructs the decompose context from the tfr text module and the mlir
47   // context. The tfr text module is added to the mlir context.
48   static std::unique_ptr<TFRDecomposeContext> GetFromText(
49       StringPiece tfr_raw_text, mlir::MLIRContext* mlir_ctx);
50 
51   // Decomposes the op in the NodeDef to a set of primitive ops according to the
52   // decompose library in the context. Wrap the decomposed result in a
53   // FunctionDef.
54   StatusOr<FunctionDef> ExpandNode(const NodeDef& node_def,
55                                    StringPiece func_name);
56 
57   // Runs the decompose passes on the user_module.
58   Status DecomposeGraph(mlir::ModuleOp user_module);
59 
60   // Erases the tfr_module created.
61   void Destroy();
62 
63  private:
64   mlir::ModuleOp tfr_module_;
65   mlir::PassManager pm_;
66 
67   GraphExportConfig export_confs_;
68 };
69 
70 // Decomposes the NodeDef to a set of primitive ops according to the decompose
71 // library loaded. Wrap the decomposed result in a FunctionDef.
72 StatusOr<FunctionDef> ExpandNode(const NodeDef& node_def,
73                                  StringPiece func_name);
74 
75 // Decomposes the ops in the ModuleOp to a set of primitive ops according to
76 // decompose library in the context.
77 Status DecomposeGraph(mlir::ModuleOp user_module);
78 
79 }  // namespace tfr
80 }  // namespace tensorflow
81 
82 #endif  // TENSORFLOW_COMPILER_MLIR_TFR_INTEGRATION_TFR_DECOMPOSE_CTX_H_
83