Skip to content

Commit f76fd43

Browse files
Merge pull request #39868 from mansnils:relu
PiperOrigin-RevId: 321655082 Change-Id: I652e24e2c6a990e808b1ca54507e846abc0f323c
2 parents 6ddf013 + 8af3689 commit f76fd43

2 files changed

Lines changed: 44 additions & 17 deletions

File tree

‎tensorflow/lite/kernels/internal/types.h‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class RuntimeShape {
140140
if (dimensions_count > kMaxSmallSize) {
141141
#ifdef TF_LITE_STATIC_MEMORY
142142
TFLITE_CHECK(false && "No shape resizing supported on this platform");
143-
#else // TF_LITE_STATIC_MEMORY
143+
#else // TF_LITE_STATIC_MEMORY
144144
dims_pointer_ = new int32[dimensions_count];
145145
#endif // TF_LITE_STATIC_MEMORY
146146
}
@@ -179,7 +179,7 @@ class RuntimeShape {
179179
if (size_ > kMaxSmallSize) {
180180
#ifdef TF_LITE_STATIC_MEMORY
181181
TFLITE_CHECK(false && "No shape resizing supported on this platform");
182-
#else // TF_LITE_STATIC_MEMORY
182+
#else // TF_LITE_STATIC_MEMORY
183183
delete[] dims_pointer_;
184184
#endif // TF_LITE_STATIC_MEMORY
185185
}
@@ -214,15 +214,15 @@ class RuntimeShape {
214214
if (size_ > kMaxSmallSize) {
215215
#ifdef TF_LITE_STATIC_MEMORY
216216
TFLITE_CHECK(false && "No shape resizing supported on this platform");
217-
#else // TF_LITE_STATIC_MEMORY
217+
#else // TF_LITE_STATIC_MEMORY
218218
delete[] dims_pointer_;
219219
#endif // TF_LITE_STATIC_MEMORY
220220
}
221221
size_ = dimensions_count;
222222
if (dimensions_count > kMaxSmallSize) {
223223
#ifdef TF_LITE_STATIC_MEMORY
224224
TFLITE_CHECK(false && "No shape resizing supported on this platform");
225-
#else // TF_LITE_STATIC_MEMORY
225+
#else // TF_LITE_STATIC_MEMORY
226226
dims_pointer_ = new int32[dimensions_count];
227227
#endif // TF_LITE_STATIC_MEMORY
228228
}
@@ -738,7 +738,7 @@ struct ReluParams : public ActivationParams {
738738
int32 input_offset;
739739
int32 output_offset;
740740
int32 output_multiplier;
741-
int32 output_shift;
741+
int output_shift;
742742
};
743743

744744
// Styles of resizing op usages. For example, kImageStyle can be used with a Pad

‎tensorflow/lite/micro/kernels/activations.cc‎

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
#include "tensorflow/lite/kernels/internal/common.h"
1919
#include "tensorflow/lite/kernels/internal/quantization_util.h"
2020
#include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
21+
#include "tensorflow/lite/kernels/internal/types.h"
2122
#include "tensorflow/lite/kernels/kernel_util.h"
2223
#include "tensorflow/lite/kernels/op_macros.h"
2324
#include "tensorflow/lite/micro/micro_utils.h"
@@ -31,14 +32,44 @@ constexpr int kInputTensor = 0;
3132
constexpr int kOutputTensor = 0;
3233

3334
template <typename Q>
34-
inline void ReluQuantized(int32_t lower, const RuntimeShape& input_shape,
35-
const Q* input_data, const RuntimeShape& output_shape,
36-
Q* output_data) {
35+
inline void ReluQuantized(const TfLiteTensor* input, TfLiteTensor* output,
36+
const Q* input_data, Q* output_data) {
37+
ReluParams params;
38+
float act_min = 0.0;
39+
float act_max = std::numeric_limits<float>::infinity();
40+
double real_multiplier =
41+
static_cast<double>(input->params.scale / output->params.scale);
42+
43+
const RuntimeShape input_shape = GetTensorShape(input);
44+
const RuntimeShape output_shape = GetTensorShape(output);
45+
46+
QuantizeMultiplier(real_multiplier, &params.output_multiplier,
47+
&params.output_shift);
48+
49+
params.quantized_activation_min =
50+
std::max(static_cast<int32_t>(std::numeric_limits<Q>::min()),
51+
output->params.zero_point +
52+
static_cast<int32>(roundf(act_min / output->params.scale)));
53+
params.quantized_activation_max =
54+
act_max == std::numeric_limits<float>::infinity()
55+
? static_cast<int32_t>(std::numeric_limits<Q>::max())
56+
: std::min(
57+
static_cast<int32_t>(std::numeric_limits<Q>::max()),
58+
output->params.zero_point +
59+
static_cast<int32>(roundf(act_max / output->params.scale)));
60+
params.input_offset = input->params.zero_point;
61+
params.output_offset = output->params.zero_point;
62+
3763
const int flat_size = MatchingFlatSize(input_shape, output_shape);
3864
for (int i = 0; i < flat_size; ++i) {
39-
const Q val = input_data[i];
40-
const Q clamped = val < lower ? lower : val;
41-
output_data[i] = clamped;
65+
const int32 val = static_cast<int32_t>(input_data[i]);
66+
int32 clamped = params.output_offset +
67+
MultiplyByQuantizedMultiplier(val - params.input_offset,
68+
params.output_multiplier,
69+
params.output_shift);
70+
clamped = std::max(params.quantized_activation_min, clamped);
71+
clamped = std::min(params.quantized_activation_max, clamped);
72+
output_data[i] = static_cast<Q>(clamped);
4273
}
4374
}
4475

@@ -93,16 +124,12 @@ TfLiteStatus ReluEval(TfLiteContext* context, TfLiteNode* node) {
93124
return kTfLiteOk;
94125
}
95126
case kTfLiteInt8: {
96-
ReluQuantized<int8_t>(input->params.zero_point, GetTensorShape(input),
97-
GetTensorData<int8_t>(input),
98-
GetTensorShape(output),
127+
ReluQuantized<int8_t>(input, output, GetTensorData<int8_t>(input),
99128
GetTensorData<int8_t>(output));
100129
return kTfLiteOk;
101130
}
102131
case kTfLiteUInt8: {
103-
ReluQuantized<uint8_t>(input->params.zero_point, GetTensorShape(input),
104-
GetTensorData<uint8_t>(input),
105-
GetTensorShape(output),
132+
ReluQuantized<uint8_t>(input, output, GetTensorData<uint8_t>(input),
106133
GetTensorData<uint8_t>(output));
107134
return kTfLiteOk;
108135
}

0 commit comments

Comments
 (0)