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/experimental/kernels/gru_cell.h"
17
18 #include <vector>
19
20 #include "tensorflow/lite/kernels/internal/optimized/optimized_ops.h"
21
22 namespace tflite {
23 namespace ops {
24 namespace experimental {
25 namespace gru_cell {
26
27 using optimized_ops::ArrayMap;
28 using optimized_ops::FullyConnected;
29 using optimized_ops::MapAsArrayWithLastDimAsRows;
30 using reference_ops::Concatenation;
31
GruCell(const RuntimeShape & input_shape,const float * input,const RuntimeShape & state_shape,const float * input_state,const RuntimeShape & gate_weight_shape,const float * gate_weight,const RuntimeShape & gate_bias_shape,const float * gate_bias,const RuntimeShape & candidate_weight_shape,const float * candidate_weight,const RuntimeShape & candidate_bias_shape,const float * candidate_bias,const RuntimeShape & output_shape,float * output,float * output_state,const RuntimeShape & activation_shape,float * activation,const RuntimeShape & concat_shape,float * concat,const tflite::FullyConnectedParams & fc_params,tflite::CpuBackendContext * cpu_backend_context)32 void GruCell(const RuntimeShape& input_shape, const float* input,
33 const RuntimeShape& state_shape, const float* input_state,
34 const RuntimeShape& gate_weight_shape, const float* gate_weight,
35 const RuntimeShape& gate_bias_shape, const float* gate_bias,
36 const RuntimeShape& candidate_weight_shape,
37 const float* candidate_weight,
38 const RuntimeShape& candidate_bias_shape,
39 const float* candidate_bias, const RuntimeShape& output_shape,
40 float* output, float* output_state,
41 const RuntimeShape& activation_shape, float* activation,
42 const RuntimeShape& concat_shape, float* concat,
43 const tflite::FullyConnectedParams& fc_params,
44 tflite::CpuBackendContext* cpu_backend_context) {
45 const int n_batch = input_shape.Dims(0);
46 const int n_input = input_shape.Dims(1);
47 const int n_output = state_shape.Dims(1);
48
49 // [x h] = concat(input, state)
50 std::vector<float const*> concat_arrays_data;
51 std::vector<RuntimeShape const*> concat_arrays_shapes;
52 concat_arrays_data.push_back(input);
53 concat_arrays_data.push_back(input_state);
54 concat_arrays_shapes.push_back(&input_shape);
55 concat_arrays_shapes.push_back(&state_shape);
56 tflite::ConcatenationParams concat_params;
57 concat_params.axis = 1;
58 concat_params.inputs_count = concat_arrays_data.size();
59 Concatenation(concat_params, &(concat_arrays_shapes[0]),
60 &(concat_arrays_data[0]), concat_shape, concat);
61
62 // [r u] = [x h] * gate_weight + gate_bias
63 FullyConnected(fc_params, concat_shape, concat, gate_weight_shape,
64 gate_weight, gate_bias_shape, gate_bias, activation_shape,
65 activation, cpu_backend_context);
66
67 // [r u] = sigmoid([r u])
68 auto ru = MapAsArrayWithLastDimAsRows(activation, activation_shape);
69 ru = ru.unaryExpr(Eigen::internal::scalar_logistic_op<float>());
70 auto r = ru.block(0 * n_output, 0, n_output, n_batch);
71 auto u = ru.block(1 * n_output, 0, n_output, n_batch);
72
73 // hr = h .* r
74 auto h = MapAsArrayWithLastDimAsRows(input_state, state_shape);
75 auto xh = MapAsArrayWithLastDimAsRows(concat, concat_shape);
76 auto hr = xh.block(n_input, 0, n_output, n_batch);
77 hr = h * r;
78
79 // c = [x hr] * candidate_weight + candidate_bias
80 FullyConnected(fc_params, concat_shape, concat, candidate_weight_shape,
81 candidate_weight, candidate_bias_shape, candidate_bias,
82 output_shape, output, cpu_backend_context);
83
84 auto c = MapAsArrayWithLastDimAsRows(output, output_shape);
85 // output = (1 - u) .* tanh(c) + u .* h
86 c = (1.0 - u) * c.tanh() + u * h;
87
88 memcpy(output_state, output, n_batch * n_output * sizeof(float));
89 }
90
91 } // namespace gru_cell
92 } // namespace experimental
93 } // namespace ops
94 } // namespace tflite
95