• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tensorflow/lite/delegates/gpu/common/task/buffer_desc.h"
17 
18 #include <string>
19 
20 #include "absl/strings/str_cat.h"
21 #include "tensorflow/lite/delegates/gpu/common/data_type.h"
22 #include "tensorflow/lite/delegates/gpu/common/status.h"
23 #include "tensorflow/lite/delegates/gpu/common/task/util.h"
24 
25 namespace tflite {
26 namespace gpu {
27 
Release()28 void BufferDescriptor::Release() { data.clear(); }
29 
GetGPUResources() const30 GPUResources BufferDescriptor::GetGPUResources() const {
31   GPUResources resources;
32   GPUBufferDescriptor desc;
33   desc.data_type = element_type;
34   desc.access_type = access_type_;
35   desc.element_size = element_size;
36   desc.memory_type = memory_type;
37   desc.attributes = attributes;
38   resources.buffers.push_back({"buffer", desc});
39   return resources;
40 }
41 
PerformSelector(const GpuInfo & gpu_info,const std::string & selector,const std::vector<std::string> & args,const std::vector<std::string> & template_args,std::string * result) const42 absl::Status BufferDescriptor::PerformSelector(
43     const GpuInfo& gpu_info, const std::string& selector,
44     const std::vector<std::string>& args,
45     const std::vector<std::string>& template_args, std::string* result) const {
46   if (selector == "Read") {
47     return PerformReadSelector(args, result);
48   } else if (selector == "GetPtr") {
49     return PerformGetPtrSelector(args, template_args, result);
50   } else {
51     return absl::NotFoundError(absl::StrCat(
52         "BufferDescriptor don't have selector with name - ", selector));
53   }
54 }
55 
PerformReadSelector(const std::vector<std::string> & args,std::string * result) const56 absl::Status BufferDescriptor::PerformReadSelector(
57     const std::vector<std::string>& args, std::string* result) const {
58   if (args.size() != 1) {
59     return absl::NotFoundError(
60         absl::StrCat("BufferDescriptor Read require one argument, but ",
61                      args.size(), " was passed"));
62   }
63   *result = absl::StrCat("buffer[", args[0], "]");
64   return absl::OkStatus();
65 }
66 
PerformGetPtrSelector(const std::vector<std::string> & args,const std::vector<std::string> & template_args,std::string * result) const67 absl::Status BufferDescriptor::PerformGetPtrSelector(
68     const std::vector<std::string>& args,
69     const std::vector<std::string>& template_args, std::string* result) const {
70   if (args.size() > 1) {
71     return absl::NotFoundError(absl::StrCat(
72         "BufferDescriptor GetPtr require one or zero arguments, but ",
73         args.size(), " was passed"));
74   }
75   if (template_args.size() > 1) {
76     return absl::NotFoundError(
77         absl::StrCat("BufferDescriptor GetPtr require one or zero teemplate "
78                      "arguments, but ",
79                      template_args.size(), " was passed"));
80   }
81   std::string conversion;
82   if (template_args.size() == 1) {
83     const std::string type_name = ToCLDataType(element_type, element_size);
84     if (type_name != template_args[0]) {
85       conversion = absl::StrCat("(", MemoryTypeToCLType(memory_type), " ",
86                                 template_args[0], "*)&");
87     }
88   }
89   if (args.empty()) {
90     *result = absl::StrCat(conversion, "buffer");
91   } else if (conversion.empty()) {
92     *result = absl::StrCat("(buffer + ", args[0], ")");
93   } else {
94     *result = absl::StrCat(conversion, "buffer[", args[0], "]");
95   }
96   return absl::OkStatus();
97 }
98 
99 }  // namespace gpu
100 }  // namespace tflite
101