Skip to content

Commit

Permalink
Update to r2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenGene committed Jan 20, 2020
1 parent 871746a commit 605b1e5
Show file tree
Hide file tree
Showing 24 changed files with 276 additions and 36 deletions.
48 changes: 46 additions & 2 deletions schema.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ enum BuiltinOperator : byte {
QUANTIZE = 114,
MATRIX_SET_DIAG = 115,
ROUND = 116,
HARD_SWISH = 117,
IF = 118,
WHILE = 119,
}

// Options for the builtin operators.
Expand Down Expand Up @@ -323,7 +326,10 @@ union BuiltinOptions {
ReverseSequenceOptions,
MatrixDiagOptions,
QuantizeOptions,
MatrixSetDiagOptions
MatrixSetDiagOptions,
HardSwishOptions,
IfOptions,
WhileOptions
}

enum Padding : byte { SAME, VALID }
Expand Down Expand Up @@ -418,6 +424,11 @@ table FullyConnectedOptions {

// Parameters for FullyConnected version 2 or above.
weights_format:FullyConnectedOptionsWeightsFormat = DEFAULT;

// Parameters for FullyConnected version 5 or above.
// If set to true, then the number of dimension is preserved. Furthermore,
// all but the last dimension of the input and output shapes will be equal.
keep_num_dims: bool;
}

table SoftmaxOptions {
Expand Down Expand Up @@ -696,6 +707,9 @@ table AbsOptions {
}


table HardSwishOptions {
}

table LogicalAndOptions {
}

Expand Down Expand Up @@ -773,6 +787,16 @@ table QuantizeOptions {
table MatrixSetDiagOptions {
}

table IfOptions {
then_subgraph_index:int;
else_subgraph_index:int;
}

table WhileOptions {
cond_subgraph_index:int;
body_subgraph_index:int;
}

// An OperatorCode can be an enum value (BuiltinOperator) if the operator is a
// builtin, or a string if the operator is custom.
table OperatorCode {
Expand Down Expand Up @@ -814,6 +838,15 @@ table Operator {
// If the list is empty, no variable is mutated in this operator.
// The list either has the same length as `inputs`, or is empty.
mutating_variable_inputs:[bool];

// A list of indices to the subgraph's "tensors" that are internal to an Op.
// Internal tensors are those that do not flow in or out of the operation,
// but instead are part of internal computation. As such, the operation's
// implementation may manage its memory more efficiently. They are needed
// however (i.e. not just an implementation detail) since they are part of the
// computation, which may require relevant metadata such as quantization
// parameters.
intermediates:[int];
}

// The root type, defining a subgraph, which typically represents an entire
Expand Down Expand Up @@ -844,6 +877,13 @@ table Buffer {
data:[ubyte] (force_align: 16);
}

table Metadata {
// A human readable string to uniquely identify a Metadata.
name:string;
// An index to the buffers table.
buffer:uint;
}

table Model {
// Version of the schema.
version:uint;
Expand All @@ -866,8 +906,12 @@ table Model {
// their buffer.
buffers:[Buffer];

// Metadata about the model. Indirects into the existings buffers list.
// Metadata about the model. Indirects into the existings buffers list.
// Deprecated, prefer to use metadata field.
metadata_buffer:[int];

// Metadata about the model.
metadata:[Metadata];
}

root_type Model;
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="tflite",
version="1.14.0",
version="2.0",
author="google",
author_email="[email protected]",
description="TFLite",
Expand Down
8 changes: 4 additions & 4 deletions tflite/BidirectionalSequenceLSTMOptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ def ProjClip(self):
def MergeOutputs(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(10))
if o != 0:
return bool(self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos))
return False
return self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos)
return 0

# BidirectionalSequenceLSTMOptions
def TimeMajor(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(12))
if o != 0:
return bool(self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos))
return True
return self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos)
return 1

def BidirectionalSequenceLSTMOptionsStart(builder): builder.StartObject(5)
def BidirectionalSequenceLSTMOptionsAddFusedActivationFunction(builder, fusedActivationFunction): builder.PrependInt8Slot(0, fusedActivationFunction, 0)
Expand Down
8 changes: 4 additions & 4 deletions tflite/BidirectionalSequenceRNNOptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def Init(self, buf, pos):
def TimeMajor(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
if o != 0:
return bool(self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos))
return False
return self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos)
return 0

# BidirectionalSequenceRNNOptions
def FusedActivationFunction(self):
Expand All @@ -36,8 +36,8 @@ def FusedActivationFunction(self):
def MergeOutputs(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(8))
if o != 0:
return bool(self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos))
return False
return self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos)
return 0

def BidirectionalSequenceRNNOptionsStart(builder): builder.StartObject(3)
def BidirectionalSequenceRNNOptionsAddTimeMajor(builder, timeMajor): builder.PrependBoolSlot(0, timeMajor, 0)
Expand Down
3 changes: 3 additions & 0 deletions tflite/BuiltinOperator.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,7 @@ class BuiltinOperator(object):
QUANTIZE = 114
MATRIX_SET_DIAG = 115
ROUND = 116
HARD_SWISH = 117
IF = 118
WHILE = 119

3 changes: 3 additions & 0 deletions tflite/BuiltinOptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,7 @@ class BuiltinOptions(object):
MatrixDiagOptions = 88
QuantizeOptions = 89
MatrixSetDiagOptions = 90
HardSwishOptions = 91
IfOptions = 92
WhileOptions = 93

4 changes: 2 additions & 2 deletions tflite/FakeQuantOptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def NumBits(self):
def NarrowRange(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(10))
if o != 0:
return bool(self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos))
return False
return self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos)
return 0

def FakeQuantOptionsStart(builder): builder.StartObject(4)
def FakeQuantOptionsAddMin(builder, min): builder.PrependFloat32Slot(0, min, 0.0)
Expand Down
10 changes: 9 additions & 1 deletion tflite/FullyConnectedOptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ def WeightsFormat(self):
return self._tab.Get(flatbuffers.number_types.Int8Flags, o + self._tab.Pos)
return 0

def FullyConnectedOptionsStart(builder): builder.StartObject(2)
# FullyConnectedOptions
def KeepNumDims(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(8))
if o != 0:
return self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos)
return 0

def FullyConnectedOptionsStart(builder): builder.StartObject(3)
def FullyConnectedOptionsAddFusedActivationFunction(builder, fusedActivationFunction): builder.PrependInt8Slot(0, fusedActivationFunction, 0)
def FullyConnectedOptionsAddWeightsFormat(builder, weightsFormat): builder.PrependInt8Slot(1, weightsFormat, 0)
def FullyConnectedOptionsAddKeepNumDims(builder, keepNumDims): builder.PrependBoolSlot(2, keepNumDims, 0)
def FullyConnectedOptionsEnd(builder): return builder.EndObject()
22 changes: 22 additions & 0 deletions tflite/HardSwishOptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# automatically generated by the FlatBuffers compiler, do not modify

# namespace: tflite

import flatbuffers

class HardSwishOptions(object):
__slots__ = ['_tab']

@classmethod
def GetRootAsHardSwishOptions(cls, buf, offset):
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)
x = HardSwishOptions()
x.Init(buf, n + offset)
return x

# HardSwishOptions
def Init(self, buf, pos):
self._tab = flatbuffers.table.Table(buf, pos)

def HardSwishOptionsStart(builder): builder.StartObject(0)
def HardSwishOptionsEnd(builder): return builder.EndObject()
38 changes: 38 additions & 0 deletions tflite/IfOptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# automatically generated by the FlatBuffers compiler, do not modify

# namespace: tflite

import flatbuffers

class IfOptions(object):
__slots__ = ['_tab']

@classmethod
def GetRootAsIfOptions(cls, buf, offset):
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)
x = IfOptions()
x.Init(buf, n + offset)
return x

# IfOptions
def Init(self, buf, pos):
self._tab = flatbuffers.table.Table(buf, pos)

# IfOptions
def ThenSubgraphIndex(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
if o != 0:
return self._tab.Get(flatbuffers.number_types.Int32Flags, o + self._tab.Pos)
return 0

# IfOptions
def ElseSubgraphIndex(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(6))
if o != 0:
return self._tab.Get(flatbuffers.number_types.Int32Flags, o + self._tab.Pos)
return 0

def IfOptionsStart(builder): builder.StartObject(2)
def IfOptionsAddThenSubgraphIndex(builder, thenSubgraphIndex): builder.PrependInt32Slot(0, thenSubgraphIndex, 0)
def IfOptionsAddElseSubgraphIndex(builder, elseSubgraphIndex): builder.PrependInt32Slot(1, elseSubgraphIndex, 0)
def IfOptionsEnd(builder): return builder.EndObject()
38 changes: 38 additions & 0 deletions tflite/Metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# automatically generated by the FlatBuffers compiler, do not modify

# namespace: tflite

import flatbuffers

class Metadata(object):
__slots__ = ['_tab']

@classmethod
def GetRootAsMetadata(cls, buf, offset):
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)
x = Metadata()
x.Init(buf, n + offset)
return x

# Metadata
def Init(self, buf, pos):
self._tab = flatbuffers.table.Table(buf, pos)

# Metadata
def Name(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
if o != 0:
return self._tab.String(o + self._tab.Pos)
return bytes()

# Metadata
def Buffer(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(6))
if o != 0:
return self._tab.Get(flatbuffers.number_types.Uint32Flags, o + self._tab.Pos)
return 0

def MetadataStart(builder): builder.StartObject(2)
def MetadataAddName(builder, name): builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(name), 0)
def MetadataAddBuffer(builder, buffer): builder.PrependUint32Slot(1, buffer, 0)
def MetadataEnd(builder): return builder.EndObject()
26 changes: 24 additions & 2 deletions tflite/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def Description(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(10))
if o != 0:
return self._tab.String(o + self._tab.Pos)
return None
return bytes()

# Model
def Buffers(self, j):
Expand Down Expand Up @@ -114,7 +114,27 @@ def MetadataBufferLength(self):
return self._tab.VectorLen(o)
return 0

def ModelStart(builder): builder.StartObject(6)
# Model
def Metadata(self, j):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(16))
if o != 0:
x = self._tab.Vector(o)
x += flatbuffers.number_types.UOffsetTFlags.py_type(j) * 4
x = self._tab.Indirect(x)
from .Metadata import Metadata
obj = Metadata()
obj.Init(self._tab.Bytes, x)
return obj
return None

# Model
def MetadataLength(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(16))
if o != 0:
return self._tab.VectorLen(o)
return 0

def ModelStart(builder): builder.StartObject(7)
def ModelAddVersion(builder, version): builder.PrependUint32Slot(0, version, 0)
def ModelAddOperatorCodes(builder, operatorCodes): builder.PrependUOffsetTRelativeSlot(1, flatbuffers.number_types.UOffsetTFlags.py_type(operatorCodes), 0)
def ModelStartOperatorCodesVector(builder, numElems): return builder.StartVector(4, numElems, 4)
Expand All @@ -125,4 +145,6 @@ def ModelAddBuffers(builder, buffers): builder.PrependUOffsetTRelativeSlot(4, fl
def ModelStartBuffersVector(builder, numElems): return builder.StartVector(4, numElems, 4)
def ModelAddMetadataBuffer(builder, metadataBuffer): builder.PrependUOffsetTRelativeSlot(5, flatbuffers.number_types.UOffsetTFlags.py_type(metadataBuffer), 0)
def ModelStartMetadataBufferVector(builder, numElems): return builder.StartVector(4, numElems, 4)
def ModelAddMetadata(builder, metadata): builder.PrependUOffsetTRelativeSlot(6, flatbuffers.number_types.UOffsetTFlags.py_type(metadata), 0)
def ModelStartMetadataVector(builder, numElems): return builder.StartVector(4, numElems, 4)
def ModelEnd(builder): return builder.EndObject()
26 changes: 25 additions & 1 deletion tflite/Operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,29 @@ def MutatingVariableInputsLength(self):
return self._tab.VectorLen(o)
return 0

def OperatorStart(builder): builder.StartObject(8)
# Operator
def Intermediates(self, j):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(20))
if o != 0:
a = self._tab.Vector(o)
return self._tab.Get(flatbuffers.number_types.Int32Flags, a + flatbuffers.number_types.UOffsetTFlags.py_type(j * 4))
return 0

# Operator
def IntermediatesAsNumpy(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(20))
if o != 0:
return self._tab.GetVectorAsNumpy(flatbuffers.number_types.Int32Flags, o)
return 0

# Operator
def IntermediatesLength(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(20))
if o != 0:
return self._tab.VectorLen(o)
return 0

def OperatorStart(builder): builder.StartObject(9)
def OperatorAddOpcodeIndex(builder, opcodeIndex): builder.PrependUint32Slot(0, opcodeIndex, 0)
def OperatorAddInputs(builder, inputs): builder.PrependUOffsetTRelativeSlot(1, flatbuffers.number_types.UOffsetTFlags.py_type(inputs), 0)
def OperatorStartInputsVector(builder, numElems): return builder.StartVector(4, numElems, 4)
Expand All @@ -150,4 +172,6 @@ def OperatorStartCustomOptionsVector(builder, numElems): return builder.StartVec
def OperatorAddCustomOptionsFormat(builder, customOptionsFormat): builder.PrependInt8Slot(6, customOptionsFormat, 0)
def OperatorAddMutatingVariableInputs(builder, mutatingVariableInputs): builder.PrependUOffsetTRelativeSlot(7, flatbuffers.number_types.UOffsetTFlags.py_type(mutatingVariableInputs), 0)
def OperatorStartMutatingVariableInputsVector(builder, numElems): return builder.StartVector(1, numElems, 1)
def OperatorAddIntermediates(builder, intermediates): builder.PrependUOffsetTRelativeSlot(8, flatbuffers.number_types.UOffsetTFlags.py_type(intermediates), 0)
def OperatorStartIntermediatesVector(builder, numElems): return builder.StartVector(4, numElems, 4)
def OperatorEnd(builder): return builder.EndObject()
2 changes: 1 addition & 1 deletion tflite/OperatorCode.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def CustomCode(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(6))
if o != 0:
return self._tab.String(o + self._tab.Pos)
return None
return bytes()

# OperatorCode
def Version(self):
Expand Down
Loading

0 comments on commit 605b1e5

Please sign in to comment.