Skip to content

AidGen C++ API

AidGen C++ API Documentation

AidGen 2.x C++ API Documentation

💡Note

Before developing with AidGen-SDK 2.x C++, please be aware of the following basics:

  • During compilation, include the header file located at /usr/local/include/aidlux/aidgen/aidgen.hpp
  • During linking, specify the library file located at /usr/local/lib/libaidgen.so
  • All interfaces are under the aplux::aidgen namespace
  • AidGen 2.x unifies LLM text generation, multimodal inference, and embedding vectorization capabilities under a consistent API style and programming model
  • Compared to version 1.x, 2.x integrates aidllm and aidmlm functionality into the unified aidgen namespace, providing a Context → Tokenizer / EmbeddingExtractor / Generator layered architecture

Log Level.enum LogLevel

AidGen-SDK provides an API for setting the log level. You need to specify which log level to use with this log-level enum.

Member NameTypeValueDescription
INFOuint8_t0Informational message
WARNINGuint8_t1Warning
ERRORuint8_t2Error
FATALuint8_t3Fatal error

Error Code.enum ErrorCode

AidGen-SDK 2.x interface methods uniformly return ErrorCode enum values. Developers can determine whether operations succeeded and locate issues based on the returned error codes.

Member NameTypeValueDescription
SUCCESSint32_t0Operation succeeded
INVALID_PARAMint32_t-1Invalid parameter
NOT_INITIALIZEDint32_t-2Not yet initialized
ALREADY_INITIALIZEDint32_t-3Already initialized
OUT_OF_MEMORYint32_t-4Out of memory
UNSUPPORTEDint32_t-5Unsupported operation
ABORTEDint32_t-6Operation aborted
FILE_NOT_FOUNDint32_t-10File does not exist or invalid path
FILE_OPEN_FAILEDint32_t-11File cannot be opened (permission denied or locked)
FILE_EMPTYint32_t-12File exists but has no content
FILE_READ_ERRORint32_t-13I/O truncation or system-level error during file read
FILE_PARSE_ERRORint32_t-14File content JSON parsing failed
BACKEND_ERRORint32_t-20Generic runtime error thrown by the backend
BACKEND_NOT_FOUNDint32_t-21Backend dynamic library not found
INTERNAL_ERRORint32_t-99Unknown exception or system-level crash fallback

Backend Type.enum BackendType

For AidGen-SDK 2.x, different inference backend frameworks are supported to implement LLM inference tasks. The available inference backends are listed below.

Member NameTypeValueDescription
DEFAULTuint8_t0Auto-select (priority: QNN248 > QNN240 > QNN236)
QNN236uint8_t1Qualcomm QNN SDK 2.36 inference backend
QNN240uint8_t2Qualcomm QNN SDK 2.40 inference backend
QNN248uint8_t3Qualcomm QNN SDK 2.48 inference backend

Generation State.enum GenState

During inference, the Generator continuously returns generation results to the developer via callback functions. The GenState enum identifies the stage of each callback event, allowing developers to determine generation progress.

Member NameTypeValueDescription
IDLEuint8_t0Idle, waiting for a new generation round
BEGINuint8_t1First token fragment of a sentence
CONTINUEuint8_t2Middle continuation fragment of a sentence
ENDuint8_t3End of sentence
ABORTuint8_t4User interruption or task voluntarily abandoned
ERRORuint8_t5System or hardware-level error

Global Functions

Get Library Version.get_library_version()

Gets the version string of the current AidGen library.

API get_library_version
Description Gets the version information of the AidGen library
Parameters void
Return Value A string containing the library version, e.g. "2.3.0"
cpp
std::string version = aplux::aidgen::get_library_version();
printf("Current aidgen library version: %s\n", version.c_str());

Get ABI Version.get_abi_version()

Gets the ABI version number for binary compatibility detection.

API get_abi_version
Description Gets the ABI version number of the AidGen library
Parameters void
Return Value ABI version number (unsigned 64-bit integer)
cpp
uint64_t abi_ver = aplux::aidgen::get_abi_version();
printf("ABI version: %lu\n", abi_ver);

Set Log Level.set_log_level()

Sets the minimum log output level for AidGen. Logs below this level will not be output.

API set_log_level
Description Sets the minimum log output level
Parameters log_level: LogLevel enum value specifying the minimum log level
Return Value void
cpp
aplux::aidgen::set_log_level(aplux::aidgen::LogLevel::ERROR);

Set Log File Prefix.set_log_file_prefix()

Sets the log file path prefix. The library automatically appends suffixes to generate the actual log file and returns the final full path.

API set_log_file_prefix
Description Sets the log file path prefix
Parameters log_file_prefix: Log file prefix string (must not be empty)
Return Value The final full log file path generated
cpp
std::string log_path = aplux::aidgen::set_log_file_prefix("./aidgen_log_");
printf("Log file path: %s\n", log_path.c_str());

Context Properties.struct ContextProperties

The ContextProperties struct is used to configure the behavior attributes of the Context, passed when creating a Context instance.

Member List

Member NameTypeDefault ValueDescription
streambooltrueWhether to enable streaming output
enable_profilerboolfalseWhether to enable the profiler
enable_prompt_cachebooltrueWhether to enable Prompt Cache acceleration

Tensor.struct Tensor

The Tensor struct holds a data buffer and its size. It serves as the output container for EmbeddingExtractor and the input container for Generator's multimodal mode. By default, the deleter is nullptr, meaning the unique_ptr automatically calls delete[] to free memory upon destruction. Users can also provide a custom deleter (e.g., std::free) to manage externally allocated memory.

Member List

Member NameTypeDefault ValueDescription
datastd::unique_ptr<uint8_t[], TensorDeleter>Data buffer pointer with support for custom deallocation functions
sizesize_t0Data size in bytes

Generation Event.struct GenEvent

The data structure returned by the Generator through callback functions during inference, containing the current generation state and text content.

Member List

Member NameTypeDefault ValueDescription
stateGenStateCurrent generation state code
textstd::stringText fragment from this callback

Meaning of text under each state:

stateMeaningtext Content
IDLENot yet startedEmpty
BEGINFirst tokenFirst generated text
CONTINUEIntermediate continuationContinued generated text
ENDGeneration completeFinal text
ABORTInterruptedPartially generated text before interruption
ERRORError occurredMay be empty

Generation Callback Type.GenCallback

Callback function type definition used during Generator inference. Developers need to implement this callback function type to process inference results.

cpp
using GenCallback = std::function<void(const GenEvent& event, void* user_data)>;

⚠️Warning

It is strictly prohibited to call Generator::abort() inside the callback function, as this may cause deadlocks or undefined behavior.

Extractor Profile Data.struct ExtractorProfileData

Performance metrics collected by EmbeddingExtractor during inference.

Member List

Member NameTypeDefault ValueDescription
init_time_usuint64_t0Initialization time (microseconds)
prompt_token_countuint64_t0Prompt token count
execute_time_usuint64_t0Inference execution time (microseconds)
prompt_processing_tpsfloat0.fPrompt processing throughput (tokens/s)

Generator Profile Data.struct GeneratorProfileData

Performance metrics collected by Generator during inference.

Member List

Member NameTypeDefault ValueDescription
init_time_usuint64_t0Initialization time (microseconds)
prompt_token_countuint64_t0Prompt token count
time_to_first_token_time_usuint64_t0Time to first token latency (microseconds)
prompt_processing_tpsfloat0.fPrompt processing throughput (tokens/s)
generated_token_countuint64_t0Generated token count
generate_time_usuint64_t0Generation phase duration (microseconds)
generate_tpsfloat0.fGeneration throughput (tokens/s)

Profile Data.struct ProfileData

Aggregates performance data from both the EmbeddingExtractor and Generator stages.

Member List

Member NameTypeDefault ValueDescription
extractorExtractorProfileDataEmbedding extractor performance data
generatorGeneratorProfileDataLLM generator performance data

Runtime Context Class.class Context

Context is the inference context class that holds model configuration and backend settings, shared by Tokenizer, EmbeddingExtractor, and Generator. Context uses a global singleton pattern — only one Context instance is permitted at a time. Its lifecycle is: create_instance()initialize() → use → automatic cleanup on destruction. Context is non-copyable and non-movable.

Create Instance Object.create_instance()

Creates the singleton Context instance. If an instance already exists, returns nullptr.

API create_instance
Description Creates a Context singleton instance; only one is allowed at a time
Parameters config: Model configuration file path (JSON), or the JSON content string itself (auto-detected by first non-whitespace character: `{` or `[` means JSON content, otherwise treated as a file path)
properties: ContextProperties struct reference, configuring streaming output, profiling, Prompt Cache, etc.
backend_type: BackendType enum value specifying the inference backend, default BackendType::DEFAULT (auto-selects the best available backend)
Return Value If nullptr, an instance already exists or construction failed; otherwise, a shared_ptr to the Context object
cpp
// Create context properties
aplux::aidgen::ContextProperties props;
props.stream = false;
props.enable_profiler = true;
props.enable_prompt_cache = true;

// Create a Context instance
std::shared_ptr<aplux::aidgen::Context> ctx_ptr = aplux::aidgen::Context::create_instance(
    "qwen2-7b/qwen2-7b.json", props, aplux::aidgen::BackendType::DEFAULT);
if(ctx_ptr == nullptr){
    printf("Context create_instance failed.\n");
    return EXIT_FAILURE;
}

Initialize Operation.initialize()

Performs the complete initialization flow: license validation → search and load backend plugin → parse model configuration JSON → verify backend version compatibility (major version + ABI + minimum minor/patch) → initialize backend handle.

API initialize
Description Performs complete initialization including license validation, backend loading, and config parsing
Parameters reserve: Reserved field, default value is nullptr
Return Value ErrorCode::SUCCESS indicates success. May return BACKEND_NOT_FOUND, FILE_OPEN_FAILED, FILE_EMPTY, FILE_READ_ERROR, FILE_PARSE_ERROR, NOT_INITIALIZED, etc.

💡Tip

If initialization reports Required QNN backend not found (error code BACKEND_NOT_FOUND), the corresponding QNN inference backend SDK is missing from the system. Install the appropriate backend package based on your model and target platform, for example:

  • aidgen-qnn236 — for Qualcomm QNN SDK 2.36
  • aidgen-qnn240 — for Qualcomm QNN SDK 2.40
  • aidgen-qnn248 — for Qualcomm QNN SDK 2.48

After installation, ensure the backend dynamic library (e.g., libaidgen_qnn240.so) is located under /usr/local/lib.

cpp
// Initialize Context; non-SUCCESS return indicates an error
if(ctx_ptr->initialize() != aplux::aidgen::ErrorCode::SUCCESS){
    printf("Context initialize failed.\n");
    return EXIT_FAILURE;
}

Get Config.get_config()

Queries all configurable parameters under the backend's "context" scope. Must be called after initialize().

API get_config
Description Queries all context-level configurable parameters from the backend
Parameters void
Return Value A string containing all context configuration parameters. Returns an empty string if not initialized
cpp
std::string config = ctx_ptr->get_config();
printf("Context config: %s\n", config.c_str());

Set Config.set_config()

Sets the value of a specified configuration item via the backend's "context" scope. Must be called after initialize(), and only effective before Tokenizer, Generator, or EmbeddingExtractor instances are created.

API set_config
Description Sets a specified context-level configuration item value
Parameters key: Configuration item key name
value: Configuration value
Return Value ErrorCode::SUCCESS indicates success. NOT_INITIALIZED if not initialized
cpp
if(ctx_ptr->set_config("key", "value") != aplux::aidgen::ErrorCode::SUCCESS){
    printf("Context set_config failed.\n");
}

Tokenizer Class.class Tokenizer

Tokenizer is a text tokenizer responsible for bidirectional conversion between text and token IDs. It uses a global singleton pattern — only one instance is allowed at a time. Its lifecycle is: create_instance()initialize()encode() / decode()finalize() → destruction. Tokenizer is non-copyable and non-movable.

Create Instance Object.create_instance()

API create_instance
Description Creates a Tokenizer singleton instance; only one is allowed at a time
Parameters context: std::shared_ptr<Context>, a shared initialized Context object (must not be nullptr)
reserve: Reserved field, default value is nullptr
Return Value If nullptr, the context is null, an instance already exists, or construction failed; otherwise, a unique_ptr to the Tokenizer object
cpp
std::unique_ptr<aplux::aidgen::Tokenizer> tokenizer_ptr = aplux::aidgen::Tokenizer::create_instance(ctx_ptr);
if(tokenizer_ptr == nullptr){
    printf("Tokenizer create_instance failed.\n");
    return EXIT_FAILURE;
}

Initialize Operation.initialize()

Initializes the tokenizer backend, loading the tokenizer model with the "NoEmbedding" target (vocabulary only, without embedding weights).

API initialize
Description Initializes the tokenizer backend and loads the vocabulary model
Parameters reserve: Reserved field, default value is nullptr
Return Value ErrorCode::SUCCESS indicates success
cpp
if(tokenizer_ptr->initialize() != aplux::aidgen::ErrorCode::SUCCESS){
    printf("Tokenizer initialize failed.\n");
    return EXIT_FAILURE;
}

Encode Text to Token.encode()

Encodes a plain text string into a token ID sequence. Must call initialize() first.

API encode
Description Encodes a text string into a token ID sequence
Parameters text: Input text string to be encoded
token_ids: std::vector<int32_t>&, output token ID sequence
Return Value ErrorCode::SUCCESS indicates success. NOT_INITIALIZED if not initialized
cpp
std::vector<int32_t> token_ids;
if(tokenizer_ptr->encode("Hello world!", token_ids) != aplux::aidgen::ErrorCode::SUCCESS){
    printf("Tokenizer encode failed.\n");
    return EXIT_FAILURE;
}
printf("Token count: %zu\n", token_ids.size());

Decode Token to Text.decode()

Decodes a token ID sequence back into human-readable text. Must call initialize() first.

API decode
Description Decodes a token ID sequence into a text string
Parameters token_ids: Input token ID sequence
text: std::string&, output decoded text
Return Value ErrorCode::SUCCESS indicates success. NOT_INITIALIZED if not initialized
cpp
std::string decoded_text;
if(tokenizer_ptr->decode(token_ids, decoded_text) != aplux::aidgen::ErrorCode::SUCCESS){
    printf("Tokenizer decode failed.\n");
    return EXIT_FAILURE;
}
printf("Decoded: %s\n", decoded_text.c_str());

Finalize Release Operation.finalize()

Releases tokenizer backend resources. Must call initialize() first.

API finalize
Description Releases tokenizer backend resources
Parameters reserve: Reserved field, default value is nullptr
Return Value ErrorCode::SUCCESS indicates success. NOT_INITIALIZED if not initialized
cpp
if(tokenizer_ptr->finalize() != aplux::aidgen::ErrorCode::SUCCESS){
    printf("Tokenizer finalize failed.\n");
    return EXIT_FAILURE;
}

Embedding Extractor Class.class EmbeddingExtractor

EmbeddingExtractor is used to encode text into a single embedding tensor, suitable for Retrieval-Augmented Generation (RAG), text semantic similarity computation, feature extraction, and other scenarios. It uses a global singleton pattern — only one instance is allowed at a time. Its lifecycle is: create_instance()initialize()encode() / get_profiler()finalize() → destruction. EmbeddingExtractor is non-copyable and non-movable.

Create Instance Object.create_instance()

API create_instance
Description Creates an EmbeddingExtractor singleton instance; only one is allowed at a time
Parameters context: std::shared_ptr<Context>, a shared initialized Context object (must not be nullptr)
reserve: Reserved field, default value is nullptr
Return Value If nullptr, the context is null, an instance already exists, or construction failed; otherwise, a unique_ptr to the EmbeddingExtractor object
cpp
std::unique_ptr<aplux::aidgen::EmbeddingExtractor> extractor_ptr = 
    aplux::aidgen::EmbeddingExtractor::create_instance(ctx_ptr);
if(extractor_ptr == nullptr){
    printf("EmbeddingExtractor create_instance failed.\n");
    return EXIT_FAILURE;
}

Initialize Operation.initialize()

Initializes the embedding extractor backend and loads the embedding model. Must be called after Context::initialize().

API initialize
Description Initializes the extractor backend and loads the embedding model
Parameters reserve: Reserved field, default value is nullptr
Return Value ErrorCode::SUCCESS indicates success
cpp
if(extractor_ptr->initialize() != aplux::aidgen::ErrorCode::SUCCESS){
    printf("EmbeddingExtractor initialize failed.\n");
    return EXIT_FAILURE;
}

Finalize Release Operation.finalize()

Releases embedding extractor backend resources. Must call initialize() first.

API finalize
Description Releases embedding extractor backend resources
Parameters reserve: Reserved field, default value is nullptr
Return Value ErrorCode::SUCCESS indicates success. NOT_INITIALIZED if not initialized

Get Property.get_property()

Queries all configurable parameters under the backend's "extractor" scope. Must call initialize() first.

API get_property
Description Queries all configurable parameters under the extractor scope
Parameters void
Return Value Property value string. Returns an empty string if not initialized

Set Property.set_property()

Sets a specified property value via the backend's "extractor" scope. Must call initialize() first.

API set_property
Description Sets a specified property value under the extractor scope
Parameters key: Property key name
value: Property value
Return Value ErrorCode::SUCCESS indicates success

Encode to Embedding.encode()

Encodes a text string into an embedding tensor. Must call initialize() first.

API encode
Description Encodes text into an embedding tensor
Parameters prompt: Input text string to be encoded
embedding: Tensor&, output embedding tensor (data and size are populated by the backend)
Return Value ErrorCode::SUCCESS indicates success
cpp
aplux::aidgen::Tensor embedding;
if(extractor_ptr->encode("What is the most popular cookie in the world?", embedding) != 
    aplux::aidgen::ErrorCode::SUCCESS){
    printf("EmbeddingExtractor encode failed.\n");
    return EXIT_FAILURE;
}
printf("Embedding size: %zu bytes\n", embedding.size);

Get Profiler.get_profiler()

Retrieves performance profiling data from the last embedding extraction run, only filling the ProfileData::extractor field. Must call initialize() first.

API get_profiler
Description Retrieves performance data from the last embedding extraction
Parameters profile_data: ProfileData&, output performance data (only the extractor field is filled)
Return Value ErrorCode::SUCCESS indicates success
cpp
aplux::aidgen::ProfileData perf;
if(extractor_ptr->get_profiler(perf) == aplux::aidgen::ErrorCode::SUCCESS){
    printf("Extractor TPS: %.2f tok/s\n", perf.extractor.prompt_processing_tps);
}

Generator Class.class Generator

Generator is an LLM token generator that supports both text-only and multimodal (embedding input) inference modes. It uses a global singleton pattern — only one instance is allowed at a time. Consecutive calls to run() are treated as multi-turn conversations, with internal automatic KV Cache reuse for acceleration; call reset() to clear historical cache and start a fresh conversation. Its lifecycle is: create_instance()initialize()run() / reset() / abort() / get_profiler() / get_state()finalize() → destruction. Generator is non-copyable and non-movable.

Create Instance Object.create_instance()

API create_instance
Description Creates a Generator singleton instance; only one is allowed at a time
Parameters context: std::shared_ptr<Context>, a shared initialized Context object (must not be nullptr)
reserve: Reserved field, default value is nullptr
Return Value If nullptr, the context is null, an instance already exists, or construction failed; otherwise, a unique_ptr to the Generator object
cpp
std::unique_ptr<aplux::aidgen::Generator> generator_ptr = aplux::aidgen::Generator::create_instance(ctx_ptr);
if(generator_ptr == nullptr){
    printf("Generator create_instance failed.\n");
    return EXIT_FAILURE;
}

Initialize Operation.initialize()

Initializes the generator backend and loads the language model. Must be called after Context::initialize().

API initialize
Description Initializes the generator backend and loads the language model
Parameters reserve: Reserved field, default value is nullptr
Return Value ErrorCode::SUCCESS indicates success
cpp
if(generator_ptr->initialize() != aplux::aidgen::ErrorCode::SUCCESS){
    printf("Generator initialize failed.\n");
    return EXIT_FAILURE;
}

Finalize Release Operation.finalize()

Releases generator backend resources. Must call initialize() first.

API finalize
Description Releases generator backend resources
Parameters reserve: Reserved field, default value is nullptr
Return Value ErrorCode::SUCCESS indicates success. NOT_INITIALIZED if not initialized

Get Property.get_property()

Queries all configurable parameters under the backend's "generator" scope. Must call initialize() first.

API get_property
Description Queries all configurable parameters under the generator scope
Parameters void
Return Value Property value string. Returns an empty string if not initialized

Set Property.set_property()

Sets generation parameters via the backend's "generator" scope. Must call initialize() first. Common properties include: "stream" (value "0" or "1", controls streaming output), and sampling parameters "temp", "top-k", "top-p" (all values in string format).

API set_property
Description Sets a specified property value under the generator scope
Parameters key: Property key name (e.g. "stream", "temp", "top-k", "top-p")
value: Property value (string format)
Return Value ErrorCode::SUCCESS indicates success
cpp
// Set sampling parameters
generator_ptr->set_property("stream", "1");
generator_ptr->set_property("temp", "0.8");
generator_ptr->set_property("top-k", "20");
generator_ptr->set_property("top-p", "0.9");

Get Embedding Buffer.get_embedding_buff()

Retrieves the raw pointer and size of the model's token embedding weight lookup table. Must call initialize() first.

The returned buffer stores all token embedding vectors contiguously in row-major order. Each token's embedding occupies embedding_size * sizeof(float) bytes. The embedding for the i-th token is located at offset i * embedding_size * sizeof(float). The pointer remains valid for the lifetime of the Generator instance — callers must NOT free this buffer.

Typical use case: manually constructing combined text + vision embedding tensors for multimodal inference.

API get_embedding_buff
Description Retrieves the pointer and size of the model's token embedding weight buffer
Parameters out_ptr: const char*&, output parameter receiving the pointer to the embedding weight buffer; set to nullptr on failure
out_size: size_t&, output parameter receiving the total size (in bytes) of the weight buffer
Return Value ErrorCode::SUCCESS indicates success
cpp
const char* embedding_buf = nullptr;
size_t embedding_buf_size = 0;
if(generator_ptr->get_embedding_buff(embedding_buf, embedding_buf_size) != 
    aplux::aidgen::ErrorCode::SUCCESS){
    printf("get_embedding_buff failed.\n");
}

Text-Only Inference.run()

Starts text-only inference based on a full prompt string. Consecutive calls to run() are treated as multi-turn conversations, with internal automatic KV Cache reuse for acceleration. Call reset() to start a fresh conversation.

⚠️Warning

It is strictly prohibited to call abort() inside the callback function, as this may cause deadlocks or undefined behavior.

API run
Description Starts text-only inference based on a full prompt string (caller is responsible for chat template assembly)
Parameters prompt: Complete prompt string; the developer is responsible for assembling system/user/assistant role markers
cb: GenCallback type callback function, triggered token-by-token as fragments
user_data: Pointer to user-defined data, passed through to the callback function; default value is nullptr
Return Value ErrorCode::SUCCESS indicates success
cpp
// Define the callback function
auto dialog_callback = [](const aplux::aidgen::GenEvent& event, void* user_data){
    switch(event.state){
        case aplux::aidgen::GenState::BEGIN:
            printf("[BEGIN]%s", event.text.c_str());
            break;
        case aplux::aidgen::GenState::CONTINUE:
            printf("%s", event.text.c_str());
            fflush(stdout);
            break;
        case aplux::aidgen::GenState::END:
            printf("%s[END]\n", event.text.c_str());
            break;
        case aplux::aidgen::GenState::ABORT:
            printf("\n[ABORT]%s\n", event.text.c_str());
            break;
        case aplux::aidgen::GenState::ERROR:
            printf("\n[ERROR]%s\n", event.text.c_str());
            break;
        default: break;
    }
};

// Execute text-only inference
std::string prompt = "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n"
                     "<|im_start|>user\nHello<|im_end|>\n"
                     "<|im_start|>assistant\n";
if(generator_ptr->run(prompt, dialog_callback) != aplux::aidgen::ErrorCode::SUCCESS){
    printf("Generator run failed.\n");
    return EXIT_FAILURE;
}

Multimodal Inference.run()

Starts multimodal inference based on a pre-computed embedding tensor. The developer is responsible for generating and assembling text embeddings and image embeddings. Consecutive calls to run() also reuse KV Cache; call reset() to clear.

⚠️Warning

It is strictly prohibited to call abort() inside the callback function, as this may cause deadlocks or undefined behavior.

API run
Description Starts multimodal inference based on a pre-computed embedding tensor
Parameters embedding: Tensor&, the embedding tensor (caller is responsible for generation and assembly)
cb: GenCallback type callback function
user_data: Pointer to user-defined data, passed through to the callback function; default value is nullptr
Return Value ErrorCode::SUCCESS indicates success

Reset Conversation State.reset()

Resets the internal KV Cache and conversation state via the backend's "generator_reset" action. Must be called after initialize(). Call this before starting a new topic in multi-turn conversations; after calling, previous KV Cache acceleration becomes invalid.

API reset
Description Resets KV Cache and conversation state, preparing for a new conversation round
Parameters reserve: Reserved field, default value is nullptr
Return Value ErrorCode::SUCCESS indicates success
cpp
// Reset before starting a new topic
if(generator_ptr->reset() != aplux::aidgen::ErrorCode::SUCCESS){
    printf("Generator reset failed.\n");
    return EXIT_FAILURE;
}
// A fresh conversation can now begin
generator_ptr->run(new_prompt, dialog_callback);

Abort Inference.abort()

Actively terminates the current inference task via the backend's "generator_abort" action. Must be called after initialize(). Typically called from another thread or triggered via a timeout mechanism.

⚠️Warning

It is strictly prohibited to call abort() inside the GenCallback callback function.

API abort
Description Actively terminates the currently running inference task
Parameters reserve: Reserved field, default value is nullptr
Return Value ErrorCode::SUCCESS indicates success
cpp
// Terminate inference from another thread
if(generator_ptr->abort() != aplux::aidgen::ErrorCode::SUCCESS){
    printf("Generator abort failed.\n");
}

Get Profiler.get_profiler()

Retrieves performance profiling data from the last inference run, only filling the ProfileData::generator field. Must call initialize() first.

API get_profiler
Description Retrieves performance data from the last generation inference
Parameters profile_data: ProfileData&, output performance data (only the generator field is filled)
Return Value ErrorCode::SUCCESS indicates success
cpp
aplux::aidgen::ProfileData perf;
if(generator_ptr->get_profiler(perf) == aplux::aidgen::ErrorCode::SUCCESS){
    printf("Time to first token: %lu us\n", perf.generator.time_to_first_token_time_us);
    printf("Generate rate: %.2f tok/s\n", perf.generator.generate_tps);
    printf("Generated token count: %lu\n", perf.generator.generated_token_count);
}

Get Current State.get_state()

Queries the current state of the generator (IDLE / BEGIN / CONTINUE / END / ABORT / ERROR). Must be called after initialize().

API get_state
Description Queries the current running state of the generator
Parameters state: GenState&, output parameter receiving the current state value
Return Value Always returns ErrorCode::SUCCESS when initialized. NOT_INITIALIZED if not initialized
cpp
aplux::aidgen::GenState current_state;
if(generator_ptr->get_state(current_state) == aplux::aidgen::ErrorCode::SUCCESS){
    printf("Current state: %d\n", static_cast<int>(current_state));
}

Typical Usage Flow

Complete Text-Only Inference Flow

Context::create_instance(config, props, backend)
  → Context::initialize()
  → Generator::create_instance(ctx) → Generator::initialize()
  → Generator::set_property("stream", "1")
  → Generator::run(prompt, callback)
  → Generator::get_profiler(profile_data)
  → [Optional] Generator::reset() → Generator::run(new_prompt, callback)
  → Generator::finalize()

Complete example code:

cpp
#include "aidgen.hpp"
using namespace aplux::aidgen;

int main() {
    // 1. Create Context and initialize
    ContextProperties props;
    props.stream = false;
    props.enable_profiler = true;
    auto ctx = Context::create_instance("/path/to/model.json", props, BackendType::QNN240);
    if (!ctx || ctx->initialize() != ErrorCode::SUCCESS) return -1;

    // 2. Create and initialize Generator
    auto generator = Generator::create_instance(ctx);
    if (!generator || generator->initialize() != ErrorCode::SUCCESS) return -1;
    generator->set_property("stream", "1");

    // 3. Execute inference
    std::string prompt =
        "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n"
        "<|im_start|>user\nHello, introduce yourself<|im_end|>\n"
        "<|im_start|>assistant\n";
    generator->run(prompt, [](const GenEvent& event, void*) {
        printf("%s", event.text.c_str());
        fflush(stdout);
    });

    // 4. Get performance data and release
    ProfileData perf;
    generator->get_profiler(perf);
    generator->finalize();
    return 0;
}

Tokenizer Usage Flow

Context::create_instance() → Context::initialize()
  → Tokenizer::create_instance(ctx) → Tokenizer::initialize()
  → Tokenizer::encode(text, token_ids)   // Text → Token
  → Tokenizer::decode(token_ids, text)   // Token → Text
  → Tokenizer::finalize()
cpp
auto tokenizer = Tokenizer::create_instance(ctx);
tokenizer->initialize();

// Encoding
std::vector<int32_t> tokens;
tokenizer->encode("Hello world!", tokens);

// Decoding
std::string decoded;
tokenizer->decode(tokens, decoded);

tokenizer->finalize();

Embedding Extraction Flow

Context::create_instance() → Context::initialize()
  → EmbeddingExtractor::create_instance(ctx) → EmbeddingExtractor::initialize()
  → EmbeddingExtractor::encode(prompt, embedding)
  → EmbeddingExtractor::get_profiler(profile_data)
  → EmbeddingExtractor::finalize()
cpp
#include "aidgen.hpp"
using namespace aplux::aidgen;

int main() {
    ContextProperties props;
    props.enable_profiler = true;
    auto ctx = Context::create_instance("/path/to/bge-large-htp.json", props, BackendType::QNN240);
    if (!ctx || ctx->initialize() != ErrorCode::SUCCESS) return -1;

    auto extractor = EmbeddingExtractor::create_instance(ctx);
    if (!extractor || extractor->initialize() != ErrorCode::SUCCESS) return -1;

    Tensor embedding;
    extractor->encode("What is the most popular cookie in the world?", embedding);

    ProfileData perf;
    extractor->get_profiler(perf);
    printf("TPS: %.2f tok/s\n", perf.extractor.prompt_processing_tps);

    // Save embedding to file
    std::ofstream out("output.raw", std::ios::binary);
    out.write(reinterpret_cast<const char*>(embedding.data.get()), embedding.size);

    return 0;
}

Error Handling Pattern

All methods returning ErrorCode should have their return values checked:

cpp
if (ctx->initialize() != ErrorCode::SUCCESS) {
    fprintf(stderr, "Context initialization failed.\n");
    return EXIT_FAILURE;
}

Common initialization failure scenarios:

  • BACKEND_NOT_FOUND: QNN inference backend not found. Install the corresponding backend SDK (e.g., aidgen-qnn240). See the tip under Context::initialize() above for details.
  • FILE_OPEN_FAILED: Invalid model config file path or insufficient permissions
  • FILE_PARSE_ERROR: Model config file has malformed JSON
  • NOT_INITIALIZED: Version incompatibility or license validation failure

Memory Management Principles

ObjectManagementDescription
Contextshared_ptrShared ownership; should be destroyed after Generator / Tokenizer / EmbeddingExtractor
Generatorunique_ptrExclusive ownership; destructor automatically releases the singleton lock
Tokenizerunique_ptrExclusive ownership; destructor automatically releases the singleton lock
EmbeddingExtractorunique_ptrExclusive ownership; destructor automatically releases the singleton lock
Tensor::dataunique_ptr + custom deleterDefault delete[]; can pass std::free or other custom deleters

AidGen 1.x C++ API Documentation (Deprecated)

⚠️Warning

The following is the AidGen 1.x API documentation, covering the aplux::aidllm and aplux::aidmlm namespaces. This version is no longer maintained. New projects should use the AidGen 2.x API above.

AidLLM C++ API Documentation

💡Note

Before developing with AidGen-SDK C++, please be aware of the following basics:

  • During compilation, include the header file located at /usr/local/include/aidlux/aidgen/aidllm.hpp
  • During linking, specify the library file located at /usr/local/lib/libaidgen.so
  • All interfaces are under the aplux::aidllm namespace

Inference Backend Type.enum LLmBackendType

For AidllmSDK, different inference backend frameworks are supported to implement LLM inference tasks. The available inference backends are listed below.

Member NameTypeValueDescription
TYPE_DEFAULTuint8_t0Unknown backend type
TYPE_GENIEuint8_t1Genie inference backend

Inference Task State.enum LLMSentenceState

During an inference task, a single session may go through multiple stages. Developers can use these state codes to understand the current runtime status of the inference task.

Member NameTypeValueDescription
BEGINenum class0Session start segment
CONTINUEenum class1Intermediate content during ongoing session inference
ENDenum class2Session ending segment
COMPLETEenum class3Current session completed successfully
ABORTenum class4Current session terminated passively
ERRORenum class5Current session inference error

Interpreter Runtime State.enum LLMState

The overall state of the Aidllm interpreter during runtime. Developers can query this state to understand the interpreter's current working status.

Member NameTypeValueDescription
STANDIDLEenum class0Idle standby state
BUSYINGenum class1Busy processing inference
ABORTenum class2Inference has been terminated
ERRORenum class3Inference encountered an error

Log Level.enum LogLevel

AidllmSDK provides an API for logging (introduced later). You need to specify which log level is currently used, so this log-level enum is required.

Member NameTypeValueDescription
INFOuint8_t0Message
WARNINGuint8_t1Warning
ERRORuint8_t2Error
FATALuint8_t3Fatal error

Global Functions

Get Library Version.get_library_version()

Gets the version information string of the current Aidllm library.

API get_library_version
Description Gets the version information of the Aidllm library
Parameters void
Return Value A string containing the library version information
cpp
std::string version = aplux::aidllm::get_library_version();
printf("Current aidllm library version: %s\n", version.c_str());

Set Log Level.set_log_level()

Sets the minimum log output level for Aidllm. Logs below this level will not be output.

API set_log_level
Description Sets the minimum log output level
Parameters log_level: LogLevel enum value specifying the minimum log level
Return Value void
cpp
aplux::aidllm::set_log_level(aplux::aidllm::LogLevel::ERROR);

Set Log File Prefix.set_log_file_prefix()

Sets the log file name prefix for outputting logs to files with the specified prefix.

API set_log_file_prefix
Description Sets the log file name prefix
Parameters log_file_prefix: Log file name prefix string
Return Value void
cpp
aplux::aidllm::set_log_file_prefix("aidllm_log_");

Inference Callback Function Type.LLMCallback

The callback function type definition used during Aidllm inference. Developers need to implement a callback function of this type to handle inference results.

cpp
using LLMCallback = std::function<int32_t(LLMCallbackData& cb_data, void* user_data)>;

💡Note

The callback function return type is int32_t. Returning 0 indicates normal continuation of inference; a non-zero value can be used to control the inference flow.

Inference Callback Data Type.struct LLMCallbackData

During inference tasks, Aidllm uses developer-provided callback functions. This data type is the argument passed to that callback function, and developers can use it in custom callbacks to process inference results.

Member List

The LLMCallbackData struct contains the following members:

Member state
Type enum LLMSentenceState
Default Value
Description Status code of the current inference session
Member text
Type std::string
Default Value
Description Result text of the inference task / message corresponding to special status codes

Runtime Context Class.class LLMContext

During Aidllm runtime, some configuration information may need to be set, and runtime-related data also needs to be passed around. Objects of this runtime context type are used to complete data flow.

Create Instance Object.create_instance()

To set runtime context information, you first need a configuration instance object. This function is used to create an instance object of type LLMContext.

API create_instance
Description Used to construct an instance object of class LLMContext
Parameters config_file: Initial configuration file, where key information such as backend type and model file names can be configured
Return Value If it is nullptr, object construction failed; otherwise, it is a pointer to an LLMContext object
cpp
// Create a configuration instance object; report an error if the return value is null
std::unique_ptr<LLMContext> llm_context_ptr = LLMContext::create_instance("qwen2-7b/qwen2-7b.json");
if(llm_context_ptr == nullptr){
    printf("Test sample: LLMContext create_instance failed.\n");
    return EXIT_FAILURE;
}

Member List

The LLMContext object is used to manage runtime configuration information, including the following parameters:

Member config_file
Type std::string
Default Value
Description Initial configuration file, the config file parameter passed when creating the object
Member backend_type
Type LLmBackendType
Default Value LLmBackendType::TYPE_DEFAULT
Description The developer is required to specify the inference backend in the config file. After initialization parses the config file, this field will be overwritten to indicate the backend type specified by the developer
Member model_file_vec
Type std::vector<std::string>
Default Value
Description The developer is required to specify model files in the config file. After initialization parses the config file, this field will be overwritten to indicate the model files specified by the developer
Member config_overwrite_options
Type std::string
Default Value
Description By setting this field, you can specify certain key parameters in the inference process, thereby affecting inference speed, inference results, etc.
Member android_tmp_directory
Type std::string
Default Value
Description This field is only valid on the Android platform. By setting this field, you can specify a directory for which the system user has valid permissions, for temporary use by the inference program

Interpreter Class.class LLMInterpreter

An object instance of type LLMInterpreter is the main executor of inference operations and is used to carry out specific inference processes.

Create Instance Object.create_instance()

To perform inference-related operations, an inference interpreter is essential. This function is used to construct an instance object of the inference interpreter.

API create_instance
Description Uses various data managed by the LLMContext object to construct an object of type LLMInterpreter
Parameters llm_context: Reference to the unique_ptr of an LLMContext instance object (std::unique_ptr<LLMContext>&)
reserve: Reserved field, default value is nullptr
Return Value If it is nullptr, object construction failed; otherwise, it is a unique_ptr to an LLMInterpreter object
cpp
 // Use the LLMContext object pointer to create the interpreter object; report an error if the return value is null
std::unique_ptr<LLMInterpreter> llm_interpreter_ptr = LLMInterpreter::create_instance(llm_context_ptr);
if(llm_interpreter_ptr == nullptr){
    printf("Test sample: LLMInterpreter create_instance failed.\n");
    return EXIT_FAILURE;
}

Initialization Operation.initialize()

After the interpreter object is created, some initialization operations are required, such as environment checks and resource construction.

API initialize
Description Completes the initialization work required for inference
Parameters enable_profiler: Whether to enable the profiler, default value is false
reserve: Reserved field, default value is nullptr
Return Value A value of 0 indicates successful initialization; otherwise a non-zero value indicates failure
cpp
// Initialize the interpreter; report an error if the return value is non-zero
int init_result = llm_interpreter_ptr->initialize();
if(init_result != EXIT_SUCCESS){
    printf("Test sample: aidllm initialize failed.\n");
    return EXIT_FAILURE;
}

Sampling Parameter Setup Operation.set_sampler()

After initialization completes successfully, sampling parameters can be set with this function to control the randomness, diversity, and quality of generated content.

API set_sampler
Description Sets sampling parameters to control the randomness and diversity of LLM outputs.
Parameters key: Name of the sampling parameter. Currently supported:
  • "temp": Controls output randomness (Temperature); smaller values are more conservative.
  • "top-k": Limits the sampling range to the top K tokens with the highest probability.
  • "top-p": Nucleus Sampling; limits to the token pool whose cumulative probability reaches P.
value: Parameter value represented as a string:
  • For "temp": floating-point numeric string (e.g. "1.2").
  • For "top-k": integer numeric string (e.g. "20").
  • For "top-p": floating-point numeric string (e.g. "0.6").
Return Value A value of 0 indicates success; a non-zero value indicates failure (e.g. invalid key or unsupported value format).
cpp
// Set sampling parameters
llm_interpreter_ptr->set_sampler("temp", "0.8");
llm_interpreter_ptr->set_sampler("top-k", "20");
llm_interpreter_ptr->set_sampler("top-p", "0.9");

Session Inference Operation.run()

After successful initialization, you can run dialog inference with the LLM. Developers provide a custom callback function to handle continuous inference results during the session.

API run
Description Executes one session inference
Parameters prompt: Prompt string
cb: Callback function of type LLMCallback for handling continuous inference results during the session
user_data: Pointer to user data, convenient for using this data in custom callback functions, default value is nullptr
Return Value A value of 0 indicates the inference executed successfully; otherwise a non-zero value indicates failure
cpp
// Define callback function
LLMCallback dialog_callback = [&](LLMCallbackData& cb_data, void* user_data)->int32_t{
    if(cb_data.state == LLMSentenceState::BEGIN){
        printf("%s", cb_data.text.c_str());
    }else if(cb_data.state == LLMSentenceState::CONTINUE){
        printf("%s", cb_data.text.c_str());
        fflush(stdout);
    }else if(cb_data.state == LLMSentenceState::END){
        printf("%s\n", cb_data.text.c_str());
    }else if(cb_data.state == LLMSentenceState::COMPLETE){
        printf("\n[COMPLETE]%s\n", cb_data.text.c_str());
    }else if(cb_data.state == LLMSentenceState::ABORT){
        printf("\n[ABORT]%s\n", cb_data.text.c_str());
    }else if(cb_data.state == LLMSentenceState::ERROR){
        printf("\n[ERROR]%s\n", cb_data.text.c_str());
    }
    return EXIT_SUCCESS;
};

// Execute inference
std::string prompt = "<|im_start|>user\nHello<|im_end|>\n<|im_start|>assistant\n";
int run_result = llm_interpreter_ptr->run(prompt, dialog_callback);
if(run_result != EXIT_SUCCESS){
    printf("Test sample: aidllm run failed.\n");
    return EXIT_FAILURE;
}

Query Inference State.state()

During inference, developers may need to query the current runtime state of the interpreter, such as determining whether it is idle or actively inferring.

API state
Description Gets the current runtime state of the inference task
Parameters state: Reference to an LLMState variable; the function will overwrite this variable with the current state
Return Value A value of 0 indicates the query executed successfully; otherwise a non-zero value indicates failure
cpp
LLMState current_state = LLMState::STANDIDLE;
llm_interpreter_ptr->state(current_state);
printf("Current state: %d\n", (int)current_state);

Session Termination Operation.abort()

In some situations, users may want to interrupt the session that is currently running inference. This function is used to terminate inference.

⚠️Warning

It is strictly forbidden to call the abort function inside the callback function (LLMCallback), as this may cause deadlocks or undefined behavior.

API abort
Description Terminates the currently running inference session
Parameters reserve: Reserved field, default value is nullptr
Return Value A value of 0 indicates successful termination; otherwise a non-zero value indicates failure
cpp
// Terminate inference in another thread
int abort_result = llm_interpreter_ptr->abort();
if(abort_result != EXIT_SUCCESS){
    printf("Test sample: aidllm abort failed.\n");
    return EXIT_FAILURE;
}

Final Release Operation.finalize()

As mentioned above, the interpreter object needs to run initialize() for initialization. Correspondingly, the interpreter also needs to run release operations to destroy previously created resources.

API finalize
Description Completes necessary de-initialization and release operations
Parameters reserve: Reserved field, default value is nullptr
Return Value A value of 0 indicates the release operation executed successfully; otherwise a non-zero value indicates failure
cpp
// Execute interpreter de-initialization; report an error if the return value is non-zero
int fin_result = llm_interpreter_ptr->finalize();
if(fin_result != EXIT_SUCCESS){
    printf("Test : aidllm finalize failed.\n");
    return EXIT_FAILURE;
}

Get Profiler.get_profiler()

When the profiler is enabled during initialization (enable_profiler = true), this function can be used to obtain the profiler object pointer for performance data collection and analysis. For detailed usage, refer to the "Profiler C++ API Documentation" section below.

API get_profiler
Description Gets the pointer to the profiler object
Parameters void
Return Value If the profiler is enabled, returns a Profiler object pointer; if not enabled, returns nullptr
cpp
// Enable profiler during initialization
int init_result = llm_interpreter_ptr->initialize(true);

// Get the profiler
aplux::aidgen::Profiler* profiler = llm_interpreter_ptr->get_profiler();

AidMLM C++ API Documentation

💡Note

Before developing with AidMLM-SDK C++, please be aware of the following basics:

  • During compilation, include the header file located at /usr/local/include/aidlux/aidgen/aidmlm.hpp
  • During linking, specify the library file located at /usr/local/lib/libaidgen.so
  • All interfaces are under the aplux::aidmlm namespace
  • AidMLM is designed for multimodal large model (vision-language model) inference, currently supporting Qwen2-VL and Qwen2.5-VL series models

Inference State.enum AidLLMState

During an AidMLM inference task, a single session may go through various stages. Developers can use these state codes to understand the current runtime status of the inference task.

Member NameTypeValueDescription
STANDenum class0Not yet working
STARTenum class1Inference started
BUSYINGenum class2Inference in progress
FINISHenum class3Inference finished
COMPLETEenum class4Inference completed fully or truncated
WAITINGenum class5Current token decoding failed, waiting for next decode
ABORTenum class6Current inference terminated early by developer
ERRORenum class7Inference failed due to exception

Log Level.enum LogLevel

Member NameTypeValueDescription
INFOuint8_t0Message
WARNINGuint8_t1Warning
ERRORuint8_t2Error
FATALuint8_t3Fatal error

Model Type.enum ModelType

Specifies the type of multimodal model currently in use.

Member NameTypeValueDescription
RESERVEDenum class0Reserved type
QWEN2VLenum class1Qwen2-VL model
QWEN25VLenum class2Qwen2.5-VL model

Inference Callback Data Type.struct AidLLMCBData

During AidMLM inference tasks, developer-provided callback functions are used. This data type is the argument passed to that callback function.

Member List

Member state
Type enum AidLLMState
Default Value
Description Status code of the current inference session
Member text
Type std::string
Default Value
Description Result text of the inference task / message corresponding to special status codes

Inference Callback Function Type.AidLLMCB

The callback function type definition used during AidMLM inference.

cpp
using AidLLMCB = std::function<void(AidLLMCBData& cb_data, void* user_data)>;

Image Data Type.struct ImageData

A struct for passing image data to the multimodal model.

Member List

Member img_pos
Type int
Default Value -1
Description Position index of the image in the prompt. If -1, the image is appended at the end of the prompt
Member img_data
Type uint8_t*
Default Value nullptr
Description Image data pointer, pointing to RGB format image pixel data. Developers need to pre-resize to the model's required width and height

Initialization Parameter Type.struct AidmlmInitParam

Configuration parameters required for AidMLM initialization.

Member List

Member NameTypeDefault ValueDescription
vision_model_pathstd::stringVision encoder model file path
pos_emb_cos_pathstd::stringPosition encoding cosine weight file path
pos_emb_sin_pathstd::stringPosition encoding sine weight file path
embedding_weights_pathstd::stringWord embedding weights file path
window_attention_mask_pathstd::stringWindow attention mask file path (Qwen2.5-VL only)
full_attention_mask_pathstd::stringFull attention mask file path (Qwen2.5-VL only)
llm_model_path_vecstd::vector<std::string>LLM model file path list
dbg_optstd::stringDebug options string
typeModelTypeModelType::RESERVEDMultimodal model type
qwen2vl_cfgQwen2VLConfigQwen2-VL model configuration
qwen25vl_cfgQwen25VLConfigQwen2.5-VL model configuration
enable_profilerboolfalseWhether to enable the profiler
genie_log_levelint1Genie backend log level (1=ERROR, 2=WARN, 3=INFO, 4=VERBOSE)
use_shared_bufferboolfalseWhether to use shared buffer
use_mmapboolfalseWhether to use memory-mapped model loading
use_genie_load_model_exboolfalseWhether to use Genie extended model loading

Model Configuration Structs

AidMLM provides predefined model configuration structs to specify vision model configurations for different resolutions and parameter scales. Developers can choose the corresponding configuration based on the model in use.

Config Struct NameModelImage SizeEmbedding Dim
Qwen2VLConfigQwen2-VL644×6441536
Qwen25VLConfigQwen2.5-VL 3B392×3922048
Qwen25VL3B644ConfigQwen2.5-VL 3B644×6442048
Qwen25VL3B672ConfigQwen2.5-VL 3B672×6722048
Qwen25VL7B392ConfigQwen2.5-VL 7B392×3923584
Qwen25VL7B644ConfigQwen2.5-VL 7B644×6443584
Qwen25VL7B672ConfigQwen2.5-VL 7B672×6723584

Global Functions

Get Library Version.get_library_version()

API get_library_version
Description Gets the version information of the AidMLM library
Parameters void
Return Value A string containing the library version information
cpp
std::string version = aplux::aidmlm::get_library_version();
printf("Current aidmlm library version: %s\n", version.c_str());

Multimodal Inference Class.class Aidmlm

An object instance of type Aidmlm is the main executor of multimodal inference operations and is used to carry out vision-language model inference processes.

Construction and Destruction

Aidmlm objects are created via the default constructor.

cpp
aplux::aidmlm::Aidmlm mlm_ctx;

Set Log Level.set_log_level()

A static method that sets the minimum log output level for AidMLM.

API set_log_level
Description Sets the minimum log output level (static method)
Parameters log_level: LogLevel enum value
Return Value void
cpp
aplux::aidmlm::Aidmlm::set_log_level(aplux::aidmlm::LogLevel::INFO);

Set Log File Prefix.set_log_file_prefix()

A static method that sets the log file name prefix.

API set_log_file_prefix
Description Sets the log file name prefix (static method)
Parameters log_file: Log file name prefix string
Return Value void
cpp
aplux::aidmlm::Aidmlm::set_log_file_prefix("./test_mlm");

Initialization Operation.initialize()

Loads the multimodal model and initializes the inference environment.

API initialize
Description Loads the model and completes the initialization work required for inference
Parameters param: AidmlmInitParam struct reference containing model paths, configurations, and other initialization parameters
enable_profiler: Whether to enable the profiler, default value is false
Return Value A value of 0 indicates successful initialization; otherwise a non-zero value indicates failure
cpp
aplux::aidmlm::AidmlmInitParam init_param;
init_param.type = aplux::aidmlm::ModelType::QWEN25VL;
init_param.vision_model_path = "/path/to/veg.serialized.bin.aidem";
init_param.pos_emb_cos_path = "/path/to/position_ids_cos.raw";
init_param.pos_emb_sin_path = "/path/to/position_ids_sin.raw";
init_param.embedding_weights_path = "/path/to/embedding_weights.raw";
init_param.window_attention_mask_path = "/path/to/window_attention_mask.raw";
init_param.full_attention_mask_path = "/path/to/full_attention_mask.raw";
init_param.llm_model_path_vec.push_back("/path/to/llm_model.serialized.bin.aidem");
init_param.use_genie_load_model_ex = true;

aplux::aidmlm::Aidmlm mlm_ctx;
if(mlm_ctx.initialize(init_param) < 0){
    printf("AidMLM initialize failed.\n");
    return EXIT_FAILURE;
}

Sampling Parameter Setup Operation.set_sampler()

After initialization completes successfully, sampling parameters can be set with this function to control the randomness, diversity, and quality of generated content.

API set_sampler
Description Sets sampling parameters to control the randomness and diversity of LLM outputs.
Parameters key: Name of the sampling parameter. Currently supported:
  • "temp": Controls output randomness (Temperature); smaller values are more conservative.
  • "top-k": Limits the sampling range to the top K tokens with the highest probability.
  • "top-p": Nucleus Sampling; limits to the token pool whose cumulative probability reaches P.
value: Parameter value represented as a string:
  • For "temp": floating-point numeric string (e.g. "1.2").
  • For "top-k": integer numeric string (e.g. "20").
  • For "top-p": floating-point numeric string (e.g. "0.6").
Return Value A value of 0 indicates success; a non-zero value indicates failure (e.g. invalid key or unsupported value format).
cpp
mlm_ctx.set_sampler("top-k", "20");
mlm_ctx.set_sampler("temp", "0.8");

Session Inference Operation.run()

After successful initialization, you can send image-text combined prompts to the multimodal model for inference.

💡Note

This function is not thread-safe. Only one thread can call the run method at a time.

API run
Description Executes one multimodal session inference
Parameters prompt: User prompt string
sys_prompt: System prompt string
img_vec: Reference to an ImageData vector containing the image data to input
cb: Callback function of type AidLLMCB for handling inference results
starting_round: Whether this is the start of a new conversation round (true for new conversation start)
user_data: Pointer to user data, convenient for using this data in custom callback functions, default value is nullptr
Return Value A value of 0 indicates the inference executed successfully; otherwise a non-zero value indicates failure
cpp
// Define callback function
void my_callback(aplux::aidmlm::AidLLMCBData& cb_data, void* user_data){
    if(cb_data.state == aplux::aidmlm::AidLLMState::START){
        printf("[BOS]%s", cb_data.text.c_str());
    }else if(cb_data.state == aplux::aidmlm::AidLLMState::FINISH){
        printf("[EOS]%s\n", cb_data.text.c_str());
    }else if(cb_data.state == aplux::aidmlm::AidLLMState::ERROR){
        printf("[ERROR]%s\n", cb_data.text.c_str());
    }else{
        printf("%s", cb_data.text.c_str());
    }
}

// Prepare image data (pre-resize to model-required dimensions, RGB format)
cv::Mat img = cv::imread("test.jpg");
cv::Mat img_rgb;
cv::cvtColor(img, img_rgb, cv::COLOR_BGR2RGB);
cv::Mat img_resized;
cv::resize(img_rgb, img_resized, cv::Size(392, 392));

aplux::aidmlm::ImageData img_data = {
    .img_pos = -1,
    .img_data = (uint8_t*)img_resized.data,
};
std::vector<aplux::aidmlm::ImageData> img_vec;
img_vec.push_back(img_data);

// Execute inference
std::string sys_prompt = "You are a helpful assistant.";
std::string user_prompt = "Please describe the scene in this image";
int run_result = mlm_ctx.run(user_prompt, sys_prompt, img_vec, my_callback, true);
if(run_result < 0){
    printf("AidMLM run failed.\n");
    return EXIT_FAILURE;
}

Session Termination Operation.abort()

Used to interrupt the currently running inference session.

API abort
Description Terminates the currently running inference session
Parameters reserve: Reserved field, default value is nullptr
Return Value A value of 0 indicates successful termination; otherwise a non-zero value indicates failure

Reset Operation.reset()

In multi-round conversation scenarios, when you need to process the next image or restart a conversation, call reset to clear internal state.

API reset
Description Resets the internal state of the inference engine to prepare for the next inference
Parameters void
Return Value A value of 0 indicates successful reset; otherwise a non-zero value indicates failure
cpp
// Reset after processing one image, prepare for the next
if(mlm_ctx.reset() < 0){
    printf("AidMLM reset failed.\n");
    return EXIT_FAILURE;
}

Final Release Operation.finalize()

Releases model resources and completes de-initialization.

API finalize
Description Releases model resources and completes necessary de-initialization operations
Parameters void
Return Value A value of 0 indicates successful release; otherwise a non-zero value indicates failure
cpp
if(mlm_ctx.finalize() < 0){
    printf("AidMLM finalize failed.\n");
    return EXIT_FAILURE;
}

Get Profiler.get_profiler()

When the profiler is enabled during initialization, this function can be used to obtain the Profiler object pointer. For detailed usage, refer to the "Profiler C++ API Documentation" section below.

API get_profiler
Description Gets the pointer to the profiler object
Parameters void
Return Value If the profiler is enabled, returns a Profiler object pointer; if not enabled, returns nullptr
cpp
// Enable profiler
aplux::aidmlm::AidmlmInitParam init_param;
init_param.enable_profiler = true;
// ... other parameter setup ...
mlm_ctx.initialize(init_param, true);

// Get performance data after inference
aplux::aidgen::Profiler* profiler = mlm_ctx.get_profiler();
aplux::aidgen::ProfileData data = profiler->get_data();
printf("Init time: %lu us\n", data.init_time_us);
printf("Time to first token: %lu us\n", data.time_to_first_token_us);
printf("Generate rate: %.2f tok/s\n", data.generate_rate);
printf("ViT execute time: %lu us\n", data.vit_execute_time_us);

Profiler C++ API Documentation

💡Note

Profiler-related interfaces are under the aplux::aidgen namespace (independent of aplux::aidllm and aplux::aidmlm).

  • Header file path /usr/local/include/aidlux/aidgen/profiler.hpp
  • Both AidLLM and AidMLM use their respective get_profiler() methods to obtain a Profiler object for performance analysis

Performance Data Type.struct ProfileData

During inference, developers may want to monitor performance metrics at each stage. The ProfileData struct stores performance data collected during the inference process.

Member List

Member NameTypeDescription
init_time_usuint64_tInitialization time (microseconds)
prompt_token_numuint64_tNumber of input prompt tokens
prompt_processing_ratefloatPrompt processing rate (tok/s)
time_to_first_token_usuint64_tTime to first token (microseconds)
generated_token_numuint64_tNumber of generated tokens
generate_ratefloatToken generation rate (tok/s)
generate_time_usuint64_tTotal generation time (microseconds)
vit_execute_time_usuint64_tVision model execution time (microseconds), AidMLM only
vit_init_time_usuint64_tVision model initialization time (microseconds), AidMLM only
vit_preprocess_time_usuint64_tVision model preprocessing time (microseconds), AidMLM only
vit_postprocess_time_usuint64_tVision model postprocessing time (microseconds), AidMLM only

Profiler Class.class Profiler

The Profiler class manages performance data collection during inference. It must be enabled during initialization (enable_profiler = true) to be used.

Get Performance Data.get_data()

Gets the currently collected performance data.

API get_data
Description Gets the performance analysis data collected during inference
Parameters void
Return Value ProfileData struct containing performance metrics for each stage

Reset Performance Data.reset()

Resets the collected performance data, typically called before starting a new inference round.

API reset
Description Clears collected performance data and restores to initial state
Parameters void
Return Value void
cpp
// Enable profiler during initialization
int init_result = llm_interpreter_ptr->initialize(true);

// Get the profiler
aplux::aidgen::Profiler* profiler = llm_interpreter_ptr->get_profiler();

// Execute inference...
llm_interpreter_ptr->run(prompt, dialog_callback);

// Get performance data
aplux::aidgen::ProfileData data = profiler->get_data();
printf("Time to first token: %lu us\n", data.time_to_first_token_us);
printf("Generate rate: %.2f tok/s\n", data.generate_rate);
printf("Generated token count: %lu\n", data.generated_token_num);

// Reset data, prepare for next inference round
profiler->reset();