From 54cfe0f6ffc5bc427b9eb2e8a1df456a7f070636 Mon Sep 17 00:00:00 2001 From: Artsiom Ablavatski Date: Tue, 21 May 2024 09:09:37 -0700 Subject: [PATCH] Align DRQ TransposeConv XNNPack test with reference TFLite implementation PiperOrigin-RevId: 635826787 --- ...namically_quantized_transpose_conv_test.cc | 44 +---- ...mically_quantized_transpose_conv_tester.cc | 173 ++---------------- ...amically_quantized_transpose_conv_tester.h | 6 +- 3 files changed, 22 insertions(+), 201 deletions(-) diff --git a/tensorflow/lite/delegates/xnnpack/dynamically_quantized_transpose_conv_test.cc b/tensorflow/lite/delegates/xnnpack/dynamically_quantized_transpose_conv_test.cc index 3b83541b81b215..de863e4f1e2125 100644 --- a/tensorflow/lite/delegates/xnnpack/dynamically_quantized_transpose_conv_test.cc +++ b/tensorflow/lite/delegates/xnnpack/dynamically_quantized_transpose_conv_test.cc @@ -27,11 +27,8 @@ namespace tflite { namespace xnnpack { TEST(DynamicallyQuantizedTransposeConvTest, 2x2Stride2) { - auto delegate_options = TfLiteXNNPackDelegateOptionsDefault(); - delegate_options.flags |= - TFLITE_XNNPACK_DELEGATE_FLAG_ENABLE_SUBGRAPH_RESHAPING; std::unique_ptr - xnnpack_delegate(TfLiteXNNPackDelegateCreate(&delegate_options), + xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr), TfLiteXNNPackDelegateDelete); std::random_device random_device; @@ -55,11 +52,8 @@ TEST(DynamicallyQuantizedTransposeConvTest, 2x2Stride2) { } TEST(DynamicallyQuantizedTransposeConvTest, 3x3Stride2) { - auto delegate_options = TfLiteXNNPackDelegateOptionsDefault(); - delegate_options.flags |= - TFLITE_XNNPACK_DELEGATE_FLAG_ENABLE_SUBGRAPH_RESHAPING; std::unique_ptr - xnnpack_delegate(TfLiteXNNPackDelegateCreate(&delegate_options), + xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr), TfLiteXNNPackDelegateDelete); std::random_device random_device; auto rng = std::mt19937(random_device()); @@ -82,11 +76,8 @@ TEST(DynamicallyQuantizedTransposeConvTest, 3x3Stride2) { } TEST(DynamicallyQuantizedTransposeConvTest, 4x4Stride2) { - auto delegate_options = TfLiteXNNPackDelegateOptionsDefault(); - delegate_options.flags |= - TFLITE_XNNPACK_DELEGATE_FLAG_ENABLE_SUBGRAPH_RESHAPING; std::unique_ptr - xnnpack_delegate(TfLiteXNNPackDelegateCreate(&delegate_options), + xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr), TfLiteXNNPackDelegateDelete); std::random_device random_device; @@ -110,11 +101,8 @@ TEST(DynamicallyQuantizedTransposeConvTest, 4x4Stride2) { } TEST(DynamicallyQuantizedTransposeConvTest, 4x4Stride4) { - auto delegate_options = TfLiteXNNPackDelegateOptionsDefault(); - delegate_options.flags |= - TFLITE_XNNPACK_DELEGATE_FLAG_ENABLE_SUBGRAPH_RESHAPING; std::unique_ptr - xnnpack_delegate(TfLiteXNNPackDelegateCreate(&delegate_options), + xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr), TfLiteXNNPackDelegateDelete); std::random_device random_device; @@ -138,11 +126,8 @@ TEST(DynamicallyQuantizedTransposeConvTest, 4x4Stride4) { } TEST(DynamicallyQuantizedTransposeConvTest, SmallKernelWithSamePadding) { - auto delegate_options = TfLiteXNNPackDelegateOptionsDefault(); - delegate_options.flags |= - TFLITE_XNNPACK_DELEGATE_FLAG_ENABLE_SUBGRAPH_RESHAPING; std::unique_ptr - xnnpack_delegate(TfLiteXNNPackDelegateCreate(&delegate_options), + xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr), TfLiteXNNPackDelegateDelete); std::random_device random_device; @@ -169,11 +154,8 @@ TEST(DynamicallyQuantizedTransposeConvTest, SmallKernelWithSamePadding) { } TEST(DynamicallyQuantizedTransposeConvTest, SmallKernelWithValidPadding) { - auto delegate_options = TfLiteXNNPackDelegateOptionsDefault(); - delegate_options.flags |= - TFLITE_XNNPACK_DELEGATE_FLAG_ENABLE_SUBGRAPH_RESHAPING; std::unique_ptr - xnnpack_delegate(TfLiteXNNPackDelegateCreate(&delegate_options), + xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr), TfLiteXNNPackDelegateDelete); std::random_device random_device; auto rng = std::mt19937(random_device()); @@ -199,11 +181,8 @@ TEST(DynamicallyQuantizedTransposeConvTest, SmallKernelWithValidPadding) { } TEST(DynamicallyQuantizedTransposeConvTest, StrideWithSamePadding) { - auto delegate_options = TfLiteXNNPackDelegateOptionsDefault(); - delegate_options.flags |= - TFLITE_XNNPACK_DELEGATE_FLAG_ENABLE_SUBGRAPH_RESHAPING; std::unique_ptr - xnnpack_delegate(TfLiteXNNPackDelegateCreate(&delegate_options), + xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr), TfLiteXNNPackDelegateDelete); std::random_device random_device; @@ -234,11 +213,8 @@ TEST(DynamicallyQuantizedTransposeConvTest, StrideWithSamePadding) { } TEST(DynamicallyQuantizedTransposeConvTest, StrideWithValidPadding) { - auto delegate_options = TfLiteXNNPackDelegateOptionsDefault(); - delegate_options.flags |= - TFLITE_XNNPACK_DELEGATE_FLAG_ENABLE_SUBGRAPH_RESHAPING; std::unique_ptr - xnnpack_delegate(TfLiteXNNPackDelegateCreate(&delegate_options), + xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr), TfLiteXNNPackDelegateDelete); std::random_device random_device; @@ -272,8 +248,6 @@ TEST(DynamicallyQuantizedTransposeConvTest, MultiThreading) { TfLiteXNNPackDelegateOptions delegate_options = TfLiteXNNPackDelegateOptionsDefault(); delegate_options.num_threads = 2; - delegate_options.flags |= - TFLITE_XNNPACK_DELEGATE_FLAG_ENABLE_SUBGRAPH_RESHAPING; std::unique_ptr xnnpack_delegate(TfLiteXNNPackDelegateCreate(&delegate_options), TfLiteXNNPackDelegateDelete); @@ -308,8 +282,6 @@ TEST(DynamicallyQuantizedTransposeConvTest, MultiThreading) { TEST(DynamicallyQuantizedTransposeConvTest, WeightsCache) { TfLiteXNNPackDelegateOptions delegate_options = TfLiteXNNPackDelegateOptionsDefault(); - delegate_options.flags |= - TFLITE_XNNPACK_DELEGATE_FLAG_ENABLE_SUBGRAPH_RESHAPING; std::unique_ptr weights_cache(TfLiteXNNPackDelegateWeightsCacheCreate(), diff --git a/tensorflow/lite/delegates/xnnpack/dynamically_quantized_transpose_conv_tester.cc b/tensorflow/lite/delegates/xnnpack/dynamically_quantized_transpose_conv_tester.cc index bbc7110920ae08..35c1b590c7396a 100644 --- a/tensorflow/lite/delegates/xnnpack/dynamically_quantized_transpose_conv_tester.cc +++ b/tensorflow/lite/delegates/xnnpack/dynamically_quantized_transpose_conv_tester.cc @@ -49,23 +49,19 @@ void DynamicallyQuantizedTransposeConvTester::Test( const std::vector kernel_data = GenerateKernelData(); const std::vector bias_data = GenerateBiasData(); const std::vector kernel_scale_data = GenerateKernelScaleData(); - std::vector drq_buffer = - CreateDRQTfLiteModel(kernel_data, bias_data, kernel_scale_data); - std::vector dequantize_buffer = - CreateDequantizeTfLiteModel(kernel_data, bias_data, kernel_scale_data); - const Model* drq_model = GetModel(drq_buffer.data()); - const Model* dequantize_model = GetModel(dequantize_buffer.data()); + std::vector buffer = + CreateTfLiteModel(kernel_data, bias_data, kernel_scale_data); + const Model* model = GetModel(buffer.data()); std::unique_ptr delegate_interpreter; - ASSERT_EQ( - InterpreterBuilder( - drq_model, ::tflite::ops::builtin::BuiltinOpResolverWithXNNPACK())( - &delegate_interpreter), - kTfLiteOk); + ASSERT_EQ(InterpreterBuilder( + model, ::tflite::ops::builtin::BuiltinOpResolverWithXNNPACK())( + &delegate_interpreter), + kTfLiteOk); std::unique_ptr default_interpreter; ASSERT_EQ( InterpreterBuilder( - dequantize_model, + model, ::tflite::ops::builtin::BuiltinOpResolverWithoutDefaultDelegates())( &default_interpreter), kTfLiteOk); @@ -79,11 +75,11 @@ void DynamicallyQuantizedTransposeConvTester::Test( ASSERT_EQ(delegate_interpreter->outputs().size(), 1); ASSERT_EQ(default_interpreter->outputs().size(), 1); - ASSERT_EQ(delegate_interpreter->ModifyGraphWithDelegate(delegate), kTfLiteOk); - ASSERT_EQ(delegate_interpreter->AllocateTensors(), kTfLiteOk); ASSERT_EQ(default_interpreter->AllocateTensors(), kTfLiteOk); + ASSERT_EQ(delegate_interpreter->ModifyGraphWithDelegate(delegate), kTfLiteOk); + if (weights_cache_ != nullptr) { TfLiteXNNPackDelegateWeightsCacheFinalizeHard(weights_cache_); } @@ -110,12 +106,11 @@ void DynamicallyQuantizedTransposeConvTester::Test( int different_output_values = 0; for (size_t i = 0; i < num_output_values; i++) { if (std::abs(default_output_data[i] - xnnpack_output_data[i]) > - 0.1 * std::abs(default_output_data[i])) { + 0.005 * std::abs(default_output_data[i])) { ++different_output_values; } } - - if (different_output_values > 0.055 * num_output_values) { + if (different_output_values > 0.05 * num_output_values) { GTEST_FAIL() << (float)different_output_values / num_output_values * 100.f << "% of output values differ"; } @@ -182,7 +177,7 @@ DynamicallyQuantizedTransposeConvTester::GenerateKernelScaleData() const { return kernel_scale; } -std::vector DynamicallyQuantizedTransposeConvTester::CreateDRQTfLiteModel( +std::vector DynamicallyQuantizedTransposeConvTester::CreateTfLiteModel( const std::vector& filter_data, const std::vector& bias_data, const std::vector& kernel_scale) const { /*************************** Define operator codes **************************/ @@ -297,147 +292,5 @@ std::vector DynamicallyQuantizedTransposeConvTester::CreateDRQTfLiteModel( builder.GetBufferPointer() + builder.GetSize()); } -std::vector -DynamicallyQuantizedTransposeConvTester::CreateDequantizeTfLiteModel( - const std::vector& filter_data, const std::vector& bias_data, - const std::vector& kernel_scale) const { - /*************************** Define operator codes **************************/ - flatbuffers::FlatBufferBuilder builder; - std::vector> operator_codes{ - {CreateOperatorCode(builder, BuiltinOperator_TRANSPOSE_CONV)}}; - const int dequantize_operator_code = operator_codes.size(); - operator_codes.emplace_back( - CreateOperatorCode(builder, BuiltinOperator_DEQUANTIZE)); - - /****************************** Define buffers ******************************/ - std::vector> buffers{ - {CreateBuffer(builder, builder.CreateVector({}))}}; - - int filter_buffer_id = 0; - const int quantized_filter_buffer_id = buffers.size(); - buffers.emplace_back(CreateBuffer( - builder, - builder.CreateVector(reinterpret_cast(filter_data.data()), - sizeof(int8_t) * filter_data.size()))); - - int bias_buffer_id = buffers.size(); - buffers.emplace_back(CreateBuffer( - builder, - builder.CreateVector(reinterpret_cast(bias_data.data()), - sizeof(float) * bias_data.size()))); - const std::array output_shape{ - {BatchSize(), OutputHeight(), OutputWidth(), OutputChannels()}}; - const int output_shape_buffer_id = buffers.size(); - buffers.emplace_back(CreateBuffer( - builder, builder.CreateVector( - reinterpret_cast(output_shape.data()), - sizeof(int32_t) * output_shape.size()))); - - /****************************** Define tensors ******************************/ - const std::vector filter_shape = {OutputChannels(), KernelHeight(), - KernelWidth(), InputChannels()}; - const std::vector bias_shape = {OutputChannels()}; - const std::array input_shape{ - {BatchSize(), InputHeight(), InputWidth(), InputChannels()}}; - - std::vector> tensors; - const int quantized_filter_tensor_id = tensors.size(); - tensors.emplace_back(CreateTensor( - builder, - builder.CreateVector(filter_shape.data(), filter_shape.size()), - /*type=*/TensorType_INT8, - /*buffer=*/quantized_filter_buffer_id, - /*name=*/0, - CreateQuantizationParameters( - builder, /*min=*/0, /*max=*/0, - builder.CreateVector(kernel_scale), - builder.CreateVector( - std::vector(OutputChannels(), 0))))); - - const int input_tensor_id = tensors.size(); - tensors.emplace_back(CreateTensor( - builder, - builder.CreateVector(input_shape.data(), input_shape.size()), - TensorType_FLOAT32)); - - const int filter_tensor_id = tensors.size(); - tensors.emplace_back(CreateTensor( - builder, - builder.CreateVector(filter_shape.data(), filter_shape.size()), - TensorType_FLOAT32, - /*buffer=*/filter_buffer_id)); - - const int bias_tensor_id = tensors.size(); - tensors.emplace_back(CreateTensor( - builder, - builder.CreateVector(bias_shape.data(), bias_shape.size()), - TensorType_FLOAT32, bias_buffer_id)); - - const int output_tensor_id = tensors.size(); - tensors.emplace_back(CreateTensor( - builder, - builder.CreateVector(output_shape.data(), output_shape.size()), - TensorType_FLOAT32)); - - const int output_shape_tensor_id = tensors.size(); - const std::array output_shape_shape{{4}}; - tensors.emplace_back( - CreateTensor(builder, - builder.CreateVector(output_shape_shape.data(), - output_shape_shape.size()), - TensorType_INT32, output_shape_buffer_id)); - - /***************************** Define operators *****************************/ - std::vector> operators; - - const std::array dequantize_filter_inputs{ - {quantized_filter_tensor_id}}; - const std::array dequantize_filter_outputs{{filter_tensor_id}}; - operators.emplace_back(CreateOperator( - builder, /*opcode_index=*/dequantize_operator_code, - builder.CreateVector(dequantize_filter_inputs.data(), - dequantize_filter_inputs.size()), - builder.CreateVector(dequantize_filter_outputs.data(), - dequantize_filter_outputs.size()))); - - std::vector op_inputs{ - {output_shape_tensor_id, filter_tensor_id, input_tensor_id}}; - op_inputs.push_back(bias_tensor_id); - const std::array op_outputs{{output_tensor_id}}; - const flatbuffers::Offset transpose_conv_options = - CreateTransposeConvOptions(builder, Padding(), StrideWidth(), - StrideHeight()); - operators.emplace_back(CreateOperator( - builder, /*opcode_index=*/0, - builder.CreateVector(op_inputs.data(), op_inputs.size()), - builder.CreateVector(op_outputs.data(), op_outputs.size()), - BuiltinOptions_TransposeConvOptions, transpose_conv_options.Union())); - - /****************************** Define subgraph *****************************/ - const std::array subgraph_inputs{{input_tensor_id}}; - const std::array subgraph_outputs{{output_tensor_id}}; - const flatbuffers::Offset subgraph = CreateSubGraph( - builder, builder.CreateVector(tensors.data(), tensors.size()), - builder.CreateVector(subgraph_inputs.data(), - subgraph_inputs.size()), - builder.CreateVector(subgraph_outputs.data(), - subgraph_outputs.size()), - builder.CreateVector(operators.data(), operators.size())); - - const flatbuffers::Offset description = - builder.CreateString("TransposeConv model"); - - const flatbuffers::Offset model_buffer = CreateModel( - builder, TFLITE_SCHEMA_VERSION, - builder.CreateVector(operator_codes.data(), operator_codes.size()), - builder.CreateVector(&subgraph, 1), description, - builder.CreateVector(buffers.data(), buffers.size())); - - builder.Finish(model_buffer); - - return std::vector(builder.GetBufferPointer(), - builder.GetBufferPointer() + builder.GetSize()); -} - } // namespace xnnpack } // namespace tflite diff --git a/tensorflow/lite/delegates/xnnpack/dynamically_quantized_transpose_conv_tester.h b/tensorflow/lite/delegates/xnnpack/dynamically_quantized_transpose_conv_tester.h index e7dc7cd9536608..5c69808c836b33 100644 --- a/tensorflow/lite/delegates/xnnpack/dynamically_quantized_transpose_conv_tester.h +++ b/tensorflow/lite/delegates/xnnpack/dynamically_quantized_transpose_conv_tester.h @@ -183,11 +183,7 @@ class DynamicallyQuantizedTransposeConvTester { std::vector GenerateKernelData() const; std::vector GenerateBiasData() const; std::vector GenerateKernelScaleData() const; - std::vector CreateDRQTfLiteModel( - const std::vector& filter_data, - const std::vector& bias_data, - const std::vector& kernel_scale) const; - std::vector CreateDequantizeTfLiteModel( + std::vector CreateTfLiteModel( const std::vector& filter_data, const std::vector& bias_data, const std::vector& kernel_scale) const;