xref: /aosp_15_r20/external/tensorflow/tensorflow/core/common_runtime/eager/custom_device_op_handler.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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_COMMON_RUNTIME_EAGER_CUSTOM_DEVICE_OP_HANDLER_H_
16 #define TENSORFLOW_CORE_COMMON_RUNTIME_EAGER_CUSTOM_DEVICE_OP_HANDLER_H_
17 
18 #include "tensorflow/c/eager/immediate_execution_operation.h"
19 #include "tensorflow/c/eager/immediate_execution_tensor_handle.h"
20 #include "tensorflow/core/common_runtime/eager/custom_device.h"
21 #include "tensorflow/core/lib/core/status.h"
22 namespace tensorflow {
23 
24 // TODO(tfrt-devs): Figure out a way to unify it with OpHandler in TFRT.
25 class CustomDeviceOpHandler {
26  public:
~CustomDeviceOpHandler()27   ~CustomDeviceOpHandler() {}
28   // Register a new custom device.
29   Status RegisterCustomDevice(const string& device_name,
30                               std::unique_ptr<CustomDevice> device);
31 
32   // Find the custom device from given name. Return true if it finds one.
33   bool FindCustomDeviceFromName(const string& name,
34                                 CustomDevice** device) const;
35 
36   Status Execute(ImmediateExecutionOperation* op,
37                  ImmediateExecutionTensorHandle** retvals, int* num_retvals);
38 
39   ImmediateExecutionTensorHandle* CopyTensorHandleToDevice(
40       ImmediateExecutionContext* context,
41       ImmediateExecutionTensorHandle* handle, const char* device_name,
42       Status* status);
43 
44   // Determine whether to place an op on a custom device. This method is
45   // exposed as public for test only.
46   Status MaybePinToCustomDevice(CustomDevice** device,
47                                 const ImmediateExecutionOperation& op) const;
48 
49   void Clear();
50 
51  private:
52   std::unordered_map<string, std::unique_ptr<CustomDevice>> custom_devices_;
53 };
54 }  // namespace tensorflow
55 
56 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_EAGER_CUSTOM_DEVICE_OP_HANDLER_H_
57