Skip to content

Commit c497964

Browse files
authoredDec 12, 2024··
Automated sync from github.com/tensorflow/tensorflow (#2723)
BUG=automated sync from upstream NO_CHECK_TFLITE_FILES=automated sync from upstream
1 parent 048c954 commit c497964

File tree

6 files changed

+288
-8
lines changed

6 files changed

+288
-8
lines changed
 

‎tensorflow/compiler/mlir/lite/schema/schema.fbs

+13
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ table CustomQuantization {
7070
// Represents a specific quantization technique's parameters.
7171
union QuantizationDetails {
7272
CustomQuantization,
73+
BlockwiseQuantization,
74+
}
75+
76+
// Parameters for blockwise quantization.
77+
table BlockwiseQuantization {
78+
// index to the scale tensor, the tensor can be found in tensors array in
79+
// subgraph.
80+
scales: int;
81+
// index to the zero point tensor. If zero_points is -1, the zero point is
82+
// assumed to be 0, following the convention of optional tensors in tflite.
83+
zero_points: int;
84+
// The block size of the tensor.
85+
block_size: int;
7386
}
7487

7588
// Parameters for converting a quantized tensor back to float.

‎tensorflow/lite/core/c/c_api_types.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ extern "C" {
5656
#define TFL_CAPI_EXPORT
5757
#elif defined(TFL_STATIC_LIBRARY_BUILD)
5858
#define TFL_CAPI_EXPORT
59-
#else // not definded TFL_STATIC_LIBRARY_BUILD
59+
#else // not defined TFL_STATIC_LIBRARY_BUILD
6060
#if defined(_WIN32)
6161
#ifdef TFL_COMPILE_LIBRARY
6262
#define TFL_CAPI_EXPORT __declspec(dllexport)

‎tensorflow/lite/core/c/common.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ typedef enum TfLiteExternalContextType {
8080
kTfLiteGemmLowpContext = 1, /// include gemm_support.h to use.
8181
kTfLiteEdgeTpuContext = 2, /// Placeholder for Edge TPU support.
8282
kTfLiteCpuBackendContext = 3, /// include cpu_backend_context.h to use.
83-
kTfLiteMaxExternalContexts = 4
83+
kTfLiteLiteRtBufferContext =
84+
4, /// include external_litert_buffer_context.h to use.
85+
kTfLiteMaxExternalContexts = 5
8486
} TfLiteExternalContextType;
8587

8688
// Forward declare so dependent structs and methods can reference these types

‎tensorflow/lite/kernels/internal/reference/comparisons.h

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ limitations under the License.
1515
#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_COMPARISONS_H_
1616
#define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_COMPARISONS_H_
1717

18+
#include <cstdint>
19+
1820
#include "tensorflow/lite/core/c/common.h"
1921
#include "tensorflow/lite/core/macros.h"
2022
#include "tensorflow/lite/kernels/internal/common.h"

‎tensorflow/lite/python/schema_py_generated.py

+107-1
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ class TensorType(object):
3232
class QuantizationDetails(object):
3333
NONE = 0
3434
CustomQuantization = 1
35+
BlockwiseQuantization = 2
3536

3637
def QuantizationDetailsCreator(unionType, table):
3738
from flatbuffers.table import Table
3839
if not isinstance(table, Table):
3940
return None
4041
if unionType == QuantizationDetails().CustomQuantization:
4142
return CustomQuantizationT.InitFromBuf(table.Bytes, table.Pos)
43+
if unionType == QuantizationDetails().BlockwiseQuantization:
44+
return BlockwiseQuantizationT.InitFromBuf(table.Bytes, table.Pos)
4245
return None
4346

4447

@@ -949,6 +952,109 @@ def Pack(self, builder):
949952
return customQuantization
950953

951954

955+
class BlockwiseQuantization(object):
956+
__slots__ = ['_tab']
957+
958+
@classmethod
959+
def GetRootAs(cls, buf, offset=0):
960+
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)
961+
x = BlockwiseQuantization()
962+
x.Init(buf, n + offset)
963+
return x
964+
965+
@classmethod
966+
def GetRootAsBlockwiseQuantization(cls, buf, offset=0):
967+
"""This method is deprecated. Please switch to GetRootAs."""
968+
return cls.GetRootAs(buf, offset)
969+
@classmethod
970+
def BlockwiseQuantizationBufferHasIdentifier(cls, buf, offset, size_prefixed=False):
971+
return flatbuffers.util.BufferHasIdentifier(buf, offset, b"\x54\x46\x4C\x33", size_prefixed=size_prefixed)
972+
973+
# BlockwiseQuantization
974+
def Init(self, buf, pos):
975+
self._tab = flatbuffers.table.Table(buf, pos)
976+
977+
# BlockwiseQuantization
978+
def Scales(self):
979+
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
980+
if o != 0:
981+
return self._tab.Get(flatbuffers.number_types.Int32Flags, o + self._tab.Pos)
982+
return 0
983+
984+
# BlockwiseQuantization
985+
def ZeroPoints(self):
986+
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(6))
987+
if o != 0:
988+
return self._tab.Get(flatbuffers.number_types.Int32Flags, o + self._tab.Pos)
989+
return 0
990+
991+
# BlockwiseQuantization
992+
def BlockSize(self):
993+
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(8))
994+
if o != 0:
995+
return self._tab.Get(flatbuffers.number_types.Int32Flags, o + self._tab.Pos)
996+
return 0
997+
998+
def BlockwiseQuantizationStart(builder):
999+
builder.StartObject(3)
1000+
1001+
def BlockwiseQuantizationAddScales(builder, scales):
1002+
builder.PrependInt32Slot(0, scales, 0)
1003+
1004+
def BlockwiseQuantizationAddZeroPoints(builder, zeroPoints):
1005+
builder.PrependInt32Slot(1, zeroPoints, 0)
1006+
1007+
def BlockwiseQuantizationAddBlockSize(builder, blockSize):
1008+
builder.PrependInt32Slot(2, blockSize, 0)
1009+
1010+
def BlockwiseQuantizationEnd(builder):
1011+
return builder.EndObject()
1012+
1013+
1014+
1015+
class BlockwiseQuantizationT(object):
1016+
1017+
# BlockwiseQuantizationT
1018+
def __init__(self):
1019+
self.scales = 0 # type: int
1020+
self.zeroPoints = 0 # type: int
1021+
self.blockSize = 0 # type: int
1022+
1023+
@classmethod
1024+
def InitFromBuf(cls, buf, pos):
1025+
blockwiseQuantization = BlockwiseQuantization()
1026+
blockwiseQuantization.Init(buf, pos)
1027+
return cls.InitFromObj(blockwiseQuantization)
1028+
1029+
@classmethod
1030+
def InitFromPackedBuf(cls, buf, pos=0):
1031+
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, pos)
1032+
return cls.InitFromBuf(buf, pos+n)
1033+
1034+
@classmethod
1035+
def InitFromObj(cls, blockwiseQuantization):
1036+
x = BlockwiseQuantizationT()
1037+
x._UnPack(blockwiseQuantization)
1038+
return x
1039+
1040+
# BlockwiseQuantizationT
1041+
def _UnPack(self, blockwiseQuantization):
1042+
if blockwiseQuantization is None:
1043+
return
1044+
self.scales = blockwiseQuantization.Scales()
1045+
self.zeroPoints = blockwiseQuantization.ZeroPoints()
1046+
self.blockSize = blockwiseQuantization.BlockSize()
1047+
1048+
# BlockwiseQuantizationT
1049+
def Pack(self, builder):
1050+
BlockwiseQuantizationStart(builder)
1051+
BlockwiseQuantizationAddScales(builder, self.scales)
1052+
BlockwiseQuantizationAddZeroPoints(builder, self.zeroPoints)
1053+
BlockwiseQuantizationAddBlockSize(builder, self.blockSize)
1054+
blockwiseQuantization = BlockwiseQuantizationEnd(builder)
1055+
return blockwiseQuantization
1056+
1057+
9521058
class QuantizationParameters(object):
9531059
__slots__ = ['_tab']
9541060

@@ -1157,7 +1263,7 @@ def __init__(self):
11571263
self.scale = None # type: List[float]
11581264
self.zeroPoint = None # type: List[int]
11591265
self.detailsType = 0 # type: int
1160-
self.details = None # type: Union[None, CustomQuantizationT]
1266+
self.details = None # type: Union[None, CustomQuantizationT, BlockwiseQuantizationT]
11611267
self.quantizedDimension = 0 # type: int
11621268

11631269
@classmethod

0 commit comments

Comments
 (0)
Please sign in to comment.