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