xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/kernels/internal/reference/integer_ops/mul.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_MUL_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_MUL_H_
17 
18 #include <algorithm>
19 
20 #include "fixedpoint/fixedpoint.h"
21 #include "ruy/profiler/instrumentation.h"  // from @ruy
22 #include "tensorflow/lite/kernels/internal/common.h"
23 
24 namespace tflite {
25 namespace reference_integer_ops {
26 
27 template <typename T>
MulElementwise(int size,const ArithmeticParams & params,const T * input1_data,const T * input2_data,T * output_data)28 inline void MulElementwise(int size, const ArithmeticParams& params,
29                            const T* input1_data, const T* input2_data,
30                            T* output_data) {
31   for (int i = 0; i < size; ++i) {
32     const int32_t input1_val = params.input1_offset + input1_data[i];
33     const int32_t input2_val = params.input2_offset + input2_data[i];
34     const int32_t unclamped_result =
35         params.output_offset +
36         MultiplyByQuantizedMultiplier(input1_val * input2_val,
37                                       params.output_multiplier,
38                                       params.output_shift);
39     const int32_t clamped_output =
40         std::min(params.quantized_activation_max,
41                  std::max(params.quantized_activation_min, unclamped_result));
42     output_data[i] = static_cast<T>(clamped_output);
43   }
44 }
45 
46 template <typename T>
Mul(const ArithmeticParams & params,const RuntimeShape & input1_shape,const T * input1_data,const RuntimeShape & input2_shape,const T * input2_data,const RuntimeShape & output_shape,T * output_data)47 inline void Mul(const ArithmeticParams& params,
48                 const RuntimeShape& input1_shape, const T* input1_data,
49                 const RuntimeShape& input2_shape, const T* input2_data,
50                 const RuntimeShape& output_shape, T* output_data) {
51   TFLITE_DCHECK_LE(params.quantized_activation_min,
52                    params.quantized_activation_max);
53   ruy::profiler::ScopeLabel label("Mul/8bit");
54   const int flat_size =
55       MatchingElementsSize(input1_shape, input2_shape, output_shape);
56 
57   MulElementwise(flat_size, params, input1_data, input2_data, output_data);
58 }
59 
60 // Mul with 16 bit inputs and int8_t outputs.
Mul(const ArithmeticParams & params,const RuntimeShape & input1_shape,const int16_t * input1_data,const RuntimeShape & input2_shape,const int16_t * input2_data,const RuntimeShape & output_shape,int8_t * output_data)61 inline void Mul(const ArithmeticParams& params,
62                 const RuntimeShape& input1_shape, const int16_t* input1_data,
63                 const RuntimeShape& input2_shape, const int16_t* input2_data,
64                 const RuntimeShape& output_shape, int8_t* output_data) {
65   ruy::profiler::ScopeLabel label("Mul/Int16Int8");
66   int32_t output_offset = params.output_offset;
67   int32_t output_activation_min = params.quantized_activation_min;
68   int32_t output_activation_max = params.quantized_activation_max;
69   TFLITE_DCHECK_LE(output_activation_min, output_activation_max);
70 
71   const int flat_size =
72       MatchingElementsSize(input1_shape, input2_shape, output_shape);
73 
74   for (int i = 0; i < flat_size; i++) {
75     // F0 uses 0 integer bits, range [-1, 1].
76     using F0 = gemmlowp::FixedPoint<std::int16_t, 0>;
77 
78     F0 unclamped_result =
79         F0::FromRaw(input1_data[i]) * F0::FromRaw(input2_data[i]);
80     int16_t rescaled_result =
81         gemmlowp::RoundingDivideByPOT(unclamped_result.raw(), 8);
82     int16_t clamped_result = std::min<int16_t>(
83         output_activation_max - output_offset, rescaled_result);
84     clamped_result = std::max<int16_t>(output_activation_min - output_offset,
85                                        clamped_result);
86     output_data[i] = output_offset + clamped_result;
87   }
88 }
89 
90 template <typename T>
BroadcastMul4DSlow(const ArithmeticParams & params,const RuntimeShape & input1_shape,const T * input1_data,const RuntimeShape & input2_shape,const T * input2_data,const RuntimeShape & output_shape,T * output_data)91 inline void BroadcastMul4DSlow(
92     const ArithmeticParams& params, const RuntimeShape& input1_shape,
93     const T* input1_data, const RuntimeShape& input2_shape,
94     const T* input2_data, const RuntimeShape& output_shape, T* output_data) {
95   ruy::profiler::ScopeLabel label("BroadcastMul4DSlow");
96 
97   NdArrayDesc<4> desc1;
98   NdArrayDesc<4> desc2;
99   // The input shapes are extended as part of NdArrayDesc initialization.
100   NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1,
101                                       &desc2);
102   const RuntimeShape extended_output_shape =
103       RuntimeShape::ExtendedShape(4, output_shape);
104 
105   for (int b = 0; b < extended_output_shape.Dims(0); ++b) {
106     for (int y = 0; y < extended_output_shape.Dims(1); ++y) {
107       for (int x = 0; x < extended_output_shape.Dims(2); ++x) {
108         for (int c = 0; c < extended_output_shape.Dims(3); ++c) {
109           const int32_t input1_val =
110               params.input1_offset +
111               input1_data[SubscriptToIndex(desc1, b, y, x, c)];
112           const int32_t input2_val =
113               params.input2_offset +
114               input2_data[SubscriptToIndex(desc2, b, y, x, c)];
115           const int32_t unclamped_result =
116               params.output_offset +
117               MultiplyByQuantizedMultiplier(input1_val * input2_val,
118                                             params.output_multiplier,
119                                             params.output_shift);
120           const int32_t clamped_output = std::min(
121               params.quantized_activation_max,
122               std::max(params.quantized_activation_min, unclamped_result));
123           output_data[Offset(extended_output_shape, b, y, x, c)] =
124               static_cast<T>(clamped_output);
125         }
126       }
127     }
128   }
129 }
130 
131 }  // namespace reference_integer_ops
132 }  // namespace tflite
133 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_MUL_H_
134