xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/metal/gpu_object.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2020 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_LITE_DELEGATES_GPU_METAL_GPU_OBJECT_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_METAL_GPU_OBJECT_H_
18 
19 #import <Metal/Metal.h>
20 
21 #include <map>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include "tensorflow/lite/delegates/gpu/common/access_type.h"
27 #include "tensorflow/lite/delegates/gpu/common/data_type.h"
28 #include "tensorflow/lite/delegates/gpu/common/status.h"
29 #include "tensorflow/lite/delegates/gpu/common/task/gpu_object_desc.h"
30 
31 namespace tflite {
32 namespace gpu {
33 namespace metal {
34 
35 struct GPUResourcesWithValue {
36   GenericGPUResourcesWithValue generic;
37 
38   struct BufferParameter {
39     id<MTLBuffer> handle;
40     uint64_t offset;
41   };
42   std::vector<std::pair<std::string, BufferParameter>> buffers;
43   std::vector<std::pair<std::string, id<MTLTexture>>> images2d;
44   std::vector<std::pair<std::string, id<MTLTexture>>> image2d_arrays;
45   std::vector<std::pair<std::string, id<MTLTexture>>> images3d;
46   std::vector<std::pair<std::string, id<MTLTexture>>> image_buffers;
47 
AddFloatGPUResourcesWithValue48   void AddFloat(const std::string& name, float value) {
49     generic.AddFloat(name, value);
50   }
AddIntGPUResourcesWithValue51   void AddInt(const std::string& name, int value) {
52     generic.AddInt(name, value);
53   }
54 };
55 
56 class GPUObject {
57  public:
58   GPUObject() = default;
59   // Move only
60   GPUObject(GPUObject&& obj_desc) = default;
61   GPUObject& operator=(GPUObject&& obj_desc) = default;
62   GPUObject(const GPUObject&) = delete;
63   GPUObject& operator=(const GPUObject&) = delete;
64   virtual ~GPUObject() = default;
65   virtual absl::Status GetGPUResources(
66       const GPUObjectDescriptor* obj_ptr,
67       GPUResourcesWithValue* resources) const = 0;
68 };
69 
70 using GPUObjectPtr = std::unique_ptr<GPUObject>;
71 
72 }  // namespace metal
73 }  // namespace gpu
74 }  // namespace tflite
75 
76 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_METAL_GPU_OBJECT_H_
77