Skip to content

API Deployment Flow for NPU Models*

1. Using the NPU*

After generating the model file with the NPU compiler, you need to deploy the model file to the board. The model file generated by the NPU compiler is a C file. Combined with the API interfaces we provide, the model can be run on the board.

1.1 API Call Flow*

  • Open SNPU
  • Load the model and input data, run the model, and obtain the output data.
  • Close SNPU
gx_snpu.h
    /**
     * @brief Initialize snpu
     *
     *
     * @return Whether snpu initialization is successful
     * @retval 0 Normal
     * @retval -1 Failed
     */
    int gx_snpu_init(void);

    /**
     * @brief Close snpu
     *
     *
     * @return Whether snpu shutdown is successful
     * @retval 0 Normal
     * @retval -1 Failed
     */
    int gx_snpu_exit(void);

    /**
     * @brief snpu input and output data format
    */
    typedef short GX_SNPU_FLOAT;

    /**
     * @brief snpu working state
    */
    typedef enum {
        GX_SNPU_IDLE,    /**< Idle state*/
        GX_SNPU_BUSY,    /**< Busy state*/
        GX_SNPU_STALL,   /**< Stalled state*/
    } GX_SNPU_STATE;

    /**
     * @brief snpu callback function, executed in the interrupt after the task is completed
    */
    typedef int (*GX_SNPU_CALLBACK)(int module_id, GX_SNPU_STATE state, void *private_data);

    /**
     * @brief snpu work task
    */
    typedef struct {
        int module_id;    /**< Module identifier          module id defined by programmer*/
        void *data;       /**< Temporary data content area    data_content in model.h*/
        void *input;      /**< Model input data        input in model.h*/
        void *output;     /**< Model output data        output in model.h*/
        void *cmd;        /**< Instruction content area        cmd_content in model.h*/
        void *weight;     /**< Weight content area        weight_content in model.h*/
        void *cache;      /**< Weight cache area        cache_content in model.h*/
    } GX_SNPU_TASK;

    /**
     * FFT operator type
     */
    typedef enum {
        GX_SNPU_REAL_FFT_256          = 0x00, /*!< 256-point real FFT   input 256 shorts, output 258 shorts */
        GX_SNPU_REAL_IFFT_256         = 0x01, /*!< 256-point real IFFT  input 258 shorts, output 256 ints */
        GX_SNPU_CPLX_FFT_256          = 0x02, /*!< 256-point complex FFT   input 512 shorts, output 512 shorts */
        GX_SNPU_CPLX_IFFT_256         = 0x03, /*!< 256-point complex IFFT  input 512 shorts, output 512 shorts */
        GX_SNPU_REAL_FFT_512          = 0x04, /*!< 512-point real FFT   input 512 shorts, output 514 shorts */
        GX_SNPU_REAL_IFFT_512         = 0x05, /*!< 512-point real IFFT  input 514 shorts, output 512 ints */
        GX_SNPU_CPLX_FFT_512          = 0x06, /*!< 512-point complex FFT   input 1024 shorts, output 1024 shorts */
        GX_SNPU_CPLX_IFFT_512         = 0x07, /*!< 512-point complex IFFT  input 1024 shorts, output 1024 shorts */
    } GX_SNPU_FFT_TYPE;

    /**
     * FFT operator information
     */
    typedef struct {
        int ifft_input_shift;              /*!< Externally configured ifft data shift, signed number, positive means right shift, negative means left shift*/
        unsigned int phy_fft_src_addr;     /*!< Computation data input address, 4-byte aligned */
        unsigned int phy_fft_dst_addr;     /*!< Computation result destination address, 4-byte aligned */
        GX_SNPU_FFT_TYPE fft_type;         /*!< Operator type */
    } GX_SNPU_FFT_INFO;

    /**
     * @brief Run a work task asynchronously
     *
     * @param task Work task, for details please refer to gxdocref GX_SNPU_TASK
     * @param callback Callback function, executed in the interrupt after the task is completed, for details please refer to gxdocref GX_SNPU_CALLBACK
     * @param private_data Callback function parameter
     *
     * @return snpu task working state
     * @retval 0  Normal
     * @retval -1 Failed
     */
    int gx_snpu_run_task(GX_SNPU_TASK *task, GX_SNPU_CALLBACK callback, void *private_data);

    /**
     * @brief Run a work task synchronously
     *
     * @param task Work task, for details please refer to gxdocref GX_SNPU_TASK
     *
     * @return snpu task working state
     * @retval 0  Normal
     * @retval -1 Failed
     */
    int gx_snpu_run_task_sync(GX_SNPU_TASK *task);

    /**
     * @brief Run the FFT operator synchronously. Before calling this function, gx_snpu_pause must be called first. After completion, gx_snpu_resume must be called to resume model execution.
     *
     * @param fft_info FFT operator information, for details please refer to gxdocref GX_SNPU_FFT_INFO
     * @param out_shift The FFT computation output is of type short. To obtain float32, an out_shift operation is required. A positive shift means left shift; a negative shift means right shift.
     *
     * @return snpu task working state
     * @retval 0  Normal
     * @retval -1 Failed
     */
    int gx_snpu_run_fft(GX_SNPU_FFT_INFO *fft_info, int *out_shift);

    /**
     * @brief snpu get working state
     *
     * @return snpu task working state
     * @retval GX_SNPU_IDLE    Idle state
     * @retval GX_SNPU_BUSY    Busy state
     * @retval GX_SNPU_STALL   Stalled state
    */
    GX_SNPU_STATE gx_snpu_get_state(void);

    /**
     * @brief Pause snpu execution
     *
     * @return snpu task working state
     * @retval 0  Normal
     * @retval -1 Failed
    */
    int gx_snpu_pause(void);

    /**
     * @brief Resume snpu execution
     *
     * @return snpu task working state
     * @retval 0  Normal
     * @retval -1 Failed
    */
    int gx_snpu_resume(void);