Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 16bit support for unpack, transpose and comparisons operators #3028

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tensorflow/lite/micro/kernels/comparisons.cc
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,19 @@ TfLiteStatus GreaterEval(TfLiteContext* context, TfLiteNode* node) {
tflite::micro::GetTensorData<int8_t>(input2), output_shape,
output_data);
break;
case kTfLiteInt16:
requires_broadcast
? reference_ops::Broadcast4DSlowGreaterWithScaling(
data->params, input1_shape,
tflite::micro::GetTensorData<int16_t>(input1), input2_shape,
tflite::micro::GetTensorData<int16_t>(input2), output_shape,
output_data)
: reference_ops::GreaterWithScaling(
data->params, input1_shape,
tflite::micro::GetTensorData<int16_t>(input1), input2_shape,
tflite::micro::GetTensorData<int16_t>(input2), output_shape,
output_data);
break;
default:
MicroPrintf("Type %s (%d) not supported.",
TfLiteTypeGetName(input1->type), input1->type);
Expand Down
102 changes: 86 additions & 16 deletions tensorflow/lite/micro/kernels/fully_connected.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,25 +238,95 @@ TfLiteStatus FullyConnectedEval(TfLiteContext* context, TfLiteNode* node) {
case kTfLiteInt16: {
switch (filter->type) {
case kTfLiteInt8: {
tflite::reference_integer_ops::FullyConnected(
FullyConnectedParamsQuantized(data),
tflite::micro::GetTensorShape(input),
tflite::micro::GetTensorData<int16_t>(input),
tflite::micro::GetTensorShape(filter),
if (bias == nullptr || bias->type == kTfLiteInt32) {
data.is_per_channel
? tflite::reference_integer_ops::FullyConnectedPerChannel(
FullyConnectedParamsQuantized(data),
data.per_channel_output_multiplier,
reinterpret_cast<const int*>(data.per_channel_output_shift),
tflite::micro::GetTensorShape(input),
tflite::micro::GetTensorData<int16_t>(input),
tflite::micro::GetTensorShape(filter),
#ifdef USE_TFLM_COMPRESSION
tflite::micro::GetTensorData<int8_t>(micro_context, filter,
weights_comp_td,
data.weights_scratch_index),
tflite::micro::GetTensorShape(bias),
tflite::micro::GetOptionalTensorData<int64_t>(
micro_context, bias, bias_comp_td, data.bias_scratch_index),
tflite::micro::GetTensorData<int8_t>(micro_context, filter,
weights_comp_td,
data.weights_scratch_index),
tflite::micro::GetTensorShape(bias),
tflite::micro::GetOptionalTensorData<int32_t>(
micro_context, bias, bias_comp_td,
data.bias_scratch_index),
#else // USE_TFLM_COMPRESSION
tflite::micro::GetTensorData<int8_t>(filter),
tflite::micro::GetTensorShape(bias),
tflite::micro::GetOptionalTensorData<int64_t>(bias),
tflite::micro::GetTensorData<int8_t>(filter),
tflite::micro::GetTensorShape(bias),
tflite::micro::GetOptionalTensorData<int32_t>(bias),
#endif // USE_TFLM_COMPRESSION
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int16_t>(output));
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int16_t>(output))
: tflite::reference_integer_ops::FullyConnected(
FullyConnectedParamsQuantized(data),
tflite::micro::GetTensorShape(input),
tflite::micro::GetTensorData<int16_t>(input),
tflite::micro::GetTensorShape(filter),
#ifdef USE_TFLM_COMPRESSION
tflite::micro::GetTensorData<int8_t>(micro_context, filter,
weights_comp_td,
data.weights_scratch_index),
tflite::micro::GetTensorShape(bias),
tflite::micro::GetOptionalTensorData<int32_t>(
micro_context, bias, bias_comp_td,
data.bias_scratch_index),
#else // USE_TFLM_COMPRESSION
tflite::micro::GetTensorData<int8_t>(filter),
tflite::micro::GetTensorShape(bias),
tflite::micro::GetOptionalTensorData<int32_t>(bias),
#endif // USE_TFLM_COMPRESSION
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int16_t>(output));
} else if (bias->type == kTfLiteInt64) {
data.is_per_channel
? tflite::reference_integer_ops::FullyConnectedPerChannel(
FullyConnectedParamsQuantized(data),
data.per_channel_output_multiplier,
reinterpret_cast<const int*>(data.per_channel_output_shift),
tflite::micro::GetTensorShape(input),
tflite::micro::GetTensorData<int16_t>(input),
tflite::micro::GetTensorShape(filter),
#ifdef USE_TFLM_COMPRESSION
tflite::micro::GetTensorData<int8_t>(micro_context, filter,
weights_comp_td,
data.weights_scratch_index),
tflite::micro::GetTensorShape(bias),
tflite::micro::GetOptionalTensorData<int64_t>(
micro_context, bias, bias_comp_td,
data.bias_scratch_index),
#else // USE_TFLM_COMPRESSION
tflite::micro::GetTensorData<int8_t>(filter),
tflite::micro::GetTensorShape(bias),
tflite::micro::GetOptionalTensorData<int64_t>(bias),
#endif // USE_TFLM_COMPRESSION
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int16_t>(output))
: tflite::reference_integer_ops::FullyConnected(
FullyConnectedParamsQuantized(data),
tflite::micro::GetTensorShape(input),
tflite::micro::GetTensorData<int16_t>(input),
tflite::micro::GetTensorShape(filter),
#ifdef USE_TFLM_COMPRESSION
tflite::micro::GetTensorData<int8_t>(micro_context, filter,
weights_comp_td,
data.weights_scratch_index),
tflite::micro::GetTensorShape(bias),
tflite::micro::GetOptionalTensorData<int64_t>(
micro_context, bias, bias_comp_td,
data.bias_scratch_index),
#else // USE_TFLM_COMPRESSION
tflite::micro::GetTensorData<int8_t>(filter),
tflite::micro::GetTensorShape(bias),
tflite::micro::GetOptionalTensorData<int64_t>(bias),
#endif // USE_TFLM_COMPRESSION
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int16_t>(output));
}
break;
}
default: {
Expand Down
11 changes: 8 additions & 3 deletions tensorflow/lite/micro/kernels/fully_connected_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,14 @@ TfLiteStatus CalculateOpDataFullyConnected(
filter->quantization.params);
const int per_channel_quantization_size = affine_quantization->scale->size;

// Currently only Int8 is supported for per channel quantization.
TF_LITE_ENSURE(context,
input->type == kTfLiteInt8 && filter->type != kTfLiteInt4);
// Currently only Int8/Int16 are supported for per channel quantization.
TF_LITE_ENSURE(
context,
(input->type == kTfLiteInt8 && filter->type != kTfLiteInt4) ||
(input->type == kTfLiteInt16 && filter->type != kTfLiteInt4));

TF_LITE_ENSURE_EQ(context, affine_quantization->scale->size,
per_channel_quantization_size);

TF_LITE_ENSURE_EQ(
context, per_channel_quantization_size,
Expand Down
8 changes: 7 additions & 1 deletion tensorflow/lite/micro/kernels/transpose.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,16 @@ TfLiteStatus TransposeEval(TfLiteContext* context, TfLiteNode* node) {
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int8_t>(output));
break;
case kTfLiteInt16:
reference_ops::Transpose(params, tflite::micro::GetTensorShape(input),
tflite::micro::GetTensorData<int16_t>(input),
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int16_t>(output));
break;
default:
MicroPrintf(
"Type %s is currently not supported by Transpose. "
"Only float32 and int8 is supported",
"Only float32, int8 and int16 is supported",
TfLiteTypeGetName(input->type));
return kTfLiteError;
}
Expand Down
3 changes: 3 additions & 0 deletions tensorflow/lite/micro/kernels/unpack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ TfLiteStatus UnpackEval(TfLiteContext* context, TfLiteNode* node) {
case kTfLiteInt8: {
return UnpackImpl<int8_t>(context, node, input, data->num, data->axis);
}
case kTfLiteInt16: {
return UnpackImpl<int16_t>(context, node, input, data->num, data->axis);
}
default: {
MicroPrintf("Type '%s' is not supported by unpack.",
TfLiteTypeGetName(input->type));
Expand Down
Loading