Skip to content

Commit c02f6e4

Browse files
committed
Sync from upstream TF.
1 parent bc68d36 commit c02f6e4

4 files changed

Lines changed: 36 additions & 15 deletions

File tree

tensorflow/lite/core/api/tensor_utils.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ TfLiteStatus ResetVariableTensor(TfLiteTensor* tensor) {
3333
}
3434
// TODO(b/139446230): Provide a platform header to better handle these
3535
// specific scenarios.
36-
#if __ANDROID__ || defined(__x86_64__) || defined(__i386__) || \
37-
defined(__i386) || defined(__x86__) || defined(__X86__) || \
36+
#if defined(__ANDROID__) || defined(__x86_64__) || defined(__i386__) || \
37+
defined(__i386) || defined(__x86__) || defined(__X86__) || \
3838
defined(_X86_) || defined(_M_IX86) || defined(_M_X64)
3939
memset(tensor->data.raw, value, tensor->bytes);
4040
#else

tensorflow/lite/core/c/common.cc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,25 @@ TfLiteQuantization TfLiteQuantizationClone(const TfLiteQuantization& src) {
113113
case kTfLiteAffineQuantization: {
114114
dst.params = calloc(1, sizeof(TfLiteAffineQuantization));
115115
const TfLiteAffineQuantization* const src_params =
116-
(TfLiteAffineQuantization*)(src.params);
116+
reinterpret_cast<TfLiteAffineQuantization*>(src.params);
117117
TfLiteAffineQuantization* const dst_params =
118-
(TfLiteAffineQuantization*)(dst.params);
118+
reinterpret_cast<TfLiteAffineQuantization*>(dst.params);
119119
dst_params->quantized_dimension = src_params->quantized_dimension;
120120
dst_params->scale = TfLiteFloatArrayCopy(src_params->scale);
121121
dst_params->zero_point = TfLiteIntArrayCopy(src_params->zero_point);
122122
break;
123123
}
124+
case kTfLiteBlockwiseQuantization: {
125+
dst.params = calloc(1, sizeof(TfLiteBlockwiseQuantization));
126+
const TfLiteBlockwiseQuantization* const src_params =
127+
(TfLiteBlockwiseQuantization*)(src.params);
128+
TfLiteBlockwiseQuantization* const dst_params =
129+
(TfLiteBlockwiseQuantization*)(dst.params);
130+
dst_params->blocksize = src_params->blocksize;
131+
dst_params->scale = src_params->scale;
132+
dst_params->zero_point = src_params->zero_point;
133+
break;
134+
}
124135
}
125136
return dst;
126137
}
@@ -225,7 +236,7 @@ void TfLiteTensorDataFree(TfLiteTensor* t) {
225236
void TfLiteQuantizationFree(TfLiteQuantization* quantization) {
226237
if (quantization->type == kTfLiteAffineQuantization) {
227238
TfLiteAffineQuantization* q_params =
228-
(TfLiteAffineQuantization*)(quantization->params);
239+
reinterpret_cast<TfLiteAffineQuantization*>(quantization->params);
229240
if (q_params->scale) {
230241
TfLiteFloatArrayFree(q_params->scale);
231242
q_params->scale = nullptr;

tensorflow/lite/core/c/common.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ typedef enum TfLiteQuantizationType : int {
328328
/// Affine quantization (with support for per-channel quantization).
329329
/// Corresponds to TfLiteAffineQuantization.
330330
kTfLiteAffineQuantization = 1,
331+
/// Blockwise quantization.
332+
kTfLiteBlockwiseQuantization = 2,
331333
} TfLiteQuantizationType;
332334

333335
/// Structure specifying the quantization used by the tensor, if-any.
@@ -353,6 +355,20 @@ typedef struct TfLiteAffineQuantization {
353355
int32_t quantized_dimension;
354356
} TfLiteAffineQuantization;
355357

358+
/// Parameters for blockwise quantization across the output channels dimension.
359+
/// For a particular value in quantized_dimension, quantized values can be
360+
/// converted back to float using:
361+
/// `real_value = scale * (quantized_value - zero_point)`
362+
typedef struct TfLiteBlockwiseQuantization {
363+
// Index of the tensor containing the scales.
364+
int32_t scale;
365+
// Index of the tensor containing the zero points.
366+
int32_t zero_point;
367+
// Quantization blocksize.
368+
int32_t blocksize;
369+
int32_t quantized_dimension;
370+
} TfLiteBlockwiseQuantization;
371+
356372
/// A union of pointers that points to memory for a given tensor.
357373
///
358374
/// Do not access these members directly, if possible, use

tensorflow/lite/tools/flatbuffer_utils.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"""
2222

2323
import copy
24-
import functools
2524
import random
2625
import re
2726
import struct
@@ -490,12 +489,7 @@ def get_options_as(
490489
):
491490
raise err
492491

493-
@functools.singledispatch
494-
def _get_opts(unused_op):
495-
return None
496-
497-
@_get_opts.register
498-
def _(op: schema_fb.Operator):
492+
if isinstance(op, schema_fb.Operator):
499493
if not is_opt_1_type:
500494
enum_val = getattr(schema_fb.BuiltinOptions2, base_type_name)
501495
opts_creator = schema_fb.BuiltinOptions2Creator
@@ -510,8 +504,7 @@ def _(op: schema_fb.Operator):
510504
return None
511505
return opts_creator(enum_val, raw_ops)
512506

513-
@_get_opts.register
514-
def _(op: schema_fb.OperatorT):
507+
elif isinstance(op, schema_fb.OperatorT):
515508
if is_opt_1_type:
516509
raw_ops_t = op.builtinOptions
517510
else:
@@ -520,4 +513,5 @@ def _(op: schema_fb.OperatorT):
520513
return None
521514
return raw_ops_t
522515

523-
return _get_opts(op)
516+
else:
517+
return None

0 commit comments

Comments
 (0)