• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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 #include "tensorflow/lite/delegates/gpu/gl/object_manager.h"
17 
18 #include "absl/memory/memory.h"
19 #include "absl/types/span.h"
20 #include "tensorflow/lite/delegates/gpu/common/convert.h"
21 #include "tensorflow/lite/delegates/gpu/common/status.h"
22 
23 namespace tflite {
24 namespace gpu {
25 namespace gl {
26 
CreatePHWC4BufferFromTensor(const TensorFloat32 & tensor,GlBuffer * gl_buffer)27 absl::Status CreatePHWC4BufferFromTensor(const TensorFloat32& tensor,
28                                          GlBuffer* gl_buffer) {
29   std::vector<float> transposed(GetElementsSizeForPHWC4(tensor.shape));
30   RETURN_IF_ERROR(
31       ConvertToPHWC4(tensor.data, tensor.shape, absl::MakeSpan(transposed)));
32   return CreateReadOnlyShaderStorageBuffer<float>(transposed, gl_buffer);
33 }
34 
CreatePHWC4BufferFromTensorRef(const TensorRef<BHWC> & tensor_ref,GlBuffer * gl_buffer)35 absl::Status CreatePHWC4BufferFromTensorRef(const TensorRef<BHWC>& tensor_ref,
36                                             GlBuffer* gl_buffer) {
37   return CreateReadWriteShaderStorageBuffer<float>(
38       GetElementsSizeForPHWC4(tensor_ref.shape), gl_buffer);
39 }
40 
CopyFromPHWC4Buffer(const GlBuffer & buffer,TensorFloat32 * tensor)41 absl::Status CopyFromPHWC4Buffer(const GlBuffer& buffer,
42                                  TensorFloat32* tensor) {
43   return buffer.MappedRead<float>([tensor](absl::Span<const float> data) {
44     tensor->data.resize(tensor->shape.DimensionsProduct());
45     return ConvertFromPHWC4(absl::MakeConstSpan(data), tensor->shape,
46                             absl::MakeSpan(tensor->data));
47   });
48 }
49 
RegisterBuffer(uint32_t id,GlBuffer buffer)50 absl::Status ObjectManager::RegisterBuffer(uint32_t id, GlBuffer buffer) {
51   if (id >= buffers_.size()) {
52     buffers_.resize(id + 1);
53   }
54   buffers_[id] = absl::make_unique<GlBuffer>(std::move(buffer));
55   return absl::OkStatus();
56 }
57 
RemoveBuffer(uint32_t id)58 void ObjectManager::RemoveBuffer(uint32_t id) {
59   if (id < buffers_.size()) {
60     buffers_[id].reset(nullptr);
61   }
62 }
63 
FindBuffer(uint32_t id) const64 GlBuffer* ObjectManager::FindBuffer(uint32_t id) const {
65   return id >= buffers_.size() ? nullptr : buffers_[id].get();
66 }
67 
RegisterTexture(uint32_t id,GlTexture texture)68 absl::Status ObjectManager::RegisterTexture(uint32_t id, GlTexture texture) {
69   if (id >= textures_.size()) {
70     textures_.resize(id + 1);
71   }
72   textures_[id] = absl::make_unique<GlTexture>(std::move(texture));
73   return absl::OkStatus();
74 }
75 
RemoveTexture(uint32_t id)76 void ObjectManager::RemoveTexture(uint32_t id) {
77   if (id < textures_.size()) {
78     textures_[id].reset(nullptr);
79   }
80 }
81 
FindTexture(uint32_t id) const82 GlTexture* ObjectManager::FindTexture(uint32_t id) const {
83   return id >= textures_.size() ? nullptr : textures_[id].get();
84 }
85 
stats() const86 ObjectsStats ObjectManager::stats() const {
87   ObjectsStats stats;
88   for (auto& texture : textures_) {
89     if (!texture || !texture->has_ownership()) continue;
90     stats.textures.count++;
91     stats.textures.total_bytes += texture->bytes_size();
92   }
93   for (auto& buffer : buffers_) {
94     if (!buffer || !buffer->has_ownership()) continue;
95     stats.buffers.count++;
96     stats.buffers.total_bytes += buffer->bytes_size();
97   }
98   return stats;
99 }
100 
101 }  // namespace gl
102 }  // namespace gpu
103 }  // namespace tflite
104