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 #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CONCATENATION_H_
17 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CONCATENATION_H_
18 
19 #include "tensorflow/lite/kernels/internal/common.h"
20 #include "tensorflow/lite/kernels/internal/compatibility.h"
21 #include "tensorflow/lite/kernels/internal/cppmath.h"
22 #include "tensorflow/lite/kernels/internal/types.h"
23 
24 namespace tflite {
25 namespace reference_ops {
26 
27 template <typename Scalar>
Concatenation(const ConcatenationParams & params,const RuntimeShape * const * input_shapes,const Scalar * const * input_data,const RuntimeShape & output_shape,Scalar * output_data)28 inline void Concatenation(const ConcatenationParams& params,
29                           const RuntimeShape* const* input_shapes,
30                           const Scalar* const* input_data,
31                           const RuntimeShape& output_shape,
32                           Scalar* output_data) {
33   int axis = params.axis;
34   int inputs_count = params.inputs_count;
35   const int concat_dimensions = output_shape.DimensionsCount();
36   TFLITE_DCHECK_LT(axis, concat_dimensions);
37 
38   int64_t concat_size = 0;
39   for (int i = 0; i < inputs_count; i++) {
40     TFLITE_DCHECK_EQ(input_shapes[i]->DimensionsCount(), concat_dimensions);
41     for (int j = 0; j < concat_dimensions; j++) {
42       if (j != axis) {
43         MatchingDim(*input_shapes[i], j, output_shape, j);
44       }
45     }
46     concat_size += input_shapes[i]->Dims(axis);
47   }
48   TFLITE_DCHECK_EQ(concat_size, output_shape.Dims(axis));
49   int64_t outer_size = 1;
50   for (int i = 0; i < axis; ++i) {
51     outer_size *= output_shape.Dims(i);
52   }
53   // For all input arrays,
54   // FlatSize() = outer_size * Dims(axis) * base_inner_size;
55   int64_t base_inner_size = 1;
56   for (int i = axis + 1; i < concat_dimensions; ++i) {
57     base_inner_size *= output_shape.Dims(i);
58   }
59 
60   Scalar* output_ptr = output_data;
61   for (int k = 0; k < outer_size; k++) {
62     for (int i = 0; i < inputs_count; ++i) {
63       const int copy_size = input_shapes[i]->Dims(axis) * base_inner_size;
64       const Scalar* input_ptr = input_data[i] + k * copy_size;
65       memcpy(output_ptr, input_ptr, copy_size * sizeof(Scalar));
66       output_ptr += copy_size;
67     }
68   }
69 }
70 
71 // TODO(b/174275780): The quantized implementation of concatentation isn't fully
72 // quantized as it takes scale as a floating point value. This should be fixed
73 // when optimizng this routine further.
ConcatenationWithScaling(const ConcatenationParams & params,const RuntimeShape * const * input_shapes,const uint8_t * const * input_data,const RuntimeShape & output_shape,uint8_t * output_data)74 inline void ConcatenationWithScaling(const ConcatenationParams& params,
75                                      const RuntimeShape* const* input_shapes,
76                                      const uint8_t* const* input_data,
77                                      const RuntimeShape& output_shape,
78                                      uint8_t* output_data) {
79   int axis = params.axis;
80   const int32_t* input_zeropoint = params.input_zeropoint;
81   const float* input_scale = params.input_scale;
82   int inputs_count = params.inputs_count;
83   const int32_t output_zeropoint = params.output_zeropoint;
84   const float output_scale = params.output_scale;
85 
86   const int concat_dimensions = output_shape.DimensionsCount();
87   TFLITE_DCHECK_LT(axis, concat_dimensions);
88 
89   int64_t concat_size = 0;
90   for (int i = 0; i < inputs_count; i++) {
91     TFLITE_DCHECK_EQ(input_shapes[i]->DimensionsCount(), concat_dimensions);
92     for (int j = 0; j < concat_dimensions; j++) {
93       if (j != axis) {
94         MatchingDim(*input_shapes[i], j, output_shape, j);
95       }
96     }
97     concat_size += input_shapes[i]->Dims(axis);
98   }
99   TFLITE_DCHECK_EQ(concat_size, output_shape.Dims(axis));
100   int64_t outer_size = 1;
101   for (int i = 0; i < axis; ++i) {
102     outer_size *= output_shape.Dims(i);
103   }
104   // For all input arrays,
105   // FlatSize() = outer_size * Dims(axis) * base_inner_size;
106   int64_t base_inner_size = 1;
107   for (int i = axis + 1; i < concat_dimensions; ++i) {
108     base_inner_size *= output_shape.Dims(i);
109   }
110 
111   const float inverse_output_scale = 1.f / output_scale;
112   uint8_t* output_ptr = output_data;
113   for (int k = 0; k < outer_size; k++) {
114     for (int i = 0; i < inputs_count; ++i) {
115       const int copy_size = input_shapes[i]->Dims(axis) * base_inner_size;
116       const uint8_t* input_ptr = input_data[i] + k * copy_size;
117       if (input_zeropoint[i] == output_zeropoint &&
118           input_scale[i] == output_scale) {
119         memcpy(output_ptr, input_ptr, copy_size);
120       } else {
121         const float scale = input_scale[i] * inverse_output_scale;
122         const float bias = -input_zeropoint[i] * scale;
123         for (int j = 0; j < copy_size; ++j) {
124           const int32_t value = static_cast<int32_t>(tflite::TfLiteRound(
125                                     input_ptr[j] * scale + bias)) +
126                                 output_zeropoint;
127           output_ptr[j] = static_cast<uint8_t>(
128               std::max<int32_t>(std::min<int32_t>(255, value), 0));
129         }
130       }
131       output_ptr += copy_size;
132     }
133   }
134 }
135 
136 }  // namespace reference_ops
137 }  // namespace tflite
138 
139 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CONCATENATION_H_
140