• 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/tensor_linear_desc.h"
17 
18 #include "absl/strings/str_cat.h"
19 #include "tensorflow/lite/delegates/gpu/common/data_type.h"
20 #include "tensorflow/lite/delegates/gpu/common/status.h"
21 #include "tensorflow/lite/delegates/gpu/common/types.h"
22 #include "tensorflow/lite/delegates/gpu/common/util.h"
23 
24 namespace tflite {
25 namespace gpu {
26 
Release()27 void TensorLinearDescriptor::Release() { data.clear(); }
28 
GetGPUResources() const29 GPUResources TensorLinearDescriptor::GetGPUResources() const {
30   GPUResources resources;
31   resources.ints.push_back("length");
32   if (storage_type == LinearStorageType::BUFFER) {
33     GPUBufferDescriptor desc;
34     desc.data_type = element_type;
35     desc.access_type = access_type_;
36     desc.element_size = 4;
37     desc.memory_type = memory_type;
38     resources.buffers.push_back({"buffer", desc});
39   } else {
40     GPUImage2DDescriptor desc;
41     desc.data_type = element_type;
42     desc.access_type = access_type_;
43     resources.images2d.push_back({"tex2d", desc});
44   }
45   return resources;
46 }
47 
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) const48 absl::Status TensorLinearDescriptor::PerformSelector(
49     const GpuInfo& gpu_info, const std::string& selector,
50     const std::vector<std::string>& args,
51     const std::vector<std::string>& template_args, std::string* result) const {
52   if (selector == "Length") {
53     *result = "length";
54     return absl::OkStatus();
55   } else if (selector == "Read") {
56     return PerformReadSelector(gpu_info, args, result);
57   } else if (selector == "GetPtr") {
58     if (storage_type != LinearStorageType::BUFFER) {
59       return absl::InvalidArgumentError(
60           "GetPtr selector supported for LinearStorageType::BUFFER only.");
61     }
62     *result = "buffer";
63     return absl::OkStatus();
64   } else {
65     return absl::NotFoundError(absl::StrCat(
66         "TensorLinearDescriptor don't have selector with name - ", selector));
67   }
68 }
69 
PerformReadSelector(const GpuInfo & gpu_info,const std::vector<std::string> & args,std::string * result) const70 absl::Status TensorLinearDescriptor::PerformReadSelector(
71     const GpuInfo& gpu_info, const std::vector<std::string>& args,
72     std::string* result) const {
73   if (args.size() != 1) {
74     return absl::NotFoundError(
75         absl::StrCat("TensorLinearDescriptor Read require one argument, but ",
76                      args.size(), " was passed"));
77   }
78   if (storage_type == LinearStorageType::BUFFER) {
79     *result = absl::StrCat("buffer[", args[0], "]");
80     return absl::OkStatus();
81   } else {
82     if (gpu_info.IsApiMetal()) {
83       *result = absl::StrCat("tex2d.read(ushort2(", args[0], ", 0))");
84       return absl::OkStatus();
85     } else if (gpu_info.IsApiOpenCl()) {
86       const std::string read =
87           element_type == DataType::FLOAT16 ? "read_imageh" : "read_imagef";
88       *result =
89           absl::StrCat(read, "(tex2d, smp_none, (int2)(", args[0], ", 0))");
90       return absl::OkStatus();
91     } else {
92       return absl::UnimplementedError(
93           "No implementation of TensorLinear.Read for this API.");
94     }
95   }
96 }
97 
UploadLinearData(const tflite::gpu::Tensor<Linear,DataType::FLOAT32> & src,int aligned_size)98 void TensorLinearDescriptor::UploadLinearData(
99     const tflite::gpu::Tensor<Linear, DataType::FLOAT32>& src,
100     int aligned_size) {
101   size = aligned_size == 0 ? DivideRoundUp(src.shape.v, 4) : aligned_size;
102   if (element_type == DataType::FLOAT32) {
103     data.resize(size * sizeof(float) * 4);
104     float* gpu_data = reinterpret_cast<float*>(data.data());
105     for (int i = 0; i < size * 4; ++i) {
106       if (i < src.shape.v) {
107         gpu_data[i] = src.data[i];
108       } else {
109         gpu_data[i] = 0.0f;
110       }
111     }
112   } else {
113     data.resize(size * sizeof(half) * 4);
114     half* gpu_data = reinterpret_cast<half*>(data.data());
115     for (int i = 0; i < size * 4; ++i) {
116       if (i < src.shape.v) {
117         gpu_data[i] = src.data[i];
118       } else {
119         gpu_data[i] = 0.0f;
120       }
121     }
122   }
123 }
124 
125 }  // namespace gpu
126 }  // namespace tflite
127