Skip to content

Commit 9c63f4f

Browse files
authored
[TFLite] Mimic the TFLite's 2.4 reader's behaviour (#8538)
In TFLite 2.4, the builtin code value can be either in "deprecated_builtin_code" field or "builtin_code" field (as long as the value is less than 127) and similarly to the TFLite's reader, we should use the higher value of the two. Change-Id: I0d738f9257187903b4c5b4cc5a8733a451ddc02e
1 parent 3445532 commit 9c63f4f

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

python/tvm/relay/frontend/tflite.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,23 +255,23 @@ def get_op_code_str(self, op):
255255
op_c = self.model.OperatorCodes(op_code_list_idx)
256256
# In TFlite 2.4.x there was a change where the type of the field that contained
257257
# the builtin code changed from int8 to int32 in the flat buffer representation.
258-
# However to retain support for old flat buffers that were created, they retained
259-
# the original 8 bit encoding for the operator but in a new field accessed by the
260-
# DeprecatedBuiltinCode method.
261-
# This means that the API function BuiltinCode() is used on an operator
262-
# which was originally encoded as an 8 bit quantity it would look for the
263-
# code in the new int32 field in the schema and this creates the need
264-
# for the check for the magic number of 127 which is indicated by
265-
# BuiltinOperator.PLACEHOLDER_FOR_GREATER_OP_CODES
258+
# However, to retain support for old flat buffers that were created, they retained
259+
# the original 8 bit field, but named it "deprecated_builtin_code" in TFLite 2.4.
260+
# This means that the API function BuiltinCode() which originally returned the value
261+
# of the 8 bit field would now look for the value in the new int32 field in the
262+
# schema and DeprecatedBuiltinCode() will look at the old 8 bit field.
263+
# In TFLite 2.4, if the opcode value is less than 127, it can be in either field
264+
# (however, if it is only in the "builtin_code" field, the model is not backward
265+
# compatible), so similarly to TFLite 2.4 reader, we'll pick the higher value of the
266+
# two fields.
266267
# Remember however that this value came into existence only after Tensorflow
267268
# lite 2.4.x and hence encase it in a try -except block.
268269
# Phew !
269270
try:
270-
if op_c.BuiltinCode() < BuiltinOperator.PLACEHOLDER_FOR_GREATER_OP_CODES:
271-
opc = op_c.DeprecatedBuiltinCode()
272-
else:
273-
opc = op_c.BuiltinCode()
271+
opc = max(op_c.DeprecatedBuiltinCode(), op_c.BuiltinCode())
274272
except AttributeError:
273+
# In versions before 2.4 the int8 field that holds the builtin code is accessed
274+
# by BuiltinCode() and DeprecatedBuiltinCode() doesn't exist
275275
opc = op_c.BuiltinCode()
276276

277277
op_code_id = opc

0 commit comments

Comments
 (0)