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 16 #ifndef TENSORFLOW_COMPILER_MLIR_TFRT_FUNCTION_FUNCTION_H_ 17 #define TENSORFLOW_COMPILER_MLIR_TFRT_FUNCTION_FUNCTION_H_ 18 19 #include <string> 20 #include <unordered_set> 21 #include <vector> 22 23 #include "absl/container/flat_hash_map.h" 24 #include "absl/strings/string_view.h" 25 #include "mlir/IR/BuiltinTypes.h" // from @llvm-project 26 #include "tensorflow/compiler/mlir/tfrt/translate/tfrt_compile_options.h" 27 #include "tensorflow/core/platform/status.h" 28 #include "tfrt/bef/bef_buffer.h" // from @tf_runtime 29 #include "tfrt/core_runtime/tensor_handle.h" // from @tf_runtime 30 31 namespace tfrt { 32 class CoreRuntime; 33 } 34 35 namespace mlir { 36 class ModuleOp; 37 } 38 39 namespace tensorflow { 40 41 struct TfrtFunctionCompileOptions : public TfrtCompileOptions { 42 // Currently only SavedModel API inference uses the tpu_fuse_ops option TfrtFunctionCompileOptionsTfrtFunctionCompileOptions43 TfrtFunctionCompileOptions() { 44 tpu_fuse_ops = false; 45 // TF function in eager execution uses CoreRT native ops as fallback states 46 // are not initialized in that code path. 47 enable_native_ops = true; 48 // Currently grappler is not correctly applied in the eager execution of TF 49 // functions, as it may sometimes remove arguments and results. 50 enable_grappler = false; 51 } 52 53 // If true, use ServingCoreSelector to pick TPU core. Otherwise, obtain core 54 // location from assigned device name. 55 // Currently we don't use core_selector for training use cases. 56 bool tpu_use_core_selector = false; 57 58 // If true, use BundledTransferToTpuOp to transfer variables and input tensors 59 // to TPU. 60 bool tpu_use_bundled_transfer = false; 61 62 // If true, lower an TF op that's placed on TPU device to be executed with 63 // tfrt_fallback.execute. 64 // Currently for training use cases we need to lower the op to corert.execute 65 // to execute with TPU OpHandler, and with TFRT's native implementation. 66 // TODO(b/188940204): remove this config after we clear up the TPU variable 67 // implementation. 68 bool tpu_lower_to_fallback = false; 69 // If true, transfer the result of TPUExecuteOp from TPU to host. 70 // Currently for training and Python bulk inference use cases, we don't need 71 // to proactively transfer the result to host since the consumer op (or 72 // function) of the result may still be on TPU. 73 // TODO(b/194081364): remove this option once we unify servo TPU serving 74 // result transfer behavior. 75 bool tpu_transfer_result_to_host = false; 76 }; 77 78 // Compile MLIR generated by tf.function in TF dialect into BEF. 79 Status CompileTFMLIRToBEF(const TfrtFunctionCompileOptions& options, 80 mlir::ModuleOp module, tfrt::BefBuffer* bef_buffer); 81 82 } // namespace tensorflow 83 84 #endif // TENSORFLOW_COMPILER_MLIR_TFRT_FUNCTION_FUNCTION_H_ 85