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/common/tasks/prelu.h"
17
18 #include "absl/strings/str_cat.h"
19 #include "absl/types/variant.h"
20 #include "tensorflow/lite/delegates/gpu/common/task/storage_type_util.h"
21 #include "tensorflow/lite/delegates/gpu/common/tensor.h"
22
23 namespace tflite {
24 namespace gpu {
25
CreatePReLU(const GpuInfo & gpu_info,const OperationDef & definition,const PReLUAttributes & attr)26 GPUOperation CreatePReLU(const GpuInfo& gpu_info,
27 const OperationDef& definition,
28 const PReLUAttributes& attr) {
29 GPUOperation result(definition);
30 result.elementwise_ = true;
31
32 std::string alpha_read;
33 auto alpha_linear =
34 absl::get_if<tflite::gpu::Tensor<Linear, DataType::FLOAT32>>(&attr.alpha);
35 if (alpha_linear) {
36 TensorLinearDescriptor desc;
37 desc.storage_type =
38 DeduceLinearStorageType(definition.GetPrimaryStorageType());
39 desc.element_type = definition.GetPrimaryDataType();
40 desc.UploadLinearData(*alpha_linear);
41 result.args_.AddObject(
42 "alpha", absl::make_unique<TensorLinearDescriptor>(std::move(desc)));
43 alpha_read = "FLT4 alpha_val = args.alpha.Read(S_COORD);\n";
44 }
45
46 auto alpha_hwc =
47 absl::get_if<tflite::gpu::Tensor<HWC, DataType::FLOAT32>>(&attr.alpha);
48 if (alpha_hwc) {
49 const BHWC shape =
50 BHWC(1, alpha_hwc->shape.h, alpha_hwc->shape.w, alpha_hwc->shape.c);
51 TensorStorageType storage_type = SelectBestStorageType(
52 gpu_info, shape, definition.GetPrimaryStorageType(),
53 definition.GetDataType(), Layout::HWC);
54 TensorDescriptor desc{definition.GetDataType(), storage_type, Layout::HWC};
55 desc.UploadData(*alpha_hwc);
56 result.args_.AddObject(
57 "alpha", absl::make_unique<TensorDescriptor>(std::move(desc)));
58 const std::string x_coord = shape.w == 1 ? "0" : "X_COORD";
59 const std::string y_coord = shape.h == 1 ? "0" : "Y_COORD";
60 const std::string s_coord = shape.c == 1 ? "0" : "S_COORD";
61 alpha_read = absl::StrCat("FLT4 alpha_val = args.alpha.Read(", x_coord,
62 ", ", y_coord, ", ", s_coord, ");\n");
63 if (shape.c == 1) {
64 alpha_read += " alpha_val.y = alpha_val.x;\n";
65 alpha_read += " alpha_val.z = alpha_val.x;\n";
66 alpha_read += " alpha_val.w = alpha_val.x;\n";
67 }
68 }
69
70 if (attr.clip != 0) {
71 if (definition.precision == CalculationsPrecision::F32) {
72 result.args_.AddFloat("clip", attr.clip);
73 } else {
74 result.args_.AddHalf("clip", half(attr.clip));
75 }
76 result.code_ = alpha_read +
77 "in_out_value = clamp(in_out_value, INIT_FLT4(0.0f), "
78 "INIT_FLT4(args.clip)) + "
79 "min(INIT_FLT4(0.0f), in_out_value) * alpha_val;";
80 } else {
81 result.code_ = alpha_read +
82 "in_out_value = max(INIT_FLT4(0.0f), in_out_value) + "
83 "min(INIT_FLT4(0.0f), "
84 "in_out_value) * alpha_val;";
85 }
86
87 return result;
88 }
89
90 } // namespace gpu
91 } // namespace tflite
92