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_CORE_RUNTIME_FALLBACK_KERNEL_KERNEL_FALLBACK_EXECUTE_COMPAT_H_
16 #define TENSORFLOW_CORE_RUNTIME_FALLBACK_KERNEL_KERNEL_FALLBACK_EXECUTE_COMPAT_H_
17 
18 #include <optional>
19 #include <string>
20 
21 #include "tensorflow/core/common_runtime/eager/context.h"
22 #include "tensorflow/core/platform/status.h"
23 #include "tensorflow/core/platform/threadpool_interface.h"
24 #include "tensorflow/core/platform/types.h"
25 #include "tensorflow/core/runtime_fallback/kernel/kernel_fallback_compat_request_state.h"
26 #include "tensorflow/core/tfrt/fallback/op_kernel_runner.h"
27 #include "tfrt/core_runtime/op_attrs.h"  // from @tf_runtime
28 #include "tfrt/host_context/async_value_ref.h"  // from @tf_runtime
29 #include "tfrt/host_context/chain.h"  // from @tf_runtime
30 #include "tfrt/host_context/execution_context.h"  // from @tf_runtime
31 #include "tfrt/host_context/kernel_utils.h"  // from @tf_runtime
32 #include "tfrt/support/forward_decls.h"  // from @tf_runtime
33 #include "tfrt/tensor/tensor.h"  // from @tf_runtime
34 
35 namespace tfrt {
36 class SyncKernelFrame;
37 }  // namespace tfrt
38 
39 namespace tensorflow {
40 namespace tfd {
41 
42 ABSL_CONST_INIT extern const char kOpKernelRunnerCacheResourceName[];
43 
44 // Set up fallback context with common tensorflow states such as devices,
45 // function library runtime. They will be forwarded to tensorflow::OpKernel as
46 // in tensorflow::Executor. If `runner` is nullptr, internally it will use a
47 // default runner that executes tasks in the caller thread.
48 Status SetUpKernelFallbackCompatRequestContext(
49     tfrt::RequestContextBuilder* builder,
50     const tensorflow::DeviceMgr* device_manager,
51     const tensorflow::ProcessFunctionLibraryRuntime* pflr,
52     tensorflow::thread::ThreadPoolInterface* user_intra_op_threadpool = nullptr,
53     const absl::optional<SessionMetadata>& model_metadata = absl::nullopt,
54     std::function<void(std::function<void()>)>* runner = nullptr);
55 
56 // Runner_table can be nullptr. In that case, kernel_fallback will use
57 // the default runner_table.
58 Status SetUpKernelFallbackCompatRequestContext(
59     tfrt::RequestContextBuilder* builder,
60     tfrt_stub::OpKernelRunnerTable* runner_table,
61     tensorflow::EagerContext* eager_context,
62     tensorflow::thread::ThreadPoolInterface* user_intra_op_threadpool = nullptr,
63     const absl::optional<SessionMetadata>& model_metadata = absl::nullopt);
64 
65 // The CoreRuntime dispatch function to run a TF kernel in kernel fallback
66 // compat mode.
67 tfrt::AsyncValueRef<tfrt::Chain> KernelFallbackExecuteCompatCoreRuntimeDispatch(
68     const tfrt::ExecutionContext& exec_ctx, tfrt::string_view op_name,
69     tfrt::string_view device_name, llvm::ArrayRef<tfrt::Tensor*> arguments,
70     llvm::MutableArrayRef<tfrt::RCReference<tfrt::AsyncValue>> results,
71     const KernelFallbackCompatRequestState& fallback_request_state,
72     const tfrt_stub::OpKernelRunner& op_kernel_runner);
73 
74 }  // namespace tfd
75 }  // namespace tensorflow
76 
77 #endif  // TENSORFLOW_CORE_RUNTIME_FALLBACK_KERNEL_KERNEL_FALLBACK_EXECUTE_COMPAT_H_
78