[TEST/MODELGEN] Sync with the updated npubinfmt.h
authorDongju Chae <dongju.chae@samsung.com>
Fri, 13 Dec 2019 05:44:43 +0000 (14:44 +0900)
committer채동주/On-Device Lab(SR)/Staff Engineer/삼성전자 <dongju.chae@samsung.com>
Wed, 18 Dec 2019 09:29:05 +0000 (18:29 +0900)
This commit fixes a script to generate test models to sync new
npubinfmt.h

Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
include/common/npubinfmt.h
tools/gen-testdata/model_gen.py

index 640d515..1839baf 100644 (file)
@@ -61,6 +61,7 @@ typedef struct {
       /**
        * Descriptions for NPUBIN_MAGICCODE (64 bits)
        *
+       *          Little-Endian Byte Order
        *  ---------------------+--------------------
        * |       63 - 24       |  23 - 8  |  7 - 0  |
        * | ASCII hex for SRNPU | RESERVED | VERSION |
@@ -82,17 +83,30 @@ typedef struct {
       uint64_t size;          /**< Size of this model, including this meta */
       model_opmode type;      /**< Type of the model, consistent with model_opmode. Refer to typedef.h */
 
-      uint64_t program_size;  /**< Size of the program list instructions */
-      uint64_t weight_size;   /**< Size of the model weights */
-
       /**
        * If input comes from host, the buffer may be filled with input data.
        * Input and output areas may have intersection as they are not required simultaneously.
        * For per-tensor info, refer to 'reserved_for_compiler' field.
        */
+      struct {
+        /**
+         * npubinfmt v1; this format assumes contiguous tensors
+         * keep these variables for backward compatibility.
+         */
+        uint64_t input_offset;  /**< Input offset for this model. */
+        uint64_t input_size;    /**< Input size for this model.*/
+
+        uint64_t output_offset; /**< Output offset for this model.*/
+        uint64_t output_size;   /**< Output size for this model.*/
+      };
+
+      uint64_t program_size;  /**< Size of the program list instructions */
+      uint64_t weight_size;   /**< Size of the model weights */
+
       union {
-        /** npubinfmt v2; this format supports non-contiguous tensors */
         struct {
+          /** npubinfmt v2; this format supports non-contiguous tensors */
+
           /** input tensor details */
           uint32_t input_num;                          /**< Number of input tensors (<= MAX_TENSORS) */
           uint32_t input_offsets[MAX_TENSORS];         /**< offsets for input tensors; each tensor would be not contiguous */
@@ -113,14 +127,7 @@ typedef struct {
           uint32_t output_emod_y[MAX_TENSORS];         /**< output addressing info (emod_y) */
           uint32_t output_emod_z[MAX_TENSORS];         /**< output addressing info (emod_z) */
         };
-        /** npubinfmt v1; this format assumes contiguous tensors */
-        struct {
-          uint64_t input_offset;  /**< Input offset for this model. */
-          uint64_t input_size;    /**< Input size for this model.*/
-
-          uint64_t output_offset; /**< Output offset for this model.*/
-          uint64_t output_size;   /**< Output size for this model.*/
-        };
+        /** If npubinfmt should be changed, append here as another version */
       };
     };
     char reserved_npu_engine[2048]; /**< Ensure NPU-Engine part is 2048 bytes */
index e06a2f4..1dcd311 100755 (executable)
@@ -19,6 +19,9 @@ import re
 ## these values are common for all existing example visa binaries
 META_SIZE=4096
 SIZE_ALIGN=4096
+SIGNATURE='SRNPU'
+VERSION=1
+DEBUG=0
 
 ## @brief class for NPU model used in NPU Engine
 class NPUModel:
@@ -37,7 +40,21 @@ class NPUModel:
     self.input = self.outdir + "/input_fmap.bin"
     self.output = self.outdir + "/output_fmap.bin"
 
-    print ("binary size: " + str(self.in_binary_size))
+    self.signature = SIGNATURE
+    self.version = VERSION
+
+    if DEBUG:
+      print ("binary size: " + str(self.in_binary_size))
+
+  ## @brief get magic code
+  def get_magic_code (self):
+    value = 0
+    for s in self.signature:
+      value = (value << 8) + ord(s)
+    value <<= (8 - len(self.signature)) * 8
+    value += self.version
+
+    return value
 
   ## @brief get binary size; return zero if it does not exist
   def get_bin_size (self, name, idx):
@@ -123,10 +140,11 @@ class NPUModel:
       if self.single_mode:
         log_tag = '[single_layer] '
 
-      print (log_tag + ' input size: ' + str(input_fmap_size))
-      print (log_tag + ' esum size: ' + str(input_esum_size))
-      print (log_tag + ' weight size: ' + str(input_weight_size))
-      print (log_tag + ' output size: ' + str(output_fmap_size))
+      if DEBUG:
+        print (log_tag + ' input size: ' + str(input_fmap_size))
+        print (log_tag + ' esum size: ' + str(input_esum_size))
+        print (log_tag + ' weight size: ' + str(input_weight_size))
+        print (log_tag + ' output size: ' + str(output_fmap_size))
 
       if first_input_size == 0:
         first_input_offset = in0_eaddr0
@@ -162,7 +180,7 @@ class NPUModel:
     f.seek(0)
 
     # we don't know the exact values yet
-    f.write(struct.pack('<Q', 0)) # magiccode
+    f.write(struct.pack('<Q', self.get_magic_code())) # magiccode
     f.write(struct.pack('<Q', 0)) # npu_version
     f.write(struct.pack('<Q', 0)) # compiler_version
     f.write("visa_example_model") # name
@@ -200,6 +218,7 @@ if __name__ == '__main__':
   try:
     model = NPUModel (args)
     model.gen()
-    print ("[SUCCESS] final model size: " + str(model.size))
+    if DEBUG:
+      print ("[SUCCESS] final model size: " + str(model.size))
   except Exception as e:
     print ("[FAILURE] error detected: " + str(e))