--- /dev/null
+Language: Cpp
+Standard: Cpp11
+TabWidth: 2
+UseTab: Never
+BasedOnStyle: Google
+SortIncludes: false
+CommentPragmas: '\* @'
+ReflowComments: false
+AllowShortFunctionsOnASingleLine: Inline
+AllowShortIfStatementsOnASingleLine: false
+AlwaysBreakAfterReturnType: TopLevelDefinitions
+SpaceBeforeParens: Always
+SpaceAfterCStyleCast: true
+Cpp11BracedListStyle: true
#define NPUBIN_META_NUM_EXTENDED(magiccode) (((magiccode) >> 8) & 0xFFULL)
/* @brief return extended metadata size of npu binary */
-static inline uint64_t NPUBIN_META_EXTENDED_SIZE (uint64_t magiccode) {
+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 */
+ assert (num_extended <= UINT8_MAX); /** sanity check for svace */
return num_extended * NPUBIN_META_SIZE;
}
/* @brief return total metadata size of npu binary, including extended data */
-static inline uint64_t NPUBIN_META_TOTAL_SIZE (uint64_t magiccode) {
+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 */
+ 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 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_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)
+#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)
+#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" *
* 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 {
* 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 */
+ uint64_t magiccode; /**< npubinfmt's magiccode */
/**
* Descriptions for npu_version (64 bits)
*
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; /**< 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 dummy; /**< FIXME: Dummy field to keep an aligned offset */
+ 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 */
+ uint32_t dummy; /**< FIXME: Dummy field to keep an aligned offset */
/**
* If input comes from host, the buffer may be filled with input data.
* 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 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 */
+ uint64_t program_size; /**< Size of the program list instructions */
+ uint64_t weight_size; /**< Size of the model weights */
union {
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_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_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_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) */
+ 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) */
+ 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 {
/**
*/
/** 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 */
+ 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 */
+ 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) */
+ 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) */
+ 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 */
+ 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) */
+ uint32_t
+ visa_num_plus_one; /**< Number of vISA insts + 1 (i.e., unknown if zero) */
};
/** If npubinfmt should be changed, append here as another version */
};
};
char reserved_compiler[1024]; /**< Reserved for NPU Compiler */
char reserved_extended[]; /**< Reserved for future; zero-length array */
-} __attribute__((packed, aligned)) npubin_meta;
+} __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
+ 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) */
+ 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).
* - if 'node_num' is zero, 'node_ids' would be empty.
*/
char entry_data[];
-} __attribute__((packed, aligned)) npubin_meta_profile;
+} __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)) }
+#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);
+ct_assert (sizeof (npubin_meta) == NPUBIN_META_SIZE);
#endif /* NPU_BIN_FMT_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_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 */
+#define DEVICETYPE_TRIV2 (0x40000) /** VD/SR-NPU 2020 */
/** TODO: there will be other device variants */
-#define DEVICETYPE_MASK (0xFFFF0000)
+#define DEVICETYPE_MASK (0xFFFF0000)
/**
* @brief Represents an "opened" single NPU device (e.g., TRIV2).
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 */
+ /**< 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;
/**
*/
typedef enum {
/* 8 bits */
- DATA_TYPE_SRNPU = 0, /**< integer-arithmetic-only quantization (TRIV1) */
- DATA_TYPE_QASYMM8, /**< 8-bit asymmetric quantization (TRIV2) */
+ 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_QSYMM16, /**< 16-bit symmetric quantization (TRIV2) */
DATA_TYPE_INT16,
DATA_TYPE_UINT16,
/* 32 bits */
DATA_TYPE_UINT64,
DATA_TYPE_FLOAT64,
/* unknown */
- DATA_TYPE_MODEL, /**< use the same data type specified in model metadata */
+ 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)
+#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_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;
/**
*/
typedef struct {
union {
- struct { /** BUFFER_FILE */
- const char *filepath; /**< The filepath for the data */
+ struct { /** BUFFER_FILE */
+ const char *filepath; /**< The filepath for the data */
};
- 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 */
+ 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 */
+ uint64_t size; /**< The size of the buffer in bytes */
+ buffer_types type; /**< Type of memory in this buffer */
} generic_buffer;
/**
*/
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_buffer
+ bufs[NPU_TENSOR_CARDINALITY_MAX]; /**< Buffer struct for each tensor */
} generic_buffers;
/**
* Approach 3: 1 + 2.
*/
typedef void (*npuOutputNotify) (output_buffers *output, uint64_t sequence,
- void *data);
+ void *data);
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
+ 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" */
* 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_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;
/**
* 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_INTERRUPT =
+ 0, /**< interrupt: moderate latency but save CPU usage (default) */
+ NPU_POLLING = 1, /**< polling: consume CPU usage but achieve low latency */
} npu_notimode;
/**
* @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_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;
/**
* @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
+ 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.
+ 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.
+ 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.
+ 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) */
+#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_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;
*/
#define NPU_PROFILE_SIZE (256)
-#define NPU_OPNAME_MAX (128)
+#define NPU_OPNAME_MAX (128)
typedef struct {
union {
struct {
- char name[NPU_OPNAME_MAX]; /**< node name (null-terminated) */
- int32_t node_id; /**< node id ('-1' means unclassified node) */
+ char name[NPU_OPNAME_MAX]; /**< node name (null-terminated) */
+ int32_t node_id; /**< node id ('-1' means unclassified node) */
int64_t running_cycles;
int64_t sram_read_bytes;
int64_t sram_write_bytes;
- int64_t visa_exec_seq; /**< vISA global execution sequence */
+ int64_t visa_exec_seq; /**< vISA global execution sequence */
int64_t start_cycles;
int64_t end_cycles;
/** TODO: Add more info */
- } __attribute__((packed, aligned));
+ } __attribute__ ((packed, aligned));
char reserved[NPU_PROFILE_SIZE];
};
} npu_profile_layer;
typedef struct {
- char *prof_path; /**< Profile data path (.rec file, if exists) */
+ char *prof_path; /**< Profile data path (.rec file, if exists) */
uint32_t num_layers;
int64_t total_system_cycles;
*
* @note the caller needs to free the metadata
*/
-npubin_meta * getNPUmodel_metadata(const char *model, bool need_extra);
+npubin_meta *getNPUmodel_metadata (const char *model, bool need_extra);
/**
* @brief Send the NN model to NPU.
* embedded in devices, you do not need to call register and
* register calls for ASR are ignored.
*/
-int registerNPUmodel(npudev_h dev, generic_buffer *modelfile, uint32_t *model_id);
+int registerNPUmodel (npudev_h dev, generic_buffer *modelfile,
+ uint32_t *model_id);
/**
* @brief Remove the NN model from NPU
* @return @c 0 if no error. otherwise a negative error value
* @detail This may incur some latency with memory compatcion.
*/
-int unregisterNPUmodel(npudev_h dev, uint32_t model_id);
+int unregisterNPUmodel (npudev_h dev, uint32_t model_id);
/**
* @brief Remove all NN models from NPU
* @param[in] dev The NPU device handle
* @return @c 0 if no error. otherwise a negative error value
*/
-int unregisterNPUmodel_all(npudev_h dev);
+int unregisterNPUmodel_all (npudev_h dev);
/**
* @brief [OPTIONAL] Set the data layout for input/output tensors
* @return @c 0 if no error. otherwise a negative error value
* @note if this function is not called, default layout/type will be used.
*/
-int setNPU_dataInfo(npudev_h dev, uint32_t model_id,
- tensors_data_info *info_in, tensors_data_info *info_out);
+int setNPU_dataInfo (npudev_h dev, uint32_t model_id,
+ tensors_data_info *info_in, tensors_data_info *info_out);
/**
* @brief [OPTIONAL] Set the inference constraint for next NPU inferences
* @return @c 0 if no error. otherwise a negative error value
* @note If this function is not called, default values are used.
*/
-int setNPU_constraint(npudev_h dev, uint32_t model_id, npuConstraint constraint);
+int setNPU_constraint (npudev_h dev, uint32_t model_id,
+ npuConstraint constraint);
/**
* @brief Execute inference. Wait (block) until the output is available.
* @detail This is a syntactic sugar of runNPU_async().
* CAUTION: There is a memcpy for the output buffer.
*/
-int runNPU_sync(npudev_h dev, uint32_t model_id, const input_buffers *input,
- output_buffers *output);
+int runNPU_sync (npudev_h dev, uint32_t model_id, const input_buffers *input,
+ output_buffers *output);
/**
* @brief Let NPU accept input frames from its internal source continuously
* @return @c positive id if no error. otherwise a negative error value
* @note input and output are handled internally by third-party HW (e.g., DSP)
*/
-int runNPU_internalInput(npudev_h dev, uint32_t model_id, npu_input_opmode opmode,
- const char * hw_dev);
+int runNPU_internalInput (npudev_h dev, uint32_t model_id,
+ npu_input_opmode opmode, const char *hw_dev);
/**
* @brief Stop the request with the given id
* @param[in] id The request id
* @return @c 0 if no error. otherwise a negative error value
*/
-int stopNPU_internalInput(npudev_h dev, int id);
+int stopNPU_internalInput (npudev_h dev, int id);
/**
* @brief Invoke NPU inference. Unblocking call.
* @param[in] mode Configures how this operation works.
* @return @c positive id if no error. otherwise a negative error value
*/
-int runNPU_async(npudev_h dev, uint32_t model_id, const input_buffers *input,
- npuOutputNotify cb, uint64_t *sequence, void *data,
- npu_async_mode mode);
+int runNPU_async (npudev_h dev, uint32_t model_id, const input_buffers *input,
+ npuOutputNotify cb, uint64_t *sequence, void *data,
+ npu_async_mode mode);
/**
* @brief get the current memory status for the given device
* @param[out] free_total The size of freed memory until now
* @return @c 0 if no error. otherwise a negatice error value
*/
-int getNPU_memoryStatus(npudev_h dev, size_t *alloc_total, size_t *free_total);
+int getNPU_memoryStatus (npudev_h dev, size_t *alloc_total, size_t *free_total);
/**
* @brief Get the current device status to be used
* @param[out] num_requests the number of running requests (or pending)
* @return 0 if no error, otherwise a negative errno.
*/
-int getNPU_deviceStatus(npudev_h dev, npu_status *status, uint32_t *num_requests);
+int getNPU_deviceStatus (npudev_h dev, npu_status *status,
+ uint32_t *num_requests);
/**
* [IMPORTANT] Descriptions for buffer allocation APIs.
* @return 0 if no error, otherwise a negative errno.
* @note it reuses allocInputBuffer().
*/
-int allocNPU_inputBuffers (npudev_h dev, input_buffers * input);
+int allocNPU_inputBuffers (npudev_h dev, input_buffers *input);
/**
* @brief Free input buffers allocated by allocInputBuffers().
* @note it reuses cleanInputbuffer().
* @return 0 if no error, otherwise a negative errno.
*/
-int cleanNPU_inputBuffers (npudev_h dev, input_buffers * input);
+int cleanNPU_inputBuffers (npudev_h dev, input_buffers *input);
/**
* @brief Allocate a generic buffer with the requested buffer type.
* @param[in/out] Buffer the buffer pointer where memory is allocated.
* @return 0 if no error, otherwise a negative errno.
*/
-int allocNPU_genericBuffer (npudev_h dev, generic_buffer * buffer);
+int allocNPU_genericBuffer (npudev_h dev, generic_buffer *buffer);
/**
* @brief Free the generic buffer and remove the address mapping
* @param[in] buffer the model buffer
* @return 0 if no error, otherwise a negative errno.
*/
-int cleanNPU_genericBuffer (npudev_h dev, generic_buffer * buffer);
+int cleanNPU_genericBuffer (npudev_h dev, generic_buffer *buffer);
/**
* @brief Allocate generic buffers with the requested buffer type.
* @param[in/out] Buffer the buffer pointer where memory is allocated.
* @return 0 if no error, otherwise a negative errno.
*/
-int allocNPU_genericBuffers (npudev_h dev, generic_buffers * buffers);
+int allocNPU_genericBuffers (npudev_h dev, generic_buffers *buffers);
/**
* @brief Free generic buffers allocated by allocGenericBuffers().
* @param[in/out] input input buffers.
* @return 0 if no error, otherwise a negative errno.
*/
-int cleanNPU_genericBuffers (npudev_h dev, generic_buffers * buffers);
+int cleanNPU_genericBuffers (npudev_h dev, generic_buffers *buffers);
/**
* @brief Get the profile information from NPU
#include <functional>
/** @brief the binded data manipulation function */
-typedef std::function<size_t (void*, void*, size_t)> dataManipFunc;
+typedef std::function<size_t (void *, void *, size_t)> dataManipFunc;
/** @brief communication plugin: singleton */
class CommPlugin {
- public:
- static CommPlugin& getCommPlugin();
+ public:
+ static CommPlugin &getCommPlugin ();
- /** host -> npu-engine */
- virtual int extractGenericBuffer (const generic_buffer *buf, void *data,
- dataManipFunc func = nullptr, size_t offset = 0, size_t size = 0) = 0;
- /** npu-engine -> host */
- virtual int insertGenericBuffer (void *data, generic_buffer *buf,
- dataManipFunc func = nullptr) = 0;
+ /** host -> npu-engine */
+ virtual int extractGenericBuffer (const generic_buffer *buf, void *data,
+ dataManipFunc func = nullptr,
+ size_t offset = 0, size_t size = 0) = 0;
+ /** npu-engine -> host */
+ virtual int insertGenericBuffer (void *data, generic_buffer *buf,
+ dataManipFunc func = nullptr) = 0;
- protected:
- CommPlugin () {}
- virtual ~CommPlugin() {};
+ protected:
+ CommPlugin () {}
+ virtual ~CommPlugin (){};
};
#endif /* NE_COMM_H__ */
/** @brief IP communication plugin */
class CommPluginIP : public CommPlugin {
-public:
+ public:
/** @brief get singleton instance */
- static CommPluginIP & getInstance () {
- call_once (once_flag_, []() {
- instance_.reset (new CommPluginIP);
- });
+ static CommPluginIP &getInstance () {
+ call_once (once_flag_, []() { instance_.reset (new CommPluginIP); });
return *(instance_.get ());
};
* @return 0 if no error. otherwise a negative errno
*/
int extractGenericBuffer (const generic_buffer *buf, void *data,
- dataManipFunc func = nullptr, size_t offset = 0, size_t size = 0) {
+ dataManipFunc func = nullptr, size_t offset = 0,
+ size_t size = 0) {
size_t count = 0;
if (buf == nullptr || data == nullptr)
* In this case, ensure that the data format is compatible with the device. */
if (buf->type == BUFFER_FILE) {
std::ifstream ifs (buf->filepath, std::ios::binary);
- if (ifs.good()) {
- ifs.seekg(offset);
+ if (ifs.good ()) {
+ ifs.seekg (offset);
if (func != nullptr) {
- char * tmp = new char [size];
+ char *tmp = new char[size];
- ifs.read(tmp, size);
+ ifs.read (tmp, size);
func (data, tmp, size);
- delete [] tmp;
+ delete[] tmp;
} else {
- ifs.read(static_cast<char *>(data), size);
+ ifs.read (static_cast<char *> (data), size);
}
- count = ifs.gcount();
- ifs.close();
+ count = ifs.gcount ();
+ ifs.close ();
}
} else if (buf->type == BUFFER_MAPPED) {
if (buf->addr == nullptr)
return -EINVAL;
if (func != nullptr) {
- count = func (data, static_cast<char*>(buf->addr) + offset, size);
+ count = func (data, static_cast<char *> (buf->addr) + offset, size);
} else {
- memcpy (data, static_cast<char*>(buf->addr) + offset, size);
+ memcpy (data, static_cast<char *> (buf->addr) + offset, size);
count = size;
}
}
* @return 0 if no error. otherwise a negative errno
*/
int insertGenericBuffer (void *data, generic_buffer *buf,
- dataManipFunc func = nullptr) {
+ dataManipFunc func = nullptr) {
if (buf == nullptr || data == nullptr)
return -EINVAL;
if (buf->size == 0)
return 0;
}
-private:
- CommPluginIP () {};
+ private:
+ CommPluginIP (){};
static std::unique_ptr<CommPluginIP> instance_;
static std::once_flag once_flag_;
* @brief get communication plugin instance
* @note only one plugin can implement CommPlugin::createCommPlugin()
*/
-CommPlugin& CommPlugin::getCommPlugin () {
- return CommPluginIP::getInstance();
+CommPlugin &
+CommPlugin::getCommPlugin () {
+ return CommPluginIP::getInstance ();
}
/** @brief constructor of buffer class */
Buffer::Buffer (const HWmemImpl* impl)
- : HWmem (impl), num_input_ (0), num_output_ (0)
-{
-}
+ : HWmem (impl), num_input_ (0), num_output_ (0) {}
/** @brief destructor of buffer class */
-Buffer::~Buffer ()
-{
- input_.clear();
- output_.clear();
+Buffer::~Buffer () {
+ input_.clear ();
+ output_.clear ();
}
/**
* @note if metadata is not provided, this assumes that
* there is a single pair of input/output tensors.
*/
-int Buffer::createTensors (const Metadata *meta)
-{
+int
+Buffer::createTensors (const Metadata* meta) {
if (meta != nullptr) {
num_input_ = meta->getInputNum ();
num_output_ = meta->getOutputNum ();
/** all tensors have the buffer as the parent hwmem */
for (uint32_t i = 0; i < num_input_; i++)
- input_.push_back (std::unique_ptr<HWmem>(new HWmem (new HWmemChunk)));
+ input_.push_back (std::unique_ptr<HWmem> (new HWmem (new HWmemChunk)));
for (uint32_t i = 0; i < num_output_; i++)
- output_.push_back (std::unique_ptr<HWmem>(new HWmem (new HWmemChunk)));
+ output_.push_back (std::unique_ptr<HWmem> (new HWmem (new HWmemChunk)));
int status = 0;
if (meta != nullptr) {
- for (uint32_t i = 0; i < meta->getInputNum(); i++) {
+ for (uint32_t i = 0; i < meta->getInputNum (); i++) {
status = setInputTensor (i, meta->getInputOffset (i),
- meta->getInputTensorSize (i, DATA_LAYOUT_SRNPU));
+ meta->getInputTensorSize (i, DATA_LAYOUT_SRNPU));
if (status != 0) {
logerr (TAG, "failed to set input tensor: index (%d)\n", i);
return status;
}
}
- for (uint32_t i = 0; i < meta->getOutputNum(); i++) {
- status = setOutputTensor (i, meta->getOutputOffset (i),
- meta->getOutputTensorSize (i, DATA_LAYOUT_SRNPU));
+ for (uint32_t i = 0; i < meta->getOutputNum (); i++) {
+ status =
+ setOutputTensor (i, meta->getOutputOffset (i),
+ meta->getOutputTensorSize (i, DATA_LAYOUT_SRNPU));
if (status != 0) {
logerr (TAG, "failed to set output tensor: index (%d)\n", i);
return status;
}
}
} else {
- status = setInputTensor (0, 0, getSize());
+ status = setInputTensor (0, 0, getSize ());
if (status != 0) {
logerr (TAG, "failed to set input tensor: index (0)\n");
return status;
}
- status = setOutputTensor (0, 0, getSize());
+ status = setOutputTensor (0, 0, getSize ());
if (status != 0) {
logerr (TAG, "failed to set output tensor: index (0)\n");
return status;
* @param[in] idx tensor index
* @return hwmem instance
*/
-HWmem *
+HWmem*
Buffer::getInputTensor (uint32_t idx) {
if (idx < num_input_)
- return input_[idx].get();
+ return input_[idx].get ();
return nullptr;
}
* @param[in] idx tensor index
* @return hwmem instance
*/
-HWmem *
+HWmem*
Buffer::getOutputTensor (uint32_t idx) {
if (idx < num_output_)
- return output_[idx].get();
+ return output_[idx].get ();
return nullptr;
}
/** @brief buffer class derived from hwmem */
class Buffer : public HWmem {
- public:
- Buffer (const HWmemImpl* impl);
- ~Buffer ();
+ public:
+ Buffer (const HWmemImpl *impl);
+ ~Buffer ();
- int createTensors (const Metadata *meta = nullptr);
+ int createTensors (const Metadata *meta = nullptr);
- HWmem *getInputTensor (uint32_t idx);
- HWmem *getOutputTensor (uint32_t idx);
+ HWmem *getInputTensor (uint32_t idx);
+ HWmem *getOutputTensor (uint32_t idx);
- uint32_t getInputNum () { return num_input_; }
- uint32_t getOutputNum () { return num_output_; }
+ uint32_t getInputNum () { return num_input_; }
+ uint32_t getOutputNum () { return num_output_; }
- private:
- int setInputTensor (uint32_t idx, size_t offset, size_t size);
- int setOutputTensor (uint32_t idx, size_t offset, size_t size);
+ private:
+ int setInputTensor (uint32_t idx, size_t offset, size_t size);
+ int setOutputTensor (uint32_t idx, size_t offset, size_t size);
- std::vector <std::unique_ptr<HWmem>> input_; /**< input tensors */
- std::vector <std::unique_ptr<HWmem>> output_; /**< output tensors */
+ std::vector<std::unique_ptr<HWmem>> input_; /**< input tensors */
+ std::vector<std::unique_ptr<HWmem>> output_; /**< output tensors */
- uint32_t num_input_; /**< number of input tensors */
- uint32_t num_output_; /**< number of output tensors */
+ uint32_t num_input_; /**< number of input tensors */
+ uint32_t num_output_; /**< number of output tensors */
};
#endif /* __NPU_ENGINE_BUFFER_H__ */
#include <functional>
/** the binded output callback */
-typedef std::function<void ()> outputCallback;
+typedef std::function<void()> outputCallback;
#endif /* NE_COMMON_H__ */
#define TAG _N2
-#define switch_npu_type(S, n)\
- switch (n) {\
- case DATA_TYPE_SRNPU:\
- /* no-break */\
- case DATA_TYPE_QASYMM8:\
- quantizer = new QuantizerImpl<S, uint8_t>;\
- break;\
- case DATA_TYPE_QSYMM16:\
- quantizer = new QuantizerImpl<S, int16_t>;\
- break;\
- default:\
- logerr (TAG, "Unsupported datatype %d\n", n);\
- return 0;\
+#define switch_npu_type(S, n) \
+ switch (n) { \
+ case DATA_TYPE_SRNPU: \
+ /* no-break */ \
+ case DATA_TYPE_QASYMM8: \
+ quantizer = new QuantizerImpl<S, uint8_t>; \
+ break; \
+ case DATA_TYPE_QSYMM16: \
+ quantizer = new QuantizerImpl<S, int16_t>; \
+ break; \
+ default: \
+ logerr (TAG, "Unsupported datatype %d\n", n); \
+ return 0; \
}
-#define switch_std_npu_type(s, n)\
- switch (s) {\
- case DATA_TYPE_INT8:\
- switch_npu_type (int8_t, n);\
- break;\
- case DATA_TYPE_UINT8:\
- switch_npu_type (uint8_t, n);\
- break;\
- case DATA_TYPE_INT16:\
- switch_npu_type (int16_t, n);\
- break;\
- case DATA_TYPE_UINT16:\
- switch_npu_type (uint16_t, n);\
- break;\
- case DATA_TYPE_INT32:\
- switch_npu_type (int32_t, n);\
- break;\
- case DATA_TYPE_UINT32:\
- switch_npu_type (uint32_t, n);\
- break;\
- case DATA_TYPE_INT64:\
- switch_npu_type (int64_t, n);\
- break;\
- case DATA_TYPE_UINT64:\
- switch_npu_type (uint64_t, n);\
- break;\
- case DATA_TYPE_FLOAT32:\
- switch_npu_type (float, n);\
- break;\
- case DATA_TYPE_FLOAT64:\
- switch_npu_type (double, n);\
- break;\
- default:\
- logerr (TAG, "Unsupported datatype %d\n", s);\
- return 0;\
+#define switch_std_npu_type(s, n) \
+ switch (s) { \
+ case DATA_TYPE_INT8: \
+ switch_npu_type (int8_t, n); \
+ break; \
+ case DATA_TYPE_UINT8: \
+ switch_npu_type (uint8_t, n); \
+ break; \
+ case DATA_TYPE_INT16: \
+ switch_npu_type (int16_t, n); \
+ break; \
+ case DATA_TYPE_UINT16: \
+ switch_npu_type (uint16_t, n); \
+ break; \
+ case DATA_TYPE_INT32: \
+ switch_npu_type (int32_t, n); \
+ break; \
+ case DATA_TYPE_UINT32: \
+ switch_npu_type (uint32_t, n); \
+ break; \
+ case DATA_TYPE_INT64: \
+ switch_npu_type (int64_t, n); \
+ break; \
+ case DATA_TYPE_UINT64: \
+ switch_npu_type (uint64_t, n); \
+ break; \
+ case DATA_TYPE_FLOAT32: \
+ switch_npu_type (float, n); \
+ break; \
+ case DATA_TYPE_FLOAT64: \
+ switch_npu_type (double, n); \
+ break; \
+ default: \
+ logerr (TAG, "Unsupported datatype %d\n", s); \
+ return 0; \
}
-#define DECLARE_QUANTIZER(st, dt)\
- Quantizer *quantizer = nullptr;\
- if (get_data_size (st) != get_data_size (dt)) {\
- if (to_npu_)\
- switch_std_npu_type(st, dt)\
- else\
- switch_std_npu_type(dt, st)\
- quantizer->set_direction(to_npu_);\
- quantizer->set_quant(zero_, scale_);\
+#define DECLARE_QUANTIZER(st, dt) \
+ Quantizer *quantizer = nullptr; \
+ if (get_data_size (st) != get_data_size (dt)) { \
+ if (to_npu_) \
+ switch_std_npu_type (st, dt) else switch_std_npu_type (dt, st) \
+ quantizer->set_direction (to_npu_); \
+ quantizer->set_quant (zero_, scale_); \
}
/** @brief The base class for quantization */
-class Quantizer
-{
- public:
- Quantizer () : to_npu_ (true), zero_ (0), scale_ (0.0) {}
- virtual ~Quantizer () {}
+class Quantizer {
+ public:
+ Quantizer () : to_npu_ (true), zero_ (0), scale_ (0.0) {}
+ virtual ~Quantizer () {}
- void set_direction (bool to_npu) { to_npu_ = to_npu; }
+ void set_direction (bool to_npu) { to_npu_ = to_npu; }
- void set_quant (uint32_t zero, float scale) {
- zero_ = zero;
- scale_ = scale;
- }
+ void set_quant (uint32_t zero, float scale) {
+ zero_ = zero;
+ scale_ = scale;
+ }
- virtual void memcpy (void *dst, void *src, size_t size) {}
+ virtual void memcpy (void *dst, void *src, size_t size) {}
- protected:
- bool to_npu_;
- uint32_t zero_;
- float scale_;
+ protected:
+ bool to_npu_;
+ uint32_t zero_;
+ float scale_;
};
/** @brief The derived class for quantization with various data types */
template <typename S, typename N>
-class QuantizerImpl : public Quantizer
-{
- public:
- QuantizerImpl () {}
- ~QuantizerImpl () {}
+class QuantizerImpl : public Quantizer {
+ public:
+ QuantizerImpl () {}
+ ~QuantizerImpl () {}
- void memcpy (void *dst, void *src, size_t size);
+ void memcpy (void *dst, void *src, size_t size);
- private:
- void quantized_memcpy (void *dst, void *src, uint32_t num);
- void dequantized_memcpy (void *dst, void *src, uint32_t num);
+ private:
+ void quantized_memcpy (void *dst, void *src, uint32_t num);
+ void dequantized_memcpy (void *dst, void *src, uint32_t num);
};
/** @brief memory copy wrapper for quantization */
template <typename S, typename N>
-void QuantizerImpl<S, N>::memcpy (void *dst, void *src, size_t size)
-{
+void
+QuantizerImpl<S, N>::memcpy (void *dst, void *src, size_t size) {
if (to_npu_)
quantized_memcpy (dst, src, size / sizeof (S));
else
/** @brief quantized memory copy */
template <typename S, typename N>
-void QuantizerImpl<S, N>::quantized_memcpy (void *dst, void *src, uint32_t num)
-{
+void
+QuantizerImpl<S, N>::quantized_memcpy (void *dst, void *src, uint32_t num) {
double scale = (double) scale_;
double zero = (double) zero_;
double min, max, val;
/** @brief dequantized memory copy */
template <typename S, typename N>
-void QuantizerImpl<S, N>::dequantized_memcpy (void *dst, void *src, uint32_t num)
-{
+void
+QuantizerImpl<S, N>::dequantized_memcpy (void *dst, void *src, uint32_t num) {
double scale = (double) scale_;
double zero = (double) zero_;
double val;
* @note Support only NHWC as the standard format
*/
bool
-DataConverter::checkCapability ()
-{
+DataConverter::checkCapability () {
/* if not resolved yet */
- if (src_layout_ == DATA_LAYOUT_MODEL ||
- dst_layout_ == DATA_LAYOUT_MODEL)
+ if (src_layout_ == DATA_LAYOUT_MODEL || dst_layout_ == DATA_LAYOUT_MODEL)
return false;
- if (src_type_ == DATA_TYPE_MODEL ||
- dst_type_ == DATA_TYPE_MODEL)
+ if (src_type_ == DATA_TYPE_MODEL || dst_type_ == DATA_TYPE_MODEL)
return false;
/* same layout */
* @return the number of bytes copied
*/
size_t
-DataConverter::perform ()
-{
+DataConverter::perform () {
/* check parameters first */
if (!src_ || !dst_ || size_ == 0 || !dims_) {
logwarn (TAG, "Skip data conversion: Invalid parameter detected");
granularity /= granularity_div;
- bool input_image =
- (to_npu_ && (depth == 1 || depth == 3));
+ bool input_image = (to_npu_ && (depth == 1 || depth == 3));
bool layout_conversion =
- (!input_image && (depth != granularity) && (src_layout_ != dst_layout_));
+ (!input_image && (depth != granularity) && (src_layout_ != dst_layout_));
- DECLARE_QUANTIZER(src_type_, dst_type_);
+ DECLARE_QUANTIZER (src_type_, dst_type_);
if (layout_conversion) {
uint32_t MPA_L = granularity;
slice_size *= src_data_size;
if (quantizer)
- quantizer->memcpy (
- static_cast<char*>(dst_) + dst_offset,
- static_cast<char*>(src_) + src_offset,
- slice_size);
+ quantizer->memcpy (static_cast<char *> (dst_) + dst_offset,
+ static_cast<char *> (src_) + src_offset,
+ slice_size);
else
- memcpy (
- static_cast<char*>(dst_) + dst_offset,
- static_cast<char*>(src_) + src_offset,
- slice_size);
+ memcpy (static_cast<char *> (dst_) + dst_offset,
+ static_cast<char *> (src_) + src_offset, slice_size);
}
}
}
#include <string.h>
#include <stdint.h>
-class DataConverter
-{
- public:
- DataConverter (bool to_npu) :
- dims_ (nullptr), npu_tops_ (8), src_ (nullptr), dst_ (nullptr), size_ (0),
- src_layout_ (DATA_LAYOUT_MODEL), dst_layout_ (DATA_LAYOUT_MODEL),
- src_type_ (DATA_TYPE_MODEL), dst_type_ (DATA_TYPE_MODEL),
- zero_ (0), scale_ (127.0), to_npu_ (to_npu) {}
-
- size_t perform ();
- bool checkCapability ();
-
- void setData (void *src, void *dst, size_t size) {
- src_ = src;
- dst_ = dst;
- size_ = size;
- }
-
- void setDataDims (const uint32_t *dims) {
- dims_ = dims;
- }
-
- void setDataLayout (data_layout src, data_layout dst) {
- src_layout_ = src;
- dst_layout_ = dst;
- }
-
- void setDataType (data_type src, data_type dst) {
- src_type_ = src;
- dst_type_ = dst;
- }
-
- void setQuantZero (uint32_t zero) {
- zero_ = zero;
- }
-
- void setQuantScale (float scale) {
- scale_ = scale;
- }
-
- void setTops (uint32_t tops) {
- npu_tops_ = tops;
- }
-
- private:
- const uint32_t *dims_;
- uint32_t npu_tops_;
-
- void *src_;
- void *dst_;
- size_t size_;
-
- data_layout src_layout_;
- data_layout dst_layout_;
-
- data_type src_type_;
- data_type dst_type_;
-
- uint32_t zero_;
- float scale_;
-
- bool to_npu_;
+class DataConverter {
+ public:
+ DataConverter (bool to_npu)
+ : dims_ (nullptr),
+ npu_tops_ (8),
+ src_ (nullptr),
+ dst_ (nullptr),
+ size_ (0),
+ src_layout_ (DATA_LAYOUT_MODEL),
+ dst_layout_ (DATA_LAYOUT_MODEL),
+ src_type_ (DATA_TYPE_MODEL),
+ dst_type_ (DATA_TYPE_MODEL),
+ zero_ (0),
+ scale_ (127.0),
+ to_npu_ (to_npu) {}
+
+ size_t perform ();
+ bool checkCapability ();
+
+ void setData (void *src, void *dst, size_t size) {
+ src_ = src;
+ dst_ = dst;
+ size_ = size;
+ }
+
+ void setDataDims (const uint32_t *dims) { dims_ = dims; }
+
+ void setDataLayout (data_layout src, data_layout dst) {
+ src_layout_ = src;
+ dst_layout_ = dst;
+ }
+
+ void setDataType (data_type src, data_type dst) {
+ src_type_ = src;
+ dst_type_ = dst;
+ }
+
+ void setQuantZero (uint32_t zero) { zero_ = zero; }
+
+ void setQuantScale (float scale) { scale_ = scale; }
+
+ void setTops (uint32_t tops) { npu_tops_ = tops; }
+
+ private:
+ const uint32_t *dims_;
+ uint32_t npu_tops_;
+
+ void *src_;
+ void *dst_;
+ size_t size_;
+
+ data_layout src_layout_;
+ data_layout dst_layout_;
+
+ data_type src_type_;
+ data_type dst_type_;
+
+ uint32_t zero_;
+ float scale_;
+
+ bool to_npu_;
};
#endif /* __NPU_ENGINE_DATA_H__ */
/** @brief host handler constructor */
HostHandler::HostHandler (Device *device)
- : device_(device),
- /* ignored as we don't use double buffering anymore, but for backward-compatibility */
- async_mode_ (NPUASYNC_WAIT)
-{
+ : device_ (device),
+ /* ignored as we don't use double buffering anymore, but for backward-compatibility */
+ async_mode_ (NPUASYNC_WAIT) {
profiler_ = new ModelProfiler (device_->getDriverAPI ());
}
/** @brief host handler destructor */
-HostHandler::~HostHandler ()
-{
+HostHandler::~HostHandler () {
delete profiler_;
}
* @return 0 if no error. otherwise a negative errno
*/
int
-HostHandler::registerModel (generic_buffer *model_buf, uint32_t *modelid)
-{
+HostHandler::registerModel (generic_buffer *model_buf, uint32_t *modelid) {
if (model_buf == nullptr || modelid == nullptr) {
logerr (TAG, "Invalid arguments given\n");
return -EINVAL;
assert (model != nullptr);
- status = models_.insert (model->getID(), model);
+ status = models_.insert (model->getID (), model);
if (status != 0) {
logerr (TAG, "Failed to insert model id\n");
delete model;
return status;
}
- *modelid = model->getID();
+ *modelid = model->getID ();
return 0;
}
* @return 0 if no error. otherwise a negative errno
*/
int
-HostHandler::unregisterModel (uint32_t modelid)
-{
+HostHandler::unregisterModel (uint32_t modelid) {
Model *model = models_.find (modelid);
if (model == nullptr)
return -ENOENT;
* @return 0
*/
int
-HostHandler::unregisterModels ()
-{
- std::function <bool (Model *)> functor =
- [&] (Model *m) -> bool {
- bool can_remove = true;
- int status = device_->unsetModel (m);
- if (status != 0) {
- logwarn (TAG, "Failed to unset model: %d\n", status);
- can_remove = false;
- }
- return can_remove;
- };
+HostHandler::unregisterModels () {
+ std::function<bool(Model *)> functor = [&](Model *m) -> bool {
+ bool can_remove = true;
+ int status = device_->unsetModel (m);
+ if (status != 0) {
+ logwarn (TAG, "Failed to unset model: %d\n", status);
+ can_remove = false;
+ }
+ return can_remove;
+ };
models_.for_each (functor);
return 0;
* @return 0 if no error, otherwise a negative errno.
*/
int
-HostHandler::getProfile (int req_id, npu_profile *profile)
-{
+HostHandler::getProfile (int req_id, npu_profile *profile) {
if (req_id < 0 || profile == nullptr) {
logerr (TAG, "Invalid parameter provided\n");
return -EINVAL;
* This API is not working on the emulated envionment.
*/
int
-HostHandler::getStatApps (npu_stat_apps *stat)
-{
- const DriverAPI * api = device_->getDriverAPI ();
+HostHandler::getStatApps (npu_stat_apps *stat) {
+ const DriverAPI *api = device_->getDriverAPI ();
assert (api != nullptr);
return api->getStatApps (stat);
* This API is not working on the emulated envionment.
*/
int
-HostHandler::getStatReqs (int appid, npu_stat_reqs *stat)
-{
- const DriverAPI * api = device_->getDriverAPI ();
+HostHandler::getStatReqs (int appid, npu_stat_reqs *stat) {
+ const DriverAPI *api = device_->getDriverAPI ();
assert (api != nullptr);
return api->getStatReqs (appid, stat);
* @return 0 if no error, otherwise a negative errno
*/
int
-HostHandler::getAPILevel (uint32_t *level)
-{
- const DriverAPI * api = device_->getDriverAPI ();
+HostHandler::getAPILevel (uint32_t *level) {
+ const DriverAPI *api = device_->getDriverAPI ();
assert (api != nullptr);
return api->getAPILevel (level);
* @note this does not support for emulated devices
*/
int
-HostHandler::getTops (uint32_t *tops)
-{
- const DriverAPI * api = device_->getDriverAPI ();
+HostHandler::getTops (uint32_t *tops) {
+ const DriverAPI *api = device_->getDriverAPI ();
assert (api != nullptr);
return api->getTops (tops);
* @note this does not support for emulated devices
*/
int
-HostHandler::getDspmSize (uint32_t *dspm)
-{
- const DriverAPI * api = device_->getDriverAPI ();
+HostHandler::getDspmSize (uint32_t *dspm) {
+ const DriverAPI *api = device_->getDriverAPI ();
assert (api != nullptr);
return api->getDspmSize (dspm);
*/
int
HostHandler::setDataInfo (uint32_t modelid, tensors_data_info *in,
- tensors_data_info *out)
-{
+ tensors_data_info *out) {
Model *model = models_.find (modelid);
if (model == nullptr)
return -ENOENT;
* @note If this function is not called, default values are used.
*/
int
-HostHandler::setConstraint (uint32_t modelid, npuConstraint constraint)
-{
+HostHandler::setConstraint (uint32_t modelid, npuConstraint constraint) {
Model *model = models_.find (modelid);
if (model == nullptr)
return -ENOENT;
* @return model instance if found. otherwise nullptr
*/
Model *
-HostHandler::getModel (uint32_t modelid)
-{
+HostHandler::getModel (uint32_t modelid) {
return models_.find (modelid);
}
/** @brief dummay callback for runSync. */
class callbackSync {
- public:
- callbackSync (output_buffers *output) : output_(output), done_(false) {}
+ public:
+ callbackSync (output_buffers *output) : output_ (output), done_ (false) {}
- static void callback (output_buffers *output, uint64_t sequence, void *data) {
- callbackSync *sync = static_cast<callbackSync *>(data);
- sync->callback (output, sequence);
- }
+ static void callback (output_buffers *output, uint64_t sequence, void *data) {
+ callbackSync *sync = static_cast<callbackSync *> (data);
+ sync->callback (output, sequence);
+ }
- void callback (output_buffers *output, uint64_t sequence) {
- if (output_ != nullptr && output != nullptr) {
- /** just copy internal variables of output buffers */
- memcpy (output_, output, sizeof (output_buffers));
- }
- done_ = true;
- cv_.notify_one ();
+ void callback (output_buffers *output, uint64_t sequence) {
+ if (output_ != nullptr && output != nullptr) {
+ /** just copy internal variables of output buffers */
+ memcpy (output_, output, sizeof (output_buffers));
}
+ done_ = true;
+ cv_.notify_one ();
+ }
- void wait () {
- std::unique_lock<std::mutex> lock (m_);
- cv_.wait (lock, [this]() { return done_; });
- }
+ void wait () {
+ std::unique_lock<std::mutex> lock (m_);
+ cv_.wait (lock, [this]() { return done_; });
+ }
- private:
- std::mutex m_;
- std::condition_variable cv_;
- output_buffers *output_;
- bool done_;
+ private:
+ std::mutex m_;
+ std::condition_variable cv_;
+ output_buffers *output_;
+ bool done_;
};
/**
*/
int
HostHandler::runSync (uint32_t modelid, const input_buffers *input,
- output_buffers *output)
-{
+ output_buffers *output) {
callbackSync sync (output);
- int status = runAsync (modelid, input, callbackSync::callback,
- static_cast <void*> (&sync), NPUASYNC_DROP_OLD, nullptr);
+ int status =
+ runAsync (modelid, input, callbackSync::callback,
+ static_cast<void *> (&sync), NPUASYNC_DROP_OLD, nullptr);
if (status > 0) {
/** sync needs to wait callback */
sync.wait ();
*/
int
HostHandler::runAsync (uint32_t modelid, const input_buffers *input,
- npuOutputNotify cb, void *cb_data, npu_async_mode mode, uint64_t *sequence)
-{
+ npuOutputNotify cb, void *cb_data, npu_async_mode mode,
+ uint64_t *sequence) {
Model *model = nullptr;
- if (device_->needModel()) {
+ if (device_->needModel ()) {
model = getModel (modelid);
if (model == nullptr)
return -ENOENT;
device_->setAsyncMode (mode);
- int req_id = device_->run (NPUINPUT_HOST, model, input, cb, cb_data, sequence);
+ int req_id =
+ device_->run (NPUINPUT_HOST, model, input, cb, cb_data, sequence);
if (req_id > 0)
profiler_->appendRequest (req_id, model);
*/
int
HostHandler::runInternal (uint32_t modelid, npu_input_opmode opmode,
- std::string hw_dev)
-{
+ std::string hw_dev) {
Model *model = nullptr;
- if (device_->needModel()) {
+ if (device_->needModel ()) {
model = getModel (modelid);
if (model == nullptr)
return -ENOENT;
* @return @c 0 if no error. otherwise a negative error value
*/
int
-HostHandler::stopInternal (int id)
-{
+HostHandler::stopInternal (int id) {
if (id <= 0) {
logerr (TAG, "Unable to stop this request with id (%d)\n", id);
return -EINVAL;
}
- const DriverAPI * api = device_->getDriverAPI ();
+ const DriverAPI *api = device_->getDriverAPI ();
assert (api != nullptr);
return api->stop_target (id);
* @return number of devices
*/
int
-HostHandler::getNumDevices (dev_type type)
-{
+HostHandler::getNumDevices (dev_type type) {
return DriverAPI::getNumDevices (type);
}
* @return 0 if no error. otherwise a negative errno
*/
int
-HostHandler::getDevice (npudev_h *dev, dev_type type, uint32_t id)
-{
+HostHandler::getDevice (npudev_h *dev, dev_type type, uint32_t id) {
int num_devices = getNumDevices (type);
/** check the validity of device id */
- if (!(num_devices > 0 && id < static_cast<uint32_t>(num_devices))) {
+ if (!(num_devices > 0 && id < static_cast<uint32_t> (num_devices))) {
logerr (TAG, "Invalid arguments provided\n");
return -ENODEV;
}
* @return 0 if no error. otherwise a negative errno
*/
int
-HostHandler::allocGenericBuffer (generic_buffer *buffer)
-{
+HostHandler::allocGenericBuffer (generic_buffer *buffer) {
if (buffer == NULL)
return -EINVAL;
if (buffer->filepath == nullptr)
return -EINVAL;
break;
- case BUFFER_MAPPED:
- {
+ case BUFFER_MAPPED: {
/* now, npu-engine always provides dmabuf-based allocation */
void *addr = nullptr;
int dmabuf = device_->allocMemory (buffer->size, &addr);
* @return 0 if no error. otherwise a negative errno
*/
int
-HostHandler::deallocGenericBuffer (generic_buffer *buffer)
-{
+HostHandler::deallocGenericBuffer (generic_buffer *buffer) {
if (buffer == NULL)
return -EINVAL;
/** always true cuz nothing to do */
break;
case BUFFER_MAPPED:
- return device_->deallocMemory (buffer->dmabuf, buffer->size, buffer->addr);
+ return device_->deallocMemory (buffer->dmabuf, buffer->size,
+ buffer->addr);
default:
return -EINVAL;
}
* @return 0 if no error. otherwise a negative errno
*/
int
-HostHandler::allocGenericBuffer (generic_buffers *buffers)
-{
+HostHandler::allocGenericBuffer (generic_buffers *buffers) {
uint32_t idx;
int status = 0;
* @return 0 if no error. otherwise a negative errno
*/
int
-HostHandler::deallocGenericBuffer (generic_buffers *buffers)
-{
+HostHandler::deallocGenericBuffer (generic_buffers *buffers) {
if (buffers == NULL || buffers->num_buffers < 1)
return -EINVAL;
* @return 0 if no error. otherwise a negatice error value
*/
int
-HostHandler::getMemoryStatus (size_t *alloc_total, size_t *free_total)
-{
+HostHandler::getMemoryStatus (size_t *alloc_total, size_t *free_total) {
/** API is always set in initialize () */
- const DriverAPI * api = device_->getDriverAPI ();
+ const DriverAPI *api = device_->getDriverAPI ();
assert (api != nullptr);
return api->getMemoryStatus (alloc_total, free_total);
* @return 0 if no error, otherwise a negative errno.
*/
int
-HostHandler::getDeviceStatus (npu_status *status, uint32_t *num_requests)
-{
+HostHandler::getDeviceStatus (npu_status *status, uint32_t *num_requests) {
/** API is always set in initialize () */
- const DriverAPI * api = device_->getDriverAPI ();
+ const DriverAPI *api = device_->getDriverAPI ();
if (!api)
return -EINVAL;
/** @brief constructor of device */
Device::Device (dev_type type, int id, bool need_model)
- : comm_ (CommPlugin::getCommPlugin()), type_ (type), id_ (id), need_model_ (true),
- mode_ (NPUASYNC_WAIT), initialized_ (false), atomic_flag_ (ATOMIC_FLAG_INIT)
-{
-}
+ : comm_ (CommPlugin::getCommPlugin ()),
+ type_ (type),
+ id_ (id),
+ need_model_ (true),
+ mode_ (NPUASYNC_WAIT),
+ initialized_ (false),
+ atomic_flag_ (ATOMIC_FLAG_INIT) {}
/**
* @brief create device instance depending on device type and id
* @return device instance
*/
Device *
-Device::createInstance (dev_type type, int id)
-{
+Device::createInstance (dev_type type, int id) {
Device *device = nullptr;
switch (type & DEVICETYPE_MASK) {
* @note Init failures come from createDriverAPI() only.
*/
int
-Device::init ()
-{
+Device::init () {
/** should be initilizaed only once */
- if (!atomic_flag_.test_and_set()) {
+ if (!atomic_flag_.test_and_set ()) {
/** create the corresponding driver API */
api_ = DriverAPI::createDriverAPI (type_, id_);
- if (api_.get() == nullptr) {
- atomic_flag_.clear();
+ if (api_.get () == nullptr) {
+ atomic_flag_.clear ();
logerr (TAG, "Failed to create driver API\n");
return -EINVAL;
}
handler_.reset (new HostHandler (this));
- scheduler_.reset (new Scheduler (api_.get()));
- mem_ = MemAllocator::createInstance (api_.get());
+ scheduler_.reset (new Scheduler (api_.get ()));
+ mem_ = MemAllocator::createInstance (api_.get ());
- initialized_ = true; /** c++11 does not provide test() of atomic flag */
+ initialized_ = true; /** c++11 does not provide test() of atomic flag */
}
return 0;
* @return 0 if no error, otherwise a negative errno
*/
int
-Device::stop (bool force_stop)
-{
+Device::stop (bool force_stop) {
if (!initialized ()) {
logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
return -EPERM;
* @return dmabuf fd if no error, otherwise a negative errno
*/
int
-Device::allocMemory (size_t size, void **addr)
-{
+Device::allocMemory (size_t size, void **addr) {
if (!initialized ()) {
logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
return -EPERM;
* @return 0 if no error, otherwise a negative errno
*/
int
-Device::deallocMemory (int dmabuf_fd, size_t size, void * addr)
-{
+Device::deallocMemory (int dmabuf_fd, size_t size, void *addr) {
if (!initialized ()) {
logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
return -EPERM;
* @return the segment table instance
*/
SegmentTable *
-TrinityVision2::prepareSegmentTable (const Model *model, const input_buffers *input,
- const output_buffers *output)
-{
+TrinityVision2::prepareSegmentTable (const Model *model,
+ const input_buffers *input,
+ const output_buffers *output) {
const Metadata *meta = model->getMetadata ();
- if (meta == nullptr || (input != nullptr &&
- meta->getInputNum() != input->num_buffers)) {
+ if (meta == nullptr ||
+ (input != nullptr && meta->getInputNum () != input->num_buffers)) {
logerr (TAG, "Invalid metadata info provided\n");
return nullptr;
}
- SegmentTable * segt = mem_->allocSegmentTable (new HWmemDevice);
+ SegmentTable *segt = mem_->allocSegmentTable (new HWmemDevice);
int status = segt->alloc ();
if (status != 0) {
logerr (TAG, "Failed to allocate segment table: %d\n", status);
* @return 0 if no error, otherwise a negative errno
*/
int
-TrinityVision2::setModel (const generic_buffer *model_buf, Model ** model_ptr)
-{
+TrinityVision2::setModel (const generic_buffer *model_buf, Model **model_ptr) {
if (!initialized ()) {
logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
return -EPERM;
int status;
switch (model_buf->type) {
- case BUFFER_FILE:
- case BUFFER_MAPPED:
- model = mem_->allocModel (new HWmemDevice);
- if (model == nullptr) {
- logerr (TAG, "Failed to allocate model\n");
- return -ENOMEM;
- }
+ case BUFFER_FILE:
+ case BUFFER_MAPPED:
+ model = mem_->allocModel (new HWmemDevice);
+ if (model == nullptr) {
+ logerr (TAG, "Failed to allocate model\n");
+ return -ENOMEM;
+ }
- status = model->alloc (NPUBIN_META_SIZE);
- if (status != 0) {
- logerr (TAG, "Failed to allocate model: %d\n", status);
- goto delete_exit;
- }
+ status = model->alloc (NPUBIN_META_SIZE);
+ if (status != 0) {
+ logerr (TAG, "Failed to allocate model: %d\n", status);
+ goto delete_exit;
+ }
- status = comm_.extractGenericBuffer (model_buf, model->getData(), nullptr,
- 0, NPUBIN_META_SIZE);
- if (status != 0) {
- logerr (TAG, "Failed to extract generic buffer: %d\n", status);
- goto delete_exit;
- }
- break;
- default:
- return -EINVAL;
+ status = comm_.extractGenericBuffer (model_buf, model->getData (),
+ nullptr, 0, NPUBIN_META_SIZE);
+ if (status != 0) {
+ logerr (TAG, "Failed to extract generic buffer: %d\n", status);
+ goto delete_exit;
+ }
+ break;
+ default:
+ return -EINVAL;
}
- status = model->setMetadata (model->getData());
+ status = model->setMetadata (model->getData ());
if (status != 0)
goto delete_exit;
/** allocate program (optional; NOP) */
- if (model->getMetadata()->getProgramSize() > 0) {
- HWmem * hwmem_prog = new HWmem (new HWmemDevice);
- hwmem_prog->setDriverAPI (api_.get());
+ if (model->getMetadata ()->getProgramSize () > 0) {
+ HWmem *hwmem_prog = new HWmem (new HWmemDevice);
+ hwmem_prog->setDriverAPI (api_.get ());
hwmem_prog->setContiguous (true);
model->setProgramData (hwmem_prog);
- status = hwmem_prog->alloc (model->getMetadata()->getProgramSize());
+ status = hwmem_prog->alloc (model->getMetadata ()->getProgramSize ());
if (status != 0) {
logerr (TAG, "Failed to allocate program\n");
goto delete_exit;
}
- status = comm_.extractGenericBuffer (model_buf, hwmem_prog->getData(), nullptr,
- model->getMetadata()->getMetaSize(),
- model->getMetadata()->getProgramSize());
+ status =
+ comm_.extractGenericBuffer (model_buf, hwmem_prog->getData (), nullptr,
+ model->getMetadata ()->getMetaSize (),
+ model->getMetadata ()->getProgramSize ());
if (status != 0) {
logerr (TAG, "Failed to extract generic buffer: %d\n", status);
goto delete_exit;
/** register this model to the driver */
model_config_t config;
- config.version = model->getMetadata()->getVersion ();
+ config.version = model->getMetadata ()->getVersion ();
config.dbuf_fd = hwmem_prog->getDmabuf ();
config.program_size = hwmem_prog->getSize ();
config.program_offset_addr = 0;
config.metadata_dbuf_fd = model->getDmabuf ();
/** for metadata extended section */
- size_t extended_size = model->getMetadata()->getMetaExtendedSize();
+ size_t extended_size = model->getMetadata ()->getMetaExtendedSize ();
if (extended_size > 0) {
- HWmem * hwmem_extended = new HWmem (new HWmemDevice);
+ HWmem *hwmem_extended = new HWmem (new HWmemDevice);
hwmem_extended->setDriverAPI (api_.get ());
model->setExtendedMetadata (hwmem_extended);
config.metadata_ext_dbuf_fd = hwmem_extended->getDmabuf ();
config.metadata_ext_size = extended_size;
- status = comm_.extractGenericBuffer (model_buf, hwmem_extended->getData (),
- nullptr, NPUBIN_META_SIZE, extended_size);
+ status =
+ comm_.extractGenericBuffer (model_buf, hwmem_extended->getData (),
+ nullptr, NPUBIN_META_SIZE, extended_size);
if (status != 0) {
logerr (TAG, "Failed to extract generic buffer: %d\n", status);
goto delete_exit;
config.metadata_ext_size = 0;
}
- status = api_->registerModel (&config, model->getMetadata()->getNPUVersion());
+ status =
+ api_->registerModel (&config, model->getMetadata ()->getNPUVersion ());
if (status != 0)
goto delete_exit;
- model->setInternalID(config.id);
+ model->setInternalID (config.id);
}
/** allocate weight (optional) */
- if (model->getMetadata()->getWeightSize() > 0) {
- HWmem * hwmem_weight = new HWmem (new HWmemDevice);
- hwmem_weight->setDriverAPI (api_.get());
+ if (model->getMetadata ()->getWeightSize () > 0) {
+ HWmem *hwmem_weight = new HWmem (new HWmemDevice);
+ hwmem_weight->setDriverAPI (api_.get ());
model->setWeightData (hwmem_weight);
- status = hwmem_weight->alloc (model->getMetadata()->getWeightSize());
+ status = hwmem_weight->alloc (model->getMetadata ()->getWeightSize ());
if (status != 0) {
logerr (TAG, "Failed to allocate program\n");
goto delete_exit;
}
- status = comm_.extractGenericBuffer (model_buf, hwmem_weight->getData(), nullptr,
- model->getMetadata()->getMetaSize() + model->getMetadata()->getProgramSize(),
- model->getMetadata()->getWeightSize());
+ status = comm_.extractGenericBuffer (
+ model_buf, hwmem_weight->getData (), nullptr,
+ model->getMetadata ()->getMetaSize () +
+ model->getMetadata ()->getProgramSize (),
+ model->getMetadata ()->getWeightSize ());
if (status != 0) {
logerr (TAG, "Failed to extract generic buffer: %d\n", status);
goto delete_exit;
* @return 0 if no error, otherwise a negative errno
*/
int
-TrinityVision2::unsetModel (Model * model)
-{
+TrinityVision2::unsetModel (Model *model) {
if (!initialized ()) {
logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
return -EPERM;
return -EINVAL;
}
- if (model->getMetadata()->getProgramSize() > 0)
+ if (model->getMetadata ()->getProgramSize () > 0)
return api_->deregisterModel (model->getInternalID ());
return 0;
/** @brief implementation of TRIV2's run() */
int
TrinityVision2::run (npu_input_opmode opmode, const Model *model,
- const input_buffers *input, npuOutputNotify cb, void *cb_data,
- uint64_t *sequence)
-{
+ const input_buffers *input, npuOutputNotify cb,
+ void *cb_data, uint64_t *sequence) {
if (!initialized ()) {
logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
return -EPERM;
if (input == nullptr || input->num_buffers == 0 || model == nullptr)
return -EINVAL;
- const_cast<Model *>(model)->updateDataInfo ();
+ const_cast<Model *> (model)->updateDataInfo ();
/** this device uses segment table */
- SegmentTable * segt = prepareSegmentTable (model, input);
+ SegmentTable *segt = prepareSegmentTable (model, input);
if (segt == nullptr) {
logerr (TAG, "Failed to create segment table instance\n");
return -EINVAL;
/** extract input data */
for (uint32_t idx = 0; idx < input->num_buffers; idx++) {
- if (!segt->getInputSegment(idx)->isExternal ()) {
- uint32_t seg_offset = segt->getInputSegmentOffset(idx);
+ if (!segt->getInputSegment (idx)->isExternal ()) {
+ uint32_t seg_offset = segt->getInputSegmentOffset (idx);
auto func = std::bind (TrinityVision2::manipulateData, model, idx, true,
- std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
+ std::placeholders::_1, std::placeholders::_2,
+ std::placeholders::_3);
int status = comm_.extractGenericBuffer (
&input->bufs[idx],
- segt->getInputSegment(idx)->getData() + seg_offset,
- func);
+ segt->getInputSegment (idx)->getData () + seg_offset, func);
if (status != 0) {
logerr (TAG, "Failed to feed input segment: %d\n", status);
return status;
Request *req = new Request (opmode);
req->setModel (model);
req->setInferData (segt);
- req->setCallback (std::bind (&TrinityVision2::callback, this, req, cb, cb_data));
+ req->setCallback (
+ std::bind (&TrinityVision2::callback, this, req, cb, cb_data));
if (sequence && req->getID () > 0) {
*sequence = (uint32_t) req->getID ();
/** @brief implementation of TRIV2's runInternal() */
int
TrinityVision2::runInternal (npu_input_opmode opmode, const Model *model,
- std::string hw_dev)
-{
+ std::string hw_dev) {
if (!initialized ()) {
logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
return -EPERM;
return -EINVAL;
/** this device uses segment table */
- SegmentTable * segt = prepareSegmentTable (model, nullptr, nullptr);
+ SegmentTable *segt = prepareSegmentTable (model, nullptr, nullptr);
if (segt == nullptr) {
logerr (TAG, "Failed to create segment table instance\n");
return -EINVAL;
/** @brief callback of TRIV2 request */
void
-TrinityVision2::callback (Request *req, npuOutputNotify cb, void *cb_data)
-{
+TrinityVision2::callback (Request *req, npuOutputNotify cb, void *cb_data) {
if (cb == nullptr)
return;
/** internal logic error */
assert (segt != nullptr);
- output_buffers output = {
- .num_buffers = segt->getNumOutputSegments ()
- };
+ output_buffers output = {.num_buffers = segt->getNumOutputSegments ()};
for (uint32_t idx = 0; idx < output.num_buffers; idx++) {
uint32_t output_tensor_size = model->getOutputTensorSize (idx);
output.bufs[idx].addr = calloc (1, output_tensor_size);
#if defined(ENABLE_FPGA_WORKAROUND)
- api_->fpga_memcpy (
- segt->getOutputSegment(idx)->getDmabuf(),
- segt->getOutputSegmentOffset(idx),
- output.bufs[idx].addr,
- output.bufs[idx].size);
+ api_->fpga_memcpy (segt->getOutputSegment (idx)->getDmabuf (),
+ segt->getOutputSegmentOffset (idx),
+ output.bufs[idx].addr, output.bufs[idx].size);
#else
auto func = std::bind (TrinityVision2::manipulateData, model, idx, false,
- std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
- int status = comm_.insertGenericBuffer (
- segt->getOutputSegment(idx)->getData() + segt->getOutputSegmentOffset(idx),
- &output.bufs[idx], func);
+ std::placeholders::_1, std::placeholders::_2,
+ std::placeholders::_3);
+ int status =
+ comm_.insertGenericBuffer (segt->getOutputSegment (idx)->getData () +
+ segt->getOutputSegmentOffset (idx),
+ &output.bufs[idx], func);
if (status != 0) {
logerr (TAG, "Failed to return output buffer: %d\n", status);
#endif
}
- cb (&output, req->getID(), cb_data);
+ cb (&output, req->getID (), cb_data);
delete segt;
}
*/
size_t
TrinityVision2::manipulateData (const Model *model, uint32_t idx, bool is_input,
- void *dst, void *src, size_t size)
-{
+ void *dst, void *src, size_t size) {
const Metadata *meta = model->getMetadata ();
DataConverter converter (is_input);
converter.setData (src, dst, size);
converter.setTops (meta->getTops ());
if (is_input) {
- const tensor_data_info* info = model->getInputDataInfo (idx);
+ const tensor_data_info *info = model->getInputDataInfo (idx);
if (info == nullptr)
return 0;
converter.setQuantZero (meta->getInputQuantZero (idx));
converter.setQuantScale (meta->getInputQuantScale (idx));
} else {
- const tensor_data_info* info = model->getOutputDataInfo (idx);
+ const tensor_data_info *info = model->getOutputDataInfo (idx);
if (info == nullptr)
return 0;
size_t
TrinityVision2::manipulateData (const Model *model, uint32_t idx, bool is_input,
- void *dst, void *src, size_t size)
-{
+ void *dst, void *src, size_t size) {
memcpy (dst, src, size);
return size;
}
/** @brief class def. of host handler */
class HostHandler {
friend class Device;
- public:
- ~HostHandler ();
- int registerModel (generic_buffer *model_buf, uint32_t *modelid);
- int unregisterModel (uint32_t modelid);
- int unregisterModels ();
+ public:
+ ~HostHandler ();
- int getProfile (int req_id, npu_profile *profile);
- int getAPILevel (uint32_t *level);
- int getTops (uint32_t *tops);
- int getDspmSize (uint32_t *dspm);
+ int registerModel (generic_buffer *model_buf, uint32_t *modelid);
+ int unregisterModel (uint32_t modelid);
+ int unregisterModels ();
- int setDataInfo (uint32_t modelid, tensors_data_info *in, tensors_data_info *out);
- int setConstraint (uint32_t modelid, npuConstraint constraint);
+ int getProfile (int req_id, npu_profile *profile);
+ int getAPILevel (uint32_t *level);
+ int getTops (uint32_t *tops);
+ int getDspmSize (uint32_t *dspm);
- Model *getModel (uint32_t modelid);
+ int setDataInfo (uint32_t modelid, tensors_data_info *in,
+ tensors_data_info *out);
+ int setConstraint (uint32_t modelid, npuConstraint constraint);
- int allocGenericBuffer (generic_buffer *buffer);
- int allocGenericBuffer (generic_buffers *buffers);
+ Model *getModel (uint32_t modelid);
- int deallocGenericBuffer (generic_buffer *buffer);
- int deallocGenericBuffer (generic_buffers *buffers);
+ int allocGenericBuffer (generic_buffer *buffer);
+ int allocGenericBuffer (generic_buffers *buffers);
- int runSync (uint32_t modelid, const input_buffers *input,
- output_buffers *output = nullptr);
- int runAsync (uint32_t modelid, const input_buffers *input,
- npuOutputNotify cb = nullptr, void *cb_data = nullptr,
- npu_async_mode mode = NPUASYNC_WAIT, uint64_t *sequence = nullptr);
- int runInternal (uint32_t modelid, npu_input_opmode opmode, std::string hw_dev);
- int stopInternal (int id);
+ int deallocGenericBuffer (generic_buffer *buffer);
+ int deallocGenericBuffer (generic_buffers *buffers);
- /** @brief get statistics */
- int getMemoryStatus (size_t *alloc_total, size_t *free_total);
- int getDeviceStatus (npu_status *status, uint32_t *num_requests);
+ int runSync (uint32_t modelid, const input_buffers *input,
+ output_buffers *output = nullptr);
+ int runAsync (uint32_t modelid, const input_buffers *input,
+ npuOutputNotify cb = nullptr, void *cb_data = nullptr,
+ npu_async_mode mode = NPUASYNC_WAIT,
+ uint64_t *sequence = nullptr);
+ int runInternal (uint32_t modelid, npu_input_opmode opmode,
+ std::string hw_dev);
+ int stopInternal (int id);
- int getStatApps (npu_stat_apps *stat);
- int getStatReqs (int appid, npu_stat_reqs *stat);
+ /** @brief get statistics */
+ int getMemoryStatus (size_t *alloc_total, size_t *free_total);
+ int getDeviceStatus (npu_status *status, uint32_t *num_requests);
- static int getNumDevices (dev_type type);
- static int getDevice (npudev_h *dev, dev_type type, uint32_t id);
+ int getStatApps (npu_stat_apps *stat);
+ int getStatReqs (int appid, npu_stat_reqs *stat);
- private:
- /** only Device can create a HostHandler instance */
- HostHandler (Device *device);
+ static int getNumDevices (dev_type type);
+ static int getDevice (npudev_h *dev, dev_type type, uint32_t id);
- Device *device_; /**< dedicated device instance */
- ModelProfiler *profiler_;
+ private:
+ /** only Device can create a HostHandler instance */
+ HostHandler (Device *device);
- ThreadSafeMap<uint32_t, Model> models_;
- /**< registerd models */
- npu_async_mode async_mode_;
- /**< async mode of runAsync */
- static npudev_h latest_dev_;
- /**< latest device; just for backward-compatability */
+ Device *device_; /**< dedicated device instance */
+ ModelProfiler *profiler_;
+
+ ThreadSafeMap<uint32_t, Model> models_;
+ /**< registerd models */
+ npu_async_mode async_mode_;
+ /**< async mode of runAsync */
+ static npudev_h latest_dev_;
+ /**< latest device; just for backward-compatability */
};
/**
* Also, the caller has a responsibility to release this instance using putNPUdevice()
*/
class Device {
- public:
- /** @brief Factory method to create a trinity device dependong on dev type */
- static Device *createInstance (dev_type device_type, int device_id);
- /** @brief destructor of device */
- virtual ~Device () {}
-
- /** primitives */
- dev_type getType () { return type_; }
- int getID () { return id_; }
- bool needModel () { return need_model_; }
- void setNeedModel (bool need_model) { need_model_ = need_model; }
-
- void setAsyncMode (npu_async_mode mode) { mode_ = mode; }
- HostHandler *getHostHandler () { return handler_.get(); }
- bool initialized () { return initialized_; }
- const DriverAPI *getDriverAPI () { return api_.get(); }
-
- /** the below requires initialized variables */
-
- /** @brief stops all requests from this device (choose wait or force) */
- int stop (bool force_stop);
- int allocMemory (size_t size, void ** addr);
- int deallocMemory (int dmabuf_fd, size_t size, void * addr);
-
- /** virtual methods to implement each device's behaviors */
- virtual int setModel (const generic_buffer *model, Model ** model_ptr) { return -EPERM; }
- virtual int unsetModel (Model * model) { return -EPERM; }
-
- virtual int run (npu_input_opmode opmode, const Model *model,
- const input_buffers *input, npuOutputNotify cb = nullptr,
- void *cb_data = nullptr, uint64_t *sequence = nullptr) = 0;
-
- virtual int runInternal (npu_input_opmode opmode, const Model *model,
- std::string hw_dev) { return -EPERM; }
-
- protected:
- /** the device instance has ownership of all related components */
- std::unique_ptr<DriverAPI> api_; /**< device api */
- std::unique_ptr<MemAllocator> mem_; /**< memory allocator */
- std::unique_ptr<HostHandler> handler_; /**< host handler */
- std::unique_ptr<Scheduler> scheduler_; /**< scheduler */
-
- CommPlugin& comm_; /**< plugin communicator */
-
- dev_type type_; /**< device type */
- int id_; /**< device id */
- bool need_model_; /**< indicates whether the device needs model */
- npu_async_mode mode_; /**< async run mode */
-
- /** @brief constructor of device */
- Device (dev_type type, int id, bool need_model = true);
-
- private:
- /** @brief initialization */
- int init ();
-
- bool initialized_;
- std::atomic_flag atomic_flag_; /**< atomic flag to check initilization */
+ public:
+ /** @brief Factory method to create a trinity device dependong on dev type */
+ static Device *createInstance (dev_type device_type, int device_id);
+ /** @brief destructor of device */
+ virtual ~Device () {}
+
+ /** primitives */
+ dev_type getType () { return type_; }
+ int getID () { return id_; }
+ bool needModel () { return need_model_; }
+ void setNeedModel (bool need_model) { need_model_ = need_model; }
+
+ void setAsyncMode (npu_async_mode mode) { mode_ = mode; }
+ HostHandler *getHostHandler () { return handler_.get (); }
+ bool initialized () { return initialized_; }
+ const DriverAPI *getDriverAPI () { return api_.get (); }
+
+ /** the below requires initialized variables */
+
+ /** @brief stops all requests from this device (choose wait or force) */
+ int stop (bool force_stop);
+ int allocMemory (size_t size, void **addr);
+ int deallocMemory (int dmabuf_fd, size_t size, void *addr);
+
+ /** virtual methods to implement each device's behaviors */
+ virtual int setModel (const generic_buffer *model, Model **model_ptr) {
+ return -EPERM;
+ }
+ virtual int unsetModel (Model *model) { return -EPERM; }
+
+ virtual int run (npu_input_opmode opmode, const Model *model,
+ const input_buffers *input, npuOutputNotify cb = nullptr,
+ void *cb_data = nullptr, uint64_t *sequence = nullptr) = 0;
+
+ virtual int runInternal (npu_input_opmode opmode, const Model *model,
+ std::string hw_dev) {
+ return -EPERM;
+ }
+
+ protected:
+ /** the device instance has ownership of all related components */
+ std::unique_ptr<DriverAPI> api_; /**< device api */
+ std::unique_ptr<MemAllocator> mem_; /**< memory allocator */
+ std::unique_ptr<HostHandler> handler_; /**< host handler */
+ std::unique_ptr<Scheduler> scheduler_; /**< scheduler */
+
+ CommPlugin &comm_; /**< plugin communicator */
+
+ dev_type type_; /**< device type */
+ int id_; /**< device id */
+ bool need_model_; /**< indicates whether the device needs model */
+ npu_async_mode mode_; /**< async run mode */
+
+ /** @brief constructor of device */
+ Device (dev_type type, int id, bool need_model = true);
+
+ private:
+ /** @brief initialization */
+ int init ();
+
+ bool initialized_;
+ std::atomic_flag atomic_flag_; /**< atomic flag to check initilization */
};
/** @brief Trinity Vision2 (TRIV2) class */
class TrinityVision2 : public Device {
- public:
- TrinityVision2 (int id) : Device (NPUCOND_TRIV2_CONN_SOCIP, id) {}
- ~TrinityVision2 () {}
-
- static size_t manipulateData (const Model *model, uint32_t idx, bool is_input,
- void *dst, void *src, size_t size);
-
- SegmentTable * prepareSegmentTable (const Model *model, const input_buffers *input,
- const output_buffers *output = nullptr);
-
- int setModel (const generic_buffer *model, Model ** model_ptr);
- int unsetModel (Model * model);
- int run (npu_input_opmode opmode, const Model *model,
- const input_buffers *input, npuOutputNotify cb = nullptr,
- void *cb_data = nullptr, uint64_t *sequence = nullptr);
- int runInternal (npu_input_opmode opmode, const Model *model, std::string hw_dev);
-
- private:
- void callback (Request *req, npuOutputNotify cb, void *cb_data);
+ public:
+ TrinityVision2 (int id) : Device (NPUCOND_TRIV2_CONN_SOCIP, id) {}
+ ~TrinityVision2 () {}
+
+ static size_t manipulateData (const Model *model, uint32_t idx, bool is_input,
+ void *dst, void *src, size_t size);
+
+ SegmentTable *prepareSegmentTable (const Model *model,
+ const input_buffers *input,
+ const output_buffers *output = nullptr);
+
+ int setModel (const generic_buffer *model, Model **model_ptr);
+ int unsetModel (Model *model);
+ int run (npu_input_opmode opmode, const Model *model,
+ const input_buffers *input, npuOutputNotify cb = nullptr,
+ void *cb_data = nullptr, uint64_t *sequence = nullptr);
+ int runInternal (npu_input_opmode opmode, const Model *model,
+ std::string hw_dev);
+
+ private:
+ void callback (Request *req, npuOutputNotify cb, void *cb_data);
};
#endif /* __NPU_ENGINE_HANDLER_H__ */
std::once_flag HostInputService::once_flag_;
HostInputService &
-HostInputService::getInstance ()
-{
- call_once (once_flag_, []() {
- instance_.reset (new HostInputService);
- });
+HostInputService::getInstance () {
+ call_once (once_flag_, []() { instance_.reset (new HostInputService); });
return *(instance_.get ());
}
int
-HostInputService::submit (const DriverAPI *api, int id,
- const Model *model, HWmem *data, outputCallback callback)
-{
+HostInputService::submit (const DriverAPI *api, int id, const Model *model,
+ HWmem *data, outputCallback callback) {
if (api == nullptr)
return -EINVAL;
if (dynamic_cast<Buffer *> (data)) {
/* empty model is possible */
- return submit_buffer (api, id, model, dynamic_cast<Buffer *> (data), callback);
+ return submit_buffer (api, id, model, dynamic_cast<Buffer *> (data),
+ callback);
} else if (dynamic_cast<SegmentTable *> (data)) {
if (model == nullptr)
return -EINVAL;
- return submit_segt (api, id, model, dynamic_cast<SegmentTable *> (data), callback);
+ return submit_segt (api, id, model, dynamic_cast<SegmentTable *> (data),
+ callback);
} else {
return -EINVAL;
}
*/
int
HostInputService::submit_buffer (const DriverAPI *api, int id,
- const Model *model, Buffer *buffer, outputCallback callback)
-{
- taskFunc func = std::bind (&HostInputService::invoke_buffer, this,
- api, model, buffer, callback, id);
+ const Model *model, Buffer *buffer,
+ outputCallback callback) {
+ taskFunc func = std::bind (&HostInputService::invoke_buffer, this, api, model,
+ buffer, callback, id);
ThreadTask *task = new ThreadTask (id, func);
- return ThreadPool::getInstance().enqueueTask (task);
+ return ThreadPool::getInstance ().enqueueTask (task);
}
/**
* @return task id if no error, otherwise a negative errno.
*/
int
-HostInputService::submit_segt (const DriverAPI *api, int id,
- const Model *model, SegmentTable *segt, outputCallback callback)
-{
- taskFunc func = std::bind (&HostInputService::invoke_segt, this,
- api, model, segt, callback, id);
+HostInputService::submit_segt (const DriverAPI *api, int id, const Model *model,
+ SegmentTable *segt, outputCallback callback) {
+ taskFunc func = std::bind (&HostInputService::invoke_segt, this, api, model,
+ segt, callback, id);
ThreadTask *task = new ThreadTask (id, func);
- return ThreadPool::getInstance().enqueueTask (task);
+ return ThreadPool::getInstance ().enqueueTask (task);
}
/**
* @return 0 if no erorr. otherwise a negative errno
*/
int
-HostInputService::remove (int id)
-{
- return ThreadPool::getInstance().removeTask (id);
+HostInputService::remove (int id) {
+ return ThreadPool::getInstance ().removeTask (id);
}
/**
*/
int
HostInputService::invoke_buffer (const DriverAPI *api, const Model *model,
- Buffer *buffer, outputCallback callback, int req_id)
-{
+ Buffer *buffer, outputCallback callback,
+ int req_id) {
input_config_t input_config;
device_state_t state;
int ret = -EINVAL;
- state = api->isReady();
+ state = api->isReady ();
if (state != device_state_t::STATE_READY) {
logerr (TAG, "device is not available to run inference %d\n", state);
goto handle_callback;
if (model != nullptr) {
/** consider NOP cases */
- if (model->getProgramData() == nullptr) {
+ if (model->getProgramData () == nullptr) {
ret = 0;
goto handle_callback;
}
- input_config.model_id = model->getInternalID();
+ input_config.model_id = model->getInternalID ();
} else {
input_config.model_id = 0;
}
*/
int
HostInputService::invoke_segt (const DriverAPI *api, const Model *model,
- SegmentTable *segt, outputCallback callback, int req_id)
-{
+ SegmentTable *segt, outputCallback callback,
+ int req_id) {
input_config_t input_config;
device_state_t state;
npuConstraint constraint;
int ret = -EINVAL;
- state = api->isReady();
+ state = api->isReady ();
if (state != device_state_t::STATE_READY) {
logerr (TAG, "device is not available to run inference %d\n", state);
goto handle_callback;
assert (segt != nullptr);
/** consider NOP cases */
- if (model->getProgramData() == nullptr) {
+ if (model->getProgramData () == nullptr) {
ret = 0;
goto handle_callback;
}
- input_config.model_id = model->getInternalID();
+ input_config.model_id = model->getInternalID ();
input_config.dbuf_fd = segt->getDmabuf ();
input_config.num_segments = segt->getNumTotalSegments ();
std::once_flag HwInputService::once_flag_;
HwInputService &
-HwInputService::getInstance ()
-{
- call_once (once_flag_, []() {
- instance_.reset (new HwInputService);
- });
+HwInputService::getInstance () {
+ call_once (once_flag_, []() { instance_.reset (new HwInputService); });
return *(instance_.get ());
}
* @return req_id if no error, otherwise a negative errno.
*/
int
-HwInputService::submit (const DriverAPI *api, int id,
- const Model *model, HWmem *data, outputCallback callback)
-{
+HwInputService::submit (const DriverAPI *api, int id, const Model *model,
+ HWmem *data, outputCallback callback) {
if (api == nullptr || model == nullptr)
return -EINVAL;
*/
int
HwInputService::invoke (const DriverAPI *api, const Model *model,
- SegmentTable *segt, outputCallback callback, int req_id)
-{
+ SegmentTable *segt, outputCallback callback,
+ int req_id) {
input_config_t input_config;
device_state_t state;
npuConstraint constraint;
int ret = -EINVAL;
- state = api->isReady();
+ state = api->isReady ();
if (state != device_state_t::STATE_READY) {
logerr (TAG, "device is not available to run inference %d\n", state);
goto handle_callback;
}
/** consider NOP cases */
- if (model->getProgramData() == nullptr) {
+ if (model->getProgramData () == nullptr) {
ret = 0;
goto handle_callback;
}
- input_config.model_id = model->getInternalID();
+ input_config.model_id = model->getInternalID ();
if (segt != nullptr) {
input_config.dbuf_fd = segt->getDmabuf ();
input_config.num_segments = segt->getNumTotalSegments ();
/** set constraints */
constraint = model->getConstraint ();
- input_config.timeout_ms = 0; /* immediatedly handled */
+ input_config.timeout_ms = 0; /* immediatedly handled */
input_config.priority = NPU_PRIORITY_HIGH;
input_config.input_mode = INPUT_HW;
input_config.output_mode = OUTPUT_HW;
goto out_close_fd;
}
- if (!S_ISBLK(devnode_stat.st_mode) && !S_ISCHR(devnode_stat.st_mode)) {
+ if (!S_ISBLK (devnode_stat.st_mode) && !S_ISCHR (devnode_stat.st_mode)) {
ret = -EINVAL;
goto out_close_fd;
}
#include "ne-hwmem.h"
/** @brief HWmem constructor */
-HWmem::HWmem (const HWmemImpl* impl)
- : impl_ (impl), api_ (nullptr), dmabuf_ (-1), base_addr_ (nullptr),
- offset_ (0), size_ (0), contiguous_(false), parent_ (nullptr)
-{
-}
+HWmem::HWmem (const HWmemImpl *impl)
+ : impl_ (impl),
+ api_ (nullptr),
+ dmabuf_ (-1),
+ base_addr_ (nullptr),
+ offset_ (0),
+ size_ (0),
+ contiguous_ (false),
+ parent_ (nullptr) {}
/** @brief HWmem destructor */
-HWmem::~HWmem ()
-{
+HWmem::~HWmem () {
if (impl_) {
impl_->cleanup (this);
delete impl_;
* @return data address if no error. otherwise nullptr
*/
char *
-HWmem::getData ()
-{
+HWmem::getData () {
if (impl_ == nullptr)
return nullptr;
* @return 0 if no error. otherwise a negative errno
*/
int
-HWmem::alloc (size_t size)
-{
+HWmem::alloc (size_t size) {
if (size == 0 || impl_ == nullptr)
return -EINVAL;
* @return 0 if no error. otherwise a negative errno
*/
int
-HWmem::dealloc ()
-{
+HWmem::dealloc () {
if (size_ == 0 || impl_ == nullptr)
return -EINVAL;
* @param[in] hwmem hwmem instance
*/
void
-HWmemDevice::cleanup (HWmem *hwmem) const
-{
- if (hwmem->getDriverAPI() == nullptr)
+HWmemDevice::cleanup (HWmem *hwmem) const {
+ if (hwmem->getDriverAPI () == nullptr)
return;
if (hwmem->getBaseAddr () != nullptr) {
- hwmem->getDriverAPI()->munmap (hwmem->getBaseAddr (), ALIGNED_SIZE (hwmem->getSize()));
+ hwmem->getDriverAPI ()->munmap (hwmem->getBaseAddr (),
+ ALIGNED_SIZE (hwmem->getSize ()));
hwmem->setBaseAddr (nullptr);
}
if (hwmem->getDmabuf () >= 0) {
- hwmem->getDriverAPI()->dealloc (hwmem->getDmabuf (), hwmem->isContiguous());
+ hwmem->getDriverAPI ()->dealloc (hwmem->getDmabuf (),
+ hwmem->isContiguous ());
hwmem->setDmabuf (-1);
hwmem->setSize (0);
* @return 0 if no erorr. otherwise a negative errno
*/
int
-HWmemDevice::alloc (HWmem *hwmem, size_t size) const
-{
- if (hwmem->getDriverAPI() == nullptr)
+HWmemDevice::alloc (HWmem *hwmem, size_t size) const {
+ if (hwmem->getDriverAPI () == nullptr)
return -EINVAL;
/** already allocated */
- if (hwmem->getDmabuf() >= 0) {
+ if (hwmem->getDmabuf () >= 0) {
return -EBUSY;
}
- int dmabuf = hwmem->getDriverAPI()->alloc (ALIGNED_SIZE (size),
- hwmem->isContiguous());
+ int dmabuf = hwmem->getDriverAPI ()->alloc (ALIGNED_SIZE (size),
+ hwmem->isContiguous ());
if (dmabuf < 0)
return dmabuf;
* @return 0 if no errror. otherwise a negative errno
*/
int
-HWmemDevice::dealloc (HWmem *hwmem) const
-{
- if (hwmem->getDriverAPI() == nullptr)
+HWmemDevice::dealloc (HWmem *hwmem) const {
+ if (hwmem->getDriverAPI () == nullptr)
return -EINVAL;
/** not allocated */
- if (hwmem->getDmabuf() < 0)
+ if (hwmem->getDmabuf () < 0)
return -EINVAL;
- hwmem->getDriverAPI()->dealloc (hwmem->getDmabuf(), hwmem->isContiguous());
+ hwmem->getDriverAPI ()->dealloc (hwmem->getDmabuf (), hwmem->isContiguous ());
hwmem->setDmabuf (-1);
hwmem->setSize (0);
return 0;
* @return mapped address
*/
char *
-HWmemDevice::getData (HWmem *hwmem) const
-{
- const DriverAPI * api = hwmem->getDriverAPI();
+HWmemDevice::getData (HWmem *hwmem) const {
+ const DriverAPI *api = hwmem->getDriverAPI ();
if (api == nullptr)
return nullptr;
- int dmabuf = hwmem->getDmabuf();
- size_t size = hwmem->getSize();
- size_t offset = hwmem->getOffset();
+ int dmabuf = hwmem->getDmabuf ();
+ size_t size = hwmem->getSize ();
+ size_t offset = hwmem->getOffset ();
if (!(dmabuf >= 0 && size > 0))
return nullptr;
return nullptr;
}
- return static_cast <char*> (hwmem->getBaseAddr ()) + offset;
+ return static_cast<char *> (hwmem->getBaseAddr ()) + offset;
}
/** Impl. of HWmemChunk */
* @param[in] hwmem hwmem instance
*/
void
-HWmemChunk::cleanup (HWmem *hwmem) const
-{
+HWmemChunk::cleanup (HWmem *hwmem) const {
hwmem->setParent (nullptr);
hwmem->setOffset (0);
hwmem->setSize (0);
* @return mapped address
*/
char *
-HWmemChunk::getData (HWmem *hwmem) const
-{
+HWmemChunk::getData (HWmem *hwmem) const {
HWmem *parent = hwmem->getParent ();
if (parent == nullptr)
return nullptr;
- if (parent->getDriverAPI() == nullptr)
+ if (parent->getDriverAPI () == nullptr)
return nullptr;
- char * data = parent->getData();
+ char *data = parent->getData ();
if (data == nullptr)
return nullptr;
* @return 0 if no erorr. otherwise a negative errno
*/
int
-HWmemChunk::alloc (HWmem *hwmem, size_t size) const
-{
+HWmemChunk::alloc (HWmem *hwmem, size_t size) const {
HWmem *parent = hwmem->getParent ();
if (parent == nullptr)
return -EINVAL;
- int dmabuf = parent->getDmabuf();
+ int dmabuf = parent->getDmabuf ();
/** parent was not allocated */
if (dmabuf < 0)
return -EPERM;
/** check offset */
- if (parent->getSize() <= hwmem->getOffset ())
+ if (parent->getSize () <= hwmem->getOffset ())
return -EINVAL;
/** check size */
- if (parent->getSize() < hwmem->getOffset () + size)
+ if (parent->getSize () < hwmem->getOffset () + size)
return -EINVAL;
/** just set the size */
* @return 0 if no errror. otherwise a negative errno
*/
int
-HWmemChunk::dealloc (HWmem *hwmem) const
-{
+HWmemChunk::dealloc (HWmem *hwmem) const {
HWmem *parent = hwmem->getParent ();
if (parent == nullptr)
return -EINVAL;
/** parent was not allocated */
- if (parent->getDmabuf() < 0)
+ if (parent->getDmabuf () < 0)
return -EPERM;
/** just reset the size */
* @return mapped address
*/
char *
-HWmemExternal::getData (HWmem *hwmem) const
-{
+HWmemExternal::getData (HWmem *hwmem) const {
/**
* In fact, a driver API does not have a responsibility to support mmap()
* for an external dmabuf FD. But, let's just rely on the API for uniformity.
*/
- const DriverAPI * api = hwmem->getDriverAPI();
+ const DriverAPI *api = hwmem->getDriverAPI ();
if (api == nullptr)
return nullptr;
- int dmabuf = hwmem->getDmabuf();
- size_t size = hwmem->getSize();
- size_t offset = hwmem->getOffset();
+ int dmabuf = hwmem->getDmabuf ();
+ size_t size = hwmem->getSize ();
+ size_t offset = hwmem->getOffset ();
if (!(dmabuf >= 0 && size > 0))
return nullptr;
return nullptr;
}
- return static_cast <char*> (hwmem->getBaseAddr ()) + offset;
+ return static_cast<char *> (hwmem->getBaseAddr ()) + offset;
}
/** HWmem implementation */
class HWmemImpl {
- public:
- virtual ~HWmemImpl() {};
+ public:
+ virtual ~HWmemImpl (){};
- virtual void cleanup (HWmem *hwmem) const {}
- virtual int alloc (HWmem *hwmem, size_t size) const { return 0; }
- virtual int dealloc (HWmem *hwmem) const { return 0; }
- virtual char *getData (HWmem *hwmem) const { return nullptr; }
+ virtual void cleanup (HWmem *hwmem) const {}
+ virtual int alloc (HWmem *hwmem, size_t size) const { return 0; }
+ virtual int dealloc (HWmem *hwmem) const { return 0; }
+ virtual char *getData (HWmem *hwmem) const { return nullptr; }
};
/** HWmem allocated by device */
class HWmemDevice : public HWmemImpl {
- public:
- void cleanup (HWmem *hwmem) const;
- int alloc (HWmem *hwmem, size_t size) const;
- int dealloc (HWmem *hwmem) const;
- char *getData (HWmem *hwmem) const;
+ public:
+ void cleanup (HWmem *hwmem) const;
+ int alloc (HWmem *hwmem, size_t size) const;
+ int dealloc (HWmem *hwmem) const;
+ char *getData (HWmem *hwmem) const;
};
/** HWmem served by another hwmem */
class HWmemChunk : public HWmemImpl {
- public:
- void cleanup (HWmem *hwmem) const;
- char *getData (HWmem *hwmem) const;
- int alloc (HWmem *hwmem, size_t size) const;
- int dealloc (HWmem *hwmem) const;
+ public:
+ void cleanup (HWmem *hwmem) const;
+ char *getData (HWmem *hwmem) const;
+ int alloc (HWmem *hwmem, size_t size) const;
+ int dealloc (HWmem *hwmem) const;
};
/** HWmem shared by external dmabuf */
class HWmemExternal : public HWmemImpl {
- public:
- /** only support getData() */
- char *getData (HWmem *hwmem) const;
+ public:
+ /** only support getData() */
+ char *getData (HWmem *hwmem) const;
};
/** HWmem base class */
class HWmem {
- public:
- HWmem (const HWmemImpl* impl = nullptr);
- virtual ~HWmem ();
-
- /** get private member variables */
- const DriverAPI * getDriverAPI () const { return api_; }
- int getDmabuf () const { return dmabuf_; }
- size_t getSize () const { return size_; }
- size_t getOffset () const { return offset_; }
- void *getBaseAddr () const { return base_addr_; }
- HWmem *getParent () const { return parent_; }
- bool isExternal () const { return nullptr != dynamic_cast<const HWmemExternal *>(impl_); }
- bool isContiguous () const { return contiguous_; }
-
- /** set private member variables */
- void setDriverAPI (const DriverAPI* api) { api_ = api; }
- void setDmabuf (int dmabuf) { dmabuf_ = dmabuf; }
- void setSize (size_t size) { size_ = size; }
- void setOffset (size_t offset) { offset_ = offset; }
- void setBaseAddr (void *base_addr) { base_addr_ = base_addr; }
- void setParent (HWmem *hwmem) { parent_ = hwmem; }
- void setContiguous (bool contiguous) { contiguous_ = contiguous; }
-
- /** the below APIs require its HWmem impl. */
- char *getData ();
- int alloc (size_t size);
- int dealloc ();
-
- private:
- const HWmemImpl* impl_; /**< HWmem impl. */
- const DriverAPI* api_; /**< driver API */
-
- int dmabuf_; /**< dmabuf fd of hwmem (equal to base address when mapped) */
- void *base_addr_; /**< base address of hwmem */
- size_t offset_; /**< offset of hwmem (starting from base address) */
- size_t size_; /**< size of hwmem */
-
- bool contiguous_; /**< Force physically-contiguous mapping (no iommu) */
-
- HWmem *parent_; /**< optional. used only for HWmemChunk */
+ public:
+ HWmem (const HWmemImpl *impl = nullptr);
+ virtual ~HWmem ();
+
+ /** get private member variables */
+ const DriverAPI *getDriverAPI () const { return api_; }
+ int getDmabuf () const { return dmabuf_; }
+ size_t getSize () const { return size_; }
+ size_t getOffset () const { return offset_; }
+ void *getBaseAddr () const { return base_addr_; }
+ HWmem *getParent () const { return parent_; }
+ bool isExternal () const {
+ return nullptr != dynamic_cast<const HWmemExternal *> (impl_);
+ }
+ bool isContiguous () const { return contiguous_; }
+
+ /** set private member variables */
+ void setDriverAPI (const DriverAPI *api) { api_ = api; }
+ void setDmabuf (int dmabuf) { dmabuf_ = dmabuf; }
+ void setSize (size_t size) { size_ = size; }
+ void setOffset (size_t offset) { offset_ = offset; }
+ void setBaseAddr (void *base_addr) { base_addr_ = base_addr; }
+ void setParent (HWmem *hwmem) { parent_ = hwmem; }
+ void setContiguous (bool contiguous) { contiguous_ = contiguous; }
+
+ /** the below APIs require its HWmem impl. */
+ char *getData ();
+ int alloc (size_t size);
+ int dealloc ();
+
+ private:
+ const HWmemImpl *impl_; /**< HWmem impl. */
+ const DriverAPI *api_; /**< driver API */
+
+ int dmabuf_; /**< dmabuf fd of hwmem (equal to base address when mapped) */
+ void *base_addr_; /**< base address of hwmem */
+ size_t offset_; /**< offset of hwmem (starting from base address) */
+ size_t size_; /**< size of hwmem */
+
+ bool contiguous_; /**< Force physically-contiguous mapping (no iommu) */
+
+ HWmem *parent_; /**< optional. used only for HWmemChunk */
};
#endif /* __NPU_ENGINE_HWMEM_H__ */
/** @brief input service class */
class InputService {
- public:
- /** @brief submit request to somewhere (depends on each impl.) */
- virtual int submit (const DriverAPI *api, int request_id, const Model *model,
- HWmem *data, outputCallback callback = nullptr) { return -EPERM; }
- /** @brief [OPTIONAL] remove the submitted request (if possible) */
- virtual int remove (int request_id) { return -EINVAL; }
-
- virtual ~InputService () {}
-
- protected:
- InputService () {}
+ public:
+ /** @brief submit request to somewhere (depends on each impl.) */
+ virtual int submit (const DriverAPI *api, int request_id, const Model *model,
+ HWmem *data, outputCallback callback = nullptr) {
+ return -EPERM;
+ }
+ /** @brief [OPTIONAL] remove the submitted request (if possible) */
+ virtual int remove (int request_id) { return -EINVAL; }
+
+ virtual ~InputService () {}
+
+ protected:
+ InputService () {}
};
/** @brief host input service */
class HostInputService : public InputService {
- public:
- static HostInputService & getInstance ();
-
- int submit (const DriverAPI *api, int request_id, const Model *model,
- HWmem *data, outputCallback callback = nullptr);
- int remove (int request_id);
-
- private:
- int submit_buffer (const DriverAPI *api, int request_id, const Model *model,
- Buffer *buffer, outputCallback callback = nullptr);
- int submit_segt (const DriverAPI *api, int request_id, const Model *model,
- SegmentTable *segt, outputCallback callback = nullptr);
-
- /** do not allow to directly call invoke () */
- int invoke_buffer (const DriverAPI *api, const Model *model, Buffer *buffer,
- outputCallback callback, int task_id);
- int invoke_segt (const DriverAPI *api, const Model *model, SegmentTable *segt,
- outputCallback callback, int task_id);
-
- static std::unique_ptr<HostInputService> instance_;
- static std::once_flag once_flag_;
+ public:
+ static HostInputService &getInstance ();
+
+ int submit (const DriverAPI *api, int request_id, const Model *model,
+ HWmem *data, outputCallback callback = nullptr);
+ int remove (int request_id);
+
+ private:
+ int submit_buffer (const DriverAPI *api, int request_id, const Model *model,
+ Buffer *buffer, outputCallback callback = nullptr);
+ int submit_segt (const DriverAPI *api, int request_id, const Model *model,
+ SegmentTable *segt, outputCallback callback = nullptr);
+
+ /** do not allow to directly call invoke () */
+ int invoke_buffer (const DriverAPI *api, const Model *model, Buffer *buffer,
+ outputCallback callback, int task_id);
+ int invoke_segt (const DriverAPI *api, const Model *model, SegmentTable *segt,
+ outputCallback callback, int task_id);
+
+ static std::unique_ptr<HostInputService> instance_;
+ static std::once_flag once_flag_;
};
/** @brief HW recurring input service (works only with segment table) */
class HwInputService : public InputService {
- public:
- static HwInputService & getInstance ();
+ public:
+ static HwInputService &getInstance ();
- int submit (const DriverAPI *api, int request_id, const Model *model,
- HWmem *data, outputCallback callback = nullptr);
+ int submit (const DriverAPI *api, int request_id, const Model *model,
+ HWmem *data, outputCallback callback = nullptr);
- private:
- /** do not allow to directly call invoke () */
- int invoke (const DriverAPI *api, const Model *model, SegmentTable *segt,
- outputCallback callback, int task_id);
+ private:
+ /** do not allow to directly call invoke () */
+ int invoke (const DriverAPI *api, const Model *model, SegmentTable *segt,
+ outputCallback callback, int task_id);
- static std::unique_ptr<HwInputService> instance_;
- static std::once_flag once_flag_;
+ static std::unique_ptr<HwInputService> instance_;
+ static std::once_flag once_flag_;
};
#endif /* __NPU_ENGINE_INPUTSERVICE_H__ */
/** @brief default memory allocator */
class MemDefault : public MemAllocator {
- public:
- MemDefault (const DriverAPI *api) : MemAllocator (api) {}
-
- /** For library users. its backing storage is always a device driver */
- int allocMemory (size_t size, void ** addr) {
- int dmabuf = api_->alloc (size);
- if (dmabuf < 0)
- return dmabuf;
-
- *addr = api_->mmap (dmabuf, size);
- if (*addr == nullptr) {
- api_->dealloc (dmabuf);
- return -EINVAL;
- }
+ public:
+ MemDefault (const DriverAPI *api) : MemAllocator (api) {}
+ /** For library users. its backing storage is always a device driver */
+ int allocMemory (size_t size, void **addr) {
+ int dmabuf = api_->alloc (size);
+ if (dmabuf < 0)
return dmabuf;
+
+ *addr = api_->mmap (dmabuf, size);
+ if (*addr == nullptr) {
+ api_->dealloc (dmabuf);
+ return -EINVAL;
}
- int deallocMemory (int dmabuf_fd, size_t size, void * addr) {
- int status = api_->munmap (addr, size);
- if (status != 0)
- return status;
+ return dmabuf;
+ }
- return api_->dealloc (dmabuf_fd);
- }
+ int deallocMemory (int dmabuf_fd, size_t size, void *addr) {
+ int status = api_->munmap (addr, size);
+ if (status != 0)
+ return status;
- /** For internal npu-engine components */
- Model* allocModel (HWmemImpl *impl) {
- if (impl == nullptr)
- return nullptr;
+ return api_->dealloc (dmabuf_fd);
+ }
- Model * model = new Model (impl);
- model->setDriverAPI (api_);
- return model;
- }
+ /** For internal npu-engine components */
+ Model *allocModel (HWmemImpl *impl) {
+ if (impl == nullptr)
+ return nullptr;
- Buffer *allocBuffer (HWmemImpl *impl) {
- if (impl == nullptr)
- return nullptr;
+ Model *model = new Model (impl);
+ model->setDriverAPI (api_);
+ return model;
+ }
- Buffer * buffer = new Buffer (impl);
- buffer->setDriverAPI (api_);
- return buffer;
- }
+ Buffer *allocBuffer (HWmemImpl *impl) {
+ if (impl == nullptr)
+ return nullptr;
+
+ Buffer *buffer = new Buffer (impl);
+ buffer->setDriverAPI (api_);
+ return buffer;
+ }
- SegmentTable * allocSegmentTable (HWmemImpl *impl) {
- if (impl == nullptr)
- return nullptr;
+ SegmentTable *allocSegmentTable (HWmemImpl *impl) {
+ if (impl == nullptr)
+ return nullptr;
- SegmentTable * segt = new SegmentTable (impl);
- segt->setDriverAPI (api_);
- return segt;
- }
+ SegmentTable *segt = new SegmentTable (impl);
+ segt->setDriverAPI (api_);
+ return segt;
+ }
};
/** @brief memory pool allocator: TODO */
class MemPool : public MemAllocator {
- public:
- MemPool (const DriverAPI *api) : MemAllocator (api) {}
+ public:
+ MemPool (const DriverAPI *api) : MemAllocator (api) {}
};
/**
* @note the configuration decides which allocator will be used.
*/
std::unique_ptr<MemAllocator>
-MemAllocator::createInstance (const DriverAPI *api)
-{
+MemAllocator::createInstance (const DriverAPI *api) {
std::unique_ptr<MemAllocator> mem;
if (api != nullptr) {
- if (Conf::getInstance().getResvMemSize() > 0)
+ if (Conf::getInstance ().getResvMemSize () > 0)
mem.reset (new MemPool (api));
else
mem.reset (new MemDefault (api));
/** @brief memory allocator class */
class MemAllocator {
- public:
- static std::unique_ptr<MemAllocator> createInstance (const DriverAPI *api);
+ public:
+ static std::unique_ptr<MemAllocator> createInstance (const DriverAPI *api);
- MemAllocator (const DriverAPI* api) : api_ (api) {}
- virtual ~MemAllocator () {}
+ MemAllocator (const DriverAPI *api) : api_ (api) {}
+ virtual ~MemAllocator () {}
- /**
+ /**
* Below allocates the actual memory. So, the caller don't need to call alloc().
*/
- virtual int allocMemory (size_t size, void **addr) { return -EINVAL; }
- virtual int deallocMemory (int dmabuf, size_t size, void *addr) { return -EINVAL; }
+ virtual int allocMemory (size_t size, void **addr) { return -EINVAL; }
+ virtual int deallocMemory (int dmabuf, size_t size, void *addr) {
+ return -EINVAL;
+ }
- /**
+ /**
* Below does not allocate actual memory as we don't know the type of generic buffer
* from users yet. So, the caller should explictly call alloc() when required.
*/
- virtual Model * allocModel (HWmemImpl *impl) { return nullptr; }
- virtual Buffer * allocBuffer (HWmemImpl *impl) { return nullptr; }
- virtual SegmentTable * allocSegmentTable (HWmemImpl *impl) { return nullptr; }
+ virtual Model *allocModel (HWmemImpl *impl) { return nullptr; }
+ virtual Buffer *allocBuffer (HWmemImpl *impl) { return nullptr; }
+ virtual SegmentTable *allocSegmentTable (HWmemImpl *impl) { return nullptr; }
- protected:
- const DriverAPI *api_; /**< driver API that this allocator is supposed to access */
+ protected:
+ const DriverAPI
+ *api_; /**< driver API that this allocator is supposed to access */
};
#endif /* __NPU_ENGINE_MEM_H__ */
/**
* @brief constructor of metadata class
*/
-Metadata::Metadata (npubin_meta *meta)
- : meta_ (meta)
-{
+Metadata::Metadata (npubin_meta *meta) : meta_ (meta) {
version_ = NPUBIN_VERSION (meta->magiccode);
- if (version_ == 0) /* backward-compatability */
+ if (version_ == 0) /* backward-compatability */
version_ = 1;
}
* @return metadata instance if no error. otherwise nullptr
*/
std::unique_ptr<Metadata>
-Metadata::extractMetadata (void *data)
-{
- npubin_meta * meta_data = static_cast <npubin_meta *> (data);
+Metadata::extractMetadata (void *data) {
+ npubin_meta *meta_data = static_cast<npubin_meta *> (data);
/** check whether it's npu binary */
if (!CHECK_NPUBIN (meta_data->magiccode)) {
std::unique_ptr<Metadata> meta (nullptr);
switch (NPUBIN_VERSION (meta_data->magiccode)) {
- case 0:
- case 1:
- meta.reset (Metadata_v1::createInstance (meta_data));
- break;
- case 2:
- meta.reset (Metadata_v2::createInstance (meta_data));
- break;
- case 3:
- meta.reset (Metadata_v3::createInstance (meta_data));
- break;
- default:
- logerr (TAG, "Invalid NPU binary format version: %" PRIu64 "\n",
- NPUBIN_VERSION (meta_data->magiccode));
- break;
+ case 0:
+ case 1:
+ meta.reset (Metadata_v1::createInstance (meta_data));
+ break;
+ case 2:
+ meta.reset (Metadata_v2::createInstance (meta_data));
+ break;
+ case 3:
+ meta.reset (Metadata_v3::createInstance (meta_data));
+ break;
+ default:
+ logerr (TAG, "Invalid NPU binary format version: %" PRIu64 "\n",
+ NPUBIN_VERSION (meta_data->magiccode));
+ break;
}
if (meta.get () == nullptr)
}
/** @brief constructor of npubinfmt v1 */
-Metadata_v1::Metadata_v1 (npubin_meta *meta)
- : Metadata (meta)
-{
+Metadata_v1::Metadata_v1 (npubin_meta *meta) : Metadata (meta) {
/** set dummy input/output dimensions as metadata v1 does not have dimension info. */
for (uint32_t idx = 0; idx < MAX_TENSORS; idx++) {
input_dims[idx][0] = getInputTensorSize (idx, DATA_LAYOUT_SRNPU);
/** @brief create npubinfmt v1 instance with sanity check */
Metadata_v1 *
-Metadata_v1::createInstance (npubin_meta *meta)
-{
+Metadata_v1::createInstance (npubin_meta *meta) {
Metadata_v1 *metadata = new Metadata_v1 (meta);
if (metadata->getSize () != metadata->getMetaSize () +
- metadata->getProgramSize () + metadata->getWeightSize ())
+ metadata->getProgramSize () +
+ metadata->getWeightSize ())
goto sanity_violation;
return metadata;
}
/** @brief constructor of npubinfmt v2 */
-Metadata_v2::Metadata_v2 (npubin_meta *meta)
- : Metadata (meta)
-{
-}
+Metadata_v2::Metadata_v2 (npubin_meta *meta) : Metadata (meta) {}
/** @brief create npubinfmt v1 instance with sanity check */
Metadata_v2 *
-Metadata_v2::createInstance (npubin_meta *meta)
-{
+Metadata_v2::createInstance (npubin_meta *meta) {
Metadata_v2 *metadata = new Metadata_v2 (meta);
if (metadata->getInputNum () > MAX_TENSORS)
if (metadata->getOutputNum () > MAX_TENSORS)
goto sanity_violation;
if (metadata->getSize () != metadata->getMetaSize () +
- metadata->getProgramSize () + metadata->getWeightSize ())
+ metadata->getProgramSize () +
+ metadata->getWeightSize ())
goto sanity_violation;
return metadata;
/** @brief calculate tensor size depending on specified layout */
uint32_t
-Metadata_v2::getInputTensorSize (uint32_t idx, data_layout layout) const
-{
+Metadata_v2::getInputTensorSize (uint32_t idx, data_layout layout) const {
assert (idx < getInputNum ());
const uint32_t *dims = getInputDims (idx);
/** @brief calculate tensor size depending on specified layout */
uint32_t
-Metadata_v2::getOutputTensorSize (uint32_t idx, data_layout layout) const
-{
+Metadata_v2::getOutputTensorSize (uint32_t idx, data_layout layout) const {
assert (idx < getOutputNum ());
const uint32_t *dims = getOutputDims (idx);
}
/** @brief constructor of npubinfmt v3 */
-Metadata_v3::Metadata_v3 (npubin_meta *meta)
- : Metadata (meta)
-{
-}
+Metadata_v3::Metadata_v3 (npubin_meta *meta) : Metadata (meta) {}
/** @brief create npubinfmt v1 instance with sanity check */
Metadata_v3 *
-Metadata_v3::createInstance (npubin_meta *meta)
-{
+Metadata_v3::createInstance (npubin_meta *meta) {
Metadata_v3 *metadata = new Metadata_v3 (meta);
if (metadata->getSegmentsNum () > MAX_SEGMENTS)
metadata->getOutputNum () > MAX_TENSORS)
goto sanity_violation;
- if (metadata->getWeightSize() > 0 &&
+ if (metadata->getWeightSize () > 0 &&
metadata->getWeightSegmentIndex () >= metadata->getSegmentsNum ())
- goto sanity_violation;
+ goto sanity_violation;
for (uint32_t i = 0; i < metadata->getInputNum (); i++) {
if (metadata->getInputSegmentIndex (i) >= metadata->getSegmentsNum ())
goto sanity_violation;
}
if (metadata->getSize () != metadata->getMetaSize () +
- metadata->getProgramSize () + metadata->getWeightSize ())
+ metadata->getProgramSize () +
+ metadata->getWeightSize ())
goto sanity_violation;
return metadata;
* @todo need to fix this when the data manipulation in TRIV2 is finalized
*/
uint32_t
-Metadata_v3::getInputTensorSize (uint32_t idx, data_layout layout) const
-{
+Metadata_v3::getInputTensorSize (uint32_t idx, data_layout layout) const {
assert (idx < getInputNum ());
const uint32_t *dims = getInputDims (idx);
* @todo need to fix this when the data manipulation in TRIV2 is finalized
*/
uint32_t
-Metadata_v3::getOutputTensorSize (uint32_t idx, data_layout layout) const
-{
+Metadata_v3::getOutputTensorSize (uint32_t idx, data_layout layout) const {
assert (idx < getOutputNum ());
const uint32_t *dims = getOutputDims (idx);
}
/** @brief constructor of model class */
-Model::Model (const HWmemImpl* impl)
- : HWmem (impl), weight_data_ (nullptr), program_data_ (nullptr),
- extended_meta_ (nullptr), meta_ (nullptr)
-{
- model_id_ = global_model_id_.fetch_add(1);
+Model::Model (const HWmemImpl *impl)
+ : HWmem (impl),
+ weight_data_ (nullptr),
+ program_data_ (nullptr),
+ extended_meta_ (nullptr),
+ meta_ (nullptr) {
+ model_id_ = global_model_id_.fetch_add (1);
internal_id_ = 0xFFFFFFFFFFFFFFFF;
/** set default values */
}
/** @brief destructor of model class */
-Model::~Model ()
-{
+Model::~Model () {
if (weight_data_ != nullptr)
delete weight_data_;
if (program_data_ != nullptr)
* @return 0 if no error, otherwise a negative errno
*/
int
-Model::setDataInfo (const tensors_data_info *in, const tensors_data_info *out)
-{
+Model::setDataInfo (const tensors_data_info *in, const tensors_data_info *out) {
if (in == nullptr || out == nullptr) {
logerr (TAG, "invalid arguments provided\n");
return -EINVAL;
}
void
-Model::updateDataInfo ()
-{
+Model::updateDataInfo () {
for (uint32_t idx = 0; idx < in_.num_info; idx++) {
if (in_.info[idx].layout == DATA_LAYOUT_MODEL) {
if (meta_->getVersion () < 3)
* @return 0 if no error, otherwise a negative errno
*/
int
-Model::setMetadata (void *data)
-{
+Model::setMetadata (void *data) {
if (data == nullptr)
return -EINVAL;
this->meta_ = Metadata::extractMetadata (data);
- if (this->meta_.get() == nullptr)
+ if (this->meta_.get () == nullptr)
return -EINVAL;
in_.num_info = getInputTensorNum ();
* @brief get the number of input tensors
*/
uint32_t
-Model::getInputTensorNum () const
-{
- if (meta_.get() == nullptr) {
+Model::getInputTensorNum () const {
+ if (meta_.get () == nullptr) {
logerr (TAG, "Invalid metadata\n");
return 0;
}
* @brief get the number of input tensors
*/
uint32_t
-Model::getOutputTensorNum () const
-{
- if (meta_.get() == nullptr) {
+Model::getOutputTensorNum () const {
+ if (meta_.get () == nullptr) {
logerr (TAG, "Invalid metadata\n");
return 0;
}
* @return the size of input tensor
*/
uint32_t
-Model::getInputTensorSize (uint32_t idx) const
-{
- if (meta_.get() == nullptr) {
+Model::getInputTensorSize (uint32_t idx) const {
+ if (meta_.get () == nullptr) {
logerr (TAG, "Invalid metadata\n");
return 0;
}
if (in_.num_info <= idx) {
- logerr (TAG, "Input tensor info mismatch. Do setNPU_dataInfo() first properly\n");
+ logerr (
+ TAG,
+ "Input tensor info mismatch. Do setNPU_dataInfo() first properly\n");
return 0;
}
* @return the size of output tensor
*/
uint32_t
-Model::getOutputTensorSize (uint32_t idx) const
-{
- if (meta_.get() == nullptr) {
+Model::getOutputTensorSize (uint32_t idx) const {
+ if (meta_.get () == nullptr) {
logerr (TAG, "Invalid metadata\n");
return 0;
}
if (out_.num_info <= idx) {
- logerr (TAG, "Output tensor info mismatch. Do setNPU_dataInfo() first properly\n");
+ logerr (
+ TAG,
+ "Output tensor info mismatch. Do setNPU_dataInfo() first properly\n");
return 0;
}
*/
const tensor_data_info *
Model::getInputDataInfo (uint32_t idx) const {
- if (meta_.get() == nullptr) {
+ if (meta_.get () == nullptr) {
logerr (TAG, "Invalid metadata\n");
return nullptr;
}
if (in_.num_info <= idx) {
- logerr (TAG, "Input tensor info mismatch. Do setNPU_dataInfo() first properly\n");
+ logerr (
+ TAG,
+ "Input tensor info mismatch. Do setNPU_dataInfo() first properly\n");
return nullptr;
}
*/
const tensor_data_info *
Model::getOutputDataInfo (uint32_t idx) const {
- if (meta_.get() == nullptr) {
+ if (meta_.get () == nullptr) {
logerr (TAG, "Invalid metadata\n");
return nullptr;
}
if (out_.num_info <= idx) {
- logerr (TAG, "Output tensor info mismatch. Do setNPU_dataInfo() first properly\n");
+ logerr (
+ TAG,
+ "Output tensor info mismatch. Do setNPU_dataInfo() first properly\n");
return nullptr;
}
/** check tensors info */
if (input_num != in_.num_info) {
- logerr (TAG, "The number of input tensors is different. Please set setNPU_dataInfo()\n");
+ logerr (TAG,
+ "The number of input tensors is different. Please set "
+ "setNPU_dataInfo()\n");
return false;
}
if (output_num != out_.num_info) {
- logerr (TAG, "The number of output tensors is different. Please set setNPU_dataInfo()\n");
+ logerr (TAG,
+ "The number of output tensors is different. Please set "
+ "setNPU_dataInfo()\n");
return false;
}
* @brief get the size of data type
* @return the data size
*/
-uint32_t get_data_size (data_type type)
-{
+uint32_t
+get_data_size (data_type type) {
switch (type) {
case DATA_TYPE_SRNPU:
case DATA_TYPE_INT8:
case DATA_TYPE_UINT8:
case DATA_TYPE_QASYMM8:
- return sizeof(uint8_t);
+ return sizeof (uint8_t);
case DATA_TYPE_INT16:
case DATA_TYPE_UINT16:
case DATA_TYPE_QSYMM16:
- return sizeof(uint16_t);
+ return sizeof (uint16_t);
case DATA_TYPE_INT32:
case DATA_TYPE_UINT32:
case DATA_TYPE_FLOAT32:
- return sizeof(uint32_t);
+ return sizeof (uint32_t);
case DATA_TYPE_INT64:
case DATA_TYPE_UINT64:
case DATA_TYPE_FLOAT64:
- return sizeof(uint64_t);
+ return sizeof (uint64_t);
default:
return 0;
}
/** @brief model metadata class */
class Metadata {
- public:
- /** @brief extract metadata info from the data and create metadata instance */
- static std::unique_ptr<Metadata> extractMetadata (void *data);
-
- virtual ~Metadata () {};
-
- virtual uint32_t getInputNum () const = 0;
- virtual uint32_t getOutputNum () const = 0;
-
- virtual uint32_t getInputOffset (uint32_t idx) const = 0;
- virtual uint32_t getOutputOffset (uint32_t idx) const = 0;
-
- virtual uint32_t getInputTensorSize (uint32_t idx, data_layout layout) const = 0;
- virtual uint32_t getOutputTensorSize (uint32_t idx, data_layout layout) const = 0;
-
- virtual uint32_t getInputElemSize (uint32_t idx) const = 0;
- virtual uint32_t getOutputElemSize (uint32_t idx) const = 0;
-
- virtual const uint32_t* getInputDims (uint32_t idx) const = 0;
- virtual const uint32_t* getOutputDims (uint32_t idx) const = 0;
-
- virtual uint32_t getInputQuantZero (uint32_t idx) const { return default_quant_zero; }
- virtual float getInputQuantScale (uint32_t idx) const { return default_quant_scale; }
-
- virtual uint32_t getOutputQuantZero (uint32_t idx) const { return default_quant_zero; }
- virtual float getOutputQuantScale (uint32_t idx) const { return default_quant_scale; }
-
- virtual data_type getInputQuantType (uint32_t idx) const { return default_data_type; }
- virtual data_type getOutputQuantType (uint32_t idx) const { return default_data_type; }
-
- virtual uint32_t getSegmentsNum () const { return 0; }
- virtual uint32_t getSegmentSize (uint32_t idx) const { return 0; }
- virtual uint32_t getWeightSegmentIndex () const { return 0; }
- virtual uint32_t getInputSegmentIndex (uint32_t idx) const { return 0; }
- virtual uint32_t getOutputSegmentIndex (uint32_t idx) const { return 0; }
- virtual uint32_t getInputSegmentOffset (uint32_t idx) const { return 0; }
- virtual uint32_t getOutputSegmentOffset (uint32_t idx) const { return 0; }
-
- virtual uint32_t getInputEmodY (uint32_t idx) const { return 0; }
- virtual uint32_t getInputEmodZ (uint32_t idx) const { return 0; }
-
- virtual uint32_t getOutputEmodY (uint32_t idx) const { return 0; }
- virtual uint32_t getOutputEmodZ (uint32_t idx) const { return 0; }
-
- uint64_t getSize () const { return meta_->size; }
- uint64_t getProgramSize () const { return meta_->program_size; }
- uint64_t getWeightSize () const { return meta_->weight_size; }
- uint64_t getBufferSize () const { return meta_->buffer_size; }
- uint32_t getMetaSize () const { return NPUBIN_META_TOTAL_SIZE(meta_->magiccode); }
- uint32_t getMetaExtendedSize () const { return NPUBIN_META_EXTENDED_SIZE(meta_->magiccode); }
- uint32_t getTops () const { return NPU_VERSION_TOPS (getNPUVersion ()); }
-
- int getVersion () const { return version_; }
- uint64_t getNPUVersion () const { return meta_->npu_version; }
-
- protected:
- Metadata (npubin_meta *meta);
-
- npubin_meta *meta_; /**< user-exposed metadata structure */
- int version_; /**< metadata version */
+ public:
+ /** @brief extract metadata info from the data and create metadata instance */
+ static std::unique_ptr<Metadata> extractMetadata (void *data);
+
+ virtual ~Metadata (){};
+
+ virtual uint32_t getInputNum () const = 0;
+ virtual uint32_t getOutputNum () const = 0;
+
+ virtual uint32_t getInputOffset (uint32_t idx) const = 0;
+ virtual uint32_t getOutputOffset (uint32_t idx) const = 0;
+
+ virtual uint32_t getInputTensorSize (uint32_t idx,
+ data_layout layout) const = 0;
+ virtual uint32_t getOutputTensorSize (uint32_t idx,
+ data_layout layout) const = 0;
+
+ virtual uint32_t getInputElemSize (uint32_t idx) const = 0;
+ virtual uint32_t getOutputElemSize (uint32_t idx) const = 0;
+
+ virtual const uint32_t *getInputDims (uint32_t idx) const = 0;
+ virtual const uint32_t *getOutputDims (uint32_t idx) const = 0;
+
+ virtual uint32_t getInputQuantZero (uint32_t idx) const {
+ return default_quant_zero;
+ }
+ virtual float getInputQuantScale (uint32_t idx) const {
+ return default_quant_scale;
+ }
+
+ virtual uint32_t getOutputQuantZero (uint32_t idx) const {
+ return default_quant_zero;
+ }
+ virtual float getOutputQuantScale (uint32_t idx) const {
+ return default_quant_scale;
+ }
+
+ virtual data_type getInputQuantType (uint32_t idx) const {
+ return default_data_type;
+ }
+ virtual data_type getOutputQuantType (uint32_t idx) const {
+ return default_data_type;
+ }
+
+ virtual uint32_t getSegmentsNum () const { return 0; }
+ virtual uint32_t getSegmentSize (uint32_t idx) const { return 0; }
+ virtual uint32_t getWeightSegmentIndex () const { return 0; }
+ virtual uint32_t getInputSegmentIndex (uint32_t idx) const { return 0; }
+ virtual uint32_t getOutputSegmentIndex (uint32_t idx) const { return 0; }
+ virtual uint32_t getInputSegmentOffset (uint32_t idx) const { return 0; }
+ virtual uint32_t getOutputSegmentOffset (uint32_t idx) const { return 0; }
+
+ virtual uint32_t getInputEmodY (uint32_t idx) const { return 0; }
+ virtual uint32_t getInputEmodZ (uint32_t idx) const { return 0; }
+
+ virtual uint32_t getOutputEmodY (uint32_t idx) const { return 0; }
+ virtual uint32_t getOutputEmodZ (uint32_t idx) const { return 0; }
+
+ uint64_t getSize () const { return meta_->size; }
+ uint64_t getProgramSize () const { return meta_->program_size; }
+ uint64_t getWeightSize () const { return meta_->weight_size; }
+ uint64_t getBufferSize () const { return meta_->buffer_size; }
+ uint32_t getMetaSize () const {
+ return NPUBIN_META_TOTAL_SIZE (meta_->magiccode);
+ }
+ uint32_t getMetaExtendedSize () const {
+ return NPUBIN_META_EXTENDED_SIZE (meta_->magiccode);
+ }
+ uint32_t getTops () const { return NPU_VERSION_TOPS (getNPUVersion ()); }
+
+ int getVersion () const { return version_; }
+ uint64_t getNPUVersion () const { return meta_->npu_version; }
+
+ protected:
+ Metadata (npubin_meta *meta);
+
+ npubin_meta *meta_; /**< user-exposed metadata structure */
+ int version_; /**< metadata version */
};
/** @brief metadata version 1: support only a single pair of input/ouput tensors */
class Metadata_v1 : public Metadata {
- public:
- /** @brief create metadata with npubinfmt v1. if sanity check is failed, return nullptr */
- static Metadata_v1 * createInstance (npubin_meta *meta);
-
- uint32_t getInputNum () const override { return 1; }
- uint32_t getOutputNum () const override { return 1; }
-
- uint32_t getInputOffset (uint32_t idx) const override {
- return static_cast <uint32_t> (meta_->input_offset);
- }
- uint32_t getOutputOffset (uint32_t idx) const override {
- return static_cast <uint32_t> (meta_->output_offset);
- }
- uint32_t getInputTensorSize (uint32_t idx, data_layout layout) const override {
- return static_cast <uint32_t> (meta_->input_size);
- }
- uint32_t getOutputTensorSize (uint32_t idx, data_layout layout) const override {
- return static_cast <uint32_t> (meta_->output_size);
- }
- uint32_t getInputElemSize (uint32_t idx) const override {
- return 1;
- }
- uint32_t getOutputElemSize (uint32_t idx) const override {
- return 1;
- }
- const uint32_t* getInputDims (uint32_t idx) const override {
- return input_dims[idx];
- }
- const uint32_t* getOutputDims (uint32_t idx) const override {
- return output_dims[idx];
- }
-
- data_type getInputQuantType (uint32_t idx) const override {
- return DATA_TYPE_SRNPU;
- }
-
- data_type getOutputQuantType (uint32_t idx) const override {
- return DATA_TYPE_SRNPU;
- }
- private:
- Metadata_v1 (npubin_meta *meta);
-
- uint32_t input_dims[MAX_TENSORS][MAX_RANK]; /**< dummy input dimensions */
- uint32_t output_dims[MAX_TENSORS][MAX_RANK]; /**< dummy output dimensions */
+ public:
+ /** @brief create metadata with npubinfmt v1. if sanity check is failed, return nullptr */
+ static Metadata_v1 *createInstance (npubin_meta *meta);
+
+ uint32_t getInputNum () const override { return 1; }
+ uint32_t getOutputNum () const override { return 1; }
+
+ uint32_t getInputOffset (uint32_t idx) const override {
+ return static_cast<uint32_t> (meta_->input_offset);
+ }
+ uint32_t getOutputOffset (uint32_t idx) const override {
+ return static_cast<uint32_t> (meta_->output_offset);
+ }
+ uint32_t getInputTensorSize (uint32_t idx,
+ data_layout layout) const override {
+ return static_cast<uint32_t> (meta_->input_size);
+ }
+ uint32_t getOutputTensorSize (uint32_t idx,
+ data_layout layout) const override {
+ return static_cast<uint32_t> (meta_->output_size);
+ }
+ uint32_t getInputElemSize (uint32_t idx) const override { return 1; }
+ uint32_t getOutputElemSize (uint32_t idx) const override { return 1; }
+ const uint32_t *getInputDims (uint32_t idx) const override {
+ return input_dims[idx];
+ }
+ const uint32_t *getOutputDims (uint32_t idx) const override {
+ return output_dims[idx];
+ }
+
+ data_type getInputQuantType (uint32_t idx) const override {
+ return DATA_TYPE_SRNPU;
+ }
+
+ data_type getOutputQuantType (uint32_t idx) const override {
+ return DATA_TYPE_SRNPU;
+ }
+
+ private:
+ Metadata_v1 (npubin_meta *meta);
+
+ uint32_t input_dims[MAX_TENSORS][MAX_RANK]; /**< dummy input dimensions */
+ uint32_t output_dims[MAX_TENSORS][MAX_RANK]; /**< dummy output dimensions */
};
/** @brief metadata version 2: support multiple input/output tensors */
class Metadata_v2 : public Metadata {
- public:
- /** @brief create metadata with npubinfmt v2. if sanity check is failed, return nullptr */
- static Metadata_v2 * createInstance (npubin_meta *meta);
-
- uint32_t getInputNum () const override { return meta_->input_num; }
- uint32_t getOutputNum () const override { return meta_->output_num; }
-
- uint32_t getInputOffset (uint32_t idx) const override {
- assert (idx < getInputNum ());
- return meta_->input_offsets[idx];
- }
- uint32_t getOutputOffset (uint32_t idx) const override {
- assert (idx < getOutputNum ());
- return meta_->output_offsets[idx];
- }
-
- uint32_t getInputTensorSize (uint32_t idx, data_layout layout) const override;
- uint32_t getOutputTensorSize (uint32_t idx, data_layout layout) const override;
-
- uint32_t getInputElemSize (uint32_t idx) const override {
- assert (idx < getInputNum ());
- return meta_->input_elem_size[idx];
- }
- uint32_t getOutputElemSize (uint32_t idx) const override {
- assert (idx < getOutputNum ());
- return meta_->output_elem_size[idx];
- }
- const uint32_t* getInputDims (uint32_t idx) const override {
- assert (idx < getInputNum ());
- return meta_->input_dims[idx];
- }
- const uint32_t* getOutputDims (uint32_t idx) const override {
- assert (idx < getOutputNum ());
- return meta_->output_dims[idx];
- }
-
- uint32_t getInputQuantZero (uint32_t idx) const override {
- assert (idx < getInputNum ());
- return meta_->input_quant_z[idx];
- }
- float getInputQuantScale (uint32_t idx) const override {
- assert (idx < getInputNum ());
- return meta_->input_quant_s[idx];
- }
- uint32_t getOutputQuantZero (uint32_t idx) const override {
- assert (idx < getOutputNum ());
- return meta_->output_quant_z[idx];
- }
- float getOutputQuantScale (uint32_t idx) const override {
- assert (idx < getOutputNum ());
- return meta_->output_quant_s[idx];
- }
-
- data_type getInputQuantType (uint32_t idx) const override {
- assert (idx < getOutputNum ());
- return DATA_TYPE_SRNPU;
- }
-
- data_type getOutputQuantType (uint32_t idx) const override {
- assert (idx < getOutputNum ());
- return DATA_TYPE_SRNPU;
- }
-
- private:
- Metadata_v2 (npubin_meta *meta);
+ public:
+ /** @brief create metadata with npubinfmt v2. if sanity check is failed, return nullptr */
+ static Metadata_v2 *createInstance (npubin_meta *meta);
+
+ uint32_t getInputNum () const override { return meta_->input_num; }
+ uint32_t getOutputNum () const override { return meta_->output_num; }
+
+ uint32_t getInputOffset (uint32_t idx) const override {
+ assert (idx < getInputNum ());
+ return meta_->input_offsets[idx];
+ }
+ uint32_t getOutputOffset (uint32_t idx) const override {
+ assert (idx < getOutputNum ());
+ return meta_->output_offsets[idx];
+ }
+
+ uint32_t getInputTensorSize (uint32_t idx, data_layout layout) const override;
+ uint32_t getOutputTensorSize (uint32_t idx,
+ data_layout layout) const override;
+
+ uint32_t getInputElemSize (uint32_t idx) const override {
+ assert (idx < getInputNum ());
+ return meta_->input_elem_size[idx];
+ }
+ uint32_t getOutputElemSize (uint32_t idx) const override {
+ assert (idx < getOutputNum ());
+ return meta_->output_elem_size[idx];
+ }
+ const uint32_t *getInputDims (uint32_t idx) const override {
+ assert (idx < getInputNum ());
+ return meta_->input_dims[idx];
+ }
+ const uint32_t *getOutputDims (uint32_t idx) const override {
+ assert (idx < getOutputNum ());
+ return meta_->output_dims[idx];
+ }
+
+ uint32_t getInputQuantZero (uint32_t idx) const override {
+ assert (idx < getInputNum ());
+ return meta_->input_quant_z[idx];
+ }
+ float getInputQuantScale (uint32_t idx) const override {
+ assert (idx < getInputNum ());
+ return meta_->input_quant_s[idx];
+ }
+ uint32_t getOutputQuantZero (uint32_t idx) const override {
+ assert (idx < getOutputNum ());
+ return meta_->output_quant_z[idx];
+ }
+ float getOutputQuantScale (uint32_t idx) const override {
+ assert (idx < getOutputNum ());
+ return meta_->output_quant_s[idx];
+ }
+
+ data_type getInputQuantType (uint32_t idx) const override {
+ assert (idx < getOutputNum ());
+ return DATA_TYPE_SRNPU;
+ }
+
+ data_type getOutputQuantType (uint32_t idx) const override {
+ assert (idx < getOutputNum ());
+ return DATA_TYPE_SRNPU;
+ }
+
+ private:
+ Metadata_v2 (npubin_meta *meta);
};
/** @brief metadata version 3: support a segment table */
class Metadata_v3 : public Metadata {
- public:
- /** @brief create metadata with npubinfmt v3. if sanity check is failed, return nullptr */
- static Metadata_v3 * createInstance (npubin_meta *meta);
-
- uint32_t getInputNum () const override { return meta_->input_seg_num; }
- uint32_t getOutputNum () const override { return meta_->output_seg_num; }
-
- uint32_t getInputOffset (uint32_t idx) const override { return 0; }
- uint32_t getOutputOffset (uint32_t idx) const override { return 0; }
-
- uint32_t getInputTensorSize (uint32_t idx, data_layout layout) const override;
- uint32_t getOutputTensorSize (uint32_t idx, data_layout layout) const override;
-
- uint32_t getInputElemSize (uint32_t idx) const override {
- return get_data_size (getInputQuantType (idx));
- }
- uint32_t getOutputElemSize (uint32_t idx) const override {
- return get_data_size (getOutputQuantType (idx));
- }
-
- const uint32_t* getInputDims (uint32_t idx) const override {
- assert (idx < getInputNum ());
- return meta_->input_seg_dims[idx];
- }
- const uint32_t* getOutputDims (uint32_t idx) const override {
- assert (idx < getOutputNum ());
- return meta_->output_seg_dims[idx];
- }
-
- uint32_t getInputEmodY (uint32_t idx) const override {
- assert (idx < getInputNum ());
- return meta_->input_seg_emod_y[idx];
- }
- uint32_t getInputEmodZ (uint32_t idx) const override {
- assert (idx < getInputNum ());
- return meta_->input_seg_emod_z[idx];
- }
-
- uint32_t getOutputEmodY (uint32_t idx) const override {
- assert (idx < getOutputNum ());
- return meta_->output_seg_emod_y[idx];
- }
- uint32_t getOutputEmodZ (uint32_t idx) const override {
- assert (idx < getOutputNum ());
- return meta_->output_seg_emod_z[idx];
- }
-
- uint32_t getInputQuantZero (uint32_t idx) const override {
- assert (idx < getInputNum ());
- return meta_->input_seg_quant_z[idx];
- }
- float getInputQuantScale (uint32_t idx) const override {
- assert (idx < getInputNum ());
- return meta_->input_seg_quant_s[idx];
- }
- uint32_t getOutputQuantZero (uint32_t idx) const override {
- assert (idx < getOutputNum ());
- return meta_->output_seg_quant_z[idx];
- }
- float getOutputQuantScale (uint32_t idx) const override {
- assert (idx < getOutputNum ());
- return meta_->output_seg_quant_s[idx];
- }
-
- data_type getInputQuantType (uint32_t idx) const override {
- assert (idx < getInputNum ());
- return meta_->input_seg_quant_type[idx];
- }
- data_type getOutputQuantType (uint32_t idx) const override {
- assert (idx < getOutputNum ());
- return meta_->output_seg_quant_type[idx];
- }
-
- uint32_t getSegmentsNum () const override { return meta_->segment_num; }
- uint32_t getSegmentSize (uint32_t idx) const override {
- assert (idx < getSegmentsNum ());
- return meta_->segment_size[idx];
- }
- uint32_t getWeightSegmentIndex () const override {
- return meta_->weight_seg_idx;
- }
- uint32_t getInputSegmentIndex (uint32_t idx) const override {
- assert (idx < getInputNum ());
- return meta_->input_seg_idx[idx];
- }
- uint32_t getOutputSegmentIndex (uint32_t idx) const override {
- assert (idx < getOutputNum ());
- return meta_->output_seg_idx[idx];
- }
- uint32_t getInputSegmentOffset (uint32_t idx) const override {
- assert (idx < getInputNum ());
- return meta_->input_seg_off[idx];
- }
- uint32_t getOutputSegmentOffset (uint32_t idx) const override {
- assert (idx < getOutputNum ());
- return meta_->output_seg_off[idx];
- }
-
- private:
- Metadata_v3 (npubin_meta *meta);
+ public:
+ /** @brief create metadata with npubinfmt v3. if sanity check is failed, return nullptr */
+ static Metadata_v3 *createInstance (npubin_meta *meta);
+
+ uint32_t getInputNum () const override { return meta_->input_seg_num; }
+ uint32_t getOutputNum () const override { return meta_->output_seg_num; }
+
+ uint32_t getInputOffset (uint32_t idx) const override { return 0; }
+ uint32_t getOutputOffset (uint32_t idx) const override { return 0; }
+
+ uint32_t getInputTensorSize (uint32_t idx, data_layout layout) const override;
+ uint32_t getOutputTensorSize (uint32_t idx,
+ data_layout layout) const override;
+
+ uint32_t getInputElemSize (uint32_t idx) const override {
+ return get_data_size (getInputQuantType (idx));
+ }
+ uint32_t getOutputElemSize (uint32_t idx) const override {
+ return get_data_size (getOutputQuantType (idx));
+ }
+
+ const uint32_t *getInputDims (uint32_t idx) const override {
+ assert (idx < getInputNum ());
+ return meta_->input_seg_dims[idx];
+ }
+ const uint32_t *getOutputDims (uint32_t idx) const override {
+ assert (idx < getOutputNum ());
+ return meta_->output_seg_dims[idx];
+ }
+
+ uint32_t getInputEmodY (uint32_t idx) const override {
+ assert (idx < getInputNum ());
+ return meta_->input_seg_emod_y[idx];
+ }
+ uint32_t getInputEmodZ (uint32_t idx) const override {
+ assert (idx < getInputNum ());
+ return meta_->input_seg_emod_z[idx];
+ }
+
+ uint32_t getOutputEmodY (uint32_t idx) const override {
+ assert (idx < getOutputNum ());
+ return meta_->output_seg_emod_y[idx];
+ }
+ uint32_t getOutputEmodZ (uint32_t idx) const override {
+ assert (idx < getOutputNum ());
+ return meta_->output_seg_emod_z[idx];
+ }
+
+ uint32_t getInputQuantZero (uint32_t idx) const override {
+ assert (idx < getInputNum ());
+ return meta_->input_seg_quant_z[idx];
+ }
+ float getInputQuantScale (uint32_t idx) const override {
+ assert (idx < getInputNum ());
+ return meta_->input_seg_quant_s[idx];
+ }
+ uint32_t getOutputQuantZero (uint32_t idx) const override {
+ assert (idx < getOutputNum ());
+ return meta_->output_seg_quant_z[idx];
+ }
+ float getOutputQuantScale (uint32_t idx) const override {
+ assert (idx < getOutputNum ());
+ return meta_->output_seg_quant_s[idx];
+ }
+
+ data_type getInputQuantType (uint32_t idx) const override {
+ assert (idx < getInputNum ());
+ return meta_->input_seg_quant_type[idx];
+ }
+ data_type getOutputQuantType (uint32_t idx) const override {
+ assert (idx < getOutputNum ());
+ return meta_->output_seg_quant_type[idx];
+ }
+
+ uint32_t getSegmentsNum () const override { return meta_->segment_num; }
+ uint32_t getSegmentSize (uint32_t idx) const override {
+ assert (idx < getSegmentsNum ());
+ return meta_->segment_size[idx];
+ }
+ uint32_t getWeightSegmentIndex () const override {
+ return meta_->weight_seg_idx;
+ }
+ uint32_t getInputSegmentIndex (uint32_t idx) const override {
+ assert (idx < getInputNum ());
+ return meta_->input_seg_idx[idx];
+ }
+ uint32_t getOutputSegmentIndex (uint32_t idx) const override {
+ assert (idx < getOutputNum ());
+ return meta_->output_seg_idx[idx];
+ }
+ uint32_t getInputSegmentOffset (uint32_t idx) const override {
+ assert (idx < getInputNum ());
+ return meta_->input_seg_off[idx];
+ }
+ uint32_t getOutputSegmentOffset (uint32_t idx) const override {
+ assert (idx < getOutputNum ());
+ return meta_->output_seg_off[idx];
+ }
+
+ private:
+ Metadata_v3 (npubin_meta *meta);
};
/** @brief model class derived from hwmem */
class Model : public HWmem {
- public:
- Model (const HWmemImpl* impl);
- ~Model ();
+ public:
+ Model (const HWmemImpl *impl);
+ ~Model ();
- void setConstraint (const npuConstraint& constraint) { constraint_ = constraint; }
- int setDataInfo (const tensors_data_info *in, const tensors_data_info *out);
- int setMetadata (void *data);
- void setExtendedMetadata (HWmem *data) { extended_meta_ = data; }
- void setWeightData (HWmem *data) { weight_data_ = data; }
- void setProgramData (HWmem *data) { program_data_ = data; }
- void setInternalID (uint64_t id) { internal_id_ = id; }
+ void setConstraint (const npuConstraint &constraint) {
+ constraint_ = constraint;
+ }
+ int setDataInfo (const tensors_data_info *in, const tensors_data_info *out);
+ int setMetadata (void *data);
+ void setExtendedMetadata (HWmem *data) { extended_meta_ = data; }
+ void setWeightData (HWmem *data) { weight_data_ = data; }
+ void setProgramData (HWmem *data) { program_data_ = data; }
+ void setInternalID (uint64_t id) { internal_id_ = id; }
- void updateDataInfo ();
+ void updateDataInfo ();
- const Metadata *getMetadata () const { return meta_.get(); }
+ const Metadata *getMetadata () const { return meta_.get (); }
- uint32_t getID () const { return model_id_; }
- uint64_t getInternalID () const { return internal_id_; }
- HWmem * getWeightData () const { return weight_data_; }
- HWmem * getProgramData () const { return program_data_; }
- HWmem * getExtendedMetadata () const { return extended_meta_; }
+ uint32_t getID () const { return model_id_; }
+ uint64_t getInternalID () const { return internal_id_; }
+ HWmem *getWeightData () const { return weight_data_; }
+ HWmem *getProgramData () const { return program_data_; }
+ HWmem *getExtendedMetadata () const { return extended_meta_; }
- uint32_t getInputTensorNum () const;
- uint32_t getOutputTensorNum () const;
+ uint32_t getInputTensorNum () const;
+ uint32_t getOutputTensorNum () const;
- uint32_t getInputTensorSize (uint32_t idx) const;
- uint32_t getOutputTensorSize (uint32_t idx) const;
+ uint32_t getInputTensorSize (uint32_t idx) const;
+ uint32_t getOutputTensorSize (uint32_t idx) const;
- npuConstraint getConstraint () const { return constraint_; }
+ npuConstraint getConstraint () const { return constraint_; }
- const tensor_data_info * getInputDataInfo (uint32_t idx) const;
- const tensor_data_info * getOutputDataInfo (uint32_t idx) const;
+ const tensor_data_info *getInputDataInfo (uint32_t idx) const;
+ const tensor_data_info *getOutputDataInfo (uint32_t idx) const;
- bool finalize ();
+ bool finalize ();
- private:
- static std::atomic<uint32_t> global_model_id_; /**< global model id */
- uint32_t model_id_; /**< model id */
- uint64_t internal_id_; /**< model id internally managed by the device */
+ private:
+ static std::atomic<uint32_t> global_model_id_; /**< global model id */
+ uint32_t model_id_; /**< model id */
+ uint64_t internal_id_; /**< model id internally managed by the device */
- HWmem * weight_data_; /**< weight data */
- HWmem * program_data_; /**< weight data */
- HWmem * extended_meta_; /**< extended metadata */
+ HWmem *weight_data_; /**< weight data */
+ HWmem *program_data_; /**< weight data */
+ HWmem *extended_meta_; /**< extended metadata */
- std::unique_ptr<Metadata> meta_; /**< metadata */
- npuConstraint constraint_; /**< per-model constraint (e.g., timeout/priority) */
- tensors_data_info in_; /**< input tensor info. (e.g., data type/layout) */
- tensors_data_info out_; /**< output tensor info. */
+ std::unique_ptr<Metadata> meta_; /**< metadata */
+ npuConstraint
+ constraint_; /**< per-model constraint (e.g., timeout/priority) */
+ tensors_data_info in_; /**< input tensor info. (e.g., data type/layout) */
+ tensors_data_info out_; /**< output tensor info. */
};
#endif /* __NE_MODEL_HH__ */
#include "ne-profiler.h"
-ModelProfiler::ModelProfiler (const DriverAPI * api)
- : api_ (api)
-{
-}
+ModelProfiler::ModelProfiler (const DriverAPI *api) : api_ (api) {}
-ModelProfiler::~ModelProfiler ()
-{
+ModelProfiler::~ModelProfiler () {
profile_map_.clear ();
}
int
-ModelProfiler::appendRequest (int req_id, const Model * model)
-{
- ProfileData * data = new ProfileData (req_id, model);
+ModelProfiler::appendRequest (int req_id, const Model *model) {
+ ProfileData *data = new ProfileData (req_id, model);
return profile_map_.insert (req_id, data);
}
int
-ModelProfiler::getProfile (int req_id, npu_profile *profile)
-{
- ProfileData * data = profile_map_.find (req_id);
+ModelProfiler::getProfile (int req_id, npu_profile *profile) {
+ ProfileData *data = profile_map_.find (req_id);
if (data == nullptr)
return -ENOENT;
- const Model * model = data->getModel ();
+ const Model *model = data->getModel ();
if (model == nullptr)
return -EINVAL;
if (status != 0)
return status;
- HWmem * extended = model->getExtendedMetadata ();
+ HWmem *extended = model->getExtendedMetadata ();
if (extended != nullptr)
manipulateProfile (extended, profile);
}
void
-ModelProfiler::manipulateProfile (HWmem * extended, npu_profile *profile)
-{
+ModelProfiler::manipulateProfile (HWmem *extended, npu_profile *profile) {
npubin_meta_profile *meta_profile =
- reinterpret_cast <npubin_meta_profile *> (extended->getData ());
- npu_profile_layer * new_layers =
- new npu_profile_layer[meta_profile->node_entry_num + 1];
+ reinterpret_cast<npubin_meta_profile *> (extended->getData ());
+ npu_profile_layer *new_layers =
+ new npu_profile_layer[meta_profile->node_entry_num + 1];
- npu_profile_layer * unclassified =
- &new_layers[meta_profile->node_entry_num];
+ npu_profile_layer *unclassified = &new_layers[meta_profile->node_entry_num];
snprintf (unclassified->name, NPU_OPNAME_MAX - 1, "%s", "Unclassified");
unclassified->name[NPU_OPNAME_MAX - 1] = '\x00';
std::cerr << "Zero length detected at ";
std::cerr << id << "th node" << std::endl;
- delete [] new_layers;
+ delete[] new_layers;
return;
}
std::string name (meta_profile->entry_data + pos);
pos += length;
- npu_profile_layer * layer = &new_layers[i];
+ npu_profile_layer *layer = &new_layers[i];
snprintf (layer->name, NPU_OPNAME_MAX - 1, "%s", name.c_str ());
layer->name[NPU_OPNAME_MAX - 1] = '\x00';
layer->sram_read_bytes = 0;
layer->sram_write_bytes = 0;
- node_table.insert(std::make_pair(id, layer));
+ node_table.insert (std::make_pair (id, layer));
}
/** 2) parsing visa table */
pos += sizeof (uint32_t);
if (node_num > 0) {
- uint32_t * node_ids = (uint32_t *) (meta_profile->entry_data + pos);
+ uint32_t *node_ids = (uint32_t *) (meta_profile->entry_data + pos);
for (uint32_t j = 0; j < node_num; j++) {
uint32_t node_id = node_ids[j];
auto it = node_table.find (node_id);
if (it != node_table.end ()) {
- npu_profile_layer * layer = it->second;
+ npu_profile_layer *layer = it->second;
/** TODO: evenly divided to fused layers */
layer->running_cycles += profile->layers[i].running_cycles / node_num;
layer->start_cycles = profile->layers[i].start_cycles;
if (layer->end_cycles < profile->layers[i].end_cycles)
layer->end_cycles = profile->layers[i].end_cycles;
- layer->dram_read_bytes += profile->layers[i].dram_read_bytes / node_num;
- layer->dram_write_bytes += profile->layers[i].dram_write_bytes / node_num;
- layer->sram_read_bytes += profile->layers[i].sram_read_bytes / node_num;
- layer->sram_write_bytes += profile->layers[i].sram_write_bytes / node_num;
+ layer->dram_read_bytes +=
+ profile->layers[i].dram_read_bytes / node_num;
+ layer->dram_write_bytes +=
+ profile->layers[i].dram_write_bytes / node_num;
+ layer->sram_read_bytes +=
+ profile->layers[i].sram_read_bytes / node_num;
+ layer->sram_write_bytes +=
+ profile->layers[i].sram_write_bytes / node_num;
layer->visa_exec_seq = -1;
} else {
std::cerr << "Unable to find the node ID " << node_id << std::endl;
/** 3) profile data mapping */
size_t num_layers = node_table.size ();
if (num_layers > 0) {
- delete [] profile->layers;
+ delete[] profile->layers;
profile->layers = new_layers;
profile->num_layers = num_layers + 1;
} else {
- delete [] new_layers;
+ delete[] new_layers;
}
}
#include <typedef.h>
#include "ne-model.h"
-class ProfileData
-{
- public:
- ProfileData (int req_id, const Model * model) :
- req_id_ (req_id), model_ (model) {}
+class ProfileData {
+ public:
+ ProfileData (int req_id, const Model *model)
+ : req_id_ (req_id), model_ (model) {}
- const Model * getModel () { return model_; }
+ const Model *getModel () { return model_; }
- private:
- int req_id_;
- const Model * model_;
+ private:
+ int req_id_;
+ const Model *model_;
};
-class ModelProfiler
-{
- public:
- ModelProfiler (const DriverAPI * api);
- ~ModelProfiler ();
+class ModelProfiler {
+ public:
+ ModelProfiler (const DriverAPI *api);
+ ~ModelProfiler ();
- int appendRequest (int req_id, const Model * model);
- int getProfile (int req_id, npu_profile *profile);
+ int appendRequest (int req_id, const Model *model);
+ int getProfile (int req_id, npu_profile *profile);
- void manipulateProfile (HWmem * extended, npu_profile *profile);
+ void manipulateProfile (HWmem *extended, npu_profile *profile);
- private:
- const DriverAPI * api_;
- ThreadSafeMap<int, ProfileData> profile_map_;
+ private:
+ const DriverAPI *api_;
+ ThreadSafeMap<int, ProfileData> profile_map_;
};
#endif
/** @brief constructor of request class */
Request::Request (npu_input_opmode opmode)
- : opmode_ (opmode), force_stop_ (false), stopped_ (false),
- model_ (nullptr), data_ (nullptr), cb_ (nullptr)
-{
- request_id_ = Request::global_request_id_.fetch_add(1);
+ : opmode_ (opmode),
+ force_stop_ (false),
+ stopped_ (false),
+ model_ (nullptr),
+ data_ (nullptr),
+ cb_ (nullptr) {
+ request_id_ = Request::global_request_id_.fetch_add (1);
}
/** @brief destructor of scheduler class */
-Scheduler::~Scheduler ()
-{
+Scheduler::~Scheduler () {
/** wait until all callbacks are called */
request_map_.wait_empty ();
}
* @return input service if no error. otherwise nullptr
*/
static InputService *
-getInputService (npu_input_opmode opmode)
-{
+getInputService (npu_input_opmode opmode) {
switch (opmode) {
case NPUINPUT_HOST:
return &HostInputService::getInstance ();
* @param[in] req the request instance
* @return 0 if no error. otherwise a negative errno.
*/
-int Scheduler::handleStop (Request *req)
-{
+int
+Scheduler::handleStop (Request *req) {
if (req->getForceStop ()) {
- std::function <bool (Request *)> functor =
- [] (Request *r) -> bool {
- InputService * service = getInputService (r->getOpmode ());
-
- /* remove a request if it's not scheduled */
- if (service->remove (r->getID ()) != 0) {
- /* In case of already-served requests, let's mark it as stopped */
- r->setStopped ();
- return false;
- }
-
- return true;
- };
+ std::function<bool(Request *)> functor = [](Request *r) -> bool {
+ InputService *service = getInputService (r->getOpmode ());
+
+ /* remove a request if it's not scheduled */
+ if (service->remove (r->getID ()) != 0) {
+ /* In case of already-served requests, let's mark it as stopped */
+ r->setStopped ();
+ return false;
+ }
+
+ return true;
+ };
request_map_.for_each (functor);
/* send the stop signal to the device driver */
* @param[in] service the input service
* @return positive id if no error. otherwise a negative errno.
*/
-int Scheduler::handleHostInput (Request *req, InputService *service)
-{
+int
+Scheduler::handleHostInput (Request *req, InputService *service) {
int req_id = req->getID ();
int status = request_map_.insert (req_id, req);
- assert (status == 0); /** request ID is atomic value. So, should be successful */
+ assert (status ==
+ 0); /** request ID is atomic value. So, should be successful */
- const Model * model = req->getModel ();
- HWmem * data = req->getInferData ();
+ const Model *model = req->getModel ();
+ HWmem *data = req->getInferData ();
auto callback = std::bind (&Scheduler::handleCallback, this, req);
status = service->submit (api_, req_id, model, data, callback);
* @param[in] service the input service
* @return positive id if no error. otherwise a negative errno.
*/
-int Scheduler::handleHwInput (Request *req, InputService *service)
-{
+int
+Scheduler::handleHwInput (Request *req, InputService *service) {
int req_id = req->getID ();
int status = request_map_.insert (req_id, req);
- assert (status == 0); /** request ID is atomic value. So, should be successful */
+ assert (status ==
+ 0); /** request ID is atomic value. So, should be successful */
- const Model * model = req->getModel ();
- HWmem * data = req->getInferData ();
+ const Model *model = req->getModel ();
+ HWmem *data = req->getInferData ();
auto callback = std::bind (&Scheduler::handleCallback, this, req);
SegmentTable *segt = dynamic_cast<SegmentTable *> (data);
* @return 0 or positive value if no error. otherwise a negative errno.
*/
int
-Scheduler::submitRequest (Request *req)
-{
+Scheduler::submitRequest (Request *req) {
if (api_ == nullptr) {
logerr (TAG, "no valid api was set\n");
return -EINVAL;
return -EINVAL;
}
- npu_input_opmode opmode = req->getOpmode();
- InputService * service = getInputService (opmode);
+ npu_input_opmode opmode = req->getOpmode ();
+ InputService *service = getInputService (opmode);
int status = 0;
switch (opmode) {
/** if failed to invoke input service, directly handle callback (if exists) */
if (status < 0)
- handleCallback (req);
+ handleCallback (req);
return status;
}
* @brief handle request callback
*/
void
-Scheduler::handleCallback (Request *req)
-{
- outputCallback callback = req->getCallback();
+Scheduler::handleCallback (Request *req) {
+ outputCallback callback = req->getCallback ();
if (!req->isStopped () && callback != nullptr)
callback ();
/** the request instance is also deleted here */
- request_map_.remove (req->getID());
+ request_map_.remove (req->getID ());
}
/** @brief class def. of requests created from host handler */
class Request {
-public:
+ public:
Request (npu_input_opmode opmode);
void setModel (const Model *model) { model_ = model; }
void setHwDevice (std::string hw_dev) { hw_dev_ = hw_dev; }
std::string getHwDevice () { return hw_dev_; }
-private:
+ private:
static std::atomic<int> global_request_id_;
- int request_id_; /**< request id */
+ int request_id_; /**< request id */
npu_input_opmode opmode_; /**< opmode of the request */
bool force_stop_; /**< indicates force stop */
bool stopped_; /**< stopped request */
- const Model *model_; /**< model of the request */
- HWmem *data_; /**< inference data of the request */
+ const Model *model_; /**< model of the request */
+ HWmem *data_; /**< inference data of the request */
- outputCallback cb_; /**< request callback */
- std::string hw_dev_; /**< HW device path */
+ outputCallback cb_; /**< request callback */
+ std::string hw_dev_; /**< HW device path */
};
/** @brief class def. of scheduler to handle requests */
class Scheduler {
-public:
- Scheduler (const DriverAPI *api) : api_(api) {}
+ public:
+ Scheduler (const DriverAPI *api) : api_ (api) {}
~Scheduler ();
/** @brief submit the request */
int submitRequest (Request *req);
-private:
+ private:
/**
* @brief handle callback. Each request always has its callback
* even if an user does not specify it.
int handleHwInput (Request *req, InputService *service);
ThreadSafeMap<int, Request> request_map_;
- /**< request map */
- const DriverAPI *api_; /**< driver api */
+ /**< request map */
+ const DriverAPI *api_; /**< driver api */
};
#endif /* NE_SCHEDULER_H__ */
#define SEGMENT_ALIGN (0x1000)
/** @brief constructor of segment table derived from HWmem */
-SegmentTable::SegmentTable (const HWmemImpl* impl)
- : HWmem (impl),
- weight_seg_idx_ (nullptr),
- input_seg_idx_ (nullptr),
- output_seg_idx_ (nullptr),
- input_seg_off_ (0),
- output_seg_off_ (0),
- num_total_segments_ (0),
- num_weight_segments_ (0),
- num_input_segments_ (0),
- num_output_segments_ (0)
-{
-}
+SegmentTable::SegmentTable (const HWmemImpl *impl)
+ : HWmem (impl),
+ weight_seg_idx_ (nullptr),
+ input_seg_idx_ (nullptr),
+ output_seg_idx_ (nullptr),
+ input_seg_off_ (0),
+ output_seg_off_ (0),
+ num_total_segments_ (0),
+ num_weight_segments_ (0),
+ num_input_segments_ (0),
+ num_output_segments_ (0) {}
/** @brief destructor of segment table */
-SegmentTable::~SegmentTable ()
-{
+SegmentTable::~SegmentTable () {
segments_.clear ();
if (weight_seg_idx_)
- delete [] weight_seg_idx_;
+ delete[] weight_seg_idx_;
if (input_seg_idx_)
- delete [] input_seg_idx_;
+ delete[] input_seg_idx_;
if (output_seg_idx_)
- delete [] output_seg_idx_;
+ delete[] output_seg_idx_;
}
/**
* @return true if no error. otherwise false
*/
bool
-SegmentTable::setSegmentSlot (HWmem *hwmem, int slot)
-{
- segments_.at (slot) = std::unique_ptr<HWmem>(hwmem);
+SegmentTable::setSegmentSlot (HWmem *hwmem, int slot) {
+ segments_.at (slot) = std::unique_ptr<HWmem> (hwmem);
/**
* The size of a segment table should have a single page size (i.e., 4KB).
* to actual physical addresses, accessed by NPU hardware, before invoking
* an inference. Note that each entry in segment table has 32-bits slot.
*/
- reinterpret_cast<int32_t *>(getData())[slot] = hwmem->getDmabuf ();
- reinterpret_cast<uint32_t *>(getData() + getSize()/2)[slot] = hwmem->getOffset ();
+ reinterpret_cast<int32_t *> (getData ())[slot] = hwmem->getDmabuf ();
+ reinterpret_cast<uint32_t *> (getData () + getSize () / 2)[slot] =
+ hwmem->getOffset ();
/** should be aligned */
return (hwmem->getOffset () % SEGMENT_ALIGN) == 0;
* @return 0 if no error. a negative errno
*/
int
-SegmentTable::createSegment (uint32_t seg_idx, uint32_t seg_size)
-{
- HWmem * hwmem = new HWmem (new HWmemDevice);
+SegmentTable::createSegment (uint32_t seg_idx, uint32_t seg_size) {
+ HWmem *hwmem = new HWmem (new HWmemDevice);
hwmem->setDriverAPI (getDriverAPI ());
int status = hwmem->alloc (seg_size);
*/
int
SegmentTable::createExternalSegment (uint32_t seg_idx, uint32_t seg_size,
- uint32_t seg_off, int dbuf_fd)
-{
+ uint32_t seg_off, int dbuf_fd) {
if (seg_off != 0) {
- logerr (TAG, "Non-zero segment offset is not supported for BUFFER_DMABUF\n");
+ logerr (TAG,
+ "Non-zero segment offset is not supported for BUFFER_DMABUF\n");
return -EINVAL;
}
- HWmem * hwmem = new HWmem (new HWmemExternal);
+ HWmem *hwmem = new HWmem (new HWmemExternal);
hwmem->setDriverAPI (getDriverAPI ());
/** it assume that dmabuf indicates the base address of segment */
hwmem->setDmabuf (dbuf_fd);
}
int
-SegmentTable::createWeightSegment (const Model *model)
-{
+SegmentTable::createWeightSegment (const Model *model) {
const Metadata *meta = model->getMetadata ();
/** weight data is uncessary: skip */
if (meta->getWeightSize () == 0)
uint32_t idx = meta->getWeightSegmentIndex ();
uint32_t size = meta->getSegmentSize (idx);
if (meta->getWeightSize () > size) {
- logerr (TAG, "The size of weight (%" PRIu64 ") is larger than segment size (%u)\n",
- meta->getWeightSize (), size);
+ logerr (TAG,
+ "The size of weight (%" PRIu64
+ ") is larger than segment size (%u)\n",
+ meta->getWeightSize (), size);
return -EINVAL;
}
}
/** it's a just wrapper of weight data */
- HWmem * hwmem = new HWmem (new HWmemExternal);
+ HWmem *hwmem = new HWmem (new HWmemExternal);
hwmem->setDriverAPI (getDriverAPI ());
- HWmem * weight = model->getWeightData ();
+ HWmem *weight = model->getWeightData ();
if (weight != nullptr) {
hwmem->setDmabuf (weight->getDmabuf ());
hwmem->setSize (weight->getSize ());
int
SegmentTable::createExternalInputSegments (const Model *model,
- const input_buffers *input)
-{
+ const input_buffers *input) {
const Metadata *meta = model->getMetadata ();
if (input == nullptr)
uint32_t offset = meta->getInputSegmentOffset (i);
uint32_t size = meta->getSegmentSize (idx);
- int status = createExternalSegment (idx, size, offset,
- input->bufs[i].dmabuf);
+ int status =
+ createExternalSegment (idx, size, offset, input->bufs[i].dmabuf);
if (status != 0)
return status;
}
int
SegmentTable::createExternalOutputSegments (const Model *model,
- const output_buffers *output)
-{
+ const output_buffers *output) {
const Metadata *meta = model->getMetadata ();
if (output == nullptr)
uint32_t offset = meta->getOutputSegmentOffset (i);
uint32_t size = meta->getSegmentSize (idx);
- int status = createExternalSegment (idx, size, offset,
- output->bufs[i].dmabuf);
+ int status =
+ createExternalSegment (idx, size, offset, output->bufs[i].dmabuf);
if (status != 0)
return status;
}
* @note we assume that # weight segments is always 1. (fix impl when it's changed)
*/
int
-SegmentTable::createSegments (const Model *model,
- const input_buffers *input, const output_buffers *output)
-{
+SegmentTable::createSegments (const Model *model, const input_buffers *input,
+ const output_buffers *output) {
if (model == nullptr) {
logerr (TAG, "No model provided\n");
return -EINVAL;
return -EINVAL;
}
- if (getData () == nullptr){
+ if (getData () == nullptr) {
logerr (TAG, "Not allocated segment table\n");
return -EINVAL;
}
/** check npubinfmt version */
if (meta->getVersion () != SUPPORTED_VERSION) {
- logerr (TAG, "Suppory only npubinfmt version %d\n",
- SUPPORTED_VERSION);
+ logerr (TAG, "Suppory only npubinfmt version %d\n", SUPPORTED_VERSION);
return -EINVAL;
}
goto err_clear;
/** Still exist remaining segements? */
- for (uint32_t idx = 0; idx < segments_.size(); idx++) {
+ for (uint32_t idx = 0; idx < segments_.size (); idx++) {
if (segments_[idx].get () == nullptr) {
uint32_t size = meta->getSegmentSize (idx);
status = createSegment (idx, size);
if (status != 0) {
- logerr (TAG, "Failed to allocate %uth segment with size %u: %d\n",
- idx, size, status);
+ logerr (TAG, "Failed to allocate %uth segment with size %u: %d\n", idx,
+ size, status);
goto err_clear;
}
}
/** set invalid dmabuf FDs for unused segt slots */
for (uint32_t i = meta->getSegmentsNum (); i < MAX_SEGMENTS; i++)
- reinterpret_cast<int32_t *>(getData())[i] = -1;
+ reinterpret_cast<int32_t *> (getData ())[i] = -1;
/** segment index validity is already checked in Metadata's checkSanity () */
num_total_segments_ = meta->getSegmentsNum ();
num_input_segments_ = meta->getInputNum ();
num_output_segments_ = meta->getOutputNum ();
- weight_seg_idx_ = new uint32_t [num_weight_segments_];
- input_seg_idx_ = new uint32_t [num_input_segments_];
- output_seg_idx_ = new uint32_t [num_output_segments_];
+ weight_seg_idx_ = new uint32_t[num_weight_segments_];
+ input_seg_idx_ = new uint32_t[num_input_segments_];
+ output_seg_idx_ = new uint32_t[num_output_segments_];
- input_seg_off_ = new uint32_t [num_input_segments_];
- output_seg_off_ = new uint32_t [num_output_segments_];
+ input_seg_off_ = new uint32_t[num_input_segments_];
+ output_seg_off_ = new uint32_t[num_output_segments_];
weight_seg_idx_[0] = meta->getWeightSegmentIndex ();
for (uint32_t i = 0; i < num_input_segments_; i++) {
* @return the HWmem instance for the segment
*/
HWmem *
-SegmentTable::getWeightSegment (uint32_t idx)
-{
+SegmentTable::getWeightSegment (uint32_t idx) {
if (weight_seg_idx_ == nullptr) {
logerr (TAG, "No valid segments in this table, maybe uninitialized?\n");
return nullptr;
if (idx >= num_weight_segments_) {
logerr (TAG, "Invalid weight segment index (%u). Should be less than %u\n",
- idx, num_weight_segments_);
+ idx, num_weight_segments_);
return nullptr;
}
uint32_t seg_idx = weight_seg_idx_[idx];
- assert (seg_idx < segments_.size ()); /** this is ensured in checkSanity() */
+ assert (seg_idx < segments_.size ()); /** this is ensured in checkSanity() */
- return segments_ [seg_idx].get();
+ return segments_[seg_idx].get ();
}
/**
* @return the HWmem instance for the segment
*/
HWmem *
-SegmentTable::getInputSegment (uint32_t idx)
-{
+SegmentTable::getInputSegment (uint32_t idx) {
if (input_seg_idx_ == nullptr) {
logerr (TAG, "No valid segments in this table, maybe uninitialized?\n");
return nullptr;
if (idx >= num_input_segments_) {
logerr (TAG, "Invalid input segment index (%u). Should be less than %u\n",
- idx, num_input_segments_);
+ idx, num_input_segments_);
return nullptr;
}
uint32_t seg_idx = input_seg_idx_[idx];
- assert (seg_idx < segments_.size ()); /** this is ensured in checkSanity() */
+ assert (seg_idx < segments_.size ()); /** this is ensured in checkSanity() */
- return segments_ [seg_idx].get();
+ return segments_[seg_idx].get ();
}
/**
* @return the HWmem instance for the segment
*/
HWmem *
-SegmentTable::getOutputSegment (uint32_t idx)
-{
+SegmentTable::getOutputSegment (uint32_t idx) {
if (output_seg_idx_ == nullptr) {
logerr (TAG, "No valid segments in this table, maybe uninitialized?\n");
return nullptr;
if (idx >= num_output_segments_) {
logerr (TAG, "Invalid output segment index (%u). Should be less than %u\n",
- idx, num_output_segments_);
+ idx, num_output_segments_);
return nullptr;
}
uint32_t seg_idx = output_seg_idx_[idx];
- assert (seg_idx < segments_.size ()); /** this is ensured in checkSanity() */
+ assert (seg_idx < segments_.size ()); /** this is ensured in checkSanity() */
- return segments_ [seg_idx].get();
+ return segments_[seg_idx].get ();
}
/**
* @return the offset of input segment
*/
uint32_t
-SegmentTable::getInputSegmentOffset (uint32_t idx)
-{
+SegmentTable::getInputSegmentOffset (uint32_t idx) {
if (input_seg_idx_ == nullptr) {
logerr (TAG, "No valid segments in this table, maybe uninitialized?\n");
return 0;
if (idx >= num_input_segments_) {
logerr (TAG, "Invalid input segment index (%u). Should be less than %u\n",
- idx, num_input_segments_);
+ idx, num_input_segments_);
return 0;
}
- return input_seg_off_ [idx];
+ return input_seg_off_[idx];
}
/**
* @return the offset of output segment
*/
uint32_t
-SegmentTable::getOutputSegmentOffset (uint32_t idx)
-{
+SegmentTable::getOutputSegmentOffset (uint32_t idx) {
if (output_seg_idx_ == nullptr) {
logerr (TAG, "No valid segments in this table, maybe uninitialized?\n");
return 0;
if (idx >= num_output_segments_) {
logerr (TAG, "Invalid output segment index (%u). Should be less than %u\n",
- idx, num_output_segments_);
+ idx, num_output_segments_);
return 0;
}
- return output_seg_off_ [idx];
+ return output_seg_off_[idx];
}
/** @brief segment table class derived from hwmem */
class SegmentTable : public HWmem {
- public:
- SegmentTable (const HWmemImpl* impl);
- ~SegmentTable ();
+ public:
+ SegmentTable (const HWmemImpl *impl);
+ ~SegmentTable ();
- /** Override alloc () and DO NOT support a variable-sized segment table */
- int alloc (size_t = 0) { return static_cast<HWmem *>(this)->alloc (PAGE_SIZE); }
- int createSegments (const Model *model, const input_buffers *input,
- const output_buffers *output = nullptr);
+ /** Override alloc () and DO NOT support a variable-sized segment table */
+ int alloc (size_t = 0) {
+ return static_cast<HWmem *> (this)->alloc (PAGE_SIZE);
+ }
+ int createSegments (const Model *model, const input_buffers *input,
+ const output_buffers *output = nullptr);
- HWmem *getWeightSegment (uint32_t idx = 0); /** maybe, # weight segments is 1 */
- HWmem *getInputSegment (uint32_t idx);
- HWmem *getOutputSegment (uint32_t idx);
+ HWmem *getWeightSegment (
+ uint32_t idx = 0); /** maybe, # weight segments is 1 */
+ HWmem *getInputSegment (uint32_t idx);
+ HWmem *getOutputSegment (uint32_t idx);
- uint32_t getInputSegmentOffset (uint32_t idx);
- uint32_t getOutputSegmentOffset (uint32_t idx);
+ uint32_t getInputSegmentOffset (uint32_t idx);
+ uint32_t getOutputSegmentOffset (uint32_t idx);
- uint32_t getNumTotalSegments () const { return num_total_segments_; }
- uint32_t getNumWeightSegments () const { return num_weight_segments_; }
- uint32_t getNumInputSegments () const { return num_input_segments_; }
- uint32_t getNumOutputSegments () const { return num_output_segments_; }
+ uint32_t getNumTotalSegments () const { return num_total_segments_; }
+ uint32_t getNumWeightSegments () const { return num_weight_segments_; }
+ uint32_t getNumInputSegments () const { return num_input_segments_; }
+ uint32_t getNumOutputSegments () const { return num_output_segments_; }
- std::string getHwDevice () { return hw_dev_; }
- void setHwDevice (std::string hw_dev) { hw_dev_ = hw_dev; }
+ std::string getHwDevice () { return hw_dev_; }
+ void setHwDevice (std::string hw_dev) { hw_dev_ = hw_dev; }
- private:
- bool setSegmentSlot (HWmem *hwmem, int slot);
+ private:
+ bool setSegmentSlot (HWmem *hwmem, int slot);
- int createSegment (uint32_t seg_idx, uint32_t seg_size);
- int createExternalSegment (uint32_t seg_idx, uint32_t seg_size,
- uint32_t seg_off, int dbuf_fd);
+ int createSegment (uint32_t seg_idx, uint32_t seg_size);
+ int createExternalSegment (uint32_t seg_idx, uint32_t seg_size,
+ uint32_t seg_off, int dbuf_fd);
- int createWeightSegment (const Model *model);
- int createExternalInputSegments (const Model *model, const input_buffers *input);
- int createExternalOutputSegments (const Model *model, const output_buffers *output);
+ int createWeightSegment (const Model *model);
+ int createExternalInputSegments (const Model *model,
+ const input_buffers *input);
+ int createExternalOutputSegments (const Model *model,
+ const output_buffers *output);
- std::vector <std::unique_ptr<HWmem>> segments_; /**< segments */
+ std::vector<std::unique_ptr<HWmem>> segments_; /**< segments */
- uint32_t * weight_seg_idx_; /**< segment index for weight */
- uint32_t * input_seg_idx_; /**< segment index for input tensors */
- uint32_t * output_seg_idx_; /**< segment index for output tensors */
+ uint32_t *weight_seg_idx_; /**< segment index for weight */
+ uint32_t *input_seg_idx_; /**< segment index for input tensors */
+ uint32_t *output_seg_idx_; /**< segment index for output tensors */
- uint32_t * input_seg_off_; /**< segment offset for input tensors */
- uint32_t * output_seg_off_; /**< segment offset for output tensors */
+ uint32_t *input_seg_off_; /**< segment offset for input tensors */
+ uint32_t *output_seg_off_; /**< segment offset for output tensors */
- uint32_t num_total_segments_; /**< number of total segments */
- uint32_t num_weight_segments_; /**< number of weight segments */
- uint32_t num_input_segments_; /**< number of input segments */
- uint32_t num_output_segments_; /**< number of output segments */
+ uint32_t num_total_segments_; /**< number of total segments */
+ uint32_t num_weight_segments_; /**< number of weight segments */
+ uint32_t num_input_segments_; /**< number of input segments */
+ uint32_t num_output_segments_; /**< number of output segments */
- std::string hw_dev_; /**< hw device for recurring task */
+ std::string hw_dev_; /**< hw device for recurring task */
};
#endif /* __NPU_ENGINE_SEGMENT_TABLE_H__ */
std::unique_ptr<ThreadPool> ThreadPool::instance_;
std::once_flag ThreadPool::once_flag_;
-ThreadPool &
-ThreadPool::getInstance ()
-{
- call_once (once_flag_, []() {
- instance_.reset (new ThreadPool);
- });
+ThreadPool&
+ThreadPool::getInstance () {
+ call_once (once_flag_, []() { instance_.reset (new ThreadPool); });
return *(instance_.get ());
}
/** @brief constructor of thread pool */
-ThreadPool::ThreadPool ()
- : stop_ (false)
-{
+ThreadPool::ThreadPool () : stop_ (false) {
/** create workers with the number of threads configured */
- num_threads_ = (Conf::getInstance().getNumThreads());
+ num_threads_ = (Conf::getInstance ().getNumThreads ());
for (uint32_t i = 0; i < num_threads_; i++)
- threads_.emplace_back ([this]() { this->worker(); });
+ threads_.emplace_back ([this]() { this->worker (); });
}
/** @brief destructor of thread pool */
-ThreadPool::~ThreadPool ()
-{
+ThreadPool::~ThreadPool () {
/** terminate all workers */
{
- std::unique_lock<std::mutex> lock(m_);
+ std::unique_lock<std::mutex> lock (m_);
stop_ = true;
}
- cv_.notify_all();
+ cv_.notify_all ();
- for (auto & t : threads_)
- t.join();
+ for (auto& t : threads_) t.join ();
}
/** @brief worker main function */
void
-ThreadPool::worker ()
-{
+ThreadPool::worker () {
while (1) {
std::unique_ptr<ThreadTask> task;
{
- std::unique_lock<std::mutex> lock(m_);
- cv_.wait (lock, [this]() { return should_stop() || !queue_.empty(); });
+ std::unique_lock<std::mutex> lock (m_);
+ cv_.wait (lock, [this]() { return should_stop () || !queue_.empty (); });
- if (should_stop())
+ if (should_stop ())
break;
task = std::move (queue_.front ());
* @return 0 if no error, otherwise a negative errno
*/
int
-ThreadPool::enqueueTask (ThreadTask *task)
-{
+ThreadPool::enqueueTask (ThreadTask* task) {
if (task == nullptr)
return -EINVAL;
{
- std::unique_lock<std::mutex> lock(m_);
+ std::unique_lock<std::mutex> lock (m_);
std::deque<std::unique_ptr<ThreadTask>>::iterator it;
- uint32_t task_id = task->getID();
+ uint32_t task_id = task->getID ();
- it = std::find_if (queue_.begin(), queue_.end(),
- [&task_id] (const std::unique_ptr<ThreadTask>& t) {
- return t->getID() == task_id;
- });
+ it = std::find_if (queue_.begin (), queue_.end (),
+ [&task_id](const std::unique_ptr<ThreadTask>& t) {
+ return t->getID () == task_id;
+ });
/** does not allow the same ID */
- if (it != queue_.end())
+ if (it != queue_.end ())
return -EBUSY;
queue_.push_back (std::unique_ptr<ThreadTask> (task));
}
- cv_.notify_one();
+ cv_.notify_one ();
return 0;
}
* @return 0 if no error. otherwise a negative errno
*/
int
-ThreadPool::removeTask (uint32_t task_id)
-{
- std::unique_lock<std::mutex> lock(m_);
+ThreadPool::removeTask (uint32_t task_id) {
+ std::unique_lock<std::mutex> lock (m_);
std::deque<std::unique_ptr<ThreadTask>>::iterator it;
- it = std::find_if (queue_.begin(), queue_.end(),
- [&task_id] (const std::unique_ptr<ThreadTask>& task) {
- return task->getID() == task_id;
- });
- if (it == queue_.end())
+ it = std::find_if (queue_.begin (), queue_.end (),
+ [&task_id](const std::unique_ptr<ThreadTask>& task) {
+ return task->getID () == task_id;
+ });
+ if (it == queue_.end ())
return -EBUSY;
queue_.erase (it);
#include <condition_variable>
/** @brief binded invoke function for task */
-typedef std::function<void ()> taskFunc;
+typedef std::function<void()> taskFunc;
/** @brief thread task class */
class ThreadTask {
- public:
- ThreadTask (uint32_t id, taskFunc func) : id_ (id), func_ (func) {}
- void invoke () { if (func_) func_ (); }
- uint32_t getID () const { return id_; }
-
- private:
- uint32_t id_; /**< task id (provided from users) */
- taskFunc func_; /**< task func */
+ public:
+ ThreadTask (uint32_t id, taskFunc func) : id_ (id), func_ (func) {}
+ void invoke () {
+ if (func_)
+ func_ ();
+ }
+ uint32_t getID () const { return id_; }
+
+ private:
+ uint32_t id_; /**< task id (provided from users) */
+ taskFunc func_; /**< task func */
};
/** @brief thread pool class */
class ThreadPool {
- public:
- static ThreadPool& getInstance ();
+ public:
+ static ThreadPool& getInstance ();
- ~ThreadPool ();
+ ~ThreadPool ();
- int enqueueTask (ThreadTask *task);
- int removeTask (uint32_t task_id);
+ int enqueueTask (ThreadTask* task);
+ int removeTask (uint32_t task_id);
- uint32_t getNumThreads () { return num_threads_; }
+ uint32_t getNumThreads () { return num_threads_; }
- private:
- ThreadPool ();
+ private:
+ ThreadPool ();
- void worker ();
+ void worker ();
- /** the below needs a pre-acquired lock */
- bool should_stop () { return stop_; }
+ /** the below needs a pre-acquired lock */
+ bool should_stop () { return stop_; }
- static std::unique_ptr<ThreadPool> instance_; /**< thread pool singleton instance */
- static std::once_flag once_flag_; /**< call once */
+ static std::unique_ptr<ThreadPool>
+ instance_; /**< thread pool singleton instance */
+ static std::once_flag once_flag_; /**< call once */
- std::mutex m_; /**< mutex for sync */
- std::condition_variable cv_; /**< cond for sync */
+ std::mutex m_; /**< mutex for sync */
+ std::condition_variable cv_; /**< cond for sync */
- uint32_t num_threads_; /**< number of threads */
- std::vector<std::thread> threads_; /**< worker threads */
- std::deque<std::unique_ptr<ThreadTask>> queue_;
- /**< task queue */
+ uint32_t num_threads_; /**< number of threads */
+ std::vector<std::thread> threads_; /**< worker threads */
+ std::deque<std::unique_ptr<ThreadTask>> queue_;
+ /**< task queue */
- bool stop_; /**< force to stop all workers */
+ bool stop_; /**< force to stop all workers */
};
#endif
/**
* @brief DriverAPI constructor
*/
-DriverAPI::DriverAPI (int dev_id) :
- dev_id_ (dev_id), dev_fd_ (-1) /* not opened yet */, initialized_ (false)
-{
-}
+DriverAPI::DriverAPI (int dev_id)
+ : dev_id_ (dev_id),
+ dev_fd_ (-1) /* not opened yet */,
+ initialized_ (false) {}
/**
* @brief get number of available devices with the given type
* @return a number of devices
*/
int
-DriverAPI::getNumDevices (dev_type type)
-{
+DriverAPI::getNumDevices (dev_type type) {
switch (type & DEVICETYPE_MASK) {
case DEVICETYPE_TRIV2:
#ifdef ENABLE_EMUL
* @return the instance of driver API
*/
std::unique_ptr<DriverAPI>
-DriverAPI::createDriverAPI (dev_type type, int id)
-{
+DriverAPI::createDriverAPI (dev_type type, int id) {
std::unique_ptr<DriverAPI> api_ptr (nullptr);
if (id < 0 || getNumDevices (type) <= id)
return api_ptr;
- DriverAPI * api = nullptr;
+ DriverAPI* api = nullptr;
switch (type & DEVICETYPE_MASK) {
case DEVICETYPE_TRIV2:
#ifdef ENABLE_EMUL
* @brief initialize device driver API
*/
int
-DriverAPI::init ()
-{
+DriverAPI::init () {
int status;
initialized_ = true;
status = open ();
- if ((status == 0) && !checkSanity())
+ if ((status == 0) && !checkSanity ())
return status;
initialized_ = false;
/** @brief class for Driver API. Each device should implement its APIs */
class DriverAPI {
- public:
- DriverAPI (int dev_id);
- virtual ~DriverAPI () {}
-
- static int getNumDevices (dev_type type);
- static std::unique_ptr<DriverAPI> createDriverAPI (dev_type type, int device_id);
- static uint32_t getCompiledAPILevel () { return api_level_; }
- int getDeviceID () const { return dev_id_; }
- int getDeviceFD () const { return dev_fd_; }
- bool initialized () const { return initialized_; }
-
- /** @brief open the device and set device fd */
- virtual int open () { return -ENODEV; }
- virtual int getAPILevel (uint32_t *level) const { return -EPERM; }
- virtual int getTops (uint32_t *tops) const { return -EPERM; }
- virtual int getDspmSize (uint32_t *dspm) const { return -EPERM; }
-
- virtual int checkSanity () { return 0; }
-
- /** The below requires initialized device */
- /** @brief check whether the device is ready (true) or busy (false) */
- virtual device_state_t isReady () const { return device_state_t::STATE_UNKNOWN; }
-
- /** @brief return a number of requests submitted */
- virtual uint32_t numRequests () const { return 0; }
-
- /** @brief allocate memory with the given size. return dmabuf fd */
- virtual int alloc (size_t size, bool contiguous = false) const { return -EPERM; }
- /** @brief deallocate memory with the corresponding dmabuf fd */
- virtual int dealloc (int dmabuf, bool contiguous = false) const { return -EPERM; }
- /** @brief get memory status */
- virtual int getMemoryStatus (size_t *alloc, size_t *free) const { return -EPERM; }
-
- /** @brief do mmap() for the dmabuf fd */
- virtual void *mmap (int dmabuf, size_t size) const { return nullptr; }
- /** @brief do munmap() for the mmaped pointer */
- virtual int munmap (void *addr, size_t size) const { return -EPERM; }
-
- /** @brief run inference with the input config */
- virtual int runInput (input_config_t *input) const { return -EPERM; }
- /** @brief stop all requests. The stopped requests should be notified */
- virtual int stop () const { return 0; }
- /** @brief stop the target request with the given id obtained by runInput() */
- virtual int stop_target (int id) const { return -EPERM; }
-
- /** @brief register model config to the driver */
- virtual int registerModel (model_config_t *model,
- uint64_t npu_version = 0) const { return -EPERM; }
- virtual int deregisterModel (unsigned long long id) const { return -EPERM; }
+ public:
+ DriverAPI (int dev_id);
+ virtual ~DriverAPI () {}
+
+ static int getNumDevices (dev_type type);
+ static std::unique_ptr<DriverAPI> createDriverAPI (dev_type type,
+ int device_id);
+ static uint32_t getCompiledAPILevel () { return api_level_; }
+ int getDeviceID () const { return dev_id_; }
+ int getDeviceFD () const { return dev_fd_; }
+ bool initialized () const { return initialized_; }
+
+ /** @brief open the device and set device fd */
+ virtual int open () { return -ENODEV; }
+ virtual int getAPILevel (uint32_t *level) const { return -EPERM; }
+ virtual int getTops (uint32_t *tops) const { return -EPERM; }
+ virtual int getDspmSize (uint32_t *dspm) const { return -EPERM; }
+
+ virtual int checkSanity () { return 0; }
+
+ /** The below requires initialized device */
+ /** @brief check whether the device is ready (true) or busy (false) */
+ virtual device_state_t isReady () const {
+ return device_state_t::STATE_UNKNOWN;
+ }
+
+ /** @brief return a number of requests submitted */
+ virtual uint32_t numRequests () const { return 0; }
+
+ /** @brief allocate memory with the given size. return dmabuf fd */
+ virtual int alloc (size_t size, bool contiguous = false) const {
+ return -EPERM;
+ }
+ /** @brief deallocate memory with the corresponding dmabuf fd */
+ virtual int dealloc (int dmabuf, bool contiguous = false) const {
+ return -EPERM;
+ }
+ /** @brief get memory status */
+ virtual int getMemoryStatus (size_t *alloc, size_t *free) const {
+ return -EPERM;
+ }
+
+ /** @brief do mmap() for the dmabuf fd */
+ virtual void *mmap (int dmabuf, size_t size) const { return nullptr; }
+ /** @brief do munmap() for the mmaped pointer */
+ virtual int munmap (void *addr, size_t size) const { return -EPERM; }
+
+ /** @brief run inference with the input config */
+ virtual int runInput (input_config_t *input) const { return -EPERM; }
+ /** @brief stop all requests. The stopped requests should be notified */
+ virtual int stop () const { return 0; }
+ /** @brief stop the target request with the given id obtained by runInput() */
+ virtual int stop_target (int id) const { return -EPERM; }
+
+ /** @brief register model config to the driver */
+ virtual int registerModel (model_config_t *model,
+ uint64_t npu_version = 0) const {
+ return -EPERM;
+ }
+ virtual int deregisterModel (unsigned long long id) const { return -EPERM; }
#if defined(ENABLE_FPGA_WORKAROUND)
- virtual int fpga_memcpy (int dmabuf, uint32_t offset,
- void *addr, size_t size) const { return -EPERM; }
+ virtual int fpga_memcpy (int dmabuf, uint32_t offset, void *addr,
+ size_t size) const {
+ return -EPERM;
+ }
#endif
- /** @brief get profile data for vISA instructions */
- virtual int getProfile (int req_id, npu_profile *profile) const { return -EPERM; }
- virtual int getStatApps (npu_stat_apps *stat) const { return -EPERM; }
- virtual int getStatReqs (int appid, npu_stat_reqs *stat) const { return -EPERM; }
-
- protected:
- int dev_id_; /**< device id. assume that 0 <= id < getNUmDevices() */
- int dev_fd_; /**< devide fd. opened in constructor and closed in destructor */
-
- private:
- /** @brief initialize device API. open the device here */
- int init ();
- bool initialized_;
-
- static uint32_t api_level_; /**< Trinity API level */
+ /** @brief get profile data for vISA instructions */
+ virtual int getProfile (int req_id, npu_profile *profile) const {
+ return -EPERM;
+ }
+ virtual int getStatApps (npu_stat_apps *stat) const { return -EPERM; }
+ virtual int getStatReqs (int appid, npu_stat_reqs *stat) const {
+ return -EPERM;
+ }
+
+ protected:
+ int dev_id_; /**< device id. assume that 0 <= id < getNUmDevices() */
+ int dev_fd_; /**< devide fd. opened in constructor and closed in destructor */
+
+ private:
+ /** @brief initialize device API. open the device here */
+ int init ();
+ bool initialized_;
+
+ static uint32_t api_level_; /**< Trinity API level */
};
class CuseElement;
/** @brief Driver APIs for TRIV2 */
class TrinityVision2API : public DriverAPI {
- public:
- static int getNumDevices ();
- TrinityVision2API (int dev_id);
- ~TrinityVision2API ();
+ public:
+ static int getNumDevices ();
+ TrinityVision2API (int dev_id);
+ ~TrinityVision2API ();
- int open ();
- int checkSanity ();
- int getAPILevel (uint32_t *level) const;
- int getTops (uint32_t *tops) const;
- int getDspmSize (uint32_t *dspm) const;
+ int open ();
+ int checkSanity ();
+ int getAPILevel (uint32_t *level) const;
+ int getTops (uint32_t *tops) const;
+ int getDspmSize (uint32_t *dspm) const;
- device_state_t isReady () const;
- uint32_t numRequests () const;
+ device_state_t isReady () const;
+ uint32_t numRequests () const;
- int alloc (size_t size, bool contiguous) const;
- int dealloc (int dmabuf, bool contiguous) const;
- int getMemoryStatus (size_t *alloc, size_t *free) const;
+ int alloc (size_t size, bool contiguous) const;
+ int dealloc (int dmabuf, bool contiguous) const;
+ int getMemoryStatus (size_t *alloc, size_t *free) const;
- void *mmap (int dmabuf, size_t size) const;
- int munmap (void *addr, size_t size) const;
+ void *mmap (int dmabuf, size_t size) const;
+ int munmap (void *addr, size_t size) const;
- int runInput (input_config_t *input) const;
- int stop () const;
- int stop_target (int id) const;
+ int runInput (input_config_t *input) const;
+ int stop () const;
+ int stop_target (int id) const;
- int registerModel (model_config_t *model, uint64_t npu_version) const;
- int deregisterModel (unsigned long long id) const;
+ int registerModel (model_config_t *model, uint64_t npu_version) const;
+ int deregisterModel (unsigned long long id) const;
#if defined(ENABLE_FPGA_WORKAROUND)
- int fpga_memcpy (int dmabuf, uint32_t offset,
- void *addr, size_t size) const;
+ int fpga_memcpy (int dmabuf, uint32_t offset, void *addr, size_t size) const;
#endif
- int getProfile (int req_id, npu_profile *profile) const;
+ int getProfile (int req_id, npu_profile *profile) const;
- int getStatApps (npu_stat_apps *stat) const;
- int getStatReqs (int appid, npu_stat_reqs *stat) const;
+ int getStatApps (npu_stat_apps *stat) const;
+ int getStatReqs (int appid, npu_stat_reqs *stat) const;
- private:
- int getDrvVersion () const;
- static const std::string dev_node_base;
- static std::bitset<CHAR_BIT> dev_bitset;
+ private:
+ int getDrvVersion () const;
+ static const std::string dev_node_base;
+ static std::bitset<CHAR_BIT> dev_bitset;
- static ThreadSafeMap<int, CuseElement> cuse_map;
+ static ThreadSafeMap<int, CuseElement> cuse_map;
- int sched_dev_fd_;
- bool is_cuse_;
+ int sched_dev_fd_;
+ bool is_cuse_;
- std::fstream dev_ios_;
+ std::fstream dev_ios_;
};
#ifdef ENABLE_EMUL
/** @brief Driver APIs for emulation */
class TrinityEmulAPI : public DriverAPI {
- public:
- static int getNumDevices ();
+ public:
+ static int getNumDevices ();
- TrinityEmulAPI (int dev_id, dev_type type);
- ~TrinityEmulAPI ();
+ TrinityEmulAPI (int dev_id, dev_type type);
+ ~TrinityEmulAPI ();
- int open ();
- device_state_t isReady () const;
+ int open ();
+ device_state_t isReady () const;
- int alloc (size_t size, bool contiguous) const;
- int dealloc (int dmabuf, bool contiguous) const;
- int getMemoryStatus (size_t *alloc_total, size_t *free_total) const;
+ int alloc (size_t size, bool contiguous) const;
+ int dealloc (int dmabuf, bool contiguous) const;
+ int getMemoryStatus (size_t *alloc_total, size_t *free_total) const;
- void *mmap (int dmabuf, size_t size) const;
- int munmap (void *addr, size_t size) const;
+ void *mmap (int dmabuf, size_t size) const;
+ int munmap (void *addr, size_t size) const;
- int runInput (input_config_t *input) const;
- int stop () const;
- int stop_target (int id) const;
+ int runInput (input_config_t *input) const;
+ int stop () const;
+ int stop_target (int id) const;
- int registerModel (model_config_t *model, uint64_t npu_version) const;
- int deregisterModel (unsigned long long id) const;
+ int registerModel (model_config_t *model, uint64_t npu_version) const;
+ int deregisterModel (unsigned long long id) const;
- int getProfile (int req_id, npu_profile *profile) const;
- void manipulateProfile (EmulReq *req, npu_profile *profile) const;
+ int getProfile (int req_id, npu_profile *profile) const;
+ void manipulateProfile (EmulReq *req, npu_profile *profile) const;
- private:
- static std::atomic<int> global_fd_;
- /**< global api fd */
- static ThreadSafeMap<int, EmulElement> elem_map_;
- /**< dmabuf-to-element map. to track memory allocation */
- static ThreadSafeMap<int, EmulStat> stat_map_;
- /**< devfd-to-stat map. to track memory statistics */
- static ThreadSafeMap<int, EmulReq> req_map_;
- /**< reqid-to-req map. to support async invoke/stop */
+ private:
+ static std::atomic<int> global_fd_;
+ /**< global api fd */
+ static ThreadSafeMap<int, EmulElement> elem_map_;
+ /**< dmabuf-to-element map. to track memory allocation */
+ static ThreadSafeMap<int, EmulStat> stat_map_;
+ /**< devfd-to-stat map. to track memory statistics */
+ static ThreadSafeMap<int, EmulReq> req_map_;
+ /**< reqid-to-req map. to support async invoke/stop */
- dev_type dev_type_; /**< emulated device type */
+ dev_type dev_type_; /**< emulated device type */
- char *prefix_share_; /**< prefix of share directory */
- char *prefix_profile_; /**< prefix of profile directory */
+ char *prefix_share_; /**< prefix of share directory */
+ char *prefix_profile_; /**< prefix of profile directory */
};
#endif
#define DEFAULT_PREFIX_PROFILE "/tmp"
class EmulReq {
- public:
- EmulReq (int req_id) :
- req_id_ (req_id), stop_ (false), first_run_ (false) {}
+ public:
+ EmulReq (int req_id) : req_id_ (req_id), stop_ (false), first_run_ (false) {}
- const char * get_profile_path () const { return prof_path_.c_str (); }
+ const char *get_profile_path () const { return prof_path_.c_str (); }
- void run_emul (char *prog, char **segt, char *metadata,
- std::string cmd_path, std::string prof_path) {
- first_run_ = true;
+ void run_emul (char *prog, char **segt, char *metadata, std::string cmd_path,
+ std::string prof_path) {
+ first_run_ = true;
- prof_path_ = prof_path + ".rec";
- while (!stop_ || first_run_) {
- run_triv2_emul (prog, segt, metadata, cmd_path.c_str (), prof_path.c_str ());
- first_run_ = false;
+ prof_path_ = prof_path + ".rec";
+ while (!stop_ || first_run_) {
+ run_triv2_emul (prog, segt, metadata, cmd_path.c_str (),
+ prof_path.c_str ());
+ first_run_ = false;
- if (!stop_)
- std::this_thread::sleep_for (std::chrono::seconds (1));
- };
+ if (!stop_)
+ std::this_thread::sleep_for (std::chrono::seconds (1));
+ };
- delete [] segt;
- }
+ delete[] segt;
+ }
- void run (std::function<void ()> func) {
- req_ = std::thread (func);
- }
+ void run (std::function<void()> func) { req_ = std::thread (func); }
- void stop () {
- stop_ = true;
- req_.join ();
- }
+ void stop () {
+ stop_ = true;
+ req_.join ();
+ }
- bool get_profile (npu_profile *profile) {
- std::ifstream ifs (prof_path_, std::ios::binary);
- if (!ifs.good ()) {
- std::cerr << "Failed to find the profile data " << prof_path_ << "\n";
- return false;
- }
+ bool get_profile (npu_profile *profile) {
+ std::ifstream ifs (prof_path_, std::ios::binary);
+ if (!ifs.good ()) {
+ std::cerr << "Failed to find the profile data " << prof_path_ << "\n";
+ return false;
+ }
- profile->prof_path = strdup (prof_path_.c_str ());
- if (!profile->prof_path) {
- std::cerr << "Unable to duplicate the profile path " << prof_path_ << "\n";
- return false;
- }
+ profile->prof_path = strdup (prof_path_.c_str ());
+ if (!profile->prof_path) {
+ std::cerr << "Unable to duplicate the profile path " << prof_path_
+ << "\n";
+ return false;
+ }
- T2PF_HEAD head;
- ifs.read ((char *) &head, sizeof (T2PF_HEAD));
+ T2PF_HEAD head;
+ ifs.read ((char *) &head, sizeof (T2PF_HEAD));
- if (head.fmt_vesion != TRIV2PROF_FMT_VER) {
- std::cerr << "Profile data format mismatch: "
- << "(" << head.fmt_vesion << " vs. " << TRIV2PROF_FMT_VER << ")\n";
- return false;
- }
+ if (head.fmt_vesion != TRIV2PROF_FMT_VER) {
+ std::cerr << "Profile data format mismatch: "
+ << "(" << head.fmt_vesion << " vs. " << TRIV2PROF_FMT_VER
+ << ")\n";
+ return false;
+ }
- uint32_t total_dump = 0;
-
- /* Neual Network Accelerator (NNA) */
- total_dump += head.nna.num_of_dump;
- total_dump += head.nna_dma_in.num_of_dump;
- total_dump += head.nna_dma_out.num_of_dump;
-
- /* Digital Signal Processor (DSP) */
- total_dump += head.dsp.num_of_dump;
- total_dump += head.dsp_dma_in.num_of_dump;
- total_dump += head.dsp_dma_out.num_of_dump;
-
- if (total_dump > 0) {
- profile->layers = new npu_profile_layer [total_dump];
- profile->num_layers = total_dump;
- profile->total_system_cycles = head.total_cycles;
- profile->dram_input_footprint = head.nna_dma_in.access_footprint_byte;
- profile->dram_output_footprint = head.nna_dma_out.access_footprint_byte;
- profile->dram_input_footprint += head.dsp_dma_in.access_footprint_byte;
- profile->dram_output_footprint += head.dsp_dma_out.access_footprint_byte;
-
- for (uint32_t i = 0; i < total_dump; i++) {
- npu_profile_layer *layer = &profile->layers[i];
-
- T2PF_DUMP common;
- T2PF_DUMP_NNA nna;
- T2PF_DUMP_DMA nna_dma;
- T2PF_DUMP_DSP dsp;
- T2PF_DUMP_DMA dsp_dma;
-
- std::streampos pos;
-
- pos = ifs.tellg ();
- ifs.read ((char *) &common, sizeof (T2PF_DUMP));
- ifs.seekg (pos);
-
- memset (layer, '\x00', sizeof (npu_profile_layer));
-
- layer->running_cycles = common.cycle_end - common.cycle_start;
- layer->start_cycles = common.cycle_start;
- layer->end_cycles = common.cycle_end;
-
- switch (common.block_id) {
- case TRIV2PROF_BLOCKID_NNA:
- ifs.read ((char *) &nna, sizeof (T2PF_DUMP_NNA));
-
- snprintf (layer->name, NPU_OPNAME_MAX, "%s", nna.op_name);
- break;
- case TRIV2PROF_BLOCKID_NNA_DMA_IN:
- ifs.read ((char *) &nna_dma, sizeof (T2PF_DUMP_DMA));
-
- snprintf (layer->name, NPU_OPNAME_MAX, "%s", "ADMA_IN");
- layer->dram_read_bytes = nna_dma.src_addr_end - nna_dma.src_addr_start;
- layer->sram_write_bytes = nna_dma.dest_addr_end - nna_dma.dest_addr_start;
- break;
- case TRIV2PROF_BLOCKID_NNA_DMA_OUT:
- ifs.read ((char *) &nna_dma, sizeof (T2PF_DUMP_DMA));
-
- snprintf (layer->name, NPU_OPNAME_MAX, "%s", "ADMA_OUT");
- layer->dram_write_bytes = nna_dma.dest_addr_end - nna_dma.dest_addr_start;
- layer->sram_read_bytes = nna_dma.src_addr_end - nna_dma.src_addr_start;
- break;
- case TRIV2PROF_BLOCKID_DSP:
- ifs.read ((char *) &dsp, sizeof (T2PF_DUMP_DSP));
-
- snprintf (layer->name, NPU_OPNAME_MAX, "%s", dsp.op_name);
- break;
- case TRIV2PROF_BLOCKID_DSP_DMA_IN:
- ifs.read ((char *) &dsp_dma, sizeof (T2PF_DUMP_DMA));
-
- snprintf (layer->name, NPU_OPNAME_MAX, "%s", "PDMA_IN");
- layer->dram_read_bytes = dsp_dma.src_addr_end - dsp_dma.src_addr_start;
- layer->sram_write_bytes = dsp_dma.dest_addr_end - dsp_dma.dest_addr_start;
- break;
- case TRIV2PROF_BLOCKID_DSP_DMA_OUT:
- ifs.read ((char *) &dsp_dma, sizeof (T2PF_DUMP_DMA));
-
- snprintf (layer->name, NPU_OPNAME_MAX, "%s", "PDMA_OUT");
- layer->dram_write_bytes = dsp_dma.dest_addr_end - dsp_dma.dest_addr_start;
- layer->sram_read_bytes = dsp_dma.src_addr_end - dsp_dma.src_addr_start;
- break;
- default:
- std::cerr << "Unknown block id detected: " << common.block_id << std::endl;
- delete [] profile->layers;
- profile->layers = nullptr;
- ifs.close();
- return false;
- }
+ uint32_t total_dump = 0;
+
+ /* Neual Network Accelerator (NNA) */
+ total_dump += head.nna.num_of_dump;
+ total_dump += head.nna_dma_in.num_of_dump;
+ total_dump += head.nna_dma_out.num_of_dump;
+
+ /* Digital Signal Processor (DSP) */
+ total_dump += head.dsp.num_of_dump;
+ total_dump += head.dsp_dma_in.num_of_dump;
+ total_dump += head.dsp_dma_out.num_of_dump;
+
+ if (total_dump > 0) {
+ profile->layers = new npu_profile_layer[total_dump];
+ profile->num_layers = total_dump;
+ profile->total_system_cycles = head.total_cycles;
+ profile->dram_input_footprint = head.nna_dma_in.access_footprint_byte;
+ profile->dram_output_footprint = head.nna_dma_out.access_footprint_byte;
+ profile->dram_input_footprint += head.dsp_dma_in.access_footprint_byte;
+ profile->dram_output_footprint += head.dsp_dma_out.access_footprint_byte;
+
+ for (uint32_t i = 0; i < total_dump; i++) {
+ npu_profile_layer *layer = &profile->layers[i];
+
+ T2PF_DUMP common;
+ T2PF_DUMP_NNA nna;
+ T2PF_DUMP_DMA nna_dma;
+ T2PF_DUMP_DSP dsp;
+ T2PF_DUMP_DMA dsp_dma;
+
+ std::streampos pos;
+
+ pos = ifs.tellg ();
+ ifs.read ((char *) &common, sizeof (T2PF_DUMP));
+ ifs.seekg (pos);
+
+ memset (layer, '\x00', sizeof (npu_profile_layer));
+
+ layer->running_cycles = common.cycle_end - common.cycle_start;
+ layer->start_cycles = common.cycle_start;
+ layer->end_cycles = common.cycle_end;
+
+ switch (common.block_id) {
+ case TRIV2PROF_BLOCKID_NNA:
+ ifs.read ((char *) &nna, sizeof (T2PF_DUMP_NNA));
+
+ snprintf (layer->name, NPU_OPNAME_MAX, "%s", nna.op_name);
+ break;
+ case TRIV2PROF_BLOCKID_NNA_DMA_IN:
+ ifs.read ((char *) &nna_dma, sizeof (T2PF_DUMP_DMA));
+
+ snprintf (layer->name, NPU_OPNAME_MAX, "%s", "ADMA_IN");
+ layer->dram_read_bytes =
+ nna_dma.src_addr_end - nna_dma.src_addr_start;
+ layer->sram_write_bytes =
+ nna_dma.dest_addr_end - nna_dma.dest_addr_start;
+ break;
+ case TRIV2PROF_BLOCKID_NNA_DMA_OUT:
+ ifs.read ((char *) &nna_dma, sizeof (T2PF_DUMP_DMA));
+
+ snprintf (layer->name, NPU_OPNAME_MAX, "%s", "ADMA_OUT");
+ layer->dram_write_bytes =
+ nna_dma.dest_addr_end - nna_dma.dest_addr_start;
+ layer->sram_read_bytes =
+ nna_dma.src_addr_end - nna_dma.src_addr_start;
+ break;
+ case TRIV2PROF_BLOCKID_DSP:
+ ifs.read ((char *) &dsp, sizeof (T2PF_DUMP_DSP));
+
+ snprintf (layer->name, NPU_OPNAME_MAX, "%s", dsp.op_name);
+ break;
+ case TRIV2PROF_BLOCKID_DSP_DMA_IN:
+ ifs.read ((char *) &dsp_dma, sizeof (T2PF_DUMP_DMA));
+
+ snprintf (layer->name, NPU_OPNAME_MAX, "%s", "PDMA_IN");
+ layer->dram_read_bytes =
+ dsp_dma.src_addr_end - dsp_dma.src_addr_start;
+ layer->sram_write_bytes =
+ dsp_dma.dest_addr_end - dsp_dma.dest_addr_start;
+ break;
+ case TRIV2PROF_BLOCKID_DSP_DMA_OUT:
+ ifs.read ((char *) &dsp_dma, sizeof (T2PF_DUMP_DMA));
+
+ snprintf (layer->name, NPU_OPNAME_MAX, "%s", "PDMA_OUT");
+ layer->dram_write_bytes =
+ dsp_dma.dest_addr_end - dsp_dma.dest_addr_start;
+ layer->sram_read_bytes =
+ dsp_dma.src_addr_end - dsp_dma.src_addr_start;
+ break;
+ default:
+ std::cerr << "Unknown block id detected: " << common.block_id
+ << std::endl;
+ delete[] profile->layers;
+ profile->layers = nullptr;
+ ifs.close ();
+ return false;
}
}
-
- ifs.close();
- return true;
}
- private:
- int req_id_;
- bool stop_;
- bool first_run_;
- std::thread req_;
- std::string prof_path_;
+ ifs.close ();
+ return true;
+ }
+
+ private:
+ int req_id_;
+ bool stop_;
+ bool first_run_;
+ std::thread req_;
+ std::string prof_path_;
};
/**
* we use a static variable for std::map to update statistics for each device emulation.
*/
class EmulStat {
- public:
- EmulStat () : alloc_total_ (0), free_total_ (0) {}
+ public:
+ EmulStat () : alloc_total_ (0), free_total_ (0) {}
- void addMemoryAlloc (size_t size) {
- std::unique_lock<std::mutex> lock(lock_);
- alloc_total_ += size;
- }
+ void addMemoryAlloc (size_t size) {
+ std::unique_lock<std::mutex> lock (lock_);
+ alloc_total_ += size;
+ }
- void addMemoryFree (size_t size) {
- std::unique_lock<std::mutex> lock(lock_);
- free_total_ += size;
- }
+ void addMemoryFree (size_t size) {
+ std::unique_lock<std::mutex> lock (lock_);
+ free_total_ += size;
+ }
- void getMemoryStatus (size_t * alloc_total, size_t * free_total) {
- std::unique_lock<std::mutex> lock(lock_);
- *alloc_total = alloc_total_;
- *free_total = free_total_;
- }
+ void getMemoryStatus (size_t *alloc_total, size_t *free_total) {
+ std::unique_lock<std::mutex> lock (lock_);
+ *alloc_total = alloc_total_;
+ *free_total = free_total_;
+ }
- private:
- std::mutex lock_;
- size_t alloc_total_;
- size_t free_total_;
- /** @todo add more */
+ private:
+ std::mutex lock_;
+ size_t alloc_total_;
+ size_t free_total_;
+ /** @todo add more */
};
/** @brief emulation element */
class EmulElement {
- public:
- EmulElement (size_t size);
- ~EmulElement () {
- free (addr_);
- model_config_map_.clear ();
- }
+ public:
+ EmulElement (size_t size);
+ ~EmulElement () {
+ free (addr_);
+ model_config_map_.clear ();
+ }
- int getDmabuf () const { return dmabuf_; }
- void *getAddr () const { return addr_; }
- size_t getSize () const { return size_; }
+ int getDmabuf () const { return dmabuf_; }
+ void *getAddr () const { return addr_; }
+ size_t getSize () const { return size_; }
- void setModelConfig (model_config_t *model) {
- model_config_map_.insert (model->id, model);
- }
+ void setModelConfig (model_config_t *model) {
+ model_config_map_.insert (model->id, model);
+ }
- model_config_t * getModelConfig (uint64_t id) {
- return model_config_map_.find (id);
- }
+ model_config_t *getModelConfig (uint64_t id) {
+ return model_config_map_.find (id);
+ }
- void unsetModelConfig (uint64_t id) {
- model_config_map_.remove (id);
- }
+ void unsetModelConfig (uint64_t id) { model_config_map_.remove (id); }
- void setNpuTops (uint64_t tops) {
- /** for backward-compatibility, 0-tops is regarded as 8-tops */
- if (tops == 0)
- tops_ = 8;
- else
- tops_ = tops;
- }
+ void setNpuTops (uint64_t tops) {
+ /** for backward-compatibility, 0-tops is regarded as 8-tops */
+ if (tops == 0)
+ tops_ = 8;
+ else
+ tops_ = tops;
+ }
- uint64_t getNpuTops () const { return tops_; }
+ uint64_t getNpuTops () const { return tops_; }
- private:
- static std::atomic<int> global_id_;
- /**< element global id (== dmabuf fd) */
- int dmabuf_; /**< dmabuf fd */
- void *addr_; /**< mapped address */
- size_t size_; /**< the allocated size */
+ private:
+ static std::atomic<int> global_id_;
+ /**< element global id (== dmabuf fd) */
+ int dmabuf_; /**< dmabuf fd */
+ void *addr_; /**< mapped address */
+ size_t size_; /**< the allocated size */
- ThreadSafeMap<uint64_t, model_config_t> model_config_map_;
- uint64_t tops_; /**< npu tops */
+ ThreadSafeMap<uint64_t, model_config_t> model_config_map_;
+ uint64_t tops_; /**< npu tops */
};
/**
* @brief constructor of emulation element
* @param[in] size the size of element
*/
-EmulElement::EmulElement (size_t size)
-{
+EmulElement::EmulElement (size_t size) {
/** just use malloc() for void type */
addr_ = malloc (size);
assert (addr_);
* @param[in] dev_id device id. device id is used to emulate device fd
*/
TrinityEmulAPI::TrinityEmulAPI (int dev_id, dev_type type)
- : DriverAPI (dev_id), dev_type_ (type)
-{
+ : DriverAPI (dev_id), dev_type_ (type) {
static char default_prefix_share[] = DEFAULT_PREFIX_SHARE;
static char default_prefix_profile[] = DEFAULT_PREFIX_PROFILE;
- prefix_share_ = getenv(ENV_PREFIX_SHARE);
+ prefix_share_ = getenv (ENV_PREFIX_SHARE);
if (prefix_share_ == nullptr) {
prefix_share_ = default_prefix_share;
- setenv(ENV_PREFIX_SHARE, default_prefix_share, 1);
+ setenv (ENV_PREFIX_SHARE, default_prefix_share, 1);
}
- prefix_profile_ = getenv(ENV_PREFIX_PROFILE);
+ prefix_profile_ = getenv (ENV_PREFIX_PROFILE);
if (prefix_profile_ == nullptr) {
prefix_profile_ = default_prefix_profile;
- setenv(ENV_PREFIX_PROFILE, default_prefix_profile, 1);
+ setenv (ENV_PREFIX_PROFILE, default_prefix_profile, 1);
}
}
/** @brief destructor of emulation API driver */
-TrinityEmulAPI::~TrinityEmulAPI ()
-{
- elem_map_.clear();
- stat_map_.clear();
+TrinityEmulAPI::~TrinityEmulAPI () {
+ elem_map_.clear ();
+ stat_map_.clear ();
}
/**
* @return 0 if no error, otherwise a negative errno
*/
int
-TrinityEmulAPI::open ()
-{
+TrinityEmulAPI::open () {
if (dev_fd_ >= 0)
return -EBUSY;
* @return number of available devices.
*/
int
-TrinityEmulAPI::getNumDevices ()
-{
+TrinityEmulAPI::getNumDevices () {
return MAX_EMUL_DEVICES;
}
* @return STATE_READY if no error. otherwise STATE_END.
*/
device_state_t
-TrinityEmulAPI::isReady () const
-{
- if (!initialized())
+TrinityEmulAPI::isReady () const {
+ if (!initialized ())
return device_state_t::STATE_UNKNOWN;
return STATE_READY;
* @return dmabuf fd (>= 0) if no error. otherwise a negative errno.
*/
int
-TrinityEmulAPI::alloc (size_t size, bool contiguous) const
-{
- if (!initialized())
+TrinityEmulAPI::alloc (size_t size, bool contiguous) const {
+ if (!initialized ())
return -EPERM;
if (size == 0 || size > UINT32_MAX)
return -EINVAL;
EmulElement *elem = new EmulElement (size);
- int status = elem_map_.insert (elem->getDmabuf(), elem);
+ int status = elem_map_.insert (elem->getDmabuf (), elem);
if (status != 0)
return status;
assert (stat != nullptr); /* always exist */
stat->addMemoryAlloc (size);
- return elem->getDmabuf();
+ return elem->getDmabuf ();
}
/**
* @return 0 if no error. otherwise a negative errno.
*/
int
-TrinityEmulAPI::dealloc (int dmabuf, bool contiguous) const
-{
- if (!initialized())
+TrinityEmulAPI::dealloc (int dmabuf, bool contiguous) const {
+ if (!initialized ())
return -EPERM;
EmulElement *elem = elem_map_.find (dmabuf);
EmulStat *stat = stat_map_.find (dev_fd_);
assert (stat != nullptr); /* always exist */
- stat->addMemoryFree (elem->getSize());
+ stat->addMemoryFree (elem->getSize ());
- elem_map_.remove (elem->getDmabuf());
+ elem_map_.remove (elem->getDmabuf ());
return 0;
}
* @return 0 if no error. otherwise a negatice error value
*/
int
-TrinityEmulAPI::getMemoryStatus (size_t *alloc_total, size_t *free_total) const
-{
- if (!initialized())
+TrinityEmulAPI::getMemoryStatus (size_t *alloc_total,
+ size_t *free_total) const {
+ if (!initialized ())
return -EPERM;
if (alloc_total == nullptr || free_total == nullptr)
* @note it's already memory mapped.
*/
void *
-TrinityEmulAPI::mmap (int dmabuf, size_t size) const
-{
- if (!initialized())
+TrinityEmulAPI::mmap (int dmabuf, size_t size) const {
+ if (!initialized ())
return nullptr;
EmulElement *elem = elem_map_.find (dmabuf);
* @note nothing to do
*/
int
-TrinityEmulAPI::munmap (void *addr, size_t size) const
-{
- if (!initialized())
+TrinityEmulAPI::munmap (void *addr, size_t size) const {
+ if (!initialized ())
return -EPERM;
if (addr == nullptr || size == 0)
*/
int
TrinityEmulAPI::registerModel (model_config_t *model_config,
- uint64_t npu_version) const
-{
+ uint64_t npu_version) const {
static std::atomic<uint64_t> global_internal_id (0);
- if (!initialized())
+ if (!initialized ())
return -EPERM;
if (model_config == nullptr || model_config->dbuf_fd < 0)
if (elem == nullptr)
return -ENOENT;
- model_config_t * config = new model_config_t;
+ model_config_t *config = new model_config_t;
uint32_t dbuf_fd_unsigned = model_config->dbuf_fd;
uint64_t internal_id = global_internal_id.fetch_add (1);
* @return 0 if no error. otherwise a negative errno
*/
int
-TrinityEmulAPI::deregisterModel (unsigned long long id) const
-{
- if (!initialized())
+TrinityEmulAPI::deregisterModel (unsigned long long id) const {
+ if (!initialized ())
return -EPERM;
int dbuf_fd = id >> TRINITY_SHIFT_MODEL_ID;
* @return 0 or positive id if no error. otherwise a negative errno
*/
int
-TrinityEmulAPI::runInput (input_config_t *input_config) const
-{
- if (!initialized())
+TrinityEmulAPI::runInput (input_config_t *input_config) const {
+ if (!initialized ())
return -EPERM;
if (input_config == nullptr)
if (elem_model == nullptr || elem_model->getAddr () == nullptr)
return -EINVAL;
- char * addr_input = static_cast<char *>(elem_input->getAddr ());
- char * addr_model = static_cast<char *>(elem_model->getAddr ());
+ char *addr_input = static_cast<char *> (elem_input->getAddr ());
+ char *addr_model = static_cast<char *> (elem_model->getAddr ());
- model_config_t * model = elem_model->getModelConfig (input_config->model_id);
+ model_config_t *model = elem_model->getModelConfig (input_config->model_id);
EmulElement *elem_metadata = elem_map_.find (model->metadata_dbuf_fd);
if (elem_metadata == nullptr || elem_metadata->getAddr () == nullptr)
return -EINVAL;
return 0;
uint32_t num_segs = input_config->num_segments;
- char ** segment_table = new char* [num_segs];
+ char **segment_table = new char *[num_segs];
for (uint32_t i = 0; i < num_segs; i++) {
- int32_t dmabuf = reinterpret_cast<int32_t *>(addr_input)[i];
- uint32_t offset = reinterpret_cast<uint32_t *>(addr_input + elem_input->getSize ()/2)[i];
+ int32_t dmabuf = reinterpret_cast<int32_t *> (addr_input)[i];
+ uint32_t offset = reinterpret_cast<uint32_t *> (
+ addr_input + elem_input->getSize () / 2)[i];
EmulElement *elem = elem_map_.find (dmabuf);
if (elem == nullptr) {
- delete [] segment_table;
+ delete[] segment_table;
return -EINVAL;
}
/** Here, set virtual address for emulations */
- segment_table[i] = static_cast<char *>(elem->getAddr ()) + offset;
+ segment_table[i] = static_cast<char *> (elem->getAddr ()) + offset;
}
std::string cmd_path (prefix_share_);
status = req_map_.insert (req_id, req);
if (status != 0) {
- delete [] segment_table;
+ delete[] segment_table;
return status;
}
prof_path += "/ne_profile." + std::to_string (req_id);
- auto func = std::bind (&EmulReq::run_emul, req,
- prog, segment_table, static_cast <char*> (elem_metadata->getAddr ()),
- cmd_path, prof_path);
+ auto func = std::bind (&EmulReq::run_emul, req, prog, segment_table,
+ static_cast<char *> (elem_metadata->getAddr ()),
+ cmd_path, prof_path);
req->run (func);
status = req_id;
* @return 0 if no error. otherwise a negative errno
*/
int
-TrinityEmulAPI::stop () const
-{
- if (!initialized())
+TrinityEmulAPI::stop () const {
+ if (!initialized ())
return -EPERM;
#if 0
}
int
-TrinityEmulAPI::stop_target (int req_id) const
-{
- if (!initialized())
+TrinityEmulAPI::stop_target (int req_id) const {
+ if (!initialized ())
return -EPERM;
- EmulReq * req = req_map_.find (req_id);
+ EmulReq *req = req_map_.find (req_id);
if (req == nullptr)
return -ENOENT;
}
int
-TrinityEmulAPI::getProfile (int req_id, npu_profile *profile) const
-{
- if (!initialized())
+TrinityEmulAPI::getProfile (int req_id, npu_profile *profile) const {
+ if (!initialized ())
return -EPERM;
- EmulReq * req = req_map_.find (req_id);
+ EmulReq *req = req_map_.find (req_id);
if (req == nullptr)
return -ENOENT;
+/**
+ * Proprietary
+ * Copyright (C) 2020 Samsung Electronics
+ * Copyright (C) 2020 Dongju Chae <dongju.chae@samsung.com>
+ */
+/**
+ * @file NPUdrvAPI_triv2.cc
+ * @date 13 Feb 2020
+ * @brief Implementation of APIs to handle TRIV2 device
+ * @author Dongju Chae <dongju.chae@samsung.com>
+ * @bug No known bugs except for NYI items
+ */
+
#include <misc/trinity.h>
#include <misc/npu_sched.h>
/** @brief Cuse-hwmem element */
class CuseElement {
- public:
- CuseElement (int dbuf_fd, int shm_fd, const std::string & shm_name) :
- dbuf_fd_ (dbuf_fd), shm_fd_ (shm_fd), shm_name_ (shm_name) {}
+ public:
+ CuseElement (int dbuf_fd, int shm_fd, const std::string &shm_name)
+ : dbuf_fd_ (dbuf_fd), shm_fd_ (shm_fd), shm_name_ (shm_name) {}
- ~CuseElement () {
- close (shm_fd_);
- shm_unlink (shm_name_.c_str ());
- }
+ ~CuseElement () {
+ close (shm_fd_);
+ shm_unlink (shm_name_.c_str ());
+ }
- int getDbufFD () { return dbuf_fd_; }
- int getShmFD () { return shm_fd_; }
- const std::string & getShmName () { return shm_name_; }
+ int getDbufFD () { return dbuf_fd_; }
+ int getShmFD () { return shm_fd_; }
+ const std::string &getShmName () { return shm_name_; }
- private:
- int dbuf_fd_;
- int shm_fd_;
- const std::string shm_name_;
+ private:
+ int dbuf_fd_;
+ int shm_fd_;
+ const std::string shm_name_;
};
ThreadSafeMap<int, CuseElement> TrinityVision2API::cuse_map;
* @brief constructor of the API instance for Trinity Vision driver
* @param[in] dev_id device id
*/
-TrinityVision2API::TrinityVision2API (int dev_id) :
- DriverAPI (dev_id), sched_dev_fd_ (-1), is_cuse_ (false)
-{
+TrinityVision2API::TrinityVision2API (int dev_id)
+ : DriverAPI (dev_id), sched_dev_fd_ (-1), is_cuse_ (false) {
int num_devs;
if (dev_id > max_num_devs) {
}
/** @brief destructor of emulation API driver */
-TrinityVision2API::~TrinityVision2API ()
-{
+TrinityVision2API::~TrinityVision2API () {
if (dev_fd_ >= 0)
::close (dev_fd_);
if (sched_dev_fd_ >= 0)
}
int
-TrinityVision2API::open ()
-{
+TrinityVision2API::open () {
std::string path;
int fd;
- if (!this->initialized())
+ if (!this->initialized ())
return -EPERM;
if (dev_id_ == -1)
return -ENODEV;
path = "/dev/" + TrinityVision2API::dev_node_base + "-" +
- std::to_string(dev_id_);
- fd = ::open (path.c_str(), O_RDWR);
+ std::to_string (dev_id_);
+ fd = ::open (path.c_str (), O_RDWR);
if (fd < 0)
return -ENODEV;
dev_fd_ = fd;
}
int
-TrinityVision2API::checkSanity ()
-{
+TrinityVision2API::checkSanity () {
int lib_ver = trinity_gen_ver (0, VER_NE_MAJOR, VER_NE_MINOR, VER_NE_EXTRA);
- int min_ver = this->getDrvVersion();
- enum trinity_dev_type type =
- static_cast<enum trinity_dev_type> ((min_ver & TRINITY_MASK_DEV) >> TRINITY_SHIFT_DEV);
+ int min_ver = this->getDrvVersion ();
+ enum trinity_dev_type type = static_cast<enum trinity_dev_type> (
+ (min_ver & TRINITY_MASK_DEV) >> TRINITY_SHIFT_DEV);
/* Check if this device is a TRIV2-compatible one */
if (type != TRINITY_DEV_VISION2 && type != TRINITY_DEV_VISION2_CUSE)
* @return number of available devices.
*/
int
-TrinityVision2API::getNumDevices ()
-{
+TrinityVision2API::getNumDevices () {
static std::string path;
struct stat sb;
int cnt = 0;
int ret;
- path.clear();
+ path.clear ();
for (int i = 0; i < max_num_devs; ++i) {
- path = "/dev/" + TrinityVision2API::dev_node_base + "-" + std::to_string(i);
- ret = stat (path.c_str(), &sb);
+ path =
+ "/dev/" + TrinityVision2API::dev_node_base + "-" + std::to_string (i);
+ ret = stat (path.c_str (), &sb);
if (ret == 0 && S_ISCHR (sb.st_mode)) {
cnt++;
- TrinityVision2API::dev_bitset.set(i);
+ TrinityVision2API::dev_bitset.set (i);
}
}
* @return true if the device is ready. otherwise false.
*/
device_state_t
-TrinityVision2API::isReady () const
-{
+TrinityVision2API::isReady () const {
device_state_t state;
int ret;
- if (!this->initialized())
+ if (!this->initialized ())
return device_state_t::STATE_UNKNOWN;
ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_STATE, &state);
* @return the number of requests if no error, otherwise 0.
*/
uint32_t
-TrinityVision2API::numRequests () const
-{
- if (this->initialized()) {
+TrinityVision2API::numRequests () const {
+ if (this->initialized ()) {
struct trinity_ioctl_stat_apps stat;
int ret;
* @return dmabuf fd (>= 0) if no error. otherwise a negative errno.
*/
int
-TrinityVision2API::alloc (size_t size, bool contiguous) const
-{
+TrinityVision2API::alloc (size_t size, bool contiguous) const {
struct trinity_ioctl_hwmem hwmem;
int ret;
- if (!this->initialized())
+ if (!this->initialized ())
return -EPERM;
if (size == 0 || size > UINT32_MAX)
struct stat statbuf;
/* Let's use shared memory for emulated dmabuf */
- shm_name = "/triv2_" + std::to_string (getpid ()) + "_" + std::to_string (dbuf_fd);
+ shm_name =
+ "/triv2_" + std::to_string (getpid ()) + "_" + std::to_string (dbuf_fd);
ret = shm_open (shm_name.c_str (), O_RDWR, 0666);
if (ret < 0)
return ret;
* @return 0 if no error. otherwise a negative errno.
*/
int
-TrinityVision2API::dealloc (int dmabuf, bool contiguous) const
-{
+TrinityVision2API::dealloc (int dmabuf, bool contiguous) const {
struct trinity_ioctl_hwmem hwmem;
int ret;
- if (!this->initialized())
+ if (!this->initialized ())
return -EPERM;
hwmem.type = contiguous ? HWMEM_DMA_CONT : HWMEM_DMA_IOMMU;
* @return 0 if no error. otherwise a negatice error value
*/
int
-TrinityVision2API::getMemoryStatus (size_t *alloc_total, size_t *free_total) const
-{
+TrinityVision2API::getMemoryStatus (size_t *alloc_total,
+ size_t *free_total) const {
struct trinity_ioctl_stat_app stat;
int ret;
- if (!initialized())
+ if (!initialized ())
return -EPERM;
if (alloc_total == nullptr || free_total == nullptr)
* @note it's already memory mapped.
*/
void *
-TrinityVision2API::mmap (int dmabuf, size_t size) const
-{
+TrinityVision2API::mmap (int dmabuf, size_t size) const {
void *ret;
if (!this->initialized ())
* @note nothing to do
*/
int
-TrinityVision2API::munmap (void *addr, size_t size) const
-{
+TrinityVision2API::munmap (void *addr, size_t size) const {
int ret;
- if (!this->initialized())
+ if (!this->initialized ())
return -EPERM;
if (addr == nullptr || size == 0)
return -EINVAL;
- ret = ::munmap(addr, size);
+ ret = ::munmap (addr, size);
return ret;
}
*/
int
TrinityVision2API::registerModel (model_config_t *model_config,
- uint64_t npu_version) const
-{
+ uint64_t npu_version) const {
uint32_t model_tops;
uint32_t device_tops;
int ret;
- if (!this->initialized())
+ if (!this->initialized ())
return -EPERM;
- if (NPU_VERSION_MAJOR(npu_version) != 2)
+ if (NPU_VERSION_MAJOR (npu_version) != 2)
return -EINVAL;
- model_tops = NPU_VERSION_TOPS(npu_version);
+ model_tops = NPU_VERSION_TOPS (npu_version);
if (model_tops != 0) {
ret = getTops (&device_tops);
if (ret != 0)
return -EINVAL;
} /** else: don't care */
- ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_REGISTER_MODEL,
- model_config);
+ ret =
+ ioctl (this->getDeviceFD (), TRINITY_IOCTL_REGISTER_MODEL, model_config);
if (ret != 0)
return -errno;
}
int
-TrinityVision2API::deregisterModel (unsigned long long id) const
-{
+TrinityVision2API::deregisterModel (unsigned long long id) const {
int ret;
- if (!this->initialized())
+ if (!this->initialized ())
return -EPERM;
ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_DEREGISTER_MODEL, &id);
* @return 0 or positive id if no error. otherwise a negative errno
*/
int
-TrinityVision2API::runInput (input_config_t *input_config) const
-{
+TrinityVision2API::runInput (input_config_t *input_config) const {
int ret;
- if (!this->initialized())
+ if (!this->initialized ())
return -EPERM;
if (input_config == nullptr)
input_config->task_handle = val.task_handle;
}
- ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_RUN_INPUT,
- input_config);
+ ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_RUN_INPUT, input_config);
if (ret != 0)
return -errno;
* Also, the stopped requests should be notified to users.
*/
int
-TrinityVision2API::stop () const
-{
+TrinityVision2API::stop () const {
int ret;
- if (!this->initialized())
+ if (!this->initialized ())
return -EPERM;
ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_STOP_REQUESTS);
return 0;
}
-int TrinityVision2API::stop_target (int id) const
-{
+int
+TrinityVision2API::stop_target (int id) const {
int ret;
- if (!this->initialized())
+ if (!this->initialized ())
return -EPERM;
ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_STOP_REQUEST, &id);
}
int
-TrinityVision2API::getDrvVersion () const
-{
+TrinityVision2API::getDrvVersion () const {
int ver;
int err;
- if (!this->initialized())
+ if (!this->initialized ())
return -EPERM;
err = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_VERSION, &ver);
#if defined(ENABLE_FPGA_WORKAROUND)
int
-TrinityVision2API::fpga_memcpy (int dmabuf, uint32_t offset, void *addr, size_t size) const
-{
+TrinityVision2API::fpga_memcpy (int dmabuf, uint32_t offset, void *addr,
+ size_t size) const {
struct trinity_fpga_memcpy fpga;
int ret;
- if (!this->initialized())
+ if (!this->initialized ())
return -EPERM;
fpga.dbuf_fd = dmabuf;
#endif
int
-TrinityVision2API::getProfile (int req_id, npu_profile *profile) const
-{
+TrinityVision2API::getProfile (int req_id, npu_profile *profile) const {
struct trinity_ioctl_profile t_profile;
int ret = 0;
t_profile.req_id = req_id;
- ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_PROFILE_META, &t_profile);
+ ret =
+ ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_PROFILE_META, &t_profile);
if (ret != 0)
return -errno;
profile->layers = new npu_profile_layer[profile->num_layers];
t_profile.profile_buf = profile->layers;
- ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_PROFILE_BUFF, &t_profile);
+ ret =
+ ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_PROFILE_BUFF, &t_profile);
if (ret != 0) {
- delete [] (profile->layers);
+ delete[](profile->layers);
profile->layers = nullptr;
profile->num_layers = 0;
}
int
-TrinityVision2API::getAPILevel (uint32_t *level) const
-{
- int err;
+TrinityVision2API::getAPILevel (uint32_t *level) const {
+ int err;
- if (!this->initialized())
+ if (!this->initialized ())
return -EPERM;
err = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_API_LEVEL, level);
* @return 0 if no error. otherwise a negative errno
*/
int
-TrinityVision2API::getTops (uint32_t *tops) const
-{
+TrinityVision2API::getTops (uint32_t *tops) const {
int ret;
- if (!this->initialized())
+ if (!this->initialized ())
return -EPERM;
ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_TOPS, tops);
}
int
-TrinityVision2API::getDspmSize (uint32_t *dspm) const
-{
+TrinityVision2API::getDspmSize (uint32_t *dspm) const {
int ret;
- if (!this->initialized())
+ if (!this->initialized ())
return -EPERM;
ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_DSPM, dspm);
}
int
-TrinityVision2API::getStatApps (npu_stat_apps *stat) const
-{
+TrinityVision2API::getStatApps (npu_stat_apps *stat) const {
struct trinity_ioctl_stat_apps stat_apps;
int ret;
- if (!this->initialized())
+ if (!this->initialized ())
return -EPERM;
if (stat == nullptr)
strncpy (stat->stat[i].name, stat_app->name, TRINITY_APP_NAME_MAX - 1);
stat->stat[i].app_id = stat_app->app_id;
- stat->stat[i].status = static_cast<npu_app_status>(stat_app->status);
+ stat->stat[i].status = static_cast<npu_app_status> (stat_app->status);
stat->stat[i].num_total_reqs = stat_app->num_total_reqs;
stat->stat[i].num_active_reqs = stat_app->num_active_reqs;
}
int
-TrinityVision2API::getStatReqs (int app_id, npu_stat_reqs *stat) const
-{
+TrinityVision2API::getStatReqs (int app_id, npu_stat_reqs *stat) const {
struct trinity_ioctl_stat_reqs stat_reqs;
int ret;
- if (!this->initialized())
+ if (!this->initialized ())
return -EPERM;
if (stat == nullptr)
stat->stat[i].req_id = stat_req->req_id;
stat->stat[i].model_id = stat_req->model_id;
- stat->stat[i].priority = static_cast<npu_priority>(stat_req->priority);
- stat->stat[i].status = static_cast<npu_req_status>(stat_req->status);
+ stat->stat[i].priority = static_cast<npu_priority> (stat_req->priority);
+ stat->stat[i].status = static_cast<npu_req_status> (stat_req->status);
stat->stat[i].sched_time = stat_req->sched_time;
stat->stat[i].infer_time = stat_req->infer_time;
* @return the conf instance
*/
Conf &
-Conf::getInstance (const char *inipath)
-{
- call_once (once_flag_, []() {
- instance_.reset (new Conf);
- });
+Conf::getInstance (const char *inipath) {
+ call_once (once_flag_, []() { instance_.reset (new Conf); });
instance_->loadConf (inipath);
return *(instance_.get ());
/**
* @breif error callback of iniparser, used to ignore its error message
*/
-static int error_callback (const char*, ...)
-{
+static int
+error_callback (const char *, ...) {
return 0;
}
* @brief Constructor of Conf class, loading default configurations.
* @note this cannot be called explicitly.
*/
-Conf::Conf ()
-{
+Conf::Conf () {
reset ();
}
* @detail Refer to the header file for more detail.
*/
void
-Conf::loadConf (const char *inipath)
-{
+Conf::loadConf (const char *inipath) {
static const char INI_FILE_NAME[] = "npu-engine.ini";
char default_ini_path[PATH_MAX];
- snprintf(default_ini_path, PATH_MAX, "%s/%s", NE_INIDIR, INI_FILE_NAME);
+ snprintf (default_ini_path, PATH_MAX, "%s/%s", NE_INIDIR, INI_FILE_NAME);
/** Ignore error messages */
iniparser_set_error_callback (error_callback);
/** Load up config from ini if available */
- dictionary *ini = iniparser_load (inipath != nullptr ? inipath : default_ini_path);
+ dictionary *ini =
+ iniparser_load (inipath != nullptr ? inipath : default_ini_path);
if (ini) {
- loadConfIni(ini);
- iniparser_freedict(ini);
+ loadConfIni (ini);
+ iniparser_freedict (ini);
}
/** Load up config from envvar if available */
- loadConfEnvvar();
+ loadConfEnvvar ();
}
/**
* @brief Restore configurations to the default ones
*/
void
-Conf::reset ()
-{
- setResvMemSize (std::to_string (default_resv_mem_size).c_str());
- setNumThreads (std::to_string (default_num_threads).c_str());
- setLogLevel (std::to_string (default_log_level).c_str());
+Conf::reset () {
+ setResvMemSize (std::to_string (default_resv_mem_size).c_str ());
+ setNumThreads (std::to_string (default_num_threads).c_str ());
+ setLogLevel (std::to_string (default_log_level).c_str ());
setLogDir (dafault_log_dir);
}
* @param[in] log_dir config string for log dir
*/
void
-Conf::setResvMemSize (const char *resv_mem_size)
-{
+Conf::setResvMemSize (const char *resv_mem_size) {
if (resv_mem_size) {
unsigned long val;
char *unit = nullptr;
* @param[in] log_dir config string for log dir
*/
void
-Conf::setLogDir (const char *log_dir)
-{
- if (log_dir && strlen(log_dir) < MAX_DIR_LEN) {
- memcpy (log_dir_, log_dir, strlen(log_dir));
- log_dir_[strlen(log_dir)] = '\x00';
+Conf::setLogDir (const char *log_dir) {
+ if (log_dir && strlen (log_dir) < MAX_DIR_LEN) {
+ memcpy (log_dir_, log_dir, strlen (log_dir));
+ log_dir_[strlen (log_dir)] = '\x00';
}
}
* @param[in] log_level maxmimum log level to print log messages to stderr
*/
void
-Conf::setLogLevel (const char *log_level)
-{
+Conf::setLogLevel (const char *log_level) {
if (log_level) {
unsigned long val;
* @param[in] num_threads config string for a number of threads
*/
void
-Conf::setNumThreads (const char *num_threads)
-{
+Conf::setNumThreads (const char *num_threads) {
if (num_threads) {
unsigned long val;
* @note if a config param is nullptr, it is not set. so use default one.
*/
void
-Conf::loadConfIni(dictionary *ini)
-{
- const char *resv_mem_size = iniparser_getstring(ini, "main:resv_mem_size", nullptr);
- const char *num_threads = iniparser_getstring (ini, "main:num_threads", nullptr);
- const char *log_dir = iniparser_getstring(ini, "debug:log_dir", nullptr);
+Conf::loadConfIni (dictionary *ini) {
+ const char *resv_mem_size =
+ iniparser_getstring (ini, "main:resv_mem_size", nullptr);
+ const char *num_threads =
+ iniparser_getstring (ini, "main:num_threads", nullptr);
+ const char *log_dir = iniparser_getstring (ini, "debug:log_dir", nullptr);
const char *log_level = iniparser_getstring (ini, "debug:log_level", nullptr);
setLogDir (log_dir);
* @brief Load configuration with env-vars
*/
void
-Conf::loadConfEnvvar()
-{
+Conf::loadConfEnvvar () {
const char *log_dir = getenv (ENV_LOG_DIR);
const char *resv_mem_size = getenv (ENV_RESV_MEM_SIZE);
const char *num_threads = getenv (ENV_NUM_THREADS);
#define MAX_DIR_LEN 256
-#define ENV_NUM_THREADS "NE_NUM_THREADS"
-#define ENV_RESV_MEM_SIZE "NE_RESV_MEM_SIZE"
-#define ENV_LOG_DIR "NE_LOG_DIR"
-#define ENV_LOG_LEVEL "NE_LOG_LEVEL"
+#define ENV_NUM_THREADS "NE_NUM_THREADS"
+#define ENV_RESV_MEM_SIZE "NE_RESV_MEM_SIZE"
+#define ENV_LOG_DIR "NE_LOG_DIR"
+#define ENV_LOG_LEVEL "NE_LOG_LEVEL"
static const uint32_t default_num_threads = 8;
-static const size_t default_resv_mem_size = 0; /* default mode is the CMA allocation */
+static const size_t default_resv_mem_size =
+ 0; /* default mode is the CMA allocation */
static const char dafault_log_dir[] = "/tmp/";
-static const uint32_t default_log_level = 1; /* default log level showing error messages only */
+static const uint32_t default_log_level =
+ 1; /* default log level showing error messages only */
/** @brief Configuration for NPU-Engine components */
class Conf {
- public:
- static Conf & getInstance (const char *inipath = nullptr);
+ public:
+ static Conf &getInstance (const char *inipath = nullptr);
- uint32_t getNumThreads() { return num_threads_; }
- size_t getResvMemSize() { return reserved_mem_size_; }
- const char * getLogDir() { return log_dir_; }
- uint32_t getLogLevel() { return log_level_; }
+ uint32_t getNumThreads () { return num_threads_; }
+ size_t getResvMemSize () { return reserved_mem_size_; }
+ const char *getLogDir () { return log_dir_; }
+ uint32_t getLogLevel () { return log_level_; }
- void reset();
+ void reset ();
- private:
- static std::unique_ptr<Conf> instance_;
- static std::once_flag once_flag_;
+ private:
+ static std::unique_ptr<Conf> instance_;
+ static std::once_flag once_flag_;
- Conf ();
+ Conf ();
- void loadConf (const char *inipath);
- void loadConfIni (dictionary *ini);
- void loadConfDefault ();
- void loadConfEnvvar();
+ void loadConf (const char *inipath);
+ void loadConfIni (dictionary *ini);
+ void loadConfDefault ();
+ void loadConfEnvvar ();
- void setNumThreads (const char *str);
- void setResvMemSize (const char *str);
- void setLogDir (const char *str);
- void setLogLevel (const char *str);
- void setLayerDump (const char *str);
+ void setNumThreads (const char *str);
+ void setResvMemSize (const char *str);
+ void setLogDir (const char *str);
+ void setLogLevel (const char *str);
+ void setLayerDump (const char *str);
- uint32_t num_threads_;
- /**< NE_NUM_THREADS, [main] num_threads, the number of threads in thread pool */
- size_t reserved_mem_size_;
- /**< NE_RESV_MEM_SIZE, [main] resv_mem_size, the size of memory reserved */
- char log_dir_[MAX_DIR_LEN];
- /**< NE_LOG_DIR, [debug] log_dir, the path where log files are created */
- uint32_t log_level_;
- /**< NE_LOG_LEVEL, [debug] log_level, maximum log level to print log messages to stderr */
+ uint32_t num_threads_;
+ /**< NE_NUM_THREADS, [main] num_threads, the number of threads in thread pool */
+ size_t reserved_mem_size_;
+ /**< NE_RESV_MEM_SIZE, [main] resv_mem_size, the size of memory reserved */
+ char log_dir_[MAX_DIR_LEN];
+ /**< NE_LOG_DIR, [debug] log_dir, the path where log files are created */
+ uint32_t log_level_;
+ /**< NE_LOG_LEVEL, [debug] log_level, maximum log level to print log messages to stderr */
};
#endif /* __NE_CONF_H__ */
* Logging utilities *
********************************************************************/
#define LOG_FILENAME "npu-engine.log"
-#define MAX_LOG_MSG 512
+#define MAX_LOG_MSG 512
/**
* @brief Log Level String in 5 characters
*/
static const char *loglevelstr[LOG_END] = {
- [LOG_NONE] = "NONE",
- [LOG_ERROR] = "ERROR",
- [LOG_WARN] = "WARN ",
- [LOG_INFO] = "INFO ",
+ [LOG_NONE] = "NONE",
+ [LOG_ERROR] = "ERROR",
+ [LOG_WARN] = "WARN ",
+ [LOG_INFO] = "INFO ",
};
/**
* @brief Module Name String in 8 characters
*/
static const char *modulestr[_NEND] = {
- [_N1] = "N1/CommPl",
- [_N11] = "N11/IP ",
- [_N2] = "N2/Handlr",
- [_N21] = "N21/Host ",
- [_N3] = "N3/Sched ",
- [_N4] = "N4/InputC",
- [_N41] = "N41/HWrec",
- [_N42] = "N42/Host ",
- [_N5] = "N5/ThPool",
- [_N6] = "N6/Mem ",
- [_N61] = "N61/MemDe",
- [_N62] = "N62/MemPl",
- [_N7] = "N7/HWmem ",
- [_N71] = "N71/Model",
- [_N72] = "N72/Buff ",
- [_N73] = "N72/Segt ",
- [_N8] = "N8/HWmemI",
- [_N9] = "N9/NPUdrv",
- [_N91] = "N91/TRIV ",
- [_N92] = "N92/TRIV2",
- [_N93] = "N93/TRIA ",
- [_N94] = "N94/Emul ",
+ [_N1] = "N1/CommPl", [_N11] = "N11/IP ", [_N2] = "N2/Handlr",
+ [_N21] = "N21/Host ", [_N3] = "N3/Sched ", [_N4] = "N4/InputC",
+ [_N41] = "N41/HWrec", [_N42] = "N42/Host ", [_N5] = "N5/ThPool",
+ [_N6] = "N6/Mem ", [_N61] = "N61/MemDe", [_N62] = "N62/MemPl",
+ [_N7] = "N7/HWmem ", [_N71] = "N71/Model", [_N72] = "N72/Buff ",
+ [_N73] = "N72/Segt ", [_N8] = "N8/HWmemI", [_N9] = "N9/NPUdrv",
+ [_N91] = "N91/TRIV ", [_N92] = "N92/TRIV2", [_N93] = "N93/TRIA ",
+ [_N94] = "N94/Emul ",
};
/** @brief singleton instance */
std::once_flag Logger::once_flag_;
/** @brief logger destructor. close the file pointer if opened before */
-Logger::~Logger ()
-{
+Logger::~Logger () {
std::unique_lock<std::mutex> lock (m_);
if (fp_ != nullptr)
fclose (fp_);
}
Logger &
-Logger::getInstance ()
-{
- call_once (once_flag_, []() {
- instance_.reset (new Logger);
- });
+Logger::getInstance () {
+ call_once (once_flag_, []() { instance_.reset (new Logger); });
return *(instance_.get ());
}
* a negative value is returned. Note that 0 is not regarded as an error.
*/
int
-Logger::logwrite (loglevel l, module m, const char *format, ...)
-{
+Logger::logwrite (loglevel l, module m, const char *format, ...) {
int ret;
if (l <= LOG_NONE || l >= LOG_END)
std::unique_lock<std::mutex> lock (m_);
time_t ltime = time (nullptr);
- char* time_str;
+ char *time_str;
va_list args;
/**
time_str[strcspn (time_str, "\n")] = '\x00';
char log_msg[MAX_LOG_MSG];
- int log_len = snprintf (log_msg, MAX_LOG_MSG,
- "[%s][%s][%s] ", loglevelstr[l], modulestr[m], time_str);
+ int log_len = snprintf (log_msg, MAX_LOG_MSG, "[%s][%s][%s] ", loglevelstr[l],
+ modulestr[m], time_str);
va_start (args, format);
- log_len += vsnprintf (log_msg + log_len, MAX_LOG_MSG - log_len,
- format, args);
+ log_len += vsnprintf (log_msg + log_len, MAX_LOG_MSG - log_len, format, args);
va_end (args);
- if (Conf::getInstance().getLogLevel() >= l) {
+ if (Conf::getInstance ().getLogLevel () >= l) {
fprintf (stderr, "%s\n", log_msg);
fflush (stderr);
}
if (fp_ == nullptr) {
char filename[FILENAME_MAX];
- snprintf(filename, FILENAME_MAX, "%s/%s",
- Conf::getInstance().getLogDir(), LOG_FILENAME);
+ snprintf (filename, FILENAME_MAX, "%s/%s",
+ Conf::getInstance ().getLogDir (), LOG_FILENAME);
fp_ = fopen (filename, "a");
if (fp_ == nullptr)
return -errno;
#ifndef PFN_UP
#define PFN_UP(x) (((x) + PAGE_SIZE - 1) >> PAGE_SHIFT)
#endif
-#define ALIGNED_SIZE(x) (PFN_UP(x) * PAGE_SIZE)
+#define ALIGNED_SIZE(x) (PFN_UP (x) * PAGE_SIZE)
/********************************************************************
* Logging utilities *
* @brief class for logging (singleton)
*/
class Logger {
- public:
- static Logger& getInstance ();
+ public:
+ static Logger &getInstance ();
- int logwrite (loglevel l, module m, const char *format, ...);
+ int logwrite (loglevel l, module m, const char *format, ...);
- ~Logger ();
+ ~Logger ();
- private:
- Logger () { fp_ = nullptr; }
+ private:
+ Logger () { fp_ = nullptr; }
- static std::unique_ptr<Logger> instance_;
- static std::once_flag once_flag_;
+ static std::unique_ptr<Logger> instance_;
+ static std::once_flag once_flag_;
- FILE *fp_; /**< file pointer of the log file */
- std::mutex m_; /**< mutex for synchronous writing */
+ FILE *fp_; /**< file pointer of the log file */
+ std::mutex m_; /**< mutex for synchronous writing */
};
/** @brief log macros with different log levels */
-#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
-#define logerr(m, fmt, ...) Logger::getInstance().logwrite(LOG_ERROR, m, "(%s:%d) " fmt, \
- __FILENAME__, __LINE__, ##__VA_ARGS__)
-#define logwarn(m, fmt, ...) Logger::getInstance().logwrite(LOG_WARN, m, "(%s:%d) " fmt, \
- __FILENAME__, __LINE__, ##__VA_ARGS__)
-#define loginfo(m, fmt, ...) Logger::getInstance().logwrite(LOG_INFO, m, "(%s:%d) " fmt, \
- __FILENAME__, __LINE__, ##__VA_ARGS__)
+#define __FILENAME__ \
+ (strrchr (__FILE__, '/') ? strrchr (__FILE__, '/') + 1 : __FILE__)
+#define logerr(m, fmt, ...) \
+ Logger::getInstance ().logwrite (LOG_ERROR, m, "(%s:%d) " fmt, __FILENAME__, \
+ __LINE__, ##__VA_ARGS__)
+#define logwarn(m, fmt, ...) \
+ Logger::getInstance ().logwrite (LOG_WARN, m, "(%s:%d) " fmt, __FILENAME__, \
+ __LINE__, ##__VA_ARGS__)
+#define loginfo(m, fmt, ...) \
+ Logger::getInstance ().logwrite (LOG_INFO, m, "(%s:%d) " fmt, __FILENAME__, \
+ __LINE__, ##__VA_ARGS__)
/** @brief thread-safe map */
-template<typename K, typename V>
-class ThreadSafeMap
-{
- public:
- ThreadSafeMap () : num_entries_ (0) {}
- ~ThreadSafeMap () { clear(); }
-
- /** @brief find the target element */
- V * find (K key) {
- typename std::map<K, std::unique_ptr<V>>::iterator it;
- std::unique_lock<std::mutex> lock(m_);
-
- it = map_.find (key);
- if (it == map_.end())
- return nullptr;
-
- return it->second.get();
- }
+template <typename K, typename V>
+class ThreadSafeMap {
+ public:
+ ThreadSafeMap () : num_entries_ (0) {}
+ ~ThreadSafeMap () { clear (); }
- /** @brief insert element to the map */
- int insert (K key, V * value) {
- std::unique_lock<std::mutex> lock(m_);
- auto status = map_.insert (std::make_pair (key, std::unique_ptr<V>(value)));
+ /** @brief find the target element */
+ V *find (K key) {
+ typename std::map<K, std::unique_ptr<V>>::iterator it;
+ std::unique_lock<std::mutex> lock (m_);
- if (status.second == false)
- return -EINVAL;
+ it = map_.find (key);
+ if (it == map_.end ())
+ return nullptr;
- num_entries_++;
+ return it->second.get ();
+ }
- return 0;
- }
+ /** @brief insert element to the map */
+ int insert (K key, V *value) {
+ std::unique_lock<std::mutex> lock (m_);
+ auto status =
+ map_.insert (std::make_pair (key, std::unique_ptr<V> (value)));
- /** @brief remove the target element */
- int remove (K key) {
- typename std::map<K, std::unique_ptr<V>>::iterator it;
- std::unique_lock<std::mutex> lock(m_);
+ if (status.second == false)
+ return -EINVAL;
- it = map_.find (key);
- if (it == map_.end())
- return -ENOENT;
+ num_entries_++;
- map_.erase (it);
- num_entries_--;
- if (num_entries_ == 0)
- cv_.notify_all ();
+ return 0;
+ }
- return 0;
- }
+ /** @brief remove the target element */
+ int remove (K key) {
+ typename std::map<K, std::unique_ptr<V>>::iterator it;
+ std::unique_lock<std::mutex> lock (m_);
- /** @brief remove all elements */
- void clear () {
- typename std::map<K, std::unique_ptr<V>>::iterator it;
- std::unique_lock<std::mutex> lock(m_);
+ it = map_.find (key);
+ if (it == map_.end ())
+ return -ENOENT;
- it = map_.begin ();
- while (it != map_.end())
- it = map_.erase (it);
+ map_.erase (it);
+ num_entries_--;
+ if (num_entries_ == 0)
+ cv_.notify_all ();
- num_entries_ = 0;
- }
+ return 0;
+ }
- /** @brief wait until all elements are removed */
- void wait_empty () {
- std::unique_lock<std::mutex> lock(m_);
- cv_.wait (lock, [this]() { return num_entries_ == 0; });
- }
+ /** @brief remove all elements */
+ void clear () {
+ typename std::map<K, std::unique_ptr<V>>::iterator it;
+ std::unique_lock<std::mutex> lock (m_);
+
+ it = map_.begin ();
+ while (it != map_.end ()) it = map_.erase (it);
- /**
+ num_entries_ = 0;
+ }
+
+ /** @brief wait until all elements are removed */
+ void wait_empty () {
+ std::unique_lock<std::mutex> lock (m_);
+ cv_.wait (lock, [this]() { return num_entries_ == 0; });
+ }
+
+ /**
* @brief apply the passed function for each entry.
* @note if the func returns true, it removes the entry
*/
- void for_each (std::function<bool (V *)> & func) {
- typename std::map<K, std::unique_ptr<V>>::iterator it;
- std::unique_lock<std::mutex> lock(m_);
-
- it = map_.begin ();
- while (it != map_.end()) {
- if (func (it->second.get ())) {
- it = map_.erase (it);
- num_entries_--;
- } else {
- ++it;
- }
- }
+ void for_each (std::function<bool(V *)> &func) {
+ typename std::map<K, std::unique_ptr<V>>::iterator it;
+ std::unique_lock<std::mutex> lock (m_);
- if (num_entries_ == 0)
- cv_.notify_all ();
+ it = map_.begin ();
+ while (it != map_.end ()) {
+ if (func (it->second.get ())) {
+ it = map_.erase (it);
+ num_entries_--;
+ } else {
+ ++it;
+ }
}
- private:
- uint32_t num_entries_; /**< number of entries */
- std::map<K, std::unique_ptr<V>> map_; /**< map internal instance */
- std::condition_variable cv_; /**< cond var for sync */
- std::mutex m_; /**< mutex for sync */
+ if (num_entries_ == 0)
+ cv_.notify_all ();
+ }
+
+ private:
+ uint32_t num_entries_; /**< number of entries */
+ std::map<K, std::unique_ptr<V>> map_; /**< map internal instance */
+ std::condition_variable cv_; /**< cond var for sync */
+ std::mutex m_; /**< mutex for sync */
};
#endif /* __NPU_ENGINE_UTILS_H__ */
#define TAG _N2
-#define INIT_HOST_HANDLER(handler, dev) \
- Device *tdev = static_cast <Device *> (dev); \
- if (tdev == nullptr) return -EINVAL; \
+#define INIT_HOST_HANDLER(handler, dev) \
+ Device *tdev = static_cast<Device *> (dev); \
+ if (tdev == nullptr) \
+ return -EINVAL; \
HostHandler *handler = tdev->getHostHandler (); \
- if (handler == nullptr) return -EINVAL;
+ if (handler == nullptr) \
+ return -EINVAL;
/**
* @brief Get npu-engine libraray version
* @param[out] minor minor version
* @param[out] extra extra version
*/
-void getVersion (uint32_t *major, uint32_t *minor, uint32_t *extra)
-{
+void
+getVersion (uint32_t *major, uint32_t *minor, uint32_t *extra) {
if (major != nullptr)
*major = VER_NE_MAJOR;
if (minor != nullptr)
* @retval 0 if no NPU devices available. if positive (number of NPUs) if NPU devices available. otherwise, a negative error value.
* @note the caller should call putNPUdevice() to release the device handle
*/
-int getnumNPUdeviceByType (dev_type type)
-{
+int
+getnumNPUdeviceByType (dev_type type) {
return HostHandler::getNumDevices (type);
}
* @return @c 0 if no error. otherwise a negative error value
* @note the caller should call putNPUdevice() to release the device handle
*/
-int getNPUdeviceByType (npudev_h *dev, dev_type type, uint32_t id)
-{
+int
+getNPUdeviceByType (npudev_h *dev, dev_type type, uint32_t id) {
return HostHandler::getDevice (dev, type, id);
}
* @return @c 0 if no error. otherwise a negative error value
* @note the caller should call putNPUdevice() to release the device handle
*/
-int getNPUdeviceByTypeAny (npudev_h *dev, dev_type type, uint32_t tops)
-{
+int
+getNPUdeviceByTypeAny (npudev_h *dev, dev_type type, uint32_t tops) {
int num_devices = getnumNPUdeviceByType (type);
if (tops == 0)
* @brief release the NPU device instance obtained by getDevice ()
* @param[in] dev the NPU device handle
*/
-void putNPUdevice (npudev_h dev)
-{
+void
+putNPUdevice (npudev_h dev) {
if (dev != nullptr)
delete static_cast<Device *> (dev);
}
* @note when this API level is lower than actual driver's one,
* some operations might be not working.
*/
-void getDriverAPILevel (uint32_t *level)
-{
+void
+getDriverAPILevel (uint32_t *level) {
if (level != nullptr)
*level = DriverAPI::getCompiledAPILevel ();
}
* @param[out] level driver API level
* @return 0 if no error, otherwise a negative errno
*/
-int getNPU_driverAPILevel (npudev_h dev, uint32_t *level)
-{
+int
+getNPU_driverAPILevel (npudev_h dev, uint32_t *level) {
INIT_HOST_HANDLER (host_handler, dev);
if (level == nullptr)
* @return 0 if no error, otherwise a negative errno
* @note this does not support for emulated devices
*/
-int getNPU_tops (npudev_h dev, uint32_t *tops)
-{
+int
+getNPU_tops (npudev_h dev, uint32_t *tops) {
INIT_HOST_HANDLER (host_handler, dev);
if (tops == nullptr)
* @return 0 if no error, otherwise a negative errno
* @note this does not support for emulated devices
*/
-int getNPU_dspmSize (npudev_h dev, uint32_t *dspm)
-{
+int
+getNPU_dspmSize (npudev_h dev, uint32_t *dspm) {
INIT_HOST_HANDLER (host_handler, dev);
if (dspm == nullptr)
* @param[out] profile The profile instance
* @return 0 if no error, otherwise a negative errno.
*/
-int getNPU_profile (npudev_h dev, int task_id, npu_profile *profile)
-{
+int
+getNPU_profile (npudev_h dev, int task_id, npu_profile *profile) {
INIT_HOST_HANDLER (host_handler, dev);
return host_handler->getProfile (task_id, profile);
* @brief Free the profile instance obtained by getNPU_profile().
* @param[in] profile The profile instance
*/
-void putNPU_profile (npu_profile *profile)
-{
+void
+putNPU_profile (npu_profile *profile) {
if (profile != nullptr) {
- delete [] profile->layers;
+ delete[] profile->layers;
if (profile->prof_path != nullptr)
free (profile->prof_path);
* @note The caller has the responsibility to free the resources.
* This API is not working on the emulated envionment.
*/
-int getNPU_statApps (npudev_h dev, npu_stat_apps *stat)
-{
+int
+getNPU_statApps (npudev_h dev, npu_stat_apps *stat) {
INIT_HOST_HANDLER (host_handler, dev);
if (!stat)
* @brief Free the stat instance obtained by getNPU_statApps().
* @param[in] stat Stat instance
*/
-void putNPU_statApps (npu_stat_apps *stat)
-{
+void
+putNPU_statApps (npu_stat_apps *stat) {
if (stat) {
- delete [] stat->stat;
+ delete[] stat->stat;
stat->num = 0;
}
}
* @note The caller has the responsibility to free the resources.
* This API is not working on the emulated envionment.
*/
-int getNPU_statReqs (npudev_h dev, int appid, npu_stat_reqs *stat)
-{
+int
+getNPU_statReqs (npudev_h dev, int appid, npu_stat_reqs *stat) {
INIT_HOST_HANDLER (host_handler, dev);
if (!stat)
* @brief Free the stat instance obtained by getNPU_statReqs().
* @param[in] stat Stat instance
*/
-void putNPU_statReqs (npu_stat_reqs *stat)
-{
+void
+putNPU_statReqs (npu_stat_reqs *stat) {
if (stat) {
- delete [] stat->stat;
+ delete[] stat->stat;
stat->num = 0;
}
}
*
* @todo Add a variation: in-memory model register.
*/
-int registerNPUmodel (npudev_h dev, generic_buffer *modelfile, uint32_t *modelid)
-{
+int
+registerNPUmodel (npudev_h dev, generic_buffer *modelfile, uint32_t *modelid) {
INIT_HOST_HANDLER (host_handler, dev);
return host_handler->registerModel (modelfile, modelid);
* @return @c 0 if no error. otherwise a negative error value
* @detail This may incur some latency with memory compatcion.
*/
-int unregisterNPUmodel(npudev_h dev, uint32_t modelid)
-{
+int
+unregisterNPUmodel (npudev_h dev, uint32_t modelid) {
INIT_HOST_HANDLER (host_handler, dev);
return host_handler->unregisterModel (modelid);
* @param[in] dev The NPU device handle
* @return @c 0 if no error. otherwise a negative error value
*/
-int unregisterNPUmodel_all(npudev_h dev)
-{
+int
+unregisterNPUmodel_all (npudev_h dev) {
INIT_HOST_HANDLER (host_handler, dev);
return host_handler->unregisterModels ();
* @return @c 0 if no error. otherwise a negative error value
* @note if this function is not called, default layout/type will be used.
*/
-int setNPU_dataInfo(npudev_h dev, uint32_t modelid,
- tensors_data_info *info_in, tensors_data_info *info_out)
-{
+int
+setNPU_dataInfo (npudev_h dev, uint32_t modelid, tensors_data_info *info_in,
+ tensors_data_info *info_out) {
INIT_HOST_HANDLER (host_handler, dev);
return host_handler->setDataInfo (modelid, info_in, info_out);
* @return @c 0 if no error. otherwise a negative error value
* @note If this function is not called, default values are used.
*/
-int setNPU_constraint(npudev_h dev, uint32_t modelid, npuConstraint constraint)
-{
+int
+setNPU_constraint (npudev_h dev, uint32_t modelid, npuConstraint constraint) {
INIT_HOST_HANDLER (host_handler, dev);
return host_handler->setConstraint (modelid, constraint);
* @detail This is a syntactic sugar of runNPU_async().
* CAUTION: There is a memcpy for the output buffer.
*/
-int runNPU_sync(npudev_h dev, uint32_t modelid, const input_buffers *input,
- output_buffers *output)
-{
+int
+runNPU_sync (npudev_h dev, uint32_t modelid, const input_buffers *input,
+ output_buffers *output) {
INIT_HOST_HANDLER (host_handler, dev);
return host_handler->runSync (modelid, input, output);
* @param[in] mode Configures how this operation works.
* @return @c 0 if no error. otherwise a negative error value
*/
-int runNPU_async(npudev_h dev, uint32_t modelid, const input_buffers *input,
- npuOutputNotify cb, uint64_t *sequence, void *data,
- npu_async_mode mode)
-{
+int
+runNPU_async (npudev_h dev, uint32_t modelid, const input_buffers *input,
+ npuOutputNotify cb, uint64_t *sequence, void *data,
+ npu_async_mode mode) {
INIT_HOST_HANDLER (host_handler, dev);
return host_handler->runAsync (modelid, input, cb, data, mode, sequence);
* @param[in] hw_dev The target device feeding input data
* @return @c 0 if no error. otherwise a negative error value
*/
-int runNPU_internalInput(npudev_h dev, uint32_t modelid, npu_input_opmode opmode,
- const char * hw_dev)
-{
+int
+runNPU_internalInput (npudev_h dev, uint32_t modelid, npu_input_opmode opmode,
+ const char *hw_dev) {
INIT_HOST_HANDLER (host_handler, dev);
return host_handler->runInternal (modelid, opmode, hw_dev);
* @param[in] id The request id
* @return @c 0 if no error. otherwise a negative error value
*/
-int stopNPU_internalInput(npudev_h dev, int id)
-{
+int
+stopNPU_internalInput (npudev_h dev, int id) {
INIT_HOST_HANDLER (host_handler, dev);
return host_handler->stopInternal (id);
* @param[in/out] Buffer the buffer pointer where memory is allocated.
* @return 0 if no error, otherwise a negative errno.
*/
-int allocNPU_genericBuffer (npudev_h dev, generic_buffer * buffer)
-{
+int
+allocNPU_genericBuffer (npudev_h dev, generic_buffer *buffer) {
INIT_HOST_HANDLER (host_handler, dev);
return host_handler->allocGenericBuffer (buffer);
* @param[in] buffer the model buffer
* @return 0 if no error, otherwise a negative errno.
*/
-int cleanNPU_genericBuffer (npudev_h dev, generic_buffer * buffer)
-{
+int
+cleanNPU_genericBuffer (npudev_h dev, generic_buffer *buffer) {
INIT_HOST_HANDLER (host_handler, dev);
return host_handler->deallocGenericBuffer (buffer);
* @return 0 if no error, otherwise a negative errno.
* @note it reuses allocGenericBuffer().
*/
-int allocNPU_genericBuffers (npudev_h dev, generic_buffers * buffers)
-{
+int
+allocNPU_genericBuffers (npudev_h dev, generic_buffers *buffers) {
INIT_HOST_HANDLER (host_handler, dev);
return host_handler->allocGenericBuffer (buffers);
* @note it reuses cleanGenericbuffer().
* @return 0 if no error, otherwise a negative errno.
*/
-int cleanNPU_genericBuffers (npudev_h dev, generic_buffers * buffers)
-{
+int
+cleanNPU_genericBuffers (npudev_h dev, generic_buffers *buffers) {
INIT_HOST_HANDLER (host_handler, dev);
return host_handler->deallocGenericBuffer (buffers);
/**
* @brief alias of allocNPU_genericBuffer for model buffer
*/
-int allocNPU_modelBuffer (npudev_h dev, generic_buffer * model)
-{
+int
+allocNPU_modelBuffer (npudev_h dev, generic_buffer *model) {
return allocNPU_genericBuffer (dev, model);
}
/**
* @brief alias of cleanNPU_genericBuffer for model buffer
*/
-int cleanNPU_modelBuffer (npudev_h dev, generic_buffer * model)
-{
+int
+cleanNPU_modelBuffer (npudev_h dev, generic_buffer *model) {
return cleanNPU_genericBuffer (dev, model);
}
/**
* @brief alias of allocNPU_genericBuffer for input buffer
*/
-int allocNPU_inputBuffer (npudev_h dev, generic_buffer * input)
-{
+int
+allocNPU_inputBuffer (npudev_h dev, generic_buffer *input) {
return allocNPU_genericBuffer (dev, input);
}
/**
* @brief alias of cleanNPU_genericBuffer for input buffer
*/
-int cleanNPU_inputBuffer (npudev_h dev, generic_buffer * input)
-{
+int
+cleanNPU_inputBuffer (npudev_h dev, generic_buffer *input) {
return cleanNPU_genericBuffer (dev, input);
}
/**
* @brief alias of allocNPU_genericBuffers for input buffers
*/
-int allocNPU_inputBuffers (npudev_h dev, input_buffers * input)
-{
+int
+allocNPU_inputBuffers (npudev_h dev, input_buffers *input) {
return allocNPU_genericBuffers (dev, input);
}
/**
* @brief alias of cleanNPU_genericBuffers for input buffers
*/
-int cleanNPU_inputBuffers (npudev_h dev, input_buffers * input)
-{
+int
+cleanNPU_inputBuffers (npudev_h dev, input_buffers *input) {
return cleanNPU_genericBuffers (dev, input);
}
* @param[out] free_total The size of freed memory until now
* @return @c 0 if no error. otherwise a negatice error value
*/
-int getNPU_memoryStatus(npudev_h dev, size_t *alloc_total, size_t *free_total)
-{
+int
+getNPU_memoryStatus (npudev_h dev, size_t *alloc_total, size_t *free_total) {
INIT_HOST_HANDLER (host_handler, dev);
return host_handler->getMemoryStatus (alloc_total, free_total);
* @param[out] num_requests the number of running requests (or pending)
* @return 0 if no error, otherwise a negative errno.
*/
-int getNPU_deviceStatus(npudev_h dev, npu_status *status, uint32_t *num_requests)
-{
+int
+getNPU_deviceStatus (npudev_h dev, npu_status *status, uint32_t *num_requests) {
INIT_HOST_HANDLER (host_handler, dev);
return host_handler->getDeviceStatus (status, num_requests);
* used for second-party users (e.g., compiler, simulator).
* @note the caller needs to free the metadata
*/
-npubin_meta * getNPUmodel_metadata (const char *model, bool need_extended)
-{
+npubin_meta *
+getNPUmodel_metadata (const char *model, bool need_extended) {
npubin_meta *meta;
FILE *fp;
size_t ret;
if (need_extended && NPUBIN_META_NUM_EXTENDED (meta->magiccode) > 0) {
npubin_meta *new_meta;
- new_meta = (npubin_meta *) malloc (NPUBIN_META_TOTAL_SIZE(meta->magiccode));
+ new_meta =
+ (npubin_meta *) malloc (NPUBIN_META_TOTAL_SIZE (meta->magiccode));
if (!new_meta) {
logerr (TAG, "Failed to allocate extended metadata\n");
goto exit_free;
}
- memcpy (new_meta, meta, NPUBIN_META_TOTAL_SIZE(meta->magiccode));
+ memcpy (new_meta, meta, NPUBIN_META_TOTAL_SIZE (meta->magiccode));
free (meta);
meta = nullptr;
ret = fread (new_meta->reserved_extended, 1,
- NPUBIN_META_EXTENDED_SIZE (new_meta->magiccode), fp);
+ NPUBIN_META_EXTENDED_SIZE (new_meta->magiccode), fp);
if (ret != NPUBIN_META_EXTENDED_SIZE (new_meta->magiccode)) {
logerr (TAG, "Invalid extended metadata provided\n");
free (new_meta);
/** @brief c++ class to describe how to use npu-engine library */
class TesterTRIV2 {
- public:
- TesterTRIV2 () : dev_ (nullptr), model_id_ (0) {
- memset (&input_, '\x00', sizeof (input_buffers));
- done_ = 0;
- repeat_ = TRIV2_NUM_RUNS;
- }
-
- ~TesterTRIV2 () {
- /** release resources */
- if (dev_ != nullptr) {
- if (input_.num_buffers != 0)
- cleanNPU_inputBuffers (dev_, &input_);
-
- if (model_id_ > 0)
- unregisterNPUmodel (dev_, model_id_);
-
- putNPUdevice (dev_);
- }
- }
-
- /** @brief initilize the device handle */
- int init () {
- int num_devices = getnumNPUdeviceByType (TRIV2_TYPE);
- if (num_devices <= 0)
- return -ENODEV;
-
- /** use any available TRIV2 device. (0 <= dev_id < num_devices) */
- int dev_id = num_devices - 1;
- return getNPUdeviceByType (&dev_, TRIV2_TYPE, dev_id);
+ public:
+ TesterTRIV2 () : dev_ (nullptr), model_id_ (0) {
+ memset (&input_, '\x00', sizeof (input_buffers));
+ done_ = 0;
+ repeat_ = TRIV2_NUM_RUNS;
+ }
+
+ ~TesterTRIV2 () {
+ /** release resources */
+ if (dev_ != nullptr) {
+ if (input_.num_buffers != 0)
+ cleanNPU_inputBuffers (dev_, &input_);
+
+ if (model_id_ > 0)
+ unregisterNPUmodel (dev_, model_id_);
+
+ putNPUdevice (dev_);
}
+ }
+
+ /** @brief initilize the device handle */
+ int init () {
+ int num_devices = getnumNPUdeviceByType (TRIV2_TYPE);
+ if (num_devices <= 0)
+ return -ENODEV;
+
+ /** use any available TRIV2 device. (0 <= dev_id < num_devices) */
+ int dev_id = num_devices - 1;
+ return getNPUdeviceByType (&dev_, TRIV2_TYPE, dev_id);
+ }
+
+ /** @brief run the inference (with dummy data) */
+ int run () {
+ int status = prepare_model ();
+ if (status != 0)
+ return status;
- /** @brief run the inference (with dummy data) */
- int run () {
- int status = prepare_model ();
- if (status != 0)
- return status;
+ status = prepare_input ();
+ if (status != 0)
+ return status;
- status = prepare_input ();
- if (status != 0)
+ for (uint32_t i = 0; i < repeat_; i++) {
+ status = runNPU_async (dev_, model_id_, &input_, callback, NULL, this,
+ NPUASYNC_WAIT);
+ if (status < 0)
return status;
-
- for (uint32_t i = 0; i < repeat_; i++) {
- status = runNPU_async (dev_, model_id_, &input_, callback,
- NULL, this, NPUASYNC_WAIT);
- if (status < 0)
- return status;
- }
-
- return wait_runs ();
}
- int wait_runs () {
- std::unique_lock<std::mutex> lock (m_);
- cv_.wait (lock, [this]() { return done_ == repeat_; });
-
- return 0;
- }
-
- static void callback (output_buffers *output, uint64_t sequence,
- void *data) {
- TesterTRIV2 *tester = static_cast<TesterTRIV2 *>(data);
-
- tester->compare_result (output);
- }
+ return wait_runs ();
+ }
+
+ int wait_runs () {
+ std::unique_lock<std::mutex> lock (m_);
+ cv_.wait (lock, [this]() { return done_ == repeat_; });
+
+ return 0;
+ }
+
+ static void callback (output_buffers *output, uint64_t sequence, void *data) {
+ TesterTRIV2 *tester = static_cast<TesterTRIV2 *> (data);
+
+ tester->compare_result (output);
+ }
+
+ private:
+ const uint32_t segment_size = 0x1000;
+ const uint64_t weight_size = 0x1000;
+
+ int prepare_model () {
+ generic_buffer model;
+ npubin_meta meta;
+
+ memset (&meta, '\x00', sizeof (npubin_meta));
+
+ meta.magiccode = NPUBIN_MAGICCODE | 0x3; /** npubinfmt v3 for TRIV2 */
+ meta.program_size = 0;
+ meta.weight_size = weight_size;
+ meta.size = NPUBIN_META_SIZE + weight_size;
+ meta.type = SMODEL_OPS_NPU;
+
+ /** TRIV2 requires segment table */
+ meta.segment_num = 3;
+ for (uint32_t idx = 0; idx < meta.segment_num; idx++)
+ meta.segment_size[idx] = segment_size;
+
+ meta.weight_seg_idx = 0;
+
+ meta.input_seg_num = 1;
+ meta.input_seg_idx[0] = 1;
+ meta.input_seg_off[0] = 0;
+ meta.input_seg_dims[0][0] = 1;
+ meta.input_seg_dims[0][1] = 1;
+ meta.input_seg_dims[0][2] = 1;
+ meta.input_seg_dims[0][3] = segment_size;
+
+ meta.output_seg_num = 1;
+ meta.output_seg_idx[0] = 1;
+ meta.output_seg_off[0] = 0;
+ meta.output_seg_dims[0][0] = 1;
+ meta.output_seg_dims[0][1] = 1;
+ meta.output_seg_dims[0][2] = 1;
+ meta.output_seg_dims[0][3] = segment_size;
+
+ model.type = BUFFER_MAPPED;
+ model.size = meta.size;
+ int status = allocNPU_modelBuffer (dev_, &model);
+ if (status != 0)
+ return status;
- private:
- const uint32_t segment_size = 0x1000;
- const uint64_t weight_size = 0x1000;
-
- int prepare_model () {
- generic_buffer model;
- npubin_meta meta;
-
- memset (&meta, '\x00', sizeof(npubin_meta));
-
- meta.magiccode = NPUBIN_MAGICCODE | 0x3; /** npubinfmt v3 for TRIV2 */
- meta.program_size = 0;
- meta.weight_size = weight_size;
- meta.size = NPUBIN_META_SIZE + weight_size;
- meta.type = SMODEL_OPS_NPU;
-
- /** TRIV2 requires segment table */
- meta.segment_num = 3;
- for (uint32_t idx = 0; idx < meta.segment_num; idx++)
- meta.segment_size[idx] = segment_size;
-
- meta.weight_seg_idx = 0;
-
- meta.input_seg_num = 1;
- meta.input_seg_idx[0] = 1;
- meta.input_seg_off[0] = 0;
- meta.input_seg_dims[0][0] = 1;
- meta.input_seg_dims[0][1] = 1;
- meta.input_seg_dims[0][2] = 1;
- meta.input_seg_dims[0][3] = segment_size;
-
- meta.output_seg_num = 1;
- meta.output_seg_idx[0] = 1;
- meta.output_seg_off[0] = 0;
- meta.output_seg_dims[0][0] = 1;
- meta.output_seg_dims[0][1] = 1;
- meta.output_seg_dims[0][2] = 1;
- meta.output_seg_dims[0][3] = segment_size;
-
- model.type = BUFFER_MAPPED;
- model.size = meta.size;
- int status = allocNPU_modelBuffer (dev_, &model);
- if (status != 0)
- return status;
+ /** copy only metadata as it's dummy model */
+ memcpy (model.addr, &meta, NPUBIN_META_SIZE);
- /** copy only metadata as it's dummy model */
- memcpy (model.addr, &meta, NPUBIN_META_SIZE);
+ status = registerNPUmodel (dev_, &model, &model_id_);
+ cleanNPU_modelBuffer (dev_, &model);
- status = registerNPUmodel (dev_, &model, &model_id_);
- cleanNPU_modelBuffer (dev_, &model);
+ return status;
+ }
- return status;
- }
+ int prepare_input () {
+ input_.num_buffers = 1;
+ input_.bufs[0].type = BUFFER_MAPPED;
+ input_.bufs[0].size = segment_size;
- int prepare_input () {
- input_.num_buffers = 1;
- input_.bufs[0].type = BUFFER_MAPPED;
- input_.bufs[0].size = segment_size;
+ return allocNPU_inputBuffers (dev_, &input_);
+ }
- return allocNPU_inputBuffers (dev_, &input_);
+ void compare_result (output_buffers *output) {
+ /** return success as it's dummy, but need to free output buffers */
+ for (uint32_t idx = 0; idx < output->num_buffers; idx++) {
+ generic_buffer *buffer = &output->bufs[idx];
+ if (buffer->addr != NULL)
+ free (buffer->addr);
}
- void compare_result (output_buffers * output) {
- /** return success as it's dummy, but need to free output buffers */
- for (uint32_t idx = 0; idx < output->num_buffers; idx++) {
- generic_buffer * buffer = &output->bufs[idx];
- if (buffer->addr != NULL)
- free (buffer->addr);
- }
-
- {
- std::unique_lock<std::mutex> lock (m_);
- done_++;
- cv_.notify_one ();
- }
+ {
+ std::unique_lock<std::mutex> lock (m_);
+ done_++;
+ cv_.notify_one ();
}
+ }
- npudev_h dev_;
+ npudev_h dev_;
- uint32_t done_;
- uint32_t repeat_;
+ uint32_t done_;
+ uint32_t repeat_;
- std::mutex m_;
- std::condition_variable cv_;
+ std::mutex m_;
+ std::condition_variable cv_;
- uint32_t model_id_;
- input_buffers input_;
+ uint32_t model_id_;
+ input_buffers input_;
};
/** @brief apptest main */
int
-main (int argc, char **argv)
-{
-#if defined (ENABLE_EMUL)
+main (int argc, char **argv) {
+#if defined(ENABLE_EMUL)
TesterTRIV2 tester;
int status;
/** ./include/typedef.h */
#define TRIV2_TYPE NPUCOND_TRIV2_CONN_SOCIP
-#define DUMMY_HW_DEVPATH ("/dev/dummy")
-#define NPU_MODEL_NAME ("model.tvn")
-#define DEFAULT_TIME (3000) /* 3000 ms */
+#define DUMMY_HW_DEVPATH ("/dev/dummy")
+#define NPU_MODEL_NAME ("model.tvn")
+#define DEFAULT_TIME (3000) /* 3000 ms */
/** @brief c++ class to describe how to use npu-engine library */
class TesterTRIV2 {
- public:
- TesterTRIV2 () : dev_ (nullptr), meta_ (nullptr), model_id_ (0) {
- }
+ public:
+ TesterTRIV2 () : dev_ (nullptr), meta_ (nullptr), model_id_ (0) {}
- ~TesterTRIV2 () {
- /** release resources */
- if (dev_ != nullptr) {
- if (model_id_ > 0)
- unregisterNPUmodel (dev_, model_id_);
+ ~TesterTRIV2 () {
+ /** release resources */
+ if (dev_ != nullptr) {
+ if (model_id_ > 0)
+ unregisterNPUmodel (dev_, model_id_);
- if (meta_ != nullptr)
- free (meta_);
+ if (meta_ != nullptr)
+ free (meta_);
- putNPUdevice (dev_);
- }
+ putNPUdevice (dev_);
}
+ }
+
+ /** @brief initilize the device handle */
+ int init () {
+ int num_devices = getnumNPUdeviceByType (TRIV2_TYPE);
+ if (num_devices <= 0)
+ return -ENODEV;
+
+ /** use any available TRIV2 device. (0 <= dev_id < num_devices) */
+ int dev_id = num_devices - 1;
+ return getNPUdeviceByType (&dev_, TRIV2_TYPE, dev_id);
+ }
+
+ /** @brief run the inference (with dummy data) */
+ int run () {
+ int status = prepare_model ();
+ if (status != 0)
+ return status;
- /** @brief initilize the device handle */
- int init () {
- int num_devices = getnumNPUdeviceByType (TRIV2_TYPE);
- if (num_devices <= 0)
- return -ENODEV;
-
- /** use any available TRIV2 device. (0 <= dev_id < num_devices) */
- int dev_id = num_devices - 1;
- return getNPUdeviceByType (&dev_, TRIV2_TYPE, dev_id);
- }
+ status = set_data_info ();
+ if (status != 0)
+ return status;
- /** @brief run the inference (with dummy data) */
- int run () {
- int status = prepare_model ();
- if (status != 0)
- return status;
+ int id = runNPU_internalInput (dev_, model_id_, NPUINPUT_HW_RECURRING,
+ DUMMY_HW_DEVPATH);
+ if (id < 0)
+ return id;
+
+ usleep (DEFAULT_TIME * 1000);
+
+ return stopNPU_internalInput (dev_, id);
+ }
+
+ private:
+ const uint64_t program_size = 0x1000;
+ const uint64_t weight_size = 0x1000;
+ const uint32_t segment_size = 0x1000;
+
+ int prepare_model () {
+ meta_ = (npubin_meta *) calloc (1, NPUBIN_META_SIZE);
+ if (meta_ == nullptr)
+ return -ENOMEM;
+
+ meta_->magiccode = NPUBIN_MAGICCODE | 0x3; /** npubinfmt v3 for TRIV2 */
+ meta_->program_size = program_size;
+ meta_->weight_size = weight_size;
+ meta_->size = NPUBIN_META_SIZE + program_size + weight_size;
+ meta_->type = SMODEL_OPS_NPU;
+
+ /** TRIV2 requires segment table */
+ meta_->segment_num = 3;
+ for (uint32_t idx = 0; idx < meta_->segment_num; idx++)
+ meta_->segment_size[idx] = segment_size;
+
+ meta_->weight_seg_idx = 0;
+
+ meta_->input_seg_num = 1;
+ meta_->input_seg_idx[0] = 1;
+ meta_->input_seg_off[0] = 0;
+ meta_->input_seg_dims[0][0] = 1;
+ meta_->input_seg_dims[0][1] = 1;
+ meta_->input_seg_dims[0][2] = 1;
+ meta_->input_seg_dims[0][3] = segment_size;
+
+ meta_->output_seg_num = 1;
+ meta_->output_seg_idx[0] = 1;
+ meta_->output_seg_off[0] = 0;
+ meta_->output_seg_dims[0][0] = 1;
+ meta_->output_seg_dims[0][1] = 1;
+ meta_->output_seg_dims[0][2] = 1;
+ meta_->output_seg_dims[0][3] = segment_size;
+
+ generic_buffer model;
+
+ model.type = BUFFER_MAPPED;
+ model.size = meta_->size;
+ int status = allocNPU_modelBuffer (dev_, &model);
+ if (status != 0)
+ return status;
- status = set_data_info ();
- if (status != 0)
- return status;
+ /** copy only metadata as it's dummy model */
+ memcpy (model.addr, meta_, NPUBIN_META_SIZE);
- int id = runNPU_internalInput (dev_, model_id_, NPUINPUT_HW_RECURRING,
- DUMMY_HW_DEVPATH);
- if (id < 0)
- return id;
+ status = registerNPUmodel (dev_, &model, &model_id_);
+ cleanNPU_modelBuffer (dev_, &model);
- usleep (DEFAULT_TIME * 1000);
+ return status;
+ }
- return stopNPU_internalInput (dev_, id);
- }
+ int set_data_info () {
+ tensors_data_info info_in;
+ tensors_data_info info_out;
- private:
- const uint64_t program_size = 0x1000;
- const uint64_t weight_size = 0x1000;
- const uint32_t segment_size = 0x1000;
-
- int prepare_model () {
- meta_ = (npubin_meta *) calloc (1, NPUBIN_META_SIZE);
- if (meta_ == nullptr)
- return -ENOMEM;
-
- meta_->magiccode = NPUBIN_MAGICCODE | 0x3; /** npubinfmt v3 for TRIV2 */
- meta_->program_size = program_size;
- meta_->weight_size = weight_size;
- meta_->size = NPUBIN_META_SIZE + program_size + weight_size;
- meta_->type = SMODEL_OPS_NPU;
-
- /** TRIV2 requires segment table */
- meta_->segment_num = 3;
- for (uint32_t idx = 0; idx < meta_->segment_num; idx++)
- meta_->segment_size[idx] = segment_size;
-
- meta_->weight_seg_idx = 0;
-
- meta_->input_seg_num = 1;
- meta_->input_seg_idx[0] = 1;
- meta_->input_seg_off[0] = 0;
- meta_->input_seg_dims[0][0] = 1;
- meta_->input_seg_dims[0][1] = 1;
- meta_->input_seg_dims[0][2] = 1;
- meta_->input_seg_dims[0][3] = segment_size;
-
- meta_->output_seg_num = 1;
- meta_->output_seg_idx[0] = 1;
- meta_->output_seg_off[0] = 0;
- meta_->output_seg_dims[0][0] = 1;
- meta_->output_seg_dims[0][1] = 1;
- meta_->output_seg_dims[0][2] = 1;
- meta_->output_seg_dims[0][3] = segment_size;
-
- generic_buffer model;
-
- model.type = BUFFER_MAPPED;
- model.size = meta_->size;
- int status = allocNPU_modelBuffer (dev_, &model);
- if (status != 0)
- return status;
-
- /** copy only metadata as it's dummy model */
- memcpy (model.addr, meta_, NPUBIN_META_SIZE);
-
- status = registerNPUmodel (dev_, &model, &model_id_);
- cleanNPU_modelBuffer (dev_, &model);
+ /* No data manipulation & quantization in this test */
- return status;
+ info_in.num_info = meta_->input_seg_num;
+ for (uint32_t idx = 0; idx < info_in.num_info; idx++) {
+ info_in.info[idx].layout = DATA_LAYOUT_TRIV2;
+ info_in.info[idx].type = DATA_TYPE_QASYMM8;
}
- int set_data_info () {
- tensors_data_info info_in;
- tensors_data_info info_out;
-
- /* No data manipulation & quantization in this test */
-
- info_in.num_info = meta_->input_seg_num;
- for (uint32_t idx = 0; idx < info_in.num_info; idx++) {
- info_in.info[idx].layout = DATA_LAYOUT_TRIV2;
- info_in.info[idx].type = DATA_TYPE_QASYMM8;
- }
-
- info_out.num_info = meta_->output_seg_num;
- for (uint32_t idx = 0; idx < info_out.num_info; idx++) {
- info_out.info[idx].layout = DATA_LAYOUT_TRIV2;
- info_out.info[idx].type = DATA_TYPE_QASYMM8;
- }
-
- return setNPU_dataInfo (dev_, model_id_, &info_in, &info_out);
+ info_out.num_info = meta_->output_seg_num;
+ for (uint32_t idx = 0; idx < info_out.num_info; idx++) {
+ info_out.info[idx].layout = DATA_LAYOUT_TRIV2;
+ info_out.info[idx].type = DATA_TYPE_QASYMM8;
}
- npudev_h dev_;
+ return setNPU_dataInfo (dev_, model_id_, &info_in, &info_out);
+ }
+
+ npudev_h dev_;
- npubin_meta *meta_;
- uint32_t model_id_;
+ npubin_meta *meta_;
+ uint32_t model_id_;
};
/** @brief apptest main */
int
-main (int argc, char **argv)
-{
-#if defined (ENABLE_EMUL)
+main (int argc, char **argv) {
+#if defined(ENABLE_EMUL)
TesterTRIV2 tester;
int status;
/** @brief c++ class to describe how to use npu-engine library */
class TesterTRIV2 {
- public:
- TesterTRIV2 () : dev_ (nullptr), model_id_ (0) {
- memset (&input_, '\x00', sizeof (input_buffers));
- }
+ public:
+ TesterTRIV2 () : dev_ (nullptr), model_id_ (0) {
+ memset (&input_, '\x00', sizeof (input_buffers));
+ }
- ~TesterTRIV2 () {
- /** release resources */
- if (dev_ != nullptr) {
- if (input_.num_buffers != 0)
- cleanNPU_inputBuffers (dev_, &input_);
+ ~TesterTRIV2 () {
+ /** release resources */
+ if (dev_ != nullptr) {
+ if (input_.num_buffers != 0)
+ cleanNPU_inputBuffers (dev_, &input_);
- if (model_id_ > 0)
- unregisterNPUmodel (dev_, model_id_);
+ if (model_id_ > 0)
+ unregisterNPUmodel (dev_, model_id_);
- putNPUdevice (dev_);
- }
+ putNPUdevice (dev_);
}
+ }
+
+ /** @brief initilize the device handle */
+ int init () {
+ int num_devices = getnumNPUdeviceByType (TRIV2_TYPE);
+ if (num_devices <= 0)
+ return -ENODEV;
+
+ /** use any available TRIV2 device. (0 <= dev_id < num_devices) */
+ int dev_id = num_devices - 1;
+ return getNPUdeviceByType (&dev_, TRIV2_TYPE, dev_id);
+ }
+
+ /** @brief run the inference (with dummy data) */
+ int run () {
+ int status = prepare_model ();
+ if (status != 0)
+ return status;
- /** @brief initilize the device handle */
- int init () {
- int num_devices = getnumNPUdeviceByType (TRIV2_TYPE);
- if (num_devices <= 0)
- return -ENODEV;
-
- /** use any available TRIV2 device. (0 <= dev_id < num_devices) */
- int dev_id = num_devices - 1;
- return getNPUdeviceByType (&dev_, TRIV2_TYPE, dev_id);
- }
+ status = prepare_input ();
+ if (status != 0)
+ return status;
- /** @brief run the inference (with dummy data) */
- int run () {
- int status = prepare_model ();
- if (status != 0)
- return status;
+ output_buffers output;
+ status = runNPU_sync (dev_, model_id_, &input_, &output);
+ if (status < 0)
+ return status;
- status = prepare_input ();
- if (status != 0)
- return status;
+ return compare_result (output);
+ }
+
+ private:
+ const uint64_t weight_size = 0x1000;
+ const uint32_t segment_size = 0x1000;
+
+ int prepare_model () {
+ generic_buffer model;
+ npubin_meta meta;
+
+ memset (&meta, '\x00', sizeof (npubin_meta));
+
+ meta.magiccode = NPUBIN_MAGICCODE | 0x3; /** npubinfmt v3 for TRIV2 */
+ meta.program_size = 0;
+ meta.weight_size = weight_size;
+ meta.size = NPUBIN_META_SIZE + weight_size;
+ meta.type = SMODEL_OPS_NPU;
+
+ /** TRIV2 requires segment table */
+ meta.segment_num = 3;
+ for (uint32_t idx = 0; idx < meta.segment_num; idx++)
+ meta.segment_size[idx] = segment_size;
+
+ meta.weight_seg_idx = 0;
+
+ meta.input_seg_num = 1;
+ meta.input_seg_idx[0] = 1;
+ meta.input_seg_off[0] = 0;
+ meta.input_seg_dims[0][0] = 1;
+ meta.input_seg_dims[0][1] = 1;
+ meta.input_seg_dims[0][2] = 1;
+ meta.input_seg_dims[0][3] = segment_size;
+
+ meta.output_seg_num = 1;
+ meta.output_seg_idx[0] = 1;
+ meta.output_seg_off[0] = 0;
+ meta.output_seg_dims[0][0] = 1;
+ meta.output_seg_dims[0][1] = 1;
+ meta.output_seg_dims[0][2] = 1;
+ meta.output_seg_dims[0][3] = segment_size;
+
+ model.type = BUFFER_MAPPED;
+ model.size = meta.size;
+ int status = allocNPU_modelBuffer (dev_, &model);
+ if (status != 0)
+ return status;
- output_buffers output;
- status = runNPU_sync (dev_, model_id_, &input_, &output);
- if (status < 0)
- return status;
+ /** copy only metadata as it's dummy model */
+ memcpy (model.addr, &meta, NPUBIN_META_SIZE);
- return compare_result (output);
- }
+ status = registerNPUmodel (dev_, &model, &model_id_);
+ cleanNPU_modelBuffer (dev_, &model);
- private:
- const uint64_t weight_size = 0x1000;
- const uint32_t segment_size = 0x1000;
-
- int prepare_model () {
- generic_buffer model;
- npubin_meta meta;
-
- memset (&meta, '\x00', sizeof(npubin_meta));
-
- meta.magiccode = NPUBIN_MAGICCODE | 0x3; /** npubinfmt v3 for TRIV2 */
- meta.program_size = 0;
- meta.weight_size = weight_size;
- meta.size = NPUBIN_META_SIZE + weight_size;
- meta.type = SMODEL_OPS_NPU;
-
- /** TRIV2 requires segment table */
- meta.segment_num = 3;
- for (uint32_t idx = 0; idx < meta.segment_num; idx++)
- meta.segment_size[idx] = segment_size;
-
- meta.weight_seg_idx = 0;
-
- meta.input_seg_num = 1;
- meta.input_seg_idx[0] = 1;
- meta.input_seg_off[0] = 0;
- meta.input_seg_dims[0][0] = 1;
- meta.input_seg_dims[0][1] = 1;
- meta.input_seg_dims[0][2] = 1;
- meta.input_seg_dims[0][3] = segment_size;
-
- meta.output_seg_num = 1;
- meta.output_seg_idx[0] = 1;
- meta.output_seg_off[0] = 0;
- meta.output_seg_dims[0][0] = 1;
- meta.output_seg_dims[0][1] = 1;
- meta.output_seg_dims[0][2] = 1;
- meta.output_seg_dims[0][3] = segment_size;
-
- model.type = BUFFER_MAPPED;
- model.size = meta.size;
- int status = allocNPU_modelBuffer (dev_, &model);
- if (status != 0)
- return status;
-
- /** copy only metadata as it's dummy model */
- memcpy (model.addr, &meta, NPUBIN_META_SIZE);
-
- status = registerNPUmodel (dev_, &model, &model_id_);
- cleanNPU_modelBuffer (dev_, &model);
+ return status;
+ }
- return status;
- }
+ int prepare_input () {
+ input_.num_buffers = 1;
+ input_.bufs[0].type = BUFFER_MAPPED;
+ input_.bufs[0].size = segment_size;
- int prepare_input () {
- input_.num_buffers = 1;
- input_.bufs[0].type = BUFFER_MAPPED;
- input_.bufs[0].size = segment_size;
+ return allocNPU_inputBuffers (dev_, &input_);
+ }
- return allocNPU_inputBuffers (dev_, &input_);
+ int compare_result (output_buffers &output) {
+ /** return success as it's dummy, but need to free output buffers */
+ for (uint32_t idx = 0; idx < output.num_buffers; idx++) {
+ generic_buffer *buffer = &output.bufs[idx];
+ if (buffer->addr != NULL)
+ free (buffer->addr);
}
- int compare_result (output_buffers & output) {
- /** return success as it's dummy, but need to free output buffers */
- for (uint32_t idx = 0; idx < output.num_buffers; idx++) {
- generic_buffer * buffer = &output.bufs[idx];
- if (buffer->addr != NULL)
- free (buffer->addr);
- }
-
- return 0;
- }
+ return 0;
+ }
- npudev_h dev_;
+ npudev_h dev_;
- uint32_t model_id_;
- input_buffers input_;
+ uint32_t model_id_;
+ input_buffers input_;
};
/** @brief apptest main */
int
-main (int argc, char **argv)
-{
-#if defined (ENABLE_EMUL)
+main (int argc, char **argv) {
+#if defined(ENABLE_EMUL)
TesterTRIV2 tester;
int status;
#include <ne_test_utils.h>
-#define NPU_TIMEOUT_MS 5000
+#define NPU_TIMEOUT_MS 5000
using namespace std;
/** @brief C++ class to describe how to use npu-engine library */
-class Tester : public UtilTRIV2
-{
- public:
- Tester () : model_id_ (0) {}
-
- /** @brief initilize the device handle */
- int init (std::string model_dir) {
- if (model_dir == "")
- return -EINVAL;
-
- std::string model_path = model_dir + "/model.tvn";
- npubin_meta * meta = getNPUmodel_metadata (model_path.c_str(), false);
- if (meta == nullptr)
- return -EINVAL;
-
- uint32_t tops = NPU_VERSION_TOPS (meta->npu_version);
- free (meta);
-
- int status = UtilTRIV2::init (tops);
- if (status != test_ret_success) {
- cerr << "Failed to initialize\n";
- return status;
- }
-
- return UtilTRIV2::loadModel (
- model_dir, &model_id_, NPU_PRIORITY_MID, NPU_TIMEOUT_MS);
+class Tester : public UtilTRIV2 {
+ public:
+ Tester () : model_id_ (0) {}
+
+ /** @brief initilize the device handle */
+ int init (std::string model_dir) {
+ if (model_dir == "")
+ return -EINVAL;
+
+ std::string model_path = model_dir + "/model.tvn";
+ npubin_meta *meta = getNPUmodel_metadata (model_path.c_str (), false);
+ if (meta == nullptr)
+ return -EINVAL;
+
+ uint32_t tops = NPU_VERSION_TOPS (meta->npu_version);
+ free (meta);
+
+ int status = UtilTRIV2::init (tops);
+ if (status != test_ret_success) {
+ cerr << "Failed to initialize\n";
+ return status;
}
- /** @brief run the inference */
- int run () {
- if (model_id_ == 0)
- return test_ret_failure;
+ return UtilTRIV2::loadModel (model_dir, &model_id_, NPU_PRIORITY_MID,
+ NPU_TIMEOUT_MS);
+ }
- int task_id = UtilTRIV2::run (model_id_);
- if (task_id < 0)
- return task_id;
+ /** @brief run the inference */
+ int run () {
+ if (model_id_ == 0)
+ return test_ret_failure;
- return wait () == 1 ? test_ret_success : test_ret_failure;
- }
+ int task_id = UtilTRIV2::run (model_id_);
+ if (task_id < 0)
+ return task_id;
+
+ return wait () == 1 ? test_ret_success : test_ret_failure;
+ }
- private:
- uint32_t model_id_;
+ private:
+ uint32_t model_id_;
};
/** @brief apptest main */
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
Tester tester;
- char * dir;
+ char *dir;
int status;
status = tester.parseArgs (argc, argv, &dir, "[model dir]");
#include <sys/types.h>
#include <dirent.h>
-#define NPU_TIMEOUT_MS 5000
+#define NPU_TIMEOUT_MS 5000
using namespace std;
/** @brief C++ class to describe how to use npu-engine library */
-class Tester : public UtilTRIV2
-{
- public:
- Tester () : model_id_ (0) {}
-
- /** @brief initilize the device handle */
- int init (std::string model_dir) {
- std::string model_path = model_dir + "/model.tvn";
- npubin_meta * meta = getNPUmodel_metadata (model_path.c_str(), false);
- if (meta == nullptr) {
- cerr << "Failed to get the model metadata\n";
- return -EINVAL;
- }
-
- uint32_t tops = NPU_VERSION_TOPS (meta->npu_version);
- free (meta);
+class Tester : public UtilTRIV2 {
+ public:
+ Tester () : model_id_ (0) {}
+
+ /** @brief initilize the device handle */
+ int init (std::string model_dir) {
+ std::string model_path = model_dir + "/model.tvn";
+ npubin_meta *meta = getNPUmodel_metadata (model_path.c_str (), false);
+ if (meta == nullptr) {
+ cerr << "Failed to get the model metadata\n";
+ return -EINVAL;
+ }
- int status = UtilTRIV2::init (tops);
- if (status != test_ret_success) {
- cerr << "Failed to initialize the device\n";
- return status;
- }
+ uint32_t tops = NPU_VERSION_TOPS (meta->npu_version);
+ free (meta);
- return UtilTRIV2::loadModel (
- model_dir, &model_id_, NPU_PRIORITY_MID, NPU_TIMEOUT_MS);
+ int status = UtilTRIV2::init (tops);
+ if (status != test_ret_success) {
+ cerr << "Failed to initialize the device\n";
+ return status;
}
- /** @brief run the inference */
- int run () {
- if (model_id_ == 0)
- return test_ret_failure;
+ return UtilTRIV2::loadModel (model_dir, &model_id_, NPU_PRIORITY_MID,
+ NPU_TIMEOUT_MS);
+ }
- int task_id = UtilTRIV2::run (model_id_);
- if (task_id < 0)
- return task_id;
+ /** @brief run the inference */
+ int run () {
+ if (model_id_ == 0)
+ return test_ret_failure;
- return wait () == 1 ? test_ret_success : test_ret_failure;
- }
+ int task_id = UtilTRIV2::run (model_id_);
+ if (task_id < 0)
+ return task_id;
+
+ return wait () == 1 ? test_ret_success : test_ret_failure;
+ }
- private:
- uint32_t model_id_;
+ private:
+ uint32_t model_id_;
};
/** @brief apptest main */
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
Tester tester;
- char * dir_str;
+ char *dir_str;
int status;
std::ios_base::fmtflags f (cerr.flags ());
/** @brief c++ class to describe how to use npu-engine library */
class TesterTRIV2 {
- public:
- TesterTRIV2 () : dev_ (nullptr), dev_tmp_ (nullptr),
- meta_ (nullptr), model_id_ (0) {
- memset (&input_, '\x00', sizeof (input_buffers));
- memset (&input_tmp_, '\x00', sizeof (input_buffers));
- }
-
- ~TesterTRIV2 () {
- /** release resources */
- if (dev_ != nullptr) {
- if (model_id_ > 0)
- unregisterNPUmodel (dev_, model_id_);
+ public:
+ TesterTRIV2 ()
+ : dev_ (nullptr), dev_tmp_ (nullptr), meta_ (nullptr), model_id_ (0) {
+ memset (&input_, '\x00', sizeof (input_buffers));
+ memset (&input_tmp_, '\x00', sizeof (input_buffers));
+ }
- if (meta_ != nullptr)
- free (meta_);
+ ~TesterTRIV2 () {
+ /** release resources */
+ if (dev_ != nullptr) {
+ if (model_id_ > 0)
+ unregisterNPUmodel (dev_, model_id_);
- putNPUdevice (dev_);
- }
+ if (meta_ != nullptr)
+ free (meta_);
- if (dev_tmp_ != nullptr) {
- for (uint32_t idx = 0; idx < input_tmp_.num_buffers; idx++) {
- cleanNPU_inputBuffer (dev_tmp_, &input_tmp_.bufs[idx]);
- }
+ putNPUdevice (dev_);
+ }
- putNPUdevice (dev_tmp_);
+ if (dev_tmp_ != nullptr) {
+ for (uint32_t idx = 0; idx < input_tmp_.num_buffers; idx++) {
+ cleanNPU_inputBuffer (dev_tmp_, &input_tmp_.bufs[idx]);
}
- }
- /** @brief initilize the device handle */
- int init (const std::string dir) {
- dir_ = dir;
- return getNPUdeviceByTypeAny (&dev_, TRIV2_TYPE, 2);
+ putNPUdevice (dev_tmp_);
}
+ }
- /** @brief run the inference (with dummy data) */
- int run () {
- int status = prepare_model ();
- if (status != 0)
- return status;
+ /** @brief initilize the device handle */
+ int init (const std::string dir) {
+ dir_ = dir;
+ return getNPUdeviceByTypeAny (&dev_, TRIV2_TYPE, 2);
+ }
- status = prepare_input ();
- if (status != 0)
- return status;
+ /** @brief run the inference (with dummy data) */
+ int run () {
+ int status = prepare_model ();
+ if (status != 0)
+ return status;
- status = set_data_info ();
- if (status != 0)
- return status;
+ status = prepare_input ();
+ if (status != 0)
+ return status;
- output_buffers output;
- status = runNPU_sync (dev_, model_id_, &input_, &output);
- if (status < 0)
- return status;
+ status = set_data_info ();
+ if (status != 0)
+ return status;
- return compare_result (output);
- }
+ output_buffers output;
+ status = runNPU_sync (dev_, model_id_, &input_, &output);
+ if (status < 0)
+ return status;
- private:
- int prepare_model () {
- generic_buffer model;
- std::string model_path;
- uint32_t i;
+ return compare_result (output);
+ }
- model_path = dir_ + "/" + NPU_MODEL_NAME;
+ private:
+ int prepare_model () {
+ generic_buffer model;
+ std::string model_path;
+ uint32_t i;
- meta_ = getNPUmodel_metadata (model_path.c_str(), false);
- if (meta_ == nullptr)
- return -EINVAL;
+ model_path = dir_ + "/" + NPU_MODEL_NAME;
- if (NPUBIN_VERSION (meta_->magiccode) != 3)
- return -EINVAL;
+ meta_ = getNPUmodel_metadata (model_path.c_str (), false);
+ if (meta_ == nullptr)
+ return -EINVAL;
- for (i = 0; i < meta_->segment_num; ++i) {
- if (meta_->input_seg_off[i] != 0)
- return test_ret::test_ret_skipped_not_compatible;
- }
+ if (NPUBIN_VERSION (meta_->magiccode) != 3)
+ return -EINVAL;
- model.type = BUFFER_FILE;
- model.size = meta_->size;
- model.filepath = model_path.c_str();
+ for (i = 0; i < meta_->segment_num; ++i) {
+ if (meta_->input_seg_off[i] != 0)
+ return test_ret::test_ret_skipped_not_compatible;
+ }
- int status = allocNPU_modelBuffer (dev_, &model);
- if (status != 0)
- return status;
+ model.type = BUFFER_FILE;
+ model.size = meta_->size;
+ model.filepath = model_path.c_str ();
+
+ int status = allocNPU_modelBuffer (dev_, &model);
+ if (status != 0)
+ return status;
- status = registerNPUmodel (dev_, &model, &model_id_);
- cleanNPU_modelBuffer (dev_, &model);
+ status = registerNPUmodel (dev_, &model, &model_id_);
+ cleanNPU_modelBuffer (dev_, &model);
+ return status;
+ }
+
+ int prepare_input () {
+ /** let's emulate external dmabuf using 8TOPS TRIV2 device */
+ int status = getNPUdeviceByTypeAny (&dev_tmp_, TRIV2_TYPE, 8);
+ if (status != 0)
return status;
- }
- int prepare_input () {
- /** let's emulate external dmabuf using 8TOPS TRIV2 device */
- int status = getNPUdeviceByTypeAny (&dev_tmp_, TRIV2_TYPE, 8);
+ input_.num_buffers = meta_->input_seg_num;
+ input_tmp_.num_buffers = meta_->input_seg_num;
+ for (uint32_t idx = 0; idx < meta_->input_seg_num; idx++) {
+ uint32_t input_seg_idx = meta_->input_seg_idx[idx];
+ uint32_t input_seg_size = meta_->segment_size[input_seg_idx];
+ off_t input_seg_off = meta_->input_seg_off[idx];
+
+ std::string input_path;
+ input_path = dir_ + "/input_fmap_" + std::to_string (idx) + ".bin";
+
+ input_tmp_.bufs[idx].type = BUFFER_MAPPED;
+ input_tmp_.bufs[idx].size = input_seg_size;
+ status = allocNPU_inputBuffer (dev_tmp_, &input_tmp_.bufs[idx]);
if (status != 0)
return status;
- input_.num_buffers = meta_->input_seg_num;
- input_tmp_.num_buffers = meta_->input_seg_num;
- for (uint32_t idx = 0; idx < meta_->input_seg_num; idx++) {
- uint32_t input_seg_idx = meta_->input_seg_idx[idx];
- uint32_t input_seg_size = meta_->segment_size[input_seg_idx];
- off_t input_seg_off = meta_->input_seg_off[idx];
-
- std::string input_path;
- input_path = dir_ + "/input_fmap_" + std::to_string (idx) + ".bin";
-
- input_tmp_.bufs[idx].type = BUFFER_MAPPED;
- input_tmp_.bufs[idx].size = input_seg_size;
- status = allocNPU_inputBuffer (dev_tmp_, &input_tmp_.bufs[idx]);
- if (status != 0)
- return status;
-
- std::ifstream ifs (input_path, std::ios::binary);
- if (!ifs.good ())
- return -EINVAL;
-
- ifs.seekg (0, std::ios::end);
- size_t size = ifs.tellg ();
-
- ifs.seekg (0);
- ifs.read (static_cast<char *>(input_tmp_.bufs[idx].addr) + input_seg_off, size);
- ifs.close ();
-
- input_.bufs[idx].type = BUFFER_DMABUF;
- input_.bufs[idx].size = size;
- input_.bufs[idx].dmabuf = input_tmp_.bufs[idx].dmabuf;
- input_.bufs[idx].offset = input_seg_off;
- }
+ std::ifstream ifs (input_path, std::ios::binary);
+ if (!ifs.good ())
+ return -EINVAL;
+
+ ifs.seekg (0, std::ios::end);
+ size_t size = ifs.tellg ();
+
+ ifs.seekg (0);
+ ifs.read (static_cast<char *> (input_tmp_.bufs[idx].addr) + input_seg_off,
+ size);
+ ifs.close ();
- return 0;
+ input_.bufs[idx].type = BUFFER_DMABUF;
+ input_.bufs[idx].size = size;
+ input_.bufs[idx].dmabuf = input_tmp_.bufs[idx].dmabuf;
+ input_.bufs[idx].offset = input_seg_off;
}
- int set_data_info () {
- tensors_data_info info_in;
- tensors_data_info info_out;
+ return 0;
+ }
- /* No data manipulation & quantization in this test */
+ int set_data_info () {
+ tensors_data_info info_in;
+ tensors_data_info info_out;
- info_in.num_info = meta_->input_seg_num;
- for (uint32_t idx = 0; idx < info_in.num_info; idx++) {
- info_in.info[idx].layout = DATA_LAYOUT_TRIV2;
- info_in.info[idx].type = DATA_TYPE_QASYMM8;
- }
+ /* No data manipulation & quantization in this test */
- info_out.num_info = meta_->output_seg_num;
- for (uint32_t idx = 0; idx < info_out.num_info; idx++) {
- info_out.info[idx].layout = DATA_LAYOUT_TRIV2;
- info_out.info[idx].type = DATA_TYPE_QASYMM8;
- }
+ info_in.num_info = meta_->input_seg_num;
+ for (uint32_t idx = 0; idx < info_in.num_info; idx++) {
+ info_in.info[idx].layout = DATA_LAYOUT_TRIV2;
+ info_in.info[idx].type = DATA_TYPE_QASYMM8;
+ }
- return setNPU_dataInfo (dev_, model_id_, &info_in, &info_out);
+ info_out.num_info = meta_->output_seg_num;
+ for (uint32_t idx = 0; idx < info_out.num_info; idx++) {
+ info_out.info[idx].layout = DATA_LAYOUT_TRIV2;
+ info_out.info[idx].type = DATA_TYPE_QASYMM8;
}
- int compare_result (output_buffers & output) {
- int err = 0;
+ return setNPU_dataInfo (dev_, model_id_, &info_in, &info_out);
+ }
- for (uint32_t idx = 0; idx < output.num_buffers; idx++) {
- char * output_data = static_cast<char*> (output.bufs[idx].addr);
- off_t output_size = output.bufs[idx].size;
- std::string golden_path;
+ int compare_result (output_buffers &output) {
+ int err = 0;
- golden_path = dir_ + "/output_fmap_" + std::to_string (idx) + ".bin";
+ for (uint32_t idx = 0; idx < output.num_buffers; idx++) {
+ char *output_data = static_cast<char *> (output.bufs[idx].addr);
+ off_t output_size = output.bufs[idx].size;
+ std::string golden_path;
- err = compare_data (golden_path.c_str(), output_data, output_size);
+ golden_path = dir_ + "/output_fmap_" + std::to_string (idx) + ".bin";
- assert (output.bufs[idx].type == BUFFER_MAPPED);
- free (output_data);
+ err = compare_data (golden_path.c_str (), output_data, output_size);
- if (err != 0)
- break;
- }
+ assert (output.bufs[idx].type == BUFFER_MAPPED);
+ free (output_data);
- return err;
+ if (err != 0)
+ break;
}
- std::string dir_;
+ return err;
+ }
+
+ std::string dir_;
- npudev_h dev_;
- npudev_h dev_tmp_;
+ npudev_h dev_;
+ npudev_h dev_tmp_;
- npubin_meta *meta_;
- uint32_t model_id_;
- input_buffers input_;
- input_buffers input_tmp_;
+ npubin_meta *meta_;
+ uint32_t model_id_;
+ input_buffers input_;
+ input_buffers input_tmp_;
};
/** @brief apptest main */
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
TesterTRIV2 tester;
int status;
using namespace std;
-ifstream::pos_type filesize (string filename)
-{
- ifstream in(filename, ifstream::ate | ifstream::binary);
- return in.tellg();
+ifstream::pos_type
+filesize (string filename) {
+ ifstream in (filename, ifstream::ate | ifstream::binary);
+ return in.tellg ();
}
/** @brief apptest main */
-int main (int argc, char **argv)
-{
+int
+main (int argc, char **argv) {
if (argc != 2) {
cerr << "No model directory provided. Skip this test\n";
return 0;
/* here, we assume a single input/output tensor */
in.num_info = 1;
- in.info[0].layout = DATA_LAYOUT_MODEL; /* model-expected layout */
- in.info[0].type = DATA_TYPE_MODEL; /* model-expected type */
+ in.info[0].layout = DATA_LAYOUT_MODEL; /* model-expected layout */
+ in.info[0].type = DATA_TYPE_MODEL; /* model-expected type */
out.num_info = 1;
- out.info[0].layout = DATA_LAYOUT_MODEL; /* model-expected layout */
- out.info[0].type = DATA_TYPE_MODEL; /* model-expected type */
+ out.info[0].layout = DATA_LAYOUT_MODEL; /* model-expected layout */
+ out.info[0].type = DATA_TYPE_MODEL; /* model-expected type */
if (setNPU_dataInfo (dev, modelid, &in, &out) < 0) {
cerr << "Fail to set data info\n";
* 6) Check the output buffer
*/
for (uint32_t idx = 0; idx < output.num_buffers; idx++) {
- generic_buffer * buffer = &output.bufs[idx];
+ generic_buffer *buffer = &output.bufs[idx];
if (buffer->addr != NULL) {
/* DO SOMETHING */
free (buffer->addr);
#include <ne_test_utils.h>
-#define NPU_TIMEOUT_MS 5000
-#define MID_MODEL_NAME "MOBILENET_V1"
+#define NPU_TIMEOUT_MS 5000
+#define MID_MODEL_NAME "MOBILENET_V1"
#define HIGH_MODEL_NAME "MAX_POOL_2D_000"
using namespace std;
/** @brief C++ class to describe how to use npu-engine library */
-class Tester : public UtilTRIV2
-{
- public:
- Tester () : mid_model_id_ (0), high_model_id_ (0) {}
-
- /** @brief initilize the device handle */
- int init (std::string models_dir) {
- int status = UtilTRIV2::init ();
- if (status != test_ret_success) {
- cerr << "Failed to initialize\n";
- return status;
- }
-
- status = UtilTRIV2::loadModel (models_dir + "/" + MID_MODEL_NAME,
- &mid_model_id_, NPU_PRIORITY_MID, NPU_TIMEOUT_MS);
- if (status != test_ret_success) {
- cerr << "Failed to find a model: " << MID_MODEL_NAME << "\n";
- return status;
- }
+class Tester : public UtilTRIV2 {
+ public:
+ Tester () : mid_model_id_ (0), high_model_id_ (0) {}
+
+ /** @brief initilize the device handle */
+ int init (std::string models_dir) {
+ int status = UtilTRIV2::init ();
+ if (status != test_ret_success) {
+ cerr << "Failed to initialize\n";
+ return status;
+ }
- status = UtilTRIV2::loadModel (models_dir + "/" + HIGH_MODEL_NAME,
- &high_model_id_, NPU_PRIORITY_HIGH, NPU_TIMEOUT_MS);
- if (status != test_ret_success) {
- cerr << "Failed to find a model: " << HIGH_MODEL_NAME << "\n";
- return status;
- }
+ status =
+ UtilTRIV2::loadModel (models_dir + "/" + MID_MODEL_NAME, &mid_model_id_,
+ NPU_PRIORITY_MID, NPU_TIMEOUT_MS);
+ if (status != test_ret_success) {
+ cerr << "Failed to find a model: " << MID_MODEL_NAME << "\n";
+ return status;
+ }
- return 0;
+ status = UtilTRIV2::loadModel (models_dir + "/" + HIGH_MODEL_NAME,
+ &high_model_id_, NPU_PRIORITY_HIGH,
+ NPU_TIMEOUT_MS);
+ if (status != test_ret_success) {
+ cerr << "Failed to find a model: " << HIGH_MODEL_NAME << "\n";
+ return status;
}
- /** @brief run the inference */
- int run () {
- if (mid_model_id_ == 0 || high_model_id_ == 0)
- return test_ret_failure;
+ return 0;
+ }
- int mid_task_id = UtilTRIV2::run (mid_model_id_);
- if (mid_task_id < 0)
- return mid_task_id;
+ /** @brief run the inference */
+ int run () {
+ if (mid_model_id_ == 0 || high_model_id_ == 0)
+ return test_ret_failure;
- int high_task_id = UtilTRIV2::run (high_model_id_);
- if (high_task_id < 0)
- return high_task_id;
+ int mid_task_id = UtilTRIV2::run (mid_model_id_);
+ if (mid_task_id < 0)
+ return mid_task_id;
- int result = wait () == 2 ? test_ret_success : test_ret_failure;
+ int high_task_id = UtilTRIV2::run (high_model_id_);
+ if (high_task_id < 0)
+ return high_task_id;
- if (result == test_ret_success) {
- int64_t mid_last_seq = get_last_sequence (mid_task_id);
- int64_t high_last_seq = get_last_sequence (high_task_id);
+ int result = wait () == 2 ? test_ret_success : test_ret_failure;
- /** The expected execution order: mid --> high --> mid */
- if (high_last_seq > mid_last_seq) {
- cerr << "Unable to confirm the evidence of preemption\n";
- result = test_ret_failure;
- }
- }
+ if (result == test_ret_success) {
+ int64_t mid_last_seq = get_last_sequence (mid_task_id);
+ int64_t high_last_seq = get_last_sequence (high_task_id);
- return result;
+ /** The expected execution order: mid --> high --> mid */
+ if (high_last_seq > mid_last_seq) {
+ cerr << "Unable to confirm the evidence of preemption\n";
+ result = test_ret_failure;
+ }
}
- /** @brief get the last sequence */
- int64_t get_last_sequence (int task_id) {
- npu_profile profile;
- int64_t sequence = -1;
- int status;
-
- status = UtilTRIV2::getProfile (task_id, &profile);
- if (status == 0) {
- if (profile.layers != nullptr)
- sequence = profile.layers[profile.num_layers - 1].visa_exec_seq;
-
- putNPU_profile (&profile);
- } else {
- cerr << "Failed to get profile: " << status << "\n";
- sequence = status;
- }
+ return result;
+ }
- return sequence;
+ /** @brief get the last sequence */
+ int64_t get_last_sequence (int task_id) {
+ npu_profile profile;
+ int64_t sequence = -1;
+ int status;
+
+ status = UtilTRIV2::getProfile (task_id, &profile);
+ if (status == 0) {
+ if (profile.layers != nullptr)
+ sequence = profile.layers[profile.num_layers - 1].visa_exec_seq;
+
+ putNPU_profile (&profile);
+ } else {
+ cerr << "Failed to get profile: " << status << "\n";
+ sequence = status;
}
- private:
- uint32_t mid_model_id_;
- uint32_t high_model_id_;
+ return sequence;
+ }
+
+ private:
+ uint32_t mid_model_id_;
+ uint32_t high_model_id_;
};
/** @brief apptest main */
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
Tester tester;
- char * dir;
+ char *dir;
int status;
status = tester.parseArgs (argc, argv, &dir, "[models' dir]");
#include <ne_test_utils.h>
-#define NPU_TIMEOUT_MS 5000
+#define NPU_TIMEOUT_MS 5000
using namespace std;
/** @brief C++ class to describe how to use npu-engine library */
-class Tester : public UtilTRIV2
-{
- public:
- Tester () : model_id_ (0) {}
-
- /** @brief initilize the device handle */
- int init (std::string model_dir) {
- int status = UtilTRIV2::init ();
- if (status != test_ret_success) {
- cerr << "Failed to initialize\n";
- return status;
- }
-
- return UtilTRIV2::loadModel (
- model_dir, &model_id_, NPU_PRIORITY_MID, NPU_TIMEOUT_MS);
- }
-
- /** @brief run the inference */
- int run () {
- if (model_id_ == 0)
- return test_ret_failure;
-
- int task_id = UtilTRIV2::run (model_id_);
- if (task_id < 0)
- return task_id;
-
- int result = wait () == 1 ? test_ret_success : test_ret_failure;
-
- print_profile (task_id);
-
- return result;
+class Tester : public UtilTRIV2 {
+ public:
+ Tester () : model_id_ (0) {}
+
+ /** @brief initilize the device handle */
+ int init (std::string model_dir) {
+ int status = UtilTRIV2::init ();
+ if (status != test_ret_success) {
+ cerr << "Failed to initialize\n";
+ return status;
}
- /** @brief print profile */
- void print_profile (int task_id) {
- npu_profile profile;
- int status = UtilTRIV2::getProfile (task_id, &profile);
- if (status == 0) {
- if (profile.prof_path != nullptr)
- cerr << "Profile Path : " << profile.prof_path << "\n";
- cerr << "Total System Cycles : " << profile.total_system_cycles << "\n";
- if (profile.dram_input_footprint > 0)
- cerr << "DRAM Input Footprint (KB) : " << (profile.dram_input_footprint >> 10) << "\n";
- if (profile.dram_output_footprint > 0)
- cerr << "DRAM Output Footprint (KB) : " << (profile.dram_output_footprint >> 10) << "\n";
-
- if (profile.layers != nullptr) {
- cerr << "\n";
- for (uint32_t i = 0; i < profile.num_layers; i++) {
- cerr << "[" << i << "] " << profile.layers[i].name << "\n";
- if (profile.layers[i].running_cycles > 0)
- cerr << "\tRunning Cycles : " << profile.layers[i].running_cycles << "\n";
- if (profile.layers[i].start_cycles > 0)
- cerr << "\tStart Cycles : " << profile.layers[i].start_cycles << "\n";
- if (profile.layers[i].end_cycles > 0)
- cerr << "\tEnd Cycles : " << profile.layers[i].end_cycles << "\n";
- if (profile.layers[i].dram_read_bytes > 0)
- cerr << "\tDRAM Read (KB) : " << (profile.layers[i].dram_read_bytes >> 10) << "\n";
- if (profile.layers[i].dram_write_bytes > 0)
- cerr << "\tDRAM Write (KB) : " << (profile.layers[i].dram_write_bytes >> 10) << "\n";
- if (profile.layers[i].sram_read_bytes > 0)
- cerr << "\tSRAM Read (KB) : " << (profile.layers[i].sram_read_bytes >> 10) << "\n";
- if (profile.layers[i].sram_write_bytes > 0)
- cerr << "\tSRAM Write (KB) : " << (profile.layers[i].sram_write_bytes >> 10) << "\n";
- if (profile.layers[i].visa_exec_seq > 0)
- cerr << "\tvISA Exec Seq : " << profile.layers[i].visa_exec_seq << "\n";
- }
+ return UtilTRIV2::loadModel (model_dir, &model_id_, NPU_PRIORITY_MID,
+ NPU_TIMEOUT_MS);
+ }
+
+ /** @brief run the inference */
+ int run () {
+ if (model_id_ == 0)
+ return test_ret_failure;
+
+ int task_id = UtilTRIV2::run (model_id_);
+ if (task_id < 0)
+ return task_id;
+
+ int result = wait () == 1 ? test_ret_success : test_ret_failure;
+
+ print_profile (task_id);
+
+ return result;
+ }
+
+ /** @brief print profile */
+ void print_profile (int task_id) {
+ npu_profile profile;
+ int status = UtilTRIV2::getProfile (task_id, &profile);
+ if (status == 0) {
+ if (profile.prof_path != nullptr)
+ cerr << "Profile Path : " << profile.prof_path << "\n";
+ cerr << "Total System Cycles : " << profile.total_system_cycles << "\n";
+ if (profile.dram_input_footprint > 0)
+ cerr << "DRAM Input Footprint (KB) : "
+ << (profile.dram_input_footprint >> 10) << "\n";
+ if (profile.dram_output_footprint > 0)
+ cerr << "DRAM Output Footprint (KB) : "
+ << (profile.dram_output_footprint >> 10) << "\n";
+
+ if (profile.layers != nullptr) {
+ cerr << "\n";
+ for (uint32_t i = 0; i < profile.num_layers; i++) {
+ cerr << "[" << i << "] " << profile.layers[i].name << "\n";
+ if (profile.layers[i].running_cycles > 0)
+ cerr << "\tRunning Cycles : " << profile.layers[i].running_cycles
+ << "\n";
+ if (profile.layers[i].start_cycles > 0)
+ cerr << "\tStart Cycles : " << profile.layers[i].start_cycles
+ << "\n";
+ if (profile.layers[i].end_cycles > 0)
+ cerr << "\tEnd Cycles : " << profile.layers[i].end_cycles << "\n";
+ if (profile.layers[i].dram_read_bytes > 0)
+ cerr << "\tDRAM Read (KB) : "
+ << (profile.layers[i].dram_read_bytes >> 10) << "\n";
+ if (profile.layers[i].dram_write_bytes > 0)
+ cerr << "\tDRAM Write (KB) : "
+ << (profile.layers[i].dram_write_bytes >> 10) << "\n";
+ if (profile.layers[i].sram_read_bytes > 0)
+ cerr << "\tSRAM Read (KB) : "
+ << (profile.layers[i].sram_read_bytes >> 10) << "\n";
+ if (profile.layers[i].sram_write_bytes > 0)
+ cerr << "\tSRAM Write (KB) : "
+ << (profile.layers[i].sram_write_bytes >> 10) << "\n";
+ if (profile.layers[i].visa_exec_seq > 0)
+ cerr << "\tvISA Exec Seq : " << profile.layers[i].visa_exec_seq
+ << "\n";
}
-
- putNPU_profile (&profile);
- } else {
- cerr << "Failed to get profile: " << status << "\n";
}
+
+ putNPU_profile (&profile);
+ } else {
+ cerr << "Failed to get profile: " << status << "\n";
}
+ }
- private:
- uint32_t model_id_;
+ private:
+ uint32_t model_id_;
};
/** @brief apptest main */
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
Tester tester;
- char * dir;
+ char *dir;
int status;
status = tester.parseArgs (argc, argv, &dir, "[model dir]");
/** ./include/typedef.h */
#define TRIV2_TYPE NPUCOND_TRIV2_CONN_SOCIP
#define NPU_MODEL_NAME ("model.tvn")
-#define DEFAULT_INTERVAL (30000) /* 30 ms */
-#define DEFAULT_LOOPS (30)
+#define DEFAULT_INTERVAL (30000) /* 30 ms */
+#define DEFAULT_LOOPS (30)
/** @brief c++ class to emulate third-party HW to feed recurring input to the NPU */
class ThirdPartyHW {
- public:
- ThirdPartyHW () {
- dev_ = nullptr;
+ public:
+ ThirdPartyHW () {
+ dev_ = nullptr;
- memset ((void *) &input_, '\x00', sizeof (input_));
- memset ((void *) &output_, '\x00', sizeof (output_));
- }
+ memset ((void *) &input_, '\x00', sizeof (input_));
+ memset ((void *) &output_, '\x00', sizeof (output_));
+ }
- ~ThirdPartyHW () {
- if (dev_ != nullptr) {
- cleanNPU_genericBuffers (dev_, &input_);
- cleanNPU_genericBuffers (dev_, &output_);
- putNPUdevice (dev_);
- }
+ ~ThirdPartyHW () {
+ if (dev_ != nullptr) {
+ cleanNPU_genericBuffers (dev_, &input_);
+ cleanNPU_genericBuffers (dev_, &output_);
+ putNPUdevice (dev_);
}
+ }
- int init (const npubin_meta *meta) {
- /** let's emulate an external dmabuf using TRIV2 */
- int status = getNPUdeviceByTypeAny (&dev_, TRIV2_TYPE, default_tops);
- if (status != 0)
- return status;
-
- status = alloc_inputs (meta);
- if (status != 0)
- return status;
+ int init (const npubin_meta *meta) {
+ /** let's emulate an external dmabuf using TRIV2 */
+ int status = getNPUdeviceByTypeAny (&dev_, TRIV2_TYPE, default_tops);
+ if (status != 0)
+ return status;
- status = alloc_outputs (meta);
- if (status != 0)
- return status;
+ status = alloc_inputs (meta);
+ if (status != 0)
+ return status;
- return 0;
- }
+ status = alloc_outputs (meta);
+ if (status != 0)
+ return status;
- int get_input_dmabuf (uint32_t idx) {
- return input_.bufs[idx].dmabuf;
- }
+ return 0;
+ }
- int get_output_dmabuf (uint32_t idx) {
- return output_.bufs[idx].dmabuf;
- }
+ int get_input_dmabuf (uint32_t idx) { return input_.bufs[idx].dmabuf; }
- private:
- /** we don't consider the case that input/output segments are overlapped */
- int alloc_inputs (const npubin_meta *meta) {
- for (uint32_t idx = 0; idx < meta->input_seg_num; idx++) {
- uint32_t seg_idx = meta->input_seg_idx[idx];
- uint32_t seg_size = meta->segment_size[seg_idx];
+ int get_output_dmabuf (uint32_t idx) { return output_.bufs[idx].dmabuf; }
- input_.bufs[idx].type = BUFFER_MAPPED;
- input_.bufs[idx].size = seg_size;
- }
+ private:
+ /** we don't consider the case that input/output segments are overlapped */
+ int alloc_inputs (const npubin_meta *meta) {
+ for (uint32_t idx = 0; idx < meta->input_seg_num; idx++) {
+ uint32_t seg_idx = meta->input_seg_idx[idx];
+ uint32_t seg_size = meta->segment_size[seg_idx];
- input_.num_buffers = meta->input_seg_num;
- return allocNPU_genericBuffers (dev_, &input_);
+ input_.bufs[idx].type = BUFFER_MAPPED;
+ input_.bufs[idx].size = seg_size;
}
- int alloc_outputs (const npubin_meta *meta) {
- for (uint32_t idx = 0; idx < meta->output_seg_num; idx++) {
- uint32_t seg_idx = meta->output_seg_idx[idx];
- uint32_t seg_size = meta->segment_size[seg_idx];
+ input_.num_buffers = meta->input_seg_num;
+ return allocNPU_genericBuffers (dev_, &input_);
+ }
- output_.bufs[idx].type = BUFFER_MAPPED;
- output_.bufs[idx].size = seg_size;
- }
+ int alloc_outputs (const npubin_meta *meta) {
+ for (uint32_t idx = 0; idx < meta->output_seg_num; idx++) {
+ uint32_t seg_idx = meta->output_seg_idx[idx];
+ uint32_t seg_size = meta->segment_size[seg_idx];
- output_.num_buffers = meta->output_seg_num;
- return allocNPU_genericBuffers (dev_, &output_);
+ output_.bufs[idx].type = BUFFER_MAPPED;
+ output_.bufs[idx].size = seg_size;
}
- npudev_h dev_;
- input_buffers input_;
- output_buffers output_;
+ output_.num_buffers = meta->output_seg_num;
+ return allocNPU_genericBuffers (dev_, &output_);
+ }
+
+ npudev_h dev_;
+ input_buffers input_;
+ output_buffers output_;
};
/** @brief c++ class to describe how to use npu-engine library */
class TesterTRIV2 {
- public:
- TesterTRIV2 () : dev_ (nullptr), meta_ (nullptr), model_id_ (0) {
- loop_ = DEFAULT_LOOPS;
- }
+ public:
+ TesterTRIV2 () : dev_ (nullptr), meta_ (nullptr), model_id_ (0) {
+ loop_ = DEFAULT_LOOPS;
+ }
- ~TesterTRIV2 () {
- /** release resources */
- if (dev_ != nullptr) {
- if (model_id_ > 0)
- unregisterNPUmodel (dev_, model_id_);
+ ~TesterTRIV2 () {
+ /** release resources */
+ if (dev_ != nullptr) {
+ if (model_id_ > 0)
+ unregisterNPUmodel (dev_, model_id_);
- if (meta_ != nullptr)
- free (meta_);
+ if (meta_ != nullptr)
+ free (meta_);
- putNPUdevice (dev_);
- }
+ putNPUdevice (dev_);
}
+ }
- void set_loop (int loop) {
- loop_ = loop;
- }
+ void set_loop (int loop) { loop_ = loop; }
- /** @brief initilize the device handle */
- int init (const std::string dir, const std::string hw_dev) {
- struct stat stat_buf;
- if (stat (hw_dev.c_str(), &stat_buf) != 0)
- return -EEXIST;
+ /** @brief initilize the device handle */
+ int init (const std::string dir, const std::string hw_dev) {
+ struct stat stat_buf;
+ if (stat (hw_dev.c_str (), &stat_buf) != 0)
+ return -EEXIST;
- dir_ = dir;
- hw_dev_ = hw_dev;
+ dir_ = dir;
+ hw_dev_ = hw_dev;
- return getNPUdeviceByTypeAny (&dev_, TRIV2_TYPE, default_tops);
- }
+ return getNPUdeviceByTypeAny (&dev_, TRIV2_TYPE, default_tops);
+ }
- void trigger_internal_task (int32_t *ret) {
- const char dev_node_triv2[] = "/dev/triv2-0";
- struct stat stat_buf;
- dev_t rdev;
- int32_t fd;
- uint32_t i;
+ void trigger_internal_task (int32_t *ret) {
+ const char dev_node_triv2[] = "/dev/triv2-0";
+ struct stat stat_buf;
+ dev_t rdev;
+ int32_t fd;
+ uint32_t i;
- if (stat (hw_dev_.c_str(), &stat_buf) != 0) {
- *ret = -EEXIST;
- return;
- }
+ if (stat (hw_dev_.c_str (), &stat_buf) != 0) {
+ *ret = -EEXIST;
+ return;
+ }
- rdev = stat_buf.st_rdev;
+ rdev = stat_buf.st_rdev;
- fd = open (dev_node_triv2, O_RDWR);
- if (fd < 0) {
- *ret = -ENODEV;
- return;
- }
+ fd = open (dev_node_triv2, O_RDWR);
+ if (fd < 0) {
+ *ret = -ENODEV;
+ return;
+ }
- for (i = 0; i < loop_; ++i) {
- fprintf (stdout, "[%2u/%2u].......", i + 1, loop_);
- usleep(DEFAULT_INTERVAL);
- *ret = ioctl (fd, TRINITY_IOCTL_RUN_INTERNAL_REQ, &rdev);
- if (*ret != 0) {
- fprintf (stdout, "failed\n");
- close(fd);
- return;
- }
- fprintf (stdout, "done\n");
+ for (i = 0; i < loop_; ++i) {
+ fprintf (stdout, "[%2u/%2u].......", i + 1, loop_);
+ usleep (DEFAULT_INTERVAL);
+ *ret = ioctl (fd, TRINITY_IOCTL_RUN_INTERNAL_REQ, &rdev);
+ if (*ret != 0) {
+ fprintf (stdout, "failed\n");
+ close (fd);
+ return;
}
-
- close(fd);
+ fprintf (stdout, "done\n");
}
- /** @brief run the inference (with dummy data) */
- int run () {
- int status = prepare_model ();
- if (status != 0)
- return status;
+ close (fd);
+ }
- status = hw_.init (meta_);
- if (status != 0)
- return status;
+ /** @brief run the inference (with dummy data) */
+ int run () {
+ int status = prepare_model ();
+ if (status != 0)
+ return status;
- status = set_data_info ();
- if (status != 0)
- return status;
+ status = hw_.init (meta_);
+ if (status != 0)
+ return status;
- int id = runNPU_internalInput (dev_, model_id_, NPUINPUT_HW_RECURRING,
- hw_dev_.c_str());
- if (id < 0)
- return id;
+ status = set_data_info ();
+ if (status != 0)
+ return status;
- std::thread t1(&TesterTRIV2::trigger_internal_task, this, &status);
- t1.join();
+ int id = runNPU_internalInput (dev_, model_id_, NPUINPUT_HW_RECURRING,
+ hw_dev_.c_str ());
+ if (id < 0)
+ return id;
- if (status < 0) {
- stopNPU_internalInput (dev_, id);
- return -1;
- }
+ std::thread t1 (&TesterTRIV2::trigger_internal_task, this, &status);
+ t1.join ();
- return stopNPU_internalInput (dev_, id);
+ if (status < 0) {
+ stopNPU_internalInput (dev_, id);
+ return -1;
}
- private:
- int prepare_model () {
- generic_buffer model;
- std::string model_path;
-
- model_path = dir_ + "/" + NPU_MODEL_NAME;
+ return stopNPU_internalInput (dev_, id);
+ }
- meta_ = getNPUmodel_metadata (model_path.c_str(), false);
- if (meta_ == nullptr)
- return -EINVAL;
+ private:
+ int prepare_model () {
+ generic_buffer model;
+ std::string model_path;
- if (NPUBIN_VERSION (meta_->magiccode) != 3)
- return -EINVAL;
+ model_path = dir_ + "/" + NPU_MODEL_NAME;
- model.type = BUFFER_FILE;
- model.size = meta_->size;
- model.filepath = model_path.c_str();
+ meta_ = getNPUmodel_metadata (model_path.c_str (), false);
+ if (meta_ == nullptr)
+ return -EINVAL;
- int status = allocNPU_modelBuffer (dev_, &model);
- if (status != 0)
- return status;
+ if (NPUBIN_VERSION (meta_->magiccode) != 3)
+ return -EINVAL;
- status = registerNPUmodel (dev_, &model, &model_id_);
- cleanNPU_modelBuffer (dev_, &model);
+ model.type = BUFFER_FILE;
+ model.size = meta_->size;
+ model.filepath = model_path.c_str ();
+ int status = allocNPU_modelBuffer (dev_, &model);
+ if (status != 0)
return status;
- }
- int set_data_info () {
- tensors_data_info info_in;
- tensors_data_info info_out;
+ status = registerNPUmodel (dev_, &model, &model_id_);
+ cleanNPU_modelBuffer (dev_, &model);
+
+ return status;
+ }
- /* No data manipulation & quantization in this test */
+ int set_data_info () {
+ tensors_data_info info_in;
+ tensors_data_info info_out;
- info_in.num_info = meta_->input_seg_num;
- for (uint32_t idx = 0; idx < info_in.num_info; idx++) {
- info_in.info[idx].layout = DATA_LAYOUT_TRIV2;
- info_in.info[idx].type = DATA_TYPE_QASYMM8;
- }
+ /* No data manipulation & quantization in this test */
- info_out.num_info = meta_->output_seg_num;
- for (uint32_t idx = 0; idx < info_out.num_info; idx++) {
- info_out.info[idx].layout = DATA_LAYOUT_TRIV2;
- info_out.info[idx].type = DATA_TYPE_QASYMM8;
- }
+ info_in.num_info = meta_->input_seg_num;
+ for (uint32_t idx = 0; idx < info_in.num_info; idx++) {
+ info_in.info[idx].layout = DATA_LAYOUT_TRIV2;
+ info_in.info[idx].type = DATA_TYPE_QASYMM8;
+ }
- return setNPU_dataInfo (dev_, model_id_, &info_in, &info_out);
+ info_out.num_info = meta_->output_seg_num;
+ for (uint32_t idx = 0; idx < info_out.num_info; idx++) {
+ info_out.info[idx].layout = DATA_LAYOUT_TRIV2;
+ info_out.info[idx].type = DATA_TYPE_QASYMM8;
}
- std::string dir_;
- std::string hw_dev_;
+ return setNPU_dataInfo (dev_, model_id_, &info_in, &info_out);
+ }
+
+ std::string dir_;
+ std::string hw_dev_;
- npudev_h dev_;
+ npudev_h dev_;
- npubin_meta *meta_;
- uint32_t model_id_;
+ npubin_meta *meta_;
+ uint32_t model_id_;
- uint32_t loop_;
- ThirdPartyHW hw_;
+ uint32_t loop_;
+ ThirdPartyHW hw_;
};
static void
-print_usage (const char *prog_name)
-{
+print_usage (const char *prog_name) {
std::cerr << "Usage: " << prog_name << " [options] model_dir target_dev\n";
std::cerr << "Options: \n";
std::cerr << " -l <arg> \t Set the number of recurring invocations\n";
/** @brief apptest main */
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
TesterTRIV2 tester;
int c, status;
while ((c = getopt (argc, argv, "hl:")) != -1) {
switch (c) {
case 'l':
- tester.set_loop (atoi(optarg));
+ tester.set_loop (atoi (optarg));
break;
case '?':
if (optopt == 'l')
std::cerr << std::endl;
// no-break
case 'h':
- print_usage(argv[0]);
+ print_usage (argv[0]);
return 0;
}
}
using namespace tinyxml2;
/** @brief C++ class to describe how to use npu-engine library */
-class Tester : public UtilTRIV2
-{
- public:
- Tester () : total_ (0) {}
-
- /** @brief initilize the device handle */
- int init (std::string xml_file) {
- int status = UtilTRIV2::init ();
- if (status != 0) {
- cerr << "Failed to initialize\n";
- return status;
- }
-
- XMLDocument doc;
- if (doc.LoadFile (xml_file.c_str ()) != XML_SUCCESS) {
- cerr << "Unable to find the given xml file\n";
- return -ENOENT;
- }
-
- status = parse_xml (doc);
- if (status != 0) {
- cerr << "Invalid model description detected\n";
- return status;
- }
-
- return 0;
+class Tester : public UtilTRIV2 {
+ public:
+ Tester () : total_ (0) {}
+
+ /** @brief initilize the device handle */
+ int init (std::string xml_file) {
+ int status = UtilTRIV2::init ();
+ if (status != 0) {
+ cerr << "Failed to initialize\n";
+ return status;
}
- /** @brief parse the model xml file */
- int parse_xml (XMLDocument& xml) {
- XMLElement *root = xml.RootElement ();
- if (root == nullptr)
- return -EINVAL;
+ XMLDocument doc;
+ if (doc.LoadFile (xml_file.c_str ()) != XML_SUCCESS) {
+ cerr << "Unable to find the given xml file\n";
+ return -ENOENT;
+ }
- XMLElement *elem = root->FirstChildElement ("model");
- while (elem != nullptr) {
- int priority = elem->IntAttribute ("priority");
- int timeout = elem->IntAttribute ("timeout");
+ status = parse_xml (doc);
+ if (status != 0) {
+ cerr << "Invalid model description detected\n";
+ return status;
+ }
- if (priority < NPU_PRIORITY_LOW ||
- priority > NPU_PRIORITY_HIGH ||
- timeout < 0)
- return -EINVAL;
+ return 0;
+ }
- XMLElement *item = elem->FirstChildElement ("dirpath");
- if (item == nullptr)
- return -EINVAL;
+ /** @brief parse the model xml file */
+ int parse_xml (XMLDocument &xml) {
+ XMLElement *root = xml.RootElement ();
+ if (root == nullptr)
+ return -EINVAL;
- if (UtilTRIV2::loadModel (item->GetText (), nullptr,
- static_cast<npu_priority>(priority), timeout) != 0)
- return -EINVAL;
+ XMLElement *elem = root->FirstChildElement ("model");
+ while (elem != nullptr) {
+ int priority = elem->IntAttribute ("priority");
+ int timeout = elem->IntAttribute ("timeout");
- total_++;
- elem = elem->NextSiblingElement ("model");
- }
+ if (priority < NPU_PRIORITY_LOW || priority > NPU_PRIORITY_HIGH ||
+ timeout < 0)
+ return -EINVAL;
- return 0;
+ XMLElement *item = elem->FirstChildElement ("dirpath");
+ if (item == nullptr)
+ return -EINVAL;
+
+ if (UtilTRIV2::loadModel (item->GetText (), nullptr,
+ static_cast<npu_priority> (priority),
+ timeout) != 0)
+ return -EINVAL;
+
+ total_++;
+ elem = elem->NextSiblingElement ("model");
}
- /** @brief run the inference */
- int run () {
- int status = UtilTRIV2::runAll ();
- if (status != 0) {
- cerr << "Failed to run models in .xml file: " << status << "\n";
- return status;
- }
+ return 0;
+ }
- return wait () == total_ ? test_ret_success : test_ret_failure;
+ /** @brief run the inference */
+ int run () {
+ int status = UtilTRIV2::runAll ();
+ if (status != 0) {
+ cerr << "Failed to run models in .xml file: " << status << "\n";
+ return status;
}
- private:
- uint32_t total_;
+ return wait () == total_ ? test_ret_success : test_ret_failure;
+ }
+
+ private:
+ uint32_t total_;
};
/** @brief apptest main */
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
Tester tester;
- char * xml_file;
+ char *xml_file;
int status;
status = tester.parseArgs (argc, argv, &xml_file, "[.xml file]");
* @brief fill dummy metadata v1
*/
static void
-fill_test_metadata_v1 (npubin_meta & data, uint32_t input_size, uint32_t output_size)
-{
- data.magiccode = NPUBIN_MAGICCODE | 0x1; /* v1 */
+fill_test_metadata_v1 (npubin_meta &data, uint32_t input_size,
+ uint32_t output_size) {
+ data.magiccode = NPUBIN_MAGICCODE | 0x1; /* v1 */
data.size = 8192;
data.buffer_size = 4096;
data.program_size = 2048;
* @brief fill dummy metadata v2
*/
static void
-fill_test_metadata_v2 (npubin_meta & data, uint32_t input_num, uint32_t output_num)
-{
- data.magiccode = NPUBIN_MAGICCODE | 0x2; /* v2 */
+fill_test_metadata_v2 (npubin_meta &data, uint32_t input_num,
+ uint32_t output_num) {
+ data.magiccode = NPUBIN_MAGICCODE | 0x2; /* v2 */
data.size = 8192;
data.buffer_size = 4096;
data.program_size = 2048;
for (uint32_t i = 0; i < data.output_num; i++) {
data.output_offsets[i] = 100 * (i + 1);
data.output_elem_size[i] = 1 * (i + 1);
- for (uint32_t j = 0; j < MAX_RANK -1; j++)
+ for (uint32_t j = 0; j < MAX_RANK - 1; j++)
data.output_dims[i][j] = 1 * (i + 1);
data.output_dims[i][MAX_RANK - 1] = DATA_GRANULARITY;
data.output_quant_z[i] = 3 * (i + 1);
/**
* @brief check buffer primitives
*/
-TEST (ne_core_buffer_test, buffer_primitives)
-{
+TEST (ne_core_buffer_test, buffer_primitives) {
const size_t exp_size_v1 = 4096;
const uint32_t exp_input_num_v1 = 1;
const uint32_t exp_output_num_v1 = 1;
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
std::unique_ptr<Buffer> buffer (new Buffer (new HWmemDevice));
buffer->setDriverAPI (api.get ());
EXPECT_EQ (buffer->alloc (exp_size_v1), 0);
EXPECT_EQ (buffer->getSize (), exp_size_v1);
- EXPECT_EQ (api.get(), buffer->getDriverAPI ());
+ EXPECT_EQ (api.get (), buffer->getDriverAPI ());
/** it's okay even if metadata is nullptr */
EXPECT_EQ (buffer->createTensors (nullptr), 0);
fill_test_metadata_v1 (data, input_size, output_size);
std::unique_ptr<Metadata> meta = Metadata::extractMetadata (&data);
- EXPECT_EQ (buffer->createTensors (meta.get()), 0);
+ EXPECT_EQ (buffer->createTensors (meta.get ()), 0);
EXPECT_EQ (buffer->getInputNum (), exp_input_num_v1);
EXPECT_EQ (buffer->getOutputNum (), exp_output_num_v1);
EXPECT_EQ (buffer->getInputTensor (0)->getSize (), input_size);
EXPECT_EQ (buffer2->alloc (exp_size_v2), 0);
EXPECT_EQ (buffer2->getSize (), exp_size_v2);
- EXPECT_EQ (buffer2->createTensors (meta.get()), 0);
+ EXPECT_EQ (buffer2->createTensors (meta.get ()), 0);
EXPECT_EQ (buffer2->getInputNum (), exp_input_num_v2);
EXPECT_EQ (buffer2->getOutputNum (), exp_output_num_v2);
for (uint32_t i = 0; i < data.input_num; i++)
EXPECT_EQ (buffer2->getInputTensor (i)->getSize (),
- meta->getInputTensorSize (i, DATA_LAYOUT_SRNPU));
+ meta->getInputTensorSize (i, DATA_LAYOUT_SRNPU));
for (uint32_t i = 0; i < data.output_num; i++)
EXPECT_EQ (buffer2->getOutputTensor (i)->getSize (),
- meta->getOutputTensorSize (i, DATA_LAYOUT_SRNPU));
+ meta->getOutputTensorSize (i, DATA_LAYOUT_SRNPU));
}
/**
* @brief check buffer alloc with error handling
*/
-TEST (ne_core_buffer_test, buffer_alloc_no_drv_api_n)
-{
+TEST (ne_core_buffer_test, buffer_alloc_no_drv_api_n) {
std::unique_ptr<Buffer> buffer (new Buffer (new HWmemDevice));
uint32_t size = 4096;
/**
* @brief check buffer's createTensors() with error handling
*/
-TEST (ne_core_buffer_test, buffer_create_tensors_n)
-{
+TEST (ne_core_buffer_test, buffer_create_tensors_n) {
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
uint32_t size = 4096;
std::unique_ptr<Buffer> buffer (new Buffer (new HWmemDevice));
- buffer->setDriverAPI (api.get());
+ buffer->setDriverAPI (api.get ());
ASSERT_EQ (buffer->alloc (size), 0);
/** createTensors() after dealloc the base buffer */
/**
* @brief check buffer getInput/OutputTensor with error handling
*/
-TEST (ne_core_buffer_test, buffer_get_tensor_n)
-{
+TEST (ne_core_buffer_test, buffer_get_tensor_n) {
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
std::unique_ptr<Buffer> buffer (new Buffer (new HWmemDevice));
buffer->setDriverAPI (api.get ());
* @brief main function for unit test
*/
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
return start_gtest (argc, argv);
}
/**
* @brief Test getCommPlugin()
*/
-TEST (ne_core_comm_test, get_comm_plugin)
-{
- CommPlugin & plugin = CommPlugin::getCommPlugin();
- CommPlugin & plugin2 = CommPlugin::getCommPlugin();
+TEST (ne_core_comm_test, get_comm_plugin) {
+ CommPlugin &plugin = CommPlugin::getCommPlugin ();
+ CommPlugin &plugin2 = CommPlugin::getCommPlugin ();
/** should be same as it's singleton */
EXPECT_EQ (&plugin, &plugin2);
}
-static size_t test_manip (char *data, size_t size)
-{
- for (size_t i = 0; i < size; i++)
- data[i] = 'b';
+static size_t
+test_manip (char *data, size_t size) {
+ for (size_t i = 0; i < size; i++) data[i] = 'b';
return size;
}
/**
* @brief Test extractGenericBuffer()
*/
-TEST (ne_core_comm_test, extract_buffer)
-{
- CommPlugin & plugin = CommPlugin::getCommPlugin();
+TEST (ne_core_comm_test, extract_buffer) {
+ CommPlugin &plugin = CommPlugin::getCommPlugin ();
size_t size = 4096;
char *data = new char[size];
buffer.addr = new char[size];
buffer.size = size;
- for (size_t i = 0; i < size; i++)
- static_cast<char*>(buffer.addr)[i] = 'a';
+ for (size_t i = 0; i < size; i++) static_cast<char *> (buffer.addr)[i] = 'a';
EXPECT_EQ (plugin.extractGenericBuffer (&buffer, data), 0);
- for (size_t i = 0; i < size; i++)
- EXPECT_EQ (data[i], 'a');
+ for (size_t i = 0; i < size; i++) EXPECT_EQ (data[i], 'a');
/** data manipulation func is called */
auto manip_func = std::bind (test_manip, data, size);
EXPECT_EQ (plugin.extractGenericBuffer (&buffer, data, manip_func), 0);
- for (size_t i = 0; i < size; i++)
- EXPECT_EQ (data[i], 'b');
+ for (size_t i = 0; i < size; i++) EXPECT_EQ (data[i], 'b');
- delete [] static_cast<char*>(buffer.addr);
- delete [] data;
+ delete[] static_cast<char *> (buffer.addr);
+ delete[] data;
}
/**
* @brief Test extractGenericBuffer() with error handling
*/
-TEST (ne_core_comm_test, extract_buffer_args_n)
-{
- CommPlugin & plugin = CommPlugin::getCommPlugin();
+TEST (ne_core_comm_test, extract_buffer_args_n) {
+ CommPlugin &plugin = CommPlugin::getCommPlugin ();
size_t size = 4096;
char *data = new char[size];
buffer.size = 0;
EXPECT_NE (plugin.extractGenericBuffer (&buffer, data), 0);
- delete [] static_cast<char*>(buffer.addr);
- delete [] data;
+ delete[] static_cast<char *> (buffer.addr);
+ delete[] data;
}
/**
* @brief Test extractGenericBuffer() with error handling
*/
-TEST (ne_core_comm_test, extract_buffer_not_supported_n)
-{
- CommPlugin & plugin = CommPlugin::getCommPlugin();
+TEST (ne_core_comm_test, extract_buffer_not_supported_n) {
+ CommPlugin &plugin = CommPlugin::getCommPlugin ();
size_t size = 4096;
char *data = new char[size];
/** not supported */
buffer.type = BUFFER_DMABUF;
EXPECT_NE (plugin.extractGenericBuffer (&buffer, data), 0);
- buffer.type = static_cast<buffer_types>(-1);
+ buffer.type = static_cast<buffer_types> (-1);
EXPECT_NE (plugin.extractGenericBuffer (&buffer, data), 0);
- delete [] data;
+ delete[] data;
}
/**
* @brief Test extractGenericBuffer(), file type
*/
-TEST (ne_core_comm_test, extract_buffer_file)
-{
- CommPlugin & plugin = CommPlugin::getCommPlugin();
+TEST (ne_core_comm_test, extract_buffer_file) {
+ CommPlugin &plugin = CommPlugin::getCommPlugin ();
size_t size = 4096;
char *data = new char[size];
buffer.size = size;
std::ofstream ofs (buffer.filepath, std::ios::binary);
- EXPECT_EQ (ofs.good(), true);
+ EXPECT_EQ (ofs.good (), true);
- for (size_t i = 0; i < size; i++)
- data[i] = 'a';
+ for (size_t i = 0; i < size; i++) data[i] = 'a';
ofs.write (data, size);
ofs.close ();
EXPECT_EQ (plugin.extractGenericBuffer (&buffer, data), 0);
- for (size_t i = 0; i < size; i++)
- EXPECT_EQ (data[i], 'a');
+ for (size_t i = 0; i < size; i++) EXPECT_EQ (data[i], 'a');
- delete [] data;
+ delete[] data;
}
/**
* @brief Test extractGenericBuffer() with error handling (file)
*/
-TEST (ne_core_comm_test, extract_buffer_file_n)
-{
- CommPlugin & plugin = CommPlugin::getCommPlugin();
+TEST (ne_core_comm_test, extract_buffer_file_n) {
+ CommPlugin &plugin = CommPlugin::getCommPlugin ();
size_t size = 4096;
char *data = new char[size];
EXPECT_NE (plugin.extractGenericBuffer (&buffer, data), 0);
- delete [] data;
+ delete[] data;
}
/**
* @brief Test insertGenericBuffer()
*/
-TEST (ne_core_comm_test, insert_buffer)
-{
- CommPlugin & plugin = CommPlugin::getCommPlugin();
+TEST (ne_core_comm_test, insert_buffer) {
+ CommPlugin &plugin = CommPlugin::getCommPlugin ();
size_t size = 4096;
char *data = new char[size];
buffer.addr = new char[size];
buffer.size = size;
- for (size_t i = 0; i < size; i++)
- data[i] = 'a';
+ for (size_t i = 0; i < size; i++) data[i] = 'a';
EXPECT_EQ (plugin.insertGenericBuffer (data, &buffer), 0);
for (size_t i = 0; i < size; i++)
- EXPECT_EQ (static_cast<char*>(buffer.addr)[i], 'a');
+ EXPECT_EQ (static_cast<char *> (buffer.addr)[i], 'a');
/** data manipulation func is called */
- auto manip_func = std::bind (test_manip, static_cast<char*>(buffer.addr), size);
+ auto manip_func =
+ std::bind (test_manip, static_cast<char *> (buffer.addr), size);
EXPECT_EQ (plugin.insertGenericBuffer (data, &buffer, manip_func), 0);
for (size_t i = 0; i < size; i++)
- EXPECT_EQ (static_cast<char*>(buffer.addr)[i], 'b');
+ EXPECT_EQ (static_cast<char *> (buffer.addr)[i], 'b');
- delete [] static_cast<char*>(buffer.addr);
- delete [] data;
+ delete[] static_cast<char *> (buffer.addr);
+ delete[] data;
}
/**
* @brief Test insertGenericBuffer() with error handling
*/
-TEST (ne_core_comm_test, insert_buffer_args_n)
-{
- CommPlugin & plugin = CommPlugin::getCommPlugin();
+TEST (ne_core_comm_test, insert_buffer_args_n) {
+ CommPlugin &plugin = CommPlugin::getCommPlugin ();
size_t size = 4096;
char *data = new char[size];
buffer.size = 0;
EXPECT_NE (plugin.insertGenericBuffer (data, &buffer), 0);
- delete [] static_cast<char*>(buffer.addr);
- delete [] data;
+ delete[] static_cast<char *> (buffer.addr);
+ delete[] data;
}
/**
* @brief Test insertGenericBuffer() with error handling
*/
-TEST (ne_core_comm_test, insert_buffer_not_supported_n)
-{
- CommPlugin & plugin = CommPlugin::getCommPlugin();
+TEST (ne_core_comm_test, insert_buffer_not_supported_n) {
+ CommPlugin &plugin = CommPlugin::getCommPlugin ();
size_t size = 4096;
char *data = new char[size];
EXPECT_NE (plugin.insertGenericBuffer (data, &buffer), 0);
buffer.type = BUFFER_DMABUF;
EXPECT_NE (plugin.insertGenericBuffer (data, &buffer), 0);
- buffer.type = static_cast<buffer_types>(-1);
+ buffer.type = static_cast<buffer_types> (-1);
EXPECT_NE (plugin.insertGenericBuffer (data, &buffer), 0);
- delete [] data;
+ delete[] data;
}
/**
* @brief main function for unit test
*/
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
return start_gtest (argc, argv);
}
/**
* @brief Test checkCapability()
*/
-TEST (ne_core_data_test, check_capability)
-{
+TEST (ne_core_data_test, check_capability) {
std::unique_ptr<DataConverter> converter (new DataConverter (true));
converter->setDataType (DATA_TYPE_QASYMM8, DATA_TYPE_QASYMM8);
/**
* @brief Test checkCapability() with negative cases
*/
-TEST (ne_core_data_test, check_capability_n)
-{
+TEST (ne_core_data_test, check_capability_n) {
std::unique_ptr<DataConverter> converter (new DataConverter (true));
/* layouts are not resolved yet */
/**
* @brief Test perform() with quantization (QASYMM8)
*/
-TEST (ne_core_data_test, perform_quantization_asymm8)
-{
+TEST (ne_core_data_test, perform_quantization_asymm8) {
std::unique_ptr<DataConverter> converter (new DataConverter (true));
- uint16_t * src_data = new uint16_t [4096];
- uint8_t * dst_data = new uint8_t [4096];
+ uint16_t *src_data = new uint16_t[4096];
+ uint8_t *dst_data = new uint8_t[4096];
uint32_t data_dims[] = {1, 8, 8, 64};
uint32_t zero = 127;
float scale = 127.0;
EXPECT_EQ (dst_data[i], (uint8_t) val);
}
- delete [] src_data;
- delete [] dst_data;
+ delete[] src_data;
+ delete[] dst_data;
}
/**
* @brief Test perform() with quantization (QSYMM16)
*/
-TEST (ne_core_data_test, perform_quantization_symm16)
-{
+TEST (ne_core_data_test, perform_quantization_symm16) {
std::unique_ptr<DataConverter> converter (new DataConverter (true));
- uint32_t * src_data = new uint32_t [4096];
- int16_t * dst_data = new int16_t [4096];
+ uint32_t *src_data = new uint32_t[4096];
+ int16_t *dst_data = new int16_t[4096];
uint32_t data_dims[] = {1, 8, 8, 64};
uint32_t zero = 0;
float scale = 127.0;
EXPECT_EQ (dst_data[i], (int16_t) val);
}
- delete [] src_data;
- delete [] dst_data;
+ delete[] src_data;
+ delete[] dst_data;
}
/**
* @brief Test perform() with quantization (negative)
*/
-TEST (ne_core_data_test, perform_quantization_n)
-{
+TEST (ne_core_data_test, perform_quantization_n) {
std::unique_ptr<DataConverter> converter (new DataConverter (true));
- uint32_t * src_data = new uint32_t [4096];
- int16_t * dst_data = new int16_t [4096];
+ uint32_t *src_data = new uint32_t[4096];
+ int16_t *dst_data = new int16_t[4096];
uint32_t data_dims[] = {1, 8, 8, 64};
uint32_t zero = 0;
float scale = 0.0;
EXPECT_EQ (dst_data[i], 0);
}
- delete [] src_data;
- delete [] dst_data;
+ delete[] src_data;
+ delete[] dst_data;
}
/**
* @brief Test perform() with dequantization (QASYMM8)
*/
-TEST (ne_core_data_test, perform_dequantization_asymm8)
-{
+TEST (ne_core_data_test, perform_dequantization_asymm8) {
std::unique_ptr<DataConverter> converter (new DataConverter (false));
- uint8_t * src_data = new uint8_t [4096];
- uint16_t * dst_data = new uint16_t [4096];
+ uint8_t *src_data = new uint8_t[4096];
+ uint16_t *dst_data = new uint16_t[4096];
uint32_t data_dims[] = {1, 8, 8, 64};
uint32_t zero = 127;
float scale = 127.0;
EXPECT_EQ (dst_data[i], (uint16_t) val);
}
- delete [] src_data;
- delete [] dst_data;
+ delete[] src_data;
+ delete[] dst_data;
}
/**
* @brief Test perform() with dequantization (QSYMM16)
*/
-TEST (ne_core_data_test, perform_dequantization_symm16)
-{
+TEST (ne_core_data_test, perform_dequantization_symm16) {
std::unique_ptr<DataConverter> converter (new DataConverter (false));
- int16_t * src_data = new int16_t [4096];
- uint32_t * dst_data = new uint32_t [4096];
+ int16_t *src_data = new int16_t[4096];
+ uint32_t *dst_data = new uint32_t[4096];
uint32_t data_dims[] = {1, 8, 8, 64};
uint32_t zero = 0;
float scale = 127.0;
EXPECT_EQ (dst_data[i], (int32_t) val);
}
- delete [] src_data;
- delete [] dst_data;
+ delete[] src_data;
+ delete[] dst_data;
}
/**
* @brief Test perform() with layer conversion (QASYMM8)
*/
-TEST (ne_core_data_test, perform_layer_conversion_asymm8)
-{
+TEST (ne_core_data_test, perform_layer_conversion_asymm8) {
std::unique_ptr<DataConverter> converter (new DataConverter (true));
- uint8_t * src_data = new uint8_t [4096];
- uint8_t * dst_data = new uint8_t [4096];
+ uint8_t *src_data = new uint8_t[4096];
+ uint8_t *dst_data = new uint8_t[4096];
uint32_t data_dims[] = {1, 8, 8, 64};
uint32_t granularity = DATA_GRANULARITY;
EXPECT_EQ (converter->perform (), 4096);
for (int i = 0; i < 4096; i += granularity) {
- EXPECT_EQ (memcmp (src_data + i,
- dst_data + (granularity / 2) * (i / granularity),
- granularity / 2), 0);
+ EXPECT_EQ (
+ memcmp (src_data + i, dst_data + (granularity / 2) * (i / granularity),
+ granularity / 2),
+ 0);
EXPECT_EQ (memcmp (src_data + i + (granularity / 2),
- dst_data + (granularity / 2) * (i / granularity) + 2048,
- granularity / 2), 0);
+ dst_data + (granularity / 2) * (i / granularity) + 2048,
+ granularity / 2),
+ 0);
}
- delete [] src_data;
- delete [] dst_data;
+ delete[] src_data;
+ delete[] dst_data;
}
/**
* @brief Test perform() with layer conversion (QSYMM16)
*/
-TEST (ne_core_data_test, perform_layer_conversion_symm16)
-{
+TEST (ne_core_data_test, perform_layer_conversion_symm16) {
std::unique_ptr<DataConverter> converter (new DataConverter (true));
- int16_t * src_data = new int16_t [4096];
- int16_t * dst_data = new int16_t [4096];
+ int16_t *src_data = new int16_t[4096];
+ int16_t *dst_data = new int16_t[4096];
uint32_t data_dims[] = {1, 8, 8, 64};
uint32_t granularity = DATA_GRANULARITY;
/* check values */
for (int i = 0; i < 4096; i += granularity) {
- EXPECT_EQ (memcmp (src_data + i,
- dst_data + (granularity / 2) * (i / granularity),
- granularity / 2), 0);
+ EXPECT_EQ (
+ memcmp (src_data + i, dst_data + (granularity / 2) * (i / granularity),
+ granularity / 2),
+ 0);
EXPECT_EQ (memcmp (src_data + i + (granularity / 2),
- dst_data + (granularity / 2) * (i / granularity) + 2048,
- granularity / 2), 0);
+ dst_data + (granularity / 2) * (i / granularity) + 2048,
+ granularity / 2),
+ 0);
}
/* 2-TOPS */
EXPECT_EQ (converter->perform (), 4096 * 2);
for (int i = 0; i < 4096; i += granularity) {
- EXPECT_EQ (memcmp (src_data + i,
- dst_data + (granularity / 4) * (i / granularity),
- granularity / 4), 0);
+ EXPECT_EQ (
+ memcmp (src_data + i, dst_data + (granularity / 4) * (i / granularity),
+ granularity / 4),
+ 0);
EXPECT_EQ (memcmp (src_data + i + (granularity / 4),
- dst_data + (granularity / 4) * (i / granularity) + 1024,
- granularity / 4), 0);
+ dst_data + (granularity / 4) * (i / granularity) + 1024,
+ granularity / 4),
+ 0);
EXPECT_EQ (memcmp (src_data + i + 2 * (granularity / 4),
- dst_data + (granularity / 4) * (i / granularity) + 2048,
- granularity / 4), 0);
+ dst_data + (granularity / 4) * (i / granularity) + 2048,
+ granularity / 4),
+ 0);
EXPECT_EQ (memcmp (src_data + i + 3 * (granularity / 4),
- dst_data + (granularity / 4) * (i / granularity) + 3072,
- granularity / 4), 0);
+ dst_data + (granularity / 4) * (i / granularity) + 3072,
+ granularity / 4),
+ 0);
}
- delete [] src_data;
- delete [] dst_data;
+ delete[] src_data;
+ delete[] dst_data;
}
/**
* @brief Test perform() with negative cases
*/
-TEST (ne_core_data_test, perform_n)
-{
+TEST (ne_core_data_test, perform_n) {
std::unique_ptr<DataConverter> converter (new DataConverter (true));
- char * dummy_data = new char [4096];
+ char *dummy_data = new char[4096];
uint32_t dummy_dims[] = {1, 1, 1, 4096};
/* invalid parameters */
converter->setDataDims (dummy_dims);
EXPECT_EQ (converter->perform (), 0);
- delete [] dummy_data;
+ delete[] dummy_data;
}
/**
* @brief main function for unit test
*/
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
return start_gtest (argc, argv);
}
/**
* @brief test Device's createInstance()
*/
-TEST (ne_core_handler_test, device_create_instance)
-{
+TEST (ne_core_handler_test, device_create_instance) {
dev_type type = NPUCOND_TRIV2_CONN_SOCIP;
int num_devices = HostHandler::getNumDevices (type);
EXPECT_GT (num_devices, 0);
/**
* @brief test Device's createInstance() with out-of-range id
*/
-TEST (ne_core_handler_test, device_create_instance_out_of_range_n)
-{
+TEST (ne_core_handler_test, device_create_instance_out_of_range_n) {
dev_type type = NPUCOND_TRIV2_CONN_SOCIP;
int num_devices = HostHandler::getNumDevices (type);
EXPECT_GT (num_devices, 0);
/**
* @brief test Device's createInstance() with invalid types
*/
-TEST (ne_core_handler_test, device_create_instance_invalid_n)
-{
- EXPECT_EQ (Device::createInstance (static_cast<dev_type> (0x1111), 0), nullptr);
- EXPECT_EQ (Device::createInstance (static_cast<dev_type> (0xFFFF), 0), nullptr);
+TEST (ne_core_handler_test, device_create_instance_invalid_n) {
+ EXPECT_EQ (Device::createInstance (static_cast<dev_type> (0x1111), 0),
+ nullptr);
+ EXPECT_EQ (Device::createInstance (static_cast<dev_type> (0xFFFF), 0),
+ nullptr);
}
/**
* @brief test Device's createInstance() with deprecated types
*/
-TEST (ne_core_handler_test, device_create_instance_deprecated_n)
-{
+TEST (ne_core_handler_test, device_create_instance_deprecated_n) {
EXPECT_EQ (Device::createInstance (NPUCOND_TRIV_CONN_UNKNOWN, 0), nullptr);
EXPECT_EQ (Device::createInstance (NPUCOND_TRIV_CONN_SOCIP, 0), nullptr);
EXPECT_EQ (Device::createInstance (NPUCOND_TRIA_CONN_UNKNOWN, 0), nullptr);
* @brief test Device's primitives without initialization
* @note Only createInstance() calls init().
*/
-TEST (ne_core_handler_test, device_instance_uninitilized_n)
-{
+TEST (ne_core_handler_test, device_instance_uninitilized_n) {
dev_type type = NPUCOND_TRIV2_CONN_SOCIP;
int num_devices = HostHandler::getNumDevices (type);
EXPECT_GT (num_devices, 0);
std::unique_ptr<Device> triv2 (new TrinityVision2 (0));
EXPECT_EQ (triv2->initialized (), false);
- void * addr = nullptr;
+ void *addr = nullptr;
Model *model;
generic_buffer model_buf;
input_buffers input;
EXPECT_EQ (triv2->deallocMemory (0, 4096, addr), -EPERM);
EXPECT_EQ (triv2->stop (true), -EPERM);
EXPECT_EQ (triv2->setModel (&model_buf, &model), -EPERM);
- EXPECT_EQ (triv2->run (NPUINPUT_HOST, model, &input, nullptr, nullptr, nullptr), -EPERM);
+ EXPECT_EQ (
+ triv2->run (NPUINPUT_HOST, model, &input, nullptr, nullptr, nullptr),
+ -EPERM);
EXPECT_EQ (triv2->unsetModel (model), -EPERM);
}
/**
* @brief test Device's alloc/deallocMemory ()
*/
-TEST (ne_core_handler_test, device_memory)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, device_memory) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- void * addr;
+ void *addr;
int dmabuf;
dmabuf = device->allocMemory (4096, &addr);
EXPECT_GE (dmabuf, 0);
/**
* @brief test Device's allocMemory () with error handling
*/
-TEST (ne_core_handler_test, device_memory_args_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, device_memory_args_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- void * addr;
+ void *addr;
int dmabuf;
EXPECT_NE (device->allocMemory (0, &addr), 0);
EXPECT_NE (device->allocMemory (4096, nullptr), 0);
}
static void
-create_model_buffer (generic_buffer & model_buf)
-{
+create_model_buffer (generic_buffer &model_buf) {
npubin_meta meta = {0};
size_t size = 4096;
- meta.magiccode = NPUBIN_MAGICCODE | 0x3; /** npubinfmt v3 */
+ meta.magiccode = NPUBIN_MAGICCODE | 0x3; /** npubinfmt v3 */
/* no instructions */
meta.program_size = 0;
model_buf.type = BUFFER_MAPPED;
model_buf.size = meta.size;
- model_buf.addr = new char [meta.size];
+ model_buf.addr = new char[meta.size];
memcpy (model_buf.addr, &meta, NPUBIN_META_SIZE);
}
static void
-destroy_model_buffer (generic_buffer & model_buf)
-{
+destroy_model_buffer (generic_buffer &model_buf) {
if (model_buf.addr != nullptr)
- delete [] (static_cast<char *> (model_buf.addr));
+ delete[](static_cast<char *> (model_buf.addr));
}
/**
* @brief test TRIV2's setModel ()
*/
-TEST (ne_core_handler_test, triv2_set_model)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, triv2_set_model) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
generic_buffer model_buf;
/**
* @brief test TRIV2's setModel () with error handling
*/
-TEST (ne_core_handler_test, triv2_set_model_invalid_args_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, triv2_set_model_invalid_args_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
generic_buffer model_buf;
/**
* @brief test TRIV2's setModel () with error handling
*/
-TEST (ne_core_handler_test, triv2_set_model_invalid_data_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, triv2_set_model_invalid_data_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
generic_buffer model_buf;
/** generic_buffer: invalid addr */
model_buf.type = BUFFER_MAPPED;
- delete [] (static_cast<char *> (model_buf.addr));
+ delete[](static_cast<char *> (model_buf.addr));
model_buf.addr = nullptr;
EXPECT_NE (device->setModel (&model_buf, &model), 0);
/**
* @brief test TRIV2's unsetModel () with error handling
*/
-TEST (ne_core_handler_test, triv2_unset_model_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, triv2_unset_model_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
EXPECT_NE (device->unsetModel (nullptr), 0);
}
static void
-create_input_buffers (input_buffers & input_buf)
-{
+create_input_buffers (input_buffers &input_buf) {
size_t size = 4096;
input_buf.num_buffers = 1;
input_buf.bufs[0].type = BUFFER_MAPPED;
input_buf.bufs[0].size = size;
- input_buf.bufs[0].addr = new char [size];
+ input_buf.bufs[0].addr = new char[size];
}
static void
-destroy_input_buffers (input_buffers & input_buf)
-{
- delete [] (static_cast<char *> (input_buf.bufs[0].addr));
+destroy_input_buffers (input_buffers &input_buf) {
+ delete[](static_cast<char *> (input_buf.bufs[0].addr));
}
typedef struct {
int num_called;
} user_cb_data;
-static void user_cb (output_buffers *output, uint64_t sequence,
- void *data)
-{
- user_cb_data * cb_data = static_cast<user_cb_data *> (data);
+static void
+user_cb (output_buffers *output, uint64_t sequence, void *data) {
+ user_cb_data *cb_data = static_cast<user_cb_data *> (data);
std::unique_lock<std::mutex> lock (cb_data->m);
cb_data->num_called++;
cb_data->cv.notify_one ();
/**
* @brief test Device's run ()
*/
-TEST (ne_core_handler_test, triv2_run)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, triv2_run) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
/** prepare model */
uint64_t sequence;
data.num_called = 0;
- EXPECT_GT (device->run (opmode, model, &input_buf, user_cb, &data, &sequence), 0);
- EXPECT_EQ (sequence, (uint64_t) 7); /* 7th run request */
- EXPECT_GT (device->run (opmode, model, &input_buf, user_cb, &data, &sequence), 0);
+ EXPECT_GT (device->run (opmode, model, &input_buf, user_cb, &data, &sequence),
+ 0);
+ EXPECT_EQ (sequence, (uint64_t) 7); /* 7th run request */
+ EXPECT_GT (device->run (opmode, model, &input_buf, user_cb, &data, &sequence),
+ 0);
EXPECT_EQ (sequence, (uint64_t) 8);
- EXPECT_GT (device->run (opmode, model, &input_buf, user_cb, &data, &sequence), 0);
+ EXPECT_GT (device->run (opmode, model, &input_buf, user_cb, &data, &sequence),
+ 0);
EXPECT_EQ (sequence, (uint64_t) 9);
wait_callbacks (data.num_called, max_called, data.m, data.cv);
/**
* @brief test TRIV2's run () with error handling
*/
-TEST (ne_core_handler_test, triv2_run_invalid_args_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, triv2_run_invalid_args_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
/** TRIV2 requires valid model and buffer arguments */
/**
* @brief test TRIV2's run () with error handling
*/
-TEST (ne_core_handler_test, triv2_run_invalid_opmode_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, triv2_run_invalid_opmode_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
/** prepare model */
EXPECT_NE (device->run (NPUINPUT_INTERNAL_CAM, model, &input_buf), 0);
EXPECT_NE (device->run (NPUINPUT_I2S_MIC, model, &input_buf), 0);
EXPECT_NE (device->run (NPUINPUT_HW_RECURRING, model, &input_buf), 0);
- EXPECT_NE (device->run (static_cast<npu_input_opmode>(0x10), model, &input_buf), 0);
+ EXPECT_NE (
+ device->run (static_cast<npu_input_opmode> (0x10), model, &input_buf), 0);
destroy_input_buffers (input_buf);
destroy_model_buffer (model_buf);
/**
* @brief test Device's stop ()
*/
-TEST (ne_core_handler_test, triv2_stop)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, triv2_stop) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
/** prepare model */
EXPECT_GT (device->run (opmode, model, &input_buf, user_cb, &data), 0);
EXPECT_GT (device->run (opmode, model, &input_buf, user_cb, &data), 0);
EXPECT_GT (device->run (opmode, model, &input_buf, user_cb, &data), 0);
- EXPECT_EQ (device->stop (false), 0); /* wait until all requests are resolved */
+ EXPECT_EQ (device->stop (false),
+ 0); /* wait until all requests are resolved */
usleep (TEST_SLEEP_MS); /** TODO remove this when stop() is implemented */
- EXPECT_EQ (data.num_called, max_called); /** callbacks are called successfully */
+ EXPECT_EQ (data.num_called,
+ max_called); /** callbacks are called successfully */
destroy_input_buffers (input_buf);
destroy_model_buffer (model_buf);
/**
* @brief test HostHandler's registerModel ()
*/
-TEST (ne_core_handler_test, handler_register_model)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_register_model) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
uint32_t modelid_start, modelid;
/**
* @brief test HostHandler's registerModel () with error handling
*/
-TEST (ne_core_handler_test, handler_register_model_invalid_args_01_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_register_model_invalid_args_01_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
uint32_t modelid;
/**
* @brief test HostHandler's registerModel () with error handling
*/
-TEST (ne_core_handler_test, handler_register_model_invalid_args_02_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_register_model_invalid_args_02_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
uint32_t modelid;
/**
* @brief test HostHandler's unregisterModel () with error handling
*/
-TEST (ne_core_handler_test, handler_unregister_model_invalid_id_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_unregister_model_invalid_id_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
/** no such model IDs */
/**
* @brief test HostHandler's getModel () with error handling
*/
-TEST (ne_core_handler_test, handler_get_model_invalid_id_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_get_model_invalid_id_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
uint32_t modelid;
/**
* @brief test HostHandler's setDataInfo ()
*/
-TEST (ne_core_handler_test, handler_set_data_info)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_set_data_info) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
uint32_t modelid;
/**
* @brief test HostHandler's setDataInfo () with error handling
*/
-TEST (ne_core_handler_test, handler_set_data_info_invalid_args_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_set_data_info_invalid_args_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
uint32_t modelid;
/**
* @brief test HostHandler's setConstraint ()
*/
-TEST (ne_core_handler_test, handler_set_constraint)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_set_constraint) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
uint32_t modelid;
EXPECT_EQ (handler->setConstraint (modelid, constraint), 0);
- Model * model = handler->getModel (modelid);
+ Model *model = handler->getModel (modelid);
EXPECT_NE (model, nullptr);
- EXPECT_EQ (model->getConstraint().priority, constraint.priority);
- EXPECT_EQ (model->getConstraint().timeout_ms, constraint.timeout_ms);
+ EXPECT_EQ (model->getConstraint ().priority, constraint.priority);
+ EXPECT_EQ (model->getConstraint ().timeout_ms, constraint.timeout_ms);
EXPECT_EQ (handler->unregisterModels (), 0);
/**
* @brief test HostHandler's setConstraint () with error handling
*/
-TEST (ne_core_handler_test, handler_set_constraint_invalid_args_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_set_constraint_invalid_args_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
uint32_t modelid;
/**
* @brief test HostHandler's allocGenericBuffer ()
*/
-TEST (ne_core_handler_test, handler_generic_buffer)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_generic_buffer) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
generic_buffer buf;
/**
* @brief test HostHandler's allocGenericBuffer () with error handling
*/
-TEST (ne_core_handler_test, handler_generic_buffer_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_generic_buffer_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
/** invalid args */
- EXPECT_NE (handler->allocGenericBuffer (static_cast<generic_buffer *>(nullptr)), 0);
- EXPECT_NE (handler->deallocGenericBuffer (static_cast<generic_buffer *>(nullptr)), 0);
+ EXPECT_NE (
+ handler->allocGenericBuffer (static_cast<generic_buffer *> (nullptr)), 0);
+ EXPECT_NE (
+ handler->deallocGenericBuffer (static_cast<generic_buffer *> (nullptr)),
+ 0);
/** invalid type */
generic_buffer buf;
/**
* @brief test HostHandler's allocGenericBuffer () with error handling
*/
-TEST (ne_core_handler_test, handler_generic_buffers_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_generic_buffers_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
/** invalid args */
- EXPECT_NE (handler->allocGenericBuffer (static_cast<generic_buffers *>(nullptr)), 0);
- EXPECT_NE (handler->deallocGenericBuffer (static_cast<generic_buffers *>(nullptr)), 0);
+ EXPECT_NE (
+ handler->allocGenericBuffer (static_cast<generic_buffers *> (nullptr)),
+ 0);
+ EXPECT_NE (
+ handler->deallocGenericBuffer (static_cast<generic_buffers *> (nullptr)),
+ 0);
/** invalid type */
/**
* @brief test HostHandler's runSync ()
*/
-TEST (ne_core_handler_test, handler_triv2_run_sync)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_triv2_run_sync) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
/** model */
/**
* @brief test HostHandler's runSync () with error handling
*/
-TEST (ne_core_handler_test, handler_triv2_run_sync_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_triv2_run_sync_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
/** model */
/**
* @brief test HostHandler's runAsync ()
*/
-TEST (ne_core_handler_test, handler_triv2_run_async)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_triv2_run_async) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
/** model */
/**
* @brief test HostHandler's runAsync () with error handling
*/
-TEST (ne_core_handler_test, handler_triv2_run_async_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_triv2_run_async_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
/** model */
/**
* @brief test HostHandler's getMemoryStatus ()
*/
-TEST (ne_core_handler_test, handler_triv2_get_memory_status)
-{
+TEST (ne_core_handler_test, handler_triv2_get_memory_status) {
// disabled until this feature is implemented
-#if defined (ENABLE_EMUL)
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+#if defined(ENABLE_EMUL)
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
const size_t size = 4096;
/**
* @brief test HostHandler's getMemoryStatus () with error handling
*/
-TEST (ne_core_handler_test, handler_triv2_get_memory_status_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_triv2_get_memory_status_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
size_t alloc_total;
/**
* @brief test HostHandler's stopInternal () with invalid id
*/
-TEST (ne_core_handler_test, handler_triv2_stop_internal_n)
-{
- std::unique_ptr<Device> device (Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
+TEST (ne_core_handler_test, handler_triv2_stop_internal_n) {
+ std::unique_ptr<Device> device (
+ Device::createInstance (NPUCOND_TRIV2_CONN_SOCIP, 0));
ASSERT_NE (device.get (), nullptr);
- HostHandler * handler = device->getHostHandler ();
+ HostHandler *handler = device->getHostHandler ();
ASSERT_NE (handler, nullptr);
-
EXPECT_NE (handler->stopInternal (0), 0);
}
* @brief main function for unit test
*/
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
return start_gtest (argc, argv);
}
#include <xf86drm.h>
#include "ne_unittest_utils.h"
-#if defined (ENABLE_EMUL)
+#if defined(ENABLE_EMUL)
constexpr bool is_enabled_emulation = true;
#else
constexpr bool is_enabled_emulation = false;
#endif /* defined (ENABLE_EMUL) */
/** use TRIV driver api for this test */
-#define INIT_TEST_DRIVER_API()\
+#define INIT_TEST_DRIVER_API() \
ASSERT_GT (DriverAPI::getNumDevices (NPUCOND_TRIV2_CONN_SOCIP), 0); \
- std::unique_ptr<DriverAPI> api;\
- api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0); \
- EXPECT_NE (api.get(), nullptr); \
+ std::unique_ptr<DriverAPI> api; \
+ api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0); \
+ EXPECT_NE (api.get (), nullptr);
#ifndef DRM_RDWR
#define DRM_RDWR O_RDWR
/**
* @brief check set/get methods
*/
-TEST (ne_core_hwmem_test, hwmem_set_get_methods)
-{
+TEST (ne_core_hwmem_test, hwmem_set_get_methods) {
const size_t exp_size = 1024;
const size_t exp_offset = 0x1000;
HWmem *hwmem;
- INIT_TEST_DRIVER_API();
+ INIT_TEST_DRIVER_API ();
/** testing set/get methods */
hwmem = new HWmem;
- hwmem->setDriverAPI (api.get());
- EXPECT_EQ (hwmem->getDriverAPI(), api.get());
+ hwmem->setDriverAPI (api.get ());
+ EXPECT_EQ (hwmem->getDriverAPI (), api.get ());
hwmem->setDmabuf (1);
- EXPECT_EQ (hwmem->getDmabuf(), 1);
+ EXPECT_EQ (hwmem->getDmabuf (), 1);
hwmem->setDmabuf (-1);
- EXPECT_EQ (hwmem->getDmabuf(), -1);
+ EXPECT_EQ (hwmem->getDmabuf (), -1);
hwmem->setSize (exp_size);
- EXPECT_EQ (hwmem->getSize(), exp_size);
+ EXPECT_EQ (hwmem->getSize (), exp_size);
hwmem->setOffset (exp_offset);
- EXPECT_EQ (hwmem->getOffset(), exp_offset);
- hwmem->setBaseAddr ((void*) 0x1000);
- EXPECT_EQ (hwmem->getBaseAddr(), (void*) 0x1000);
+ EXPECT_EQ (hwmem->getOffset (), exp_offset);
+ hwmem->setBaseAddr ((void *) 0x1000);
+ EXPECT_EQ (hwmem->getBaseAddr (), (void *) 0x1000);
hwmem->setBaseAddr (nullptr);
- EXPECT_EQ (hwmem->getBaseAddr(), nullptr);
+ EXPECT_EQ (hwmem->getBaseAddr (), nullptr);
hwmem->setParent (hwmem);
- EXPECT_EQ (hwmem->getParent(), hwmem);
+ EXPECT_EQ (hwmem->getParent (), hwmem);
hwmem->setParent (nullptr);
- EXPECT_EQ (hwmem->getParent(), nullptr);
+ EXPECT_EQ (hwmem->getParent (), nullptr);
delete hwmem;
}
/**
* @brief check impl methods, no impl
*/
-TEST (ne_core_hwmem_test, hwmem_no_impl_n)
-{
+TEST (ne_core_hwmem_test, hwmem_no_impl_n) {
HWmem *hwmem;
size_t size = 0x1000;
/**
* @brief check impl methods, HWmemDevice
*/
-TEST (ne_core_hwmem_test, hwmem_device)
-{
- INIT_TEST_DRIVER_API();
+TEST (ne_core_hwmem_test, hwmem_device) {
+ INIT_TEST_DRIVER_API ();
- HWmem * hwmem = new HWmem (new HWmemDevice);
+ HWmem *hwmem = new HWmem (new HWmemDevice);
size_t size = 0x1000;
- hwmem->setDriverAPI (api.get());
+ hwmem->setDriverAPI (api.get ());
EXPECT_EQ (hwmem->alloc (size), 0);
EXPECT_EQ (hwmem->getSize (), size);
/**
* @brief check impl methods, HWmemDevice, with error handling
*/
-TEST (ne_core_hwmem_test, hwmem_device_n)
-{
- INIT_TEST_DRIVER_API();
+TEST (ne_core_hwmem_test, hwmem_device_n) {
+ INIT_TEST_DRIVER_API ();
- HWmem * hwmem = new HWmem (new HWmemDevice);
+ HWmem *hwmem = new HWmem (new HWmemDevice);
size_t size = 0x1000;
/** without setting driver APIs, all methods will be failed */
/**
* @brief check alloc() method, HWmemDevice, with error handling
*/
-TEST (ne_core_hwmem_test, hwmem_device_alloc_n)
-{
- INIT_TEST_DRIVER_API();
+TEST (ne_core_hwmem_test, hwmem_device_alloc_n) {
+ INIT_TEST_DRIVER_API ();
- HWmem * hwmem = new HWmem (new HWmemDevice);
+ HWmem *hwmem = new HWmem (new HWmemDevice);
size_t size = 0x1000;
- hwmem->setDriverAPI (api.get());
+ hwmem->setDriverAPI (api.get ());
/** size zero is ignored */
EXPECT_NE (hwmem->alloc (0), 0);
/**
* @brief check dealloc() method, HWmemDevice, with error handling
*/
-TEST (ne_core_hwmem_test, hwmem_device_dealloc_n)
-{
- INIT_TEST_DRIVER_API();
+TEST (ne_core_hwmem_test, hwmem_device_dealloc_n) {
+ INIT_TEST_DRIVER_API ();
- HWmem * hwmem = new HWmem (new HWmemDevice);
+ HWmem *hwmem = new HWmem (new HWmemDevice);
size_t size = 0x1000;
- hwmem->setDriverAPI (api.get());
+ hwmem->setDriverAPI (api.get ());
hwmem->setSize (size);
/** without allocation */
/**
* @brief check getData() method, HWmemDevice, with error handling
*/
-TEST (ne_core_hwmem_test, hwmem_device_get_data_n)
-{
- INIT_TEST_DRIVER_API();
+TEST (ne_core_hwmem_test, hwmem_device_get_data_n) {
+ INIT_TEST_DRIVER_API ();
- HWmem * hwmem = new HWmem (new HWmemDevice);
+ HWmem *hwmem = new HWmem (new HWmemDevice);
size_t size = 0x1000;
int dmabuf = 0x1234;
- hwmem->setDriverAPI (api.get());
+ hwmem->setDriverAPI (api.get ());
hwmem->setSize (size);
hwmem->setDmabuf (dmabuf);
/**
* @brief check impl methods, HWmemChunk
*/
-TEST (ne_core_hwmem_test, hwmem_chunk)
-{
- INIT_TEST_DRIVER_API();
+TEST (ne_core_hwmem_test, hwmem_chunk) {
+ INIT_TEST_DRIVER_API ();
- HWmem * parent = new HWmem (new HWmemDevice);
+ HWmem *parent = new HWmem (new HWmemDevice);
size_t size = 0x1000;
/** parent should reserve memory first */
- parent->setDriverAPI (api.get());
+ parent->setDriverAPI (api.get ());
EXPECT_EQ (parent->alloc (size), 0);
EXPECT_NE (parent->getData (), nullptr);
/** chunk just sets its offset/size */
- HWmem * chunk;
+ HWmem *chunk;
chunk = new HWmem (new HWmemChunk);
chunk->setParent (parent);
- chunk->setOffset (size/2);
- chunk->setSize (size/2);
+ chunk->setOffset (size / 2);
+ chunk->setSize (size / 2);
EXPECT_NE (chunk->getData (), nullptr);
EXPECT_EQ (chunk->getData (), parent->getData () + chunk->getOffset ());
/** alloc () also works after setting offset */
chunk = new HWmem (new HWmemChunk);
chunk->setParent (parent);
- chunk->setOffset (size/2);
- EXPECT_EQ (chunk->alloc (size/2), 0);
+ chunk->setOffset (size / 2);
+ EXPECT_EQ (chunk->alloc (size / 2), 0);
EXPECT_NE (chunk->getData (), nullptr);
EXPECT_EQ (chunk->getData (), parent->getData () + chunk->getOffset ());
/**
* @brief check impl methods, HWmemChunk, with error handling
*/
-TEST (ne_core_hwmem_test, hwmem_chunk_n)
-{
- INIT_TEST_DRIVER_API();
+TEST (ne_core_hwmem_test, hwmem_chunk_n) {
+ INIT_TEST_DRIVER_API ();
size_t size = 0x1000;
- HWmem * chunk;
+ HWmem *chunk;
/** access chunk without parent */
chunk = new HWmem (new HWmemChunk);
chunk->setSize (0);
/** access chunk with parent, but not api set */
- HWmem * parent;
+ HWmem *parent;
parent = new HWmem (new HWmemDevice);
chunk->setParent (parent);
chunk->setSize (0);
/** access chunk with parent, but not allocated */
- parent->setDriverAPI (api.get());
+ parent->setDriverAPI (api.get ());
EXPECT_NE (chunk->alloc (size), 0);
EXPECT_EQ (chunk->getData (), nullptr);
/**
* @brief check impl methods, HWmemExternal
*/
-TEST (ne_core_hwmem_test, hwmem_external)
-{
- INIT_TEST_DRIVER_API();
+TEST (ne_core_hwmem_test, hwmem_external) {
+ INIT_TEST_DRIVER_API ();
size_t size = 0x1000;
- HWmem * hwmem;
+ HWmem *hwmem;
hwmem = new HWmem (new HWmemDevice);
- hwmem->setDriverAPI (api.get());
+ hwmem->setDriverAPI (api.get ());
ASSERT_EQ (hwmem->alloc (size), 0);
- char * buf = hwmem->getData ();
- for (size_t i = 0; i < size; i++)
- buf[i] = i % INT8_MAX;
+ char *buf = hwmem->getData ();
+ for (size_t i = 0; i < size; i++) buf[i] = i % INT8_MAX;
- HWmem * external;
+ HWmem *external;
external = new HWmem (new HWmemExternal);
- external->setDriverAPI (api.get());
+ external->setDriverAPI (api.get ());
external->setSize (hwmem->getSize ());
external->setDmabuf (hwmem->getDmabuf ());
buf = external->getData ();
EXPECT_NE (external->getData (), nullptr);
- for (size_t i = 0; i < size; i++)
- EXPECT_EQ (buf[i], (char) (i % INT8_MAX));
+ for (size_t i = 0; i < size; i++) EXPECT_EQ (buf[i], (char) (i % INT8_MAX));
delete hwmem;
delete external;
/**
* @brief check impl methods, HWmemExternal, with error handling
*/
-TEST (ne_core_hwmem_test, hwmem_external_n)
-{
- INIT_TEST_DRIVER_API();
+TEST (ne_core_hwmem_test, hwmem_external_n) {
+ INIT_TEST_DRIVER_API ();
size_t size = 0x1000;
- HWmem * hwmem;
+ HWmem *hwmem;
hwmem = new HWmem (new HWmemDevice);
- hwmem->setDriverAPI (api.get());
+ hwmem->setDriverAPI (api.get ());
ASSERT_EQ (hwmem->alloc (size), 0);
- HWmem * external;
+ HWmem *external;
external = new HWmem (new HWmemExternal);
EXPECT_NE (hwmem->getData (), external->getData ());
- external->setDriverAPI (api.get());
+ external->setDriverAPI (api.get ());
EXPECT_NE (hwmem->getData (), external->getData ());
external->setSize (hwmem->getSize ());
* @brief main function for unit test
*/
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
return start_gtest (argc, argv);
}
/**
* @brief test single instances
*/
-TEST (ne_core_inputservice_test, instance_singleton)
-{
- HostInputService & host_is = HostInputService::getInstance ();
- HostInputService & host_is2 = HostInputService::getInstance ();
+TEST (ne_core_inputservice_test, instance_singleton) {
+ HostInputService &host_is = HostInputService::getInstance ();
+ HostInputService &host_is2 = HostInputService::getInstance ();
EXPECT_EQ (&host_is, &host_is2);
- HwInputService & hw_is = HwInputService::getInstance ();
- HwInputService & hw_is2 = HwInputService::getInstance ();
+ HwInputService &hw_is = HwInputService::getInstance ();
+ HwInputService &hw_is2 = HwInputService::getInstance ();
EXPECT_EQ (&hw_is, &hw_is2);
}
/**
* @brief test features of submit() with a buffer
*/
-TEST (ne_core_inputservice_test, host_submit)
-{
+TEST (ne_core_inputservice_test, host_submit) {
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
/** create dummy model & buffer */
- Model * model = new Model (new HWmemDevice);
- model->setDriverAPI (api.get());
+ Model *model = new Model (new HWmemDevice);
+ model->setDriverAPI (api.get ());
EXPECT_EQ (model->alloc (4096), 0);
- Buffer * buffer = new Buffer (new HWmemDevice);
- buffer->setDriverAPI (api.get());
+ Buffer *buffer = new Buffer (new HWmemDevice);
+ buffer->setDriverAPI (api.get ());
EXPECT_EQ (buffer->alloc (4096), 0);
- HostInputService & service = HostInputService::getInstance ();
- EXPECT_EQ (service.submit (api.get(), 1, model, buffer), 0);
+ HostInputService &service = HostInputService::getInstance ();
+ EXPECT_EQ (service.submit (api.get (), 1, model, buffer), 0);
usleep (TEST_SLEEP_MS);
std::mutex m;
auto callback = std::bind (test_callback, &num_called, &m, &cv);
- EXPECT_EQ (service.submit (api.get(), 2, model, buffer, callback), 0);
- EXPECT_EQ (service.submit (api.get(), 3, model, buffer, callback), 0);
- EXPECT_EQ (service.submit (api.get(), 4, model, buffer, callback), 0);
+ EXPECT_EQ (service.submit (api.get (), 2, model, buffer, callback), 0);
+ EXPECT_EQ (service.submit (api.get (), 3, model, buffer, callback), 0);
+ EXPECT_EQ (service.submit (api.get (), 4, model, buffer, callback), 0);
wait_callbacks (num_called, 3, m, cv);
EXPECT_EQ (num_called, 3);
/**
* @brief test features of submit() with error handling
*/
-TEST (ne_core_inputservice_test, host_submit_args_n)
-{
+TEST (ne_core_inputservice_test, host_submit_args_n) {
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
/** create dummy model */
- Model * model = new Model (new HWmemDevice);
- model->setDriverAPI (api.get());
+ Model *model = new Model (new HWmemDevice);
+ model->setDriverAPI (api.get ());
EXPECT_EQ (model->alloc (4096), 0);
- HostInputService & service = HostInputService::getInstance ();
+ HostInputService &service = HostInputService::getInstance ();
- Buffer * buffer = new Buffer (new HWmemDevice);
- buffer->setDriverAPI (api.get());
+ Buffer *buffer = new Buffer (new HWmemDevice);
+ buffer->setDriverAPI (api.get ());
EXPECT_EQ (buffer->alloc (4096), 0);
EXPECT_NE (service.submit (nullptr, 0, nullptr, buffer), 0);
EXPECT_NE (service.submit (nullptr, 0, model, buffer), 0);
- EXPECT_EQ (service.submit (api.get(), 0, nullptr, buffer), 0);
- EXPECT_EQ (service.submit (api.get(), 1, model, buffer), 0);
+ EXPECT_EQ (service.submit (api.get (), 0, nullptr, buffer), 0);
+ EXPECT_EQ (service.submit (api.get (), 1, model, buffer), 0);
- SegmentTable * segt = new SegmentTable (new HWmemDevice);
- segt->setDriverAPI (api.get());
+ SegmentTable *segt = new SegmentTable (new HWmemDevice);
+ segt->setDriverAPI (api.get ());
EXPECT_EQ (segt->alloc (4096), 0);
EXPECT_NE (service.submit (nullptr, 2, nullptr, segt), 0);
EXPECT_NE (service.submit (nullptr, 2, model, segt), 0);
- EXPECT_NE (service.submit (api.get(), 2, nullptr, segt), 0);
- EXPECT_EQ (service.submit (api.get(), 2, model, segt), 0);
+ EXPECT_NE (service.submit (api.get (), 2, nullptr, segt), 0);
+ EXPECT_EQ (service.submit (api.get (), 2, model, segt), 0);
usleep (TEST_SLEEP_MS);
delete segt;
}
-class TestInputService : public InputService
-{
- public:
- TestInputService () {};
+class TestInputService : public InputService {
+ public:
+ TestInputService (){};
};
/**
* @brief test features of submit() with unsupported input service
*/
-TEST (ne_core_inputservice_test, host_submit_unsupported_n)
-{
- InputService * service = new TestInputService;
+TEST (ne_core_inputservice_test, host_submit_unsupported_n) {
+ InputService *service = new TestInputService;
EXPECT_NE (service->submit (nullptr, 0, nullptr, (Buffer *) nullptr), 0);
- EXPECT_NE (service->submit (nullptr, 0, nullptr, (SegmentTable *) nullptr), 0);
+ EXPECT_NE (service->submit (nullptr, 0, nullptr, (SegmentTable *) nullptr),
+ 0);
delete service;
}
/**
* @brief test features of submit() with error handling
*/
-TEST (ne_core_inputservice_test, host_submit_same_id_n)
-{
+TEST (ne_core_inputservice_test, host_submit_same_id_n) {
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
/** create dummy model */
- Model * model = new Model (new HWmemDevice);
- model->setDriverAPI (api.get());
+ Model *model = new Model (new HWmemDevice);
+ model->setDriverAPI (api.get ());
EXPECT_EQ (model->alloc (4096), 0);
- HostInputService & service = HostInputService::getInstance ();
+ HostInputService &service = HostInputService::getInstance ();
int num_called = 0;
std::condition_variable cv;
auto callback = std::bind (test_callback_sleep, &num_called, &m, &cv);
/** saturate requests up to the maximum (in thread pool) */
- uint32_t num_threads = ThreadPool::getInstance().getNumThreads();
+ uint32_t num_threads = ThreadPool::getInstance ().getNumThreads ();
- Buffer * buffer = new Buffer (new HWmemDevice);
- buffer->setDriverAPI (api.get());
+ Buffer *buffer = new Buffer (new HWmemDevice);
+ buffer->setDriverAPI (api.get ());
EXPECT_EQ (buffer->alloc (4096), 0);
for (uint32_t i = 0; i < num_threads + 1; i++)
- EXPECT_EQ (service.submit (api.get(), i, model, buffer, callback), 0);
+ EXPECT_EQ (service.submit (api.get (), i, model, buffer, callback), 0);
/** there's a pending task with the same ID */
- EXPECT_NE (service.submit (api.get(), num_threads, model, buffer, callback), 0);
- EXPECT_NE (service.submit (api.get(), num_threads, model, buffer, callback), 0);
+ EXPECT_NE (service.submit (api.get (), num_threads, model, buffer, callback),
+ 0);
+ EXPECT_NE (service.submit (api.get (), num_threads, model, buffer, callback),
+ 0);
wait_callbacks (num_called, num_threads + 1, m, cv);
/**
* @brief test features of remove() in HostInputService
*/
-TEST (ne_core_inputservice_test, host_remove)
-{
+TEST (ne_core_inputservice_test, host_remove) {
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
/** create dummy model */
- Model * model = new Model (new HWmemDevice);
- model->setDriverAPI (api.get());
+ Model *model = new Model (new HWmemDevice);
+ model->setDriverAPI (api.get ());
EXPECT_EQ (model->alloc (4096), 0);
- HostInputService & service = HostInputService::getInstance ();
+ HostInputService &service = HostInputService::getInstance ();
int num_called = 0;
std::condition_variable cv;
auto callback = std::bind (test_callback_sleep, &num_called, &m, &cv);
/** saturate requests up to the maximum (in thread pool) */
- uint32_t num_threads = ThreadPool::getInstance().getNumThreads();
+ uint32_t num_threads = ThreadPool::getInstance ().getNumThreads ();
- Buffer * buffer = new Buffer (new HWmemDevice);
- buffer->setDriverAPI (api.get());
+ Buffer *buffer = new Buffer (new HWmemDevice);
+ buffer->setDriverAPI (api.get ());
EXPECT_EQ (buffer->alloc (4096), 0);
for (uint32_t i = 0; i < num_threads + 1; i++)
- EXPECT_EQ (service.submit (api.get(), i, model, buffer, callback), 0);
+ EXPECT_EQ (service.submit (api.get (), i, model, buffer, callback), 0);
/** remove un-resolved task */
EXPECT_EQ (service.remove (num_threads), 0);
/**
* @brief test features of remove() with error handling
*/
-TEST (ne_core_inputservice_test, host_remove_n)
-{
- HostInputService & service = HostInputService::getInstance ();
+TEST (ne_core_inputservice_test, host_remove_n) {
+ HostInputService &service = HostInputService::getInstance ();
/** no such tasks */
EXPECT_NE (service.remove (0), 0);
/**
* @brief test features of HW inputservice's submit()
*/
-TEST (ne_core_inputservice_test, hw_recurring_submit)
-{
+TEST (ne_core_inputservice_test, hw_recurring_submit) {
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
/** create dummy model */
- Model * model = new Model (new HWmemDevice);
- model->setDriverAPI (api.get());
+ Model *model = new Model (new HWmemDevice);
+ model->setDriverAPI (api.get ());
EXPECT_EQ (model->alloc (4096), 0);
- HwInputService & service = HwInputService::getInstance ();
+ HwInputService &service = HwInputService::getInstance ();
int num_called = 0;
std::condition_variable cv;
auto callback = std::bind (test_callback_sleep, &num_called, &m, &cv);
- SegmentTable * segt = new SegmentTable (new HWmemDevice);
- segt->setDriverAPI (api.get());
+ SegmentTable *segt = new SegmentTable (new HWmemDevice);
+ segt->setDriverAPI (api.get ());
EXPECT_EQ (segt->alloc (4096), 0);
- EXPECT_EQ (service.submit (api.get(), 0, model, segt, callback), 0);
- EXPECT_EQ (service.submit (api.get(), 1, model, segt, callback), 0);
- EXPECT_EQ (service.submit (api.get(), 2, model, segt, nullptr), 0);
+ EXPECT_EQ (service.submit (api.get (), 0, model, segt, callback), 0);
+ EXPECT_EQ (service.submit (api.get (), 1, model, segt, callback), 0);
+ EXPECT_EQ (service.submit (api.get (), 2, model, segt, nullptr), 0);
/** remove is unsupported (it's designed to handle high priority tasks) */
EXPECT_NE (service.remove (0), 0);
/**
* @brief test features of HW inputservice with error handling
*/
-TEST (ne_core_inputservice_test, hw_recurring_n)
-{
+TEST (ne_core_inputservice_test, hw_recurring_n) {
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
/** create dummy model */
- Model * model = new Model (new HWmemDevice);
- model->setDriverAPI (api.get());
+ Model *model = new Model (new HWmemDevice);
+ model->setDriverAPI (api.get ());
EXPECT_EQ (model->alloc (4096), 0);
- HwInputService & service = HwInputService::getInstance ();
+ HwInputService &service = HwInputService::getInstance ();
int num_called = 0;
std::condition_variable cv;
auto callback = std::bind (test_callback_sleep, &num_called, &m, &cv);
- SegmentTable * segt = new SegmentTable (new HWmemDevice);
- segt->setDriverAPI (api.get());
+ SegmentTable *segt = new SegmentTable (new HWmemDevice);
+ segt->setDriverAPI (api.get ());
EXPECT_EQ (segt->alloc (4096), 0);
EXPECT_NE (service.submit (nullptr, 0, nullptr, segt, nullptr), 0);
- EXPECT_NE (service.submit (api.get(), 0, nullptr, segt, callback), 0);
- EXPECT_EQ (service.submit (api.get(), 0, model, segt, callback), 0);
+ EXPECT_NE (service.submit (api.get (), 0, nullptr, segt, callback), 0);
+ EXPECT_EQ (service.submit (api.get (), 0, model, segt, callback), 0);
/** remove is unsupported (it's designed to handle high priority tasks) */
EXPECT_NE (service.remove (0), 0);
* @brief main function for unit test
*/
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
return start_gtest (argc, argv);
}
/**
* @brief test primitives of memory allocator default (MemDefault)
*/
-TEST (ne_core_mem_test, mem_default_primitives)
-{
- std::unique_ptr<DriverAPI> api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
+TEST (ne_core_mem_test, mem_default_primitives) {
+ std::unique_ptr<DriverAPI> api =
+ DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
ASSERT_NE (api.get (), nullptr);
/**
std::unique_ptr<MemAllocator> mem = MemAllocator::createInstance (api.get ());
EXPECT_NE (mem.get (), nullptr);
- void * addr = nullptr;
+ void *addr = nullptr;
int dmabuf = mem->allocMemory (4 * K, &addr);
EXPECT_GE (dmabuf, 0);
EXPECT_NE (addr, nullptr);
EXPECT_EQ (mem->deallocMemory (dmabuf, 4 * K, addr), 0);
- Model * model = mem->allocModel (new HWmemDevice);
+ Model *model = mem->allocModel (new HWmemDevice);
EXPECT_NE (model, nullptr);
delete model;
- Buffer * buffer = mem->allocBuffer (new HWmemDevice);
+ Buffer *buffer = mem->allocBuffer (new HWmemDevice);
EXPECT_NE (buffer, nullptr);
delete buffer;
}
/**
* @brief test primitives of memory allocator (unimplemented)
*/
-TEST (ne_core_mem_test, mem_pool_primitives_n)
-{
- std::unique_ptr<DriverAPI> api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
+TEST (ne_core_mem_test, mem_pool_primitives_n) {
+ std::unique_ptr<DriverAPI> api =
+ DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
ASSERT_NE (api.get (), nullptr);
/** 'resv_mem_size' > 0: MemPool (not implemented yet) */
std::unique_ptr<MemAllocator> mem = MemAllocator::createInstance (api.get ());
EXPECT_NE (mem.get (), nullptr);
- void * addr;
+ void *addr;
int dmabuf;
dmabuf = mem->allocMemory (4096, &addr);
/**
* @brief test createInstance() with error handling
*/
-TEST (ne_core_mem_test, create_instance_no_api_n)
-{
+TEST (ne_core_mem_test, create_instance_no_api_n) {
std::unique_ptr<MemAllocator> mem = MemAllocator::createInstance (nullptr);
EXPECT_EQ (mem.get (), nullptr);
}
* @brief main function for unit test
*/
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
return start_gtest (argc, argv);
}
/**
* @brief check set/get primitives of model
*/
-TEST (ne_core_model_test, model_primitives)
-{
+TEST (ne_core_model_test, model_primitives) {
const uint32_t exp_num_tensors_v1 = 1;
const uint64_t exp_size_input = 2048;
const uint64_t exp_size_output = 2048;
- std::unique_ptr <Model> model (new Model (new HWmemDevice));
+ std::unique_ptr<Model> model (new Model (new HWmemDevice));
EXPECT_GT (model->getID (), (uint32_t) 0);
/** make dummy metadata */
npubin_meta data;
- data.magiccode = NPUBIN_MAGICCODE | 0x1; /* v1 */
+ data.magiccode = NPUBIN_MAGICCODE | 0x1; /* v1 */
data.size = 4096;
data.buffer_size = 4096;
data.input_size = exp_size_input;
EXPECT_EQ (model->setMetadata (&data), 0);
EXPECT_NE (model->getMetadata (), nullptr);
- EXPECT_EQ (model->getInputTensorNum (), exp_num_tensors_v1); /* v1 has one input/output tensors */
+ EXPECT_EQ (model->getInputTensorNum (),
+ exp_num_tensors_v1); /* v1 has one input/output tensors */
EXPECT_EQ (model->getOutputTensorNum (), exp_num_tensors_v1);
EXPECT_EQ (model->getInputTensorSize (0), exp_size_input);
- EXPECT_EQ (model->getOutputTensorSize (0),exp_size_output);
+ EXPECT_EQ (model->getOutputTensorSize (0), exp_size_output);
/** check default values */
npuConstraint constraint = model->getConstraint ();
EXPECT_EQ (constraint.timeout_ms, default_timeout);
EXPECT_EQ (constraint.priority, default_priority);
- const tensor_data_info * in = model->getInputDataInfo (0);
+ const tensor_data_info *in = model->getInputDataInfo (0);
EXPECT_NE (in, nullptr);
EXPECT_EQ (in->layout, default_data_layout);
EXPECT_EQ (in->type, default_data_type);
- const tensor_data_info * out = model->getOutputDataInfo (0);
+ const tensor_data_info *out = model->getOutputDataInfo (0);
EXPECT_NE (out, nullptr);
EXPECT_EQ (out->layout, default_data_layout);
EXPECT_EQ (out->type, default_data_type);
/**
* @brief check set/get primitives of model with error handling
*/
-TEST (ne_core_model_test, model_primitives_invalid_args_n)
-{
- std::unique_ptr <Model> model (new Model (new HWmemDevice));
+TEST (ne_core_model_test, model_primitives_invalid_args_n) {
+ std::unique_ptr<Model> model (new Model (new HWmemDevice));
const uint64_t exp_size_input = 2048;
const uint64_t exp_size_output = 2048;
/** make dummy metadata */
npubin_meta data;
- data.magiccode = NPUBIN_MAGICCODE | 0x1; /* v1 */
+ data.magiccode = NPUBIN_MAGICCODE | 0x1; /* v1 */
data.size = 4096;
data.buffer_size = 4096;
data.input_size = exp_size_input;
/**
* @brief check set/get primitives of model with error handling
*/
-TEST (ne_core_model_test, model_primitives_no_metadata_n)
-{
- std::unique_ptr <Model> model (new Model (new HWmemDevice));
+TEST (ne_core_model_test, model_primitives_no_metadata_n) {
+ std::unique_ptr<Model> model (new Model (new HWmemDevice));
/** access info without setting metadata */
EXPECT_EQ (model->getMetadata (), nullptr);
/**
* @brief check setMetadata with invalid contents
*/
-TEST (ne_core_model_test, model_set_metadata_n)
-{
- std::unique_ptr <Model> model (new Model (new HWmemDevice));
+TEST (ne_core_model_test, model_set_metadata_n) {
+ std::unique_ptr<Model> model (new Model (new HWmemDevice));
/** start with the valid metadata */
npubin_meta data;
- data.magiccode = NPUBIN_MAGICCODE | 0x1; /* v1 */
+ data.magiccode = NPUBIN_MAGICCODE | 0x1; /* v1 */
data.size = 8192;
data.program_size = 2048;
data.weight_size = 2048;
EXPECT_NE (model->setMetadata (&data), 0);
/** unsupported version */
- data.magiccode = NPUBIN_MAGICCODE | 0x9; /* v9 ?? */
+ data.magiccode = NPUBIN_MAGICCODE | 0x9; /* v9 ?? */
EXPECT_NE (model->setMetadata (&data), 0);
/** invalid number of tensors */
- data.magiccode = NPUBIN_MAGICCODE | 0x2; /* v2 */
+ data.magiccode = NPUBIN_MAGICCODE | 0x2; /* v2 */
data.input_num = MAX_TENSORS + 1;
EXPECT_NE (model->setMetadata (&data), 0);
data.input_num = 1;
EXPECT_NE (model->setMetadata (&data), 0); /* size overflow */
/** invalid segment num or index */
- data.magiccode = NPUBIN_MAGICCODE | 0x3; /* v3 */
+ data.magiccode = NPUBIN_MAGICCODE | 0x3; /* v3 */
data.segment_num = MAX_SEGMENTS + 1;
EXPECT_NE (model->setMetadata (&data), 0);
data.segment_num = MAX_SEGMENTS / 2;
/**
* @brief check get_data_size
*/
-TEST (ne_core_model_test, get_data_size)
-{
+TEST (ne_core_model_test, get_data_size) {
EXPECT_EQ (get_data_size (DATA_TYPE_SRNPU), (uint32_t) 1);
EXPECT_EQ (get_data_size (DATA_TYPE_INT8), (uint32_t) 1);
EXPECT_EQ (get_data_size (DATA_TYPE_UINT8), (uint32_t) 1);
/**
* @brief check set/get primitives of metadata v1
*/
-TEST (ne_core_model_test, metadata_primitives_v1)
-{
- std::unique_ptr <Model> model (new Model (new HWmemDevice));
+TEST (ne_core_model_test, metadata_primitives_v1) {
+ std::unique_ptr<Model> model (new Model (new HWmemDevice));
/** make dummy metadata v1 */
npubin_meta data;
const uint32_t exp_meta_size_v1 = 4096;
const uint32_t exp_num_tensors_v1 = 1;
- data.magiccode = NPUBIN_MAGICCODE | 0x1; /* v1 */
+ data.magiccode = NPUBIN_MAGICCODE | 0x1; /* v1 */
data.size = 8192;
data.buffer_size = 4096;
data.program_size = 2048;
EXPECT_EQ (model->setMetadata (&data), 0);
- const Metadata * meta = model->getMetadata ();
+ const Metadata *meta = model->getMetadata ();
EXPECT_NE (meta, nullptr);
EXPECT_EQ (meta->getVersion (), 1);
EXPECT_EQ (meta->getInputOffset (0), data.input_offset);
EXPECT_EQ (meta->getInputTensorSize (0, DATA_LAYOUT_SRNPU), data.input_size);
EXPECT_EQ (meta->getOutputOffset (0), data.output_offset);
- EXPECT_EQ (meta->getOutputTensorSize (0, DATA_LAYOUT_SRNPU), data.output_size);
+ EXPECT_EQ (meta->getOutputTensorSize (0, DATA_LAYOUT_SRNPU),
+ data.output_size);
/** return default values for unsupported info */
EXPECT_EQ (meta->getInputElemSize (0), (uint32_t) 1);
EXPECT_EQ (meta->getOutputElemSize (0), (uint32_t) 1);
- EXPECT_EQ (meta->getInputDims (0)[0], meta->getInputTensorSize (0, default_data_layout));
- EXPECT_EQ (meta->getOutputDims (0)[0], meta->getOutputTensorSize (0, default_data_layout));
+ EXPECT_EQ (meta->getInputDims (0)[0],
+ meta->getInputTensorSize (0, default_data_layout));
+ EXPECT_EQ (meta->getOutputDims (0)[0],
+ meta->getOutputTensorSize (0, default_data_layout));
EXPECT_EQ (meta->getInputQuantZero (0), default_quant_zero);
EXPECT_EQ (meta->getOutputQuantZero (0), default_quant_zero);
EXPECT_FLOAT_EQ (meta->getInputQuantScale (0), default_quant_scale);
EXPECT_EQ (meta->getOutputSegmentIndex (0), (uint32_t) 0);
/** v0 also works */
- data.magiccode = NPUBIN_MAGICCODE; /* v0 --> v1 */
+ data.magiccode = NPUBIN_MAGICCODE; /* v0 --> v1 */
EXPECT_EQ (model->setMetadata (&data), 0);
EXPECT_NE (model->getMetadata (), nullptr);
EXPECT_EQ (model->getMetadata ()->getVersion (), 1);
/**
* @brief check set/get primitives of metadata v2
*/
-TEST (ne_core_model_test, metadata_primitives_v2)
-{
- std::unique_ptr <Model> model (new Model (new HWmemDevice));
+TEST (ne_core_model_test, metadata_primitives_v2) {
+ std::unique_ptr<Model> model (new Model (new HWmemDevice));
/** make dummy metadata v2 */
npubin_meta data;
- data.magiccode = NPUBIN_MAGICCODE | 0x2; /* v2 */
+ data.magiccode = NPUBIN_MAGICCODE | 0x2; /* v2 */
data.size = 8192;
data.buffer_size = 4096;
data.program_size = 2048;
for (uint32_t i = 0; i < data.input_num; i++) {
data.input_offsets[i] = 100 * (i + 1);
data.input_elem_size[i] = 1 * (i + 1);
- for (uint32_t j = 0; j < MAX_RANK; j++)
- data.input_dims[i][j] = 2 * (i + 1);
+ for (uint32_t j = 0; j < MAX_RANK; j++) data.input_dims[i][j] = 2 * (i + 1);
data.input_quant_z[i] = 3 * (i + 1);
data.input_quant_s[i] = 4 * (i + 1);
}
EXPECT_EQ (model->setMetadata (&data), 0);
- const Metadata * meta = model->getMetadata ();
+ const Metadata *meta = model->getMetadata ();
EXPECT_NE (meta, nullptr);
EXPECT_EQ (meta->getVersion (), 2);
- EXPECT_EQ (meta->getBufferSize(), data.buffer_size);
- EXPECT_EQ (meta->getProgramSize(), data.program_size);
- EXPECT_EQ (meta->getWeightSize(), data.weight_size);
+ EXPECT_EQ (meta->getBufferSize (), data.buffer_size);
+ EXPECT_EQ (meta->getProgramSize (), data.program_size);
+ EXPECT_EQ (meta->getWeightSize (), data.weight_size);
EXPECT_EQ (meta->getInputNum (), input_num);
for (uint32_t i = 0; i < meta->getInputNum (); i++) {
EXPECT_EQ (meta->getInputOffset (i), 100 * (i + 1));
EXPECT_EQ (meta->getInputElemSize (i), 1 * (i + 1));
for (uint32_t j = 0; j < MAX_RANK; j++)
- EXPECT_EQ (meta->getInputDims(i)[j], 2 * (i + 1));
+ EXPECT_EQ (meta->getInputDims (i)[j], 2 * (i + 1));
EXPECT_EQ (meta->getInputQuantZero (i), 3 * (i + 1));
EXPECT_EQ (meta->getInputQuantScale (i), 4 * (i + 1));
}
EXPECT_EQ (meta->getOutputOffset (i), 100 * (i + 1));
EXPECT_EQ (meta->getOutputElemSize (i), 1 * (i + 1));
for (uint32_t j = 0; j < MAX_RANK; j++)
- EXPECT_EQ (meta->getOutputDims(i)[j], 2 * (i + 1));
+ EXPECT_EQ (meta->getOutputDims (i)[j], 2 * (i + 1));
EXPECT_EQ (meta->getOutputQuantZero (i), 3 * (i + 1));
EXPECT_EQ (meta->getOutputQuantScale (i), 4 * (i + 1));
}
/**
* @brief check set/get primitives of metadata v3
*/
-TEST (ne_core_model_test, metadata_primitives_v3)
-{
- std::unique_ptr <Model> model (new Model (new HWmemDevice));
+TEST (ne_core_model_test, metadata_primitives_v3) {
+ std::unique_ptr<Model> model (new Model (new HWmemDevice));
/** make dummy metadata v3 */
npubin_meta data;
- data.magiccode = NPUBIN_MAGICCODE | 0x3; /* v3 */
+ data.magiccode = NPUBIN_MAGICCODE | 0x3; /* v3 */
data.size = 8192;
data.buffer_size = 4096;
data.program_size = 2048;
uint32_t output_num = 4;
data.segment_num = input_num + output_num + 1;
- for (uint32_t i = 0; i < data.segment_num; i++)
- data.segment_size[i] = 0x1000;
+ for (uint32_t i = 0; i < data.segment_num; i++) data.segment_size[i] = 0x1000;
data.input_seg_num = input_num;
for (uint32_t i = 0; i < data.input_seg_num; i++) {
EXPECT_EQ (model->setMetadata (&data), 0);
- const Metadata * meta = model->getMetadata ();
+ const Metadata *meta = model->getMetadata ();
EXPECT_NE (meta, nullptr);
EXPECT_EQ (meta->getVersion (), 3);
- EXPECT_EQ (meta->getBufferSize(), data.buffer_size);
- EXPECT_EQ (meta->getProgramSize(), data.program_size);
- EXPECT_EQ (meta->getWeightSize(), data.weight_size);
+ EXPECT_EQ (meta->getBufferSize (), data.buffer_size);
+ EXPECT_EQ (meta->getProgramSize (), data.program_size);
+ EXPECT_EQ (meta->getWeightSize (), data.weight_size);
EXPECT_EQ (meta->getInputNum (), input_num);
for (uint32_t i = 0; i < meta->getInputNum (); i++) {
EXPECT_EQ (meta->getInputOffset (i), 0);
EXPECT_EQ (meta->getInputElemSize (i), get_data_size (DATA_TYPE_QASYMM8));
for (uint32_t j = 0; j < MAX_RANK; j++) {
- EXPECT_EQ (meta->getInputDims(i)[j], 2 * (i + 1));
- size *= meta->getInputDims(i)[j];
+ EXPECT_EQ (meta->getInputDims (i)[j], 2 * (i + 1));
+ size *= meta->getInputDims (i)[j];
}
EXPECT_EQ (meta->getInputQuantZero (i), 3 * (i + 1));
EXPECT_EQ (meta->getInputQuantScale (i), 4 * (i + 1));
EXPECT_EQ (meta->getOutputOffset (i), 0);
EXPECT_EQ (meta->getOutputElemSize (i), get_data_size (DATA_TYPE_QSYMM16));
for (uint32_t j = 0; j < MAX_RANK; j++) {
- EXPECT_EQ (meta->getOutputDims(i)[j], 2 * (i + 1));
- size *= meta->getOutputDims(i)[j];
+ EXPECT_EQ (meta->getOutputDims (i)[j], 2 * (i + 1));
+ size *= meta->getOutputDims (i)[j];
}
EXPECT_EQ (meta->getOutputQuantZero (i), 3 * (i + 1));
EXPECT_EQ (meta->getOutputQuantScale (i), 4 * (i + 1));
/**
* @brief model alloc without driver api
*/
-TEST (ne_core_model_test, model_alloc_no_drv_api_n)
-{
- std::unique_ptr <Model> model (new Model (new HWmemDevice));
+TEST (ne_core_model_test, model_alloc_no_drv_api_n) {
+ std::unique_ptr<Model> model (new Model (new HWmemDevice));
EXPECT_EQ (model->getDriverAPI (), nullptr);
EXPECT_NE (model->alloc (4096), 0);
* @brief main function for unit test
*/
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
return start_gtest (argc, argv);
}
/**
* @brief check getNumDevices()
*/
-TEST (ne_core_npu_test, get_num_devices)
-{
+TEST (ne_core_npu_test, get_num_devices) {
int num_devices;
/** supported device types (currently) */
/**
* @brief check getNumDevices() for invalid types
*/
-TEST (ne_core_npu_test, get_num_devices_invalid_n)
-{
+TEST (ne_core_npu_test, get_num_devices_invalid_n) {
int num_devices;
/** not supported device types */
/**
* @brief check getNumDevices() for deprecated devices
*/
-TEST (ne_core_npu_test, get_num_devices_deprecated_n)
-{
+TEST (ne_core_npu_test, get_num_devices_deprecated_n) {
int num_devices;
num_devices = DriverAPI::getNumDevices (NPUCOND_TRIV_CONN_UNKNOWN);
/**
* @brief check device statuses
*/
-TEST (ne_core_npu_test, check_dev_status_triv2)
-{
+TEST (ne_core_npu_test, check_dev_status_triv2) {
int num_devices = DriverAPI::getNumDevices (NPUCOND_TRIV2_CONN_SOCIP);
ASSERT_GT (num_devices, 0);
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- EXPECT_NE (api.get(), nullptr);
- EXPECT_GE (api->getDeviceFD(), 0);
- EXPECT_EQ (api->getDeviceID(), 0);
- EXPECT_EQ (api->isReady(), device_state_t::STATE_READY);
+ EXPECT_NE (api.get (), nullptr);
+ EXPECT_GE (api->getDeviceFD (), 0);
+ EXPECT_EQ (api->getDeviceID (), 0);
+ EXPECT_EQ (api->isReady (), device_state_t::STATE_READY);
/** open same device */
std::unique_ptr<DriverAPI> api2;
api2 = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- EXPECT_NE (api2.get(), nullptr);
- EXPECT_GE (api2->getDeviceFD(), 0);
- EXPECT_EQ (api2->getDeviceID(), 0);
- EXPECT_EQ (api2->isReady(), device_state_t::STATE_READY);
+ EXPECT_NE (api2.get (), nullptr);
+ EXPECT_GE (api2->getDeviceFD (), 0);
+ EXPECT_EQ (api2->getDeviceID (), 0);
+ EXPECT_EQ (api2->isReady (), device_state_t::STATE_READY);
- EXPECT_EQ (api->getDeviceID(), api2->getDeviceID());
+ EXPECT_EQ (api->getDeviceID (), api2->getDeviceID ());
/** file descriptors should be different */
- EXPECT_NE (api->getDeviceFD(), api2->getDeviceFD());
+ EXPECT_NE (api->getDeviceFD (), api2->getDeviceFD ());
}
/**
* @brief check device statuses with error handling
*/
-TEST (ne_core_npu_test, check_dev_status_triv2_n)
-{
+TEST (ne_core_npu_test, check_dev_status_triv2_n) {
int num_devices = DriverAPI::getNumDevices (NPUCOND_TRIV2_CONN_SOCIP);
ASSERT_GT (num_devices, 0);
/** invalid id */
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, -1);
- EXPECT_EQ (api.get(), nullptr);
+ EXPECT_EQ (api.get (), nullptr);
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, num_devices);
- EXPECT_EQ (api.get(), nullptr);
+ EXPECT_EQ (api.get (), nullptr);
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, num_devices + 1);
- EXPECT_EQ (api.get(), nullptr);
+ EXPECT_EQ (api.get (), nullptr);
}
/**
* @brief create API instances without initailiation
*/
-TEST (ne_core_npu_test, create_instance_uninitialized_n)
-{
+TEST (ne_core_npu_test, create_instance_uninitialized_n) {
const size_t size = 4096;
model_config_t model;
input_config_t input;
/**
* @brief manage memory blocks using driver api
*/
-TEST (ne_core_npu_test, manage_mem_triv2)
-{
+TEST (ne_core_npu_test, manage_mem_triv2) {
int num_devices = DriverAPI::getNumDevices (NPUCOND_TRIV2_CONN_SOCIP);
ASSERT_GT (num_devices, 0);
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- EXPECT_NE (api.get(), nullptr);
+ EXPECT_NE (api.get (), nullptr);
size_t size = 4096;
int dmabuf = api->alloc (size);
EXPECT_NE (ptr, nullptr);
for (size_t i = 0; i < size; i++)
- static_cast<uint8_t*>(ptr)[i] = static_cast<uint8_t>(i % 256);
+ static_cast<uint8_t *> (ptr)[i] = static_cast<uint8_t> (i % 256);
EXPECT_EQ (api->munmap (ptr, size), 0);
EXPECT_NE (ptr, nullptr);
for (size_t i = 0; i < size; i++)
- EXPECT_EQ (static_cast<uint8_t*>(ptr)[i], static_cast<uint8_t>(i % 256));
+ EXPECT_EQ (static_cast<uint8_t *> (ptr)[i], static_cast<uint8_t> (i % 256));
EXPECT_EQ (api->munmap (ptr, size), 0);
EXPECT_EQ (api->dealloc (dmabuf), 0);
/**
* @brief manage memory blocks using driver api (negative cases)
*/
-TEST (ne_core_npu_test, manage_mem_triv2_n)
-{
+TEST (ne_core_npu_test, manage_mem_triv2_n) {
int num_devices = DriverAPI::getNumDevices (NPUCOND_TRIV2_CONN_SOCIP);
ASSERT_GT (num_devices, 0);
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- EXPECT_NE (api.get(), nullptr);
+ EXPECT_NE (api.get (), nullptr);
size_t size = 4096;
int dmabuf = api->alloc (size);
/**
* @brief set model data with error handling
*/
-TEST (ne_core_npu_test, set_model_data_n)
-{
+TEST (ne_core_npu_test, set_model_data_n) {
std::unique_ptr<DriverAPI> api;
model_config_t model;
int num_devices;
num_devices = DriverAPI::getNumDevices (NPUCOND_TRIV2_CONN_SOCIP);
ASSERT_GT (num_devices, 0);
- api.reset();
+ api.reset ();
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
/** invalid arguments */
EXPECT_NE (api->registerModel (nullptr), 0);
/**
* @brief set input data with error handling
*/
-TEST (ne_core_npu_test, set_input_data_n)
-{
+TEST (ne_core_npu_test, set_input_data_n) {
std::unique_ptr<DriverAPI> api;
input_config_t input;
int num_devices;
num_devices = DriverAPI::getNumDevices (NPUCOND_TRIV2_CONN_SOCIP);
ASSERT_GT (num_devices, 0);
- api.reset();
+ api.reset ();
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
/** invalid arguments */
EXPECT_NE (api->runInput (nullptr), 0);
/**
* @brief test running inference with valid data
*/
-TEST (ne_core_npu_test, run_inference_triv2)
-{
+TEST (ne_core_npu_test, run_inference_triv2) {
const size_t size = 4096;
std::unique_ptr<DriverAPI> api;
model_config_t model;
ASSERT_GT (num_devices, 0);
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
/** register model data */
model_dmabuf = api->alloc (size);
* @brief A test for running inference with a valid model allocated from a
* separate thread
*/
-TEST (ne_core_npu_test, run_inference_async0_triv2)
-{
+TEST (ne_core_npu_test, run_inference_async0_triv2) {
const size_t size = 4096;
std::unique_ptr<DriverAPI> api;
model_config_t model;
ASSERT_GT (num_devices, 0);
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
/** register model data */
model_dmabuf = api->alloc (size);
int *buf = nullptr;
int buffer_dmabuf = -1;
- f_ret = std::async(std::launch::async,
- [&] () -> int {
- /** allocate input data */
- buffer_dmabuf = api->alloc (size);
- if (buffer_dmabuf < 0)
- return -ENOMEM;
-
- /** configure segment table */
- buf = (int *) api->mmap (buffer_dmabuf, size);
- memset (buf, 0, size);
- seg_dmabuf = api->alloc (size);
- if (seg_dmabuf < 0)
- return -ENOMEM;
- buf[0] = seg_dmabuf;
-
- /** run with this model */
- input.dbuf_fd = buffer_dmabuf;
- input.model_id = model.id;
- input.num_segments = 1;
- input.input_mode = INPUT_CPU;
- input.output_mode = OUTPUT_CPU_INTR;
- input.timeout_ms = 100;
-
- return api->runInput (&input);
- });
-
- EXPECT_GE (f_ret.get(), 0);
+ f_ret = std::async (std::launch::async, [&]() -> int {
+ /** allocate input data */
+ buffer_dmabuf = api->alloc (size);
+ if (buffer_dmabuf < 0)
+ return -ENOMEM;
+
+ /** configure segment table */
+ buf = (int *) api->mmap (buffer_dmabuf, size);
+ memset (buf, 0, size);
+ seg_dmabuf = api->alloc (size);
+ if (seg_dmabuf < 0)
+ return -ENOMEM;
+ buf[0] = seg_dmabuf;
+
+ /** run with this model */
+ input.dbuf_fd = buffer_dmabuf;
+ input.model_id = model.id;
+ input.num_segments = 1;
+ input.input_mode = INPUT_CPU;
+ input.output_mode = OUTPUT_CPU_INTR;
+ input.timeout_ms = 100;
+
+ return api->runInput (&input);
+ });
+
+ EXPECT_GE (f_ret.get (), 0);
EXPECT_EQ (api->munmap (buf, size), 0);
EXPECT_EQ (api->deregisterModel (model.id), 0);
EXPECT_EQ (api->dealloc (model_dmabuf), 0);
/**
* @brief A test for running inference using async thread with a valid model
*/
-TEST (ne_core_npu_test, run_inference_async1_triv2)
-{
+TEST (ne_core_npu_test, run_inference_async1_triv2) {
const size_t size = 4096;
std::unique_ptr<DriverAPI> api;
std::future<model_config_t> f_model;
ASSERT_GT (num_devices, 0);
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
-
- f_model = std::async(std::launch::async,
- [&] () -> model_config_t {
- int ret;
- model_config_t model = {0};
-
- /** register model data */
- ret = api->alloc (size);
- model.dbuf_fd = ret;
- if (ret < 0)
- return model;
-
- model.version = 3;
- model.program_offset_addr = 0;
- model.program_size = 0;
- model.metadata_dbuf_fd = ret; /* dummy */
-
- ret = api->registerModel (&model, TEST_NPU_VERSION);
- if (ret < 0) {
- model.dbuf_fd = -1;
- }
-
- return model;
+ ASSERT_NE (api.get (), nullptr);
+
+ f_model = std::async (std::launch::async, [&]() -> model_config_t {
+ int ret;
+ model_config_t model = {0};
+
+ /** register model data */
+ ret = api->alloc (size);
+ model.dbuf_fd = ret;
+ if (ret < 0)
+ return model;
+
+ model.version = 3;
+ model.program_offset_addr = 0;
+ model.program_size = 0;
+ model.metadata_dbuf_fd = ret; /* dummy */
+
+ ret = api->registerModel (&model, TEST_NPU_VERSION);
+ if (ret < 0) {
+ model.dbuf_fd = -1;
}
- );
+
+ return model;
+ });
/** allocate input data */
buffer_dmabuf = api->alloc (size);
/** run with this model */
input.dbuf_fd = buffer_dmabuf;
- model = f_model.get();
+ model = f_model.get ();
ASSERT_GE (model.dbuf_fd, 0);
input.model_id = model.id;
/**
* @brief test running inference with invalid segment table
*/
-TEST (ne_core_npu_test, run_inference_triv2_n)
-{
+TEST (ne_core_npu_test, run_inference_triv2_n) {
const size_t size = 8192;
std::unique_ptr<DriverAPI> api;
model_config_t model;
ASSERT_GT (num_devices, 0);
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
/** register model data */
model_dmabuf = api->alloc (size);
* @brief main function for unit test
*/
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
return start_gtest (argc, argv);
}
/**
* @brief test set/get primitives of request
*/
-TEST (ne_core_sched_test, request_set_get_primitives)
-{
+TEST (ne_core_sched_test, request_set_get_primitives) {
/** create dummy model & buffer */
std::unique_ptr<Model> model (new Model (new HWmemDevice));
std::unique_ptr<Buffer> buffer (new Buffer (new HWmemDevice));
auto callback = std::bind (test_callback, &num_called, &m, &cv);
bool force_stop = true;
- Request * req = new Request (NPUINPUT_HOST);
+ Request *req = new Request (NPUINPUT_HOST);
EXPECT_EQ (req->getOpmode (), NPUINPUT_HOST);
EXPECT_GT (req->getID (), (uint32_t) 0);
/**
* @brief test set, get api with null value
*/
-TEST (ne_core_sched_test, request_set_get_args_n)
-{
+TEST (ne_core_sched_test, request_set_get_args_n) {
bool force_stop = false;
std::string hwDevice = "";
- Request * req = new Request (NPUINPUT_HOST);
+ Request *req = new Request (NPUINPUT_HOST);
EXPECT_EQ (req->getOpmode (), NPUINPUT_HOST);
EXPECT_GT (req->getID (), (uint32_t) 0);
req->setInferData (nullptr);
req->setForceStop (force_stop);
req->setHwDevice (hwDevice);
- req->setStopped();
+ req->setStopped ();
EXPECT_EQ (req->getModel (), nullptr);
EXPECT_EQ (req->getInferData (), nullptr);
/**
* @brief test submitRequest()
*/
-TEST (ne_core_sched_test, submit_request)
-{
+TEST (ne_core_sched_test, submit_request) {
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
/** create dummy model & buffer */
std::unique_ptr<Model> model (new Model (new HWmemDevice));
std::mutex m;
auto callback = std::bind (test_callback, &num_called, &m, &cv);
- Request * req = new Request (NPUINPUT_HOST);
+ Request *req = new Request (NPUINPUT_HOST);
req->setModel (model.get ());
req->setInferData (buffer.get ());
req->setCallback (callback);
- Request * req2 = new Request (NPUINPUT_HOST);
+ Request *req2 = new Request (NPUINPUT_HOST);
req2->setModel (model.get ());
req2->setInferData (buffer.get ());
req2->setCallback (callback);
- Request * req3 = new Request (NPUINPUT_HOST);
+ Request *req3 = new Request (NPUINPUT_HOST);
req3->setModel (model.get ());
req3->setInferData (buffer.get ());
/** it's fine to have no callback */
- Scheduler * sched;
+ Scheduler *sched;
sched = new Scheduler (api.get ());
EXPECT_EQ (sched->submitRequest (req), req->getID ());
/**
* @brief test submitRequest() with error handling
*/
-TEST (ne_core_sched_test, submit_request_args_n)
-{
+TEST (ne_core_sched_test, submit_request_args_n) {
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
/** create dummy model & buffer */
std::unique_ptr<Model> model (new Model (new HWmemDevice));
std::mutex m;
auto callback = std::bind (test_callback, &num_called, &m, &cv);
- Request * req = new Request (NPUINPUT_HOST);
+ Request *req = new Request (NPUINPUT_HOST);
req->setModel (model.get ());
req->setInferData (buffer.get ());
req->setCallback (callback);
- Scheduler * sched;
+ Scheduler *sched;
/** no API provided */
sched = new Scheduler (nullptr);
EXPECT_NE (sched->submitRequest (req), 0);
/**
* @brief test submitRequest() with error handling
*/
-TEST (ne_core_sched_test, submit_request_opmode_n)
-{
+TEST (ne_core_sched_test, submit_request_opmode_n) {
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
/** create dummy model & buffer */
std::unique_ptr<Model> model (new Model (new HWmemDevice));
buffer->setDriverAPI (api.get ());
buffer->alloc (4096);
- Scheduler * sched = new Scheduler (api.get ());
+ Scheduler *sched = new Scheduler (api.get ());
- Request * req;
+ Request *req;
/** requests with not-supported opmode */
req = new Request (NPUINPUT_INTERNAL_CAM);
- req->setModel (model.get());
- req->setInferData (buffer.get());
+ req->setModel (model.get ());
+ req->setInferData (buffer.get ());
EXPECT_NE (sched->submitRequest (req), 0);
req = new Request (NPUINPUT_I2S_MIC);
- req->setModel (model.get());
- req->setInferData (buffer.get());
+ req->setModel (model.get ());
+ req->setInferData (buffer.get ());
EXPECT_NE (sched->submitRequest (req), 0);
/**
* @brief test submitRequest() with empty request
*/
-TEST (ne_core_sched_test, submit_request_empty_n)
-{
+TEST (ne_core_sched_test, submit_request_empty_n) {
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
- Scheduler * sched = new Scheduler (api.get ());
- Request * req = new Request (NPUINPUT_HOST);
+ Scheduler *sched = new Scheduler (api.get ());
+ Request *req = new Request (NPUINPUT_HOST);
EXPECT_NE (sched->submitRequest (req), 0);
* @brief main function for unit test
*/
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
return start_gtest (argc, argv);
}
/**
* @brief test basic primitives of SegmentTable
*/
-TEST (ne_core_segment_table_test, primitives)
-{
+TEST (ne_core_segment_table_test, primitives) {
std::unique_ptr<SegmentTable> table (new SegmentTable (new HWmemDevice));
EXPECT_EQ (table->getWeightSegment (), nullptr);
/** @brief create dummy metadata v3 */
static void
-create_metadata_v3 (npubin_meta & meta)
-{
- meta.magiccode = NPUBIN_MAGICCODE | 0x3; /* v3 */
+create_metadata_v3 (npubin_meta &meta) {
+ meta.magiccode = NPUBIN_MAGICCODE | 0x3; /* v3 */
meta.program_size = 0x1000;
meta.weight_size = 0x1000;
meta.size = NPUBIN_META_SIZE + meta.program_size + meta.weight_size;
meta.segment_num = 16;
- for (uint32_t i = 0; i < meta.segment_num; i++)
- meta.segment_size[i] = 0x1000;
+ for (uint32_t i = 0; i < meta.segment_num; i++) meta.segment_size[i] = 0x1000;
meta.weight_seg_idx = 0;
/** @brief set dummy input buffer info */
static void
-set_input_buffers (input_buffers &input)
-{
+set_input_buffers (input_buffers &input) {
input.num_buffers = 2;
input.bufs[0].type = BUFFER_MAPPED;
input.bufs[0].size = 0x1000;
/**
* @brief test createSegments() of SegmentTable
*/
-TEST (ne_core_segment_table_test, create_segments)
-{
+TEST (ne_core_segment_table_test, create_segments) {
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
std::unique_ptr<Model> model (new Model (new HWmemDevice));
model->setDriverAPI (api.get ());
set_input_buffers (input);
EXPECT_EQ (table->alloc (), 0);
- EXPECT_EQ (table->createSegments (model.get(), &input), 0);
+ EXPECT_EQ (table->createSegments (model.get (), &input), 0);
EXPECT_EQ (table->getNumTotalSegments (), meta.segment_num);
EXPECT_EQ (table->getNumWeightSegments (), 1);
/**
* @brief test basic primitives of SegmentTable with error handling
*/
-TEST (ne_core_segment_table_test, primitives_n)
-{
+TEST (ne_core_segment_table_test, primitives_n) {
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
std::unique_ptr<Model> model (new Model (new HWmemDevice));
model->setDriverAPI (api.get ());
set_input_buffers (input);
ASSERT_EQ (table->alloc (), 0);
- ASSERT_EQ (table->createSegments (model.get(), &input), 0);
+ ASSERT_EQ (table->createSegments (model.get (), &input), 0);
/** get segment with invalid index */
EXPECT_EQ (table->getWeightSegment (1), nullptr);
/**
* @brief test createSegments() of SegmentTable with error handling
*/
-TEST (ne_core_segment_table_test, create_segments_n)
-{
+TEST (ne_core_segment_table_test, create_segments_n) {
std::unique_ptr<DriverAPI> api;
api = DriverAPI::createDriverAPI (NPUCOND_TRIV2_CONN_SOCIP, 0);
- ASSERT_NE (api.get(), nullptr);
+ ASSERT_NE (api.get (), nullptr);
std::unique_ptr<Model> model (new Model (new HWmemDevice));
model->setDriverAPI (api.get ());
/** without model or input */
EXPECT_NE (table->createSegments (nullptr, nullptr), 0);
EXPECT_NE (table->createSegments (nullptr, &input), 0);
- EXPECT_NE (table->createSegments (model.get(), nullptr), 0);
+ EXPECT_NE (table->createSegments (model.get (), nullptr), 0);
/** before setting driver API */
- EXPECT_NE (table->createSegments (model.get(), &input), 0);
+ EXPECT_NE (table->createSegments (model.get (), &input), 0);
table->setDriverAPI (api.get ());
/** before allocating segment table */
- EXPECT_NE (table->createSegments (model.get(), &input), 0);
+ EXPECT_NE (table->createSegments (model.get (), &input), 0);
table.reset (new SegmentTable (new HWmemDevice));
table->setDriverAPI (api.get ());
ASSERT_EQ (table->alloc (), 0);
/** invalid metadata (only support v3) */
- meta.magiccode = NPUBIN_MAGICCODE | 0x2; /* v2 */
+ meta.magiccode = NPUBIN_MAGICCODE | 0x2; /* v2 */
ASSERT_EQ (model->setMetadata (&meta), 0);
- EXPECT_NE (table->createSegments (model.get(), &input), 0);
+ EXPECT_NE (table->createSegments (model.get (), &input), 0);
/** invalid metadata (zero size) */
- meta.magiccode = NPUBIN_MAGICCODE | 0x3; /* v3 */
+ meta.magiccode = NPUBIN_MAGICCODE | 0x3; /* v3 */
meta.segment_size[1] = 0;
ASSERT_EQ (model->setMetadata (&meta), 0);
- EXPECT_NE (table->createSegments (model.get(), &input), 0);
+ EXPECT_NE (table->createSegments (model.get (), &input), 0);
}
/**
* @brief main function for unit test
*/
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
return start_gtest (argc, argv);
}
/**
* @brief test getInstance()
*/
-TEST (ne_core_thread_pool_test, get_instance_singleton)
-{
- ThreadPool & pool = ThreadPool::getInstance ();
- ThreadPool & pool2 = ThreadPool::getInstance ();
+TEST (ne_core_thread_pool_test, get_instance_singleton) {
+ ThreadPool &pool = ThreadPool::getInstance ();
+ ThreadPool &pool2 = ThreadPool::getInstance ();
EXPECT_EQ (&pool, &pool2);
}
/**
* @brief test enqueueTask()
*/
-TEST (ne_core_thread_pool_test, enqueue_task)
-{
- ThreadPool & pool = ThreadPool::getInstance ();
+TEST (ne_core_thread_pool_test, enqueue_task) {
+ ThreadPool &pool = ThreadPool::getInstance ();
int num_called = 0;
std::condition_variable cv;
/**
* @brief test enqueueTask() with error handling
*/
-TEST (ne_core_thread_pool_test, enqueue_task_args_n)
-{
- ThreadPool & pool = ThreadPool::getInstance ();
+TEST (ne_core_thread_pool_test, enqueue_task_args_n) {
+ ThreadPool &pool = ThreadPool::getInstance ();
/** invalid arg */
EXPECT_NE (pool.enqueueTask (nullptr), 0);
/**
* @brief test enqueueTask() with error handling
*/
-TEST (ne_core_thread_pool_test, enqueue_task_same_id_n)
-{
- ThreadPool & pool = ThreadPool::getInstance ();
+TEST (ne_core_thread_pool_test, enqueue_task_same_id_n) {
+ ThreadPool &pool = ThreadPool::getInstance ();
int num_called = 0;
std::condition_variable cv;
std::mutex m;
- uint32_t num_threads = pool.getNumThreads();
+ uint32_t num_threads = pool.getNumThreads ();
auto callback = std::bind (test_callback_sleep, &num_called, &m, &cv);
/** at least one request should be pending */
/**
* @brief test removeTask()
*/
-TEST (ne_core_thread_pool_test, remove_task)
-{
- ThreadPool & pool = ThreadPool::getInstance ();
+TEST (ne_core_thread_pool_test, remove_task) {
+ ThreadPool &pool = ThreadPool::getInstance ();
int num_called = 0;
std::condition_variable cv;
std::mutex m;
- uint32_t num_threads = pool.getNumThreads();
+ uint32_t num_threads = pool.getNumThreads ();
auto callback = std::bind (test_callback_sleep, &num_called, &m, &cv);
/** at least one request should be pending */
/**
* @brief test removeTask() with error handling
*/
-TEST (ne_core_thread_pool_test, remove_task_args_n)
-{
- ThreadPool & pool = ThreadPool::getInstance ();
+TEST (ne_core_thread_pool_test, remove_task_args_n) {
+ ThreadPool &pool = ThreadPool::getInstance ();
/** no such task */
EXPECT_NE (pool.removeTask (0), 0);
/**
* @brief test removeTask() with error handling
*/
-TEST (ne_core_thread_pool_test, remove_task_resolved_n)
-{
- ThreadPool & pool = ThreadPool::getInstance ();
+TEST (ne_core_thread_pool_test, remove_task_resolved_n) {
+ ThreadPool &pool = ThreadPool::getInstance ();
/** add any task */
int num_called = 0;
* @brief main function for unit test
*/
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
return start_gtest (argc, argv);
}
/**
* @brief save and unset enviroment variables
*/
-static void save_and_unset_envvars (ENV_PAIR & envvars)
-{
+static void
+save_and_unset_envvars (ENV_PAIR &envvars) {
envvars.push_back (std::make_pair (ENV_RESV_MEM_SIZE, nullptr));
envvars.push_back (std::make_pair (ENV_NUM_THREADS, nullptr));
envvars.push_back (std::make_pair (ENV_LOG_DIR, nullptr));
- for ( auto & pair : envvars) {
+ for (auto &pair : envvars) {
pair.second = getenv (pair.first);
if (pair.second != nullptr)
unsetenv (pair.first);
/**
* @brief restore enviornment variables
*/
-static void restore_envvars (ENV_PAIR & envvars)
-{
- for ( auto & pair : envvars) {
+static void
+restore_envvars (ENV_PAIR &envvars) {
+ for (auto &pair : envvars) {
if (pair.second != nullptr)
setenv (pair.first, pair.second, 1);
}
/**
* @brief test getInstance() singleton
*/
-TEST (ne_core_utils_test, get_instance_singleton)
-{
- Conf & conf = Conf::getInstance ();
- Conf & conf2 = Conf::getInstance ();
+TEST (ne_core_utils_test, get_instance_singleton) {
+ Conf &conf = Conf::getInstance ();
+ Conf &conf2 = Conf::getInstance ();
EXPECT_EQ (&conf, &conf2);
- Logger & logger = Logger::getInstance ();
- Logger & logger2 = Logger::getInstance ();
+ Logger &logger = Logger::getInstance ();
+ Logger &logger2 = Logger::getInstance ();
EXPECT_EQ (&logger, &logger2);
}
/**
* @brief test loadConfDefault()
*/
-TEST (ne_core_utils_test, load_conf_default)
-{
+TEST (ne_core_utils_test, load_conf_default) {
/** to load default configurations, need to drop envvars */
ENV_PAIR envvars;
save_and_unset_envvars (envvars);
/** load configuration without argument */
- Conf & conf = Conf::getInstance ();
+ Conf &conf = Conf::getInstance ();
/** default configurations */
- EXPECT_EQ (conf.getResvMemSize(), default_resv_mem_size);
- EXPECT_EQ (conf.getNumThreads(), default_num_threads);
- EXPECT_STREQ (conf.getLogDir(), dafault_log_dir);
+ EXPECT_EQ (conf.getResvMemSize (), default_resv_mem_size);
+ EXPECT_EQ (conf.getNumThreads (), default_num_threads);
+ EXPECT_STREQ (conf.getLogDir (), dafault_log_dir);
restore_envvars (envvars);
}
/**
* @brief test loadConfIni()
*/
-TEST (ne_core_utils_test, load_conf_ini)
-{
+TEST (ne_core_utils_test, load_conf_ini) {
const char test_ini_file[] = "dummy.ini";
const char exp_log_dir[] = "/tmp/test_log";
const uint32_t exp_num_threads = 4;
save_and_unset_envvars (envvars);
- /** make dummy .ini file */
+ /** make dummy .ini file */
ofs = std::ofstream (test_ini_file);
- ASSERT_EQ (ofs.good(), true);
+ ASSERT_EQ (ofs.good (), true);
ofs << "[main]\n";
ofs << "resv_mem_size=" << exp_rsv_mem_sizek << "k" << std::endl;
ofs << "[debug]\n";
ofs << "log_dir=" << exp_log_dir << std::endl;
- ofs.close();
+ ofs.close ();
- Conf & conf = Conf::getInstance (test_ini_file);
+ Conf &conf = Conf::getInstance (test_ini_file);
- EXPECT_EQ (conf.getResvMemSize(), exp_rsv_mem_sizeb);
- EXPECT_EQ (conf.getNumThreads(), exp_num_threads);
- EXPECT_STREQ (conf.getLogDir(), exp_log_dir);
+ EXPECT_EQ (conf.getResvMemSize (), exp_rsv_mem_sizeb);
+ EXPECT_EQ (conf.getNumThreads (), exp_num_threads);
+ EXPECT_STREQ (conf.getLogDir (), exp_log_dir);
conf.reset ();
restore_envvars (envvars);
/**
* @brief test loadConfIni() with error handling
*/
-TEST (ne_core_utils_test, load_conf_ini_n)
-{
+TEST (ne_core_utils_test, load_conf_ini_n) {
ENV_PAIR envvars;
save_and_unset_envvars (envvars);
/** load default configurations */
- Conf & conf = Conf::getInstance ();
+ Conf &conf = Conf::getInstance ();
/** make dummy .ini file (with invalid values) */
std::ofstream ofs ("dummy.ini");
- ASSERT_EQ (ofs.good(), true);
+ ASSERT_EQ (ofs.good (), true);
ofs << "[main]\n";
ofs << "resv_mem_size=asdf\n";
ofs << "num_threads=asdf\n";
- ofs.close();
+ ofs.close ();
conf = Conf::getInstance ("dummy.ini");
/** still default configurations */
- EXPECT_EQ (conf.getResvMemSize(), default_resv_mem_size);
- EXPECT_EQ (conf.getNumThreads(), default_num_threads);
- EXPECT_STREQ (conf.getLogDir(), dafault_log_dir);
+ EXPECT_EQ (conf.getResvMemSize (), default_resv_mem_size);
+ EXPECT_EQ (conf.getNumThreads (), default_num_threads);
+ EXPECT_STREQ (conf.getLogDir (), dafault_log_dir);
conf.reset ();
restore_envvars (envvars);
/**
* @brief test loading configuration (envvars)
*/
-TEST (ne_core_utils_test, load_conf_env)
-{
+TEST (ne_core_utils_test, load_conf_env) {
const char exp_log_dir[] = "/tmp/";
const uint32_t exp_num_threads = 16;
const size_t exp_rsv_mem_sizeg = 1;
const size_t exp_rsv_mem_sizeb = exp_rsv_mem_sizeg * 1024 * 1024 * 1024;
- const std::string exp_str_rsv_mem_sizeg (std::to_string(exp_rsv_mem_sizeg) + "G");
- const std::string exp_str_num_threads (std::to_string(exp_num_threads));
+ const std::string exp_str_rsv_mem_sizeg (std::to_string (exp_rsv_mem_sizeg) +
+ "G");
+ const std::string exp_str_num_threads (std::to_string (exp_num_threads));
ENV_PAIR envvars;
save_and_unset_envvars (envvars);
- EXPECT_EQ (setenv("NE_RESV_MEM_SIZE", exp_str_rsv_mem_sizeg.c_str(), 1), 0);
- EXPECT_EQ (setenv("NE_NUM_THREADS", exp_str_num_threads.c_str(), 1), 0);
- EXPECT_EQ (setenv("NE_LOG_DIR", exp_log_dir, 1), 0);
+ EXPECT_EQ (setenv ("NE_RESV_MEM_SIZE", exp_str_rsv_mem_sizeg.c_str (), 1), 0);
+ EXPECT_EQ (setenv ("NE_NUM_THREADS", exp_str_num_threads.c_str (), 1), 0);
+ EXPECT_EQ (setenv ("NE_LOG_DIR", exp_log_dir, 1), 0);
- Conf & conf = Conf::getInstance ();
+ Conf &conf = Conf::getInstance ();
- EXPECT_EQ (conf.getResvMemSize(), exp_rsv_mem_sizeb);
- EXPECT_EQ (conf.getNumThreads(), exp_num_threads);
- EXPECT_STREQ (conf.getLogDir(), exp_log_dir);
+ EXPECT_EQ (conf.getResvMemSize (), exp_rsv_mem_sizeb);
+ EXPECT_EQ (conf.getNumThreads (), exp_num_threads);
+ EXPECT_STREQ (conf.getLogDir (), exp_log_dir);
conf.reset ();
restore_envvars (envvars);
/**
* @brief test loading configuration (negative cases)
*/
-TEST (ne_core_utils_test, load_conf_env_n)
-{
+TEST (ne_core_utils_test, load_conf_env_n) {
ENV_PAIR envvars;
save_and_unset_envvars (envvars);
/** from env (wrong values) */
- EXPECT_EQ (setenv("NE_RESV_MEM_SIZE", "asdf", 1), 0);
- EXPECT_EQ (setenv("NE_NUM_THREADS", "asdf", 1), 0);
+ EXPECT_EQ (setenv ("NE_RESV_MEM_SIZE", "asdf", 1), 0);
+ EXPECT_EQ (setenv ("NE_NUM_THREADS", "asdf", 1), 0);
- char long_log_dir_str[MAX_DIR_LEN+1];
- for (int i = 0; i < MAX_DIR_LEN; i++)
- long_log_dir_str[i] = 'a';
+ char long_log_dir_str[MAX_DIR_LEN + 1];
+ for (int i = 0; i < MAX_DIR_LEN; i++) long_log_dir_str[i] = 'a';
long_log_dir_str[MAX_DIR_LEN] = '\x00';
- EXPECT_EQ (setenv("NE_LOG_DIR", long_log_dir_str, 1), 0);
+ EXPECT_EQ (setenv ("NE_LOG_DIR", long_log_dir_str, 1), 0);
/** load conf with envvars */
- Conf & conf = Conf::getInstance ();
+ Conf &conf = Conf::getInstance ();
/** still default configurations */
- EXPECT_EQ (conf.getResvMemSize(), default_resv_mem_size);
- EXPECT_EQ (conf.getNumThreads(), default_num_threads);
- EXPECT_STREQ (conf.getLogDir(), dafault_log_dir);
+ EXPECT_EQ (conf.getResvMemSize (), default_resv_mem_size);
+ EXPECT_EQ (conf.getNumThreads (), default_num_threads);
+ EXPECT_STREQ (conf.getLogDir (), dafault_log_dir);
conf.reset ();
restore_envvars (envvars);
/**
* @brief test features of thread-safe map
*/
-TEST (ne_core_utils_test, thread_safe_map)
-{
+TEST (ne_core_utils_test, thread_safe_map) {
ThreadSafeMap<int, int> m;
- EXPECT_EQ (m.insert (1, new int (2)), 0);
- EXPECT_EQ (m.insert (2, new int (4)), 0);
- EXPECT_EQ (m.insert (3, new int (6)), 0);
+ EXPECT_EQ (m.insert (1, new int(2)), 0);
+ EXPECT_EQ (m.insert (2, new int(4)), 0);
+ EXPECT_EQ (m.insert (3, new int(6)), 0);
int *val = m.find (1);
EXPECT_EQ (*val, 2);
/**
* @brief test features of thread-safe map (negative cases)
*/
-TEST (ne_core_utils_test, thread_safe_map_insert_n)
-{
+TEST (ne_core_utils_test, thread_safe_map_insert_n) {
ThreadSafeMap<int, int> m;
- EXPECT_EQ (m.insert (1, new int (2)), 0);
- EXPECT_NE (m.insert (1, new int (3)), 0);
- EXPECT_NE (m.insert (1, new int (4)), 0);
+ EXPECT_EQ (m.insert (1, new int(2)), 0);
+ EXPECT_NE (m.insert (1, new int(3)), 0);
+ EXPECT_NE (m.insert (1, new int(4)), 0);
m.clear ();
}
/**
* @brief test features of thread-safe map (negative cases)
*/
-TEST (ne_core_utils_test, thread_safe_map_remove_n)
-{
+TEST (ne_core_utils_test, thread_safe_map_remove_n) {
ThreadSafeMap<int, int> m;
EXPECT_NE (m.remove (0), 0);
- EXPECT_EQ (m.insert (1, new int (2)), 0);
+ EXPECT_EQ (m.insert (1, new int(2)), 0);
EXPECT_EQ (m.remove (1), 0);
EXPECT_NE (m.remove (1), 0);
/**
* @brief test features of thread-safe map (negative cases)
*/
-TEST (ne_core_utils_test, thread_safe_map_find_n)
-{
+TEST (ne_core_utils_test, thread_safe_map_find_n) {
ThreadSafeMap<int, int> m;
EXPECT_EQ (m.find (0), nullptr);
- EXPECT_EQ (m.insert (1, new int (2)), 0);
+ EXPECT_EQ (m.insert (1, new int(2)), 0);
EXPECT_EQ (m.remove (1), 0);
EXPECT_EQ (m.find (1), nullptr);
/**
* @brief test features of log writing
*/
-TEST (ne_core_utils_test, log_write)
-{
- Logger & logger = Logger::getInstance ();
+TEST (ne_core_utils_test, log_write) {
+ Logger &logger = Logger::getInstance ();
int ret;
ret = logger.logwrite (LOG_ERROR, _N1, "test error\n");
/**
* @brief test features of log writing (negative cases)
*/
-TEST (ne_core_utils_test, log_write_n)
-{
- Logger & logger = Logger::getInstance ();
+TEST (ne_core_utils_test, log_write_n) {
+ Logger &logger = Logger::getInstance ();
/** logwrite is ignored for invalid arguments */
int ret;
* @brief main function for unit test
*/
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
return start_gtest (argc, argv);
}
/**
* @brief test primitives supported in libnpuhost
*/
-TEST (ne_libnpuhost_test, primitives)
-{
+TEST (ne_libnpuhost_test, primitives) {
uint32_t major;
uint32_t minor;
uint32_t extra;
/**
* @brief test APIs to check number of devices
*/
-TEST (ne_libnpuhost_test, get_num_devices)
-{
+TEST (ne_libnpuhost_test, get_num_devices) {
EXPECT_GT (getnumNPUdeviceByType (NPUCOND_TRIV2_CONN_SOCIP), 0);
EXPECT_GT (getnumNPUdeviceByType (NPUCOND_TRIV2_CONN_UNKNOWN), 0);
}
/**
* @brief test APIs to check number of devices (negative cases)
*/
-TEST (ne_libnpuhost_test, get_num_devices_n)
-{
+TEST (ne_libnpuhost_test, get_num_devices_n) {
EXPECT_EQ (getnumNPUdeviceByType ((dev_type) 0), 0);
EXPECT_EQ (getnumNPUdeviceByType ((dev_type) 0x1234), 0);
EXPECT_EQ (getnumNPUdeviceByType ((dev_type) 0xdeadbeef), 0);
EXPECT_EQ (getnumNPUdeviceByType ((dev_type) 0xffffffff), 0);
}
-static dev_type test_types[] = {
- NPUCOND_TRIV2_CONN_SOCIP
-};
+static dev_type test_types[] = {NPUCOND_TRIV2_CONN_SOCIP};
/**
* @brief test APIs to get a device instance
*/
-TEST (ne_libnpuhost_test, get_device_instance)
-{
- for (int i = 0; i < sizeof(test_types) / sizeof(dev_type); i++) {
+TEST (ne_libnpuhost_test, get_device_instance) {
+ for (int i = 0; i < sizeof (test_types) / sizeof (dev_type); i++) {
int num_devices;
dev_type type = test_types[i];
/**
* @brief test APIs to get a device instance (negative cases)
*/
-TEST (ne_libnpuhost_test, get_device_instance_n)
-{
+TEST (ne_libnpuhost_test, get_device_instance_n) {
npudev_h dev;
int num_devices;
- for (int i = 0; i < sizeof(test_types) / sizeof(dev_type); i++) {
+ for (int i = 0; i < sizeof (test_types) / sizeof (dev_type); i++) {
dev_type type = test_types[i];
num_devices = getnumNPUdeviceByType (type);
ASSERT_GT (num_devices, 0);
/**
* @brief test APIs to registerNPU model
*/
-TEST (ne_libnpuhost_test, register_model)
-{
+TEST (ne_libnpuhost_test, register_model) {
npudev_h dev;
ASSERT_EQ (getNPUdeviceByType (&dev, NPUCOND_TRIV2_CONN_SOCIP, 0), 0);
std::string model_path (NE_DATADIR);
model_path += "/testdata/TRIV234_2TOPS/CONV_2D_000/model.tvn";
- npubin_meta *meta = getNPUmodel_metadata (model_path.c_str(), false);
+ npubin_meta *meta = getNPUmodel_metadata (model_path.c_str (), false);
if (meta == nullptr) {
/* skip */
putNPUdevice (dev);
model.type = BUFFER_FILE;
model.size = meta->size;
- model.filepath = model_path.c_str();
+ model.filepath = model_path.c_str ();
EXPECT_EQ (registerNPUmodel (dev, &model, &model_id), 0);
EXPECT_EQ (unregisterNPUmodel (dev, model_id), 0);
/**
* @brief test APIs to registerNPU model (negative cases)
*/
-TEST (ne_libnpuhost_test, register_model_n)
-{
+TEST (ne_libnpuhost_test, register_model_n) {
npudev_h dev;
ASSERT_EQ (getNPUdeviceByType (&dev, NPUCOND_TRIV2_CONN_SOCIP, 0), 0);
std::string model_path (NE_DATADIR);
model_path += "/testdata/TRIV234_2TOPS/CONV_2D_000/model.tvn";
- npubin_meta *meta = getNPUmodel_metadata (model_path.c_str(), false);
+ npubin_meta *meta = getNPUmodel_metadata (model_path.c_str (), false);
if (meta == nullptr) {
/* skip */
putNPUdevice (dev);
model.type = BUFFER_FILE;
model.size = meta->size;
- model.filepath = model_path.c_str();
+ model.filepath = model_path.c_str ();
EXPECT_NE (registerNPUmodel (nullptr, &model, &model_id), 0);
EXPECT_NE (registerNPUmodel (dev, nullptr, &model_id), 0);
/**
* @brief test APIs to run model with input data (TRIV2)
*/
-TEST (ne_libnpuhost_test, run_input_triv2_binfmt_v3)
-{
+TEST (ne_libnpuhost_test, run_input_triv2_binfmt_v3) {
UtilTRIV2 tester;
ASSERT_EQ (tester.init (), 0);
/**
* @brief test APIs to run model with input data (TRIV2) (negative cases)
*/
-TEST (ne_libnpuhost_test, run_input_triv2_binfmt_v3_n)
-{
+TEST (ne_libnpuhost_test, run_input_triv2_binfmt_v3_n) {
UtilTRIV2 tester;
/* without init */
/**
* @brief test APIs to run model with internal input (TRIV2)
*/
-TEST (ne_libnpuhost_test, run_internal_triv2_binfmt_v3)
-{
+TEST (ne_libnpuhost_test, run_internal_triv2_binfmt_v3) {
UtilTRIV2 tester;
ASSERT_EQ (tester.init (), 0);
/**
* @brief test APIs to run model with internal input (TRIV2) (negative cases)
*/
-TEST (ne_libnpuhost_test, run_internal_triv2_binfmt_v3_n)
-{
+TEST (ne_libnpuhost_test, run_internal_triv2_binfmt_v3_n) {
UtilTRIV2 tester;
ASSERT_EQ (tester.init (), 0);
/**
* @brief test getNPUmodel_metadata ()
*/
-TEST (ne_libnpuhost_test, get_npumodel_metadata_01)
-{
+TEST (ne_libnpuhost_test, get_npumodel_metadata_01) {
/* Constants */
const size_t program_size = 4096;
const size_t weight_size = 4096;
DIR *dir;
size_t cnt;
- memset (&meta, 0, sizeof(meta));
+ memset (&meta, 0, sizeof (meta));
/**
* Since get_npumodel_metadata () is a common function regardless of the
* devices, this test uses a simple TRIV1 meta data.
*/
meta.magiccode = NPUBIN_MAGICCODE | 0x1;
- strncpy(meta.name, "get_npumodel_metadata", 128);
+ strncpy (meta.name, "get_npumodel_metadata", 128);
meta.model_id = 1;
meta.model_version = 1;
meta.buffer_size = buffer_size;
if (cnt != 1)
return;
- loaded_meta = getNPUmodel_metadata(bin_path, false);
+ loaded_meta = getNPUmodel_metadata (bin_path, false);
ASSERT_NE (loaded_meta, nullptr);
EXPECT_EQ (meta.magiccode, loaded_meta->magiccode);
EXPECT_EQ (meta.model_id, loaded_meta->model_id);
/**
* @brief test getNPUmodel_metadata () when the meta data includes extras
*/
-TEST (ne_libnpuhost_test, get_npumodel_metadata_02)
-{
+TEST (ne_libnpuhost_test, get_npumodel_metadata_02) {
/* Constants */
const size_t program_size = 4096;
const size_t weight_size = 4096;
DIR *dir;
size_t cnt;
- memset (&meta, 0, sizeof(meta));
+ memset (&meta, 0, sizeof (meta));
/**
* Since get_npumodel_metadata () is a common function regardless of the
* devices, this test uses a simple TRIV1 meta data.
*/
meta.magiccode = NPUBIN_MAGICCODE | 0x1 | (extra_size << 8);
- strncpy(meta.name, "get_npumodel_metadata", 128);
+ strncpy (meta.name, "get_npumodel_metadata", 128);
meta.model_id = 1;
meta.model_version = 1;
meta.buffer_size = buffer_size;
extra = (uint8_t *) malloc (extra_size * NPUBIN_META_SIZE);
memset (extra, 0, extra_size * NPUBIN_META_SIZE);
- cnt = fwrite (extra, extra_size * NPUBIN_META_SIZE, 1, meta_only_bin_fp);
+ cnt = fwrite (extra, extra_size * NPUBIN_META_SIZE, 1, meta_only_bin_fp);
fclose (meta_only_bin_fp);
free (extra);
if (cnt != 1)
return;
- loaded_meta = getNPUmodel_metadata(bin_path, true);
+ loaded_meta = getNPUmodel_metadata (bin_path, true);
ASSERT_NE (loaded_meta, nullptr);
EXPECT_EQ (meta.magiccode, loaded_meta->magiccode);
EXPECT_EQ (meta.model_id, loaded_meta->model_id);
/**
* @brief test getNPUmodel_metadata () with error handling #1
*/
-TEST (ne_libnpuhost_test, get_npumodel_metadata_01_n)
-{
+TEST (ne_libnpuhost_test, get_npumodel_metadata_01_n) {
npubin_meta *loaded_meta;
/** The given path is nullptr */
- loaded_meta = getNPUmodel_metadata(nullptr, false);
+ loaded_meta = getNPUmodel_metadata (nullptr, false);
ASSERT_EQ (loaded_meta, nullptr);
}
/**
* @brief test getNPUmodel_metadata () with error handling #2
*/
-TEST (ne_libnpuhost_test, get_npumodel_metadata_02_n)
-{
- /* Constants */
+TEST (ne_libnpuhost_test, get_npumodel_metadata_02_n) {
+ /* Constants */
const char wrong_bin_path[] = "nowhere/notfile.bin";
npubin_meta *loaded_meta;
/** The given path is nullptr */
- loaded_meta = getNPUmodel_metadata(wrong_bin_path, false);
+ loaded_meta = getNPUmodel_metadata (wrong_bin_path, false);
ASSERT_EQ (loaded_meta, nullptr);
}
/**
* @brief test getNPUmodel_metadata () with error handling #3
*/
-TEST (ne_libnpuhost_test, get_npumodel_metadata_03_n)
-{
+TEST (ne_libnpuhost_test, get_npumodel_metadata_03_n) {
/* Constants */
const size_t program_size = 4096;
const size_t weight_size = 4096;
DIR *dir;
size_t cnt;
- memset (&meta, 0, sizeof(meta));
+ memset (&meta, 0, sizeof (meta));
/**
* Since get_npumodel_metadata () is a common function regardless of the
* devices, this test uses a simple TRIV1 meta data.
*/
/* Wrong magic code */
meta.magiccode = 0xFF;
- strncpy(meta.name, "get_npumodel_metadata", 128);
+ strncpy (meta.name, "get_npumodel_metadata", 128);
meta.model_id = 1;
meta.model_version = 1;
meta.buffer_size = buffer_size;
if (cnt != 1)
return;
- loaded_meta = getNPUmodel_metadata(bin_path, false);
+ loaded_meta = getNPUmodel_metadata (bin_path, false);
ASSERT_EQ (loaded_meta, nullptr);
}
/**
* @brief test HostHandler's getNPUmodel_metadata () with error handling #4
*/
-TEST (ne_libnpuhost_test, get_npumodel_metadata_04_n)
-{
+TEST (ne_libnpuhost_test, get_npumodel_metadata_04_n) {
/* Constants */
const size_t program_size = 4096;
const size_t weight_size = 4096;
DIR *dir;
size_t cnt;
- memset (&meta, 0, sizeof(meta));
+ memset (&meta, 0, sizeof (meta));
/**
* Since get_npumodel_metadata () is a common function regardless of the
* devices, this test uses a simple TRIV1 meta data.
*/
meta.magiccode = NPUBIN_MAGICCODE | 0x1 | (extra_size << 8);
- strncpy(meta.name, "get_npumodel_metadata", 128);
+ strncpy (meta.name, "get_npumodel_metadata", 128);
meta.model_id = 1;
meta.model_version = 1;
meta.buffer_size = buffer_size;
extra = (uint8_t *) malloc (extra_size * NPUBIN_META_SIZE);
memset (extra, 0, extra_size * NPUBIN_META_SIZE);
- cnt = fwrite (extra, extra_size * (NPUBIN_META_SIZE / 2), 1, meta_only_bin_fp);
+ cnt =
+ fwrite (extra, extra_size * (NPUBIN_META_SIZE / 2), 1, meta_only_bin_fp);
fclose (meta_only_bin_fp);
free (extra);
if (cnt != 1)
return;
- loaded_meta = getNPUmodel_metadata(bin_path, true);
+ loaded_meta = getNPUmodel_metadata (bin_path, true);
ASSERT_EQ (loaded_meta, nullptr);
}
/**
* @brief test getNPU_deviceStatus()
*/
-TEST (ne_libnpuhost_test, get_npu_device_status)
-{
+TEST (ne_libnpuhost_test, get_npu_device_status) {
dev_type type = NPUCOND_TRIV2_CONN_SOCIP;
npudev_h dev;
npu_status status;
/**
*@brief test getNPUdeviceByType with invalid dev_type
*/
-TEST (ne_libnpuhost_test, get_npu_device_by_type_n)
-{
+TEST (ne_libnpuhost_test, get_npu_device_by_type_n) {
npudev_h dev;
int ret;
/**
* @brief test getNPU_deviceStatus() with error handling
*/
-TEST (ne_libnpuhost_test, get_npu_device_status_n)
-{
+TEST (ne_libnpuhost_test, get_npu_device_status_n) {
npudev_h dev = nullptr;
npu_status status;
uint32_t num_requests;
/**
* @brief test unsupported APIs in emulated envionment
*/
-TEST (ne_libnpuhost_test, unsupported_apis_in_emul_n)
-{
+TEST (ne_libnpuhost_test, unsupported_apis_in_emul_n) {
dev_type type = NPUCOND_TRIV2_CONN_SOCIP;
npudev_h dev = nullptr;
int ret;
/**
* @brief test unsupported APIs in emulated envionment
*/
-TEST (ne_libnpuhost_test, driver_level_and_tops)
-{
+TEST (ne_libnpuhost_test, driver_level_and_tops) {
dev_type type = NPUCOND_TRIV2_CONN_SOCIP;
npudev_h dev = nullptr;
uint32_t target_tops = 2;
/**
* @brief test statistics APIs
*/
-TEST (ne_libnpuhost_test, statistics_apis)
-{
+TEST (ne_libnpuhost_test, statistics_apis) {
dev_type type = NPUCOND_TRIV2_CONN_SOCIP;
npudev_h dev = nullptr;
int ret;
/**
* @brief test statistics APIs with negative cases
*/
-TEST (ne_libnpuhost_test, statistics_apis_n)
-{
+TEST (ne_libnpuhost_test, statistics_apis_n) {
dev_type type = NPUCOND_TRIV2_CONN_SOCIP;
npudev_h dev = nullptr;
int ret;
* @brief main function for unit test
*/
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
return start_gtest (argc, argv);
}
#include <condition_variable>
#include <mutex>
-#define TEST_SLEEP_MS (100 * 1000) /* 100ms is enough */
+#define TEST_SLEEP_MS (100 * 1000) /* 100ms is enough */
/**
* @brief start gtest with tricky exception handling (due to svace)
*/
-static inline int start_gtest (int argc, char **argv) {
+static inline int
+start_gtest (int argc, char **argv) {
int ret = -1;
try {
testing::InitGoogleTest (&argc, argv);
} catch (...) {
- std::cerr << "catch 'testing::internal::<unnamed>::ClassUniqueToAlwaysTrue'" << std::endl;
+ std::cerr << "catch 'testing::internal::<unnamed>::ClassUniqueToAlwaysTrue'"
+ << std::endl;
}
try {
- ret = RUN_ALL_TESTS();
+ ret = RUN_ALL_TESTS ();
} catch (...) {
- std::cerr << "catch `testing::internal::GoogleTestFailureException`" << std::endl;
+ std::cerr << "catch `testing::internal::GoogleTestFailureException`"
+ << std::endl;
}
return ret;
}
-static inline void test_callback (int * num_called,
- std::mutex * m, std::condition_variable * cv)
-{
+static inline void
+test_callback (int *num_called, std::mutex *m, std::condition_variable *cv) {
std::unique_lock<std::mutex> lock (*m);
(*num_called)++;
cv->notify_one ();
}
-static inline void test_callback_sleep (int * num_called,
- std::mutex * m, std::condition_variable * cv)
-{
+static inline void
+test_callback_sleep (int *num_called, std::mutex *m,
+ std::condition_variable *cv) {
{
std::unique_lock<std::mutex> lock (*m);
(*num_called)++;
usleep (TEST_SLEEP_MS);
}
-static inline void wait_callbacks (int & num_called, int max_called,
- std::mutex & m, std::condition_variable & cv)
-{
+static inline void
+wait_callbacks (int &num_called, int max_called, std::mutex &m,
+ std::condition_variable &cv) {
std::unique_lock<std::mutex> lock (m);
- cv.wait (lock, [&]() {
- return max_called == num_called;
- });
+ cv.wait (lock, [&]() { return max_called == num_called; });
}
#endif /* _NE_UNITTEST_UTILS_H_ */
#define TO_ALIGN_SIZE(size) (((size - 1) / ALIGN_SIZE + 1) * ALIGN_SIZE)
/** @brief fill program data for testing */
-static void fill_program (void *buf, size_t size)
-{
+static void
+fill_program (void *buf, size_t size) {
/**
* @todo fill virtual instructions
*/
}
/** @brief fill weight data for testing */
-static void fill_weight (void *buf, size_t size)
-{
+static void
+fill_weight (void *buf, size_t size) {
/**
* @todo fill weight data
*/
* @param[in] offset The offset indicating the point where the actual data starts in the buffer
* @param[in] size The size of input data
*/
-void fill_input_data (generic_buffer *buffer, size_t offset, size_t size)
-{
+void
+fill_input_data (generic_buffer *buffer, size_t offset, size_t size) {
/* @todo fill input data */
memset ((char *) buffer->addr + offset, '\x00', size);
}
* @param[in] buf_type The memory type of the buffer included in the model
* @return A generic buffer including dummy model data, NULL if error
*/
-generic_buffer* make_model (npudev_h dev, npubin_meta *meta, buffer_types buf_type)
-{
+generic_buffer *
+make_model (npudev_h dev, npubin_meta *meta, buffer_types buf_type) {
size_t size = NPUBIN_META_SIZE + meta->program_size + meta->weight_size;
generic_buffer *buffer = (generic_buffer *) malloc (sizeof (*buffer));
case BUFFER_DMABUF:
/* BUFFER_MAPPED, BUFFER_DMABUF */
memcpy (buffer->addr, meta, NPUBIN_META_SIZE);
- fill_program ((char *) buffer->addr + NPUBIN_META_SIZE, meta->program_size);
- fill_weight ((char *) buffer->addr + NPUBIN_META_SIZE + meta->program_size,
+ fill_program ((char *) buffer->addr + NPUBIN_META_SIZE,
+ meta->program_size);
+ fill_weight (
+ (char *) buffer->addr + NPUBIN_META_SIZE + meta->program_size,
meta->weight_size);
break;
default:
* @brief Destroy generic buffer with model data
* @param[in] model the generic buffer instance
*/
-void destroy_model (npudev_h dev, generic_buffer *model)
-{
+void
+destroy_model (npudev_h dev, generic_buffer *model) {
if (model) {
cleanNPU_modelBuffer (dev, model);
free (model);
* @param[out] meta the metadata of model
* @return 0 if no error, otherwise a negative errno
*/
-int get_model_meta (const char *model_file, npubin_meta *meta)
-{
+int
+get_model_meta (const char *model_file, npubin_meta *meta) {
FILE *fp;
int ret;
* @note it traverse sub-directory and call 'func'
* @return 0 if no error, otherwise a negative value
*/
-int run_apptest (npudev_h dev, char **argv, FUNC_APPTEST func)
-{
+int
+run_apptest (npudev_h dev, char **argv, FUNC_APPTEST func) {
DIR *d;
struct dirent *dir;
const char *name = argv[0];
int total_succs, total_fails, total_skpds;
int ret;
- d = opendir(path);
+ d = opendir (path);
if (!d) {
- fprintf(stderr, "Fail to open %s\n", path);
+ fprintf (stderr, "Fail to open %s\n", path);
return -1;
}
total_succs = total_fails = total_skpds = 0;
- while ((dir = readdir(d)) != NULL) {
+ while ((dir = readdir (d)) != NULL) {
/** a target binary and data are in each sub-directory */
if (dir->d_type != DT_DIR)
continue;
if (dir->d_name[0] == '.')
continue;
- fprintf(stderr, "[APPTEST] %-15s Running... ", dir->d_name);
+ fprintf (stderr, "[APPTEST] %-15s Running... ", dir->d_name);
ret = func (dev, path, dir->d_name);
if (ret == test_ret_success) {
- fprintf(stderr, "PASSED\n");
+ fprintf (stderr, "PASSED\n");
total_succs++;
} else if (ret > test_ret_skipped) {
- fprintf(stderr, "SKIPPED\n");
- fprintf(stderr, "\t %s\n", msg_skipped_reason[ret]);
+ fprintf (stderr, "SKIPPED\n");
+ fprintf (stderr, "\t %s\n", msg_skipped_reason[ret]);
++total_skpds;
} else {
- fprintf(stderr, "FAILED\n");
+ fprintf (stderr, "FAILED\n");
total_fails++;
}
}
- closedir(d);
+ closedir (d);
putNPUdevice (dev);
- fprintf(stderr, "[APPTEST] %s: PASSED (%d), FAILED (%d), SKIPPED (%d)\n",
- name, total_succs, total_fails, total_skpds);
+ fprintf (stderr, "[APPTEST] %s: PASSED (%d), FAILED (%d), SKIPPED (%d)\n",
+ name, total_succs, total_fails, total_skpds);
return -total_fails;
}
* @param[in] file path the path of file
* @return the size of file
*/
-off_t get_file_size (const char *file_path)
-{
+off_t
+get_file_size (const char *file_path) {
struct stat _stat;
if (stat (file_path, &_stat) != 0) {
- fprintf (stderr, "Fail to find the file %s (errno %d)\n", file_path, -errno);
+ fprintf (stderr, "Fail to find the file %s (errno %d)\n", file_path,
+ -errno);
return -errno;
}
/** @brief check memory leak by calling memory status API */
int
-check_memory_leak (npudev_h dev)
-{
+check_memory_leak (npudev_h dev) {
size_t alloc_total = 0;
size_t free_total = 0;
bool rechecked = false;
if (alloc_total != free_total) {
if (!rechecked) {
rechecked = true;
- sleep (1); /* there may be a timing issue until all resources are freed */
+ sleep (1); /* there may be a timing issue until all resources are freed */
goto recheck;
}
- fprintf (stderr, "memory leak check failed (0x%zx != 0x%zx)\n",
- free_total, alloc_total);
+ fprintf (stderr, "memory leak check failed (0x%zx != 0x%zx)\n", free_total,
+ alloc_total);
return -EINVAL;
}
/** @brief compare output tensor with the golden data */
int
compare_data (const char *golden_path, const char *output_data,
- uint64_t output_size)
-{
+ uint64_t output_size) {
FILE *fp = fopen (golden_path, "r");
uint64_t idx;
int c, err = 0;
}
idx = 0;
- while ((c = fgetc(fp)) != EOF) {
+ while ((c = fgetc (fp)) != EOF) {
if (idx >= output_size) {
fprintf (stderr, "Out-of-range!\n");
err = -1;
goto out;
}
if ((uint8_t) c != (uint8_t) output_data[idx]) {
- fprintf (stderr, "Hex diff at %" PRIu64 "; %#1x vs. %#1x\n",
- idx, c, output_data[idx]);
+ fprintf (stderr, "Hex diff at %" PRIu64 "; %#1x vs. %#1x\n", idx, c,
+ output_data[idx]);
err = -1;
goto out;
}
bool UtilTrinity::dump_ = false;
/** @brief constructor of UtilTrinity */
-UtilTrinity::UtilTrinity (dev_type type, bool need_model, bool verify)
-{
+UtilTrinity::UtilTrinity (dev_type type, bool need_model, bool verify) {
dev_ = nullptr;
type_ = type;
noti_mode_ = NPU_INTERRUPT;
}
/** @brief device instance cleanup */
-void UtilTrinity::clear () {
+void
+UtilTrinity::clear () {
if (dev_ != nullptr) {
unregisterNPUmodel_all (dev_);
putNPUdevice (dev_);
}
/** @brief initialize trinity device and get the device handle */
-int UtilTrinity::init (uint32_t tops)
-{
+int
+UtilTrinity::init (uint32_t tops) {
uint32_t id;
if (node_ != "") {
}
/** @brief print program usage */
-void UtilTrinity::printUsage (const char *prog_name, const char *param_str)
-{
+void
+UtilTrinity::printUsage (const char *prog_name, const char *param_str) {
if (param_str)
std::cerr << "Usage: " << prog_name << " [options] " << param_str << "\n";
else
}
/** @brief disable stdout/stderr */
-void UtilTrinity::setMute ()
-{
+void
+UtilTrinity::setMute () {
/** redirect to /dev/null */
- if (freopen("/dev/null", "w", stdout) == NULL)
+ if (freopen ("/dev/null", "w", stdout) == NULL)
std::cerr << "Warning: Failed to reopen stdout\n";
- if (freopen("/dev/null", "w", stderr) == NULL)
+ if (freopen ("/dev/null", "w", stderr) == NULL)
std::cerr << "Warning: Failed to reopen stderr\n";
mute_ = true;
}
/** @brief set device node path */
-void UtilTrinity::setNodePath (const std::string node)
-{
+void
+UtilTrinity::setNodePath (const std::string node) {
node_ = node;
}
/** @brief set output notification mode */
-void UtilTrinity::setNotiMode (const std::string mode)
-{
+void
+UtilTrinity::setNotiMode (const std::string mode) {
if (mode == "polling") {
noti_mode_ = NPU_POLLING;
} else if (mode == "intr") {
}
/** @brief parse/set arguments and get working directory */
-int UtilTrinity::parseArgs (int argc, char **argv, char **param,
- const char *param_str)
-{
+int
+UtilTrinity::parseArgs (int argc, char **argv, char **param,
+ const char *param_str) {
int c;
optind = 0;
}
/** @brief load NPU model and set relevant info */
-int UtilTrinity::loadModel (std::string dirpath, uint32_t *model_id_ptr,
- npu_priority priority, uint32_t timeout)
-{
+int
+UtilTrinity::loadModel (std::string dirpath, uint32_t *model_id_ptr,
+ npu_priority priority, uint32_t timeout) {
if (dirpath == "")
return -EINVAL;
std::string model_path = dirpath + "/model.tvn";
- npubin_meta * meta = getNPUmodel_metadata (model_path.c_str(), false);
+ npubin_meta *meta = getNPUmodel_metadata (model_path.c_str (), false);
if (meta == nullptr)
return -EINVAL;
model_buf.type = BUFFER_FILE;
model_buf.size = meta->size;
- model_buf.filepath = model_path.c_str();
+ model_buf.filepath = model_path.c_str ();
status = registerNPUmodel (dev_, &model_buf, &model_id);
if (status != 0)
return status;
}
- models_.push_back (std::unique_ptr<UtilModel>(model));
+ models_.push_back (std::unique_ptr<UtilModel> (model));
if (model_id_ptr)
*model_id_ptr = model_id;
}
/** @brief find the utility model instance */
-UtilModel *UtilTrinity::findModel (uint32_t model_id)
-{
- for (auto& model : models_) {
+UtilModel *
+UtilTrinity::findModel (uint32_t model_id) {
+ for (auto &model : models_) {
if (model->getModelID () == model_id)
return model.get ();
}
}
/** @brief wait completion */
-uint32_t UtilTrinity::wait ()
-{
+uint32_t
+UtilTrinity::wait () {
uint32_t result;
std::unique_lock<std::mutex> lock (m_);
}
/** @brief callback with verification */
-void UtilTrinity::callbackVerify (output_buffers *output, uint64_t sequence,
- void *data)
-{
+void
+UtilTrinity::callbackVerify (output_buffers *output, uint64_t sequence,
+ void *data) {
bool success = true;
- UtilModel *model = static_cast <UtilModel *> (data);
+ UtilModel *model = static_cast<UtilModel *> (data);
for (uint32_t idx = 0; idx < output->num_buffers; idx++) {
if (model != nullptr) {
const char *path = model->getOutputPath (idx);
- char *buf = static_cast<char*>(output->bufs[idx].addr);
+ char *buf = static_cast<char *> (output->bufs[idx].addr);
size_t size = output->bufs[idx].size;
if (compare_data (path, buf, size) != 0)
std::string dump_path (path);
dump_path += ".dump";
- FILE * dump_fp = fopen (dump_path.c_str (), "w");
+ FILE *dump_fp = fopen (dump_path.c_str (), "w");
if (dump_fp != NULL) {
fwrite (buf, size, 1, dump_fp);
fclose (dump_fp);
}
/** @brief callback without verification */
-void UtilTrinity::callback (output_buffers *output, uint64_t sequence,
- void *data)
-{
+void
+UtilTrinity::callback (output_buffers *output, uint64_t sequence, void *data) {
for (uint32_t idx = 0; idx < output->num_buffers; idx++)
free (output->bufs[idx].addr);
}
/** @brief run inference with the given model info */
-int UtilTrinity::run_each (UtilModel *model, bool sync)
-{
+int
+UtilTrinity::run_each (UtilModel *model, bool sync) {
int status = -EINVAL;
void *cb_data = nullptr;
npuOutputNotify cb;
if (verify_) {
cb = UtilTrinity::callbackVerify;
if (model) {
- cb_data = static_cast<void*> (model);
+ cb_data = static_cast<void *> (model);
}
} else {
cb = UtilTrinity::callback;
output_buffers output;
if (model) {
- status = runNPU_sync (dev_, model->getModelID (), model->getInput (),
- &output);
+ status =
+ runNPU_sync (dev_, model->getModelID (), model->getInput (), &output);
} else {
/* TODO: revise this when TRIA is implemented */
input_buffers input;
}
} else {
if (model) {
- status = runNPU_async (dev_, model->getModelID (), model->getInput (),
- cb, NULL, cb_data, NPUASYNC_WAIT);
+ status = runNPU_async (dev_, model->getModelID (), model->getInput (), cb,
+ NULL, cb_data, NPUASYNC_WAIT);
} else {
/* TODO: revise this when TRIA is implemented */
input_buffers input;
if (status != 0)
return status;
- status = runNPU_async (dev_, 0, &input, cb, NULL, cb_data,
- NPUASYNC_WAIT);
+ status = runNPU_async (dev_, 0, &input, cb, NULL, cb_data, NPUASYNC_WAIT);
cleanNPU_inputBuffers (dev_, &input);
}
}
/** @brief run inference with the given model id (default sync) */
-int UtilTrinity::run (uint32_t model_id)
-{
+int
+UtilTrinity::run (uint32_t model_id) {
return run (model_id, sync_);
}
/** @brief run inference with the given model id */
-int UtilTrinity::run (uint32_t model_id, bool sync)
-{
+int
+UtilTrinity::run (uint32_t model_id, bool sync) {
UtilModel *model = findModel (model_id);
return run_each (model, sync);
}
/** @brief run inference across all registered models (default sync) */
-int UtilTrinity::runAll ()
-{
+int
+UtilTrinity::runAll () {
return runAll (sync_);
}
/** @brief run inference across all registered models */
-int UtilTrinity::runAll (bool sync)
-{
- for (auto& model : models_) {
+int
+UtilTrinity::runAll (bool sync) {
+ for (auto &model : models_) {
int status = run_each (model.get (), sync);
if (status < 0)
return status;
}
/** @brief run inference with internal input */
-int UtilTrinity::runInternal (uint32_t model_id, std::string dev_path)
-{
+int
+UtilTrinity::runInternal (uint32_t model_id, std::string dev_path) {
return runNPU_internalInput (dev_, model_id, NPUINPUT_HW_RECURRING,
- dev_path.c_str ());
+ dev_path.c_str ());
}
/** @brief stop inference from run_internal */
-int UtilTrinity::stopInternal (int req_id)
-{
+int
+UtilTrinity::stopInternal (int req_id) {
return stopNPU_internalInput (dev_, req_id);
}
/** @brief get profile information from NPU */
-int UtilTrinity::getProfile (int req_id, npu_profile *profile)
-{
+int
+UtilTrinity::getProfile (int req_id, npu_profile *profile) {
return getNPU_profile (dev_, req_id, profile);
}
/** @brief configure constraint for the model */
-int UtilTrinity::set_constraint (uint32_t model_id, uint32_t timeout,
- npu_priority priority) {
+int
+UtilTrinity::set_constraint (uint32_t model_id, uint32_t timeout,
+ npu_priority priority) {
npuConstraint constraint;
constraint.timeout_ms = timeout;
}
/** @brief implementation of extract_node_id for TRIV2 */
-bool UtilTRIV2::extract_node_id (uint32_t *id) {
+bool
+UtilTRIV2::extract_node_id (uint32_t *id) {
if (node_ == "" || id == nullptr)
return false;
}
/** @brief implementation of check_version for TRIV2 */
-bool UtilTRIV2::check_version (npubin_meta *meta) {
+bool
+UtilTRIV2::check_version (npubin_meta *meta) {
uint64_t version = NPUBIN_VERSION (meta->magiccode);
return (version == 3);
}
/** @brief implementation of set_data_info for TRIV2 */
-int UtilTRIV2::set_data_info (npubin_meta *meta, uint32_t model_id) {
+int
+UtilTRIV2::set_data_info (npubin_meta *meta, uint32_t model_id) {
tensors_data_info info_in;
tensors_data_info info_out;
return setNPU_dataInfo (dev_, model_id, &info_in, &info_out);
}
-int UtilTRIV2::prepare_model (UtilModel *model) {
+int
+UtilTRIV2::prepare_model (UtilModel *model) {
input_buffers *input = model->getInput ();
npubin_meta *meta = model->getMetadata ();
std::string dirpath = model->getDirpath ();
/** @brief utility to access test model */
class UtilModel {
- public:
- UtilModel () : meta_ (nullptr), model_id_ (0) {
- memset (&input_, '\x00', sizeof(input_buffers));
+ public:
+ UtilModel () : meta_ (nullptr), model_id_ (0) {
+ memset (&input_, '\x00', sizeof (input_buffers));
+ }
+ ~UtilModel () {
+ if (meta_)
+ free (meta_);
+ }
+
+ void setMetadata (npubin_meta *meta) { meta_ = meta; }
+ void setDirpath (std::string dirpath) { dirpath_ = dirpath; }
+ void setModelID (uint32_t model_id) { model_id_ = model_id; }
+ void prepareInputPath (uint32_t num) {
+ if (NPUBIN_VERSION (meta_->magiccode) <= 1) {
+ inpath_.resize (1);
+ inpath_[0] = dirpath_ + "/input_fmap.bin";
+ } else {
+ inpath_.resize (num);
+ for (uint32_t idx = 0; idx < num; idx++)
+ inpath_[idx] =
+ dirpath_ + "/input_fmap_" + std::to_string (idx) + ".bin";
}
- ~UtilModel () { if (meta_) free (meta_); }
-
- void setMetadata (npubin_meta *meta) { meta_ = meta; }
- void setDirpath (std::string dirpath) { dirpath_ = dirpath; }
- void setModelID (uint32_t model_id) { model_id_ = model_id; }
- void prepareInputPath (uint32_t num) {
- if (NPUBIN_VERSION (meta_->magiccode) <= 1) {
- inpath_.resize (1);
- inpath_[0] = dirpath_ + "/input_fmap.bin";
- } else {
- inpath_.resize (num);
- for (uint32_t idx = 0; idx < num; idx++)
- inpath_[idx] = dirpath_ + "/input_fmap_" + std::to_string (idx) + ".bin";
- }
+ }
+ void prepareOutputPath (uint32_t num) {
+ if (NPUBIN_VERSION (meta_->magiccode) <= 1) {
+ outpath_.resize (1);
+ outpath_[0] = dirpath_ + "/output_fmap.bin";
+ } else {
+ outpath_.resize (num);
+ for (uint32_t idx = 0; idx < num; idx++)
+ outpath_[idx] =
+ dirpath_ + "/output_fmap_" + std::to_string (idx) + ".bin";
}
- void prepareOutputPath (uint32_t num) {
- if (NPUBIN_VERSION (meta_->magiccode) <= 1) {
- outpath_.resize (1);
- outpath_[0] = dirpath_ + "/output_fmap.bin";
- } else {
- outpath_.resize (num);
- for (uint32_t idx = 0; idx < num; idx++)
- outpath_[idx] = dirpath_ + "/output_fmap_" + std::to_string (idx) + ".bin";
- }
- }
- const char *getInputPath (uint32_t idx) { return inpath_[idx].c_str (); }
- const char *getOutputPath (uint32_t idx) { return outpath_[idx].c_str (); }
+ }
+ const char *getInputPath (uint32_t idx) { return inpath_[idx].c_str (); }
+ const char *getOutputPath (uint32_t idx) { return outpath_[idx].c_str (); }
- npubin_meta *getMetadata () { return meta_; }
- std::string& getDirpath () { return dirpath_; }
- uint32_t getModelID () { return model_id_; }
- input_buffers* getInput () { return &input_; }
+ npubin_meta *getMetadata () { return meta_; }
+ std::string &getDirpath () { return dirpath_; }
+ uint32_t getModelID () { return model_id_; }
+ input_buffers *getInput () { return &input_; }
- private:
- npubin_meta *meta_;
- uint32_t model_id_;
+ private:
+ npubin_meta *meta_;
+ uint32_t model_id_;
- std::string dirpath_;
+ std::string dirpath_;
- input_buffers input_;
- std::vector<std::string> inpath_;
- std::vector<std::string> outpath_;
+ input_buffers input_;
+ std::vector<std::string> inpath_;
+ std::vector<std::string> outpath_;
};
/** @brief utility to access trinity device */
class UtilTrinity {
- public:
- UtilTrinity (dev_type type, bool need_model, bool verify);
- virtual ~UtilTrinity ();
+ public:
+ UtilTrinity (dev_type type, bool need_model, bool verify);
+ virtual ~UtilTrinity ();
- npudev_h getDeviceHandle () { return dev_; }
+ npudev_h getDeviceHandle () { return dev_; }
- static void setDump () { dump_ = true; }
- void setSync () { sync_ = true; }
- void setMute ();
- void setNotiMode (const std::string mode);
- void setNodePath (const std::string node);
+ static void setDump () { dump_ = true; }
+ void setSync () { sync_ = true; }
+ void setMute ();
+ void setNotiMode (const std::string mode);
+ void setNodePath (const std::string node);
- int init (uint32_t tops = default_tops);
- void clear ();
- void printUsage (const char *prog_name, const char *param_str = nullptr);
- int parseArgs (int argc, char **argv, char **param, const char *param_str = nullptr);
- int loadModel (std::string dirpath, uint32_t *model_id,
- npu_priority priority = NPU_PRIORITY_MID,
- uint32_t timeout = 5000);
+ int init (uint32_t tops = default_tops);
+ void clear ();
+ void printUsage (const char *prog_name, const char *param_str = nullptr);
+ int parseArgs (int argc, char **argv, char **param,
+ const char *param_str = nullptr);
+ int loadModel (std::string dirpath, uint32_t *model_id,
+ npu_priority priority = NPU_PRIORITY_MID,
+ uint32_t timeout = 5000);
- int run (uint32_t model_id);
- int run (uint32_t model_id, bool sync);
- int runAll ();
- int runAll (bool sync);
+ int run (uint32_t model_id);
+ int run (uint32_t model_id, bool sync);
+ int runAll ();
+ int runAll (bool sync);
- int runInternal (uint32_t model_id, std::string dev_path);
- int stopInternal (int req_id);
+ int runInternal (uint32_t model_id, std::string dev_path);
+ int stopInternal (int req_id);
- int getProfile (int req_id, npu_profile *profile);
+ int getProfile (int req_id, npu_profile *profile);
- UtilModel *findModel (uint32_t model_id);
+ UtilModel *findModel (uint32_t model_id);
- static void callbackVerify (output_buffers *output, uint64_t sequence,
- void *data);
- static void callback (output_buffers *output, uint64_t sequence,
- void *data);
+ static void callbackVerify (output_buffers *output, uint64_t sequence,
+ void *data);
+ static void callback (output_buffers *output, uint64_t sequence, void *data);
- uint32_t wait ();
+ uint32_t wait ();
- protected:
- npudev_h dev_;
- std::vector<std::unique_ptr<UtilModel>> models_;
- npu_notimode noti_mode_;
- bool need_model_;
+ protected:
+ npudev_h dev_;
+ std::vector<std::unique_ptr<UtilModel>> models_;
+ npu_notimode noti_mode_;
+ bool need_model_;
- bool mute_;
- bool sync_;
- bool verify_;
+ bool mute_;
+ bool sync_;
+ bool verify_;
- std::string node_;
+ std::string node_;
- private:
- int set_constraint (uint32_t model_id, uint32_t timeout, npu_priority priority);
- int run_each (UtilModel *model, bool sync);
+ private:
+ int set_constraint (uint32_t model_id, uint32_t timeout,
+ npu_priority priority);
+ int run_each (UtilModel *model, bool sync);
- virtual bool check_version (npubin_meta *meta) { return false; }
- virtual int prepare_model (UtilModel *model) { return -EPERM; }
- virtual int set_data_info (npubin_meta *meta, uint32_t model_id) { return -EPERM; }
- virtual bool extract_node_id (uint32_t *id) { return false; }
+ virtual bool check_version (npubin_meta *meta) { return false; }
+ virtual int prepare_model (UtilModel *model) { return -EPERM; }
+ virtual int set_data_info (npubin_meta *meta, uint32_t model_id) {
+ return -EPERM;
+ }
+ virtual bool extract_node_id (uint32_t *id) { return false; }
- dev_type type_;
+ dev_type type_;
- static std::mutex m_;
- static std::condition_variable cv_;
+ static std::mutex m_;
+ static std::condition_variable cv_;
- static uint32_t success_;
- static uint32_t done_;
- static uint32_t total_;
+ static uint32_t success_;
+ static uint32_t done_;
+ static uint32_t total_;
- static bool dump_;
+ static bool dump_;
};
class UtilTRIV2 : public UtilTrinity {
- public:
- UtilTRIV2 () : UtilTrinity (NPUCOND_TRIV2_CONN_SOCIP, true, true) {}
-
- private:
- bool check_version (npubin_meta *meta);
- int set_data_info (npubin_meta *meta, uint32_t model_id);
- int prepare_model (UtilModel *model);
- bool extract_node_id (uint32_t *id);
+ public:
+ UtilTRIV2 () : UtilTrinity (NPUCOND_TRIV2_CONN_SOCIP, true, true) {}
+
+ private:
+ bool check_version (npubin_meta *meta);
+ int set_data_info (npubin_meta *meta, uint32_t model_id);
+ int prepare_model (UtilModel *model);
+ bool extract_node_id (uint32_t *id);
};
extern "C" {
};
const char msg_skipped_reason[][255] = {
- "",
- "",
- "Wrong number of arguments are provided.",
- "The given model is not compatible with this test.",
+ "",
+ "",
+ "Wrong number of arguments are provided.",
+ "The given model is not compatible with this test.",
};
-typedef int (*FUNC_APPTEST)(npudev_h, const char *, const char *);
+typedef int (*FUNC_APPTEST) (npudev_h, const char *, const char *);
/**
* @brief entry function to run apptest
* @param[in] buf_type The memory type of the buffer included in the model
* @return A generic buffer including dummy model data, NULL if error
*/
-generic_buffer* make_model (npudev_h dev, npubin_meta *meta, buffer_types buf_type);
+generic_buffer *make_model (npudev_h dev, npubin_meta *meta,
+ buffer_types buf_type);
/**
* @brief Destroy generic buffer with model data
* @param[in] output_size the output data size
* @return 0 if no error, otherwise a negative errno
*/
-int
-compare_data (const char *golden_path, const char *output_data,
- uint64_t output_size);
+int compare_data (const char *golden_path, const char *output_data,
+ uint64_t output_size);
#if defined(__cplusplus)
}
#include <errno.h>
class Inspector {
- public:
- Inspector ();
- ~Inspector ();
+ public:
+ Inspector ();
+ ~Inspector ();
- int init (std::string model);
- int run ();
+ int init (std::string model);
+ int run ();
- void set_dump_meta () { dump_meta_ = true; }
- void set_dump_program () { dump_program_ = true; }
- void set_dump_weight () { dump_weight_ = true; }
- void set_output_dir (std::string output) { dump_output_ = output; }
+ void set_dump_meta () { dump_meta_ = true; }
+ void set_dump_program () { dump_program_ = true; }
+ void set_dump_weight () { dump_weight_ = true; }
+ void set_output_dir (std::string output) { dump_output_ = output; }
- private:
- void dump ();
- int show ();
+ private:
+ void dump ();
+ int show ();
- void show_common ();
- void show_v1 ();
- void show_v2 ();
- void show_v3 ();
+ void show_common ();
+ void show_v1 ();
+ void show_v2 ();
+ void show_v3 ();
- npubin_meta * meta_;
- std::ifstream ifs_;
+ npubin_meta *meta_;
+ std::ifstream ifs_;
- std::string dump_output_;
+ std::string dump_output_;
- bool dump_meta_;
- bool dump_program_;
- bool dump_weight_;
+ bool dump_meta_;
+ bool dump_program_;
+ bool dump_weight_;
};
-Inspector::Inspector ()
-{
+Inspector::Inspector () {
meta_ = nullptr;
dump_meta_ = false;
dump_program_ = false;
dump_weight_ = false;
}
-Inspector::~Inspector ()
-{
+Inspector::~Inspector () {
if (meta_ != nullptr)
free (meta_);
}
-int Inspector::init (std::string model)
-{
+int
+Inspector::init (std::string model) {
std::cerr << "Inspecting model binary: " << model << "\n\n";
ifs_.open (model, std::ios::binary);
return -errno;
}
- ifs_.read (reinterpret_cast<char*>(meta_), NPUBIN_META_SIZE);
+ ifs_.read (reinterpret_cast<char *> (meta_), NPUBIN_META_SIZE);
if (!CHECK_NPUBIN (meta_->magiccode)) {
std::cerr << "Non-compatible binary format\n";
return -EINVAL;
return 0;
}
-int Inspector::run ()
-{
+int
+Inspector::run () {
int status = show ();
if (status == 0)
return status;
}
-int Inspector::show ()
-{
+int
+Inspector::show () {
uint64_t version = NPUBIN_VERSION (meta_->magiccode);
switch (version) {
return 0;
}
-void Inspector::show_common ()
-{
+void
+Inspector::show_common () {
std::ios_base::fmtflags f (std::cout.flags ());
- std::cout << "[common] " << "\n";
+ std::cout << "[common] "
+ << "\n";
std::cout << " model format: " << NPUBIN_VERSION (meta_->magiccode) << "\n";
std::cout << " model size: " << meta_->size << "\n";
- std::cout << " - metadata size: " << NPUBIN_META_TOTAL_SIZE (meta_->magiccode) << "\n";
+ std::cout << " - metadata size: "
+ << NPUBIN_META_TOTAL_SIZE (meta_->magiccode) << "\n";
std::cout << " - program size: " << meta_->program_size << "\n";
std::cout << " - weight size: " << meta_->weight_size << "\n";
std::cout << "\n";
std::cout.flags (f);
}
-void Inspector::show_v1 ()
-{
+void
+Inspector::show_v1 () {
show_common ();
std::ios_base::fmtflags f (std::cout.flags ());
- std::cout << "[npubinfmt v1] " << "\n";
+ std::cout << "[npubinfmt v1] "
+ << "\n";
std::cout << std::dec;
std::cout << " buffer_size: " << meta_->buffer_size << "\n";
std::cout << " input_offset: " << meta_->input_offset << "\n";
std::cout.flags (f);
}
-void Inspector::show_v2 ()
-{
+void
+Inspector::show_v2 () {
show_common ();
std::ios_base::fmtflags f (std::cout.flags ());
- std::cout << "[npubinfmt v2] " << "\n";
+ std::cout << "[npubinfmt v2] "
+ << "\n";
std::cout << std::dec;
std::cout << " input_num: " << meta_->input_num << "\n";
for (uint32_t i = 0; i < meta_->input_num; i++) {
std::cout << std::dec;
- std::cout << " input_offsets[" << i << "]: " << meta_->input_offsets[i] << "\n";
- std::cout << " input_elem_size[" << i << "]: " << meta_->input_elem_size[i] << "\n";
+ std::cout << " input_offsets[" << i << "]: " << meta_->input_offsets[i]
+ << "\n";
+ std::cout << " input_elem_size[" << i << "]: " << meta_->input_elem_size[i]
+ << "\n";
for (uint32_t j = 0; j < MAX_RANK; j++)
- std::cout << " input_dims[" << i << "][" << j << "]: " << meta_->input_dims[i][j] << "\n";
- std::cout << " input_emod_y[" << i << "]: " << meta_->input_emod_y[i] << "\n";
- std::cout << " input_emod_z[" << i << "]: " << meta_->input_emod_z[i] << "\n";
- std::cout << " input_quant_z[" << i << "]: " << meta_->input_quant_z[i] << "\n";
+ std::cout << " input_dims[" << i << "][" << j
+ << "]: " << meta_->input_dims[i][j] << "\n";
+ std::cout << " input_emod_y[" << i << "]: " << meta_->input_emod_y[i]
+ << "\n";
+ std::cout << " input_emod_z[" << i << "]: " << meta_->input_emod_z[i]
+ << "\n";
+ std::cout << " input_quant_z[" << i << "]: " << meta_->input_quant_z[i]
+ << "\n";
std::cout << std::fixed;
- std::cout << " input_quant_s[" << i << "]: " << meta_->input_quant_s[i] << "\n";
+ std::cout << " input_quant_s[" << i << "]: " << meta_->input_quant_s[i]
+ << "\n";
}
std::cout << std::dec << "\n";
std::cout << " output_num: " << meta_->output_num << "\n";
for (uint32_t i = 0; i < meta_->output_num; i++) {
std::cout << std::dec;
- std::cout << " output_offsets[" << i << "]: " << meta_->output_offsets[i] << "\n";
- std::cout << " output_elem_size[" << i << "]: " << meta_->output_elem_size[i] << "\n";
+ std::cout << " output_offsets[" << i << "]: " << meta_->output_offsets[i]
+ << "\n";
+ std::cout << " output_elem_size[" << i
+ << "]: " << meta_->output_elem_size[i] << "\n";
for (uint32_t j = 0; j < MAX_RANK; j++)
- std::cout << " output_dims[" << i << "][" << j << "]: " << meta_->output_dims[i][j] << "\n";
- std::cout << " output_emod_y[" << i << "]: " << meta_->output_emod_y[i] << "\n";
- std::cout << " output_emod_z[" << i << "]: " << meta_->output_emod_z[i] << "\n";
- std::cout << " output_quant_z[" << i << "]: " << meta_->output_quant_z[i] << "\n";
+ std::cout << " output_dims[" << i << "][" << j
+ << "]: " << meta_->output_dims[i][j] << "\n";
+ std::cout << " output_emod_y[" << i << "]: " << meta_->output_emod_y[i]
+ << "\n";
+ std::cout << " output_emod_z[" << i << "]: " << meta_->output_emod_z[i]
+ << "\n";
+ std::cout << " output_quant_z[" << i << "]: " << meta_->output_quant_z[i]
+ << "\n";
std::cout << std::fixed;
- std::cout << " output_quant_s[" << i << "]: " << meta_->output_quant_s[i] << "\n";
+ std::cout << " output_quant_s[" << i << "]: " << meta_->output_quant_s[i]
+ << "\n";
}
std::cout.flags (f);
}
-void Inspector::show_v3 ()
-{
+void
+Inspector::show_v3 () {
show_common ();
std::ios_base::fmtflags f (std::cout.flags ());
- std::cout << "[npubinfmt v3] " << "\n";
+ std::cout << "[npubinfmt v3] "
+ << "\n";
std::cout << std::dec;
std::cout << " segment_num: " << meta_->segment_num << "\n";
for (uint32_t i = 0; i < meta_->segment_num; i++) {
- std::cout << " segment_size[" << i << "]: " << meta_->segment_size[i] << "\n";
+ std::cout << " segment_size[" << i << "]: " << meta_->segment_size[i]
+ << "\n";
}
std::cout << "\n";
std::cout << " weight_seg_idx: " << meta_->weight_seg_idx << "\n";
std::cout << " input_seg_num: " << meta_->input_seg_num << "\n";
for (uint32_t i = 0; i < meta_->input_seg_num; i++) {
std::cout << std::dec;
- std::cout << " input_seg_idx[" << i << "]: " << meta_->input_seg_idx[i] << "\n";
- std::cout << " input_seg_off[" << i << "]: " << meta_->input_seg_off[i] << "\n";
+ std::cout << " input_seg_idx[" << i << "]: " << meta_->input_seg_idx[i]
+ << "\n";
+ std::cout << " input_seg_off[" << i << "]: " << meta_->input_seg_off[i]
+ << "\n";
for (uint32_t j = 0; j < MAX_RANK; j++)
- std::cout << " input_seg_dims[" << i << "][" << j << "]: " << meta_->input_seg_dims[i][j] << "\n";
- std::cout << " input_seg_emod_y[" << i << "]: " << meta_->input_seg_emod_y[i] << "\n";
- std::cout << " input_seg_emod_z[" << i << "]: " << meta_->input_seg_emod_z[i] << "\n";
- std::cout << " input_seg_quant_type[" << i << "]: " << meta_->input_seg_quant_type[i] << "\n";
- std::cout << " input_seg_quant_z[" << i << "]: " << meta_->input_seg_quant_z[i] << "\n";
+ std::cout << " input_seg_dims[" << i << "][" << j
+ << "]: " << meta_->input_seg_dims[i][j] << "\n";
+ std::cout << " input_seg_emod_y[" << i
+ << "]: " << meta_->input_seg_emod_y[i] << "\n";
+ std::cout << " input_seg_emod_z[" << i
+ << "]: " << meta_->input_seg_emod_z[i] << "\n";
+ std::cout << " input_seg_quant_type[" << i
+ << "]: " << meta_->input_seg_quant_type[i] << "\n";
+ std::cout << " input_seg_quant_z[" << i
+ << "]: " << meta_->input_seg_quant_z[i] << "\n";
std::cout << std::fixed;
- std::cout << " input_seg_quant_s[" << i << "]: " << meta_->input_seg_quant_s[i] << "\n";
+ std::cout << " input_seg_quant_s[" << i
+ << "]: " << meta_->input_seg_quant_s[i] << "\n";
}
std::cout << std::dec << "\n";
std::cout << " output_seg_num: " << meta_->output_seg_num << "\n";
for (uint32_t i = 0; i < meta_->output_seg_num; i++) {
std::cout << std::dec;
- std::cout << " output_seg_idx[" << i << "]: " << meta_->output_seg_idx[i] << "\n";
- std::cout << " output_seg_off[" << i << "]: " << meta_->output_seg_off[i] << "\n";
+ std::cout << " output_seg_idx[" << i << "]: " << meta_->output_seg_idx[i]
+ << "\n";
+ std::cout << " output_seg_off[" << i << "]: " << meta_->output_seg_off[i]
+ << "\n";
for (uint32_t j = 0; j < MAX_RANK; j++)
- std::cout << " output_seg_dims[" << i << "][" << j << "]: " << meta_->output_seg_dims[i][j] << "\n";
- std::cout << " output_seg_emod_y[" << i << "]: " << meta_->output_seg_emod_y[i] << "\n";
- std::cout << " output_seg_emod_z[" << i << "]: " << meta_->output_seg_emod_z[i] << "\n";
- std::cout << " output_seg_quant_type[" << i << "]: " << meta_->output_seg_quant_type[i] << "\n";
- std::cout << " output_seg_quant_z[" << i << "]: " << meta_->output_seg_quant_z[i] << "\n";
+ std::cout << " output_seg_dims[" << i << "][" << j
+ << "]: " << meta_->output_seg_dims[i][j] << "\n";
+ std::cout << " output_seg_emod_y[" << i
+ << "]: " << meta_->output_seg_emod_y[i] << "\n";
+ std::cout << " output_seg_emod_z[" << i
+ << "]: " << meta_->output_seg_emod_z[i] << "\n";
+ std::cout << " output_seg_quant_type[" << i
+ << "]: " << meta_->output_seg_quant_type[i] << "\n";
+ std::cout << " output_seg_quant_z[" << i
+ << "]: " << meta_->output_seg_quant_z[i] << "\n";
std::cout << std::fixed;
- std::cout << " output_seg_quant_s[" << i << "]: " << meta_->output_seg_quant_s[i] << "\n";
+ std::cout << " output_seg_quant_s[" << i
+ << "]: " << meta_->output_seg_quant_s[i] << "\n";
}
std::cout << std::dec << "\n";
std::cout.flags (f);
}
-void Inspector::dump ()
-{
+void
+Inspector::dump () {
size_t size_metadata = NPUBIN_META_TOTAL_SIZE (meta_->magiccode);
if (dump_meta_) {
std::ofstream ofs (dump_output_ + "/metadata.bin", std::ios::binary);
if (ofs.good ()) {
- char * buf = new char [size_metadata];
+ char *buf = new char[size_metadata];
ifs_.seekg (0);
ifs_.read (buf, size_metadata);
ofs.write (buf, size_metadata);
- delete [] buf;
+ delete[] buf;
}
}
if (dump_program_) {
std::ofstream ofs (dump_output_ + "/program.bin", std::ios::binary);
if (ofs.good ()) {
- char * buf = new char [meta_->program_size];
+ char *buf = new char[meta_->program_size];
ifs_.seekg (size_metadata);
ifs_.read (buf, meta_->program_size);
ofs.write (buf, meta_->program_size);
- delete [] buf;
+ delete[] buf;
}
}
if (dump_weight_) {
std::ofstream ofs (dump_output_ + "/weight.bin", std::ios::binary);
if (ofs.good ()) {
- char * buf = new char [meta_->weight_size];
+ char *buf = new char[meta_->weight_size];
ifs_.seekg (size_metadata + meta_->program_size);
ifs_.read (buf, meta_->weight_size);
ofs.write (buf, meta_->weight_size);
- delete [] buf;
+ delete[] buf;
}
}
}
static void
-print_usage (const char *prog_name)
-{
+print_usage (const char *prog_name) {
std::cerr << "Usage: " << prog_name << " [options] model_path\n";
std::cerr << "Options: \n";
std::cerr << " -o <arg> \t Set output directory\n";
std::cerr << " -w \t\t Extract weight section\n";
}
-int main (int argc, char **argv)
-{
+int
+main (int argc, char **argv) {
Inspector inspector;
int c, status = 0;
* @brief Emulated Dmabuf Impl.
*/
class EmulDmabuf {
- public:
- EmulDmabuf (int app_id) : fd_ (-1), shm_fd_ (-1), size_ (0) {
- std::string name;
-
- fd_ = global_fd_.fetch_add (1);
-
- /* create a shared memory */
- mode_t old_umask = umask (0);
- shm_name_ = "/triv2_" + std::to_string (app_id) + "_" + std::to_string (fd_);
- shm_fd_ = shm_open (shm_name_.c_str (), O_RDWR | O_CREAT, 0666);
- umask (old_umask);
- }
+ public:
+ EmulDmabuf (int app_id) : fd_ (-1), shm_fd_ (-1), size_ (0) {
+ std::string name;
+
+ fd_ = global_fd_.fetch_add (1);
+
+ /* create a shared memory */
+ mode_t old_umask = umask (0);
+ shm_name_ =
+ "/triv2_" + std::to_string (app_id) + "_" + std::to_string (fd_);
+ shm_fd_ = shm_open (shm_name_.c_str (), O_RDWR | O_CREAT, 0666);
+ umask (old_umask);
+ }
- ~EmulDmabuf () {
- if (shm_fd_ < 0)
- return;
+ ~EmulDmabuf () {
+ if (shm_fd_ < 0)
+ return;
- shm_unlink (shm_name_.c_str ());
- close (shm_fd_);
- }
+ shm_unlink (shm_name_.c_str ());
+ close (shm_fd_);
+ }
- int setSize (size_t size) {
- int err;
+ int setSize (size_t size) {
+ int err;
- if (shm_fd_ < 0)
- return -EINVAL;
+ if (shm_fd_ < 0)
+ return -EINVAL;
- /* change file size */
- err = ftruncate (shm_fd_, size);
- if (err != 0)
- return err;
+ /* change file size */
+ err = ftruncate (shm_fd_, size);
+ if (err != 0)
+ return err;
- size_ = size;
- return 0;
- }
+ size_ = size;
+ return 0;
+ }
- size_t getSize () const { return size_; }
- int getFD () const { return fd_; }
+ size_t getSize () const { return size_; }
+ int getFD () const { return fd_; }
- private:
- static std::atomic<int> global_fd_;
+ private:
+ static std::atomic<int> global_fd_;
- int fd_;
- int shm_fd_;
- std::string shm_name_;
- size_t size_;
+ int fd_;
+ int shm_fd_;
+ std::string shm_name_;
+ size_t size_;
};
/** @brief Global mapping of dmabuf */
* @brief TRIV2 impl. of get_version ioctl()
*/
static int
-triv2_get_version (trinity_cuse_context *ctx, uint32_t *version)
-{
+triv2_get_version (trinity_cuse_context *ctx, uint32_t *version) {
*version = trinity_gen_ver (TRINITY_DEV_VISION2_CUSE, 2, 0, 0);
return 0;
}
* @brief TRIV2 impl. of get_api_level ioctl()
*/
static int
-triv2_get_api_level (trinity_cuse_context *ctx, uint32_t *api_level)
-{
+triv2_get_api_level (trinity_cuse_context *ctx, uint32_t *api_level) {
*api_level = TRINITY_API_LEVEL;
return 0;
}
* @brief TRIV2 impl. of get_state ioctl()
*/
static int
-triv2_get_state (trinity_cuse_context *ctx, uint32_t *state)
-{
+triv2_get_state (trinity_cuse_context *ctx, uint32_t *state) {
*state = STATE_READY;
return 0;
}
* @brief TRIV2 impl. of get_tops ioctl()
*/
static int
-triv2_get_tops (trinity_cuse_context *ctx, uint32_t *tops)
-{
+triv2_get_tops (trinity_cuse_context *ctx, uint32_t *tops) {
*tops = 2;
return 0;
}
* @brief TRIV2 impl. of get_dspm ioctl()
*/
static int
-triv2_get_dspm (trinity_cuse_context *ctx, uint32_t *dspm)
-{
+triv2_get_dspm (trinity_cuse_context *ctx, uint32_t *dspm) {
*dspm = UINT32_MAX;
return 0;
}
* @brief TRIV2 impl. of hwmem_alloc ioctl()
*/
static int
-triv2_hwmem_alloc (trinity_cuse_context *ctx, const struct trinity_ioctl_hwmem * hwmem)
-{
+triv2_hwmem_alloc (trinity_cuse_context *ctx,
+ const struct trinity_ioctl_hwmem *hwmem) {
EmulDmabuf *dmabuf;
size_t size;
* @brief TRIV2 impl. of hwmem_dealloc ioctl()
*/
static int
-triv2_hwmem_dealloc (trinity_cuse_context *ctx, const struct trinity_ioctl_hwmem * hwmem)
-{
+triv2_hwmem_dealloc (trinity_cuse_context *ctx,
+ const struct trinity_ioctl_hwmem *hwmem) {
return global_dmabuf_map.remove (hwmem->dbuf_fd);
}
/** @brief The ioctl vtable for TRIV2 devices */
-static trinity_cuse_ioctl_vtable triv2_vtable {
- /* Device Info. */
- .get_version = triv2_get_version,
- .get_api_level = triv2_get_api_level,
- .get_state = triv2_get_state,
- .get_tops = triv2_get_tops,
- .get_dspm = triv2_get_dspm,
- /* Device Control */
- .hwmem_alloc = triv2_hwmem_alloc,
- .hwmem_dealloc = triv2_hwmem_dealloc,
+static trinity_cuse_ioctl_vtable triv2_vtable{
+ /* Device Info. */
+ .get_version = triv2_get_version,
+ .get_api_level = triv2_get_api_level,
+ .get_state = triv2_get_state,
+ .get_tops = triv2_get_tops,
+ .get_dspm = triv2_get_dspm,
+ /* Device Control */
+ .hwmem_alloc = triv2_hwmem_alloc,
+ .hwmem_dealloc = triv2_hwmem_dealloc,
};
/** @brief Setting the ioctl vtable */
bool
-set_ioctl_vtable_triv2 (trinity_cuse_ioctl_vtable ** vtable) {
+set_ioctl_vtable_triv2 (trinity_cuse_ioctl_vtable **vtable) {
if (vtable == nullptr)
return false;
* @brief Find the ioctl vtable for the given device type
*/
bool
-find_ioctl_vtable (const std::string &dev_name)
-{
+find_ioctl_vtable (const std::string &dev_name) {
if (dev_name == "triv2")
return set_ioctl_vtable_triv2 (&vtable);
* @brief impl. of .open ()
*/
static void
-trinity_cuse_open (fuse_req_t req, struct fuse_file_info *fi)
-{
+trinity_cuse_open (fuse_req_t req, struct fuse_file_info *fi) {
trinity_cuse_context *ctx;
ctx = (trinity_cuse_context *) malloc (sizeof (*ctx));
ctx->app_id = fuse_req_ctx (req)->pid;
fi->fh = (uint64_t) ctx;
- fuse_reply_open(req, fi);
+ fuse_reply_open (req, fi);
}
/**
* @brief impl. of .release ()
*/
static void
-trinity_cuse_release (fuse_req_t req, struct fuse_file_info *fi)
-{
+trinity_cuse_release (fuse_req_t req, struct fuse_file_info *fi) {
trinity_cuse_context *ctx = (trinity_cuse_context *) fi->fh;
if (ctx)
*/
static void
trinity_cuse_ioctl (fuse_req_t req, int cmd, void *arg,
- struct fuse_file_info *fi, unsigned int flags,
- const void *in_buf, size_t in_size, size_t out_size)
-{
+ struct fuse_file_info *fi, unsigned int flags,
+ const void *in_buf, size_t in_size, size_t out_size) {
trinity_cuse_context *ctx = (trinity_cuse_context *) fi->fh;
if (flags & FUSE_IOCTL_COMPAT) {
switch (cmd) {
case TRINITY_IOCTL_GET_VERSION:
if (!out_size) {
- struct iovec iov = { arg, sizeof (uint32_t) };
+ struct iovec iov = {arg, sizeof (uint32_t)};
fuse_reply_ioctl_retry (req, NULL, 0, &iov, 1);
} else {
uint32_t val;
break;
case TRINITY_IOCTL_GET_API_LEVEL:
if (!out_size) {
- struct iovec iov = { arg, sizeof (uint32_t) };
+ struct iovec iov = {arg, sizeof (uint32_t)};
fuse_reply_ioctl_retry (req, NULL, 0, &iov, 1);
} else {
uint32_t val;
break;
case TRINITY_IOCTL_GET_STATE:
if (!out_size) {
- struct iovec iov = { arg, sizeof (uint32_t) };
+ struct iovec iov = {arg, sizeof (uint32_t)};
fuse_reply_ioctl_retry (req, NULL, 0, &iov, 1);
} else {
uint32_t val;
break;
case TRINITY_IOCTL_GET_TOPS:
if (!out_size) {
- struct iovec iov = { arg, sizeof (uint32_t) };
+ struct iovec iov = {arg, sizeof (uint32_t)};
fuse_reply_ioctl_retry (req, NULL, 0, &iov, 1);
} else {
uint32_t val;
break;
case TRINITY_IOCTL_GET_DSPM:
if (!out_size) {
- struct iovec iov = { arg, sizeof (uint32_t) };
+ struct iovec iov = {arg, sizeof (uint32_t)};
fuse_reply_ioctl_retry (req, NULL, 0, &iov, 1);
} else {
uint32_t val;
break;
case TRINITY_IOCTL_HWMEM_ALLOC:
if (!in_size) {
- struct iovec iov = { arg, sizeof (struct trinity_ioctl_hwmem) };
+ struct iovec iov = {arg, sizeof (struct trinity_ioctl_hwmem)};
fuse_reply_ioctl_retry (req, &iov, 1, NULL, 0);
} else {
const struct trinity_ioctl_hwmem *val =
- static_cast<const struct trinity_ioctl_hwmem *> (in_buf);
+ static_cast<const struct trinity_ioctl_hwmem *> (in_buf);
fuse_reply_ioctl (req, vtable->hwmem_alloc (ctx, val), NULL, 0);
}
break;
case TRINITY_IOCTL_HWMEM_DEALLOC:
if (!in_size) {
- struct iovec iov = { arg, sizeof (struct trinity_ioctl_hwmem) };
+ struct iovec iov = {arg, sizeof (struct trinity_ioctl_hwmem)};
fuse_reply_ioctl_retry (req, &iov, 1, NULL, 0);
} else {
const struct trinity_ioctl_hwmem *val =
- static_cast<const struct trinity_ioctl_hwmem *> (in_buf);
+ static_cast<const struct trinity_ioctl_hwmem *> (in_buf);
fuse_reply_ioctl (req, vtable->hwmem_dealloc (ctx, val), NULL, 0);
}
break;
/** @brief Structure to describe parameters */
struct trinity_cuse_param {
- unsigned int major;
- unsigned int minor;
- std::string dev_name;
- bool is_help;
+ unsigned int major;
+ unsigned int minor;
+ std::string dev_name;
+ bool is_help;
};
-#define TRINITY_OPT(t, p) { t, offsetof (struct trinity_cuse_param, p), 1 }
+#define TRINITY_OPT(t, p) \
+ { t, offsetof (struct trinity_cuse_param, p), 1 }
-enum {
- KEY_HELP
-};
+enum { KEY_HELP };
/** @brief The definitions of available options */
static const struct fuse_opt trinity_cuse_opts[] = {
- TRINITY_OPT("-M %u", major),
- TRINITY_OPT("--maj=%u", major),
- TRINITY_OPT("-m %u", minor),
- TRINITY_OPT("--min=%u", minor),
- FUSE_OPT_KEY("-h", KEY_HELP),
- FUSE_OPT_KEY("--help", KEY_HELP),
- FUSE_OPT_END
-};
+ TRINITY_OPT ("-M %u", major),
+ TRINITY_OPT ("--maj=%u", major),
+ TRINITY_OPT ("-m %u", minor),
+ TRINITY_OPT ("--min=%u", minor),
+ FUSE_OPT_KEY ("-h", KEY_HELP),
+ FUSE_OPT_KEY ("--help", KEY_HELP),
+ FUSE_OPT_END};
/** @brief Print the help message */
static void
-print_help_message ()
-{
+print_help_message () {
static const char *usage =
- "\nUsage: trinity-cuse [options] [device name]\n"
- "options:\n"
- " --maj=MAJ|-M MAJ device major number\n"
- " --min=MIN|-m MIN device minor number\n"
- " --help|-h print this help message\n";
+ "\nUsage: trinity-cuse [options] [device name]\n"
+ "options:\n"
+ " --maj=MAJ|-M MAJ device major number\n"
+ " --min=MIN|-m MIN device minor number\n"
+ " --help|-h print this help message\n";
std::cerr << usage;
}
/** @brief Parse the given arguments */
static int
-trinity_cuse_parse_args(void *data, const char *arg, int key,
- struct fuse_args *outargs)
-{
+trinity_cuse_parse_args (void *data, const char *arg, int key,
+ struct fuse_args *outargs) {
struct trinity_cuse_param *param = (struct trinity_cuse_param *) data;
- (void)outargs;
- (void)arg;
+ (void) outargs;
+ (void) arg;
switch (key) {
- case FUSE_OPT_KEY_NONOPT:
- param->dev_name = std::string (arg);
- return 0;
- case KEY_HELP:
- param->is_help = true;
- print_help_message ();
- return fuse_opt_add_arg (outargs, "-ho");
- default:
- return 1;
+ case FUSE_OPT_KEY_NONOPT:
+ param->dev_name = std::string (arg);
+ return 0;
+ case KEY_HELP:
+ param->is_help = true;
+ print_help_message ();
+ return fuse_opt_add_arg (outargs, "-ho");
+ default:
+ return 1;
}
}
/** @brief trinity cuse driver's lowlevel ops */
static const struct cuse_lowlevel_ops trinity_cuse_clop = {
- .init = nullptr,
- .init_done = nullptr,
- .destroy = nullptr,
- .open = trinity_cuse_open,
- .read = nullptr,
- .write = nullptr,
- .flush = nullptr,
- .release = trinity_cuse_release,
- .fsync = nullptr,
- .ioctl = trinity_cuse_ioctl,
- .poll = nullptr
-};
+ .init = nullptr,
+ .init_done = nullptr,
+ .destroy = nullptr,
+ .open = trinity_cuse_open,
+ .read = nullptr,
+ .write = nullptr,
+ .flush = nullptr,
+ .release = trinity_cuse_release,
+ .fsync = nullptr,
+ .ioctl = trinity_cuse_ioctl,
+ .poll = nullptr};
/**
* @brief main routine of trinity-cuse
*/
int
-main (int argc, char **argv)
-{
+main (int argc, char **argv) {
struct fuse_args args = FUSE_ARGS_INIT (argc, argv);
- struct trinity_cuse_param param = { 0 };
- struct cuse_info ci = { 0 };
+ struct trinity_cuse_param param = {0};
+ struct cuse_info ci = {0};
std::string dev_name ("DEVNAME=");
- if (fuse_opt_parse (&args, ¶m, trinity_cuse_opts, trinity_cuse_parse_args)) {
+ if (fuse_opt_parse (&args, ¶m, trinity_cuse_opts,
+ trinity_cuse_parse_args)) {
std::cerr << "[Error] Failed to parse options\n";
return -EINVAL;
}
dev_name += param.dev_name;
dev_name += "-0";
- const char *dev_info_argv[] = { dev_name.c_str() };
+ const char *dev_info_argv[] = {dev_name.c_str ()};
ci.dev_major = param.major;
ci.dev_minor = param.minor;
ci.dev_info_argv = dev_info_argv;
ci.flags = CUSE_UNRESTRICTED_IOCTL;
- return cuse_lowlevel_main (args.argc, args.argv, &ci, &trinity_cuse_clop, NULL);
+ return cuse_lowlevel_main (args.argc, args.argv, &ci, &trinity_cuse_clop,
+ NULL);
}
int (*get_tops) (trinity_cuse_context *, uint32_t *);
int (*get_dspm) (trinity_cuse_context *, uint32_t *);
/* Device Control */
- int (*hwmem_alloc) (trinity_cuse_context *, const struct trinity_ioctl_hwmem *);
- int (*hwmem_dealloc) (trinity_cuse_context *, const struct trinity_ioctl_hwmem *);
+ int (*hwmem_alloc) (trinity_cuse_context *,
+ const struct trinity_ioctl_hwmem *);
+ int (*hwmem_dealloc) (trinity_cuse_context *,
+ const struct trinity_ioctl_hwmem *);
} trinity_cuse_ioctl_vtable;
#endif
using namespace std;
-static struct option longopts[] = {
- {"help", no_argument, 0, 'h'},
- {"device-node", optional_argument, 0, 'd'},
- {"app-id", optional_argument, 0, 'a'},
- {"interval", optional_argument, 0, 'i'},
- {0, 0, 0, 0}
-};
+static struct option longopts[] = {{"help", no_argument, 0, 'h'},
+ {"device-node", optional_argument, 0, 'd'},
+ {"app-id", optional_argument, 0, 'a'},
+ {"interval", optional_argument, 0, 'i'},
+ {0, 0, 0, 0}};
/** @brief Class of Trinity SMI (Trinity System Management Interface) */
class TrinitySMI {
- public:
- TrinitySMI () : help_only_ (false) {}
- ~TrinitySMI () {}
+ public:
+ TrinitySMI () : help_only_ (false) {}
+ ~TrinitySMI () {}
- void parse_arguments (int argc, char ** argv);
- void check_arguments (int argc, char ** argv);
+ void parse_arguments (int argc, char **argv);
+ void check_arguments (int argc, char **argv);
- void append_version ();
- void append_stats ();
+ void append_version ();
+ void append_stats ();
- void dump (bool use_curses);
- int loop ();
+ void dump (bool use_curses);
+ int loop ();
- void set_device (string node) { node_ = node; }
- void set_app_id (string app_id) { app_id_ = app_id; }
- void set_interval (string interval) { interval_ = interval; }
+ void set_device (string node) { node_ = node; }
+ void set_app_id (string app_id) { app_id_ = app_id; }
+ void set_interval (string interval) { interval_ = interval; }
- static bool signal_exit;
+ static bool signal_exit;
- private:
- uint32_t get_api_level ();
- bool parse_device_node (dev_type & type, int & dev_id);
- bool parse_app_id (int & app_id);
- string to_hex_string (uint64_t val);
+ private:
+ uint32_t get_api_level ();
+ bool parse_device_node (dev_type &type, int &dev_id);
+ bool parse_app_id (int &app_id);
+ string to_hex_string (uint64_t val);
- void append_help ();
- void append_devices ();
- void append_apps ();
- void append_reqs ();
+ void append_help ();
+ void append_devices ();
+ void append_apps ();
+ void append_reqs ();
- string name_;
- stringstream ss_;
+ string name_;
+ stringstream ss_;
- string help_msg_;
- bool help_only_;
+ string help_msg_;
+ bool help_only_;
- string app_id_;
- string node_;
- string interval_;
+ string app_id_;
+ string node_;
+ string interval_;
};
/** @brief get driver api level from any device */
uint32_t
-TrinitySMI::get_api_level ()
-{
+TrinitySMI::get_api_level () {
dev_type types[] = {NPUCOND_TRIV2_CONN_SOCIP};
uint32_t level = 0;
/** @brief parse the given device node */
bool
-TrinitySMI::parse_device_node (dev_type & type, int & dev_id)
-{
- const char * last = strrchr (node_.c_str(), '-');
+TrinitySMI::parse_device_node (dev_type &type, int &dev_id) {
+ const char *last = strrchr (node_.c_str (), '-');
dev_id = -1;
if (last != NULL && *(last + 1) != '\x00') {
type = NPUCOND_TRIV2_CONN_SOCIP;
if (dev_id < 0 || type == NPUCOND_CONN_UNKNOWN) {
- ss_ << "| Invalid device node provided: " << left << setw(29) << node_ << "|\n";
+ ss_ << "| Invalid device node provided: " << left << setw (29) << node_
+ << "|\n";
ss_ << "+------------------------------------------------------------+\n";
return false;
/** @brief parse the given app id */
bool
-TrinitySMI::parse_app_id (int & app_id)
-{
- const char *nptr = app_id_.c_str();
+TrinitySMI::parse_app_id (int &app_id) {
+ const char *nptr = app_id_.c_str ();
char *endptr;
errno = 0;
app_id = strtol (nptr, &endptr, 10);
if (errno != 0 || nptr == endptr) {
- ss_ << "| Invalid app id provided: " << left << setw(34) << app_id_ << "|\n";
+ ss_ << "| Invalid app id provided: " << left << setw (34) << app_id_
+ << "|\n";
ss_ << "+------------------------------------------------------------+\n";
return false;
/** @brief convert a value to the hex string */
string
-TrinitySMI::to_hex_string (uint64_t val)
-{
+TrinitySMI::to_hex_string (uint64_t val) {
stringstream ss;
ss << "0x";
ss << hex << val;
- return ss.str();
+ return ss.str ();
}
/** @brief show the version of npu-engine and driver */
void
-TrinitySMI::append_version ()
-{
+TrinitySMI::append_version () {
auto now = chrono::system_clock::now ();
time_t now_time = chrono::system_clock::to_time_t (now);
string smi_ver, lib_ver;
/** @brief main routine to append stats */
void
-TrinitySMI::append_stats ()
-{
+TrinitySMI::append_stats () {
if (!node_.empty ()) {
if (!app_id_.empty ()) {
append_reqs ();
/** @brief show the list of trinity devices */
void
-TrinitySMI::append_devices ()
-{
+TrinitySMI::append_devices () {
dev_type types[] = {
- NPUCOND_TRIV2_CONN_SOCIP,
+ NPUCOND_TRIV2_CONN_SOCIP,
};
ss_ << "\n";
}
}
if (count == 0)
- ss_ << "| " << left << setw (59) << "No available device detected" << "|\n";
+ ss_ << "| " << left << setw (59) << "No available device detected"
+ << "|\n";
ss_ << "+------------------------------------------------------------+\n";
}
/** @brief show the list of executed apps */
void
-TrinitySMI::append_apps ()
-{
+TrinitySMI::append_apps () {
ss_ << "\n";
ss_ << "+------------------------------------------------------------+\n";
ss_ << "| APP ID | APP NAME | STATUS | MEM (KiB) | # RUNS |\n";
else
status_str = string ("unknown");
- ss_ << "| " << right << setw(6) << stat->app_id << " ";
- ss_ << "| " << right << setw(15) << stat->name << " ";
- ss_ << "| " << right << setw(10) << status_str << " ";
- ss_ << "| " << right << setw(9) << fixed << setprecision(2);
+ ss_ << "| " << right << setw (6) << stat->app_id << " ";
+ ss_ << "| " << right << setw (15) << stat->name << " ";
+ ss_ << "| " << right << setw (10) << status_str << " ";
+ ss_ << "| " << right << setw (9) << fixed << setprecision (2);
ss_ << (((double) stat->total_alloc_mem) / 1024.0) << " ";
- ss_ << "| " << right << setw(6) << stat->num_total_reqs << " |\n";
+ ss_ << "| " << right << setw (6) << stat->num_total_reqs << " |\n";
count++;
}
putNPUdevice (dev);
out:
if (count == 0)
- ss_ << "| " << left << setw (59) << "No executed applications found" << "|\n";
+ ss_ << "| " << left << setw (59) << "No executed applications found"
+ << "|\n";
#else
- ss_ << "| " << left << setw (59) << "Not supported envionment" << "|\n";
+ ss_ << "| " << left << setw (59) << "Not supported envionment"
+ << "|\n";
#endif
ss_ << "+------------------------------------------------------------+\n";
/** @brief show the list of submitted reqs */
void
-TrinitySMI::append_reqs ()
-{
+TrinitySMI::append_reqs () {
ss_ << "\n";
ss_ << "+------------------------------------------------------------+\n";
ss_ << "| req ID | MODEL ID | PRIO | STATUS | SCHED | INFER |\n";
else
status_str = string ("unknown");
- ss_ << "| " << right << setw(7) << stat->req_id << " ";
- ss_ << "| " << right << setw(10) << to_hex_string (stat->model_id) << " ";
- ss_ << "| " << right << setw(4) << priority_str << " ";
- ss_ << "| " << right << setw(8) << status_str << " ";
- ss_ << "| " << right << setw(7) << to_string (stat->sched_time) + "ms" << " ";
- ss_ << "| " << right << setw(7) << to_string (stat->infer_time) + "ms" << " |\n";
+ ss_ << "| " << right << setw (7) << stat->req_id << " ";
+ ss_ << "| " << right << setw (10) << to_hex_string (stat->model_id) << " ";
+ ss_ << "| " << right << setw (4) << priority_str << " ";
+ ss_ << "| " << right << setw (8) << status_str << " ";
+ ss_ << "| " << right << setw (7) << to_string (stat->sched_time) + "ms"
+ << " ";
+ ss_ << "| " << right << setw (7) << to_string (stat->infer_time) + "ms"
+ << " |\n";
count++;
}
putNPUdevice (dev);
out:
if (count == 0)
- ss_ << "| " << left << setw (59) << "No submitted reqs found" << "|\n";
+ ss_ << "| " << left << setw (59) << "No submitted reqs found"
+ << "|\n";
#else
- ss_ << "| " << left << setw (59) << "Not supported envionment" << "|\n";
+ ss_ << "| " << left << setw (59) << "Not supported envionment"
+ << "|\n";
#endif
ss_ << "+------------------------------------------------------------+\n";
/** @brief show the usage and help messages */
void
-TrinitySMI::append_help ()
-{
+TrinitySMI::append_help () {
ss_ << help_msg_;
ss_ << "\nUsage: " << name_ << " [options]\n";
ss_ << "Options:\n";
ss_ << "\t\t\t\t(e.g., -d /dev/triv2-0)\n";
ss_ << " -a, --app-id=<id>\t\tApp id that you're interested in\n";
ss_ << "\t\t\t\t(e.g., -d /dev/triv2-0 -a 125)\n";
- ss_ << " -i, --interval=<secs>\t\tInterval in seconds to wait between updates\n";
+ ss_ << " -i, --interval=<secs>\t\tInterval in seconds to wait between "
+ "updates\n";
ss_ << "\t\t\t\t(e.g., -i 1)\n";
}
/** @brief show the appended string to screen */
void
-TrinitySMI::dump (bool use_curses)
-{
+TrinitySMI::dump (bool use_curses) {
string s = ss_.str ();
if (use_curses)
}
static uint32_t
-get_interval (const char * interval_str)
-{
+get_interval (const char *interval_str) {
char *endptr;
unsigned long interval = 0;
bool TrinitySMI::signal_exit = false;
-static void die(int notused)
-{
+static void
+die (int notused) {
TrinitySMI::signal_exit = true;
}
int
-TrinitySMI::loop ()
-{
+TrinitySMI::loop () {
uint32_t interval = get_interval (interval_.c_str ());
bool use_curses = (interval != 0 && !help_only_);
append_stats ();
dump (use_curses);
- refresh();
+ refresh ();
if (interval != 0)
sleep (interval);
} while (use_curses && !TrinitySMI::signal_exit);
if (use_curses)
- endwin();
+ endwin ();
return 0;
}
/** @brief check the given arguments */
void
-TrinitySMI::check_arguments (int argc, char ** argv)
-{
+TrinitySMI::check_arguments (int argc, char **argv) {
int c;
optind = 0;
opterr = 0;
- while ((c = getopt_long (argc, argv, "hd:a:i:", longopts, (int *) 0)) != EOF) {
+ while ((c = getopt_long (argc, argv, "hd:a:i:", longopts, (int *) 0)) !=
+ EOF) {
switch (c) {
case 'd':
set_device (optarg);
else if (optopt == 'i')
help_msg_ = "\nOption '-i' requires an extra argument\n";
else
- help_msg_ = "\nUnknown flag detected: " + to_string ((char) optopt) + "\n";
+ help_msg_ =
+ "\nUnknown flag detected: " + to_string ((char) optopt) + "\n";
// no-break
case 'h':
help_only_ = true;
}
if (!app_id_.empty () && node_.empty ()) {
- help_msg_ = "\nOption '-a' requires Option '-d' as well, to specify a device node\n";
+ help_msg_ =
+ "\nOption '-a' requires Option '-d' as well, to specify a device "
+ "node\n";
help_only_ = true;
}
}
void
-TrinitySMI::parse_arguments (int argc, char ** argv)
-{
- char * name = argv[0];
- const char * last = strrchr (name, '/');
+TrinitySMI::parse_arguments (int argc, char **argv) {
+ char *name = argv[0];
+ const char *last = strrchr (name, '/');
if (last != NULL) {
name_ = string (last + 1);
}
/** @brief main routine of trinity-smi */
-int main (int argc, char **argv)
-{
+int
+main (int argc, char **argv) {
TrinitySMI smi;
smi.parse_arguments (argc, argv);