• 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/common/tasks/lstm.h"
17 
18 #include <string>
19 
20 #include "tensorflow/lite/delegates/gpu/common/task/work_group_picking.h"
21 
22 namespace tflite {
23 namespace gpu {
24 
25 namespace {
GetLSTMCode(const OperationDef & op_def,const GpuInfo & gpu_info)26 std::string GetLSTMCode(const OperationDef& op_def, const GpuInfo& gpu_info) {
27   std::string c;
28   c += "MAIN_FUNCTION(\n";
29   c += "$0) {\n";
30   c += "  int B = GLOBAL_ID_0;\n";
31   c += "  int Z = GLOBAL_ID_2;\n";
32   c += "  if (Z >= args.activation.Slices() || B >= args.activation.Batch()) "
33        "return;\n";
34   c += "  FLT4 prev_st = args.prev_state.Read(0, 0, Z, B);\n";
35   c += "  FLT4 r0 = args.intermediate.Read(0, 0, Z, B);\n";
36   c += "  int state_stride = args.activation.Slices();\n";
37   c += "  FLT4 r1 = args.intermediate.Read(0, 0, Z + state_stride, B);\n";
38   c += "  FLT4 r2 = args.intermediate.Read(0, 0, Z + state_stride * 2, B);\n";
39   c += "  FLT4 r3 = args.intermediate.Read(0, 0, Z + state_stride * 3, B);\n";
40   if (gpu_info.IsApiOpenCl() &&
41       op_def.precision != CalculationsPrecision::F32 && gpu_info.IsAdreno()) {
42     c += "  FLT4 input_gate;\n";
43     c += "  FLT4 new_input;\n";
44     c += "  FLT4 forget_gate;\n";
45     c += "  FLT4 output_gate;\n";
46     c += "  input_gate.x = native_recip(1.0h + native_exp(-r0.x));\n";
47     c += "  input_gate.y = native_recip(1.0h + native_exp(-r0.y));\n";
48     c += "  input_gate.z = native_recip(1.0h + native_exp(-r0.z));\n";
49     c += "  input_gate.w = native_recip(1.0h + native_exp(-r0.w));\n";
50     c += "  new_input.x = 1.0h - 2.0h * native_recip(1.0h + native_exp(2.0h * "
51          "r1.x));\n";
52     c += "  new_input.y = 1.0h - 2.0h * native_recip(1.0h + native_exp(2.0h * "
53          "r1.y));\n";
54     c += "  new_input.z = 1.0h - 2.0h * native_recip(1.0h + native_exp(2.0h * "
55          "r1.z));\n";
56     c += "  new_input.w = 1.0h - 2.0h * native_recip(1.0h + native_exp(2.0h * "
57          "r1.w));\n";
58     c += "  forget_gate.x = native_recip(1.0h + native_exp(-r2.x));\n";
59     c += "  forget_gate.y = native_recip(1.0h + native_exp(-r2.y));\n";
60     c += "  forget_gate.z = native_recip(1.0h + native_exp(-r2.z));\n";
61     c += "  forget_gate.w = native_recip(1.0h + native_exp(-r2.w));\n";
62     c += "  output_gate.x = native_recip(1.0h + native_exp(-r3.x));\n";
63     c += "  output_gate.y = native_recip(1.0h + native_exp(-r3.y));\n";
64     c += "  output_gate.z = native_recip(1.0h + native_exp(-r3.z));\n";
65     c += "  output_gate.w = native_recip(1.0h + native_exp(-r3.w));\n";
66   } else {
67     c += "  FLT4 input_gate  = INIT_FLT4(1.0f) / (INIT_FLT4(1.0f) + "
68          "exp(INIT_FLT4(-1.0f) "
69          "* r0));\n";
70     c += "  FLT4 new_input   = tanh(r1);\n";
71     c += "  FLT4 forget_gate = INIT_FLT4(1.0f) / (INIT_FLT4(1.0f) + "
72          "exp(INIT_FLT4(-1.0f) "
73          "* r2));\n";
74     c += "  FLT4 output_gate = INIT_FLT4(1.0f) / (INIT_FLT4(1.0f) + "
75          "exp(INIT_FLT4(-1.0f) "
76          "* r3));\n";
77   }
78   c += "  FLT4 new_st = input_gate * new_input + forget_gate * prev_st;\n";
79   c += "  FLT4 act_value = output_gate * tanh(new_st);\n";
80   c += "  args.activation.Write(act_value, 0, 0, Z, B);\n";
81   c += "  args.new_state.Write(new_st, 0, 0, Z, B);\n";
82   c += "}\n";
83   return c;
84 }
85 
86 }  // namespace
87 
CreateLSTM(const OperationDef & definition,const GpuInfo & gpu_info)88 GPUOperation CreateLSTM(const OperationDef& definition,
89                         const GpuInfo& gpu_info) {
90   GPUOperation op(definition);
91   op.AddSrcTensor("intermediate", definition.src_tensors[0]);
92   op.AddSrcTensor("prev_state", definition.src_tensors[1]);
93   op.AddDstTensor("new_state", definition.dst_tensors[0]);
94   op.AddDstTensor("activation", definition.dst_tensors[1]);
95   op.code_ = GetLSTMCode(definition, gpu_info);
96   op.tensor_to_grid_ = TensorToGrid::kWBToX_HDToY_SToZ;
97   return op;
98 }
99 
100 }  // namespace gpu
101 }  // namespace tflite
102