AI引擎模块架构
源码:https://gitee.com/openharmony/ai_engine
1. 模块概述
1.1 功能与目标
主要功能:
AI引擎模块是OpenHarmony系统提供原生分布式AI能力的核心子系统,实现了统一的AI引擎框架,支持算法能力的快速插件化集成。该模块的主要功能包括:
- 插件管理:提供AI算法插件的生命周期管理和按需部署
- 模块管理:管理AI算法模块的加载、卸载和版本控制
- 通信管理:提供客户端与服务端之间的通信机制
- 算法执行:支持同步和异步的AI算法推理执行
- 资源管理:管理AI模型、数据和计算资源
- 分布式调用:支持跨设备的AI能力调用
- 统一接口:提供适配不同推理框架层级的统一推理接口
- 算法插件:支持ASR(语音识别)、CV(计算机视觉)等算法插件
使用场景:
- 语音识别和唤醒词检测
- 图像分类和目标检测
- 自然语言处理
- 机器学习模型推理
- 分布式AI计算
- 智能设备控制
- 多媒体内容分析
1.2 系统位置
系统架构位置:
AI引擎模块位于OpenHarmony系统的foundation/ai
子系统下,是AI业务子系统的核心组件。
模块关系:
- 上层对接:为应用层提供统一的AI能力API
- 下层对接:对接系统能力管理器(SAMGR)、IPC通信等底层服务
- 横向协作:与分布式调度、设备管理、多媒体等模块协作
- 系统集成:集成到系统能力管理器中,提供系统级AI服务
核心模块地位:
该模块是AI子系统的核心模块,承担着统一AI能力接口和算法插件管理的重要职责,是实现分布式AI能力的关键基础设施。
1.3 设计思路与模式
设计思路:
- 插件化架构设计:采用插件化架构,支持算法能力的快速集成和扩展
- 分层架构设计:采用Client-Server分层架构,分离客户端和服务端职责
- 统一接口设计:通过统一的API接口屏蔽不同算法和推理框架的差异
- 异步处理机制:采用异步处理模式,提高系统响应性能
- 资源池管理:采用资源池模式管理计算资源和模型资源
- 分布式设计:支持跨设备的AI能力调用和资源共享
设计模式:
- 工厂模式:ClientFactory用于创建不同类型的客户端
- 单例模式:PluginManager等核心组件采用单例模式
- 策略模式:不同算法插件采用不同的处理策略
- 观察者模式:回调机制支持异步结果通知
- 适配器模式:通信适配器支持不同的通信协议
- 插件模式:算法插件通过IPlugin接口实现统一管理
1.4 系统框图
外部依赖 公共组件 算法插件 Server组件 Client组件 AI引擎模块 应用层 SAMGR IPC Hilog Utils Base Protocol Utils Platform DataChannel KWS Plugin IC Plugin ASR Plugin CV Plugin PluginManager EngineManager EngineWorker CommunicationAdapter KWSSdk IcSdk ClientFactory CommunicationAdapter Client SDK Client Executor Server Executor Plugin Manager 语音应用 图像应用 AI应用 系统服务
2. 模块结构
2.1 源文件与头文件
核心接口文件:
AI数据类型:
interfaces/kits/ai_datatype.h
- AI数据类型定义,包含Array模板类interfaces/kits/ai_retcode.h
- AI错误码定义interfaces/kits/ai_callback.h
- AI回调接口定义
ASR算法接口:
interfaces/kits/asr/keyword_spotting/kws_sdk.h
- 唤醒词识别SDK接口interfaces/kits/asr/keyword_spotting/kws_callback.h
- 唤醒词识别回调接口interfaces/kits/asr/keyword_spotting/kws_constants.h
- 唤醒词识别常量定义interfaces/kits/asr/keyword_spotting/kws_retcode.h
- 唤醒词识别错误码
CV算法接口:
interfaces/kits/cv/image_classification/ic_sdk.h
- 图像分类SDK接口interfaces/kits/cv/image_classification/ic_callback.h
- 图像分类回调接口interfaces/kits/cv/image_classification/ic_constants.h
- 图像分类常量定义interfaces/kits/cv/image_classification/ic_retcode.h
- 图像分类错误码interfaces/kits/cv/ai_image.h
- AI图像数据结构
客户端组件:
services/client/client_executor/include/i_aie_client.inl
- AI客户端接口实现services/client/client_executor/include/client_factory.h
- 客户端工厂类services/client/client_executor/include/async_handler.h
- 异步处理器services/client/communication_adapter/include/sa_client.h
- 系统能力客户端适配器
服务端组件:
services/server/plugin/i_plugin.h
- 插件接口定义services/server/plugin/i_plugin_callback.h
- 插件回调接口services/server/plugin_manager/include/plugin_manager.h
- 插件管理器services/server/server_executor/include/engine_manager.h
- 引擎管理器services/server/server_executor/include/engine_worker.h
- 引擎工作器
公共组件:
services/common/protocol/struct_definition/aie_info_define.h
- AI信息结构定义services/common/protocol/retcode_inner/aie_retcode_inner.h
- 内部错误码定义services/common/utils/log/aie_log.h
- AI日志工具services/common/platform/threadpool/include/thread_pool.h
- 线程池管理
2.2 类、结构体、函数与方法
核心类定义:
Array模板结构体
cpp
template<typename T>
struct Array {
/* Pointer to the start address of the array */
T *data;
/* Array size */
size_t size;
};
KWSSdk类
cpp
class KWSSdk {
public:
/**
* @brief Defines the constructor for the KWS SDK.
*/
KWSSdk();
/**
* @brief Defines the destructor for the KWS SDK.
*/
virtual ~KWSSdk();
/**
* @brief Creates a KWS SDK instance.
* @return Returns KWS_RETCODE_SUCCESS if the operation is successful;
* returns a non-zero error code defined by KWSRetCode otherwise.
*/
int32_t Create();
/**
* @brief Synchronously executes the KWS task.
* @param input Indicates the input array defined by Array for the KWS task. The element type is int16_t.
* @return Returns KWS_RETCODE_SUCCESS if the operation is successful;
* returns a non-zero error code defined by KWSRetCode otherwise.
*/
int32_t SyncExecute(const Array<int16_t> &input);
/**
* @brief Sets the callback for the KWS task.
* @param callback Indicates the callback defined by KWSCallback for implementing the post-processing logic.
* @return Returns KWS_RETCODE_SUCCESS if the operation is successful;
* returns a non-zero error code defined by KWSRetCode otherwise.
*/
int32_t SetCallback(const std::shared_ptr<KWSCallback> &callback);
/**
* @brief Destroys the KWS SDK instance to release the session engaged with the plugin.
* @return Returns KWS_RETCODE_SUCCESS if the operation is successful;
* returns a non-zero error code defined by KWSRetCode otherwise.
*/
int32_t Destroy();
private:
class KWSSdkImpl;
std::unique_ptr<KWSSdkImpl> kwsSdkImpl_;
};
IcSdk类
cpp
class IcSdk {
public:
/**
* @brief Defines the constructor for the development tool.
*/
IcSdk() = default;
/**
* @brief Defines the destructor for the development tool, release model, and related variables.
*/
~IcSdk();
/**
* @brief Establishes a connection from the development tool to the AI server.
* @return Returns IC_RETCODE_SUCCESS if the operation is successful;
* returns a non-zero error code defined by IcRetCode otherwise.
*/
int32_t Create();
/**
* @brief Imports images from the development tool to the image classification model for prediction.
* @param picInput Indicates the input BGR image.
* @return Returns IC_RETCODE_SUCCESS if the operation is successful;
* returns a non-zero error code defined by IcRetCode otherwise.
*/
int32_t SyncExecute(const IcInput &picInput);
/**
* @brief Defines the callback function.
* @param callback Indicates the specified callback.
* @return Returns IC_RETCODE_SUCCESS if the operation is successful;
* returns a non-zero error code defined by IcRetCode otherwise.
*/
int32_t SetCallback(std::shared_ptr<IcCallback> callback);
/**
* @brief Destroys the loaded model and related resources.
* @return Returns IC_RETCODE_SUCCESS if the operation is successful;
* returns a non-zero error code defined by IcRetCode otherwise.
*/
int32_t Destroy();
};
IPlugin接口
cpp
class IPlugin {
public:
virtual ~IPlugin() = default;
virtual const long long GetVersion() const = 0;
virtual const char *GetName() const = 0;
virtual const char *GetInferMode() const = 0;
/**
* Algorithmic inference interface for synchronous tasks.
* @param [in] request Request task which contains the specific information of the task.
* @param [out] response Results of encapsulated algorithmic inference.
* @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
*/
virtual int SyncProcess(IRequest *request, IResponse *&response) = 0;
/**
* Algorithmic inference interface for asynchronous tasks.
* @param [in] request Request task which contains the specific information of the task.
* @param [in] callback Callback which is used to return the result of asynchronous inference.
* @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
*/
virtual int AsyncProcess(IRequest *request, IPluginCallback *callback) = 0;
/**
* Initialize plugin.
* @param [in] transactionId Transaction ID.
* @param [in] inputInfo Data information needed to initialize plugin.
* @param [out] outputInfo The returned data information of initializing plugin.
* @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
*/
virtual int Prepare(long long transactionId, const DataInfo &inputInfo, DataInfo &outputInfo) = 0;
/**
* Unload model and plugin.
* @param [in] isFullUnload Whether to unload completely.
* @param [in] transactionId Transaction ID.
* @param [in] inputInfo Data information needed to unload model and plugin.
* @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
*/
virtual int Release(bool isFullUnload, long long transactionId, const DataInfo &inputInfo) = 0;
/**
* Set the configuration parameters of the plugin.
* @param [in] optionType The type of setting option.
* @param [in] inputInfo Configuration parameter needed to set up the plugin.
* @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
*/
virtual int SetOption(int optionType, const DataInfo &inputInfo) = 0;
/**
* Get the configuration parameters of plugin.
* @param [in] optionType The type of getting option.
* @param [in] inputInfo Parameter information for getting options.
* @param [out] outputInfo The configuration information of plugin.
* @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
*/
virtual int GetOption(int optionType, const DataInfo &inputInfo, DataInfo &outputInfo) = 0;
};
PluginManager类
cpp
class PluginManager : public IPluginManager {
FORBID_COPY_AND_ASSIGN(PluginManager);
FORBID_CREATE_BY_SELF(PluginManager);
public:
static PluginManager *GetInstance();
void Destroy() override;
int GetPlugin(const std::string &aid, long long version, std::shared_ptr<Plugin> &plugin) override;
void UnloadPlugin(const std::string &aid, long long version) override;
private:
std::shared_ptr<Plugin> FindPlugin(const PluginKey &pluginKey);
void AddPlugin(const PluginKey &pluginKey, const std::shared_ptr<Plugin> &pluginValue);
void DelPlugin(const PluginKey &pluginKey);
void DelPluginByAID(const std::string &aid);
int LoadPlugin(const std::string &aid, long long version, std::shared_ptr<Plugin> &plugin);
private:
static std::mutex instanceLock_;
static PluginManager *instance_;
private:
std::mutex mutex4Interface_;
std::mutex mutex_;
PluginMap pluginMap_;
};
重要结构体:
PluginKey结构体
cpp
struct PluginKey {
std::string aid;
long long version;
PluginKey(const std::string &aid, long long version)
: aid(aid), version(version)
{
}
inline bool operator< (const PluginKey& another) const
{
if (aid < another.aid) {
return true;
}
if (aid == another.aid && version < another.version) {
return true;
}
return false;
}
};
AiRetCode枚举
cpp
typedef enum {
AI_RETCODE_SUCCESS = 0, /** Return code for an operation success */
AI_RETCODE_FAILURE = -1, /** Return code for an operation failure */
AI_RETCODE_INIT_ERROR = 1001, /** Return code for an initialization error */
AI_RETCODE_NULL_PARAM = 1002, /** Return code for a null parameter error */
AI_RETCODE_DUPLICATE_INIT_ERROR = 1004, /** Return code for a duplicate initialization error */
AI_RETCODE_SERIALIZATION_ERROR = 2001, /** Return code for a serialization error */
AI_RETCODE_UNSERIALIZATION_ERROR = 2002, /** Return code for a deserialization error */
AI_RETCODE_PLUGIN_EXECUTION_ERROR = 3001, /** Return code for a plugin execution error */
AI_RETCODE_PLUGIN_SESSION_ERROR = 3002, /** Return code for a plugin session error */
} AiRetCode;
2.3 继承与多态
继承关系:
PluginManager
继承自IPluginManager
接口- 各种算法插件继承自
IPlugin
接口 - 各种回调类继承自相应的回调接口基类
- 各种SDK类通过组合模式使用实现类
多态设计:
- 通过
IPlugin
接口实现不同算法插件的统一管理 - 通过回调机制支持多种事件类型的统一处理
- 通过工厂模式实现不同客户端的动态创建
- 通过策略模式实现不同通信协议的处理
2.4 类图
uses manages uses uses creates creates manages executes Array<T> +T* data +size_t size KWSSdk -kwsSdkImpl_: unique_ptr<KWSSdkImpl> +KWSSdk() +~KWSSdk() +Create() : int32_t +SyncExecute() : int32_t +SetCallback() : int32_t +Destroy() : int32_t IcSdk +IcSdk() +~IcSdk() +Create() : int32_t +SyncExecute() : int32_t +SetCallback() : int32_t +Destroy() : int32_t <<interface>> IPlugin +GetVersion() : long long +GetName() +GetInferMode() +SyncProcess() : int +AsyncProcess() : int +Prepare() : int +Release() : int +SetOption() : int +GetOption() : int PluginManager -instance_: PluginManager* -instanceLock_: mutex -mutex4Interface_: mutex -mutex_: mutex -pluginMap_: PluginMap +GetInstance() +Destroy() : void +GetPlugin() : int +UnloadPlugin() : void -FindPlugin() : shared_ptr<Plugin> -AddPlugin() : void -DelPlugin() : void -LoadPlugin() : int <<interface>> IPluginManager +Destroy() : void +GetPlugin() : int +UnloadPlugin() : void PluginKey +aid: string +version: long long +PluginKey() +operator<() <<interface>> KWSCallback +OnResult() : void +OnError() : void <<interface>> IcCallback +OnResult() : void +OnError() : void <<interface>> IPluginCallback +OnResult() : void +OnError() : void ClientFactory +GetClient() +ClientInit() : int +ClientPrepare() : int +ClientSyncProcess() : int +ClientAsyncProcess() : int +ClientDestroy() : int EngineManager +CreateEngine() : int +DestroyEngine() : int +ExecuteTask() : int +RegisterPlugin() : int +UnregisterPlugin() : int EngineWorker +ProcessTask() : void +HandleRequest() : void +HandleResponse() : void KWSPlugin IcPlugin
2.5 模块内部依赖框图
回调接口 数据结构 公共组件 算法插件 Server组件 Client组件 AI引擎模块内部结构 KWSCallback IcCallback IPluginCallback IClientCb Array PluginKey DataInfo ConfigInfo Protocol Utils Platform DataChannel KWS Plugin IC Plugin ASR Plugin CV Plugin PluginManager EngineManager EngineWorker CommunicationAdapter KWSSdk IcSdk ClientFactory CommunicationAdapter Client SDK Client Executor Server Executor Plugin Manager
3. 模块间交互
3.1 交互描述
与系统模块的交互:
- 系统能力管理器:通过SAMGR模块注册和管理AI服务
- IPC通信:通过IPC模块实现客户端与服务端的通信
- 日志系统:通过Hilog模块记录AI引擎的运行日志
- 工具库:通过utils_base模块使用基础工具函数
- 分布式调度:通过分布式调度模块实现跨设备AI能力调用
外部库依赖:
- 系统框架:SAMGR、IPC等系统框架
- 工具库:utils_base、bounds_checking_function等基础库
- 第三方库:根据具体算法插件可能需要特定的AI推理框架
异步处理机制:
- 使用线程池进行异步任务处理
- 通过回调机制处理异步结果通知
- 支持同步和异步两种执行模式
- 采用事件驱动机制处理插件状态变化
3.2 事件驱动机制
事件类型:
- 插件加载/卸载事件
- 算法执行开始/完成事件
- 客户端连接/断开事件
- 资源分配/释放事件
- 错误和异常事件
事件处理流程:
- 注册事件监听器
- 接收系统或插件事件通知
- 解析事件参数
- 更新内部状态
- 通知相关回调
- 执行相应的处理逻辑
3.3 外部依赖框图
应用层 外部库 系统框架 系统服务 AI引擎模块 语音应用 图像应用 AI应用 系统服务 bounds_checking_function AI推理框架 数学库 图像处理库 分布式调度 设备管理 多媒体 权限管理 SAMGR IPC Hilog Utils Base Client SDK Client Executor Server Executor Plugin Manager
4. 状态机转换图
4.1 状态机模型
AI引擎模块的状态机包含以下主要状态:
服务状态:
SERVICE_NOT_START
- 服务未启动SERVICE_RUNNING
- 服务运行中SERVICE_STOPPING
- 服务停止中SERVICE_EXIT
- 服务已退出
客户端状态:
CLIENT_UNINITIALIZED
- 客户端未初始化CLIENT_INITIALIZING
- 客户端初始化中CLIENT_INITIALIZED
- 客户端已初始化CLIENT_PREPARING
- 客户端准备中CLIENT_READY
- 客户端就绪CLIENT_PROCESSING
- 客户端处理中CLIENT_ERROR
- 客户端错误
插件状态:
PLUGIN_UNLOADED
- 插件未加载PLUGIN_LOADING
- 插件加载中PLUGIN_LOADED
- 插件已加载PLUGIN_PREPARING
- 插件准备中PLUGIN_READY
- 插件就绪PLUGIN_PROCESSING
- 插件处理中PLUGIN_ERROR
- 插件错误
算法执行状态:
ALGORITHM_IDLE
- 算法空闲ALGORITHM_PREPARING
- 算法准备中ALGORITHM_RUNNING
- 算法运行中ALGORITHM_COMPLETED
- 算法完成ALGORITHM_ERROR
- 算法错误
4.2 状态切换规则
服务启动流程:
- 服务初始状态为
SERVICE_NOT_START
- 收到启动事件后,进入
SERVICE_RUNNING
状态 - 初始化各个子模块和插件管理器
- 注册到系统能力管理器
客户端状态转换:
- 客户端初始状态为
CLIENT_UNINITIALIZED
- 调用Create()时,进入
CLIENT_INITIALIZING
状态 - 初始化完成后,进入
CLIENT_INITIALIZED
状态 - 调用Prepare()时,进入
CLIENT_PREPARING
状态 - 准备完成后,进入
CLIENT_READY
状态 - 调用SyncExecute()时,进入
CLIENT_PROCESSING
状态 - 处理完成后,回到
CLIENT_READY
状态 - 发生错误时,进入
CLIENT_ERROR
状态
插件状态转换:
- 插件初始状态为
PLUGIN_UNLOADED
- 收到加载请求时,进入
PLUGIN_LOADING
状态 - 加载完成后,进入
PLUGIN_LOADED
状态 - 调用Prepare()时,进入
PLUGIN_PREPARING
状态 - 准备完成后,进入
PLUGIN_READY
状态 - 调用SyncProcess()时,进入
PLUGIN_PROCESSING
状态 - 处理完成后,回到
PLUGIN_READY
状态 - 发生错误时,进入
PLUGIN_ERROR
状态
算法执行状态转换:
- 算法初始状态为
ALGORITHM_IDLE
- 收到执行请求时,进入
ALGORITHM_PREPARING
状态 - 准备完成后,进入
ALGORITHM_RUNNING
状态 - 执行完成后,进入
ALGORITHM_COMPLETED
状态 - 发生错误时,进入
ALGORITHM_ERROR
状态 - 清理完成后,回到
ALGORITHM_IDLE
状态
事件触发条件:
- 服务启动/停止事件
- 客户端创建/销毁事件
- 插件加载/卸载事件
- 算法执行请求事件
- 错误和异常事件
4.3 状态机转换图
系统启动 OnStart() OnStop() 停止完成 重启服务 初始化客户端 Create()调用 初始化成功 初始化失败 Prepare()调用 准备成功 准备失败 SyncExecute()调用 处理完成 处理失败 错误恢复 初始化插件 加载请求 加载成功 加载失败 Prepare()调用 准备成功 准备失败 SyncProcess()调用 处理完成 处理失败 错误恢复 初始化算法 执行请求 准备完成 准备失败 执行完成 执行失败 清理完成 错误恢复 ServiceNotStart ServiceRunning ServiceStopping ServiceExit ClientUninitialized ClientInitializing ClientInitialized ClientError ClientPreparing ClientReady ClientProcessing PluginUnloaded PluginLoading PluginLoaded PluginError PluginPreparing PluginReady PluginProcessing AlgorithmIdle AlgorithmPreparing AlgorithmRunning AlgorithmError AlgorithmCompleted 服务运行状态,可以处理
各种AI算法请求 客户端就绪状态,可以
执行AI算法任务 插件就绪状态,可以
处理AI算法推理 算法运行状态,正在
执行AI推理计算
5. 接口设计
5.1 公共接口
AI客户端接口:
初始化客户端
cpp
int AieClientInit(const ConfigInfo &configInfo, ClientInfo &clientInfo,
const AlgorithmInfo &algorithmInfo, IServiceDeadCb *cb);
- 功能:初始化AI客户端
- 参数 :
configInfo
- 配置信息clientInfo
- 客户端信息(输出参数)algorithmInfo
- 算法信息cb
- 服务死亡回调
- 返回值:操作结果码
- 异常处理:初始化失败时返回错误码
准备客户端
cpp
int AieClientPrepare(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
const DataInfo &inputInfo, DataInfo &outputInfo, IClientCb *cb);
- 功能:准备AI客户端
- 参数 :
clientInfo
- 客户端信息algorithmInfo
- 算法信息inputInfo
- 输入数据信息outputInfo
- 输出数据信息(输出参数)cb
- 客户端回调
- 返回值:操作结果码
- 异常处理:准备失败时返回错误码
同步处理
cpp
int AieClientSyncProcess(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
const DataInfo &inputInfo, DataInfo &outputInfo);
- 功能:同步执行AI算法
- 参数 :
clientInfo
- 客户端信息algorithmInfo
- 算法信息inputInfo
- 输入数据信息outputInfo
- 输出数据信息(输出参数)
- 返回值:操作结果码
- 异常处理:处理失败时返回错误码
异步处理
cpp
int AieClientAsyncProcess(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
const DataInfo &inputInfo);
- 功能:异步执行AI算法
- 参数 :
clientInfo
- 客户端信息algorithmInfo
- 算法信息inputInfo
- 输入数据信息
- 返回值:操作结果码
- 异常处理:处理失败时返回错误码
销毁客户端
cpp
int AieClientDestroy(ClientInfo &clientInfo);
- 功能:销毁AI客户端
- 参数 :
clientInfo
- 客户端信息 - 返回值:操作结果码
- 异常处理:销毁失败时返回错误码
唤醒词识别SDK接口:
创建KWS SDK
cpp
int32_t KWSSdk::Create();
- 功能:创建唤醒词识别SDK实例
- 返回值:操作结果码
- 异常处理:创建失败时返回错误码
同步执行
cpp
int32_t KWSSdk::SyncExecute(const Array<int16_t> &input);
- 功能:同步执行唤醒词识别
- 参数 :
input
- 输入音频数据 - 返回值:操作结果码
- 异常处理:执行失败时返回错误码
设置回调
cpp
int32_t KWSSdk::SetCallback(const std::shared_ptr<KWSCallback> &callback);
- 功能:设置唤醒词识别回调
- 参数 :
callback
- 回调函数 - 返回值:操作结果码
- 异常处理:设置失败时返回错误码
销毁SDK
cpp
int32_t KWSSdk::Destroy();
- 功能:销毁唤醒词识别SDK实例
- 返回值:操作结果码
- 异常处理:销毁失败时返回错误码
图像分类SDK接口:
创建IC SDK
cpp
int32_t IcSdk::Create();
- 功能:创建图像分类SDK实例
- 返回值:操作结果码
- 异常处理:创建失败时返回错误码
同步执行
cpp
int32_t IcSdk::SyncExecute(const IcInput &picInput);
- 功能:同步执行图像分类
- 参数 :
picInput
- 输入图像数据 - 返回值:操作结果码
- 异常处理:执行失败时返回错误码
设置回调
cpp
int32_t IcSdk::SetCallback(std::shared_ptr<IcCallback> callback);
- 功能:设置图像分类回调
- 参数 :
callback
- 回调函数 - 返回值:操作结果码
- 异常处理:设置失败时返回错误码
销毁SDK
cpp
int32_t IcSdk::Destroy();
- 功能:销毁图像分类SDK实例
- 返回值:操作结果码
- 异常处理:销毁失败时返回错误码
5.2 数据交换接口
IPC接口定义:
- 使用系统能力接口进行跨进程通信
- 支持DataInfo结构体的序列化传输
- 提供异步回调机制
数据格式:
- Array模板类支持各种数据类型的传输
- DataInfo结构体支持二进制数据的传输
- ConfigInfo结构体支持配置信息的传输
5.3 接口调用时序图
应用程序 AI SDK Client Executor Server Executor AI Plugin 回调函数 Create() AieClientInit() 初始化请求 创建客户端会话 初始化成功 返回结果 创建成功 Prepare() AieClientPrepare() 准备请求 Prepare() 加载模型 准备完成 准备成功 返回结果 准备成功 SyncExecute() AieClientSyncProcess() 处理请求 SyncProcess() 执行算法 处理结果 返回结果 返回结果 OnResult() 结果回调 Destroy() AieClientDestroy() 销毁请求 Release() 释放资源 释放完成 销毁成功 返回结果 销毁成功 应用程序 AI SDK Client Executor Server Executor AI Plugin 回调函数
6. 总结
AI引擎模块是OpenHarmony系统AI子系统的核心组件,通过插件化架构设计为系统和应用提供统一的AI能力接口。该模块采用Client-Server分层架构,实现了算法能力的快速插件化集成和按需部署。
主要特点:
- 插件化架构设计,支持算法能力的快速集成
- 分层架构设计,职责清晰
- 统一的AI能力接口
- 支持同步和异步执行模式
- 高效的资源管理和生命周期管理
- 支持分布式AI能力调用
技术优势:
- 基于系统能力管理器的服务注册机制
- 完善的错误处理和异常管理
- 支持多种AI算法类型
- 灵活的插件扩展机制
- 高效的异步处理能力
该模块为OpenHarmony系统的AI能力提供了坚实的基础,支持各种AI算法需求,是构建智能应用的重要基础设施。通过插件化设计和统一接口,为开发者提供了灵活、高效、易用的AI开发能力。