From 5e4951727063b3cde8c1e461be5e042cc80b419c Mon Sep 17 00:00:00 2001 From: ddavis-2015 Date: Wed, 7 Aug 2024 17:20:29 -0700 Subject: [PATCH] Cleanup TODOs. Remove unit test debug output. Support no bias tensor for FULLY_CONNECTED compressed unit tests. More code cleanup. --- tensorflow/lite/micro/kernels/conv.cc | 1 - tensorflow/lite/micro/kernels/conv_test.h | 13 +--- .../lite/micro/kernels/fully_connected.cc | 1 - .../micro/kernels/fully_connected_test.cc | 57 ++++++++-------- tensorflow/lite/micro/kernels/kernel_util.h | 2 - .../lite/micro/kernels/transpose_conv.cc | 1 - .../lite/micro/kernels/transpose_conv_test.cc | 13 +--- tensorflow/lite/micro/micro_allocator.cc | 4 +- .../micro/recording_micro_allocator_test.cc | 2 +- tensorflow/lite/micro/test_helpers.cc | 68 ------------------- tensorflow/lite/micro/test_helpers.h | 39 ++++------- 11 files changed, 50 insertions(+), 151 deletions(-) diff --git a/tensorflow/lite/micro/kernels/conv.cc b/tensorflow/lite/micro/kernels/conv.cc index 7364d609e5b..221d560afa6 100644 --- a/tensorflow/lite/micro/kernels/conv.cc +++ b/tensorflow/lite/micro/kernels/conv.cc @@ -47,7 +47,6 @@ TfLiteStatus ConvEval(TfLiteContext* context, TfLiteNode* node) { #ifdef USE_TFLM_COMPRESSION - // TODO(ddavis-2015): make micro_context a const pointer MicroContext* micro_context = GetMicroContext(context); const CompressionTensorData* weights_comp_td = diff --git a/tensorflow/lite/micro/kernels/conv_test.h b/tensorflow/lite/micro/kernels/conv_test.h index e0d0a277c0f..7f6c55e2a9e 100644 --- a/tensorflow/lite/micro/kernels/conv_test.h +++ b/tensorflow/lite/micro/kernels/conv_test.h @@ -39,7 +39,7 @@ TfLiteStatus InvokeConv(TfLiteTensor* tensors, int tensors_size, const CompressedTensorList* comp_list_p = nullptr #endif // USE_TFLM_COMPRESSION ) { - // TODO(ddavis-2015): support optional bias tensor + // TODO(b/358165875): support optional bias tensor int inputs_array_data[] = {3, 0, 1, 2}; TfLiteIntArray* inputs_array = IntArrayFromInts(inputs_array_data); int outputs_array_data[] = {1, 3}; @@ -187,7 +187,7 @@ TfLiteStatus TestConvQuantizedPerChannelCompressed( TIO* output_quantized, float output_scale, int output_zero_point, const TfLiteConvParams* conv_params, TFLMRegistration registration, const TestCompressionQuantizedInfo* comp_info) { - // TODO(ddavis-2015): account for optional bias tensor + // TODO(b/358165875): account for optional bias tensor // bool null_bias = comp_info->bias_data == nullptr ? true : false; TfLiteIntArray* input_dims = IntArrayFromInts(input_dims_data); @@ -221,15 +221,6 @@ TfLiteStatus TestConvQuantizedPerChannelCompressed( ElementCount(*bias_dims), bias_scales->size, bias_scales->data); - for (int i = 0; i < ElementCount(*bias_dims); i++) { - int64_t bias_data0 = comp_info->bias_value_table[i]; - MicroPrintf( - "bias scale %f bias zero_point %d" - " bias data %f bias data quantized %lld", - (double)bias_scales->data[i], bias_zero_points->data[i], - (double)comp_info->bias_data[i], bias_data0); - } - constexpr int tensors_size = kConvMaxTensors; TfLiteTensor tensors[tensors_size] = { CreateQuantizedTensor(input_data, input_quantized, input_dims, diff --git a/tensorflow/lite/micro/kernels/fully_connected.cc b/tensorflow/lite/micro/kernels/fully_connected.cc index c779ea329f3..21d061ae430 100644 --- a/tensorflow/lite/micro/kernels/fully_connected.cc +++ b/tensorflow/lite/micro/kernels/fully_connected.cc @@ -121,7 +121,6 @@ TfLiteStatus FullyConnectedEval(TfLiteContext* context, TfLiteNode* node) { #ifdef USE_TFLM_COMPRESSION - // TODO(ddavis-2015): make micro_context a const pointer MicroContext* micro_context = GetMicroContext(context); const CompressionTensorData* weights_comp_td = diff --git a/tensorflow/lite/micro/kernels/fully_connected_test.cc b/tensorflow/lite/micro/kernels/fully_connected_test.cc index 6054282dab2..8c605fefbcb 100644 --- a/tensorflow/lite/micro/kernels/fully_connected_test.cc +++ b/tensorflow/lite/micro/kernels/fully_connected_test.cc @@ -304,11 +304,13 @@ TfLiteStatus ValidateFullyConnectedGoldens( kFullyConnectedWeightsTensor), kTfLiteOk); TF_LITE_MICRO_CHECK_FAIL(); - TF_LITE_MICRO_EXPECT_EQ( - tcl.AddBias(*comp_info, tensors[kFullyConnectedBiasTensor], - kFullyConnectedBiasTensor), - kTfLiteOk); - TF_LITE_MICRO_CHECK_FAIL(); + if (!null_bias) { + TF_LITE_MICRO_EXPECT_EQ( + tcl.AddBias(*comp_info, tensors[kFullyConnectedBiasTensor], + kFullyConnectedBiasTensor), + kTfLiteOk); + TF_LITE_MICRO_CHECK_FAIL(); + } comp_list_p = tcl.GetCompressedTensorList(); } @@ -440,8 +442,6 @@ TfLiteStatus TestFullyConnectedQuantizedCompressed( TIO* output_quantized, float output_scale, int output_zero_point, const TfLiteFusedActivation activation, const TestCompressionQuantizedInfo* comp_info) { - // TODO(ddavis-2015): account for optional bias tensor - bool null_bias = comp_info->bias_data == nullptr ? true : false; TfLiteIntArray* input_dims = IntArrayFromInts(input_dims_data); @@ -460,30 +460,29 @@ TfLiteStatus TestFullyConnectedQuantizedCompressed( SymmetricQuantize(comp_info->filter_data, comp_info->filter_value_table, ElementCount(*filter_dims), filter_scales->data[0]); - TfLiteTensor bias_tensor = CreateQuantizedTensor( - comp_info->bias_compressed, bias_dims, - input_scale * filter_scales->data[0], 0, false, typeToTfLiteType()); - SymmetricQuantize(comp_info->bias_data, comp_info->bias_value_table, - ElementCount(*bias_dims), bias_tensor.params.scale); - - for (int i = 0; i < ElementCount(*bias_dims); i++) { - int64_t bias_data0 = comp_info->bias_value_table[i]; - MicroPrintf( - "bias scale %f bias zero_point %d" - " bias data %f bias data quantized %lld", - (double)bias_tensor.params.scale, bias_tensor.params.zero_point, - (double)comp_info->bias_data[i], bias_data0); + TfLiteTensor bias_tensor = {}; + if (!null_bias) { + bias_tensor = CreateQuantizedTensor(comp_info->bias_compressed, bias_dims, + input_scale * filter_scales->data[0], 0, + false, typeToTfLiteType()); + SymmetricQuantize(comp_info->bias_data, comp_info->bias_value_table, + ElementCount(*bias_dims), bias_tensor.params.scale); } - constexpr int tensors_size = kMaxTensors; - TfLiteTensor tensors[tensors_size] = { - CreateQuantizedTensor(input_data, input_quantized, input_dims, - input_scale, input_zero_point), - filter_tensor, - bias_tensor, - CreateQuantizedTensor(output_quantized, output_dims, output_scale, - output_zero_point), - }; + TfLiteTensor output_tensor = CreateQuantizedTensor( + output_quantized, output_dims, output_scale, output_zero_point); + + const int tensors_size = null_bias ? kMaxTensors - 1 : kMaxTensors; + TfLiteTensor tensors[kMaxTensors] = {}; + tensors[0] = CreateQuantizedTensor(input_data, input_quantized, input_dims, + input_scale, input_zero_point); + tensors[1] = filter_tensor; + if (null_bias) { + tensors[2] = output_tensor; + } else { + tensors[2] = bias_tensor; + tensors[3] = output_tensor; + } const int output_dims_count = ElementCount(*output_dims); Quantize(expected_output_data, expected_output_quantized, output_dims_count, diff --git a/tensorflow/lite/micro/kernels/kernel_util.h b/tensorflow/lite/micro/kernels/kernel_util.h index 977ed9563e1..5ef4bac85c9 100644 --- a/tensorflow/lite/micro/kernels/kernel_util.h +++ b/tensorflow/lite/micro/kernels/kernel_util.h @@ -95,8 +95,6 @@ const T* GetOptionalTensorData(const TfLiteEvalTensor* tensor) { // Overloads existing GetTensorData. If not compressed, this will return // tensor->data. -// -// TODO(ddavis-2015): make micro_context a const pointer template const T* GetTensorData(MicroContext* micro_context, const TfLiteEvalTensor* tensor, diff --git a/tensorflow/lite/micro/kernels/transpose_conv.cc b/tensorflow/lite/micro/kernels/transpose_conv.cc index a6c3692a86e..7d65dc3de7c 100644 --- a/tensorflow/lite/micro/kernels/transpose_conv.cc +++ b/tensorflow/lite/micro/kernels/transpose_conv.cc @@ -272,7 +272,6 @@ TfLiteStatus TransposeConvEval(TfLiteContext* context, TfLiteNode* node) { #ifdef USE_TFLM_COMPRESSION - // TODO(ddavis-2015): make micro_context a const pointer MicroContext* micro_context = GetMicroContext(context); const CompressionTensorData* filter_comp_td = diff --git a/tensorflow/lite/micro/kernels/transpose_conv_test.cc b/tensorflow/lite/micro/kernels/transpose_conv_test.cc index e8c6bd4ad27..e9716794229 100644 --- a/tensorflow/lite/micro/kernels/transpose_conv_test.cc +++ b/tensorflow/lite/micro/kernels/transpose_conv_test.cc @@ -162,7 +162,7 @@ TfLiteStatus InvokeTransposeConv( const CompressedTensorList* comp_list_p = nullptr #endif // USE_TFLM_COMPRESSION ) { - // TODO(ddavis-2015): support optional bias tensor + // TODO(b/358151309): support optional bias tensor int inputs_array_data[] = {4, 0, 1, 2, 3}; TfLiteIntArray* inputs_array = IntArrayFromInts(inputs_array_data); int outputs_array_data[] = {1, 4}; @@ -378,7 +378,7 @@ TfLiteStatus TestTransposeConvQuantizedCompressed( TIO* output_quantized, float output_scale, int output_zero_point, const TfLiteConvParams* conv_params, const unsigned int tolerance, const TestCompressionQuantizedInfo* comp_info) { - // TODO(ddavis-2015): account for optional bias tensor + // TODO(b/358151309): account for optional bias tensor // bool null_bias = comp_info->bias_data == nullptr ? true : false; TfLiteIntArray* input_dims = IntArrayFromInts(input_dims_data); @@ -413,15 +413,6 @@ TfLiteStatus TestTransposeConvQuantizedCompressed( ElementCount(*bias_dims), bias_scales->size, bias_scales->data); - for (int i = 0; i < ElementCount(*bias_dims); i++) { - int64_t bias_data0 = comp_info->bias_value_table[i]; - MicroPrintf( - "bias scale %f bias zero_point %d" - " bias data %f bias data quantized %lld", - (double)bias_scales->data[i], bias_zero_points->data[i], - (double)comp_info->bias_data[i], bias_data0); - } - int output_shape_dims_data[] = {1, 0}; int32_t* output_shape = nullptr; TfLiteIntArray* output_shape_dims = IntArrayFromInts(output_shape_dims_data); diff --git a/tensorflow/lite/micro/micro_allocator.cc b/tensorflow/lite/micro/micro_allocator.cc index d65a076deaa..5caefa34764 100644 --- a/tensorflow/lite/micro/micro_allocator.cc +++ b/tensorflow/lite/micro/micro_allocator.cc @@ -398,7 +398,9 @@ const tflite::micro::compression::Metadata* GetCompressionMetadata( buffer_index); continue; } - // TODO(ddavis-2015): support multiple compression methods + // TODO(ddavis-2015): support multiple compression methods, possibly + // through multiple verification checks. + // Then return a pair. auto compression_metadata = tflite::micro::compression::GetSizePrefixedMetadata(vp); flatbuffers::Verifier verifier(vp->data(), vp->size(), diff --git a/tensorflow/lite/micro/recording_micro_allocator_test.cc b/tensorflow/lite/micro/recording_micro_allocator_test.cc index 0ec19370c72..121a74c3324 100644 --- a/tensorflow/lite/micro/recording_micro_allocator_test.cc +++ b/tensorflow/lite/micro/recording_micro_allocator_test.cc @@ -348,7 +348,7 @@ TF_LITE_MICRO_TEST(TestCompressedModel) { size_t size_compression_allocations = 0; for (size_t subgraph_idx = 0; subgraph_idx < model->subgraphs()->size(); subgraph_idx++) { - tflite::CompressionTensorData** ctl = + const tflite::CompressionTensorData** ctl = subgraph_allocations[subgraph_idx].compressed.tensors; if (ctl == nullptr) { continue; diff --git a/tensorflow/lite/micro/test_helpers.cc b/tensorflow/lite/micro/test_helpers.cc index d5f50773f69..aeb9a439ecf 100644 --- a/tensorflow/lite/micro/test_helpers.cc +++ b/tensorflow/lite/micro/test_helpers.cc @@ -2021,74 +2021,6 @@ TfLiteFloatArray* FloatArrayFromFloats(const float* floats) { return reinterpret_cast(const_cast(floats)); } -// TODO(ddavis-2015): make template -TfLiteTensor CreateQuantizedBiasTensor(const float* data, int16_t* quantized, - TfLiteIntArray* dims, float input_scale, - float weights_scale, bool is_variable) { - float bias_scale = input_scale * weights_scale; - tflite::SymmetricQuantize(data, quantized, ElementCount(*dims), bias_scale); - - // Quantized int16_t tensors always have a zero point of 0, since the range of - // int16_t values is large, and because zero point costs extra cycles during - // processing. - TfLiteTensor result = - CreateQuantizedTensor(quantized, dims, bias_scale, 0, is_variable); - return result; -} - -// TODO(ddavis-2015): make template -TfLiteTensor CreateQuantizedBiasTensor(const float* data, int32_t* quantized, - TfLiteIntArray* dims, float input_scale, - float weights_scale, bool is_variable) { - float bias_scale = input_scale * weights_scale; - tflite::SymmetricQuantize(data, quantized, ElementCount(*dims), bias_scale); - - // Quantized int32_t tensors always have a zero point of 0, since the range of - // int32_t values is large, and because zero point costs extra cycles during - // processing. - TfLiteTensor result = - CreateQuantizedTensor(quantized, dims, bias_scale, 0, is_variable); - return result; -} - -// TODO(ddavis-2015): make template -TfLiteTensor CreateQuantizedBiasTensor(const float* data, - std::int64_t* quantized, - TfLiteIntArray* dims, float input_scale, - float weights_scale, bool is_variable) { - float bias_scale = input_scale * weights_scale; - tflite::SymmetricQuantize(data, quantized, ElementCount(*dims), bias_scale); - - // Quantized int32_t tensors always have a zero point of 0, since the range of - // int32_t values is large, and because zero point costs extra cycles during - // processing. - TfLiteTensor result = - CreateQuantizedTensor(quantized, dims, bias_scale, 0, is_variable); - return result; -} - -// TODO(ddavis-2015): remove -TfLiteTensor CreatePerChannelQuantizedBiasTensor( - const float* input, int32_t* quantized, TfLiteIntArray* dims, - float input_scale, float* weight_scales, float* scales, int* zero_points, - TfLiteAffineQuantization* affine_quant, int quantized_dimension, - bool is_variable) { - return CreatePerChannelQuantizedBiasTensor( - input, quantized, dims, input_scale, weight_scales, scales, zero_points, - affine_quant, quantized_dimension, is_variable); -} - -// TODO(ddavis-2015): remove -TfLiteTensor CreatePerChannelQuantizedBiasTensor( - const float* input, std::int64_t* quantized, TfLiteIntArray* dims, - float input_scale, float* weight_scales, float* scales, int* zero_points, - TfLiteAffineQuantization* affine_quant, int quantized_dimension, - bool is_variable) { - return CreatePerChannelQuantizedBiasTensor( - input, quantized, dims, input_scale, weight_scales, scales, zero_points, - affine_quant, quantized_dimension, is_variable); -} - TfLiteTensor CreateSymmetricPerChannelQuantizedTensor( const float* input, int8_t* quantized, TfLiteIntArray* dims, float* scales, int* zero_points, TfLiteAffineQuantization* affine_quant, diff --git a/tensorflow/lite/micro/test_helpers.h b/tensorflow/lite/micro/test_helpers.h index b996dcb4f7b..6831b467bc8 100644 --- a/tensorflow/lite/micro/test_helpers.h +++ b/tensorflow/lite/micro/test_helpers.h @@ -280,24 +280,21 @@ TfLiteTensor CreateQuantizedTensor(const float* input, T* quantized, type); } -// TODO(ddavis-2015): remove -TfLiteTensor CreateQuantizedBiasTensor(const float* data, int16_t* quantized, - TfLiteIntArray* dims, float input_scale, - float weights_scale, - bool is_variable = false); - -// TODO(ddavis-2015): remove -TfLiteTensor CreateQuantizedBiasTensor(const float* data, int32_t* quantized, - TfLiteIntArray* dims, float input_scale, - float weights_scale, - bool is_variable = false); - -// TODO(ddavis-2015): remove -TfLiteTensor CreateQuantizedBiasTensor(const float* data, - std::int64_t* quantized, +template +TfLiteTensor CreateQuantizedBiasTensor(const float* data, T* quantized, TfLiteIntArray* dims, float input_scale, float weights_scale, - bool is_variable = false); + bool is_variable = false) { + float bias_scale = input_scale * weights_scale; + tflite::SymmetricQuantize(data, quantized, ElementCount(*dims), bias_scale); + + // Quantized bias tensors always have a zero point of 0, since the range of + // values is large, and because zero point costs extra cycles during + // processing. + TfLiteTensor result = + CreateQuantizedTensor(quantized, dims, bias_scale, 0, is_variable); + return result; +} // Creates bias tensor with input data, and per-channel weights determined by // input scale multiplied by weight scale for each channel. Input data will not @@ -315,9 +312,6 @@ TfLiteTensor CreatePerChannelQuantizedBiasTensor( for (int i = 0; i < num_channels; i++) { scales->data[i] = input_scale * weight_scales->data[i]; zero_points->data[i] = 0; - MicroPrintf("index %d scales %f zero_point %d input scale %f weight %f", i, - (double)scales->data[i], zero_points->data[i], - (double)input_scale, (double)weight_scales->data[i]); } affine_quant->scale = scales; @@ -346,9 +340,6 @@ TfLiteTensor CreatePerChannelQuantizedBiasTensor( for (int i = 0; i < num_channels; i++) { scales_array[i] = input_scale * weight_scales[i]; zero_points[i + 1] = 0; - MicroPrintf("index %d scales %f zero_point %d input scale %f weight %f", i, - (double)scales_array[i], zero_points[i + 1], - (double)input_scale, (double)weight_scales[i]); } SymmetricPerChannelQuantize(input, quantized, input_size, num_channels, @@ -360,9 +351,7 @@ TfLiteTensor CreatePerChannelQuantizedBiasTensor( TfLiteTensor result = CreateTensor(quantized, dims, is_variable); result.quantization = {kTfLiteAffineQuantization, affine_quant}; - int64_t data0 = quantized[0]; - MicroPrintf("quantp %p data %f data quantized %lld", affine_quant, - (double)input[0], data0); + return result; }