Initial import from NPU_SystemService
authorJiho Chu <jiho.chu@samsung.com>
Tue, 13 Jun 2023 07:40:11 +0000 (16:40 +0900)
committerJiho Chu <jiho.chu@samsung.com>
Wed, 14 Jun 2023 03:43:49 +0000 (12:43 +0900)
npubin format is imported from:
https://github.sec.samsung.net/AIP/NPU_SystemService

Signed-off-by: Jiho Chu <jiho.chu@samsung.com>
14 files changed:
.github/CODEOWNERS [new file with mode: 0644]
.gitignore [new file with mode: 0644]
CHANGES [new file with mode: 0644]
debian/changelog [new file with mode: 0644]
debian/compat [new file with mode: 0644]
debian/control [new file with mode: 0644]
debian/copyright [new file with mode: 0644]
debian/rules [new file with mode: 0755]
include/npubinfmt.h [new file with mode: 0644]
include/typedef.h [new file with mode: 0644]
meson.build [new file with mode: 0644]
npubin-fmt.pc.in [new file with mode: 0644]
packaging/npubin-fmt.manifest [new file with mode: 0644]
packaging/npubin-fmt.spec [new file with mode: 0644]

diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS
new file mode 100644 (file)
index 0000000..884172c
--- /dev/null
@@ -0,0 +1,7 @@
+# This is a comment.
+# Each line is a file pattern followed by one or more owners.
+# For more details, visit https://help.github.com/articles/about-codeowners/
+# The lists of people and groups are for github.com
+
+# All members of npu-OS-team are supposed to review each other
+* @AIP/npu-os @myungjoo-ham @jiho-chu @yelini-jeong
diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..891470a
--- /dev/null
@@ -0,0 +1,71 @@
+# Created by https://www.toptal.com/developers/gitignore/api/git,meson,vim
+# Edit at https://www.toptal.com/developers/gitignore?templates=git,meson,vim
+
+### Git ###
+# Created by git for backups. To disable backups in Git:
+# $ git config --global mergetool.keepBackup false
+*.orig
+
+# Created by git when using merge tools for conflicts
+*.BACKUP.*
+*.BASE.*
+*.LOCAL.*
+*.REMOTE.*
+*_BACKUP_*.txt
+*_BASE_*.txt
+*_LOCAL_*.txt
+*_REMOTE_*.txt
+
+### Meson ###
+# subproject directories
+/subprojects/*
+!/subprojects/*.wrap
+
+# Meson Directories
+meson-logs
+meson-private
+
+# Meson Files
+meson_benchmark_setup.dat
+meson_test_setup.dat
+sanitycheckcpp.cc     # C++ specific
+sanitycheckcpp.exe    # C++ specific
+
+# Ninja
+build.ninja
+.ninja_deps
+.ninja_logs
+
+# Misc
+compile_commands.json
+
+### Vim ###
+# Swap
+[._]*.s[a-v][a-z]
+!*.svg  # comment out if you don't need vector files
+[._]*.sw[a-p]
+[._]s[a-rt-v][a-z]
+[._]ss[a-gi-z]
+[._]sw[a-p]
+
+# Session
+Session.vim
+Sessionx.vim
+
+# Temporary
+.netrwhist
+*~
+# Auto-generated tag files
+tags
+# Persistent undo
+[._]*.un~
+
+# End of https://www.toptal.com/developers/gitignore/api/git,meson,vim
+
+# Local Variables:
+.tags*
+.clangd*
+debian/npubin-fmt*
+debian/.debhelper*
+debian/files
+build*
diff --git a/CHANGES b/CHANGES
new file mode 100644 (file)
index 0000000..82183b6
--- /dev/null
+++ b/CHANGES
@@ -0,0 +1,5 @@
+0.0.1 -> 1.0.0:
+  - Build package for npubin-fmt
+
+0.0.1:
+  - Imported from NPU_SystemService
diff --git a/debian/changelog b/debian/changelog
new file mode 100644 (file)
index 0000000..7a0ed98
--- /dev/null
@@ -0,0 +1,12 @@
+npubin-fmt (1.0.0) unstable bionic focal; urgency=medium
+
+  * 1.0.0 Build package for npubin-fmt
+
+ -- Jiho Chu <jiho.chu@samsung.com>  Thu, 13 Jun 2023 13:00:00 +0900
+
+npubin-fmt (0.0.1) unstable bionic focal; urgency=medium
+
+  * 0.0.1 Import from NPU_SystemService binfmt
+
+ -- MyungJoo Ham <myungjoo.ham@samsung.com>  Thu, 13 Jun 2023 12:00:00 +0900
+
diff --git a/debian/compat b/debian/compat
new file mode 100644 (file)
index 0000000..48082f7
--- /dev/null
@@ -0,0 +1 @@
+12
diff --git a/debian/control b/debian/control
new file mode 100644 (file)
index 0000000..09388f3
--- /dev/null
@@ -0,0 +1,15 @@
+Source: npubin-fmt
+Section: libs
+Priority: optional
+Maintainer: MyungJoo Ham <myungjoo.ham@samsung.com>
+Build-Depends: ninja-build, meson (>=0.50), debhelper (>=9), pkg-config
+Standards-Version: 1.0.0
+Homepage: https://research.samsung.com
+
+Package: npubin-fmt
+Architecture: amd64
+Multi-Arch: same
+Depends: ${misc:Depends}
+Description: NPU Engine Utils Package
+ This provides npu metadata information for NPU.
+
diff --git a/debian/copyright b/debian/copyright
new file mode 100644 (file)
index 0000000..7467957
--- /dev/null
@@ -0,0 +1,3 @@
+Files: *
+License: Apache-2.0
+Copyright(C) Samsung Electonics 2022
diff --git a/debian/rules b/debian/rules
new file mode 100755 (executable)
index 0000000..7a05869
--- /dev/null
@@ -0,0 +1,31 @@
+#!/usr/bin/make -f
+# See debhelper(7) (uncomment to enable)
+# output every command that modifies files on the build system.
+#export DH_VERBOSE = 1
+
+ROOT_DIR:=$(shell pwd)
+export NPU_INSTALL_PREFIX=/opt/trinity
+export LC_ALL=C.UTF-8
+export BUILD_TYPE?=plain
+
+%:
+       dh $@ --parallel
+
+override_dh_auto_clean:
+       rm -rf build
+       rm -rf debian/tmp
+
+override_dh_auto_configure:
+       PKG_CONFIG_PATH=${NPU_INSTALL_PREFIX}/lib/pkgconfig meson --buildtype=$(BUILD_TYPE) --prefix=$(NPU_INSTALL_PREFIX) --sysconfdir=$(NPU_INSTALL_PREFIX)/etc --libdir=lib --bindir=bin --includedir=include build -Dtarget_platform=debian 
+
+override_dh_auto_build:
+       ninja -C build
+
+override_dh_auto_install:
+       DESTDIR=$(CURDIR)/debian/tmp ninja -C build install
+
+override_dh_install:
+       dh_install --sourcedir=debian/tmp
+
+override_dh_missing:
+       dh_missing --list-missing --fail-missing
diff --git a/include/npubinfmt.h b/include/npubinfmt.h
new file mode 100644 (file)
index 0000000..0cce584
--- /dev/null
@@ -0,0 +1,344 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+/**
+ * Copyright (C) 2019 Samsung Electronics
+ * Copyright (C) 2019 MyungJoo Ham <myungjoo.ham@samsung.com>
+ */
+/**
+ * @file npubinfmt.h
+ * @date 05 Jun 2019
+ * @brief Describes the format of NPU model binary as a result of NPU compiler
+ * @author MyungJoo Ham <myungjoo.ham@samsung.com>
+ * @bug No know bugs except for NYI items.
+ *
+ * To packagers: this is to be exported externally including NPU compilers
+ */
+
+#ifndef NPU_BIN_FMT_H__
+#define NPU_BIN_FMT_H__
+
+#include <stdint.h>
+#include <typedef.h>
+#include <assert.h>
+
+/* size of metadata */
+
+/* @brief unit size of metadata (minimal size) */
+#define NPUBIN_META_SIZE (4096)
+
+/**
+ * @deprecated valid if extended metasize < 1MB. otherwise, use 'extended_metasize' field.
+ * @brief number of extended metadata
+ */
+#define NPUBIN_META_NUM_EXTENDED(magiccode) (((magiccode) >> 8) & 0xFFULL)
+
+/**
+ * @deprecated valid if extended metasize < 1MB. otherwise, use 'extended_metasize' field.
+ * @brief return extended metadata size of npu binary
+ */
+static inline uint64_t
+NPUBIN_META_EXTENDED_SIZE (uint64_t magiccode) {
+  uint64_t num_extended = NPUBIN_META_NUM_EXTENDED (magiccode);
+  assert (num_extended <= UINT8_MAX); /** sanity check for svace */
+  return num_extended * NPUBIN_META_SIZE;
+}
+
+/**
+ * @deprecated valid if extended metasize < 1MB. otherwise, use 'extended_metasize' field.
+ * @brief return total metadata size of npu binary, including extended data
+ */
+static inline uint64_t
+NPUBIN_META_TOTAL_SIZE (uint64_t magiccode) {
+  uint64_t size_extended = NPUBIN_META_EXTENDED_SIZE (magiccode);
+  assert (size_extended <= UINT8_MAX * NPUBIN_META_SIZE); /** sanity check for svace */
+  return size_extended + NPUBIN_META_SIZE;
+}
+
+/* tensor data constrains */
+#define MAX_TENSORS (16)
+#define MAX_RANK (4)
+#define MAX_SEGMENTS (256)    /* 8-bit segment indexing */
+#define DATA_GRANULARITY (64) /* Default MPA_L in TRIV1 and 8-TOPS TRIV2 */
+#define DATA_GRANULARITY_SHIFT (6)
+
+/* npubinfmt magiccode macros */
+#define NPUBIN_MAGICCODE (0x53524E5055000000ULL) /* ASCII hex for 'SRNPU' */
+#define NPUBIN_VERSION_MAX (3)
+#define NPUBIN_VERSION(magiccode) ((magiccode) &0xFFULL)
+#define CHECK_NPUBIN(magiccode) (((magiccode) & ~0xFFFFFFULL) == NPUBIN_MAGICCODE)
+
+/* npubinfmt npu_version macros */
+#define NPU_VERSION_MAJOR(npu_version) (((npu_version) &0x000000FFULL))
+#define NPU_VERSION_MINOR(npu_version) (((npu_version) &0x0000FF00ULL) >> 8)
+#define NPU_VERSION_EXTRA(npu_version) (((npu_version) &0x00FF0000ULL) >> 16)
+#define NPU_VERSION_TOPS(npu_version) (((npu_version) &0x0F000000ULL) >> 24)
+
+/******************************************************************
+ * NPU Binary Format: "Main"                                      *
+ ******************************************************************
+ * Meta data for model                                            *
+ ******************************************************************
+ * Meta            *                   Content                    *
+ * 4096B           ************************************************
+ *                 * Extended Meta * Program      * Weight Values *
+ *                 * (Optional)    *              *               *
+ * type:           * NPUBIN_META_\ * program_size * weight_size   *
+ * npubin_\        * EXTENDED_SIZE * (bytes)      * (bytes)       *
+ * meta            * (bytes)       *              *               *
+ ******************************************************************
+ * Actual composition of values to be determined **
+ * by H/W and compiler. ***************************/
+
+/**
+ * @brief Meta data, "MAIN".
+ * @detail First 3072 bytes are to be accessed by NPU Engine
+ *         Then, next 1024 bytes are reserved for compilers and runtime
+ *         The remaining bytes (if exist) are reserved for future usage.
+ */
+typedef struct {
+  /** Reserved for NPU Engine. Do not use these without approvals from NPU Engine matinainers */
+  union {
+    struct {
+      /**
+       * Descriptions for NPUBIN_MAGICCODE (64 bits)
+       *
+       *              Little-Endian Byte Order
+       *  ---------------------+-------------------------------
+       * |       63 - 24       |  23 - 16 |  15 - 8  |  7 - 0  |
+       * | ASCII hex for SRNPU | RESERVED | EXTENDED | 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) == 2.
+       *
+       * CAUTION: the following descriptions for extended metadata are valid only when
+       * 'extended_metasize' field is zero. Otherwise, 'EXTENDED' bits are ignored.
+       *
+       * Also, the size of metadata can be extended depending on the 'EXTENDED' field. E.g.,
+       * if 'EXTENDED' is 0, the size of metadata is NPUBIN_META_SIZE. If it's larger than 0,
+       * the actual size of metadata is NPUBIN_META_SIZE + NPUBIN_META_EXTENDED_SIZE(magiccode),
+       * which is equal to NPUBIN_META_TOTAL_SIZE(magiccode).
+       */
+      uint64_t magiccode; /**< npubinfmt's magiccode */
+      /**
+       * Descriptions for npu_version (64 bits)
+       *
+       *             Little-Endian Byte Order
+       *  ---------------------+--------------------------
+       * |  63 - 32  | 31 - 24 | 23 - 16 | 15 - 8 | 7 - 0 |
+       * |  RESERVED |  TOPS   |  EXTRA  |  MINOR | MAJOR |
+       *  ------------------------------------------------
+       */
+      uint64_t npu_version;
+      uint64_t compiler_version;
+
+      char name[128];
+      uint64_t model_id;
+      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;
+      /**< [v1-only] 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 */
+      uint32_t extended_metasize;
+      /**< The size of extended metadata */
+
+      /**
+       * 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 {
+        struct {
+          /**
+           * npubinfmt v2; this format supports non-contiguous tensors
+           * input/output dimensions are regarded as NHWC. But, users may
+           * expect any data layout defined in typedef.h.
+           */
+
+          /** 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 (N, H, W, C) */
+
+          uint32_t input_emod_y[MAX_TENSORS];
+          /**< input addressing info (emod_y) */
+          uint32_t input_emod_z[MAX_TENSORS];
+          /**< input addressing info (emod_z) */
+
+          uint32_t input_quant_z[MAX_TENSORS];
+          /**< input quantization parameter (zero-point) */
+          float input_quant_s[MAX_TENSORS];
+          /**< input quantization parameter (scale) */
+
+          /** 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 (N, H, W, C) */
+
+          uint32_t output_emod_y[MAX_TENSORS];
+          /**< output addressing info (emod_y) */
+          uint32_t output_emod_z[MAX_TENSORS];
+          /**< output addressing info (emod_z) */
+
+          uint32_t output_quant_z[MAX_TENSORS];
+          /**< output quantization parameter (zero-point) */
+          float output_quant_s[MAX_TENSORS];
+          /**< output quantization parameter (scale) */
+        };
+        struct {
+          /**
+           * npubinfmt v3; this format supports TRIV2 models.
+           * segment_num should be equal to or greater than input_seg_num and output_seg_num.
+           * The segment index and offset of tensors describe locations where they are stored.
+           * FYI, TRIV2 supports two quantization methods: QASYMM8 and QSYMM16.
+           */
+
+          /** segments to hold input/weight/output data */
+          uint32_t segment_num;
+          /**< number of total segments */
+          uint32_t segment_size[MAX_SEGMENTS];
+          /**< size of each segment */
+
+          /** model weight segment */
+          uint32_t weight_seg_idx;
+          /**< segment index for model weight */
+
+          /** input tensor segment */
+          uint32_t input_seg_num;
+          /**< Number of input tensors (<= MAX_TENSORS) */
+          uint32_t input_seg_idx[MAX_TENSORS];
+          /**< segment index for input tensors */
+          uint32_t input_seg_off[MAX_TENSORS];
+          /**< segment offset for input tensors */
+          uint32_t input_seg_dims[MAX_TENSORS][MAX_RANK];
+          /**< input tensor dimensions (N, H, W, C) */
+
+          uint32_t input_seg_emod_y[MAX_TENSORS];
+          /**< input tensor addressing info (emod_y) */
+          uint32_t input_seg_emod_z[MAX_TENSORS];
+          /**< input tensor addressing info (emod_z) */
+
+          data_type input_seg_quant_type[MAX_TENSORS];
+          /**< input quantization data type */
+          int32_t input_seg_quant_z[MAX_TENSORS];
+          /**< input quantization parameter (zero-point) */
+          float input_seg_quant_s[MAX_TENSORS];
+          /**< input quantization parameter (scale) */
+
+          /** output tensor segment */
+          uint32_t output_seg_num;
+          /**< Number of output tensors (<= MAX_TENSORS) */
+          uint32_t output_seg_idx[MAX_TENSORS];
+          /**< segment index for output tensors */
+          uint32_t output_seg_off[MAX_TENSORS];
+          /**< segment offset for output tensors */
+          uint32_t output_seg_dims[MAX_TENSORS][MAX_RANK];
+          /**< output tensor dimensions (N, H, W, C) */
+
+          uint32_t output_seg_emod_y[MAX_TENSORS];
+          /**< output tensor addressing info (emod_y) */
+          uint32_t output_seg_emod_z[MAX_TENSORS];
+          /**< output tensor addressing info (emod_z) */
+
+          data_type output_seg_quant_type[MAX_TENSORS];
+          /**< output quantization data type */
+          int32_t output_seg_quant_z[MAX_TENSORS];
+          /**< output quantization parameter (zero-point) */
+          float output_seg_quant_s[MAX_TENSORS];
+          /**< output quantization parameter (scale) */
+
+          /** DSP-specific variables */
+          uint32_t dspm_size;
+          /**< The data scratch-pad memory (dspm) size of the DSP */
+
+          /** vISA instructions */
+          uint32_t visa_num_plus_one;
+          /**< Number of vISA insts + 1 (i.e., unknown if zero) */
+
+          data_layout input_seg_layout[MAX_TENSORS];
+          /**< Model-expected data layout for input tensors */
+          data_layout output_seg_layout[MAX_TENSORS];
+          /**< Model-expected data layout for output tensors */
+        };
+        /** If npubinfmt should be changed, append here as another version */
+      };
+    };
+    char reserved_npu_engine[3072]; /**< Ensure NPU-Engine part is 3072 bytes */
+  };
+  char reserved_compiler[1024]; /**< Reserved for NPU Compiler */
+  char reserved_extended[];     /**< Reserved for future; zero-length array */
+} __attribute__ ((packed, aligned)) npubin_meta;
+
+/**
+ * @brief Extended metadata for profiling (obtained from a compiler)
+ */
+typedef struct {
+  uint32_t total_size;
+  /**< actual size of extended metadata, not 4K-multiplied one */
+
+  uint32_t node_table_size;
+  /**< size of a node table (node ID to node name) */
+  uint32_t visa_table_size;
+  /**< size of a visa table (visa ID to node ID) */
+
+  uint32_t node_entry_num;
+  /**< total number of node entries */
+  uint32_t visa_entry_num;
+  /**< total number of visa entries (i.e., number of vISA insts) */
+
+  /**
+   * The below shows the data format that 'entry_data' should have (little endian).
+   *
+   * 'entry_data'
+   * - [ node_table : 'node_table_size' Bytes ][ visa_table : 'visa_table_size' Bytes ]
+   *
+   * 'node_table'
+   * - [ node_id : uint32_t ][ length : uint32_t ][ data : 'length' Bytes ]
+   * - if 'data' has a string type, it should be null-terminated and its 'length' includes it.
+   *
+   * 'visa_table'
+   * - [ visa_id : uint32_t ][ node_num : uint32_t ][ node_ids : node_num * uint32_t ]
+   * - if 'node_num' is zero, 'node_ids' would be empty.
+   */
+  char entry_data[];
+} __attribute__ ((packed, aligned)) npubin_meta_profile;
+
+/* Compile-time assert. From http://www.pixelbeat.org/programming/gcc/static_assert.html */
+#define ASSERT_CONCAT_(a, b) a##b
+#define ASSERT_CONCAT(a, b) ASSERT_CONCAT_ (a, b)
+#define ct_assert(e) enum { ASSERT_CONCAT (assert_line_, __LINE__) = 1 / (!!(e)) }
+
+ct_assert (sizeof (npubin_meta) == NPUBIN_META_SIZE);
+
+#endif /* NPU_BIN_FMT_H__ */
diff --git a/include/typedef.h b/include/typedef.h
new file mode 100644 (file)
index 0000000..aa04988
--- /dev/null
@@ -0,0 +1,437 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+/**
+ * Copyright (C) 2019 Samsung Electronics
+ * Copyright (C) 2019 MyungJoo Ham <myungjoo.ham@samsung.com>
+ */
+/**
+ * @file typedef.h
+ * @date 15 May 2019
+ * @brief Internal API to access global configuration of NE.
+ * @see https://code.sec.samsung.net/confluence/display/ODLC/2020+Overall+Software+Stack
+ * @author MyungJoo Ham <myungjoo.ham@samsung.com>
+ * @bug No known bugs except for NYI items
+ *
+ * To packagers: this is internal header for NE. Don't export this.
+ */
+
+#ifndef NPU_TYPEDEF_H__
+#define NPU_TYPEDEF_H__
+
+#include <stdint.h>
+
+/** Deprecated devices */
+#define DEVICETYPE_DEPR (0x10000)
+#define DEVICETYPE_ASR DEVICETYPE_DEPR
+#define DEVICETYPE_TRIV DEVICETYPE_DEPR
+#define DEVICETYPE_TRIA DEVICETYPE_DEPR
+
+#define DEVICETYPE_TRIV2 (0x40000) /** VD/SR-NPU 2020 */
+/** TODO: there will be other device variants */
+#define DEVICETYPE_MASK (0xFFFF0000)
+#define DEVICEVERSION_MASK (0x0000FFFF)
+
+#define DEVICEGROUP_SOCIP (0x00000100)
+
+/**
+ * @brief Represents an "opened" single NPU device (e.g., TRIV2).
+ */
+typedef void *npudev_h;
+
+/**
+ * @brief Description of npu device types.
+ */
+typedef enum {
+  NPUCOND_CONN_UNKNOWN = DEVICETYPE_TRIV2,
+  /* TRIV2.3 */
+  NPUCOND_TRIV2_CONN_UNKNOWN = (DEVICETYPE_TRIV2 | 0),
+  NPUCOND_TRIV2_CONN_SOCIP = (DEVICETYPE_TRIV2 | 2),
+  NPUCOND_TRIV23_CONN_UNKNOWN = (DEVICETYPE_TRIV2 | 0x4),
+  NPUCOND_TRIV23_CONN_SOCIP = (DEVICETYPE_TRIV2 | 0x8),
+  /* TRIV2.4 */
+  NPUCOND_TRIV24_CONN_UNKNOWN = (DEVICETYPE_TRIV2 | 0x10),
+  NPUCOND_TRIV24_CONN_SOCIP = (DEVICETYPE_TRIV2 | 0x20),
+  /* deprecated */
+  NPUCOND_TRIV_CONN_UNKNOWN = (DEVICETYPE_TRIV | 0),
+  NPUCOND_TRIV_CONN_SOCIP = (DEVICETYPE_TRIV | 2),
+  NPUCOND_TRIA_CONN_UNKNOWN = (DEVICETYPE_TRIA | 0),
+  NPUCOND_TRIA_CONN_SOCIP = (DEVICETYPE_TRIA | 2),
+} dev_type;
+
+/**
+ * @brief Description of data layout.
+ */
+typedef enum {
+  DATA_LAYOUT_NONE = 0, /**< undefined layout, regarded as TRIV2 */
+  DATA_LAYOUT_NHWC,     /**< standard layout, NHWC */
+  DATA_LAYOUT_NCHW,     /**< standard layout, NCHW */
+  DATA_LAYOUT_TRIV,     /**< customized layout for TRIV1 (based on NHWC) */
+  DATA_LAYOUT_SRNPU = DATA_LAYOUT_TRIV,
+  /**< alias for backward-compatibility */
+  DATA_LAYOUT_TRIV2, /**< customized layout for TRIV2 (based on NHWC) */
+  DATA_LAYOUT_MODEL, /**< use the same data layout specified in model metadata */
+  DATA_LAYOUT_RAW,   /**< any raw data. npu-engine does not care about its layout */
+} data_layout;
+
+/**
+ * @brief Description of data type.
+ */
+typedef enum {
+  /* 8 bits */
+  DATA_TYPE_SRNPU = 0, /**< integer-arithmetic-only quantization (TRIV1) */
+  DATA_TYPE_QASYMM8,   /**< 8-bit asymmetric quantization (TRIV2) */
+  DATA_TYPE_INT8,
+  DATA_TYPE_UINT8,
+  /* 16 bits */
+  DATA_TYPE_QSYMM16, /**< 16-bit symmetric quantization (TRIV2) */
+  DATA_TYPE_INT16,
+  DATA_TYPE_UINT16,
+  /* 32 bits */
+  DATA_TYPE_INT32,
+  DATA_TYPE_UINT32,
+  DATA_TYPE_FLOAT32,
+  /* 64 bits */
+  DATA_TYPE_INT64,
+  DATA_TYPE_UINT64,
+  DATA_TYPE_FLOAT64,
+  /* unknown */
+  DATA_TYPE_MODEL, /**< use the same data type specified in model metadata */
+} data_type;
+
+/* NNstreamer/GStreamer supports up to 16 bufs */
+#define NPU_TENSOR_CARDINALITY_MAX (16)
+
+/** @brief type of memory represented by the buffer */
+typedef enum {
+  BUFFER_FILE = 0, /**< buffer is a file */
+  BUFFER_MAPPED,   /**< buffer is a memory-mapped ptr */
+  BUFFER_DMABUF,   /**< buffer is a dmabuf fd, representing contiguous memory */
+  BUFFER_UNDEFINED /**< buffer type is undefined */
+} buffer_types;
+
+/**
+ * @brief Various kinds of buffer supported for input/output/model
+ * @note In case of BUFFER_FILE, 'size' indicates a file size.
+ */
+typedef struct {
+  union {
+    struct {                /** BUFFER_FILE */
+      const char *filepath; /**< The filepath for the data */
+      int fd;               /**< The file descriptor (optional) */
+    };
+    struct {             /** BUFFER_MAPPED/DMABUF */
+      void *addr;        /**< Mapped address of the buffer */
+      struct {           /** BUFFER_DMABUF only */
+        int dmabuf;      /**< The dma-buf fd handle of the memory allocated */
+        uint64_t offset; /**< Offset to be applied to the base memory address */
+      };
+    };
+  };
+  uint64_t size;     /**< The size of the buffer in bytes */
+  buffer_types type; /**< Type of memory in this buffer */
+} generic_buffer;
+
+/**
+ * @brief Multiple buffers to contain dis-contiguous tensor buffers.
+ *        Each generic buffer can have different buffer types.
+ */
+typedef struct {
+  uint32_t num_buffers;                            /**< The number of tensors in this "buffer" */
+  generic_buffer bufs[NPU_TENSOR_CARDINALITY_MAX]; /**< Buffer struct for each tensor */
+} generic_buffers;
+
+/**
+ * @brief NPU input/output buffers are compatible with arbitrary other/tensors.
+ */
+typedef generic_buffers input_buffers;
+typedef generic_buffers output_buffers;
+
+/**
+ * @brief Description of tensor data info.
+ */
+typedef struct {
+  data_layout layout;
+  data_type type;
+} tensor_data_info;
+
+/**
+ * @brief Each tensor may have different data layout and type
+ */
+typedef struct {
+  uint32_t num_info;
+  tensor_data_info info[NPU_TENSOR_CARDINALITY_MAX];
+} tensors_data_info;
+
+/**
+ * @brief Receives NPU results.
+ * @param[in] output NPU results. callback() should free this.
+ * @param[in] req_id The request id returned with runNPU_*.
+ * @param[in] data The data given as a parameter to the runNPU_* call.
+ *
+ * @detail The callback function has the responsibility to free the given
+ *         output buffer (output) when its type is BUFFER_MAPPED.
+  */
+typedef void (*npuOutputNotify) (output_buffers *output, int req_id, void *data);
+
+/**
+ * @brief deprecated mode.
+ */
+typedef enum {
+  NPUASYNC_DROP_OLD, /**< If there is an unprocessed input data
+                       in the double buffer, overwrite it */
+  NPUASYNC_WAIT,     /**< If there is an unprocessed input data in
+                   the double buffer, wait for it processed */
+  NPUASYNC_DROP_NEW, /**< If there is an unprocessed input data in
+                       the double buffer, return error "RETRY" */
+} npu_async_mode;
+
+/**
+ * @brief NPU model inference mode (used in runNPU_model)
+ */
+typedef enum {
+  NPU_INFER_BLOCKING = 0, /**< Blocking. Wait for NPU to finish inference */
+  NPU_INFER_NON_BLOCKING, /**< Non-blocking. Invoke a callback when available */
+} npu_infer_mode;
+
+/**
+ * @brief Description of priority for NPU inference requests
+ * @details NPU Engine currently supports three priorities; low, mid, and high.
+ *          requests with higher priority are always handled preferentially than
+ *          other requests. FIFO is used among the same priority requests.
+ */
+typedef enum {
+  NPU_PRIORITY_LOW = 0,
+  /**< Low priority: requests could be delayed or canceled */
+  NPU_PRIORITY_MID = 1,
+  /**< Mid priority: requests could be slightly delayed */
+  NPU_PRIORITY_HIGH = 2,
+  /**< High priority: requests should be issued immediately */
+  NPU_PRIORITY_PROFILE = NPU_PRIORITY_HIGH /**< Deprecated */
+} npu_priority;
+
+/**
+ * @brief Description of output notification mode for NPU inference requests
+ * @note in case of high priority apps, this mode is ignored. Output is handled by
+ *       third-party hardware.
+ */
+typedef enum {
+  NPU_INTERRUPT = 0, /**< interrupt: moderate latency but save CPU usage (default) */
+  NPU_POLLING = 1,   /**< polling: consume CPU usage but achieve low latency */
+} npu_notimode;
+
+/**
+ * @brief Constraints for NPU inferences (per model)
+ */
+typedef struct {
+  uint32_t timeout_ms;
+  npu_priority priority;
+  npu_notimode notimode;
+} npu_constraint;
+
+/* deprecated, for backward-compatibility */
+#define npuConstraint npu_constraint
+
+/**
+ * @brief Describes the supported NPU Scheduler (in kernel modules)
+ */
+typedef enum { NPU_SCHEDULER_UNKNOWN = 0, NPU_SCHEDULER_SR, NPU_SCHEDULER_VD } npu_scheduler;
+
+/** @brief optional parameter for npu scheduler */
+typedef void *npu_scheduler_param;
+
+static const uint32_t default_timeout = 3000;
+static const npu_priority default_priority = NPU_PRIORITY_MID;
+static const npu_notimode default_notimode = NPU_INTERRUPT;
+static const npu_scheduler default_scheduler = NPU_SCHEDULER_SR;
+static const uint32_t default_tops = 4;
+
+/**
+ * @brief Description of npu device status
+ */
+typedef enum {
+  NPU_ERROR = 0, /**< The NPU is not available for now (check the log file for details) */
+  NPU_READY = 1, /**< The NPU is available but the request can be delayed due to others */
+  NPU_IDLE = 2,  /**< The NPU is available and there's no active request currently */
+} npu_status;
+
+/**
+ * @brief Operable modes of NPU when the inputs are from NPU's own hardware.
+ * @note this mode will decide which input service performs the inference of a model.
+ */
+typedef enum {
+  NPUINPUT_STOP = 0,         /**< Stop Processing */
+  NPUINPUT_INTERNAL_CAM = 1, /**< Let ADSP preprocess image stream from MIPI and send it to
+                                   NPU-core to be processed with the given model.
+                                   (not supported yet) */
+  NPUINPUT_I2S_MIC = 2,      /**< ASR mode with on-chip internal I2S. modelid is ignored.
+                                   (not supported yet) */
+  NPUINPUT_HOST = 3,         /**< Process input frames transmitted from Host.
+                                   TRIA, TRIV, and TRIV2 may use this. */
+  NPUINPUT_HW_RECURRING = 4, /**< Process input frames transmitted from third-party HW.
+                                   TRIV2 may use this for high-priority models. */
+} npu_input_opmode;
+
+#define IS_DEVICE \
+  (val, devname) (((val) &DEVICETYPE_MASK) == DEVICETYPE_##devname) /** E.g., IS_DEVICE(dev, ASR) */
+
+/**
+ * @brief Various device types to be supported for running models
+ */
+typedef enum {
+  SMODEL_OPS_NPU = 0,          /**< NPU model (TRIV/TRIV2) */
+  SMODEL_OPS_ARM = 1,          /**< ARM model (not supported yet) */
+  SMODEL_OPS_INTERNAL_CAM = 2, /**< Internal Camera model (not supported yet) */
+  SMODEL_OPS_I2S_MIC = 3,      /**< Internal Mic model (not supported yet) */
+  SMODEL_OPS_NPU_ASR = 4,      /**< ASR model (TRIA) */
+  SMODEL_OPS_END,
+} model_opmode;
+
+/** NPU Statistics (only for real-device envionment) */
+
+/**
+ * @brief Description of npu app (per device open) status
+ */
+typedef enum {
+  NPU_APP_UNKNOWN = 0,
+  NPU_APP_ERROR = 1,
+  NPU_APP_PENDING = 2,
+  NPU_APP_STARTED = 3,
+  NPU_APP_TERMINATED = 4,
+} npu_app_status;
+
+/**
+ * @brief Description of npu request (per inference) status
+ */
+typedef enum {
+  NPU_REQ_UNKNOWN = 0,
+  NPU_REQ_ERROR = 1,
+  NPU_REQ_PENDING = 2,
+  NPU_REQ_RUNNING = 3,
+  NPU_REQ_FINISHED = 4,
+} npu_req_status;
+
+typedef struct {
+  int app_id;
+  char name[16];
+
+  npu_app_status status;
+
+  uint32_t num_total_reqs;
+  uint32_t num_active_reqs;
+
+  uint64_t total_alloc_mem;
+  uint64_t total_freed_mem;
+} npu_stat_app;
+
+typedef struct {
+  uint32_t num;
+  npu_stat_app *stat;
+} npu_stat_apps;
+
+typedef struct {
+  int req_id;
+  uint64_t model_id;
+
+  npu_priority priority;
+  npu_req_status status;
+
+  uint32_t sched_time;
+  uint32_t infer_time;
+} npu_stat_req;
+
+typedef struct {
+  uint32_t num;
+  npu_stat_req *stat;
+} npu_stat_reqs;
+
+/**
+ * NPU Profiling (both for emulated/real-device envionment)
+ *
+ * Note that negative values mean non-supported profiling info.
+ */
+
+#define NPU_PROFILE_SIZE (256)
+#define NPU_OPNAME_MAX (128)
+
+/**
+ * Note that the below structure is shared among kernel/decoder and emulation codes.
+ * Please make sure any updates do not hurt the exising SW stack.
+ */
+typedef struct {
+  union {
+    struct {
+      char name[NPU_OPNAME_MAX]; /**< node/vISA name (null-terminated) */
+
+      int64_t running_cycles;
+
+      int64_t dram_read_bytes;
+      int64_t dram_write_bytes;
+
+      int64_t sram_read_bytes;
+      int64_t sram_write_bytes;
+
+      int64_t start_cycles;
+      int64_t end_cycles;
+
+      union {
+        /* layer-level profiling specific info. */
+        struct {
+          int32_t node_id; /**< node id ('-1' means unclassified node) */
+        } __attribute__ ((packed, aligned));
+        /* vISA-level profiling specific info. */
+        struct {
+          uint32_t visa_opcode;  /**< viSA opcode */
+          int64_t visa_prog_seq; /**< vISA program sequence */
+          int64_t visa_exec_seq; /**< vISA execution sequence (global) */
+        } __attribute__ ((packed, aligned));
+        /* vISA-level Data-level Parallelism profiling specific info. */
+        /**
+         * vISA instruction may be divided into two or more instructions with divided data
+         * which introduce parallel execution.
+         * visa_dlp_xxx contains each divided instruction's profiling information,
+         * while visa_xxx contains the original vISA instruction's profiling data.  
+         */
+        struct {
+          uint32_t visa_dlp_opcode;  /**< viSA dlp opcode */
+          int64_t visa_dlp_prog_seq; /**< vISA dlp program sequence */
+          int64_t visa_dlp_exec_seq; /**< vISA dlp execution sequence (global) */
+        } __attribute__ ((packed, aligned));
+      };
+      /** TODO: Add more info */
+    } __attribute__ ((packed, aligned));
+    char reserved[NPU_PROFILE_SIZE];
+  };
+} npu_profile_layer;
+
+typedef struct {
+  char *prof_path; /**< Profile data path (.rec file, if exists) */
+  uint32_t num_layers;
+
+  int64_t total_system_cycles;
+  int64_t dram_input_footprint;
+  int64_t dram_output_footprint;
+
+  npu_profile_layer *layers;
+} npu_profile;
+
+typedef enum {
+  PROFILE_LEVEL_EXT_META = 0, /**< Depends on extended metadata */
+  PROFILE_LEVEL_VISA,         /**< vISA-level profiling */
+  PROFILE_LEVEL_VISA_DLP,     /**< vISA-level DLP profiling */
+  PROFILE_LEVEL_LAYER,        /**< Layer-level profiling (requires the extended meta) */
+  PROFILE_LEVEL_MAX
+} profile_level_type;
+
+typedef struct {
+  profile_level_type level;
+  /* TODO: Add more options if needed */
+} npu_profile_opt;
+
+typedef enum {
+  NPU_LOG_NONE = 0,
+  NPU_LOG_ERROR,
+  NPU_LOG_WARN,
+  NPU_LOG_INFO,
+  NPU_LOG_END,
+} npu_loglevel;
+
+#endif /* NPU_TYPEDEF_H__ */
diff --git a/meson.build b/meson.build
new file mode 100644 (file)
index 0000000..ecd6106
--- /dev/null
@@ -0,0 +1,32 @@
+project('npubin-fmt', [],
+  version: '1.0.0',
+  license: ['Apache-2.0'],
+  meson_version: '>=0.47.0',
+  default_options: [
+  ]
+)
+
+prefix = get_option('prefix')
+libdir = join_paths(prefix, get_option('libdir'))
+includedir = join_paths(prefix, get_option('includedir'))
+
+# Set configuration to install .ini
+install_conf = configuration_data()
+
+install_conf.set('NPUBIN_FMT_VERSION', meson.project_version())
+install_conf.set('PREFIX', prefix)
+install_conf.set('LIB_INSTALL_DIR', libdir)
+install_conf.set('INCLUDE_INSTALL_DIR', includedir)
+
+configure_file(input: 'npubin-fmt.pc.in', output: 'npubin-fmt.pc',
+  install_dir: join_paths(libdir, 'pkgconfig'),
+  configuration: install_conf)
+
+
+headers = [
+  'include/npubinfmt.h',
+  'include/typedef.h',
+]
+
+install_headers(headers, subdir: 'npubin-fmt')
+
diff --git a/npubin-fmt.pc.in b/npubin-fmt.pc.in
new file mode 100644 (file)
index 0000000..95f94f4
--- /dev/null
@@ -0,0 +1,10 @@
+# Package Information for pkg-config, for SR NPU developers
+
+prefix=@PREFIX@
+includedir=@INCLUDE_INSTALL_DIR@
+
+Name: npubin-fmt
+Description: NPU Metadata Format for Samsung Research NPU
+Version: @NPUBIN_FMT_VERSION@
+Requires:
+Cflags: -I${includedir}/npubin-fmt
diff --git a/packaging/npubin-fmt.manifest b/packaging/npubin-fmt.manifest
new file mode 100644 (file)
index 0000000..017d22d
--- /dev/null
@@ -0,0 +1,5 @@
+<manifest>
+ <request>
+    <domain name="_"/>
+ </request>
+</manifest>
diff --git a/packaging/npubin-fmt.spec b/packaging/npubin-fmt.spec
new file mode 100644 (file)
index 0000000..7aea7d3
--- /dev/null
@@ -0,0 +1,44 @@
+Name:          npubin-fmt
+Summary:       SR NPU binary metadata format
+Version:       1.0.0
+Release:       0
+Group:         System Environment/Daemons
+Packager:      MyungJoo Ham <myungjoo.ham@samsung.com>
+License:       Apache-2.0
+Source0:       npubin-fmt-%{version}.tar.gz
+Source1001:    npubin-fmt.manifest
+
+BuildRequires: meson
+BuildRequires: ninja
+BuildRequires: pkg-config
+
+%prep
+%setup -q
+cp %{SOURCE1001} .
+
+%build
+meson --buildtype=plain --bindir=%{neexampledir} --prefix=%{_prefix} \
+      --libdir=%{_libdir} --includedir=%{_includedir} \
+      --datadir=%{_datadir} --sysconfdir=%{_sysconfdir} \
+      build
+ninja -C build %{?_smp_mflags}
+
+%install
+DESTDIR=%{buildroot} ninja install -C build %{?_smp_mflags}
+
+%description
+This provides npu metadata information for NPU.
+
+%files
+%manifest npubin-fmt.manifest
+%defattr(-,root,root,-)
+%{_includedir}/npubin-fmt/npubinfmt.h
+%{_includedir}/npubin-fmt/typedef.h
+%{_libdir}/pkgconfig/npubin-fmt.pc
+
+%changelog
+* Thu Jun 13 2023 Jiho Chu <jiho.chu@samsung.com>
+- Release of 1.0.0
+
+* Thu Jun 13 2023 MyungJoo Ham <myungjoo.ham@samsung.com>
+- Release of 0.0.1