xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/tensorflow/utils/session_utils.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 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_TENSORFLOW_UTILS_SESSION_UTILS_H_
16 #define TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_SESSION_UTILS_H_
17 
18 #include "absl/status/statusor.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
21 #include "mlir/IR/BuiltinOps.h"  // from @llvm-project
22 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
23 #include "tensorflow/core/public/session.h"
24 
25 namespace mlir {
26 namespace tf_saved_model {
27 
28 // Returns the variable for the provided 'var_handle_op'.
29 std::string GetVariableName(TF::VarHandleOp var_handle_op);
30 
31 // Returns pointer to the variable from 'session' that 'var_handle_op'
32 // refers to which is in 'device_name' device. If failed to fetch the value null
33 // will be returned.
34 // Note, caller is responsible for Unref the variable.
35 tensorflow::Var* GetVariableFromSession(mlir::TF::VarHandleOp var_handle_op,
36                                         llvm::StringRef device_name,
37                                         const tensorflow::DeviceMgr* mgr);
38 
39 // Returns resource tensors from session for all variables in 'module'.
40 absl::StatusOr<std::vector<tensorflow::Tensor>> GetResourcesFromSession(
41     llvm::ArrayRef<TF::VarHandleOp> var_handle_ops,
42     tensorflow::Session* session);
43 
44 }  // namespace tf_saved_model
45 }  // namespace mlir
46 
47 #endif  // TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_SESSION_UTILS_H_
48