# CANNKit_Codelab_NPUProfiling_cpp **Repository Path**: harmonyos_codelabs/cannkit_codelab_optimization_cpp ## Basic Information - **Project Name**: CANNKit_Codelab_NPUProfiling_cpp - **Description**: 本示例基于CANN Kit提供的维测调优能力,实现基于用户提供的AI模型和输入数据获取Profiling/Dump维测数据的示例,辅助开发者用于模型和单算子的性能调优与分析。 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2025-09-29 - **Last Updated**: 2026-05-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Obtain profiling/dump measurement data of the model based on the CANN Kit. ## Introduction This example demonstrates how to build a HarmonyOS application to run AI models and obtain Profiling/Dump data for performance analysis, supporting offline model tuning and output data visualization. Compilation requires dependencies on the CANN dynamic library **libhiai_foundation.so** and the AI domain public dynamic library **libneural_network_core.so**. ## Preview | **Home screen** | **Tuning data display** | **Tuning data display** | |:----------------------------------------------------:|:---------------------------------------------------:|-------------------------------------------------------| | | | | ## Instructions 1. Environmental Preparation
Installation [DevEco Studio](https://developer.huawei.com/consumer/en/deveco-studio/)。
2. Model Conversion
Complete [Offline Model Conversion](https://developer.huawei.com/consumer/en/doc/hiai-Guides/offline-model-conversion-0000001053807006) using the DDK tool.
3. File Transfer
Pre-create a directory with the same name as the application locally (defined by the bundleName in the app.json5 file), and use a specified command to preload the offline model and input files into the directory on the phone (in this example, "Download" is used as an example).
1)hdc file send com.huawei.canndemo/test.om /mnt/hmdfs/100/account/device_view/local/files/Docs/Download
2)hdc file send com.huawei.canndemo/input.bin /mnt/hmdfs/100/account/device_view/local/files/Docs/Download
4. Execute Reasoning
1)On the home screen of the phone, sequentially obtain the input bin file and the model file of the model, and select the measurement option.
2)Click "Run," set the tuning mode to "Profiling," and you can display the data by "Viewing Tuning Data"; if the tuning mode is set to "Dump," you can directly obtain the data from the phone's directory.
5. When you exit the demo, the model is automatically uninstalled.
## Project Directory ``` ├──entry/src/main/cpp │ ├──CMakeLists.txt // Native layer compilation configuration │ ├──HIAIModelManager.cpp // Interface implementation for HiAI model loading, initialization, and inference management │ └──HIAIModelManager.h // Encapsulation of HiAI model loading, initialization, and inference interfaces ├──entry/src/main/ets │ ├──constants │ │ └──CommonConstants.ets // Public constants │ ├──entryability │ │ └──EntryAbility.ets // Handles application startup, UI loading, and lifecycle management │ ├──model │ │ └──PathModel.ets // Application resource entity class │ ├──pages │ │ ├──Index.ets // Main UI │ │ └──TabsManager.ets │ ├──utils │ │ ├──DataUtils.ets // Model data utility class │ │ ├──DrawUtils.ets // Window management │ │ └──FileUtils.ets │ ├──view │ │ ├──DataShower.ets // Optimization data visualization component │ │ ├──DetailDialog.ets // Popup data display component │ │ └──FilePicker.ets // File and optimization options configuration component │ └──viewmodel │ ├──DetailData.ets // Main window entity class │ └──DialogData.ets // Sub-window entity class └──entry/src/main/resources // Referenced resource directory ``` ## Implementation Details This example shows the APIs used by the maintenance, commissioning, and optimization functions of the. - OH_NN_ReturnCode HMS_HiAIOptions_SetOmOptions(OH_NNCompilation* compilation, HiAI_OmType type, const char* outputDir); ``` typedef enum { /** Disables operating and maintenance. */ HIAI_OM_TYPE_OFF = 0, // closs OM Option /** Profiling type of operating and maintenance. */ HIAI_OM_TYPE_PROFILING = 1, // Enable performance analysis. /** Dump model level output. */ HIAI_OM_TYPE_DUMP = 2, // Enable model data export. } HiAI_OmType; ``` The functionality demonstrated also utilizes the following APIs defined in CANN and NNCore: ``` OH_NN_ReturnCode OH_NNDevice_GetAllDevicesID(const size_t **allDevicesID, uint32_t *deviceCount); void *OH_NNTensor_GetDataBuffer(const NN_Tensor *tensor); OH_NNCompilation *OH_NNCompilation_ConstructWithOfflineModelBuffer(const void *modelBuffer, size_t modelSize); OH_NN_ReturnCode OH_NNCompilation_SetDevice(OH_NNCompilation *compilation, size_t deviceID); void OH_NNCompilation_Destroy(OH_NNCompilation **compilation); NN_TensorDesc *OH_NNExecutor_CreateInputTensorDesc(const OH_NNExecutor *executor, size_t index); OH_NN_ReturnCode OH_NNTensorDesc_SetShape(NN_TensorDesc *tensorDesc, const int32_t *shape, size_t shapeLength); NN_TensorDesc *OH_NNExecutor_CreateOutputTensorDesc(const OH_NNExecutor *executor, size_t index); OH_NN_ReturnCode OH_NNExecutor_RunSync(OH_NNExecutor *executor, NN_Tensor *inputTensor[], size_t inputCount, NN_Tensor *outputTensor[], size_t outputCount); OH_NN_ReturnCode OH_NNTensor_GetSize(const NN_Tensor *tensor, size_t *size); ``` ## Required Permissions N/A ## Dependency N/A ## Constraints 1. This example is only supported on standard systems. Supported devices: Huawei Phone. 2. HarmonyOS system: HarmonyOS 6.0.0 Release or later. 3. DevEco Studio version: DevEco Studio 6.0.0 Release or later. 4. HarmonyOS SDK version: HarmonyOS 6.0.0 Release SDK or later.