Skip to content

Commit

Permalink
Cleanup TODOs.
Browse files Browse the repository at this point in the history
Remove unit test debug output.
Support no bias tensor for FULLY_CONNECTED compressed unit tests.
More code cleanup.
  • Loading branch information
ddavis-2015 committed Aug 8, 2024
1 parent eb156ca commit 5e49517
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 151 deletions.
1 change: 0 additions & 1 deletion tensorflow/lite/micro/kernels/conv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
13 changes: 2 additions & 11 deletions tensorflow/lite/micro/kernels/conv_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -187,7 +187,7 @@ TfLiteStatus TestConvQuantizedPerChannelCompressed(
TIO* output_quantized, float output_scale, int output_zero_point,
const TfLiteConvParams* conv_params, TFLMRegistration registration,
const TestCompressionQuantizedInfo<CTB>* 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);
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion tensorflow/lite/micro/kernels/fully_connected.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
57 changes: 28 additions & 29 deletions tensorflow/lite/micro/kernels/fully_connected_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -440,8 +442,6 @@ TfLiteStatus TestFullyConnectedQuantizedCompressed(
TIO* output_quantized, float output_scale, int output_zero_point,
const TfLiteFusedActivation activation,
const TestCompressionQuantizedInfo<CTB>* 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);
Expand All @@ -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<CTB>());
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<CTB>());
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,
Expand Down
2 changes: 0 additions & 2 deletions tensorflow/lite/micro/kernels/kernel_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
const T* GetTensorData(MicroContext* micro_context,
const TfLiteEvalTensor* tensor,
Expand Down
1 change: 0 additions & 1 deletion tensorflow/lite/micro/kernels/transpose_conv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
13 changes: 2 additions & 11 deletions tensorflow/lite/micro/kernels/transpose_conv_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<CTB>* 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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion tensorflow/lite/micro/micro_allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<void*, compression_scheme>.
auto compression_metadata =
tflite::micro::compression::GetSizePrefixedMetadata(vp);
flatbuffers::Verifier verifier(vp->data(), vp->size(),
Expand Down
2 changes: 1 addition & 1 deletion tensorflow/lite/micro/recording_micro_allocator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
68 changes: 0 additions & 68 deletions tensorflow/lite/micro/test_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2021,74 +2021,6 @@ TfLiteFloatArray* FloatArrayFromFloats(const float* floats) {
return reinterpret_cast<TfLiteFloatArray*>(const_cast<float*>(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<int32_t>(
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<std::int64_t>(
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,
Expand Down
39 changes: 14 additions & 25 deletions tensorflow/lite/micro/test_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
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
Expand All @@ -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;
Expand Down Expand Up @@ -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<T>(input, quantized, input_size, num_channels,
Expand All @@ -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;
}

Expand Down

0 comments on commit 5e49517

Please sign in to comment.