Skip to content

Model

#include <qbruntime/model.h>

class mobilint::Model

Represents an AI model loaded from an MXQ file.

This class loads an AI model from an MXQ file and provides functions to launch it on the NPU and perform inference.

NHWC float-to-float inference

Performs inference with input and output elements of type float in NHWC (batch N, height H, width W, channels C) or HWC format.

Two input-output type pairs are supported:

  1. std::vector<NDArray<float>> for both input and output
    • Recommended approach, as NDArray allows the qbruntime to avoid unnecessary data copies internally.
  2. std::vector<float*> for input and std::vector<std::vector<float>> for output
    • Provided for user convenience, but results in unavoidable extra copies within the qbruntime.
Member Description
infer ×10 Performs inference.

infer

StatusCode infer(const std::vector<NDArray<float>> &input, std::vector<NDArray<float>> &output)

Performs inference.

Parameters

  • input [in] — A vector of NDArray<float>. Each NDArray must be in NHWC or HWC format.
  • output [out] — A reference to a vector of NDArray<float> that will store the inference results.

Returns

A status code indicating whether the inference operation completed successfully or encountered an error.

std::vector<NDArray<float>> infer(const std::vector<NDArray<float>> &input, StatusCode &sc)

This overload differs from the above function in that it directly returns the inference results instead of modifying an output parameter.

Parameters

  • input [in] — A vector of NDArray<float>. Each NDArray must be in NHWC or HWC format.
  • sc [out] — A reference to a status code that will be updated to indicate whether the inference operation was successful or encountered an error.

Returns

A vector of NDArray<float> containing the inference results.

StatusCode infer(const std::vector<float *> &input, std::vector<std::vector<float>> &output)

This overload is provided for convenience but may result in additional data copies within the qbruntime.

Parameters

  • input [in] — A vector of float pointers, where each pointer represents input data in HWC format.
  • output [out] — A reference to a vector of float vectors that will store the inference results.

Returns

A status code indicating whether the inference operation completed successfully or encountered an error.

std::vector<std::vector<float>> infer(const std::vector<float *> &input, StatusCode &sc)

This overload is provided for convenience but may result in additional data copies within the qbruntime.

Unlike the above overload, this function returns the inference results directly instead of modifying an output parameter.

Parameters

  • input [in] — A vector of float pointers, where each pointer represents input data in HWC format.
  • sc [out] — A reference to a status code that will be updated to indicate whether the inference operation was successful or encountered an error.

Returns

A vector of float vectors containing the inference results.

StatusCode infer(const std::vector<float *> &input, std::vector<std::vector<float>> &output, const std::vector<std::vector<int64_t>> &shape)

This overload is provided for convenience but may result in additional data copies within the qbruntime.

Unlike other overloads, this version allows explicitly specifying the shape of each input data, which can be in NHWC or HWC format.

Parameters

  • input [in] — A vector of float pointers, where each pointer represents input data in NHWC or HWC format.
  • output [out] — A reference to a vector of float vectors that will store the inference results.
  • shape [in] — A vector of vectors, where each inner vector specifies the shape of the corresponding input data.

Returns

A status code indicating whether the inference operation completed successfully or encountered an error.

std::vector<std::vector<float>> infer(const std::vector<float *> &input, const std::vector<std::vector<int64_t>> &shape, StatusCode &sc)

This overload is provided for convenience but may result in additional data copies within the qbruntime.

Unlike the above overload, this function returns the inference results directly instead of modifying an output parameter.

Parameters

  • input [in] — A vector of float pointers, where each pointer represents input data in NHWC or HWC format.
  • shape [in] — A vector of vectors, where each inner vector specifies the shape of the corresponding input data.
  • sc [out] — A reference to a status code that will be updated to indicate whether the inference operation was successful or encountered an error.

Returns

A vector of float vectors containing the inference results.

StatusCode infer(const std::vector<NDArray<float>> &input, std::vector<NDArray<float>> &output, uint32_t cache_size)

This overload supports inference with KV cache.

Note

This function is relevant for LLM models that use KV cache.

Parameters

  • input [in] — A vector of NDArrays, where each NDArray represents input data in NHWC or HWC format.
  • output [out] — A reference to a vector of NDArrays that will store the inference results.
  • cache_size [in] — The number of tokens accumulated in the KV cache so far.

Returns

A status code indicating whether the inference operation completed successfully or encountered an error.

std::vector<NDArray<float>> infer(const std::vector<NDArray<float>> &input, uint32_t cache_size, StatusCode &sc)

This overload supports inference with KV cache.

Unlike the above overload, this function returns the inference results directly instead of modifying an output parameter.

Note

This function is relevant for LLM models that use KV cache.

Parameters

  • input [in] — A vector of NDArrays, where each NDArray represents input data in NHWC or HWC format.
  • cache_size [in] — The number of tokens accumulated in the KV cache so far.
  • sc [out] — A reference to a status code that will be updated to indicate whether the inference operation was successful or encountered an error.

Returns

A vector of NDArrays containing the inference results.

StatusCode infer(const std::vector<float *> &input, std::vector<std::vector<float>> &output, const std::vector<std::vector<int64_t>> &shape, uint32_t cache_size)

This overload supports inference with KV cache.

Note

This function is relevant for LLM models that use KV cache.

Parameters

  • input [in] — A vector of float pointers, where each pointer represents input data in NHWC or HWC format.
  • output [out] — A reference to a vector of float vectors that will store the inference results.
  • shape [in] — A vector of vectors, where each inner vector specifies the shape of the corresponding input data.
  • cache_size [in] — The number of tokens accumulated in the KV cache so far.

Returns

A status code indicating whether the inference operation completed successfully or encountered an error.

std::vector<std::vector<float>> infer(const std::vector<float *> &input, const std::vector<std::vector<int64_t>> &shape, uint32_t cache_size, StatusCode &sc)

This overload supports inference with KV cache.

Unlike the above overload, this function returns the inference results directly instead of modifying an output parameter.

Note

This function is relevant for LLM models that use KV cache.

Parameters

  • input [in] — A vector of float pointers, where each pointer represents input data in NHWC or HWC format.
  • shape [in] — A vector of vectors, where each inner vector specifies the shape of the corresponding input data.
  • cache_size [in] — The number of tokens accumulated in the KV cache so far.
  • sc [out] — A reference to a status code that will be updated to indicate whether the inference operation was successful or encountered an error.

Returns

A vector of float vectors containing the inference results.

NCHW float-to-float inference

Performs inference with input and output elements of type float in NCHW (batch N, channels C, height H, width W) or CHW format.

Two input-output type pairs are supported:

  1. std::vector<NDArray<float>> for both input and output
    • Recommended approach, as NDArray allows the qbruntime to avoid unnecessary data copies internally.
  2. std::vector<float*> for input and std::vector<std::vector<float>> for output
    • Provided for user convenience, but results in unavoidable extra copies within the qbruntime.

Note

CHW is not the recommended format, as the NPU natively operates on HWC-ordered data. When input is provided in CHW format, it will be transposed internally, introducing additional overhead.

Note

If your data is in HWC format, use Model::infer instead of Model::inferCHW, as it avoids unnecessary format conversion.

Member Description
inferCHW ×10 Performs inference.

inferCHW

StatusCode inferCHW(const std::vector<NDArray<float>> &input, std::vector<NDArray<float>> &output)

Performs inference.

Parameters

  • input [in] — A vector of NDArray<float>. Each NDArray must be in NCHW or CHW format.
  • output [out] — A reference to a vector of NDArray<float> that will store the inference results.

Returns

A status code indicating whether the inference operation completed successfully or encountered an error.

std::vector<NDArray<float>> inferCHW(const std::vector<NDArray<float>> &input, StatusCode &sc)

This overload differs from the above function in that it directly returns the inference results instead of modifying an output parameter.

Parameters

  • input [in] — A vector of NDArray<float>. Each NDArray must be in NCHW or CHW format.
  • sc [out] — A reference to a status code that will be updated to indicate whether the inference operation was successful or encountered an error.

Returns

A vector of NDArray<float> containing the inference results.

StatusCode inferCHW(const std::vector<float *> &input, std::vector<std::vector<float>> &output)

This overload is provided for convenience but may result in additional data copies within the qbruntime.

Parameters

  • input [in] — A vector of float pointers, where each pointer represents input data in CHW format.
  • output [out] — A reference to a vector of float vectors that will store the inference results.

Returns

A status code indicating whether the inference operation completed successfully or encountered an error.

std::vector<std::vector<float>> inferCHW(const std::vector<float *> &input, StatusCode &sc)

This overload is provided for convenience but may result in additional data copies within the qbruntime.

Unlike the above overload, this function returns the inference results directly instead of modifying an output parameter.

Parameters

  • input [in] — A vector of float pointers, where each pointer represents input data in CHW format.
  • sc [out] — A reference to a status code that will be updated to indicate whether the inference operation was successful or encountered an error.

Returns

A vector of float vectors containing the inference results.

StatusCode inferCHW(const std::vector<float *> &input, std::vector<std::vector<float>> &output, const std::vector<std::vector<int64_t>> &shape)

This overload is provided for convenience but may result in additional data copies within the qbruntime.

Unlike other overloads, this version allows explicitly specifying the shape of each input data, which can be in NCHW or CHW format.

Parameters

  • input [in] — A vector of float pointers, where each pointer represents input data in NCHW or CHW format.
  • output [out] — A reference to a vector of float vectors that will store the inference results.
  • shape [in] — A vector of vectors, where each inner vector specifies the shape of the corresponding input data.

Returns

A status code indicating whether the inference operation completed successfully or encountered an error.

std::vector<std::vector<float>> inferCHW(const std::vector<float *> &input, const std::vector<std::vector<int64_t>> &shape, StatusCode &sc)

This overload is provided for convenience but may result in additional data copies within the qbruntime.

Unlike the above overload, this function returns the inference results directly instead of modifying an output parameter.

Parameters

  • input [in] — A vector of float pointers, where each pointer represents input data in NCHW or CHW format.
  • shape [in] — A vector of vectors, where each inner vector specifies the shape of the corresponding input data.
  • sc [out] — A reference to a status code that will be updated to indicate whether the inference operation was successful or encountered an error.

Returns

A vector of float vectors containing the inference results.

StatusCode inferCHW(const std::vector<NDArray<float>> &input, std::vector<NDArray<float>> &output, uint32_t cache_size)

This overload supports inference with KV cache.

Note

This function is relevant for LLM models that use KV cache.

Parameters

  • input [in] — A vector of NDArrays, where each NDArray represents input data in NCHW or CHW format.
  • output [out] — A reference to a vector of NDArrays that will store the inference results.
  • cache_size [in] — The number of tokens accumulated in the KV cache so far.

Returns

A status code indicating whether the inference operation completed successfully or encountered an error.

std::vector<NDArray<float>> inferCHW(const std::vector<NDArray<float>> &input, uint32_t cache_size, StatusCode &sc)

This overload supports inference with KV cache.

Unlike the above overload, this function returns the inference results directly instead of modifying an output parameter.

Note

This function is relevant for LLM models that use KV cache.

Parameters

  • input [in] — A vector of NDArrays, where each NDArray represents input data in NCHW or CHW format.
  • cache_size [in] — The number of tokens accumulated in the KV cache so far.
  • sc [out] — A reference to a status code that will be updated to indicate whether the inference operation was successful or encountered an error.

Returns

A vector of NDArrays containing the inference results.

StatusCode inferCHW(const std::vector<float *> &input, std::vector<std::vector<float>> &output, const std::vector<std::vector<int64_t>> &shape, uint32_t cache_size)

This overload supports inference with KV cache.

Note

This function is relevant for LLM models that use KV cache.

Parameters

  • input [in] — A vector of float pointers, where each pointer represents input data in NCHW or CHW format.
  • output [out] — A reference to a vector of float vectors that will store the inference results.
  • shape [in] — A vector of vectors, where each inner vector specifies the shape of the corresponding input data.
  • cache_size [in] — The number of tokens accumulated in the KV cache so far.

Returns

A status code indicating whether the inference operation completed successfully or encountered an error.

std::vector<std::vector<float>> inferCHW(const std::vector<float *> &input, const std::vector<std::vector<int64_t>> &shape, uint32_t cache_size, StatusCode &sc)

This overload supports inference with KV cache.

Unlike the above overload, this function returns the inference results directly instead of modifying an output parameter.

Note

This function is relevant for LLM models that use KV cache.

Parameters

  • input [in] — A vector of float pointers, where each pointer represents input data in NCHW or CHW format.
  • shape [in] — A vector of vectors, where each inner vector specifies the shape of the corresponding input data.
  • cache_size [in] — The number of tokens accumulated in the KV cache so far.
  • sc [out] — A reference to a status code that will be updated to indicate whether the inference operation was successful or encountered an error.

Returns

A vector of float vectors containing the inference results.

NHWC uint8_t-to-float inference

Performs inference with input and output elements of type uint8_t in NHWC (batch N, height H, width W, channels C) or HWC format.

Member Description
infer ×10

infer

StatusCode infer(const std::vector<NDArray<uint8_t>> &input, std::vector<NDArray<float>> &output)
std::vector<NDArray<float>> infer(const std::vector<NDArray<uint8_t>> &input, StatusCode &sc)
StatusCode infer(const std::vector<uint8_t *> &input, std::vector<std::vector<float>> &output)
std::vector<std::vector<float>> infer(const std::vector<uint8_t *> &input, StatusCode &sc)
StatusCode infer(const std::vector<uint8_t *> &input, std::vector<std::vector<float>> &output, const std::vector<std::vector<int64_t>> &shape)
std::vector<std::vector<float>> infer(const std::vector<uint8_t *> &input, const std::vector<std::vector<int64_t>> &shape, StatusCode &sc)
StatusCode infer(const std::vector<NDArray<uint8_t>> &input, std::vector<NDArray<float>> &output, uint32_t cache_size)
std::vector<NDArray<float>> infer(const std::vector<NDArray<uint8_t>> &input, uint32_t cache_size, StatusCode &sc)
StatusCode infer(const std::vector<uint8_t *> &input, std::vector<std::vector<float>> &output, const std::vector<std::vector<int64_t>> &shape, uint32_t cache_size)
std::vector<std::vector<float>> infer(const std::vector<uint8_t *> &input, const std::vector<std::vector<int64_t>> &shape, uint32_t cache_size, StatusCode &sc)

NCHW uint8_t-to-float inference

Performs inference with input and output elements of type uint8_t in NCHW (batch N, channels C, height H, width W) or CHW format.

Member Description
inferCHW ×10

inferCHW

StatusCode inferCHW(const std::vector<NDArray<uint8_t>> &input, std::vector<NDArray<float>> &output)
std::vector<NDArray<float>> inferCHW(const std::vector<NDArray<uint8_t>> &input, StatusCode &sc)
StatusCode inferCHW(const std::vector<uint8_t *> &input, std::vector<std::vector<float>> &output)
std::vector<std::vector<float>> inferCHW(const std::vector<uint8_t *> &input, StatusCode &sc)
StatusCode inferCHW(const std::vector<uint8_t *> &input, std::vector<std::vector<float>> &output, const std::vector<std::vector<int64_t>> &shape)
std::vector<std::vector<float>> inferCHW(const std::vector<uint8_t *> &input, const std::vector<std::vector<int64_t>> &shape, StatusCode &sc)
StatusCode inferCHW(const std::vector<NDArray<uint8_t>> &input, std::vector<NDArray<float>> &output, uint32_t cache_size)
std::vector<NDArray<float>> inferCHW(const std::vector<NDArray<uint8_t>> &input, uint32_t cache_size, StatusCode &sc)
StatusCode inferCHW(const std::vector<uint8_t *> &input, std::vector<std::vector<float>> &output, const std::vector<std::vector<int64_t>> &shape, uint32_t cache_size)
std::vector<std::vector<float>> inferCHW(const std::vector<uint8_t *> &input, const std::vector<std::vector<int64_t>> &shape, uint32_t cache_size, StatusCode &sc)

NHWC int8_t-to-int8_t inference

Performs inference with input and output elements of type int8_t in NHWC (batch N, height H, width W, channels C) or HWC format.

Using these inference APIs requires manual scaling (quantization) of float values to int8_t for input and int8_t to float for output.

Note

These APIs are intended for advanced use rather than typical usage.

Member Description
infer ×10

infer

StatusCode infer(const std::vector<NDArray<int8_t>> &input, std::vector<NDArray<int8_t>> &output)
std::vector<NDArray<int8_t>> infer(const std::vector<NDArray<int8_t>> &input, StatusCode &sc)
StatusCode infer(const std::vector<int8_t *> &input, std::vector<std::vector<int8_t>> &output)
std::vector<std::vector<int8_t>> infer(const std::vector<int8_t *> &input, StatusCode &sc)
StatusCode infer(const std::vector<int8_t *> &input, std::vector<std::vector<int8_t>> &output, const std::vector<std::vector<int64_t>> &shape)
std::vector<std::vector<int8_t>> infer(const std::vector<int8_t *> &input, const std::vector<std::vector<int64_t>> &shape, StatusCode &sc)
StatusCode infer(const std::vector<NDArray<int8_t>> &input, std::vector<NDArray<int8_t>> &output, uint32_t cache_size)
std::vector<NDArray<int8_t>> infer(const std::vector<NDArray<int8_t>> &input, uint32_t cache_size, StatusCode &sc)
StatusCode infer(const std::vector<int8_t *> &input, std::vector<std::vector<int8_t>> &output, const std::vector<std::vector<int64_t>> &shape, uint32_t cache_size)
std::vector<std::vector<int8_t>> infer(const std::vector<int8_t *> &input, const std::vector<std::vector<int64_t>> &shape, uint32_t cache_size, StatusCode &sc)

NCHW int8_t-to-int8_t inference

Performs inference with input and output elements of type int8_t in NCHW (batch N, channels C, height H, width W) or CHW format.

Using these inference APIs requires manual scaling (quantization) of float values to int8_t for input and int8_t to float for output.

Note

These APIs are intended for advanced use rather than typical usage.

Member Description
inferCHW ×10

inferCHW

StatusCode inferCHW(const std::vector<NDArray<int8_t>> &input, std::vector<NDArray<int8_t>> &output)
std::vector<NDArray<int8_t>> inferCHW(const std::vector<NDArray<int8_t>> &input, StatusCode &sc)
StatusCode inferCHW(const std::vector<int8_t *> &input, std::vector<std::vector<int8_t>> &output)
std::vector<std::vector<int8_t>> inferCHW(const std::vector<int8_t *> &input, StatusCode &sc)
StatusCode inferCHW(const std::vector<int8_t *> &input, std::vector<std::vector<int8_t>> &output, const std::vector<std::vector<int64_t>> &shape)
std::vector<std::vector<int8_t>> inferCHW(const std::vector<int8_t *> &input, const std::vector<std::vector<int64_t>> &shape, StatusCode &sc)
StatusCode inferCHW(const std::vector<NDArray<int8_t>> &input, std::vector<NDArray<int8_t>> &output, uint32_t cache_size)
std::vector<NDArray<int8_t>> inferCHW(const std::vector<NDArray<int8_t>> &input, uint32_t cache_size, StatusCode &sc)
StatusCode inferCHW(const std::vector<int8_t *> &input, std::vector<std::vector<int8_t>> &output, const std::vector<std::vector<int64_t>> &shape, uint32_t cache_size)
std::vector<std::vector<int8_t>> inferCHW(const std::vector<int8_t *> &input, const std::vector<std::vector<int64_t>> &shape, uint32_t cache_size, StatusCode &sc)

NHWC int8_t-to-float inference

Performs inference with input and output elements of type int8_t in NHWC (batch N, height H, width W, channels C) or HWC format.

Using these inference APIs requires manual scaling (quantization) of float values to int8_t for input.

Note

These APIs are intended for advanced use rather than typical usage.

Member Description
inferToFloat ×5

inferToFloat

std::vector<NDArray<float>> inferToFloat(const std::vector<NDArray<int8_t>> &input, StatusCode &sc)
std::vector<std::vector<float>> inferToFloat(const std::vector<int8_t *> &input, StatusCode &sc)
std::vector<std::vector<float>> inferToFloat(const std::vector<int8_t *> &input, const std::vector<std::vector<int64_t>> &shape, StatusCode &sc)
std::vector<NDArray<float>> inferToFloat(const std::vector<NDArray<int8_t>> &input, uint32_t cache_size, StatusCode &sc)
std::vector<std::vector<float>> inferToFloat(const std::vector<int8_t *> &input, const std::vector<std::vector<int64_t>> &shape, uint32_t cache_size, StatusCode &sc)

NCHW int8_t-to-float inference

Performs inference with input and output elements of type int8_t in NCHW (batch N, channels C, height H, width W) or CHW format.

Using these inference APIs requires manual scaling (quantization) of float values to int8_t for input.

Note

These APIs are intended for advanced use rather than typical usage.

Member Description
inferCHWToFloat ×5

inferCHWToFloat

std::vector<NDArray<float>> inferCHWToFloat(const std::vector<NDArray<int8_t>> &input, StatusCode &sc)
std::vector<std::vector<float>> inferCHWToFloat(const std::vector<int8_t *> &input, StatusCode &sc)
std::vector<std::vector<float>> inferCHWToFloat(const std::vector<int8_t *> &input, const std::vector<std::vector<int64_t>> &shape, StatusCode &sc)
std::vector<NDArray<float>> inferCHWToFloat(const std::vector<NDArray<int8_t>> &input, uint32_t cache_size, StatusCode &sc)
std::vector<std::vector<float>> inferCHWToFloat(const std::vector<int8_t *> &input, const std::vector<std::vector<int64_t>> &shape, uint32_t cache_size, StatusCode &sc)

NHWC Buffer-to-Buffer inference

Performs inference using input and output elements in the NPU’s internal data type. The inference operates on buffers allocated via the following APIs:

  • Model::acquireInputBuffer
  • Model::acquireOutputBuffer
  • Model::acquireInputBuffers
  • Model::acquireOutputBuffers
  • ModelVariantHandle::acquireInputBuffer
  • ModelVariantHandle::acquireOutputBuffer
  • ModelVariantHandle::acquireInputBuffers
  • ModelVariantHandle::acquireOutputBuffers

Additionally, Model::repositionInputs, Model::repositionOutputs, ModelVariantHandle::repositionInputs and ModelVariantHandle::repositionOutputs must be used properly.

Note

These APIs are intended for advanced use rather than typical usage.

Member Description
inferBuffer ×2

inferBuffer

StatusCode inferBuffer(const std::vector<Buffer> &input, std::vector<Buffer> &output, const std::vector<std::vector<int64_t>> &shape={}, uint32_t cache_size=0)
StatusCode inferBuffer(const std::vector<std::vector<Buffer>> &input, std::vector<std::vector<Buffer>> &output, const std::vector<std::vector<int64_t>> &shape={}, uint32_t cache_size=0)

NHWC Buffer-to-float inference

Performs inference using input and output elements in the NPU’s internal data type. The inference operates on buffers allocated via the following APIs:

  • Model::acquireInputBuffer
  • Model::acquireInputBuffers
  • ModelVariantHandle::acquireInputBuffer
  • ModelVariantHandle::acquireInputBuffers

Additionally, Model::repositionInputs and ModelVariantHandle::repositionInputs must be used properly.

Note

These APIs are intended for advanced use rather than typical usage.

Member Description
inferBufferToFloat ×4

inferBufferToFloat

StatusCode inferBufferToFloat(const std::vector<Buffer> &input, std::vector<NDArray<float>> &output, const std::vector<std::vector<int64_t>> &shape={}, uint32_t cache_size=0)
StatusCode inferBufferToFloat(const std::vector<std::vector<Buffer>> &input, std::vector<NDArray<float>> &output, const std::vector<std::vector<int64_t>> &shape={}, uint32_t cache_size=0)
StatusCode inferBufferToFloat(const std::vector<Buffer> &input, std::vector<std::vector<float>> &output, const std::vector<std::vector<int64_t>> &shape={}, uint32_t cache_size=0)
StatusCode inferBufferToFloat(const std::vector<std::vector<Buffer>> &input, std::vector<std::vector<float>> &output, const std::vector<std::vector<int64_t>> &shape={}, uint32_t cache_size=0)

Asynchronous Inference

Performs inference asynchronously.

To use asynchronous inference, the model must be created using a ModelConfig object with the async pipeline configured to be enabled. This is done by calling ModelConfig::setAsyncPipelineEnabled(true) before passing the configuration to Model::create.

Example:

using namespace mobilint;

ModelConfig mc;

// Enables support for `inferAsync` and `inferAsyncCHW`
mc.setAsyncPipelineEnabled(true);

StatusCode sc;
std::unique_ptr<Model> model = Model::create("resnet50.mxq", mc, sc);
if (!sc) {
   // Handle error appropriately
}

// Now `inferAsync` can be called.
Future future = model->inferAsync(input, sc);

Note

Functions in the inferAsync family (inferAsync, inferAsyncCHW, inferAsyncToFloat, inferAsyncCHWToFloat) typically return immediately. However, they may block if the input queue in the qbruntime is full.

Note

For all functions in the inferAsync family (inferAsync, inferAsyncCHW, inferAsyncToFloat, inferAsyncCHWToFloat), the data provided through the input parameter must remain unmodified until the asynchronous inference has completed. Modifying this data during execution may result in invalid results.

Note

Currently, only CNN-based models are supported, as asynchronous execution is particularly effective for this type of workload.

Note

Limitations:

  • RNN/LSTM and LLM models are not supported yet.
  • Models requiring CPU offloading are not supported yet.
  • Currently, only single-batch inference is supported (i.e., N = 1).
  • Currently, Buffer inference is not supported. The following types are supported in the synchronous API for advanced use cases, but are not yet available for asynchronous inference:
Member Description
inferAsync ×3 Initiates asynchronous inference with input in NHWC (batch N, height H, width W, channels C) or HWC format.
inferAsyncCHW ×3 Initiates asynchronous inference with input in NCHW (batch N, channels C, height H, width W) or CHW format.
inferAsyncToFloat This overload supports int8_t-to-float asynchronous inference.
inferAsyncCHWToFloat This overload supports int8_t-to-float asynchronous inference.

inferAsync

Future<float> inferAsync(const std::vector<NDArray<float>> &input, StatusCode &sc)

Initiates asynchronous inference with input in NHWC (batch N, height H, width W, channels C) or HWC format.

Parameters

  • input [in] — A vector of NDArray<float>. Each NDArray must be in NHWC or HWC format.
  • sc [out] — A reference to a status code that will be updated to indicate whether the asynchronous inference request was successfully initiated or encountered an error.

Returns

A future that can be used to retrieve the inference result.

Future<int8_t> inferAsync(const std::vector<NDArray<int8_t>> &input, StatusCode &sc)

This overload supports int8_t-to-int8_t asynchronous inference.

Parameters

  • input [in] — A vector of NDArray<int8_t>. Each NDArray must be in NHWC or HWC format.
  • sc [out] — A reference to a status code that will be updated to indicate whether the asynchronous inference request was successfully initiated or encountered an error.

Returns

A future that can be used to retrieve the inference result.

Future<float> inferAsync(const std::vector<NDArray<uint8_t>> &input, StatusCode &sc)

This overload supports uint8_t-to-float asynchronous inference.

Parameters

  • input [in] — A vector of NDArray<uint8_t>. Each NDArray must be in NHWC or HWC format.
  • sc [out] — A reference to a status code that will be updated to indicate whether the asynchronous inference request was successfully initiated or encountered an error.

Returns

A future that can be used to retrieve the inference result.

inferAsyncCHW

Future<float> inferAsyncCHW(const std::vector<NDArray<float>> &input, StatusCode &sc)

Initiates asynchronous inference with input in NCHW (batch N, channels C, height H, width W) or CHW format.

Parameters

  • input [in] — A vector of NDArray<float>. Each NDArray must be in NCHW or CHW format.
  • sc [out] — A reference to a status code that will be updated to indicate whether the asynchronous inference request was successfully initiated or encountered an error.

Returns

A future that can be used to retrieve the inference result.

Future<int8_t> inferAsyncCHW(const std::vector<NDArray<int8_t>> &input, StatusCode &sc)

This overload supports int8_t-to-int8_t asynchronous inference.

Parameters

  • input [in] — A vector of NDArray<int8_t>. Each NDArray must be in NCHW or CHW format.
  • sc [out] — A reference to a status code that will be updated to indicate whether the asynchronous inference request was successfully initiated or encountered an error.

Returns

A future that can be used to retrieve the inference result.

Future<float> inferAsyncCHW(const std::vector<NDArray<uint8_t>> &input, StatusCode &sc)

This overload supports uint8_t-to-float asynchronous inference.

Parameters

  • input [in] — A vector of NDArray<uint8_t>. Each NDArray must be in NCHW or CHW format.
  • sc [out] — A reference to a status code that will be updated to indicate whether the asynchronous inference request was successfully initiated or encountered an error.

Returns

A future that can be used to retrieve the inference result.

inferAsyncToFloat

Future<float> inferAsyncToFloat(const std::vector<NDArray<int8_t>> &input, StatusCode &sc)

This overload supports int8_t-to-float asynchronous inference.

Parameters

  • input [in] — A vector of NDArray<int8_t>. Each NDArray must be in NHWC or HWC format.
  • sc [out] — A reference to a status code that will be updated to indicate whether the asynchronous inference request was successfully initiated or encountered an error.

Returns

A future that can be used to retrieve the inference result.

inferAsyncCHWToFloat

Future<float> inferAsyncCHWToFloat(const std::vector<NDArray<int8_t>> &input, StatusCode &sc)

This overload supports int8_t-to-float asynchronous inference.

Parameters

  • input [in] — A vector of NDArray<int8_t>. Each NDArray must be in NCHW or CHW format.
  • sc [out] — A reference to a status code that will be updated to indicate whether the asynchronous inference request was successfully initiated or encountered an error.

Returns

A future that can be used to retrieve the inference result.

Buffer Management APIs

These APIs are required when calling Model::inferBuffer or Model::inferBufferToFloat.

Buffers are acquired using:

  • acquireInputBuffer
  • acquireOutputBuffer

Any acquired buffer must be released using:

  • releaseBuffer
  • releaseBuffers

Repositioning is handled by:

  • repositionInputs
  • repositionOutputs

Note

These APIs are intended for advanced use rather than typical usage.

Member Description
acquireInputBuffer
acquireOutputBuffer
acquireInputBuffers
acquireOutputBuffers
releaseBuffer
releaseBuffers
repositionInputs ×4
repositionOutputs ×4

acquireInputBuffer

std::vector<Buffer> acquireInputBuffer(const std::vector<std::vector<int>> &seqlens={}) const

acquireOutputBuffer

std::vector<Buffer> acquireOutputBuffer(const std::vector<std::vector<int>> &seqlens={}) const

acquireInputBuffers

std::vector<std::vector<Buffer>> acquireInputBuffers(const int batch_size, const std::vector<std::vector<int>> &seqlens={}) const

acquireOutputBuffers

std::vector<std::vector<Buffer>> acquireOutputBuffers(const int batch_size, const std::vector<std::vector<int>> &seqlens={}) const

releaseBuffer

StatusCode releaseBuffer(std::vector<Buffer> &buffer) const

releaseBuffers

StatusCode releaseBuffers(std::vector<std::vector<Buffer>> &buffers) const

repositionInputs

StatusCode repositionInputs(const std::vector<float *> &input, std::vector<Buffer> &input_buf, const std::vector<std::vector<int>> &seqlens={}) const
StatusCode repositionInputs(const std::vector<uint8_t *> &input, std::vector<Buffer> &input_buf, const std::vector<std::vector<int>> &seqlens={}) const
StatusCode repositionInputs(const std::vector<float *> &input, std::vector<std::vector<Buffer>> &input_buf, const std::vector<std::vector<int>> &seqlens={}) const
StatusCode repositionInputs(const std::vector<uint8_t *> &input, std::vector<std::vector<Buffer>> &input_buf, const std::vector<std::vector<int>> &seqlens={}) const

repositionOutputs

StatusCode repositionOutputs(const std::vector<Buffer> &output_buf, std::vector<float *> &output, const std::vector<std::vector<int>> &seqlens={}) const
StatusCode repositionOutputs(const std::vector<Buffer> &output_buf, std::vector<std::vector<float>> &output, const std::vector<std::vector<int>> &seqlens={}) const
StatusCode repositionOutputs(const std::vector<std::vector<Buffer>> &output_buf, std::vector<float *> &output, const std::vector<std::vector<int>> &seqlens={}) const
StatusCode repositionOutputs(const std::vector<std::vector<Buffer>> &output_buf, std::vector<std::vector<float>> &output, const std::vector<std::vector<int>> &seqlens={}) const

KV Cache Management

Note

These APIs are used for LLM models that utilize KV cache.

Member Description
dumpCacheMemory ×3 Dumps the KV cache memory into buffers.
loadCacheMemory ×2 Loads the KV cache memory from buffers.
filterCacheTail Filter the tail of the KV cache memory.
moveCacheTail Moves the tail of the KV cache memory to the end of the head.

dumpCacheMemory

StatusCode dumpCacheMemory(std::vector<std::vector<int8_t>> &bufs)

Dumps the KV cache memory into buffers.

Writes the current KV cache data into provided buffers.

Parameters

  • bufs [out] — A reference to vectors of byte vectors that will store the KV cache data.

Returns

A status code indicating whether the dump operation was successful or if an error occurred.

std::vector<std::vector<int8_t>> dumpCacheMemory(StatusCode &sc)

Dumps the KV cache memory into buffers.

Writes the KV cache data into buffers and returns them.

Parameters

  • sc [out] — A reference to a status code that will be updated to indicate whether the dump operation was successful or if an error occurred.

Returns

A vector of byte vectors containing the KV cache data.

StatusCode dumpCacheMemory(const std::string &cache_dir)

Dumps KV cache memory to files in the specified directory.

Writes the KV cache data to binary files within the given directory. Each file is named using the format: cache_<layer_hash>.bin.

Parameters

  • cache_dir [in] — Path to the directory where KV cache files will be saved.

Returns

A status code indicating whether the dump operation was successful or if an error occurred.

loadCacheMemory

StatusCode loadCacheMemory(const std::vector<std::vector<int8_t>> &bufs)

Loads the KV cache memory from buffers.

Restores the KV cache from the provided buffers.

Parameters

  • bufs [in] — A reference to a vector of byte vectors containing the KV cache data.

Returns

A status code indicating whether the load operation was successful or if an error occurred.

StatusCode loadCacheMemory(const std::string &cache_dir)

Loads the KV cache memory from files in the specified directory.

Reads KV cache data from files within the given directory and restores them. Each file is named using the format: cache_<layer_hash>.bin.

Parameters

  • cache_dir [in] — Path to the directory where KV cache files are saved.

Returns

A status code indicating whether the load operation was successful or if an error occurred.

filterCacheTail

int filterCacheTail(int cache_size, int tail_size, const std::vector<bool> &mask, StatusCode &sc)

Filter the tail of the KV cache memory.

Retains the desired caches in the tail of the KV cache memory, excludes the others, and shifts the remaining caches forward.

Parameters

  • cache_size [in] — The number of tokens accumulated in the KV cache so far.
  • tail_size [in] — The tail size of the KV cache to filter (<=32).
  • mask [in] — A mask indicating tokens to retain or exclude at the tail of the KV cache.
  • sc [out] — A status code indicating the outcome of the tail filtering.

Returns

New cache size after tail filtering.

moveCacheTail

int moveCacheTail(int num_head, int num_tail, int cache_size, StatusCode &sc)

Moves the tail of the KV cache memory to the end of the head.

Slice the tail of the KV cache memory up to the specified size and moves it to the designated cache position.

Parameters

  • num_head [in] — The size of the KV cache head where the tail is appended.
  • num_tail [in] — The size of the KV cache tail to be moved.
  • cache_size [in] — The total number of tokens accumulated in the KV cache so far.
  • sc [out] — A status code indicating the result of the tail move.

Returns

The updated cache size after moving the tail.

Deprecated APIs

Note

These APIs are deprecated and should not be used.

Member Description
infer ×2
getLatencyConsumed
getLatencyFinished

infer

StatusCode infer(const std::vector<float *> &input, std::vector<std::vector<float>> &output, int batch_size)

Deprecated

Use infer(input, output, shape) instead.

std::vector<std::vector<float>> infer(const std::vector<float *> &input, int batch_size, StatusCode &sc)

Deprecated

Use infer(input, shape, sc) instead.

getLatencyConsumed

uint64_t getLatencyConsumed(const int npu_op_idx) const

Deprecated

getLatencyFinished

uint64_t getLatencyFinished(const int npu_op_idx) const

Deprecated

Static functions

Member Description
create ×2 Creates a Model object from the specified MXQ model file.

create

static std::unique_ptr<Model> create(const std::string &mxq_path, StatusCode &sc)

Creates a Model object from the specified MXQ model file.

Parses the MXQ file and constructs a Model object. The model is initialized in single-core mode with all NPU local cores included.

Note

The created Model object must be launched before performing inference. See Model::launch for more details.

Parameters

  • mxq_path [in] — The path to the MXQ model file.
  • sc [out] — A reference to a status code that will be updated to indicate whether the model was successfully created or if an error occurred.

Returns

A unique pointer to the created Model object.

static std::unique_ptr<Model> create(const std::string &mxq_path, const ModelConfig &config, StatusCode &sc)

Creates a Model object from the specified MXQ model file and configuration.

Parses the MXQ file and constructs a Model object using the provided configuration, initializing the model with the given settings.

Note

The created Model object must be launched before performing inference. See Model::launch for more details.

Parameters

  • mxq_path [in] — The path to the MXQ model file.
  • config [in] — The configuration settings to initialize the Model.
  • sc [out] — A reference to a status code that will be updated to indicate whether the model was successfully created or if an error occurred.

Returns

A unique pointer to the created Model object.

Member functions

Member Description
Model ×2
operator= ×2
launch Launches the model on the specified Accelerator, which represents the actual NPU.
dispose Disposes of the model loaded onto the NPU.
getCoreMode Retrieves the core mode of the model.
isTarget Checks if the NPU core specified by CoreId is the target of the model. In other words, whether the model is configured to use the given NPU core.
getTargetCores Returns the NPU cores the model is configured to use.
inferSpeedrun Development-only API for measuring pure NPU inference speed.
getNumModelVariants Returns the total number of model variants available in this model.
getModelVariantHandle Retrieves a handle to the specified model variant.
getModelInputShape Returns the input shape of the model.
getModelOutputShape Returns the output shape of the model.
getInputBufferInfo Returns the input buffer information for the model.
getOutputBufferInfo Returns the output buffer information of the model.
getInputScale Returns the input quantization scale(s) of the model.
getOutputScale Returns the output quantization scale(s) of the model.
getIdentifier Returns the model's unique identifier.
getModelPath Returns the path to the MXQ model file associated with the Model.
getCacheInfos Returns informations of KV-cache of the model.

Model

Model(const Model &other)=delete
Model(Model &&other) noexcept

operator=

Model & operator=(const Model &rhs)=delete
Model & operator=(Model &&rhs) noexcept

launch

StatusCode launch(Accelerator &acc)

Launches the model on the specified Accelerator, which represents the actual NPU.

Parameters

  • acc [in] — The accelerator on which to launch the model.

Returns

A status code indicating whether the model was successfully launched or if an error occurred.

dispose

StatusCode dispose()

Disposes of the model loaded onto the NPU.

Releases any resources associated with the model on the NPU.

Returns

A status code indicating whether the disposal was successful or if an error occurred.

getCoreMode

CoreMode getCoreMode() const

Retrieves the core mode of the model.

Returns

The CoreMode of the model.

isTarget

bool isTarget(CoreId core_id) const

Checks if the NPU core specified by CoreId is the target of the model. In other words, whether the model is configured to use the given NPU core.

Parameters

  • core_id [in] — The CoreId to check.

Returns

True if the model is configured to use the specified CoreId, false otherwise.

getTargetCores

std::vector<CoreId> getTargetCores() const

Returns the NPU cores the model is configured to use.

Returns

A vector of CoreIds representing the target NPU cores.

inferSpeedrun

StatusCode inferSpeedrun(int variant_idx=0)

Development-only API for measuring pure NPU inference speed.

Runs NPU inference without uploading inputs and without retrieving outputs.

Parameters

  • variant_idx [in] — Index of model variant to run

Returns

A status code indicating the result.

getNumModelVariants

int getNumModelVariants() const

Returns the total number of model variants available in this model.

The variant_idx parameter passed to Model::getModelVariantHandle must be in the range [0, return value of this function).

Returns

The total number of model variants.

getModelVariantHandle

std::unique_ptr<ModelVariantHandle> getModelVariantHandle(int variant_idx, StatusCode &sc) const

Retrieves a handle to the specified model variant.

Use the returned ModelVariantHandle to query details such as input and output shapes for the selected variant.

Parameters

  • variant_idx [in] — Index of the model variant to retrieve. Must be in the range [0, getNumModelVariants()).
  • sc [out] — A reference to a StatusCode variable that will be updated to indicate success or failure.

Returns

A unique pointer to the corresponding ModelVariantHandle if successful; otherwise, nullptr.

getModelInputShape

const std::vector<std::vector<int64_t>> & getModelInputShape() const

Returns the input shape of the model.

Returns

A reference to the input shape of the model.

getModelOutputShape

const std::vector<std::vector<int64_t>> & getModelOutputShape() const

Returns the output shape of the model.

Returns

A reference to the output shape of the model.

getInputBufferInfo

const std::vector<BufferInfo> & getInputBufferInfo() const

Returns the input buffer information for the model.

Returns

A reference to a vector of input buffer information.

getOutputBufferInfo

const std::vector<BufferInfo> & getOutputBufferInfo() const

Returns the output buffer information of the model.

Returns

A reference to a vector of output buffer information.

getInputScale

std::vector<Scale> getInputScale() const

Returns the input quantization scale(s) of the model.

Returns

A vector of input scales.

getOutputScale

std::vector<Scale> getOutputScale() const

Returns the output quantization scale(s) of the model.

Returns

A vector of output scales.

getIdentifier

uint32_t getIdentifier() const

Returns the model's unique identifier.

This identifier distinguishes multiple models within a single user program. It is assigned incrementally, starting from 0 (e.g., 0, 1, 2, 3, ...).

Returns

The model identifier.

getModelPath

std::string getModelPath() const

Returns the path to the MXQ model file associated with the Model.

Returns

The MXQ file path.

getCacheInfos

std::vector<CacheInfo> getCacheInfos() const

Returns informations of KV-cache of the model.

Returns

A vector of CacheInfo objects.