You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2021/06/30 09:18:23 UTC

[GitHub] [tvm] ANSHUMAN87 commented on a change in pull request #8375: Fix issue with importing models using Tensorflow Lite 2.4.x schema

ANSHUMAN87 commented on a change in pull request #8375:
URL: https://github.com/apache/tvm/pull/8375#discussion_r661281565



##########
File path: python/tvm/relay/frontend/tflite.py
##########
@@ -251,7 +251,30 @@ def get_op_code_str(self, op):
             raise ImportError("The tflite package must be installed")
 
         op_code_list_idx = op.OpcodeIndex()
-        op_code_id = self.model.OperatorCodes(op_code_list_idx).BuiltinCode()
+
+        op_c = self.model.OperatorCodes(op_code_list_idx)
+        # In TFlite 2.4.x there was a change where the type of the field that contained
+        # the builtin code changed from int8 to int32 in the flat buffer representation.
+        # However to retain support for old flat buffers that were created, they retained
+        # the original 8 bit encoding for the operator but in a new field accessed by the
+        # DeprecatedBuiltinCode method.
+        # This means that the API function BuiltinCode() is used on an operator
+        # which was originally encoded as an 8 bit quantity it would look for the
+        # code in the new int32 field in the schema and this creates the need
+        # for the check for the magic number of 127 which is indicated by
+        # BuiltinOperator.PLACEHOLDER_FOR_GREATER_OP_CODES
+        # Remember however that this value came into existence only after Tensorflow
+        # lite 2.4.x and hence encase it in a try -except block.
+        # Phew !
+        try:
+            if op_c.BuiltinCode() < BuiltinOperator.PLACEHOLDER_FOR_GREATER_OP_CODES:

Review comment:
       We are duplicating the implementation. Should not do that. Should always use higher level APIs, so that in future any bug fix or version upgrade, the underlying changes will be taken care automatically.
   
   Use below APIs:
   
        from tensorflow.lite.python import schema_util
        from tensorflow.lite.tools import visualize
          
   
           op_code_list_idx = op.OpcodeIndex()
           op_code_id = self.model.OperatorCodes(op_code_list_idx).BuiltinCode()
           op_code_id = schema_util.get_builtin_code_from_operator_code(op_code_id)
           op_code_str = visualize.BuiltinCodeToName(op_code_id)`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org