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_TFRT_EAGER_CORE_RUNTIME_PLACER_H_ 16 #define TENSORFLOW_CORE_TFRT_EAGER_CORE_RUNTIME_PLACER_H_ 17 18 #include "tensorflow/core/platform/status.h" 19 #include "tfrt/support/forward_decls.h" // from @tf_runtime 20 21 namespace tensorflow { 22 23 class ImmediateExecutionOperation; 24 class EagerContext; 25 class AttrBuilder; 26 class NodeDef; 27 } // namespace tensorflow 28 29 namespace tfrt { 30 class OpHandler; 31 class CoreRuntime; 32 class Device; 33 34 namespace tf { 35 36 using ::tensorflow::EagerContext; 37 using ::tensorflow::ImmediateExecutionOperation; 38 using ::tensorflow::NodeDef; 39 using ::tensorflow::Status; 40 41 // A helper class to select op handler in op-by-op execution. 42 class EagerOpHandlerSelector final { 43 public: 44 EagerOpHandlerSelector(CoreRuntime* core_runtime, EagerContext* eager_context, 45 OpHandler* fallback_op_handler, 46 bool pin_small_ops_to_cpu); 47 ~EagerOpHandlerSelector(); 48 49 // Selects the op handler to execute the op based on the arguments. This 50 // op handler selection is cheap. But it can be nullptr even it return OK 51 // status. 52 Status SelectFromArguments(const ImmediateExecutionOperation& op, 53 OpHandler** op_handler); 54 55 // Selects the op handler to execute the op based on NodeDef. This op handler 56 // selection is expensive. It will never return nullptr unless there is an 57 // error. Please only invoke this method when the cheap version fails. 58 Status SelectFromNodeDef(const ImmediateExecutionOperation& op, 59 const NodeDef* ndef, OpHandler** op_handler); 60 61 private: 62 CoreRuntime* core_runtime_; 63 EagerContext* eager_context_; 64 65 const Device& cpu_device_; 66 OpHandler* cpu_op_handler_; 67 OpHandler* fallback_op_handler_; 68 bool pin_small_ops_to_cpu_; 69 }; 70 71 } // namespace tf 72 } // namespace tfrt 73 74 #endif // TENSORFLOW_CORE_TFRT_EAGER_CORE_RUNTIME_PLACER_H_ 75