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