xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/jit/xla_kernel_creator.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2017 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 #include "tensorflow/compiler/jit/xla_kernel_creator.h"
16 
17 #include "absl/memory/memory.h"
18 #include "absl/strings/str_cat.h"
19 #include "absl/strings/str_format.h"
20 #include "tensorflow/compiler/jit/compilability_check_util.h"
21 #include "tensorflow/compiler/jit/defs.h"
22 #include "tensorflow/compiler/jit/flags.h"
23 #include "tensorflow/compiler/jit/kernels/xla_ops.h"
24 #include "tensorflow/compiler/tf2xla/const_analysis.h"
25 #include "tensorflow/compiler/tf2xla/mlir_bridge_pass.h"
26 #include "tensorflow/compiler/tf2xla/xla_op_registry.h"
27 #include "tensorflow/core/common_runtime/function.h"
28 #include "tensorflow/core/framework/node_def_builder.h"
29 #include "tensorflow/core/framework/node_def_util.h"
30 #include "tensorflow/core/lib/core/status.h"
31 #include "tensorflow/core/util/ptr_util.h"
32 
33 namespace tensorflow {
34 
CanCreateKernel(const FunctionLibraryRuntime & flr,const std::shared_ptr<const NodeProperties> & props) const35 bool XlaKernelCreator::CanCreateKernel(
36     const FunctionLibraryRuntime& flr,
37     const std::shared_ptr<const NodeProperties>& props) const {
38   return CanCreateXlaKernel(props->node_def) &&
39          !XlaOpRegistry::IsCompilationDevice(flr.device()->device_type());
40 }
41 
CreateXlaKernel(FunctionLibraryRuntime * flr,const NodeDef & node_def,std::unique_ptr<OpKernel> * kernel)42 static Status CreateXlaKernel(FunctionLibraryRuntime* flr,
43                               const NodeDef& node_def,
44                               std::unique_ptr<OpKernel>* kernel) {
45   if (!CanCreateXlaKernel(node_def)) {
46     return errors::Internal("Invalid node: ", node_def.ShortDebugString());
47   }
48 
49   VLOG(3) << "Attempting to create XlaLaunchOp for " << node_def.DebugString();
50 
51   // Make sure that kernels have been registered on the JIT device.
52   XlaOpRegistry::RegisterCompilationKernels();
53 
54   // Get function body, constant args, and resource args.
55   NameAttrList function;
56   TF_RETURN_IF_ERROR(NameAndAttrsFromFunctionCall(node_def, &function));
57   const FunctionBody* fbody = nullptr;
58   std::vector<int> constant_arg_indices;
59   std::vector<int> resource_arg_indices;
60   TF_RETURN_IF_ERROR(GetBodyAndConstantsAndResources(
61       flr, function, &fbody, &constant_arg_indices, &resource_arg_indices));
62 
63   MemoryTypeVector input_memory_types =
64       GetInputMemoryTypes(fbody, constant_arg_indices, resource_arg_indices);
65   MemoryTypeVector output_memory_types = GetOutputMemoryTypes(fbody);
66 
67   // Create the kernel.
68   Device* dev = flr->device();
69   Status s;
70   auto props = std::make_shared<NodeProperties>(
71       &fbody->fdef.signature(), node_def, fbody->arg_types, fbody->ret_types);
72   OpKernelConstruction construction(DeviceType(dev->device_type()), dev,
73                                     dev->GetAllocator(AllocatorAttributes()),
74                                     flr, dev->resource_manager(), props,
75                                     input_memory_types, output_memory_types,
76                                     flr->graph_def_version(), &s);
77 
78   *kernel = std::make_unique<XlaLocalLaunchBase>(
79       &construction, constant_arg_indices, resource_arg_indices, function,
80       /*has_ref_vars=*/false);
81   return s;
82 }
83 
CreateKernel(FunctionLibraryRuntime * flr,const std::shared_ptr<const NodeProperties> & props,std::unique_ptr<OpKernel> * kernel) const84 Status XlaKernelCreator::CreateKernel(
85     FunctionLibraryRuntime* flr,
86     const std::shared_ptr<const NodeProperties>& props,
87     std::unique_ptr<OpKernel>* kernel) const {
88   return CreateXlaKernel(flr, props->node_def, kernel);
89 }
90 
RegisterLaunchOpCreator()91 static bool RegisterLaunchOpCreator() {
92   XlaKernelCreator* xla_kernel_creator = new XlaKernelCreator();
93   RegisterDefaultCustomKernelCreator(xla_kernel_creator);
94   return true;
95 }
96 
97 static bool register_me = RegisterLaunchOpCreator();
98 
99 }  // namespace tensorflow
100