• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LITE_DELEGATES_GPU_METAL_LINEAR_STORAGE_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_METAL_LINEAR_STORAGE_H_
18 
19 #import <Metal/Metal.h>
20 
21 #include <string>
22 #include <utility>
23 
24 #include "tensorflow/lite/delegates/gpu/common/data_type.h"
25 #include "tensorflow/lite/delegates/gpu/common/status.h"
26 #include "tensorflow/lite/delegates/gpu/common/task/tensor_desc.h"
27 #include "tensorflow/lite/delegates/gpu/common/task/tensor_linear_desc.h"
28 #include "tensorflow/lite/delegates/gpu/common/types.h"
29 #include "tensorflow/lite/delegates/gpu/metal/gpu_object.h"
30 
31 namespace tflite {
32 namespace gpu {
33 namespace metal {
34 
35 // Represent GPU 1D-array of FLT4(float4/half4) values
36 // Can use inside texture or buffer
37 class LinearStorage : public GPUObject {
38  public:
LinearStorage()39   LinearStorage() {}
~LinearStorage()40   ~LinearStorage() override { Release(); }
41 
42   // Move only
43   LinearStorage(LinearStorage&& storage);
44   LinearStorage& operator=(LinearStorage&& storage);
45   LinearStorage(const LinearStorage&) = delete;
46   LinearStorage& operator=(const LinearStorage&) = delete;
47 
48   absl::Status GetGPUResources(const GPUObjectDescriptor* obj_ptr,
49                                GPUResourcesWithValue* resources) const override;
50 
51   absl::Status CreateFromTensorLinearDescriptor(
52       const TensorLinearDescriptor& desc, id<MTLDevice> device);
53 
54  private:
55   void Release();
56 
57   id<MTLBuffer> buffer_ = nullptr;
58   id<MTLTexture> texture_ = nullptr;
59   int depth_;
60   LinearStorageType storage_type_;
61 };
62 
63 }  // namespace metal
64 }  // namespace gpu
65 }  // namespace tflite
66 
67 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_METAL_LINEAR_STORAGE_H_
68