Skip to content

Commit ad8d238

Browse files
authored
Automated sync from github.com/tensorflow/tensorflow (#2221)
BUG=automated sync from upstream NO_CHECK_TFLITE_FILES=automated sync from upstream
1 parent 77e2cdb commit ad8d238

File tree

12 files changed

+178
-31
lines changed

12 files changed

+178
-31
lines changed

codegen/examples/hello_world/hello_world_model.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ struct Node0_0 {
122122
.activation = kTfLiteActRelu,
123123
.weights_format = kTfLiteFullyConnectedWeightsFormatDefault,
124124
.keep_num_dims = false,
125-
.asymmetric_quantize_inputs = false};
125+
.asymmetric_quantize_inputs = false,
126+
.quantized_bias_type = kTfLiteNoType};
126127
} node_0_0;
127128

128129
struct Node0_1 {
@@ -139,7 +140,8 @@ struct Node0_1 {
139140
.activation = kTfLiteActRelu,
140141
.weights_format = kTfLiteFullyConnectedWeightsFormatDefault,
141142
.keep_num_dims = false,
142-
.asymmetric_quantize_inputs = false};
143+
.asymmetric_quantize_inputs = false,
144+
.quantized_bias_type = kTfLiteNoType};
143145
} node_0_1;
144146

145147
struct Node0_2 {
@@ -156,7 +158,8 @@ struct Node0_2 {
156158
.activation = kTfLiteActNone,
157159
.weights_format = kTfLiteFullyConnectedWeightsFormatDefault,
158160
.keep_num_dims = false,
159-
.asymmetric_quantize_inputs = false};
161+
.asymmetric_quantize_inputs = false,
162+
.quantized_bias_type = kTfLiteNoType};
160163
} node_0_2;
161164

162165
struct Tensor0_0Dims {

codegen/operators/constants.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,25 @@
2626
schema_fb.ActivationFunctionType.TANH: "kTfLiteActTanh",
2727
schema_fb.ActivationFunctionType.SIGN_BIT: "kTfLiteActSignBit",
2828
}
29+
30+
TFLITE_TYPE: Dict[int, str] = {
31+
0: "kTfLiteNoType",
32+
1: "kTfLiteFloat32",
33+
2: "kTfLiteInt32",
34+
3: "kTfLiteUInt8",
35+
4: "kTfLiteInt64",
36+
5: "kTfLiteString",
37+
6: "kTfLiteBool",
38+
7: "kTfLiteInt16",
39+
8: "kTfLiteComplex64",
40+
9: "kTfLiteInt8",
41+
10: "kTfLiteFloat16",
42+
11: "kTfLiteFloat64",
43+
12: "kTfLiteComplex128",
44+
13: "kTfLiteUInt64",
45+
14: "kTfLiteResource",
46+
15: "kTfLiteVariant",
47+
16: "kTfLiteUInt32",
48+
17: "kTfLiteUInt16",
49+
18: "kTfLiteInt4",
50+
}

codegen/operators/fully_connected.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ def generate_c_builtin_data(self) -> str:
4343
" .activation = ${activation},\n"
4444
" .weights_format = ${weights_format},\n"
4545
" .keep_num_dims = ${keep_num_dims},\n"
46-
" .asymmetric_quantize_inputs = ${asymmetric_quantize_inputs}};")
46+
" .asymmetric_quantize_inputs = ${asymmetric_quantize_inputs},\n"
47+
" .quantized_bias_type = ${quantized_bias_type}};")
4748
return builtin_template.substitute(
4849
activation=constants.ACTIVATION_FUNCS[
4950
self._builtin_options.fusedActivationFunction],
5051
weights_format=_WEIGHTS_FORMATS[self._builtin_options.weightsFormat],
5152
keep_num_dims=utils.bool_to_c_str(self._builtin_options.keepNumDims),
5253
asymmetric_quantize_inputs=utils.bool_to_c_str(
53-
self._builtin_options.asymmetricQuantizeInputs))
54+
self._builtin_options.asymmetricQuantizeInputs),
55+
quantized_bias_type=constants.TFLITE_TYPE[
56+
self._builtin_options.quantizedBiasType])

tensorflow/lite/core/api/flatbuffer_conversions.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,9 @@ TfLiteStatus ParseConv2D(const Operator* op, ErrorReporter* error_reporter,
13051305

13061306
params->dilation_width_factor = schema_params->dilation_w_factor();
13071307
params->dilation_height_factor = schema_params->dilation_h_factor();
1308+
TF_LITE_ENSURE_STATUS(
1309+
ConvertTensorType(schema_params->quantized_bias_type(),
1310+
&params->quantized_bias_type, error_reporter));
13081311
} else {
13091312
// TODO(b/157480169): We should either return kTfLiteError or fill in some
13101313
// reasonable defaults in the params struct. We are not doing so until we
@@ -1519,7 +1522,9 @@ TfLiteStatus ParseFullyConnected(const Operator* op,
15191522
params->keep_num_dims = schema_params->keep_num_dims();
15201523
params->asymmetric_quantize_inputs =
15211524
schema_params->asymmetric_quantize_inputs();
1522-
1525+
TF_LITE_ENSURE_STATUS(
1526+
ConvertTensorType(schema_params->quantized_bias_type(),
1527+
&params->quantized_bias_type, error_reporter));
15231528
switch (schema_params->weights_format()) {
15241529
case FullyConnectedOptionsWeightsFormat_DEFAULT:
15251530
params->weights_format = kTfLiteFullyConnectedWeightsFormatDefault;
@@ -2450,6 +2455,9 @@ TfLiteStatus ParseTransposeConv(const Operator* op,
24502455

24512456
params->activation =
24522457
ConvertActivation(transpose_conv_params->fused_activation_function());
2458+
TF_LITE_ENSURE_STATUS(
2459+
ConvertTensorType(transpose_conv_params->quantized_bias_type(),
2460+
&params->quantized_bias_type, error_reporter));
24532461
} else {
24542462
// TODO(b/157480169): We should either return kTfLiteError or fill in some
24552463
// reasonable defaults in the params struct. We are not doing so until we

tensorflow/lite/core/c/builtin_op_data.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ typedef struct {
9191
// Note: Version 2 supports dilation values not equal to 1.
9292
int dilation_width_factor;
9393
int dilation_height_factor;
94+
95+
// Parameters for CONV_2D version 7 or above.
96+
// Used to determine the default value for the quantized bias.
97+
TfLiteType quantized_bias_type;
9498
} TfLiteConvParams;
9599

96100
typedef struct {
@@ -194,6 +198,10 @@ typedef struct {
194198
// If set to true and the weights are quantized, then non constant inputs
195199
// are quantized at evaluation time with asymmetric quantization.
196200
bool asymmetric_quantize_inputs;
201+
202+
// Parameters for FullyConnected version 10 or above.
203+
// Used to determine the default value for the quantized bias.
204+
TfLiteType quantized_bias_type;
197205
} TfLiteFullyConnectedParams;
198206

199207
typedef enum {
@@ -431,6 +439,10 @@ typedef struct {
431439

432440
// Parameters supported by version 4:
433441
TfLiteFusedActivation activation;
442+
443+
// Parameters for TransposeConv version 5 or above.
444+
// Used to determine the default value for the quantized bias.
445+
TfLiteType quantized_bias_type;
434446
} TfLiteTransposeConvParams;
435447

436448
typedef struct {

tensorflow/lite/micro/kernels/conv_test.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ static TfLiteConvParams common_conv_params = {
5353
kTfLiteActNone, // activation
5454
1, // dilation_width_factor
5555
1, // dilation_height_factor
56+
kTfLiteNoType // quantized_bias_type
5657
};
5758

5859
} // namespace
@@ -420,8 +421,8 @@ TF_LITE_MICRO_TEST(SimpleTestQuantized16x8PerChannelRelu632bBias) {
420421
TF_LITE_MICRO_TEST(Kernel1x1QuantizedPerChannel) {
421422
// conv params:
422423
// padding, stride_<width,height>, activation, dilation_<width, height>
423-
TfLiteConvParams conv_params = {kTfLitePaddingValid, 1, 1,
424-
kTfLiteActNone, 1, 1};
424+
TfLiteConvParams conv_params = {
425+
kTfLitePaddingValid, 1, 1, kTfLiteActNone, 1, 1, kTfLiteNoType};
425426

426427
int input_shape[] = {4, 1, 2, 2, 4}; // [len,N,H,W,C]
427428
constexpr int input_elements =
@@ -473,8 +474,8 @@ TF_LITE_MICRO_TEST(Kernel1x1QuantizedPerChannel) {
473474
TF_LITE_MICRO_TEST(Kernel1x1QuantizedPerChannelRelu6) {
474475
// conv params:
475476
// padding, stride_<width,height>, activation, dilation_<width, height>
476-
TfLiteConvParams conv_params = {kTfLitePaddingValid, 1, 1,
477-
kTfLiteActRelu6, 1, 1};
477+
TfLiteConvParams conv_params = {
478+
kTfLitePaddingValid, 1, 1, kTfLiteActRelu6, 1, 1, kTfLiteNoType};
478479

479480
int input_shape[] = {4, 1, 2, 2, 4}; // [len,N,H,W,C]
480481
constexpr int input_elements =
@@ -526,8 +527,8 @@ TF_LITE_MICRO_TEST(Kernel1x1QuantizedPerChannelRelu6) {
526527
TF_LITE_MICRO_TEST(Kernel1x1Quantized16x8PerChannelRelu6) {
527528
// conv params:
528529
// padding, stride_<width,height>, activation, dilation_<width, height>
529-
TfLiteConvParams conv_params = {kTfLitePaddingValid, 1, 1,
530-
kTfLiteActRelu6, 1, 1};
530+
TfLiteConvParams conv_params = {
531+
kTfLitePaddingValid, 1, 1, kTfLiteActRelu6, 1, 1, kTfLiteNoType};
531532

532533
int input_shape[] = {4, 1, 2, 2, 4}; // [len,N,H,W,C]
533534
const int input_elements = 1 * 2 * 2 * 4;

tensorflow/lite/micro/kernels/fully_connected_test.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ TfLiteStatus ValidateFullyConnectedGoldens(
247247
const TfLiteFusedActivation activation, const float tolerance,
248248
const int output_len, const T* golden, T* output_data) {
249249
TfLiteFullyConnectedParams builtin_data = {
250-
activation, kTfLiteFullyConnectedWeightsFormatDefault, false, false};
250+
activation, kTfLiteFullyConnectedWeightsFormatDefault, false, false,
251+
kTfLiteNoType};
251252

252253
// Avoid variable length array warning.
253254
constexpr int inputs_array_len = 4;

tensorflow/lite/micro/kernels/transpose_conv_test.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ static TfLiteConvParams common_conv_params = {kTfLitePaddingSame, // padding
5353
1, // stride_height
5454
kTfLiteActNone,
5555
1,
56-
1};
56+
1,
57+
kTfLiteNoType};
5758

5859
template <typename T>
5960
TfLiteStatus InvokeTransposeConv(TfLiteTensor* tensors, int tensors_size,
@@ -253,7 +254,8 @@ TF_LITE_MICRO_TEST(fusedRELUTest) {
253254
1, // stride_height
254255
kTfLiteActRelu,
255256
1,
256-
1};
257+
1,
258+
kTfLiteNoType};
257259

258260
TF_LITE_MICRO_EXPECT_EQ(
259261
kTfLiteOk, tflite::testing::TestTransposeConvFloat(
@@ -276,7 +278,8 @@ TF_LITE_MICRO_TEST(AccuracyWithFusedActivationTest) {
276278
3, // stride_height
277279
kTfLiteActRelu,
278280
1,
279-
1};
281+
1,
282+
kTfLiteNoType};
280283

281284
TF_LITE_MICRO_EXPECT_EQ(
282285
kTfLiteOk, tflite::testing::TestTransposeConvFloat(
@@ -304,7 +307,8 @@ TF_LITE_MICRO_TEST(MultiChannelBiasWithFusedActivationTest) {
304307
2, // stride_height
305308
kTfLiteActRelu,
306309
1,
307-
1};
310+
1,
311+
kTfLiteNoType};
308312

309313
TF_LITE_MICRO_EXPECT_EQ(
310314
kTfLiteOk,

tensorflow/lite/micro/memory_arena_threshold_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ constexpr int kTestConvModelOnlyTotalSize = 9488;
9797
// Tail size contributed by the conv model excluding the
9898
// RecordingMicroAllocator's overhead
9999
// TODO(b/207157610): replace magic number that depends on OPs
100-
constexpr int kTestConvModelOnlyTailSize = 1744;
100+
constexpr int kTestConvModelOnlyTailSize = 1816;
101101
constexpr int kTestConvModelPersistentTfLiteTensorDataSize = 128;
102102
constexpr int kTestConvModelPersistentBufferDataSize = 728;
103103
#else
@@ -108,7 +108,7 @@ constexpr int kTestConvModelOnlyTotalSize = 9760;
108108
// Tail size contributed by the conv model excluding the
109109
// RecordingMicroAllocator's overhead
110110
// TODO(b/207157610): replace magic number that depends on OPs
111-
constexpr int kTestConvModelOnlyTailSize = 2016;
111+
constexpr int kTestConvModelOnlyTailSize = 2088;
112112
constexpr int kTestConvModelPersistentTfLiteTensorDataSize = 224;
113113
constexpr int kTestConvModelPersistentBufferDataSize = 720;
114114
#endif

tensorflow/lite/python/schema_py_generated.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,7 +2640,14 @@ def DilationHFactor(self):
26402640
return self._tab.Get(flatbuffers.number_types.Int32Flags, o + self._tab.Pos)
26412641
return 1
26422642

2643-
def Conv2DOptionsStart(builder): builder.StartObject(6)
2643+
# Conv2DOptions
2644+
def QuantizedBiasType(self):
2645+
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(16))
2646+
if o != 0:
2647+
return self._tab.Get(flatbuffers.number_types.Int8Flags, o + self._tab.Pos)
2648+
return 0
2649+
2650+
def Conv2DOptionsStart(builder): builder.StartObject(7)
26442651
def Start(builder):
26452652
return Conv2DOptionsStart(builder)
26462653
def Conv2DOptionsAddPadding(builder, padding): builder.PrependInt8Slot(0, padding, 0)
@@ -2661,6 +2668,9 @@ def AddDilationWFactor(builder, dilationWFactor):
26612668
def Conv2DOptionsAddDilationHFactor(builder, dilationHFactor): builder.PrependInt32Slot(5, dilationHFactor, 1)
26622669
def AddDilationHFactor(builder, dilationHFactor):
26632670
return Conv2DOptionsAddDilationHFactor(builder, dilationHFactor)
2671+
def Conv2DOptionsAddQuantizedBiasType(builder, quantizedBiasType): builder.PrependInt8Slot(6, quantizedBiasType, 0)
2672+
def AddQuantizedBiasType(builder, quantizedBiasType):
2673+
return Conv2DOptionsAddQuantizedBiasType(builder, quantizedBiasType)
26642674
def Conv2DOptionsEnd(builder): return builder.EndObject()
26652675
def End(builder):
26662676
return Conv2DOptionsEnd(builder)
@@ -2675,6 +2685,7 @@ def __init__(self):
26752685
self.fusedActivationFunction = 0 # type: int
26762686
self.dilationWFactor = 1 # type: int
26772687
self.dilationHFactor = 1 # type: int
2688+
self.quantizedBiasType = 0 # type: int
26782689

26792690
@classmethod
26802691
def InitFromBuf(cls, buf, pos):
@@ -2698,6 +2709,7 @@ def _UnPack(self, conv2doptions):
26982709
self.fusedActivationFunction = conv2doptions.FusedActivationFunction()
26992710
self.dilationWFactor = conv2doptions.DilationWFactor()
27002711
self.dilationHFactor = conv2doptions.DilationHFactor()
2712+
self.quantizedBiasType = conv2doptions.QuantizedBiasType()
27012713

27022714
# Conv2DOptionsT
27032715
def Pack(self, builder):
@@ -2708,6 +2720,7 @@ def Pack(self, builder):
27082720
Conv2DOptionsAddFusedActivationFunction(builder, self.fusedActivationFunction)
27092721
Conv2DOptionsAddDilationWFactor(builder, self.dilationWFactor)
27102722
Conv2DOptionsAddDilationHFactor(builder, self.dilationHFactor)
2723+
Conv2DOptionsAddQuantizedBiasType(builder, self.quantizedBiasType)
27112724
conv2doptions = Conv2DOptionsEnd(builder)
27122725
return conv2doptions
27132726
# automatically generated by the FlatBuffers compiler, do not modify
@@ -4512,7 +4525,14 @@ def AsymmetricQuantizeInputs(self):
45124525
return bool(self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos))
45134526
return False
45144527

4515-
def FullyConnectedOptionsStart(builder): builder.StartObject(4)
4528+
# FullyConnectedOptions
4529+
def QuantizedBiasType(self):
4530+
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(12))
4531+
if o != 0:
4532+
return self._tab.Get(flatbuffers.number_types.Int8Flags, o + self._tab.Pos)
4533+
return 0
4534+
4535+
def FullyConnectedOptionsStart(builder): builder.StartObject(5)
45164536
def Start(builder):
45174537
return FullyConnectedOptionsStart(builder)
45184538
def FullyConnectedOptionsAddFusedActivationFunction(builder, fusedActivationFunction): builder.PrependInt8Slot(0, fusedActivationFunction, 0)
@@ -4527,6 +4547,9 @@ def AddKeepNumDims(builder, keepNumDims):
45274547
def FullyConnectedOptionsAddAsymmetricQuantizeInputs(builder, asymmetricQuantizeInputs): builder.PrependBoolSlot(3, asymmetricQuantizeInputs, 0)
45284548
def AddAsymmetricQuantizeInputs(builder, asymmetricQuantizeInputs):
45294549
return FullyConnectedOptionsAddAsymmetricQuantizeInputs(builder, asymmetricQuantizeInputs)
4550+
def FullyConnectedOptionsAddQuantizedBiasType(builder, quantizedBiasType): builder.PrependInt8Slot(4, quantizedBiasType, 0)
4551+
def AddQuantizedBiasType(builder, quantizedBiasType):
4552+
return FullyConnectedOptionsAddQuantizedBiasType(builder, quantizedBiasType)
45304553
def FullyConnectedOptionsEnd(builder): return builder.EndObject()
45314554
def End(builder):
45324555
return FullyConnectedOptionsEnd(builder)
@@ -4539,6 +4562,7 @@ def __init__(self):
45394562
self.weightsFormat = 0 # type: int
45404563
self.keepNumDims = False # type: bool
45414564
self.asymmetricQuantizeInputs = False # type: bool
4565+
self.quantizedBiasType = 0 # type: int
45424566

45434567
@classmethod
45444568
def InitFromBuf(cls, buf, pos):
@@ -4560,6 +4584,7 @@ def _UnPack(self, fullyConnectedOptions):
45604584
self.weightsFormat = fullyConnectedOptions.WeightsFormat()
45614585
self.keepNumDims = fullyConnectedOptions.KeepNumDims()
45624586
self.asymmetricQuantizeInputs = fullyConnectedOptions.AsymmetricQuantizeInputs()
4587+
self.quantizedBiasType = fullyConnectedOptions.QuantizedBiasType()
45634588

45644589
# FullyConnectedOptionsT
45654590
def Pack(self, builder):
@@ -4568,6 +4593,7 @@ def Pack(self, builder):
45684593
FullyConnectedOptionsAddWeightsFormat(builder, self.weightsFormat)
45694594
FullyConnectedOptionsAddKeepNumDims(builder, self.keepNumDims)
45704595
FullyConnectedOptionsAddAsymmetricQuantizeInputs(builder, self.asymmetricQuantizeInputs)
4596+
FullyConnectedOptionsAddQuantizedBiasType(builder, self.quantizedBiasType)
45714597
fullyConnectedOptions = FullyConnectedOptionsEnd(builder)
45724598
return fullyConnectedOptions
45734599
# automatically generated by the FlatBuffers compiler, do not modify
@@ -16436,7 +16462,14 @@ def FusedActivationFunction(self):
1643616462
return self._tab.Get(flatbuffers.number_types.Int8Flags, o + self._tab.Pos)
1643716463
return 0
1643816464

16439-
def TransposeConvOptionsStart(builder): builder.StartObject(4)
16465+
# TransposeConvOptions
16466+
def QuantizedBiasType(self):
16467+
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(12))
16468+
if o != 0:
16469+
return self._tab.Get(flatbuffers.number_types.Int8Flags, o + self._tab.Pos)
16470+
return 0
16471+
16472+
def TransposeConvOptionsStart(builder): builder.StartObject(5)
1644016473
def Start(builder):
1644116474
return TransposeConvOptionsStart(builder)
1644216475
def TransposeConvOptionsAddPadding(builder, padding): builder.PrependInt8Slot(0, padding, 0)
@@ -16451,6 +16484,9 @@ def AddStrideH(builder, strideH):
1645116484
def TransposeConvOptionsAddFusedActivationFunction(builder, fusedActivationFunction): builder.PrependInt8Slot(3, fusedActivationFunction, 0)
1645216485
def AddFusedActivationFunction(builder, fusedActivationFunction):
1645316486
return TransposeConvOptionsAddFusedActivationFunction(builder, fusedActivationFunction)
16487+
def TransposeConvOptionsAddQuantizedBiasType(builder, quantizedBiasType): builder.PrependInt8Slot(4, quantizedBiasType, 0)
16488+
def AddQuantizedBiasType(builder, quantizedBiasType):
16489+
return TransposeConvOptionsAddQuantizedBiasType(builder, quantizedBiasType)
1645416490
def TransposeConvOptionsEnd(builder): return builder.EndObject()
1645516491
def End(builder):
1645616492
return TransposeConvOptionsEnd(builder)
@@ -16463,6 +16499,7 @@ def __init__(self):
1646316499
self.strideW = 0 # type: int
1646416500
self.strideH = 0 # type: int
1646516501
self.fusedActivationFunction = 0 # type: int
16502+
self.quantizedBiasType = 0 # type: int
1646616503

1646716504
@classmethod
1646816505
def InitFromBuf(cls, buf, pos):
@@ -16484,6 +16521,7 @@ def _UnPack(self, transposeConvOptions):
1648416521
self.strideW = transposeConvOptions.StrideW()
1648516522
self.strideH = transposeConvOptions.StrideH()
1648616523
self.fusedActivationFunction = transposeConvOptions.FusedActivationFunction()
16524+
self.quantizedBiasType = transposeConvOptions.QuantizedBiasType()
1648716525

1648816526
# TransposeConvOptionsT
1648916527
def Pack(self, builder):
@@ -16492,6 +16530,7 @@ def Pack(self, builder):
1649216530
TransposeConvOptionsAddStrideW(builder, self.strideW)
1649316531
TransposeConvOptionsAddStrideH(builder, self.strideH)
1649416532
TransposeConvOptionsAddFusedActivationFunction(builder, self.fusedActivationFunction)
16533+
TransposeConvOptionsAddQuantizedBiasType(builder, self.quantizedBiasType)
1649516534
transposeConvOptions = TransposeConvOptionsEnd(builder)
1649616535
return transposeConvOptions
1649716536
# automatically generated by the FlatBuffers compiler, do not modify

0 commit comments

Comments
 (0)