[NPUBINFMT] Append per-tensor information (e.g., num, dims, emod)
authorDongju Chae <dongju.chae@samsung.com>
Fri, 13 Dec 2019 05:39:17 +0000 (14:39 +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 appends per-tensor information such as dimensions, elem
size, emod, etc to 'npubinfmt.h'. This information is valid only for
npubinfmt v2. The previous version (v1) still valid. Also, we need to
make sure to assign its magic code to specify the npubinfmt version.

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

index 30f738b..640d515 100644 (file)
 #include <typedef.h>
 
 /* Same with the typical page size */
-#define NPUBIN_META_SIZE  (4096)
+#define NPUBIN_META_SIZE (4096)
+#define MAX_TENSORS (16)
+#define MAX_RANK    (4)
+#define DATA_GRANULARITY (64)
 
+/* npubinfmt magiccode macros */
+#define NPUBIN_MAGICCODE (0x53524E5055000000ULL)  /* ASCII hex for 'SRNPU' */
+#define NPUBIN_VERSION(magiccode) ((magiccode) & 0xFFULL)
+#define CHECK_NPUBIN(magiccode) (((magiccode) & ~0xFFFFFFULL) == NPUBIN_MAGICCODE)
 
 /**************************************************
  * NPU Binary Format: "Main"                      *
@@ -40,7 +47,6 @@
  * Actual composition of values to be determined **
  * by H/W and compiler. ***************************/
 
-
 /**
  * @brief Meta data, "MAIN".
  * @detail First 2048 bytes are to be accessed by NPU Engine
@@ -52,7 +58,18 @@ typedef struct {
   /** Reserved for NPU Engine. Do not use these without approvals from NPU Engine matinainers */
   union {
     struct {
-      uint64_t magiccode;
+      /**
+       * Descriptions for NPUBIN_MAGICCODE (64 bits)
+       *
+       *  ---------------------+--------------------
+       * |       63 - 24       |  23 - 8  |  7 - 0  |
+       * | ASCII hex for SRNPU | RESERVED | VERSION |
+       *  ------------------------------------------
+       *
+       * CHECK_NPUBIN(magiccode) should be true. Also, its least significant byte indicates
+       * the npubinfmt's version. E.g., if npubinfmt is v2, NPUBIN_VERSION(magiccode) == 0x2
+       */
+      uint64_t magiccode;     /**< npubinfmt's magiccode */
       uint64_t npu_version;
       uint64_t compiler_version;
 
@@ -61,18 +78,50 @@ typedef struct {
       uint64_t model_version;
 
       /** About input/output/intermediate DRAM buffer format, for the first input and the last output, which are required for host communication. */
-      uint64_t buffer_size;
+      uint64_t buffer_size;   /**< Total buffer size to cover all data (i.e., allocate DRAM size) */
       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 */
-      /** 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. */
-      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.*/
+      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.
+       */
+      union {
+        /** npubinfmt v2; this format supports non-contiguous tensors */
+        struct {
+          /** 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 */
+
+          uint32_t input_elem_size[MAX_TENSORS];       /**< input per-element size (in bytes) */
+          uint32_t input_dims[MAX_TENSORS][MAX_RANK];  /**< input dimensions (e.g., width, height, depth, batch) */
+
+          uint32_t input_emod_y[MAX_TENSORS];          /**< input addressing info (emod_y) */
+          uint32_t input_emod_z[MAX_TENSORS];          /**< input addressing info (emod_z) */
+
+          /** output tensor details */
+          uint32_t output_num;                         /**< Number of output tensors (<= MAX_TENSORS) */
+          uint32_t output_offsets[MAX_TENSORS];        /**< offsets for output tensors; each tensor would be not contiguous */
+
+          uint32_t output_elem_size[MAX_TENSORS];      /**< output per-element size (in bytes) */
+          uint32_t output_dims[MAX_TENSORS][MAX_RANK]; /**< output dimensions (e.g., width, height, depth, batch) */
+
+          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.*/
+        };
+      };
     };
     char reserved_npu_engine[2048]; /**< Ensure NPU-Engine part is 2048 bytes */
   };
@@ -82,7 +131,7 @@ typedef struct {
 
   char reserved_for_future[1024]; /**< Ensure the whole meta is within size with ct_assert */
 
-} npubin_meta;
+} __attribute__((packed)) npubin_meta;
 
 /* Compile-time assert. From http://www.pixelbeat.org/programming/gcc/static_assert.html */
 #define ASSERT_CONCAT_(a, b) a##b