Skip to content

Commit f98a42d

Browse files
Xharktensorflower-gardener
authored andcommitted
Enable per tensor quantized weight for per channel int8 transpose conv ops.
PiperOrigin-RevId: 353147555 Change-Id: I07b2b7f9aa1580ff5e9ac20dbe06bfa61e36171d
1 parent 9045dca commit f98a42d

2 files changed

Lines changed: 38 additions & 6 deletions

File tree

‎tensorflow/lite/kernels/transpose_conv.cc‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ struct OpData {
8080
int output_shift;
8181

8282
// Per channel output multiplier and shift.
83-
// TODO(b/144846950): Add channel dimension index for the kernel to be more
84-
// flexible.
8583
std::vector<int32_t> per_channel_output_multiplier;
8684
std::vector<int32_t> per_channel_output_shift;
8785

@@ -374,17 +372,20 @@ TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
374372
const auto* affine_quantization =
375373
reinterpret_cast<TfLiteAffineQuantization*>(
376374
weights->quantization.params);
375+
const int channels_out = weights->dims->data[0];
377376
TF_LITE_ENSURE(context, affine_quantization);
378377
TF_LITE_ENSURE(context, affine_quantization->scale);
379-
const int number_channel = affine_quantization->scale->size;
380-
data->per_channel_output_multiplier.resize(number_channel);
381-
data->per_channel_output_shift.resize(number_channel);
378+
TF_LITE_ENSURE(context, (affine_quantization->scale->size == 1 ||
379+
affine_quantization->scale->size == channels_out));
380+
381+
data->per_channel_output_multiplier.resize(channels_out);
382+
data->per_channel_output_shift.resize(channels_out);
382383
TF_LITE_ENSURE_STATUS(tflite::PopulateConvolutionQuantizationParams(
383384
context, input, weights, bias, output, kTfLiteActNone,
384385
&data->output_multiplier, &data->output_shift,
385386
&data->output_activation_min, &data->output_activation_max,
386387
data->per_channel_output_multiplier.data(),
387-
data->per_channel_output_shift.data()));
388+
data->per_channel_output_shift.data(), channels_out));
388389
}
389390

390391
return kTfLiteOk;

‎tensorflow/lite/kernels/transpose_conv_test.cc‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,37 @@ TEST_P(TransposeConvOpTest, TestQuantizedPerChannelMultiChannel) {
462462
EXPECT_THAT(model.GetOutputShape(), ElementsAreArray({1, 5, 5, 2}));
463463
}
464464

465+
// Test data copied from the float multi-channel test above.
466+
TEST_P(TransposeConvOpTest, TestQuantizedPerTensorMultiChannel) {
467+
const std::initializer_list<float> filter_data = {
468+
1, 3, 5, 7, 9, 11, 13, 15, 17, 2, 4, 6, 8, 10, 12, 14, 16, 18};
469+
const std::initializer_list<int8_t> const_filter_data = {
470+
7, 21, 35, 49, 64, 78, 92, 106, 120,
471+
14, 28, 42, 56, 71, 85, 99, 113, 127};
472+
PerChannelQuantizedTransposeConvOpModel model(
473+
GetRegistration(), {1, 5, 5, 2},
474+
{TensorType_INT8, {2, 3, 3, 1}, 0, 0, 0, 0, true, {18.0 / 127}, {0}, 0},
475+
const_filter_data, {TensorType_INT8, {1, 2, 2, 1}, 0, 0, 4.0 / 255, -128},
476+
{TensorType_INT8, {}, 0, 0, 1, -128}, Padding_VALID, 2, 2, GetTestType(),
477+
/* version */ 2);
478+
model.SetInput({1, 2, 3, 4});
479+
if (GetTestType() == TestType::kDynamic) {
480+
model.SetFilter(filter_data);
481+
}
482+
model.Invoke();
483+
484+
EXPECT_THAT(
485+
model.GetDequantizedOutput(),
486+
ElementsAreArray(ArrayFloatNear(
487+
{1, 2, 3, 4, 7, 10, 6, 8, 10, 12, 7, 8, 9, 10, 25, 28, 18,
488+
20, 22, 24, 16, 20, 24, 28, 62, 72, 42, 48, 54, 60, 21, 24, 27, 30,
489+
61, 68, 36, 40, 44, 48, 39, 42, 45, 48, 103, 110, 60, 64, 68, 72},
490+
1e-5)));
491+
492+
// GetOutputShape() should always be same as model.SetOutputShape(...);
493+
EXPECT_THAT(model.GetOutputShape(), ElementsAreArray({1, 5, 5, 2}));
494+
}
495+
465496
class PerChannelQuantizedTransposeConvOpModel16x8
466497
: public BaseTransposeConvOpModel<int16_t> {
467498
public:

0 commit comments

Comments
 (0)