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/utils/model_metadata.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 // `builder`, `eager_context`, and `pflr` can't be null. 43 Status SetUpKernelFallbackCompatRequestContext( 44 tfrt::RequestContextBuilder* builder, 45 const tensorflow::DeviceMgr* device_manager, 46 const tensorflow::ProcessFunctionLibraryRuntime* pflr, 47 tensorflow::thread::ThreadPoolInterface* user_intra_op_threadpool = nullptr, 48 const absl::optional<tfrt::ModelMetadata>& model_metadata = absl::nullopt); 49 50 // Runner_table can be nullptr. In that case, kernel_fallback will use 51 // the default runner_table. 52 Status SetUpKernelFallbackCompatRequestContext( 53 tfrt::RequestContextBuilder* builder, OpKernelRunnerTable* runner_table, 54 tensorflow::EagerContext* eager_context, 55 tensorflow::thread::ThreadPoolInterface* user_intra_op_threadpool = nullptr, 56 const absl::optional<tfrt::ModelMetadata>& model_metadata = absl::nullopt); 57 58 // The CoreRuntime dispatch function to run a TF kernel in kernel fallback 59 // compat mode. 60 tfrt::AsyncValueRef<tfrt::Chain> KernelFallbackExecuteCompatCoreRuntimeDispatch( 61 const tfrt::ExecutionContext& exec_ctx, tfrt::string_view op_name, 62 tfrt::string_view device_name, llvm::ArrayRef<tfrt::Tensor*> arguments, 63 llvm::MutableArrayRef<tfrt::RCReference<tfrt::AsyncValue>> results, 64 const tfrt::OpAttrsRef& attrs); 65 66 // `frame` is used to consume the inputs and hold the outputs from kernel 67 // execution. 68 // 69 // TODO(tfrt-devs): switch `attrs` to using tfrt::AggregateAttr after 70 // cl/343983780. 71 Status KernelFallbackSyncExecuteCompat(const tfrt::ExecutionContext& exec_ctx, 72 absl::string_view op_name, 73 absl::string_view device_name, 74 tfrt::SyncKernelFrame* frame, 75 const tfrt::OpAttrsRef& attrs); 76 77 // TODO(tfrt-devs): Consider moving following method to a separate file. 78 llvm::Expected<Device*> GetTfDevice(const tfrt::ExecutionContext& exec_ctx, 79 const tfrt::Device& device); 80 81 } // namespace tfd 82 } // namespace tensorflow 83 84 #endif // TENSORFLOW_CORE_RUNTIME_FALLBACK_KERNEL_KERNEL_FALLBACK_EXECUTE_COMPAT_H_ 85